Commit 832e9fe8 authored by Yizhen Fan's avatar Yizhen Fan Committed by fanyizhen1995
Browse files

ub: ubcore add create/delete tp table api



driver inclusion
category: feature
bugzilla: NA
CVE: NA

--------------------------------

Ubcore add create/delete tp table api, including:
- ubcore_create_tptable: it will use ubcore_hash_table as the hash table.
- ubcore_destroy_tptable: destroy the tp hash table.
- ubcore_remove_tp_node: remove tp node in tp hash table.
- ubcore_destroy_tp: destroy the tp by calling the destroy_tp method
registered by driver.

Signed-off-by: default avatarGuoxin Qian <qianguoxin@huawei.com>
Signed-off-by: default avatarYizhen Fan <fanyizhen@huawei.com>
parent bbf7bf45
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -7,8 +7,9 @@ ubcore-objs := ubcore_main.o \
			ubcore_device.o	\
			ubcore_jetty.o \
			ubcore_umem.o \
			ubcore_tp.o \
			ubcore_hash_table.o \
			ubcore_tp.o \
			ubcore_tp_table.o \
			ubcore_netlink.o \
			ubcore_dp.o

+5 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@
#include <urma/ubcore_api.h>
#include "ubcore_priv.h"
#include "ubcore_hash_table.h"
#include "ubcore_tp.h"
#include "ubcore_tp_table.h"

static LIST_HEAD(g_client_list);
static LIST_HEAD(g_device_list);
@@ -322,6 +324,9 @@ static struct ubcore_ht_param g_ht_params[] = {

	[UBCORE_HT_JETTY] = { UBCORE_HASH_TABLE_SIZE, offsetof(struct ubcore_jetty, hnode),
			      offsetof(struct ubcore_jetty, id), sizeof(uint32_t), NULL, NULL },
	[UBCORE_HT_TP] = { UBCORE_HASH_TABLE_SIZE, offsetof(struct ubcore_tp_node, hnode),
			   offsetof(struct ubcore_tp_node, key), sizeof(struct ubcore_tp_key), NULL,
			   NULL },
};

static int ubcore_alloc_hash_tables(struct ubcore_device *dev)
+19 −1
Original line number Diff line number Diff line
@@ -22,8 +22,12 @@
#include <linux/uaccess.h>
#include <linux/random.h>
#include <linux/netdevice.h>
#include <urma/ubcore_api.h>
#include "ubcore_log.h"
#include "ubcore_netlink.h"
#include "ubcore_priv.h"
#include <urma/ubcore_uapi.h>
#include "ubcore_tp_table.h"
#include "ubcore_tp.h"

#define UB_PROTOCOL_HEAD_BYTES 313
#define UB_MTU_BITS_BASE_SHIFT 7
@@ -54,6 +58,20 @@ enum ubcore_mtu ubcore_get_mtu(int mtu)
}
EXPORT_SYMBOL(ubcore_get_mtu);

int ubcore_destroy_tp(struct ubcore_tp *tp)
{
	if (!ubcore_have_tp_ops(tp->ub_dev)) {
		ubcore_log_err("TP ops is NULL");
		return -1;
	}

	if (tp->peer_ext.len > 0 && tp->peer_ext.addr != 0)
		kfree((void *)tp->peer_ext.addr);

	return tp->ub_dev->ops->destroy_tp(tp);
}
EXPORT_SYMBOL(ubcore_destroy_tp);

struct ubcore_tp *ubcore_create_vtp(struct ubcore_device *dev, const union ubcore_eid *remote_eid,
				    enum ubcore_transport_mode trans_mode,
				    struct ubcore_udata *udata)
+3 −0
Original line number Diff line number Diff line
@@ -29,4 +29,7 @@ static inline bool ubcore_have_tp_ops(const struct ubcore_device *dev)
	return (dev != NULL && dev->ops->create_tp != NULL && dev->ops->modify_tp != NULL &&
		dev->ops->destroy_tp != NULL);
}

/* Called when clear tp table */
int ubcore_destroy_tp(struct ubcore_tp *tp);
#endif
+91 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * Description: ubcore tp table implementation
 * Author: Yan Fangfang
 * Create: 2023-02-09
 * Note:
 * History: 2023-02-09: Create file
 */

#include <linux/slab.h>
#include "ubcore_log.h"
#include "ubcore_priv.h"
#include "ubcore_tp.h"
#include "ubcore_tp_table.h"

void ubcore_init_tp_key_jetty_id(struct ubcore_tp_key *key, const struct ubcore_jetty_id *jetty_id)
{
	memset(key, 0, sizeof(struct ubcore_tp_key));
	key->key_type = UBCORE_TP_KEY_JETTY_ID;
	key->jetty_id = *jetty_id;
}

void ubcore_remove_tp_node(struct ubcore_hash_table *ht, struct ubcore_tp_node *tp_node)
{
	if (tp_node == NULL)
		return;

	ubcore_hash_table_remove(ht, &tp_node->hnode);
	kfree(tp_node);
}

struct ubcore_hash_table *ubcore_create_tptable(void)
{
	struct ubcore_ht_param p = { .size = UBCORE_HASH_TABLE_SIZE,
				     .node_offset = offsetof(struct ubcore_tp_node, hnode),
				     .key_offset = offsetof(struct ubcore_tp_node, key),
				     .key_size = sizeof(struct ubcore_tp_key),
				     .cmp_f = NULL,
				     .free_f = NULL };
	struct ubcore_hash_table *ht;

	ht = kcalloc(1, sizeof(struct ubcore_hash_table), GFP_KERNEL);
	if (ht == NULL)
		return NULL;

	if (ubcore_hash_table_alloc(ht, &p) != 0) {
		kfree(ht);
		ubcore_log_err("Failed to calloc jfs tp hash table");
		return NULL;
	}
	return ht;
}

static void ubcore_free_tp_node(void *obj)
{
	struct ubcore_tp_node *tp_node = (struct ubcore_tp_node *)obj;
	(void)ubcore_destroy_tp(tp_node->tp);
	kfree(tp_node);
}

static void ubcore_tptable_release(struct kref *kref)
{
	struct ubcore_hash_table *ht = container_of(kref, struct ubcore_hash_table, kref);

	kfree(ht);
}

void ubcore_destroy_tptable(struct ubcore_hash_table **pp_ht)
{
	struct ubcore_hash_table *ht;

	if (pp_ht == NULL || *pp_ht == NULL)
		return;

	ht = *pp_ht;
	*pp_ht = NULL;
	ubcore_hash_table_free_with_cb(ht, ubcore_free_tp_node);
	/* pair with kref_init */
	(void)kref_put(&ht->kref, ubcore_tptable_release);
}
Loading