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