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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/devices/dpdk/dpdk.h>
23 #include <vlib/unix/physmem.h>
24 #include <vlib/pci/pci.h>
30 #include <sys/mount.h>
34 #include "dpdk_priv.h"
36 dpdk_main_t dpdk_main;
38 /* force linker to link functions used by vlib and declared weak */
39 void *vlib_weakly_linked_functions[] = {
41 &rte_pktmbuf_pool_init,
44 #define LINK_STATE_ELOGS 0
46 #define DEFAULT_HUGE_DIR "/run/vpp/hugepages"
47 #define VPP_RUN_DIR "/run/vpp"
49 /* Port configuration, mildly modified Intel app values */
51 static struct rte_eth_conf port_conf_template = {
54 .header_split = 0, /**< Header Split disabled */
55 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
56 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
57 .hw_strip_crc = 0, /**< CRC stripped by hardware */
60 .mq_mode = ETH_MQ_TX_NONE,
65 dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd)
70 ASSERT (os_get_cpu_number () == 0);
72 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
74 vnet_hw_interface_set_flags (dm->vnet_main, xd->vlib_hw_if_index, 0);
75 rte_eth_dev_stop (xd->device_index);
78 rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
79 xd->tx_q_used, &xd->port_conf);
82 return clib_error_return (0, "rte_eth_dev_configure[%d]: err %d",
83 xd->device_index, rv);
85 /* Set up one TX-queue per worker thread */
86 for (j = 0; j < xd->tx_q_used; j++)
88 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
89 xd->cpu_socket, &xd->tx_conf);
91 /* retry with any other CPU socket */
93 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
94 SOCKET_ID_ANY, &xd->tx_conf);
100 return clib_error_return (0, "rte_eth_tx_queue_setup[%d]: err %d",
101 xd->device_index, rv);
103 for (j = 0; j < xd->rx_q_used; j++)
106 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
109 pktmbuf_pools[xd->cpu_socket_id_by_queue
112 /* retry with any other CPU socket */
114 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
117 pktmbuf_pools[xd->cpu_socket_id_by_queue
120 return clib_error_return (0, "rte_eth_rx_queue_setup[%d]: err %d",
121 xd->device_index, rv);
124 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
127 rv = rte_eth_dev_start (xd->device_index);
128 if (!rv && xd->default_mac_address)
129 rv = rte_eth_dev_default_mac_addr_set (xd->device_index,
130 (struct ether_addr *)
131 xd->default_mac_address);
133 clib_warning ("rte_eth_dev_start %d returned %d",
134 xd->device_index, rv);
140 dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
142 dpdk_main_t *dm = &dpdk_main;
143 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
146 if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags))
148 old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
150 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
151 xd->flags |= DPDK_DEVICE_FLAG_PROMISC;
153 xd->flags &= ~DPDK_DEVICE_FLAG_PROMISC;
155 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
157 if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
158 rte_eth_promiscuous_enable (xd->device_index);
160 rte_eth_promiscuous_disable (xd->device_index);
163 else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU (flags))
166 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
167 * driver to dynamically change the mtu. If/when the
168 * VIC firmware gets fixed, then this should be removed.
170 if (xd->pmd == VNET_DPDK_PMD_ENIC)
172 struct rte_eth_dev_info dev_info;
175 * Restore mtu to what has been set by CIMC in the firmware cfg.
177 rte_eth_dev_info_get (xd->device_index, &dev_info);
178 hi->max_packet_bytes = dev_info.max_rx_pktlen;
180 vlib_cli_output (vlib_get_main (),
181 "Cisco VIC mtu can only be changed "
182 "using CIMC then rebooting the server!");
188 xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
190 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
191 rte_eth_dev_stop (xd->device_index);
193 rv = rte_eth_dev_configure
194 (xd->device_index, xd->rx_q_used, xd->tx_q_used, &xd->port_conf);
197 vlib_cli_output (vlib_get_main (),
198 "rte_eth_dev_configure[%d]: err %d",
199 xd->device_index, rv);
201 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
203 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
205 int rv = rte_eth_dev_start (xd->device_index);
206 if (!rv && xd->default_mac_address)
207 rv = rte_eth_dev_default_mac_addr_set (xd->device_index,
208 (struct ether_addr *)
209 xd->default_mac_address);
211 clib_warning ("rte_eth_dev_start %d returned %d",
212 xd->device_index, rv);
220 dpdk_device_lock_init (dpdk_device_t * xd)
223 vec_validate (xd->lockp, xd->tx_q_used - 1);
224 for (q = 0; q < xd->tx_q_used; q++)
226 xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
227 CLIB_CACHE_LINE_BYTES);
228 memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
233 dpdk_device_lock_free (dpdk_device_t * xd)
237 for (q = 0; q < vec_len (xd->lockp); q++)
238 clib_mem_free ((void *) xd->lockp[q]);
239 vec_free (xd->lockp);
243 static clib_error_t *
244 dpdk_lib_init (dpdk_main_t * dm)
250 vlib_main_t *vm = vlib_get_main ();
251 vlib_thread_main_t *tm = vlib_get_thread_main ();
252 vnet_sw_interface_t *sw;
253 vnet_hw_interface_t *hi;
255 vlib_pci_addr_t last_pci_addr;
256 u32 last_pci_addr_port = 0;
257 vlib_thread_registration_t *tr, *tr_hqos;
260 u32 next_cpu = 0, next_hqos_cpu = 0;
261 u8 af_packet_port_id = 0;
262 last_pci_addr.as_u32 = ~0;
264 dm->input_cpu_first_index = 0;
265 dm->input_cpu_count = 1;
267 /* find out which cpus will be used for input */
268 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
269 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
271 if (tr && tr->count > 0)
273 dm->input_cpu_first_index = tr->first_index;
274 dm->input_cpu_count = tr->count;
277 vec_validate_aligned (dm->devices_by_cpu, tm->n_vlib_mains - 1,
278 CLIB_CACHE_LINE_BYTES);
280 vec_validate_aligned (dm->workers, tm->n_vlib_mains - 1,
281 CLIB_CACHE_LINE_BYTES);
283 dm->hqos_cpu_first_index = 0;
284 dm->hqos_cpu_count = 0;
286 /* find out which cpus will be used for I/O TX */
287 p_hqos = hash_get_mem (tm->thread_registrations_by_name, "hqos-threads");
288 tr_hqos = p_hqos ? (vlib_thread_registration_t *) p_hqos[0] : 0;
290 if (tr_hqos && tr_hqos->count > 0)
292 dm->hqos_cpu_first_index = tr_hqos->first_index;
293 dm->hqos_cpu_count = tr_hqos->count;
296 vec_validate_aligned (dm->devices_by_hqos_cpu, tm->n_vlib_mains - 1,
297 CLIB_CACHE_LINE_BYTES);
299 vec_validate_aligned (dm->hqos_threads, tm->n_vlib_mains - 1,
300 CLIB_CACHE_LINE_BYTES);
302 nports = rte_eth_dev_count ();
305 clib_warning ("DPDK drivers found no ports...");
309 clib_warning ("DPDK drivers found %d ports...", nports);
312 * All buffers are all allocated from the same rte_mempool.
313 * Thus they all have the same number of data bytes.
315 dm->vlib_buffer_free_list_index =
316 vlib_buffer_get_or_create_free_list (vm,
317 VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
320 if (dm->conf->enable_tcp_udp_checksum)
321 dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
322 | IP_BUFFER_L4_CHECKSUM_COMPUTED);
324 for (i = 0; i < nports; i++)
329 struct rte_eth_dev_info dev_info;
331 struct rte_eth_link l;
332 dpdk_device_config_t *devconf = 0;
333 vlib_pci_addr_t pci_addr;
336 rte_eth_dev_info_get (i, &dev_info);
337 if (dev_info.pci_dev) /* bonded interface has no pci info */
339 pci_addr.domain = dev_info.pci_dev->addr.domain;
340 pci_addr.bus = dev_info.pci_dev->addr.bus;
341 pci_addr.slot = dev_info.pci_dev->addr.devid;
342 pci_addr.function = dev_info.pci_dev->addr.function;
344 hash_get (dm->conf->device_config_index_by_pci_addr,
349 devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
351 devconf = &dm->conf->default_devconf;
353 /* Create vnet interface */
354 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
355 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
356 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
357 xd->cpu_socket = (i8) rte_eth_dev_socket_id (i);
359 /* Handle interface naming for devices with multiple ports sharing same PCI ID */
360 if (dev_info.pci_dev)
362 struct rte_eth_dev_info di = { 0 };
363 rte_eth_dev_info_get (i + 1, &di);
364 if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
365 memcmp (&dev_info.pci_dev->addr, &di.pci_dev->addr,
366 sizeof (struct rte_pci_addr)) == 0)
368 xd->interface_name_suffix = format (0, "0");
369 last_pci_addr.as_u32 = pci_addr.as_u32;
370 last_pci_addr_port = i;
372 else if (pci_addr.as_u32 == last_pci_addr.as_u32)
374 xd->interface_name_suffix =
375 format (0, "%u", i - last_pci_addr_port);
379 last_pci_addr.as_u32 = ~0;
383 last_pci_addr.as_u32 = ~0;
385 clib_memcpy (&xd->tx_conf, &dev_info.default_txconf,
386 sizeof (struct rte_eth_txconf));
387 if (dm->conf->no_multi_seg)
389 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
390 port_conf_template.rxmode.jumbo_frame = 0;
394 xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
395 port_conf_template.rxmode.jumbo_frame = 1;
396 xd->flags |= DPDK_DEVICE_FLAG_MAYBE_MULTISEG;
399 clib_memcpy (&xd->port_conf, &port_conf_template,
400 sizeof (struct rte_eth_conf));
402 xd->tx_q_used = clib_min (dev_info.max_tx_queues, tm->n_vlib_mains);
404 if (devconf->num_tx_queues > 0
405 && devconf->num_tx_queues < xd->tx_q_used)
406 xd->tx_q_used = clib_min (xd->tx_q_used, devconf->num_tx_queues);
408 if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
413 if (devconf->num_rx_queues > 1
414 && dev_info.max_rx_queues >= devconf->num_rx_queues)
416 xd->rx_q_used = devconf->num_rx_queues;
417 xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
418 if (devconf->rss_fn == 0)
419 xd->port_conf.rx_adv_conf.rss_conf.rss_hf =
420 ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
422 xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
427 xd->flags |= DPDK_DEVICE_FLAG_PMD;
429 /* workaround for drivers not setting driver_name */
430 if ((!dev_info.driver_name) && (dev_info.pci_dev))
431 dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
433 ASSERT (dev_info.driver_name);
439 #define _(s,f) else if (dev_info.driver_name && \
440 !strcmp(dev_info.driver_name, s)) \
441 xd->pmd = VNET_DPDK_PMD_##f;
447 xd->pmd = VNET_DPDK_PMD_UNKNOWN;
449 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
450 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
451 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
456 case VNET_DPDK_PMD_E1000EM:
457 case VNET_DPDK_PMD_IGB:
458 case VNET_DPDK_PMD_IGBVF:
459 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
463 case VNET_DPDK_PMD_IXGBE:
464 case VNET_DPDK_PMD_IXGBEVF:
465 case VNET_DPDK_PMD_THUNDERX:
466 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
468 case VNET_DPDK_PMD_DPAA2:
469 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
473 case VNET_DPDK_PMD_ENIC:
474 rte_eth_link_get_nowait (i, &l);
475 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
476 if (l.link_speed == 40000)
477 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
479 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
482 /* Intel Fortville */
483 case VNET_DPDK_PMD_I40E:
484 case VNET_DPDK_PMD_I40EVF:
485 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
486 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
488 switch (dev_info.pci_dev->id.device_id)
490 case I40E_DEV_ID_10G_BASE_T:
491 case I40E_DEV_ID_SFP_XL710:
492 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
494 case I40E_DEV_ID_QSFP_A:
495 case I40E_DEV_ID_QSFP_B:
496 case I40E_DEV_ID_QSFP_C:
497 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
500 rte_eth_link_get_nowait (i, &l);
501 xd->port_type = l.link_speed == 10000 ?
502 VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
505 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
509 case VNET_DPDK_PMD_CXGBE:
510 switch (dev_info.pci_dev->id.device_id)
512 case 0x540d: /* T580-CR */
513 case 0x5410: /* T580-LP-cr */
514 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
516 case 0x5403: /* T540-CR */
517 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
520 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
524 case VNET_DPDK_PMD_MLX5:
526 char *pn_100g[] = { "MCX415A-CCAT", "MCX416A-CCAT", 0 };
527 char *pn_40g[] = { "MCX413A-BCAT", "MCX414A-BCAT",
528 "MCX415A-BCAT", "MCX416A-BCAT", "MCX4131A-BCAT", 0
530 char *pn_10g[] = { "MCX4111A-XCAT", "MCX4121A-XCAT", 0 };
532 vlib_pci_device_t *pd = vlib_get_pci_device (&pci_addr);
536 pn = format (0, "%U%c",
537 format_vlib_pci_vpd, pd->vpd_r, "PN", 0);
543 while (!found && c[0])
545 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
547 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_100G;
554 while (!found && c[0])
556 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
558 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
565 while (!found && c[0])
567 if (strncmp ((char *) pn, c[0], strlen (c[0])) == 0)
569 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
579 /* Intel Red Rock Canyon */
580 case VNET_DPDK_PMD_FM10K:
581 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
585 case VNET_DPDK_PMD_VIRTIO:
586 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
587 xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
588 xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
592 case VNET_DPDK_PMD_VMXNET3:
593 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
594 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
597 case VNET_DPDK_PMD_AF_PACKET:
598 xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
599 xd->af_packet_port_id = af_packet_port_id++;
602 case VNET_DPDK_PMD_BOND:
603 xd->flags |= DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE;
604 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
608 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
611 if (devconf->num_rx_desc)
612 xd->nb_rx_desc = devconf->num_rx_desc;
614 if (devconf->num_tx_desc)
615 xd->nb_tx_desc = devconf->num_tx_desc;
619 * Ensure default mtu is not > the mtu read from the hardware.
620 * Otherwise rte_eth_dev_configure() will fail and the port will
623 if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
626 * This device does not support the platforms's max frame
627 * size. Use it's advertised mru instead.
629 xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
633 xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
636 * Some platforms do not account for Ethernet FCS (4 bytes) in
637 * MTU calculations. To interop with them increase mru but only
638 * if the device's settings can support it.
640 if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
641 xd->port_conf.rxmode.hw_strip_crc)
644 * Allow additional 4 bytes (for Ethernet FCS). These bytes are
645 * stripped by h/w and so will not consume any buffer memory.
647 xd->port_conf.rxmode.max_rx_pkt_len += 4;
651 if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
653 f64 now = vlib_time_now (vm);
655 rnd = (u32) (now * 1e6);
656 rnd = random_u32 (&rnd);
657 clib_memcpy (addr + 2, &rnd, sizeof (rnd));
662 rte_eth_macaddr_get (i, (struct ether_addr *) addr);
664 if (xd->tx_q_used < tm->n_vlib_mains)
665 dpdk_device_lock_init (xd);
667 xd->device_index = xd - dm->devices;
668 ASSERT (i == xd->device_index);
669 xd->per_interface_next_index = ~0;
671 /* assign interface to input thread */
672 dpdk_device_and_queue_t *dq;
675 if (devconf->workers)
680 clib_bitmap_foreach (i, devconf->workers, ({
681 int cpu = dm->input_cpu_first_index + i;
682 unsigned lcore = vlib_worker_threads[cpu].lcore_id;
683 vec_validate(xd->cpu_socket_id_by_queue, q);
684 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
685 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
686 dq->device = xd->device_index;
692 for (q = 0; q < xd->rx_q_used; q++)
694 int cpu = dm->input_cpu_first_index + next_cpu;
695 unsigned lcore = vlib_worker_threads[cpu].lcore_id;
698 * numa node for worker thread handling this queue
699 * needed for taking buffers from the right mempool
701 vec_validate (xd->cpu_socket_id_by_queue, q);
702 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id (lcore);
705 * construct vector of (device,queue) pairs for each worker thread
707 vec_add2 (dm->devices_by_cpu[cpu], dq, 1);
708 dq->device = xd->device_index;
712 if (next_cpu == dm->input_cpu_count)
717 if (devconf->hqos_enabled)
719 xd->flags |= DPDK_DEVICE_FLAG_HQOS;
721 if (devconf->hqos.hqos_thread_valid)
723 int cpu = dm->hqos_cpu_first_index + devconf->hqos.hqos_thread;
725 if (devconf->hqos.hqos_thread >= dm->hqos_cpu_count)
726 return clib_error_return (0, "invalid HQoS thread index");
728 vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
729 dq->device = xd->device_index;
734 int cpu = dm->hqos_cpu_first_index + next_hqos_cpu;
736 if (dm->hqos_cpu_count == 0)
737 return clib_error_return (0, "no HQoS threads available");
739 vec_add2 (dm->devices_by_hqos_cpu[cpu], dq, 1);
740 dq->device = xd->device_index;
744 if (next_hqos_cpu == dm->hqos_cpu_count)
747 devconf->hqos.hqos_thread_valid = 1;
748 devconf->hqos.hqos_thread = cpu;
752 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
753 CLIB_CACHE_LINE_BYTES);
754 for (j = 0; j < tm->n_vlib_mains; j++)
756 vec_validate_ha (xd->tx_vectors[j], xd->nb_tx_desc,
757 sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
758 vec_reset_length (xd->tx_vectors[j]);
761 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
762 CLIB_CACHE_LINE_BYTES);
763 for (j = 0; j < xd->rx_q_used; j++)
765 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
766 CLIB_CACHE_LINE_BYTES);
767 vec_reset_length (xd->rx_vectors[j]);
770 vec_validate_aligned (xd->d_trace_buffers, tm->n_vlib_mains,
771 CLIB_CACHE_LINE_BYTES);
773 rv = dpdk_port_setup (dm, xd);
778 if (devconf->hqos_enabled)
780 rv = dpdk_port_setup_hqos (xd, &devconf->hqos);
785 /* count the number of descriptors used for this device */
786 nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
788 error = ethernet_register_interface
789 (dm->vnet_main, dpdk_device_class.index, xd->device_index,
790 /* ethernet address */ addr,
791 &xd->vlib_hw_if_index, dpdk_flag_change);
795 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
796 xd->vlib_sw_if_index = sw->sw_if_index;
797 hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
800 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
801 * driver to dynamically change the mtu. If/when the
802 * VIC firmware gets fixed, then this should be removed.
804 if (xd->pmd == VNET_DPDK_PMD_ENIC)
807 * Initialize mtu to what has been set by CIMC in the firmware cfg.
809 hi->max_packet_bytes = dev_info.max_rx_pktlen;
810 if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
811 vlan_strip = 1; /* remove vlan tag from VIC port by default */
813 clib_warning ("VLAN strip disabled for interface\n");
815 else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
821 vlan_off = rte_eth_dev_get_vlan_offload (xd->device_index);
822 vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
823 xd->port_conf.rxmode.hw_vlan_strip = vlan_off;
824 if (rte_eth_dev_set_vlan_offload (xd->device_index, vlan_off) == 0)
825 clib_warning ("VLAN strip enabled for interface\n");
827 clib_warning ("VLAN strip cannot be supported by interface\n");
830 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
831 xd->port_conf.rxmode.max_rx_pkt_len - sizeof (ethernet_header_t);
833 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
836 if (nb_desc > dm->conf->num_mbufs)
837 clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
838 dm->conf->num_mbufs, nb_desc);
844 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
846 vlib_pci_main_t *pm = &pci_main;
848 vlib_pci_device_t *d;
850 int num_whitelisted = vec_len (conf->dev_confs);
853 pool_foreach (d, pm->pci_devs, ({
854 dpdk_device_config_t * devconf = 0;
855 vec_reset_length (pci_addr);
856 pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
858 if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && d->device_class != PCI_CLASS_PROCESSOR_CO)
863 uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
868 devconf = pool_elt_at_index (conf->dev_confs, p[0]);
872 if (d->vendor_id == 0x1af4 && d->device_id == 0x1000)
875 else if (d->vendor_id == 0x15ad && d->device_id == 0x07b0)
877 /* all Intel devices */
878 else if (d->vendor_id == 0x8086)
881 else if (d->vendor_id == 0x1137 && d->device_id == 0x0043)
884 else if (d->vendor_id == 0x1425 && (d->device_id & 0xe000) == 0x4000)
888 clib_warning ("Unsupported Ethernet PCI device 0x%04x:0x%04x found "
889 "at PCI address %s\n", (u16) d->vendor_id, (u16) d->device_id,
894 error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
900 pool_get (conf->dev_confs, devconf);
901 hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
902 devconf - conf->dev_confs);
903 devconf->pci_addr.as_u32 = d->bus_address.as_u32;
905 devconf->is_blacklisted = 1;
906 clib_error_report (error);
913 static clib_error_t *
914 dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr,
915 unformat_input_t * input, u8 is_default)
917 clib_error_t *error = 0;
919 dpdk_device_config_t *devconf;
920 unformat_input_t sub_input;
924 devconf = &conf->default_devconf;
928 p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
932 pool_get (conf->dev_confs, devconf);
933 hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32,
934 devconf - conf->dev_confs);
937 return clib_error_return (0,
938 "duplicate configuration for PCI address %U",
939 format_vlib_pci_addr, &pci_addr);
942 devconf->pci_addr.as_u32 = pci_addr.as_u32;
943 devconf->hqos_enabled = 0;
944 dpdk_device_config_hqos_default (&devconf->hqos);
949 unformat_skip_white_space (input);
950 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
952 if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
954 else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
956 else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
958 else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
960 else if (unformat (input, "workers %U", unformat_bitmap_list,
965 (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
967 error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
971 else if (unformat (input, "vlan-strip-offload off"))
972 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
973 else if (unformat (input, "vlan-strip-offload on"))
974 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
977 (input, "hqos %U", unformat_vlib_cli_sub_input, &sub_input))
979 devconf->hqos_enabled = 1;
980 error = unformat_hqos (&sub_input, &devconf->hqos);
984 else if (unformat (input, "hqos"))
986 devconf->hqos_enabled = 1;
990 error = clib_error_return (0, "unknown input `%U'",
991 format_unformat_error, input);
999 if (devconf->workers && devconf->num_rx_queues == 0)
1000 devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
1001 else if (devconf->workers &&
1002 clib_bitmap_count_set_bits (devconf->workers) !=
1003 devconf->num_rx_queues)
1005 clib_error_return (0,
1006 "%U: number of worker threadds must be "
1007 "equal to number of rx queues", format_vlib_pci_addr,
1013 static clib_error_t *
1014 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
1016 clib_error_t *error = 0;
1017 dpdk_main_t *dm = &dpdk_main;
1018 dpdk_config_main_t *conf = &dpdk_config_main;
1019 vlib_thread_main_t *tm = vlib_get_thread_main ();
1020 dpdk_device_config_t *devconf;
1021 vlib_pci_addr_t pci_addr;
1022 unformat_input_t sub_input;
1024 u8 *rte_cmd = 0, *ethname = 0;
1027 int num_whitelisted = 0;
1034 conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
1036 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1038 /* Prime the pump */
1039 if (unformat (input, "no-hugetlb"))
1041 vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
1045 else if (unformat (input, "enable-tcp-udp-checksum"))
1046 conf->enable_tcp_udp_checksum = 1;
1048 else if (unformat (input, "decimal-interface-names"))
1049 conf->interface_name_format_decimal = 1;
1051 else if (unformat (input, "no-multi-seg"))
1052 conf->no_multi_seg = 1;
1054 else if (unformat (input, "enable-cryptodev"))
1055 conf->cryptodev = 1;
1057 else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1061 dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
1069 (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1070 unformat_vlib_cli_sub_input, &sub_input))
1072 error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1079 else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1081 error = dpdk_device_config (conf, pci_addr, 0, 0);
1088 else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
1090 else if (unformat (input, "kni %d", &conf->num_kni))
1092 else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1094 else if (unformat (input, "socket-mem %s", &socket_mem))
1096 else if (unformat (input, "no-pci"))
1099 tmp = format (0, "--no-pci%c", 0);
1100 vec_add1 (conf->eal_init_args, tmp);
1102 else if (unformat (input, "poll-sleep %d", &dm->poll_sleep))
1106 else if (unformat(input, #a)) \
1108 tmp = format (0, "--%s%c", #a, 0); \
1109 vec_add1 (conf->eal_init_args, tmp); \
1111 foreach_eal_double_hyphen_predicate_arg
1114 else if (unformat(input, #a " %s", &s)) \
1116 if (!strncmp(#a, "huge-dir", 8)) \
1118 else if (!strncmp(#a, "file-prefix", 11)) \
1120 tmp = format (0, "--%s%c", #a, 0); \
1121 vec_add1 (conf->eal_init_args, tmp); \
1123 if (!strncmp(#a, "vdev", 4)) \
1124 if (strstr((char*)s, "af_packet")) \
1125 clib_warning ("af_packet obsoleted. Use CLI 'create host-interface'."); \
1126 vec_add1 (conf->eal_init_args, s); \
1128 foreach_eal_double_hyphen_arg
1131 else if (unformat(input, #a " %s", &s)) \
1133 tmp = format (0, "-%s%c", #b, 0); \
1134 vec_add1 (conf->eal_init_args, tmp); \
1136 vec_add1 (conf->eal_init_args, s); \
1138 foreach_eal_single_hyphen_arg
1141 else if (unformat(input, #a " %s", &s)) \
1143 tmp = format (0, "-%s%c", #b, 0); \
1144 vec_add1 (conf->eal_init_args, tmp); \
1146 vec_add1 (conf->eal_init_args, s); \
1147 conf->a##_set_manually = 1; \
1149 foreach_eal_single_hyphen_mandatory_arg
1151 else if (unformat (input, "default"))
1154 else if (unformat_skip_white_space (input))
1158 error = clib_error_return (0, "unknown input `%U'",
1159 format_unformat_error, input);
1164 if (!conf->uio_driver_name)
1165 conf->uio_driver_name = format (0, "uio_pci_generic%c", 0);
1168 * Use 1G huge pages if available.
1170 if (!no_huge && !huge_dir)
1172 u32 x, *mem_by_socket = 0;
1176 u8 less_than_1g = 1;
1179 umount (DEFAULT_HUGE_DIR);
1181 /* Process "socket-mem" parameter value */
1182 if (vec_len (socket_mem))
1184 unformat_input_t in;
1185 unformat_init_vector (&in, socket_mem);
1186 while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1188 if (unformat (&in, "%u,", &x))
1190 else if (unformat (&in, "%u", &x))
1192 else if (unformat (&in, ","))
1197 vec_add1 (mem_by_socket, x);
1202 /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1203 unformat_free (&in);
1209 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1211 vec_validate(mem_by_socket, c);
1212 mem_by_socket[c] = 256; /* default per-socket mem */
1218 /* check if available enough 1GB pages for each socket */
1220 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1222 int pages_avail, page_size, mem;
1224 vec_validate(mem_by_socket, c);
1225 mem = mem_by_socket[c];
1228 pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1230 if (pages_avail < 0 || page_size * pages_avail < mem)
1234 pages_avail = vlib_sysfs_get_free_hugepages(c, page_size * 1024);
1236 if (pages_avail < 0 || page_size * pages_avail < mem)
1241 if (mem_by_socket == 0)
1243 error = clib_error_return (0, "mem_by_socket NULL");
1246 _vec_len (mem_by_socket) = c + 1;
1248 /* regenerate socket_mem string */
1249 vec_foreach_index (x, mem_by_socket)
1250 socket_mem = format (socket_mem, "%s%u",
1251 socket_mem ? "," : "", mem_by_socket[x]);
1252 socket_mem = format (socket_mem, "%c", 0);
1254 vec_free (mem_by_socket);
1256 rv = mkdir (VPP_RUN_DIR, 0755);
1257 if (rv && errno != EEXIST)
1259 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1260 VPP_RUN_DIR, errno);
1264 rv = mkdir (DEFAULT_HUGE_DIR, 0755);
1265 if (rv && errno != EEXIST)
1267 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1268 DEFAULT_HUGE_DIR, errno);
1272 if (use_1g && !(less_than_1g && use_2m))
1275 mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1279 rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1283 return clib_error_return (0, "not enough free huge pages");
1288 error = clib_error_return (0, "mount failed %d", errno);
1292 tmp = format (0, "--huge-dir%c", 0);
1293 vec_add1 (conf->eal_init_args, tmp);
1294 tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1295 vec_add1 (conf->eal_init_args, tmp);
1298 tmp = format (0, "--file-prefix%c", 0);
1299 vec_add1 (conf->eal_init_args, tmp);
1300 tmp = format (0, "vpp%c", 0);
1301 vec_add1 (conf->eal_init_args, tmp);
1311 /* I'll bet that -c and -n must be the first and second args... */
1312 if (!conf->coremask_set_manually)
1314 vlib_thread_registration_t *tr;
1315 uword *coremask = 0;
1318 /* main thread core */
1319 coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
1321 for (i = 0; i < vec_len (tm->registrations); i++)
1323 tr = tm->registrations[i];
1324 coremask = clib_bitmap_or (coremask, tr->coremask);
1327 vec_insert (conf->eal_init_args, 2, 1);
1328 conf->eal_init_args[1] = (u8 *) "-c";
1329 tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1330 conf->eal_init_args[2] = tmp;
1331 clib_bitmap_free (coremask);
1334 if (!conf->nchannels_set_manually)
1336 vec_insert (conf->eal_init_args, 2, 3);
1337 conf->eal_init_args[3] = (u8 *) "-n";
1338 tmp = format (0, "%d", conf->nchannels);
1339 conf->eal_init_args[4] = tmp;
1342 if (no_pci == 0 && geteuid () == 0)
1343 dpdk_bind_devices_to_uio (conf);
1346 if (devconf->x == 0 && conf->default_devconf.x > 0) \
1347 devconf->x = conf->default_devconf.x ;
1350 pool_foreach (devconf, conf->dev_confs, ({
1352 /* default per-device config items */
1353 foreach_dpdk_device_config_item
1355 /* add DPDK EAL whitelist/blacklist entry */
1356 if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1358 tmp = format (0, "-w%c", 0);
1359 vec_add1 (conf->eal_init_args, tmp);
1360 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1361 vec_add1 (conf->eal_init_args, tmp);
1363 else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1365 tmp = format (0, "-b%c", 0);
1366 vec_add1 (conf->eal_init_args, tmp);
1367 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1368 vec_add1 (conf->eal_init_args, tmp);
1375 /* set master-lcore */
1376 tmp = format (0, "--master-lcore%c", 0);
1377 vec_add1 (conf->eal_init_args, tmp);
1378 tmp = format (0, "%u%c", tm->main_lcore, 0);
1379 vec_add1 (conf->eal_init_args, tmp);
1381 /* set socket-mem */
1382 tmp = format (0, "--socket-mem%c", 0);
1383 vec_add1 (conf->eal_init_args, tmp);
1384 tmp = format (0, "%s%c", socket_mem, 0);
1385 vec_add1 (conf->eal_init_args, tmp);
1387 /* NULL terminate the "argv" vector, in case of stupidity */
1388 vec_add1 (conf->eal_init_args, 0);
1389 _vec_len (conf->eal_init_args) -= 1;
1391 /* Set up DPDK eal and packet mbuf pool early. */
1393 log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1395 rte_set_log_level (log_level);
1397 vm = vlib_get_main ();
1399 /* make copy of args as rte_eal_init tends to mess up with arg array */
1400 for (i = 1; i < vec_len (conf->eal_init_args); i++)
1401 conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1402 conf->eal_init_args[i]);
1405 rte_eal_init (vec_len (conf->eal_init_args),
1406 (char **) conf->eal_init_args);
1408 /* lazy umount hugepages */
1409 umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
1412 return clib_error_return (0, "rte_eal_init returned %d", ret);
1414 /* Dump the physical memory layout prior to creating the mbuf_pool */
1415 fprintf (stdout, "DPDK physical memory layout:\n");
1416 rte_dump_physmem_layout (stdout);
1418 /* main thread 1st */
1419 error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
1423 for (i = 0; i < RTE_MAX_LCORE; i++)
1425 error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1426 rte_lcore_to_socket_id (i));
1435 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1438 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1440 vnet_main_t *vnm = vnet_get_main ();
1441 struct rte_eth_link prev_link = xd->link;
1443 u8 hw_flags_chg = 0;
1445 /* only update link state for PMD interfaces */
1446 if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
1449 xd->time_last_link_update = now ? now : xd->time_last_link_update;
1450 memset (&xd->link, 0, sizeof (xd->link));
1451 rte_eth_link_get_nowait (xd->device_index, &xd->link);
1453 if (LINK_STATE_ELOGS)
1455 vlib_main_t *vm = vlib_get_main ();
1456 ELOG_TYPE_DECLARE (e) =
1459 "update-link-state: sw_if_index %d, admin_up %d,"
1460 "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1469 ed = ELOG_DATA (&vm->elog_main, e);
1470 ed->sw_if_index = xd->vlib_sw_if_index;
1471 ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1472 ed->old_link_state = (u8)
1473 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index);
1474 ed->new_link_state = (u8) xd->link.link_status;
1477 if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
1478 ((xd->link.link_status != 0) ^
1479 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index)))
1482 hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1485 if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1488 switch (xd->link.link_duplex)
1490 case ETH_LINK_HALF_DUPLEX:
1491 hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1493 case ETH_LINK_FULL_DUPLEX:
1494 hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1500 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1503 switch (xd->link.link_speed)
1505 case ETH_SPEED_NUM_10M:
1506 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1508 case ETH_SPEED_NUM_100M:
1509 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1511 case ETH_SPEED_NUM_1G:
1512 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1514 case ETH_SPEED_NUM_10G:
1515 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1517 case ETH_SPEED_NUM_40G:
1518 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1523 clib_warning ("unknown link speed %d", xd->link.link_speed);
1529 if (LINK_STATE_ELOGS)
1531 vlib_main_t *vm = vlib_get_main ();
1533 ELOG_TYPE_DECLARE (e) =
1536 "update-link-state: sw_if_index %d, new flags %d",.format_args
1544 ed = ELOG_DATA (&vm->elog_main, e);
1545 ed->sw_if_index = xd->vlib_sw_if_index;
1546 ed->flags = hw_flags;
1548 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, hw_flags);
1553 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1555 clib_error_t *error;
1556 vnet_main_t *vnm = vnet_get_main ();
1557 dpdk_main_t *dm = &dpdk_main;
1558 ethernet_main_t *em = ðernet_main;
1560 vlib_thread_main_t *tm = vlib_get_thread_main ();
1563 error = dpdk_lib_init (dm);
1566 * Turn on the input node if we found some devices to drive
1567 * and we're not running worker threads or i/o threads
1570 if (error == 0 && vec_len (dm->devices) > 0)
1572 if (tm->n_vlib_mains == 1)
1573 vlib_node_set_state (vm, dpdk_input_node.index,
1574 VLIB_NODE_STATE_POLLING);
1576 for (i = 0; i < tm->n_vlib_mains; i++)
1577 if (vec_len (dm->devices_by_cpu[i]) > 0)
1578 vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1579 VLIB_NODE_STATE_POLLING);
1583 clib_error_report (error);
1585 tm->worker_thread_release = 1;
1587 f64 now = vlib_time_now (vm);
1588 vec_foreach (xd, dm->devices)
1590 dpdk_update_link_state (xd, now);
1595 * Extra set up for bond interfaces:
1596 * 1. Setup MACs for bond interfaces and their slave links which was set
1597 * in dpdk_port_setup() but needs to be done again here to take effect.
1598 * 2. Set up info for bond interface related CLI support.
1600 int nports = rte_eth_dev_count ();
1603 for (i = 0; i < nports; i++)
1605 struct rte_eth_dev_info dev_info;
1606 rte_eth_dev_info_get (i, &dev_info);
1607 if (!dev_info.driver_name)
1608 dev_info.driver_name = dev_info.pci_dev->driver->driver.name;
1610 ASSERT (dev_info.driver_name);
1611 if (strncmp (dev_info.driver_name, "rte_bond_pmd", 12) == 0)
1615 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1618 vnet_hw_interface_t *bhi;
1619 ethernet_interface_t *bei;
1622 /* Get MAC of 1st slave link */
1623 rte_eth_macaddr_get (slink[0],
1624 (struct ether_addr *) addr);
1625 /* Set MAC of bounded interface to that of 1st slave link */
1627 rte_eth_bond_mac_address_set (i,
1628 (struct ether_addr *)
1631 clib_warning ("Failed to set MAC address");
1633 /* Populate MAC of bonded interface in VPP hw tables */
1635 vnet_get_hw_interface (vnm,
1636 dm->devices[i].vlib_hw_if_index);
1638 pool_elt_at_index (em->interfaces, bhi->hw_instance);
1639 clib_memcpy (bhi->hw_address, addr, 6);
1640 clib_memcpy (bei->address, addr, 6);
1641 /* Init l3 packet size allowed on bonded interface */
1642 bhi->max_packet_bytes = ETHERNET_MAX_PACKET_BYTES;
1643 bhi->max_l3_packet_bytes[VLIB_RX] =
1644 bhi->max_l3_packet_bytes[VLIB_TX] =
1645 ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1647 { /* for all slave links */
1648 int slave = slink[--nlink];
1649 dpdk_device_t *sdev = &dm->devices[slave];
1650 vnet_hw_interface_t *shi;
1651 vnet_sw_interface_t *ssi;
1652 /* Add MAC to all slave links except the first one */
1654 rte_eth_dev_mac_addr_add (slave,
1655 (struct ether_addr *)
1657 /* Set slaves bitmap for bonded interface */
1659 clib_bitmap_set (bhi->bond_info,
1660 sdev->vlib_hw_if_index, 1);
1661 /* Set slave link flags on slave interface */
1663 vnet_get_hw_interface (vnm, sdev->vlib_hw_if_index);
1665 vnet_get_sw_interface (vnm, sdev->vlib_sw_if_index);
1666 shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1667 ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1669 /* Set l3 packet size allowed as the lowest of slave */
1670 if (bhi->max_l3_packet_bytes[VLIB_RX] >
1671 shi->max_l3_packet_bytes[VLIB_RX])
1672 bhi->max_l3_packet_bytes[VLIB_RX] =
1673 bhi->max_l3_packet_bytes[VLIB_TX] =
1674 shi->max_l3_packet_bytes[VLIB_RX];
1676 /* Set max packet size allowed as the lowest of slave */
1677 if (bhi->max_packet_bytes > shi->max_packet_bytes)
1678 bhi->max_packet_bytes = shi->max_packet_bytes;
1689 * check each time through the loop in case intervals are changed
1691 f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1692 dm->link_state_poll_interval : dm->stat_poll_interval;
1694 vlib_process_wait_for_event_or_clock (vm, min_wait);
1696 if (dm->admin_up_down_in_progress)
1697 /* skip the poll if an admin up down is in progress (on any interface) */
1700 vec_foreach (xd, dm->devices)
1702 f64 now = vlib_time_now (vm);
1703 if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1704 dpdk_update_counters (xd, now);
1705 if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1706 dpdk_update_link_state (xd, now);
1715 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1716 .function = dpdk_process,
1717 .type = VLIB_NODE_TYPE_PROCESS,
1718 .name = "dpdk-process",
1719 .process_log2_n_stack_bytes = 17,
1724 dpdk_set_stat_poll_interval (f64 interval)
1726 if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
1727 return (VNET_API_ERROR_INVALID_VALUE);
1729 dpdk_main.stat_poll_interval = interval;
1735 dpdk_set_link_state_poll_interval (f64 interval)
1737 if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
1738 return (VNET_API_ERROR_INVALID_VALUE);
1740 dpdk_main.link_state_poll_interval = interval;
1746 dpdk_init (vlib_main_t * vm)
1748 dpdk_main_t *dm = &dpdk_main;
1750 clib_error_t *error = 0;
1751 vlib_thread_main_t *tm = vlib_get_thread_main ();
1753 /* verify that structs are cacheline aligned */
1754 STATIC_ASSERT (offsetof (dpdk_device_t, cacheline0) == 0,
1755 "Cache line marker must be 1st element in dpdk_device_t");
1756 STATIC_ASSERT (offsetof (dpdk_device_t, cacheline1) ==
1757 CLIB_CACHE_LINE_BYTES,
1758 "Data in cache line 0 is bigger than cache line size");
1759 STATIC_ASSERT (offsetof (dpdk_worker_t, cacheline0) == 0,
1760 "Cache line marker must be 1st element in dpdk_worker_t");
1761 STATIC_ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0,
1762 "Cache line marker must be 1st element in frame_queue_trace_t");
1765 dm->vnet_main = vnet_get_main ();
1766 dm->conf = &dpdk_config_main;
1768 ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1770 return clib_error_return (0, "ethernet-input node AWOL");
1772 dm->ethernet_input_node_index = ei->index;
1774 dm->conf->nchannels = 4;
1775 dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1776 vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1778 dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1779 dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1780 dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
1782 /* $$$ use n_thread_stacks since it's known-good at this point */
1783 vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1785 /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1786 dm->buffer_flags_template =
1787 (VLIB_BUFFER_TOTAL_LENGTH_VALID | VLIB_BUFFER_EXT_HDR_VALID
1788 | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
1790 dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1791 dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1794 if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1800 VLIB_INIT_FUNCTION (dpdk_init);
1804 * fd.io coding-style-patch-verification: ON
1807 * eval: (c-set-style "gnu")