dpdk: use speed_capa to detect i40e device speed
[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
21 #include <vnet/ethernet/ethernet.h>
22 #include <dpdk/device/dpdk.h>
23 #include <vlib/unix/physmem.h>
24 #include <vlib/pci/pci.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/mount.h>
31 #include <string.h>
32 #include <fcntl.h>
33
34 #include <dpdk/device/dpdk_priv.h>
35
36 dpdk_main_t dpdk_main;
37
38 #define LINK_STATE_ELOGS        0
39
40 #define DEFAULT_HUGE_DIR "/run/vpp/hugepages"
41 #define VPP_RUN_DIR "/run/vpp"
42
43 /* Port configuration, mildly modified Intel app values */
44
45 static struct rte_eth_conf port_conf_template = {
46   .rxmode = {
47              .split_hdr_size = 0,
48              .header_split = 0,         /**< Header Split disabled */
49              .hw_ip_checksum = 0,       /**< IP checksum offload disabled */
50              .hw_vlan_filter = 0,       /**< VLAN filtering disabled */
51              .hw_strip_crc = 0,         /**< CRC stripped by hardware */
52              },
53   .txmode = {
54              .mq_mode = ETH_MQ_TX_NONE,
55              },
56 };
57
58 clib_error_t *
59 dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd)
60 {
61   int rv;
62   int j;
63
64   ASSERT (vlib_get_thread_index () == 0);
65
66   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
67     {
68       vnet_hw_interface_set_flags (dm->vnet_main, xd->hw_if_index, 0);
69       rte_eth_dev_stop (xd->device_index);
70     }
71
72   rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
73                               xd->tx_q_used, &xd->port_conf);
74
75   if (rv < 0)
76     return clib_error_return (0, "rte_eth_dev_configure[%d]: err %d",
77                               xd->device_index, rv);
78
79   /* Set up one TX-queue per worker thread */
80   for (j = 0; j < xd->tx_q_used; j++)
81     {
82       rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
83                                    xd->cpu_socket, &xd->tx_conf);
84
85       /* retry with any other CPU socket */
86       if (rv < 0)
87         rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
88                                      SOCKET_ID_ANY, &xd->tx_conf);
89       if (rv < 0)
90         break;
91     }
92
93   if (rv < 0)
94     return clib_error_return (0, "rte_eth_tx_queue_setup[%d]: err %d",
95                               xd->device_index, rv);
96
97   for (j = 0; j < xd->rx_q_used; j++)
98     {
99       uword tidx = vnet_get_device_input_thread_index (dm->vnet_main,
100                                                        xd->hw_if_index, j);
101       unsigned lcore = vlib_worker_threads[tidx].lcore_id;
102       u16 socket_id = rte_lcore_to_socket_id (lcore);
103
104       rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
105                                    xd->cpu_socket, 0,
106                                    dm->pktmbuf_pools[socket_id]);
107
108       /* retry with any other CPU socket */
109       if (rv < 0)
110         rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
111                                      SOCKET_ID_ANY, 0,
112                                      dm->pktmbuf_pools[socket_id]);
113       if (rv < 0)
114         return clib_error_return (0, "rte_eth_rx_queue_setup[%d]: err %d",
115                                   xd->device_index, rv);
116     }
117
118   if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
119     {
120       int rv;
121       rv = rte_eth_dev_start (xd->device_index);
122       if (!rv && xd->default_mac_address)
123         rv = rte_eth_dev_default_mac_addr_set (xd->device_index,
124                                                (struct ether_addr *)
125                                                xd->default_mac_address);
126       if (rv < 0)
127         clib_warning ("rte_eth_dev_start %d returned %d",
128                       xd->device_index, rv);
129     }
130   return 0;
131 }
132
133 static u32
134 dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
135 {
136   dpdk_main_t *dm = &dpdk_main;
137   dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
138   u32 old = 0;
139
140   if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags))
141     {
142       old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
143
144       if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
145         xd->flags |= DPDK_DEVICE_FLAG_PROMISC;
146       else
147         xd->flags &= ~DPDK_DEVICE_FLAG_PROMISC;
148
149       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
150         {
151           if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
152             rte_eth_promiscuous_enable (xd->device_index);
153           else
154             rte_eth_promiscuous_disable (xd->device_index);
155         }
156     }
157   else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU (flags))
158     {
159       int rv;
160
161       xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
162
163       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
164         rte_eth_dev_stop (xd->device_index);
165
166       rv = rte_eth_dev_configure
167         (xd->device_index, xd->rx_q_used, xd->tx_q_used, &xd->port_conf);
168
169       if (rv < 0)
170         vlib_cli_output (vlib_get_main (),
171                          "rte_eth_dev_configure[%d]: err %d",
172                          xd->device_index, rv);
173
174       rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
175
176       if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
177         {
178           int rv = rte_eth_dev_start (xd->device_index);
179           if (!rv && xd->default_mac_address)
180             rv = rte_eth_dev_default_mac_addr_set (xd->device_index,
181                                                    (struct ether_addr *)
182                                                    xd->default_mac_address);
183           if (rv < 0)
184             clib_warning ("rte_eth_dev_start %d returned %d",
185                           xd->device_index, rv);
186         }
187
188     }
189   return old;
190 }
191
192 static void
193 dpdk_device_lock_init (dpdk_device_t * xd)
194 {
195   int q;
196   vec_validate (xd->lockp, xd->tx_q_used - 1);
197   for (q = 0; q < xd->tx_q_used; q++)
198     {
199       xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
200                                              CLIB_CACHE_LINE_BYTES);
201       memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
202     }
203 }
204
205 static clib_error_t *
206 dpdk_lib_init (dpdk_main_t * dm)
207 {
208   u32 nports;
209   u32 nb_desc = 0;
210   int i;
211   clib_error_t *error;
212   vlib_main_t *vm = vlib_get_main ();
213   vlib_thread_main_t *tm = vlib_get_thread_main ();
214   vnet_device_main_t *vdm = &vnet_device_main;
215   vnet_sw_interface_t *sw;
216   vnet_hw_interface_t *hi;
217   dpdk_device_t *xd;
218   vlib_pci_addr_t last_pci_addr;
219   u32 last_pci_addr_port = 0;
220   vlib_thread_registration_t *tr_hqos;
221   uword *p_hqos;
222
223   u32 next_hqos_cpu = 0;
224   u8 af_packet_port_id = 0;
225   u8 bond_ether_port_id = 0;
226   last_pci_addr.as_u32 = ~0;
227
228   dm->hqos_cpu_first_index = 0;
229   dm->hqos_cpu_count = 0;
230
231   /* find out which cpus will be used for I/O TX */
232   p_hqos = hash_get_mem (tm->thread_registrations_by_name, "hqos-threads");
233   tr_hqos = p_hqos ? (vlib_thread_registration_t *) p_hqos[0] : 0;
234
235   if (tr_hqos && tr_hqos->count > 0)
236     {
237       dm->hqos_cpu_first_index = tr_hqos->first_index;
238       dm->hqos_cpu_count = tr_hqos->count;
239     }
240
241   vec_validate_aligned (dm->devices_by_hqos_cpu, tm->n_vlib_mains - 1,
242                         CLIB_CACHE_LINE_BYTES);
243
244   nports = rte_eth_dev_count ();
245   if (nports < 1)
246     {
247       clib_warning ("DPDK drivers found no ports...");
248     }
249
250   if (CLIB_DEBUG > 0)
251     clib_warning ("DPDK drivers found %d ports...", nports);
252
253   /*
254    * All buffers are all allocated from the same rte_mempool.
255    * Thus they all have the same number of data bytes.
256    */
257   dm->vlib_buffer_free_list_index =
258     vlib_buffer_get_or_create_free_list (vm,
259                                          VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
260                                          "dpdk rx");
261
262   if (dm->conf->enable_tcp_udp_checksum)
263     dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
264                                    | IP_BUFFER_L4_CHECKSUM_COMPUTED);
265
266   /* vlib_buffer_t template */
267   vec_validate_aligned (dm->buffer_templates, tm->n_vlib_mains - 1,
268                         CLIB_CACHE_LINE_BYTES);
269   for (i = 0; i < tm->n_vlib_mains; i++)
270     {
271       vlib_buffer_free_list_t *fl;
272       vlib_buffer_t *bt = vec_elt_at_index (dm->buffer_templates, i);
273       fl = vlib_buffer_get_free_list (vm,
274                                       VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
275       vlib_buffer_init_for_free_list (bt, fl);
276       bt->flags = dm->buffer_flags_template;
277       bt->current_data = -RTE_PKTMBUF_HEADROOM;
278       vnet_buffer (bt)->sw_if_index[VLIB_TX] = (u32) ~ 0;
279     }
280
281   for (i = 0; i < nports; i++)
282     {
283       u8 addr[6];
284       u8 vlan_strip = 0;
285       int j;
286       struct rte_eth_dev_info dev_info;
287       clib_error_t *rv;
288       struct rte_eth_link l;
289       dpdk_device_config_t *devconf = 0;
290       vlib_pci_addr_t pci_addr;
291       uword *p = 0;
292
293       rte_eth_dev_info_get (i, &dev_info);
294       if (dev_info.pci_dev)     /* bonded interface has no pci info */
295         {
296           pci_addr.domain = dev_info.pci_dev->addr.domain;
297           pci_addr.bus = dev_info.pci_dev->addr.bus;
298           pci_addr.slot = dev_info.pci_dev->addr.devid;
299           pci_addr.function = dev_info.pci_dev->addr.function;
300           p =
301             hash_get (dm->conf->device_config_index_by_pci_addr,
302                       pci_addr.as_u32);
303         }
304
305       if (p)
306         devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
307       else
308         devconf = &dm->conf->default_devconf;
309
310       /* Create vnet interface */
311       vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
312       xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
313       xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
314       xd->cpu_socket = (i8) rte_eth_dev_socket_id (i);
315
316       /* Handle interface naming for devices with multiple ports sharing same PCI ID */
317       if (dev_info.pci_dev)
318         {
319           struct rte_eth_dev_info di = { 0 };
320           rte_eth_dev_info_get (i + 1, &di);
321           if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
322               memcmp (&dev_info.pci_dev->addr, &di.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       if (dm->conf->no_multi_seg)
345         {
346           xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
347           port_conf_template.rxmode.jumbo_frame = 0;
348           port_conf_template.rxmode.enable_scatter = 0;
349         }
350       else
351         {
352           xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
353           port_conf_template.rxmode.jumbo_frame = 1;
354           port_conf_template.rxmode.enable_scatter = 1;
355           xd->flags |= DPDK_DEVICE_FLAG_MAYBE_MULTISEG;
356         }
357
358       clib_memcpy (&xd->port_conf, &port_conf_template,
359                    sizeof (struct rte_eth_conf));
360
361       xd->tx_q_used = clib_min (dev_info.max_tx_queues, tm->n_vlib_mains);
362
363       if (devconf->num_tx_queues > 0
364           && devconf->num_tx_queues < xd->tx_q_used)
365         xd->tx_q_used = clib_min (xd->tx_q_used, devconf->num_tx_queues);
366
367       if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
368         {
369           dm->use_rss = 1;
370         }
371
372       if (devconf->num_rx_queues > 1
373           && dev_info.max_rx_queues >= devconf->num_rx_queues)
374         {
375           xd->rx_q_used = devconf->num_rx_queues;
376           xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
377           if (devconf->rss_fn == 0)
378             xd->port_conf.rx_adv_conf.rss_conf.rss_hf =
379               ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
380           else
381             xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
382         }
383       else
384         xd->rx_q_used = 1;
385
386       xd->flags |= DPDK_DEVICE_FLAG_PMD;
387
388       /* workaround for drivers not setting driver_name */
389       if ((!dev_info.driver_name) && (dev_info.pci_dev))
390         dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
391
392       ASSERT (dev_info.driver_name);
393
394       if (!xd->pmd)
395         {
396
397
398 #define _(s,f) else if (dev_info.driver_name &&                 \
399                         !strcmp(dev_info.driver_name, s))       \
400                  xd->pmd = VNET_DPDK_PMD_##f;
401           if (0)
402             ;
403           foreach_dpdk_pmd
404 #undef _
405             else
406             xd->pmd = VNET_DPDK_PMD_UNKNOWN;
407
408           xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
409           xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
410           xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
411
412           switch (xd->pmd)
413             {
414               /* 1G adapters */
415             case VNET_DPDK_PMD_E1000EM:
416             case VNET_DPDK_PMD_IGB:
417             case VNET_DPDK_PMD_IGBVF:
418               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
419               break;
420
421               /* 10G adapters */
422             case VNET_DPDK_PMD_IXGBE:
423             case VNET_DPDK_PMD_IXGBEVF:
424             case VNET_DPDK_PMD_THUNDERX:
425               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
426               break;
427             case VNET_DPDK_PMD_DPAA2:
428               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
429               break;
430
431               /* Cisco VIC */
432             case VNET_DPDK_PMD_ENIC:
433               rte_eth_link_get_nowait (i, &l);
434               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
435               if (l.link_speed == 40000)
436                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
437               else
438                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
439               break;
440
441               /* Intel Fortville */
442             case VNET_DPDK_PMD_I40E:
443             case VNET_DPDK_PMD_I40EVF:
444               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
445               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
446
447               if (dev_info.speed_capa & ETH_LINK_SPEED_40G)
448                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
449               else if (dev_info.speed_capa & ETH_LINK_SPEED_25G)
450                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_25G;
451               else if (dev_info.speed_capa & ETH_LINK_SPEED_10G)
452                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
453               else
454                 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
455               break;
456
457             case VNET_DPDK_PMD_CXGBE:
458               switch (dev_info.pci_dev->id.device_id)
459                 {
460                 case 0x540d:    /* T580-CR */
461                 case 0x5410:    /* T580-LP-cr */
462                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
463                   break;
464                 case 0x5403:    /* T540-CR */
465                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
466                   break;
467                 default:
468                   xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
469                 }
470               break;
471
472             case VNET_DPDK_PMD_MLX5:
473               {
474                 char *pn_100g[] = { "MCX415A-CCAT", "MCX416A-CCAT",
475                   "MCX556A-ECAT", "MCX556A-EDAT", "MCX555A-ECAT",
476                   "MCX515A-CCAT", "MCX516A-CCAT", "MCX516A-CDAT", 0
477                 };
478                 char *pn_40g[] = { "MCX413A-BCAT", "MCX414A-BCAT",
479                   "MCX415A-BCAT", "MCX416A-BCAT", "MCX4131A-BCAT", 0
480                 };
481                 char *pn_10g[] = { "MCX4111A-XCAT", "MCX4121A-XCAT", 0 };
482
483                 vlib_pci_device_t *pd = vlib_get_pci_device (&pci_addr);
484                 u8 *pn = 0;
485                 char **c;
486                 int found = 0;
487                 pn = format (0, "%U%c",
488                              format_vlib_pci_vpd, pd->vpd_r, "PN", 0);
489
490                 if (!pn)
491                   break;
492
493                 c = pn_100g;
494                 while (!found && c[0])
495                   {
496                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
497                       {
498                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_100G;
499                         break;
500                       }
501                     c++;
502                   }
503
504                 c = pn_40g;
505                 while (!found && c[0])
506                   {
507                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
508                       {
509                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
510                         break;
511                       }
512                     c++;
513                   }
514
515                 c = pn_10g;
516                 while (!found && c[0])
517                   {
518                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
519                       {
520                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
521                         break;
522                       }
523                     c++;
524                   }
525
526                 vec_free (pn);
527               }
528
529               break;
530               /* Intel Red Rock Canyon */
531             case VNET_DPDK_PMD_FM10K:
532               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
533               break;
534
535               /* virtio */
536             case VNET_DPDK_PMD_VIRTIO:
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->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
546               break;
547
548             case VNET_DPDK_PMD_AF_PACKET:
549               xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
550               xd->port_id = af_packet_port_id++;
551               break;
552
553             case VNET_DPDK_PMD_BOND:
554               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
555               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
556               xd->port_id = bond_ether_port_id++;
557               break;
558
559             default:
560               xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
561             }
562
563           if (devconf->num_rx_desc)
564             xd->nb_rx_desc = devconf->num_rx_desc;
565
566           if (devconf->num_tx_desc)
567             xd->nb_tx_desc = devconf->num_tx_desc;
568         }
569
570       /*
571        * Ensure default mtu is not > the mtu read from the hardware.
572        * Otherwise rte_eth_dev_configure() will fail and the port will
573        * not be available.
574        */
575       if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
576         {
577           /*
578            * This device does not support the platforms's max frame
579            * size. Use it's advertised mru instead.
580            */
581           xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
582         }
583       else
584         {
585           xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
586
587           /*
588            * Some platforms do not account for Ethernet FCS (4 bytes) in
589            * MTU calculations. To interop with them increase mru but only
590            * if the device's settings can support it.
591            */
592           if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
593               xd->port_conf.rxmode.hw_strip_crc)
594             {
595               /*
596                * Allow additional 4 bytes (for Ethernet FCS). These bytes are
597                * stripped by h/w and so will not consume any buffer memory.
598                */
599               xd->port_conf.rxmode.max_rx_pkt_len += 4;
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, (struct ether_addr *) addr);
615
616       if (xd->tx_q_used < tm->n_vlib_mains)
617         dpdk_device_lock_init (xd);
618
619       xd->device_index = xd - dm->devices;
620       ASSERT (i == xd->device_index);
621       xd->per_interface_next_index = ~0;
622
623       /* assign interface to input thread */
624       dpdk_device_and_queue_t *dq;
625       int q;
626
627       if (devconf->hqos_enabled)
628         {
629           xd->flags |= DPDK_DEVICE_FLAG_HQOS;
630
631           if (devconf->hqos.hqos_thread_valid)
632             {
633               int cpu = dm->hqos_cpu_first_index + devconf->hqos.hqos_thread;
634
635               if (devconf->hqos.hqos_thread >= dm->hqos_cpu_count)
636                 return clib_error_return (0, "invalid HQoS thread index");
637
638               vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
639               dq->device = xd->device_index;
640               dq->queue_id = 0;
641             }
642           else
643             {
644               int cpu = dm->hqos_cpu_first_index + next_hqos_cpu;
645
646               if (dm->hqos_cpu_count == 0)
647                 return clib_error_return (0, "no HQoS threads available");
648
649               vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
650               dq->device = xd->device_index;
651               dq->queue_id = 0;
652
653               next_hqos_cpu++;
654               if (next_hqos_cpu == dm->hqos_cpu_count)
655                 next_hqos_cpu = 0;
656
657               devconf->hqos.hqos_thread_valid = 1;
658               devconf->hqos.hqos_thread = cpu;
659             }
660         }
661
662       vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
663                             CLIB_CACHE_LINE_BYTES);
664       for (j = 0; j < tm->n_vlib_mains; j++)
665         {
666           vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
667                            sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
668           vec_reset_length (xd->tx_vectors[j]);
669         }
670
671       vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
672                             CLIB_CACHE_LINE_BYTES);
673       for (j = 0; j < xd->rx_q_used; j++)
674         {
675           vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
676                                 CLIB_CACHE_LINE_BYTES);
677           vec_reset_length (xd->rx_vectors[j]);
678         }
679
680       vec_validate_aligned (xd->d_trace_buffers, tm->n_vlib_mains,
681                             CLIB_CACHE_LINE_BYTES);
682
683
684       /* count the number of descriptors used for this device */
685       nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
686
687       error = ethernet_register_interface
688         (dm->vnet_main, dpdk_device_class.index, xd->device_index,
689          /* ethernet address */ addr,
690          &xd->hw_if_index, dpdk_flag_change);
691       if (error)
692         return error;
693
694       sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->hw_if_index);
695       xd->vlib_sw_if_index = sw->sw_if_index;
696       vnet_hw_interface_set_input_node (dm->vnet_main, xd->hw_if_index,
697                                         dpdk_input_node.index);
698
699       if (devconf->workers)
700         {
701           int i;
702           q = 0;
703           /* *INDENT-OFF* */
704           clib_bitmap_foreach (i, devconf->workers, ({
705             vnet_hw_interface_assign_rx_thread (dm->vnet_main, xd->hw_if_index, q++,
706                                              vdm->first_worker_thread_index + i);
707           }));
708           /* *INDENT-ON* */
709         }
710       else
711         for (q = 0; q < xd->rx_q_used; q++)
712           {
713             vnet_hw_interface_assign_rx_thread (dm->vnet_main, xd->hw_if_index, q,      /* any */
714                                                 ~1);
715           }
716
717       hi = vnet_get_hw_interface (dm->vnet_main, xd->hw_if_index);
718
719       rv = dpdk_port_setup (dm, xd);
720
721       if (rv)
722         return rv;
723
724       if (devconf->hqos_enabled)
725         {
726           rv = dpdk_port_setup_hqos (xd, &devconf->hqos);
727           if (rv)
728             return rv;
729         }
730
731       /*
732        * For cisco VIC vNIC, set default to VLAN strip enabled, unless
733        * specified otherwise in the startup config.
734        * For other NICs default to VLAN strip disabled, unless specified
735        * otherwis in the startup config.
736        */
737       if (xd->pmd == VNET_DPDK_PMD_ENIC)
738         {
739           if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
740             vlan_strip = 1;     /* remove vlan tag from VIC port by default */
741           else
742             clib_warning ("VLAN strip disabled for interface\n");
743         }
744       else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
745         vlan_strip = 1;
746
747       if (vlan_strip)
748         {
749           int vlan_off;
750           vlan_off = rte_eth_dev_get_vlan_offload (xd->device_index);
751           vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
752           xd->port_conf.rxmode.hw_vlan_strip = vlan_off;
753           if (rte_eth_dev_set_vlan_offload (xd->device_index, vlan_off) == 0)
754             clib_warning ("VLAN strip enabled for interface\n");
755           else
756             clib_warning ("VLAN strip cannot be supported by interface\n");
757         }
758
759       hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
760         xd->port_conf.rxmode.max_rx_pkt_len - sizeof (ethernet_header_t);
761
762       rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
763     }
764
765   if (nb_desc > dm->conf->num_mbufs)
766     clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
767                   dm->conf->num_mbufs, nb_desc);
768
769   return 0;
770 }
771
772 static void
773 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
774 {
775   vlib_pci_main_t *pm = &pci_main;
776   clib_error_t *error;
777   vlib_pci_device_t *d;
778   u8 *pci_addr = 0;
779   int num_whitelisted = vec_len (conf->dev_confs);
780
781   /* *INDENT-OFF* */
782   pool_foreach (d, pm->pci_devs, ({
783     dpdk_device_config_t * devconf = 0;
784     vec_reset_length (pci_addr);
785     pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
786
787     if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && d->device_class != PCI_CLASS_PROCESSOR_CO)
788       continue;
789
790     if (num_whitelisted)
791       {
792         uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
793
794         if (!p)
795           continue;
796
797         devconf = pool_elt_at_index (conf->dev_confs, p[0]);
798       }
799
800     /* virtio */
801     if (d->vendor_id == 0x1af4 && d->device_id == 0x1000)
802       ;
803     /* vmxnet3 */
804     else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
805       ;
806     /* all Intel network devices */
807     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_NETWORK_ETHERNET)
808       ;
809     /* all Intel QAT devices VFs */
810     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_PROCESSOR_CO &&
811         (d->device_id == 0x0443 || d->device_id == 0x37c9 || d->device_id == 0x19e3))
812       ;
813     /* Cisco VIC */
814     else if (d->vendor_id == 0x1137 && d->device_id == 0x0043)
815       ;
816     /* Chelsio T4/T5 */
817     else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
818       ;
819     else
820       {
821         clib_warning ("Unsupported PCI device 0x%04x:0x%04x found "
822                       "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
823                       pci_addr);
824         continue;
825       }
826
827     error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
828
829     if (error)
830       {
831         if (devconf == 0)
832           {
833             pool_get (conf->dev_confs, devconf);
834             hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
835                       devconf - conf->dev_confs);
836             devconf->pci_addr.as_u32 = d->bus_address.as_u32;
837           }
838         devconf->is_blacklisted = 1;
839         clib_error_report (error);
840       }
841   }));
842   /* *INDENT-ON* */
843   vec_free (pci_addr);
844 }
845
846 static clib_error_t *
847 dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr,
848                     unformat_input_t * input, u8 is_default)
849 {
850   clib_error_t *error = 0;
851   uword *p;
852   dpdk_device_config_t *devconf;
853   unformat_input_t sub_input;
854
855   if (is_default)
856     {
857       devconf = &conf->default_devconf;
858     }
859   else
860     {
861       p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
862
863       if (!p)
864         {
865           pool_get (conf->dev_confs, devconf);
866           hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32,
867                     devconf - conf->dev_confs);
868         }
869       else
870         return clib_error_return (0,
871                                   "duplicate configuration for PCI address %U",
872                                   format_vlib_pci_addr, &pci_addr);
873     }
874
875   devconf->pci_addr.as_u32 = pci_addr.as_u32;
876   devconf->hqos_enabled = 0;
877   dpdk_device_config_hqos_default (&devconf->hqos);
878
879   if (!input)
880     return 0;
881
882   unformat_skip_white_space (input);
883   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
884     {
885       if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
886         ;
887       else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
888         ;
889       else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
890         ;
891       else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
892         ;
893       else if (unformat (input, "workers %U", unformat_bitmap_list,
894                          &devconf->workers))
895         ;
896       else
897         if (unformat
898             (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
899         {
900           error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
901           if (error)
902             break;
903         }
904       else if (unformat (input, "vlan-strip-offload off"))
905         devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
906       else if (unformat (input, "vlan-strip-offload on"))
907         devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
908       else
909         if (unformat
910             (input, "hqos %U", unformat_vlib_cli_sub_input, &sub_input))
911         {
912           devconf->hqos_enabled = 1;
913           error = unformat_hqos (&sub_input, &devconf->hqos);
914           if (error)
915             break;
916         }
917       else if (unformat (input, "hqos"))
918         {
919           devconf->hqos_enabled = 1;
920         }
921       else
922         {
923           error = clib_error_return (0, "unknown input `%U'",
924                                      format_unformat_error, input);
925           break;
926         }
927     }
928
929   if (error)
930     return error;
931
932   if (devconf->workers && devconf->num_rx_queues == 0)
933     devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
934   else if (devconf->workers &&
935            clib_bitmap_count_set_bits (devconf->workers) !=
936            devconf->num_rx_queues)
937     error =
938       clib_error_return (0,
939                          "%U: number of worker threadds must be "
940                          "equal to number of rx queues", format_vlib_pci_addr,
941                          &pci_addr);
942
943   return error;
944 }
945
946 static clib_error_t *
947 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
948 {
949   clib_error_t *error = 0;
950   dpdk_main_t *dm = &dpdk_main;
951   dpdk_config_main_t *conf = &dpdk_config_main;
952   vlib_thread_main_t *tm = vlib_get_thread_main ();
953   dpdk_device_config_t *devconf;
954   vlib_pci_addr_t pci_addr;
955   unformat_input_t sub_input;
956   u8 *s, *tmp = 0;
957   u8 *rte_cmd = 0, *ethname = 0;
958   u32 log_level;
959   int ret, i;
960   int num_whitelisted = 0;
961   u8 no_pci = 0;
962   u8 no_huge = 0;
963   u8 huge_dir = 0;
964   u8 file_prefix = 0;
965   u8 *socket_mem = 0;
966
967   conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
968
969   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
970     {
971       /* Prime the pump */
972       if (unformat (input, "no-hugetlb"))
973         {
974           vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
975           no_huge = 1;
976         }
977
978       else if (unformat (input, "enable-tcp-udp-checksum"))
979         conf->enable_tcp_udp_checksum = 1;
980
981       else if (unformat (input, "decimal-interface-names"))
982         conf->interface_name_format_decimal = 1;
983
984       else if (unformat (input, "no-multi-seg"))
985         conf->no_multi_seg = 1;
986
987       else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
988                          &sub_input))
989         {
990           error =
991             dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
992                                 1);
993
994           if (error)
995             return error;
996         }
997       else
998         if (unformat
999             (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1000              unformat_vlib_cli_sub_input, &sub_input))
1001         {
1002           error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1003
1004           if (error)
1005             return error;
1006
1007           num_whitelisted++;
1008         }
1009       else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1010         {
1011           error = dpdk_device_config (conf, pci_addr, 0, 0);
1012
1013           if (error)
1014             return error;
1015
1016           num_whitelisted++;
1017         }
1018       else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
1019         ;
1020       else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1021         ;
1022       else if (unformat (input, "socket-mem %s", &socket_mem))
1023         ;
1024       else if (unformat (input, "no-pci"))
1025         {
1026           no_pci = 1;
1027           tmp = format (0, "--no-pci%c", 0);
1028           vec_add1 (conf->eal_init_args, tmp);
1029         }
1030       else if (unformat (input, "poll-sleep %d", &dm->poll_sleep_usec))
1031         ;
1032
1033 #define _(a)                                    \
1034       else if (unformat(input, #a))             \
1035         {                                       \
1036           tmp = format (0, "--%s%c", #a, 0);    \
1037           vec_add1 (conf->eal_init_args, tmp);    \
1038         }
1039       foreach_eal_double_hyphen_predicate_arg
1040 #undef _
1041 #define _(a)                                          \
1042         else if (unformat(input, #a " %s", &s))       \
1043           {                                           \
1044             if (!strncmp(#a, "huge-dir", 8))          \
1045               huge_dir = 1;                           \
1046             else if (!strncmp(#a, "file-prefix", 11)) \
1047               file_prefix = 1;                        \
1048             tmp = format (0, "--%s%c", #a, 0);        \
1049             vec_add1 (conf->eal_init_args, tmp);      \
1050             vec_add1 (s, 0);                          \
1051             if (!strncmp(#a, "vdev", 4))              \
1052               if (strstr((char*)s, "af_packet"))      \
1053                 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
1054             vec_add1 (conf->eal_init_args, s);        \
1055           }
1056         foreach_eal_double_hyphen_arg
1057 #undef _
1058 #define _(a,b)                                          \
1059           else if (unformat(input, #a " %s", &s))       \
1060             {                                           \
1061               tmp = format (0, "-%s%c", #b, 0);         \
1062               vec_add1 (conf->eal_init_args, tmp);      \
1063               vec_add1 (s, 0);                          \
1064               vec_add1 (conf->eal_init_args, s);        \
1065             }
1066         foreach_eal_single_hyphen_arg
1067 #undef _
1068 #define _(a,b)                                          \
1069             else if (unformat(input, #a " %s", &s))     \
1070               {                                         \
1071                 tmp = format (0, "-%s%c", #b, 0);       \
1072                 vec_add1 (conf->eal_init_args, tmp);    \
1073                 vec_add1 (s, 0);                        \
1074                 vec_add1 (conf->eal_init_args, s);      \
1075                 conf->a##_set_manually = 1;             \
1076               }
1077         foreach_eal_single_hyphen_mandatory_arg
1078 #undef _
1079         else if (unformat (input, "default"))
1080         ;
1081
1082       else if (unformat_skip_white_space (input))
1083         ;
1084       else
1085         {
1086           error = clib_error_return (0, "unknown input `%U'",
1087                                      format_unformat_error, input);
1088           goto done;
1089         }
1090     }
1091
1092   if (!conf->uio_driver_name)
1093     conf->uio_driver_name = format (0, "uio_pci_generic%c", 0);
1094
1095   /*
1096    * Use 1G huge pages if available.
1097    */
1098   if (!no_huge && !huge_dir)
1099     {
1100       u32 x, *mem_by_socket = 0;
1101       uword c = 0;
1102       u8 use_1g = 1;
1103       u8 use_2m = 1;
1104       u8 less_than_1g = 1;
1105       int rv;
1106
1107       umount (DEFAULT_HUGE_DIR);
1108
1109       /* Process "socket-mem" parameter value */
1110       if (vec_len (socket_mem))
1111         {
1112           unformat_input_t in;
1113           unformat_init_vector (&in, socket_mem);
1114           while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1115             {
1116               if (unformat (&in, "%u,", &x))
1117                 ;
1118               else if (unformat (&in, "%u", &x))
1119                 ;
1120               else if (unformat (&in, ","))
1121                 x = 0;
1122               else
1123                 break;
1124
1125               vec_add1 (mem_by_socket, x);
1126
1127               if (x > 1023)
1128                 less_than_1g = 0;
1129             }
1130           /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1131           unformat_free (&in);
1132           socket_mem = 0;
1133         }
1134       else
1135         {
1136           /* *INDENT-OFF* */
1137           clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1138             {
1139               vec_validate(mem_by_socket, c);
1140               mem_by_socket[c] = 256; /* default per-socket mem */
1141             }
1142           ));
1143           /* *INDENT-ON* */
1144         }
1145
1146       /* check if available enough 1GB pages for each socket */
1147       /* *INDENT-OFF* */
1148       clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1149         {
1150           int pages_avail, page_size, mem;
1151
1152           vec_validate(mem_by_socket, c);
1153           mem = mem_by_socket[c];
1154
1155           page_size = 1024;
1156           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1157
1158           if (pages_avail < 0 || page_size * pages_avail < mem)
1159             use_1g = 0;
1160
1161           page_size = 2;
1162           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1163
1164           if (pages_avail < 0 || page_size * pages_avail < mem)
1165             use_2m = 0;
1166       }));
1167       /* *INDENT-ON* */
1168
1169       if (mem_by_socket == 0)
1170         {
1171           error = clib_error_return (0, "mem_by_socket NULL");
1172           goto done;
1173         }
1174       _vec_len (mem_by_socket) = c + 1;
1175
1176       /* regenerate socket_mem string */
1177       vec_foreach_index (x, mem_by_socket)
1178         socket_mem = format (socket_mem, "%s%u",
1179                              socket_mem ? "," : "", mem_by_socket[x]);
1180       socket_mem = format (socket_mem, "%c", 0);
1181
1182       vec_free (mem_by_socket);
1183
1184       rv = mkdir (VPP_RUN_DIR, 0755);
1185       if (rv && errno != EEXIST)
1186         {
1187           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1188                                      VPP_RUN_DIR, errno);
1189           goto done;
1190         }
1191
1192       rv = mkdir (DEFAULT_HUGE_DIR, 0755);
1193       if (rv && errno != EEXIST)
1194         {
1195           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1196                                      DEFAULT_HUGE_DIR, errno);
1197           goto done;
1198         }
1199
1200       if (use_1g && !(less_than_1g && use_2m))
1201         {
1202           rv =
1203             mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1204         }
1205       else if (use_2m)
1206         {
1207           rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1208         }
1209       else
1210         {
1211           return clib_error_return (0, "not enough free huge pages");
1212         }
1213
1214       if (rv)
1215         {
1216           error = clib_error_return (0, "mount failed %d", errno);
1217           goto done;
1218         }
1219
1220       tmp = format (0, "--huge-dir%c", 0);
1221       vec_add1 (conf->eal_init_args, tmp);
1222       tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1223       vec_add1 (conf->eal_init_args, tmp);
1224       if (!file_prefix)
1225         {
1226           tmp = format (0, "--file-prefix%c", 0);
1227           vec_add1 (conf->eal_init_args, tmp);
1228           tmp = format (0, "vpp%c", 0);
1229           vec_add1 (conf->eal_init_args, tmp);
1230         }
1231     }
1232
1233   vec_free (rte_cmd);
1234   vec_free (ethname);
1235
1236   if (error)
1237     return error;
1238
1239   /* I'll bet that -c and -n must be the first and second args... */
1240   if (!conf->coremask_set_manually)
1241     {
1242       vlib_thread_registration_t *tr;
1243       uword *coremask = 0;
1244       int i;
1245
1246       /* main thread core */
1247       coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
1248
1249       for (i = 0; i < vec_len (tm->registrations); i++)
1250         {
1251           tr = tm->registrations[i];
1252           coremask = clib_bitmap_or (coremask, tr->coremask);
1253         }
1254
1255       vec_insert (conf->eal_init_args, 2, 1);
1256       conf->eal_init_args[1] = (u8 *) "-c";
1257       tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1258       conf->eal_init_args[2] = tmp;
1259       clib_bitmap_free (coremask);
1260     }
1261
1262   if (!conf->nchannels_set_manually)
1263     {
1264       vec_insert (conf->eal_init_args, 2, 3);
1265       conf->eal_init_args[3] = (u8 *) "-n";
1266       tmp = format (0, "%d", conf->nchannels);
1267       conf->eal_init_args[4] = tmp;
1268     }
1269
1270   if (no_pci == 0 && geteuid () == 0)
1271     dpdk_bind_devices_to_uio (conf);
1272
1273 #define _(x) \
1274     if (devconf->x == 0 && conf->default_devconf.x > 0) \
1275       devconf->x = conf->default_devconf.x ;
1276
1277   /* *INDENT-OFF* */
1278   pool_foreach (devconf, conf->dev_confs, ({
1279
1280     /* default per-device config items */
1281     foreach_dpdk_device_config_item
1282
1283     /* add DPDK EAL whitelist/blacklist entry */
1284     if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1285       {
1286         tmp = format (0, "-w%c", 0);
1287         vec_add1 (conf->eal_init_args, tmp);
1288         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1289         vec_add1 (conf->eal_init_args, tmp);
1290       }
1291     else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1292       {
1293         tmp = format (0, "-b%c", 0);
1294         vec_add1 (conf->eal_init_args, tmp);
1295         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1296         vec_add1 (conf->eal_init_args, tmp);
1297       }
1298   }));
1299   /* *INDENT-ON* */
1300
1301 #undef _
1302
1303   /* set master-lcore */
1304   tmp = format (0, "--master-lcore%c", 0);
1305   vec_add1 (conf->eal_init_args, tmp);
1306   tmp = format (0, "%u%c", tm->main_lcore, 0);
1307   vec_add1 (conf->eal_init_args, tmp);
1308
1309   /* set socket-mem */
1310   tmp = format (0, "--socket-mem%c", 0);
1311   vec_add1 (conf->eal_init_args, tmp);
1312   tmp = format (0, "%s%c", socket_mem, 0);
1313   vec_add1 (conf->eal_init_args, tmp);
1314
1315   /* NULL terminate the "argv" vector, in case of stupidity */
1316   vec_add1 (conf->eal_init_args, 0);
1317   _vec_len (conf->eal_init_args) -= 1;
1318
1319   /* Set up DPDK eal and packet mbuf pool early. */
1320
1321   log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1322
1323   rte_set_log_level (log_level);
1324
1325   vm = vlib_get_main ();
1326
1327   /* make copy of args as rte_eal_init tends to mess up with arg array */
1328   for (i = 1; i < vec_len (conf->eal_init_args); i++)
1329     conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1330                                       conf->eal_init_args[i]);
1331
1332   ret =
1333     rte_eal_init (vec_len (conf->eal_init_args),
1334                   (char **) conf->eal_init_args);
1335
1336   /* lazy umount hugepages */
1337   umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
1338
1339   if (ret < 0)
1340     return clib_error_return (0, "rte_eal_init returned %d", ret);
1341
1342   /* Dump the physical memory layout prior to creating the mbuf_pool */
1343   fprintf (stdout, "DPDK physical memory layout:\n");
1344   rte_dump_physmem_layout (stdout);
1345
1346   /* main thread 1st */
1347   error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
1348   if (error)
1349     return error;
1350
1351   for (i = 0; i < RTE_MAX_LCORE; i++)
1352     {
1353       error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1354                                        rte_lcore_to_socket_id (i));
1355       if (error)
1356         return error;
1357     }
1358
1359 done:
1360   return error;
1361 }
1362
1363 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1364
1365 void
1366 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1367 {
1368   vnet_main_t *vnm = vnet_get_main ();
1369   struct rte_eth_link prev_link = xd->link;
1370   u32 hw_flags = 0;
1371   u8 hw_flags_chg = 0;
1372
1373   /* only update link state for PMD interfaces */
1374   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1375     return;
1376
1377   xd->time_last_link_update = now ? now : xd->time_last_link_update;
1378   memset (&xd->link, 0, sizeof (xd->link));
1379   rte_eth_link_get_nowait (xd->device_index, &xd->link);
1380
1381   if (LINK_STATE_ELOGS)
1382     {
1383       vlib_main_t *vm = vlib_get_main ();
1384       ELOG_TYPE_DECLARE (e) =
1385       {
1386       .format =
1387           "update-link-state: sw_if_index %d, admin_up %d,"
1388           "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1389
1390       struct
1391       {
1392         u32 sw_if_index;
1393         u8 admin_up;
1394         u8 old_link_state;
1395         u8 new_link_state;
1396       } *ed;
1397       ed = ELOG_DATA (&vm->elog_main, e);
1398       ed->sw_if_index = xd->vlib_sw_if_index;
1399       ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1400       ed->old_link_state = (u8)
1401         vnet_hw_interface_is_link_up (vnm, xd->hw_if_index);
1402       ed->new_link_state = (u8) xd->link.link_status;
1403     }
1404
1405   if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
1406       ((xd->link.link_status != 0) ^
1407        vnet_hw_interface_is_link_up (vnm, xd->hw_if_index)))
1408     {
1409       hw_flags_chg = 1;
1410       hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1411     }
1412
1413   if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1414     {
1415       hw_flags_chg = 1;
1416       switch (xd->link.link_duplex)
1417         {
1418         case ETH_LINK_HALF_DUPLEX:
1419           hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1420           break;
1421         case ETH_LINK_FULL_DUPLEX:
1422           hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1423           break;
1424         default:
1425           break;
1426         }
1427     }
1428   if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1429     {
1430       hw_flags_chg = 1;
1431       switch (xd->link.link_speed)
1432         {
1433         case ETH_SPEED_NUM_10M:
1434           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1435           break;
1436         case ETH_SPEED_NUM_100M:
1437           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1438           break;
1439         case ETH_SPEED_NUM_1G:
1440           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1441           break;
1442         case ETH_SPEED_NUM_10G:
1443           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1444           break;
1445         case ETH_SPEED_NUM_40G:
1446           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1447           break;
1448         case 0:
1449           break;
1450         default:
1451           clib_warning ("unknown link speed %d", xd->link.link_speed);
1452           break;
1453         }
1454     }
1455   if (hw_flags_chg)
1456     {
1457       if (LINK_STATE_ELOGS)
1458         {
1459           vlib_main_t *vm = vlib_get_main ();
1460
1461           ELOG_TYPE_DECLARE (e) =
1462           {
1463           .format =
1464               "update-link-state: sw_if_index %d, new flags %d",.format_args
1465               = "i4i4",};
1466
1467           struct
1468           {
1469             u32 sw_if_index;
1470             u32 flags;
1471           } *ed;
1472           ed = ELOG_DATA (&vm->elog_main, e);
1473           ed->sw_if_index = xd->vlib_sw_if_index;
1474           ed->flags = hw_flags;
1475         }
1476       vnet_hw_interface_set_flags (vnm, xd->hw_if_index, hw_flags);
1477     }
1478 }
1479
1480 static uword
1481 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1482 {
1483   clib_error_t *error;
1484   vnet_main_t *vnm = vnet_get_main ();
1485   dpdk_main_t *dm = &dpdk_main;
1486   ethernet_main_t *em = &ethernet_main;
1487   dpdk_device_t *xd;
1488   vlib_thread_main_t *tm = vlib_get_thread_main ();
1489   int i;
1490
1491   error = dpdk_lib_init (dm);
1492
1493   if (error)
1494     clib_error_report (error);
1495
1496   tm->worker_thread_release = 1;
1497
1498   f64 now = vlib_time_now (vm);
1499   vec_foreach (xd, dm->devices)
1500   {
1501     dpdk_update_link_state (xd, now);
1502   }
1503
1504   {
1505     /*
1506      * Extra set up for bond interfaces:
1507      *  1. Setup MACs for bond interfaces and their slave links which was set
1508      *     in dpdk_port_setup() but needs to be done again here to take effect.
1509      *  2. Set up info for bond interface related CLI support.
1510      */
1511     int nports = rte_eth_dev_count ();
1512     if (nports > 0)
1513       {
1514         for (i = 0; i < nports; i++)
1515           {
1516             xd = &dm->devices[i];
1517             ASSERT (i == xd->device_index);
1518             if (xd->pmd == VNET_DPDK_PMD_BOND)
1519               {
1520                 u8 addr[6];
1521                 u8 slink[16];
1522                 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1523                 if (nlink > 0)
1524                   {
1525                     vnet_hw_interface_t *bhi;
1526                     ethernet_interface_t *bei;
1527                     int rv;
1528
1529                     /* Get MAC of 1st slave link */
1530                     rte_eth_macaddr_get
1531                       (slink[0], (struct ether_addr *) addr);
1532
1533                     /* Set MAC of bounded interface to that of 1st slave link */
1534                     clib_warning ("Set MAC for bond dev# %d", i);
1535                     rv = rte_eth_bond_mac_address_set
1536                       (i, (struct ether_addr *) addr);
1537                     if (rv)
1538                       clib_warning ("Set MAC addr failure rv=%d", rv);
1539
1540                     /* Populate MAC of bonded interface in VPP hw tables */
1541                     bhi = vnet_get_hw_interface
1542                       (vnm, dm->devices[i].hw_if_index);
1543                     bei = pool_elt_at_index
1544                       (em->interfaces, bhi->hw_instance);
1545                     clib_memcpy (bhi->hw_address, addr, 6);
1546                     clib_memcpy (bei->address, addr, 6);
1547
1548                     /* Init l3 packet size allowed on bonded interface */
1549                     bhi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
1550                     bhi->max_l3_packet_bytes[VLIB_RX] =
1551                       bhi->max_l3_packet_bytes[VLIB_TX] =
1552                       ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1553                     while (nlink >= 1)
1554                       {         /* for all slave links */
1555                         int slave = slink[--nlink];
1556                         dpdk_device_t *sdev = &dm->devices[slave];
1557                         vnet_hw_interface_t *shi;
1558                         vnet_sw_interface_t *ssi;
1559                         ethernet_interface_t *sei;
1560                         /* Add MAC to all slave links except the first one */
1561                         if (nlink)
1562                           {
1563                             clib_warning ("Add MAC for slave dev# %d", slave);
1564                             rv = rte_eth_dev_mac_addr_add
1565                               (slave, (struct ether_addr *) addr, 0);
1566                             if (rv)
1567                               clib_warning ("Add MAC addr failure rv=%d", rv);
1568                           }
1569                         /* Set slaves bitmap for bonded interface */
1570                         bhi->bond_info = clib_bitmap_set
1571                           (bhi->bond_info, sdev->hw_if_index, 1);
1572                         /* Set slave link flags on slave interface */
1573                         shi = vnet_get_hw_interface (vnm, sdev->hw_if_index);
1574                         ssi = vnet_get_sw_interface
1575                           (vnm, sdev->vlib_sw_if_index);
1576                         sei = pool_elt_at_index
1577                           (em->interfaces, shi->hw_instance);
1578
1579                         shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1580                         ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1581                         clib_memcpy (shi->hw_address, addr, 6);
1582                         clib_memcpy (sei->address, addr, 6);
1583
1584                         /* Set l3 packet size allowed as the lowest of slave */
1585                         if (bhi->max_l3_packet_bytes[VLIB_RX] >
1586                             shi->max_l3_packet_bytes[VLIB_RX])
1587                           bhi->max_l3_packet_bytes[VLIB_RX] =
1588                             bhi->max_l3_packet_bytes[VLIB_TX] =
1589                             shi->max_l3_packet_bytes[VLIB_RX];
1590
1591                         /* Set max packet size allowed as the lowest of slave */
1592                         if (bhi->max_packet_bytes > shi->max_packet_bytes)
1593                           bhi->max_packet_bytes = shi->max_packet_bytes;
1594                       }
1595                   }
1596               }
1597           }
1598       }
1599   }
1600
1601   while (1)
1602     {
1603       /*
1604        * check each time through the loop in case intervals are changed
1605        */
1606       f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1607         dm->link_state_poll_interval : dm->stat_poll_interval;
1608
1609       vlib_process_wait_for_event_or_clock (vm, min_wait);
1610
1611       if (dm->admin_up_down_in_progress)
1612         /* skip the poll if an admin up down is in progress (on any interface) */
1613         continue;
1614
1615       vec_foreach (xd, dm->devices)
1616       {
1617         f64 now = vlib_time_now (vm);
1618         if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1619           dpdk_update_counters (xd, now);
1620         if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1621           dpdk_update_link_state (xd, now);
1622
1623       }
1624     }
1625
1626   return 0;
1627 }
1628
1629 /* *INDENT-OFF* */
1630 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1631     .function = dpdk_process,
1632     .type = VLIB_NODE_TYPE_PROCESS,
1633     .name = "dpdk-process",
1634     .process_log2_n_stack_bytes = 17,
1635 };
1636 /* *INDENT-ON* */
1637
1638 static clib_error_t *
1639 dpdk_init (vlib_main_t * vm)
1640 {
1641   dpdk_main_t *dm = &dpdk_main;
1642   vlib_node_t *ei;
1643   clib_error_t *error = 0;
1644   vlib_thread_main_t *tm = vlib_get_thread_main ();
1645
1646   /* verify that structs are cacheline aligned */
1647   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1648                  "Cache line marker must be 1st element in dpdk_device_t");
1649   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1650                  CLIB_CACHE_LINE_BYTES,
1651                  "Data in cache line 0 is bigger than cache line size");
1652   STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1653                  "Cache line marker must be 1st element in frame_queue_trace_t");
1654
1655   dm->vlib_main = vm;
1656   dm->vnet_main = vnet_get_main ();
1657   dm->conf = &dpdk_config_main;
1658
1659   ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1660   if (ei == 0)
1661     return clib_error_return (0, "ethernet-input node AWOL");
1662
1663   dm->ethernet_input_node_index = ei->index;
1664
1665   dm->conf->nchannels = 4;
1666   dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1667   vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1668
1669   vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1670
1671   /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1672   dm->buffer_flags_template =
1673     (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_EXT_HDR_VALID
1674      | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
1675
1676   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1677   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1678
1679   /* init CLI */
1680   if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1681     return error;
1682
1683   return error;
1684 }
1685
1686 VLIB_INIT_FUNCTION (dpdk_init);
1687
1688
1689 /*
1690  * fd.io coding-style-patch-verification: ON
1691  *
1692  * Local Variables:
1693  * eval: (c-set-style "gnu")
1694  * End:
1695  */