Add support for multiple microarchitectures in single binary
[vpp.git] / vnet / vnet / devices / dpdk / init.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/vnet.h>
16 #include <vppinfra/vec.h>
17 #include <vppinfra/error.h>
18 #include <vppinfra/format.h>
19 #include <vppinfra/bitmap.h>
20
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/dpdk/dpdk.h>
23 #include <vlib/unix/physmem.h>
24 #include <vlib/pci/pci.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/mount.h>
31 #include <string.h>
32 #include <fcntl.h>
33
34 #include "dpdk_priv.h"
35
36 dpdk_main_t dpdk_main;
37
38 /* force linker to link functions used by vlib and declared weak */
39 void *vlib_weakly_linked_functions[] = {
40   &rte_pktmbuf_init,
41   &rte_pktmbuf_pool_init,
42 };
43
44 #define LINK_STATE_ELOGS        0
45
46 #define DEFAULT_HUGE_DIR "/run/vpp/hugepages"
47 #define VPP_RUN_DIR "/run/vpp"
48
49 /* Port configuration, mildly modified Intel app values */
50
51 static struct rte_eth_conf port_conf_template = {
52   .rxmode = {
53     .split_hdr_size = 0,
54     .header_split   = 0, /**< Header Split disabled */
55     .hw_ip_checksum = 0, /**< IP checksum offload disabled */
56     .hw_vlan_filter = 0, /**< VLAN filtering disabled */
57     .hw_strip_crc   = 1, /**< CRC stripped by hardware */
58   },
59   .txmode = {
60     .mq_mode = ETH_MQ_TX_NONE,
61   },
62 };
63
64 clib_error_t *
65 dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd)
66 {
67   vlib_main_t * vm = vlib_get_main();
68   vlib_buffer_main_t * bm = vm->buffer_main;
69   int rv;
70   int j;
71
72   ASSERT(os_get_cpu_number() == 0);
73
74   if (xd->admin_up) {
75     vnet_hw_interface_set_flags (dm->vnet_main, xd->vlib_hw_if_index, 0);
76     rte_eth_dev_stop (xd->device_index);
77   }
78
79   rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
80                               xd->tx_q_used, &xd->port_conf);
81
82   if (rv < 0)
83     return clib_error_return (0, "rte_eth_dev_configure[%d]: err %d",
84                               xd->device_index, rv);
85
86   /* Set up one TX-queue per worker thread */
87   for (j = 0; j < xd->tx_q_used; j++)
88     {
89       rv = rte_eth_tx_queue_setup(xd->device_index, j, xd->nb_tx_desc,
90                                  xd->cpu_socket, &xd->tx_conf);
91
92       /* retry with any other CPU socket */
93       if (rv < 0)
94         rv = rte_eth_tx_queue_setup(xd->device_index, j, xd->nb_tx_desc,
95                                    SOCKET_ID_ANY, &xd->tx_conf);
96       if (rv < 0)
97         break;
98     }
99
100     if (rv < 0)
101       return clib_error_return (0, "rte_eth_tx_queue_setup[%d]: err %d",
102                                 xd->device_index, rv);
103
104   for (j = 0; j < xd->rx_q_used; j++)
105     {
106
107       rv = rte_eth_rx_queue_setup(xd->device_index, j, xd->nb_rx_desc,
108                                   xd->cpu_socket, 0,
109                                   bm->pktmbuf_pools[xd->cpu_socket_id_by_queue[j]]);
110
111       /* retry with any other CPU socket */
112       if (rv < 0)
113         rv = rte_eth_rx_queue_setup(xd->device_index, j, xd->nb_rx_desc,
114                                     SOCKET_ID_ANY, 0,
115                                     bm->pktmbuf_pools[xd->cpu_socket_id_by_queue[j]]);
116       if (rv < 0)
117         return clib_error_return (0, "rte_eth_rx_queue_setup[%d]: err %d",
118                                   xd->device_index, rv);
119     }
120
121   if (xd->admin_up) {
122     rte_eth_dev_start (xd->device_index);
123   }
124   return 0;
125 }
126
127 static u32 dpdk_flag_change (vnet_main_t * vnm, 
128                              vnet_hw_interface_t * hi,
129                              u32 flags)
130 {
131   dpdk_main_t * dm = &dpdk_main;
132   dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
133   u32 old = 0;
134
135   if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC(flags))
136     {
137       old = xd->promisc;
138       xd->promisc = flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
139       
140       if (xd->admin_up)
141         {
142           if (xd->promisc)
143             rte_eth_promiscuous_enable(xd->device_index);
144           else
145             rte_eth_promiscuous_disable(xd->device_index);
146         }
147     }
148   else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU(flags))
149     {
150       /*
151        * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
152        *            driver to dynamically change the mtu.  If/when the 
153        *            VIC firmware gets fixed, then this should be removed.
154        */
155       if (xd->pmd == VNET_DPDK_PMD_VICE ||
156           xd->pmd == VNET_DPDK_PMD_ENIC)
157         {
158           struct rte_eth_dev_info dev_info;
159
160           /*
161            * Restore mtu to what has been set by CIMC in the firmware cfg.
162            */
163           rte_eth_dev_info_get(xd->device_index, &dev_info);
164           hi->max_packet_bytes = dev_info.max_rx_pktlen;
165
166           vlib_cli_output (vlib_get_main(), 
167                            "Cisco VIC mtu can only be changed "
168                            "using CIMC then rebooting the server!");
169         }
170       else
171         {
172           int rv;
173       
174           xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
175
176           if (xd->admin_up)
177             rte_eth_dev_stop (xd->device_index);
178
179     rv = rte_eth_dev_configure
180       (xd->device_index,
181        xd->rx_q_used,
182        xd->tx_q_used,
183        &xd->port_conf);
184
185           if (rv < 0)
186             vlib_cli_output (vlib_get_main(), 
187                              "rte_eth_dev_configure[%d]: err %d",
188                              xd->device_index, rv);
189
190           rte_eth_dev_set_mtu(xd->device_index, hi->max_packet_bytes);
191
192           if (xd->admin_up)
193             rte_eth_dev_start (xd->device_index);
194         }
195     }
196   return old;
197 }
198
199 #ifdef NETMAP
200 extern int rte_netmap_probe(void);
201 #endif
202
203 void
204 dpdk_device_lock_init(dpdk_device_t * xd)
205 {
206   int q;
207   vec_validate(xd->lockp, xd->tx_q_used - 1);
208   for (q = 0; q < xd->tx_q_used; q++)
209     {
210       xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
211                                              CLIB_CACHE_LINE_BYTES);
212       memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
213   }
214   xd->need_txlock = 1;
215 }
216
217 void
218 dpdk_device_lock_free(dpdk_device_t * xd)
219 {
220   int q;
221
222   for (q = 0; q < vec_len(xd->lockp); q++)
223     clib_mem_free((void *) xd->lockp[q]);
224   vec_free(xd->lockp);
225   xd->lockp = 0;
226   xd->need_txlock = 0;
227 }
228
229 static clib_error_t *
230 dpdk_lib_init (dpdk_main_t * dm)
231 {
232   u32 nports;
233   u32 nb_desc = 0;
234   int i;
235   clib_error_t * error;
236   vlib_main_t * vm = vlib_get_main();
237   vlib_thread_main_t * tm = vlib_get_thread_main();
238   vnet_sw_interface_t * sw;
239   vnet_hw_interface_t * hi;
240   dpdk_device_t * xd;
241   vlib_thread_registration_t * tr;
242   uword * p;
243
244   u32 next_cpu = 0;
245   u8 af_packet_port_id = 0;
246
247   dm->input_cpu_first_index = 0;
248   dm->input_cpu_count = 1;
249
250   /* find out which cpus will be used for input */
251   p = hash_get_mem (tm->thread_registrations_by_name, "io");
252   tr = p ? (vlib_thread_registration_t *) p[0] : 0;
253
254   if (!tr || tr->count == 0)
255     {
256       /* no io threads, workers doing input */
257       p = hash_get_mem (tm->thread_registrations_by_name, "workers");
258       tr = p ? (vlib_thread_registration_t *) p[0] : 0;
259     }
260   else
261     {
262       dm->have_io_threads = 1;
263     }
264
265   if (tr && tr->count > 0)
266     {
267       dm->input_cpu_first_index = tr->first_index;
268       dm->input_cpu_count = tr->count;
269     }
270
271   vec_validate_aligned (dm->devices_by_cpu, tm->n_vlib_mains - 1,
272                         CLIB_CACHE_LINE_BYTES);
273
274   vec_validate_aligned (dm->workers, tm->n_vlib_mains - 1,
275                         CLIB_CACHE_LINE_BYTES);
276
277 #ifdef NETMAP
278   if(rte_netmap_probe() < 0)
279     return clib_error_return (0, "rte netmap probe failed");
280 #endif
281
282   nports = rte_eth_dev_count();
283   if (nports < 1) 
284     {
285       clib_warning ("DPDK drivers found no ports...");
286     }
287
288   if (CLIB_DEBUG > 0)
289     clib_warning ("DPDK drivers found %d ports...", nports);
290
291   /* 
292    * All buffers are all allocated from the same rte_mempool.
293    * Thus they all have the same number of data bytes.
294    */
295   dm->vlib_buffer_free_list_index = 
296       vlib_buffer_get_or_create_free_list (
297           vm, VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES, "dpdk rx");
298
299   for (i = 0; i < nports; i++)
300     {
301       u8 addr[6];
302       int j;
303       struct rte_eth_dev_info dev_info;
304       clib_error_t * rv;
305       struct rte_eth_link l;
306
307       /* Create vnet interface */
308       vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
309       xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
310       xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
311       xd->cpu_socket = (i8) rte_eth_dev_socket_id(i);
312       rte_eth_dev_info_get(i, &dev_info);
313
314       clib_memcpy(&xd->tx_conf, &dev_info.default_txconf,
315              sizeof(struct rte_eth_txconf));
316       if (dm->no_multi_seg)
317         {
318           xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
319           port_conf_template.rxmode.jumbo_frame = 0;
320         }
321       else
322         {
323           xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
324           port_conf_template.rxmode.jumbo_frame = 1;
325         }
326
327       clib_memcpy(&xd->port_conf, &port_conf_template, sizeof(struct rte_eth_conf));
328
329       xd->tx_q_used = clib_min(dev_info.max_tx_queues, tm->n_vlib_mains);
330
331       if (dm->max_tx_queues)
332         xd->tx_q_used = clib_min(xd->tx_q_used, dm->max_tx_queues);
333
334       if (dm->use_rss > 1 && dev_info.max_rx_queues >= dm->use_rss)
335         {
336           xd->rx_q_used = dm->use_rss;
337           xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
338           xd->port_conf.rx_adv_conf.rss_conf.rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
339         }
340       else
341         xd->rx_q_used = 1;
342
343       xd->dev_type = VNET_DPDK_DEV_ETH;
344
345       /* workaround for drivers not setting driver_name */
346       if (!dev_info.driver_name)
347         dev_info.driver_name = dev_info.pci_dev->driver->name;
348       ASSERT(dev_info.driver_name);
349
350       if (!xd->pmd) {
351
352
353 #define _(s,f) else if (!strcmp(dev_info.driver_name, s)) \
354                  xd->pmd = VNET_DPDK_PMD_##f;
355         if (0)
356           ;
357         foreach_dpdk_pmd
358 #undef _
359         else
360           xd->pmd = VNET_DPDK_PMD_UNKNOWN;
361
362
363         switch (xd->pmd) {
364           /* 1G adapters */
365           case VNET_DPDK_PMD_E1000EM:
366           case VNET_DPDK_PMD_IGB:
367           case VNET_DPDK_PMD_IGBVF:
368             xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
369             break;
370
371           /* 10G adapters */
372           case VNET_DPDK_PMD_IXGBE:
373           case VNET_DPDK_PMD_IXGBEVF:
374           case VNET_DPDK_PMD_THUNDERX:
375             xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
376             xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
377             xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
378             break;
379
380           /* Cisco VIC */
381           case VNET_DPDK_PMD_VICE:
382           case VNET_DPDK_PMD_ENIC:
383             rte_eth_link_get_nowait(i, &l);
384             if (l.link_speed == 40000)
385               {
386                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
387                 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
388                 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
389               }
390             else
391               {
392                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
393                 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
394                 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
395               }
396             break;
397
398           /* Intel Fortville */
399           case VNET_DPDK_PMD_I40E:
400           case VNET_DPDK_PMD_I40EVF:
401             xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
402             xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
403             xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
404
405             switch (dev_info.pci_dev->id.device_id) {
406               case I40E_DEV_ID_10G_BASE_T:
407               case I40E_DEV_ID_SFP_XL710:
408                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
409                 break;
410               case I40E_DEV_ID_QSFP_A:
411               case I40E_DEV_ID_QSFP_B:
412               case I40E_DEV_ID_QSFP_C:
413                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
414                 break;
415               case I40E_DEV_ID_VF:
416                 rte_eth_link_get_nowait(i, &l);
417                 xd->port_type = l.link_speed == 10000 ?
418                   VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
419                 break;
420               default:
421                 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
422             }
423             break;
424
425           case VNET_DPDK_PMD_CXGBE:
426             switch (dev_info.pci_dev->id.device_id) {
427               case 0x5410: /* T580-LP-cr */
428                 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
429                 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
430                 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
431                 break;
432               default:
433                 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
434                 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
435                 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
436             }
437             break;
438
439           /* Intel Red Rock Canyon */
440           case VNET_DPDK_PMD_FM10K:
441             xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
442             xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
443             xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
444             break;
445
446           /* virtio */
447           case VNET_DPDK_PMD_VIRTIO:
448             xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
449             xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
450             xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
451             break;
452
453           /* vmxnet3 */
454           case VNET_DPDK_PMD_VMXNET3:
455             xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
456             xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
457             break;
458
459           case VNET_DPDK_PMD_AF_PACKET:
460             xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
461             xd->af_packet_port_id = af_packet_port_id++;
462             break;
463
464           case VNET_DPDK_PMD_BOND:
465             xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
466             break;
467
468           default:
469             xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
470         }
471
472   #ifdef NETMAP
473         if(strncmp(dev_info.driver_name, "vale", 4) == 0
474              || strncmp(dev_info.driver_name, "netmap", 6) == 0)
475           {
476             xd->pmd = VNET_DPDK_PMD_NETMAP;
477             xd->port_type = VNET_DPDK_PORT_TYPE_NETMAP;
478           }
479   #endif
480
481       }
482
483       /*
484        * Ensure default mtu is not > the mtu read from the hardware.
485        * Otherwise rte_eth_dev_configure() will fail and the port will
486        * not be available.
487        */
488       if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
489         {
490           /*
491            * This device does not support the platforms's max frame
492            * size. Use it's advertised mru instead.
493            */
494           xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
495         }
496       else
497         {
498           xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
499
500           /*
501            * Some platforms do not account for Ethernet FCS (4 bytes) in
502            * MTU calculations. To interop with them increase mru but only
503            * if the device's settings can support it.
504            */
505           if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
506               xd->port_conf.rxmode.hw_strip_crc)
507             {
508               /*
509                * Allow additional 4 bytes (for Ethernet FCS). These bytes are
510                * stripped by h/w and so will not consume any buffer memory.
511                */
512               xd->port_conf.rxmode.max_rx_pkt_len += 4;
513             }
514         }
515
516 #if RTE_VERSION < RTE_VERSION_NUM(16, 4, 0, 0) 
517       /*
518        * Older VMXNET3 driver doesn't support jumbo / multi-buffer pkts
519        */
520       if (xd->pmd == VNET_DPDK_PMD_VMXNET3)
521         {
522           xd->port_conf.rxmode.max_rx_pkt_len = 1518;
523           xd->port_conf.rxmode.jumbo_frame = 0;
524         }
525 #endif
526
527       if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
528         {
529           f64 now = vlib_time_now(vm);
530           u32 rnd;
531           rnd = (u32) (now * 1e6);
532           rnd = random_u32 (&rnd);
533           clib_memcpy (addr+2, &rnd, sizeof(rnd));
534           addr[0] = 2;
535           addr[1] = 0xfe;
536         }
537       else
538         rte_eth_macaddr_get(i,(struct ether_addr *)addr);
539
540       if (xd->tx_q_used < tm->n_vlib_mains)
541         dpdk_device_lock_init(xd);
542
543       xd->device_index = xd - dm->devices;
544       ASSERT(i == xd->device_index);
545       xd->per_interface_next_index = ~0;
546
547       /* assign interface to input thread */
548       dpdk_device_and_queue_t * dq;
549       int q;
550
551       for (q = 0; q < xd->rx_q_used; q++)
552         {
553           int cpu = dm->input_cpu_first_index + next_cpu;
554           unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
555
556           /*
557            * numa node for worker thread handling this queue
558            * needed for taking buffers from the right mempool
559            */
560           vec_validate(xd->cpu_socket_id_by_queue, q);
561           xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
562
563           /*
564            * construct vector of (device,queue) pairs for each worker thread
565            */
566           vec_add2(dm->devices_by_cpu[cpu], dq, 1);
567           dq->device = xd->device_index;
568           dq->queue_id = q;
569
570           next_cpu++;
571           if (next_cpu == dm->input_cpu_count)
572             next_cpu = 0;
573         }
574
575       vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
576                             CLIB_CACHE_LINE_BYTES);
577       for (j = 0; j < tm->n_vlib_mains; j++)
578         {
579           vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE, 
580                            sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
581           vec_reset_length (xd->tx_vectors[j]);
582         }
583
584       vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
585                             CLIB_CACHE_LINE_BYTES);
586       for (j = 0; j< xd->rx_q_used; j++)
587         {
588           vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
589                                 CLIB_CACHE_LINE_BYTES);
590           vec_reset_length (xd->rx_vectors[j]);
591         }
592
593       vec_validate_aligned (xd->frames, tm->n_vlib_mains,
594                             CLIB_CACHE_LINE_BYTES);
595
596       rv = dpdk_port_setup(dm, xd);
597
598       if (rv < 0)
599         return rv;
600
601       /* count the number of descriptors used for this device */
602       nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
603
604       error = ethernet_register_interface
605         (dm->vnet_main,
606          dpdk_device_class.index,
607          xd->device_index,
608          /* ethernet address */ addr,
609          &xd->vlib_hw_if_index, 
610          dpdk_flag_change);
611       if (error)
612         return error;
613       
614       sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
615       xd->vlib_sw_if_index = sw->sw_if_index;
616       hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
617
618       /*
619        * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
620        *            driver to dynamically change the mtu.  If/when the 
621        *            VIC firmware gets fixed, then this should be removed.
622        */
623       if (xd->pmd == VNET_DPDK_PMD_VICE ||
624           xd->pmd == VNET_DPDK_PMD_ENIC)
625         {
626           /*
627            * Initialize mtu to what has been set by CIMC in the firmware cfg.
628            */
629           hi->max_packet_bytes = dev_info.max_rx_pktlen;
630           /*
631            * remove vlan tag from VIC port to fix VLAN0 issue.
632            * TODO Handle VLAN tagged traffic
633            */
634           int vlan_off;
635           vlan_off = rte_eth_dev_get_vlan_offload(xd->device_index);
636           vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
637           rte_eth_dev_set_vlan_offload(xd->device_index, vlan_off);
638         }
639
640 #if RTE_VERSION < RTE_VERSION_NUM(16, 4, 0, 0) 
641       /*
642        * Older VMXNET3 driver doesn't support jumbo / multi-buffer pkts
643        */
644       else if (xd->pmd == VNET_DPDK_PMD_VMXNET3)
645           hi->max_packet_bytes = 1518;
646 #endif
647
648       hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 
649               xd->port_conf.rxmode.max_rx_pkt_len - sizeof(ethernet_header_t);
650
651      rte_eth_dev_set_mtu(xd->device_index, hi->max_packet_bytes);
652     }
653
654 #ifdef RTE_LIBRTE_KNI
655   if (dm->num_kni) {
656     clib_warning("Initializing KNI interfaces...");
657     rte_kni_init(dm->num_kni);
658     for (i = 0; i < dm->num_kni; i++)
659     {
660       u8 addr[6];
661       int j;
662
663       /* Create vnet interface */
664       vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
665       xd->dev_type = VNET_DPDK_DEV_KNI;
666
667       xd->device_index = xd - dm->devices;
668       ASSERT(nports + i == xd->device_index);
669       xd->per_interface_next_index = ~0;
670       xd->kni_port_id = i;
671       xd->cpu_socket = -1;
672       hash_set (dm->dpdk_device_by_kni_port_id, i, xd - dm->devices);
673       xd->rx_q_used = 1;
674
675       /* assign interface to input thread */
676       dpdk_device_and_queue_t * dq;
677       vec_add2(dm->devices_by_cpu[dm->input_cpu_first_index], dq, 1);
678       dq->device = xd->device_index;
679       dq->queue_id = 0;
680
681       vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
682                             CLIB_CACHE_LINE_BYTES);
683       for (j = 0; j < tm->n_vlib_mains; j++)
684         {
685           vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE, 
686                            sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
687           vec_reset_length (xd->tx_vectors[j]);
688         }
689
690       vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
691                             CLIB_CACHE_LINE_BYTES);
692       for (j = 0; j< xd->rx_q_used; j++)
693         {
694           vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
695                                 CLIB_CACHE_LINE_BYTES);
696           vec_reset_length (xd->rx_vectors[j]);
697         }
698
699       vec_validate_aligned (xd->frames, tm->n_vlib_mains,
700                             CLIB_CACHE_LINE_BYTES);
701
702       /* FIXME Set up one TX-queue per worker thread */
703
704       {
705         f64 now = vlib_time_now(vm);
706         u32 rnd;
707         rnd = (u32) (now * 1e6);
708         rnd = random_u32 (&rnd);
709
710         clib_memcpy (addr+2, &rnd, sizeof(rnd));
711         addr[0] = 2;
712         addr[1] = 0xfe;
713       }
714
715       error = ethernet_register_interface
716         (dm->vnet_main,
717          dpdk_device_class.index,
718          xd->device_index,
719          /* ethernet address */ addr,
720          &xd->vlib_hw_if_index, 
721          dpdk_flag_change);
722
723       if (error)
724         return error;
725
726       sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
727       xd->vlib_sw_if_index = sw->sw_if_index;
728       hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
729     }
730   }
731 #endif
732
733   if (nb_desc > dm->num_mbufs) 
734     clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
735                   dm->num_mbufs, nb_desc);
736
737   /* init next vhost-user if index */
738   dm->next_vu_if_id = 0;
739
740   return 0;
741 }
742
743 static void
744 dpdk_bind_devices_to_uio (dpdk_main_t * dm)
745 {
746   vlib_pci_main_t * pm = &pci_main;
747   clib_error_t * error;
748   vlib_pci_device_t * d;
749   pci_config_header_t * c;
750   u8 * pci_addr = 0;
751
752   pool_foreach (d, pm->pci_devs, ({
753     c = &d->config0.header;
754     vec_reset_length (pci_addr);
755     pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
756
757     if (c->device_class != PCI_CLASS_NETWORK_ETHERNET)
758       continue;
759
760     /* if whitelist exists process only whitelisted devices */
761     if (dm->eth_if_whitelist &&
762         !strstr ((char *) dm->eth_if_whitelist, (char *) pci_addr))
763     continue;
764
765     /* virtio */
766     if (c->vendor_id == 0x1af4 && c->device_id == 0x1000)
767       ;
768     /* vmxnet3 */
769     else if (c->vendor_id == 0x15ad && c->device_id == 0x07b0)
770       ;
771     /* all Intel devices */
772     else if (c->vendor_id == 0x8086)
773       ;
774     /* Cisco VIC */
775     else if (c->vendor_id == 0x1137 && c->device_id == 0x0043)
776       ;
777     /* Chelsio T4/T5 */
778     else if (c->vendor_id == 0x1425 && (c->device_id & 0xe000) == 0x4000)
779       ;
780     else
781       {
782         clib_warning ("Unsupported Ethernet PCI device 0x%04x:0x%04x found "
783                       "at PCI address %s\n", (u16) c->vendor_id, (u16) c->device_id,
784                       pci_addr);
785         continue;
786       }
787
788     error = vlib_pci_bind_to_uio (d, (char *) dm->uio_driver_name);
789
790     if (error)
791       {
792         if (!dm->eth_if_whitelist)
793           dm->eth_if_blacklist = format (dm->eth_if_blacklist, "%U ",
794                                          format_vlib_pci_addr, &d->bus_address);
795         clib_error_report (error);
796       }
797   }));
798   vec_free (pci_addr);
799 }
800
801 static clib_error_t *
802 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
803 {
804   clib_error_t * error = 0;
805   dpdk_main_t * dm = &dpdk_main;
806   vlib_thread_main_t * tm = vlib_get_thread_main();
807   vlib_node_runtime_t * rt = vlib_node_get_runtime (vm, dpdk_input_node.index);
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           vlib_sysfs_read ((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           vlib_sysfs_read ((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     rt->function = dpdk_input_rss_multiarch_select();
1256   else
1257     rt->function = dpdk_input_multiarch_select();
1258  done:
1259   return error;
1260 }
1261
1262 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1263
1264 void dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1265 {
1266     vnet_main_t * vnm = vnet_get_main();
1267     struct rte_eth_link prev_link = xd->link;
1268     u32 hw_flags =  0;
1269     u8 hw_flags_chg = 0;
1270
1271     /* only update link state for PMD interfaces */
1272     if (xd->dev_type != VNET_DPDK_DEV_ETH)
1273       return;
1274
1275     xd->time_last_link_update = now ? now : xd->time_last_link_update;
1276     memset(&xd->link, 0, sizeof(xd->link));
1277     rte_eth_link_get_nowait (xd->device_index, &xd->link);
1278
1279     if (LINK_STATE_ELOGS)
1280       {
1281         vlib_main_t * vm = vlib_get_main();
1282         ELOG_TYPE_DECLARE(e) = {
1283           .format = 
1284           "update-link-state: sw_if_index %d, admin_up %d,"
1285           "old link_state %d new link_state %d",
1286           .format_args = "i4i1i1i1",
1287         };
1288
1289         struct { u32 sw_if_index; u8 admin_up; 
1290           u8 old_link_state; u8 new_link_state;} *ed;
1291         ed = ELOG_DATA (&vm->elog_main, e);
1292         ed->sw_if_index = xd->vlib_sw_if_index;
1293         ed->admin_up = xd->admin_up;
1294         ed->old_link_state = (u8)
1295           vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index);
1296         ed->new_link_state = (u8) xd->link.link_status;
1297       }
1298
1299     if ((xd->admin_up == 1) && 
1300         ((xd->link.link_status != 0) ^ 
1301          vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index)))
1302       {
1303         hw_flags_chg = 1;
1304         hw_flags |= (xd->link.link_status ? 
1305                      VNET_HW_INTERFACE_FLAG_LINK_UP: 0);
1306       }
1307
1308     if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1309       {
1310         hw_flags_chg = 1;
1311         switch (xd->link.link_duplex)
1312           {
1313           case ETH_LINK_HALF_DUPLEX:
1314             hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1315             break;
1316           case ETH_LINK_FULL_DUPLEX:
1317             hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1318             break;
1319           default:
1320             break;
1321           }
1322       }
1323 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
1324     if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1325       {
1326         hw_flags_chg = 1;
1327         switch (xd->link.link_speed)
1328           {
1329           case ETH_SPEED_NUM_10M:
1330             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1331             break;
1332           case ETH_SPEED_NUM_100M:
1333             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1334             break;
1335           case ETH_SPEED_NUM_1G:
1336             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1337             break;
1338           case ETH_SPEED_NUM_10G:
1339             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1340             break;
1341           case ETH_SPEED_NUM_40G:
1342             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1343             break;
1344     case 0:
1345       break;
1346     default:
1347       clib_warning("unknown link speed %d", xd->link.link_speed);
1348             break;
1349           }
1350       }
1351 #else
1352     if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1353       {
1354         hw_flags_chg = 1;
1355         switch (xd->link.link_speed)
1356           {
1357           case ETH_LINK_SPEED_10:
1358             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1359             break;
1360           case ETH_LINK_SPEED_100:
1361             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1362             break;
1363           case ETH_LINK_SPEED_1000:
1364             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1365             break;
1366           case ETH_LINK_SPEED_10000:
1367             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1368             break;
1369           case ETH_LINK_SPEED_40G:
1370             hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1371             break;
1372     case 0:
1373       break;
1374     default:
1375       clib_warning("unknown link speed %d", xd->link.link_speed);
1376             break;
1377           }
1378       }
1379 #endif
1380     if (hw_flags_chg)
1381       {
1382         if (LINK_STATE_ELOGS)
1383           {
1384             vlib_main_t * vm = vlib_get_main();
1385
1386             ELOG_TYPE_DECLARE(e) = {
1387               .format = "update-link-state: sw_if_index %d, new flags %d",
1388               .format_args = "i4i4",
1389             };
1390
1391             struct { u32 sw_if_index; u32 flags; } *ed;
1392             ed = ELOG_DATA (&vm->elog_main, e);
1393             ed->sw_if_index = xd->vlib_sw_if_index;
1394             ed->flags = hw_flags;
1395           }
1396         vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, hw_flags);
1397       }
1398 }
1399
1400 static uword
1401 dpdk_process (vlib_main_t * vm,
1402               vlib_node_runtime_t * rt,
1403               vlib_frame_t * f)
1404 {
1405   clib_error_t * error;
1406   vnet_main_t * vnm = vnet_get_main();
1407   dpdk_main_t * dm = &dpdk_main;
1408   ethernet_main_t * em = &ethernet_main;
1409   dpdk_device_t * xd;
1410   vlib_thread_main_t * tm = vlib_get_thread_main();
1411   void *vu_state;
1412   int i;
1413
1414   error = dpdk_lib_init (dm);
1415
1416   /* 
1417    * Turn on the input node if we found some devices to drive
1418    * and we're not running worker threads or i/o threads
1419    */
1420
1421   if (error == 0 && vec_len(dm->devices) > 0)
1422     {
1423         if (tm->n_vlib_mains == 1)
1424           vlib_node_set_state (vm, dpdk_input_node.index,
1425                                VLIB_NODE_STATE_POLLING);
1426         else if (tm->main_thread_is_io_node)
1427           vlib_node_set_state (vm, dpdk_io_input_node.index,
1428                                VLIB_NODE_STATE_POLLING);
1429         else if (!dm->have_io_threads)
1430           for (i=0; i < tm->n_vlib_mains; i++)
1431             if (vec_len(dm->devices_by_cpu[i]) > 0)
1432               vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1433                                    VLIB_NODE_STATE_POLLING);
1434     }
1435
1436   if (error)
1437     clib_error_report (error);
1438
1439   dpdk_vhost_user_process_init(&vu_state);
1440
1441   dm->io_thread_release = 1;
1442
1443   f64 now = vlib_time_now (vm);
1444   vec_foreach (xd, dm->devices)
1445     {
1446       dpdk_update_link_state (xd, now);
1447     }
1448
1449 { // Extra set up for bond interfaces:
1450   // 1. Setup MACs for bond interfaces and their slave links which was set
1451   //    in dpdk_port_setup() but needs to be done again here to take effect.
1452   // 2. Set max L3 packet size of each bond interface to the lowerst value of 
1453   //    its slave links 
1454   // 3. Set up info for bond interface related CLI support.
1455   int nports = rte_eth_dev_count();
1456   if (nports > 0) {
1457       for (i = 0; i < nports; i++) {
1458           struct rte_eth_dev_info dev_info;
1459           rte_eth_dev_info_get(i, &dev_info);
1460           if (!dev_info.driver_name)
1461               dev_info.driver_name = dev_info.pci_dev->driver->name;
1462           ASSERT(dev_info.driver_name);
1463           if (strncmp(dev_info.driver_name, "rte_bond_pmd", 12) == 0) {
1464               u8  addr[6]; 
1465               u8  slink[16];
1466               int nlink = rte_eth_bond_slaves_get(i, slink, 16);
1467               if (nlink > 0) {
1468                   vnet_hw_interface_t * bhi;
1469                   ethernet_interface_t * bei;
1470                   /* Get MAC of 1st slave link */
1471                   rte_eth_macaddr_get(slink[0], (struct ether_addr *)addr);
1472                   /* Set MAC of bounded interface to that of 1st slave link */
1473                   rte_eth_bond_mac_address_set(i, (struct ether_addr *)addr);
1474                   /* Populate MAC of bonded interface in VPP hw tables */
1475                   bhi = vnet_get_hw_interface(
1476                       vnm, dm->devices[i].vlib_hw_if_index);
1477                   bei = pool_elt_at_index(em->interfaces, bhi->hw_instance);
1478                   clib_memcpy(bhi->hw_address, addr, 6);
1479                   clib_memcpy(bei->address, addr, 6);
1480                   /* Init l3 packet size allowed on bonded interface */
1481                   bhi->max_l3_packet_bytes[VLIB_RX] = 
1482                   bhi->max_l3_packet_bytes[VLIB_TX] = 
1483                       ETHERNET_MAX_PACKET_BYTES - sizeof(ethernet_header_t);
1484                   while (nlink >= 1) { /* for all slave links */
1485                       int slave = slink[--nlink];
1486                       dpdk_device_t * sdev = &dm->devices[slave];
1487                       vnet_hw_interface_t * shi;
1488                       vnet_sw_interface_t * ssi;
1489                       /* Add MAC to all slave links except the first one */
1490                       if (nlink) rte_eth_dev_mac_addr_add(
1491                           slave, (struct ether_addr *)addr, 0);
1492                       /* Set slaves bitmap for bonded interface */
1493                       bhi->bond_info = clib_bitmap_set(
1494                           bhi->bond_info, sdev->vlib_hw_if_index, 1);
1495                       /* Set slave link flags on slave interface */
1496                       shi = vnet_get_hw_interface(vnm, sdev->vlib_hw_if_index);
1497                       ssi = vnet_get_sw_interface(vnm, sdev->vlib_sw_if_index);
1498                       shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1499                       ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1500                       /* Set l3 packet size allowed as the lowest of slave */
1501                       if (bhi->max_l3_packet_bytes[VLIB_RX] >
1502                           shi->max_l3_packet_bytes[VLIB_RX]) 
1503                           bhi->max_l3_packet_bytes[VLIB_RX] =
1504                           bhi->max_l3_packet_bytes[VLIB_TX] =
1505                               shi->max_l3_packet_bytes[VLIB_RX];
1506                   }
1507               }
1508           }
1509       }
1510   }
1511 }
1512
1513   while (1)
1514     {
1515       /*
1516        * check each time through the loop in case intervals are changed
1517        */
1518       f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1519                      dm->link_state_poll_interval : dm->stat_poll_interval;
1520
1521       vlib_process_wait_for_event_or_clock (vm, min_wait);
1522
1523       if (dpdk_get_admin_up_down_in_progress())
1524           /* skip the poll if an admin up down is in progress (on any interface) */
1525           continue;
1526
1527       vec_foreach (xd, dm->devices)
1528         {
1529           f64 now = vlib_time_now (vm);
1530           if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1531             dpdk_update_counters (xd, now);
1532           if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1533             dpdk_update_link_state (xd, now);
1534
1535       if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
1536           if (dpdk_vhost_user_process_if(vm, xd, vu_state) != 0)
1537               continue;
1538         }
1539     }
1540
1541   dpdk_vhost_user_process_cleanup(vu_state);
1542
1543   return 0; 
1544 }
1545
1546 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1547     .function = dpdk_process,
1548     .type = VLIB_NODE_TYPE_PROCESS,
1549     .name = "dpdk-process",
1550     .process_log2_n_stack_bytes = 17,
1551 };
1552
1553 int dpdk_set_stat_poll_interval (f64 interval)
1554 {
1555   if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
1556       return (VNET_API_ERROR_INVALID_VALUE);
1557
1558   dpdk_main.stat_poll_interval = interval;
1559
1560   return 0;
1561 }
1562
1563 int dpdk_set_link_state_poll_interval (f64 interval)
1564 {
1565   if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
1566       return (VNET_API_ERROR_INVALID_VALUE);
1567
1568   dpdk_main.link_state_poll_interval = interval;
1569
1570   return 0;
1571 }
1572
1573 clib_error_t *
1574 dpdk_init (vlib_main_t * vm)
1575 {
1576   dpdk_main_t * dm = &dpdk_main;
1577   vlib_node_t * ei;
1578   clib_error_t * error = 0;
1579   vlib_thread_main_t * tm = vlib_get_thread_main();
1580
1581   /* verify that structs are cacheline aligned */
1582   ASSERT(offsetof(dpdk_device_t, cacheline0) == 0);
1583   ASSERT(offsetof(dpdk_device_t, cacheline1) == CLIB_CACHE_LINE_BYTES);
1584   ASSERT(offsetof(dpdk_worker_t, cacheline0) == 0);
1585   ASSERT(offsetof(frame_queue_trace_t, cacheline0) == 0);
1586
1587   dm->vlib_main = vm;
1588   dm->vnet_main = vnet_get_main();
1589
1590   ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1591   if (ei == 0)
1592       return clib_error_return (0, "ethernet-input node AWOL");
1593
1594   dm->ethernet_input_node_index = ei->index;
1595
1596   dm->nchannels = 4;
1597   dm->num_mbufs = dm->num_mbufs ? dm->num_mbufs : NB_MBUF;
1598   vec_add1 (dm->eal_init_args, (u8 *) "vnet");
1599
1600   dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1601   dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1602   dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
1603
1604   /* $$$ use n_thread_stacks since it's known-good at this point */
1605   vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1606
1607   /* initialize EFD (early fast discard) default settings */
1608   dm->efd.enabled = DPDK_EFD_DISABLED;
1609   dm->efd.queue_hi_thresh = ((DPDK_EFD_DEFAULT_DEVICE_QUEUE_HI_THRESH_PCT *
1610                               DPDK_NB_RX_DESC_10GE)/100);
1611   dm->efd.consec_full_frames_hi_thresh =
1612       DPDK_EFD_DEFAULT_CONSEC_FULL_FRAMES_HI_THRESH;
1613
1614   /* vhost-user coalescence frames defaults */
1615   dm->vhost_coalesce_frames = 32;
1616   dm->vhost_coalesce_time = 1e-3;
1617
1618   /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1619   dm->buffer_flags_template = 
1620     (VLIB_BUFFER_TOTAL_LENGTH_VALID 
1621      | IP_BUFFER_L4_CHECKSUM_COMPUTED
1622      | IP_BUFFER_L4_CHECKSUM_CORRECT);
1623
1624   dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1625   dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1626
1627   /* init CLI */
1628   if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1629     return error;
1630
1631   return error;
1632 }
1633
1634 VLIB_INIT_FUNCTION (dpdk_init);
1635