common.c 10.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
/*
 * 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.0  (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
 */

22
/*! \file common.c
23
* \brief
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
* \author Navid Nikaein and Raymond Knopp, Lionel GAUTHIER
* \date 2013
* \version 1.0
* \company Eurecom
* \email: navid.nikaein@eurecom.fr, lionel.gauthier@eurecom.fr
*/

#include "local.h"
#include "proto_extern.h"
#ifndef OAI_NW_DRIVER_USE_NETLINK
#include "rtai_fifos.h"
#endif


#include <linux/inetdevice.h>

#include <net/tcp.h>
#include <net/udp.h>

#define NIPADDR(addr) \
        (uint8_t)(addr & 0x000000FF), \
        (uint8_t)((addr & 0x0000FF00) >> 8), \
        (uint8_t)((addr & 0x00FF0000) >> 16), \
        (uint8_t)((addr & 0xFF000000) >> 24)

#define NIP6ADDR(addr) \
        ntohs((addr)->s6_addr16[0]), \
        ntohs((addr)->s6_addr16[1]), \
        ntohs((addr)->s6_addr16[2]), \
        ntohs((addr)->s6_addr16[3]), \
        ntohs((addr)->s6_addr16[4]), \
        ntohs((addr)->s6_addr16[5]), \
        ntohs((addr)->s6_addr16[6]), \
        ntohs((addr)->s6_addr16[7])


//#define OAI_DRV_DEBUG_SEND
//#define OAI_DRV_DEBUG_RECEIVE
gauthier's avatar
gauthier committed
62 63 64

