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