[Issue-ID : ODUHIGH-486] SCTP Port fix at F1 and E2 interface
[o-du/l2.git] / src / ric_stub / ric_stub_sctp.c
index f040b95..e8737f4 100644 (file)
 /* This file contains all SCTP related functionality */
 
 #include "common_def.h"
-#include "odu_common_codec.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
  *
@@ -45,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 = 0, assocIdx = 0;
 
+   sctpCb.sctpCfg = ricCb.ricCfgParams.sctpParams;   
+   fillAddrLst(&sctpCb.localAddrLst, &sctpCb.sctpCfg.localIpAddr);
+   memset(&sctpCb.e2LstnSockFd, -1, sizeof(CmInetFd));
+   for(destIdx=0; destIdx < sctpCb.sctpCfg.numDestNode; destIdx++)
+   {   
+      sctpCb.assocCb[assocIdx].destPort = sctpCb.sctpCfg.destCb[destIdx].destPort;
+      sctpCb.assocCb[assocIdx].bReadFdSet = ROK;
+      memset(&sctpCb.assocCb[assocIdx].sockFd, -1, sizeof(CmInetFd));
+      fillDestNetAddr(&sctpCb.assocCb[assocIdx].destIpNetAddr, &sctpCb.sctpCfg.destCb[destIdx].destIpAddr);
+      sctpCb.assocCb[assocIdx].connUp = false;
+      assocIdx++;
+   }   
+   sctpCb.numAssoc = assocIdx;
    return ROK;
 }
 
@@ -79,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_U32(ipAddr->ipV4Addr);
 
    return ROK;
 }
@@ -105,11 +103,11 @@ 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;
-   destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_U32(dstIpPtr->ipV4Addr);
+   destAddrPtr->u.ipv4NetAddr = CM_INET_NTOH_UINT32(dstIpPtr->ipV4Addr);
    return ROK;
 }
 
@@ -130,30 +128,53 @@ S16 fillDestNetAddr(CmInetNetAddr *destAddrPtr, SctpIpAddr *dstIpPtr)
  *
  *******************************************************************************/
 
-S16 sctpStartReq()
+uint8_t sctpStartReq()
 {
-   S16 ret = ROK;
+   uint8_t assocIdx  = 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("\nSCTP : Socket[%d] coudnt open for listening", ricParams.lstnSockFd.fd);
-   } 
-   else if((ret = cmInetSctpBindx(&ricParams.lstnSockFd, &ricParams.localAddrLst, ricParams.srcPort)) != ROK)
+   if(sctpCb.numAssoc)
    {
-      DU_LOG("\nSCTP: Binding failed at RIC");
-   }
-   else if((ret = sctpAccept(&ricParams.lstnSockFd, &ricParams.peerAddr, &ricParams.sockFd)) != ROK)
-   {
-      DU_LOG("\nSCTP: Unable to accept the connection at RIC");
+
+      if((ret = cmInetSocket(socket_type, &sctpCb.e2LstnSockFd, IPPROTO_SCTP) != ROK))
+      {
+         DU_LOG("\nERROR  -->  SCTP : Socket[%d] coudnt open for listening", sctpCb.e2LstnSockFd.fd);
+      } 
+      else if((ret = cmInetSctpBindx(&sctpCb.e2LstnSockFd, &sctpCb.localAddrLst, sctpCb.sctpCfg.e2SctpPort)) != ROK)
+      {
+         DU_LOG("\nERROR  -->  SCTP: Binding failed at RIC");
+      }
+      else if(ret = cmInetListen(&sctpCb.e2LstnSockFd, 1) != ROK)
+      {
+         DU_LOG("\nERROR  -->  SCTP: Unable to accept the connection at CU");
+         DU_LOG("\nERROR  -->  SCTP : Listening on socket failed");
+         cmInetClose(&sctpCb.e2LstnSockFd);
+         return RFAILED;
+      }
+      else
+      {
+         for(assocIdx=0; assocIdx < sctpCb.numAssoc; assocIdx++)
+         {
+            if((ret = sctpAccept(&sctpCb.assocCb[assocIdx])) != ROK)
+            {
+               DU_LOG("\nERROR  -->  SCTP: Unable to accept the connection at RIC");
+            }
+         }
+      }
    }
