lte-softmodem.c 23.9 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

22
/*! \file lte-softmodem.c
23
 * \brief Top-level threads for eNodeB
24
 * \author R. Knopp, F. Kaltenberger, Navid Nikaein
Raymond Knopp's avatar
 
Raymond Knopp committed
25 26 27
 * \date 2012
 * \version 0.1
 * \company Eurecom
28
 * \email: knopp@eurecom.fr,florian.kaltenberger@eurecom.fr, navid.nikaein@eurecom.fr
Raymond Knopp's avatar
 
Raymond Knopp committed
29 30 31
 * \note
 * \warning
 */
32

33

Cedric Roux's avatar
Cedric Roux committed
34 35 36
#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <sched.h>

37
#include "rt_wrapper.h"
38

39

40 41
#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all

42
#include "assertions.h"
43
#include "msc.h"
44 45

#include "PHY/types.h"
46

47
#include "PHY/defs_eNB.h"
48
#include "common/ran_context.h"
49
#include "common/config/config_userapi.h"
oai's avatar
oai committed
50
#include "common/utils/load_module_shlib.h"
51
#undef MALLOC //there are two conflicting definitions, so we better make sure we don't use it at all
Raymond Knopp's avatar
 
Raymond Knopp committed
52
//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all
53

Raymond Knopp's avatar
 
Raymond Knopp committed
54
#include "../../ARCH/COMMON/common_lib.h"
55
#include "../../ARCH/ETHERNET/USERSPACE/LIB/if_defs.h"
Raymond Knopp's avatar
 
Raymond Knopp committed
56

Raymond Knopp's avatar
 
Raymond Knopp committed
57
//#undef FRAME_LENGTH_COMPLEX_SAMPLES //there are two conflicting definitions, so we better make sure we don't use it at all
58

59 60 61
#include "PHY/phy_vars.h"
#include "SCHED/sched_common_vars.h"
#include "LAYER2/MAC/mac_vars.h"
62

63 64 65 66
#include "LAYER2/MAC/mac.h"
#include "LAYER2/MAC/mac_proto.h"
#include "RRC/LTE/rrc_vars.h"
#include "PHY_INTERFACE/phy_interface_vars.h"
67
#include "PHY/TOOLS/phy_scope_interface.h"
frtabu's avatar
frtabu committed
68
#include "nfapi/oai_integration/vendor_ext.h"
69 70 71 72
#ifdef SMBV
#include "PHY/TOOLS/smbv.h"
unsigned short config_frames[4] = {2,9,11,13};
#endif
73
#include "common/utils/LOG/log.h"
Navid Nikaein's avatar
Navid Nikaein committed
74
#include "UTIL/OTG/otg_tx.h"
75
#include "UTIL/OTG/otg_externs.h"
76
#include "UTIL/MATH/oml.h"
77
#include "common/utils/LOG/vcd_signal_dumper.h"
78
#include "UTIL/OPT/opt.h"
79
#include "enb_config.h"
Navid Nikaein's avatar
Navid Nikaein committed
80
//#include "PHY/TOOLS/time_meas.h"
81

Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
82
#ifndef OPENAIR2
83
  #include "UTIL/OTG/otg_vars.h"
Florian Kaltenberger's avatar
 
Florian Kaltenberger committed
84 85
#endif

86 87 88

#include "create_tasks.h"

89

90 91
#include "PHY/INIT/phy_init.h"

92 93
#include "system.h"

94

95
#include "lte-softmodem.h"
oai's avatar
oai committed
96
#include "NB_IoT_interface.h"
97

frtabu's avatar
frtabu committed
98

99 100 101 102 103
pthread_cond_t nfapi_sync_cond;
pthread_mutex_t nfapi_sync_mutex;
int nfapi_sync_var=-1; //!< protected by mutex \ref nfapi_sync_mutex


104 105
uint16_t sf_ahead=4;

Raymond Knopp's avatar
 
Raymond Knopp committed
106 107
pthread_cond_t sync_cond;
pthread_mutex_t sync_mutex;
108
int sync_var=-1; //!< protected by mutex \ref sync_mutex.
109
int config_sync_var=-1;
110

111 112
uint16_t runtime_phy_rx[29][6]; // SISO [MCS 0-28][RBs 0-5 : 6, 15, 25, 50, 75, 100]
uint16_t runtime_phy_tx[29][6]; // SISO [MCS 0-28][RBs 0-5 : 6, 15, 25, 50, 75, 100]
Raymond Knopp's avatar
 
Raymond Knopp committed
113 114


Raymond Knopp's avatar
Raymond Knopp committed
115
volatile int             oai_exit = 0;
116

117
uint32_t                 downlink_frequency[MAX_NUM_CCs][4];
118
int32_t                  uplink_frequency_offset[MAX_NUM_CCs][4];
Lionel Gauthier's avatar
Lionel Gauthier committed
119

