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