eth_raw.c 15.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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
 */
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

/*! \file ethernet_lib.c 
 * \brief API to stream I/Q samples over standard ethernet
 * \author  add alcatel Katerina Trilyraki, Navid Nikaein, Pedro Dinis, Lucio Ferreira, Raymond Knopp
 * \date 2015
 * \version 0.2
 * \company Eurecom
 * \maintainer:  navid.nikaein@eurecom.fr
 * \note
 * \warning 
 */

#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <unistd.h>
#include <errno.h>

#include "common_lib.h"
#include "ethernet_lib.h"

48
#define DEBUG 0
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

struct sockaddr_ll dest_addr[MAX_INST];
struct sockaddr_ll local_addr[MAX_INST];
int addr_len[MAX_INST];
struct ifreq if_index[MAX_INST];

int eth_socket_init_raw(openair0_device *device) {
 
  int i = 0;
  eth_state_t *eth = (eth_state_t*)device->priv;
  int Mod_id = device->Mod_id;
  const char *local_mac, *remote_mac;
  int sock_dom=0;
  int sock_type=0;
  int sock_proto=0;  
64 65
 
  if (device->host_type == RRH_HOST ) {  /* RRH doesn't know remote MAC(will be retrieved from first packet send from BBU) and remote port(don't care) */
66
    local_mac = device->openair0_cfg->my_addr; 
67
    remote_mac = malloc(ETH_ALEN);
Raymond Knopp's avatar
Raymond Knopp committed
68
    memset((void*)remote_mac,0,ETH_ALEN);
69 70
    printf("[%s] local MAC addr %s remote MAC addr %s\n","RRH", local_mac,remote_mac);    
  } else {
71 72
    local_mac = device->openair0_cfg->my_addr;
    remote_mac = device->openair0_cfg->remote_addr;
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
    printf("[%s] local MAC addr %s remote MAC addr %s\n","BBU", local_mac,remote_mac);    
  }
   
  
  /* Open a RAW socket to send on */
  sock_dom=AF_PACKET;
  sock_type=SOCK_RAW;
  sock_proto=IPPROTO_RAW;
  if ((eth->sockfd[Mod_id] = socket(sock_dom, sock_type, sock_proto)) == -1) {
    perror("ETHERNET: Error opening RAW socket");
    exit(0);
  }
  
  /* initialize destination address */
  for (i=0; i< MAX_INST; i++) {
    bzero((void *)&(local_addr[i]), sizeof(struct sockaddr_ll));
    bzero((void *)&(if_index[i]), sizeof(struct ifreq)); 
  }
  /* Get the index of the interface to send on */
  strcpy(if_index[Mod_id].ifr_name,eth->if_name[Mod_id]);
  if (ioctl(eth->sockfd[Mod_id], SIOCGIFINDEX, &(if_index[Mod_id])) < 0)
    perror("SIOCGIFINDEX");
   
  local_addr[Mod_id].sll_family   = AF_PACKET;
  local_addr[Mod_id].sll_ifindex  = if_index[Mod_id].ifr_ifindex;
  /* hear traffic from specific protocol*/
99
  local_addr[Mod_id].sll_protocol = htons((short)device->openair0_cfg->my_port);
100 101 102 103 104 105 106 107 108 109
  local_addr[Mod_id].sll_halen    = ETH_ALEN;
  local_addr[Mod_id].sll_pkttype  = PACKET_OTHERHOST;
  addr_len[Mod_id] = sizeof(struct sockaddr_ll);
  
 if (bind(eth->sockfd[Mod_id],(struct sockaddr *)&local_addr[Mod_id],addr_len[Mod_id])<0) {
   perror("ETHERNET: Cannot bind to socket");
   exit(0);
 }
 
 /* Construct the Ethernet header */ 
110 111
 ether_aton_r(local_mac, (struct ether_addr *)(&(eth->eh.ether_shost)));
 ether_aton_r(remote_mac, (struct ether_addr *)(&(eth->eh.ether_dhost)));
Sandeep Kumar's avatar
Sandeep Kumar committed
112
 if (eth->flags == ETH_RAW_IF5_MOBIPASS) {
113
   eth->eh.ether_type = htons(0xbffe);
Sandeep Kumar's avatar
Sandeep Kumar committed
114 115 116
 } else {
   eth->eh.ether_type = htons((short)device->openair0_cfg->my_port);
 } 
117
 printf("[%s] binding mod_%d to hardware address %x:%x:%x:%x:%x:%x\n",((device->host_type == BBU_HOST) ? "BBU": "RRH"),Mod_id,eth->eh.ether_shost[0],eth->eh.ether_shost[1],eth->eh.ether_shost[2],eth->eh.ether_shost[3],eth->eh.ether_shost[4],eth->eh.ether_shost[5]);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
 
 return 0;
}