120
int UE_scan = 1;
121
int UE_scan_carrier = 0;
122
runmode_t mode = normal_txrx;
frtabu's avatar
frtabu committed
123

124 125
FILE *input_fd=NULL;

126

127 128
#if MAX_NUM_CCs == 1
rx_gain_t                rx_gain_mode[MAX_NUM_CCs][4] = {{max_gain,max_gain,max_gain,max_gain}};
kortke's avatar
kortke committed
129 130
double tx_gain[MAX_NUM_CCs][4] = {{20,0,0,0}};
double rx_gain[MAX_NUM_CCs][4] = {{110,0,0,0}};
131 132 133 134
#else
rx_gain_t                rx_gain_mode[MAX_NUM_CCs][4] = {{max_gain,max_gain,max_gain,max_gain},{max_gain,max_gain,max_gain,max_gain}};
double tx_gain[MAX_NUM_CCs][4] = {{20,0,0,0},{20,0,0,0}};
double rx_gain[MAX_NUM_CCs][4] = {{110,0,0,0},{20,0,0,0}};
Lionel Gauthier's avatar
Lionel Gauthier committed
135
#endif
Raymond Knopp's avatar
 
Raymond Knopp committed
136

137
double rx_gain_off = 0.0;
138

Raymond Knopp's avatar
 
Raymond Knopp committed
139
double sample_rate=30.72e6;
140
double bw = 10.0e6;
141

Florian Kaltenberger's avatar
Florian Kaltenberger committed
142

143 144 145
uint8_t dci_Format = 0;
uint8_t agregation_Level =0xFF;

146 147
uint8_t nb_antenna_tx = 1;
uint8_t nb_antenna_rx = 1;
148

Raymond Knopp's avatar
 
Raymond Knopp committed
149 150 151
char ref[128] = "internal";
char channels[128] = "0";

152
int                      rx_input_level_dBm;
153

154 155

int                             otg_enabled;
156

157

158 159
uint8_t exit_missed_slots=1;
uint64_t num_missed_slots=0; // counter for the number of missed slots
160

161

162 163
extern void reset_opp_meas(void);
extern void print_opp_meas(void);
164

165 166 167

extern void init_eNB_afterRU(void);

168
int transmission_mode=1;
169
int emulate_rf = 0;
Wang Tsu-Han's avatar
Wang Tsu-Han committed
170
int numerology = 0;
171

laurent's avatar
laurent committed
172
THREAD_STRUCT thread_struct;
173 174 175
/* struct for ethernet specific parameters given in eNB conf file */
eth_params_t *eth_params;

176 177
double cpuf;

178

Raymond Knopp's avatar
 
Raymond Knopp committed
179

180 181 182
/* forward declarations */
void set_default_frame_parms(LTE_DL_FRAME_PARMS *frame_parms[MAX_NUM_CCs]);

183 184 185 186 187
/*---------------------BMC: timespec helpers -----------------------------*/

struct timespec min_diff_time = { .tv_sec = 0, .tv_nsec = 0 };
struct timespec max_diff_time = { .tv_sec = 0, .tv_nsec = 0 };

188
struct timespec clock_difftime(struct timespec start, struct timespec end) {
189
  struct timespec temp;
190

191 192 193 194 195 196 197
  if ((end.tv_nsec-start.tv_nsec)<0) {
    temp.tv_sec = end.tv_sec-start.tv_sec-1;
    temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
  } else {
    temp.tv_sec = end.tv_sec-start.tv_sec;
    temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  }
198

199
  return temp;
200 201
}

202
void print_difftimes(void) {
203
#ifdef DEBUG
204
  printf("difftimes min = %lu ns ; max = %lu ns\n", min_diff_time.tv_nsec, max_diff_time.tv_nsec);
205
#else
206
  LOG_I(HW,"difftimes min = %lu ns ; max = %lu ns\n", min_diff_time.tv_nsec, max_diff_time.tv_nsec);
207 208 209
#endif
}

210
void update_difftimes(struct timespec start, struct timespec end) {
211 212 213
  struct timespec diff_time = { .tv_sec = 0, .tv_nsec = 0 };
  int             changed = 0;
  diff_time = clock_difftime(start, end);
214

215 216 217 218
  if ((min_diff_time.tv_nsec == 0) || (diff_time.tv_nsec < min_diff_time.tv_nsec)) {
    min_diff_time.tv_nsec = diff_time.tv_nsec;
    changed = 1;
  }
219

220 221 222 223
  if ((max_diff_time.tv_nsec == 0) || (diff_time.tv_nsec > max_diff_time.tv_nsec)) {
    max_diff_time.tv_nsec = diff_time.tv_nsec;
    changed = 1;
  }
224

225
#if 1
226

227
  if (changed) print_difftimes();
228

229 230 231 232 233
#endif
}

/*------------------------------------------------------------------------*/