-   else if(sctpSockPoll() != ROK)
+
+   if(ret == ROK)
    {
-      DU_LOG("\nSCTP: Polling failed to start at RIC");
+      if(sctpSockPoll() != ROK)
+      {
+         DU_LOG("\nERROR  -->  SCTP: Polling failed to start at RIC");
+      }
    }
-   RETVALUE(ret);
+   return (ret);
 }
+
 /*******************************************************************
  *
  * @brief Sets socket options as per requirement
@@ -170,7 +191,7 @@ S16 sctpStartReq()
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpSetSockOpts(CmInetFd *sock_Fd)
+uint8_t sctpSetSockOpts(CmInetFd *sock_Fd)
 {
    S16 ret = ROK;
    CmSctpEvent sctpEvent;
@@ -189,7 +210,7 @@ S16 sctpSetSockOpts(CmInetFd *sock_Fd)
      ret = RFAILED;
    }
   
-   RETVALUE(ret);
+   return (ret);
 }
 
 /*******************************************************************
@@ -209,40 +230,32 @@ S16 sctpSetSockOpts(CmInetFd *sock_Fd)
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpAccept(CmInetFd *lstnSock_Fd, CmInetAddr *peerAddr, CmInetFd *sock_Fd)
+uint8_t sctpAccept(RicSctpAssocCb *assocCb)
 {
-   U8  ret;
+   uint8_t  ret;
 
-   ret = cmInetListen(lstnSock_Fd, 1);
-   if (ret != ROK)
-   {
-      DU_LOG("\nSCTP : Listening on socket failed");
-      cmInetClose(lstnSock_Fd);
-      return RFAILED;
-   }
-   
-   DU_LOG("\nSCTP : Connecting");
+   DU_LOG("\nINFO   -->  SCTP : Connecting");
 
-   while(!connUp)
+   while(!assocCb->connUp)
    {
-      ret = cmInetAccept(lstnSock_Fd, peerAddr, sock_Fd);
+      ret = cmInetAccept(&sctpCb.e2LstnSockFd, &assocCb->peerAddr, &assocCb->sockFd);
       if (ret == ROKDNA)
       {
          continue;
       }
       else if(ret != ROK)
       {
-         DU_LOG("\nSCTP : Failed to accept connection");
+         DU_LOG("\nERROR  -->  SCTP : Failed to accept connection");
          return RFAILED;
       }
       else
       {
-         connUp = TRUE;
-         sctpSetSockOpts(sock_Fd);
+         assocCb->connUp = TRUE;
+         sctpSetSockOpts(&assocCb->sockFd);
          break;
       }
    }
-   DU_LOG("\nSCTP : Connection established");
+   DU_LOG("\nINFO   -->  SCTP : Connection established");
 
    return ROK;
 }
@@ -264,62 +277,62 @@ S16 sctpAccept(CmInetFd *lstnSock_Fd, CmInetAddr *peerAddr, CmInetFd *sock_Fd)
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
+uint8_t sctpNtfyHdlr(RicSctpAssocCb *assocCb, CmInetSctpNotification *ntfy)
 {
    switch(ntfy->header.nType)
    {
       case CM_INET_SCTP_ASSOC_CHANGE :
-         DU_LOG("\nSCTP : Assoc change notification received");
+         DU_LOG("\nINFO   -->  SCTP : Assoc change notification received");
          switch(ntfy->u.assocChange.state)
          {
             case CM_INET_SCTP_COMM_UP:
-               DU_LOG("Event : COMMUNICATION UP");
-               connUp = TRUE;
+               DU_LOG("\nINFO   -->  Event : COMMUNICATION UP");
+               assocCb->connUp = TRUE;
                break;
             case CM_INET_SCTP_COMM_LOST:
-               DU_LOG("Event : COMMUNICATION LOST");
-               connUp = FALSE;
+               DU_LOG("\nINFO   -->  Event : COMMUNICATION LOST");
+               assocCb->connUp = FALSE;
                break;
             case CM_INET_SCTP_RESTART:
-               DU_LOG("Event : SCTP RESTART");
-               connUp = FALSE;
+               DU_LOG("\nINFO   -->  Event : SCTP RESTART");
+               assocCb->connUp = FALSE;
                break;
             case CM_INET_SCTP_SHUTDOWN_COMP: /* association gracefully shutdown */
