Commit 45dde1be authored by Yizhen Fan's avatar Yizhen Fan Committed by fanyizhen1995
Browse files

ub: init netlink in ubcore



driver inclusion
category: feature
bugzilla: NA
CVE: NA

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

Support use netlink in ubcore.

The UB Core uses the netlink solution to implement the communication
between the kernel-mode driver and the user-mode DFX tool.

Signed-off-by: default avatarGuoxin Qian <qianguoxin@huawei.com>
Signed-off-by: default avatarYizhen Fan <fanyizhen@huawei.com>
parent eb7ae3e3
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
#

ubcore-objs := ubcore_main.o \
			ubcore_device.o
			ubcore_device.o	\
			ubcore_netlink.o

obj-$(CONFIG_UB) += ubcore.o
+8 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@

#include "ubcore_cmd.h"
#include "ubcore_log.h"
#include "ubcore_netlink.h"
#include <urma/ubcore_types.h>
#include <urma/ubcore_uapi.h>
#include "ubcore_priv.h"
@@ -606,10 +607,16 @@ static int __init ubcore_init(void)
	bitmap_zero(g_uasid_bitmap, UBCORE_MAX_UASID);
	set_bit(0, g_uasid_bitmap);

	if (ubcore_netlink_init() != 0) {
		ubcore_unregister_sysfs();
		return -1;
	}

	ret = ubcore_register_notifiers();
	if (ret != 0) {
		pr_err("Failed to register notifiers\n");
		ubcore_unregister_sysfs();
		ubcore_netlink_exit();
		return -1;
	}
	ubcore_log_info("ubcore module init success.\n");
@@ -619,6 +626,7 @@ static int __init ubcore_init(void)
static void __exit ubcore_exit(void)
{
	ubcore_unregister_notifiers();
	ubcore_netlink_exit();
	ubcore_unregister_sysfs();
	ubcore_log_info("ubcore module exits.\n");
}
+74 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. 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 netlink module
 * Author: Chen Wen, Yan Fangfang
 * Create: 2022-08-27
 * Note:
 * History: 2022-08-27: create file
 */

#include <net/sock.h>
#include <linux/kernel.h>
#include <linux/netlink.h>
#include <linux/list.h>
#include "ubcore_log.h"
#include "ubcore_netlink.h"

#define UBCORE_NL_TYPE 24 /* same with agent netlink type */
#define UBCORE_NL_TIMEOUT 10000 /* 10s */
#define UBCORE_NL_INVALID_PORT 0

struct sock *nl_sock;
static uint32_t g_agent_port = UBCORE_NL_INVALID_PORT; /* get agent pid */

static void ubcore_nl_cb_func(struct sk_buff *skb)
{
	struct nlmsghdr *nlh;

	nlh = nlmsg_hdr(skb);
	if (nlmsg_len(nlh) < sizeof(struct ubcore_nlmsg) || skb->len < nlh->nlmsg_len) {
		ubcore_log_err("Invalid nl msg received");
		return;
	}

	switch (nlh->nlmsg_type) {
	case UBCORE_NL_SET_AGENT_PID:
		g_agent_port = nlh->nlmsg_pid;
		break;
	default:
		ubcore_log_err("Unexpected nl msg type: %d received\n", nlh->nlmsg_type);
		break;
	}
}

int ubcore_netlink_init(void)
{
	/* create netlink socket */
	struct netlink_kernel_cfg cfg = { .input = ubcore_nl_cb_func };

	nl_sock = (struct sock *)netlink_kernel_create(&init_net, UBCORE_NL_TYPE, &cfg);
	if (nl_sock == NULL) {
		ubcore_log_err("Netlink_kernel_create error.\n");
		return -1;
	}
	return 0;
}

void ubcore_netlink_exit(void)
{
	if (nl_sock != NULL) {
		netlink_kernel_release(nl_sock);
		nl_sock = NULL;
	}
}
+67 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. 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 netlink head file
 * Author: Chen Wen
 * Create: 2022-08-27
 * Note:
 * History: 2022-08-27: Create file
 */

#ifndef UBCORE_NETLINK_H
#define UBCORE_NETLINK_H

#include <linux/netlink.h>
#include <urma/ubcore_types.h>

enum ubcore_nl_resp_status { UBCORE_NL_RESP_FAIL = -1, UBCORE_NL_RESP_SUCCESS = 0 };

enum ubcore_nlmsg_type {
	UBCORE_NL_CREATE_TP_REQ = NLMSG_MIN_TYPE, /* 0x10 */
	UBCORE_NL_CREATE_TP_RESP,
	UBCORE_NL_DESTROY_TP_REQ,
	UBCORE_NL_DESTROY_TP_RESP,
	UBCORE_NL_QUERY_TP_REQ,
	UBCORE_NL_QUERY_TP_RESP,
	UBCORE_NL_RESTORE_TP_REQ,
	UBCORE_NL_RESTORE_TP_RESP,
	UBCORE_NL_SET_AGENT_PID
};

struct ubcore_nlmsg {
	uint32_t nlmsg_seq;
	enum ubcore_nlmsg_type msg_type;
	enum ubcore_transport_type transport_type;
	union ubcore_eid src_eid;
	union ubcore_eid dst_eid;
	uint32_t payload_len;
	uint8_t payload[0];
} __packed;

struct ubcore_nl_session {
	struct ubcore_nlmsg *req;
	struct ubcore_nlmsg *resp;
	struct list_head node;
	struct kref kref;
	struct completion comp; /* Synchronization event of timeout sleep and thread wakeup */
};

static inline uint32_t ubcore_nlmsg_len(struct ubcore_nlmsg *msg)
{
	return sizeof(struct ubcore_nlmsg) + msg->payload_len;
}

int ubcore_netlink_init(void);
void ubcore_netlink_exit(void);

#endif