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