2 *------------------------------------------------------------------
5 * Copyright (c) 2014 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/uio.h> /* for iovec */
27 #include <netinet/in.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
36 #include <vnet/ip/ip.h>
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/devices/devices.h>
40 #include <vnet/feature/feature.h>
42 #include <vnet/devices/virtio/vhost-user.h>
46 * @brief vHost User Device Driver.
48 * This file contains the source code for vHost User interface.
52 #define VHOST_USER_DEBUG_SOCKET 0
53 #define VHOST_DEBUG_VQ 0
55 #if VHOST_USER_DEBUG_SOCKET == 1
56 #define DBG_SOCK(args...) clib_warning(args);
58 #define DBG_SOCK(args...)
61 #if VHOST_DEBUG_VQ == 1
62 #define DBG_VQ(args...) clib_warning(args);
64 #define DBG_VQ(args...)
68 * When an RX queue is down but active, received packets
69 * must be discarded. This value controls up to how many
70 * packets will be discarded during each round.
72 #define VHOST_USER_DOWN_DISCARD_COUNT 256
75 * When the number of available buffers gets under this threshold,
76 * RX node will start discarding packets.
78 #define VHOST_USER_RX_BUFFER_STARVATION 32
81 * On the receive side, the host should free descriptors as soon
82 * as possible in order to avoid TX drop in the VM.
83 * This value controls the number of copy operations that are stacked
84 * before copy is done for all and descriptors are given back to
86 * The value 64 was obtained by testing (48 and 128 were not as good).
88 #define VHOST_USER_RX_COPY_THRESHOLD 64
90 #define UNIX_GET_FD(unixfd_idx) \
91 (unixfd_idx != ~0) ? \
92 pool_elt_at_index (unix_main.file_pool, \
93 unixfd_idx)->file_descriptor : -1;
95 #define foreach_virtio_trace_flags \
96 _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
97 _ (SINGLE_DESC, 1, "Single descriptor packet") \
98 _ (INDIRECT, 2, "Indirect descriptor") \
99 _ (MAP_ERROR, 4, "Memory mapping error")
103 #define _(n,i,s) VIRTIO_TRACE_F_##n,
104 foreach_virtio_trace_flags
106 } virtio_trace_flag_t;
108 vlib_node_registration_t vhost_user_input_node;
110 #define foreach_vhost_user_tx_func_error \
111 _(NONE, "no error") \
112 _(NOT_READY, "vhost vring not ready") \
113 _(DOWN, "vhost interface is down") \
114 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
115 _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
116 _(MMAP_FAIL, "mmap failure") \
117 _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
121 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
122 foreach_vhost_user_tx_func_error
124 VHOST_USER_TX_FUNC_N_ERROR,
125 } vhost_user_tx_func_error_t;
127 static char *vhost_user_tx_func_error_strings[] = {
129 foreach_vhost_user_tx_func_error
133 #define foreach_vhost_user_input_func_error \
134 _(NO_ERROR, "no error") \
135 _(NO_BUFFER, "no available buffer") \
136 _(MMAP_FAIL, "mmap failure") \
137 _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
138 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
139 _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
143 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
144 foreach_vhost_user_input_func_error
146 VHOST_USER_INPUT_FUNC_N_ERROR,
147 } vhost_user_input_func_error_t;
149 static char *vhost_user_input_func_error_strings[] = {
151 foreach_vhost_user_input_func_error
156 static vhost_user_main_t vhost_user_main = {
160 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
161 .name = "vhost-user",
166 format_vhost_user_interface_name (u8 * s, va_list * args)
168 u32 i = va_arg (*args, u32);
169 u32 show_dev_instance = ~0;
170 vhost_user_main_t *vum = &vhost_user_main;
172 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
173 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
175 if (show_dev_instance != ~0)
176 i = show_dev_instance;
178 s = format (s, "VirtualEthernet0/0/%d", i);
183 vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
185 // FIXME: check if the new dev instance is already used
186 vhost_user_main_t *vum = &vhost_user_main;
187 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
188 hi->dev_instance, ~0);
190 vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
193 DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
194 hi->dev_instance, new_dev_instance);
199 static_always_inline void *
200 map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint)
203 if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) &&
204 ((vui->regions[i].guest_phys_addr +
205 vui->regions[i].memory_size) > addr)))
207 return (void *) (vui->region_mmap_addr[i] + addr -
208 vui->regions[i].guest_phys_addr);
211 __m128i rl, rh, al, ah, r;
212 al = _mm_set1_epi64x (addr + 1);
213 ah = _mm_set1_epi64x (addr);
215 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]);
216 rl = _mm_cmpgt_epi64 (al, rl);
217 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]);
218 rh = _mm_cmpgt_epi64 (rh, ah);
219 r = _mm_and_si128 (rl, rh);
221 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]);
222 rl = _mm_cmpgt_epi64 (al, rl);
223 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]);
224 rh = _mm_cmpgt_epi64 (rh, ah);
225 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22);
227 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]);
228 rl = _mm_cmpgt_epi64 (al, rl);
229 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]);
230 rh = _mm_cmpgt_epi64 (rh, ah);
231 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44);
233 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]);
234 rl = _mm_cmpgt_epi64 (al, rl);
235 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]);
236 rh = _mm_cmpgt_epi64 (rh, ah);
237 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
239 r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
240 i = __builtin_ctzll (_mm_movemask_epi8 (r) |
241 (1 << VHOST_MEMORY_MAX_NREGIONS));
243 if (i < vui->nregions)
246 return (void *) (vui->region_mmap_addr[i] + addr -
247 vui->regions[i].guest_phys_addr);
251 for (i = 0; i < vui->nregions; i++)
253 if ((vui->regions[i].guest_phys_addr <= addr) &&
254 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
258 return (void *) (vui->region_mmap_addr[i] + addr -
259 vui->regions[i].guest_phys_addr);
263 DBG_VQ ("failed to map guest mem addr %llx", addr);
269 map_user_mem (vhost_user_intf_t * vui, uword addr)
272 for (i = 0; i < vui->nregions; i++)
274 if ((vui->regions[i].userspace_addr <= addr) &&
275 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
278 return (void *) (vui->region_mmap_addr[i] + addr -
279 vui->regions[i].userspace_addr);
286 get_huge_page_size (int fd)
294 unmap_all_mem_regions (vhost_user_intf_t * vui)
297 for (i = 0; i < vui->nregions; i++)
299 if (vui->region_mmap_addr[i] != (void *) -1)
302 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
304 ssize_t map_sz = (vui->regions[i].memory_size +
305 vui->regions[i].mmap_offset +
306 page_sz) & ~(page_sz - 1);
309 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
313 ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
314 vui->region_mmap_addr[i], map_sz, page_sz);
316 vui->region_mmap_addr[i] = (void *) -1;
320 clib_warning ("failed to unmap memory region (errno %d)",
323 close (vui->region_mmap_fd[i]);
330 vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
332 //Let's try to assign one queue to each thread
335 vui->use_tx_spinlock = 0;
338 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
340 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
341 if (!rxvq->started || !rxvq->enabled)
344 vui->per_cpu_tx_qid[cpu_index] = qid;
346 if (cpu_index == vlib_get_thread_main ()->n_vlib_mains)
349 //We need to loop, meaning the spinlock has to be used
350 vui->use_tx_spinlock = 1;
353 //Could not find a single valid one
355 cpu_index < vlib_get_thread_main ()->n_vlib_mains; cpu_index++)
357 vui->per_cpu_tx_qid[cpu_index] = 0;
365 vhost_user_rx_thread_placement ()
367 vhost_user_main_t *vum = &vhost_user_main;
368 vhost_user_intf_t *vui;
372 //Let's list all workers cpu indexes
374 for (i = vum->input_cpu_first_index;
375 i < vum->input_cpu_first_index + vum->input_cpu_count; i++)
377 vlib_node_set_state (vlib_mains[i], vhost_user_input_node.index,
378 VLIB_NODE_STATE_DISABLED);
379 vec_add1 (workers, i);
382 vec_foreach (vhc, vum->cpus)
384 vec_reset_length (vhc->rx_queues);
388 vhost_iface_and_queue_t iaq;
390 pool_foreach (vui, vum->vhost_user_interfaces, {
391 u32 *vui_workers = vec_len (vui->workers) ? vui->workers : workers;
393 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
395 vhost_user_vring_t *txvq =
396 &vui->vrings[VHOST_VRING_IDX_TX (qid)];
400 i %= vec_len (vui_workers);
401 u32 cpu_index = vui_workers[i];
403 vhc = &vum->cpus[cpu_index];
406 iaq.vhost_iface_index = vui - vum->vhost_user_interfaces;
407 vec_add1 (vhc->rx_queues, iaq);
408 vlib_node_set_state (vlib_mains[cpu_index],
409 vhost_user_input_node.index,
410 VLIB_NODE_STATE_POLLING);
417 vhost_user_thread_placement (u32 sw_if_index, u32 worker_thread_index, u8 del)
419 vhost_user_main_t *vum = &vhost_user_main;
420 vhost_user_intf_t *vui;
421 vnet_hw_interface_t *hw;
423 if (worker_thread_index < vum->input_cpu_first_index ||
424 worker_thread_index >=
425 vum->input_cpu_first_index + vum->input_cpu_count)
428 if (!(hw = vnet_get_sup_hw_interface (vnet_get_main (), sw_if_index)))
431 vui = pool_elt_at_index (vum->vhost_user_interfaces, hw->dev_instance);
433 vec_foreach (w, vui->workers)
435 if (*w == worker_thread_index)
437 found = w - vui->workers;
446 vec_del1 (vui->workers, found);
448 else if (found == ~0)
450 vec_add1 (vui->workers, worker_thread_index);
453 vhost_user_rx_thread_placement ();
457 /** @brief Returns whether at least one TX and one RX vring are enabled */
459 vhost_user_intf_ready (vhost_user_intf_t * vui)
461 int i, found[2] = { }; //RX + TX
463 for (i = 0; i < VHOST_VRING_MAX_N; i++)
464 if (vui->vrings[i].started && vui->vrings[i].enabled)
467 return found[0] && found[1];
471 vhost_user_update_iface_state (vhost_user_intf_t * vui)
473 /* if we have pointers to descriptor table, go up */
474 int is_up = vhost_user_intf_ready (vui);
475 if (is_up != vui->is_up)
477 DBG_SOCK ("interface %d %s", vui->sw_if_index,
478 is_up ? "ready" : "down");
479 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
480 is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP :
484 vhost_user_rx_thread_placement ();
485 vhost_user_tx_thread_placement (vui);
488 static clib_error_t *
489 vhost_user_callfd_read_ready (unix_file_t * uf)
491 __attribute__ ((unused)) int n;
493 n = read (uf->file_descriptor, ((char *) &buff), 8);
497 static clib_error_t *
498 vhost_user_kickfd_read_ready (unix_file_t * uf)
500 __attribute__ ((unused)) int n;
502 vhost_user_intf_t *vui =
503 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
504 uf->private_data >> 8);
505 u32 qid = uf->private_data & 0xff;
506 n = read (uf->file_descriptor, ((char *) &buff), 8);
507 DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
509 vlib_worker_thread_barrier_sync (vlib_get_main ());
510 vui->vrings[qid].started = 1;
511 vhost_user_update_iface_state (vui);
512 vlib_worker_thread_barrier_release (vlib_get_main ());
517 * @brief Try once to lock the vring
518 * @return 0 on success, non-zero on failure.
521 vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
523 return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
527 * @brief Spin until the vring is successfully locked
530 vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
532 while (vhost_user_vring_try_lock (vui, qid))
537 * @brief Unlock the vring lock
540 vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
542 *vui->vring_locks[qid] = 0;
546 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
548 vhost_user_vring_t *vring = &vui->vrings[qid];
549 memset (vring, 0, sizeof (*vring));
550 vring->kickfd_idx = ~0;
551 vring->callfd_idx = ~0;
555 * We have a bug with some qemu 2.5, and this may be a fix.
556 * Feel like interpretation holy text, but this is from vhost-user.txt.
558 * One queue pair is enabled initially. More queues are enabled
559 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
561 * Don't know who's right, but this is what DPDK does.
563 if (qid == 0 || qid == 1)
568 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
570 vhost_user_vring_t *vring = &vui->vrings[qid];
571 if (vring->kickfd_idx != ~0)
573 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
575 unix_file_del (&unix_main, uf);
576 vring->kickfd_idx = ~0;
578 if (vring->callfd_idx != ~0)
580 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
582 unix_file_del (&unix_main, uf);
583 vring->callfd_idx = ~0;
585 if (vring->errfd != -1)
586 close (vring->errfd);
587 vhost_user_vring_init (vui, qid);
591 vhost_user_if_disconnect (vhost_user_intf_t * vui)
593 vnet_main_t *vnm = vnet_get_main ();
596 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
598 if (vui->unix_file_index != ~0)
600 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
601 vui->unix_file_index = ~0;
606 for (q = 0; q < VHOST_VRING_MAX_N; q++)
607 vhost_user_vring_close (vui, q);
609 unmap_all_mem_regions (vui);
610 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
613 #define VHOST_LOG_PAGE 0x1000
614 static_always_inline void
615 vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
616 u64 addr, u64 len, u8 is_host_address)
618 if (PREDICT_TRUE (vui->log_base_addr == 0
619 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
625 addr = (u64) map_user_mem (vui, (uword) addr);
627 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
629 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
633 CLIB_MEMORY_BARRIER ();
634 u64 page = addr / VHOST_LOG_PAGE;
635 while (page * VHOST_LOG_PAGE < addr + len)
637 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
642 static_always_inline void
643 vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
645 vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
648 #define vhost_user_log_dirty_ring(vui, vq, member) \
649 if (PREDICT_FALSE(vq->log_used)) { \
650 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
651 sizeof(vq->used->member)); \
654 static clib_error_t *
655 vhost_user_socket_read (unix_file_t * uf)
658 int fd, number_of_fds = 0;
659 int fds[VHOST_MEMORY_MAX_NREGIONS];
660 vhost_user_msg_t msg;
663 vhost_user_main_t *vum = &vhost_user_main;
664 vhost_user_intf_t *vui;
665 struct cmsghdr *cmsg;
667 unix_file_t template = { 0 };
668 vnet_main_t *vnm = vnet_get_main ();
670 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
672 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
674 memset (&mh, 0, sizeof (mh));
675 memset (control, 0, sizeof (control));
677 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
680 /* set the payload */
681 iov[0].iov_base = (void *) &msg;
682 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
686 mh.msg_control = control;
687 mh.msg_controllen = sizeof (control);
689 n = recvmsg (uf->file_descriptor, &mh, 0);
691 /* Stop workers to avoid end of the world */
692 vlib_worker_thread_barrier_sync (vlib_get_main ());
694 if (n != VHOST_USER_MSG_HDR_SZ)
698 DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
702 DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
703 n, VHOST_USER_MSG_HDR_SZ);
708 if (mh.msg_flags & MSG_CTRUNC)
710 DBG_SOCK ("MSG_CTRUNC is set");
714 cmsg = CMSG_FIRSTHDR (&mh);
716 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
717 (cmsg->cmsg_type == SCM_RIGHTS) &&
718 (cmsg->cmsg_len - CMSG_LEN (0) <=
719 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
721 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
722 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
725 /* version 1, no reply bit set */
726 if ((msg.flags & 7) != 1)
728 DBG_SOCK ("malformed message received. closing socket");
735 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
739 DBG_SOCK ("read failed %s", strerror (errno));
742 else if (rv != msg.size)
744 DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
751 case VHOST_USER_GET_FEATURES:
753 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
754 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
755 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
756 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
757 (1ULL << FEAT_VHOST_F_LOG_ALL) |
758 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
759 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
760 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
761 (1ULL << FEAT_VIRTIO_F_VERSION_1);
762 msg.u64 &= vui->feature_mask;
763 msg.size = sizeof (msg.u64);
764 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
765 vui->hw_if_index, msg.u64);
768 case VHOST_USER_SET_FEATURES:
769 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
770 vui->hw_if_index, msg.u64);
772 vui->features = msg.u64;
775 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
776 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
777 vui->virtio_net_hdr_sz = 12;
779 vui->virtio_net_hdr_sz = 10;
782 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
784 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
785 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
788 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
789 vhost_user_vring_close(&vui->vrings[q]); */
793 case VHOST_USER_SET_MEM_TABLE:
794 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
795 vui->hw_if_index, msg.memory.nregions);
797 if ((msg.memory.nregions < 1) ||
798 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
801 DBG_SOCK ("number of mem regions must be between 1 and %i",
802 VHOST_MEMORY_MAX_NREGIONS);
807 if (msg.memory.nregions != number_of_fds)
809 DBG_SOCK ("each memory region must have FD");
812 unmap_all_mem_regions (vui);
813 for (i = 0; i < msg.memory.nregions; i++)
815 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
816 sizeof (vhost_user_memory_region_t));
818 long page_sz = get_huge_page_size (fds[i]);
820 /* align size to 2M page */
821 ssize_t map_sz = (vui->regions[i].memory_size +
822 vui->regions[i].mmap_offset +
823 page_sz) & ~(page_sz - 1);
825 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
826 MAP_SHARED, fds[i], 0);
827 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
828 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
829 vui->regions[i].memory_size;
832 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
833 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
836 if (vui->region_mmap_addr[i] == MAP_FAILED)
838 clib_warning ("failed to map memory. errno is %d", errno);
841 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
842 vui->region_mmap_fd[i] = fds[i];
844 vui->nregions = msg.memory.nregions;
847 case VHOST_USER_SET_VRING_NUM:
848 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
849 vui->hw_if_index, msg.state.index, msg.state.num);
851 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
852 (msg.state.num == 0) || /* it cannot be zero */
853 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
855 vui->vrings[msg.state.index].qsz = msg.state.num;
858 case VHOST_USER_SET_VRING_ADDR:
859 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
860 vui->hw_if_index, msg.state.index);
862 if (msg.state.index >= VHOST_VRING_MAX_N)
864 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
865 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
869 if (msg.size < sizeof (msg.addr))
871 DBG_SOCK ("vhost message is too short (%d < %d)",
872 msg.size, sizeof (msg.addr));
876 vui->vrings[msg.state.index].desc = (vring_desc_t *)
877 map_user_mem (vui, msg.addr.desc_user_addr);
878 vui->vrings[msg.state.index].used = (vring_used_t *)
879 map_user_mem (vui, msg.addr.used_user_addr);
880 vui->vrings[msg.state.index].avail = (vring_avail_t *)
881 map_user_mem (vui, msg.addr.avail_user_addr);
883 if ((vui->vrings[msg.state.index].desc == NULL) ||
884 (vui->vrings[msg.state.index].used == NULL) ||
885 (vui->vrings[msg.state.index].avail == NULL))
887 DBG_SOCK ("failed to map user memory for hw_if_index %d",
892 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
893 vui->vrings[msg.state.index].log_used =
894 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
896 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
897 the ring is initialized in an enabled state. */
898 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
900 vui->vrings[msg.state.index].enabled = 1;
903 vui->vrings[msg.state.index].last_used_idx =
904 vui->vrings[msg.state.index].last_avail_idx =
905 vui->vrings[msg.state.index].used->idx;
907 /* tell driver that we don't want interrupts */
908 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
911 case VHOST_USER_SET_OWNER:
912 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
915 case VHOST_USER_RESET_OWNER:
916 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
919 case VHOST_USER_SET_VRING_CALL:
920 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
921 vui->hw_if_index, msg.u64);
923 q = (u8) (msg.u64 & 0xFF);
925 /* if there is old fd, delete and close it */
926 if (vui->vrings[q].callfd_idx != ~0)
928 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
929 vui->vrings[q].callfd_idx);
930 unix_file_del (&unix_main, uf);
931 vui->vrings[q].callfd_idx = ~0;
934 if (!(msg.u64 & 0x100))
936 if (number_of_fds != 1)
938 DBG_SOCK ("More than one fd received !");
942 template.read_function = vhost_user_callfd_read_ready;
943 template.file_descriptor = fds[0];
944 template.private_data =
945 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
946 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
949 vui->vrings[q].callfd_idx = ~0;
952 case VHOST_USER_SET_VRING_KICK:
953 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
954 vui->hw_if_index, msg.u64);
956 q = (u8) (msg.u64 & 0xFF);
958 if (vui->vrings[q].kickfd_idx != ~0)
960 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
961 vui->vrings[q].kickfd_idx);
962 unix_file_del (&unix_main, uf);
963 vui->vrings[q].kickfd_idx = ~0;
966 if (!(msg.u64 & 0x100))
968 if (number_of_fds != 1)
970 DBG_SOCK ("More than one fd received !");
974 template.read_function = vhost_user_kickfd_read_ready;
975 template.file_descriptor = fds[0];
976 template.private_data =
977 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
979 vui->vrings[q].kickfd_idx = unix_file_add (&unix_main, &template);
983 //When no kickfd is set, the queue is initialized as started
984 vui->vrings[q].kickfd_idx = ~0;
985 vui->vrings[q].started = 1;
990 case VHOST_USER_SET_VRING_ERR:
991 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
992 vui->hw_if_index, msg.u64);
994 q = (u8) (msg.u64 & 0xFF);
996 if (vui->vrings[q].errfd != -1)
997 close (vui->vrings[q].errfd);
999 if (!(msg.u64 & 0x100))
1001 if (number_of_fds != 1)
1004 vui->vrings[q].errfd = fds[0];
1007 vui->vrings[q].errfd = -1;
1011 case VHOST_USER_SET_VRING_BASE:
1012 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1013 vui->hw_if_index, msg.state.index, msg.state.num);
1015 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
1018 case VHOST_USER_GET_VRING_BASE:
1019 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1020 vui->hw_if_index, msg.state.index, msg.state.num);
1022 if (msg.state.index >= VHOST_VRING_MAX_N)
1024 DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
1025 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1029 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
1030 vhost_user_vring_close (vui, msg.state.index);
1032 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
1034 msg.size = sizeof (msg.state);
1037 case VHOST_USER_NONE:
1038 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
1042 case VHOST_USER_SET_LOG_BASE:
1044 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
1046 if (msg.size != sizeof (msg.log))
1049 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
1050 msg.size, sizeof (msg.log));
1055 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
1058 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1063 /* align size to 2M page */
1064 long page_sz = get_huge_page_size (fd);
1066 (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
1068 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1072 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
1073 map_sz, msg.log.offset, fd, vui->log_base_addr);
1075 if (vui->log_base_addr == MAP_FAILED)
1077 clib_warning ("failed to map memory. errno is %d", errno);
1081 vui->log_base_addr += msg.log.offset;
1082 vui->log_size = msg.log.size;
1085 msg.size = sizeof (msg.u64);
1090 case VHOST_USER_SET_LOG_FD:
1091 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1095 case VHOST_USER_GET_PROTOCOL_FEATURES:
1096 DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1100 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1101 (1 << VHOST_USER_PROTOCOL_F_MQ);
1102 msg.size = sizeof (msg.u64);
1105 case VHOST_USER_SET_PROTOCOL_FEATURES:
1106 DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
1107 vui->hw_if_index, msg.u64);
1109 vui->protocol_features = msg.u64;
1113 case VHOST_USER_GET_QUEUE_NUM:
1114 DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM", vui->hw_if_index);
1116 msg.u64 = VHOST_VRING_MAX_N;
1117 msg.size = sizeof (msg.u64);
1120 case VHOST_USER_SET_VRING_ENABLE:
1121 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1122 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1124 if (msg.state.index >= VHOST_VRING_MAX_N)
1126 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
1127 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1131 vui->vrings[msg.state.index].enabled = msg.state.num;
1135 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1140 /* if we need to reply */
1144 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1145 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1147 DBG_SOCK ("could not send message response");
1152 vhost_user_update_iface_state (vui);
1153 vlib_worker_thread_barrier_release (vlib_get_main ());
1157 vhost_user_if_disconnect (vui);
1158 vhost_user_update_iface_state (vui);
1159 vlib_worker_thread_barrier_release (vlib_get_main ());
1163 static clib_error_t *
1164 vhost_user_socket_error (unix_file_t * uf)
1166 vlib_main_t *vm = vlib_get_main ();
1167 vhost_user_main_t *vum = &vhost_user_main;
1168 vhost_user_intf_t *vui =
1169 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1171 DBG_SOCK ("socket error on if %d", vui->sw_if_index);
1172 vlib_worker_thread_barrier_sync (vm);
1173 vhost_user_if_disconnect (vui);
1174 vhost_user_rx_thread_placement ();
1175 vlib_worker_thread_barrier_release (vm);
1179 static clib_error_t *
1180 vhost_user_socksvr_accept_ready (unix_file_t * uf)
1182 int client_fd, client_len;
1183 struct sockaddr_un client;
1184 unix_file_t template = { 0 };
1185 vhost_user_main_t *vum = &vhost_user_main;
1186 vhost_user_intf_t *vui;
1188 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1190 client_len = sizeof (client);
1191 client_fd = accept (uf->file_descriptor,
1192 (struct sockaddr *) &client,
1193 (socklen_t *) & client_len);
1196 return clib_error_return_unix (0, "accept");
1198 DBG_SOCK ("New client socket for vhost interface %d", vui->sw_if_index);
1199 template.read_function = vhost_user_socket_read;
1200 template.error_function = vhost_user_socket_error;
1201 template.file_descriptor = client_fd;
1202 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1203 vui->unix_file_index = unix_file_add (&unix_main, &template);
1207 static clib_error_t *
1208 vhost_user_init (vlib_main_t * vm)
1210 clib_error_t *error;
1211 vhost_user_main_t *vum = &vhost_user_main;
1212 vlib_thread_main_t *tm = vlib_get_thread_main ();
1213 vlib_thread_registration_t *tr;
1216 error = vlib_call_init_function (vm, ip4_init);
1220 vum->coalesce_frames = 32;
1221 vum->coalesce_time = 1e-3;
1223 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1226 vec_foreach (cpu, vum->cpus)
1228 /* This is actually not necessary as validate already zeroes it
1229 * Just keeping the loop here for later because I am lazy. */
1230 cpu->rx_buffers_len = 0;
1233 /* find out which cpus will be used for input */
1234 vum->input_cpu_first_index = 0;
1235 vum->input_cpu_count = 1;
1236 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1237 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
1239 if (tr && tr->count > 0)
1241 vum->input_cpu_first_index = tr->first_index;
1242 vum->input_cpu_count = tr->count;
1245 vum->random = random_default_seed ();
1250 VLIB_INIT_FUNCTION (vhost_user_init);
1252 static clib_error_t *
1253 vhost_user_exit (vlib_main_t * vm)
1259 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1262 format_vhost_trace (u8 * s, va_list * va)
1264 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1265 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
1266 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
1267 vhost_user_main_t *vum = &vhost_user_main;
1268 vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
1269 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
1272 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
1274 uword indent = format_get_indent (s);
1276 s = format (s, "%U %U queue %d\n", format_white_space, indent,
1277 format_vnet_sw_interface_name, vnm, sw, t->qid);
1279 s = format (s, "%U virtio flags:\n", format_white_space, indent);
1281 if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
1282 s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
1283 foreach_virtio_trace_flags
1285 s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
1286 format_white_space, indent, t->first_desc_len);
1288 s = format (s, "%U flags 0x%02x gso_type %u\n",
1289 format_white_space, indent,
1290 t->hdr.hdr.flags, t->hdr.hdr.gso_type);
1292 if (vui->virtio_net_hdr_sz == 12)
1293 s = format (s, "%U num_buff %u",
1294 format_white_space, indent, t->hdr.num_buffers);
1300 vhost_user_rx_trace (vhost_trace_t * t,
1301 vhost_user_intf_t * vui, u16 qid,
1302 vlib_buffer_t * b, vhost_user_vring_t * txvq)
1304 vhost_user_main_t *vum = &vhost_user_main;
1305 u32 qsz_mask = txvq->qsz - 1;
1306 u32 last_avail_idx = txvq->last_avail_idx;
1307 u32 desc_current = txvq->avail->ring[last_avail_idx & qsz_mask];
1308 vring_desc_t *hdr_desc = 0;
1309 virtio_net_hdr_mrg_rxbuf_t *hdr;
1312 memset (t, 0, sizeof (*t));
1313 t->device_index = vui - vum->vhost_user_interfaces;
1316 hdr_desc = &txvq->desc[desc_current];
1317 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1319 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1320 /* Header is the first here */
1321 hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
1323 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1325 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1327 if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1328 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1330 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1333 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1335 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
1337 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
1341 u32 len = vui->virtio_net_hdr_sz;
1342 memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
1347 vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
1349 vhost_user_main_t *vum = &vhost_user_main;
1351 int fd = UNIX_GET_FD (vq->callfd_idx);
1352 int rv __attribute__ ((unused));
1353 /* TODO: pay attention to rv */
1354 rv = write (fd, &x, sizeof (x));
1355 vq->n_since_last_int = 0;
1356 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
1359 static_always_inline u32
1360 vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1361 u16 copy_len, u32 * map_hint)
1363 void *src0, *src1, *src2, *src3;
1364 if (PREDICT_TRUE (copy_len >= 4))
1366 if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
1368 if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
1371 while (PREDICT_TRUE (copy_len >= 4))
1377 (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
1380 (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
1383 CLIB_PREFETCH (src2, 64, LOAD);
1384 CLIB_PREFETCH (src3, 64, LOAD);
1386 clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
1387 clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
1394 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
1396 clib_memcpy ((void *) cpy->dst, src0, cpy->len);
1404 * Try to discard packets from the tx ring (VPP RX path).
1405 * Returns the number of discarded packets.
1408 vhost_user_rx_discard_packet (vlib_main_t * vm,
1409 vhost_user_intf_t * vui,
1410 vhost_user_vring_t * txvq, u32 discard_max)
1413 * On the RX side, each packet corresponds to one descriptor
1414 * (it is the same whether it is a shallow descriptor, chained, or indirect).
1415 * Therefore, discarding a packet is like discarding a descriptor.
1417 u32 discarded_packets = 0;
1418 u32 avail_idx = txvq->avail->idx;
1419 u16 qsz_mask = txvq->qsz - 1;
1420 while (discarded_packets != discard_max)
1422 if (avail_idx == txvq->last_avail_idx)
1425 u16 desc_chain_head =
1426 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1427 txvq->last_avail_idx++;
1428 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
1429 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1430 vhost_user_log_dirty_ring (vui, txvq,
1431 ring[txvq->last_used_idx & qsz_mask]);
1432 txvq->last_used_idx++;
1433 discarded_packets++;
1437 CLIB_MEMORY_BARRIER ();
1438 txvq->used->idx = txvq->last_used_idx;
1439 vhost_user_log_dirty_ring (vui, txvq, idx);
1440 return discarded_packets;
1444 * In case of overflow, we need to rewind the array of allocated buffers.
1447 vhost_user_input_rewind_buffers (vlib_main_t * vm,
1448 vhost_cpu_t * cpu, vlib_buffer_t * b_head)
1450 u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1451 vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
1452 b_current->current_length = 0;
1453 b_current->flags = 0;
1454 while (b_current != b_head)
1456 cpu->rx_buffers_len++;
1457 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1458 b_current = vlib_get_buffer (vm, bi_current);
1459 b_current->current_length = 0;
1460 b_current->flags = 0;
1465 vhost_user_if_input (vlib_main_t * vm,
1466 vhost_user_main_t * vum,
1467 vhost_user_intf_t * vui,
1468 u16 qid, vlib_node_runtime_t * node)
1470 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1471 u16 n_rx_packets = 0;
1474 u32 n_left_to_next, *to_next;
1475 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1476 u32 n_trace = vlib_get_trace_count (vm, node);
1479 u16 cpu_index = os_get_cpu_number ();
1483 /* do we have pending interrupts ? */
1484 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1485 f64 now = vlib_time_now (vm);
1487 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1488 vhost_user_send_call (vm, txvq);
1490 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1491 vhost_user_send_call (vm, rxvq);
1494 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
1497 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1500 if (PREDICT_FALSE (n_left == 0))
1503 if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
1506 * Discard input packet if interface is admin down or vring is not
1508 * "For example, for a networking device, in the disabled state
1509 * client must not supply any new RX packets, but must process
1510 * and discard any TX packets."
1512 vhost_user_rx_discard_packet (vm, vui, txvq,
1513 VHOST_USER_DOWN_DISCARD_COUNT);
1517 if (PREDICT_FALSE (n_left == txvq->qsz))
1520 * Informational error logging when VPP is not
1521 * receiving packets fast enough.
1523 vlib_error_count (vm, node->node_index,
1524 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1527 qsz_mask = txvq->qsz - 1;
1529 if (n_left > VLIB_FRAME_SIZE)
1530 n_left = VLIB_FRAME_SIZE;
1533 * For small packets (<2kB), we will not need more than one vlib buffer
1534 * per packet. In case packets are bigger, we will just yeld at some point
1535 * in the loop and come back later. This is not an issue as for big packet,
1536 * processing cost really comes from the memory copy.
1538 if (PREDICT_FALSE (vum->cpus[cpu_index].rx_buffers_len < n_left + 1))
1540 u32 curr_len = vum->cpus[cpu_index].rx_buffers_len;
1541 vum->cpus[cpu_index].rx_buffers_len +=
1542 vlib_buffer_alloc_from_free_list (vm,
1543 vum->cpus[cpu_index].rx_buffers +
1545 VHOST_USER_RX_BUFFERS_N - curr_len,
1546 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1549 (vum->cpus[cpu_index].rx_buffers_len <
1550 VHOST_USER_RX_BUFFER_STARVATION))
1552 /* In case of buffer starvation, discard some packets from the queue
1553 * and log the event.
1554 * We keep doing best effort for the remaining packets. */
1555 u32 flush = (n_left + 1 > vum->cpus[cpu_index].rx_buffers_len) ?
1556 n_left + 1 - vum->cpus[cpu_index].rx_buffers_len : 1;
1557 flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1560 vlib_increment_simple_counter (vnet_main.
1561 interface_main.sw_if_counters +
1562 VNET_INTERFACE_COUNTER_DROP,
1563 os_get_cpu_number (),
1564 vui->sw_if_index, flush);
1566 vlib_error_count (vm, vhost_user_input_node.index,
1567 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1573 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1575 while (n_left > 0 && n_left_to_next > 0)
1577 vlib_buffer_t *b_head, *b_current;
1580 u32 desc_data_offset;
1581 vring_desc_t *desc_table = txvq->desc;
1583 if (PREDICT_FALSE (vum->cpus[cpu_index].rx_buffers_len <= 1))
1585 /* Not enough rx_buffers
1586 * Note: We yeld on 1 so we don't need to do an additional
1587 * check for the next buffer prefetch.
1593 desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1594 vum->cpus[cpu_index].rx_buffers_len--;
1595 bi_current = (vum->cpus[cpu_index].rx_buffers)
1596 [vum->cpus[cpu_index].rx_buffers_len];
1597 b_head = b_current = vlib_get_buffer (vm, bi_current);
1598 to_next[0] = bi_current; //We do that now so we can forget about bi_current
1602 vlib_prefetch_buffer_with_index (vm,
1603 (vum->cpus[cpu_index].rx_buffers)
1604 [vum->cpus[cpu_index].
1605 rx_buffers_len - 1], LOAD);
1607 /* Just preset the used descriptor id and length for later */
1608 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_current;
1609 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1610 vhost_user_log_dirty_ring (vui, txvq,
1611 ring[txvq->last_used_idx & qsz_mask]);
1613 /* The buffer should already be initialized */
1614 b_head->total_length_not_including_first_buffer = 0;
1615 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1617 if (PREDICT_FALSE (n_trace))
1619 //TODO: next_index is not exactly known at that point
1620 vlib_trace_buffer (vm, node, next_index, b_head,
1621 /* follow_chain */ 0);
1623 vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1624 vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1626 vlib_set_trace_count (vm, node, n_trace);
1629 /* This depends on the setup but is very consistent
1630 * So I think the CPU branch predictor will make a pretty good job
1631 * at optimizing the decision. */
1632 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1634 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1637 if (PREDICT_FALSE (desc_table == 0))
1639 //FIXME: Handle error by shutdown the queue
1644 if (PREDICT_TRUE (vui->is_any_layout) ||
1645 (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
1647 /* ANYLAYOUT or single buffer */
1648 desc_data_offset = vui->virtio_net_hdr_sz;
1652 /* CSR case without ANYLAYOUT, skip 1st buffer */
1653 desc_data_offset = desc_table[desc_current].len;
1658 /* Get more input if necessary. Or end of packet. */
1659 if (desc_data_offset == desc_table[desc_current].len)
1661 if (PREDICT_FALSE (desc_table[desc_current].flags &
1664 desc_current = desc_table[desc_current].next;
1665 desc_data_offset = 0;
1673 /* Get more output if necessary. Or end of packet. */
1675 (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1678 (vum->cpus[cpu_index].rx_buffers_len == 0))
1680 /* Cancel speculation */
1685 * Checking if there are some left buffers.
1686 * If not, just rewind the used buffers and stop.
1687 * Note: Scheduled copies are not cancelled. This is
1688 * not an issue as they would still be valid. Useless,
1691 vhost_user_input_rewind_buffers (vm,
1692 &vum->cpus[cpu_index],
1698 /* Get next output */
1699 vum->cpus[cpu_index].rx_buffers_len--;
1701 (vum->cpus[cpu_index].rx_buffers)[vum->cpus
1702 [cpu_index].rx_buffers_len];
1703 b_current->next_buffer = bi_next;
1704 b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1705 bi_current = bi_next;
1706 b_current = vlib_get_buffer (vm, bi_current);
1709 /* Prepare a copy order executed later for the data */
1710 vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
1713 desc_table[desc_current].len - desc_data_offset;
1714 cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1715 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1716 cpy->dst = (uword) vlib_buffer_get_current (b_current);
1717 cpy->src = desc_table[desc_current].addr + desc_data_offset;
1719 desc_data_offset += cpy->len;
1721 b_current->current_length += cpy->len;
1722 b_head->total_length_not_including_first_buffer += cpy->len;
1726 CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1728 n_rx_bytes += b_head->total_length_not_including_first_buffer;
1731 b_head->total_length_not_including_first_buffer -=
1732 b_head->current_length;
1734 /* consume the descriptor and return it as used */
1735 txvq->last_avail_idx++;
1736 txvq->last_used_idx++;
1738 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1740 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1741 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1745 u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1747 /* redirect if feature path enabled */
1748 vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
1751 u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1752 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1753 to_next, n_left_to_next,
1760 * Although separating memory copies from virtio ring parsing
1761 * is beneficial, we can offer to perform the copies from time
1762 * to time in order to free some space in the ring.
1764 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1767 (vhost_user_input_copy (vui, vum->cpus[cpu_index].copy,
1768 copy_len, &map_hint)))
1771 ("Memory mapping error on interface hw_if_index=%d "
1772 "(Shutting down - Switch interface down and up to restart)",
1780 /* give buffers back to driver */
1781 CLIB_MEMORY_BARRIER ();
1782 txvq->used->idx = txvq->last_used_idx;
1783 vhost_user_log_dirty_ring (vui, txvq, idx);
1787 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1790 /* Do the memory copies */
1792 (vhost_user_input_copy (vui, vum->cpus[cpu_index].copy,
1793 copy_len, &map_hint)))
1795 clib_warning ("Memory mapping error on interface hw_if_index=%d "
1796 "(Shutting down - Switch interface down and up to restart)",
1801 /* give buffers back to driver */
1802 CLIB_MEMORY_BARRIER ();
1803 txvq->used->idx = txvq->last_used_idx;
1804 vhost_user_log_dirty_ring (vui, txvq, idx);
1806 /* interrupt (call) handling */
1807 if ((txvq->callfd_idx != ~0) && !(txvq->avail->flags & 1))
1809 txvq->n_since_last_int += n_rx_packets;
1811 if (txvq->n_since_last_int > vum->coalesce_frames)
1812 vhost_user_send_call (vm, txvq);
1815 /* increase rx counters */
1816 vlib_increment_combined_counter
1817 (vnet_main.interface_main.combined_sw_if_counters
1818 + VNET_INTERFACE_COUNTER_RX,
1819 os_get_cpu_number (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1821 vnet_device_increment_rx_packets (cpu_index, n_rx_packets);
1823 return n_rx_packets;
1827 vhost_user_input (vlib_main_t * vm,
1828 vlib_node_runtime_t * node, vlib_frame_t * f)
1830 vhost_user_main_t *vum = &vhost_user_main;
1831 uword n_rx_packets = 0;
1832 u32 cpu_index = os_get_cpu_number ();
1835 vhost_iface_and_queue_t *vhiq;
1836 vec_foreach (vhiq, vum->cpus[cpu_index].rx_queues)
1838 vhost_user_intf_t *vui =
1839 &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
1840 n_rx_packets += vhost_user_if_input (vm, vum, vui, vhiq->qid, node);
1843 return n_rx_packets;
1847 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1848 .function = vhost_user_input,
1849 .type = VLIB_NODE_TYPE_INPUT,
1850 .name = "vhost-user-input",
1851 .sibling_of = "device-input",
1853 /* Will be enabled if/when hardware is detected. */
1854 .state = VLIB_NODE_STATE_DISABLED,
1856 .format_buffer = format_ethernet_header_with_length,
1857 .format_trace = format_vhost_trace,
1859 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1860 .error_strings = vhost_user_input_func_error_strings,
1863 VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
1868 vhost_user_tx_trace (vhost_trace_t * t,
1869 vhost_user_intf_t * vui, u16 qid,
1870 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
1872 vhost_user_main_t *vum = &vhost_user_main;
1873 u32 qsz_mask = rxvq->qsz - 1;
1874 u32 last_avail_idx = rxvq->last_avail_idx;
1875 u32 desc_current = rxvq->avail->ring[last_avail_idx & qsz_mask];
1876 vring_desc_t *hdr_desc = 0;
1879 memset (t, 0, sizeof (*t));
1880 t->device_index = vui - vum->vhost_user_interfaces;
1883 hdr_desc = &rxvq->desc[desc_current];
1884 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1886 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1887 /* Header is the first here */
1888 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
1890 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1892 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1894 if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1895 !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1897 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1900 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1903 static_always_inline u32
1904 vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1905 u16 copy_len, u32 * map_hint)
1907 void *dst0, *dst1, *dst2, *dst3;
1908 if (PREDICT_TRUE (copy_len >= 4))
1910 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
1912 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
1914 while (PREDICT_TRUE (copy_len >= 4))
1920 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
1923 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
1926 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
1927 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
1929 clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
1930 clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
1932 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
1933 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
1940 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
1942 clib_memcpy (dst0, (void *) cpy->src, cpy->len);
1943 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
1952 vhost_user_tx (vlib_main_t * vm,
1953 vlib_node_runtime_t * node, vlib_frame_t * frame)
1955 u32 *buffers = vlib_frame_args (frame);
1956 u32 n_left = frame->n_vectors;
1957 vhost_user_main_t *vum = &vhost_user_main;
1958 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
1959 vhost_user_intf_t *vui =
1960 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1962 vhost_user_vring_t *rxvq;
1965 u32 cpu_index = os_get_cpu_number ();
1971 if (PREDICT_FALSE (!vui->admin_up))
1973 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
1977 if (PREDICT_FALSE (!vui->is_up))
1979 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1984 VHOST_VRING_IDX_RX (*vec_elt_at_index
1985 (vui->per_cpu_tx_qid, os_get_cpu_number ()));
1986 rxvq = &vui->vrings[qid];
1987 if (PREDICT_FALSE (vui->use_tx_spinlock))
1988 vhost_user_vring_lock (vui, qid);
1990 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
1993 error = VHOST_USER_TX_FUNC_ERROR_NONE;
1998 vlib_buffer_t *b0, *current_b0;
1999 u16 desc_head, desc_index, desc_len;
2000 vring_desc_t *desc_table;
2001 uword buffer_map_addr;
2005 if (PREDICT_TRUE (n_left > 1))
2006 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2008 b0 = vlib_get_buffer (vm, buffers[0]);
2010 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2012 vum->cpus[cpu_index].current_trace =
2013 vlib_add_trace (vm, node, b0,
2014 sizeof (*vum->cpus[cpu_index].current_trace));
2015 vhost_user_tx_trace (vum->cpus[cpu_index].current_trace,
2016 vui, qid / 2, b0, rxvq);
2019 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2021 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2025 desc_table = rxvq->desc;
2026 desc_head = desc_index =
2027 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2029 /* Go deeper in case of indirect descriptor
2030 * I don't know of any driver providing indirect for RX. */
2031 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2034 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2036 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2041 map_guest_mem (vui, rxvq->desc[desc_index].addr,
2044 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2050 desc_len = vui->virtio_net_hdr_sz;
2051 buffer_map_addr = desc_table[desc_index].addr;
2052 buffer_len = desc_table[desc_index].len;
2055 // Get a header from the header array
2056 virtio_net_hdr_mrg_rxbuf_t *hdr =
2057 &vum->cpus[cpu_index].tx_headers[tx_headers_len];
2060 hdr->hdr.gso_type = 0;
2061 hdr->num_buffers = 1; //This is local, no need to check
2063 // Prepare a copy order executed later for the header
2064 vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
2066 cpy->len = vui->virtio_net_hdr_sz;
2067 cpy->dst = buffer_map_addr;
2068 cpy->src = (uword) hdr;
2071 buffer_map_addr += vui->virtio_net_hdr_sz;
2072 buffer_len -= vui->virtio_net_hdr_sz;
2073 bytes_left = b0->current_length;
2077 if (buffer_len == 0)
2079 if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2081 //Next one is chained
2082 desc_index = desc_table[desc_index].next;
2083 buffer_map_addr = desc_table[desc_index].addr;
2084 buffer_len = desc_table[desc_index].len;
2086 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2088 virtio_net_hdr_mrg_rxbuf_t *hdr =
2089 &vum->cpus[cpu_index].tx_headers[tx_headers_len - 1];
2091 //Move from available to used buffer
2092 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id =
2094 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len =
2096 vhost_user_log_dirty_ring (vui, rxvq,
2097 ring[rxvq->last_used_idx &
2100 rxvq->last_avail_idx++;
2101 rxvq->last_used_idx++;
2106 (rxvq->last_avail_idx == rxvq->avail->idx))
2108 //Dequeue queued descriptors for this packet
2109 rxvq->last_used_idx -= hdr->num_buffers - 1;
2110 rxvq->last_avail_idx -= hdr->num_buffers - 1;
2111 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2115 desc_table = rxvq->desc;
2116 desc_head = desc_index =
2117 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2119 (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2121 //It is seriously unlikely that a driver will put indirect descriptor
2122 //after non-indirect descriptor.
2124 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2126 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2132 rxvq->desc[desc_index].addr,
2135 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2140 buffer_map_addr = desc_table[desc_index].addr;
2141 buffer_len = desc_table[desc_index].len;
2145 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2151 vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
2153 cpy->len = bytes_left;
2154 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2155 cpy->dst = buffer_map_addr;
2156 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2157 current_b0->current_length - bytes_left;
2159 bytes_left -= cpy->len;
2160 buffer_len -= cpy->len;
2161 buffer_map_addr += cpy->len;
2162 desc_len += cpy->len;
2164 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
2167 // Check if vlib buffer has more data. If not, get more or break.
2168 if (PREDICT_TRUE (!bytes_left))
2171 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2173 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2174 bytes_left = current_b0->current_length;
2184 //Move from available to used ring
2185 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
2186 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
2187 vhost_user_log_dirty_ring (vui, rxvq,
2188 ring[rxvq->last_used_idx & qsz_mask]);
2189 rxvq->last_avail_idx++;
2190 rxvq->last_used_idx++;
2192 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2194 vum->cpus[cpu_index].current_trace->hdr =
2195 vum->cpus[cpu_index].tx_headers[tx_headers_len - 1];
2198 n_left--; //At the end for error counting when 'goto done' is invoked
2203 //Do the memory copies
2205 (vhost_user_tx_copy (vui, vum->cpus[cpu_index].copy,
2206 copy_len, &map_hint)))
2208 clib_warning ("Memory mapping error on interface hw_if_index=%d "
2209 "(Shutting down - Switch interface down and up to restart)",
2214 CLIB_MEMORY_BARRIER ();
2215 rxvq->used->idx = rxvq->last_used_idx;
2216 vhost_user_log_dirty_ring (vui, rxvq, idx);
2219 * When n_left is set, error is always set to something too.
2220 * In case error is due to lack of remaining buffers, we go back up and
2222 * The idea is that it is better to waste some time on packets
2223 * that have been processed already than dropping them and get
2224 * more fresh packets with a good likelyhood that they will be dropped too.
2225 * This technique also gives more time to VM driver to pick-up packets.
2226 * In case the traffic flows from physical to virtual interfaces, this
2227 * technique will end-up leveraging the physical NIC buffer in order to
2228 * absorb the VM's CPU jitter.
2230 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2236 /* interrupt (call) handling */
2237 if ((rxvq->callfd_idx != ~0) && !(rxvq->avail->flags & 1))
2239 rxvq->n_since_last_int += frame->n_vectors - n_left;
2241 if (rxvq->n_since_last_int > vum->coalesce_frames)
2242 vhost_user_send_call (vm, rxvq);
2245 vhost_user_vring_unlock (vui, qid);
2248 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2250 vlib_error_count (vm, node->node_index, error, n_left);
2251 vlib_increment_simple_counter
2252 (vnet_main.interface_main.sw_if_counters
2253 + VNET_INTERFACE_COUNTER_DROP,
2254 os_get_cpu_number (), vui->sw_if_index, n_left);
2257 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2258 return frame->n_vectors;
2261 static clib_error_t *
2262 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
2265 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2266 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
2267 vhost_user_main_t *vum = &vhost_user_main;
2268 vhost_user_intf_t *vui =
2269 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
2271 vui->admin_up = is_up;
2274 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
2275 VNET_HW_INTERFACE_FLAG_LINK_UP);
2277 return /* no error */ 0;
2281 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2282 .name = "vhost-user",
2283 .tx_function = vhost_user_tx,
2284 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2285 .tx_function_error_strings = vhost_user_tx_func_error_strings,
2286 .format_device_name = format_vhost_user_interface_name,
2287 .name_renumber = vhost_user_name_renumber,
2288 .admin_up_down_function = vhost_user_interface_admin_up_down,
2289 .format_tx_trace = format_vhost_trace,
2292 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
2297 vhost_user_process (vlib_main_t * vm,
2298 vlib_node_runtime_t * rt, vlib_frame_t * f)
2300 vhost_user_main_t *vum = &vhost_user_main;
2301 vhost_user_intf_t *vui;
2302 struct sockaddr_un sun;
2304 unix_file_t template = { 0 };
2305 f64 timeout = 3153600000.0 /* 100 years */ ;
2306 uword *event_data = 0;
2308 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
2309 sun.sun_family = AF_UNIX;
2310 template.read_function = vhost_user_socket_read;
2311 template.error_function = vhost_user_socket_error;
2318 vlib_process_wait_for_event_or_clock (vm, timeout);
2319 vlib_process_get_events (vm, &event_data);
2320 vec_reset_length (event_data);
2325 pool_foreach (vui, vum->vhost_user_interfaces, {
2327 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
2328 if (vui->unix_file_index == ~0)
2330 /* try to connect */
2331 strncpy (sun.sun_path, (char *) vui->sock_filename,
2332 sizeof (sun.sun_path) - 1);
2334 /* Avoid hanging VPP if the other end does not accept */
2335 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2336 clib_unix_warning ("fcntl");
2338 if (connect (sockfd, (struct sockaddr *) &sun,
2339 sizeof (struct sockaddr_un)) == 0)
2341 /* Set the socket to blocking as it was before */
2342 if (fcntl(sockfd, F_SETFL, 0) < 0)
2343 clib_unix_warning ("fcntl2");
2345 vui->sock_errno = 0;
2346 template.file_descriptor = sockfd;
2347 template.private_data =
2348 vui - vhost_user_main.vhost_user_interfaces;
2349 vui->unix_file_index = unix_file_add (&unix_main, &template);
2351 //Re-open for next connect
2352 if ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
2353 clib_warning("Critical: Could not open unix socket");
2359 vui->sock_errno = errno;
2364 /* check if socket is alive */
2366 socklen_t len = sizeof (error);
2367 int fd = UNIX_GET_FD(vui->unix_file_index);
2369 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
2373 DBG_SOCK ("getsockopt returned %d", retval);
2374 vhost_user_if_disconnect (vui);
2385 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
2386 .function = vhost_user_process,
2387 .type = VLIB_NODE_TYPE_PROCESS,
2388 .name = "vhost-user-process",
2393 * Disables and reset interface structure.
2394 * It can then be either init again, or removed from used interfaces.
2397 vhost_user_term_if (vhost_user_intf_t * vui)
2401 // Delete configured thread pinning
2402 vec_reset_length (vui->workers);
2403 // disconnect interface sockets
2404 vhost_user_if_disconnect (vui);
2405 vhost_user_update_iface_state (vui);
2407 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2409 clib_mem_free ((void *) vui->vring_locks[q]);
2412 if (vui->unix_server_index != ~0)
2414 //Close server socket
2415 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
2416 vui->unix_server_index);
2417 unix_file_del (&unix_main, uf);
2418 vui->unix_server_index = ~0;
2423 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
2425 vhost_user_main_t *vum = &vhost_user_main;
2426 vhost_user_intf_t *vui;
2428 vnet_hw_interface_t *hwif;
2430 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2431 hwif->dev_class_index != vhost_user_dev_class.index)
2432 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2434 DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2435 hwif->name, hwif->dev_instance);
2437 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2439 // Disable and reset interface
2440 vhost_user_term_if (vui);
2442 // Reset renumbered iface
2443 if (hwif->dev_instance <
2444 vec_len (vum->show_dev_instance_by_real_dev_instance))
2445 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
2447 // Delete ethernet interface
2448 ethernet_delete_interface (vnm, vui->hw_if_index);
2451 pool_put (vum->vhost_user_interfaces, vui);
2457 * Open server unix socket on specified sock_filename.
2460 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
2463 struct sockaddr_un un = { };
2465 /* create listening socket */
2466 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2467 return VNET_API_ERROR_SYSCALL_ERROR_1;
2469 un.sun_family = AF_UNIX;
2470 strncpy ((char *) un.sun_path, (char *) sock_filename,
2471 sizeof (un.sun_path) - 1);
2473 /* remove if exists */
2474 unlink ((char *) sock_filename);
2476 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2478 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2482 if (listen (fd, 1) == -1)
2484 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2497 * Create ethernet interface for vhost user interface.
2500 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
2501 vhost_user_intf_t * vui, u8 * hwaddress)
2503 vhost_user_main_t *vum = &vhost_user_main;
2505 clib_error_t *error;
2507 /* create hw and sw interface */
2510 clib_memcpy (hwaddr, hwaddress, 6);
2514 random_u32 (&vum->random);
2515 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
2520 error = ethernet_register_interface
2522 vhost_user_dev_class.index,
2523 vui - vum->vhost_user_interfaces /* device instance */ ,
2524 hwaddr /* ethernet address */ ,
2525 &vui->hw_if_index, 0 /* flag change */ );
2528 clib_error_report (error);
2530 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
2531 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
2535 * Initialize vui with specified attributes
2538 vhost_user_vui_init (vnet_main_t * vnm,
2539 vhost_user_intf_t * vui,
2541 const char *sock_filename,
2542 u64 feature_mask, u32 * sw_if_index, u8 operation_mode)
2544 vnet_sw_interface_t *sw;
2545 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
2548 if (server_sock_fd != -1)
2550 unix_file_t template = { 0 };
2551 template.read_function = vhost_user_socksvr_accept_ready;
2552 template.file_descriptor = server_sock_fd;
2553 template.private_data = vui - vhost_user_main.vhost_user_interfaces; //hw index
2554 vui->unix_server_index = unix_file_add (&unix_main, &template);
2558 vui->unix_server_index = ~0;
2561 vui->sw_if_index = sw->sw_if_index;
2562 strncpy (vui->sock_filename, sock_filename,
2563 ARRAY_LEN (vui->sock_filename) - 1);
2564 vui->sock_errno = 0;
2566 vui->feature_mask = feature_mask;
2567 vui->unix_file_index = ~0;
2568 vui->log_base_addr = 0;
2569 vui->operation_mode = operation_mode;
2571 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2572 vhost_user_vring_init (vui, q);
2574 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
2577 *sw_if_index = vui->sw_if_index;
2579 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2581 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
2582 CLIB_CACHE_LINE_BYTES);
2583 memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
2586 vec_validate (vui->per_cpu_tx_qid,
2587 vlib_get_thread_main ()->n_vlib_mains - 1);
2588 vhost_user_tx_thread_placement (vui);
2592 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2593 const char *sock_filename,
2597 u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
2600 vhost_user_intf_t *vui = NULL;
2603 int server_sock_fd = -1;
2605 if (operation_mode != VHOST_USER_POLLING_MODE)
2606 return VNET_API_ERROR_UNIMPLEMENTED;
2608 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2610 return VNET_API_ERROR_INVALID_ARGUMENT;
2616 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
2622 pool_get (vhost_user_main.vhost_user_interfaces, vui);
2624 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
2625 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
2626 feature_mask, &sw_if_idx, operation_mode);
2629 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2632 *sw_if_index = sw_if_idx;
2634 // Process node must connect
2635 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2640 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2641 const char *sock_filename,
2644 u64 feature_mask, u8 renumber, u32 custom_dev_instance,
2647 vhost_user_main_t *vum = &vhost_user_main;
2648 vhost_user_intf_t *vui = NULL;
2650 int server_sock_fd = -1;
2652 vnet_hw_interface_t *hwif;
2654 if (operation_mode != VHOST_USER_POLLING_MODE)
2655 return VNET_API_ERROR_UNIMPLEMENTED;
2656 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2657 hwif->dev_class_index != vhost_user_dev_class.index)
2658 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2660 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2662 // First try to open server socket
2664 if ((rv = vhost_user_init_server_sock (sock_filename,
2665 &server_sock_fd)) != 0)
2668 vhost_user_term_if (vui);
2669 vhost_user_vui_init (vnm, vui, server_sock_fd,
2670 sock_filename, feature_mask, &sw_if_idx,
2674 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2676 // Process node must connect
2677 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2682 vhost_user_connect_command_fn (vlib_main_t * vm,
2683 unformat_input_t * input,
2684 vlib_cli_command_t * cmd)
2686 unformat_input_t _line_input, *line_input = &_line_input;
2687 u8 *sock_filename = NULL;
2690 u64 feature_mask = (u64) ~ (0ULL);
2692 u32 custom_dev_instance = ~0;
2695 clib_error_t *error = NULL;
2696 u8 operation_mode = VHOST_USER_POLLING_MODE;
2698 /* Get a line of input. */
2699 if (!unformat_user (input, unformat_line_input, line_input))
2702 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2704 if (unformat (line_input, "socket %s", &sock_filename))
2706 else if (unformat (line_input, "server"))
2708 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2712 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
2714 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
2720 error = clib_error_return (0, "unknown input `%U'",
2721 format_unformat_error, line_input);
2726 vnet_main_t *vnm = vnet_get_main ();
2729 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
2730 is_server, &sw_if_index, feature_mask,
2731 renumber, custom_dev_instance, hw,
2734 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
2738 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
2742 vec_free (sock_filename);
2743 unformat_free (line_input);
2749 vhost_user_delete_command_fn (vlib_main_t * vm,
2750 unformat_input_t * input,
2751 vlib_cli_command_t * cmd)
2753 unformat_input_t _line_input, *line_input = &_line_input;
2754 u32 sw_if_index = ~0;
2755 vnet_main_t *vnm = vnet_get_main ();
2756 clib_error_t *error = NULL;
2758 /* Get a line of input. */
2759 if (!unformat_user (input, unformat_line_input, line_input))
2762 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2764 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
2767 (line_input, "%U", unformat_vnet_sw_interface, vnm,
2770 vnet_hw_interface_t *hwif =
2771 vnet_get_sup_hw_interface (vnm, sw_if_index);
2773 vhost_user_dev_class.index != hwif->dev_class_index)
2775 error = clib_error_return (0, "Not a vhost interface");
2781 error = clib_error_return (0, "unknown input `%U'",
2782 format_unformat_error, line_input);
2787 vhost_user_delete_if (vnm, vm, sw_if_index);
2790 unformat_free (line_input);
2796 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
2797 vhost_user_intf_details_t ** out_vuids)
2800 vhost_user_main_t *vum = &vhost_user_main;
2801 vhost_user_intf_t *vui;
2802 vhost_user_intf_details_t *r_vuids = NULL;
2803 vhost_user_intf_details_t *vuid = NULL;
2804 u32 *hw_if_indices = 0;
2805 vnet_hw_interface_t *hi;
2812 pool_foreach (vui, vum->vhost_user_interfaces,
2813 vec_add1 (hw_if_indices, vui->hw_if_index);
2816 for (i = 0; i < vec_len (hw_if_indices); i++)
2818 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2819 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2821 vec_add2 (r_vuids, vuid, 1);
2822 vuid->operation_mode = vui->operation_mode;
2823 vuid->sw_if_index = vui->sw_if_index;
2824 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
2825 vuid->features = vui->features;
2826 vuid->num_regions = vui->nregions;
2827 vuid->is_server = vui->unix_server_index != ~0;
2828 vuid->sock_errno = vui->sock_errno;
2829 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
2830 ARRAY_LEN (vuid->sock_filename) - 1);
2832 s = format (s, "%v%c", hi->name, 0);
2834 strncpy ((char *) vuid->if_name, (char *) s,
2835 ARRAY_LEN (vuid->if_name) - 1);
2840 vec_free (hw_if_indices);
2842 *out_vuids = r_vuids;
2848 show_vhost_user_command_fn (vlib_main_t * vm,
2849 unformat_input_t * input,
2850 vlib_cli_command_t * cmd)
2852 clib_error_t *error = 0;
2853 vnet_main_t *vnm = vnet_get_main ();
2854 vhost_user_main_t *vum = &vhost_user_main;
2855 vhost_user_intf_t *vui;
2856 u32 hw_if_index, *hw_if_indices = 0;
2857 vnet_hw_interface_t *hi;
2859 vhost_iface_and_queue_t *vhiq;
2869 struct feat_struct *feat_entry;
2871 static struct feat_struct feat_array[] = {
2872 #define _(s,b) { .str = #s, .bit = b, },
2873 foreach_virtio_net_feature
2878 #define foreach_protocol_feature \
2879 _(VHOST_USER_PROTOCOL_F_MQ) \
2880 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2882 static struct feat_struct proto_feat_array[] = {
2883 #define _(s) { .str = #s, .bit = s},
2884 foreach_protocol_feature
2889 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2892 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2894 vec_add1 (hw_if_indices, hw_if_index);
2896 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2900 error = clib_error_return (0, "unknown input `%U'",
2901 format_unformat_error, input);
2905 if (vec_len (hw_if_indices) == 0)
2907 pool_foreach (vui, vum->vhost_user_interfaces,
2908 vec_add1 (hw_if_indices, vui->hw_if_index);
2911 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2912 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2913 vum->coalesce_frames, vum->coalesce_time);
2915 for (i = 0; i < vec_len (hw_if_indices); i++)
2917 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2918 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2919 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
2920 hi->name, hw_if_indices[i]);
2922 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2923 " features mask (0x%llx): \n"
2924 " features (0x%llx): \n",
2925 vui->virtio_net_hdr_sz, vui->feature_mask,
2928 feat_entry = (struct feat_struct *) &feat_array;
2929 while (feat_entry->str)
2931 if (vui->features & (1ULL << feat_entry->bit))
2932 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2937 vlib_cli_output (vm, " protocol features (0x%llx)",
2938 vui->protocol_features);
2939 feat_entry = (struct feat_struct *) &proto_feat_array;
2940 while (feat_entry->str)
2942 if (vui->protocol_features & (1ULL << feat_entry->bit))
2943 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2948 vlib_cli_output (vm, "\n");
2950 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2952 (vui->unix_server_index != ~0) ? "server" : "client",
2953 strerror (vui->sock_errno));
2955 vlib_cli_output (vm, " rx placement: ");
2956 vec_foreach (vhc, vum->cpus)
2958 vec_foreach (vhiq, vhc->rx_queues)
2960 if (vhiq->vhost_iface_index == vui - vum->vhost_user_interfaces)
2961 vlib_cli_output (vm, " thread %d on vring %d\n",
2962 vhc - vum->cpus, VHOST_VRING_IDX_TX (vhiq->qid));
2966 vlib_cli_output (vm, " tx placement: %s\n",
2967 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2969 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2971 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2972 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2975 vlib_cli_output (vm, "\n");
2977 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2981 vlib_cli_output (vm,
2982 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2983 vlib_cli_output (vm,
2984 " ====== ===== ================== ================== ================== ================== ==================\n");
2986 for (j = 0; j < vui->nregions; j++)
2988 vlib_cli_output (vm,
2989 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2990 j, vui->region_mmap_fd[j],
2991 vui->regions[j].guest_phys_addr,
2992 vui->regions[j].memory_size,
2993 vui->regions[j].userspace_addr,
2994 vui->regions[j].mmap_offset,
2995 pointer_to_uword (vui->region_mmap_addr[j]));
2997 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2999 if (!vui->vrings[q].started)
3002 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
3003 (q & 1) ? "RX" : "TX",
3004 vui->vrings[q].enabled ? "" : " disabled");
3006 vlib_cli_output (vm,
3007 " qsz %d last_avail_idx %d last_used_idx %d\n",
3008 vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
3009 vui->vrings[q].last_used_idx);
3011 if (vui->vrings[q].avail && vui->vrings[q].used)
3012 vlib_cli_output (vm,
3013 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
3014 vui->vrings[q].avail->flags,
3015 vui->vrings[q].avail->idx,
3016 vui->vrings[q].used->flags,
3017 vui->vrings[q].used->idx);
3019 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
3020 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
3021 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
3022 kickfd, callfd, vui->vrings[q].errfd);
3026 vlib_cli_output (vm, "\n descriptor table:\n");
3027 vlib_cli_output (vm,
3028 " id addr len flags next user_addr\n");
3029 vlib_cli_output (vm,
3030 " ===== ================== ===== ====== ===== ==================\n");
3031 for (j = 0; j < vui->vrings[q].qsz; j++)
3034 vlib_cli_output (vm,
3035 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3036 j, vui->vrings[q].desc[j].addr,
3037 vui->vrings[q].desc[j].len,
3038 vui->vrings[q].desc[j].flags,
3039 vui->vrings[q].desc[j].next,
3040 pointer_to_uword (map_guest_mem
3042 vui->vrings[q].desc[j].
3047 vlib_cli_output (vm, "\n");
3050 vec_free (hw_if_indices);
3059 * Create a vHost User interface. Once created, a new virtual interface
3060 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3061 * is the next free index.
3063 * There are several parameters associated with a vHost interface:
3065 * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3066 * VPP to manage the vHost interface. If socket does not already exist, VPP will
3067 * create the socket.
3069 * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3070 * linux socket. If not provided, VPP will be the client.
3072 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3073 * startup. By default, all supported features will be advertised. Otherwise,
3074 * provide the set of features desired.
3075 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3076 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3077 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3078 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3079 * - 0x004000000 (26) - VHOST_F_LOG_ALL
3080 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3081 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3082 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3083 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3085 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3086 * X:X:X:X:X:X unix or X.X.X cisco format.
3088 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3089 * in the name to be specified. If instance already exists, name will be used
3090 * anyway and multiple instances will have the same name. Use with caution.
3093 * Example of how to create a vhost interface with VPP as the client and all features enabled:
3094 * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3095 * VirtualEthernet0/0/0
3097 * Example of how to create a vhost interface with VPP as the server and with just
3098 * multiple queues enabled:
3099 * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3100 * VirtualEthernet0/0/1
3102 * Once the vHost interface is created, enable the interface using:
3103 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3106 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3107 .path = "create vhost-user",
3108 .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>]",
3109 .function = vhost_user_connect_command_fn,
3114 * Delete a vHost User interface using the interface name or the
3115 * software interface index. Use the '<em>show interfaces</em>'
3116 * command to determine the software interface index. On deletion,
3117 * the linux socket will not be deleted.
3120 * Example of how to delete a vhost interface by name:
3121 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3122 * Example of how to delete a vhost interface by software interface index:
3123 * @cliexcmd{delete vhost-user sw_if_index 1}
3126 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3127 .path = "delete vhost-user",
3128 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
3129 .function = vhost_user_delete_command_fn,
3133 * Display the attributes of a single vHost User interface (provide interface
3134 * name), multiple vHost User interfaces (provide a list of interface names seperated
3135 * by spaces) or all Vhost User interfaces (omit an interface name to display all
3136 * vHost interfaces).
3140 * Example of how to display a vhost interface:
3141 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3142 * Virtio vhost-user interfaces
3144 * coalesce frames 32 time 1e-3
3145 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3146 * virtio_net_hdr_sz 12
3147 * features mask (0xffffffffffffffff):
3148 * features (0x50408000):
3149 * VIRTIO_NET_F_MRG_RXBUF (15)
3150 * VIRTIO_NET_F_MQ (22)
3151 * VIRTIO_F_INDIRECT_DESC (28)
3152 * VHOST_USER_F_PROTOCOL_FEATURES (30)
3153 * protocol features (0x3)
3154 * VHOST_USER_PROTOCOL_F_MQ (0)
3155 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3157 * socket filename /tmp/vhost1.sock type client errno "Success"
3160 * thread 1 on vring 1
3161 * thread 1 on vring 5
3162 * thread 2 on vring 3
3163 * thread 2 on vring 7
3164 * tx placement: spin-lock
3165 * thread 0 on vring 0
3166 * thread 1 on vring 2
3167 * thread 2 on vring 0
3169 * Memory regions (total 2)
3170 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3171 * ====== ===== ================== ================== ================== ================== ==================
3172 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3173 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3176 * qsz 256 last_avail_idx 0 last_used_idx 0
3177 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3178 * kickfd 62 callfd 64 errfd -1
3181 * qsz 256 last_avail_idx 0 last_used_idx 0
3182 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3183 * kickfd 65 callfd 66 errfd -1
3186 * qsz 256 last_avail_idx 0 last_used_idx 0
3187 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3188 * kickfd 63 callfd 70 errfd -1
3191 * qsz 256 last_avail_idx 0 last_used_idx 0
3192 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3193 * kickfd 72 callfd 74 errfd -1
3195 * Virtqueue 4 (TX disabled)
3196 * qsz 256 last_avail_idx 0 last_used_idx 0
3197 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3198 * kickfd 76 callfd 78 errfd -1
3200 * Virtqueue 5 (RX disabled)
3201 * qsz 256 last_avail_idx 0 last_used_idx 0
3202 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3203 * kickfd 80 callfd 82 errfd -1
3205 * Virtqueue 6 (TX disabled)
3206 * qsz 256 last_avail_idx 0 last_used_idx 0
3207 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3208 * kickfd 84 callfd 86 errfd -1
3210 * Virtqueue 7 (RX disabled)
3211 * qsz 256 last_avail_idx 0 last_used_idx 0
3212 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3213 * kickfd 88 callfd 90 errfd -1
3217 * The optional '<em>descriptors</em>' parameter will display the same output as
3218 * the previous example but will include the descriptor table for each queue.
3219 * The output is truncated below:
3220 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3221 * Virtio vhost-user interfaces
3223 * coalesce frames 32 time 1e-3
3224 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3225 * virtio_net_hdr_sz 12
3226 * features mask (0xffffffffffffffff):
3227 * features (0x50408000):
3228 * VIRTIO_NET_F_MRG_RXBUF (15)
3229 * VIRTIO_NET_F_MQ (22)
3232 * qsz 256 last_avail_idx 0 last_used_idx 0
3233 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3234 * kickfd 62 callfd 64 errfd -1
3237 * id addr len flags next user_addr
3238 * ===== ================== ===== ====== ===== ==================
3239 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3240 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3241 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3242 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3243 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3244 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3245 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3246 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3247 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3248 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3249 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3251 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3252 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3253 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3254 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3255 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3256 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3257 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3260 * qsz 256 last_avail_idx 0 last_used_idx 0
3266 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3267 .path = "show vhost-user",
3268 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
3269 .function = show_vhost_user_command_fn,
3273 static clib_error_t *
3274 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
3276 vhost_user_main_t *vum = &vhost_user_main;
3278 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3280 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
3282 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
3284 else if (unformat (input, "dont-dump-memory"))
3285 vum->dont_dump_vhost_user_memory = 1;
3287 return clib_error_return (0, "unknown input `%U'",
3288 format_unformat_error, input);
3294 /* vhost-user { ... } configuration. */
3295 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3298 vhost_user_unmap_all (void)
3300 vhost_user_main_t *vum = &vhost_user_main;
3301 vhost_user_intf_t *vui;
3303 if (vum->dont_dump_vhost_user_memory)
3305 pool_foreach (vui, vum->vhost_user_interfaces,
3306 unmap_all_mem_regions (vui);
3311 static clib_error_t *
3312 vhost_thread_command_fn (vlib_main_t * vm,
3313 unformat_input_t * input, vlib_cli_command_t * cmd)
3315 unformat_input_t _line_input, *line_input = &_line_input;
3316 u32 worker_thread_index;
3320 clib_error_t *error = NULL;
3322 /* Get a line of input. */
3323 if (!unformat_user (input, unformat_line_input, line_input))
3327 (line_input, "%U %d", unformat_vnet_sw_interface, vnet_get_main (),
3328 &sw_if_index, &worker_thread_index))
3330 error = clib_error_return (0, "unknown input `%U'",
3331 format_unformat_error, line_input);
3335 if (unformat (line_input, "del"))
3339 vhost_user_thread_placement (sw_if_index, worker_thread_index, del)))
3341 error = clib_error_return (0, "vhost_user_thread_placement returned %d",
3347 unformat_free (line_input);
3354 * This command is used to move the RX processing for the given
3355 * interfaces to the provided thread. If the '<em>del</em>' option is used,
3356 * the forced thread assignment is removed and the thread assigment is
3357 * reassigned automatically. Use '<em>show vhost-user <interface></em>'
3358 * to see the thread assignment.
3361 * Example of how to move the RX processing for a given interface to a given thread:
3362 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1}
3363 * Example of how to remove the forced thread assignment for a given interface:
3364 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1 del}
3367 VLIB_CLI_COMMAND (vhost_user_thread_command, static) = {
3368 .path = "vhost thread",
3369 .short_help = "vhost thread <iface> <worker-index> [del]",
3370 .function = vhost_thread_command_fn,
3375 * fd.io coding-style-patch-verification: ON
3378 * eval: (c-set-style "gnu")