Add extern to *_main global variable declarations in header files.
[vpp.git] / src / vnet / devices / af_packet / af_packet.c
index e491ba4..d0a241e 100644 (file)
 
 #include <linux/if_ether.h>
 #include <linux/if_packet.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
 
+#include <vppinfra/linux/sysfs.h>
 #include <vlib/vlib.h>
 #include <vlib/unix/unix.h>
 #include <vnet/ip/ip.h>
@@ -27,6 +32,8 @@
 
 #include <vnet/devices/af_packet/af_packet.h>
 
+af_packet_main_t af_packet_main;
+
 #define AF_PACKET_DEBUG_SOCKET         0
 
 #define AF_PACKET_TX_FRAMES_PER_BLOCK  1024
@@ -60,47 +67,76 @@ static u32
 af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
                           u32 flags)
 {
-  /* nothing for now */
+  clib_error_t *error;
+  u8 *s;
+  af_packet_main_t *apm = &af_packet_main;
+  af_packet_if_t *apif =
+    pool_elt_at_index (apm->interfaces, hi->dev_instance);
+
+  if (ETHERNET_INTERFACE_FLAG_MTU == (flags & ETHERNET_INTERFACE_FLAG_MTU))
+    {
+      s = format (0, "/sys/class/net/%s/mtu%c", apif->host_if_name, 0);
+
+      error = clib_sysfs_write ((char *) s, "%d", hi->max_packet_bytes);
+      vec_free (s);
+
+      if (error)
+       {
+         clib_error_report (error);
+         return VNET_API_ERROR_SYSCALL_ERROR_1;
+       }
+    }
+
   return 0;
 }
 
 static clib_error_t *
-af_packet_fd_read_ready (unix_file_t * uf)
+af_packet_fd_read_ready (clib_file_t * uf)
 {
-  vlib_main_t *vm = vlib_get_main ();
   af_packet_main_t *apm = &af_packet_main;
+  vnet_main_t *vnm = vnet_get_main ();
   u32 idx = uf->private_data;
+  af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, idx);
 
   apm->pending_input_bitmap =
     clib_bitmap_set (apm->pending_input_bitmap, idx, 1);
 
   /* Schedule the rx node */
-  vlib_node_set_interrupt_pending (vm, af_packet_input_node.index);
+  vnet_device_input_set_interrupt_pending (vnm, apif->hw_if_index, 0);
 
   return 0;
 }
 
 static int
-create_packet_v2_sock (u8 * name, tpacket_req_t * rx_req,
+is_bridge (const u8 * host_if_name)
+{
+  u8 *s;
+  DIR *dir = NULL;
+
+  s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0);
+  dir = opendir ((char *) s);
+  vec_free (s);
+
+  if (dir)
+    {
+      closedir (dir);
+      return 0;
+    }
+
+  return -1;
+}
+
+static int
+create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req,
                       tpacket_req_t * tx_req, int *fd, u8 ** ring)
 {
   int ret, err;
   struct sockaddr_ll sll;
-  uint host_if_index;
   int ver = TPACKET_V2;
   socklen_t req_sz = sizeof (struct tpacket_req);
   u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
     tx_req->tp_block_size * tx_req->tp_block_nr;
 
-  host_if_index = if_nametoindex ((const char *) name);
-
-  if (!host_if_index)
-    {
-      DBG_SOCK ("Wrong host interface name");
-      ret = VNET_API_ERROR_INVALID_INTERFACE;
-      goto error;
-    }
-
   if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
     {
       DBG_SOCK ("Failed to create socket");
@@ -171,31 +207,6 @@ error:
   return ret;
 }
 
-static void
-af_packet_worker_thread_enable ()
-{
-  /* If worker threads are enabled, switch to polling mode */
-  foreach_vlib_main ((
-                      {
-                      vlib_node_set_state (this_vlib_main,
-                                           af_packet_input_node.index,
-                                           VLIB_NODE_STATE_POLLING);
-                      }));
-
-}
-
-static void
-af_packet_worker_thread_disable ()
-{
-  foreach_vlib_main ((
-                      {
-                      vlib_node_set_state (this_vlib_main,
-                                           af_packet_input_node.index,
-                                           VLIB_NODE_STATE_INTERRUPT);
-                      }));
-
-}
-
 int
 af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
                     u32 * sw_if_index)
@@ -209,11 +220,13 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
   u8 hw_addr[6];
   clib_error_t *error;
   vnet_sw_interface_t *sw;
+  vnet_hw_interface_t *hw;
   vlib_thread_main_t *tm = vlib_get_thread_main ();
   vnet_main_t *vnm = vnet_get_main ();
   uword *p;
   uword if_index;
   u8 *host_if_name_dup = vec_dup (host_if_name);
