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