FD.io VPP  v21.06
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/ethernet/ethernet.h>
37 #include <vnet/devices/devices.h>
38 #include <vnet/feature/feature.h>
40 
43 
44 /**
45  * @file
46  * @brief vHost User Device Driver.
47  *
48  * This file contains the source code for vHost User interface.
49  */
50 
51 
53 
54 /* *INDENT-OFF* */
55 vhost_user_main_t vhost_user_main = {
56  .mtu_bytes = 1518,
57 };
58 
59 VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
60  .name = "vhost-user",
61 };
62 /* *INDENT-ON* */
63 
64 static long
66 {
67  struct statfs s;
68  fstatfs (fd, &s);
69  return s.f_bsize;
70 }
71 
72 static void
74 {
75  int i, r, q;
77 
78  for (i = 0; i < vui->nregions; i++)
79  {
80  if (vui->region_mmap_addr[i] != MAP_FAILED)
81  {
82 
83  long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
84 
85  ssize_t map_sz = (vui->regions[i].memory_size +
86  vui->regions[i].mmap_offset +
87  page_sz - 1) & ~(page_sz - 1);
88 
89  r =
90  munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
91  map_sz);
92 
93  vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx "
94  "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz,
95  page_sz);
96 
97  vui->region_mmap_addr[i] = MAP_FAILED;
98 
99  if (r == -1)
100  {
101  vu_log_err (vui, "failed to unmap memory region (errno %d)",
102  errno);
103  }
104  close (vui->region_mmap_fd[i]);
105  }
106  }
107  vui->nregions = 0;
108 
109  for (q = 0; q < vui->num_qid; q++)
110  {
111  vq = &vui->vrings[q];
112  vq->avail = 0;
113  vq->used = 0;
114  vq->desc = 0;
115  }
116 }
117 
120 {
121  //Let's try to assign one queue to each thread
122  u32 qid;
123  u32 thread_index = 0;
124 
125  vui->use_tx_spinlock = 0;
126  while (1)
127  {
128  for (qid = 0; qid < vui->num_qid / 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  */
161 {
162  vhost_user_vring_t *txvq = &vui->vrings[qid];
163  vnet_main_t *vnm = vnet_get_main ();
164  int rv;
165  u32 q = qid >> 1;
166 
167  ASSERT ((qid & 1) == 1); // should be odd
168  // Assign new queue mappings for the interface
170  vhost_user_input_node.index);
173  if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN)
174  /* Set polling as the default */
176  txvq->qid = q;
177  rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, 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);
182 }
183 
184 /** @brief Returns whether at least one TX and one RX vring are enabled */
187 {
188  int i, found[2] = { }; //RX + TX
189 
190  for (i = 0; i < vui->num_qid; i++)
191  if (vui->vrings[i].started && vui->vrings[i].enabled)
192  found[i & 1] = 1;
193 
194  return found[0] && found[1];
195 }
196 
199 {
200  /* if we have pointers to descriptor table, go up */
201  int is_ready = vhost_user_intf_ready (vui);
202  if (is_ready != vui->is_ready)
203  {
204  vu_log_debug (vui, "interface %d %s", vui->sw_if_index,
205  is_ready ? "ready" : "down");
206  if (vui->admin_up)
209  : 0);
210  vui->is_ready = is_ready;
211  }
212 }
213 
214 static clib_error_t *
216 {
217  __attribute__ ((unused)) int n;
218  u8 buff[8];
219 
220  n = read (uf->file_descriptor, ((char *) &buff), 8);
221 
222  return 0;
223 }
224 
227 {
228  if (qid & 1) // RX is odd, TX is even
229  {
230  if (vui->vrings[qid].qid == -1)
232  }
233  else
235 }
236 
237 static clib_error_t *
239 {
240  __attribute__ ((unused)) ssize_t n;
241  u8 buff[8];
243  vhost_user_intf_t *vui =
245  u32 qid = uf->private_data & 0xff;
246  u32 is_txq = qid & 1;
247  vhost_user_vring_t *vq = &vui->vrings[qid];
248  vnet_main_t *vnm = vnet_get_main ();
249 
250  n = read (uf->file_descriptor, buff, 8);
251  if (vq->started == 0)
252  {
253  vq->started = 1;
254  vhost_user_thread_placement (vui, qid);
256  if (is_txq)
258  vq->kickfd_idx);
259  }
260 
261  if (is_txq && (vhost_user_intf_ready (vui) &&
262  ((vq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE) ||
265 
266  return 0;
267 }
268 
271 {
272  vhost_user_vring_t *vring = &vui->vrings[qid];
273 
274  clib_memset (vring, 0, sizeof (*vring));
275  vring->kickfd_idx = ~0;
276  vring->callfd_idx = ~0;
277  vring->errfd = -1;
278  vring->qid = -1;
279 
280  clib_spinlock_init (&vring->vring_lock);
281 
282  /*
283  * We have a bug with some qemu 2.5, and this may be a fix.
284  * Feel like interpretation holy text, but this is from vhost-user.txt.
285  * "
286  * One queue pair is enabled initially. More queues are enabled
287  * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
288  * "
289  * Don't know who's right, but this is what DPDK does.
290  */
291  if (qid == 0 || qid == 1)
292  vring->enabled = 1;
293 }
294 
297 {
298  vhost_user_vring_t *vring = &vui->vrings[qid];
299 
300  if (vring->kickfd_idx != ~0)
301  {
303  vring->kickfd_idx);
304  clib_file_del (&file_main, uf);
305  vring->kickfd_idx = ~0;
306  }
307  if (vring->callfd_idx != ~0)
308  {
310  vring->callfd_idx);
311  clib_file_del (&file_main, uf);
312  vring->callfd_idx = ~0;
313  }
314  if (vring->errfd != -1)
315  {
316  close (vring->errfd);
317  vring->errfd = -1;
318  }
319 
320  clib_spinlock_free (&vring->vring_lock);
321 
322  // save the qid so that we don't need to unassign and assign_rx_thread
323  // when the interface comes back up. They are expensive calls.
324  u16 q = vui->vrings[qid].qid;
325  vhost_user_vring_init (vui, qid);
326  vui->vrings[qid].qid = q;
327 }
328 
331 {
332  vnet_main_t *vnm = vnet_get_main ();
333  int q;
334 
336 
337  if (vui->clib_file_index != ~0)
338  {
340  vui->clib_file_index = ~0;
341  }
342 
343  vui->is_ready = 0;
344 
345  for (q = 0; q < vui->num_qid; q++)
346  vhost_user_vring_close (vui, q);
347 
348  unmap_all_mem_regions (vui);
349  vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index);
350 }
351 
352 static clib_error_t *
354 {
355  int n, i, j;
356  int fd, number_of_fds = 0;
357  int fds[VHOST_MEMORY_MAX_NREGIONS];
358  vhost_user_msg_t msg;
359  struct msghdr mh;
360  struct iovec iov[1];
362  vhost_user_intf_t *vui;
363  struct cmsghdr *cmsg;
364  u8 q;
365  clib_file_t template = { 0 };
366  vnet_main_t *vnm = vnet_get_main ();
368 
369  vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
370 
371  char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
372 
373  clib_memset (&mh, 0, sizeof (mh));
374  clib_memset (control, 0, sizeof (control));
375 
376  for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
377  fds[i] = -1;
378 
379  /* set the payload */
380  iov[0].iov_base = (void *) &msg;
381  iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
382 
383  mh.msg_iov = iov;
384  mh.msg_iovlen = 1;
385  mh.msg_control = control;
386  mh.msg_controllen = sizeof (control);
387 
388  n = recvmsg (uf->file_descriptor, &mh, 0);
389 
390  if (n != VHOST_USER_MSG_HDR_SZ)
391  {
392  if (n == -1)
393  {
394  vu_log_debug (vui, "recvmsg returned error %d %s", errno,
395  strerror (errno));
396  }
397  else
398  {
399  vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
401  }
402  goto close_socket;
403  }
404 
405  if (mh.msg_flags & MSG_CTRUNC)
406  {
407  vu_log_debug (vui, "MSG_CTRUNC is set");
408  goto close_socket;
409  }
410 
411  cmsg = CMSG_FIRSTHDR (&mh);
412 
413  if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
414  (cmsg->cmsg_type == SCM_RIGHTS) &&
415  (cmsg->cmsg_len - CMSG_LEN (0) <=
416  VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
417  {
418  number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
419  clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
420  }
421 
422  /* version 1, no reply bit set */
423  if ((msg.flags & 7) != 1)
424  {
425  vu_log_debug (vui, "malformed message received. closing socket");
426  goto close_socket;
427  }
428 
429  {
430  int rv;
431  rv =
432  read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
433  msg.size);
434  if (rv < 0)
435  {
436  vu_log_debug (vui, "read failed %s", strerror (errno));
437  goto close_socket;
438  }
439  else if (rv != msg.size)
440  {
441  vu_log_debug (vui, "message too short (read %dB should be %dB)", rv,
442  msg.size);
443  goto close_socket;
444  }
445  }
446 
447  switch (msg.request)
448  {
450  msg.flags |= 4;
451  msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
452  VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) |
453  VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) |
454  VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) |
455  VIRTIO_FEATURE (VHOST_F_LOG_ALL) |
456  VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) |
457  VIRTIO_FEATURE (VIRTIO_NET_F_MQ) |
458  VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) |
459  VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
460  msg.u64 &= vui->feature_mask;
461 
462  if (vui->enable_event_idx)
463  msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
464  if (vui->enable_gso)
466  if (vui->enable_packed)
467  msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
468 
469  msg.size = sizeof (msg.u64);
470  vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply "
471  "0x%016llx", vui->hw_if_index, msg.u64);
472  n =
473  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
474  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
475  {
476  vu_log_debug (vui, "could not send message response");
477  goto close_socket;
478  }
479  break;
480 
482  vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features "
483  "0x%016llx", vui->hw_if_index, msg.u64);
484 
485  vui->features = msg.u64;
486 
487  if (vui->features &
488  (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) |
489  VIRTIO_FEATURE (VIRTIO_F_VERSION_1)))
490  vui->virtio_net_hdr_sz = 12;
491  else
492  vui->virtio_net_hdr_sz = 10;
493 
494  vui->is_any_layout =
495  (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
496 
499  if (vui->enable_gso &&
502  {
506  }
507  else
508  {
511  }
513  vui->is_ready = 0;
515  break;
516 
518  vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
519  vui->hw_if_index, msg.memory.nregions);
520 
521  if ((msg.memory.nregions < 1) ||
522  (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
523  {
524  vu_log_debug (vui, "number of mem regions must be between 1 and %i",
525  VHOST_MEMORY_MAX_NREGIONS);
526  goto close_socket;
527  }
528 
529  if (msg.memory.nregions != number_of_fds)
530  {
531  vu_log_debug (vui, "each memory region must have FD");
532  goto close_socket;
533  }
534 
535  /* Do the mmap without barrier sync */
536  void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
537  for (i = 0; i < msg.memory.nregions; i++)
538  {
539  long page_sz = get_huge_page_size (fds[i]);
540 
541  /* align size to page */
542  ssize_t map_sz = (msg.memory.regions[i].memory_size +
543  msg.memory.regions[i].mmap_offset +
544  page_sz - 1) & ~(page_sz - 1);
545 
546  region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
547  MAP_SHARED, fds[i], 0);
548  if (region_mmap_addr[i] == MAP_FAILED)
549  {
550  vu_log_err (vui, "failed to map memory. errno is %d", errno);
551  for (j = 0; j < i; j++)
552  munmap (region_mmap_addr[j], map_sz);
553  goto close_socket;
554  }
555  vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d "
556  "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i],
557  region_mmap_addr[i], page_sz);
558  }
559 
561  unmap_all_mem_regions (vui);
562  for (i = 0; i < msg.memory.nregions; i++)
563  {
564  clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i],
565  sizeof (vhost_user_memory_region_t));
566 
567  vui->region_mmap_addr[i] = region_mmap_addr[i];
568  vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
569  vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
570  vui->regions[i].memory_size;
571 
572  vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
573  vui->region_mmap_fd[i] = fds[i];
574 
575  vui->nregions++;
576  }
577 
578  /*
579  * Re-compute desc, used, and avail descriptor table if vring address
580  * is set.
581  */
582  for (q = 0; q < vui->num_qid; q++)
583  {
584  if (vui->vrings[q].desc_user_addr &&
585  vui->vrings[q].used_user_addr && vui->vrings[q].avail_user_addr)
586  {
587  vui->vrings[q].desc =
588  map_user_mem (vui, vui->vrings[q].desc_user_addr);
589  vui->vrings[q].used =
590  map_user_mem (vui, vui->vrings[q].used_user_addr);
591  vui->vrings[q].avail =
592  map_user_mem (vui, vui->vrings[q].avail_user_addr);
593  }
594  }
596  break;
597 
599  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
600  vui->hw_if_index, msg.state.index, msg.state.num);
601 
602  if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
603  (msg.state.num == 0) || /* it cannot be zero */
604  ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */
605  (msg.state.index >= vui->num_qid))
606  {
607  vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num"
608  " %d, msg.state.index %d, curruent max q %d",
609  msg.state.num, msg.state.index, vui->num_qid);
610  goto close_socket;
611  }
612  vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
613  break;
614 
616  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
617  vui->hw_if_index, msg.state.index);
618 
619  if (msg.state.index >= vui->num_qid)
620  {
621  vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
622  " %u >= %u", msg.state.index, vui->num_qid);
623  goto close_socket;
624  }
625 
626  if (msg.size < sizeof (msg.addr))
627  {
628  vu_log_debug (vui, "vhost message is too short (%d < %d)",
629  msg.size, sizeof (msg.addr));
630  goto close_socket;
631  }
632 
633  vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr);
634  vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr);
635  vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr);
636 
637  if ((desc == NULL) || (used == NULL) || (avail == NULL))
638  {
639  vu_log_debug (vui, "failed to map user memory for hw_if_index %d",
640  vui->hw_if_index);
641  goto close_socket;
642  }
643 
644  vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr;
645  vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr;
646  vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr;
647 
649  vui->vrings[msg.state.index].desc = desc;
650  vui->vrings[msg.state.index].used = used;
651  vui->vrings[msg.state.index].avail = avail;
652 
653  vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
654  vui->vrings[msg.state.index].log_used =
655  (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
656 
657  /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
658  the ring is initialized in an enabled state. */
659  if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES)))
660  vui->vrings[msg.state.index].enabled = 1;
661 
662  vui->vrings[msg.state.index].last_used_idx =
663  vui->vrings[msg.state.index].last_avail_idx =
664  vui->vrings[msg.state.index].used->idx;
665  vui->vrings[msg.state.index].last_kick =
666  vui->vrings[msg.state.index].last_used_idx;
667 
668  /* tell driver that we don't want interrupts */
670  vui->vrings[msg.state.index].used_event->flags =
671  VRING_EVENT_F_DISABLE;
672  else
673  vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
676  break;
677 
679  vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
680  break;
681 
683  vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER",
684  vui->hw_if_index);
685  break;
686 
688  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d",
689  vui->hw_if_index, msg.u64);
690 
691  q = (u8) (msg.u64 & 0xFF);
692  if (vui->num_qid > q)
693  {
694  /* if there is old fd, delete and close it */
695  if (vui->vrings[q].callfd_idx != ~0)
696  {
698  vui->vrings[q].callfd_idx);
699  clib_file_del (&file_main, uf);
700  vui->vrings[q].callfd_idx = ~0;
701  }
702  }
703  else if (vec_len (vui->vrings) > q)
704  {
705  /* grow vrings by pair (RX + TX) */
706  vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
707  }
708  else
709  {
710  u32 i, new_max_q, old_max_q = vec_len (vui->vrings);
711 
712  /*
713  * Double the array size if it is less than 64 entries.
714  * Slow down thereafter.
715  */
716  if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3))
717  new_max_q = vec_len (vui->vrings) << 1;
718  else
719  new_max_q = vec_len (vui->vrings) +
721  if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1))
722  new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1);
723 
724  /* sync with the worker threads, vrings may move due to realloc */
726  vec_validate_aligned (vui->vrings, new_max_q - 1,
729 
730  for (i = old_max_q; i < vec_len (vui->vrings); i++)
731  vhost_user_vring_init (vui, i);
732 
733  /* grow vrings by pair (RX + TX) */
734  vui->num_qid = (q & 1) ? (q + 1) : (q + 2);
735  }
736 
737  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
738  {
739  if (number_of_fds != 1)
740  {
741  vu_log_debug (vui, "More than one fd received !");
742  goto close_socket;
743  }
744 
745  template.read_function = vhost_user_callfd_read_ready;
746  template.file_descriptor = fds[0];
747  template.private_data =
748  ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
749  template.description = format (0, "vhost user");
750  vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
751  }
752  else
753  vui->vrings[q].callfd_idx = ~0;
754  break;
755 
757  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d",
758  vui->hw_if_index, msg.u64);
759 
760  q = (u8) (msg.u64 & 0xFF);
761  if (q >= vui->num_qid)
762  {
763  vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:"
764  " %u >= %u", q, vui->num_qid);
765  goto close_socket;
766  }
767 
768  if (vui->vrings[q].kickfd_idx != ~0)
769  {
771  vui->vrings[q].kickfd_idx);
772  clib_file_del (&file_main, uf);
773  vui->vrings[q].kickfd_idx = ~0;
774  }
775 
776  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
777  {
778  if (number_of_fds != 1)
779  {
780  vu_log_debug (vui, "More than one fd received !");
781  goto close_socket;
782  }
783 
784  template.read_function = vhost_user_kickfd_read_ready;
785  template.file_descriptor = fds[0];
786  template.private_data =
787  (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
788  q;
789  vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
790  }
791  else
792  {
793  //When no kickfd is set, the queue is initialized as started
794  vui->vrings[q].kickfd_idx = ~0;
795  vui->vrings[q].started = 1;
797  }
799  break;
800 
802  vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d",
803  vui->hw_if_index, msg.u64);
804 
805  q = (u8) (msg.u64 & 0xFF);
806  if (q >= vui->num_qid)
807  {
808  vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:"
809  " %u >= %u", q, vui->num_qid);
810  goto close_socket;
811  }
812 
813  if (vui->vrings[q].errfd != -1)
814  close (vui->vrings[q].errfd);
815 
816  if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
817  {
818  if (number_of_fds != 1)
819  goto close_socket;
820 
821  vui->vrings[q].errfd = fds[0];
822  }
823  else
824  vui->vrings[q].errfd = -1;
825  break;
826 
828  vu_log_debug (vui,
829  "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x",
830  vui->hw_if_index, msg.state.index, msg.state.num);
831  if (msg.state.index >= vui->num_qid)
832  {
833  vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:"
834  " %u >= %u", msg.state.index, vui->num_qid);
835  goto close_socket;
836  }
838  vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
840  {
841  /*
842  * 0 1 2 3
843  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
844  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
845  * | last avail idx | | last used idx | |
846  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
847  * ^ ^
848  * | |
849  * avail wrap counter used wrap counter
850  */
851  /* last avail idx at bit 0-14. */
852  vui->vrings[msg.state.index].last_avail_idx =
853  msg.state.num & 0x7fff;
854  /* avail wrap counter at bit 15 */
855  vui->vrings[msg.state.index].avail_wrap_counter =
856  ! !(msg.state.num & (1 << 15));
857 
858  /*
859  * Although last_used_idx is passed in the upper 16 bits in qemu
860  * implementation, in practice, last_avail_idx and last_used_idx are
861  * usually the same. As a result, DPDK does not bother to pass us
862  * last_used_idx. The spec is not clear on thex coding. I figured it
863  * out by reading the qemu code. So let's just read last_avail_idx
864  * and set last_used_idx equals to last_avail_idx.
865  */
866  vui->vrings[msg.state.index].last_used_idx =
867  vui->vrings[msg.state.index].last_avail_idx;
868  vui->vrings[msg.state.index].last_kick =
869  vui->vrings[msg.state.index].last_used_idx;
870  vui->vrings[msg.state.index].used_wrap_counter =
871  vui->vrings[msg.state.index].avail_wrap_counter;
872 
873  if (vui->vrings[msg.state.index].avail_wrap_counter == 1)
874  vui->vrings[msg.state.index].avail_wrap_counter =
876  }
878  break;
879 
881  if (msg.state.index >= vui->num_qid)
882  {
883  vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:"
884  " %u >= %u", msg.state.index, vui->num_qid);
885  goto close_socket;
886  }
887 
888  /* protection is needed to prevent rx/tx from changing last_avail_idx */
890  /*
891  * Copy last_avail_idx from the vring before closing it because
892  * closing the vring also initializes the vring last_avail_idx
893  */
894  msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
896  {
897  msg.state.num =
898  (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) |
899  (! !vui->vrings[msg.state.index].avail_wrap_counter << 15);
900  msg.state.num |=
901  ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) |
902  (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16;
903  }
904  msg.flags |= 4;
905  msg.size = sizeof (msg.state);
906 
907  /*
908  * Spec says: Client must [...] stop ring upon receiving
909  * VHOST_USER_GET_VRING_BASE
910  */
911  vhost_user_vring_close (vui, msg.state.index);
913  vu_log_debug (vui,
914  "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x",
915  vui->hw_if_index, msg.state.index, msg.state.num);
916  n =
917  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
918  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
919  {
920  vu_log_debug (vui, "could not send message response");
921  goto close_socket;
922  }
924  break;
925 
926  case VHOST_USER_NONE:
927  vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index);
928  break;
929 
931  vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE",
932  vui->hw_if_index);
933 
934  if (msg.size != sizeof (msg.log))
935  {
936  vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:"
937  " %d instead of %d", msg.size, sizeof (msg.log));
938  goto close_socket;
939  }
940 
942  {
943  vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but "
944  "VHOST_USER_SET_LOG_BASE received");
945  goto close_socket;
946  }
947 
948  fd = fds[0];
949  /* align size to page */
950  long page_sz = get_huge_page_size (fd);
951  ssize_t map_sz =
952  (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
953 
954  void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
955  MAP_SHARED, fd, 0);
956 
957  vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d "
958  "mapped 0x%lx", map_sz, msg.log.offset, fd,
959  log_base_addr);
960 
961  if (log_base_addr == MAP_FAILED)
962  {
963  vu_log_err (vui, "failed to map memory. errno is %d", errno);
964  goto close_socket;
965  }
966 
968  vui->log_base_addr = log_base_addr;
969  vui->log_base_addr += msg.log.offset;
970  vui->log_size = msg.log.size;
972 
973  msg.flags |= 4;
974  msg.size = sizeof (msg.u64);
975  n =
976  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
977  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
978  {
979  vu_log_debug (vui, "could not send message response");
980  goto close_socket;
981  }
982  break;
983 
985  vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
986  break;
987 
989  msg.flags |= 4;
990  msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
992  msg.size = sizeof (msg.u64);
993  vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - "
994  "reply 0x%016llx", vui->hw_if_index, msg.u64);
995  n =
996  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
997  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
998  {
999  vu_log_debug (vui, "could not send message response");
1000  goto close_socket;
1001  }
1002  break;
1003 
1005  vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES "
1006  "features 0x%016llx", vui->hw_if_index, msg.u64);
1007  vui->protocol_features = msg.u64;
1008  break;
1009 
1011  msg.flags |= 4;
1012  msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ;
1013  msg.size = sizeof (msg.u64);
1014  vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1015  vui->hw_if_index, msg.u64);
1016  n =
1017  send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1018  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1019  {
1020  vu_log_debug (vui, "could not send message response");
1021  goto close_socket;
1022  }
1023  break;
1024 
1026  vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1027  vui->hw_if_index, msg.state.num ? "enable" : "disable",
1028  msg.state.index);
1029  if (msg.state.index >= vui->num_qid)
1030  {
1031  vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:"
1032  " %u >= %u", msg.state.index, vui->num_qid);
1033  goto close_socket;
1034  }
1035 
1036  vui->vrings[msg.state.index].enabled = msg.state.num;
1037  vhost_user_thread_placement (vui, msg.state.index);
1039  break;
1040 
1041  default:
1042  vu_log_debug (vui, "unknown vhost-user message %d received. "
1043  "closing socket", msg.request);
1044  goto close_socket;
1045  }
1046 
1047  return 0;
1048 
1049 close_socket:
1054  return 0;
1055 }
1056 
1057 static clib_error_t *
1059 {
1062  vhost_user_intf_t *vui =
1064 
1065  vu_log_debug (vui, "socket error on if %d", vui->sw_if_index);
1069  return 0;
1070 }
1071 
1072 static clib_error_t *
1074 {
1075  int client_fd, client_len;
1076  struct sockaddr_un client;
1077  clib_file_t template = { 0 };
1079  vhost_user_intf_t *vui;
1080 
1082 
1083  client_len = sizeof (client);
1084  client_fd = accept (uf->file_descriptor,
1085  (struct sockaddr *) &client,
1086  (socklen_t *) & client_len);
1087 
1088  if (client_fd < 0)
1089  return clib_error_return_unix (0, "accept");
1090 
1091  if (vui->clib_file_index != ~0)
1092  {
1093  vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d",
1094  vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index));
1096  }
1097 
1098  vu_log_debug (vui, "New client socket for vhost interface %d, fd %d",
1099  vui->sw_if_index, client_fd);
1100  template.read_function = vhost_user_socket_read;
1101  template.error_function = vhost_user_socket_error;
1102  template.file_descriptor = client_fd;
1103  template.private_data = vui - vhost_user_main.vhost_user_interfaces;
1104  template.description = format (0, "vhost interface %d", vui->sw_if_index);
1105  vui->clib_file_index = clib_file_add (&file_main, &template);
1106  vui->num_qid = 2;
1107  return 0;
1108 }
1109 
1110 static clib_error_t *
1112 {
1115 
1116  vum->log_default = vlib_log_register_class ("vhost-user", 0);
1117 
1118  vum->coalesce_frames = 32;
1119  vum->coalesce_time = 1e-3;
1120 
1121  vec_validate (vum->cpus, tm->n_vlib_mains - 1);
1122 
1123  vhost_cpu_t *cpu;
1124  vec_foreach (cpu, vum->cpus)
1125  {
1126  /* This is actually not necessary as validate already zeroes it
1127  * Just keeping the loop here for later because I am lazy. */
1128  cpu->rx_buffers_len = 0;
1129  }
1130 
1131  vum->random = random_default_seed ();
1132 
1134 
1135  return 0;
1136 }
1137 
1138 /* *INDENT-OFF* */
1140 {
1141  .runs_after = VLIB_INITS("ip4_init"),
1142 };
1143 /* *INDENT-ON* */
1144 
1145 static uword
1148 {
1149  vhost_user_intf_t *vui;
1150  f64 timeout = 3153600000.0 /* 100 years */ ;
1151  uword event_type, *event_data = 0;
1153  u16 qid;
1154  f64 now, poll_time_remaining;
1155  f64 next_timeout;
1156  u8 stop_timer = 0;
1157 
1158  while (1)
1159  {
1160  poll_time_remaining =
1162  event_type = vlib_process_get_events (vm, &event_data);
1163  vec_reset_length (event_data);
1164 
1165  /*
1166  * Use the remaining timeout if it is less than coalesce time to avoid
1167  * resetting the existing timer in the middle of expiration
1168  */
1169  timeout = poll_time_remaining;
1170  if (vlib_process_suspend_time_is_zero (timeout) ||
1171  (timeout > vum->coalesce_time))
1172  timeout = vum->coalesce_time;
1173 
1174  now = vlib_time_now (vm);
1175  switch (event_type)
1176  {
1178  stop_timer = 1;
1179  break;
1180 
1182  stop_timer = 0;
1183  if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
1184  break;
1185  /* fall through */
1186 
1187  case ~0:
1188  /* *INDENT-OFF* */
1189  pool_foreach (vui, vum->vhost_user_interfaces) {
1190  next_timeout = timeout;
1191  for (qid = 0; qid < vui->num_qid / 2; qid += 2)
1192  {
1193  vhost_user_vring_t *rxvq = &vui->vrings[qid];
1194  vhost_user_vring_t *txvq = &vui->vrings[qid + 1];
1195 
1196  if (txvq->qid == -1)
1197  continue;
1198  if (txvq->n_since_last_int)
1199  {
1200  if (now >= txvq->int_deadline)
1201  vhost_user_send_call (vm, vui, txvq);
1202  else
1203  next_timeout = txvq->int_deadline - now;
1204  }
1205 
1206  if (rxvq->n_since_last_int)
1207  {
1208  if (now >= rxvq->int_deadline)
1209  vhost_user_send_call (vm, vui, rxvq);
1210  else
1211  next_timeout = rxvq->int_deadline - now;
1212  }
1213 
1214  if ((next_timeout < timeout) && (next_timeout > 0.0))
1215  timeout = next_timeout;
1216  }
1217  }
1218  /* *INDENT-ON* */
1219  break;
1220 
1221  default:
1222  clib_warning ("BUG: unhandled event type %d", event_type);
1223  break;
1224  }
1225  /* No less than 1 millisecond */
1226  if (timeout < 1e-3)
1227  timeout = 1e-3;
1228  if (stop_timer)
1229  timeout = 3153600000.0;
1230  }
1231  return 0;
1232 }
1233 
1234 /* *INDENT-OFF* */
1237  .type = VLIB_NODE_TYPE_PROCESS,
1238  .name = "vhost-user-send-interrupt-process",
1239 };
1240 /* *INDENT-ON* */
1241 
1242 static uword
1245 {
1247  vhost_user_intf_t *vui;
1248  struct sockaddr_un sun;
1249  int sockfd;
1250  clib_file_t template = { 0 };
1251  f64 timeout = 3153600000.0 /* 100 years */ ;
1252  uword *event_data = 0;
1253 
1254  sockfd = -1;
1255  sun.sun_family = AF_UNIX;
1257  template.error_function = vhost_user_socket_error;
1258 
1259  while (1)
1260  {
1262  vlib_process_get_events (vm, &event_data);
1263  vec_reset_length (event_data);
1264 
1265  timeout = 3.0;
1266 
1267  /* *INDENT-OFF* */
1268  pool_foreach (vui, vum->vhost_user_interfaces) {
1269 
1270  if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
1271  if (vui->clib_file_index == ~0)
1272  {
1273  if ((sockfd < 0) &&
1274  ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
1275  {
1276  /*
1277  * 1st time error or new error for this interface,
1278  * spit out the message and record the error
1279  */
1280  if (!vui->sock_errno || (vui->sock_errno != errno))
1281  {
1283  ("Error: Could not open unix socket for %s",
1284  vui->sock_filename);
1285  vui->sock_errno = errno;
1286  }
1287  continue;
1288  }
1289 
1290  /* try to connect */
1291  strncpy (sun.sun_path, (char *) vui->sock_filename,
1292  sizeof (sun.sun_path) - 1);
1293  sun.sun_path[sizeof (sun.sun_path) - 1] = 0;
1294 
1295  /* Avoid hanging VPP if the other end does not accept */
1296  if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
1297  clib_unix_warning ("fcntl");
1298 
1299  if (connect (sockfd, (struct sockaddr *) &sun,
1300  sizeof (struct sockaddr_un)) == 0)
1301  {
1302  /* Set the socket to blocking as it was before */
1303  if (fcntl(sockfd, F_SETFL, 0) < 0)
1304  clib_unix_warning ("fcntl2");
1305 
1306  vui->sock_errno = 0;
1307  template.file_descriptor = sockfd;
1308  template.private_data =
1309  vui - vhost_user_main.vhost_user_interfaces;
1310  template.description = format (0, "vhost user process");
1311  vui->clib_file_index = clib_file_add (&file_main, &template);
1312  vui->num_qid = 2;
1313 
1314  /* This sockfd is considered consumed */
1315  sockfd = -1;
1316  }
1317  else
1318  {
1319  vui->sock_errno = errno;
1320  }
1321  }
1322  else
1323  {
1324  /* check if socket is alive */
1325  int error = 0;
1326  socklen_t len = sizeof (error);
1327  int fd = UNIX_GET_FD(vui->clib_file_index);
1328  int retval =
1329  getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
1330 
1331  if (retval)
1332  {
1333  vu_log_debug (vui, "getsockopt returned %d", retval);
1335  }
1336  }
1337  }
1338  }
1339  /* *INDENT-ON* */
1340  }
1341  return 0;
1342 }
1343 
1344 /* *INDENT-OFF* */
1346  .function = vhost_user_process,
1347  .type = VLIB_NODE_TYPE_PROCESS,
1348  .name = "vhost-user-process",
1349 };
1350 /* *INDENT-ON* */
1351 
1352 /**
1353  * Disables and reset interface structure.
1354  * It can then be either init again, or removed from used interfaces.
1355  */
1356 static void
1358 {
1359  int q;
1361 
1362  // disconnect interface sockets
1364  vhost_user_update_gso_interface_count (vui, 0 /* delete */ );
1366 
1367  for (q = 0; q < vui->num_qid; q++)
1368  {
1370  }
1371 
1372  if (vui->unix_server_index != ~0)
1373  {
1374  //Close server socket
1376  vui->unix_server_index);
1377  clib_file_del (&file_main, uf);
1378  vui->unix_server_index = ~0;
1379  unlink (vui->sock_filename);
1380  }
1381 
1383  &vui->if_index);
1384 }
1385 
1386 int
1388 {
1390  vhost_user_intf_t *vui;
1391  int rv = 0;
1392  vnet_hw_interface_t *hwif;
1393  u16 qid;
1394 
1395  if (!
1396  (hwif =
1398  || hwif->dev_class_index != vhost_user_device_class.index)
1399  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1400 
1402 
1403  vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)",
1404  hwif->name, hwif->dev_instance);
1405 
1406  for (qid = 1; qid < vui->num_qid / 2; qid += 2)
1407  {
1408  vhost_user_vring_t *txvq = &vui->vrings[qid];
1409 
1410  if (txvq->qid == -1)
1411  continue;
1412  if ((vum->ifq_count > 0) &&
1413  ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) ||
1414  (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)))
1415  {
1416  vum->ifq_count--;
1417  // Stop the timer if there is no more interrupt interface/queue
1418  if ((vum->ifq_count == 0) &&
1419  (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
1420  {
1424  break;
1425  }
1426  }
1427  }
1428 
1429  // Disable and reset interface
1430  vhost_user_term_if (vui);
1431 
1432  // Reset renumbered iface
1433  if (hwif->dev_instance <
1436 
1437  // Delete ethernet interface
1439 
1440  // free vrings
1441  vec_free (vui->vrings);
1442 
1443  // Back to pool
1444  pool_put (vum->vhost_user_interfaces, vui);
1445 
1446  return rv;
1447 }
1448 
1449 static clib_error_t *
1451 {
1452  vnet_main_t *vnm = vnet_get_main ();
1454  vhost_user_intf_t *vui;
1455 
1457  /* *INDENT-OFF* */
1458  pool_foreach (vui, vum->vhost_user_interfaces) {
1459  vhost_user_delete_if (vnm, vm, vui->sw_if_index);
1460  }
1461  /* *INDENT-ON* */
1463  return 0;
1464 }
1465 
1467 
1468 /**
1469  * Open server unix socket on specified sock_filename.
1470  */
1471 static int
1472 vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
1473 {
1474  int rv = 0;
1475  struct sockaddr_un un = { };
1476  int fd;
1477  /* create listening socket */
1478  if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
1479  return VNET_API_ERROR_SYSCALL_ERROR_1;
1480 
1481  un.sun_family = AF_UNIX;
1482  strncpy ((char *) un.sun_path, (char *) sock_filename,
1483  sizeof (un.sun_path) - 1);
1484 
1485  /* remove if exists */
1486  unlink ((char *) sock_filename);
1487 
1488  if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
1489  {
1490  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1491  goto error;
1492  }
1493 
1494  if (listen (fd, 1) == -1)
1495  {
1496  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1497  goto error;
1498  }
1499 
1500  *sock_fd = fd;
1501  return 0;
1502 
1503 error:
1504  close (fd);
1505  return rv;
1506 }
1507 
1508 /**
1509  * Create ethernet interface for vhost user interface.
1510  */
1511 static void
1513  vhost_user_intf_t *vui,
1515 {
1517  u8 hwaddr[6];
1519 
1520  /* create hw and sw interface */
1521  if (args->use_custom_mac)
1522  {
1523  clib_memcpy (hwaddr, args->hwaddr, 6);
1524  }
1525  else
1526  {
1527  random_u32 (&vum->random);
1528  clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
1529  hwaddr[0] = 2;
1530  hwaddr[1] = 0xfe;
1531  }
1532 
1534  (vnm,
1536  vui - vum->vhost_user_interfaces /* device instance */ ,
1537  hwaddr /* ethernet address */ ,
1538  &vui->hw_if_index, 0 /* flag change */ );
1539 
1540  if (error)
1541  clib_error_report (error);
1542 }
1543 
1544 /*
1545  * Initialize vui with specified attributes
1546  */
1547 static void
1549  int server_sock_fd, vhost_user_create_if_args_t * args,
1550  u32 * sw_if_index)
1551 {
1552  vnet_sw_interface_t *sw;
1553  int q;
1555  vnet_hw_interface_t *hw;
1556 
1557  hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
1558  sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
1559  if (server_sock_fd != -1)
1560  {
1561  clib_file_t template = { 0 };
1563  template.file_descriptor = server_sock_fd;
1564  template.private_data = vui - vum->vhost_user_interfaces; //hw index
1565  template.description = format (0, "vhost user %d", sw);
1566  vui->unix_server_index = clib_file_add (&file_main, &template);
1567  }
1568  else
1569  {
1570  vui->unix_server_index = ~0;
1571  }
1572 
1573  vui->sw_if_index = sw->sw_if_index;
1574  strncpy (vui->sock_filename, args->sock_filename,
1575  ARRAY_LEN (vui->sock_filename) - 1);
1576  vui->sock_errno = 0;
1577  vui->is_ready = 0;
1578  vui->feature_mask = args->feature_mask;
1579  vui->clib_file_index = ~0;
1580  vui->log_base_addr = 0;
1581  vui->if_index = vui - vum->vhost_user_interfaces;
1582  vui->enable_gso = args->enable_gso;
1583  vui->enable_event_idx = args->enable_event_idx;
1584  vui->enable_packed = args->enable_packed;
1585  /*
1586  * enable_gso takes precedence over configurable feature mask if there
1587  * is a clash.
1588  * if feature mask disables gso, but enable_gso is configured,
1589  * then gso is enable
1590  * if feature mask enables gso, but enable_gso is not configured,
1591  * then gso is enable
1592  *
1593  * if gso is enable via feature mask, it must enable both host and guest
1594  * gso feature mask, we don't support one sided GSO or partial GSO.
1595  */
1596  if ((vui->enable_gso == 0) &&
1599  vui->enable_gso = 1;
1600  vhost_user_update_gso_interface_count (vui, 1 /* add */ );
1602  &vui->if_index, 0);
1603 
1606  vui->num_qid = 2;
1607  for (q = 0; q < vec_len (vui->vrings); q++)
1608  vhost_user_vring_init (vui, q);
1609 
1612 
1613  if (sw_if_index)
1614  *sw_if_index = vui->sw_if_index;
1615 
1617  vlib_get_thread_main ()->n_vlib_mains - 1);
1619 }
1620 
1621 int
1624 {
1625  vhost_user_intf_t *vui = NULL;
1626  u32 sw_if_idx = ~0;
1627  int rv = 0;
1628  int server_sock_fd = -1;
1630  uword *if_index;
1631 
1632  if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1633  {
1634  return VNET_API_ERROR_INVALID_ARGUMENT;
1635  }
1636 
1637  if_index = mhash_get (&vum->if_index_by_sock_name,
1638  (void *) args->sock_filename);
1639  if (if_index)
1640  {
1641  vui = &vum->vhost_user_interfaces[*if_index];
1642  args->sw_if_index = vui->sw_if_index;
1643  return VNET_API_ERROR_IF_ALREADY_EXISTS;
1644  }
1645 
1646  if (args->is_server)
1647  {
1648  if ((rv =
1650  &server_sock_fd)) != 0)
1651  {
1652  return rv;
1653  }
1654  }
1655 
1656  /* Protect the uninitialized vui from being dispatched by rx/tx */
1658  pool_get (vhost_user_main.vhost_user_interfaces, vui);
1659  vhost_user_create_ethernet (vnm, vm, vui, args);
1661 
1662  vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1663  vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000);
1665 
1666  if (args->renumber)
1668 
1669  args->sw_if_index = sw_if_idx;
1670 
1671  // Process node must connect
1673 
1674  return rv;
1675 }
1676 
1677 int
1680 {
1682  vhost_user_intf_t *vui = NULL;
1683  u32 sw_if_idx = ~0;
1684  int server_sock_fd = -1;
1685  int rv = 0;
1686  vnet_hw_interface_t *hwif;
1687  uword *if_index;
1688 
1690  args->sw_if_index))
1691  || hwif->dev_class_index != vhost_user_device_class.index)
1692  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1693 
1694  if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0))
1695  return VNET_API_ERROR_INVALID_ARGUMENT;
1696 
1698 
1699  /*
1700  * Disallow changing the interface to have the same path name
1701  * as other interface
1702  */
1703  if_index = mhash_get (&vum->if_index_by_sock_name,
1704  (void *) args->sock_filename);
1705  if (if_index && (*if_index != vui->if_index))
1706  return VNET_API_ERROR_IF_ALREADY_EXISTS;
1707 
1708  // First try to open server socket
1709  if (args->is_server)
1710  if ((rv = vhost_user_init_server_sock (args->sock_filename,
1711  &server_sock_fd)) != 0)
1712  return rv;
1713 
1714  vhost_user_term_if (vui);
1715  vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx);
1716 
1717  if (args->renumber)
1719 
1720  // Process node must connect
1722 
1723  return rv;
1724 }
1725 
1726 clib_error_t *
1728  unformat_input_t * input,
1729  vlib_cli_command_t * cmd)
1730 {
1731  vnet_main_t *vnm = vnet_get_main ();
1732  unformat_input_t _line_input, *line_input = &_line_input;
1733  clib_error_t *error = NULL;
1734  vhost_user_create_if_args_t args = { 0 };
1735  int rv;
1736 
1737  /* Get a line of input. */
1738  if (!unformat_user (input, unformat_line_input, line_input))
1739  return 0;
1740 
1741  args.feature_mask = (u64) ~ (0ULL);
1742  args.custom_dev_instance = ~0;
1743  /* GSO feature is disable by default */
1745  /* packed-ring feature is disable by default */
1746  args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED);
1747  /* event_idx feature is disable by default */
1748  args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX);
1749 
1750  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1751  {
1752  if (unformat (line_input, "socket %s", &args.sock_filename))
1753  ;
1754  else if (unformat (line_input, "server"))
1755  args.is_server = 1;
1756  else if (unformat (line_input, "gso"))
1757  args.enable_gso = 1;
1758  else if (unformat (line_input, "packed"))
1759  args.enable_packed = 1;
1760  else if (unformat (line_input, "event-idx"))
1761  args.enable_event_idx = 1;
1762  else if (unformat (line_input, "feature-mask 0x%llx",
1763  &args.feature_mask))
1764  ;
1765  else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address,
1766  args.hwaddr))
1767  args.use_custom_mac = 1;
1768  else if (unformat (line_input, "renumber %d",
1769  &args.custom_dev_instance))
1770  args.renumber = 1;
1771  else
1772  {
1773  error = clib_error_return (0, "unknown input `%U'",
1774  format_unformat_error, line_input);
1775  goto done;
1776  }
1777  }
1778 
1779  if ((rv = vhost_user_create_if (vnm, vm, &args)))
1780  {
1781  error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
1782  goto done;
1783  }
1784 
1786  args.sw_if_index);
1787 
1788 done:
1789  vec_free (args.sock_filename);
1790  unformat_free (line_input);
1791 
1792  return error;
1793 }
1794 
1795 clib_error_t *
1797  unformat_input_t * input,
1798  vlib_cli_command_t * cmd)
1799 {
1800  unformat_input_t _line_input, *line_input = &_line_input;
1801  u32 sw_if_index = ~0;
1802  vnet_main_t *vnm = vnet_get_main ();
1803  clib_error_t *error = NULL;
1804 
1805  /* Get a line of input. */
1806  if (!unformat_user (input, unformat_line_input, line_input))
1807  return 0;
1808 
1809  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1810  {
1811  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1812  ;
1813  else if (unformat
1814  (line_input, "%U", unformat_vnet_sw_interface, vnm,
1815  &sw_if_index))
1816  {
1817  vnet_hw_interface_t *hwif =
1819  if (hwif == NULL ||
1821  {
1822  error = clib_error_return (0, "Not a vhost interface");
1823  goto done;
1824  }
1825  }
1826  else
1827  {
1828  error = clib_error_return (0, "unknown input `%U'",
1829  format_unformat_error, line_input);
1830  goto done;
1831  }
1832  }
1833 
1834  vhost_user_delete_if (vnm, vm, sw_if_index);
1835 
1836 done:
1837  unformat_free (line_input);
1838 
1839  return error;
1840 }
1841 
1842 int
1844  vhost_user_intf_details_t ** out_vuids)
1845 {
1846  int rv = 0;
1848  vhost_user_intf_t *vui;
1849  vhost_user_intf_details_t *r_vuids = NULL;
1850  vhost_user_intf_details_t *vuid = NULL;
1851  u32 *hw_if_indices = 0;
1853  int i;
1854 
1855  if (!out_vuids)
1856  return -1;
1857 
1859  vec_add1 (hw_if_indices, vui->hw_if_index);
1860 
1861  for (i = 0; i < vec_len (hw_if_indices); i++)
1862  {
1863  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1865 
1866  vec_add2 (r_vuids, vuid, 1);
1867  vuid->sw_if_index = vui->sw_if_index;
1868  vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
1869  vuid->features = vui->features;
1870  vuid->num_regions = vui->nregions;
1871  vuid->is_server = vui->unix_server_index != ~0;
1872  vuid->sock_errno = vui->sock_errno;
1873  snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename),
1874  "%s", vui->sock_filename);
1875  memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name,
1876  clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1));
1877  vuid->if_name[sizeof (vuid->if_name) - 1] = 0;
1878  }
1879 
1880  vec_free (hw_if_indices);
1881 
1882  *out_vuids = r_vuids;
1883 
1884  return rv;
1885 }
1886 
1887 static u8 *
1888 format_vhost_user_desc (u8 * s, va_list * args)
1889 {
1890  char *fmt = va_arg (*args, char *);
1891  vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1892  vring_desc_t *desc_table = va_arg (*args, vring_desc_t *);
1893  int idx = va_arg (*args, int);
1894  u32 *mem_hint = va_arg (*args, u32 *);
1895 
1896  s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1897  desc_table[idx].flags, desc_table[idx].next,
1898  pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1899  mem_hint)));
1900  return s;
1901 }
1902 
1903 static void
1905 {
1906  int kickfd = UNIX_GET_FD (vq->kickfd_idx);
1907  int callfd = UNIX_GET_FD (vq->callfd_idx);
1908 
1909  vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd,
1910  vq->errfd);
1911 }
1912 
1913 static void
1915  int show_descr, int show_verbose)
1916 {
1917  int j;
1918  u32 mem_hint = 0;
1919  u32 idx;
1920  u32 n_entries;
1921  vring_desc_t *desc_table;
1922  vhost_user_vring_t *vq = &vui->vrings[q];
1923 
1924  if (vq->avail && vq->used)
1925  vlib_cli_output (vm, " avail.flags %x avail event idx %u avail.idx %d "
1926  "used event idx %u used.idx %d\n", vq->avail->flags,
1928  vhost_user_used_event_idx (vq), vq->used->idx);
1929 
1930  vhost_user_show_fds (vm, vq);
1931 
1932  if (show_descr)
1933  {
1934  vlib_cli_output (vm, "\n descriptor table:\n");
1935  vlib_cli_output (vm,
1936  " slot addr len flags next "
1937  "user_addr\n");
1938  vlib_cli_output (vm,
1939  " ===== ================== ===== ====== ===== "
1940  "==================\n");
1941  for (j = 0; j < vq->qsz_mask + 1; j++)
1942  {
1943  desc_table = vq->desc;
1945  " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui,
1946  desc_table, j, &mem_hint);
1947  if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
1948  {
1949  n_entries = desc_table[j].len / sizeof (vring_desc_t);
1950  desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
1951  if (desc_table)
1952  {
1953  for (idx = 0; idx < clib_min (20, n_entries); idx++)
1954  {
1956  (vm, "%U", format_vhost_user_desc,
1957  "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
1958  desc_table, idx, &mem_hint);
1959  }
1960  if (n_entries >= 20)
1961  vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
1962  n_entries);
1963  }
1964  }
1965  }
1966  }
1967 }
1968 
1969 static u8 *
1970 format_vhost_user_packed_desc (u8 * s, va_list * args)
1971 {
1972  char *fmt = va_arg (*args, char *);
1973  vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *);
1974  vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *);
1975  int idx = va_arg (*args, int);
1976  u32 *mem_hint = va_arg (*args, u32 *);
1977 
1978  s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len,
1979  desc_table[idx].flags, desc_table[idx].id,
1980  pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr,
1981  mem_hint)));
1982  return s;
1983 }
1984 
1985 static u8 *
1987 {
1988  u32 flags = va_arg (*args, u32);
1989  typedef struct
1990  {
1991  u8 value;
1992  char *str;
1993  } event_idx_flags;
1994  static event_idx_flags event_idx_array[] = {
1995 #define _(s,v) { .str = #s, .value = v, },
1997 #undef _
1998  };
1999  u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags);
2000 
2001  if (flags < num_entries)
2002  s = format (s, "%s", event_idx_array[flags].str);
2003  else
2004  s = format (s, "%u", flags);
2005  return s;
2006 }
2007 
2008 static void
2010  int show_descr, int show_verbose)
2011 {
2012  int j;
2013  u32 mem_hint = 0;
2014  u32 idx;
2015  u32 n_entries;
2016  vring_packed_desc_t *desc_table;
2017  vhost_user_vring_t *vq = &vui->vrings[q];
2018  u16 off_wrap, event_idx;
2019 
2020  off_wrap = vq->avail_event->off_wrap;
2021  event_idx = off_wrap & 0x7fff;
2022  vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u "
2023  "avail event idx %u\n", format_vhost_user_event_idx_flags,
2024  (u32) vq->avail_event->flags, off_wrap, event_idx);
2025 
2026  off_wrap = vq->used_event->off_wrap;
2027  event_idx = off_wrap & 0x7fff;
2028  vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u "
2029  "used event idx %u\n", format_vhost_user_event_idx_flags,
2030  (u32) vq->used_event->flags, off_wrap, event_idx);
2031 
2032  vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n",
2034 
2035  vhost_user_show_fds (vm, vq);
2036 
2037  if (show_descr)
2038  {
2039  vlib_cli_output (vm, "\n descriptor table:\n");
2040  vlib_cli_output (vm,
2041  " slot addr len flags id "
2042  "user_addr\n");
2043  vlib_cli_output (vm,
2044  " ===== ================== ===== ====== ===== "
2045  "==================\n");
2046  for (j = 0; j < vq->qsz_mask + 1; j++)
2047  {
2048  desc_table = vq->packed_desc;
2050  " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2051  desc_table, j, &mem_hint);
2052  if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT))
2053  {
2054  n_entries = desc_table[j].len >> 4;
2055  desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint);
2056  if (desc_table)
2057  {
2058  for (idx = 0; idx < clib_min (20, n_entries); idx++)
2059  {
2061  (vm, "%U", format_vhost_user_packed_desc,
2062  "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui,
2063  desc_table, idx, &mem_hint);
2064  }
2065  if (n_entries >= 20)
2066  vlib_cli_output (vm, "Skip displaying entries 20...%u\n",
2067  n_entries);
2068  }
2069  }
2070  }
2071  }
2072 }
2073 
2074 clib_error_t *
2076  unformat_input_t * input,
2077  vlib_cli_command_t * cmd)
2078 {
2079  clib_error_t *error = 0;
2080  vnet_main_t *vnm = vnet_get_main ();
2082  vhost_user_intf_t *vui;
2083  u32 hw_if_index, *hw_if_indices = 0;
2085  u16 qid;
2086  u32 ci;
2087  int i, j, q;
2088  int show_descr = 0;
2089  int show_verbose = 0;
2090  struct feat_struct
2091  {
2092  u8 bit;
2093  char *str;
2094  };
2095  struct feat_struct *feat_entry;
2096 
2097  static struct feat_struct feat_array[] = {
2098 #define _(s,b) { .str = #s, .bit = b, },
2100 #undef _
2101  {.str = NULL}
2102  };
2103 
2104 #define foreach_protocol_feature \
2105  _(VHOST_USER_PROTOCOL_F_MQ) \
2106  _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
2107 
2108  static struct feat_struct proto_feat_array[] = {
2109 #define _(s) { .str = #s, .bit = s},
2111 #undef _
2112  {.str = NULL}
2113  };
2114 
2115  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2116  {
2117  if (unformat
2118  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2119  {
2120  hi = vnet_get_hw_interface (vnm, hw_if_index);
2121  if (vhost_user_device_class.index != hi->dev_class_index)
2122  {
2123  error = clib_error_return (0, "unknown input `%U'",
2124  format_unformat_error, input);
2125  goto done;
2126  }
2127  vec_add1 (hw_if_indices, hw_if_index);
2128  }
2129  else if (unformat (input, "descriptors") || unformat (input, "desc"))
2130  show_descr = 1;
2131  else if (unformat (input, "verbose"))
2132  show_verbose = 1;
2133  else
2134  {
2135  error = clib_error_return (0, "unknown input `%U'",
2136  format_unformat_error, input);
2137  goto done;
2138  }
2139  }
2140  if (vec_len (hw_if_indices) == 0)
2141  {
2143  vec_add1 (hw_if_indices, vui->hw_if_index);
2144  }
2145  vlib_cli_output (vm, "Virtio vhost-user interfaces");
2146  vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
2147  vum->coalesce_frames, vum->coalesce_time);
2148  vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d",
2149  vum->ifq_count);
2150  vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count);
2151 
2152  for (i = 0; i < vec_len (hw_if_indices); i++)
2153  {
2154  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
2156  vlib_cli_output (vm, "Interface: %U (ifindex %d)",
2157  format_vnet_hw_if_index_name, vnm, hw_if_indices[i],
2158  hw_if_indices[i]);
2159  vlib_cli_output (vm, " Number of qids %u", vui->num_qid);
2160  if (vui->enable_gso)
2161  vlib_cli_output (vm, " GSO enable");
2162  if (vui->enable_packed)
2163  vlib_cli_output (vm, " Packed ring enable");
2164  if (vui->enable_event_idx)
2165  vlib_cli_output (vm, " Event index enable");
2166 
2167  vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
2168  " features mask (0x%llx): \n"
2169  " features (0x%llx): \n",
2170  vui->virtio_net_hdr_sz, vui->feature_mask,
2171  vui->features);
2172 
2173  feat_entry = (struct feat_struct *) &feat_array;
2174  while (feat_entry->str)
2175  {
2176  if (vui->features & (1ULL << feat_entry->bit))
2177  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2178  feat_entry->bit);
2179  feat_entry++;
2180  }
2181 
2182  vlib_cli_output (vm, " protocol features (0x%llx)",
2183  vui->protocol_features);
2184  feat_entry = (struct feat_struct *) &proto_feat_array;
2185  while (feat_entry->str)
2186  {
2187  if (vui->protocol_features & (1ULL << feat_entry->bit))
2188  vlib_cli_output (vm, " %s (%d)", feat_entry->str,
2189  feat_entry->bit);
2190  feat_entry++;
2191  }
2192 
2193  vlib_cli_output (vm, "\n");
2194 
2195  vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
2196  vui->sock_filename,
2197  (vui->unix_server_index != ~0) ? "server" : "client",
2198  strerror (vui->sock_errno));
2199 
2200  vlib_cli_output (vm, " rx placement: ");
2201 
2202  for (qid = 1; qid < vui->num_qid / 2; qid += 2)
2203  {
2204  vnet_main_t *vnm = vnet_get_main ();
2206  vhost_user_vring_t *txvq = &vui->vrings[qid];
2207 
2208  if (txvq->qid == -1)
2209  continue;
2210  thread_index =
2212  vlib_cli_output (vm, " thread %d on vring %d, %U\n", thread_index,
2213  qid, format_vnet_hw_if_rx_mode, txvq->mode);
2214  }
2215 
2216  vlib_cli_output (vm, " tx placement: %s\n",
2217  vui->use_tx_spinlock ? "spin-lock" : "lock-free");
2218 
2220  {
2221  vlib_cli_output (vm, " thread %d on vring %d\n", ci,
2222  VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
2223  }
2224 
2225  vlib_cli_output (vm, "\n");
2226 
2227  vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
2228 
2229  if (vui->nregions)
2230  {
2231  vlib_cli_output (vm,
2232  " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
2233  vlib_cli_output (vm,
2234  " ====== ===== ================== ================== ================== ================== ==================\n");
2235  }
2236  for (j = 0; j < vui->nregions; j++)
2237  {
2238  vlib_cli_output (vm,
2239  " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
2240  j, vui->region_mmap_fd[j],
2241  vui->regions[j].guest_phys_addr,
2242  vui->regions[j].memory_size,
2243  vui->regions[j].userspace_addr,
2244  vui->regions[j].mmap_offset,
2246  }
2247  for (q = 0; q < vui->num_qid; q++)
2248  {
2249  if (!vui->vrings[q].started)
2250  continue;
2251 
2252  vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
2253  (q & 1) ? "RX" : "TX",
2254  vui->vrings[q].enabled ? "" : " disabled");
2255 
2256  vlib_cli_output (vm,
2257  " qsz %d last_avail_idx %d last_used_idx %d"
2258  " last_kick %u\n",
2259  vui->vrings[q].qsz_mask + 1,
2260  vui->vrings[q].last_avail_idx,
2261  vui->vrings[q].last_used_idx,
2262  vui->vrings[q].last_kick);
2263 
2265  vhost_user_show_desc_packed (vm, vui, q, show_descr,
2266  show_verbose);
2267  else
2268  vhost_user_show_desc (vm, vui, q, show_descr, show_verbose);
2269  }
2270  vlib_cli_output (vm, "\n");
2271  }
2272 done:
2273  vec_free (hw_if_indices);
2274  return error;
2275 }
2276 
2277 /*
2278  * CLI functions
2279  */
2280 
2281 /*?
2282  * Create a vHost User interface. Once created, a new virtual interface
2283  * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
2284  * is the next free index.
2285  *
2286  * There are several parameters associated with a vHost interface:
2287  *
2288  * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor
2289  * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will
2290  * create the socket if it does not already exist. If in '<em>client</em>' mode,
2291  * hypervisor will create the socket if it does not already exist. The VPP code
2292  * is indifferent to the file location. However, if SELinux is enabled, then the
2293  * socket needs to be created in '<em>/var/run/vpp/</em>'.
2294  *
2295  * - <b>server</b> - Optional flag to indicate that VPP should be the server for
2296  * the linux socket. If not provided, VPP will be the client. In '<em>server</em>'
2297  * mode, the VM can be reset without tearing down the vHost Interface. In
2298  * '<em>client</em>' mode, VPP can be reset without bringing down the VM and
2299  * tearing down the vHost Interface.
2300  *
2301  * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
2302  * startup. <b>This is intended for degugging only.</b> It is recommended that this
2303  * parameter not be used except by experienced users. By default, all supported
2304  * features will be advertised. Otherwise, provide the set of features desired.
2305  * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
2306  * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
2307  * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
2308  * - 0x000400000 (22) - VIRTIO_NET_F_MQ
2309  * - 0x004000000 (26) - VHOST_F_LOG_ALL
2310  * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
2311  * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
2312  * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
2313  * - 0x100000000 (32) - VIRTIO_F_VERSION_1
2314  *
2315  * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
2316  * X:X:X:X:X:X unix or X.X.X cisco format.
2317  *
2318  * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
2319  * in the name to be specified. If instance already exists, name will be used
2320  * anyway and multiple instances will have the same name. Use with caution.
2321  *
2322  * @cliexpar
2323  * Example of how to create a vhost interface with VPP as the client and all features enabled:
2324  * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock}
2325  * VirtualEthernet0/0/0
2326  * @cliexend
2327  * Example of how to create a vhost interface with VPP as the server and with just
2328  * multiple queues enabled:
2329  * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000}
2330  * VirtualEthernet0/0/1
2331  * @cliexend
2332  * Once the vHost interface is created, enable the interface using:
2333  * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
2334 ?*/
2335 /* *INDENT-OFF* */
2336 VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
2337  .path = "create vhost-user",
2338  .short_help = "create vhost-user socket <socket-filename> [server] "
2339  "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] "
2340  "[packed] [event-idx]",
2341  .function = vhost_user_connect_command_fn,
2342  .is_mp_safe = 1,
2343 };
2344 /* *INDENT-ON* */
2345 
2346 /*?
2347  * Delete a vHost User interface using the interface name or the
2348  * software interface index. Use the '<em>show interface</em>'
2349  * command to determine the software interface index. On deletion,
2350  * the linux socket will not be deleted.
2351  *
2352  * @cliexpar
2353  * Example of how to delete a vhost interface by name:
2354  * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
2355  * Example of how to delete a vhost interface by software interface index:
2356  * @cliexcmd{delete vhost-user sw_if_index 1}
2357 ?*/
2358 /* *INDENT-OFF* */
2359 VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
2360  .path = "delete vhost-user",
2361  .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
2362  .function = vhost_user_delete_command_fn,
2363 };
2364 
2365 /*?
2366  * Display the attributes of a single vHost User interface (provide interface
2367  * name), multiple vHost User interfaces (provide a list of interface names seperated
2368  * by spaces) or all Vhost User interfaces (omit an interface name to display all
2369  * vHost interfaces).
2370  *
2371  * @cliexpar
2372  * @parblock
2373  * Example of how to display a vhost interface:
2374  * @cliexstart{show vhost-user VirtualEthernet0/0/0}
2375  * Virtio vhost-user interfaces
2376  * Global:
2377  * coalesce frames 32 time 1e-3
2378  * Interface: VirtualEthernet0/0/0 (ifindex 1)
2379  * virtio_net_hdr_sz 12
2380  * features mask (0xffffffffffffffff):
2381  * features (0x50408000):
2382  * VIRTIO_NET_F_MRG_RXBUF (15)
2383  * VIRTIO_NET_F_MQ (22)
2384  * VIRTIO_F_INDIRECT_DESC (28)
2385  * VHOST_USER_F_PROTOCOL_FEATURES (30)
2386  * protocol features (0x3)
2387  * VHOST_USER_PROTOCOL_F_MQ (0)
2388  * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
2389  *
2390  * socket filename /var/run/vpp/vhost1.sock type client errno "Success"
2391  *
2392  * rx placement:
2393  * thread 1 on vring 1
2394  * thread 1 on vring 5
2395  * thread 2 on vring 3
2396  * thread 2 on vring 7
2397  * tx placement: spin-lock
2398  * thread 0 on vring 0
2399  * thread 1 on vring 2
2400  * thread 2 on vring 0
2401  *
2402  * Memory regions (total 2)
2403  * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
2404  * ====== ===== ================== ================== ================== ================== ==================
2405  * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
2406  * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
2407  *
2408  * Virtqueue 0 (TX)
2409  * qsz 256 last_avail_idx 0 last_used_idx 0
2410  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2411  * kickfd 62 callfd 64 errfd -1
2412  *
2413  * Virtqueue 1 (RX)
2414  * qsz 256 last_avail_idx 0 last_used_idx 0
2415  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2416  * kickfd 65 callfd 66 errfd -1
2417  *
2418  * Virtqueue 2 (TX)
2419  * qsz 256 last_avail_idx 0 last_used_idx 0
2420  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2421  * kickfd 63 callfd 70 errfd -1
2422  *
2423  * Virtqueue 3 (RX)
2424  * qsz 256 last_avail_idx 0 last_used_idx 0
2425  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2426  * kickfd 72 callfd 74 errfd -1
2427  *
2428  * Virtqueue 4 (TX disabled)
2429  * qsz 256 last_avail_idx 0 last_used_idx 0
2430  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2431  * kickfd 76 callfd 78 errfd -1
2432  *
2433  * Virtqueue 5 (RX disabled)
2434  * qsz 256 last_avail_idx 0 last_used_idx 0
2435  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2436  * kickfd 80 callfd 82 errfd -1
2437  *
2438  * Virtqueue 6 (TX disabled)
2439  * qsz 256 last_avail_idx 0 last_used_idx 0
2440  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2441  * kickfd 84 callfd 86 errfd -1
2442  *
2443  * Virtqueue 7 (RX disabled)
2444  * qsz 256 last_avail_idx 0 last_used_idx 0
2445  * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
2446  * kickfd 88 callfd 90 errfd -1
2447  *
2448  * @cliexend
2449  *
2450  * The optional '<em>descriptors</em>' parameter will display the same output as
2451  * the previous example but will include the descriptor table for each queue.
2452  * The output is truncated below:
2453  * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
2454  * Virtio vhost-user interfaces
2455  * Global:
2456  * coalesce frames 32 time 1e-3
2457  * Interface: VirtualEthernet0/0/0 (ifindex 1)
2458  * virtio_net_hdr_sz 12
2459  * features mask (0xffffffffffffffff):
2460  * features (0x50408000):
2461  * VIRTIO_NET_F_MRG_RXBUF (15)
2462  * VIRTIO_NET_F_MQ (22)
2463  * :
2464  * Virtqueue 0 (TX)
2465  * qsz 256 last_avail_idx 0 last_used_idx 0
2466  * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
2467  * kickfd 62 callfd 64 errfd -1
2468  *
2469  * descriptor table:
2470  * id addr len flags next user_addr
2471  * ===== ================== ===== ====== ===== ==================
2472  * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
2473  * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
2474  * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
2475  * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
2476  * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
2477  * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
2478  * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
2479  * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
2480  * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
2481  * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
2482  * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
2483  * :
2484  * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
2485  * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
2486  * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
2487  * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
2488  * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
2489  * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
2490  * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
2491  *
2492  * Virtqueue 1 (RX)
2493  * qsz 256 last_avail_idx 0 last_used_idx 0
2494  * :
2495  * @cliexend
2496  * @endparblock
2497 ?*/
2498 /* *INDENT-OFF* */
2499 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
2500  .path = "show vhost-user",
2501  .short_help = "show vhost-user [<interface> [<interface> [..]]] "
2502  "[[descriptors] [verbose]]",
2503  .function = show_vhost_user_command_fn,
2504 };
2505 /* *INDENT-ON* */
2506 
2507 
2508 static clib_error_t *
2510 {
2512 
2513  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2514  {
2515  if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
2516  ;
2517  else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
2518  ;
2519  else if (unformat (input, "dont-dump-memory"))
2520  vum->dont_dump_vhost_user_memory = 1;
2521  else
2522  return clib_error_return (0, "unknown input `%U'",
2523  format_unformat_error, input);
2524  }
2525 
2526  return 0;
2527 }
2528 
2529 /* vhost-user { ... } configuration. */
2530 VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
2531 
2532 void
2534 {
2536  vhost_user_intf_t *vui;
2537 
2538  if (vum->dont_dump_vhost_user_memory)
2539  {
2541  unmap_all_mem_regions (vui);
2542  }
2543 }
2544 
2545 /*
2546  * fd.io coding-style-patch-verification: ON
2547  *
2548  * Local Variables:
2549  * eval: (c-set-style "gnu")
2550  * End:
2551  */
unformat_function_t unformat_vnet_hw_interface
vlib_log_class_t vlib_log_register_class(char *class, char *subclass)
Definition: log.c:339
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:524
static_always_inline void vhost_user_send_call(vlib_main_t *vm, vhost_user_intf_t *vui, vhost_user_vring_t *vq)
format_function_t format_vnet_hw_if_rx_mode
#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
vnet_interface_output_runtime_t * rt
#define vu_log_warn(dev, f,...)
Definition: vhost_user.h:52
#define clib_min(x, y)
Definition: clib.h:342
vring_desc_t * desc
Definition: vhost_user.h:189
static u8 * format_vhost_user_event_idx_flags(u8 *s, va_list *args)
Definition: vhost_user.c:1986
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:39
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:755
VNET_HW_INTERFACE_CLASS(vhost_interface_class, static)
vhost_cpu_t * cpus
Per-CPU data for vhost-user.
Definition: vhost_user.h:339
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105
vring_desc_event_t * used_event
Definition: vhost_user.h:200
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:393
vnet_hw_if_output_node_runtime_t * r
static void stop_timer(wg_peer_t *peer, u32 timer_id)
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:261
void vnet_sw_interface_set_mtu(vnet_main_t *vnm, u32 sw_if_index, u32 mtu)
Definition: interface.c:706
u32 thread_index
static clib_error_t * vhost_user_socksvr_accept_ready(clib_file_t *uf)
Definition: vhost_user.c:1073
unsigned long u64
Definition: types.h:89
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
vnet_hw_interface_capabilities_t caps
Definition: interface.h:645
vring_packed_desc_t * packed_desc
Definition: vhost_user.h:190
static_always_inline void vhost_user_update_iface_state(vhost_user_intf_t *vui)
Definition: vhost_user.c:198
u64 private_data
Definition: file.h:64
#define VLIB_BUFFER_PRE_DATA_SIZE
Definition: buffer.h:51
vring_avail_t * avail
Definition: vhost_user.h:194
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:607
static clib_error_t * vhost_user_callfd_read_ready(clib_file_t *uf)
Definition: vhost_user.c:215
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:645
#define VHOST_USER_EVENT_START_TIMER
Definition: vhost_user.h:236
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
static_always_inline void vhost_user_vring_init(vhost_user_intf_t *vui, u32 qid)
Definition: vhost_user.c:270
static_always_inline void vnet_hw_if_rx_queue_set_int_pending(vnet_main_t *vnm, u32 queue_index)
unformat_function_t unformat_vnet_sw_interface
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:535
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:513
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
vring_used_t * used
Definition: vhost_user.h:199
vhost_vring_addr_t addr
Definition: vhost_user.h:130
static void vhost_user_create_ethernet(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_t *vui, vhost_user_create_if_args_t *args)
Create ethernet interface for vhost user interface.
Definition: vhost_user.c:1512
format_function_t format_vnet_sw_if_index_name
#define VHOST_USER_VRING_NOFD_MASK
Definition: vhost_user.h:36
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:160
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:474
#define VIRTIO_FEATURE(X)
Definition: virtio_std.h:69
#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
static void clib_spinlock_free(clib_spinlock_t *p)
Definition: lock.h:72
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:194
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
unsigned int u32
Definition: types.h:88
#define clib_memcpy(d, s, n)
Definition: string.h:197
#define vu_log_debug(dev, f,...)
Definition: vhost_user.h:45
vlib_frame_t * f
clib_file_t * file_pool
Definition: file.h:88
static void vhost_user_vui_init(vnet_main_t *vnm, vhost_user_intf_t *vui, int server_sock_fd, vhost_user_create_if_args_t *args, u32 *sw_if_index)
Definition: vhost_user.c:1548
static void vhost_user_show_fds(vlib_main_t *vm, vhost_user_vring_t *vq)
Definition: vhost_user.c:1904
#define static_always_inline
Definition: clib.h:112
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
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:583
static clib_error_t * vhost_user_socket_read(clib_file_t *uf)
Definition: vhost_user.c:353
#define UNIX_GET_FD(unixfd_idx)
Definition: vhost_user.h:65
static void vhost_user_show_desc(vlib_main_t *vm, vhost_user_intf_t *vui, int q, int show_descr, int show_verbose)
Definition: vhost_user.c:1914
#define event_idx
Definition: tls_async.c:41
void * log_base_addr
Definition: vhost_user.h:278
static_always_inline void vhost_user_thread_placement(vhost_user_intf_t *vui, u32 qid)
Definition: vhost_user.c:226
static_always_inline void * map_guest_mem(vhost_user_intf_t *vui, uword addr, u32 *hint)
description fragment has unexpected format
Definition: map.api:433
static void vhost_user_show_desc_packed(vlib_main_t *vm, vhost_user_intf_t *vui, int q, int show_descr, int show_verbose)
Definition: vhost_user.c:2009
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
vnet_main_t * vnet_get_main(void)
clib_file_main_t file_main
Definition: main.c:63
int __clib_unused rv
Definition: application.c:491
static vlib_node_registration_t vhost_user_process_node
(constructor) VLIB_REGISTER_NODE (vhost_user_process_node)
Definition: vhost_user.c:1345
static clib_error_t * vhost_user_kickfd_read_ready(clib_file_t *uf)
Definition: vhost_user.c:238
static clib_error_t * vhost_user_socket_error(clib_file_t *uf)
Definition: vhost_user.c:1058
void vhost_user_unmap_all(void)
Definition: vhost_user.c:2533
static u8 * format_vhost_user_packed_desc(u8 *s, va_list *args)
Definition: vhost_user.c:1970
unformat_function_t unformat_line_input
Definition: format.h:275
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:65
static_always_inline void vhost_user_tx_thread_placement(vhost_user_intf_t *vui)
Definition: vhost_user.c:119
int vhost_user_create_if(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_create_if_args_t *args)
Definition: vhost_user.c:1622
#define VHOST_USER_PROTOCOL_F_MQ
Definition: vhost_user.h:38
#define VRING_USED_F_NO_NOTIFY
Definition: virtio_std.h:92
Definition: cJSON.c:88
u16 * next
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
u32 vnet_hw_if_register_rx_queue(vnet_main_t *vnm, u32 hw_if_index, u32 queue_id, u32 thread_index)
Definition: rx_queue.c:64
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
static void vhost_user_term_if(vhost_user_intf_t *vui)
Disables and reset interface structure.
Definition: vhost_user.c:1357
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1019
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:186
u32 random
Pseudo random iterator.
Definition: vhost_user.h:342
vlib_node_registration_t vhost_user_input_node
(constructor) VLIB_REGISTER_NODE (vhost_user_input_node)
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define clib_error_return_unix(e, args...)
Definition: error.h:102
vhost_user_vring_t * vrings
Definition: vhost_user.h:265
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:305
int vhost_user_delete_if(vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index)
Definition: vhost_user.c:1387
static u8 * format_vhost_user_desc(u8 *s, va_list *args)
Definition: vhost_user.c:1888
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:181
int vhost_user_modify_if(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_create_if_args_t *args)
Definition: vhost_user.c:1678
char sock_filename[256]
Definition: vhost_user.h:246
vhost_user_main_t vhost_user_main
Definition: vhost_user.c:55
clib_spinlock_t vring_lock
Definition: vhost_user.h:209
#define VHOST_USER_MSG_HDR_SZ
Definition: vhost_user.h:24
u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:262
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
int vhost_user_dump_ifs(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_details_t **out_vuids)
Definition: vhost_user.c:1843
void vnet_hw_if_update_runtime_data(vnet_main_t *vnm, u32 hw_if_index)
Definition: runtime.c:58
vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:258
vlib_log_class_t log_default
Definition: vhost_user.h:348
int cJSON_bool fmt
Definition: cJSON.h:160
u8 len
Definition: ip_types.api:103
static_always_inline void vhost_user_update_gso_interface_count(vhost_user_intf_t *vui, u8 add)
int vnet_hw_if_set_rx_queue_mode(vnet_main_t *vnm, u32 queue_index, vnet_hw_if_rx_mode mode)
Definition: rx_queue.c:167
#define VHOST_VRING_IDX_RX(qid)
Definition: vhost_user.h:33
u32 * show_dev_instance_by_real_dev_instance
Definition: vhost_user.h:333
static_always_inline void vhost_user_vring_close(vhost_user_intf_t *vui, u32 qid)
Definition: vhost_user.c:296
vhost_user_intf_t * vhost_user_interfaces
Definition: vhost_user.h:332
#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:137
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
static void unmap_all_mem_regions(vhost_user_intf_t *vui)
Definition: vhost_user.c:73
static clib_error_t * vhost_user_config(vlib_main_t *vm, unformat_input_t *input)
Definition: vhost_user.c:2509
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:177
static_always_inline u64 vhost_user_is_packed_ring_supported(vhost_user_intf_t *vui)
#define VHOST_USER_EVENT_STOP_TIMER
Definition: vhost_user.h:237
static vnet_hw_interface_t * vnet_get_sup_hw_interface_api_visible_or_null(vnet_main_t *vnm, u32 sw_if_index)
#define VHOST_VRING_MAX_MQ_PAIR_SZ
Definition: vhost_user.h:32
#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:1243
#define ARRAY_LEN(x)
Definition: clib.h:70
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:1796
#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:1472
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
static_always_inline u16 vhost_user_used_event_idx(vhost_user_vring_t *vq)
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:2075
u8 value
Definition: qos.api:54
#define ASSERT(truth)
#define VHOST_VRING_F_LOG
Definition: vhost_user.h:40
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
static uword * mhash_get(mhash_t *h, const void *key)
Definition: mhash.h:110
#define vu_log_err(dev, f,...)
Definition: vhost_user.h:58
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:330
__clib_export errno_t memcpy_s(void *__restrict__ dest, rsize_t dmax, const void *__restrict__ src, rsize_t n)
copy src to dest, at most n bytes, up to dmax
Definition: string.c:120
vring_desc_event_t * avail_event
Definition: vhost_user.h:195
mhash_t if_index_by_sock_name
Definition: vhost_user.h:330
static clib_error_t * vhost_user_exit(vlib_main_t *vm)
Definition: vhost_user.c:1450
#define clib_error_report(e)
Definition: error.h:113
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
vlib_node_registration_t vhost_user_send_interrupt_node
(constructor) VLIB_REGISTER_NODE (vhost_user_send_interrupt_node)
Definition: vhost_user.c:52
static uword pointer_to_uword(const void *p)
Definition: types.h:131
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
vl_api_ip4_address_t hi
Definition: arp.api:37
struct _vlib_node_registration vlib_node_registration_t
__clib_export uword mhash_unset(mhash_t *h, void *key, uword *old_value)
Definition: mhash.c:346
void * region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:259
void vnet_hw_if_set_input_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: rx_queue.c:157
static_always_inline u16 vhost_user_avail_event_idx(vhost_user_vring_t *vq)
static long get_huge_page_size(int fd)
Definition: vhost_user.c:65
#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:348
#define VHOST_VRING_INIT_MQ_PAIR_SZ
Definition: vhost_user.h:25
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
#define clib_unix_warning(format, args...)
Definition: error.h:68
#define VHOST_MEMORY_MAX_NREGIONS
Definition: vhost_user.h:23
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:1727
u32 rx_buffers_len
Definition: vhost_user.h:314
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:1386
int vnet_interface_name_renumber(u32 sw_if_index, u32 new_show_dev_instance)
Definition: interface.c:1452
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:56
#define VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM
Definition: interface.h:555
f64 now
u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost_user.h:260
#define vec_foreach(var, vec)
Vector iterator.
Definition: file.h:51
static clib_error_t * vhost_user_init(vlib_main_t *vm)
Definition: vhost_user.c:1111
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:1146
#define FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS
Definition: vhost_user.h:96
#define VNET_HW_IF_RXQ_THREAD_ANY
Definition: interface.h:598
vl_api_address_union_t un
Definition: ip_types.api:98
#define VLIB_INITS(...)
Definition: init.h:352
__clib_export uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
Definition: mhash.c:264
void vnet_hw_if_set_rx_queue_file_index(vnet_main_t *vnm, u32 queue_index, u32 file_index)
Definition: rx_queue.c:144
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
#define VRING_DESC_F_INDIRECT
Definition: virtio_std.h:75
#define u8
Padding.
Definition: clib.h:121
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
int dont_dump_vhost_user_memory
Definition: vhost_user.h:336
static_always_inline u32 vnet_hw_if_get_rx_queue_thread_index(vnet_main_t *vnm, u32 queue_index)
#define VRING_DESC_F_AVAIL
Definition: virtio_std.h:77