+  int host_if_index = -1;
 
   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
   if (p)
@@ -233,15 +246,29 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
   tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
   tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR;
 
-  ret = create_packet_v2_sock (host_if_name, rx_req, tx_req, &fd, &ring);
+  host_if_index = if_nametoindex ((const char *) host_if_name);
+
+  if (!host_if_index)
+    {
+      DBG_SOCK ("Wrong host interface name");
+      return VNET_API_ERROR_INVALID_INTERFACE;
+    }
+
+  ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring);
 
   if (ret != 0)
     goto error;
 
+  ret = is_bridge (host_if_name);
+
+  if (ret == 0)                        /* is a bridge, ignore state */
+    host_if_index = -1;
+
   /* So far everything looks good, let's create interface */
   pool_get (apm->interfaces, apif);
   if_index = apif - apm->interfaces;
 
+  apif->host_if_index = host_if_index;
   apif->fd = fd;
   apif->rx_ring = ring;
   apif->tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;
@@ -253,19 +280,15 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
   apif->next_rx_frame = 0;
 
   if (tm->n_vlib_mains > 1)
-    {
-      apif->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
-                                           CLIB_CACHE_LINE_BYTES);
-      memset ((void *) apif->lockp, 0, CLIB_CACHE_LINE_BYTES);
-    }
+    clib_spinlock_init (&apif->lockp);
 
   {
-    unix_file_t template = { 0 };
+    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;
-    apif->unix_file_index = unix_file_add (&unix_main, &template);
+    apif->clib_file_index = clib_file_add (&file_main, &template);
   }
 
   /*use configured or generate random MAC address */
@@ -297,19 +320,26 @@ 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 */ );
+
+  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);
+
   mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
                 0);
   if (sw_if_index)
     *sw_if_index = apif->sw_if_index;
 
-  if (tm->n_vlib_mains > 1 && pool_elts (apm->interfaces) == 1)
-    af_packet_worker_thread_enable ();
-
   return 0;
 
 error:
@@ -323,7 +353,6 @@ int
 af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
 {
   vnet_main_t *vnm = vnet_get_main ();
-  vlib_thread_main_t *tm = vlib_get_thread_main ();
   af_packet_main_t *apm = &af_packet_main;
   af_packet_if_t *apif;
   uword *p;
@@ -341,12 +370,13 @@ 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->unix_file_index != ~0)
+  if (apif->clib_file_index != ~0)
     {
-      unix_file_del (&unix_main, unix_main.file_pool + apif->unix_file_index);
-      apif->unix_file_index = ~0;
+      clib_file_del (&file_main, file_main.file_pool + apif->clib_file_index);
+      apif->clib_file_index = ~0;
     }
   else
     close (apif->fd);
@@ -367,14 +397,29 @@ af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
 
   vec_free (apif->host_if_name);
   apif->host_if_name = NULL;
+  apif->host_if_index = -1;
 
   mhash_unset (&apm->if_index_by_host_if_name, host_if_name, &if_index);
 
   ethernet_delete_interface (vnm, apif->hw_if_index);
 
   pool_put (apm->interfaces, apif);
-  if (tm->n_vlib_mains > 1 && pool_elts (apm->interfaces) == 0)
-    af_packet_worker_thread_disable ();
+
+  return 0;
+}
+
+int
+af_packet_set_l4_cksum_offload (vlib_main_t * vm, u32 sw_if_index, u8 set)
+{
+  vnet_main_t *vnm = vnet_get_main ();
+  vnet_hw_interface_t *hw;
+
+  hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
+
+  if (set)
+    hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
+  else
+    hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
 
   return 0;
 }
@@ -384,24 +429,9 @@ af_packet_init (vlib_main_t * vm)
 {
   af_packet_main_t *apm = &af_packet_main;
   vlib_thread_main_t *tm = vlib_get_thread_main ();
-  vlib_thread_registration_t *tr;
-  uword *p;
 
   memset (apm, 0, sizeof (af_packet_main_t));
 
-  apm->input_cpu_first_index = 0;
-  apm->input_cpu_count = 1;
-
-  /* find out which cpus will be used for input */
-  p = hash_get_mem (tm->thread_registrations_by_name, "workers");
-  tr = p ? (vlib_thread_registration_t *) p[0] : 0;
-
-  if (tr && tr->count > 0)
-    {
-      apm->input_cpu_first_index = tr->first_index;
-      apm->input_cpu_count = tr->count;
-    }
-
   mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword));
 
   vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,