nr_pdcp_oai_api.c 43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * 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
 */

Laurent's avatar
Laurent committed
22 23 24
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
25
#include "nr_pdcp_asn1_utils.h"
26
#include "nr_pdcp_ue_manager.h"
Cedric Roux's avatar
Cedric Roux committed
27
#include "nr_pdcp_timer_thread.h"
28 29
#include "NR_RadioBearerConfig.h"
#include "NR_RLC-BearerConfig.h"
30
#include "NR_RLC-Config.h"
31 32
#include "NR_CellGroupConfig.h"
#include "openair2/RRC/NR/nr_rrc_proto.h"
33
#include <stdint.h>
34 35

/* from OAI */
36
#include "oai_asn1.h"
37
#include "nr_pdcp_oai_api.h"
38
#include "LAYER2/nr_rlc/nr_rlc_oai_api.h"
39
#include "openair2/F1AP/f1ap_ids.h"
Laurent Thomas's avatar
Laurent Thomas committed
40
#include <openair3/ocp-gtpu/gtp_itf.h>
41
#include "openair2/SDAP/nr_sdap/nr_sdap.h"
42
#include "gnb_config.h"
Melissa Elkadi's avatar
Melissa Elkadi committed
43
#include "executables/softmodem-common.h"
44
#include "cuup_cucp_if.h"
45 46 47 48 49 50 51 52

#define TODO do { \
    printf("%s:%d:%s: todo\n", __FILE__, __LINE__, __FUNCTION__); \
    exit(1); \
  } while (0)

static nr_pdcp_ue_manager_t *nr_pdcp_ue_manager;

Cedric Roux's avatar
Cedric Roux committed
53 54 55 56
/* TODO: handle time a bit more properly */
static uint64_t nr_pdcp_current_time;
static int      nr_pdcp_current_time_last_frame;
static int      nr_pdcp_current_time_last_subframe;
57 58 59 60 61

/* necessary globals for OAI, not used internally */
hash_table_t  *pdcp_coll_p;
static uint64_t pdcp_optmask;

Xue Song's avatar
Xue Song committed
62
uint8_t first_dcch = 0;
63
uint8_t proto_agent_flag = 0;
64

65 66
static ngran_node_t node_type;

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
nr_pdcp_entity_t *nr_pdcp_get_rb(nr_pdcp_ue_t *ue, int rb_id, bool srb_flag)
{
  nr_pdcp_entity_t *rb;

  if (srb_flag) {
    if (rb_id < 1 || rb_id > 2)
      rb = NULL;
    else
      rb = ue->srb[rb_id - 1];
  } else {
    if (rb_id < 1 || rb_id > MAX_DRBS_PER_UE)
      rb = NULL;
    else
      rb = ue->drb[rb_id - 1];
  }

  return rb;
}

86 87 88 89
/****************************************************************************/
/* rlc_data_req queue - begin                                               */
/****************************************************************************/

Laurent's avatar
Laurent committed
90

91 92 93 94 95
#include <pthread.h>

/* NR PDCP and RLC both use "big locks". In some cases a thread may do
 * lock(rlc) followed by lock(pdcp) (typically when running 'rx_sdu').
 * Another thread may first do lock(pdcp) and then lock(rlc) (typically
96
 * the GTP module calls 'nr_pdcp_data_req' that, in a previous implementation
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
 * was indirectly calling 'rlc_data_req' which does lock(rlc)).
 * To avoid the resulting deadlock it is enough to ensure that a call
 * to lock(pdcp) will never be followed by a call to lock(rlc). So,
 * here we chose to have a separate thread that deals with rlc_data_req,
 * out of the PDCP lock. Other solutions may be possible.
 * So instead of calling 'rlc_data_req' directly we have a queue and a
 * separate thread emptying it.
 */

typedef struct {
  protocol_ctxt_t ctxt_pP;
  srb_flag_t      srb_flagP;
  MBMS_flag_t     MBMS_flagP;
  rb_id_t         rb_idP;
  mui_t           muiP;
  confirm_t       confirmP;
  sdu_size_t      sdu_sizeP;
  mem_block_t     *sdu_pP;
} rlc_data_req_queue_item;

#define RLC_DATA_REQ_QUEUE_SIZE 10000

typedef struct {
  rlc_data_req_queue_item q[RLC_DATA_REQ_QUEUE_SIZE];
  volatile int start;
  volatile int length;
  pthread_mutex_t m;
  pthread_cond_t c;
} rlc_data_req_queue;

static rlc_data_req_queue q;

static void *rlc_data_req_thread(void *_)
{
  int i;

Laurent's avatar
Laurent committed
133
  pthread_setname_np(pthread_self(), "RLC queue");
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  while (1) {
    if (pthread_mutex_lock(&q.m) != 0) abort();
    while (q.length == 0)
      if (pthread_cond_wait(&q.c, &q.m) != 0) abort();
    i = q.start;
    if (pthread_mutex_unlock(&q.m) != 0) abort();

    rlc_data_req(&q.q[i].ctxt_pP,
                 q.q[i].srb_flagP,
                 q.q[i].MBMS_flagP,
                 q.q[i].rb_idP,
                 q.q[i].muiP,
                 q.q[i].confirmP,
                 q.q[i].sdu_sizeP,
                 q.q[i].sdu_pP,
                 NULL,
                 NULL);

    if (pthread_mutex_lock(&q.m) != 0) abort();

    q.length--;
    q.start = (q.start + 1) % RLC_DATA_REQ_QUEUE_SIZE;

    if (pthread_cond_signal(&q.c) != 0) abort();
    if (pthread_mutex_unlock(&q.m) != 0) abort();
  }
}

static void init_nr_rlc_data_req_queue(void)
{
  pthread_t t;

  pthread_mutex_init(&q.m, NULL);
  pthread_cond_init(&q.c, NULL);

  if (pthread_create(&t, NULL, rlc_data_req_thread, NULL) != 0) {
    LOG_E(PDCP, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__);
    exit(1);
  }
}

static void enqueue_rlc_data_req(const protocol_ctxt_t *const ctxt_pP,
                                 const srb_flag_t   srb_flagP,
                                 const MBMS_flag_t  MBMS_flagP,
                                 const rb_id_t      rb_idP,
                                 const mui_t        muiP,
                                 confirm_t    confirmP,
                                 sdu_size_t   sdu_sizeP,
Laurent Thomas's avatar
Laurent Thomas committed
182
                                 mem_block_t *sdu_pP)
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
  int i;
  int logged = 0;

  if (pthread_mutex_lock(&q.m) != 0) abort();
  while (q.length == RLC_DATA_REQ_QUEUE_SIZE) {
    if (!logged) {
      logged = 1;
      LOG_W(PDCP, "%s: rlc_data_req queue is full\n", __FUNCTION__);
    }
    if (pthread_cond_wait(&q.c, &q.m) != 0) abort();
  }

  i = (q.start + q.length) % RLC_DATA_REQ_QUEUE_SIZE;
  q.length++;

  q.q[i].ctxt_pP    = *ctxt_pP;
  q.q[i].srb_flagP  = srb_flagP;
  q.q[i].MBMS_flagP = MBMS_flagP;
  q.q[i].rb_idP     = rb_idP;
  q.q[i].muiP       = muiP;
  q.q[i].confirmP   = confirmP;
  q.q[i].sdu_sizeP  = sdu_sizeP;
  q.q[i].sdu_pP     = sdu_pP;

  if (pthread_cond_signal(&q.c) != 0) abort();
  if (pthread_mutex_unlock(&q.m) != 0) abort();
}

