devices: af-packet gso mtu
[vpp.git] / src / vnet / devices / af_packet / af_packet.c
index f1a7b6a..2cc0cc7 100644 (file)
@@ -31,6 +31,7 @@
 #include <vlib/unix/unix.h>
 #include <vnet/ip/ip.h>
 #include <vnet/ethernet/ethernet.h>
+#include <vnet/interface/rx_queue_funcs.h>
 
 #include <vnet/devices/af_packet/af_packet.h>
 
@@ -67,7 +68,7 @@ af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
   af_packet_if_t *apif =
     pool_elt_at_index (apm->interfaces, hi->dev_instance);
 
-  if (ETHERNET_INTERFACE_FLAG_MTU == (flags & ETHERNET_INTERFACE_FLAG_MTU))
+  if (flags == ETHERNET_INTERFACE_FLAG_MTU)
     {
       s = format (0, "/sys/class/net/%s/mtu%c", apif->host_if_name, 0);
 
@@ -82,11 +83,32 @@ af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
          clib_error_free (error);
          return VNET_API_ERROR_SYSCALL_ERROR_1;
        }
+      else
+       apif->host_mtu = hi->max_packet_bytes;
     }
 
   return 0;
 }
 
+static int
+af_packet_read_mtu (af_packet_if_t *apif)
+{
+  af_packet_main_t *apm = &af_packet_main;
+  clib_error_t *error;
+  u8 *s;
+  s = format (0, "/sys/class/net/%s/mtu%c", apif->host_if_name, 0);
+  error = clib_sysfs_read ((char *) s, "%d", &apif->host_mtu);
+  vec_free (s);
+  if (error)
+    {
+      vlib_log_err (apm->log_class, "sysfs read failed to get MTU: %U",
+                   format_clib_error, error);
+      clib_error_free (error);
+      return VNET_API_ERROR_SYSCALL_ERROR_1;
+    }
+  return 0;
+}
+
 static clib_error_t *
 af_packet_fd_read_ready (clib_file_t * uf)
 {
@@ -99,8 +121,7 @@ af_packet_fd_read_ready (clib_file_t * uf)
     clib_bitmap_set (apm->pending_input_bitmap, idx, 1);
 
   /* Schedule the rx node */
-  vnet_device_input_set_interrupt_pending (vnm, apif->hw_if_index, 0);
-
+  vnet_hw_if_rx_queue_set_int_pending (vnm, apif->queue_index);
   return 0;
 }
 
@@ -128,7 +149,7 @@ create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req,
                       tpacket_req_t * tx_req, int *fd, u8 ** ring)
 {
   af_packet_main_t *apm = &af_packet_main;
-  int ret, err;
+  int ret;
   struct sockaddr_ll sll;
   int ver = TPACKET_V2;
   socklen_t req_sz = sizeof (struct tpacket_req);
@@ -137,55 +158,60 @@ create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req,
 
   if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
     {
-      vlib_log_debug (apm->log_class, "Failed to create socket");
+      vlib_log_debug (apm->log_class,
+                     "Failed to create AF_PACKET socket: %s (errno %d)",
+                     strerror (errno), errno);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
 
   /* bind before rx ring is cfged so we don't receive packets from other interfaces */
-  memset (&sll, 0, sizeof (sll));
+  clib_memset (&sll, 0, sizeof (sll));
   sll.sll_family = PF_PACKET;
   sll.sll_protocol = htons (ETH_P_ALL);
   sll.sll_ifindex = host_if_index;
-  if ((err = bind (*fd, (struct sockaddr *) &sll, sizeof (sll))) < 0)
+  if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
     {
       vlib_log_debug (apm->log_class,
-                     "Failed to bind rx packet socket (error %d)", err);
+                     "Failed to bind rx packet socket: %s (errno %d)",
+                     strerror (errno), errno);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
 
-  if ((err =
-       setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver))) < 0)
+  if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0)
     {
       vlib_log_debug (apm->log_class,
-                     "Failed to set rx packet interface version");
+                     "Failed to set rx packet interface version: %s (errno %d)",
+                     strerror (errno), errno);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
 
   int opt = 1;
-  if ((err =
-       setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt))) < 0)
+  if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
     {
       vlib_log_debug (apm->log_class,
-                     "Failed to set packet tx ring error handling option");
+                     "Failed to set packet tx ring error handling option: %s (errno %d)",
+                     strerror (errno), errno);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
 
