misc: move to new pool_foreach macros
[vpp.git] / src / vnet / devices / af_packet / af_packet.c
index 2a13510..31d4df0 100644 (file)
@@ -19,6 +19,8 @@
 
 #include <linux/if_ether.h>
 #include <linux/if_packet.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
 #include <dirent.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -34,8 +36,6 @@
 
 af_packet_main_t af_packet_main;
 
-#define AF_PACKET_DEBUG_SOCKET         0
-
 #define AF_PACKET_TX_FRAMES_PER_BLOCK  1024
 #define AF_PACKET_TX_FRAME_SIZE                (2048 * 5)
 #define AF_PACKET_TX_BLOCK_NR          1
@@ -52,12 +52,6 @@ af_packet_main_t af_packet_main;
 #define AF_PACKET_RX_BLOCK_SIZE                (AF_PACKET_RX_FRAME_SIZE * \
                                         AF_PACKET_RX_FRAMES_PER_BLOCK)
 
-#if AF_PACKET_DEBUG_SOCKET == 1
-#define DBG_SOCK(args...) clib_warning(args);
-#else
-#define DBG_SOCK(args...)
-#endif
-
 /*defined in net/if.h but clashes with dpdk headers */
 unsigned int if_nametoindex (const char *ifname);
 
@@ -73,7 +67,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,7 +76,10 @@ af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
 
       if (error)
        {
-         clib_error_report (error);
+         vlib_log_err (apm->log_class,
+                       "sysfs write failed to change MTU: %U",
+                       format_clib_error, error);
+         clib_error_free (error);
          return VNET_API_ERROR_SYSCALL_ERROR_1;
        }
     }
@@ -130,7 +127,8 @@ 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;
+  af_packet_main_t *apm = &af_packet_main;
+  int ret;
   struct sockaddr_ll sll;
   int ver = TPACKET_V2;
   socklen_t req_sz = sizeof (struct tpacket_req);
@@ -139,62 +137,71 @@ 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)
     {
-      DBG_SOCK ("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;
     }
 
-  if ((err =
-       setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver))) < 0)
+  /* bind before rx ring is cfged so we don't receive packets from other interfaces */
+  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 (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0)
     {
-      DBG_SOCK ("Failed to set rx packet interface version");
+      vlib_log_debug (apm->log_class,
+                     "Failed to bind rx packet socket: %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_VERSION, &ver, sizeof (ver)) < 0)
     {
-      DBG_SOCK ("Failed to set packet tx ring error handling option");
+      vlib_log_debug (apm->log_class,
+                     "Failed to set rx packet interface version: %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)
+  int opt = 1;
+  if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0)
     {
-      DBG_SOCK ("Failed to set packet rx ring options");
+      vlib_log_debug (apm->log_class,
+                     "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_TX_RING, tx_req, req_sz)) < 0)
+  if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0)
     {
-      DBG_SOCK ("Failed to set packet tx 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;
     }
 
-  *ring =
-    mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd,
-         0);
-  if (*ring == MAP_FAILED)
+  if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0)
     {
-      DBG_SOCK ("mmap failure");
+      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;
     }
 
-  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)
+  *ring =
+    mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd,
+         0);
+  if (*ring == MAP_FAILED)
     {
-      DBG_SOCK ("Failed to bind rx packet socket (error %d)", err);
+      vlib_log_debug (apm->log_class, "mmap failure: %s (errno %d)",
+                     strerror (errno), errno);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
@@ -202,8 +209,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;
 }
 
@@ -212,9 +221,10 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
                     u32 * sw_if_index)
 {
   af_packet_main_t *apm = &af_packet_main;
-  int ret, fd = -1;
+  int ret, fd = -1, fd2 = -1;
   struct tpacket_req *rx_req = 0;
   struct tpacket_req *tx_req = 0;
+  struct ifreq ifr;
   u8 *ring = 0;
   af_packet_if_t *apif = 0;
   u8 hw_addr[6];
@@ -225,15 +235,19 @@ 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);
   if (p)
     {
-      return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
+      apif = vec_elt_at_index (apm->interfaces, p[0]);
+      *sw_if_index = apif->sw_if_index;
+      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;
@@ -246,12 +260,57 @@ 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;
 
-  host_if_index = if_nametoindex ((const char *) host_if_name);
+  /*
+   * make sure host side of interface is 'UP' before binding AF_PACKET
+   * socket on it.
+   */
+  if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
+    {
+      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 (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0)
+    {
+      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 (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0)
+    {
+      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 (!host_if_index)
+  if (!(ifr.ifr_flags & IFF_UP))
     {
-      DBG_SOCK ("Wrong host interface name");
-      return VNET_API_ERROR_INVALID_INTERFACE;
+      ifr.ifr_flags |= IFF_UP;
+      if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0)
+       {
+         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);
+      fd2 = -1;
     }
 
   ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring);
@@ -314,9 +373,11 @@ 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);
-      clib_error_report (error);
+      vlib_log_err (apm->log_class, "Unable to register interface: %U",
+                   format_clib_error, error);
+      clib_error_free (error);
       ret = VNET_API_ERROR_SYSCALL_ERROR_1;
       goto error;
     }
@@ -335,7 +396,7 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
                               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_RX_MODE_INTERRUPT);
 
   mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index,
                 0);
@@ -345,6 +406,11 @@ af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set,
   return 0;
 
 error:
+  if (fd2 > -1)
+    {
+      close (fd2);
+      fd2 = -1;
+    }
   vec_free (host_if_name_dup);
   vec_free (rx_req);
   vec_free (tx_req);
@@ -364,7 +430,8 @@ af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
   p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
   if (p == NULL)
     {
-      clib_warning ("Host interface %s does not exist", host_if_name);
+      vlib_log_warn (apm->log_class, "Host interface %s does not exist",
+                    host_if_name);
       return VNET_API_ERROR_SYSCALL_ERROR_1;
     }
   apif = pool_elt_at_index (apm->interfaces, p[0]);
@@ -386,8 +453,9 @@ af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name)
   ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr +
     apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr;
   if (munmap (apif->rx_ring, ring_sz))
-    clib_warning ("Host interface %s could not free rx/tx ring",
-                 host_if_name);
+    vlib_log_warn (apm->log_class,
+                  "Host interface %s could not free rx/tx ring",
+                  host_if_name);
   apif->rx_ring = NULL;
   apif->tx_ring = NULL;
   apif->fd = -1;
@@ -429,19 +497,49 @@ af_packet_set_l4_cksum_offload (vlib_main_t * vm, u32 sw_if_index, u8 set)
   return 0;
 }
 
+int
+af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs)
+{
+  af_packet_main_t *apm = &af_packet_main;
+  af_packet_if_t *apif;
+  af_packet_if_detail_t *r_af_packet_ifs = NULL;
+  af_packet_if_detail_t *af_packet_if = NULL;
+
+  /* *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;
+
+  return 0;
+}
+
 static clib_error_t *
 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));
 
   vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1,
                        CLIB_CACHE_LINE_BYTES);
 
+  apm->log_class = vlib_log_register_class ("af_packet", 0);
+  vlib_log_debug (apm->log_class, "initialized");
+
   return 0;
 }