From 1ba7b82d8f933550dd1552143b77d077b7e4f40a Mon Sep 17 00:00:00 2001
From: veega <zhuweijia@huawei.com>
Date: Wed, 9 Aug 2023 20:04:27 +0800
Subject: [PATCH] hikptool: add new cmd support query ncsi dfx info

Add nic_ncsi cmd for query nic port number of NCSI packets processed by software.

Signed-off-by: veega <zhuweijia@huawei.com>
---
 net/hikp_net_lib.h               |   5 ++
 net/nic/nic_ncsi/hikp_nic_ncsi.c | 116 +++++++++++++++++++++++++++++++
 net/nic/nic_ncsi/hikp_nic_ncsi.h |  57 +++++++++++++++
 3 files changed, 178 insertions(+)
 create mode 100644 net/nic/nic_ncsi/hikp_nic_ncsi.c
 create mode 100644 net/nic/nic_ncsi/hikp_nic_ncsi.h

diff --git a/net/hikp_net_lib.h b/net/hikp_net_lib.h
index af0a51d..ed984ce 100644
--- a/net/hikp_net_lib.h
+++ b/net/hikp_net_lib.h
@@ -78,6 +78,7 @@ enum nic_cmd_type {
 	GET_FD_INFO_CMD,
 	GET_FEC_INFO_CMD,
 	GET_GRO_INFO_CMD,
+	GET_NCSI_INFO_CMD,
 };
 
 enum roh_cmd_type {
@@ -107,6 +108,10 @@ enum ub_cmd_type {
 	GET_UB_BASIC_INFO_CMD,
 };
 
+enum nic_get_ncsi_sub_cmd {
+	NIC_NCSI_GET_DFX_INFO,
+};
+
 #define HIKP_MAX_PF_NUM 8
 #define HIKP_NIC_MAX_FUNC_NUM   256
 
diff --git a/net/nic/nic_ncsi/hikp_nic_ncsi.c b/net/nic/nic_ncsi/hikp_nic_ncsi.c
new file mode 100644
index 0000000..d09e16f
--- /dev/null
+++ b/net/nic/nic_ncsi/hikp_nic_ncsi.c
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2022 Hisilicon Technologies Co., Ltd.
+ * Hikptool is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ *
+ * See the Mulan PSL v2 for more details.
+ */
+
+#include <unistd.h>
+#include "tool_cmd.h"
+#include "hikp_net_lib.h"
+#include "hikptdev_plug.h"
+#include "hikp_nic_ncsi.h"
+
+static struct nic_ncsi_cmd_info g_ncsi_cmd_info = {0};
+
+static void nic_ncsi_cmd_print_dfx_info(struct nic_ncsi_cmd_resp *ncsi_info)
+{
+	printf("port ncsi: %s\n", ncsi_info->ncsi_en ? "enable" : "disable");
+	if (!ncsi_info->ncsi_en)
+		return;    /* ncsi not enable do not print dfx info */
+
+	printf("processing packet statistics\n");
+	printf("\tncsi_control_total: %u\n", ncsi_info->ncsi_dfx.ncsi_control_total);
+	printf("\tncsi_eth_to_ub_total: %u\n", ncsi_info->ncsi_dfx.ncsi_eth_to_ub_total);
+	printf("\tncsi_ub_to_eth_total: %u\n", ncsi_info->ncsi_dfx.ncsi_ub_to_eth_total);
+	printf("\tncsi_control_good: %u\n", ncsi_info->ncsi_dfx.ncsi_control_good);
+	printf("\tncsi_eth_to_ub_good: %u\n", ncsi_info->ncsi_dfx.ncsi_eth_to_ub_good);
+	printf("\tncsi_ub_to_eth_good: %u\n", ncsi_info->ncsi_dfx.ncsi_ub_to_eth_good);
+
+	printf("\tncsi_eth_to_ub_arp: %u\n", ncsi_info->ncsi_dfx.ncsi_eth_to_ub_arp);
+	printf("\tncsi_eth_to_ub_free_arp: %u\n", ncsi_info->ncsi_dfx.ncsi_eth_to_ub_free_arp);
+	printf("\tncsi_eth_to_ub_ipv6_ra: %u\n", ncsi_info->ncsi_dfx.ncsi_eth_to_ub_ipv6_ra);
+	printf("\tncsi_eth_to_ub_dhcpv4: %u\n", ncsi_info->ncsi_dfx.ncsi_eth_to_ub_dhcpv4);
+	printf("\tncsi_eth_to_ub_dhcpv6: %u\n", ncsi_info->ncsi_dfx.ncsi_eth_to_ub_dhcpv6);
+	printf("\tncsi_eth_to_ub_lldp: %u\n", ncsi_info->ncsi_dfx.ncsi_eth_to_ub_lldp);
+
+	printf("\tncsi_ub_to_eth_ipv4: %u\n", ncsi_info->ncsi_dfx.ncsi_ub_to_eth_ipv4);
+	printf("\tncsi_ub_to_eth_ipv6: %u\n", ncsi_info->ncsi_dfx.ncsi_ub_to_eth_ipv6);
+	printf("\tncsi_ub_to_eth_ipnotify: %u\n", ncsi_info->ncsi_dfx.ncsi_ub_to_eth_ipnotify);
+	printf("\tncsi_ub_to_eth_dhcpv4: %u\n", ncsi_info->ncsi_dfx.ncsi_ub_to_eth_dhcpv4);
+	printf("\tncsi_ub_to_eth_dhcpv6: %u\n", ncsi_info->ncsi_dfx.ncsi_ub_to_eth_dhcpv6);
+	printf("\tncsi_ub_to_eth_lldp: %u\n", ncsi_info->ncsi_dfx.ncsi_ub_to_eth_lldp);
+}
+
+static void nic_ncsi_cmd_execute(struct major_cmd_ctrl *self)
+{
+	struct hikp_cmd_ret *cmd_resp = NULL;
+	struct hikp_cmd_header req_header = {0};
+	struct nic_ncsi_cmd_req ncsi_req = {0};
+
+	if (!g_ncsi_cmd_info.port_flag) {
+		self->err_no = -EINVAL;
+		snprintf(self->err_str, sizeof(self->err_str), "Need port id.");
+		return;
+	}
+
+	memcpy(&ncsi_req.bdf, &g_ncsi_cmd_info.target.bdf, sizeof(ncsi_req.bdf));
+	hikp_cmd_init(&req_header, NIC_MOD, GET_NCSI_INFO_CMD, NIC_NCSI_GET_DFX_INFO);
+	cmd_resp = hikp_cmd_alloc(&req_header, &ncsi_req, sizeof(ncsi_req));
+	self->err_no = hikp_rsp_normal_check(cmd_resp);
+	if (self->err_no) {
+		snprintf(self->err_str, sizeof(self->err_str), "Get ncsi dfx info failed.");
+		goto ERR_OUT;
+	}
+
+	nic_ncsi_cmd_print_dfx_info((struct nic_ncsi_cmd_resp *)cmd_resp->rsp_data);
+
+ERR_OUT:
+	free(cmd_resp);
+	cmd_resp = NULL;
+}
+
+static int nic_ncsi_cmd_get_port_info(struct major_cmd_ctrl *self, const char *argv)
+{
+	self->err_no = tool_check_and_get_valid_bdf_id(argv, &g_ncsi_cmd_info.target);
+	if (self->err_no) {
+		snprintf(self->err_str, sizeof(self->err_str), "Unknown device %s.", argv);
+		return self->err_no;
+	}
+	g_ncsi_cmd_info.port_flag = true;
+
+	return 0;
+}
+
+static int nic_ncsi_cmd_show_help(struct major_cmd_ctrl *self, const char *argv)
+{
+	printf("\n  Usage: %s %s\n", self->cmd_ptr->name, "-i <interface>");
+	printf("\n         %s\n", self->cmd_ptr->help_info);
+	printf("\n  Options:\n\n");
+	printf("    %s, %-25s %s\n", "-h", "--help", "display this help and exit");
+	printf("    %s, %-25s %s\n", "-i", "--interface=<interface>",
+	       "device target or bdf id, e.g. eth0~3 or 0000:35:00.0");
+	printf("\n");
+
+	return 0;
+}
+
+static void cmd_nic_get_ncsi_init(void)
+{
+	struct major_cmd_ctrl *major_cmd = get_major_cmd();
+
+	g_ncsi_cmd_info.port_flag = false;
+	major_cmd->option_count = 0;
+	major_cmd->execute = nic_ncsi_cmd_execute;
+
+	cmd_option_register("-h", "--help",       false, nic_ncsi_cmd_show_help);
+	cmd_option_register("-i", "--interface",  true,  nic_ncsi_cmd_get_port_info);
+}
+
+HIKP_CMD_DECLARE("nic_ncsi", "query nic port ncsi information", cmd_nic_get_ncsi_init);
diff --git a/net/nic/nic_ncsi/hikp_nic_ncsi.h b/net/nic/nic_ncsi/hikp_nic_ncsi.h
new file mode 100644
index 0000000..a391d20
--- /dev/null
+++ b/net/nic/nic_ncsi/hikp_nic_ncsi.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2022 Hisilicon Technologies Co., Ltd.
+ * Hikptool is licensed under Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *          http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
+ *
+ * See the Mulan PSL v2 for more details.
+ */
+
+#ifndef HIKP_NIC_NCSI_H
+#define HIKP_NIC_NCSI_H
+
+#include "hikp_net_lib.h"
+
+struct nic_ncsi_dfx_info {
+	uint16_t ncsi_control_total;
+	uint16_t ncsi_eth_to_ub_total;
+	uint16_t ncsi_ub_to_eth_total;
+	uint16_t ncsi_control_good;
+	uint16_t ncsi_eth_to_ub_good;
+	uint16_t ncsi_ub_to_eth_good;
+	uint16_t ncsi_eth_to_ub_arp;
+	uint16_t ncsi_eth_to_ub_free_arp;
+	uint16_t ncsi_eth_to_ub_ipv6_ra;
+	uint16_t ncsi_eth_to_ub_dhcpv4;
+	uint16_t ncsi_eth_to_ub_dhcpv6;
+	uint16_t ncsi_eth_to_ub_lldp;
+	uint16_t ncsi_ub_to_eth_ipv4;
+	uint16_t ncsi_ub_to_eth_ipv6;
+	uint16_t ncsi_ub_to_eth_ipnotify;
+	uint16_t ncsi_ub_to_eth_dhcpv4;
+	uint16_t ncsi_ub_to_eth_dhcpv6;
+	uint16_t ncsi_ub_to_eth_lldp;
+};
+
+struct nic_ncsi_cmd_resp {
+	uint8_t ncsi_en;
+	uint8_t rsv0[3];
+	struct nic_ncsi_dfx_info ncsi_dfx;
+	uint32_t rsv1[50];    /* max resp data: 240 Bytes */
+};
+
+struct nic_ncsi_cmd_req {
+	struct bdf_t bdf;
+	uint32_t rsv0[30];    /* max req data: 128 Bytes */
+};
+
+struct nic_ncsi_cmd_info {
+	struct tool_target target;
+	bool port_flag;
+};
+
+#endif  /* HIKP_NIC_NCSI_H */
-- 
2.36.1.windows.1