234
unsigned int build_rflocal(int txi, int txq, int rxi, int rxq) {
Raymond Knopp's avatar
 
Raymond Knopp committed
235
  return (txi + (txq<<6) + (rxi<<12) + (rxq<<18));
236
}
237
unsigned int build_rfdc(int dcoff_i_rxfe, int dcoff_q_rxfe) {
Raymond Knopp's avatar
 
Raymond Knopp committed
238
  return (dcoff_i_rxfe + (dcoff_q_rxfe<<8));
239 240
}

241

242
void signal_handler(int sig) {
243 244 245 246 247 248 249 250 251 252
  void *array[10];
  size_t size;

  if (sig==SIGSEGV) {
    // get void*'s for all entries on the stack
    size = backtrace(array, 10);
    // print out all the frames to stderr
    fprintf(stderr, "Error: signal %d:\n", sig);
    backtrace_symbols_fd(array, size, 2);
    exit(-1);
253
  } else {
254 255
    printf("Linux signal %s...\n",strsignal(sig));
    exit_function(__FILE__, __FUNCTION__, __LINE__,"softmodem starting exit procedure\n");
256 257
  }
}
258

259

260
void exit_function(const char *file, const char *function, const int line, const char *s) {
261
  int ru_id;
Raymond Knopp's avatar
Raymond Knopp committed
262

263
  if (s != NULL) {
264
    printf("%s:%d %s() Exiting OAI softmodem: %s\n",file,line, function, s);
265 266 267
  }

  oai_exit = 1;
268

269 270
  if (RC.ru == NULL)
    exit(-1); // likely init not completed, prevent crash or hang, exit now...
271

272 273 274 275
  for (ru_id=0; ru_id<RC.nb_RU; ru_id++) {
    if (RC.ru[ru_id] && RC.ru[ru_id]->rfdevice.trx_end_func) {
      RC.ru[ru_id]->rfdevice.trx_end_func(&RC.ru[ru_id]->rfdevice);
      RC.ru[ru_id]->rfdevice.trx_end_func = NULL;
276 277
    }

278 279 280 281 282
    if (RC.ru[ru_id] && RC.ru[ru_id]->ifdevice.trx_end_func) {
      RC.ru[ru_id]->ifdevice.trx_end_func(&RC.ru[ru_id]->ifdevice);
      RC.ru[ru_id]->ifdevice.trx_end_func = NULL;
    }
  }
283

284 285
  sleep(1); //allow lte-softmodem threads to exit first
  exit(1);
286
}
287

288

289

290
static void get_options(void) {
291
  CONFIG_SETRTFLAG(CONFIG_NOEXITONHELP);
292
  get_common_options();
293
  CONFIG_CLEARRTFLAG(CONFIG_NOEXITONHELP);
oai's avatar
oai committed
294

295
  if ( !(CONFIG_ISFLAGSET(CONFIG_ABORT)) ) {
296 297 298 299 300 301
    memset((void *)&RC,0,sizeof(RC));
    /* Read RC configuration file */
    RCConfig();
    NB_eNB_INST = RC.nb_inst;
    printf("Configuration: nb_rrc_inst %d, nb_L1_inst %d, nb_ru %d\n",NB_eNB_INST,RC.nb_L1_inst,RC.nb_RU);

302
    if (!IS_SOFTMODEM_NONBIOT) {
303 304 305 306 307 308 309 310
      load_NB_IoT();
      printf("               nb_nbiot_rrc_inst %d, nb_nbiot_L1_inst %d, nb_nbiot_macrlc_inst %d\n",
             RC.nb_nb_iot_rrc_inst, RC.nb_nb_iot_L1_inst, RC.nb_nb_iot_macrlc_inst);
    } else {
      printf("All Nb-IoT instances disabled\n");
      RC.nb_nb_iot_rrc_inst=RC.nb_nb_iot_L1_inst=RC.nb_nb_iot_macrlc_inst=0;
    }
  }
311 312 313
}


314

315

316

