JIRA ID: ODUHIGH-314 : Time domain allocation for SSB in TDD mode
[o-du/l2.git] / src / du_app / du_egtp.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
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                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
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 *******************************************************************************/
18  
19 /* This file contains all EGTP related functionality */
20 #include "common_def.h"
21 #include "lrg.h"
22 #include "lkw.x"
23 #include "lrg.x"
24 #include "cm_inet.h"
25 #include "legtp.h"
26 #include "cm_inet.x"
27 #include "du_app_mac_inf.h"
28 #include "du_cfg.h"
29 #include "du_egtp.h"
30 #include "du_utils.h"
31
32 /* Global variable declaration */
33 EgtpGlobalCb egtpCb;
34
35 /**************************************************************************
36  * @brief Task Initiation callback function. 
37  *
38  * @details
39  *
40  *     Function : egtpActvInit 
41  *    
42  *     Functionality:
43  *             This function is supplied as one of parameters during EGTP's 
44  *             task registration. SSI will invoke this function once, after
45  *             it creates and attaches this TAPA Task to a system task.
46  *     
47  * @param[in]  Ent entity, the entity ID of this task.     
48  * @param[in]  Inst inst, the instance ID of this task.
49  * @param[in]  Region region, the region ID registered for memory 
50  *              usage of this task.
51  * @param[in]  Reason reason.
52  * @return ROK     - success
53  *         RFAILED - failure
54  ***************************************************************************/
55 uint8_t egtpActvInit(Ent entity, Inst inst, Region region, Reason reason)
56 {
57   DU_LOG("\n\nDEBUG   -->  EGTP : Initializing");
58
59   memset (&egtpCb, 0, sizeof(EgtpGlobalCb));
60   protType = CM_INET_PROTO_UDP;
61   gDlDataRcvdCnt = 0;
62
63   return ROK;
64 }
65
66
67 /**************************************************************************
68  * @brief Task Activation callback function. 
69  *
70  * @details
71  *
72  *      Function : egtpActvTsk 
73  * 
74  *      Functionality:
75  *           This function handles all EGTP messages received
76  *           This API is registered with SSI during the 
77  *           Task Registration of DU APP.
78  *     
79  * @param[in]  Pst     *pst, Post structure of the primitive.     
80  * @param[in]  Buffer *mBuf, Packed primitive parameters in the
81  *  buffer.
82  * @return ROK     - success
83  *         RFAILED - failure
84  *
85  ***************************************************************************/
86 uint8_t egtpActvTsk(Pst *pst, Buffer *mBuf)
87 {
88    uint8_t ret = ROK;
89
90    switch(pst->srcEnt)
91    {
92       case ENTDUAPP:
93       {
94          switch(pst->event)
95          {
96             case EVTCFGREQ:
97             {
98                ret = unpackEgtpCfgReq(egtpCfgReq, pst, mBuf);
99                break;
100             }
101             case EVTSRVOPENREQ:
102             {
103                ret = unpackEgtpSrvOpenReq(egtpSrvOpenReq, pst, mBuf);
104                break;
105             }
106             case EVTTNLMGMTREQ:
107             {
108                ret = unpackEgtpTnlMgmtReq(egtpTnlMgmtReq, pst, mBuf);
109                break;
110             }
111             default:
112             {
113                DU_LOG("\nERROR  -->  EGTP : Invalid event %d", pst->event);
114                ODU_PUT_MSG_BUF(mBuf);
115                ret = RFAILED;
116             }
117          }
118          break;
119       }
120       case ENTEGTP:
121       {
122          switch(pst->event)
123          {
124             case EVTSTARTPOLL:
125             {
126                DU_LOG("\nDEBUG   -->  EGTP : Starting Socket Polling");
127                egtpRecvMsg();
128                ODU_PUT_MSG_BUF(mBuf);
129                break;
130             }
131             default:
132             {
133                DU_LOG("\nERROR  -->  EGTP : Invalid event %d", pst->event);
134                ODU_PUT_MSG_BUF(mBuf);
135                ret = RFAILED;
136             }
137         }
138         break;
139       }
140       case ENTRLC:
141       {
142          switch(pst->event)
143          {
144             case EVTDATIND:
145             {
146                //TODO
147                break;
148             }
149             default:
150             {
151                DU_LOG("\nERROR  -->  EGTP : Invalid event %d", pst->event);
152                ret = RFAILED;
153             }
154          }
155          break;
156       }
157       default:
158       {
159          DU_LOG("\nERROR  -->  EGTP : Invalid source entity %d", pst->srcEnt);
160          ret = RFAILED;
161       }
162    }
163    ODU_EXIT_TASK();
164    return ret;
165 }
166
167 /**************************************************************************
168  * @brief EGTP server configuration 
169  *
170  * @details
171  *
172  *      Function : egtpCfgReq
173  * 
174  *      Functionality:
175  *           This function handles EGTP configuration request.
176  *     
177  * @return ROK     - success
178  *         RFAILED - failure
179  *
180  * ***********************************************************************/
181 uint8_t egtpCfgReq(Pst *pst, EgtpConfig egtpCfg)
182 {
183    uint8_t  ret;          /* Return value */
184    Pst      rspPst;      /* Response Pst structure */
185    CmStatus cfgCfm; /* Configuration Confirm */
186
187    memcpy(&egtpCb.egtpCfg, &egtpCfg, sizeof(EgtpConfig));
188
189    egtpCb.recvTptSrvr.addr.address = CM_INET_NTOH_UINT32(egtpCb.egtpCfg.localIp.ipV4Addr);
190    egtpCb.recvTptSrvr.addr.port = EGTP_DFLT_PORT;
191
192    egtpCb.dstCb.dstIp = CM_INET_NTOH_UINT32(egtpCb.egtpCfg.destIp.ipV4Addr);
193    egtpCb.dstCb.dstPort = egtpCb.egtpCfg.destPort;
194    egtpCb.dstCb.sendTptSrvr.addr.address = CM_INET_NTOH_UINT32(egtpCb.egtpCfg.localIp.ipV4Addr);
195    egtpCb.dstCb.sendTptSrvr.addr.port = egtpCb.egtpCfg.localPort;
196    egtpCb.dstCb.numTunn = 0;
197
198    ret = cmHashListInit(&(egtpCb.dstCb.teIdLst), 1024, sizeof(EgtpTeIdCb), FALSE, CM_HASH_KEYTYPE_UINT32_MOD, DU_APP_MEM_REGION, DU_POOL);
199
200    if(ret != ROK)
201    {
202       DU_LOG("\nERROR  -->  EGTP : TeId hash list initialization failed");
203       cfgCfm.status = LCM_PRIM_NOK;
204       cfgCfm.reason = LCM_REASON_HASHING_FAILED;
205    }
206    else
207    {
208       DU_LOG("\nDEBUG   -->  EGTP : EGTP configuration successful");
209       cfgCfm.status = LCM_PRIM_OK;
210       cfgCfm.reason = LCM_REASON_NOT_APPL;
211    }
212
213    /* Fill response Pst */
214    egtpFillRspPst(pst, &rspPst);
215    rspPst.event = EVTCFGCFM;
216
217    packEgtpCfgCfm(&rspPst, cfgCfm);
218
219    return ROK;
220 }
221
222 /**************************************************************************
223  * @brief Fills post structure to send response
224  *
225  * @details
226  *
227  *      Function : egtpFillRspPst
228  * 
229  *      Functionality:
230  *           Fills post struture to send response
231  *     
232  * @return ROK     - success
233  *         RFAILED - failure
234  *
235  *
236  * ***********************************************************************/
237 uint8_t egtpFillRspPst(Pst *pst, Pst *rspPst)
238 {
239
240    memset(rspPst, 0, sizeof(Pst));
241    rspPst->srcEnt = pst->dstEnt;
242    rspPst->srcInst = pst->dstInst;
243    rspPst->srcProcId = pst->dstProcId;
244    rspPst->dstEnt = pst->srcEnt;
245    rspPst->dstInst = pst->srcInst;
246    rspPst->dstProcId = pst->srcProcId;
247    rspPst->selector = ODU_SELECTOR_LC;
248    rspPst->pool= DU_POOL;
249     
250    return ROK;
251 }
252
253 /**************************************************************************
254  * @brief EGTP server open request 
255  *
256  * @details
257  *
258  *      Function : egtpSrvOpenReq
259  * 
260  *      Functionality:
261  *           This function handles EGTP open server request.
262  *           It opens udp socket to receive/send msgs.
263  *     
264  * @param[in]  Pst *pst, post structure
265  * @return ROK     - success
266  *         RFAILED - failure
267  *
268  ***************************************************************************/
269
270 uint8_t egtpSrvOpenReq(Pst *pst)
271 {
272
273    uint8_t  ret;       /* Return value */
274    Pst      rspPst;    /* Response Pst structure */ 
275    Pst      egtpPst;   /* Self post */
276    CmStatus cfm;       /* Confirmation status */
277    uint8_t  sockType;  /* Socket type */
278
279    DU_LOG("\nDEBUG  -->  EGTP : Received EGTP open server request");
280  
281    sockType = CM_INET_DGRAM;
282    ret = egtpSrvOpenPrc(sockType, &(egtpCb.recvTptSrvr));
283         /* Opening and Binding receiver socket */
284    if(ret != ROK)
285    {
286       DU_LOG("\nERROR  -->  EGTP : Failed while opening receiver transport server");
287       return ret;
288    }
289    /* Opening and Binding sender socket */
290         ret = egtpSrvOpenPrc(sockType, &(egtpCb.dstCb.sendTptSrvr));
291    if(ret != ROK)
292    {
293       DU_LOG("\nERROR  -->  EGTP : Failed while opening sender transport server");
294       return ret;
295    }
296
297    DU_LOG("\nDEBUG   -->  EGTP : Receiver socket[%d] and Sender socket[%d] open", egtpCb.recvTptSrvr.sockFd.fd,\
298    egtpCb.dstCb.sendTptSrvr.sockFd.fd);
299
300    /* Start Socket polling */
301    memset(&egtpPst, 0, sizeof(egtpPst));
302    egtpPst.srcEnt = (Ent)ENTEGTP;
303    egtpPst.srcInst = (Inst)EGTP_INST;
304    egtpPst.srcProcId = DU_PROC;
305    egtpPst.dstEnt = (Ent)ENTEGTP;
306    egtpPst.dstInst = (Inst)EGTP_INST;
307    egtpPst.dstProcId = DU_PROC;
308    egtpPst.event = EVTSTARTPOLL;
309    egtpPst.selector = ODU_SELECTOR_LC;
310    egtpPst.pool= DU_POOL;
311    packEgtpStartPollingReq(&egtpPst);
312
313    /* Filling and sending response */
314    cfm.status = LCM_PRIM_OK;
315    cfm.reason = LCM_REASON_NOT_APPL;
316
317    egtpFillRspPst(pst, &rspPst);
318    rspPst.event = EVTSRVOPENCFM;
319    packEgtpSrvOpenCfm(&rspPst, cfm);
320
321    return ret;
322 }
323
324 /*******************************************************************
325  *
326  * @brief Processing Sever open request
327  *
328  * @details
329  *
330  *    Function : egtpSrvOpenPrc
331  *
332  *    Functionality:
333  *      
334  *
335  * @params[in] 
336  * @return ROK     - success
337  *         RFAILED - failure
338  *
339  * ****************************************************************/
340
341 uint8_t egtpSrvOpenPrc(uint8_t sockType, EgtpTptSrvr *server)
342 {
343    S8 ret=ROK;
344    ret = cmInetSocket(sockType, &(server->sockFd), protType); 
345         if(ret != ROK)
346    {  
347       DU_LOG("\nERROR  -->  EGTP : Failed to open UDP socket");
348       return ret;
349    }
350    ret = cmInetBind(&(server->sockFd), &(server->addr));  
351    if(ret != ROK)
352    {  
353       DU_LOG("\nERROR  -->  EGTP : Failed to bind socket");
354       return ret;
355    }
356    
357    return ret;
358 }
359
360 /**************************************************************************
361  * @brief EGTP tunnel management request
362  *
363  * @details
364  *
365  *      Function : egtpTnlMgmtReq
366  * 
367  *      Functionality:
368  *           This function handles EGTP tunnel managament request
369  *     
370  * @param[in] Tunnel Eveny structure
371  * @return ROK     - success
372  *         RFAILED - failure
373  *
374  
375  * ***************************************************************************/
376 uint8_t egtpTnlMgmtReq(Pst *pst, EgtpTnlEvt tnlEvt)
377 {
378    uint8_t ret = ROK;
379
380    DU_LOG("\nDEBUG   -->  EGTP : Received tunnel management request");
381    switch(tnlEvt.action)
382    {
383       case EGTP_TNL_MGMT_ADD:
384       {
385          ret = egtpTnlAdd(tnlEvt);
386          break;
387       }
388       case EGTP_TNL_MGMT_MOD:
389       {
390          ret = egtpTnlMod(tnlEvt);
391          break;
392       }
393       case EGTP_TNL_MGMT_DEL:
394       {
395          ret = egtpTnlDel(tnlEvt);
396          break;
397       }
398       default:
399       {
400          DU_LOG("\nERROR  -->  EGTP : Invalid tunnel management action[%d]", tnlEvt.action);
401          ret = LCM_REASON_INVALID_ACTION;
402       }
403    }
404
405    if(ret == ROK)
406    {
407       tnlEvt.cfmStatus.status = LCM_PRIM_OK;
408       tnlEvt.cfmStatus.reason = LCM_REASON_NOT_APPL;
409    }
410    else
411    {
412       tnlEvt.cfmStatus.status = LCM_PRIM_NOK;
413       tnlEvt.cfmStatus.reason = ret;
414    }
415
416    DU_LOG("\nDEBUG   -->  EGTP : Sending Tunnel management confirmation");
417    duHdlEgtpTnlMgmtCfm(tnlEvt);
418
419    return ret;
420 }
421
422 /**************************************************************************
423  * @brief EGTP tunnel addition
424  *
425  * @details
426  *
427  *      Function : egtpTnlAdd
428  * 
429  *      Functionality:
430  *           This function handles EGTP tunnel addition
431  *     
432  * @param[in]  Tunnel Event structure
433  * @return ROK     - success
434  *         RFAILED - failure
435  *
436  * ***************************************************************************/
437 uint8_t egtpTnlAdd(EgtpTnlEvt tnlEvt)
438 {
439    uint8_t    ret;
440    EgtpTeIdCb *teidCb;    /* Tunnel endpoint control block */
441    EgtpMsgHdr preDefHdr; /* pre-define header for this tunnel */
442
443    DU_LOG("\nINFO   -->  EGTP : Tunnel addition : LocalTeid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
444
445    DU_ALLOC(teidCb, sizeof(EgtpTeIdCb));
446    if(teidCb == NULLP)
447    {
448       DU_LOG("\nERROR  -->  EGTP : Memory allocation failed");
449       return LCM_REASON_MEM_NOAVAIL;
450    }
451
452    memset(teidCb, 0, sizeof(EgtpTeIdCb));
453    teidCb->teId = tnlEvt.lclTeid;
454    teidCb->remTeId = tnlEvt.remTeid;
455
456    ret = cmHashListInsert(&(egtpCb.dstCb.teIdLst), (PTR)teidCb, (uint8_t *)&(teidCb->teId), sizeof(uint32_t));
457    if(ret != ROK)
458    {
459       DU_LOG("\nERROR  -->  EGTP : Failed to insert in hash list");
460       DU_FREE(teidCb, sizeof(EgtpTeIdCb));
461       return LCM_REASON_HASHING_FAILED;
462    }
463    egtpCb.dstCb.numTunn++;
464
465    /* Encoding pre-defined header */
466    memset(&preDefHdr, 0, sizeof(EgtpMsgHdr));
467    preDefHdr.msgType = EGTPU_MSG_GPDU;
468    preDefHdr.teId = teidCb->remTeId;
469    preDefHdr.extHdr.pdcpNmb.pres = FALSE;
470    preDefHdr.extHdr.udpPort.pres = FALSE;
471    preDefHdr.nPdu.pres = FALSE;
472   
473    egtpEncodeHdr((uint8_t *)teidCb->preEncodedHdr.hdr, &preDefHdr, &(teidCb->preEncodedHdr.cnt));
474
475    return ROK;
476 } /* egtpTnlAdd */
477
478 /**************************************************************************
479  * @brief EGTP tunnel modification
480  *
481  * @details
482  *
483  *      Function : egtpTnlMod
484  * 
485  *      Functionality:
486  *           This function handles EGTP tunnel modification
487  *     
488  * @param[in]  Tunnel Event structure
489  * @return ROK     - success
490  *         RFAILED - failure
491  * 
492  * ***************************************************************************/
493 uint8_t egtpTnlMod(EgtpTnlEvt tnlEvt)
494 {
495    EgtpTeIdCb     *teidCb = NULLP;
496
497    DU_LOG("\nINFO   -->  EGTP : Tunnel modification : LocalTeid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
498
499    cmHashListFind(&(egtpCb.dstCb.teIdLst), (uint8_t *)&(tnlEvt.lclTeid), sizeof(uint32_t), 0, (PTR *)&teidCb);
500    if(teidCb == NULLP)
501    {
502       DU_LOG("\nERROR  -->  EGTP : Tunnel id not found");
503       return RFAILED;
504    }  
505    teidCb->teId = tnlEvt.remTeid;
506    teidCb->remTeId = tnlEvt.remTeid;
507    return ROK;
508 }
509
510 /**************************************************************************
511  * @brief EGTP tunnel deletion
512  *
513  * @details
514  *
515  *     Function : egtpTnlDel
516  * 
517  *     Functionality:
518  *         This function handles EGTP tunnel deletion
519  *    
520  * @param[in]  Tunnel Event structure
521  * @return ROK     - success
522  *         RFAILED - failure
523  * 
524  * ***************************************************************************/
525 uint8_t egtpTnlDel(EgtpTnlEvt tnlEvt)
526 {
527    EgtpTeIdCb     *teidCb = NULLP;
528
529    DU_LOG("\nINFO   -->  EGTP : Tunnel deletion : Local Teid[%d] Remote Teid[%d]", tnlEvt.lclTeid, tnlEvt.remTeid);
530    
531    cmHashListFind(&(egtpCb.dstCb.teIdLst), (uint8_t *)&(tnlEvt.lclTeid), sizeof(uint32_t), 0, (PTR *)&teidCb);
532    if(teidCb == NULLP)
533    {
534       DU_LOG("\nERROR  -->  EGTP : Tunnel id[%d] not configured", tnlEvt.lclTeid);
535       return LCM_REASON_INVALID_PAR_VAL;
536    } 
537
538    cmHashListDelete(&(egtpCb.dstCb.teIdLst), (PTR)teidCb);
539    DU_FREE(teidCb, sizeof(EgtpTeIdCb));
540    egtpCb.dstCb.numTunn--;
541    return ROK;
542 }
543
544 /*******************************************************************
545  *
546  * @brief Handles data indication
547  *
548  * @details
549  *
550  *    Function : EgtpHdlDatInd
551  *
552  *    Functionality:
553  *      Handles incoming data from peer to be passed 
554  *      on from DU to CU
555  *
556  * @params[in] 
557  * @return ROK     - success
558  *         RFAILED - failure
559  *
560  * ****************************************************************/
561 uint8_t egtpHdlDatInd(EgtpMsg egtpMsg)
562 {
563    EgtpTeIdCb  *teidCb = NULLP;
564    uint16_t    tPduSize;
565    uint8_t     hdrLen;
566    uint32_t    msgLen;
567    EgtpMsgHdr  *msgHdr;
568
569    DU_LOG("\nDEBUG  -->  EGTP : Received Data Indication");
570
571    cmHashListFind(&(egtpCb.dstCb.teIdLst), (uint8_t *)&(egtpMsg.msgHdr.teId), sizeof(uint32_t), 0, (PTR *)&teidCb);
572    if(teidCb == NULLP)
573    {
574       DU_LOG("\nERROR  -->  EGTP : Tunnel id[%d] not configured", egtpMsg.msgHdr.teId);
575       return LCM_REASON_INVALID_PAR_VAL;
576    }
577    
578    msgHdr = &(egtpMsg.msgHdr);
579
580    hdrLen = teidCb->preEncodedHdr.cnt;
581
582    if(msgHdr->extHdr.pdcpNmb.pres)
583    {
584       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] |= EGTP_MASK_BIT3;
585       teidCb->preEncodedHdr.hdr[hdrLen] = EGTP_EXT_HDR_PDCP_TYPE;
586       teidCb->preEncodedHdr.hdr[--hdrLen] = 1;
587       teidCb->preEncodedHdr.hdr[--hdrLen] = GetHiByte(msgHdr->extHdr.pdcpNmb.val);
588       teidCb->preEncodedHdr.hdr[--hdrLen] = GetLoByte(msgHdr->extHdr.pdcpNmb.val);
589       teidCb->preEncodedHdr.hdr[--hdrLen] = 0;
590    }
591    else
592    {
593       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] &= ~(EGTP_MASK_BIT3);
594    }
595
596    ODU_GET_MSG_LEN(egtpMsg.msg, (int16_t *)&tPduSize);
597
598    /*Adjust the header to fill the correct length*/
599    msgLen = tPduSize +  (EGTP_MAX_HDR_LEN - hdrLen) - 0x08;
600
601    /***********************************************
602     * Fill the length field of the message header *
603     ***********************************************/
604    teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 3] = (uint8_t)GetHiByte(msgLen);
605    teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 4] = (uint8_t)GetLoByte(msgLen);
606
607    /*Update the sequence number*/
608    if(egtpMsg.msgHdr.seqNum.pres)
609    {
610       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] |= (EGTP_MASK_BIT2);
611       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 9] = (uint8_t)GetHiByte(egtpMsg.msgHdr.seqNum.val);
612       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 10] = (uint8_t)GetLoByte(egtpMsg.msgHdr.seqNum.val);
613    }
614    else
615    {
616       teidCb->preEncodedHdr.hdr[EGTP_MAX_HDR_LEN - 1] &= ~(EGTP_MASK_BIT2);
617    }
618
619    DU_LOG("\nDEBUG  -->  EGTP : UL Data buffer before encoding header");
620    ODU_PRINT_MSG(egtpMsg.msg, 0, 0);
621
622    ODU_ADD_PRE_MSG_MULT(&teidCb->preEncodedHdr.hdr[hdrLen], (EGTP_MAX_HDR_LEN - hdrLen), egtpMsg.msg);
623
624
625    DU_LOG("\nDEBUG  -->  EGTP : UL Data buffer after encoding header");
626    ODU_PRINT_MSG(egtpMsg.msg, 0, 0);
627
628    /* Send over UDP */
629    egtpSendMsg(egtpMsg.msg);
630    ODU_PUT_MSG_BUF(egtpMsg.msg);
631
632    return ROK;
633 }/* EgtpHdlDatInd */
634
635 /*******************************************************************
636  *
637  * @brief Encodes outgoing message
638  *
639  * @details
640  *
641  *    Function : egtpEncodeMsg
642  *
643  *    Functionality:
644  *       Encodes EGTP message to be sent
645  *
646  * @params[in] EGTP message
647  *             Message Buffer 
648  * @return ROK     - success
649  *         RFAILED - failure
650  *
651  * ****************************************************************/
652 uint8_t egtpEncodeHdr(uint8_t *preEncodedHdr, EgtpMsgHdr *preDefHdr, uint8_t *hdrIdx)
653 {
654    uint8_t    tmpByte = 0;                 /* Stores one byte of data for enc */
655    uint8_t    cnt     = EGTP_MAX_HDR_LEN;  /* Stores the position */
656    bool       extPres = FALSE;             /* Flag for indication of S, E or P presense flag */
657    uint16_t   nwWord = 0;
658    
659    /* Encoding header */
660    tmpByte |= EGTP_MASK_BIT6;   /* Setting 6th LSB of 1st byte as version */
661    tmpByte |= EGTP_MASK_BIT5;   /* Setting 5th LSB of 1st byte as protocol type */
662
663    if(preDefHdr->extHdr.udpPort.pres || preDefHdr->extHdr.pdcpNmb.pres)
664    {
665       tmpByte |= EGTP_MASK_BIT3;  /* Setting 3rd LSB of 1st byte if Extension heaeder is present */
666    }
667
668    if(preDefHdr->seqNum.pres)
669    {
670       tmpByte |= EGTP_MASK_BIT2;
671    }
672
673    if(preDefHdr->nPdu.pres)
674    {
675       tmpByte |= EGTP_MASK_BIT1;
676    }
677
678    if((tmpByte & EGTP_MASK_BIT1) || (tmpByte & EGTP_MASK_BIT2)||(tmpByte & EGTP_MASK_BIT3))
679    {
680       extPres = TRUE;
681    }
682
683    preEncodedHdr[--cnt] = tmpByte;
684    preEncodedHdr[--cnt] = preDefHdr->msgType;
685
686    /* Encode Tunnel endpoint */
687    preEncodedHdr[--cnt] = 0;
688    preEncodedHdr[--cnt] = 0;
689    nwWord = (uint16_t)(GetHiWord(preDefHdr->teId));
690    preEncodedHdr[--cnt] = (uint8_t)(GetHiByte(nwWord));
691    preEncodedHdr[--cnt] = (uint8_t)(GetLoByte(nwWord));
692    nwWord = (uint16_t)(GetLoWord(preDefHdr->teId));
693    preEncodedHdr[--cnt] = (uint8_t)(GetHiByte(nwWord));
694    preEncodedHdr[--cnt] = (uint8_t)(GetLoByte(nwWord));
695
696    /* Encode sequence number */
697    if(preDefHdr->seqNum.pres)
698    {
699       preEncodedHdr[--cnt] = GetHiByte(preDefHdr->seqNum.val);
700       preEncodedHdr[--cnt] = GetLoByte(preDefHdr->seqNum.val);
701    }
702    else if(extPres)
703    {
704       preEncodedHdr[--cnt] = 0;
705       preEncodedHdr[--cnt] = 0;
706    }
707
708    /* Encode nPdu number */
709    if(preDefHdr->nPdu.pres)
710    {
711       preEncodedHdr[--cnt] = preDefHdr->nPdu.val;
712    }
713    else if(extPres)
714    {
715       preEncodedHdr[--cnt] = 0;
716    }
717
718    if(preDefHdr->extHdr.udpPort.pres)
719    {
720       preEncodedHdr[--cnt] = EGTP_EXT_HDR_UDP_TYPE;
721       preEncodedHdr[--cnt] = 1;
722       preEncodedHdr[--cnt] = GetHiByte(preDefHdr->extHdr.udpPort.val);
723       preEncodedHdr[--cnt] = GetLoByte(preDefHdr->extHdr.udpPort.val);
724    }
725  
726    if(preDefHdr->extHdr.pdcpNmb.pres)
727    {
728       preEncodedHdr[--cnt] = EGTP_EXT_HDR_PDCP_TYPE;
729       preEncodedHdr[--cnt] = 1;
730       preEncodedHdr[--cnt] = GetHiByte(preDefHdr->extHdr.pdcpNmb.val);
731       preEncodedHdr[--cnt] = GetLoByte(preDefHdr->extHdr.pdcpNmb.val);
732    }
733  
734    if(tmpByte & EGTP_MASK_BIT3)
735    {
736       preEncodedHdr[--cnt] = 0;
737    }
738    else if(extPres)
739    {
740       preEncodedHdr[--cnt] = 0;
741    }
742
743    *hdrIdx = cnt;
744    return ROK;
745 } /* egtpEncodeHdr */
746
747 /*******************************************************************
748  *
749  * @brief Sends message over UDP
750  *
751  * @details
752  *
753  *    Function : egtpSendMsg
754  *
755  *    Functionality:
756  *           Sends message over UDP
757  *
758  * @params[in] Message Buffer 
759  * @return ROK     - success
760  *         RFAILED - failure
761  *
762  * ****************************************************************/
763 uint8_t egtpSendMsg(Buffer *mBuf)
764 {
765    uint8_t        ret;
766    uint16_t       txLen;
767    CmInetMemInfo  info;
768    CmInetAddr     dstAddr;
769
770    info.region = DU_APP_MEM_REGION;
771    info.pool = DU_POOL;
772
773    dstAddr.port = EGTP_DFLT_PORT;
774    dstAddr.address = egtpCb.dstCb.dstIp;
775
776    ret = cmInetSendMsg(&(egtpCb.dstCb.sendTptSrvr.sockFd), &dstAddr, &info, \
777       mBuf, (int16_t *)&txLen, CM_INET_NO_FLAG);
778    if(ret != ROK && ret != RWOULDBLOCK)
779    {
780       DU_LOG("\nERROR  -->  EGTP : Failed sending the message");
781       return RFAILED;
782    }
783
784    DU_LOG("\nDEBUG   -->  EGTP : Message Sent");
785
786    return ROK;
787 }
788
789 /*******************************************************************
790  *
791  * @brief Receives EGTP message from UDP socket 
792  *
793  * @details
794  *
795  *    Function : egtpRecvMsg
796  *
797  *    Functionality:
798  *      Receive incoming messages from UDP socket
799  *
800  * @params[in] 
801  * @return ROK     - success
802  *         RFAILED - failure
803  *
804  * ****************************************************************/
805
806 uint8_t egtpRecvMsg()
807 {
808    uint8_t        ret;           /* Return value */
809    uint16_t       bufLen;        /* Length of received buffer */
810    Buffer         *recvBuf;      /* Received buffer */
811    CmInetAddr     fromAddr;      /* Egtp data sender address */
812    CmInetMemInfo  memInfo;       /* Buffer allocation info */
813
814    memInfo.region = DU_APP_MEM_REGION;
815    memInfo.pool   = DU_POOL;
816     
817    fromAddr.port = egtpCb.dstCb.dstPort;
818    fromAddr.address = egtpCb.dstCb.dstIp;
819
820    while(true)
821    {
822       bufLen = -1;
823       ret = cmInetRecvMsg(&(egtpCb.recvTptSrvr.sockFd), &fromAddr, &memInfo, \
824          &recvBuf, (int16_t *)&bufLen, CM_INET_NO_FLAG);
825       if(ret == ROK && recvBuf != NULLP)
826       {  
827          //DU_LOG("\nDEBUG  -->  EGTP : Received DL Message[%ld]\n", gDlDataRcvdCnt + 1);
828          //ODU_PRINT_MSG(recvBuf, 0 ,0);
829          egtpHdlRecvData(recvBuf);
830          gDlDataRcvdCnt++;
831       }
832    }
833    
834    return ROK;
835 }
836
837 /*******************************************************************
838  *
839  * @brief Handles DL User data received from CU
840  *
841  * @details
842  *
843  *    Function : egtpHdlRecvData
844  *
845  *    Functionality: Handles DL User data received from CU
846  *
847  * @params[in] DL Usre data buffer
848  * @return ROK     - success
849  *         RFAILED - failure
850  *
851  * ****************************************************************/
852 uint8_t egtpHdlRecvData(Buffer *mBuf)
853 {
854    EgtpMsg  egtpMsg;
855
856    /* Decode EGTP header */
857    egtpDecodeHdr(mBuf, &egtpMsg);
858
859    /* TODO : Send received message to RLC */
860    duHdlEgtpDlData(&egtpMsg);
861
862    return ROK;
863 }
864
865 /*******************************************************************
866  *
867  * @brief Decodes EGTP header from DL User data
868  *
869  * @details
870  *
871  *    Function : egtpDecodeHdr
872  *
873  *    Functionality: Decodes EGTP header from DL User data
874  *
875  * @params[in] 
876  * @return ROK     - success
877  *         RFAILED - failure
878  *
879  * ****************************************************************/
880 uint8_t egtpDecodeHdr(Buffer *mBuf, EgtpMsg  *egtpMsg)
881 {
882    uint8_t    tmpByte[5];         /* Holds 5 byte of data after Decoding */
883    uint8_t    version = 0;         /* Holds the version type, decoded */
884    uint16_t   msgLen  = 0;         /* Holds the msgLen from the Hdr */
885    uint16_t   bufLen  = 0;         /* Holds the total buffer length */
886    uint8_t    extHdrType = 0;       /* Holds the Extension hdr type */
887    uint8_t    extHdrLen = 0;        /* Extension hdr length */
888    bool       extPres = FALSE;      /* Flag for indication of S, E or P presense flag */
889  
890    ODU_GET_MSG_LEN(mBuf, (int16_t *)&bufLen);
891  
892    /* Decode first byte and storing in temporary variable */
893    ODU_REM_PRE_MSG(&tmpByte[0], mBuf);
894
895    /* Extracting version fro 1st byte */
896    version = tmpByte[0] >> 5;
897    
898    //DU_LOG("\nDEBUG   -->  EGTP : Version %d", version);
899  
900    /* Decode message type */
901    ODU_REM_PRE_MSG((Data*)&(egtpMsg->msgHdr.msgType), mBuf);
902    //DU_LOG("\nDEBUG   -->  EGTP : msgType %d", egtpMsg->msgHdr.msgType);
903
904    /****************************************************************************
905     * Message length param is 2 bytes. So decode next 2 bytes from msg hdr and
906     * performing OR operation on these two bytes to calculate message length 
907     ***************************************************************************/
908    ODU_REM_PRE_MSG(&tmpByte[1], mBuf);
909    ODU_REM_PRE_MSG(&tmpByte[2], mBuf);
910    msgLen = (tmpByte[1] << 8) | tmpByte[2];
911    UNUSED(msgLen);
912    UNUSED(version);
913    //DU_LOG("\nDEBUG   -->  EGTP : msgLen %d", msgLen);
914
915
916    /****************************************************************************
917     * Tunnel id param is 4 bytes. So decode next 4 bytes from msg hdr and 
918     * perform OR operation on these 4 bytes to calculate tunnel id
919     ***************************************************************************/
920    ODU_REM_PRE_MSG(&tmpByte[1], mBuf);
921    ODU_REM_PRE_MSG(&tmpByte[2], mBuf);
922    ODU_REM_PRE_MSG(&tmpByte[3], mBuf);
923    ODU_REM_PRE_MSG(&tmpByte[4], mBuf);
924    egtpMsg->msgHdr.teId = (tmpByte[1] << 24) | (tmpByte[2] << 16) | (tmpByte[3] << 8) | tmpByte[4];
925    //DU_LOG("\nDEBUG   -->  EGTP : teId %d",egtpMsg->msgHdr.teId);
926
927
928    /* If any one of S, E or PN flag is set, set extension present as true. */
929    if((tmpByte[0] & EGTP_MASK_BIT1) || (tmpByte[0] & EGTP_MASK_BIT2)||(tmpByte[0] & EGTP_MASK_BIT3))
930    {
931       extPres = TRUE;
932    }
933
934    /* Decode sequence number, if S flag is set in first byte */
935    if (tmpByte[0] & EGTP_MASK_BIT2)
936    {
937       /************************************************************************
938        * Sequence num is 2 bytes. So decode next 2 bytes from msg hdr and 
939        * perform OR operation on them 
940        ************************************************************************/
941       egtpMsg->msgHdr.seqNum.pres = TRUE;
942       ODU_REM_PRE_MSG(&tmpByte[1], mBuf);
943       ODU_REM_PRE_MSG(&tmpByte[2], mBuf);
944       egtpMsg->msgHdr.seqNum.val = (tmpByte[1] << 8) | tmpByte[2];
945    }
946    /**************************************************************************** 
947     * If extPres is true, but S bit is not set, implies, either of PN or E bit 
948     * was set during Encode so accordingly extract Byte fields for seqNum anyway 
949     ***************************************************************************/
950    else if(extPres)
951    {
952       /*************************************************************************
953        * Sequence num is 2 bytes. So decode next 2 bytes from msg hdr and 
954        * perform OR operation on them 
955        ************************************************************************/
956       egtpMsg->msgHdr.seqNum.pres = 0;
957       ODU_REM_PRE_MSG(&tmpByte[1], mBuf);
958       ODU_REM_PRE_MSG(&tmpByte[2], mBuf);
959       egtpMsg->msgHdr.seqNum.val = (tmpByte[1] << 8) | tmpByte[2];
960    }
961
962    /* Decode N-PDU number if present flag is set */
963    if (tmpByte[0] & EGTP_MASK_BIT1)
964    {
965       egtpMsg->msgHdr.nPdu.pres = TRUE;
966       ODU_REM_PRE_MSG(&(egtpMsg->msgHdr.nPdu.val), mBuf);
967    }
968    /****************************************************************************
969     * If extPres is true, but PN bit is not set, implies, either of S or E bit 
970     * was set during Encode. Aaccordingly extract Byte fields of N-PDU num anyway 
971     ***************************************************************************/
972    else if(extPres)
973    {
974       egtpMsg->msgHdr.nPdu.pres = TRUE;
975       ODU_REM_PRE_MSG(&(egtpMsg->msgHdr.nPdu.val), mBuf);
976    }
977
978    /* If E flag is set in first byte, decode extension header */ 
979    if(tmpByte[0] & EGTP_MASK_BIT3)
980    {
981       ODU_REM_PRE_MSG(&extHdrType, mBuf);
982       while( 0 != extHdrType)
983       {
984          switch (extHdrType)
985          {
986             case EGTP_EXT_HDR_UDP_TYPE:
987             {
988                ODU_REM_PRE_MSG(&extHdrLen, mBuf);
989                if(extHdrLen == 0x01)
990                {
991                   /************************************************************
992                    * UDP Port is 2 bytes. So decode next 2 bytes from msg hdr
993                   * and perform OR operation on them 
994                   *************************************************************/
995                   egtpMsg->msgHdr.extHdr.udpPort.pres = TRUE;
996                   ODU_REM_PRE_MSG(&tmpByte[1], mBuf);
997                   ODU_REM_PRE_MSG(&tmpByte[2], mBuf);
998                   egtpMsg->msgHdr.extHdr.udpPort.val = (tmpByte[1] << 8) | tmpByte[2];
999                }
1000                break;
1001             }
1002
1003             case EGTP_EXT_HDR_PDCP_TYPE:
1004             {
1005                ODU_REM_PRE_MSG(&extHdrLen, mBuf);
1006                if(extHdrLen == 0x01)
1007                {
1008                   /*************************************************************
1009                    * PDCP num is 2 bytes. So decode next 2 bytes from msg hdr
1010                    * and perform OR operation on them 
1011                    ************************************************************/
1012                   egtpMsg->msgHdr.extHdr.pdcpNmb.pres = TRUE;
1013                   ODU_REM_PRE_MSG(&tmpByte[1], mBuf);
1014                   ODU_REM_PRE_MSG(&tmpByte[2], mBuf);
1015                   egtpMsg->msgHdr.extHdr.pdcpNmb.val = (tmpByte[1] << 8) | tmpByte[2];
1016                }
1017                break;
1018             }
1019          } /* End of switch */
1020  
1021          ODU_REM_PRE_MSG(&extHdrType, mBuf);
1022  
1023       } /* End of while */
1024    }  
1025    /****************************************************************************
1026     * If extPres is true, but E bit is not set, implies, either of PN or S bit 
1027     * was set during Encode so accordingly extract Byte fields for extension
1028     * header anyway 
1029     ***************************************************************************/
1030    else if(extPres)
1031    {
1032       ODU_REM_PRE_MSG(&extHdrType, mBuf);
1033    }
1034
1035    egtpMsg->msg = mBuf;
1036
1037    //DU_LOG("\nDEBUG   -->  EGTP : DL Data Buffer after decoding header ");
1038    //ODU_PRINT_MSG(mBuf, 0, 0);
1039
1040    /* Forward the data to duApp/RLC */
1041  
1042    return ROK;
1043
1044 }
1045 /**********************************************************************
1046          End of file
1047 **********************************************************************/