hs-test: cache docker build in local filesystem
[vpp.git] / src / plugins / dpdk / device / init.c
index 6b1479b..453d9cf 100644 (file)
@@ -30,7 +30,7 @@
 #include <dpdk/cryptodev/cryptodev.h>
 #include <vlib/pci/pci.h>
 #include <vlib/vmbus/vmbus.h>
-
+#include <vlib/stats/stats.h>
 #include <rte_ring.h>
 #include <rte_vect.h>
 
@@ -187,9 +187,11 @@ dpdk_find_startup_config (struct rte_eth_dev_info *di)
 {
   dpdk_main_t *dm = &dpdk_main;
   struct rte_pci_device *pci_dev;
-  struct rte_vmbus_device *vmbus_dev;
   vlib_pci_addr_t pci_addr;
+#ifdef __linux__
+  struct rte_vmbus_device *vmbus_dev;
   vlib_vmbus_addr_t vmbus_addr;
+#endif /* __linux__ */
   uword *p = 0;
 
   if ((pci_dev = dpdk_get_pci_device (di)))
@@ -202,22 +204,97 @@ dpdk_find_startup_config (struct rte_eth_dev_info *di)
        hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
     }
 
+#ifdef __linux__
   if ((vmbus_dev = dpdk_get_vmbus_device (di)))
     {
       unformat_input_t input_vmbus;
-      unformat_init_string (&input_vmbus, di->device->name,
-                           strlen (di->device->name));
+#if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
+      const char *dev_name = rte_dev_name (di->device);
+#else
+      const char *dev_name = di->device->name;
+#endif
+      unformat_init_string (&input_vmbus, dev_name, strlen (dev_name));
       if (unformat (&input_vmbus, "%U", unformat_vlib_vmbus_addr, &vmbus_addr))
        p = mhash_get (&dm->conf->device_config_index_by_vmbus_addr,
                       &vmbus_addr);
       unformat_free (&input_vmbus);
     }
+#endif /* __linux__ */
 
   if (p)
     return pool_elt_at_index (dm->conf->dev_confs, p[0]);
   return &dm->conf->default_devconf;
 }
 
+/*
+ * Initialise or refresh the xstats counters for a device
+ */
+void
+dpdk_counters_xstats_init (dpdk_device_t *xd)
+{
+  int len, ret, i;
+  struct rte_eth_xstat_name *xstats_names = 0;
+  char *name;
+  dpdk_driver_t *dr = xd->driver;
+
+  /* Only support xstats for supported drivers */
+  if (!dr)
+    return;
+
+  len = rte_eth_xstats_get_names (xd->port_id, 0, 0);
+  if (len < 0)
+    {
+      dpdk_log_err ("[%u] rte_eth_xstats_get_names failed: %d", xd->port_id,
+                   len);
+      return;
+    }
+  /* Counters for this driver is already initialised */
+  if (vec_len (dr->xstats_counters) == len)
+    {
+      vec_foreach_index (i, dr->xstats_counters)
+       {
+         vlib_validate_simple_counter (&dr->xstats_counters[i],
+                                       xd->sw_if_index);
+         vlib_zero_simple_counter (&dr->xstats_counters[i], xd->sw_if_index);
+       }
+      return;
+    }
+
+  /* Same driver, different interface, different length of counter array. */
+  ASSERT (vec_len (dr->xstats_counters) == 0);
+
+  vec_validate (xstats_names, len - 1);
+
+  ret = rte_eth_xstats_get_names (xd->port_id, xstats_names, len);
+  if (ret >= 0 && ret <= len)
+    {
+      vec_validate (dr->xstats_counters, len - 1);
+      vec_foreach_index (i, xstats_names)
+       {
+         name = (char *) format (0, "/if/%s/%s%c", dr->drivers->name,
+                                 xstats_names[i].name, 0);
+
+         /* There is a bug in the ENA driver where the xstats names are not
+          * unique. */
+         if (vlib_stats_find_entry_index (name) != STAT_SEGMENT_INDEX_INVALID)
+           {
+             vec_free (name);
+             name = (char *) format (0, "/if/%s/%s_%d%c", dr->drivers->name,
+                                     xstats_names[i].name, i, 0);
+           }
+
+         dr->xstats_counters[i].name = name;
+         dr->xstats_counters[i].stat_segment_name = name;
+         dr->xstats_counters[i].counters = 0;
+         vlib_validate_simple_counter (&dr->xstats_counters[i],
+                                       xd->sw_if_index);
+         vlib_zero_simple_counter (&dr->xstats_counters[i], xd->sw_if_index);
+         vec_free (name);
+       }
+    }
+  vec_free (xstats_names);
+}
+
 static clib_error_t *
 dpdk_lib_init (dpdk_main_t * dm)
 {
@@ -264,6 +341,7 @@ dpdk_lib_init (dpdk_main_t * dm)
       dpdk_device_config_t *devconf = 0;
       vnet_eth_interface_registration_t eir = {};
       dpdk_driver_t *dr;
+      i8 numa_node;
 
       if (!rte_eth_dev_is_valid_port (port_id))
        continue;
@@ -355,12 +433,13 @@ dpdk_lib_init (dpdk_main_t * dm)
                               pci_dev->addr.devid, pci_dev->addr.function);
          else
            xd->name = format (xd->name, "%u", port_id);
