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].last_avail_idx =
592 vui->vrings[msg.state.index].used->idx;
594 /* tell driver that we don't want interrupts */
595 vui->vrings[msg.state.index].used->flags |= 1;
598 case VHOST_USER_SET_OWNER:
599 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
602 case VHOST_USER_RESET_OWNER:
603 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
606 case VHOST_USER_SET_VRING_CALL:
607 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
608 vui->hw_if_index, msg.u64);
610 q = (u8) (msg.u64 & 0xFF);
612 if (!(msg.u64 & 0x100))
614 if (number_of_fds != 1)
617 /* if there is old fd, delete it */
618 if (vui->vrings[q].callfd > -1)
620 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
621 vui->vrings[q].callfd_idx);
622 unix_file_del (&unix_main, uf);
624 vui->vrings[q].callfd = fds[0];
625 template.read_function = vhost_user_callfd_read_ready;
626 template.file_descriptor = fds[0];
627 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
630 vui->vrings[q].callfd = -1;
633 case VHOST_USER_SET_VRING_KICK:
634 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
635 vui->hw_if_index, msg.u64);
637 q = (u8) (msg.u64 & 0xFF);
639 if (!(msg.u64 & 0x100))
641 if (number_of_fds != 1)
644 if (vui->vrings[q].kickfd > -1)
645 close (vui->vrings[q].kickfd);
647 vui->vrings[q].kickfd = fds[0];
650 vui->vrings[q].kickfd = -1;
653 case VHOST_USER_SET_VRING_ERR:
654 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
655 vui->hw_if_index, msg.u64);
657 q = (u8) (msg.u64 & 0xFF);
659 if (!(msg.u64 & 0x100))
661 if (number_of_fds != 1)
669 vui->vrings[q].errfd = fd;
672 case VHOST_USER_SET_VRING_BASE:
673 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
674 vui->hw_if_index, msg.state.index, msg.state.num);
676 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
679 case VHOST_USER_GET_VRING_BASE:
680 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
681 vui->hw_if_index, msg.state.index, msg.state.num);
683 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
684 vui->vrings[msg.state.index].enabled = 0;
686 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
688 msg.size = sizeof (msg.state);
691 case VHOST_USER_NONE:
692 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
696 case VHOST_USER_SET_LOG_BASE:
698 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
700 if (msg.size != sizeof (msg.log))
703 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
704 msg.size, sizeof (msg.log));
709 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
712 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
717 /* align size to 2M page */
718 long page_sz = get_huge_page_size (fd);
720 (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
722 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
726 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
727 map_sz, msg.log.offset, fd, vui->log_base_addr);
729 if (vui->log_base_addr == MAP_FAILED)
731 clib_warning ("failed to map memory. errno is %d", errno);
735 vui->log_base_addr += msg.log.offset;
736 vui->log_size = msg.log.size;
739 msg.size = sizeof (msg.u64);
744 case VHOST_USER_SET_LOG_FD:
745 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
749 case VHOST_USER_GET_PROTOCOL_FEATURES:
750 DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
754 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD);
755 msg.size = sizeof (msg.u64);
758 case VHOST_USER_SET_PROTOCOL_FEATURES:
759 DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
760 vui->hw_if_index, msg.u64);
762 vui->protocol_features = msg.u64;
766 case VHOST_USER_SET_VRING_ENABLE:
767 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE, enable: %d",
768 vui->hw_if_index, msg.state.num);
769 vui->vrings[msg.state.index].enabled = msg.state.num;
773 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
778 /* if we have pointers to descriptor table, go up */
780 vui->vrings[VHOST_NET_VRING_IDX_TX].desc &&
781 vui->vrings[VHOST_NET_VRING_IDX_RX].desc)
784 DBG_SOCK ("interface %d connected", vui->sw_if_index);
786 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
787 VNET_HW_INTERFACE_FLAG_LINK_UP);
792 /* if we need to reply */
796 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
797 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
804 vhost_user_if_disconnect (vui);
808 static clib_error_t *
809 vhost_user_socket_error (unix_file_t * uf)
811 vhost_user_main_t *vum = &vhost_user_main;
812 vhost_user_intf_t *vui;
815 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
816 uf->file_descriptor);
819 DBG_SOCK ("fd %d doesn't belong to any interface", uf->file_descriptor);
823 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
825 vhost_user_if_disconnect (vui);
829 static clib_error_t *
830 vhost_user_socksvr_accept_ready (unix_file_t * uf)
832 int client_fd, client_len;
833 struct sockaddr_un client;
834 unix_file_t template = { 0 };
835 vhost_user_main_t *vum = &vhost_user_main;
836 vhost_user_intf_t *vui;
839 p = hash_get (vum->vhost_user_interface_index_by_listener_fd,
840 uf->file_descriptor);
843 DBG_SOCK ("fd %d doesn't belong to any interface", uf->file_descriptor);
847 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
849 client_len = sizeof (client);
850 client_fd = accept (uf->file_descriptor,
851 (struct sockaddr *) &client,
852 (socklen_t *) & client_len);
855 return clib_error_return_unix (0, "accept");
857 template.read_function = vhost_user_socket_read;
858 template.error_function = vhost_user_socket_error;
859 template.file_descriptor = client_fd;
860 vui->unix_file_index = unix_file_add (&unix_main, &template);
862 vui->client_fd = client_fd;
863 hash_set (vum->vhost_user_interface_index_by_sock_fd, vui->client_fd,
864 vui - vum->vhost_user_interfaces);
869 static clib_error_t *
870 vhost_user_init (vlib_main_t * vm)
873 vhost_user_main_t *vum = &vhost_user_main;
874 vlib_thread_main_t *tm = vlib_get_thread_main ();
875 vlib_thread_registration_t *tr;
878 error = vlib_call_init_function (vm, ip4_init);
882 vum->vhost_user_interface_index_by_listener_fd =
883 hash_create (0, sizeof (uword));
884 vum->vhost_user_interface_index_by_sock_fd =
885 hash_create (0, sizeof (uword));
886 vum->vhost_user_interface_index_by_sw_if_index =
887 hash_create (0, sizeof (uword));
888 vum->coalesce_frames = 32;
889 vum->coalesce_time = 1e-3;
891 vec_validate_aligned (vum->rx_buffers, tm->n_vlib_mains - 1,
892 CLIB_CACHE_LINE_BYTES);
894 /* find out which cpus will be used for input */
895 vum->input_cpu_first_index = 0;
896 vum->input_cpu_count = 1;
897 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
898 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
900 if (tr && tr->count > 0)
902 vum->input_cpu_first_index = tr->first_index;
903 vum->input_cpu_count = tr->count;
909 VLIB_INIT_FUNCTION (vhost_user_init);
911 static clib_error_t *
912 vhost_user_exit (vlib_main_t * vm)
918 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
922 VHOST_USER_RX_NEXT_ETHERNET_INPUT,
923 VHOST_USER_RX_NEXT_DROP,
924 VHOST_USER_RX_N_NEXT,
932 #if VHOST_USER_COPY_TX_HDR == 1
933 virtio_net_hdr_t hdr;
935 } vhost_user_input_trace_t;
938 format_vhost_user_input_trace (u8 * s, va_list * va)
940 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
941 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
942 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
943 vhost_user_main_t *vum = &vhost_user_main;
944 vhost_user_input_trace_t *t = va_arg (*va, vhost_user_input_trace_t *);
945 vhost_user_intf_t *vui = vec_elt_at_index (vum->vhost_user_interfaces,
948 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
950 #if VHOST_USER_COPY_TX_HDR == 1
951 uword indent = format_get_indent (s);
954 s = format (s, "%U virtqueue %d",
955 format_vnet_sw_interface_name, vnm, sw, t->virtqueue);
957 #if VHOST_USER_COPY_TX_HDR == 1
958 s = format (s, "\n%Uvirtio_net_hdr flags 0x%02x gso_type %u hdr_len %u",
959 format_white_space, indent,
960 t->hdr.flags, t->hdr.gso_type, t->hdr.hdr_len);
967 vhost_user_rx_trace (vlib_main_t * vm,
968 vlib_node_runtime_t * node,
969 vhost_user_intf_t * vui, i16 virtqueue)
972 vhost_user_main_t *vum = &vhost_user_main;
974 u32 next_index = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
976 n_left = vec_len (vui->d_trace_buffers);
977 b = vui->d_trace_buffers;
983 vhost_user_input_trace_t *t0;
988 b0 = vlib_get_buffer (vm, bi0);
989 vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 0);
990 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
991 t0->virtqueue = virtqueue;
992 t0->device_index = vui - vum->vhost_user_interfaces;
993 #if VHOST_USER_COPY_TX_HDR == 1
994 clib_memcpy (&t0->hdr, b0->pre_data, sizeof (virtio_net_hdr_t));
1002 vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
1004 vhost_user_main_t *vum = &vhost_user_main;
1006 int rv __attribute__ ((unused));
1007 /* $$$$ pay attention to rv */
1008 rv = write (vq->callfd, &x, sizeof (x));
1009 vq->n_since_last_int = 0;
1010 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
1015 vhost_user_if_input (vlib_main_t * vm,
1016 vhost_user_main_t * vum,
1017 vhost_user_intf_t * vui, vlib_node_runtime_t * node)
1019 vhost_user_vring_t *txvq = &vui->vrings[VHOST_NET_VRING_IDX_TX];
1020 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
1021 uword n_rx_packets = 0, n_rx_bytes = 0;
1023 u32 n_left_to_next, *to_next;
1026 uword n_trace = vlib_get_trace_count (vm, node);
1028 u32 cpu_index, rx_len, drops, flush;
1029 f64 now = vlib_time_now (vm);
1030 u32 map_guest_hint_desc = 0;
1031 u32 map_guest_hint_indirect = 0;
1032 u32 *map_guest_hint_p = &map_guest_hint_desc;
1034 vec_reset_length (vui->d_trace_buffers);
1036 /* no descriptor ptr - bail out */
1037 if (PREDICT_FALSE (!txvq->desc || !txvq->avail || !txvq->enabled))
1040 /* do we have pending intterupts ? */
1041 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1042 vhost_user_send_call (vm, txvq);
1044 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1045 vhost_user_send_call (vm, rxvq);
1047 /* only bit 0 of avail.flags is used so we don't want to deal with this
1048 interface if any other bit is set */
1049 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
1052 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1055 if (PREDICT_FALSE (n_left == 0))
1058 if (PREDICT_FALSE (n_left == txvq->qsz))
1060 //Informational error logging when VPP is not receiving packets fast enough
1061 vlib_error_count (vm, node->node_index,
1062 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1065 if (PREDICT_FALSE (!vui->admin_up))
1067 /* if intf is admin down, just drop all packets waiting in the ring */
1068 txvq->last_avail_idx = txvq->last_used_idx = txvq->avail->idx;
1069 CLIB_MEMORY_BARRIER ();
1070 txvq->used->idx = txvq->last_used_idx;
1071 vhost_user_log_dirty_ring (vui, txvq, idx);
1072 vhost_user_send_call (vm, txvq);
1076 qsz_mask = txvq->qsz - 1;
1077 cpu_index = os_get_cpu_number ();
1081 if (n_left > VLIB_FRAME_SIZE)
1082 n_left = VLIB_FRAME_SIZE;
1084 /* Allocate some buffers.
1085 * Note that buffers that are chained for jumbo
1086 * frames are allocated separately using a slower path.
1087 * The idea is to be certain to have enough buffers at least
1088 * to cycle through the descriptors without having to check for errors.
1089 * For jumbo frames, the bottleneck is memory copy anyway.
1091 if (PREDICT_FALSE (!vum->rx_buffers[cpu_index]))
1093 vec_alloc (vum->rx_buffers[cpu_index], 2 * VLIB_FRAME_SIZE);
1095 if (PREDICT_FALSE (!vum->rx_buffers[cpu_index]))
1096 flush = n_left; //Drop all input
1099 if (PREDICT_FALSE (_vec_len (vum->rx_buffers[cpu_index]) < n_left))
1101 u32 curr_len = _vec_len (vum->rx_buffers[cpu_index]);
1102 _vec_len (vum->rx_buffers[cpu_index]) +=
1103 vlib_buffer_alloc_from_free_list (vm,
1104 vum->rx_buffers[cpu_index] +
1106 2 * VLIB_FRAME_SIZE - curr_len,
1107 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
1109 if (PREDICT_FALSE (n_left > _vec_len (vum->rx_buffers[cpu_index])))
1110 flush = n_left - _vec_len (vum->rx_buffers[cpu_index]);
1113 if (PREDICT_FALSE (flush))
1115 //Remove some input buffers
1118 vlib_error_count (vm, vhost_user_input_node.index,
1119 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
1122 u16 desc_chain_head =
1123 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1124 txvq->last_avail_idx++;
1125 txvq->used->ring[txvq->last_used_idx & qsz_mask].id =
1127 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1128 vhost_user_log_dirty_ring (vui, txvq,
1129 ring[txvq->last_used_idx & qsz_mask]);
1130 txvq->last_used_idx++;
1135 rx_len = vec_len (vum->rx_buffers[cpu_index]); //vector might be null
1138 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1140 while (n_left > 0 && n_left_to_next > 0)
1142 vlib_buffer_t *b_head, *b_current;
1143 u32 bi_head, bi_current;
1144 u16 desc_chain_head, desc_current;
1145 u8 error = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
1147 if (PREDICT_TRUE (n_left > 1))
1150 txvq->avail->ring[(txvq->last_avail_idx + 1) & qsz_mask];
1152 map_guest_mem (vui, txvq->desc[next_desc].addr,
1153 &map_guest_hint_desc);
1154 if (PREDICT_TRUE (buffer_addr != 0))
1155 CLIB_PREFETCH (buffer_addr, 64, STORE);
1157 u32 bi = vum->rx_buffers[cpu_index][rx_len - 2];
1158 vlib_prefetch_buffer_with_index (vm, bi, STORE);
1159 CLIB_PREFETCH (vlib_get_buffer (vm, bi)->data, 128, STORE);
1162 desc_chain_head = desc_current =
1163 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1164 bi_head = bi_current = vum->rx_buffers[cpu_index][--rx_len];
1165 b_head = b_current = vlib_get_buffer (vm, bi_head);
1166 vlib_buffer_chain_init (b_head);
1169 if (PREDICT_TRUE (vui->is_any_layout) ||
1170 (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1171 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)))
1173 /* ANYLAYOUT or single buffer */
1174 offset = vui->virtio_net_hdr_sz;
1178 /* CSR case without ANYLAYOUT, skip 1st buffer */
1179 offset = txvq->desc[desc_current].len;
1182 vring_desc_t *desc_table = txvq->desc;
1183 u32 desc_index = desc_current;
1184 map_guest_hint_p = &map_guest_hint_desc;
1186 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1188 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1189 &map_guest_hint_desc);
1191 map_guest_hint_p = &map_guest_hint_indirect;
1192 if (PREDICT_FALSE (desc_table == 0))
1194 error = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
1202 map_guest_mem (vui, desc_table[desc_index].addr,
1204 if (PREDICT_FALSE (buffer_addr == 0))
1206 error = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
1211 (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT))
1213 CLIB_PREFETCH (&desc_table[desc_table[desc_index].next],
1214 sizeof (vring_desc_t), STORE);
1217 #if VHOST_USER_COPY_TX_HDR == 1
1218 if (PREDICT_TRUE (offset))
1219 clib_memcpy (b->pre_data, buffer_addr, sizeof (virtio_net_hdr_t)); /* 12 byte hdr is not used on tx */
1222 if (desc_table[desc_index].len > offset)
1224 u16 len = desc_table[desc_index].len - offset;
1225 u16 copied = vlib_buffer_chain_append_data_with_alloc (vm,
1226 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX,
1235 error = VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER;
1241 /* if next flag is set, take next desc in the chain */
1242 if ((desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT))
1243 desc_index = desc_table[desc_index].next;
1249 /* consume the descriptor and return it as used */
1250 txvq->last_avail_idx++;
1251 txvq->used->ring[txvq->last_used_idx & qsz_mask].id =
1253 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1254 vhost_user_log_dirty_ring (vui, txvq,
1255 ring[txvq->last_used_idx & qsz_mask]);
1256 txvq->last_used_idx++;
1258 //It is important to free RX as fast as possible such that the TX
1259 //process does not drop packets
1260 if ((txvq->last_used_idx & 0x3f) == 0) // Every 64 packets
1261 txvq->used->idx = txvq->last_used_idx;
1263 if (PREDICT_FALSE (b_head->current_length < 14 &&
1264 error == VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR))
1265 error = VHOST_USER_INPUT_FUNC_ERROR_UNDERSIZED_FRAME;
1267 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
1269 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1270 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1271 b_head->error = node->errors[error];
1273 if (PREDICT_FALSE (n_trace > n_rx_packets))
1274 vec_add1 (vui->d_trace_buffers, bi_head);
1276 if (PREDICT_FALSE (error))
1279 next0 = VHOST_USER_RX_NEXT_DROP;
1284 b_head->current_length +
1285 b_head->total_length_not_including_first_buffer;
1287 next0 = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
1290 to_next[0] = bi_head;
1293 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1294 to_next, n_left_to_next,
1299 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1303 if (PREDICT_TRUE (vum->rx_buffers[cpu_index] != 0))
1304 _vec_len (vum->rx_buffers[cpu_index]) = rx_len;
1306 /* give buffers back to driver */
1307 CLIB_MEMORY_BARRIER ();
1308 txvq->used->idx = txvq->last_used_idx;
1309 vhost_user_log_dirty_ring (vui, txvq, idx);
1311 if (PREDICT_FALSE (vec_len (vui->d_trace_buffers) > 0))
1313 vhost_user_rx_trace (vm, node, vui, VHOST_NET_VRING_IDX_TX);
1314 vlib_set_trace_count (vm, node,
1315 n_trace - vec_len (vui->d_trace_buffers));
1318 /* interrupt (call) handling */
1319 if ((txvq->callfd > -1) && !(txvq->avail->flags & 1))
1321 txvq->n_since_last_int += n_rx_packets;
1323 if (txvq->n_since_last_int > vum->coalesce_frames)
1324 vhost_user_send_call (vm, txvq);
1327 if (PREDICT_FALSE (drops))
1329 vlib_increment_simple_counter
1330 (vnet_main.interface_main.sw_if_counters
1331 + VNET_INTERFACE_COUNTER_DROP, os_get_cpu_number (),
1332 vui->sw_if_index, drops);
1335 /* increase rx counters */
1336 vlib_increment_combined_counter
1337 (vnet_main.interface_main.combined_sw_if_counters
1338 + VNET_INTERFACE_COUNTER_RX,
1339 os_get_cpu_number (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
1341 return n_rx_packets;
1345 vhost_user_input (vlib_main_t * vm,
1346 vlib_node_runtime_t * node, vlib_frame_t * f)
1348 vhost_user_main_t *vum = &vhost_user_main;
1349 u32 cpu_index = os_get_cpu_number ();
1350 vhost_user_intf_t *vui;
1351 uword n_rx_packets = 0;
1354 for (i = 0; i < vec_len (vum->vhost_user_interfaces); i++)
1356 vui = vec_elt_at_index (vum->vhost_user_interfaces, i);
1359 if ((i % vum->input_cpu_count) ==
1360 (cpu_index - vum->input_cpu_first_index))
1361 n_rx_packets += vhost_user_if_input (vm, vum, vui, node);
1364 return n_rx_packets;
1368 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1369 .function = vhost_user_input,
1370 .type = VLIB_NODE_TYPE_INPUT,
1371 .name = "vhost-user-input",
1373 /* Will be enabled if/when hardware is detected. */
1374 .state = VLIB_NODE_STATE_DISABLED,
1376 .format_buffer = format_ethernet_header_with_length,
1377 .format_trace = format_vhost_user_input_trace,
1379 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1380 .error_strings = vhost_user_input_func_error_strings,
1382 .n_next_nodes = VHOST_USER_RX_N_NEXT,
1384 [VHOST_USER_RX_NEXT_DROP] = "error-drop",
1385 [VHOST_USER_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
1389 VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
1393 vhost_user_intfc_tx (vlib_main_t * vm,
1394 vlib_node_runtime_t * node, vlib_frame_t * frame)
1396 u32 *buffers = vlib_frame_args (frame);
1398 vhost_user_main_t *vum = &vhost_user_main;
1399 uword n_packets = 0;
1400 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
1401 vhost_user_intf_t *vui =
1402 vec_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1403 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
1405 u8 error = VHOST_USER_TX_FUNC_ERROR_NONE;
1407 n_left = n_packets = frame->n_vectors;
1408 u32 map_guest_hint_desc = 0;
1409 u32 map_guest_hint_indirect = 0;
1410 u32 *map_guest_hint_p = &map_guest_hint_desc;
1412 if (PREDICT_FALSE (!vui->is_up))
1416 (!rxvq->desc || !rxvq->avail || vui->sock_errno != 0 || !rxvq->enabled))
1418 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1422 if (PREDICT_FALSE (vui->lockp != 0))
1424 while (__sync_lock_test_and_set (vui->lockp, 1))
1428 /* only bit 0 of avail.flags is used so we don't want to deal with this
1429 interface if any other bit is set */
1430 if (PREDICT_FALSE (rxvq->avail->flags & 0xFFFE))
1432 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1436 if (PREDICT_FALSE ((rxvq->avail->idx == rxvq->last_avail_idx)))
1438 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1442 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
1446 vlib_buffer_t *b0, *current_b0;
1447 u16 desc_head, desc_index, desc_len;
1448 vring_desc_t *desc_table;
1452 b0 = vlib_get_buffer (vm, buffers[0]);
1455 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
1457 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1461 desc_table = rxvq->desc;
1462 map_guest_hint_p = &map_guest_hint_desc;
1463 desc_head = desc_index =
1464 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1465 if (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT)
1468 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
1470 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
1475 map_guest_mem (vui, rxvq->desc[desc_index].addr,
1476 &map_guest_hint_desc))))
1478 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1482 map_guest_hint_p = &map_guest_hint_indirect;
1485 desc_len = vui->virtio_net_hdr_sz;
1489 map_guest_mem (vui, desc_table[desc_index].addr,
1490 map_guest_hint_p))))
1492 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1495 buffer_len = desc_table[desc_index].len;
1497 CLIB_PREFETCH (buffer_addr, 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, CLIB_CACHE_LINE_BYTES, STORE);
1612 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
1617 u16 bytes_to_copy = bytes_left;
1619 (bytes_to_copy > buffer_len) ? buffer_len : bytes_to_copy;
1620 clib_memcpy (buffer_addr,
1621 vlib_buffer_get_current (current_b0) +
1622 current_b0->current_length - bytes_left,
1625 vhost_user_log_dirty_pages (vui,
1626 desc_table[desc_index].addr +
1627 desc_table[desc_index].len -
1628 bytes_left - bytes_to_copy,
1631 bytes_left -= bytes_to_copy;
1632 buffer_len -= bytes_to_copy;
1633 buffer_addr += bytes_to_copy;
1634 desc_len += bytes_to_copy;
1637 if (PREDICT_TRUE (n_left >= 2))
1639 vlib_prefetch_buffer_with_index (vm, buffers[1], STORE);
1640 CLIB_PREFETCH (&n_left, sizeof (n_left), STORE);
1643 //Move from available to used ring
1644 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
1645 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
1646 vhost_user_log_dirty_ring (vui, rxvq,
1647 ring[rxvq->last_used_idx & qsz_mask]);
1649 rxvq->last_avail_idx++;
1650 rxvq->last_used_idx++;
1652 n_left--; //At the end for error counting when 'goto done' is invoked
1656 CLIB_MEMORY_BARRIER ();
1657 rxvq->used->idx = rxvq->last_used_idx;
1658 vhost_user_log_dirty_ring (vui, rxvq, idx);
1660 /* interrupt (call) handling */
1661 if ((rxvq->callfd > -1) && !(rxvq->avail->flags & 1))
1663 rxvq->n_since_last_int += n_packets - n_left;
1665 if (rxvq->n_since_last_int > vum->coalesce_frames)
1666 vhost_user_send_call (vm, rxvq);
1671 if (PREDICT_FALSE (vui->lockp != 0))
1674 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
1676 vlib_error_count (vm, node->node_index, error, n_left);
1677 vlib_increment_simple_counter
1678 (vnet_main.interface_main.sw_if_counters
1679 + VNET_INTERFACE_COUNTER_DROP,
1680 os_get_cpu_number (), vui->sw_if_index, n_left);
1683 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
1684 return frame->n_vectors;
1687 static clib_error_t *
1688 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
1691 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
1692 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1693 vhost_user_main_t *vum = &vhost_user_main;
1694 vhost_user_intf_t *vui =
1695 vec_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1697 vui->admin_up = is_up;
1700 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
1701 VNET_HW_INTERFACE_FLAG_LINK_UP);
1703 return /* no error */ 0;
1707 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
1708 .name = "vhost-user",
1709 .tx_function = vhost_user_intfc_tx,
1710 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
1711 .tx_function_error_strings = vhost_user_tx_func_error_strings,
1712 .format_device_name = format_vhost_user_interface_name,
1713 .name_renumber = vhost_user_name_renumber,
1714 .admin_up_down_function = vhost_user_interface_admin_up_down,
1715 .no_flatten_output_chains = 1,
1718 VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
1719 vhost_user_intfc_tx)
1723 vhost_user_process (vlib_main_t * vm,
1724 vlib_node_runtime_t * rt, vlib_frame_t * f)
1726 vhost_user_main_t *vum = &vhost_user_main;
1727 vhost_user_intf_t *vui;
1728 struct sockaddr_un sun;
1730 unix_file_t template = { 0 };
1731 f64 timeout = 3153600000.0 /* 100 years */ ;
1732 uword *event_data = 0;
1734 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
1735 sun.sun_family = AF_UNIX;
1736 template.read_function = vhost_user_socket_read;
1737 template.error_function = vhost_user_socket_error;
1745 vlib_process_wait_for_event_or_clock (vm, timeout);
1746 vlib_process_get_events (vm, &event_data);
1747 vec_reset_length (event_data);
1751 vec_foreach (vui, vum->vhost_user_interfaces)
1754 if (vui->sock_is_server || !vui->active)
1757 if (vui->unix_fd == -1)
1759 /* try to connect */
1761 strncpy (sun.sun_path, (char *) vui->sock_filename,
1762 sizeof (sun.sun_path) - 1);
1765 (sockfd, (struct sockaddr *) &sun,
1766 sizeof (struct sockaddr_un)) == 0)
1768 vui->sock_errno = 0;
1769 vui->unix_fd = sockfd;
1770 template.file_descriptor = sockfd;
1771 vui->unix_file_index = unix_file_add (&unix_main, &template);
1772 hash_set (vum->vhost_user_interface_index_by_sock_fd, sockfd,
1773 vui - vum->vhost_user_interfaces);
1775 sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
1781 vui->sock_errno = errno;
1786 /* check if socket is alive */
1788 socklen_t len = sizeof (error);
1790 getsockopt (vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1793 vhost_user_if_disconnect (vui);
1801 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1802 .function = vhost_user_process,
1803 .type = VLIB_NODE_TYPE_PROCESS,
1804 .name = "vhost-user-process",
1809 vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
1811 vhost_user_main_t *vum = &vhost_user_main;
1812 vhost_user_intf_t *vui;
1816 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index, sw_if_index);
1819 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1823 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1826 // interface is inactive
1828 // disconnect interface sockets
1829 vhost_user_if_disconnect (vui);
1830 // add to inactive interface list
1831 vec_add1 (vum->vhost_user_inactive_interfaces_index, p[0]);
1833 // reset renumbered iface
1834 if (p[0] < vec_len (vum->show_dev_instance_by_real_dev_instance))
1835 vum->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1837 ethernet_delete_interface (vnm, vui->hw_if_index);
1838 DBG_SOCK ("deleted (deactivated) vhost-user interface instance %d", p[0]);
1843 // init server socket on specified sock_filename
1845 vhost_user_init_server_sock (const char *sock_filename, int *sockfd)
1848 struct sockaddr_un un = { };
1850 /* create listening socket */
1851 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1855 return VNET_API_ERROR_SYSCALL_ERROR_1;
1858 un.sun_family = AF_UNIX;
1859 strncpy ((char *) un.sun_path, (char *) sock_filename,
1860 sizeof (un.sun_path) - 1);
1862 /* remove if exists */
1863 unlink ((char *) sock_filename);
1865 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1867 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1871 if (listen (fd, 1) == -1)
1873 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1877 unix_file_t template = { 0 };
1878 template.read_function = vhost_user_socksvr_accept_ready;
1879 template.file_descriptor = fd;
1880 unix_file_add (&unix_main, &template);
1889 // get new vhost_user_intf_t from inactive interfaces or create new one
1890 static vhost_user_intf_t *
1891 vhost_user_vui_new ()
1893 vhost_user_main_t *vum = &vhost_user_main;
1894 vhost_user_intf_t *vui = NULL;
1895 int inactive_cnt = vec_len (vum->vhost_user_inactive_interfaces_index);
1896 // if there are any inactive ifaces
1897 if (inactive_cnt > 0)
1901 vum->vhost_user_inactive_interfaces_index[inactive_cnt - 1];
1902 if (vec_len (vum->vhost_user_interfaces) > vui_idx)
1904 vui = vec_elt_at_index (vum->vhost_user_interfaces, vui_idx);
1905 DBG_SOCK ("reusing inactive vhost-user interface index %d",
1908 // "remove" from inactive list
1909 _vec_len (vum->vhost_user_inactive_interfaces_index) -= 1;
1912 // vui was not retrieved from inactive ifaces - create new
1914 vec_add2 (vum->vhost_user_interfaces, vui, 1);
1918 // create ethernet interface for vhost user intf
1920 vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
1921 vhost_user_intf_t * vui, u8 * hwaddress)
1923 vhost_user_main_t *vum = &vhost_user_main;
1925 clib_error_t *error;
1927 /* create hw and sw interface */
1930 clib_memcpy (hwaddr, hwaddress, 6);
1934 f64 now = vlib_time_now (vm);
1936 rnd = (u32) (now * 1e6);
1937 rnd = random_u32 (&rnd);
1939 clib_memcpy (hwaddr + 2, &rnd, sizeof (rnd));
1944 error = ethernet_register_interface
1946 vhost_user_dev_class.index,
1947 vui - vum->vhost_user_interfaces /* device instance */ ,
1948 hwaddr /* ethernet address */ ,
1949 &vui->hw_if_index, 0 /* flag change */ );
1951 clib_error_report (error);
1953 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
1954 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
1957 // initialize vui with specified attributes
1959 vhost_user_vui_init (vnet_main_t * vnm,
1960 vhost_user_intf_t * vui, int sockfd,
1961 const char *sock_filename,
1962 u8 is_server, u64 feature_mask, u32 * sw_if_index)
1964 vnet_sw_interface_t *sw;
1965 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1966 vlib_thread_main_t *tm = vlib_get_thread_main ();
1969 vui->unix_fd = sockfd;
1970 vui->sw_if_index = sw->sw_if_index;
1971 vui->num_vrings = 2;
1972 vui->sock_is_server = is_server;
1973 strncpy (vui->sock_filename, sock_filename,
1974 ARRAY_LEN (vui->sock_filename) - 1);
1975 vui->sock_errno = 0;
1977 vui->feature_mask = feature_mask;
1979 vui->unix_file_index = ~0;
1980 vui->log_base_addr = 0;
1982 for (q = 0; q < 2; q++)
1984 vui->vrings[q].enabled = 0;
1985 vui->vrings[q].callfd = -1;
1986 vui->vrings[q].kickfd = -1;
1989 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1992 *sw_if_index = vui->sw_if_index;
1994 if (tm->n_vlib_mains > 1)
1996 vui->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1997 CLIB_CACHE_LINE_BYTES);
1998 memset ((void *) vui->lockp, 0, CLIB_CACHE_LINE_BYTES);
2002 // register vui and start polling on it
2004 vhost_user_vui_register (vlib_main_t * vm, vhost_user_intf_t * vui)
2006 vhost_user_main_t *vum = &vhost_user_main;
2008 vlib_thread_main_t *tm = vlib_get_thread_main ();
2010 hash_set (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd,
2011 vui - vum->vhost_user_interfaces);
2012 hash_set (vum->vhost_user_interface_index_by_sw_if_index, vui->sw_if_index,
2013 vui - vum->vhost_user_interfaces);
2016 cpu_index = vum->input_cpu_first_index +
2017 (vui - vum->vhost_user_interfaces) % vum->input_cpu_count;
2019 if (tm->n_vlib_mains == 1)
2020 vlib_node_set_state (vm, vhost_user_input_node.index,
2021 VLIB_NODE_STATE_POLLING);
2023 vlib_node_set_state (vlib_mains[cpu_index], vhost_user_input_node.index,
2024 VLIB_NODE_STATE_POLLING);
2026 /* tell process to start polling for sockets */
2027 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
2031 vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2032 const char *sock_filename,
2036 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
2038 vhost_user_intf_t *vui = NULL;
2045 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0)
2051 vui = vhost_user_vui_new ();
2052 ASSERT (vui != NULL);
2054 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
2055 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
2056 feature_mask, &sw_if_idx);
2060 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2063 vhost_user_vui_register (vm, vui);
2066 *sw_if_index = sw_if_idx;
2072 vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2073 const char *sock_filename,
2076 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
2078 vhost_user_main_t *vum = &vhost_user_main;
2079 vhost_user_intf_t *vui = NULL;
2085 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index, sw_if_index);
2088 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2092 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
2095 // interface is inactive
2097 // disconnect interface sockets
2098 vhost_user_if_disconnect (vui);
2102 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0)
2108 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
2109 feature_mask, &sw_if_idx);
2113 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
2116 vhost_user_vui_register (vm, vui);
2122 vhost_user_connect_command_fn (vlib_main_t * vm,
2123 unformat_input_t * input,
2124 vlib_cli_command_t * cmd)
2126 unformat_input_t _line_input, *line_input = &_line_input;
2127 u8 *sock_filename = NULL;
2130 u64 feature_mask = (u64) ~ 0;
2132 u32 custom_dev_instance = ~0;
2136 /* Get a line of input. */
2137 if (!unformat_user (input, unformat_line_input, line_input))
2140 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2142 if (unformat (line_input, "socket %s", &sock_filename))
2144 else if (unformat (line_input, "server"))
2146 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2150 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
2152 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
2157 return clib_error_return (0, "unknown input `%U'",
2158 format_unformat_error, input);
2160 unformat_free (line_input);
2162 vnet_main_t *vnm = vnet_get_main ();
2165 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
2166 is_server, &sw_if_index, feature_mask,
2167 renumber, custom_dev_instance, hw)))
2169 vec_free (sock_filename);
2170 return clib_error_return (0, "vhost_user_create_if returned %d", rv);
2173 vec_free (sock_filename);
2174 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
2180 vhost_user_delete_command_fn (vlib_main_t * vm,
2181 unformat_input_t * input,
2182 vlib_cli_command_t * cmd)
2184 unformat_input_t _line_input, *line_input = &_line_input;
2185 u32 sw_if_index = ~0;
2187 /* Get a line of input. */
2188 if (!unformat_user (input, unformat_line_input, line_input))
2191 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2193 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
2196 return clib_error_return (0, "unknown input `%U'",
2197 format_unformat_error, input);
2199 unformat_free (line_input);
2201 vnet_main_t *vnm = vnet_get_main ();
2203 vhost_user_delete_if (vnm, vm, sw_if_index);
2209 vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
2210 vhost_user_intf_details_t ** out_vuids)
2213 vhost_user_main_t *vum = &vhost_user_main;
2214 vhost_user_intf_t *vui;
2215 vhost_user_intf_details_t *r_vuids = NULL;
2216 vhost_user_intf_details_t *vuid = NULL;
2217 u32 *hw_if_indices = 0;
2218 vnet_hw_interface_t *hi;
2225 vec_foreach (vui, vum->vhost_user_interfaces)
2228 vec_add1 (hw_if_indices, vui->hw_if_index);
2231 for (i = 0; i < vec_len (hw_if_indices); i++)
2233 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2234 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2236 vec_add2 (r_vuids, vuid, 1);
2237 vuid->sw_if_index = vui->sw_if_index;
2238 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
2239 vuid->features = vui->features;
2240 vuid->is_server = vui->sock_is_server;
2241 vuid->num_regions = vui->nregions;
2242 vuid->sock_errno = vui->sock_errno;
2243 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
2244 ARRAY_LEN (vuid->sock_filename) - 1);
2246 s = format (s, "%v%c", hi->name, 0);
2248 strncpy ((char *) vuid->if_name, (char *) s,
2249 ARRAY_LEN (vuid->if_name) - 1);
2254 vec_free (hw_if_indices);
2256 *out_vuids = r_vuids;
2262 show_vhost_user_command_fn (vlib_main_t * vm,
2263 unformat_input_t * input,
2264 vlib_cli_command_t * cmd)
2266 clib_error_t *error = 0;
2267 vnet_main_t *vnm = vnet_get_main ();
2268 vhost_user_main_t *vum = &vhost_user_main;
2269 vhost_user_intf_t *vui;
2270 u32 hw_if_index, *hw_if_indices = 0;
2271 vnet_hw_interface_t *hi;
2279 struct feat_struct *feat_entry;
2281 static struct feat_struct feat_array[] = {
2282 #define _(s,b) { .str = #s, .bit = b, },
2283 foreach_virtio_net_feature
2288 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2291 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2293 vec_add1 (hw_if_indices, hw_if_index);
2294 vlib_cli_output (vm, "add %d", hw_if_index);
2296 else if (unformat (input, "descriptors") || unformat (input, "desc"))
2300 error = clib_error_return (0, "unknown input `%U'",
2301 format_unformat_error, input);
2305 if (vec_len (hw_if_indices) == 0)
2307 vec_foreach (vui, vum->vhost_user_interfaces)
2310 vec_add1 (hw_if_indices, vui->hw_if_index);
2313 vlib_cli_output (vm, "Virtio vhost-user interfaces");
2314 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
2315 vum->coalesce_frames, vum->coalesce_time);
2317 for (i = 0; i < vec_len (hw_if_indices); i++)
2319 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2320 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
2321 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
2322 hi->name, hw_if_indices[i]);
2324 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
2325 vui->virtio_net_hdr_sz, vui->features);
2327 feat_entry = (struct feat_struct *) &feat_array;
2328 while (feat_entry->str)
2330 if (vui->features & (1 << feat_entry->bit))
2331 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2336 vlib_cli_output (vm, "\n");
2339 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2341 vui->sock_is_server ? "server" : "client",
2342 strerror (vui->sock_errno));
2344 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2348 vlib_cli_output (vm,
2349 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2350 vlib_cli_output (vm,
2351 " ====== ===== ================== ================== ================== ================== ==================\n");
2353 for (j = 0; j < vui->nregions; j++)
2355 vlib_cli_output (vm,
2356 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2357 j, vui->region_mmap_fd[j],
2358 vui->regions[j].guest_phys_addr,
2359 vui->regions[j].memory_size,
2360 vui->regions[j].userspace_addr,
2361 vui->regions[j].mmap_offset,
2362 pointer_to_uword (vui->region_mmap_addr[j]));
2364 for (q = 0; q < vui->num_vrings; q++)
2366 vlib_cli_output (vm, "\n Virtqueue %d\n", q);
2368 vlib_cli_output (vm,
2369 " qsz %d last_avail_idx %d last_used_idx %d\n",
2370 vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
2371 vui->vrings[q].last_used_idx);
2373 if (vui->vrings[q].avail && vui->vrings[q].used)
2374 vlib_cli_output (vm,
2375 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
2376 vui->vrings[q].avail->flags,
2377 vui->vrings[q].avail->idx,
2378 vui->vrings[q].used->flags,
2379 vui->vrings[q].used->idx);
2381 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
2382 vui->vrings[q].kickfd,
2383 vui->vrings[q].callfd, vui->vrings[q].errfd);
2387 vlib_cli_output (vm, "\n descriptor table:\n");
2388 vlib_cli_output (vm,
2389 " id addr len flags next user_addr\n");
2390 vlib_cli_output (vm,
2391 " ===== ================== ===== ====== ===== ==================\n");
2392 for (j = 0; j < vui->vrings[q].qsz; j++)
2395 vlib_cli_output (vm,
2396 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
2397 j, vui->vrings[q].desc[j].addr,
2398 vui->vrings[q].desc[j].len,
2399 vui->vrings[q].desc[j].flags,
2400 vui->vrings[q].desc[j].next,
2401 pointer_to_uword (map_guest_mem
2403 vui->vrings[q].desc[j].
2408 vlib_cli_output (vm, "\n");
2411 vec_free (hw_if_indices);
2421 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2422 .path = "create vhost-user",
2423 .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [renumber <dev_instance>]",
2424 .function = vhost_user_connect_command_fn,
2427 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2428 .path = "delete vhost-user",
2429 .short_help = "delete vhost-user sw_if_index <nn>",
2430 .function = vhost_user_delete_command_fn,
2433 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2434 .path = "show vhost-user",
2435 .short_help = "show vhost-user interface",
2436 .function = show_vhost_user_command_fn,
2441 static clib_error_t *
2442 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
2444 vhost_user_main_t *vum = &vhost_user_main;
2446 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2448 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2450 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2452 else if (unformat (input, "dont-dump-memory"))
2453 vum->dont_dump_vhost_user_memory = 1;
2455 return clib_error_return (0, "unknown input `%U'",
2456 format_unformat_error, input);
2462 /* vhost-user { ... } configuration. */
2463 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2466 vhost_user_unmap_all (void)
2468 vhost_user_main_t *vum = &vhost_user_main;
2469 vhost_user_intf_t *vui;
2471 if (vum->dont_dump_vhost_user_memory)
2473 vec_foreach (vui, vum->vhost_user_interfaces)
2475 unmap_all_mem_regions (vui);
2481 * fd.io coding-style-patch-verification: ON
2484 * eval: (c-set-style "gnu")