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 : // no need to parse pid sign, skip
59 1 : return 0;
60 : }
61 :
62 5 : STATIC int HccpParseLogLevel(const char *input, struct HccpInitParam *param)
63 : {
64 : int ret;
65 :
66 5 : ret = HccpParamParseId(input, ¶m->logLevel);
67 5 : CHK_PRT_RETURN(ret, hccp_err("hccp parse log level failed, ret[%d]", ret), ret);
68 3 : hccp_info("log_level from TSD is [%d]", param->logLevel);
69 3 : return 0;
70 : }
71 :
72 6 : STATIC int HccpParseHdcType(const char *input, struct HccpInitParam *param)
73 : {
74 : int ret;
75 :
76 6 : ret = HccpParamParseId(input, ¶m->hdcType);
77 6 : if (ret != 0 || (param->hdcType != HDC_SERVICE_TYPE_RDMA && param->hdcType != HDC_SERVICE_TYPE_RDMA_V2)) {
78 3 : hccp_warn("parse hdcType unsuccessful ret:%d or hdc_type[%d] invalid. set to default hdc_type[%d]",
79 : ret, param->hdcType, HDC_SERVICE_TYPE_RDMA);
80 3 : param->hdcType = HDC_SERVICE_TYPE_RDMA;
81 : }
82 :
83 6 : hccp_info("hdc_type from TSD is [%d]", param->hdcType);
84 6 : return 0;
85 : }
86 :
87 5 : STATIC int HccpParseWhiteListStatus(const char *input, struct HccpInitParam *param)
88 : {
89 : int ret;
90 :
91 5 : ret = HccpParamParseId(input, (int *)¶m->whiteListStatus);
92 5 : if (ret != 0) {
93 2 : param->whiteListStatus = WHITE_LIST_ENABLE;
94 2 : hccp_warn("parse whiteListStatus unsuccessful ret:%d. set to default [%u]", ret, param->whiteListStatus);
95 : }
96 :
97 5 : param->whiteListStatus = (param->whiteListStatus != 0) ? WHITE_LIST_ENABLE : WHITE_LIST_DISABLE;
98 5 : hccp_info("white_list_status from TSD is [%u]", param->whiteListStatus);
99 5 : return 0;
100 : }
101 :
102 4 : STATIC int HccpParseBackupPhyid(const char *input, struct HccpInitParam *param)
103 : {
104 4 : unsigned int backupPhyId = 0;
105 : int ret;
106 :
107 4 : ret = HccpParamParseId(input, (int *)(void *)&backupPhyId);
108 4 : if (ret != 0) {
109 2 : hccp_warn("parse backup phy_id unsuccessful ret:%d", ret);
110 2 : return 0;
111 : }
112 :
113 2 : ret = DlDrvGetLocalDevIdByHostDevId(backupPhyId, ¶m->backupChipId);
114 2 : if (ret != 0) {
115 2 : hccp_warn("dl_drv_get_local_dev_id_by_host_dev_id unsuccessful ret:%d, backupPhyId:%u", ret, backupPhyId);
116 2 : return 0;
117 : }
118 :
119 0 : hccp_info("backup_phy_id from TSD is [%u], backupChipId[%u]", backupPhyId, param->backupChipId);
120 0 : param->backupFlag = true;
121 0 : return 0;
122 : }
123 :
124 8 : int HccpParamParse(int argc, char *argv[], struct HccpInitParam *param)
125 : {
126 : static struct option longOpts[] = {
127 : {DEVID_PREFIX, required_argument, NULL, HCCP_ARGC_DEV},
128 : {PID_PREFIX, required_argument, NULL, HCCP_ARGC_PID},
129 : {PID_SIGN_PREFIX, required_argument, NULL, HCCP_ARGC_PID_SIGN},
130 : {LOG_LEVEL_PREFIX, required_argument, NULL, HCCP_ARGC_LOG_LEVEL},
131 : {HDC_TYPE_PREFIX, required_argument, NULL, HCCP_ARGC_HDC_TYPE},
132 : {WHITE_LIST_STATUS_PREFIX, required_argument, NULL, HCCP_ARGC_WHITE_LIST_STATUS},
133 : {BACKUP_PHYID_PREFIX, required_argument, NULL, HCCP_ARGC_BACKUP_PHYID},
134 : {0, no_argument, NULL, HCCP_ARGC_NUM},
135 : };
136 : static struct ParamHandle paramHandles[] = {
137 : {HccpParseLogicId, true, HCCP_ARGC_DEV},
138 : {HccpParsePid, true, HCCP_ARGC_PID},
139 : {HccpParsePidSign, false, HCCP_ARGC_PID_SIGN},
140 : {HccpParseLogLevel, true, HCCP_ARGC_LOG_LEVEL},
141 : {HccpParseHdcType, false, HCCP_ARGC_HDC_TYPE},
142 : {HccpParseWhiteListStatus, false, HCCP_ARGC_WHITE_LIST_STATUS},
143 : {HccpParseBackupPhyid, false, HCCP_ARGC_BACKUP_PHYID},
144 : {NULL, false, HCCP_ARGC_NUM},
145 : };
146 8 : const char *optstring = "";
147 8 : int requiredCnt = 0;
148 : int optIdx;
149 : int ret;
150 :
151 : // set default attr
152 8 : param->hdcType = HDC_SERVICE_TYPE_RDMA;
153 8 : param->whiteListStatus = WHITE_LIST_ENABLE;
154 8 : param->backupFlag = false;
155 :
156 15 : while (getopt_long(argc, argv, optstring, longOpts, &optIdx) != -1) {
157 : // unrecognized option, skip to parse
158 12 : if (optarg == NULL) {
159 1 : continue;
160 : }
161 :
162 11 : CHK_PRT_RETURN(optIdx < 0 || optIdx >= HCCP_ARGC_NUM || paramHandles[optIdx].optHandle == NULL,
163 : hccp_err("opt_idx:%d invalid, valid range[0, %d), errno:%d", optIdx, HCCP_ARGC_NUM, errno),-EINVAL);
164 :
165 11 : CHK_PRT_RETURN(paramHandles[optIdx].optVal != optIdx, hccp_err("opt_val:%d != opt_idx:%d",
166 : paramHandles[optIdx].optVal, optIdx), -EINVAL);
167 :
168 11 : ret = paramHandles[optIdx].optHandle(optarg, param);
169 11 : CHK_PRT_RETURN(ret != 0, hccp_err("parse param failed, optIdx:%d, ret:%d", optIdx, ret), ret);
170 :
171 6 : if (paramHandles[optIdx].isDefaultRequired) {
172 3 : requiredCnt++;
173 : }
174 : }
175 :
176 3 : CHK_PRT_RETURN(requiredCnt != HCCP_DEFAULT_REQUIRED_ARGC_NUM,
177 : hccp_err("param num error, requiredCnt:%d, argc:%d", requiredCnt, HCCP_DEFAULT_REQUIRED_ARGC_NUM), -EINVAL);
178 0 : return 0;
179 : }
|