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)
67 vlib_main_t * vm = vlib_get_main();
68 vlib_buffer_main_t * bm = vm->buffer_main;
72 ASSERT(os_get_cpu_number() == 0);
75 vnet_hw_interface_set_flags (dm->vnet_main, xd->vlib_hw_if_index, 0);
76 rte_eth_dev_stop (xd->device_index);
79 rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
80 xd->tx_q_used, &xd->port_conf);
83 return clib_error_return (0, "rte_eth_dev_configure[%d]: err %d",
84 xd->device_index, rv);
86 /* Set up one TX-queue per worker thread */
87 for (j = 0; j < xd->tx_q_used; j++)
89 rv = rte_eth_tx_queue_setup(xd->device_index, j, xd->nb_tx_desc,
90 xd->cpu_socket, &xd->tx_conf);
92 /* retry with any other CPU socket */
94 rv = rte_eth_tx_queue_setup(xd->device_index, j, xd->nb_tx_desc,
95 SOCKET_ID_ANY, &xd->tx_conf);
101 return clib_error_return (0, "rte_eth_tx_queue_setup[%d]: err %d",
102 xd->device_index, rv);
104 for (j = 0; j < xd->rx_q_used; j++)
107 rv = rte_eth_rx_queue_setup(xd->device_index, j, xd->nb_rx_desc,
109 bm->pktmbuf_pools[xd->cpu_socket_id_by_queue[j]]);
111 /* retry with any other CPU socket */
113 rv = rte_eth_rx_queue_setup(xd->device_index, j, xd->nb_rx_desc,
115 bm->pktmbuf_pools[xd->cpu_socket_id_by_queue[j]]);
117 return clib_error_return (0, "rte_eth_rx_queue_setup[%d]: err %d",
118 xd->device_index, rv);
123 rv = rte_eth_dev_start (xd->device_index);
125 clib_warning ("rte_eth_dev_start %d returned %d",
126 xd->device_index, rv);
131 static u32 dpdk_flag_change (vnet_main_t * vnm,
132 vnet_hw_interface_t * hi,
135 dpdk_main_t * dm = &dpdk_main;
136 dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
139 if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC(flags))
142 xd->promisc = flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
147 rte_eth_promiscuous_enable(xd->device_index);
149 rte_eth_promiscuous_disable(xd->device_index);
152 else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU(flags))
155 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
156 * driver to dynamically change the mtu. If/when the
157 * VIC firmware gets fixed, then this should be removed.
159 if (xd->pmd == VNET_DPDK_PMD_ENIC)
161 struct rte_eth_dev_info dev_info;
164 * Restore mtu to what has been set by CIMC in the firmware cfg.
166 rte_eth_dev_info_get(xd->device_index, &dev_info);
167 hi->max_packet_bytes = dev_info.max_rx_pktlen;
169 vlib_cli_output (vlib_get_main(),
170 "Cisco VIC mtu can only be changed "
171 "using CIMC then rebooting the server!");
177 xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
180 rte_eth_dev_stop (xd->device_index);
182 rv = rte_eth_dev_configure
189 vlib_cli_output (vlib_get_main(),
190 "rte_eth_dev_configure[%d]: err %d",
191 xd->device_index, rv);
193 rte_eth_dev_set_mtu(xd->device_index, hi->max_packet_bytes);
197 int rv = rte_eth_dev_start (xd->device_index);
199 clib_warning ("rte_eth_dev_start %d returned %d",
200 xd->device_index, rv);
208 dpdk_device_lock_init(dpdk_device_t * xd)
211 vec_validate(xd->lockp, xd->tx_q_used - 1);
212 for (q = 0; q < xd->tx_q_used; q++)
214 xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
215 CLIB_CACHE_LINE_BYTES);
216 memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
222 dpdk_device_lock_free(dpdk_device_t * xd)
226 for (q = 0; q < vec_len(xd->lockp); q++)
227 clib_mem_free((void *) xd->lockp[q]);
233 static clib_error_t *
234 dpdk_lib_init (dpdk_main_t * dm)
239 clib_error_t * error;
240 vlib_main_t * vm = vlib_get_main();
241 vlib_thread_main_t * tm = vlib_get_thread_main();
242 vlib_node_runtime_t * rt;
243 vnet_sw_interface_t * sw;
244 vnet_hw_interface_t * hi;
246 vlib_pci_addr_t last_pci_addr;
247 u32 last_pci_addr_port = 0;
248 vlib_thread_registration_t * tr;
252 u8 af_packet_port_id = 0;
253 last_pci_addr.as_u32 = ~0;
255 dm->input_cpu_first_index = 0;
256 dm->input_cpu_count = 1;
258 rt = vlib_node_get_runtime (vm, dpdk_input_node.index);
259 rt->function = dpdk_input_multiarch_select();
261 /* find out which cpus will be used for input */
262 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
263 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
265 if (tr && tr->count > 0)
267 dm->input_cpu_first_index = tr->first_index;
268 dm->input_cpu_count = tr->count;
271 vec_validate_aligned (dm->devices_by_cpu, tm->n_vlib_mains - 1,
272 CLIB_CACHE_LINE_BYTES);
274 vec_validate_aligned (dm->workers, tm->n_vlib_mains - 1,
275 CLIB_CACHE_LINE_BYTES);
277 nports = rte_eth_dev_count();
280 clib_warning ("DPDK drivers found no ports...");
284 clib_warning ("DPDK drivers found %d ports...", nports);
287 * All buffers are all allocated from the same rte_mempool.
288 * Thus they all have the same number of data bytes.
290 dm->vlib_buffer_free_list_index =
291 vlib_buffer_get_or_create_free_list (
292 vm, VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES, "dpdk rx");
294 if (dm->conf->enable_tcp_udp_checksum)
295 dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
296 | IP_BUFFER_L4_CHECKSUM_COMPUTED);
298 for (i = 0; i < nports; i++)
303 struct rte_eth_dev_info dev_info;
305 struct rte_eth_link l;
306 dpdk_device_config_t * devconf = 0;
307 vlib_pci_addr_t pci_addr;
310 rte_eth_dev_info_get(i, &dev_info);
311 if (dev_info.pci_dev) /* bonded interface has no pci info */
313 pci_addr.domain = dev_info.pci_dev->addr.domain;
314 pci_addr.bus = dev_info.pci_dev->addr.bus;
315 pci_addr.slot = dev_info.pci_dev->addr.devid;
316 pci_addr.function = dev_info.pci_dev->addr.function;
317 p = hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
321 devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
323 devconf = &dm->conf->default_devconf;
325 /* Create vnet interface */
326 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
327 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
328 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
329 xd->cpu_socket = (i8) rte_eth_dev_socket_id(i);
331 /* Handle interface naming for devices with multiple ports sharing same PCI ID */
332 if (dev_info.pci_dev)
334 struct rte_eth_dev_info di = {0};
335 rte_eth_dev_info_get (i + 1, &di);
336 if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
337 memcmp(&dev_info.pci_dev->addr, &di.pci_dev->addr, sizeof(struct rte_pci_addr)) == 0)
339 xd->interface_name_suffix = format (0, "0");
340 last_pci_addr.as_u32 = pci_addr.as_u32;
341 last_pci_addr_port = i;
343 else if (pci_addr.as_u32 == last_pci_addr.as_u32)
345 xd->interface_name_suffix = format (0, "%u", i - last_pci_addr_port);
349 last_pci_addr.as_u32 = ~0;
353 last_pci_addr.as_u32 = ~0;
355 clib_memcpy(&xd->tx_conf, &dev_info.default_txconf,
356 sizeof(struct rte_eth_txconf));
357 if (dm->conf->no_multi_seg)
359 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
360 port_conf_template.rxmode.jumbo_frame = 0;
364 xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
365 port_conf_template.rxmode.jumbo_frame = 1;
368 clib_memcpy(&xd->port_conf, &port_conf_template, sizeof(struct rte_eth_conf));
370 xd->tx_q_used = clib_min(dev_info.max_tx_queues, tm->n_vlib_mains);
372 if (devconf->num_tx_queues > 0 && devconf->num_tx_queues < xd->tx_q_used)
373 xd->tx_q_used = clib_min(xd->tx_q_used, devconf->num_tx_queues);
375 if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
377 rt->function = dpdk_input_rss_multiarch_select();
381 if (devconf->num_rx_queues > 1 && dev_info.max_rx_queues >= devconf->num_rx_queues)
383 xd->rx_q_used = devconf->num_rx_queues;
384 xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
385 if (devconf->rss_fn == 0)
386 xd->port_conf.rx_adv_conf.rss_conf.rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
388 xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
393 xd->dev_type = VNET_DPDK_DEV_ETH;
395 /* workaround for drivers not setting driver_name */
396 if ((!dev_info.driver_name) && (dev_info.pci_dev))
397 dev_info.driver_name = dev_info.pci_dev->driver->name;
398 ASSERT(dev_info.driver_name);
403 #define _(s,f) else if (dev_info.driver_name && \
404 !strcmp(dev_info.driver_name, s)) \
405 xd->pmd = VNET_DPDK_PMD_##f;
411 xd->pmd = VNET_DPDK_PMD_UNKNOWN;
416 case VNET_DPDK_PMD_E1000EM:
417 case VNET_DPDK_PMD_IGB:
418 case VNET_DPDK_PMD_IGBVF:
419 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
423 case VNET_DPDK_PMD_IXGBE:
424 case VNET_DPDK_PMD_IXGBEVF:
425 case VNET_DPDK_PMD_THUNDERX:
426 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
427 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
428 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
430 case VNET_DPDK_PMD_DPAA2:
431 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
435 case VNET_DPDK_PMD_ENIC:
436 rte_eth_link_get_nowait(i, &l);
437 xd->nb_rx_desc = DPDK_NB_RX_DESC_ENIC;
438 if (l.link_speed == 40000)
440 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
441 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
445 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
446 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
450 /* Intel Fortville */
451 case VNET_DPDK_PMD_I40E:
452 case VNET_DPDK_PMD_I40EVF:
453 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
454 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
455 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
457 switch (dev_info.pci_dev->id.device_id) {
458 case I40E_DEV_ID_10G_BASE_T:
459 case I40E_DEV_ID_SFP_XL710:
460 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
462 case I40E_DEV_ID_QSFP_A:
463 case I40E_DEV_ID_QSFP_B:
464 case I40E_DEV_ID_QSFP_C:
465 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
468 rte_eth_link_get_nowait(i, &l);
469 xd->port_type = l.link_speed == 10000 ?
470 VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
473 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
477 case VNET_DPDK_PMD_CXGBE:
478 switch (dev_info.pci_dev->id.device_id) {
479 case 0x540d: /* T580-CR */
480 case 0x5410: /* T580-LP-cr */
481 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
482 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
483 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
485 case 0x5403: /* T540-CR */
486 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
487 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
488 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
491 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
492 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
493 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
497 /* Intel Red Rock Canyon */
498 case VNET_DPDK_PMD_FM10K:
499 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
500 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
501 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
505 case VNET_DPDK_PMD_VIRTIO:
506 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
507 xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
508 xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
512 case VNET_DPDK_PMD_VMXNET3:
513 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
514 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
517 case VNET_DPDK_PMD_AF_PACKET:
518 xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
519 xd->af_packet_port_id = af_packet_port_id++;
522 case VNET_DPDK_PMD_BOND:
523 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
527 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
530 if (devconf->num_rx_desc)
531 xd->nb_rx_desc = devconf->num_rx_desc;
533 if (devconf->num_tx_desc)
534 xd->nb_tx_desc = devconf->num_tx_desc;
538 * Ensure default mtu is not > the mtu read from the hardware.
539 * Otherwise rte_eth_dev_configure() will fail and the port will
542 if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
545 * This device does not support the platforms's max frame
546 * size. Use it's advertised mru instead.
548 xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
552 xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
555 * Some platforms do not account for Ethernet FCS (4 bytes) in
556 * MTU calculations. To interop with them increase mru but only
557 * if the device's settings can support it.
559 if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
560 xd->port_conf.rxmode.hw_strip_crc)
563 * Allow additional 4 bytes (for Ethernet FCS). These bytes are
564 * stripped by h/w and so will not consume any buffer memory.
566 xd->port_conf.rxmode.max_rx_pkt_len += 4;
570 #if RTE_VERSION < RTE_VERSION_NUM(16, 4, 0, 0)
572 * Older VMXNET3 driver doesn't support jumbo / multi-buffer pkts
574 if (xd->pmd == VNET_DPDK_PMD_VMXNET3)
576 xd->port_conf.rxmode.max_rx_pkt_len = 1518;
577 xd->port_conf.rxmode.jumbo_frame = 0;
581 if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
583 f64 now = vlib_time_now(vm);
585 rnd = (u32) (now * 1e6);
586 rnd = random_u32 (&rnd);
587 clib_memcpy (addr+2, &rnd, sizeof(rnd));
592 rte_eth_macaddr_get(i,(struct ether_addr *)addr);
594 if (xd->tx_q_used < tm->n_vlib_mains)
595 dpdk_device_lock_init(xd);
597 xd->device_index = xd - dm->devices;
598 ASSERT(i == xd->device_index);
599 xd->per_interface_next_index = ~0;
601 /* assign interface to input thread */
602 dpdk_device_and_queue_t * dq;
605 if (devconf->workers)
609 clib_bitmap_foreach (i, devconf->workers, ({
610 int cpu = dm->input_cpu_first_index + i;
611 unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
612 vec_validate(xd->cpu_socket_id_by_queue, q);
613 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
614 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
615 dq->device = xd->device_index;
620 for (q = 0; q < xd->rx_q_used; q++)
622 int cpu = dm->input_cpu_first_index + next_cpu;
623 unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
626 * numa node for worker thread handling this queue
627 * needed for taking buffers from the right mempool
629 vec_validate(xd->cpu_socket_id_by_queue, q);
630 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
633 * construct vector of (device,queue) pairs for each worker thread
635 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
636 dq->device = xd->device_index;
640 if (next_cpu == dm->input_cpu_count)
644 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
645 CLIB_CACHE_LINE_BYTES);
646 for (j = 0; j < tm->n_vlib_mains; j++)
648 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
649 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
650 vec_reset_length (xd->tx_vectors[j]);
653 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
654 CLIB_CACHE_LINE_BYTES);
655 for (j = 0; j< xd->rx_q_used; j++)
657 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
658 CLIB_CACHE_LINE_BYTES);
659 vec_reset_length (xd->rx_vectors[j]);
662 rv = dpdk_port_setup(dm, xd);
667 /* count the number of descriptors used for this device */
668 nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
670 error = ethernet_register_interface
672 dpdk_device_class.index,
674 /* ethernet address */ addr,
675 &xd->vlib_hw_if_index,
680 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
681 xd->vlib_sw_if_index = sw->sw_if_index;
682 hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
685 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
686 * driver to dynamically change the mtu. If/when the
687 * VIC firmware gets fixed, then this should be removed.
689 if (xd->pmd == VNET_DPDK_PMD_ENIC)
692 * Initialize mtu to what has been set by CIMC in the firmware cfg.
694 hi->max_packet_bytes = dev_info.max_rx_pktlen;
695 if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
696 vlan_strip = 1; /* remove vlan tag from VIC port by default */
698 clib_warning("VLAN strip disabled for interface\n");
700 else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
706 vlan_off = rte_eth_dev_get_vlan_offload(xd->device_index);
707 vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
708 if (rte_eth_dev_set_vlan_offload(xd->device_index, vlan_off) == 0)
709 clib_warning("VLAN strip enabled for interface\n");
711 clib_warning("VLAN strip cannot be supported by interface\n");
714 #if RTE_VERSION < RTE_VERSION_NUM(16, 4, 0, 0)
716 * Older VMXNET3 driver doesn't support jumbo / multi-buffer pkts
718 else if (xd->pmd == VNET_DPDK_PMD_VMXNET3)
719 hi->max_packet_bytes = 1518;
722 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
723 xd->port_conf.rxmode.max_rx_pkt_len - sizeof(ethernet_header_t);
725 rte_eth_dev_set_mtu(xd->device_index, hi->max_packet_bytes);
728 #ifdef RTE_LIBRTE_KNI
729 if (dm->conf->num_kni) {
730 clib_warning("Initializing KNI interfaces...");
731 rte_kni_init(dm->conf->num_kni);
732 for (i = 0; i < dm->conf->num_kni; i++)
737 /* Create vnet interface */
738 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
739 xd->dev_type = VNET_DPDK_DEV_KNI;
741 xd->device_index = xd - dm->devices;
742 ASSERT(nports + i == xd->device_index);
743 xd->per_interface_next_index = ~0;
746 hash_set (dm->dpdk_device_by_kni_port_id, i, xd - dm->devices);
749 /* assign interface to input thread */
750 dpdk_device_and_queue_t * dq;
751 vec_add2(dm->devices_by_cpu[dm->input_cpu_first_index], dq, 1);
752 dq->device = xd->device_index;
755 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
756 CLIB_CACHE_LINE_BYTES);
757 for (j = 0; j < tm->n_vlib_mains; j++)
759 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
760 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
761 vec_reset_length (xd->tx_vectors[j]);
764 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
765 CLIB_CACHE_LINE_BYTES);
766 for (j = 0; j< xd->rx_q_used; j++)
768 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
769 CLIB_CACHE_LINE_BYTES);
770 vec_reset_length (xd->rx_vectors[j]);
773 /* FIXME Set up one TX-queue per worker thread */
776 f64 now = vlib_time_now(vm);
778 rnd = (u32) (now * 1e6);
779 rnd = random_u32 (&rnd);
781 clib_memcpy (addr+2, &rnd, sizeof(rnd));
786 error = ethernet_register_interface
788 dpdk_device_class.index,
790 /* ethernet address */ addr,
791 &xd->vlib_hw_if_index,
797 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
798 xd->vlib_sw_if_index = sw->sw_if_index;
799 hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
804 if (nb_desc > dm->conf->num_mbufs)
805 clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
806 dm->conf->num_mbufs, nb_desc);
808 /* init next vhost-user if index */
809 dm->next_vu_if_id = 0;
815 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
817 vlib_pci_main_t * pm = &pci_main;
818 clib_error_t * error;
819 vlib_pci_device_t * d;
820 pci_config_header_t * c;
822 int num_whitelisted = vec_len (conf->dev_confs);
824 pool_foreach (d, pm->pci_devs, ({
825 dpdk_device_config_t * devconf = 0;
826 c = &d->config0.header;
827 vec_reset_length (pci_addr);
828 pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
830 if (c->device_class != PCI_CLASS_NETWORK_ETHERNET)
835 uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
840 devconf = pool_elt_at_index (conf->dev_confs, p[0]);
844 if (c->vendor_id == 0x1af4 && c->device_id == 0x1000)
847 else if (c->vendor_id == 0x15ad && c->device_id == 0x07b0)
849 /* all Intel devices */
850 else if (c->vendor_id == 0x8086)
853 else if (c->vendor_id == 0x1137 && c->device_id == 0x0043)
856 else if (c->vendor_id == 0x1425 && (c->device_id & 0xe000) == 0x4000)
860 clib_warning ("Unsupported Ethernet PCI device 0x%04x:0x%04x found "
861 "at PCI address %s\n", (u16) c->vendor_id, (u16) c->device_id,
866 error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
872 pool_get (conf->dev_confs, devconf);
873 hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
874 devconf - conf->dev_confs);
875 devconf->pci_addr.as_u32 = d->bus_address.as_u32;
877 devconf->is_blacklisted = 1;
878 clib_error_report (error);
884 static clib_error_t *
885 dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr, unformat_input_t * input, u8 is_default)
887 clib_error_t * error = 0;
889 dpdk_device_config_t * devconf;
890 unformat_input_t sub_input;
894 devconf = &conf->default_devconf;
898 p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
902 pool_get (conf->dev_confs, devconf);
903 hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32, devconf - conf->dev_confs);
906 return clib_error_return(0, "duplicate configuration for PCI address %U",
907 format_vlib_pci_addr, &pci_addr);
910 devconf->pci_addr.as_u32 = pci_addr.as_u32;
915 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
917 if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
919 else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
921 else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
923 else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
925 else if (unformat (input, "workers %U", unformat_bitmap_list,
928 else if (unformat (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
930 error = unformat_rss_fn(&sub_input, &devconf->rss_fn);
934 else if (unformat (input, "vlan-strip-offload off"))
935 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
936 else if (unformat (input, "vlan-strip-offload on"))
937 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
940 error = clib_error_return (0, "unknown input `%U'",
941 format_unformat_error, input);
949 if (devconf->workers && devconf->num_rx_queues == 0)
950 devconf->num_rx_queues = clib_bitmap_count_set_bits(devconf->workers);
951 else if (devconf->workers &&
952 clib_bitmap_count_set_bits(devconf->workers) != devconf->num_rx_queues)
953 error = clib_error_return (0, "%U: number of worker threadds must be "
954 "equal to number of rx queues",
955 format_vlib_pci_addr, &pci_addr);
960 static clib_error_t *
961 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
963 clib_error_t * error = 0;
964 dpdk_main_t * dm = &dpdk_main;
965 dpdk_config_main_t * conf = &dpdk_config_main;
966 vlib_thread_main_t * tm = vlib_get_thread_main();
967 dpdk_device_config_t * devconf;
968 vlib_pci_addr_t pci_addr;
969 unformat_input_t sub_input;
971 u8 * rte_cmd = 0, * ethname = 0;
974 int num_whitelisted = 0;
981 conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
983 // MATT-FIXME: inverted virtio-vhost logic to use virtio by default
984 conf->use_virtio_vhost = 1;
986 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
989 if (unformat (input, "no-hugetlb"))
991 vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
995 else if (unformat (input, "enable-tcp-udp-checksum"))
996 conf->enable_tcp_udp_checksum = 1;
998 else if (unformat (input, "decimal-interface-names"))
999 conf->interface_name_format_decimal = 1;
1001 else if (unformat (input, "no-multi-seg"))
1002 conf->no_multi_seg = 1;
1004 else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1007 error = dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~1, &sub_input, 1);
1012 else if (unformat (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1013 unformat_vlib_cli_sub_input, &sub_input))
1015 error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1022 else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1024 error = dpdk_device_config (conf, pci_addr, 0, 0);
1031 else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
1033 else if (unformat (input, "kni %d", &conf->num_kni))
1035 else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1037 else if (unformat (input, "socket-mem %s", &socket_mem))
1039 else if (unformat (input, "vhost-user-coalesce-frames %d", &conf->vhost_coalesce_frames))
1041 else if (unformat (input, "vhost-user-coalesce-time %f", &conf->vhost_coalesce_time))
1043 else if (unformat (input, "enable-vhost-user"))
1044 conf->use_virtio_vhost = 0;
1045 else if (unformat (input, "poll-sleep %d", &dm->poll_sleep))
1049 else if (unformat(input, #a)) \
1051 if (!strncmp(#a, "no-pci", 6)) \
1053 tmp = format (0, "--%s%c", #a, 0); \
1054 vec_add1 (conf->eal_init_args, tmp); \
1056 foreach_eal_double_hyphen_predicate_arg
1060 else if (unformat(input, #a " %s", &s)) \
1062 if (!strncmp(#a, "huge-dir", 8)) \
1064 else if (!strncmp(#a, "file-prefix", 11)) \
1066 tmp = format (0, "--%s%c", #a, 0); \
1067 vec_add1 (conf->eal_init_args, tmp); \
1069 vec_add1 (conf->eal_init_args, s); \
1071 foreach_eal_double_hyphen_arg
1075 else if (unformat(input, #a " %s", &s)) \
1077 tmp = format (0, "-%s%c", #b, 0); \
1078 vec_add1 (conf->eal_init_args, tmp); \
1080 vec_add1 (conf->eal_init_args, s); \
1082 foreach_eal_single_hyphen_arg
1086 else if (unformat(input, #a " %s", &s)) \
1088 tmp = format (0, "-%s%c", #b, 0); \
1089 vec_add1 (conf->eal_init_args, tmp); \
1091 vec_add1 (conf->eal_init_args, s); \
1092 conf->a##_set_manually = 1; \
1094 foreach_eal_single_hyphen_mandatory_arg
1097 else if (unformat(input, "default"))
1102 error = clib_error_return (0, "unknown input `%U'",
1103 format_unformat_error, input);
1108 if (!conf->uio_driver_name)
1109 conf->uio_driver_name = format (0, "igb_uio%c", 0);
1112 * Use 1G huge pages if available.
1114 if (!no_huge && !huge_dir)
1116 u32 x, * mem_by_socket = 0;
1120 u8 less_than_1g = 1;
1123 umount(DEFAULT_HUGE_DIR);
1125 /* Process "socket-mem" parameter value */
1126 if (vec_len (socket_mem))
1128 unformat_input_t in;
1129 unformat_init_vector(&in, socket_mem);
1130 while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1132 if (unformat (&in, "%u,", &x))
1134 else if (unformat (&in, "%u", &x))
1136 else if (unformat (&in, ","))
1141 vec_add1(mem_by_socket, x);
1146 /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1152 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1154 vec_validate(mem_by_socket, c);
1155 mem_by_socket[c] = 256; /* default per-socket mem */
1160 /* check if available enough 1GB pages for each socket */
1161 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1163 u32 pages_avail, page_size, mem;
1166 char * numa_path = "/sys/devices/system/node/node%u/";
1167 char * nonnuma_path = "/sys/kernel/mm/";
1168 char * suffix = "hugepages/hugepages-%ukB/free_hugepages%c";
1170 struct stat sb_numa, sb_nonnuma;
1172 p = format(p, numa_path, c);
1173 stat(numa_path, &sb_numa);
1174 stat(nonnuma_path, &sb_nonnuma);
1176 if (S_ISDIR(sb_numa.st_mode)) {
1177 path = (char*)format((u8*)path, "%s%s", p, suffix);
1178 } else if (S_ISDIR(sb_nonnuma.st_mode)) {
1179 path = (char*)format((u8*)path, "%s%s", nonnuma_path, suffix);
1187 vec_validate(mem_by_socket, c);
1188 mem = mem_by_socket[c];
1192 s = format (s, path, page_size * 1024, 0);
1193 vlib_sysfs_read ((char *) s, "%u", &pages_avail);
1194 vec_reset_length (s);
1196 if (page_size * pages_avail < mem)
1201 s = format (s, path, page_size * 1024, 0);
1202 vlib_sysfs_read ((char *) s, "%u", &pages_avail);
1203 vec_reset_length (s);
1205 if (page_size * pages_avail < mem)
1213 if (mem_by_socket == 0)
1215 error = clib_error_return (0, "mem_by_socket NULL");
1218 _vec_len (mem_by_socket) = c + 1;
1220 /* regenerate socket_mem string */
1221 vec_foreach_index (x, mem_by_socket)
1222 socket_mem = format (socket_mem, "%s%u",
1223 socket_mem ? "," : "",
1225 socket_mem = format (socket_mem, "%c", 0);
1227 vec_free (mem_by_socket);
1229 rv = mkdir(VPP_RUN_DIR, 0755);
1230 if (rv && errno != EEXIST)
1232 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1233 VPP_RUN_DIR, errno);
1237 rv = mkdir(DEFAULT_HUGE_DIR, 0755);
1238 if (rv && errno != EEXIST)
1240 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1241 DEFAULT_HUGE_DIR, errno);
1245 if (use_1g && !(less_than_1g && use_2m))
1247 rv = mount("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1251 rv = mount("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1255 return clib_error_return (0, "not enough free huge pages");
1260 error = clib_error_return (0, "mount failed %d", errno);
1264 tmp = format (0, "--huge-dir%c", 0);
1265 vec_add1 (conf->eal_init_args, tmp);
1266 tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1267 vec_add1 (conf->eal_init_args, tmp);
1270 tmp = format (0, "--file-prefix%c", 0);
1271 vec_add1 (conf->eal_init_args, tmp);
1272 tmp = format (0, "vpp%c", 0);
1273 vec_add1 (conf->eal_init_args, tmp);
1283 /* I'll bet that -c and -n must be the first and second args... */
1284 if (!conf->coremask_set_manually)
1286 vlib_thread_registration_t * tr;
1287 uword * coremask = 0;
1290 /* main thread core */
1291 coremask = clib_bitmap_set(coremask, tm->main_lcore, 1);
1293 for (i = 0; i < vec_len (tm->registrations); i++)
1295 tr = tm->registrations[i];
1296 coremask = clib_bitmap_or(coremask, tr->coremask);
1299 vec_insert (conf->eal_init_args, 2, 1);
1300 conf->eal_init_args[1] = (u8 *) "-c";
1301 tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1302 conf->eal_init_args[2] = tmp;
1303 clib_bitmap_free(coremask);
1306 if (!conf->nchannels_set_manually)
1308 vec_insert (conf->eal_init_args, 2, 3);
1309 conf->eal_init_args[3] = (u8 *) "-n";
1310 tmp = format (0, "%d", conf->nchannels);
1311 conf->eal_init_args[4] = tmp;
1314 if (no_pci == 0 && geteuid() == 0)
1315 dpdk_bind_devices_to_uio(conf);
1318 if (devconf->x == 0 && conf->default_devconf.x > 0) \
1319 devconf->x = conf->default_devconf.x ;
1321 pool_foreach (devconf, conf->dev_confs, ({
1323 /* default per-device config items */
1324 foreach_dpdk_device_config_item
1326 /* add DPDK EAL whitelist/blacklist entry */
1327 if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1329 tmp = format (0, "-w%c", 0);
1330 vec_add1 (conf->eal_init_args, tmp);
1331 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1332 vec_add1 (conf->eal_init_args, tmp);
1334 else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1336 tmp = format (0, "-b%c", 0);
1337 vec_add1 (conf->eal_init_args, tmp);
1338 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1339 vec_add1 (conf->eal_init_args, tmp);
1345 /* set master-lcore */
1346 tmp = format (0, "--master-lcore%c", 0);
1347 vec_add1 (conf->eal_init_args, tmp);
1348 tmp = format (0, "%u%c", tm->main_lcore, 0);
1349 vec_add1 (conf->eal_init_args, tmp);
1351 /* set socket-mem */
1352 tmp = format (0, "--socket-mem%c", 0);
1353 vec_add1 (conf->eal_init_args, tmp);
1354 tmp = format (0, "%s%c", socket_mem, 0);
1355 vec_add1 (conf->eal_init_args, tmp);
1357 /* NULL terminate the "argv" vector, in case of stupidity */
1358 vec_add1 (conf->eal_init_args, 0);
1359 _vec_len(conf->eal_init_args) -= 1;
1361 /* Set up DPDK eal and packet mbuf pool early. */
1363 log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1365 rte_set_log_level (log_level);
1367 vm = vlib_get_main ();
1369 /* make copy of args as rte_eal_init tends to mess up with arg array */
1370 for (i = 1; i < vec_len(conf->eal_init_args); i++)
1371 conf->eal_init_args_str = format(conf->eal_init_args_str, "%s ",
1372 conf->eal_init_args[i]);
1374 ret = rte_eal_init(vec_len(conf->eal_init_args), (char **) conf->eal_init_args);
1376 /* lazy umount hugepages */
1377 umount2(DEFAULT_HUGE_DIR, MNT_DETACH);
1380 return clib_error_return (0, "rte_eal_init returned %d", ret);
1382 /* Dump the physical memory layout prior to creating the mbuf_pool */
1383 fprintf(stdout, "DPDK physical memory layout:\n");
1384 rte_dump_physmem_layout(stdout);
1386 /* main thread 1st */
1387 error = vlib_buffer_pool_create(vm, conf->num_mbufs, rte_socket_id());
1391 for (i = 0; i < RTE_MAX_LCORE; i++)
1393 error = vlib_buffer_pool_create(vm, conf->num_mbufs,
1394 rte_lcore_to_socket_id(i));
1403 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1405 void dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1407 vnet_main_t * vnm = vnet_get_main();
1408 struct rte_eth_link prev_link = xd->link;
1410 u8 hw_flags_chg = 0;
1412 /* only update link state for PMD interfaces */
1413 if (xd->dev_type != VNET_DPDK_DEV_ETH)
1416 xd->time_last_link_update = now ? now : xd->time_last_link_update;
1417 memset(&xd->link, 0, sizeof(xd->link));
1418 rte_eth_link_get_nowait (xd->device_index, &xd->link);
1420 if (LINK_STATE_ELOGS)
1422 vlib_main_t * vm = vlib_get_main();
1423 ELOG_TYPE_DECLARE(e) = {
1425 "update-link-state: sw_if_index %d, admin_up %d,"
1426 "old link_state %d new link_state %d",
1427 .format_args = "i4i1i1i1",
1430 struct { u32 sw_if_index; u8 admin_up;
1431 u8 old_link_state; u8 new_link_state;} *ed;
1432 ed = ELOG_DATA (&vm->elog_main, e);
1433 ed->sw_if_index = xd->vlib_sw_if_index;
1434 ed->admin_up = xd->admin_up;
1435 ed->old_link_state = (u8)
1436 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index);
1437 ed->new_link_state = (u8) xd->link.link_status;
1440 if ((xd->admin_up == 1) &&
1441 ((xd->link.link_status != 0) ^
1442 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index)))
1445 hw_flags |= (xd->link.link_status ?
1446 VNET_HW_INTERFACE_FLAG_LINK_UP: 0);
1449 if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1452 switch (xd->link.link_duplex)
1454 case ETH_LINK_HALF_DUPLEX:
1455 hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1457 case ETH_LINK_FULL_DUPLEX:
1458 hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1464 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
1465 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1468 switch (xd->link.link_speed)
1470 case ETH_SPEED_NUM_10M:
1471 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1473 case ETH_SPEED_NUM_100M:
1474 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1476 case ETH_SPEED_NUM_1G:
1477 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1479 case ETH_SPEED_NUM_10G:
1480 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1482 case ETH_SPEED_NUM_40G:
1483 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1488 clib_warning("unknown link speed %d", xd->link.link_speed);
1493 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1496 switch (xd->link.link_speed)
1498 case ETH_LINK_SPEED_10:
1499 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1501 case ETH_LINK_SPEED_100:
1502 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1504 case ETH_LINK_SPEED_1000:
1505 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1507 case ETH_LINK_SPEED_10000:
1508 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1510 case ETH_LINK_SPEED_40G:
1511 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1516 clib_warning("unknown link speed %d", xd->link.link_speed);
1523 if (LINK_STATE_ELOGS)
1525 vlib_main_t * vm = vlib_get_main();
1527 ELOG_TYPE_DECLARE(e) = {
1528 .format = "update-link-state: sw_if_index %d, new flags %d",
1529 .format_args = "i4i4",
1532 struct { u32 sw_if_index; u32 flags; } *ed;
1533 ed = ELOG_DATA (&vm->elog_main, e);
1534 ed->sw_if_index = xd->vlib_sw_if_index;
1535 ed->flags = hw_flags;
1537 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, hw_flags);
1542 dpdk_process (vlib_main_t * vm,
1543 vlib_node_runtime_t * rt,
1546 clib_error_t * error;
1547 vnet_main_t * vnm = vnet_get_main();
1548 dpdk_main_t * dm = &dpdk_main;
1549 ethernet_main_t * em = ðernet_main;
1551 vlib_thread_main_t * tm = vlib_get_thread_main();
1557 error = dpdk_lib_init (dm);
1560 * Turn on the input node if we found some devices to drive
1561 * and we're not running worker threads or i/o threads
1564 if (error == 0 && vec_len(dm->devices) > 0)
1566 if (tm->n_vlib_mains == 1)
1567 vlib_node_set_state (vm, dpdk_input_node.index,
1568 VLIB_NODE_STATE_POLLING);
1570 for (i=0; i < tm->n_vlib_mains; i++)
1571 if (vec_len(dm->devices_by_cpu[i]) > 0)
1572 vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1573 VLIB_NODE_STATE_POLLING);
1577 clib_error_report (error);
1580 dpdk_vhost_user_process_init(&vu_state);
1583 tm->worker_thread_release = 1;
1585 f64 now = vlib_time_now (vm);
1586 vec_foreach (xd, dm->devices)
1588 dpdk_update_link_state (xd, now);
1592 * Extra set up for bond interfaces:
1593 * 1. Setup MACs for bond interfaces and their slave links which was set
1594 * in dpdk_port_setup() but needs to be done again here to take effect.
1595 * 2. Set up info for bond interface related CLI support.
1597 int nports = rte_eth_dev_count();
1599 for (i = 0; i < nports; i++) {
1600 struct rte_eth_dev_info dev_info;
1601 rte_eth_dev_info_get(i, &dev_info);
1602 if (!dev_info.driver_name)
1603 dev_info.driver_name = dev_info.pci_dev->driver->name;
1604 ASSERT(dev_info.driver_name);
1605 if (strncmp(dev_info.driver_name, "rte_bond_pmd", 12) == 0) {
1608 int nlink = rte_eth_bond_slaves_get(i, slink, 16);
1610 vnet_hw_interface_t * bhi;
1611 ethernet_interface_t * bei;
1612 /* Get MAC of 1st slave link */
1613 rte_eth_macaddr_get(slink[0], (struct ether_addr *)addr);
1614 /* Set MAC of bounded interface to that of 1st slave link */
1615 rte_eth_bond_mac_address_set(i, (struct ether_addr *)addr);
1616 /* Populate MAC of bonded interface in VPP hw tables */
1617 bhi = vnet_get_hw_interface(
1618 vnm, dm->devices[i].vlib_hw_if_index);
1619 bei = pool_elt_at_index(em->interfaces, bhi->hw_instance);
1620 clib_memcpy(bhi->hw_address, addr, 6);
1621 clib_memcpy(bei->address, addr, 6);
1622 /* Init l3 packet size allowed on bonded interface */
1623 bhi->max_l3_packet_bytes[VLIB_RX] =
1624 bhi->max_l3_packet_bytes[VLIB_TX] =
1625 ETHERNET_MAX_PACKET_BYTES - sizeof(ethernet_header_t);
1626 while (nlink >= 1) { /* for all slave links */
1627 int slave = slink[--nlink];
1628 dpdk_device_t * sdev = &dm->devices[slave];
1629 vnet_hw_interface_t * shi;
1630 vnet_sw_interface_t * ssi;
1631 /* Add MAC to all slave links except the first one */
1632 if (nlink) rte_eth_dev_mac_addr_add(
1633 slave, (struct ether_addr *)addr, 0);
1634 /* Set slaves bitmap for bonded interface */
1635 bhi->bond_info = clib_bitmap_set(
1636 bhi->bond_info, sdev->vlib_hw_if_index, 1);
1637 /* Set slave link flags on slave interface */
1638 shi = vnet_get_hw_interface(vnm, sdev->vlib_hw_if_index);
1639 ssi = vnet_get_sw_interface(vnm, sdev->vlib_sw_if_index);
1640 shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1641 ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1652 * check each time through the loop in case intervals are changed
1654 f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1655 dm->link_state_poll_interval : dm->stat_poll_interval;
1657 vlib_process_wait_for_event_or_clock (vm, min_wait);
1659 if (dpdk_get_admin_up_down_in_progress())
1660 /* skip the poll if an admin up down is in progress (on any interface) */
1663 vec_foreach (xd, dm->devices)
1665 f64 now = vlib_time_now (vm);
1666 if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1667 dpdk_update_counters (xd, now);
1668 if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1669 dpdk_update_link_state (xd, now);
1672 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
1673 if (dpdk_vhost_user_process_if(vm, xd, vu_state) != 0)
1680 dpdk_vhost_user_process_cleanup(vu_state);
1686 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1687 .function = dpdk_process,
1688 .type = VLIB_NODE_TYPE_PROCESS,
1689 .name = "dpdk-process",
1690 .process_log2_n_stack_bytes = 17,
1693 int dpdk_set_stat_poll_interval (f64 interval)
1695 if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
1696 return (VNET_API_ERROR_INVALID_VALUE);
1698 dpdk_main.stat_poll_interval = interval;
1703 int dpdk_set_link_state_poll_interval (f64 interval)
1705 if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
1706 return (VNET_API_ERROR_INVALID_VALUE);
1708 dpdk_main.link_state_poll_interval = interval;
1714 dpdk_init (vlib_main_t * vm)
1716 dpdk_main_t * dm = &dpdk_main;
1718 clib_error_t * error = 0;
1719 vlib_thread_main_t * tm = vlib_get_thread_main();
1721 /* verify that structs are cacheline aligned */
1722 ASSERT(offsetof(dpdk_device_t, cacheline0) == 0);
1723 ASSERT(offsetof(dpdk_device_t, cacheline1) == CLIB_CACHE_LINE_BYTES);
1724 ASSERT(offsetof(dpdk_worker_t, cacheline0) == 0);
1725 ASSERT(offsetof(frame_queue_trace_t, cacheline0) == 0);
1728 dm->vnet_main = vnet_get_main();
1729 dm->conf = &dpdk_config_main;
1731 ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1733 return clib_error_return (0, "ethernet-input node AWOL");
1735 dm->ethernet_input_node_index = ei->index;
1737 dm->conf->nchannels = 4;
1738 dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1739 vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1741 dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1742 dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1743 dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
1745 /* $$$ use n_thread_stacks since it's known-good at this point */
1746 vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1748 /* initialize EFD (early fast discard) default settings */
1749 dm->efd.enabled = DPDK_EFD_DISABLED;
1750 dm->efd.queue_hi_thresh = ((DPDK_EFD_DEFAULT_DEVICE_QUEUE_HI_THRESH_PCT *
1751 DPDK_NB_RX_DESC_10GE)/100);
1752 dm->efd.consec_full_frames_hi_thresh =
1753 DPDK_EFD_DEFAULT_CONSEC_FULL_FRAMES_HI_THRESH;
1755 /* vhost-user coalescence frames defaults */
1756 dm->conf->vhost_coalesce_frames = 32;
1757 dm->conf->vhost_coalesce_time = 1e-3;
1759 /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1760 dm->buffer_flags_template =
1761 (VLIB_BUFFER_TOTAL_LENGTH_VALID
1762 | IP_BUFFER_L4_CHECKSUM_COMPUTED
1763 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1765 dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1766 dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1769 if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1775 VLIB_INIT_FUNCTION (dpdk_init);