a131ce55f4ad80b732dbe8bcc38527887375c25c
[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;
137    uint32_t ipv4_du, ipv4_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.sctpAssoc[0].duIpAddr.ipV4Addr = ipv4_du;
154    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].duIpAddr.ipV6Pres = false;
155    
156    cmInetAddr((S8*)g_cfg.CU_IPV4_Addr, &ipv4_cu);
157    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].cuIpAddr.ipV4Addr = ipv4_cu;
158    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].cuIpAddr.ipV6Pres = false;
159    
160    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].duPort = g_cfg.DU_Port;
161    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].cuPort = g_cfg.CU_Port; 
162    cuCb.cuCfgParams.sctpParams.numDu = 1;
163    
164    cuCb.cuCfgParams.egtpParams.egtpAssoc[0].localIp.ipV4Pres = TRUE;
165    cuCb.cuCfgParams.egtpParams.egtpAssoc[0].localIp.ipV4Addr = ipv4_cu;
166    cuCb.cuCfgParams.egtpParams.egtpAssoc[0].localPort = CU_EGTP_PORT[0];
167    cuCb.cuCfgParams.egtpParams.egtpAssoc[0].destIp.ipV4Pres = TRUE;
168    cuCb.cuCfgParams.egtpParams.egtpAssoc[0].destIp.ipV4Addr = ipv4_du;
169    cuCb.cuCfgParams.egtpParams.egtpAssoc[0].destPort = DU_EGTP_PORT[0];
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    cuCb.cuCfgParams.sctpParams.numDu = 0;
177    cuCb.cuCfgParams.egtpParams.numDu = 0;
178    numDu = 0;
179    while(numDu < MAX_DU_SUPPORTED)
180    {
181       /* DU IP Address and Port*/
182       memset(&ipv4_du, 0, sizeof(uint32_t));
183       cmInetAddr((S8*)DU_IP_V4_ADDR[numDu], &ipv4_du);
184       cuCb.cuCfgParams.sctpParams.sctpAssoc[numDu].duIpAddr.ipV4Addr = ipv4_du;
185       cuCb.cuCfgParams.sctpParams.sctpAssoc[numDu].duIpAddr.ipV6Pres = false;
186       cuCb.cuCfgParams.sctpParams.sctpAssoc[numDu].duPort = DU_SCTP_PORT[numDu];
187
188       /* CU IP Address and Port*/
189       memset(&ipv4_du, 0, sizeof(uint32_t));
190       cmInetAddr((S8*)CU_IP_V4_ADDR, &ipv4_cu);
191       cuCb.cuCfgParams.sctpParams.sctpAssoc[numDu].cuIpAddr.ipV4Addr = ipv4_cu;
192       cuCb.cuCfgParams.sctpParams.sctpAssoc[numDu].cuIpAddr.ipV6Pres = false;
193       cuCb.cuCfgParams.sctpParams.sctpAssoc[numDu].cuPort = CU_SCTP_PORT_TO_DU[numDu];
194
195       /* EGTP Parameters */
196       memset(&ipv4_du, 0, sizeof(uint32_t));
197       cmInetAddr((S8*)DU_IP_V4_ADDR[numDu], &ipv4_du);
198       cuCb.cuCfgParams.egtpParams.egtpAssoc[numDu].localIp.ipV4Pres = TRUE;
199       cuCb.cuCfgParams.egtpParams.egtpAssoc[numDu].localIp.ipV4Addr = ipv4_cu;
200       cuCb.cuCfgParams.egtpParams.egtpAssoc[numDu].localPort = CU_EGTP_PORT[numDu];
201       cuCb.cuCfgParams.egtpParams.egtpAssoc[numDu].destIp.ipV4Pres = TRUE;
202       cuCb.cuCfgParams.egtpParams.egtpAssoc[numDu].destIp.ipV4Addr = ipv4_du;
203       cuCb.cuCfgParams.egtpParams.egtpAssoc[numDu].destPort = DU_EGTP_PORT[numDu];
204       
205       (numDu)++;
206    }
207
208    cuCb.cuCfgParams.egtpParams.minTunnelId = MIN_TEID;
209    cuCb.cuCfgParams.egtpParams.currTunnelId = cuCb.cuCfgParams.egtpParams.minTunnelId;
210    cuCb.cuCfgParams.egtpParams.maxTunnelId = MAX_TEID;
211    cuCb.cuCfgParams.egtpParams.numDu = numDu;
212    cuCb.cuCfgParams.sctpParams.numDu = numDu;
213 #endif
214
215    /*PLMN*/
216    cuCb.cuCfgParams.plmn.mcc[0] = PLMN_MCC0;
217    cuCb.cuCfgParams.plmn.mcc[1] = PLMN_MCC1;
218    cuCb.cuCfgParams.plmn.mcc[2] = PLMN_MCC2;
219    cuCb.cuCfgParams.plmn.mnc[0] = PLMN_MNC0;
220    cuCb.cuCfgParams.plmn.mnc[1] = PLMN_MNC1;
221    cuCb.cuCfgParams.plmn.mnc[2] = PLMN_MNC2;
222
223    /*RRC Version*/
224    cuCb.cuCfgParams.rrcVersion.rrcVer = RRC_VER;
225    cuCb.cuCfgParams.rrcVersion.extRrcVer = EXT_RRC_VER;
226
227
228
229 } /* End of readCuCfg */
230
231 /*******************************************************************
232  *
233  * @brief Initiates inter DU handover
234  *
235  * @details
236  *
237  *    Function : initiateInterDuHandover
238  *
239  *    Functionality: Initiates the first procedure of inter-DU
240  *    handover i.eG GNB-DU configuration query to source DU
241  *
242  * @params[in] Source DU Id
243  *             Target DU Id
244  *             UE Id to be handed off 
245  * @return ROK     - success
246  *         RFAILED - failure
247  *
248  * ****************************************************************/
249 void initiateInterDuHandover(uint32_t sourceDuId, uint32_t targetDuId, uint32_t ueId)
250 {
251     uint8_t duIdx = 0;
252     DuDb *duDb = NULLP;
253     CuUeCb *ueCb = NULLP;
254    
255     SEARCH_DU_DB(duIdx, sourceDuId, duDb); 
256     if(duDb)
257        ueCb = &duDb->ueCb[ueId-1];
258     if(ueCb)
259     {
260        ueCb->state = UE_HANDOVER_IN_PROGRESS;
261        ueCb->hoInfo.sourceDuId = sourceDuId;
262        ueCb->hoInfo.targetDuId = targetDuId;
263     }
264
265     DU_LOG("\nINFO  --> CU_STUB: Inter-DU Handover Started for ueId [%d] from DU ID [%d] to DU ID [%d]", \
266           ueId, sourceDuId, targetDuId);
267     BuildAndSendUeContextModificationReq(sourceDuId, ueCb, QUERY_CONFIG);
268 }
269
270 /*******************************************************************
271  *
272  * @brief start Dl data
273  *
274  * @details
275  *
276  *    Function : startDlData
277  *
278  *    Functionality: start the downlink data
279  *
280  * @params[in] 
281  * @return ROK     - success
282  *         RFAILED - failure
283  *
284  * ****************************************************************/
285
286 uint8_t startDlData()
287 {
288    uint32_t teId = 0;
289    uint32_t duId;
290    uint8_t ret = ROK;
291    uint8_t cnt = 0;
292    int32_t totalNumOfTestFlow = 20; 
293    EgtpTeIdCb *teidCb = NULLP;
294    
295    while(totalNumOfTestFlow)
296    {
297       for(duId = 1; duId<=MAX_DU_SUPPORTED; duId++)
298       {
299          for(teId = 1; teId <= NUM_TUNNEL_TO_PUMP_DATA; teId++)
300          {
301             teidCb = NULLP;
302             cmHashListFind(&(egtpCb.dstCb[duId-1].teIdLst), (uint8_t *)&(teId), sizeof(uint32_t), 0, (PTR *)&teidCb);
303             if(teidCb)
304             {
305                cnt =0;
306                DU_LOG("\nDEBUG  -->  EGTP: Sending DL User Data(duId %d, teId:%d)\n", duId, teId);
307                while(cnt < NUM_DL_PACKETS)
308                {
309                   ret =  cuEgtpDatReq(duId, teId);      
310                   if(ret != ROK)
311                   {
312                      DU_LOG("\nERROR --> EGTP: Issue with teid=%d\n",teId);
313                      break;
314                   }
315                   /* TODO : sleep(1) will be removed later once we will be able to
316                    * support the continuous data pack transfer */
317                   sleep(1);
318                   cnt++;
319                }
320             }
321             else
322             {
323                DU_LOG("\nDEBUG  -->  EGTP: TunnelId Not Found for (duId %d, teId:%d)\n", duId, teId);
324             }
325          }
326       }
327       totalNumOfTestFlow--;
328    }
329    
330    return ROK;
331 }
332
333 /*******************************************************************
334  *
335  * @brief Handles Console input
336  *
337  * @details
338  *
339  *    Function : cuConsoleHandler
340  *
341  *    Functionality: Handles Console input
342  *
343  * @params[in] 
344  * @return ROK     - success
345  *         RFAILED - failure
346  *
347  * ****************************************************************/
348 void *cuConsoleHandler(void *args)
349 {
350    char ch;
351
352    while(true) 
353    {
354       ch = getchar();
355       /* Send DL user data to CU when user enters 'd' on console */
356       if(ch == 'd')
357       {
358
359          /* Change #if 0 to #if 1 to take input from user */
360 #if 0
361          DU_LOG("\n EGTP --> : Enter TEID id(1..10) where DL Data to be sent\n");
362          scanf("%d",&teId);
363
364          if(teId > MAX_TEID || teId < MIN_TEID)
365          {
366             DU_LOG("\nERROR  -->  EGTP : TEID(%x) OUT Of Range",teId);
367             printf("\n");
368             continue;
369          }
370          /* Start Pumping data from CU to DU */
371          DU_LOG("\nDEBUG  -->  EGTP: Sending DL User Data(teId:%d)\n",teId);
372
373          cnt =0;
374          while(cnt < NUM_DL_PACKETS)
375          {
376             ret =  cuEgtpDatReq(teId);
377             if(ret != ROK)
378             {
379                DU_LOG("\nERROR --> EGTP: Issue with teid=%d\n",teId);
380                break;
381             }
382             cnt++;
383          }
384 #else
385
386          /* This variable is taken for sending specific number of downlink data packet. 
387           * Presently the code is supporting total 4500 data packets trasfer for 3 UEs only with sleep(1).
388           * If you wants to pump data for 3 UE change the following macro values
389           * NUM_TUNNEL_TO_PUMP_DATA = 9, NUM_DL_PACKETS = 1.
390           * totalDataPacket = totalNumOfTestFlow * NUM_TUNNEL_TO_PUMP_DATA * NUM_DL_PACKETS 
391           * totalDataPacket = [500*9*1] */
392          
393          startDlData();
394 #endif
395          continue;
396       } 
397
398       /* Start Handover procedure towards DU when 'h' is received from console input */
399       else if(ch == 'h')
400       {
401          uint32_t sourceDuId, targetDuId, ueId;
402
403          DU_LOG("\nEnter Source DU ID for Inter-DU Handover");
404          scanf("%d", &sourceDuId);
405          DU_LOG("\nEnter Target DU ID for Inter-DU Handover");
406          scanf("%d", &targetDuId);
407          DU_LOG("\nEnter DU UE F1AP ID to be handed over");
408          scanf("%d", &ueId);
409
410          initiateInterDuHandover(sourceDuId, targetDuId, ueId);
411       }
412       /* Start Idle mode paging when 'p' is received from console input */
413       else if(ch == 'p')
414       {
415          uint64_t sTmsi = 0;
416          uint8_t duId = 0;
417
418          DU_LOG("\nEnter DU ID on which this UE to be pagged");
419          scanf("%d", &duId);
420          DU_LOG("\nEnter 5g-S-TMSI");
421          scanf("%lu", &sTmsi);
422
423          if(BuildAndSendPagingMsg(sTmsi, duId) != ROK)
424          {
425             DU_LOG("\nERROR --> EGTP: Failed to build and send paging message for 5gsTmsi[%lu]\n", sTmsi);   
426          }
427          continue;
428       }
429       else if(ch == 'm')
430       {
431          uint8_t ueId = 1;
432          uint8_t duId = 1;
433          uint8_t duIdx = 0;
434
435          DU_LOG("\nINFO  --> CU_STUB: UE Context Mod for ueId [%d] at DU ID [%d]", \
436                   ueId, duId );
437          DuDb *duDb = NULLP;
438          CuUeCb *ueCb = NULLP;
439
440          SEARCH_DU_DB(duIdx, duId, duDb); 
441          if(duDb)
442             ueCb = &duDb->ueCb[ueId-1];
443          BuildAndSendUeContextModificationReq(duId, ueCb, MODIFY_UE);
444
445       }
446    }
447 }
448 /**********************************************************************
449          End of file
450 **********************************************************************/