2 *------------------------------------------------------------------
5 * Copyright (c) 2014 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <sys/uio.h> /* for iovec */
27 #include <netinet/in.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
36 #include <vnet/ip/ip.h>
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/devices/devices.h>
40 #include <vnet/feature/feature.h>
42 #include <vnet/devices/virtio/vhost-user.h>
46 * @brief vHost User Device Driver.
48 * This file contains the source code for vHost User interface.
52 #define VHOST_USER_DEBUG_SOCKET 0
53 #define VHOST_DEBUG_VQ 0
55 #if VHOST_USER_DEBUG_SOCKET == 1
56 #define DBG_SOCK(args...) clib_warning(args);
58 #define DBG_SOCK(args...)
61 #if VHOST_DEBUG_VQ == 1
62 #define DBG_VQ(args...) clib_warning(args);
64 #define DBG_VQ(args...)
68 * When an RX queue is down but active, received packets
69 * must be discarded. This value controls up to how many
70 * packets will be discarded during each round.
72 #define VHOST_USER_DOWN_DISCARD_COUNT 256
75 * When the number of available buffers gets under this threshold,
76 * RX node will start discarding packets.
78 #define VHOST_USER_RX_BUFFER_STARVATION 32
81 * On the receive side, the host should free descriptors as soon
82 * as possible in order to avoid TX drop in the VM.
83 * This value controls the number of copy operations that are stacked
84 * before copy is done for all and descriptors are given back to
86 * The value 64 was obtained by testing (48 and 128 were not as good).
88 #define VHOST_USER_RX_COPY_THRESHOLD 64
90 #define UNIX_GET_FD(unixfd_idx) \
91 (unixfd_idx != ~0) ? \
92 pool_elt_at_index (unix_main.file_pool, \
93 unixfd_idx)->file_descriptor : -1;
95 #define foreach_virtio_trace_flags \
96 _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
97 _ (SINGLE_DESC, 1, "Single descriptor packet") \
98 _ (INDIRECT, 2, "Indirect descriptor") \
99 _ (MAP_ERROR, 4, "Memory mapping error")
103 #define _(n,i,s) VIRTIO_TRACE_F_##n,
104 foreach_virtio_trace_flags
106 } virtio_trace_flag_t;
108 vlib_node_registration_t vhost_user_input_node;
110 #define foreach_vhost_user_tx_func_error \
111 _(NONE, "no error") \
112 _(NOT_READY, "vhost vring not ready") \
113 _(DOWN, "vhost interface is down") \
114 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
115 _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
116 _(MMAP_FAIL, "mmap failure") \
117 _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
121 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
122 foreach_vhost_user_tx_func_error
124 VHOST_USER_TX_FUNC_N_ERROR,
125 } vhost_user_tx_func_error_t;
127 static char *vhost_user_tx_func_error_strings[] = {
129 foreach_vhost_user_tx_func_error
133 #define foreach_vhost_user_input_func_error \
134 _(NO_ERROR, "no error") \
135 _(NO_BUFFER, "no available buffer") \
136 _(MMAP_FAIL, "mmap failure") \
137 _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
138 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
139 _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
143 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
144 foreach_vhost_user_input_func_error
146 VHOST_USER_INPUT_FUNC_N_ERROR,
147 } vhost_user_input_func_error_t;
149 static char *vhost_user_input_func_error_strings[] = {
151 foreach_vhost_user_input_func_error
156 static vhost_user_main_t vhost_user_main = {
160 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
161 .name = "vhost-user",
166 format_vhost_user_interface_name (u8 * s, va_list * args)
168 u32 i = va_arg (*args, u32);
169 u32 show_dev_instance = ~0;
170 vhost_user_main_t *vum = &vhost_user_main;
172 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
173 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
175 if (show_dev_instance != ~0)
176 i = show_dev_instance;
178 s = format (s, "VirtualEthernet0/0/%d", i);
183 vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
185 // FIXME: check if the new dev instance is already used
186 vhost_user_main_t *vum = &vhost_user_main;
187 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
188 hi->dev_instance, ~0);
190 vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
193 DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
194 hi->dev_instance, new_dev_instance);
199 static_always_inline void *
200 map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint)
203 if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) &&
204 ((vui->regions[i].guest_phys_addr +
205 vui->regions[i].memory_size) > addr)))
207 return (void *) (vui->region_mmap_addr[i] + addr -
208 vui->regions[i].guest_phys_addr);
211 __m128i rl, rh, al, ah, r;
212 al = _mm_set1_epi64x (addr + 1);
213 ah = _mm_set1_epi64x (addr);
215 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]);
216 rl = _mm_cmpgt_epi64 (al, rl);
217 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]);
218 rh = _mm_cmpgt_epi64 (rh, ah);
219 r = _mm_and_si128 (rl, rh);
221 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]);
222 rl = _mm_cmpgt_epi64 (al, rl);
223 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]);
224 rh = _mm_cmpgt_epi64 (rh, ah);
225 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22);
227 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]);
228 rl = _mm_cmpgt_epi64 (al, rl);
229 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]);
230 rh = _mm_cmpgt_epi64 (rh, ah);
231 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44);
233 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]);
234 rl = _mm_cmpgt_epi64 (al, rl);
235 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]);
236 rh = _mm_cmpgt_epi64 (rh, ah);
237 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
239 r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
240 i = __builtin_ctzll (_mm_movemask_epi8 (r) |
241 (1 << VHOST_MEMORY_MAX_NREGIONS));
243 if (i < vui->nregions)
246 return (void *) (vui->region_mmap_addr[i] + addr -
247 vui->regions[i].guest_phys_addr);
251 for (i = 0; i < vui->nregions; i++)
253 if ((vui->regions[i].guest_phys_addr <= addr) &&
254 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
258 return (void *) (vui->region_mmap_addr[i] + addr -
259 vui->regions[i].guest_phys_addr);
263 DBG_VQ ("failed to map guest mem addr %llx", addr);
269 map_user_mem (vhost_user_intf_t * vui, uword addr)
272 for (i = 0; i < vui->nregions; i++)
274 if ((vui->regions[i].userspace_addr <= addr) &&
275 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
278 return (void *) (vui->region_mmap_addr[i] + addr -
279 vui->regions[i].userspace_addr);
286 get_huge_page_size (int fd)
294 unmap_all_mem_regions (vhost_user_intf_t * vui)
297 for (i = 0; i < vui->nregions; i++)
299 if (vui->region_mmap_addr[i] != (void *) -1)
302 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
304 ssize_t map_sz = (vui->regions[i].memory_size +
305 vui->regions[i].mmap_offset +
306 page_sz) & ~(page_sz - 1);
309 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
313 ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
314 vui->region_mmap_addr[i], map_sz, page_sz);
316 vui->region_mmap_addr[i] = (void *) -1;
320 clib_warning ("failed to unmap memory region (errno %d)",
323 close (vui->region_mmap_fd[i]);
330 vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
332 //Let's try to assign one queue to each thread
335 vui->use_tx_spinlock = 0;
338 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
340 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
341 if (!rxvq->started || !rxvq->enabled)
344 vui->per_cpu_tx_qid[cpu_index] = qid;
346 if (cpu_index == vlib_get_thread_main ()->n_vlib_mains)
349 //We need to loop, meaning the spinlock has to be used
350 vui->use_tx_spinlock = 1;
353 //Could not find a single valid one
355 cpu_index < vlib_get_thread_main ()->n_vlib_mains; cpu_index++)
357 vui->per_cpu_tx_qid[cpu_index] = 0;
365 vhost_user_rx_thread_placement ()
367 vhost_user_main_t *vum = &vhost_user_main;
368 vhost_user_intf_t *vui;
372 //Let's list all workers cpu indexes
374 for (i = vum->input_cpu_first_index;
375 i < vum->input_cpu_first_index + vum->input_cpu_count; i++)
377 vlib_node_set_state (vlib_mains ? vlib_mains[i] : &vlib_global_main,
378 vhost_user_input_node.index,
379 VLIB_NODE_STATE_DISABLED);
380 vec_add1 (workers, i);
383 vec_foreach (vhc, vum->cpus)
385 vec_reset_length (vhc->rx_queues);
389 vhost_iface_and_queue_t iaq;
391 pool_foreach (vui, vum->vhost_user_interfaces, {
392 u32 *vui_workers = vec_len (vui->workers) ? vui->workers : workers;
394 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
396 vhost_user_vring_t *txvq =
397 &vui->vrings[VHOST_VRING_IDX_TX (qid)];
401 i %= vec_len (vui_workers);
402 u32 cpu_index = vui_workers[i];
404 vhc = &vum->cpus[cpu_index];
407 iaq.vhost_iface_index = vui - vum->vhost_user_interfaces;
408 vec_add1 (vhc->rx_queues, iaq);
409 vlib_node_set_state (vlib_mains ? vlib_mains[cpu_index] :
410 &vlib_global_main, vhost_user_input_node.index,
411 VLIB_NODE_STATE_POLLING);
418 vhost_user_thread_placement (u32 sw_if_index, u32 worker_thread_index, u8 del)
420 vhost_user_main_t *vum = &vhost_user_main;
421 vhost_user_intf_t *vui;
422 vnet_hw_interface_t *hw;
424 if (worker_thread_index < vum->input_cpu_first_index ||
425 worker_thread_index >=
426 vum->input_cpu_first_index + vum->input_cpu_count)
429 if (!(hw = vnet_get_sup_hw_interface (vnet_get_main (), sw_if_index)))
432 vui = pool_elt_at_index (vum->vhost_user_interfaces, hw->dev_instance);
434 vec_foreach (w, vui->workers)
436 if (*w == worker_thread_index)
438 found = w - vui->workers;
447 vec_del1 (vui->workers, found);
449 else if (found == ~0)
451 vec_add1 (vui->workers, worker_thread_index);
454 vhost_user_rx_thread_placement ();
458 /** @brief Returns whether at least one TX and one RX vring are enabled */
460 vhost_user_intf_ready (vhost_user_intf_t * vui)
462 int i, found[2] = { }; //RX + TX
464 for (i = 0; i < VHOST_VRING_MAX_N; i++)
465 if (vui->vrings[i].started && vui->vrings[i].enabled)
468 return found[0] && found[1];
472 vhost_user_update_iface_state (vhost_user_intf_t * vui)
474 /* if we have pointers to descriptor table, go up */
475 int is_up = vhost_user_intf_ready (vui);
476 if (is_up != vui->is_up)
478 DBG_SOCK ("interface %d %s", vui->sw_if_index,
479 is_up ? "ready" : "down");
480 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
481 is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP :
485 vhost_user_rx_thread_placement ();
486 vhost_user_tx_thread_placement (vui);
489 static clib_error_t *
490 vhost_user_callfd_read_ready (unix_file_t * uf)
492 __attribute__ ((unused)) int n;
494 n = read (uf->file_descriptor, ((char *) &buff), 8);
498 static clib_error_t *
499 vhost_user_kickfd_read_ready (unix_file_t * uf)
501 __attribute__ ((unused)) int n;
503 vhost_user_intf_t *vui =
504 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
505 uf->private_data >> 8);
506 u32 qid = uf->private_data & 0xff;
507 n = read (uf->file_descriptor, ((char *) &buff), 8);
508 DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
510 vlib_worker_thread_barrier_sync (vlib_get_main ());
511 vui->vrings[qid].started = 1;
512 vhost_user_update_iface_state (vui);
513 vlib_worker_thread_barrier_release (vlib_get_main ());
518 * @brief Try once to lock the vring
519 * @return 0 on success, non-zero on failure.
522 vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
524 return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
528 * @brief Spin until the vring is successfully locked
531 vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
533 while (vhost_user_vring_try_lock (vui, qid))
538 * @brief Unlock the vring lock
541 vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
543 *vui->vring_locks[qid] = 0;
547 vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
549 vhost_user_vring_t *vring = &vui->vrings[qid];
550 memset (vring, 0, sizeof (*vring));
551 vring->kickfd_idx = ~0;
552 vring->callfd_idx = ~0;
556 * We have a bug with some qemu 2.5, and this may be a fix.
557 * Feel like interpretation holy text, but this is from vhost-user.txt.
559 * One queue pair is enabled initially. More queues are enabled
560 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
562 * Don't know who's right, but this is what DPDK does.
564 if (qid == 0 || qid == 1)
569 vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
571 vhost_user_vring_t *vring = &vui->vrings[qid];
572 if (vring->kickfd_idx != ~0)
574 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
576 unix_file_del (&unix_main, uf);
577 vring->kickfd_idx = ~0;
579 if (vring->callfd_idx != ~0)
581 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
583 unix_file_del (&unix_main, uf);
584 vring->callfd_idx = ~0;
586 if (vring->errfd != -1)
587 close (vring->errfd);
588 vhost_user_vring_init (vui, qid);
592 vhost_user_if_disconnect (vhost_user_intf_t * vui)
594 vnet_main_t *vnm = vnet_get_main ();
597 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
599 if (vui->unix_file_index != ~0)
601 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
602 vui->unix_file_index = ~0;
607 for (q = 0; q < VHOST_VRING_MAX_N; q++)
608 vhost_user_vring_close (vui, q);
610 unmap_all_mem_regions (vui);
611 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
614 #define VHOST_LOG_PAGE 0x1000
615 static_always_inline void
616 vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
617 u64 addr, u64 len, u8 is_host_address)
619 if (PREDICT_TRUE (vui->log_base_addr == 0
620 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
626 addr = (u64) map_user_mem (vui, (uword) addr);
628 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
630 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
634 CLIB_MEMORY_BARRIER ();
635 u64 page = addr / VHOST_LOG_PAGE;
636 while (page * VHOST_LOG_PAGE < addr + len)
638 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
643 static_always_inline void
644 vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
646 vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
649 #define vhost_user_log_dirty_ring(vui, vq, member) \
650 if (PREDICT_FALSE(vq->log_used)) { \
651 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
652 sizeof(vq->used->member)); \
655 static clib_error_t *
656 vhost_user_socket_read (unix_file_t * uf)
659 int fd, number_of_fds = 0;
660 int fds[VHOST_MEMORY_MAX_NREGIONS];
661 vhost_user_msg_t msg;
664 vhost_user_main_t *vum = &vhost_user_main;
665 vhost_user_intf_t *vui;
666 struct cmsghdr *cmsg;
668 unix_file_t template = { 0 };
669 vnet_main_t *vnm = vnet_get_main ();
671 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
673 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
675 memset (&mh, 0, sizeof (mh));
676 memset (control, 0, sizeof (control));
678 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
681 /* set the payload */
682 iov[0].iov_base = (void *) &msg;
683 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
687 mh.msg_control = control;
688 mh.msg_controllen = sizeof (control);
690 n = recvmsg (uf->file_descriptor, &mh, 0);
692 /* Stop workers to avoid end of the world */
693 vlib_worker_thread_barrier_sync (vlib_get_main ());
695 if (n != VHOST_USER_MSG_HDR_SZ)
699 DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
703 DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
704 n, VHOST_USER_MSG_HDR_SZ);
709 if (mh.msg_flags & MSG_CTRUNC)
711 DBG_SOCK ("MSG_CTRUNC is set");
715 cmsg = CMSG_FIRSTHDR (&mh);
717 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
718 (cmsg->cmsg_type == SCM_RIGHTS) &&
719 (cmsg->cmsg_len - CMSG_LEN (0) <=
720 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
722 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
723 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
726 /* version 1, no reply bit set */
727 if ((msg.flags & 7) != 1)
729 DBG_SOCK ("malformed message received. closing socket");
736 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
740 DBG_SOCK ("read failed %s", strerror (errno));
743 else if (rv != msg.size)
745 DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
752 case VHOST_USER_GET_FEATURES:
754 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
755 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
756 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
757 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
758 (1ULL << FEAT_VHOST_F_LOG_ALL) |
759 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
760 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
761 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
762 (1ULL << FEAT_VIRTIO_F_VERSION_1);
763 msg.u64 &= vui->feature_mask;
764 msg.size = sizeof (msg.u64);
765 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
766 vui->hw_if_index, msg.u64);
769 case VHOST_USER_SET_FEATURES:
770 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
771 vui->hw_if_index, msg.u64);
773 vui->features = msg.u64;
776 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
777 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
778 vui->virtio_net_hdr_sz = 12;
780 vui->virtio_net_hdr_sz = 10;
783 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
785 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
786 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
789 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
790 vhost_user_vring_close(&vui->vrings[q]); */
794 case VHOST_USER_SET_MEM_TABLE:
795 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
796 vui->hw_if_index, msg.memory.nregions);
798 if ((msg.memory.nregions < 1) ||
799 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
802 DBG_SOCK ("number of mem regions must be between 1 and %i",
803 VHOST_MEMORY_MAX_NREGIONS);
808 if (msg.memory.nregions != number_of_fds)
810 DBG_SOCK ("each memory region must have FD");
813 unmap_all_mem_regions (vui);
814 for (i = 0; i < msg.memory.nregions; i++)
816 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
817 sizeof (vhost_user_memory_region_t));
819 long page_sz = get_huge_page_size (fds[i]);
821 /* align size to 2M page */
822 ssize_t map_sz = (vui->regions[i].memory_size +
823 vui->regions[i].mmap_offset +
824 page_sz) & ~(page_sz - 1);
826 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
827 MAP_SHARED, fds[i], 0);
828 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
829 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
830 vui->regions[i].memory_size;
833 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
834 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
837 if (vui->region_mmap_addr[i] == MAP_FAILED)
839 clib_warning ("failed to map memory. errno is %d", errno);
842 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
843 vui->region_mmap_fd[i] = fds[i];
845 vui->nregions = msg.memory.nregions;
848 case VHOST_USER_SET_VRING_NUM:
849 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
850 vui->hw_if_index, msg.state.index, msg.state.num);
852 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
853 (msg.state.num == 0) || /* it cannot be zero */
854 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
856 vui->vrings[msg.state.index].qsz = msg.state.num;
859 case VHOST_USER_SET_VRING_ADDR:
860 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
861 vui->hw_if_index, msg.state.index);
863 if (msg.state.index >= VHOST_VRING_MAX_N)
865 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
866 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
870 if (msg.size < sizeof (msg.addr))
872 DBG_SOCK ("vhost message is too short (%d < %d)",
873 msg.size, sizeof (msg.addr));
877 vui->vrings[msg.state.index].desc = (vring_desc_t *)
878 map_user_mem (vui, msg.addr.desc_user_addr);
879 vui->vrings[msg.state.index].used = (vring_used_t *)
880 map_user_mem (vui, msg.addr.used_user_addr);
881 vui->vrings[msg.state.index].avail = (vring_avail_t *)
882 map_user_mem (vui, msg.addr.avail_user_addr);
884 if ((vui->vrings[msg.state.index].desc == NULL) ||
885 (vui->vrings[msg.state.index].used == NULL) ||
886 (vui->vrings[msg.state.index].avail == NULL))
888 DBG_SOCK ("failed to map user memory for hw_if_index %d",
893 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
894 vui->vrings[msg.state.index].log_used =
895 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
897 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
898 the ring is initialized in an enabled state. */
899 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
901 vui->vrings[msg.state.index].enabled = 1;
904 vui->vrings[msg.state.index].last_used_idx =
905 vui->vrings[msg.state.index].last_avail_idx =
906 vui->vrings[msg.state.index].used->idx;
908 /* tell driver that we don't want interrupts */
909 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
912 case VHOST_USER_SET_OWNER:
913 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
916 case VHOST_USER_RESET_OWNER:
917 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
920 case VHOST_USER_SET_VRING_CALL:
921 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
922 vui->hw_if_index, msg.u64);
924 q = (u8) (msg.u64 & 0xFF);
926 /* if there is old fd, delete and close it */
927 if (vui->vrings[q].callfd_idx != ~0)
929 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
930 vui->vrings[q].callfd_idx);
931 unix_file_del (&unix_main, uf);
932 vui->vrings[q].callfd_idx = ~0;
935 if (!(msg.u64 & 0x100))
937 if (number_of_fds != 1)
939 DBG_SOCK ("More than one fd received !");
943 template.read_function = vhost_user_callfd_read_ready;
944 template.file_descriptor = fds[0];
945 template.private_data =
946 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
947 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
950 vui->vrings[q].callfd_idx = ~0;
953 case VHOST_USER_SET_VRING_KICK:
954 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
955 vui->hw_if_index, msg.u64);
957 q = (u8) (msg.u64 & 0xFF);
959 if (vui->vrings[q].kickfd_idx != ~0)
961 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
962 vui->vrings[q].kickfd_idx);
963 unix_file_del (&unix_main, uf);
964 vui->vrings[q].kickfd_idx = ~0;
967 if (!(msg.u64 & 0x100))
969 if (number_of_fds != 1)
971 DBG_SOCK ("More than one fd received !");
975 template.read_function = vhost_user_kickfd_read_ready;
976 template.file_descriptor = fds[0];
977 template.private_data =
978 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
980 vui->vrings[q].kickfd_idx = unix_file_add (&unix_main, &template);
984 //When no kickfd is set, the queue is initialized as started
985 vui->vrings[q].kickfd_idx = ~0;
986 vui->vrings[q].started = 1;
991 case VHOST_USER_SET_VRING_ERR:
992 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
993 vui->hw_if_index, msg.u64);
995 q = (u8) (msg.u64 & 0xFF);
997 if (vui->vrings[q].errfd != -1)
998 close (vui->vrings[q].errfd);
1000 if (!(msg.u64 & 0x100))
1002 if (number_of_fds != 1)
1005 vui->vrings[q].errfd = fds[0];
1008 vui->vrings[q].errfd = -1;
1012 case VHOST_USER_SET_VRING_BASE:
1013 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1014 vui->hw_if_index, msg.state.index, msg.state.num);
1016 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
1019 case VHOST_USER_GET_VRING_BASE:
1020 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1021 vui->hw_if_index, msg.state.index, msg.state.num);
1023 if (msg.state.index >= VHOST_VRING_MAX_N)
1025 DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
1026 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1030 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
1031 vhost_user_vring_close (vui, msg.state.index);
1033 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
1035 msg.size = sizeof (msg.state);
1038 case VHOST_USER_NONE:
1039 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
1043 case VHOST_USER_SET_LOG_BASE:
1045 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
1047 if (msg.size != sizeof (msg.log))
1050 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
1051 msg.size, sizeof (msg.log));
1056 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
1059 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1064 /* align size to 2M page */
1065 long page_sz = get_huge_page_size (fd);
1067 (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
1069 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1073 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
1074 map_sz, msg.log.offset, fd, vui->log_base_addr);
1076 if (vui->log_base_addr == MAP_FAILED)
1078 clib_warning ("failed to map memory. errno is %d", errno);
1082 vui->log_base_addr += msg.log.offset;
1083 vui->log_size = msg.log.size;
1086 msg.size = sizeof (msg.u64);
1091 case VHOST_USER_SET_LOG_FD:
1092 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
1096 case VHOST_USER_GET_PROTOCOL_FEATURES:
1097 DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1101 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1102 (1 << VHOST_USER_PROTOCOL_F_MQ);
1103 msg.size = sizeof (msg.u64);
1106 case VHOST_USER_SET_PROTOCOL_FEATURES:
1107 DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
1108 vui->hw_if_index, msg.u64);
1110 vui->protocol_features = msg.u64;
1114 case VHOST_USER_GET_QUEUE_NUM:
1115 DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM", vui->hw_if_index);
1117 msg.u64 = VHOST_VRING_MAX_N;
1118 msg.size = sizeof (msg.u64);
1121 case VHOST_USER_SET_VRING_ENABLE:
1122 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1123 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1125 if (msg.state.index >= VHOST_VRING_MAX_N)
1127 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
1128 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1132 vui->vrings[msg.state.index].enabled = msg.state.num;
1136 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1141 /* if we need to reply */
1145 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1146 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1148 DBG_SOCK ("could not send message response");
1153 vhost_user_update_iface_state (vui);
1154 vlib_worker_thread_barrier_release (vlib_get_main ());
1158 vhost_user_if_disconnect (vui);
1159 vhost_user_update_iface_state (vui);
1160 vlib_worker_thread_barrier_release (vlib_get_main ());
1164 static clib_error_t *
1165 vhost_user_socket_error (unix_file_t * uf)
1167 vlib_main_t *vm = vlib_get_main ();
1168 vhost_user_main_t *vum = &vhost_user_main;
1169 vhost_user_intf_t *vui =
1170 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1172 DBG_SOCK ("socket error on if %d", vui->sw_if_index);
1173 vlib_worker_thread_barrier_sync (vm);
1174 vhost_user_if_disconnect (vui);
1175 vhost_user_rx_thread_placement ();
1176 vlib_worker_thread_barrier_release (vm);
1180 static clib_error_t *
1181 vhost_user_socksvr_accept_ready (unix_file_t * uf)
1183 int client_fd, client_len;
1184 struct sockaddr_un client;
1185 unix_file_t template = { 0 };
1186 vhost_user_main_t *vum = &vhost_user_main;
1187 vhost_user_intf_t *vui;
1189 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
1191 client_len = sizeof (client);
1192 client_fd = accept (uf->file_descriptor,
1193 (struct sockaddr *) &client,
1194 (socklen_t *) & client_len);
1197 return clib_error_return_unix (0, "accept");
1199 DBG_SOCK ("New client socket for vhost interface %d", vui->sw_if_index);
1200 template.read_function = vhost_user_socket_read;
1201 template.error_function = vhost_user_socket_error;
1202 template.file_descriptor = client_fd;
1203 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1204 vui->unix_file_index = unix_file_add (&unix_main, &template);
1208 static clib_error_t *
1209 vhost_user_init (vlib_main_t * vm)
1211 clib_error_t *error;
1212 vhost_user_main_t *vum = &vhost_user_main;
1213 vlib_thread_main_t *tm = vlib_get_thread_main ();
1214 vlib_thread_registration_t *tr;
1217 error = vlib_call_init_function (vm, ip4_init);
1221 vum->coalesce_frames = 32;
1222 vum->coalesce_time = 1e-3;
1224 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1227 vec_foreach (cpu, vum->cpus)
1229 /* This is actually not necessary as validate already zeroes it
1230 * Just keeping the loop here for later because I am lazy. */
1231 cpu->rx_buffers_len = 0;
1234 /* find out which cpus will be used for input */
1235 vum->input_cpu_first_index = 0;
1236 vum->input_cpu_count = 1;
1237 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1238 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
1240 if (tr && tr->count > 0)
1242 vum->input_cpu_first_index = tr->first_index;
1243 vum->input_cpu_count = tr->count;
1246 vum->random = random_default_seed ();
1251 VLIB_INIT_FUNCTION (vhost_user_init);
1253 static clib_error_t *
1254 vhost_user_exit (vlib_main_t * vm)
1260 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
1263 format_vhost_trace (u8 * s, va_list * va)
1265 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1266 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
1267 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
1268 vhost_user_main_t *vum = &vhost_user_main;
1269 vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
1270 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
1273 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
1275 uword indent = format_get_indent (s);
1277 s = format (s, "%U %U queue %d\n", format_white_space, indent,
1278 format_vnet_sw_interface_name, vnm, sw, t->qid);
1280 s = format (s, "%U virtio flags:\n", format_white_space, indent);
1282 if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
1283 s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
1284 foreach_virtio_trace_flags
1286 s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
1287 format_white_space, indent, t->first_desc_len);
1289 s = format (s, "%U flags 0x%02x gso_type %u\n",
1290 format_white_space, indent,
1291 t->hdr.hdr.flags, t->hdr.hdr.gso_type);
1293 if (vui->virtio_net_hdr_sz == 12)
1294 s = format (s, "%U num_buff %u",
1295 format_white_space, indent, t->hdr.num_buffers);
1301 vhost_user_rx_trace (vhost_trace_t * t,
1302 vhost_user_intf_t * vui, u16 qid,
1303 vlib_buffer_t * b, vhost_user_vring_t * txvq)
1305 vhost_user_main_t *vum = &vhost_user_main;
1306 u32 qsz_mask = txvq->qsz - 1;
1307 u32 last_avail_idx = txvq->last_avail_idx;
1308 u32 desc_current = txvq->avail->ring[last_avail_idx & qsz_mask];
1309 vring_desc_t *hdr_desc = 0;
1310 virtio_net_hdr_mrg_rxbuf_t *hdr;
1313 memset (t, 0, sizeof (*t));
1314 t->device_index = vui - vum->vhost_user_interfaces;
1317 hdr_desc = &txvq->desc[desc_current];
1318 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1320 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1321 /* Header is the first here */
1322 hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
1324 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1326 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1328 if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1329 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1331 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1334 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1336 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
1338 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
1342 u32 len = vui->virtio_net_hdr_sz;
1343 memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
1348 vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
1350 vhost_user_main_t *vum = &vhost_user_main;
1352 int fd = UNIX_GET_FD (vq->callfd_idx);
1353 int rv __attribute__ ((unused));
1354 /* TODO: pay attention to rv */
1355 rv = write (fd, &x, sizeof (x));
1356 vq->n_since_last_int = 0;
1357 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
1360 static_always_inline u32
1361 vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1362 u16 copy_len, u32 * map_hint)
1364 void *src0, *src1, *src2, *src3;
1365 if (PREDICT_TRUE (copy_len >= 4))
1367 if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
1369 if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
1372 while (PREDICT_TRUE (copy_len >= 4))
1378 (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
1381 (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
1384 CLIB_PREFETCH (src2, 64, LOAD);
1385 CLIB_PREFETCH (src3, 64, LOAD);
1387 clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
1388 clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
1395 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
1397 clib_memcpy ((void *) cpy->dst, src0, cpy->len);
1405 * Try to discard packets from the tx ring (VPP RX path).
1406 * Returns the number of discarded packets.
1409 vhost_user_rx_discard_packet (vlib_main_t * vm,
1410 vhost_user_intf_t * vui,
1411 vhost_user_vring_t * txvq, u32 discard_max)
1414 * On the RX side, each packet corresponds to one descriptor
1415 * (it is the same whether it is a shallow descriptor, chained, or indirect).
1416 * Therefore, discarding a packet is like discarding a descriptor.
1418 u32 discarded_packets = 0;
1419 u32 avail_idx = txvq->avail->idx;
1420 u16 qsz_mask = txvq->qsz - 1;
1421 while (discarded_packets != discard_max)
1423 if (avail_idx == txvq->last_avail_idx)
1426 u16 desc_chain_head =
1427 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1428 txvq->last_avail_idx++;
1429 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
1430 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1431 vhost_user_log_dirty_ring (vui, txvq,
1432 ring[txvq->last_used_idx & qsz_mask]);
1433 txvq->last_used_idx++;
1434 discarded_packets++;
1438 CLIB_MEMORY_BARRIER ();
1439 txvq->used->idx = txvq->last_used_idx;
1440 vhost_user_log_dirty_ring (vui, txvq, idx);
1441 return discarded_packets;
1445 * In case of overflow, we need to rewind the array of allocated buffers.
1448 vhost_user_input_rewind_buffers (vlib_main_t * vm,
1449 vhost_cpu_t * cpu, vlib_buffer_t * b_head)
1451 u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1452 vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
1453 b_current->current_length = 0;
1454 b_current->flags = 0;
1455 while (b_current != b_head)
1457 cpu->rx_buffers_len++;
1458 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1459 b_current = vlib_get_buffer (vm, bi_current);
1460 b_current->current_length = 0;
1461 b_current->flags = 0;
1466 vhost_user_if_input (vlib_main_t * vm,
1467 vhost_user_main_t * vum,
1468 vhost_user_intf_t * vui,
1469 u16 qid, vlib_node_runtime_t * node)
1471 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
1472 u16 n_rx_packets = 0;
1475 u32 n_left_to_next, *to_next;
1476 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1477 u32 n_trace = vlib_get_trace_count (vm, node);
1480 u16 cpu_index = os_get_cpu_number ();
1484 /* do we have pending interrupts ? */
1485 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1486 f64 now = vlib_time_now (vm);
1488 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1489 vhost_user_send_call (vm, txvq);
1491 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1492 vhost_user_send_call (vm, rxvq);
1495 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
1498 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1501 if (PREDICT_FALSE (n_left == 0))
1504 if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
1507 * Discard input packet if interface is admin down or vring is not
1509 * "For example, for a networking device, in the disabled state
1510 * client must not supply any new RX packets, but must process
1511 * and discard any TX packets."
1513 vhost_user_rx_discard_packet (vm, vui, txvq,
1514 VHOST_USER_DOWN_DISCARD_COUNT);
1518 if (PREDICT_FALSE (n_left == txvq->qsz))
1521 * Informational error logging when VPP is not
1522 * receiving packets fast enough.
1524 vlib_error_count (vm, node->node_index,
1525 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1528 qsz_mask = txvq->qsz - 1;
1530 if (n_left > VLIB_FRAME_SIZE)
1531 n_left = VLIB_FRAME_SIZE;
1534 * For small packets (<2kB), we will not need more than one vlib buffer
1535 * per packet. In case packets are bigger, we will just yeld at some point
1536 * in the loop and come back later. This is not an issue as for big packet,
1537 * processing cost really comes from the memory copy.
1539 if (PREDICT_FALSE (vum->cpus[cpu_index].rx_buffers_len < n_left + 1))
1541 u32 curr_len = vum->cpus[cpu_index].rx_buffers_len;
1542 vum->cpus[cpu_index].rx_buffers_len +=
1543 vlib_buffer_alloc_from_free_list (vm,
1544 vum->cpus[cpu_index].rx_buffers +
1546 VHOST_USER_RX_BUFFERS_N - curr_len,
1547 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1550 (vum->cpus[cpu_index].rx_buffers_len <
1551 VHOST_USER_RX_BUFFER_STARVATION))
1553 /* In case of buffer starvation, discard some packets from the queue
1554 * and log the event.
1555 * We keep doing best effort for the remaining packets. */
1556 u32 flush = (n_left + 1 > vum->cpus[cpu_index].rx_buffers_len) ?
1557 n_left + 1 - vum->cpus[cpu_index].rx_buffers_len : 1;
1558 flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1561 vlib_increment_simple_counter (vnet_main.
1562 interface_main.sw_if_counters +
1563 VNET_INTERFACE_COUNTER_DROP,
1564 os_get_cpu_number (),
1565 vui->sw_if_index, flush);
1567 vlib_error_count (vm, vhost_user_input_node.index,
1568 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1574 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1576 while (n_left > 0 && n_left_to_next > 0)
1578 vlib_buffer_t *b_head, *b_current;
1581 u32 desc_data_offset;
1582 vring_desc_t *desc_table = txvq->desc;
1584 if (PREDICT_FALSE (vum->cpus[cpu_index].rx_buffers_len <= 1))
1586 /* Not enough rx_buffers
1587 * Note: We yeld on 1 so we don't need to do an additional
1588 * check for the next buffer prefetch.
1594 desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1595 vum->cpus[cpu_index].rx_buffers_len--;
1596 bi_current = (vum->cpus[cpu_index].rx_buffers)
1597 [vum->cpus[cpu_index].rx_buffers_len];
1598 b_head = b_current = vlib_get_buffer (vm, bi_current);
1599 to_next[0] = bi_current; //We do that now so we can forget about bi_current
1603 vlib_prefetch_buffer_with_index (vm,
1604 (vum->cpus[cpu_index].rx_buffers)
1605 [vum->cpus[cpu_index].
1606 rx_buffers_len - 1], LOAD);
1608 /* Just preset the used descriptor id and length for later */
1609 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_current;
1610 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1611 vhost_user_log_dirty_ring (vui, txvq,
1612 ring[txvq->last_used_idx & qsz_mask]);
1614 /* The buffer should already be initialized */
1615 b_head->total_length_not_including_first_buffer = 0;
1616 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1618 if (PREDICT_FALSE (n_trace))
1620 //TODO: next_index is not exactly known at that point
1621 vlib_trace_buffer (vm, node, next_index, b_head,
1622 /* follow_chain */ 0);
1624 vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1625 vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1627 vlib_set_trace_count (vm, node, n_trace);
1630 /* This depends on the setup but is very consistent
1631 * So I think the CPU branch predictor will make a pretty good job
1632 * at optimizing the decision. */
1633 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1635 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1638 if (PREDICT_FALSE (desc_table == 0))
1640 //FIXME: Handle error by shutdown the queue
1645 if (PREDICT_TRUE (vui->is_any_layout) ||
1646 (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
1648 /* ANYLAYOUT or single buffer */
1649 desc_data_offset = vui->virtio_net_hdr_sz;
1653 /* CSR case without ANYLAYOUT, skip 1st buffer */
1654 desc_data_offset = desc_table[desc_current].len;
1659 /* Get more input if necessary. Or end of packet. */
1660 if (desc_data_offset == desc_table[desc_current].len)
1662 if (PREDICT_FALSE (desc_table[desc_current].flags &
1665 desc_current = desc_table[desc_current].next;
1666 desc_data_offset = 0;
1674 /* Get more output if necessary. Or end of packet. */
1676 (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1679 (vum->cpus[cpu_index].rx_buffers_len == 0))
1681 /* Cancel speculation */
1686 * Checking if there are some left buffers.
1687 * If not, just rewind the used buffers and stop.
1688 * Note: Scheduled copies are not cancelled. This is
1689 * not an issue as they would still be valid. Useless,
1692 vhost_user_input_rewind_buffers (vm,
1693 &vum->cpus[cpu_index],
1699 /* Get next output */
1700 vum->cpus[cpu_index].rx_buffers_len--;
1702 (vum->cpus[cpu_index].rx_buffers)[vum->cpus
1703 [cpu_index].rx_buffers_len];
1704 b_current->next_buffer = bi_next;
1705 b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1706 bi_current = bi_next;
1707 b_current = vlib_get_buffer (vm, bi_current);
1710 /* Prepare a copy order executed later for the data */
1711 vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
1714 desc_table[desc_current].len - desc_data_offset;
1715 cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1716 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
1717 cpy->dst = (uword) vlib_buffer_get_current (b_current);
1718 cpy->src = desc_table[desc_current].addr + desc_data_offset;
1720 desc_data_offset += cpy->len;
1722 b_current->current_length += cpy->len;
1723 b_head->total_length_not_including_first_buffer += cpy->len;
1727 CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1729 n_rx_bytes += b_head->total_length_not_including_first_buffer;
1732 b_head->total_length_not_including_first_buffer -=
1733 b_head->current_length;
1735 /* consume the descriptor and return it as used */
1736 txvq->last_avail_idx++;
1737 txvq->last_used_idx++;
1739 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1741 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1742 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1746 u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1748 /* redirect if feature path enabled */
1749 vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
1752 u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1753 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1754 to_next, n_left_to_next,
1761 * Although separating memory copies from virtio ring parsing
1762 * is beneficial, we can offer to perform the copies from time
1763 * to time in order to free some space in the ring.
1765 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
1768 (vhost_user_input_copy (vui, vum->cpus[cpu_index].copy,
1769 copy_len, &map_hint)))
1772 ("Memory mapping error on interface hw_if_index=%d "
1773 "(Shutting down - Switch interface down and up to restart)",
1781 /* give buffers back to driver */
1782 CLIB_MEMORY_BARRIER ();
1783 txvq->used->idx = txvq->last_used_idx;
1784 vhost_user_log_dirty_ring (vui, txvq, idx);
1788 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1791 /* Do the memory copies */
1793 (vhost_user_input_copy (vui, vum->cpus[cpu_index].copy,
1794 copy_len, &map_hint)))
1796 clib_warning ("Memory mapping error on interface hw_if_index=%d "
1797 "(Shutting down - Switch interface down and up to restart)",
1802 /* give buffers back to driver */
1803 CLIB_MEMORY_BARRIER ();
1804 txvq->used->idx = txvq->last_used_idx;
1805 vhost_user_log_dirty_ring (vui, txvq, idx);
1807 /* interrupt (call) handling */
1808 if ((txvq->callfd_idx != ~0) && !(txvq->avail->flags & 1))
1810 txvq->n_since_last_int += n_rx_packets;
1812 if (txvq->n_since_last_int > vum->coalesce_frames)
1813 vhost_user_send_call (vm, txvq);
1816 /* increase rx counters */
1817 vlib_increment_combined_counter
1818 (vnet_main.interface_main.combined_sw_if_counters
1819 + VNET_INTERFACE_COUNTER_RX,
1820 os_get_cpu_number (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1822 return n_rx_packets;
1826 vhost_user_input (vlib_main_t * vm,
1827 vlib_node_runtime_t * node, vlib_frame_t * f)
1829 vhost_user_main_t *vum = &vhost_user_main;
1830 uword n_rx_packets = 0;
1831 u32 cpu_index = os_get_cpu_number ();
1834 vhost_iface_and_queue_t *vhiq;
1835 vec_foreach (vhiq, vum->cpus[cpu_index].rx_queues)
1837 vhost_user_intf_t *vui =
1838 &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
1839 n_rx_packets += vhost_user_if_input (vm, vum, vui, vhiq->qid, node);
1842 return n_rx_packets;
1846 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1847 .function = vhost_user_input,
1848 .type = VLIB_NODE_TYPE_INPUT,
1849 .name = "vhost-user-input",
1850 .sibling_of = "device-input",
1852 /* Will be enabled if/when hardware is detected. */
1853 .state = VLIB_NODE_STATE_DISABLED,
1855 .format_buffer = format_ethernet_header_with_length,
1856 .format_trace = format_vhost_trace,
1858 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1859 .error_strings = vhost_user_input_func_error_strings,
1862 VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
1867 vhost_user_tx_trace (vhost_trace_t * t,
1868 vhost_user_intf_t * vui, u16 qid,
1869 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
1871 vhost_user_main_t *vum = &vhost_user_main;
1872 u32 qsz_mask = rxvq->qsz - 1;
1873 u32 last_avail_idx = rxvq->last_avail_idx;
1874 u32 desc_current = rxvq->avail->ring[last_avail_idx & qsz_mask];
1875 vring_desc_t *hdr_desc = 0;
1878 memset (t, 0, sizeof (*t));
1879 t->device_index = vui - vum->vhost_user_interfaces;
1882 hdr_desc = &rxvq->desc[desc_current];
1883 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1885 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
1886 /* Header is the first here */
1887 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
1889 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1891 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1893 if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1894 !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1896 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1899 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1902 static_always_inline u32
1903 vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1904 u16 copy_len, u32 * map_hint)
1906 void *dst0, *dst1, *dst2, *dst3;
1907 if (PREDICT_TRUE (copy_len >= 4))
1909 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
1911 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
1913 while (PREDICT_TRUE (copy_len >= 4))
1919 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
1922 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
1925 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
1926 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
1928 clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
1929 clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
1931 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
1932 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
1939 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
1941 clib_memcpy (dst0, (void *) cpy->src, cpy->len);
1942 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
1951 vhost_user_tx (vlib_main_t * vm,
1952 vlib_node_runtime_t * node, vlib_frame_t * frame)
1954 u32 *buffers = vlib_frame_args (frame);
1955 u32 n_left = frame->n_vectors;
1956 vhost_user_main_t *vum = &vhost_user_main;
1957 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
1958 vhost_user_intf_t *vui =
1959 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1961 vhost_user_vring_t *rxvq;
1964 u32 cpu_index = os_get_cpu_number ();
1970 if (PREDICT_FALSE (!vui->admin_up))
1972 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
1976 if (PREDICT_FALSE (!vui->is_up))
1978 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1983 VHOST_VRING_IDX_RX (*vec_elt_at_index
1984 (vui->per_cpu_tx_qid, os_get_cpu_number ()));
1985 rxvq = &vui->vrings[qid];
1986 if (PREDICT_FALSE (vui->use_tx_spinlock))
1987 vhost_user_vring_lock (vui, qid);
1989 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
1992 error = VHOST_USER_TX_FUNC_ERROR_NONE;
1997 vlib_buffer_t *b0, *current_b0;
1998 u16 desc_head, desc_index, desc_len;
1999 vring_desc_t *desc_table;
2000 uword buffer_map_addr;
2004 if (PREDICT_TRUE (n_left > 1))
2005 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2007 b0 = vlib_get_buffer (vm, buffers[0]);
2009 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2011 vum->cpus[cpu_index].current_trace =
2012 vlib_add_trace (vm, node, b0,
2013 sizeof (*vum->cpus[cpu_index].current_trace));
2014 vhost_user_tx_trace (vum->cpus[cpu_index].current_trace,
2015 vui, qid / 2, b0, rxvq);
2018 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2020 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2024 desc_table = rxvq->desc;
2025 desc_head = desc_index =
2026 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2028 /* Go deeper in case of indirect descriptor
2029 * I don't know of any driver providing indirect for RX. */
2030 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2033 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2035 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2040 map_guest_mem (vui, rxvq->desc[desc_index].addr,
2043 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2049 desc_len = vui->virtio_net_hdr_sz;
2050 buffer_map_addr = desc_table[desc_index].addr;
2051 buffer_len = desc_table[desc_index].len;
2054 // Get a header from the header array
2055 virtio_net_hdr_mrg_rxbuf_t *hdr =
2056 &vum->cpus[cpu_index].tx_headers[tx_headers_len];
2059 hdr->hdr.gso_type = 0;
2060 hdr->num_buffers = 1; //This is local, no need to check
2062 // Prepare a copy order executed later for the header
2063 vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
2065 cpy->len = vui->virtio_net_hdr_sz;
2066 cpy->dst = buffer_map_addr;
2067 cpy->src = (uword) hdr;
2070 buffer_map_addr += vui->virtio_net_hdr_sz;
2071 buffer_len -= vui->virtio_net_hdr_sz;
2072 bytes_left = b0->current_length;
2076 if (buffer_len == 0)
2078 if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2080 //Next one is chained
2081 desc_index = desc_table[desc_index].next;
2082 buffer_map_addr = desc_table[desc_index].addr;
2083 buffer_len = desc_table[desc_index].len;
2085 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2087 virtio_net_hdr_mrg_rxbuf_t *hdr =
2088 &vum->cpus[cpu_index].tx_headers[tx_headers_len - 1];
2090 //Move from available to used buffer
2091 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id =
2093 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len =
2095 vhost_user_log_dirty_ring (vui, rxvq,
2096 ring[rxvq->last_used_idx &
2099 rxvq->last_avail_idx++;
2100 rxvq->last_used_idx++;
2105 (rxvq->last_avail_idx == rxvq->avail->idx))
2107 //Dequeue queued descriptors for this packet
2108 rxvq->last_used_idx -= hdr->num_buffers - 1;
2109 rxvq->last_avail_idx -= hdr->num_buffers - 1;
2110 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2114 desc_table = rxvq->desc;
2115 desc_head = desc_index =
2116 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2118 (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2120 //It is seriously unlikely that a driver will put indirect descriptor
2121 //after non-indirect descriptor.
2123 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2125 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2131 rxvq->desc[desc_index].addr,
2134 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2139 buffer_map_addr = desc_table[desc_index].addr;
2140 buffer_len = desc_table[desc_index].len;
2144 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2150 vhost_copy_t *cpy = &vum->cpus[cpu_index].copy[copy_len];
2152 cpy->len = bytes_left;
2153 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2154 cpy->dst = buffer_map_addr;
2155 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2156 current_b0->current_length - bytes_left;
2158 bytes_left -= cpy->len;
2159 buffer_len -= cpy->len;
2160 buffer_map_addr += cpy->len;
2161 desc_len += cpy->len;
2163 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
2166 // Check if vlib buffer has more data. If not, get more or break.
2167 if (PREDICT_TRUE (!bytes_left))
2170 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2172 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2173 bytes_left = current_b0->current_length;
2183 //Move from available to used ring
2184 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
2185 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
2186 vhost_user_log_dirty_ring (vui, rxvq,
2187 ring[rxvq->last_used_idx & qsz_mask]);
2188 rxvq->last_avail_idx++;
2189 rxvq->last_used_idx++;
2191 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2193 vum->cpus[cpu_index].current_trace->hdr =
2194 vum->cpus[cpu_index].tx_headers[tx_headers_len - 1];
2197 n_left--; //At the end for error counting when 'goto done' is invoked
2202 //Do the memory copies
2204 (vhost_user_tx_copy (vui, vum->cpus[cpu_index].copy,
2205 copy_len, &map_hint)))
2207 clib_warning ("Memory mapping error on interface hw_if_index=%d "
2208 "(Shutting down - Switch interface down and up to restart)",
2213 CLIB_MEMORY_BARRIER ();
2214 rxvq->used->idx = rxvq->last_used_idx;
2215 vhost_user_log_dirty_ring (vui, rxvq, idx);
2218 * When n_left is set, error is always set to something too.
2219 * In case error is due to lack of remaining buffers, we go back up and
2221 * The idea is that it is better to waste some time on packets
2222 * that have been processed already than dropping them and get
2223 * more fresh packets with a good likelyhood that they will be dropped too.
2224 * This technique also gives more time to VM driver to pick-up packets.
2225 * In case the traffic flows from physical to virtual interfaces, this
2226 * technique will end-up leveraging the physical NIC buffer in order to
2227 * absorb the VM's CPU jitter.
2229 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2235 /* interrupt (call) handling */
2236 if ((rxvq->callfd_idx != ~0) && !(rxvq->avail->flags & 1))
2238 rxvq->n_since_last_int += frame->n_vectors - n_left;
2240 if (rxvq->n_since_last_int > vum->coalesce_frames)
2241 vhost_user_send_call (vm, rxvq);
2244 vhost_user_vring_unlock (vui, qid);
2247 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2249 vlib_error_count (vm, node->node_index, error, n_left);
2250 vlib_increment_simple_counter
2251 (vnet_main.interface_main.sw_if_counters
2252 + VNET_INTERFACE_COUNTER_DROP,
2253 os_get_cpu_number (), vui->sw_if_index, n_left);
2256 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2257 return frame->n_vectors;
2260 static clib_error_t *
2261 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
2264 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2265 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
2266 vhost_user_main_t *vum = &vhost_user_main;
2267 vhost_user_intf_t *vui =
2268 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
2270 vui->admin_up = is_up;
2273 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
2274 VNET_HW_INTERFACE_FLAG_LINK_UP);
2276 return /* no error */ 0;
2280 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2281 .name = "vhost-user",
2282 .tx_function = vhost_user_tx,
2283 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2284 .tx_function_error_strings = vhost_user_tx_func_error_strings,
2285 .format_device_name = format_vhost_user_interface_name,
2286 .name_renumber = vhost_user_name_renumber,
2287 .admin_up_down_function = vhost_user_interface_admin_up_down,
2288 .format_tx_trace = format_vhost_trace,
2291 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
2296 vhost_user_process (vlib_main_t * vm,
2297 vlib_node_runtime_t * rt, vlib_frame_t * f)
2299 vhost_user_main_t *vum = &vhost_user_main;
2300 vhost_user_intf_t *vui;
2301 struct sockaddr_un sun;
2303 unix_file_t template = { 0 };
2304 f64 timeout = 3153600000.0 /* 100 years */ ;
2305 uword *event_data = 0;
2307 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
2308 sun.sun_family = AF_UNIX;
2309 template.read_function = vhost_user_socket_read;
2310 template.error_function = vhost_user_socket_error;
2317 vlib_process_wait_for_event_or_clock (vm, timeout);
2318 vlib_process_get_events (vm, &event_data);
2319 vec_reset_length (event_data);
2324 pool_foreach (vui, vum->vhost_user_interfaces, {
2326 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
2327 if (vui->unix_file_index == ~0)
2329 /* try to connect */
2330 strncpy (sun.sun_path, (char *) vui->sock_filename,
2331 sizeof (sun.sun_path) - 1);
2333 /* Avoid hanging VPP if the other end does not accept */
2334 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2335 clib_unix_warning ("fcntl");
2337 if (connect (sockfd, (struct sockaddr *) &sun,
2338 sizeof (struct sockaddr_un)) == 0)
2340 /* Set the socket to blocking as it was before */
2341 if (fcntl(sockfd, F_SETFL, 0) < 0)
2342 clib_unix_warning ("fcntl2");
2344 vui->sock_errno = 0;
2345 template.file_descriptor = sockfd;
2346 template.private_data =
2347 vui - vhost_user_main.vhost_user_interfaces;
2348 vui->unix_file_index = unix_file_add (&unix_main, &template);
2350 //Re-open for next connect
2351 if ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
2352 clib_warning("Critical: Could not open unix socket");
2358 vui->sock_errno = errno;
2363 /* check if socket is alive */
2365 socklen_t len = sizeof (error);
2366 int fd = UNIX_GET_FD(vui->unix_file_index);
2368 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
2372 DBG_SOCK ("getsockopt returned %d", retval);
2373 vhost_user_if_disconnect (vui);
2384 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
2385 .function = vhost_user_process,
2386 .type = VLIB_NODE_TYPE_PROCESS,
2387 .name = "vhost-user-process",
2392 * Disables and reset interface structure.
2393 * It can then be either init again, or removed from used interfaces.
2396 vhost_user_term_if (vhost_user_intf_t * vui)
2400 // Delete configured thread pinning
2401 vec_reset_length (vui->workers);
2402 // disconnect interface sockets
2403 vhost_user_if_disconnect (vui);
2404 vhost_user_update_iface_state (vui);
2406 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2408 clib_mem_free ((void *) vui->vring_locks[q]);
2411 if (vui->unix_server_index != ~0)
2413 //Close server socket
2414 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
2415 vui->unix_server_index);
2416 unix_file_del (&unix_main, uf);
2417 vui->unix_server_index = ~0;
2422 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
2424 vhost_user_main_t *vum = &vhost_user_main;
2425 vhost_user_intf_t *vui;
2427 vnet_hw_interface_t *hwif;
2429 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2430 hwif->dev_class_index != vhost_user_dev_class.index)
2431 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2433 DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2434 hwif->name, hwif->dev_instance);
2436 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2438 // Disable and reset interface
2439 vhost_user_term_if (vui);
2441 // Reset renumbered iface
2442 if (hwif->dev_instance <
2443 vec_len (vum->show_dev_instance_by_real_dev_instance))
2444 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
2446 // Delete ethernet interface
2447 ethernet_delete_interface (vnm, vui->hw_if_index);
2450 pool_put (vum->vhost_user_interfaces, vui);
2456 * Open server unix socket on specified sock_filename.
2459 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
2462 struct sockaddr_un un = { };
2464 /* create listening socket */
2465 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2466 return VNET_API_ERROR_SYSCALL_ERROR_1;
2468 un.sun_family = AF_UNIX;
2469 strncpy ((char *) un.sun_path, (char *) sock_filename,
2470 sizeof (un.sun_path) - 1);
2472 /* remove if exists */
2473 unlink ((char *) sock_filename);
2475 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2477 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2481 if (listen (fd, 1) == -1)
2483 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2496 * Create ethernet interface for vhost user interface.
2499 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
2500 vhost_user_intf_t * vui, u8 * hwaddress)
2502 vhost_user_main_t *vum = &vhost_user_main;
2504 clib_error_t *error;
2506 /* create hw and sw interface */
2509 clib_memcpy (hwaddr, hwaddress, 6);
2513 random_u32 (&vum->random);
2514 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
2519 error = ethernet_register_interface
2521 vhost_user_dev_class.index,
2522 vui - vum->vhost_user_interfaces /* device instance */ ,
2523 hwaddr /* ethernet address */ ,
2524 &vui->hw_if_index, 0 /* flag change */ );
2527 clib_error_report (error);
2529 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
2530 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
2534 * Initialize vui with specified attributes
2537 vhost_user_vui_init (vnet_main_t * vnm,
2538 vhost_user_intf_t * vui,
2540 const char *sock_filename,
2541 u64 feature_mask, u32 * sw_if_index)
2543 vnet_sw_interface_t *sw;
2544 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
2547 if (server_sock_fd != -1)
2549 unix_file_t template = { 0 };
2550 template.read_function = vhost_user_socksvr_accept_ready;
2551 template.file_descriptor = server_sock_fd;
2552 template.private_data = vui - vhost_user_main.vhost_user_interfaces; //hw index
2553 vui->unix_server_index = unix_file_add (&unix_main, &template);
2557 vui->unix_server_index = ~0;
2560 vui->sw_if_index = sw->sw_if_index;
2561 strncpy (vui->sock_filename, sock_filename,
2562 ARRAY_LEN (vui->sock_filename) - 1);
2563 vui->sock_errno = 0;
2565 vui->feature_mask = feature_mask;
2566 vui->unix_file_index = ~0;
2567 vui->log_base_addr = 0;
2569 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2570 vhost_user_vring_init (vui, q);
2572 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
2575 *sw_if_index = vui->sw_if_index;
2577 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2579 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
2580 CLIB_CACHE_LINE_BYTES);
2581 memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
2584 vec_validate (vui->per_cpu_tx_qid,
2585 vlib_get_thread_main ()->n_vlib_mains - 1);
2586 vhost_user_tx_thread_placement (vui);
2590 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2591 const char *sock_filename,
2595 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
2597 vhost_user_intf_t *vui = NULL;
2600 int server_sock_fd = -1;
2602 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2604 return VNET_API_ERROR_INVALID_ARGUMENT;
2610 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
2616 pool_get (vhost_user_main.vhost_user_interfaces, vui);
2618 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
2619 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
2620 feature_mask, &sw_if_idx);
2623 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2626 *sw_if_index = sw_if_idx;
2628 // Process node must connect
2629 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2634 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2635 const char *sock_filename,
2638 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
2640 vhost_user_main_t *vum = &vhost_user_main;
2641 vhost_user_intf_t *vui = NULL;
2643 int server_sock_fd = -1;
2645 vnet_hw_interface_t *hwif;
2647 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2648 hwif->dev_class_index != vhost_user_dev_class.index)
2649 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2651 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2653 // First try to open server socket
2655 if ((rv = vhost_user_init_server_sock (sock_filename,
2656 &server_sock_fd)) != 0)
2659 vhost_user_term_if (vui);
2660 vhost_user_vui_init (vnm, vui, server_sock_fd,
2661 sock_filename, feature_mask, &sw_if_idx);
2664 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2666 // Process node must connect
2667 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2672 vhost_user_connect_command_fn (vlib_main_t * vm,
2673 unformat_input_t * input,
2674 vlib_cli_command_t * cmd)
2676 unformat_input_t _line_input, *line_input = &_line_input;
2677 u8 *sock_filename = NULL;
2680 u64 feature_mask = (u64) ~ (0ULL);
2682 u32 custom_dev_instance = ~0;
2686 /* Get a line of input. */
2687 if (!unformat_user (input, unformat_line_input, line_input))
2690 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2692 if (unformat (line_input, "socket %s", &sock_filename))
2694 else if (unformat (line_input, "server"))
2696 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2700 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
2702 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
2707 return clib_error_return (0, "unknown input `%U'",
2708 format_unformat_error, input);
2710 unformat_free (line_input);
2712 vnet_main_t *vnm = vnet_get_main ();
2715 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
2716 is_server, &sw_if_index, feature_mask,
2717 renumber, custom_dev_instance, hw)))
2719 vec_free (sock_filename);
2720 return clib_error_return (0, "vhost_user_create_if returned %d", rv);
2723 vec_free (sock_filename);
2724 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
2730 vhost_user_delete_command_fn (vlib_main_t * vm,
2731 unformat_input_t * input,
2732 vlib_cli_command_t * cmd)
2734 unformat_input_t _line_input, *line_input = &_line_input;
2735 u32 sw_if_index = ~0;
2736 vnet_main_t *vnm = vnet_get_main ();
2738 /* Get a line of input. */
2739 if (!unformat_user (input, unformat_line_input, line_input))
2742 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2744 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
2747 (line_input, "%U", unformat_vnet_sw_interface, vnm,
2750 vnet_hw_interface_t *hwif =
2751 vnet_get_sup_hw_interface (vnm, sw_if_index);
2753 vhost_user_dev_class.index != hwif->dev_class_index)
2754 return clib_error_return (0, "Not a vhost interface");
2757 return clib_error_return (0, "unknown input `%U'",
2758 format_unformat_error, input);
2760 unformat_free (line_input);
2761 vhost_user_delete_if (vnm, vm, sw_if_index);
2766 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
2767 vhost_user_intf_details_t ** out_vuids)
2770 vhost_user_main_t *vum = &vhost_user_main;
2771 vhost_user_intf_t *vui;
2772 vhost_user_intf_details_t *r_vuids = NULL;
2773 vhost_user_intf_details_t *vuid = NULL;
2774 u32 *hw_if_indices = 0;
2775 vnet_hw_interface_t *hi;
2782 pool_foreach (vui, vum->vhost_user_interfaces,
2783 vec_add1 (hw_if_indices, vui->hw_if_index);
2786 for (i = 0; i < vec_len (hw_if_indices); i++)
2788 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2789 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2791 vec_add2 (r_vuids, vuid, 1);
2792 vuid->sw_if_index = vui->sw_if_index;
2793 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
2794 vuid->features = vui->features;
2795 vuid->num_regions = vui->nregions;
2796 vuid->sock_errno = vui->sock_errno;
2797 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
2798 ARRAY_LEN (vuid->sock_filename) - 1);
2800 s = format (s, "%v%c", hi->name, 0);
2802 strncpy ((char *) vuid->if_name, (char *) s,
2803 ARRAY_LEN (vuid->if_name) - 1);
2808 vec_free (hw_if_indices);
2810 *out_vuids = r_vuids;
2816 show_vhost_user_command_fn (vlib_main_t * vm,
2817 unformat_input_t * input,
2818 vlib_cli_command_t * cmd)
2820 clib_error_t *error = 0;
2821 vnet_main_t *vnm = vnet_get_main ();
2822 vhost_user_main_t *vum = &vhost_user_main;
2823 vhost_user_intf_t *vui;
2824 u32 hw_if_index, *hw_if_indices = 0;
2825 vnet_hw_interface_t *hi;
2827 vhost_iface_and_queue_t *vhiq;
2837 struct feat_struct *feat_entry;
2839 static struct feat_struct feat_array[] = {
2840 #define _(s,b) { .str = #s, .bit = b, },
2841 foreach_virtio_net_feature
2846 #define foreach_protocol_feature \
2847 _(VHOST_USER_PROTOCOL_F_MQ) \
2848 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2850 static struct feat_struct proto_feat_array[] = {
2851 #define _(s) { .str = #s, .bit = s},
2852 foreach_protocol_feature
2857 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2860 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2862 vec_add1 (hw_if_indices, hw_if_index);
2864 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2868 error = clib_error_return (0, "unknown input `%U'",
2869 format_unformat_error, input);
2873 if (vec_len (hw_if_indices) == 0)
2875 pool_foreach (vui, vum->vhost_user_interfaces,
2876 vec_add1 (hw_if_indices, vui->hw_if_index);
2879 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2880 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2881 vum->coalesce_frames, vum->coalesce_time);
2883 for (i = 0; i < vec_len (hw_if_indices); i++)
2885 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2886 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2887 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
2888 hi->name, hw_if_indices[i]);
2890 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2891 " features mask (0x%llx): \n"
2892 " features (0x%llx): \n",
2893 vui->virtio_net_hdr_sz, vui->feature_mask,
2896 feat_entry = (struct feat_struct *) &feat_array;
2897 while (feat_entry->str)
2899 if (vui->features & (1ULL << feat_entry->bit))
2900 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2905 vlib_cli_output (vm, " protocol features (0x%llx)",
2906 vui->protocol_features);
2907 feat_entry = (struct feat_struct *) &proto_feat_array;
2908 while (feat_entry->str)
2910 if (vui->protocol_features & (1ULL << feat_entry->bit))
2911 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2916 vlib_cli_output (vm, "\n");
2918 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2920 (vui->unix_server_index != ~0) ? "server" : "client",
2921 strerror (vui->sock_errno));
2923 vlib_cli_output (vm, " rx placement: ");
2924 vec_foreach (vhc, vum->cpus)
2926 vec_foreach (vhiq, vhc->rx_queues)
2928 if (vhiq->vhost_iface_index == vui - vum->vhost_user_interfaces)
2929 vlib_cli_output (vm, " thread %d on vring %d\n",
2930 vhc - vum->cpus, VHOST_VRING_IDX_TX (vhiq->qid));
2934 vlib_cli_output (vm, " tx placement: %s\n",
2935 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2937 vec_foreach_index (ci, vui->per_cpu_tx_qid)
2939 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2940 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2943 vlib_cli_output (vm, "\n");
2945 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2949 vlib_cli_output (vm,
2950 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2951 vlib_cli_output (vm,
2952 " ====== ===== ================== ================== ================== ================== ==================\n");
2954 for (j = 0; j < vui->nregions; j++)
2956 vlib_cli_output (vm,
2957 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2958 j, vui->region_mmap_fd[j],
2959 vui->regions[j].guest_phys_addr,
2960 vui->regions[j].memory_size,
2961 vui->regions[j].userspace_addr,
2962 vui->regions[j].mmap_offset,
2963 pointer_to_uword (vui->region_mmap_addr[j]));
2965 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2967 if (!vui->vrings[q].started)
2970 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2971 (q & 1) ? "RX" : "TX",
2972 vui->vrings[q].enabled ? "" : " disabled");
2974 vlib_cli_output (vm,
2975 " qsz %d last_avail_idx %d last_used_idx %d\n",
2976 vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
2977 vui->vrings[q].last_used_idx);
2979 if (vui->vrings[q].avail && vui->vrings[q].used)
2980 vlib_cli_output (vm,
2981 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
2982 vui->vrings[q].avail->flags,
2983 vui->vrings[q].avail->idx,
2984 vui->vrings[q].used->flags,
2985 vui->vrings[q].used->idx);
2987 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
2988 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
2989 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
2990 kickfd, callfd, vui->vrings[q].errfd);
2994 vlib_cli_output (vm, "\n descriptor table:\n");
2995 vlib_cli_output (vm,
2996 " id addr len flags next user_addr\n");
2997 vlib_cli_output (vm,
2998 " ===== ================== ===== ====== ===== ==================\n");
2999 for (j = 0; j < vui->vrings[q].qsz; j++)
3002 vlib_cli_output (vm,
3003 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3004 j, vui->vrings[q].desc[j].addr,
3005 vui->vrings[q].desc[j].len,
3006 vui->vrings[q].desc[j].flags,
3007 vui->vrings[q].desc[j].next,
3008 pointer_to_uword (map_guest_mem
3010 vui->vrings[q].desc[j].
3015 vlib_cli_output (vm, "\n");
3018 vec_free (hw_if_indices);
3027 * Create a vHost User interface. Once created, a new virtual interface
3028 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3029 * is the next free index.
3031 * There are several parameters associated with a vHost interface:
3033 * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3034 * VPP to manage the vHost interface. If socket does not already exist, VPP will
3035 * create the socket.
3037 * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3038 * linux socket. If not provided, VPP will be the client.
3040 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3041 * startup. By default, all supported features will be advertised. Otherwise,
3042 * provide the set of features desired.
3043 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3044 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3045 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3046 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3047 * - 0x004000000 (26) - VHOST_F_LOG_ALL
3048 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3049 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3050 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3051 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3053 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3054 * X:X:X:X:X:X unix or X.X.X cisco format.
3056 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3057 * in the name to be specified. If instance already exists, name will be used
3058 * anyway and multiple instances will have the same name. Use with caution.
3061 * Example of how to create a vhost interface with VPP as the client and all features enabled:
3062 * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3063 * VirtualEthernet0/0/0
3065 * Example of how to create a vhost interface with VPP as the server and with just
3066 * multiple queues enabled:
3067 * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3068 * VirtualEthernet0/0/1
3070 * Once the vHost interface is created, enable the interface using:
3071 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3074 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3075 .path = "create vhost-user",
3076 .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>]",
3077 .function = vhost_user_connect_command_fn,
3082 * Delete a vHost User interface using the interface name or the
3083 * software interface index. Use the '<em>show interfaces</em>'
3084 * command to determine the software interface index. On deletion,
3085 * the linux socket will not be deleted.
3088 * Example of how to delete a vhost interface by name:
3089 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3090 * Example of how to delete a vhost interface by software interface index:
3091 * @cliexcmd{delete vhost-user sw_if_index 1}
3094 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3095 .path = "delete vhost-user",
3096 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
3097 .function = vhost_user_delete_command_fn,
3101 * Display the attributes of a single vHost User interface (provide interface
3102 * name), multiple vHost User interfaces (provide a list of interface names seperated
3103 * by spaces) or all Vhost User interfaces (omit an interface name to display all
3104 * vHost interfaces).
3108 * Example of how to display a vhost interface:
3109 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3110 * Virtio vhost-user interfaces
3112 * coalesce frames 32 time 1e-3
3113 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3114 * virtio_net_hdr_sz 12
3115 * features mask (0xffffffffffffffff):
3116 * features (0x50408000):
3117 * VIRTIO_NET_F_MRG_RXBUF (15)
3118 * VIRTIO_NET_F_MQ (22)
3119 * VIRTIO_F_INDIRECT_DESC (28)
3120 * VHOST_USER_F_PROTOCOL_FEATURES (30)
3121 * protocol features (0x3)
3122 * VHOST_USER_PROTOCOL_F_MQ (0)
3123 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3125 * socket filename /tmp/vhost1.sock type client errno "Success"
3128 * thread 1 on vring 1
3129 * thread 1 on vring 5
3130 * thread 2 on vring 3
3131 * thread 2 on vring 7
3132 * tx placement: spin-lock
3133 * thread 0 on vring 0
3134 * thread 1 on vring 2
3135 * thread 2 on vring 0
3137 * Memory regions (total 2)
3138 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3139 * ====== ===== ================== ================== ================== ================== ==================
3140 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3141 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3144 * qsz 256 last_avail_idx 0 last_used_idx 0
3145 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3146 * kickfd 62 callfd 64 errfd -1
3149 * qsz 256 last_avail_idx 0 last_used_idx 0
3150 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3151 * kickfd 65 callfd 66 errfd -1
3154 * qsz 256 last_avail_idx 0 last_used_idx 0
3155 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3156 * kickfd 63 callfd 70 errfd -1
3159 * qsz 256 last_avail_idx 0 last_used_idx 0
3160 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3161 * kickfd 72 callfd 74 errfd -1
3163 * Virtqueue 4 (TX disabled)
3164 * qsz 256 last_avail_idx 0 last_used_idx 0
3165 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3166 * kickfd 76 callfd 78 errfd -1
3168 * Virtqueue 5 (RX disabled)
3169 * qsz 256 last_avail_idx 0 last_used_idx 0
3170 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3171 * kickfd 80 callfd 82 errfd -1
3173 * Virtqueue 6 (TX disabled)
3174 * qsz 256 last_avail_idx 0 last_used_idx 0
3175 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3176 * kickfd 84 callfd 86 errfd -1
3178 * Virtqueue 7 (RX disabled)
3179 * qsz 256 last_avail_idx 0 last_used_idx 0
3180 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3181 * kickfd 88 callfd 90 errfd -1
3185 * The optional '<em>descriptors</em>' parameter will display the same output as
3186 * the previous example but will include the descriptor table for each queue.
3187 * The output is truncated below:
3188 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3189 * Virtio vhost-user interfaces
3191 * coalesce frames 32 time 1e-3
3192 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3193 * virtio_net_hdr_sz 12
3194 * features mask (0xffffffffffffffff):
3195 * features (0x50408000):
3196 * VIRTIO_NET_F_MRG_RXBUF (15)
3197 * VIRTIO_NET_F_MQ (22)
3200 * qsz 256 last_avail_idx 0 last_used_idx 0
3201 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3202 * kickfd 62 callfd 64 errfd -1
3205 * id addr len flags next user_addr
3206 * ===== ================== ===== ====== ===== ==================
3207 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3208 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3209 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3210 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3211 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3212 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3213 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3214 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3215 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3216 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3217 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3219 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3220 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3221 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3222 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3223 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3224 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3225 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3228 * qsz 256 last_avail_idx 0 last_used_idx 0
3234 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3235 .path = "show vhost-user",
3236 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
3237 .function = show_vhost_user_command_fn,
3241 static clib_error_t *
3242 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
3244 vhost_user_main_t *vum = &vhost_user_main;
3246 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3248 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
3250 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
3252 else if (unformat (input, "dont-dump-memory"))
3253 vum->dont_dump_vhost_user_memory = 1;
3255 return clib_error_return (0, "unknown input `%U'",
3256 format_unformat_error, input);
3262 /* vhost-user { ... } configuration. */
3263 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3266 vhost_user_unmap_all (void)
3268 vhost_user_main_t *vum = &vhost_user_main;
3269 vhost_user_intf_t *vui;
3271 if (vum->dont_dump_vhost_user_memory)
3273 pool_foreach (vui, vum->vhost_user_interfaces,
3274 unmap_all_mem_regions (vui);
3279 static clib_error_t *
3280 vhost_thread_command_fn (vlib_main_t * vm,
3281 unformat_input_t * input, vlib_cli_command_t * cmd)
3283 unformat_input_t _line_input, *line_input = &_line_input;
3284 u32 worker_thread_index;
3289 /* Get a line of input. */
3290 if (!unformat_user (input, unformat_line_input, line_input))
3294 (line_input, "%U %d", unformat_vnet_sw_interface, vnet_get_main (),
3295 &sw_if_index, &worker_thread_index))
3297 unformat_free (line_input);
3298 return clib_error_return (0, "unknown input `%U'",
3299 format_unformat_error, input);
3302 if (unformat (line_input, "del"))
3306 vhost_user_thread_placement (sw_if_index, worker_thread_index, del)))
3307 return clib_error_return (0, "vhost_user_thread_placement returned %d",
3314 * This command is used to move the RX processing for the given
3315 * interfaces to the provided thread. If the '<em>del</em>' option is used,
3316 * the forced thread assignment is removed and the thread assigment is
3317 * reassigned automatically. Use '<em>show vhost-user <interface></em>'
3318 * to see the thread assignment.
3321 * Example of how to move the RX processing for a given interface to a given thread:
3322 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1}
3323 * Example of how to remove the forced thread assignment for a given interface:
3324 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1 del}
3327 VLIB_CLI_COMMAND (vhost_user_thread_command, static) = {
3328 .path = "vhost thread",
3329 .short_help = "vhost thread <iface> <worker-index> [del]",
3330 .function = vhost_thread_command_fn,
3335 * fd.io coding-style-patch-verification: ON
3338 * eval: (c-set-style "gnu")