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