Fix for memory leak in UL data path at cu_stub [Issue-ID: ODUHIGH-398]
[o-du/l2.git] / src / cu_stub / cu_stub_egtp.c
index 114154b..cb3c5c8 100644 (file)
 /* This file contains all EGTP related functionality */
 
 #include "common_def.h"
+#include "cu_f1ap_msg_hdl.h"
 #include "cu_stub_egtp.h"
 #include "du_log.h"
 
 /* Global variable declaration */
 EgtpGlobalCb egtpCb;
-
 /**************************************************************************
  * @brief Task Initiation callback function. 
  *
@@ -47,8 +47,11 @@ EgtpGlobalCb egtpCb;
  ***************************************************************************/
 S16 egtpActvInit()
 {
-  DU_LOG("\n\nEGTP : Initializing");
+  DU_LOG("\n\nDEBUG  -->  EGTP : Initializing");
   memset (&egtpCb, 0, sizeof(EgtpGlobalCb));
+  
+  //Initializing with INVALID value
+  memset(egtpCb.gCntPdu, 0xFF , sizeof(uint8_t)*(MAX_TEID+1)); 
   protType = CM_INET_PROTO_UDP;
   return ROK;
 }
@@ -76,32 +79,21 @@ S16 egtpActvInit()
 uint8_t egtpInitReq()
 {
    uint8_t ret = ROK;
-   EgtpTnlEvt tnlEvt;
 
    ret = cuEgtpCfgReq();
    if(ret != ROK)
    {
-      DU_LOG("\nEGTP : Configuration failed");
+      DU_LOG("\nERROR  -->  EGTP : Configuration failed");
       return (ret);
    }
 
    ret = cuEgtpSrvOpenReq();
    if(ret != ROK)
    {
-       DU_LOG("\nEGTP : Transport server open request failed");
+       DU_LOG("\nERROR  -->  EGTP : Transport server open request failed");
        return (ret);
    }
 
-   tnlEvt.action = EGTP_TNL_MGMT_ADD;
-   tnlEvt.lclTeid = 10;
-   tnlEvt.remTeid = 1;
-   ret = cuEgtpTnlMgmtReq(tnlEvt);
-   if(ret != ROK)
-   {
-      DU_LOG("\n EGTP : Tunnel management request failed");
-      return RFAILED;
-   }
-
    return ret;
 } /* egtpInitReq */
 
@@ -138,12 +130,12 @@ S16 cuEgtpCfgReq()
 
    if(ret != ROK)
    {
-      DU_LOG("\nEGTP : TeId hash list initialization failed");
+      DU_LOG("\nERROR  -->  EGTP : TeId hash list initialization failed");
       return RFAILED;
    }
    else
    {
-      DU_LOG("\nEGTP : Configuration successful");
+      DU_LOG("\nINFO  -->  EGTP : Configuration successful");
    }
 
    return ROK;
@@ -171,38 +163,39 @@ S16 cuEgtpSrvOpenReq(Pst *pst)
 
    uint8_t ret;
 
-   DU_LOG("\nEGTP : Received open server request");
+   DU_LOG("\nINFO  -->  EGTP : Received open server request");
  
    sockType = CM_INET_DGRAM;
    if((ret = (cmInetSocket(sockType, &(egtpCb.recvTptSrvr.sockFd), protType))) != ROK)
    {
-      DU_LOG("\nEGTP : Failed to open UDP socket");
+      DU_LOG("\nERROR  -->  EGTP : Failed to open UDP socket");
       return RFAILED;
    }
 
    ret = cmInetBind(&(egtpCb.recvTptSrvr.sockFd), &(egtpCb.recvTptSrvr.addr));
    if(ret != ROK)
    {
-      DU_LOG("\nEGTP : Failed to bind socket");
+      DU_LOG("\nERROR  -->  EGTP : Failed to bind socket");
       return RFAILED;
    }
 
    if(ret = (cmInetSocket(sockType, &(egtpCb.dstCb.sendTptSrvr.sockFd), protType)) != ROK)
    {  
-      DU_LOG("\nEGTP : Failed to open UDP socket");
+      DU_LOG("\nERROR  -->  EGTP : Failed to open UDP socket");
       return RFAILED;
    }
       
    ret = cmInetBind(&(egtpCb.dstCb.sendTptSrvr.sockFd), &(egtpCb.dstCb.sendTptSrvr.addr));
    if(ret != ROK)
    {  
-      DU_LOG("\nEGTP : Failed to bind socket");
+      DU_LOG("\nERROR  -->  EGTP : Failed to bind socket");
       return RFAILED;
    }
        
    /* TODO: set socket options */
 
