[Epic-ID: ODUHIGH-463][Task-ID: ODUHIGH-497] Inter-CU-HO: GNB-DU Config Query
[o-du/l2.git] / src / cu_stub / cu_stub.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 functions contains main() for cu_app */
20 #include "common_def.h"
21 #include "cu_stub_sctp.h"
22 #include "cu_stub_egtp.h"
23 #include "OCTET_STRING.h"
24 #include "cu_f1ap_msg_hdl.h"
25 #include "cu_stub.h"
26
27 #ifdef O1_ENABLE
28 #include "CmInterface.h"
29 #endif
30
31
32 #ifdef O1_ENABLE
33 extern StartupConfig g_cfg;
34 #endif
35
36 /*******************************************************************
37  *
38  * @brief Handles SCTP notification 
39  *
40  * @details
41  *
42  *    Function : sctpNtfyInd
43  *
44  *    Functionality:
45  *         Handles SCTP notification
46  *
47  * @params[in] sctp notification
48  * @return void
49  *
50  ******************************************************************/
51 void sctpNtfyInd(CmInetSctpNotification *ntfy)
52 {
53 //TODO
54 }
55
56 void init_log()
57 {
58     openlog("CU_STUB",LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
59 }
60 /*******************************************************************
61  *
62  * @brief Main function of CU APP
63  *
64  * @details
65  *
66  *    Function : main
67  *
68  *    Functionality:
69  *         - Reads CU related configurations
70  *         - Initialize SCTP Parameters
71  *         - Start SCTP receiver thread
72  *
73  * @params[in] 
74  * @return ROK     - success
75  *         RFAILED - failure
76  *
77  * ****************************************************************/
78
79 uint8_t tst()
80 {
81    int retVal=0;
82    pthread_t conThrdId;
83    pthread_attr_t attr;
84
85    init_log();   
86    DU_LOG("\nINFO   -->  CU_STUB : Starting CU_STUB\n");
87
88    /* Start thread to receive console input */
89    pthread_attr_init(&attr);
90    pthread_attr_setstacksize(&attr, (size_t)NULLD);
91    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
92    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
93    retVal = pthread_create(&conThrdId, &attr, cuConsoleHandler, NULLP);
94    if(retVal != 0)
95    {
96       DU_LOG("\nERROR  -->  CU_STUB :  Thread creation failed. Cause %d", retVal);
97    }
98    pthread_attr_destroy(&attr);
99
100    /* Read CU configurations */
101    readCuCfg();
102
103    /* Start CU-EGTP */
104    egtpActvInit();
105    egtpInitReq();
106
107    /* Initializing SCTP global parameters */
108    sctpActvInit();
109  
110    /* Start CU-SCTP to listen on incoming connection */
111    sctpCfgReq();
112    sctpStartReq();
113
114    return ROK;
115 }
116
117 /*******************************************************************
118  *
119  * @brief Read CU related configuration
120  *
121  * @details
122  *
123  *    Function : readCuCfg
124  *
125  *    Functionality:
126  *            - RRead CU related configuration
127  *
128  * @params[in] 
129  * @return ROK     - success
130  *         RFAILED - failure
131  *
132  * ****************************************************************/
133
134 void readCuCfg()
135 {
136    uint8_t  numDu, *numRemoteCu;
137    uint32_t ipv4_du, ipv4_cu, ipv4_remote_cu;
138
139    DU_LOG("\nDEBUG  -->  CU_STUB : Reading CU configurations");
140
141    cuCb.cuCfgParams.cuId = CU_ID;
142    strcpy(cuCb.cuCfgParams.cuName, CU_NAME);
143
144 #ifdef O1_ENABLE
145    if( getStartupConfigForStub(&g_cfg) != ROK )
146    {
147       DU_LOG("\nError  -->  CU_STUB : Could not fetch startup "\
148              "configurations from Netconf interface\n");
149       exit(1);
150    }
151    
152    cmInetAddr((S8*)g_cfg.DU_IPV4_Addr, &ipv4_du);
153    cuCb.cuCfgParams.sctpParams.destCb[0].destIpAddr.ipV4Addr = ipv4_du;
154    cuCb.cuCfgParams.sctpParams.destCb[0].destIpAddr.ipV6Pres = false;
155    
156    cmInetAddr((S8*)g_cfg.CU_IPV4_Addr, &ipv4_cu);
157    cuCb.cuCfgParams.sctpParams.localIpAddr.ipV4Addr = ipv4_cu;
158    cuCb.cuCfgParams.sctpParams.localIpAddr.ipV6Pres = false;
159    
160    cuCb.cuCfgParams.sctpParams.destCb[0].destPort = g_cfg.DU_Port;
161    cuCb.cuCfgParams.sctpParams.f1SctpPort = g_cfg.CU_Port; 
162    cuCb.cuCfgParams.sctpParams.numDestNode = 1;
163    
164    cuCb.cuCfgParams.egtpParams.localIp.ipV4Pres = TRUE;
165    cuCb.cuCfgParams.egtpParams.localIp.ipV4Addr = ipv4_cu;
166    cuCb.cuCfgParams.egtpParams.localPort = F1_EGTP_PORT;
167    cuCb.cuCfgParams.egtpParams.dstCfg[0].dstIp.ipV4Pres = TRUE;
168    cuCb.cuCfgParams.egtpParams.dstCfg[0].dstIp.ipV4Addr = ipv4_du;
169    cuCb.cuCfgParams.egtpParams.dstCfg[0].dstPort = F1_EGTP_PORT;
170    cuCb.cuCfgParams.egtpParams.minTunnelId = MIN_TEID;
171    cuCb.cuCfgParams.egtpParams.currTunnelId = cuCb.cuCfgParams.egtpParams.minTunnelId;
172    cuCb.cuCfgParams.egtpParams.maxTunnelId = MAX_TEID;
173    cuCb.cuCfgParams.egtpParams.numDu = 1;
174
175 #else
176    /* CU IP Address and Port*/
177    memset(&ipv4_du, 0, sizeof(uint32_t));
178    cmInetAddr((S8*)LOCAL_IP_CU, &ipv4_cu);
179
180    cuCb.cuCfgParams.sctpParams.localIpAddr.ipV4Addr = ipv4_cu;
181    cuCb.cuCfgParams.sctpParams.localIpAddr.ipV6Pres = false;
182
183    /* SCTP and EGTP configuration for F1 Interface */
184    cuCb.cuCfgParams.sctpParams.f1SctpInfo.port = F1_SCTP_PORT;
185
186    cuCb.cuCfgParams.egtpParams.localIp.ipV4Pres = TRUE;
187    cuCb.cuCfgParams.egtpParams.localIp.ipV4Addr = ipv4_cu;
188    cuCb.cuCfgParams.egtpParams.localPort = F1_EGTP_PORT;
189
190    cuCb.cuCfgParams.sctpParams.f1SctpInfo.numDestNode = 0;
191    cuCb.cuCfgParams.egtpParams.numDu = 0;
192
193    numDu = 0;
194    while(numDu < NUM_F1_ASSOC)
195    {
196       /* DU IP Address */
197       memset(&ipv4_du, 0, sizeof(uint32_t));
198       cmInetAddr((S8*)REMOTE_IP_DU[numDu], &ipv4_du);
199       
200       /* SCTP Parameters */
201       cuCb.cuCfgParams.sctpParams.f1SctpInfo.destCb[numDu].destIpAddr.ipV4Addr = ipv4_du;
202       cuCb.cuCfgParams.sctpParams.f1SctpInfo.destCb[numDu].destIpAddr.ipV6Pres = false;
203       cuCb.cuCfgParams.sctpParams.f1SctpInfo.destCb[numDu].destPort = F1_SCTP_PORT;
204
205       /* EGTP Parameters */
206       cuCb.cuCfgParams.egtpParams.dstCfg[numDu].dstIp.ipV4Pres = TRUE;
207       cuCb.cuCfgParams.egtpParams.dstCfg[numDu].dstIp.ipV4Addr = ipv4_du;
208       cuCb.cuCfgParams.egtpParams.dstCfg[numDu].dstPort = F1_EGTP_PORT;
209       
210       (numDu)++;
211    }
212
213    cuCb.cuCfgParams.egtpParams.minTunnelId = MIN_TEID;
214    cuCb.cuCfgParams.egtpParams.currTunnelId = cuCb.cuCfgParams.egtpParams.minTunnelId;
215    cuCb.cuCfgParams.egtpParams.maxTunnelId = MAX_TEID;
216    cuCb.cuCfgParams.egtpParams.numDu = numDu;
217    cuCb.cuCfgParams.sctpParams.f1SctpInfo.numDestNode = numDu;
218
219    /* SCTP configuration for Xn interface */
220    cuCb.cuCfgParams.sctpParams.xnSctpInfo.port = XN_SCTP_PORT;
221    cuCb.cuCfgParams.sctpParams.xnSctpInfo.localNodeType = LOCAL_NODE_TYPE;
222    numRemoteCu = &cuCb.cuCfgParams.sctpParams.xnSctpInfo.numDestNode;   
223    (*numRemoteCu) = 0;
224    while((*numRemoteCu) < NUM_XN_ASSOC)
225    {
226       /* Remote CU IP address */
227       memset(&ipv4_remote_cu, 0, sizeof(uint32_t));
228       cmInetAddr((S8*)REMOTE_IP_CU[*numRemoteCu], &ipv4_remote_cu);
229
230       /* SCTP Parameters */
231       cuCb.cuCfgParams.sctpParams.xnSctpInfo.destCb[*numRemoteCu].destIpAddr.ipV4Addr = ipv4_remote_cu;
232       cuCb.cuCfgParams.sctpParams.xnSctpInfo.destCb[*numRemoteCu].destIpAddr.ipV6Pres = false;
233       cuCb.cuCfgParams.sctpParams.xnSctpInfo.destCb[*numRemoteCu].destPort = XN_SCTP_PORT;
234
235       (*numRemoteCu)++;
236    }
237 #endif
238
239    /*PLMN*/
240    cuCb.cuCfgParams.plmn.mcc[0] = PLMN_MCC0;
241    cuCb.cuCfgParams.plmn.mcc[1] = PLMN_MCC1;
242    cuCb.cuCfgParams.plmn.mcc[2] = PLMN_MCC2;
243    cuCb.cuCfgParams.plmn.mnc[0] = PLMN_MNC0;
244    cuCb.cuCfgParams.plmn.mnc[1] = PLMN_MNC1;
245    cuCb.cuCfgParams.plmn.mnc[2] = PLMN_MNC2;
246
247    /*RRC Version*/
248    cuCb.cuCfgParams.rrcVersion.rrcVer = RRC_VER;
249    cuCb.cuCfgParams.rrcVersion.extRrcVer = EXT_RRC_VER;
250
251
252
253 } /* End of readCuCfg */
254
255 /*******************************************************************
256  *
257  * @brief Initiates inter DU handover
258  *
259  * @details
260  *
261  *    Function : initiateInterDuHandover
262  *
263  *    Functionality: Initiates the first procedure of inter-DU
264  *    handover i.eG GNB-DU configuration query to source DU
265  *
266  * @params[in] Source DU Id
267  *             Target DU Id
268  *             UE Id to be handed off 
269  * @return ROK     - success
270  *         RFAILED - failure
271  *
272  * ****************************************************************/
273 void initiateInterDuHandover(uint32_t sourceDuId, uint32_t targetDuId, uint32_t duUeF1apId)
274 {
275    uint8_t duIdx = 0;
276    DuDb *duDb = NULLP;
277    CuUeCb *ueCb = NULLP;
278
279    DU_LOG("\nINFO  --> CU_STUB: Inter-DU Handover Started for ueId [%d] from DU ID [%d] to DU ID [%d]", \
280          duUeF1apId, sourceDuId, targetDuId);
281
282    SEARCH_DU_DB(duIdx, sourceDuId, duDb); 
283    if(duDb)
284       ueCb = &duDb->ueCb[duUeF1apId-1];
285    if(ueCb)
286    {
287       ueCb->state = UE_HANDOVER_IN_PROGRESS;
288       ueCb->hoInfo.HOType = Inter_DU_HO;
289       ueCb->hoInfo.sourceId = sourceDuId;
290       ueCb->hoInfo.targetId = targetDuId;
291
292       BuildAndSendUeContextModificationReq(sourceDuId, ueCb, QUERY_CONFIG);
293    }
294    else
295    {
296       DU_LOG("\nINFO  --> CU_STUB: DU UE F1AP ID [%d] not found", duUeF1apId);
297    }
298 }
299
300
301 /*******************************************************************
302  *
303  * @brief Initiates inter CU handover
304  *
305  * @details
306  *
307  *    Function : initiateInterCuHandover
308  *
309  *    Functionality: Initiates the first procedure of inter-CU
310  *    handover
311  *
312  * @params[in] Source CU Id
313  *             Target CU Id
314  *             UE Id to be handed off 
315  * @return ROK     - success
316  *         RFAILED - failure
317  *
318  * ****************************************************************/
319 void initiateInterCuHandover(uint32_t sourceCuId, uint32_t targetCuId, uint32_t cuUeF1apId)
320 {
321     uint8_t  duIdx, ueIdx;
322     CuUeCb   *ueCb = NULLP;
323
324     DU_LOG("\nINFO  --> CU_STUB: Inter-CU Handover Started for ueId [%d] from CU ID [%d] to CU ID [%d]", \
325           cuUeF1apId, sourceCuId, targetCuId);
326
327     for(duIdx = 0; duIdx < cuCb.numDu; duIdx++)
328     {
329        for(ueIdx = 0; ueIdx < (MAX_NUM_CELL * MAX_NUM_UE); ueIdx++)
330        {
331           ueCb = &cuCb.duInfo[duIdx].ueCb[ueIdx];
332           if(cuCb.duInfo[duIdx].ueCb[ueIdx].gnbCuUeF1apId == cuUeF1apId)
333           {
334              ueCb = &cuCb.duInfo[duIdx].ueCb[ueIdx];
335              break; 
336           }
337        }
338        if(ueCb)
339           break;
340     }
341
342     if(ueCb)
343     {   
344        ueCb->state = UE_HANDOVER_IN_PROGRESS;
345        ueCb->hoInfo.HOType = Xn_Based_Inter_CU_HO;
346        ueCb->hoInfo.sourceId = sourceCuId;
347        ueCb->hoInfo.targetId = targetCuId;
348        BuildAndSendUeContextModificationReq(cuCb.duInfo[duIdx].duId, ueCb, QUERY_CONFIG);
349     }   
350     else
351     {
352        DU_LOG("\nINFO  --> CU_STUB: CU UE F1AP ID [%d] not found", cuUeF1apId);
353     }
354 }
355
356 /*******************************************************************
357  *
358  * @brief start Dl data
359  *
360  * @details
361  *
362  *    Function : startDlData
363  *
364  *    Functionality: start the downlink data
365  *
366  * @params[in] 
367  * @return ROK     - success
368  *         RFAILED - failure
369  *
370  * ****************************************************************/
371
372 uint8_t startDlData()
373 {
374    uint32_t teId = 0;
375    uint32_t duId;
376    uint8_t ret = ROK;
377    uint8_t cnt = 0;
378    int32_t totalNumOfTestFlow = 20; 
379    EgtpTeIdCb *teidCb = NULLP;
380    
381    while(totalNumOfTestFlow)
382    {
383       for(duId = 1; duId <= cuCb.cuCfgParams.egtpParams.numDu; duId++)
384       {
385          for(teId = 1; teId <= NUM_TUNNEL_TO_PUMP_DATA; teId++)
386          {
387             teidCb = NULLP;
388             cmHashListFind(&(egtpCb.dstCb[duId-1].teIdLst), (uint8_t *)&(teId), sizeof(uint32_t), 0, (PTR *)&teidCb);
389             if(teidCb)
390             {
391                cnt =0;
392                DU_LOG("\nDEBUG  -->  EGTP: Sending DL User Data(duId %d, teId:%d)\n", duId, teId);
393                while(cnt < NUM_DL_PACKETS)
394                {
395                   ret =  cuEgtpDatReq(duId, teId);      
396                   if(ret != ROK)
397                   {
398                      DU_LOG("\nERROR --> EGTP: Issue with teid=%d\n",teId);
399                      break;
400                   }
401                   /* TODO : sleep(1) will be removed later once we will be able to
402                    * support the continuous data pack transfer */
403                   sleep(1);
404                   cnt++;
405                }
406             }
407             else
408             {
409                DU_LOG("\nDEBUG  -->  EGTP: TunnelId Not Found for (duId %d, teId:%d)\n", duId, teId);
410             }
411          }
412       }
413       totalNumOfTestFlow--;
414    }
415    
416    return ROK;
417 }
418
419 /*******************************************************************
420  *
421  * @brief Handles Console input
422  *
423  * @details
424  *
425  *    Function : cuConsoleHandler
426  *
427  *    Functionality: Handles Console input
428  *
429  * @params[in] 
430  * @return ROK     - success
431  *         RFAILED - failure
432  *
433  * ****************************************************************/
434 void *cuConsoleHandler(void *args)
435 {
436    char ch;
437
438    while(true) 
439    {
440       ch = getchar();
441       /* Send DL user data to CU when user enters 'd' on console */
442       if(ch == 'd')
443       {
444
445          /* Change #if 0 to #if 1 to take input from user */
446 #if 0
447          DU_LOG("\n EGTP --> : Enter TEID id(1..10) where DL Data to be sent\n");
448          scanf("%d",&teId);
449
450          if(teId > MAX_TEID || teId < MIN_TEID)
451          {
452             DU_LOG("\nERROR  -->  EGTP : TEID(%x) OUT Of Range",teId);
453             printf("\n");
454             continue;
455          }
456          /* Start Pumping data from CU to DU */
457          DU_LOG("\nDEBUG  -->  EGTP: Sending DL User Data(teId:%d)\n",teId);
458
459          cnt =0;
460          while(cnt < NUM_DL_PACKETS)
461          {
462             ret =  cuEgtpDatReq(teId);
463             if(ret != ROK)
464             {
465                DU_LOG("\nERROR --> EGTP: Issue with teid=%d\n",teId);
466                break;
467             }
468             cnt++;
469          }
470 #else
471
472          /* This variable is taken for sending specific number of downlink data packet. 
473           * Presently the code is supporting total 4500 data packets trasfer for 3 UEs only with sleep(1).
474           * If you wants to pump data for 3 UE change the following macro values
475           * NUM_TUNNEL_TO_PUMP_DATA = 9, NUM_DL_PACKETS = 1.
476           * totalDataPacket = totalNumOfTestFlow * NUM_TUNNEL_TO_PUMP_DATA * NUM_DL_PACKETS 
477           * totalDataPacket = [500*9*1] */
478          
479          startDlData();
480 #endif
481          continue;
482       } 
483
484       /* Start Handover procedure towards DU when 'h' is received from console input */
485       else if(ch == 'h')
486       {
487          HandoverType hoType;
488          uint32_t sourceId, targetId, ueId;
489
490          DU_LOG("\n\nChoose the type of handover to initiate : \nEnter 1 for Inter-CU Handover over Xn interface\nEnter 2 for Inter-DU Handover\n");
491          scanf("%d", &hoType);
492
493          if(hoType == Xn_Based_Inter_CU_HO)
494          {
495             DU_LOG("\nEnter Source CU ID for Inter-CU Handover : ");
496             scanf("%d", &sourceId);
497             DU_LOG("\nEnter Target CU ID for Inter-CU Handover : ");
498             scanf("%d", &targetId);
499             DU_LOG("\nEnter CU UE F1AP ID to be handed over : ");
500             scanf("%d", &ueId);
501
502             initiateInterCuHandover(sourceId, targetId, ueId);
503          }
504          else if(hoType == Inter_DU_HO)
505          {
506             DU_LOG("\nEnter Source DU ID for Inter-DU Handover : ");
507             scanf("%d", &sourceId);
508             DU_LOG("\nEnter Target DU ID for Inter-DU Handover : ");
509             scanf("%d", &targetId);
510             DU_LOG("\nEnter DU UE F1AP ID to be handed over : ");
511             scanf("%d", &ueId);
512
513             initiateInterDuHandover(sourceId, targetId, ueId);
514          }
515       }
516       /* Start Idle mode paging when 'p' is received from console input */
517       else if(ch == 'p')
518       {
519          uint64_t sTmsi = 0;
520          uint8_t duId = 0;
521
522          DU_LOG("\nEnter DU ID on which this UE to be pagged");
523          scanf("%d", &duId);
524          DU_LOG("\nEnter 5g-S-TMSI");
525          scanf("%lu", &sTmsi);
526
527          if(BuildAndSendPagingMsg(sTmsi, duId) != ROK)
528          {
529             DU_LOG("\nERROR --> EGTP: Failed to build and send paging message for 5gsTmsi[%lu]\n", sTmsi);   
530          }
531          continue;
532       }
533       else if(ch == 'm')
534       {
535          uint8_t  ueId  = 1;
536          uint8_t  duId  = 0;
537          uint8_t  duIdx = 0;
538          DuDb    *duDb  = NULLP;
539          CuUeCb  *ueCb  = NULLP;
540
541          DU_LOG("\nEnter DU ID whose UE has to be modified");
542          scanf("%d", &duId);
543          DU_LOG("\nEnter UE ID to be modified");
544          scanf("%d", &ueId);
545
546          DU_LOG("\nINFO  --> CU_STUB: UE Context Mod for ueId [%d] at DU ID [%d]", \
547                   ueId, duId);
548
549          SEARCH_DU_DB(duIdx, duId, duDb); 
550          if(duDb)
551          {
552             ueCb = &duDb->ueCb[ueId-1];
553             BuildAndSendUeContextModificationReq(duId, ueCb, MODIFY_UE);
554          }
555          else
556          {
557             DU_LOG("ERROR --> DuDb is NULLP");
558          }
559          continue;
560
561       }
562       /*UE context release command from CU*/
563       else if(ch == 'c')
564       {
565          uint32_t duId, cuUeF1apId, duUeF1apId;
566          uint8_t  duIdx = 0;
567          DuDb    *duDb  = NULLP;
568          
569          DU_LOG("\nEnter DU ID on which UE has to be released");
570          scanf("%d", &duId);
571          DU_LOG("\nEnter UE ID to be released");
572          scanf("%d", &duUeF1apId);
573         
574          SEARCH_DU_DB(duIdx, duId, duDb); 
575          if(duDb)
576          {
577             if(duDb->ueCb[duUeF1apId-1].gnbDuUeF1apId == duUeF1apId)
578             {
579                cuUeF1apId = duDb->ueCb[duUeF1apId-1].gnbCuUeF1apId;
580                DU_LOG("INFO   -->  CU_STUB: Cu UeId: %d Du UeId:%d",cuUeF1apId, duUeF1apId);
581                BuildAndSendUeContextReleaseCommand(duId, cuUeF1apId, duUeF1apId);
582             }
583             else
584             {
585                DU_LOG("ERROR  -->  CU_STUB: Du UeId:%d in UeCb mismatch",\
586                         duDb->ueCb[duUeF1apId-1].gnbDuUeF1apId);
587             }
588          }
589
590          sleep(5);
591          continue;
592       }
593    }
594 }
595 /**********************************************************************
596          End of file
597 **********************************************************************/