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 _(MMAP_FAIL, "mmap failure")
70 #define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
71 foreach_vhost_user_tx_func_error
73 VHOST_USER_TX_FUNC_N_ERROR,
74 } vhost_user_tx_func_error_t;
76 static char * vhost_user_tx_func_error_strings[] = {
78 foreach_vhost_user_tx_func_error
82 #define foreach_vhost_user_input_func_error \
83 _(NO_ERROR, "no error") \
84 _(NO_BUFFER, "no available buffer") \
85 _(MMAP_FAIL, "mmap failure") \
86 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)")
89 #define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
90 foreach_vhost_user_input_func_error
92 VHOST_USER_INPUT_FUNC_N_ERROR,
93 } vhost_user_input_func_error_t;
95 static char * vhost_user_input_func_error_strings[] = {
97 foreach_vhost_user_input_func_error
101 static vhost_user_main_t vhost_user_main = {
105 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
106 .name = "vhost-user",
109 static u8 * format_vhost_user_interface_name (u8 * s, va_list * args)
111 u32 i = va_arg (*args, u32);
112 u32 show_dev_instance = ~0;
113 vhost_user_main_t * vum = &vhost_user_main;
115 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
116 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
118 if (show_dev_instance != ~0)
119 i = show_dev_instance;
121 s = format (s, "VirtualEthernet0/0/%d", i);
125 static int vhost_user_name_renumber (vnet_hw_interface_t * hi,
126 u32 new_dev_instance)
128 vhost_user_main_t * vum = &vhost_user_main;
130 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
131 hi->dev_instance, ~0);
133 vum->show_dev_instance_by_real_dev_instance [hi->dev_instance] =
136 DBG_SOCK("renumbered vhost-user interface dev_instance %d to %d",
137 hi->dev_instance, new_dev_instance);
143 static inline void * map_guest_mem(vhost_user_intf_t * vui, u64 addr)
146 for (i=0; i<vui->nregions; i++) {
147 if ((vui->regions[i].guest_phys_addr <= addr) &&
148 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) > addr)) {
149 return (void *) (vui->region_mmap_addr[i] + addr - vui->regions[i].guest_phys_addr);
152 DBG_VQ("failed to map guest mem addr %llx", addr);
156 static inline void * map_user_mem(vhost_user_intf_t * vui, u64 addr)
159 for (i=0; i<vui->nregions; i++) {
160 if ((vui->regions[i].userspace_addr <= addr) &&
161 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) > addr)) {
162 return (void *) (vui->region_mmap_addr[i] + addr - vui->regions[i].userspace_addr);
168 static long get_huge_page_size(int fd)
175 static void unmap_all_mem_regions(vhost_user_intf_t * vui)
178 for (i=0; i<vui->nregions; i++) {
179 if (vui->region_mmap_addr[i] != (void *) -1) {
181 long page_sz = get_huge_page_size(vui->region_mmap_fd[i]);
183 ssize_t map_sz = (vui->regions[i].memory_size +
184 vui->regions[i].mmap_offset + page_sz) & ~(page_sz - 1);
186 r = munmap(vui->region_mmap_addr[i] - vui->regions[i].mmap_offset, map_sz);
188 DBG_SOCK("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
189 vui->region_mmap_addr[i], map_sz, page_sz);
191 vui->region_mmap_addr[i]= (void *) -1;
194 clib_warning("failed to unmap memory region (errno %d)", errno);
196 close(vui->region_mmap_fd[i]);
203 static clib_error_t * vhost_user_callfd_read_ready (unix_file_t * uf)
205 __attribute__((unused)) int n;
207 n = read(uf->file_descriptor, ((char*)&buff), 8);
211 static inline void vhost_user_if_disconnect(vhost_user_intf_t * vui)
213 vhost_user_main_t * vum = &vhost_user_main;
214 vnet_main_t * vnm = vnet_get_main();
217 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
219 if (vui->unix_file_index != ~0) {
220 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
221 vui->unix_file_index = ~0;
224 hash_unset(vum->vhost_user_interface_index_by_sock_fd, vui->unix_fd);
225 hash_unset(vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd);
229 for (q = 0; q < vui->num_vrings; q++) {
230 vui->vrings[q].desc = NULL;
231 vui->vrings[q].avail = NULL;
232 vui->vrings[q].used = NULL;
235 unmap_all_mem_regions(vui);
236 DBG_SOCK("interface ifindex %d disconnected", vui->sw_if_index);
239 static clib_error_t * vhost_user_socket_read (unix_file_t * uf)
242 int fd, number_of_fds = 0;
243 int fds[VHOST_MEMORY_MAX_NREGIONS];
244 vhost_user_msg_t msg;
247 vhost_user_main_t * vum = &vhost_user_main;
248 vhost_user_intf_t * vui;
249 struct cmsghdr *cmsg;
252 unix_file_t template = {0};
253 vnet_main_t * vnm = vnet_get_main();
255 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
256 uf->file_descriptor);
258 DBG_SOCK ("FD %d doesn't belong to any interface",
259 uf->file_descriptor);
263 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
265 char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))];
267 memset(&mh, 0, sizeof(mh));
268 memset(control, 0, sizeof(control));
270 /* set the payload */
271 iov[0].iov_base = (void *) &msg;
272 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
276 mh.msg_control = control;
277 mh.msg_controllen = sizeof(control);
279 n = recvmsg(uf->file_descriptor, &mh, 0);
281 if (n != VHOST_USER_MSG_HDR_SZ)
284 if (mh.msg_flags & MSG_CTRUNC) {
288 cmsg = CMSG_FIRSTHDR(&mh);
290 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
291 (cmsg->cmsg_type == SCM_RIGHTS) &&
292 (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
293 number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
294 memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
297 /* version 1, no reply bit set*/
298 if ((msg.flags & 7) != 1) {
299 DBG_SOCK("malformed message received. closing socket");
304 int rv __attribute__((unused));
305 /* $$$$ pay attention to rv */
306 rv = read(uf->file_descriptor, ((char*)&msg) + n, msg.size);
309 switch (msg.request) {
310 case VHOST_USER_GET_FEATURES:
311 DBG_SOCK("if %d msg VHOST_USER_GET_FEATURES",
315 msg.u64 = (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
316 (1 << FEAT_VIRTIO_F_ANY_LAYOUT);
317 msg.u64 &= vui->feature_mask;
319 msg.size = sizeof(msg.u64);
322 case VHOST_USER_SET_FEATURES:
323 DBG_SOCK("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
324 vui->hw_if_index, msg.u64);
326 vui->features = msg.u64;
327 if (vui->features & (1 << FEAT_VIRTIO_NET_F_MRG_RXBUF))
328 vui->virtio_net_hdr_sz = 12;
330 vui->virtio_net_hdr_sz = 10;
332 vui->is_any_layout = (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
334 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
335 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
338 for (q = 0; q < 2; q++) {
339 vui->vrings[q].desc = 0;
340 vui->vrings[q].avail = 0;
341 vui->vrings[q].used = 0;
344 DBG_SOCK("interface %d disconnected", vui->sw_if_index);
348 case VHOST_USER_SET_MEM_TABLE:
349 DBG_SOCK("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
350 vui->hw_if_index, msg.memory.nregions);
352 if ((msg.memory.nregions < 1) ||
353 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) {
355 DBG_SOCK("number of mem regions must be between 1 and %i",
356 VHOST_MEMORY_MAX_NREGIONS);
361 if (msg.memory.nregions != number_of_fds) {
362 DBG_SOCK("each memory region must have FD");
365 unmap_all_mem_regions(vui);
366 for(i=0; i < msg.memory.nregions; i++) {
367 memcpy(&(vui->regions[i]), &msg.memory.regions[i],
368 sizeof(vhost_user_memory_region_t));
370 long page_sz = get_huge_page_size(fds[i]);
372 /* align size to 2M page */
373 ssize_t map_sz = (vui->regions[i].memory_size +
374 vui->regions[i].mmap_offset + page_sz) & ~(page_sz - 1);
376 vui->region_mmap_addr[i] = mmap(0, map_sz, PROT_READ | PROT_WRITE,
377 MAP_SHARED, fds[i], 0);
379 DBG_SOCK("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
380 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i], page_sz);
382 if (vui->region_mmap_addr[i] == MAP_FAILED) {
383 clib_warning("failed to map memory. errno is %d", errno);
386 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
387 vui->region_mmap_fd[i] = fds[i];
389 vui->nregions = msg.memory.nregions;
392 case VHOST_USER_SET_VRING_NUM:
393 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
394 vui->hw_if_index, msg.state.index, msg.state.num);
396 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
397 (msg.state.num == 0) || /* it cannot be zero */
398 (msg.state.num % 2)) /* must be power of 2 */
400 vui->vrings[msg.state.index].qsz = msg.state.num;
403 case VHOST_USER_SET_VRING_ADDR:
404 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
405 vui->hw_if_index, msg.state.index);
407 vui->vrings[msg.state.index].desc = (vring_desc_t *)
408 map_user_mem(vui, msg.addr.desc_user_addr);
409 vui->vrings[msg.state.index].used = (vring_used_t *)
410 map_user_mem(vui, msg.addr.used_user_addr);
411 vui->vrings[msg.state.index].avail = (vring_avail_t *)
412 map_user_mem(vui, msg.addr.avail_user_addr);
414 if ((vui->vrings[msg.state.index].desc == NULL) ||
415 (vui->vrings[msg.state.index].used == NULL) ||
416 (vui->vrings[msg.state.index].avail == NULL)) {
417 DBG_SOCK("failed to map user memory for hw_if_index %d",
422 vui->vrings[msg.state.index].last_used_idx =
423 vui->vrings[msg.state.index].used->idx;
425 /* tell driver that we don't want interrupts */
426 vui->vrings[msg.state.index].used->flags |= 1;
429 case VHOST_USER_SET_OWNER:
430 DBG_SOCK("if %d msg VHOST_USER_SET_OWNER",
434 case VHOST_USER_RESET_OWNER:
435 DBG_SOCK("if %d msg VHOST_USER_RESET_OWNER",
439 case VHOST_USER_SET_VRING_CALL:
440 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
441 vui->hw_if_index, msg.u64);
443 q = (u8) (msg.u64 & 0xFF);
445 if (!(msg.u64 & 0x100))
447 if (number_of_fds != 1)
450 /* if there is old fd, delete it */
451 if (vui->vrings[q].callfd) {
452 unix_file_t * uf = pool_elt_at_index (unix_main.file_pool,
453 vui->vrings[q].callfd_idx);
454 unix_file_del (&unix_main, uf);
456 vui->vrings[q].callfd = fds[0];
457 template.read_function = vhost_user_callfd_read_ready;
458 template.file_descriptor = fds[0];
459 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
462 vui->vrings[q].callfd = -1;
465 case VHOST_USER_SET_VRING_KICK:
466 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
467 vui->hw_if_index, msg.u64);
469 q = (u8) (msg.u64 & 0xFF);
471 if (!(msg.u64 & 0x100))
473 if (number_of_fds != 1)
476 vui->vrings[q].kickfd = fds[0];
479 vui->vrings[q].kickfd = -1;
482 case VHOST_USER_SET_VRING_ERR:
483 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
484 vui->hw_if_index, msg.u64);
486 q = (u8) (msg.u64 & 0xFF);
488 if (!(msg.u64 & 0x100))
490 if (number_of_fds != 1)
498 vui->vrings[q].errfd = fd;
501 case VHOST_USER_SET_VRING_BASE:
502 DBG_SOCK("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
503 vui->hw_if_index, msg.state.index, msg.state.num);
505 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
508 case VHOST_USER_GET_VRING_BASE:
509 DBG_SOCK("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
510 vui->hw_if_index, msg.state.index, msg.state.num);
512 msg.state.num = vui->vrings[msg.state.index].last_used_idx;
514 msg.size = sizeof(msg.state);
517 case VHOST_USER_NONE:
518 DBG_SOCK("if %d msg VHOST_USER_NONE",
523 case VHOST_USER_SET_LOG_BASE:
524 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
529 case VHOST_USER_SET_LOG_FD:
530 DBG_SOCK("if %d msg VHOST_USER_SET_LOG_FD",
536 DBG_SOCK("unknown vhost-user message %d received. closing socket",
541 /* if we have pointers to descriptor table, go up*/
543 vui->vrings[VHOST_NET_VRING_IDX_TX].desc &&
544 vui->vrings[VHOST_NET_VRING_IDX_RX].desc) {
546 DBG_SOCK("interface %d connected", vui->sw_if_index);
548 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP);
553 /* if we need to reply */
556 n = send(uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
557 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
564 vhost_user_if_disconnect(vui);
568 static clib_error_t * vhost_user_socket_error (unix_file_t * uf)
570 vhost_user_main_t * vum = &vhost_user_main;
571 vhost_user_intf_t * vui;
574 p = hash_get (vum->vhost_user_interface_index_by_sock_fd,
575 uf->file_descriptor);
577 DBG_SOCK ("fd %d doesn't belong to any interface",
578 uf->file_descriptor);
582 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
584 vhost_user_if_disconnect(vui);
588 static clib_error_t * vhost_user_socksvr_accept_ready (unix_file_t * uf)
590 int client_fd, client_len;
591 struct sockaddr_un client;
592 unix_file_t template = {0};
593 vhost_user_main_t * vum = &vhost_user_main;
594 vhost_user_intf_t * vui;
597 p = hash_get (vum->vhost_user_interface_index_by_listener_fd,
598 uf->file_descriptor);
600 DBG_SOCK ("fd %d doesn't belong to any interface",
601 uf->file_descriptor);
605 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
607 client_len = sizeof(client);
608 client_fd = accept (uf->file_descriptor,
609 (struct sockaddr *)&client,
610 (socklen_t *)&client_len);
613 return clib_error_return_unix (0, "accept");
615 template.read_function = vhost_user_socket_read;
616 template.error_function = vhost_user_socket_error;
617 template.file_descriptor = client_fd;
618 vui->unix_file_index = unix_file_add (&unix_main, &template);
620 vui->client_fd = client_fd;
621 hash_set (vum->vhost_user_interface_index_by_sock_fd, vui->client_fd,
622 vui - vum->vhost_user_interfaces);
627 static clib_error_t *
628 vhost_user_init (vlib_main_t * vm)
630 clib_error_t * error;
631 vhost_user_main_t * vum = &vhost_user_main;
632 vlib_thread_main_t * tm = vlib_get_thread_main();
634 error = vlib_call_init_function (vm, ip4_init);
638 vum->vhost_user_interface_index_by_listener_fd = hash_create (0, sizeof (uword));
639 vum->vhost_user_interface_index_by_sock_fd = hash_create (0, sizeof (uword));
640 vum->vhost_user_interface_index_by_sw_if_index = hash_create (0, sizeof (uword));
641 vum->coalesce_frames = 32;
642 vum->coalesce_time = 1e-3;
644 vec_validate_aligned (vum->rx_buffers, tm->n_vlib_mains - 1,
645 CLIB_CACHE_LINE_BYTES);
650 VLIB_INIT_FUNCTION (vhost_user_init);
652 static clib_error_t *
653 vhost_user_exit (vlib_main_t * vm)
659 VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
662 VHOST_USER_RX_NEXT_ETHERNET_INPUT,
663 VHOST_USER_RX_NEXT_DROP,
664 VHOST_USER_RX_N_NEXT,
671 #if VHOST_USER_COPY_TX_HDR == 1
672 virtio_net_hdr_t hdr;
674 } vhost_user_input_trace_t;
676 static u8 * format_vhost_user_input_trace (u8 * s, va_list * va)
678 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
679 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
680 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main();
681 vhost_user_main_t * vum = &vhost_user_main;
682 vhost_user_input_trace_t * t = va_arg (*va, vhost_user_input_trace_t *);
683 vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces,
686 vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
688 #if VHOST_USER_COPY_TX_HDR == 1
689 uword indent = format_get_indent (s);
692 s = format (s, "%U virtqueue %d",
693 format_vnet_sw_interface_name, vnm, sw,
696 #if VHOST_USER_COPY_TX_HDR == 1
697 s = format (s, "\n%Uvirtio_net_hdr flags 0x%02x gso_type %u hdr_len %u",
698 format_white_space, indent,
707 void vhost_user_rx_trace (vlib_main_t * vm,
708 vlib_node_runtime_t * node,
709 vhost_user_intf_t *vui,
713 vhost_user_main_t * vum = &vhost_user_main;
715 u32 next_index = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
717 n_left = vec_len(vui->d_trace_buffers);
718 b = vui->d_trace_buffers;
724 vhost_user_input_trace_t * t0;
729 b0 = vlib_get_buffer (vm, bi0);
730 vlib_trace_buffer (vm, node, next_index, b0, /* follow_chain */ 0);
731 t0 = vlib_add_trace (vm, node, b0, sizeof (t0[0]));
732 t0->virtqueue = virtqueue;
733 t0->device_index = vui - vum->vhost_user_interfaces;
734 #if VHOST_USER_COPY_TX_HDR == 1
735 rte_memcpy(&t0->hdr, b0->pre_data, sizeof(virtio_net_hdr_t));
742 static inline void vhost_user_send_call(vlib_main_t * vm, vhost_user_vring_t * vq)
744 vhost_user_main_t * vum = &vhost_user_main;
746 int rv __attribute__((unused));
747 /* $$$$ pay attention to rv */
748 rv = write(vq->callfd, &x, sizeof(x));
749 vq->n_since_last_int = 0;
750 vq->int_deadline = vlib_time_now(vm) + vum->coalesce_time;
753 static u32 vhost_user_if_input ( vlib_main_t * vm,
754 vhost_user_main_t * vum,
755 vhost_user_intf_t * vui,
756 vlib_node_runtime_t * node)
758 vhost_user_vring_t * txvq = &vui->vrings[VHOST_NET_VRING_IDX_TX];
759 vhost_user_vring_t * rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
760 uword n_rx_packets = 0, n_rx_bytes = 0;
762 u32 n_left_to_next, * to_next;
765 uword n_trace = vlib_get_trace_count (vm, node);
767 u32 cpu_index, rx_len, drops, flush;
768 f64 now = vlib_time_now (vm);
770 vec_reset_length (vui->d_trace_buffers);
772 /* no descriptor ptr - bail out */
773 if (PREDICT_FALSE(!txvq->desc || !txvq->avail))
776 /* do we have pending intterupts ? */
777 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
778 vhost_user_send_call(vm, txvq);
780 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
781 vhost_user_send_call(vm, rxvq);
783 /* only bit 0 of avail.flags is used so we don't want to deal with this
784 interface if any other bit is set */
785 if (PREDICT_FALSE(txvq->avail->flags & 0xFFFE))
789 if (txvq->avail->idx == txvq->last_avail_idx)
792 if (PREDICT_TRUE(txvq->avail->idx > txvq->last_avail_idx))
793 n_left = txvq->avail->idx - txvq->last_avail_idx;
795 n_left = (u16) -1 - txvq->last_avail_idx + txvq->avail->idx;
797 if (PREDICT_FALSE(!vui->admin_up)) {
798 /* if intf is admin down, just drop all packets waiting in the ring */
799 txvq->last_avail_idx = txvq->last_used_idx = txvq->avail->idx;
800 CLIB_MEMORY_BARRIER();
801 txvq->used->idx = txvq->last_used_idx;
802 vhost_user_send_call(vm, txvq);
806 if (PREDICT_FALSE(n_left > txvq->qsz))
809 qsz_mask = txvq->qsz - 1;
810 cpu_index = os_get_cpu_number();
814 if (n_left > VLIB_FRAME_SIZE)
815 n_left = VLIB_FRAME_SIZE;
817 /* Allocate some buffers.
818 * Note that buffers that are chained for jumbo
819 * frames are allocated separately using a slower path.
820 * The idea is to be certain to have enough buffers at least
821 * to cycle through the descriptors without having to check for errors.
822 * For jumbo frames, the bottleneck is memory copy anyway.
824 if (PREDICT_FALSE(!vum->rx_buffers[cpu_index])) {
825 vec_alloc (vum->rx_buffers[cpu_index], VLIB_FRAME_SIZE);
827 if (PREDICT_FALSE(!vum->rx_buffers[cpu_index]))
828 flush = n_left; //Drop all input
831 if (PREDICT_FALSE(_vec_len(vum->rx_buffers[cpu_index]) < n_left)) {
832 _vec_len(vum->rx_buffers[cpu_index]) +=
833 vlib_buffer_alloc_from_free_list(vm, vum->rx_buffers[cpu_index] + _vec_len(vum->rx_buffers[cpu_index]),
834 VLIB_FRAME_SIZE - _vec_len(vum->rx_buffers[cpu_index]),
835 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
837 if (PREDICT_FALSE(n_left > _vec_len(vum->rx_buffers[cpu_index])))
838 flush = n_left - _vec_len(vum->rx_buffers[cpu_index]);
841 if (PREDICT_FALSE(flush)) {
842 //Remove some input buffers
845 vlib_error_count(vm, vhost_user_input_node.index,
846 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
848 u16 desc_chain_head = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
849 txvq->last_avail_idx++;
850 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
851 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
852 txvq->last_used_idx++;
857 rx_len = vec_len(vum->rx_buffers[cpu_index]); //vector might be null
859 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
861 while (n_left > 0 && n_left_to_next > 0) {
862 vlib_buffer_t *b_head, *b_current;
863 u32 bi_head, bi_current;
864 u16 desc_chain_head, desc_current;
865 u8 error = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR;
867 desc_chain_head = desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
868 bi_head = bi_current = vum->rx_buffers[cpu_index][--rx_len];
869 b_head = b_current = vlib_get_buffer (vm, bi_head);
870 vlib_buffer_chain_init(b_head);
873 if (PREDICT_TRUE(vui->is_any_layout) ||
874 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)) {
875 /* ANYLAYOUT or single buffer */
876 offset = vui->virtio_net_hdr_sz;
878 /* CSR case without ANYLAYOUT, skip 1st buffer */
879 offset = txvq->desc[desc_current].len;
883 void * buffer_addr = map_guest_mem(vui, txvq->desc[desc_current].addr);
884 if (PREDICT_FALSE(buffer_addr == 0)) {
885 error = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL;
889 #if VHOST_USER_COPY_TX_HDR == 1
890 if (PREDICT_TRUE(offset))
891 rte_memcpy(b->pre_data, buffer_addr, sizeof(virtio_net_hdr_t)); /* 12 byte hdr is not used on tx */
894 if (txvq->desc[desc_current].len > offset) {
895 u16 len = txvq->desc[desc_current].len - offset;
896 u16 copied = vlib_buffer_chain_append_data_with_alloc(vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX,
897 b_head, &b_current, buffer_addr + offset, len);
900 error = VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER;
906 /* if next flag is set, take next desc in the chain */
907 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT )
908 desc_current = txvq->desc[desc_current].next;
913 /* consume the descriptor and return it as used */
914 txvq->last_avail_idx++;
915 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
916 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
917 txvq->last_used_idx++;
919 if(PREDICT_FALSE(b_head->current_length < 14 &&
920 error == VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)) {
921 error = VHOST_USER_INPUT_FUNC_ERROR_UNDERSIZED_FRAME;
924 VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b);
926 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
927 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32)~0;
928 b_head->error = node->errors[error];
930 if (PREDICT_FALSE (n_trace > n_rx_packets))
931 vec_add1 (vui->d_trace_buffers, bi_head);
933 if (PREDICT_FALSE(error)) {
935 next0 = VHOST_USER_RX_NEXT_DROP;
937 n_rx_bytes += b_head->current_length + b_head->total_length_not_including_first_buffer;
939 next0 = VHOST_USER_RX_NEXT_ETHERNET_INPUT;
942 to_next[0] = bi_head;
945 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
946 to_next, n_left_to_next,
951 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
954 if (PREDICT_TRUE(vum->rx_buffers[cpu_index] != 0))
955 _vec_len(vum->rx_buffers[cpu_index]) = rx_len;
957 /* give buffers back to driver */
958 CLIB_MEMORY_BARRIER();
959 txvq->used->idx = txvq->last_used_idx;
961 if (PREDICT_FALSE (vec_len (vui->d_trace_buffers) > 0))
963 vhost_user_rx_trace (vm, node, vui, VHOST_NET_VRING_IDX_TX);
964 vlib_set_trace_count (vm, node, n_trace - vec_len (vui->d_trace_buffers));
967 /* interrupt (call) handling */
968 if((txvq->callfd > 0) && !(txvq->avail->flags & 1)) {
969 txvq->n_since_last_int += n_rx_packets;
971 if(txvq->n_since_last_int > vum->coalesce_frames)
972 vhost_user_send_call(vm, txvq);
975 if (PREDICT_FALSE(drops)) {
976 vlib_increment_simple_counter
977 (vnet_main.interface_main.sw_if_counters
978 + VNET_INTERFACE_COUNTER_DROP, os_get_cpu_number(),
979 vui->sw_if_index, drops);
982 /* increase rx counters */
983 vlib_increment_combined_counter
984 (vnet_main.interface_main.combined_sw_if_counters
985 + VNET_INTERFACE_COUNTER_RX,
988 n_rx_packets, n_rx_bytes);
994 vhost_user_input (vlib_main_t * vm,
995 vlib_node_runtime_t * node,
998 vhost_user_main_t * vum = &vhost_user_main;
999 dpdk_main_t * dm = &dpdk_main;
1000 vhost_user_intf_t * vui;
1001 uword n_rx_packets = 0;
1002 u32 cpu_index = os_get_cpu_number();
1005 for(i = 0; i < vec_len(vum->vhost_user_interfaces); i++ )
1007 vui = vec_elt_at_index(vum->vhost_user_interfaces, i);
1009 (i % dm->input_cpu_count) == (cpu_index - dm->input_cpu_first_index))
1010 n_rx_packets += vhost_user_if_input (vm, vum, vui, node);
1012 return n_rx_packets;
1015 VLIB_REGISTER_NODE (vhost_user_input_node) = {
1016 .function = vhost_user_input,
1017 .type = VLIB_NODE_TYPE_INPUT,
1018 .name = "vhost-user-input",
1020 /* Will be enabled if/when hardware is detected. */
1021 .state = VLIB_NODE_STATE_DISABLED,
1023 .format_buffer = format_ethernet_header_with_length,
1024 .format_trace = format_vhost_user_input_trace,
1026 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1027 .error_strings = vhost_user_input_func_error_strings,
1029 .n_next_nodes = VHOST_USER_RX_N_NEXT,
1031 [VHOST_USER_RX_NEXT_DROP] = "error-drop",
1032 [VHOST_USER_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
1037 vhost_user_intfc_tx (vlib_main_t * vm,
1038 vlib_node_runtime_t * node,
1039 vlib_frame_t * frame)
1041 u32 * buffers = vlib_frame_args (frame);
1044 vhost_user_main_t * vum = &vhost_user_main;
1045 uword n_packets = 0;
1046 vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
1047 vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
1048 vhost_user_vring_t * rxvq = &vui->vrings[VHOST_NET_VRING_IDX_RX];
1050 u8 error = VHOST_USER_TX_FUNC_ERROR_NONE;
1052 if (PREDICT_FALSE(!vui->is_up))
1055 if (PREDICT_FALSE(!rxvq->desc || !rxvq->avail || vui->sock_errno != 0)) {
1056 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1060 if (PREDICT_FALSE(vui->lockp != 0))
1062 while (__sync_lock_test_and_set (vui->lockp, 1))
1066 /* only bit 0 of avail.flags is used so we don't want to deal with this
1067 interface if any other bit is set */
1068 if (PREDICT_FALSE(rxvq->avail->flags & 0xFFFE)) {
1069 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
1073 if (PREDICT_FALSE((rxvq->avail->idx == rxvq->last_avail_idx))) {
1074 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1078 n_left = n_packets = frame->n_vectors;
1079 used_index = rxvq->used->idx;
1080 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
1084 vlib_buffer_t *b0, *current_b0;
1085 u16 desc_chain_head, desc_current, desc_len;
1090 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
1092 b0 = vlib_get_buffer (vm, buffers[0]);
1096 if (PREDICT_FALSE(rxvq->last_avail_idx == rxvq->avail->idx)) {
1097 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1101 desc_current = desc_chain_head = rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1102 offset = vui->virtio_net_hdr_sz;
1104 if (PREDICT_FALSE(!(buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr)))) {
1105 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1108 CLIB_PREFETCH(buffer_addr, clib_min(rxvq->desc[desc_current].len, 500), STORE);
1110 virtio_net_hdr_mrg_rxbuf_t * hdr = (virtio_net_hdr_mrg_rxbuf_t *) buffer_addr;
1112 hdr->hdr.gso_type = 0;
1114 if (vui->virtio_net_hdr_sz == 12)
1115 hdr->num_buffers = 1;
1117 u16 bytes_left = b0->current_length;
1118 buffer_addr += offset;
1121 //FIXME: This was in the code but I don't think it is valid
1122 /*if (PREDICT_FALSE(!vui->is_any_layout && (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT))) {
1123 rxvq->desc[desc_current].len = vui->virtio_net_hdr_sz;
1127 if (!bytes_left) { //Get new input
1128 if (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT) {
1129 current_b0 = vlib_get_buffer(vm, current_b0->next_buffer);
1130 bytes_left = current_b0->current_length;
1137 if (rxvq->desc[desc_current].len <= offset) { //Get new output
1138 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) {
1140 desc_current = rxvq->desc[desc_current].next;
1141 if (PREDICT_FALSE(!(buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr)))) {
1142 used_index -= hdr->num_buffers - 1;
1143 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1144 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1147 } else if (vui->virtio_net_hdr_sz == 12) { //MRG is available
1149 //Move from available to used buffer
1150 rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head;
1151 rxvq->used->ring[used_index & qsz_mask].len = desc_len;
1152 rxvq->last_avail_idx++;
1156 if (PREDICT_FALSE(rxvq->last_avail_idx == rxvq->avail->idx)) {
1157 //Dequeue queued descriptors for this packet
1158 used_index -= hdr->num_buffers - 1;
1159 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1160 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1165 desc_chain_head = rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
1166 desc_current = desc_chain_head;
1169 if (PREDICT_FALSE(!(buffer_addr = map_guest_mem(vui, rxvq->desc[desc_current].addr)))) {
1170 //Dequeue queued descriptors for this packet
1171 used_index -= hdr->num_buffers - 1;
1172 rxvq->last_avail_idx -= hdr->num_buffers - 1;
1173 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
1177 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
1182 u16 bytes_to_copy = bytes_left > (rxvq->desc[desc_current].len - offset) ? (rxvq->desc[desc_current].len - offset) : bytes_left;
1183 rte_memcpy(buffer_addr, vlib_buffer_get_current (current_b0) + current_b0->current_length - bytes_left, bytes_to_copy);
1185 bytes_left -= bytes_to_copy;
1186 offset += bytes_to_copy;
1187 buffer_addr += bytes_to_copy;
1188 desc_len += bytes_to_copy;
1191 //Move from available to used ring
1192 rxvq->used->ring[used_index & qsz_mask].id = desc_chain_head;
1193 rxvq->used->ring[used_index & qsz_mask].len = desc_len;
1194 rxvq->last_avail_idx++;
1199 CLIB_MEMORY_BARRIER();
1200 rxvq->used->idx = used_index;
1202 /* interrupt (call) handling */
1203 if((rxvq->callfd > 0) && !(rxvq->avail->flags & 1)) {
1204 rxvq->n_since_last_int += n_packets - n_left;
1206 if(rxvq->n_since_last_int > vum->coalesce_frames)
1207 vhost_user_send_call(vm, rxvq);
1212 if (PREDICT_FALSE(vui->lockp != 0))
1215 if (PREDICT_FALSE(n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE)) {
1216 vlib_error_count(vm, node->node_index, error, n_left);
1217 vlib_increment_simple_counter
1218 (vnet_main.interface_main.sw_if_counters
1219 + VNET_INTERFACE_COUNTER_DROP,
1220 os_get_cpu_number(),
1225 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
1226 return frame->n_vectors;
1229 static clib_error_t *
1230 vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1232 vnet_hw_interface_t * hif = vnet_get_hw_interface (vnm, hw_if_index);
1233 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
1234 vhost_user_main_t * vum = &vhost_user_main;
1235 vhost_user_intf_t * vui = vec_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
1237 vui->admin_up = is_up;
1240 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
1241 VNET_HW_INTERFACE_FLAG_LINK_UP);
1243 return /* no error */ 0;
1246 VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
1247 .name = "vhost-user",
1248 .tx_function = vhost_user_intfc_tx,
1249 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
1250 .tx_function_error_strings = vhost_user_tx_func_error_strings,
1251 .format_device_name = format_vhost_user_interface_name,
1252 .name_renumber = vhost_user_name_renumber,
1253 .admin_up_down_function = vhost_user_interface_admin_up_down,
1254 .no_flatten_output_chains = 1,
1258 vhost_user_process (vlib_main_t * vm,
1259 vlib_node_runtime_t * rt,
1262 vhost_user_main_t * vum = &vhost_user_main;
1263 vhost_user_intf_t * vui;
1264 struct sockaddr_un sun;
1266 unix_file_t template = {0};
1267 f64 timeout = 3153600000.0 /* 100 years */;
1268 uword *event_data = 0;
1270 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1271 sun.sun_family = AF_UNIX;
1272 template.read_function = vhost_user_socket_read;
1273 template.error_function = vhost_user_socket_error;
1280 vlib_process_wait_for_event_or_clock (vm, timeout);
1281 vlib_process_get_events (vm, &event_data);
1282 vec_reset_length (event_data);
1286 vec_foreach (vui, vum->vhost_user_interfaces) {
1288 if (vui->sock_is_server || !vui->active)
1291 if (vui->unix_fd == -1) {
1292 /* try to connect */
1294 strncpy(sun.sun_path, (char *) vui->sock_filename, sizeof(sun.sun_path) - 1);
1296 if (connect(sockfd, (struct sockaddr *) &sun, sizeof(struct sockaddr_un)) == 0) {
1297 vui->sock_errno = 0;
1298 vui->unix_fd = sockfd;
1299 template.file_descriptor = sockfd;
1300 vui->unix_file_index = unix_file_add (&unix_main, &template);
1301 hash_set (vum->vhost_user_interface_index_by_sock_fd, sockfd, vui - vum->vhost_user_interfaces);
1303 sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1308 vui->sock_errno = errno;
1311 /* check if socket is alive */
1313 socklen_t len = sizeof (error);
1314 int retval = getsockopt(vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1317 vhost_user_if_disconnect(vui);
1324 VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
1325 .function = vhost_user_process,
1326 .type = VLIB_NODE_TYPE_PROCESS,
1327 .name = "vhost-user-process",
1330 int vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm,
1333 vhost_user_main_t * vum = &vhost_user_main;
1334 vhost_user_intf_t * vui;
1338 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index,
1341 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1343 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1346 // interface is inactive
1348 // disconnect interface sockets
1349 vhost_user_if_disconnect(vui);
1350 // add to inactive interface list
1351 vec_add1 (vum->vhost_user_inactive_interfaces_index, p[0]);
1353 // reset renumbered iface
1354 if (p[0] < vec_len (vum->show_dev_instance_by_real_dev_instance))
1355 vum->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
1357 ethernet_delete_interface (vnm, vui->hw_if_index);
1358 DBG_SOCK ("deleted (deactivated) vhost-user interface instance %d", p[0]);
1363 // init server socket on specified sock_filename
1364 static int vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
1367 struct sockaddr_un un;
1369 /* create listening socket */
1370 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1373 return VNET_API_ERROR_SYSCALL_ERROR_1;
1376 un.sun_family = AF_UNIX;
1377 strcpy((char *) un.sun_path, (char *) sock_filename);
1379 /* remove if exists */
1380 unlink( (char *) sock_filename);
1382 len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
1384 if (bind(fd, (struct sockaddr *) &un, len) == -1) {
1385 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1389 if (listen(fd, 1) == -1) {
1390 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1394 unix_file_t template = {0};
1395 template.read_function = vhost_user_socksvr_accept_ready;
1396 template.file_descriptor = fd;
1397 unix_file_add (&unix_main, &template);
1406 // get new vhost_user_intf_t from inactive interfaces or create new one
1407 static vhost_user_intf_t *vhost_user_vui_new()
1409 vhost_user_main_t * vum = &vhost_user_main;
1410 vhost_user_intf_t * vui = NULL;
1411 int inactive_cnt = vec_len(vum->vhost_user_inactive_interfaces_index);
1412 // if there are any inactive ifaces
1413 if (inactive_cnt > 0) {
1415 u32 vui_idx = vum->vhost_user_inactive_interfaces_index[inactive_cnt - 1];
1416 if (vec_len(vum->vhost_user_interfaces) > vui_idx) {
1417 vui = vec_elt_at_index (vum->vhost_user_interfaces, vui_idx);
1418 DBG_SOCK("reusing inactive vhost-user interface index %d", vui_idx);
1420 // "remove" from inactive list
1421 _vec_len(vum->vhost_user_inactive_interfaces_index) -= 1;
1424 // vui was not retrieved from inactive ifaces - create new
1426 vec_add2 (vum->vhost_user_interfaces, vui, 1);
1430 // create ethernet interface for vhost user intf
1431 static void vhost_user_create_ethernet(vnet_main_t * vnm, vlib_main_t * vm,
1432 vhost_user_intf_t *vui)
1434 vhost_user_main_t * vum = &vhost_user_main;
1436 clib_error_t * error;
1438 /* create hw and sw interface */
1440 f64 now = vlib_time_now(vm);
1442 rnd = (u32) (now * 1e6);
1443 rnd = random_u32 (&rnd);
1445 memcpy (hwaddr+2, &rnd, sizeof(rnd));
1450 error = ethernet_register_interface
1452 vhost_user_dev_class.index,
1453 vui - vum->vhost_user_interfaces /* device instance */,
1454 hwaddr /* ethernet address */,
1456 0 /* flag change */);
1458 clib_error_report (error);
1460 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
1461 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
1464 // initialize vui with specified attributes
1465 static void vhost_user_vui_init(vnet_main_t * vnm,
1466 vhost_user_intf_t *vui, int sockfd,
1467 const char * sock_filename,
1468 u8 is_server, u64 feature_mask,
1471 vnet_sw_interface_t * sw;
1472 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1473 vlib_thread_main_t * tm = vlib_get_thread_main();
1475 vui->unix_fd = sockfd;
1476 vui->sw_if_index = sw->sw_if_index;
1477 vui->num_vrings = 2;
1478 vui->sock_is_server = is_server;
1479 strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
1480 vui->sock_errno = 0;
1482 vui->feature_mask = feature_mask;
1484 vui->unix_file_index = ~0;
1486 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
1489 *sw_if_index = vui->sw_if_index;
1491 if (tm->n_vlib_mains > 1)
1493 vui->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
1494 CLIB_CACHE_LINE_BYTES);
1495 memset ((void *) vui->lockp, 0, CLIB_CACHE_LINE_BYTES);
1499 // register vui and start polling on it
1500 static void vhost_user_vui_register(vlib_main_t * vm, vhost_user_intf_t *vui)
1502 vhost_user_main_t * vum = &vhost_user_main;
1503 dpdk_main_t * dm = &dpdk_main;
1505 vlib_thread_main_t * tm = vlib_get_thread_main();
1507 hash_set (vum->vhost_user_interface_index_by_listener_fd, vui->unix_fd,
1508 vui - vum->vhost_user_interfaces);
1509 hash_set (vum->vhost_user_interface_index_by_sw_if_index, vui->sw_if_index,
1510 vui - vum->vhost_user_interfaces);
1513 cpu_index = dm->input_cpu_first_index +
1514 (vui - vum->vhost_user_interfaces) % dm->input_cpu_count;
1516 if (tm->n_vlib_mains == 1)
1517 vlib_node_set_state (vm, vhost_user_input_node.index,
1518 VLIB_NODE_STATE_POLLING);
1519 else if (!dm->have_io_threads)
1520 vlib_node_set_state (vlib_mains[cpu_index], vhost_user_input_node.index,
1521 VLIB_NODE_STATE_POLLING);
1523 /* tell process to start polling for sockets */
1524 vlib_process_signal_event(vm, vhost_user_process_node.index, 0, 0);
1527 int vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm,
1528 const char * sock_filename,
1532 u8 renumber, u32 custom_dev_instance)
1534 vhost_user_intf_t * vui = NULL;
1535 dpdk_main_t * dm = &dpdk_main;
1536 vlib_thread_main_t * tm = vlib_get_thread_main();
1541 if (tm->n_vlib_mains > 1 && dm->have_io_threads)
1543 clib_warning("vhost-user interfaces are not supported with multiple io threads");
1548 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1553 vui = vhost_user_vui_new ();
1554 ASSERT(vui != NULL);
1556 vhost_user_create_ethernet (vnm, vm, vui);
1557 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1558 feature_mask, &sw_if_idx);
1561 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1564 vhost_user_vui_register (vm, vui);
1567 *sw_if_index = sw_if_idx;
1572 int vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
1573 const char * sock_filename,
1577 u8 renumber, u32 custom_dev_instance)
1579 vhost_user_main_t * vum = &vhost_user_main;
1580 vhost_user_intf_t * vui = NULL;
1586 p = hash_get (vum->vhost_user_interface_index_by_sw_if_index,
1589 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1591 vui = vec_elt_at_index (vum->vhost_user_interfaces, p[0]);
1594 // interface is inactive
1596 // disconnect interface sockets
1597 vhost_user_if_disconnect(vui);
1600 if ((rv = vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1605 vhost_user_vui_init (vnm, vui, sockfd, sock_filename, is_server,
1606 feature_mask, &sw_if_idx);
1609 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1612 vhost_user_vui_register (vm, vui);
1618 vhost_user_connect_command_fn (vlib_main_t * vm,
1619 unformat_input_t * input,
1620 vlib_cli_command_t * cmd)
1622 unformat_input_t _line_input, * line_input = &_line_input;
1623 u8 * sock_filename = NULL;
1626 u64 feature_mask = (u64)~0;
1628 u32 custom_dev_instance = ~0;
1630 /* Get a line of input. */
1631 if (! unformat_user (input, unformat_line_input, line_input))
1634 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1635 if (unformat (line_input, "socket %s", &sock_filename))
1637 else if (unformat (line_input, "server"))
1639 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1641 else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
1645 return clib_error_return (0, "unknown input `%U'",
1646 format_unformat_error, input);
1648 unformat_free (line_input);
1650 vnet_main_t *vnm = vnet_get_main();
1652 vhost_user_create_if(vnm, vm, (char *)sock_filename,
1653 is_server, &sw_if_index, feature_mask,
1654 renumber, custom_dev_instance);
1656 vec_free(sock_filename);
1662 vhost_user_delete_command_fn (vlib_main_t * vm,
1663 unformat_input_t * input,
1664 vlib_cli_command_t * cmd)
1666 unformat_input_t _line_input, * line_input = &_line_input;
1667 u32 sw_if_index = ~0;
1669 /* Get a line of input. */
1670 if (! unformat_user (input, unformat_line_input, line_input))
1673 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1674 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1677 return clib_error_return (0, "unknown input `%U'",
1678 format_unformat_error, input);
1680 unformat_free (line_input);
1682 vnet_main_t *vnm = vnet_get_main();
1684 vhost_user_delete_if(vnm, vm, sw_if_index);
1689 int vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm, vhost_user_intf_details_t **out_vuids)
1692 vhost_user_main_t * vum = &vhost_user_main;
1693 vhost_user_intf_t * vui;
1694 vhost_user_intf_details_t * r_vuids = NULL;
1695 vhost_user_intf_details_t * vuid = NULL;
1696 u32 * hw_if_indices = 0;
1697 vnet_hw_interface_t * hi;
1704 vec_foreach (vui, vum->vhost_user_interfaces) {
1706 vec_add1(hw_if_indices, vui->hw_if_index);
1709 for (i = 0; i < vec_len (hw_if_indices); i++) {
1710 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1711 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1713 vec_add2(r_vuids, vuid, 1);
1714 vuid->sw_if_index = vui->sw_if_index;
1715 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1716 vuid->features = vui->features;
1717 vuid->is_server = vui->sock_is_server;
1718 vuid->num_regions = vui->nregions;
1719 vuid->sock_errno = vui->sock_errno;
1720 strncpy((char *)vuid->sock_filename, (char *)vui->sock_filename,
1721 ARRAY_LEN(vuid->sock_filename)-1);
1723 s = format (s, "%v%c", hi->name, 0);
1725 strncpy((char *)vuid->if_name, (char *)s,
1726 ARRAY_LEN(vuid->if_name)-1);
1731 vec_free (hw_if_indices);
1733 *out_vuids = r_vuids;
1739 show_vhost_user_command_fn (vlib_main_t * vm,
1740 unformat_input_t * input,
1741 vlib_cli_command_t * cmd)
1743 clib_error_t * error = 0;
1744 vnet_main_t * vnm = vnet_get_main();
1745 vhost_user_main_t * vum = &vhost_user_main;
1746 vhost_user_intf_t * vui;
1747 u32 hw_if_index, * hw_if_indices = 0;
1748 vnet_hw_interface_t * hi;
1751 struct feat_struct { u8 bit; char *str;};
1752 struct feat_struct *feat_entry;
1754 static struct feat_struct feat_array[] = {
1755 #define _(s,b) { .str = #s, .bit = b, },
1756 foreach_virtio_net_feature
1761 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1762 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) {
1763 vec_add1 (hw_if_indices, hw_if_index);
1764 vlib_cli_output(vm, "add %d", hw_if_index);
1766 else if (unformat (input, "descriptors") || unformat (input, "desc") )
1769 error = clib_error_return (0, "unknown input `%U'",
1770 format_unformat_error, input);
1774 if (vec_len (hw_if_indices) == 0) {
1775 vec_foreach (vui, vum->vhost_user_interfaces) {
1777 vec_add1(hw_if_indices, vui->hw_if_index);
1780 vlib_cli_output (vm, "Virtio vhost-user interfaces");
1781 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
1782 vum->coalesce_frames, vum->coalesce_time);
1784 for (i = 0; i < vec_len (hw_if_indices); i++) {
1785 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1786 vui = vec_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
1787 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
1788 hi->name, hw_if_indices[i]);
1790 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
1791 vui->virtio_net_hdr_sz, vui->features);
1793 feat_entry = (struct feat_struct *) &feat_array;
1794 while(feat_entry->str) {
1795 if (vui->features & (1 << feat_entry->bit))
1796 vlib_cli_output (vm, " %s (%d)", feat_entry->str, feat_entry->bit);
1800 vlib_cli_output (vm, "\n");
1803 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1804 vui->sock_filename, vui->sock_is_server ? "server" : "client",
1805 strerror(vui->sock_errno));
1807 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1810 vlib_cli_output(vm, " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1811 vlib_cli_output(vm, " ====== ===== ================== ================== ================== ================== ==================\n");
1813 for (j = 0; j < vui->nregions; j++) {
1814 vlib_cli_output(vm, " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", j,
1815 vui->region_mmap_fd[j],
1816 vui->regions[j].guest_phys_addr,
1817 vui->regions[j].memory_size,
1818 vui->regions[j].userspace_addr,
1819 vui->regions[j].mmap_offset,
1820 (u64) vui->region_mmap_addr[j]);
1822 for (q = 0; q < vui->num_vrings; q++) {
1823 vlib_cli_output(vm, "\n Virtqueue %d\n", q);
1825 vlib_cli_output(vm, " qsz %d last_avail_idx %d last_used_idx %d\n",
1827 vui->vrings[q].last_avail_idx,
1828 vui->vrings[q].last_used_idx);
1830 if (vui->vrings[q].avail && vui->vrings[q].used)
1831 vlib_cli_output(vm, " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1832 vui->vrings[q].avail->flags,
1833 vui->vrings[q].avail->idx,
1834 vui->vrings[q].used->flags,
1835 vui->vrings[q].used->idx);
1837 vlib_cli_output(vm, " kickfd %d callfd %d errfd %d\n",
1838 vui->vrings[q].kickfd,
1839 vui->vrings[q].callfd,
1840 vui->vrings[q].errfd);
1843 vlib_cli_output(vm, "\n descriptor table:\n");
1844 vlib_cli_output(vm, " id addr len flags next user_addr\n");
1845 vlib_cli_output(vm, " ===== ================== ===== ====== ===== ==================\n");
1846 for(j = 0; j < vui->vrings[q].qsz; j++) {
1847 vlib_cli_output(vm, " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1849 vui->vrings[q].desc[j].addr,
1850 vui->vrings[q].desc[j].len,
1851 vui->vrings[q].desc[j].flags,
1852 vui->vrings[q].desc[j].next,
1853 (u64) map_guest_mem(vui, vui->vrings[q].desc[j].addr));}
1856 vlib_cli_output (vm, "\n");
1859 vec_free (hw_if_indices);
1863 static clib_error_t *
1864 vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
1866 vhost_user_main_t * vum = &vhost_user_main;
1868 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1870 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
1872 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
1874 else if (unformat (input, "dont-dump-memory"))
1875 vum->dont_dump_vhost_user_memory = 1;
1877 return clib_error_return (0, "unknown input `%U'",
1878 format_unformat_error, input);
1884 /* vhost-user { ... } configuration. */
1885 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
1888 vhost_user_unmap_all (void)
1890 vhost_user_main_t * vum = &vhost_user_main;
1891 vhost_user_intf_t * vui;
1893 if (vum->dont_dump_vhost_user_memory)
1895 vec_foreach (vui, vum->vhost_user_interfaces)
1897 unmap_all_mem_regions(vui);