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