-  if ((err =
-       setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0)
+  if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
     {
-      vlib_log_debug (apm->log_class, "Failed to set packet rx ring options");
+      vlib_log_debug (apm->log_class,
+                     "Failed to set packet rx ring options: %s (errno %d)",
+                     strerror (errno), errno);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
 
-  if ((err =
-       setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0)
+  if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0)
     {
-      vlib_log_debug (apm->log_class, "Failed to set packet tx ring options");
+      vlib_log_debug (apm->log_class,
+                     "Failed to set packet tx ring options: %s (errno %d)",
+                     strerror (errno), errno);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
@@ -195,7 +221,8 @@ create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req,
          0);
   if (*ring == MAP_FAILED)
     {
-      vlib_log_debug (apm->log_class, "mmap failure");
+      vlib_log_debug (apm->log_class, "mmap failure: %s (errno %d)",
+                     strerror (errno), errno);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
@@ -203,8 +230,10 @@ create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req,
   return 0;
 error:
   if (*fd >= 0)
-    close (*fd);
-  *fd = -1;
+    {
+      close (*fd);
+      *fd = -1;
+    }
   return ret;
 }
 
@@ -227,7 +256,7 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
   vnet_main_t *vnm = vnet_get_main ();
   uword *p;
   uword if_index;
-  u8 *host_if_name_dup = vec_dup (host_if_name);
+  u8 *host_if_name_dup = 0;
   int host_if_index = -1;
 
   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
@@ -238,6 +267,8 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
       return VNET_API_ERROR_IF_ALREADY_EXISTS;
     }
 
+  host_if_name_dup = vec_dup (host_if_name);
+
   vec_validate (rx_req, 0);
   rx_req->tp_block_size = AF_PACKET_RX_BLOCK_SIZE;
   rx_req->tp_frame_size = AF_PACKET_RX_FRAME_SIZE;
@@ -256,39 +287,52 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
    */
   if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
     {
-      vlib_log_debug (apm->log_class, "Failed to create socket");
+      vlib_log_debug (apm->log_class,
+                     "Failed to create AF_UNIX socket: %s (errno %d)",
+                     strerror (errno), errno);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
 
   clib_memcpy (ifr.ifr_name, (const char *) host_if_name,
               vec_len (host_if_name));
-  if ((ret = ioctl (fd2, SIOCGIFINDEX, &ifr)) < 0)
+  if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
     {
-      vlib_log_debug (apm->log_class, "af_packet_create error: %d", ret);
-      close (fd2);
-      return VNET_API_ERROR_INVALID_INTERFACE;
+      vlib_log_debug (apm->log_class,
+                     "Failed to retrieve the interface (%s) index: %s (errno %d)",
+                     host_if_name, strerror (errno), errno);
+      ret = VNET_API_ERROR_INVALID_INTERFACE;
+      goto error;
     }
 
   host_if_index = ifr.ifr_ifindex;
-  if ((ret = ioctl (fd2, SIOCGIFFLAGS, &ifr)) < 0)
+  if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
     {
-      vlib_log_warn (apm->log_class, "af_packet_create error: %d", ret);
+      vlib_log_debug (apm->log_class,
+                     "Failed to get the active flag: %s (errno %d)",
+                     strerror (errno), errno);
+      ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
 
   if (!(ifr.ifr_flags & IFF_UP))
     {
       ifr.ifr_flags |= IFF_UP;
-      if ((ret = ioctl (fd2, SIOCSIFFLAGS, &ifr)) < 0)
+      if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
        {
-         vlib_log_warn (apm->log_class, "af_packet_create error: %d", ret);
+         vlib_log_debug (apm->log_class,
+                         "Failed to set the active flag: %s (errno %d)",
+                         strerror (errno), errno);
+         ret = VNET_API_ERROR_SYSCALL_ERROR_1;
          goto error;
        }
     }
 
   if (fd2 > -1)
-    close (fd2);
+    {
+      close (fd2);
+      fd2 = -1;
+    }
 
   ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring);
 
@@ -315,20 +359,13 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
   apif->next_tx_frame = 0;
   apif->next_rx_frame = 0;
 
+  ret = af_packet_read_mtu (apif);
+  if (ret != 0)
+    goto error;
+
   if (tm->n_vlib_mains > 1)
     clib_spinlock_init (&apif->lockp);
 
-  {
-    clib_file_t template = { 0 };
-    template.read_function = af_packet_fd_read_ready;
-    template.file_descriptor = fd;
-    template.private_data = if_index;
-    template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
-    template.description = format (0, "%U", format_af_packet_device_name,
-                                  if_index);
-    apif->clib_file_index = clib_file_add (&file_main, &template);
-  }
-
   /*use configured or generate random MAC address */
   if (hw_addr_set)
     clib_memcpy (hw_addr, hw_addr_set, 6);
@@ -350,7 +387,7 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
 
   if (error)
     {
-      memset (apif, 0, sizeof (*apif));
+      clib_memset (apif, 0, sizeof (*apif));
       pool_put (apm->interfaces, apif);
       vlib_log_err (apm->log_class, "Unable to register interface: %U",
                    format_clib_error, error);
@@ -362,18 +399,30 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
   sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index);
   hw = vnet_get_hw_interface (vnm, apif->hw_if_index);
   apif->sw_if_index = sw->sw_if_index;
-  vnet_hw_interface_set_input_node (vnm, apif->hw_if_index,
-                                   af_packet_input_node.index);
-
-  vnet_hw_interface_assign_rx_thread (vnm, apif->hw_if_index, 0,       /* queue */
-                                     ~0 /* any cpu */ );
+  vnet_hw_if_set_input_node (vnm, apif->hw_if_index,
+                            af_packet_input_node.index);
+  apif->queue_index = vnet_hw_if_register_rx_queue (vnm, apif->hw_if_index, 0,
+                                                   VNET_HW_IF_RXQ_THREAD_ANY);
 
   hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
   vnet_hw_interface_set_flags (vnm, apif->hw_if_index,
                               VNET_HW_INTERFACE_FLAG_LINK_UP);
 
-  vnet_hw_interface_set_rx_mode (vnm, apif->hw_if_index, 0,
-                                VNET_HW_INTERFACE_RX_MODE_INTERRUPT);
+  vnet_hw_if_set_rx_queue_mode (vnm, apif->queue_index,
+                               VNET_HW_IF_RX_MODE_INTERRUPT);
+  vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index);
+  {
+    clib_file_t template = { 0 };
+    template.read_function = af_packet_fd_read_ready;
+    template.file_descriptor = fd;
+    template.private_data = if_index;
+    template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
+    template.description =
+      format (0, "%U", format_af_packet_device_name, if_index);
+    apif->clib_file_index = clib_file_add (&file_main, &template);
+  }
+  vnet_hw_if_set_rx_queue_file_index (vnm, apif->queue_index,
+                                     apif->clib_file_index);
 
   mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
                 0);
