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