Merge "[Epic-ID: ODUHIGH-405][Task-ID: ODUHIGH-420] GNB-DU Configuration Query and...
[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 Fetches pointer to DU Database
39  *
40  * @details
41  *
42  *    Function : getDuDb
43  *
44  *    Functionality:
45  *      Searches and returns pointer to DU structure based on DU Id
46  *
47  * @params[in] DU Id
48  * @return Pointer to DU Db
49  *
50  ******************************************************************/
51 DuDb* getDuDb(uint32_t duId)
52 {
53    uint8_t duIdx;
54    for(duIdx=0; duIdx < cuCb.numDu; duIdx++)
55    {
56       if(cuCb.duInfo[duIdx].duId == duId)
57          return (&cuCb.duInfo[duIdx]);
58    }
59    return NULLP;
60 }
61
62 /*******************************************************************
63  *
64  * @brief Fetches pointer to Cell Cb
65  *
66  * @details
67  *
68  *    Function : getCellCb
69  *
70  *    Functionality:
71  *       Searches for a cell within a DU based on NR cell Id
72  *       Returns pointer to this cell Cb structure
73  *
74  * @params[in] Pointer to DU Db
75  *             NR Cell ID
76  * @return Pointer to cell Cb
77  *
78  ******************************************************************/
79 CuCellCb* getCellCb(DuDb *duDb, uint32_t cellId)
80 {
81    uint8_t cellIdx;
82    for(cellIdx=0; cellIdx < duDb->numCells; cellIdx++)
83    {
84       if(duDb->cellCb[cellIdx].nrCellId == cellId)
85          return &(duDb->cellCb[cellIdx]);
86    }
87    return NULLP;
88 }
89
90 /*******************************************************************
91  *
92  * @brief Handles SCTP notification 
93  *
94  * @details
95  *
96  *    Function : sctpNtfyInd
97  *
98  *    Functionality:
99  *         Handles SCTP notification
100  *
101  * @params[in] sctp notification
102  * @return void
103  *
104  ******************************************************************/
105 void sctpNtfyInd(CmInetSctpNotification *ntfy)
106 {
107 //TODO
108 }
109
110 void init_log()
111 {
112     openlog("CU_STUB",LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
113 }
114 /*******************************************************************
115  *
116  * @brief Main function of CU APP
117  *
118  * @details
119  *
120  *    Function : main
121  *
122  *    Functionality:
123  *         - Reads CU related configurations
124  *         - Initialize SCTP Parameters
125  *         - Start SCTP receiver thread
126  *
127  * @params[in] 
128  * @return ROK     - success
129  *         RFAILED - failure
130  *
131  * ****************************************************************/
132
133 uint8_t tst()
134 {
135    int retVal=0;
136    pthread_t conThrdId;
137    pthread_attr_t attr;
138
139    init_log();   
140    DU_LOG("\nINFO   -->  CU_STUB : Starting CU_STUB\n");
141
142    /* Start thread to receive console input */
143    pthread_attr_init(&attr);
144    pthread_attr_setstacksize(&attr, (size_t)NULLD);
145    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
146    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
147    retVal = pthread_create(&conThrdId, &attr, cuConsoleHandler, NULLP);
148    if(retVal != 0)
149    {
150       DU_LOG("\nERROR  -->  CU_STUB :  Thread creation failed. Cause %d", retVal);
151    }
152    pthread_attr_destroy(&attr);
153
154    /* Read CU configurations */
155    readCuCfg();
156
157    /* Start CU-EGTP */
158    egtpActvInit();
159    egtpInitReq();
160
161    /* Initializing SCTP global parameters */
162    sctpActvInit();
163  
164    /* Start CU-SCTP to listen on incoming connection */
165    sctpCfgReq();
166    sctpStartReq();
167
168    return ROK;
169 }
170
171 /*******************************************************************
172  *
173  * @brief Read CU related configuration
174  *
175  * @details
176  *
177  *    Function : readCuCfg
178  *
179  *    Functionality:
180  *            - RRead CU related configuration
181  *
182  * @params[in] 
183  * @return ROK     - success
184  *         RFAILED - failure
185  *
186  * ****************************************************************/
187
188 void readCuCfg()
189 {
190    uint8_t  *numDu;
191    uint32_t ipv4_du, ipv4_cu;
192
193    DU_LOG("\nDEBUG  -->  CU_STUB : Reading CU configurations");
194
195    cuCb.cuCfgParams.cuId = CU_ID;
196    strcpy(cuCb.cuCfgParams.cuName, CU_NAME);
197
198 #ifdef O1_ENABLE
199    if( getStartupConfigForStub(&g_cfg) != ROK )
200    {
201       DU_LOG("\nError  -->  CU_STUB : Could not fetch startup "\
202              "configurations from Netconf interface\n");
203       exit(1);
204    }
205    
206    cmInetAddr((S8*)g_cfg.DU_IPV4_Addr, &ipv4_du);
207    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].duIpAddr.ipV4Addr = ipv4_du;
208    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].duIpAddr.ipV6Pres = false;
209    
210    cmInetAddr((S8*)g_cfg.CU_IPV4_Addr, &ipv4_cu);
211    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].cuIpAddr.ipV4Addr = ipv4_cu;
212    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].cuIpAddr.ipV6Pres = false;
213    
214    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].duPort = g_cfg.DU_Port;
215    cuCb.cuCfgParams.sctpParams.sctpAssoc[0].cuPort = g_cfg.CU_Port; 
216    cuCb.cuCfgParams.sctpParams.numDu = 1;
217 #else
218    cuCb.cuCfgParams.sctpParams.numDu = 0;
219    numDu = &cuCb.cuCfgParams.sctpParams.numDu;
220    while(*numDu < MAX_DU_SUPPORTED)
221    {
222       /* DU IP Address and Port*/
223       memset(&ipv4_du, 0, sizeof(uint32_t));
224       cmInetAddr((S8*)DU_IP_V4_ADDR[*numDu], &ipv4_du);
225       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].duIpAddr.ipV4Addr = ipv4_du;
226       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].duIpAddr.ipV6Pres = false;
227       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].duPort = DU_SCTP_PORT[*numDu];
228
229       /* CU IP Address and Port*/
230       memset(&ipv4_du, 0, sizeof(uint32_t));
231       cmInetAddr((S8*)CU_IP_V4_ADDR, &ipv4_cu);
232       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].cuIpAddr.ipV4Addr = ipv4_cu;
233       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].cuIpAddr.ipV6Pres = false;
234       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].cuPort = CU_SCTP_PORT_TO_DU[*numDu];
235       (*numDu)++;
236    }
237
238 #endif
239
240    /*PLMN*/
241    cuCb.cuCfgParams.plmn.mcc[0] = PLMN_MCC0;
242    cuCb.cuCfgParams.plmn.mcc[1] = PLMN_MCC1;
243    cuCb.cuCfgParams.plmn.mcc[2] = PLMN_MCC2;
244    cuCb.cuCfgParams.plmn.mnc[0] = PLMN_MNC0;
245    cuCb.cuCfgParams.plmn.mnc[1] = PLMN_MNC1;
246    cuCb.cuCfgParams.plmn.mnc[2] = PLMN_MNC2;
247
248    /*RRC Version*/
249    cuCb.cuCfgParams.rrcVersion.rrcVer = RRC_VER;
250    cuCb.cuCfgParams.rrcVersion.extRrcVer = EXT_RRC_VER;
251
252
253    /* EGTP Parameters */
254    cuCb.cuCfgParams.egtpParams.localIp.ipV4Pres = TRUE;
255    cuCb.cuCfgParams.egtpParams.localIp.ipV4Addr = ipv4_cu;
256    cuCb.cuCfgParams.egtpParams.localPort = CU_EGTP_PORT;
257    cuCb.cuCfgParams.egtpParams.destIp.ipV4Pres = TRUE;
258    cuCb.cuCfgParams.egtpParams.destIp.ipV4Addr = ipv4_du;
259    cuCb.cuCfgParams.egtpParams.destPort = DU_EGTP_PORT;
260    cuCb.cuCfgParams.egtpParams.minTunnelId = MIN_TEID;
261    cuCb.cuCfgParams.egtpParams.currTunnelId = cuCb.cuCfgParams.egtpParams.minTunnelId;
262    cuCb.cuCfgParams.egtpParams.maxTunnelId = MAX_TEID;
263
264 } /* End of readCuCfg */
265
266 /*******************************************************************
267  *
268  * @brief Initiates inter DU handover
269  *
270  * @details
271  *
272  *    Function : initiateInterDuHandover
273  *
274  *    Functionality: Initiates the first procedure of inter-DU
275  *    handover i.eG GNB-DU configuration query to source DU
276  *
277  * @params[in] Source DU Id
278  *             Target DU Id
279  *             UE Id to be handed off 
280  * @return ROK     - success
281  *         RFAILED - failure
282  *
283  * ****************************************************************/
284 void initiateInterDuHandover(uint32_t sourceDuId, uint32_t targetDuId, uint32_t ueId)
285 {
286     DuDb *duDb = NULLP;
287     CuUeCb *ueCb = NULLP;
288
289     duDb = getDuDb(sourceDuId);
290     if(duDb)
291        ueCb = &duDb->ueCb[ueId-1];
292     if(ueCb)
293        ueCb->state = HANDOVER_IN_PROGRESS;
294
295     DU_LOG("\nINFO  --> CU_STUB: Inter-DU Handover Started for ueId [%d] from DU ID [%d] to DU ID [%d]", \
296           ueId, sourceDuId, targetDuId);
297     BuildAndSendUeContextModificationReq(sourceDuId, ueCb, QUERY_CONFIG);
298 }
299
300 /*******************************************************************
301  *
302  * @brief Handles Console input
303  *
304  * @details
305  *
306  *    Function : cuConsoleHandler
307  *
308  *    Functionality: Handles Console input
309  *
310  * @params[in] 
311  * @return ROK     - success
312  *         RFAILED - failure
313  *
314  * ****************************************************************/
315 void *cuConsoleHandler(void *args)
316 {
317    char ch;
318    uint8_t teId = 0;
319    uint8_t ret = ROK;
320    uint8_t cnt = 0;
321
322    /* This variable is taken for sending specific number of downlink data packet. 
323     * Presently the code is supporting total 4500 data packets trasfer for 3 UEs only with sleep(1).
324     * If you wants to pump data for 3 UE change the following macro values
325     * NUM_TUNNEL_TO_PUMP_DATA = 9, NUM_DL_PACKETS = 1.
326     * totalDataPacket = totalNumOfTestFlow * NUM_TUNNEL_TO_PUMP_DATA * NUM_DL_PACKETS 
327     * totalDataPacket = [500*9*1] */
328    int32_t totalNumOfTestFlow = 500; 
329
330    while(true) 
331    {
332       ch = getchar();
333       /* Send DL user data to CU when user enters 'd' on console */
334       if(ch == 'd')
335       {
336
337       /* Change #if 0 to #if 1 to take input from user */
338 #if 0
339          DU_LOG("\n EGTP --> : Enter TEID id(1..10) where DL Data to be sent\n");
340          scanf("%d",&teId);
341          
342          if(teId > MAX_TEID || teId < MIN_TEID)
343          {
344             DU_LOG("\nERROR  -->  EGTP : TEID(%x) OUT Of Range",teId);
345             printf("\n");
346             continue;
347          }
348          /* Start Pumping data from CU to DU */
349          DU_LOG("\nDEBUG  -->  EGTP: Sending DL User Data(teId:%d)\n",teId);
350
351          cnt =0;
352          while(cnt < NUM_DL_PACKETS)
353          {
354             ret =  cuEgtpDatReq(teId);
355             if(ret != ROK)
356             {
357                DU_LOG("\nERROR --> EGTP: Issue with teid=%d\n",teId);
358                break;
359             }
360             cnt++;
361          }
362 #else
363          while(totalNumOfTestFlow)
364          {
365             for(teId = 1; teId <= NUM_TUNNEL_TO_PUMP_DATA; teId++)
366             {
367                DU_LOG("\nDEBUG  -->  EGTP: Sending DL User Data(teId:%d)\n",teId);
368                cnt =0;
369                while(cnt < NUM_DL_PACKETS)
370                {
371                   ret =  cuEgtpDatReq(teId);      
372                   if(ret != ROK)
373                   {
374                      DU_LOG("\nERROR --> EGTP: Issue with teid=%d\n",teId);
375                      break;
376                   }
377                   /* TODO : sleep(1) will be removed later once we will be able to
378                    * support the continuous data pack transfer */
379                   sleep(1);
380                   cnt++;
381                }
382             }
383             totalNumOfTestFlow--;
384          }
385 #endif
386          continue;
387       } 
388
389       /* Start Handover procedure towards DU when 'h' is received from console input */
390       else if(ch == 'h')
391       {
392          uint32_t sourceDuId, targetDuId, ueId;
393
394          DU_LOG("\nEnter Source DU ID for Inter-DU Handover");
395          scanf("%d", &sourceDuId);
396          DU_LOG("\nEnter Target DU ID for Inter-DU Handover");
397          scanf("%d", &targetDuId);
398          DU_LOG("\nEnter DU UE F1AP ID to be handed over");
399          scanf("%d", &ueId);
400
401          initiateInterDuHandover(sourceDuId, targetDuId, ueId);
402       }
403    }
404 }
405 /**********************************************************************
406          End of file
407 **********************************************************************/