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"
28 #include "du_e2ap_mgr.h"
32 #include "du_app_rlc_inf.h"
36 /**************************************************************************
37 * @brief Task Initiation callback function.
41 * Function : sctpActvInit
44 * This function is supplied as one of parameters during SCTP's
45 * task registration. SSI will invoke this function once, after
46 * it creates and attaches this TAPA Task to a system task.
48 * @param[in] Ent entity, the entity ID of this task.
49 * @param[in] Inst inst, the instance ID of this task.
50 * @param[in] Region region, the region ID registered for memory
52 * @param[in] Reason reason.
53 * @return ROK - success
55 ***************************************************************************/
56 uint8_t sctpActvInit(Ent entity, Inst inst, Region region, Reason reason)
58 DU_LOG("\n\nDEBUG --> SCTP : Initializing");
59 ODU_SET_PROC_ID(DU_PROC);
62 memset(&f1Params, 0, sizeof(DuSctpDestCb));
63 f1Params.assocId = -1;
64 memset(&ricParams, 0, sizeof(DuSctpDestCb));
65 ricParams.assocId = -1;
71 /**************************************************************************
72 * @brief Function prints src, dest, msg infor about all the msgs that received.
76 * Function : callFlowSctpActvTsk
78 * Function prints src, dest, msg infor about all the msgs that received
80 * @param[in] Pst *pst, Post structure of the primitive.
83 ***************************************************************************/
84 void callFlowSctpActvTsk(Pst *pst)
87 char destTask[50]="ENTSCTP";
94 strcpy(sourceTask,"ENTDUAPP");
99 strcpy(message,"EVTSTARTPOLL");
106 DU_LOG("\nCall Flow: %s -> %s : %s\n", sourceTask, destTask, message);
109 /**************************************************************************
110 * @brief Task Activation callback function.
114 * Function : sctpActvTsk
117 * This function handles all SCTP messages received
118 * This API is registered with SSI during the
119 * Task Registration of DU APP.
121 * @param[in] Pst *pst, Post structure of the primitive.
122 * @param[in] Buffer *mBuf, Packed primitive parameters in the
124 * @return ROK - success
127 ***************************************************************************/
128 uint8_t sctpActvTsk(Pst *pst, Buffer *mBuf)
131 #ifdef CALL_FLOW_DEBUG_LOG
132 callFlowSctpActvTsk(pst);
154 /*******************************************************************
156 * @brief Checks the status of the received information
160 * Function : duCheckReqStatus
163 * Checks the status of the received information
165 * @params[in] Confirm status
166 * @return ROK - success
169 ******************************************************************/
170 uint8_t duCheckReqStatus(CmStatus *cfm)
173 if(cfm->status != LCM_PRIM_OK)
175 DU_LOG("\nERROR --> DU_APP : Failed to process the request successfully");
181 /**************************************************************************
182 * @brief Function to configure the Sctp Params during config Request
186 * Function : duSctpCfgReq
189 * This function configures SCTP Params during the config Request
191 * @param[in] SctpParams sctpCfg, common structure which has all the configuration
192 * @param[in] CmStatus cfm, Builds the cfm status and reason
195 * @return ROK - success
198 ***************************************************************************/
200 uint8_t duSctpCfgReq(SctpParams sctpCfg)
205 #ifdef CALL_FLOW_DEBUG_LOG
206 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_CFG_REQ_TO_SCTP\n");
209 f1Params.destIpAddr.ipV4Pres = sctpCfg.cuIpAddr.ipV4Pres;
210 f1Params.destIpAddr.ipV4Addr = sctpCfg.cuIpAddr.ipV4Addr;
211 f1Params.destPort = sctpCfg.cuPort;
212 f1Params.itfState = DU_SCTP_DOWN;
213 f1Params.srcPort = sctpCfg.duPort[F1_INTERFACE];
214 f1Params.recvMsgSet = ROK;
215 memset (&f1Params.sockFd, -1, sizeof(CmInetFd));
216 fillDestNetAddr(&f1Params.destIpNetAddr, &f1Params.destIpAddr);
217 fillAddrLst(&f1Params.destAddrLst, &f1Params.destIpAddr);
219 /* Fill RIC Params */
220 ricParams.destIpAddr.ipV4Pres = sctpCfg.ricIpAddr.ipV4Pres;
221 ricParams.destIpAddr.ipV4Addr = sctpCfg.ricIpAddr.ipV4Addr;
222 ricParams.destPort = sctpCfg.ricPort;
223 ricParams.itfState = DU_SCTP_DOWN;
224 ricParams.srcPort = sctpCfg.duPort[E2_INTERFACE];
225 ricParams.recvMsgSet = ROK;
226 memset (&ricParams.sockFd, -1, sizeof(CmInetFd));
227 fillDestNetAddr(&ricParams.destIpNetAddr, &ricParams.destIpAddr);
228 fillAddrLst(&ricParams.destAddrLst, &ricParams.destIpAddr);
230 /* Fill AddressList */
231 fillAddrLst(&localAddrLst, &sctpCfg.duIpAddr);
233 /* Set polling to FALSE */
234 pollingState = FALSE;
236 /* Fill Cfm Status */
237 cfm.status = LCM_PRIM_OK;
238 cfm.reason = LCM_REASON_NOT_APPL;
240 ret = duCheckReqStatus(&cfm);
245 /*******************************************************************
247 * @brief Fills the address List of the source Ip Address
251 * Function : fillAddrLst
254 * Fills the address List of source Ip Address
256 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
257 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
259 * @return ROK - success
262 ******************************************************************/
264 uint8_t fillAddrLst(CmInetNetAddrLst *addrLstPtr, F1IpAddr *ipAddr)
267 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
268 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_UINT32(ipAddr->ipV4Addr);
273 /******************************************************************************
275 * @brief Fills the address List of the source Ip Address
279 * Function : fillDestNetAddr
282 * Fills the address List of destinatoion Ip Address
284 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
285 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
287 * @return ROK - success
290 *******************************************************************************/
291 uint8_t fillDestNetAddr(CmInetNetAddr *destAddrPtr, F1IpAddr *dstIpPtr)
293 /* Filling destination address */
294 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
295 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_UINT32(dstIpPtr->ipV4Addr);
299 /******************************************************************************
301 * @brief Establishes the Assoc Req for the received interface type
305 * Function : establishAssocReq
308 * Eastablishes the Assoc Req for the received interface type
310 * @params[in] DuSctpDestCb *paramPtr
312 * @return ROK - success
315 *******************************************************************************/
317 uint8_t establishReq(DuSctpDestCb *paramPtr)
321 socket_type = CM_INET_STREAM;
323 if((ret = cmInetSocket(socket_type, ¶mPtr->sockFd, IPPROTO_SCTP)) != ROK)
325 DU_LOG("\nERROR --> SCTP : Failed while opening a socket in ODU");
327 else if((ret = cmInetSctpBindx(¶mPtr->sockFd, &localAddrLst, paramPtr->srcPort)) != ROK)
329 DU_LOG("\nERROR --> SCTP: Failed during Binding in ODU");
331 else if((ret = sctpSetSockOpts(¶mPtr->sockFd)) != ROK)
333 DU_LOG("\nERROR --> SCTP : Failed to set Socket Opt in ODU");
339 DU_LOG("\nERROR --> SCTP : Failed while establishing Req at DU");
344 ret = cmInetSctpConnectx(¶mPtr->sockFd, ¶mPtr->destIpNetAddr, ¶mPtr->destAddrLst, paramPtr->destPort);
345 /* 115 error_code indicates that Operation is in progress and hence ignored if SctpConnect failed due to this */
352 if((ret == ROK) & (paramPtr->itfState == DU_SCTP_DOWN))
354 paramPtr->itfState = DU_SCTP_CONNECTING;
357 /* Post the EVTSTARTPOLL Msg */
361 duFillSctpPst(&pst, EVTSTARTPOLL);
367 /******************************************************************************
369 * @brief Processes the assoc Req for the received interface type
373 * Function : duSctpAssocReq
376 * Processes the Assoc Req for the received interface type
378 * @params[in] itfType interface Type,
379 * @params[in] cfm , represents the status of request
381 * @return ROK - success
384 *******************************************************************************/
386 uint8_t duSctpAssocReq(uint8_t itfType)
390 DuSctpDestCb *paramPtr = NULLP;
392 #ifdef CALL_FLOW_DEBUG_LOG
393 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_ASSOC_REQ_TO_SCTP\n");
396 DU_ALLOC(paramPtr, sizeof(DuSctpDestCb));
397 if(paramPtr == NULLP)
399 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
406 paramPtr = &f1Params;
407 ret = establishReq(paramPtr);
412 paramPtr = &ricParams;
413 ret = establishReq(paramPtr);
418 DU_LOG("\nERROR --> SCTP : Invalid Interface Type %d", itfType);
424 DU_LOG("\nERROR --> SCTP : ASSOC Req Failed.");
425 cfm.status = LCM_PRIM_NOK;
426 cfm.reason = LCM_REASON_NOT_APPL;
430 cfm.status = LCM_PRIM_OK;
431 cfm.reason = LCM_REASON_NOT_APPL;
433 ret = duCheckReqStatus(&cfm);
437 /*******************************************************************
439 * @brief Fills Pst struct for ENTSCTP
443 * Function : duFillSctpPst
446 * Fills Pst struct for ENTSCTP
449 * @return ROK - success
452 * ****************************************************************/
453 uint8_t duFillSctpPst(Pst *pst, Event event)
456 if(ODU_GET_MSG_BUF(DFLT_REGION, DU_POOL, &mBuf) != ROK)
458 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
461 memset(pst, 0, sizeof(Pst));
462 pst->srcEnt = (Ent)ENTDUAPP;
463 pst->srcInst = (Inst)DU_INST;
464 pst->srcProcId = DU_PROC;
465 pst->dstEnt = (Ent)ENTSCTP;
466 pst->dstInst = (Inst)SCTP_INST;
467 pst->dstProcId = pst->srcProcId;
469 pst->selector = ODU_SELECTOR_LC;
471 ODU_POST_TASK(pst, mBuf);
476 /*******************************************************************
478 * @brief Sets socket options as per requirement
482 * Function : sctpSetSockOpts
485 * Sets socket options as per requirement
487 * @params[in] sock_Fd determines the sockFd to be set
488 * @return ROK - success
491 * ****************************************************************/
492 uint8_t sctpSetSockOpts(CmInetFd *sock_Fd)
495 CmSctpEvent sctpEvent;
497 sctpEvent.dataIoEvent = TRUE;
498 sctpEvent.associationEvent = TRUE;
499 sctpEvent.addressEvent = TRUE;
500 sctpEvent.sendFailureEvent = TRUE;
501 sctpEvent.peerErrorEvent = TRUE;
502 sctpEvent.shutdownEvent = TRUE;
503 sctpEvent.partialDeliveryEvent = TRUE;
504 sctpEvent.adaptationLayerEvent = TRUE;
506 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent) != ROK))
514 /*******************************************************************
516 * @brief Post received data/notification to DU APP
520 * Function : sendToDuApp
523 * Post received data/notification to DU APP
525 * @params[in] Message buffer
528 * @return ROK - success
531 * ****************************************************************/
532 void sendToDuApp(Buffer *mBuf, Event event)
535 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
536 ODU_PRINT_MSG(mBuf, 0, 0);
539 memset(&(pst), 0, sizeof(Pst));
540 pst.srcEnt = (Ent)ENTSCTP;
541 pst.srcInst = (Inst)SCTP_INST;
542 pst.srcProcId = DU_PROC;
543 pst.dstEnt = (Ent)ENTDUAPP;
544 pst.dstInst = (Inst)DU_INST;
545 pst.dstProcId = pst.srcProcId;
547 pst.selector = ODU_SELECTOR_LC;
549 pst.region = DFLT_REGION;
551 if (ODU_POST_TASK(&pst, mBuf) != ROK)
553 DU_LOG("\nERROR --> SCTP : ODU_POST_TASK failed in duReadCfg");
557 /*******************************************************************
559 * @brief Handles an SCTP notification message
562 * Function : sctpNtfyHdlr
565 * Handles an SCTP notification message
567 * @params[in] Notify message
569 * @return ROK - success
572 * ****************************************************************/
573 uint8_t sctpNtfyHdlr(CmInetSctpNotification *ntfy, uint8_t *itfState)
577 switch(ntfy->header.nType)
579 case CM_INET_SCTP_ASSOC_CHANGE :
580 DU_LOG("\nDEBUG --> SCTP : Assoc change notification received");
581 switch(ntfy->u.assocChange.state)
583 case CM_INET_SCTP_COMM_UP:
584 DU_LOG("INFO --> Event : COMMUNICATION UP");
585 *itfState = DU_SCTP_UP;
587 case CM_INET_SCTP_COMM_LOST:
588 DU_LOG("INFO --> Event : COMMUNICATION LOST");
589 *itfState = DU_SCTP_DOWN;
591 case CM_INET_SCTP_RESTART:
592 DU_LOG("INFO --> Event : SCTP RESTART");
593 *itfState = DU_SCTP_DOWN;
595 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
596 DU_LOG("INFO --> Event : SHUTDOWN COMPLETE");
597 *itfState = DU_SCTP_DOWN;
599 case CM_INET_SCTP_CANT_STR_ASSOC:
600 DU_LOG("INFO --> Event : CANT START ASSOC");
601 *itfState = DU_SCTP_DOWN;
604 DU_LOG("\nERROR --> Invalid event %d", ntfy->u.assocChange.state);
608 case CM_INET_SCTP_PEER_ADDR_CHANGE :
609 DU_LOG("\nINFO --> SCTP : Peer Address Change notificarion received");
610 /* Need to add handler */
612 case CM_INET_SCTP_REMOTE_ERROR :
613 DU_LOG("\nINFO --> SCTP : Remote Error notification received");
615 case CM_INET_SCTP_SEND_FAILED :
616 DU_LOG("\nINFO --> SCTP : Send Failed notification received\n");
618 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
619 DU_LOG("\nINFO --> SCTP : Shutdown Event notification received\n");
620 *itfState = DU_SCTP_DOWN;
623 case CM_INET_SCTP_ADAPTATION_INDICATION :
624 DU_LOG("\nINFO --> SCTP : Adaptation Indication received\n");
626 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
627 DU_LOG("\nINFO --> SCTP : Partial Delivery Event received\n");
630 DU_LOG("\nERROR --> SCTP : Invalid sctp notification type %d", ntfy->header.nType);
634 /* Pack notification and send to APP */
635 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
637 memset(&(pst), 0, sizeof(Pst));
638 pst.srcEnt = (Ent)ENTSCTP;
639 pst.srcInst = (Inst)SCTP_INST;
640 pst.srcProcId = DU_PROC;
641 pst.dstEnt = (Ent)ENTDUAPP;
642 pst.dstInst = (Inst)DU_INST;
643 pst.dstProcId = pst.srcProcId;
644 pst.event = EVENT_SCTP_NTFY;
645 pst.selector = ODU_SELECTOR_LC;
647 pst.region = DU_APP_MEM_REGION;
649 if(cmPkSctpNtfy(&pst, ntfy) != ROK)
651 DU_LOG("\nERROR --> SCTP : Failed to pack SCTP notification");
657 /*******************************************************************
659 * @brief checks for valid readFd and process the InetSctpRecvMsg
664 * Function : processPolling
667 * checks for valid readFd and process the InetSctpRecvMsg
670 * @params[in] Params required for polling
671 * @params[in] SockFd for file descriptor
672 * @params[in] timeoutPtr indicates the timeout value
673 * @params[in] MemInfo, recvMsgSet
675 * @return ROK - success
678 * ****************************************************************/
680 uint8_t processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, uint32_t *timeoutPtr, CmInetMemInfo *memInfo, bool recvMsgSet)
683 CM_INET_FD_SET(sockFd, &pollParams->readFd);
684 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, (int16_t *)&pollParams->numFd);
685 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
687 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
688 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &(pollParams->mBuf), &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
692 DU_LOG("\nERROR --> SCTP: Failed to receive sctp msg for sockFd[%d]\n", sockFd->fd);
693 recvMsgSet = RFAILED;
697 #ifdef CALL_FLOW_DEBUG_LOG
698 if(pollParams->port == f1Params.destPort)
699 DU_LOG("\nCall Flow: CU -> ENTSCTP : EVENT_SCTP_MSG\n");
701 DU_LOG("\nCall Flow: RIC -> ENTSCTP : EVENT_SCTP_MSG\n");
703 if((((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0)) && ret == ROK)
705 if(pollParams->port == f1Params.destPort)
707 f1Params.assocId = pollParams->ntfy.u.assocChange.assocId;
708 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to F1Params from PollParams [%d]\n", f1Params.assocId);
709 ret = sctpNtfyHdlr(&pollParams->ntfy, &f1Params.itfState);
711 else if(pollParams->port == ricParams.destPort)
713 ricParams.assocId = pollParams->ntfy.u.assocChange.assocId;
714 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to ricParams from PollParams [%d]\n", ricParams.assocId);
715 ret = sctpNtfyHdlr(&pollParams->ntfy, &ricParams.itfState);
719 DU_LOG("\nERROR --> SCTP : Failed to fill AssocId\n");
724 DU_LOG("\nERROR --> SCTP : Failed to process sctp notify msg\n");
727 else if(f1Params.itfState & (pollParams->port == f1Params.destPort))
729 sendToDuApp(pollParams->mBuf, EVENT_CU_DATA);
731 else if(ricParams.itfState & (pollParams->port == ricParams.destPort))
733 sendToDuApp(pollParams->mBuf, EVENT_RIC_DATA);
738 ODU_PUT_MSG_BUF(pollParams->mBuf);
744 /*******************************************************************
746 * @brief Receives message on the socket
750 * Function : sctpSockPoll
753 * Receives message on the socket
756 * @return ROK - success
759 * ****************************************************************/
760 uint8_t sctpSockPoll()
764 uint32_t *timeout_Ptr;
765 CmInetMemInfo memInfo;
766 sctpSockPollParams f1PollParams, e2PollParams;
768 memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
769 memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
771 if (f1Params.sockFd.blocking & ricParams.sockFd.blocking)
780 timeout_Ptr = &timeout;
782 memInfo.region = DU_APP_MEM_REGION;
783 memInfo.pool = DU_POOL;
785 CM_INET_FD_ZERO(&f1PollParams.readFd);
786 CM_INET_FD_ZERO(&e2PollParams.readFd);
788 DU_LOG("\nINFO --> SCTP : Polling started at DU\n");
791 if(f1Params.itfState)
793 if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeout_Ptr, &memInfo, f1Params.recvMsgSet)) != ROK)
795 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for F1\n");
798 if(ricParams.itfState)
800 if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeout_Ptr, &memInfo, ricParams.recvMsgSet)) != ROK)
802 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for E2\n");
807 }/* End of sctpSockPoll() */
809 /*******************************************************************
811 * @brief Send message on SCTP socket
815 * Function : sctpSend
818 * Send message on SCTP socket
821 * @return ROK - success
824 * ****************************************************************/
825 uint8_t sctpSend(Buffer *mBuf, uint8_t itfType)
828 MsgLen len =0; /* number of actually sent octets */
829 CmInetMemInfo memInfo;
831 memInfo.region = DU_APP_MEM_REGION;
832 memInfo.pool = DU_POOL;
834 #ifdef CALL_FLOW_DEBUG_LOG
835 if(itfType == F1_INTERFACE)
837 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_F1AP_MSG_TO_SCTP\n");
841 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_E2AP_MSG_TO_SCTP\n");
845 if(itfType == F1_INTERFACE)
847 DU_LOG("\nDEBUG --> SCTP : sending the message to CU");
848 #ifdef CALL_FLOW_DEBUG_LOG
849 DU_LOG("\nCall Flow: ENTSCTP -> CU : EVENT_F1AP_MSG_TO_CU\n");
851 ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
854 if(itfType == E2_INTERFACE)
856 DU_LOG("\nDEBUG --> SCTP : sending the message to ric");
857 #ifdef CALL_FLOW_DEBUG_LOG
858 DU_LOG("\nCall Flow: ENTSCTP -> RIC : EVENT_E2AP_MSG_TO_RIC\n");
860 ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
863 if(ret != ROK && ret != RWOULDBLOCK)
865 DU_LOG("\nERROR --> SCTP : Failed sending the message");
870 } /* End of sctpSend */
872 /**********************************************************************
874 **********************************************************************/