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, hccp_err("[get][tls_enable]phyId(%u) must smaller than %u",
38 : info->phyId, RA_MAX_PHY_ID_NUM), ConverReturnCode(OTHERS, -EINVAL));
39 :
40 2 : hccp_run_info("Input parameters: phyId[%u], nicPosition:[%d]", info->phyId, info->mode);
41 2 : if (info->mode == NETWORK_PEER_ONLINE) {
42 1 : ret = RaPeerGetTlsEnable(info->phyId, tlsEnable);
43 1 : } else if (info->mode == NETWORK_OFFLINE) {
44 1 : ret = RaHdcGetTlsEnable(info->phyId, tlsEnable);
45 : } else {
46 0 : hccp_err("[get][tls_enable]do not support mode(%d) phyId(%u)", info->mode, info->phyId);
47 0 : return ConverReturnCode(OTHERS, -ENOTSUPP);
48 : }
49 2 : return ConverReturnCode(OTHERS, ret);
50 : }
51 :
52 0 : HCCP_ATTRI_VISI_DEF int RaSaveSnapshot(struct RaInfo *info, enum SaveSnapshotAction action)
53 : {
54 0 : struct RaRdmaHandle *rdmaHandle = NULL;
55 : int ret;
56 :
57 0 : CHK_PRT_RETURN(info == NULL, hccp_err("[save][snapshot]info is NULL"), ConverReturnCode(OTHERS, -EINVAL));
58 0 : CHK_PRT_RETURN(action < SAVE_SNAPSHOT_ACTION_PRE_PROCESSING || action >= SAVE_SNAPSHOT_ACTION_MAX,
59 : hccp_err("[save][snapshot]invalid action(%d)", action), ConverReturnCode(OTHERS, -EINVAL));
60 :
61 0 : hccp_run_info("Input parameters: phyId[%u], nicPosition:[%d], action:[%d]", info->phyId, info->mode, action);
62 0 : if (info->mode == NETWORK_PEER_ONLINE) {
63 0 : return 0;
64 0 : } else if (info->mode == NETWORK_OFFLINE) {
65 0 : ret = RaRdevGetHandle(info->phyId, (void **)&rdmaHandle);
66 0 : CHK_PRT_RETURN(ret != 0 && ret != -ENODEV, hccp_err("[save][snapshot]ra_rdev_get_handle failed, ret[%d]", ret),
67 : ConverReturnCode(OTHERS, ret));
68 0 : ret = RaHdcRdmaSaveSnapshot(rdmaHandle, action);
69 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[save][snapshot]ra_hdc_rdma_save_snapshot failed, ret[%d]", ret),
70 : ConverReturnCode(OTHERS, ret));
71 :
72 0 : ret = RaHdcSaveSnapshot(info->phyId, action);
73 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[save][snapshot]ra_hdc_save_snapshot failed, ret[%d]", ret),
74 : ConverReturnCode(OTHERS, ret));
75 : } else {
76 0 : hccp_err("[save][snapshot]do not support mode[%d] phyId[%u]", info->mode, info->phyId);
77 0 : return ConverReturnCode(OTHERS, -ENOTSUPP);
78 : }
79 :
80 0 : return ConverReturnCode(OTHERS, ret);
81 : }
82 :
83 0 : HCCP_ATTRI_VISI_DEF int RaRestoreSnapshot(struct RaInfo *info)
84 : {
85 0 : struct RaRdmaHandle *rdmaHandle = NULL;
86 0 : int ret = 0;
87 :
88 0 : CHK_PRT_RETURN(info == NULL, hccp_err("[restore][snapshot]info is NULL"), ConverReturnCode(OTHERS, -EINVAL));
89 :
90 0 : hccp_run_info("Input parameters: phyId[%u], nicPosition:[%d]", info->phyId, info->mode);
91 0 : if (info->mode == NETWORK_PEER_ONLINE) {
92 0 : return 0;
93 0 : } else if (info->mode == NETWORK_OFFLINE) {
94 0 : ret = RaRdevGetHandle(info->phyId, (void **)&rdmaHandle);
95 0 : CHK_PRT_RETURN(ret != 0 && ret != -ENODEV, hccp_err("[restore][snapshot]ra_rdev_get_handle failed, ret[%d]",
96 : ret),ConverReturnCode(OTHERS, ret));
97 0 : ret = RaHdcRdmaRestoreSnapshot(rdmaHandle, &gRaRestoreRdmaOps);
98 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[restore][snapshot]ra_hdc_rdma_restore_snapshot failed, ret[%d]", ret),
99 : ConverReturnCode(OTHERS, ret));
100 :
101 0 : ret = RaHdcRestoreSnapshot(info->phyId);
102 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[restore][snapshot]ra_hdc_restore_snapshot failed, ret[%d]", ret),
103 : ConverReturnCode(OTHERS, ret));
104 : } else {
105 0 : hccp_err("[restore][snapshot]do not support mode[%d] phyId[%u]", info->mode, info->phyId);
106 0 : return ConverReturnCode(OTHERS, -ENOTSUPP);
107 : }
108 :
109 0 : return ConverReturnCode(OTHERS, ret);
110 : }
111 :
112 4 : HCCP_ATTRI_VISI_DEF int RaGetHccnCfg(struct RaInfo *info, enum HccnCfgKey key, char *value, unsigned int *valueLen)
113 : {
114 : int ret;
115 :
116 4 : CHK_PRT_RETURN(info == NULL || value == NULL || valueLen == NULL, hccp_err("[get][hccn_cfg]info or value or value_len is NULL"),
117 : ConverReturnCode(OTHERS, -EINVAL));
118 3 : CHK_PRT_RETURN(*valueLen < HCCN_CFG_MSG_DATA_LEN,
119 : hccp_err("[get][hccn_cfg] failed, valueLen[%d] < min_len[%d]", *valueLen, HCCN_CFG_MSG_DATA_LEN),
120 : ConverReturnCode(OTHERS, -EINVAL));
121 2 : CHK_PRT_RETURN(info->phyId >= RA_MAX_PHY_ID_NUM, hccp_err("[get][hccn_cfg]phyId(%u) must smaller than %u",
122 : info->phyId, RA_MAX_PHY_ID_NUM), ConverReturnCode(OTHERS, -EINVAL));
123 1 : CHK_PRT_RETURN(info->mode != NETWORK_OFFLINE, hccp_err("[get][hccn_cfg]do not support mode(%u)", info->mode),
124 : ConverReturnCode(OTHERS, -EINVAL));
125 :
126 1 : hccp_run_info("Input parameters: phyId[%u], nicPosition:[%d], hccn_cfg_key[%d]",
127 : info->phyId, info->mode, key);
128 1 : ret = RaHdcGetHccnCfg(info->phyId, key, value, valueLen);
129 1 : if (ret != 0) {
130 0 : hccp_err("[get][hccn_cfg] failed, phyId[%u], ret[%d]", info->phyId, ret);
131 : }
132 :
133 1 : return ConverReturnCode(OTHERS, ret);
134 : }
135 :
136 3 : HCCP_ATTRI_VISI_DEF int RaGetSecRandom(struct RaInfo *info, u32 *value)
137 : {
138 : int ret;
139 :
140 3 : CHK_PRT_RETURN(info == NULL || value == NULL, hccp_err("[get][sec_random]info or value is NULL"),
141 : ConverReturnCode(OTHERS, -EINVAL));
142 2 : hccp_run_info("Input parameters: phy_id[%u], nic_position:[%d]", info->phyId, info->mode);
143 :
144 2 : ret = RaPeerGetSecRandom(value);
145 2 : if(ret != 0 && info->mode == NETWORK_OFFLINE) {
146 1 : CHK_PRT_RETURN(info->phyId >= RA_MAX_PHY_ID_NUM, hccp_err("[get][sec_random]phy_id(%u) must smaller than %u",
147 : info->phyId, RA_MAX_PHY_ID_NUM), ConverReturnCode(OTHERS, -EINVAL));
148 1 : ret = RaHdcGetSecRandom(info->phyId, value);
149 1 : } else if (ret != 0 && info->mode != NETWORK_OFFLINE) {
150 0 : hccp_err("[get][sec_random] failed, mode[%u], ret[%d]", info->mode, ret);
151 : }
152 :
153 2 : return ConverReturnCode(OTHERS, ret);
154 : }
155 :
156 0 : HCCP_ATTRI_VISI_DEF bool RaHasCapability(struct RaInfo *info, unsigned int capability)
157 : {
158 0 : CHK_PRT_RETURN(info == NULL, hccp_warn("info is NULL"), false);
159 0 : CHK_PRT_RETURN(info->phyId >= RA_MAX_PHY_ID_NUM, hccp_warn("phy_id(%u) must smaller than %u",
160 : info->phyId, RA_MAX_PHY_ID_NUM), false);
161 0 : CHK_PRT_RETURN(info->mode != NETWORK_OFFLINE, hccp_warn("mode:%u not support", info->mode), false);
162 :
163 0 : return RaHdcHasCapability(info->phyId, capability);
164 : }
|