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