dpdk: plugin init should be protect by thread barrier
[vpp.git] / src / plugins / dpdk / device / init.c
index 4faf629..74181ea 100644 (file)
@@ -24,6 +24,7 @@
 #include <vnet/vnet.h>
 #include <vnet/ethernet/ethernet.h>
 #include <vnet/interface/rx_queue_funcs.h>
+#include <vnet/interface/tx_queue_funcs.h>
 #include <dpdk/buffer.h>
 #include <dpdk/device/dpdk.h>
 #include <dpdk/cryptodev/cryptodev.h>
@@ -56,25 +57,29 @@ const struct
   const char *pfx;
 } if_name_prefixes[] = {
   /* sorted, higher speed first */
-  { ETH_LINK_SPEED_200G, "TwoHundredGigabitEthernet" },
-  { ETH_LINK_SPEED_100G, "HundredGigabitEthernet" },
-  { ETH_LINK_SPEED_56G, "FiftySixGigabitEthernet" },
-  { ETH_LINK_SPEED_50G, "FiftyGigabitEthernet" },
-  { ETH_LINK_SPEED_40G, "FortyGigabitEthernet" },
-  { ETH_LINK_SPEED_25G, "TwentyFiveGigabitEthernet" },
-  { ETH_LINK_SPEED_20G, "TwentyGigabitEthernet" },
-  { ETH_LINK_SPEED_10G, "TenGigabitEthernet" },
-  { ETH_LINK_SPEED_5G, "FiveGigabitEthernet" },
-  { ETH_LINK_SPEED_2_5G, "TwoDotFiveGigabitEthernet" },
-  { ETH_LINK_SPEED_1G, "GigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_200G, "TwoHundredGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_100G, "HundredGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_56G, "FiftySixGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_50G, "FiftyGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_40G, "FortyGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_25G, "TwentyFiveGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_20G, "TwentyGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_10G, "TenGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_5G, "FiveGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_2_5G, "TwoDotFiveGigabitEthernet" },
+  { RTE_ETH_LINK_SPEED_1G, "GigabitEthernet" },
 };
 
 static clib_error_t *
-dpdk_set_mtu (vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 mtu)
+dpdk_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
+                        u32 frame_size)
 {
   dpdk_main_t *dm = &dpdk_main;
   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
   int rv;
+  u32 mtu;
+
+  mtu = frame_size - xd->driver_frame_overhead;
 
   rv = rte_eth_dev_set_mtu (xd->port_id, mtu);
 
@@ -99,7 +104,8 @@ dpdk_set_mtu (vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 mtu)
        }
     }
   else
-    dpdk_log_debug ("[%u] mtu set to %u", xd->port_id, mtu);
+    dpdk_log_debug ("[%u] max_frame_size set to %u by setting MTU to %u",
+                   xd->port_id, frame_size, mtu);
 
   return 0;
 }
@@ -242,7 +248,8 @@ dpdk_lib_init (dpdk_main_t * dm)
   dm->default_port_conf.n_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
   dm->default_port_conf.n_rx_queues = 1;
   dm->default_port_conf.n_tx_queues = tm->n_vlib_mains;