Xue Song's avatar
Xue Song committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
void du_rlc_data_req(const protocol_ctxt_t *const ctxt_pP,
                     const srb_flag_t   srb_flagP,
                     const MBMS_flag_t  MBMS_flagP,
                     const rb_id_t      rb_idP,
                     const mui_t        muiP,
                     confirm_t    confirmP,
                     sdu_size_t   sdu_sizeP,
                     mem_block_t *sdu_pP)
{
  enqueue_rlc_data_req(ctxt_pP,
                       srb_flagP,
                       MBMS_flagP,
                       rb_idP, muiP,
                       confirmP,
                       sdu_sizeP,
Laurent Thomas's avatar
Laurent Thomas committed
227
                       sdu_pP);
Xue Song's avatar
Xue Song committed
228 229
}

230 231 232 233
/****************************************************************************/
/* rlc_data_req queue - end                                                 */
/****************************************************************************/

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/****************************************************************************/
/* pdcp_data_ind thread - begin                                             */
/****************************************************************************/

typedef struct {
  protocol_ctxt_t ctxt_pP;
  srb_flag_t      srb_flagP;
  MBMS_flag_t     MBMS_flagP;
  rb_id_t         rb_id;
  sdu_size_t      sdu_buffer_size;
  mem_block_t     *sdu_buffer;
} pdcp_data_ind_queue_item;

#define PDCP_DATA_IND_QUEUE_SIZE 10000

typedef struct {
  pdcp_data_ind_queue_item q[PDCP_DATA_IND_QUEUE_SIZE];
  volatile int start;
  volatile int length;
  pthread_mutex_t m;
  pthread_cond_t c;
} pdcp_data_ind_queue;

static pdcp_data_ind_queue pq;

static void do_pdcp_data_ind(
  const protocol_ctxt_t *const  ctxt_pP,
  const srb_flag_t srb_flagP,
  const MBMS_flag_t MBMS_flagP,
  const rb_id_t rb_id,
  const sdu_size_t sdu_buffer_size,
  mem_block_t *const sdu_buffer)
{
  nr_pdcp_ue_t *ue;
  nr_pdcp_entity_t *rb;
269
  ue_id_t rntiMaybeUEid = ctxt_pP->rntiMaybeUEid;
270 271 272 273 274 275 276 277 278 279 280

  if (ctxt_pP->module_id != 0 ||
      //ctxt_pP->enb_flag != 1 ||
      ctxt_pP->instance != 0 ||
      ctxt_pP->eNB_index != 0 ||
      ctxt_pP->brOption != 0) {
    LOG_E(PDCP, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__);
    exit(1);
  }

  if (ctxt_pP->enb_flag)
281
    T(T_ENB_PDCP_UL, T_INT(ctxt_pP->module_id), T_INT(rntiMaybeUEid), T_INT(rb_id), T_INT(sdu_buffer_size));
282 283

  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
284
  ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, rntiMaybeUEid);
285
  rb = nr_pdcp_get_rb(ue, rb_id, srb_flagP);
286 287 288 289

  if (rb != NULL) {
    rb->recv_pdu(rb, (char *)sdu_buffer->data, sdu_buffer_size);
  } else {
290
    LOG_E(PDCP, "pdcp_data_ind: no RB found (rb_id %ld, srb_flag %d)\n", rb_id, srb_flagP);
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  }

  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);

  free_mem_block(sdu_buffer, __FUNCTION__);
}

static void *pdcp_data_ind_thread(void *_)
{
  int i;

  pthread_setname_np(pthread_self(), "PDCP data ind");
  while (1) {
    if (pthread_mutex_lock(&pq.m) != 0) abort();
    while (pq.length == 0)
      if (pthread_cond_wait(&pq.c, &pq.m) != 0) abort();
    i = pq.start;
    if (pthread_mutex_unlock(&pq.m) != 0) abort();

    do_pdcp_data_ind(&pq.q[i].ctxt_pP,
                     pq.q[i].srb_flagP,
                     pq.q[i].MBMS_flagP,
                     pq.q[i].rb_id,
                     pq.q[i].sdu_buffer_size,
                     pq.q[i].sdu_buffer);

    if (pthread_mutex_lock(&pq.m) != 0) abort();

    pq.length--;
    pq.start = (pq.start + 1) % PDCP_DATA_IND_QUEUE_SIZE;

    if (pthread_cond_signal(&pq.c) != 0) abort();
    if (pthread_mutex_unlock(&pq.m) != 0) abort();
  }
}

static void init_nr_pdcp_data_ind_queue(void)
{
  pthread_t t;

  pthread_mutex_init(&pq.m, NULL);
  pthread_cond_init(&pq.c, NULL);

  if (pthread_create(&t, NULL, pdcp_data_ind_thread, NULL) != 0) {
    LOG_E(PDCP, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__);
    exit(1);
  }
}

static void enqueue_pdcp_data_ind(
  const protocol_ctxt_t *const  ctxt_pP,
  const srb_flag_t srb_flagP,
  const MBMS_flag_t MBMS_flagP,
  const rb_id_t rb_id,
  const sdu_size_t sdu_buffer_size,
  mem_block_t *const sdu_buffer)
{
  int i;
  int logged = 0;

  if (pthread_mutex_lock(&pq.m) != 0) abort();
  while (pq.length == PDCP_DATA_IND_QUEUE_SIZE) {
    if (!logged) {
      logged = 1;
      LOG_W(PDCP, "%s: pdcp_data_ind queue is full\n", __FUNCTION__);
    }
    if (pthread_cond_wait(&pq.c, &pq.m) != 0) abort();
  }

  i = (pq.start + pq.length) % PDCP_DATA_IND_QUEUE_SIZE;
  pq.length++;

  pq.q[i].ctxt_pP         = *ctxt_pP;
  pq.q[i].srb_flagP       = srb_flagP;
  pq.q[i].MBMS_flagP      = MBMS_flagP;
  pq.q[i].rb_id           = rb_id;
  pq.q[i].sdu_buffer_size = sdu_buffer_size;
  pq.q[i].sdu_buffer      = sdu_buffer;

  if (pthread_cond_signal(&pq.c) != 0) abort();
  if (pthread_mutex_unlock(&pq.m) != 0) abort();
}

374 375 376 377 378 379 380 381
bool pdcp_data_ind(const protocol_ctxt_t *const  ctxt_pP,
                   const srb_flag_t srb_flagP,
                   const MBMS_flag_t MBMS_flagP,
                   const rb_id_t rb_id,
                   const sdu_size_t sdu_buffer_size,
                   mem_block_t *const sdu_buffer,
                   const uint32_t *const srcID,
                   const uint32_t *const dstID)
382 383 384 385 386 387 388 389 390 391 392 393 394 395
{
  enqueue_pdcp_data_ind(ctxt_pP,
                        srb_flagP,
                        MBMS_flagP,
                        rb_id,
                        sdu_buffer_size,
                        sdu_buffer);
  return true;
}

/****************************************************************************/
/* pdcp_data_ind thread - end                                               */
/****************************************************************************/

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
/****************************************************************************/
/* hacks to be cleaned up at some point - begin                             */
/****************************************************************************/

#include "LAYER2/MAC/mac_extern.h"

static void reblock_tun_socket(void)
{
  extern int nas_sock_fd[];
  int f;

  f = fcntl(nas_sock_fd[0], F_GETFL, 0);
  f &= ~(O_NONBLOCK);
  if (fcntl(nas_sock_fd[0], F_SETFL, f) == -1) {
    LOG_E(PDCP, "reblock_tun_socket failed\n");
    exit(1);
  }
}