-       }
 
-      /* Handle representor devices that share the same PCI ID */
-      if ((di.switch_info.domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) &&
-         (di.switch_info.port_id != (uint16_t) -1))
-       xd->name = format (xd->name, "/%d", di.switch_info.port_id);
+         /* Handle representor devices that share the same PCI ID */
+         if ((di.switch_info.domain_id !=
+              RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) &&
+             (di.switch_info.port_id != (uint16_t) -1))
+           xd->name = format (xd->name, "/%d", di.switch_info.port_id);
+       }
 
       /* number of RX and TX queues */
       if (devconf->num_tx_queues > 0)
@@ -402,7 +481,8 @@ dpdk_lib_init (dpdk_main_t * dm)
       q = di.max_rx_pktlen - di.max_mtu;
 
       /* attempt to protect from bogus value provided by pmd */
-      if (q < (2 * xd->driver_frame_overhead) && q > 0)
+      if (q < (2 * xd->driver_frame_overhead) && q > 0 &&
+         di.max_mtu != UINT16_MAX)
        xd->driver_frame_overhead = q;
       dpdk_log_debug ("[%u] min_mtu: %u, max_mtu: %u, min_rx_bufsize: %u, "
                      "max_rx_pktlen: %u, max_lro_pkt_size: %u",
@@ -443,10 +523,18 @@ dpdk_lib_init (dpdk_main_t * dm)
       eir.cb.set_max_frame_size = dpdk_set_max_frame_size;
       xd->hw_if_index = vnet_eth_register_interface (vnm, &eir);
       hi = vnet_get_hw_interface (vnm, xd->hw_if_index);
-      hi->numa_node = xd->cpu_socket = (i8) rte_eth_dev_socket_id (port_id);
+      numa_node = (i8) rte_eth_dev_socket_id (port_id);
+      if (numa_node == SOCKET_ID_ANY)
+       /* numa_node is not set, default to 0 */
+       hi->numa_node = xd->cpu_socket = 0;
+      else
+       hi->numa_node = xd->cpu_socket = numa_node;
       sw = vnet_get_hw_sw_interface (vnm, xd->hw_if_index);
       xd->sw_if_index = sw->sw_if_index;
-      dpdk_log_debug ("[%u] interface %s created", port_id, hi->name);
+      dpdk_log_debug ("[%u] interface %v created", port_id, hi->name);
+
+      if (devconf->tag)
+       vnet_set_sw_interface_tag (vnm, devconf->tag, sw->sw_if_index);
 
       ethernet_set_flags (vnm, xd->hw_if_index,
                          ETHERNET_INTERFACE_FLAG_DEFAULT_L3);
@@ -513,6 +601,7 @@ dpdk_lib_init (dpdk_main_t * dm)
       if (vec_len (xd->errors))
        dpdk_log_err ("[%u] setup failed Errors:\n  %U", port_id,
                      format_dpdk_device_errors, xd);
+      dpdk_counters_xstats_init (xd);
     }
 
   for (int i = 0; i < vec_len (dm->devices); i++)
@@ -533,7 +622,6 @@ dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
   int i;
 
   addrs = vlib_pci_get_all_dev_addrs ();
-  /* *INDENT-OFF* */
   vec_foreach (addr, addrs)
     {
     dpdk_device_config_t * devconf = 0;
@@ -552,8 +640,18 @@ dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
       continue;
     }
 
