Fix coverity issues in DPDK code, fixes VPP-189
[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, "no-pci"))
1046         {
1047           no_pci = 1;
1048           tmp = format (0, "--no-pci%c", 0);
1049           vec_add1 (conf->eal_init_args, tmp);
1050         }
1051       else if (unformat (input, "poll-sleep %d", &dm->poll_sleep))
1052         ;
1053
1054 #define _(a)                                    \
1055       else if (unformat(input, #a))             \
1056         {                                       \
1057           tmp = format (0, "--%s%c", #a, 0);    \
1058           vec_add1 (conf->eal_init_args, tmp);    \
1059         }
1060       foreach_eal_double_hyphen_predicate_arg
1061 #undef _
1062
1063 #define _(a)                                          \
1064         else if (unformat(input, #a " %s", &s))       \
1065           {                                           \
1066             if (!strncmp(#a, "huge-dir", 8))          \
1067               huge_dir = 1;                           \
1068             else if (!strncmp(#a, "file-prefix", 11)) \
1069               file_prefix = 1;                        \
1070             tmp = format (0, "--%s%c", #a, 0);        \
1071             vec_add1 (conf->eal_init_args, tmp);      \
1072             vec_add1 (s, 0);                          \
1073             vec_add1 (conf->eal_init_args, s);        \
1074           }
1075         foreach_eal_double_hyphen_arg
1076 #undef _
1077
1078 #define _(a,b)                                          \
1079           else if (unformat(input, #a " %s", &s))       \
1080             {                                           \
1081               tmp = format (0, "-%s%c", #b, 0);         \
1082               vec_add1 (conf->eal_init_args, tmp);      \
1083               vec_add1 (s, 0);                          \
1084               vec_add1 (conf->eal_init_args, s);        \
1085             }
1086           foreach_eal_single_hyphen_arg
1087 #undef _
1088
1089 #define _(a,b)                                          \
1090             else if (unformat(input, #a " %s", &s))     \
1091               {                                         \
1092                 tmp = format (0, "-%s%c", #b, 0);       \
1093                 vec_add1 (conf->eal_init_args, tmp);    \
1094                 vec_add1 (s, 0);                        \
1095                 vec_add1 (conf->eal_init_args, s);      \
1096                 conf->a##_set_manually = 1;             \
1097               }
1098             foreach_eal_single_hyphen_mandatory_arg
1099 #undef _
1100
1101           else if (unformat(input, "default"))
1102             ;
1103
1104           else
1105             {
1106               error = clib_error_return (0, "unknown input `%U'",
1107                                          format_unformat_error, input);
1108               goto done;
1109             }
1110     }
1111
1112   if (!conf->uio_driver_name)
1113     conf->uio_driver_name = format (0, "igb_uio%c", 0);
1114
1115   /*
1116    * Use 1G huge pages if available.
1117    */
1118   if (!no_huge && !huge_dir)
1119     {
1120       u32 x, * mem_by_socket = 0;
1121       uword c = 0;
1122       u8 use_1g = 1;
1123       u8 use_2m = 1;
1124       u8 less_than_1g = 1;
1125       int rv;
1126
1127       umount(DEFAULT_HUGE_DIR);
1128
1129       /* Process "socket-mem" parameter value */
1130       if (vec_len (socket_mem))
1131         {
1132           unformat_input_t in;
1133           unformat_init_vector(&in, socket_mem);
1134           while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1135             {
1136               if (unformat (&in, "%u,", &x))
1137                 ;
1138               else if (unformat (&in, "%u", &x))
1139                 ;
1140               else if (unformat (&in, ","))
1141                 x = 0;
1142               else
1143                 break;
1144
1145               vec_add1(mem_by_socket, x);
1146
1147               if (x > 1023)
1148                 less_than_1g = 0;
1149             }
1150           /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1151           unformat_free(&in);
1152           socket_mem = 0;
1153         }
1154       else
1155         {
1156           clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1157             {
1158               vec_validate(mem_by_socket, c);
1159               mem_by_socket[c] = 256; /* default per-socket mem */
1160             }
1161           ));
1162         }
1163
1164       /* check if available enough 1GB pages for each socket */
1165       clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1166         {
1167           u32 pages_avail, page_size, mem;
1168           u8 *s = 0;
1169           u8 *p = 0;
1170           char * numa_path = "/sys/devices/system/node/node%u/";
1171           char * nonnuma_path = "/sys/kernel/mm/";
1172           char * suffix = "hugepages/hugepages-%ukB/free_hugepages%c";
1173           char * path = NULL;
1174           struct stat sb_numa, sb_nonnuma;
1175
1176           p = format(p, numa_path, c);
1177           if (stat(numa_path, &sb_numa) < 0)
1178             sb_numa.st_mode = 0;
1179
1180           if (stat(nonnuma_path, &sb_nonnuma) < 0)
1181             sb_nonnuma.st_mode = 0;
1182
1183           if (S_ISDIR(sb_numa.st_mode)) {
1184             path = (char*)format((u8*)path, "%s%s", p, suffix);
1185           } else if (S_ISDIR(sb_nonnuma.st_mode)) {
1186             path = (char*)format((u8*)path, "%s%s", nonnuma_path, suffix);
1187           } else {
1188             use_1g = 0;
1189             use_2m = 0;
1190             vec_free(p);
1191             break;
1192           }
1193
1194           vec_validate(mem_by_socket, c);
1195           mem = mem_by_socket[c];
1196
1197           page_size = 1024;
1198           pages_avail = 0;
1199           s = format (s, path, page_size * 1024, 0);
1200           vlib_sysfs_read ((char *) s, "%u", &pages_avail);
1201           vec_reset_length (s);
1202
1203           if (page_size * pages_avail < mem)
1204             use_1g = 0;
1205
1206           page_size = 2;
1207           pages_avail = 0;
1208           s = format (s, path, page_size * 1024, 0);
1209           vlib_sysfs_read ((char *) s, "%u", &pages_avail);
1210           vec_reset_length (s);
1211
1212           if (page_size * pages_avail < mem)
1213             use_2m = 0;
1214
1215           vec_free(s);
1216           vec_free(p);
1217           vec_free(path);
1218       }));
1219
1220       if (mem_by_socket == 0)
1221         {
1222           error = clib_error_return (0, "mem_by_socket NULL");
1223           goto done;
1224         }
1225       _vec_len (mem_by_socket) = c + 1;
1226
1227       /* regenerate socket_mem string */
1228       vec_foreach_index (x, mem_by_socket)
1229         socket_mem = format (socket_mem, "%s%u",
1230                              socket_mem ? "," : "",
1231                              mem_by_socket[x]);
1232       socket_mem = format (socket_mem, "%c", 0);
1233
1234       vec_free (mem_by_socket);
1235
1236       rv = mkdir(VPP_RUN_DIR, 0755);
1237       if (rv && errno != EEXIST)
1238         {
1239           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1240                                      VPP_RUN_DIR, errno);
1241           goto done;
1242         }
1243
1244       rv = mkdir(DEFAULT_HUGE_DIR, 0755);
1245       if (rv && errno != EEXIST)
1246         {
1247           error = clib_error_return (0, "mkdir '%s' failed errno %d",
1248                                      DEFAULT_HUGE_DIR, errno);
1249           goto done;
1250         }
1251
1252       if (use_1g && !(less_than_1g && use_2m))
1253         {
1254           rv = mount("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1255         }
1256       else if (use_2m)
1257         {
1258           rv = mount("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1259         }
1260       else
1261         {
1262           return clib_error_return (0, "not enough free huge pages");
1263         }
1264
1265       if (rv)
1266         {
1267           error = clib_error_return (0, "mount failed %d", errno);
1268           goto done;
1269         }
1270
1271       tmp = format (0, "--huge-dir%c", 0);
1272       vec_add1 (conf->eal_init_args, tmp);
1273       tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1274       vec_add1 (conf->eal_init_args, tmp);
1275       if (!file_prefix)
1276         {
1277           tmp = format (0, "--file-prefix%c", 0);
1278           vec_add1 (conf->eal_init_args, tmp);
1279           tmp = format (0, "vpp%c", 0);
1280           vec_add1 (conf->eal_init_args, tmp);
1281         }
1282     }
1283
1284   vec_free (rte_cmd);
1285   vec_free (ethname);
1286
1287   if (error)
1288     return error;
1289
1290   /* I'll bet that -c and -n must be the first and second args... */
1291   if (!conf->coremask_set_manually)
1292     {
1293       vlib_thread_registration_t * tr;
1294       uword * coremask = 0;
1295       int i;
1296
1297       /* main thread core */
1298       coremask = clib_bitmap_set(coremask, tm->main_lcore, 1);
1299
1300       for (i = 0; i < vec_len (tm->registrations); i++)
1301         {
1302           tr = tm->registrations[i];
1303           coremask = clib_bitmap_or(coremask, tr->coremask);
1304         }
1305
1306       vec_insert (conf->eal_init_args, 2, 1);
1307       conf->eal_init_args[1] = (u8 *) "-c";
1308       tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1309       conf->eal_init_args[2] = tmp;
1310       clib_bitmap_free(coremask);
1311     }
1312
1313   if (!conf->nchannels_set_manually)
1314     {
1315       vec_insert (conf->eal_init_args, 2, 3);
1316       conf->eal_init_args[3] = (u8 *) "-n";
1317       tmp = format (0, "%d", conf->nchannels);
1318       conf->eal_init_args[4] = tmp;
1319     }
1320
1321   if (no_pci == 0 && geteuid() == 0)
1322     dpdk_bind_devices_to_uio(conf);
1323
1324 #define _(x) \
1325     if (devconf->x == 0 && conf->default_devconf.x > 0) \
1326       devconf->x = conf->default_devconf.x ;
1327
1328   pool_foreach (devconf, conf->dev_confs, ({
1329
1330     /* default per-device config items */
1331     foreach_dpdk_device_config_item
1332
1333     /* add DPDK EAL whitelist/blacklist entry */
1334     if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1335       {
1336         tmp = format (0, "-w%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     else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1342       {
1343         tmp = format (0, "-b%c", 0);
1344         vec_add1 (conf->eal_init_args, tmp);
1345         tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1346         vec_add1 (conf->eal_init_args, tmp);
1347       }
1348   }));
1349
1350 #undef _
1351
1352   /* set master-lcore */
1353   tmp = format (0, "--master-lcore%c", 0);
1354   vec_add1 (conf->eal_init_args, tmp);
1355   tmp = format (0, "%u%c", tm->main_lcore, 0);
1356   vec_add1 (conf->eal_init_args, tmp);
1357
1358   /* set socket-mem */
1359   tmp = format (0, "--socket-mem%c", 0);
1360   vec_add1 (conf->eal_init_args, tmp);
1361   tmp = format (0, "%s%c", socket_mem, 0);
1362   vec_add1 (conf->eal_init_args, tmp);
1363
1364   /* NULL terminate the "argv" vector, in case of stupidity */
1365   vec_add1 (conf->eal_init_args, 0);
1366   _vec_len(conf->eal_init_args) -= 1;
1367
1368   /* Set up DPDK eal and packet mbuf pool early. */
1369
1370   log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1371
1372   rte_set_log_level (log_level);
1373
1374   vm = vlib_get_main ();
1375
1376   /* make copy of args as rte_eal_init tends to mess up with arg array */
1377   for (i = 1; i < vec_len(conf->eal_init_args); i++)
1378     conf->eal_init_args_str = format(conf->eal_init_args_str, "%s ",
1379                                      conf->eal_init_args[i]);
1380
1381   ret = rte_eal_init(vec_len(conf->eal_init_args), (char **) conf->eal_init_args);
1382
1383   /* lazy umount hugepages */
1384   umount2(DEFAULT_HUGE_DIR, MNT_DETACH);
1385
1386   if (ret < 0)
1387     return clib_error_return (0, "rte_eal_init returned %d", ret);
1388
1389   /* Dump the physical memory layout prior to creating the mbuf_pool */
1390   fprintf(stdout, "DPDK physical memory layout:\n");
1391   rte_dump_physmem_layout(stdout);
1392
1393   /* main thread 1st */
1394   error = vlib_buffer_pool_create(vm, conf->num_mbufs, rte_socket_id());
1395   if (error)
1396     return error;
1397
1398   for (i = 0; i < RTE_MAX_LCORE; i++)
1399     {
1400       error = vlib_buffer_pool_create(vm, conf->num_mbufs,
1401                                       rte_lcore_to_socket_id(i));
1402       if (error)
1403         return error;
1404     }
1405
1406  done:
1407   return error;
1408 }
1409
1410 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1411
1412 void dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1413 {
1414     vnet_main_t * vnm = vnet_get_main();
1415     struct rte_eth_link prev_link = xd->link;
1416     u32 hw_flags =  0;
1417     u8 hw_flags_chg = 0;
1418
1419     /* only update link state for PMD interfaces */
1420     if (xd->dev_type != VNET_DPDK_DEV_ETH)
1421       return;
1422
1423     xd->time_last_link_update = now ? now : xd->time_last_link_update;
1424     memset(&xd->link, 0, sizeof(xd->link));
1425     rte_eth_link_get_nowait (xd->device_index, &xd->link);
1426
1427     if (LINK_STATE_ELOGS)
1428       {
1429         vlib_main_t * vm = vlib_get_main();
1430         ELOG_TYPE_DECLARE(e) = {
1431           .format = 
1432           "update-link-state: sw_if_index %d, admin_up %d,"
1433           "old link_state %d new link_state %d",
1434           .format_args = "i4i1i1i1",
1435         };
1436
1437         struct { u32 sw_if_index; u8 admin_up; 
1438           u8 old_link_state; u8 new_link_state;} *ed;
1439         ed = ELOG_DATA (&vm->elog_main, e);
1440         ed->sw_if_index = xd->vlib_sw_if_index;
1441         ed->admin_up = xd->admin_up;
1442         ed->old_link_state = (u8)
1443           vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index);
1444         ed->new_link_state = (u8) xd->link.link_status;
1445       }
1446
1447     if ((xd->admin_up == 1) && 
1448         ((xd->link.link_status != 0) ^ 
1449          vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index)))
1450       {
1451         hw_flags_chg = 1;
1452         hw_flags |= (xd->link.link_status ? 
1453                      VNET_HW_INTERFACE_FLAG_LINK_UP: 0);
1454       }
1455
1456     if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1457       {
1458         hw_flags_chg = 1;
1459         switch (xd->link.link_duplex)
1460           {
1461           case ETH_LINK_HALF_DUPLEX:
1462             hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1463             break;
1464           case ETH_LINK_FULL_DUPLEX:
1465             hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1466             break;
1467           default:
1468             break;
1469           }
1470       }
1471 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
1472     if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1473       {
1474         hw_flags_chg = 1;
1475         switch (xd->link.link_speed)
1476           {
1477           case ETH_SPEED_NUM_10M:
1478             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1479             break;
1480           case ETH_SPEED_NUM_100M:
1481             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1482             break;
1483           case ETH_SPEED_NUM_1G:
1484             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1485             break;
1486           case ETH_SPEED_NUM_10G:
1487             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1488             break;
1489           case ETH_SPEED_NUM_40G:
1490             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1491             break;
1492     case 0:
1493       break;
1494     default:
1495       clib_warning("unknown link speed %d", xd->link.link_speed);
1496             break;
1497           }
1498       }
1499 #else
1500     if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1501       {
1502         hw_flags_chg = 1;
1503         switch (xd->link.link_speed)
1504           {
1505           case ETH_LINK_SPEED_10:
1506             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1507             break;
1508           case ETH_LINK_SPEED_100:
1509             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1510             break;
1511           case ETH_LINK_SPEED_1000:
1512             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1513             break;
1514           case ETH_LINK_SPEED_10000:
1515             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1516             break;
1517           case ETH_LINK_SPEED_40G:
1518             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1519             break;
1520     case 0:
1521       break;
1522     default:
1523       clib_warning("unknown link speed %d", xd->link.link_speed);
1524             break;
1525           }
1526       }
1527 #endif
1528     if (hw_flags_chg)
1529       {
1530         if (LINK_STATE_ELOGS)
1531           {
1532             vlib_main_t * vm = vlib_get_main();
1533
1534             ELOG_TYPE_DECLARE(e) = {
1535               .format = "update-link-state: sw_if_index %d, new flags %d",
1536               .format_args = "i4i4",
1537             };
1538
1539             struct { u32 sw_if_index; u32 flags; } *ed;
1540             ed = ELOG_DATA (&vm->elog_main, e);
1541             ed->sw_if_index = xd->vlib_sw_if_index;
1542             ed->flags = hw_flags;
1543           }
1544         vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, hw_flags);
1545       }
1546 }
1547
1548 static uword
1549 dpdk_process (vlib_main_t * vm,
1550               vlib_node_runtime_t * rt,
1551               vlib_frame_t * f)
1552 {
1553   clib_error_t * error;
1554   vnet_main_t * vnm = vnet_get_main();
1555   dpdk_main_t * dm = &dpdk_main;
1556   ethernet_main_t * em = &ethernet_main;
1557   dpdk_device_t * xd;
1558   vlib_thread_main_t * tm = vlib_get_thread_main();
1559 #if DPDK_VHOST_USER
1560   void *vu_state;
1561 #endif
1562   int i;
1563
1564   error = dpdk_lib_init (dm);
1565
1566   /* 
1567    * Turn on the input node if we found some devices to drive
1568    * and we're not running worker threads or i/o threads
1569    */
1570
1571   if (error == 0 && vec_len(dm->devices) > 0)
1572     {
1573         if (tm->n_vlib_mains == 1)
1574           vlib_node_set_state (vm, dpdk_input_node.index,
1575                                VLIB_NODE_STATE_POLLING);
1576         else
1577           for (i=0; i < tm->n_vlib_mains; i++)
1578             if (vec_len(dm->devices_by_cpu[i]) > 0)
1579               vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1580                                    VLIB_NODE_STATE_POLLING);
1581     }
1582
1583   if (error)
1584     clib_error_report (error);
1585
1586 #if DPDK_VHOST_USER
1587   dpdk_vhost_user_process_init(&vu_state);
1588 #endif
1589
1590   tm->worker_thread_release = 1;
1591
1592   f64 now = vlib_time_now (vm);
1593   vec_foreach (xd, dm->devices)
1594     {
1595       dpdk_update_link_state (xd, now);
1596     }
1597
1598 { /* 
1599    * Extra set up for bond interfaces:
1600    *  1. Setup MACs for bond interfaces and their slave links which was set
1601    *     in dpdk_port_setup() but needs to be done again here to take effect.
1602    *  2. Set up info for bond interface related CLI support. 
1603    */
1604   int nports = rte_eth_dev_count();
1605   if (nports > 0) {
1606       for (i = 0; i < nports; i++) {
1607           struct rte_eth_dev_info dev_info;
1608           rte_eth_dev_info_get(i, &dev_info);
1609           if (!dev_info.driver_name)
1610               dev_info.driver_name = dev_info.pci_dev->driver->name;
1611           ASSERT(dev_info.driver_name);
1612           if (strncmp(dev_info.driver_name, "rte_bond_pmd", 12) == 0) {
1613               u8  addr[6]; 
1614               u8  slink[16];
1615               int nlink = rte_eth_bond_slaves_get(i, slink, 16);
1616               if (nlink > 0) {
1617                   vnet_hw_interface_t * bhi;
1618                   ethernet_interface_t * bei;
1619                   int rv;
1620
1621                   /* Get MAC of 1st slave link */
1622                   rte_eth_macaddr_get(slink[0], (struct ether_addr *)addr);
1623                   /* Set MAC of bounded interface to that of 1st slave link */
1624                   rv = rte_eth_bond_mac_address_set(i, (struct ether_addr *)addr);
1625                   if (rv < 0)
1626                     clib_warning("Failed to set MAC address");
1627
1628                   /* Populate MAC of bonded interface in VPP hw tables */
1629                   bhi = vnet_get_hw_interface(
1630                       vnm, dm->devices[i].vlib_hw_if_index);
1631                   bei = pool_elt_at_index(em->interfaces, bhi->hw_instance);
1632                   clib_memcpy(bhi->hw_address, addr, 6);
1633                   clib_memcpy(bei->address, addr, 6);
1634                   /* Init l3 packet size allowed on bonded interface */
1635                   bhi->max_l3_packet_bytes[VLIB_RX] = 
1636                   bhi->max_l3_packet_bytes[VLIB_TX] = 
1637                       ETHERNET_MAX_PACKET_BYTES - sizeof(ethernet_header_t);
1638                   while (nlink >= 1) { /* for all slave links */
1639                       int slave = slink[--nlink];
1640                       dpdk_device_t * sdev = &dm->devices[slave];
1641                       vnet_hw_interface_t * shi;
1642                       vnet_sw_interface_t * ssi;
1643                       /* Add MAC to all slave links except the first one */
1644                       if (nlink) rte_eth_dev_mac_addr_add(
1645                           slave, (struct ether_addr *)addr, 0);
1646                       /* Set slaves bitmap for bonded interface */
1647                       bhi->bond_info = clib_bitmap_set(
1648                           bhi->bond_info, sdev->vlib_hw_if_index, 1);
1649                       /* Set slave link flags on slave interface */
1650                       shi = vnet_get_hw_interface(vnm, sdev->vlib_hw_if_index);
1651                       ssi = vnet_get_sw_interface(vnm, sdev->vlib_sw_if_index);
1652                       shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1653                       ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1654                   }
1655               }
1656           }
1657       }
1658   }
1659 }
1660
1661   while (1)
1662     {
1663       /*
1664        * check each time through the loop in case intervals are changed
1665        */
1666       f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1667                      dm->link_state_poll_interval : dm->stat_poll_interval;
1668
1669       vlib_process_wait_for_event_or_clock (vm, min_wait);
1670
1671       if (dpdk_get_admin_up_down_in_progress())
1672           /* skip the poll if an admin up down is in progress (on any interface) */
1673           continue;
1674
1675       vec_foreach (xd, dm->devices)
1676         {
1677           f64 now = vlib_time_now (vm);
1678           if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1679             dpdk_update_counters (xd, now);
1680           if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1681             dpdk_update_link_state (xd, now);
1682
1683 #if DPDK_VHOST_USER
1684       if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
1685           if (dpdk_vhost_user_process_if(vm, xd, vu_state) != 0)
1686               continue;
1687 #endif
1688         }
1689     }
1690
1691 #if DPDK_VHOST_USER
1692   dpdk_vhost_user_process_cleanup(vu_state);
1693 #endif
1694
1695   return 0; 
1696 }
1697
1698 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1699     .function = dpdk_process,
1700     .type = VLIB_NODE_TYPE_PROCESS,
1701     .name = "dpdk-process",
1702     .process_log2_n_stack_bytes = 17,
1703 };
1704
1705 int dpdk_set_stat_poll_interval (f64 interval)
1706 {
1707   if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
1708       return (VNET_API_ERROR_INVALID_VALUE);
1709
1710   dpdk_main.stat_poll_interval = interval;
1711
1712   return 0;
1713 }
1714
1715 int dpdk_set_link_state_poll_interval (f64 interval)
1716 {
1717   if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
1718       return (VNET_API_ERROR_INVALID_VALUE);
1719
1720   dpdk_main.link_state_poll_interval = interval;
1721
1722   return 0;
1723 }
1724
1725 clib_error_t *
1726 dpdk_init (vlib_main_t * vm)
1727 {
1728   dpdk_main_t * dm = &dpdk_main;
1729   vlib_node_t * ei;
1730   clib_error_t * error = 0;
1731   vlib_thread_main_t * tm = vlib_get_thread_main();
1732
1733   /* verify that structs are cacheline aligned */
1734   ASSERT(offsetof(dpdk_device_t, cacheline0) == 0);
1735   ASSERT(offsetof(dpdk_device_t, cacheline1) == CLIB_CACHE_LINE_BYTES);
1736   ASSERT(offsetof(dpdk_worker_t, cacheline0) == 0);
1737   ASSERT(offsetof(frame_queue_trace_t, cacheline0) == 0);
1738
1739   dm->vlib_main = vm;
1740   dm->vnet_main = vnet_get_main();
1741   dm->conf = &dpdk_config_main;
1742
1743   ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1744   if (ei == 0)
1745       return clib_error_return (0, "ethernet-input node AWOL");
1746
1747   dm->ethernet_input_node_index = ei->index;
1748
1749   dm->conf->nchannels = 4;
1750   dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1751   vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1752
1753   dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1754   dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1755   dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
1756
1757   /* $$$ use n_thread_stacks since it's known-good at this point */
1758   vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1759
1760   /* initialize EFD (early fast discard) default settings */
1761   dm->efd.enabled = DPDK_EFD_DISABLED;
1762   dm->efd.queue_hi_thresh = ((DPDK_EFD_DEFAULT_DEVICE_QUEUE_HI_THRESH_PCT *
1763                               DPDK_NB_RX_DESC_10GE)/100);
1764   dm->efd.consec_full_frames_hi_thresh =
1765       DPDK_EFD_DEFAULT_CONSEC_FULL_FRAMES_HI_THRESH;
1766
1767   /* vhost-user coalescence frames defaults */
1768   dm->conf->vhost_coalesce_frames = 32;
1769   dm->conf->vhost_coalesce_time = 1e-3;
1770
1771   /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1772   dm->buffer_flags_template = 
1773     (VLIB_BUFFER_TOTAL_LENGTH_VALID 
1774      | IP_BUFFER_L4_CHECKSUM_COMPUTED
1775      | IP_BUFFER_L4_CHECKSUM_CORRECT);
1776
1777   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1778   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1779
1780   /* init CLI */
1781   if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1782     return error;
1783
1784   return error;
1785 }
1786
1787 VLIB_INIT_FUNCTION (dpdk_init);
1788