1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
5 # Licensed under the Apache License, Version 2.0 (the "License"); #
6 # you may not use this file except in compliance with the License. #
7 # You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # Unless required by applicable law or agreed to in writing, software #
12 # distributed under the License is distributed on an "AS IS" BASIS, #
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 # See the License for the specific language governing permissions and #
15 # limitations under the License. #
16 ################################################################################
17 *******************************************************************************/
19 /* This file contains all SCTP related functionality */
20 #include "common_def.h"
28 #include "du_app_mac_inf.h"
29 #include "du_e2ap_mgr.h"
33 #include "du_app_rlc_inf.h"
36 /* Global variable declaration */
37 uint8_t socket_type; /* Socket type */
38 bool nonblocking; /* Blocking/Non-blocking socket */
39 bool connUp; /* Is connection up */
41 CmInetNetAddrLst localAddrLst;
42 CmInetNetAddrLst remoteAddrLst;
43 /* Global variable declaration */
44 DuSctpDestCb f1Params; /* SCTP configurations at DU */
45 DuSctpDestCb ricParams; /* SCTP configurations at DU */
47 /**************************************************************************
48 * @brief Task Initiation callback function.
52 * Function : sctpActvInit
55 * This function is supplied as one of parameters during SCTP's
56 * task registration. SSI will invoke this function once, after
57 * it creates and attaches this TAPA Task to a system task.
59 * @param[in] Ent entity, the entity ID of this task.
60 * @param[in] Inst inst, the instance ID of this task.
61 * @param[in] Region region, the region ID registered for memory
63 * @param[in] Reason reason.
64 * @return ROK - success
66 ***************************************************************************/
67 uint8_t sctpActvInit(Ent entity, Inst inst, Region region, Reason reason)
69 DU_LOG("\n\nDEBUG --> SCTP : Initializing");
70 ODU_SET_PROC_ID(DU_PROC);
73 memset(&f1Params, 0, sizeof(DuSctpDestCb));
74 f1Params.assocId = -1;
75 memset(&ricParams, 0, sizeof(DuSctpDestCb));
76 ricParams.assocId = -1;
82 /**************************************************************************
83 * @brief Function prints src, dest, msg infor about all the msgs that received.
87 * Function : callFlowSctpActvTsk
89 * Function prints src, dest, msg infor about all the msgs that received
91 * @param[in] Pst *pst, Post structure of the primitive.
94 ***************************************************************************/
95 void callFlowSctpActvTsk(Pst *pst)
98 char destTask[50]="ENTSCTP";
105 strcpy(sourceTask,"ENTDUAPP");
110 strcpy(message,"EVTSTARTPOLL");
117 DU_LOG("\nCall Flow: %s -> %s : %s\n", sourceTask, destTask, message);
120 /**************************************************************************
121 * @brief Task Activation callback function.
125 * Function : sctpActvTsk
128 * This function handles all SCTP messages received
129 * This API is registered with SSI during the
130 * Task Registration of DU APP.
132 * @param[in] Pst *pst, Post structure of the primitive.
133 * @param[in] Buffer *mBuf, Packed primitive parameters in the
135 * @return ROK - success
138 ***************************************************************************/
139 uint8_t sctpActvTsk(Pst *pst, Buffer *mBuf)
142 #ifdef CALL_FLOW_DEBUG_LOG
143 callFlowSctpActvTsk(pst);
165 /*******************************************************************
167 * @brief Checks the status of the received information
171 * Function : duCheckReqStatus
174 * Checks the status of the received information
176 * @params[in] Confirm status
177 * @return ROK - success
180 ******************************************************************/
181 uint8_t duCheckReqStatus(CmStatus *cfm)
184 if(cfm->status != LCM_PRIM_OK)
186 DU_LOG("\nERROR --> DU_APP : Failed to process the request successfully");
192 /**************************************************************************
193 * @brief Function to configure the Sctp Params during config Request
197 * Function : duSctpCfgReq
200 * This function configures SCTP Params during the config Request
202 * @param[in] SctpParams sctpCfg, common structure which has all the configuration
203 * @param[in] CmStatus cfm, Builds the cfm status and reason
206 * @return ROK - success
209 ***************************************************************************/
211 uint8_t duSctpCfgReq(SctpParams sctpCfg)
216 #ifdef CALL_FLOW_DEBUG_LOG
217 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_CFG_REQ_TO_SCTP\n");
220 f1Params.destIpAddr.ipV4Pres = sctpCfg.cuIpAddr.ipV4Pres;
221 f1Params.destIpAddr.ipV4Addr = sctpCfg.cuIpAddr.ipV4Addr;
222 f1Params.destPort = sctpCfg.cuPort;
223 f1Params.itfState = DU_SCTP_DOWN;
224 f1Params.srcPort = sctpCfg.duPort[F1_INTERFACE];
225 memset (&f1Params.sockFd, -1, sizeof(CmInetFd));
226 fillDestNetAddr(&f1Params.destIpNetAddr, &f1Params.destIpAddr);
227 fillAddrLst(&f1Params.destAddrLst, &f1Params.destIpAddr);
229 /* Fill RIC Params */
230 ricParams.destIpAddr.ipV4Pres = sctpCfg.ricIpAddr.ipV4Pres;
231 ricParams.destIpAddr.ipV4Addr = sctpCfg.ricIpAddr.ipV4Addr;
232 ricParams.destPort = sctpCfg.ricPort;
233 ricParams.itfState = DU_SCTP_DOWN;
234 ricParams.srcPort = sctpCfg.duPort[E2_INTERFACE];
235 memset (&ricParams.sockFd, -1, sizeof(CmInetFd));
236 fillDestNetAddr(&ricParams.destIpNetAddr, &ricParams.destIpAddr);
237 fillAddrLst(&ricParams.destAddrLst, &ricParams.destIpAddr);
239 /* Fill AddressList */
240 fillAddrLst(&localAddrLst, &sctpCfg.duIpAddr);
242 /* Set polling to FALSE */
243 pollingState = FALSE;
245 /* Fill Cfm Status */
246 cfm.status = LCM_PRIM_OK;
247 cfm.reason = LCM_REASON_NOT_APPL;
249 ret = duCheckReqStatus(&cfm);
254 /*******************************************************************
256 * @brief Fills the address List of the source Ip Address
260 * Function : fillAddrLst
263 * Fills the address List of source Ip Address
265 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
266 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
268 * @return ROK - success
271 ******************************************************************/
273 uint8_t fillAddrLst(CmInetNetAddrLst *addrLstPtr, F1IpAddr *ipAddr)
276 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
277 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_UINT32(ipAddr->ipV4Addr);
282 /******************************************************************************
284 * @brief Fills the address List of the source Ip Address
288 * Function : fillDestNetAddr
291 * Fills the address List of destinatoion Ip Address
293 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
294 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
296 * @return ROK - success
299 *******************************************************************************/
300 uint8_t fillDestNetAddr(CmInetNetAddr *destAddrPtr, F1IpAddr *dstIpPtr)
302 /* Filling destination address */
303 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
304 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_UINT32(dstIpPtr->ipV4Addr);
308 /******************************************************************************
310 * @brief Establishes the Assoc Req for the received interface type
314 * Function : establishAssocReq
317 * Eastablishes the Assoc Req for the received interface type
319 * @params[in] DuSctpDestCb *paramPtr
321 * @return ROK - success
324 *******************************************************************************/
326 uint8_t establishReq(DuSctpDestCb *paramPtr)
330 socket_type = CM_INET_STREAM;
332 if((ret = cmInetSocket(socket_type, ¶mPtr->sockFd, IPPROTO_SCTP)) != ROK)
334 DU_LOG("\nERROR --> SCTP : Failed while opening a socket in ODU");
336 else if((ret = cmInetSctpBindx(¶mPtr->sockFd, &localAddrLst, paramPtr->srcPort)) != ROK)
338 DU_LOG("\nERROR --> SCTP: Failed during Binding in ODU");
340 else if((ret = sctpSetSockOpts(¶mPtr->sockFd)) != ROK)
342 DU_LOG("\nERROR --> SCTP : Failed to set Socket Opt in ODU");
348 DU_LOG("\nERROR --> SCTP : Failed while establishing Req at DU");
353 ret = cmInetSctpConnectx(¶mPtr->sockFd, ¶mPtr->destIpNetAddr, ¶mPtr->destAddrLst, paramPtr->destPort);
354 /* 115 error_code indicates that Operation is in progress and hence ignored if SctpConnect failed due to this */
361 if((ret == ROK) & (paramPtr->itfState == DU_SCTP_DOWN))
363 paramPtr->itfState = DU_SCTP_CONNECTING;
366 /* Post the EVTSTARTPOLL Msg */
370 duFillSctpPst(&pst, EVTSTARTPOLL);
376 /******************************************************************************
378 * @brief Processes the assoc Req for the received interface type
382 * Function : duSctpAssocReq
385 * Processes the Assoc Req for the received interface type
387 * @params[in] itfType interface Type,
388 * @params[in] cfm , represents the status of request
390 * @return ROK - success
393 *******************************************************************************/
395 uint8_t duSctpAssocReq(uint8_t itfType)
399 DuSctpDestCb *paramPtr = NULLP;
401 #ifdef CALL_FLOW_DEBUG_LOG
402 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_ASSOC_REQ_TO_SCTP\n");
405 DU_ALLOC(paramPtr, sizeof(DuSctpDestCb));
406 if(paramPtr == NULLP)
408 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
415 paramPtr = &f1Params;
416 ret = establishReq(paramPtr);
421 paramPtr = &ricParams;
422 ret = establishReq(paramPtr);
427 DU_LOG("\nERROR --> SCTP : Invalid Interface Type %d", itfType);
433 DU_LOG("\nERROR --> SCTP : ASSOC Req Failed.");
434 cfm.status = LCM_PRIM_NOK;
435 cfm.reason = LCM_REASON_NOT_APPL;
439 cfm.status = LCM_PRIM_OK;
440 cfm.reason = LCM_REASON_NOT_APPL;
442 ret = duCheckReqStatus(&cfm);
446 /*******************************************************************
448 * @brief Fills Pst struct for ENTSCTP
452 * Function : duFillSctpPst
455 * Fills Pst struct for ENTSCTP
458 * @return ROK - success
461 * ****************************************************************/
462 uint8_t duFillSctpPst(Pst *pst, Event event)
465 if(ODU_GET_MSG_BUF(DFLT_REGION, DU_POOL, &mBuf) != ROK)
467 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
470 memset(pst, 0, sizeof(Pst));
471 pst->srcEnt = (Ent)ENTDUAPP;
472 pst->srcInst = (Inst)DU_INST;
473 pst->srcProcId = DU_PROC;
474 pst->dstEnt = (Ent)ENTSCTP;
475 pst->dstInst = (Inst)SCTP_INST;
476 pst->dstProcId = pst->srcProcId;
478 pst->selector = ODU_SELECTOR_LC;
480 ODU_POST_TASK(pst, mBuf);
485 /*******************************************************************
487 * @brief Sets socket options as per requirement
491 * Function : sctpSetSockOpts
494 * Sets socket options as per requirement
496 * @params[in] sock_Fd determines the sockFd to be set
497 * @return ROK - success
500 * ****************************************************************/
501 uint8_t sctpSetSockOpts(CmInetFd *sock_Fd)
504 CmSctpEvent sctpEvent;
506 sctpEvent.dataIoEvent = TRUE;
507 sctpEvent.associationEvent = TRUE;
508 sctpEvent.addressEvent = TRUE;
509 sctpEvent.sendFailureEvent = TRUE;
510 sctpEvent.peerErrorEvent = TRUE;
511 sctpEvent.shutdownEvent = TRUE;
512 sctpEvent.partialDeliveryEvent = TRUE;
513 sctpEvent.adaptationLayerEvent = TRUE;
515 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent) != ROK))
523 /*******************************************************************
525 * @brief Post received data/notification to DU APP
529 * Function : sendToDuApp
532 * Post received data/notification to DU APP
534 * @params[in] Message buffer
537 * @return ROK - success
540 * ****************************************************************/
541 void sendToDuApp(Buffer *mBuf, Event event)
544 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
545 ODU_PRINT_MSG(mBuf, 0, 0);
548 memset(&(pst), 0, sizeof(Pst));
549 pst.srcEnt = (Ent)ENTSCTP;
550 pst.srcInst = (Inst)SCTP_INST;
551 pst.srcProcId = DU_PROC;
552 pst.dstEnt = (Ent)ENTDUAPP;
553 pst.dstInst = (Inst)DU_INST;
554 pst.dstProcId = pst.srcProcId;
556 pst.selector = ODU_SELECTOR_LC;
558 pst.region = DFLT_REGION;
560 if (ODU_POST_TASK(&pst, mBuf) != ROK)
562 DU_LOG("\nERROR --> SCTP : ODU_POST_TASK failed in duReadCfg");
566 /*******************************************************************
568 * @brief Handles an SCTP notification message
571 * Function : sctpNtfyHdlr
574 * Handles an SCTP notification message
576 * @params[in] Notify message
578 * @return ROK - success
581 * ****************************************************************/
582 uint8_t sctpNtfyHdlr(CmInetSctpNotification *ntfy, uint8_t *itfState, uint8_t interface)
586 switch(ntfy->header.nType)
588 case CM_INET_SCTP_ASSOC_CHANGE :
589 DU_LOG("\nDEBUG --> SCTP : Assoc change notification received");
590 switch(ntfy->u.assocChange.state)
592 case CM_INET_SCTP_COMM_UP:
593 DU_LOG("INFO --> Event : COMMUNICATION UP");
594 *itfState = DU_SCTP_UP;
596 case CM_INET_SCTP_COMM_LOST:
597 DU_LOG("INFO --> Event : COMMUNICATION LOST");
598 *itfState = DU_SCTP_DOWN;
600 case CM_INET_SCTP_RESTART:
601 DU_LOG("INFO --> Event : SCTP RESTART");
602 *itfState = DU_SCTP_DOWN;
604 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
605 DU_LOG("INFO --> Event : SHUTDOWN COMPLETE");
606 *itfState = DU_SCTP_DOWN;
608 case CM_INET_SCTP_CANT_STR_ASSOC:
609 DU_LOG("INFO --> Event : CANT START ASSOC");
610 *itfState = DU_SCTP_DOWN;
613 DU_LOG("\nERROR --> Invalid event %d", ntfy->u.assocChange.state);
617 case CM_INET_SCTP_PEER_ADDR_CHANGE :
618 DU_LOG("\nINFO --> SCTP : Peer Address Change notificarion received");
619 /* Need to add handler */
621 case CM_INET_SCTP_REMOTE_ERROR :
622 DU_LOG("\nINFO --> SCTP : Remote Error notification received");
624 case CM_INET_SCTP_SEND_FAILED :
625 DU_LOG("\nINFO --> SCTP : Send Failed notification received\n");
627 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
629 DU_LOG("\nINFO --> SCTP : Shutdown Event notification received\n");
630 *itfState = DU_SCTP_DOWN;
636 removeE2NodeInformation();
643 case CM_INET_SCTP_ADAPTATION_INDICATION :
644 DU_LOG("\nINFO --> SCTP : Adaptation Indication received\n");
646 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
647 DU_LOG("\nINFO --> SCTP : Partial Delivery Event received\n");
650 DU_LOG("\nERROR --> SCTP : Invalid sctp notification type %d", ntfy->header.nType);
654 /* Pack notification and send to APP */
655 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
657 memset(&(pst), 0, sizeof(Pst));
658 pst.srcEnt = (Ent)ENTSCTP;
659 pst.srcInst = (Inst)SCTP_INST;
660 pst.srcProcId = DU_PROC;
661 pst.dstEnt = (Ent)ENTDUAPP;
662 pst.dstInst = (Inst)DU_INST;
663 pst.dstProcId = pst.srcProcId;
664 pst.event = EVENT_SCTP_NTFY;
665 pst.selector = ODU_SELECTOR_LC;
667 pst.region = DU_APP_MEM_REGION;
669 if(cmPkSctpNtfy(&pst, ntfy) != ROK)
671 DU_LOG("\nERROR --> SCTP : Failed to pack SCTP notification");
677 /*******************************************************************
679 * @brief checks for valid readFd and process the InetSctpRecvMsg
684 * Function : processPolling
687 * checks for valid readFd and process the InetSctpRecvMsg
690 * @params[in] Params required for polling
691 * @params[in] SockFd for file descriptor
692 * @params[in] timeoutPtr indicates the timeout value
693 * @params[in] MemInfo
695 * @return ROK - success
698 * ****************************************************************/
700 uint8_t processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, uint32_t *timeoutPtr, CmInetMemInfo *memInfo)
703 CM_INET_FD_SET(sockFd, &pollParams->readFd);
704 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, (int16_t *)&pollParams->numFd);
705 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
707 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
708 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &(pollParams->mBuf), &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
712 DU_LOG("\nERROR --> SCTP: Failed to receive sctp msg for sockFd[%d]\n", sockFd->fd);
716 #ifdef CALL_FLOW_DEBUG_LOG
717 if(pollParams->port == f1Params.destPort)
718 DU_LOG("\nCall Flow: CU -> ENTSCTP : EVENT_SCTP_MSG\n");
720 DU_LOG("\nCall Flow: RIC -> ENTSCTP : EVENT_SCTP_MSG\n");
722 if((((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0)) && ret == ROK)
724 if(pollParams->port == f1Params.destPort)
726 f1Params.assocId = pollParams->ntfy.u.assocChange.assocId;
727 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to F1Params from PollParams [%d]\n", f1Params.assocId);
728 ret = sctpNtfyHdlr(&pollParams->ntfy, &f1Params.itfState, F1_INTERFACE);
730 else if(pollParams->port == ricParams.destPort)
732 ricParams.assocId = pollParams->ntfy.u.assocChange.assocId;
733 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to ricParams from PollParams [%d]\n", ricParams.assocId);
734 ret = sctpNtfyHdlr(&pollParams->ntfy, &ricParams.itfState, E2_INTERFACE);
738 DU_LOG("\nERROR --> SCTP : Failed to fill AssocId\n");
743 DU_LOG("\nERROR --> SCTP : Failed to process sctp notify msg\n");
746 else if(f1Params.itfState & (pollParams->port == f1Params.destPort))
748 sendToDuApp(pollParams->mBuf, EVENT_CU_DATA);
750 else if(ricParams.itfState & (pollParams->port == ricParams.destPort))
752 sendToDuApp(pollParams->mBuf, EVENT_RIC_DATA);
757 ODU_PUT_MSG_BUF(pollParams->mBuf);
763 /*******************************************************************
765 * @brief Receives message on the socket
769 * Function : sctpSockPoll
772 * Receives message on the socket
775 * @return ROK - success
778 * ****************************************************************/
779 uint8_t sctpSockPoll()
783 uint32_t *timeout_Ptr;
784 CmInetMemInfo memInfo;
785 sctpSockPollParams f1PollParams, e2PollParams;
787 memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
788 memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
790 if (f1Params.sockFd.blocking & ricParams.sockFd.blocking)
799 timeout_Ptr = &timeout;
801 memInfo.region = DU_APP_MEM_REGION;
802 memInfo.pool = DU_POOL;
804 CM_INET_FD_ZERO(&f1PollParams.readFd);
805 CM_INET_FD_ZERO(&e2PollParams.readFd);
807 DU_LOG("\nINFO --> SCTP : Polling started at DU\n");
810 if(f1Params.itfState)
812 if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeout_Ptr, &memInfo)) != ROK)
814 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for F1\n");
817 if(ricParams.itfState)
819 if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeout_Ptr, &memInfo)) != ROK)
821 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for E2\n");
826 }/* End of sctpSockPoll() */
828 /*******************************************************************
830 * @brief Send message on SCTP socket
834 * Function : sctpSend
837 * Send message on SCTP socket
840 * @return ROK - success
843 * ****************************************************************/
844 uint8_t sctpSend(Buffer *mBuf, uint8_t itfType)
847 MsgLen len =0; /* number of actually sent octets */
848 CmInetMemInfo memInfo;
850 memInfo.region = DU_APP_MEM_REGION;
851 memInfo.pool = DU_POOL;
853 #ifdef CALL_FLOW_DEBUG_LOG
854 if(itfType == F1_INTERFACE)
856 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_F1AP_MSG_TO_SCTP\n");
860 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_E2AP_MSG_TO_SCTP\n");
864 if(itfType == F1_INTERFACE)
866 DU_LOG("\nDEBUG --> SCTP : sending the message to CU");
867 #ifdef CALL_FLOW_DEBUG_LOG
868 DU_LOG("\nCall Flow: ENTSCTP -> CU : EVENT_F1AP_MSG_TO_CU\n");
870 ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
873 if(itfType == E2_INTERFACE)
875 DU_LOG("\nDEBUG --> SCTP : sending the message to ric");
876 #ifdef CALL_FLOW_DEBUG_LOG
877 DU_LOG("\nCall Flow: ENTSCTP -> RIC : EVENT_E2AP_MSG_TO_RIC\n");
879 ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
882 if(ret != ROK && ret != RWOULDBLOCK)
884 DU_LOG("\nERROR --> SCTP : Failed sending the message");
889 } /* End of sctpSend */
891 /**********************************************************************
893 **********************************************************************/