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