dpdk: port type code cleanup
[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/ethernet/ethernet.h>
25 #include <vnet/interface/rx_queue_funcs.h>
26 #include <dpdk/buffer.h>
27 #include <dpdk/device/dpdk.h>
28 #include <dpdk/cryptodev/cryptodev.h>
29 #include <vlib/pci/pci.h>
30 #include <vlib/vmbus/vmbus.h>
31
32 #include <rte_ring.h>
33 #include <rte_vect.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #include <sys/mount.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <dirent.h>
43
44 #include <dpdk/device/dpdk_priv.h>
45
46 #define ETHER_MAX_LEN   1518  /**< Maximum frame len, including CRC. */
47
48 dpdk_main_t dpdk_main;
49 dpdk_config_main_t dpdk_config_main;
50
51 #define LINK_STATE_ELOGS        0
52
53 /* Port configuration, mildly modified Intel app values */
54
55 static dpdk_port_type_t
56 port_type_from_speed_capa (struct rte_eth_dev_info *dev_info)
57 {
58
59   if (dev_info->speed_capa & ETH_LINK_SPEED_100G)
60     return VNET_DPDK_PORT_TYPE_ETH_100G;
61   else if (dev_info->speed_capa & ETH_LINK_SPEED_56G)
62     return VNET_DPDK_PORT_TYPE_ETH_56G;
63   else if (dev_info->speed_capa & ETH_LINK_SPEED_50G)
64     return VNET_DPDK_PORT_TYPE_ETH_50G;
65   else if (dev_info->speed_capa & ETH_LINK_SPEED_40G)
66     return VNET_DPDK_PORT_TYPE_ETH_40G;
67   else if (dev_info->speed_capa & ETH_LINK_SPEED_25G)
68     return VNET_DPDK_PORT_TYPE_ETH_25G;
69   else if (dev_info->speed_capa & ETH_LINK_SPEED_20G)
70     return VNET_DPDK_PORT_TYPE_ETH_20G;
71   else if (dev_info->speed_capa & ETH_LINK_SPEED_10G)
72     return VNET_DPDK_PORT_TYPE_ETH_10G;
73   else if (dev_info->speed_capa & ETH_LINK_SPEED_5G)
74     return VNET_DPDK_PORT_TYPE_ETH_5G;
75   else if (dev_info->speed_capa & ETH_LINK_SPEED_2_5G)
76     return VNET_DPDK_PORT_TYPE_ETH_2_5G;
77   else if (dev_info->speed_capa & ETH_LINK_SPEED_1G)
78     return VNET_DPDK_PORT_TYPE_ETH_1G;
79
80   return VNET_DPDK_PORT_TYPE_UNKNOWN;
81 }
82
83 static u32
84 dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
85 {
86   dpdk_main_t *dm = &dpdk_main;
87   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
88   u32 old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
89
90   switch (flags)
91     {
92     case ETHERNET_INTERFACE_FLAG_DEFAULT_L3:
93       /* set to L3/non-promisc mode */
94       xd->flags &= ~DPDK_DEVICE_FLAG_PROMISC;
95       break;
96     case ETHERNET_INTERFACE_FLAG_ACCEPT_ALL:
97       xd->flags |= DPDK_DEVICE_FLAG_PROMISC;
98       break;
99     case ETHERNET_INTERFACE_FLAG_MTU:
100       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
101         rte_eth_dev_stop (xd->port_id);
102       rte_eth_dev_set_mtu (xd->port_id, hi->max_packet_bytes);
103       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
104         rte_eth_dev_start (xd->port_id);
105       dpdk_log_debug ("[%u] mtu changed to %u", xd->port_id,
106                       hi->max_packet_bytes);
107       return 0;
108     default:
109       return ~0;
110     }
111
112   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
113     {
114       if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
115         rte_eth_promiscuous_enable (xd->port_id);
116       else
117         rte_eth_promiscuous_disable (xd->port_id);
118     }
119
120   return old;
121 }
122
123 /* The function check_l3cache helps check if Level 3 cache exists or not on current CPUs
124   return value 1: exist.
125   return value 0: not exist.
126 */
127 static int
128 check_l3cache ()
129 {
130
131   struct dirent *dp;
132   clib_error_t *err;
133   const char *sys_cache_dir = "/sys/devices/system/cpu/cpu0/cache";
134   DIR *dir_cache = opendir (sys_cache_dir);
135
136   if (dir_cache == NULL)
137     return -1;
138
139   while ((dp = readdir (dir_cache)) != NULL)
140     {
141       if (dp->d_type == DT_DIR)
142         {
143           u8 *p = NULL;
144           int level_cache = -1;
145
146           p = format (p, "%s/%s/%s%c", sys_cache_dir, dp->d_name, "level", 0);
147           if ((err = clib_sysfs_read ((char *) p, "%d", &level_cache)))
148             clib_error_free (err);
149
150           if (level_cache == 3)
151             {
152               closedir (dir_cache);
153               return 1;
154             }
155         }
156     }
157
158   if (dir_cache != NULL)
159     closedir (dir_cache);
160
161   return 0;
162 }
163
164 static void
165 dpdk_enable_l4_csum_offload (dpdk_device_t * xd)
166 {
167   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
168   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
169   xd->flags |= DPDK_DEVICE_FLAG_TX_OFFLOAD |
170     DPDK_DEVICE_FLAG_INTEL_PHDR_CKSUM;
171 }
172
173 static clib_error_t *
174 dpdk_lib_init (dpdk_main_t * dm)
175 {
176   vnet_main_t *vnm = vnet_get_main ();
177   u32 nports;
178   u16 port_id;
179   vlib_main_t *vm = vlib_get_main ();
180   vlib_thread_main_t *tm = vlib_get_thread_main ();
181   vnet_device_main_t *vdm = &vnet_device_main;
182   vnet_sw_interface_t *sw;
183   vnet_hw_interface_t *hi;
184   dpdk_device_t *xd;
185   vlib_pci_addr_t last_pci_addr;
186   u32 last_pci_addr_port = 0;
187   last_pci_addr.as_u32 = ~0;
188
189   nports = rte_eth_dev_count_avail ();
190
191   if (nports < 1)
192     {
193       dpdk_log_notice ("DPDK drivers found no Ethernet devices...");
194     }
195
196   if (CLIB_DEBUG > 0)
197     dpdk_log_notice ("DPDK drivers found %d ports...", nports);
198
199   /* vlib_buffer_t template */
200   vec_validate_aligned (dm->per_thread_data, tm->n_vlib_mains - 1,
201                         CLIB_CACHE_LINE_BYTES);
202   for (int i = 0; i < tm->n_vlib_mains; i++)
203     {
204       dpdk_per_thread_data_t *ptd = vec_elt_at_index (dm->per_thread_data, i);
205       clib_memset (&ptd->buffer_template, 0, sizeof (vlib_buffer_t));
206       vnet_buffer (&ptd->buffer_template)->sw_if_index[VLIB_TX] = (u32) ~ 0;
207     }
208
209   /* device config defaults */
210   dm->default_port_conf.n_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
211   dm->default_port_conf.n_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
212   dm->default_port_conf.n_rx_queues = 1;
213   dm->default_port_conf.n_tx_queues = tm->n_vlib_mains;
214
215   if ((clib_mem_get_default_hugepage_size () == 2 << 20) &&
216       check_l3cache () == 0)
217     dm->default_port_conf.n_rx_desc = dm->default_port_conf.n_tx_desc = 512;
218
219   RTE_ETH_FOREACH_DEV (port_id)
220     {
221       u8 addr[6];
222       struct rte_eth_dev_info di;
223       struct rte_pci_device *pci_dev;
224       struct rte_vmbus_device *vmbus_dev;
225       dpdk_portid_t next_port_id;
226       dpdk_device_config_t *devconf = 0;
227       vnet_eth_interface_registration_t eir = {};
228       vlib_pci_addr_t pci_addr;
229       vlib_vmbus_addr_t vmbus_addr;
230       uword *p = 0;
231
232       if (!rte_eth_dev_is_valid_port (port_id))
233         continue;
234
235       rte_eth_dev_info_get (port_id, &di);
236
237       if (di.device == 0)
238         {
239           dpdk_log_notice ("DPDK bug: missing device info. Skipping %s device",
240                            di.driver_name);
241           continue;
242         }
243
244       pci_dev = dpdk_get_pci_device (&di);
245
246       if (pci_dev)
247         {
248           pci_addr.domain = pci_dev->addr.domain;
249           pci_addr.bus = pci_dev->addr.bus;
250           pci_addr.slot = pci_dev->addr.devid;
251           pci_addr.function = pci_dev->addr.function;
252           p = hash_get (dm->conf->device_config_index_by_pci_addr,
253                         pci_addr.as_u32);
254         }
255
256       vmbus_dev = dpdk_get_vmbus_device (&di);
257
258       if (vmbus_dev)
259         {
260           unformat_input_t input_vmbus;
261           unformat_init_string (&input_vmbus, di.device->name,
262                                 strlen (di.device->name));
263           if (unformat (&input_vmbus, "%U", unformat_vlib_vmbus_addr,
264                         &vmbus_addr))
265             {
266               p = mhash_get (&dm->conf->device_config_index_by_vmbus_addr,
267                              &vmbus_addr);
268             }
269           unformat_free (&input_vmbus);
270         }
271
272       if (p)
273         {
274           devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
275           /* If device is blacklisted, we should skip it */
276           if (devconf->is_blacklisted)
277             {
278               continue;
279             }
280         }
281       else
282         devconf = &dm->conf->default_devconf;
283
284       /* Create vnet interface */
285       vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
286       xd->cpu_socket = (i8) rte_eth_dev_socket_id (port_id);
287       clib_memcpy (&xd->conf, &dm->default_port_conf,
288                    sizeof (dpdk_port_conf_t));
289
290       if (p)
291         {
292           xd->name = devconf->name;
293         }
294
295       /* Handle representor devices that share the same PCI ID */
296       if (di.switch_info.domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
297         {
298           if (di.switch_info.port_id != (uint16_t) -1)
299             xd->interface_name_suffix =
300               format (0, "%d", di.switch_info.port_id);
301         }
302       /* Handle interface naming for devices with multiple ports sharing same
303        * PCI ID */
304       else if (pci_dev && ((next_port_id = rte_eth_find_next (port_id + 1)) !=
305                            RTE_MAX_ETHPORTS))
306         {
307           struct rte_eth_dev_info next_di = { 0 };
308           struct rte_pci_device *next_pci_dev;
309           rte_eth_dev_info_get (next_port_id, &next_di);
310           next_pci_dev = next_di.device ? RTE_DEV_TO_PCI (next_di.device) : 0;
311           if (next_pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
312               memcmp (&pci_dev->addr, &next_pci_dev->addr,
313                       sizeof (struct rte_pci_addr)) == 0)
314             {
315               xd->interface_name_suffix = format (0, "0");
316               last_pci_addr.as_u32 = pci_addr.as_u32;
317               last_pci_addr_port = port_id;
318             }
319           else if (pci_addr.as_u32 == last_pci_addr.as_u32)
320             {
321               xd->interface_name_suffix =
322                 format (0, "%u", port_id - last_pci_addr_port);
323             }
324           else
325             {
326               last_pci_addr.as_u32 = ~0;
327             }
328         }
329       else
330         last_pci_addr.as_u32 = ~0;
331
332       if (di.rx_offload_capa & DEV_RX_OFFLOAD_IPV4_CKSUM)
333         {
334           xd->port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_IPV4_CKSUM;
335           xd->flags |= DPDK_DEVICE_FLAG_RX_IP4_CKSUM;
336         }
337
338       if (xd->conf.enable_tcp_udp_checksum)
339         {
340           if (di.rx_offload_capa & DEV_RX_OFFLOAD_UDP_CKSUM)
341             xd->port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_UDP_CKSUM;
342           if (di.rx_offload_capa & DEV_RX_OFFLOAD_TCP_CKSUM)
343             xd->port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_TCP_CKSUM;
344           if (di.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)
345             xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
346
347           if (xd->conf.enable_outer_checksum_offload)
348             {
349               if (di.tx_offload_capa & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM)
350                 xd->port_conf.txmode.offloads |=
351                   DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
352               if (di.tx_offload_capa & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM)
353                 xd->port_conf.txmode.offloads |=
354                   DEV_TX_OFFLOAD_OUTER_UDP_CKSUM;
355             }
356         }
357
358       if (xd->conf.enable_lro)
359         {
360           if (di.rx_offload_capa & DEV_RX_OFFLOAD_TCP_LRO)
361             {
362               xd->port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_TCP_LRO;
363               if (devconf->max_lro_pkt_size)
364                 xd->port_conf.rxmode.max_lro_pkt_size =
365                   devconf->max_lro_pkt_size;
366               else
367                 xd->port_conf.rxmode.max_lro_pkt_size =
368                   DPDK_MAX_LRO_SIZE_DEFAULT;
369             }
370         }
371       if (xd->conf.no_multi_seg)
372         {
373           xd->port_conf.txmode.offloads &= ~DEV_TX_OFFLOAD_MULTI_SEGS;
374           xd->port_conf.rxmode.offloads &= ~DEV_RX_OFFLOAD_JUMBO_FRAME;
375           xd->port_conf.rxmode.offloads &= ~DEV_RX_OFFLOAD_SCATTER;
376         }
377       else
378         {
379           xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
380           xd->port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
381           xd->port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER;
382           xd->flags |= DPDK_DEVICE_FLAG_MAYBE_MULTISEG;
383         }
384
385       xd->conf.n_tx_queues = clib_min (di.max_tx_queues, xd->conf.n_tx_queues);
386
387       if (devconf->num_tx_queues > 0 &&
388           devconf->num_tx_queues < xd->conf.n_tx_queues)
389         xd->conf.n_tx_queues = devconf->num_tx_queues;
390
391       if (devconf->num_rx_queues > 1 &&
392           di.max_rx_queues >= devconf->num_rx_queues)
393         {
394           xd->conf.n_rx_queues = devconf->num_rx_queues;
395           xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
396           if (devconf->rss_fn == 0)
397             xd->port_conf.rx_adv_conf.rss_conf.rss_hf =
398               ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
399           else
400             {
401               u64 unsupported_bits;
402               xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
403               unsupported_bits = xd->port_conf.rx_adv_conf.rss_conf.rss_hf;
404               unsupported_bits &= ~di.flow_type_rss_offloads;
405               if (unsupported_bits)
406                 dpdk_log_warn ("Unsupported RSS hash functions: %U",
407                                format_dpdk_rss_hf_name, unsupported_bits);
408             }
409           xd->port_conf.rx_adv_conf.rss_conf.rss_hf &=
410             di.flow_type_rss_offloads;
411         }
412
413       if (devconf->num_rx_desc)
414         xd->conf.n_rx_desc = devconf->num_rx_desc;
415
416       if (devconf->num_tx_desc)
417         xd->conf.n_tx_desc = devconf->num_tx_desc;
418
419       vec_validate_aligned (xd->rx_queues, xd->conf.n_rx_queues - 1,
420                             CLIB_CACHE_LINE_BYTES);
421
422       /* workaround for drivers not setting driver_name */
423       if ((!di.driver_name) && (pci_dev))
424         di.driver_name = pci_dev->driver->driver.name;
425
426       ASSERT (di.driver_name);
427
428       if (!xd->pmd)
429         {
430
431 #define _(s, f)                                                               \
432   else if (di.driver_name && !strcmp (di.driver_name, s)) xd->pmd =           \
433     VNET_DPDK_PMD_##f;
434           if (0)
435             ;
436           foreach_dpdk_pmd
437 #undef _
438             else xd->pmd = VNET_DPDK_PMD_UNKNOWN;
439
440           xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
441
442           switch (xd->pmd)
443             {
444               /* Drivers with valid speed_capa set */
445             case VNET_DPDK_PMD_I40E:
446               xd->flags |= DPDK_DEVICE_FLAG_INT_UNMASKABLE;
447               /* fall through */
448             case VNET_DPDK_PMD_E1000EM:
449             case VNET_DPDK_PMD_IGB:
450             case VNET_DPDK_PMD_IGC:
451             case VNET_DPDK_PMD_IXGBE:
452             case VNET_DPDK_PMD_ICE:
453               xd->port_type = port_type_from_speed_capa (&di);
454               xd->supported_flow_actions =
455                 VNET_FLOW_ACTION_MARK | VNET_FLOW_ACTION_REDIRECT_TO_NODE |
456                 VNET_FLOW_ACTION_REDIRECT_TO_QUEUE |
457                 VNET_FLOW_ACTION_BUFFER_ADVANCE | VNET_FLOW_ACTION_COUNT |
458                 VNET_FLOW_ACTION_DROP | VNET_FLOW_ACTION_RSS;
459
460               if (xd->conf.no_tx_checksum_offload == 0)
461                 {
462                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
463                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
464                   xd->flags |= DPDK_DEVICE_FLAG_TX_OFFLOAD |
465                                DPDK_DEVICE_FLAG_INTEL_PHDR_CKSUM;
466                 }
467
468               xd->port_conf.intr_conf.rxq = 1;
469               break;
470             case VNET_DPDK_PMD_MLX5:
471               if (xd->conf.no_tx_checksum_offload == 0)
472                 {
473                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
474                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
475                   xd->flags |= DPDK_DEVICE_FLAG_TX_OFFLOAD |
476                                DPDK_DEVICE_FLAG_INTEL_PHDR_CKSUM;
477                 }
478               xd->port_type = port_type_from_speed_capa (&di);
479               break;
480             case VNET_DPDK_PMD_CXGBE:
481             case VNET_DPDK_PMD_MLX4:
482             case VNET_DPDK_PMD_QEDE:
483             case VNET_DPDK_PMD_BNXT:
484               xd->port_type = port_type_from_speed_capa (&di);
485               break;
486
487               /* SR-IOV VFs */
488             case VNET_DPDK_PMD_I40EVF:
489               xd->flags |= DPDK_DEVICE_FLAG_INT_UNMASKABLE;
490               /* fall through */
491             case VNET_DPDK_PMD_IGBVF:
492             case VNET_DPDK_PMD_IXGBEVF:
493               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_VF;
494               if (xd->conf.no_tx_checksum_offload == 0)
495                 {
496                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
497                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
498                   xd->flags |= DPDK_DEVICE_FLAG_TX_OFFLOAD |
499                                DPDK_DEVICE_FLAG_INTEL_PHDR_CKSUM;
500                 }
501               /* DPDK bug in multiqueue... */
502               /* xd->port_conf.intr_conf.rxq = 1; */
503               break;
504
505               /* iAVF */
506             case VNET_DPDK_PMD_IAVF:
507               xd->flags |= DPDK_DEVICE_FLAG_INT_UNMASKABLE;
508               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_VF;
509               xd->supported_flow_actions =
510                 VNET_FLOW_ACTION_MARK | VNET_FLOW_ACTION_REDIRECT_TO_NODE |
511                 VNET_FLOW_ACTION_REDIRECT_TO_QUEUE |
512                 VNET_FLOW_ACTION_BUFFER_ADVANCE | VNET_FLOW_ACTION_COUNT |
513                 VNET_FLOW_ACTION_DROP | VNET_FLOW_ACTION_RSS;
514
515               if (xd->conf.no_tx_checksum_offload == 0)
516                 {
517                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
518                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
519                   xd->flags |= DPDK_DEVICE_FLAG_TX_OFFLOAD |
520                                DPDK_DEVICE_FLAG_INTEL_PHDR_CKSUM;
521                 }
522               /* DPDK bug in multiqueue... */
523               /* xd->port_conf.intr_conf.rxq = 1; */
524               break;
525
526             case VNET_DPDK_PMD_THUNDERX:
527               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_VF;
528
529               if (xd->conf.no_tx_checksum_offload == 0)
530                 {
531                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
532                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
533                   xd->flags |= DPDK_DEVICE_FLAG_TX_OFFLOAD;
534                 }
535               break;
536
537             case VNET_DPDK_PMD_ENA:
538               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_VF;
539               xd->port_conf.rxmode.offloads &= ~DEV_RX_OFFLOAD_SCATTER;
540               xd->port_conf.intr_conf.rxq = 1;
541               if (xd->conf.no_tx_checksum_offload == 0)
542                 {
543                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
544                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
545                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
546                   xd->flags |= DPDK_DEVICE_FLAG_TX_OFFLOAD;
547                 }
548               break;
549
550             case VNET_DPDK_PMD_DPAA2:
551               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
552               break;
553
554               /* Cisco VIC */
555             case VNET_DPDK_PMD_ENIC:
556               {
557                 xd->port_type = port_type_from_speed_capa (&di);
558                 if (xd->conf.enable_tcp_udp_checksum)
559                   dpdk_enable_l4_csum_offload (xd);
560               }
561               break;
562
563               /* Intel Red Rock Canyon */
564             case VNET_DPDK_PMD_FM10K:
565               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
566               break;
567
568               /* virtio */
569             case VNET_DPDK_PMD_VIRTIO:
570               xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
571               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
572               xd->conf.n_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
573               xd->conf.n_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
574               /*
575                * Enable use of RX interrupts if supported.
576                *
577                * There is no device flag or capability for this, so
578                * use the same check that the virtio driver does.
579                */
580               if (pci_dev && rte_intr_cap_multiple (&pci_dev->intr_handle))
581                 xd->port_conf.intr_conf.rxq = 1;
582               break;
583
584               /* vmxnet3 */
585             case VNET_DPDK_PMD_VMXNET3:
586               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
587               xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
588               /* TCP csum offload not working although udp might work. Left
589                * disabled for now */
590               if (0 && (xd->conf.no_tx_checksum_offload == 0))
591                 {
592                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
593                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_CKSUM;
594                   xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_UDP_CKSUM;
595                   xd->flags |= DPDK_DEVICE_FLAG_TX_OFFLOAD;
596                 }
597               break;
598
599             case VNET_DPDK_PMD_AF_PACKET:
600               xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
601               break;
602
603             case VNET_DPDK_PMD_VIRTIO_USER:
604               xd->port_type = VNET_DPDK_PORT_TYPE_VIRTIO_USER;
605               break;
606
607             case VNET_DPDK_PMD_VHOST_ETHER:
608               xd->port_type = VNET_DPDK_PORT_TYPE_VHOST_ETHER;
609               break;
610
611             case VNET_DPDK_PMD_LIOVF_ETHER:
612               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_VF;
613               break;
614
615             case VNET_DPDK_PMD_FAILSAFE:
616               xd->port_type = VNET_DPDK_PORT_TYPE_FAILSAFE;
617               xd->port_conf.intr_conf.lsc = 1;
618               break;
619
620             case VNET_DPDK_PMD_NETVSC:
621               {
622                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_VF;
623               }
624               break;
625
626             default:
627               xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
628             }
629         }
630
631       if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
632         {
633           f64 now = vlib_time_now (vm);
634           u32 rnd;
635           rnd = (u32) (now * 1e6);
636           rnd = random_u32 (&rnd);
637           clib_memcpy (addr + 2, &rnd, sizeof (rnd));
638           addr[0] = 2;
639           addr[1] = 0xfe;
640         }
641       else
642         rte_eth_macaddr_get (port_id, (void *) addr);
643
644       xd->port_id = port_id;
645       xd->device_index = xd - dm->devices;
646       xd->per_interface_next_index = ~0;
647
648       /* assign interface to input thread */
649       int q;
650
651       eir.dev_class_index = dpdk_device_class.index;
652       eir.dev_instance = xd->device_index;
653       eir.address = addr;
654       eir.cb.flag_change = dpdk_flag_change;
655       xd->hw_if_index = vnet_eth_register_interface (vnm, &eir);
656
657       sw = vnet_get_hw_sw_interface (vnm, xd->hw_if_index);
658       xd->sw_if_index = sw->sw_if_index;
659       vnet_hw_if_set_input_node (vnm, xd->hw_if_index, dpdk_input_node.index);
660
661       if (devconf->workers)
662         {
663           int j;
664           q = 0;
665           clib_bitmap_foreach (j, devconf->workers)
666             {
667               dpdk_rx_queue_t *rxq = vec_elt_at_index (xd->rx_queues, q);
668               rxq->queue_index = vnet_hw_if_register_rx_queue (
669                 vnm, xd->hw_if_index, q++, vdm->first_worker_thread_index + j);
670             }
671         }
672       else
673         for (q = 0; q < xd->conf.n_rx_queues; q++)
674           {
675             dpdk_rx_queue_t *rxq = vec_elt_at_index (xd->rx_queues, q);
676             rxq->queue_index = vnet_hw_if_register_rx_queue (
677               vnm, xd->hw_if_index, q, VNET_HW_IF_RXQ_THREAD_ANY);
678           }
679
680       vnet_hw_if_update_runtime_data (vnm, xd->hw_if_index);
681
682       /*Get vnet hardware interface */
683       hi = vnet_get_hw_interface (vnm, xd->hw_if_index);
684
685       if (hi)
686         {
687           hi->numa_node = xd->cpu_socket;
688
689           /* Indicate ability to support L3 DMAC filtering and
690            * initialize interface to L3 non-promisc mode */
691           hi->caps |= VNET_HW_IF_CAP_MAC_FILTER;
692           ethernet_set_flags (vnm, xd->hw_if_index,
693                               ETHERNET_INTERFACE_FLAG_DEFAULT_L3);
694         }
695
696       if (xd->conf.no_tx_checksum_offload == 0)
697         if (xd->flags & DPDK_DEVICE_FLAG_TX_OFFLOAD && hi != NULL)
698           {
699             hi->caps |= VNET_HW_IF_CAP_TX_IP4_CKSUM |
700                         VNET_HW_IF_CAP_TX_TCP_CKSUM |
701                         VNET_HW_IF_CAP_TX_UDP_CKSUM;
702             if (xd->conf.enable_outer_checksum_offload)
703               {
704                 hi->caps |= VNET_HW_IF_CAP_TX_IP4_OUTER_CKSUM |
705                             VNET_HW_IF_CAP_TX_UDP_OUTER_CKSUM;
706               }
707           }
708       if (devconf->tso == DPDK_DEVICE_TSO_ON && hi != NULL)
709         {
710           /*tcp_udp checksum must be enabled*/
711           if ((xd->conf.enable_tcp_udp_checksum) &&
712               (hi->caps & VNET_HW_IF_CAP_TX_CKSUM))
713             {
714               hi->caps |= VNET_HW_IF_CAP_TCP_GSO;
715               xd->port_conf.txmode.offloads |= DEV_TX_OFFLOAD_TCP_TSO;
716
717               if (xd->conf.enable_outer_checksum_offload &&
718                   (di.tx_offload_capa & DEV_TX_OFFLOAD_VXLAN_TNL_TSO))
719                 {
720                   xd->port_conf.txmode.offloads |=
721                     DEV_TX_OFFLOAD_VXLAN_TNL_TSO;
722                   hi->caps |= VNET_HW_IF_CAP_VXLAN_TNL_GSO;
723                 }
724             }
725           else
726             clib_warning ("%s: TCP/UDP checksum offload must be enabled",
727                           hi->name);
728         }
729
730       dpdk_device_setup (xd);
731
732       /* rss queues should be configured after dpdk_device_setup() */
733       if ((hi != NULL) && (devconf->rss_queues != NULL))
734         {
735           if (vnet_hw_interface_set_rss_queues (vnet_get_main (), hi,
736                                                 devconf->rss_queues))
737             {
738               clib_warning ("%s: Failed to set rss queues", hi->name);
739             }
740         }
741
742       if (vec_len (xd->errors))
743         dpdk_log_err ("setup failed for device %U. Errors:\n  %U",
744                       format_dpdk_device_name, port_id,
745                       format_dpdk_device_errors, xd);
746     }
747
748   return 0;
749 }
750
751 static void
752 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
753 {
754   vlib_main_t *vm = vlib_get_main ();
755   clib_error_t *error;
756   u8 *pci_addr = 0;
757   int num_whitelisted = vec_len (conf->dev_confs);
758   vlib_pci_device_info_t *d = 0;
759   vlib_pci_addr_t *addr = 0, *addrs;
760   int i;
761
762   addrs = vlib_pci_get_all_dev_addrs ();
763   /* *INDENT-OFF* */
764   vec_foreach (addr, addrs)
765     {
766     dpdk_device_config_t * devconf = 0;
767     vec_reset_length (pci_addr);
768     pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, addr, 0);
769     if (d)
770     {
771       vlib_pci_free_device_info (d);
772       d = 0;
773       }
774     d = vlib_pci_get_device_info (vm, addr, &error);
775     if (error)
776     {
777       vlib_log_warn (dpdk_main.log_default, "%U", format_clib_error, error);
778       clib_error_free (error);
779       continue;
780     }
781
782     if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && d->device_class != PCI_CLASS_PROCESSOR_CO)
783       continue;
784
785     if (num_whitelisted)
786       {
787         uword * p = hash_get (conf->device_config_index_by_pci_addr, addr->as_u32);
788
789         if (!p)
790           {
791           skipped_pci:
792             continue;
793           }
794
795         devconf = pool_elt_at_index (conf->dev_confs, p[0]);
796       }
797
798     /* Enforce Device blacklist by vendor and device */
799     for (i = 0; i < vec_len (conf->blacklist_by_pci_vendor_and_device); i++)
800       {
801         u16 vendor, device;
802         vendor = (u16)(conf->blacklist_by_pci_vendor_and_device[i] >> 16);
803         device = (u16)(conf->blacklist_by_pci_vendor_and_device[i] & 0xFFFF);
804         if (d->vendor_id == vendor && d->device_id == device)
805           {
806             /*
807              * Expected case: device isn't whitelisted,
808              * so blacklist it...
809              */
810             if (devconf == 0)
811               {
812                 /* Device is blacklisted */
813                 pool_get (conf->dev_confs, devconf);
814                 hash_set (conf->device_config_index_by_pci_addr, addr->as_u32,
815                           devconf - conf->dev_confs);
816                 devconf->pci_addr.as_u32 = addr->as_u32;
817                 devconf->dev_addr_type = VNET_DEV_ADDR_PCI;
818                 devconf->is_blacklisted = 1;
819                 goto skipped_pci;
820               }
821             else /* explicitly whitelisted, ignore the device blacklist  */
822               break;
823           }
824       }
825
826     /* virtio */
827     if (d->vendor_id == 0x1af4 &&
828             (d->device_id == VIRTIO_PCI_LEGACY_DEVICEID_NET ||
829              d->device_id == VIRTIO_PCI_MODERN_DEVICEID_NET))
830       ;
831     /* vmxnet3 */
832     else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
833       {
834         /*
835          * For vmxnet3 PCI, unless it is explicitly specified in the whitelist,
836          * the default is to put it in the blacklist.
837          */
838         if (devconf == 0)
839           {
840             pool_get (conf->dev_confs, devconf);
841             hash_set (conf->device_config_index_by_pci_addr, addr->as_u32,
842                       devconf - conf->dev_confs);
843             devconf->pci_addr.as_u32 = addr->as_u32;
844             devconf->is_blacklisted = 1;
845           }
846       }
847     /* all Intel network devices */
848     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_NETWORK_ETHERNET)
849       ;
850     /* all Intel QAT devices VFs */
851     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_PROCESSOR_CO &&
852         (d->device_id == 0x0443 || d->device_id == 0x18a1 || d->device_id == 0x19e3 ||
853         d->device_id == 0x37c9 || d->device_id == 0x6f55))
854       ;
855     /* Cisco VIC */
856     else if (d->vendor_id == 0x1137 &&
857         (d->device_id == 0x0043 || d->device_id == 0x0071))
858       ;
859     /* Chelsio T4/T5 */
860     else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
861       ;
862     /* Amazon Elastic Network Adapter */
863     else if (d->vendor_id == 0x1d0f && d->device_id >= 0xec20 && d->device_id <= 0xec21)
864       ;
865     /* Cavium Network Adapter */
866     else if (d->vendor_id == 0x177d && d->device_id == 0x9712)
867       ;
868     /* Cavium FastlinQ QL41000 Series */
869     else if (d->vendor_id == 0x1077 && d->device_id >= 0x8070 && d->device_id <= 0x8090)
870       ;
871     /* Mellanox CX3, CX3VF */
872     else if (d->vendor_id == 0x15b3 && d->device_id >= 0x1003 && d->device_id <= 0x1004)
873       {
874         continue;
875       }
876     /* Mellanox CX4, CX4VF, CX4LX, CX4LXVF, CX5, CX5VF, CX5EX, CX5EXVF */
877     else if (d->vendor_id == 0x15b3 && d->device_id >= 0x1013 && d->device_id <= 0x101a)
878       {
879         continue;
880       }
881     /* Mellanox CX6, CX6VF, CX6DX, CX6DXVF */
882     else if (d->vendor_id == 0x15b3 && d->device_id >= 0x101b && d->device_id <= 0x101e)
883       {
884         continue;
885       }
886     /* Broadcom NetXtreme S, and E series only */
887     else if (d->vendor_id == 0x14e4 &&
888         ((d->device_id >= 0x16c0 &&
889                 d->device_id != 0x16c6 && d->device_id != 0x16c7 &&
890                 d->device_id != 0x16dd && d->device_id != 0x16f7 &&
891                 d->device_id != 0x16fd && d->device_id != 0x16fe &&
892                 d->device_id != 0x170d && d->device_id != 0x170c &&
893                 d->device_id != 0x170e && d->device_id != 0x1712 &&
894                 d->device_id != 0x1713) ||
895         (d->device_id == 0x1604 || d->device_id == 0x1605 ||
896          d->device_id == 0x1614 || d->device_id == 0x1606 ||
897          d->device_id == 0x1609 || d->device_id == 0x1614)))
898       ;
899     else
900       {
901         dpdk_log_warn ("Unsupported PCI device 0x%04x:0x%04x found "
902                       "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
903                       pci_addr);
904         continue;
905       }
906
907     error = vlib_pci_bind_to_uio (vm, addr, (char *) conf->uio_driver_name);
908
909     if (error)
910       {
911         if (devconf == 0)
912           {
913             pool_get (conf->dev_confs, devconf);
914             hash_set (conf->device_config_index_by_pci_addr, addr->as_u32,
915                       devconf - conf->dev_confs);
916             devconf->pci_addr.as_u32 = addr->as_u32;
917           }
918         devconf->dev_addr_type = VNET_DEV_ADDR_PCI;
919         devconf->is_blacklisted = 1;
920         clib_error_report (error);
921       }
922   }
923   /* *INDENT-ON* */
924   vec_free (pci_addr);
925   vlib_pci_free_device_info (d);
926 }
927
928 static void
929 dpdk_bind_vmbus_devices_to_uio (dpdk_config_main_t * conf)
930 {
931   clib_error_t *error;
932   vlib_vmbus_addr_t *addrs, *addr = 0;
933   int num_whitelisted = vec_len (conf->dev_confs);
934   int i;
935
936   addrs = vlib_vmbus_get_all_dev_addrs ();
937
938   /* *INDENT-OFF* */
939   vec_foreach (addr, addrs)
940     {
941       dpdk_device_config_t *devconf = 0;
942       if (num_whitelisted)
943         {
944           uword *p =
945             mhash_get (&conf->device_config_index_by_vmbus_addr, addr);
946           if (!p)
947             {
948               /* No devices blacklisted, but have whitelisted. blacklist all
949                * non-whitelisted */
950               pool_get (conf->dev_confs, devconf);
951               mhash_set (&conf->device_config_index_by_vmbus_addr, addr,
952                          devconf - conf->dev_confs, 0);
953               devconf->vmbus_addr = *addr;
954               devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
955               devconf->is_blacklisted = 1;
956             skipped_vmbus:
957               continue;
958             }
959
960           devconf = pool_elt_at_index (conf->dev_confs, p[0]);
961         }
962
963       /* Enforce Device blacklist by vmbus_addr */
964       for (i = 0; i < vec_len (conf->blacklist_by_vmbus_addr); i++)
965         {
966           vlib_vmbus_addr_t *a1 = &conf->blacklist_by_vmbus_addr[i];
967           vlib_vmbus_addr_t *a2 = addr;
968           if (memcmp (a1, a2, sizeof (vlib_vmbus_addr_t)) == 0)
969             {
970               if (devconf == 0)
971                 {
972                   /* Device not whitelisted */
973                   pool_get (conf->dev_confs, devconf);
974                   mhash_set (&conf->device_config_index_by_vmbus_addr, addr,
975                              devconf - conf->dev_confs, 0);
976                   devconf->vmbus_addr = *addr;
977                   devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
978                   devconf->is_blacklisted = 1;
979                   goto skipped_vmbus;
980                 }
981               else
982                 {
983                   break;
984                 }
985             }
986         }
987
988       error = vlib_vmbus_bind_to_uio (addr);
989       if (error)
990         {
991           if (devconf == 0)
992             {
993               pool_get (conf->dev_confs, devconf);
994               mhash_set (&conf->device_config_index_by_vmbus_addr, addr,
995                          devconf - conf->dev_confs, 0);
996               devconf->vmbus_addr = *addr;
997             }
998           devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
999           devconf->is_blacklisted = 1;
1000           clib_error_report (error);
1001         }
1002     }
1003   /* *INDENT-ON* */
1004 }
1005
1006 uword
1007 unformat_max_simd_bitwidth (unformat_input_t *input, va_list *va)
1008 {
1009   uword *max_simd_bitwidth = va_arg (*va, uword *);
1010
1011   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1012     {
1013       if (!unformat (input, "%u", max_simd_bitwidth))
1014         goto error;
1015
1016       if (*max_simd_bitwidth != DPDK_MAX_SIMD_BITWIDTH_256 &&
1017           *max_simd_bitwidth != DPDK_MAX_SIMD_BITWIDTH_512)
1018         goto error;
1019     }
1020   return 1;
1021 error:
1022   return 0;
1023 }
1024
1025 static clib_error_t *
1026 dpdk_device_config (dpdk_config_main_t *conf, void *addr,
1027                     dpdk_device_addr_type_t addr_type, unformat_input_t *input,
1028                     u8 is_default)
1029 {
1030   clib_error_t *error = 0;
1031   uword *p;
1032   dpdk_device_config_t *devconf = 0;
1033   unformat_input_t sub_input;
1034
1035   if (is_default)
1036     {
1037       devconf = &conf->default_devconf;
1038     }
1039   else if (addr_type == VNET_DEV_ADDR_PCI)
1040     {
1041       p = hash_get (conf->device_config_index_by_pci_addr,
1042                     ((vlib_pci_addr_t *) (addr))->as_u32);
1043
1044       if (!p)
1045         {
1046           pool_get (conf->dev_confs, devconf);
1047           hash_set (conf->device_config_index_by_pci_addr,
1048                     ((vlib_pci_addr_t *) (addr))->as_u32,
1049                     devconf - conf->dev_confs);
1050         }
1051       else
1052         return clib_error_return (0,
1053                                   "duplicate configuration for PCI address %U",
1054                                   format_vlib_pci_addr, addr);
1055     }
1056   else if (addr_type == VNET_DEV_ADDR_VMBUS)
1057     {
1058       p = mhash_get (&conf->device_config_index_by_vmbus_addr,
1059                      (vlib_vmbus_addr_t *) (addr));
1060
1061       if (!p)
1062         {
1063           pool_get (conf->dev_confs, devconf);
1064           mhash_set (&conf->device_config_index_by_vmbus_addr, addr,
1065                      devconf - conf->dev_confs, 0);
1066         }
1067       else
1068         return clib_error_return (
1069           0, "duplicate configuration for VMBUS address %U",
1070           format_vlib_vmbus_addr, addr);
1071     }
1072
1073   if (addr_type == VNET_DEV_ADDR_PCI)
1074     {
1075       devconf->pci_addr.as_u32 = ((vlib_pci_addr_t *) (addr))->as_u32;
1076       devconf->tso = DPDK_DEVICE_TSO_DEFAULT;
1077       devconf->dev_addr_type = VNET_DEV_ADDR_PCI;
1078     }
1079   else if (addr_type == VNET_DEV_ADDR_VMBUS)
1080     {
1081       devconf->vmbus_addr = *((vlib_vmbus_addr_t *) (addr));
1082       devconf->tso = DPDK_DEVICE_TSO_DEFAULT;
1083       devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
1084     }
1085
1086   if (!input)
1087     return 0;
1088
1089   unformat_skip_white_space (input);
1090   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1091     {
1092       if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
1093         ;
1094       else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
1095         ;
1096       else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
1097         ;
1098       else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
1099         ;
1100       else if (unformat (input, "name %s", &devconf->name))
1101         ;
1102       else if (unformat (input, "workers %U", unformat_bitmap_list,
1103                          &devconf->workers))
1104         ;
1105       else
1106         if (unformat
1107             (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
1108         {
1109           error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
1110           if (error)
1111             break;
1112         }
1113       else if (unformat (input, "tso on"))
1114         {
1115           devconf->tso = DPDK_DEVICE_TSO_ON;
1116         }
1117       else if (unformat (input, "tso off"))
1118         {
1119           devconf->tso = DPDK_DEVICE_TSO_OFF;
1120         }
1121       else if (unformat (input, "devargs %s", &devconf->devargs))
1122         ;
1123       else if (unformat (input, "rss-queues %U",
1124                          unformat_bitmap_list, &devconf->rss_queues))
1125         ;
1126       else if (unformat (input, "max-lro-pkt-size %u",
1127                          &devconf->max_lro_pkt_size))
1128         ;
1129       else
1130         {
1131           error = clib_error_return (0, "unknown input `%U'",
1132                                      format_unformat_error, input);
1133           break;
1134         }
1135     }
1136
1137   if (error)
1138     return error;
1139
1140   if (devconf->workers && devconf->num_rx_queues == 0)
1141     devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
1142   else if (devconf->workers &&
1143            clib_bitmap_count_set_bits (devconf->workers) !=
1144            devconf->num_rx_queues)
1145     error = clib_error_return (0,
1146                                "%U: number of worker threads must be "
1147                                "equal to number of rx queues",
1148                                format_vlib_pci_addr, addr);
1149
1150   return error;
1151 }
1152
1153 static clib_error_t *
1154 dpdk_log_read_ready (clib_file_t * uf)
1155 {
1156   unformat_input_t input;
1157   u8 *line, *s = 0;
1158   int n, n_try;
1159
1160   n = n_try = 4096;
1161   while (n == n_try)
1162     {
1163       uword len = vec_len (s);
1164       vec_resize (s, len + n_try);
1165
1166       n = read (uf->file_descriptor, s + len, n_try);
1167       if (n < 0 && errno != EAGAIN)
1168         return clib_error_return_unix (0, "read");
1169       _vec_len (s) = len + (n < 0 ? 0 : n);
1170     }
1171
1172   unformat_init_vector (&input, s);
1173
1174   while (unformat_user (&input, unformat_line, &line))
1175     {
1176       dpdk_log_notice ("%v", line);
1177       vec_free (line);
1178     }
1179
1180   unformat_free (&input);
1181   return 0;
1182 }
1183
1184 static clib_error_t *
1185 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
1186 {
1187   dpdk_main_t *dm = &dpdk_main;
1188   clib_error_t *error = 0;
1189   dpdk_config_main_t *conf = &dpdk_config_main;
1190   vlib_thread_main_t *tm = vlib_get_thread_main ();
1191   dpdk_device_config_t *devconf;
1192   vlib_pci_addr_t pci_addr = { 0 };
1193   vlib_vmbus_addr_t vmbus_addr = { 0 };
1194   unformat_input_t sub_input;
1195   uword default_hugepage_sz, x;
1196   u8 *s, *tmp = 0;
1197   int ret, i;
1198   int num_whitelisted = 0;
1199   int eal_no_hugetlb = 0;
1200   u8 no_pci = 0;
1201   u8 no_vmbus = 0;
1202   u8 file_prefix = 0;
1203   u8 *socket_mem = 0;
1204   u8 *huge_dir_path = 0;
1205   u32 vendor, device, domain, bus, func;
1206
1207   huge_dir_path =
1208     format (0, "%s/hugepages%c", vlib_unix_get_runtime_dir (), 0);
1209
1210   conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
1211   mhash_init (&conf->device_config_index_by_vmbus_addr, sizeof (uword),
1212               sizeof (vlib_vmbus_addr_t));
1213
1214   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1215     {
1216       /* Prime the pump */
1217       if (unformat (input, "no-hugetlb"))
1218         {
1219           vec_add1 (conf->eal_init_args, (u8 *) "--no-huge");
1220           eal_no_hugetlb = 1;
1221         }
1222       else if (unformat (input, "telemetry"))
1223         conf->enable_telemetry = 1;
1224
1225       else if (unformat (input, "enable-tcp-udp-checksum"))
1226         {
1227           dm->default_port_conf.enable_tcp_udp_checksum = 1;
1228           if (unformat (input, "enable-outer-checksum-offload"))
1229             dm->default_port_conf.enable_outer_checksum_offload = 1;
1230         }
1231       else if (unformat (input, "no-tx-checksum-offload"))
1232         dm->default_port_conf.no_tx_checksum_offload = 1;
1233
1234       else if (unformat (input, "decimal-interface-names"))
1235         conf->interface_name_format_decimal = 1;
1236
1237       else if (unformat (input, "no-multi-seg"))
1238         dm->default_port_conf.no_multi_seg = 1;
1239       else if (unformat (input, "enable-lro"))
1240         dm->default_port_conf.enable_lro = 1;
1241       else if (unformat (input, "max-simd-bitwidth %U",
1242                          unformat_max_simd_bitwidth, &conf->max_simd_bitwidth))
1243         ;
1244       else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1245                          &sub_input))
1246         {
1247           error =
1248             dpdk_device_config (conf, 0, VNET_DEV_ADDR_ANY, &sub_input, 1);
1249
1250           if (error)
1251             return error;
1252         }
1253       else
1254         if (unformat
1255             (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1256              unformat_vlib_cli_sub_input, &sub_input))
1257         {
1258           error = dpdk_device_config (conf, &pci_addr, VNET_DEV_ADDR_PCI,
1259                                       &sub_input, 0);
1260
1261           if (error)
1262             return error;
1263
1264           num_whitelisted++;
1265         }
1266       else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1267         {
1268           error =
1269             dpdk_device_config (conf, &pci_addr, VNET_DEV_ADDR_PCI, 0, 0);
1270
1271           if (error)
1272             return error;
1273
1274           num_whitelisted++;
1275         }
1276       else if (unformat (input, "dev %U %U", unformat_vlib_vmbus_addr,
1277                          &vmbus_addr, unformat_vlib_cli_sub_input, &sub_input))
1278         {
1279           error = dpdk_device_config (conf, &vmbus_addr, VNET_DEV_ADDR_VMBUS,
1280                                       &sub_input, 0);
1281
1282           if (error)
1283             return error;
1284
1285           num_whitelisted++;
1286         }
1287       else if (unformat (input, "dev %U", unformat_vlib_vmbus_addr,
1288                          &vmbus_addr))
1289         {
1290           error =
1291             dpdk_device_config (conf, &vmbus_addr, VNET_DEV_ADDR_VMBUS, 0, 0);
1292
1293           if (error)
1294             return error;
1295
1296           num_whitelisted++;
1297         }
1298       else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1299         ;
1300       else if (unformat (input, "socket-mem %s", &socket_mem))
1301         ;
1302       else if (unformat (input, "no-pci"))
1303         {
1304           no_pci = 1;
1305           tmp = format (0, "--no-pci%c", 0);
1306           vec_add1 (conf->eal_init_args, tmp);
1307         }
1308       else if (unformat (input, "blacklist %U", unformat_vlib_vmbus_addr,
1309                          &vmbus_addr))
1310         {
1311           vec_add1 (conf->blacklist_by_vmbus_addr, vmbus_addr);
1312         }
1313       else
1314         if (unformat
1315             (input, "blacklist %x:%x:%x.%x", &domain, &bus, &device, &func))
1316         {
1317           tmp = format (0, "-b%c", 0);
1318           vec_add1 (conf->eal_init_args, tmp);
1319           tmp =
1320             format (0, "%04x:%02x:%02x.%x%c", domain, bus, device, func, 0);
1321           vec_add1 (conf->eal_init_args, tmp);
1322         }
1323       else if (unformat (input, "blacklist %x:%x", &vendor, &device))
1324         {
1325           u32 blacklist_entry;
1326           if (vendor > 0xFFFF)
1327             return clib_error_return (0, "blacklist PCI vendor out of range");
1328           if (device > 0xFFFF)
1329             return clib_error_return (0, "blacklist PCI device out of range");
1330           blacklist_entry = (vendor << 16) | (device & 0xffff);
1331           vec_add1 (conf->blacklist_by_pci_vendor_and_device,
1332                     blacklist_entry);
1333         }
1334       else if (unformat (input, "no-vmbus"))
1335         {
1336           no_vmbus = 1;
1337           tmp = format (0, "--no-vmbus%c", 0);
1338           vec_add1 (conf->eal_init_args, tmp);
1339         }
1340
1341 #define _(a)                                    \
1342       else if (unformat(input, #a))             \
1343         {                                       \
1344           tmp = format (0, "--%s%c", #a, 0);    \
1345           vec_add1 (conf->eal_init_args, tmp);    \
1346         }
1347       foreach_eal_double_hyphen_predicate_arg
1348 #undef _
1349 #define _(a)                                          \
1350         else if (unformat(input, #a " %s", &s))       \
1351           {                                           \
1352             if (!strncmp(#a, "file-prefix", 11)) \
1353               file_prefix = 1;                        \
1354             tmp = format (0, "--%s%c", #a, 0);        \
1355             vec_add1 (conf->eal_init_args, tmp);      \
1356             vec_add1 (s, 0);                          \
1357             if (!strncmp(#a, "vdev", 4))              \
1358               if (strstr((char*)s, "af_packet"))      \
1359                 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
1360             vec_add1 (conf->eal_init_args, s);        \
1361           }
1362         foreach_eal_double_hyphen_arg
1363 #undef _
1364 #define _(a,b)                                          \
1365           else if (unformat(input, #a " %s", &s))       \
1366             {                                           \
1367               tmp = format (0, "-%s%c", #b, 0);         \
1368               vec_add1 (conf->eal_init_args, tmp);      \
1369               vec_add1 (s, 0);                          \
1370               vec_add1 (conf->eal_init_args, s);        \
1371             }
1372         foreach_eal_single_hyphen_arg
1373 #undef _
1374         else if (unformat (input, "default"))
1375         ;
1376
1377       else if (unformat_skip_white_space (input))
1378         ;
1379       else
1380         {
1381           error = clib_error_return (0, "unknown input `%U'",
1382                                      format_unformat_error, input);
1383           goto done;
1384         }
1385     }
1386
1387   if (!conf->uio_driver_name)
1388     conf->uio_driver_name = format (0, "auto%c", 0);
1389
1390   if (eal_no_hugetlb == 0)
1391     {
1392       vec_add1 (conf->eal_init_args, (u8 *) "--in-memory");
1393
1394       default_hugepage_sz = clib_mem_get_default_hugepage_size ();
1395
1396       /* *INDENT-OFF* */
1397       clib_bitmap_foreach (x, tm->cpu_socket_bitmap)
1398         {
1399           clib_error_t *e;
1400           uword n_pages;
1401           /* preallocate at least 16MB of hugepages per socket,
1402             if more is needed it is up to consumer to preallocate more */
1403           n_pages = round_pow2 ((uword) 16 << 20, default_hugepage_sz);
1404           n_pages /= default_hugepage_sz;
1405
1406           if ((e = clib_sysfs_prealloc_hugepages(x, 0, n_pages)))
1407             clib_error_report (e);
1408         }
1409       /* *INDENT-ON* */
1410     }
1411
1412   /* on/off dpdk's telemetry thread */
1413   if (conf->enable_telemetry == 0)
1414     {
1415       vec_add1 (conf->eal_init_args, (u8 *) "--no-telemetry");
1416     }
1417
1418   if (!file_prefix)
1419     {
1420       tmp = format (0, "--file-prefix%c", 0);
1421       vec_add1 (conf->eal_init_args, tmp);
1422       tmp = format (0, "vpp%c", 0);
1423       vec_add1 (conf->eal_init_args, tmp);
1424     }
1425
1426   if (error)
1427     return error;
1428
1429   if (no_pci == 0 && geteuid () == 0)
1430     dpdk_bind_devices_to_uio (conf);
1431
1432   if (no_vmbus == 0 && geteuid () == 0)
1433     dpdk_bind_vmbus_devices_to_uio (conf);
1434
1435 #define _(x) \
1436     if (devconf->x == 0 && conf->default_devconf.x > 0) \
1437       devconf->x = conf->default_devconf.x ;
1438
1439   pool_foreach (devconf, conf->dev_confs)  {
1440
1441     /* default per-device config items */
1442     foreach_dpdk_device_config_item
1443
1444       /* copy tso config from default device */
1445       _ (tso)
1446
1447       /* copy tso config from default device */
1448       _ (devargs)
1449
1450       /* copy rss_queues config from default device */
1451       _ (rss_queues)
1452
1453       /* add DPDK EAL whitelist/blacklist entry */
1454       if (num_whitelisted > 0 && devconf->is_blacklisted == 0 &&
1455           devconf->dev_addr_type == VNET_DEV_ADDR_PCI)
1456     {
1457           tmp = format (0, "-a%c", 0);
1458           vec_add1 (conf->eal_init_args, tmp);
1459           if (devconf->devargs)
1460           {
1461             tmp = format (0, "%U,%s%c", format_vlib_pci_addr,
1462                           &devconf->pci_addr, devconf->devargs, 0);
1463           }
1464           else
1465           {
1466             tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1467           }
1468           vec_add1 (conf->eal_init_args, tmp);
1469     }
1470     else if (num_whitelisted == 0 && devconf->is_blacklisted != 0 &&
1471              devconf->dev_addr_type == VNET_DEV_ADDR_PCI)
1472     {
1473           tmp = format (0, "-b%c", 0);
1474           vec_add1 (conf->eal_init_args, tmp);
1475           tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1476           vec_add1 (conf->eal_init_args, tmp);
1477     }
1478   }
1479
1480 #undef _
1481
1482   if (socket_mem)
1483     clib_warning ("socket-mem argument is deprecated");
1484
1485   /* NULL terminate the "argv" vector, in case of stupidity */
1486   vec_add1 (conf->eal_init_args, 0);
1487   _vec_len (conf->eal_init_args) -= 1;
1488
1489   /* Set up DPDK eal and packet mbuf pool early. */
1490
1491   int log_fds[2] = { 0 };
1492   if (pipe (log_fds) == 0)
1493     {
1494       if (fcntl (log_fds[1], F_SETFL, O_NONBLOCK) == 0)
1495         {
1496           FILE *f = fdopen (log_fds[1], "a");
1497           if (f && rte_openlog_stream (f) == 0)
1498             {
1499               clib_file_t t = { 0 };
1500               t.read_function = dpdk_log_read_ready;
1501               t.file_descriptor = log_fds[0];
1502               t.description = format (0, "DPDK logging pipe");
1503               clib_file_add (&file_main, &t);
1504             }
1505         }
1506       else
1507         {
1508           close (log_fds[0]);
1509           close (log_fds[1]);
1510         }
1511     }
1512
1513   vm = vlib_get_main ();
1514
1515   /* make copy of args as rte_eal_init tends to mess up with arg array */
1516   for (i = 1; i < vec_len (conf->eal_init_args); i++)
1517     conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1518                                       conf->eal_init_args[i]);
1519
1520   vec_terminate_c_string (conf->eal_init_args_str);
1521
1522   dpdk_log_notice ("EAL init args: %s", conf->eal_init_args_str);
1523   ret = rte_eal_init (vec_len (conf->eal_init_args),
1524                       (char **) conf->eal_init_args);
1525
1526   /* enable the AVX-512 vPMDs in DPDK */
1527   if (clib_cpu_supports_avx512_bitalg () &&
1528       conf->max_simd_bitwidth == DPDK_MAX_SIMD_BITWIDTH_DEFAULT)
1529     rte_vect_set_max_simd_bitwidth (RTE_VECT_SIMD_512);
1530   else if (conf->max_simd_bitwidth != DPDK_MAX_SIMD_BITWIDTH_DEFAULT)
1531     rte_vect_set_max_simd_bitwidth (conf->max_simd_bitwidth ==
1532                                         DPDK_MAX_SIMD_BITWIDTH_256 ?
1533                                       RTE_VECT_SIMD_256 :
1534                                       RTE_VECT_SIMD_512);
1535
1536   /* lazy umount hugepages */
1537   umount2 ((char *) huge_dir_path, MNT_DETACH);
1538   rmdir ((char *) huge_dir_path);
1539   vec_free (huge_dir_path);
1540
1541   if (ret < 0)
1542     return clib_error_return (0, "rte_eal_init returned %d", ret);
1543
1544   /* main thread 1st */
1545   if ((error = dpdk_buffer_pools_create (vm)))
1546     return error;
1547
1548 done:
1549   return error;
1550 }
1551
1552 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1553
1554 void
1555 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1556 {
1557   vnet_main_t *vnm = vnet_get_main ();
1558   struct rte_eth_link prev_link = xd->link;
1559   u32 hw_flags = 0;
1560   u8 hw_flags_chg = 0;
1561
1562   xd->time_last_link_update = now ? now : xd->time_last_link_update;
1563   clib_memset (&xd->link, 0, sizeof (xd->link));
1564   rte_eth_link_get_nowait (xd->port_id, &xd->link);
1565
1566   if (LINK_STATE_ELOGS)
1567     {
1568       ELOG_TYPE_DECLARE (e) =
1569       {
1570       .format =
1571           "update-link-state: sw_if_index %d, admin_up %d,"
1572           "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1573
1574       struct
1575       {
1576         u32 sw_if_index;
1577         u8 admin_up;
1578         u8 old_link_state;
1579         u8 new_link_state;
1580       } *ed;
1581       ed = ELOG_DATA (&vlib_global_main.elog_main, e);
1582       ed->sw_if_index = xd->sw_if_index;
1583       ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1584       ed->old_link_state = (u8)
1585         vnet_hw_interface_is_link_up (vnm, xd->hw_if_index);
1586       ed->new_link_state = (u8) xd->link.link_status;
1587     }
1588
1589   if ((xd->link.link_duplex != prev_link.link_duplex))
1590     {
1591       hw_flags_chg = 1;
1592       switch (xd->link.link_duplex)
1593         {
1594         case ETH_LINK_HALF_DUPLEX:
1595           hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1596           break;
1597         case ETH_LINK_FULL_DUPLEX:
1598           hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1599           break;
1600         default:
1601           break;
1602         }
1603     }
1604   if (xd->link.link_speed != prev_link.link_speed)
1605     vnet_hw_interface_set_link_speed (vnm, xd->hw_if_index,
1606                                       xd->link.link_speed * 1000);
1607
1608   if (xd->link.link_status != prev_link.link_status)
1609     {
1610       hw_flags_chg = 1;
1611
1612       if (xd->link.link_status)
1613         hw_flags |= VNET_HW_INTERFACE_FLAG_LINK_UP;
1614     }
1615
1616   if (hw_flags_chg)
1617     {
1618       if (LINK_STATE_ELOGS)
1619         {
1620           ELOG_TYPE_DECLARE (e) =
1621           {
1622           .format =
1623               "update-link-state: sw_if_index %d, new flags %d",.format_args
1624               = "i4i4",};
1625
1626           struct
1627           {
1628             u32 sw_if_index;
1629             u32 flags;
1630           } *ed;
1631           ed = ELOG_DATA (&vlib_global_main.elog_main, e);
1632           ed->sw_if_index = xd->sw_if_index;
1633           ed->flags = hw_flags;
1634         }
1635       vnet_hw_interface_set_flags (vnm, xd->hw_if_index, hw_flags);
1636     }
1637 }
1638
1639 static uword
1640 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1641 {
1642   clib_error_t *error;
1643   dpdk_main_t *dm = &dpdk_main;
1644   dpdk_device_t *xd;
1645   vlib_thread_main_t *tm = vlib_get_thread_main ();
1646
1647   error = dpdk_lib_init (dm);
1648
1649   if (error)
1650     clib_error_report (error);
1651
1652   if (dpdk_cryptodev_init)
1653     {
1654       error = dpdk_cryptodev_init (vm);
1655       if (error)
1656         {
1657           vlib_log_warn (dpdk_main.log_cryptodev, "%U", format_clib_error,
1658                          error);
1659           clib_error_free (error);
1660         }
1661     }
1662
1663   tm->worker_thread_release = 1;
1664
1665   f64 now = vlib_time_now (vm);
1666   vec_foreach (xd, dm->devices)
1667   {
1668     dpdk_update_link_state (xd, now);
1669   }
1670
1671   while (1)
1672     {
1673       /*
1674        * check each time through the loop in case intervals are changed
1675        */
1676       f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1677         dm->link_state_poll_interval : dm->stat_poll_interval;
1678
1679       vlib_process_wait_for_event_or_clock (vm, min_wait);
1680
1681       if (dm->admin_up_down_in_progress)
1682         /* skip the poll if an admin up down is in progress (on any interface) */
1683         continue;
1684
1685       vec_foreach (xd, dm->devices)
1686       {
1687         f64 now = vlib_time_now (vm);
1688         if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1689           dpdk_update_counters (xd, now);
1690         if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1691           dpdk_update_link_state (xd, now);
1692
1693       }
1694     }
1695
1696   return 0;
1697 }
1698
1699 /* *INDENT-OFF* */
1700 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1701     .function = dpdk_process,
1702     .type = VLIB_NODE_TYPE_PROCESS,
1703     .name = "dpdk-process",
1704     .process_log2_n_stack_bytes = 17,
1705 };
1706 /* *INDENT-ON* */
1707
1708 static clib_error_t *
1709 dpdk_init (vlib_main_t * vm)
1710 {
1711   dpdk_main_t *dm = &dpdk_main;
1712   clib_error_t *error = 0;
1713
1714   /* verify that structs are cacheline aligned */
1715   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1716                  "Cache line marker must be 1st element in dpdk_device_t");
1717   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1718                  CLIB_CACHE_LINE_BYTES,
1719                  "Data in cache line 0 is bigger than cache line size");
1720   STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1721                  "Cache line marker must be 1st element in frame_queue_trace_t");
1722
1723   dpdk_cli_reference ();
1724
1725   dm->conf = &dpdk_config_main;
1726
1727   vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1728
1729   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1730   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1731
1732   dm->log_default = vlib_log_register_class ("dpdk", 0);
1733   dm->log_cryptodev = vlib_log_register_class ("dpdk", "cryptodev");
1734
1735   return error;
1736 }
1737
1738 VLIB_INIT_FUNCTION (dpdk_init);
1739
1740 static clib_error_t *
1741 dpdk_worker_thread_init (vlib_main_t *vm)
1742 {
1743   if (rte_thread_register () < 0)
1744     clib_panic ("dpdk: cannot register thread %u - %s", vm->thread_index,
1745                 rte_strerror (rte_errno));
1746   return 0;
1747 }
1748
1749 VLIB_WORKER_INIT_FUNCTION (dpdk_worker_thread_init);