pdcp_fifo.c 32.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*******************************************************************************

  Eurecom OpenAirInterface
  Copyright(c) 1999 - 2011 Eurecom

  This program is free software; you can redistribute it and/or modify it
  under the terms and conditions of the GNU General Public License,
  version 2, as published by the Free Software Foundation.

  This program is distributed in the hope it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  The full GNU General Public License is included in this distribution in
  the file called "COPYING".

  Contact Information
  Openair Admin: openair_admin@eurecom.fr
  Openair Tech : openair_tech@eurecom.fr
  Forums       : http://forums.eurecom.fsr/openairinterface
  Address      : Eurecom, 2229, route des crêtes, 06560 Valbonne Sophia Antipolis, France

*******************************************************************************/

/*! \file pdcp_fifo.c
31
* \brief pdcp interface with linux IP interface, have a look at http://man7.org/linux/man-pages/man7/netlink.7.html for netlink
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
* \author  Lionel GAUTHIER and Navid Nikaein
* \date 2009
* \version 0.5
* \warning This component can be runned only in user-space
* @ingroup pdcp
*/

#define PDCP_FIFO_C
#define PDCP_DEBUG 1
//#define IDROMEL_NEMO 1

#ifndef OAI_EMU
extern int otg_enabled;
#endif

#include "pdcp.h"
#include "pdcp_primitives.h"

#ifdef USER_MODE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define rtf_put write
#define rtf_get read
#else
#include <rtai_fifos.h>
#endif //USER_MODE

#include "../MAC/extern.h"
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
#include "SIMULATION/ETH_TRANSPORT/extern.h"
#include "UTIL/OCG/OCG.h"
#include "UTIL/OCG/OCG_extern.h"
#include "UTIL/LOG/log.h"
#include "UTIL/FIFO/pad_list.h"
68 69
#include "platform_constants.h"

70 71 72 73 74

#ifdef NAS_NETLINK
#include <sys/socket.h>
#include <linux/netlink.h>

75
extern char nl_rx_buf[NL_MAX_PAYLOAD];
76
extern struct sockaddr_nl nas_src_addr, nas_dest_addr;
77 78 79 80
extern struct nlmsghdr *nas_nlh_tx;
extern struct nlmsghdr *nas_nlh_rx;
extern struct iovec nas_iov_tx;
extern struct iovec nas_iov_rx;
81
extern int nas_sock_fd;
82 83
extern struct msghdr nas_msg_tx;
extern struct msghdr nas_msg_rx;
84 85 86 87

#define MAX_PAYLOAD 1600

unsigned char pdcp_read_state = 0;
88
//unsigned char pdcp_read_payload[MAX_PAYLOAD];
89 90 91 92 93 94 95
#endif

extern Packet_OTG_List *otg_pdcp_buffer;

pdcp_data_req_header_t pdcp_read_header;