void
ue_ip_common_class_wireless2ip(
65 66 67 68 69
  sdu_size_t data_lenP,
  void      *pdcp_sdu_pP,
  int        instP,
  rb_id_t    rb_idP)
{
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
  struct sk_buff      *skb_p           = NULL;
  ipversion_t         *ipv_p           = NULL;
  ue_ip_priv_t        *gpriv_p         = netdev_priv(ue_ip_dev[instP]);
  unsigned int         hard_header_len = 0;
#ifdef OAI_DRV_DEBUG_RECEIVE
  int                  i;
  unsigned char       *addr_p          = 0;
#endif
  unsigned char        protocol;
  struct iphdr        *network_header_p  = NULL;

#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[UE_IP_DRV][%s] begin RB %d Inst %d Length %d bytes\n",__FUNCTION__, rb_idP,instP,data_lenP);
#endif

  skb_p = dev_alloc_skb( data_lenP + 2 );

  if(!skb_p) {
    printk("[UE_IP_DRV][%s] low on memory\n",__FUNCTION__);
    ++gpriv_p->stats.rx_dropped;
    return;
  }

  skb_reserve(skb_p,2);
  memcpy(skb_put(skb_p, data_lenP), pdcp_sdu_pP,data_lenP);

  skb_p->dev = ue_ip_dev[instP];
  hard_header_len = ue_ip_dev[instP]->hard_header_len;

  skb_set_mac_header(skb_p, 0);
102 103
  skb_set_network_header(skb_p, hard_header_len);
skb_p->mark = rb_idP;
104 105 106 107 108 109 110
  //printk("[NAC_COMMIN_RECEIVE]: Packet Type %d (%d,%d)",skb_p->pkt_type,PACKET_HOST,PACKET_BROADCAST);
  skb_p->pkt_type = PACKET_HOST;


#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[UE_IP_DRV][%s] Receiving packet of size %d from PDCP \n",__FUNCTION__, skb_p->len);

111
  for (i=0; i<skb_p->len; i++) {
112
    printk("%2x ",((unsigned char *)(skb_p->data))[i]);
113
  }
114 115 116 117 118 119 120 121 122 123 124 125 126 127

  printk("\n");
#endif
#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[UE_IP_DRV][%s] skb_p->data           @ %p\n",__FUNCTION__,  skb_p->data);
  printk("[UE_IP_DRV][%s] skb_p->mac_header     @ %p\n",__FUNCTION__,  skb_p->mac_header);
#endif



  // LG TEST skb_p->ip_summed = CHECKSUM_NONE;
  skb_p->ip_summed = CHECKSUM_UNNECESSARY;


128
  ipv_p = (struct ipversion*)((void*)&(skb_p->data[hard_header_len]));
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

  switch (ipv_p->version) {

  case 6:
#ifdef OAI_DRV_DEBUG_RECEIVE
    printk("[UE_IP_DRV][%s] receive IPv6 message\n",__FUNCTION__);
#endif
    skb_set_network_header(skb_p, hard_header_len);
    //skb_p->network_header_p = &skb_p->data[hard_header_len];

    if (hard_header_len == 0) {
      skb_p->protocol = htons(ETH_P_IPV6);
    } else {
#ifdef OAI_NW_DRIVER_TYPE_ETHERNET
      skb_p->protocol = eth_type_trans(skb_p, ue_ip_dev[instP]);
#else
#endif
    }

    //printk("Writing packet with protocol %x\n",ntohs(skb_p->protocol));
    break;

  case 4:

#ifdef OAI_DRV_DEBUG_RECEIVE
    //printk("NAS_TOOL_RECEIVE: receive IPv4 message\n");
    addr_p = (unsigned char *)&((struct iphdr *)&skb_p->data[hard_header_len])->saddr;

    if (addr_p) {
      printk("[UE_IP_DRV][%s] Source %d.%d.%d.%d\n",__FUNCTION__, addr_p[0],addr_p[1],addr_p[2],addr_p[3]);
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

    addr_p = (unsigned char *)&((struct iphdr *)&skb_p->data[hard_header_len])->daddr;

    if (addr_p) {
      printk("[UE_IP_DRV][%s] Dest %d.%d.%d.%d\n",__FUNCTION__, addr_p[0],addr_p[1],addr_p[2],addr_p[3]);
    }

    printk("[UE_IP_DRV][%s] protocol  %d\n",__FUNCTION__, ((struct iphdr *)&skb_p->data[hard_header_len])->protocol);
#endif

    skb_set_network_header(skb_p, hard_header_len);
    //network_header_p = (struct iphdr *)skb_network_header(skb_p);
    network_header_p = (struct iphdr *)skb_network_header(skb_p);
    protocol = network_header_p->protocol;

#ifdef OAI_DRV_DEBUG_RECEIVE

    switch (protocol) {
    case IPPROTO_IP:
      printk("[UE_IP_DRV][%s] Received Raw IPv4 packet\n",__FUNCTION__);
      break;

    case IPPROTO_IPV6:
      printk("[UE_IP_DRV][%s] Received Raw IPv6 packet\n",__FUNCTION__);
      break;

    case IPPROTO_ICMP:
      printk("[UE_IP_DRV][%s] Received Raw ICMP packet\n",__FUNCTION__);
      break;

    case IPPROTO_TCP:
      printk("[UE_IP_DRV][%s] Received TCP packet\n",__FUNCTION__);
      break;

    case IPPROTO_UDP:
      printk("[UE_IP_DRV][%s] Received UDP packet\n",__FUNCTION__);
      break;

    default:
      break;
    }

#endif

    if (hard_header_len == 0) {
      skb_p->protocol = htons(ETH_P_IP);
    }

    //printk("[UE_IP_DRV][COMMON] Writing packet with protocol %x\n",ntohs(skb_p->protocol));
    break;

  default:
    printk("[UE_IP_DRV][%s] begin RB %d Inst %d Length %d bytes\n",__FUNCTION__,rb_idP,instP,data_lenP);
    printk("[UE_IP_DRV][%s] Inst %d: receive unknown message (version=%d)\n",__FUNCTION__,instP,ipv_p->version);
  }

  ++gpriv_p->stats.rx_packets;
  gpriv_p->stats.rx_bytes += data_lenP;
#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[UE_IP_DRV][%s] sending packet of size %d to kernel\n",__FUNCTION__,skb_p->len);

221
  for (i=0; i<skb_p->len; i++) {
222
    printk("%2x ",((unsigned char *)(skb_p->data))[i]);
223
  }
224 225 226 227 228 229 230

  printk("\n");
#endif //OAI_DRV_DEBUG_RECEIVE
  netif_rx(skb_p);
#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[UE_IP_DRV][%s] end\n",__FUNCTION__);
#endif
231 232 233 234
}

//---------------------------------------------------------------------------
// Delete the data
235 236
void ue_ip_common_ip2wireless_drop(struct sk_buff *skb_pP,  int instP)
{
237
  //---------------------------------------------------------------------------
gauthier's avatar
gauthier committed
238 239
  ue_ip_priv_t *priv_p=netdev_priv(ue_ip_dev[instP]);
  ++priv_p->stats.tx_dropped;
240 241 242 243
}

//---------------------------------------------------------------------------
// Request the transfer of data (QoS SAP)
gauthier's avatar
gauthier committed
244 245
void
ue_ip_common_ip2wireless(
246 247 248 249
  struct sk_buff *skb_pP,
  int instP
)
{
250
  //---------------------------------------------------------------------------
251
  struct pdcp_data_req_header_s     pdcph;
gauthier's avatar
gauthier committed
252
  ue_ip_priv_t                     *priv_p=netdev_priv(ue_ip_dev[instP]);
253 254 255 256 257 258 259 260 261
#ifdef LOOPBACK_TEST
  int i;
#endif
#ifdef OAI_DRV_DEBUG_SEND
  int j;
#endif
  unsigned int bytes_wrote;
  // Start debug information
#ifdef OAI_DRV_DEBUG_SEND
gauthier's avatar
gauthier committed
262
  printk("[UE_IP_DRV][%s] inst %d begin \n",__FUNCTION__,instP);
263 264
#endif

265
  if (skb_pP==NULL) {
266 267 268 269 270 271 272
#ifdef OAI_DRV_DEBUG_SEND
    printk("[UE_IP_DRV][%s] input parameter skb is NULL \n",__FUNCTION__);
#endif
    return;
  }


gauthier's avatar
gauthier committed
273
  pdcph.data_size  = skb_pP->len;
274

gauthier's avatar
gauthier committed
275
  if (skb_pP->mark) {
276
    pdcph.rb_id      = skb_pP->mark;
277
  } else {
278
    pdcph.rb_id      = UE_IP_DEFAULT_RAB_ID;
279
  }
280

gauthier's avatar
gauthier committed
281
  pdcph.inst       = instP;
282 283 284 285 286


  bytes_wrote = ue_ip_netlink_send((char *)&pdcph,UE_IP_PDCPH_SIZE);
#ifdef OAI_DRV_DEBUG_SEND
  printk("[UE_IP_DRV][%s] Wrote %d bytes (header for %d byte skb) to PDCP via netlink\n",__FUNCTION__,
287
         bytes_wrote,skb_pP->len);
288 289
#endif

290 291 292 293 294 295
  if (bytes_wrote != UE_IP_PDCPH_SIZE) {
    printk("[UE_IP_DRV][%s] problem while writing PDCP's header (bytes wrote = %d)\n",__FUNCTION__,bytes_wrote);
    printk("rb_id %d, Wrote %d, Header Size %d \n", pdcph.rb_id , bytes_wrote, UE_IP_PDCPH_SIZE);
    priv_p->stats.tx_dropped ++;
    return;
  }
296

gauthier's avatar
gauthier committed
297
  bytes_wrote += ue_ip_netlink_send((char *)skb_pP->data,skb_pP->len);
298 299


300 301 302 303 304 305 306 307 308 309 310 311
  if (bytes_wrote != skb_pP->len+UE_IP_PDCPH_SIZE) {
    printk("[UE_IP_DRV][%s] Inst %d, RB_ID %d: problem while writing PDCP's data, bytes_wrote = %d, Data_len %d, PDCPH_SIZE %d\n",
           __FUNCTION__,
           instP,
           pdcph.rb_id,
           bytes_wrote,
           skb_pP->len,
           UE_IP_PDCPH_SIZE); // congestion

    priv_p->stats.tx_dropped ++;
    return;
  }
312 313

#ifdef OAI_DRV_DEBUG_SEND
gauthier's avatar
gauthier committed
314
  printk("[UE_IP_DRV][%s] Sending packet of size %d to PDCP \n",__FUNCTION__,skb_pP->len);
315

316
  for (j=0; j<skb_pP->len; j++) {
gauthier's avatar
gauthier committed
317
    printk("%2x ",((unsigned char *)(skb_pP->data))[j]);
318
  }
319

320 321 322
  printk("\n");
#endif

gauthier's avatar
gauthier committed
323 324
  priv_p->stats.tx_bytes   += skb_pP->len;
  priv_p->stats.tx_packets ++;
325 326 327 328 329 330
#ifdef OAI_DRV_DEBUG_SEND
  printk("[UE_IP_DRV][%s] end \n",__FUNCTION__);
#endif
}

//---------------------------------------------------------------------------
331 332 333
void ue_ip_common_wireless2ip(struct nlmsghdr *nlh_pP)
{
  //---------------------------------------------------------------------------
334

gauthier's avatar
gauthier committed
335 336
  struct pdcp_data_ind_header_s     *pdcph_p = (struct pdcp_data_ind_header_s *)NLMSG_DATA(nlh_pP);
  ue_ip_priv_t                      *priv_p;
337

gauthier's avatar
gauthier committed
338
  priv_p = netdev_priv(ue_ip_dev[pdcph_p->inst]);
339 340 341 342


#ifdef OAI_DRV_DEBUG_RECEIVE
  printk("[UE_IP_DRV][%s] QOS receive from PDCP, size %d, rab %d, inst %d\n",__FUNCTION__,
gauthier's avatar
gauthier committed
343
         pdcph_p->data_size,pdcph_p->rb_id,pdcph_p->inst);
344 345
#endif

gauthier's avatar
gauthier committed
346
  ue_ip_common_class_wireless2ip(pdcph_p->data_size,
347
                                 (unsigned char *)NLMSG_DATA(nlh_pP) + UE_IP_PDCPH_SIZE,
348
                                 1, //pdcph_p->inst,
349
                                 pdcph_p->rb_id);
350 351 352

}