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