From 8014a94020693ba21f0ed3a53d399af12da1e271 Mon Sep 17 00:00:00 2001
From: Raymond Knopp <knopp@eurecom.fr>
Date: Fri, 5 Aug 2016 09:58:57 +0200
Subject: [PATCH] IF4p5 with UDP.

---
 targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c |  19 +---
 targets/ARCH/ETHERNET/USERSPACE/LIB/eth_udp.c | 103 +++++++++++++++++-
 .../ETHERNET/USERSPACE/LIB/ethernet_lib.c     |  41 ++++---
 .../ETHERNET/USERSPACE/LIB/ethernet_lib.h     |   5 +-
 targets/ARCH/ETHERNET/USERSPACE/LIB/if_defs.h |   3 +
 5 files changed, 133 insertions(+), 38 deletions(-)

diff --git a/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c b/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c
index cfff2158a7..126030d3d4 100644
--- a/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c
+++ b/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_raw.c
@@ -65,23 +65,18 @@ int eth_socket_init_raw(openair0_device *device) {
   eth_state_t *eth = (eth_state_t*)device->priv;
   int Mod_id = device->Mod_id;
   const char *local_mac, *remote_mac;
-  int local_port=0, remote_port=0;
   int sock_dom=0;
   int sock_type=0;
   int sock_proto=0;  
  
   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) */
     local_mac = device->openair0_cfg->my_addr; 
-    local_port = device->openair0_cfg->my_port;    
     remote_mac = malloc(ETH_ALEN);
-    memset(remote_mac,0,ETH_ALEN);
-    remote_port = 0;    
+    memset((void*)remote_mac,0,ETH_ALEN);
     printf("[%s] local MAC addr %s remote MAC addr %s\n","RRH", local_mac,remote_mac);    
   } else {
     local_mac = device->openair0_cfg->my_addr;
-    local_port = device->openair0_cfg->my_port;  
     remote_mac = device->openair0_cfg->remote_addr;
-    remote_port = device->openair0_cfg->remote_port;  
     printf("[%s] local MAC addr %s remote MAC addr %s\n","BBU", local_mac,remote_mac);    
   }
    
@@ -182,7 +177,7 @@ int trx_eth_write_raw(openair0_device *device, openair0_timestamp timestamp, voi
 	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)),
-	       *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t)),
+	       *(openair0_timestamp *)(buff2 + MAC_HEADER_SIZE_BYTES + sizeof(int32_t)), 
 	       bytes_sent);
     dump_packet((device->host_type == BBU_HOST)? "BBU":"RRH", buff2, RAW_PACKET_SIZE_BYTES(nsamps), TX_FLAG);
 #endif
@@ -343,10 +338,10 @@ int trx_eth_read_raw_IF4p5(openair0_device *device, openair0_timestamp *timestam
                           buff[0],
                           packet_size,
                           0);
