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_DEBUG_VQ 0
54 #define DBG_SOCK(args...) \
56 vhost_user_main_t *_vum = &vhost_user_main; \
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 * On the transmit side, we keep processing the buffers from vlib in the while
91 * loop and prepare the copy order to be executed later. However, the static
92 * array which we keep the copy order is limited to VHOST_USER_COPY_ARRAY_N
93 * entries. In order to not corrupt memory, we have to do the copy when the
94 * static array reaches the copy threshold. We subtract 40 in case the code
95 * goes into the inner loop for a maximum of 64k frames which may require
98 #define VHOST_USER_TX_COPY_THRESHOLD (VHOST_USER_COPY_ARRAY_N - 40)
100 #define UNIX_GET_FD(unixfd_idx) \
101 (unixfd_idx != ~0) ? \
102 pool_elt_at_index (file_main.file_pool, \
103 unixfd_idx)->file_descriptor : -1;
105 #define foreach_virtio_trace_flags \
106 _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
107 _ (SINGLE_DESC, 1, "Single descriptor packet") \
108 _ (INDIRECT, 2, "Indirect descriptor") \
109 _ (MAP_ERROR, 4, "Memory mapping error")
113 #define _(n,i,s) VIRTIO_TRACE_F_##n,
114 foreach_virtio_trace_flags
116 } virtio_trace_flag_t;
118 vlib_node_registration_t vhost_user_input_node;
120 #define foreach_vhost_user_tx_func_error \
121 _(NONE, "no error") \
122 _(NOT_READY, "vhost vring not ready") \
123 _(DOWN, "vhost interface is down") \
124 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
125 _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
126 _(MMAP_FAIL, "mmap failure") \
127 _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
131 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
132 foreach_vhost_user_tx_func_error
134 VHOST_USER_TX_FUNC_N_ERROR,
135 } vhost_user_tx_func_error_t;
137 static char *vhost_user_tx_func_error_strings[] = {
139 foreach_vhost_user_tx_func_error
143 #define foreach_vhost_user_input_func_error \
144 _(NO_ERROR, "no error") \
145 _(NO_BUFFER, "no available buffer") \
146 _(MMAP_FAIL, "mmap failure") \
147 _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
148 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
149 _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
153 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
154 foreach_vhost_user_input_func_error
156 VHOST_USER_INPUT_FUNC_N_ERROR,
157 } vhost_user_input_func_error_t;
159 static char *vhost_user_input_func_error_strings[] = {
161 foreach_vhost_user_input_func_error
166 static vhost_user_main_t vhost_user_main = {
170 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
171 .name = "vhost-user",
176 format_vhost_user_interface_name (u8 * s, va_list * args)
178 u32 i = va_arg (*args, u32);
179 u32 show_dev_instance = ~0;
180 vhost_user_main_t *vum = &vhost_user_main;
182 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
183 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
185 if (show_dev_instance != ~0)
186 i = show_dev_instance;
188 s = format (s, "VirtualEthernet0/0/%d", i);
193 vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
195 // FIXME: check if the new dev instance is already used
196 vhost_user_main_t *vum = &vhost_user_main;
197 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
198 hi->dev_instance, ~0);
200 vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
203 DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
204 hi->dev_instance, new_dev_instance);
209 static_always_inline void *
210 map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint)
213 if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) &&
214 ((vui->regions[i].guest_phys_addr +
215 vui->regions[i].memory_size) > addr)))
217 return (void *) (vui->region_mmap_addr[i] + addr -
218 vui->regions[i].guest_phys_addr);
221 __m128i rl, rh, al, ah, r;
222 al = _mm_set1_epi64x (addr + 1);
223 ah = _mm_set1_epi64x (addr);
225 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]);
226 rl = _mm_cmpgt_epi64 (al, rl);
227 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]);
228 rh = _mm_cmpgt_epi64 (rh, ah);
229 r = _mm_and_si128 (rl, rh);
231 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]);
232 rl = _mm_cmpgt_epi64 (al, rl);
233 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]);
234 rh = _mm_cmpgt_epi64 (rh, ah);
235 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22);
237 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]);
238 rl = _mm_cmpgt_epi64 (al, rl);
239 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]);
240 rh = _mm_cmpgt_epi64 (rh, ah);
241 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44);
243 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]);
244 rl = _mm_cmpgt_epi64 (al, rl);
245 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]);
246 rh = _mm_cmpgt_epi64 (rh, ah);
247 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
249 r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
250 i = __builtin_ctzll (_mm_movemask_epi8 (r) |
251 (1 << VHOST_MEMORY_MAX_NREGIONS));
253 if (i < vui->nregions)
256 return (void *) (vui->region_mmap_addr[i] + addr -
257 vui->regions[i].guest_phys_addr);
261 for (i = 0; i < vui->nregions; i++)
263 if ((vui->regions[i].guest_phys_addr <= addr) &&
264 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
268 return (void *) (vui->region_mmap_addr[i] + addr -
269 vui->regions[i].guest_phys_addr);
273 DBG_VQ ("failed to map guest mem addr %llx", addr);
279 map_user_mem (vhost_user_intf_t * vui, uword addr)
282 for (i = 0; i < vui->nregions; i++)
284 if ((vui->regions[i].userspace_addr <= addr) &&
285 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
288 return (void *) (vui->region_mmap_addr[i] + addr -
289 vui->regions[i].userspace_addr);
296 get_huge_page_size (int fd)
304 unmap_all_mem_regions (vhost_user_intf_t * vui)
307 for (i = 0; i < vui->nregions; i++)
309 if (vui->region_mmap_addr[i] != (void *) -1)
312 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
314 ssize_t map_sz = (vui->regions[i].memory_size +
315 vui->regions[i].mmap_offset +
316 page_sz - 1) & ~(page_sz - 1);
319 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
323 ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
324 vui->region_mmap_addr[i], map_sz, page_sz);
326 vui->region_mmap_addr[i] = (void *) -1;
330 clib_warning ("failed to unmap memory region (errno %d)",
333 close (vui->region_mmap_fd[i]);
340 vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
342 //Let's try to assign one queue to each thread
344 u32 thread_index = 0;
345 vui->use_tx_spinlock = 0;
348 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
350 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
351 if (!rxvq->started || !rxvq->enabled)
354 vui->per_cpu_tx_qid[thread_index] = qid;
356 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
359 //We need to loop, meaning the spinlock has to be used
360 vui->use_tx_spinlock = 1;
361 if (thread_index == 0)
363 //Could not find a single valid one
364 for (thread_index = 0;
365 thread_index < vlib_get_thread_main ()->n_vlib_mains;
368 vui->per_cpu_tx_qid[thread_index] = 0;
376 * @brief Unassign existing interface/queue to thread mappings and re-assign
377 * new interface/queue to thread mappings
380 vhost_user_rx_thread_placement ()
382 vhost_user_main_t *vum = &vhost_user_main;
383 vhost_user_intf_t *vui;
384 vhost_user_vring_t *txvq;
385 vnet_main_t *vnm = vnet_get_main ();
390 // Scrap all existing mappings for all interfaces/queues
392 pool_foreach (vui, vum->vhost_user_interfaces, {
393 vec_foreach (queue, vui->rx_queues)
395 rv = vnet_hw_interface_unassign_rx_thread (vnm, vui->hw_if_index,
398 clib_warning ("Warning: unable to unassign interface %d, "
399 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
401 vec_reset_length (vui->rx_queues);
405 // Create the rx_queues for all interfaces
407 pool_foreach (vui, vum->vhost_user_interfaces, {
408 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
410 txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
413 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
414 /* Set polling as the default */
415 txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
416 vec_add1 (vui->rx_queues, qid);
422 // Assign new mappings for all interfaces/queues
424 pool_foreach (vui, vum->vhost_user_interfaces, {
425 vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
426 vhost_user_input_node.index);
427 vec_foreach (queue, vui->rx_queues)
429 vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, *queue,
431 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
432 rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, *queue,
435 clib_warning ("Warning: unable to set rx mode for interface %d, "
436 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
442 /** @brief Returns whether at least one TX and one RX vring are enabled */
444 vhost_user_intf_ready (vhost_user_intf_t * vui)
446 int i, found[2] = { }; //RX + TX
448 for (i = 0; i < VHOST_VRING_MAX_N; i++)
449 if (vui->vrings[i].started && vui->vrings[i].enabled)
452 return found[0] && found[1];
456 vhost_user_update_iface_state (vhost_user_intf_t * vui)
458 /* if we have pointers to descriptor table, go up */
459 int is_up = vhost_user_intf_ready (vui);
460 if (is_up != vui->is_up)
462 DBG_SOCK ("interface %d %s", vui->sw_if_index,
463 is_up ? "ready" : "down");
464 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
465 is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP :
469 vhost_user_rx_thread_placement ();
470 vhost_user_tx_thread_placement (vui);
474 vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
477 vnet_main_t *vnm = vnet_get_main ();
481 /* Only care about the odd number, or TX, virtqueue */
484 if (vhost_user_intf_ready (vui))
485 // qid >> 1 is to convert virtqueue number to vring queue index
486 vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1);
489 static clib_error_t *
490 vhost_user_callfd_read_ready (clib_file_t * uf)
492 __attribute__ ((unused)) int n;
495 n = read (uf->file_descriptor, ((char *) &buff), 8);
500 static clib_error_t *
501 vhost_user_kickfd_read_ready (clib_file_t * uf)
503 __attribute__ ((unused)) int n;
505 vhost_user_intf_t *vui =
506 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
507 uf->private_data >> 8);
508 u32 qid = uf->private_data & 0xff;
510 n = read (uf->file_descriptor, ((char *) &buff), 8);
511 DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
512 if (!vui->vrings[qid].started ||
513 (vhost_user_intf_ready (vui) != vui->is_up))
515 vlib_worker_thread_barrier_sync (vlib_get_main ());
516 vui->vrings[qid].started = 1;
517 vhost_user_update_iface_state (vui);
518 vlib_worker_thread_barrier_release (vlib_get_main ());
521 vhost_user_set_interrupt_pending (vui, uf->private_data);
526 * @brief Try once to lock the vring
527 * @return 0 on success, non-zero on failure.
530 vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
532 return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
536 * @brief Spin until the vring is successfully locked
539 vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
541 while (vhost_user_vring_try_lock (vui, qid))
546 * @brief Unlock the vring lock
549 vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
551 *vui->vring_locks[qid] = 0;
555 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
557 vhost_user_vring_t *vring = &vui->vrings[qid];
558 memset (vring, 0, sizeof (*vring));
559 vring->kickfd_idx = ~0;
560 vring->callfd_idx = ~0;
564 * We have a bug with some qemu 2.5, and this may be a fix.
565 * Feel like interpretation holy text, but this is from vhost-user.txt.
567 * One queue pair is enabled initially. More queues are enabled
568 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
570 * Don't know who's right, but this is what DPDK does.
572 if (qid == 0 || qid == 1)
577 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
579 vhost_user_vring_t *vring = &vui->vrings[qid];
580 if (vring->kickfd_idx != ~0)
582 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
584 clib_file_del (&file_main, uf);
585 vring->kickfd_idx = ~0;
587 if (vring->callfd_idx != ~0)
589 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
591 clib_file_del (&file_main, uf);
592 vring->callfd_idx = ~0;
594 if (vring->errfd != -1)
596 close (vring->errfd);
599 vhost_user_vring_init (vui, qid);
603 vhost_user_if_disconnect (vhost_user_intf_t * vui)
605 vnet_main_t *vnm = vnet_get_main ();
608 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
610 if (vui->clib_file_index != ~0)
612 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
613 vui->clib_file_index = ~0;
618 for (q = 0; q < VHOST_VRING_MAX_N; q++)
619 vhost_user_vring_close (vui, q);
621 unmap_all_mem_regions (vui);
622 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
625 #define VHOST_LOG_PAGE 0x1000
626 static_always_inline void
627 vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
628 u64 addr, u64 len, u8 is_host_address)
630 if (PREDICT_TRUE (vui->log_base_addr == 0
631 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
637 addr = pointer_to_uword (map_user_mem (vui, (uword) addr));
639 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
641 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
645 CLIB_MEMORY_BARRIER ();
646 u64 page = addr / VHOST_LOG_PAGE;
647 while (page * VHOST_LOG_PAGE < addr + len)
649 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
654 static_always_inline void
655 vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
657 vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
660 #define vhost_user_log_dirty_ring(vui, vq, member) \
661 if (PREDICT_FALSE(vq->log_used)) { \
662 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
663 sizeof(vq->used->member)); \
666 static clib_error_t *
667 vhost_user_socket_read (clib_file_t * uf)
670 int fd, number_of_fds = 0;
671 int fds[VHOST_MEMORY_MAX_NREGIONS];
672 vhost_user_msg_t msg;
675 vhost_user_main_t *vum = &vhost_user_main;
676 vhost_user_intf_t *vui;
677 struct cmsghdr *cmsg;
679 clib_file_t template = { 0 };
680 vnet_main_t *vnm = vnet_get_main ();
682 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
684 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
686 memset (&mh, 0, sizeof (mh));
687 memset (control, 0, sizeof (control));
689 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
692 /* set the payload */
693 iov[0].iov_base = (void *) &msg;
694 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
698 mh.msg_control = control;
699 mh.msg_controllen = sizeof (control);
701 n = recvmsg (uf->file_descriptor, &mh, 0);
703 /* Stop workers to avoid end of the world */
704 vlib_worker_thread_barrier_sync (vlib_get_main ());
706 if (n != VHOST_USER_MSG_HDR_SZ)
710 DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
714 DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
715 n, VHOST_USER_MSG_HDR_SZ);
720 if (mh.msg_flags & MSG_CTRUNC)
722 DBG_SOCK ("MSG_CTRUNC is set");
726 cmsg = CMSG_FIRSTHDR (&mh);
728 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
729 (cmsg->cmsg_type == SCM_RIGHTS) &&
730 (cmsg->cmsg_len - CMSG_LEN (0) <=
731 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
733 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
734 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
737 /* version 1, no reply bit set */
738 if ((msg.flags & 7) != 1)
740 DBG_SOCK ("malformed message received. closing socket");
747 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
751 DBG_SOCK ("read failed %s", strerror (errno));
754 else if (rv != msg.size)
756 DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
763 case VHOST_USER_GET_FEATURES:
765 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
766 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
767 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
768 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
769 (1ULL << FEAT_VHOST_F_LOG_ALL) |
770 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
771 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
772 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
773 (1ULL << FEAT_VIRTIO_F_VERSION_1);
774 msg.u64 &= vui->feature_mask;
775 msg.size = sizeof (msg.u64);
776 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
777 vui->hw_if_index, msg.u64);
780 case VHOST_USER_SET_FEATURES:
781 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
782 vui->hw_if_index, msg.u64);
784 vui->features = msg.u64;
787 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
788 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
789 vui->virtio_net_hdr_sz = 12;
791 vui->virtio_net_hdr_sz = 10;
794 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
796 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
797 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
800 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
801 vhost_user_vring_close(&vui->vrings[q]); */
805 case VHOST_USER_SET_MEM_TABLE:
806 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
807 vui->hw_if_index, msg.memory.nregions);
809 if ((msg.memory.nregions < 1) ||
810 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
813 DBG_SOCK ("number of mem regions must be between 1 and %i",
814 VHOST_MEMORY_MAX_NREGIONS);
819 if (msg.memory.nregions != number_of_fds)
821 DBG_SOCK ("each memory region must have FD");
824 unmap_all_mem_regions (vui);
825 for (i = 0; i < msg.memory.nregions; i++)
827 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
828 sizeof (vhost_user_memory_region_t));
830 long page_sz = get_huge_page_size (fds[i]);
832 /* align size to 2M page */
833 ssize_t map_sz = (vui->regions[i].memory_size +
834 vui->regions[i].mmap_offset +
835 page_sz - 1) & ~(page_sz - 1);
837 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
838 MAP_SHARED, fds[i], 0);
839 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
840 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
841 vui->regions[i].memory_size;
844 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
845 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
848 if (vui->region_mmap_addr[i] == MAP_FAILED)
850 clib_warning ("failed to map memory. errno is %d", errno);
853 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
854 vui->region_mmap_fd[i] = fds[i];
860 case VHOST_USER_SET_VRING_NUM:
861 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
862 vui->hw_if_index, msg.state.index, msg.state.num);
864 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
865 (msg.state.num == 0) || /* it cannot be zero */
866 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
868 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
871 case VHOST_USER_SET_VRING_ADDR:
872 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
873 vui->hw_if_index, msg.state.index);
875 if (msg.state.index >= VHOST_VRING_MAX_N)
877 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
878 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
882 if (msg.size < sizeof (msg.addr))
884 DBG_SOCK ("vhost message is too short (%d < %d)",
885 msg.size, sizeof (msg.addr));
889 vui->vrings[msg.state.index].desc = (vring_desc_t *)
890 map_user_mem (vui, msg.addr.desc_user_addr);
891 vui->vrings[msg.state.index].used = (vring_used_t *)
892 map_user_mem (vui, msg.addr.used_user_addr);
893 vui->vrings[msg.state.index].avail = (vring_avail_t *)
894 map_user_mem (vui, msg.addr.avail_user_addr);
896 if ((vui->vrings[msg.state.index].desc == NULL) ||
897 (vui->vrings[msg.state.index].used == NULL) ||
898 (vui->vrings[msg.state.index].avail == NULL))
900 DBG_SOCK ("failed to map user memory for hw_if_index %d",
905 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
906 vui->vrings[msg.state.index].log_used =
907 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
909 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
910 the ring is initialized in an enabled state. */
911 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
913 vui->vrings[msg.state.index].enabled = 1;
916 vui->vrings[msg.state.index].last_used_idx =
917 vui->vrings[msg.state.index].last_avail_idx =
918 vui->vrings[msg.state.index].used->idx;
920 /* tell driver that we don't want interrupts */
921 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
924 case VHOST_USER_SET_OWNER:
925 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
928 case VHOST_USER_RESET_OWNER:
929 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
932 case VHOST_USER_SET_VRING_CALL:
933 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL %d",
934 vui->hw_if_index, msg.u64);
936 q = (u8) (msg.u64 & 0xFF);
938 /* if there is old fd, delete and close it */
939 if (vui->vrings[q].callfd_idx != ~0)
941 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
942 vui->vrings[q].callfd_idx);
943 clib_file_del (&file_main, uf);
944 vui->vrings[q].callfd_idx = ~0;
947 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
949 if (number_of_fds != 1)
951 DBG_SOCK ("More than one fd received !");
955 template.read_function = vhost_user_callfd_read_ready;
956 template.file_descriptor = fds[0];
957 template.private_data =
958 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
959 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
962 vui->vrings[q].callfd_idx = ~0;
965 case VHOST_USER_SET_VRING_KICK:
966 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK %d",
967 vui->hw_if_index, msg.u64);
969 q = (u8) (msg.u64 & 0xFF);
971 if (vui->vrings[q].kickfd_idx != ~0)
973 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
974 vui->vrings[q].kickfd_idx);
975 clib_file_del (&file_main, uf);
976 vui->vrings[q].kickfd_idx = ~0;
979 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
981 if (number_of_fds != 1)
983 DBG_SOCK ("More than one fd received !");
987 template.read_function = vhost_user_kickfd_read_ready;
988 template.file_descriptor = fds[0];
989 template.private_data =
990 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
992 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
996 //When no kickfd is set, the queue is initialized as started
997 vui->vrings[q].kickfd_idx = ~0;
998 vui->vrings[q].started = 1;
1003 case VHOST_USER_SET_VRING_ERR:
1004 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR %d",
1005 vui->hw_if_index, msg.u64);
1007 q = (u8) (msg.u64 & 0xFF);
1009 if (vui->vrings[q].errfd != -1)
1010 close (vui->vrings[q].errfd);
1012 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
1014 if (number_of_fds != 1)
1017 vui->vrings[q].errfd = fds[0];
1020 vui->vrings[q].errfd = -1;
1024 case VHOST_USER_SET_VRING_BASE:
1025 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1026 vui->hw_if_index, msg.state.index, msg.state.num);
1028 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
1031 case VHOST_USER_GET_VRING_BASE:
1032 if (msg.state.index >= VHOST_VRING_MAX_N)
1034 DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
1035 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1040 * Copy last_avail_idx from the vring before closing it because
1041 * closing the vring also initializes the vring last_avail_idx
1043 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
1045 msg.size = sizeof (msg.state);
1047 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
1048 vhost_user_vring_close (vui, msg.state.index);
1049 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1050 vui->hw_if_index, msg.state.index, msg.state.num);
1053 case VHOST_USER_NONE:
1054 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
1058 case VHOST_USER_SET_LOG_BASE:
1060 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
1062 if (msg.size != sizeof (msg.log))
1065 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
1066 msg.size, sizeof (msg.log));
1071 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
1074 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1079 /* align size to 2M page */
1080 long page_sz = get_huge_page_size (fd);
1082 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
1084 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1088 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
1089 map_sz, msg.log.offset, fd, vui->log_base_addr);
1091 if (vui->log_base_addr == MAP_FAILED)
1093 clib_warning ("failed to map memory. errno is %d", errno);
1097 vui->log_base_addr += msg.log.offset;
1098 vui->log_size = msg.log.size;
1101 msg.size = sizeof (msg.u64);
1106 case VHOST_USER_SET_LOG_FD:
1107 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1111 case VHOST_USER_GET_PROTOCOL_FEATURES:
1113 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1114 (1 << VHOST_USER_PROTOCOL_F_MQ);
1115 msg.size = sizeof (msg.u64);
1117 ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - reply 0x%016llx",
1118 vui->hw_if_index, msg.u64);
1121 case VHOST_USER_SET_PROTOCOL_FEATURES:
1123 ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%016llx",
1124 vui->hw_if_index, msg.u64);
1126 vui->protocol_features = msg.u64;
1130 case VHOST_USER_GET_QUEUE_NUM:
1132 msg.u64 = VHOST_VRING_MAX_N;
1133 msg.size = sizeof (msg.u64);
1134 DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1135 vui->hw_if_index, msg.u64);
1138 case VHOST_USER_SET_VRING_ENABLE:
1139 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1140 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1142 if (msg.state.index >= VHOST_VRING_MAX_N)
1144 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
1145 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1149 vui->vrings[msg.state.index].enabled = msg.state.num;
1153 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1158 /* if we need to reply */
1162 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1163 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1165 DBG_SOCK ("could not send message response");
1170 vhost_user_update_iface_state (vui);
1171 vlib_worker_thread_barrier_release (vlib_get_main ());
1175 vhost_user_if_disconnect (vui);
1176 vhost_user_update_iface_state (vui);
1177 vlib_worker_thread_barrier_release (vlib_get_main ());
1181 static clib_error_t *
1182 vhost_user_socket_error (clib_file_t * uf)
1184 vlib_main_t *vm = vlib_get_main ();
1185 vhost_user_main_t *vum = &vhost_user_main;
1186 vhost_user_intf_t *vui =
1187 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1189 DBG_SOCK ("socket error on if %d", vui->sw_if_index);
1190 vlib_worker_thread_barrier_sync (vm);
1191 vhost_user_if_disconnect (vui);
1192 vhost_user_rx_thread_placement ();
1193 vlib_worker_thread_barrier_release (vm);
1197 static clib_error_t *
1198 vhost_user_socksvr_accept_ready (clib_file_t * uf)
1200 int client_fd, client_len;
1201 struct sockaddr_un client;
1202 clib_file_t template = { 0 };
1203 vhost_user_main_t *vum = &vhost_user_main;
1204 vhost_user_intf_t *vui;
1206 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1208 client_len = sizeof (client);
1209 client_fd = accept (uf->file_descriptor,
1210 (struct sockaddr *) &client,
1211 (socklen_t *) & client_len);
1214 return clib_error_return_unix (0, "accept");
1216 DBG_SOCK ("New client socket for vhost interface %d", vui->sw_if_index);
1217 template.read_function = vhost_user_socket_read;
1218 template.error_function = vhost_user_socket_error;
1219 template.file_descriptor = client_fd;
1220 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1221 vui->clib_file_index = clib_file_add (&file_main, &template);
1225 static clib_error_t *
1226 vhost_user_init (vlib_main_t * vm)
1228 clib_error_t *error;
1229 vhost_user_main_t *vum = &vhost_user_main;
1230 vlib_thread_main_t *tm = vlib_get_thread_main ();
1232 error = vlib_call_init_function (vm, ip4_init);
1236 vum->coalesce_frames = 32;
1237 vum->coalesce_time = 1e-3;
1239 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1242 vec_foreach (cpu, vum->cpus)
1244 /* This is actually not necessary as validate already zeroes it
1245 * Just keeping the loop here for later because I am lazy. */
1246 cpu->rx_buffers_len = 0;
1249 vum->random = random_default_seed ();
1251 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1256 VLIB_INIT_FUNCTION (vhost_user_init);
1259 format_vhost_trace (u8 * s, va_list * va)
1261 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1262 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
1263 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
1264 vhost_user_main_t *vum = &vhost_user_main;
1265 vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
1266 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
1269 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
1271 u32 indent = format_get_indent (s);
1273 s = format (s, "%U %U queue %d\n", format_white_space, indent,
1274 format_vnet_sw_interface_name, vnm, sw, t->qid);
1276 s = format (s, "%U virtio flags:\n", format_white_space, indent);
1278 if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
1279 s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
1280 foreach_virtio_trace_flags
1282 s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
1283 format_white_space, indent, t->first_desc_len);
1285 s = format (s, "%U flags 0x%02x gso_type %u\n",
1286 format_white_space, indent,
1287 t->hdr.hdr.flags, t->hdr.hdr.gso_type);
1289 if (vui->virtio_net_hdr_sz == 12)
1290 s = format (s, "%U num_buff %u",
1291 format_white_space, indent, t->hdr.num_buffers);
1297 vhost_user_rx_trace (vhost_trace_t * t,
1298 vhost_user_intf_t * vui, u16 qid,
1299 vlib_buffer_t * b, vhost_user_vring_t * txvq)
1301 vhost_user_main_t *vum = &vhost_user_main;
1302 u32 last_avail_idx = txvq->last_avail_idx;
1303 u32 desc_current = txvq->avail->ring[last_avail_idx & txvq->qsz_mask];
1304 vring_desc_t *hdr_desc = 0;
1305 virtio_net_hdr_mrg_rxbuf_t *hdr;
1308 memset (t, 0, sizeof (*t));
1309 t->device_index = vui - vum->vhost_user_interfaces;
1312 hdr_desc = &txvq->desc[desc_current];
1313 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1315 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1316 /* Header is the first here */
1317 hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
1319 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1321 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1323 if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1324 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1326 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1329 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1331 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
1333 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
1337 u32 len = vui->virtio_net_hdr_sz;
1338 memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
1343 vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
1345 vhost_user_main_t *vum = &vhost_user_main;
1347 int fd = UNIX_GET_FD (vq->callfd_idx);
1350 rv = write (fd, &x, sizeof (x));
1354 ("Error: Could not write to unix socket for callfd %d", fd);
1358 vq->n_since_last_int = 0;
1359 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
1362 static_always_inline u32
1363 vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1364 u16 copy_len, u32 * map_hint)
1366 void *src0, *src1, *src2, *src3;
1367 if (PREDICT_TRUE (copy_len >= 4))
1369 if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
1371 if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
1374 while (PREDICT_TRUE (copy_len >= 4))
1380 (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
1383 (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
1386 CLIB_PREFETCH (src2, 64, LOAD);
1387 CLIB_PREFETCH (src3, 64, LOAD);
1389 clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
1390 clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
1397 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
1399 clib_memcpy ((void *) cpy->dst, src0, cpy->len);
1407 * Try to discard packets from the tx ring (VPP RX path).
1408 * Returns the number of discarded packets.
1411 vhost_user_rx_discard_packet (vlib_main_t * vm,
1412 vhost_user_intf_t * vui,
1413 vhost_user_vring_t * txvq, u32 discard_max)
1416 * On the RX side, each packet corresponds to one descriptor
1417 * (it is the same whether it is a shallow descriptor, chained, or indirect).
1418 * Therefore, discarding a packet is like discarding a descriptor.
1420 u32 discarded_packets = 0;
1421 u32 avail_idx = txvq->avail->idx;
1422 while (discarded_packets != discard_max)
1424 if (avail_idx == txvq->last_avail_idx)
1427 u16 desc_chain_head =
1428 txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask];
1429 txvq->last_avail_idx++;
1430 txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id =
1432 txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0;
1433 vhost_user_log_dirty_ring (vui, txvq,
1434 ring[txvq->last_used_idx & txvq->qsz_mask]);
1435 txvq->last_used_idx++;
1436 discarded_packets++;
1440 CLIB_MEMORY_BARRIER ();
1441 txvq->used->idx = txvq->last_used_idx;
1442 vhost_user_log_dirty_ring (vui, txvq, idx);
1443 return discarded_packets;
1447 * In case of overflow, we need to rewind the array of allocated buffers.
1450 vhost_user_input_rewind_buffers (vlib_main_t * vm,
1451 vhost_cpu_t * cpu, vlib_buffer_t * b_head)
1453 u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1454 vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
1455 b_current->current_length = 0;
1456 b_current->flags = 0;
1457 while (b_current != b_head)
1459 cpu->rx_buffers_len++;
1460 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1461 b_current = vlib_get_buffer (vm, bi_current);
1462 b_current->current_length = 0;
1463 b_current->flags = 0;
1465 cpu->rx_buffers_len++;
1469 vhost_user_if_input (vlib_main_t * vm,
1470 vhost_user_main_t * vum,
1471 vhost_user_intf_t * vui,
1472 u16 qid, vlib_node_runtime_t * node,
1473 vnet_hw_interface_rx_mode mode)
1475 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1476 u16 n_rx_packets = 0;
1479 u32 n_left_to_next, *to_next;
1480 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1481 u32 n_trace = vlib_get_trace_count (vm, node);
1483 u16 thread_index = vlib_get_thread_index ();
1487 /* do we have pending interrupts ? */
1488 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1489 f64 now = vlib_time_now (vm);
1491 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1492 vhost_user_send_call (vm, txvq);
1494 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1495 vhost_user_send_call (vm, rxvq);
1499 * For adaptive mode, it is optimized to reduce interrupts.
1500 * If the scheduler switches the input node to polling due
1501 * to burst of traffic, we tell the driver no interrupt.
1502 * When the traffic subsides, the scheduler switches the node back to
1503 * interrupt mode. We must tell the driver we want interrupt.
1505 if (PREDICT_FALSE (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
1508 VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) ||
1510 VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
1511 /* Tell driver we want notification */
1512 txvq->used->flags = 0;
1514 /* Tell driver we don't want notification */
1515 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
1518 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
1521 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1524 if (PREDICT_FALSE (n_left == 0))
1527 if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
1530 * Discard input packet if interface is admin down or vring is not
1532 * "For example, for a networking device, in the disabled state
1533 * client must not supply any new RX packets, but must process
1534 * and discard any TX packets."
1536 vhost_user_rx_discard_packet (vm, vui, txvq,
1537 VHOST_USER_DOWN_DISCARD_COUNT);
1541 if (PREDICT_FALSE (n_left == (txvq->qsz_mask + 1)))
1544 * Informational error logging when VPP is not
1545 * receiving packets fast enough.
1547 vlib_error_count (vm, node->node_index,
1548 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1551 if (n_left > VLIB_FRAME_SIZE)
1552 n_left = VLIB_FRAME_SIZE;
1555 * For small packets (<2kB), we will not need more than one vlib buffer
1556 * per packet. In case packets are bigger, we will just yeld at some point
1557 * in the loop and come back later. This is not an issue as for big packet,
1558 * processing cost really comes from the memory copy.
1559 * The assumption is that big packets will fit in 40 buffers.
1561 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len < n_left + 1 ||
1562 vum->cpus[thread_index].rx_buffers_len < 40))
1564 u32 curr_len = vum->cpus[thread_index].rx_buffers_len;
1565 vum->cpus[thread_index].rx_buffers_len +=
1566 vlib_buffer_alloc_from_free_list (vm,
1567 vum->cpus[thread_index].rx_buffers +
1569 VHOST_USER_RX_BUFFERS_N - curr_len,
1570 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1573 (vum->cpus[thread_index].rx_buffers_len <
1574 VHOST_USER_RX_BUFFER_STARVATION))
1576 /* In case of buffer starvation, discard some packets from the queue
1577 * and log the event.
1578 * We keep doing best effort for the remaining packets. */
1579 u32 flush = (n_left + 1 > vum->cpus[thread_index].rx_buffers_len) ?
1580 n_left + 1 - vum->cpus[thread_index].rx_buffers_len : 1;
1581 flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1584 vlib_increment_simple_counter (vnet_main.
1585 interface_main.sw_if_counters +
1586 VNET_INTERFACE_COUNTER_DROP,
1587 vlib_get_thread_index (),
1588 vui->sw_if_index, flush);
1590 vlib_error_count (vm, vhost_user_input_node.index,
1591 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1597 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1599 while (n_left > 0 && n_left_to_next > 0)
1601 vlib_buffer_t *b_head, *b_current;
1604 u32 desc_data_offset;
1605 vring_desc_t *desc_table = txvq->desc;
1607 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len <= 1))
1609 /* Not enough rx_buffers
1610 * Note: We yeld on 1 so we don't need to do an additional
1611 * check for the next buffer prefetch.
1618 txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask];
1619 vum->cpus[thread_index].rx_buffers_len--;
1620 bi_current = (vum->cpus[thread_index].rx_buffers)
1621 [vum->cpus[thread_index].rx_buffers_len];
1622 b_head = b_current = vlib_get_buffer (vm, bi_current);
1623 to_next[0] = bi_current; //We do that now so we can forget about bi_current
1627 vlib_prefetch_buffer_with_index (vm,
1629 cpus[thread_index].rx_buffers)
1630 [vum->cpus[thread_index].
1631 rx_buffers_len - 1], LOAD);
1633 /* Just preset the used descriptor id and length for later */
1634 txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id =
1636 txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0;
1637 vhost_user_log_dirty_ring (vui, txvq,
1638 ring[txvq->last_used_idx &
1641 /* The buffer should already be initialized */
1642 b_head->total_length_not_including_first_buffer = 0;
1643 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1645 if (PREDICT_FALSE (n_trace))
1647 //TODO: next_index is not exactly known at that point
1648 vlib_trace_buffer (vm, node, next_index, b_head,
1649 /* follow_chain */ 0);
1651 vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1652 vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1654 vlib_set_trace_count (vm, node, n_trace);
1657 /* This depends on the setup but is very consistent
1658 * So I think the CPU branch predictor will make a pretty good job
1659 * at optimizing the decision. */
1660 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1662 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1665 if (PREDICT_FALSE (desc_table == 0))
1667 vlib_error_count (vm, node->node_index,
1668 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
1673 if (PREDICT_TRUE (vui->is_any_layout) ||
1674 (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
1676 /* ANYLAYOUT or single buffer */
1677 desc_data_offset = vui->virtio_net_hdr_sz;
1681 /* CSR case without ANYLAYOUT, skip 1st buffer */
1682 desc_data_offset = desc_table[desc_current].len;
1687 /* Get more input if necessary. Or end of packet. */
1688 if (desc_data_offset == desc_table[desc_current].len)
1690 if (PREDICT_FALSE (desc_table[desc_current].flags &
1693 desc_current = desc_table[desc_current].next;
1694 desc_data_offset = 0;
1702 /* Get more output if necessary. Or end of packet. */
1704 (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1707 (vum->cpus[thread_index].rx_buffers_len == 0))
1709 /* Cancel speculation */
1714 * Checking if there are some left buffers.
1715 * If not, just rewind the used buffers and stop.
1716 * Note: Scheduled copies are not cancelled. This is
1717 * not an issue as they would still be valid. Useless,
1720 vhost_user_input_rewind_buffers (vm,
1728 /* Get next output */
1729 vum->cpus[thread_index].rx_buffers_len--;
1731 (vum->cpus[thread_index].rx_buffers)[vum->cpus
1732 [thread_index].rx_buffers_len];
1733 b_current->next_buffer = bi_next;
1734 b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1735 bi_current = bi_next;
1736 b_current = vlib_get_buffer (vm, bi_current);
1739 /* Prepare a copy order executed later for the data */
1740 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
1743 desc_table[desc_current].len - desc_data_offset;
1744 cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1745 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1746 cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
1747 b_current->current_length);
1748 cpy->src = desc_table[desc_current].addr + desc_data_offset;
1750 desc_data_offset += cpy->len;
1752 b_current->current_length += cpy->len;
1753 b_head->total_length_not_including_first_buffer += cpy->len;
1757 CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1759 n_rx_bytes += b_head->total_length_not_including_first_buffer;
1762 b_head->total_length_not_including_first_buffer -=
1763 b_head->current_length;
1765 /* consume the descriptor and return it as used */
1766 txvq->last_avail_idx++;
1767 txvq->last_used_idx++;
1769 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1771 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1772 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1776 u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1778 /* redirect if feature path enabled */
1779 vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
1782 u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1783 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1784 to_next, n_left_to_next,
1791 * Although separating memory copies from virtio ring parsing
1792 * is beneficial, we can offer to perform the copies from time
1793 * to time in order to free some space in the ring.
1795 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1798 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
1799 copy_len, &map_hint)))
1801 vlib_error_count (vm, node->node_index,
1802 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
1806 /* give buffers back to driver */
1807 CLIB_MEMORY_BARRIER ();
1808 txvq->used->idx = txvq->last_used_idx;
1809 vhost_user_log_dirty_ring (vui, txvq, idx);
1813 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1816 /* Do the memory copies */
1818 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
1819 copy_len, &map_hint)))
1821 vlib_error_count (vm, node->node_index,
1822 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
1825 /* give buffers back to driver */
1826 CLIB_MEMORY_BARRIER ();
1827 txvq->used->idx = txvq->last_used_idx;
1828 vhost_user_log_dirty_ring (vui, txvq, idx);
1830 /* interrupt (call) handling */
1831 if ((txvq->callfd_idx != ~0) &&
1832 !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
1834 txvq->n_since_last_int += n_rx_packets;
1836 if (txvq->n_since_last_int > vum->coalesce_frames)
1837 vhost_user_send_call (vm, txvq);
1840 /* increase rx counters */
1841 vlib_increment_combined_counter
1842 (vnet_main.interface_main.combined_sw_if_counters
1843 + VNET_INTERFACE_COUNTER_RX,
1844 vlib_get_thread_index (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1846 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
1848 return n_rx_packets;
1852 vhost_user_input (vlib_main_t * vm,
1853 vlib_node_runtime_t * node, vlib_frame_t * f)
1855 vhost_user_main_t *vum = &vhost_user_main;
1856 uword n_rx_packets = 0;
1857 vhost_user_intf_t *vui;
1858 vnet_device_input_runtime_t *rt =
1859 (vnet_device_input_runtime_t *) node->runtime_data;
1860 vnet_device_and_queue_t *dq;
1862 vec_foreach (dq, rt->devices_and_queues)
1864 if (clib_smp_swap (&dq->interrupt_pending, 0) ||
1865 (node->state == VLIB_NODE_STATE_POLLING))
1868 pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance);
1869 n_rx_packets = vhost_user_if_input (vm, vum, vui, dq->queue_id, node,
1874 return n_rx_packets;
1878 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1879 .function = vhost_user_input,
1880 .type = VLIB_NODE_TYPE_INPUT,
1881 .name = "vhost-user-input",
1882 .sibling_of = "device-input",
1884 /* Will be enabled if/when hardware is detected. */
1885 .state = VLIB_NODE_STATE_DISABLED,
1887 .format_buffer = format_ethernet_header_with_length,
1888 .format_trace = format_vhost_trace,
1890 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1891 .error_strings = vhost_user_input_func_error_strings,
1894 VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
1899 vhost_user_tx_trace (vhost_trace_t * t,
1900 vhost_user_intf_t * vui, u16 qid,
1901 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
1903 vhost_user_main_t *vum = &vhost_user_main;
1904 u32 last_avail_idx = rxvq->last_avail_idx;
1905 u32 desc_current = rxvq->avail->ring[last_avail_idx & rxvq->qsz_mask];
1906 vring_desc_t *hdr_desc = 0;
1909 memset (t, 0, sizeof (*t));
1910 t->device_index = vui - vum->vhost_user_interfaces;
1913 hdr_desc = &rxvq->desc[desc_current];
1914 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1916 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1917 /* Header is the first here */
1918 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
1920 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1922 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1924 if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1925 !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1927 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1930 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1933 static_always_inline u32
1934 vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1935 u16 copy_len, u32 * map_hint)
1937 void *dst0, *dst1, *dst2, *dst3;
1938 if (PREDICT_TRUE (copy_len >= 4))
1940 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
1942 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
1944 while (PREDICT_TRUE (copy_len >= 4))
1950 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
1953 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
1956 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
1957 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
1959 clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
1960 clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
1962 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
1963 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
1970 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
1972 clib_memcpy (dst0, (void *) cpy->src, cpy->len);
1973 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
1982 vhost_user_tx (vlib_main_t * vm,
1983 vlib_node_runtime_t * node, vlib_frame_t * frame)
1985 u32 *buffers = vlib_frame_args (frame);
1986 u32 n_left = frame->n_vectors;
1987 vhost_user_main_t *vum = &vhost_user_main;
1988 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
1989 vhost_user_intf_t *vui =
1990 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1992 vhost_user_vring_t *rxvq;
1994 u32 thread_index = vlib_get_thread_index ();
2000 if (PREDICT_FALSE (!vui->admin_up))
2002 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
2006 if (PREDICT_FALSE (!vui->is_up))
2008 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
2013 VHOST_VRING_IDX_RX (*vec_elt_at_index
2014 (vui->per_cpu_tx_qid, thread_index));
2015 rxvq = &vui->vrings[qid];
2016 if (PREDICT_FALSE (vui->use_tx_spinlock))
2017 vhost_user_vring_lock (vui, qid);
2020 error = VHOST_USER_TX_FUNC_ERROR_NONE;
2025 vlib_buffer_t *b0, *current_b0;
2026 u16 desc_head, desc_index, desc_len;
2027 vring_desc_t *desc_table;
2028 uword buffer_map_addr;
2032 if (PREDICT_TRUE (n_left > 1))
2033 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2035 b0 = vlib_get_buffer (vm, buffers[0]);
2037 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2039 vum->cpus[thread_index].current_trace =
2040 vlib_add_trace (vm, node, b0,
2041 sizeof (*vum->cpus[thread_index].current_trace));
2042 vhost_user_tx_trace (vum->cpus[thread_index].current_trace,
2043 vui, qid / 2, b0, rxvq);
2046 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2048 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2052 desc_table = rxvq->desc;
2053 desc_head = desc_index =
2054 rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
2056 /* Go deeper in case of indirect descriptor
2057 * I don't know of any driver providing indirect for RX. */
2058 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2061 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2063 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2068 map_guest_mem (vui, rxvq->desc[desc_index].addr,
2071 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2077 desc_len = vui->virtio_net_hdr_sz;
2078 buffer_map_addr = desc_table[desc_index].addr;
2079 buffer_len = desc_table[desc_index].len;
2082 // Get a header from the header array
2083 virtio_net_hdr_mrg_rxbuf_t *hdr =
2084 &vum->cpus[thread_index].tx_headers[tx_headers_len];
2087 hdr->hdr.gso_type = 0;
2088 hdr->num_buffers = 1; //This is local, no need to check
2090 // Prepare a copy order executed later for the header
2091 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
2093 cpy->len = vui->virtio_net_hdr_sz;
2094 cpy->dst = buffer_map_addr;
2095 cpy->src = (uword) hdr;
2098 buffer_map_addr += vui->virtio_net_hdr_sz;
2099 buffer_len -= vui->virtio_net_hdr_sz;
2100 bytes_left = b0->current_length;
2104 if (buffer_len == 0)
2106 if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2108 //Next one is chained
2109 desc_index = desc_table[desc_index].next;
2110 buffer_map_addr = desc_table[desc_index].addr;
2111 buffer_len = desc_table[desc_index].len;
2113 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2115 virtio_net_hdr_mrg_rxbuf_t *hdr =
2116 &vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
2118 //Move from available to used buffer
2119 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id =
2121 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len =
2123 vhost_user_log_dirty_ring (vui, rxvq,
2124 ring[rxvq->last_used_idx &
2127 rxvq->last_avail_idx++;
2128 rxvq->last_used_idx++;
2133 (rxvq->last_avail_idx == rxvq->avail->idx))
2135 //Dequeue queued descriptors for this packet
2136 rxvq->last_used_idx -= hdr->num_buffers - 1;
2137 rxvq->last_avail_idx -= hdr->num_buffers - 1;
2138 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2142 desc_table = rxvq->desc;
2143 desc_head = desc_index =
2144 rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
2146 (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2148 //It is seriously unlikely that a driver will put indirect descriptor
2149 //after non-indirect descriptor.
2151 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2153 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2159 rxvq->desc[desc_index].addr,
2162 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2167 buffer_map_addr = desc_table[desc_index].addr;
2168 buffer_len = desc_table[desc_index].len;
2172 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2178 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
2180 cpy->len = bytes_left;
2181 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2182 cpy->dst = buffer_map_addr;
2183 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2184 current_b0->current_length - bytes_left;
2186 bytes_left -= cpy->len;
2187 buffer_len -= cpy->len;
2188 buffer_map_addr += cpy->len;
2189 desc_len += cpy->len;
2191 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
2194 // Check if vlib buffer has more data. If not, get more or break.
2195 if (PREDICT_TRUE (!bytes_left))
2198 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2200 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2201 bytes_left = current_b0->current_length;
2211 //Move from available to used ring
2212 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = desc_head;
2213 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = desc_len;
2214 vhost_user_log_dirty_ring (vui, rxvq,
2215 ring[rxvq->last_used_idx & rxvq->qsz_mask]);
2216 rxvq->last_avail_idx++;
2217 rxvq->last_used_idx++;
2219 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2221 vum->cpus[thread_index].current_trace->hdr =
2222 vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
2225 n_left--; //At the end for error counting when 'goto done' is invoked
2228 * Do the copy periodically to prevent
2229 * vum->cpus[thread_index].copy array overflow and corrupt memory
2231 if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD))
2234 (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
2235 copy_len, &map_hint)))
2237 vlib_error_count (vm, node->node_index,
2238 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
2242 /* give buffers back to driver */
2243 CLIB_MEMORY_BARRIER ();
2244 rxvq->used->idx = rxvq->last_used_idx;
2245 vhost_user_log_dirty_ring (vui, rxvq, idx);
2251 //Do the memory copies
2253 (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
2254 copy_len, &map_hint)))
2256 vlib_error_count (vm, node->node_index,
2257 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
2260 CLIB_MEMORY_BARRIER ();
2261 rxvq->used->idx = rxvq->last_used_idx;
2262 vhost_user_log_dirty_ring (vui, rxvq, idx);
2265 * When n_left is set, error is always set to something too.
2266 * In case error is due to lack of remaining buffers, we go back up and
2268 * The idea is that it is better to waste some time on packets
2269 * that have been processed already than dropping them and get
2270 * more fresh packets with a good likelyhood that they will be dropped too.
2271 * This technique also gives more time to VM driver to pick-up packets.
2272 * In case the traffic flows from physical to virtual interfaces, this
2273 * technique will end-up leveraging the physical NIC buffer in order to
2274 * absorb the VM's CPU jitter.
2276 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2282 /* interrupt (call) handling */
2283 if ((rxvq->callfd_idx != ~0) &&
2284 !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
2286 rxvq->n_since_last_int += frame->n_vectors - n_left;
2288 if (rxvq->n_since_last_int > vum->coalesce_frames)
2289 vhost_user_send_call (vm, rxvq);
2292 vhost_user_vring_unlock (vui, qid);
2295 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2297 vlib_error_count (vm, node->node_index, error, n_left);
2298 vlib_increment_simple_counter
2299 (vnet_main.interface_main.sw_if_counters
2300 + VNET_INTERFACE_COUNTER_DROP,
2301 thread_index, vui->sw_if_index, n_left);
2304 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2305 return frame->n_vectors;
2309 vhost_user_send_interrupt_process (vlib_main_t * vm,
2310 vlib_node_runtime_t * rt, vlib_frame_t * f)
2312 vhost_user_intf_t *vui;
2313 f64 timeout = 3153600000.0 /* 100 years */ ;
2314 uword event_type, *event_data = 0;
2315 vhost_user_main_t *vum = &vhost_user_main;
2317 f64 now, poll_time_remaining;
2323 poll_time_remaining =
2324 vlib_process_wait_for_event_or_clock (vm, timeout);
2325 event_type = vlib_process_get_events (vm, &event_data);
2326 vec_reset_length (event_data);
2329 * Use the remaining timeout if it is less than coalesce time to avoid
2330 * resetting the existing timer in the middle of expiration
2332 timeout = poll_time_remaining;
2333 if (vlib_process_suspend_time_is_zero (timeout) ||
2334 (timeout > vum->coalesce_time))
2335 timeout = vum->coalesce_time;
2337 now = vlib_time_now (vm);
2340 case VHOST_USER_EVENT_STOP_TIMER:
2344 case VHOST_USER_EVENT_START_TIMER:
2346 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
2352 pool_foreach (vui, vum->vhost_user_interfaces, {
2353 next_timeout = timeout;
2354 vec_foreach (queue, vui->rx_queues)
2356 vhost_user_vring_t *rxvq =
2357 &vui->vrings[VHOST_VRING_IDX_RX (*queue)];
2358 vhost_user_vring_t *txvq =
2359 &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
2361 if (txvq->n_since_last_int)
2363 if (now >= txvq->int_deadline)
2364 vhost_user_send_call (vm, txvq);
2366 next_timeout = txvq->int_deadline - now;
2369 if (rxvq->n_since_last_int)
2371 if (now >= rxvq->int_deadline)
2372 vhost_user_send_call (vm, rxvq);
2374 next_timeout = rxvq->int_deadline - now;
2377 if ((next_timeout < timeout) && (next_timeout > 0.0))
2378 timeout = next_timeout;
2385 clib_warning ("BUG: unhandled event type %d", event_type);
2388 /* No less than 1 millisecond */
2392 timeout = 3153600000.0;
2398 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node,static) = {
2399 .function = vhost_user_send_interrupt_process,
2400 .type = VLIB_NODE_TYPE_PROCESS,
2401 .name = "vhost-user-send-interrupt-process",
2405 static clib_error_t *
2406 vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index,
2407 u32 qid, vnet_hw_interface_rx_mode mode)
2409 vlib_main_t *vm = vnm->vlib_main;
2410 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2411 vhost_user_main_t *vum = &vhost_user_main;
2412 vhost_user_intf_t *vui =
2413 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
2414 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
2416 if ((mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2417 (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
2419 if (txvq->kickfd_idx == ~0)
2421 // We cannot support interrupt mode if the driver opts out
2422 return clib_error_return (0, "Driver does not support interrupt");
2424 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2427 // Start the timer if this is the first encounter on interrupt
2429 if ((vum->ifq_count == 1) &&
2430 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2431 vlib_process_signal_event (vm,
2432 vhost_user_send_interrupt_node.index,
2433 VHOST_USER_EVENT_START_TIMER, 0);
2436 else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2438 if (((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2439 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) &&
2443 // Stop the timer if there is no more interrupt interface/queue
2444 if ((vum->ifq_count == 0) &&
2445 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2446 vlib_process_signal_event (vm,
2447 vhost_user_send_interrupt_node.index,
2448 VHOST_USER_EVENT_STOP_TIMER, 0);
2453 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2454 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
2455 else if ((mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE) ||
2456 (mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT))
2457 txvq->used->flags = 0;
2460 clib_warning ("BUG: unhandled mode %d changed for if %d queue %d", mode,
2462 return clib_error_return (0, "unsupported");
2468 static clib_error_t *
2469 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
2472 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2473 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
2474 vhost_user_main_t *vum = &vhost_user_main;
2475 vhost_user_intf_t *vui =
2476 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
2478 vui->admin_up = is_up;
2480 if (is_up && vui->is_up)
2481 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
2482 VNET_HW_INTERFACE_FLAG_LINK_UP);
2484 return /* no error */ 0;
2488 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2489 .name = "vhost-user",
2490 .tx_function = vhost_user_tx,
2491 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2492 .tx_function_error_strings = vhost_user_tx_func_error_strings,
2493 .format_device_name = format_vhost_user_interface_name,
2494 .name_renumber = vhost_user_name_renumber,
2495 .admin_up_down_function = vhost_user_interface_admin_up_down,
2496 .rx_mode_change_function = vhost_user_interface_rx_mode_change,
2497 .format_tx_trace = format_vhost_trace,
2500 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
2505 vhost_user_process (vlib_main_t * vm,
2506 vlib_node_runtime_t * rt, vlib_frame_t * f)
2508 vhost_user_main_t *vum = &vhost_user_main;
2509 vhost_user_intf_t *vui;
2510 struct sockaddr_un sun;
2512 clib_file_t template = { 0 };
2513 f64 timeout = 3153600000.0 /* 100 years */ ;
2514 uword *event_data = 0;
2517 sun.sun_family = AF_UNIX;
2518 template.read_function = vhost_user_socket_read;
2519 template.error_function = vhost_user_socket_error;
2523 vlib_process_wait_for_event_or_clock (vm, timeout);
2524 vlib_process_get_events (vm, &event_data);
2525 vec_reset_length (event_data);
2530 pool_foreach (vui, vum->vhost_user_interfaces, {
2532 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
2533 if (vui->clib_file_index == ~0)
2536 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
2539 * 1st time error or new error for this interface,
2540 * spit out the message and record the error
2542 if (!vui->sock_errno || (vui->sock_errno != errno))
2545 ("Error: Could not open unix socket for %s",
2546 vui->sock_filename);
2547 vui->sock_errno = errno;
2552 /* try to connect */
2553 strncpy (sun.sun_path, (char *) vui->sock_filename,
2554 sizeof (sun.sun_path) - 1);
2556 /* Avoid hanging VPP if the other end does not accept */
2557 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2558 clib_unix_warning ("fcntl");
2560 if (connect (sockfd, (struct sockaddr *) &sun,
2561 sizeof (struct sockaddr_un)) == 0)
2563 /* Set the socket to blocking as it was before */
2564 if (fcntl(sockfd, F_SETFL, 0) < 0)
2565 clib_unix_warning ("fcntl2");
2567 vui->sock_errno = 0;
2568 template.file_descriptor = sockfd;
2569 template.private_data =
2570 vui - vhost_user_main.vhost_user_interfaces;
2571 vui->clib_file_index = clib_file_add (&file_main, &template);
2573 /* This sockfd is considered consumed */
2578 vui->sock_errno = errno;
2583 /* check if socket is alive */
2585 socklen_t len = sizeof (error);
2586 int fd = UNIX_GET_FD(vui->clib_file_index);
2588 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
2592 DBG_SOCK ("getsockopt returned %d", retval);
2593 vhost_user_if_disconnect (vui);
2604 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
2605 .function = vhost_user_process,
2606 .type = VLIB_NODE_TYPE_PROCESS,
2607 .name = "vhost-user-process",
2612 * Disables and reset interface structure.
2613 * It can then be either init again, or removed from used interfaces.
2616 vhost_user_term_if (vhost_user_intf_t * vui)
2619 vhost_user_main_t *vum = &vhost_user_main;
2621 // disconnect interface sockets
2622 vhost_user_if_disconnect (vui);
2623 vhost_user_update_iface_state (vui);
2625 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2627 clib_mem_free ((void *) vui->vring_locks[q]);
2630 if (vui->unix_server_index != ~0)
2632 //Close server socket
2633 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
2634 vui->unix_server_index);
2635 clib_file_del (&file_main, uf);
2636 vui->unix_server_index = ~0;
2637 unlink (vui->sock_filename);
2640 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
2645 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
2647 vhost_user_main_t *vum = &vhost_user_main;
2648 vhost_user_intf_t *vui;
2650 vnet_hw_interface_t *hwif;
2653 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2654 hwif->dev_class_index != vhost_user_dev_class.index)
2655 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2657 DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2658 hwif->name, hwif->dev_instance);
2660 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2662 vec_foreach (queue, vui->rx_queues)
2664 vhost_user_vring_t *txvq;
2666 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
2667 if ((vum->ifq_count > 0) &&
2668 ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2669 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
2672 // Stop the timer if there is no more interrupt interface/queue
2673 if ((vum->ifq_count == 0) &&
2674 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2676 vlib_process_signal_event (vm,
2677 vhost_user_send_interrupt_node.index,
2678 VHOST_USER_EVENT_STOP_TIMER, 0);
2684 // Disable and reset interface
2685 vhost_user_term_if (vui);
2687 // Reset renumbered iface
2688 if (hwif->dev_instance <
2689 vec_len (vum->show_dev_instance_by_real_dev_instance))
2690 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
2692 // Delete ethernet interface
2693 ethernet_delete_interface (vnm, vui->hw_if_index);
2696 pool_put (vum->vhost_user_interfaces, vui);
2701 static clib_error_t *
2702 vhost_user_exit (vlib_main_t * vm)
2704 vnet_main_t *vnm = vnet_get_main ();
2705 vhost_user_main_t *vum = &vhost_user_main;
2706 vhost_user_intf_t *vui;
2708 vlib_worker_thread_barrier_sync (vlib_get_main ());
2710 pool_foreach (vui, vum->vhost_user_interfaces, {
2711 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
2714 vlib_worker_thread_barrier_release (vlib_get_main ());
2718 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
2721 * Open server unix socket on specified sock_filename.
2724 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
2727 struct sockaddr_un un = { };
2729 /* create listening socket */
2730 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2731 return VNET_API_ERROR_SYSCALL_ERROR_1;
2733 un.sun_family = AF_UNIX;
2734 strncpy ((char *) un.sun_path, (char *) sock_filename,
2735 sizeof (un.sun_path) - 1);
2737 /* remove if exists */
2738 unlink ((char *) sock_filename);
2740 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2742 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2746 if (listen (fd, 1) == -1)
2748 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2761 * Create ethernet interface for vhost user interface.
2764 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
2765 vhost_user_intf_t * vui, u8 * hwaddress)
2767 vhost_user_main_t *vum = &vhost_user_main;
2769 clib_error_t *error;
2771 /* create hw and sw interface */
2774 clib_memcpy (hwaddr, hwaddress, 6);
2778 random_u32 (&vum->random);
2779 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
2784 error = ethernet_register_interface
2786 vhost_user_dev_class.index,
2787 vui - vum->vhost_user_interfaces /* device instance */ ,
2788 hwaddr /* ethernet address */ ,
2789 &vui->hw_if_index, 0 /* flag change */ );
2792 clib_error_report (error);
2794 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
2795 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
2799 * Initialize vui with specified attributes
2802 vhost_user_vui_init (vnet_main_t * vnm,
2803 vhost_user_intf_t * vui,
2805 const char *sock_filename,
2806 u64 feature_mask, u32 * sw_if_index)
2808 vnet_sw_interface_t *sw;
2810 vhost_user_main_t *vum = &vhost_user_main;
2811 vnet_hw_interface_t *hw;
2813 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
2814 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
2815 if (server_sock_fd != -1)
2817 clib_file_t template = { 0 };
2818 template.read_function = vhost_user_socksvr_accept_ready;
2819 template.file_descriptor = server_sock_fd;
2820 template.private_data = vui - vum->vhost_user_interfaces; //hw index
2821 vui->unix_server_index = clib_file_add (&file_main, &template);
2825 vui->unix_server_index = ~0;
2828 vui->sw_if_index = sw->sw_if_index;
2829 strncpy (vui->sock_filename, sock_filename,
2830 ARRAY_LEN (vui->sock_filename) - 1);
2831 vui->sock_errno = 0;
2833 vui->feature_mask = feature_mask;
2834 vui->clib_file_index = ~0;
2835 vui->log_base_addr = 0;
2836 vui->if_index = vui - vum->vhost_user_interfaces;
2837 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
2840 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2841 vhost_user_vring_init (vui, q);
2843 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
2844 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
2847 *sw_if_index = vui->sw_if_index;
2849 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2851 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
2852 CLIB_CACHE_LINE_BYTES);
2853 memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
2856 vec_validate (vui->per_cpu_tx_qid,
2857 vlib_get_thread_main ()->n_vlib_mains - 1);
2858 vhost_user_tx_thread_placement (vui);
2862 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2863 const char *sock_filename,
2867 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
2869 vhost_user_intf_t *vui = NULL;
2872 int server_sock_fd = -1;
2873 vhost_user_main_t *vum = &vhost_user_main;
2876 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2878 return VNET_API_ERROR_INVALID_ARGUMENT;
2881 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2886 vui = &vum->vhost_user_interfaces[*if_index];
2887 *sw_if_index = vui->sw_if_index;
2889 return VNET_API_ERROR_IF_ALREADY_EXISTS;
2895 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
2901 pool_get (vhost_user_main.vhost_user_interfaces, vui);
2903 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
2904 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
2905 feature_mask, &sw_if_idx);
2908 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2911 *sw_if_index = sw_if_idx;
2913 // Process node must connect
2914 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2920 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2921 const char *sock_filename,
2924 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
2926 vhost_user_main_t *vum = &vhost_user_main;
2927 vhost_user_intf_t *vui = NULL;
2929 int server_sock_fd = -1;
2931 vnet_hw_interface_t *hwif;
2934 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2935 hwif->dev_class_index != vhost_user_dev_class.index)
2936 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2938 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2939 return VNET_API_ERROR_INVALID_ARGUMENT;
2941 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2944 * Disallow changing the interface to have the same path name
2945 * as other interface
2947 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2948 if (if_index && (*if_index != vui->if_index))
2949 return VNET_API_ERROR_IF_ALREADY_EXISTS;
2951 // First try to open server socket
2953 if ((rv = vhost_user_init_server_sock (sock_filename,
2954 &server_sock_fd)) != 0)
2957 vhost_user_term_if (vui);
2958 vhost_user_vui_init (vnm, vui, server_sock_fd,
2959 sock_filename, feature_mask, &sw_if_idx);
2962 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2964 // Process node must connect
2965 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2971 vhost_user_connect_command_fn (vlib_main_t * vm,
2972 unformat_input_t * input,
2973 vlib_cli_command_t * cmd)
2975 unformat_input_t _line_input, *line_input = &_line_input;
2976 u8 *sock_filename = NULL;
2979 u64 feature_mask = (u64) ~ (0ULL);
2981 u32 custom_dev_instance = ~0;
2984 clib_error_t *error = NULL;
2986 /* Get a line of input. */
2987 if (!unformat_user (input, unformat_line_input, line_input))
2990 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2992 if (unformat (line_input, "socket %s", &sock_filename))
2994 else if (unformat (line_input, "server"))
2996 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
3000 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
3002 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
3008 error = clib_error_return (0, "unknown input `%U'",
3009 format_unformat_error, line_input);
3014 vnet_main_t *vnm = vnet_get_main ();
3017 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
3018 is_server, &sw_if_index, feature_mask,
3019 renumber, custom_dev_instance, hw)))
3021 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
3025 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
3029 vec_free (sock_filename);
3030 unformat_free (line_input);
3036 vhost_user_delete_command_fn (vlib_main_t * vm,
3037 unformat_input_t * input,
3038 vlib_cli_command_t * cmd)
3040 unformat_input_t _line_input, *line_input = &_line_input;
3041 u32 sw_if_index = ~0;
3042 vnet_main_t *vnm = vnet_get_main ();
3043 clib_error_t *error = NULL;
3045 /* Get a line of input. */
3046 if (!unformat_user (input, unformat_line_input, line_input))
3049 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3051 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
3054 (line_input, "%U", unformat_vnet_sw_interface, vnm,
3057 vnet_hw_interface_t *hwif =
3058 vnet_get_sup_hw_interface (vnm, sw_if_index);
3060 vhost_user_dev_class.index != hwif->dev_class_index)
3062 error = clib_error_return (0, "Not a vhost interface");
3068 error = clib_error_return (0, "unknown input `%U'",
3069 format_unformat_error, line_input);
3074 vhost_user_delete_if (vnm, vm, sw_if_index);
3077 unformat_free (line_input);
3083 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
3084 vhost_user_intf_details_t ** out_vuids)
3087 vhost_user_main_t *vum = &vhost_user_main;
3088 vhost_user_intf_t *vui;
3089 vhost_user_intf_details_t *r_vuids = NULL;
3090 vhost_user_intf_details_t *vuid = NULL;
3091 u32 *hw_if_indices = 0;
3092 vnet_hw_interface_t *hi;
3099 pool_foreach (vui, vum->vhost_user_interfaces,
3100 vec_add1 (hw_if_indices, vui->hw_if_index);
3103 for (i = 0; i < vec_len (hw_if_indices); i++)
3105 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3106 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
3108 vec_add2 (r_vuids, vuid, 1);
3109 vuid->sw_if_index = vui->sw_if_index;
3110 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
3111 vuid->features = vui->features;
3112 vuid->num_regions = vui->nregions;
3113 vuid->is_server = vui->unix_server_index != ~0;
3114 vuid->sock_errno = vui->sock_errno;
3115 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
3116 ARRAY_LEN (vuid->sock_filename) - 1);
3118 s = format (s, "%v%c", hi->name, 0);
3120 strncpy ((char *) vuid->if_name, (char *) s,
3121 ARRAY_LEN (vuid->if_name) - 1);
3126 vec_free (hw_if_indices);
3128 *out_vuids = r_vuids;
3134 show_vhost_user_command_fn (vlib_main_t * vm,
3135 unformat_input_t * input,
3136 vlib_cli_command_t * cmd)
3138 clib_error_t *error = 0;
3139 vnet_main_t *vnm = vnet_get_main ();
3140 vhost_user_main_t *vum = &vhost_user_main;
3141 vhost_user_intf_t *vui;
3142 u32 hw_if_index, *hw_if_indices = 0;
3143 vnet_hw_interface_t *hi;
3153 struct feat_struct *feat_entry;
3155 static struct feat_struct feat_array[] = {
3156 #define _(s,b) { .str = #s, .bit = b, },
3157 foreach_virtio_net_feature
3162 #define foreach_protocol_feature \
3163 _(VHOST_USER_PROTOCOL_F_MQ) \
3164 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
3166 static struct feat_struct proto_feat_array[] = {
3167 #define _(s) { .str = #s, .bit = s},
3168 foreach_protocol_feature
3173 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3176 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
3178 vec_add1 (hw_if_indices, hw_if_index);
3180 else if (unformat (input, "descriptors") || unformat (input, "desc"))
3184 error = clib_error_return (0, "unknown input `%U'",
3185 format_unformat_error, input);
3189 if (vec_len (hw_if_indices) == 0)
3191 pool_foreach (vui, vum->vhost_user_interfaces,
3192 vec_add1 (hw_if_indices, vui->hw_if_index);
3195 vlib_cli_output (vm, "Virtio vhost-user interfaces");
3196 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
3197 vum->coalesce_frames, vum->coalesce_time);
3198 vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d",
3201 for (i = 0; i < vec_len (hw_if_indices); i++)
3203 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3204 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
3205 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
3206 hi->name, hw_if_indices[i]);
3208 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
3209 " features mask (0x%llx): \n"
3210 " features (0x%llx): \n",
3211 vui->virtio_net_hdr_sz, vui->feature_mask,
3214 feat_entry = (struct feat_struct *) &feat_array;
3215 while (feat_entry->str)
3217 if (vui->features & (1ULL << feat_entry->bit))
3218 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3223 vlib_cli_output (vm, " protocol features (0x%llx)",
3224 vui->protocol_features);
3225 feat_entry = (struct feat_struct *) &proto_feat_array;
3226 while (feat_entry->str)
3228 if (vui->protocol_features & (1ULL << feat_entry->bit))
3229 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3234 vlib_cli_output (vm, "\n");
3236 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
3238 (vui->unix_server_index != ~0) ? "server" : "client",
3239 strerror (vui->sock_errno));
3241 vlib_cli_output (vm, " rx placement: ");
3243 vec_foreach (queue, vui->rx_queues)
3245 vnet_main_t *vnm = vnet_get_main ();
3247 vnet_hw_interface_rx_mode mode;
3249 thread_index = vnet_get_device_input_thread_index (vnm,
3252 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, *queue, &mode);
3253 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
3254 thread_index, VHOST_VRING_IDX_TX (*queue),
3255 format_vnet_hw_interface_rx_mode, mode);
3258 vlib_cli_output (vm, " tx placement: %s\n",
3259 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
3261 vec_foreach_index (ci, vui->per_cpu_tx_qid)
3263 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
3264 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
3267 vlib_cli_output (vm, "\n");
3269 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
3273 vlib_cli_output (vm,
3274 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
3275 vlib_cli_output (vm,
3276 " ====== ===== ================== ================== ================== ================== ==================\n");
3278 for (j = 0; j < vui->nregions; j++)
3280 vlib_cli_output (vm,
3281 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
3282 j, vui->region_mmap_fd[j],
3283 vui->regions[j].guest_phys_addr,
3284 vui->regions[j].memory_size,
3285 vui->regions[j].userspace_addr,
3286 vui->regions[j].mmap_offset,
3287 pointer_to_uword (vui->region_mmap_addr[j]));
3289 for (q = 0; q < VHOST_VRING_MAX_N; q++)
3291 if (!vui->vrings[q].started)
3294 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
3295 (q & 1) ? "RX" : "TX",
3296 vui->vrings[q].enabled ? "" : " disabled");
3298 vlib_cli_output (vm,
3299 " qsz %d last_avail_idx %d last_used_idx %d\n",
3300 vui->vrings[q].qsz_mask + 1,
3301 vui->vrings[q].last_avail_idx,
3302 vui->vrings[q].last_used_idx);
3304 if (vui->vrings[q].avail && vui->vrings[q].used)
3305 vlib_cli_output (vm,
3306 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
3307 vui->vrings[q].avail->flags,
3308 vui->vrings[q].avail->idx,
3309 vui->vrings[q].used->flags,
3310 vui->vrings[q].used->idx);
3312 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
3313 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
3314 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
3315 kickfd, callfd, vui->vrings[q].errfd);
3319 vlib_cli_output (vm, "\n descriptor table:\n");
3320 vlib_cli_output (vm,
3321 " id addr len flags next user_addr\n");
3322 vlib_cli_output (vm,
3323 " ===== ================== ===== ====== ===== ==================\n");
3324 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
3327 vlib_cli_output (vm,
3328 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3329 j, vui->vrings[q].desc[j].addr,
3330 vui->vrings[q].desc[j].len,
3331 vui->vrings[q].desc[j].flags,
3332 vui->vrings[q].desc[j].next,
3333 pointer_to_uword (map_guest_mem
3335 vui->vrings[q].desc[j].
3340 vlib_cli_output (vm, "\n");
3343 vec_free (hw_if_indices);
3352 * Create a vHost User interface. Once created, a new virtual interface
3353 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3354 * is the next free index.
3356 * There are several parameters associated with a vHost interface:
3358 * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3359 * VPP to manage the vHost interface. If socket does not already exist, VPP will
3360 * create the socket.
3362 * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3363 * linux socket. If not provided, VPP will be the client.
3365 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3366 * startup. By default, all supported features will be advertised. Otherwise,
3367 * provide the set of features desired.
3368 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3369 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3370 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3371 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3372 * - 0x004000000 (26) - VHOST_F_LOG_ALL
3373 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3374 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3375 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3376 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3378 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3379 * X:X:X:X:X:X unix or X.X.X cisco format.
3381 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3382 * in the name to be specified. If instance already exists, name will be used
3383 * anyway and multiple instances will have the same name. Use with caution.
3385 * - <b>mode [interrupt | polling]</b> - Optional parameter specifying
3386 * the input thread polling policy.
3389 * Example of how to create a vhost interface with VPP as the client and all features enabled:
3390 * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3391 * VirtualEthernet0/0/0
3393 * Example of how to create a vhost interface with VPP as the server and with just
3394 * multiple queues enabled:
3395 * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3396 * VirtualEthernet0/0/1
3398 * Once the vHost interface is created, enable the interface using:
3399 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3402 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3403 .path = "create vhost-user",
3404 .short_help = "create vhost-user socket <socket-filename> [server] "
3405 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ",
3406 .function = vhost_user_connect_command_fn,
3411 * Delete a vHost User interface using the interface name or the
3412 * software interface index. Use the '<em>show interface</em>'
3413 * command to determine the software interface index. On deletion,
3414 * the linux socket will not be deleted.
3417 * Example of how to delete a vhost interface by name:
3418 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3419 * Example of how to delete a vhost interface by software interface index:
3420 * @cliexcmd{delete vhost-user sw_if_index 1}
3423 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3424 .path = "delete vhost-user",
3425 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
3426 .function = vhost_user_delete_command_fn,
3430 * Display the attributes of a single vHost User interface (provide interface
3431 * name), multiple vHost User interfaces (provide a list of interface names seperated
3432 * by spaces) or all Vhost User interfaces (omit an interface name to display all
3433 * vHost interfaces).
3437 * Example of how to display a vhost interface:
3438 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3439 * Virtio vhost-user interfaces
3441 * coalesce frames 32 time 1e-3
3442 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3443 * virtio_net_hdr_sz 12
3444 * features mask (0xffffffffffffffff):
3445 * features (0x50408000):
3446 * VIRTIO_NET_F_MRG_RXBUF (15)
3447 * VIRTIO_NET_F_MQ (22)
3448 * VIRTIO_F_INDIRECT_DESC (28)
3449 * VHOST_USER_F_PROTOCOL_FEATURES (30)
3450 * protocol features (0x3)
3451 * VHOST_USER_PROTOCOL_F_MQ (0)
3452 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3454 * socket filename /tmp/vhost1.sock type client errno "Success"
3457 * thread 1 on vring 1
3458 * thread 1 on vring 5
3459 * thread 2 on vring 3
3460 * thread 2 on vring 7
3461 * tx placement: spin-lock
3462 * thread 0 on vring 0
3463 * thread 1 on vring 2
3464 * thread 2 on vring 0
3466 * Memory regions (total 2)
3467 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3468 * ====== ===== ================== ================== ================== ================== ==================
3469 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3470 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3473 * qsz 256 last_avail_idx 0 last_used_idx 0
3474 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3475 * kickfd 62 callfd 64 errfd -1
3478 * qsz 256 last_avail_idx 0 last_used_idx 0
3479 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3480 * kickfd 65 callfd 66 errfd -1
3483 * qsz 256 last_avail_idx 0 last_used_idx 0
3484 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3485 * kickfd 63 callfd 70 errfd -1
3488 * qsz 256 last_avail_idx 0 last_used_idx 0
3489 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3490 * kickfd 72 callfd 74 errfd -1
3492 * Virtqueue 4 (TX disabled)
3493 * qsz 256 last_avail_idx 0 last_used_idx 0
3494 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3495 * kickfd 76 callfd 78 errfd -1
3497 * Virtqueue 5 (RX disabled)
3498 * qsz 256 last_avail_idx 0 last_used_idx 0
3499 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3500 * kickfd 80 callfd 82 errfd -1
3502 * Virtqueue 6 (TX disabled)
3503 * qsz 256 last_avail_idx 0 last_used_idx 0
3504 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3505 * kickfd 84 callfd 86 errfd -1
3507 * Virtqueue 7 (RX disabled)
3508 * qsz 256 last_avail_idx 0 last_used_idx 0
3509 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3510 * kickfd 88 callfd 90 errfd -1
3514 * The optional '<em>descriptors</em>' parameter will display the same output as
3515 * the previous example but will include the descriptor table for each queue.
3516 * The output is truncated below:
3517 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3518 * Virtio vhost-user interfaces
3520 * coalesce frames 32 time 1e-3
3521 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3522 * virtio_net_hdr_sz 12
3523 * features mask (0xffffffffffffffff):
3524 * features (0x50408000):
3525 * VIRTIO_NET_F_MRG_RXBUF (15)
3526 * VIRTIO_NET_F_MQ (22)
3529 * qsz 256 last_avail_idx 0 last_used_idx 0
3530 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3531 * kickfd 62 callfd 64 errfd -1
3534 * id addr len flags next user_addr
3535 * ===== ================== ===== ====== ===== ==================
3536 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3537 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3538 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3539 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3540 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3541 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3542 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3543 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3544 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3545 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3546 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3548 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3549 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3550 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3551 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3552 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3553 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3554 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3557 * qsz 256 last_avail_idx 0 last_used_idx 0
3563 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3564 .path = "show vhost-user",
3565 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
3566 .function = show_vhost_user_command_fn,
3571 debug_vhost_user_command_fn (vlib_main_t * vm,
3572 unformat_input_t * input,
3573 vlib_cli_command_t * cmd)
3575 unformat_input_t _line_input, *line_input = &_line_input;
3576 clib_error_t *error = NULL;
3577 vhost_user_main_t *vum = &vhost_user_main;
3581 /* Get a line of input. */
3582 if (!unformat_user (input, unformat_line_input, line_input))
3583 return clib_error_return (0, "missing argument");
3585 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3589 error = clib_error_return (0, "unknown input `%U'",
3590 format_unformat_error, line_input);
3594 if (unformat (line_input, "on"))
3599 else if (unformat (line_input, "off"))
3606 error = clib_error_return (0, "unknown input `%U'",
3607 format_unformat_error, line_input);
3615 unformat_free (line_input);
3621 VLIB_CLI_COMMAND (debug_vhost_user_command, static) = {
3622 .path = "debug vhost-user",
3623 .short_help = "debug vhost-user <on | off>",
3624 .function = debug_vhost_user_command_fn,
3628 static clib_error_t *
3629 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
3631 vhost_user_main_t *vum = &vhost_user_main;
3633 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3635 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
3637 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
3639 else if (unformat (input, "dont-dump-memory"))
3640 vum->dont_dump_vhost_user_memory = 1;
3642 return clib_error_return (0, "unknown input `%U'",
3643 format_unformat_error, input);
3649 /* vhost-user { ... } configuration. */
3650 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3653 vhost_user_unmap_all (void)
3655 vhost_user_main_t *vum = &vhost_user_main;
3656 vhost_user_intf_t *vui;
3658 if (vum->dont_dump_vhost_user_memory)
3660 pool_foreach (vui, vum->vhost_user_interfaces,
3661 unmap_all_mem_regions (vui);
3667 * fd.io coding-style-patch-verification: ON
3670 * eval: (c-set-style "gnu")