static void *enb_tun_read_thread(void *_)
{
  extern int nas_sock_fd[];
  char rx_buf[NL_MAX_PAYLOAD];
  int len;
  protocol_ctxt_t ctxt;
421
  ue_id_t rntiMaybeUEid;
422

423
  int rb_id = 1;
Laurent's avatar
Laurent committed
424
  pthread_setname_np( pthread_self(),"enb_tun_read");
425 426 427 428 429 430 431 432

  while (1) {
    len = read(nas_sock_fd[0], &rx_buf, NL_MAX_PAYLOAD);
    if (len == -1) {
      LOG_E(PDCP, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__);
      exit(1);
    }

Robert Schmidt's avatar
Robert Schmidt committed
433
    LOG_D(PDCP, "%s(): nas_sock_fd read returns len %d\n", __func__, len);
434 435

    nr_pdcp_manager_lock(nr_pdcp_ue_manager);
436
    const bool has_ue = nr_pdcp_get_first_ue_id(nr_pdcp_ue_manager, &rntiMaybeUEid);
437 438
    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);

439
    if (!has_ue) continue;
440 441 442 443 444 445 446 447

    ctxt.module_id = 0;
    ctxt.enb_flag = 1;
    ctxt.instance = 0;
    ctxt.frame = 0;
    ctxt.subframe = 0;
    ctxt.eNB_index = 0;
    ctxt.brOption = 0;
448
    ctxt.rntiMaybeUEid = rntiMaybeUEid;
449

athanassopoulos's avatar
athanassopoulos committed
450
    uint8_t qfi = 7;
451
    bool rqi = 0;
athanassopoulos's avatar
athanassopoulos committed
452
    int pdusession_id = 10;
453

454
    sdap_data_req(&ctxt, rntiMaybeUEid, SRB_FLAG_NO, rb_id, RLC_MUI_UNDEFINED, RLC_SDU_CONFIRM_NO, len, (unsigned char *)rx_buf, PDCP_TRANSMISSION_MODE_DATA, NULL, NULL, qfi, rqi, pdusession_id);
455 456 457 458 459 460 461 462 463 464 465
  }

  return NULL;
}

static void *ue_tun_read_thread(void *_)
{
  extern int nas_sock_fd[];
  char rx_buf[NL_MAX_PAYLOAD];
  int len;
  protocol_ctxt_t ctxt;
466
  ue_id_t rntiMaybeUEid;
467
  int has_ue;
468

469
  int rb_id = 1;
Laurent's avatar
Laurent committed
470
  pthread_setname_np( pthread_self(),"ue_tun_read"); 
471 472 473 474 475 476 477
  while (1) {
    len = read(nas_sock_fd[0], &rx_buf, NL_MAX_PAYLOAD);
    if (len == -1) {
      LOG_E(PDCP, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__);
      exit(1);
    }

Robert Schmidt's avatar
Robert Schmidt committed
478
    LOG_D(PDCP, "%s(): nas_sock_fd read returns len %d\n", __func__, len);
479 480

    nr_pdcp_manager_lock(nr_pdcp_ue_manager);
481
    has_ue = nr_pdcp_get_first_ue_id(nr_pdcp_ue_manager, &rntiMaybeUEid);
482 483
    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);

484
    if (!has_ue) continue;
485 486 487 488 489 490 491 492

    ctxt.module_id = 0;
    ctxt.enb_flag = 0;
    ctxt.instance = 0;
    ctxt.frame = 0;
    ctxt.subframe = 0;
    ctxt.eNB_index = 0;
    ctxt.brOption = 0;
493
    ctxt.rntiMaybeUEid = rntiMaybeUEid;
494

495
    bool dc = SDAP_HDR_UL_DATA_PDU;
496 497
    extern uint8_t nas_qfi;
    extern uint8_t nas_pduid;
498

499
    sdap_data_req(&ctxt, rntiMaybeUEid, SRB_FLAG_NO, rb_id, RLC_MUI_UNDEFINED, RLC_SDU_CONFIRM_NO, len, (unsigned char *)rx_buf, PDCP_TRANSMISSION_MODE_DATA, NULL, NULL, nas_qfi, dc, nas_pduid);
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
  }

  return NULL;
}

static void start_pdcp_tun_enb(void)
{
  pthread_t t;

  reblock_tun_socket();

  if (pthread_create(&t, NULL, enb_tun_read_thread, NULL) != 0) {
    LOG_E(PDCP, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__);
    exit(1);
  }
}

static void start_pdcp_tun_ue(void)
{
  pthread_t t;

  reblock_tun_socket();

  if (pthread_create(&t, NULL, ue_tun_read_thread, NULL) != 0) {
    LOG_E(PDCP, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__);
    exit(1);
  }
}

/****************************************************************************/
/* hacks to be cleaned up at some point - end                               */
/****************************************************************************/

int pdcp_fifo_flush_sdus(const protocol_ctxt_t *const ctxt_pP)
{
  return 0;
}

538 539 540 541
static void set_node_type() {
  node_type = get_node_type();
}

542
/* hack: dummy function needed due to LTE dependencies */
543
void pdcp_layer_init(void)
544 545 546 547
{
  abort();
}

548
void nr_pdcp_layer_init(bool uses_e1)
549 550 551 552 553 554 555 556 557 558 559 560 561
{
  /* hack: be sure to initialize only once */
  static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  static int initialized = 0;
  if (pthread_mutex_lock(&m) != 0) abort();
  if (initialized) {
    if (pthread_mutex_unlock(&m) != 0) abort();
    return;
  }
  initialized = 1;
  if (pthread_mutex_unlock(&m) != 0) abort();

  nr_pdcp_ue_manager = new_nr_pdcp_ue_manager(1);
562

563 564 565
  set_node_type();

  if ((RC.nrrrc == NULL) || (!NODE_IS_CU(node_type))) {
566
    init_nr_rlc_data_req_queue();
Xue Song's avatar
Xue Song committed
567 568
  }

569
  nr_pdcp_e1_if_init(uses_e1);
570
  init_nr_pdcp_data_ind_queue();
571
  nr_pdcp_init_timer_thread(nr_pdcp_ue_manager);
Xue Song's avatar
Xue Song committed
572 573
}

574
#include "nfapi/oai_integration/vendor_ext.h"
575
#include "executables/lte-softmodem.h"
576 577
#include "openair2/RRC/NAS/nas_config.h"