317
void set_default_frame_parms(LTE_DL_FRAME_PARMS *frame_parms[MAX_NUM_CCs]) {
Raymond Knopp's avatar
Raymond Knopp committed
318 319 320
  int CC_id;

  for (CC_id=0; CC_id<MAX_NUM_CCs; CC_id++) {
321
    frame_parms[CC_id] = (LTE_DL_FRAME_PARMS *) malloc(sizeof(LTE_DL_FRAME_PARMS));
Raymond Knopp's avatar
Raymond Knopp committed
322 323 324 325 326 327 328 329 330 331
    /* Set some default values that may be overwritten while reading options */
    frame_parms[CC_id]->frame_type          = FDD;
    frame_parms[CC_id]->tdd_config          = 3;
    frame_parms[CC_id]->tdd_config_S        = 0;
    frame_parms[CC_id]->N_RB_DL             = 100;
    frame_parms[CC_id]->N_RB_UL             = 100;
    frame_parms[CC_id]->Ncp                 = NORMAL;
    frame_parms[CC_id]->Ncp_UL              = NORMAL;
    frame_parms[CC_id]->Nid_cell            = 0;
    frame_parms[CC_id]->num_MBSFN_config    = 0;
332
    frame_parms[CC_id]->nb_antenna_ports_eNB  = 1;
Raymond Knopp's avatar
Raymond Knopp committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    frame_parms[CC_id]->nb_antennas_tx      = 1;
    frame_parms[CC_id]->nb_antennas_rx      = 1;
    frame_parms[CC_id]->nushift             = 0;
    frame_parms[CC_id]->phich_config_common.phich_resource = oneSixth;
    frame_parms[CC_id]->phich_config_common.phich_duration = normal;
    // UL RS Config
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.cyclicShift = 0;//n_DMRS1 set to 0
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.groupHoppingEnabled = 0;
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.sequenceHoppingEnabled = 0;
    frame_parms[CC_id]->pusch_config_common.ul_ReferenceSignalsPUSCH.groupAssignmentPUSCH = 0;
    frame_parms[CC_id]->prach_config_common.rootSequenceIndex=22;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.zeroCorrelationZoneConfig=1;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.prach_ConfigIndex=0;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.highSpeedFlag=0;
    frame_parms[CC_id]->prach_config_common.prach_ConfigInfo.prach_FreqOffset=0;
348 349 350 351
    //    downlink_frequency[CC_id][0] = 2680000000; // Use float to avoid issue with frequency over 2^31.
    //    downlink_frequency[CC_id][1] = downlink_frequency[CC_id][0];
    //    downlink_frequency[CC_id][2] = downlink_frequency[CC_id][0];
    //    downlink_frequency[CC_id][3] = downlink_frequency[CC_id][0];
Raymond Knopp's avatar
Raymond Knopp committed
352
    //printf("Downlink for CC_id %d frequency set to %u\n", CC_id, downlink_frequency[CC_id][0]);
353
    frame_parms[CC_id]->dl_CarrierFreq=downlink_frequency[CC_id][0];
Raymond Knopp's avatar
Raymond Knopp committed
354 355 356
  }
}

357
void wait_RUs(void) {
358 359
  /* do not modify the following LOG_UI message, which is used by CI */
  LOG_UI(ENB_APP,"Waiting for RUs to be configured ... RC.ru_mask:%02lx\n", RC.ru_mask);
360 361
  // wait for all RUs to be configured over fronthaul
  pthread_mutex_lock(&RC.ru_mutex);
362

363 364
  while (RC.ru_mask>0) {
    pthread_cond_wait(&RC.ru_cond,&RC.ru_mutex);
365
    printf("RC.ru_mask:%02lx\n", RC.ru_mask);
366 367
  }

368
  pthread_mutex_unlock(&RC.ru_mutex);
369 370 371
  LOG_I(PHY,"RUs configured\n");
}

372
void wait_eNBs(void) {
373 374 375 376
  int i,j;
  int waiting=1;

  while (waiting==1) {
Cedric Roux's avatar
Cedric Roux committed
377 378
    printf("Waiting for eNB L1 instances to all get configured ... sleeping 50ms (nb_L1_inst %d)\n",RC.nb_L1_inst);
    usleep(50*1000);
379
    waiting=0;
380

381
    for (i=0; i<RC.nb_L1_inst; i++) {
382 383
      printf("RC.nb_L1_CC[%d]:%d\n", i, RC.nb_L1_CC[i]);

384 385 386 387 388
      for (j=0; j<RC.nb_L1_CC[i]; j++) {
        if (RC.eNB[i][j]->configured==0) {
          waiting=1;
          break;
        }
389 390
      }
    }
391
  }
392

393 394
  printf("eNB L1 are configured\n");
}
395

396

397 398
/*
 * helper function to terminate a certain ITTI task
399
 */
400
void terminate_task(module_id_t mod_id, task_id_t from, task_id_t to) {
401
  LOG_I(ENB_APP, "sending TERMINATE_MESSAGE from task %s (%d) to task %s (%d)\n",
402
        itti_get_task_name(from), from, itti_get_task_name(to), to);
403
  MessageDef *msg;
404 405
  msg = itti_alloc_new_message (from, TERMINATE_MESSAGE);
  itti_send_msg_to_task (to, ENB_MODULE_ID_TO_INSTANCE(mod_id), msg);
406 407
}

408
extern void  free_transport(PHY_VARS_eNB *);
409
extern void  phy_free_RU(RU_t *);
410

