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