578
uint64_t nr_pdcp_module_init(uint64_t _pdcp_optmask, int id)
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
{
  /* hack: be sure to initialize only once */
  static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  static int initialized = 0;
  if (pthread_mutex_lock(&m) != 0) abort();
  if (initialized) {
    abort();
  }
  initialized = 1;
  if (pthread_mutex_unlock(&m) != 0) abort();

#if 0
  pdcp_optmask = _pdcp_optmask;
  return pdcp_optmask;
#endif
  /* temporary enforce netlink when UE_NAS_USE_TUN is set,
     this is while switching from noS1 as build option
     to noS1 as config option                               */
  if ( _pdcp_optmask & UE_NAS_USE_TUN_BIT) {
    pdcp_optmask = pdcp_optmask | PDCP_USE_NETLINK_BIT ;
  }

  pdcp_optmask = pdcp_optmask | _pdcp_optmask ;
  LOG_I(PDCP, "pdcp init,%s %s\n",
        ((LINK_ENB_PDCP_TO_GTPV1U)?"usegtp":""),
        ((PDCP_USE_NETLINK)?"usenetlink":""));

  if (PDCP_USE_NETLINK) {
    nas_getparams();

    if(UE_NAS_USE_TUN) {
610
      char *ifsuffix_ue = get_softmodem_params()->nsa ? "nrue" : "ue";
611
      int num_if = (NFAPI_MODE == NFAPI_UE_STUB_PNF || IS_SOFTMODEM_SIML1 || NFAPI_MODE == NFAPI_MODE_STANDALONE_PNF)? MAX_MOBILES_PER_ENB : 1;
612
      netlink_init_tun(ifsuffix_ue, num_if, id);
613
      //Add --nr-ip-over-lte option check for next line
614 615 616 617
      if (IS_SOFTMODEM_NOS1){
        nas_config(1, 1, !get_softmodem_params()->nsa ? 2 : 3, ifsuffix_ue);
        set_qfi_pduid(7, 10);
      }
618 619 620
      LOG_I(PDCP, "UE pdcp will use tun interface\n");
      start_pdcp_tun_ue();
    } else if(ENB_NAS_USE_TUN) {
621 622 623
      char *ifsuffix_base_s = get_softmodem_params()->nsa ? "gnb" : "enb";
      netlink_init_tun(ifsuffix_base_s, 1, id);
      nas_config(1, 1, 1, ifsuffix_base_s);
624 625 626 627 628 629 630 631 632 633
      LOG_I(PDCP, "ENB pdcp will use tun interface\n");
      start_pdcp_tun_enb();
    } else {
      LOG_I(PDCP, "pdcp will use kernel modules\n");
      abort();
      netlink_init();
    }
  }
  return pdcp_optmask ;
}
Xue Song's avatar
Xue Song committed
634

635
static void deliver_sdu_drb(void *_ue, nr_pdcp_entity_t *entity,
636 637
                            char *buf, int size)
{
638 639 640 641
  nr_pdcp_ue_t *ue = _ue;
  int rb_id;
  int i;

642
  if (IS_SOFTMODEM_NOS1 || UE_NAS_USE_TUN) {
643
    LOG_D(PDCP, "IP packet received with size %d, to be sent to SDAP interface, UE ID/RNTI: %ld\n", size, ue->rntiMaybeUEid);
644 645 646
    // in NoS1 mode: the SDAP should write() packets to an FD (TUN interface),
    // so below, set is_gnb == 0 to do that
    sdap_data_ind(entity->rb_id, 0, entity->has_sdap_rx, entity->pdusession_id, ue->rntiMaybeUEid, buf, size);
647 648
  }
  else{
649
    for (i = 0; i < MAX_DRBS_PER_UE; i++) {
650 651 652 653 654 655
        if (entity == ue->drb[i]) {
          rb_id = i+1;
          goto rb_found;
        }
      }

656
      LOG_E(PDCP, "%s:%d:%s: fatal, no RB found for UE ID/RNTI %ld\n", __FILE__, __LINE__, __FUNCTION__, ue->rntiMaybeUEid);
657 658 659
      exit(1);

    rb_found:
660
    {
661
      LOG_D(PDCP, "%s() (drb %d) sending message to SDAP size %d\n", __func__, rb_id, size);
Cedric Roux's avatar
Cedric Roux committed
662
      sdap_data_ind(rb_id, ue->drb[rb_id - 1]->is_gnb, ue->drb[rb_id - 1]->has_sdap_rx, ue->drb[rb_id - 1]->pdusession_id, ue->rntiMaybeUEid, buf, size);
663
    }
664 665 666
  }
}

667 668
static void deliver_pdu_drb_ue(void *deliver_pdu_data, ue_id_t ue_id, int rb_id,
                               char *buf, int size, int sdu_id)
669
{
670
  DevAssert(deliver_pdu_data == NULL);
671 672 673 674 675 676 677 678 679 680 681 682 683 684
  protocol_ctxt_t ctxt = { .enb_flag = 0, .rntiMaybeUEid = ue_id };

  mem_block_t *memblock = get_free_mem_block(size, __FUNCTION__);
  memcpy(memblock->data, buf, size);
  LOG_D(PDCP, "%s(): (drb %d) calling rlc_data_req size %d UE %ld/%04lx\n", __func__, rb_id, size, ctxt.rntiMaybeUEid, ctxt.rntiMaybeUEid);
  enqueue_rlc_data_req(&ctxt, 0, MBMS_FLAG_NO, rb_id, sdu_id, 0, size, memblock);
}

static void deliver_pdu_drb_gnb(void *deliver_pdu_data, ue_id_t ue_id, int rb_id,
                                char *buf, int size, int sdu_id)
{
  DevAssert(deliver_pdu_data == NULL);
  f1_ue_data_t ue_data = cu_get_f1_ue_data(ue_id);
  protocol_ctxt_t ctxt = { .enb_flag = 1, .rntiMaybeUEid = ue_data.secondary_ue };
685

686
  if (NODE_IS_CU(node_type)) {
Laurent THOMAS's avatar
Laurent THOMAS committed
687
    MessageDef  *message_p = itti_alloc_new_message_sized(TASK_PDCP_ENB, 0,
688 689
							  GTPV1U_TUNNEL_DATA_REQ,
							  sizeof(gtpv1u_tunnel_data_req_t)
Laurent THOMAS's avatar
Laurent THOMAS committed
690 691 692
							  + size
							  + GTPU_HEADER_OVERHEAD_MAX);
    AssertFatal(message_p != NULL, "OUT OF MEMORY");
693
    gtpv1u_tunnel_data_req_t *req=&GTPV1U_TUNNEL_DATA_REQ(message_p);
Laurent THOMAS's avatar
Laurent THOMAS committed
694
    uint8_t *gtpu_buffer_p = (uint8_t*)(req+1);
695
    memcpy(gtpu_buffer_p + GTPU_HEADER_OVERHEAD_MAX, buf, size);
Laurent THOMAS's avatar
Laurent THOMAS committed
696 697 698
    req->buffer        = gtpu_buffer_p;
    req->length        = size;
    req->offset        = GTPU_HEADER_OVERHEAD_MAX;
699
    req->ue_id = ue_id; // use CU UE ID as GTP will use that to look up TEID
700
    req->bearer_id = rb_id;
Robert Schmidt's avatar
Robert Schmidt committed
701
    LOG_D(PDCP, "%s() (drb %d) sending message to gtp size %d\n", __func__, rb_id, size);
Laurent THOMAS's avatar
Laurent THOMAS committed
702
    extern instance_t CUuniqInstance;
Laurent THOMAS's avatar
Laurent THOMAS committed
703
    itti_send_msg_to_task(TASK_GTPV1_U, CUuniqInstance, message_p);
704
  } else {
705
    mem_block_t *memblock = get_free_mem_block(size, __FUNCTION__);
706
    memcpy(memblock->data, buf, size);
Angelo's avatar
Angelo committed
707
    LOG_D(PDCP, "%s(): (drb %d) calling rlc_data_req size %d\n", __func__, rb_id, size);
708 709
    enqueue_rlc_data_req(&ctxt, 0, MBMS_FLAG_NO, rb_id, sdu_id, 0, size, memblock);
  }
710 711
}