-   DU_LOG("\nEGTP : Receiver socket[%d] and Sender socket[%d] open", egtpCb.recvTptSrvr.sockFd.fd, egtpCb.dstCb.sendTptSrvr.sockFd.fd);
+   DU_LOG("\nINFO  -->  EGTP : Receiver socket[%d] and Sender socket[%d] open", egtpCb.recvTptSrvr.sockFd.fd,\
+   egtpCb.dstCb.sendTptSrvr.sockFd.fd);
    return ROK;
 } /* cuEgtpSrvOpenReq */
 
@@ -228,7 +221,7 @@ S16 cuEgtpTnlMgmtReq(EgtpTnlEvt tnlEvt)
 {
    S8 ret;
 
-   DU_LOG("\nEGTP : Received tunnel management request");
+   DU_LOG("\nINFO  -->  EGTP : Received tunnel management request");
    switch(tnlEvt.action)
    {
       case EGTP_TNL_MGMT_ADD:
@@ -248,7 +241,7 @@ S16 cuEgtpTnlMgmtReq(EgtpTnlEvt tnlEvt)
       }
       default:
       {
-         DU_LOG("\nEGTP : Invalid tunnel management action[%d]", tnlEvt.action);
+         DU_LOG("\nERROR  -->  EGTP : Invalid tunnel management action[%d]", tnlEvt.action);
          ret = RFAILED;
       }
    }
@@ -278,13 +271,13 @@ S16 cuEgtpTnlAdd(EgtpTnlEvt tnlEvt)
    EgtpMsgHdr   preDefHdr; /* pre-define header for this tunnel */
    
 