//-----------------------------------------------------------------------------
96
int pdcp_fifo_flush_sdus (u32_t frame,u8 eNB_flag)
97 98 99 100 101 102 103 104 105
{
  //-----------------------------------------------------------------------------

  mem_block_t     *sdu = list_get_head (&pdcp_sdu_list);
  int             bytes_wrote = 0;
  int             pdcp_nb_sdu_sent = 0;
  u8              cont = 1;
  int ret;
  int mcs_inst;
106

107 108 109
  while (sdu && cont) {

#if defined(OAI_EMU)
110
    mcs_inst = ((pdcp_data_ind_header_t *)(sdu->data))->inst;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    // asjust the instance id when passing sdu to IP
    ((pdcp_data_ind_header_t *)(sdu->data))->inst = (((pdcp_data_ind_header_t *)(sdu->data))->inst >= NB_eNB_INST) ?
                                                    ((pdcp_data_ind_header_t *)(sdu->data))->inst - NB_eNB_INST +oai_emulation.info.nb_enb_local - oai_emulation.info.first_ue_local :// UE
                                                    ((pdcp_data_ind_header_t *)(sdu->data))->inst - oai_emulation.info.first_ue_local; // ENB
#else
    ((pdcp_data_ind_header_t *)(sdu->data))->inst = 0;
#endif

#ifdef PDCP_DEBUG
    LOG_I(PDCP, "PDCP->IP TTI %d INST %d: Preparing %d Bytes of data from rab %d to Nas_mesh\n",
          frame, ((pdcp_data_ind_header_t *)(sdu->data))->inst,
          ((pdcp_data_ind_header_t *)(sdu->data))->data_size, ((pdcp_data_ind_header_t *)(sdu->data))->rb_id);
#endif //PDCP_DEBUG

    cont = 0;

    if (!pdcp_output_sdu_bytes_to_write) {
      if (!pdcp_output_header_bytes_to_write) {
        pdcp_output_header_bytes_to_write = sizeof (pdcp_data_ind_header_t);
      }

#ifdef NAS_FIFO
      bytes_wrote = rtf_put (PDCP2NAS_FIFO,
                             &(((u8 *) sdu->data)[sizeof (pdcp_data_ind_header_t) - pdcp_output_header_bytes_to_write]),
                             pdcp_output_header_bytes_to_write);

#else
#ifdef NAS_NETLINK
#ifdef LINUX
140
      memcpy(NLMSG_DATA(nas_nlh_tx), &(((u8 *) sdu->data)[sizeof (pdcp_data_ind_header_t) - pdcp_output_header_bytes_to_write]),
141
             pdcp_output_header_bytes_to_write);
142
      nas_nlh_tx->nlmsg_len = pdcp_output_header_bytes_to_write;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
#endif //LINUX
#endif //NAS_NETLINK

      bytes_wrote = pdcp_output_header_bytes_to_write;
#endif //NAS_FIFO

#ifdef PDCP_DEBUG
      LOG_I(PDCP, "Frame %d Sent %d Bytes of header to Nas_mesh\n",
            frame,
            bytes_wrote);
#endif //PDCP_DEBUG

      if (bytes_wrote > 0) {
        pdcp_output_header_bytes_to_write = pdcp_output_header_bytes_to_write - bytes_wrote;

        if (!pdcp_output_header_bytes_to_write) { // continue with sdu
          pdcp_output_sdu_bytes_to_write = ((pdcp_data_ind_header_t *) sdu->data)->data_size;

#ifdef NAS_FIFO
          bytes_wrote = rtf_put (PDCP2NAS_FIFO, &(sdu->data[sizeof (pdcp_data_ind_header_t)]), pdcp_output_sdu_bytes_to_write);
#else

#ifdef NAS_NETLINK
#ifdef LINUX
167 168 169
          memcpy(NLMSG_DATA(nas_nlh_tx)+sizeof(pdcp_data_ind_header_t), &(sdu->data[sizeof (pdcp_data_ind_header_t)]), pdcp_output_sdu_bytes_to_write);
          nas_nlh_tx->nlmsg_len += pdcp_output_sdu_bytes_to_write;
          ret = sendmsg(nas_sock_fd,&nas_msg_tx,0);
170 171
          if (ret<0) {
            LOG_D(PDCP, "[PDCP_FIFOS] sendmsg returns %d (errno: %d)\n", ret, errno);
172
            mac_xface->macphy_exit("sendmsg failed for nas_sock_fd\n");
173 174 175 176 177 178 179 180 181 182 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 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
            break;
          }
#endif // LINUX
#endif //NAS_NETLINK
          bytes_wrote= pdcp_output_sdu_bytes_to_write;
#endif // NAS_FIFO

#ifdef PDCP_DEBUG
          LOG_I(PDCP, "PDCP->IP Frame %d INST %d: Sent %d Bytes of data from rab %d to Nas_mesh\n",
                frame,
                ((pdcp_data_ind_header_t *)(sdu->data))->inst,
                bytes_wrote,
                ((pdcp_data_ind_header_t *)(sdu->data))->rb_id);
#endif //PDCP_DEBUG
          if (bytes_wrote > 0) {
            pdcp_output_sdu_bytes_to_write -= bytes_wrote;

            if (!pdcp_output_sdu_bytes_to_write) { // OK finish with this SDU
              // LOG_D(PDCP, "rb sent a sdu qos_sap %d\n", sapiP);
              LOG_D(PDCP,
                    "[MSC_MSG][FRAME %05d][PDCP][MOD %02d][RB %02d][--- PDCP_DATA_IND / %d Bytes --->][IP][MOD %02d][]\n",
                    frame,
                    mcs_inst, //((pdcp_data_ind_header_t *)(sdu->data))->inst,
                    ((pdcp_data_ind_header_t *)(sdu->data))->rb_id,
                    ((pdcp_data_ind_header_t *)(sdu->data))->data_size,
                    mcs_inst); //((pdcp_data_ind_header_t *)(sdu->data))->inst

              list_remove_head (&pdcp_sdu_list);
              free_mem_block (sdu);
              cont = 1;
              pdcp_nb_sdu_sent += 1;
              sdu = list_get_head (&pdcp_sdu_list);
            }
          } else {
            LOG_W(PDCP, "RADIO->IP SEND SDU CONGESTION!\n");
          }
        } else {
          LOG_W(PDCP, "RADIO->IP SEND SDU CONGESTION!\n");
        }
      }
    } else {
      // continue writing sdu
#ifdef NAS_FIFO
      bytes_wrote = rtf_put (PDCP2NAS_FIFO,
                             (u8 *) (&(sdu->data[sizeof (pdcp_data_ind_header_t) + ((pdcp_data_ind_header_t *) sdu->data)->data_size - pdcp_output_sdu_bytes_to_write])),
                             pdcp_output_sdu_bytes_to_write);
#else  // NAS_FIFO
      bytes_wrote = pdcp_output_sdu_bytes_to_write;
#endif  // NAS_FIFO

      if (bytes_wrote > 0) {
        pdcp_output_sdu_bytes_to_write -= bytes_wrote;

        if (!pdcp_output_sdu_bytes_to_write) {     // OK finish with this SDU
          //PRINT_RB_SEND_OUTPUT_SDU ("[PDCP] RADIO->IP SEND SDU\n");
          list_remove_head (&pdcp_sdu_list);
          free_mem_block (sdu);
          cont = 1;
          pdcp_nb_sdu_sent += 1;
          sdu = list_get_head (&pdcp_sdu_list);
          // LOG_D(PDCP, "rb sent a sdu from rab\n");
        }
      }
    }
  }
#ifdef NAS_FIFO
  if ((pdcp_nb_sdu_sent)) {
    if ((pdcp_2_nas_irq > 0)) {
#ifdef PDCP_DEBUG
      LOG_I(PDCP, "Frame %d : Trigger NAS RX interrupt\n",
            frame);
#endif //PDCP_DEBUG

      rt_pend_linux_srq (pdcp_2_nas_irq);
    } else {
      LOG_E(PDCP, "Frame %d: ERROR IF IP STACK WANTED : NOTIF PACKET(S) pdcp_2_nas_irq not initialized : %d\n",
            frame,
            pdcp_2_nas_irq);
    }
  }
#endif  //NAS_FIFO

  return pdcp_nb_sdu_sent;
}