int trx_eth_write_raw(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps,int cc, int flags) {
  
  int bytes_sent=0;
  eth_state_t *eth = (eth_state_t*)device->priv;
  int Mod_id = device->Mod_id;
  int sendto_flag =0;
  int i=0;
  //sendto_flag|=flags;

  eth->tx_nsamps=nsamps;
133
  
134 135 136 137 138 139 140 141 142 143 144
  for (i=0;i<cc;i++) {	
    /* buff[i] points to the position in tx buffer where the payload to be sent is
       buff2 points to the position in tx buffer where the packet header will be placed */
    void *buff2 = (void*)(buff[i]-APP_HEADER_SIZE_BYTES-MAC_HEADER_SIZE_BYTES); 
    
    /* we don't want to ovewrite with the header info the previous tx buffer data so we store it*/
    struct ether_header temp =  *(struct ether_header *)buff2;
    int32_t temp0 = *(int32_t *)(buff2 + MAC_HEADER_SIZE_BYTES);
    openair0_timestamp  temp1 = *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t));
    
    bytes_sent = 0;
145
    memcpy(buff2,(void*)&eth->eh,MAC_HEADER_SIZE_BYTES);
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
    *(int16_t *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int16_t))=1+(i<<1);
    *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t)) = timestamp;
    
    /*printf("[RRH]write mod_%d %d , len %d, buff %p \n",
      Mod_id,eth->sockfd[Mod_id],RAW_PACKET_SIZE_BYTES(nsamps), buff2);*/
    
    while(bytes_sent < RAW_PACKET_SIZE_BYTES(nsamps)) {
#if DEBUG   
      printf("------- TX ------: buff2 current position=%d remaining_bytes=%d  bytes_sent=%d \n",
	     (void *)(buff2+bytes_sent), 
	     RAW_PACKET_SIZE_BYTES(nsamps) - bytes_sent,
	     bytes_sent);
#endif
      /* Send packet */
      bytes_sent += send(eth->sockfd[Mod_id],
			   buff2, 
			   RAW_PACKET_SIZE_BYTES(nsamps),
			   sendto_flag);
      if ( bytes_sent == -1) {
	eth->num_tx_errors++;
	perror("ETHERNET WRITE: ");
	exit(-1);
      } else {
#if DEBUG
	printf("------- TX ------: nu=%x an_id=%d ts%d bytes_sent=%d\n",
	       *(uint8_t *)(buff2+ETH_ALEN),
	       *(int16_t *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int16_t)),
Raymond Knopp's avatar
Raymond Knopp committed
173
	       *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t)), 
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	       bytes_sent);
    dump_packet((device->host_type == BBU_HOST)? "BBU":"RRH", buff2, RAW_PACKET_SIZE_BYTES(nsamps), TX_FLAG);
#endif
    eth->tx_actual_nsamps=bytes_sent>>2;
    eth->tx_count++;
      }
    }    			    
    
  /* tx buffer values restored */  
    *(struct ether_header *)buff2 = temp;
    *(int32_t *)(buff2 + MAC_HEADER_SIZE_BYTES) = temp0;
    *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t)) = temp1;
  }
  return (bytes_sent-APP_HEADER_SIZE_BYTES-MAC_HEADER_SIZE_BYTES)>>2;
}


191