-               DU_LOG("Event : SHUTDOWN COMPLETE");
-               connUp = FALSE;
+               DU_LOG("\nINFO   -->  Event : SHUTDOWN COMPLETE");
+               assocCb->connUp = FALSE;
                break;
             case CM_INET_SCTP_CANT_STR_ASSOC:
-               DU_LOG("Event : CANT START ASSOC");
-               connUp = FALSE;
+               DU_LOG("\nINFO   -->  Event : CANT START ASSOC");
+               assocCb->connUp = FALSE;
                break;
             default:
-               DU_LOG("\nInvalid event");
+               DU_LOG("\nERROR   -->  Invalid event");
                break;
          }
          break;
       case CM_INET_SCTP_PEER_ADDR_CHANGE :
-         DU_LOG("\nSCTP : Peer Address Change notificarion received");
+         DU_LOG("\nINFO   -->  SCTP : Peer Address Change notificarion received");
          /* Need to add handler */
          break;
       case CM_INET_SCTP_REMOTE_ERROR :
-         DU_LOG("\nSCTP : Remote Error notification received");
+         DU_LOG("\nINFO   -->  SCTP : Remote Error notification received");
          break;
       case CM_INET_SCTP_SEND_FAILED :
-         DU_LOG("\nSCTP : Send Failed notification received\n");
+         DU_LOG("\nINFO   -->  SCTP : Send Failed notification received\n");
          break;
       case CM_INET_SCTP_SHUTDOWN_EVENT : /* peer socket gracefully closed */
-         DU_LOG("\nSCTP : Shutdown Event notification received\n");
-         connUp = FALSE;
+         DU_LOG("\nINFO   -->  SCTP : Shutdown Event notification received\n");
+         assocCb->connUp = FALSE;
          exit(0);
          break;
       case CM_INET_SCTP_ADAPTATION_INDICATION :
-         DU_LOG("\nSCTP : Adaptation Indication received\n");
+         DU_LOG("\nINFO   -->  SCTP : Adaptation Indication received\n");
          break;
       case CM_INET_SCTP_PARTIAL_DELIVERY_EVENT:
-         DU_LOG("\nSCTP : Partial Delivery Event received\n");
+         DU_LOG("\nINFO   -->  SCTP : Partial Delivery Event received\n");
          break;
       default:
-         DU_LOG("\nSCTP : Invalid notification type\n");
+         DU_LOG("\nERROR   -->  SCTP : Invalid notification type\n");
          break;
    }
 
@@ -343,42 +356,38 @@ S16 sctpNtfyHdlr(CmInetSctpNotification *ntfy)
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpSockPoll()
+uint8_t sctpSockPoll()
 {
-   U16 ret = ROK;
-   U32           timeout;
-   U32           *timeoutPtr;
-   CmInetMemInfo memInfo;
+   uint8_t            assocIdx;
+   uint16_t           ret = ROK;
+   uint32_t           timeout;
+   uint32_t           *timeoutPtr;
+   CmInetMemInfo      memInfo;
    sctpSockPollParams e2PollParams;
 
    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;
 
    CM_INET_FD_ZERO(&e2PollParams.readFd);
 
-   DU_LOG("\nSCTP : Polling started at RIC\n");
+   DU_LOG("\nINFO  -->  SCTP : Polling started at RIC\n");
    while(1)
    {
-      if((ret = processPolling(&e2PollParams, &ricParams.sockFd, timeoutPtr, &memInfo)) != ROK)
+      /* Receiving SCTP data */
+      for(assocIdx = 0; assocIdx < sctpCb.numAssoc; assocIdx++)
       {
-         DU_LOG("\nSCTP : Failed to RecvMsg for E2 at RIC \n");
+         if((ret = processPolling(&e2PollParams, &sctpCb.assocCb[assocIdx], timeoutPtr, &memInfo)) != ROK)
+         {
+            DU_LOG("\nERROR  -->  SCTP : Failed to RecvMsg for E2 at RIC \n");
+         }
       }
    };
-   RETVALUE(ret);
+   return (ret);
 }/* End of sctpSockPoll() */
 
 /*******************************************************************
@@ -405,37 +414,39 @@ S16 sctpSockPoll()
  * ****************************************************************/
 
  
