pdcp_fifo.c 13.7 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 31 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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 133 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 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 268 269 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 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 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 427 428 429 430 431 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 466 467 468 469
/*******************************************************************************

  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
* \brief pdcp interface with linux IP interface
* \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
#include "pdcp.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 "SIMULATION/ETH_TRANSPORT/extern.h"

#define PDCP_DEBUG 1

//#define IDROMEL_NEMO 1

#ifdef USER_MODE
#include "UTIL/OCG/OCG.h"
#include "UTIL/OCG/OCG_extern.h"
#endif

#ifdef NAS_NETLINK



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



extern struct sockaddr_nl nas_src_addr, nas_dest_addr;
extern struct nlmsghdr *nas_nlh;
extern struct iovec nas_iov;
extern int nas_sock_fd;
extern struct msghdr nas_msg;

#define MAX_PAYLOAD 1600  /* maximum payload size*/

unsigned char pdcp_read_state = 0;

char pdcp_read_payload[MAX_PAYLOAD];
#endif
pdcp_data_req_header_t pdcp_read_header;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
int
pdcp_fifo_flush_sdus ()
{
//-----------------------------------------------------------------------------

  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;


  while ((sdu) && (cont)) {

#ifdef USER_MODE
    // 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
	  msg("[PDCP][INFO] PDCP->IP TTI %d INST %d: Preparing %d Bytes of data from rab %d to Nas_mesh\n",
	      Mac_rlc_xface->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);
      }

#ifndef USER_MODE
      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
      memcpy(NLMSG_DATA(nas_nlh), &(((u8 *) sdu->data)[sizeof (pdcp_data_ind_header_t) - pdcp_output_header_bytes_to_write]),
                             pdcp_output_header_bytes_to_write);
      nas_nlh->nlmsg_len = pdcp_output_header_bytes_to_write;
#endif //LINUX
#endif //NAS_NETLINK

      bytes_wrote = pdcp_output_header_bytes_to_write;
#endif //USER_MODE


#ifdef PDCP_DEBUG
      msg("[PDCP][INFO] TTI %d Sent %d Bytes of header to Nas_mesh\n",
	  Mac_rlc_xface->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;

#ifndef USER_MODE
          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
	  memcpy(NLMSG_DATA(nas_nlh)+sizeof(pdcp_data_ind_header_t), &(sdu->data[sizeof (pdcp_data_ind_header_t)]), pdcp_output_sdu_bytes_to_write);
	  nas_nlh->nlmsg_len += pdcp_output_sdu_bytes_to_write;
	  ret = sendmsg(nas_sock_fd,&nas_msg,0);
	  if (ret<0) {
	    msg("[PDCP_FIFOS] sendmsg returns %d\n",ret);
	    perror("error code:");
	    mac_xface->macphy_exit("");
	    break;
	  }
#endif // LINUX
#endif //NAS_NETLINK
	  bytes_wrote= pdcp_output_sdu_bytes_to_write;
#endif // USER_MODE

#ifdef PDCP_DEBUG
	  msg("[PDCP][INFO] PDCP->IP TTI %d INST %d: Sent %d Bytes of data from rab %d to Nas_mesh\n",
	      Mac_rlc_xface->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
	      // msg("rb sent a sdu qos_sap %d\n",sapiP);

              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 {
            msg ("[PDCP] RADIO->IP SEND SDU CONGESTION!\n");
          }
        } else {
          msg ("[PDCP] RADIO->IP SEND SDU CONGESTION!\n");
        }
      }
    } else {
      // continue writing sdu
#ifndef USER_MODE
      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  // USER_MODE
      bytes_wrote = pdcp_output_sdu_bytes_to_write;
#endif  // USER_MODE

      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);
	  // msg("rb sent a sdu from rab\n");
        }
      }
    }
  }
#ifndef USER_MODE
    if ((pdcp_nb_sdu_sent)) {
      if ((pdcp_2_nas_irq > 0)) {
#ifdef PDCP_DEBUG
	msg("[PDCP][INFO] TTI %d : Trigger NAS RX interrupt\n",
	    Mac_rlc_xface->frame);
#endif //PDCP_DEBUG

           rt_pend_linux_srq (pdcp_2_nas_irq);
      } else {
        msg ("[PDCP] TTI %d: ERROR IF IP STACK WANTED : NOTIF PACKET(S) pdcp_2_nas_irq not initialized : %d\n",
	     Mac_rlc_xface->frame,
	     pdcp_2_nas_irq);
      }
    }
#endif  //USER_MODE

  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 ()
{
//-----------------------------------------------------------------------------
  sdu_size_t             bytes_read=0;

  if (pdcp_input_sdu_remaining_size_to_read > 0) {

    // printk("[PDCP][INFO] read_input_sdus pdcp_input_sdu_remaining_size_to_read = %d \n", pdcp_input_sdu_remaining_size_to_read);
    bytes_read = rtf_get (NAS2PDCP_FIFO,
			  &(pdcp_input_sdu_buffer[pdcp_input_sdu_size_read]),
			  pdcp_input_sdu_remaining_size_to_read);

    //printk("[PDCP][INFO] read fifo returned %d \n", bytes_read);
    if (bytes_read > 0) {

      //msg("[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
	msg("[PDCP][INFO]  TTI %d: IP->RADIO RECEIVED COMPLETE SDU size %d inst %d rb %d\n",
	    Mac_rlc_xface->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
        pdcp_data_req (pdcp_input_header.inst,
		       pdcp_input_header.rb_id,
		       pdcp_input_header.data_size,
		       pdcp_input_sdu_buffer);

        // not necessary
        //memset(pdcp_input_sdu_buffer, 0, MAX_IP_PACKET_SIZE);
        return 1;
      }
    } else {
      return bytes_read;
    }
  }
  return 1;
}

#ifndef NAS_NETLINK

//-----------------------------------------------------------------------------
int
pdcp_fifo_read_input_sdus ()
{
//-----------------------------------------------------------------------------
  int             cont;
  int             bytes_read;

  // if remaining bytes to read
  if (pdcp_fifo_read_input_sdus_remaining_bytes () > 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
	msg("[PDCP_FIFOS] TTI %d Read %d Bytes of data (header %d) from Nas_mesh\n",
	    Mac_rlc_xface->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
	  msg("[PDCP] TTI %d IP->RADIO READ HEADER sdu size %d\n",
	      Mac_rlc_xface->frame,
	      pdcp_input_header.data_size);
#endif //PDCP_DEBUG
          pdcp_input_index_header = 0;
	  if(pdcp_input_header.data_size<0){
	    msg("[PDCP][FATAL ERROR] 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 ();
        } else {
          cont = 0;
        }
        // error while reading rt fifo
      } else {
        cont = 0;
      }
    }
  }
  return 0;
}

#else //NAS_NETLINK



//-----------------------------------------------------------------------------
int
pdcp_fifo_read_input_sdus ()
{
//-----------------------------------------------------------------------------
  int             cont;
  int             bytes_read;
  int len;



    if (pdcp_read_state == 0) {

#ifdef LINUX
      len = recvmsg(nas_sock_fd, &nas_msg, 0);
#else
      len = -1;
#endif

      if (len<0) {
	//		printf("[PDCP][NETLINK %d] nothing in pdcp NAS socket\n", nas_sock_fd);
       }
      else {
#ifdef PDCP_DEBUG
#ifdef LINUX
		printf("[PDCP][NETLINK] Received socket with length %d (nlmsg_len = %d)\n",len,nas_nlh->nlmsg_len-sizeof(struct nlmsghdr));
#endif PDCP_DEBUG
#ifdef PDCP_DEBUG
		printf("[PDCP][NETLINK] nlmsg_len = %d (%d,%d)\n",nas_nlh->nlmsg_len,
		       sizeof(pdcp_data_req_header_t),
		       sizeof(struct nlmsghdr));
#endif LINUX
#endif PDCP_DEBUG
      }
#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 //LINUX
      pdcp_read_state = 1;
#endif //LINUX
    }

    if (pdcp_read_state == 1) {

#ifdef LINUX
      len = recvmsg(nas_sock_fd, &nas_msg, 0);
#else
      len = -1;
#endif //LINUX

      if (len<0) {
	// nothing in pdcp NAS socket
      }
      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 IDROMEL_NEMO
	pdcp_read_header.inst = 0;
#endif
	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;

#ifdef PDCP_DEBUG
	printf("[PDCP][NETLINK][IP->PDCP] TTI %d, INST %d: Received socket with length %d (nlmsg_len = %d) on Rab %d \n",
	       Mac_rlc_xface->frame,
	       pdcp_read_header.inst,
	       len,
	       nas_nlh->nlmsg_len-sizeof(struct nlmsghdr),
	       pdcp_read_header.rb_id);
#endif PDCP_DEBUG

	pdcp_data_req(pdcp_read_header.inst,
		      pdcp_read_header.rb_id,
		      pdcp_read_header.data_size,
		      pdcp_read_payload);
      }

    }
}
#endif