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