-S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, U32 *timeoutPtr, CmInetMemInfo *memInfo)
+uint8_t processPolling(sctpSockPollParams *pollParams, RicSctpAssocCb *assocCb, uint32_t *timeoutPtr, CmInetMemInfo *memInfo)
 {
-   U16 ret = ROK;
-   CM_INET_FD_SET(sockFd, &pollParams->readFd);
+   uint16_t ret = ROK;
+
+   CM_INET_FD_SET(&assocCb->sockFd, &pollParams->readFd);
    ret = cmInetSelect(&pollParams->readFd, NULLP, timeoutPtr, &pollParams->numFd);
-   if(CM_INET_FD_ISSET(sockFd, &pollParams->readFd))
+   if(CM_INET_FD_ISSET(&assocCb->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(&assocCb->sockFd, &pollParams->readFd);
+      ret = cmInetSctpRecvMsg(&assocCb->sockFd, &pollParams->addr, &pollParams->port, memInfo, &pollParams->mBuf, \
+          &pollParams->bufLen, &pollParams->info, &pollParams->flag, &pollParams->ntfy);
+      if(assocCb->connUp & (ret != ROK))
       {
-         ricParams.bReadFdSet = RFAILED;
+         assocCb->bReadFdSet = RFAILED;
       }
       else
       {
          if(((pollParams->flag & CM_INET_SCTP_MSG_NOTIFICATION) != 0) && (ret == ROK))
          {
-            ret = sctpNtfyHdlr(&pollParams->ntfy);
+            ret = sctpNtfyHdlr(assocCb, &pollParams->ntfy);
             if(ret != ROK)
             {
-               DU_LOG("\nSCTP : Failed to process sctp notify msg\n");
+               DU_LOG("\nERROR  -->  SCTP : Failed to process sctp notify msg\n");
             }
          }
-         else if(connUp & (pollParams->port == ricParams.destPort))
+         else if(assocCb->connUp)
          {  
-            E2APMsgHdlr(pollParams->mBuf);
-            SPutMsg(pollParams->mBuf);
+            E2APMsgHdlr(&assocCb->duId, pollParams->mBuf);
+            ODU_PUT_MSG_BUF(pollParams->mBuf);
          }
          else
          {
-            SPutMsg(pollParams->mBuf);
+            ODU_PUT_MSG_BUF(pollParams->mBuf);
          }
       } 
   }
@@ -458,23 +469,30 @@ S16 processPolling(sctpSockPollParams *pollParams, CmInetFd *sockFd, U32 *timeou
  *         RFAILED - failure
  *
  * ****************************************************************/
-S16 sctpSend(Buffer *mBuf)
+uint8_t sctpSend(uint32_t duId, Buffer *mBuf)
 {
-   U8               ret;
+   uint8_t          assocIdx;
+   uint8_t          ret;
    MsgLen           len;          /* number of actually sent octets */
    CmInetMemInfo    memInfo;                        
    
    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);
-
-   if(ret != ROK && ret != RWOULDBLOCK)
+   for(assocIdx = 0; assocIdx < sctpCb.numAssoc; assocIdx++)
    {
-      DU_LOG("\nSCTP : Send message failed");
-      return RFAILED;
-   }
+      if(sctpCb.assocCb[assocIdx].duId == duId)
+      {
+         ret = cmInetSctpSendMsg(&sctpCb.assocCb[assocIdx].sockFd, &sctpCb.assocCb[assocIdx].destIpNetAddr, \
+               sctpCb.assocCb[assocIdx].destPort, &memInfo, mBuf, &len, 0, FALSE, 0, 0/*SCT_PROTID_NONE*/, RWOULDBLOCK);
 
+         if(ret != ROK && ret != RWOULDBLOCK)
+         {
+            DU_LOG("\nERROR  -->  SCTP : Send message failed");
+            return RFAILED;
+         }
+      }
+   }
    return ROK;
 } /* End of sctpSend */
 /**********************************************************************