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
334 u32 thread_index = 0;
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[thread_index] = qid;
346 if (thread_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;
351 if (thread_index == 0)
353 //Could not find a single valid one
354 for (thread_index = 0;
355 thread_index < vlib_get_thread_main ()->n_vlib_mains;
358 vui->per_cpu_tx_qid[thread_index] = 0;
366 vhost_user_rx_thread_placement ()
368 vhost_user_main_t *vum = &vhost_user_main;
369 vhost_user_intf_t *vui;
375 //Let's list all workers cpu indexes
377 for (i = vum->input_cpu_first_index;
378 i < vum->input_cpu_first_index + vum->input_cpu_count; i++)
380 vlib_node_set_state (vlib_mains[i], vhost_user_input_node.index,
381 VLIB_NODE_STATE_DISABLED);
382 vec_add1 (workers, i);
385 vec_foreach (vhc, vum->cpus)
387 vec_reset_length (vhc->rx_queues);
391 vhost_iface_and_queue_t iaq;
393 pool_foreach (vui, vum->vhost_user_interfaces, {
394 u32 *vui_workers = vec_len (vui->workers) ? vui->workers : workers;
396 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
398 vhost_user_vring_t *txvq =
399 &vui->vrings[VHOST_VRING_IDX_TX (qid)];
403 i %= vec_len (vui_workers);
404 thread_index = vui_workers[i];
406 vhc = &vum->cpus[thread_index];
407 txvq->interrupt_thread_index = thread_index;
410 iaq.vhost_iface_index = vui - vum->vhost_user_interfaces;
411 vec_add1 (vhc->rx_queues, iaq);
416 vec_foreach (vhc, vum->cpus)
418 vhost_iface_and_queue_t *vhiq;
419 u8 mode = VHOST_USER_INTERRUPT_MODE;
421 vec_foreach (vhiq, vhc->rx_queues)
423 vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
424 if (vui->operation_mode == VHOST_USER_POLLING_MODE)
426 /* At least one interface is polling, cpu is set to polling */
427 mode = VHOST_USER_POLLING_MODE;
431 vhc->operation_mode = mode;
434 for (thread_index = vum->input_cpu_first_index;
435 thread_index < vum->input_cpu_first_index + vum->input_cpu_count;
438 vlib_node_state_t state = VLIB_NODE_STATE_POLLING;
440 vhc = &vum->cpus[thread_index];
441 vm = vlib_mains ? vlib_mains[thread_index] : &vlib_global_main;
442 switch (vhc->operation_mode)
444 case VHOST_USER_INTERRUPT_MODE:
445 state = VLIB_NODE_STATE_INTERRUPT;
447 case VHOST_USER_POLLING_MODE:
448 state = VLIB_NODE_STATE_POLLING;
451 clib_warning ("BUG: bad operation mode %d", vhc->operation_mode);
454 vlib_node_set_state (vm, vhost_user_input_node.index, state);
461 vhost_user_thread_placement (u32 sw_if_index, u32 worker_thread_index, u8 del)
463 vhost_user_main_t *vum = &vhost_user_main;
464 vhost_user_intf_t *vui;
465 vnet_hw_interface_t *hw;
467 if (worker_thread_index < vum->input_cpu_first_index ||
468 worker_thread_index >=
469 vum->input_cpu_first_index + vum->input_cpu_count)
472 if (!(hw = vnet_get_sup_hw_interface (vnet_get_main (), sw_if_index)))
475 vui = pool_elt_at_index (vum->vhost_user_interfaces, hw->dev_instance);
477 vec_foreach (w, vui->workers)
479 if (*w == worker_thread_index)
481 found = w - vui->workers;
490 vec_del1 (vui->workers, found);
492 else if (found == ~0)
494 vec_add1 (vui->workers, worker_thread_index);
497 vhost_user_rx_thread_placement ();
501 /** @brief Returns whether at least one TX and one RX vring are enabled */
503 vhost_user_intf_ready (vhost_user_intf_t * vui)
505 int i, found[2] = { }; //RX + TX
507 for (i = 0; i < VHOST_VRING_MAX_N; i++)
508 if (vui->vrings[i].started && vui->vrings[i].enabled)
511 return found[0] && found[1];
515 vhost_user_update_iface_state (vhost_user_intf_t * vui)
517 /* if we have pointers to descriptor table, go up */
518 int is_up = vhost_user_intf_ready (vui);
519 if (is_up != vui->is_up)
521 DBG_SOCK ("interface %d %s", vui->sw_if_index,
522 is_up ? "ready" : "down");
523 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
524 is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP :
528 vhost_user_rx_thread_placement ();
529 vhost_user_tx_thread_placement (vui);
533 vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
535 vhost_user_main_t *vum = &vhost_user_main;
540 vhost_user_vring_t *txvq;
544 /* Only care about the odd number virtqueue which is TX */
547 if (vhost_user_intf_ready (vui))
549 txvq = &vui->vrings[qid];
550 thread_index = txvq->interrupt_thread_index;
551 vhc = &vum->cpus[thread_index];
552 if (vhc->operation_mode == VHOST_USER_INTERRUPT_MODE)
554 vm = vlib_mains ? vlib_mains[thread_index] : &vlib_global_main;
556 * Convert virtqueue number in the lower byte to vring
557 * queue index for the input node process. Top bytes contain
558 * the interface, lower byte contains the queue index.
560 ifq2 = ((ifq >> 8) << 8) | qid / 2;
561 vhc->pending_input_bitmap =
562 clib_bitmap_set (vhc->pending_input_bitmap, ifq2, 1);
563 vlib_node_set_interrupt_pending (vm, vhost_user_input_node.index);
568 static clib_error_t *
569 vhost_user_callfd_read_ready (unix_file_t * uf)
571 __attribute__ ((unused)) int n;
573 vhost_user_intf_t *vui =
574 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
575 uf->private_data >> 8);
577 n = read (uf->file_descriptor, ((char *) &buff), 8);
578 DBG_SOCK ("if %d CALL queue %d", uf->private_data >> 8,
579 uf->private_data & 0xff);
580 vhost_user_set_interrupt_pending (vui, uf->private_data);
585 static clib_error_t *
586 vhost_user_kickfd_read_ready (unix_file_t * uf)
588 __attribute__ ((unused)) int n;
590 vhost_user_intf_t *vui =
591 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
592 uf->private_data >> 8);
593 u32 qid = uf->private_data & 0xff;
595 n = read (uf->file_descriptor, ((char *) &buff), 8);
596 DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
597 if (!vui->vrings[qid].started ||
598 (vhost_user_intf_ready (vui) != vui->is_up))
600 vlib_worker_thread_barrier_sync (vlib_get_main ());
601 vui->vrings[qid].started = 1;
602 vhost_user_update_iface_state (vui);
603 vlib_worker_thread_barrier_release (vlib_get_main ());
606 vhost_user_set_interrupt_pending (vui, uf->private_data);
611 * @brief Try once to lock the vring
612 * @return 0 on success, non-zero on failure.
615 vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
617 return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
621 * @brief Spin until the vring is successfully locked
624 vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
626 while (vhost_user_vring_try_lock (vui, qid))
631 * @brief Unlock the vring lock
634 vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
636 *vui->vring_locks[qid] = 0;
640 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
642 vhost_user_vring_t *vring = &vui->vrings[qid];
643 memset (vring, 0, sizeof (*vring));
644 vring->kickfd_idx = ~0;
645 vring->callfd_idx = ~0;
649 * We have a bug with some qemu 2.5, and this may be a fix.
650 * Feel like interpretation holy text, but this is from vhost-user.txt.
652 * One queue pair is enabled initially. More queues are enabled
653 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
655 * Don't know who's right, but this is what DPDK does.
657 if (qid == 0 || qid == 1)
662 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
664 vhost_user_vring_t *vring = &vui->vrings[qid];
665 if (vring->kickfd_idx != ~0)
667 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
669 unix_file_del (&unix_main, uf);
670 vring->kickfd_idx = ~0;
672 if (vring->callfd_idx != ~0)
674 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
676 unix_file_del (&unix_main, uf);
677 vring->callfd_idx = ~0;
679 if (vring->errfd != -1)
681 close (vring->errfd);
684 vhost_user_vring_init (vui, qid);
688 vhost_user_if_disconnect (vhost_user_intf_t * vui)
690 vnet_main_t *vnm = vnet_get_main ();
693 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
695 if (vui->unix_file_index != ~0)
697 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
698 vui->unix_file_index = ~0;
703 for (q = 0; q < VHOST_VRING_MAX_N; q++)
704 vhost_user_vring_close (vui, q);
706 unmap_all_mem_regions (vui);
707 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
710 #define VHOST_LOG_PAGE 0x1000
711 static_always_inline void
712 vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
713 u64 addr, u64 len, u8 is_host_address)
715 if (PREDICT_TRUE (vui->log_base_addr == 0
716 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
722 addr = (u64) map_user_mem (vui, (uword) addr);
724 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
726 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
730 CLIB_MEMORY_BARRIER ();
731 u64 page = addr / VHOST_LOG_PAGE;
732 while (page * VHOST_LOG_PAGE < addr + len)
734 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
739 static_always_inline void
740 vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
742 vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
745 #define vhost_user_log_dirty_ring(vui, vq, member) \
746 if (PREDICT_FALSE(vq->log_used)) { \
747 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
748 sizeof(vq->used->member)); \
751 static clib_error_t *
752 vhost_user_socket_read (unix_file_t * uf)
755 int fd, number_of_fds = 0;
756 int fds[VHOST_MEMORY_MAX_NREGIONS];
757 vhost_user_msg_t msg;
760 vhost_user_main_t *vum = &vhost_user_main;
761 vhost_user_intf_t *vui;
762 struct cmsghdr *cmsg;
764 unix_file_t template = { 0 };
765 vnet_main_t *vnm = vnet_get_main ();
767 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
769 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
771 memset (&mh, 0, sizeof (mh));
772 memset (control, 0, sizeof (control));
774 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
777 /* set the payload */
778 iov[0].iov_base = (void *) &msg;
779 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
783 mh.msg_control = control;
784 mh.msg_controllen = sizeof (control);
786 n = recvmsg (uf->file_descriptor, &mh, 0);
788 /* Stop workers to avoid end of the world */
789 vlib_worker_thread_barrier_sync (vlib_get_main ());
791 if (n != VHOST_USER_MSG_HDR_SZ)
795 DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
799 DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
800 n, VHOST_USER_MSG_HDR_SZ);
805 if (mh.msg_flags & MSG_CTRUNC)
807 DBG_SOCK ("MSG_CTRUNC is set");
811 cmsg = CMSG_FIRSTHDR (&mh);
813 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
814 (cmsg->cmsg_type == SCM_RIGHTS) &&
815 (cmsg->cmsg_len - CMSG_LEN (0) <=
816 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
818 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
819 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
822 /* version 1, no reply bit set */
823 if ((msg.flags & 7) != 1)
825 DBG_SOCK ("malformed message received. closing socket");
832 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
836 DBG_SOCK ("read failed %s", strerror (errno));
839 else if (rv != msg.size)
841 DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
848 case VHOST_USER_GET_FEATURES:
850 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
851 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
852 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
853 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
854 (1ULL << FEAT_VHOST_F_LOG_ALL) |
855 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
856 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
857 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
858 (1ULL << FEAT_VIRTIO_F_VERSION_1);
859 msg.u64 &= vui->feature_mask;
860 msg.size = sizeof (msg.u64);
861 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
862 vui->hw_if_index, msg.u64);
865 case VHOST_USER_SET_FEATURES:
866 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
867 vui->hw_if_index, msg.u64);
869 vui->features = msg.u64;
872 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
873 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
874 vui->virtio_net_hdr_sz = 12;
876 vui->virtio_net_hdr_sz = 10;
879 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
881 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
882 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
885 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
886 vhost_user_vring_close(&vui->vrings[q]); */
890 case VHOST_USER_SET_MEM_TABLE:
891 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
892 vui->hw_if_index, msg.memory.nregions);
894 if ((msg.memory.nregions < 1) ||
895 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
898 DBG_SOCK ("number of mem regions must be between 1 and %i",
899 VHOST_MEMORY_MAX_NREGIONS);
904 if (msg.memory.nregions != number_of_fds)
906 DBG_SOCK ("each memory region must have FD");
909 unmap_all_mem_regions (vui);
910 for (i = 0; i < msg.memory.nregions; i++)
912 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
913 sizeof (vhost_user_memory_region_t));
915 long page_sz = get_huge_page_size (fds[i]);
917 /* align size to 2M page */
918 ssize_t map_sz = (vui->regions[i].memory_size +
919 vui->regions[i].mmap_offset +
920 page_sz) & ~(page_sz - 1);
922 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
923 MAP_SHARED, fds[i], 0);
924 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
925 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
926 vui->regions[i].memory_size;
929 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
930 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
933 if (vui->region_mmap_addr[i] == MAP_FAILED)
935 clib_warning ("failed to map memory. errno is %d", errno);
938 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
939 vui->region_mmap_fd[i] = fds[i];
941 vui->nregions = msg.memory.nregions;
944 case VHOST_USER_SET_VRING_NUM:
945 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
946 vui->hw_if_index, msg.state.index, msg.state.num);
948 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
949 (msg.state.num == 0) || /* it cannot be zero */
950 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
952 vui->vrings[msg.state.index].qsz = msg.state.num;
955 case VHOST_USER_SET_VRING_ADDR:
956 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
957 vui->hw_if_index, msg.state.index);
959 if (msg.state.index >= VHOST_VRING_MAX_N)
961 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
962 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
966 if (msg.size < sizeof (msg.addr))
968 DBG_SOCK ("vhost message is too short (%d < %d)",
969 msg.size, sizeof (msg.addr));
973 vui->vrings[msg.state.index].desc = (vring_desc_t *)
974 map_user_mem (vui, msg.addr.desc_user_addr);
975 vui->vrings[msg.state.index].used = (vring_used_t *)
976 map_user_mem (vui, msg.addr.used_user_addr);
977 vui->vrings[msg.state.index].avail = (vring_avail_t *)
978 map_user_mem (vui, msg.addr.avail_user_addr);
980 if ((vui->vrings[msg.state.index].desc == NULL) ||
981 (vui->vrings[msg.state.index].used == NULL) ||
982 (vui->vrings[msg.state.index].avail == NULL))
984 DBG_SOCK ("failed to map user memory for hw_if_index %d",
989 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
990 vui->vrings[msg.state.index].log_used =
991 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
993 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
994 the ring is initialized in an enabled state. */
995 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
997 vui->vrings[msg.state.index].enabled = 1;
1000 vui->vrings[msg.state.index].last_used_idx =
1001 vui->vrings[msg.state.index].last_avail_idx =
1002 vui->vrings[msg.state.index].used->idx;
1004 if (vui->operation_mode == VHOST_USER_POLLING_MODE)
1005 /* tell driver that we don't want interrupts */
1006 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
1008 /* tell driver that we want interrupts */
1009 vui->vrings[msg.state.index].used->flags = 0;
1012 case VHOST_USER_SET_OWNER:
1013 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
1016 case VHOST_USER_RESET_OWNER:
1017 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
1020 case VHOST_USER_SET_VRING_CALL:
1021 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
1022 vui->hw_if_index, msg.u64);
1024 q = (u8) (msg.u64 & 0xFF);
1026 /* if there is old fd, delete and close it */
1027 if (vui->vrings[q].callfd_idx != ~0)
1029 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
1030 vui->vrings[q].callfd_idx);
1031 unix_file_del (&unix_main, uf);
1032 vui->vrings[q].callfd_idx = ~0;
1035 if (!(msg.u64 & 0x100))
1037 if (number_of_fds != 1)
1039 DBG_SOCK ("More than one fd received !");
1043 template.read_function = vhost_user_callfd_read_ready;
1044 template.file_descriptor = fds[0];
1045 template.private_data =
1046 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
1047 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
1050 vui->vrings[q].callfd_idx = ~0;
1053 case VHOST_USER_SET_VRING_KICK:
1054 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
1055 vui->hw_if_index, msg.u64);
1057 q = (u8) (msg.u64 & 0xFF);
1059 if (vui->vrings[q].kickfd_idx != ~0)
1061 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
1062 vui->vrings[q].kickfd_idx);
1063 unix_file_del (&unix_main, uf);
1064 vui->vrings[q].kickfd_idx = ~0;
1067 if (!(msg.u64 & 0x100))
1069 if (number_of_fds != 1)
1071 DBG_SOCK ("More than one fd received !");
1075 template.read_function = vhost_user_kickfd_read_ready;
1076 template.file_descriptor = fds[0];
1077 template.private_data =
1078 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
1080 vui->vrings[q].kickfd_idx = unix_file_add (&unix_main, &template);
1084 //When no kickfd is set, the queue is initialized as started
1085 vui->vrings[q].kickfd_idx = ~0;
1086 vui->vrings[q].started = 1;
1091 case VHOST_USER_SET_VRING_ERR:
1092 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
1093 vui->hw_if_index, msg.u64);
1095 q = (u8) (msg.u64 & 0xFF);
1097 if (vui->vrings[q].errfd != -1)
1098 close (vui->vrings[q].errfd);
1100 if (!(msg.u64 & 0x100))
1102 if (number_of_fds != 1)
1105 vui->vrings[q].errfd = fds[0];
1108 vui->vrings[q].errfd = -1;
1112 case VHOST_USER_SET_VRING_BASE:
1113 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1114 vui->hw_if_index, msg.state.index, msg.state.num);
1116 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
1119 case VHOST_USER_GET_VRING_BASE:
1120 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1121 vui->hw_if_index, msg.state.index, msg.state.num);
1123 if (msg.state.index >= VHOST_VRING_MAX_N)
1125 DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
1126 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1131 * Copy last_avail_idx from the vring before closing it because
1132 * closing the vring also initializes the vring last_avail_idx
1134 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
1136 msg.size = sizeof (msg.state);
1138 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
1139 vhost_user_vring_close (vui, msg.state.index);
1142 case VHOST_USER_NONE:
1143 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
1147 case VHOST_USER_SET_LOG_BASE:
1149 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
1151 if (msg.size != sizeof (msg.log))
1154 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
1155 msg.size, sizeof (msg.log));
1160 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
1163 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1168 /* align size to 2M page */
1169 long page_sz = get_huge_page_size (fd);
1171 (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
1173 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1177 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
1178 map_sz, msg.log.offset, fd, vui->log_base_addr);
1180 if (vui->log_base_addr == MAP_FAILED)
1182 clib_warning ("failed to map memory. errno is %d", errno);
1186 vui->log_base_addr += msg.log.offset;
1187 vui->log_size = msg.log.size;
1190 msg.size = sizeof (msg.u64);
1195 case VHOST_USER_SET_LOG_FD:
1196 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1200 case VHOST_USER_GET_PROTOCOL_FEATURES:
1201 DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1205 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1206 (1 << VHOST_USER_PROTOCOL_F_MQ);
1207 msg.size = sizeof (msg.u64);
1210 case VHOST_USER_SET_PROTOCOL_FEATURES:
1211 DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
1212 vui->hw_if_index, msg.u64);
1214 vui->protocol_features = msg.u64;
1218 case VHOST_USER_GET_QUEUE_NUM:
1219 DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM", vui->hw_if_index);
1221 msg.u64 = VHOST_VRING_MAX_N;
1222 msg.size = sizeof (msg.u64);
1225 case VHOST_USER_SET_VRING_ENABLE:
1226 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1227 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1229 if (msg.state.index >= VHOST_VRING_MAX_N)
1231 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
1232 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1236 vui->vrings[msg.state.index].enabled = msg.state.num;
1240 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1245 /* if we need to reply */
1249 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1250 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1252 DBG_SOCK ("could not send message response");
1257 vhost_user_update_iface_state (vui);
1258 vlib_worker_thread_barrier_release (vlib_get_main ());
1262 vhost_user_if_disconnect (vui);
1263 vhost_user_update_iface_state (vui);
1264 vlib_worker_thread_barrier_release (vlib_get_main ());
1268 static clib_error_t *
1269 vhost_user_socket_error (unix_file_t * uf)
1271 vlib_main_t *vm = vlib_get_main ();
1272 vhost_user_main_t *vum = &vhost_user_main;
1273 vhost_user_intf_t *vui =
1274 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1276 DBG_SOCK ("socket error on if %d", vui->sw_if_index);
1277 vlib_worker_thread_barrier_sync (vm);
1278 vhost_user_if_disconnect (vui);
1279 vhost_user_rx_thread_placement ();
1280 vlib_worker_thread_barrier_release (vm);
1284 static clib_error_t *
1285 vhost_user_socksvr_accept_ready (unix_file_t * uf)
1287 int client_fd, client_len;
1288 struct sockaddr_un client;
1289 unix_file_t template = { 0 };
1290 vhost_user_main_t *vum = &vhost_user_main;
1291 vhost_user_intf_t *vui;
1293 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1295 client_len = sizeof (client);
1296 client_fd = accept (uf->file_descriptor,
1297 (struct sockaddr *) &client,
1298 (socklen_t *) & client_len);
1301 return clib_error_return_unix (0, "accept");
1303 DBG_SOCK ("New client socket for vhost interface %d", vui->sw_if_index);
1304 template.read_function = vhost_user_socket_read;
1305 template.error_function = vhost_user_socket_error;
1306 template.file_descriptor = client_fd;
1307 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1308 vui->unix_file_index = unix_file_add (&unix_main, &template);
1312 static clib_error_t *
1313 vhost_user_init (vlib_main_t * vm)
1315 clib_error_t *error;
1316 vhost_user_main_t *vum = &vhost_user_main;
1317 vlib_thread_main_t *tm = vlib_get_thread_main ();
1318 vlib_thread_registration_t *tr;
1321 error = vlib_call_init_function (vm, ip4_init);
1325 vum->coalesce_frames = 32;
1326 vum->coalesce_time = 1e-3;
1328 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1331 vec_foreach (cpu, vum->cpus)
1333 /* This is actually not necessary as validate already zeroes it
1334 * Just keeping the loop here for later because I am lazy. */
1335 cpu->rx_buffers_len = 0;
1338 /* find out which cpus will be used for input */
1339 vum->input_cpu_first_index = 0;
1340 vum->input_cpu_count = 1;
1341 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1342 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
1344 if (tr && tr->count > 0)
1346 vum->input_cpu_first_index = tr->first_index;
1347 vum->input_cpu_count = tr->count;
1350 vum->random = random_default_seed ();
1355 VLIB_INIT_FUNCTION (vhost_user_init);
1357 static clib_error_t *
1358 vhost_user_exit (vlib_main_t * vm)
1364 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1367 format_vhost_trace (u8 * s, va_list * va)
1369 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1370 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
1371 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
1372 vhost_user_main_t *vum = &vhost_user_main;
1373 vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
1374 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
1377 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
1379 uword indent = format_get_indent (s);
1381 s = format (s, "%U %U queue %d\n", format_white_space, indent,
1382 format_vnet_sw_interface_name, vnm, sw, t->qid);
1384 s = format (s, "%U virtio flags:\n", format_white_space, indent);
1386 if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
1387 s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
1388 foreach_virtio_trace_flags
1390 s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
1391 format_white_space, indent, t->first_desc_len);
1393 s = format (s, "%U flags 0x%02x gso_type %u\n",
1394 format_white_space, indent,
1395 t->hdr.hdr.flags, t->hdr.hdr.gso_type);
1397 if (vui->virtio_net_hdr_sz == 12)
1398 s = format (s, "%U num_buff %u",
1399 format_white_space, indent, t->hdr.num_buffers);
1405 vhost_user_rx_trace (vhost_trace_t * t,
1406 vhost_user_intf_t * vui, u16 qid,
1407 vlib_buffer_t * b, vhost_user_vring_t * txvq)
1409 vhost_user_main_t *vum = &vhost_user_main;
1410 u32 qsz_mask = txvq->qsz - 1;
1411 u32 last_avail_idx = txvq->last_avail_idx;
1412 u32 desc_current = txvq->avail->ring[last_avail_idx & qsz_mask];
1413 vring_desc_t *hdr_desc = 0;
1414 virtio_net_hdr_mrg_rxbuf_t *hdr;
1417 memset (t, 0, sizeof (*t));
1418 t->device_index = vui - vum->vhost_user_interfaces;
1421 hdr_desc = &txvq->desc[desc_current];
1422 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1424 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1425 /* Header is the first here */
1426 hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
1428 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1430 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1432 if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1433 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1435 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1438 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1440 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
1442 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
1446 u32 len = vui->virtio_net_hdr_sz;
1447 memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
1452 vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
1454 vhost_user_main_t *vum = &vhost_user_main;
1456 int fd = UNIX_GET_FD (vq->callfd_idx);
1457 int rv __attribute__ ((unused));
1458 /* TODO: pay attention to rv */
1459 rv = write (fd, &x, sizeof (x));
1460 vq->n_since_last_int = 0;
1461 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
1464 static_always_inline u32
1465 vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1466 u16 copy_len, u32 * map_hint)
1468 void *src0, *src1, *src2, *src3;
1469 if (PREDICT_TRUE (copy_len >= 4))
1471 if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
1473 if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
1476 while (PREDICT_TRUE (copy_len >= 4))
1482 (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
1485 (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
1488 CLIB_PREFETCH (src2, 64, LOAD);
1489 CLIB_PREFETCH (src3, 64, LOAD);
1491 clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
1492 clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
1499 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
1501 clib_memcpy ((void *) cpy->dst, src0, cpy->len);
1509 * Try to discard packets from the tx ring (VPP RX path).
1510 * Returns the number of discarded packets.
1513 vhost_user_rx_discard_packet (vlib_main_t * vm,
1514 vhost_user_intf_t * vui,
1515 vhost_user_vring_t * txvq, u32 discard_max)
1518 * On the RX side, each packet corresponds to one descriptor
1519 * (it is the same whether it is a shallow descriptor, chained, or indirect).
1520 * Therefore, discarding a packet is like discarding a descriptor.
1522 u32 discarded_packets = 0;
1523 u32 avail_idx = txvq->avail->idx;
1524 u16 qsz_mask = txvq->qsz - 1;
1525 while (discarded_packets != discard_max)
1527 if (avail_idx == txvq->last_avail_idx)
1530 u16 desc_chain_head =
1531 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1532 txvq->last_avail_idx++;
1533 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
1534 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1535 vhost_user_log_dirty_ring (vui, txvq,
1536 ring[txvq->last_used_idx & qsz_mask]);
1537 txvq->last_used_idx++;
1538 discarded_packets++;
1542 CLIB_MEMORY_BARRIER ();
1543 txvq->used->idx = txvq->last_used_idx;
1544 vhost_user_log_dirty_ring (vui, txvq, idx);
1545 return discarded_packets;
1549 * In case of overflow, we need to rewind the array of allocated buffers.
1552 vhost_user_input_rewind_buffers (vlib_main_t * vm,
1553 vhost_cpu_t * cpu, vlib_buffer_t * b_head)
1555 u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1556 vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
1557 b_current->current_length = 0;
1558 b_current->flags = 0;
1559 while (b_current != b_head)
1561 cpu->rx_buffers_len++;
1562 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1563 b_current = vlib_get_buffer (vm, bi_current);
1564 b_current->current_length = 0;
1565 b_current->flags = 0;
1570 vhost_user_if_input (vlib_main_t * vm,
1571 vhost_user_main_t * vum,
1572 vhost_user_intf_t * vui,
1573 u16 qid, vlib_node_runtime_t * node)
1575 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1576 u16 n_rx_packets = 0;
1579 u32 n_left_to_next, *to_next;
1580 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1581 u32 n_trace = vlib_get_trace_count (vm, node);
1584 u16 thread_index = vlib_get_thread_index ();
1588 /* do we have pending interrupts ? */
1589 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1590 f64 now = vlib_time_now (vm);
1592 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1593 vhost_user_send_call (vm, txvq);
1595 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1596 vhost_user_send_call (vm, rxvq);
1599 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
1602 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1605 if (PREDICT_FALSE (n_left == 0))
1608 if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
1611 * Discard input packet if interface is admin down or vring is not
1613 * "For example, for a networking device, in the disabled state
1614 * client must not supply any new RX packets, but must process
1615 * and discard any TX packets."
1617 vhost_user_rx_discard_packet (vm, vui, txvq,
1618 VHOST_USER_DOWN_DISCARD_COUNT);
1622 if (PREDICT_FALSE (n_left == txvq->qsz))
1625 * Informational error logging when VPP is not
1626 * receiving packets fast enough.
1628 vlib_error_count (vm, node->node_index,
1629 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1632 qsz_mask = txvq->qsz - 1;
1634 if (n_left > VLIB_FRAME_SIZE)
1635 n_left = VLIB_FRAME_SIZE;
1638 * For small packets (<2kB), we will not need more than one vlib buffer
1639 * per packet. In case packets are bigger, we will just yeld at some point
1640 * in the loop and come back later. This is not an issue as for big packet,
1641 * processing cost really comes from the memory copy.
1643 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len < n_left + 1))
1645 u32 curr_len = vum->cpus[thread_index].rx_buffers_len;
1646 vum->cpus[thread_index].rx_buffers_len +=
1647 vlib_buffer_alloc_from_free_list (vm,
1648 vum->cpus[thread_index].rx_buffers +
1650 VHOST_USER_RX_BUFFERS_N - curr_len,
1651 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1654 (vum->cpus[thread_index].rx_buffers_len <
1655 VHOST_USER_RX_BUFFER_STARVATION))
1657 /* In case of buffer starvation, discard some packets from the queue
1658 * and log the event.
1659 * We keep doing best effort for the remaining packets. */
1660 u32 flush = (n_left + 1 > vum->cpus[thread_index].rx_buffers_len) ?
1661 n_left + 1 - vum->cpus[thread_index].rx_buffers_len : 1;
1662 flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1665 vlib_increment_simple_counter (vnet_main.
1666 interface_main.sw_if_counters +
1667 VNET_INTERFACE_COUNTER_DROP,
1668 vlib_get_thread_index (),
1669 vui->sw_if_index, flush);
1671 vlib_error_count (vm, vhost_user_input_node.index,
1672 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1678 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1680 while (n_left > 0 && n_left_to_next > 0)
1682 vlib_buffer_t *b_head, *b_current;
1685 u32 desc_data_offset;
1686 vring_desc_t *desc_table = txvq->desc;
1688 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len <= 1))
1690 /* Not enough rx_buffers
1691 * Note: We yeld on 1 so we don't need to do an additional
1692 * check for the next buffer prefetch.
1698 desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1699 vum->cpus[thread_index].rx_buffers_len--;
1700 bi_current = (vum->cpus[thread_index].rx_buffers)
1701 [vum->cpus[thread_index].rx_buffers_len];
1702 b_head = b_current = vlib_get_buffer (vm, bi_current);
1703 to_next[0] = bi_current; //We do that now so we can forget about bi_current
1707 vlib_prefetch_buffer_with_index (vm,
1709 cpus[thread_index].rx_buffers)
1710 [vum->cpus[thread_index].
1711 rx_buffers_len - 1], LOAD);
1713 /* Just preset the used descriptor id and length for later */
1714 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_current;
1715 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1716 vhost_user_log_dirty_ring (vui, txvq,
1717 ring[txvq->last_used_idx & qsz_mask]);
1719 /* The buffer should already be initialized */
1720 b_head->total_length_not_including_first_buffer = 0;
1721 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1723 if (PREDICT_FALSE (n_trace))
1725 //TODO: next_index is not exactly known at that point
1726 vlib_trace_buffer (vm, node, next_index, b_head,
1727 /* follow_chain */ 0);
1729 vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1730 vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1732 vlib_set_trace_count (vm, node, n_trace);
1735 /* This depends on the setup but is very consistent
1736 * So I think the CPU branch predictor will make a pretty good job
1737 * at optimizing the decision. */
1738 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1740 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1743 if (PREDICT_FALSE (desc_table == 0))
1745 //FIXME: Handle error by shutdown the queue
1750 if (PREDICT_TRUE (vui->is_any_layout) ||
1751 (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
1753 /* ANYLAYOUT or single buffer */
1754 desc_data_offset = vui->virtio_net_hdr_sz;
1758 /* CSR case without ANYLAYOUT, skip 1st buffer */
1759 desc_data_offset = desc_table[desc_current].len;
1764 /* Get more input if necessary. Or end of packet. */
1765 if (desc_data_offset == desc_table[desc_current].len)
1767 if (PREDICT_FALSE (desc_table[desc_current].flags &
1770 desc_current = desc_table[desc_current].next;
1771 desc_data_offset = 0;
1779 /* Get more output if necessary. Or end of packet. */
1781 (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1784 (vum->cpus[thread_index].rx_buffers_len == 0))
1786 /* Cancel speculation */
1791 * Checking if there are some left buffers.
1792 * If not, just rewind the used buffers and stop.
1793 * Note: Scheduled copies are not cancelled. This is
1794 * not an issue as they would still be valid. Useless,
1797 vhost_user_input_rewind_buffers (vm,
1805 /* Get next output */
1806 vum->cpus[thread_index].rx_buffers_len--;
1808 (vum->cpus[thread_index].rx_buffers)[vum->cpus
1809 [thread_index].rx_buffers_len];
1810 b_current->next_buffer = bi_next;
1811 b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1812 bi_current = bi_next;
1813 b_current = vlib_get_buffer (vm, bi_current);
1816 /* Prepare a copy order executed later for the data */
1817 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
1820 desc_table[desc_current].len - desc_data_offset;
1821 cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1822 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1823 cpy->dst = (uword) vlib_buffer_get_current (b_current);
1824 cpy->src = desc_table[desc_current].addr + desc_data_offset;
1826 desc_data_offset += cpy->len;
1828 b_current->current_length += cpy->len;
1829 b_head->total_length_not_including_first_buffer += cpy->len;
1833 CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1835 n_rx_bytes += b_head->total_length_not_including_first_buffer;
1838 b_head->total_length_not_including_first_buffer -=
1839 b_head->current_length;
1841 /* consume the descriptor and return it as used */
1842 txvq->last_avail_idx++;
1843 txvq->last_used_idx++;
1845 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1847 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1848 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1852 u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1854 /* redirect if feature path enabled */
1855 vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
1858 u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1859 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1860 to_next, n_left_to_next,
1867 * Although separating memory copies from virtio ring parsing
1868 * is beneficial, we can offer to perform the copies from time
1869 * to time in order to free some space in the ring.
1871 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1874 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
1875 copy_len, &map_hint)))
1878 ("Memory mapping error on interface hw_if_index=%d "
1879 "(Shutting down - Switch interface down and up to restart)",
1887 /* give buffers back to driver */
1888 CLIB_MEMORY_BARRIER ();
1889 txvq->used->idx = txvq->last_used_idx;
1890 vhost_user_log_dirty_ring (vui, txvq, idx);
1894 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1897 /* Do the memory copies */
1899 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
1900 copy_len, &map_hint)))
1902 clib_warning ("Memory mapping error on interface hw_if_index=%d "
1903 "(Shutting down - Switch interface down and up to restart)",
1908 /* give buffers back to driver */
1909 CLIB_MEMORY_BARRIER ();
1910 txvq->used->idx = txvq->last_used_idx;
1911 vhost_user_log_dirty_ring (vui, txvq, idx);
1913 /* interrupt (call) handling */
1914 if ((txvq->callfd_idx != ~0) &&
1915 !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
1917 txvq->n_since_last_int += n_rx_packets;
1919 if (txvq->n_since_last_int > vum->coalesce_frames)
1920 vhost_user_send_call (vm, txvq);
1923 /* increase rx counters */
1924 vlib_increment_combined_counter
1925 (vnet_main.interface_main.combined_sw_if_counters
1926 + VNET_INTERFACE_COUNTER_RX,
1927 vlib_get_thread_index (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1929 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
1931 return n_rx_packets;
1935 vhost_user_input (vlib_main_t * vm,
1936 vlib_node_runtime_t * node, vlib_frame_t * f)
1938 vhost_user_main_t *vum = &vhost_user_main;
1939 uword n_rx_packets = 0;
1940 u32 thread_index = vlib_get_thread_index ();
1941 vhost_iface_and_queue_t *vhiq;
1942 vhost_user_intf_t *vui;
1945 vhc = &vum->cpus[thread_index];
1946 if (PREDICT_TRUE (vhc->operation_mode == VHOST_USER_POLLING_MODE))
1948 vec_foreach (vhiq, vum->cpus[thread_index].rx_queues)
1950 vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
1951 n_rx_packets += vhost_user_if_input (vm, vum, vui, vhiq->qid, node);
1959 clib_bitmap_foreach (i, vhc->pending_input_bitmap, ({
1962 clib_bitmap_set (vhc->pending_input_bitmap, i, 0);
1963 vui = pool_elt_at_index (vum->vhost_user_interfaces, i >> 8);
1964 n_rx_packets += vhost_user_if_input (vm, vum, vui, qid, node);
1968 return n_rx_packets;
1972 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1973 .function = vhost_user_input,
1974 .type = VLIB_NODE_TYPE_INPUT,
1975 .name = "vhost-user-input",
1976 .sibling_of = "device-input",
1978 /* Will be enabled if/when hardware is detected. */
1979 .state = VLIB_NODE_STATE_DISABLED,
1981 .format_buffer = format_ethernet_header_with_length,
1982 .format_trace = format_vhost_trace,
1984 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1985 .error_strings = vhost_user_input_func_error_strings,
1988 VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
1993 vhost_user_tx_trace (vhost_trace_t * t,
1994 vhost_user_intf_t * vui, u16 qid,
1995 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
1997 vhost_user_main_t *vum = &vhost_user_main;
1998 u32 qsz_mask = rxvq->qsz - 1;
1999 u32 last_avail_idx = rxvq->last_avail_idx;
2000 u32 desc_current = rxvq->avail->ring[last_avail_idx & qsz_mask];
2001 vring_desc_t *hdr_desc = 0;
2004 memset (t, 0, sizeof (*t));
2005 t->device_index = vui - vum->vhost_user_interfaces;
2008 hdr_desc = &rxvq->desc[desc_current];
2009 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
2011 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
2012 /* Header is the first here */
2013 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
2015 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
2017 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
2019 if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
2020 !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
2022 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
2025 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
2028 static_always_inline u32
2029 vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
2030 u16 copy_len, u32 * map_hint)
2032 void *dst0, *dst1, *dst2, *dst3;
2033 if (PREDICT_TRUE (copy_len >= 4))
2035 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
2037 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
2039 while (PREDICT_TRUE (copy_len >= 4))
2045 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
2048 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
2051 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
2052 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
2054 clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
2055 clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
2057 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
2058 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
2065 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
2067 clib_memcpy (dst0, (void *) cpy->src, cpy->len);
2068 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
2077 vhost_user_tx (vlib_main_t * vm,
2078 vlib_node_runtime_t * node, vlib_frame_t * frame)
2080 u32 *buffers = vlib_frame_args (frame);
2081 u32 n_left = frame->n_vectors;
2082 vhost_user_main_t *vum = &vhost_user_main;
2083 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
2084 vhost_user_intf_t *vui =
2085 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
2087 vhost_user_vring_t *rxvq;
2090 u32 thread_index = vlib_get_thread_index ();
2096 if (PREDICT_FALSE (!vui->admin_up))
2098 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
2102 if (PREDICT_FALSE (!vui->is_up))
2104 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
2109 VHOST_VRING_IDX_RX (*vec_elt_at_index
2110 (vui->per_cpu_tx_qid, vlib_get_thread_index ()));
2111 rxvq = &vui->vrings[qid];
2112 if (PREDICT_FALSE (vui->use_tx_spinlock))
2113 vhost_user_vring_lock (vui, qid);
2115 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
2118 error = VHOST_USER_TX_FUNC_ERROR_NONE;
2123 vlib_buffer_t *b0, *current_b0;
2124 u16 desc_head, desc_index, desc_len;
2125 vring_desc_t *desc_table;
2126 uword buffer_map_addr;
2130 if (PREDICT_TRUE (n_left > 1))
2131 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2133 b0 = vlib_get_buffer (vm, buffers[0]);
2135 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2137 vum->cpus[thread_index].current_trace =
2138 vlib_add_trace (vm, node, b0,
2139 sizeof (*vum->cpus[thread_index].current_trace));
2140 vhost_user_tx_trace (vum->cpus[thread_index].current_trace,
2141 vui, qid / 2, b0, rxvq);
2144 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2146 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2150 desc_table = rxvq->desc;
2151 desc_head = desc_index =
2152 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2154 /* Go deeper in case of indirect descriptor
2155 * I don't know of any driver providing indirect for RX. */
2156 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2159 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2161 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2166 map_guest_mem (vui, rxvq->desc[desc_index].addr,
2169 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2175 desc_len = vui->virtio_net_hdr_sz;
2176 buffer_map_addr = desc_table[desc_index].addr;
2177 buffer_len = desc_table[desc_index].len;
2180 // Get a header from the header array
2181 virtio_net_hdr_mrg_rxbuf_t *hdr =
2182 &vum->cpus[thread_index].tx_headers[tx_headers_len];
2185 hdr->hdr.gso_type = 0;
2186 hdr->num_buffers = 1; //This is local, no need to check
2188 // Prepare a copy order executed later for the header
2189 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
2191 cpy->len = vui->virtio_net_hdr_sz;
2192 cpy->dst = buffer_map_addr;
2193 cpy->src = (uword) hdr;
2196 buffer_map_addr += vui->virtio_net_hdr_sz;
2197 buffer_len -= vui->virtio_net_hdr_sz;
2198 bytes_left = b0->current_length;
2202 if (buffer_len == 0)
2204 if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2206 //Next one is chained
2207 desc_index = desc_table[desc_index].next;
2208 buffer_map_addr = desc_table[desc_index].addr;
2209 buffer_len = desc_table[desc_index].len;
2211 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2213 virtio_net_hdr_mrg_rxbuf_t *hdr =
2214 &vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
2216 //Move from available to used buffer
2217 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id =
2219 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len =
2221 vhost_user_log_dirty_ring (vui, rxvq,
2222 ring[rxvq->last_used_idx &
2225 rxvq->last_avail_idx++;
2226 rxvq->last_used_idx++;
2231 (rxvq->last_avail_idx == rxvq->avail->idx))
2233 //Dequeue queued descriptors for this packet
2234 rxvq->last_used_idx -= hdr->num_buffers - 1;
2235 rxvq->last_avail_idx -= hdr->num_buffers - 1;
2236 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2240 desc_table = rxvq->desc;
2241 desc_head = desc_index =
2242 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2244 (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2246 //It is seriously unlikely that a driver will put indirect descriptor
2247 //after non-indirect descriptor.
2249 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2251 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2257 rxvq->desc[desc_index].addr,
2260 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2265 buffer_map_addr = desc_table[desc_index].addr;
2266 buffer_len = desc_table[desc_index].len;
2270 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2276 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
2278 cpy->len = bytes_left;
2279 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2280 cpy->dst = buffer_map_addr;
2281 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2282 current_b0->current_length - bytes_left;
2284 bytes_left -= cpy->len;
2285 buffer_len -= cpy->len;
2286 buffer_map_addr += cpy->len;
2287 desc_len += cpy->len;
2289 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
2292 // Check if vlib buffer has more data. If not, get more or break.
2293 if (PREDICT_TRUE (!bytes_left))
2296 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2298 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2299 bytes_left = current_b0->current_length;
2309 //Move from available to used ring
2310 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
2311 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
2312 vhost_user_log_dirty_ring (vui, rxvq,
2313 ring[rxvq->last_used_idx & qsz_mask]);
2314 rxvq->last_avail_idx++;
2315 rxvq->last_used_idx++;
2317 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2319 vum->cpus[thread_index].current_trace->hdr =
2320 vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
2323 n_left--; //At the end for error counting when 'goto done' is invoked
2328 //Do the memory copies
2330 (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
2331 copy_len, &map_hint)))
2333 clib_warning ("Memory mapping error on interface hw_if_index=%d "
2334 "(Shutting down - Switch interface down and up to restart)",
2339 CLIB_MEMORY_BARRIER ();
2340 rxvq->used->idx = rxvq->last_used_idx;
2341 vhost_user_log_dirty_ring (vui, rxvq, idx);
2344 * When n_left is set, error is always set to something too.
2345 * In case error is due to lack of remaining buffers, we go back up and
2347 * The idea is that it is better to waste some time on packets
2348 * that have been processed already than dropping them and get
2349 * more fresh packets with a good likelyhood that they will be dropped too.
2350 * This technique also gives more time to VM driver to pick-up packets.
2351 * In case the traffic flows from physical to virtual interfaces, this
2352 * technique will end-up leveraging the physical NIC buffer in order to
2353 * absorb the VM's CPU jitter.
2355 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2361 /* interrupt (call) handling */
2362 if ((rxvq->callfd_idx != ~0) &&
2363 !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
2365 rxvq->n_since_last_int += frame->n_vectors - n_left;
2367 if (rxvq->n_since_last_int > vum->coalesce_frames)
2368 vhost_user_send_call (vm, rxvq);
2371 vhost_user_vring_unlock (vui, qid);
2374 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2376 vlib_error_count (vm, node->node_index, error, n_left);
2377 vlib_increment_simple_counter
2378 (vnet_main.interface_main.sw_if_counters
2379 + VNET_INTERFACE_COUNTER_DROP,
2380 vlib_get_thread_index (), vui->sw_if_index, n_left);
2383 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2384 return frame->n_vectors;
2387 static clib_error_t *
2388 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
2391 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2392 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
2393 vhost_user_main_t *vum = &vhost_user_main;
2394 vhost_user_intf_t *vui =
2395 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
2397 vui->admin_up = is_up;
2400 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
2401 VNET_HW_INTERFACE_FLAG_LINK_UP);
2403 return /* no error */ 0;
2407 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2408 .name = "vhost-user",
2409 .tx_function = vhost_user_tx,
2410 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2411 .tx_function_error_strings = vhost_user_tx_func_error_strings,
2412 .format_device_name = format_vhost_user_interface_name,
2413 .name_renumber = vhost_user_name_renumber,
2414 .admin_up_down_function = vhost_user_interface_admin_up_down,
2415 .format_tx_trace = format_vhost_trace,
2418 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
2423 vhost_user_process (vlib_main_t * vm,
2424 vlib_node_runtime_t * rt, vlib_frame_t * f)
2426 vhost_user_main_t *vum = &vhost_user_main;
2427 vhost_user_intf_t *vui;
2428 struct sockaddr_un sun;
2430 unix_file_t template = { 0 };
2431 f64 timeout = 3153600000.0 /* 100 years */ ;
2432 uword *event_data = 0;
2435 sun.sun_family = AF_UNIX;
2436 template.read_function = vhost_user_socket_read;
2437 template.error_function = vhost_user_socket_error;
2441 vlib_process_wait_for_event_or_clock (vm, timeout);
2442 vlib_process_get_events (vm, &event_data);
2443 vec_reset_length (event_data);
2448 pool_foreach (vui, vum->vhost_user_interfaces, {
2450 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
2451 if (vui->unix_file_index == ~0)
2454 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
2457 * 1st time error or new error for this interface,
2458 * spit out the message and record the error
2460 if (!vui->sock_errno || (vui->sock_errno != errno))
2463 ("Error: Could not open unix socket for %s",
2464 vui->sock_filename);
2465 vui->sock_errno = errno;
2470 /* try to connect */
2471 strncpy (sun.sun_path, (char *) vui->sock_filename,
2472 sizeof (sun.sun_path) - 1);
2474 /* Avoid hanging VPP if the other end does not accept */
2475 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2476 clib_unix_warning ("fcntl");
2478 if (connect (sockfd, (struct sockaddr *) &sun,
2479 sizeof (struct sockaddr_un)) == 0)
2481 /* Set the socket to blocking as it was before */
2482 if (fcntl(sockfd, F_SETFL, 0) < 0)
2483 clib_unix_warning ("fcntl2");
2485 vui->sock_errno = 0;
2486 template.file_descriptor = sockfd;
2487 template.private_data =
2488 vui - vhost_user_main.vhost_user_interfaces;
2489 vui->unix_file_index = unix_file_add (&unix_main, &template);
2491 /* This sockfd is considered consumed */
2496 vui->sock_errno = errno;
2501 /* check if socket is alive */
2503 socklen_t len = sizeof (error);
2504 int fd = UNIX_GET_FD(vui->unix_file_index);
2506 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
2510 DBG_SOCK ("getsockopt returned %d", retval);
2511 vhost_user_if_disconnect (vui);
2522 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
2523 .function = vhost_user_process,
2524 .type = VLIB_NODE_TYPE_PROCESS,
2525 .name = "vhost-user-process",
2530 * Disables and reset interface structure.
2531 * It can then be either init again, or removed from used interfaces.
2534 vhost_user_term_if (vhost_user_intf_t * vui)
2538 // Delete configured thread pinning
2539 vec_reset_length (vui->workers);
2540 // disconnect interface sockets
2541 vhost_user_if_disconnect (vui);
2542 vhost_user_update_iface_state (vui);
2544 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2546 clib_mem_free ((void *) vui->vring_locks[q]);
2549 if (vui->unix_server_index != ~0)
2551 //Close server socket
2552 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
2553 vui->unix_server_index);
2554 unix_file_del (&unix_main, uf);
2555 vui->unix_server_index = ~0;
2560 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
2562 vhost_user_main_t *vum = &vhost_user_main;
2563 vhost_user_intf_t *vui;
2565 vnet_hw_interface_t *hwif;
2567 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2568 hwif->dev_class_index != vhost_user_dev_class.index)
2569 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2571 DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2572 hwif->name, hwif->dev_instance);
2574 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2576 // Disable and reset interface
2577 vhost_user_term_if (vui);
2579 // Reset renumbered iface
2580 if (hwif->dev_instance <
2581 vec_len (vum->show_dev_instance_by_real_dev_instance))
2582 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
2584 // Delete ethernet interface
2585 ethernet_delete_interface (vnm, vui->hw_if_index);
2588 pool_put (vum->vhost_user_interfaces, vui);
2594 * Open server unix socket on specified sock_filename.
2597 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
2600 struct sockaddr_un un = { };
2602 /* create listening socket */
2603 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2604 return VNET_API_ERROR_SYSCALL_ERROR_1;
2606 un.sun_family = AF_UNIX;
2607 strncpy ((char *) un.sun_path, (char *) sock_filename,
2608 sizeof (un.sun_path) - 1);
2610 /* remove if exists */
2611 unlink ((char *) sock_filename);
2613 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2615 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2619 if (listen (fd, 1) == -1)
2621 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2634 * Create ethernet interface for vhost user interface.
2637 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
2638 vhost_user_intf_t * vui, u8 * hwaddress)
2640 vhost_user_main_t *vum = &vhost_user_main;
2642 clib_error_t *error;
2644 /* create hw and sw interface */
2647 clib_memcpy (hwaddr, hwaddress, 6);
2651 random_u32 (&vum->random);
2652 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
2657 error = ethernet_register_interface
2659 vhost_user_dev_class.index,
2660 vui - vum->vhost_user_interfaces /* device instance */ ,
2661 hwaddr /* ethernet address */ ,
2662 &vui->hw_if_index, 0 /* flag change */ );
2665 clib_error_report (error);
2667 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
2668 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
2672 * Initialize vui with specified attributes
2675 vhost_user_vui_init (vnet_main_t * vnm,
2676 vhost_user_intf_t * vui,
2678 const char *sock_filename,
2679 u64 feature_mask, u32 * sw_if_index, u8 operation_mode)
2681 vnet_sw_interface_t *sw;
2682 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
2685 if (server_sock_fd != -1)
2687 unix_file_t template = { 0 };
2688 template.read_function = vhost_user_socksvr_accept_ready;
2689 template.file_descriptor = server_sock_fd;
2690 template.private_data = vui - vhost_user_main.vhost_user_interfaces; //hw index
2691 vui->unix_server_index = unix_file_add (&unix_main, &template);
2695 vui->unix_server_index = ~0;
2698 vui->sw_if_index = sw->sw_if_index;
2699 strncpy (vui->sock_filename, sock_filename,
2700 ARRAY_LEN (vui->sock_filename) - 1);
2701 vui->sock_errno = 0;
2703 vui->feature_mask = feature_mask;
2704 vui->unix_file_index = ~0;
2705 vui->log_base_addr = 0;
2706 vui->operation_mode = operation_mode;
2708 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2709 vhost_user_vring_init (vui, q);
2711 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
2714 *sw_if_index = vui->sw_if_index;
2716 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2718 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
2719 CLIB_CACHE_LINE_BYTES);
2720 memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
2723 vec_validate (vui->per_cpu_tx_qid,
2724 vlib_get_thread_main ()->n_vlib_mains - 1);
2725 vhost_user_tx_thread_placement (vui);
2729 vhost_user_send_interrupt_process (vlib_main_t * vm,
2730 vlib_node_runtime_t * rt, vlib_frame_t * f)
2732 vhost_user_intf_t *vui;
2733 f64 timeout = 3153600000.0 /* 100 years */ ;
2734 uword event_type, *event_data = 0;
2735 vhost_user_main_t *vum = &vhost_user_main;
2736 vhost_iface_and_queue_t *vhiq;
2738 f64 now, poll_time_remaining;
2742 poll_time_remaining =
2743 vlib_process_wait_for_event_or_clock (vm, timeout);
2744 event_type = vlib_process_get_events (vm, &event_data);
2745 vec_reset_length (event_data);
2748 * Use the remaining timeout if it is less than coalesce time to avoid
2749 * resetting the existing timer in the middle of expiration
2751 timeout = poll_time_remaining;
2752 if (vlib_process_suspend_time_is_zero (timeout) ||
2753 (timeout > vum->coalesce_time))
2754 timeout = vum->coalesce_time;
2756 now = vlib_time_now (vm);
2759 case VHOST_USER_EVENT_START_TIMER:
2760 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
2765 vec_foreach (vhc, vum->cpus)
2767 u32 thread_index = vhc - vum->cpus;
2770 next_timeout = timeout;
2771 vec_foreach (vhiq, vum->cpus[thread_index].rx_queues)
2773 vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
2774 vhost_user_vring_t *rxvq =
2775 &vui->vrings[VHOST_VRING_IDX_RX (vhiq->qid)];
2776 vhost_user_vring_t *txvq =
2777 &vui->vrings[VHOST_VRING_IDX_TX (vhiq->qid)];
2779 if (txvq->n_since_last_int)
2781 if (now >= txvq->int_deadline)
2782 vhost_user_send_call (vm, txvq);
2784 next_timeout = txvq->int_deadline - now;
2787 if (rxvq->n_since_last_int)
2789 if (now >= rxvq->int_deadline)
2790 vhost_user_send_call (vm, rxvq);
2792 next_timeout = rxvq->int_deadline - now;
2795 if ((next_timeout < timeout) && (next_timeout > 0.0))
2796 timeout = next_timeout;
2802 clib_warning ("BUG: unhandled event type %d", event_type);
2805 /* No less than 1 millisecond */
2813 VLIB_REGISTER_NODE (vhost_user_send_interrupt_node,static) = {
2814 .function = vhost_user_send_interrupt_process,
2815 .type = VLIB_NODE_TYPE_PROCESS,
2816 .name = "vhost-user-send-interrupt-process",
2821 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2822 const char *sock_filename,
2826 u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
2829 vhost_user_intf_t *vui = NULL;
2832 int server_sock_fd = -1;
2833 vhost_user_main_t *vum = &vhost_user_main;
2835 if ((operation_mode != VHOST_USER_POLLING_MODE) &&
2836 (operation_mode != VHOST_USER_INTERRUPT_MODE))
2837 return VNET_API_ERROR_UNIMPLEMENTED;
2839 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2841 return VNET_API_ERROR_INVALID_ARGUMENT;
2847 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
2853 pool_get (vhost_user_main.vhost_user_interfaces, vui);
2855 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
2856 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
2857 feature_mask, &sw_if_idx, operation_mode);
2860 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2863 *sw_if_index = sw_if_idx;
2865 // Process node must connect
2866 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2868 if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
2869 !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
2870 (vum->coalesce_frames > 0))
2872 vum->interrupt_mode = 1;
2873 vlib_process_signal_event (vm, vhost_user_send_interrupt_node.index,
2874 VHOST_USER_EVENT_START_TIMER, 0);
2880 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2881 const char *sock_filename,
2884 u64 feature_mask, u8 renumber, u32 custom_dev_instance,
2887 vhost_user_main_t *vum = &vhost_user_main;
2888 vhost_user_intf_t *vui = NULL;
2890 int server_sock_fd = -1;
2892 vnet_hw_interface_t *hwif;
2894 if ((operation_mode != VHOST_USER_POLLING_MODE) &&
2895 (operation_mode != VHOST_USER_INTERRUPT_MODE))
2896 return VNET_API_ERROR_UNIMPLEMENTED;
2897 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2898 hwif->dev_class_index != vhost_user_dev_class.index)
2899 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2901 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2903 // First try to open server socket
2905 if ((rv = vhost_user_init_server_sock (sock_filename,
2906 &server_sock_fd)) != 0)
2909 vhost_user_term_if (vui);
2910 vhost_user_vui_init (vnm, vui, server_sock_fd,
2911 sock_filename, feature_mask, &sw_if_idx,
2915 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2917 // Process node must connect
2918 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2920 if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
2921 !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
2922 (vum->coalesce_frames > 0))
2924 vum->interrupt_mode = 1;
2925 vlib_process_signal_event (vm, vhost_user_send_interrupt_node.index,
2926 VHOST_USER_EVENT_START_TIMER, 0);
2932 unformat_vhost_user_operation_mode (unformat_input_t * input, va_list * args)
2934 u8 *operation_mode = va_arg (*args, u8 *);
2937 if (unformat (input, "interrupt"))
2938 *operation_mode = VHOST_USER_INTERRUPT_MODE;
2939 else if (unformat (input, "polling"))
2940 *operation_mode = VHOST_USER_POLLING_MODE;
2948 vhost_user_connect_command_fn (vlib_main_t * vm,
2949 unformat_input_t * input,
2950 vlib_cli_command_t * cmd)
2952 unformat_input_t _line_input, *line_input = &_line_input;
2953 u8 *sock_filename = NULL;
2956 u64 feature_mask = (u64) ~ (0ULL);
2958 u32 custom_dev_instance = ~0;
2961 clib_error_t *error = NULL;
2962 u8 operation_mode = VHOST_USER_POLLING_MODE;
2964 /* Get a line of input. */
2965 if (!unformat_user (input, unformat_line_input, line_input))
2968 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2970 if (unformat (line_input, "socket %s", &sock_filename))
2972 else if (unformat (line_input, "server"))
2974 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2978 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
2980 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
2984 else if (unformat (line_input, "mode %U",
2985 unformat_vhost_user_operation_mode, &operation_mode))
2989 error = clib_error_return (0, "unknown input `%U'",
2990 format_unformat_error, line_input);
2995 vnet_main_t *vnm = vnet_get_main ();
2998 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
2999 is_server, &sw_if_index, feature_mask,
3000 renumber, custom_dev_instance, hw,
3003 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
3007 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
3011 vec_free (sock_filename);
3012 unformat_free (line_input);
3018 vhost_user_delete_command_fn (vlib_main_t * vm,
3019 unformat_input_t * input,
3020 vlib_cli_command_t * cmd)
3022 unformat_input_t _line_input, *line_input = &_line_input;
3023 u32 sw_if_index = ~0;
3024 vnet_main_t *vnm = vnet_get_main ();
3025 clib_error_t *error = NULL;
3027 /* Get a line of input. */
3028 if (!unformat_user (input, unformat_line_input, line_input))
3031 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3033 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
3036 (line_input, "%U", unformat_vnet_sw_interface, vnm,
3039 vnet_hw_interface_t *hwif =
3040 vnet_get_sup_hw_interface (vnm, sw_if_index);
3042 vhost_user_dev_class.index != hwif->dev_class_index)
3044 error = clib_error_return (0, "Not a vhost interface");
3050 error = clib_error_return (0, "unknown input `%U'",
3051 format_unformat_error, line_input);
3056 vhost_user_delete_if (vnm, vm, sw_if_index);
3059 unformat_free (line_input);
3065 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
3066 vhost_user_intf_details_t ** out_vuids)
3069 vhost_user_main_t *vum = &vhost_user_main;
3070 vhost_user_intf_t *vui;
3071 vhost_user_intf_details_t *r_vuids = NULL;
3072 vhost_user_intf_details_t *vuid = NULL;
3073 u32 *hw_if_indices = 0;
3074 vnet_hw_interface_t *hi;
3081 pool_foreach (vui, vum->vhost_user_interfaces,
3082 vec_add1 (hw_if_indices, vui->hw_if_index);
3085 for (i = 0; i < vec_len (hw_if_indices); i++)
3087 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3088 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
3090 vec_add2 (r_vuids, vuid, 1);
3091 vuid->operation_mode = vui->operation_mode;
3092 vuid->sw_if_index = vui->sw_if_index;
3093 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
3094 vuid->features = vui->features;
3095 vuid->num_regions = vui->nregions;
3096 vuid->is_server = vui->unix_server_index != ~0;
3097 vuid->sock_errno = vui->sock_errno;
3098 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
3099 ARRAY_LEN (vuid->sock_filename) - 1);
3101 s = format (s, "%v%c", hi->name, 0);
3103 strncpy ((char *) vuid->if_name, (char *) s,
3104 ARRAY_LEN (vuid->if_name) - 1);
3109 vec_free (hw_if_indices);
3111 *out_vuids = r_vuids;
3117 format_vhost_user_operation_mode (u8 * s, va_list * va)
3119 int operation_mode = va_arg (*va, int);
3121 switch (operation_mode)
3123 case VHOST_USER_POLLING_MODE:
3124 s = format (s, "%s", "polling");
3126 case VHOST_USER_INTERRUPT_MODE:
3127 s = format (s, "%s", "interrupt");
3130 s = format (s, "%s", "invalid");
3136 show_vhost_user_command_fn (vlib_main_t * vm,
3137 unformat_input_t * input,
3138 vlib_cli_command_t * cmd)
3140 clib_error_t *error = 0;
3141 vnet_main_t *vnm = vnet_get_main ();
3142 vhost_user_main_t *vum = &vhost_user_main;
3143 vhost_user_intf_t *vui;
3144 u32 hw_if_index, *hw_if_indices = 0;
3145 vnet_hw_interface_t *hi;
3147 vhost_iface_and_queue_t *vhiq;
3157 struct feat_struct *feat_entry;
3159 static struct feat_struct feat_array[] = {
3160 #define _(s,b) { .str = #s, .bit = b, },
3161 foreach_virtio_net_feature
3166 #define foreach_protocol_feature \
3167 _(VHOST_USER_PROTOCOL_F_MQ) \
3168 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
3170 static struct feat_struct proto_feat_array[] = {
3171 #define _(s) { .str = #s, .bit = s},
3172 foreach_protocol_feature
3177 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3180 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
3182 vec_add1 (hw_if_indices, hw_if_index);
3184 else if (unformat (input, "descriptors") || unformat (input, "desc"))
3188 error = clib_error_return (0, "unknown input `%U'",
3189 format_unformat_error, input);
3193 if (vec_len (hw_if_indices) == 0)
3195 pool_foreach (vui, vum->vhost_user_interfaces,
3196 vec_add1 (hw_if_indices, vui->hw_if_index);
3199 vlib_cli_output (vm, "Virtio vhost-user interfaces");
3200 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
3201 vum->coalesce_frames, vum->coalesce_time);
3203 for (i = 0; i < vec_len (hw_if_indices); i++)
3205 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
3206 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
3207 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
3208 hi->name, hw_if_indices[i]);
3210 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
3211 " features mask (0x%llx): \n"
3212 " features (0x%llx): \n",
3213 vui->virtio_net_hdr_sz, vui->feature_mask,
3216 feat_entry = (struct feat_struct *) &feat_array;
3217 while (feat_entry->str)
3219 if (vui->features & (1ULL << feat_entry->bit))
3220 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3225 vlib_cli_output (vm, " protocol features (0x%llx)",
3226 vui->protocol_features);
3227 feat_entry = (struct feat_struct *) &proto_feat_array;
3228 while (feat_entry->str)
3230 if (vui->protocol_features & (1ULL << feat_entry->bit))
3231 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3236 vlib_cli_output (vm, "\n");
3238 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
3240 (vui->unix_server_index != ~0) ? "server" : "client",
3241 strerror (vui->sock_errno));
3243 vlib_cli_output (vm, " configured mode: %U\n",
3244 format_vhost_user_operation_mode, vui->operation_mode);
3245 vlib_cli_output (vm, " rx placement: ");
3246 vec_foreach (vhc, vum->cpus)
3248 vec_foreach (vhiq, vhc->rx_queues)
3250 if (vhiq->vhost_iface_index == vui - vum->vhost_user_interfaces)
3252 vlib_cli_output (vm, " thread %d on vring %d\n",
3254 VHOST_VRING_IDX_TX (vhiq->qid));
3255 vlib_cli_output (vm, " mode: %U\n",
3256 format_vhost_user_operation_mode,
3257 vhc->operation_mode);
3262 vlib_cli_output (vm, " tx placement: %s\n",
3263 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
3265 vec_foreach_index (ci, vui->per_cpu_tx_qid)
3267 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
3268 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
3271 vlib_cli_output (vm, "\n");
3273 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
3277 vlib_cli_output (vm,
3278 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
3279 vlib_cli_output (vm,
3280 " ====== ===== ================== ================== ================== ================== ==================\n");
3282 for (j = 0; j < vui->nregions; j++)
3284 vlib_cli_output (vm,
3285 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
3286 j, vui->region_mmap_fd[j],
3287 vui->regions[j].guest_phys_addr,
3288 vui->regions[j].memory_size,
3289 vui->regions[j].userspace_addr,
3290 vui->regions[j].mmap_offset,
3291 pointer_to_uword (vui->region_mmap_addr[j]));
3293 for (q = 0; q < VHOST_VRING_MAX_N; q++)
3295 if (!vui->vrings[q].started)
3298 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
3299 (q & 1) ? "RX" : "TX",
3300 vui->vrings[q].enabled ? "" : " disabled");
3302 vlib_cli_output (vm,
3303 " qsz %d last_avail_idx %d last_used_idx %d\n",
3304 vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
3305 vui->vrings[q].last_used_idx);
3307 if (vui->vrings[q].avail && vui->vrings[q].used)
3308 vlib_cli_output (vm,
3309 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
3310 vui->vrings[q].avail->flags,
3311 vui->vrings[q].avail->idx,
3312 vui->vrings[q].used->flags,
3313 vui->vrings[q].used->idx);
3315 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
3316 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
3317 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
3318 kickfd, callfd, vui->vrings[q].errfd);
3322 vlib_cli_output (vm, "\n descriptor table:\n");
3323 vlib_cli_output (vm,
3324 " id addr len flags next user_addr\n");
3325 vlib_cli_output (vm,
3326 " ===== ================== ===== ====== ===== ==================\n");
3327 for (j = 0; j < vui->vrings[q].qsz; j++)
3330 vlib_cli_output (vm,
3331 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3332 j, vui->vrings[q].desc[j].addr,
3333 vui->vrings[q].desc[j].len,
3334 vui->vrings[q].desc[j].flags,
3335 vui->vrings[q].desc[j].next,
3336 pointer_to_uword (map_guest_mem
3338 vui->vrings[q].desc[j].
3343 vlib_cli_output (vm, "\n");
3346 vec_free (hw_if_indices);
3355 * Create a vHost User interface. Once created, a new virtual interface
3356 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3357 * is the next free index.
3359 * There are several parameters associated with a vHost interface:
3361 * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3362 * VPP to manage the vHost interface. If socket does not already exist, VPP will
3363 * create the socket.
3365 * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3366 * linux socket. If not provided, VPP will be the client.
3368 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3369 * startup. By default, all supported features will be advertised. Otherwise,
3370 * provide the set of features desired.
3371 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3372 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3373 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3374 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3375 * - 0x004000000 (26) - VHOST_F_LOG_ALL
3376 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3377 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3378 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3379 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3381 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3382 * X:X:X:X:X:X unix or X.X.X cisco format.
3384 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3385 * in the name to be specified. If instance already exists, name will be used
3386 * anyway and multiple instances will have the same name. Use with caution.
3388 * - <b>mode [interrupt | polling]</b> - Optional parameter specifying
3389 * the input thread polling policy.
3392 * Example of how to create a vhost interface with VPP as the client and all features enabled:
3393 * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3394 * VirtualEthernet0/0/0
3396 * Example of how to create a vhost interface with VPP as the server and with just
3397 * multiple queues enabled:
3398 * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3399 * VirtualEthernet0/0/1
3401 * Once the vHost interface is created, enable the interface using:
3402 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3405 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3406 .path = "create vhost-user",
3407 .short_help = "create vhost-user socket <socket-filename> [server] "
3408 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] "
3409 "[mode {interrupt | polling}]",
3410 .function = vhost_user_connect_command_fn,
3415 * Delete a vHost User interface using the interface name or the
3416 * software interface index. Use the '<em>show interface</em>'
3417 * command to determine the software interface index. On deletion,
3418 * the linux socket will not be deleted.
3421 * Example of how to delete a vhost interface by name:
3422 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3423 * Example of how to delete a vhost interface by software interface index:
3424 * @cliexcmd{delete vhost-user sw_if_index 1}
3427 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3428 .path = "delete vhost-user",
3429 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
3430 .function = vhost_user_delete_command_fn,
3434 * Display the attributes of a single vHost User interface (provide interface
3435 * name), multiple vHost User interfaces (provide a list of interface names seperated
3436 * by spaces) or all Vhost User interfaces (omit an interface name to display all
3437 * vHost interfaces).
3441 * Example of how to display a vhost interface:
3442 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3443 * Virtio vhost-user interfaces
3445 * coalesce frames 32 time 1e-3
3446 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3447 * virtio_net_hdr_sz 12
3448 * features mask (0xffffffffffffffff):
3449 * features (0x50408000):
3450 * VIRTIO_NET_F_MRG_RXBUF (15)
3451 * VIRTIO_NET_F_MQ (22)
3452 * VIRTIO_F_INDIRECT_DESC (28)
3453 * VHOST_USER_F_PROTOCOL_FEATURES (30)
3454 * protocol features (0x3)
3455 * VHOST_USER_PROTOCOL_F_MQ (0)
3456 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3458 * socket filename /tmp/vhost1.sock type client errno "Success"
3461 * thread 1 on vring 1
3462 * thread 1 on vring 5
3463 * thread 2 on vring 3
3464 * thread 2 on vring 7
3465 * tx placement: spin-lock
3466 * thread 0 on vring 0
3467 * thread 1 on vring 2
3468 * thread 2 on vring 0
3470 * Memory regions (total 2)
3471 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3472 * ====== ===== ================== ================== ================== ================== ==================
3473 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3474 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3477 * qsz 256 last_avail_idx 0 last_used_idx 0
3478 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3479 * kickfd 62 callfd 64 errfd -1
3482 * qsz 256 last_avail_idx 0 last_used_idx 0
3483 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3484 * kickfd 65 callfd 66 errfd -1
3487 * qsz 256 last_avail_idx 0 last_used_idx 0
3488 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3489 * kickfd 63 callfd 70 errfd -1
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 72 callfd 74 errfd -1
3496 * Virtqueue 4 (TX 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 76 callfd 78 errfd -1
3501 * Virtqueue 5 (RX 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 80 callfd 82 errfd -1
3506 * Virtqueue 6 (TX 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 84 callfd 86 errfd -1
3511 * Virtqueue 7 (RX disabled)
3512 * qsz 256 last_avail_idx 0 last_used_idx 0
3513 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3514 * kickfd 88 callfd 90 errfd -1
3518 * The optional '<em>descriptors</em>' parameter will display the same output as
3519 * the previous example but will include the descriptor table for each queue.
3520 * The output is truncated below:
3521 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3522 * Virtio vhost-user interfaces
3524 * coalesce frames 32 time 1e-3
3525 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3526 * virtio_net_hdr_sz 12
3527 * features mask (0xffffffffffffffff):
3528 * features (0x50408000):
3529 * VIRTIO_NET_F_MRG_RXBUF (15)
3530 * VIRTIO_NET_F_MQ (22)
3533 * qsz 256 last_avail_idx 0 last_used_idx 0
3534 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3535 * kickfd 62 callfd 64 errfd -1
3538 * id addr len flags next user_addr
3539 * ===== ================== ===== ====== ===== ==================
3540 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3541 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3542 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3543 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3544 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3545 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3546 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3547 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3548 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3549 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3550 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3552 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3553 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3554 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3555 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3556 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3557 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3558 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3561 * qsz 256 last_avail_idx 0 last_used_idx 0
3567 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3568 .path = "show vhost-user",
3569 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
3570 .function = show_vhost_user_command_fn,
3574 static clib_error_t *
3575 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
3577 vhost_user_main_t *vum = &vhost_user_main;
3579 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3581 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
3583 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
3585 else if (unformat (input, "dont-dump-memory"))
3586 vum->dont_dump_vhost_user_memory = 1;
3588 return clib_error_return (0, "unknown input `%U'",
3589 format_unformat_error, input);
3595 /* vhost-user { ... } configuration. */
3596 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3599 vhost_user_unmap_all (void)
3601 vhost_user_main_t *vum = &vhost_user_main;
3602 vhost_user_intf_t *vui;
3604 if (vum->dont_dump_vhost_user_memory)
3606 pool_foreach (vui, vum->vhost_user_interfaces,
3607 unmap_all_mem_regions (vui);
3612 static clib_error_t *
3613 vhost_thread_command_fn (vlib_main_t * vm,
3614 unformat_input_t * input, vlib_cli_command_t * cmd)
3616 unformat_input_t _line_input, *line_input = &_line_input;
3617 u32 worker_thread_index;
3621 clib_error_t *error = NULL;
3623 /* Get a line of input. */
3624 if (!unformat_user (input, unformat_line_input, line_input))
3628 (line_input, "%U %d", unformat_vnet_sw_interface, vnet_get_main (),
3629 &sw_if_index, &worker_thread_index))
3631 error = clib_error_return (0, "unknown input `%U'",
3632 format_unformat_error, line_input);
3636 if (unformat (line_input, "del"))
3640 vhost_user_thread_placement (sw_if_index, worker_thread_index, del)))
3642 error = clib_error_return (0, "vhost_user_thread_placement returned %d",
3648 unformat_free (line_input);
3655 * This command is used to move the RX processing for the given
3656 * interfaces to the provided thread. If the '<em>del</em>' option is used,
3657 * the forced thread assignment is removed and the thread assigment is
3658 * reassigned automatically. Use '<em>show vhost-user <interface></em>'
3659 * to see the thread assignment.
3662 * Example of how to move the RX processing for a given interface to a given thread:
3663 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1}
3664 * Example of how to remove the forced thread assignment for a given interface:
3665 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1 del}
3668 VLIB_CLI_COMMAND (vhost_user_thread_command, static) = {
3669 .path = "vhost thread",
3670 .short_help = "vhost thread <iface> <worker-index> [del]",
3671 .function = vhost_thread_command_fn,
3676 * fd.io coding-style-patch-verification: ON
3679 * eval: (c-set-style "gnu")