2 *------------------------------------------------------------------
5 * Copyright (c) 2014 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/uio.h> /* for iovec */
27 #include <netinet/in.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
36 #include <vnet/ip/ip.h>
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/devices/devices.h>
40 #include <vnet/feature/feature.h>
42 #include <vnet/devices/virtio/vhost-user.h>
46 * @brief vHost User Device Driver.
48 * This file contains the source code for vHost User interface.
52 #define VHOST_USER_DEBUG_SOCKET 0
53 #define VHOST_DEBUG_VQ 0
55 #if VHOST_USER_DEBUG_SOCKET == 1
56 #define DBG_SOCK(args...) clib_warning(args);
58 #define DBG_SOCK(args...)
61 #if VHOST_DEBUG_VQ == 1
62 #define DBG_VQ(args...) clib_warning(args);
64 #define DBG_VQ(args...)
68 * When an RX queue is down but active, received packets
69 * must be discarded. This value controls up to how many
70 * packets will be discarded during each round.
72 #define VHOST_USER_DOWN_DISCARD_COUNT 256
75 * When the number of available buffers gets under this threshold,
76 * RX node will start discarding packets.
78 #define VHOST_USER_RX_BUFFER_STARVATION 32
81 * On the receive side, the host should free descriptors as soon
82 * as possible in order to avoid TX drop in the VM.
83 * This value controls the number of copy operations that are stacked
84 * before copy is done for all and descriptors are given back to
86 * The value 64 was obtained by testing (48 and 128 were not as good).
88 #define VHOST_USER_RX_COPY_THRESHOLD 64
90 #define UNIX_GET_FD(unixfd_idx) \
91 (unixfd_idx != ~0) ? \
92 pool_elt_at_index (unix_main.file_pool, \
93 unixfd_idx)->file_descriptor : -1;
95 #define foreach_virtio_trace_flags \
96 _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
97 _ (SINGLE_DESC, 1, "Single descriptor packet") \
98 _ (INDIRECT, 2, "Indirect descriptor") \
99 _ (MAP_ERROR, 4, "Memory mapping error")
103 #define _(n,i,s) VIRTIO_TRACE_F_##n,
104 foreach_virtio_trace_flags
106 } virtio_trace_flag_t;
108 vlib_node_registration_t vhost_user_input_node;
110 #define foreach_vhost_user_tx_func_error \
111 _(NONE, "no error") \
112 _(NOT_READY, "vhost vring not ready") \
113 _(DOWN, "vhost interface is down") \
114 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
115 _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
116 _(MMAP_FAIL, "mmap failure") \
117 _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
121 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
122 foreach_vhost_user_tx_func_error
124 VHOST_USER_TX_FUNC_N_ERROR,
125 } vhost_user_tx_func_error_t;
127 static char *vhost_user_tx_func_error_strings[] = {
129 foreach_vhost_user_tx_func_error
133 #define foreach_vhost_user_input_func_error \
134 _(NO_ERROR, "no error") \
135 _(NO_BUFFER, "no available buffer") \
136 _(MMAP_FAIL, "mmap failure") \
137 _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
138 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
139 _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
143 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
144 foreach_vhost_user_input_func_error
146 VHOST_USER_INPUT_FUNC_N_ERROR,
147 } vhost_user_input_func_error_t;
149 static char *vhost_user_input_func_error_strings[] = {
151 foreach_vhost_user_input_func_error
156 static vhost_user_main_t vhost_user_main = {
160 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
161 .name = "vhost-user",
166 format_vhost_user_interface_name (u8 * s, va_list * args)
168 u32 i = va_arg (*args, u32);
169 u32 show_dev_instance = ~0;
170 vhost_user_main_t *vum = &vhost_user_main;
172 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
173 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
175 if (show_dev_instance != ~0)
176 i = show_dev_instance;
178 s = format (s, "VirtualEthernet0/0/%d", i);
183 vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
185 // FIXME: check if the new dev instance is already used
186 vhost_user_main_t *vum = &vhost_user_main;
187 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
188 hi->dev_instance, ~0);
190 vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
193 DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
194 hi->dev_instance, new_dev_instance);
199 static_always_inline void *
200 map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint)
203 if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) &&
204 ((vui->regions[i].guest_phys_addr +
205 vui->regions[i].memory_size) > addr)))
207 return (void *) (vui->region_mmap_addr[i] + addr -
208 vui->regions[i].guest_phys_addr);
211 __m128i rl, rh, al, ah, r;
212 al = _mm_set1_epi64x (addr + 1);
213 ah = _mm_set1_epi64x (addr);
215 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]);
216 rl = _mm_cmpgt_epi64 (al, rl);
217 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]);
218 rh = _mm_cmpgt_epi64 (rh, ah);
219 r = _mm_and_si128 (rl, rh);
221 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]);
222 rl = _mm_cmpgt_epi64 (al, rl);
223 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]);
224 rh = _mm_cmpgt_epi64 (rh, ah);
225 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22);
227 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]);
228 rl = _mm_cmpgt_epi64 (al, rl);
229 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]);
230 rh = _mm_cmpgt_epi64 (rh, ah);
231 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44);
233 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]);
234 rl = _mm_cmpgt_epi64 (al, rl);
235 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]);
236 rh = _mm_cmpgt_epi64 (rh, ah);
237 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
239 r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
240 i = __builtin_ctzll (_mm_movemask_epi8 (r) |
241 (1 << VHOST_MEMORY_MAX_NREGIONS));
243 if (i < vui->nregions)
246 return (void *) (vui->region_mmap_addr[i] + addr -
247 vui->regions[i].guest_phys_addr);
251 for (i = 0; i < vui->nregions; i++)
253 if ((vui->regions[i].guest_phys_addr <= addr) &&
254 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
258 return (void *) (vui->region_mmap_addr[i] + addr -
259 vui->regions[i].guest_phys_addr);
263 DBG_VQ ("failed to map guest mem addr %llx", addr);
269 map_user_mem (vhost_user_intf_t * vui, uword addr)
272 for (i = 0; i < vui->nregions; i++)
274 if ((vui->regions[i].userspace_addr <= addr) &&
275 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
278 return (void *) (vui->region_mmap_addr[i] + addr -
279 vui->regions[i].userspace_addr);
286 get_huge_page_size (int fd)
294 unmap_all_mem_regions (vhost_user_intf_t * vui)
297 for (i = 0; i < vui->nregions; i++)
299 if (vui->region_mmap_addr[i] != (void *) -1)
302 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
304 ssize_t map_sz = (vui->regions[i].memory_size +
305 vui->regions[i].mmap_offset +
306 page_sz) & ~(page_sz - 1);
309 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
313 ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
314 vui->region_mmap_addr[i], map_sz, page_sz);
316 vui->region_mmap_addr[i] = (void *) -1;
320 clib_warning ("failed to unmap memory region (errno %d)",
323 close (vui->region_mmap_fd[i]);
330 vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
332 //Let's try to assign one queue to each thread
335 vui->use_tx_spinlock = 0;
338 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
340 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
341 if (!rxvq->started || !rxvq->enabled)
344 vui->per_cpu_tx_qid[cpu_index] = qid;
346 if (cpu_index == vlib_get_thread_main ()->n_vlib_mains)
349 //We need to loop, meaning the spinlock has to be used
350 vui->use_tx_spinlock = 1;
353 //Could not find a single valid one
355 cpu_index < vlib_get_thread_main ()->n_vlib_mains; cpu_index++)
357 vui->per_cpu_tx_qid[cpu_index] = 0;
365 vhost_user_rx_thread_placement ()
367 vhost_user_main_t *vum = &vhost_user_main;
368 vhost_user_intf_t *vui;
374 //Let's list all workers cpu indexes
376 for (i = vum->input_cpu_first_index;
377 i < vum->input_cpu_first_index + vum->input_cpu_count; i++)
379 vlib_node_set_state (vlib_mains[i], vhost_user_input_node.index,
380 VLIB_NODE_STATE_DISABLED);
381 vec_add1 (workers, i);
384 vec_foreach (vhc, vum->cpus)
386 vec_reset_length (vhc->rx_queues);
390 vhost_iface_and_queue_t iaq;
392 pool_foreach (vui, vum->vhost_user_interfaces, {
393 u32 *vui_workers = vec_len (vui->workers) ? vui->workers : workers;
395 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
397 vhost_user_vring_t *txvq =
398 &vui->vrings[VHOST_VRING_IDX_TX (qid)];
402 i %= vec_len (vui_workers);
403 cpu_index = vui_workers[i];
405 vhc = &vum->cpus[cpu_index];
408 iaq.vhost_iface_index = vui - vum->vhost_user_interfaces;
409 vec_add1 (vhc->rx_queues, iaq);
414 vec_foreach (vhc, vum->cpus)
416 vhost_iface_and_queue_t *vhiq;
417 u8 mode = VHOST_USER_INTERRUPT_MODE;
419 vec_foreach (vhiq, vhc->rx_queues)
421 vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
422 if (vui->operation_mode == VHOST_USER_POLLING_MODE)
424 /* At least one interface is polling, cpu is set to polling */
425 mode = VHOST_USER_POLLING_MODE;
429 vhc->operation_mode = mode;
432 for (cpu_index = vum->input_cpu_first_index;
433 cpu_index < vum->input_cpu_first_index + vum->input_cpu_count;
436 vlib_node_state_t state = VLIB_NODE_STATE_POLLING;
438 vhc = &vum->cpus[cpu_index];
439 vm = vlib_mains ? vlib_mains[cpu_index] : &vlib_global_main;
440 switch (vhc->operation_mode)
442 case VHOST_USER_INTERRUPT_MODE:
443 state = VLIB_NODE_STATE_INTERRUPT;
445 case VHOST_USER_POLLING_MODE:
446 state = VLIB_NODE_STATE_POLLING;
449 clib_warning ("BUG: bad operation mode %d", vhc->operation_mode);
452 vlib_node_set_state (vm, vhost_user_input_node.index, state);
459 vhost_user_thread_placement (u32 sw_if_index, u32 worker_thread_index, u8 del)
461 vhost_user_main_t *vum = &vhost_user_main;
462 vhost_user_intf_t *vui;
463 vnet_hw_interface_t *hw;
465 if (worker_thread_index < vum->input_cpu_first_index ||
466 worker_thread_index >=
467 vum->input_cpu_first_index + vum->input_cpu_count)
470 if (!(hw = vnet_get_sup_hw_interface (vnet_get_main (), sw_if_index)))
473 vui = pool_elt_at_index (vum->vhost_user_interfaces, hw->dev_instance);
475 vec_foreach (w, vui->workers)
477 if (*w == worker_thread_index)
479 found = w - vui->workers;
488 vec_del1 (vui->workers, found);
490 else if (found == ~0)
492 vec_add1 (vui->workers, worker_thread_index);
495 vhost_user_rx_thread_placement ();
499 /** @brief Returns whether at least one TX and one RX vring are enabled */
501 vhost_user_intf_ready (vhost_user_intf_t * vui)
503 int i, found[2] = { }; //RX + TX
505 for (i = 0; i < VHOST_VRING_MAX_N; i++)
506 if (vui->vrings[i].started && vui->vrings[i].enabled)
509 return found[0] && found[1];
513 vhost_user_update_iface_state (vhost_user_intf_t * vui)
515 /* if we have pointers to descriptor table, go up */
516 int is_up = vhost_user_intf_ready (vui);
517 if (is_up != vui->is_up)
519 DBG_SOCK ("interface %d %s", vui->sw_if_index,
520 is_up ? "ready" : "down");
521 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
522 is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP :
526 vhost_user_rx_thread_placement ();
527 vhost_user_tx_thread_placement (vui);
531 vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
533 vhost_user_main_t *vum = &vhost_user_main;
536 vhost_iface_and_queue_t *vhiq;
541 if (vhost_user_intf_ready (vui))
543 vec_foreach (vhc, vum->cpus)
545 if (vhc->operation_mode == VHOST_USER_POLLING_MODE)
548 vec_foreach (vhiq, vhc->rx_queues)
551 * Match the interface and the virtqueue number
553 if ((vhiq->vhost_iface_index == (ifq >> 8)) &&
554 (VHOST_VRING_IDX_TX (vhiq->qid) == (ifq & 0xff)))
556 cpu_index = vhc - vum->cpus;
557 vm = vlib_mains ? vlib_mains[cpu_index] : &vlib_global_main;
559 * Convert RX virtqueue number in the lower byte to vring
560 * queue index for the input node process. Top bytes contain
561 * the interface, lower byte contains the queue index.
563 ifq2 = ((ifq >> 8) << 8) | vhiq->qid;
564 vhc->pending_input_bitmap =
565 clib_bitmap_set (vhc->pending_input_bitmap, ifq2, 1);
566 vlib_node_set_interrupt_pending (vm,
567 vhost_user_input_node.index);
578 static clib_error_t *
579 vhost_user_callfd_read_ready (unix_file_t * uf)
581 __attribute__ ((unused)) int n;
583 vhost_user_intf_t *vui =
584 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
585 uf->private_data >> 8);
587 n = read (uf->file_descriptor, ((char *) &buff), 8);
588 DBG_SOCK ("if %d CALL queue %d", uf->private_data >> 8,
589 uf->private_data & 0xff);
590 vhost_user_set_interrupt_pending (vui, uf->private_data);
595 static clib_error_t *
596 vhost_user_kickfd_read_ready (unix_file_t * uf)
598 __attribute__ ((unused)) int n;
600 vhost_user_intf_t *vui =
601 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
602 uf->private_data >> 8);
603 u32 qid = uf->private_data & 0xff;
605 n = read (uf->file_descriptor, ((char *) &buff), 8);
606 DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
608 vlib_worker_thread_barrier_sync (vlib_get_main ());
609 if (!vui->vrings[qid].started ||
610 (vhost_user_intf_ready (vui) != vui->is_up))
612 vui->vrings[qid].started = 1;
613 vhost_user_update_iface_state (vui);
615 vlib_worker_thread_barrier_release (vlib_get_main ());
617 vhost_user_set_interrupt_pending (vui, uf->private_data);
622 * @brief Try once to lock the vring
623 * @return 0 on success, non-zero on failure.
626 vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
628 return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
632 * @brief Spin until the vring is successfully locked
635 vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
637 while (vhost_user_vring_try_lock (vui, qid))
642 * @brief Unlock the vring lock
645 vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
647 *vui->vring_locks[qid] = 0;
651 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
653 vhost_user_vring_t *vring = &vui->vrings[qid];
654 memset (vring, 0, sizeof (*vring));
655 vring->kickfd_idx = ~0;
656 vring->callfd_idx = ~0;
660 * We have a bug with some qemu 2.5, and this may be a fix.
661 * Feel like interpretation holy text, but this is from vhost-user.txt.
663 * One queue pair is enabled initially. More queues are enabled
664 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
666 * Don't know who's right, but this is what DPDK does.
668 if (qid == 0 || qid == 1)
673 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
675 vhost_user_vring_t *vring = &vui->vrings[qid];
676 if (vring->kickfd_idx != ~0)
678 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
680 unix_file_del (&unix_main, uf);
681 vring->kickfd_idx = ~0;
683 if (vring->callfd_idx != ~0)
685 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
687 unix_file_del (&unix_main, uf);
688 vring->callfd_idx = ~0;
690 if (vring->errfd != -1)
692 close (vring->errfd);
695 vhost_user_vring_init (vui, qid);
699 vhost_user_if_disconnect (vhost_user_intf_t * vui)
701 vnet_main_t *vnm = vnet_get_main ();
704 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
706 if (vui->unix_file_index != ~0)
708 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
709 vui->unix_file_index = ~0;
714 for (q = 0; q < VHOST_VRING_MAX_N; q++)
715 vhost_user_vring_close (vui, q);
717 unmap_all_mem_regions (vui);
718 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
721 #define VHOST_LOG_PAGE 0x1000
722 static_always_inline void
723 vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
724 u64 addr, u64 len, u8 is_host_address)
726 if (PREDICT_TRUE (vui->log_base_addr == 0
727 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
733 addr = (u64) map_user_mem (vui, (uword) addr);
735 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
737 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
741 CLIB_MEMORY_BARRIER ();
742 u64 page = addr / VHOST_LOG_PAGE;
743 while (page * VHOST_LOG_PAGE < addr + len)
745 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
750 static_always_inline void
751 vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
753 vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
756 #define vhost_user_log_dirty_ring(vui, vq, member) \
757 if (PREDICT_FALSE(vq->log_used)) { \
758 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
759 sizeof(vq->used->member)); \
762 static clib_error_t *
763 vhost_user_socket_read (unix_file_t * uf)
766 int fd, number_of_fds = 0;
767 int fds[VHOST_MEMORY_MAX_NREGIONS];
768 vhost_user_msg_t msg;
771 vhost_user_main_t *vum = &vhost_user_main;
772 vhost_user_intf_t *vui;
773 struct cmsghdr *cmsg;
775 unix_file_t template = { 0 };
776 vnet_main_t *vnm = vnet_get_main ();
778 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
780 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
782 memset (&mh, 0, sizeof (mh));
783 memset (control, 0, sizeof (control));
785 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
788 /* set the payload */
789 iov[0].iov_base = (void *) &msg;
790 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
794 mh.msg_control = control;
795 mh.msg_controllen = sizeof (control);
797 n = recvmsg (uf->file_descriptor, &mh, 0);
799 /* Stop workers to avoid end of the world */
800 vlib_worker_thread_barrier_sync (vlib_get_main ());
802 if (n != VHOST_USER_MSG_HDR_SZ)
806 DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
810 DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
811 n, VHOST_USER_MSG_HDR_SZ);
816 if (mh.msg_flags & MSG_CTRUNC)
818 DBG_SOCK ("MSG_CTRUNC is set");
822 cmsg = CMSG_FIRSTHDR (&mh);
824 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
825 (cmsg->cmsg_type == SCM_RIGHTS) &&
826 (cmsg->cmsg_len - CMSG_LEN (0) <=
827 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
829 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
830 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
833 /* version 1, no reply bit set */
834 if ((msg.flags & 7) != 1)
836 DBG_SOCK ("malformed message received. closing socket");
843 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
847 DBG_SOCK ("read failed %s", strerror (errno));
850 else if (rv != msg.size)
852 DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
859 case VHOST_USER_GET_FEATURES:
861 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
862 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
863 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
864 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
865 (1ULL << FEAT_VHOST_F_LOG_ALL) |
866 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
867 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
868 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
869 (1ULL << FEAT_VIRTIO_F_VERSION_1);
870 msg.u64 &= vui->feature_mask;
871 msg.size = sizeof (msg.u64);
872 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
873 vui->hw_if_index, msg.u64);
876 case VHOST_USER_SET_FEATURES:
877 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
878 vui->hw_if_index, msg.u64);
880 vui->features = msg.u64;
883 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
884 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
885 vui->virtio_net_hdr_sz = 12;
887 vui->virtio_net_hdr_sz = 10;
890 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
892 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
893 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
896 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
897 vhost_user_vring_close(&vui->vrings[q]); */
901 case VHOST_USER_SET_MEM_TABLE:
902 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
903 vui->hw_if_index, msg.memory.nregions);
905 if ((msg.memory.nregions < 1) ||
906 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
909 DBG_SOCK ("number of mem regions must be between 1 and %i",
910 VHOST_MEMORY_MAX_NREGIONS);
915 if (msg.memory.nregions != number_of_fds)
917 DBG_SOCK ("each memory region must have FD");
920 unmap_all_mem_regions (vui);
921 for (i = 0; i < msg.memory.nregions; i++)
923 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
924 sizeof (vhost_user_memory_region_t));
926 long page_sz = get_huge_page_size (fds[i]);
928 /* align size to 2M page */
929 ssize_t map_sz = (vui->regions[i].memory_size +
930 vui->regions[i].mmap_offset +
931 page_sz) & ~(page_sz - 1);
933 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
934 MAP_SHARED, fds[i], 0);
935 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
936 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
937 vui->regions[i].memory_size;
940 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
941 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
944 if (vui->region_mmap_addr[i] == MAP_FAILED)
946 clib_warning ("failed to map memory. errno is %d", errno);
949 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
950 vui->region_mmap_fd[i] = fds[i];
952 vui->nregions = msg.memory.nregions;
955 case VHOST_USER_SET_VRING_NUM:
956 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
957 vui->hw_if_index, msg.state.index, msg.state.num);
959 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
960 (msg.state.num == 0) || /* it cannot be zero */
961 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
963 vui->vrings[msg.state.index].qsz = msg.state.num;
966 case VHOST_USER_SET_VRING_ADDR:
967 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
968 vui->hw_if_index, msg.state.index);
970 if (msg.state.index >= VHOST_VRING_MAX_N)
972 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
973 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
977 if (msg.size < sizeof (msg.addr))
979 DBG_SOCK ("vhost message is too short (%d < %d)",
980 msg.size, sizeof (msg.addr));
984 vui->vrings[msg.state.index].desc = (vring_desc_t *)
985 map_user_mem (vui, msg.addr.desc_user_addr);
986 vui->vrings[msg.state.index].used = (vring_used_t *)
987 map_user_mem (vui, msg.addr.used_user_addr);
988 vui->vrings[msg.state.index].avail = (vring_avail_t *)
989 map_user_mem (vui, msg.addr.avail_user_addr);
991 if ((vui->vrings[msg.state.index].desc == NULL) ||
992 (vui->vrings[msg.state.index].used == NULL) ||
993 (vui->vrings[msg.state.index].avail == NULL))
995 DBG_SOCK ("failed to map user memory for hw_if_index %d",
1000 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
1001 vui->vrings[msg.state.index].log_used =
1002 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
1004 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
1005 the ring is initialized in an enabled state. */
1006 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
1008 vui->vrings[msg.state.index].enabled = 1;
1011 vui->vrings[msg.state.index].last_used_idx =
1012 vui->vrings[msg.state.index].last_avail_idx =
1013 vui->vrings[msg.state.index].used->idx;
1015 if (vui->operation_mode == VHOST_USER_POLLING_MODE)
1016 /* tell driver that we don't want interrupts */
1017 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
1019 /* tell driver that we want interrupts */
1020 vui->vrings[msg.state.index].used->flags = 0;
1023 case VHOST_USER_SET_OWNER:
1024 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
1027 case VHOST_USER_RESET_OWNER:
1028 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
1031 case VHOST_USER_SET_VRING_CALL:
1032 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
1033 vui->hw_if_index, msg.u64);
1035 q = (u8) (msg.u64 & 0xFF);
1037 /* if there is old fd, delete and close it */
1038 if (vui->vrings[q].callfd_idx != ~0)
1040 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
1041 vui->vrings[q].callfd_idx);
1042 unix_file_del (&unix_main, uf);
1043 vui->vrings[q].callfd_idx = ~0;
1046 if (!(msg.u64 & 0x100))
1048 if (number_of_fds != 1)
1050 DBG_SOCK ("More than one fd received !");
1054 template.read_function = vhost_user_callfd_read_ready;
1055 template.file_descriptor = fds[0];
1056 template.private_data =
1057 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
1058 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
1061 vui->vrings[q].callfd_idx = ~0;
1064 case VHOST_USER_SET_VRING_KICK:
1065 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
1066 vui->hw_if_index, msg.u64);
1068 q = (u8) (msg.u64 & 0xFF);
1070 if (vui->vrings[q].kickfd_idx != ~0)
1072 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
1073 vui->vrings[q].kickfd_idx);
1074 unix_file_del (&unix_main, uf);
1075 vui->vrings[q].kickfd_idx = ~0;
1078 if (!(msg.u64 & 0x100))
1080 if (number_of_fds != 1)
1082 DBG_SOCK ("More than one fd received !");
1086 template.read_function = vhost_user_kickfd_read_ready;
1087 template.file_descriptor = fds[0];
1088 template.private_data =
1089 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
1091 vui->vrings[q].kickfd_idx = unix_file_add (&unix_main, &template);
1095 //When no kickfd is set, the queue is initialized as started
1096 vui->vrings[q].kickfd_idx = ~0;
1097 vui->vrings[q].started = 1;
1102 case VHOST_USER_SET_VRING_ERR:
1103 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
1104 vui->hw_if_index, msg.u64);
1106 q = (u8) (msg.u64 & 0xFF);
1108 if (vui->vrings[q].errfd != -1)
1109 close (vui->vrings[q].errfd);
1111 if (!(msg.u64 & 0x100))
1113 if (number_of_fds != 1)
1116 vui->vrings[q].errfd = fds[0];
1119 vui->vrings[q].errfd = -1;
1123 case VHOST_USER_SET_VRING_BASE:
1124 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1125 vui->hw_if_index, msg.state.index, msg.state.num);
1127 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
1130 case VHOST_USER_GET_VRING_BASE:
1131 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1132 vui->hw_if_index, msg.state.index, msg.state.num);
1134 if (msg.state.index >= VHOST_VRING_MAX_N)
1136 DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
1137 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1142 * Copy last_avail_idx from the vring before closing it because
1143 * closing the vring also initializes the vring last_avail_idx
1145 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
1147 msg.size = sizeof (msg.state);
1149 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
1150 vhost_user_vring_close (vui, msg.state.index);
1153 case VHOST_USER_NONE:
1154 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
1158 case VHOST_USER_SET_LOG_BASE:
1160 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
1162 if (msg.size != sizeof (msg.log))
1165 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
1166 msg.size, sizeof (msg.log));
1171 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
1174 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1179 /* align size to 2M page */
1180 long page_sz = get_huge_page_size (fd);
1182 (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
1184 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1188 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
1189 map_sz, msg.log.offset, fd, vui->log_base_addr);
1191 if (vui->log_base_addr == MAP_FAILED)
1193 clib_warning ("failed to map memory. errno is %d", errno);
1197 vui->log_base_addr += msg.log.offset;
1198 vui->log_size = msg.log.size;
1201 msg.size = sizeof (msg.u64);
1206 case VHOST_USER_SET_LOG_FD:
1207 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1211 case VHOST_USER_GET_PROTOCOL_FEATURES:
1212 DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1216 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1217 (1 << VHOST_USER_PROTOCOL_F_MQ);
1218 msg.size = sizeof (msg.u64);
1221 case VHOST_USER_SET_PROTOCOL_FEATURES:
1222 DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
1223 vui->hw_if_index, msg.u64);
1225 vui->protocol_features = msg.u64;
1229 case VHOST_USER_GET_QUEUE_NUM:
1230 DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM", vui->hw_if_index);
1232 msg.u64 = VHOST_VRING_MAX_N;
1233 msg.size = sizeof (msg.u64);
1236 case VHOST_USER_SET_VRING_ENABLE:
1237 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1238 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1240 if (msg.state.index >= VHOST_VRING_MAX_N)
1242 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
1243 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1247 vui->vrings[msg.state.index].enabled = msg.state.num;
1251 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1256 /* if we need to reply */
1260 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1261 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1263 DBG_SOCK ("could not send message response");
1268 vhost_user_update_iface_state (vui);
1269 vlib_worker_thread_barrier_release (vlib_get_main ());
1273 vhost_user_if_disconnect (vui);
1274 vhost_user_update_iface_state (vui);
1275 vlib_worker_thread_barrier_release (vlib_get_main ());
1279 static clib_error_t *
1280 vhost_user_socket_error (unix_file_t * uf)
1282 vlib_main_t *vm = vlib_get_main ();
1283 vhost_user_main_t *vum = &vhost_user_main;
1284 vhost_user_intf_t *vui =
1285 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1287 DBG_SOCK ("socket error on if %d", vui->sw_if_index);
1288 vlib_worker_thread_barrier_sync (vm);
1289 vhost_user_if_disconnect (vui);
1290 vhost_user_rx_thread_placement ();
1291 vlib_worker_thread_barrier_release (vm);
1295 static clib_error_t *
1296 vhost_user_socksvr_accept_ready (unix_file_t * uf)
1298 int client_fd, client_len;
1299 struct sockaddr_un client;
1300 unix_file_t template = { 0 };
1301 vhost_user_main_t *vum = &vhost_user_main;
1302 vhost_user_intf_t *vui;
1304 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1306 client_len = sizeof (client);
1307 client_fd = accept (uf->file_descriptor,
1308 (struct sockaddr *) &client,
1309 (socklen_t *) & client_len);
1312 return clib_error_return_unix (0, "accept");
1314 DBG_SOCK ("New client socket for vhost interface %d", vui->sw_if_index);
1315 template.read_function = vhost_user_socket_read;
1316 template.error_function = vhost_user_socket_error;
1317 template.file_descriptor = client_fd;
1318 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1319 vui->unix_file_index = unix_file_add (&unix_main, &template);
1323 static clib_error_t *
1324 vhost_user_init (vlib_main_t * vm)
1326 clib_error_t *error;
1327 vhost_user_main_t *vum = &vhost_user_main;
1328 vlib_thread_main_t *tm = vlib_get_thread_main ();
1329 vlib_thread_registration_t *tr;
1332 error = vlib_call_init_function (vm, ip4_init);
1336 vum->coalesce_frames = 32;
1337 vum->coalesce_time = 1e-3;
1339 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1342 vec_foreach (cpu, vum->cpus)
1344 /* This is actually not necessary as validate already zeroes it
1345 * Just keeping the loop here for later because I am lazy. */
1346 cpu->rx_buffers_len = 0;
1349 /* find out which cpus will be used for input */
1350 vum->input_cpu_first_index = 0;
1351 vum->input_cpu_count = 1;
1352 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1353 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
1355 if (tr && tr->count > 0)
1357 vum->input_cpu_first_index = tr->first_index;
1358 vum->input_cpu_count = tr->count;
1361 vum->random = random_default_seed ();
1366 VLIB_INIT_FUNCTION (vhost_user_init);
1368 static clib_error_t *
1369 vhost_user_exit (vlib_main_t * vm)
1375 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1378 format_vhost_trace (u8 * s, va_list * va)
1380 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1381 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
1382 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
1383 vhost_user_main_t *vum = &vhost_user_main;
1384 vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
1385 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
1388 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
1390 uword indent = format_get_indent (s);
1392 s = format (s, "%U %U queue %d\n", format_white_space, indent,
1393 format_vnet_sw_interface_name, vnm, sw, t->qid);
1395 s = format (s, "%U virtio flags:\n", format_white_space, indent);
1397 if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
1398 s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
1399 foreach_virtio_trace_flags
1401 s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
1402 format_white_space, indent, t->first_desc_len);
1404 s = format (s, "%U flags 0x%02x gso_type %u\n",
1405 format_white_space, indent,
1406 t->hdr.hdr.flags, t->hdr.hdr.gso_type);
1408 if (vui->virtio_net_hdr_sz == 12)
1409 s = format (s, "%U num_buff %u",
1410 format_white_space, indent, t->hdr.num_buffers);
1416 vhost_user_rx_trace (vhost_trace_t * t,
1417 vhost_user_intf_t * vui, u16 qid,
1418 vlib_buffer_t * b, vhost_user_vring_t * txvq)
1420 vhost_user_main_t *vum = &vhost_user_main;
1421 u32 qsz_mask = txvq->qsz - 1;
1422 u32 last_avail_idx = txvq->last_avail_idx;
1423 u32 desc_current = txvq->avail->ring[last_avail_idx & qsz_mask];
1424 vring_desc_t *hdr_desc = 0;
1425 virtio_net_hdr_mrg_rxbuf_t *hdr;
1428 memset (t, 0, sizeof (*t));
1429 t->device_index = vui - vum->vhost_user_interfaces;
1432 hdr_desc = &txvq->desc[desc_current];
1433 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1435 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1436 /* Header is the first here */
1437 hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
1439 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1441 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1443 if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1444 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1446 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1449 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1451 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
1453 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
1457 u32 len = vui->virtio_net_hdr_sz;
1458 memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
1463 vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
1465 vhost_user_main_t *vum = &vhost_user_main;
1467 int fd = UNIX_GET_FD (vq->callfd_idx);
1468 int rv __attribute__ ((unused));
1469 /* TODO: pay attention to rv */
1470 rv = write (fd, &x, sizeof (x));
1471 vq->n_since_last_int = 0;
1472 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
1475 static_always_inline u32
1476 vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1477 u16 copy_len, u32 * map_hint)
1479 void *src0, *src1, *src2, *src3;
1480 if (PREDICT_TRUE (copy_len >= 4))
1482 if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
1484 if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
1487 while (PREDICT_TRUE (copy_len >= 4))
1493 (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
1496 (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
1499 CLIB_PREFETCH (src2, 64, LOAD);
1500 CLIB_PREFETCH (src3, 64, LOAD);
1502 clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
1503 clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
1510 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
1512 clib_memcpy ((void *) cpy->dst, src0, cpy->len);
1520 * Try to discard packets from the tx ring (VPP RX path).
1521 * Returns the number of discarded packets.
1524 vhost_user_rx_discard_packet (vlib_main_t * vm,
1525 vhost_user_intf_t * vui,
1526 vhost_user_vring_t * txvq, u32 discard_max)
1529 * On the RX side, each packet corresponds to one descriptor
1530 * (it is the same whether it is a shallow descriptor, chained, or indirect).
1531 * Therefore, discarding a packet is like discarding a descriptor.
1533 u32 discarded_packets = 0;
1534 u32 avail_idx = txvq->avail->idx;
1535 u16 qsz_mask = txvq->qsz - 1;
1536 while (discarded_packets != discard_max)
1538 if (avail_idx == txvq->last_avail_idx)
1541 u16 desc_chain_head =
1542 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1543 txvq->last_avail_idx++;
1544 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
1545 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1546 vhost_user_log_dirty_ring (vui, txvq,
1547 ring[txvq->last_used_idx & qsz_mask]);
1548 txvq->last_used_idx++;
1549 discarded_packets++;
1553 CLIB_MEMORY_BARRIER ();
1554 txvq->used->idx = txvq->last_used_idx;
1555 vhost_user_log_dirty_ring (vui, txvq, idx);
1556 return discarded_packets;
1560 * In case of overflow, we need to rewind the array of allocated buffers.
1563 vhost_user_input_rewind_buffers (vlib_main_t * vm,
1564 vhost_cpu_t * cpu, vlib_buffer_t * b_head)
1566 u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1567 vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
1568 b_current->current_length = 0;
1569 b_current->flags = 0;
1570 while (b_current != b_head)
1572 cpu->rx_buffers_len++;
1573 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1574 b_current = vlib_get_buffer (vm, bi_current);
1575 b_current->current_length = 0;
1576 b_current->flags = 0;
1581 vhost_user_if_input (vlib_main_t * vm,
1582 vhost_user_main_t * vum,
1583 vhost_user_intf_t * vui,
1584 u16 qid, vlib_node_runtime_t * node)
1586 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1587 u16 n_rx_packets = 0;
1590 u32 n_left_to_next, *to_next;
1591 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1592 u32 n_trace = vlib_get_trace_count (vm, node);
1595 u16 cpu_index = os_get_cpu_number ();
1599 /* do we have pending interrupts ? */
1600 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1601 f64 now = vlib_time_now (vm);
1603 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1604 vhost_user_send_call (vm, txvq);
1606 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1607 vhost_user_send_call (vm, rxvq);
1610 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
1613 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1616 if (PREDICT_FALSE (n_left == 0))
1619 if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
1622 * Discard input packet if interface is admin down or vring is not
1624 * "For example, for a networking device, in the disabled state
1625 * client must not supply any new RX packets, but must process
1626 * and discard any TX packets."
1628 vhost_user_rx_discard_packet (vm, vui, txvq,
1629 VHOST_USER_DOWN_DISCARD_COUNT);
1633 if (PREDICT_FALSE (n_left == txvq->qsz))
1636 * Informational error logging when VPP is not
1637 * receiving packets fast enough.
1639 vlib_error_count (vm, node->node_index,
1640 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1643 qsz_mask = txvq->qsz - 1;
1645 if (n_left > VLIB_FRAME_SIZE)
1646 n_left = VLIB_FRAME_SIZE;
1649 * For small packets (<2kB), we will not need more than one vlib buffer
1650 * per packet. In case packets are bigger, we will just yeld at some point
1651 * in the loop and come back later. This is not an issue as for big packet,
1652 * processing cost really comes from the memory copy.
1654 if (PREDICT_FALSE (vum->cpus[cpu_index].rx_buffers_len < n_left + 1))
1656 u32 curr_len = vum->cpus[cpu_index].rx_buffers_len;
1657 vum->cpus[cpu_index].rx_buffers_len +=
1658 vlib_buffer_alloc_from_free_list (vm,
1659 vum->cpus[cpu_index].rx_buffers +
1661 VHOST_USER_RX_BUFFERS_N - curr_len,
1662 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1665 (vum->cpus[cpu_index].rx_buffers_len <
1666 VHOST_USER_RX_BUFFER_STARVATION))
1668 /* In case of buffer starvation, discard some packets from the queue
1669 * and log the event.
1670 * We keep doing best effort for the remaining packets. */
1671 u32 flush = (n_left + 1 > vum->cpus[cpu_index].rx_buffers_len) ?
1672 n_left + 1 - vum->cpus[cpu_index].rx_buffers_len : 1;
1673 flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1676 vlib_increment_simple_counter (vnet_main.
1677 interface_main.sw_if_counters +
1678 VNET_INTERFACE_COUNTER_DROP,
1679 os_get_cpu_number (),
1680 vui->sw_if_index, flush);
1682 vlib_error_count (vm, vhost_user_input_node.index,
1683 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1689 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1691 while (n_left > 0 && n_left_to_next > 0)
1693 vlib_buffer_t *b_head, *b_current;
1696 u32 desc_data_offset;
1697 vring_desc_t *desc_table = txvq->desc;
1699 if (PREDICT_FALSE (vum->cpus[cpu_index].rx_buffers_len <= 1))
1701 /* Not enough rx_buffers
1702 * Note: We yeld on 1 so we don't need to do an additional
1703 * check for the next buffer prefetch.
1709 desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1710 vum->cpus[cpu_index].rx_buffers_len--;
1711 bi_current = (vum->cpus[cpu_index].rx_buffers)
1712 [vum->cpus[cpu_index].rx_buffers_len];
1713 b_head = b_current = vlib_get_buffer (vm, bi_current);
1714 to_next[0] = bi_current; //We do that now so we can forget about bi_current
1718 vlib_prefetch_buffer_with_index (vm,
1719 (vum->cpus[cpu_index].rx_buffers)
1720 [vum->cpus[cpu_index].
1721 rx_buffers_len - 1], LOAD);
1723 /* Just preset the used descriptor id and length for later */
1724 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_current;
1725 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1726 vhost_user_log_dirty_ring (vui, txvq,
1727 ring[txvq->last_used_idx & qsz_mask]);
1729 /* The buffer should already be initialized */
1730 b_head->total_length_not_including_first_buffer = 0;
1731 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1733 if (PREDICT_FALSE (n_trace))
1735 //TODO: next_index is not exactly known at that point
1736 vlib_trace_buffer (vm, node, next_index, b_head,
1737 /* follow_chain */ 0);
1739 vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1740 vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1742 vlib_set_trace_count (vm, node, n_trace);
1745 /* This depends on the setup but is very consistent
1746 * So I think the CPU branch predictor will make a pretty good job
1747 * at optimizing the decision. */
1748 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1750 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1753 if (PREDICT_FALSE (desc_table == 0))
1755 //FIXME: Handle error by shutdown the queue
1760 if (PREDICT_TRUE (vui->is_any_layout) ||
1761 (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
1763 /* ANYLAYOUT or single buffer */
1764 desc_data_offset = vui->virtio_net_hdr_sz;
1768 /* CSR case without ANYLAYOUT, skip 1st buffer */
1769 desc_data_offset = desc_table[desc_current].len;
1774 /* Get more input if necessary. Or end of packet. */
1775 if (desc_data_offset == desc_table[desc_current].len)
1777 if (PREDICT_FALSE (desc_table[desc_current].flags &
1780 desc_current = desc_table[desc_current].next;
1781 desc_data_offset = 0;
1789 /* Get more output if necessary. Or end of packet. */
1791 (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1794 (vum->cpus[cpu_index].rx_buffers_len == 0))
1796 /* Cancel speculation */
1801 * Checking if there are some left buffers.
1802 * If not, just rewind the used buffers and stop.
1803 * Note: Scheduled copies are not cancelled. This is
1804 * not an issue as they would still be valid. Useless,
1807 vhost_user_input_rewind_buffers (vm,
1808 &vum->cpus[cpu_index],
1814 /* Get next output */
1815 vum->cpus[cpu_index].rx_buffers_len--;
1817 (vum->cpus[cpu_index].rx_buffers)[vum->cpus
1818 [cpu_index].rx_buffers_len];
1819 b_current->next_buffer = bi_next;
1820 b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1821 bi_current = bi_next;
1822 b_current = vlib_get_buffer (vm, bi_current);
1825 /* Prepare a copy order executed later for the data */
1826 vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
1829 desc_table[desc_current].len - desc_data_offset;
1830 cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1831 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1832 cpy->dst = (uword) vlib_buffer_get_current (b_current);
1833 cpy->src = desc_table[desc_current].addr + desc_data_offset;
1835 desc_data_offset += cpy->len;
1837 b_current->current_length += cpy->len;
1838 b_head->total_length_not_including_first_buffer += cpy->len;
1842 CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1844 n_rx_bytes += b_head->total_length_not_including_first_buffer;
1847 b_head->total_length_not_including_first_buffer -=
1848 b_head->current_length;
1850 /* consume the descriptor and return it as used */
1851 txvq->last_avail_idx++;
1852 txvq->last_used_idx++;
1854 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1856 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1857 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1861 u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1863 /* redirect if feature path enabled */
1864 vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
1867 u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1868 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1869 to_next, n_left_to_next,
1876 * Although separating memory copies from virtio ring parsing
1877 * is beneficial, we can offer to perform the copies from time
1878 * to time in order to free some space in the ring.
1880 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1883 (vhost_user_input_copy (vui, vum->cpus[cpu_index].copy,
1884 copy_len, &map_hint)))
1887 ("Memory mapping error on interface hw_if_index=%d "
1888 "(Shutting down - Switch interface down and up to restart)",
1896 /* give buffers back to driver */
1897 CLIB_MEMORY_BARRIER ();
1898 txvq->used->idx = txvq->last_used_idx;
1899 vhost_user_log_dirty_ring (vui, txvq, idx);
1903 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1906 /* Do the memory copies */
1908 (vhost_user_input_copy (vui, vum->cpus[cpu_index].copy,
1909 copy_len, &map_hint)))
1911 clib_warning ("Memory mapping error on interface hw_if_index=%d "
1912 "(Shutting down - Switch interface down and up to restart)",
1917 /* give buffers back to driver */
1918 CLIB_MEMORY_BARRIER ();
1919 txvq->used->idx = txvq->last_used_idx;
1920 vhost_user_log_dirty_ring (vui, txvq, idx);
1922 /* interrupt (call) handling */
1923 if ((txvq->callfd_idx != ~0) &&
1924 !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
1926 txvq->n_since_last_int += n_rx_packets;
1928 if (txvq->n_since_last_int > vum->coalesce_frames)
1929 vhost_user_send_call (vm, txvq);
1932 /* increase rx counters */
1933 vlib_increment_combined_counter
1934 (vnet_main.interface_main.combined_sw_if_counters
1935 + VNET_INTERFACE_COUNTER_RX,
1936 os_get_cpu_number (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1938 vnet_device_increment_rx_packets (cpu_index, n_rx_packets);
1940 return n_rx_packets;
1944 vhost_user_input (vlib_main_t * vm,
1945 vlib_node_runtime_t * node, vlib_frame_t * f)
1947 vhost_user_main_t *vum = &vhost_user_main;
1948 uword n_rx_packets = 0;
1949 u32 cpu_index = os_get_cpu_number ();
1950 vhost_iface_and_queue_t *vhiq;
1951 vhost_user_intf_t *vui;
1954 vhc = &vum->cpus[cpu_index];
1955 if (PREDICT_TRUE (vhc->operation_mode == VHOST_USER_POLLING_MODE))
1957 vec_foreach (vhiq, vum->cpus[cpu_index].rx_queues)
1959 vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
1960 n_rx_packets += vhost_user_if_input (vm, vum, vui, vhiq->qid, node);
1968 clib_bitmap_foreach (i, vhc->pending_input_bitmap, ({
1971 clib_bitmap_set (vhc->pending_input_bitmap, i, 0);
1972 vui = pool_elt_at_index (vum->vhost_user_interfaces, i >> 8);
1973 n_rx_packets += vhost_user_if_input (vm, vum, vui, qid, node);
1977 return n_rx_packets;
1981 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1982 .function = vhost_user_input,
1983 .type = VLIB_NODE_TYPE_INPUT,
1984 .name = "vhost-user-input",
1985 .sibling_of = "device-input",
1987 /* Will be enabled if/when hardware is detected. */
1988 .state = VLIB_NODE_STATE_DISABLED,
1990 .format_buffer = format_ethernet_header_with_length,
1991 .format_trace = format_vhost_trace,
1993 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1994 .error_strings = vhost_user_input_func_error_strings,
1997 VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
2002 vhost_user_tx_trace (vhost_trace_t * t,
2003 vhost_user_intf_t * vui, u16 qid,
2004 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
2006 vhost_user_main_t *vum = &vhost_user_main;
2007 u32 qsz_mask = rxvq->qsz - 1;
2008 u32 last_avail_idx = rxvq->last_avail_idx;
2009 u32 desc_current = rxvq->avail->ring[last_avail_idx & qsz_mask];
2010 vring_desc_t *hdr_desc = 0;
2013 memset (t, 0, sizeof (*t));
2014 t->device_index = vui - vum->vhost_user_interfaces;
2017 hdr_desc = &rxvq->desc[desc_current];
2018 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
2020 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
2021 /* Header is the first here */
2022 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
2024 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
2026 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
2028 if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
2029 !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
2031 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
2034 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
2037 static_always_inline u32
2038 vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
2039 u16 copy_len, u32 * map_hint)
2041 void *dst0, *dst1, *dst2, *dst3;
2042 if (PREDICT_TRUE (copy_len >= 4))
2044 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
2046 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
2048 while (PREDICT_TRUE (copy_len >= 4))
2054 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
2057 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
2060 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
2061 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
2063 clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
2064 clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
2066 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
2067 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
2074 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
2076 clib_memcpy (dst0, (void *) cpy->src, cpy->len);
2077 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
2086 vhost_user_tx (vlib_main_t * vm,
2087 vlib_node_runtime_t * node, vlib_frame_t * frame)
2089 u32 *buffers = vlib_frame_args (frame);
2090 u32 n_left = frame->n_vectors;
2091 vhost_user_main_t *vum = &vhost_user_main;
2092 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
2093 vhost_user_intf_t *vui =
2094 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
2096 vhost_user_vring_t *rxvq;
2099 u32 cpu_index = os_get_cpu_number ();
2105 if (PREDICT_FALSE (!vui->admin_up))
2107 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
2111 if (PREDICT_FALSE (!vui->is_up))
2113 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
2118 VHOST_VRING_IDX_RX (*vec_elt_at_index
2119 (vui->per_cpu_tx_qid, os_get_cpu_number ()));
2120 rxvq = &vui->vrings[qid];
2121 if (PREDICT_FALSE (vui->use_tx_spinlock))
2122 vhost_user_vring_lock (vui, qid);
2124 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
2127 error = VHOST_USER_TX_FUNC_ERROR_NONE;
2132 vlib_buffer_t *b0, *current_b0;
2133 u16 desc_head, desc_index, desc_len;
2134 vring_desc_t *desc_table;
2135 uword buffer_map_addr;
2139 if (PREDICT_TRUE (n_left > 1))
2140 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2142 b0 = vlib_get_buffer (vm, buffers[0]);
2144 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2146 vum->cpus[cpu_index].current_trace =
2147 vlib_add_trace (vm, node, b0,
2148 sizeof (*vum->cpus[cpu_index].current_trace));
2149 vhost_user_tx_trace (vum->cpus[cpu_index].current_trace,
2150 vui, qid / 2, b0, rxvq);
2153 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2155 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2159 desc_table = rxvq->desc;
2160 desc_head = desc_index =
2161 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2163 /* Go deeper in case of indirect descriptor
2164 * I don't know of any driver providing indirect for RX. */
2165 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2168 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2170 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2175 map_guest_mem (vui, rxvq->desc[desc_index].addr,
2178 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2184 desc_len = vui->virtio_net_hdr_sz;
2185 buffer_map_addr = desc_table[desc_index].addr;
2186 buffer_len = desc_table[desc_index].len;
2189 // Get a header from the header array
2190 virtio_net_hdr_mrg_rxbuf_t *hdr =
2191 &vum->cpus[cpu_index].tx_headers[tx_headers_len];
2194 hdr->hdr.gso_type = 0;
2195 hdr->num_buffers = 1; //This is local, no need to check
2197 // Prepare a copy order executed later for the header
2198 vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
2200 cpy->len = vui->virtio_net_hdr_sz;
2201 cpy->dst = buffer_map_addr;
2202 cpy->src = (uword) hdr;
2205 buffer_map_addr += vui->virtio_net_hdr_sz;
2206 buffer_len -= vui->virtio_net_hdr_sz;
2207 bytes_left = b0->current_length;
2211 if (buffer_len == 0)
2213 if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2215 //Next one is chained
2216 desc_index = desc_table[desc_index].next;
2217 buffer_map_addr = desc_table[desc_index].addr;
2218 buffer_len = desc_table[desc_index].len;
2220 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2222 virtio_net_hdr_mrg_rxbuf_t *hdr =
2223 &vum->cpus[cpu_index].tx_headers[tx_headers_len - 1];
2225 //Move from available to used buffer
2226 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id =
2228 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len =
2230 vhost_user_log_dirty_ring (vui, rxvq,
2231 ring[rxvq->last_used_idx &
2234 rxvq->last_avail_idx++;
2235 rxvq->last_used_idx++;
2240 (rxvq->last_avail_idx == rxvq->avail->idx))
2242 //Dequeue queued descriptors for this packet
2243 rxvq->last_used_idx -= hdr->num_buffers - 1;
2244 rxvq->last_avail_idx -= hdr->num_buffers - 1;
2245 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2249 desc_table = rxvq->desc;
2250 desc_head = desc_index =
2251 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2253 (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2255 //It is seriously unlikely that a driver will put indirect descriptor
2256 //after non-indirect descriptor.
2258 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2260 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2266 rxvq->desc[desc_index].addr,
2269 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2274 buffer_map_addr = desc_table[desc_index].addr;
2275 buffer_len = desc_table[desc_index].len;
2279 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2285 vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
2287 cpy->len = bytes_left;
2288 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2289 cpy->dst = buffer_map_addr;
2290 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2291 current_b0->current_length - bytes_left;
2293 bytes_left -= cpy->len;
2294 buffer_len -= cpy->len;
2295 buffer_map_addr += cpy->len;
2296 desc_len += cpy->len;
2298 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
2301 // Check if vlib buffer has more data. If not, get more or break.
2302 if (PREDICT_TRUE (!bytes_left))
2305 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2307 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2308 bytes_left = current_b0->current_length;
2318 //Move from available to used ring
2319 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
2320 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
2321 vhost_user_log_dirty_ring (vui, rxvq,
2322 ring[rxvq->last_used_idx & qsz_mask]);
2323 rxvq->last_avail_idx++;
2324 rxvq->last_used_idx++;
2326 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2328 vum->cpus[cpu_index].current_trace->hdr =
2329 vum->cpus[cpu_index].tx_headers[tx_headers_len - 1];
2332 n_left--; //At the end for error counting when 'goto done' is invoked
2337 //Do the memory copies
2339 (vhost_user_tx_copy (vui, vum->cpus[cpu_index].copy,
2340 copy_len, &map_hint)))
2342 clib_warning ("Memory mapping error on interface hw_if_index=%d "
2343 "(Shutting down - Switch interface down and up to restart)",
2348 CLIB_MEMORY_BARRIER ();
2349 rxvq->used->idx = rxvq->last_used_idx;
2350 vhost_user_log_dirty_ring (vui, rxvq, idx);
2353 * When n_left is set, error is always set to something too.
2354 * In case error is due to lack of remaining buffers, we go back up and
2356 * The idea is that it is better to waste some time on packets
2357 * that have been processed already than dropping them and get
2358 * more fresh packets with a good likelyhood that they will be dropped too.
2359 * This technique also gives more time to VM driver to pick-up packets.
2360 * In case the traffic flows from physical to virtual interfaces, this
2361 * technique will end-up leveraging the physical NIC buffer in order to
2362 * absorb the VM's CPU jitter.
2364 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2370 /* interrupt (call) handling */
2371 if ((rxvq->callfd_idx != ~0) &&
2372 !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
2374 rxvq->n_since_last_int += frame->n_vectors - n_left;
2376 if (rxvq->n_since_last_int > vum->coalesce_frames)
2377 vhost_user_send_call (vm, rxvq);
2380 vhost_user_vring_unlock (vui, qid);
2383 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2385 vlib_error_count (vm, node->node_index, error, n_left);
2386 vlib_increment_simple_counter
2387 (vnet_main.interface_main.sw_if_counters
2388 + VNET_INTERFACE_COUNTER_DROP,
2389 os_get_cpu_number (), vui->sw_if_index, n_left);
2392 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2393 return frame->n_vectors;
2396 static clib_error_t *
2397 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
2400 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2401 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
2402 vhost_user_main_t *vum = &vhost_user_main;
2403 vhost_user_intf_t *vui =
2404 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
2406 vui->admin_up = is_up;
2409 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
2410 VNET_HW_INTERFACE_FLAG_LINK_UP);
2412 return /* no error */ 0;
2416 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2417 .name = "vhost-user",
2418 .tx_function = vhost_user_tx,
2419 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2420 .tx_function_error_strings = vhost_user_tx_func_error_strings,
2421 .format_device_name = format_vhost_user_interface_name,
2422 .name_renumber = vhost_user_name_renumber,
2423 .admin_up_down_function = vhost_user_interface_admin_up_down,
2424 .format_tx_trace = format_vhost_trace,
2427 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
2432 vhost_user_process (vlib_main_t * vm,
2433 vlib_node_runtime_t * rt, vlib_frame_t * f)
2435 vhost_user_main_t *vum = &vhost_user_main;
2436 vhost_user_intf_t *vui;
2437 struct sockaddr_un sun;
2439 unix_file_t template = { 0 };
2440 f64 timeout = 3153600000.0 /* 100 years */ ;
2441 uword *event_data = 0;
2443 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
2444 sun.sun_family = AF_UNIX;
2445 template.read_function = vhost_user_socket_read;
2446 template.error_function = vhost_user_socket_error;
2453 vlib_process_wait_for_event_or_clock (vm, timeout);
2454 vlib_process_get_events (vm, &event_data);
2455 vec_reset_length (event_data);
2460 pool_foreach (vui, vum->vhost_user_interfaces, {
2462 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
2463 if (vui->unix_file_index == ~0)
2465 /* try to connect */
2466 strncpy (sun.sun_path, (char *) vui->sock_filename,
2467 sizeof (sun.sun_path) - 1);
2469 /* Avoid hanging VPP if the other end does not accept */
2470 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2471 clib_unix_warning ("fcntl");
2473 if (connect (sockfd, (struct sockaddr *) &sun,
2474 sizeof (struct sockaddr_un)) == 0)
2476 /* Set the socket to blocking as it was before */
2477 if (fcntl(sockfd, F_SETFL, 0) < 0)
2478 clib_unix_warning ("fcntl2");
2480 vui->sock_errno = 0;
2481 template.file_descriptor = sockfd;
2482 template.private_data =
2483 vui - vhost_user_main.vhost_user_interfaces;
2484 vui->unix_file_index = unix_file_add (&unix_main, &template);
2486 //Re-open for next connect
2487 if ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
2488 clib_warning("Critical: Could not open unix socket");
2494 vui->sock_errno = errno;
2499 /* check if socket is alive */
2501 socklen_t len = sizeof (error);
2502 int fd = UNIX_GET_FD(vui->unix_file_index);
2504 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
2508 DBG_SOCK ("getsockopt returned %d", retval);
2509 vhost_user_if_disconnect (vui);
2520 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
2521 .function = vhost_user_process,
2522 .type = VLIB_NODE_TYPE_PROCESS,
2523 .name = "vhost-user-process",
2528 * Disables and reset interface structure.
2529 * It can then be either init again, or removed from used interfaces.
2532 vhost_user_term_if (vhost_user_intf_t * vui)
2536 // Delete configured thread pinning
2537 vec_reset_length (vui->workers);
2538 // disconnect interface sockets
2539 vhost_user_if_disconnect (vui);
2540 vhost_user_update_iface_state (vui);
2542 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2544 clib_mem_free ((void *) vui->vring_locks[q]);
2547 if (vui->unix_server_index != ~0)
2549 //Close server socket
2550 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
2551 vui->unix_server_index);
2552 unix_file_del (&unix_main, uf);
2553 vui->unix_server_index = ~0;
2558 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
2560 vhost_user_main_t *vum = &vhost_user_main;
2561 vhost_user_intf_t *vui;
2563 vnet_hw_interface_t *hwif;
2565 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2566 hwif->dev_class_index != vhost_user_dev_class.index)
2567 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2569 DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2570 hwif->name, hwif->dev_instance);
2572 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2574 // Disable and reset interface
2575 vhost_user_term_if (vui);
2577 // Reset renumbered iface
2578 if (hwif->dev_instance <
2579 vec_len (vum->show_dev_instance_by_real_dev_instance))
2580 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
2582 // Delete ethernet interface
2583 ethernet_delete_interface (vnm, vui->hw_if_index);
2586 pool_put (vum->vhost_user_interfaces, vui);
2592 * Open server unix socket on specified sock_filename.
2595 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
2598 struct sockaddr_un un = { };
2600 /* create listening socket */
2601 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2602 return VNET_API_ERROR_SYSCALL_ERROR_1;
2604 un.sun_family = AF_UNIX;
2605 strncpy ((char *) un.sun_path, (char *) sock_filename,
2606 sizeof (un.sun_path) - 1);
2608 /* remove if exists */
2609 unlink ((char *) sock_filename);
2611 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2613 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2617 if (listen (fd, 1) == -1)
2619 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2632 * Create ethernet interface for vhost user interface.
2635 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
2636 vhost_user_intf_t * vui, u8 * hwaddress)
2638 vhost_user_main_t *vum = &vhost_user_main;
2640 clib_error_t *error;
2642 /* create hw and sw interface */
2645 clib_memcpy (hwaddr, hwaddress, 6);
2649 random_u32 (&vum->random);
2650 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
2655 error = ethernet_register_interface
2657 vhost_user_dev_class.index,
2658 vui - vum->vhost_user_interfaces /* device instance */ ,
2659 hwaddr /* ethernet address */ ,
2660 &vui->hw_if_index, 0 /* flag change */ );
2663 clib_error_report (error);
2665 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
2666 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
2670 * Initialize vui with specified attributes
2673 vhost_user_vui_init (vnet_main_t * vnm,
2674 vhost_user_intf_t * vui,
2676 const char *sock_filename,
2677 u64 feature_mask, u32 * sw_if_index, u8 operation_mode)
2679 vnet_sw_interface_t *sw;
2680 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
2683 if (server_sock_fd != -1)
2685 unix_file_t template = { 0 };
2686 template.read_function = vhost_user_socksvr_accept_ready;
2687 template.file_descriptor = server_sock_fd;
2688 template.private_data = vui - vhost_user_main.vhost_user_interfaces; //hw index
2689 vui->unix_server_index = unix_file_add (&unix_main, &template);
2693 vui->unix_server_index = ~0;
2696 vui->sw_if_index = sw->sw_if_index;
2697 strncpy (vui->sock_filename, sock_filename,
2698 ARRAY_LEN (vui->sock_filename) - 1);
2699 vui->sock_errno = 0;
2701 vui->feature_mask = feature_mask;
2702 vui->unix_file_index = ~0;
2703 vui->log_base_addr = 0;
2704 vui->operation_mode = operation_mode;
2706 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2707 vhost_user_vring_init (vui, q);
2709 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
2712 *sw_if_index = vui->sw_if_index;
2714 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2716 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
2717 CLIB_CACHE_LINE_BYTES);
2718 memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
2721 vec_validate (vui->per_cpu_tx_qid,
2722 vlib_get_thread_main ()->n_vlib_mains - 1);
2723 vhost_user_tx_thread_placement (vui);
2727 vhost_user_send_interrupt_process (vlib_main_t * vm,
2728 vlib_node_runtime_t * rt, vlib_frame_t * f)
2730 vhost_user_intf_t *vui;
2731 f64 timeout = 3153600000.0 /* 100 years */ ;
2732 uword event_type, *event_data = 0;
2733 vhost_user_main_t *vum = &vhost_user_main;
2734 vhost_iface_and_queue_t *vhiq;
2736 f64 now, poll_time_remaining;
2740 poll_time_remaining =
2741 vlib_process_wait_for_event_or_clock (vm, timeout);
2742 event_type = vlib_process_get_events (vm, &event_data);
2743 vec_reset_length (event_data);
2746 * Use the remaining timeout if it is less than coalesce time to avoid
2747 * resetting the existing timer in the middle of expiration
2749 timeout = poll_time_remaining;
2750 if (vlib_process_suspend_time_is_zero (timeout) ||
2751 (timeout > vum->coalesce_time))
2752 timeout = vum->coalesce_time;
2754 now = vlib_time_now (vm);
2757 case VHOST_USER_EVENT_START_TIMER:
2758 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
2763 vec_foreach (vhc, vum->cpus)
2765 u32 cpu_index = vhc - vum->cpus;
2768 next_timeout = timeout;
2769 vec_foreach (vhiq, vum->cpus[cpu_index].rx_queues)
2771 vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
2772 vhost_user_vring_t *rxvq =
2773 &vui->vrings[VHOST_VRING_IDX_RX (vhiq->qid)];
2774 vhost_user_vring_t *txvq =
2775 &vui->vrings[VHOST_VRING_IDX_TX (vhiq->qid)];
2777 if (txvq->n_since_last_int)
2779 if (now >= txvq->int_deadline)
2780 vhost_user_send_call (vm, txvq);
2782 next_timeout = txvq->int_deadline - now;
2785 if (rxvq->n_since_last_int)
2787 if (now >= rxvq->int_deadline)
2788 vhost_user_send_call (vm, rxvq);
2790 next_timeout = rxvq->int_deadline - now;
2793 if ((next_timeout < timeout) && (next_timeout > 0.0))
2794 timeout = next_timeout;
2800 clib_warning ("BUG: unhandled event type %d", event_type);
2808 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node,static) = {
2809 .function = vhost_user_send_interrupt_process,
2810 .type = VLIB_NODE_TYPE_PROCESS,
2811 .name = "vhost-user-send-interrupt-process",
2816 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2817 const char *sock_filename,
2821 u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
2824 vhost_user_intf_t *vui = NULL;
2827 int server_sock_fd = -1;
2828 vhost_user_main_t *vum = &vhost_user_main;
2830 if ((operation_mode != VHOST_USER_POLLING_MODE) &&
2831 (operation_mode != VHOST_USER_INTERRUPT_MODE))
2832 return VNET_API_ERROR_UNIMPLEMENTED;
2834 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2836 return VNET_API_ERROR_INVALID_ARGUMENT;
2842 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
2848 pool_get (vhost_user_main.vhost_user_interfaces, vui);
2850 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
2851 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
2852 feature_mask, &sw_if_idx, operation_mode);
2855 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2858 *sw_if_index = sw_if_idx;
2860 // Process node must connect
2861 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2863 if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
2864 !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
2865 (vum->coalesce_frames > 0))
2867 vum->interrupt_mode = 1;
2868 vlib_process_signal_event (vm, vhost_user_send_interrupt_node.index,
2869 VHOST_USER_EVENT_START_TIMER, 0);
2875 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2876 const char *sock_filename,
2879 u64 feature_mask, u8 renumber, u32 custom_dev_instance,
2882 vhost_user_main_t *vum = &vhost_user_main;
2883 vhost_user_intf_t *vui = NULL;
2885 int server_sock_fd = -1;
2887 vnet_hw_interface_t *hwif;
2889 if ((operation_mode != VHOST_USER_POLLING_MODE) &&
2890 (operation_mode != VHOST_USER_INTERRUPT_MODE))
2891 return VNET_API_ERROR_UNIMPLEMENTED;
2892 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2893 hwif->dev_class_index != vhost_user_dev_class.index)
2894 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2896 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2898 // First try to open server socket
2900 if ((rv = vhost_user_init_server_sock (sock_filename,
2901 &server_sock_fd)) != 0)
2904 vhost_user_term_if (vui);
2905 vhost_user_vui_init (vnm, vui, server_sock_fd,
2906 sock_filename, feature_mask, &sw_if_idx,
2910 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2912 // Process node must connect
2913 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2915 if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
2916 !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
2917 (vum->coalesce_frames > 0))
2919 vum->interrupt_mode = 1;
2920 vlib_process_signal_event (vm, vhost_user_send_interrupt_node.index,
2921 VHOST_USER_EVENT_START_TIMER, 0);
2927 unformat_vhost_user_operation_mode (unformat_input_t * input, va_list * args)
2929 u8 *operation_mode = va_arg (*args, u8 *);
2932 if (unformat (input, "interrupt"))
2933 *operation_mode = VHOST_USER_INTERRUPT_MODE;
2934 else if (unformat (input, "polling"))
2935 *operation_mode = VHOST_USER_POLLING_MODE;
2943 vhost_user_connect_command_fn (vlib_main_t * vm,
2944 unformat_input_t * input,
2945 vlib_cli_command_t * cmd)
2947 unformat_input_t _line_input, *line_input = &_line_input;
2948 u8 *sock_filename = NULL;
2951 u64 feature_mask = (u64) ~ (0ULL);
2953 u32 custom_dev_instance = ~0;
2956 clib_error_t *error = NULL;
2957 u8 operation_mode = VHOST_USER_POLLING_MODE;
2959 /* Get a line of input. */
2960 if (!unformat_user (input, unformat_line_input, line_input))
2963 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2965 if (unformat (line_input, "socket %s", &sock_filename))
2967 else if (unformat (line_input, "server"))
2969 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2973 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
2975 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
2979 else if (unformat (line_input, "mode %U",
2980 unformat_vhost_user_operation_mode, &operation_mode))
2984 error = clib_error_return (0, "unknown input `%U'",
2985 format_unformat_error, line_input);
2990 vnet_main_t *vnm = vnet_get_main ();
2993 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
2994 is_server, &sw_if_index, feature_mask,
2995 renumber, custom_dev_instance, hw,
2998 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
3002 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
3006 vec_free (sock_filename);
3007 unformat_free (line_input);
3013 vhost_user_delete_command_fn (vlib_main_t * vm,
3014 unformat_input_t * input,
3015 vlib_cli_command_t * cmd)
3017 unformat_input_t _line_input, *line_input = &_line_input;
3018 u32 sw_if_index = ~0;
3019 vnet_main_t *vnm = vnet_get_main ();
3020 clib_error_t *error = NULL;
3022 /* Get a line of input. */
3023 if (!unformat_user (input, unformat_line_input, line_input))
3026 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3028 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
3031 (line_input, "%U", unformat_vnet_sw_interface, vnm,
3034 vnet_hw_interface_t *hwif =
3035 vnet_get_sup_hw_interface (vnm, sw_if_index);
3037 vhost_user_dev_class.index != hwif->dev_class_index)
3039 error = clib_error_return (0, "Not a vhost interface");
3045 error = clib_error_return (0, "unknown input `%U'",
3046 format_unformat_error, line_input);
3051 vhost_user_delete_if (vnm, vm, sw_if_index);
3054 unformat_free (line_input);
3060 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
3061 vhost_user_intf_details_t ** out_vuids)
3064 vhost_user_main_t *vum = &vhost_user_main;
3065 vhost_user_intf_t *vui;
3066 vhost_user_intf_details_t *r_vuids = NULL;
3067 vhost_user_intf_details_t *vuid = NULL;
3068 u32 *hw_if_indices = 0;
3069 vnet_hw_interface_t *hi;
3076 pool_foreach (vui, vum->vhost_user_interfaces,
3077 vec_add1 (hw_if_indices, vui->hw_if_index);
3080 for (i = 0; i < vec_len (hw_if_indices); i++)
3082 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3083 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
3085 vec_add2 (r_vuids, vuid, 1);
3086 vuid->operation_mode = vui->operation_mode;
3087 vuid->sw_if_index = vui->sw_if_index;
3088 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
3089 vuid->features = vui->features;
3090 vuid->num_regions = vui->nregions;
3091 vuid->is_server = vui->unix_server_index != ~0;
3092 vuid->sock_errno = vui->sock_errno;
3093 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
3094 ARRAY_LEN (vuid->sock_filename) - 1);
3096 s = format (s, "%v%c", hi->name, 0);
3098 strncpy ((char *) vuid->if_name, (char *) s,
3099 ARRAY_LEN (vuid->if_name) - 1);
3104 vec_free (hw_if_indices);
3106 *out_vuids = r_vuids;
3112 format_vhost_user_operation_mode (u8 * s, va_list * va)
3114 int operation_mode = va_arg (*va, int);
3116 switch (operation_mode)
3118 case VHOST_USER_POLLING_MODE:
3119 s = format (s, "%s", "polling");
3121 case VHOST_USER_INTERRUPT_MODE:
3122 s = format (s, "%s", "interrupt");
3125 s = format (s, "%s", "invalid");
3131 show_vhost_user_command_fn (vlib_main_t * vm,
3132 unformat_input_t * input,
3133 vlib_cli_command_t * cmd)
3135 clib_error_t *error = 0;
3136 vnet_main_t *vnm = vnet_get_main ();
3137 vhost_user_main_t *vum = &vhost_user_main;
3138 vhost_user_intf_t *vui;
3139 u32 hw_if_index, *hw_if_indices = 0;
3140 vnet_hw_interface_t *hi;
3142 vhost_iface_and_queue_t *vhiq;
3152 struct feat_struct *feat_entry;
3154 static struct feat_struct feat_array[] = {
3155 #define _(s,b) { .str = #s, .bit = b, },
3156 foreach_virtio_net_feature
3161 #define foreach_protocol_feature \
3162 _(VHOST_USER_PROTOCOL_F_MQ) \
3163 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
3165 static struct feat_struct proto_feat_array[] = {
3166 #define _(s) { .str = #s, .bit = s},
3167 foreach_protocol_feature
3172 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3175 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
3177 vec_add1 (hw_if_indices, hw_if_index);
3179 else if (unformat (input, "descriptors") || unformat (input, "desc"))
3183 error = clib_error_return (0, "unknown input `%U'",
3184 format_unformat_error, input);
3188 if (vec_len (hw_if_indices) == 0)
3190 pool_foreach (vui, vum->vhost_user_interfaces,
3191 vec_add1 (hw_if_indices, vui->hw_if_index);
3194 vlib_cli_output (vm, "Virtio vhost-user interfaces");
3195 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
3196 vum->coalesce_frames, vum->coalesce_time);
3198 for (i = 0; i < vec_len (hw_if_indices); i++)
3200 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3201 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
3202 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
3203 hi->name, hw_if_indices[i]);
3205 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
3206 " features mask (0x%llx): \n"
3207 " features (0x%llx): \n",
3208 vui->virtio_net_hdr_sz, vui->feature_mask,
3211 feat_entry = (struct feat_struct *) &feat_array;
3212 while (feat_entry->str)
3214 if (vui->features & (1ULL << feat_entry->bit))
3215 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3220 vlib_cli_output (vm, " protocol features (0x%llx)",
3221 vui->protocol_features);
3222 feat_entry = (struct feat_struct *) &proto_feat_array;
3223 while (feat_entry->str)
3225 if (vui->protocol_features & (1ULL << feat_entry->bit))
3226 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3231 vlib_cli_output (vm, "\n");
3233 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
3235 (vui->unix_server_index != ~0) ? "server" : "client",
3236 strerror (vui->sock_errno));
3238 vlib_cli_output (vm, " configured mode: %U\n",
3239 format_vhost_user_operation_mode, vui->operation_mode);
3240 vlib_cli_output (vm, " rx placement: ");
3241 vec_foreach (vhc, vum->cpus)
3243 vec_foreach (vhiq, vhc->rx_queues)
3245 if (vhiq->vhost_iface_index == vui - vum->vhost_user_interfaces)
3247 vlib_cli_output (vm, " thread %d on vring %d\n",
3249 VHOST_VRING_IDX_TX (vhiq->qid));
3250 vlib_cli_output (vm, " mode: %U\n",
3251 format_vhost_user_operation_mode,
3252 vhc->operation_mode);
3257 vlib_cli_output (vm, " tx placement: %s\n",
3258 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
3260 vec_foreach_index (ci, vui->per_cpu_tx_qid)
3262 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
3263 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
3266 vlib_cli_output (vm, "\n");
3268 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
3272 vlib_cli_output (vm,
3273 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
3274 vlib_cli_output (vm,
3275 " ====== ===== ================== ================== ================== ================== ==================\n");
3277 for (j = 0; j < vui->nregions; j++)
3279 vlib_cli_output (vm,
3280 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
3281 j, vui->region_mmap_fd[j],
3282 vui->regions[j].guest_phys_addr,
3283 vui->regions[j].memory_size,
3284 vui->regions[j].userspace_addr,
3285 vui->regions[j].mmap_offset,
3286 pointer_to_uword (vui->region_mmap_addr[j]));
3288 for (q = 0; q < VHOST_VRING_MAX_N; q++)
3290 if (!vui->vrings[q].started)
3293 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
3294 (q & 1) ? "RX" : "TX",
3295 vui->vrings[q].enabled ? "" : " disabled");
3297 vlib_cli_output (vm,
3298 " qsz %d last_avail_idx %d last_used_idx %d\n",
3299 vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
3300 vui->vrings[q].last_used_idx);
3302 if (vui->vrings[q].avail && vui->vrings[q].used)
3303 vlib_cli_output (vm,
3304 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
3305 vui->vrings[q].avail->flags,
3306 vui->vrings[q].avail->idx,
3307 vui->vrings[q].used->flags,
3308 vui->vrings[q].used->idx);
3310 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
3311 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
3312 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
3313 kickfd, callfd, vui->vrings[q].errfd);
3317 vlib_cli_output (vm, "\n descriptor table:\n");
3318 vlib_cli_output (vm,
3319 " id addr len flags next user_addr\n");
3320 vlib_cli_output (vm,
3321 " ===== ================== ===== ====== ===== ==================\n");
3322 for (j = 0; j < vui->vrings[q].qsz; j++)
3325 vlib_cli_output (vm,
3326 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3327 j, vui->vrings[q].desc[j].addr,
3328 vui->vrings[q].desc[j].len,
3329 vui->vrings[q].desc[j].flags,
3330 vui->vrings[q].desc[j].next,
3331 pointer_to_uword (map_guest_mem
3333 vui->vrings[q].desc[j].
3338 vlib_cli_output (vm, "\n");
3341 vec_free (hw_if_indices);
3350 * Create a vHost User interface. Once created, a new virtual interface
3351 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3352 * is the next free index.
3354 * There are several parameters associated with a vHost interface:
3356 * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3357 * VPP to manage the vHost interface. If socket does not already exist, VPP will
3358 * create the socket.
3360 * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3361 * linux socket. If not provided, VPP will be the client.
3363 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3364 * startup. By default, all supported features will be advertised. Otherwise,
3365 * provide the set of features desired.
3366 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3367 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3368 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3369 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3370 * - 0x004000000 (26) - VHOST_F_LOG_ALL
3371 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3372 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3373 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3374 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3376 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3377 * X:X:X:X:X:X unix or X.X.X cisco format.
3379 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3380 * in the name to be specified. If instance already exists, name will be used
3381 * anyway and multiple instances will have the same name. Use with caution.
3383 * - <b>mode [interrupt | polling]</b> - Optional parameter specifying
3384 * the input thread polling policy.
3387 * Example of how to create a vhost interface with VPP as the client and all features enabled:
3388 * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3389 * VirtualEthernet0/0/0
3391 * Example of how to create a vhost interface with VPP as the server and with just
3392 * multiple queues enabled:
3393 * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3394 * VirtualEthernet0/0/1
3396 * Once the vHost interface is created, enable the interface using:
3397 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3400 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3401 .path = "create vhost-user",
3402 .short_help = "create vhost-user socket <socket-filename> [server] "
3403 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] "
3404 "[mode {interrupt | polling}]",
3405 .function = vhost_user_connect_command_fn,
3410 * Delete a vHost User interface using the interface name or the
3411 * software interface index. Use the '<em>show interface</em>'
3412 * command to determine the software interface index. On deletion,
3413 * the linux socket will not be deleted.
3416 * Example of how to delete a vhost interface by name:
3417 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3418 * Example of how to delete a vhost interface by software interface index:
3419 * @cliexcmd{delete vhost-user sw_if_index 1}
3422 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3423 .path = "delete vhost-user",
3424 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
3425 .function = vhost_user_delete_command_fn,
3429 * Display the attributes of a single vHost User interface (provide interface
3430 * name), multiple vHost User interfaces (provide a list of interface names seperated
3431 * by spaces) or all Vhost User interfaces (omit an interface name to display all
3432 * vHost interfaces).
3436 * Example of how to display a vhost interface:
3437 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3438 * Virtio vhost-user interfaces
3440 * coalesce frames 32 time 1e-3
3441 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3442 * virtio_net_hdr_sz 12
3443 * features mask (0xffffffffffffffff):
3444 * features (0x50408000):
3445 * VIRTIO_NET_F_MRG_RXBUF (15)
3446 * VIRTIO_NET_F_MQ (22)
3447 * VIRTIO_F_INDIRECT_DESC (28)
3448 * VHOST_USER_F_PROTOCOL_FEATURES (30)
3449 * protocol features (0x3)
3450 * VHOST_USER_PROTOCOL_F_MQ (0)
3451 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3453 * socket filename /tmp/vhost1.sock type client errno "Success"
3456 * thread 1 on vring 1
3457 * thread 1 on vring 5
3458 * thread 2 on vring 3
3459 * thread 2 on vring 7
3460 * tx placement: spin-lock
3461 * thread 0 on vring 0
3462 * thread 1 on vring 2
3463 * thread 2 on vring 0
3465 * Memory regions (total 2)
3466 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3467 * ====== ===== ================== ================== ================== ================== ==================
3468 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3469 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3472 * qsz 256 last_avail_idx 0 last_used_idx 0
3473 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3474 * kickfd 62 callfd 64 errfd -1
3477 * qsz 256 last_avail_idx 0 last_used_idx 0
3478 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3479 * kickfd 65 callfd 66 errfd -1
3482 * qsz 256 last_avail_idx 0 last_used_idx 0
3483 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3484 * kickfd 63 callfd 70 errfd -1
3487 * qsz 256 last_avail_idx 0 last_used_idx 0
3488 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3489 * kickfd 72 callfd 74 errfd -1
3491 * Virtqueue 4 (TX disabled)
3492 * qsz 256 last_avail_idx 0 last_used_idx 0
3493 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3494 * kickfd 76 callfd 78 errfd -1
3496 * Virtqueue 5 (RX disabled)
3497 * qsz 256 last_avail_idx 0 last_used_idx 0
3498 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3499 * kickfd 80 callfd 82 errfd -1
3501 * Virtqueue 6 (TX disabled)
3502 * qsz 256 last_avail_idx 0 last_used_idx 0
3503 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3504 * kickfd 84 callfd 86 errfd -1
3506 * Virtqueue 7 (RX disabled)
3507 * qsz 256 last_avail_idx 0 last_used_idx 0
3508 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3509 * kickfd 88 callfd 90 errfd -1
3513 * The optional '<em>descriptors</em>' parameter will display the same output as
3514 * the previous example but will include the descriptor table for each queue.
3515 * The output is truncated below:
3516 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3517 * Virtio vhost-user interfaces
3519 * coalesce frames 32 time 1e-3
3520 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3521 * virtio_net_hdr_sz 12
3522 * features mask (0xffffffffffffffff):
3523 * features (0x50408000):
3524 * VIRTIO_NET_F_MRG_RXBUF (15)
3525 * VIRTIO_NET_F_MQ (22)
3528 * qsz 256 last_avail_idx 0 last_used_idx 0
3529 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3530 * kickfd 62 callfd 64 errfd -1
3533 * id addr len flags next user_addr
3534 * ===== ================== ===== ====== ===== ==================
3535 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3536 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3537 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3538 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3539 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3540 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3541 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3542 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3543 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3544 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3545 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3547 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3548 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3549 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3550 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3551 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3552 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3553 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3556 * qsz 256 last_avail_idx 0 last_used_idx 0
3562 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3563 .path = "show vhost-user",
3564 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
3565 .function = show_vhost_user_command_fn,
3569 static clib_error_t *
3570 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
3572 vhost_user_main_t *vum = &vhost_user_main;
3574 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3576 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
3578 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
3580 else if (unformat (input, "dont-dump-memory"))
3581 vum->dont_dump_vhost_user_memory = 1;
3583 return clib_error_return (0, "unknown input `%U'",
3584 format_unformat_error, input);
3590 /* vhost-user { ... } configuration. */
3591 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3594 vhost_user_unmap_all (void)
3596 vhost_user_main_t *vum = &vhost_user_main;
3597 vhost_user_intf_t *vui;
3599 if (vum->dont_dump_vhost_user_memory)
3601 pool_foreach (vui, vum->vhost_user_interfaces,
3602 unmap_all_mem_regions (vui);
3607 static clib_error_t *
3608 vhost_thread_command_fn (vlib_main_t * vm,
3609 unformat_input_t * input, vlib_cli_command_t * cmd)
3611 unformat_input_t _line_input, *line_input = &_line_input;
3612 u32 worker_thread_index;
3616 clib_error_t *error = NULL;
3618 /* Get a line of input. */
3619 if (!unformat_user (input, unformat_line_input, line_input))
3623 (line_input, "%U %d", unformat_vnet_sw_interface, vnet_get_main (),
3624 &sw_if_index, &worker_thread_index))
3626 error = clib_error_return (0, "unknown input `%U'",
3627 format_unformat_error, line_input);
3631 if (unformat (line_input, "del"))
3635 vhost_user_thread_placement (sw_if_index, worker_thread_index, del)))
3637 error = clib_error_return (0, "vhost_user_thread_placement returned %d",
3643 unformat_free (line_input);
3650 * This command is used to move the RX processing for the given
3651 * interfaces to the provided thread. If the '<em>del</em>' option is used,
3652 * the forced thread assignment is removed and the thread assigment is
3653 * reassigned automatically. Use '<em>show vhost-user <interface></em>'
3654 * to see the thread assignment.
3657 * Example of how to move the RX processing for a given interface to a given thread:
3658 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1}
3659 * Example of how to remove the forced thread assignment for a given interface:
3660 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1 del}
3663 VLIB_CLI_COMMAND (vhost_user_thread_command, static) = {
3664 .path = "vhost thread",
3665 .short_help = "vhost thread <iface> <worker-index> [del]",
3666 .function = vhost_thread_command_fn,
3671 * fd.io coding-style-patch-verification: ON
3674 * eval: (c-set-style "gnu")