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