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