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