//-----------------------------------------------------------------------------
/*
 * returns a positive value if whole bytes that had to be read were read
 * returns zero  value if whole bytes that had to be read were not read at all
 * returns a negative  value if an error was encountered while reading the rt fifo
 */
int
    pdcp_fifo_read_input_sdus_remaining_bytes (u32_t frame,u8_t eNB_flag)
{
  //-----------------------------------------------------------------------------
268 269
  sdu_size_t             bytes_read = 0;
  rb_id_t                rab_id  = 0;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

  // if remaining bytes to read
  if (pdcp_input_sdu_remaining_size_to_read > 0) {
    bytes_read = rtf_get (NAS2PDCP_FIFO,
                          &(pdcp_input_sdu_buffer[pdcp_input_sdu_size_read]),
                          pdcp_input_sdu_remaining_size_to_read);

    if (bytes_read > 0) {
      LOG_D(PDCP, "[PDCP_FIFOS] Read %d remaining bytes of data from Nas_mesh\n", bytes_read);

      pdcp_input_sdu_remaining_size_to_read = pdcp_input_sdu_remaining_size_to_read - bytes_read;
      pdcp_input_sdu_size_read = pdcp_input_sdu_size_read + bytes_read;

      if (pdcp_input_sdu_remaining_size_to_read != 0) {
        return 0;
      } else {
#ifdef PDCP_DEBUG
        LOG_I(PDCP, "Frame %d: IP->RADIO RECEIVED COMPLETE SDU size %d inst %d rb %d\n",
              frame,
              pdcp_input_sdu_size_read,
              pdcp_input_header.inst,
              pdcp_input_header.rb_id);
#endif //PDCP_DEBUG
        pdcp_input_sdu_size_read = 0;
#ifdef IDROMEL_NEMO
        pdcp_read_header.inst = 0;
#endif

298
        if (pdcp_input_header.rb_id != 0) {
299 300 301
          LOG_D(PDCP, "[MSC_MSG][FRAME %05d][IP][MOD %02d][][--- PDCP_DATA_REQ / %d Bytes --->][PDCP][MOD %02d][RB %02d]\n",
                frame, pdcp_read_header.inst,  pdcp_read_header.data_size, pdcp_read_header.inst, pdcp_read_header.rb_id);

302
            if (pdcp_array[pdcp_read_header.inst][pdcp_read_header.rb_id%NB_RB_MAX].instanciated_instance) {
303
                pdcp_data_req (pdcp_input_header.inst,
304 305 306 307 308 309 310
                         frame, eNB_flag,
                         pdcp_input_header.rb_id,
                         RLC_MUI_UNDEFINED,
                         RLC_SDU_CONFIRM_NO,
                         pdcp_input_header.data_size,
                         pdcp_input_sdu_buffer,
                         PDCP_DATA_PDU);
311 312 313 314 315 316 317
            }
        } else if (eNB_flag) {
            // is a broadcast packet, we have to send this packet on all default RABS of all connected UEs
            LOG_D(PDCP, "Checking if could sent on default rabs\n");
#warning CODE TO BE REVIEWED, ONLY WORK FOR SIMPLE TOPOLOGY CASES
            for (rab_id = DEFAULT_RAB_ID; rab_id < MAX_RB; rab_id = rab_id + NB_RB_MAX) {
                LOG_D(PDCP, "Checking if could sent on default rab id %d\n", rab_id);
318
                if (pdcp_array[pdcp_input_header.inst][rab_id%NB_RB_MAX].instanciated_instance == (pdcp_input_header.inst + 1)) {
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
                    pdcp_data_req (pdcp_input_header.inst,
                                frame, eNB_flag,
                                rab_id,
                                RLC_MUI_UNDEFINED,
                                RLC_SDU_CONFIRM_NO,
                                pdcp_input_header.data_size,
                                pdcp_input_sdu_buffer,
                                PDCP_DATA_PDU);
                }
            }
        } else {
            LOG_D(PDCP, "Forcing send on DEFAULT_RAB_ID\n");
            pdcp_data_req (pdcp_input_header.inst,
                         frame, eNB_flag,
                         DEFAULT_RAB_ID,
                         RLC_MUI_UNDEFINED,
                         RLC_SDU_CONFIRM_NO,
                         pdcp_input_header.data_size,
                         pdcp_input_sdu_buffer,
                         PDCP_DATA_PDU);
339 340 341 342 343 344 345 346 347 348 349 350 351
        }
        // not necessary
        //memset(pdcp_input_sdu_buffer, 0, MAX_IP_PACKET_SIZE);
        return 1;
      }
    } else {
      return bytes_read;
    }
  }
  return 1;
}

