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"
30 /**************************************************************************
31 * @brief Task Initiation callback function.
35 * Function : sctpActvInit
38 * This function is supplied as one of parameters during SCTP's
39 * task registration. SSI will invoke this function once, after
40 * it creates and attaches this TAPA Task to a system task.
42 * @param[in] Ent entity, the entity ID of this task.
43 * @param[in] Inst inst, the instance ID of this task.
44 * @param[in] Region region, the region ID registered for memory
46 * @param[in] Reason reason.
47 * @return ROK - success
49 ***************************************************************************/
50 S16 sctpActvInit(Ent entity, Inst inst, Region region, Reason reason)
52 DU_LOG("\n\nSCTP : Initializing");
55 f1Params.assocId = -1;
56 ricParams.assocId = -1;
62 /**************************************************************************
63 * @brief Task Activation callback function.
67 * Function : sctpActvTsk
70 * This function handles all SCTP messages received
71 * This API is registered with SSI during the
72 * Task Registration of DU APP.
74 * @param[in] Pst *pst, Post structure of the primitive.
75 * @param[in] Buffer *mBuf, Packed primitive parameters in the
77 * @return ROK - success
80 ***************************************************************************/
81 S16 sctpActvTsk(Pst *pst, Buffer *mBuf)
101 /*******************************************************************
103 * @brief Checks the status of the received information
107 * Function : duCheckReqStatus
110 * Checks the status of the received information
112 * @params[in] Confirm status
113 * @return ROK - success
116 ******************************************************************/
117 S16 duCheckReqStatus(CmStatus *cfm)
120 if(cfm->status != LCM_PRIM_OK)
122 DU_LOG("\nDU_APP : Failed to process the request successfully");
128 /**************************************************************************
129 * @brief Function to configure the Sctp Params during config Request
133 * Function : duSctpCfgReq
136 * This function configures SCTP Params during the config Request
138 * @param[in] SctpParams sctpCfg, common structure which has all the configuration
139 * @param[in] CmStatus cfm, Builds the cfm status and reason
142 * @return ROK - success
145 ***************************************************************************/
147 S16 duSctpCfgReq(SctpParams sctpCfg)
153 f1Params.destIpAddr.ipV4Pres = sctpCfg.cuIpAddr.ipV4Pres;
154 f1Params.destIpAddr.ipV4Addr = sctpCfg.cuIpAddr.ipV4Addr;
155 f1Params.destPort = sctpCfg.cuPort;
156 f1Params.itfState = DU_SCTP_DOWN;
157 f1Params.srcPort = sctpCfg.duPort[F1_INTERFACE];
158 f1Params.recvMsgSet = ROK;
159 cmMemset ((U8 *)&f1Params.sockFd, -1, sizeof(CmInetFd));
160 fillDestNetAddr(&f1Params.destIpNetAddr, &f1Params.destIpAddr);
161 fillAddrLst(&f1Params.destAddrLst, &f1Params.destIpAddr);
163 /* Fill RIC Params */
164 ricParams.destIpAddr.ipV4Pres = sctpCfg.ricIpAddr.ipV4Pres;
165 ricParams.destIpAddr.ipV4Addr = sctpCfg.ricIpAddr.ipV4Addr;
166 ricParams.destPort = sctpCfg.ricPort;
167 ricParams.itfState = DU_SCTP_DOWN;
168 ricParams.srcPort = sctpCfg.duPort[E2_INTERFACE];
169 ricParams.recvMsgSet = ROK;
170 cmMemset ((U8 *)&ricParams.sockFd, -1, sizeof(CmInetFd));
171 fillDestNetAddr(&ricParams.destIpNetAddr, &ricParams.destIpAddr);
172 fillAddrLst(&ricParams.destAddrLst, &ricParams.destIpAddr);
174 /* Fill AddressList */
175 fillAddrLst(&localAddrLst, &sctpCfg.duIpAddr);
177 /* Set polling to FALSE */
178 pollingState = FALSE;
180 /* Fill Cfm Status */
181 cfm.status = LCM_PRIM_OK;
182 cfm.reason = LCM_REASON_NOT_APPL;
184 ret = duCheckReqStatus(&cfm);
189 /*******************************************************************
191 * @brief Fills the address List of the source Ip Address
195 * Function : fillAddrLst
198 * Fills the address List of source Ip Address
200 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
201 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
203 * @return ROK - success
206 ******************************************************************/
208 S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, F1IpAddr *ipAddr)
211 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
212 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_U32(ipAddr->ipV4Addr);
217 /******************************************************************************
219 * @brief Fills the address List of the source Ip Address
223 * Function : fillDestNetAddr
226 * Fills the address List of destinatoion Ip Address
228 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
229 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
231 * @return ROK - success
234 *******************************************************************************/
235 S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, F1IpAddr *dstIpPtr)
237 /* Filling destination address */
238 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
239 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_U32(dstIpPtr->ipV4Addr);
243 /******************************************************************************
245 * @brief Establishes the Assoc Req for the received interface type
249 * Function : establishAssocReq
252 * Eastablishes the Assoc Req for the received interface type
254 * @params[in] DuSctpDestCb *paramPtr
256 * @return ROK - success
259 *******************************************************************************/
261 S16 establishReq(DuSctpDestCb *paramPtr)
265 socket_type = CM_INET_STREAM;
267 if((ret = cmInetSocket(socket_type, ¶mPtr->sockFd, IPPROTO_SCTP)) != ROK)
269 DU_LOG("\nSCTP : Failed while opening a socket in ODU");
271 else if((ret = cmInetSctpBindx(¶mPtr->sockFd, &localAddrLst, paramPtr->srcPort)) != ROK)
273 DU_LOG("\nSCTP: Failed during Binding in ODU");
275 else if((ret = sctpSetSockOpts(¶mPtr->sockFd)) != ROK)
277 DU_LOG("\nSCTP : Failed to set Socket Opt in ODU");
283 DU_LOG("\nSCTP : Failed while establishing Req at DU");
288 ret = cmInetSctpConnectx(¶mPtr->sockFd, ¶mPtr->destIpNetAddr, ¶mPtr->destAddrLst, paramPtr->destPort);
289 /* 115 error_code indicates that Operation is in progress and hence ignored if SctpConnect failed due to this */
296 if((ret == ROK) & (paramPtr->itfState == DU_SCTP_DOWN))
298 paramPtr->itfState = DU_SCTP_CONNECTING;
301 /* Post the EVTSTARTPOLL Msg */
305 duFillSctpPst(&pst, EVTSTARTPOLL);
311 /******************************************************************************
313 * @brief Processes the assoc Req for the received interface type
317 * Function : duSctpAssocReq
320 * Processes the Assoc Req for the received interface type
322 * @params[in] itfType interface Type,
323 * @params[in] cfm , represents the status of request
325 * @return ROK - success
328 *******************************************************************************/
330 S16 duSctpAssocReq(U8 itfType)
334 DuSctpDestCb *paramPtr = NULLP;
336 if(SGetSBuf(DU_APP_MEM_REGION, DU_POOL, (Data **)¶mPtr, (Size)sizeof(DuSctpDestCb)) != ROK)
338 printf("\nDU_APP : Failed to allocate memory");
345 paramPtr = &f1Params;
346 ret = establishReq(paramPtr);
351 paramPtr = &ricParams;
352 ret = establishReq(paramPtr);
357 DU_LOG("\nSCTP : Invalid Interface Type");
363 DU_LOG("\nSCTP : ASSOC Req Failed.");
364 cfm.status = LCM_PRIM_NOK;
365 cfm.reason = LCM_REASON_NOT_APPL;
369 cfm.status = LCM_PRIM_OK;
370 cfm.reason = LCM_REASON_NOT_APPL;
372 ret = duCheckReqStatus(&cfm);
376 /*******************************************************************
378 * @brief Fills Pst struct for ENTSCTP
382 * Function : duFillSctpPst
385 * Fills Pst struct for ENTSCTP
388 * @return ROK - success
391 * ****************************************************************/
392 S16 duFillSctpPst(Pst *pst, Event event)
395 if(SGetMsg(DFLT_REGION, DU_POOL, &mBuf) != ROK)
397 printf("\nDU_APP : Failed to allocate memory");
400 cmMemset((U8 *)pst, 0, sizeof(Pst));
401 pst->srcEnt = (Ent)ENTDUAPP;
402 pst->srcInst = (Inst)DU_INST;
403 pst->srcProcId = DU_PROC;
404 pst->dstEnt = (Ent)ENTSCTP;
405 pst->dstInst = (Inst)SCTP_INST;
406 pst->dstProcId = pst->srcProcId;
408 pst->selector = ODU_SELECTOR_LC;
415 /*******************************************************************
417 * @brief Sets socket options as per requirement
421 * Function : sctpSetSockOpts
424 * Sets socket options as per requirement
426 * @params[in] sock_Fd determines the sockFd to be set
427 * @return ROK - success
430 * ****************************************************************/
431 S16 sctpSetSockOpts(CmInetFd *sock_Fd)
434 CmSctpEvent sctpEvent;
436 sctpEvent.dataIoEvent = TRUE;
437 sctpEvent.associationEvent = TRUE;
438 sctpEvent.addressEvent = TRUE;
439 sctpEvent.sendFailureEvent = TRUE;
440 sctpEvent.peerErrorEvent = TRUE;
441 sctpEvent.shutdownEvent = TRUE;
442 sctpEvent.partialDeliveryEvent = TRUE;
443 sctpEvent.adaptationLayerEvent = TRUE;
445 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent) != ROK))
453 /*******************************************************************
455 * @brief Post received data/notification to DU APP
459 * Function : sendToDuApp
462 * Post received data/notification to DU APP
464 * @params[in] Message buffer
467 * @return ROK - success
470 * ****************************************************************/
471 void sendToDuApp(Buffer *mBuf, Event event)
474 DU_LOG("\nSCTP : Forwarding received message to duApp");
475 SPrntMsg(mBuf, 0, 0);
478 cmMemset((U8 *)&(pst), 0, sizeof(Pst));
479 pst.srcEnt = (Ent)ENTSCTP;
480 pst.srcInst = (Inst)SCTP_INST;
481 pst.srcProcId = DU_PROC;
482 pst.dstEnt = (Ent)ENTDUAPP;
483 pst.dstInst = (Inst)DU_INST;
484 pst.dstProcId = pst.srcProcId;
486 pst.selector = ODU_SELECTOR_LC;
488 pst.region = DFLT_REGION;
490 if (SPstTsk(&pst, mBuf) != ROK)
492 DU_LOG("\nSCTP : SPstTsk failed in duReadCfg");
496 /*******************************************************************
498 * @brief Handles an SCTP notification message
501 * Function : sctpNtfyHdlr
504 * Handles an SCTP notification message
506 * @params[in] Notify message
508 * @return ROK - success
511 * ****************************************************************/
512 S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy, U8 *itfState)
516 switch(ntfy->header.nType)
518 case CM_INET_SCTP_ASSOC_CHANGE :
519 DU_LOG("\nSCTP : Assoc change notification received");
520 switch(ntfy->u.assocChange.state)
522 case CM_INET_SCTP_COMM_UP:
523 DU_LOG("Event : COMMUNICATION UP");
524 *itfState = DU_SCTP_UP;
526 case CM_INET_SCTP_COMM_LOST:
527 DU_LOG("Event : COMMUNICATION LOST");
528 *itfState = DU_SCTP_DOWN;
530 case CM_INET_SCTP_RESTART:
531 DU_LOG("Event : SCTP RESTART");
532 *itfState = DU_SCTP_DOWN;
534 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
535 DU_LOG("Event : SHUTDOWN COMPLETE");
536 *itfState = DU_SCTP_DOWN;
538 case CM_INET_SCTP_CANT_STR_ASSOC:
539 DU_LOG("Event : CANT START ASSOC");
540 *itfState = DU_SCTP_DOWN;
543 DU_LOG("\nInvalid event");
547 case CM_INET_SCTP_PEER_ADDR_CHANGE :
548 DU_LOG("\nSCTP : Peer Address Change notificarion received");
549 /* Need to add handler */
551 case CM_INET_SCTP_REMOTE_ERROR :
552 DU_LOG("\nSCTP : Remote Error notification received");
554 case CM_INET_SCTP_SEND_FAILED :
555 DU_LOG("\nSCTP : Send Failed notification received\n");
557 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
558 DU_LOG("\nSCTP : Shutdown Event notification received\n");
559 *itfState = DU_SCTP_DOWN;
562 case CM_INET_SCTP_ADAPTATION_INDICATION :
563 DU_LOG("\nSCTP : Adaptation Indication received\n");
565 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
566 DU_LOG("\nSCTP : Partial Delivery Event received\n");
569 DU_LOG("\nSCTP : Invalid sctp notification type\n");
573 /* Pack notification and send to APP */
574 DU_LOG("\nSCTP : Forwarding received message to duApp");
576 cmMemset((U8 *)&(pst), 0, sizeof(Pst));
577 pst.srcEnt = (Ent)ENTSCTP;
578 pst.srcInst = (Inst)SCTP_INST;
579 pst.srcProcId = DU_PROC;
580 pst.dstEnt = (Ent)ENTDUAPP;
581 pst.dstInst = (Inst)DU_INST;
582 pst.dstProcId = pst.srcProcId;
583 pst.event = EVENT_SCTP_NTFY;
584 pst.selector = ODU_SELECTOR_LC;
586 pst.region = DFLT_REGION;
588 if(cmPkSctpNtfy(&pst, ntfy) != ROK)
590 DU_LOG("\nSCTP : Failed to pack SCTP notification");
596 /*******************************************************************
598 * @brief checks for valid readFd and process the InetSctpRecvMsg
603 * Function : processPolling
606 * checks for valid readFd and process the InetSctpRecvMsg
609 * @params[in] Params required for polling
610 * @params[in] SockFd for file descriptor
611 * @params[in] timeoutPtr indicates the timeout value
612 * @params[in] MemInfo, recvMsgSet
614 * @return ROK - success
617 * ****************************************************************/
619 S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, U32 *timeoutPtr, CmInetMemInfo *memInfo, Bool recvMsgSet)
622 CM_INET_FD_SET(sockFd, &pollParams->readFd);
623 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, &pollParams->numFd);
624 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
626 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
627 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &(pollParams->mBuf), &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
631 DU_LOG("\n SCTP: Failed to receive sctp msg for sockFd[%d]\n", sockFd->fd);
632 recvMsgSet = RFAILED;
636 if((((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0)) && ret == ROK)
638 if(pollParams->port == f1Params.destPort)
640 f1Params.assocId = pollParams->ntfy.u.assocChange.assocId;
641 DU_LOG("\nSCTP : AssocId assigned to F1Params from PollParams [%d]\n", f1Params.assocId);
642 ret = sctpNtfyHdlr(&pollParams->ntfy, &f1Params.itfState);
644 else if(pollParams->port == ricParams.destPort)
646 ricParams.assocId = pollParams->ntfy.u.assocChange.assocId;
647 DU_LOG("\nSCTP : AssocId assigned to ricParams from PollParams [%d]\n", ricParams.assocId);
648 ret = sctpNtfyHdlr(&pollParams->ntfy, &ricParams.itfState);
652 DU_LOG("\nSCTP : Failed to fill AssocId\n");
657 DU_LOG("\nSCTP : Failed to process sctp notify msg\n");
660 else if(f1Params.itfState & (pollParams->port == f1Params.destPort))
662 sendToDuApp(pollParams->mBuf, EVENT_CU_DATA);
664 else if(ricParams.itfState & (pollParams->port == ricParams.destPort))
666 sendToDuApp(pollParams->mBuf, EVENT_RIC_DATA);
671 SPutMsg(pollParams->mBuf);
677 /*******************************************************************
679 * @brief Receives message on the socket
683 * Function : sctpSockPoll
686 * Receives message on the socket
689 * @return ROK - success
692 * ****************************************************************/
698 CmInetMemInfo memInfo;
699 sctpSockPollParams f1PollParams, e2PollParams;
701 memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
702 memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
704 if (f1Params.sockFd.blocking & ricParams.sockFd.blocking)
713 timeout_Ptr = &timeout;
715 memInfo.region = DU_APP_MEM_REGION;
716 memInfo.pool = DU_POOL;
718 CM_INET_FD_ZERO(&f1PollParams.readFd);
719 CM_INET_FD_ZERO(&e2PollParams.readFd);
721 DU_LOG("\nSCTP : Polling started at DU\n");
724 if(f1Params.itfState)
726 if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeout_Ptr, &memInfo, f1Params.recvMsgSet)) != ROK)
728 DU_LOG("\nSCTP : Failed to RecvMsg for F1\n");
731 if(ricParams.itfState)
733 if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeout_Ptr, &memInfo, ricParams.recvMsgSet)) != ROK)
735 DU_LOG("\nSCTP : Failed to RecvMsg for E2\n");
740 }/* End of sctpSockPoll() */
742 /*******************************************************************
744 * @brief Send message on SCTP socket
748 * Function : sctpSend
751 * Send message on SCTP socket
754 * @return ROK - success
757 * ****************************************************************/
758 S16 sctpSend(Buffer *mBuf, U8 itfType)
761 MsgLen len; /* number of actually sent octets */
762 CmInetMemInfo memInfo;
764 memInfo.region = DU_APP_MEM_REGION;
765 memInfo.pool = DU_POOL;
767 if(itfType == F1_INTERFACE)
769 DU_LOG("\nSCTP : sending the message to DuApp");
770 ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
773 if(itfType == E2_INTERFACE)
775 DU_LOG("\nSCTP : sending the message to ric");
776 ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
779 if(ret != ROK && ret != RWOULDBLOCK)
781 DU_LOG("\nSCTP : Failed sending the message");
786 } /* End of sctpSend */
788 /**********************************************************************
790 **********************************************************************/