From 43db6b06b49696cb4fd9556c975b2cf67da4b86b Mon Sep 17 00:00:00 2001
From: veega2022 <zhuweijia@huawei.com>
Date: Thu, 11 May 2023 09:00:25 +0800
Subject: [PATCH 12/15] Complete the developing of hikptool ub_info function

The hikptool ub_info command is submitted for the first time.
This command is used to query whether the cloud attack defense function is enabled and query the single-port multi-stack working mode.

Signed-off-by: Jianqiang Li <lijianqiang16@huawei.com>
---
 net/ub/ub_info/hikp_ub_info.c | 96 +++++++++++++++++++++++++++++++++++
 net/ub/ub_info/hikp_ub_info.h | 37 ++++++++++++++
 2 files changed, 133 insertions(+)
 create mode 100644 net/ub/ub_info/hikp_ub_info.c
 create mode 100644 net/ub/ub_info/hikp_ub_info.h

diff --git a/net/ub/ub_info/hikp_ub_info.c b/net/ub/ub_info/hikp_ub_info.c
new file mode 100644
index 0000000..3b354dd
--- /dev/null
+++ b/net/ub/ub_info/hikp_ub_info.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2023 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 "hikp_ub_info.h"
+
+static struct ub_info_param g_ub_info_param = { 0 };
+
+static int hikp_ub_info_help(struct major_cmd_ctrl *self, const char *argv)
+{
+	printf("\n  Usage: %s %s\n", self->cmd_ptr->name, "-i <interface>\n");
+	printf("\n         %s\n", self->cmd_ptr->help_info);
+	printf("  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. ubn0 or 0000:35:00.0");
+	printf("\n");
+	return 0;
+}
+
+static int hikp_ub_info_target(struct major_cmd_ctrl *self, const char *argv)
+{
+	self->err_no = tool_check_and_get_valid_bdf_id(argv, &(g_ub_info_param.target));
+	if (self->err_no != 0) {
+		snprintf(self->err_str, sizeof(self->err_str), "Unknown device %s.", argv);
+		return self->err_no;
+	}
+
+	return 0;
+}
+
+static void hikp_ub_basic_info_show(const struct ub_info_rsp *info)
+{
+	printf("%-32s : %s\n", "cloud-based attack defense", info->cloud_mode ? "YES" : "NO");
+	printf("%-32s : %s\n", "pf working mode", info->pf_drv_type ? "UB" : "RDMA");
+	printf("%-32s : %s\n", "vf working mode", info->vf_drv_type ? "UB" : "RDMA");
+}
+
+static int hikp_ub_query_basic_info(const struct bdf_t *bdf)
+{
+	struct hikp_cmd_header header = { 0 };
+	struct ub_info_req_para req = { 0 };
+	struct hikp_cmd_ret *cmd_ret;
+	struct ub_info_rsp *rsp;
+
+	req.bdf = *bdf;
+	hikp_cmd_init(&header, UB_MOD, GET_UB_BASIC_INFO_CMD, UB_BASIC_INFO_DUMP);
+	cmd_ret = hikp_cmd_alloc(&header, &req, sizeof(req));
+	if (cmd_ret == NULL || cmd_ret->status != 0) {
+		free(cmd_ret);
+		cmd_ret = NULL;
+		return -EIO;
+	}
+
+	rsp = (struct ub_info_rsp *)cmd_ret->rsp_data;
+	hikp_ub_basic_info_show(rsp);
+
+	free(cmd_ret);
+	cmd_ret = NULL;
+	return 0;
+}
+
+static void hikp_ub_info_cmd_execute(struct major_cmd_ctrl *self)
+{
+	struct bdf_t *bdf = &g_ub_info_param.target.bdf;
+	int ret;
+
+	ret = hikp_ub_query_basic_info(bdf);
+	if (ret != 0) {
+		snprintf(self->err_str, sizeof(self->err_str), "fail to get basic info.");
+		self->err_no = ret;
+		return;
+	}
+}
+
+static void cmd_ub_info_init(void)
+{
+	struct major_cmd_ctrl *major_cmd = get_major_cmd();
+
+	major_cmd->option_count = 0;
+	major_cmd->execute = hikp_ub_info_cmd_execute;
+
+	cmd_option_register("-h", "--help", false, hikp_ub_info_help);
+	cmd_option_register("-i", "--interface", true, hikp_ub_info_target);
+}
+
+HIKP_CMD_DECLARE("ub_info", "get ub basic information", cmd_ub_info_init);
diff --git a/net/ub/ub_info/hikp_ub_info.h b/net/ub/ub_info/hikp_ub_info.h
new file mode 100644
index 0000000..a9cb858
--- /dev/null
+++ b/net/ub/ub_info/hikp_ub_info.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2023 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_UB_INFO_H
+#define HIKP_UB_INFO_H
+
+#include "hikp_net_lib.h"
+
+enum ub_info_sub_cmd_type {
+	UB_BASIC_INFO_DUMP = 0,
+};
+
+struct ub_info_param {
+	struct tool_target target;
+};
+
+struct ub_info_req_para {
+	struct bdf_t bdf;
+};
+
+struct ub_info_rsp {
+	uint32_t cloud_mode;
+	uint32_t pf_drv_type;
+	uint32_t vf_drv_type;
+};
+
+#endif /* HIKP_UB_INFO_H */
-- 
2.36.1.windows.1

