Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include <getopt.h>
12 : #include <errno.h>
13 : #include "user_log.h"
14 : #include "securec.h"
15 : #include "dl_hal_function.h"
16 : #include "ra_adp.h"
17 : #include "param.h"
18 :
19 38 : STATIC int HccpParamParseId(const char *input, int *id)
20 : {
21 : int ret;
22 :
23 38 : ret = sscanf_s(input, "%d", id);
24 38 : CHK_PRT_RETURN(ret != 1, hccp_err("get id from str failed, ret %d", ret), -EINVAL);
25 :
26 23 : return 0;
27 : }
28 :
29 6 : STATIC int HccpParseLogicId(const char *input, struct HccpInitParam *param)
30 : {
31 : int ret;
32 :
33 6 : ret = HccpParamParseId(input, ¶m->logicId);
34 6 : CHK_PRT_RETURN(ret, hccp_err("hccp_param_parse_id failed"), ret);
35 :
36 4 : ret = DlDrvDeviceGetPhyIdByIndex(param->logicId, &(param->chipId));
37 4 : CHK_PRT_RETURN(ret != 0 || param->chipId > HCCP_MAX_CHIP_ID,
38 : hccp_err("get chip id failed, ret:%d, chipId:%u > %u", ret, param->chipId, HCCP_MAX_CHIP_ID), -EINVAL);
39 :
40 0 : hccp_info("logic_id from TSD is [%d], chipId[%u]", param->logicId, param->chipId);
41 0 : return 0;
42 : }
43 :
44 8 : STATIC int HccpParsePid(const char *input, struct HccpInitParam *param)
45 : {
46 : int ret;
47 :
48 8 : ret = HccpParamParseId(input, ¶m->pid);
49 8 : CHK_PRT_RETURN(ret, hccp_err("hccp parse pid failed"), ret);
50 :
51 5 : CHK_PRT_RETURN(param->pid <= 0, hccp_err("pid:%d <= 0", param->pid), -EINVAL);
52 3 : hccp_info("pid from TSD is [%d]", param->pid);
53 3 : return 0;
54 : }
55 :
56 1 : STATIC int HccpParsePidSign(const char *input, struct HccpInitParam *param)
57 : {
58 : (void)input;
59 : (void)param;
60 : // no need to parse pid sign, skip
61 1 : return 0;
62 : }
63 :
64 5 : STATIC int HccpParseLogLevel(const char *input, struct HccpInitParam *param)
65 : {
66 : int ret;
67 :
68 5 : ret = HccpParamParseId(input, ¶m->logLevel);
69 5 : CHK_PRT_RETURN(ret, hccp_err("hccp parse log level failed, ret[%d]", ret), ret);
70 3 : hccp_info("log_level from TSD is [%d]", param->logLevel);
71 3 : return 0;
72 : }
73 :
74 6 : STATIC int HccpParseHdcType(const char *input, struct HccpInitParam *param)
75 : {
76 : int ret;
77 :
78 6 : ret = HccpParamParseId(input, ¶m->hdcType);
79 6 : if (ret != 0 || (param->hdcType != HDC_SERVICE_TYPE_RDMA && param->hdcType != HDC_SERVICE_TYPE_RDMA_V2)) {
80 3 : hccp_warn("parse hdcType unsuccessful ret:%d or hdc_type[%d] invalid. set to default hdc_type[%d]", ret,
81 : param->hdcType, HDC_SERVICE_TYPE_RDMA);
82 3 : param->hdcType = HDC_SERVICE_TYPE_RDMA;
83 : }
84 :
85 6 : hccp_info("hdc_type from TSD is [%d]", param->hdcType);
86 6 : return 0;
87 : }
88 :
89 5 : STATIC int HccpParseWhiteListStatus(const char *input, struct HccpInitParam *param)
90 : {
91 : int ret;
92 :
93 5 : ret = HccpParamParseId(input, (int *)¶m->whiteListStatus);
94 5 : if (ret != 0) {
95 2 : param->whiteListStatus = WHITE_LIST_ENABLE;
96 2 : hccp_warn("parse whiteListStatus unsuccessful ret:%d. set to default [%u]", ret, param->whiteListStatus);
97 : }
98 :
99 5 : param->whiteListStatus = (param->whiteListStatus != 0) ? WHITE_LIST_ENABLE : WHITE_LIST_DISABLE;
100 5 : hccp_info("white_list_status from TSD is [%u]", param->whiteListStatus);
101 5 : return 0;
102 : }
103 :
104 4 : STATIC int HccpParseBackupPhyid(const char *input, struct HccpInitParam *param)
105 : {
106 4 : unsigned int backupPhyId = 0;
107 : int ret;
108 :
109 4 : ret = HccpParamParseId(input, (int *)(void *)&backupPhyId);
110 4 : if (ret != 0) {
111 2 : hccp_warn("parse backup phy_id unsuccessful ret:%d", ret);
112 2 : return 0;
113 : }
114 :
115 2 : ret = DlDrvGetLocalDevIdByHostDevId(backupPhyId, ¶m->backupChipId);
116 2 : if (ret != 0) {
117 2 : hccp_warn("dl_drv_get_local_dev_id_by_host_dev_id unsuccessful ret:%d, backupPhyId:%u", ret, backupPhyId);
118 2 : return 0;
119 : }
120 :
121 0 : hccp_info("backup_phy_id from TSD is [%u], backupChipId[%u]", backupPhyId, param->backupChipId);
122 0 : param->backupFlag = true;
123 0 : return 0;
124 : }
125 :
126 8 : int HccpParamParse(int argc, char *argv[], struct HccpInitParam *param)
127 : {
128 : static struct option longOpts[] = {
129 : {DEVID_PREFIX, required_argument, NULL, HCCP_ARGC_DEV},
130 : {PID_PREFIX, required_argument, NULL, HCCP_ARGC_PID},
131 : {PID_SIGN_PREFIX, required_argument, NULL, HCCP_ARGC_PID_SIGN},
132 : {LOG_LEVEL_PREFIX, required_argument, NULL, HCCP_ARGC_LOG_LEVEL},
133 : {HDC_TYPE_PREFIX, required_argument, NULL, HCCP_ARGC_HDC_TYPE},
134 : {WHITE_LIST_STATUS_PREFIX, required_argument, NULL, HCCP_ARGC_WHITE_LIST_STATUS},
135 : {BACKUP_PHYID_PREFIX, required_argument, NULL, HCCP_ARGC_BACKUP_PHYID},
136 : {0, no_argument, NULL, HCCP_ARGC_NUM},
137 : };
138 : static struct ParamHandle paramHandles[] = {
139 : {HccpParseLogicId, true, HCCP_ARGC_DEV},
140 : {HccpParsePid, true, HCCP_ARGC_PID},
141 : {HccpParsePidSign, false, HCCP_ARGC_PID_SIGN},
142 : {HccpParseLogLevel, true, HCCP_ARGC_LOG_LEVEL},
143 : {HccpParseHdcType, false, HCCP_ARGC_HDC_TYPE},
144 : {HccpParseWhiteListStatus, false, HCCP_ARGC_WHITE_LIST_STATUS},
145 : {HccpParseBackupPhyid, false, HCCP_ARGC_BACKUP_PHYID},
146 : {NULL, false, HCCP_ARGC_NUM},
147 : };
148 8 : const char *optstring = "";
149 8 : int requiredCnt = 0;
150 : int optIdx;
151 : int ret;
152 :
153 : // set default attr
154 8 : param->hdcType = HDC_SERVICE_TYPE_RDMA;
155 8 : param->whiteListStatus = WHITE_LIST_ENABLE;
156 8 : param->backupFlag = false;
157 :
158 15 : while (getopt_long(argc, argv, optstring, longOpts, &optIdx) != -1) {
159 : // unrecognized option, skip to parse
160 12 : if (optarg == NULL) {
161 1 : continue;
162 : }
163 :
164 11 : CHK_PRT_RETURN(optIdx < 0 || optIdx >= HCCP_ARGC_NUM || paramHandles[optIdx].optHandle == NULL,
165 : hccp_err("opt_idx:%d invalid, valid range[0, %d), errno:%d", optIdx, HCCP_ARGC_NUM, errno), -EINVAL);
166 :
167 11 : CHK_PRT_RETURN(paramHandles[optIdx].optVal != optIdx,
168 : hccp_err("opt_val:%d != opt_idx:%d", paramHandles[optIdx].optVal, optIdx), -EINVAL);
169 :
170 11 : ret = paramHandles[optIdx].optHandle(optarg, param);
171 11 : CHK_PRT_RETURN(ret != 0, hccp_err("parse param failed, optIdx:%d, ret:%d", optIdx, ret), ret);
172 :
173 6 : if (paramHandles[optIdx].isDefaultRequired) {
174 3 : requiredCnt++;
175 : }
176 : }
177 :
178 3 : CHK_PRT_RETURN(requiredCnt != HCCP_DEFAULT_REQUIRED_ARGC_NUM,
179 : hccp_err("param num error, requiredCnt:%d, argc:%d", requiredCnt, HCCP_DEFAULT_REQUIRED_ARGC_NUM), -EINVAL);
180 0 : return 0;
181 : }
|