712 713 714 715 716 717 718
static void deliver_sdu_srb(void *_ue, nr_pdcp_entity_t *entity,
                            char *buf, int size)
{
  nr_pdcp_ue_t *ue = _ue;
  int srb_id;
  int i;

Laurent THOMAS's avatar
Laurent THOMAS committed
719
  for (i = 0; i < sizeofArray(ue->srb) ; i++) {
720 721 722 723 724 725
    if (entity == ue->srb[i]) {
      srb_id = i+1;
      goto srb_found;
    }
  }

726
  LOG_E(PDCP, "%s:%d:%s: fatal, no SRB found for UE ID/RNTI %ld\n", __FILE__, __LINE__, __FUNCTION__, ue->rntiMaybeUEid);
727
  exit(1);
728

729 730 731 732 733
srb_found:
  if (entity->is_gnb) {
    MessageDef *message_p = itti_alloc_new_message(TASK_PDCP_GNB, 0, F1AP_UL_RRC_MESSAGE);
    AssertFatal(message_p != NULL, "OUT OF MEMORY\n");
    f1ap_ul_rrc_message_t *ul_rrc = &F1AP_UL_RRC_MESSAGE(message_p);
734 735 736 737 738
    ul_rrc->gNB_CU_ue_id = ue->rntiMaybeUEid;
    /* look up the correct secondary UE ID to provide complete information to
     * RRC, the RLC-PDCP interface does not transport this information */
    f1_ue_data_t ue_data = cu_get_f1_ue_data(ue->rntiMaybeUEid);
    ul_rrc->gNB_DU_ue_id = ue_data.secondary_ue;
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    ul_rrc->srb_id = srb_id;
    ul_rrc->rrc_container = malloc(size);
    AssertFatal(ul_rrc->rrc_container != NULL, "OUT OF MEMORY\n");
    memcpy(ul_rrc->rrc_container, buf, size);
    ul_rrc->rrc_container_length = size;
    itti_send_msg_to_task(TASK_RRC_GNB, 0, message_p);
  } else {
    uint8_t *rrc_buffer_p = itti_malloc(TASK_PDCP_UE, TASK_RRC_NRUE, size);
    AssertFatal(rrc_buffer_p != NULL, "OUT OF MEMORY\n");
    memcpy(rrc_buffer_p, buf, size);
    MessageDef *message_p = itti_alloc_new_message(TASK_PDCP_UE, 0, NR_RRC_DCCH_DATA_IND);
    AssertFatal(message_p != NULL, "OUT OF MEMORY\n");
    NR_RRC_DCCH_DATA_IND(message_p).dcch_index = srb_id;
    NR_RRC_DCCH_DATA_IND(message_p).sdu_p = rrc_buffer_p;
    NR_RRC_DCCH_DATA_IND(message_p).sdu_size = size;
754
    NR_RRC_DCCH_DATA_IND(message_p).rnti = ue->rntiMaybeUEid;
755 756
    itti_send_msg_to_task(TASK_RRC_NRUE, 0, message_p);
  }
757 758
}

759 760
void deliver_pdu_srb_rlc(void *deliver_pdu_data, ue_id_t ue_id, int srb_id,
                         char *buf, int size, int sdu_id)
761
{
762 763 764 765 766 767
  protocol_ctxt_t ctxt = { .enb_flag = 1, .rntiMaybeUEid = ue_id };
  mem_block_t *memblock = get_free_mem_block(size, __FUNCTION__);
  memcpy(memblock->data, buf, size);
  enqueue_rlc_data_req(&ctxt, 1, MBMS_FLAG_NO, srb_id, sdu_id, 0, size, memblock);
}

768 769 770 771 772 773 774
void add_srb(int is_gnb,
             ue_id_t rntiMaybeUEid,
             struct NR_SRB_ToAddMod *s,
             int ciphering_algorithm,
             int integrity_algorithm,
             unsigned char *ciphering_key,
             unsigned char *integrity_key)
775
{
776 777 778 779
  nr_pdcp_entity_t *pdcp_srb;
  nr_pdcp_ue_t *ue;

  int srb_id = s->srb_Identity;
780
  int t_Reordering = -1; // infinity as per default SRB configuration in 9.2.1 of 38.331
francescomani's avatar
francescomani committed
781
  if (s->pdcp_Config != NULL && s->pdcp_Config->t_Reordering != NULL)
782
    t_Reordering = decode_t_reordering(*s->pdcp_Config->t_Reordering);
783 784

  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
785
  ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, rntiMaybeUEid);
786 787
  if (nr_pdcp_get_rb(ue, srb_id, true) != NULL) {
    LOG_E(PDCP, "warning SRB %d already exist for UE ID/RNTI %ld, do nothing\n", srb_id, rntiMaybeUEid);
788
  } else {
789
    pdcp_srb = new_nr_pdcp_entity(NR_PDCP_SRB, is_gnb, srb_id,
Cedric Roux's avatar
Cedric Roux committed
790
                                  0, false, false, // sdap parameters
791
                                  deliver_sdu_srb, ue, NULL, ue,
792
                                  12, t_Reordering, -1,
Cedric Roux's avatar
Cedric Roux committed
793 794 795 796
                                  ciphering_algorithm,
                                  integrity_algorithm,
                                  ciphering_key,
                                  integrity_key);
797 798
    nr_pdcp_ue_add_srb_pdcp_entity(ue, srb_id, pdcp_srb);

799
    LOG_D(PDCP, "%s:%d:%s: added srb %d to UE ID/RNTI %ld\n", __FILE__, __LINE__, __FUNCTION__, srb_id, rntiMaybeUEid);
800 801
  }
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
802 803
}

francescomani's avatar
francescomani committed
804 805
void add_drb(int is_gnb,
             ue_id_t rntiMaybeUEid,
806 807 808 809 810
             struct NR_DRB_ToAddMod *s,
             int ciphering_algorithm,
             int integrity_algorithm,
             unsigned char *ciphering_key,
             unsigned char *integrity_key)
811 812 813 814 815
{
  nr_pdcp_entity_t *pdcp_drb;
  nr_pdcp_ue_t *ue;

  int drb_id = s->drb_Identity;
Cedric Roux's avatar
Cedric Roux committed
816 817 818 819
  int sn_size_ul = decode_sn_size_ul(*s->pdcp_Config->drb->pdcp_SN_SizeUL);
  int sn_size_dl = decode_sn_size_dl(*s->pdcp_Config->drb->pdcp_SN_SizeDL);
  int discard_timer = decode_discard_timer(*s->pdcp_Config->drb->discardTimer);

Cedric Roux's avatar
Cedric Roux committed
820 821 822
  int has_integrity;
  int has_ciphering;

823
  /* if pdcp_Config->t_Reordering is not present, it means infinity (-1) */
824 825
  int t_reordering = -1;
  if (s->pdcp_Config->t_Reordering != NULL) {
826
    t_reordering = decode_t_reordering(*s->pdcp_Config->t_Reordering);
827
  }
828

Cedric Roux's avatar
Cedric Roux committed
829 830 831 832 833 834 835 836 837 838 839 840
  if (s->pdcp_Config->drb != NULL
      && s->pdcp_Config->drb->integrityProtection != NULL)
    has_integrity = 1;
  else
    has_integrity = 0;

  if (s->pdcp_Config->ext1 != NULL
     && s->pdcp_Config->ext1->cipheringDisabled != NULL)
    has_ciphering = 0;
  else
    has_ciphering = 1;

841
  if ((!s->cnAssociation) || s->cnAssociation->present == NR_DRB_ToAddMod__cnAssociation_PR_NOTHING) {
842 843 844
    LOG_E(PDCP,"%s:%d:%s: fatal, cnAssociation is missing or present is NR_DRB_ToAddMod__cnAssociation_PR_NOTHING\n",__FILE__,__LINE__,__FUNCTION__);
    exit(-1);
  }
845 846

  int pdusession_id;
Cedric Roux's avatar
Cedric Roux committed
847 848
  bool has_sdap_rx = false;
  bool has_sdap_tx = false;
849
  bool is_sdap_DefaultDRB = false;
850 851
  NR_QFI_t *mappedQFIs2Add = NULL;
  uint8_t mappedQFIs2AddCount=0;
852 853 854 855 856 857 858 859
  if (s->cnAssociation->present == NR_DRB_ToAddMod__cnAssociation_PR_eps_BearerIdentity)
     pdusession_id = s->cnAssociation->choice.eps_BearerIdentity;
  else {
    if (!s->cnAssociation->choice.sdap_Config) {
      LOG_E(PDCP,"%s:%d:%s: fatal, sdap_Config is null",__FILE__,__LINE__,__FUNCTION__);
      exit(-1);
    }
    pdusession_id = s->cnAssociation->choice.sdap_Config->pdu_Session;
Cedric Roux's avatar
Cedric Roux committed
860 861 862 863 864 865 866
    if (is_gnb) {
      has_sdap_rx = s->cnAssociation->choice.sdap_Config->sdap_HeaderUL == NR_SDAP_Config__sdap_HeaderUL_present;
      has_sdap_tx = s->cnAssociation->choice.sdap_Config->sdap_HeaderDL == NR_SDAP_Config__sdap_HeaderDL_present;
    } else {
      has_sdap_tx = s->cnAssociation->choice.sdap_Config->sdap_HeaderUL == NR_SDAP_Config__sdap_HeaderUL_present;
      has_sdap_rx = s->cnAssociation->choice.sdap_Config->sdap_HeaderDL == NR_SDAP_Config__sdap_HeaderDL_present;
    }
867 868 869 870
    is_sdap_DefaultDRB = s->cnAssociation->choice.sdap_Config->defaultDRB == true ? 1 : 0;
    mappedQFIs2Add = (NR_QFI_t*)s->cnAssociation->choice.sdap_Config->mappedQoS_FlowsToAdd->list.array[0]; 
    mappedQFIs2AddCount = s->cnAssociation->choice.sdap_Config->mappedQoS_FlowsToAdd->list.count;
    LOG_D(SDAP, "Captured mappedQoS_FlowsToAdd from RRC: %ld \n", *mappedQFIs2Add);
871
  }
Cedric Roux's avatar
Cedric Roux committed
872 873 874 875 876 877
  /* TODO(?): accept different UL and DL SN sizes? */
  if (sn_size_ul != sn_size_dl) {
    LOG_E(PDCP, "%s:%d:%s: fatal, bad SN sizes, must be same. ul=%d, dl=%d\n",
          __FILE__, __LINE__, __FUNCTION__, sn_size_ul, sn_size_dl);
    exit(1);
  }
878 879 880


  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
881
  ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, rntiMaybeUEid);
