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"
31 #include "du_app_rlc_inf.h"
35 /**************************************************************************
36 * @brief Task Initiation callback function.
40 * Function : sctpActvInit
43 * This function is supplied as one of parameters during SCTP's
44 * task registration. SSI will invoke this function once, after
45 * it creates and attaches this TAPA Task to a system task.
47 * @param[in] Ent entity, the entity ID of this task.
48 * @param[in] Inst inst, the instance ID of this task.
49 * @param[in] Region region, the region ID registered for memory
51 * @param[in] Reason reason.
52 * @return ROK - success
54 ***************************************************************************/
55 uint8_t sctpActvInit(Ent entity, Inst inst, Region region, Reason reason)
57 DU_LOG("\n\nDEBUG --> SCTP : Initializing");
58 ODU_SET_PROC_ID(DU_PROC);
61 memset(&f1Params, 0, sizeof(DuSctpDestCb));
62 f1Params.assocId = -1;
63 memset(&ricParams, 0, sizeof(DuSctpDestCb));
64 ricParams.assocId = -1;
70 /**************************************************************************
71 * @brief Function prints src, dest, msg infor about all the msgs that received.
75 * Function : callFlowSctpActvTsk
77 * Function prints src, dest, msg infor about all the msgs that received
79 * @param[in] Pst *pst, Post structure of the primitive.
82 ***************************************************************************/
83 void callFlowSctpActvTsk(Pst *pst)
86 char destTask[50]="ENTSCTP";
93 strcpy(sourceTask,"ENTDUAPP");
98 strcpy(message,"EVTSTARTPOLL");
105 DU_LOG("\nCall Flow: %s -> %s : %s\n", sourceTask, destTask, message);
108 /**************************************************************************
109 * @brief Task Activation callback function.
113 * Function : sctpActvTsk
116 * This function handles all SCTP messages received
117 * This API is registered with SSI during the
118 * Task Registration of DU APP.
120 * @param[in] Pst *pst, Post structure of the primitive.
121 * @param[in] Buffer *mBuf, Packed primitive parameters in the
123 * @return ROK - success
126 ***************************************************************************/
127 uint8_t sctpActvTsk(Pst *pst, Buffer *mBuf)
130 #ifdef CALL_FLOW_DEBUG_LOG
131 callFlowSctpActvTsk(pst);
153 /*******************************************************************
155 * @brief Checks the status of the received information
159 * Function : duCheckReqStatus
162 * Checks the status of the received information
164 * @params[in] Confirm status
165 * @return ROK - success
168 ******************************************************************/
169 uint8_t duCheckReqStatus(CmStatus *cfm)
172 if(cfm->status != LCM_PRIM_OK)
174 DU_LOG("\nERROR --> DU_APP : Failed to process the request successfully");
180 /**************************************************************************
181 * @brief Function to configure the Sctp Params during config Request
185 * Function : duSctpCfgReq
188 * This function configures SCTP Params during the config Request
190 * @param[in] SctpParams sctpCfg, common structure which has all the configuration
191 * @param[in] CmStatus cfm, Builds the cfm status and reason
194 * @return ROK - success
197 ***************************************************************************/
199 uint8_t duSctpCfgReq(SctpParams sctpCfg)
204 #ifdef CALL_FLOW_DEBUG_LOG
205 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_CFG_REQ_TO_SCTP\n");
208 f1Params.destIpAddr.ipV4Pres = sctpCfg.cuIpAddr.ipV4Pres;
209 f1Params.destIpAddr.ipV4Addr = sctpCfg.cuIpAddr.ipV4Addr;
210 f1Params.destPort = sctpCfg.cuPort;
211 f1Params.itfState = DU_SCTP_DOWN;
212 f1Params.srcPort = sctpCfg.duPort[F1_INTERFACE];
213 f1Params.recvMsgSet = ROK;
214 memset (&f1Params.sockFd, -1, sizeof(CmInetFd));
215 fillDestNetAddr(&f1Params.destIpNetAddr, &f1Params.destIpAddr);
216 fillAddrLst(&f1Params.destAddrLst, &f1Params.destIpAddr);
218 /* Fill RIC Params */
219 ricParams.destIpAddr.ipV4Pres = sctpCfg.ricIpAddr.ipV4Pres;
220 ricParams.destIpAddr.ipV4Addr = sctpCfg.ricIpAddr.ipV4Addr;
221 ricParams.destPort = sctpCfg.ricPort;
222 ricParams.itfState = DU_SCTP_DOWN;
223 ricParams.srcPort = sctpCfg.duPort[E2_INTERFACE];
224 ricParams.recvMsgSet = ROK;
225 memset (&ricParams.sockFd, -1, sizeof(CmInetFd));
226 fillDestNetAddr(&ricParams.destIpNetAddr, &ricParams.destIpAddr);
227 fillAddrLst(&ricParams.destAddrLst, &ricParams.destIpAddr);
229 /* Fill AddressList */
230 fillAddrLst(&localAddrLst, &sctpCfg.duIpAddr);
232 /* Set polling to FALSE */
233 pollingState = FALSE;
235 /* Fill Cfm Status */
236 cfm.status = LCM_PRIM_OK;
237 cfm.reason = LCM_REASON_NOT_APPL;
239 ret = duCheckReqStatus(&cfm);
244 /*******************************************************************
246 * @brief Fills the address List of the source Ip Address
250 * Function : fillAddrLst
253 * Fills the address List of source Ip Address
255 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
256 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
258 * @return ROK - success
261 ******************************************************************/
263 uint8_t fillAddrLst(CmInetNetAddrLst *addrLstPtr, F1IpAddr *ipAddr)
266 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
267 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_UINT32(ipAddr->ipV4Addr);
272 /******************************************************************************
274 * @brief Fills the address List of the source Ip Address
278 * Function : fillDestNetAddr
281 * Fills the address List of destinatoion Ip Address
283 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
284 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
286 * @return ROK - success
289 *******************************************************************************/
290 uint8_t fillDestNetAddr(CmInetNetAddr *destAddrPtr, F1IpAddr *dstIpPtr)
292 /* Filling destination address */
293 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
294 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_UINT32(dstIpPtr->ipV4Addr);
298 /******************************************************************************
300 * @brief Establishes the Assoc Req for the received interface type
304 * Function : establishAssocReq
307 * Eastablishes the Assoc Req for the received interface type
309 * @params[in] DuSctpDestCb *paramPtr
311 * @return ROK - success
314 *******************************************************************************/
316 uint8_t establishReq(DuSctpDestCb *paramPtr)
320 socket_type = CM_INET_STREAM;
322 if((ret = cmInetSocket(socket_type, ¶mPtr->sockFd, IPPROTO_SCTP)) != ROK)
324 DU_LOG("\nERROR --> SCTP : Failed while opening a socket in ODU");
326 else if((ret = cmInetSctpBindx(¶mPtr->sockFd, &localAddrLst, paramPtr->srcPort)) != ROK)
328 DU_LOG("\nERROR --> SCTP: Failed during Binding in ODU");
330 else if((ret = sctpSetSockOpts(¶mPtr->sockFd)) != ROK)
332 DU_LOG("\nERROR --> SCTP : Failed to set Socket Opt in ODU");
338 DU_LOG("\nERROR --> SCTP : Failed while establishing Req at DU");
343 ret = cmInetSctpConnectx(¶mPtr->sockFd, ¶mPtr->destIpNetAddr, ¶mPtr->destAddrLst, paramPtr->destPort);
344 /* 115 error_code indicates that Operation is in progress and hence ignored if SctpConnect failed due to this */
351 if((ret == ROK) & (paramPtr->itfState == DU_SCTP_DOWN))
353 paramPtr->itfState = DU_SCTP_CONNECTING;
356 /* Post the EVTSTARTPOLL Msg */
360 duFillSctpPst(&pst, EVTSTARTPOLL);
366 /******************************************************************************
368 * @brief Processes the assoc Req for the received interface type
372 * Function : duSctpAssocReq
375 * Processes the Assoc Req for the received interface type
377 * @params[in] itfType interface Type,
378 * @params[in] cfm , represents the status of request
380 * @return ROK - success
383 *******************************************************************************/
385 uint8_t duSctpAssocReq(uint8_t itfType)
389 DuSctpDestCb *paramPtr = NULLP;
391 #ifdef CALL_FLOW_DEBUG_LOG
392 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_ASSOC_REQ_TO_SCTP\n");
395 DU_ALLOC(paramPtr, sizeof(DuSctpDestCb));
396 if(paramPtr == NULLP)
398 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
405 paramPtr = &f1Params;
406 ret = establishReq(paramPtr);
411 paramPtr = &ricParams;
412 ret = establishReq(paramPtr);
417 DU_LOG("\nERROR --> SCTP : Invalid Interface Type %d", itfType);
423 DU_LOG("\nERROR --> SCTP : ASSOC Req Failed.");
424 cfm.status = LCM_PRIM_NOK;
425 cfm.reason = LCM_REASON_NOT_APPL;
429 cfm.status = LCM_PRIM_OK;
430 cfm.reason = LCM_REASON_NOT_APPL;
432 ret = duCheckReqStatus(&cfm);
436 /*******************************************************************
438 * @brief Fills Pst struct for ENTSCTP
442 * Function : duFillSctpPst
445 * Fills Pst struct for ENTSCTP
448 * @return ROK - success
451 * ****************************************************************/
452 uint8_t duFillSctpPst(Pst *pst, Event event)
455 if(ODU_GET_MSG_BUF(DFLT_REGION, DU_POOL, &mBuf) != ROK)
457 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
460 memset(pst, 0, sizeof(Pst));
461 pst->srcEnt = (Ent)ENTDUAPP;
462 pst->srcInst = (Inst)DU_INST;
463 pst->srcProcId = DU_PROC;
464 pst->dstEnt = (Ent)ENTSCTP;
465 pst->dstInst = (Inst)SCTP_INST;
466 pst->dstProcId = pst->srcProcId;
468 pst->selector = ODU_SELECTOR_LC;
470 ODU_POST_TASK(pst, mBuf);
475 /*******************************************************************
477 * @brief Sets socket options as per requirement
481 * Function : sctpSetSockOpts
484 * Sets socket options as per requirement
486 * @params[in] sock_Fd determines the sockFd to be set
487 * @return ROK - success
490 * ****************************************************************/
491 uint8_t sctpSetSockOpts(CmInetFd *sock_Fd)
494 CmSctpEvent sctpEvent;
496 sctpEvent.dataIoEvent = TRUE;
497 sctpEvent.associationEvent = TRUE;
498 sctpEvent.addressEvent = TRUE;
499 sctpEvent.sendFailureEvent = TRUE;
500 sctpEvent.peerErrorEvent = TRUE;
501 sctpEvent.shutdownEvent = TRUE;
502 sctpEvent.partialDeliveryEvent = TRUE;
503 sctpEvent.adaptationLayerEvent = TRUE;
505 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent) != ROK))
513 /*******************************************************************
515 * @brief Post received data/notification to DU APP
519 * Function : sendToDuApp
522 * Post received data/notification to DU APP
524 * @params[in] Message buffer
527 * @return ROK - success
530 * ****************************************************************/
531 void sendToDuApp(Buffer *mBuf, Event event)
534 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
535 ODU_PRINT_MSG(mBuf, 0, 0);
538 memset(&(pst), 0, sizeof(Pst));
539 pst.srcEnt = (Ent)ENTSCTP;
540 pst.srcInst = (Inst)SCTP_INST;
541 pst.srcProcId = DU_PROC;
542 pst.dstEnt = (Ent)ENTDUAPP;
543 pst.dstInst = (Inst)DU_INST;
544 pst.dstProcId = pst.srcProcId;
546 pst.selector = ODU_SELECTOR_LC;
548 pst.region = DFLT_REGION;
550 if (ODU_POST_TASK(&pst, mBuf) != ROK)
552 DU_LOG("\nERROR --> SCTP : ODU_POST_TASK failed in duReadCfg");
556 /*******************************************************************
558 * @brief Handles an SCTP notification message
561 * Function : sctpNtfyHdlr
564 * Handles an SCTP notification message
566 * @params[in] Notify message
568 * @return ROK - success
571 * ****************************************************************/
572 uint8_t sctpNtfyHdlr(CmInetSctpNotification *ntfy, uint8_t *itfState)
576 switch(ntfy->header.nType)
578 case CM_INET_SCTP_ASSOC_CHANGE :
579 DU_LOG("\nDEBUG --> SCTP : Assoc change notification received");
580 switch(ntfy->u.assocChange.state)
582 case CM_INET_SCTP_COMM_UP:
583 DU_LOG("INFO --> Event : COMMUNICATION UP");
584 *itfState = DU_SCTP_UP;
586 case CM_INET_SCTP_COMM_LOST:
587 DU_LOG("INFO --> Event : COMMUNICATION LOST");
588 *itfState = DU_SCTP_DOWN;
590 case CM_INET_SCTP_RESTART:
591 DU_LOG("INFO --> Event : SCTP RESTART");
592 *itfState = DU_SCTP_DOWN;
594 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
595 DU_LOG("INFO --> Event : SHUTDOWN COMPLETE");
596 *itfState = DU_SCTP_DOWN;
598 case CM_INET_SCTP_CANT_STR_ASSOC:
599 DU_LOG("INFO --> Event : CANT START ASSOC");
600 *itfState = DU_SCTP_DOWN;
603 DU_LOG("\nERROR --> Invalid event %d", ntfy->u.assocChange.state);
607 case CM_INET_SCTP_PEER_ADDR_CHANGE :
608 DU_LOG("\nINFO --> SCTP : Peer Address Change notificarion received");
609 /* Need to add handler */
611 case CM_INET_SCTP_REMOTE_ERROR :
612 DU_LOG("\nINFO --> SCTP : Remote Error notification received");
614 case CM_INET_SCTP_SEND_FAILED :
615 DU_LOG("\nINFO --> SCTP : Send Failed notification received\n");
617 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
618 DU_LOG("\nINFO --> SCTP : Shutdown Event notification received\n");
619 *itfState = DU_SCTP_DOWN;
622 case CM_INET_SCTP_ADAPTATION_INDICATION :
623 DU_LOG("\nINFO --> SCTP : Adaptation Indication received\n");
625 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
626 DU_LOG("\nINFO --> SCTP : Partial Delivery Event received\n");
629 DU_LOG("\nERROR --> SCTP : Invalid sctp notification type %d", ntfy->header.nType);
633 /* Pack notification and send to APP */
634 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
636 memset(&(pst), 0, sizeof(Pst));
637 pst.srcEnt = (Ent)ENTSCTP;
638 pst.srcInst = (Inst)SCTP_INST;
639 pst.srcProcId = DU_PROC;
640 pst.dstEnt = (Ent)ENTDUAPP;
641 pst.dstInst = (Inst)DU_INST;
642 pst.dstProcId = pst.srcProcId;
643 pst.event = EVENT_SCTP_NTFY;
644 pst.selector = ODU_SELECTOR_LC;
646 pst.region = DU_APP_MEM_REGION;
648 if(cmPkSctpNtfy(&pst, ntfy) != ROK)
650 DU_LOG("\nERROR --> SCTP : Failed to pack SCTP notification");
656 /*******************************************************************
658 * @brief checks for valid readFd and process the InetSctpRecvMsg
663 * Function : processPolling
666 * checks for valid readFd and process the InetSctpRecvMsg
669 * @params[in] Params required for polling
670 * @params[in] SockFd for file descriptor
671 * @params[in] timeoutPtr indicates the timeout value
672 * @params[in] MemInfo, recvMsgSet
674 * @return ROK - success
677 * ****************************************************************/
679 uint8_t processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, uint32_t *timeoutPtr, CmInetMemInfo *memInfo, bool recvMsgSet)
682 CM_INET_FD_SET(sockFd, &pollParams->readFd);
683 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, (int16_t *)&pollParams->numFd);
684 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
686 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
687 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &(pollParams->mBuf), &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
691 DU_LOG("\nERROR --> SCTP: Failed to receive sctp msg for sockFd[%d]\n", sockFd->fd);
692 recvMsgSet = RFAILED;
696 #ifdef CALL_FLOW_DEBUG_LOG
697 if(pollParams->port == f1Params.destPort)
698 DU_LOG("\nCall Flow: CU -> ENTSCTP : EVENT_SCTP_MSG\n");
700 DU_LOG("\nCall Flow: RIC -> ENTSCTP : EVENT_SCTP_MSG\n");
702 if((((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0)) && ret == ROK)
704 if(pollParams->port == f1Params.destPort)
706 f1Params.assocId = pollParams->ntfy.u.assocChange.assocId;
707 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to F1Params from PollParams [%d]\n", f1Params.assocId);
708 ret = sctpNtfyHdlr(&pollParams->ntfy, &f1Params.itfState);
710 else if(pollParams->port == ricParams.destPort)
712 ricParams.assocId = pollParams->ntfy.u.assocChange.assocId;
713 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to ricParams from PollParams [%d]\n", ricParams.assocId);
714 ret = sctpNtfyHdlr(&pollParams->ntfy, &ricParams.itfState);
718 DU_LOG("\nERROR --> SCTP : Failed to fill AssocId\n");
723 DU_LOG("\nERROR --> SCTP : Failed to process sctp notify msg\n");
726 else if(f1Params.itfState & (pollParams->port == f1Params.destPort))
728 sendToDuApp(pollParams->mBuf, EVENT_CU_DATA);
730 else if(ricParams.itfState & (pollParams->port == ricParams.destPort))
732 sendToDuApp(pollParams->mBuf, EVENT_RIC_DATA);
737 ODU_PUT_MSG_BUF(pollParams->mBuf);
743 /*******************************************************************
745 * @brief Receives message on the socket
749 * Function : sctpSockPoll
752 * Receives message on the socket
755 * @return ROK - success
758 * ****************************************************************/
759 uint8_t sctpSockPoll()
763 uint32_t *timeout_Ptr;
764 CmInetMemInfo memInfo;
765 sctpSockPollParams f1PollParams, e2PollParams;
767 memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
768 memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
770 if (f1Params.sockFd.blocking & ricParams.sockFd.blocking)
779 timeout_Ptr = &timeout;
781 memInfo.region = DU_APP_MEM_REGION;
782 memInfo.pool = DU_POOL;
784 CM_INET_FD_ZERO(&f1PollParams.readFd);
785 CM_INET_FD_ZERO(&e2PollParams.readFd);
787 DU_LOG("\nINFO --> SCTP : Polling started at DU\n");
790 if(f1Params.itfState)
792 if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeout_Ptr, &memInfo, f1Params.recvMsgSet)) != ROK)
794 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for F1\n");
797 if(ricParams.itfState)
799 if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeout_Ptr, &memInfo, ricParams.recvMsgSet)) != ROK)
801 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for E2\n");
806 }/* End of sctpSockPoll() */
808 /*******************************************************************
810 * @brief Send message on SCTP socket
814 * Function : sctpSend
817 * Send message on SCTP socket
820 * @return ROK - success
823 * ****************************************************************/
824 uint8_t sctpSend(Buffer *mBuf, uint8_t itfType)
827 MsgLen len =0; /* number of actually sent octets */
828 CmInetMemInfo memInfo;
830 memInfo.region = DU_APP_MEM_REGION;
831 memInfo.pool = DU_POOL;
833 #ifdef CALL_FLOW_DEBUG_LOG
834 if(itfType == F1_INTERFACE)
836 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_F1AP_MSG_TO_SCTP\n");
840 DU_LOG("\nCall Flow: ENTDUAPP -> ENTSCTP : EVENT_E2AP_MSG_TO_SCTP\n");
844 if(itfType == F1_INTERFACE)
846 DU_LOG("\nDEBUG --> SCTP : sending the message to CU");
847 #ifdef CALL_FLOW_DEBUG_LOG
848 DU_LOG("\nCall Flow: ENTSCTP -> CU : EVENT_F1AP_MSG_TO_CU\n");
850 ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
853 if(itfType == E2_INTERFACE)
855 DU_LOG("\nDEBUG --> SCTP : sending the message to ric");
856 #ifdef CALL_FLOW_DEBUG_LOG
857 DU_LOG("\nCall Flow: ENTSCTP -> RIC : EVENT_E2AP_MSG_TO_RIC\n");
859 ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
862 if(ret != ROK && ret != RWOULDBLOCK)
864 DU_LOG("\nERROR --> SCTP : Failed sending the message");
869 } /* End of sctpSend */
871 /**********************************************************************
873 **********************************************************************/