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 : #ifndef RS_INNER_H
12 : #define RS_INNER_H
13 :
14 : #include <pthread.h>
15 : #include <stdint.h>
16 : #include <sys/time.h>
17 : #include <infiniband/verbs.h>
18 : #include <semaphore.h>
19 : #ifndef CA_CONFIG_LLT
20 : #include <openssl/crypto.h>
21 : #include <openssl/ssl.h>
22 : #include <openssl/err.h>
23 : #include <openssl/rsa.h>
24 : #include <crypto/x509.h>
25 : #include <crypto/evp.h>
26 : #include <crypto/asn1.h>
27 : #include <openssl/x509v3.h>
28 : #else
29 : #include "stub_ssl.h"
30 : #endif
31 : #include "ascend_hal_external.h"
32 : #ifndef HNS_ROCE_LLT
33 : #include "dlog_pub.h"
34 : #endif
35 : #include "hccp_common.h"
36 : #include "hccp_ping.h"
37 : #include "hccp_nda.h"
38 : #include "rs_rdma_inner.h"
39 : #include "rs_common_inner.h"
40 : #include "rs_ping_inner.h"
41 : #include "rs.h"
42 : #include "rs_list.h"
43 :
44 : /* priority of algos and forbid unsafety algos */
45 : #define CHIPER_LIST "ECDHE-RSA-AES256-GCM-SHA384:\
46 : !RC2:!RC4:!MD2:!MD4:!MD5:!DES:!3DES:!SHA1:!BLOWFISH:!CBC:!ECB:!ADH:!LOW:!PSK:!SRP:!DSS:!eNULL:!aNULL:!EXP:@STRENGTH"
47 :
48 : #define RS_S6_ADDR32 2
49 : #define RS_USLEEP_TIME 20000
50 : #define RS_RECV_MAX_TIME (1000.0 * 5) // ms
51 : #define RS_RECV_TAG_MAX_TIME (1000.0 * 90) // ms
52 : #define RS_DEVICE_NUM 0x3
53 : #define RS_HOSTID2DEVID(dev_id) ((dev_id) & RS_DEVICE_NUM)
54 : #define SOCK_CONN_DEV_ID_SIZE 64
55 : #define RS_VNIC_MAX 128
56 : #define RS_VNIC_IP_LEN 14
57 : #define RS_IB_NAME_LEN 10
58 :
59 : #define RS_VNIC_FIRST 192
60 : #define RS_VNIC_SECOND 168
61 : #ifndef CA_CONFIG_LLT
62 : #define RS_VNIC_THIRD 2
63 : #else
64 : #define RS_VNIC_THIRD 1
65 : #endif
66 : #define RS_VNIC_FOUTH 199
67 : #define RS_VNIC_FLAG 1
68 :
69 : #define RS_TCP_DSCP_0 0
70 : #define RS_ROCE_DSCP_33 33
71 : #define RS_DSCP_MASK 0x3f
72 : #define RS_DSCP_OFF 2
73 :
74 : #define RS_MAX_FD_NUM 65536
75 : #define TLS_CA_SSL_MAX_NEW_CERT_NUM 8
76 :
77 : #define RS_CONN_EXIT_FLAG 2
78 : #define RS_TRY_TIME 200
79 : #define RS_WLIST_VALID_FLAG_SIZE 6
80 : #define RS_SSL_CERT_LEN 2048
81 : #define RS_SSL_MIN_CERT_NUM 2
82 : #define RS_SSL_MAX_CERT_NUM 15
83 : #define RS_SSL_MAX_ALL_CERT_NUM (RS_SSL_MAX_CERT_NUM + (TLS_CA_SSL_MAX_NEW_CERT_NUM * RS_SSL_NEW_CERT_CB_NUM))
84 : #define RS_SSL_MIN_CERT_LEN (RS_SSL_CERT_LEN * RS_SSL_MIN_CERT_NUM)
85 : #define RS_SSL_MAX_CERT_LEN (RS_SSL_CERT_LEN * RS_SSL_MAX_CERT_NUM)
86 : #define RS_SSL_PRI_LEN 5120
87 : #define RS_SSL_REVOKE_LEN 20480
88 : #define RS_SSL_FALSH_HEAD_LEN 8
89 : #define RS_SSL_ENC_MODE 1
90 : #define RS_SSL_VERSION 2
91 : #define HCCP_CERTS_STATR "-----BEGIN CERTIFICATE-----"
92 : #define HCCP_CERTS_END "-----END CERTIFICATE-----"
93 : #define RS_KID_MAX_LENGTH 512
94 : #define RS_KID_MIN_LENGTH 8
95 : #define RS_RSA_KY_BITS_MIN_LEN 2048
96 : #define RS_DSA_KY_BITS_MIN_LEN 2048
97 : #define RS_DH_KY_BITS_MIN_LEN 2048
98 : #define RS_EC_KY_BITS_MIN_LEN 256
99 : #define RS_SSL_ERR_MSG_LEN 256
100 : #define RS_SOCKET_MAXLEN 2048
101 : #define RS_INTERFACE_BOND_LEN 6
102 : #define RS_INTERFACE_ETH_PREFIX_LEN 3
103 : #define RS_INTERFACE_BOND_PREFIX_LEN 4
104 :
105 : /* pcie card boardid rule: GPIO[75:73]=0x000 */
106 : #define RS_BOARDID_PCIE_CARD_MASK 0xE00
107 : #define RS_BOARDID_PCIE_CARD_MASK_VALUE 0x0
108 : #define RS_BOARDID_AI_SERVER_MODULE 0x0
109 : #define RS_BOARDID_ARM_SERVER_AG 0x20
110 : #define RS_BOARDID_ARM_POD 0x30
111 : #define RS_BOARDID_X86_16P 0x50
112 : #define RS_BOARDID_ARM_SERVER_2DIE 0xB0
113 :
114 : #define RS_MAX_RD_ATOMIC_NUM_PEER_ONLINE 16 // host RDMA adapt
115 : #define RS_QP_TX_DEPTH_PEER_ONLINE 4096 // host RDMA adapt
116 :
117 : #define RS_CLOSE_TIMEOUT 5
118 :
119 : enum CaPtye {
120 : RS_EQPT_CA = 0,
121 : RS_ROOT_CA
122 : };
123 :
124 : #define RS_SSL_DISABLE 0
125 : #define RS_SSL_ENABLE 1
126 :
127 : #define RS_EQPT_CERTS_PATH_LEN 256
128 : #define RS_CA_CERTS_PATH_LEN 256
129 :
130 : struct RsCertInfo {
131 : char certInfo[RS_SSL_CERT_LEN];
132 : };
133 :
134 : struct RsCerts {
135 : struct RsCertInfo certs[RS_SSL_MAX_CERT_NUM];
136 : };
137 :
138 : #define HCCP_NEW_CERTS_CB1_INDEX 0
139 : #define HCCP_NEW_CERTS_CB2_INDEX 1
140 : #define HCCP_NEW_CERTS_CB3_INDEX 2
141 : #define HCCP_NEW_CERTS_CB4_INDEX 3
142 : #define MAX_CERT_NUM_IN_CB 8
143 : #define RS_SSL_NEW_CERT_CB_NUM 4
144 :
145 : struct CertFile {
146 : const char *endFile;
147 : const char *caFile;
148 : };
149 :
150 : #define TLS_SALT_MAX_LEN 48
151 : #define TLS_ENC_DEC_DIV_LEN 16
152 : #define TLS_MAGIC_WORDS "1234567"
153 : #define X509_VERIFY_SUCC 1
154 :
155 : struct RsSecPara {
156 : unsigned char inBuf[RS_SSL_PRI_LEN];
157 : unsigned int inBufSize;
158 : unsigned char inSalt[TLS_SALT_MAX_LEN];
159 : unsigned int inSaltSize;
160 : unsigned char outBuf[RS_SSL_PRI_LEN];
161 : unsigned int outBufSize;
162 : };
163 :
164 : #define RS_CLOSE_RETRY_FOR_EINTR(ret, fd) do { \
165 : do { \
166 : (ret) = close((fd)); \
167 : } while (((ret) < 0) && (errno == EINTR)); \
168 : } while (0)
169 :
170 : #define RS_CHECK_RET_WITHOUT_RETURN(ret, fmt, val...) do { \
171 : if (ret) { \
172 : hccp_warn(fmt, ##val); \
173 : } \
174 : } while (0)
175 :
176 : #define RS_CHECK_POINTER_NULL_WITH_RET(ptr) do { \
177 : if ((ptr) == NULL) { \
178 : hccp_err("pointer is NULL!"); \
179 : return (-EINVAL); \
180 : } \
181 : } while (0)
182 :
183 : #define RS_CHECK_POINTER_NULL_RETURN_VOID(ptr) do { \
184 : if ((ptr) == NULL) { \
185 : hccp_err("pointer is NULL!"); \
186 : return; \
187 : } \
188 : } while (0)
189 :
190 : #define RS_CHECK_POINTER_NULL_RETURN_NULL(ptr) do { \
191 : if ((ptr) == NULL) { \
192 : hccp_err("null pointer exception!"); \
193 : return NULL; \
194 : } \
195 : } while (0)
196 :
197 : #define RS_CHECK_POINTER_NULL_RETURN_INT(ptr) do { \
198 : if ((ptr) == NULL) { \
199 : hccp_err("null pointer exception!"); \
200 : return (-EINVAL); \
201 : } \
202 : } while (0)
203 :
204 : #define RS_PTHREAD_MUTEX_LOCK(conn_mutex) do { \
205 : int ret_lock = pthread_mutex_lock(conn_mutex); \
206 : if (ret_lock) { \
207 : hccp_warn("pthread_mutex_lock unsuccessful, ret[%d]", ret_lock); \
208 : }\
209 : } while (0)
210 :
211 : #define RS_PTHREAD_MUTEX_ULOCK(conn_mutex) do { \
212 : int ret_ulock = pthread_mutex_unlock(conn_mutex); \
213 : if (ret_ulock) { \
214 : hccp_warn("pthread_mutex_unlock unsuccessful, ret[%d]", ret_ulock); \
215 : } \
216 : } while (0)
217 :
218 : #define RS_FD_INVALID (-1)
219 :
220 : /*
221 : * mr_cb also used to sync to remote
222 : */
223 : struct RsMrCb {
224 : struct RsMrInfo mrInfo; /* MUST be the first element */
225 :
226 : uint64_t wrId;
227 : uint32_t state;
228 :
229 : struct ibv_mr *ibMr;
230 : struct RsRdevCb *devCb;
231 : struct RsQpCb *qpCb;
232 : struct RsListHead list;
233 : };
234 :
235 : struct RsQpInfo {
236 : uint32_t cmd; /* MUST be the first element */
237 :
238 : int lid;
239 : int qpn;
240 : int psn;
241 : int gidIdx;
242 : union ibv_gid gid;
243 : struct RsMrInfo notifyMr;
244 : };
245 :
246 : enum RsConnState {
247 : RS_CONN_STATE_RESET,
248 : RS_CONN_STATE_INIT,
249 : RS_CONN_STATE_BIND,
250 : RS_CONN_STATE_LISTENING,
251 : RS_CONN_STATE_CONNECTED,
252 : RS_CONN_STATE_SSL_BIND_FD,
253 : RS_CONN_STATE_SSL_CONNECTED,
254 : RS_CONN_STATE_TAG_SYNC,
255 : RS_CONN_STATE_VALID_SYNC,
256 : RS_CONN_STATE_TX_TO_HCCL,
257 :
258 : RS_CONN_STATE_TIMEOUT,
259 :
260 : RS_CONN_STATE_ERR,
261 :
262 : RS_CONN_STATE_MAX,
263 : };
264 :
265 : struct RsConnInfo {
266 : struct RsIpAddrInfo serverIp;
267 : struct RsIpAddrInfo clientIp;
268 : uint16_t port;
269 : int scopeId;
270 :
271 : int connfd;
272 : SSL *ssl;
273 : uint32_t state; /* refer to enum rs_conn_state */
274 : struct timeval startTime;
275 : struct timeval endTime;
276 : bool isGot;
277 :
278 : /*
279 : * HCCL need classify the connection according by the tag.
280 : * when a client connects successfully, it need send the tag to Server,
281 : * Server return the tag to HCCL
282 : */
283 : char tag[SOCK_CONN_TAG_SIZE + SOCK_CONN_DEV_ID_SIZE];
284 : uint32_t tagSyncTimes;
285 : uint32_t tagEintrTimes;
286 :
287 : struct SocketErrInfo errInfo;
288 :
289 : struct RsListHead list;
290 : };
291 :
292 : enum ListenFdState {
293 : LISTEN_FD_STATE_ADDED = 0,
294 : LISTEN_FD_STATE_DELETED = 1,
295 : };
296 :
297 : struct RsListenInfo {
298 : struct RsIpAddrInfo serverIpAddr;
299 : struct RsIpAddrInfo clientIpAddr;
300 : uint16_t sockPort;
301 :
302 : int listenFd;
303 : uint32_t state; /* refer to enum rs_conn_state */
304 : int counter;
305 :
306 : int lastAcceptErrno; /* last accept errno, avoid log flush */
307 : struct SocketErrInfo errInfo;
308 :
309 : bool acceptCreditFlag;
310 : pthread_mutex_t acceptCreditMutex;
311 : enum ListenFdState fdState;
312 : unsigned int acceptCreditLimit;
313 :
314 : struct RsListHead list;
315 : };
316 :
317 : struct RsAcceptInfo {
318 : struct RsIpAddrInfo serverIpAddr;
319 : struct RsIpAddrInfo clientIpAddr;
320 : uint16_t sockPort;
321 : int connFd;
322 : SSL *ssl;
323 : uint32_t state;
324 :
325 : struct RsListHead list;
326 : };
327 :
328 : struct RsWhiteList {
329 : struct RsIpAddrInfo serverIp;
330 : struct RsListHead whiteList;
331 : struct RsListHead list;
332 : };
333 :
334 : struct RsWhiteListInfo {
335 : struct RsIpAddrInfo clientIp;
336 : unsigned int connLimit;
337 : char tag[SOCK_CONN_TAG_SIZE];
338 : struct RsListHead list;
339 : };
340 :
341 : struct RsHeterogTcpFdInfo {
342 : int fd;
343 : struct RsListHead list;
344 : };
345 :
346 : struct RsCqeErrInfo {
347 : pthread_mutex_t mutex;
348 : struct CqeErrInfo info;
349 : };
350 :
351 : struct RsConnCb {
352 : struct RsIpAddrInfo localIpAddr;
353 : unsigned int wlistEnable;
354 : int eventfd;
355 : int epollfd;
356 : int scopeId;
357 :
358 : pthread_mutex_t connMutex;
359 : struct rs_cb *rscb;
360 : struct SocketErrInfo epollErrInfo;
361 :
362 : struct RsListHead listenList;
363 : struct RsListHead serverAcceptList;
364 : struct RsListHead serverConnList;
365 : struct RsListHead clientConnList;
366 : struct RsListHead whiteList;
367 : };
368 :
369 : struct RsQpCb {
370 : struct RsRdevCb *rdevCb;
371 : struct ibv_pd *ibPd;
372 : struct ibv_qp *ibQp;
373 : struct ibv_qp_extend *ibQpEx;
374 :
375 : int eqNum;
376 : struct ibv_comp_channel *channel;
377 : struct ibv_cq *ibSendCq;
378 : int sendCqDepth;
379 : struct ibv_cq *ibRecvCq;
380 : int recvCqDepth;
381 : struct RsCqContext *srqContext;
382 : int numRecvCqEvents;
383 : int numSendCqEvents;
384 :
385 : unsigned int txDepth;
386 : unsigned int sendSgeNum;
387 : unsigned int rxDepth;
388 : unsigned int recvSgeNum;
389 :
390 : unsigned int sendWrNum;
391 : unsigned int recvWrNum;
392 :
393 : int sqIndex;
394 : int dbIndex;
395 : int qpMode;
396 : struct RsQpInfo qpInfoLo;
397 : struct RsQpInfo qpInfoRem;
398 :
399 : struct RsConnInfo *connInfo;
400 : int state;
401 : struct timeval startTime;
402 : struct timeval endTime;
403 : char qpMrBuf[RS_BUF_SIZE];
404 : unsigned int remainSize;
405 :
406 : pthread_mutex_t qpMutex;
407 :
408 : int mrNum;
409 : struct RsListHead list;
410 : struct RsListHead mrList;
411 : struct RsListHead remMrList;
412 : int isExp;
413 :
414 : uint32_t sendLen;
415 : uint32_t recvLen;
416 : uint32_t expectLen;
417 :
418 : struct event_summary *sendEvent;
419 : struct event_summary *recvEvent;
420 :
421 : struct QosAttr qosAttr;
422 :
423 : unsigned int timeout;
424 : unsigned int retryCnt;
425 :
426 : struct LiteQpCqAttrResp qpResp;
427 :
428 : struct LiteMemAttrResp memResp;
429 : int memAlign; // 0,1:4KB, 2:2MB
430 : uint32_t udpSport;
431 :
432 : unsigned int aiOpSupport;
433 : unsigned int grpId;
434 : unsigned int cqCstmFlag;
435 :
436 : struct RsCqeErrInfo cqeErrInfo;
437 : unsigned int useResvMem;
438 : unsigned int resvMemPoolId;
439 : };
440 :
441 : struct RsCqCreateAttr {
442 : struct RsRdevCb *rdevCb;
443 : int eqNum;
444 : int cqDepth;
445 : int cqEventId;
446 : struct ibv_cq *ibCq;
447 : struct ibv_comp_channel *channel;
448 : struct event_summary *event;
449 : };
450 :
451 : struct RsCqContext {
452 : struct RsRdevCb *rdevCb;
453 : int eqNum;
454 : int cqCreateMode;
455 : struct ibv_cq *ibSendCq;
456 : struct ibv_cq *ibRecvCq;
457 : struct ibv_cq *ibSrqCq;
458 : struct ibv_comp_channel *channel;
459 : struct event_summary *sendEvent;
460 : struct event_summary *recvEvent;
461 : struct RsCqContext *srqContext;
462 : int numRecvCqEvents;
463 : };
464 :
465 : /* rs_cb->state enum */
466 : #define RS_STATE_HALT 4
467 :
468 : struct RsAkid {
469 : char akidName[RS_KID_MAX_LENGTH];
470 : };
471 :
472 : struct RsIssuer {
473 : char issuerName[RS_KID_MAX_LENGTH];
474 : };
475 :
476 : struct RsCertAkidIssuerCb {
477 : struct RsAkid akids[RS_SSL_MAX_ALL_CERT_NUM];
478 : struct RsIssuer issers[RS_SSL_MAX_ALL_CERT_NUM];
479 : };
480 :
481 : struct RsSkid {
482 : char skidName[RS_KID_MAX_LENGTH];
483 : };
484 :
485 : struct RsSubject {
486 : char subjectName[RS_KID_MAX_LENGTH];
487 : };
488 :
489 : struct RsCertSkidSubjectCb {
490 : struct RsSkid skids[RS_SSL_MAX_ALL_CERT_NUM];
491 : struct RsSubject subjects[RS_SSL_MAX_ALL_CERT_NUM];
492 : };
493 :
494 : struct SensorNode {
495 : unsigned int logicDevid;
496 : int sensorUpdateCnt;
497 : uint64_t sensorHandle;
498 : };
499 :
500 : struct TlvBufInfo {
501 : unsigned int bufferSize;
502 : char *buf;
503 : };
504 :
505 : struct RsNslbCb {
506 : bool initFlag;
507 : void *netcoCb;
508 : pthread_mutex_t mutex;
509 : };
510 :
511 : struct RsTlvCb {
512 : unsigned int phyId;
513 : pthread_mutex_t mutex;
514 : struct TlvBufInfo bufInfo;
515 : bool initFlag;
516 : struct RsNslbCb nslbCb;
517 : };
518 :
519 : /*
520 : * Main Control block for device
521 : * for multi processor(device) in SMP system, each device have it's own rs_cb
522 : */
523 : struct rs_cb {
524 : uint32_t chipId;
525 : uint32_t hccpMode;
526 : unsigned int logicId;
527 : enum ProtocolTypeT protocol;
528 :
529 : pthread_mutex_t mutex;
530 :
531 : sem_t connectTrigSem;
532 : uint32_t state;
533 : uint32_t sslEnable;
534 : SSL_CTX *serverSslCtx;
535 : SSL_CTX *clientSslCtx;
536 : struct RsCertSkidSubjectCb *skidSubjectCb;
537 :
538 : int connFlag;
539 : struct RsConnCb connCb;
540 :
541 : unsigned int devCnt;
542 : struct RsListHead rdevList;
543 : struct RsListHead udevList;
544 : struct RsListHead heterogTcpFdList;
545 :
546 : struct RsPingCtxCb pingCb;
547 :
548 : struct RsTlvCb tlvCb;
549 :
550 : char buf[RS_BUF_SIZE];
551 : struct ProcessRsSign pRsSign;
552 :
553 : unsigned long long notifyVaBase;
554 : unsigned long long notifySize;
555 : struct SensorNode sensorNode;
556 :
557 : void (*tcpRecvCallback)(const void *fdHandle);
558 : const void **fdMap;
559 :
560 : struct ifaddrs *ifaddrList;
561 :
562 : pid_t aicpuPid;
563 : unsigned int grpId;
564 : pid_t hostPid;
565 : bool grpSetupFlag;
566 :
567 : void *ndaCb;
568 : int ndaCbRefCnt;
569 : void *custom_ssl;
570 : };
571 :
572 : extern __thread struct rs_cb *gRsCb;
573 :
574 150 : int RsSocketNodeid2vnic(uint32_t nodeId, uint32_t *ipAddr);
575 : int RsGetHccpMode(unsigned int chipId);
576 : int RsDev2conncb(uint32_t chipId, struct RsConnCb **connCb);
577 : int RsDev2rscb(uint32_t chipId, struct rs_cb **rsCb, bool initFlag);
578 : int RsQpn2qpcb(unsigned int phyId, unsigned int rdevIndex, uint32_t qpn, struct RsQpCb **qpCb);
579 : int RsRdev2rdevCb(unsigned int chipId, unsigned int rdevIndex, struct RsRdevCb **rdevCb);
580 : int RsGetRdevCb(struct rs_cb *rsCb, unsigned int rdevIndex, struct RsRdevCb **rdevCb);
581 : void RsAccpetListNodeFree(struct rs_cb *rscb);
582 16 : int RsWlistCheckConnAdd(struct rs_cb *rsCb, struct RsConnInfo* connTmp);
583 : #ifdef CUSTOM_INTERFACE
584 : int RsSetupSharemem(struct rs_cb *rsCb, bool backupFlag, unsigned int backupPhyid);
585 : #endif
586 : int RsQueryMrCb(struct RsRdevCb *devCb, uint64_t addr, struct RsMrCb **mrCb, struct RsListHead *mrList);
587 : int RsGetRsCb(unsigned int phyId, struct rs_cb **rsCb);
588 : int RsQueryGid(struct rdev rdevInfo, struct ibv_context *ibCtxTmp, uint8_t ibPort, int *gidIdx);
589 : int RsEpollEventPingHandle(struct rs_cb *rsCb, int fd);
590 : int RsSensorNodeRegister(unsigned int phyId, struct rs_cb *rsCb);
591 : void RsSensorNodeUnregister(struct rs_cb *rsCb);
592 : int RsRetryTimeoutExceptionCheck(struct SensorNode *snesorNode);
593 : #endif // RS_INNER_H
|