882 883
  if (nr_pdcp_get_rb(ue, drb_id, false) != NULL) {
    LOG_W(PDCP, "warning DRB %d already exist for UE ID/RNTI %ld, do nothing\n", drb_id, rntiMaybeUEid);
884
  } else {
Cedric Roux's avatar
Cedric Roux committed
885
    pdcp_drb = new_nr_pdcp_entity(NR_PDCP_DRB_AM, is_gnb, drb_id, pdusession_id,
886 887 888 889
                                  has_sdap_rx, has_sdap_tx, deliver_sdu_drb, ue,
                                  is_gnb ?
                                    deliver_pdu_drb_gnb : deliver_pdu_drb_ue,
                                  ue,
Cedric Roux's avatar
Cedric Roux committed
890
                                  sn_size_dl, t_reordering, discard_timer,
Cedric Roux's avatar
Cedric Roux committed
891 892 893 894
                                  has_ciphering ? ciphering_algorithm : 0,
                                  has_integrity ? integrity_algorithm : 0,
                                  has_ciphering ? ciphering_key : NULL,
                                  has_integrity ? integrity_key : NULL);
895 896
    nr_pdcp_ue_add_drb_pdcp_entity(ue, drb_id, pdcp_drb);

897
    LOG_D(PDCP, "%s:%d:%s: added drb %d to UE ID/RNTI %ld\n", __FILE__, __LINE__, __FUNCTION__, drb_id, rntiMaybeUEid);
898

Cedric Roux's avatar
Cedric Roux committed
899
    new_nr_sdap_entity(is_gnb, has_sdap_rx, has_sdap_tx, rntiMaybeUEid, pdusession_id, is_sdap_DefaultDRB, drb_id, mappedQFIs2Add, mappedQFIs2AddCount);
900 901 902 903
  }
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
}

904 905
void nr_pdcp_add_srbs(eNB_flag_t enb_flag, ue_id_t rntiMaybeUEid, NR_SRB_ToAddModList_t *const srb2add_list, const uint8_t security_modeP, uint8_t *const kRRCenc, uint8_t *const kRRCint)
{
906
  if (srb2add_list != NULL) {
907
    for (int i = 0; i < srb2add_list->list.count; i++) {
908
      add_srb(enb_flag, rntiMaybeUEid, srb2add_list->list.array[i], security_modeP & 0x0f, (security_modeP >> 4) & 0x0f, kRRCenc, kRRCint);
909
    }
910 911 912 913
  } else
    LOG_W(PDCP, "nr_pdcp_add_srbs() with void list\n");
}

914 915 916 917 918
void nr_pdcp_add_drbs(eNB_flag_t enb_flag,
                      ue_id_t rntiMaybeUEid,
                      NR_DRB_ToAddModList_t *const drb2add_list,
                      const uint8_t security_modeP,
                      uint8_t *const kUPenc,
919
                      uint8_t *const kUPint)
920
{
921
  if (drb2add_list != NULL) {
922
    for (int i = 0; i < drb2add_list->list.count; i++) {
francescomani's avatar
francescomani committed
923 924 925 926 927 928 929
      add_drb(enb_flag,
              rntiMaybeUEid,
              drb2add_list->list.array[i],
              security_modeP & 0x0f,
              (security_modeP >> 4) & 0x0f,
              kUPenc,
              kUPint);
930
    }
931 932
  } else
    LOG_W(PDCP, "nr_pdcp_add_drbs() with void list\n");
933 934
}

935
/* Dummy function due to dependency from LTE libraries */
936 937 938 939 940 941 942 943 944 945
bool rrc_pdcp_config_asn1_req(const protocol_ctxt_t *const  ctxt_pP,
                              LTE_SRB_ToAddModList_t  *const srb2add_list,
                              LTE_DRB_ToAddModList_t  *const drb2add_list,
                              LTE_DRB_ToReleaseList_t *const drb2release_list,
                              const uint8_t                   security_modeP,
                              uint8_t                  *const kRRCenc,
                              uint8_t                  *const kRRCint,
                              uint8_t                  *const kUPenc,
                              LTE_PMCH_InfoList_r9_t  *pmch_InfoList_r9,
                              rb_id_t                 *const defaultDRB)
946 947 948 949 950 951 952 953 954
{
  return 0;
}

uint64_t get_pdcp_optmask(void)
{
  return pdcp_optmask;
}

955
/* hack: dummy function needed due to LTE dependencies */
956
bool pdcp_remove_UE(const protocol_ctxt_t *const ctxt_pP)
957
{
958 959
  abort();
}
Cedric Roux's avatar
Cedric Roux committed
960

961 962
bool nr_pdcp_remove_UE(ue_id_t ue_id)
{
Cedric Roux's avatar
Cedric Roux committed
963
  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
964
  nr_pdcp_manager_remove_ue(nr_pdcp_ue_manager, ue_id);
Cedric Roux's avatar
Cedric Roux committed
965 966
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);

967 968 969
  return 1;
}

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
/* hack: dummy function needed due to LTE dependencies */
void pdcp_config_set_security(const protocol_ctxt_t *const ctxt_pP,
                                 pdcp_t *const pdcp_pP,
                                 const rb_id_t rb_id,
                                 const uint16_t lc_idP,
                                 const uint8_t security_modeP,
                                 uint8_t *const kRRCenc_pP,
                                 uint8_t *const kRRCint_pP,
                                 uint8_t *const kUPenc_pP)
{
  abort();
}

