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