@@ -384,7 +433,10 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
 
 error:
   if (fd2 > -1)
-    close (fd2);
+    {
+      close (fd2);
+      fd2 = -1;
+    }
   vec_free (host_if_name_dup);
   vec_free (rx_req);
   vec_free (tx_req);
@@ -413,7 +465,6 @@ af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
 
   /* bring down the interface */
   vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0);
-  vnet_hw_interface_unassign_rx_thread (vnm, apif->hw_if_index, 0);
 
   /* clean up */
   if (apif->clib_file_index != ~0)
@@ -479,17 +530,19 @@ af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
   af_packet_if_detail_t *r_af_packet_ifs = NULL;
   af_packet_if_detail_t *af_packet_if = NULL;
 
-  vec_foreach (apif, apm->interfaces)
-  {
-    vec_add2 (r_af_packet_ifs, af_packet_if, 1);
-    af_packet_if->sw_if_index = apif->sw_if_index;
-    if (apif->host_if_name)
-      {
-       clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
-                    MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
-                         strlen ((const char *) apif->host_if_name)));
-      }
-  }
+  /* *INDENT-OFF* */
+  pool_foreach (apif, apm->interfaces)
+     {
+      vec_add2 (r_af_packet_ifs, af_packet_if, 1);
+      af_packet_if->sw_if_index = apif->sw_if_index;
+      if (apif->host_if_name)
+       {
+         clib_memcpy (af_packet_if->host_if_name, apif->host_if_name,
+                      MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1,
+                      strlen ((const char *) apif->host_if_name)));
+       }
+    }
+  /* *INDENT-ON* */
 
   *out_af_packet_ifs = r_af_packet_ifs;
 
@@ -502,7 +555,7 @@ af_packet_init (vlib_main_t * vm)
   af_packet_main_t *apm = &af_packet_main;
   vlib_thread_main_t *tm = vlib_get_thread_main ();
 
-  memset (apm, 0, sizeof (af_packet_main_t));
+  clib_memset (apm, 0, sizeof (af_packet_main_t));
 
   mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));