void nr_pdcp_config_set_security(ue_id_t ue_id,
                                 const rb_id_t rb_id,
                                 const uint8_t security_modeP,
                                 uint8_t *const kRRCenc_pP,
                                 uint8_t *const kRRCint_pP,
                                 uint8_t *const kUPenc_pP)
989
{
990 991 992 993 994 995 996
  nr_pdcp_ue_t *ue;
  nr_pdcp_entity_t *rb;
  int integrity_algorithm;
  int ciphering_algorithm;

  nr_pdcp_manager_lock(nr_pdcp_ue_manager);

997
  ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
998 999 1000

  /* TODO: proper handling of DRBs, for the moment only SRBs are handled */

1001
  rb = nr_pdcp_get_rb(ue, rb_id, true);
1002

1003 1004 1005 1006
  if (rb == NULL) {
    LOG_E(PDCP, "no SRB found (ue_id %ld, rb_id %ld)\n", ue_id, rb_id);
    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
    return;
1007 1008
  }

1009 1010 1011 1012 1013 1014
  integrity_algorithm = (security_modeP>>4) & 0xf;
  ciphering_algorithm = security_modeP & 0x0f;
  rb->set_security(rb, integrity_algorithm, (char *)kRRCint_pP,
                   ciphering_algorithm, (char *)kRRCenc_pP);
  rb->security_mode_completed = false;

1015
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
1016 1017
}

1018 1019 1020 1021
bool nr_pdcp_data_req_srb(ue_id_t ue_id,
                          const rb_id_t rb_id,
                          const mui_t muiP,
                          const sdu_size_t sdu_buffer_size,
1022 1023 1024
                          unsigned char *const sdu_buffer,
                          deliver_pdu deliver_pdu_cb,
                          void *data)
1025 1026 1027 1028 1029 1030 1031
{
  LOG_D(PDCP, "%s() called, size %d\n", __func__, sdu_buffer_size);
  nr_pdcp_ue_t *ue;
  nr_pdcp_entity_t *rb;

  nr_pdcp_manager_lock(nr_pdcp_ue_manager);

1032
  ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
1033
  rb = nr_pdcp_get_rb(ue, rb_id, true);
1034 1035

  if (rb == NULL) {
1036
    LOG_E(PDCP, "no SRB found (ue_id %ld, rb_id %ld)\n", ue_id, rb_id);
1037 1038 1039 1040
    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
    return 0;
  }

1041 1042 1043
  int max_size = sdu_buffer_size + 3 + 4; // 3: max header, 4: max integrity
  char pdu_buf[max_size];
  int pdu_size = rb->process_sdu(rb, (char *)sdu_buffer, sdu_buffer_size, muiP, pdu_buf, max_size);
1044
  AssertFatal(rb->deliver_pdu == NULL, "SRB callback should be NULL, to be provided on every invocation\n");
1045 1046 1047

  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);

1048
  deliver_pdu_cb(data, ue_id, rb_id, pdu_buf, pdu_size, muiP);
1049

1050 1051 1052
  return 1;
}

1053 1054 1055 1056
void nr_pdcp_suspend_srb(ue_id_t ue_id, int srb_id)
{
  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
  nr_pdcp_ue_t *ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
1057
  nr_pdcp_entity_t *srb = nr_pdcp_get_rb(ue, srb_id, true);
1058
  if (srb == NULL) {
1059 1060
    LOG_E(PDCP, "Trying to suspend SRB with ID %d but it is not established\n", srb_id);
    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    return;
  }
  srb->suspend_entity(srb);
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
}

void nr_pdcp_suspend_drb(ue_id_t ue_id, int drb_id)
{
  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
  nr_pdcp_ue_t *ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
1071
  nr_pdcp_entity_t *drb = nr_pdcp_get_rb(ue, drb_id, false);
1072
  if (drb == NULL) {
1073 1074
    LOG_E(PDCP, "Trying to suspend DRB with ID %d but it is not established\n", drb_id);
    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
1075 1076 1077 1078 1079 1080
    return;
  }
  drb->suspend_entity(drb);
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
}

francescomani's avatar
francescomani committed
1081
void nr_pdcp_reconfigure_srb(ue_id_t ue_id, int srb_id, long t_Reordering)
1082 1083 1084
{
  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
  nr_pdcp_ue_t *ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
1085 1086 1087 1088 1089 1090
  nr_pdcp_entity_t *srb = nr_pdcp_get_rb(ue, srb_id, true);
  if (srb == NULL) {
    LOG_E(PDCP, "Trying to reconfigure SRB with ID %d but it is not established\n", srb_id);
    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
    return;
  }
1091 1092 1093
  int decoded_t_reordering = decode_t_reordering(t_Reordering);
  srb->t_reordering = decoded_t_reordering;
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
1094 1095
}

francescomani's avatar
francescomani committed
1096
void nr_pdcp_reconfigure_drb(ue_id_t ue_id, int drb_id, long t_Reordering)
1097
{
1098 1099 1100 1101
  /* The enabling/disabling of ciphering or integrity protection
   * can be changed only by releasing and adding the DRB
   * (so not by reconfiguring).
   */
1102 1103
  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
  nr_pdcp_ue_t *ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
1104 1105 1106 1107 1108 1109
  nr_pdcp_entity_t *drb = nr_pdcp_get_rb(ue, drb_id, false);
  if (drb == NULL) {
    LOG_E(PDCP, "Trying to reconfigure DRB with ID %d but it is not established\n", drb_id);
    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
    return;
  }
1110 1111 1112 1113
  int decoded_t_reordering = decode_t_reordering(t_Reordering);
  drb->t_reordering = decoded_t_reordering;
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
}
1114

1115
void nr_pdcp_release_srb(ue_id_t ue_id, int srb_id)
francescomani's avatar
francescomani committed
1116 1117 1118
{
  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
  nr_pdcp_ue_t *ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
1119
  if (ue->srb[srb_id - 1] != NULL) {
francescomani's avatar
francescomani committed
1120
    ue->srb[srb_id - 1]->delete_entity(ue->srb[srb_id - 1]);
1121 1122
    ue->srb[srb_id - 1] = NULL;
  }
francescomani's avatar
francescomani committed
1123 1124 1125 1126 1127
  else
    LOG_E(PDCP, "Attempting to release SRB%d but it is not configured\n", srb_id);
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
}

1128
void nr_pdcp_release_drb(ue_id_t ue_id, int drb_id)
francescomani's avatar
francescomani committed
1129 1130 1131 1132 1133 1134 1135
{
  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
  nr_pdcp_ue_t *ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
  nr_pdcp_entity_t *drb = ue->drb[drb_id - 1];
  if (drb) {
    drb->release_entity(drb);
    drb->delete_entity(drb);
1136
    ue->drb[drb_id - 1] = NULL;
francescomani's avatar
francescomani committed
1137 1138 1139 1140 1141 1142
  }
  else
    LOG_E(PDCP, "Attempting to release DRB%d but it is not configured\n", drb_id);
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
}

