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 Task Activation callback function.
73 * Function : sctpActvTsk
76 * This function handles all SCTP messages received
77 * This API is registered with SSI during the
78 * Task Registration of DU APP.
80 * @param[in] Pst *pst, Post structure of the primitive.
81 * @param[in] Buffer *mBuf, Packed primitive parameters in the
83 * @return ROK - success
86 ***************************************************************************/
87 uint8_t sctpActvTsk(Pst *pst, Buffer *mBuf)
107 /*******************************************************************
109 * @brief Checks the status of the received information
113 * Function : duCheckReqStatus
116 * Checks the status of the received information
118 * @params[in] Confirm status
119 * @return ROK - success
122 ******************************************************************/
123 uint8_t duCheckReqStatus(CmStatus *cfm)
126 if(cfm->status != LCM_PRIM_OK)
128 DU_LOG("\nERROR --> DU_APP : Failed to process the request successfully");
134 /**************************************************************************
135 * @brief Function to configure the Sctp Params during config Request
139 * Function : duSctpCfgReq
142 * This function configures SCTP Params during the config Request
144 * @param[in] SctpParams sctpCfg, common structure which has all the configuration
145 * @param[in] CmStatus cfm, Builds the cfm status and reason
148 * @return ROK - success
151 ***************************************************************************/
153 uint8_t duSctpCfgReq(SctpParams sctpCfg)
159 f1Params.destIpAddr.ipV4Pres = sctpCfg.cuIpAddr.ipV4Pres;
160 f1Params.destIpAddr.ipV4Addr = sctpCfg.cuIpAddr.ipV4Addr;
161 f1Params.destPort = sctpCfg.cuPort;
162 f1Params.itfState = DU_SCTP_DOWN;
163 f1Params.srcPort = sctpCfg.duPort[F1_INTERFACE];
164 f1Params.recvMsgSet = ROK;
165 memset (&f1Params.sockFd, -1, sizeof(CmInetFd));
166 fillDestNetAddr(&f1Params.destIpNetAddr, &f1Params.destIpAddr);
167 fillAddrLst(&f1Params.destAddrLst, &f1Params.destIpAddr);
169 /* Fill RIC Params */
170 ricParams.destIpAddr.ipV4Pres = sctpCfg.ricIpAddr.ipV4Pres;
171 ricParams.destIpAddr.ipV4Addr = sctpCfg.ricIpAddr.ipV4Addr;
172 ricParams.destPort = sctpCfg.ricPort;
173 ricParams.itfState = DU_SCTP_DOWN;
174 ricParams.srcPort = sctpCfg.duPort[E2_INTERFACE];
175 ricParams.recvMsgSet = ROK;
176 memset (&ricParams.sockFd, -1, sizeof(CmInetFd));
177 fillDestNetAddr(&ricParams.destIpNetAddr, &ricParams.destIpAddr);
178 fillAddrLst(&ricParams.destAddrLst, &ricParams.destIpAddr);
180 /* Fill AddressList */
181 fillAddrLst(&localAddrLst, &sctpCfg.duIpAddr);
183 /* Set polling to FALSE */
184 pollingState = FALSE;
186 /* Fill Cfm Status */
187 cfm.status = LCM_PRIM_OK;
188 cfm.reason = LCM_REASON_NOT_APPL;
190 ret = duCheckReqStatus(&cfm);
195 /*******************************************************************
197 * @brief Fills the address List of the source Ip Address
201 * Function : fillAddrLst
204 * Fills the address List of source Ip Address
206 * @params[in] CmInetNetAddrLst *addrLstPtr, Address List pointer
207 * @params[in] F1IpAddr *srcIpAddr, src Ip Adrress to be filled in the Address List
209 * @return ROK - success
212 ******************************************************************/
214 uint8_t fillAddrLst(CmInetNetAddrLst *addrLstPtr, F1IpAddr *ipAddr)
217 addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
218 addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_UINT32(ipAddr->ipV4Addr);
223 /******************************************************************************
225 * @brief Fills the address List of the source Ip Address
229 * Function : fillDestNetAddr
232 * Fills the address List of destinatoion Ip Address
234 * @params[in] CmInetNetAddr *destAddrPtr, Address List pointer
235 * @params[in] F1IpAddr *dstIpAddr, destIp Address to be filled in the Address List
237 * @return ROK - success
240 *******************************************************************************/
241 uint8_t fillDestNetAddr(CmInetNetAddr *destAddrPtr, F1IpAddr *dstIpPtr)
243 /* Filling destination address */
244 destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
245 destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_UINT32(dstIpPtr->ipV4Addr);
249 /******************************************************************************
251 * @brief Establishes the Assoc Req for the received interface type
255 * Function : establishAssocReq
258 * Eastablishes the Assoc Req for the received interface type
260 * @params[in] DuSctpDestCb *paramPtr
262 * @return ROK - success
265 *******************************************************************************/
267 uint8_t establishReq(DuSctpDestCb *paramPtr)
271 socket_type = CM_INET_STREAM;
273 if((ret = cmInetSocket(socket_type, ¶mPtr->sockFd, IPPROTO_SCTP)) != ROK)
275 DU_LOG("\nERROR --> SCTP : Failed while opening a socket in ODU");
277 else if((ret = cmInetSctpBindx(¶mPtr->sockFd, &localAddrLst, paramPtr->srcPort)) != ROK)
279 DU_LOG("\nERROR --> SCTP: Failed during Binding in ODU");
281 else if((ret = sctpSetSockOpts(¶mPtr->sockFd)) != ROK)
283 DU_LOG("\nERROR --> SCTP : Failed to set Socket Opt in ODU");
289 DU_LOG("\nERROR --> SCTP : Failed while establishing Req at DU");
294 ret = cmInetSctpConnectx(¶mPtr->sockFd, ¶mPtr->destIpNetAddr, ¶mPtr->destAddrLst, paramPtr->destPort);
295 /* 115 error_code indicates that Operation is in progress and hence ignored if SctpConnect failed due to this */
302 if((ret == ROK) & (paramPtr->itfState == DU_SCTP_DOWN))
304 paramPtr->itfState = DU_SCTP_CONNECTING;
307 /* Post the EVTSTARTPOLL Msg */
311 duFillSctpPst(&pst, EVTSTARTPOLL);
317 /******************************************************************************
319 * @brief Processes the assoc Req for the received interface type
323 * Function : duSctpAssocReq
326 * Processes the Assoc Req for the received interface type
328 * @params[in] itfType interface Type,
329 * @params[in] cfm , represents the status of request
331 * @return ROK - success
334 *******************************************************************************/
336 uint8_t duSctpAssocReq(uint8_t itfType)
340 DuSctpDestCb *paramPtr = NULLP;
342 DU_ALLOC(paramPtr, sizeof(DuSctpDestCb));
343 if(paramPtr == NULLP)
345 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
352 paramPtr = &f1Params;
353 ret = establishReq(paramPtr);
358 paramPtr = &ricParams;
359 ret = establishReq(paramPtr);
364 DU_LOG("\nERROR --> SCTP : Invalid Interface Type %d", itfType);
370 DU_LOG("\nERROR --> SCTP : ASSOC Req Failed.");
371 cfm.status = LCM_PRIM_NOK;
372 cfm.reason = LCM_REASON_NOT_APPL;
376 cfm.status = LCM_PRIM_OK;
377 cfm.reason = LCM_REASON_NOT_APPL;
379 ret = duCheckReqStatus(&cfm);
383 /*******************************************************************
385 * @brief Fills Pst struct for ENTSCTP
389 * Function : duFillSctpPst
392 * Fills Pst struct for ENTSCTP
395 * @return ROK - success
398 * ****************************************************************/
399 uint8_t duFillSctpPst(Pst *pst, Event event)
402 if(ODU_GET_MSG_BUF(DFLT_REGION, DU_POOL, &mBuf) != ROK)
404 DU_LOG("\nERROR --> DU_APP : Failed to allocate memory");
407 memset(pst, 0, sizeof(Pst));
408 pst->srcEnt = (Ent)ENTDUAPP;
409 pst->srcInst = (Inst)DU_INST;
410 pst->srcProcId = DU_PROC;
411 pst->dstEnt = (Ent)ENTSCTP;
412 pst->dstInst = (Inst)SCTP_INST;
413 pst->dstProcId = pst->srcProcId;
415 pst->selector = ODU_SELECTOR_LC;
417 ODU_POST_TASK(pst, mBuf);
422 /*******************************************************************
424 * @brief Sets socket options as per requirement
428 * Function : sctpSetSockOpts
431 * Sets socket options as per requirement
433 * @params[in] sock_Fd determines the sockFd to be set
434 * @return ROK - success
437 * ****************************************************************/
438 uint8_t sctpSetSockOpts(CmInetFd *sock_Fd)
441 CmSctpEvent sctpEvent;
443 sctpEvent.dataIoEvent = TRUE;
444 sctpEvent.associationEvent = TRUE;
445 sctpEvent.addressEvent = TRUE;
446 sctpEvent.sendFailureEvent = TRUE;
447 sctpEvent.peerErrorEvent = TRUE;
448 sctpEvent.shutdownEvent = TRUE;
449 sctpEvent.partialDeliveryEvent = TRUE;
450 sctpEvent.adaptationLayerEvent = TRUE;
452 if((ret = cmInetSetOpt(sock_Fd, CM_SOCKOPT_LEVEL_SCTP, CM_SOCKOPT_OPT_SCTP_EVENTS, &sctpEvent) != ROK))
460 /*******************************************************************
462 * @brief Post received data/notification to DU APP
466 * Function : sendToDuApp
469 * Post received data/notification to DU APP
471 * @params[in] Message buffer
474 * @return ROK - success
477 * ****************************************************************/
478 void sendToDuApp(Buffer *mBuf, Event event)
481 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
482 ODU_PRINT_MSG(mBuf, 0, 0);
485 memset(&(pst), 0, sizeof(Pst));
486 pst.srcEnt = (Ent)ENTSCTP;
487 pst.srcInst = (Inst)SCTP_INST;
488 pst.srcProcId = DU_PROC;
489 pst.dstEnt = (Ent)ENTDUAPP;
490 pst.dstInst = (Inst)DU_INST;
491 pst.dstProcId = pst.srcProcId;
493 pst.selector = ODU_SELECTOR_LC;
495 pst.region = DFLT_REGION;
497 if (ODU_POST_TASK(&pst, mBuf) != ROK)
499 DU_LOG("\nERROR --> SCTP : ODU_POST_TASK failed in duReadCfg");
503 /*******************************************************************
505 * @brief Handles an SCTP notification message
508 * Function : sctpNtfyHdlr
511 * Handles an SCTP notification message
513 * @params[in] Notify message
515 * @return ROK - success
518 * ****************************************************************/
519 uint8_t sctpNtfyHdlr(CmInetSctpNotification *ntfy, uint8_t *itfState)
523 switch(ntfy->header.nType)
525 case CM_INET_SCTP_ASSOC_CHANGE :
526 DU_LOG("\nDEBUG --> SCTP : Assoc change notification received");
527 switch(ntfy->u.assocChange.state)
529 case CM_INET_SCTP_COMM_UP:
530 DU_LOG("INFO --> Event : COMMUNICATION UP");
531 *itfState = DU_SCTP_UP;
533 case CM_INET_SCTP_COMM_LOST:
534 DU_LOG("INFO --> Event : COMMUNICATION LOST");
535 *itfState = DU_SCTP_DOWN;
537 case CM_INET_SCTP_RESTART:
538 DU_LOG("INFO --> Event : SCTP RESTART");
539 *itfState = DU_SCTP_DOWN;
541 case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
542 DU_LOG("INFO --> Event : SHUTDOWN COMPLETE");
543 *itfState = DU_SCTP_DOWN;
545 case CM_INET_SCTP_CANT_STR_ASSOC:
546 DU_LOG("INFO --> Event : CANT START ASSOC");
547 *itfState = DU_SCTP_DOWN;
550 DU_LOG("\nERROR --> Invalid event %d", ntfy->u.assocChange.state);
554 case CM_INET_SCTP_PEER_ADDR_CHANGE :
555 DU_LOG("\nINFO --> SCTP : Peer Address Change notificarion received");
556 /* Need to add handler */
558 case CM_INET_SCTP_REMOTE_ERROR :
559 DU_LOG("\nINFO --> SCTP : Remote Error notification received");
561 case CM_INET_SCTP_SEND_FAILED :
562 DU_LOG("\nINFO --> SCTP : Send Failed notification received\n");
564 case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
565 DU_LOG("\nINFO --> SCTP : Shutdown Event notification received\n");
566 *itfState = DU_SCTP_DOWN;
569 case CM_INET_SCTP_ADAPTATION_INDICATION :
570 DU_LOG("\nINFO --> SCTP : Adaptation Indication received\n");
572 case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
573 DU_LOG("\nINFO --> SCTP : Partial Delivery Event received\n");
576 DU_LOG("\nERROR --> SCTP : Invalid sctp notification type %d", ntfy->header.nType);
580 /* Pack notification and send to APP */
581 DU_LOG("\nDEBUG --> SCTP : Forwarding received message to duApp");
583 memset(&(pst), 0, sizeof(Pst));
584 pst.srcEnt = (Ent)ENTSCTP;
585 pst.srcInst = (Inst)SCTP_INST;
586 pst.srcProcId = DU_PROC;
587 pst.dstEnt = (Ent)ENTDUAPP;
588 pst.dstInst = (Inst)DU_INST;
589 pst.dstProcId = pst.srcProcId;
590 pst.event = EVENT_SCTP_NTFY;
591 pst.selector = ODU_SELECTOR_LC;
593 pst.region = DU_APP_MEM_REGION;
595 if(cmPkSctpNtfy(&pst, ntfy) != ROK)
597 DU_LOG("\nERROR --> SCTP : Failed to pack SCTP notification");
603 /*******************************************************************
605 * @brief checks for valid readFd and process the InetSctpRecvMsg
610 * Function : processPolling
613 * checks for valid readFd and process the InetSctpRecvMsg
616 * @params[in] Params required for polling
617 * @params[in] SockFd for file descriptor
618 * @params[in] timeoutPtr indicates the timeout value
619 * @params[in] MemInfo, recvMsgSet
621 * @return ROK - success
624 * ****************************************************************/
626 uint8_t processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, uint32_t *timeoutPtr, CmInetMemInfo *memInfo, bool recvMsgSet)
629 CM_INET_FD_SET(sockFd, &pollParams->readFd);
630 ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, (int16_t *)&pollParams->numFd);
631 if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
633 CM_INET_FD_CLR(sockFd, &pollParams->readFd);
634 ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &(pollParams->mBuf), &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
638 DU_LOG("\nERROR --> SCTP: Failed to receive sctp msg for sockFd[%d]\n", sockFd->fd);
639 recvMsgSet = RFAILED;
643 if((((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0)) && ret == ROK)
645 if(pollParams->port == f1Params.destPort)
647 f1Params.assocId = pollParams->ntfy.u.assocChange.assocId;
648 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to F1Params from PollParams [%d]\n", f1Params.assocId);
649 ret = sctpNtfyHdlr(&pollParams->ntfy, &f1Params.itfState);
651 else if(pollParams->port == ricParams.destPort)
653 ricParams.assocId = pollParams->ntfy.u.assocChange.assocId;
654 DU_LOG("\nDEBUG --> SCTP : AssocId assigned to ricParams from PollParams [%d]\n", ricParams.assocId);
655 ret = sctpNtfyHdlr(&pollParams->ntfy, &ricParams.itfState);
659 DU_LOG("\nERROR --> SCTP : Failed to fill AssocId\n");
664 DU_LOG("\nERROR --> SCTP : Failed to process sctp notify msg\n");
667 else if(f1Params.itfState & (pollParams->port == f1Params.destPort))
669 sendToDuApp(pollParams->mBuf, EVENT_CU_DATA);
671 else if(ricParams.itfState & (pollParams->port == ricParams.destPort))
673 sendToDuApp(pollParams->mBuf, EVENT_RIC_DATA);
678 ODU_PUT_MSG_BUF(pollParams->mBuf);
684 /*******************************************************************
686 * @brief Receives message on the socket
690 * Function : sctpSockPoll
693 * Receives message on the socket
696 * @return ROK - success
699 * ****************************************************************/
700 uint8_t sctpSockPoll()
704 uint32_t *timeout_Ptr;
705 CmInetMemInfo memInfo;
706 sctpSockPollParams f1PollParams, e2PollParams;
708 memset(&f1PollParams, 0, sizeof(sctpSockPollParams));
709 memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
711 if (f1Params.sockFd.blocking & ricParams.sockFd.blocking)
720 timeout_Ptr = &timeout;
722 memInfo.region = DU_APP_MEM_REGION;
723 memInfo.pool = DU_POOL;
725 CM_INET_FD_ZERO(&f1PollParams.readFd);
726 CM_INET_FD_ZERO(&e2PollParams.readFd);
728 DU_LOG("\nINFO --> SCTP : Polling started at DU\n");
731 if(f1Params.itfState)
733 if((ret = processPolling(&f1PollParams, &f1Params.sockFd, timeout_Ptr, &memInfo, f1Params.recvMsgSet)) != ROK)
735 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for F1\n");
738 if(ricParams.itfState)
740 if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeout_Ptr, &memInfo, ricParams.recvMsgSet)) != ROK)
742 DU_LOG("\nERROR --> SCTP : Failed to RecvMsg for E2\n");
747 }/* End of sctpSockPoll() */
749 /*******************************************************************
751 * @brief Send message on SCTP socket
755 * Function : sctpSend
758 * Send message on SCTP socket
761 * @return ROK - success
764 * ****************************************************************/
765 uint8_t sctpSend(Buffer *mBuf, uint8_t itfType)
768 MsgLen len =0; /* number of actually sent octets */
769 CmInetMemInfo memInfo;
771 memInfo.region = DU_APP_MEM_REGION;
772 memInfo.pool = DU_POOL;
774 if(itfType == F1_INTERFACE)
776 DU_LOG("\nDEBUG --> SCTP : sending the message to DuApp");
777 ret = cmInetSctpSendMsg(&f1Params.sockFd, &f1Params.destIpNetAddr, f1Params.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
780 if(itfType == E2_INTERFACE)
782 DU_LOG("\nDEBUG --> SCTP : sending the message to ric");
783 ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
786 if(ret != ROK && ret != RWOULDBLOCK)
788 DU_LOG("\nERROR --> SCTP : Failed sending the message");
793 } /* End of sctpSend */
795 /**********************************************************************
797 **********************************************************************/