-  dm->default_port_conf.rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
+  dm->default_port_conf.rss_hf =
+    RTE_ETH_RSS_IP | RTE_ETH_RSS_UDP | RTE_ETH_RSS_TCP;
   dm->default_port_conf.max_lro_pkt_size = DPDK_MAX_LRO_SIZE_DEFAULT;
 
   if ((clib_mem_get_default_hugepage_size () == 2 << 20) &&
@@ -300,6 +307,7 @@ dpdk_lib_init (dpdk_main_t * dm)
          xd->supported_flow_actions = dr->supported_flow_actions;
          xd->conf.disable_rss = dr->mq_mode_none;
          xd->conf.disable_rx_scatter = dr->disable_rx_scatter;
+         xd->conf.enable_rxq_int = dr->enable_rxq_int;
          if (dr->use_intel_phdr_cksum)
            dpdk_device_flag_set (xd, DPDK_DEVICE_FLAG_INTEL_PHDR_CKSUM, 1);
          if (dr->int_unmaskable)
@@ -355,9 +363,16 @@ dpdk_lib_init (dpdk_main_t * dm)
        xd->name = format (xd->name, "/%d", di.switch_info.port_id);
 
       /* number of RX and TX queues */
-      if (devconf->num_tx_queues > 0 &&
-         devconf->num_tx_queues < xd->conf.n_tx_queues)
-       xd->conf.n_tx_queues = devconf->num_tx_queues;
+      if (devconf->num_tx_queues > 0)
+       {
+         if (di.max_tx_queues < devconf->num_tx_queues)
+           dpdk_log_warn ("[%u] Configured number of TX queues (%u) is "
+                          "bigger than maximum supported (%u)",
+                          port_id, devconf->num_tx_queues, di.max_tx_queues);
+         xd->conf.n_tx_queues = devconf->num_tx_queues;
+       }
+
+      xd->conf.n_tx_queues = clib_min (di.max_tx_queues, xd->conf.n_tx_queues);
 
       if (devconf->num_rx_queues > 1 &&
          di.max_rx_queues >= devconf->num_rx_queues)
@@ -374,8 +389,30 @@ dpdk_lib_init (dpdk_main_t * dm)
                               format_dpdk_rss_hf_name, unsupported_bits);
            }
          xd->conf.rss_hf &= di.flow_type_rss_offloads;
+         dpdk_log_debug ("[%u] rss_hf: %U", port_id, format_dpdk_rss_hf_name,
+                         xd->conf.rss_hf);
        }
 
+#ifndef RTE_VLAN_HLEN
+#define RTE_VLAN_HLEN 4
+#endif
+      xd->driver_frame_overhead =
+       RTE_ETHER_HDR_LEN + 2 * RTE_VLAN_HLEN + RTE_ETHER_CRC_LEN;
+#if RTE_VERSION >= RTE_VERSION_NUM(21, 11, 0, 0)
+      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 &&
+         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",
+                     xd->port_id, di.min_mtu, di.max_mtu, di.min_rx_bufsize,
+                     di.max_rx_pktlen, di.max_lro_pkt_size);
+#endif
+      dpdk_log_debug ("[%u] driver frame overhead is %u", port_id,
+                     xd->driver_frame_overhead);
+
       /* number of RX and TX tescriptors */
       if (devconf->num_rx_desc)
        xd->conf.n_rx_desc = devconf->num_rx_desc;
@@ -387,8 +424,15 @@ dpdk_lib_init (dpdk_main_t * dm)
       else if (dr && dr->n_tx_desc)
        xd->conf.n_tx_desc = dr->n_tx_desc;
 
+      dpdk_log_debug (
+       "[%u] n_rx_queues: %u n_tx_queues: %u n_rx_desc: %u n_tx_desc: %u",
+       port_id, xd->conf.n_rx_queues, xd->conf.n_tx_queues,
+       xd->conf.n_rx_desc, xd->conf.n_tx_desc);
+
       vec_validate_aligned (xd->rx_queues, xd->conf.n_rx_queues - 1,
                            CLIB_CACHE_LINE_BYTES);
+      vec_validate_aligned (xd->tx_queues, xd->conf.n_tx_queues - 1,
+                           CLIB_CACHE_LINE_BYTES);
 
       rte_eth_macaddr_get (port_id, (void *) addr);
 
@@ -397,7 +441,7 @@ dpdk_lib_init (dpdk_main_t * dm)
       eir.dev_instance = xd->device_index;
       eir.address = addr;
       eir.cb.flag_change = dpdk_flag_change;
-      eir.cb.set_mtu = dpdk_set_mtu;
+      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);
@@ -405,6 +449,9 @@ dpdk_lib_init (dpdk_main_t * dm)
       xd->sw_if_index = sw->sw_if_index;
       dpdk_log_debug ("[%u] interface %s 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);
 
@@ -429,13 +476,26 @@ dpdk_lib_init (dpdk_main_t * dm)
              vnm, xd->hw_if_index, q, VNET_HW_IF_RXQ_THREAD_ANY);
          }
 
