vcl: allow more rx events on peek
[vpp.git] / src / plugins / dpdk / device / init.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/vnet.h>
16 #include <vppinfra/vec.h>
17 #include <vppinfra/error.h>
18 #include <vppinfra/format.h>
19 #include <vppinfra/bitmap.h>
20 #include <vppinfra/linux/sysfs.h>
21 #include <vlib/unix/unix.h>
22 #include <vlib/log.h>
23
24 #include <vnet/vnet.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/interface/rx_queue_funcs.h>
27 #include <vnet/interface/tx_queue_funcs.h>
28 #include <dpdk/buffer.h>
29 #include <dpdk/device/dpdk.h>
30 #include <dpdk/cryptodev/cryptodev.h>
31 #include <vlib/pci/pci.h>
32 #include <vlib/vmbus/vmbus.h>
33
34 #include <rte_ring.h>
35 #include <rte_vect.h>
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <sys/stat.h>
41 #include <sys/mount.h>
42 #include <string.h>
43 #include <fcntl.h>
44 #include <dirent.h>
45
46 #include <dpdk/device/dpdk_priv.h>
47
48 dpdk_main_t dpdk_main;
49 dpdk_config_main_t dpdk_config_main;
50
51 #define LINK_STATE_ELOGS        0
52
53 /* dev_info.speed_capa -> interface name mapppings */
54 const struct
55 {
56   u32 link_speed;
57   const char *pfx;
58 } if_name_prefixes[] = {
59   /* sorted, higher speed first */
60   { RTE_ETH_LINK_SPEED_200G, "TwoHundredGigabitEthernet" },
61   { RTE_ETH_LINK_SPEED_100G, "HundredGigabitEthernet" },
62   { RTE_ETH_LINK_SPEED_56G, "FiftySixGigabitEthernet" },
63   { RTE_ETH_LINK_SPEED_50G, "FiftyGigabitEthernet" },
64   { RTE_ETH_LINK_SPEED_40G, "FortyGigabitEthernet" },
65   { RTE_ETH_LINK_SPEED_25G, "TwentyFiveGigabitEthernet" },
66   { RTE_ETH_LINK_SPEED_20G, "TwentyGigabitEthernet" },
67   { RTE_ETH_LINK_SPEED_10G, "TenGigabitEthernet" },
68   { RTE_ETH_LINK_SPEED_5G, "FiveGigabitEthernet" },
69   { RTE_ETH_LINK_SPEED_2_5G, "TwoDotFiveGigabitEthernet" },
70   { RTE_ETH_LINK_SPEED_1G, "GigabitEthernet" },
71 };
72
73 static clib_error_t *
74 dpdk_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
75                          u32 frame_size)
76 {
77   dpdk_main_t *dm = &dpdk_main;
78   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
79   int rv;
80   u32 mtu;
81
82   mtu = frame_size - xd->driver_frame_overhead;
83
84   rv = rte_eth_dev_set_mtu (xd->port_id, mtu);
85
86   if (rv < 0)
87     {
88       dpdk_log_err ("[%u] rte_eth_dev_set_mtu failed (mtu %u, rv %d)",
89                     xd->port_id, mtu, rv);
90       switch (rv)
91         {
92         case -ENOTSUP:
93           return vnet_error (VNET_ERR_UNSUPPORTED,
94                              "dpdk driver doesn't support MTU change");
95         case -EBUSY:
96           return vnet_error (VNET_ERR_BUSY, "port is running");
97         case -EINVAL:
98           return vnet_error (VNET_ERR_INVALID_VALUE, "invalid MTU");
99         default:
100           return vnet_error (VNET_ERR_BUG,
101                              "unexpected return value %d returned from "
102                              "rte_eth_dev_set_mtu(...)",
103                              rv);
104         }
105     }
106   else
107     dpdk_log_debug ("[%u] max_frame_size set to %u by setting MTU to %u",
108                     xd->port_id, frame_size, mtu);
109
110   return 0;
111 }
112
113 static u32
114 dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
115 {
116   dpdk_main_t *dm = &dpdk_main;
117   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
118   u32 old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
119
120   switch (flags)
121     {
122     case ETHERNET_INTERFACE_FLAG_DEFAULT_L3:
123       /* set to L3/non-promisc mode */
124       dpdk_device_flag_set (xd, DPDK_DEVICE_FLAG_PROMISC, 0);
125       break;
126     case ETHERNET_INTERFACE_FLAG_ACCEPT_ALL:
127       dpdk_device_flag_set (xd, DPDK_DEVICE_FLAG_PROMISC, 1);
128       break;
129     default:
130       return ~0;
131     }
132
133   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
134     {
135       if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
136         rte_eth_promiscuous_enable (xd->port_id);
137       else
138         rte_eth_promiscuous_disable (xd->port_id);
139     }
140
141   return old;
142 }
143
144 /* The function check_l3cache helps check if Level 3 cache exists or not on current CPUs
145   return value 1: exist.
146   return value 0: not exist.
147 */
148 static int
149 check_l3cache ()
150 {
151
152   struct dirent *dp;
153   clib_error_t *err;
154   const char *sys_cache_dir = "/sys/devices/system/cpu/cpu0/cache";
155   DIR *dir_cache = opendir (sys_cache_dir);
156
157   if (dir_cache == NULL)
158     return -1;
159
160   while ((dp = readdir (dir_cache)) != NULL)
161     {
162       if (dp->d_type == DT_DIR)
163         {
164           u8 *p = NULL;
165           int level_cache = -1;
166
167           p = format (p, "%s/%s/%s%c", sys_cache_dir, dp->d_name, "level", 0);
168           if ((err = clib_sysfs_read ((char *) p, "%d", &level_cache)))
169             clib_error_free (err);
170
171           if (level_cache == 3)
172             {
173               closedir (dir_cache);
174               return 1;
175             }
176         }
177     }
178
179   if (dir_cache != NULL)
180     closedir (dir_cache);
181
182   return 0;
183 }
184
185 static dpdk_device_config_t *
186 dpdk_find_startup_config (struct rte_eth_dev_info *di)
187 {
188   dpdk_main_t *dm = &dpdk_main;
189   struct rte_pci_device *pci_dev;
190   vlib_pci_addr_t pci_addr;
191 #ifdef __linux__
192   struct rte_vmbus_device *vmbus_dev;
193   vlib_vmbus_addr_t vmbus_addr;
194 #endif /* __linux__ */
195   uword *p = 0;
196
197   if ((pci_dev = dpdk_get_pci_device (di)))
198     {
199       pci_addr.domain = pci_dev->addr.domain;
200       pci_addr.bus = pci_dev->addr.bus;
201       pci_addr.slot = pci_dev->addr.devid;
202       pci_addr.function = pci_dev->addr.function;
203       p =
204         hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
205     }
206
207 #ifdef __linux__
208   if ((vmbus_dev = dpdk_get_vmbus_device (di)))
209     {
210       unformat_input_t input_vmbus;
211 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
212       const char *dev_name = rte_dev_name (di->device);
213 #else
214       const char *dev_name = di->device->name;
215 #endif
216       unformat_init_string (&input_vmbus, dev_name, strlen (dev_name));
217       if (unformat (&input_vmbus, "%U", unformat_vlib_vmbus_addr, &vmbus_addr))
218         p = mhash_get (&dm->conf->device_config_index_by_vmbus_addr,
219                        &vmbus_addr);
220       unformat_free (&input_vmbus);
221     }
222 #endif /* __linux__ */
223
224   if (p)
225     return pool_elt_at_index (dm->conf->dev_confs, p[0]);
226   return &dm->conf->default_devconf;
227 }
228
229 static clib_error_t *
230 dpdk_lib_init (dpdk_main_t * dm)
231 {
232   vnet_main_t *vnm = vnet_get_main ();
233   u16 port_id;
234   vlib_thread_main_t *tm = vlib_get_thread_main ();
235   vnet_device_main_t *vdm = &vnet_device_main;
236   vnet_sw_interface_t *sw;
237   vnet_hw_interface_t *hi;
238   dpdk_device_t *xd;
239   char *if_num_fmt;
240
241   /* vlib_buffer_t template */
242   vec_validate_aligned (dm->per_thread_data, tm->n_vlib_mains - 1,
243                         CLIB_CACHE_LINE_BYTES);
244   for (int i = 0; i < tm->n_vlib_mains; i++)
245     {
246       dpdk_per_thread_data_t *ptd = vec_elt_at_index (dm->per_thread_data, i);
247       clib_memset (&ptd->buffer_template, 0, sizeof (vlib_buffer_t));
248       vnet_buffer (&ptd->buffer_template)->sw_if_index[VLIB_TX] = (u32) ~ 0;
249     }
250
251   if_num_fmt =
252     dm->conf->interface_name_format_decimal ? "%d/%d/%d" : "%x/%x/%x";
253
254   /* device config defaults */
255   dm->default_port_conf.n_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
256   dm->default_port_conf.n_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
257   dm->default_port_conf.n_rx_queues = 1;
258   dm->default_port_conf.n_tx_queues = tm->n_vlib_mains;
259   dm->default_port_conf.rss_hf =
260     RTE_ETH_RSS_IP | RTE_ETH_RSS_UDP | RTE_ETH_RSS_TCP;
261   dm->default_port_conf.max_lro_pkt_size = DPDK_MAX_LRO_SIZE_DEFAULT;
262
263   if ((clib_mem_get_default_hugepage_size () == 2 << 20) &&
264       check_l3cache () == 0)
265     dm->default_port_conf.n_rx_desc = dm->default_port_conf.n_tx_desc = 512;
266
267   RTE_ETH_FOREACH_DEV (port_id)
268     {
269       u8 addr[6];
270       int rv, q;
271       struct rte_eth_dev_info di;
272       dpdk_device_config_t *devconf = 0;
273       vnet_eth_interface_registration_t eir = {};
274       dpdk_driver_t *dr;
275       i8 numa_node;
276
277       if (!rte_eth_dev_is_valid_port (port_id))
278         continue;
279
280       if ((rv = rte_eth_dev_info_get (port_id, &di)) != 0)
281         {
282           dpdk_log_warn ("[%u] failed to get device info. skipping device.",
283                          port_id);
284           continue;
285         }
286
287       if (di.device == 0)
288         {
289           dpdk_log_warn ("[%u] missing device info. Skipping '%s' device",
290                          port_id, di.driver_name);
291           continue;
292         }
293
294       devconf = dpdk_find_startup_config (&di);
295
296       /* If device is blacklisted, we should skip it */
297       if (devconf->is_blacklisted)
298         {
299           dpdk_log_notice ("[%d] Device %s blacklisted. Skipping...", port_id,
300                            di.driver_name);
301           continue;
302         }
303
304       vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
305       xd->port_id = port_id;
306       xd->device_index = xd - dm->devices;
307       xd->per_interface_next_index = ~0;
308
309       clib_memcpy (&xd->conf, &dm->default_port_conf,
310                    sizeof (dpdk_port_conf_t));
311
312       /* find driver datea for this PMD */
313       if ((dr = dpdk_driver_find (di.driver_name, &xd->if_desc)))
314         {
315           xd->driver = dr;
316           xd->supported_flow_actions = dr->supported_flow_actions;
317           xd->conf.disable_rss = dr->mq_mode_none;
318           xd->conf.disable_rx_scatter = dr->disable_rx_scatter;
319           xd->conf.enable_rxq_int = dr->enable_rxq_int;
320           if (dr->use_intel_phdr_cksum)
321             dpdk_device_flag_set (xd, DPDK_DEVICE_FLAG_INTEL_PHDR_CKSUM, 1);
322           if (dr->int_unmaskable)
323             dpdk_device_flag_set (xd, DPDK_DEVICE_FLAG_INT_UNMASKABLE, 1);
324         }
325       else
326         dpdk_log_warn ("[%u] unknown driver '%s'", port_id, di.driver_name);
327
328       if (devconf->name)
329         {
330           xd->name = devconf->name;
331         }
332       else
333         {
334           struct rte_pci_device *pci_dev;
335           if (dr && dr->interface_name_prefix)
336             {
337               /* prefix override by driver */
338               xd->name = format (xd->name, "%s", dr->interface_name_prefix);
339             }
340           else
341             {
342               /* interface name prefix from speed_capa */
343               u64 mask = ~((if_name_prefixes[0].link_speed << 1) - 1);
344
345               if (di.speed_capa & mask)
346                 dpdk_log_warn ("[%u] unknown speed capability 0x%x reported",
347                                xd->port_id, di.speed_capa & mask);
348
349               for (int i = 0; i < ARRAY_LEN (if_name_prefixes); i++)
350                 if (if_name_prefixes[i].link_speed & di.speed_capa)
351                   {
352                     xd->name =
353                       format (xd->name, "%s", if_name_prefixes[i].pfx);
354                     break;
355                   }
356               if (xd->name == 0)
357                 xd->name = format (xd->name, "Ethernet");
358             }
359
360           if (dr && dr->interface_number_from_port_id)
361             xd->name = format (xd->name, "%u", port_id);
362           else if ((pci_dev = dpdk_get_pci_device (&di)))
363             xd->name = format (xd->name, if_num_fmt, pci_dev->addr.bus,
364                                pci_dev->addr.devid, pci_dev->addr.function);
365           else
366             xd->name = format (xd->name, "%u", port_id);
367
368           /* Handle representor devices that share the same PCI ID */
369           if ((di.switch_info.domain_id !=
370                RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) &&
371               (di.switch_info.port_id != (uint16_t) -1))
372             xd->name = format (xd->name, "/%d", di.switch_info.port_id);
373         }
374
375       /* number of RX and TX queues */
376       if (devconf->num_tx_queues > 0)
377         {
378           if (di.max_tx_queues < devconf->num_tx_queues)
379             dpdk_log_warn ("[%u] Configured number of TX queues (%u) is "
380                            "bigger than maximum supported (%u)",
381                            port_id, devconf->num_tx_queues, di.max_tx_queues);
382           xd->conf.n_tx_queues = devconf->num_tx_queues;
383         }
384
385       xd->conf.n_tx_queues = clib_min (di.max_tx_queues, xd->conf.n_tx_queues);
386
387       if (devconf->num_rx_queues > 1 &&
388           di.max_rx_queues >= devconf->num_rx_queues)
389         {
390           xd->conf.n_rx_queues = devconf->num_rx_queues;
391           if (devconf->rss_fn)
392             {
393               u64 unsupported_bits;
394               xd->conf.rss_hf = devconf->rss_fn;
395               unsupported_bits = xd->conf.rss_hf;
396               unsupported_bits &= ~di.flow_type_rss_offloads;
397               if (unsupported_bits)
398                 dpdk_log_warn ("Unsupported RSS hash functions: %U",
399                                format_dpdk_rss_hf_name, unsupported_bits);
400             }
401           xd->conf.rss_hf &= di.flow_type_rss_offloads;
402           dpdk_log_debug ("[%u] rss_hf: %U", port_id, format_dpdk_rss_hf_name,
403                           xd->conf.rss_hf);
404         }
405
406 #ifndef RTE_VLAN_HLEN
407 #define RTE_VLAN_HLEN 4
408 #endif
409       xd->driver_frame_overhead =
410         RTE_ETHER_HDR_LEN + 2 * RTE_VLAN_HLEN + RTE_ETHER_CRC_LEN;
411 #if RTE_VERSION >= RTE_VERSION_NUM(21, 11, 0, 0)
412       q = di.max_rx_pktlen - di.max_mtu;
413
414       /* attempt to protect from bogus value provided by pmd */
415       if (q < (2 * xd->driver_frame_overhead) && q > 0 &&
416           di.max_mtu != UINT16_MAX)
417         xd->driver_frame_overhead = q;
418       dpdk_log_debug ("[%u] min_mtu: %u, max_mtu: %u, min_rx_bufsize: %u, "
419                       "max_rx_pktlen: %u, max_lro_pkt_size: %u",
420                       xd->port_id, di.min_mtu, di.max_mtu, di.min_rx_bufsize,
421                       di.max_rx_pktlen, di.max_lro_pkt_size);
422 #endif
423       dpdk_log_debug ("[%u] driver frame overhead is %u", port_id,
424                       xd->driver_frame_overhead);
425
426       /* number of RX and TX tescriptors */
427       if (devconf->num_rx_desc)
428         xd->conf.n_rx_desc = devconf->num_rx_desc;
429       else if (dr && dr->n_rx_desc)
430         xd->conf.n_rx_desc = dr->n_rx_desc;
431
432       if (devconf->num_tx_desc)
433         xd->conf.n_tx_desc = devconf->num_tx_desc;
434       else if (dr && dr->n_tx_desc)
435         xd->conf.n_tx_desc = dr->n_tx_desc;
436
437       dpdk_log_debug (
438         "[%u] n_rx_queues: %u n_tx_queues: %u n_rx_desc: %u n_tx_desc: %u",
439         port_id, xd->conf.n_rx_queues, xd->conf.n_tx_queues,
440         xd->conf.n_rx_desc, xd->conf.n_tx_desc);
441
442       vec_validate_aligned (xd->rx_queues, xd->conf.n_rx_queues - 1,
443                             CLIB_CACHE_LINE_BYTES);
444       vec_validate_aligned (xd->tx_queues, xd->conf.n_tx_queues - 1,
445                             CLIB_CACHE_LINE_BYTES);
446
447       rte_eth_macaddr_get (port_id, (void *) addr);
448
449       /* create interface */
450       eir.dev_class_index = dpdk_device_class.index;
451       eir.dev_instance = xd->device_index;
452       eir.address = addr;
453       eir.cb.flag_change = dpdk_flag_change;
454       eir.cb.set_max_frame_size = dpdk_set_max_frame_size;
455       xd->hw_if_index = vnet_eth_register_interface (vnm, &eir);
456       hi = vnet_get_hw_interface (vnm, xd->hw_if_index);
457       numa_node = (i8) rte_eth_dev_socket_id (port_id);
458       if (numa_node == SOCKET_ID_ANY)
459         /* numa_node is not set, default to 0 */
460         hi->numa_node = xd->cpu_socket = 0;
461       else
462         hi->numa_node = xd->cpu_socket = numa_node;
463       sw = vnet_get_hw_sw_interface (vnm, xd->hw_if_index);
464       xd->sw_if_index = sw->sw_if_index;
465       dpdk_log_debug ("[%u] interface %v created", port_id, hi->name);
466
467       if (devconf->tag)
468         vnet_set_sw_interface_tag (vnm, devconf->tag, sw->sw_if_index);
469
470       ethernet_set_flags (vnm, xd->hw_if_index,
471                           ETHERNET_INTERFACE_FLAG_DEFAULT_L3);
472
473       /* assign worker threads */
474       vnet_hw_if_set_input_node (vnm, xd->hw_if_index, dpdk_input_node.index);
475       if (devconf->workers)
476         {
477           int j;
478           q = 0;
479           clib_bitmap_foreach (j, devconf->workers)
480             {
481               dpdk_rx_queue_t *rxq = vec_elt_at_index (xd->rx_queues, q);
482               rxq->queue_index = vnet_hw_if_register_rx_queue (
483                 vnm, xd->hw_if_index, q++, vdm->first_worker_thread_index + j);
484             }
485         }
486       else
487         for (q = 0; q < xd->conf.n_rx_queues; q++)
488           {
489             dpdk_rx_queue_t *rxq = vec_elt_at_index (xd->rx_queues, q);
490             rxq->queue_index = vnet_hw_if_register_rx_queue (
491               vnm, xd->hw_if_index, q, VNET_HW_IF_RXQ_THREAD_ANY);
492           }
493
494       for (q = 0; q < xd->conf.n_tx_queues; q++)
495         {
496           dpdk_tx_queue_t *txq = vec_elt_at_index (xd->tx_queues, q);
497           txq->queue_index =
498             vnet_hw_if_register_tx_queue (vnm, xd->hw_if_index, q);
499         }
500
501       for (q = 0; q < tm->n_vlib_mains; q++)
502         {
503           u32 qi = xd->tx_queues[q % xd->conf.n_tx_queues].queue_index;
504           vnet_hw_if_tx_queue_assign_thread (vnm, qi, q);
505         }
506
507       if (devconf->tso == DPDK_DEVICE_TSO_ON)
508         {
509           /*tcp_udp checksum must be enabled*/
510           if (xd->conf.enable_tcp_udp_checksum == 0)
511             dpdk_log_warn ("[%u] TCP/UDP checksum offload must be enabled",
512                            xd->port_id);
513           else if ((di.tx_offload_capa & RTE_ETH_TX_OFFLOAD_TCP_TSO) == 0)
514             dpdk_log_warn ("[%u] TSO not supported by device", xd->port_id);
515           else
516             xd->conf.enable_tso = 1;
517         }
518
519       if (devconf->max_lro_pkt_size)
520         xd->conf.max_lro_pkt_size = devconf->max_lro_pkt_size;
521
522       dpdk_device_setup (xd);
523
524       /* rss queues should be configured after dpdk_device_setup() */
525       if (devconf->rss_queues)
526         {
527           if (vnet_hw_interface_set_rss_queues (vnet_get_main (), hi,
528                                                 devconf->rss_queues))
529             dpdk_log_warn ("[%u] Failed to set rss queues", port_id);
530         }
531
532       if (vec_len (xd->errors))
533         dpdk_log_err ("[%u] setup failed Errors:\n  %U", port_id,
534                       format_dpdk_device_errors, xd);
535     }
536
537   for (int i = 0; i < vec_len (dm->devices); i++)
538     vnet_hw_if_update_runtime_data (vnm, dm->devices[i].hw_if_index);
539
540   return 0;
541 }
542
543 static void
544 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
545 {
546   vlib_main_t *vm = vlib_get_main ();
547   clib_error_t *error;
548   u8 *pci_addr = 0;
549   int num_whitelisted = vec_len (conf->dev_confs);
550   vlib_pci_device_info_t *d = 0;
551   vlib_pci_addr_t *addr = 0, *addrs;
552   int i;
553
554   addrs = vlib_pci_get_all_dev_addrs ();
555   vec_foreach (addr, addrs)
556     {
557     dpdk_device_config_t * devconf = 0;
558     vec_reset_length (pci_addr);
559     pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, addr, 0);
560     if (d)
561     {
562       vlib_pci_free_device_info (d);
563       d = 0;
564       }
565     d = vlib_pci_get_device_info (vm, addr, &error);
566     if (error)
567     {
568       vlib_log_warn (dpdk_main.log_default, "%U", format_clib_error, error);
569       clib_error_free (error);
570       continue;
571     }
572
573 #ifdef __FreeBSD__
574     /*
575      * The defines for the PCI_CLASS_* types are platform specific and differ
576      * on FreeBSD.
577      */
578     if (d->device_class != PCI_CLASS_NETWORK &&
579         d->device_class != PCI_CLASS_PROCESSOR_CO)
580       continue;
581 #else
582     if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && d->device_class != PCI_CLASS_PROCESSOR_CO)
583       continue;
584 #endif /* __FreeBSD__ */
585
586     if (num_whitelisted)
587       {
588         uword * p = hash_get (conf->device_config_index_by_pci_addr, addr->as_u32);
589
590         if (!p)
591           {
592           skipped_pci:
593             continue;
594           }
595
596         devconf = pool_elt_at_index (conf->dev_confs, p[0]);
597       }
598
599     /* Enforce Device blacklist by vendor and device */
600     for (i = 0; i < vec_len (conf->blacklist_by_pci_vendor_and_device); i++)
601       {
602         u16 vendor, device;
603         vendor = (u16)(conf->blacklist_by_pci_vendor_and_device[i] >> 16);
604         device = (u16)(conf->blacklist_by_pci_vendor_and_device[i] & 0xFFFF);
605         if (d->vendor_id == vendor && d->device_id == device)
606           {
607             /*
608              * Expected case: device isn't whitelisted,
609              * so blacklist it...
610              */
611             if (devconf == 0)
612               {
613                 /* Device is blacklisted */
614                 pool_get (conf->dev_confs, devconf);
615                 hash_set (conf->device_config_index_by_pci_addr, addr->as_u32,
616                           devconf - conf->dev_confs);
617                 devconf->pci_addr.as_u32 = addr->as_u32;
618                 devconf->dev_addr_type = VNET_DEV_ADDR_PCI;
619                 devconf->is_blacklisted = 1;
620                 goto skipped_pci;
621               }
622             else /* explicitly whitelisted, ignore the device blacklist  */
623               break;
624           }
625       }
626
627     /* virtio */
628     if (d->vendor_id == 0x1af4 &&
629             (d->device_id == VIRTIO_PCI_LEGACY_DEVICEID_NET ||
630              d->device_id == VIRTIO_PCI_MODERN_DEVICEID_NET))
631       ;
632     /* vmxnet3 */
633     else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
634       {
635         /*
636          * For vmxnet3 PCI, unless it is explicitly specified in the whitelist,
637          * the default is to put it in the blacklist.
638          */
639         if (devconf == 0)
640           {
641             pool_get (conf->dev_confs, devconf);
642             hash_set (conf->device_config_index_by_pci_addr, addr->as_u32,
643                       devconf - conf->dev_confs);
644             devconf->pci_addr.as_u32 = addr->as_u32;
645             devconf->is_blacklisted = 1;
646           }
647       }
648     /* all Intel network devices */
649     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_NETWORK_ETHERNET)
650       ;
651     /* all Intel QAT devices VFs */
652     else if (d->vendor_id == 0x8086 &&
653              d->device_class == PCI_CLASS_PROCESSOR_CO &&
654              (d->device_id == 0x0443 || d->device_id == 0x18a1 ||
655               d->device_id == 0x19e3 || d->device_id == 0x37c9 ||
656               d->device_id == 0x6f55 || d->device_id == 0x18ef ||
657               d->device_id == 0x4941 || d->device_id == 0x4943 ||
658               d->device_id == 0x4945))
659       ;
660     /* Cisco VIC */
661     else if (d->vendor_id == 0x1137 &&
662         (d->device_id == 0x0043 || d->device_id == 0x0071))
663       ;
664     /* Chelsio T4/T5 */
665     else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
666       ;
667     /* Amazon Elastic Network Adapter */
668     else if (d->vendor_id == 0x1d0f && d->device_id >= 0xec20 && d->device_id <= 0xec21)
669       ;
670     /* Cavium Network Adapter */
671     else if (d->vendor_id == 0x177d && d->device_id == 0x9712)
672       ;
673     /* Cavium FastlinQ QL41000 Series */
674     else if (d->vendor_id == 0x1077 && d->device_id >= 0x8070 && d->device_id <= 0x8090)
675       ;
676     /* Mellanox CX3, CX3VF */
677     else if (d->vendor_id == 0x15b3 && d->device_id >= 0x1003 && d->device_id <= 0x1004)
678       {
679         continue;
680       }
681     /* Mellanox CX4, CX4VF, CX4LX, CX4LXVF, CX5, CX5VF, CX5EX, CX5EXVF */
682     else if (d->vendor_id == 0x15b3 && d->device_id >= 0x1013 && d->device_id <= 0x101a)
683       {
684         continue;
685       }
686     /* Mellanox CX6, CX6VF, CX6DX, CX6DXVF, CX6LX */
687     else if (d->vendor_id == 0x15b3 &&
688              (d->device_id >= 0x101b && d->device_id <= 0x101f))
689       {
690         continue;
691       }
692     /* Mellanox CX7 */
693     else if (d->vendor_id == 0x15b3 && d->device_id == 0x1021)
694       {
695         continue;
696       }
697     /* Mellanox BF, BFVF */
698     else if (d->vendor_id == 0x15b3 &&
699              (d->device_id >= 0xa2d2 && d->device_id <= 0Xa2d3))
700       {
701         continue;
702       }
703     /* Mellanox BF2, BF3 */
704     else if (d->vendor_id == 0x15b3 &&
705              (d->device_id == 0xa2d6 || d->device_id == 0xa2dc))
706       {
707         continue;
708       }
709     /* Broadcom NetXtreme S, and E series only */
710     else if (d->vendor_id == 0x14e4 &&
711         ((d->device_id >= 0x16c0 &&
712                 d->device_id != 0x16c6 && d->device_id != 0x16c7 &&
713                 d->device_id != 0x16dd && d->device_id != 0x16f7 &&
714                 d->device_id != 0x16fd && d->device_id != 0x16fe &&
715                 d->device_id != 0x170d && d->device_id != 0x170c &&
716                 d->device_id != 0x170e && d->device_id != 0x1712 &&
717                 d->device_id != 0x1713) ||
718         (d->device_id == 0x1604 || d->device_id == 0x1605 ||
719          d->device_id == 0x1614 || d->device_id == 0x1606 ||
720          d->device_id == 0x1609 || d->device_id == 0x1614)))
721       ;
722     /* Google vNIC */
723     else if (d->vendor_id == 0x1ae0 && d->device_id == 0x0042)
724       ;
725     else
726       {
727         dpdk_log_warn ("Unsupported PCI device 0x%04x:0x%04x found "
728                       "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
729                       pci_addr);
730         continue;
731       }
732
733     error = vlib_pci_bind_to_uio (vm, addr, (char *) conf->uio_driver_name,
734                                   conf->uio_bind_force);
735
736     if (error)
737       {
738         if (devconf == 0)
739           {
740             pool_get (conf->dev_confs, devconf);
741             hash_set (conf->device_config_index_by_pci_addr, addr->as_u32,
742                       devconf - conf->dev_confs);
743             devconf->pci_addr.as_u32 = addr->as_u32;
744           }
745         devconf->dev_addr_type = VNET_DEV_ADDR_PCI;
746         devconf->is_blacklisted = 1;
747         clib_error_report (error);
748       }
749   }
750   vec_free (pci_addr);
751   vlib_pci_free_device_info (d);
752 }
753
754 static void
755 dpdk_bind_vmbus_devices_to_uio (dpdk_config_main_t * conf)
756 {
757   clib_error_t *error;
758   vlib_vmbus_addr_t *addrs, *addr = 0;
759   int num_whitelisted = vec_len (conf->dev_confs);
760   int i;
761
762   addrs = vlib_vmbus_get_all_dev_addrs ();
763
764   vec_foreach (addr, addrs)
765     {
766       dpdk_device_config_t *devconf = 0;
767       if (num_whitelisted)
768         {
769           uword *p =
770             mhash_get (&conf->device_config_index_by_vmbus_addr, addr);
771           if (!p)
772             {
773               /* No devices blacklisted, but have whitelisted. blacklist all
774                * non-whitelisted */
775               pool_get (conf->dev_confs, devconf);
776               mhash_set (&conf->device_config_index_by_vmbus_addr, addr,
777                          devconf - conf->dev_confs, 0);
778               devconf->vmbus_addr = *addr;
779               devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
780               devconf->is_blacklisted = 1;
781             skipped_vmbus:
782               continue;
783             }
784
785           devconf = pool_elt_at_index (conf->dev_confs, p[0]);
786         }
787
788       /* Enforce Device blacklist by vmbus_addr */
789       for (i = 0; i < vec_len (conf->blacklist_by_vmbus_addr); i++)
790         {
791           vlib_vmbus_addr_t *a1 = &conf->blacklist_by_vmbus_addr[i];
792           vlib_vmbus_addr_t *a2 = addr;
793           if (memcmp (a1, a2, sizeof (vlib_vmbus_addr_t)) == 0)
794             {
795               if (devconf == 0)
796                 {
797                   /* Device not whitelisted */
798                   pool_get (conf->dev_confs, devconf);
799                   mhash_set (&conf->device_config_index_by_vmbus_addr, addr,
800                              devconf - conf->dev_confs, 0);
801                   devconf->vmbus_addr = *addr;
802                   devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
803                   devconf->is_blacklisted = 1;
804                   goto skipped_vmbus;
805                 }
806               else
807                 {
808                   break;
809                 }
810             }
811         }
812
813       error = vlib_vmbus_bind_to_uio (addr);
814       if (error)
815         {
816           if (devconf == 0)
817             {
818               pool_get (conf->dev_confs, devconf);
819               mhash_set (&conf->device_config_index_by_vmbus_addr, addr,
820                          devconf - conf->dev_confs, 0);
821               devconf->vmbus_addr = *addr;
822             }
823           devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
824           devconf->is_blacklisted = 1;
825           clib_error_report (error);
826         }
827     }
828 }
829
830 uword
831 unformat_max_simd_bitwidth (unformat_input_t *input, va_list *va)
832 {
833   uword *max_simd_bitwidth = va_arg (*va, uword *);
834
835   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
836     {
837       if (!unformat (input, "%u", max_simd_bitwidth))
838         goto error;
839
840       if (*max_simd_bitwidth != DPDK_MAX_SIMD_BITWIDTH_256 &&
841           *max_simd_bitwidth != DPDK_MAX_SIMD_BITWIDTH_512)
842         goto error;
843     }
844   return 1;
845 error:
846   return 0;
847 }
848
849 static clib_error_t *
850 dpdk_device_config (dpdk_config_main_t *conf, void *addr,
851                     dpdk_device_addr_type_t addr_type, unformat_input_t *input,
852                     u8 is_default)
853 {
854   clib_error_t *error = 0;
855   uword *p;
856   dpdk_device_config_t *devconf = 0;
857   unformat_input_t sub_input;
858
859   if (is_default)
860     {
861       devconf = &conf->default_devconf;
862     }
863   else if (addr_type == VNET_DEV_ADDR_PCI)
864     {
865       p = hash_get (conf->device_config_index_by_pci_addr,
866                     ((vlib_pci_addr_t *) (addr))->as_u32);
867
868       if (!p)
869         {
870           pool_get (conf->dev_confs, devconf);
871           hash_set (conf->device_config_index_by_pci_addr,
872                     ((vlib_pci_addr_t *) (addr))->as_u32,
873                     devconf - conf->dev_confs);
874         }
875       else
876         return clib_error_return (0,
877                                   "duplicate configuration for PCI address %U",
878                                   format_vlib_pci_addr, addr);
879     }
880   else if (addr_type == VNET_DEV_ADDR_VMBUS)
881     {
882       p = mhash_get (&conf->device_config_index_by_vmbus_addr,
883                      (vlib_vmbus_addr_t *) (addr));
884
885       if (!p)
886         {
887           pool_get (conf->dev_confs, devconf);
888           mhash_set (&conf->device_config_index_by_vmbus_addr, addr,
889                      devconf - conf->dev_confs, 0);
890         }
891       else
892         return clib_error_return (
893           0, "duplicate configuration for VMBUS address %U",
894           format_vlib_vmbus_addr, addr);
895     }
896
897   if (addr_type == VNET_DEV_ADDR_PCI)
898     {
899       devconf->pci_addr.as_u32 = ((vlib_pci_addr_t *) (addr))->as_u32;
900       devconf->tso = DPDK_DEVICE_TSO_DEFAULT;
901       devconf->dev_addr_type = VNET_DEV_ADDR_PCI;
902     }
903   else if (addr_type == VNET_DEV_ADDR_VMBUS)
904     {
905       devconf->vmbus_addr = *((vlib_vmbus_addr_t *) (addr));
906       devconf->tso = DPDK_DEVICE_TSO_DEFAULT;
907       devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
908     }
909
910   if (!input)
911     return 0;
912
913   unformat_skip_white_space (input);
914   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
915     {
916       if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
917         ;
918       else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
919         ;
920       else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
921         ;
922       else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
923         ;
924       else if (unformat (input, "name %v", &devconf->name))
925         ;
926       else if (unformat (input, "tag %s", &devconf->tag))
927         ;
928       else if (unformat (input, "workers %U", unformat_bitmap_list,
929                          &devconf->workers))
930         ;
931       else
932         if (unformat
933             (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
934         {
935           error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
936           if (error)
937             break;
938         }
939       else if (unformat (input, "tso on"))
940         {
941           devconf->tso = DPDK_DEVICE_TSO_ON;
942         }
943       else if (unformat (input, "tso off"))
944         {
945           devconf->tso = DPDK_DEVICE_TSO_OFF;
946         }
947       else if (unformat (input, "devargs %s", &devconf->devargs))
948         ;
949       else if (unformat (input, "rss-queues %U",
950                          unformat_bitmap_list, &devconf->rss_queues))
951         ;
952       else if (unformat (input, "max-lro-pkt-size %u",
953                          &devconf->max_lro_pkt_size))
954         ;
955       else
956         {
957           error = clib_error_return (0, "unknown input `%U'",
958                                      format_unformat_error, input);
959           break;
960         }
961     }
962
963   if (error)
964     return error;
965
966   if (devconf->workers && devconf->num_rx_queues == 0)
967     devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
968   else if (devconf->workers &&
969            clib_bitmap_count_set_bits (devconf->workers) !=
970            devconf->num_rx_queues)
971     error = clib_error_return (0,
972                                "%U: number of worker threads must be "
973                                "equal to number of rx queues",
974                                format_vlib_pci_addr, addr);
975
976   return error;
977 }
978
979 static clib_error_t *
980 dpdk_log_read_ready (clib_file_t * uf)
981 {
982   unformat_input_t input;
983   u8 *line, *s = 0;
984   int n, n_try;
985
986   n = n_try = 4096;
987   while (n == n_try)
988     {
989       uword len = vec_len (s);
990       vec_resize (s, len + n_try);
991
992       n = read (uf->file_descriptor, s + len, n_try);
993       if (n < 0 && errno != EAGAIN)
994         return clib_error_return_unix (0, "read");
995       vec_set_len (s, len + (n < 0 ? 0 : n));
996     }
997
998   unformat_init_vector (&input, s);
999
1000   while (unformat_user (&input, unformat_line, &line))
1001     {
1002       int skip = 0;
1003       vec_add1 (line, 0);
1004
1005       /* unfortunatelly DPDK polutes log with this error messages
1006        * even when we pass --in-memory which means no secondary process */
1007       if (strstr ((char *) line, "WARNING! Base virtual address hint"))
1008         skip = 1;
1009       else if (strstr ((char *) line, "This may cause issues with mapping "
1010                                       "memory into secondary processes"))
1011         skip = 1;
1012       vec_pop (line);
1013       if (!skip)
1014         dpdk_log_notice ("%v", line);
1015       vec_free (line);
1016     }
1017
1018   unformat_free (&input);
1019   return 0;
1020 }
1021
1022 static clib_error_t *
1023 dpdk_set_stat_poll_interval (f64 interval)
1024 {
1025   if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
1026     return clib_error_return (0, "wrong stats-poll-interval value");
1027
1028   dpdk_main.stat_poll_interval = interval;
1029   return 0;
1030 }
1031
1032 static clib_error_t *
1033 dpdk_set_link_state_poll_interval (f64 interval)
1034 {
1035   if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
1036     return clib_error_return (0, "wrong link-state-poll-interval value");
1037
1038   dpdk_main.link_state_poll_interval = interval;
1039   return 0;
1040 }
1041
1042 static clib_error_t *
1043 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
1044 {
1045   dpdk_main_t *dm = &dpdk_main;
1046   clib_error_t *error = 0;
1047   dpdk_config_main_t *conf = &dpdk_config_main;
1048   vlib_thread_main_t *tm = vlib_get_thread_main ();
1049   dpdk_device_config_t *devconf;
1050   vlib_pci_addr_t pci_addr = { 0 };
1051   vlib_vmbus_addr_t vmbus_addr = { 0 };
1052   unformat_input_t sub_input;
1053   uword default_hugepage_sz, x;
1054   u8 *s, *tmp = 0;
1055   int ret, i;
1056   int num_whitelisted = 0;
1057   int eal_no_hugetlb = 0;
1058   u8 no_pci = 0;
1059   u8 no_vmbus = 0;
1060   u8 file_prefix = 0;
1061   u8 *socket_mem = 0;
1062   u32 vendor, device, domain, bus, func;
1063   void *fmt_func;
1064   void *fmt_addr;
1065   f64 poll_interval;
1066
1067   conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
1068   mhash_init (&conf->device_config_index_by_vmbus_addr, sizeof (uword),
1069               sizeof (vlib_vmbus_addr_t));
1070
1071   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1072     {
1073       /* Prime the pump */
1074       if (unformat (input, "no-hugetlb"))
1075         {
1076           vec_add1 (conf->eal_init_args, (u8 *) "--no-huge");
1077           eal_no_hugetlb = 1;
1078         }
1079       else if (unformat (input, "telemetry"))
1080         conf->enable_telemetry = 1;
1081
1082       else if (unformat (input, "enable-tcp-udp-checksum"))
1083         {
1084           dm->default_port_conf.enable_tcp_udp_checksum = 1;
1085           if (unformat (input, "enable-outer-checksum-offload"))
1086             dm->default_port_conf.enable_outer_checksum_offload = 1;
1087         }
1088       else if (unformat (input, "no-tx-checksum-offload"))
1089         dm->default_port_conf.disable_tx_checksum_offload = 1;
1090
1091       else if (unformat (input, "decimal-interface-names"))
1092         conf->interface_name_format_decimal = 1;
1093
1094       else if (unformat (input, "no-multi-seg"))
1095         dm->default_port_conf.disable_multi_seg = 1;
1096       else if (unformat (input, "enable-lro"))
1097         dm->default_port_conf.enable_lro = 1;
1098       else if (unformat (input, "max-simd-bitwidth %U",
1099                          unformat_max_simd_bitwidth, &conf->max_simd_bitwidth))
1100         ;
1101       else if (unformat (input, "link-state-poll-interval %f", &poll_interval))
1102         {
1103           error = dpdk_set_link_state_poll_interval (poll_interval);
1104           if (error != 0)
1105             return error;
1106         }
1107       else if (unformat (input, "stats-poll-interval %f", &poll_interval))
1108         {
1109           error = dpdk_set_stat_poll_interval (poll_interval);
1110           if (error != 0)
1111             return error;
1112         }
1113       else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1114                          &sub_input))
1115         {
1116           error =
1117             dpdk_device_config (conf, 0, VNET_DEV_ADDR_ANY, &sub_input, 1);
1118
1119           if (error)
1120             return error;
1121         }
1122       else
1123         if (unformat
1124             (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1125              unformat_vlib_cli_sub_input, &sub_input))
1126         {
1127           error = dpdk_device_config (conf, &pci_addr, VNET_DEV_ADDR_PCI,
1128                                       &sub_input, 0);
1129
1130           if (error)
1131             return error;
1132
1133           num_whitelisted++;
1134         }
1135       else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1136         {
1137           error =
1138             dpdk_device_config (conf, &pci_addr, VNET_DEV_ADDR_PCI, 0, 0);
1139
1140           if (error)
1141             return error;
1142
1143           num_whitelisted++;
1144         }
1145       else if (unformat (input, "dev %U %U", unformat_vlib_vmbus_addr,
1146                          &vmbus_addr, unformat_vlib_cli_sub_input, &sub_input))
1147         {
1148           error = dpdk_device_config (conf, &vmbus_addr, VNET_DEV_ADDR_VMBUS,
1149                                       &sub_input, 0);
1150
1151           if (error)
1152             return error;
1153
1154           num_whitelisted++;
1155         }
1156       else if (unformat (input, "dev %U", unformat_vlib_vmbus_addr,
1157                          &vmbus_addr))
1158         {
1159           error =
1160             dpdk_device_config (conf, &vmbus_addr, VNET_DEV_ADDR_VMBUS, 0, 0);
1161
1162           if (error)
1163             return error;
1164
1165           num_whitelisted++;
1166         }
1167       else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1168         ;
1169       else if (unformat (input, "uio-bind-force"))
1170         conf->uio_bind_force = 1;
1171       else if (unformat (input, "socket-mem %s", &socket_mem))
1172         ;
1173       else if (unformat (input, "no-pci"))
1174         {
1175           no_pci = 1;
1176           tmp = format (0, "--no-pci%c", 0);
1177           vec_add1 (conf->eal_init_args, tmp);
1178         }
1179       else if (unformat (input, "blacklist %U", unformat_vlib_vmbus_addr,
1180                          &vmbus_addr))
1181         {
1182           vec_add1 (conf->blacklist_by_vmbus_addr, vmbus_addr);
1183         }
1184       else
1185         if (unformat
1186             (input, "blacklist %x:%x:%x.%x", &domain, &bus, &device, &func))
1187         {
1188           tmp = format (0, "-b%c", 0);
1189           vec_add1 (conf->eal_init_args, tmp);
1190           tmp =
1191             format (0, "%04x:%02x:%02x.%x%c", domain, bus, device, func, 0);
1192           vec_add1 (conf->eal_init_args, tmp);
1193         }
1194       else if (unformat (input, "blacklist %x:%x", &vendor, &device))
1195         {
1196           u32 blacklist_entry;
1197           if (vendor > 0xFFFF)
1198             return clib_error_return (0, "blacklist PCI vendor out of range");
1199           if (device > 0xFFFF)
1200             return clib_error_return (0, "blacklist PCI device out of range");
1201           blacklist_entry = (vendor << 16) | (device & 0xffff);
1202           vec_add1 (conf->blacklist_by_pci_vendor_and_device,
1203                     blacklist_entry);
1204         }
1205       else if (unformat (input, "no-vmbus"))
1206         {
1207           no_vmbus = 1;
1208           tmp = format (0, "--no-vmbus%c", 0);
1209           vec_add1 (conf->eal_init_args, tmp);
1210         }
1211
1212 #define _(a)                                    \
1213       else if (unformat(input, #a))             \
1214         {                                       \
1215           tmp = format (0, "--%s%c", #a, 0);    \
1216           vec_add1 (conf->eal_init_args, tmp);    \
1217         }
1218       foreach_eal_double_hyphen_predicate_arg
1219 #undef _
1220 #define _(a)                                          \
1221         else if (unformat(input, #a " %s", &s))       \
1222           {                                           \
1223             if (!strncmp(#a, "file-prefix", 11)) \
1224               file_prefix = 1;                        \
1225             tmp = format (0, "--%s%c", #a, 0);        \
1226             vec_add1 (conf->eal_init_args, tmp);      \
1227             vec_add1 (s, 0);                          \
1228             if (!strncmp(#a, "vdev", 4))              \
1229               if (strstr((char*)s, "af_packet"))      \
1230                 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
1231             vec_add1 (conf->eal_init_args, s);        \
1232           }
1233         foreach_eal_double_hyphen_arg
1234 #undef _
1235 #define _(a,b)                                          \
1236           else if (unformat(input, #a " %s", &s))       \
1237             {                                           \
1238               tmp = format (0, "-%s%c", #b, 0);         \
1239               vec_add1 (conf->eal_init_args, tmp);      \
1240               vec_add1 (s, 0);                          \
1241               vec_add1 (conf->eal_init_args, s);        \
1242             }
1243         foreach_eal_single_hyphen_arg
1244 #undef _
1245         else if (unformat (input, "default"))
1246         ;
1247
1248       else if (unformat_skip_white_space (input))
1249         ;
1250       else return clib_error_return (0, "unknown input `%U'",
1251                                      format_unformat_error, input);
1252     }
1253
1254   if (!conf->uio_driver_name)
1255     conf->uio_driver_name = format (0, "auto%c", 0);
1256
1257   if (eal_no_hugetlb == 0)
1258     {
1259       vec_add1 (conf->eal_init_args, (u8 *) "--in-memory");
1260
1261       default_hugepage_sz = clib_mem_get_default_hugepage_size ();
1262
1263       clib_bitmap_foreach (x, tm->cpu_socket_bitmap)
1264         {
1265           clib_error_t *e;
1266           uword n_pages;
1267           /* preallocate at least 16MB of hugepages per socket,
1268             if more is needed it is up to consumer to preallocate more */
1269           n_pages = round_pow2 ((uword) 16 << 20, default_hugepage_sz);
1270           n_pages /= default_hugepage_sz;
1271
1272           if ((e = clib_sysfs_prealloc_hugepages(x, 0, n_pages)))
1273             clib_error_report (e);
1274         }
1275     }
1276
1277   /* on/off dpdk's telemetry thread */
1278   if (conf->enable_telemetry == 0)
1279     {
1280       vec_add1 (conf->eal_init_args, (u8 *) "--no-telemetry");
1281     }
1282
1283   if (!file_prefix)
1284     {
1285       tmp = format (0, "--file-prefix%c", 0);
1286       vec_add1 (conf->eal_init_args, tmp);
1287       tmp = format (0, "vpp%c", 0);
1288       vec_add1 (conf->eal_init_args, tmp);
1289     }
1290
1291   if (no_pci == 0 && geteuid () == 0)
1292     dpdk_bind_devices_to_uio (conf);
1293
1294   if (no_vmbus == 0 && geteuid () == 0)
1295     dpdk_bind_vmbus_devices_to_uio (conf);
1296
1297 #define _(x) \
1298     if (devconf->x == 0 && conf->default_devconf.x > 0) \
1299       devconf->x = conf->default_devconf.x ;
1300
1301   pool_foreach (devconf, conf->dev_confs)  {
1302
1303     /* default per-device config items */
1304     foreach_dpdk_device_config_item
1305
1306       /* copy tso config from default device */
1307       _ (tso)
1308
1309       /* copy tso config from default device */
1310       _ (devargs)
1311
1312       /* copy rss_queues config from default device */
1313       _ (rss_queues)
1314
1315       /* assume that default is PCI */
1316       fmt_func = format_vlib_pci_addr;
1317     fmt_addr = &devconf->pci_addr;
1318
1319     if (devconf->dev_addr_type == VNET_DEV_ADDR_VMBUS)
1320       {
1321         fmt_func = format_vlib_vmbus_addr;
1322         fmt_addr = &devconf->vmbus_addr;
1323       }
1324
1325     /* add DPDK EAL whitelist/blacklist entry */
1326     if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1327       {
1328         tmp = format (0, "-a%c", 0);
1329         vec_add1 (conf->eal_init_args, tmp);
1330         if (devconf->devargs)
1331           {
1332             tmp =
1333               format (0, "%U,%s%c", fmt_func, fmt_addr, devconf->devargs, 0);
1334           }
1335           else
1336           {
1337             tmp = format (0, "%U%c", fmt_func, fmt_addr, 0);
1338           }
1339           vec_add1 (conf->eal_init_args, tmp);
1340       }
1341     else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1342       {
1343         tmp = format (0, "-b%c", 0);
1344         vec_add1 (conf->eal_init_args, tmp);
1345         tmp = format (0, "%U%c", fmt_func, fmt_addr, 0);
1346         vec_add1 (conf->eal_init_args, tmp);
1347       }
1348   }
1349
1350 #undef _
1351
1352   if (socket_mem)
1353     clib_warning ("socket-mem argument is deprecated");
1354
1355   /* NULL terminate the "argv" vector, in case of stupidity */
1356   vec_add1 (conf->eal_init_args, 0);
1357   vec_dec_len (conf->eal_init_args, 1);
1358
1359   /* Set up DPDK eal and packet mbuf pool early. */
1360
1361   int log_fds[2] = { 0 };
1362   if (pipe (log_fds) == 0)
1363     {
1364       if (fcntl (log_fds[0], F_SETFL, O_NONBLOCK) == 0 &&
1365           fcntl (log_fds[1], F_SETFL, O_NONBLOCK) == 0)
1366         {
1367           FILE *f = fdopen (log_fds[1], "a");
1368           if (f && rte_openlog_stream (f) == 0)
1369             {
1370               clib_file_t t = { 0 };
1371               t.read_function = dpdk_log_read_ready;
1372               t.file_descriptor = log_fds[0];
1373               t.description = format (0, "DPDK logging pipe");
1374               clib_file_add (&file_main, &t);
1375             }
1376         }
1377       else
1378         {
1379           close (log_fds[0]);
1380           close (log_fds[1]);
1381         }
1382     }
1383
1384   vm = vlib_get_main ();
1385
1386   /* make copy of args as rte_eal_init tends to mess up with arg array */
1387   for (i = 1; i < vec_len (conf->eal_init_args); i++)
1388     conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1389                                       conf->eal_init_args[i]);
1390
1391   vec_terminate_c_string (conf->eal_init_args_str);
1392
1393   dpdk_log_notice ("EAL init args: %s", conf->eal_init_args_str);
1394   ret = rte_eal_init (vec_len (conf->eal_init_args),
1395                       (char **) conf->eal_init_args);
1396   if (ret < 0)
1397     return clib_error_return (0, "rte_eal_init returned %d", ret);
1398
1399   /* enable the AVX-512 vPMDs in DPDK */
1400   if (clib_cpu_supports_avx512_bitalg () &&
1401       conf->max_simd_bitwidth == DPDK_MAX_SIMD_BITWIDTH_DEFAULT)
1402     rte_vect_set_max_simd_bitwidth (RTE_VECT_SIMD_512);
1403   else if (conf->max_simd_bitwidth != DPDK_MAX_SIMD_BITWIDTH_DEFAULT)
1404     rte_vect_set_max_simd_bitwidth (conf->max_simd_bitwidth ==
1405                                         DPDK_MAX_SIMD_BITWIDTH_256 ?
1406                                       RTE_VECT_SIMD_256 :
1407                                       RTE_VECT_SIMD_512);
1408
1409   /* main thread 1st */
1410   if ((error = dpdk_buffer_pools_create (vm)))
1411     return error;
1412
1413   return 0;
1414 }
1415
1416 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1417
1418 void
1419 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1420 {
1421   vnet_main_t *vnm = vnet_get_main ();
1422   struct rte_eth_link prev_link = xd->link;
1423   u32 hw_flags = 0;
1424   u8 hw_flags_chg = 0;
1425
1426   xd->time_last_link_update = now ? now : xd->time_last_link_update;
1427   clib_memset (&xd->link, 0, sizeof (xd->link));
1428   rte_eth_link_get_nowait (xd->port_id, &xd->link);
1429
1430   if (LINK_STATE_ELOGS)
1431     {
1432       ELOG_TYPE_DECLARE (e) =
1433       {
1434       .format =
1435           "update-link-state: sw_if_index %d, admin_up %d,"
1436           "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1437
1438       struct
1439       {
1440         u32 sw_if_index;
1441         u8 admin_up;
1442         u8 old_link_state;
1443         u8 new_link_state;
1444       } *ed;
1445       ed = ELOG_DATA (&vlib_global_main.elog_main, e);
1446       ed->sw_if_index = xd->sw_if_index;
1447       ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1448       ed->old_link_state = (u8)
1449         vnet_hw_interface_is_link_up (vnm, xd->hw_if_index);
1450       ed->new_link_state = (u8) xd->link.link_status;
1451     }
1452
1453   hw_flags_chg = ((xd->link.link_duplex != prev_link.link_duplex) ||
1454                   (xd->link.link_status != prev_link.link_status));
1455
1456   if (xd->link.link_speed != prev_link.link_speed)
1457     vnet_hw_interface_set_link_speed (vnm, xd->hw_if_index,
1458                                       (xd->link.link_speed == UINT32_MAX) ?
1459                                               UINT32_MAX :
1460                                               xd->link.link_speed * 1000);
1461
1462   if (hw_flags_chg)
1463     {
1464       if (xd->link.link_status)
1465         hw_flags |= VNET_HW_INTERFACE_FLAG_LINK_UP;
1466
1467       switch (xd->link.link_duplex)
1468         {
1469         case RTE_ETH_LINK_HALF_DUPLEX:
1470           hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1471           break;
1472         case RTE_ETH_LINK_FULL_DUPLEX:
1473           hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1474           break;
1475         default:
1476           break;
1477         }
1478
1479       if (LINK_STATE_ELOGS)
1480         {
1481           ELOG_TYPE_DECLARE (e) =
1482           {
1483           .format =
1484               "update-link-state: sw_if_index %d, new flags %d",.format_args
1485               = "i4i4",};
1486
1487           struct
1488           {
1489             u32 sw_if_index;
1490             u32 flags;
1491           } *ed;
1492           ed = ELOG_DATA (&vlib_global_main.elog_main, e);
1493           ed->sw_if_index = xd->sw_if_index;
1494           ed->flags = hw_flags;
1495         }
1496       vnet_hw_interface_set_flags (vnm, xd->hw_if_index, hw_flags);
1497     }
1498 }
1499
1500 static uword
1501 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1502 {
1503   clib_error_t *error;
1504   dpdk_main_t *dm = &dpdk_main;
1505   dpdk_device_t *xd;
1506   vlib_thread_main_t *tm = vlib_get_thread_main ();
1507
1508   vlib_worker_thread_barrier_sync (vm);
1509   error = dpdk_lib_init (dm);
1510
1511   if (error)
1512     clib_error_report (error);
1513
1514   if (dpdk_cryptodev_init)
1515     {
1516       error = dpdk_cryptodev_init (vm);
1517       if (error)
1518         {
1519           vlib_log_warn (dpdk_main.log_cryptodev, "%U", format_clib_error,
1520                          error);
1521           clib_error_free (error);
1522         }
1523     }
1524
1525   vlib_worker_thread_barrier_release (vm);
1526   tm->worker_thread_release = 1;
1527
1528   f64 now = vlib_time_now (vm);
1529   vec_foreach (xd, dm->devices)
1530   {
1531     dpdk_update_link_state (xd, now);
1532   }
1533
1534   f64 timeout =
1535     clib_min (dm->link_state_poll_interval, dm->stat_poll_interval);
1536
1537   while (1)
1538     {
1539       f64 min_wait = clib_max (timeout, DPDK_MIN_POLL_INTERVAL);
1540       vlib_process_wait_for_event_or_clock (vm, min_wait);
1541
1542       timeout =
1543         clib_min (dm->link_state_poll_interval, dm->stat_poll_interval);
1544
1545       if (dm->admin_up_down_in_progress)
1546         /* skip the poll if an admin up down is in progress (on any interface) */
1547         continue;
1548
1549       vec_foreach (xd, dm->devices)
1550       {
1551         f64 now = vlib_time_now (vm);
1552         if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1553           dpdk_update_counters (xd, now);
1554         if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1555           dpdk_update_link_state (xd, now);
1556
1557       }
1558
1559       now = vlib_time_now (vm);
1560       vec_foreach (xd, dm->devices)
1561         {
1562           timeout = clib_min (timeout, xd->time_last_stats_update +
1563                                          dm->stat_poll_interval - now);
1564           timeout = clib_min (timeout, xd->time_last_link_update +
1565                                          dm->link_state_poll_interval - now);
1566         }
1567     }
1568   return 0;
1569 }
1570
1571 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1572     .function = dpdk_process,
1573     .type = VLIB_NODE_TYPE_PROCESS,
1574     .name = "dpdk-process",
1575     .process_log2_n_stack_bytes = 17,
1576 };
1577
1578 static clib_error_t *
1579 dpdk_init (vlib_main_t * vm)
1580 {
1581   dpdk_main_t *dm = &dpdk_main;
1582   clib_error_t *error = 0;
1583
1584   /* verify that structs are cacheline aligned */
1585   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1586                  "Cache line marker must be 1st element in dpdk_device_t");
1587   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1588                  CLIB_CACHE_LINE_BYTES,
1589                  "Data in cache line 0 is bigger than cache line size");
1590   STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1591                  "Cache line marker must be 1st element in frame_queue_trace_t");
1592
1593   dpdk_cli_reference ();
1594
1595   dm->conf = &dpdk_config_main;
1596
1597   vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1598
1599   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1600   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1601
1602   dm->log_default = vlib_log_register_class ("dpdk", 0);
1603   dm->log_cryptodev = vlib_log_register_class ("dpdk", "cryptodev");
1604
1605   return error;
1606 }
1607
1608 VLIB_INIT_FUNCTION (dpdk_init);
1609
1610 static clib_error_t *
1611 dpdk_worker_thread_init (vlib_main_t *vm)
1612 {
1613   if (rte_thread_register () < 0)
1614     clib_panic ("dpdk: cannot register thread %u - %s", vm->thread_index,
1615                 rte_strerror (rte_errno));
1616   return 0;
1617 }
1618
1619 VLIB_WORKER_INIT_FUNCTION (dpdk_worker_thread_init);