[Epic-ID: ODUHIGH-405][Task-ID: ODUHIGH-414] Support at CU and RIC stub for SCTP...
[o-du/l2.git] / src / ric_stub / ric_stub_sctp.c
index 679c43e..662087c 100644 (file)
 /* This file contains all SCTP related functionality */
 
 #include "common_def.h"
-#include "ric_e2ap_msg_hdl.h"
 #include "ric_stub_sctp.h"
 #include "ric_stub.h"
+#include "ric_e2ap_msg_hdl.h"
 #include "E2AP-PDU.h"
-#include "du_log.h"
 
-CuSctpDestCb ricParams;
-RicSctpParams sctpCfg;
-S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, SctpIpAddr *ipAddr);
-S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr);
 /**************************************************************************
  * @brief Function to configure the Sctp Params during config Request
  *
@@ -44,19 +39,23 @@ S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr);
  *
  ***************************************************************************/
 
-S16 sctpCfgReq()
+uint8_t sctpCfgReq()
 {
-   connUp = FALSE;
-   sctpCfg = ricCfgParams.sctpParams;
-
-/* Fill F1 Params */
-   ricParams.destPort             = sctpCfg.duPort;
-   ricParams.srcPort              = sctpCfg.ricPort;
-   ricParams.bReadFdSet           = ROK;
-   memset(&ricParams.sockFd, -1, sizeof(CmInetFd));
-   memset(&ricParams.lstnSockFd, -1, sizeof(CmInetFd));
-   fillDestNetAddr(&ricParams.destIpNetAddr, &sctpCfg.duIpAddr);
+   int destIdx;
 
+   sctpCb.sctpCfg = ricCfgParams.sctpParams;   
+   for(destIdx=0; destIdx < sctpCb.sctpCfg.numDu; destIdx++)
+   {   
+      sctpCb.destCb[destIdx].destPort = sctpCb.sctpCfg.sctpAssoc[destIdx].duPort;
+      sctpCb.destCb[destIdx].srcPort  = sctpCb.sctpCfg.sctpAssoc[destIdx].ricPort;
+      sctpCb.destCb[destIdx].bReadFdSet = ROK;
+      memset(&sctpCb.destCb[destIdx].sockFd, -1, sizeof(CmInetFd));
+      memset(&sctpCb.destCb[destIdx].lstnSockFd, -1, sizeof(CmInetFd));
+      fillDestNetAddr(&sctpCb.destCb[destIdx].destIpNetAddr, &sctpCb.sctpCfg.sctpAssoc[destIdx].duIpAddr);
+      fillAddrLst(&sctpCb.destCb[destIdx].localAddrLst, &sctpCb.sctpCfg.sctpAssoc[destIdx].ricIpAddr);
+      sctpCb.destCb[destIdx].connUp = false;
+   }   
+   sctpCb.numDu = sctpCb.sctpCfg.numDu;
    return ROK;
 }
 
@@ -78,11 +77,11 @@ S16 sctpCfgReq()
  *
  ******************************************************************/
 
-S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, SctpIpAddr *ipAddr)
+uint8_t fillAddrLst(CmInetNetAddrLst *addrLstPtr, SctpIpAddr *ipAddr)
 { 
+   addrLstPtr->addrs[addrLstPtr->count].type = CM_INET_IPV4ADDR_TYPE;
+   addrLstPtr->addrs[addrLstPtr->count].u.ipv4NetAddr = CM_INET_NTOH_UINT32(ipAddr->ipV4Addr);
    addrLstPtr->count++;
-   addrLstPtr->addrs[(addrLstPtr->count - 1)].type = CM_INET_IPV4ADDR_TYPE;
-   addrLstPtr->addrs[(addrLstPtr->count - 1)].u.ipv4NetAddr = CM_INET_NTOH_UINT32(ipAddr->ipV4Addr);
 
    return ROK;
 }
@@ -104,7 +103,7 @@ S16 fillAddrLst(CmInetNetAddrLst *addrLstPtr, SctpIpAddr *ipAddr)
  *         RFAILED - failure
  *
  *******************************************************************************/
-S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr)
+uint8_t fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr)
 {
    /* Filling destination address */
    destAddrPtr->type = CM_INET_IPV4ADDR_TYPE;
@@ -129,25 +128,31 @@ S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr)
  *
  *******************************************************************************/
 
