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