dpdk: cleanup, move APIs to separate .c file
[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               switch (dev_info.pci_dev->id.device_id)
448                 {
449                 case I40E_DEV_ID_10G_BASE_T:
450                 case I40E_DEV_ID_SFP_XL710:
451                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
452                   break;
453                 case I40E_DEV_ID_QSFP_A:
454                 case I40E_DEV_ID_QSFP_B:
455                 case I40E_DEV_ID_QSFP_C:
456                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
457                   break;
458                 case I40E_DEV_ID_VF:
459                   rte_eth_link_get_nowait (i, &l);
460                   xd->port_type = l.link_speed == 10000 ?
461                     VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
462                   break;
463                 default:
464                   xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
465                 }
466               break;
467
468             case VNET_DPDK_PMD_CXGBE:
469               switch (dev_info.pci_dev->id.device_id)
470                 {
471                 case 0x540d:    /* T580-CR */
472                 case 0x5410:    /* T580-LP-cr */
473                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
474                   break;
475                 case 0x5403:    /* T540-CR */
476                   xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
477                   break;
478                 default:
479                   xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
480                 }
481               break;
482
483             case VNET_DPDK_PMD_MLX5:
484               {
485                 char *pn_100g[] = { "MCX415A-CCAT", "MCX416A-CCAT",
486                   "MCX556A-ECAT", "MCX556A-EDAT", "MCX555A-ECAT",
487                   "MCX515A-CCAT", "MCX516A-CCAT", "MCX516A-CDAT", 0
488                 };
489                 char *pn_40g[] = { "MCX413A-BCAT", "MCX414A-BCAT",
490                   "MCX415A-BCAT", "MCX416A-BCAT", "MCX4131A-BCAT", 0
491                 };
492                 char *pn_10g[] = { "MCX4111A-XCAT", "MCX4121A-XCAT", 0 };
493
494                 vlib_pci_device_t *pd = vlib_get_pci_device (&pci_addr);
495                 u8 *pn = 0;
496                 char **c;
497                 int found = 0;
498                 pn = format (0, "%U%c",
499                              format_vlib_pci_vpd, pd->vpd_r, "PN", 0);
500
501                 if (!pn)
502                   break;
503
504                 c = pn_100g;
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_100G;
510                         break;
511                       }
512                     c++;
513                   }
514
515                 c = pn_40g;
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_40G;
521                         break;
522                       }
523                     c++;
524                   }
525
526                 c = pn_10g;
527                 while (!found && c[0])
528                   {
529                     if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
530                       {
531                         xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
532                         break;
533                       }
534                     c++;
535                   }
536
537                 vec_free (pn);
538               }
539
540               break;
541               /* Intel Red Rock Canyon */
542             case VNET_DPDK_PMD_FM10K:
543               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
544               break;
545
546               /* virtio */
547             case VNET_DPDK_PMD_VIRTIO:
548               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
549               xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
550               xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
551               break;
552
553               /* vmxnet3 */
554             case VNET_DPDK_PMD_VMXNET3:
555               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
556               xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
557               break;
558
559             case VNET_DPDK_PMD_AF_PACKET:
560               xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
561               xd->port_id = af_packet_port_id++;
562               break;
563
564             case VNET_DPDK_PMD_BOND:
565               xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
566               xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
567               xd->port_id = bond_ether_port_id++;
568               break;
569
570             default:
571               xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
572             }
573
574           if (devconf->num_rx_desc)
575             xd->nb_rx_desc = devconf->num_rx_desc;
576
577           if (devconf->num_tx_desc)
578             xd->nb_tx_desc = devconf->num_tx_desc;
579         }
580
581       /*
582        * Ensure default mtu is not > the mtu read from the hardware.
583        * Otherwise rte_eth_dev_configure() will fail and the port will
584        * not be available.
585        */
586       if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
587         {
588           /*
589            * This device does not support the platforms's max frame
590            * size. Use it's advertised mru instead.
591            */
592           xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
593         }
594       else
595         {
596           xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
597
598           /*
599            * Some platforms do not account for Ethernet FCS (4 bytes) in
600            * MTU calculations. To interop with them increase mru but only
601            * if the device's settings can support it.
602            */
603           if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
604               xd->port_conf.rxmode.hw_strip_crc)
605             {
606               /*
607                * Allow additional 4 bytes (for Ethernet FCS). These bytes are
608                * stripped by h/w and so will not consume any buffer memory.
609                */
610               xd->port_conf.rxmode.max_rx_pkt_len += 4;
611             }
612         }
613
614       if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
615         {
616           f64 now = vlib_time_now (vm);
617           u32 rnd;
618           rnd = (u32) (now * 1e6);
619           rnd = random_u32 (&rnd);
620           clib_memcpy (addr + 2, &rnd, sizeof (rnd));
621           addr[0] = 2;
622           addr[1] = 0xfe;
623         }
624       else
625         rte_eth_macaddr_get (i, (struct ether_addr *) addr);
626
627       if (xd->tx_q_used < tm->n_vlib_mains)
628         dpdk_device_lock_init (xd);
629
630       xd->device_index = xd - dm->devices;
631       ASSERT (i == xd->device_index);
632       xd->per_interface_next_index = ~0;
633
634       /* assign interface to input thread */
635       dpdk_device_and_queue_t *dq;
636       int q;
637
638       if (devconf->hqos_enabled)
639         {
640           xd->flags |= DPDK_DEVICE_FLAG_HQOS;
641
642           if (devconf->hqos.hqos_thread_valid)
643             {
644               int cpu = dm->hqos_cpu_first_index + devconf->hqos.hqos_thread;
645
646               if (devconf->hqos.hqos_thread >= dm->hqos_cpu_count)
647                 return clib_error_return (0, "invalid HQoS thread index");
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           else
654             {
655               int cpu = dm->hqos_cpu_first_index + next_hqos_cpu;
656
657               if (dm->hqos_cpu_count == 0)
658                 return clib_error_return (0, "no HQoS threads available");
659
660               vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
661               dq->device = xd->device_index;
662               dq->queue_id = 0;
663
664               next_hqos_cpu++;
665               if (next_hqos_cpu == dm->hqos_cpu_count)
666                 next_hqos_cpu = 0;
667
668               devconf->hqos.hqos_thread_valid = 1;
669               devconf->hqos.hqos_thread = cpu;
670             }
671         }
672
673       vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
674                             CLIB_CACHE_LINE_BYTES);
675       for (j = 0; j < tm->n_vlib_mains; j++)
676         {
677           vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
678                            sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
679           vec_reset_length (xd->tx_vectors[j]);
680         }
681
682       vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
683                             CLIB_CACHE_LINE_BYTES);
684       for (j = 0; j < xd->rx_q_used; j++)
685         {
686           vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
687                                 CLIB_CACHE_LINE_BYTES);
688           vec_reset_length (xd->rx_vectors[j]);
689         }
690
691       vec_validate_aligned (xd->d_trace_buffers, tm->n_vlib_mains,
692                             CLIB_CACHE_LINE_BYTES);
693
694
695       /* count the number of descriptors used for this device */
696       nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
697
698       error = ethernet_register_interface
699         (dm->vnet_main, dpdk_device_class.index, xd->device_index,
700          /* ethernet address */ addr,
701          &xd->hw_if_index, dpdk_flag_change);
702       if (error)
703         return error;
704
705       sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->hw_if_index);
706       xd->vlib_sw_if_index = sw->sw_if_index;
707       vnet_set_device_input_node (dm->vnet_main, xd->hw_if_index,
708                                   dpdk_input_node.index);
709
710       if (devconf->workers)
711         {
712           int i;
713           q = 0;
714           /* *INDENT-OFF* */
715           clib_bitmap_foreach (i, devconf->workers, ({
716             vnet_device_input_assign_thread (dm->vnet_main, xd->hw_if_index, q++,
717                                              vdm->first_worker_thread_index + i);
718           }));
719           /* *INDENT-ON* */
720         }
721       else
722         for (q = 0; q < xd->rx_q_used; q++)
723           {
724             vnet_device_input_assign_thread (dm->vnet_main, xd->hw_if_index, q, /* any */
725                                              ~1);
726           }
727
728       hi = vnet_get_hw_interface (dm->vnet_main, xd->hw_if_index);
729
730       rv = dpdk_port_setup (dm, xd);
731
732       if (rv)
733         return rv;
734
735       if (devconf->hqos_enabled)
736         {
737           rv = dpdk_port_setup_hqos (xd, &devconf->hqos);
738           if (rv)
739             return rv;
740         }
741
742       /*
743        * For cisco VIC vNIC, set default to VLAN strip enabled, unless
744        * specified otherwise in the startup config.
745        * For other NICs default to VLAN strip disabled, unless specified
746        * otherwis in the startup config.
747        */
748       if (xd->pmd == VNET_DPDK_PMD_ENIC)
749         {
750           if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
751             vlan_strip = 1;     /* remove vlan tag from VIC port by default */
752           else
753             clib_warning ("VLAN strip disabled for interface\n");
754         }
755       else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
756         vlan_strip = 1;
757
758       if (vlan_strip)
759         {
760           int vlan_off;
761           vlan_off = rte_eth_dev_get_vlan_offload (xd->device_index);
762           vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
763           xd->port_conf.rxmode.hw_vlan_strip = vlan_off;
764           if (rte_eth_dev_set_vlan_offload (xd->device_index, vlan_off) == 0)
765             clib_warning ("VLAN strip enabled for interface\n");
766           else
767             clib_warning ("VLAN strip cannot be supported by interface\n");
768         }
769
770       hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
771         xd->port_conf.rxmode.max_rx_pkt_len - sizeof (ethernet_header_t);
772
773       rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
774     }
775
776   if (nb_desc > dm->conf->num_mbufs)
777     clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
778                   dm->conf->num_mbufs, nb_desc);
779
780   return 0;
781 }
782
783 static void
784 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
785 {
786   vlib_pci_main_t *pm = &pci_main;
787   clib_error_t *error;
788   vlib_pci_device_t *d;
789   u8 *pci_addr = 0;
790   int num_whitelisted = vec_len (conf->dev_confs);
791
792   /* *INDENT-OFF* */
793   pool_foreach (d, pm->pci_devs, ({
794     dpdk_device_config_t * devconf = 0;
795     vec_reset_length (pci_addr);
796     pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
797
798     if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && d->device_class != PCI_CLASS_PROCESSOR_CO)
799       continue;
800
801     if (num_whitelisted)
802       {
803         uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
804
805         if (!p)
806           continue;
807
808         devconf = pool_elt_at_index (conf->dev_confs, p[0]);
809       }
810
811     /* virtio */
812     if (d->vendor_id == 0x1af4 && d->device_id == 0x1000)
813       ;
814     /* vmxnet3 */
815     else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
816       ;
817     /* all Intel network devices */
818     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_NETWORK_ETHERNET)
819       ;
820     /* all Intel QAT devices VFs */
821     else if (d->vendor_id == 0x8086 && d->device_class == PCI_CLASS_PROCESSOR_CO &&
822         (d->device_id == 0x0443 || d->device_id == 0x37c9 || d->device_id == 0x19e3))
823       ;
824     /* Cisco VIC */
825     else if (d->vendor_id == 0x1137 && d->device_id == 0x0043)
826       ;
827     /* Chelsio T4/T5 */
828     else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
829       ;
830     else
831       {
832         clib_warning ("Unsupported PCI device 0x%04x:0x%04x found "
833                       "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
834                       pci_addr);
835         continue;
836       }
837
838     error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
839
840     if (error)
841       {
842         if (devconf == 0)
843           {
844             pool_get (conf->dev_confs, devconf);
845             hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
846                       devconf - conf->dev_confs);
847             devconf->pci_addr.as_u32 = d->bus_address.as_u32;
848           }
849         devconf->is_blacklisted = 1;
850         clib_error_report (error);
851       }
852   }));
853   /* *INDENT-ON* */
854   vec_free (pci_addr);
855 }
856
857 static clib_error_t *
858 dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr,
859                     unformat_input_t * input, u8 is_default)
860 {
861   clib_error_t *error = 0;
862   uword *p;
863   dpdk_device_config_t *devconf;
864   unformat_input_t sub_input;
865
866   if (is_default)
867     {
868       devconf = &conf->default_devconf;
869     }
870   else
871     {
872       p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
873
874       if (!p)
875         {
876           pool_get (conf->dev_confs, devconf);
877           hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32,
878                     devconf - conf->dev_confs);
879         }
880       else
881         return clib_error_return (0,
882                                   "duplicate configuration for PCI address %U",
883                                   format_vlib_pci_addr, &pci_addr);
884     }
885
886   devconf->pci_addr.as_u32 = pci_addr.as_u32;
887   devconf->hqos_enabled = 0;
888   dpdk_device_config_hqos_default (&devconf->hqos);
889
890   if (!input)
891     return 0;
892
893   unformat_skip_white_space (input);
894   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
895     {
896       if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
897         ;
898       else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
899         ;
900       else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
901         ;
902       else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
903         ;
904       else if (unformat (input, "workers %U", unformat_bitmap_list,
905                          &devconf->workers))
906         ;
907       else
908         if (unformat
909             (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
910         {
911           error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
912           if (error)
913             break;
914         }
915       else if (unformat (input, "vlan-strip-offload off"))
916         devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
917       else if (unformat (input, "vlan-strip-offload on"))
918         devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
919       else
920         if (unformat
921             (input, "hqos %U", unformat_vlib_cli_sub_input, &sub_input))
922         {
923           devconf->hqos_enabled = 1;
924           error = unformat_hqos (&sub_input, &devconf->hqos);
925           if (error)
926             break;
927         }
928       else if (unformat (input, "hqos"))
929         {
930           devconf->hqos_enabled = 1;
931         }
932       else
933         {
934           error = clib_error_return (0, "unknown input `%U'",
935                                      format_unformat_error, input);
936           break;
937         }
938     }
939
940   if (error)
941     return error;
942
943   if (devconf->workers && devconf->num_rx_queues == 0)
944     devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
945   else if (devconf->workers &&
946            clib_bitmap_count_set_bits (devconf->workers) !=
947            devconf->num_rx_queues)
948     error =
949       clib_error_return (0,
950                          "%U: number of worker threadds must be "
951                          "equal to number of rx queues", format_vlib_pci_addr,
952                          &pci_addr);
953
954   return error;
955 }
956
957 static clib_error_t *
958 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
959 {
960   clib_error_t *error = 0;
961   dpdk_main_t *dm = &dpdk_main;
962   dpdk_config_main_t *conf = &dpdk_config_main;
963   vlib_thread_main_t *tm = vlib_get_thread_main ();
964   dpdk_device_config_t *devconf;
965   vlib_pci_addr_t pci_addr;
966   unformat_input_t sub_input;
967   u8 *s, *tmp = 0;
968   u8 *rte_cmd = 0, *ethname = 0;
969   u32 log_level;
970   int ret, i;
971   int num_whitelisted = 0;
972   u8 no_pci = 0;
973   u8 no_huge = 0;
974   u8 huge_dir = 0;
975   u8 file_prefix = 0;
976   u8 *socket_mem = 0;
977
978   conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
979
980   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
981     {
982       /* Prime the pump */
983       if (unformat (input, "no-hugetlb"))
984         {
985           vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
986           no_huge = 1;
987         }
988
989       else if (unformat (input, "enable-tcp-udp-checksum"))
990         conf->enable_tcp_udp_checksum = 1;
991
992       else if (unformat (input, "decimal-interface-names"))
993         conf->interface_name_format_decimal = 1;
994
995       else if (unformat (input, "no-multi-seg"))
996         conf->no_multi_seg = 1;
997
998       else if (unformat (input, "enable-cryptodev"))
999         conf->cryptodev = 1;
1000
1001       else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1002                          &sub_input))
1003         {
1004           error =
1005             dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
1006                                 1);
1007
1008           if (error)
1009             return error;
1010         }
1011       else
1012         if (unformat
1013             (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1014              unformat_vlib_cli_sub_input, &sub_input))
1015         {
1016           error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1017
1018           if (error)
1019             return error;
1020
1021           num_whitelisted++;
1022         }
1023       else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1024         {
1025           error = dpdk_device_config (conf, pci_addr, 0, 0);
1026
1027           if (error)
1028             return error;
1029
1030           num_whitelisted++;
1031         }
1032       else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
1033         ;
1034       else if (unformat (input, "kni %d", &conf->num_kni))
1035         ;
1036       else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1037         ;
1038       else if (unformat (input, "socket-mem %s", &socket_mem))
1039         ;
1040       else if (unformat (input, "no-pci"))
1041         {
1042           no_pci = 1;
1043           tmp = format (0, "--no-pci%c", 0);
1044           vec_add1 (conf->eal_init_args, tmp);
1045         }
1046       else if (unformat (input, "poll-sleep %d", &dm->poll_sleep_usec))
1047         ;
1048
1049 #define _(a)                                    \
1050       else if (unformat(input, #a))             \
1051         {                                       \
1052           tmp = format (0, "--%s%c", #a, 0);    \
1053           vec_add1 (conf->eal_init_args, tmp);    \
1054         }
1055       foreach_eal_double_hyphen_predicate_arg
1056 #undef _
1057 #define _(a)                                          \
1058         else if (unformat(input, #a " %s", &s))       \
1059           {                                           \
1060             if (!strncmp(#a, "huge-dir", 8))          \
1061               huge_dir = 1;                           \
1062             else if (!strncmp(#a, "file-prefix", 11)) \
1063               file_prefix = 1;                        \
1064             tmp = format (0, "--%s%c", #a, 0);        \
1065             vec_add1 (conf->eal_init_args, tmp);      \
1066             vec_add1 (s, 0);                          \
1067             if (!strncmp(#a, "vdev", 4))              \
1068               if (strstr((char*)s, "af_packet"))      \
1069                 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
1070             vec_add1 (conf->eal_init_args, s);        \
1071           }
1072         foreach_eal_double_hyphen_arg
1073 #undef _
1074 #define _(a,b)                                          \
1075           else if (unformat(input, #a " %s", &s))       \
1076             {                                           \
1077               tmp = format (0, "-%s%c", #b, 0);         \
1078               vec_add1 (conf->eal_init_args, tmp);      \
1079               vec_add1 (s, 0);                          \
1080               vec_add1 (conf->eal_init_args, s);        \
1081             }
1082         foreach_eal_single_hyphen_arg
1083 #undef _
1084 #define _(a,b)                                          \
1085             else if (unformat(input, #a " %s", &s))     \
1086               {                                         \
1087                 tmp = format (0, "-%s%c", #b, 0);       \
1088                 vec_add1 (conf->eal_init_args, tmp);    \
1089                 vec_add1 (s, 0);                        \
1090                 vec_add1 (conf->eal_init_args, s);      \
1091                 conf->a##_set_manually = 1;             \
1092               }
1093         foreach_eal_single_hyphen_mandatory_arg
1094 #undef _
1095         else if (unformat (input, "default"))
1096         ;
1097
1098       else if (unformat_skip_white_space (input))
1099         ;
1100       else
1101         {
1102           error = clib_error_return (0, "unknown input `%U'",
1103                                      format_unformat_error, input);
1104           goto done;
1105         }
1106     }
1107
1108   if (!conf->uio_driver_name)
1109     conf->uio_driver_name = format (0, "uio_pci_generic%c", 0);
1110
1111   /*
1112    * Use 1G huge pages if available.
1113    */
1114   if (!no_huge && !huge_dir)
1115     {
1116       u32 x, *mem_by_socket = 0;
1117       uword c = 0;
1118       u8 use_1g = 1;
1119       u8 use_2m = 1;
1120       u8 less_than_1g = 1;
1121       int rv;
1122
1123       umount (DEFAULT_HUGE_DIR);
1124
1125       /* Process "socket-mem" parameter value */
1126       if (vec_len (socket_mem))
1127         {
1128           unformat_input_t in;
1129           unformat_init_vector (&in, socket_mem);
1130           while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1131             {
1132               if (unformat (&in, "%u,", &x))
1133                 ;
1134               else if (unformat (&in, "%u", &x))
1135                 ;
1136               else if (unformat (&in, ","))
1137                 x = 0;
1138               else
1139                 break;
1140
1141               vec_add1 (mem_by_socket, x);
1142
1143               if (x > 1023)
1144                 less_than_1g = 0;
1145             }
1146           /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1147           unformat_free (&in);
1148           socket_mem = 0;
1149         }
1150       else
1151         {
1152           /* *INDENT-OFF* */
1153           clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1154             {
1155               vec_validate(mem_by_socket, c);
1156               mem_by_socket[c] = 256; /* default per-socket mem */
1157             }
1158           ));
1159           /* *INDENT-ON* */
1160         }
1161
1162       /* check if available enough 1GB pages for each socket */
1163       /* *INDENT-OFF* */
1164       clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1165         {
1166           int pages_avail, page_size, mem;
1167
1168           vec_validate(mem_by_socket, c);
1169           mem = mem_by_socket[c];
1170
1171           page_size = 1024;
1172           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1173
1174           if (pages_avail < 0 || page_size * pages_avail < mem)
1175             use_1g = 0;
1176
1177           page_size = 2;
1178           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1179
1180           if (pages_avail < 0 || page_size * pages_avail < mem)
1181             use_2m = 0;
1182       }));
1183       /* *INDENT-ON* */
1184
1185       if (mem_by_socket == 0)
1186         {
1187           error = clib_error_return (0, "mem_by_socket NULL");
1188           goto done;
1189         }
1190       _vec_len (mem_by_socket) = c + 1;
1191
1192       /* regenerate socket_mem string */
1193       vec_foreach_index (x, mem_by_socket)
1194         socket_mem = format (socket_mem, "%s%u",
1195                              socket_mem ? "," : "", mem_by_socket[x]);
1196       socket_mem = format (socket_mem, "%c", 0);
1197
1198       vec_free (mem_by_socket);
1199
1200       rv = mkdir (VPP_RUN_DIR, 0755);
1201       if (rv && errno != EEXIST)
1202         {
1203           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1204                                      VPP_RUN_DIR, errno);
1205           goto done;
1206         }
1207
1208       rv = mkdir (DEFAULT_HUGE_DIR, 0755);
1209       if (rv && errno != EEXIST)
1210         {
1211           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1212                                      DEFAULT_HUGE_DIR, errno);
1213           goto done;
1214         }
1215
1216       if (use_1g && !(less_than_1g && use_2m))
1217         {
1218           rv =
1219             mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1220         }
1221       else if (use_2m)
1222         {
1223           rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1224         }
1225       else
1226         {
1227           return clib_error_return (0, "not enough free huge pages");
1228         }
1229
1230       if (rv)
1231         {
1232           error = clib_error_return (0, "mount failed %d", errno);
1233           goto done;
1234         }
1235
1236       tmp = format (0, "--huge-dir%c", 0);
1237       vec_add1 (conf->eal_init_args, tmp);
1238       tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1239       vec_add1 (conf->eal_init_args, tmp);
1240       if (!file_prefix)
1241         {
1242           tmp = format (0, "--file-prefix%c", 0);
1243           vec_add1 (conf->eal_init_args, tmp);
1244           tmp = format (0, "vpp%c", 0);
1245           vec_add1 (conf->eal_init_args, tmp);
1246         }
1247     }
1248
1249   vec_free (rte_cmd);
1250   vec_free (ethname);
1251
1252   if (error)
1253     return error;
1254
1255   /* I'll bet that -c and -n must be the first and second args... */
1256   if (!conf->coremask_set_manually)
1257     {
1258       vlib_thread_registration_t *tr;
1259       uword *coremask = 0;
1260       int i;
1261
1262       /* main thread core */
1263       coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
1264
1265       for (i = 0; i < vec_len (tm->registrations); i++)
1266         {
1267           tr = tm->registrations[i];
1268           coremask = clib_bitmap_or (coremask, tr->coremask);
1269         }
1270
1271       vec_insert (conf->eal_init_args, 2, 1);
1272       conf->eal_init_args[1] = (u8 *) "-c";
1273       tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1274       conf->eal_init_args[2] = tmp;
1275       clib_bitmap_free (coremask);
1276     }
1277
1278   if (!conf->nchannels_set_manually)
1279     {
1280       vec_insert (conf->eal_init_args, 2, 3);
1281       conf->eal_init_args[3] = (u8 *) "-n";
1282       tmp = format (0, "%d", conf->nchannels);
1283       conf->eal_init_args[4] = tmp;
1284     }
1285
1286   if (no_pci == 0 && geteuid () == 0)
1287     dpdk_bind_devices_to_uio (conf);
1288
1289 #define _(x) \
1290     if (devconf->x == 0 && conf->default_devconf.x > 0) \
1291       devconf->x = conf->default_devconf.x ;
1292
1293   /* *INDENT-OFF* */
1294   pool_foreach (devconf, conf->dev_confs, ({
1295
1296     /* default per-device config items */
1297     foreach_dpdk_device_config_item
1298
1299     /* add DPDK EAL whitelist/blacklist entry */
1300     if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1301       {
1302         tmp = format (0, "-w%c", 0);
1303         vec_add1 (conf->eal_init_args, tmp);
1304         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1305         vec_add1 (conf->eal_init_args, tmp);
1306       }
1307     else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1308       {
1309         tmp = format (0, "-b%c", 0);
1310         vec_add1 (conf->eal_init_args, tmp);
1311         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1312         vec_add1 (conf->eal_init_args, tmp);
1313       }
1314   }));
1315   /* *INDENT-ON* */
1316
1317 #undef _
1318
1319   /* set master-lcore */
1320   tmp = format (0, "--master-lcore%c", 0);
1321   vec_add1 (conf->eal_init_args, tmp);
1322   tmp = format (0, "%u%c", tm->main_lcore, 0);
1323   vec_add1 (conf->eal_init_args, tmp);
1324
1325   /* set socket-mem */
1326   tmp = format (0, "--socket-mem%c", 0);
1327   vec_add1 (conf->eal_init_args, tmp);
1328   tmp = format (0, "%s%c", socket_mem, 0);
1329   vec_add1 (conf->eal_init_args, tmp);
1330
1331   /* NULL terminate the "argv" vector, in case of stupidity */
1332   vec_add1 (conf->eal_init_args, 0);
1333   _vec_len (conf->eal_init_args) -= 1;
1334
1335   /* Set up DPDK eal and packet mbuf pool early. */
1336
1337   log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1338
1339   rte_set_log_level (log_level);
1340
1341   vm = vlib_get_main ();
1342
1343   /* make copy of args as rte_eal_init tends to mess up with arg array */
1344   for (i = 1; i < vec_len (conf->eal_init_args); i++)
1345     conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1346                                       conf->eal_init_args[i]);
1347
1348   ret =
1349     rte_eal_init (vec_len (conf->eal_init_args),
1350                   (char **) conf->eal_init_args);
1351
1352   /* lazy umount hugepages */
1353   umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
1354
1355   if (ret < 0)
1356     return clib_error_return (0, "rte_eal_init returned %d", ret);
1357
1358   /* Dump the physical memory layout prior to creating the mbuf_pool */
1359   fprintf (stdout, "DPDK physical memory layout:\n");
1360   rte_dump_physmem_layout (stdout);
1361
1362   /* main thread 1st */
1363   error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
1364   if (error)
1365     return error;
1366
1367   for (i = 0; i < RTE_MAX_LCORE; i++)
1368     {
1369       error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1370                                        rte_lcore_to_socket_id (i));
1371       if (error)
1372         return error;
1373     }
1374
1375 done:
1376   return error;
1377 }
1378
1379 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1380
1381 void
1382 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1383 {
1384   vnet_main_t *vnm = vnet_get_main ();
1385   struct rte_eth_link prev_link = xd->link;
1386   u32 hw_flags = 0;
1387   u8 hw_flags_chg = 0;
1388
1389   /* only update link state for PMD interfaces */
1390   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1391     return;
1392
1393   xd->time_last_link_update = now ? now : xd->time_last_link_update;
1394   memset (&xd->link, 0, sizeof (xd->link));
1395   rte_eth_link_get_nowait (xd->device_index, &xd->link);
1396
1397   if (LINK_STATE_ELOGS)
1398     {
1399       vlib_main_t *vm = vlib_get_main ();
1400       ELOG_TYPE_DECLARE (e) =
1401       {
1402       .format =
1403           "update-link-state: sw_if_index %d, admin_up %d,"
1404           "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1405
1406       struct
1407       {
1408         u32 sw_if_index;
1409         u8 admin_up;
1410         u8 old_link_state;
1411         u8 new_link_state;
1412       } *ed;
1413       ed = ELOG_DATA (&vm->elog_main, e);
1414       ed->sw_if_index = xd->vlib_sw_if_index;
1415       ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1416       ed->old_link_state = (u8)
1417         vnet_hw_interface_is_link_up (vnm, xd->hw_if_index);
1418       ed->new_link_state = (u8) xd->link.link_status;
1419     }
1420
1421   if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
1422       ((xd->link.link_status != 0) ^
1423        vnet_hw_interface_is_link_up (vnm, xd->hw_if_index)))
1424     {
1425       hw_flags_chg = 1;
1426       hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1427     }
1428
1429   if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1430     {
1431       hw_flags_chg = 1;
1432       switch (xd->link.link_duplex)
1433         {
1434         case ETH_LINK_HALF_DUPLEX:
1435           hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1436           break;
1437         case ETH_LINK_FULL_DUPLEX:
1438           hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1439           break;
1440         default:
1441           break;
1442         }
1443     }
1444   if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1445     {
1446       hw_flags_chg = 1;
1447       switch (xd->link.link_speed)
1448         {
1449         case ETH_SPEED_NUM_10M:
1450           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1451           break;
1452         case ETH_SPEED_NUM_100M:
1453           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1454           break;
1455         case ETH_SPEED_NUM_1G:
1456           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1457           break;
1458         case ETH_SPEED_NUM_10G:
1459           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1460           break;
1461         case ETH_SPEED_NUM_40G:
1462           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1463           break;
1464         case 0:
1465           break;
1466         default:
1467           clib_warning ("unknown link speed %d", xd->link.link_speed);
1468           break;
1469         }
1470     }
1471   if (hw_flags_chg)
1472     {
1473       if (LINK_STATE_ELOGS)
1474         {
1475           vlib_main_t *vm = vlib_get_main ();
1476
1477           ELOG_TYPE_DECLARE (e) =
1478           {
1479           .format =
1480               "update-link-state: sw_if_index %d, new flags %d",.format_args
1481               = "i4i4",};
1482
1483           struct
1484           {
1485             u32 sw_if_index;
1486             u32 flags;
1487           } *ed;
1488           ed = ELOG_DATA (&vm->elog_main, e);
1489           ed->sw_if_index = xd->vlib_sw_if_index;
1490           ed->flags = hw_flags;
1491         }
1492       vnet_hw_interface_set_flags (vnm, xd->hw_if_index, hw_flags);
1493     }
1494 }
1495
1496 static uword
1497 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1498 {
1499   clib_error_t *error;
1500   vnet_main_t *vnm = vnet_get_main ();
1501   dpdk_main_t *dm = &dpdk_main;
1502   ethernet_main_t *em = &ethernet_main;
1503   dpdk_device_t *xd;
1504   vlib_thread_main_t *tm = vlib_get_thread_main ();
1505   int i;
1506
1507   error = dpdk_lib_init (dm);
1508
1509   if (error)
1510     clib_error_report (error);
1511
1512   tm->worker_thread_release = 1;
1513
1514   f64 now = vlib_time_now (vm);
1515   vec_foreach (xd, dm->devices)
1516   {
1517     dpdk_update_link_state (xd, now);
1518   }
1519
1520   {
1521     /*
1522      * Extra set up for bond interfaces:
1523      *  1. Setup MACs for bond interfaces and their slave links which was set
1524      *     in dpdk_port_setup() but needs to be done again here to take effect.
1525      *  2. Set up info for bond interface related CLI support.
1526      */
1527     int nports = rte_eth_dev_count ();
1528     if (nports > 0)
1529       {
1530         for (i = 0; i < nports; i++)
1531           {
1532             xd = &dm->devices[i];
1533             ASSERT (i == xd->device_index);
1534             if (xd->pmd == VNET_DPDK_PMD_BOND)
1535               {
1536                 u8 addr[6];
1537                 u8 slink[16];
1538                 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1539                 if (nlink > 0)
1540                   {
1541                     vnet_hw_interface_t *bhi;
1542                     ethernet_interface_t *bei;
1543                     int rv;
1544
1545                     /* Get MAC of 1st slave link */
1546                     rte_eth_macaddr_get
1547                       (slink[0], (struct ether_addr *) addr);
1548
1549                     /* Set MAC of bounded interface to that of 1st slave link */
1550                     clib_warning ("Set MAC for bond dev# %d", i);
1551                     rv = rte_eth_bond_mac_address_set
1552                       (i, (struct ether_addr *) addr);
1553                     if (rv)
1554                       clib_warning ("Set MAC addr failure rv=%d", rv);
1555
1556                     /* Populate MAC of bonded interface in VPP hw tables */
1557                     bhi = vnet_get_hw_interface
1558                       (vnm, dm->devices[i].hw_if_index);
1559                     bei = pool_elt_at_index
1560                       (em->interfaces, bhi->hw_instance);
1561                     clib_memcpy (bhi->hw_address, addr, 6);
1562                     clib_memcpy (bei->address, addr, 6);
1563
1564                     /* Init l3 packet size allowed on bonded interface */
1565                     bhi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
1566                     bhi->max_l3_packet_bytes[VLIB_RX] =
1567                       bhi->max_l3_packet_bytes[VLIB_TX] =
1568                       ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1569                     while (nlink >= 1)
1570                       {         /* for all slave links */
1571                         int slave = slink[--nlink];
1572                         dpdk_device_t *sdev = &dm->devices[slave];
1573                         vnet_hw_interface_t *shi;
1574                         vnet_sw_interface_t *ssi;
1575                         ethernet_interface_t *sei;
1576                         /* Add MAC to all slave links except the first one */
1577                         if (nlink)
1578                           {
1579                             clib_warning ("Add MAC for slave dev# %d", slave);
1580                             rv = rte_eth_dev_mac_addr_add
1581                               (slave, (struct ether_addr *) addr, 0);
1582                             if (rv)
1583                               clib_warning ("Add MAC addr failure rv=%d", rv);
1584                           }
1585                         /* Set slaves bitmap for bonded interface */
1586                         bhi->bond_info = clib_bitmap_set
1587                           (bhi->bond_info, sdev->hw_if_index, 1);
1588                         /* Set slave link flags on slave interface */
1589                         shi = vnet_get_hw_interface (vnm, sdev->hw_if_index);
1590                         ssi = vnet_get_sw_interface
1591                           (vnm, sdev->vlib_sw_if_index);
1592                         sei = pool_elt_at_index
1593                           (em->interfaces, shi->hw_instance);
1594
1595                         shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1596                         ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1597                         clib_memcpy (shi->hw_address, addr, 6);
1598                         clib_memcpy (sei->address, addr, 6);
1599
1600                         /* Set l3 packet size allowed as the lowest of slave */
1601                         if (bhi->max_l3_packet_bytes[VLIB_RX] >
1602                             shi->max_l3_packet_bytes[VLIB_RX])
1603                           bhi->max_l3_packet_bytes[VLIB_RX] =
1604                             bhi->max_l3_packet_bytes[VLIB_TX] =
1605                             shi->max_l3_packet_bytes[VLIB_RX];
1606
1607                         /* Set max packet size allowed as the lowest of slave */
1608                         if (bhi->max_packet_bytes > shi->max_packet_bytes)
1609                           bhi->max_packet_bytes = shi->max_packet_bytes;
1610                       }
1611                   }
1612               }
1613           }
1614       }
1615   }
1616
1617   while (1)
1618     {
1619       /*
1620        * check each time through the loop in case intervals are changed
1621        */
1622       f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1623         dm->link_state_poll_interval : dm->stat_poll_interval;
1624
1625       vlib_process_wait_for_event_or_clock (vm, min_wait);
1626
1627       if (dm->admin_up_down_in_progress)
1628         /* skip the poll if an admin up down is in progress (on any interface) */
1629         continue;
1630
1631       vec_foreach (xd, dm->devices)
1632       {
1633         f64 now = vlib_time_now (vm);
1634         if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1635           dpdk_update_counters (xd, now);
1636         if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1637           dpdk_update_link_state (xd, now);
1638
1639       }
1640     }
1641
1642   return 0;
1643 }
1644
1645 /* *INDENT-OFF* */
1646 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1647     .function = dpdk_process,
1648     .type = VLIB_NODE_TYPE_PROCESS,
1649     .name = "dpdk-process",
1650     .process_log2_n_stack_bytes = 17,
1651 };
1652 /* *INDENT-ON* */
1653
1654 static clib_error_t *
1655 dpdk_init (vlib_main_t * vm)
1656 {
1657   dpdk_main_t *dm = &dpdk_main;
1658   vlib_node_t *ei;
1659   clib_error_t *error = 0;
1660   vlib_thread_main_t *tm = vlib_get_thread_main ();
1661
1662   /* verify that structs are cacheline aligned */
1663   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1664                  "Cache line marker must be 1st element in dpdk_device_t");
1665   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1666                  CLIB_CACHE_LINE_BYTES,
1667                  "Data in cache line 0 is bigger than cache line size");
1668   STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1669                  "Cache line marker must be 1st element in frame_queue_trace_t");
1670
1671   dm->vlib_main = vm;
1672   dm->vnet_main = vnet_get_main ();
1673   dm->conf = &dpdk_config_main;
1674
1675   ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1676   if (ei == 0)
1677     return clib_error_return (0, "ethernet-input node AWOL");
1678
1679   dm->ethernet_input_node_index = ei->index;
1680
1681   dm->conf->nchannels = 4;
1682   dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1683   vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1684
1685   dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1686   dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1687   dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
1688
1689   /* $$$ use n_thread_stacks since it's known-good at this point */
1690   vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1691
1692   /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1693   dm->buffer_flags_template =
1694     (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_EXT_HDR_VALID
1695      | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
1696
1697   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1698   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1699
1700   /* init CLI */
1701   if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1702     return error;
1703
1704   return error;
1705 }
1706
1707 VLIB_INIT_FUNCTION (dpdk_init);
1708
1709
1710 /*
1711  * fd.io coding-style-patch-verification: ON
1712  *
1713  * Local Variables:
1714  * eval: (c-set-style "gnu")
1715  * End:
1716  */