411
int stop_L1L2(module_id_t enb_id) {
412 413
  LOG_W(ENB_APP, "stopping lte-softmodem\n");

414
  if (!RC.ru) {
415
    LOG_UI(ENB_APP, "no RU configured\n");
416 417 418
    return -1;
  }

419
  /* these tasks need to pick up new configuration */
420
  terminate_task(enb_id, TASK_ENB_APP, TASK_RRC_ENB);
Robert Schmidt's avatar
Robert Schmidt committed
421
  oai_exit = 1;
422
  LOG_I(ENB_APP, "calling kill_RU_proc() for instance %d\n", enb_id);
Robert Schmidt's avatar
Robert Schmidt committed
423
  kill_RU_proc(RC.ru[enb_id]);
424
  LOG_I(ENB_APP, "calling kill_eNB_proc() for instance %d\n", enb_id);
425
  kill_eNB_proc(enb_id);
426
  oai_exit = 0;
427

428 429 430 431
  for (int cc_id = 0; cc_id < RC.nb_CC[enb_id]; cc_id++) {
    free_transport(RC.eNB[enb_id][cc_id]);
    phy_free_lte_eNB(RC.eNB[enb_id][cc_id]);
  }
432

433
  phy_free_RU(RC.ru[enb_id]);
434
  free_lte_top();
435 436 437 438
  return 0;
}

/*
439
 * Restart the lte-softmodem after it has been soft-stopped with stop_L1L2()
440
 */
441
int restart_L1L2(module_id_t enb_id) {
442 443 444
  RU_t *ru = RC.ru[enb_id];
  int cc_id;
  MessageDef *msg_p = NULL;
445 446
  LOG_W(ENB_APP, "restarting lte-softmodem\n");
  /* block threads */
447
  pthread_mutex_lock(&sync_mutex);
448
  sync_var = -1;
449
  pthread_mutex_unlock(&sync_mutex);
450

451 452
  for (cc_id = 0; cc_id < RC.nb_L1_CC[enb_id]; cc_id++) {
    RC.eNB[enb_id][cc_id]->configured = 0;
453 454
  }

455 456 457 458 459
  RC.ru_mask |= (1 << ru->idx);
  /* copy the changed frame parameters to the RU */
  /* TODO this should be done for all RUs associated to this eNB */
  memcpy(&ru->frame_parms, &RC.eNB[enb_id][0]->frame_parms, sizeof(LTE_DL_FRAME_PARMS));
  set_function_spec_param(RC.ru[enb_id]);
460 461 462
  /* reset the list of connected UEs in the MAC, since in this process with
   * loose all UEs (have to reconnect) */
  init_UE_list(&RC.mac[enb_id]->UE_list);
463
  LOG_I(ENB_APP, "attempting to create ITTI tasks\n");
464

465 466 467 468 469 470 471
  if (itti_create_task (TASK_RRC_ENB, rrc_enb_task, NULL) < 0) {
    LOG_E(RRC, "Create task for RRC eNB failed\n");
    return -1;
  } else {
    LOG_I(RRC, "Re-created task for RRC eNB successfully\n");
  }

472 473 474 475 476
  /* pass a reconfiguration request which will configure everything down to
   * RC.eNB[i][j]->frame_parms, too */
  msg_p = itti_alloc_new_message(TASK_ENB_APP, RRC_CONFIGURATION_REQ);
  RRC_CONFIGURATION_REQ(msg_p) = RC.rrc[enb_id]->configuration;
  itti_send_msg_to_task(TASK_RRC_ENB, ENB_MODULE_ID_TO_INSTANCE(enb_id), msg_p);
477 478
  /* TODO XForms might need to be restarted, but it is currently (09/02/18)
   * broken, so we cannot test it */
479 480 481 482 483 484
  wait_eNBs();
  init_RU_proc(ru);
  ru->rf_map.card = 0;
  ru->rf_map.chain = 0; /* CC_id + chain_offset;*/
  wait_RUs();
  init_eNB_afterRU();
485 486 487 488 489 490
  printf("Sending sync to all threads\n");
  pthread_mutex_lock(&sync_mutex);
  sync_var=0;
  pthread_cond_broadcast(&sync_cond);
  pthread_mutex_unlock(&sync_mutex);
  return 0;
491
}
492

493 494 495 496
void init_pdcp(void) {
  if (!NODE_IS_DU(RC.rrc[0]->node_type)) {
    pdcp_layer_init();
    uint32_t pdcp_initmask = (IS_SOFTMODEM_NOS1) ?
497 498
                             (PDCP_USE_NETLINK_BIT | LINK_ENB_PDCP_TO_IP_DRIVER_BIT) : LINK_ENB_PDCP_TO_GTPV1U_BIT;

499 500
    if (IS_SOFTMODEM_NOS1)
      pdcp_initmask = pdcp_initmask | ENB_NAS_USE_TUN_BIT | SOFTMODEM_NOKRNMOD_BIT  ;
501

502 503 504
    pdcp_module_init(pdcp_initmask);

    if (NODE_IS_CU(RC.rrc[0]->node_type)) {
505
      pdcp_set_rlc_data_req_func((send_rlc_data_req_func_t)proto_agent_send_rlc_data_req);
506
    } else {
507 508
      pdcp_set_rlc_data_req_func((send_rlc_data_req_func_t) rlc_data_req);
      pdcp_set_pdcp_data_ind_func((pdcp_data_ind_func_t) pdcp_data_ind);
509 510
    }
  } else {
511
    pdcp_set_pdcp_data_ind_func((pdcp_data_ind_func_t) proto_agent_send_pdcp_data_ind);
512 513
  }
}
514