-S16 sctpStartReq()
+uint8_t sctpStartReq()
 {
-   S16 ret = ROK;
+   uint8_t destIdx  = 0;
+   uint8_t ret = ROK;
+
    socket_type = CM_INET_STREAM;
-   fillAddrLst(&ricParams.localAddrLst, &sctpCfg.ricIpAddr);
 
-   if((ret = cmInetSocket(socket_type, &ricParams.lstnSockFd, IPPROTO_SCTP) != ROK))
-   {
-      DU_LOG("\nERROR  -->  SCTP : Socket[%d] coudnt open for listening", ricParams.lstnSockFd.fd);
-   } 
-   else if((ret = cmInetSctpBindx(&ricParams.lstnSockFd, &ricParams.localAddrLst, ricParams.srcPort)) != ROK)
+   for(destIdx=0; destIdx < sctpCb.numDu; destIdx++)
    {
-      DU_LOG("\nERROR  -->  SCTP: Binding failed at RIC");
-   }
-   else if((ret = sctpAccept(&ricParams.lstnSockFd, &ricParams.peerAddr, &ricParams.sockFd)) != ROK)
-   {
-      DU_LOG("\nERROR  -->  SCTP: Unable to accept the connection at RIC");
+      if((ret = cmInetSocket(socket_type, &sctpCb.destCb[destIdx].lstnSockFd, IPPROTO_SCTP) != ROK))
+      {
+         DU_LOG("\nERROR  -->  SCTP : Socket[%d] coudnt open for listening", sctpCb.destCb[destIdx].lstnSockFd.fd);
+      } 
+      else if((ret = cmInetSctpBindx(&sctpCb.destCb[destIdx].lstnSockFd, &sctpCb.destCb[destIdx].localAddrLst, \
+         sctpCb.destCb[destIdx].srcPort)) != ROK)
+      {
+         DU_LOG("\nERROR  -->  SCTP: Binding failed at RIC");
+      }
+      else if((ret = sctpAccept(&sctpCb.destCb[destIdx])) != ROK)
+      {
+         DU_LOG("\nERROR  -->  SCTP: Unable to accept the connection at RIC");
+      }
    }
-   else if(sctpSockPoll() != ROK)
+
+   if(sctpSockPoll() != ROK)
    {
       DU_LOG("\nERROR  -->  SCTP: Polling failed to start at RIC");
    }
@@ -169,7 +174,7 @@ S16 sctpStartReq()
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpSetSockOpts(CmInetFd *sock_Fd)
+uint8_t sctpSetSockOpts(CmInetFd *sock_Fd)
 {
    S16 ret = ROK;
    CmSctpEvent sctpEvent;
@@ -208,23 +213,23 @@ S16 sctpSetSockOpts(CmInetFd *sock_Fd)
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpAccept(CmInetFd *lstnSock_Fd, CmInetAddr *peerAddr, CmInetFd *sock_Fd)
+uint8_t sctpAccept(RicSctpDestCb *destCb)
 {
    uint8_t  ret;
 
-   ret = cmInetListen(lstnSock_Fd, 1);
+   ret = cmInetListen(&destCb->lstnSockFd, 1);
    if (ret != ROK)
    {
       DU_LOG("\nERROR   -->  SCTP : Listening on socket failed");
-      cmInetClose(lstnSock_Fd);
+      cmInetClose(&destCb->lstnSockFd);
       return RFAILED;
    }
    
    DU_LOG("\nINFO   -->  SCTP : Connecting");
 
-   while(!connUp)
+   while(!destCb->connUp)
    {
-      ret = cmInetAccept(lstnSock_Fd, peerAddr, sock_Fd);
+      ret = cmInetAccept(&destCb->lstnSockFd, &destCb->peerAddr, &destCb->sockFd);
       if (ret == ROKDNA)
       {
          continue;
@@ -236,8 +241,8 @@ S16 sctpAccept(CmInetFd *lstnSock_Fd, CmInetAddr *peerAddr, CmInetFd *sock_Fd)
       }
       else
       {
-         connUp = TRUE;
-         sctpSetSockOpts(sock_Fd);
+         destCb->connUp = TRUE;
+         sctpSetSockOpts(&destCb->sockFd);
          break;
       }
    }
@@ -263,7 +268,7 @@ S16 sctpAccept(CmInetFd *lstnSock_Fd, CmInetAddr *peerAddr, CmInetFd *sock_Fd)
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
+uint8_t sctpNtfyHdlr(RicSctpDestCb *destCb, CmInetSctpNotification *ntfy)
 {
    switch(ntfy->header.nType)
    {
@@ -273,23 +278,23 @@ S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
          {
             case CM_INET_SCTP_COMM_UP:
                DU_LOG("\nINFO   -->  Event : COMMUNICATION UP");
-               connUp = TRUE;
+               destCb->connUp = TRUE;
                break;
             case CM_INET_SCTP_COMM_LOST:
                DU_LOG("\nINFO   -->  Event : COMMUNICATION LOST");
-               connUp = FALSE;
+               destCb->connUp = FALSE;
                break;
             case CM_INET_SCTP_RESTART:
                DU_LOG("\nINFO   -->  Event : SCTP RESTART");
-               connUp = FALSE;
+               destCb->connUp = FALSE;
                break;
             case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
                DU_LOG("\nINFO   -->  Event : SHUTDOWN COMPLETE");
-               connUp = FALSE;
+               destCb->connUp = FALSE;
                break;
             case CM_INET_SCTP_CANT_STR_ASSOC:
                DU_LOG("\nINFO   -->  Event : CANT START ASSOC");
-               connUp = FALSE;
+               destCb->connUp = FALSE;
                break;
             default:
                DU_LOG("\nERROR   -->  Invalid event");
@@ -308,7 +313,7 @@ S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
          break;
       case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
          DU_LOG("\nINFO   -->  SCTP : Shutdown Event notification received\n");
-         connUp = FALSE;
+         destCb->connUp = FALSE;
          exit(0);
          break;
       case CM_INET_SCTP_ADAPTATION_INDICATION :
@@ -342,8 +347,9 @@ S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpSockPoll()
+uint8_t sctpSockPoll()
 {
+   uint8_t            destIdx;
    uint16_t           ret = ROK;
    uint32_t           timeout;
    uint32_t           *timeoutPtr;
@@ -352,18 +358,9 @@ S16 sctpSockPoll()
 
    memset(&e2PollParams, 0, sizeof(sctpSockPollParams));
     
-   if(ricParams.sockFd.blocking)
-   {
-      /* blocking */
-      timeoutPtr = NULLP;
-   }
-   else
-   {
-      /* non-blocking */
-      timeout = 0;
-      timeoutPtr = &timeout;
-   }
-
+   /* All sockets are non-blocking */
+   timeout = 0;
+   timeoutPtr = &timeout;
    memInfo.region = RIC_APP_MEM_REG;
    memInfo.pool   = RIC_POOL;
 
@@ -372,9 +369,14 @@ S16 sctpSockPoll()
    DU_LOG("\nINFO  -->  SCTP : Polling started at RIC\n");
    while(1)
    {
-      if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeoutPtr, &memInfo)) != ROK)
+      /* Receiving SCTP data */
+      for(destIdx = 0; destIdx < sctpCb.numDu; destIdx++)
       {
-         DU_LOG("\nERROR  -->  SCTP : Failed to RecvMsg for E2 at RIC \n");
+
+         if((ret = processPolling(&e2PollParams, &sctpCb.destCb[destIdx], timeoutPtr, &memInfo)) != ROK)
+         {
+            DU_LOG("\nERROR  -->  SCTP : Failed to RecvMsg for E2 at RIC \n");
+         }
       }
    };
    return (ret);
@@ -404,30 +406,32 @@ S16 sctpSockPoll()
  * ****************************************************************/
 
  
-S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, uint32_t *timeoutPtr, CmInetMemInfo *memInfo)
+uint8_t processPolling(sctpSockPollParams *pollParams, RicSctpDestCb *destCb, uint32_t *timeoutPtr, CmInetMemInfo *memInfo)
 {
    uint16_t ret = ROK;
-   CM_INET_FD_SET(sockFd, &pollParams->readFd);
+
+   CM_INET_FD_SET(&destCb->sockFd, &pollParams->readFd);
    ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, &pollParams->numFd);
-   if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
+   if(CM_INET_FD_ISSET(&destCb->sockFd, &pollParams->readFd))
    {
-      CM_INET_FD_CLR(sockFd, &pollParams->readFd);
-      ret = cmInetSctpRecvMsg(sockFd, &pollParams->addr, &pollParams->port, memInfo, &pollParams->mBuf, &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
-      if(connUp & (ret != ROK))
+      CM_INET_FD_CLR(&destCb->sockFd, &pollParams->readFd);
+      ret = cmInetSctpRecvMsg(&destCb->sockFd, &pollParams->addr, &pollParams->port, memInfo, &pollParams->mBuf, \
+          &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
+      if(destCb->connUp & (ret != ROK))
       {
-         ricParams.bReadFdSet = RFAILED;
+         destCb->bReadFdSet = RFAILED;
       }
       else
       {
          if(((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0) && (ret == ROK))
          {
-            ret = sctpNtfyHdlr(&pollParams->ntfy);
+            ret = sctpNtfyHdlr(destCb, &pollParams->ntfy);
             if(ret != ROK)
             {
                DU_LOG("\nERROR  -->  SCTP : Failed to process sctp notify msg\n");
             }
          }
-         else if(connUp & (pollParams->port == ricParams.destPort))
+         else if(destCb->connUp & (pollParams->port == destCb->destPort))
          {  
             E2APMsgHdlr(pollParams->mBuf);
             ODU_PUT_MSG_BUF(pollParams->mBuf);
@@ -457,7 +461,7 @@ S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, uint32_t *t
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpSend(Buffer *mBuf)
+uint8_t sctpSend(Buffer *mBuf)
 {
    uint8_t          ret;
    MsgLen           len;          /* number of actually sent octets */
@@ -466,7 +470,8 @@ S16 sctpSend(Buffer *mBuf)
    memInfo.region = RIC_APP_MEM_REG;               
    memInfo.pool   = RIC_POOL;
 
-   ret = cmInetSctpSendMsg(&ricParams.sockFd, &ricParams.destIpNetAddr, ricParams.destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
+   ret = cmInetSctpSendMsg(&sctpCb.destCb[0].sockFd, &sctpCb.destCb[0].destIpNetAddr, sctpCb.destCb[0].destPort, &memInfo, \
+      mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
 
    if(ret != ROK && ret != RWOULDBLOCK)
    {