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