+      for (q = 0; q < xd->conf.n_tx_queues; q++)
+       {
+         dpdk_tx_queue_t *txq = vec_elt_at_index (xd->tx_queues, q);
+         txq->queue_index =
+           vnet_hw_if_register_tx_queue (vnm, xd->hw_if_index, q);
+       }
+
+      for (q = 0; q < tm->n_vlib_mains; q++)
+       {
+         u32 qi = xd->tx_queues[q % xd->conf.n_tx_queues].queue_index;
+         vnet_hw_if_tx_queue_assign_thread (vnm, qi, q);
+       }
+
       if (devconf->tso == DPDK_DEVICE_TSO_ON)
        {
          /*tcp_udp checksum must be enabled*/
          if (xd->conf.enable_tcp_udp_checksum == 0)
            dpdk_log_warn ("[%u] TCP/UDP checksum offload must be enabled",
                           xd->port_id);
-         else if ((di.tx_offload_capa & DEV_TX_OFFLOAD_TCP_TSO) == 0)
+         else if ((di.tx_offload_capa & RTE_ETH_TX_OFFLOAD_TCP_TSO) == 0)
            dpdk_log_warn ("[%u] TSO not supported by device", xd->port_id);
          else
            xd->conf.enable_tso = 1;
@@ -565,9 +625,12 @@ 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))
       ;
     /* Cisco VIC */
     else if (d->vendor_id == 0x1137 &&
@@ -621,7 +684,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)
       {
@@ -816,6 +880,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))
        ;
@@ -883,7 +949,7 @@ dpdk_log_read_ready (clib_file_t * uf)
       n = read (uf->file_descriptor, s + len, n_try);
       if (n < 0 && errno != EAGAIN)
        return clib_error_return_unix (0, "read");
-      _vec_len (s) = len + (n < 0 ? 0 : n);
+      vec_set_len (s, len + (n < 0 ? 0 : n));
     }
 
   unformat_init_vector (&input, s);
@@ -928,10 +994,13 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
   int eal_no_hugetlb = 0;
   u8 no_pci = 0;
   u8 no_vmbus = 0;
+  u8 no_dsa = 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);
@@ -1026,6 +1095,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"))
@@ -1034,6 +1105,8 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
          tmp = format (0, "--no-pci%c", 0);
          vec_add1 (conf->eal_init_args, tmp);
        }
+      else if (unformat (input, "no-dsa"))
+       no_dsa = 1;
       else if (unformat (input, "blacklist %U", unformat_vlib_vmbus_addr,
                         &vmbus_addr))
        {
@@ -1105,12 +1178,8 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
 
       else if (unformat_skip_white_space (input))
        ;
-      else
-       {
-         error = clib_error_return (0, "unknown input `%U'",
+      else return clib_error_return (0, "unknown input `%U'",
                                     format_unformat_error, input);
-         goto done;
-       }
     }
 
   if (!conf->uio_driver_name)
@@ -1152,9 +1221,6 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
       vec_add1 (conf->eal_init_args, tmp);
     }
 
-  if (error)
-    return error;
-
   if (no_pci == 0 && geteuid () == 0)
     dpdk_bind_devices_to_uio (conf);
 
@@ -1179,31 +1245,39 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
       /* copy rss_queues config from default device */
       _ (rss_queues)
 