+#ifdef __FreeBSD__
+    /*
+     * The defines for the PCI_CLASS_* types are platform specific and differ
+     * on FreeBSD.
+     */
+    if (d->device_class != PCI_CLASS_NETWORK &&
+       d->device_class != PCI_CLASS_PROCESSOR_CO)
+      continue;
+#else
     if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && d->device_class != PCI_CLASS_PROCESSOR_CO)
       continue;
+#endif /* __FreeBSD__ */
 
     if (num_whitelisted)
       {
@@ -621,9 +719,13 @@ dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_NETWORK_ETHERNET)
       ;
     /* all Intel QAT devices VFs */
-    else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_PROCESSOR_CO &&
-        (d->device_id == 0x0443 || d->device_id == 0x18a1 || d->device_id == 0x19e3 ||
-        d->device_id == 0x37c9 || d->device_id == 0x6f55))
+    else if (d->vendor_id == 0x8086 &&
+            d->device_class == PCI_CLASS_PROCESSOR_CO &&
+            (d->device_id == 0x0443 || d->device_id == 0x18a1 ||
+             d->device_id == 0x19e3 || d->device_id == 0x37c9 ||
+             d->device_id == 0x6f55 || d->device_id == 0x18ef ||
+             d->device_id == 0x4941 || d->device_id == 0x4943 ||
+             d->device_id == 0x4945))
       ;
     /* Cisco VIC */
     else if (d->vendor_id == 0x1137 &&
@@ -651,10 +753,28 @@ dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
       {
         continue;
       }
-    /* Mellanox CX6, CX6VF, CX6DX, CX6DXVF */
-    else if (d->vendor_id == 0x15b3 && d->device_id >= 0x101b && d->device_id <= 0x101e)
+    /* Mellanox CX6, CX6VF, CX6DX, CX6DXVF, CX6LX */
+    else if (d->vendor_id == 0x15b3 &&
+            (d->device_id >= 0x101b && d->device_id <= 0x101f))
       {
-        continue;
+       continue;
+      }
+    /* Mellanox CX7 */
+    else if (d->vendor_id == 0x15b3 && d->device_id == 0x1021)
+      {
+       continue;
+      }
+    /* Mellanox BF, BFVF */
+    else if (d->vendor_id == 0x15b3 &&
+            (d->device_id >= 0xa2d2 && d->device_id <= 0Xa2d3))
+      {
+       continue;
+      }
+    /* Mellanox BF2, BF3 */
+    else if (d->vendor_id == 0x15b3 &&
+            (d->device_id == 0xa2d6 || d->device_id == 0xa2dc))
+      {
+       continue;
       }
     /* Broadcom NetXtreme S, and E series only */
     else if (d->vendor_id == 0x14e4 &&
@@ -669,6 +789,9 @@ dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
         d->device_id == 0x1614 || d->device_id == 0x1606 ||
         d->device_id == 0x1609 || d->device_id == 0x1614)))
       ;
+    /* Google vNIC */
+    else if (d->vendor_id == 0x1ae0 && d->device_id == 0x0042)
+      ;
     else
       {
         dpdk_log_warn ("Unsupported PCI device 0x%04x:0x%04x found "
@@ -677,7 +800,8 @@ dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
         continue;
       }
 
-    error = vlib_pci_bind_to_uio (vm, addr, (char *) conf->uio_driver_name);
+    error = vlib_pci_bind_to_uio (vm, addr, (char *) conf->uio_driver_name,
+                                 conf->uio_bind_force);
 
     if (error)
       {
@@ -693,7 +817,6 @@ dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
        clib_error_report (error);
       }
   }
-  /* *INDENT-ON* */
   vec_free (pci_addr);
   vlib_pci_free_device_info (d);
 }
@@ -708,7 +831,6 @@ dpdk_bind_vmbus_devices_to_uio (dpdk_config_main_t * conf)
 
   addrs = vlib_vmbus_get_all_dev_addrs ();
 
-  /* *INDENT-OFF* */
   vec_foreach (addr, addrs)
     {
       dpdk_device_config_t *devconf = 0;
@@ -773,7 +895,6 @@ dpdk_bind_vmbus_devices_to_uio (dpdk_config_main_t * conf)
          clib_error_report (error);
        }
     }
-  /* *INDENT-ON* */
 }
 
 uword
@@ -872,6 +993,8 @@ dpdk_device_config (dpdk_config_main_t *conf, void *addr,
        ;
       else if (unformat (input, "name %v", &devconf->name))
        ;
+      else if (unformat (input, "tag %s", &devconf->tag))
+       ;
       else if (unformat (input, "workers %U", unformat_bitmap_list,
                         &devconf->workers))
        ;
@@ -966,18 +1089,40 @@ dpdk_log_read_ready (clib_file_t * uf)
   return 0;
 }
 
