dpdk: remove unused code
[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, "dev default %U", unformat_vlib_cli_sub_input,
999                          &sub_input))
1000         {
1001           error =
1002             dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
1003                                 1);
1004
1005           if (error)
1006             return error;
1007         }
1008       else
1009         if (unformat
1010             (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1011              unformat_vlib_cli_sub_input, &sub_input))
1012         {
1013           error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1014
1015           if (error)
1016             return error;
1017
1018           num_whitelisted++;
1019         }
1020       else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1021         {
1022           error = dpdk_device_config (conf, pci_addr, 0, 0);
1023
1024           if (error)
1025             return error;
1026
1027           num_whitelisted++;
1028         }
1029       else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
1030         ;
1031       else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1032         ;
1033       else if (unformat (input, "socket-mem %s", &socket_mem))
1034         ;
1035       else if (unformat (input, "no-pci"))
1036         {
1037           no_pci = 1;
1038           tmp = format (0, "--no-pci%c", 0);
1039           vec_add1 (conf->eal_init_args, tmp);
1040         }
1041       else if (unformat (input, "poll-sleep %d", &dm->poll_sleep_usec))
1042         ;
1043
1044 #define _(a)                                    \
1045       else if (unformat(input, #a))             \
1046         {                                       \
1047           tmp = format (0, "--%s%c", #a, 0);    \
1048           vec_add1 (conf->eal_init_args, tmp);    \
1049         }
1050       foreach_eal_double_hyphen_predicate_arg
1051 #undef _
1052 #define _(a)                                          \
1053         else if (unformat(input, #a " %s", &s))       \
1054           {                                           \
1055             if (!strncmp(#a, "huge-dir", 8))          \
1056               huge_dir = 1;                           \
1057             else if (!strncmp(#a, "file-prefix", 11)) \
1058               file_prefix = 1;                        \
1059             tmp = format (0, "--%s%c", #a, 0);        \
1060             vec_add1 (conf->eal_init_args, tmp);      \
1061             vec_add1 (s, 0);                          \
1062             if (!strncmp(#a, "vdev", 4))              \
1063               if (strstr((char*)s, "af_packet"))      \
1064                 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
1065             vec_add1 (conf->eal_init_args, s);        \
1066           }
1067         foreach_eal_double_hyphen_arg
1068 #undef _
1069 #define _(a,b)                                          \
1070           else if (unformat(input, #a " %s", &s))       \
1071             {                                           \
1072               tmp = format (0, "-%s%c", #b, 0);         \
1073               vec_add1 (conf->eal_init_args, tmp);      \
1074               vec_add1 (s, 0);                          \
1075               vec_add1 (conf->eal_init_args, s);        \
1076             }
1077         foreach_eal_single_hyphen_arg
1078 #undef _
1079 #define _(a,b)                                          \
1080             else if (unformat(input, #a " %s", &s))     \
1081               {                                         \
1082                 tmp = format (0, "-%s%c", #b, 0);       \
1083                 vec_add1 (conf->eal_init_args, tmp);    \
1084                 vec_add1 (s, 0);                        \
1085                 vec_add1 (conf->eal_init_args, s);      \
1086                 conf->a##_set_manually = 1;             \
1087               }
1088         foreach_eal_single_hyphen_mandatory_arg
1089 #undef _
1090         else if (unformat (input, "default"))
1091         ;
1092
1093       else if (unformat_skip_white_space (input))
1094         ;
1095       else
1096         {
1097           error = clib_error_return (0, "unknown input `%U'",
1098                                      format_unformat_error, input);
1099           goto done;
1100         }
1101     }
1102
1103   if (!conf->uio_driver_name)
1104     conf->uio_driver_name = format (0, "uio_pci_generic%c", 0);
1105
1106   /*
1107    * Use 1G huge pages if available.
1108    */
1109   if (!no_huge && !huge_dir)
1110     {
1111       u32 x, *mem_by_socket = 0;
1112       uword c = 0;
1113       u8 use_1g = 1;
1114       u8 use_2m = 1;
1115       u8 less_than_1g = 1;
1116       int rv;
1117
1118       umount (DEFAULT_HUGE_DIR);
1119
1120       /* Process "socket-mem" parameter value */
1121       if (vec_len (socket_mem))
1122         {
1123           unformat_input_t in;
1124           unformat_init_vector (&in, socket_mem);
1125           while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1126             {
1127               if (unformat (&in, "%u,", &x))
1128                 ;
1129               else if (unformat (&in, "%u", &x))
1130                 ;
1131               else if (unformat (&in, ","))
1132                 x = 0;
1133               else
1134                 break;
1135
1136               vec_add1 (mem_by_socket, x);
1137
1138               if (x > 1023)
1139                 less_than_1g = 0;
1140             }
1141           /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1142           unformat_free (&in);
1143           socket_mem = 0;
1144         }
1145       else
1146         {
1147           /* *INDENT-OFF* */
1148           clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1149             {
1150               vec_validate(mem_by_socket, c);
1151               mem_by_socket[c] = 256; /* default per-socket mem */
1152             }
1153           ));
1154           /* *INDENT-ON* */
1155         }
1156
1157       /* check if available enough 1GB pages for each socket */
1158       /* *INDENT-OFF* */
1159       clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1160         {
1161           int pages_avail, page_size, mem;
1162
1163           vec_validate(mem_by_socket, c);
1164           mem = mem_by_socket[c];
1165
1166           page_size = 1024;
1167           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1168
1169           if (pages_avail < 0 || page_size * pages_avail < mem)
1170             use_1g = 0;
1171
1172           page_size = 2;
1173           pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1174
1175           if (pages_avail < 0 || page_size * pages_avail < mem)
1176             use_2m = 0;
1177       }));
1178       /* *INDENT-ON* */
1179
1180       if (mem_by_socket == 0)
1181         {
1182           error = clib_error_return (0, "mem_by_socket NULL");
1183           goto done;
1184         }
1185       _vec_len (mem_by_socket) = c + 1;
1186
1187       /* regenerate socket_mem string */
1188       vec_foreach_index (x, mem_by_socket)
1189         socket_mem = format (socket_mem, "%s%u",
1190                              socket_mem ? "," : "", mem_by_socket[x]);
1191       socket_mem = format (socket_mem, "%c", 0);
1192
1193       vec_free (mem_by_socket);
1194
1195       rv = mkdir (VPP_RUN_DIR, 0755);
1196       if (rv && errno != EEXIST)
1197         {
1198           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1199                                      VPP_RUN_DIR, errno);
1200           goto done;
1201         }
1202
1203       rv = mkdir (DEFAULT_HUGE_DIR, 0755);
1204       if (rv && errno != EEXIST)
1205         {
1206           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1207                                      DEFAULT_HUGE_DIR, errno);
1208           goto done;
1209         }
1210
1211       if (use_1g && !(less_than_1g && use_2m))
1212         {
1213           rv =
1214             mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1215         }
1216       else if (use_2m)
1217         {
1218           rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1219         }
1220       else
1221         {
1222           return clib_error_return (0, "not enough free huge pages");
1223         }
1224
1225       if (rv)
1226         {
1227           error = clib_error_return (0, "mount failed %d", errno);
1228           goto done;
1229         }
1230
1231       tmp = format (0, "--huge-dir%c", 0);
1232       vec_add1 (conf->eal_init_args, tmp);
1233       tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1234       vec_add1 (conf->eal_init_args, tmp);
1235       if (!file_prefix)
1236         {
1237           tmp = format (0, "--file-prefix%c", 0);
1238           vec_add1 (conf->eal_init_args, tmp);
1239           tmp = format (0, "vpp%c", 0);
1240           vec_add1 (conf->eal_init_args, tmp);
1241         }
1242     }
1243
1244   vec_free (rte_cmd);
1245   vec_free (ethname);
1246
1247   if (error)
1248     return error;
1249
1250   /* I'll bet that -c and -n must be the first and second args... */
1251   if (!conf->coremask_set_manually)
1252     {
1253       vlib_thread_registration_t *tr;
1254       uword *coremask = 0;
1255       int i;
1256
1257       /* main thread core */
1258       coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
1259
1260       for (i = 0; i < vec_len (tm->registrations); i++)
1261         {
1262           tr = tm->registrations[i];
1263           coremask = clib_bitmap_or (coremask, tr->coremask);
1264         }
1265
1266       vec_insert (conf->eal_init_args, 2, 1);
1267       conf->eal_init_args[1] = (u8 *) "-c";
1268       tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1269       conf->eal_init_args[2] = tmp;
1270       clib_bitmap_free (coremask);
1271     }
1272
1273   if (!conf->nchannels_set_manually)
1274     {
1275       vec_insert (conf->eal_init_args, 2, 3);
1276       conf->eal_init_args[3] = (u8 *) "-n";
1277       tmp = format (0, "%d", conf->nchannels);
1278       conf->eal_init_args[4] = tmp;
1279     }
1280
1281   if (no_pci == 0 && geteuid () == 0)
1282     dpdk_bind_devices_to_uio (conf);
1283
1284 #define _(x) \
1285     if (devconf->x == 0 && conf->default_devconf.x > 0) \
1286       devconf->x = conf->default_devconf.x ;
1287
1288   /* *INDENT-OFF* */
1289   pool_foreach (devconf, conf->dev_confs, ({
1290
1291     /* default per-device config items */
1292     foreach_dpdk_device_config_item
1293
1294     /* add DPDK EAL whitelist/blacklist entry */
1295     if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1296       {
1297         tmp = format (0, "-w%c", 0);
1298         vec_add1 (conf->eal_init_args, tmp);
1299         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1300         vec_add1 (conf->eal_init_args, tmp);
1301       }
1302     else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1303       {
1304         tmp = format (0, "-b%c", 0);
1305         vec_add1 (conf->eal_init_args, tmp);
1306         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1307         vec_add1 (conf->eal_init_args, tmp);
1308       }
1309   }));
1310   /* *INDENT-ON* */
1311
1312 #undef _
1313
1314   /* set master-lcore */
1315   tmp = format (0, "--master-lcore%c", 0);
1316   vec_add1 (conf->eal_init_args, tmp);
1317   tmp = format (0, "%u%c", tm->main_lcore, 0);
1318   vec_add1 (conf->eal_init_args, tmp);
1319
1320   /* set socket-mem */
1321   tmp = format (0, "--socket-mem%c", 0);
1322   vec_add1 (conf->eal_init_args, tmp);
1323   tmp = format (0, "%s%c", socket_mem, 0);
1324   vec_add1 (conf->eal_init_args, tmp);
1325
1326   /* NULL terminate the "argv" vector, in case of stupidity */
1327   vec_add1 (conf->eal_init_args, 0);
1328   _vec_len (conf->eal_init_args) -= 1;
1329
1330   /* Set up DPDK eal and packet mbuf pool early. */
1331
1332   log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1333
1334   rte_set_log_level (log_level);
1335
1336   vm = vlib_get_main ();
1337
1338   /* make copy of args as rte_eal_init tends to mess up with arg array */
1339   for (i = 1; i < vec_len (conf->eal_init_args); i++)
1340     conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1341                                       conf->eal_init_args[i]);
1342
1343   ret =
1344     rte_eal_init (vec_len (conf->eal_init_args),
1345                   (char **) conf->eal_init_args);
1346
1347   /* lazy umount hugepages */
1348   umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
1349
1350   if (ret < 0)
1351     return clib_error_return (0, "rte_eal_init returned %d", ret);
1352
1353   /* Dump the physical memory layout prior to creating the mbuf_pool */
1354   fprintf (stdout, "DPDK physical memory layout:\n");
1355   rte_dump_physmem_layout (stdout);
1356
1357   /* main thread 1st */
1358   error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
1359   if (error)
1360     return error;
1361
1362   for (i = 0; i < RTE_MAX_LCORE; i++)
1363     {
1364       error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1365                                        rte_lcore_to_socket_id (i));
1366       if (error)
1367         return error;
1368     }
1369
1370 done:
1371   return error;
1372 }
1373
1374 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1375
1376 void
1377 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1378 {
1379   vnet_main_t *vnm = vnet_get_main ();
1380   struct rte_eth_link prev_link = xd->link;
1381   u32 hw_flags = 0;
1382   u8 hw_flags_chg = 0;
1383
1384   /* only update link state for PMD interfaces */
1385   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1386     return;
1387
1388   xd->time_last_link_update = now ? now : xd->time_last_link_update;
1389   memset (&xd->link, 0, sizeof (xd->link));
1390   rte_eth_link_get_nowait (xd->device_index, &xd->link);
1391
1392   if (LINK_STATE_ELOGS)
1393     {
1394       vlib_main_t *vm = vlib_get_main ();
1395       ELOG_TYPE_DECLARE (e) =
1396       {
1397       .format =
1398           "update-link-state: sw_if_index %d, admin_up %d,"
1399           "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1400
1401       struct
1402       {
1403         u32 sw_if_index;
1404         u8 admin_up;
1405         u8 old_link_state;
1406         u8 new_link_state;
1407       } *ed;
1408       ed = ELOG_DATA (&vm->elog_main, e);
1409       ed->sw_if_index = xd->vlib_sw_if_index;
1410       ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1411       ed->old_link_state = (u8)
1412         vnet_hw_interface_is_link_up (vnm, xd->hw_if_index);
1413       ed->new_link_state = (u8) xd->link.link_status;
1414     }
1415
1416   if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
1417       ((xd->link.link_status != 0) ^
1418        vnet_hw_interface_is_link_up (vnm, xd->hw_if_index)))
1419     {
1420       hw_flags_chg = 1;
1421       hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1422     }
1423
1424   if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1425     {
1426       hw_flags_chg = 1;
1427       switch (xd->link.link_duplex)
1428         {
1429         case ETH_LINK_HALF_DUPLEX:
1430           hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1431           break;
1432         case ETH_LINK_FULL_DUPLEX:
1433           hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1434           break;
1435         default:
1436           break;
1437         }
1438     }
1439   if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1440     {
1441       hw_flags_chg = 1;
1442       switch (xd->link.link_speed)
1443         {
1444         case ETH_SPEED_NUM_10M:
1445           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1446           break;
1447         case ETH_SPEED_NUM_100M:
1448           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1449           break;
1450         case ETH_SPEED_NUM_1G:
1451           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1452           break;
1453         case ETH_SPEED_NUM_10G:
1454           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1455           break;
1456         case ETH_SPEED_NUM_40G:
1457           hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1458           break;
1459         case 0:
1460           break;
1461         default:
1462           clib_warning ("unknown link speed %d", xd->link.link_speed);
1463           break;
1464         }
1465     }
1466   if (hw_flags_chg)
1467     {
1468       if (LINK_STATE_ELOGS)
1469         {
1470           vlib_main_t *vm = vlib_get_main ();
1471
1472           ELOG_TYPE_DECLARE (e) =
1473           {
1474           .format =
1475               "update-link-state: sw_if_index %d, new flags %d",.format_args
1476               = "i4i4",};
1477
1478           struct
1479           {
1480             u32 sw_if_index;
1481             u32 flags;
1482           } *ed;
1483           ed = ELOG_DATA (&vm->elog_main, e);
1484           ed->sw_if_index = xd->vlib_sw_if_index;
1485           ed->flags = hw_flags;
1486         }
1487       vnet_hw_interface_set_flags (vnm, xd->hw_if_index, hw_flags);
1488     }
1489 }
1490
1491 static uword
1492 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1493 {
1494   clib_error_t *error;
1495   vnet_main_t *vnm = vnet_get_main ();
1496   dpdk_main_t *dm = &dpdk_main;
1497   ethernet_main_t *em = &ethernet_main;
1498   dpdk_device_t *xd;
1499   vlib_thread_main_t *tm = vlib_get_thread_main ();
1500   int i;
1501
1502   error = dpdk_lib_init (dm);
1503
1504   if (error)
1505     clib_error_report (error);
1506
1507   tm->worker_thread_release = 1;
1508
1509   f64 now = vlib_time_now (vm);
1510   vec_foreach (xd, dm->devices)
1511   {
1512     dpdk_update_link_state (xd, now);
1513   }
1514
1515   {
1516     /*
1517      * Extra set up for bond interfaces:
1518      *  1. Setup MACs for bond interfaces and their slave links which was set
1519      *     in dpdk_port_setup() but needs to be done again here to take effect.
1520      *  2. Set up info for bond interface related CLI support.
1521      */
1522     int nports = rte_eth_dev_count ();
1523     if (nports > 0)
1524       {
1525         for (i = 0; i < nports; i++)
1526           {
1527             xd = &dm->devices[i];
1528             ASSERT (i == xd->device_index);
1529             if (xd->pmd == VNET_DPDK_PMD_BOND)
1530               {
1531                 u8 addr[6];
1532                 u8 slink[16];
1533                 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1534                 if (nlink > 0)
1535                   {
1536                     vnet_hw_interface_t *bhi;
1537                     ethernet_interface_t *bei;
1538                     int rv;
1539
1540                     /* Get MAC of 1st slave link */
1541                     rte_eth_macaddr_get
1542                       (slink[0], (struct ether_addr *) addr);
1543
1544                     /* Set MAC of bounded interface to that of 1st slave link */
1545                     clib_warning ("Set MAC for bond dev# %d", i);
1546                     rv = rte_eth_bond_mac_address_set
1547                       (i, (struct ether_addr *) addr);
1548                     if (rv)
1549                       clib_warning ("Set MAC addr failure rv=%d", rv);
1550
1551                     /* Populate MAC of bonded interface in VPP hw tables */
1552                     bhi = vnet_get_hw_interface
1553                       (vnm, dm->devices[i].hw_if_index);
1554                     bei = pool_elt_at_index
1555                       (em->interfaces, bhi->hw_instance);
1556                     clib_memcpy (bhi->hw_address, addr, 6);
1557                     clib_memcpy (bei->address, addr, 6);
1558
1559                     /* Init l3 packet size allowed on bonded interface */
1560                     bhi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
1561                     bhi->max_l3_packet_bytes[VLIB_RX] =
1562                       bhi->max_l3_packet_bytes[VLIB_TX] =
1563                       ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1564                     while (nlink >= 1)
1565                       {         /* for all slave links */
1566                         int slave = slink[--nlink];
1567                         dpdk_device_t *sdev = &dm->devices[slave];
1568                         vnet_hw_interface_t *shi;
1569                         vnet_sw_interface_t *ssi;
1570                         ethernet_interface_t *sei;
1571                         /* Add MAC to all slave links except the first one */
1572                         if (nlink)
1573                           {
1574                             clib_warning ("Add MAC for slave dev# %d", slave);
1575                             rv = rte_eth_dev_mac_addr_add
1576                               (slave, (struct ether_addr *) addr, 0);
1577                             if (rv)
1578                               clib_warning ("Add MAC addr failure rv=%d", rv);
1579                           }
1580                         /* Set slaves bitmap for bonded interface */
1581                         bhi->bond_info = clib_bitmap_set
1582                           (bhi->bond_info, sdev->hw_if_index, 1);
1583                         /* Set slave link flags on slave interface */
1584                         shi = vnet_get_hw_interface (vnm, sdev->hw_if_index);
1585                         ssi = vnet_get_sw_interface
1586                           (vnm, sdev->vlib_sw_if_index);
1587                         sei = pool_elt_at_index
1588                           (em->interfaces, shi->hw_instance);
1589
1590                         shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1591                         ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1592                         clib_memcpy (shi->hw_address, addr, 6);
1593                         clib_memcpy (sei->address, addr, 6);
1594
1595                         /* Set l3 packet size allowed as the lowest of slave */
1596                         if (bhi->max_l3_packet_bytes[VLIB_RX] >
1597                             shi->max_l3_packet_bytes[VLIB_RX])
1598                           bhi->max_l3_packet_bytes[VLIB_RX] =
1599                             bhi->max_l3_packet_bytes[VLIB_TX] =
1600                             shi->max_l3_packet_bytes[VLIB_RX];
1601
1602                         /* Set max packet size allowed as the lowest of slave */
1603                         if (bhi->max_packet_bytes > shi->max_packet_bytes)
1604                           bhi->max_packet_bytes = shi->max_packet_bytes;
1605                       }
1606                   }
1607               }
1608           }
1609       }
1610   }
1611
1612   while (1)
1613     {
1614       /*
1615        * check each time through the loop in case intervals are changed
1616        */
1617       f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1618         dm->link_state_poll_interval : dm->stat_poll_interval;
1619
1620       vlib_process_wait_for_event_or_clock (vm, min_wait);
1621
1622       if (dm->admin_up_down_in_progress)
1623         /* skip the poll if an admin up down is in progress (on any interface) */
1624         continue;
1625
1626       vec_foreach (xd, dm->devices)
1627       {
1628         f64 now = vlib_time_now (vm);
1629         if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1630           dpdk_update_counters (xd, now);
1631         if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1632           dpdk_update_link_state (xd, now);
1633
1634       }
1635     }
1636
1637   return 0;
1638 }
1639
1640 /* *INDENT-OFF* */
1641 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1642     .function = dpdk_process,
1643     .type = VLIB_NODE_TYPE_PROCESS,
1644     .name = "dpdk-process",
1645     .process_log2_n_stack_bytes = 17,
1646 };
1647 /* *INDENT-ON* */
1648
1649 static clib_error_t *
1650 dpdk_init (vlib_main_t * vm)
1651 {
1652   dpdk_main_t *dm = &dpdk_main;
1653   vlib_node_t *ei;
1654   clib_error_t *error = 0;
1655   vlib_thread_main_t *tm = vlib_get_thread_main ();
1656
1657   /* verify that structs are cacheline aligned */
1658   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1659                  "Cache line marker must be 1st element in dpdk_device_t");
1660   STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1661                  CLIB_CACHE_LINE_BYTES,
1662                  "Data in cache line 0 is bigger than cache line size");
1663   STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1664                  "Cache line marker must be 1st element in frame_queue_trace_t");
1665
1666   dm->vlib_main = vm;
1667   dm->vnet_main = vnet_get_main ();
1668   dm->conf = &dpdk_config_main;
1669
1670   ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1671   if (ei == 0)
1672     return clib_error_return (0, "ethernet-input node AWOL");
1673
1674   dm->ethernet_input_node_index = ei->index;
1675
1676   dm->conf->nchannels = 4;
1677   dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1678   vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1679
1680   vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1681
1682   /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1683   dm->buffer_flags_template =
1684     (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_EXT_HDR_VALID
1685      | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
1686
1687   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1688   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1689
1690   /* init CLI */
1691   if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1692     return error;
1693
1694   return error;
1695 }
1696
1697 VLIB_INIT_FUNCTION (dpdk_init);
1698
1699
1700 /*
1701  * fd.io coding-style-patch-verification: ON
1702  *
1703  * Local Variables:
1704  * eval: (c-set-style "gnu")
1705  * End:
1706  */