af_packet: Fix lock position
[vpp.git] / src / vnet / devices / af_packet / device.c
index 1fb4000..0991378 100644 (file)
  */
 
 #include <linux/if_packet.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <net/if_arp.h>
 
 #include <vlib/vlib.h>
 #include <vlib/unix/unix.h>
@@ -47,7 +51,7 @@ static char *af_packet_tx_func_error_strings[] = {
 };
 
 
-static u8 *
+u8 *
 format_af_packet_device_name (u8 * s, va_list * args)
 {
   u32 i = va_arg (*args, u32);
@@ -83,6 +87,7 @@ af_packet_interface_tx (vlib_main_t * vm,
   vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
   af_packet_if_t *apif =
     pool_elt_at_index (apm->interfaces, rd->dev_instance);
+  clib_spinlock_lock_if_init (&apif->lockp);
   int block = 0;
   u32 block_size = apif->tx_req->tp_block_size;
   u32 frame_size = apif->tx_req->tp_frame_size;
@@ -119,17 +124,18 @@ af_packet_interface_tx (vlib_main_t * vm,
                       vlib_buffer_get_current (b0), len);
          offset += len;
        }
-      while ((bi = b0->next_buffer));
+      while ((bi =
+             (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0));
 
       tph->tp_len = tph->tp_snaplen = offset;
       tph->tp_status = TP_STATUS_SEND_REQUEST;
       n_sent++;
     next:
+      tx_frame = (tx_frame + 1) % frame_num;
+
       /* check if we've exhausted the ring */
       if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num))
        break;
-
-      tx_frame = (tx_frame + 1) % frame_num;
     }
 
   CLIB_MEMORY_BARRIER ();
@@ -152,6 +158,8 @@ af_packet_interface_tx (vlib_main_t * vm,
        }
     }
 
+  clib_spinlock_unlock_if_init (&apif->lockp);
+
   if (PREDICT_FALSE (frame_not_ready))
     vlib_error_count (vm, node->node_index,
                      AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready);
@@ -200,17 +208,63 @@ af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
   af_packet_if_t *apif =
     pool_elt_at_index (apm->interfaces, hw->dev_instance);
   u32 hw_flags;
+  int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
+  struct ifreq ifr;
+
+  if (0 > fd)
+    {
+      clib_unix_warning ("af_packet_%s could not open socket",
+                        apif->host_if_name);
+      return 0;
+    }
+
+  /* if interface is a bridge ignore */
+  if (apif->host_if_index < 0)
+    goto error;                        /* no error */
+
+  /* use host_if_index in case host name has changed */
+  ifr.ifr_ifindex = apif->host_if_index;
+  if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
+    {
+      clib_unix_warning ("af_packet_%s ioctl could not retrieve eth name",
+                        apif->host_if_name);
+      goto error;
+    }
 
   apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
 
+  if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0)
+    {
+      clib_unix_warning ("af_packet_%s error: %d",
+                        apif->is_admin_up ? "up" : "down", rv);
+      goto error;
+    }
+
   if (apif->is_admin_up)
-    hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
+    {
+      hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP;
+      ifr.ifr_flags |= IFF_UP;
+    }
   else
-    hw_flags = 0;
+    {
+      hw_flags = 0;
+      ifr.ifr_flags &= ~IFF_UP;
+    }
+
+  if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0)
+    {
+      clib_unix_warning ("af_packet_%s error: %d",
+                        apif->is_admin_up ? "up" : "down", rv);
+      goto error;
+    }
 
   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
 
-  return 0;
+error:
+  if (0 <= fd)
+    close (fd);
+
+  return 0;                    /* no error */
 }
 
 static clib_error_t *
@@ -222,6 +276,54 @@ af_packet_subif_add_del_function (vnet_main_t * vnm,
   return 0;
 }
 
+static clib_error_t *af_packet_set_mac_address_function
+  (struct vnet_hw_interface_t *hi, char *address)
+{
+  af_packet_main_t *apm = &af_packet_main;
+  af_packet_if_t *apif =
+    pool_elt_at_index (apm->interfaces, hi->dev_instance);
+  int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0);
+  struct ifreq ifr;
+
+  if (0 > fd)
+    {
+      clib_unix_warning ("af_packet_%s could not open socket",
+                        apif->host_if_name);
+      return 0;
+    }
+
+  /* if interface is a bridge ignore */
+  if (apif->host_if_index < 0)
+    goto error;                        /* no error */
+
+  /* use host_if_index in case host name has changed */
+  ifr.ifr_ifindex = apif->host_if_index;
+  if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0)
+    {
+      clib_unix_warning
+       ("af_packet_%s ioctl could not retrieve eth name, error: %d",
+        apif->host_if_name, rv);
+      goto error;
+    }
+
+  clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6);
+  ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
+
+  if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0)
+    {
+      clib_unix_warning ("af_packet_%s ioctl could not set mac, error: %d",
+                        apif->host_if_name, rv);
+      goto error;
+    }
+
+error:
+
+  if (0 <= fd)
+    close (fd);
+
+  return 0;                    /* no error */
+}
+
 /* *INDENT-OFF* */
 VNET_DEVICE_CLASS (af_packet_device_class) = {
   .name = "af-packet",
@@ -235,6 +337,7 @@ VNET_DEVICE_CLASS (af_packet_device_class) = {
   .clear_counters = af_packet_clear_hw_interface_counters,
   .admin_up_down_function = af_packet_interface_admin_up_down,
   .subif_add_del_function = af_packet_subif_add_del_function,
+  .mac_addr_change_function = af_packet_set_mac_address_function,
 };
 
 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (af_packet_device_class,