192
int trx_eth_write_raw_IF4p5(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps, int cc, int flags) {
193 194 195 196 197 198 199 200 201

  int nblocks = nsamps;  
  int bytes_sent = 0;
  
  eth_state_t *eth = (eth_state_t*)device->priv;
  int Mod_id = device->Mod_id;  
  
  ssize_t packet_size;
  
202 203 204 205
  if (flags == IF4p5_PDLFFT) {
    packet_size = RAW_IF4p5_PDLFFT_SIZE_BYTES(nblocks);    
  } else if (flags == IF4p5_PULFFT) {
    packet_size = RAW_IF4p5_PULFFT_SIZE_BYTES(nblocks);    
206 207
  } else if (flags == IF5_MOBIPASS) {
    packet_size = RAW_IF5_MOBIPASS_SIZE_BYTES;
208
  } else {
209
    packet_size = RAW_IF4p5_PRACH_SIZE_BYTES;
210 211 212 213
  }
  
  eth->tx_nsamps = nblocks;
  
214
  memcpy(buff[0], (void*)&eth->eh, MAC_HEADER_SIZE_BYTES);	
knopp's avatar
knopp committed
215 216


217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  bytes_sent = send(eth->sockfd[Mod_id],
                    buff[0], 
                    packet_size,
                    0);
  
  if (bytes_sent == -1) {
    eth->num_tx_errors++;
    perror("ETHERNET WRITE: ");
    exit(-1);
  } else {
    eth->tx_actual_nsamps = bytes_sent>>1;
    eth->tx_count++;
  }
  
  return (bytes_sent-MAC_HEADER_SIZE_BYTES);  	  
}



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
int trx_eth_read_raw(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc) {
      
  int bytes_received=0;
  int i=0;
  eth_state_t *eth = (eth_state_t*)device->priv;
  int Mod_id = device->Mod_id;
  int rcvfrom_flag =0;
  
  eth->rx_nsamps=nsamps;

  for (i=0;i<cc;i++) {
      /* buff[i] points to the position in rx buffer where the payload to be received will be placed
	 buff2 points to the position in rx buffer where the packet header will be placed */
      void *buff2 = (void*)(buff[i]-APP_HEADER_SIZE_BYTES-MAC_HEADER_SIZE_BYTES); 
      
      /* we don't want to ovewrite with the header info the previous rx buffer data so we store it*/
      struct ether_header temp =  *(struct ether_header *)buff2;
      int32_t temp0 = *(int32_t *)(buff2 + MAC_HEADER_SIZE_BYTES);
      openair0_timestamp  temp1 = *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t));
      
      bytes_received=0;
      
      while(bytes_received < RAW_PACKET_SIZE_BYTES(nsamps)) {
	bytes_received +=recv(eth->sockfd[Mod_id],
			      buff2,
			      RAW_PACKET_SIZE_BYTES(nsamps),
			      rcvfrom_flag);
	
	if (bytes_received ==-1) {
	  eth->num_rx_errors++;
Raymond Knopp's avatar
Raymond Knopp committed
266
	  perror("ETHERNET IF5 READ: ");
267
          if (errno == EAGAIN) continue;
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
	  exit(-1);	
	} else {
	  /* store the timestamp value from packet's header */
	  *timestamp =  *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t));  
	  eth->rx_actual_nsamps=bytes_received>>2;   
	  eth->rx_count++;
	}
      }
      
#if DEBUG   
      printf("------- RX------: nu=%x an_id=%d ts%d bytes_recv=%d \n",
	     *(uint8_t *)(buff2+ETH_ALEN),
	     *(int16_t *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int16_t)),
	     *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t)),
	     bytes_received);
      dump_packet((device->host_type == BBU_HOST)? "BBU":"RRH", buff2, RAW_PACKET_SIZE_BYTES(nsamps),RX_FLAG);	  

#endif  

     /* tx buffer values restored */  
      *(struct ether_header *)buff2 = temp;
      *(int32_t *)(buff2 + MAC_HEADER_SIZE_BYTES) = temp0;
      *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t)) = temp1;
    }
  return (bytes_received-APP_HEADER_SIZE_BYTES-MAC_HEADER_SIZE_BYTES)>>2;
}

295 296


