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