FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
vhost_user.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * vhost.c - vhost-user
4  *
5  * Copyright (c) 2014-2018 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:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19 
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <sys/uio.h> /* for iovec */
27 #include <netinet/in.h>
28 #include <sys/vfs.h>
29 
30 #include <linux/if_arp.h>
31 #include <linux/if_tun.h>
32 
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
35 
36 #include <vnet/ip/ip.h>
37 
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/devices/devices.h>
40 #include <vnet/feature/feature.h>
41 
44 
45 /**
46  * @file
47  * @brief vHost User Device Driver.
48  *
49  * This file contains the source code for vHost User interface.
50  */
51 
52 
54 
55 /* *INDENT-OFF* */
56 vhost_user_main_t vhost_user_main = {
57  .mtu_bytes = 1518,
58 };
59 
60 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
61  .name = "vhost-user",
62 };
63 /* *INDENT-ON* */
64 
65 static long
67 {
68  struct statfs s;
69  fstatfs (fd, &s);
70  return s.f_bsize;
71 }
72 
73 static void
75 {
76  int i, r, q;
78 
79  for (i = 0; i < vui->nregions; i++)
80  {
81  if (vui->region_mmap_addr[i] != MAP_FAILED)
82  {
83 
84  long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
85 
86  ssize_t map_sz = (vui->regions[i].memory_size +
87  vui->regions[i].mmap_offset +
88  page_sz - 1) & ~(page_sz - 1);
89 
90  r =
91  munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
92  map_sz);
93 
94  vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
95  "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
96  page_sz);
97 
98  vui->region_mmap_addr[i] = MAP_FAILED;
99 
100  if (r == -1)
101  {
102  vu_log_err (vui, "failed to unmap memory region (errno %d)",
103  errno);
104  }
105  close (vui->region_mmap_fd[i]);
106  }
107  }
108  vui->nregions = 0;
109 
110  for (q = 0; q < VHOST_VRING_MAX_N; q++)
111  {
112  vq = &vui->vrings[q];
113  vq->avail = 0;
114  vq->used = 0;
115  vq->desc = 0;
116  }
117 }
118 
121 {
122  //Let's try to assign one queue to each thread
123  u32 qid;
124  u32 thread_index = 0;
125 
126  vui->use_tx_spinlock = 0;
127  while (1)
128  {
129  for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
130  {
131  vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
132  if (!rxvq->started || !rxvq->enabled)
133  continue;
134 
135  vui->per_cpu_tx_qid[thread_index] = qid;
136  thread_index++;
137  if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
138  return;
139  }
140  //We need to loop, meaning the spinlock has to be used
141  vui->use_tx_spinlock = 1;
142  if (thread_index == 0)
143  {
144  //Could not find a single valid one
145  for (thread_index = 0;
146  thread_index < vlib_get_thread_main ()->n_vlib_mains;
147  thread_index++)
148  {
149  vui->per_cpu_tx_qid[thread_index] = 0;
150  }
151  return;
152  }
153  }
154 }
155 
156 /**
157  * @brief Unassign existing interface/queue to thread mappings and re-assign
158  * new interface/queue to thread mappings
159  */
162 {
163  vhost_user_vring_t *txvq = &vui->vrings[qid];
164  vnet_main_t *vnm = vnet_get_main ();
165  int rv;
166  u32 q = qid >> 1;
167 
168  ASSERT ((qid & 1) == 1); // should be odd
169  // Assign new queue mappings for the interface
171  vhost_user_input_node.index);
174  /* Set polling as the default */
176  txvq->qid = q;
177  rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, q, txvq->mode);
178  if (rv)
179  vu_log_warn (vui, "unable to set rx mode for interface %d, "
180  "queue %d: rc=%d", vui->hw_if_index, q, rv);
181 }
182 
183 /** @brief Returns whether at least one TX and one RX vring are enabled */
186 {
187  int i, found[2] = { }; //RX + TX
188 
189  for (i = 0; i < VHOST_VRING_MAX_N; i++)
190  if (vui->vrings[i].started && vui->vrings[i].enabled)
191  found[i & 1] = 1;
192 
193  return found[0] && found[1];
194 }
195 
198 {
199  /* if we have pointers to descriptor table, go up */
200  int is_ready = vhost_user_intf_ready (vui);
201  if (is_ready != vui->is_ready)
202  {
203  vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
204  is_ready ? "ready" : "down");
205  if (vui->admin_up)
208  : 0);
209  vui->is_ready = is_ready;
210  }
211 }
212 
213 static void
215 {
216  u32 qid;
217  vnet_main_t *vnm = vnet_get_main ();
218 
219  qid = ifq & 0xff;
220  if ((qid & 1) == 0)
221  /* Only care about the odd number, or TX, virtqueue */
222  return;
223 
224  if (vhost_user_intf_ready (vui))
225  // qid >> 1 is to convert virtqueue number to vring queue index
227 }
228 
229 static clib_error_t *
231 {
232  __attribute__ ((unused)) int n;
233  u8 buff[8];
234 
235  n = read (uf->file_descriptor, ((char *) &buff), 8);
236 
237  return 0;
238 }
239 
242 {
243  if (qid & 1) // RX is odd, TX is even
244  {
245  if (vui->vrings[qid].qid == -1)
247  }
248  else
250 }
251 
252 static clib_error_t *
254 {
255  __attribute__ ((unused)) int n;
256  u8 buff[8];
257  vhost_user_intf_t *vui =
258  pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
259  uf->private_data >> 8);
260  u32 qid = uf->private_data & 0xff;
261 
262  n = read (uf->file_descriptor, ((char *) &buff), 8);
263  vu_log_debug (vui, "if %d KICK queue %d", uf->private_data >> 8, qid);
264  if (!vui->vrings[qid].started ||
265  (vhost_user_intf_ready (vui) != vui->is_ready))
266  {
267  if (vui->vrings[qid].started == 0)
268  {
269  vui->vrings[qid].started = 1;
270  vhost_user_thread_placement (vui, qid);
272  }
273  }
274 
276  return 0;
277 }
278 
281 {
282  vhost_user_vring_t *vring = &vui->vrings[qid];
283  clib_memset (vring, 0, sizeof (*vring));
284  vring->kickfd_idx = ~0;
285  vring->callfd_idx = ~0;
286  vring->errfd = -1;
287  vring->qid = -1;
288 
289  /*
290  * We have a bug with some qemu 2.5, and this may be a fix.
291  * Feel like interpretation holy text, but this is from vhost-user.txt.
292  * "
293  * One queue pair is enabled initially. More queues are enabled
294  * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
295  * "
296  * Don't know who's right, but this is what DPDK does.
297  */
298  if (qid == 0 || qid == 1)
299  vring->enabled = 1;
300 }
301 
304 {
305  vhost_user_vring_t *vring = &vui->vrings[qid];
306 
307  if (vring->kickfd_idx != ~0)
308  {
310  vring->kickfd_idx);
311  clib_file_del (&file_main, uf);
312  vring->kickfd_idx = ~0;
313  }
314  if (vring->callfd_idx != ~0)
315  {
317  vring->callfd_idx);
318  clib_file_del (&file_main, uf);
319  vring->callfd_idx = ~0;
320  }
321  if (vring->errfd != -1)
322  {
323  close (vring->errfd);
324  vring->errfd = -1;
325  }
326 
327  // save the qid so that we don't need to unassign and assign_rx_thread
328  // when the interface comes back up. They are expensive calls.
329  u16 q = vui->vrings[qid].qid;
330  vhost_user_vring_init (vui, qid);
331  vui->vrings[qid].qid = q;
332 }
333 
336 {
337  vnet_main_t *vnm = vnet_get_main ();
338  int q;
339 
341 
342  if (vui->clib_file_index != ~0)
343  {
345  vui->clib_file_index = ~0;
346  }
347 
348  vui->is_ready = 0;
349 
350  for (q = 0; q < VHOST_VRING_MAX_N; q++)
351  vhost_user_vring_close (vui, q);
352 
353  unmap_all_mem_regions (vui);
354  vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
355 }
356 
357 static clib_error_t *
359 {
360  int n, i, j;
361  int fd, number_of_fds = 0;
362  int fds[VHOST_MEMORY_MAX_NREGIONS];
363  vhost_user_msg_t msg;
364  struct msghdr mh;
365  struct iovec iov[1];
367  vhost_user_intf_t *vui;
368  struct cmsghdr *cmsg;
369  u8 q;
370  clib_file_t template = { 0 };
371  vnet_main_t *vnm = vnet_get_main ();
373 
374  vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
375 
376  char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
377 
378  clib_memset (&mh, 0, sizeof (mh));
379  clib_memset (control, 0, sizeof (control));
380 
381  for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
382  fds[i] = -1;
383 
384  /* set the payload */
385  iov[0].iov_base = (void *) &msg;
386  iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
387 
388  mh.msg_iov = iov;
389  mh.msg_iovlen = 1;
390  mh.msg_control = control;
391  mh.msg_controllen = sizeof (control);
392 
393  n = recvmsg (uf->file_descriptor, &mh, 0);
394 
395  if (n != VHOST_USER_MSG_HDR_SZ)
396  {
397  if (n == -1)
398  {
399  vu_log_debug (vui, "recvmsg returned error %d %s", errno,
400  strerror (errno));
401  }
402  else
403  {
404  vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
406  }
407  goto close_socket;
408  }
409 
410  if (mh.msg_flags & MSG_CTRUNC)
411  {
412  vu_log_debug (vui, "MSG_CTRUNC is set");
413  goto close_socket;
414  }
415 
416  cmsg = CMSG_FIRSTHDR (&mh);
417 
418  if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
419  (cmsg->cmsg_type == SCM_RIGHTS) &&
420  (cmsg->cmsg_len - CMSG_LEN (0) <=
421  VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
422  {
423  number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
424  clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
425  }
426 
427  /* version 1, no reply bit set */
428  if ((msg.flags & 7) != 1)
429  {
430  vu_log_debug (vui, "malformed message received. closing socket");
431  goto close_socket;
432  }
433 
434  {
435  int rv;
436  rv =
437  read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
438  msg.size);
439  if (rv < 0)
440  {
441  vu_log_debug (vui, "read failed %s", strerror (errno));
442  goto close_socket;
443  }
444  else if (rv != msg.size)
445  {
446  vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
447  msg.size);
448  goto close_socket;
449  }
450  }
451 
452  switch (msg.request)
453  {
455  msg.flags |= 4;
456  msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
457  (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
458  (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
459  (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
460  (1ULL << FEAT_VHOST_F_LOG_ALL) |
461  (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
462  (1ULL << FEAT_VIRTIO_NET_F_MQ) |
463  (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
464  (1ULL << FEAT_VIRTIO_F_VERSION_1);
465  msg.u64 &= vui->feature_mask;
466 
467  if (vui->enable_gso)
469 
470  msg.size = sizeof (msg.u64);
471  vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
472  "0x%016llx", vui->hw_if_index, msg.u64);
473  n =
474  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
475  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
476  {
477  vu_log_debug (vui, "could not send message response");
478  goto close_socket;
479  }
480  break;
481 
483  vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
484  "0x%016llx", vui->hw_if_index, msg.u64);
485 
486  vui->features = msg.u64;
487 
488  if (vui->features &
489  ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
490  (1ULL << FEAT_VIRTIO_F_VERSION_1)))
491  vui->virtio_net_hdr_sz = 12;
492  else
493  vui->virtio_net_hdr_sz = 10;
494 
495  vui->is_any_layout =
496  (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
497 
500  if (vui->enable_gso &&
503  hw->flags |=
506  else
510  vui->is_ready = 0;
512  break;
513 
515  vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
516  vui->hw_if_index, msg.memory.nregions);
517 
518  if ((msg.memory.nregions < 1) ||
519  (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
520  {
521  vu_log_debug (vui, "number of mem regions must be between 1 and %i",
522  VHOST_MEMORY_MAX_NREGIONS);
523  goto close_socket;
524  }
525 
526  if (msg.memory.nregions != number_of_fds)
527  {
528  vu_log_debug (vui, "each memory region must have FD");
529  goto close_socket;
530  }
531 
532  /* Do the mmap without barrier sync */
533  void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
534  for (i = 0; i < msg.memory.nregions; i++)
535  {
536  long page_sz = get_huge_page_size (fds[i]);
537 
538  /* align size to page */
539  ssize_t map_sz = (msg.memory.regions[i].memory_size +
540  msg.memory.regions[i].mmap_offset +
541  page_sz - 1) & ~(page_sz - 1);
542 
543  region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
544  MAP_SHARED, fds[i], 0);
545  if (region_mmap_addr[i] == MAP_FAILED)
546  {
547  vu_log_err (vui, "failed to map memory. errno is %d", errno);
548  for (j = 0; j < i; j++)
549  munmap (region_mmap_addr[j], map_sz);
550  goto close_socket;
551  }
552  vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
553  "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
554  region_mmap_addr[i], page_sz);
555  }
556 
558  unmap_all_mem_regions (vui);
559  for (i = 0; i < msg.memory.nregions; i++)
560  {
561  clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
562  sizeof (vhost_user_memory_region_t));
563 
564  vui->region_mmap_addr[i] = region_mmap_addr[i];
565  vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
566  vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
567  vui->regions[i].memory_size;
568 
569  vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
570  vui->region_mmap_fd[i] = fds[i];
571 
572  vui->nregions++;
573  }
574 
575  /*
576  * Re-compute desc, used, and avail descriptor table if vring address
577  * is set.
578  */
579  for (q = 0; q < VHOST_VRING_MAX_N; q++)
580  {
581  if (vui->vrings[q].desc_user_addr &&
582  vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
583  {
584  vui->vrings[q].desc =
585  map_user_mem (vui, vui->vrings[q].desc_user_addr);
586  vui->vrings[q].used =
587  map_user_mem (vui, vui->vrings[q].used_user_addr);
588  vui->vrings[q].avail =
589  map_user_mem (vui, vui->vrings[q].avail_user_addr);
590  }
591  }
593  break;
594 
596  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
597  vui->hw_if_index, msg.state.index, msg.state.num);
598 
599  if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
600  (msg.state.num == 0) || /* it cannot be zero */
601  ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
602  goto close_socket;
603  vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
604  break;
605 
607  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
608  vui->hw_if_index, msg.state.index);
609 
610  if (msg.state.index >= VHOST_VRING_MAX_N)
611  {
612  vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
613  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
614  goto close_socket;
615  }
616 
617  if (msg.size < sizeof (msg.addr))
618  {
619  vu_log_debug (vui, "vhost message is too short (%d < %d)",
620  msg.size, sizeof (msg.addr));
621  goto close_socket;
622  }
623 
624  vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
625  vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
626  vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
627 
628  if ((desc == NULL) || (used == NULL) || (avail == NULL))
629  {
630  vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
631  vui->hw_if_index);
632  goto close_socket;
633  }
634 
635  vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
636  vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
637  vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
638 
640  vui->vrings[msg.state.index].desc = desc;
641  vui->vrings[msg.state.index].used = used;
642  vui->vrings[msg.state.index].avail = avail;
643 
644  vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
645  vui->vrings[msg.state.index].log_used =
646  (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
647 
648  /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
649  the ring is initialized in an enabled state. */
650  if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
651  vui->vrings[msg.state.index].enabled = 1;
652 
653  vui->vrings[msg.state.index].last_used_idx =
654  vui->vrings[msg.state.index].last_avail_idx =
655  vui->vrings[msg.state.index].used->idx;
656 
657  /* tell driver that we don't want interrupts */
658  vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
661  break;
662 
664  vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
665  break;
666 
668  vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
669  vui->hw_if_index);
670  break;
671 
673  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
674  vui->hw_if_index, msg.u64);
675 
676  q = (u8) (msg.u64 & 0xFF);
677 
678  /* if there is old fd, delete and close it */
679  if (vui->vrings[q].callfd_idx != ~0)
680  {
682  vui->vrings[q].callfd_idx);
683  clib_file_del (&file_main, uf);
684  vui->vrings[q].callfd_idx = ~0;
685  }
686 
687  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
688  {
689  if (number_of_fds != 1)
690  {
691  vu_log_debug (vui, "More than one fd received !");
692  goto close_socket;
693  }
694 
695  template.read_function = vhost_user_callfd_read_ready;
696  template.file_descriptor = fds[0];
697  template.private_data =
698  ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
699  vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
700  }
701  else
702  vui->vrings[q].callfd_idx = ~0;
703  break;
704 
706  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
707  vui->hw_if_index, msg.u64);
708 
709  q = (u8) (msg.u64 & 0xFF);
710 
711  if (vui->vrings[q].kickfd_idx != ~0)
712  {
714  vui->vrings[q].kickfd_idx);
715  clib_file_del (&file_main, uf);
716  vui->vrings[q].kickfd_idx = ~0;
717  }
718 
719  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
720  {
721  if (number_of_fds != 1)
722  {
723  vu_log_debug (vui, "More than one fd received !");
724  goto close_socket;
725  }
726 
727  template.read_function = vhost_user_kickfd_read_ready;
728  template.file_descriptor = fds[0];
729  template.private_data =
730  (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
731  q;
732  vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
733  }
734  else
735  {
736  //When no kickfd is set, the queue is initialized as started
737  vui->vrings[q].kickfd_idx = ~0;
738  vui->vrings[q].started = 1;
740  }
742  break;
743 
745  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
746  vui->hw_if_index, msg.u64);
747 
748  q = (u8) (msg.u64 & 0xFF);
749 
750  if (vui->vrings[q].errfd != -1)
751  close (vui->vrings[q].errfd);
752 
753  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
754  {
755  if (number_of_fds != 1)
756  goto close_socket;
757 
758  vui->vrings[q].errfd = fds[0];
759  }
760  else
761  vui->vrings[q].errfd = -1;
762  break;
763 
765  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
766  vui->hw_if_index, msg.state.index, msg.state.num);
768  vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
770  break;
771 
773  if (msg.state.index >= VHOST_VRING_MAX_N)
774  {
775  vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
776  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
777  goto close_socket;
778  }
779 
780  /* protection is needed to prevent rx/tx from changing last_avail_idx */
782  /*
783  * Copy last_avail_idx from the vring before closing it because
784  * closing the vring also initializes the vring last_avail_idx
785  */
786  msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
787  msg.flags |= 4;
788  msg.size = sizeof (msg.state);
789 
790  /*
791  * Spec says: Client must [...] stop ring upon receiving
792  * VHOST_USER_GET_VRING_BASE
793  */
794  vhost_user_vring_close (vui, msg.state.index);
796  vu_log_debug (vui, "if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
797  vui->hw_if_index, msg.state.index, msg.state.num);
798  n =
799  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
800  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
801  {
802  vu_log_debug (vui, "could not send message response");
803  goto close_socket;
804  }
806  break;
807 
808  case VHOST_USER_NONE:
809  vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
810  break;
811 
813  vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
814  vui->hw_if_index);
815 
816  if (msg.size != sizeof (msg.log))
817  {
818  vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
819  " %d instead of %d", msg.size, sizeof (msg.log));
820  goto close_socket;
821  }
822 
824  {
825  vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
826  "VHOST_USER_SET_LOG_BASE received");
827  goto close_socket;
828  }
829 
830  fd = fds[0];
831  /* align size to page */
832  long page_sz = get_huge_page_size (fd);
833  ssize_t map_sz =
834  (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
835 
836  void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
837  MAP_SHARED, fd, 0);
838 
839  vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
840  "mapped 0x%lx", map_sz, msg.log.offset, fd,
841  log_base_addr);
842 
843  if (log_base_addr == MAP_FAILED)
844  {
845  vu_log_err (vui, "failed to map memory. errno is %d", errno);
846  goto close_socket;
847  }
848 
850  vui->log_base_addr = log_base_addr;
851  vui->log_base_addr += msg.log.offset;
852  vui->log_size = msg.log.size;
854 
855  msg.flags |= 4;
856  msg.size = sizeof (msg.u64);
857  n =
858  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
859  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
860  {
861  vu_log_debug (vui, "could not send message response");
862  goto close_socket;
863  }
864  break;
865 
867  vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
868  break;
869 
871  msg.flags |= 4;
872  msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
874  msg.size = sizeof (msg.u64);
875  vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
876  "reply 0x%016llx", vui->hw_if_index, msg.u64);
877  n =
878  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
879  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
880  {
881  vu_log_debug (vui, "could not send message response");
882  goto close_socket;
883  }
884  break;
885 
887  vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
888  "features 0x%016llx", vui->hw_if_index, msg.u64);
889  vui->protocol_features = msg.u64;
890  break;
891 
893  msg.flags |= 4;
894  msg.u64 = VHOST_VRING_MAX_N;
895  msg.size = sizeof (msg.u64);
896  vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
897  vui->hw_if_index, msg.u64);
898  n =
899  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
900  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
901  {
902  vu_log_debug (vui, "could not send message response");
903  goto close_socket;
904  }
905  break;
906 
908  vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
909  vui->hw_if_index, msg.state.num ? "enable" : "disable",
910  msg.state.index);
911  if (msg.state.index >= VHOST_VRING_MAX_N)
912  {
913  vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
914  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
915  goto close_socket;
916  }
917 
918  vui->vrings[msg.state.index].enabled = msg.state.num;
919  vhost_user_thread_placement (vui, msg.state.index);
921  break;
922 
923  default:
924  vu_log_debug (vui, "unknown vhost-user message %d received. "
925  "closing socket", msg.request);
926  goto close_socket;
927  }
928 
929  return 0;
930 
931 close_socket:
936  return 0;
937 }
938 
939 static clib_error_t *
941 {
944  vhost_user_intf_t *vui =
946 
947  vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
951  return 0;
952 }
953 
954 static clib_error_t *
956 {
957  int client_fd, client_len;
958  struct sockaddr_un client;
959  clib_file_t template = { 0 };
961  vhost_user_intf_t *vui;
962 
964 
965  client_len = sizeof (client);
966  client_fd = accept (uf->file_descriptor,
967  (struct sockaddr *) &client,
968  (socklen_t *) & client_len);
969 
970  if (client_fd < 0)
971  return clib_error_return_unix (0, "accept");
972 
973  if (vui->clib_file_index != ~0)
974  {
975  vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
978  }
979 
980  vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
981  vui->sw_if_index, client_fd);
982  template.read_function = vhost_user_socket_read;
983  template.error_function = vhost_user_socket_error;
984  template.file_descriptor = client_fd;
985  template.private_data = vui - vhost_user_main.vhost_user_interfaces;
986  vui->clib_file_index = clib_file_add (&file_main, &template);
987  return 0;
988 }
989 
990 static clib_error_t *
992 {
995 
996  vum->log_default = vlib_log_register_class ("vhost-user", 0);
997 
998  vum->coalesce_frames = 32;
999  vum->coalesce_time = 1e-3;
1000 
1001  vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1002 
1003  vhost_cpu_t *cpu;
1004  vec_foreach (cpu, vum->cpus)
1005  {
1006  /* This is actually not necessary as validate already zeroes it
1007  * Just keeping the loop here for later because I am lazy. */
1008  cpu->rx_buffers_len = 0;
1009  }
1010 
1011  vum->random = random_default_seed ();
1012 
1014 
1015  return 0;
1016 }
1017 
1018 /* *INDENT-OFF* */
1020 {
1021  .runs_after = VLIB_INITS("ip4_init"),
1022 };
1023 /* *INDENT-ON* */
1024 
1025 static uword
1028 {
1029  vhost_user_intf_t *vui;
1030  f64 timeout = 3153600000.0 /* 100 years */ ;
1031  uword event_type, *event_data = 0;
1033  u16 qid;
1034  f64 now, poll_time_remaining;
1035  f64 next_timeout;
1036  u8 stop_timer = 0;
1037 
1038  while (1)
1039  {
1040  poll_time_remaining =
1042  event_type = vlib_process_get_events (vm, &event_data);
1043  vec_reset_length (event_data);
1044 
1045  /*
1046  * Use the remaining timeout if it is less than coalesce time to avoid
1047  * resetting the existing timer in the middle of expiration
1048  */
1049  timeout = poll_time_remaining;
1050  if (vlib_process_suspend_time_is_zero (timeout) ||
1051  (timeout > vum->coalesce_time))
1052  timeout = vum->coalesce_time;
1053 
1054  now = vlib_time_now (vm);
1055  switch (event_type)
1056  {
1058  stop_timer = 1;
1059  break;
1060 
1062  stop_timer = 0;
1063  if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1064  break;
1065  /* fall through */
1066 
1067  case ~0:
1068  /* *INDENT-OFF* */
1069  pool_foreach (vui, vum->vhost_user_interfaces, {
1070  next_timeout = timeout;
1071  for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1072  {
1073  vhost_user_vring_t *rxvq = &vui->vrings[qid];
1074  vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1075 
1076  if (txvq->qid == -1)
1077  continue;
1078  if (txvq->n_since_last_int)
1079  {
1080  if (now >= txvq->int_deadline)
1081  vhost_user_send_call (vm, txvq);
1082  else
1083  next_timeout = txvq->int_deadline - now;
1084  }
1085 
1086  if (rxvq->n_since_last_int)
1087  {
1088  if (now >= rxvq->int_deadline)
1089  vhost_user_send_call (vm, rxvq);
1090  else
1091  next_timeout = rxvq->int_deadline - now;
1092  }
1093 
1094  if ((next_timeout < timeout) && (next_timeout > 0.0))
1095  timeout = next_timeout;
1096  }
1097  });
1098  /* *INDENT-ON* */
1099  break;
1100 
1101  default:
1102  clib_warning ("BUG: unhandled event type %d", event_type);
1103  break;
1104  }
1105  /* No less than 1 millisecond */
1106  if (timeout < 1e-3)
1107  timeout = 1e-3;
1108  if (stop_timer)
1109  timeout = 3153600000.0;
1110  }
1111  return 0;
1112 }
1113 
1114 /* *INDENT-OFF* */
1117  .type = VLIB_NODE_TYPE_PROCESS,
1118  .name = "vhost-user-send-interrupt-process",
1119 };
1120 /* *INDENT-ON* */
1121 
1122 static uword
1125 {
1127  vhost_user_intf_t *vui;
1128  struct sockaddr_un sun;
1129  int sockfd;
1130  clib_file_t template = { 0 };
1131  f64 timeout = 3153600000.0 /* 100 years */ ;
1132  uword *event_data = 0;
1133 
1134  sockfd = -1;
1135  sun.sun_family = AF_UNIX;
1137  template.error_function = vhost_user_socket_error;
1138 
1139  while (1)
1140  {
1142  vlib_process_get_events (vm, &event_data);
1143  vec_reset_length (event_data);
1144 
1145  timeout = 3.0;
1146 
1147  /* *INDENT-OFF* */
1148  pool_foreach (vui, vum->vhost_user_interfaces, {
1149 
1150  if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1151  if (vui->clib_file_index == ~0)
1152  {
1153  if ((sockfd < 0) &&
1154  ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1155  {
1156  /*
1157  * 1st time error or new error for this interface,
1158  * spit out the message and record the error
1159  */
1160  if (!vui->sock_errno || (vui->sock_errno != errno))
1161  {
1162  clib_unix_warning
1163  ("Error: Could not open unix socket for %s",
1164  vui->sock_filename);
1165  vui->sock_errno = errno;
1166  }
1167  continue;
1168  }
1169 
1170  /* try to connect */
1171  strncpy (sun.sun_path, (char *) vui->sock_filename,
1172  sizeof (sun.sun_path) - 1);
1173 
1174  /* Avoid hanging VPP if the other end does not accept */
1175  if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1176  clib_unix_warning ("fcntl");
1177 
1178  if (connect (sockfd, (struct sockaddr *) &sun,
1179  sizeof (struct sockaddr_un)) == 0)
1180  {
1181  /* Set the socket to blocking as it was before */
1182  if (fcntl(sockfd, F_SETFL, 0) < 0)
1183  clib_unix_warning ("fcntl2");
1184 
1185  vui->sock_errno = 0;
1186  template.file_descriptor = sockfd;
1187  template.private_data =
1188  vui - vhost_user_main.vhost_user_interfaces;
1189  vui->clib_file_index = clib_file_add (&file_main, &template);
1190 
1191  /* This sockfd is considered consumed */
1192  sockfd = -1;
1193  }
1194  else
1195  {
1196  vui->sock_errno = errno;
1197  }
1198  }
1199  else
1200  {
1201  /* check if socket is alive */
1202  int error = 0;
1203  socklen_t len = sizeof (error);
1204  int fd = UNIX_GET_FD(vui->clib_file_index);
1205  int retval =
1206  getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1207 
1208  if (retval)
1209  {
1210  vu_log_debug (vui, "getsockopt returned %d", retval);
1211  vhost_user_if_disconnect (vui);
1212  }
1213  }
1214  }
1215  });
1216  /* *INDENT-ON* */
1217  }
1218  return 0;
1219 }
1220 
1221 /* *INDENT-OFF* */
1223  .function = vhost_user_process,
1224  .type = VLIB_NODE_TYPE_PROCESS,
1225  .name = "vhost-user-process",
1226 };
1227 /* *INDENT-ON* */
1228 
1229 /**
1230  * Disables and reset interface structure.
1231  * It can then be either init again, or removed from used interfaces.
1232  */
1233 static void
1235 {
1236  int q;
1238 
1239  // disconnect interface sockets
1241  vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1243 
1244  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1245  {
1246  // Remove existing queue mapping for the interface
1247  if (q & 1)
1248  {
1249  int rv;
1250  vnet_main_t *vnm = vnet_get_main ();
1251  vhost_user_vring_t *txvq = &vui->vrings[q];
1252 
1253  if (txvq->qid != -1)
1254  {
1256  vui->hw_if_index,
1257  q >> 1);
1258  if (rv)
1259  vu_log_warn (vui, "unable to unassign interface %d, "
1260  "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1261  }
1262  }
1263 
1264  clib_mem_free ((void *) vui->vring_locks[q]);
1265  }
1266 
1267  if (vui->unix_server_index != ~0)
1268  {
1269  //Close server socket
1271  vui->unix_server_index);
1272  clib_file_del (&file_main, uf);
1273  vui->unix_server_index = ~0;
1274  unlink (vui->sock_filename);
1275  }
1276 
1278  &vui->if_index);
1279 }
1280 
1281 int
1283 {
1285  vhost_user_intf_t *vui;
1286  int rv = 0;
1287  vnet_hw_interface_t *hwif;
1288  u16 qid;
1289 
1290  if (!
1291  (hwif =
1293  || hwif->dev_class_index != vhost_user_device_class.index)
1294  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1295 
1297 
1298  vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1299  hwif->name, hwif->dev_instance);
1300 
1301  for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1302  {
1303  vhost_user_vring_t *txvq = &vui->vrings[qid];
1304 
1305  if (txvq->qid == -1)
1306  continue;
1307  if ((vum->ifq_count > 0) &&
1310  {
1311  vum->ifq_count--;
1312  // Stop the timer if there is no more interrupt interface/queue
1313  if ((vum->ifq_count == 0) &&
1314  (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1315  {
1319  break;
1320  }
1321  }
1322  }
1323 
1324  // Disable and reset interface
1325  vhost_user_term_if (vui);
1326 
1327  // Reset renumbered iface
1328  if (hwif->dev_instance <
1331 
1332  // Delete ethernet interface
1334 
1335  // Back to pool
1336  pool_put (vum->vhost_user_interfaces, vui);
1337 
1338  return rv;
1339 }
1340 
1341 static clib_error_t *
1343 {
1344  vnet_main_t *vnm = vnet_get_main ();
1346  vhost_user_intf_t *vui;
1347 
1349  /* *INDENT-OFF* */
1350  pool_foreach (vui, vum->vhost_user_interfaces, {
1351  vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1352  });
1353  /* *INDENT-ON* */
1355  return 0;
1356 }
1357 
1359 
1360 /**
1361  * Open server unix socket on specified sock_filename.
1362  */
1363 static int
1364 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1365 {
1366  int rv = 0;
1367  struct sockaddr_un un = { };
1368  int fd;
1369  /* create listening socket */
1370  if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1371  return VNET_API_ERROR_SYSCALL_ERROR_1;
1372 
1373  un.sun_family = AF_UNIX;
1374  strncpy ((char *) un.sun_path, (char *) sock_filename,
1375  sizeof (un.sun_path) - 1);
1376 
1377  /* remove if exists */
1378  unlink ((char *) sock_filename);
1379 
1380  if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1381  {
1382  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1383  goto error;
1384  }
1385 
1386  if (listen (fd, 1) == -1)
1387  {
1388  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1389  goto error;
1390  }
1391 
1392  *sock_fd = fd;
1393  return 0;
1394 
1395 error:
1396  close (fd);
1397  return rv;
1398 }
1399 
1400 /**
1401  * Create ethernet interface for vhost user interface.
1402  */
1403 static void
1405  vhost_user_intf_t * vui, u8 * hwaddress)
1406 {
1408  u8 hwaddr[6];
1409  clib_error_t *error;
1410 
1411  /* create hw and sw interface */
1412  if (hwaddress)
1413  {
1414  clib_memcpy (hwaddr, hwaddress, 6);
1415  }
1416  else
1417  {
1418  random_u32 (&vum->random);
1419  clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1420  hwaddr[0] = 2;
1421  hwaddr[1] = 0xfe;
1422  }
1423 
1425  (vnm,
1427  vui - vum->vhost_user_interfaces /* device instance */ ,
1428  hwaddr /* ethernet address */ ,
1429  &vui->hw_if_index, 0 /* flag change */ );
1430 
1431  if (error)
1432  clib_error_report (error);
1433 }
1434 
1435 /*
1436  * Initialize vui with specified attributes
1437  */
1438 static void
1440  vhost_user_intf_t * vui,
1441  int server_sock_fd,
1442  const char *sock_filename,
1443  u64 feature_mask, u32 * sw_if_index, u8 enable_gso)
1444 {
1445  vnet_sw_interface_t *sw;
1446  int q;
1448  vnet_hw_interface_t *hw;
1449 
1450  hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1451  sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1452  if (server_sock_fd != -1)
1453  {
1454  clib_file_t template = { 0 };
1456  template.file_descriptor = server_sock_fd;
1457  template.private_data = vui - vum->vhost_user_interfaces; //hw index
1458  vui->unix_server_index = clib_file_add (&file_main, &template);
1459  }
1460  else
1461  {
1462  vui->unix_server_index = ~0;
1463  }
1464 
1465  vui->sw_if_index = sw->sw_if_index;
1466  strncpy (vui->sock_filename, sock_filename,
1467  ARRAY_LEN (vui->sock_filename) - 1);
1468  vui->sock_errno = 0;
1469  vui->is_ready = 0;
1470  vui->feature_mask = feature_mask;
1471  vui->clib_file_index = ~0;
1472  vui->log_base_addr = 0;
1473  vui->if_index = vui - vum->vhost_user_interfaces;
1474  vui->enable_gso = enable_gso;
1475  /*
1476  * enable_gso takes precedence over configurable feature mask if there
1477  * is a clash.
1478  * if feature mask disables gso, but enable_gso is configured,
1479  * then gso is enable
1480  * if feature mask enables gso, but enable_gso is not configured,
1481  * then gso is enable
1482  *
1483  * if gso is enable via feature mask, it must enable both host and guest
1484  * gso feature mask, we don't support one sided GSO or partial GSO.
1485  */
1486  if ((vui->enable_gso == 0) &&
1488  (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)))
1489  vui->enable_gso = 1;
1490  vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1492  &vui->if_index, 0);
1493 
1494  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1495  vhost_user_vring_init (vui, q);
1496 
1499 
1500  if (sw_if_index)
1501  *sw_if_index = vui->sw_if_index;
1502 
1503  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1504  {
1507  clib_memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
1508  }
1509 
1511  vlib_get_thread_main ()->n_vlib_mains - 1);
1513 }
1514 
1515 int
1517  const char *sock_filename,
1518  u8 is_server,
1519  u32 * sw_if_index,
1520  u64 feature_mask,
1521  u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
1522  u8 enable_gso)
1523 {
1524  vhost_user_intf_t *vui = NULL;
1525  u32 sw_if_idx = ~0;
1526  int rv = 0;
1527  int server_sock_fd = -1;
1529  uword *if_index;
1530 
1531  if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1532  {
1533  return VNET_API_ERROR_INVALID_ARGUMENT;
1534  }
1535 
1536  if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1537  if (if_index)
1538  {
1539  if (sw_if_index)
1540  {
1541  vui = &vum->vhost_user_interfaces[*if_index];
1542  *sw_if_index = vui->sw_if_index;
1543  }
1544  return VNET_API_ERROR_IF_ALREADY_EXISTS;
1545  }
1546 
1547  if (is_server)
1548  {
1549  if ((rv =
1550  vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1551  {
1552  return rv;
1553  }
1554  }
1555 
1556  /* Protect the uninitialized vui from being dispatched by rx/tx */
1558  pool_get (vhost_user_main.vhost_user_interfaces, vui);
1559  vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1561 
1562  vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
1563  feature_mask, &sw_if_idx, enable_gso);
1564  vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1566 
1567  if (renumber)
1568  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1569 
1570  if (sw_if_index)
1571  *sw_if_index = sw_if_idx;
1572 
1573  // Process node must connect
1575 
1576  return rv;
1577 }
1578 
1579 int
1581  const char *sock_filename,
1582  u8 is_server,
1583  u32 sw_if_index,
1584  u64 feature_mask, u8 renumber, u32 custom_dev_instance,
1585  u8 enable_gso)
1586 {
1588  vhost_user_intf_t *vui = NULL;
1589  u32 sw_if_idx = ~0;
1590  int server_sock_fd = -1;
1591  int rv = 0;
1592  vnet_hw_interface_t *hwif;
1593  uword *if_index;
1594 
1595  if (!
1596  (hwif =
1598  || hwif->dev_class_index != vhost_user_device_class.index)
1599  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1600 
1601  if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1602  return VNET_API_ERROR_INVALID_ARGUMENT;
1603 
1605 
1606  /*
1607  * Disallow changing the interface to have the same path name
1608  * as other interface
1609  */
1610  if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1611  if (if_index && (*if_index != vui->if_index))
1612  return VNET_API_ERROR_IF_ALREADY_EXISTS;
1613 
1614  // First try to open server socket
1615  if (is_server)
1616  if ((rv = vhost_user_init_server_sock (sock_filename,
1617  &server_sock_fd)) != 0)
1618  return rv;
1619 
1620  vhost_user_term_if (vui);
1621  vhost_user_vui_init (vnm, vui, server_sock_fd,
1622  sock_filename, feature_mask, &sw_if_idx, enable_gso);
1623 
1624  if (renumber)
1625  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1626 
1627  // Process node must connect
1629 
1630  return rv;
1631 }
1632 
1633 clib_error_t *
1635  unformat_input_t * input,
1636  vlib_cli_command_t * cmd)
1637 {
1638  unformat_input_t _line_input, *line_input = &_line_input;
1639  u8 *sock_filename = NULL;
1640  u32 sw_if_index;
1641  u8 is_server = 0;
1642  u64 feature_mask = (u64) ~ (0ULL);
1643  u8 renumber = 0;
1644  u32 custom_dev_instance = ~0;
1645  u8 hwaddr[6];
1646  u8 *hw = NULL;
1647  clib_error_t *error = NULL;
1648  u8 enable_gso = 0;
1649 
1650  /* Get a line of input. */
1651  if (!unformat_user (input, unformat_line_input, line_input))
1652  return 0;
1653 
1654  /* GSO feature is disable by default */
1656  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1657  {
1658  if (unformat (line_input, "socket %s", &sock_filename))
1659  ;
1660  else if (unformat (line_input, "server"))
1661  is_server = 1;
1662  else if (unformat (line_input, "gso"))
1663  enable_gso = 1;
1664  else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1665  ;
1666  else
1667  if (unformat
1668  (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1669  hw = hwaddr;
1670  else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1671  {
1672  renumber = 1;
1673  }
1674  else
1675  {
1676  error = clib_error_return (0, "unknown input `%U'",
1677  format_unformat_error, line_input);
1678  goto done;
1679  }
1680  }
1681 
1682  vnet_main_t *vnm = vnet_get_main ();
1683 
1684  int rv;
1685  if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1686  is_server, &sw_if_index, feature_mask,
1687  renumber, custom_dev_instance, hw,
1688  enable_gso)))
1689  {
1690  error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1691  goto done;
1692  }
1693 
1695  sw_if_index);
1696 
1697 done:
1698  vec_free (sock_filename);
1699  unformat_free (line_input);
1700 
1701  return error;
1702 }
1703 
1704 clib_error_t *
1706  unformat_input_t * input,
1707  vlib_cli_command_t * cmd)
1708 {
1709  unformat_input_t _line_input, *line_input = &_line_input;
1710  u32 sw_if_index = ~0;
1711  vnet_main_t *vnm = vnet_get_main ();
1712  clib_error_t *error = NULL;
1713 
1714  /* Get a line of input. */
1715  if (!unformat_user (input, unformat_line_input, line_input))
1716  return 0;
1717 
1718  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1719  {
1720  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1721  ;
1722  else if (unformat
1723  (line_input, "%U", unformat_vnet_sw_interface, vnm,
1724  &sw_if_index))
1725  {
1726  vnet_hw_interface_t *hwif =
1728  if (hwif == NULL ||
1730  {
1731  error = clib_error_return (0, "Not a vhost interface");
1732  goto done;
1733  }
1734  }
1735  else
1736  {
1737  error = clib_error_return (0, "unknown input `%U'",
1738  format_unformat_error, line_input);
1739  goto done;
1740  }
1741  }
1742 
1743  vhost_user_delete_if (vnm, vm, sw_if_index);
1744 
1745 done:
1746  unformat_free (line_input);
1747 
1748  return error;
1749 }
1750 
1751 int
1753  vhost_user_intf_details_t ** out_vuids)
1754 {
1755  int rv = 0;
1757  vhost_user_intf_t *vui;
1758  vhost_user_intf_details_t *r_vuids = NULL;
1759  vhost_user_intf_details_t *vuid = NULL;
1760  u32 *hw_if_indices = 0;
1762  u8 *s = NULL;
1763  int i;
1764 
1765  if (!out_vuids)
1766  return -1;
1767 
1769  vec_add1 (hw_if_indices, vui->hw_if_index);
1770  );
1771 
1772  for (i = 0; i < vec_len (hw_if_indices); i++)
1773  {
1774  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1776 
1777  vec_add2 (r_vuids, vuid, 1);
1778  vuid->sw_if_index = vui->sw_if_index;
1779  vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1780  vuid->features = vui->features;
1781  vuid->num_regions = vui->nregions;
1782  vuid->is_server = vui->unix_server_index != ~0;
1783  vuid->sock_errno = vui->sock_errno;
1784  strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
1785  sizeof (vuid->sock_filename));
1786  vuid->sock_filename[ARRAY_LEN (vuid->sock_filename) - 1] = '\0';
1787  s = format (s, "%v%c", hi->name, 0);
1788 
1789  strncpy ((char *) vuid->if_name, (char *) s,
1790  ARRAY_LEN (vuid->if_name) - 1);
1791  _vec_len (s) = 0;
1792  }
1793 
1794  vec_free (s);
1795  vec_free (hw_if_indices);
1796 
1797  *out_vuids = r_vuids;
1798 
1799  return rv;
1800 }
1801 
1802 clib_error_t *
1804  unformat_input_t * input,
1805  vlib_cli_command_t * cmd)
1806 {
1807  clib_error_t *error = 0;
1808  vnet_main_t *vnm = vnet_get_main ();
1810  vhost_user_intf_t *vui;
1811  u32 hw_if_index, *hw_if_indices = 0;
1813  u16 qid;
1814  u32 ci;
1815  int i, j, q;
1816  int show_descr = 0;
1817  struct feat_struct
1818  {
1819  u8 bit;
1820  char *str;
1821  };
1822  struct feat_struct *feat_entry;
1823 
1824  static struct feat_struct feat_array[] = {
1825 #define _(s,b) { .str = #s, .bit = b, },
1827 #undef _
1828  {.str = NULL}
1829  };
1830 
1831 #define foreach_protocol_feature \
1832  _(VHOST_USER_PROTOCOL_F_MQ) \
1833  _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
1834 
1835  static struct feat_struct proto_feat_array[] = {
1836 #define _(s) { .str = #s, .bit = s},
1838 #undef _
1839  {.str = NULL}
1840  };
1841 
1842  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1843  {
1844  if (unformat
1845  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1846  {
1847  hi = vnet_get_hw_interface (vnm, hw_if_index);
1848  if (vhost_user_device_class.index != hi->dev_class_index)
1849  {
1850  error = clib_error_return (0, "unknown input `%U'",
1851  format_unformat_error, input);
1852  goto done;
1853  }
1854  vec_add1 (hw_if_indices, hw_if_index);
1855  }
1856  else if (unformat (input, "descriptors") || unformat (input, "desc"))
1857  show_descr = 1;
1858  else
1859  {
1860  error = clib_error_return (0, "unknown input `%U'",
1861  format_unformat_error, input);
1862  goto done;
1863  }
1864  }
1865  if (vec_len (hw_if_indices) == 0)
1866  {
1868  vec_add1 (hw_if_indices, vui->hw_if_index);
1869  );
1870  }
1871  vlib_cli_output (vm, "Virtio vhost-user interfaces");
1872  vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
1873  vum->coalesce_frames, vum->coalesce_time);
1874  vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
1875  vum->ifq_count);
1876  vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
1877 
1878  for (i = 0; i < vec_len (hw_if_indices); i++)
1879  {
1880  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1882  vlib_cli_output (vm, "Interface: %U (ifindex %d)",
1883  format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
1884  hw_if_indices[i]);
1885  if (vui->enable_gso)
1886  vlib_cli_output (vm, " GSO enable");
1887 
1888  vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
1889  " features mask (0x%llx): \n"
1890  " features (0x%llx): \n",
1891  vui->virtio_net_hdr_sz, vui->feature_mask,
1892  vui->features);
1893 
1894  feat_entry = (struct feat_struct *) &feat_array;
1895  while (feat_entry->str)
1896  {
1897  if (vui->features & (1ULL << feat_entry->bit))
1898  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1899  feat_entry->bit);
1900  feat_entry++;
1901  }
1902 
1903  vlib_cli_output (vm, " protocol features (0x%llx)",
1904  vui->protocol_features);
1905  feat_entry = (struct feat_struct *) &proto_feat_array;
1906  while (feat_entry->str)
1907  {
1908  if (vui->protocol_features & (1ULL << feat_entry->bit))
1909  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1910  feat_entry->bit);
1911  feat_entry++;
1912  }
1913 
1914  vlib_cli_output (vm, "\n");
1915 
1916  vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1917  vui->sock_filename,
1918  (vui->unix_server_index != ~0) ? "server" : "client",
1919  strerror (vui->sock_errno));
1920 
1921  vlib_cli_output (vm, " rx placement: ");
1922 
1923  for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1924  {
1925  vnet_main_t *vnm = vnet_get_main ();
1926  uword thread_index;
1928  vhost_user_vring_t *txvq = &vui->vrings[qid];
1929 
1930  if (txvq->qid == -1)
1931  continue;
1932  thread_index =
1934  qid >> 1);
1935  vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
1936  &mode);
1937  vlib_cli_output (vm, " thread %d on vring %d, %U\n",
1938  thread_index, qid,
1940  }
1941 
1942  vlib_cli_output (vm, " tx placement: %s\n",
1943  vui->use_tx_spinlock ? "spin-lock" : "lock-free");
1944 
1946  {
1947  vlib_cli_output (vm, " thread %d on vring %d\n", ci,
1948  VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
1949  }
1950 
1951  vlib_cli_output (vm, "\n");
1952 
1953  vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1954 
1955  if (vui->nregions)
1956  {
1957  vlib_cli_output (vm,
1958  " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1959  vlib_cli_output (vm,
1960  " ====== ===== ================== ================== ================== ================== ==================\n");
1961  }
1962  for (j = 0; j < vui->nregions; j++)
1963  {
1964  vlib_cli_output (vm,
1965  " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
1966  j, vui->region_mmap_fd[j],
1967  vui->regions[j].guest_phys_addr,
1968  vui->regions[j].memory_size,
1969  vui->regions[j].userspace_addr,
1970  vui->regions[j].mmap_offset,
1972  }
1973  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1974  {
1975  if (!vui->vrings[q].started)
1976  continue;
1977 
1978  vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
1979  (q & 1) ? "RX" : "TX",
1980  vui->vrings[q].enabled ? "" : " disabled");
1981 
1982  vlib_cli_output (vm,
1983  " qsz %d last_avail_idx %d last_used_idx %d\n",
1984  vui->vrings[q].qsz_mask + 1,
1985  vui->vrings[q].last_avail_idx,
1986  vui->vrings[q].last_used_idx);
1987 
1988  if (vui->vrings[q].avail && vui->vrings[q].used)
1989  vlib_cli_output (vm,
1990  " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1991  vui->vrings[q].avail->flags,
1992  vui->vrings[q].avail->idx,
1993  vui->vrings[q].used->flags,
1994  vui->vrings[q].used->idx);
1995 
1996  int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1997  int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1998  vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
1999  kickfd, callfd, vui->vrings[q].errfd);
2000 
2001  if (show_descr)
2002  {
2003  vlib_cli_output (vm, "\n descriptor table:\n");
2004  vlib_cli_output (vm,
2005  " id addr len flags next user_addr\n");
2006  vlib_cli_output (vm,
2007  " ===== ================== ===== ====== ===== ==================\n");
2008  for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
2009  {
2010  u32 mem_hint = 0;
2011  vlib_cli_output (vm,
2012  " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
2013  j, vui->vrings[q].desc[j].addr,
2014  vui->vrings[q].desc[j].len,
2015  vui->vrings[q].desc[j].flags,
2016  vui->vrings[q].desc[j].next,
2018  (vui,
2019  vui->vrings[q].desc[j].
2020  addr, &mem_hint)));
2021  }
2022  }
2023  }
2024  vlib_cli_output (vm, "\n");
2025  }
2026 done:
2027  vec_free (hw_if_indices);
2028  return error;
2029 }
2030 
2031 /*
2032  * CLI functions
2033  */
2034 
2035 /*?
2036  * Create a vHost User interface. Once created, a new virtual interface
2037  * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2038  * is the next free index.
2039  *
2040  * There are several parameters associated with a vHost interface:
2041  *
2042  * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2043  * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2044  * create the socket if it does not already exist. If in '<em>client</em>' mode,
2045  * hypervisor will create the socket if it does not already exist. The VPP code
2046  * is indifferent to the file location. However, if SELinux is enabled, then the
2047  * socket needs to be created in '<em>/var/run/vpp/</em>'.
2048  *
2049  * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2050  * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2051  * mode, the VM can be reset without tearing down the vHost Interface. In
2052  * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2053  * tearing down the vHost Interface.
2054  *
2055  * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2056  * startup. <b>This is intended for degugging only.</b> It is recommended that this
2057  * parameter not be used except by experienced users. By default, all supported
2058  * features will be advertised. Otherwise, provide the set of features desired.
2059  * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2060  * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2061  * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2062  * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2063  * - 0x004000000 (26) - VHOST_F_LOG_ALL
2064  * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2065  * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2066  * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2067  * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2068  *
2069  * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2070  * X:X:X:X:X:X unix or X.X.X cisco format.
2071  *
2072  * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2073  * in the name to be specified. If instance already exists, name will be used
2074  * anyway and multiple instances will have the same name. Use with caution.
2075  *
2076  * @cliexpar
2077  * Example of how to create a vhost interface with VPP as the client and all features enabled:
2078  * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2079  * VirtualEthernet0/0/0
2080  * @cliexend
2081  * Example of how to create a vhost interface with VPP as the server and with just
2082  * multiple queues enabled:
2083  * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2084  * VirtualEthernet0/0/1
2085  * @cliexend
2086  * Once the vHost interface is created, enable the interface using:
2087  * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2088 ?*/
2089 /* *INDENT-OFF* */
2090 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2091  .path = "create vhost-user",
2092  .short_help = "create vhost-user socket <socket-filename> [server] "
2093  "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso]",
2094  .function = vhost_user_connect_command_fn,
2095  .is_mp_safe = 1,
2096 };
2097 /* *INDENT-ON* */
2098 
2099 /*?
2100  * Delete a vHost User interface using the interface name or the
2101  * software interface index. Use the '<em>show interface</em>'
2102  * command to determine the software interface index. On deletion,
2103  * the linux socket will not be deleted.
2104  *
2105  * @cliexpar
2106  * Example of how to delete a vhost interface by name:
2107  * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2108  * Example of how to delete a vhost interface by software interface index:
2109  * @cliexcmd{delete vhost-user sw_if_index 1}
2110 ?*/
2111 /* *INDENT-OFF* */
2112 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2113  .path = "delete vhost-user",
2114  .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2115  .function = vhost_user_delete_command_fn,
2116 };
2117 
2118 /*?
2119  * Display the attributes of a single vHost User interface (provide interface
2120  * name), multiple vHost User interfaces (provide a list of interface names seperated
2121  * by spaces) or all Vhost User interfaces (omit an interface name to display all
2122  * vHost interfaces).
2123  *
2124  * @cliexpar
2125  * @parblock
2126  * Example of how to display a vhost interface:
2127  * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2128  * Virtio vhost-user interfaces
2129  * Global:
2130  * coalesce frames 32 time 1e-3
2131  * Interface: VirtualEthernet0/0/0 (ifindex 1)
2132  * virtio_net_hdr_sz 12
2133  * features mask (0xffffffffffffffff):
2134  * features (0x50408000):
2135  * VIRTIO_NET_F_MRG_RXBUF (15)
2136  * VIRTIO_NET_F_MQ (22)
2137  * VIRTIO_F_INDIRECT_DESC (28)
2138  * VHOST_USER_F_PROTOCOL_FEATURES (30)
2139  * protocol features (0x3)
2140  * VHOST_USER_PROTOCOL_F_MQ (0)
2141  * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2142  *
2143  * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2144  *
2145  * rx placement:
2146  * thread 1 on vring 1
2147  * thread 1 on vring 5
2148  * thread 2 on vring 3
2149  * thread 2 on vring 7
2150  * tx placement: spin-lock
2151  * thread 0 on vring 0
2152  * thread 1 on vring 2
2153  * thread 2 on vring 0
2154  *
2155  * Memory regions (total 2)
2156  * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2157  * ====== ===== ================== ================== ================== ================== ==================
2158  * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2159  * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2160  *
2161  * Virtqueue 0 (TX)
2162  * qsz 256 last_avail_idx 0 last_used_idx 0
2163  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2164  * kickfd 62 callfd 64 errfd -1
2165  *
2166  * Virtqueue 1 (RX)
2167  * qsz 256 last_avail_idx 0 last_used_idx 0
2168  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2169  * kickfd 65 callfd 66 errfd -1
2170  *
2171  * Virtqueue 2 (TX)
2172  * qsz 256 last_avail_idx 0 last_used_idx 0
2173  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2174  * kickfd 63 callfd 70 errfd -1
2175  *
2176  * Virtqueue 3 (RX)
2177  * qsz 256 last_avail_idx 0 last_used_idx 0
2178  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2179  * kickfd 72 callfd 74 errfd -1
2180  *
2181  * Virtqueue 4 (TX disabled)
2182  * qsz 256 last_avail_idx 0 last_used_idx 0
2183  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2184  * kickfd 76 callfd 78 errfd -1
2185  *
2186  * Virtqueue 5 (RX disabled)
2187  * qsz 256 last_avail_idx 0 last_used_idx 0
2188  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2189  * kickfd 80 callfd 82 errfd -1
2190  *
2191  * Virtqueue 6 (TX disabled)
2192  * qsz 256 last_avail_idx 0 last_used_idx 0
2193  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2194  * kickfd 84 callfd 86 errfd -1
2195  *
2196  * Virtqueue 7 (RX disabled)
2197  * qsz 256 last_avail_idx 0 last_used_idx 0
2198  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2199  * kickfd 88 callfd 90 errfd -1
2200  *
2201  * @cliexend
2202  *
2203  * The optional '<em>descriptors</em>' parameter will display the same output as
2204  * the previous example but will include the descriptor table for each queue.
2205  * The output is truncated below:
2206  * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2207  * Virtio vhost-user interfaces
2208  * Global:
2209  * coalesce frames 32 time 1e-3
2210  * Interface: VirtualEthernet0/0/0 (ifindex 1)
2211  * virtio_net_hdr_sz 12
2212  * features mask (0xffffffffffffffff):
2213  * features (0x50408000):
2214  * VIRTIO_NET_F_MRG_RXBUF (15)
2215  * VIRTIO_NET_F_MQ (22)
2216  * :
2217  * Virtqueue 0 (TX)
2218  * qsz 256 last_avail_idx 0 last_used_idx 0
2219  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2220  * kickfd 62 callfd 64 errfd -1
2221  *
2222  * descriptor table:
2223  * id addr len flags next user_addr
2224  * ===== ================== ===== ====== ===== ==================
2225  * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2226  * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2227  * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2228  * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2229  * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2230  * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2231  * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2232  * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2233  * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2234  * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2235  * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2236  * :
2237  * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2238  * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2239  * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2240  * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2241  * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2242  * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2243  * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2244  *
2245  * Virtqueue 1 (RX)
2246  * qsz 256 last_avail_idx 0 last_used_idx 0
2247  * :
2248  * @cliexend
2249  * @endparblock
2250 ?*/
2251 /* *INDENT-OFF* */
2252 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2253  .path = "show vhost-user",
2254  .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
2255  .function = show_vhost_user_command_fn,
2256 };
2257 /* *INDENT-ON* */
2258 
2259 
2260 static clib_error_t *
2262 {
2264 
2265  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2266  {
2267  if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2268  ;
2269  else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2270  ;
2271  else if (unformat (input, "dont-dump-memory"))
2272  vum->dont_dump_vhost_user_memory = 1;
2273  else
2274  return clib_error_return (0, "unknown input `%U'",
2275  format_unformat_error, input);
2276  }
2277 
2278  return 0;
2279 }
2280 
2281 /* vhost-user { ... } configuration. */
2282 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2283 
2284 void
2286 {
2288  vhost_user_intf_t *vui;
2289 
2290  if (vum->dont_dump_vhost_user_memory)
2291  {
2293  unmap_all_mem_regions (vui);
2294  );
2295  }
2296 }
2297 
2298 /*
2299  * fd.io coding-style-patch-verification: ON
2300  *
2301  * Local Variables:
2302  * eval: (c-set-style "gnu")
2303  * End:
2304  */
unformat_function_t unformat_vnet_hw_interface
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:176
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
u32 len
Definition: pci.h:212
vmrglw vmrglh hi
#define vec_foreach_index(var, v)
Iterate over vector indices.
static void clib_file_del(clib_file_main_t *um, clib_file_t *f)
Definition: file.h:109
format_function_t format_vnet_hw_if_index_name
#define vu_log_warn(dev, f,...)
Definition: vhost_user.h:55
vring_desc_t * desc
Definition: vhost_user.h:263
static uword random_default_seed(void)
Default random seed (unix/linux user-mode)
Definition: random.h:91
#define VHOST_USER_PROTOCOL_F_LOG_SHMFD
Definition: vhost_user.h:32
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:673
VNET_HW_INTERFACE_CLASS(vhost_interface_class, static)
vhost_cpu_t * cpus
Per-CPU data for vhost-user.
Definition: vhost_user.h:381
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:324
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:317
static clib_error_t * vhost_user_socksvr_accept_ready(clib_file_t *uf)
Definition: vhost_user.c:955
unsigned long u64
Definition: types.h:89
#define VHOST_VRING_MAX_N
Definition: vhost_user.h:22
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:346
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
static_always_inline void vhost_user_update_iface_state(vhost_user_intf_t *vui)
Definition: vhost_user.c:197
u16 next
Definition: pci.h:214
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:51
vring_avail_t * avail
Definition: vhost_user.h:264
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 file_descriptor
Definition: file.h:54
vnet_device_class_t vhost_user_device_class
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
int vnet_interface_name_renumber(u32 sw_if_index, u32 new_show_dev_instance)
Definition: interface.c:1376
static clib_error_t * vhost_user_callfd_read_ready(clib_file_t *uf)
Definition: vhost_user.c:230
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:560
int i
#define VHOST_USER_EVENT_START_TIMER
Definition: vhost_user.h:292
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
static_always_inline void vhost_user_vring_init(vhost_user_intf_t *vui, u32 qid)
Definition: vhost_user.c:280
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unformat_function_t unformat_vnet_sw_interface
u16 idx
Definition: pci.h:220
u16 flags
Definition: pci.h:233
int vhost_user_create_if(vnet_main_t *vnm, vlib_main_t *vm, const char *sock_filename, u8 is_server, u32 *sw_if_index, u64 feature_mask, u8 renumber, u32 custom_dev_instance, u8 *hwaddr, u8 enable_gso)
Definition: vhost_user.c:1516
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:236
vring_used_t * used
Definition: vhost_user.h:265
vhost_vring_addr_t addr
Definition: vhost_user.h:147
format_function_t format_vnet_sw_if_index_name
#define VHOST_USER_VRING_NOFD_MASK
Definition: vhost_user.h:26
unsigned char u8
Definition: types.h:56
static_always_inline void vhost_user_rx_thread_placement(vhost_user_intf_t *vui, u32 qid)
Unassign existing interface/queue to thread mappings and re-assign new interface/queue to thread mapp...
Definition: vhost_user.c:161
static uword vlib_process_suspend_time_is_zero(f64 dt)
Returns TRUE if a process suspend time is less than 10us.
Definition: node_funcs.h:410
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_file_function_t * read_function
Definition: file.h:67
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:204
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define clib_memcpy(d, s, n)
Definition: string.h:180
static void vhost_user_vui_init(vnet_main_t *vnm, vhost_user_intf_t *vui, int server_sock_fd, const char *sock_filename, u64 feature_mask, u32 *sw_if_index, u8 enable_gso)
Definition: vhost_user.c:1439
#define vu_log_debug(dev, f,...)
Definition: vhost_user.h:48
clib_file_t * file_pool
Definition: file.h:88
u64 addr
Definition: pci.h:211
vnet_hw_interface_rx_mode
Definition: interface.h:53
#define static_always_inline
Definition: clib.h:100
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:516
static clib_error_t * vhost_user_socket_read(clib_file_t *uf)
Definition: vhost_user.c:358
#define UNIX_GET_FD(unixfd_idx)
Definition: vhost_user.h:68
void * log_base_addr
Definition: vhost_user.h:327
static_always_inline void vhost_user_thread_placement(vhost_user_intf_t *vui, u32 qid)
Definition: vhost_user.c:241
static_always_inline void * map_guest_mem(vhost_user_intf_t *vui, uword addr, u32 *hint)
static_always_inline void vnet_device_input_set_interrupt_pending(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.h:136
vnet_hw_interface_flags_t flags
Definition: interface.h:506
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define VRING_USED_F_NO_NOTIFY
Definition: vhost_user.h:45
#define clib_error_return(e, args...)
Definition: error.h:99
clib_file_main_t file_main
Definition: main.c:63
unsigned int u32
Definition: types.h:88
static vlib_node_registration_t vhost_user_process_node
(constructor) VLIB_REGISTER_NODE (vhost_user_process_node)
Definition: vhost_user.c:1222
static clib_error_t * vhost_user_kickfd_read_ready(clib_file_t *uf)
Definition: vhost_user.c:253
static clib_error_t * vhost_user_socket_error(clib_file_t *uf)
Definition: vhost_user.c:940
void vhost_user_unmap_all(void)
Definition: vhost_user.c:2285
unformat_function_t unformat_line_input
Definition: format.h:283
static_always_inline void vhost_user_tx_thread_placement(vhost_user_intf_t *vui)
Definition: vhost_user.c:120
#define VHOST_USER_PROTOCOL_F_MQ
Definition: vhost_user.h:31
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
static_always_inline uword vnet_get_device_input_thread_index(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.h:127
static void vhost_user_term_if(vhost_user_intf_t *vui)
Disables and reset interface structure.
Definition: vhost_user.c:1234
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:934
static_always_inline int vhost_user_intf_ready(vhost_user_intf_t *vui)
Returns whether at least one TX and one RX vring are enabled.
Definition: vhost_user.c:185
u32 random
Pseudo random iterator.
Definition: vhost_user.h:384
uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
Definition: mhash.c:264
vlib_node_registration_t vhost_user_input_node
(constructor) VLIB_REGISTER_NODE (vhost_user_input_node)
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define clib_error_return_unix(e, args...)
Definition: error.h:102
int vnet_hw_interface_get_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_interface_rx_mode *mode)
Definition: devices.c:313
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
int vhost_user_delete_if(vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index)
Definition: vhost_user.c:1282
format_function_t format_vnet_hw_interface_rx_mode
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:182
char sock_filename[256]
Definition: vhost_user.h:302
vhost_user_main_t vhost_user_main
Definition: vhost_user.c:56
#define VHOST_USER_MSG_HDR_SZ
Definition: vhost_user.h:20
u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:318
int vhost_user_dump_ifs(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_details_t **out_vuids)
Definition: vhost_user.c:1752
vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:314
vlib_log_class_t log_default
Definition: vhost_user.h:390
static_always_inline void vhost_user_update_gso_interface_count(vhost_user_intf_t *vui, u8 add)
#define VHOST_VRING_IDX_RX(qid)
Definition: vhost_user.h:23
u32 * show_dev_instance_by_real_dev_instance
Definition: vhost_user.h:375
static_always_inline void vhost_user_vring_close(vhost_user_intf_t *vui, u32 qid)
Definition: vhost_user.c:303
vhost_user_intf_t * vhost_user_interfaces
Definition: vhost_user.h:374
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
static void mhash_init_c_string(mhash_t *h, uword n_value_bytes)
Definition: mhash.h:78
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
vlib_main_t * vm
Definition: buffer.c:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static void unmap_all_mem_regions(vhost_user_intf_t *vui)
Definition: vhost_user.c:74
static clib_error_t * vhost_user_config(vlib_main_t *vm, unformat_input_t *input)
Definition: vhost_user.c:2261
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:178
#define VHOST_USER_EVENT_STOP_TIMER
Definition: vhost_user.h:293
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
#define clib_warning(format, args...)
Definition: error.h:59
static uword vhost_user_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: vhost_user.c:1123
#define ARRAY_LEN(x)
Definition: clib.h:63
clib_error_t * vhost_user_delete_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost_user.c:1705
vl_api_vxlan_gbp_api_tunnel_mode_t mode
Definition: vxlan_gbp.api:44
#define foreach_protocol_feature
u16 flags
Definition: pci.h:219
static int vhost_user_init_server_sock(const char *sock_filename, int *sock_fd)
Open server unix socket on specified sock_filename.
Definition: vhost_user.c:1364
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:161
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:233
clib_error_t * show_vhost_user_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost_user.c:1803
#define ASSERT(truth)
#define VHOST_VRING_F_LOG
Definition: vhost_user.h:33
void vnet_hw_interface_assign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, uword thread_index)
Definition: devices.c:139
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
#define vu_log_err(dev, f,...)
Definition: vhost_user.h:61
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:96
static_always_inline void vhost_user_if_disconnect(vhost_user_intf_t *vui)
Definition: vhost_user.c:335
static void clib_mem_free(void *p)
Definition: mem.h:226
volatile u32 * vring_locks[VHOST_VRING_MAX_N]
Definition: vhost_user.h:322
mhash_t if_index_by_sock_name
Definition: vhost_user.h:372
static clib_error_t * vhost_user_exit(vlib_main_t *vm)
Definition: vhost_user.c:1342
#define clib_error_report(e)
Definition: error.h:113
vlib_node_registration_t vhost_user_send_interrupt_node
(constructor) VLIB_REGISTER_NODE (vhost_user_send_interrupt_node)
Definition: vhost_user.c:53
static uword pointer_to_uword(const void *p)
Definition: types.h:131
static void vhost_user_create_ethernet(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_t *vui, u8 *hwaddress)
Create ethernet interface for vhost user interface.
Definition: vhost_user.c:1404
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
struct _vlib_node_registration vlib_node_registration_t
void * region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:315
int vhost_user_modify_if(vnet_main_t *vnm, vlib_main_t *vm, const char *sock_filename, u8 is_server, u32 sw_if_index, u64 feature_mask, u8 renumber, u32 custom_dev_instance, u8 enable_gso)
Definition: vhost_user.c:1580
static long get_huge_page_size(int fd)
Definition: vhost_user.c:66
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, const u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
Definition: interface.c:278
vhost_user_vring_t vrings[VHOST_VRING_MAX_N]
Definition: vhost_user.h:321
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
Definition: interface.c:492
static void vhost_user_set_interrupt_pending(vhost_user_intf_t *vui, u32 ifq)
Definition: vhost_user.c:214
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
#define VHOST_MEMORY_MAX_NREGIONS
Definition: vhost_user.h:19
clib_error_t * vhost_user_connect_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost_user.c:1634
void vnet_sw_interface_set_mtu(vnet_main_t *vnm, u32 sw_if_index, u32 mtu)
Definition: interface.c:664
u32 rx_buffers_len
Definition: vhost_user.h:359
int vnet_hw_interface_unassign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
Definition: devices.c:188
u16 idx
Definition: pci.h:234
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:161
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1520
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:316
u16 flags
Definition: pci.h:213
#define vec_foreach(var, vec)
Vector iterator.
uword private_data
Definition: file.h:64
Definition: file.h:51
int vnet_hw_interface_set_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_interface_rx_mode mode)
Definition: devices.c:253
static clib_error_t * vhost_user_init(vlib_main_t *vm)
Definition: vhost_user.c:991
static_always_inline void * map_user_mem(vhost_user_intf_t *vui, uword addr)
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
static uword vhost_user_send_interrupt_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: vhost_user.c:1026
#define FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS
Definition: vhost_user.h:125
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:772
vl_api_address_union_t un
Definition: ip_types.api:85
#define VLIB_INITS(...)
Definition: init.h:344
static void vnet_hw_interface_set_input_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: devices.h:79
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
int dont_dump_vhost_user_memory
Definition: vhost_user.h:378