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>
40 #include <vnet/devices/virtio/vhost-user.h>
42 #define VHOST_USER_DEBUG_SOCKET 0
43 #define VHOST_USER_DEBUG_VQ 0
45 /* Set to get virtio_net_hdr in buffer pre-data
46 details will be shown in packet trace */
47 #define VHOST_USER_COPY_TX_HDR 0
49 #if VHOST_USER_DEBUG_SOCKET == 1
50 #define DBG_SOCK(args...) clib_warning(args);
52 #define DBG_SOCK(args...)
55 #if VHOST_USER_DEBUG_VQ == 1
56 #define DBG_VQ(args...) clib_warning(args);
58 #define DBG_VQ(args...)
61 vlib_node_registration_t vhost_user_input_node;
63 #define foreach_vhost_user_tx_func_error \
65 _(NOT_READY, "vhost user state error") \
66 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
67 _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
68 _(MMAP_FAIL, "mmap failure") \
69 _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
73 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
74 foreach_vhost_user_tx_func_error
76 VHOST_USER_TX_FUNC_N_ERROR,
77 } vhost_user_tx_func_error_t;
79 static char *vhost_user_tx_func_error_strings[] = {
81 foreach_vhost_user_tx_func_error
85 #define foreach_vhost_user_input_func_error \
86 _(NO_ERROR, "no error") \
87 _(NO_BUFFER, "no available buffer") \
88 _(MMAP_FAIL, "mmap failure") \
89 _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
90 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
91 _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
95 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
96 foreach_vhost_user_input_func_error
98 VHOST_USER_INPUT_FUNC_N_ERROR,
99 } vhost_user_input_func_error_t;
101 static char *vhost_user_input_func_error_strings[] = {
103 foreach_vhost_user_input_func_error
108 static vhost_user_main_t vhost_user_main = {
112 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
113 .name = "vhost-user",
118 format_vhost_user_interface_name (u8 * s, va_list * args)
120 u32 i = va_arg (*args, u32);
121 u32 show_dev_instance = ~0;
122 vhost_user_main_t *vum = &vhost_user_main;
124 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
125 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
127 if (show_dev_instance != ~0)
128 i = show_dev_instance;
130 s = format (s, "VirtualEthernet0/0/%d", i);
135 vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
137 vhost_user_main_t *vum = &vhost_user_main;
139 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
140 hi->dev_instance, ~0);
142 vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
145 DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
146 hi->dev_instance, new_dev_instance);
151 static_always_inline void *
152 map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint)
155 if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) &&
156 ((vui->regions[i].guest_phys_addr +
157 vui->regions[i].memory_size) > addr)))
159 return (void *) (vui->region_mmap_addr[i] + addr -
160 vui->regions[i].guest_phys_addr);
163 __m128i rl, rh, al, ah, r;
164 al = _mm_set1_epi64x (addr + 1);
165 ah = _mm_set1_epi64x (addr);
167 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]);
168 rl = _mm_cmpgt_epi64 (al, rl);
169 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]);
170 rh = _mm_cmpgt_epi64 (rh, ah);
171 r = _mm_and_si128 (rl, rh);
173 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]);
174 rl = _mm_cmpgt_epi64 (al, rl);
175 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]);
176 rh = _mm_cmpgt_epi64 (rh, ah);
177 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22);
179 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]);
180 rl = _mm_cmpgt_epi64 (al, rl);
181 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]);
182 rh = _mm_cmpgt_epi64 (rh, ah);
183 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44);
185 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]);
186 rl = _mm_cmpgt_epi64 (al, rl);
187 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]);
188 rh = _mm_cmpgt_epi64 (rh, ah);
189 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
191 r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
192 i = __builtin_ctzll (_mm_movemask_epi8 (r));
194 if (i < vui->nregions)
197 return (void *) (vui->region_mmap_addr[i] + addr -
198 vui->regions[i].guest_phys_addr);
202 for (i = 0; i < vui->nregions; i++)
204 if ((vui->regions[i].guest_phys_addr <= addr) &&
205 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
209 return (void *) (vui->region_mmap_addr[i] + addr -
210 vui->regions[i].guest_phys_addr);
214 DBG_VQ ("failed to map guest mem addr %llx", addr);
220 map_user_mem (vhost_user_intf_t * vui, uword addr)
223 for (i = 0; i < vui->nregions; i++)
225 if ((vui->regions[i].userspace_addr <= addr) &&
226 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
229 return (void *) (vui->region_mmap_addr[i] + addr -
230 vui->regions[i].userspace_addr);
237 get_huge_page_size (int fd)
245 unmap_all_mem_regions (vhost_user_intf_t * vui)
248 for (i = 0; i < vui->nregions; i++)
250 if (vui->region_mmap_addr[i] != (void *) -1)
253 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
255 ssize_t map_sz = (vui->regions[i].memory_size +
256 vui->regions[i].mmap_offset +
257 page_sz) & ~(page_sz - 1);
260 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
264 ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
265 vui->region_mmap_addr[i], map_sz, page_sz);
267 vui->region_mmap_addr[i] = (void *) -1;
271 clib_warning ("failed to unmap memory region (errno %d)",
274 close (vui->region_mmap_fd[i]);
281 static clib_error_t *
282 vhost_user_callfd_read_ready (unix_file_t * uf)
284 __attribute__ ((unused)) int n;
286 n = read (uf->file_descriptor, ((char *) &buff), 8);
291 vhost_user_if_disconnect (vhost_user_intf_t * vui)
293 vhost_user_main_t *vum = &vhost_user_main;
294 vnet_main_t *vnm = vnet_get_main ();
297 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
299 if (vui->unix_file_index != ~0)
301 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
302 vui->unix_file_index = ~0;
305 close (vui->unix_fd);
307 hash_unset (vum->vhost_user_interface_index_by_sock_fd, vui->unix_fd);
308 hash_unset (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd);
311 for (q = 0; q < vui->num_vrings; q++)
313 if (vui->vrings[q].callfd > -1)
315 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
316 vui->vrings[q].callfd_idx);
317 unix_file_del (&unix_main, uf);
320 if (vui->vrings[q].kickfd > -1)
321 close (vui->vrings[q].kickfd);
323 vui->vrings[q].callfd = -1;
324 vui->vrings[q].kickfd = -1;
325 vui->vrings[q].desc = NULL;
326 vui->vrings[q].avail = NULL;
327 vui->vrings[q].used = NULL;
328 vui->vrings[q].log_guest_addr = 0;
329 vui->vrings[q].log_used = 0;
332 unmap_all_mem_regions (vui);
333 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
336 #define VHOST_LOG_PAGE 0x1000
338 vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
340 if (PREDICT_TRUE (vui->log_base_addr == 0
341 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
345 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
347 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
351 CLIB_MEMORY_BARRIER ();
352 u64 page = addr / VHOST_LOG_PAGE;
353 while (page * VHOST_LOG_PAGE < addr + len)
355 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
360 #define vhost_user_log_dirty_ring(vui, vq, member) \
361 if (PREDICT_FALSE(vq->log_used)) { \
362 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
363 sizeof(vq->used->member)); \
366 static clib_error_t *
367 vhost_user_socket_read (unix_file_t * uf)
370 int fd, number_of_fds = 0;
371 int fds[VHOST_MEMORY_MAX_NREGIONS];
372 vhost_user_msg_t msg;
375 vhost_user_main_t *vum = &vhost_user_main;
376 vhost_user_intf_t *vui;
377 struct cmsghdr *cmsg;
380 unix_file_t template = { 0 };
381 vnet_main_t *vnm = vnet_get_main ();
383 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
384 uf->file_descriptor);
387 DBG_SOCK ("FD %d doesn't belong to any interface", uf->file_descriptor);
391 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
393 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
395 memset (&mh, 0, sizeof (mh));
396 memset (control, 0, sizeof (control));
398 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
401 /* set the payload */
402 iov[0].iov_base = (void *) &msg;
403 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
407 mh.msg_control = control;
408 mh.msg_controllen = sizeof (control);
410 n = recvmsg (uf->file_descriptor, &mh, 0);
412 if (n != VHOST_USER_MSG_HDR_SZ)
415 if (mh.msg_flags & MSG_CTRUNC)
420 cmsg = CMSG_FIRSTHDR (&mh);
422 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
423 (cmsg->cmsg_type == SCM_RIGHTS) &&
424 (cmsg->cmsg_len - CMSG_LEN (0) <=
425 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
427 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
428 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
431 /* version 1, no reply bit set */
432 if ((msg.flags & 7) != 1)
434 DBG_SOCK ("malformed message received. closing socket");
439 int rv __attribute__ ((unused));
440 /* $$$$ pay attention to rv */
441 rv = read (uf->file_descriptor, ((char *) &msg) + n, msg.size);
446 case VHOST_USER_GET_FEATURES:
447 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES", vui->hw_if_index);
450 msg.u64 = (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
451 (1 << FEAT_VIRTIO_F_ANY_LAYOUT) |
452 (1 << FEAT_VIRTIO_F_INDIRECT_DESC) |
453 (1 << FEAT_VHOST_F_LOG_ALL) |
454 (1 << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
455 (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
456 (1UL << FEAT_VIRTIO_F_VERSION_1);
457 msg.u64 &= vui->feature_mask;
459 msg.size = sizeof (msg.u64);
462 case VHOST_USER_SET_FEATURES:
463 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
464 vui->hw_if_index, msg.u64);
466 vui->features = msg.u64;
468 if (vui->features & (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF))
469 vui->virtio_net_hdr_sz = 12;
471 vui->virtio_net_hdr_sz = 10;
474 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
476 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
477 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
480 for (q = 0; q < 2; q++)
482 vui->vrings[q].desc = 0;
483 vui->vrings[q].avail = 0;
484 vui->vrings[q].used = 0;
485 vui->vrings[q].log_guest_addr = 0;
486 vui->vrings[q].log_used = 0;
489 DBG_SOCK ("interface %d disconnected", vui->sw_if_index);
493 case VHOST_USER_SET_MEM_TABLE:
494 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
495 vui->hw_if_index, msg.memory.nregions);
497 if ((msg.memory.nregions < 1) ||
498 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
501 DBG_SOCK ("number of mem regions must be between 1 and %i",
502 VHOST_MEMORY_MAX_NREGIONS);
507 if (msg.memory.nregions != number_of_fds)
509 DBG_SOCK ("each memory region must have FD");
512 unmap_all_mem_regions (vui);
513 for (i = 0; i < msg.memory.nregions; i++)
515 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
516 sizeof (vhost_user_memory_region_t));
518 long page_sz = get_huge_page_size (fds[i]);
520 /* align size to 2M page */
521 ssize_t map_sz = (vui->regions[i].memory_size +
522 vui->regions[i].mmap_offset +
523 page_sz) & ~(page_sz - 1);
525 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
526 MAP_SHARED, fds[i], 0);
527 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
528 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
529 vui->regions[i].memory_size;
532 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
533 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
536 if (vui->region_mmap_addr[i] == MAP_FAILED)
538 clib_warning ("failed to map memory. errno is %d", errno);
541 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
542 vui->region_mmap_fd[i] = fds[i];
544 vui->nregions = msg.memory.nregions;
547 case VHOST_USER_SET_VRING_NUM:
548 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
549 vui->hw_if_index, msg.state.index, msg.state.num);
551 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
552 (msg.state.num == 0) || /* it cannot be zero */
553 (msg.state.num % 2)) /* must be power of 2 */
555 vui->vrings[msg.state.index].qsz = msg.state.num;
558 case VHOST_USER_SET_VRING_ADDR:
559 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
560 vui->hw_if_index, msg.state.index);
562 vui->vrings[msg.state.index].desc = (vring_desc_t *)
563 map_user_mem (vui, msg.addr.desc_user_addr);
564 vui->vrings[msg.state.index].used = (vring_used_t *)
565 map_user_mem (vui, msg.addr.used_user_addr);
566 vui->vrings[msg.state.index].avail = (vring_avail_t *)
567 map_user_mem (vui, msg.addr.avail_user_addr);
569 if ((vui->vrings[msg.state.index].desc == NULL) ||
570 (vui->vrings[msg.state.index].used == NULL) ||
571 (vui->vrings[msg.state.index].avail == NULL))
573 DBG_SOCK ("failed to map user memory for hw_if_index %d",
578 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
579 vui->vrings[msg.state.index].log_used =
580 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
582 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
583 the ring is initialized in an enabled state. */
585 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
587 vui->vrings[msg.state.index].enabled = 1;
590 vui->vrings[msg.state.index].last_used_idx =
591 vui->vrings[msg.state.index].used->idx;
593 /* tell driver that we don't want interrupts */
594 vui->vrings[msg.state.index].used->flags |= 1;
597 case VHOST_USER_SET_OWNER:
598 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
601 case VHOST_USER_RESET_OWNER:
602 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
605 case VHOST_USER_SET_VRING_CALL:
606 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
607 vui->hw_if_index, msg.u64);
609 q = (u8) (msg.u64 & 0xFF);
611 if (!(msg.u64 & 0x100))
613 if (number_of_fds != 1)
616 /* if there is old fd, delete it */
617 if (vui->vrings[q].callfd > -1)
619 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
620 vui->vrings[q].callfd_idx);
621 unix_file_del (&unix_main, uf);
623 vui->vrings[q].callfd = fds[0];
624 template.read_function = vhost_user_callfd_read_ready;
625 template.file_descriptor = fds[0];
626 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
629 vui->vrings[q].callfd = -1;
632 case VHOST_USER_SET_VRING_KICK:
633 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
634 vui->hw_if_index, msg.u64);
636 q = (u8) (msg.u64 & 0xFF);
638 if (!(msg.u64 & 0x100))
640 if (number_of_fds != 1)
643 if (vui->vrings[q].kickfd > -1)
644 close (vui->vrings[q].kickfd);
646 vui->vrings[q].kickfd = fds[0];
649 vui->vrings[q].kickfd = -1;
652 case VHOST_USER_SET_VRING_ERR:
653 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
654 vui->hw_if_index, msg.u64);
656 q = (u8) (msg.u64 & 0xFF);
658 if (!(msg.u64 & 0x100))
660 if (number_of_fds != 1)
668 vui->vrings[q].errfd = fd;
671 case VHOST_USER_SET_VRING_BASE:
672 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
673 vui->hw_if_index, msg.state.index, msg.state.num);
675 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
678 case VHOST_USER_GET_VRING_BASE:
679 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
680 vui->hw_if_index, msg.state.index, msg.state.num);
682 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
683 vui->vrings[msg.state.index].enabled = 0;
685 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
687 msg.size = sizeof (msg.state);
690 case VHOST_USER_NONE:
691 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
695 case VHOST_USER_SET_LOG_BASE:
697 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
699 if (msg.size != sizeof (msg.log))
702 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
703 msg.size, sizeof (msg.log));
708 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
711 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
716 /* align size to 2M page */
717 long page_sz = get_huge_page_size (fd);
719 (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
721 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
725 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
726 map_sz, msg.log.offset, fd, vui->log_base_addr);
728 if (vui->log_base_addr == MAP_FAILED)
730 clib_warning ("failed to map memory. errno is %d", errno);
734 vui->log_base_addr += msg.log.offset;
735 vui->log_size = msg.log.size;
738 msg.size = sizeof (msg.u64);
743 case VHOST_USER_SET_LOG_FD:
744 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
748 case VHOST_USER_GET_PROTOCOL_FEATURES:
749 DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
753 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD);
754 msg.size = sizeof (msg.u64);
757 case VHOST_USER_SET_PROTOCOL_FEATURES:
758 DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
759 vui->hw_if_index, msg.u64);
761 vui->protocol_features = msg.u64;
765 case VHOST_USER_SET_VRING_ENABLE:
766 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE, enable: %d",
767 vui->hw_if_index, msg.state.num);
768 vui->vrings[msg.state.index].enabled = msg.state.num;
772 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
777 /* if we have pointers to descriptor table, go up */
779 vui->vrings[VHOST_NET_VRING_IDX_TX].desc &&
780 vui->vrings[VHOST_NET_VRING_IDX_RX].desc)
783 DBG_SOCK ("interface %d connected", vui->sw_if_index);
785 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
786 VNET_HW_INTERFACE_FLAG_LINK_UP);
791 /* if we need to reply */
795 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
796 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
803 vhost_user_if_disconnect (vui);
807 static clib_error_t *
808 vhost_user_socket_error (unix_file_t * uf)
810 vhost_user_main_t *vum = &vhost_user_main;
811 vhost_user_intf_t *vui;
814 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
815 uf->file_descriptor);
818 DBG_SOCK ("fd %d doesn't belong to any interface", uf->file_descriptor);
822 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
824 vhost_user_if_disconnect (vui);
828 static clib_error_t *
829 vhost_user_socksvr_accept_ready (unix_file_t * uf)
831 int client_fd, client_len;
832 struct sockaddr_un client;
833 unix_file_t template = { 0 };
834 vhost_user_main_t *vum = &vhost_user_main;
835 vhost_user_intf_t *vui;
838 p = hash_get (vum->vhost_user_interface_index_by_listener_fd,
839 uf->file_descriptor);
842 DBG_SOCK ("fd %d doesn't belong to any interface", uf->file_descriptor);
846 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
848 client_len = sizeof (client);
849 client_fd = accept (uf->file_descriptor,
850 (struct sockaddr *) &client,
851 (socklen_t *) & client_len);
854 return clib_error_return_unix (0, "accept");
856 template.read_function = vhost_user_socket_read;
857 template.error_function = vhost_user_socket_error;
858 template.file_descriptor = client_fd;
859 vui->unix_file_index = unix_file_add (&unix_main, &template);
861 vui->client_fd = client_fd;
862 hash_set (vum->vhost_user_interface_index_by_sock_fd, vui->client_fd,
863 vui - vum->vhost_user_interfaces);
868 static clib_error_t *
869 vhost_user_init (vlib_main_t * vm)
872 vhost_user_main_t *vum = &vhost_user_main;
873 vlib_thread_main_t *tm = vlib_get_thread_main ();
874 vlib_thread_registration_t *tr;
877 error = vlib_call_init_function (vm, ip4_init);
881 vum->vhost_user_interface_index_by_listener_fd =
882 hash_create (0, sizeof (uword));
883 vum->vhost_user_interface_index_by_sock_fd =
884 hash_create (0, sizeof (uword));
885 vum->vhost_user_interface_index_by_sw_if_index =
886 hash_create (0, sizeof (uword));
887 vum->coalesce_frames = 32;
888 vum->coalesce_time = 1e-3;
890 vec_validate_aligned (vum->rx_buffers, tm->n_vlib_mains - 1,
891 CLIB_CACHE_LINE_BYTES);
893 /* find out which cpus will be used for input */
894 vum->input_cpu_first_index = 0;
895 vum->input_cpu_count = 1;
896 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
897 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
899 if (tr && tr->count > 0)
901 vum->input_cpu_first_index = tr->first_index;
902 vum->input_cpu_count = tr->count;
908 VLIB_INIT_FUNCTION (vhost_user_init);
910 static clib_error_t *
911 vhost_user_exit (vlib_main_t * vm)
917 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
921 VHOST_USER_RX_NEXT_ETHERNET_INPUT,
922 VHOST_USER_RX_NEXT_DROP,
923 VHOST_USER_RX_N_NEXT,
931 #if VHOST_USER_COPY_TX_HDR == 1
932 virtio_net_hdr_t hdr;
934 } vhost_user_input_trace_t;
937 format_vhost_user_input_trace (u8 * s, va_list * va)
939 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
940 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
941 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
942 vhost_user_main_t *vum = &vhost_user_main;
943 vhost_user_input_trace_t *t = va_arg (*va, vhost_user_input_trace_t *);
944 vhost_user_intf_t *vui = vec_elt_at_index (vum->vhost_user_interfaces,
947 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
949 #if VHOST_USER_COPY_TX_HDR == 1
950 uword indent = format_get_indent (s);
953 s = format (s, "%U virtqueue %d",
954 format_vnet_sw_interface_name, vnm, sw, t->virtqueue);
956 #if VHOST_USER_COPY_TX_HDR == 1
957 s = format (s, "\n%Uvirtio_net_hdr flags 0x%02x gso_type %u hdr_len %u",
958 format_white_space, indent,
959 t->hdr.flags, t->hdr.gso_type, t->hdr.hdr_len);
966 vhost_user_rx_trace (vlib_main_t * vm,
967 vlib_node_runtime_t * node,
968 vhost_user_intf_t * vui, i16 virtqueue)
971 vhost_user_main_t *vum = &vhost_user_main;
973 u32 next_index = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
975 n_left = vec_len (vui->d_trace_buffers);
976 b = vui->d_trace_buffers;
982 vhost_user_input_trace_t *t0;
987 b0 = vlib_get_buffer (vm, bi0);
988 vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 0);
989 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
990 t0->virtqueue = virtqueue;
991 t0->device_index = vui - vum->vhost_user_interfaces;
992 #if VHOST_USER_COPY_TX_HDR == 1
993 clib_memcpy (&t0->hdr, b0->pre_data, sizeof (virtio_net_hdr_t));
1001 vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
1003 vhost_user_main_t *vum = &vhost_user_main;
1005 int rv __attribute__ ((unused));
1006 /* $$$$ pay attention to rv */
1007 rv = write (vq->callfd, &x, sizeof (x));
1008 vq->n_since_last_int = 0;
1009 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
1014 vhost_user_if_input (vlib_main_t * vm,
1015 vhost_user_main_t * vum,
1016 vhost_user_intf_t * vui, vlib_node_runtime_t * node)
1018 vhost_user_vring_t *txvq = &vui->vrings[VHOST_NET_VRING_IDX_TX];
1019 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
1020 uword n_rx_packets = 0, n_rx_bytes = 0;
1022 u32 n_left_to_next, *to_next;
1025 uword n_trace = vlib_get_trace_count (vm, node);
1027 u32 cpu_index, rx_len, drops, flush;
1028 f64 now = vlib_time_now (vm);
1029 u32 map_guest_hint_desc = 0;
1030 u32 map_guest_hint_indirect = 0;
1031 u32 *map_guest_hint_p = &map_guest_hint_desc;
1033 vec_reset_length (vui->d_trace_buffers);
1035 /* no descriptor ptr - bail out */
1036 if (PREDICT_FALSE (!txvq->desc || !txvq->avail || !txvq->enabled))
1039 /* do we have pending intterupts ? */
1040 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1041 vhost_user_send_call (vm, txvq);
1043 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1044 vhost_user_send_call (vm, rxvq);
1046 /* only bit 0 of avail.flags is used so we don't want to deal with this
1047 interface if any other bit is set */
1048 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
1051 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1054 if (PREDICT_FALSE (n_left == 0))
1057 if (PREDICT_FALSE (n_left == txvq->qsz))
1059 //Informational error logging when VPP is not receiving packets fast enough
1060 vlib_error_count (vm, node->node_index,
1061 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1064 if (PREDICT_FALSE (!vui->admin_up))
1066 /* if intf is admin down, just drop all packets waiting in the ring */
1067 txvq->last_avail_idx = txvq->last_used_idx = txvq->avail->idx;
1068 CLIB_MEMORY_BARRIER ();
1069 txvq->used->idx = txvq->last_used_idx;
1070 vhost_user_log_dirty_ring (vui, txvq, idx);
1071 vhost_user_send_call (vm, txvq);
1075 qsz_mask = txvq->qsz - 1;
1076 cpu_index = os_get_cpu_number ();
1080 if (n_left > VLIB_FRAME_SIZE)
1081 n_left = VLIB_FRAME_SIZE;
1083 /* Allocate some buffers.
1084 * Note that buffers that are chained for jumbo
1085 * frames are allocated separately using a slower path.
1086 * The idea is to be certain to have enough buffers at least
1087 * to cycle through the descriptors without having to check for errors.
1088 * For jumbo frames, the bottleneck is memory copy anyway.
1090 if (PREDICT_FALSE (!vum->rx_buffers[cpu_index]))
1092 vec_alloc (vum->rx_buffers[cpu_index], 2 * VLIB_FRAME_SIZE);
1094 if (PREDICT_FALSE (!vum->rx_buffers[cpu_index]))
1095 flush = n_left; //Drop all input
1098 if (PREDICT_FALSE (_vec_len (vum->rx_buffers[cpu_index]) < n_left))
1100 u32 curr_len = _vec_len (vum->rx_buffers[cpu_index]);
1101 _vec_len (vum->rx_buffers[cpu_index]) +=
1102 vlib_buffer_alloc_from_free_list (vm,
1103 vum->rx_buffers[cpu_index] +
1105 2 * VLIB_FRAME_SIZE - curr_len,
1106 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1108 if (PREDICT_FALSE (n_left > _vec_len (vum->rx_buffers[cpu_index])))
1109 flush = n_left - _vec_len (vum->rx_buffers[cpu_index]);
1112 if (PREDICT_FALSE (flush))
1114 //Remove some input buffers
1117 vlib_error_count (vm, vhost_user_input_node.index,
1118 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1121 u16 desc_chain_head =
1122 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1123 txvq->last_avail_idx++;
1124 txvq->used->ring[txvq->last_used_idx & qsz_mask].id =
1126 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1127 vhost_user_log_dirty_ring (vui, txvq,
1128 ring[txvq->last_used_idx & qsz_mask]);
1129 txvq->last_used_idx++;
1134 rx_len = vec_len (vum->rx_buffers[cpu_index]); //vector might be null
1137 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1139 while (n_left > 0 && n_left_to_next > 0)
1141 vlib_buffer_t *b_head, *b_current;
1142 u32 bi_head, bi_current;
1143 u16 desc_chain_head, desc_current;
1144 u8 error = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
1146 if (PREDICT_TRUE (n_left > 1))
1149 txvq->avail->ring[(txvq->last_avail_idx + 1) & qsz_mask];
1151 map_guest_mem (vui, txvq->desc[next_desc].addr,
1152 &map_guest_hint_desc);
1153 if (PREDICT_TRUE (buffer_addr != 0))
1154 CLIB_PREFETCH (buffer_addr, 64, STORE);
1156 u32 bi = vum->rx_buffers[cpu_index][rx_len - 2];
1157 vlib_prefetch_buffer_with_index (vm, bi, STORE);
1158 CLIB_PREFETCH (vlib_get_buffer (vm, bi)->data, 128, STORE);
1161 desc_chain_head = desc_current =
1162 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1163 bi_head = bi_current = vum->rx_buffers[cpu_index][--rx_len];
1164 b_head = b_current = vlib_get_buffer (vm, bi_head);
1165 vlib_buffer_chain_init (b_head);
1168 if (PREDICT_TRUE (vui->is_any_layout) ||
1169 (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1170 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)))
1172 /* ANYLAYOUT or single buffer */
1173 offset = vui->virtio_net_hdr_sz;
1177 /* CSR case without ANYLAYOUT, skip 1st buffer */
1178 offset = txvq->desc[desc_current].len;
1181 vring_desc_t *desc_table = txvq->desc;
1182 u32 desc_index = desc_current;
1183 map_guest_hint_p = &map_guest_hint_desc;
1185 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1187 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1188 &map_guest_hint_desc);
1190 map_guest_hint_p = &map_guest_hint_indirect;
1191 if (PREDICT_FALSE (desc_table == 0))
1193 error = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
1201 map_guest_mem (vui, desc_table[desc_index].addr,
1203 if (PREDICT_FALSE (buffer_addr == 0))
1205 error = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
1210 (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT))
1212 CLIB_PREFETCH (&desc_table[desc_table[desc_index].next],
1213 sizeof (vring_desc_t), STORE);
1216 #if VHOST_USER_COPY_TX_HDR == 1
1217 if (PREDICT_TRUE (offset))
1218 clib_memcpy (b->pre_data, buffer_addr, sizeof (virtio_net_hdr_t)); /* 12 byte hdr is not used on tx */
1221 if (desc_table[desc_index].len > offset)
1223 u16 len = desc_table[desc_index].len - offset;
1224 u16 copied = vlib_buffer_chain_append_data_with_alloc (vm,
1225 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX,
1234 error = VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER;
1240 /* if next flag is set, take next desc in the chain */
1241 if ((desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT))
1242 desc_index = desc_table[desc_index].next;
1248 /* consume the descriptor and return it as used */
1249 txvq->last_avail_idx++;
1250 txvq->used->ring[txvq->last_used_idx & qsz_mask].id =
1252 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1253 vhost_user_log_dirty_ring (vui, txvq,
1254 ring[txvq->last_used_idx & qsz_mask]);
1255 txvq->last_used_idx++;
1257 //It is important to free RX as fast as possible such that the TX
1258 //process does not drop packets
1259 if ((txvq->last_used_idx & 0x3f) == 0) // Every 64 packets
1260 txvq->used->idx = txvq->last_used_idx;
1262 if (PREDICT_FALSE (b_head->current_length < 14 &&
1263 error == VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1264 error = VHOST_USER_INPUT_FUNC_ERROR_UNDERSIZED_FRAME;
1266 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1268 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1269 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1270 b_head->error = node->errors[error];
1272 if (PREDICT_FALSE (n_trace > n_rx_packets))
1273 vec_add1 (vui->d_trace_buffers, bi_head);
1275 if (PREDICT_FALSE (error))
1278 next0 = VHOST_USER_RX_NEXT_DROP;
1283 b_head->current_length +
1284 b_head->total_length_not_including_first_buffer;
1286 next0 = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
1289 to_next[0] = bi_head;
1292 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1293 to_next, n_left_to_next,
1298 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1302 if (PREDICT_TRUE (vum->rx_buffers[cpu_index] != 0))
1303 _vec_len (vum->rx_buffers[cpu_index]) = rx_len;
1305 /* give buffers back to driver */
1306 CLIB_MEMORY_BARRIER ();
1307 txvq->used->idx = txvq->last_used_idx;
1308 vhost_user_log_dirty_ring (vui, txvq, idx);
1310 if (PREDICT_FALSE (vec_len (vui->d_trace_buffers) > 0))
1312 vhost_user_rx_trace (vm, node, vui, VHOST_NET_VRING_IDX_TX);
1313 vlib_set_trace_count (vm, node,
1314 n_trace - vec_len (vui->d_trace_buffers));
1317 /* interrupt (call) handling */
1318 if ((txvq->callfd > -1) && !(txvq->avail->flags & 1))
1320 txvq->n_since_last_int += n_rx_packets;
1322 if (txvq->n_since_last_int > vum->coalesce_frames)
1323 vhost_user_send_call (vm, txvq);
1326 if (PREDICT_FALSE (drops))
1328 vlib_increment_simple_counter
1329 (vnet_main.interface_main.sw_if_counters
1330 + VNET_INTERFACE_COUNTER_DROP, os_get_cpu_number (),
1331 vui->sw_if_index, drops);
1334 /* increase rx counters */
1335 vlib_increment_combined_counter
1336 (vnet_main.interface_main.combined_sw_if_counters
1337 + VNET_INTERFACE_COUNTER_RX,
1338 os_get_cpu_number (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1340 return n_rx_packets;
1344 vhost_user_input (vlib_main_t * vm,
1345 vlib_node_runtime_t * node, vlib_frame_t * f)
1347 vhost_user_main_t *vum = &vhost_user_main;
1348 u32 cpu_index = os_get_cpu_number ();
1349 vhost_user_intf_t *vui;
1350 uword n_rx_packets = 0;
1353 for (i = 0; i < vec_len (vum->vhost_user_interfaces); i++)
1355 vui = vec_elt_at_index (vum->vhost_user_interfaces, i);
1358 if ((i % vum->input_cpu_count) ==
1359 (cpu_index - vum->input_cpu_first_index))
1360 n_rx_packets += vhost_user_if_input (vm, vum, vui, node);
1363 return n_rx_packets;
1367 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1368 .function = vhost_user_input,
1369 .type = VLIB_NODE_TYPE_INPUT,
1370 .name = "vhost-user-input",
1372 /* Will be enabled if/when hardware is detected. */
1373 .state = VLIB_NODE_STATE_DISABLED,
1375 .format_buffer = format_ethernet_header_with_length,
1376 .format_trace = format_vhost_user_input_trace,
1378 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1379 .error_strings = vhost_user_input_func_error_strings,
1381 .n_next_nodes = VHOST_USER_RX_N_NEXT,
1383 [VHOST_USER_RX_NEXT_DROP] = "error-drop",
1384 [VHOST_USER_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
1388 VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
1392 vhost_user_intfc_tx (vlib_main_t * vm,
1393 vlib_node_runtime_t * node, vlib_frame_t * frame)
1395 u32 *buffers = vlib_frame_args (frame);
1397 vhost_user_main_t *vum = &vhost_user_main;
1398 uword n_packets = 0;
1399 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
1400 vhost_user_intf_t *vui =
1401 vec_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1402 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
1404 u8 error = VHOST_USER_TX_FUNC_ERROR_NONE;
1406 n_left = n_packets = frame->n_vectors;
1407 u32 map_guest_hint_desc = 0;
1408 u32 map_guest_hint_indirect = 0;
1409 u32 *map_guest_hint_p = &map_guest_hint_desc;
1411 if (PREDICT_FALSE (!vui->is_up))
1415 (!rxvq->desc || !rxvq->avail || vui->sock_errno != 0 || !rxvq->enabled))
1417 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1421 if (PREDICT_FALSE (vui->lockp != 0))
1423 while (__sync_lock_test_and_set (vui->lockp, 1))
1427 /* only bit 0 of avail.flags is used so we don't want to deal with this
1428 interface if any other bit is set */
1429 if (PREDICT_FALSE (rxvq->avail->flags & 0xFFFE))
1431 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1435 if (PREDICT_FALSE ((rxvq->avail->idx == rxvq->last_avail_idx)))
1437 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1441 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
1445 vlib_buffer_t *b0, *current_b0;
1446 u16 desc_head, desc_index, desc_len;
1447 vring_desc_t *desc_table;
1451 b0 = vlib_get_buffer (vm, buffers[0]);
1454 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
1456 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1460 desc_table = rxvq->desc;
1461 map_guest_hint_p = &map_guest_hint_desc;
1462 desc_head = desc_index =
1463 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1464 if (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT)
1467 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
1469 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
1474 map_guest_mem (vui, rxvq->desc[desc_index].addr,
1475 &map_guest_hint_desc))))
1477 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1481 map_guest_hint_p = &map_guest_hint_indirect;
1484 desc_len = vui->virtio_net_hdr_sz;
1488 map_guest_mem (vui, desc_table[desc_index].addr,
1489 map_guest_hint_p))))
1491 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1494 buffer_len = desc_table[desc_index].len;
1496 CLIB_PREFETCH (buffer_addr,
1497 clib_min (buffer_len, 2 * CLIB_CACHE_LINE_BYTES), STORE);
1499 virtio_net_hdr_mrg_rxbuf_t *hdr =
1500 (virtio_net_hdr_mrg_rxbuf_t *) buffer_addr;
1502 hdr->hdr.gso_type = 0;
1503 if (vui->virtio_net_hdr_sz == 12)
1504 hdr->num_buffers = 1;
1506 vhost_user_log_dirty_pages (vui, desc_table[desc_index].addr,
1507 vui->virtio_net_hdr_sz);
1509 u16 bytes_left = b0->current_length;
1510 buffer_addr += vui->virtio_net_hdr_sz;
1511 buffer_len -= vui->virtio_net_hdr_sz;
1517 if (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT)
1519 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
1520 bytes_left = current_b0->current_length;
1529 if (buffer_len == 0)
1531 if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
1533 //Next one is chained
1534 desc_index = desc_table[desc_index].next;
1537 map_guest_mem (vui, desc_table[desc_index].addr,
1538 map_guest_hint_p))))
1540 rxvq->last_used_idx -= hdr->num_buffers - 1;
1541 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1542 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1545 buffer_len = desc_table[desc_index].len;
1547 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
1549 //Move from available to used buffer
1550 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id =
1552 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len =
1554 vhost_user_log_dirty_ring (vui, rxvq,
1555 ring[rxvq->last_used_idx &
1557 rxvq->last_avail_idx++;
1558 rxvq->last_used_idx++;
1562 (rxvq->last_avail_idx == rxvq->avail->idx))
1564 //Dequeue queued descriptors for this packet
1565 rxvq->last_used_idx -= hdr->num_buffers - 1;
1566 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1567 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1571 desc_table = rxvq->desc;
1572 map_guest_hint_p = &map_guest_hint_desc;
1573 desc_head = desc_index =
1574 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1576 (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
1578 //It is seriously unlikely that a driver will put indirect descriptor
1579 //after non-indirect descriptor.
1581 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
1583 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
1589 rxvq->desc[desc_index].addr,
1590 &map_guest_hint_desc))))
1592 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1596 map_guest_hint_p = &map_guest_hint_indirect;
1601 map_guest_mem (vui, desc_table[desc_index].addr,
1602 map_guest_hint_p))))
1604 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1607 buffer_len = desc_table[desc_index].len;
1608 CLIB_PREFETCH (buffer_addr,
1609 clib_min (buffer_len,
1610 2 * CLIB_CACHE_LINE_BYTES), STORE);
1614 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
1619 u16 bytes_to_copy = bytes_left;
1621 (bytes_to_copy > buffer_len) ? buffer_len : bytes_to_copy;
1622 clib_memcpy (buffer_addr,
1623 vlib_buffer_get_current (current_b0) +
1624 current_b0->current_length - bytes_left,
1627 vhost_user_log_dirty_pages (vui,
1628 desc_table[desc_index].addr +
1629 desc_table[desc_index].len -
1630 bytes_left - bytes_to_copy,
1633 bytes_left -= bytes_to_copy;
1634 buffer_len -= bytes_to_copy;
1635 buffer_addr += bytes_to_copy;
1636 desc_len += bytes_to_copy;
1639 if (PREDICT_TRUE (n_left >= 2))
1641 vlib_prefetch_buffer_with_index (vm, buffers[1], STORE);
1642 CLIB_PREFETCH (&n_left, sizeof (n_left), STORE);
1645 //Move from available to used ring
1646 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
1647 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
1648 vhost_user_log_dirty_ring (vui, rxvq,
1649 ring[rxvq->last_used_idx & qsz_mask]);
1651 rxvq->last_avail_idx++;
1652 rxvq->last_used_idx++;
1654 n_left--; //At the end for error counting when 'goto done' is invoked
1658 CLIB_MEMORY_BARRIER ();
1659 rxvq->used->idx = rxvq->last_used_idx;
1660 vhost_user_log_dirty_ring (vui, rxvq, idx);
1662 /* interrupt (call) handling */
1663 if ((rxvq->callfd > -1) && !(rxvq->avail->flags & 1))
1665 rxvq->n_since_last_int += n_packets - n_left;
1667 if (rxvq->n_since_last_int > vum->coalesce_frames)
1668 vhost_user_send_call (vm, rxvq);
1673 if (PREDICT_FALSE (vui->lockp != 0))
1676 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
1678 vlib_error_count (vm, node->node_index, error, n_left);
1679 vlib_increment_simple_counter
1680 (vnet_main.interface_main.sw_if_counters
1681 + VNET_INTERFACE_COUNTER_DROP,
1682 os_get_cpu_number (), vui->sw_if_index, n_left);
1685 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
1686 return frame->n_vectors;
1689 static clib_error_t *
1690 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
1693 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
1694 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1695 vhost_user_main_t *vum = &vhost_user_main;
1696 vhost_user_intf_t *vui =
1697 vec_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1699 vui->admin_up = is_up;
1702 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
1703 VNET_HW_INTERFACE_FLAG_LINK_UP);
1705 return /* no error */ 0;
1709 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
1710 .name = "vhost-user",
1711 .tx_function = vhost_user_intfc_tx,
1712 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
1713 .tx_function_error_strings = vhost_user_tx_func_error_strings,
1714 .format_device_name = format_vhost_user_interface_name,
1715 .name_renumber = vhost_user_name_renumber,
1716 .admin_up_down_function = vhost_user_interface_admin_up_down,
1717 .no_flatten_output_chains = 1,
1720 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
1721 vhost_user_intfc_tx)
1725 vhost_user_process (vlib_main_t * vm,
1726 vlib_node_runtime_t * rt, vlib_frame_t * f)
1728 vhost_user_main_t *vum = &vhost_user_main;
1729 vhost_user_intf_t *vui;
1730 struct sockaddr_un sun;
1732 unix_file_t template = { 0 };
1733 f64 timeout = 3153600000.0 /* 100 years */ ;
1734 uword *event_data = 0;
1736 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
1737 sun.sun_family = AF_UNIX;
1738 template.read_function = vhost_user_socket_read;
1739 template.error_function = vhost_user_socket_error;
1747 vlib_process_wait_for_event_or_clock (vm, timeout);
1748 vlib_process_get_events (vm, &event_data);
1749 vec_reset_length (event_data);
1753 vec_foreach (vui, vum->vhost_user_interfaces)
1756 if (vui->sock_is_server || !vui->active)
1759 if (vui->unix_fd == -1)
1761 /* try to connect */
1763 strncpy (sun.sun_path, (char *) vui->sock_filename,
1764 sizeof (sun.sun_path) - 1);
1767 (sockfd, (struct sockaddr *) &sun,
1768 sizeof (struct sockaddr_un)) == 0)
1770 vui->sock_errno = 0;
1771 vui->unix_fd = sockfd;
1772 template.file_descriptor = sockfd;
1773 vui->unix_file_index = unix_file_add (&unix_main, &template);
1774 hash_set (vum->vhost_user_interface_index_by_sock_fd, sockfd,
1775 vui - vum->vhost_user_interfaces);
1777 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
1783 vui->sock_errno = errno;
1788 /* check if socket is alive */
1790 socklen_t len = sizeof (error);
1792 getsockopt (vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1795 vhost_user_if_disconnect (vui);
1803 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1804 .function = vhost_user_process,
1805 .type = VLIB_NODE_TYPE_PROCESS,
1806 .name = "vhost-user-process",
1811 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1813 vhost_user_main_t *vum = &vhost_user_main;
1814 vhost_user_intf_t *vui;
1818 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index, sw_if_index);
1821 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1825 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1828 // interface is inactive
1830 // disconnect interface sockets
1831 vhost_user_if_disconnect (vui);
1832 // add to inactive interface list
1833 vec_add1 (vum->vhost_user_inactive_interfaces_index, p[0]);
1835 // reset renumbered iface
1836 if (p[0] < vec_len (vum->show_dev_instance_by_real_dev_instance))
1837 vum->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1839 ethernet_delete_interface (vnm, vui->hw_if_index);
1840 DBG_SOCK ("deleted (deactivated) vhost-user interface instance %d", p[0]);
1845 // init server socket on specified sock_filename
1847 vhost_user_init_server_sock (const char *sock_filename, int *sockfd)
1850 struct sockaddr_un un = { };
1852 /* create listening socket */
1853 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1857 return VNET_API_ERROR_SYSCALL_ERROR_1;
1860 un.sun_family = AF_UNIX;
1861 strncpy ((char *) un.sun_path, (char *) sock_filename,
1862 sizeof (un.sun_path) - 1);
1864 /* remove if exists */
1865 unlink ((char *) sock_filename);
1867 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1869 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1873 if (listen (fd, 1) == -1)
1875 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1879 unix_file_t template = { 0 };
1880 template.read_function = vhost_user_socksvr_accept_ready;
1881 template.file_descriptor = fd;
1882 unix_file_add (&unix_main, &template);
1891 // get new vhost_user_intf_t from inactive interfaces or create new one
1892 static vhost_user_intf_t *
1893 vhost_user_vui_new ()
1895 vhost_user_main_t *vum = &vhost_user_main;
1896 vhost_user_intf_t *vui = NULL;
1897 int inactive_cnt = vec_len (vum->vhost_user_inactive_interfaces_index);
1898 // if there are any inactive ifaces
1899 if (inactive_cnt > 0)
1903 vum->vhost_user_inactive_interfaces_index[inactive_cnt - 1];
1904 if (vec_len (vum->vhost_user_interfaces) > vui_idx)
1906 vui = vec_elt_at_index (vum->vhost_user_interfaces, vui_idx);
1907 DBG_SOCK ("reusing inactive vhost-user interface index %d",
1910 // "remove" from inactive list
1911 _vec_len (vum->vhost_user_inactive_interfaces_index) -= 1;
1914 // vui was not retrieved from inactive ifaces - create new
1916 vec_add2 (vum->vhost_user_interfaces, vui, 1);
1920 // create ethernet interface for vhost user intf
1922 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1923 vhost_user_intf_t * vui, u8 * hwaddress)
1925 vhost_user_main_t *vum = &vhost_user_main;
1927 clib_error_t *error;
1929 /* create hw and sw interface */
1932 clib_memcpy (hwaddr, hwaddress, 6);
1936 f64 now = vlib_time_now (vm);
1938 rnd = (u32) (now * 1e6);
1939 rnd = random_u32 (&rnd);
1941 clib_memcpy (hwaddr + 2, &rnd, sizeof (rnd));
1946 error = ethernet_register_interface
1948 vhost_user_dev_class.index,
1949 vui - vum->vhost_user_interfaces /* device instance */ ,
1950 hwaddr /* ethernet address */ ,
1951 &vui->hw_if_index, 0 /* flag change */ );
1953 clib_error_report (error);
1955 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
1956 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
1959 // initialize vui with specified attributes
1961 vhost_user_vui_init (vnet_main_t * vnm,
1962 vhost_user_intf_t * vui, int sockfd,
1963 const char *sock_filename,
1964 u8 is_server, u64 feature_mask, u32 * sw_if_index)
1966 vnet_sw_interface_t *sw;
1967 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1968 vlib_thread_main_t *tm = vlib_get_thread_main ();
1971 vui->unix_fd = sockfd;
1972 vui->sw_if_index = sw->sw_if_index;
1973 vui->num_vrings = 2;
1974 vui->sock_is_server = is_server;
1975 strncpy (vui->sock_filename, sock_filename,
1976 ARRAY_LEN (vui->sock_filename) - 1);
1977 vui->sock_errno = 0;
1979 vui->feature_mask = feature_mask;
1981 vui->unix_file_index = ~0;
1982 vui->log_base_addr = 0;
1984 for (q = 0; q < 2; q++)
1986 vui->vrings[q].enabled = 0;
1987 vui->vrings[q].callfd = -1;
1988 vui->vrings[q].kickfd = -1;
1991 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1994 *sw_if_index = vui->sw_if_index;
1996 if (tm->n_vlib_mains > 1)
1998 vui->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1999 CLIB_CACHE_LINE_BYTES);
2000 memset ((void *) vui->lockp, 0, CLIB_CACHE_LINE_BYTES);
2004 // register vui and start polling on it
2006 vhost_user_vui_register (vlib_main_t * vm, vhost_user_intf_t * vui)
2008 vhost_user_main_t *vum = &vhost_user_main;
2010 vlib_thread_main_t *tm = vlib_get_thread_main ();
2012 hash_set (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd,
2013 vui - vum->vhost_user_interfaces);
2014 hash_set (vum->vhost_user_interface_index_by_sw_if_index, vui->sw_if_index,
2015 vui - vum->vhost_user_interfaces);
2018 cpu_index = vum->input_cpu_first_index +
2019 (vui - vum->vhost_user_interfaces) % vum->input_cpu_count;
2021 if (tm->n_vlib_mains == 1)
2022 vlib_node_set_state (vm, vhost_user_input_node.index,
2023 VLIB_NODE_STATE_POLLING);
2025 vlib_node_set_state (vlib_mains[cpu_index], vhost_user_input_node.index,
2026 VLIB_NODE_STATE_POLLING);
2028 /* tell process to start polling for sockets */
2029 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2033 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2034 const char *sock_filename,
2038 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
2040 vhost_user_intf_t *vui = NULL;
2047 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0)
2053 vui = vhost_user_vui_new ();
2054 ASSERT (vui != NULL);
2056 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
2057 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
2058 feature_mask, &sw_if_idx);
2062 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2065 vhost_user_vui_register (vm, vui);
2068 *sw_if_index = sw_if_idx;
2074 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2075 const char *sock_filename,
2078 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
2080 vhost_user_main_t *vum = &vhost_user_main;
2081 vhost_user_intf_t *vui = NULL;
2087 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index, sw_if_index);
2090 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2094 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
2097 // interface is inactive
2099 // disconnect interface sockets
2100 vhost_user_if_disconnect (vui);
2104 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0)
2110 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
2111 feature_mask, &sw_if_idx);
2115 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2118 vhost_user_vui_register (vm, vui);
2124 vhost_user_connect_command_fn (vlib_main_t * vm,
2125 unformat_input_t * input,
2126 vlib_cli_command_t * cmd)
2128 unformat_input_t _line_input, *line_input = &_line_input;
2129 u8 *sock_filename = NULL;
2132 u64 feature_mask = (u64) ~ 0;
2134 u32 custom_dev_instance = ~0;
2138 /* Get a line of input. */
2139 if (!unformat_user (input, unformat_line_input, line_input))
2142 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2144 if (unformat (line_input, "socket %s", &sock_filename))
2146 else if (unformat (line_input, "server"))
2148 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2152 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
2154 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
2159 return clib_error_return (0, "unknown input `%U'",
2160 format_unformat_error, input);
2162 unformat_free (line_input);
2164 vnet_main_t *vnm = vnet_get_main ();
2167 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
2168 is_server, &sw_if_index, feature_mask,
2169 renumber, custom_dev_instance, hw)))
2171 vec_free (sock_filename);
2172 return clib_error_return (0, "vhost_user_create_if returned %d", rv);
2175 vec_free (sock_filename);
2176 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
2182 vhost_user_delete_command_fn (vlib_main_t * vm,
2183 unformat_input_t * input,
2184 vlib_cli_command_t * cmd)
2186 unformat_input_t _line_input, *line_input = &_line_input;
2187 u32 sw_if_index = ~0;
2189 /* Get a line of input. */
2190 if (!unformat_user (input, unformat_line_input, line_input))
2193 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2195 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
2198 return clib_error_return (0, "unknown input `%U'",
2199 format_unformat_error, input);
2201 unformat_free (line_input);
2203 vnet_main_t *vnm = vnet_get_main ();
2205 vhost_user_delete_if (vnm, vm, sw_if_index);
2211 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
2212 vhost_user_intf_details_t ** out_vuids)
2215 vhost_user_main_t *vum = &vhost_user_main;
2216 vhost_user_intf_t *vui;
2217 vhost_user_intf_details_t *r_vuids = NULL;
2218 vhost_user_intf_details_t *vuid = NULL;
2219 u32 *hw_if_indices = 0;
2220 vnet_hw_interface_t *hi;
2227 vec_foreach (vui, vum->vhost_user_interfaces)
2230 vec_add1 (hw_if_indices, vui->hw_if_index);
2233 for (i = 0; i < vec_len (hw_if_indices); i++)
2235 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2236 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2238 vec_add2 (r_vuids, vuid, 1);
2239 vuid->sw_if_index = vui->sw_if_index;
2240 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
2241 vuid->features = vui->features;
2242 vuid->is_server = vui->sock_is_server;
2243 vuid->num_regions = vui->nregions;
2244 vuid->sock_errno = vui->sock_errno;
2245 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
2246 ARRAY_LEN (vuid->sock_filename) - 1);
2248 s = format (s, "%v%c", hi->name, 0);
2250 strncpy ((char *) vuid->if_name, (char *) s,
2251 ARRAY_LEN (vuid->if_name) - 1);
2256 vec_free (hw_if_indices);
2258 *out_vuids = r_vuids;
2264 show_vhost_user_command_fn (vlib_main_t * vm,
2265 unformat_input_t * input,
2266 vlib_cli_command_t * cmd)
2268 clib_error_t *error = 0;
2269 vnet_main_t *vnm = vnet_get_main ();
2270 vhost_user_main_t *vum = &vhost_user_main;
2271 vhost_user_intf_t *vui;
2272 u32 hw_if_index, *hw_if_indices = 0;
2273 vnet_hw_interface_t *hi;
2281 struct feat_struct *feat_entry;
2283 static struct feat_struct feat_array[] = {
2284 #define _(s,b) { .str = #s, .bit = b, },
2285 foreach_virtio_net_feature
2290 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2293 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2295 vec_add1 (hw_if_indices, hw_if_index);
2296 vlib_cli_output (vm, "add %d", hw_if_index);
2298 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2302 error = clib_error_return (0, "unknown input `%U'",
2303 format_unformat_error, input);
2307 if (vec_len (hw_if_indices) == 0)
2309 vec_foreach (vui, vum->vhost_user_interfaces)
2312 vec_add1 (hw_if_indices, vui->hw_if_index);
2315 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2316 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
2317 vum->coalesce_frames, vum->coalesce_time);
2319 for (i = 0; i < vec_len (hw_if_indices); i++)
2321 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2322 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2323 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
2324 hi->name, hw_if_indices[i]);
2326 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
2327 vui->virtio_net_hdr_sz, vui->features);
2329 feat_entry = (struct feat_struct *) &feat_array;
2330 while (feat_entry->str)
2332 if (vui->features & (1 << feat_entry->bit))
2333 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2338 vlib_cli_output (vm, "\n");
2341 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2343 vui->sock_is_server ? "server" : "client",
2344 strerror (vui->sock_errno));
2346 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2350 vlib_cli_output (vm,
2351 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2352 vlib_cli_output (vm,
2353 " ====== ===== ================== ================== ================== ================== ==================\n");
2355 for (j = 0; j < vui->nregions; j++)
2357 vlib_cli_output (vm,
2358 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2359 j, vui->region_mmap_fd[j],
2360 vui->regions[j].guest_phys_addr,
2361 vui->regions[j].memory_size,
2362 vui->regions[j].userspace_addr,
2363 vui->regions[j].mmap_offset,
2364 pointer_to_uword (vui->region_mmap_addr[j]));
2366 for (q = 0; q < vui->num_vrings; q++)
2368 vlib_cli_output (vm, "\n Virtqueue %d\n", q);
2370 vlib_cli_output (vm,
2371 " qsz %d last_avail_idx %d last_used_idx %d\n",
2372 vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
2373 vui->vrings[q].last_used_idx);
2375 if (vui->vrings[q].avail && vui->vrings[q].used)
2376 vlib_cli_output (vm,
2377 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
2378 vui->vrings[q].avail->flags,
2379 vui->vrings[q].avail->idx,
2380 vui->vrings[q].used->flags,
2381 vui->vrings[q].used->idx);
2383 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
2384 vui->vrings[q].kickfd,
2385 vui->vrings[q].callfd, vui->vrings[q].errfd);
2389 vlib_cli_output (vm, "\n descriptor table:\n");
2390 vlib_cli_output (vm,
2391 " id addr len flags next user_addr\n");
2392 vlib_cli_output (vm,
2393 " ===== ================== ===== ====== ===== ==================\n");
2394 for (j = 0; j < vui->vrings[q].qsz; j++)
2397 vlib_cli_output (vm,
2398 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
2399 j, vui->vrings[q].desc[j].addr,
2400 vui->vrings[q].desc[j].len,
2401 vui->vrings[q].desc[j].flags,
2402 vui->vrings[q].desc[j].next,
2403 pointer_to_uword (map_guest_mem
2405 vui->vrings[q].desc[j].
2410 vlib_cli_output (vm, "\n");
2413 vec_free (hw_if_indices);
2423 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2424 .path = "create vhost-user",
2425 .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [renumber <dev_instance>]",
2426 .function = vhost_user_connect_command_fn,
2429 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2430 .path = "delete vhost-user",
2431 .short_help = "delete vhost-user sw_if_index <nn>",
2432 .function = vhost_user_delete_command_fn,
2435 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2436 .path = "show vhost-user",
2437 .short_help = "show vhost-user interface",
2438 .function = show_vhost_user_command_fn,
2443 static clib_error_t *
2444 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2446 vhost_user_main_t *vum = &vhost_user_main;
2448 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2450 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2452 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2454 else if (unformat (input, "dont-dump-memory"))
2455 vum->dont_dump_vhost_user_memory = 1;
2457 return clib_error_return (0, "unknown input `%U'",
2458 format_unformat_error, input);
2464 /* vhost-user { ... } configuration. */
2465 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2468 vhost_user_unmap_all (void)
2470 vhost_user_main_t *vum = &vhost_user_main;
2471 vhost_user_intf_t *vui;
2473 if (vum->dont_dump_vhost_user_memory)
2475 vec_foreach (vui, vum->vhost_user_interfaces)
2477 unmap_all_mem_regions (vui);
2483 * fd.io coding-style-patch-verification: ON
2486 * eval: (c-set-style "gnu")