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