interface: rx queue infra rework, part one
[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 = hash_get (dm->conf->device_config_index_by_vmbus_addr,
295                             vmbus_addr.as_u32[0]);
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 = hash_get (conf->device_config_index_by_vmbus_addr,
1066                                addr->as_u32[0]);
1067           if (!p)
1068             {
1069               /* No devices blacklisted, but have whitelisted. blacklist all
1070                * non-whitelisted */
1071               pool_get (conf->dev_confs, devconf);
1072               hash_set (conf->device_config_index_by_vmbus_addr,
1073                         addr->as_u32[0], devconf - conf->dev_confs);
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           u32 vmbus_as_u32 = conf->blacklist_by_vmbus_addr[i];
1088           if (vmbus_as_u32 == addr->as_u32[0])
1089             {
1090               if (devconf == 0)
1091                 {
1092                   /* Device not whitelisted */
1093                   pool_get (conf->dev_confs, devconf);
1094                   hash_set (conf->device_config_index_by_vmbus_addr,
1095                             addr->as_u32[0], devconf - conf->dev_confs);
1096                   devconf->vmbus_addr = *addr;
1097                   devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
1098                   devconf->is_blacklisted = 1;
1099                   goto skipped_vmbus;
1100                 }
1101               else
1102                 {
1103                   break;
1104                 }
1105             }
1106         }
1107
1108       error = vlib_vmbus_bind_to_uio (addr);
1109       if (error)
1110         {
1111           if (devconf == 0)
1112             {
1113               pool_get (conf->dev_confs, devconf);
1114               hash_set (conf->device_config_index_by_vmbus_addr,
1115                         addr->as_u32[0], devconf - conf->dev_confs);
1116               devconf->vmbus_addr = *addr;
1117             }
1118           devconf->dev_addr_type = VNET_DEV_ADDR_VMBUS;
1119           devconf->is_blacklisted = 1;
1120           clib_error_report (error);
1121         }
1122     }
1123   /* *INDENT-ON* */
1124 }
1125
1126 static clib_error_t *
1127 dpdk_device_config (dpdk_config_main_t *conf, void *addr,
1128                     dpdk_device_addr_type_t addr_type, unformat_input_t *input,
1129                     u8 is_default)
1130 {
1131   clib_error_t *error = 0;
1132   uword *p;
1133   dpdk_device_config_t *devconf = 0;
1134   unformat_input_t sub_input;
1135
1136   if (is_default)
1137     {
1138       devconf = &conf->default_devconf;
1139     }
1140   else if (addr_type == VNET_DEV_ADDR_PCI)
1141     {
1142       p = hash_get (conf->device_config_index_by_pci_addr,
1143                     ((vlib_pci_addr_t *) (addr))->as_u32);
1144
1145       if (!p)
1146         {
1147           pool_get (conf->dev_confs, devconf);
1148           hash_set (conf->device_config_index_by_pci_addr,
1149                     ((vlib_pci_addr_t *) (addr))->as_u32,
1150                     devconf - conf->dev_confs);
1151         }
1152       else
1153         return clib_error_return (0,
1154                                   "duplicate configuration for PCI address %U",
1155                                   format_vlib_pci_addr, addr);
1156     }
1157   else if (addr_type == VNET_DEV_ADDR_VMBUS)
1158     {
1159       p = hash_get (conf->device_config_index_by_vmbus_addr,
1160                     ((vlib_vmbus_addr_t *) (addr))->as_u32[0]);
1161
1162       if (!p)
1163         {
1164           pool_get (conf->dev_confs, devconf);
1165           hash_set (conf->device_config_index_by_vmbus_addr,
1166                     ((vlib_vmbus_addr_t *) (addr))->as_u32[0],
1167                     devconf - conf->dev_confs);
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   conf->device_config_index_by_vmbus_addr = hash_create (0, sizeof (uword));
1314
1315   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1316     {
1317       /* Prime the pump */
1318       if (unformat (input, "no-hugetlb"))
1319         {
1320           vec_add1 (conf->eal_init_args, (u8 *) "--no-huge");
1321           eal_no_hugetlb = 1;
1322         }
1323       else if (unformat (input, "telemetry"))
1324         conf->enable_telemetry = 1;
1325
1326       else if (unformat (input, "enable-tcp-udp-checksum"))
1327         conf->enable_tcp_udp_checksum = 1;
1328
1329       else if (unformat (input, "no-tx-checksum-offload"))
1330         conf->no_tx_checksum_offload = 1;
1331
1332       else if (unformat (input, "decimal-interface-names"))
1333         conf->interface_name_format_decimal = 1;
1334
1335       else if (unformat (input, "no-multi-seg"))
1336         conf->no_multi_seg = 1;
1337
1338       else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1339                          &sub_input))
1340         {
1341           error =
1342             dpdk_device_config (conf, 0, VNET_DEV_ADDR_ANY, &sub_input, 1);
1343
1344           if (error)
1345             return error;
1346         }
1347       else
1348         if (unformat
1349             (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1350              unformat_vlib_cli_sub_input, &sub_input))
1351         {
1352           error = dpdk_device_config (conf, &pci_addr, VNET_DEV_ADDR_PCI,
1353                                       &sub_input, 0);
1354
1355           if (error)
1356             return error;
1357
1358           num_whitelisted++;
1359         }
1360       else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1361         {
1362           error =
1363             dpdk_device_config (conf, &pci_addr, VNET_DEV_ADDR_PCI, 0, 0);
1364
1365           if (error)
1366             return error;
1367
1368           num_whitelisted++;
1369         }
1370       else if (unformat (input, "dev %U %U", unformat_vlib_vmbus_addr,
1371                          &vmbus_addr, unformat_vlib_cli_sub_input, &sub_input))
1372         {
1373           error = dpdk_device_config (conf, &vmbus_addr, VNET_DEV_ADDR_VMBUS,
1374                                       &sub_input, 0);
1375
1376           if (error)
1377             return error;
1378
1379           num_whitelisted++;
1380         }
1381       else if (unformat (input, "dev %U", unformat_vlib_vmbus_addr,
1382                          &vmbus_addr))
1383         {
1384           error =
1385             dpdk_device_config (conf, &vmbus_addr, VNET_DEV_ADDR_VMBUS, 0, 0);
1386
1387           if (error)
1388             return error;
1389
1390           num_whitelisted++;
1391         }
1392       else if (unformat (input, "num-mem-channels %d", &conf->nchannels))
1393         conf->nchannels_set_manually = 0;
1394       else if (unformat (input, "num-crypto-mbufs %d",
1395                          &conf->num_crypto_mbufs))
1396         ;
1397       else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1398         ;
1399       else if (unformat (input, "socket-mem %s", &socket_mem))
1400         ;
1401       else if (unformat (input, "no-pci"))
1402         {
1403           no_pci = 1;
1404           tmp = format (0, "--no-pci%c", 0);
1405           vec_add1 (conf->eal_init_args, tmp);
1406         }
1407       else if (unformat (input, "blacklist %U", unformat_vlib_vmbus_addr,
1408                          &vmbus_addr))
1409         {
1410           vec_add1 (conf->blacklist_by_vmbus_addr, vmbus_addr.as_u32[0]);
1411         }
1412       else
1413         if (unformat
1414             (input, "blacklist %x:%x:%x.%x", &domain, &bus, &device, &func))
1415         {
1416           tmp = format (0, "-b%c", 0);
1417           vec_add1 (conf->eal_init_args, tmp);
1418           tmp =
1419             format (0, "%04x:%02x:%02x.%x%c", domain, bus, device, func, 0);
1420           vec_add1 (conf->eal_init_args, tmp);
1421         }
1422       else if (unformat (input, "blacklist %x:%x", &vendor, &device))
1423         {
1424           u32 blacklist_entry;
1425           if (vendor > 0xFFFF)
1426             return clib_error_return (0, "blacklist PCI vendor out of range");
1427           if (device > 0xFFFF)
1428             return clib_error_return (0, "blacklist PCI device out of range");
1429           blacklist_entry = (vendor << 16) | (device & 0xffff);
1430           vec_add1 (conf->blacklist_by_pci_vendor_and_device,
1431                     blacklist_entry);
1432         }
1433       else if (unformat (input, "no-vmbus"))
1434         {
1435           no_vmbus = 1;
1436           tmp = format (0, "--no-vmbus%c", 0);
1437           vec_add1 (conf->eal_init_args, tmp);
1438         }
1439
1440 #define _(a)                                    \
1441       else if (unformat(input, #a))             \
1442         {                                       \
1443           tmp = format (0, "--%s%c", #a, 0);    \
1444           vec_add1 (conf->eal_init_args, tmp);    \
1445         }
1446       foreach_eal_double_hyphen_predicate_arg
1447 #undef _
1448 #define _(a)                                          \
1449         else if (unformat(input, #a " %s", &s))       \
1450           {                                           \
1451             if (!strncmp(#a, "file-prefix", 11)) \
1452               file_prefix = 1;                        \
1453             tmp = format (0, "--%s%c", #a, 0);        \
1454             vec_add1 (conf->eal_init_args, tmp);      \
1455             vec_add1 (s, 0);                          \
1456             if (!strncmp(#a, "vdev", 4))              \
1457               if (strstr((char*)s, "af_packet"))      \
1458                 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
1459             vec_add1 (conf->eal_init_args, s);        \
1460           }
1461         foreach_eal_double_hyphen_arg
1462 #undef _
1463 #define _(a,b)                                          \
1464           else if (unformat(input, #a " %s", &s))       \
1465             {                                           \
1466               tmp = format (0, "-%s%c", #b, 0);         \
1467               vec_add1 (conf->eal_init_args, tmp);      \
1468               vec_add1 (s, 0);                          \
1469               vec_add1 (conf->eal_init_args, s);        \
1470             }
1471         foreach_eal_single_hyphen_arg
1472 #undef _
1473 #define _(a,b)                                          \
1474             else if (unformat(input, #a " %s", &s))     \
1475               {                                         \
1476                 tmp = format (0, "-%s%c", #b, 0);       \
1477                 vec_add1 (conf->eal_init_args, tmp);    \
1478                 vec_add1 (s, 0);                        \
1479                 vec_add1 (conf->eal_init_args, s);      \
1480                 conf->a##_set_manually = 1;             \
1481               }
1482         foreach_eal_single_hyphen_mandatory_arg
1483 #undef _
1484         else if (unformat (input, "default"))
1485         ;
1486
1487       else if (unformat_skip_white_space (input))
1488         ;
1489       else
1490         {
1491           error = clib_error_return (0, "unknown input `%U'",
1492                                      format_unformat_error, input);
1493           goto done;
1494         }
1495     }
1496
1497   if (!conf->uio_driver_name)
1498     conf->uio_driver_name = format (0, "auto%c", 0);
1499
1500   if (eal_no_hugetlb == 0)
1501     {
1502       vec_add1 (conf->eal_init_args, (u8 *) "--in-memory");
1503
1504       default_hugepage_sz = clib_mem_get_default_hugepage_size ();
1505
1506       /* *INDENT-OFF* */
1507       clib_bitmap_foreach (x, tm->cpu_socket_bitmap)
1508         {
1509           clib_error_t *e;
1510           uword n_pages;
1511           /* preallocate at least 16MB of hugepages per socket,
1512             if more is needed it is up to consumer to preallocate more */
1513           n_pages = round_pow2 ((uword) 16 << 20, default_hugepage_sz);
1514           n_pages /= default_hugepage_sz;
1515
1516           if ((e = clib_sysfs_prealloc_hugepages(x, 0, n_pages)))
1517             clib_error_report (e);
1518         }
1519       /* *INDENT-ON* */
1520     }
1521
1522   /* on/off dpdk's telemetry thread */
1523   if (conf->enable_telemetry == 0)
1524     {
1525       vec_add1 (conf->eal_init_args, (u8 *) "--no-telemetry");
1526     }
1527
1528   if (!file_prefix)
1529     {
1530       tmp = format (0, "--file-prefix%c", 0);
1531       vec_add1 (conf->eal_init_args, tmp);
1532       tmp = format (0, "vpp%c", 0);
1533       vec_add1 (conf->eal_init_args, tmp);
1534     }
1535
1536   if (error)
1537     return error;
1538
1539   /* I'll bet that -c and -n must be the first and second args... */
1540   if (!conf->coremask_set_manually)
1541     {
1542       vlib_thread_registration_t *tr;
1543       uword *coremask = 0;
1544       int i;
1545
1546       /* main thread core */
1547       coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
1548
1549       for (i = 0; i < vec_len (tm->registrations); i++)
1550         {
1551           tr = tm->registrations[i];
1552           coremask = clib_bitmap_or (coremask, tr->coremask);
1553         }
1554
1555       vec_insert (conf->eal_init_args, 2, 1);
1556       conf->eal_init_args[1] = (u8 *) "-c";
1557       tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1558       conf->eal_init_args[2] = tmp;
1559       clib_bitmap_free (coremask);
1560     }
1561
1562   if (!conf->nchannels_set_manually)
1563     {
1564       vec_insert (conf->eal_init_args, 2, 3);
1565       conf->eal_init_args[3] = (u8 *) "-n";
1566       tmp = format (0, "%d", conf->nchannels);
1567       vec_terminate_c_string (tmp);
1568       conf->eal_init_args[4] = tmp;
1569     }
1570
1571   if (no_pci == 0 && geteuid () == 0)
1572     dpdk_bind_devices_to_uio (conf);
1573
1574   if (no_vmbus == 0 && geteuid () == 0)
1575     dpdk_bind_vmbus_devices_to_uio (conf);
1576
1577 #define _(x) \
1578     if (devconf->x == 0 && conf->default_devconf.x > 0) \
1579       devconf->x = conf->default_devconf.x ;
1580
1581   /* *INDENT-OFF* */
1582   pool_foreach (devconf, conf->dev_confs)  {
1583
1584     /* default per-device config items */
1585     foreach_dpdk_device_config_item
1586
1587       /* copy vlan_strip config from default device */
1588       _ (vlan_strip_offload)
1589
1590       /* copy tso config from default device */
1591       _ (tso)
1592
1593       /* copy tso config from default device */
1594       _ (devargs)
1595
1596       /* copy rss_queues config from default device */
1597       _ (rss_queues)
1598
1599       /* add DPDK EAL whitelist/blacklist entry */
1600       if (num_whitelisted > 0 && devconf->is_blacklisted == 0 &&
1601           devconf->dev_addr_type == VNET_DEV_ADDR_PCI)
1602     {
1603           tmp = format (0, "-a%c", 0);
1604           vec_add1 (conf->eal_init_args, tmp);
1605           if (devconf->devargs)
1606           {
1607             tmp = format (0, "%U,%s%c", format_vlib_pci_addr,
1608                           &devconf->pci_addr, devconf->devargs, 0);
1609           }
1610           else
1611           {
1612             tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1613           }
1614           vec_add1 (conf->eal_init_args, tmp);
1615     }
1616     else if (num_whitelisted == 0 && devconf->is_blacklisted != 0 &&
1617              devconf->dev_addr_type == VNET_DEV_ADDR_PCI)
1618     {
1619           tmp = format (0, "-b%c", 0);
1620           vec_add1 (conf->eal_init_args, tmp);
1621           tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1622           vec_add1 (conf->eal_init_args, tmp);
1623     }
1624   }
1625   /* *INDENT-ON* */
1626
1627 #undef _
1628
1629   /* set master-lcore */
1630   tmp = format (0, "--main-lcore%c", 0);
1631   vec_add1 (conf->eal_init_args, tmp);
1632   tmp = format (0, "%u%c", tm->main_lcore, 0);
1633   vec_add1 (conf->eal_init_args, tmp);
1634
1635
1636   if (socket_mem)
1637     clib_warning ("socket-mem argument is deprecated");
1638
1639   /* NULL terminate the "argv" vector, in case of stupidity */
1640   vec_add1 (conf->eal_init_args, 0);
1641   _vec_len (conf->eal_init_args) -= 1;
1642
1643   /* Set up DPDK eal and packet mbuf pool early. */
1644
1645   int log_fds[2] = { 0 };
1646   if (pipe (log_fds) == 0)
1647     {
1648       if (fcntl (log_fds[1], F_SETFL, O_NONBLOCK) == 0)
1649         {
1650           FILE *f = fdopen (log_fds[1], "a");
1651           if (f && rte_openlog_stream (f) == 0)
1652             {
1653               clib_file_t t = { 0 };
1654               t.read_function = dpdk_log_read_ready;
1655               t.file_descriptor = log_fds[0];
1656               t.description = format (0, "DPDK logging pipe");
1657               clib_file_add (&file_main, &t);
1658             }
1659         }
1660       else
1661         {
1662           close (log_fds[0]);
1663           close (log_fds[1]);
1664         }
1665     }
1666
1667   vm = vlib_get_main ();
1668
1669   /* make copy of args as rte_eal_init tends to mess up with arg array */
1670   for (i = 1; i < vec_len (conf->eal_init_args); i++)
1671     conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1672                                       conf->eal_init_args[i]);
1673
1674   vec_terminate_c_string (conf->eal_init_args_str);
1675
1676   dpdk_log_notice ("EAL init args: %s", conf->eal_init_args_str);
1677   ret = rte_eal_init (vec_len (conf->eal_init_args),
1678                       (char **) conf->eal_init_args);
1679
1680   /* lazy umount hugepages */
1681   umount2 ((char *) huge_dir_path, MNT_DETACH);
1682   rmdir ((char *) huge_dir_path);
1683   vec_free (huge_dir_path);
1684
1685   if (ret < 0)
1686     return clib_error_return (0, "rte_eal_init returned %d", ret);
1687
1688   /* main thread 1st */
1689   if ((error = dpdk_buffer_pools_create (vm)))
1690     return error;
1691
1692 done:
1693   return error;
1694 }
1695
1696 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1697
1698 void
1699 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1700 {
1701   vnet_main_t *vnm = vnet_get_main ();
1702   struct rte_eth_link prev_link = xd->link;
1703   u32 hw_flags = 0;
1704   u8 hw_flags_chg = 0;
1705
1706   /* only update link state for PMD interfaces */
1707   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1708     return;
1709
1710   xd->time_last_link_update = now ? now : xd->time_last_link_update;
1711   clib_memset (&xd->link, 0, sizeof (xd->link));
1712   rte_eth_link_get_nowait (xd->port_id, &xd->link);
1713
1714   if (LINK_STATE_ELOGS)
1715     {
1716       vlib_main_t *vm = vlib_get_main ();
1717       ELOG_TYPE_DECLARE (e) =
1718       {
1719       .format =
1720           "update-link-state: sw_if_index %d, admin_up %d,"
1721           "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1722
1723       struct
1724       {
1725         u32 sw_if_index;
1726         u8 admin_up;
1727         u8 old_link_state;
1728         u8 new_link_state;
1729       } *ed;
1730       ed = ELOG_DATA (&vm->elog_main, e);
1731       ed->sw_if_index = xd->sw_if_index;
1732       ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1733       ed->old_link_state = (u8)
1734         vnet_hw_interface_is_link_up (vnm, xd->hw_if_index);
1735       ed->new_link_state = (u8) xd->link.link_status;
1736     }
1737
1738   if ((xd->link.link_duplex != prev_link.link_duplex))
1739     {
1740       hw_flags_chg = 1;
1741       switch (xd->link.link_duplex)
1742         {
1743         case ETH_LINK_HALF_DUPLEX:
1744           hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1745           break;
1746         case ETH_LINK_FULL_DUPLEX:
1747           hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1748           break;
1749         default:
1750           break;
1751         }
1752     }
1753   if (xd->link.link_speed != prev_link.link_speed)
1754     vnet_hw_interface_set_link_speed (vnm, xd->hw_if_index,
1755                                       xd->link.link_speed * 1000);
1756
1757   if (xd->link.link_status != prev_link.link_status)
1758     {
1759       hw_flags_chg = 1;
1760
1761       if (xd->link.link_status)
1762         hw_flags |= VNET_HW_INTERFACE_FLAG_LINK_UP;
1763     }
1764
1765   if (hw_flags_chg)
1766     {
1767       if (LINK_STATE_ELOGS)
1768         {
1769           vlib_main_t *vm = vlib_get_main ();
1770
1771           ELOG_TYPE_DECLARE (e) =
1772           {
1773           .format =
1774               "update-link-state: sw_if_index %d, new flags %d",.format_args
1775               = "i4i4",};
1776
1777           struct
1778           {
1779             u32 sw_if_index;
1780             u32 flags;
1781           } *ed;
1782           ed = ELOG_DATA (&vm->elog_main, e);
1783           ed->sw_if_index = xd->sw_if_index;
1784           ed->flags = hw_flags;
1785         }
1786       vnet_hw_interface_set_flags (vnm, xd->hw_if_index, hw_flags);
1787     }
1788 }
1789
1790 static uword
1791 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1792 {
1793   clib_error_t *error;
1794   dpdk_main_t *dm = &dpdk_main;
1795   dpdk_device_t *xd;
1796   vlib_thread_main_t *tm = vlib_get_thread_main ();
1797
1798   error = dpdk_lib_init (dm);
1799
1800   if (error)
1801     clib_error_report (error);
1802
1803   error = dpdk_cryptodev_init (vm);
1804   if (error)
1805     {
1806       vlib_log_warn (dpdk_main.log_cryptodev, "%U", format_clib_error, error);
1807       clib_error_free (error);
1808     }
1809
1810   tm->worker_thread_release = 1;
1811
1812   f64 now = vlib_time_now (vm);
1813   vec_foreach (xd, dm->devices)
1814   {
1815     dpdk_update_link_state (xd, now);
1816   }
1817
1818   while (1)
1819     {
1820       /*
1821        * check each time through the loop in case intervals are changed
1822        */
1823       f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1824         dm->link_state_poll_interval : dm->stat_poll_interval;
1825
1826       vlib_process_wait_for_event_or_clock (vm, min_wait);
1827
1828       if (dm->admin_up_down_in_progress)
1829         /* skip the poll if an admin up down is in progress (on any interface) */
1830         continue;
1831
1832       vec_foreach (xd, dm->devices)
1833       {
1834         f64 now = vlib_time_now (vm);
1835         if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1836           dpdk_update_counters (xd, now);
1837         if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1838           dpdk_update_link_state (xd, now);
1839
1840       }
1841     }
1842
1843   return 0;
1844 }
1845
1846 /* *INDENT-OFF* */
1847 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1848     .function = dpdk_process,
1849     .type = VLIB_NODE_TYPE_PROCESS,
1850     .name = "dpdk-process",
1851     .process_log2_n_stack_bytes = 17,
1852 };
1853 /* *INDENT-ON* */
1854
1855 static clib_error_t *
1856 dpdk_init (vlib_main_t * vm)
1857 {
1858   dpdk_main_t *dm = &dpdk_main;
1859   clib_error_t *error = 0;
1860
1861   /* verify that structs are cacheline aligned */
1862   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1863                  "Cache line marker must be 1st element in dpdk_device_t");
1864   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1865                  CLIB_CACHE_LINE_BYTES,
1866                  "Data in cache line 0 is bigger than cache line size");
1867   STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1868                  "Cache line marker must be 1st element in frame_queue_trace_t");
1869   STATIC_ASSERT (RTE_CACHE_LINE_SIZE == 1 << CLIB_LOG2_CACHE_LINE_BYTES,
1870                  "DPDK RTE CACHE LINE SIZE does not match with 1<<CLIB_LOG2_CACHE_LINE_BYTES");
1871
1872   dpdk_cli_reference ();
1873
1874   dm->vlib_main = vm;
1875   dm->vnet_main = vnet_get_main ();
1876   dm->conf = &dpdk_config_main;
1877
1878   dm->conf->nchannels = 4;
1879   vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1880
1881   /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1882   dm->buffer_flags_template = (VLIB_BUFFER_TOTAL_LENGTH_VALID |
1883                                VLIB_BUFFER_EXT_HDR_VALID |
1884                                VNET_BUFFER_F_L4_CHECKSUM_COMPUTED |
1885                                VNET_BUFFER_F_L4_CHECKSUM_CORRECT);
1886
1887   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1888   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1889
1890   dm->log_default = vlib_log_register_class ("dpdk", 0);
1891   dm->log_cryptodev = vlib_log_register_class ("dpdk", "cryptodev");
1892   dm->log_ipsec = vlib_log_register_class ("dpdk", "ipsec");
1893
1894   return error;
1895 }
1896
1897 VLIB_INIT_FUNCTION (dpdk_init);
1898
1899 /*
1900  * fd.io coding-style-patch-verification: ON
1901  *
1902  * Local Variables:
1903  * eval: (c-set-style "gnu")
1904  * End:
1905  */