+static clib_error_t *
+dpdk_set_stat_poll_interval (f64 interval)
+{
+  if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
+    return clib_error_return (0, "wrong stats-poll-interval value");
+
+  dpdk_main.stat_poll_interval = interval;
+  return 0;
+}
+
+static clib_error_t *
+dpdk_set_link_state_poll_interval (f64 interval)
+{
+  if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
+    return clib_error_return (0, "wrong link-state-poll-interval value");
+
+  dpdk_main.link_state_poll_interval = interval;
+  return 0;
+}
+
 static clib_error_t *
 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
 {
   dpdk_main_t *dm = &dpdk_main;
   clib_error_t *error = 0;
   dpdk_config_main_t *conf = &dpdk_config_main;
-  vlib_thread_main_t *tm = vlib_get_thread_main ();
   dpdk_device_config_t *devconf;
   vlib_pci_addr_t pci_addr = { 0 };
   vlib_vmbus_addr_t vmbus_addr = { 0 };
   unformat_input_t sub_input;
+#ifdef __linux
+  vlib_thread_main_t *tm = vlib_get_thread_main ();
   uword default_hugepage_sz, x;
+#endif /* __linux__ */
   u8 *s, *tmp = 0;
   int ret, i;
   int num_whitelisted = 0;
@@ -986,13 +1131,10 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
   u8 no_vmbus = 0;
   u8 file_prefix = 0;
   u8 *socket_mem = 0;
-  u8 *huge_dir_path = 0;
   u32 vendor, device, domain, bus, func;
   void *fmt_func;
   void *fmt_addr;
-
-  huge_dir_path =
-    format (0, "%s/hugepages%c", vlib_unix_get_runtime_dir (), 0);
+  f64 poll_interval;
 
   conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
   mhash_init (&conf->device_config_index_by_vmbus_addr, sizeof (uword),
@@ -1028,6 +1170,18 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
       else if (unformat (input, "max-simd-bitwidth %U",
                         unformat_max_simd_bitwidth, &conf->max_simd_bitwidth))
        ;
+      else if (unformat (input, "link-state-poll-interval %f", &poll_interval))
+       {
+         error = dpdk_set_link_state_poll_interval (poll_interval);
+         if (error != 0)
+           return error;
+       }
+      else if (unformat (input, "stats-poll-interval %f", &poll_interval))
+       {
+         error = dpdk_set_stat_poll_interval (poll_interval);
+         if (error != 0)
+           return error;
+       }
       else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
                         &sub_input))
        {
@@ -1084,6 +1238,8 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
        }
       else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
        ;
+      else if (unformat (input, "uio-bind-force"))
+       conf->uio_bind_force = 1;
       else if (unformat (input, "socket-mem %s", &socket_mem))
        ;
       else if (unformat (input, "no-pci"))
@@ -1174,9 +1330,13 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
     {
       vec_add1 (conf->eal_init_args, (u8 *) "--in-memory");
 
+#ifdef __linux__
+      /*
+       * FreeBSD performs huge page prealloc through a dedicated kernel mode
+       * this process is only required on Linux.
+       */
       default_hugepage_sz = clib_mem_get_default_hugepage_size ();
 
-      /* *INDENT-OFF* */
       clib_bitmap_foreach (x, tm->cpu_socket_bitmap)
        {
          clib_error_t *e;
@@ -1189,7 +1349,7 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
          if ((e = clib_sysfs_prealloc_hugepages(x, 0, n_pages)))
            clib_error_report (e);
         }
-      /* *INDENT-ON* */
+#endif /* __linux__ */
     }
 
   /* on/off dpdk's telemetry thread */
@@ -1198,6 +1358,7 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
       vec_add1 (conf->eal_init_args, (u8 *) "--no-telemetry");
     }
 
+#ifdef __linux__
   if (!file_prefix)
     {
       tmp = format (0, "--file-prefix%c", 0);
@@ -1205,6 +1366,7 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
       tmp = format (0, "vpp%c", 0);
       vec_add1 (conf->eal_init_args, tmp);
     }
+#endif
 
   if (no_pci == 0 && geteuid () == 0)
     dpdk_bind_devices_to_uio (conf);
