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"
27 #include "du_app_mac_inf.h"
33 /**************************************************************************
34 * @brief Task Initiation callback function.
38 * Function : sctpActvInit
41 * This function is supplied as one of parameters during SCTP's
42 * task registration. SSI will invoke this function once, after
43 * it creates and attaches this TAPA Task to a system task.
45 * @param[in] Ent entity, the entity ID of this task.
46 * @param[in] Inst inst, the instance ID of this task.
47 * @param[in] Region region, the region ID registered for memory
49 * @param[in] Reason reason.
50 * @return ROK - success
52 ***************************************************************************/
53 uint8_t sctpActvInit(Ent entity, Inst inst, Region region, Reason reason)
55 DU_LOG("\n\nDEBUG --> SCTP : Initializing");
56 ODU_SET_PROC_ID(DU_PROC);
59 memset(&f1Params, 0, sizeof(DuSctpDestCb));
60 f1Params.assocId = -1;
61 memset(&ricParams, 0, sizeof(DuSctpDestCb));
62 ricParams.assocId = -1;
68 /**************************************************************************
69 * @brief Function prints src, dest, msg infor about all the msgs that received.
73 * Function : callFlowSctpActvTsk
75 * Function prints src, dest, msg infor about all the msgs that received
77 * @param[in] Pst *pst, Post structure of the primitive.
80 ***************************************************************************/
81 uint8_t callFlowSctpActvTsk(Pst *pst)
84 char destTask[50]="ENTSCTP";
91 strcpy(sourceTask,"ENTDUAPP");
96 strcpy(message,"EVTSTARTPOLL");
103 DU_LOG("\nCall Flow: %s -> %s : %s\n", sourceTask, destTask, message);
106 /**************************************************************************
107 * @brief Task Activation callback function.
111 * Function : sctpActvTsk
114 * This function handles all SCTP messages received
115 * This API is registered with SSI during the
116 * Task Registration of DU APP.
118 * @param[in] Pst *pst, Post structure of the primitive.
119 * @param[in] Buffer *mBuf, Packed primitive parameters in the
121 * @return ROK - success
124 ***************************************************************************/
125 uint8_t sctpActvTsk(Pst *pst, Buffer *mBuf)
128 #ifdef CALL_FLOW_DEBUG_LOG
129 callFlowSctpActvTsk(pst);
151 /*******************************************************************
153 * @brief Checks the status of the received information
157 * Function : duCheckReqStatus
160 * Checks the status of the received information
162 * @params[in] Confirm status
163 * @return ROK - success
166 ******************************************************************/
167 uint8_t duCheckReqStatus(CmStatus *cfm)
170 if(cfm->status != LCM_PRIM_OK)
172 DU_LOG("\nERROR --> DU_APP : Failed to process the request successfully");
178 /**************************************************************************
179 * @brief Function to configure the Sctp Params during config Request
183 * Function : duSctpCfgReq
186 * This function configures SCTP Params during the config Request
188 * @param[in] SctpParams sctpCfg, common structure which has all the configuration
189 * @param[in] CmStatus cfm, Builds the cfm status and reason
192 * @return ROK - success
195 ***************************************************************************/
197 uint8_t duSctpCfgReq(SctpParams sctpCfg)
202 #ifdef CALL_FLOW_DEBUG_LOG
203 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_CFG_REQ_TO_SCTP\n");
206 f1Params.destIpAddr.ipV4Pres = sctpCfg.cuIpAddr.ipV4Pres;
207 f1Params.destIpAddr.ipV4Addr = sctpCfg.cuIpAddr.ipV4Addr;
208 f1Params.destPort = sctpCfg.cuPort;
209 f1Params.itfState = DU_SCTP_DOWN;
210 f1Params.srcPort = sctpCfg.duPort[F1_INTERFACE];
211 f1Params.recvMsgSet = ROK;
212 memset (&f1Params.sockFd, -1, sizeof(CmInetFd));
213 fillDestNetAddr(&f1Params.destIpNetAddr, &f1Params.destIpAddr);
214 fillAddrLst(&f1Params.destAddrLst, &f1Params.destIpAddr);
216 /* Fill RIC Params */
217 ricParams.destIpAddr.ipV4Pres = sctpCfg.ricIpAddr.ipV4Pres;
218 ricParams.destIpAddr.ipV4Addr = sctpCfg.ricIpAddr.ipV4Addr;
219 ricParams.destPort = sctpCfg.ricPort;
220 ricParams.itfState = DU_SCTP_DOWN;
221 ricParams.srcPort = sctpCfg.duPort[E2_INTERFACE];
222 ricParams.recvMsgSet = ROK;
223 memset (&ricParams.sockFd, -1, sizeof(CmInetFd));
224 fillDestNetAddr(&ricParams.destIpNetAddr, &ricParams.destIpAddr);
225 fillAddrLst(&ricParams.destAddrLst, &ricParams.destIpAddr);
227 /* Fill AddressList */
228 fillAddrLst(&localAddrLst, &sctpCfg.duIpAddr);
230 /* Set polling to FALSE */
231 pollingState = FALSE;
233 /* Fill Cfm Status */
234 cfm.status = LCM_PRIM_OK;
235 cfm.reason = LCM_REASON_NOT_APPL;
237 ret = duCheckReqStatus(&cfm);
242 /*******************************************************************
244 * @brief Fills the address List of the source Ip Address
248 * Function : fillAddrLst
251 * Fills the address List of source Ip Address
253 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
254 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
256 * @return ROK - success
259 ******************************************************************/
261 uint8_t fillAddrLst(CmInetNetAddrLst *addrLstPtr, F1IpAddr *ipAddr)
264 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
265 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_UINT32(ipAddr->ipV4Addr);
270 /******************************************************************************
272 * @brief Fills the address List of the source Ip Address
276 * Function : fillDestNetAddr
279 * Fills the address List of destinatoion Ip Address
281 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
282 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
284 * @return ROK - success
287 *******************************************************************************/
288 uint8_t fillDestNetAddr(CmInetNetAddr *destAddrPtr, F1IpAddr *dstIpPtr)
290 /* Filling destination address */
291 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
292 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_UINT32(dstIpPtr->ipV4Addr);
296 /******************************************************************************
298 * @brief Establishes the Assoc Req for the received interface type
302 * Function : establishAssocReq
305 * Eastablishes the Assoc Req for the received interface type
307 * @params[in] DuSctpDestCb *paramPtr
309 * @return ROK - success
312 *******************************************************************************/
314 uint8_t establishReq(DuSctpDestCb *paramPtr)
318 socket_type = CM_INET_STREAM;
320 if((ret = cmInetSocket(socket_type, ¶mPtr->sockFd, IPPROTO_SCTP)) != ROK)
322 DU_LOG("\nERROR --> SCTP : Failed while opening a socket in ODU");
324 else if((ret = cmInetSctpBindx(¶mPtr->sockFd, &localAddrLst, paramPtr->srcPort)) != ROK)
326 DU_LOG("\nERROR --> SCTP: Failed during Binding in ODU");
328 else if((ret = sctpSetSockOpts(¶mPtr->sockFd)) != ROK)
330 DU_LOG("\nERROR --> SCTP : Failed to set Socket Opt in ODU");
336 DU_LOG("\nERROR --> SCTP : Failed while establishing Req at DU");
341 ret = cmInetSctpConnectx(¶mPtr->sockFd, ¶mPtr->destIpNetAddr, ¶mPtr->destAddrLst, paramPtr->destPort);
342 /* 115 error_code indicates that Operation is in progress and hence ignored if SctpConnect failed due to this */
349 if((ret == ROK) & (paramPtr->itfState == DU_SCTP_DOWN))
351 paramPtr->itfState = DU_SCTP_CONNECTING;
354 /* Post the EVTSTARTPOLL Msg */
358 duFillSctpPst(&pst, EVTSTARTPOLL);
364 /******************************************************************************
366 * @brief Processes the assoc Req for the received interface type
370 * Function : duSctpAssocReq
373 * Processes the Assoc Req for the received interface type
375 * @params[in] itfType interface Type,
376 * @params[in] cfm , represents the status of request
378 * @return ROK - success
381 *******************************************************************************/
383 uint8_t duSctpAssocReq(uint8_t itfType)
387 DuSctpDestCb *paramPtr = NULLP;
389 #ifdef CALL_FLOW_DEBUG_LOG
390 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_ASSOC_REQ_TO_SCTP\n");
393 DU_ALLOC(paramPtr, sizeof(DuSctpDestCb));
394 if(paramPtr == NULLP)
396 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
403 paramPtr = &f1Params;
404 ret = establishReq(paramPtr);
409 paramPtr = &ricParams;
410 ret = establishReq(paramPtr);
415 DU_LOG("\nERROR --> SCTP : Invalid Interface Type %d", itfType);
421 DU_LOG("\nERROR --> SCTP : ASSOC Req Failed.");
422 cfm.status = LCM_PRIM_NOK;
423 cfm.reason = LCM_REASON_NOT_APPL;
427 cfm.status = LCM_PRIM_OK;
428 cfm.reason = LCM_REASON_NOT_APPL;
430 ret = duCheckReqStatus(&cfm);
434 /*******************************************************************
436 * @brief Fills Pst struct for ENTSCTP
440 * Function : duFillSctpPst
443 * Fills Pst struct for ENTSCTP
446 * @return ROK - success
449 * ****************************************************************/
450 uint8_t duFillSctpPst(Pst *pst, Event event)
453 if(ODU_GET_MSG_BUF(DFLT_REGION, DU_POOL, &mBuf) != ROK)
455 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
458 memset(pst, 0, sizeof(Pst));
459 pst->srcEnt = (Ent)ENTDUAPP;
460 pst->srcInst = (Inst)DU_INST;
461 pst->srcProcId = DU_PROC;
462 pst->dstEnt = (Ent)ENTSCTP;
463 pst->dstInst = (Inst)SCTP_INST;
464 pst->dstProcId = pst->srcProcId;
466 pst->selector = ODU_SELECTOR_LC;
468 ODU_POST_TASK(pst, mBuf);
473 /*******************************************************************
475 * @brief Sets socket options as per requirement
479 * Function : sctpSetSockOpts
482 * Sets socket options as per requirement
484 * @params[in] sock_Fd determines the sockFd to be set
485 * @return ROK - success
488 * ****************************************************************/
489 uint8_t sctpSetSockOpts(CmInetFd *sock_Fd)
492 CmSctpEvent sctpEvent;
494 sctpEvent.dataIoEvent = TRUE;
495 sctpEvent.associationEvent = TRUE;
496 sctpEvent.addressEvent = TRUE;
497 sctpEvent.sendFailureEvent = TRUE;
498 sctpEvent.peerErrorEvent = TRUE;
499 sctpEvent.shutdownEvent = TRUE;
500 sctpEvent.partialDeliveryEvent = TRUE;
501 sctpEvent.adaptationLayerEvent = TRUE;
503 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent) != ROK))
511 /*******************************************************************
513 * @brief Post received data/notification to DU APP
517 * Function : sendToDuApp
520 * Post received data/notification to DU APP
522 * @params[in] Message buffer
525 * @return ROK - success
528 * ****************************************************************/
529 void sendToDuApp(Buffer *mBuf, Event event)
532 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
533 ODU_PRINT_MSG(mBuf, 0, 0);
536 memset(&(pst), 0, sizeof(Pst));
537 pst.srcEnt = (Ent)ENTSCTP;
538 pst.srcInst = (Inst)SCTP_INST;
539 pst.srcProcId = DU_PROC;
540 pst.dstEnt = (Ent)ENTDUAPP;
541 pst.dstInst = (Inst)DU_INST;
542 pst.dstProcId = pst.srcProcId;
544 pst.selector = ODU_SELECTOR_LC;
546 pst.region = DFLT_REGION;
548 if (ODU_POST_TASK(&pst, mBuf) != ROK)
550 DU_LOG("\nERROR --> SCTP : ODU_POST_TASK failed in duReadCfg");
554 /*******************************************************************
556 * @brief Handles an SCTP notification message
559 * Function : sctpNtfyHdlr
562 * Handles an SCTP notification message
564 * @params[in] Notify message
566 * @return ROK - success
569 * ****************************************************************/
570 uint8_t sctpNtfyHdlr(CmInetSctpNotification *ntfy, uint8_t *itfState)
574 switch(ntfy->header.nType)
576 case CM_INET_SCTP_ASSOC_CHANGE :
577 DU_LOG("\nDEBUG --> SCTP : Assoc change notification received");
578 switch(ntfy->u.assocChange.state)
580 case CM_INET_SCTP_COMM_UP:
581 DU_LOG("INFO --> Event : COMMUNICATION UP");
582 *itfState = DU_SCTP_UP;
584 case CM_INET_SCTP_COMM_LOST:
585 DU_LOG("INFO --> Event : COMMUNICATION LOST");
586 *itfState = DU_SCTP_DOWN;
588 case CM_INET_SCTP_RESTART:
589 DU_LOG("INFO --> Event : SCTP RESTART");
590 *itfState = DU_SCTP_DOWN;
592 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
593 DU_LOG("INFO --> Event : SHUTDOWN COMPLETE");
594 *itfState = DU_SCTP_DOWN;
596 case CM_INET_SCTP_CANT_STR_ASSOC:
597 DU_LOG("INFO --> Event : CANT START ASSOC");
598 *itfState = DU_SCTP_DOWN;
601 DU_LOG("\nERROR --> Invalid event %d", ntfy->u.assocChange.state);
605 case CM_INET_SCTP_PEER_ADDR_CHANGE :
606 DU_LOG("\nINFO --> SCTP : Peer Address Change notificarion received");
607 /* Need to add handler */
609 case CM_INET_SCTP_REMOTE_ERROR :
610 DU_LOG("\nINFO --> SCTP : Remote Error notification received");
612 case CM_INET_SCTP_SEND_FAILED :
613 DU_LOG("\nINFO --> SCTP : Send Failed notification received\n");
615 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
616 DU_LOG("\nINFO --> SCTP : Shutdown Event notification received\n");
617 *itfState = DU_SCTP_DOWN;
620 case CM_INET_SCTP_ADAPTATION_INDICATION :
621 DU_LOG("\nINFO --> SCTP : Adaptation Indication received\n");
623 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
624 DU_LOG("\nINFO --> SCTP : Partial Delivery Event received\n");
627 DU_LOG("\nERROR --> SCTP : Invalid sctp notification type %d", ntfy->header.nType);
631 /* Pack notification and send to APP */
632 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
634 memset(&(pst), 0, sizeof(Pst));
635 pst.srcEnt = (Ent)ENTSCTP;
636 pst.srcInst = (Inst)SCTP_INST;
637 pst.srcProcId = DU_PROC;
638 pst.dstEnt = (Ent)ENTDUAPP;
639 pst.dstInst = (Inst)DU_INST;
640 pst.dstProcId = pst.srcProcId;
641 pst.event = EVENT_SCTP_NTFY;
642 pst.selector = ODU_SELECTOR_LC;
644 pst.region = DU_APP_MEM_REGION;
646 if(cmPkSctpNtfy(&pst, ntfy) != ROK)
648 DU_LOG("\nERROR --> SCTP : Failed to pack SCTP notification");
654 /*******************************************************************
656 * @brief checks for valid readFd and process the InetSctpRecvMsg
661 * Function : processPolling
664 * checks for valid readFd and process the InetSctpRecvMsg
667 * @params[in] Params required for polling
668 * @params[in] SockFd for file descriptor
669 * @params[in] timeoutPtr indicates the timeout value
670 * @params[in] MemInfo, recvMsgSet
672 * @return ROK - success
675 * ****************************************************************/
677 uint8_t processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, uint32_t *timeoutPtr, CmInetMemInfo *memInfo, bool recvMsgSet)
680 CM_INET_FD_SET(sockFd, &pollParams->readFd);
681 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, (int16_t *)&pollParams->numFd);
682 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
684 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
685 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &(pollParams->mBuf), &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
689 DU_LOG("\nERROR --> SCTP: Failed to receive sctp msg for sockFd[%d]\n", sockFd->fd);
690 recvMsgSet = RFAILED;
694 #ifdef CALL_FLOW_DEBUG_LOG
695 if(pollParams->port == f1Params.destPort)
696 DU_LOG("\nCall Flow: CU -> ENTSCTP : EVENT_SCTP_MSG\n");
698 DU_LOG("\nCall Flow: RIC -> ENTSCTP : EVENT_SCTP_MSG\n");
700 if((((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0)) && ret == ROK)
702 if(pollParams->port == f1Params.destPort)
704 f1Params.assocId = pollParams->ntfy.u.assocChange.assocId;
705 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to F1Params from PollParams [%d]\n", f1Params.assocId);
706 ret = sctpNtfyHdlr(&pollParams->ntfy, &f1Params.itfState);
708 else if(pollParams->port == ricParams.destPort)
710 ricParams.assocId = pollParams->ntfy.u.assocChange.assocId;
711 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to ricParams from PollParams [%d]\n", ricParams.assocId);
712 ret = sctpNtfyHdlr(&pollParams->ntfy, &ricParams.itfState);
716 DU_LOG("\nERROR --> SCTP : Failed to fill AssocId\n");
721 DU_LOG("\nERROR --> SCTP : Failed to process sctp notify msg\n");
724 else if(f1Params.itfState & (pollParams->port == f1Params.destPort))
726 sendToDuApp(pollParams->mBuf, EVENT_CU_DATA);
728 else if(ricParams.itfState & (pollParams->port == ricParams.destPort))
730 sendToDuApp(pollParams->mBuf, EVENT_RIC_DATA);
735 ODU_PUT_MSG_BUF(pollParams->mBuf);
741 /*******************************************************************
743 * @brief Receives message on the socket
747 * Function : sctpSockPoll
750 * Receives message on the socket
753 * @return ROK - success
756 * ****************************************************************/
757 uint8_t sctpSockPoll()
761 uint32_t *timeout_Ptr;
762 CmInetMemInfo memInfo;
763 sctpSockPollParams f1PollParams, e2PollParams;
765 memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
766 memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
768 if (f1Params.sockFd.blocking & ricParams.sockFd.blocking)
777 timeout_Ptr = &timeout;
779 memInfo.region = DU_APP_MEM_REGION;
780 memInfo.pool = DU_POOL;
782 CM_INET_FD_ZERO(&f1PollParams.readFd);
783 CM_INET_FD_ZERO(&e2PollParams.readFd);
785 DU_LOG("\nINFO --> SCTP : Polling started at DU\n");
788 if(f1Params.itfState)
790 if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeout_Ptr, &memInfo, f1Params.recvMsgSet)) != ROK)
792 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for F1\n");
795 if(ricParams.itfState)
797 if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeout_Ptr, &memInfo, ricParams.recvMsgSet)) != ROK)
799 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for E2\n");
804 }/* End of sctpSockPoll() */
806 /*******************************************************************
808 * @brief Send message on SCTP socket
812 * Function : sctpSend
815 * Send message on SCTP socket
818 * @return ROK - success
821 * ****************************************************************/
822 uint8_t sctpSend(Buffer *mBuf, uint8_t itfType)
825 MsgLen len =0; /* number of actually sent octets */
826 CmInetMemInfo memInfo;
828 memInfo.region = DU_APP_MEM_REGION;
829 memInfo.pool = DU_POOL;
831 #ifdef CALL_FLOW_DEBUG_LOG
832 if(itfType == F1_INTERFACE)
834 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_F1AP_MSG_TO_SCTP\n");
838 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_E2AP_MSG_TO_SCTP\n");
842 if(itfType == F1_INTERFACE)
844 DU_LOG("\nDEBUG --> SCTP : sending the message to CU");
845 #ifdef CALL_FLOW_DEBUG_LOG
846 DU_LOG("\nCall Flow: ENTSCTP -> CU : EVENT_F1AP_MSG_TO_CU\n");
848 ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
851 if(itfType == E2_INTERFACE)
853 DU_LOG("\nDEBUG --> SCTP : sending the message to ric");
854 #ifdef CALL_FLOW_DEBUG_LOG
855 DU_LOG("\nCall Flow: ENTSCTP -> RIC : EVENT_E2AP_MSG_TO_RIC\n");
857 ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
860 if(ret != ROK && ret != RWOULDBLOCK)
862 DU_LOG("\nERROR --> SCTP : Failed sending the message");
867 } /* End of sctpSend */
869 /**********************************************************************
871 **********************************************************************/