297
int trx_eth_read_raw_IF4p5(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc) {
298

299
  // Read nblocks info from packet itself
300
  int nblocks = nsamps;  
301 302 303 304
  int bytes_received=0;
  eth_state_t *eth = (eth_state_t*)device->priv;
  int Mod_id = device->Mod_id;
  
305 306
  ssize_t packet_size = MAC_HEADER_SIZE_BYTES + sizeof_IF4p5_header_t;      
  IF4p5_header_t *test_header = (IF4p5_header_t*)(buff[0] + MAC_HEADER_SIZE_BYTES);
Raymond Knopp's avatar
Raymond Knopp committed
307 308


309
  bytes_received = recv(eth->sockfd[Mod_id],
Sandeep Kumar's avatar
Sandeep Kumar committed
310
                        buff[0],
311
                        packet_size,
312
                        MSG_PEEK);                        
313 314
	if (bytes_received ==-1) {
	  eth->num_rx_errors++;
Raymond Knopp's avatar
Raymond Knopp committed
315
	  perror("ETHERNET IF4p5 READ (header): ");
316
	  exit(-1);	
317
  }
Sandeep Kumar's avatar
Sandeep Kumar committed
318
 
319 320
  *timestamp = test_header->sub_type; 
  
321 322 323 324
  if (test_header->sub_type == IF4p5_PDLFFT) {
    packet_size = RAW_IF4p5_PDLFFT_SIZE_BYTES(nblocks);             
  } else if (test_header->sub_type == IF4p5_PULFFT) {
    packet_size = RAW_IF4p5_PULFFT_SIZE_BYTES(nblocks);             
325
  } else {
326
    packet_size = RAW_IF4p5_PRACH_SIZE_BYTES;
327
  }
Raymond Knopp's avatar
Raymond Knopp committed
328 329
  
  
330
  while(bytes_received < packet_size) {
331 332 333 334
    bytes_received = recv(eth->sockfd[Mod_id],
                          buff[0],
                          packet_size,
                          0);
Raymond Knopp's avatar
Raymond Knopp committed
335 336 337 338
    if (bytes_received ==-1) {
      eth->num_rx_errors++;
      perror("ETHERNET IF4p5 READ (payload): ");
      exit(-1);	
339 340 341 342 343 344
    } else {
      eth->rx_actual_nsamps = bytes_received>>1;   
      eth->rx_count++;
    }
  }

345 346
  eth->rx_nsamps = nsamps;  
  return(bytes_received);
347 348
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365


int eth_set_dev_conf_raw(openair0_device *device) {

  int 	       Mod_id = device->Mod_id;
  eth_state_t *eth = (eth_state_t*)device->priv;
  void 	      *msg;
  ssize_t      msg_len;
  
  /* a BBU client sends to RRH a set of configuration parameters (openair0_config_t)
     so that RF front end is configured appropriately and
     frame/packet size etc. can be set */ 
  
  msg = malloc(MAC_HEADER_SIZE_BYTES + sizeof(openair0_config_t));
  msg_len = MAC_HEADER_SIZE_BYTES + sizeof(openair0_config_t);

  
366
  memcpy(msg,(void*)&eth->eh,MAC_HEADER_SIZE_BYTES);	
367
  memcpy((msg+MAC_HEADER_SIZE_BYTES),(void*)device->openair0_cfg,sizeof(openair0_config_t));
368 369 370 371 372 373 374 375 376 377 378 379 380
 	  
  if (send(eth->sockfd[Mod_id],
	     msg,
	     msg_len,
	     0)==-1) {
    perror("ETHERNET: ");
    exit(0);
  }
  
  return 0;
}


381

382
int eth_set_dev_conf_raw_IF4p5(openair0_device *device) {  
Sandeep Kumar's avatar
Sandeep Kumar committed
383
  // use for cc_id info
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

  int 	       Mod_id = device->Mod_id;
  eth_state_t *eth = (eth_state_t*)device->priv;
  void 	      *msg;
  ssize_t      msg_len;
  
  /* a BBU client sends to RRH a set of configuration parameters (openair0_config_t)
     so that RF front end is configured appropriately and
     frame/packet size etc. can be set */ 
  
  msg = malloc(MAC_HEADER_SIZE_BYTES + sizeof(openair0_config_t));
  msg_len = MAC_HEADER_SIZE_BYTES + sizeof(openair0_config_t);

  
  memcpy(msg,(void*)&eth->eh,MAC_HEADER_SIZE_BYTES);	
  memcpy((msg+MAC_HEADER_SIZE_BYTES),(void*)device->openair0_cfg,sizeof(openair0_config_t));
 	  
  if (send(eth->sockfd[Mod_id],
	     msg,
	     msg_len,
	     0)==-1) {
    perror("ETHERNET: ");
    exit(0);
  }
  
  return 0;
}


413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
int eth_get_dev_conf_raw(openair0_device *device) {

  eth_state_t   *eth = (eth_state_t*)device->priv;
  int 		Mod_id = device->Mod_id;
  void 		*msg;
  ssize_t	msg_len;
  
  msg = malloc(MAC_HEADER_SIZE_BYTES + sizeof(openair0_config_t));
  msg_len = MAC_HEADER_SIZE_BYTES + sizeof(openair0_config_t);
  
  /* RRH receives from BBU openair0_config_t */
  if (recv(eth->sockfd[Mod_id],
	   msg,
	   msg_len,
	   0)==-1) {
    perror("ETHERNET: ");
    exit(0);
  }
  
  /* RRH stores the remote MAC address */
433
  memcpy(eth->eh.ether_dhost,(msg+ETH_ALEN),ETH_ALEN);	
434 435
  //memcpy((void*)&device->openair0_cfg,(msg + MAC_HEADER_SIZE_BYTES), sizeof(openair0_config_t));
  device->openair0_cfg=(openair0_config_t *)(msg + MAC_HEADER_SIZE_BYTES);
436
  printf("[%s] binding mod_%d to hardware address %x:%x:%x:%x:%x:%x           hardware address %x:%x:%x:%x:%x:%x\n",((device->host_type == BBU_HOST) ? "BBU": "RRH"),Mod_id,eth->eh.ether_shost[0],eth->eh.ether_shost[1],eth->eh.ether_shost[2],eth->eh.ether_shost[3],eth->eh.ether_shost[4],eth->eh.ether_shost[5],eth->eh.ether_dhost[0],eth->eh.ether_dhost[1],eth->eh.ether_dhost[2],eth->eh.ether_dhost[3],eth->eh.ether_dhost[4],eth->eh.ether_dhost[5]);
437 438 439
 	  
  return 0;
}
440 441


442
int eth_get_dev_conf_raw_IF4p5(openair0_device *device) {
Sandeep Kumar's avatar
Sandeep Kumar committed
443
  // use for cc_id info
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

  eth_state_t   *eth = (eth_state_t*)device->priv;
  int 		Mod_id = device->Mod_id;
  void 		*msg;
  ssize_t	msg_len;
  
  msg = malloc(MAC_HEADER_SIZE_BYTES + sizeof(openair0_config_t));
  msg_len = MAC_HEADER_SIZE_BYTES + sizeof(openair0_config_t);
  
  /* RRH receives from BBU openair0_config_t */
  if (recv(eth->sockfd[Mod_id],
	   msg,
	   msg_len,
	   0)==-1) {
    perror("ETHERNET: ");
    exit(0);
  }
  
  /* RRH stores the remote MAC address */
  memcpy(eth->eh.ether_dhost,(msg+ETH_ALEN),ETH_ALEN);	
  //memcpy((void*)&device->openair0_cfg,(msg + MAC_HEADER_SIZE_BYTES), sizeof(openair0_config_t));
  //device->openair0_cfg=(openair0_config_t *)(msg + MAC_HEADER_SIZE_BYTES);
  printf("[%s] binding mod_%d to hardware address %x:%x:%x:%x:%x:%x           hardware address %x:%x:%x:%x:%x:%x\n",((device->host_type == BBU_HOST) ? "BBU": "RRH"),Mod_id,eth->eh.ether_shost[0],eth->eh.ether_shost[1],eth->eh.ether_shost[2],eth->eh.ether_shost[3],eth->eh.ether_shost[4],eth->eh.ether_shost[5],eth->eh.ether_dhost[0],eth->eh.ether_dhost[1],eth->eh.ether_dhost[2],eth->eh.ether_dhost[3],eth->eh.ether_dhost[4],eth->eh.ether_dhost[5]);
 	  
  return 0;
}