515
static  void wait_nfapi_init(char *thread_name) {
516 517
  printf( "waiting for NFAPI PNF connection and population of global structure (%s)\n",thread_name);
  pthread_mutex_lock( &nfapi_sync_mutex );
518

519 520
  while (nfapi_sync_var<0)
    pthread_cond_wait( &nfapi_sync_cond, &nfapi_sync_mutex );
521

522
  pthread_mutex_unlock(&nfapi_sync_mutex);
523
  printf( "NFAPI: got sync (%s)\n", thread_name);
524 525
}

526
int main( int argc, char **argv ) {
527
  int i;
528
  int CC_id = 0;
529
  int ru_id;
530

531
  if ( load_configmodule(argc,argv,0) == NULL) {
532
    exit_fun("[SOFTMODEM] Error, configuration module init failed\n");
533
  }
534

535
  mode = normal_txrx;
Raymond Knopp's avatar
 
Raymond Knopp committed
536
  set_latency_target();
537
  logInit();
538
  printf("Reading in command-line options\n");
539
  get_options ();
540

541
  if (is_nos1exec(argv[0]) )
542 543
    set_softmodem_optmask(SOFTMODEM_NOS1_BIT);

544
  EPC_MODE_ENABLED = !IS_SOFTMODEM_NOS1;
545

546
  if (CONFIG_ISFLAGSET(CONFIG_ABORT) ) {
547 548
    fprintf(stderr,"Getting configuration failed\n");
    exit(-1);
549
  }
Raymond Knopp's avatar
Raymond Knopp committed
550

551
#if T_TRACER
552
  T_Config_Init();
553
#endif
554 555
  //randominit (0);
  set_taus_seed (0);
556
  printf("configuring for RAU/RRU\n");
557

558
  if (opp_enabled ==1) {
559
    reset_opp_meas();
560
  }
561

562
  cpuf=get_cpu_freq_GHz();
563
  printf("ITTI init, useMME: %i\n",EPC_MODE_ENABLED);
Cedric Roux's avatar
Cedric Roux committed
564
  itti_init(TASK_MAX, THREAD_MAX, MESSAGES_ID_MAX, tasks_info, messages_info);
565

566
  // initialize mscgen log after ITTI
567
  if (get_softmodem_params()->start_msc) {
568
    load_module_shlib("msc",NULL,0,&msc_interface);
569
  }
570

571
  MSC_INIT(MSC_E_UTRAN, THREAD_MAX+TASK_MAX);
572
  init_opt();
Raymond Knopp's avatar
 
Raymond Knopp committed
573 574 575
  // to make a graceful exit when ctrl-c is pressed
  signal(SIGSEGV, signal_handler);
  signal(SIGINT, signal_handler);
576 577
  signal(SIGTERM, signal_handler);
  signal(SIGABRT, signal_handler);
Raymond Knopp's avatar
 
Raymond Knopp committed
578
  check_clock();
579 580 581 582
#ifndef PACKAGE_VERSION
#  define PACKAGE_VERSION "UNKNOWN-EXPERIMENTAL"
#endif
  LOG_I(HW, "Version: %s\n", PACKAGE_VERSION);
583
  printf("Runtime table\n");
584
  fill_modeled_runtime_table(runtime_phy_rx,runtime_phy_tx);
585 586

  /* Read configuration */
587
  if (RC.nb_inst > 0) {
588
    read_config_and_init();
589 590
    /* Start the agent. If it is turned off in the configuration, it won't start */
    RCconfig_flexran();
591

592 593 594
    for (i = 0; i < RC.nb_inst; i++) {
      flexran_agent_start(i);
    }
595

596 597 598
    /* initializes PDCP and sets correct RLC Request/PDCP Indication callbacks
     * for monolithic/F1 modes */
    init_pdcp();
599

600 601 602
    if (create_tasks(1) < 0) {
      printf("cannot create ITTI tasks\n");
      exit(-1);
Raymond Knopp's avatar
Raymond Knopp committed
603
    }
604 605 606

    for (int enb_id = 0; enb_id < RC.nb_inst; enb_id++) {
      MessageDef *msg_p = itti_alloc_new_message (TASK_ENB_APP, RRC_CONFIGURATION_REQ);
607
      RRC_CONFIGURATION_REQ(msg_p) = RC.rrc[enb_id]->configuration;
608
      itti_send_msg_to_task (TASK_RRC_ENB, ENB_MODULE_ID_TO_INSTANCE(enb_id), msg_p);
609
    }
frtabu's avatar
frtabu committed
610
  } else {
611
    printf("RC.nb_inst = 0, Initializing L1\n");
612
    RCconfig_L1();
613
  }
Raymond Knopp's avatar
Raymond Knopp committed
614

615
  if (RC.nb_inst > 0 && NODE_IS_CU(RC.rrc[0]->node_type)) {
616 617 618 619 620
    protocol_ctxt_t ctxt;
    ctxt.module_id = 0 ;
    ctxt.instance = 0;
    ctxt.rnti = 0;
    ctxt.enb_flag = 1;
621 622
    ctxt.frame = 0;
    ctxt.subframe = 0;
623 624 625
    pdcp_run(&ctxt);
  }

626
  /* start threads if only L1 or not a CU */
627
  if (RC.nb_inst == 0 || !NODE_IS_CU(RC.rrc[0]->node_type)) {
628
    // init UE_PF_PO and mutex lock
Raymond Knopp's avatar
Raymond Knopp committed
629 630 631 632 633
    pthread_mutex_init(&ue_pf_po_mutex, NULL);
    memset (&UE_PF_PO[0][0], 0, sizeof(UE_PF_PO_t)*MAX_MOBILES_PER_ENB*MAX_NUM_CCs);
    mlockall(MCL_CURRENT | MCL_FUTURE);
    pthread_cond_init(&sync_cond,NULL);
    pthread_mutex_init(&sync_mutex, NULL);
frtabu's avatar
frtabu committed
634

635

636

Raymond Knopp's avatar
Raymond Knopp committed
637
    rt_sleep_ns(10*100000000ULL);
638

frtabu's avatar
frtabu committed
639 640
    if (NFAPI_MODE!=NFAPI_MONOLITHIC) {
      LOG_I(ENB_APP,"NFAPI*** - mutex and cond created - will block shortly for completion of PNF connection\n");
641 642 643
      pthread_cond_init(&sync_cond,NULL);
      pthread_mutex_init(&sync_mutex, NULL);
    }
644

frtabu's avatar
frtabu committed
645
    if (NFAPI_MODE==NFAPI_MODE_VNF) {// VNF
646
#if defined(PRE_SCD_THREAD)
647
      init_ru_vnf();  // ru pointer is necessary for pre_scd.
648
#endif
Raymond Knopp's avatar
Raymond Knopp committed
649
      wait_nfapi_init("main?");
650
    }
651

frtabu's avatar
frtabu committed
652
    LOG_I(ENB_APP,"START MAIN THREADS\n");
Raymond Knopp's avatar
Raymond Knopp committed
653
    // start the main threads
654
    number_of_cards = 1;
655
    printf("RC.nb_L1_inst:%d\n", RC.nb_L1_inst);
656

657
    if (RC.nb_L1_inst > 0) {
658 659
      printf("Initializing eNB threads single_thread_flag:%d wait_for_sync:%d\n", get_softmodem_params()->single_thread_flag,get_softmodem_params()->wait_for_sync);
      init_eNB(get_softmodem_params()->single_thread_flag,get_softmodem_params()->wait_for_sync);
660
      //      for (inst=0;inst<RC.nb_L1_inst;inst++)
661
      //  for (CC_id=0;CC_id<RC.nb_L1_CC[inst];CC_id++) phy_init_lte_eNB(RC.eNB[inst][CC_id],0,0);
Raymond Knopp's avatar
 
Raymond Knopp committed
662
    }
663

664
    printf("wait_eNBs()\n");
665
    wait_eNBs();
666
    printf("About to Init RU threads RC.nb_RU:%d\n", RC.nb_RU);
667

668 669 670
    // RU thread and some L1 procedure aren't necessary in VNF or L2 FAPI simulator.
    // but RU thread deals with pre_scd and this is necessary in VNF and simulator.
    // some initialization is necessary and init_ru_vnf do this.
frtabu's avatar
frtabu committed
671
    if (RC.nb_RU >0 && NFAPI_MODE!=NFAPI_MODE_VNF) {
672
      printf("Initializing RU threads\n");
673
      init_RU(get_softmodem_params()->rf_config_file);
674

frtabu's avatar
frtabu committed
675
      for (ru_id=0; ru_id<RC.nb_RU; ru_id++) {
676 677
        RC.ru[ru_id]->rf_map.card=0;
        RC.ru[ru_id]->rf_map.chain=CC_id+(get_softmodem_params()->chain_offset);
678 679
      }
    }
680

681
    config_sync_var=0;
682

frtabu's avatar
frtabu committed
683
    if (NFAPI_MODE==NFAPI_MODE_PNF) { // PNF
684 685 686 687
      wait_nfapi_init("main?");
    }

    printf("wait RUs\n");
688
    // CI -- Flushing the std outputs for the previous marker to show on the eNB / RRU log file
689 690
    fflush(stdout);
    fflush(stderr);
691
    // end of CI modifications
692
    wait_RUs();
frtabu's avatar
frtabu committed
693
    LOG_I(ENB_APP,"RC.nb_RU:%d\n", RC.nb_RU);
694
    // once all RUs are ready intiailize the rest of the eNBs ((dependence on final RU parameters after configuration)
695
    printf("ALL RUs ready - init eNBs\n");
696

frtabu's avatar
frtabu committed
697 698
    if (NFAPI_MODE!=NFAPI_MODE_PNF && NFAPI_MODE!=NFAPI_MODE_VNF) {
      LOG_I(ENB_APP,"Not NFAPI mode - call init_eNB_afterRU()\n");
699 700
      init_eNB_afterRU();
    } else {
frtabu's avatar
frtabu committed
701
      LOG_I(ENB_APP,"NFAPI mode - DO NOT call init_eNB_afterRU()\n");
702 703
    }

frtabu's avatar
frtabu committed
704
    LOG_UI(ENB_APP,"ALL RUs ready - ALL eNBs ready\n");
Raymond Knopp's avatar
Raymond Knopp committed
705 706
    // connect the TX/RX buffers
    sleep(1); /* wait for thread activation */
frtabu's avatar
frtabu committed
707
    LOG_I(ENB_APP,"Sending sync to all threads\n");
Raymond Knopp's avatar
Raymond Knopp committed
708 709 710 711
    pthread_mutex_lock(&sync_mutex);
    sync_var=0;
    pthread_cond_broadcast(&sync_cond);
    pthread_mutex_unlock(&sync_mutex);
712
    config_check_unknown_cmdlineopt(CONFIG_CHECKALLSECTIONS);
Raymond Knopp's avatar
Raymond Knopp committed
713
  }
714

715
  // wait for end of program
716
  LOG_UI(ENB_APP,"TYPE <CTRL-C> TO TERMINATE\n");
717 718 719 720
  // CI -- Flushing the std outputs for the previous marker to show on the eNB / DU / CU log file
  fflush(stdout);
  fflush(stderr);
  // end of CI modifications
Raymond Knopp's avatar
 
Raymond Knopp committed
721
  //getchar();
722 723
  if(IS_SOFTMODEM_DOFORMS)
     load_softscope("enb");
Raymond Knopp's avatar
 
Raymond Knopp committed
724
  itti_wait_tasks_end();
725
  oai_exit=1;
726
  LOG_I(ENB_APP,"oai_exit=%d\n",oai_exit);
Raymond Knopp's avatar
 
Raymond Knopp committed
727
  // stop threads
728

729
  if (RC.nb_inst == 0 || !NODE_IS_CU(RC.rrc[0]->node_type)) {
730 731
    if(IS_SOFTMODEM_DOFORMS)
      end_forms();
732

733
    LOG_I(ENB_APP,"stopping MODEM threads\n");
Robert Schmidt's avatar
Robert Schmidt committed
734
    stop_eNB(NB_eNB_INST);
735
    stop_RU(RC.nb_RU);
frtabu's avatar
frtabu committed
736

737 738 739 740 741 742 743 744
    /* release memory used by the RU/eNB threads (incomplete), after all
     * threads have been stopped (they partially use the same memory) */
    for (int inst = 0; inst < NB_eNB_INST; inst++) {
      for (int cc_id = 0; cc_id < RC.nb_CC[inst]; cc_id++) {
        free_transport(RC.eNB[inst][cc_id]);
        phy_free_lte_eNB(RC.eNB[inst][cc_id]);
      }
    }
frtabu's avatar
frtabu committed
745

746
    for (int inst = 0; inst < RC.nb_RU; inst++) {
747 748
      phy_free_RU(RC.ru[inst]);
    }
frtabu's avatar
frtabu committed
749

750
    free_lte_top();
751
    end_configmodule();
Raymond Knopp's avatar
Raymond Knopp committed
752 753 754 755 756
    pthread_cond_destroy(&sync_cond);
    pthread_mutex_destroy(&sync_mutex);
    pthread_cond_destroy(&nfapi_sync_cond);
    pthread_mutex_destroy(&nfapi_sync_mutex);
    pthread_mutex_destroy(&ue_pf_po_mutex);
frtabu's avatar
frtabu committed
757

758
    for(ru_id=0; ru_id<RC.nb_RU; ru_id++) {
759 760 761 762
      if (RC.ru[ru_id]->rfdevice.trx_end_func) {
        RC.ru[ru_id]->rfdevice.trx_end_func(&RC.ru[ru_id]->rfdevice);
        RC.ru[ru_id]->rfdevice.trx_end_func = NULL;
      }
frtabu's avatar
frtabu committed
763

764 765 766 767
      if (RC.ru[ru_id]->ifdevice.trx_end_func) {
        RC.ru[ru_id]->ifdevice.trx_end_func(&RC.ru[ru_id]->ifdevice);
        RC.ru[ru_id]->ifdevice.trx_end_func = NULL;
      }
Raymond Knopp's avatar
Raymond Knopp committed
768
    }
Raymond Knopp's avatar
Raymond Knopp committed
769 770
  }

771
  terminate_opt();
Raymond Knopp's avatar
 
Raymond Knopp committed
772
  logClean();
773
  printf("Bye.\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
774 775
  return 0;
}