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