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);
74 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
76 vnet_hw_interface_set_flags (dm->vnet_main, xd->vlib_hw_if_index, 0);
77 rte_eth_dev_stop (xd->device_index);
80 rv = rte_eth_dev_configure (xd->device_index, xd->rx_q_used,
81 xd->tx_q_used, &xd->port_conf);
84 return clib_error_return (0, "rte_eth_dev_configure[%d]: err %d",
85 xd->device_index, rv);
87 /* Set up one TX-queue per worker thread */
88 for (j = 0; j < xd->tx_q_used; j++)
90 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
91 xd->cpu_socket, &xd->tx_conf);
93 /* retry with any other CPU socket */
95 rv = rte_eth_tx_queue_setup (xd->device_index, j, xd->nb_tx_desc,
96 SOCKET_ID_ANY, &xd->tx_conf);
102 return clib_error_return (0, "rte_eth_tx_queue_setup[%d]: err %d",
103 xd->device_index, rv);
105 for (j = 0; j < xd->rx_q_used; j++)
108 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
111 pktmbuf_pools[xd->cpu_socket_id_by_queue
114 /* retry with any other CPU socket */
116 rv = rte_eth_rx_queue_setup (xd->device_index, j, xd->nb_rx_desc,
119 pktmbuf_pools[xd->cpu_socket_id_by_queue
122 return clib_error_return (0, "rte_eth_rx_queue_setup[%d]: err %d",
123 xd->device_index, rv);
126 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
129 rv = rte_eth_dev_start (xd->device_index);
131 clib_warning ("rte_eth_dev_start %d returned %d",
132 xd->device_index, rv);
138 dpdk_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags)
140 dpdk_main_t *dm = &dpdk_main;
141 dpdk_device_t *xd = vec_elt_at_index (dm->devices, hi->dev_instance);
144 if (ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC (flags))
146 old = (xd->flags & DPDK_DEVICE_FLAG_PROMISC) != 0;
148 if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
149 xd->flags |= DPDK_DEVICE_FLAG_PROMISC;
151 xd->flags &= ~DPDK_DEVICE_FLAG_PROMISC;
153 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
155 if (xd->flags & DPDK_DEVICE_FLAG_PROMISC)
156 rte_eth_promiscuous_enable (xd->device_index);
158 rte_eth_promiscuous_disable (xd->device_index);
161 else if (ETHERNET_INTERFACE_FLAG_CONFIG_MTU (flags))
164 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
165 * driver to dynamically change the mtu. If/when the
166 * VIC firmware gets fixed, then this should be removed.
168 if (xd->pmd == VNET_DPDK_PMD_ENIC)
170 struct rte_eth_dev_info dev_info;
173 * Restore mtu to what has been set by CIMC in the firmware cfg.
175 rte_eth_dev_info_get (xd->device_index, &dev_info);
176 hi->max_packet_bytes = dev_info.max_rx_pktlen;
178 vlib_cli_output (vlib_get_main (),
179 "Cisco VIC mtu can only be changed "
180 "using CIMC then rebooting the server!");
186 xd->port_conf.rxmode.max_rx_pkt_len = hi->max_packet_bytes;
188 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
189 rte_eth_dev_stop (xd->device_index);
191 rv = rte_eth_dev_configure
192 (xd->device_index, xd->rx_q_used, xd->tx_q_used, &xd->port_conf);
195 vlib_cli_output (vlib_get_main (),
196 "rte_eth_dev_configure[%d]: err %d",
197 xd->device_index, rv);
199 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
201 if (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP)
203 int rv = rte_eth_dev_start (xd->device_index);
205 clib_warning ("rte_eth_dev_start %d returned %d",
206 xd->device_index, rv);
214 dpdk_device_lock_init (dpdk_device_t * xd)
217 vec_validate (xd->lockp, xd->tx_q_used - 1);
218 for (q = 0; q < xd->tx_q_used; q++)
220 xd->lockp[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
221 CLIB_CACHE_LINE_BYTES);
222 memset ((void *) xd->lockp[q], 0, CLIB_CACHE_LINE_BYTES);
228 dpdk_device_lock_free (dpdk_device_t * xd)
232 for (q = 0; q < vec_len (xd->lockp); q++)
233 clib_mem_free ((void *) xd->lockp[q]);
234 vec_free (xd->lockp);
239 static clib_error_t *
240 dpdk_lib_init (dpdk_main_t * dm)
246 vlib_main_t *vm = vlib_get_main ();
247 vlib_thread_main_t *tm = vlib_get_thread_main ();
248 vlib_node_runtime_t *rt;
249 vnet_sw_interface_t *sw;
250 vnet_hw_interface_t *hi;
252 vlib_pci_addr_t last_pci_addr;
253 u32 last_pci_addr_port = 0;
254 vlib_thread_registration_t *tr;
258 u8 af_packet_port_id = 0;
259 last_pci_addr.as_u32 = ~0;
261 dm->input_cpu_first_index = 0;
262 dm->input_cpu_count = 1;
264 rt = vlib_node_get_runtime (vm, dpdk_input_node.index);
265 rt->function = dpdk_input_multiarch_select ();
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 nports = rte_eth_dev_count ();
286 clib_warning ("DPDK drivers found no ports...");
290 clib_warning ("DPDK drivers found %d ports...", nports);
293 * All buffers are all allocated from the same rte_mempool.
294 * Thus they all have the same number of data bytes.
296 dm->vlib_buffer_free_list_index =
297 vlib_buffer_get_or_create_free_list (vm,
298 VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES,
301 if (dm->conf->enable_tcp_udp_checksum)
302 dm->buffer_flags_template &= ~(IP_BUFFER_L4_CHECKSUM_CORRECT
303 | IP_BUFFER_L4_CHECKSUM_COMPUTED);
305 for (i = 0; i < nports; i++)
310 struct rte_eth_dev_info dev_info;
312 struct rte_eth_link l;
313 dpdk_device_config_t *devconf = 0;
314 vlib_pci_addr_t pci_addr;
317 rte_eth_dev_info_get (i, &dev_info);
318 if (dev_info.pci_dev) /* bonded interface has no pci info */
320 pci_addr.domain = dev_info.pci_dev->addr.domain;
321 pci_addr.bus = dev_info.pci_dev->addr.bus;
322 pci_addr.slot = dev_info.pci_dev->addr.devid;
323 pci_addr.function = dev_info.pci_dev->addr.function;
325 hash_get (dm->conf->device_config_index_by_pci_addr,
330 devconf = pool_elt_at_index (dm->conf->dev_confs, p[0]);
332 devconf = &dm->conf->default_devconf;
334 /* Create vnet interface */
335 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
336 xd->nb_rx_desc = DPDK_NB_RX_DESC_DEFAULT;
337 xd->nb_tx_desc = DPDK_NB_TX_DESC_DEFAULT;
338 xd->cpu_socket = (i8) rte_eth_dev_socket_id (i);
340 /* Handle interface naming for devices with multiple ports sharing same PCI ID */
341 if (dev_info.pci_dev)
343 struct rte_eth_dev_info di = { 0 };
344 rte_eth_dev_info_get (i + 1, &di);
345 if (di.pci_dev && pci_addr.as_u32 != last_pci_addr.as_u32 &&
346 memcmp (&dev_info.pci_dev->addr, &di.pci_dev->addr,
347 sizeof (struct rte_pci_addr)) == 0)
349 xd->interface_name_suffix = format (0, "0");
350 last_pci_addr.as_u32 = pci_addr.as_u32;
351 last_pci_addr_port = i;
353 else if (pci_addr.as_u32 == last_pci_addr.as_u32)
355 xd->interface_name_suffix =
356 format (0, "%u", i - last_pci_addr_port);
360 last_pci_addr.as_u32 = ~0;
364 last_pci_addr.as_u32 = ~0;
366 clib_memcpy (&xd->tx_conf, &dev_info.default_txconf,
367 sizeof (struct rte_eth_txconf));
368 if (dm->conf->no_multi_seg)
370 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
371 port_conf_template.rxmode.jumbo_frame = 0;
375 xd->tx_conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
376 port_conf_template.rxmode.jumbo_frame = 1;
379 clib_memcpy (&xd->port_conf, &port_conf_template,
380 sizeof (struct rte_eth_conf));
382 xd->tx_q_used = clib_min (dev_info.max_tx_queues, tm->n_vlib_mains);
384 if (devconf->num_tx_queues > 0
385 && devconf->num_tx_queues < xd->tx_q_used)
386 xd->tx_q_used = clib_min (xd->tx_q_used, devconf->num_tx_queues);
388 if (devconf->num_rx_queues > 1 && dm->use_rss == 0)
390 rt->function = dpdk_input_rss_multiarch_select ();
394 if (devconf->num_rx_queues > 1
395 && dev_info.max_rx_queues >= devconf->num_rx_queues)
397 xd->rx_q_used = devconf->num_rx_queues;
398 xd->port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
399 if (devconf->rss_fn == 0)
400 xd->port_conf.rx_adv_conf.rss_conf.rss_hf =
401 ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
403 xd->port_conf.rx_adv_conf.rss_conf.rss_hf = devconf->rss_fn;
408 xd->dev_type = VNET_DPDK_DEV_ETH;
410 /* workaround for drivers not setting driver_name */
411 if ((!dev_info.driver_name) && (dev_info.pci_dev))
412 dev_info.driver_name = dev_info.pci_dev->driver->name;
413 ASSERT (dev_info.driver_name);
419 #define _(s,f) else if (dev_info.driver_name && \
420 !strcmp(dev_info.driver_name, s)) \
421 xd->pmd = VNET_DPDK_PMD_##f;
427 xd->pmd = VNET_DPDK_PMD_UNKNOWN;
433 case VNET_DPDK_PMD_E1000EM:
434 case VNET_DPDK_PMD_IGB:
435 case VNET_DPDK_PMD_IGBVF:
436 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
440 case VNET_DPDK_PMD_IXGBE:
441 case VNET_DPDK_PMD_IXGBEVF:
442 case VNET_DPDK_PMD_THUNDERX:
443 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
444 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
445 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
447 case VNET_DPDK_PMD_DPAA2:
448 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
452 case VNET_DPDK_PMD_ENIC:
453 rte_eth_link_get_nowait (i, &l);
454 xd->nb_rx_desc = DPDK_NB_RX_DESC_ENIC;
455 if (l.link_speed == 40000)
457 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
458 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
462 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
463 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
467 /* Intel Fortville */
468 case VNET_DPDK_PMD_I40E:
469 case VNET_DPDK_PMD_I40EVF:
470 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
471 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
472 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
474 switch (dev_info.pci_dev->id.device_id)
476 case I40E_DEV_ID_10G_BASE_T:
477 case I40E_DEV_ID_SFP_XL710:
478 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
480 case I40E_DEV_ID_QSFP_A:
481 case I40E_DEV_ID_QSFP_B:
482 case I40E_DEV_ID_QSFP_C:
483 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
486 rte_eth_link_get_nowait (i, &l);
487 xd->port_type = l.link_speed == 10000 ?
488 VNET_DPDK_PORT_TYPE_ETH_10G : VNET_DPDK_PORT_TYPE_ETH_40G;
491 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
495 case VNET_DPDK_PMD_CXGBE:
496 switch (dev_info.pci_dev->id.device_id)
498 case 0x540d: /* T580-CR */
499 case 0x5410: /* T580-LP-cr */
500 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
501 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
502 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_40G;
504 case 0x5403: /* T540-CR */
505 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
506 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
507 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_10G;
510 xd->nb_rx_desc = DPDK_NB_RX_DESC_10GE;
511 xd->nb_tx_desc = DPDK_NB_TX_DESC_10GE;
512 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
516 /* Intel Red Rock Canyon */
517 case VNET_DPDK_PMD_FM10K:
518 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_SWITCH;
519 xd->nb_rx_desc = DPDK_NB_RX_DESC_40GE;
520 xd->nb_tx_desc = DPDK_NB_TX_DESC_40GE;
524 case VNET_DPDK_PMD_VIRTIO:
525 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
526 xd->nb_rx_desc = DPDK_NB_RX_DESC_VIRTIO;
527 xd->nb_tx_desc = DPDK_NB_TX_DESC_VIRTIO;
531 case VNET_DPDK_PMD_VMXNET3:
532 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_1G;
533 xd->tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOMULTSEGS;
536 case VNET_DPDK_PMD_AF_PACKET:
537 xd->port_type = VNET_DPDK_PORT_TYPE_AF_PACKET;
538 xd->af_packet_port_id = af_packet_port_id++;
541 case VNET_DPDK_PMD_BOND:
542 xd->port_type = VNET_DPDK_PORT_TYPE_ETH_BOND;
546 xd->port_type = VNET_DPDK_PORT_TYPE_UNKNOWN;
549 if (devconf->num_rx_desc)
550 xd->nb_rx_desc = devconf->num_rx_desc;
552 if (devconf->num_tx_desc)
553 xd->nb_tx_desc = devconf->num_tx_desc;
557 * Ensure default mtu is not > the mtu read from the hardware.
558 * Otherwise rte_eth_dev_configure() will fail and the port will
561 if (ETHERNET_MAX_PACKET_BYTES > dev_info.max_rx_pktlen)
564 * This device does not support the platforms's max frame
565 * size. Use it's advertised mru instead.
567 xd->port_conf.rxmode.max_rx_pkt_len = dev_info.max_rx_pktlen;
571 xd->port_conf.rxmode.max_rx_pkt_len = ETHERNET_MAX_PACKET_BYTES;
574 * Some platforms do not account for Ethernet FCS (4 bytes) in
575 * MTU calculations. To interop with them increase mru but only
576 * if the device's settings can support it.
578 if ((dev_info.max_rx_pktlen >= (ETHERNET_MAX_PACKET_BYTES + 4)) &&
579 xd->port_conf.rxmode.hw_strip_crc)
582 * Allow additional 4 bytes (for Ethernet FCS). These bytes are
583 * stripped by h/w and so will not consume any buffer memory.
585 xd->port_conf.rxmode.max_rx_pkt_len += 4;
589 #if RTE_VERSION < RTE_VERSION_NUM(16, 4, 0, 0)
591 * Older VMXNET3 driver doesn't support jumbo / multi-buffer pkts
593 if (xd->pmd == VNET_DPDK_PMD_VMXNET3)
595 xd->port_conf.rxmode.max_rx_pkt_len = 1518;
596 xd->port_conf.rxmode.jumbo_frame = 0;
600 if (xd->pmd == VNET_DPDK_PMD_AF_PACKET)
602 f64 now = vlib_time_now (vm);
604 rnd = (u32) (now * 1e6);
605 rnd = random_u32 (&rnd);
606 clib_memcpy (addr + 2, &rnd, sizeof (rnd));
611 rte_eth_macaddr_get (i, (struct ether_addr *) addr);
613 if (xd->tx_q_used < tm->n_vlib_mains)
614 dpdk_device_lock_init (xd);
616 xd->device_index = xd - dm->devices;
617 ASSERT (i == xd->device_index);
618 xd->per_interface_next_index = ~0;
620 /* assign interface to input thread */
621 dpdk_device_and_queue_t *dq;
624 if (devconf->workers)
629 clib_bitmap_foreach (i, devconf->workers, ({
630 int cpu = dm->input_cpu_first_index + i;
631 unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
632 vec_validate(xd->cpu_socket_id_by_queue, q);
633 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
634 vec_add2(dm->devices_by_cpu[cpu], dq, 1);
635 dq->device = xd->device_index;
641 for (q = 0; q < xd->rx_q_used; q++)
643 int cpu = dm->input_cpu_first_index + next_cpu;
644 unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
647 * numa node for worker thread handling this queue
648 * needed for taking buffers from the right mempool
650 vec_validate (xd->cpu_socket_id_by_queue, q);
651 xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id (lcore);
654 * construct vector of (device,queue) pairs for each worker thread
656 vec_add2 (dm->devices_by_cpu[cpu], dq, 1);
657 dq->device = xd->device_index;
661 if (next_cpu == dm->input_cpu_count)
665 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
666 CLIB_CACHE_LINE_BYTES);
667 for (j = 0; j < tm->n_vlib_mains; j++)
669 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
670 sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
671 vec_reset_length (xd->tx_vectors[j]);
674 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
675 CLIB_CACHE_LINE_BYTES);
676 for (j = 0; j < xd->rx_q_used; j++)
678 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
679 CLIB_CACHE_LINE_BYTES);
680 vec_reset_length (xd->rx_vectors[j]);
683 rv = dpdk_port_setup (dm, xd);
688 /* count the number of descriptors used for this device */
689 nb_desc += xd->nb_rx_desc + xd->nb_tx_desc * xd->tx_q_used;
691 error = ethernet_register_interface
692 (dm->vnet_main, dpdk_device_class.index, xd->device_index,
693 /* ethernet address */ addr,
694 &xd->vlib_hw_if_index, dpdk_flag_change);
698 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
699 xd->vlib_sw_if_index = sw->sw_if_index;
700 hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
703 * DAW-FIXME: The Cisco VIC firmware does not provide an api for a
704 * driver to dynamically change the mtu. If/when the
705 * VIC firmware gets fixed, then this should be removed.
707 if (xd->pmd == VNET_DPDK_PMD_ENIC)
710 * Initialize mtu to what has been set by CIMC in the firmware cfg.
712 hi->max_packet_bytes = dev_info.max_rx_pktlen;
713 if (devconf->vlan_strip_offload != DPDK_DEVICE_VLAN_STRIP_OFF)
714 vlan_strip = 1; /* remove vlan tag from VIC port by default */
716 clib_warning ("VLAN strip disabled for interface\n");
718 else if (devconf->vlan_strip_offload == DPDK_DEVICE_VLAN_STRIP_ON)
724 vlan_off = rte_eth_dev_get_vlan_offload (xd->device_index);
725 vlan_off |= ETH_VLAN_STRIP_OFFLOAD;
726 if (rte_eth_dev_set_vlan_offload (xd->device_index, vlan_off) == 0)
727 clib_warning ("VLAN strip enabled for interface\n");
729 clib_warning ("VLAN strip cannot be supported by interface\n");
732 #if RTE_VERSION < RTE_VERSION_NUM(16, 4, 0, 0)
734 * Older VMXNET3 driver doesn't support jumbo / multi-buffer pkts
736 else if (xd->pmd == VNET_DPDK_PMD_VMXNET3)
737 hi->max_packet_bytes = 1518;
740 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] =
741 xd->port_conf.rxmode.max_rx_pkt_len - sizeof (ethernet_header_t);
743 rte_eth_dev_set_mtu (xd->device_index, hi->max_packet_bytes);
746 #ifdef RTE_LIBRTE_KNI
747 if (dm->conf->num_kni)
749 clib_warning ("Initializing KNI interfaces...");
750 rte_kni_init (dm->conf->num_kni);
751 for (i = 0; i < dm->conf->num_kni; i++)
756 /* Create vnet interface */
757 vec_add2_aligned (dm->devices, xd, 1, CLIB_CACHE_LINE_BYTES);
758 xd->dev_type = VNET_DPDK_DEV_KNI;
760 xd->device_index = xd - dm->devices;
761 ASSERT (nports + i == xd->device_index);
762 xd->per_interface_next_index = ~0;
765 hash_set (dm->dpdk_device_by_kni_port_id, i, xd - dm->devices);
768 /* assign interface to input thread */
769 dpdk_device_and_queue_t *dq;
770 vec_add2 (dm->devices_by_cpu[dm->input_cpu_first_index], dq, 1);
771 dq->device = xd->device_index;
774 vec_validate_aligned (xd->tx_vectors, tm->n_vlib_mains,
775 CLIB_CACHE_LINE_BYTES);
776 for (j = 0; j < tm->n_vlib_mains; j++)
778 vec_validate_ha (xd->tx_vectors[j], DPDK_TX_RING_SIZE,
779 sizeof (tx_ring_hdr_t), CLIB_CACHE_LINE_BYTES);
780 vec_reset_length (xd->tx_vectors[j]);
783 vec_validate_aligned (xd->rx_vectors, xd->rx_q_used,
784 CLIB_CACHE_LINE_BYTES);
785 for (j = 0; j < xd->rx_q_used; j++)
787 vec_validate_aligned (xd->rx_vectors[j], VLIB_FRAME_SIZE - 1,
788 CLIB_CACHE_LINE_BYTES);
789 vec_reset_length (xd->rx_vectors[j]);
792 /* FIXME Set up one TX-queue per worker thread */
795 f64 now = vlib_time_now (vm);
797 rnd = (u32) (now * 1e6);
798 rnd = random_u32 (&rnd);
800 clib_memcpy (addr + 2, &rnd, sizeof (rnd));
805 error = ethernet_register_interface
806 (dm->vnet_main, dpdk_device_class.index, xd->device_index,
807 /* ethernet address */ addr,
808 &xd->vlib_hw_if_index, dpdk_flag_change);
813 sw = vnet_get_hw_sw_interface (dm->vnet_main, xd->vlib_hw_if_index);
814 xd->vlib_sw_if_index = sw->sw_if_index;
815 hi = vnet_get_hw_interface (dm->vnet_main, xd->vlib_hw_if_index);
820 if (nb_desc > dm->conf->num_mbufs)
821 clib_warning ("%d mbufs allocated but total rx/tx ring size is %d\n",
822 dm->conf->num_mbufs, nb_desc);
824 /* init next vhost-user if index */
825 dm->next_vu_if_id = 0;
831 dpdk_bind_devices_to_uio (dpdk_config_main_t * conf)
833 vlib_pci_main_t *pm = &pci_main;
835 vlib_pci_device_t *d;
836 pci_config_header_t *c;
838 int num_whitelisted = vec_len (conf->dev_confs);
841 pool_foreach (d, pm->pci_devs, ({
842 dpdk_device_config_t * devconf = 0;
843 c = &d->config0.header;
844 vec_reset_length (pci_addr);
845 pci_addr = format (pci_addr, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
847 if (c->device_class != PCI_CLASS_NETWORK_ETHERNET)
852 uword * p = hash_get (conf->device_config_index_by_pci_addr, d->bus_address.as_u32);
857 devconf = pool_elt_at_index (conf->dev_confs, p[0]);
861 if (c->vendor_id == 0x1af4 && c->device_id == 0x1000)
864 else if (c->vendor_id == 0x15ad && c->device_id == 0x07b0)
866 /* all Intel devices */
867 else if (c->vendor_id == 0x8086)
870 else if (c->vendor_id == 0x1137 && c->device_id == 0x0043)
873 else if (c->vendor_id == 0x1425 && (c->device_id & 0xe000) == 0x4000)
877 clib_warning ("Unsupported Ethernet PCI device 0x%04x:0x%04x found "
878 "at PCI address %s\n", (u16) c->vendor_id, (u16) c->device_id,
883 error = vlib_pci_bind_to_uio (d, (char *) conf->uio_driver_name);
889 pool_get (conf->dev_confs, devconf);
890 hash_set (conf->device_config_index_by_pci_addr, d->bus_address.as_u32,
891 devconf - conf->dev_confs);
892 devconf->pci_addr.as_u32 = d->bus_address.as_u32;
894 devconf->is_blacklisted = 1;
895 clib_error_report (error);
902 static clib_error_t *
903 dpdk_device_config (dpdk_config_main_t * conf, vlib_pci_addr_t pci_addr,
904 unformat_input_t * input, u8 is_default)
906 clib_error_t *error = 0;
908 dpdk_device_config_t *devconf;
909 unformat_input_t sub_input;
913 devconf = &conf->default_devconf;
917 p = hash_get (conf->device_config_index_by_pci_addr, pci_addr.as_u32);
921 pool_get (conf->dev_confs, devconf);
922 hash_set (conf->device_config_index_by_pci_addr, pci_addr.as_u32,
923 devconf - conf->dev_confs);
926 return clib_error_return (0,
927 "duplicate configuration for PCI address %U",
928 format_vlib_pci_addr, &pci_addr);
931 devconf->pci_addr.as_u32 = pci_addr.as_u32;
936 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
938 if (unformat (input, "num-rx-queues %u", &devconf->num_rx_queues))
940 else if (unformat (input, "num-tx-queues %u", &devconf->num_tx_queues))
942 else if (unformat (input, "num-rx-desc %u", &devconf->num_rx_desc))
944 else if (unformat (input, "num-tx-desc %u", &devconf->num_tx_desc))
946 else if (unformat (input, "workers %U", unformat_bitmap_list,
951 (input, "rss %U", unformat_vlib_cli_sub_input, &sub_input))
953 error = unformat_rss_fn (&sub_input, &devconf->rss_fn);
957 else if (unformat (input, "vlan-strip-offload off"))
958 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_OFF;
959 else if (unformat (input, "vlan-strip-offload on"))
960 devconf->vlan_strip_offload = DPDK_DEVICE_VLAN_STRIP_ON;
963 error = clib_error_return (0, "unknown input `%U'",
964 format_unformat_error, input);
972 if (devconf->workers && devconf->num_rx_queues == 0)
973 devconf->num_rx_queues = clib_bitmap_count_set_bits (devconf->workers);
974 else if (devconf->workers &&
975 clib_bitmap_count_set_bits (devconf->workers) !=
976 devconf->num_rx_queues)
978 clib_error_return (0,
979 "%U: number of worker threadds must be "
980 "equal to number of rx queues", format_vlib_pci_addr,
986 static clib_error_t *
987 dpdk_config (vlib_main_t * vm, unformat_input_t * input)
989 clib_error_t *error = 0;
990 dpdk_main_t *dm = &dpdk_main;
991 dpdk_config_main_t *conf = &dpdk_config_main;
992 vlib_thread_main_t *tm = vlib_get_thread_main ();
993 dpdk_device_config_t *devconf;
994 vlib_pci_addr_t pci_addr;
995 unformat_input_t sub_input;
997 u8 *rte_cmd = 0, *ethname = 0;
1000 int num_whitelisted = 0;
1007 conf->device_config_index_by_pci_addr = hash_create (0, sizeof (uword));
1009 // MATT-FIXME: inverted virtio-vhost logic to use virtio by default
1010 conf->use_virtio_vhost = 1;
1012 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1014 /* Prime the pump */
1015 if (unformat (input, "no-hugetlb"))
1017 vec_add1 (conf->eal_init_args, (u8 *) "no-huge");
1021 else if (unformat (input, "enable-tcp-udp-checksum"))
1022 conf->enable_tcp_udp_checksum = 1;
1024 else if (unformat (input, "decimal-interface-names"))
1025 conf->interface_name_format_decimal = 1;
1027 else if (unformat (input, "no-multi-seg"))
1028 conf->no_multi_seg = 1;
1030 else if (unformat (input, "dev default %U", unformat_vlib_cli_sub_input,
1034 dpdk_device_config (conf, (vlib_pci_addr_t) (u32) ~ 1, &sub_input,
1042 (input, "dev %U %U", unformat_vlib_pci_addr, &pci_addr,
1043 unformat_vlib_cli_sub_input, &sub_input))
1045 error = dpdk_device_config (conf, pci_addr, &sub_input, 0);
1052 else if (unformat (input, "dev %U", unformat_vlib_pci_addr, &pci_addr))
1054 error = dpdk_device_config (conf, pci_addr, 0, 0);
1061 else if (unformat (input, "num-mbufs %d", &conf->num_mbufs))
1063 else if (unformat (input, "kni %d", &conf->num_kni))
1065 else if (unformat (input, "uio-driver %s", &conf->uio_driver_name))
1067 else if (unformat (input, "socket-mem %s", &socket_mem))
1071 (input, "vhost-user-coalesce-frames %d",
1072 &conf->vhost_coalesce_frames))
1076 (input, "vhost-user-coalesce-time %f",
1077 &conf->vhost_coalesce_time))
1079 else if (unformat (input, "enable-vhost-user"))
1080 conf->use_virtio_vhost = 0;
1081 else if (unformat (input, "no-pci"))
1084 tmp = format (0, "--no-pci%c", 0);
1085 vec_add1 (conf->eal_init_args, tmp);
1087 else if (unformat (input, "poll-sleep %d", &dm->poll_sleep))
1091 else if (unformat(input, #a)) \
1093 tmp = format (0, "--%s%c", #a, 0); \
1094 vec_add1 (conf->eal_init_args, tmp); \
1096 foreach_eal_double_hyphen_predicate_arg
1099 else if (unformat(input, #a " %s", &s)) \
1101 if (!strncmp(#a, "huge-dir", 8)) \
1103 else if (!strncmp(#a, "file-prefix", 11)) \
1105 tmp = format (0, "--%s%c", #a, 0); \
1106 vec_add1 (conf->eal_init_args, tmp); \
1108 vec_add1 (conf->eal_init_args, s); \
1110 foreach_eal_double_hyphen_arg
1113 else if (unformat(input, #a " %s", &s)) \
1115 tmp = format (0, "-%s%c", #b, 0); \
1116 vec_add1 (conf->eal_init_args, tmp); \
1118 vec_add1 (conf->eal_init_args, s); \
1120 foreach_eal_single_hyphen_arg
1123 else if (unformat(input, #a " %s", &s)) \
1125 tmp = format (0, "-%s%c", #b, 0); \
1126 vec_add1 (conf->eal_init_args, tmp); \
1128 vec_add1 (conf->eal_init_args, s); \
1129 conf->a##_set_manually = 1; \
1131 foreach_eal_single_hyphen_mandatory_arg
1133 else if (unformat (input, "default"))
1138 error = clib_error_return (0, "unknown input `%U'",
1139 format_unformat_error, input);
1144 if (!conf->uio_driver_name)
1145 conf->uio_driver_name = format (0, "igb_uio%c", 0);
1148 * Use 1G huge pages if available.
1150 if (!no_huge && !huge_dir)
1152 u32 x, *mem_by_socket = 0;
1156 u8 less_than_1g = 1;
1159 umount (DEFAULT_HUGE_DIR);
1161 /* Process "socket-mem" parameter value */
1162 if (vec_len (socket_mem))
1164 unformat_input_t in;
1165 unformat_init_vector (&in, socket_mem);
1166 while (unformat_check_input (&in) != UNFORMAT_END_OF_INPUT)
1168 if (unformat (&in, "%u,", &x))
1170 else if (unformat (&in, "%u", &x))
1172 else if (unformat (&in, ","))
1177 vec_add1 (mem_by_socket, x);
1182 /* Note: unformat_free vec_frees(in.buffer), aka socket_mem... */
1183 unformat_free (&in);
1189 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1191 vec_validate(mem_by_socket, c);
1192 mem_by_socket[c] = 256; /* default per-socket mem */
1198 /* check if available enough 1GB pages for each socket */
1200 clib_bitmap_foreach (c, tm->cpu_socket_bitmap, (
1202 u32 pages_avail, page_size, mem;
1205 char * numa_path = "/sys/devices/system/node/node%u/";
1206 char * nonnuma_path = "/sys/kernel/mm/";
1207 char * suffix = "hugepages/hugepages-%ukB/free_hugepages%c";
1209 struct stat sb_numa, sb_nonnuma;
1211 p = format(p, numa_path, c);
1212 if (stat(numa_path, &sb_numa) < 0)
1213 sb_numa.st_mode = 0;
1215 if (stat(nonnuma_path, &sb_nonnuma) < 0)
1216 sb_nonnuma.st_mode = 0;
1218 if (S_ISDIR(sb_numa.st_mode)) {
1219 path = (char*)format((u8*)path, "%s%s", p, suffix);
1220 } else if (S_ISDIR(sb_nonnuma.st_mode)) {
1221 path = (char*)format((u8*)path, "%s%s", nonnuma_path, suffix);
1229 vec_validate(mem_by_socket, c);
1230 mem = mem_by_socket[c];
1234 s = format (s, path, page_size * 1024, 0);
1235 vlib_sysfs_read ((char *) s, "%u", &pages_avail);
1236 vec_reset_length (s);
1238 if (page_size * pages_avail < mem)
1243 s = format (s, path, page_size * 1024, 0);
1244 vlib_sysfs_read ((char *) s, "%u", &pages_avail);
1245 vec_reset_length (s);
1247 if (page_size * pages_avail < mem)
1256 if (mem_by_socket == 0)
1258 error = clib_error_return (0, "mem_by_socket NULL");
1261 _vec_len (mem_by_socket) = c + 1;
1263 /* regenerate socket_mem string */
1264 vec_foreach_index (x, mem_by_socket)
1265 socket_mem = format (socket_mem, "%s%u",
1266 socket_mem ? "," : "", mem_by_socket[x]);
1267 socket_mem = format (socket_mem, "%c", 0);
1269 vec_free (mem_by_socket);
1271 rv = mkdir (VPP_RUN_DIR, 0755);
1272 if (rv && errno != EEXIST)
1274 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1275 VPP_RUN_DIR, errno);
1279 rv = mkdir (DEFAULT_HUGE_DIR, 0755);
1280 if (rv && errno != EEXIST)
1282 error = clib_error_return (0, "mkdir '%s' failed errno %d",
1283 DEFAULT_HUGE_DIR, errno);
1287 if (use_1g && !(less_than_1g && use_2m))
1290 mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, "pagesize=1G");
1294 rv = mount ("none", DEFAULT_HUGE_DIR, "hugetlbfs", 0, NULL);
1298 return clib_error_return (0, "not enough free huge pages");
1303 error = clib_error_return (0, "mount failed %d", errno);
1307 tmp = format (0, "--huge-dir%c", 0);
1308 vec_add1 (conf->eal_init_args, tmp);
1309 tmp = format (0, "%s%c", DEFAULT_HUGE_DIR, 0);
1310 vec_add1 (conf->eal_init_args, tmp);
1313 tmp = format (0, "--file-prefix%c", 0);
1314 vec_add1 (conf->eal_init_args, tmp);
1315 tmp = format (0, "vpp%c", 0);
1316 vec_add1 (conf->eal_init_args, tmp);
1326 /* I'll bet that -c and -n must be the first and second args... */
1327 if (!conf->coremask_set_manually)
1329 vlib_thread_registration_t *tr;
1330 uword *coremask = 0;
1333 /* main thread core */
1334 coremask = clib_bitmap_set (coremask, tm->main_lcore, 1);
1336 for (i = 0; i < vec_len (tm->registrations); i++)
1338 tr = tm->registrations[i];
1339 coremask = clib_bitmap_or (coremask, tr->coremask);
1342 vec_insert (conf->eal_init_args, 2, 1);
1343 conf->eal_init_args[1] = (u8 *) "-c";
1344 tmp = format (0, "%U%c", format_bitmap_hex, coremask, 0);
1345 conf->eal_init_args[2] = tmp;
1346 clib_bitmap_free (coremask);
1349 if (!conf->nchannels_set_manually)
1351 vec_insert (conf->eal_init_args, 2, 3);
1352 conf->eal_init_args[3] = (u8 *) "-n";
1353 tmp = format (0, "%d", conf->nchannels);
1354 conf->eal_init_args[4] = tmp;
1357 if (no_pci == 0 && geteuid () == 0)
1358 dpdk_bind_devices_to_uio (conf);
1361 if (devconf->x == 0 && conf->default_devconf.x > 0) \
1362 devconf->x = conf->default_devconf.x ;
1365 pool_foreach (devconf, conf->dev_confs, ({
1367 /* default per-device config items */
1368 foreach_dpdk_device_config_item
1370 /* add DPDK EAL whitelist/blacklist entry */
1371 if (num_whitelisted > 0 && devconf->is_blacklisted == 0)
1373 tmp = format (0, "-w%c", 0);
1374 vec_add1 (conf->eal_init_args, tmp);
1375 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1376 vec_add1 (conf->eal_init_args, tmp);
1378 else if (num_whitelisted == 0 && devconf->is_blacklisted != 0)
1380 tmp = format (0, "-b%c", 0);
1381 vec_add1 (conf->eal_init_args, tmp);
1382 tmp = format (0, "%U%c", format_vlib_pci_addr, &devconf->pci_addr, 0);
1383 vec_add1 (conf->eal_init_args, tmp);
1390 /* set master-lcore */
1391 tmp = format (0, "--master-lcore%c", 0);
1392 vec_add1 (conf->eal_init_args, tmp);
1393 tmp = format (0, "%u%c", tm->main_lcore, 0);
1394 vec_add1 (conf->eal_init_args, tmp);
1396 /* set socket-mem */
1397 tmp = format (0, "--socket-mem%c", 0);
1398 vec_add1 (conf->eal_init_args, tmp);
1399 tmp = format (0, "%s%c", socket_mem, 0);
1400 vec_add1 (conf->eal_init_args, tmp);
1402 /* NULL terminate the "argv" vector, in case of stupidity */
1403 vec_add1 (conf->eal_init_args, 0);
1404 _vec_len (conf->eal_init_args) -= 1;
1406 /* Set up DPDK eal and packet mbuf pool early. */
1408 log_level = (CLIB_DEBUG > 0) ? RTE_LOG_DEBUG : RTE_LOG_NOTICE;
1410 rte_set_log_level (log_level);
1412 vm = vlib_get_main ();
1414 /* make copy of args as rte_eal_init tends to mess up with arg array */
1415 for (i = 1; i < vec_len (conf->eal_init_args); i++)
1416 conf->eal_init_args_str = format (conf->eal_init_args_str, "%s ",
1417 conf->eal_init_args[i]);
1420 rte_eal_init (vec_len (conf->eal_init_args),
1421 (char **) conf->eal_init_args);
1423 /* lazy umount hugepages */
1424 umount2 (DEFAULT_HUGE_DIR, MNT_DETACH);
1427 return clib_error_return (0, "rte_eal_init returned %d", ret);
1429 /* Dump the physical memory layout prior to creating the mbuf_pool */
1430 fprintf (stdout, "DPDK physical memory layout:\n");
1431 rte_dump_physmem_layout (stdout);
1433 /* main thread 1st */
1434 error = vlib_buffer_pool_create (vm, conf->num_mbufs, rte_socket_id ());
1438 for (i = 0; i < RTE_MAX_LCORE; i++)
1440 error = vlib_buffer_pool_create (vm, conf->num_mbufs,
1441 rte_lcore_to_socket_id (i));
1450 VLIB_CONFIG_FUNCTION (dpdk_config, "dpdk");
1453 dpdk_update_link_state (dpdk_device_t * xd, f64 now)
1455 vnet_main_t *vnm = vnet_get_main ();
1456 struct rte_eth_link prev_link = xd->link;
1458 u8 hw_flags_chg = 0;
1460 /* only update link state for PMD interfaces */
1461 if (xd->dev_type != VNET_DPDK_DEV_ETH)
1464 xd->time_last_link_update = now ? now : xd->time_last_link_update;
1465 memset (&xd->link, 0, sizeof (xd->link));
1466 rte_eth_link_get_nowait (xd->device_index, &xd->link);
1468 if (LINK_STATE_ELOGS)
1470 vlib_main_t *vm = vlib_get_main ();
1471 ELOG_TYPE_DECLARE (e) =
1474 "update-link-state: sw_if_index %d, admin_up %d,"
1475 "old link_state %d new link_state %d",.format_args = "i4i1i1i1",};
1484 ed = ELOG_DATA (&vm->elog_main, e);
1485 ed->sw_if_index = xd->vlib_sw_if_index;
1486 ed->admin_up = (xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) != 0;
1487 ed->old_link_state = (u8)
1488 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index);
1489 ed->new_link_state = (u8) xd->link.link_status;
1492 if ((xd->flags & DPDK_DEVICE_FLAG_ADMIN_UP) &&
1493 ((xd->link.link_status != 0) ^
1494 vnet_hw_interface_is_link_up (vnm, xd->vlib_hw_if_index)))
1497 hw_flags |= (xd->link.link_status ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
1500 if (hw_flags_chg || (xd->link.link_duplex != prev_link.link_duplex))
1503 switch (xd->link.link_duplex)
1505 case ETH_LINK_HALF_DUPLEX:
1506 hw_flags |= VNET_HW_INTERFACE_FLAG_HALF_DUPLEX;
1508 case ETH_LINK_FULL_DUPLEX:
1509 hw_flags |= VNET_HW_INTERFACE_FLAG_FULL_DUPLEX;
1515 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
1516 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1519 switch (xd->link.link_speed)
1521 case ETH_SPEED_NUM_10M:
1522 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1524 case ETH_SPEED_NUM_100M:
1525 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1527 case ETH_SPEED_NUM_1G:
1528 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1530 case ETH_SPEED_NUM_10G:
1531 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1533 case ETH_SPEED_NUM_40G:
1534 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1539 clib_warning ("unknown link speed %d", xd->link.link_speed);
1544 if (hw_flags_chg || (xd->link.link_speed != prev_link.link_speed))
1547 switch (xd->link.link_speed)
1549 case ETH_LINK_SPEED_10:
1550 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10M;
1552 case ETH_LINK_SPEED_100:
1553 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_100M;
1555 case ETH_LINK_SPEED_1000:
1556 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_1G;
1558 case ETH_LINK_SPEED_10000:
1559 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_10G;
1561 case ETH_LINK_SPEED_40G:
1562 hw_flags |= VNET_HW_INTERFACE_FLAG_SPEED_40G;
1567 clib_warning ("unknown link speed %d", xd->link.link_speed);
1574 if (LINK_STATE_ELOGS)
1576 vlib_main_t *vm = vlib_get_main ();
1578 ELOG_TYPE_DECLARE (e) =
1581 "update-link-state: sw_if_index %d, new flags %d",.format_args
1589 ed = ELOG_DATA (&vm->elog_main, e);
1590 ed->sw_if_index = xd->vlib_sw_if_index;
1591 ed->flags = hw_flags;
1593 vnet_hw_interface_set_flags (vnm, xd->vlib_hw_if_index, hw_flags);
1598 dpdk_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1600 clib_error_t *error;
1601 vnet_main_t *vnm = vnet_get_main ();
1602 dpdk_main_t *dm = &dpdk_main;
1603 ethernet_main_t *em = ðernet_main;
1605 vlib_thread_main_t *tm = vlib_get_thread_main ();
1611 error = dpdk_lib_init (dm);
1614 * Turn on the input node if we found some devices to drive
1615 * and we're not running worker threads or i/o threads
1618 if (error == 0 && vec_len (dm->devices) > 0)
1620 if (tm->n_vlib_mains == 1)
1621 vlib_node_set_state (vm, dpdk_input_node.index,
1622 VLIB_NODE_STATE_POLLING);
1624 for (i = 0; i < tm->n_vlib_mains; i++)
1625 if (vec_len (dm->devices_by_cpu[i]) > 0)
1626 vlib_node_set_state (vlib_mains[i], dpdk_input_node.index,
1627 VLIB_NODE_STATE_POLLING);
1631 clib_error_report (error);
1634 dpdk_vhost_user_process_init (&vu_state);
1637 tm->worker_thread_release = 1;
1639 f64 now = vlib_time_now (vm);
1640 vec_foreach (xd, dm->devices)
1642 dpdk_update_link_state (xd, now);
1647 * Extra set up for bond interfaces:
1648 * 1. Setup MACs for bond interfaces and their slave links which was set
1649 * in dpdk_port_setup() but needs to be done again here to take effect.
1650 * 2. Set up info for bond interface related CLI support.
1652 int nports = rte_eth_dev_count ();
1655 for (i = 0; i < nports; i++)
1657 struct rte_eth_dev_info dev_info;
1658 rte_eth_dev_info_get (i, &dev_info);
1659 if (!dev_info.driver_name)
1660 dev_info.driver_name = dev_info.pci_dev->driver->name;
1661 ASSERT (dev_info.driver_name);
1662 if (strncmp (dev_info.driver_name, "rte_bond_pmd", 12) == 0)
1666 int nlink = rte_eth_bond_slaves_get (i, slink, 16);
1669 vnet_hw_interface_t *bhi;
1670 ethernet_interface_t *bei;
1673 /* Get MAC of 1st slave link */
1674 rte_eth_macaddr_get (slink[0],
1675 (struct ether_addr *) addr);
1676 /* Set MAC of bounded interface to that of 1st slave link */
1678 rte_eth_bond_mac_address_set (i,
1679 (struct ether_addr *)
1682 clib_warning ("Failed to set MAC address");
1684 /* Populate MAC of bonded interface in VPP hw tables */
1686 vnet_get_hw_interface (vnm,
1687 dm->devices[i].vlib_hw_if_index);
1689 pool_elt_at_index (em->interfaces, bhi->hw_instance);
1690 clib_memcpy (bhi->hw_address, addr, 6);
1691 clib_memcpy (bei->address, addr, 6);
1692 /* Init l3 packet size allowed on bonded interface */
1693 bhi->max_l3_packet_bytes[VLIB_RX] =
1694 bhi->max_l3_packet_bytes[VLIB_TX] =
1695 ETHERNET_MAX_PACKET_BYTES - sizeof (ethernet_header_t);
1697 { /* for all slave links */
1698 int slave = slink[--nlink];
1699 dpdk_device_t *sdev = &dm->devices[slave];
1700 vnet_hw_interface_t *shi;
1701 vnet_sw_interface_t *ssi;
1702 /* Add MAC to all slave links except the first one */
1704 rte_eth_dev_mac_addr_add (slave,
1705 (struct ether_addr *)
1707 /* Set slaves bitmap for bonded interface */
1709 clib_bitmap_set (bhi->bond_info,
1710 sdev->vlib_hw_if_index, 1);
1711 /* Set slave link flags on slave interface */
1713 vnet_get_hw_interface (vnm, sdev->vlib_hw_if_index);
1715 vnet_get_sw_interface (vnm, sdev->vlib_sw_if_index);
1716 shi->bond_info = VNET_HW_INTERFACE_BOND_INFO_SLAVE;
1717 ssi->flags |= VNET_SW_INTERFACE_FLAG_BOND_SLAVE;
1728 * check each time through the loop in case intervals are changed
1730 f64 min_wait = dm->link_state_poll_interval < dm->stat_poll_interval ?
1731 dm->link_state_poll_interval : dm->stat_poll_interval;
1733 vlib_process_wait_for_event_or_clock (vm, min_wait);
1735 if (dpdk_get_admin_up_down_in_progress ())
1736 /* skip the poll if an admin up down is in progress (on any interface) */
1739 vec_foreach (xd, dm->devices)
1741 f64 now = vlib_time_now (vm);
1742 if ((now - xd->time_last_stats_update) >= dm->stat_poll_interval)
1743 dpdk_update_counters (xd, now);
1744 if ((now - xd->time_last_link_update) >= dm->link_state_poll_interval)
1745 dpdk_update_link_state (xd, now);
1748 if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER)
1749 if (dpdk_vhost_user_process_if (vm, xd, vu_state) != 0)
1756 dpdk_vhost_user_process_cleanup (vu_state);
1763 VLIB_REGISTER_NODE (dpdk_process_node,static) = {
1764 .function = dpdk_process,
1765 .type = VLIB_NODE_TYPE_PROCESS,
1766 .name = "dpdk-process",
1767 .process_log2_n_stack_bytes = 17,
1772 dpdk_set_stat_poll_interval (f64 interval)
1774 if (interval < DPDK_MIN_STATS_POLL_INTERVAL)
1775 return (VNET_API_ERROR_INVALID_VALUE);
1777 dpdk_main.stat_poll_interval = interval;
1783 dpdk_set_link_state_poll_interval (f64 interval)
1785 if (interval < DPDK_MIN_LINK_POLL_INTERVAL)
1786 return (VNET_API_ERROR_INVALID_VALUE);
1788 dpdk_main.link_state_poll_interval = interval;
1794 dpdk_init (vlib_main_t * vm)
1796 dpdk_main_t *dm = &dpdk_main;
1798 clib_error_t *error = 0;
1799 vlib_thread_main_t *tm = vlib_get_thread_main ();
1801 /* verify that structs are cacheline aligned */
1802 ASSERT (offsetof (dpdk_device_t, cacheline0) == 0);
1803 ASSERT (offsetof (dpdk_device_t, cacheline1) == CLIB_CACHE_LINE_BYTES);
1804 ASSERT (offsetof (dpdk_worker_t, cacheline0) == 0);
1805 ASSERT (offsetof (frame_queue_trace_t, cacheline0) == 0);
1808 dm->vnet_main = vnet_get_main ();
1809 dm->conf = &dpdk_config_main;
1811 ei = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
1813 return clib_error_return (0, "ethernet-input node AWOL");
1815 dm->ethernet_input_node_index = ei->index;
1817 dm->conf->nchannels = 4;
1818 dm->conf->num_mbufs = dm->conf->num_mbufs ? dm->conf->num_mbufs : NB_MBUF;
1819 vec_add1 (dm->conf->eal_init_args, (u8 *) "vnet");
1821 dm->dpdk_device_by_kni_port_id = hash_create (0, sizeof (uword));
1822 dm->vu_sw_if_index_by_listener_fd = hash_create (0, sizeof (uword));
1823 dm->vu_sw_if_index_by_sock_fd = hash_create (0, sizeof (uword));
1825 /* $$$ use n_thread_stacks since it's known-good at this point */
1826 vec_validate (dm->recycle, tm->n_thread_stacks - 1);
1828 /* initialize EFD (early fast discard) default settings */
1829 dm->efd.enabled = DPDK_EFD_DISABLED;
1830 dm->efd.queue_hi_thresh = ((DPDK_EFD_DEFAULT_DEVICE_QUEUE_HI_THRESH_PCT *
1831 DPDK_NB_RX_DESC_10GE) / 100);
1832 dm->efd.consec_full_frames_hi_thresh =
1833 DPDK_EFD_DEFAULT_CONSEC_FULL_FRAMES_HI_THRESH;
1835 /* vhost-user coalescence frames defaults */
1836 dm->conf->vhost_coalesce_frames = 32;
1837 dm->conf->vhost_coalesce_time = 1e-3;
1839 /* Default vlib_buffer_t flags, DISABLES tcp/udp checksumming... */
1840 dm->buffer_flags_template =
1841 (VLIB_BUFFER_TOTAL_LENGTH_VALID
1842 | IP_BUFFER_L4_CHECKSUM_COMPUTED | IP_BUFFER_L4_CHECKSUM_CORRECT);
1844 dm->stat_poll_interval = DPDK_STATS_POLL_INTERVAL;
1845 dm->link_state_poll_interval = DPDK_LINK_POLL_INTERVAL;
1848 if ((error = vlib_call_init_function (vm, dpdk_cli_init)))
1854 VLIB_INIT_FUNCTION (dpdk_init);
1858 * fd.io coding-style-patch-verification: ON
1861 * eval: (c-set-style "gnu")