1143
void nr_pdcp_reestablishment(ue_id_t ue_id, int rb_id, bool srb_flag)
1144
{
1145 1146 1147
  nr_pdcp_ue_t     *ue;
  nr_pdcp_entity_t *rb;

1148
  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
1149
  ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
1150
  rb = nr_pdcp_get_rb(ue, rb_id, srb_flag);
1151

1152 1153 1154 1155
  if (rb != NULL) {
    rb->reestablish_entity(rb);
  } else {
    LOG_W(PDCP, "UE %4.4lx cannot re-establish RB %d (is_srb %d), RB not found\n", ue_id, rb_id, srb_flag);
1156
  }
1157

1158 1159 1160
  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
}

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
bool nr_pdcp_data_req_drb(protocol_ctxt_t *ctxt_pP,
                          const srb_flag_t srb_flagP,
                          const rb_id_t rb_id,
                          const mui_t muiP,
                          const confirm_t confirmP,
                          const sdu_size_t sdu_buffer_size,
                          unsigned char *const sdu_buffer,
                          const pdcp_transmission_mode_t mode,
                          const uint32_t *const sourceL2Id,
                          const uint32_t *const destinationL2Id)
1171
{
1172 1173
  DevAssert(srb_flagP == SRB_FLAG_NO);

1174
  LOG_D(PDCP, "%s() called, size %d\n", __func__, sdu_buffer_size);
1175 1176
  nr_pdcp_ue_t *ue;
  nr_pdcp_entity_t *rb;
1177
  ue_id_t ue_id = ctxt_pP->rntiMaybeUEid;
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190

  if (ctxt_pP->module_id != 0 ||
      //ctxt_pP->enb_flag != 1 ||
      ctxt_pP->instance != 0 ||
      ctxt_pP->eNB_index != 0 /*||
      ctxt_pP->configured != 1 ||
      ctxt_pP->brOption != 0*/) {
    LOG_E(PDCP, "%s:%d:%s: fatal\n", __FILE__, __LINE__, __FUNCTION__);
    exit(1);
  }

  nr_pdcp_manager_lock(nr_pdcp_ue_manager);

1191
  ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
1192
  rb = nr_pdcp_get_rb(ue, rb_id, false);
1193 1194

  if (rb == NULL) {
1195
    LOG_E(PDCP, "no DRB found (ue_id %lx, rb_id %ld)\n", ue_id, rb_id);
1196
    nr_pdcp_manager_unlock(nr_pdcp_ue_manager);
Cedric Roux's avatar
Cedric Roux committed
1197
    return 0;
1198 1199
  }

1200 1201 1202 1203
  int max_size = sdu_buffer_size + 3 + 4; // 3: max header, 4: max integrity
  char pdu_buf[max_size];
  int pdu_size = rb->process_sdu(rb, (char *)sdu_buffer, sdu_buffer_size, muiP, pdu_buf, max_size);
  deliver_pdu deliver_pdu_cb = rb->deliver_pdu;
1204 1205 1206

  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);

1207
  deliver_pdu_cb(NULL, ue_id, rb_id, pdu_buf, pdu_size, muiP);
1208

1209 1210 1211
  return 1;
}

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
bool cu_f1u_data_req(protocol_ctxt_t  *ctxt_pP,
                     const srb_flag_t srb_flagP,
                     const rb_id_t rb_id,
                     const mui_t muiP,
                     const confirm_t confirmP,
                     const sdu_size_t sdu_buffer_size,
                     unsigned char *const sdu_buffer,
                     const pdcp_transmission_mode_t mode,
                     const uint32_t *const sourceL2Id,
                     const uint32_t *const destinationL2Id) {
Laurent THOMAS's avatar
Laurent THOMAS committed
1222 1223 1224 1225 1226 1227 1228 1229
  //Force instance id to 0, OAI incoherent instance management
  ctxt_pP->instance=0;
  mem_block_t *memblock = get_free_mem_block(sdu_buffer_size, __func__);
  if (memblock == NULL) {
    LOG_E(RLC, "%s:%d:%s: ERROR: get_free_mem_block failed\n", __FILE__, __LINE__, __FUNCTION__);
    exit(1);
  }
  memcpy(memblock->data,sdu_buffer, sdu_buffer_size);
1230
  int ret=pdcp_data_ind(ctxt_pP,srb_flagP, false, rb_id, sdu_buffer_size, memblock, NULL, NULL);
Laurent THOMAS's avatar
Laurent THOMAS committed
1231 1232 1233 1234 1235
  if (!ret) {
    LOG_E(RLC, "%s:%d:%s: ERROR: pdcp_data_ind failed\n", __FILE__, __LINE__, __FUNCTION__);
    /* what to do in case of failure? for the moment: nothing */
  }
  return ret;
1236 1237
}

1238
/* hack: dummy function needed due to LTE dependencies */
1239
bool pdcp_data_req(protocol_ctxt_t  *ctxt_pP,
1240 1241 1242 1243 1244 1245 1246
                   const srb_flag_t     srb_flagP,
                   const rb_id_t        rb_idP,
                   const mui_t          muiP,
                   const confirm_t      confirmP,
                   const sdu_size_t     sdu_buffer_sizeP,
                   unsigned char *const sdu_buffer_pP,
                   const pdcp_transmission_mode_t modeP,
1247 1248
                   const uint32_t *const sourceL2Id,
                   const uint32_t *const destinationL2Id)
1249
{
1250 1251
  abort();
  return false;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
}

void pdcp_set_pdcp_data_ind_func(pdcp_data_ind_func_t pdcp_data_ind)
{
  /* nothing to do */
}

void pdcp_set_rlc_data_req_func(send_rlc_data_req_func_t send_rlc_data_req)
{
  /* nothing to do */
}

//Dummy function needed due to LTE dependencies
void
pdcp_mbms_run ( const protocol_ctxt_t *const  ctxt_pP){
  /* nothing to do */
}
Cedric Roux's avatar
Cedric Roux committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279

void nr_pdcp_tick(int frame, int subframe)
{
  if (frame != nr_pdcp_current_time_last_frame ||
      subframe != nr_pdcp_current_time_last_subframe) {
    nr_pdcp_current_time_last_frame = frame;
    nr_pdcp_current_time_last_subframe = subframe;
    nr_pdcp_current_time++;
    nr_pdcp_wakeup_timer_thread(nr_pdcp_current_time);
  }
}
1280

athanassopoulos's avatar
athanassopoulos committed
1281 1282 1283
/*
 * For the SDAP API
 */
Robert Schmidt's avatar
Robert Schmidt committed
1284 1285 1286 1287
nr_pdcp_ue_manager_t *nr_pdcp_sdap_get_ue_manager() {
  return nr_pdcp_ue_manager;
}

Cedric Roux's avatar
Cedric Roux committed
1288
/* returns false in case of error, true if everything ok */
mir's avatar
mir committed
1289
bool nr_pdcp_get_statistics(ue_id_t ue_id, int srb_flag, int rb_id, nr_pdcp_statistics_t *out)
Robert Schmidt's avatar
Robert Schmidt committed
1290 1291 1292
{
  nr_pdcp_ue_t     *ue;
  nr_pdcp_entity_t *rb;
Cedric Roux's avatar
Cedric Roux committed
1293
  bool             ret;
Robert Schmidt's avatar
Robert Schmidt committed
1294 1295

  nr_pdcp_manager_lock(nr_pdcp_ue_manager);
1296
  ue = nr_pdcp_manager_get_ue(nr_pdcp_ue_manager, ue_id);
1297
  rb = nr_pdcp_get_rb(ue, rb_id, srb_flag);
Robert Schmidt's avatar
Robert Schmidt committed
1298 1299 1300

  if (rb != NULL) {
    rb->get_stats(rb, out);
Cedric Roux's avatar
Cedric Roux committed
1301
    ret = true;
Robert Schmidt's avatar
Robert Schmidt committed
1302
  } else {
Cedric Roux's avatar
Cedric Roux committed
1303
    ret = false;
Robert Schmidt's avatar
Robert Schmidt committed
1304 1305 1306 1307 1308
  }

  nr_pdcp_manager_unlock(nr_pdcp_ue_manager);

  return ret;
1309
}