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