4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <arpa/inet.h>
36 #include <linux/if_ether.h>
37 #include <linux/if_vlan.h>
38 #include <linux/virtio_net.h>
39 #include <linux/virtio_ring.h>
42 #include <sys/eventfd.h>
43 #include <sys/param.h>
46 #include <rte_atomic.h>
47 #include <rte_cycles.h>
48 #include <rte_ethdev.h>
50 #include <rte_string_fns.h>
51 #include <rte_pause.h>
54 #include "virtio-net.h"
55 #include "xen_vhost.h"
57 #define MAX_QUEUES 128
59 /* the maximum number of external ports supported */
60 #define MAX_SUP_PORTS 1
63 * Calculate the number of buffers needed per port
65 #define NUM_MBUFS_PER_PORT ((MAX_QUEUES*RTE_TEST_RX_DESC_DEFAULT) + \
66 (num_switching_cores*MAX_PKT_BURST) + \
67 (num_switching_cores*RTE_TEST_TX_DESC_DEFAULT) +\
68 (num_switching_cores*MBUF_CACHE_SIZE))
70 #define MBUF_CACHE_SIZE 64
73 * RX and TX Prefetch, Host, and Write-back threshold values should be
74 * carefully set for optimal performance. Consult the network
75 * controller's datasheet and supporting DPDK documentation for guidance
76 * on how these parameters should be set.
78 #define RX_PTHRESH 8 /* Default values of RX prefetch threshold reg. */
79 #define RX_HTHRESH 8 /* Default values of RX host threshold reg. */
80 #define RX_WTHRESH 4 /* Default values of RX write-back threshold reg. */
83 * These default values are optimized for use with the Intel(R) 82599 10 GbE
84 * Controller and the DPDK ixgbe PMD. Consider using other values for other
85 * network controllers and/or network drivers.
87 #define TX_PTHRESH 36 /* Default values of TX prefetch threshold reg. */
88 #define TX_HTHRESH 0 /* Default values of TX host threshold reg. */
89 #define TX_WTHRESH 0 /* Default values of TX write-back threshold reg. */
91 #define MAX_PKT_BURST 32 /* Max burst size for RX/TX */
92 #define MAX_MRG_PKT_BURST 16 /* Max burst for merge buffers. Set to 1 due to performance issue. */
93 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
95 /* State of virtio device. */
96 #define DEVICE_NOT_READY 0
97 #define DEVICE_READY 1
98 #define DEVICE_SAFE_REMOVE 2
100 /* Config_core_flag status definitions. */
101 #define REQUEST_DEV_REMOVAL 1
102 #define ACK_DEV_REMOVAL 0
104 /* Configurable number of RX/TX ring descriptors */
105 #define RTE_TEST_RX_DESC_DEFAULT 128
106 #define RTE_TEST_TX_DESC_DEFAULT 512
108 #define INVALID_PORT_ID 0xFF
110 /* Max number of devices. Limited by vmdq. */
111 #define MAX_DEVICES 64
113 /* Size of buffers used for snprintfs. */
114 #define MAX_PRINT_BUFF 6072
117 /* Maximum long option length for option parsing. */
118 #define MAX_LONG_OPT_SZ 64
120 /* Used to compare MAC addresses. */
121 #define MAC_ADDR_CMP 0xFFFFFFFFFFFF
123 /* mask of enabled ports */
124 static uint32_t enabled_port_mask = 0;
126 /*Number of switching cores enabled*/
127 static uint32_t num_switching_cores = 0;
129 /* number of devices/queues to support*/
130 static uint32_t num_queues = 0;
131 uint32_t num_devices = 0;
133 /* Enable VM2VM communications. If this is disabled then the MAC address compare is skipped. */
134 static uint32_t enable_vm2vm = 1;
136 static uint32_t enable_stats = 0;
138 /* empty vmdq configuration structure. Filled in programatically */
139 static const struct rte_eth_conf vmdq_conf_default = {
141 .mq_mode = ETH_MQ_RX_VMDQ_ONLY,
143 .header_split = 0, /**< Header Split disabled */
144 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
145 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
147 * It is necessary for 1G NIC such as I350,
148 * this fixes bug of ipv4 forwarding in guest can't
149 * forward pakets from one virtio dev to another virtio dev.
151 .hw_vlan_strip = 1, /**< VLAN strip enabled. */
152 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
153 .hw_strip_crc = 1, /**< CRC stripped by hardware */
157 .mq_mode = ETH_MQ_TX_NONE,
161 * should be overridden separately in code with
165 .nb_queue_pools = ETH_8_POOLS,
166 .enable_default_pool = 0,
169 .pool_map = {{0, 0},},
174 static unsigned lcore_ids[RTE_MAX_LCORE];
175 static uint8_t ports[RTE_MAX_ETHPORTS];
176 static unsigned num_ports = 0; /**< The number of ports specified in command line */
178 const uint16_t vlan_tags[] = {
179 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007,
180 1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015,
181 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
182 1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031,
183 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039,
184 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047,
185 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055,
186 1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063,
189 /* ethernet addresses of ports */
190 static struct ether_addr vmdq_ports_eth_addr[RTE_MAX_ETHPORTS];
192 /* heads for the main used and free linked lists for the data path. */
193 static struct virtio_net_data_ll *ll_root_used = NULL;
194 static struct virtio_net_data_ll *ll_root_free = NULL;
196 /* Array of data core structures containing information on individual core linked lists. */
197 static struct lcore_info lcore_info[RTE_MAX_LCORE];
199 /* Used for queueing bursts of TX packets. */
203 struct rte_mbuf *m_table[MAX_PKT_BURST];
206 /* TX queue for each data core. */
207 struct mbuf_table lcore_tx_queue[RTE_MAX_LCORE];
209 /* Vlan header struct used to insert vlan tags on TX. */
211 unsigned char h_dest[ETH_ALEN];
212 unsigned char h_source[ETH_ALEN];
215 __be16 h_vlan_encapsulated_proto;
218 /* Header lengths. */
220 #define VLAN_ETH_HLEN 18
222 /* Per-device statistics struct */
223 struct device_statistics {
225 rte_atomic64_t rx_total;
228 } __rte_cache_aligned;
229 struct device_statistics dev_statistics[MAX_DEVICES];
232 * Builds up the correct configuration for VMDQ VLAN pool map
233 * according to the pool & queue limits.
236 get_eth_conf(struct rte_eth_conf *eth_conf, uint32_t num_devices)
238 struct rte_eth_vmdq_rx_conf conf;
241 memset(&conf, 0, sizeof(conf));
242 conf.nb_queue_pools = (enum rte_eth_nb_pools)num_devices;
243 conf.nb_pool_maps = num_devices;
245 for (i = 0; i < conf.nb_pool_maps; i++) {
246 conf.pool_map[i].vlan_id = vlan_tags[ i ];
247 conf.pool_map[i].pools = (1UL << i);
250 (void)(rte_memcpy(eth_conf, &vmdq_conf_default, sizeof(*eth_conf)));
251 (void)(rte_memcpy(ð_conf->rx_adv_conf.vmdq_rx_conf, &conf,
252 sizeof(eth_conf->rx_adv_conf.vmdq_rx_conf)));
257 * Validate the device number according to the max pool number gotten form dev_info
258 * If the device number is invalid, give the error message and return -1.
259 * Each device must have its own pool.
262 validate_num_devices(uint32_t max_nb_devices)
264 if (num_devices > max_nb_devices) {
265 RTE_LOG(ERR, VHOST_PORT, "invalid number of devices\n");
272 * Initialises a given port using global settings and with the rx buffers
273 * coming from the mbuf_pool passed as parameter
276 port_init(uint8_t port, struct rte_mempool *mbuf_pool)
278 struct rte_eth_dev_info dev_info;
279 struct rte_eth_rxconf *rxconf;
280 struct rte_eth_conf port_conf;
281 uint16_t rx_rings, tx_rings = (uint16_t)rte_lcore_count();
282 uint16_t rx_ring_size = RTE_TEST_RX_DESC_DEFAULT;
283 uint16_t tx_ring_size = RTE_TEST_TX_DESC_DEFAULT;
287 /* The max pool number from dev_info will be used to validate the pool number specified in cmd line */
288 rte_eth_dev_info_get (port, &dev_info);
290 /*configure the number of supported virtio devices based on VMDQ limits */
291 num_devices = dev_info.max_vmdq_pools;
292 num_queues = dev_info.max_rx_queues;
294 retval = validate_num_devices(MAX_DEVICES);
298 /* Get port configuration. */
299 retval = get_eth_conf(&port_conf, num_devices);
303 if (port >= rte_eth_dev_count()) return -1;
305 rx_rings = (uint16_t)num_queues,
306 /* Configure ethernet device. */
307 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
311 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &rx_ring_size,
315 if (rx_ring_size > RTE_TEST_RX_DESC_DEFAULT ||
316 tx_ring_size > RTE_TEST_TX_DESC_DEFAULT) {
317 RTE_LOG(ERR, VHOST_PORT, "Mbuf pool has an insufficient size for "
322 rte_eth_dev_info_get(port, &dev_info);
323 rxconf = &dev_info.default_rxconf;
324 rxconf->rx_drop_en = 1;
325 /* Setup the queues. */
326 for (q = 0; q < rx_rings; q ++) {
327 retval = rte_eth_rx_queue_setup(port, q, rx_ring_size,
328 rte_eth_dev_socket_id(port), rxconf,
333 for (q = 0; q < tx_rings; q ++) {
334 retval = rte_eth_tx_queue_setup(port, q, tx_ring_size,
335 rte_eth_dev_socket_id(port),
341 /* Start the device. */
342 retval = rte_eth_dev_start(port);
346 rte_eth_macaddr_get(port, &vmdq_ports_eth_addr[port]);
347 RTE_LOG(INFO, VHOST_PORT, "Max virtio devices supported: %u\n", num_devices);
348 RTE_LOG(INFO, VHOST_PORT, "Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
349 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
351 vmdq_ports_eth_addr[port].addr_bytes[0],
352 vmdq_ports_eth_addr[port].addr_bytes[1],
353 vmdq_ports_eth_addr[port].addr_bytes[2],
354 vmdq_ports_eth_addr[port].addr_bytes[3],
355 vmdq_ports_eth_addr[port].addr_bytes[4],
356 vmdq_ports_eth_addr[port].addr_bytes[5]);
362 * Parse the portmask provided at run time.
365 parse_portmask(const char *portmask)
372 /* parse hexadecimal string */
373 pm = strtoul(portmask, &end, 16);
374 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0))
385 * Parse num options at run time.
388 parse_num_opt(const char *q_arg, uint32_t max_valid_value)
395 /* parse unsigned int string */
396 num = strtoul(q_arg, &end, 10);
397 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0))
400 if (num > max_valid_value)
411 us_vhost_usage(const char *prgname)
413 RTE_LOG(INFO, VHOST_CONFIG, "%s [EAL options] -- -p PORTMASK --vm2vm [0|1] --stats [0-N] --nb-devices ND\n"
414 " -p PORTMASK: Set mask for ports to be used by application\n"
415 " --vm2vm [0|1]: disable/enable(default) vm2vm comms\n"
416 " --stats [0-N]: 0: Disable stats, N: Time in seconds to print stats\n",
421 * Parse the arguments given in the command line of the application.
424 us_vhost_parse_args(int argc, char **argv)
429 const char *prgname = argv[0];
430 static struct option long_option[] = {
431 {"vm2vm", required_argument, NULL, 0},
432 {"stats", required_argument, NULL, 0},
436 /* Parse command line */
437 while ((opt = getopt_long(argc, argv, "p:",long_option, &option_index)) != EOF) {
441 enabled_port_mask = parse_portmask(optarg);
442 if (enabled_port_mask == 0) {
443 RTE_LOG(INFO, VHOST_CONFIG, "Invalid portmask\n");
444 us_vhost_usage(prgname);
450 /* Enable/disable vm2vm comms. */
451 if (!strncmp(long_option[option_index].name, "vm2vm", MAX_LONG_OPT_SZ)) {
452 ret = parse_num_opt(optarg, 1);
454 RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for vm2vm [0|1]\n");
455 us_vhost_usage(prgname);
462 /* Enable/disable stats. */
463 if (!strncmp(long_option[option_index].name, "stats", MAX_LONG_OPT_SZ)) {
464 ret = parse_num_opt(optarg, INT32_MAX);
466 RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for stats [0..N]\n");
467 us_vhost_usage(prgname);
475 /* Invalid option - print options. */
477 us_vhost_usage(prgname);
482 for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
483 if (enabled_port_mask & (1 << i))
484 ports[num_ports++] = (uint8_t)i;
487 if ((num_ports == 0) || (num_ports > MAX_SUP_PORTS)) {
488 RTE_LOG(INFO, VHOST_PORT, "Current enabled port number is %u,"
489 "but only %u port can be enabled\n",num_ports, MAX_SUP_PORTS);
497 * Update the global var NUM_PORTS and array PORTS according to system ports number
498 * and return valid ports number
500 static unsigned check_ports_num(unsigned nb_ports)
502 unsigned valid_num_ports = num_ports;
505 if (num_ports > nb_ports) {
506 RTE_LOG(INFO, VHOST_PORT, "\nSpecified port number(%u) exceeds total system port number(%u)\n",
507 num_ports, nb_ports);
508 num_ports = nb_ports;
511 for (portid = 0; portid < num_ports; portid ++) {
512 if (ports[portid] >= nb_ports) {
513 RTE_LOG(INFO, VHOST_PORT, "\nSpecified port ID(%u) exceeds max system port ID(%u)\n",
514 ports[portid], (nb_ports - 1));
515 ports[portid] = INVALID_PORT_ID;
519 return valid_num_ports;
523 * Function to convert guest physical addresses to vhost virtual addresses. This
524 * is used to convert virtio buffer addresses.
526 static __rte_always_inline uint64_t
527 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
529 struct virtio_memory_regions *region;
531 uint64_t vhost_va = 0;
533 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
534 region = &dev->mem->regions[regionidx];
535 if ((guest_pa >= region->guest_phys_address) &&
536 (guest_pa <= region->guest_phys_address_end)) {
537 vhost_va = region->address_offset + guest_pa;
541 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%" PRIu64 ") GPA %p| VVA %p\n",
542 dev->device_fh, (void*)(uintptr_t)guest_pa, (void*)(uintptr_t)vhost_va);
548 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
549 * be received from the physical port or from another virtio device. A packet
550 * count is returned to indicate the number of packets that were successfully
551 * added to the RX queue.
553 static __rte_always_inline uint32_t
554 virtio_dev_rx(struct virtio_net *dev, struct rte_mbuf **pkts, uint32_t count)
556 struct vhost_virtqueue *vq;
557 struct vring_desc *desc;
558 struct rte_mbuf *buff;
559 /* The virtio_hdr is initialised to 0. */
560 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0,0,0,0,0,0},0};
561 uint64_t buff_addr = 0;
562 uint64_t buff_hdr_addr = 0;
563 uint32_t head[MAX_PKT_BURST], packet_len = 0;
564 uint32_t head_idx, packet_success = 0;
565 uint16_t avail_idx, res_cur_idx;
566 uint16_t res_base_idx, res_end_idx;
567 uint16_t free_entries;
571 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%" PRIu64 ") virtio_dev_rx()\n", dev->device_fh);
572 vq = dev->virtqueue_rx;
573 count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
574 /* As many data cores may want access to available buffers, they need to be reserved. */
577 res_base_idx = vq->last_used_idx_res;
579 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
581 free_entries = (avail_idx - res_base_idx);
583 /*check that we have enough buffers*/
584 if (unlikely(count > free_entries))
585 count = free_entries;
590 res_end_idx = res_base_idx + count;
591 /* vq->last_used_idx_res is atomically updated. */
592 success = rte_atomic16_cmpset(&vq->last_used_idx_res, res_base_idx,
594 } while (unlikely(success == 0));
595 res_cur_idx = res_base_idx;
596 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%" PRIu64 ") Current Index %d| End Index %d\n",
597 dev->device_fh, res_cur_idx, res_end_idx);
599 /* Prefetch available ring to retrieve indexes. */
600 rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
602 /* Retrieve all of the head indexes first to avoid caching issues. */
603 for (head_idx = 0; head_idx < count; head_idx++)
604 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) & (vq->size - 1)];
606 /*Prefetch descriptor index. */
607 rte_prefetch0(&vq->desc[head[packet_success]]);
609 while (res_cur_idx != res_end_idx) {
610 /* Get descriptor from available ring */
611 desc = &vq->desc[head[packet_success]];
612 /* Prefetch descriptor address. */
615 buff = pkts[packet_success];
617 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
618 buff_addr = gpa_to_vva(dev, desc->addr);
619 /* Prefetch buffer address. */
620 rte_prefetch0((void*)(uintptr_t)buff_addr);
623 /* Copy virtio_hdr to packet and increment buffer address */
624 buff_hdr_addr = buff_addr;
625 packet_len = rte_pktmbuf_data_len(buff) + vq->vhost_hlen;
628 * If the descriptors are chained the header and data are placed in
631 if (desc->flags & VRING_DESC_F_NEXT) {
632 desc->len = vq->vhost_hlen;
633 desc = &vq->desc[desc->next];
634 /* Buffer address translation. */
635 buff_addr = gpa_to_vva(dev, desc->addr);
636 desc->len = rte_pktmbuf_data_len(buff);
638 buff_addr += vq->vhost_hlen;
639 desc->len = packet_len;
643 /* Update used ring with desc information */
644 vq->used->ring[res_cur_idx & (vq->size - 1)].id = head[packet_success];
645 vq->used->ring[res_cur_idx & (vq->size - 1)].len = packet_len;
647 /* Copy mbuf data to buffer */
648 userdata = rte_pktmbuf_mtod(buff, void *);
649 rte_memcpy((void *)(uintptr_t)buff_addr, userdata, rte_pktmbuf_data_len(buff));
654 /* mergeable is disabled then a header is required per buffer. */
655 rte_memcpy((void *)(uintptr_t)buff_hdr_addr, (const void *)&virtio_hdr, vq->vhost_hlen);
656 if (res_cur_idx < res_end_idx) {
657 /* Prefetch descriptor index. */
658 rte_prefetch0(&vq->desc[head[packet_success]]);
662 rte_compiler_barrier();
664 /* Wait until it's our turn to add our buffer to the used ring. */
665 while (unlikely(vq->last_used_idx != res_base_idx))
668 *(volatile uint16_t *)&vq->used->idx += count;
670 vq->last_used_idx = res_end_idx;
676 * Compares a packet destination MAC address to a device MAC address.
678 static __rte_always_inline int
679 ether_addr_cmp(struct ether_addr *ea, struct ether_addr *eb)
681 return ((*(uint64_t *)ea ^ *(uint64_t *)eb) & MAC_ADDR_CMP) == 0;
685 * This function registers mac along with a
686 * vlan tag to a VMDQ.
689 link_vmdq(struct virtio_net *dev)
692 struct virtio_net_data_ll *dev_ll;
694 dev_ll = ll_root_used;
696 while (dev_ll != NULL) {
697 if ((dev != dev_ll->dev) && ether_addr_cmp(&dev->mac_address, &dev_ll->dev->mac_address)) {
698 RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") WARNING: This device is using an existing MAC address and has not been registered.\n", dev->device_fh);
701 dev_ll = dev_ll->next;
704 /* vlan_tag currently uses the device_id. */
705 dev->vlan_tag = vlan_tags[dev->device_fh];
706 dev->vmdq_rx_q = dev->device_fh * (num_queues/num_devices);
708 /* Print out VMDQ registration info. */
709 RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") MAC_ADDRESS %02x:%02x:%02x:%02x:%02x:%02x and VLAN_TAG %d registered\n",
711 dev->mac_address.addr_bytes[0], dev->mac_address.addr_bytes[1],
712 dev->mac_address.addr_bytes[2], dev->mac_address.addr_bytes[3],
713 dev->mac_address.addr_bytes[4], dev->mac_address.addr_bytes[5],
716 /* Register the MAC address. */
717 ret = rte_eth_dev_mac_addr_add(ports[0], &dev->mac_address, (uint32_t)dev->device_fh);
719 RTE_LOG(ERR, VHOST_DATA, "(%"PRIu64") Failed to add device MAC address to VMDQ\n",
724 /* Enable stripping of the vlan tag as we handle routing. */
725 rte_eth_dev_set_vlan_strip_on_queue(ports[0], dev->vmdq_rx_q, 1);
727 rte_compiler_barrier();
728 /* Set device as ready for RX. */
729 dev->ready = DEVICE_READY;
735 * Removes MAC address and vlan tag from VMDQ. Ensures that nothing is adding buffers to the RX
736 * queue before disabling RX on the device.
739 unlink_vmdq(struct virtio_net *dev)
743 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
745 if (dev->ready == DEVICE_READY) {
746 /*clear MAC and VLAN settings*/
747 rte_eth_dev_mac_addr_remove(ports[0], &dev->mac_address);
748 for (i = 0; i < 6; i++)
749 dev->mac_address.addr_bytes[i] = 0;
753 /*Clear out the receive buffers*/
754 rx_count = rte_eth_rx_burst(ports[0],
755 (uint16_t)dev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
758 for (i = 0; i < rx_count; i++)
759 rte_pktmbuf_free(pkts_burst[i]);
761 rx_count = rte_eth_rx_burst(ports[0],
762 (uint16_t)dev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
765 dev->ready = DEVICE_NOT_READY;
770 * Check if the packet destination MAC address is for a local device. If so then put
771 * the packet on that devices RX queue. If not then return.
773 static __rte_always_inline unsigned
774 virtio_tx_local(struct virtio_net *dev, struct rte_mbuf *m)
776 struct virtio_net_data_ll *dev_ll;
777 struct ether_hdr *pkt_hdr;
780 pkt_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
782 /*get the used devices list*/
783 dev_ll = ll_root_used;
785 while (dev_ll != NULL) {
786 if (likely(dev_ll->dev->ready == DEVICE_READY) && ether_addr_cmp(&(pkt_hdr->d_addr),
787 &dev_ll->dev->mac_address)) {
789 /* Drop the packet if the TX packet is destined for the TX device. */
790 if (dev_ll->dev->device_fh == dev->device_fh) {
791 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%" PRIu64 ") TX: "
792 "Source and destination MAC addresses are the same. "
793 "Dropping packet.\n",
794 dev_ll->dev->device_fh);
799 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%" PRIu64 ") TX: "
800 "MAC address is local\n", dev_ll->dev->device_fh);
802 if (dev_ll->dev->remove) {
803 /*drop the packet if the device is marked for removal*/
804 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%" PRIu64 ") "
805 "Device is marked for removal\n",
806 dev_ll->dev->device_fh);
808 /*send the packet to the local virtio device*/
809 ret = virtio_dev_rx(dev_ll->dev, &m, 1);
811 rte_atomic64_add(&dev_statistics[dev_ll->dev->device_fh].rx_total, 1);
812 rte_atomic64_add(&dev_statistics[dev_ll->dev->device_fh].rx, ret);
813 dev_statistics[dev->device_fh].tx_total++;
814 dev_statistics[dev->device_fh].tx += ret;
820 dev_ll = dev_ll->next;
827 * This function routes the TX packet to the correct interface. This may be a local device
828 * or the physical port.
830 static __rte_always_inline void
831 virtio_tx_route(struct virtio_net* dev, struct rte_mbuf *m, struct rte_mempool *mbuf_pool, uint16_t vlan_tag)
833 struct mbuf_table *tx_q;
834 struct vlan_ethhdr *vlan_hdr;
835 struct rte_mbuf **m_table;
836 struct rte_mbuf *mbuf;
838 const uint16_t lcore_id = rte_lcore_id();
840 /*check if destination is local VM*/
841 if (enable_vm2vm && (virtio_tx_local(dev, m) == 0)) {
845 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%" PRIu64 ") TX: "
846 "MAC address is external\n", dev->device_fh);
848 /*Add packet to the port tx queue*/
849 tx_q = &lcore_tx_queue[lcore_id];
852 /* Allocate an mbuf and populate the structure. */
853 mbuf = rte_pktmbuf_alloc(mbuf_pool);
857 mbuf->data_len = m->data_len + VLAN_HLEN;
858 mbuf->pkt_len = mbuf->data_len;
860 /* Copy ethernet header to mbuf. */
861 rte_memcpy(rte_pktmbuf_mtod(mbuf, void*),
862 rte_pktmbuf_mtod(m, const void*), ETH_HLEN);
865 /* Setup vlan header. Bytes need to be re-ordered for network with htons()*/
866 vlan_hdr = rte_pktmbuf_mtod(mbuf, struct vlan_ethhdr *);
867 vlan_hdr->h_vlan_encapsulated_proto = vlan_hdr->h_vlan_proto;
868 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
869 vlan_hdr->h_vlan_TCI = htons(vlan_tag);
871 /* Copy the remaining packet contents to the mbuf. */
872 rte_memcpy(rte_pktmbuf_mtod_offset(mbuf, void *, VLAN_ETH_HLEN),
873 rte_pktmbuf_mtod_offset(m, const void *, ETH_HLEN),
874 (m->data_len - ETH_HLEN));
875 tx_q->m_table[len] = mbuf;
878 dev_statistics[dev->device_fh].tx_total++;
879 dev_statistics[dev->device_fh].tx++;
882 if (unlikely(len == MAX_PKT_BURST)) {
883 m_table = (struct rte_mbuf **)tx_q->m_table;
884 ret = rte_eth_tx_burst(ports[0], (uint16_t)tx_q->txq_id, m_table, (uint16_t) len);
885 /* Free any buffers not handled by TX and update the port stats. */
886 if (unlikely(ret < len)) {
888 rte_pktmbuf_free(m_table[ret]);
889 } while (++ret < len);
899 static __rte_always_inline void
900 virtio_dev_tx(struct virtio_net* dev, struct rte_mempool *mbuf_pool)
903 struct vhost_virtqueue *vq;
904 struct vring_desc *desc;
905 uint64_t buff_addr = 0;
906 uint32_t head[MAX_PKT_BURST];
909 uint16_t free_entries, packet_success = 0;
912 vq = dev->virtqueue_tx;
913 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
915 /* If there are no available buffers then return. */
916 if (vq->last_used_idx == avail_idx)
919 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%" PRIu64 ") virtio_dev_tx()\n",
922 /* Prefetch available ring to retrieve head indexes. */
923 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
925 /*get the number of free entries in the ring*/
926 free_entries = avail_idx - vq->last_used_idx;
927 free_entries = unlikely(free_entries < MAX_PKT_BURST) ? free_entries : MAX_PKT_BURST;
929 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%" PRIu64 ") Buffers available %d\n",
930 dev->device_fh, free_entries);
931 /* Retrieve all of the head indexes first to avoid caching issues. */
932 for (i = 0; i < free_entries; i++)
933 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
935 /* Prefetch descriptor index. */
936 rte_prefetch0(&vq->desc[head[packet_success]]);
938 while (packet_success < free_entries) {
939 desc = &vq->desc[head[packet_success]];
940 /* Prefetch descriptor address. */
943 if (packet_success < (free_entries - 1)) {
944 /* Prefetch descriptor index. */
945 rte_prefetch0(&vq->desc[head[packet_success+1]]);
948 /* Update used index buffer information. */
949 used_idx = vq->last_used_idx & (vq->size - 1);
950 vq->used->ring[used_idx].id = head[packet_success];
951 vq->used->ring[used_idx].len = 0;
953 /* Discard first buffer as it is the virtio header */
954 desc = &vq->desc[desc->next];
956 /* Buffer address translation. */
957 buff_addr = gpa_to_vva(dev, desc->addr);
958 /* Prefetch buffer address. */
959 rte_prefetch0((void*)(uintptr_t)buff_addr);
961 /* Setup dummy mbuf. This is copied to a real mbuf if transmitted out the physical port. */
962 m.data_len = desc->len;
966 virtio_tx_route(dev, &m, mbuf_pool, 0);
972 rte_compiler_barrier();
973 vq->used->idx += packet_success;
974 /* Kick guest if required. */
978 * This function is called by each data core. It handles all RX/TX registered with the
979 * core. For TX the specific lcore linked list is used. For RX, MAC addresses are compared
980 * with all devices in the main linked list.
983 switch_worker(__attribute__((unused)) void *arg)
985 struct rte_mempool *mbuf_pool = arg;
986 struct virtio_net *dev = NULL;
987 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
988 struct virtio_net_data_ll *dev_ll;
989 struct mbuf_table *tx_q;
990 volatile struct lcore_ll_info *lcore_ll;
991 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
992 uint64_t prev_tsc, diff_tsc, cur_tsc, ret_count = 0;
994 const uint16_t lcore_id = rte_lcore_id();
995 const uint16_t num_cores = (uint16_t)rte_lcore_count();
996 uint16_t rx_count = 0;
998 RTE_LOG(INFO, VHOST_DATA, "Procesing on Core %u started \n", lcore_id);
999 lcore_ll = lcore_info[lcore_id].lcore_ll;
1002 tx_q = &lcore_tx_queue[lcore_id];
1003 for (i = 0; i < num_cores; i ++) {
1004 if (lcore_ids[i] == lcore_id) {
1011 cur_tsc = rte_rdtsc();
1013 * TX burst queue drain
1015 diff_tsc = cur_tsc - prev_tsc;
1016 if (unlikely(diff_tsc > drain_tsc)) {
1019 RTE_LOG_DP(DEBUG, VHOST_DATA,
1020 "TX queue drained after timeout with burst size %u\n",
1023 /*Tx any packets in the queue*/
1024 ret = rte_eth_tx_burst(ports[0], (uint16_t)tx_q->txq_id,
1025 (struct rte_mbuf **)tx_q->m_table,
1026 (uint16_t)tx_q->len);
1027 if (unlikely(ret < tx_q->len)) {
1029 rte_pktmbuf_free(tx_q->m_table[ret]);
1030 } while (++ret < tx_q->len);
1041 * Inform the configuration core that we have exited the linked list and that no devices are
1042 * in use if requested.
1044 if (lcore_ll->dev_removal_flag == REQUEST_DEV_REMOVAL)
1045 lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
1050 dev_ll = lcore_ll->ll_root_used;
1052 while (dev_ll != NULL) {
1053 /*get virtio device ID*/
1056 if (unlikely(dev->remove)) {
1057 dev_ll = dev_ll->next;
1059 dev->ready = DEVICE_SAFE_REMOVE;
1062 if (likely(dev->ready == DEVICE_READY)) {
1064 rx_count = rte_eth_rx_burst(ports[0],
1065 (uint16_t)dev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
1068 ret_count = virtio_dev_rx(dev, pkts_burst, rx_count);
1070 rte_atomic64_add(&dev_statistics[dev_ll->dev->device_fh].rx_total, rx_count);
1071 rte_atomic64_add(&dev_statistics[dev_ll->dev->device_fh].rx, ret_count);
1073 while (likely(rx_count)) {
1075 rte_pktmbuf_free_seg(pkts_burst[rx_count]);
1081 if (likely(!dev->remove))
1083 virtio_dev_tx(dev, mbuf_pool);
1085 /*move to the next device in the list*/
1086 dev_ll = dev_ll->next;
1094 * Add an entry to a used linked list. A free entry must first be found in the free linked list
1095 * using get_data_ll_free_entry();
1098 add_data_ll_entry(struct virtio_net_data_ll **ll_root_addr, struct virtio_net_data_ll *ll_dev)
1100 struct virtio_net_data_ll *ll = *ll_root_addr;
1102 /* Set next as NULL and use a compiler barrier to avoid reordering. */
1103 ll_dev->next = NULL;
1104 rte_compiler_barrier();
1106 /* If ll == NULL then this is the first device. */
1108 /* Increment to the tail of the linked list. */
1109 while ((ll->next != NULL) )
1114 *ll_root_addr = ll_dev;
1119 * Remove an entry from a used linked list. The entry must then be added to the free linked list
1120 * using put_data_ll_free_entry().
1123 rm_data_ll_entry(struct virtio_net_data_ll **ll_root_addr, struct virtio_net_data_ll *ll_dev, struct virtio_net_data_ll *ll_dev_last)
1125 struct virtio_net_data_ll *ll = *ll_root_addr;
1128 *ll_root_addr = ll_dev->next;
1130 ll_dev_last->next = ll_dev->next;
1134 * Find and return an entry from the free linked list.
1136 static struct virtio_net_data_ll *
1137 get_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr)
1139 struct virtio_net_data_ll *ll_free = *ll_root_addr;
1140 struct virtio_net_data_ll *ll_dev;
1142 if (ll_free == NULL)
1146 *ll_root_addr = ll_free->next;
1152 * Place an entry back on to the free linked list.
1155 put_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr, struct virtio_net_data_ll *ll_dev)
1157 struct virtio_net_data_ll *ll_free = *ll_root_addr;
1159 ll_dev->next = ll_free;
1160 *ll_root_addr = ll_dev;
1164 * Creates a linked list of a given size.
1166 static struct virtio_net_data_ll *
1167 alloc_data_ll(uint32_t size)
1169 struct virtio_net_data_ll *ll_new;
1172 /* Malloc and then chain the linked list. */
1173 ll_new = malloc(size * sizeof(struct virtio_net_data_ll));
1174 if (ll_new == NULL) {
1175 RTE_LOG(ERR, VHOST_CONFIG, "Failed to allocate memory for ll_new.\n");
1179 for (i = 0; i < size - 1; i++) {
1180 ll_new[i].dev = NULL;
1181 ll_new[i].next = &ll_new[i+1];
1183 ll_new[i].next = NULL;
1189 * Create the main linked list along with each individual cores linked list. A used and a free list
1190 * are created to manage entries.
1197 RTE_LCORE_FOREACH_SLAVE(lcore) {
1198 lcore_info[lcore].lcore_ll = malloc(sizeof(struct lcore_ll_info));
1199 if (lcore_info[lcore].lcore_ll == NULL) {
1200 RTE_LOG(ERR, VHOST_CONFIG, "Failed to allocate memory for lcore_ll.\n");
1204 lcore_info[lcore].lcore_ll->device_num = 0;
1205 lcore_info[lcore].lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
1206 lcore_info[lcore].lcore_ll->ll_root_used = NULL;
1207 if (num_devices % num_switching_cores)
1208 lcore_info[lcore].lcore_ll->ll_root_free = alloc_data_ll((num_devices / num_switching_cores) + 1);
1210 lcore_info[lcore].lcore_ll->ll_root_free = alloc_data_ll(num_devices / num_switching_cores);
1213 /* Allocate devices up to a maximum of MAX_DEVICES. */
1214 ll_root_free = alloc_data_ll(MIN((num_devices), MAX_DEVICES));
1219 * Remove a device from the specific data core linked list and from the main linked list. The
1220 * rx/tx thread must be set the flag to indicate that it is safe to remove the device.
1224 destroy_device (volatile struct virtio_net *dev)
1226 struct virtio_net_data_ll *ll_lcore_dev_cur;
1227 struct virtio_net_data_ll *ll_main_dev_cur;
1228 struct virtio_net_data_ll *ll_lcore_dev_last = NULL;
1229 struct virtio_net_data_ll *ll_main_dev_last = NULL;
1232 dev->flags &= ~VIRTIO_DEV_RUNNING;
1234 /*set the remove flag. */
1237 while(dev->ready != DEVICE_SAFE_REMOVE) {
1241 /* Search for entry to be removed from lcore ll */
1242 ll_lcore_dev_cur = lcore_info[dev->coreid].lcore_ll->ll_root_used;
1243 while (ll_lcore_dev_cur != NULL) {
1244 if (ll_lcore_dev_cur->dev == dev) {
1247 ll_lcore_dev_last = ll_lcore_dev_cur;
1248 ll_lcore_dev_cur = ll_lcore_dev_cur->next;
1252 /* Search for entry to be removed from main ll */
1253 ll_main_dev_cur = ll_root_used;
1254 ll_main_dev_last = NULL;
1255 while (ll_main_dev_cur != NULL) {
1256 if (ll_main_dev_cur->dev == dev) {
1259 ll_main_dev_last = ll_main_dev_cur;
1260 ll_main_dev_cur = ll_main_dev_cur->next;
1264 if (ll_lcore_dev_cur == NULL || ll_main_dev_cur == NULL) {
1265 RTE_LOG(ERR, XENHOST, "%s: could find device in per_cpu list or main_list\n", __func__);
1269 /* Remove entries from the lcore and main ll. */
1270 rm_data_ll_entry(&lcore_info[ll_lcore_dev_cur->dev->coreid].lcore_ll->ll_root_used, ll_lcore_dev_cur, ll_lcore_dev_last);
1271 rm_data_ll_entry(&ll_root_used, ll_main_dev_cur, ll_main_dev_last);
1273 /* Set the dev_removal_flag on each lcore. */
1274 RTE_LCORE_FOREACH_SLAVE(lcore) {
1275 lcore_info[lcore].lcore_ll->dev_removal_flag = REQUEST_DEV_REMOVAL;
1279 * Once each core has set the dev_removal_flag to ACK_DEV_REMOVAL we can be sure that
1280 * they can no longer access the device removed from the linked lists and that the devices
1281 * are no longer in use.
1283 RTE_LCORE_FOREACH_SLAVE(lcore) {
1284 while (lcore_info[lcore].lcore_ll->dev_removal_flag != ACK_DEV_REMOVAL) {
1289 /* Add the entries back to the lcore and main free ll.*/
1290 put_data_ll_free_entry(&lcore_info[ll_lcore_dev_cur->dev->coreid].lcore_ll->ll_root_free, ll_lcore_dev_cur);
1291 put_data_ll_free_entry(&ll_root_free, ll_main_dev_cur);
1293 /* Decrement number of device on the lcore. */
1294 lcore_info[ll_lcore_dev_cur->dev->coreid].lcore_ll->device_num--;
1296 RTE_LOG(INFO, VHOST_DATA, " #####(%"PRIu64") Device has been removed from data core\n", dev->device_fh);
1300 * A new device is added to a data core. First the device is added to the main linked list
1301 * and the allocated to a specific data core.
1304 new_device (struct virtio_net *dev)
1306 struct virtio_net_data_ll *ll_dev;
1307 int lcore, core_add = 0;
1308 uint32_t device_num_min = num_devices;
1310 /* Add device to main ll */
1311 ll_dev = get_data_ll_free_entry(&ll_root_free);
1312 if (ll_dev == NULL) {
1313 RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") No free entry found in linked list. Device limit "
1314 "of %d devices per core has been reached\n",
1315 dev->device_fh, num_devices);
1319 add_data_ll_entry(&ll_root_used, ll_dev);
1321 /*reset ready flag*/
1322 dev->ready = DEVICE_NOT_READY;
1325 /* Find a suitable lcore to add the device. */
1326 RTE_LCORE_FOREACH_SLAVE(lcore) {
1327 if (lcore_info[lcore].lcore_ll->device_num < device_num_min) {
1328 device_num_min = lcore_info[lcore].lcore_ll->device_num;
1332 /* Add device to lcore ll */
1333 ll_dev->dev->coreid = core_add;
1334 ll_dev = get_data_ll_free_entry(&lcore_info[ll_dev->dev->coreid].lcore_ll->ll_root_free);
1335 if (ll_dev == NULL) {
1336 RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") Failed to add device to data core\n", dev->device_fh);
1337 destroy_device(dev);
1341 add_data_ll_entry(&lcore_info[ll_dev->dev->coreid].lcore_ll->ll_root_used, ll_dev);
1343 /* Initialize device stats */
1344 memset(&dev_statistics[dev->device_fh], 0, sizeof(struct device_statistics));
1346 lcore_info[ll_dev->dev->coreid].lcore_ll->device_num++;
1347 dev->flags |= VIRTIO_DEV_RUNNING;
1349 RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") Device has been added to data core %d\n", dev->device_fh, dev->coreid);
1357 * These callback allow devices to be added to the data core when configuration
1358 * has been fully complete.
1360 static const struct virtio_net_device_ops virtio_net_device_ops =
1362 .new_device = new_device,
1363 .destroy_device = destroy_device,
1367 * This is a thread will wake up after a period to print stats if the user has
1373 struct virtio_net_data_ll *dev_ll;
1374 uint64_t tx_dropped, rx_dropped;
1375 uint64_t tx, tx_total, rx, rx_total;
1377 const char clr[] = { 27, '[', '2', 'J', '\0' };
1378 const char top_left[] = { 27, '[', '1', ';', '1', 'H','\0' };
1381 sleep(enable_stats);
1383 /* Clear screen and move to top left */
1384 printf("%s%s", clr, top_left);
1386 printf("\nDevice statistics ====================================");
1388 dev_ll = ll_root_used;
1389 while (dev_ll != NULL) {
1390 device_fh = (uint32_t)dev_ll->dev->device_fh;
1391 tx_total = dev_statistics[device_fh].tx_total;
1392 tx = dev_statistics[device_fh].tx;
1393 tx_dropped = tx_total - tx;
1394 rx_total = rte_atomic64_read(&dev_statistics[device_fh].rx_total);
1395 rx = rte_atomic64_read(&dev_statistics[device_fh].rx);
1396 rx_dropped = rx_total - rx;
1398 printf("\nStatistics for device %"PRIu32" ------------------------------"
1399 "\nTX total: %"PRIu64""
1400 "\nTX dropped: %"PRIu64""
1401 "\nTX successful: %"PRIu64""
1402 "\nRX total: %"PRIu64""
1403 "\nRX dropped: %"PRIu64""
1404 "\nRX successful: %"PRIu64"",
1413 dev_ll = dev_ll->next;
1415 printf("\n======================================================\n");
1420 int init_virtio_net(struct virtio_net_device_ops const * const ops);
1423 * Main function, does initialisation and calls the per-lcore functions.
1426 main(int argc, char *argv[])
1428 struct rte_mempool *mbuf_pool;
1429 unsigned lcore_id, core_id = 0;
1430 unsigned nb_ports, valid_num_ports;
1433 static pthread_t tid;
1434 char thread_name[RTE_MAX_THREAD_NAME_LEN];
1437 ret = rte_eal_init(argc, argv);
1439 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
1443 /* parse app arguments */
1444 ret = us_vhost_parse_args(argc, argv);
1446 rte_exit(EXIT_FAILURE, "Invalid argument\n");
1448 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id ++)
1449 if (rte_lcore_is_enabled(lcore_id))
1450 lcore_ids[core_id ++] = lcore_id;
1452 if (rte_lcore_count() > RTE_MAX_LCORE)
1453 rte_exit(EXIT_FAILURE,"Not enough cores\n");
1455 /*set the number of swithcing cores available*/
1456 num_switching_cores = rte_lcore_count()-1;
1458 /* Get the number of physical ports. */
1459 nb_ports = rte_eth_dev_count();
1462 * Update the global var NUM_PORTS and global array PORTS
1463 * and get value of var VALID_NUM_PORTS according to system ports number
1465 valid_num_ports = check_ports_num(nb_ports);
1467 if ((valid_num_ports == 0) || (valid_num_ports > MAX_SUP_PORTS)) {
1468 RTE_LOG(INFO, VHOST_PORT, "Current enabled port number is %u,"
1469 "but only %u port can be enabled\n",num_ports, MAX_SUP_PORTS);
1473 /* Create the mbuf pool. */
1474 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
1475 NUM_MBUFS_PER_PORT * valid_num_ports, MBUF_CACHE_SIZE, 0,
1476 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1477 if (mbuf_pool == NULL)
1478 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1480 /* initialize all ports */
1481 for (portid = 0; portid < nb_ports; portid++) {
1482 /* skip ports that are not enabled */
1483 if ((enabled_port_mask & (1 << portid)) == 0) {
1484 RTE_LOG(INFO, VHOST_PORT, "Skipping disabled port %d\n", portid);
1487 if (port_init(portid, mbuf_pool) != 0)
1488 rte_exit(EXIT_FAILURE, "Cannot initialize network ports\n");
1491 /* Initialise all linked lists. */
1492 if (init_data_ll() == -1)
1493 rte_exit(EXIT_FAILURE, "Failed to initialize linked list\n");
1495 /* Initialize device stats */
1496 memset(&dev_statistics, 0, sizeof(dev_statistics));
1498 /* Enable stats if the user option is set. */
1500 ret = pthread_create(&tid, NULL, (void *)print_stats, NULL);
1502 rte_exit(EXIT_FAILURE,
1503 "Cannot create print-stats thread\n");
1505 /* Set thread_name for aid in debugging. */
1506 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "print-xen-stats");
1507 ret = rte_thread_setname(tid, thread_name);
1509 RTE_LOG(DEBUG, VHOST_CONFIG,
1510 "Cannot set print-stats name\n");
1513 /* Launch all data cores. */
1514 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1515 rte_eal_remote_launch(switch_worker, mbuf_pool, lcore_id);
1518 init_virtio_xen(&virtio_net_device_ops);
1520 virtio_monitor_loop();