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