[Epic-ID: ODUHIGH-405][Task-ID: ODUHIGH-414] F1 Setup from multiple DU to one CU
[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 "cu_stub.h"
24
25 #ifdef O1_ENABLE
26 #include "CmInterface.h"
27 #endif
28
29
30 #ifdef O1_ENABLE
31
32 extern StartupConfig g_cfg;
33
34 #endif
35
36
37 /*******************************************************************
38  *
39  * @brief Handles SCTP notification 
40  *
41  * @details
42  *
43  *    Function : sctpNtfyInd
44  *
45  *    Functionality:
46  *         Handles SCTP notification
47  *
48  * @params[in] sctp notification
49  * @return void
50  *
51  ******************************************************************/
52 void sctpNtfyInd(CmInetSctpNotification *ntfy)
53 {
54 //TODO
55 }
56
57 void init_log()
58 {
59     openlog("CU_STUB",LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
60 }
61 /*******************************************************************
62  *
63  * @brief Main function of CU APP
64  *
65  * @details
66  *
67  *    Function : main
68  *
69  *    Functionality:
70  *         - Reads CU related configurations
71  *         - Initialize SCTP Parameters
72  *         - Start SCTP receiver thread
73  *
74  * @params[in] 
75  * @return ROK     - success
76  *         RFAILED - failure
77  *
78  * ****************************************************************/
79
80 uint8_t tst()
81 {
82    int retVal=0;
83    pthread_t conThrdId;
84    pthread_attr_t attr;
85
86    init_log();   
87    DU_LOG("\nINFO   -->  CU_STUB : Starting CU_STUB\n");
88
89    /* Start thread to receive console input */
90    pthread_attr_init(&attr);
91    pthread_attr_setstacksize(&attr, (size_t)NULLD);
92    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
93    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
94    retVal = pthread_create(&conThrdId, &attr, cuConsoleHandler, NULLP);
95    if(retVal != 0)
96    {
97       DU_LOG("\nERROR  -->  CU_STUB :  Thread creation failed. Cause %d", retVal);
98    }
99    pthread_attr_destroy(&attr);
100
101    /* Read CU configurations */
102    readCuCfg();
103
104    /* Start CU-EGTP */
105    egtpActvInit();
106    egtpInitReq();
107
108    /* Initializing SCTP global parameters */
109    sctpActvInit();
110  
111    /* Start CU-SCTP to listen on incoming connection */
112    sctpCfgReq();
113    sctpStartReq();
114
115    return ROK;
116 }
117
118 /*******************************************************************
119  *
120  * @brief Read CU related configuration
121  *
122  * @details
123  *
124  *    Function : readCuCfg
125  *
126  *    Functionality:
127  *            - RRead CU related configuration
128  *
129  * @params[in] 
130  * @return ROK     - success
131  *         RFAILED - failure
132  *
133  * ****************************************************************/
134
135 void readCuCfg()
136 {
137    uint8_t  *numDu;
138    uint32_t ipv4_du, ipv4_cu;
139
140    DU_LOG("\nDEBUG  -->  CU_STUB : Reading CU configurations");
141
142    cuCb.cuCfgParams.cuId = CU_ID;
143    strcpy(cuCb.cuCfgParams.cuName, CU_NAME);
144
145 #ifdef O1_ENABLE
146    if( getStartupConfigForStub(&g_cfg) != ROK )
147    {
148       DU_LOG("\nError  -->  CU_STUB : Could not fetch startup "\
149              "configurations from Netconf interface\n");
150       exit(1);
151    }
152    
153    cmInetAddr((S8*)g_cfg.DU_IPV4_Addr, &ipv4_du);
154    cmInetAddr((S8*)g_cfg.CU_IPV4_Addr, &ipv4_cu);
155
156    cuCb.cuCfgParams.sctpParams.duPort = g_cfg.DU_Port;
157    cuCb.cuCfgParams.sctpParams.cuPort = g_cfg.CU_Port;
158 #else
159    cuCb.cuCfgParams.sctpParams.numDu = 0;
160    numDu = &cuCb.cuCfgParams.sctpParams.numDu;
161    while(*numDu < MAX_DU_SUPPORTED)
162    {
163       /* DU IP Address and Port*/
164       memset(&ipv4_du, 0, sizeof(uint32_t));
165       cmInetAddr((S8*)DU_IP_V4_ADDR[*numDu], &ipv4_du);
166       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].duIpAddr.ipV4Addr = ipv4_du;
167       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].duIpAddr.ipV6Pres = false;
168       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].duPort = DU_SCTP_PORT[*numDu];
169
170       /* CU IP Address and Port*/
171       memset(&ipv4_du, 0, sizeof(uint32_t));
172       cmInetAddr((S8*)CU_IP_V4_ADDR, &ipv4_cu);
173       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].cuIpAddr.ipV4Addr = ipv4_cu;
174       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].cuIpAddr.ipV6Pres = false;
175       cuCb.cuCfgParams.sctpParams.sctpAssoc[*numDu].cuPort = CU_SCTP_PORT_TO_DU[*numDu];
176       (*numDu)++;
177    }
178
179 #endif
180
181    /*PLMN*/
182    cuCb.cuCfgParams.plmn.mcc[0] = PLMN_MCC0;
183    cuCb.cuCfgParams.plmn.mcc[1] = PLMN_MCC1;
184    cuCb.cuCfgParams.plmn.mcc[2] = PLMN_MCC2;
185    cuCb.cuCfgParams.plmn.mnc[0] = PLMN_MNC0;
186    cuCb.cuCfgParams.plmn.mnc[1] = PLMN_MNC1;
187    cuCb.cuCfgParams.plmn.mnc[2] = PLMN_MNC2;
188
189    /*RRC Version*/
190    cuCb.cuCfgParams.rrcVersion.rrcVer = RRC_VER;
191    cuCb.cuCfgParams.rrcVersion.extRrcVer = EXT_RRC_VER;
192
193
194    /* EGTP Parameters */
195    cuCb.cuCfgParams.egtpParams.localIp.ipV4Pres = TRUE;
196    cuCb.cuCfgParams.egtpParams.localIp.ipV4Addr = ipv4_cu;
197    cuCb.cuCfgParams.egtpParams.localPort = CU_EGTP_PORT;
198    cuCb.cuCfgParams.egtpParams.destIp.ipV4Pres = TRUE;
199    cuCb.cuCfgParams.egtpParams.destIp.ipV4Addr = ipv4_du;
200    cuCb.cuCfgParams.egtpParams.destPort = DU_EGTP_PORT;
201    cuCb.cuCfgParams.egtpParams.minTunnelId = MIN_TEID;
202    cuCb.cuCfgParams.egtpParams.currTunnelId = cuCb.cuCfgParams.egtpParams.minTunnelId;
203    cuCb.cuCfgParams.egtpParams.maxTunnelId = MAX_TEID;
204
205 } /* End of readCuCfg */
206
207 /*******************************************************************
208  *
209  * @brief Handles Console input
210  *
211  * @details
212  *
213  *    Function : cuConsoleHandler
214  *
215  *    Functionality: Handles Console input
216  *
217  * @params[in] 
218  * @return ROK     - success
219  *         RFAILED - failure
220  *
221  * ****************************************************************/
222 void *cuConsoleHandler(void *args)
223 {
224    char ch;
225    uint8_t teId = 0;
226    uint8_t ret = ROK;
227    uint8_t cnt = 0;
228
229    /* This variable is taken for sending specific number of downlink data packet. 
230     * Presently the code is supporting total 4500 data packets trasfer for 3 UEs only with sleep(1).
231     * If you wants to pump data for 3 UE change the following macro values
232     * NUM_TUNNEL_TO_PUMP_DATA = 9, NUM_DL_PACKETS = 1.
233     * totalDataPacket = totalNumOfTestFlow * NUM_TUNNEL_TO_PUMP_DATA * NUM_DL_PACKETS 
234     * totalDataPacket = [500*9*1] */
235    int32_t totalNumOfTestFlow = 500; 
236
237    while(true) 
238    {
239       /* Send DL user data to CU when user enters 'd' on console */
240       if((ch = getchar()) == 'd')
241       {
242
243       /* Change #if 0 to #if 1 to take input from user */
244 #if 0
245          DU_LOG("\n EGTP --> : Enter TEID id(1..10) where DL Data to be sent\n");
246          scanf("%d",&teId);
247          
248          if(teId > MAX_TEID || teId < MIN_TEID)
249          {
250             DU_LOG("\nERROR  -->  EGTP : TEID(%x) OUT Of Range",teId);
251             printf("\n");
252             continue;
253          }
254          /* Start Pumping data from CU to DU */
255          DU_LOG("\nDEBUG  -->  EGTP: Sending DL User Data(teId:%d)\n",teId);
256
257          cnt =0;
258          while(cnt < NUM_DL_PACKETS)
259          {
260             ret =  cuEgtpDatReq(teId);
261             if(ret != ROK)
262             {
263                DU_LOG("\nERROR --> EGTP: Issue with teid=%d\n",teId);
264                break;
265             }
266             cnt++;
267          }
268 #else
269          while(totalNumOfTestFlow)
270          {
271             for(teId = 1; teId <= NUM_TUNNEL_TO_PUMP_DATA; teId++)
272             {
273                DU_LOG("\nDEBUG  -->  EGTP: Sending DL User Data(teId:%d)\n",teId);
274                cnt =0;
275                while(cnt < NUM_DL_PACKETS)
276                {
277                   ret =  cuEgtpDatReq(teId);      
278                   if(ret != ROK)
279                   {
280                      DU_LOG("\nERROR --> EGTP: Issue with teid=%d\n",teId);
281                      break;
282                   }
283                   /* TODO : sleep(1) will be removed later once we will be able to
284                    * support the continuous data pack transfer */
285                   sleep(1);
286                   cnt++;
287                }
288             }
289             totalNumOfTestFlow--;
290          }
291 #endif
292          continue;
293       } 
294    }
295 }
296 /**********************************************************************
297          End of file
298 **********************************************************************/