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