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 */
26 /**************************************************************************
27 * @brief Task Initiation callback function.
31 * Function : sctpActvInit
34 * This function is supplied as one of parameters during SCTP's
35 * task registration. SSI will invoke this function once, after
36 * it creates and attaches this TAPA Task to a system task.
38 * @param[in] Ent entity, the entity ID of this task.
39 * @param[in] Inst inst, the instance ID of this task.
40 * @param[in] Region region, the region ID registered for memory
42 * @param[in] Reason reason.
43 * @return ROK - success
45 ***************************************************************************/
46 S16 sctpActvInit(Ent entity, Inst inst, Region region, Reason reason)
48 DU_LOG("\n\nSCTP : Initializing");
51 f1Params.assocId = -1;
52 ricParams.assocId = -1;
58 /**************************************************************************
59 * @brief Task Activation callback function.
63 * Function : sctpActvTsk
66 * This function handles all SCTP messages received
67 * This API is registered with SSI during the
68 * Task Registration of DU APP.
70 * @param[in] Pst *pst, Post structure of the primitive.
71 * @param[in] Buffer *mBuf, Packed primitive parameters in the
73 * @return ROK - success
76 ***************************************************************************/
77 S16 sctpActvTsk(Pst *pst, Buffer *mBuf)
97 /*******************************************************************
99 * @brief Checks the status of the received information
103 * Function : duCheckReqStatus
106 * Checks the status of the received information
108 * @params[in] Confirm status
109 * @return ROK - success
112 ******************************************************************/
113 S16 duCheckReqStatus(CmStatus *cfm)
116 if(cfm->status != LCM_PRIM_OK)
118 DU_LOG("\nDU_APP : Failed to process the request successfully");
124 /**************************************************************************
125 * @brief Function to configure the Sctp Params during config Request
129 * Function : duSctpCfgReq
132 * This function configures SCTP Params during the config Request
134 * @param[in] SctpParams sctpCfg, common structure which has all the configuration
135 * @param[in] CmStatus cfm, Builds the cfm status and reason
138 * @return ROK - success
141 ***************************************************************************/
143 S16 duSctpCfgReq(SctpParams sctpCfg)
149 f1Params.destIpAddr.ipV4Pres = sctpCfg.cuIpAddr.ipV4Pres;
150 f1Params.destIpAddr.ipV4Addr = sctpCfg.cuIpAddr.ipV4Addr;
151 f1Params.destPort = sctpCfg.cuPort;
152 f1Params.itfState = DU_SCTP_DOWN;
153 f1Params.srcPort = sctpCfg.duPort[F1_INTERFACE];
154 f1Params.recvMsgSet = ROK;
155 cmMemset ((U8 *)&f1Params.sockFd, -1, sizeof(CmInetFd));
156 fillDestNetAddr(&f1Params.destIpNetAddr, &f1Params.destIpAddr);
157 fillAddrLst(&f1Params.destAddrLst, &f1Params.destIpAddr);
159 /* Fill RIC Params */
160 ricParams.destIpAddr.ipV4Pres = sctpCfg.ricIpAddr.ipV4Pres;
161 ricParams.destIpAddr.ipV4Addr = sctpCfg.ricIpAddr.ipV4Addr;
162 ricParams.destPort = sctpCfg.ricPort;
163 ricParams.itfState = DU_SCTP_DOWN;
164 ricParams.srcPort = sctpCfg.duPort[E2_INTERFACE];
165 ricParams.recvMsgSet = ROK;
166 cmMemset ((U8 *)&ricParams.sockFd, -1, sizeof(CmInetFd));
167 fillDestNetAddr(&ricParams.destIpNetAddr, &ricParams.destIpAddr);
168 fillAddrLst(&ricParams.destAddrLst, &ricParams.destIpAddr);
170 /* Fill AddressList */
171 fillAddrLst(&localAddrLst, &sctpCfg.duIpAddr);
173 /* Set polling to FALSE */
174 pollingState = FALSE;
176 /* Fill Cfm Status */
177 cfm.status = LCM_PRIM_OK;
178 cfm.reason = LCM_REASON_NOT_APPL;
180 ret = duCheckReqStatus(&cfm);
185 /*******************************************************************
187 * @brief Fills the address List of the source Ip Address
191 * Function : fillAddrLst
194 * Fills the address List of source Ip Address
196 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
197 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
199 * @return ROK - success
202 ******************************************************************/
204 S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, F1IpAddr *ipAddr)
207 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
208 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_U32(ipAddr->ipV4Addr);
213 /******************************************************************************
215 * @brief Fills the address List of the source Ip Address
219 * Function : fillDestNetAddr
222 * Fills the address List of destinatoion Ip Address
224 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
225 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
227 * @return ROK - success
230 *******************************************************************************/
231 S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, F1IpAddr *dstIpPtr)
233 /* Filling destination address */
234 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
235 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_U32(dstIpPtr->ipV4Addr);
239 /******************************************************************************
241 * @brief Establishes the Assoc Req for the received interface type
245 * Function : establishAssocReq
248 * Eastablishes the Assoc Req for the received interface type
250 * @params[in] DuSctpDestCb *paramPtr
252 * @return ROK - success
255 *******************************************************************************/
257 S16 establishReq(DuSctpDestCb *paramPtr)
261 socket_type = CM_INET_STREAM;
263 if((ret = cmInetSocket(socket_type, ¶mPtr->sockFd, IPPROTO_SCTP)) != ROK)
265 DU_LOG("\nSCTP : Failed while opening a socket in ODU");
267 else if((ret = cmInetSctpBindx(¶mPtr->sockFd, &localAddrLst, paramPtr->srcPort)) != ROK)
269 DU_LOG("\nSCTP: Failed during Binding in ODU");
271 else if((ret = sctpSetSockOpts(¶mPtr->sockFd)) != ROK)
273 DU_LOG("\nSCTP : Failed to set Socket Opt in ODU");
279 DU_LOG("\nSCTP : Failed while establishing Req at DU");
284 ret = cmInetSctpConnectx(¶mPtr->sockFd, ¶mPtr->destIpNetAddr, ¶mPtr->destAddrLst, paramPtr->destPort);
285 /* 115 error_code indicates that Operation is in progress and hence ignored if SctpConnect failed due to this */
292 if((ret == ROK) & (paramPtr->itfState == DU_SCTP_DOWN))
294 paramPtr->itfState = DU_SCTP_CONNECTING;
297 /* Post the EVTSTARTPOLL Msg */
301 duFillSctpPst(&pst, EVTSTARTPOLL);
307 /******************************************************************************
309 * @brief Processes the assoc Req for the received interface type
313 * Function : duSctpAssocReq
316 * Processes the Assoc Req for the received interface type
318 * @params[in] itfType interface Type,
319 * @params[in] cfm , represents the status of request
321 * @return ROK - success
324 *******************************************************************************/
326 S16 duSctpAssocReq(U8 itfType)
330 DuSctpDestCb *paramPtr = NULLP;
332 if(SGetSBuf(DU_APP_MEM_REGION, DU_POOL, (Data **)¶mPtr, (Size)sizeof(DuSctpDestCb)) != ROK)
334 printf("\nDU_APP : Failed to allocate memory");
341 paramPtr = &f1Params;
342 ret = establishReq(paramPtr);
347 paramPtr = &ricParams;
348 ret = establishReq(paramPtr);
353 DU_LOG("\nSCTP : Invalid Interface Type");
359 DU_LOG("\nSCTP : ASSOC Req Failed.");
360 cfm.status = LCM_PRIM_NOK;
361 cfm.reason = LCM_REASON_NOT_APPL;
365 cfm.status = LCM_PRIM_OK;
366 cfm.reason = LCM_REASON_NOT_APPL;
368 ret = duCheckReqStatus(&cfm);
372 /*******************************************************************
374 * @brief Fills Pst struct for ENTSCTP
378 * Function : duFillSctpPst
381 * Fills Pst struct for ENTSCTP
384 * @return ROK - success
387 * ****************************************************************/
388 S16 duFillSctpPst(Pst *pst, Event event)
391 if(SGetMsg(DFLT_REGION, DU_POOL, &mBuf) != ROK)
393 printf("\nDU_APP : Failed to allocate memory");
396 cmMemset((U8 *)pst, 0, sizeof(Pst));
397 pst->srcEnt = (Ent)ENTDUAPP;
398 pst->srcInst = (Inst)DU_INST;
399 pst->srcProcId = DU_PROC;
400 pst->dstEnt = (Ent)ENTSCTP;
401 pst->dstInst = (Inst)SCTP_INST;
402 pst->dstProcId = pst->srcProcId;
404 pst->selector = DU_SELECTOR_LC;
411 /*******************************************************************
413 * @brief Sets socket options as per requirement
417 * Function : sctpSetSockOpts
420 * Sets socket options as per requirement
422 * @params[in] sock_Fd determines the sockFd to be set
423 * @return ROK - success
426 * ****************************************************************/
427 S16 sctpSetSockOpts(CmInetFd *sock_Fd)
430 CmSctpEvent sctpEvent;
432 sctpEvent.dataIoEvent = TRUE;
433 sctpEvent.associationEvent = TRUE;
434 sctpEvent.addressEvent = TRUE;
435 sctpEvent.sendFailureEvent = TRUE;
436 sctpEvent.peerErrorEvent = TRUE;
437 sctpEvent.shutdownEvent = TRUE;
438 sctpEvent.partialDeliveryEvent = TRUE;
439 sctpEvent.adaptationLayerEvent = TRUE;
441 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent) != ROK))
449 /*******************************************************************
451 * @brief Post received data/notification to DU APP
455 * Function : sendToDuApp
458 * Post received data/notification to DU APP
460 * @params[in] Message buffer
463 * @return ROK - success
466 * ****************************************************************/
467 void sendToDuApp(Buffer *mBuf, Event event)
470 DU_LOG("\nSCTP : Forwarding received message to duApp");
471 SPrntMsg(mBuf, 0, 0);
474 cmMemset((U8 *)&(pst), 0, sizeof(Pst));
475 pst.srcEnt = (Ent)ENTSCTP;
476 pst.srcInst = (Inst)SCTP_INST;
477 pst.srcProcId = DU_PROC;
478 pst.dstEnt = (Ent)ENTDUAPP;
479 pst.dstInst = (Inst)DU_INST;
480 pst.dstProcId = pst.srcProcId;
482 pst.selector = DU_SELECTOR_LC;
484 pst.region = DFLT_REGION;
486 if (SPstTsk(&pst, mBuf) != ROK)
488 DU_LOG("\nSCTP : SPstTsk failed in duReadCfg");
492 /*******************************************************************
494 * @brief Handles an SCTP notification message
497 * Function : sctpNtfyHdlr
500 * Handles an SCTP notification message
502 * @params[in] Notify message
504 * @return ROK - success
507 * ****************************************************************/
508 S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy, U8 *itfState)
512 switch(ntfy->header.nType)
514 case CM_INET_SCTP_ASSOC_CHANGE :
515 DU_LOG("\nSCTP : Assoc change notification received");
516 switch(ntfy->u.assocChange.state)
518 case CM_INET_SCTP_COMM_UP:
519 DU_LOG("Event : COMMUNICATION UP");
520 *itfState = DU_SCTP_UP;
522 case CM_INET_SCTP_COMM_LOST:
523 DU_LOG("Event : COMMUNICATION LOST");
524 *itfState = DU_SCTP_DOWN;
526 case CM_INET_SCTP_RESTART:
527 DU_LOG("Event : SCTP RESTART");
528 *itfState = DU_SCTP_DOWN;
530 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
531 DU_LOG("Event : SHUTDOWN COMPLETE");
532 *itfState = DU_SCTP_DOWN;
534 case CM_INET_SCTP_CANT_STR_ASSOC:
535 DU_LOG("Event : CANT START ASSOC");
536 *itfState = DU_SCTP_DOWN;
539 DU_LOG("\nInvalid event");
543 case CM_INET_SCTP_PEER_ADDR_CHANGE :
544 DU_LOG("\nSCTP : Peer Address Change notificarion received");
545 /* Need to add handler */
547 case CM_INET_SCTP_REMOTE_ERROR :
548 DU_LOG("\nSCTP : Remote Error notification received");
550 case CM_INET_SCTP_SEND_FAILED :
551 DU_LOG("\nSCTP : Send Failed notification received\n");
553 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
554 DU_LOG("\nSCTP : Shutdown Event notification received\n");
555 *itfState = DU_SCTP_DOWN;
558 case CM_INET_SCTP_ADAPTATION_INDICATION :
559 DU_LOG("\nSCTP : Adaptation Indication received\n");
561 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
562 DU_LOG("\nSCTP : Partial Delivery Event received\n");
565 DU_LOG("\nSCTP : Invalid sctp notification type\n");
569 /* Pack notification and send to APP */
570 DU_LOG("\nSCTP : Forwarding received message to duApp");
572 cmMemset((U8 *)&(pst), 0, sizeof(Pst));
573 pst.srcEnt = (Ent)ENTSCTP;
574 pst.srcInst = (Inst)SCTP_INST;
575 pst.srcProcId = DU_PROC;
576 pst.dstEnt = (Ent)ENTDUAPP;
577 pst.dstInst = (Inst)DU_INST;
578 pst.dstProcId = pst.srcProcId;
579 pst.event = EVENT_SCTP_NTFY;
580 pst.selector = DU_SELECTOR_LC;
582 pst.region = DFLT_REGION;
584 if(cmPkSctpNtfy(&pst, ntfy) != ROK)
586 DU_LOG("\nSCTP : Failed to pack SCTP notification");
592 /*******************************************************************
594 * @brief checks for valid readFd and process the InetSctpRecvMsg
599 * Function : processPolling
602 * checks for valid readFd and process the InetSctpRecvMsg
605 * @params[in] Params required for polling
606 * @params[in] SockFd for file descriptor
607 * @params[in] timeoutPtr indicates the timeout value
608 * @params[in] MemInfo, recvMsgSet
610 * @return ROK - success
613 * ****************************************************************/
615 S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, U32 *timeoutPtr, CmInetMemInfo *memInfo, Bool recvMsgSet)
618 CM_INET_FD_SET(sockFd, &pollParams->readFd);
619 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, &pollParams->numFd);
620 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
622 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
623 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &(pollParams->mBuf), &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
627 DU_LOG("\n SCTP: Failed to receive sctp msg for sockFd[%d]\n", sockFd->fd);
628 recvMsgSet = RFAILED;
632 if((((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0)) && ret == ROK)
634 if(pollParams->port == f1Params.destPort)
636 f1Params.assocId = pollParams->ntfy.u.assocChange.assocId;
637 DU_LOG("\nSCTP : AssocId assigned to F1Params from PollParams [%d]\n", f1Params.assocId);
638 ret = sctpNtfyHdlr(&pollParams->ntfy, &f1Params.itfState);
640 else if(pollParams->port == ricParams.destPort)
642 ricParams.assocId = pollParams->ntfy.u.assocChange.assocId;
643 DU_LOG("\nSCTP : AssocId assigned to ricParams from PollParams [%d]\n", ricParams.assocId);
644 ret = sctpNtfyHdlr(&pollParams->ntfy, &ricParams.itfState);
648 DU_LOG("\nSCTP : Failed to fill AssocId\n");
653 DU_LOG("\nSCTP : Failed to process sctp notify msg\n");
656 else if(f1Params.itfState & (pollParams->port == f1Params.destPort))
658 sendToDuApp(pollParams->mBuf, EVENT_CU_DATA);
660 else if(ricParams.itfState & (pollParams->port == ricParams.destPort))
662 sendToDuApp(pollParams->mBuf, EVENT_RIC_DATA);
667 SPutMsg(pollParams->mBuf);
673 /*******************************************************************
675 * @brief Receives message on the socket
679 * Function : sctpSockPoll
682 * Receives message on the socket
685 * @return ROK - success
688 * ****************************************************************/
694 CmInetMemInfo memInfo;
695 sctpSockPollParams f1PollParams, e2PollParams;
697 memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
698 memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
700 if (f1Params.sockFd.blocking & ricParams.sockFd.blocking)
709 timeout_Ptr = &timeout;
711 memInfo.region = DU_APP_MEM_REGION;
712 memInfo.pool = DU_POOL;
714 CM_INET_FD_ZERO(&f1PollParams.readFd);
715 CM_INET_FD_ZERO(&e2PollParams.readFd);
717 DU_LOG("\nSCTP : Polling started at DU\n");
720 if(f1Params.itfState)
722 if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeout_Ptr, &memInfo, f1Params.recvMsgSet)) != ROK)
724 DU_LOG("\nSCTP : Failed to RecvMsg for F1\n");
727 if(ricParams.itfState)
729 if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeout_Ptr, &memInfo, ricParams.recvMsgSet)) != ROK)
731 DU_LOG("\nSCTP : Failed to RecvMsg for E2\n");
736 }/* End of sctpSockPoll() */
738 /*******************************************************************
740 * @brief Send message on SCTP socket
744 * Function : sctpSend
747 * Send message on SCTP socket
750 * @return ROK - success
753 * ****************************************************************/
754 S16 sctpSend(Buffer *mBuf, U8 itfType)
757 MsgLen len; /* number of actually sent octets */
758 CmInetMemInfo memInfo;
760 memInfo.region = DU_APP_MEM_REGION;
761 memInfo.pool = DU_POOL;
763 if(itfType == F1_INTERFACE)
765 DU_LOG("\nSCTP : sending the message to DuApp");
766 ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
769 if(itfType == E2_INTERFACE)
771 DU_LOG("\nSCTP : sending the message to ric");
772 ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
775 if(ret != ROK && ret != RWOULDBLOCK)
777 DU_LOG("\nSCTP : Failed sending the message");
782 } /* End of sctpSend */
784 /**********************************************************************
786 **********************************************************************/