//-----------------------------------------------------------------------------
352
int pdcp_fifo_read_input_sdus (u32_t frame, u8_t eNB_flag, u8_t UE_index, u8_t eNB_index)
353 354
{
  //-----------------------------------------------------------------------------
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 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 421 422 423 424 425 426
//#ifdef NAS_FIFO
//  int             cont;
//  int             bytes_read;
//
//  // if remaining bytes to read
//  if (pdcp_fifo_read_input_sdus_remaining_bytes (frame,eNB_flag) > 0) {
//
//    // all bytes that had to be read for a SDU were read
//    // if not overflow of list, try to get new sdus from rt fifo
//    cont = 1;
//
//    while (cont > 0) {
//      bytes_read = rtf_get (NAS2PDCP_FIFO,
//                            &(((u8 *) & pdcp_input_header)[pdcp_input_index_header]),
//                            sizeof (pdcp_data_req_header_t) - pdcp_input_index_header);
//
//      if (bytes_read > 0) {
//#ifdef PDCP_DEBUG
//        LOG_D(PDCP, "[PDCP_FIFOS] TTI %d Read %d Bytes of data (header %d) from Nas_mesh\n",
//              frame,
//              bytes_read,
//              sizeof(pdcp_data_req_header_t));
//#endif // PDCP_DEBUG
//        pdcp_input_index_header += bytes_read;
//
//        if (pdcp_input_index_header == sizeof (pdcp_data_req_header_t)) {
//#ifdef PDCP_DEBUG
//          LOG_D(PDCP, "TTI %d IP->RADIO READ HEADER sdu size %d\n",
//                frame,
//                pdcp_input_header.data_size);
//#endif //PDCP_DEBUG
//          pdcp_input_index_header = 0;
//          if (pdcp_input_header.data_size < 0) {
//            LOG_E(PDCP, "READ_FIFO: DATA_SIZE %d < 0\n",pdcp_input_header.data_size);
//
//            mac_xface->macphy_exit("");
//            return 0;
//          }
//          pdcp_input_sdu_remaining_size_to_read = pdcp_input_header.data_size;
//          pdcp_input_sdu_size_read     = 0;
//          // we know the size of the sdu, so read the sdu;
//          cont = pdcp_fifo_read_input_sdus_remaining_bytes (frame,eNB_flag);
//        } else {
//          cont = 0;
//        }
//        // error while reading rt fifo
//      } else {
//        cont = 0;
//      }
//    }
//  }
//  return bytes_read;
//
//#else //NAS_FIFO
//#ifdef NAS_NETLINK
//  int len = 1;
//  rb_id_t                rab_id  = 0;
//
//  while (len > 0) {
//      if (pdcp_read_state == 0) {
//#ifdef LINUX
//          len = recvmsg(nas_sock_fd, &nas_msg, 0);
//#else
//          len = -1;
//#endif
//
//          if (len < 0) {
//              // nothing in pdcp NAS socket
//              //LOG_I(PDCP, "[PDCP][NETLINK] Nothing in socket, length %d \n", len);
//          } else {
//#ifdef PDCP_DEBUG
//#ifdef LINUX
427
//              LOG_I(PDCP, "[PDCP][NETLINK] Received socket with length %d (nlmsg_len = %d)\n",
428 429
//                      len, nas_nlh->nlmsg_len-sizeof(struct nlmsghdr));
//#else
430 431
//              LOG_I(PDCP, "[PDCP][NETLINK] nlmsg_len = %d (%d,%d)\n",
//                       nas_nlh->nlmsg_len, sizeof(pdcp_data_req_header_t),
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
//                       sizeof(struct nlmsghdr));
//#endif
//#endif
//          }
//
//#ifdef LINUX
//          if (nas_nlh->nlmsg_len == sizeof (pdcp_data_req_header_t) + sizeof(struct nlmsghdr)) {
//              pdcp_read_state = 1;  //get
//              memcpy((void *)&pdcp_read_header, (void *)NLMSG_DATA(nas_nlh), sizeof(pdcp_data_req_header_t));
//          }
//#else
//          pdcp_read_state = 1;
//#endif
//      }
//
//      if (pdcp_read_state == 1) {
//#ifdef LINUX
//          len = recvmsg(nas_sock_fd, &nas_msg, 0);
//#else
//          len = -1;
//#endif
//
//          if (len < 0) {
//              // nothing in pdcp NAS socket
//              //LOG_I(PDCP, "[PDCP][NETLINK] Nothing in socket, length %d \n", len);
//          } else {
//              pdcp_read_state = 0;
//              // print_active_requests()
//
//#ifdef LINUX
//              memcpy(pdcp_read_payload, (unsigned char *)NLMSG_DATA(nas_nlh), nas_nlh->nlmsg_len - sizeof(struct nlmsghdr));
//#endif
//
//#ifdef OAI_EMU
466
//              pdcp_read_header.inst = (pdcp_read_header.inst >= oai_emulation.info.nb_enb_local) ?
467 468 469 470 471 472 473 474 475
//                                pdcp_read_header.inst - oai_emulation.info.nb_enb_local+ NB_eNB_INST + oai_emulation.info.first_ue_local :
//                                pdcp_read_header.inst +  oai_emulation.info.first_enb_local;
//#else
//              pdcp_read_header.inst = 0;
//#endif
//
//              if (pdcp_read_header.rb_id != 0) {
//                  if (pdcp_array[pdcp_read_header.inst][pdcp_read_header.rb_id].instanciated_instance) {
//#ifdef PDCP_DEBUG
476
//                      LOG_I(PDCP, "[PDCP][NETLINK][IP->PDCP] TTI %d, INST %d: Received socket with length %d (nlmsg_len = %d) on Rab %d \n",
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 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
//                        frame, pdcp_read_header.inst, len, nas_nlh->nlmsg_len-sizeof(struct nlmsghdr), pdcp_read_header.rb_id);
//                      LOG_D(PDCP, "[MSC_MSG][FRAME %05d][IP][MOD %02d][][--- PDCP_DATA_REQ / %d Bytes --->][PDCP][MOD %02d][RB %02d]\n",
//                        frame, pdcp_read_header.inst,  pdcp_read_header.data_size, pdcp_read_header.inst, pdcp_read_header.rb_id);
//#endif
//
//                      pdcp_data_req(pdcp_read_header.inst,
//                        frame,
//                        eNB_flag,
//                        pdcp_read_header.rb_id,
//                        RLC_MUI_UNDEFINED,
//                        RLC_SDU_CONFIRM_NO,
//                        pdcp_read_header.data_size,
//                        pdcp_read_payload,
//                        PDCP_DATA_PDU);
//                  } else {
//                      LOG_E(PDCP, "Received packet for non-instanciated instance %u with rb_id %u\n",
//                                pdcp_read_header.inst, pdcp_read_header.rb_id);
//                  }
//              } else  if (eNB_flag) {
//                // is a broadcast packet, we have to send this packet on all default RABS of all connected UEs
//                  LOG_D(PDCP, "Checking if could sent on default rabs\n");
//#warning CODE TO BE REVIEWED, ONLY WORK FOR SIMPLE TOPOLOGY CASES
//                  for (rab_id = DEFAULT_RAB_ID; rab_id < MAX_RB; rab_id = rab_id + NB_RB_MAX) {
//                      LOG_D(PDCP, "Checking if could sent on default rab id %d\n", rab_id);
//                      if (pdcp_array[pdcp_input_header.inst][rab_id].instanciated_instance == (pdcp_input_header.inst + 1)) {
//                          pdcp_data_req (pdcp_input_header.inst, frame, eNB_flag, rab_id, RLC_MUI_UNDEFINED,RLC_SDU_CONFIRM_NO,
//                                pdcp_input_header.data_size,
//                                pdcp_input_sdu_buffer,
//                                PDCP_DATA_PDU);
//                      }
//                  }
//              } else {
//                  LOG_D(PDCP, "Forcing send on DEFAULT_RAB_ID\n");
//                  pdcp_data_req (pdcp_input_header.inst,
//                         frame, eNB_flag,
//                         DEFAULT_RAB_ID,
//                         RLC_MUI_UNDEFINED,
//                         RLC_SDU_CONFIRM_NO,
//                         pdcp_input_header.data_size,
//                         pdcp_input_sdu_buffer,
//                         PDCP_DATA_PDU);
//              }
//          }
//      }
//  } // end of while
//
//  return len;
//
//#else // neither NAS_NETLINK nor NAS_FIFO
//  return 0;
//#endif // NAS_NETLINK
528
#ifdef NAS_NETLINK
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
# if defined(ENABLE_PDCP_NETLINK_FIFO)
    rb_id_t rab_id;

    struct pdcp_netlink_element_s *data = NULL;

    while (pdcp_netlink_dequeue_element(eNB_flag, UE_index, eNB_index, &data) != 0) {
        if (data->pdcp_read_header.rb_id != 0) {
            if (pdcp_array[data->pdcp_read_header.inst][data->pdcp_read_header.rb_id%NB_RB_MAX].instanciated_instance) {
#ifdef PDCP_DEBUG
                LOG_D(PDCP, "[MSC_MSG][FRAME %05d][IP][MOD %02d][][--- PDCP_DATA_REQ "
                      "/ %d Bytes --->][PDCP][MOD %02d][RB %02d]\n",
                      frame, data->pdcp_read_header.inst, data->pdcp_read_header.data_size,
                      data->pdcp_read_header.inst, data->pdcp_read_header.rb_id);
#endif

                pdcp_data_req(data->pdcp_read_header.inst,
                              frame,
                              eNB_flag,
                              data->pdcp_read_header.rb_id,
                              RLC_MUI_UNDEFINED,
                              RLC_SDU_CONFIRM_NO,
                              data->pdcp_read_header.data_size,
                              data->data,
                              PDCP_DATA_PDU);
            } else {
                LOG_E(PDCP, "Received packet for non-instanciated instance %u with rb_id %u\n",
                      data->pdcp_read_header.inst, data->pdcp_read_header.rb_id);
            }
        } else if (eNB_flag) {
            /* rb_id = 0, thus interpreated as broadcast and transported as
             * multiple unicast is a broadcast packet, we have to send this
             * packet on all default RABS of all connected UEs
             */
#warning CODE TO BE REVIEWED, ONLY WORK FOR SIMPLE TOPOLOGY CASES
            for (rab_id = DEFAULT_RAB_ID; rab_id < MAX_RB; rab_id = rab_id + NB_RB_MAX) {
                if (pdcp_array[pdcp_input_header.inst][rab_id%NB_RB_MAX].instanciated_instance == (pdcp_input_header.inst + 1)) {
                    pdcp_data_req(data->pdcp_read_header.inst,
                                  frame,
                                  eNB_flag,
                                  rab_id,
                                  RLC_MUI_UNDEFINED,
                                  RLC_SDU_CONFIRM_NO,
                                  data->pdcp_read_header.data_size,
                                  data->data,
                                  PDCP_DATA_PDU);
                }
            }
        } else {
            LOG_D(PDCP, "Forcing send on DEFAULT_RAB_ID\n");
            pdcp_data_req(data->pdcp_read_header.inst,
                          frame, eNB_flag,
                          DEFAULT_RAB_ID,
                          RLC_MUI_UNDEFINED,
                          RLC_SDU_CONFIRM_NO,
                          data->pdcp_read_header.data_size,
                          data->data,
                          PDCP_DATA_PDU);
        }

        free(data->data);
        free(data);
        data = NULL;
    }
    return 0;
# else
594 595
  int              len = 1;
  rb_id_t          rab_id  = 0;
596 597

  while (len > 0) {
598
          len = recvmsg(nas_sock_fd, &nas_msg_rx, 0);
599

600 601 602 603 604 605 606 607 608 609 610
          if (len<=0) {
              // nothing in pdcp NAS socket
              //LOG_I(PDCP, "[PDCP][NETLINK] Nothing in socket, length %d \n", len);
          } else {
              for (nas_nlh_rx = (struct nlmsghdr *) nl_rx_buf;
                   NLMSG_OK (nas_nlh_rx, len);
                   nas_nlh_rx = NLMSG_NEXT (nas_nlh_rx, len)) {

                  if (nas_nlh_rx->nlmsg_type == NLMSG_DONE) {
                      LOG_I(PDCP, "[PDCP][NETLINK] RX NLMSG_DONE\n");
                      //return;
611
                  }
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626

                  if (nas_nlh_rx->nlmsg_type == NLMSG_ERROR) {
                      LOG_I(PDCP, "[PDCP][NETLINK] RX NLMSG_ERROR\n");
                  }
                  if (pdcp_read_state == 0) {
                      if (nas_nlh_rx->nlmsg_len == sizeof (pdcp_data_req_header_t) + sizeof(struct nlmsghdr)) {
                          pdcp_read_state = 1;  //get
                          memcpy((void *)&pdcp_read_header, (void *)NLMSG_DATA(nas_nlh_rx), sizeof(pdcp_data_req_header_t));
                          LOG_I(PDCP, "[PDCP][NETLINK] RX pdcp_data_req_header_t inst %u, rb_id %u data_size %d\n", pdcp_read_header.inst, pdcp_read_header.rb_id, pdcp_read_header.data_size);
                      } else {
                          LOG_E(PDCP, "[PDCP][NETLINK] WRONG size %d should be sizeof (pdcp_data_req_header_t) + sizeof(struct nlmsghdr)\n", nas_nlh_rx->nlmsg_len);
                      }
                  } else {
                      pdcp_read_state = 0;
                      // print_active_requests()
627
#ifdef PDCP_DEBUG
628
                      LOG_I(PDCP, "[PDCP][NETLINK] Something in socket, length %d \n", nas_nlh_rx->nlmsg_len - sizeof(struct nlmsghdr));
629
#endif
630 631 632 633 634 635 636 637 638 639 640
                      //memcpy(pdcp_read_payload, (unsigned char *)NLMSG_DATA(nas_nlh_rx), nas_nlh_rx->nlmsg_len - sizeof(struct nlmsghdr));

                      #ifdef OAI_EMU
                      pdcp_read_header.inst = (pdcp_read_header.inst >= oai_emulation.info.nb_enb_local) ? \
                          pdcp_read_header.inst - oai_emulation.info.nb_enb_local+ NB_eNB_INST + oai_emulation.info.first_ue_local :
                          pdcp_read_header.inst +  oai_emulation.info.first_enb_local;
                      #else
                      pdcp_read_header.inst = 0;
                      #endif

                      if (pdcp_read_header.rb_id != 0) {
641
                          if (pdcp_array[pdcp_read_header.inst][pdcp_read_header.rb_id%NB_RB_MAX].instanciated_instance) {
642
#ifdef PDCP_DEBUG
643 644 645 646
                              LOG_I(PDCP, "[PDCP][NETLINK][IP->PDCP] TTI %d, INST %d: Received socket with length %d (nlmsg_len = %d) on Rab %d \n",
                                  frame, pdcp_read_header.inst, len, nas_nlh_rx->nlmsg_len-sizeof(struct nlmsghdr), pdcp_read_header.rb_id);
                              LOG_D(PDCP, "[MSC_MSG][FRAME %05d][IP][MOD %02d][][--- PDCP_DATA_REQ / %d Bytes --->][PDCP][MOD %02d][RB %02d]\n",
                                  frame, pdcp_read_header.inst, pdcp_read_header.data_size, pdcp_read_header.inst, pdcp_read_header.rb_id);
647 648
#endif

649 650 651 652 653 654 655 656 657 658 659 660 661
                              pdcp_data_req(pdcp_read_header.inst,
                                        frame,
                                        eNB_flag,
                                        pdcp_read_header.rb_id,
                                        RLC_MUI_UNDEFINED,
                                        RLC_SDU_CONFIRM_NO,
                                        pdcp_read_header.data_size,
                                        (unsigned char *)NLMSG_DATA(nas_nlh_rx),
                                        PDCP_DATA_PDU);
                          } else {
                              LOG_E(PDCP, "Received packet for non-instanciated instance %u with rb_id %u\n",
                                  pdcp_read_header.inst, pdcp_read_header.rb_id);
                          }
662
                      } else if (eNB_flag) { // rb_id =0, thus interpreated as broadcast and transported as multiple unicast 
663 664 665
                          // is a broadcast packet, we have to send this packet on all default RABS of all connected UEs
        #warning CODE TO BE REVIEWED, ONLY WORK FOR SIMPLE TOPOLOGY CASES
                          for (rab_id = DEFAULT_RAB_ID; rab_id < MAX_RB; rab_id = rab_id + NB_RB_MAX) {
666
                              if (pdcp_array[pdcp_input_header.inst][rab_id%NB_RB_MAX].instanciated_instance == (pdcp_input_header.inst + 1)) {
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
                                  pdcp_data_req (pdcp_read_header.inst, frame, eNB_flag, rab_id, RLC_MUI_UNDEFINED,RLC_SDU_CONFIRM_NO,
                                            pdcp_read_header.data_size,
                                            (unsigned char *)NLMSG_DATA(nas_nlh_rx),
                                            PDCP_DATA_PDU);
                              }
                          }
                      } else {
                          LOG_D(PDCP, "Forcing send on DEFAULT_RAB_ID\n");
                          pdcp_data_req (pdcp_read_header.inst,
                                  frame, eNB_flag,
                                  DEFAULT_RAB_ID,
                                  RLC_MUI_UNDEFINED,
                                  RLC_SDU_CONFIRM_NO,
                                  pdcp_read_header.data_size,
                                  (unsigned char *)NLMSG_DATA(nas_nlh_rx),
                                  PDCP_DATA_PDU);
                      }
                  }
              }
          }
  }
688
  return len;
689
# endif
690 691 692 693 694 695 696 697 698 699 700
#else // neither NAS_NETLINK nor NAS_FIFO
  return 0;
#endif // NAS_NETLINK
}


