FD.io VPP  v19.04.2-12-g66b1689
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  msg.size = sizeof (msg.u64);
467  vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
468  "0x%016llx", vui->hw_if_index, msg.u64);
469  n =
470  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
471  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
472  {
473  vu_log_debug (vui, "could not send message response");
474  goto close_socket;
475  }
476  break;
477 
479  vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
480  "0x%016llx", vui->hw_if_index, msg.u64);
481 
482  vui->features = msg.u64;
483 
484  if (vui->features &
485  ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
486  (1ULL << FEAT_VIRTIO_F_VERSION_1)))
487  vui->virtio_net_hdr_sz = 12;
488  else
489  vui->virtio_net_hdr_sz = 10;
490 
491  vui->is_any_layout =
492  (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
493 
496  vui->is_ready = 0;
498  break;
499 
501  vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
502  vui->hw_if_index, msg.memory.nregions);
503 
504  if ((msg.memory.nregions < 1) ||
505  (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
506  {
507  vu_log_debug (vui, "number of mem regions must be between 1 and %i",
508  VHOST_MEMORY_MAX_NREGIONS);
509  goto close_socket;
510  }
511 
512  if (msg.memory.nregions != number_of_fds)
513  {
514  vu_log_debug (vui, "each memory region must have FD");
515  goto close_socket;
516  }
517 
518  /* Do the mmap without barrier sync */
519  void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
520  for (i = 0; i < msg.memory.nregions; i++)
521  {
522  long page_sz = get_huge_page_size (fds[i]);
523 
524  /* align size to page */
525  ssize_t map_sz = (msg.memory.regions[i].memory_size +
526  msg.memory.regions[i].mmap_offset +
527  page_sz - 1) & ~(page_sz - 1);
528 
529  region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
530  MAP_SHARED, fds[i], 0);
531  if (region_mmap_addr[i] == MAP_FAILED)
532  {
533  vu_log_err (vui, "failed to map memory. errno is %d", errno);
534  for (j = 0; j < i; j++)
535  munmap (region_mmap_addr[j], map_sz);
536  goto close_socket;
537  }
538  vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
539  "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
540  region_mmap_addr[i], page_sz);
541  }
542 
544  unmap_all_mem_regions (vui);
545  for (i = 0; i < msg.memory.nregions; i++)
546  {
547  clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
548  sizeof (vhost_user_memory_region_t));
549 
550  vui->region_mmap_addr[i] = region_mmap_addr[i];
551  vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
552  vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
553  vui->regions[i].memory_size;
554 
555  vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
556  vui->region_mmap_fd[i] = fds[i];
557 
558  vui->nregions++;
559  }
560 
561  /*
562  * Re-compute desc, used, and avail descriptor table if vring address
563  * is set.
564  */
565  for (q = 0; q < VHOST_VRING_MAX_N; q++)
566  {
567  if (vui->vrings[q].desc_user_addr &&
568  vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
569  {
570  vui->vrings[q].desc =
571  map_user_mem (vui, vui->vrings[q].desc_user_addr);
572  vui->vrings[q].used =
573  map_user_mem (vui, vui->vrings[q].used_user_addr);
574  vui->vrings[q].avail =
575  map_user_mem (vui, vui->vrings[q].avail_user_addr);
576  }
577  }
579  break;
580 
582  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
583  vui->hw_if_index, msg.state.index, msg.state.num);
584 
585  if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
586  (msg.state.num == 0) || /* it cannot be zero */
587  ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
588  goto close_socket;
589  vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
590  break;
591 
593  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
594  vui->hw_if_index, msg.state.index);
595 
596  if (msg.state.index >= VHOST_VRING_MAX_N)
597  {
598  vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
599  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
600  goto close_socket;
601  }
602 
603  if (msg.size < sizeof (msg.addr))
604  {
605  vu_log_debug (vui, "vhost message is too short (%d < %d)",
606  msg.size, sizeof (msg.addr));
607  goto close_socket;
608  }
609 
610  vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
611  vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
612  vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
613 
614  if ((desc == NULL) || (used == NULL) || (avail == NULL))
615  {
616  vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
617  vui->hw_if_index);
618  goto close_socket;
619  }
620 
621  vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
622  vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
623  vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
624 
626  vui->vrings[msg.state.index].desc = desc;
627  vui->vrings[msg.state.index].used = used;
628  vui->vrings[msg.state.index].avail = avail;
629 
630  vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
631  vui->vrings[msg.state.index].log_used =
632  (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
633 
634  /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
635  the ring is initialized in an enabled state. */
636  if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
637  vui->vrings[msg.state.index].enabled = 1;
638 
639  vui->vrings[msg.state.index].last_used_idx =
640  vui->vrings[msg.state.index].last_avail_idx =
641  vui->vrings[msg.state.index].used->idx;
642 
643  /* tell driver that we don't want interrupts */
644  vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
647  break;
648 
650  vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
651  break;
652 
654  vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
655  vui->hw_if_index);
656  break;
657 
659  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
660  vui->hw_if_index, msg.u64);
661 
662  q = (u8) (msg.u64 & 0xFF);
663 
664  /* if there is old fd, delete and close it */
665  if (vui->vrings[q].callfd_idx != ~0)
666  {
668  vui->vrings[q].callfd_idx);
669  clib_file_del (&file_main, uf);
670  vui->vrings[q].callfd_idx = ~0;
671  }
672 
673  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
674  {
675  if (number_of_fds != 1)
676  {
677  vu_log_debug (vui, "More than one fd received !");
678  goto close_socket;
679  }
680 
681  template.read_function = vhost_user_callfd_read_ready;
682  template.file_descriptor = fds[0];
683  template.private_data =
684  ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
685  vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
686  }
687  else
688  vui->vrings[q].callfd_idx = ~0;
689  break;
690 
692  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
693  vui->hw_if_index, msg.u64);
694 
695  q = (u8) (msg.u64 & 0xFF);
696 
697  if (vui->vrings[q].kickfd_idx != ~0)
698  {
700  vui->vrings[q].kickfd_idx);
701  clib_file_del (&file_main, uf);
702  vui->vrings[q].kickfd_idx = ~0;
703  }
704 
705  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
706  {
707  if (number_of_fds != 1)
708  {
709  vu_log_debug (vui, "More than one fd received !");
710  goto close_socket;
711  }
712 
713  template.read_function = vhost_user_kickfd_read_ready;
714  template.file_descriptor = fds[0];
715  template.private_data =
716  (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
717  q;
718  vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
719  }
720  else
721  {
722  //When no kickfd is set, the queue is initialized as started
723  vui->vrings[q].kickfd_idx = ~0;
724  vui->vrings[q].started = 1;
726  }
728  break;
729 
731  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
732  vui->hw_if_index, msg.u64);
733 
734  q = (u8) (msg.u64 & 0xFF);
735 
736  if (vui->vrings[q].errfd != -1)
737  close (vui->vrings[q].errfd);
738 
739  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
740  {
741  if (number_of_fds != 1)
742  goto close_socket;
743 
744  vui->vrings[q].errfd = fds[0];
745  }
746  else
747  vui->vrings[q].errfd = -1;
748  break;
749 
751  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
752  vui->hw_if_index, msg.state.index, msg.state.num);
754  vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
756  break;
757 
759  if (msg.state.index >= VHOST_VRING_MAX_N)
760  {
761  vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
762  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
763  goto close_socket;
764  }
765 
766  /* protection is needed to prevent rx/tx from changing last_avail_idx */
768  /*
769  * Copy last_avail_idx from the vring before closing it because
770  * closing the vring also initializes the vring last_avail_idx
771  */
772  msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
773  msg.flags |= 4;
774  msg.size = sizeof (msg.state);
775 
776  /*
777  * Spec says: Client must [...] stop ring upon receiving
778  * VHOST_USER_GET_VRING_BASE
779  */
780  vhost_user_vring_close (vui, msg.state.index);
782  vu_log_debug (vui, "if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
783  vui->hw_if_index, msg.state.index, msg.state.num);
784  n =
785  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
786  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
787  {
788  vu_log_debug (vui, "could not send message response");
789  goto close_socket;
790  }
792  break;
793 
794  case VHOST_USER_NONE:
795  vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
796  break;
797 
799  vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
800  vui->hw_if_index);
801 
802  if (msg.size != sizeof (msg.log))
803  {
804  vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
805  " %d instead of %d", msg.size, sizeof (msg.log));
806  goto close_socket;
807  }
808 
810  {
811  vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
812  "VHOST_USER_SET_LOG_BASE received");
813  goto close_socket;
814  }
815 
816  fd = fds[0];
817  /* align size to page */
818  long page_sz = get_huge_page_size (fd);
819  ssize_t map_sz =
820  (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
821 
822  void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
823  MAP_SHARED, fd, 0);
824 
825  vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
826  "mapped 0x%lx", map_sz, msg.log.offset, fd,
827  log_base_addr);
828 
829  if (log_base_addr == MAP_FAILED)
830  {
831  vu_log_err (vui, "failed to map memory. errno is %d", errno);
832  goto close_socket;
833  }
834 
836  vui->log_base_addr = log_base_addr;
837  vui->log_base_addr += msg.log.offset;
838  vui->log_size = msg.log.size;
840 
841  msg.flags |= 4;
842  msg.size = sizeof (msg.u64);
843  n =
844  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
845  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
846  {
847  vu_log_debug (vui, "could not send message response");
848  goto close_socket;
849  }
850  break;
851 
853  vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
854  break;
855 
857  msg.flags |= 4;
858  msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
860  msg.size = sizeof (msg.u64);
861  vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
862  "reply 0x%016llx", vui->hw_if_index, msg.u64);
863  n =
864  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
865  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
866  {
867  vu_log_debug (vui, "could not send message response");
868  goto close_socket;
869  }
870  break;
871 
873  vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
874  "features 0x%016llx", vui->hw_if_index, msg.u64);
875  vui->protocol_features = msg.u64;
876  break;
877 
879  msg.flags |= 4;
880  msg.u64 = VHOST_VRING_MAX_N;
881  msg.size = sizeof (msg.u64);
882  vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
883  vui->hw_if_index, msg.u64);
884  n =
885  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
886  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
887  {
888  vu_log_debug (vui, "could not send message response");
889  goto close_socket;
890  }
891  break;
892 
894  vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
895  vui->hw_if_index, msg.state.num ? "enable" : "disable",
896  msg.state.index);
897  if (msg.state.index >= VHOST_VRING_MAX_N)
898  {
899  vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
900  " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
901  goto close_socket;
902  }
903 
904  vui->vrings[msg.state.index].enabled = msg.state.num;
905  vhost_user_thread_placement (vui, msg.state.index);
907  break;
908 
909  default:
910  vu_log_debug (vui, "unknown vhost-user message %d received. "
911  "closing socket", msg.request);
912  goto close_socket;
913  }
914 
915  return 0;
916 
917 close_socket:
922  return 0;
923 }
924 
925 static clib_error_t *
927 {
930  vhost_user_intf_t *vui =
932 
933  vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
937  return 0;
938 }
939 
940 static clib_error_t *
942 {
943  int client_fd, client_len;
944  struct sockaddr_un client;
945  clib_file_t template = { 0 };
947  vhost_user_intf_t *vui;
948 
950 
951  client_len = sizeof (client);
952  client_fd = accept (uf->file_descriptor,
953  (struct sockaddr *) &client,
954  (socklen_t *) & client_len);
955 
956  if (client_fd < 0)
957  return clib_error_return_unix (0, "accept");
958 
959  if (vui->clib_file_index != ~0)
960  {
961  vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
964  }
965 
966  vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
967  vui->sw_if_index, client_fd);
968  template.read_function = vhost_user_socket_read;
969  template.error_function = vhost_user_socket_error;
970  template.file_descriptor = client_fd;
971  template.private_data = vui - vhost_user_main.vhost_user_interfaces;
972  vui->clib_file_index = clib_file_add (&file_main, &template);
973  return 0;
974 }
975 
976 static clib_error_t *
978 {
979  clib_error_t *error;
982 
983  error = vlib_call_init_function (vm, ip4_init);
984  if (error)
985  return error;
986 
987  vum->log_default = vlib_log_register_class ("vhost-user", 0);
988 
989  vum->coalesce_frames = 32;
990  vum->coalesce_time = 1e-3;
991 
992  vec_validate (vum->cpus, tm->n_vlib_mains - 1);
993 
994  vhost_cpu_t *cpu;
995  vec_foreach (cpu, vum->cpus)
996  {
997  /* This is actually not necessary as validate already zeroes it
998  * Just keeping the loop here for later because I am lazy. */
999  cpu->rx_buffers_len = 0;
1000  }
1001 
1002  vum->random = random_default_seed ();
1003 
1005 
1006  return 0;
1007 }
1008 
1010 
1011 static uword
1014 {
1015  vhost_user_intf_t *vui;
1016  f64 timeout = 3153600000.0 /* 100 years */ ;
1017  uword event_type, *event_data = 0;
1019  u16 qid;
1020  f64 now, poll_time_remaining;
1021  f64 next_timeout;
1022  u8 stop_timer = 0;
1023 
1024  while (1)
1025  {
1026  poll_time_remaining =
1028  event_type = vlib_process_get_events (vm, &event_data);
1029  vec_reset_length (event_data);
1030 
1031  /*
1032  * Use the remaining timeout if it is less than coalesce time to avoid
1033  * resetting the existing timer in the middle of expiration
1034  */
1035  timeout = poll_time_remaining;
1036  if (vlib_process_suspend_time_is_zero (timeout) ||
1037  (timeout > vum->coalesce_time))
1038  timeout = vum->coalesce_time;
1039 
1040  now = vlib_time_now (vm);
1041  switch (event_type)
1042  {
1044  stop_timer = 1;
1045  break;
1046 
1048  stop_timer = 0;
1049  if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1050  break;
1051  /* fall through */
1052 
1053  case ~0:
1054  /* *INDENT-OFF* */
1055  pool_foreach (vui, vum->vhost_user_interfaces, {
1056  next_timeout = timeout;
1057  for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1058  {
1059  vhost_user_vring_t *rxvq = &vui->vrings[qid];
1060  vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1061 
1062  if (txvq->qid == -1)
1063  continue;
1064  if (txvq->n_since_last_int)
1065  {
1066  if (now >= txvq->int_deadline)
1067  vhost_user_send_call (vm, txvq);
1068  else
1069  next_timeout = txvq->int_deadline - now;
1070  }
1071 
1072  if (rxvq->n_since_last_int)
1073  {
1074  if (now >= rxvq->int_deadline)
1075  vhost_user_send_call (vm, rxvq);
1076  else
1077  next_timeout = rxvq->int_deadline - now;
1078  }
1079 
1080  if ((next_timeout < timeout) && (next_timeout > 0.0))
1081  timeout = next_timeout;
1082  }
1083  });
1084  /* *INDENT-ON* */
1085  break;
1086 
1087  default:
1088  clib_warning ("BUG: unhandled event type %d", event_type);
1089  break;
1090  }
1091  /* No less than 1 millisecond */
1092  if (timeout < 1e-3)
1093  timeout = 1e-3;
1094  if (stop_timer)
1095  timeout = 3153600000.0;
1096  }
1097  return 0;
1098 }
1099 
1100 /* *INDENT-OFF* */
1103  .type = VLIB_NODE_TYPE_PROCESS,
1104  .name = "vhost-user-send-interrupt-process",
1105 };
1106 /* *INDENT-ON* */
1107 
1108 static uword
1111 {
1113  vhost_user_intf_t *vui;
1114  struct sockaddr_un sun;
1115  int sockfd;
1116  clib_file_t template = { 0 };
1117  f64 timeout = 3153600000.0 /* 100 years */ ;
1118  uword *event_data = 0;
1119 
1120  sockfd = -1;
1121  sun.sun_family = AF_UNIX;
1123  template.error_function = vhost_user_socket_error;
1124 
1125  while (1)
1126  {
1128  vlib_process_get_events (vm, &event_data);
1129  vec_reset_length (event_data);
1130 
1131  timeout = 3.0;
1132 
1133  /* *INDENT-OFF* */
1134  pool_foreach (vui, vum->vhost_user_interfaces, {
1135 
1136  if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1137  if (vui->clib_file_index == ~0)
1138  {
1139  if ((sockfd < 0) &&
1140  ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1141  {
1142  /*
1143  * 1st time error or new error for this interface,
1144  * spit out the message and record the error
1145  */
1146  if (!vui->sock_errno || (vui->sock_errno != errno))
1147  {
1148  clib_unix_warning
1149  ("Error: Could not open unix socket for %s",
1150  vui->sock_filename);
1151  vui->sock_errno = errno;
1152  }
1153  continue;
1154  }
1155 
1156  /* try to connect */
1157  strncpy (sun.sun_path, (char *) vui->sock_filename,
1158  sizeof (sun.sun_path) - 1);
1159 
1160  /* Avoid hanging VPP if the other end does not accept */
1161  if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1162  clib_unix_warning ("fcntl");
1163 
1164  if (connect (sockfd, (struct sockaddr *) &sun,
1165  sizeof (struct sockaddr_un)) == 0)
1166  {
1167  /* Set the socket to blocking as it was before */
1168  if (fcntl(sockfd, F_SETFL, 0) < 0)
1169  clib_unix_warning ("fcntl2");
1170 
1171  vui->sock_errno = 0;
1172  template.file_descriptor = sockfd;
1173  template.private_data =
1174  vui - vhost_user_main.vhost_user_interfaces;
1175  vui->clib_file_index = clib_file_add (&file_main, &template);
1176 
1177  /* This sockfd is considered consumed */
1178  sockfd = -1;
1179  }
1180  else
1181  {
1182  vui->sock_errno = errno;
1183  }
1184  }
1185  else
1186  {
1187  /* check if socket is alive */
1188  int error = 0;
1189  socklen_t len = sizeof (error);
1190  int fd = UNIX_GET_FD(vui->clib_file_index);
1191  int retval =
1192  getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1193 
1194  if (retval)
1195  {
1196  vu_log_debug (vui, "getsockopt returned %d", retval);
1197  vhost_user_if_disconnect (vui);
1198  }
1199  }
1200  }
1201  });
1202  /* *INDENT-ON* */
1203  }
1204  return 0;
1205 }
1206 
1207 /* *INDENT-OFF* */
1209  .function = vhost_user_process,
1210  .type = VLIB_NODE_TYPE_PROCESS,
1211  .name = "vhost-user-process",
1212 };
1213 /* *INDENT-ON* */
1214 
1215 /**
1216  * Disables and reset interface structure.
1217  * It can then be either init again, or removed from used interfaces.
1218  */
1219 static void
1221 {
1222  int q;
1224 
1225  // disconnect interface sockets
1228 
1229  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1230  {
1231  // Remove existing queue mapping for the interface
1232  if (q & 1)
1233  {
1234  int rv;
1235  vnet_main_t *vnm = vnet_get_main ();
1236  vhost_user_vring_t *txvq = &vui->vrings[q];
1237 
1238  if (txvq->qid != -1)
1239  {
1241  vui->hw_if_index,
1242  q >> 1);
1243  if (rv)
1244  vu_log_warn (vui, "unable to unassign interface %d, "
1245  "queue %d: rc=%d", vui->hw_if_index, q >> 1, rv);
1246  }
1247  }
1248 
1249  clib_mem_free ((void *) vui->vring_locks[q]);
1250  }
1251 
1252  if (vui->unix_server_index != ~0)
1253  {
1254  //Close server socket
1256  vui->unix_server_index);
1257  clib_file_del (&file_main, uf);
1258  vui->unix_server_index = ~0;
1259  unlink (vui->sock_filename);
1260  }
1261 
1263  &vui->if_index);
1264 }
1265 
1266 int
1268 {
1270  vhost_user_intf_t *vui;
1271  int rv = 0;
1272  vnet_hw_interface_t *hwif;
1273  u16 qid;
1274 
1275  if (!
1276  (hwif =
1278  || hwif->dev_class_index != vhost_user_device_class.index)
1279  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1280 
1282 
1283  vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1284  hwif->name, hwif->dev_instance);
1285 
1286  for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1287  {
1288  vhost_user_vring_t *txvq = &vui->vrings[qid];
1289 
1290  if (txvq->qid == -1)
1291  continue;
1292  if ((vum->ifq_count > 0) &&
1295  {
1296  vum->ifq_count--;
1297  // Stop the timer if there is no more interrupt interface/queue
1298  if ((vum->ifq_count == 0) &&
1299  (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1300  {
1304  break;
1305  }
1306  }
1307  }
1308 
1309  // Disable and reset interface
1310  vhost_user_term_if (vui);
1311 
1312  // Reset renumbered iface
1313  if (hwif->dev_instance <
1316 
1317  // Delete ethernet interface
1319 
1320  // Back to pool
1321  pool_put (vum->vhost_user_interfaces, vui);
1322 
1323  return rv;
1324 }
1325 
1326 static clib_error_t *
1328 {
1329  vnet_main_t *vnm = vnet_get_main ();
1331  vhost_user_intf_t *vui;
1332 
1334  /* *INDENT-OFF* */
1335  pool_foreach (vui, vum->vhost_user_interfaces, {
1336  vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1337  });
1338  /* *INDENT-ON* */
1340  return 0;
1341 }
1342 
1344 
1345 /**
1346  * Open server unix socket on specified sock_filename.
1347  */
1348 static int
1349 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1350 {
1351  int rv = 0;
1352  struct sockaddr_un un = { };
1353  int fd;
1354  /* create listening socket */
1355  if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1356  return VNET_API_ERROR_SYSCALL_ERROR_1;
1357 
1358  un.sun_family = AF_UNIX;
1359  strncpy ((char *) un.sun_path, (char *) sock_filename,
1360  sizeof (un.sun_path) - 1);
1361 
1362  /* remove if exists */
1363  unlink ((char *) sock_filename);
1364 
1365  if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1366  {
1367  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1368  goto error;
1369  }
1370 
1371  if (listen (fd, 1) == -1)
1372  {
1373  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1374  goto error;
1375  }
1376 
1377  *sock_fd = fd;
1378  return 0;
1379 
1380 error:
1381  close (fd);
1382  return rv;
1383 }
1384 
1385 /**
1386  * Create ethernet interface for vhost user interface.
1387  */
1388 static void
1390  vhost_user_intf_t * vui, u8 * hwaddress)
1391 {
1393  u8 hwaddr[6];
1394  clib_error_t *error;
1395 
1396  /* create hw and sw interface */
1397  if (hwaddress)
1398  {
1399  clib_memcpy (hwaddr, hwaddress, 6);
1400  }
1401  else
1402  {
1403  random_u32 (&vum->random);
1404  clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1405  hwaddr[0] = 2;
1406  hwaddr[1] = 0xfe;
1407  }
1408 
1410  (vnm,
1412  vui - vum->vhost_user_interfaces /* device instance */ ,
1413  hwaddr /* ethernet address */ ,
1414  &vui->hw_if_index, 0 /* flag change */ );
1415 
1416  if (error)
1417  clib_error_report (error);
1418 }
1419 
1420 /*
1421  * Initialize vui with specified attributes
1422  */
1423 static void
1425  vhost_user_intf_t * vui,
1426  int server_sock_fd,
1427  const char *sock_filename,
1428  u64 feature_mask, u32 * sw_if_index)
1429 {
1430  vnet_sw_interface_t *sw;
1431  int q;
1433  vnet_hw_interface_t *hw;
1434 
1435  hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1436  sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1437  if (server_sock_fd != -1)
1438  {
1439  clib_file_t template = { 0 };
1441  template.file_descriptor = server_sock_fd;
1442  template.private_data = vui - vum->vhost_user_interfaces; //hw index
1443  vui->unix_server_index = clib_file_add (&file_main, &template);
1444  }
1445  else
1446  {
1447  vui->unix_server_index = ~0;
1448  }
1449 
1450  vui->sw_if_index = sw->sw_if_index;
1451  strncpy (vui->sock_filename, sock_filename,
1452  ARRAY_LEN (vui->sock_filename) - 1);
1453  vui->sock_errno = 0;
1454  vui->is_ready = 0;
1455  vui->feature_mask = feature_mask;
1456  vui->clib_file_index = ~0;
1457  vui->log_base_addr = 0;
1458  vui->if_index = vui - vum->vhost_user_interfaces;
1460  &vui->if_index, 0);
1461 
1462  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1463  vhost_user_vring_init (vui, q);
1464 
1467 
1468  if (sw_if_index)
1469  *sw_if_index = vui->sw_if_index;
1470 
1471  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1472  {
1475  clib_memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
1476  }
1477 
1479  vlib_get_thread_main ()->n_vlib_mains - 1);
1481 }
1482 
1483 int
1485  const char *sock_filename,
1486  u8 is_server,
1487  u32 * sw_if_index,
1488  u64 feature_mask,
1489  u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
1490 {
1491  vhost_user_intf_t *vui = NULL;
1492  u32 sw_if_idx = ~0;
1493  int rv = 0;
1494  int server_sock_fd = -1;
1496  uword *if_index;
1497 
1498  if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1499  {
1500  return VNET_API_ERROR_INVALID_ARGUMENT;
1501  }
1502 
1503  if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1504  if (if_index)
1505  {
1506  if (sw_if_index)
1507  {
1508  vui = &vum->vhost_user_interfaces[*if_index];
1509  *sw_if_index = vui->sw_if_index;
1510  }
1511  return VNET_API_ERROR_IF_ALREADY_EXISTS;
1512  }
1513 
1514  if (is_server)
1515  {
1516  if ((rv =
1517  vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
1518  {
1519  return rv;
1520  }
1521  }
1522 
1523  /* Protect the uninitialized vui from being dispatched by rx/tx */
1525  pool_get (vhost_user_main.vhost_user_interfaces, vui);
1526  vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
1528 
1529  vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
1530  feature_mask, &sw_if_idx);
1531  vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1533 
1534  if (renumber)
1535  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1536 
1537  if (sw_if_index)
1538  *sw_if_index = sw_if_idx;
1539 
1540  // Process node must connect
1542 
1543  return rv;
1544 }
1545 
1546 int
1548  const char *sock_filename,
1549  u8 is_server,
1550  u32 sw_if_index,
1551  u64 feature_mask, u8 renumber, u32 custom_dev_instance)
1552 {
1554  vhost_user_intf_t *vui = NULL;
1555  u32 sw_if_idx = ~0;
1556  int server_sock_fd = -1;
1557  int rv = 0;
1558  vnet_hw_interface_t *hwif;
1559  uword *if_index;
1560 
1561  if (!
1562  (hwif =
1564  || hwif->dev_class_index != vhost_user_device_class.index)
1565  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1566 
1567  if (sock_filename == NULL || !(strlen (sock_filename) > 0))
1568  return VNET_API_ERROR_INVALID_ARGUMENT;
1569 
1571 
1572  /*
1573  * Disallow changing the interface to have the same path name
1574  * as other interface
1575  */
1576  if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
1577  if (if_index && (*if_index != vui->if_index))
1578  return VNET_API_ERROR_IF_ALREADY_EXISTS;
1579 
1580  // First try to open server socket
1581  if (is_server)
1582  if ((rv = vhost_user_init_server_sock (sock_filename,
1583  &server_sock_fd)) != 0)
1584  return rv;
1585 
1586  vhost_user_term_if (vui);
1587  vhost_user_vui_init (vnm, vui, server_sock_fd,
1588  sock_filename, feature_mask, &sw_if_idx);
1589 
1590  if (renumber)
1591  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1592 
1593  // Process node must connect
1595 
1596  return rv;
1597 }
1598 
1599 clib_error_t *
1601  unformat_input_t * input,
1602  vlib_cli_command_t * cmd)
1603 {
1604  unformat_input_t _line_input, *line_input = &_line_input;
1605  u8 *sock_filename = NULL;
1606  u32 sw_if_index;
1607  u8 is_server = 0;
1608  u64 feature_mask = (u64) ~ (0ULL);
1609  u8 renumber = 0;
1610  u32 custom_dev_instance = ~0;
1611  u8 hwaddr[6];
1612  u8 *hw = NULL;
1613  clib_error_t *error = NULL;
1614 
1615  /* Get a line of input. */
1616  if (!unformat_user (input, unformat_line_input, line_input))
1617  return 0;
1618 
1619  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1620  {
1621  if (unformat (line_input, "socket %s", &sock_filename))
1622  ;
1623  else if (unformat (line_input, "server"))
1624  is_server = 1;
1625  else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1626  ;
1627  else
1628  if (unformat
1629  (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1630  hw = hwaddr;
1631  else if (unformat (line_input, "renumber %d", &custom_dev_instance))
1632  {
1633  renumber = 1;
1634  }
1635  else
1636  {
1637  error = clib_error_return (0, "unknown input `%U'",
1638  format_unformat_error, line_input);
1639  goto done;
1640  }
1641  }
1642 
1643  vnet_main_t *vnm = vnet_get_main ();
1644 
1645  int rv;
1646  if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
1647  is_server, &sw_if_index, feature_mask,
1648  renumber, custom_dev_instance, hw)))
1649  {
1650  error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1651  goto done;
1652  }
1653 
1655  sw_if_index);
1656 
1657 done:
1658  vec_free (sock_filename);
1659  unformat_free (line_input);
1660 
1661  return error;
1662 }
1663 
1664 clib_error_t *
1666  unformat_input_t * input,
1667  vlib_cli_command_t * cmd)
1668 {
1669  unformat_input_t _line_input, *line_input = &_line_input;
1670  u32 sw_if_index = ~0;
1671  vnet_main_t *vnm = vnet_get_main ();
1672  clib_error_t *error = NULL;
1673 
1674  /* Get a line of input. */
1675  if (!unformat_user (input, unformat_line_input, line_input))
1676  return 0;
1677 
1678  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1679  {
1680  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1681  ;
1682  else if (unformat
1683  (line_input, "%U", unformat_vnet_sw_interface, vnm,
1684  &sw_if_index))
1685  {
1686  vnet_hw_interface_t *hwif =
1688  if (hwif == NULL ||
1690  {
1691  error = clib_error_return (0, "Not a vhost interface");
1692  goto done;
1693  }
1694  }
1695  else
1696  {
1697  error = clib_error_return (0, "unknown input `%U'",
1698  format_unformat_error, line_input);
1699  goto done;
1700  }
1701  }
1702 
1703  vhost_user_delete_if (vnm, vm, sw_if_index);
1704 
1705 done:
1706  unformat_free (line_input);
1707 
1708  return error;
1709 }
1710 
1711 int
1713  vhost_user_intf_details_t ** out_vuids)
1714 {
1715  int rv = 0;
1717  vhost_user_intf_t *vui;
1718  vhost_user_intf_details_t *r_vuids = NULL;
1720  u32 *hw_if_indices = 0;
1722  u8 *s = NULL;
1723  int i;
1724 
1725  if (!out_vuids)
1726  return -1;
1727 
1729  vec_add1 (hw_if_indices, vui->hw_if_index);
1730  );
1731 
1732  for (i = 0; i < vec_len (hw_if_indices); i++)
1733  {
1734  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1736 
1737  vec_add2 (r_vuids, vuid, 1);
1738  vuid->sw_if_index = vui->sw_if_index;
1739  vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1740  vuid->features = vui->features;
1741  vuid->num_regions = vui->nregions;
1742  vuid->is_server = vui->unix_server_index != ~0;
1743  vuid->sock_errno = vui->sock_errno;
1744  strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
1745  sizeof (vuid->sock_filename));
1746  vuid->sock_filename[ARRAY_LEN (vuid->sock_filename) - 1] = '\0';
1747  s = format (s, "%v%c", hi->name, 0);
1748 
1749  strncpy ((char *) vuid->if_name, (char *) s,
1750  ARRAY_LEN (vuid->if_name) - 1);
1751  _vec_len (s) = 0;
1752  }
1753 
1754  vec_free (s);
1755  vec_free (hw_if_indices);
1756 
1757  *out_vuids = r_vuids;
1758 
1759  return rv;
1760 }
1761 
1762 clib_error_t *
1764  unformat_input_t * input,
1765  vlib_cli_command_t * cmd)
1766 {
1767  clib_error_t *error = 0;
1768  vnet_main_t *vnm = vnet_get_main ();
1770  vhost_user_intf_t *vui;
1771  u32 hw_if_index, *hw_if_indices = 0;
1773  u16 qid;
1774  u32 ci;
1775  int i, j, q;
1776  int show_descr = 0;
1777  struct feat_struct
1778  {
1779  u8 bit;
1780  char *str;
1781  };
1782  struct feat_struct *feat_entry;
1783 
1784  static struct feat_struct feat_array[] = {
1785 #define _(s,b) { .str = #s, .bit = b, },
1787 #undef _
1788  {.str = NULL}
1789  };
1790 
1791 #define foreach_protocol_feature \
1792  _(VHOST_USER_PROTOCOL_F_MQ) \
1793  _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
1794 
1795  static struct feat_struct proto_feat_array[] = {
1796 #define _(s) { .str = #s, .bit = s},
1798 #undef _
1799  {.str = NULL}
1800  };
1801 
1802  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1803  {
1804  if (unformat
1805  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1806  {
1807  hi = vnet_get_hw_interface (vnm, hw_if_index);
1808  if (vhost_user_device_class.index != hi->dev_class_index)
1809  {
1810  error = clib_error_return (0, "unknown input `%U'",
1811  format_unformat_error, input);
1812  goto done;
1813  }
1814  vec_add1 (hw_if_indices, hw_if_index);
1815  }
1816  else if (unformat (input, "descriptors") || unformat (input, "desc"))
1817  show_descr = 1;
1818  else
1819  {
1820  error = clib_error_return (0, "unknown input `%U'",
1821  format_unformat_error, input);
1822  goto done;
1823  }
1824  }
1825  if (vec_len (hw_if_indices) == 0)
1826  {
1828  vec_add1 (hw_if_indices, vui->hw_if_index);
1829  );
1830  }
1831  vlib_cli_output (vm, "Virtio vhost-user interfaces");
1832  vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
1833  vum->coalesce_frames, vum->coalesce_time);
1834  vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d",
1835  vum->ifq_count);
1836 
1837  for (i = 0; i < vec_len (hw_if_indices); i++)
1838  {
1839  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1841  vlib_cli_output (vm, "Interface: %U (ifindex %d)",
1842  format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
1843  hw_if_indices[i]);
1844 
1845  vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
1846  " features mask (0x%llx): \n"
1847  " features (0x%llx): \n",
1848  vui->virtio_net_hdr_sz, vui->feature_mask,
1849  vui->features);
1850 
1851  feat_entry = (struct feat_struct *) &feat_array;
1852  while (feat_entry->str)
1853  {
1854  if (vui->features & (1ULL << feat_entry->bit))
1855  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1856  feat_entry->bit);
1857  feat_entry++;
1858  }
1859 
1860  vlib_cli_output (vm, " protocol features (0x%llx)",
1861  vui->protocol_features);
1862  feat_entry = (struct feat_struct *) &proto_feat_array;
1863  while (feat_entry->str)
1864  {
1865  if (vui->protocol_features & (1ULL << feat_entry->bit))
1866  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
1867  feat_entry->bit);
1868  feat_entry++;
1869  }
1870 
1871  vlib_cli_output (vm, "\n");
1872 
1873  vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1874  vui->sock_filename,
1875  (vui->unix_server_index != ~0) ? "server" : "client",
1876  strerror (vui->sock_errno));
1877 
1878  vlib_cli_output (vm, " rx placement: ");
1879 
1880  for (qid = 1; qid < VHOST_VRING_MAX_N / 2; qid += 2)
1881  {
1882  vnet_main_t *vnm = vnet_get_main ();
1883  uword thread_index;
1885  vhost_user_vring_t *txvq = &vui->vrings[qid];
1886 
1887  if (txvq->qid == -1)
1888  continue;
1889  thread_index =
1891  qid >> 1);
1892  vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, qid >> 1,
1893  &mode);
1894  vlib_cli_output (vm, " thread %d on vring %d, %U\n",
1895  thread_index, qid,
1897  }
1898 
1899  vlib_cli_output (vm, " tx placement: %s\n",
1900  vui->use_tx_spinlock ? "spin-lock" : "lock-free");
1901 
1903  {
1904  vlib_cli_output (vm, " thread %d on vring %d\n", ci,
1905  VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
1906  }
1907 
1908  vlib_cli_output (vm, "\n");
1909 
1910  vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
1911 
1912  if (vui->nregions)
1913  {
1914  vlib_cli_output (vm,
1915  " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1916  vlib_cli_output (vm,
1917  " ====== ===== ================== ================== ================== ================== ==================\n");
1918  }
1919  for (j = 0; j < vui->nregions; j++)
1920  {
1921  vlib_cli_output (vm,
1922  " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
1923  j, vui->region_mmap_fd[j],
1924  vui->regions[j].guest_phys_addr,
1925  vui->regions[j].memory_size,
1926  vui->regions[j].userspace_addr,
1927  vui->regions[j].mmap_offset,
1929  }
1930  for (q = 0; q < VHOST_VRING_MAX_N; q++)
1931  {
1932  if (!vui->vrings[q].started)
1933  continue;
1934 
1935  vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
1936  (q & 1) ? "RX" : "TX",
1937  vui->vrings[q].enabled ? "" : " disabled");
1938 
1939  vlib_cli_output (vm,
1940  " qsz %d last_avail_idx %d last_used_idx %d\n",
1941  vui->vrings[q].qsz_mask + 1,
1942  vui->vrings[q].last_avail_idx,
1943  vui->vrings[q].last_used_idx);
1944 
1945  if (vui->vrings[q].avail && vui->vrings[q].used)
1946  vlib_cli_output (vm,
1947  " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1948  vui->vrings[q].avail->flags,
1949  vui->vrings[q].avail->idx,
1950  vui->vrings[q].used->flags,
1951  vui->vrings[q].used->idx);
1952 
1953  int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
1954  int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
1955  vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
1956  kickfd, callfd, vui->vrings[q].errfd);
1957 
1958  if (show_descr)
1959  {
1960  vlib_cli_output (vm, "\n descriptor table:\n");
1961  vlib_cli_output (vm,
1962  " id addr len flags next user_addr\n");
1963  vlib_cli_output (vm,
1964  " ===== ================== ===== ====== ===== ==================\n");
1965  for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
1966  {
1967  u32 mem_hint = 0;
1968  vlib_cli_output (vm,
1969  " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1970  j, vui->vrings[q].desc[j].addr,
1971  vui->vrings[q].desc[j].len,
1972  vui->vrings[q].desc[j].flags,
1973  vui->vrings[q].desc[j].next,
1975  (vui,
1976  vui->vrings[q].desc[j].
1977  addr, &mem_hint)));
1978  }
1979  }
1980  }
1981  vlib_cli_output (vm, "\n");
1982  }
1983 done:
1984  vec_free (hw_if_indices);
1985  return error;
1986 }
1987 
1988 /*
1989  * CLI functions
1990  */
1991 
1992 /*?
1993  * Create a vHost User interface. Once created, a new virtual interface
1994  * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
1995  * is the next free index.
1996  *
1997  * There are several parameters associated with a vHost interface:
1998  *
1999  * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2000  * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2001  * create the socket if it does not already exist. If in '<em>client</em>' mode,
2002  * hypervisor will create the socket if it does not already exist. The VPP code
2003  * is indifferent to the file location. However, if SELinux is enabled, then the
2004  * socket needs to be created in '<em>/var/run/vpp/</em>'.
2005  *
2006  * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2007  * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2008  * mode, the VM can be reset without tearing down the vHost Interface. In
2009  * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2010  * tearing down the vHost Interface.
2011  *
2012  * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2013  * startup. <b>This is intended for degugging only.</b> It is recommended that this
2014  * parameter not be used except by experienced users. By default, all supported
2015  * features will be advertised. Otherwise, provide the set of features desired.
2016  * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2017  * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2018  * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2019  * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2020  * - 0x004000000 (26) - VHOST_F_LOG_ALL
2021  * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2022  * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2023  * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2024  * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2025  *
2026  * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2027  * X:X:X:X:X:X unix or X.X.X cisco format.
2028  *
2029  * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2030  * in the name to be specified. If instance already exists, name will be used
2031  * anyway and multiple instances will have the same name. Use with caution.
2032  *
2033  * @cliexpar
2034  * Example of how to create a vhost interface with VPP as the client and all features enabled:
2035  * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2036  * VirtualEthernet0/0/0
2037  * @cliexend
2038  * Example of how to create a vhost interface with VPP as the server and with just
2039  * multiple queues enabled:
2040  * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2041  * VirtualEthernet0/0/1
2042  * @cliexend
2043  * Once the vHost interface is created, enable the interface using:
2044  * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2045 ?*/
2046 /* *INDENT-OFF* */
2047 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2048  .path = "create vhost-user",
2049  .short_help = "create vhost-user socket <socket-filename> [server] "
2050  "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ",
2051  .function = vhost_user_connect_command_fn,
2052  .is_mp_safe = 1,
2053 };
2054 /* *INDENT-ON* */
2055 
2056 /*?
2057  * Delete a vHost User interface using the interface name or the
2058  * software interface index. Use the '<em>show interface</em>'
2059  * command to determine the software interface index. On deletion,
2060  * the linux socket will not be deleted.
2061  *
2062  * @cliexpar
2063  * Example of how to delete a vhost interface by name:
2064  * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2065  * Example of how to delete a vhost interface by software interface index:
2066  * @cliexcmd{delete vhost-user sw_if_index 1}
2067 ?*/
2068 /* *INDENT-OFF* */
2069 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2070  .path = "delete vhost-user",
2071  .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2072  .function = vhost_user_delete_command_fn,
2073 };
2074 
2075 /*?
2076  * Display the attributes of a single vHost User interface (provide interface
2077  * name), multiple vHost User interfaces (provide a list of interface names seperated
2078  * by spaces) or all Vhost User interfaces (omit an interface name to display all
2079  * vHost interfaces).
2080  *
2081  * @cliexpar
2082  * @parblock
2083  * Example of how to display a vhost interface:
2084  * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2085  * Virtio vhost-user interfaces
2086  * Global:
2087  * coalesce frames 32 time 1e-3
2088  * Interface: VirtualEthernet0/0/0 (ifindex 1)
2089  * virtio_net_hdr_sz 12
2090  * features mask (0xffffffffffffffff):
2091  * features (0x50408000):
2092  * VIRTIO_NET_F_MRG_RXBUF (15)
2093  * VIRTIO_NET_F_MQ (22)
2094  * VIRTIO_F_INDIRECT_DESC (28)
2095  * VHOST_USER_F_PROTOCOL_FEATURES (30)
2096  * protocol features (0x3)
2097  * VHOST_USER_PROTOCOL_F_MQ (0)
2098  * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2099  *
2100  * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2101  *
2102  * rx placement:
2103  * thread 1 on vring 1
2104  * thread 1 on vring 5
2105  * thread 2 on vring 3
2106  * thread 2 on vring 7
2107  * tx placement: spin-lock
2108  * thread 0 on vring 0
2109  * thread 1 on vring 2
2110  * thread 2 on vring 0
2111  *
2112  * Memory regions (total 2)
2113  * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2114  * ====== ===== ================== ================== ================== ================== ==================
2115  * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2116  * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2117  *
2118  * Virtqueue 0 (TX)
2119  * qsz 256 last_avail_idx 0 last_used_idx 0
2120  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2121  * kickfd 62 callfd 64 errfd -1
2122  *
2123  * Virtqueue 1 (RX)
2124  * qsz 256 last_avail_idx 0 last_used_idx 0
2125  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2126  * kickfd 65 callfd 66 errfd -1
2127  *
2128  * Virtqueue 2 (TX)
2129  * qsz 256 last_avail_idx 0 last_used_idx 0
2130  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2131  * kickfd 63 callfd 70 errfd -1
2132  *
2133  * Virtqueue 3 (RX)
2134  * qsz 256 last_avail_idx 0 last_used_idx 0
2135  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2136  * kickfd 72 callfd 74 errfd -1
2137  *
2138  * Virtqueue 4 (TX disabled)
2139  * qsz 256 last_avail_idx 0 last_used_idx 0
2140  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2141  * kickfd 76 callfd 78 errfd -1
2142  *
2143  * Virtqueue 5 (RX disabled)
2144  * qsz 256 last_avail_idx 0 last_used_idx 0
2145  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2146  * kickfd 80 callfd 82 errfd -1
2147  *
2148  * Virtqueue 6 (TX disabled)
2149  * qsz 256 last_avail_idx 0 last_used_idx 0
2150  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2151  * kickfd 84 callfd 86 errfd -1
2152  *
2153  * Virtqueue 7 (RX disabled)
2154  * qsz 256 last_avail_idx 0 last_used_idx 0
2155  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2156  * kickfd 88 callfd 90 errfd -1
2157  *
2158  * @cliexend
2159  *
2160  * The optional '<em>descriptors</em>' parameter will display the same output as
2161  * the previous example but will include the descriptor table for each queue.
2162  * The output is truncated below:
2163  * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2164  * Virtio vhost-user interfaces
2165  * Global:
2166  * coalesce frames 32 time 1e-3
2167  * Interface: VirtualEthernet0/0/0 (ifindex 1)
2168  * virtio_net_hdr_sz 12
2169  * features mask (0xffffffffffffffff):
2170  * features (0x50408000):
2171  * VIRTIO_NET_F_MRG_RXBUF (15)
2172  * VIRTIO_NET_F_MQ (22)
2173  * :
2174  * Virtqueue 0 (TX)
2175  * qsz 256 last_avail_idx 0 last_used_idx 0
2176  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2177  * kickfd 62 callfd 64 errfd -1
2178  *
2179  * descriptor table:
2180  * id addr len flags next user_addr
2181  * ===== ================== ===== ====== ===== ==================
2182  * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2183  * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2184  * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2185  * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2186  * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2187  * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2188  * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2189  * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2190  * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2191  * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2192  * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2193  * :
2194  * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2195  * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2196  * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2197  * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2198  * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2199  * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2200  * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2201  *
2202  * Virtqueue 1 (RX)
2203  * qsz 256 last_avail_idx 0 last_used_idx 0
2204  * :
2205  * @cliexend
2206  * @endparblock
2207 ?*/
2208 /* *INDENT-OFF* */
2209 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2210  .path = "show vhost-user",
2211  .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
2212  .function = show_vhost_user_command_fn,
2213 };
2214 /* *INDENT-ON* */
2215 
2216 
2217 static clib_error_t *
2219 {
2221 
2222  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2223  {
2224  if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2225  ;
2226  else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2227  ;
2228  else if (unformat (input, "dont-dump-memory"))
2229  vum->dont_dump_vhost_user_memory = 1;
2230  else
2231  return clib_error_return (0, "unknown input `%U'",
2232  format_unformat_error, input);
2233  }
2234 
2235  return 0;
2236 }
2237 
2238 /* vhost-user { ... } configuration. */
2239 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2240 
2241 void
2243 {
2245  vhost_user_intf_t *vui;
2246 
2247  if (vum->dont_dump_vhost_user_memory)
2248  {
2250  unmap_all_mem_regions (vui);
2251  );
2252  }
2253 }
2254 
2255 /*
2256  * fd.io coding-style-patch-verification: ON
2257  *
2258  * Local Variables:
2259  * eval: (c-set-style "gnu")
2260  * End:
2261  */
unformat_function_t unformat_vnet_hw_interface
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:227
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
u32 sw_if_index
Definition: ipsec_gre.api:37
u32 len
Definition: pci.h:200
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:237
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:703
VNET_HW_INTERFACE_CLASS(vhost_interface_class, static)
vhost_cpu_t * cpus
Per-CPU data for vhost-user.
Definition: vhost_user.h:353
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:47
u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:291
static clib_error_t * vhost_user_socksvr_accept_ready(clib_file_t *uf)
Definition: vhost_user.c:941
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
#define NULL
Definition: clib.h:58
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:255
static_always_inline void vhost_user_update_iface_state(vhost_user_intf_t *vui)
Definition: vhost_user.c:197
u16 next
Definition: pci.h:202
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:51
vring_avail_t * avail
Definition: vhost_user.h:238
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:1355
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:1424
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:266
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
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:208
u16 flags
Definition: pci.h:221
#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:239
vhost_vring_addr_t addr
Definition: vhost_user.h:121
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:1547
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:440
#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
#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:199
vnet_hw_interface_rx_mode
Definition: interface.h:52
#define static_always_inline
Definition: clib.h:99
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
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:546
static clib_error_t * ip4_init(vlib_main_t *vm)
Definition: ip4_input.c:351
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:301
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:494
#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:1208
#define vlib_call_init_function(vm, x)
Definition: init.h:260
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:926
void vhost_user_unmap_all(void)
Definition: vhost_user.c:2242
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: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:1220
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:964
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:356
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:1484
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:1267
format_function_t format_vnet_hw_interface_rx_mode
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:172
char sock_filename[256]
Definition: vhost_user.h:276
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:292
int vhost_user_dump_ifs(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_details_t **out_vuids)
Definition: vhost_user.c:1712
vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:288
vlib_log_class_t log_default
Definition: vhost_user.h:362
#define VHOST_VRING_IDX_RX(qid)
Definition: vhost_user.h:23
u32 * show_dev_instance_by_real_dev_instance
Definition: vhost_user.h:347
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:346
#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:144
vlib_main_t * vm
Definition: buffer.c:312
#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:2218
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:168
#define VHOST_USER_EVENT_STOP_TIMER
Definition: vhost_user.h:267
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:1109
#define ARRAY_LEN(x)
Definition: clib.h:62
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:1665
vl_api_vxlan_gbp_api_tunnel_mode_t mode
Definition: vxlan_gbp.api:44
#define foreach_protocol_feature
u16 flags
Definition: pci.h:207
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:1349
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
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:1763
#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:205
volatile u32 * vring_locks[VHOST_VRING_MAX_N]
Definition: vhost_user.h:296
mhash_t if_index_by_sock_name
Definition: vhost_user.h:344
static clib_error_t * vhost_user_exit(vlib_main_t *vm)
Definition: vhost_user.c:1327
#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:1389
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:289
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:295
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:504
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: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:1600
void vnet_sw_interface_set_mtu(vnet_main_t *vnm, u32 sw_if_index, u32 mtu)
Definition: interface.c:642
u32 rx_buffers_len
Definition: vhost_user.h:331
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:222
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:140
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:1487
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:290
u16 flags
Definition: pci.h:201
#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:977
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:1012
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:762
vl_api_address_union_t un
Definition: ip_types.api:32
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:350