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 <stdbool.h>
12 : #include <errno.h>
13 : #include "user_log.h"
14 : #include "hccp.h"
15 : #include "hccp_common.h"
16 : #include "ra_client_host.h"
17 : #include "ra.h"
18 : #include "ra_peer.h"
19 : #include "ra_hdc.h"
20 : #include "ra_hdc_rdma.h"
21 : #include "ra_rs_err.h"
22 : #include "ra_rs_comm.h"
23 :
24 : /* rdma ops for ra_restore_snapshot: The use of RDMA-lite related interfaces is prohibited. */
25 : static struct RaRdmaOps gRaRestoreRdmaOps = {
26 : .raRdevDeinit = RaHdcRdevRestoreDeinit,
27 : .raQpDestroy = RaHdcQpDestroy,
28 : .raDeregisterMr = RaHdcTypicalMrDereg,
29 : };
30 :
31 3 : HCCP_ATTRI_VISI_DEF int RaGetTlsEnable(struct RaInfo *info, bool *tlsEnable)
32 : {
33 : int ret;
34 :
35 3 : CHK_PRT_RETURN(info == NULL || tlsEnable == NULL, hccp_err("[get][tls_enable]info or tls_enable is NULL"),
36 : ConverReturnCode(OTHERS, -EINVAL));
37 3 : CHK_PRT_RETURN(info->phyId >= RA_MAX_PHY_ID_NUM,
38 : hccp_err("[get][tls_enable]phyId(%u) must smaller than %u", info->phyId, RA_MAX_PHY_ID_NUM),
39 : ConverReturnCode(OTHERS, -EINVAL));
40 :
41 2 : hccp_run_info("Input parameters: phyId[%u], nicPosition:[%d]", info->phyId, info->mode);
42 2 : if (info->mode == NETWORK_PEER_ONLINE) {
43 1 : ret = RaPeerGetTlsEnable(info->phyId, tlsEnable);
44 1 : } else if (info->mode == NETWORK_OFFLINE) {
45 1 : ret = RaHdcGetTlsEnable(info->phyId, tlsEnable);
46 : } else {
47 0 : hccp_err("[get][tls_enable]do not support mode(%d) phyId(%u)", info->mode, info->phyId);
48 0 : return ConverReturnCode(OTHERS, -ENOTSUPP);
49 : }
50 2 : return ConverReturnCode(OTHERS, ret);
51 : }
52 :
53 0 : HCCP_ATTRI_VISI_DEF int RaSaveSnapshot(struct RaInfo *info, enum SaveSnapshotAction action)
54 : {
55 0 : struct RaRdmaHandle *rdmaHandle = NULL;
56 : int ret;
57 :
58 0 : CHK_PRT_RETURN(info == NULL, hccp_err("[save][snapshot]info is NULL"), ConverReturnCode(OTHERS, -EINVAL));
59 0 : CHK_PRT_RETURN(action < SAVE_SNAPSHOT_ACTION_PRE_PROCESSING || action >= SAVE_SNAPSHOT_ACTION_MAX,
60 : hccp_err("[save][snapshot]invalid action(%d)", action), ConverReturnCode(OTHERS, -EINVAL));
61 :
62 0 : hccp_run_info("Input parameters: phyId[%u], nicPosition:[%d], action:[%d]", info->phyId, info->mode, action);
63 0 : if (info->mode == NETWORK_PEER_ONLINE) {
64 0 : return 0;
65 0 : } else if (info->mode == NETWORK_OFFLINE) {
66 0 : ret = RaRdevGetHandle(info->phyId, (void **)&rdmaHandle);
67 0 : CHK_PRT_RETURN(ret != 0 && ret != -ENODEV, hccp_err("[save][snapshot]ra_rdev_get_handle failed, ret[%d]", ret),
68 : ConverReturnCode(OTHERS, ret));
69 0 : ret = RaHdcRdmaSaveSnapshot(rdmaHandle, action);
70 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[save][snapshot]ra_hdc_rdma_save_snapshot failed, ret[%d]", ret),
71 : ConverReturnCode(OTHERS, ret));
72 :
73 0 : ret = RaHdcSaveSnapshot(info->phyId, action);
74 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[save][snapshot]ra_hdc_save_snapshot failed, ret[%d]", ret),
75 : ConverReturnCode(OTHERS, ret));
76 : } else {
77 0 : hccp_err("[save][snapshot]do not support mode[%d] phyId[%u]", info->mode, info->phyId);
78 0 : return ConverReturnCode(OTHERS, -ENOTSUPP);
79 : }
80 :
81 0 : return ConverReturnCode(OTHERS, ret);
82 : }
83 :
84 0 : HCCP_ATTRI_VISI_DEF int RaRestoreSnapshot(struct RaInfo *info)
85 : {
86 0 : struct RaRdmaHandle *rdmaHandle = NULL;
87 0 : int ret = 0;
88 :
89 0 : CHK_PRT_RETURN(info == NULL, hccp_err("[restore][snapshot]info is NULL"), ConverReturnCode(OTHERS, -EINVAL));
90 :
91 0 : hccp_run_info("Input parameters: phyId[%u], nicPosition:[%d]", info->phyId, info->mode);
92 0 : if (info->mode == NETWORK_PEER_ONLINE) {
93 0 : return 0;
94 0 : } else if (info->mode == NETWORK_OFFLINE) {
95 0 : ret = RaRdevGetHandle(info->phyId, (void **)&rdmaHandle);
96 0 : CHK_PRT_RETURN(ret != 0 && ret != -ENODEV,
97 : hccp_err("[restore][snapshot]ra_rdev_get_handle failed, ret[%d]", ret), ConverReturnCode(OTHERS, ret));
98 0 : ret = RaHdcRdmaRestoreSnapshot(rdmaHandle, &gRaRestoreRdmaOps);
99 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[restore][snapshot]ra_hdc_rdma_restore_snapshot failed, ret[%d]", ret),
100 : ConverReturnCode(OTHERS, ret));
101 :
102 0 : ret = RaHdcRestoreSnapshot(info->phyId);
103 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[restore][snapshot]ra_hdc_restore_snapshot failed, ret[%d]", ret),
104 : ConverReturnCode(OTHERS, ret));
105 : } else {
106 0 : hccp_err("[restore][snapshot]do not support mode[%d] phyId[%u]", info->mode, info->phyId);
107 0 : return ConverReturnCode(OTHERS, -ENOTSUPP);
108 : }
109 :
110 0 : return ConverReturnCode(OTHERS, ret);
111 : }
112 :
113 4 : HCCP_ATTRI_VISI_DEF int RaGetHccnCfg(struct RaInfo *info, enum HccnCfgKey key, char *value, unsigned int *valueLen)
114 : {
115 : int ret;
116 :
117 4 : CHK_PRT_RETURN(info == NULL || value == NULL || valueLen == NULL,
118 : hccp_err("[get][hccn_cfg]info or value or value_len is NULL"), ConverReturnCode(OTHERS, -EINVAL));
119 3 : CHK_PRT_RETURN(*valueLen < HCCN_CFG_MSG_DATA_LEN,
120 : hccp_err("[get][hccn_cfg] failed, valueLen[%d] < min_len[%d]", *valueLen, HCCN_CFG_MSG_DATA_LEN),
121 : ConverReturnCode(OTHERS, -EINVAL));
122 2 : CHK_PRT_RETURN(info->phyId >= RA_MAX_PHY_ID_NUM,
123 : hccp_err("[get][hccn_cfg]phyId(%u) must smaller than %u", info->phyId, RA_MAX_PHY_ID_NUM),
124 : ConverReturnCode(OTHERS, -EINVAL));
125 1 : CHK_PRT_RETURN(info->mode != NETWORK_OFFLINE, hccp_err("[get][hccn_cfg]do not support mode(%u)", info->mode),
126 : ConverReturnCode(OTHERS, -EINVAL));
127 :
128 1 : hccp_run_info("Input parameters: phyId[%u], nicPosition:[%d], hccn_cfg_key[%d]", info->phyId, info->mode, key);
129 1 : ret = RaHdcGetHccnCfg(info->phyId, key, value, valueLen);
130 1 : if (ret != 0) {
131 0 : hccp_err("[get][hccn_cfg] failed, phyId[%u], ret[%d]", info->phyId, ret);
132 : }
133 :
134 1 : return ConverReturnCode(OTHERS, ret);
135 : }
136 :
137 3 : HCCP_ATTRI_VISI_DEF int RaGetSecRandom(struct RaInfo *info, u32 *value)
138 : {
139 : int ret;
140 :
141 3 : CHK_PRT_RETURN(info == NULL || value == NULL, hccp_err("[get][sec_random]info or value is NULL"),
142 : ConverReturnCode(OTHERS, -EINVAL));
143 2 : hccp_run_info("Input parameters: phy_id[%u], nic_position:[%d]", info->phyId, info->mode);
144 :
145 2 : ret = RaPeerGetSecRandom(value);
146 2 : if (ret != 0 && info->mode == NETWORK_OFFLINE) {
147 1 : CHK_PRT_RETURN(info->phyId >= RA_MAX_PHY_ID_NUM,
148 : hccp_err("[get][sec_random]phy_id(%u) must smaller than %u", info->phyId, RA_MAX_PHY_ID_NUM),
149 : ConverReturnCode(OTHERS, -EINVAL));
150 1 : ret = RaHdcGetSecRandom(info->phyId, value);
151 1 : } else if (ret != 0 && info->mode != NETWORK_OFFLINE) {
152 0 : hccp_err("[get][sec_random] failed, mode[%u], ret[%d]", info->mode, ret);
153 : }
154 :
155 2 : return ConverReturnCode(OTHERS, ret);
156 : }
157 :
158 0 : HCCP_ATTRI_VISI_DEF bool RaHasCapability(struct RaInfo *info, unsigned int capability)
159 : {
160 0 : CHK_PRT_RETURN(info == NULL, hccp_warn("info is NULL"), false);
161 0 : CHK_PRT_RETURN(info->phyId >= RA_MAX_PHY_ID_NUM,
162 : hccp_warn("phy_id(%u) must smaller than %u", info->phyId, RA_MAX_PHY_ID_NUM), false);
163 0 : CHK_PRT_RETURN(info->mode != NETWORK_OFFLINE, hccp_warn("mode:%u not support", info->mode), false);
164 :
165 0 : return RaHdcHasCapability(info->phyId, capability);
166 : }
|