void pdcp_fifo_read_input_sdus_from_otg (u32_t frame, u8_t eNB_flag, u8 UE_index, u8 eNB_index) {
  unsigned char *otg_pkt=NULL;
  int src_id, module_id; // src for otg
  int dst_id, rb_id; // dst for otg
  int pkt_size=0, pkt_cnt=0;
701
  u8 pdcp_mode, is_ue=0;
702
  Packet_otg_elt * otg_pkt_info;
703

704 705 706
  // we need to add conditions to avoid transmitting data when the UE is not RRC connected.
#if defined(USER_MODE) && defined(OAI_EMU)
  if (oai_emulation.info.otg_enabled ==1 ){
Florian Kaltenberger's avatar
Florian Kaltenberger committed
707
    module_id = (eNB_flag == 1) ?  eNB_index : NB_eNB_INST + UE_index ;
708
    //rb_id    = (eNB_flag == 1) ? eNB_index * MAX_NUM_RB + DTCH : (NB_eNB_INST + UE_index -1 ) * MAX_NUM_RB + DTCH ;
709 710 711 712 713 714 715 716 717 718 719
    
    while ((otg_pkt_info = pkt_list_remove_head(&(otg_pdcp_buffer[module_id]))) != NULL) {
      LOG_I(OTG,"Mod_id %d Frame %d Got a packet (%p), HEAD of otg_pdcp_buffer[%d] is %p and Nb elements is %d\n", 
	    module_id,frame, otg_pkt_info, module_id, pkt_list_get_head(&(otg_pdcp_buffer[module_id])), otg_pdcp_buffer[module_id].nb_elements);
      //otg_pkt_info = pkt_list_remove_head(&(otg_pdcp_buffer[module_id]));
      dst_id = (otg_pkt_info->otg_pkt).dst_id;
      module_id = (otg_pkt_info->otg_pkt).module_id;
      rb_id = (otg_pkt_info->otg_pkt).rb_id;
      is_ue = (otg_pkt_info->otg_pkt).is_ue;
      pdcp_mode = (otg_pkt_info->otg_pkt).mode;
      //	LOG_I(PDCP,"pdcp_fifo, pdcp mode is= %d\n",pdcp_mode);
720
	
721 722 723 724 725 726
      // generate traffic if the ue is rrc reconfigured state
      // if (mac_get_rrc_status(module_id, eNB_flag, dst_id ) > 2 /*RRC_CONNECTED*/) { // not needed: this test is already done in update_otg_enb
      otg_pkt = (u8*) (otg_pkt_info->otg_pkt).sdu_buffer;
      pkt_size = (otg_pkt_info->otg_pkt).sdu_buffer_size;
      if (otg_pkt != NULL) {
	if (is_ue == 0 ) {
Florian Kaltenberger's avatar
Florian Kaltenberger committed
727
	  //rb_id = (/*NB_eNB_INST +*/ dst_id -1 ) * MAX_NUM_RB + DTCH;
728 729
	  LOG_D(OTG,"[eNB %d] Frame %d sending packet %d from module %d on rab id %d (src %d, dst %d) pkt size %d for pdcp mode %d\n", 
		eNB_index, frame, pkt_cnt++, module_id, rb_id, module_id, dst_id, pkt_size, pdcp_mode);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
730 731
	  pdcp_data_req(module_id, frame, eNB_flag, rb_id, RLC_MUI_UNDEFINED, RLC_SDU_CONFIRM_NO, pkt_size, otg_pkt,pdcp_mode);
	}
732
	else {
733
	  //rb_id= eNB_index * MAX_NUM_RB + DTCH;
734 735
	  LOG_D(OTG,"[UE %d] sending packet from module %d on rab id %d (src %d, dst %d) pkt size %d\n", 
		UE_index, src_id, rb_id, src_id, dst_id, pkt_size);
Florian Kaltenberger's avatar
Florian Kaltenberger committed
736 737
	  pdcp_data_req(src_id, frame, eNB_flag, rb_id, RLC_MUI_UNDEFINED, RLC_SDU_CONFIRM_NO,pkt_size, otg_pkt, PDCP_DATA_PDU);
	}
738
	free(otg_pkt);
739
      }
740 741
	  // } //else LOG_D(OTG,"frame %d enb %d-> ue %d link not yet established state %d  \n", frame, eNB_index,dst_id - NB_eNB_INST, mac_get_rrc_status(module_id, eNB_flag, dst_id - NB_eNB_INST));
      
742 743 744 745 746 747 748
    }
  }
#else
  if ((otg_enabled==1) && (eNB_flag == 1)) { // generate DL traffic
    unsigned int ctime=0;
    src_id = eNB_index;
    ctime = frame * 100;
749
    
750
    /*if  ((mac_get_rrc_status(eNB_index, eNB_flag, 0 ) > 2) &&
751
      (mac_get_rrc_status(eNB_index, eNB_flag, 1 ) > 2)) { */
752 753 754 755 756 757 758 759 760 761
    for (dst_id = 0; dst_id<NUMBER_OF_UE_MAX; dst_id++) {
      if (mac_get_rrc_status(eNB_index, eNB_flag, dst_id ) > 2) {
        otg_pkt=packet_gen(src_id, dst_id, ctime, &pkt_size);
        if (otg_pkt != NULL){
          rb_id = dst_id * NB_RB_MAX + DTCH;
          pdcp_data_req(src_id, frame, eNB_flag, rb_id, RLC_MUI_UNDEFINED, RLC_SDU_CONFIRM_NO,pkt_size, otg_pkt, PDCP_DATA_PDU);
          LOG_I(OTG,"send packet from module %d on rab id %d (src %d, dst %d) pkt size %d\n", eNB_index, rb_id, src_id, dst_id, pkt_size);
          free(otg_pkt);
        }
        /*else {
762 763
	  LOG_I(OTG,"nothing generated (src %d, dst %d)\n",src_id, dst_id);
	  }*/
764 765
      }
      /*else {
766 767
	LOG_I(OTG,"rrc_status (src %d, dst %d) = %d\n",src_id, dst_id, mac_get_rrc_status(src_id, eNB_flag, dst_id ));
	}*/
768 769 770 771
    }
  }
#endif
}