-   DU_LOG("\nEGTP : Tunnel addition : LocalTeid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
+   DU_LOG("\nINFO  -->  EGTP : Tunnel addition : LocalTeid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
 
    CU_ALLOC(teidCb, (Size)sizeof(EgtpTeIdCb));
 
    if(teidCb == NULLP)
    {
-      DU_LOG("\nEGTP : Memory allocation failed");
+      DU_LOG("\nERROR  -->  EGTP : Memory allocation failed");
       return RFAILED;
    }
 
@@ -296,7 +289,7 @@ S16 cuEgtpTnlAdd(EgtpTnlEvt tnlEvt)
    ret = cmHashListInsert(&(egtpCb.dstCb.teIdLst), (PTR)teidCb, (uint8_t *)&(teidCb->teId), sizeof(uint32_t));
    if(ret != ROK)
    {
-      DU_LOG("\nEGTP : Failed to insert in hash list");
+      DU_LOG("\nERROR  -->  EGTP : Failed to insert in hash list");
       CU_FREE(teidCb, (Size)sizeof(EgtpTeIdCb));
       return RFAILED;
    }
@@ -308,9 +301,11 @@ S16 cuEgtpTnlAdd(EgtpTnlEvt tnlEvt)
    preDefHdr.teId = teidCb->remTeId;
    preDefHdr.extHdr.pdcpNmb.pres = FALSE;
    preDefHdr.extHdr.udpPort.pres = FALSE;
-   preDefHdr.nPdu.pres = FALSE;
-   
+   preDefHdr.nPdu.pres = TRUE; //Including nPdu when sending data
+   preDefHdr.nPdu.val = 0;
+
    cuEgtpEncodeHdr((uint8_t *)teidCb->preEncodedHdr.hdr, &preDefHdr, &(teidCb->preEncodedHdr.cnt));
+   egtpCb.gCntPdu[teidCb->remTeId] = 0;//Resetting the Cnt Value for this DRB which indicates its creation
 
 /*   SPutSBuf(CU_APP_MEM_REG, CU_POOL, (Data *)teidCb, (Size)sizeof(EgtpTeIdCb));*/
 
@@ -338,12 +333,12 @@ S16 cuEgtpTnlMod(EgtpTnlEvt tnlEvt)
    S16   ret;
    EgtpTeIdCb     *teidCb = NULLP;
 
-   printf("\nTunnel modification : LocalTeid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
+   DU_LOG("\nDEBUG  -->  CU_STUB : Tunnel modification : LocalTeid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
 
    cmHashListFind(&(egtpCb.dstCb.teIdLst), (uint8_t *)&(tnlEvt.teId), sizeof(uint32_t), 0, (PTR *)&teidCb);
    if(teidCb == NULLP)
    {
-      printf("\nTunnel id not found");
+      DU_LOG("\nDEBUG --> CU_STUBTunnel id not found");
       return RFAILED;
    }  
    
@@ -372,12 +367,12 @@ S16 cuEgtpTnlDel(EgtpTnlEvt tnlEvt)
 {
    EgtpTeIdCb     *teidCb = NULLP;
 
-   DU_LOG("\nEGTP : Tunnel deletion : Local Teid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
+   DU_LOG("\nDEBUG  -->  EGTP : Tunnel deletion : Local Teid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
    
    cmHashListFind(&(egtpCb.dstCb.teIdLst), (uint8_t *)&(tnlEvt.lclTeid), sizeof(uint32_t), 0, (PTR *)&teidCb);
    if(teidCb == NULLP)
    {
-      DU_LOG("\nEGTP : Tunnel id[%d] not configured", tnlEvt.lclTeid);
+      DU_LOG("\nERROR  -->  EGTP : Tunnel id[%d] not configured", tnlEvt.lclTeid);
       return RFAILED;
    } 
 
@@ -502,8 +497,9 @@ S16 cuEgtpEncodeHdr(uint8_t *preEncodedHdr, EgtpMsgHdr *preDefHdr, uint8_t *hdrI
 S16 cuEgtpHdlRecvMsg(Buffer *mBuf)
 {
    /*Decoding of EGTP message header */
+   
    cuEgtpDecodeHdr(mBuf);
-
+   
    /* Start Pumping data from CU to DU */
    //return (cuEgtpDatReq());
 
@@ -617,32 +613,43 @@ S16 cuEgtpDecodeHdr(Buffer *mBuf)
       ODU_REM_PRE_MSG(&extHdrType, mBuf);
    }
 
-   DU_LOG("\nEGTP : Message Buffer after decoding header ");
+   DU_LOG("\nDEBUG  -->  EGTP : Message Buffer after decoding header [TEID:%d]",egtpMsg.msgHdr.teId);
    ODU_PRINT_MSG(mBuf, 0, 0);
 
+   SPutMsg(mBuf);
+
    return ROK;
      
 } /* End of cuEgtpDecodeHdr */
 
-S16 cuEgtpDatReq()
+uint16_t cuEgtpDatReq(uint8_t teId)
 {
-   uint16_t cnt = 0;
+   uint8_t ret = ROK, cnt = 0;
    EgtpMsg  egtpMsg;
 
+   egtpMsg.msgHdr.teId = teId;
+   
+   if(egtpCb.gCntPdu[teId] == 0xFF) //DRB not created
+   {
+      DU_LOG("\nERROR  -->  EGTP : DRB  not created");
+      return RFAILED ;
+   }
    /* Build Application message that is supposed to come from app to egtp */
-   BuildAppMsg(&egtpMsg);
+   ret = BuildAppMsg(&egtpMsg);
+   if(ret != ROK)
+   {
+      DU_LOG("\nERROR  -->  EGTP : Failed to build App Msg");
+      return RFAILED;
+   }
 
    /* Encode EGTP header to build final EGTP message */
-   BuildEgtpMsg(&egtpMsg);
-
-   /* Send Message to peer */
-   while(cnt < 200)
+   ret = BuildEgtpMsg(&egtpMsg);
+   if(ret != ROK)
    {
-      DU_LOG("\nEGTP : Sending message[%d]", cnt+1);
-      cuEgtpSendMsg(egtpMsg.msg);
-      cnt++;
+      DU_LOG("\nERROR  -->  EGTP : Failed to build EGTP Msg");
+      return RFAILED;
    }
-
+   cuEgtpSendMsg(egtpMsg.msg);
    ODU_PUT_MSG_BUF(egtpMsg.msg);
 
    return ROK;
@@ -651,8 +658,19 @@ S16 cuEgtpDatReq()
 
 S16 BuildAppMsg(EgtpMsg  *egtpMsg)
 {
-   char data[30] = "This is EGTP data from CU";
-   int datSize = 30;
+   char data[1215] = "In telecommunications, 5G is the fifth generation technology standard for broadband cellular"
+   " networks, which cellular phone companies began deploying worldwide in 2019, and is the planned successor to the 4G "
+   " networks which provide connectivity to most current cellphones. 5G networks are predicted to have more than 1.7"
+   " billion subscribers worldwide by 2025, according to the GSM Association.Like its predecessors, 5G networks are"
+   " cellular networks,in which the service area is divided into small geographical areas called cells.All 5G wireless"
+   " devices in a cell are connected to the Internet and telephone network by radio waves through local antenna in the"
+   " cell. The main advantage of the new networks is that they will have greater bandwidth, giving higher download"
+   " speeds, eventually up to 10 gigabits per second(Gbit/s). Due to the increased bandwidth, it is expected the"
+   " networks will not exclusively serve cellphones like existing cellular networks, but also be used as general"
+   " internet service providers for laptops and desktop computers, competing with existing ISPs such as cable"
+   " internet, and also will make possible new applications in internet of things (IoT) and machine to machine areas.";
+
+   int datSize = 1215;
  
    Buffer   *mBuf;
  
@@ -660,14 +678,14 @@ S16 BuildAppMsg(EgtpMsg  *egtpMsg)
    {
       if(ODU_ADD_POST_MSG_MULT((Data *)data, datSize, mBuf) != ROK)
       {
-         DU_LOG("\nEGTP : ODU_ADD_POST_MSG_MULT failed");
+         DU_LOG("\nERROR  -->  EGTP : ODU_ADD_POST_MSG_MULT failed");
          ODU_PUT_MSG_BUF(mBuf);
          return RFAILED;
       }
    }
    else
    {
-       DU_LOG("\nEGTP : Failed to allocate memory");
+       DU_LOG("\nERROR  -->  EGTP : Failed to allocate memory");
        return RFAILED;
    }
  
@@ -741,17 +759,22 @@ S16 BuildAppMsg(EgtpMsg  *egtpMsg)
    ret = ODU_ADD_PRE_MSG_MULT(revPkArray, (MsgLen)cnt, mBuf);
  
    egtpMsg->msgHdr.msgType = EGTPU_MSG_GPDU;
-   egtpMsg->msgHdr.nPdu.pres = FALSE;
+   egtpMsg->msgHdr.nPdu.pres = TRUE;
+
+   if(egtpCb.gCntPdu[egtpMsg->msgHdr.teId] != NUM_DL_PACKETS)
+     egtpCb.gCntPdu[egtpMsg->msgHdr.teId]++;
+   else
+     egtpCb.gCntPdu[egtpMsg->msgHdr.teId] = 1;
+
+   egtpMsg->msgHdr.nPdu.val = egtpCb.gCntPdu[egtpMsg->msgHdr.teId];
    egtpMsg->msgHdr.seqNum.pres = FALSE;
    egtpMsg->msgHdr.extHdr.udpPort.pres = FALSE;
    egtpMsg->msgHdr.extHdr.pdcpNmb.pres = FALSE;
-   egtpMsg->msgHdr.teId = 10;
    egtpMsg->msg = mBuf;
 
    return ret;
 }
 
-
 S16 BuildEgtpMsg(EgtpMsg *egtpMsg)
 {
    EgtpTeIdCb   *teidCb = NULLP;
@@ -763,7 +786,7 @@ S16 BuildEgtpMsg(EgtpMsg *egtpMsg)
    cmHashListFind(&(egtpCb.dstCb.teIdLst), (uint8_t *)&(egtpMsg->msgHdr.teId), sizeof(uint32_t), 0, (PTR *)&teidCb);
    if(teidCb == NULLP)
    {
-      DU_LOG("\nEGTP : Tunnel id[%d] not configured", egtpMsg->msgHdr.teId);
+      DU_LOG("\nERROR  -->  EGTP : Tunnel id[%d] not configured", egtpMsg->msgHdr.teId);
       return (LCM_REASON_INVALID_PAR_VAL);
    }
 
@@ -808,11 +831,18 @@ S16 BuildEgtpMsg(EgtpMsg *egtpMsg)
       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] &= ~(EGTP_MASK_BIT2);
    }
 
-   ODU_ADD_PRE_MSG_MULT(&teidCb->preEncodedHdr.hdr[hdrLen], (EGTP_MAX_HDR_LEN - hdrLen), egtpMsg->msg);
-
-   DU_LOG("\nEGTP : Sending message buffer");
-   ODU_PRINT_MSG(egtpMsg->msg, 0, 0);
+   /*Update the nPdU number*/
+   if(egtpMsg->msgHdr.nPdu.pres)
+   {
+      teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] |= (EGTP_MASK_BIT1);
+      teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 11] = egtpMsg->msgHdr.nPdu.val;
+   }
+   else
+   {
+      teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] &= ~(EGTP_MASK_BIT1);
+   }
 
+   ODU_ADD_PRE_MSG_MULT(&teidCb->preEncodedHdr.hdr[hdrLen], (EGTP_MAX_HDR_LEN - hdrLen), egtpMsg->msg);
    return ROK;
 }
 
@@ -832,11 +862,11 @@ S16 cuEgtpSendMsg(Buffer *mBuf)
    ret = cmInetSendMsg(&(egtpCb.dstCb.sendTptSrvr.sockFd), &dstAddr, &info, mBuf, &txLen, CM_INET_NO_FLAG);
    if(ret != ROK && ret != RWOULDBLOCK)
    {
-      DU_LOG("\nEGTP : Message send failure");
+      DU_LOG("\nERROR  -->  EGTP : Message send failure");
       return RFAILED;
    }
    
-   DU_LOG("\nEGTP : Message Sent");
+   DU_LOG("\nDEBUG  -->  EGTP : Message Sent");
  
    return ROK;
 }