@@ -1324,11 +1486,6 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
                                      RTE_VECT_SIMD_256 :
                                      RTE_VECT_SIMD_512);
 
-  /* lazy umount hugepages */
-  umount2 ((char *) huge_dir_path, MNT_DETACH);
-  rmdir ((char *) huge_dir_path);
-  vec_free (huge_dir_path);
-
   /* main thread 1st */
   if ((error = dpdk_buffer_pools_create (vm)))
     return error;
@@ -1373,9 +1530,20 @@ dpdk_update_link_state (dpdk_device_t * xd, f64 now)
       ed->new_link_state = (u8) xd->link.link_status;
     }
 
-  if ((xd->link.link_duplex != prev_link.link_duplex))
+  hw_flags_chg = ((xd->link.link_duplex != prev_link.link_duplex) ||
+                 (xd->link.link_status != prev_link.link_status));
+
+  if (xd->link.link_speed != prev_link.link_speed)
+    vnet_hw_interface_set_link_speed (vnm, xd->hw_if_index,
+                                     (xd->link.link_speed == UINT32_MAX) ?
+                                             UINT32_MAX :
+                                             xd->link.link_speed * 1000);
+
+  if (hw_flags_chg)
     {
-      hw_flags_chg = 1;
+      if (xd->link.link_status)
+       hw_flags |= VNET_HW_INTERFACE_FLAG_LINK_UP;
+
       switch (xd->link.link_duplex)
        {
        case RTE_ETH_LINK_HALF_DUPLEX:
@@ -1387,23 +1555,7 @@ dpdk_update_link_state (dpdk_device_t * xd, f64 now)
        default:
          break;
        }
-    }
-  if (xd->link.link_speed != prev_link.link_speed)
-    vnet_hw_interface_set_link_speed (vnm, xd->hw_if_index,
-                                     (xd->link.link_speed == UINT32_MAX) ?
-                                             UINT32_MAX :
-                                             xd->link.link_speed * 1000);
-
-  if (xd->link.link_status != prev_link.link_status)
-    {
-      hw_flags_chg = 1;
-
-      if (xd->link.link_status)
-       hw_flags |= VNET_HW_INTERFACE_FLAG_LINK_UP;
-    }
 
-  if (hw_flags_chg)
-    {
       if (LINK_STATE_ELOGS)
        {
          ELOG_TYPE_DECLARE (e) =
@@ -1433,6 +1585,7 @@ dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
   dpdk_device_t *xd;
   vlib_thread_main_t *tm = vlib_get_thread_main ();
 
+  vlib_worker_thread_barrier_sync (vm);
   error = dpdk_lib_init (dm);
 
   if (error)
@@ -1449,6 +1602,7 @@ dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
        }
     }
 
+  vlib_worker_thread_barrier_release (vm);
   tm->worker_thread_release = 1;
 
   f64 now = vlib_time_now (vm);
@@ -1457,16 +1611,17 @@ dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
     dpdk_update_link_state (xd, now);
   }
 
+  f64 timeout =
+    clib_min (dm->link_state_poll_interval, dm->stat_poll_interval);
+
   while (1)
     {
-      /*
-       * check each time through the loop in case intervals are changed
-       */
-      f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
-       dm->link_state_poll_interval : dm->stat_poll_interval;
-
+      f64 min_wait = clib_max (timeout, DPDK_MIN_POLL_INTERVAL);
       vlib_process_wait_for_event_or_clock (vm, min_wait);
 
+      timeout =
+       clib_min (dm->link_state_poll_interval, dm->stat_poll_interval);
+
       if (dm->admin_up_down_in_progress)
        /* skip the poll if an admin up down is in progress (on any interface) */
        continue;
@@ -1480,19 +1635,25 @@ dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
          dpdk_update_link_state (xd, now);
 
       }
-    }
 
+      now = vlib_time_now (vm);
+      vec_foreach (xd, dm->devices)
+       {
+         timeout = clib_min (timeout, xd->time_last_stats_update +
+                                        dm->stat_poll_interval - now);
+         timeout = clib_min (timeout, xd->time_last_link_update +
+                                        dm->link_state_poll_interval - now);
+       }
+    }
   return 0;
 }
 
-/* *INDENT-OFF* */
 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
     .function = dpdk_process,
     .type = VLIB_NODE_TYPE_PROCESS,
     .name = "dpdk-process",
     .process_log2_n_stack_bytes = 17,
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
 dpdk_init (vlib_main_t * vm)