-	  if (bytes_received ==-1) {
-	    eth->num_rx_errors++;
-	    perror("ETHERNET IF4p5 READ (payload): ");
-	    exit(-1);	
+    if (bytes_received ==-1) {
+      eth->num_rx_errors++;
+      perror("ETHERNET IF4p5 READ (payload): ");
+      exit(-1);	
     } else {
       eth->rx_actual_nsamps = bytes_received>>1;   
       eth->rx_count++;
@@ -425,7 +420,6 @@ int eth_get_dev_conf_raw(openair0_device *device) {
 
   eth_state_t   *eth = (eth_state_t*)device->priv;
   int 		Mod_id = device->Mod_id;
-  char 		str[INET_ADDRSTRLEN];
   void 		*msg;
   ssize_t	msg_len;
   
@@ -456,7 +450,6 @@ int eth_get_dev_conf_raw_IF4p5(openair0_device *device) {
 
   eth_state_t   *eth = (eth_state_t*)device->priv;
   int 		Mod_id = device->Mod_id;
-  char 		str[INET_ADDRSTRLEN];
   void 		*msg;
   ssize_t	msg_len;
   
diff --git a/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_udp.c b/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_udp.c
index a9706c73bd..863b0d02d2 100644
--- a/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_udp.c
+++ b/targets/ARCH/ETHERNET/USERSPACE/LIB/eth_udp.c
@@ -63,7 +63,7 @@ uint16_t pck_seq_num = 1;
 uint16_t pck_seq_num_cur=0;
 uint16_t pck_seq_num_prev=0;
 
- int eth_socket_init_udp(openair0_device *device) {
+int eth_socket_init_udp(openair0_device *device) {
 
   int i = 0;
   eth_state_t *eth = (eth_state_t*)device->priv;
@@ -138,6 +138,103 @@ uint16_t pck_seq_num_prev=0;
   return 0;
 }
 
+int trx_eth_read_udp_IF4p5(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc) {
+
+  // Read nblocks info from packet itself
+  int nblocks = nsamps;  
+  int bytes_received=0;
+  eth_state_t *eth = (eth_state_t*)device->priv;
+  int Mod_id = device->Mod_id;
+  
+  ssize_t packet_size = sizeof_IF4p5_header_t;      
+  IF4p5_header_t *test_header = (IF4p5_header_t*)(buff[0]);
+
+
+  bytes_received = recvfrom(eth->sockfd[Mod_id],
+			    buff[0],
+			    packet_size,
+			    0,
+			    (struct sockaddr *)&dest_addr[Mod_id],
+			    (socklen_t *)&addr_len[Mod_id]);
+  
+  if (bytes_received ==-1) {
+    eth->num_rx_errors++;
+    perror("ETHERNET IF4p5 READ (header): ");
+    exit(-1);	
+  }
+  
+  *timestamp = test_header->sub_type; 
+  
+  if (test_header->sub_type == IF4p5_PDLFFT) {
+    packet_size = UDP_IF4p5_PDLFFT_SIZE_BYTES(nblocks);             
+  } else if (test_header->sub_type == IF4p5_PULFFT) {
+    packet_size = UDP_IF4p5_PULFFT_SIZE_BYTES(nblocks);             
+  } else {
+    packet_size = UDP_IF4p5_PRACH_SIZE_BYTES;
+  }
+  
+  
+  while(bytes_received < packet_size) {
+    bytes_received = recvfrom(eth->sockfd[Mod_id],
+			      buff[0],
+			      packet_size,
+			      0,
+			      (struct sockaddr *)&dest_addr[Mod_id],
+			      (socklen_t *)&addr_len[Mod_id]);
+    if (bytes_received ==-1) {
+      eth->num_rx_errors++;
+      perror("ETHERNET IF4p5 READ (payload): ");
+      exit(-1);	
+    } else {
+      eth->rx_actual_nsamps = bytes_received>>1;   
+      eth->rx_count++;
+    }
+  }
+
+  eth->rx_nsamps = nsamps;  
+  return(bytes_received);
+}
+
+int trx_eth_write_udp_IF4p5(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps, int cc, int flags) {
+
+  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;
+  
+  if (flags == IF4p5_PDLFFT) {
+    packet_size = UDP_IF4p5_PDLFFT_SIZE_BYTES(nblocks);    
+  } else if (flags == IF4p5_PULFFT) {
+    packet_size = UDP_IF4p5_PULFFT_SIZE_BYTES(nblocks);    
+  } else {
+    printf("trx_eth_write_udp_IF4p5: unknown flags %d\n",flags);
+    return(-1);
+  }
+   
+  eth->tx_nsamps = nblocks;
+  
+  bytes_sent = sendto(eth->sockfd[Mod_id],
+		      buff[0], 
+		      packet_size,
+		      0,
+		      (struct sockaddr*)&dest_addr[Mod_id],
+		      addr_len[Mod_id]);
+  
+  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);  	  
+}
+
 int trx_eth_write_udp(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps,int cc, int flags) {	
   
   int bytes_sent=0;
@@ -216,7 +313,7 @@ int trx_eth_read_udp(openair0_device *device, openair0_timestamp *timestamp, voi
   
   int bytes_received=0;
   eth_state_t *eth = (eth_state_t*)device->priv;
-  openair0_timestamp prev_timestamp = -1;
+  //  openair0_timestamp prev_timestamp = -1;
   int Mod_id = device->Mod_id;
   int rcvfrom_flag =0;
   int block_cnt=0;
@@ -298,7 +395,7 @@ int trx_eth_read_udp(openair0_device *device, openair0_timestamp *timestamp, voi
 	   pck_seq_num_cur = *(uint16_t *)buff2;
 	   //printf("cur=%d prev=%d buff=%d\n",pck_seq_num_cur,pck_seq_num_prev,*(uint16_t *)(buff2));
 	   if ( ( pck_seq_num_cur != (pck_seq_num_prev + 1) ) && !((pck_seq_num_prev==75) && (pck_seq_num_cur==1 ))){
-	     printf("out of order packet received1! %d|%d|%d\n",pck_seq_num_cur,pck_seq_num_prev,	*timestamp);
+	     printf("out of order packet received1! %d|%d|%d\n",pck_seq_num_cur,pck_seq_num_prev,(int)*timestamp);
 	   }
 	   VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_RX_SEQ_NUM,pck_seq_num_cur);
 	   VCD_SIGNAL_DUMPER_DUMP_VARIABLE_BY_NAME( VCD_SIGNAL_DUMPER_VARIABLES_RX_SEQ_NUM_PRV,pck_seq_num_prev);
diff --git a/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.c b/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.c
index e5d606f2d7..6079781ba1 100644
--- a/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.c
+++ b/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.c
@@ -90,19 +90,15 @@ int trx_eth_start(openair0_device *device) {
     if(ethernet_tune (device,RCV_TIMEOUT,999999)!=0)  return -1;
   } else if (eth->flags == ETH_UDP_IF4p5_MODE) {
     printf("Setting ETHERNET to UDP_IF4p5_MODE\n");
-  
+    if (eth_socket_init_udp(device)!=0)   return -1; 
+    if (device->host_type == BBU_HOST) {
+      if(eth_set_dev_conf_udp(device)!=0)  return -1;
+    } else {
+      if(eth_get_dev_conf_udp(device)!=0)  return -1;
+    }
   } else if (eth->flags == ETH_RAW_IF5_MOBIPASS) {
     printf("Setting ETHERNET to RAW_IF5_MODE\n");
     if (eth_socket_init_raw(device)!=0)   return -1;
-    /* RRH gets openair0 device configuration - BBU sets openair0 device configuration*/
-    //if (device->host_type == BBU_HOST) {
-      //if(eth_set_dev_conf_raw_IF4p5(device)!=0)  return -1;
-    //} else {
-      //if(eth_get_dev_conf_raw_IF4p5(device)!=0)  return -1;
-//
-    /* adjust MTU wrt number of samples per packet */
-   // if(ethernet_tune (device,MTU_SIZE,RAW_PACKET_SIZE_BYTES(device->openair0_cfg->samples_per_packet))!=0)  return -1;
-
   } else {
     if (eth_socket_init_udp(device)!=0)   return -1; 
     /* RRH gets openair0 device configuration - BBU sets openair0 device configuration*/
@@ -171,7 +167,7 @@ int trx_eth_reply(openair0_device *device, void *msg, ssize_t msg_len) {
 
 
 
-int trx_eth_stop(int card) {
+int trx_eth_stop(openair0_device *device) {
   return(0);
 }
 
@@ -199,11 +195,11 @@ int ethernet_tune(openair0_device *device, unsigned int option, int value) {
   struct timeval timeout;
   struct ifreq ifr;   
   char system_cmd[256]; 
-  char* if_name=DEFAULT_IF;
-  struct in_addr ia;
-  struct if_nameindex *ids;
+  //  char* if_name=DEFAULT_IF;
+  //  struct in_addr ia;
+  //  struct if_nameindex *ids;
   int ret=0;
-  int i=0;
+  //  int i=0;
   
   /****************** socket level options ************************/  
   switch(option) {
@@ -238,7 +234,7 @@ int ethernet_tune(openair0_device *device, unsigned int option, int value) {
 		   (char *)&timeout,sizeof(timeout))) {
       perror("[ETHERNET] setsockopt()");  
     } else {   
-      printf( "receive timeout= %u usec\n",timeout.tv_usec);  
+      printf( "receive timeout= %u usec\n",(unsigned int)timeout.tv_usec);  
     }  
     break;
     
@@ -251,7 +247,7 @@ int ethernet_tune(openair0_device *device, unsigned int option, int value) {
 		   (char *)&timeout,sizeof(timeout))) {
       perror("[ETHERNET] setsockopt()");     
     } else {
-      printf( "send timeout= %d,%d sec\n",timeout.tv_sec,timeout.tv_usec);    
+      printf( "send timeout= %d,%d sec\n",(int)timeout.tv_sec,(int)timeout.tv_usec);    
     }
     break;
     
@@ -311,7 +307,7 @@ int ethernet_tune(openair0_device *device, unsigned int option, int value) {
     break;
     
   case RING_PAR:
-    ret=snprintf(system_cmd,sizeof(system_cmd),"ethtool -G %s rx %d tx %d",eth->if_name[Mod_id],value);
+    ret=snprintf(system_cmd,sizeof(system_cmd),"ethtool -G %s val %d",eth->if_name[Mod_id],value);
     if (ret > 0) {
       ret=system(system_cmd);
       if (ret == -1) {
@@ -376,6 +372,9 @@ int transport_init(openair0_device *device, openair0_config_t *openair0_cfg, eth
   } else if (eth->flags == ETH_RAW_IF4p5_MODE) {
     device->trx_write_func   = trx_eth_write_raw_IF4p5;
     device->trx_read_func    = trx_eth_read_raw_IF4p5;     
+  } else if (eth->flags == ETH_UDP_IF4p5_MODE) {
+    device->trx_write_func   = trx_eth_write_udp_IF4p5;
+    device->trx_read_func    = trx_eth_read_udp_IF4p5;     
   } else if (eth->flags == ETH_RAW_IF5_MOBIPASS) {
     device->trx_write_func   = trx_eth_write_raw_IF4p5;
     device->trx_read_func    = trx_eth_read_raw_IF4p5;     
@@ -460,7 +459,7 @@ void dump_dev(openair0_device *device) {
         device->openair0_cfg->num_rb_dl, device->openair0_cfg->sample_rate);
   printf("       BBU configured for %i tx/%i rx channels)\n",
 	device->openair0_cfg->tx_num_channels,device->openair0_cfg->rx_num_channels);
-   printf("       Running flags: %s %s %s\n",      
+   printf("       Running flags: %s %s (\n",      
 	((eth->flags & ETH_RAW_MODE)  ? "RAW socket mode - ":""),
 	((eth->flags & ETH_UDP_MODE)  ? "UDP socket mode - ":""));	  	
   printf("       Number of iqs dumped when displaying packets: %i\n\n",eth->iqdumpcnt);   
@@ -470,14 +469,14 @@ void dump_dev(openair0_device *device) {
 void inline dump_txcounters(openair0_device *device) {
   eth_state_t *eth = (eth_state_t*)device->priv;  
   printf("   Ethernet device interface %i, tx counters:\n" ,device->openair0_cfg->Mod_id);
-  printf("   Sent packets: %llu send errors: %i\n",   eth->tx_count, eth->num_tx_errors);	 
+  printf("   Sent packets: %llu send errors: %i\n",   (long long unsigned int)eth->tx_count, eth->num_tx_errors);	 
 }
 
 void inline dump_rxcounters(openair0_device *device) {
 
   eth_state_t *eth = (eth_state_t*)device->priv;
   printf("   Ethernet device interface %i rx counters:\n" ,device->openair0_cfg->Mod_id);
-  printf("   Received packets: %llu missed packets errors: %i\n", eth->rx_count, eth->num_underflows);	 
+  printf("   Received packets: %llu missed packets errors: %i\n", (long long unsigned int)eth->rx_count, eth->num_underflows);	 
 }  
 
 void inline dump_buff(openair0_device *device, char *buff,unsigned int tx_rx_flag, int nsamps) {
diff --git a/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.h b/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.h
index 3adb0f45cb..0d80dbd023 100644
--- a/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.h
+++ b/targets/ARCH/ETHERNET/USERSPACE/LIB/ethernet_lib.h
@@ -176,9 +176,10 @@ typedef struct {
 void dump_packet(char *title, unsigned char* pkt, int bytes, unsigned int tx_rx_flag);
 unsigned short calc_csum (unsigned short *buf, int nwords);
 void dump_dev(openair0_device *device);
-void inline dump_buff(openair0_device *device, char *buff,unsigned int tx_rx_flag,int nsamps);
+/*void inline dump_buff(openair0_device *device, char *buff,unsigned int tx_rx_flag,int nsamps);
 void inline dump_rxcounters(openair0_device *device);
 void inline dump_txcounters(openair0_device *device);
+*/
 void dump_iqs(char * buff, int iq_cnt);
 
 
@@ -224,6 +225,8 @@ int trx_eth_write_raw(openair0_device *device, openair0_timestamp timestamp, voi
 int trx_eth_read_raw(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc);
 int trx_eth_write_raw_IF4p5(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps,int cc, int flags);
 int trx_eth_read_raw_IF4p5(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc);
+int trx_eth_write_udp_IF4p5(openair0_device *device, openair0_timestamp timestamp, void **buff, int nsamps,int cc, int flags);
+int trx_eth_read_udp_IF4p5(openair0_device *device, openair0_timestamp *timestamp, void **buff, int nsamps, int cc);
 int eth_get_dev_conf_raw(openair0_device *device);
 int eth_set_dev_conf_raw(openair0_device *device);
 int eth_get_dev_conf_raw_IF4p5(openair0_device *device);
diff --git a/targets/ARCH/ETHERNET/USERSPACE/LIB/if_defs.h b/targets/ARCH/ETHERNET/USERSPACE/LIB/if_defs.h
index e118e9f99f..855ff49704 100644
--- a/targets/ARCH/ETHERNET/USERSPACE/LIB/if_defs.h
+++ b/targets/ARCH/ETHERNET/USERSPACE/LIB/if_defs.h
@@ -65,6 +65,9 @@
 #define RAW_IF4p5_PDLFFT_SIZE_BYTES(nblocks) (MAC_HEADER_SIZE_BYTES + sizeof_IF4p5_header_t + DATA_BLOCK_SIZE_BYTES(nblocks))  
 #define RAW_IF4p5_PULFFT_SIZE_BYTES(nblocks) (MAC_HEADER_SIZE_BYTES + sizeof_IF4p5_header_t + DATA_BLOCK_SIZE_BYTES(nblocks))  
 #define RAW_IF4p5_PRACH_SIZE_BYTES (MAC_HEADER_SIZE_BYTES + sizeof_IF4p5_header_t + PRACH_BLOCK_SIZE_BYTES)
+#define UDP_IF4p5_PDLFFT_SIZE_BYTES(nblocks) (sizeof_IF4p5_header_t + DATA_BLOCK_SIZE_BYTES(nblocks))  
+#define UDP_IF4p5_PULFFT_SIZE_BYTES(nblocks) (sizeof_IF4p5_header_t + DATA_BLOCK_SIZE_BYTES(nblocks))  
+#define UDP_IF4p5_PRACH_SIZE_BYTES (sizeof_IF4p5_header_t + PRACH_BLOCK_SIZE_BYTES)
 
 // Mobipass packet sizes
 #define RAW_IF5_MOBIPASS_BLOCK_SIZE_BYTES 1280
-- 
GitLab