-      /* add DPDK EAL whitelist/blacklist entry */
-      if (num_whitelisted > 0 && devconf->is_blacklisted == 0 &&
-         devconf->dev_addr_type == VNET_DEV_ADDR_PCI)
-    {
-         tmp = format (0, "-a%c", 0);
-         vec_add1 (conf->eal_init_args, tmp);
-         if (devconf->devargs)
+      /* assume that default is PCI */
+      fmt_func = format_vlib_pci_addr;
+    fmt_addr = &devconf->pci_addr;
+
+    if (devconf->dev_addr_type == VNET_DEV_ADDR_VMBUS)
+      {
+       fmt_func = format_vlib_vmbus_addr;
+       fmt_addr = &devconf->vmbus_addr;
+      }
+
+    /* add DPDK EAL whitelist/blacklist entry */
+    if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
+      {
+       tmp = format (0, "-a%c", 0);
+       vec_add1 (conf->eal_init_args, tmp);
+       if (devconf->devargs)
          {
-           tmp = format (0, "%U,%s%c", format_vlib_pci_addr,
-                         &devconf->pci_addr, devconf->devargs, 0);
+           tmp =
+             format (0, "%U,%s%c", fmt_func, fmt_addr, devconf->devargs, 0);
          }
          else
          {
-           tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
+           tmp = format (0, "%U%c", fmt_func, fmt_addr, 0);
          }
          vec_add1 (conf->eal_init_args, tmp);
-    }
-    else if (num_whitelisted == 0 && devconf->is_blacklisted != 0 &&
-            devconf->dev_addr_type == VNET_DEV_ADDR_PCI)
-    {
-         tmp = format (0, "-b%c", 0);
-         vec_add1 (conf->eal_init_args, tmp);
-         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
-         vec_add1 (conf->eal_init_args, tmp);
-    }
+      }
+    else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
+      {
+       tmp = format (0, "-b%c", 0);
+       vec_add1 (conf->eal_init_args, tmp);
+       tmp = format (0, "%U%c", fmt_func, fmt_addr, 0);
+       vec_add1 (conf->eal_init_args, tmp);
+      }
   }
 
 #undef _
@@ -1213,14 +1287,15 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
 
   /* NULL terminate the "argv" vector, in case of stupidity */
   vec_add1 (conf->eal_init_args, 0);
-  _vec_len (conf->eal_init_args) -= 1;
+  vec_dec_len (conf->eal_init_args, 1);
 
   /* Set up DPDK eal and packet mbuf pool early. */
 
   int log_fds[2] = { 0 };
   if (pipe (log_fds) == 0)
     {
-      if (fcntl (log_fds[1], F_SETFL, O_NONBLOCK) == 0)
+      if (fcntl (log_fds[0], F_SETFL, O_NONBLOCK) == 0 &&
+         fcntl (log_fds[1], F_SETFL, O_NONBLOCK) == 0)
        {
          FILE *f = fdopen (log_fds[1], "a");
          if (f && rte_openlog_stream (f) == 0)
@@ -1241,6 +1316,13 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
 
   vm = vlib_get_main ();
 
+  if (no_dsa)
+    {
+      struct rte_bus *bus;
+      bus = rte_bus_find_by_name ("dsa");
+      if (bus)
+       rte_bus_unregister (bus);
+    }
   /* make copy of args as rte_eal_init tends to mess up with arg array */
   for (i = 1; i < vec_len (conf->eal_init_args); i++)
     conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
@@ -1251,6 +1333,8 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
   dpdk_log_notice ("EAL init args: %s", conf->eal_init_args_str);
   ret = rte_eal_init (vec_len (conf->eal_init_args),
                      (char **) conf->eal_init_args);
+  if (ret < 0)
+    return clib_error_return (0, "rte_eal_init returned %d", ret);
 
   /* enable the AVX-512 vPMDs in DPDK */
   if (clib_cpu_supports_avx512_bitalg () &&
@@ -1267,15 +1351,11 @@ dpdk_config (vlib_main_t * vm, unformat_input_t * input)
   rmdir ((char *) huge_dir_path);
   vec_free (huge_dir_path);
 
-  if (ret < 0)
-    return clib_error_return (0, "rte_eal_init returned %d", ret);
-
   /* main thread 1st */
   if ((error = dpdk_buffer_pools_create (vm)))
     return error;
 
-done:
-  return error;
+  return 0;
 }
 
 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
@@ -1315,35 +1395,32 @@ 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 ETH_LINK_HALF_DUPLEX:
+       case RTE_ETH_LINK_HALF_DUPLEX:
          hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
          break;
-       case ETH_LINK_FULL_DUPLEX:
+       case RTE_ETH_LINK_FULL_DUPLEX:
          hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
          break;
        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 * 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) =
@@ -1373,6 +1450,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)
@@ -1389,6 +1467,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);