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);
122 rte_eth_dev_start (xd->device_index);
127 static u32 dpdk_flag_change (vnet_main_t * vnm,
128 vnet_hw_interface_t * hi,
131 dpdk_main_t * dm = &dpdk_main;
132 dpdk_device_t * xd = vec_elt_at_index (dm->devices, hi->dev_instance);
135 if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC(flags))
138 xd->promisc = flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
143 rte_eth_promiscuous_enable(xd->device_index);
145 rte_eth_promiscuous_disable(xd->device_index);
148 else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU(flags))
151 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
152 * driver to dynamically change the mtu. If/when the
153 * VIC firmware gets fixed, then this should be removed.
155 if (xd->pmd == VNET_DPDK_PMD_ENIC)
157 struct rte_eth_dev_info dev_info;
160 * Restore mtu to what has been set by CIMC in the firmware cfg.
162 rte_eth_dev_info_get(xd->device_index, &dev_info);
163 hi->max_packet_bytes = dev_info.max_rx_pktlen;
165 vlib_cli_output (vlib_get_main(),
166 "Cisco VIC mtu can only be changed "
167 "using CIMC then rebooting the server!");
173 xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
176 rte_eth_dev_stop (xd->device_index);
178 rv = rte_eth_dev_configure
185 vlib_cli_output (vlib_get_main(),
186 "rte_eth_dev_configure[%d]: err %d",
187 xd->device_index, rv);
189 rte_eth_dev_set_mtu(xd->device_index, hi->max_packet_bytes);
192 rte_eth_dev_start (xd->device_index);
199 dpdk_device_lock_init(dpdk_device_t * xd)
202 vec_validate(xd->lockp, xd->tx_q_used - 1);
203 for (q = 0; q < xd->tx_q_used; q++)
205 xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
206 CLIB_CACHE_LINE_BYTES);
207 memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
213 dpdk_device_lock_free(dpdk_device_t * xd)
217 for (q = 0; q < vec_len(xd->lockp); q++)
218 clib_mem_free((void *) xd->lockp[q]);
224 static clib_error_t *
225 dpdk_lib_init (dpdk_main_t * dm)
230 clib_error_t * error;
231 vlib_main_t * vm = vlib_get_main();
232 vlib_thread_main_t * tm = vlib_get_thread_main();
233 vlib_node_runtime_t * rt;
234 vnet_sw_interface_t * sw;
235 vnet_hw_interface_t * hi;
237 vlib_pci_addr_t last_pci_addr;
238 u32 last_pci_addr_port = 0;
239 vlib_thread_registration_t * tr;
243 u8 af_packet_port_id = 0;
244 last_pci_addr.as_u32 = ~0;
246 dm->input_cpu_first_index = 0;
247 dm->input_cpu_count = 1;
249 rt = vlib_node_get_runtime (vm, dpdk_input_node.index);
250 rt->function = dpdk_input_multiarch_select();
252 /* find out which cpus will be used for input */
253 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
254 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
256 if (tr && tr->count > 0)
258 dm->input_cpu_first_index = tr->first_index;
259 dm->input_cpu_count = tr->count;
262 vec_validate_aligned (dm->devices_by_cpu, tm->n_vlib_mains - 1,
263 CLIB_CACHE_LINE_BYTES);
265 vec_validate_aligned (dm->workers, tm->n_vlib_mains - 1,
266 CLIB_CACHE_LINE_BYTES);
268 nports = rte_eth_dev_count();
271 clib_warning ("DPDK drivers found no ports...");
275 clib_warning ("DPDK drivers found %d ports...", nports);
278 * All buffers are all allocated from the same rte_mempool.
279 * Thus they all have the same number of data bytes.
281 dm->vlib_buffer_free_list_index =
282 vlib_buffer_get_or_create_free_list (
283 vm, VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES, "dpdk rx");
285 if (dm->conf->enable_tcp_udp_checksum)
286 dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
287 | IP_BUFFER_L4_CHECKSUM_COMPUTED);
289 for (i = 0; i < nports; i++)
294 struct rte_eth_dev_info dev_info;
296 struct rte_eth_link l;
297 dpdk_device_config_t * devconf = 0;
298 vlib_pci_addr_t pci_addr;
301 rte_eth_dev_info_get(i, &dev_info);
302 if (dev_info.pci_dev) /* bonded interface has no pci info */
304 pci_addr.domain = dev_info.pci_dev->addr.domain;
305 pci_addr.bus = dev_info.pci_dev->addr.bus;
306 pci_addr.slot = dev_info.pci_dev->addr.devid;
307 pci_addr.function = dev_info.pci_dev->addr.function;
308 p = hash_get (dm->conf->device_config_index_by_pci_addr, pci_addr.as_u32);
312 devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
314 devconf = &dm->conf->default_devconf;
316 /* Create vnet interface */
317 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
318 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
319 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
320 xd->cpu_socket = (i8) rte_eth_dev_socket_id(i);
322 /* Handle interface naming for devices with multiple ports sharing same PCI ID */
323 if (dev_info.pci_dev)
325 struct rte_eth_dev_info di = {0};
326 rte_eth_dev_info_get (i + 1, &di);
327 if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
328 memcmp(&dev_info.pci_dev->addr, &di.pci_dev->addr, sizeof(struct rte_pci_addr)) == 0)
330 xd->interface_name_suffix = format (0, "0");
331 last_pci_addr.as_u32 = pci_addr.as_u32;
332 last_pci_addr_port = i;
334 else if (pci_addr.as_u32 == last_pci_addr.as_u32)
336 xd->interface_name_suffix = format (0, "%u", i - last_pci_addr_port);
340 last_pci_addr.as_u32 = ~0;
344 last_pci_addr.as_u32 = ~0;
346 clib_memcpy(&xd->tx_conf, &dev_info.default_txconf,
347 sizeof(struct rte_eth_txconf));
348 if (dm->conf->no_multi_seg)
350 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
351 port_conf_template.rxmode.jumbo_frame = 0;
355 xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
356 port_conf_template.rxmode.jumbo_frame = 1;
359 clib_memcpy(&xd->port_conf, &port_conf_template, sizeof(struct rte_eth_conf));
361 xd->tx_q_used = clib_min(dev_info.max_tx_queues, tm->n_vlib_mains);
363 if (devconf->num_tx_queues > 0 && devconf->num_tx_queues < xd->tx_q_used)
364 xd->tx_q_used = clib_min(xd->tx_q_used, devconf->num_tx_queues);
366 if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
368 rt->function = dpdk_input_rss_multiarch_select();
372 if (devconf->num_rx_queues > 1 && dev_info.max_rx_queues >= devconf->num_rx_queues)
374 xd->rx_q_used = devconf->num_rx_queues;
375 xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
376 if (devconf->rss_fn == 0)
377 xd->port_conf.rx_adv_conf.rss_conf.rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
379 xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
384 xd->dev_type = VNET_DPDK_DEV_ETH;
386 /* workaround for drivers not setting driver_name */
387 if ((!dev_info.driver_name) && (dev_info.pci_dev))
388 dev_info.driver_name = dev_info.pci_dev->driver->name;
389 ASSERT(dev_info.driver_name);
394 #define _(s,f) else if (!strcmp(dev_info.driver_name, s)) \
395 xd->pmd = VNET_DPDK_PMD_##f;
401 xd->pmd = VNET_DPDK_PMD_UNKNOWN;
406 case VNET_DPDK_PMD_E1000EM:
407 case VNET_DPDK_PMD_IGB:
408 case VNET_DPDK_PMD_IGBVF:
409 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
413 case VNET_DPDK_PMD_IXGBE:
414 case VNET_DPDK_PMD_IXGBEVF:
415 case VNET_DPDK_PMD_THUNDERX:
416 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
417 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
418 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
420 case VNET_DPDK_PMD_DPAA2:
421 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
425 case VNET_DPDK_PMD_ENIC:
426 rte_eth_link_get_nowait(i, &l);
427 xd->nb_rx_desc = DPDK_NB_RX_DESC_ENIC;
428 if (l.link_speed == 40000)
430 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
431 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
435 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
436 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
440 /* Intel Fortville */
441 case VNET_DPDK_PMD_I40E:
442 case VNET_DPDK_PMD_I40EVF:
443 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
444 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
445 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
447 switch (dev_info.pci_dev->id.device_id) {
448 case I40E_DEV_ID_10G_BASE_T:
449 case I40E_DEV_ID_SFP_XL710:
450 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
452 case I40E_DEV_ID_QSFP_A:
453 case I40E_DEV_ID_QSFP_B:
454 case I40E_DEV_ID_QSFP_C:
455 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
458 rte_eth_link_get_nowait(i, &l);
459 xd->port_type = l.link_speed == 10000 ?
460 VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
463 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
467 case VNET_DPDK_PMD_CXGBE:
468 switch (dev_info.pci_dev->id.device_id) {
469 case 0x540d: /* T580-CR */
470 case 0x5410: /* T580-LP-cr */
471 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
472 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
473 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
475 case 0x5403: /* T540-CR */
476 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
477 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
478 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
481 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
482 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
483 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
487 /* Intel Red Rock Canyon */
488 case VNET_DPDK_PMD_FM10K:
489 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
490 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
491 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
495 case VNET_DPDK_PMD_VIRTIO:
496 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
497 xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
498 xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
502 case VNET_DPDK_PMD_VMXNET3:
503 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
504 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
507 case VNET_DPDK_PMD_AF_PACKET:
508 xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
509 xd->af_packet_port_id = af_packet_port_id++;
512 case VNET_DPDK_PMD_BOND:
513 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
517 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
520 if (devconf->num_rx_desc)
521 xd->nb_rx_desc = devconf->num_rx_desc;
523 if (devconf->num_tx_desc)
524 xd->nb_tx_desc = devconf->num_tx_desc;
528 * Ensure default mtu is not > the mtu read from the hardware.
529 * Otherwise rte_eth_dev_configure() will fail and the port will
532 if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
535 * This device does not support the platforms's max frame
536 * size. Use it's advertised mru instead.
538 xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
542 xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
545 * Some platforms do not account for Ethernet FCS (4 bytes) in
546 * MTU calculations. To interop with them increase mru but only
547 * if the device's settings can support it.
549 if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
550 xd->port_conf.rxmode.hw_strip_crc)
553 * Allow additional 4 bytes (for Ethernet FCS). These bytes are
554 * stripped by h/w and so will not consume any buffer memory.
556 xd->port_conf.rxmode.max_rx_pkt_len += 4;
560 #if RTE_VERSION < RTE_VERSION_NUM(16, 4, 0, 0)
562 * Older VMXNET3 driver doesn't support jumbo / multi-buffer pkts
564 if (xd->pmd == VNET_DPDK_PMD_VMXNET3)
566 xd->port_conf.rxmode.max_rx_pkt_len = 1518;
567 xd->port_conf.rxmode.jumbo_frame = 0;
571 if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
573 f64 now = vlib_time_now(vm);
575 rnd = (u32) (now * 1e6);
576 rnd = random_u32 (&rnd);
577 clib_memcpy (addr+2, &rnd, sizeof(rnd));
582 rte_eth_macaddr_get(i,(struct ether_addr *)addr);
584 if (xd->tx_q_used < tm->n_vlib_mains)
585 dpdk_device_lock_init(xd);
587 xd->device_index = xd - dm->devices;
588 ASSERT(i == xd->device_index);
589 xd->per_interface_next_index = ~0;
591 /* assign interface to input thread */
592 dpdk_device_and_queue_t * dq;
595 if (devconf->workers)
599 clib_bitmap_foreach (i, devconf->workers, ({
600 int cpu = dm->input_cpu_first_index + i;
601 unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
602 vec_validate(xd->cpu_socket_id_by_queue, q);
603 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
604 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
605 dq->device = xd->device_index;
610 for (q = 0; q < xd->rx_q_used; q++)
612 int cpu = dm->input_cpu_first_index + next_cpu;
613 unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
616 * numa node for worker thread handling this queue
617 * needed for taking buffers from the right mempool
619 vec_validate(xd->cpu_socket_id_by_queue, q);
620 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
623 * construct vector of (device,queue) pairs for each worker thread
625 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
626 dq->device = xd->device_index;
630 if (next_cpu == dm->input_cpu_count)
634 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
635 CLIB_CACHE_LINE_BYTES);
636 for (j = 0; j < tm->n_vlib_mains; j++)
638 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
639 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
640 vec_reset_length (xd->tx_vectors[j]);
643 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
644 CLIB_CACHE_LINE_BYTES);
645 for (j = 0; j< xd->rx_q_used; j++)
647 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
648 CLIB_CACHE_LINE_BYTES);
649 vec_reset_length (xd->rx_vectors[j]);
652 rv = dpdk_port_setup(dm, xd);
657 /* count the number of descriptors used for this device */
658 nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
660 error = ethernet_register_interface
662 dpdk_device_class.index,
664 /* ethernet address */ addr,
665 &xd->vlib_hw_if_index,
670 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
671 xd->vlib_sw_if_index = sw->sw_if_index;
672 hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
675 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
676 * driver to dynamically change the mtu. If/when the
677 * VIC firmware gets fixed, then this should be removed.
679 if (xd->pmd == VNET_DPDK_PMD_ENIC)
682 * Initialize mtu to what has been set by CIMC in the firmware cfg.
684 hi->max_packet_bytes = dev_info.max_rx_pktlen;
685 if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
686 vlan_strip = 1; /* remove vlan tag from VIC port by default */
688 clib_warning("VLAN strip disabled for interface\n");
690 else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
696 vlan_off = rte_eth_dev_get_vlan_offload(xd->device_index);
697 vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
698 if (rte_eth_dev_set_vlan_offload(xd->device_index, vlan_off) == 0)
699 clib_warning("VLAN strip enabled for interface\n");
701 clib_warning("VLAN strip cannot be supported by interface\n");
704 #if RTE_VERSION < RTE_VERSION_NUM(16, 4, 0, 0)
706 * Older VMXNET3 driver doesn't support jumbo / multi-buffer pkts
708 else if (xd->pmd == VNET_DPDK_PMD_VMXNET3)
709 hi->max_packet_bytes = 1518;
712 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
713 xd->port_conf.rxmode.max_rx_pkt_len - sizeof(ethernet_header_t);
715 rte_eth_dev_set_mtu(xd->device_index, hi->max_packet_bytes);
718 #ifdef RTE_LIBRTE_KNI
719 if (dm->conf->num_kni) {
720 clib_warning("Initializing KNI interfaces...");
721 rte_kni_init(dm->conf->num_kni);
722 for (i = 0; i < dm->conf->num_kni; i++)
727 /* Create vnet interface */
728 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
729 xd->dev_type = VNET_DPDK_DEV_KNI;
731 xd->device_index = xd - dm->devices;
732 ASSERT(nports + i == xd->device_index);
733 xd->per_interface_next_index = ~0;
736 hash_set (dm->dpdk_device_by_kni_port_id, i, xd - dm->devices);
739 /* assign interface to input thread */
740 dpdk_device_and_queue_t * dq;
741 vec_add2(dm->devices_by_cpu[dm->input_cpu_first_index], dq, 1);
742 dq->device = xd->device_index;
745 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
746 CLIB_CACHE_LINE_BYTES);
747 for (j = 0; j < tm->n_vlib_mains; j++)
749 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
750 sizeof(tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
751 vec_reset_length (xd->tx_vectors[j]);
754 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
755 CLIB_CACHE_LINE_BYTES);
756 for (j = 0; j< xd->rx_q_used; j++)
758 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE-1,
759 CLIB_CACHE_LINE_BYTES);
760 vec_reset_length (xd->rx_vectors[j]);
763 /* FIXME Set up one TX-queue per worker thread */
766 f64 now = vlib_time_now(vm);
768 rnd = (u32) (now * 1e6);
769 rnd = random_u32 (&rnd);
771 clib_memcpy (addr+2, &rnd, sizeof(rnd));
776 error = ethernet_register_interface
778 dpdk_device_class.index,
780 /* ethernet address */ addr,
781 &xd->vlib_hw_if_index,
787 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
788 xd->vlib_sw_if_index = sw->sw_if_index;
789 hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
794 if (nb_desc > dm->conf->num_mbufs)
795 clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
796 dm->conf->num_mbufs, nb_desc);
798 /* init next vhost-user if index */
799 dm->next_vu_if_id = 0;
805 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
807 vlib_pci_main_t * pm = &pci_main;
808 clib_error_t * error;
809 vlib_pci_device_t * d;
810 pci_config_header_t * c;
812 int num_whitelisted = vec_len (conf->dev_confs);
814 pool_foreach (d, pm->pci_devs, ({
815 dpdk_device_config_t * devconf = 0;
816 c = &d->config0.header;
817 vec_reset_length (pci_addr);
818 pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
820 if (c->device_class != PCI_CLASS_NETWORK_ETHERNET)
825 uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
830 devconf = pool_elt_at_index (conf->dev_confs, p[0]);
834 if (c->vendor_id == 0x1af4 && c->device_id == 0x1000)
837 else if (c->vendor_id == 0x15ad && c->device_id == 0x07b0)
839 /* all Intel devices */
840 else if (c->vendor_id == 0x8086)
843 else if (c->vendor_id == 0x1137 && c->device_id == 0x0043)
846 else if (c->vendor_id == 0x1425 && (c->device_id & 0xe000) == 0x4000)
850 clib_warning ("Unsupported Ethernet PCI device 0x%04x:0x%04x found "
851 "at PCI address %s\n", (u16) c->vendor_id, (u16) c->device_id,
856 error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
862 pool_get (conf->dev_confs, devconf);
863 hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
864 devconf - conf->dev_confs);
865 devconf->pci_addr.as_u32 = d->bus_address.as_u32;
867 devconf->is_blacklisted = 1;
868 clib_error_report (error);
874 static clib_error_t *
875 dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr, unformat_input_t * input, u8 is_default)
877 clib_error_t * error = 0;
879 dpdk_device_config_t * devconf;
880 unformat_input_t sub_input;
884 devconf = &conf->default_devconf;
888 p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
892 pool_get (conf->dev_confs, devconf);
893 hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32, devconf - conf->dev_confs);
896 return clib_error_return(0, "duplicate configuration for PCI address %U",
897 format_vlib_pci_addr, &pci_addr);
900 devconf->pci_addr.as_u32 = pci_addr.as_u32;
905 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
907 if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
909 else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
911 else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
913 else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
915 else if (unformat (input, "workers %U", unformat_bitmap_list,
918 else if (unformat (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
920 error = unformat_rss_fn(&sub_input, &devconf->rss_fn);
924 else if (unformat (input, "vlan-strip-offload off"))
925 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
926 else if (unformat (input, "vlan-strip-offload on"))
927 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
930 error = clib_error_return (0, "unknown input `%U'",
931 format_unformat_error, input);
939 if (devconf->workers && devconf->num_rx_queues == 0)
940 devconf->num_rx_queues = clib_bitmap_count_set_bits(devconf->workers);
941 else if (devconf->workers &&
942 clib_bitmap_count_set_bits(devconf->workers) != devconf->num_rx_queues)
943 error = clib_error_return (0, "%U: number of worker threadds must be "
944 "equal to number of rx queues",
945 format_vlib_pci_addr, &pci_addr);
950 static clib_error_t *
951 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
953 clib_error_t * error = 0;
954 dpdk_main_t * dm = &dpdk_main;
955 dpdk_config_main_t * conf = &dpdk_config_main;
956 vlib_thread_main_t * tm = vlib_get_thread_main();
957 dpdk_device_config_t * devconf;
958 vlib_pci_addr_t pci_addr;
959 unformat_input_t sub_input;
961 u8 * rte_cmd = 0, * ethname = 0;
964 int num_whitelisted = 0;
971 conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
973 // MATT-FIXME: inverted virtio-vhost logic to use virtio by default
974 conf->use_virtio_vhost = 1;
976 while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
979 if (unformat (input, "no-hugetlb"))
981 vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
985 else if (unformat (input, "enable-tcp-udp-checksum"))
986 conf->enable_tcp_udp_checksum = 1;
988 else if (unformat (input, "decimal-interface-names"))
989 conf->interface_name_format_decimal = 1;
991 else if (unformat (input, "no-multi-seg"))
992 conf->no_multi_seg = 1;
994 else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
997 error = dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~1, &sub_input, 1);
1002 else if (unformat (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1003 unformat_vlib_cli_sub_input, &sub_input))
1005 error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1012 else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1014 error = dpdk_device_config (conf, pci_addr, 0, 0);
1021 else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
1023 else if (unformat (input, "kni %d", &conf->num_kni))
1025 else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1027 else if (unformat (input, "socket-mem %s", &socket_mem))
1029 else if (unformat (input, "vhost-user-coalesce-frames %d", &conf->vhost_coalesce_frames))
1031 else if (unformat (input, "vhost-user-coalesce-time %f", &conf->vhost_coalesce_time))
1033 else if (unformat (input, "enable-vhost-user"))
1034 conf->use_virtio_vhost = 0;
1035 else if (unformat (input, "poll-sleep %d", &dm->poll_sleep))
1039 else if (unformat(input, #a)) \
1041 if (!strncmp(#a, "no-pci", 6)) \
1043 tmp = format (0, "--%s%c", #a, 0); \
1044 vec_add1 (conf->eal_init_args, tmp); \
1046 foreach_eal_double_hyphen_predicate_arg
1050 else if (unformat(input, #a " %s", &s)) \
1052 if (!strncmp(#a, "huge-dir", 8)) \
1054 else if (!strncmp(#a, "file-prefix", 11)) \
1056 tmp = format (0, "--%s%c", #a, 0); \
1057 vec_add1 (conf->eal_init_args, tmp); \
1059 vec_add1 (conf->eal_init_args, s); \
1061 foreach_eal_double_hyphen_arg
1065 else if (unformat(input, #a " %s", &s)) \
1067 tmp = format (0, "-%s%c", #b, 0); \
1068 vec_add1 (conf->eal_init_args, tmp); \
1070 vec_add1 (conf->eal_init_args, s); \
1072 foreach_eal_single_hyphen_arg
1076 else if (unformat(input, #a " %s", &s)) \
1078 tmp = format (0, "-%s%c", #b, 0); \
1079 vec_add1 (conf->eal_init_args, tmp); \
1081 vec_add1 (conf->eal_init_args, s); \
1082 conf->a##_set_manually = 1; \
1084 foreach_eal_single_hyphen_mandatory_arg
1087 else if (unformat(input, "default"))
1092 error = clib_error_return (0, "unknown input `%U'",
1093 format_unformat_error, input);
1098 if (!conf->uio_driver_name)
1099 conf->uio_driver_name = format (0, "igb_uio%c", 0);
1102 * Use 1G huge pages if available.
1104 if (!no_huge && !huge_dir)
1106 u32 x, * mem_by_socket = 0;
1110 u8 less_than_1g = 1;
1113 umount(DEFAULT_HUGE_DIR);
1115 /* Process "socket-mem" parameter value */
1116 if (vec_len (socket_mem))
1118 unformat_input_t in;
1119 unformat_init_vector(&in, socket_mem);
1120 while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1122 if (unformat (&in, "%u,", &x))
1124 else if (unformat (&in, "%u", &x))
1126 else if (unformat (&in, ","))
1131 vec_add1(mem_by_socket, x);
1136 /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1142 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1144 vec_validate(mem_by_socket, c);
1145 mem_by_socket[c] = 256; /* default per-socket mem */
1150 /* check if available enough 1GB pages for each socket */
1151 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1153 u32 pages_avail, page_size, mem;
1156 char * numa_path = "/sys/devices/system/node/node%u/";
1157 char * nonnuma_path = "/sys/kernel/mm/";
1158 char * suffix = "hugepages/hugepages-%ukB/free_hugepages%c";
1160 struct stat sb_numa, sb_nonnuma;
1162 p = format(p, numa_path, c);
1163 stat(numa_path, &sb_numa);
1164 stat(nonnuma_path, &sb_nonnuma);
1166 if (S_ISDIR(sb_numa.st_mode)) {
1167 path = (char*)format((u8*)path, "%s%s", p, suffix);
1168 } else if (S_ISDIR(sb_nonnuma.st_mode)) {
1169 path = (char*)format((u8*)path, "%s%s", nonnuma_path, suffix);
1177 vec_validate(mem_by_socket, c);
1178 mem = mem_by_socket[c];
1182 s = format (s, path, page_size * 1024, 0);
1183 vlib_sysfs_read ((char *) s, "%u", &pages_avail);
1184 vec_reset_length (s);
1186 if (page_size * pages_avail < mem)
1191 s = format (s, path, page_size * 1024, 0);
1192 vlib_sysfs_read ((char *) s, "%u", &pages_avail);
1193 vec_reset_length (s);
1195 if (page_size * pages_avail < mem)
1202 _vec_len (mem_by_socket) = c + 1;
1204 /* regenerate socket_mem string */
1205 vec_foreach_index (x, mem_by_socket)
1206 socket_mem = format (socket_mem, "%s%u",
1207 socket_mem ? "," : "",
1209 socket_mem = format (socket_mem, "%c", 0);
1211 vec_free (mem_by_socket);
1213 rv = mkdir(VPP_RUN_DIR, 0755);
1214 if (rv && errno != EEXIST)
1216 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1217 VPP_RUN_DIR, errno);
1221 rv = mkdir(DEFAULT_HUGE_DIR, 0755);
1222 if (rv && errno != EEXIST)
1224 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1225 DEFAULT_HUGE_DIR, errno);
1229 if (use_1g && !(less_than_1g && use_2m))
1231 rv = mount("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1235 rv = mount("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1239 return clib_error_return (0, "not enough free huge pages");
1244 error = clib_error_return (0, "mount failed %d", errno);
1248 tmp = format (0, "--huge-dir%c", 0);
1249 vec_add1 (conf->eal_init_args, tmp);
1250 tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1251 vec_add1 (conf->eal_init_args, tmp);
1254 tmp = format (0, "--file-prefix%c", 0);
1255 vec_add1 (conf->eal_init_args, tmp);
1256 tmp = format (0, "vpp%c", 0);
1257 vec_add1 (conf->eal_init_args, tmp);
1267 /* I'll bet that -c and -n must be the first and second args... */
1268 if (!conf->coremask_set_manually)
1270 vlib_thread_registration_t * tr;
1271 uword * coremask = 0;
1274 /* main thread core */
1275 coremask = clib_bitmap_set(coremask, tm->main_lcore, 1);
1277 for (i = 0; i < vec_len (tm->registrations); i++)
1279 tr = tm->registrations[i];
1280 coremask = clib_bitmap_or(coremask, tr->coremask);
1283 vec_insert (conf->eal_init_args, 2, 1);
1284 conf->eal_init_args[1] = (u8 *) "-c";
1285 tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1286 conf->eal_init_args[2] = tmp;
1287 clib_bitmap_free(coremask);
1290 if (!conf->nchannels_set_manually)
1292 vec_insert (conf->eal_init_args, 2, 3);
1293 conf->eal_init_args[3] = (u8 *) "-n";
1294 tmp = format (0, "%d", conf->nchannels);
1295 conf->eal_init_args[4] = tmp;
1298 if (no_pci == 0 && geteuid() == 0)
1299 dpdk_bind_devices_to_uio(conf);
1302 if (devconf->x == 0 && conf->default_devconf.x > 0) \
1303 devconf->x = conf->default_devconf.x ;
1305 pool_foreach (devconf, conf->dev_confs, ({
1307 /* default per-device config items */
1308 foreach_dpdk_device_config_item
1310 /* add DPDK EAL whitelist/blacklist entry */
1311 if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1313 tmp = format (0, "-w%c", 0);
1314 vec_add1 (conf->eal_init_args, tmp);
1315 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1316 vec_add1 (conf->eal_init_args, tmp);
1318 else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1320 tmp = format (0, "-b%c", 0);
1321 vec_add1 (conf->eal_init_args, tmp);
1322 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1323 vec_add1 (conf->eal_init_args, tmp);
1329 /* set master-lcore */
1330 tmp = format (0, "--master-lcore%c", 0);
1331 vec_add1 (conf->eal_init_args, tmp);
1332 tmp = format (0, "%u%c", tm->main_lcore, 0);
1333 vec_add1 (conf->eal_init_args, tmp);
1335 /* set socket-mem */
1336 tmp = format (0, "--socket-mem%c", 0);
1337 vec_add1 (conf->eal_init_args, tmp);
1338 tmp = format (0, "%s%c", socket_mem, 0);
1339 vec_add1 (conf->eal_init_args, tmp);
1341 /* NULL terminate the "argv" vector, in case of stupidity */
1342 vec_add1 (conf->eal_init_args, 0);
1343 _vec_len(conf->eal_init_args) -= 1;
1345 /* Set up DPDK eal and packet mbuf pool early. */
1347 log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1349 rte_set_log_level (log_level);
1351 vm = vlib_get_main ();
1353 /* make copy of args as rte_eal_init tends to mess up with arg array */
1354 for (i = 1; i < vec_len(conf->eal_init_args); i++)
1355 conf->eal_init_args_str = format(conf->eal_init_args_str, "%s ",
1356 conf->eal_init_args[i]);
1358 ret = rte_eal_init(vec_len(conf->eal_init_args), (char **) conf->eal_init_args);
1360 /* lazy umount hugepages */
1361 umount2(DEFAULT_HUGE_DIR, MNT_DETACH);
1364 return clib_error_return (0, "rte_eal_init returned %d", ret);
1366 /* Dump the physical memory layout prior to creating the mbuf_pool */
1367 fprintf(stdout, "DPDK physical memory layout:\n");
1368 rte_dump_physmem_layout(stdout);
1370 /* main thread 1st */
1371 error = vlib_buffer_pool_create(vm, conf->num_mbufs, rte_socket_id());
1375 for (i = 0; i < RTE_MAX_LCORE; i++)
1377 error = vlib_buffer_pool_create(vm, conf->num_mbufs,
1378 rte_lcore_to_socket_id(i));
1387 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1389 void dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1391 vnet_main_t * vnm = vnet_get_main();
1392 struct rte_eth_link prev_link = xd->link;
1394 u8 hw_flags_chg = 0;
1396 /* only update link state for PMD interfaces */
1397 if (xd->dev_type != VNET_DPDK_DEV_ETH)
1400 xd->time_last_link_update = now ? now : xd->time_last_link_update;
1401 memset(&xd->link, 0, sizeof(xd->link));
1402 rte_eth_link_get_nowait (xd->device_index, &xd->link);
1404 if (LINK_STATE_ELOGS)
1406 vlib_main_t * vm = vlib_get_main();
1407 ELOG_TYPE_DECLARE(e) = {
1409 "update-link-state: sw_if_index %d, admin_up %d,"
1410 "old link_state %d new link_state %d",
1411 .format_args = "i4i1i1i1",
1414 struct { u32 sw_if_index; u8 admin_up;
1415 u8 old_link_state; u8 new_link_state;} *ed;
1416 ed = ELOG_DATA (&vm->elog_main, e);
1417 ed->sw_if_index = xd->vlib_sw_if_index;
1418 ed->admin_up = xd->admin_up;
1419 ed->old_link_state = (u8)
1420 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index);
1421 ed->new_link_state = (u8) xd->link.link_status;
1424 if ((xd->admin_up == 1) &&
1425 ((xd->link.link_status != 0) ^
1426 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index)))
1429 hw_flags |= (xd->link.link_status ?
1430 VNET_HW_INTERFACE_FLAG_LINK_UP: 0);
1433 if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1436 switch (xd->link.link_duplex)
1438 case ETH_LINK_HALF_DUPLEX:
1439 hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1441 case ETH_LINK_FULL_DUPLEX:
1442 hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1448 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
1449 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1452 switch (xd->link.link_speed)
1454 case ETH_SPEED_NUM_10M:
1455 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1457 case ETH_SPEED_NUM_100M:
1458 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1460 case ETH_SPEED_NUM_1G:
1461 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1463 case ETH_SPEED_NUM_10G:
1464 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1466 case ETH_SPEED_NUM_40G:
1467 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1472 clib_warning("unknown link speed %d", xd->link.link_speed);
1477 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1480 switch (xd->link.link_speed)
1482 case ETH_LINK_SPEED_10:
1483 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1485 case ETH_LINK_SPEED_100:
1486 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1488 case ETH_LINK_SPEED_1000:
1489 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1491 case ETH_LINK_SPEED_10000:
1492 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1494 case ETH_LINK_SPEED_40G:
1495 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1500 clib_warning("unknown link speed %d", xd->link.link_speed);
1507 if (LINK_STATE_ELOGS)
1509 vlib_main_t * vm = vlib_get_main();
1511 ELOG_TYPE_DECLARE(e) = {
1512 .format = "update-link-state: sw_if_index %d, new flags %d",
1513 .format_args = "i4i4",
1516 struct { u32 sw_if_index; u32 flags; } *ed;
1517 ed = ELOG_DATA (&vm->elog_main, e);
1518 ed->sw_if_index = xd->vlib_sw_if_index;
1519 ed->flags = hw_flags;
1521 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, hw_flags);
1526 dpdk_process (vlib_main_t * vm,
1527 vlib_node_runtime_t * rt,
1530 clib_error_t * error;
1531 vnet_main_t * vnm = vnet_get_main();
1532 dpdk_main_t * dm = &dpdk_main;
1533 ethernet_main_t * em = ðernet_main;
1535 vlib_thread_main_t * tm = vlib_get_thread_main();
1541 error = dpdk_lib_init (dm);
1544 * Turn on the input node if we found some devices to drive
1545 * and we're not running worker threads or i/o threads
1548 if (error == 0 && vec_len(dm->devices) > 0)
1550 if (tm->n_vlib_mains == 1)
1551 vlib_node_set_state (vm, dpdk_input_node.index,
1552 VLIB_NODE_STATE_POLLING);
1554 for (i=0; i < tm->n_vlib_mains; i++)
1555 if (vec_len(dm->devices_by_cpu[i]) > 0)
1556 vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1557 VLIB_NODE_STATE_POLLING);
1561 clib_error_report (error);
1564 dpdk_vhost_user_process_init(&vu_state);
1567 tm->worker_thread_release = 1;
1569 f64 now = vlib_time_now (vm);
1570 vec_foreach (xd, dm->devices)
1572 dpdk_update_link_state (xd, now);
1576 * Extra set up for bond interfaces:
1577 * 1. Setup MACs for bond interfaces and their slave links which was set
1578 * in dpdk_port_setup() but needs to be done again here to take effect.
1579 * 2. Set up info for bond interface related CLI support.
1581 int nports = rte_eth_dev_count();
1583 for (i = 0; i < nports; i++) {
1584 struct rte_eth_dev_info dev_info;
1585 rte_eth_dev_info_get(i, &dev_info);
1586 if (!dev_info.driver_name)
1587 dev_info.driver_name = dev_info.pci_dev->driver->name;
1588 ASSERT(dev_info.driver_name);
1589 if (strncmp(dev_info.driver_name, "rte_bond_pmd", 12) == 0) {
1592 int nlink = rte_eth_bond_slaves_get(i, slink, 16);
1594 vnet_hw_interface_t * bhi;
1595 ethernet_interface_t * bei;
1596 /* Get MAC of 1st slave link */
1597 rte_eth_macaddr_get(slink[0], (struct ether_addr *)addr);
1598 /* Set MAC of bounded interface to that of 1st slave link */
1599 rte_eth_bond_mac_address_set(i, (struct ether_addr *)addr);
1600 /* Populate MAC of bonded interface in VPP hw tables */
1601 bhi = vnet_get_hw_interface(
1602 vnm, dm->devices[i].vlib_hw_if_index);
1603 bei = pool_elt_at_index(em->interfaces, bhi->hw_instance);
1604 clib_memcpy(bhi->hw_address, addr, 6);
1605 clib_memcpy(bei->address, addr, 6);
1606 /* Init l3 packet size allowed on bonded interface */
1607 bhi->max_l3_packet_bytes[VLIB_RX] =
1608 bhi->max_l3_packet_bytes[VLIB_TX] =
1609 ETHERNET_MAX_PACKET_BYTES - sizeof(ethernet_header_t);
1610 while (nlink >= 1) { /* for all slave links */
1611 int slave = slink[--nlink];
1612 dpdk_device_t * sdev = &dm->devices[slave];
1613 vnet_hw_interface_t * shi;
1614 vnet_sw_interface_t * ssi;
1615 /* Add MAC to all slave links except the first one */
1616 if (nlink) rte_eth_dev_mac_addr_add(
1617 slave, (struct ether_addr *)addr, 0);
1618 /* Set slaves bitmap for bonded interface */
1619 bhi->bond_info = clib_bitmap_set(
1620 bhi->bond_info, sdev->vlib_hw_if_index, 1);
1621 /* Set slave link flags on slave interface */
1622 shi = vnet_get_hw_interface(vnm, sdev->vlib_hw_if_index);
1623 ssi = vnet_get_sw_interface(vnm, sdev->vlib_sw_if_index);
1624 shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1625 ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1636 * check each time through the loop in case intervals are changed
1638 f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1639 dm->link_state_poll_interval : dm->stat_poll_interval;
1641 vlib_process_wait_for_event_or_clock (vm, min_wait);
1643 if (dpdk_get_admin_up_down_in_progress())
1644 /* skip the poll if an admin up down is in progress (on any interface) */
1647 vec_foreach (xd, dm->devices)
1649 f64 now = vlib_time_now (vm);
1650 if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1651 dpdk_update_counters (xd, now);
1652 if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1653 dpdk_update_link_state (xd, now);
1656 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
1657 if (dpdk_vhost_user_process_if(vm, xd, vu_state) != 0)
1664 dpdk_vhost_user_process_cleanup(vu_state);
1670 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1671 .function = dpdk_process,
1672 .type = VLIB_NODE_TYPE_PROCESS,
1673 .name = "dpdk-process",
1674 .process_log2_n_stack_bytes = 17,
1677 int dpdk_set_stat_poll_interval (f64 interval)
1679 if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
1680 return (VNET_API_ERROR_INVALID_VALUE);
1682 dpdk_main.stat_poll_interval = interval;
1687 int dpdk_set_link_state_poll_interval (f64 interval)
1689 if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
1690 return (VNET_API_ERROR_INVALID_VALUE);
1692 dpdk_main.link_state_poll_interval = interval;
1698 dpdk_init (vlib_main_t * vm)
1700 dpdk_main_t * dm = &dpdk_main;
1702 clib_error_t * error = 0;
1703 vlib_thread_main_t * tm = vlib_get_thread_main();
1705 /* verify that structs are cacheline aligned */
1706 ASSERT(offsetof(dpdk_device_t, cacheline0) == 0);
1707 ASSERT(offsetof(dpdk_device_t, cacheline1) == CLIB_CACHE_LINE_BYTES);
1708 ASSERT(offsetof(dpdk_worker_t, cacheline0) == 0);
1709 ASSERT(offsetof(frame_queue_trace_t, cacheline0) == 0);
1712 dm->vnet_main = vnet_get_main();
1713 dm->conf = &dpdk_config_main;
1715 ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1717 return clib_error_return (0, "ethernet-input node AWOL");
1719 dm->ethernet_input_node_index = ei->index;
1721 dm->conf->nchannels = 4;
1722 dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1723 vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1725 dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1726 dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1727 dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
1729 /* $$$ use n_thread_stacks since it's known-good at this point */
1730 vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1732 /* initialize EFD (early fast discard) default settings */
1733 dm->efd.enabled = DPDK_EFD_DISABLED;
1734 dm->efd.queue_hi_thresh = ((DPDK_EFD_DEFAULT_DEVICE_QUEUE_HI_THRESH_PCT *
1735 DPDK_NB_RX_DESC_10GE)/100);
1736 dm->efd.consec_full_frames_hi_thresh =
1737 DPDK_EFD_DEFAULT_CONSEC_FULL_FRAMES_HI_THRESH;
1739 /* vhost-user coalescence frames defaults */
1740 dm->conf->vhost_coalesce_frames = 32;
1741 dm->conf->vhost_coalesce_time = 1e-3;
1743 /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1744 dm->buffer_flags_template =
1745 (VLIB_BUFFER_TOTAL_LENGTH_VALID
1746 | IP_BUFFER_L4_CHECKSUM_COMPUTED
1747 | IP_BUFFER_L4_CHECKSUM_CORRECT);
1749 dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1750 dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1753 if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1759 VLIB_INIT_FUNCTION (dpdk_init);