aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/vnet.am10
-rw-r--r--src/vnet/devices/virtio/vhost_user.c (renamed from src/vnet/devices/virtio/vhost-user.c)1548
-rw-r--r--src/vnet/devices/virtio/vhost_user.h (renamed from src/vnet/devices/virtio/vhost-user.h)40
-rw-r--r--src/vnet/devices/virtio/vhost_user_api.c2
-rw-r--r--src/vnet/devices/virtio/vhost_user_inline.h262
-rw-r--r--src/vnet/devices/virtio/vhost_user_input.c678
-rw-r--r--src/vnet/devices/virtio/vhost_user_output.c671
7 files changed, 1675 insertions, 1536 deletions
diff --git a/src/vnet.am b/src/vnet.am
index d6a5d524e53..040a4cd4b6a 100644
--- a/src/vnet.am
+++ b/src/vnet.am
@@ -917,16 +917,22 @@ API_FILES += vnet/pg/pg.api
libvnet_la_SOURCES += \
vnet/devices/virtio/device.c \
vnet/devices/virtio/node.c \
- vnet/devices/virtio/vhost-user.c \
+ vnet/devices/virtio/vhost_user.c \
+ vnet/devices/virtio/vhost_user_input.c \
+ vnet/devices/virtio/vhost_user_output.c \
vnet/devices/virtio/vhost_user_api.c \
vnet/devices/virtio/virtio.c
nobase_include_HEADERS += \
vnet/devices/virtio/virtio.h \
- vnet/devices/virtio/vhost-user.h \
+ vnet/devices/virtio/vhost_user.h \
vnet/devices/virtio/vhost_user.api.h
+libvnet_multiversioning_sources += \
+ vnet/devices/virtio/vhost_user_input.c \
+ vnet/devices/virtio/vhost_user_output.c
+
API_FILES += vnet/devices/virtio/vhost_user.api
########################################
diff --git a/src/vnet/devices/virtio/vhost-user.c b/src/vnet/devices/virtio/vhost_user.c
index f6406d469b2..a8a7ae89065 100644
--- a/src/vnet/devices/virtio/vhost-user.c
+++ b/src/vnet/devices/virtio/vhost_user.c
@@ -2,7 +2,7 @@
*------------------------------------------------------------------
* vhost.c - vhost-user
*
- * Copyright (c) 2014 Cisco and/or its affiliates.
+ * Copyright (c) 2014-2018 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
@@ -39,7 +39,8 @@
#include <vnet/devices/devices.h>
#include <vnet/feature/feature.h>
-#include <vnet/devices/virtio/vhost-user.h>
+#include <vnet/devices/virtio/vhost_user.h>
+#include <vnet/devices/virtio/vhost_user_inline.h>
/**
* @file
@@ -49,122 +50,10 @@
*/
-#define VHOST_DEBUG_VQ 0
-
-#define DBG_SOCK(args...) \
- { \
- vhost_user_main_t *_vum = &vhost_user_main; \
- if (_vum->debug) \
- clib_warning(args); \
- };
-
-#if VHOST_DEBUG_VQ == 1
-#define DBG_VQ(args...) clib_warning(args);
-#else
-#define DBG_VQ(args...)
-#endif
-
-/*
- * When an RX queue is down but active, received packets
- * must be discarded. This value controls up to how many
- * packets will be discarded during each round.
- */
-#define VHOST_USER_DOWN_DISCARD_COUNT 256
-
-/*
- * When the number of available buffers gets under this threshold,
- * RX node will start discarding packets.
- */
-#define VHOST_USER_RX_BUFFER_STARVATION 32
-
-/*
- * On the receive side, the host should free descriptors as soon
- * as possible in order to avoid TX drop in the VM.
- * This value controls the number of copy operations that are stacked
- * before copy is done for all and descriptors are given back to
- * the guest.
- * The value 64 was obtained by testing (48 and 128 were not as good).
- */
-#define VHOST_USER_RX_COPY_THRESHOLD 64
-/*
- * On the transmit side, we keep processing the buffers from vlib in the while
- * loop and prepare the copy order to be executed later. However, the static
- * array which we keep the copy order is limited to VHOST_USER_COPY_ARRAY_N
- * entries. In order to not corrupt memory, we have to do the copy when the
- * static array reaches the copy threshold. We subtract 40 in case the code
- * goes into the inner loop for a maximum of 64k frames which may require
- * more array entries.
- */
-#define VHOST_USER_TX_COPY_THRESHOLD (VHOST_USER_COPY_ARRAY_N - 40)
-
-#define UNIX_GET_FD(unixfd_idx) ({ \
- typeof(unixfd_idx) __unixfd_idx = (unixfd_idx); \
- (__unixfd_idx != ~0) ? \
- pool_elt_at_index (file_main.file_pool, \
- __unixfd_idx)->file_descriptor : -1; })
-
-#define foreach_virtio_trace_flags \
- _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
- _ (SINGLE_DESC, 1, "Single descriptor packet") \
- _ (INDIRECT, 2, "Indirect descriptor") \
- _ (MAP_ERROR, 4, "Memory mapping error")
-
-typedef enum
-{
-#define _(n,i,s) VIRTIO_TRACE_F_##n,
- foreach_virtio_trace_flags
-#undef _
-} virtio_trace_flag_t;
-
-vlib_node_registration_t vhost_user_input_node;
-
-#define foreach_vhost_user_tx_func_error \
- _(NONE, "no error") \
- _(NOT_READY, "vhost vring not ready") \
- _(DOWN, "vhost interface is down") \
- _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
- _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
- _(MMAP_FAIL, "mmap failure") \
- _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
-
-typedef enum
-{
-#define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
- foreach_vhost_user_tx_func_error
-#undef _
- VHOST_USER_TX_FUNC_N_ERROR,
-} vhost_user_tx_func_error_t;
-
-static char *vhost_user_tx_func_error_strings[] = {
-#define _(n,s) s,
- foreach_vhost_user_tx_func_error
-#undef _
-};
-
-#define foreach_vhost_user_input_func_error \
- _(NO_ERROR, "no error") \
- _(NO_BUFFER, "no available buffer") \
- _(MMAP_FAIL, "mmap failure") \
- _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
- _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
- _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
-
-typedef enum
-{
-#define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
- foreach_vhost_user_input_func_error
-#undef _
- VHOST_USER_INPUT_FUNC_N_ERROR,
-} vhost_user_input_func_error_t;
-
-static char *vhost_user_input_func_error_strings[] = {
-#define _(n,s) s,
- foreach_vhost_user_input_func_error
-#undef _
-};
+vlib_node_registration_t vhost_user_send_interrupt_node;
/* *INDENT-OFF* */
-static vhost_user_main_t vhost_user_main = {
+vhost_user_main_t vhost_user_main = {
.mtu_bytes = 1518,
};
@@ -173,195 +62,6 @@ VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
};
/* *INDENT-ON* */
-static u8 *
-format_vhost_user_interface_name (u8 * s, va_list * args)
-{
- u32 i = va_arg (*args, u32);
- u32 show_dev_instance = ~0;
- vhost_user_main_t *vum = &vhost_user_main;
-
- if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
- show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
-
- if (show_dev_instance != ~0)
- i = show_dev_instance;
-
- s = format (s, "VirtualEthernet0/0/%d", i);
- return s;
-}
-
-static int
-vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
-{
- // FIXME: check if the new dev instance is already used
- vhost_user_main_t *vum = &vhost_user_main;
- vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
- hi->dev_instance, ~0);
-
- vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
- new_dev_instance;
-
- DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
- hi->dev_instance, new_dev_instance);
-
- return 0;
-}
-
-static_always_inline void *
-map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint)
-{
- int i = *hint;
- if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) &&
- ((vui->regions[i].guest_phys_addr +
- vui->regions[i].memory_size) > addr)))
- {
- return (void *) (vui->region_mmap_addr[i] + addr -
- vui->regions[i].guest_phys_addr);
- }
-#if __SSE4_2__
- __m128i rl, rh, al, ah, r;
- al = _mm_set1_epi64x (addr + 1);
- ah = _mm_set1_epi64x (addr);
-
- rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]);
- rl = _mm_cmpgt_epi64 (al, rl);
- rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]);
- rh = _mm_cmpgt_epi64 (rh, ah);
- r = _mm_and_si128 (rl, rh);
-
- rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]);
- rl = _mm_cmpgt_epi64 (al, rl);
- rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]);
- rh = _mm_cmpgt_epi64 (rh, ah);
- r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22);
-
- rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]);
- rl = _mm_cmpgt_epi64 (al, rl);
- rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]);
- rh = _mm_cmpgt_epi64 (rh, ah);
- r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44);
-
- rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]);
- rl = _mm_cmpgt_epi64 (al, rl);
- rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]);
- rh = _mm_cmpgt_epi64 (rh, ah);
- r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
-
- r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
- i = count_trailing_zeros (_mm_movemask_epi8 (r) |
- (1 << VHOST_MEMORY_MAX_NREGIONS));
-
- if (i < vui->nregions)
- {
- *hint = i;
- return (void *) (vui->region_mmap_addr[i] + addr -
- vui->regions[i].guest_phys_addr);
- }
-#elif __aarch64__ && __ARM_NEON
- uint64x2_t al, ah, rl, rh, r;
- uint32_t u32 = 0;
-
- al = vdupq_n_u64 (addr + 1);
- ah = vdupq_n_u64 (addr);
-
- /*First Iteration */
- rl = vld1q_u64 (&vui->region_guest_addr_lo[0]);
- rl = vcgtq_u64 (al, rl);
- rh = vld1q_u64 (&vui->region_guest_addr_hi[0]);
- rh = vcgtq_u64 (rh, ah);
- r = vandq_u64 (rl, rh);
- u32 |= (vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1);
- u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 1);
-
- if (u32)
- {
- i = count_trailing_zeros (u32);
- goto vhost_map_guest_mem_done;
- }
-
- /*Second Iteration */
- rl = vld1q_u64 (&vui->region_guest_addr_lo[2]);
- rl = vcgtq_u64 (al, rl);
- rh = vld1q_u64 (&vui->region_guest_addr_hi[2]);
- rh = vcgtq_u64 (rh, ah);
- r = vandq_u64 (rl, rh);
- u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 2);
- u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 3);
-
- if (u32)
- {
- i = count_trailing_zeros (u32);
- goto vhost_map_guest_mem_done;
- }
-
- /*Third Iteration */
- rl = vld1q_u64 (&vui->region_guest_addr_lo[4]);
- rl = vcgtq_u64 (al, rl);
- rh = vld1q_u64 (&vui->region_guest_addr_hi[4]);
- rh = vcgtq_u64 (rh, ah);
- r = vandq_u64 (rl, rh);
- u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 4);
- u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 5);
-
- if (u32)
- {
- i = count_trailing_zeros (u32);
- goto vhost_map_guest_mem_done;
- }
-
- /*Fourth Iteration */
- rl = vld1q_u64 (&vui->region_guest_addr_lo[6]);
- rl = vcgtq_u64 (al, rl);
- rh = vld1q_u64 (&vui->region_guest_addr_hi[6]);
- rh = vcgtq_u64 (rh, ah);
- r = vandq_u64 (rl, rh);
- u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 6);
- u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 7);
-
- i = count_trailing_zeros (u32 | (1 << VHOST_MEMORY_MAX_NREGIONS));
-
-vhost_map_guest_mem_done:
- if (i < vui->nregions)
- {
- *hint = i;
- return (void *) (vui->region_mmap_addr[i] + addr -
- vui->regions[i].guest_phys_addr);
- }
-#else
- for (i = 0; i < vui->nregions; i++)
- {
- if ((vui->regions[i].guest_phys_addr <= addr) &&
- ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
- addr))
- {
- *hint = i;
- return (void *) (vui->region_mmap_addr[i] + addr -
- vui->regions[i].guest_phys_addr);
- }
- }
-#endif
- DBG_VQ ("failed to map guest mem addr %llx", addr);
- *hint = 0;
- return 0;
-}
-
-static inline void *
-map_user_mem (vhost_user_intf_t * vui, uword addr)
-{
- int i;
- for (i = 0; i < vui->nregions; i++)
- {
- if ((vui->regions[i].userspace_addr <= addr) &&
- ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
- addr))
- {
- return (void *) (vui->region_mmap_addr[i] + addr -
- vui->regions[i].userspace_addr);
- }
- }
- return 0;
-}
-
static long
get_huge_page_size (int fd)
{
@@ -510,7 +210,7 @@ vhost_user_rx_thread_placement ()
}
/** @brief Returns whether at least one TX and one RX vring are enabled */
-int
+static_always_inline int
vhost_user_intf_ready (vhost_user_intf_t * vui)
{
int i, found[2] = { }; //RX + TX
@@ -592,36 +292,7 @@ vhost_user_kickfd_read_ready (clib_file_t * uf)
return 0;
}
-/**
- * @brief Try once to lock the vring
- * @return 0 on success, non-zero on failure.
- */
-static inline int
-vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
-{
- return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
-}
-
-/**
- * @brief Spin until the vring is successfully locked
- */
-static inline void
-vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
-{
- while (vhost_user_vring_try_lock (vui, qid))
- ;
-}
-
-/**
- * @brief Unlock the vring lock
- */
-static inline void
-vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
-{
- *vui->vring_locks[qid] = 0;
-}
-
-static inline void
+static_always_inline void
vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
{
vhost_user_vring_t *vring = &vui->vrings[qid];
@@ -643,7 +314,7 @@ vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
vring->enabled = 1;
}
-static inline void
+static_always_inline void
vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
{
vhost_user_vring_t *vring = &vui->vrings[qid];
@@ -669,7 +340,7 @@ vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
vhost_user_vring_init (vui, qid);
}
-static inline void
+static_always_inline void
vhost_user_if_disconnect (vhost_user_intf_t * vui)
{
vnet_main_t *vnm = vnet_get_main ();
@@ -692,47 +363,6 @@ vhost_user_if_disconnect (vhost_user_intf_t * vui)
DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
}
-#define VHOST_LOG_PAGE 0x1000
-static_always_inline void
-vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
- u64 addr, u64 len, u8 is_host_address)
-{
- if (PREDICT_TRUE (vui->log_base_addr == 0
- || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
- {
- return;
- }
- if (is_host_address)
- {
- addr = pointer_to_uword (map_user_mem (vui, (uword) addr));
- }
- if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
- {
- DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
- return;
- }
-
- CLIB_MEMORY_BARRIER ();
- u64 page = addr / VHOST_LOG_PAGE;
- while (page * VHOST_LOG_PAGE < addr + len)
- {
- ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
- page++;
- }
-}
-
-static_always_inline void
-vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
-{
- vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
-}
-
-#define vhost_user_log_dirty_ring(vui, vq, member) \
- if (PREDICT_FALSE(vq->log_used)) { \
- vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
- sizeof(vq->used->member)); \
- }
-
static clib_error_t *
vhost_user_socket_read (clib_file_t * uf)
{
@@ -1333,1056 +963,6 @@ vhost_user_init (vlib_main_t * vm)
VLIB_INIT_FUNCTION (vhost_user_init);
-static u8 *
-format_vhost_trace (u8 * s, va_list * va)
-{
- CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
- CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
- CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
- vhost_user_main_t *vum = &vhost_user_main;
- vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
- vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
- t->device_index);
-
- vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
-
- u32 indent = format_get_indent (s);
-
- s = format (s, "%U %U queue %d\n", format_white_space, indent,
- format_vnet_sw_interface_name, vnm, sw, t->qid);
-
- s = format (s, "%U virtio flags:\n", format_white_space, indent);
-#define _(n,i,st) \
- if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
- s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
- foreach_virtio_trace_flags
-#undef _
- s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
- format_white_space, indent, t->first_desc_len);
-
- s = format (s, "%U flags 0x%02x gso_type %u\n",
- format_white_space, indent,
- t->hdr.hdr.flags, t->hdr.hdr.gso_type);
-
- if (vui->virtio_net_hdr_sz == 12)
- s = format (s, "%U num_buff %u",
- format_white_space, indent, t->hdr.num_buffers);
-
- return s;
-}
-
-void
-vhost_user_rx_trace (vhost_trace_t * t,
- vhost_user_intf_t * vui, u16 qid,
- vlib_buffer_t * b, vhost_user_vring_t * txvq)
-{
- vhost_user_main_t *vum = &vhost_user_main;
- u32 last_avail_idx = txvq->last_avail_idx;
- u32 desc_current = txvq->avail->ring[last_avail_idx & txvq->qsz_mask];
- vring_desc_t *hdr_desc = 0;
- virtio_net_hdr_mrg_rxbuf_t *hdr;
- u32 hint = 0;
-
- memset (t, 0, sizeof (*t));
- t->device_index = vui - vum->vhost_user_interfaces;
- t->qid = qid;
-
- hdr_desc = &txvq->desc[desc_current];
- if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
- {
- t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
- /* Header is the first here */
- hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
- }
- if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
- {
- t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
- }
- if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
- !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
- {
- t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
- }
-
- t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
-
- if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
- {
- t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
- }
- else
- {
- u32 len = vui->virtio_net_hdr_sz;
- memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
- }
-}
-
-static inline void
-vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
-{
- vhost_user_main_t *vum = &vhost_user_main;
- u64 x = 1;
- int fd = UNIX_GET_FD (vq->callfd_idx);
- int rv;
-
- rv = write (fd, &x, sizeof (x));
- if (rv <= 0)
- {
- clib_unix_warning
- ("Error: Could not write to unix socket for callfd %d", fd);
- return;
- }
-
- vq->n_since_last_int = 0;
- vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
-}
-
-static_always_inline u32
-vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
- u16 copy_len, u32 * map_hint)
-{
- void *src0, *src1, *src2, *src3;
- if (PREDICT_TRUE (copy_len >= 4))
- {
- if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
- return 1;
- if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
- return 1;
-
- while (PREDICT_TRUE (copy_len >= 4))
- {
- src0 = src2;
- src1 = src3;
-
- if (PREDICT_FALSE
- (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
- return 1;
- if (PREDICT_FALSE
- (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
- return 1;
-
- CLIB_PREFETCH (src2, 64, LOAD);
- CLIB_PREFETCH (src3, 64, LOAD);
-
- clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
- clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
- copy_len -= 2;
- cpy += 2;
- }
- }
- while (copy_len)
- {
- if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
- return 1;
- clib_memcpy ((void *) cpy->dst, src0, cpy->len);
- copy_len -= 1;
- cpy += 1;
- }
- return 0;
-}
-
-/**
- * Try to discard packets from the tx ring (VPP RX path).
- * Returns the number of discarded packets.
- */
-u32
-vhost_user_rx_discard_packet (vlib_main_t * vm,
- vhost_user_intf_t * vui,
- vhost_user_vring_t * txvq, u32 discard_max)
-{
- /*
- * On the RX side, each packet corresponds to one descriptor
- * (it is the same whether it is a shallow descriptor, chained, or indirect).
- * Therefore, discarding a packet is like discarding a descriptor.
- */
- u32 discarded_packets = 0;
- u32 avail_idx = txvq->avail->idx;
- while (discarded_packets != discard_max)
- {
- if (avail_idx == txvq->last_avail_idx)
- goto out;
-
- u16 desc_chain_head =
- txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask];
- txvq->last_avail_idx++;
- txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id =
- desc_chain_head;
- txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0;
- vhost_user_log_dirty_ring (vui, txvq,
- ring[txvq->last_used_idx & txvq->qsz_mask]);
- txvq->last_used_idx++;
- discarded_packets++;
- }
-
-out:
- CLIB_MEMORY_BARRIER ();
- txvq->used->idx = txvq->last_used_idx;
- vhost_user_log_dirty_ring (vui, txvq, idx);
- return discarded_packets;
-}
-
-/*
- * In case of overflow, we need to rewind the array of allocated buffers.
- */
-static void
-vhost_user_input_rewind_buffers (vlib_main_t * vm,
- vhost_cpu_t * cpu, vlib_buffer_t * b_head)
-{
- u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
- vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
- b_current->current_length = 0;
- b_current->flags = 0;
- while (b_current != b_head)
- {
- cpu->rx_buffers_len++;
- bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
- b_current = vlib_get_buffer (vm, bi_current);
- b_current->current_length = 0;
- b_current->flags = 0;
- }
- cpu->rx_buffers_len++;
-}
-
-static u32
-vhost_user_if_input (vlib_main_t * vm,
- vhost_user_main_t * vum,
- vhost_user_intf_t * vui,
- u16 qid, vlib_node_runtime_t * node,
- vnet_hw_interface_rx_mode mode)
-{
- vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
- u16 n_rx_packets = 0;
- u32 n_rx_bytes = 0;
- u16 n_left;
- u32 n_left_to_next, *to_next;
- u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
- u32 n_trace = vlib_get_trace_count (vm, node);
- u32 map_hint = 0;
- u16 thread_index = vlib_get_thread_index ();
- u16 copy_len = 0;
-
- {
- /* do we have pending interrupts ? */
- vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
- f64 now = vlib_time_now (vm);
-
- if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
- vhost_user_send_call (vm, txvq);
-
- if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
- vhost_user_send_call (vm, rxvq);
- }
-
- /*
- * For adaptive mode, it is optimized to reduce interrupts.
- * If the scheduler switches the input node to polling due
- * to burst of traffic, we tell the driver no interrupt.
- * When the traffic subsides, the scheduler switches the node back to
- * interrupt mode. We must tell the driver we want interrupt.
- */
- if (PREDICT_FALSE (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
- {
- if ((node->flags &
- VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) ||
- !(node->flags &
- VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
- /* Tell driver we want notification */
- txvq->used->flags = 0;
- else
- /* Tell driver we don't want notification */
- txvq->used->flags = VRING_USED_F_NO_NOTIFY;
- }
-
- if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
- return 0;
-
- n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
-
- /* nothing to do */
- if (PREDICT_FALSE (n_left == 0))
- return 0;
-
- if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
- {
- /*
- * Discard input packet if interface is admin down or vring is not
- * enabled.
- * "For example, for a networking device, in the disabled state
- * client must not supply any new RX packets, but must process
- * and discard any TX packets."
- */
- vhost_user_rx_discard_packet (vm, vui, txvq,
- VHOST_USER_DOWN_DISCARD_COUNT);
- return 0;
- }
-
- if (PREDICT_FALSE (n_left == (txvq->qsz_mask + 1)))
- {
- /*
- * Informational error logging when VPP is not
- * receiving packets fast enough.
- */
- vlib_error_count (vm, node->node_index,
- VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
- }
-
- if (n_left > VLIB_FRAME_SIZE)
- n_left = VLIB_FRAME_SIZE;
-
- /*
- * For small packets (<2kB), we will not need more than one vlib buffer
- * per packet. In case packets are bigger, we will just yeld at some point
- * in the loop and come back later. This is not an issue as for big packet,
- * processing cost really comes from the memory copy.
- * The assumption is that big packets will fit in 40 buffers.
- */
- if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len < n_left + 1 ||
- vum->cpus[thread_index].rx_buffers_len < 40))
- {
- u32 curr_len = vum->cpus[thread_index].rx_buffers_len;
- vum->cpus[thread_index].rx_buffers_len +=
- vlib_buffer_alloc_from_free_list (vm,
- vum->cpus[thread_index].rx_buffers +
- curr_len,
- VHOST_USER_RX_BUFFERS_N - curr_len,
- VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
-
- if (PREDICT_FALSE
- (vum->cpus[thread_index].rx_buffers_len <
- VHOST_USER_RX_BUFFER_STARVATION))
- {
- /* In case of buffer starvation, discard some packets from the queue
- * and log the event.
- * We keep doing best effort for the remaining packets. */
- u32 flush = (n_left + 1 > vum->cpus[thread_index].rx_buffers_len) ?
- n_left + 1 - vum->cpus[thread_index].rx_buffers_len : 1;
- flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
-
- n_left -= flush;
- vlib_increment_simple_counter (vnet_main.
- interface_main.sw_if_counters +
- VNET_INTERFACE_COUNTER_DROP,
- vlib_get_thread_index (),
- vui->sw_if_index, flush);
-
- vlib_error_count (vm, vhost_user_input_node.index,
- VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
- }
- }
-
- while (n_left > 0)
- {
- vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
-
- while (n_left > 0 && n_left_to_next > 0)
- {
- vlib_buffer_t *b_head, *b_current;
- u32 bi_current;
- u16 desc_current;
- u32 desc_data_offset;
- vring_desc_t *desc_table = txvq->desc;
-
- if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len <= 1))
- {
- /* Not enough rx_buffers
- * Note: We yeld on 1 so we don't need to do an additional
- * check for the next buffer prefetch.
- */
- n_left = 0;
- break;
- }
-
- desc_current =
- txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask];
- vum->cpus[thread_index].rx_buffers_len--;
- bi_current = (vum->cpus[thread_index].rx_buffers)
- [vum->cpus[thread_index].rx_buffers_len];
- b_head = b_current = vlib_get_buffer (vm, bi_current);
- to_next[0] = bi_current; //We do that now so we can forget about bi_current
- to_next++;
- n_left_to_next--;
-
- vlib_prefetch_buffer_with_index (vm,
- (vum->
- cpus[thread_index].rx_buffers)
- [vum->cpus[thread_index].
- rx_buffers_len - 1], LOAD);
-
- /* Just preset the used descriptor id and length for later */
- txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id =
- desc_current;
- txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0;
- vhost_user_log_dirty_ring (vui, txvq,
- ring[txvq->last_used_idx &
- txvq->qsz_mask]);
-
- /* The buffer should already be initialized */
- b_head->total_length_not_including_first_buffer = 0;
- b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
-
- if (PREDICT_FALSE (n_trace))
- {
- //TODO: next_index is not exactly known at that point
- vlib_trace_buffer (vm, node, next_index, b_head,
- /* follow_chain */ 0);
- vhost_trace_t *t0 =
- vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
- vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
- n_trace--;
- vlib_set_trace_count (vm, node, n_trace);
- }
-
- /* This depends on the setup but is very consistent
- * So I think the CPU branch predictor will make a pretty good job
- * at optimizing the decision. */
- if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
- {
- desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
- &map_hint);
- desc_current = 0;
- if (PREDICT_FALSE (desc_table == 0))
- {
- vlib_error_count (vm, node->node_index,
- VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
- goto out;
- }
- }
-
- if (PREDICT_TRUE (vui->is_any_layout) ||
- (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
- {
- /* ANYLAYOUT or single buffer */
- desc_data_offset = vui->virtio_net_hdr_sz;
- }
- else
- {
- /* CSR case without ANYLAYOUT, skip 1st buffer */
- desc_data_offset = desc_table[desc_current].len;
- }
-
- while (1)
- {
- /* Get more input if necessary. Or end of packet. */
- if (desc_data_offset == desc_table[desc_current].len)
- {
- if (PREDICT_FALSE (desc_table[desc_current].flags &
- VIRTQ_DESC_F_NEXT))
- {
- desc_current = desc_table[desc_current].next;
- desc_data_offset = 0;
- }
- else
- {
- goto out;
- }
- }
-
- /* Get more output if necessary. Or end of packet. */
- if (PREDICT_FALSE
- (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
- {
- if (PREDICT_FALSE
- (vum->cpus[thread_index].rx_buffers_len == 0))
- {
- /* Cancel speculation */
- to_next--;
- n_left_to_next++;
-
- /*
- * Checking if there are some left buffers.
- * If not, just rewind the used buffers and stop.
- * Note: Scheduled copies are not cancelled. This is
- * not an issue as they would still be valid. Useless,
- * but valid.
- */
- vhost_user_input_rewind_buffers (vm,
- &vum->cpus
- [thread_index],
- b_head);
- n_left = 0;
- goto stop;
- }
-
- /* Get next output */
- vum->cpus[thread_index].rx_buffers_len--;
- u32 bi_next =
- (vum->cpus[thread_index].rx_buffers)[vum->cpus
- [thread_index].rx_buffers_len];
- b_current->next_buffer = bi_next;
- b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
- bi_current = bi_next;
- b_current = vlib_get_buffer (vm, bi_current);
- }
-
- /* Prepare a copy order executed later for the data */
- vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
- copy_len++;
- u32 desc_data_l =
- desc_table[desc_current].len - desc_data_offset;
- cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
- cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
- cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
- b_current->current_length);
- cpy->src = desc_table[desc_current].addr + desc_data_offset;
-
- desc_data_offset += cpy->len;
-
- b_current->current_length += cpy->len;
- b_head->total_length_not_including_first_buffer += cpy->len;
- }
-
- out:
- CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
-
- n_rx_bytes += b_head->total_length_not_including_first_buffer;
- n_rx_packets++;
-
- b_head->total_length_not_including_first_buffer -=
- b_head->current_length;
-
- /* consume the descriptor and return it as used */
- txvq->last_avail_idx++;
- txvq->last_used_idx++;
-
- VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
-
- vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
- vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
- b_head->error = 0;
-
- {
- u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
-
- /* redirect if feature path enabled */
- vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
- b_head);
-
- u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
- vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
- to_next, n_left_to_next,
- bi, next0);
- }
-
- n_left--;
-
- /*
- * Although separating memory copies from virtio ring parsing
- * is beneficial, we can offer to perform the copies from time
- * to time in order to free some space in the ring.
- */
- if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
- {
- if (PREDICT_FALSE
- (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
- copy_len, &map_hint)))
- {
- vlib_error_count (vm, node->node_index,
- VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
- }
- copy_len = 0;
-
- /* give buffers back to driver */
- CLIB_MEMORY_BARRIER ();
- txvq->used->idx = txvq->last_used_idx;
- vhost_user_log_dirty_ring (vui, txvq, idx);
- }
- }
- stop:
- vlib_put_next_frame (vm, node, next_index, n_left_to_next);
- }
-
- /* Do the memory copies */
- if (PREDICT_FALSE
- (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
- copy_len, &map_hint)))
- {
- vlib_error_count (vm, node->node_index,
- VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
- }
-
- /* give buffers back to driver */
- CLIB_MEMORY_BARRIER ();
- txvq->used->idx = txvq->last_used_idx;
- vhost_user_log_dirty_ring (vui, txvq, idx);
-
- /* interrupt (call) handling */
- if ((txvq->callfd_idx != ~0) &&
- !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
- {
- txvq->n_since_last_int += n_rx_packets;
-
- if (txvq->n_since_last_int > vum->coalesce_frames)
- vhost_user_send_call (vm, txvq);
- }
-
- /* increase rx counters */
- vlib_increment_combined_counter
- (vnet_main.interface_main.combined_sw_if_counters
- + VNET_INTERFACE_COUNTER_RX,
- vlib_get_thread_index (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
-
- vnet_device_increment_rx_packets (thread_index, n_rx_packets);
-
- return n_rx_packets;
-}
-
-static uword
-vhost_user_input (vlib_main_t * vm,
- vlib_node_runtime_t * node, vlib_frame_t * f)
-{
- vhost_user_main_t *vum = &vhost_user_main;
- uword n_rx_packets = 0;
- vhost_user_intf_t *vui;
- vnet_device_input_runtime_t *rt =
- (vnet_device_input_runtime_t *) node->runtime_data;
- vnet_device_and_queue_t *dq;
-
- vec_foreach (dq, rt->devices_and_queues)
- {
- if (clib_smp_swap (&dq->interrupt_pending, 0) ||
- (node->state == VLIB_NODE_STATE_POLLING))
- {
- vui =
- pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance);
- n_rx_packets = vhost_user_if_input (vm, vum, vui, dq->queue_id, node,
- dq->mode);
- }
- }
-
- return n_rx_packets;
-}
-
-/* *INDENT-OFF* */
-VLIB_REGISTER_NODE (vhost_user_input_node) = {
- .function = vhost_user_input,
- .type = VLIB_NODE_TYPE_INPUT,
- .name = "vhost-user-input",
- .sibling_of = "device-input",
-
- /* Will be enabled if/when hardware is detected. */
- .state = VLIB_NODE_STATE_DISABLED,
-
- .format_buffer = format_ethernet_header_with_length,
- .format_trace = format_vhost_trace,
-
- .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
- .error_strings = vhost_user_input_func_error_strings,
-};
-
-VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
-/* *INDENT-ON* */
-
-
-void
-vhost_user_tx_trace (vhost_trace_t * t,
- vhost_user_intf_t * vui, u16 qid,
- vlib_buffer_t * b, vhost_user_vring_t * rxvq)
-{
- vhost_user_main_t *vum = &vhost_user_main;
- u32 last_avail_idx = rxvq->last_avail_idx;
- u32 desc_current = rxvq->avail->ring[last_avail_idx & rxvq->qsz_mask];
- vring_desc_t *hdr_desc = 0;
- u32 hint = 0;
-
- memset (t, 0, sizeof (*t));
- t->device_index = vui - vum->vhost_user_interfaces;
- t->qid = qid;
-
- hdr_desc = &rxvq->desc[desc_current];
- if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
- {
- t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
- /* Header is the first here */
- hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
- }
- if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
- {
- t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
- }
- if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
- !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
- {
- t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
- }
-
- t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
-}
-
-static_always_inline u32
-vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
- u16 copy_len, u32 * map_hint)
-{
- void *dst0, *dst1, *dst2, *dst3;
- if (PREDICT_TRUE (copy_len >= 4))
- {
- if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
- return 1;
- if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
- return 1;
- while (PREDICT_TRUE (copy_len >= 4))
- {
- dst0 = dst2;
- dst1 = dst3;
-
- if (PREDICT_FALSE
- (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
- return 1;
- if (PREDICT_FALSE
- (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
- return 1;
-
- CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
- CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
-
- clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
- clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
-
- vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
- vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
- copy_len -= 2;
- cpy += 2;
- }
- }
- while (copy_len)
- {
- if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
- return 1;
- clib_memcpy (dst0, (void *) cpy->src, cpy->len);
- vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
- copy_len -= 1;
- cpy += 1;
- }
- return 0;
-}
-
-
-static uword
-vhost_user_tx (vlib_main_t * vm,
- vlib_node_runtime_t * node, vlib_frame_t * frame)
-{
- u32 *buffers = vlib_frame_args (frame);
- u32 n_left = frame->n_vectors;
- vhost_user_main_t *vum = &vhost_user_main;
- vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
- vhost_user_intf_t *vui =
- pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
- u32 qid = ~0;
- vhost_user_vring_t *rxvq;
- u8 error;
- u32 thread_index = vlib_get_thread_index ();
- u32 map_hint = 0;
- u8 retry = 8;
- u16 copy_len;
- u16 tx_headers_len;
-
- if (PREDICT_FALSE (!vui->admin_up))
- {
- error = VHOST_USER_TX_FUNC_ERROR_DOWN;
- goto done3;
- }
-
- if (PREDICT_FALSE (!vui->is_up))
- {
- error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
- goto done3;
- }
-
- qid =
- VHOST_VRING_IDX_RX (*vec_elt_at_index
- (vui->per_cpu_tx_qid, thread_index));
- rxvq = &vui->vrings[qid];
- if (PREDICT_FALSE (vui->use_tx_spinlock))
- vhost_user_vring_lock (vui, qid);
-
-retry:
- error = VHOST_USER_TX_FUNC_ERROR_NONE;
- tx_headers_len = 0;
- copy_len = 0;
- while (n_left > 0)
- {
- vlib_buffer_t *b0, *current_b0;
- u16 desc_head, desc_index, desc_len;
- vring_desc_t *desc_table;
- uword buffer_map_addr;
- u32 buffer_len;
- u16 bytes_left;
-
- if (PREDICT_TRUE (n_left > 1))
- vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
-
- b0 = vlib_get_buffer (vm, buffers[0]);
-
- if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
- {
- vum->cpus[thread_index].current_trace =
- vlib_add_trace (vm, node, b0,
- sizeof (*vum->cpus[thread_index].current_trace));
- vhost_user_tx_trace (vum->cpus[thread_index].current_trace,
- vui, qid / 2, b0, rxvq);
- }
-
- if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
- {
- error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
- goto done;
- }
-
- desc_table = rxvq->desc;
- desc_head = desc_index =
- rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
-
- /* Go deeper in case of indirect descriptor
- * I don't know of any driver providing indirect for RX. */
- if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
- {
- if (PREDICT_FALSE
- (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
- {
- error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
- goto done;
- }
- if (PREDICT_FALSE
- (!(desc_table =
- map_guest_mem (vui, rxvq->desc[desc_index].addr,
- &map_hint))))
- {
- error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
- goto done;
- }
- desc_index = 0;
- }
-
- desc_len = vui->virtio_net_hdr_sz;
- buffer_map_addr = desc_table[desc_index].addr;
- buffer_len = desc_table[desc_index].len;
-
- {
- // Get a header from the header array
- virtio_net_hdr_mrg_rxbuf_t *hdr =
- &vum->cpus[thread_index].tx_headers[tx_headers_len];
- tx_headers_len++;
- hdr->hdr.flags = 0;
- hdr->hdr.gso_type = 0;
- hdr->num_buffers = 1; //This is local, no need to check
-
- // Prepare a copy order executed later for the header
- vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
- copy_len++;
- cpy->len = vui->virtio_net_hdr_sz;
- cpy->dst = buffer_map_addr;
- cpy->src = (uword) hdr;
- }
-
- buffer_map_addr += vui->virtio_net_hdr_sz;
- buffer_len -= vui->virtio_net_hdr_sz;
- bytes_left = b0->current_length;
- current_b0 = b0;
- while (1)
- {
- if (buffer_len == 0)
- { //Get new output
- if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
- {
- //Next one is chained
- desc_index = desc_table[desc_index].next;
- buffer_map_addr = desc_table[desc_index].addr;
- buffer_len = desc_table[desc_index].len;
- }
- else if (vui->virtio_net_hdr_sz == 12) //MRG is available
- {
- virtio_net_hdr_mrg_rxbuf_t *hdr =
- &vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
-
- //Move from available to used buffer
- rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id =
- desc_head;
- rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len =
- desc_len;
- vhost_user_log_dirty_ring (vui, rxvq,
- ring[rxvq->last_used_idx &
- rxvq->qsz_mask]);
-
- rxvq->last_avail_idx++;
- rxvq->last_used_idx++;
- hdr->num_buffers++;
- desc_len = 0;
-
- if (PREDICT_FALSE
- (rxvq->last_avail_idx == rxvq->avail->idx))
- {
- //Dequeue queued descriptors for this packet
- rxvq->last_used_idx -= hdr->num_buffers - 1;
- rxvq->last_avail_idx -= hdr->num_buffers - 1;
- error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
- goto done;
- }
-
- desc_table = rxvq->desc;
- desc_head = desc_index =
- rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
- if (PREDICT_FALSE
- (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
- {
- //It is seriously unlikely that a driver will put indirect descriptor
- //after non-indirect descriptor.
- if (PREDICT_FALSE
- (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
- {
- error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
- goto done;
- }
- if (PREDICT_FALSE
- (!(desc_table =
- map_guest_mem (vui,
- rxvq->desc[desc_index].addr,
- &map_hint))))
- {
- error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
- goto done;
- }
- desc_index = 0;
- }
- buffer_map_addr = desc_table[desc_index].addr;
- buffer_len = desc_table[desc_index].len;
- }
- else
- {
- error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
- goto done;
- }
- }
-
- {
- vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
- copy_len++;
- cpy->len = bytes_left;
- cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
- cpy->dst = buffer_map_addr;
- cpy->src = (uword) vlib_buffer_get_current (current_b0) +
- current_b0->current_length - bytes_left;
-
- bytes_left -= cpy->len;
- buffer_len -= cpy->len;
- buffer_map_addr += cpy->len;
- desc_len += cpy->len;
-
- CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
- }
-
- // Check if vlib buffer has more data. If not, get more or break.
- if (PREDICT_TRUE (!bytes_left))
- {
- if (PREDICT_FALSE
- (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
- {
- current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
- bytes_left = current_b0->current_length;
- }
- else
- {
- //End of packet
- break;
- }
- }
- }
-
- //Move from available to used ring
- rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = desc_head;
- rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = desc_len;
- vhost_user_log_dirty_ring (vui, rxvq,
- ring[rxvq->last_used_idx & rxvq->qsz_mask]);
- rxvq->last_avail_idx++;
- rxvq->last_used_idx++;
-
- if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
- {
- vum->cpus[thread_index].current_trace->hdr =
- vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
- }
-
- n_left--; //At the end for error counting when 'goto done' is invoked
-
- /*
- * Do the copy periodically to prevent
- * vum->cpus[thread_index].copy array overflow and corrupt memory
- */
- if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD))
- {
- if (PREDICT_FALSE
- (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
- copy_len, &map_hint)))
- {
- vlib_error_count (vm, node->node_index,
- VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
- }
- copy_len = 0;
-
- /* give buffers back to driver */
- CLIB_MEMORY_BARRIER ();
- rxvq->used->idx = rxvq->last_used_idx;
- vhost_user_log_dirty_ring (vui, rxvq, idx);
- }
- buffers++;
- }
-
-done:
- //Do the memory copies
- if (PREDICT_FALSE
- (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
- copy_len, &map_hint)))
- {
- vlib_error_count (vm, node->node_index,
- VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
- }
-
- CLIB_MEMORY_BARRIER ();
- rxvq->used->idx = rxvq->last_used_idx;
- vhost_user_log_dirty_ring (vui, rxvq, idx);
-
- /*
- * When n_left is set, error is always set to something too.
- * In case error is due to lack of remaining buffers, we go back up and
- * retry.
- * The idea is that it is better to waste some time on packets
- * that have been processed already than dropping them and get
- * more fresh packets with a good likelyhood that they will be dropped too.
- * This technique also gives more time to VM driver to pick-up packets.
- * In case the traffic flows from physical to virtual interfaces, this
- * technique will end-up leveraging the physical NIC buffer in order to
- * absorb the VM's CPU jitter.
- */
- if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
- {
- retry--;
- goto retry;
- }
-
- /* interrupt (call) handling */
- if ((rxvq->callfd_idx != ~0) &&
- !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
- {
- rxvq->n_since_last_int += frame->n_vectors - n_left;
-
- if (rxvq->n_since_last_int > vum->coalesce_frames)
- vhost_user_send_call (vm, rxvq);
- }
-
- vhost_user_vring_unlock (vui, qid);
-
-done3:
- if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
- {
- vlib_error_count (vm, node->node_index, error, n_left);
- vlib_increment_simple_counter
- (vnet_main.interface_main.sw_if_counters
- + VNET_INTERFACE_COUNTER_DROP,
- thread_index, vui->sw_if_index, n_left);
- }
-
- vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
- return frame->n_vectors;
-}
-
static uword
vhost_user_send_interrupt_process (vlib_main_t * vm,
vlib_node_runtime_t * rt, vlib_frame_t * f)
@@ -2473,110 +1053,13 @@ vhost_user_send_interrupt_process (vlib_main_t * vm,
}
/* *INDENT-OFF* */
-VLIB_REGISTER_NODE (vhost_user_send_interrupt_node,static) = {
+VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = {
.function = vhost_user_send_interrupt_process,
.type = VLIB_NODE_TYPE_PROCESS,
.name = "vhost-user-send-interrupt-process",
};
/* *INDENT-ON* */
-static clib_error_t *
-vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index,
- u32 qid, vnet_hw_interface_rx_mode mode)
-{
- vlib_main_t *vm = vnm->vlib_main;
- vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
- vhost_user_main_t *vum = &vhost_user_main;
- vhost_user_intf_t *vui =
- pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
- vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
-
- if ((mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
- (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
- {
- if (txvq->kickfd_idx == ~0)
- {
- // We cannot support interrupt mode if the driver opts out
- return clib_error_return (0, "Driver does not support interrupt");
- }
- if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
- {
- vum->ifq_count++;
- // Start the timer if this is the first encounter on interrupt
- // interface/queue
- if ((vum->ifq_count == 1) &&
- (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
- vlib_process_signal_event (vm,
- vhost_user_send_interrupt_node.index,
- VHOST_USER_EVENT_START_TIMER, 0);
- }
- }
- else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
- {
- if (((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
- (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) &&
- vum->ifq_count)
- {
- vum->ifq_count--;
- // Stop the timer if there is no more interrupt interface/queue
- if ((vum->ifq_count == 0) &&
- (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
- vlib_process_signal_event (vm,
- vhost_user_send_interrupt_node.index,
- VHOST_USER_EVENT_STOP_TIMER, 0);
- }
- }
-
- txvq->mode = mode;
- if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
- txvq->used->flags = VRING_USED_F_NO_NOTIFY;
- else if ((mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE) ||
- (mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT))
- txvq->used->flags = 0;
- else
- {
- clib_warning ("BUG: unhandled mode %d changed for if %d queue %d", mode,
- hw_if_index, qid);
- return clib_error_return (0, "unsupported");
- }
-
- return 0;
-}
-
-static clib_error_t *
-vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
- u32 flags)
-{
- vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
- vhost_user_main_t *vum = &vhost_user_main;
- vhost_user_intf_t *vui =
- pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
- u32 hw_flags = 0;
- vui->admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
- hw_flags = vui->admin_up ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
-
- vnet_hw_interface_set_flags (vnm, vui->hw_if_index, hw_flags);
-
- return /* no error */ 0;
-}
-
-/* *INDENT-OFF* */
-VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
- .name = "vhost-user",
- .tx_function = vhost_user_tx,
- .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
- .tx_function_error_strings = vhost_user_tx_func_error_strings,
- .format_device_name = format_vhost_user_interface_name,
- .name_renumber = vhost_user_name_renumber,
- .admin_up_down_function = vhost_user_interface_admin_up_down,
- .rx_mode_change_function = vhost_user_interface_rx_mode_change,
- .format_tx_trace = format_vhost_trace,
-};
-
-VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
- vhost_user_tx)
-/* *INDENT-ON* */
-
static uword
vhost_user_process (vlib_main_t * vm,
vlib_node_runtime_t * rt, vlib_frame_t * f)
@@ -2727,7 +1210,7 @@ vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
u16 *queue;
if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
- hwif->dev_class_index != vhost_user_dev_class.index)
+ hwif->dev_class_index != vhost_user_device_class.index)
return VNET_API_ERROR_INVALID_SW_IF_INDEX;
DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
@@ -2859,7 +1342,7 @@ vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
error = ethernet_register_interface
(vnm,
- vhost_user_dev_class.index,
+ vhost_user_device_class.index,
vui - vum->vhost_user_interfaces /* device instance */ ,
hwaddr /* ethernet address */ ,
&vui->hw_if_index, 0 /* flag change */ );
@@ -3007,7 +1490,7 @@ vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
uword *if_index;
if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
- hwif->dev_class_index != vhost_user_dev_class.index)
+ hwif->dev_class_index != vhost_user_device_class.index)
return VNET_API_ERROR_INVALID_SW_IF_INDEX;
if (sock_filename == NULL || !(strlen (sock_filename) > 0))
@@ -3132,7 +1615,7 @@ vhost_user_delete_command_fn (vlib_main_t * vm,
vnet_hw_interface_t *hwif =
vnet_get_sup_hw_interface (vnm, sw_if_index);
if (hwif == NULL ||
- vhost_user_dev_class.index != hwif->dev_class_index)
+ vhost_user_device_class.index != hwif->dev_class_index)
{
error = clib_error_return (0, "Not a vhost interface");
goto done;
@@ -3251,7 +1734,7 @@ show_vhost_user_command_fn (vlib_main_t * vm,
(input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
{
hi = vnet_get_hw_interface (vnm, hw_if_index);
- if (vhost_user_dev_class.index != hi->dev_class_index)
+ if (vhost_user_device_class.index != hi->dev_class_index)
{
error = clib_error_return (0, "unknown input `%U'",
format_unformat_error, input);
@@ -3732,6 +2215,7 @@ vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
return 0;
}
+
/* vhost-user { ... } configuration. */
VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
diff --git a/src/vnet/devices/virtio/vhost-user.h b/src/vnet/devices/virtio/vhost_user.h
index 105b92b72ef..ff0669598c9 100644
--- a/src/vnet/devices/virtio/vhost-user.h
+++ b/src/vnet/devices/virtio/vhost_user.h
@@ -45,6 +45,40 @@
#define VRING_USED_F_NO_NOTIFY 1
#define VRING_AVAIL_F_NO_INTERRUPT 1
+#define DBG_SOCK(args...) \
+ { \
+ vhost_user_main_t *_vum = &vhost_user_main; \
+ if (_vum->debug) \
+ clib_warning(args); \
+ };
+
+#define VHOST_DEBUG_VQ 0
+
+#if VHOST_DEBUG_VQ == 1
+#define DBG_VQ(args...) clib_warning(args);
+#else
+#define DBG_VQ(args...)
+#endif
+
+#define UNIX_GET_FD(unixfd_idx) ({ \
+ typeof(unixfd_idx) __unixfd_idx = (unixfd_idx); \
+ (__unixfd_idx != ~0) ? \
+ pool_elt_at_index (file_main.file_pool, \
+ __unixfd_idx)->file_descriptor : -1; })
+
+#define foreach_virtio_trace_flags \
+ _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
+ _ (SINGLE_DESC, 1, "Single descriptor packet") \
+ _ (INDIRECT, 2, "Indirect descriptor") \
+ _ (MAP_ERROR, 4, "Memory mapping error")
+
+typedef enum
+{
+#define _(n,i,s) VIRTIO_TRACE_F_##n,
+ foreach_virtio_trace_flags
+#undef _
+} virtio_trace_flag_t;
+
#define foreach_virtio_net_feature \
_ (VIRTIO_NET_F_MRG_RXBUF, 15) \
_ (VIRTIO_NET_F_CTRL_VQ, 17) \
@@ -56,7 +90,6 @@
_ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \
_ (VIRTIO_F_VERSION_1, 32)
-
typedef enum
{
#define _(f,n) FEAT_##f = (n),
@@ -331,6 +364,11 @@ typedef struct
int vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
vhost_user_intf_details_t ** out_vuids);
+extern vlib_node_registration_t vhost_user_send_interrupt_node;
+extern vnet_device_class_t vhost_user_device_class;
+extern vlib_node_registration_t vhost_user_input_node;
+extern vhost_user_main_t vhost_user_main;
+
#endif
/*
diff --git a/src/vnet/devices/virtio/vhost_user_api.c b/src/vnet/devices/virtio/vhost_user_api.c
index 442d9146945..59dba252f22 100644
--- a/src/vnet/devices/virtio/vhost_user_api.c
+++ b/src/vnet/devices/virtio/vhost_user_api.c
@@ -22,7 +22,7 @@
#include <vnet/interface.h>
#include <vnet/api_errno.h>
-#include <vnet/devices/virtio/vhost-user.h>
+#include <vnet/devices/virtio/vhost_user.h>
#include <vnet/vnet_msg_enum.h>
diff --git a/src/vnet/devices/virtio/vhost_user_inline.h b/src/vnet/devices/virtio/vhost_user_inline.h
new file mode 100644
index 00000000000..9f6135165ae
--- /dev/null
+++ b/src/vnet/devices/virtio/vhost_user_inline.h
@@ -0,0 +1,262 @@
+/*
+ * Copyright (c) 2018 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef __VIRTIO_VHOST_USER_INLINE_H__
+#define __VIRTIO_VHOST_USER_INLINE_H__
+/* vhost-user inline functions */
+
+static_always_inline void *
+map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint)
+{
+ int i = *hint;
+ if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) &&
+ ((vui->regions[i].guest_phys_addr +
+ vui->regions[i].memory_size) > addr)))
+ {
+ return (void *) (vui->region_mmap_addr[i] + addr -
+ vui->regions[i].guest_phys_addr);
+ }
+#if __SSE4_2__
+ __m128i rl, rh, al, ah, r;
+ al = _mm_set1_epi64x (addr + 1);
+ ah = _mm_set1_epi64x (addr);
+
+ rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]);
+ rl = _mm_cmpgt_epi64 (al, rl);
+ rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]);
+ rh = _mm_cmpgt_epi64 (rh, ah);
+ r = _mm_and_si128 (rl, rh);
+
+ rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]);
+ rl = _mm_cmpgt_epi64 (al, rl);
+ rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]);
+ rh = _mm_cmpgt_epi64 (rh, ah);
+ r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22);
+
+ rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]);
+ rl = _mm_cmpgt_epi64 (al, rl);
+ rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]);
+ rh = _mm_cmpgt_epi64 (rh, ah);
+ r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44);
+
+ rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]);
+ rl = _mm_cmpgt_epi64 (al, rl);
+ rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]);
+ rh = _mm_cmpgt_epi64 (rh, ah);
+ r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
+
+ r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
+ i = count_trailing_zeros (_mm_movemask_epi8 (r) |
+ (1 << VHOST_MEMORY_MAX_NREGIONS));
+
+ if (i < vui->nregions)
+ {
+ *hint = i;
+ return (void *) (vui->region_mmap_addr[i] + addr -
+ vui->regions[i].guest_phys_addr);
+ }
+#elif __aarch64__ && __ARM_NEON
+ uint64x2_t al, ah, rl, rh, r;
+ uint32_t u32 = 0;
+
+ al = vdupq_n_u64 (addr + 1);
+ ah = vdupq_n_u64 (addr);
+
+ /*First Iteration */
+ rl = vld1q_u64 (&vui->region_guest_addr_lo[0]);
+ rl = vcgtq_u64 (al, rl);
+ rh = vld1q_u64 (&vui->region_guest_addr_hi[0]);
+ rh = vcgtq_u64 (rh, ah);
+ r = vandq_u64 (rl, rh);
+ u32 |= (vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1);
+ u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 1);
+
+ if (u32)
+ {
+ i = count_trailing_zeros (u32);
+ goto vhost_map_guest_mem_done;
+ }
+
+ /*Second Iteration */
+ rl = vld1q_u64 (&vui->region_guest_addr_lo[2]);
+ rl = vcgtq_u64 (al, rl);
+ rh = vld1q_u64 (&vui->region_guest_addr_hi[2]);
+ rh = vcgtq_u64 (rh, ah);
+ r = vandq_u64 (rl, rh);
+ u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 2);
+ u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 3);
+
+ if (u32)
+ {
+ i = count_trailing_zeros (u32);
+ goto vhost_map_guest_mem_done;
+ }
+
+ /*Third Iteration */
+ rl = vld1q_u64 (&vui->region_guest_addr_lo[4]);
+ rl = vcgtq_u64 (al, rl);
+ rh = vld1q_u64 (&vui->region_guest_addr_hi[4]);
+ rh = vcgtq_u64 (rh, ah);
+ r = vandq_u64 (rl, rh);
+ u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 6);
+ u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 7);
+
+ i = count_trailing_zeros (u32 | (1 << VHOST_MEMORY_MAX_NREGIONS));
+
+vhost_map_guest_mem_done:
+ if (i < vui->nregions)
+ {
+ *hint = i;
+ return (void *) (vui->region_mmap_addr[i] + addr -
+ vui->regions[i].guest_phys_addr);
+ }
+#else
+ for (i = 0; i < vui->nregions; i++)
+ {
+ if ((vui->regions[i].guest_phys_addr <= addr) &&
+ ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
+ addr))
+ {
+ *hint = i;
+ return (void *) (vui->region_mmap_addr[i] + addr -
+ vui->regions[i].guest_phys_addr);
+ }
+ }
+#endif
+ DBG_VQ ("failed to map guest mem addr %llx", addr);
+ *hint = 0;
+ return 0;
+}
+
+static_always_inline void *
+map_user_mem (vhost_user_intf_t * vui, uword addr)
+{
+ int i;
+ for (i = 0; i < vui->nregions; i++)
+ {
+ if ((vui->regions[i].userspace_addr <= addr) &&
+ ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
+ addr))
+ {
+ return (void *) (vui->region_mmap_addr[i] + addr -
+ vui->regions[i].userspace_addr);
+ }
+ }
+ return 0;
+}
+
+#define VHOST_LOG_PAGE 0x1000
+
+static_always_inline void
+vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
+ u64 addr, u64 len, u8 is_host_address)
+{
+ if (PREDICT_TRUE (vui->log_base_addr == 0
+ || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
+ {
+ return;
+ }
+ if (is_host_address)
+ {
+ addr = pointer_to_uword (map_user_mem (vui, (uword) addr));
+ }
+ if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
+ {
+ DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
+ return;
+ }
+
+ CLIB_MEMORY_BARRIER ();
+ u64 page = addr / VHOST_LOG_PAGE;
+ while (page * VHOST_LOG_PAGE < addr + len)
+ {
+ ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
+ page++;
+ }
+}
+
+
+#define vhost_user_log_dirty_ring(vui, vq, member) \
+ if (PREDICT_FALSE(vq->log_used)) { \
+ vhost_user_log_dirty_pages_2(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
+ sizeof(vq->used->member), 0); \
+ }
+
+static_always_inline u8 *
+format_vhost_trace (u8 * s, va_list * va)
+{
+ CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
+ CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
+ CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
+ vhost_user_main_t *vum = &vhost_user_main;
+ vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
+ vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
+ t->device_index);
+
+ vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
+
+ u32 indent = format_get_indent (s);
+
+ s = format (s, "%U %U queue %d\n", format_white_space, indent,
+ format_vnet_sw_interface_name, vnm, sw, t->qid);
+
+ s = format (s, "%U virtio flags:\n", format_white_space, indent);
+#define _(n,i,st) \
+ if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
+ s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
+ foreach_virtio_trace_flags
+#undef _
+ s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
+ format_white_space, indent, t->first_desc_len);
+
+ s = format (s, "%U flags 0x%02x gso_type %u\n",
+ format_white_space, indent,
+ t->hdr.hdr.flags, t->hdr.hdr.gso_type);
+
+ if (vui->virtio_net_hdr_sz == 12)
+ s = format (s, "%U num_buff %u",
+ format_white_space, indent, t->hdr.num_buffers);
+
+ return s;
+}
+
+static_always_inline void
+vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
+{
+ vhost_user_main_t *vum = &vhost_user_main;
+ u64 x = 1;
+ int fd = UNIX_GET_FD (vq->callfd_idx);
+ int rv;
+
+ rv = write (fd, &x, sizeof (x));
+ if (rv <= 0)
+ {
+ clib_unix_warning
+ ("Error: Could not write to unix socket for callfd %d", fd);
+ return;
+ }
+
+ vq->n_since_last_int = 0;
+ vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
+}
+
+#endif
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/devices/virtio/vhost_user_input.c b/src/vnet/devices/virtio/vhost_user_input.c
new file mode 100644
index 00000000000..3db44731b5e
--- /dev/null
+++ b/src/vnet/devices/virtio/vhost_user_input.c
@@ -0,0 +1,678 @@
+/*
+ *------------------------------------------------------------------
+ * vhost-user-input
+ *
+ * Copyright (c) 2014-2018 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <fcntl.h> /* for open */
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h> /* for iovec */
+#include <netinet/in.h>
+#include <sys/vfs.h>
+
+#include <linux/if_arp.h>
+#include <linux/if_tun.h>
+
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+
+#include <vnet/ip/ip.h>
+
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/devices/devices.h>
+#include <vnet/feature/feature.h>
+
+#include <vnet/devices/virtio/vhost_user.h>
+#include <vnet/devices/virtio/vhost_user_inline.h>
+
+/*
+ * When an RX queue is down but active, received packets
+ * must be discarded. This value controls up to how many
+ * packets will be discarded during each round.
+ */
+#define VHOST_USER_DOWN_DISCARD_COUNT 256
+
+/*
+ * When the number of available buffers gets under this threshold,
+ * RX node will start discarding packets.
+ */
+#define VHOST_USER_RX_BUFFER_STARVATION 32
+
+/*
+ * On the receive side, the host should free descriptors as soon
+ * as possible in order to avoid TX drop in the VM.
+ * This value controls the number of copy operations that are stacked
+ * before copy is done for all and descriptors are given back to
+ * the guest.
+ * The value 64 was obtained by testing (48 and 128 were not as good).
+ */
+#define VHOST_USER_RX_COPY_THRESHOLD 64
+
+vlib_node_registration_t vhost_user_input_node;
+
+#define foreach_vhost_user_input_func_error \
+ _(NO_ERROR, "no error") \
+ _(NO_BUFFER, "no available buffer") \
+ _(MMAP_FAIL, "mmap failure") \
+ _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
+ _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
+ _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
+
+typedef enum
+{
+#define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
+ foreach_vhost_user_input_func_error
+#undef _
+ VHOST_USER_INPUT_FUNC_N_ERROR,
+} vhost_user_input_func_error_t;
+
+static __clib_unused char *vhost_user_input_func_error_strings[] = {
+#define _(n,s) s,
+ foreach_vhost_user_input_func_error
+#undef _
+};
+
+static_always_inline void
+vhost_user_rx_trace (vhost_trace_t * t,
+ vhost_user_intf_t * vui, u16 qid,
+ vlib_buffer_t * b, vhost_user_vring_t * txvq)
+{
+ vhost_user_main_t *vum = &vhost_user_main;
+ u32 last_avail_idx = txvq->last_avail_idx;
+ u32 desc_current = txvq->avail->ring[last_avail_idx & txvq->qsz_mask];
+ vring_desc_t *hdr_desc = 0;
+ virtio_net_hdr_mrg_rxbuf_t *hdr;
+ u32 hint = 0;
+
+ memset (t, 0, sizeof (*t));
+ t->device_index = vui - vum->vhost_user_interfaces;
+ t->qid = qid;
+
+ hdr_desc = &txvq->desc[desc_current];
+ if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
+ {
+ t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
+ /* Header is the first here */
+ hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
+ }
+ if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
+ {
+ t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
+ }
+ if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
+ !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
+ {
+ t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
+ }
+
+ t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
+
+ if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
+ {
+ t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
+ }
+ else
+ {
+ u32 len = vui->virtio_net_hdr_sz;
+ memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
+ }
+}
+
+static_always_inline u32
+vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
+ u16 copy_len, u32 * map_hint)
+{
+ void *src0, *src1, *src2, *src3;
+ if (PREDICT_TRUE (copy_len >= 4))
+ {
+ if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
+ return 1;
+ if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
+ return 1;
+
+ while (PREDICT_TRUE (copy_len >= 4))
+ {
+ src0 = src2;
+ src1 = src3;
+
+ if (PREDICT_FALSE
+ (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
+ return 1;
+ if (PREDICT_FALSE
+ (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
+ return 1;
+
+ CLIB_PREFETCH (src2, 64, LOAD);
+ CLIB_PREFETCH (src3, 64, LOAD);
+
+ clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
+ clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
+ copy_len -= 2;
+ cpy += 2;
+ }
+ }
+ while (copy_len)
+ {
+ if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
+ return 1;
+ clib_memcpy ((void *) cpy->dst, src0, cpy->len);
+ copy_len -= 1;
+ cpy += 1;
+ }
+ return 0;
+}
+
+/**
+ * Try to discard packets from the tx ring (VPP RX path).
+ * Returns the number of discarded packets.
+ */
+static_always_inline u32
+vhost_user_rx_discard_packet (vlib_main_t * vm,
+ vhost_user_intf_t * vui,
+ vhost_user_vring_t * txvq, u32 discard_max)
+{
+ /*
+ * On the RX side, each packet corresponds to one descriptor
+ * (it is the same whether it is a shallow descriptor, chained, or indirect).
+ * Therefore, discarding a packet is like discarding a descriptor.
+ */
+ u32 discarded_packets = 0;
+ u32 avail_idx = txvq->avail->idx;
+ while (discarded_packets != discard_max)
+ {
+ if (avail_idx == txvq->last_avail_idx)
+ goto out;
+
+ u16 desc_chain_head =
+ txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask];
+ txvq->last_avail_idx++;
+ txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id =
+ desc_chain_head;
+ txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0;
+ vhost_user_log_dirty_ring (vui, txvq,
+ ring[txvq->last_used_idx & txvq->qsz_mask]);
+ txvq->last_used_idx++;
+ discarded_packets++;
+ }
+
+out:
+ CLIB_MEMORY_BARRIER ();
+ txvq->used->idx = txvq->last_used_idx;
+ vhost_user_log_dirty_ring (vui, txvq, idx);
+ return discarded_packets;
+}
+
+/*
+ * In case of overflow, we need to rewind the array of allocated buffers.
+ */
+static __clib_unused void
+vhost_user_input_rewind_buffers (vlib_main_t * vm,
+ vhost_cpu_t * cpu, vlib_buffer_t * b_head)
+{
+ u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
+ vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
+ b_current->current_length = 0;
+ b_current->flags = 0;
+ while (b_current != b_head)
+ {
+ cpu->rx_buffers_len++;
+ bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
+ b_current = vlib_get_buffer (vm, bi_current);
+ b_current->current_length = 0;
+ b_current->flags = 0;
+ }
+ cpu->rx_buffers_len++;
+}
+
+static __clib_unused u32
+vhost_user_if_input (vlib_main_t * vm,
+ vhost_user_main_t * vum,
+ vhost_user_intf_t * vui,
+ u16 qid, vlib_node_runtime_t * node,
+ vnet_hw_interface_rx_mode mode)
+{
+ vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
+ u16 n_rx_packets = 0;
+ u32 n_rx_bytes = 0;
+ u16 n_left;
+ u32 n_left_to_next, *to_next;
+ u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
+ u32 n_trace = vlib_get_trace_count (vm, node);
+ u32 map_hint = 0;
+ u16 thread_index = vlib_get_thread_index ();
+ u16 copy_len = 0;
+
+ {
+ /* do we have pending interrupts ? */
+ vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
+ f64 now = vlib_time_now (vm);
+
+ if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
+ vhost_user_send_call (vm, txvq);
+
+ if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
+ vhost_user_send_call (vm, rxvq);
+ }
+
+ /*
+ * For adaptive mode, it is optimized to reduce interrupts.
+ * If the scheduler switches the input node to polling due
+ * to burst of traffic, we tell the driver no interrupt.
+ * When the traffic subsides, the scheduler switches the node back to
+ * interrupt mode. We must tell the driver we want interrupt.
+ */
+ if (PREDICT_FALSE (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
+ {
+ if ((node->flags &
+ VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) ||
+ !(node->flags &
+ VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
+ /* Tell driver we want notification */
+ txvq->used->flags = 0;
+ else
+ /* Tell driver we don't want notification */
+ txvq->used->flags = VRING_USED_F_NO_NOTIFY;
+ }
+
+ if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
+ return 0;
+
+ n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
+
+ /* nothing to do */
+ if (PREDICT_FALSE (n_left == 0))
+ return 0;
+
+ if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
+ {
+ /*
+ * Discard input packet if interface is admin down or vring is not
+ * enabled.
+ * "For example, for a networking device, in the disabled state
+ * client must not supply any new RX packets, but must process
+ * and discard any TX packets."
+ */
+ vhost_user_rx_discard_packet (vm, vui, txvq,
+ VHOST_USER_DOWN_DISCARD_COUNT);
+ return 0;
+ }
+
+ if (PREDICT_FALSE (n_left == (txvq->qsz_mask + 1)))
+ {
+ /*
+ * Informational error logging when VPP is not
+ * receiving packets fast enough.
+ */
+ vlib_error_count (vm, node->node_index,
+ VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
+ }
+
+ if (n_left > VLIB_FRAME_SIZE)
+ n_left = VLIB_FRAME_SIZE;
+
+ /*
+ * For small packets (<2kB), we will not need more than one vlib buffer
+ * per packet. In case packets are bigger, we will just yeld at some point
+ * in the loop and come back later. This is not an issue as for big packet,
+ * processing cost really comes from the memory copy.
+ * The assumption is that big packets will fit in 40 buffers.
+ */
+ if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len < n_left + 1 ||
+ vum->cpus[thread_index].rx_buffers_len < 40))
+ {
+ u32 curr_len = vum->cpus[thread_index].rx_buffers_len;
+ vum->cpus[thread_index].rx_buffers_len +=
+ vlib_buffer_alloc_from_free_list (vm,
+ vum->cpus[thread_index].rx_buffers +
+ curr_len,
+ VHOST_USER_RX_BUFFERS_N - curr_len,
+ VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+
+ if (PREDICT_FALSE
+ (vum->cpus[thread_index].rx_buffers_len <
+ VHOST_USER_RX_BUFFER_STARVATION))
+ {
+ /* In case of buffer starvation, discard some packets from the queue
+ * and log the event.
+ * We keep doing best effort for the remaining packets. */
+ u32 flush = (n_left + 1 > vum->cpus[thread_index].rx_buffers_len) ?
+ n_left + 1 - vum->cpus[thread_index].rx_buffers_len : 1;
+ flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
+
+ n_left -= flush;
+ vlib_increment_simple_counter (vnet_main.
+ interface_main.sw_if_counters +
+ VNET_INTERFACE_COUNTER_DROP,
+ vlib_get_thread_index (),
+ vui->sw_if_index, flush);
+
+ vlib_error_count (vm, vhost_user_input_node.index,
+ VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
+ }
+ }
+
+ while (n_left > 0)
+ {
+ vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
+
+ while (n_left > 0 && n_left_to_next > 0)
+ {
+ vlib_buffer_t *b_head, *b_current;
+ u32 bi_current;
+ u16 desc_current;
+ u32 desc_data_offset;
+ vring_desc_t *desc_table = txvq->desc;
+
+ if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len <= 1))
+ {
+ /* Not enough rx_buffers
+ * Note: We yeld on 1 so we don't need to do an additional
+ * check for the next buffer prefetch.
+ */
+ n_left = 0;
+ break;
+ }
+
+ desc_current =
+ txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask];
+ vum->cpus[thread_index].rx_buffers_len--;
+ bi_current = (vum->cpus[thread_index].rx_buffers)
+ [vum->cpus[thread_index].rx_buffers_len];
+ b_head = b_current = vlib_get_buffer (vm, bi_current);
+ to_next[0] = bi_current; //We do that now so we can forget about bi_current
+ to_next++;
+ n_left_to_next--;
+
+ vlib_prefetch_buffer_with_index (vm,
+ (vum->
+ cpus[thread_index].rx_buffers)
+ [vum->cpus[thread_index].
+ rx_buffers_len - 1], LOAD);
+
+ /* Just preset the used descriptor id and length for later */
+ txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id =
+ desc_current;
+ txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0;
+ vhost_user_log_dirty_ring (vui, txvq,
+ ring[txvq->last_used_idx &
+ txvq->qsz_mask]);
+
+ /* The buffer should already be initialized */
+ b_head->total_length_not_including_first_buffer = 0;
+ b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
+
+ if (PREDICT_FALSE (n_trace))
+ {
+ //TODO: next_index is not exactly known at that point
+ vlib_trace_buffer (vm, node, next_index, b_head,
+ /* follow_chain */ 0);
+ vhost_trace_t *t0 =
+ vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
+ vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
+ n_trace--;
+ vlib_set_trace_count (vm, node, n_trace);
+ }
+
+ /* This depends on the setup but is very consistent
+ * So I think the CPU branch predictor will make a pretty good job
+ * at optimizing the decision. */
+ if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
+ {
+ desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
+ &map_hint);
+ desc_current = 0;
+ if (PREDICT_FALSE (desc_table == 0))
+ {
+ vlib_error_count (vm, node->node_index,
+ VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
+ goto out;
+ }
+ }
+
+ if (PREDICT_TRUE (vui->is_any_layout) ||
+ (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
+ {
+ /* ANYLAYOUT or single buffer */
+ desc_data_offset = vui->virtio_net_hdr_sz;
+ }
+ else
+ {
+ /* CSR case without ANYLAYOUT, skip 1st buffer */
+ desc_data_offset = desc_table[desc_current].len;
+ }
+
+ while (1)
+ {
+ /* Get more input if necessary. Or end of packet. */
+ if (desc_data_offset == desc_table[desc_current].len)
+ {
+ if (PREDICT_FALSE (desc_table[desc_current].flags &
+ VIRTQ_DESC_F_NEXT))
+ {
+ desc_current = desc_table[desc_current].next;
+ desc_data_offset = 0;
+ }
+ else
+ {
+ goto out;
+ }
+ }
+
+ /* Get more output if necessary. Or end of packet. */
+ if (PREDICT_FALSE
+ (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
+ {
+ if (PREDICT_FALSE
+ (vum->cpus[thread_index].rx_buffers_len == 0))
+ {
+ /* Cancel speculation */
+ to_next--;
+ n_left_to_next++;
+
+ /*
+ * Checking if there are some left buffers.
+ * If not, just rewind the used buffers and stop.
+ * Note: Scheduled copies are not cancelled. This is
+ * not an issue as they would still be valid. Useless,
+ * but valid.
+ */
+ vhost_user_input_rewind_buffers (vm,
+ &vum->cpus
+ [thread_index],
+ b_head);
+ n_left = 0;
+ goto stop;
+ }
+
+ /* Get next output */
+ vum->cpus[thread_index].rx_buffers_len--;
+ u32 bi_next =
+ (vum->cpus[thread_index].rx_buffers)[vum->cpus
+ [thread_index].rx_buffers_len];
+ b_current->next_buffer = bi_next;
+ b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
+ bi_current = bi_next;
+ b_current = vlib_get_buffer (vm, bi_current);
+ }
+
+ /* Prepare a copy order executed later for the data */
+ vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
+ copy_len++;
+ u32 desc_data_l =
+ desc_table[desc_current].len - desc_data_offset;
+ cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
+ cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
+ cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
+ b_current->current_length);
+ cpy->src = desc_table[desc_current].addr + desc_data_offset;
+
+ desc_data_offset += cpy->len;
+
+ b_current->current_length += cpy->len;
+ b_head->total_length_not_including_first_buffer += cpy->len;
+ }
+
+ out:
+ CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
+
+ n_rx_bytes += b_head->total_length_not_including_first_buffer;
+ n_rx_packets++;
+
+ b_head->total_length_not_including_first_buffer -=
+ b_head->current_length;
+
+ /* consume the descriptor and return it as used */
+ txvq->last_avail_idx++;
+ txvq->last_used_idx++;
+
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
+
+ vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
+ vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
+ b_head->error = 0;
+
+ {
+ u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
+
+ /* redirect if feature path enabled */
+ vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
+ b_head);
+
+ u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
+ vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+ to_next, n_left_to_next,
+ bi, next0);
+ }
+
+ n_left--;
+
+ /*
+ * Although separating memory copies from virtio ring parsing
+ * is beneficial, we can offer to perform the copies from time
+ * to time in order to free some space in the ring.
+ */
+ if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
+ {
+ if (PREDICT_FALSE
+ (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
+ copy_len, &map_hint)))
+ {
+ vlib_error_count (vm, node->node_index,
+ VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
+ }
+ copy_len = 0;
+
+ /* give buffers back to driver */
+ CLIB_MEMORY_BARRIER ();
+ txvq->used->idx = txvq->last_used_idx;
+ vhost_user_log_dirty_ring (vui, txvq, idx);
+ }
+ }
+ stop:
+ vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+ }
+
+ /* Do the memory copies */
+ if (PREDICT_FALSE
+ (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
+ copy_len, &map_hint)))
+ {
+ vlib_error_count (vm, node->node_index,
+ VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
+ }
+
+ /* give buffers back to driver */
+ CLIB_MEMORY_BARRIER ();
+ txvq->used->idx = txvq->last_used_idx;
+ vhost_user_log_dirty_ring (vui, txvq, idx);
+
+ /* interrupt (call) handling */
+ if ((txvq->callfd_idx != ~0) &&
+ !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
+ {
+ txvq->n_since_last_int += n_rx_packets;
+
+ if (txvq->n_since_last_int > vum->coalesce_frames)
+ vhost_user_send_call (vm, txvq);
+ }
+
+ /* increase rx counters */
+ vlib_increment_combined_counter
+ (vnet_main.interface_main.combined_sw_if_counters
+ + VNET_INTERFACE_COUNTER_RX,
+ vlib_get_thread_index (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
+
+ vnet_device_increment_rx_packets (thread_index, n_rx_packets);
+
+ return n_rx_packets;
+}
+
+VLIB_NODE_FN (vhost_user_input_node) (vlib_main_t * vm,
+ vlib_node_runtime_t * node,
+ vlib_frame_t * frame)
+{
+ vhost_user_main_t *vum = &vhost_user_main;
+ uword n_rx_packets = 0;
+ vhost_user_intf_t *vui;
+ vnet_device_input_runtime_t *rt =
+ (vnet_device_input_runtime_t *) node->runtime_data;
+ vnet_device_and_queue_t *dq;
+
+ vec_foreach (dq, rt->devices_and_queues)
+ {
+ if (clib_smp_swap (&dq->interrupt_pending, 0) ||
+ (node->state == VLIB_NODE_STATE_POLLING))
+ {
+ vui =
+ pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance);
+ n_rx_packets = vhost_user_if_input (vm, vum, vui, dq->queue_id, node,
+ dq->mode);
+ }
+ }
+
+ return n_rx_packets;
+}
+
+#ifndef CLIB_MARCH_VARIANT
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (vhost_user_input_node) = {
+ .type = VLIB_NODE_TYPE_INPUT,
+ .name = "vhost-user-input",
+ .sibling_of = "device-input",
+
+ /* Will be enabled if/when hardware is detected. */
+ .state = VLIB_NODE_STATE_DISABLED,
+
+ .format_buffer = format_ethernet_header_with_length,
+ .format_trace = format_vhost_trace,
+
+ .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
+ .error_strings = vhost_user_input_func_error_strings,
+};
+/* *INDENT-ON* */
+#endif
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/devices/virtio/vhost_user_output.c b/src/vnet/devices/virtio/vhost_user_output.c
new file mode 100644
index 00000000000..f400f18307a
--- /dev/null
+++ b/src/vnet/devices/virtio/vhost_user_output.c
@@ -0,0 +1,671 @@
+/*
+ *------------------------------------------------------------------
+ * vhost-user-output
+ *
+ * Copyright (c) 2014-2018 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <fcntl.h> /* for open */
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h> /* for iovec */
+#include <netinet/in.h>
+#include <sys/vfs.h>
+
+#include <linux/if_arp.h>
+#include <linux/if_tun.h>
+
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+
+#include <vnet/ip/ip.h>
+
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/devices/devices.h>
+#include <vnet/feature/feature.h>
+
+#include <vnet/devices/virtio/vhost_user.h>
+#include <vnet/devices/virtio/vhost_user_inline.h>
+
+/*
+ * On the transmit side, we keep processing the buffers from vlib in the while
+ * loop and prepare the copy order to be executed later. However, the static
+ * array which we keep the copy order is limited to VHOST_USER_COPY_ARRAY_N
+ * entries. In order to not corrupt memory, we have to do the copy when the
+ * static array reaches the copy threshold. We subtract 40 in case the code
+ * goes into the inner loop for a maximum of 64k frames which may require
+ * more array entries.
+ */
+#define VHOST_USER_TX_COPY_THRESHOLD (VHOST_USER_COPY_ARRAY_N - 40)
+
+vnet_device_class_t vhost_user_device_class;
+
+#define foreach_vhost_user_tx_func_error \
+ _(NONE, "no error") \
+ _(NOT_READY, "vhost vring not ready") \
+ _(DOWN, "vhost interface is down") \
+ _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
+ _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
+ _(MMAP_FAIL, "mmap failure") \
+ _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
+
+typedef enum
+{
+#define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
+ foreach_vhost_user_tx_func_error
+#undef _
+ VHOST_USER_TX_FUNC_N_ERROR,
+} vhost_user_tx_func_error_t;
+
+static __clib_unused char *vhost_user_tx_func_error_strings[] = {
+#define _(n,s) s,
+ foreach_vhost_user_tx_func_error
+#undef _
+};
+
+static __clib_unused u8 *
+format_vhost_user_interface_name (u8 * s, va_list * args)
+{
+ u32 i = va_arg (*args, u32);
+ u32 show_dev_instance = ~0;
+ vhost_user_main_t *vum = &vhost_user_main;
+
+ if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
+ show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
+
+ if (show_dev_instance != ~0)
+ i = show_dev_instance;
+
+ s = format (s, "VirtualEthernet0/0/%d", i);
+ return s;
+}
+
+static __clib_unused int
+vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
+{
+ // FIXME: check if the new dev instance is already used
+ vhost_user_main_t *vum = &vhost_user_main;
+ vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
+ hi->dev_instance, ~0);
+
+ vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
+ new_dev_instance;
+
+ DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
+ hi->dev_instance, new_dev_instance);
+
+ return 0;
+}
+
+/**
+ * @brief Try once to lock the vring
+ * @return 0 on success, non-zero on failure.
+ */
+static_always_inline int
+vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
+{
+ return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
+}
+
+/**
+ * @brief Spin until the vring is successfully locked
+ */
+static_always_inline void
+vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
+{
+ while (vhost_user_vring_try_lock (vui, qid))
+ ;
+}
+
+/**
+ * @brief Unlock the vring lock
+ */
+static_always_inline void
+vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
+{
+ *vui->vring_locks[qid] = 0;
+}
+
+static_always_inline void
+vhost_user_tx_trace (vhost_trace_t * t,
+ vhost_user_intf_t * vui, u16 qid,
+ vlib_buffer_t * b, vhost_user_vring_t * rxvq)
+{
+ vhost_user_main_t *vum = &vhost_user_main;
+ u32 last_avail_idx = rxvq->last_avail_idx;
+ u32 desc_current = rxvq->avail->ring[last_avail_idx & rxvq->qsz_mask];
+ vring_desc_t *hdr_desc = 0;
+ u32 hint = 0;
+
+ memset (t, 0, sizeof (*t));
+ t->device_index = vui - vum->vhost_user_interfaces;
+ t->qid = qid;
+
+ hdr_desc = &rxvq->desc[desc_current];
+ if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
+ {
+ t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
+ /* Header is the first here */
+ hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
+ }
+ if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
+ {
+ t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
+ }
+ if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
+ !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
+ {
+ t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
+ }
+
+ t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
+}
+
+static_always_inline u32
+vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
+ u16 copy_len, u32 * map_hint)
+{
+ void *dst0, *dst1, *dst2, *dst3;
+ if (PREDICT_TRUE (copy_len >= 4))
+ {
+ if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
+ return 1;
+ if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
+ return 1;
+ while (PREDICT_TRUE (copy_len >= 4))
+ {
+ dst0 = dst2;
+ dst1 = dst3;
+
+ if (PREDICT_FALSE
+ (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
+ return 1;
+ if (PREDICT_FALSE
+ (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
+ return 1;
+
+ CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
+ CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
+
+ clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
+ clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
+
+ vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
+ vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
+ copy_len -= 2;
+ cpy += 2;
+ }
+ }
+ while (copy_len)
+ {
+ if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
+ return 1;
+ clib_memcpy (dst0, (void *) cpy->src, cpy->len);
+ vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
+ copy_len -= 1;
+ cpy += 1;
+ }
+ return 0;
+}
+
+
+uword
+CLIB_MULTIARCH_FN (vhost_user_tx) (vlib_main_t * vm,
+ vlib_node_runtime_t * node,
+ vlib_frame_t * frame)
+{
+ u32 *buffers = vlib_frame_args (frame);
+ u32 n_left = frame->n_vectors;
+ vhost_user_main_t *vum = &vhost_user_main;
+ vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
+ vhost_user_intf_t *vui =
+ pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
+ u32 qid = ~0;
+ vhost_user_vring_t *rxvq;
+ u8 error;
+ u32 thread_index = vlib_get_thread_index ();
+ u32 map_hint = 0;
+ u8 retry = 8;
+ u16 copy_len;
+ u16 tx_headers_len;
+
+ if (PREDICT_FALSE (!vui->admin_up))
+ {
+ error = VHOST_USER_TX_FUNC_ERROR_DOWN;
+ goto done3;
+ }
+
+ if (PREDICT_FALSE (!vui->is_up))
+ {
+ error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
+ goto done3;
+ }
+
+ qid =
+ VHOST_VRING_IDX_RX (*vec_elt_at_index
+ (vui->per_cpu_tx_qid, thread_index));
+ rxvq = &vui->vrings[qid];
+ if (PREDICT_FALSE (vui->use_tx_spinlock))
+ vhost_user_vring_lock (vui, qid);
+
+retry:
+ error = VHOST_USER_TX_FUNC_ERROR_NONE;
+ tx_headers_len = 0;
+ copy_len = 0;
+ while (n_left > 0)
+ {
+ vlib_buffer_t *b0, *current_b0;
+ u16 desc_head, desc_index, desc_len;
+ vring_desc_t *desc_table;
+ uword buffer_map_addr;
+ u32 buffer_len;
+ u16 bytes_left;
+
+ if (PREDICT_TRUE (n_left > 1))
+ vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
+
+ b0 = vlib_get_buffer (vm, buffers[0]);
+
+ if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
+ {
+ vum->cpus[thread_index].current_trace =
+ vlib_add_trace (vm, node, b0,
+ sizeof (*vum->cpus[thread_index].current_trace));
+ vhost_user_tx_trace (vum->cpus[thread_index].current_trace,
+ vui, qid / 2, b0, rxvq);
+ }
+
+ if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
+ {
+ error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
+ goto done;
+ }
+
+ desc_table = rxvq->desc;
+ desc_head = desc_index =
+ rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
+
+ /* Go deeper in case of indirect descriptor
+ * I don't know of any driver providing indirect for RX. */
+ if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
+ {
+ if (PREDICT_FALSE
+ (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
+ {
+ error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
+ goto done;
+ }
+ if (PREDICT_FALSE
+ (!(desc_table =
+ map_guest_mem (vui, rxvq->desc[desc_index].addr,
+ &map_hint))))
+ {
+ error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
+ goto done;
+ }
+ desc_index = 0;
+ }
+
+ desc_len = vui->virtio_net_hdr_sz;
+ buffer_map_addr = desc_table[desc_index].addr;
+ buffer_len = desc_table[desc_index].len;
+
+ {
+ // Get a header from the header array
+ virtio_net_hdr_mrg_rxbuf_t *hdr =
+ &vum->cpus[thread_index].tx_headers[tx_headers_len];
+ tx_headers_len++;
+ hdr->hdr.flags = 0;
+ hdr->hdr.gso_type = 0;
+ hdr->num_buffers = 1; //This is local, no need to check
+
+ // Prepare a copy order executed later for the header
+ vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
+ copy_len++;
+ cpy->len = vui->virtio_net_hdr_sz;
+ cpy->dst = buffer_map_addr;
+ cpy->src = (uword) hdr;
+ }
+
+ buffer_map_addr += vui->virtio_net_hdr_sz;
+ buffer_len -= vui->virtio_net_hdr_sz;
+ bytes_left = b0->current_length;
+ current_b0 = b0;
+ while (1)
+ {
+ if (buffer_len == 0)
+ { //Get new output
+ if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
+ {
+ //Next one is chained
+ desc_index = desc_table[desc_index].next;
+ buffer_map_addr = desc_table[desc_index].addr;
+ buffer_len = desc_table[desc_index].len;
+ }
+ else if (vui->virtio_net_hdr_sz == 12) //MRG is available
+ {
+ virtio_net_hdr_mrg_rxbuf_t *hdr =
+ &vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
+
+ //Move from available to used buffer
+ rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id =
+ desc_head;
+ rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len =
+ desc_len;
+ vhost_user_log_dirty_ring (vui, rxvq,
+ ring[rxvq->last_used_idx &
+ rxvq->qsz_mask]);
+
+ rxvq->last_avail_idx++;
+ rxvq->last_used_idx++;
+ hdr->num_buffers++;
+ desc_len = 0;
+
+ if (PREDICT_FALSE
+ (rxvq->last_avail_idx == rxvq->avail->idx))
+ {
+ //Dequeue queued descriptors for this packet
+ rxvq->last_used_idx -= hdr->num_buffers - 1;
+ rxvq->last_avail_idx -= hdr->num_buffers - 1;
+ error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
+ goto done;
+ }
+
+ desc_table = rxvq->desc;
+ desc_head = desc_index =
+ rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
+ if (PREDICT_FALSE
+ (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
+ {
+ //It is seriously unlikely that a driver will put indirect descriptor
+ //after non-indirect descriptor.
+ if (PREDICT_FALSE
+ (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
+ {
+ error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
+ goto done;
+ }
+ if (PREDICT_FALSE
+ (!(desc_table =
+ map_guest_mem (vui,
+ rxvq->desc[desc_index].addr,
+ &map_hint))))
+ {
+ error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
+ goto done;
+ }
+ desc_index = 0;
+ }
+ buffer_map_addr = desc_table[desc_index].addr;
+ buffer_len = desc_table[desc_index].len;
+ }
+ else
+ {
+ error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
+ goto done;
+ }
+ }
+
+ {
+ vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
+ copy_len++;
+ cpy->len = bytes_left;
+ cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
+ cpy->dst = buffer_map_addr;
+ cpy->src = (uword) vlib_buffer_get_current (current_b0) +
+ current_b0->current_length - bytes_left;
+
+ bytes_left -= cpy->len;
+ buffer_len -= cpy->len;
+ buffer_map_addr += cpy->len;
+ desc_len += cpy->len;
+
+ CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
+ }
+
+ // Check if vlib buffer has more data. If not, get more or break.
+ if (PREDICT_TRUE (!bytes_left))
+ {
+ if (PREDICT_FALSE
+ (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
+ {
+ current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
+ bytes_left = current_b0->current_length;
+ }
+ else
+ {
+ //End of packet
+ break;
+ }
+ }
+ }
+
+ //Move from available to used ring
+ rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = desc_head;
+ rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = desc_len;
+ vhost_user_log_dirty_ring (vui, rxvq,
+ ring[rxvq->last_used_idx & rxvq->qsz_mask]);
+ rxvq->last_avail_idx++;
+ rxvq->last_used_idx++;
+
+ if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
+ {
+ vum->cpus[thread_index].current_trace->hdr =
+ vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
+ }
+
+ n_left--; //At the end for error counting when 'goto done' is invoked
+
+ /*
+ * Do the copy periodically to prevent
+ * vum->cpus[thread_index].copy array overflow and corrupt memory
+ */
+ if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD))
+ {
+ if (PREDICT_FALSE
+ (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
+ copy_len, &map_hint)))
+ {
+ vlib_error_count (vm, node->node_index,
+ VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
+ }
+ copy_len = 0;
+
+ /* give buffers back to driver */
+ CLIB_MEMORY_BARRIER ();
+ rxvq->used->idx = rxvq->last_used_idx;
+ vhost_user_log_dirty_ring (vui, rxvq, idx);
+ }
+ buffers++;
+ }
+
+done:
+ //Do the memory copies
+ if (PREDICT_FALSE
+ (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
+ copy_len, &map_hint)))
+ {
+ vlib_error_count (vm, node->node_index,
+ VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
+ }
+
+ CLIB_MEMORY_BARRIER ();
+ rxvq->used->idx = rxvq->last_used_idx;
+ vhost_user_log_dirty_ring (vui, rxvq, idx);
+
+ /*
+ * When n_left is set, error is always set to something too.
+ * In case error is due to lack of remaining buffers, we go back up and
+ * retry.
+ * The idea is that it is better to waste some time on packets
+ * that have been processed already than dropping them and get
+ * more fresh packets with a good likelyhood that they will be dropped too.
+ * This technique also gives more time to VM driver to pick-up packets.
+ * In case the traffic flows from physical to virtual interfaces, this
+ * technique will end-up leveraging the physical NIC buffer in order to
+ * absorb the VM's CPU jitter.
+ */
+ if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
+ {
+ retry--;
+ goto retry;
+ }
+
+ /* interrupt (call) handling */
+ if ((rxvq->callfd_idx != ~0) &&
+ !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
+ {
+ rxvq->n_since_last_int += frame->n_vectors - n_left;
+
+ if (rxvq->n_since_last_int > vum->coalesce_frames)
+ vhost_user_send_call (vm, rxvq);
+ }
+
+ vhost_user_vring_unlock (vui, qid);
+
+done3:
+ if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
+ {
+ vlib_error_count (vm, node->node_index, error, n_left);
+ vlib_increment_simple_counter
+ (vnet_main.interface_main.sw_if_counters
+ + VNET_INTERFACE_COUNTER_DROP,
+ thread_index, vui->sw_if_index, n_left);
+ }
+
+ vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
+ return frame->n_vectors;
+}
+
+static __clib_unused clib_error_t *
+vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index,
+ u32 qid, vnet_hw_interface_rx_mode mode)
+{
+ vlib_main_t *vm = vnm->vlib_main;
+ vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
+ vhost_user_main_t *vum = &vhost_user_main;
+ vhost_user_intf_t *vui =
+ pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
+ vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
+
+ if ((mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
+ (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
+ {
+ if (txvq->kickfd_idx == ~0)
+ {
+ // We cannot support interrupt mode if the driver opts out
+ return clib_error_return (0, "Driver does not support interrupt");
+ }
+ if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
+ {
+ vum->ifq_count++;
+ // Start the timer if this is the first encounter on interrupt
+ // interface/queue
+ if ((vum->ifq_count == 1) &&
+ (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
+ vlib_process_signal_event (vm,
+ vhost_user_send_interrupt_node.index,
+ VHOST_USER_EVENT_START_TIMER, 0);
+ }
+ }
+ else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
+ {
+ if (((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
+ (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) &&
+ vum->ifq_count)
+ {
+ vum->ifq_count--;
+ // Stop the timer if there is no more interrupt interface/queue
+ if ((vum->ifq_count == 0) &&
+ (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
+ vlib_process_signal_event (vm,
+ vhost_user_send_interrupt_node.index,
+ VHOST_USER_EVENT_STOP_TIMER, 0);
+ }
+ }
+
+ txvq->mode = mode;
+ if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
+ txvq->used->flags = VRING_USED_F_NO_NOTIFY;
+ else if ((mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE) ||
+ (mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT))
+ txvq->used->flags = 0;
+ else
+ {
+ clib_warning ("BUG: unhandled mode %d changed for if %d queue %d", mode,
+ hw_if_index, qid);
+ return clib_error_return (0, "unsupported");
+ }
+
+ return 0;
+}
+
+static __clib_unused clib_error_t *
+vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
+ u32 flags)
+{
+ vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
+ vhost_user_main_t *vum = &vhost_user_main;
+ vhost_user_intf_t *vui =
+ pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
+ u32 hw_flags = 0;
+ vui->admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
+ hw_flags = vui->admin_up ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
+
+ vnet_hw_interface_set_flags (vnm, vui->hw_if_index, hw_flags);
+
+ return /* no error */ 0;
+}
+
+#ifndef CLIB_MARCH_VARIANT
+/* *INDENT-OFF* */
+VNET_DEVICE_CLASS (vhost_user_device_class) = {
+ .name = "vhost-user",
+ .tx_function = vhost_user_tx,
+ .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
+ .tx_function_error_strings = vhost_user_tx_func_error_strings,
+ .format_device_name = format_vhost_user_interface_name,
+ .name_renumber = vhost_user_name_renumber,
+ .admin_up_down_function = vhost_user_interface_admin_up_down,
+ .rx_mode_change_function = vhost_user_interface_rx_mode_change,
+ .format_tx_trace = format_vhost_trace,
+};
+
+#if __x86_64__
+vlib_node_function_t __clib_weak vhost_user_tx_avx512;
+vlib_node_function_t __clib_weak vhost_user_tx_avx2;
+static void __clib_constructor
+vhost_user_tx_multiarch_select (void)
+{
+ if (vhost_user_tx_avx512 && clib_cpu_supports_avx512f ())
+ vhost_user_device_class.tx_function = vhost_user_tx_avx512;
+ else if (vhost_user_tx_avx2 && clib_cpu_supports_avx2 ())
+ vhost_user_device_class.tx_function = vhost_user_tx_avx2;
+}
+#endif
+#endif
+
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */