FD.io VPP  v16.06
Vector Packet Processing
vhost_user.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <assert.h>
16 #include <sys/socket.h>
17 #include <sys/un.h>
18 #include <sys/stat.h>
19 #include <sys/vfs.h>
20 
21 #include <vlib/vlib.h>
22 #include <vlib/unix/unix.h>
23 
24 #include <vnet/vnet.h>
25 #include <vppinfra/vec.h>
26 #include <vppinfra/error.h>
27 #include <vppinfra/format.h>
28 
29 #include <vnet/ethernet/ethernet.h>
30 #include <vnet/devices/dpdk/dpdk.h>
31 
33 
34 #define VHOST_USER_DEBUG_SOCKET 0
35 
36 #if VHOST_USER_DEBUG_SOCKET == 1
37 #define DBG_SOCK(args...) clib_warning(args);
38 #else
39 #define DBG_SOCK(args...)
40 #endif
41 
42 static const char *vhost_message_str[] __attribute__((unused)) = {
43  [VHOST_USER_NONE] = "VHOST_USER_NONE",
44  [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
45  [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
46  [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
47  [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
48  [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
49  [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
50  [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
51  [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
52  [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
53  [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
54  [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
55  [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
56  [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
57  [VHOST_USER_SET_VRING_ERR] = "VHOST_USER_SET_VRING_ERR",
58 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
59  [VHOST_USER_GET_PROTOCOL_FEATURES] = "VHOST_USER_GET_PROTOCOL_FEATURES",
60  [VHOST_USER_SET_PROTOCOL_FEATURES] = "VHOST_USER_SET_PROTOCOL_FEATURES",
61  [VHOST_USER_GET_QUEUE_NUM] = "VHOST_USER_GET_QUEUE_NUM",
62  [VHOST_USER_SET_VRING_ENABLE] = "VHOST_USER_SET_VRING_ENABLE",
63 #endif
64 };
65 
66 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
67 static int dpdk_vhost_user_set_vring_enable(u32 hw_if_index,
68  u8 idx, int enable);
69 #endif
70 
71 /*
72  * DPDK vhost-user functions
73  */
74 
75 /* portions taken from dpdk
76  * BSD LICENSE
77  *
78  * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
79  * All rights reserved.
80  *
81  * Redistribution and use in source and binary forms, with or without
82  * modification, are permitted provided that the following conditions
83  * are met:
84  *
85  * * Redistributions of source code must retain the above copyright
86  * notice, this list of conditions and the following disclaimer.
87  * * Redistributions in binary form must reproduce the above copyright
88  * notice, this list of conditions and the following disclaimer in
89  * the documentation and/or other materials provided with the
90  * distribution.
91  * * Neither the name of Intel Corporation nor the names of its
92  * contributors may be used to endorse or promote products derived
93  * from this software without specific prior written permission.
94  *
95  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
96  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
97  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
98  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
99  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
100  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
101  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
102  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
103  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
104  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
105  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106  */
107 
108 
109 static uint64_t
110 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
111 {
112  struct virtio_memory_regions *region;
113  uint64_t vhost_va = 0;
114  uint32_t regionidx = 0;
115 
116  /* Find the region where the address lives. */
117  for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
118  region = &dev->mem->regions[regionidx];
119  if ((qemu_va >= region->userspace_address) &&
120  (qemu_va <= region->userspace_address +
121  region->memory_size)) {
122  vhost_va = qemu_va + region->guest_phys_address +
123  region->address_offset -
124  region->userspace_address;
125  break;
126  }
127  }
128  return vhost_va;
129 }
130 
131 static dpdk_device_t *
133 {
134  vnet_main_t *vnm = vnet_get_main();
135  dpdk_main_t * dm = &dpdk_main;
136  vnet_hw_interface_t * hi = vnet_get_hw_interface (vnm, hw_if_index);
138 
140  return 0;
141 
142  return xd;
143 }
144 
145 static dpdk_device_t *
147 {
148  vnet_main_t *vnm = vnet_get_main();
149  vnet_sw_interface_t * sw = vnet_get_sw_interface (vnm, sw_if_index);
151 
153 }
154 
155 static void stop_processing_packets(u32 hw_if_index, u8 idx)
156 {
157  dpdk_device_t *xd =
159  assert(xd);
160 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
161  xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
162 #else
163  xd->vu_is_running = 0;
164 #endif
165 }
166 
168 {
169 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
170  u8 idx;
171  int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
172  for (idx = 0; idx < numqs; idx++)
173  xd->vu_vhost_dev.virtqueue[idx]->enabled = 0;
174 #endif
175 
176  xd->vu_is_running = 0;
177 }
178 
179 static inline void * map_guest_mem(dpdk_device_t * xd, u64 addr)
180 {
181  dpdk_vu_intf_t * vui = xd->vu_intf;
182  struct virtio_memory * mem = xd->vu_vhost_dev.mem;
183  int i;
184  for (i=0; i<mem->nregions; i++) {
185  if ((mem->regions[i].guest_phys_address <= addr) &&
186  ((mem->regions[i].guest_phys_address + mem->regions[i].memory_size) > addr)) {
187  return (void *) (vui->region_addr[i] + addr - mem->regions[i].guest_phys_address);
188  }
189  }
190  DBG_SOCK("failed to map guest mem addr %lx", addr);
191  return 0;
192 }
193 
194 static clib_error_t *
195 dpdk_create_vhost_user_if_internal (u32 * hw_if_index, u32 if_id, u8 *hwaddr)
196 {
197  dpdk_main_t * dm = &dpdk_main;
198  vlib_main_t * vm = vlib_get_main();
200  vnet_sw_interface_t * sw;
201  clib_error_t * error;
203  int num_qpairs = 1;
204  dpdk_vu_intf_t *vui = NULL;
205 
206 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
207  num_qpairs = dm->use_rss < 1 ? 1 : tm->n_vlib_mains;
208 #endif
209 
210  dpdk_device_t * xd = NULL;
211  u8 addr[6];
212  int j;
213 
215 
216  int inactive_cnt = vec_len(dm->vu_inactive_interfaces_device_index);
217  // if there are any inactive ifaces
218  if (inactive_cnt > 0) {
219  // take last
220  u32 vui_idx = dm->vu_inactive_interfaces_device_index[inactive_cnt - 1];
221  if (vec_len(dm->devices) > vui_idx) {
222  xd = vec_elt_at_index (dm->devices, vui_idx);
223  if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER) {
224  DBG_SOCK("reusing inactive vhost-user interface sw_if_index %d", xd->vlib_sw_if_index);
225  } else {
226  clib_warning("error: inactive vhost-user interface sw_if_index %d not VHOST_USER type!",
227  xd->vlib_sw_if_index);
228  // reset so new interface is created
229  xd = NULL;
230  }
231  }
232  // "remove" from inactive list
233  _vec_len(dm->vu_inactive_interfaces_device_index) -= 1;
234  }
235 
236  if (xd) {
237  // existing interface used - do not overwrite if_id if not needed
238  if (if_id != (u32)~0)
239  xd->vu_if_id = if_id;
240 
241  // reset virtqueues
242  vui = xd->vu_intf;
243  for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
244  memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
245  xd->vu_vhost_dev.virtqueue[j]->kickfd = -1;
246  xd->vu_vhost_dev.virtqueue[j]->callfd = -1;
247  xd->vu_vhost_dev.virtqueue[j]->backend = -1;
248  vui->vrings[j].packets = 0;
249  vui->vrings[j].bytes = 0;
250  }
251 
252  // reset lockp
255 
256  // reset tx vectors
257  for (j = 0; j < tm->n_vlib_mains; j++)
258  {
261  vec_reset_length (xd->tx_vectors[j]);
262  }
263 
264  // reset rx vector
265  for (j = 0; j < xd->rx_q_used; j++)
266  {
269  vec_reset_length (xd->rx_vectors[j]);
270  }
271  } else {
272  // vui was not retrieved from inactive ifaces - create new
275  xd->rx_q_used = num_qpairs;
276  xd->tx_q_used = num_qpairs;
277 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
278  xd->vu_vhost_dev.virt_qp_nb = num_qpairs;
279 #endif
280 
282 
283  if (if_id == (u32)~0)
284  xd->vu_if_id = dm->next_vu_if_id++;
285  else
286  xd->vu_if_id = if_id;
287 
288  xd->device_index = xd - dm->devices;
289  xd->per_interface_next_index = ~0;
290  xd->vu_intf = clib_mem_alloc (sizeof(*(xd->vu_intf)));
291 
292  xd->vu_vhost_dev.mem = clib_mem_alloc (sizeof(struct virtio_memory) +
294  sizeof(struct virtio_memory_regions));
295 
296  /* Will be set when guest sends VHOST_USER_SET_MEM_TABLE cmd */
297  xd->vu_vhost_dev.mem->nregions = 0;
298 
299  /*
300  * New virtqueue structure is an array of VHOST_MAX_QUEUE_PAIRS * 2
301  * We need to allocate numq pairs.
302  */
303  vui = xd->vu_intf;
304  for (j = 0; j < num_qpairs * VIRTIO_QNUM; j++) {
305  xd->vu_vhost_dev.virtqueue[j] = clib_mem_alloc (sizeof(struct vhost_virtqueue));
306  memset(xd->vu_vhost_dev.virtqueue[j], 0, sizeof(struct vhost_virtqueue));
307  xd->vu_vhost_dev.virtqueue[j]->kickfd = -1;
308  xd->vu_vhost_dev.virtqueue[j]->callfd = -1;
309  xd->vu_vhost_dev.virtqueue[j]->backend = -1;
310  vui->vrings[j].packets = 0;
311  vui->vrings[j].bytes = 0;
312  }
313 
315 
316  DBG_SOCK("tm->n_vlib_mains: %d. TX %d, RX: %d, num_qpairs: %d, Lock: %p",
317  tm->n_vlib_mains, xd->tx_q_used, xd->rx_q_used, num_qpairs, xd->lockp);
318 
321 
322  for (j = 0; j < tm->n_vlib_mains; j++)
323  {
326  vec_reset_length (xd->tx_vectors[j]);
327  }
328 
329  // reset rx vector
330  for (j = 0; j < xd->rx_q_used; j++)
331  {
334  vec_reset_length (xd->rx_vectors[j]);
335  }
336 
339 
340  }
341  /*
342  * Generate random MAC address for the interface
343  */
344  if (hwaddr) {
345  clib_memcpy(addr, hwaddr, sizeof(addr));
346  } else {
347  f64 now = vlib_time_now(vm);
348  u32 rnd;
349  rnd = (u32) (now * 1e6);
350  rnd = random_u32 (&rnd);
351 
352  clib_memcpy (addr+2, &rnd, sizeof(rnd));
353  addr[0] = 2;
354  addr[1] = 0xfe;
355  }
356 
358  (dm->vnet_main,
359  dpdk_device_class.index,
360  xd->device_index,
361  /* ethernet address */ addr,
362  &xd->vlib_hw_if_index,
363  0);
364 
365  if (error)
366  return error;
367 
369  xd->vlib_sw_if_index = sw->sw_if_index;
370 
371  *hw_if_index = xd->vlib_hw_if_index;
372 
373  DBG_SOCK("xd->device_index: %d, dm->input_cpu_count: %d, "
374  "dm->input_cpu_first_index: %d\n", xd->device_index,
376 
377  int q, next_cpu = 0;
378  for (q = 0; q < num_qpairs; q++) {
379  int cpu = dm->input_cpu_first_index +
380  (next_cpu % dm->input_cpu_count);
381 
382  unsigned lcore = vlib_worker_threads[cpu].dpdk_lcore_id;
384  xd->cpu_socket_id_by_queue[q] = rte_lcore_to_socket_id(lcore);
385 
386  vec_add2(dm->devices_by_cpu[cpu], dq, 1);
387  dq->device = xd->device_index;
388  dq->queue_id = q;
389  DBG_SOCK("CPU for %d = %d. QID: %d", *hw_if_index, cpu, dq->queue_id);
390 
391  // start polling if it was not started yet (because of no phys ifaces)
392  if (tm->n_vlib_mains == 1 && dpdk_input_node.state != VLIB_NODE_STATE_POLLING)
393  vlib_node_set_state (vm, dpdk_input_node.index, VLIB_NODE_STATE_POLLING);
394 
395  if (tm->n_vlib_mains > 1 && tm->main_thread_is_io_node)
396  vlib_node_set_state (vm, dpdk_io_input_node.index, VLIB_NODE_STATE_POLLING);
397 
398  if (tm->n_vlib_mains > 1 && !tm->main_thread_is_io_node)
400  VLIB_NODE_STATE_POLLING);
401  next_cpu++;
402  }
403 
405  return 0;
406 }
407 
408 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
409 static long get_huge_page_size(int fd)
410 {
411  struct statfs s;
412  fstatfs(fd, &s);
413  return s.f_bsize;
414 }
415 #endif
416 
417 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
418 static clib_error_t *
419 dpdk_vhost_user_set_protocol_features(u32 hw_if_index, u64 prot_features)
420 {
421  dpdk_device_t * xd;
422  xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index);
423  assert(xd);
424  xd->vu_vhost_dev.protocol_features = prot_features;
425  return 0;
426 }
427 #endif
428 
429 static clib_error_t *
430 dpdk_vhost_user_get_features(u32 hw_if_index, u64 * features)
431 {
432  *features = rte_vhost_feature_get();
433 
434 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
435 #define OFFLOAD_FEATURES ((1ULL << VIRTIO_NET_F_HOST_TSO4) | \
436  (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
437  (1ULL << VIRTIO_NET_F_CSUM) | \
438  (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
439  (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
440  (1ULL << VIRTIO_NET_F_GUEST_TSO6))
441 
442  /* These are not suppoted as bridging/tunneling VHOST
443  * interfaces with hardware interfaces/drivers that does
444  * not support offloading breaks L4 traffic.
445  */
446  *features &= (~OFFLOAD_FEATURES);
447 #endif
448 
449  DBG_SOCK("supported features: 0x%lx", *features);
450  return 0;
451 }
452 
453 static clib_error_t *
454 dpdk_vhost_user_set_features(u32 hw_if_index, u64 features)
455 {
456  dpdk_device_t * xd;
457  u16 hdr_len = sizeof(struct virtio_net_hdr);
458 
459 
460  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
461  clib_warning("not a vhost-user interface");
462  return 0;
463  }
464 
465  xd->vu_vhost_dev.features = features;
466 
467  if (xd->vu_vhost_dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))
468  hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
469 
470  int numqs = VIRTIO_QNUM;
471  u8 idx;
472 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
473  int prot_feature = features &
475  numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
476 #endif
477  for (idx = 0; idx < numqs; idx++) {
478  xd->vu_vhost_dev.virtqueue[idx]->vhost_hlen = hdr_len;
479 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
480  /*
481  * Spec says, if F_PROTOCOL_FEATURE is not set by the
482  * slave, then all the vrings should start off as
483  * enabled. If slave negotiates F_PROTOCOL_FEATURE, then
484  * slave is responsible to enable it.
485  */
486  if (! prot_feature)
487  dpdk_vhost_user_set_vring_enable(hw_if_index, idx, 1);
488 #endif
489  }
490 
491  return 0;
492 }
493 
494 static clib_error_t *
496 {
497  struct virtio_memory * mem;
498  int i;
499  dpdk_device_t * xd;
500  dpdk_vu_intf_t * vui;
501 
502  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
503  clib_warning("not a vhost-user interface");
504  return 0;
505  }
506 
507  vui = xd->vu_intf;
508  mem = xd->vu_vhost_dev.mem;
509 
510  mem->nregions = vum->nregions;
511 
512  for (i=0; i < mem->nregions; i++) {
513  u64 mapped_size, mapped_address;
514 
515  mem->regions[i].guest_phys_address = vum->regions[i].guest_phys_addr;
516  mem->regions[i].guest_phys_address_end = vum->regions[i].guest_phys_addr +
517  vum->regions[i].memory_size;
518  mem->regions[i].memory_size = vum->regions[i].memory_size;
519  mem->regions[i].userspace_address = vum->regions[i].userspace_addr;
520 
521  mapped_size = mem->regions[i].memory_size + vum->regions[i].mmap_offset;
522  mapped_address = pointer_to_uword(mmap(NULL, mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0));
523 
524  if (uword_to_pointer(mapped_address, void*) == MAP_FAILED)
525  {
526  clib_warning("mmap error");
527  return 0;
528  }
529 
530  mapped_address += vum->regions[i].mmap_offset;
531  vui->region_addr[i] = mapped_address;
532  vui->region_fd[i] = fd[i];
533  mem->regions[i].address_offset = mapped_address - mem->regions[i].guest_phys_address;
534 
535  if (vum->regions[i].guest_phys_addr == 0) {
536  mem->base_address = vum->regions[i].userspace_addr;
537  mem->mapped_address = mem->regions[i].address_offset;
538  }
539  }
540 
541  disable_interface(xd);
542  return 0;
543 }
544 
545 static clib_error_t *
546 dpdk_vhost_user_set_vring_num(u32 hw_if_index, u8 idx, u32 num)
547 {
548  dpdk_device_t * xd;
549  struct vhost_virtqueue *vq;
550 
551  DBG_SOCK("idx %u num %u", idx, num);
552 
553  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
554  clib_warning("not a vhost-user interface");
555  return 0;
556  }
557  vq = xd->vu_vhost_dev.virtqueue[idx];
558  vq->size = num;
559 
560  stop_processing_packets(hw_if_index, idx);
561 
562  return 0;
563 }
564 
565 static clib_error_t *
566 dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, u64 desc, \
567  u64 used, u64 avail, u64 log)
568 {
569  dpdk_device_t * xd;
570  struct vhost_virtqueue *vq;
571 
572  DBG_SOCK("idx %u desc 0x%lx used 0x%lx avail 0x%lx log 0x%lx",
573  idx, desc, used, avail, log);
574 
575  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
576  clib_warning("not a vhost-user interface");
577  return 0;
578  }
579  vq = xd->vu_vhost_dev.virtqueue[idx];
580 
581  vq->desc = (struct vring_desc *) qva_to_vva(&xd->vu_vhost_dev, desc);
582  vq->used = (struct vring_used *) qva_to_vva(&xd->vu_vhost_dev, used);
583  vq->avail = (struct vring_avail *) qva_to_vva(&xd->vu_vhost_dev, avail);
584 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
585  vq->log_guest_addr = log;
586 #endif
587 
588  if (!(vq->desc && vq->used && vq->avail)) {
589  clib_warning("falied to set vring addr");
590  }
591 
592  if (vq->last_used_idx != vq->used->idx) {
593  clib_warning("last_used_idx (%u) and vq->used->idx (%u) mismatches; "
594  "some packets maybe resent for Tx and dropped for Rx",
595  vq->last_used_idx, vq->used->idx);
596  vq->last_used_idx = vq->used->idx;
597  vq->last_used_idx_res = vq->used->idx;
598  }
599 
600  /*
601  * Inform the guest that there is no need to inform (kick) the
602  * host when it adds buffers. kick results in vmexit and will
603  * incur performance degradation.
604  *
605  * The below function sets a flag in used table. Therefore,
606  * should be initialized after initializing vq->used.
607  */
608  rte_vhost_enable_guest_notification(&xd->vu_vhost_dev, idx, 0);
609  stop_processing_packets(hw_if_index, idx);
610 
611  return 0;
612 }
613 
614 static clib_error_t *
615 dpdk_vhost_user_get_vring_base(u32 hw_if_index, u8 idx, u32 * num)
616 {
617  dpdk_device_t * xd;
618  struct vhost_virtqueue *vq;
619 
620  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
621  clib_warning("not a vhost-user interface");
622  return 0;
623  }
624 
625  vq = xd->vu_vhost_dev.virtqueue[idx];
626  *num = vq->last_used_idx;
627 
628 /*
629  * From spec:
630  * Client must start ring upon receiving a kick
631  * (that is, detecting that file descriptor is readable)
632  * on the descriptor specified by VHOST_USER_SET_VRING_KICK,
633  * and stop ring upon receiving VHOST_USER_GET_VRING_BASE.
634  */
635  DBG_SOCK("Stopping vring Q %u of device %d", idx, hw_if_index);
636 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
637  dpdk_vu_intf_t *vui = xd->vu_intf;
638  vui->vrings[idx].enabled = 0; /* Reset local copy */
639  vui->vrings[idx].callfd = -1; /* Reset FD */
640  vq->enabled = 0;
641  vq->desc = NULL;
642  vq->used = NULL;
643  vq->avail = NULL;
644 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
645  vq->log_guest_addr = 0;
646 #endif
647 
648  /* Check if all Qs are disabled */
649  int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
650  for (idx = 0; idx < numqs; idx++) {
651  if (xd->vu_vhost_dev.virtqueue[idx]->enabled)
652  break;
653  }
654 
655  /* If all vrings are disabed then disable device */
656  if (idx == numqs) {
657  DBG_SOCK("Device %d disabled", hw_if_index);
658  xd->vu_is_running = 0;
659  }
660 #else
661  vq->desc = NULL;
662  vq->used = NULL;
663  vq->avail = NULL;
664  xd->vu_is_running = 0;
665 #endif
666 
667  return 0;
668 }
669 
670 static clib_error_t *
671 dpdk_vhost_user_set_vring_base(u32 hw_if_index, u8 idx, u32 num)
672 {
673  dpdk_device_t * xd;
674  struct vhost_virtqueue *vq;
675 
676  DBG_SOCK("idx %u num %u", idx, num);
677 
678  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
679  clib_warning("not a vhost-user interface");
680  return 0;
681  }
682 
683  vq = xd->vu_vhost_dev.virtqueue[idx];
684  vq->last_used_idx = num;
685  vq->last_used_idx_res = num;
686 
687  stop_processing_packets(hw_if_index, idx);
688 
689  return 0;
690 }
691 
692 static clib_error_t *
693 dpdk_vhost_user_set_vring_kick(u32 hw_if_index, u8 idx, int fd)
694 {
695  dpdk_main_t * dm = &dpdk_main;
696  dpdk_device_t * xd;
697 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
698  dpdk_vu_vring *vring;
699 #endif
700  struct vhost_virtqueue *vq0, *vq1, *vq;
701  int index, vu_is_running = 0;
702 
703  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
704  clib_warning("not a vhost-user interface");
705  return 0;
706  }
707 
708  vq = xd->vu_vhost_dev.virtqueue[idx];
709  vq->kickfd = fd;
710 
711 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
712  vring = &xd->vu_intf->vrings[idx];
713  vq->enabled = (vq->desc && vq->avail && vq->used && vring->enabled) ? 1 : 0;
714 #endif
715 
716  /*
717  * Set xd->vu_is_running if at least one pair of
718  * RX/TX queues are enabled.
719  */
720  int numqs = VIRTIO_QNUM;
721 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
722  numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
723 #endif
724 
725  for (index = 0; index < numqs; index += 2) {
726  vq0 = xd->vu_vhost_dev.virtqueue[index]; /* RX */
727  vq1 = xd->vu_vhost_dev.virtqueue[index + 1]; /* TX */
728 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
729  if (vq0->enabled && vq1->enabled)
730 #else
731  if (vq0->desc && vq0->avail && vq0->used &&
732  vq1->desc && vq1->avail && vq1->used)
733 #endif
734  {
735  vu_is_running = 1;
736  break;
737  }
738  }
739  DBG_SOCK("SET_VRING_KICK - idx %d, running %d, fd: %d",
740  idx, vu_is_running, fd);
741 
742  xd->vu_is_running = vu_is_running;
743  if (xd->vu_is_running && xd->admin_up) {
746  ETH_LINK_FULL_DUPLEX );
747  }
748 
749  return 0;
750 }
751 
752 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
753 static int
754 dpdk_vhost_user_set_vring_enable(u32 hw_if_index, u8 idx, int enable)
755 {
756  dpdk_device_t * xd;
757  struct vhost_virtqueue *vq;
758  dpdk_vu_intf_t *vui;
759 
760  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
761  clib_warning("not a vhost-user interface");
762  return 0;
763  }
764 
765  vui = xd->vu_intf;
766  /*
767  * Guest vhost driver wrongly enables queue before
768  * setting the vring address. Therefore, save a
769  * local copy. Reflect it in vq structure if addresses
770  * are set. If not, vq will be enabled when vring
771  * is kicked.
772  */
773  vui->vrings[idx].enabled = enable; /* Save local copy */
774 
775  int numqs = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
776  while (numqs--) {
777  if (! vui->vrings[numqs].enabled)
778  break;
779  }
780 
781  if (numqs == -1) /* All Qs are enabled */
782  xd->need_txlock = 0;
783  else
784  xd->need_txlock = 1;
785 
786  vq = xd->vu_vhost_dev.virtqueue[idx];
787  if (vq->desc && vq->avail && vq->used)
788  xd->vu_vhost_dev.virtqueue[idx]->enabled = enable;
789 
790  return 0;
791 }
792 #endif
793 
795 {
796  __attribute__((unused)) int n;
797  u8 buff[8];
798  n = read(uf->file_descriptor, ((char*)&buff), 8);
799  return 0;
800 }
801 
802 static clib_error_t *
803 dpdk_vhost_user_set_vring_call(u32 hw_if_index, u8 idx, int fd)
804 {
805  dpdk_device_t * xd;
806  struct vhost_virtqueue *vq;
807  unix_file_t template = {0};
808 
809  DBG_SOCK("SET_VRING_CALL - idx %d, fd %d", idx, fd);
810 
811  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_index))) {
812  clib_warning("not a vhost-user interface");
813  return 0;
814  }
815 
816  dpdk_vu_intf_t *vui = xd->vu_intf;
817 
818  /* if there is old fd, delete it */
819  if (vui->vrings[idx].callfd > 0) {
821  vui->vrings[idx].callfd_idx);
822  unix_file_del (&unix_main, uf);
823  }
824  vui->vrings[idx].callfd = fd;
825  template.read_function = dpdk_vhost_user_callfd_read_ready;
826  template.file_descriptor = fd;
827  vui->vrings[idx].callfd_idx = unix_file_add (&unix_main, &template);
828 
829  vq = xd->vu_vhost_dev.virtqueue[idx];
830  vq->callfd = -1; /* We use locally saved vring->callfd; */
831 
832  return 0;
833 }
834 
835 u8
837 {
838  dpdk_vu_intf_t *vui = xd->vu_intf;
839  ASSERT(vui != NULL);
840 
841  if (PREDICT_FALSE(vui->num_vrings <= 0))
842  return 0;
843 
844  dpdk_vu_vring *vring = &(vui->vrings[idx]);
845  struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
846 
847  /* return if vm is interested in interrupts */
848  return (vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
849 }
850 
851 void
853 {
854  dpdk_main_t * dm = &dpdk_main;
855  dpdk_vu_intf_t *vui = xd->vu_intf;
856  ASSERT(vui != NULL);
857 
858  if (PREDICT_FALSE(vui->num_vrings <= 0))
859  return;
860 
861  dpdk_vu_vring *vring = &(vui->vrings[idx]);
862  struct vhost_virtqueue *vq = xd->vu_vhost_dev.virtqueue[idx];
863 
864  /* if vm is interested in interrupts */
865  if((vring->callfd > 0) && !(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
866  eventfd_write(vring->callfd, (eventfd_t)1);
867  vring->n_since_last_int = 0;
869  }
870 }
871 
872 /*
873  * vhost-user interface management functions
874  */
875 
876 // initialize vui with specified attributes
877 static void
879  dpdk_device_t *xd, int sockfd,
880  const char * sock_filename,
881  u8 is_server, u64 feature_mask,
882  u32 * sw_if_index)
883 {
884  dpdk_vu_intf_t *vui = xd->vu_intf;
885  memset(vui, 0, sizeof(*vui));
886 
887  vui->unix_fd = sockfd;
888 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
889  vui->num_vrings = xd->vu_vhost_dev.virt_qp_nb * VIRTIO_QNUM;
890 #else
891  vui->num_vrings = VIRTIO_QNUM;
892 #endif
893  DBG_SOCK("dpdk_vhost_user_vui_init VRINGS: %d", vui->num_vrings);
894  vui->sock_is_server = is_server;
895  strncpy(vui->sock_filename, sock_filename, ARRAY_LEN(vui->sock_filename)-1);
896  vui->sock_errno = 0;
897  vui->is_up = 0;
898  vui->feature_mask = feature_mask;
899  vui->active = 1;
900  vui->unix_file_index = ~0;
901 
903 
904  if (sw_if_index)
905  *sw_if_index = xd->vlib_sw_if_index;
906 }
907 
908 // register vui and start polling on it
909 static void
911 {
912  dpdk_main_t * dm = &dpdk_main;
913  dpdk_vu_intf_t *vui = xd->vu_intf;
914 
916  xd->vlib_sw_if_index);
917 }
918 
919 static inline void
921 {
922  dpdk_vu_intf_t *vui = xd->vu_intf;
923  vnet_main_t * vnm = vnet_get_main();
924  dpdk_main_t * dm = &dpdk_main;
925 
926  xd->admin_up = 0;
928 
929  if (vui->unix_file_index != ~0) {
931  vui->unix_file_index = ~0;
932  }
933 
936  close(vui->unix_fd);
937  vui->unix_fd = -1;
938  vui->is_up = 0;
939 
940  DBG_SOCK("interface ifindex %d disconnected", xd->vlib_sw_if_index);
941 }
942 
944 {
945  int n;
946  int fd, number_of_fds = 0;
947  int fds[VHOST_MEMORY_MAX_NREGIONS];
948  vhost_user_msg_t msg;
949  struct msghdr mh;
950  struct iovec iov[1];
951  dpdk_main_t * dm = &dpdk_main;
952  dpdk_device_t *xd;
953  dpdk_vu_intf_t *vui;
954  struct cmsghdr *cmsg;
955  uword * p;
956  u8 q;
957  vnet_main_t * vnm = vnet_get_main();
958 
960  if (p == 0) {
961  DBG_SOCK ("FD %d doesn't belong to any interface",
962  uf->file_descriptor);
963  return 0;
964  }
965  else
967 
968  ASSERT(xd != NULL);
969  vui = xd->vu_intf;
970 
971  char control[CMSG_SPACE(VHOST_MEMORY_MAX_NREGIONS * sizeof(int))];
972 
973  memset(&mh, 0, sizeof(mh));
974  memset(control, 0, sizeof(control));
975 
976  /* set the payload */
977  iov[0].iov_base = (void *) &msg;
978  iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
979 
980  mh.msg_iov = iov;
981  mh.msg_iovlen = 1;
982  mh.msg_control = control;
983  mh.msg_controllen = sizeof(control);
984 
985  n = recvmsg(uf->file_descriptor, &mh, 0);
986 
987  if (n != VHOST_USER_MSG_HDR_SZ)
988  goto close_socket;
989 
990  if (mh.msg_flags & MSG_CTRUNC) {
991  goto close_socket;
992  }
993 
994  cmsg = CMSG_FIRSTHDR(&mh);
995 
996  if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
997  (cmsg->cmsg_type == SCM_RIGHTS) &&
998  (cmsg->cmsg_len - CMSG_LEN(0) <= VHOST_MEMORY_MAX_NREGIONS * sizeof(int))) {
999  number_of_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
1000  clib_memcpy(fds, CMSG_DATA(cmsg), number_of_fds * sizeof(int));
1001  }
1002 
1003  /* version 1, no reply bit set*/
1004  if ((msg.flags & 7) != 1) {
1005  DBG_SOCK("malformed message received. closing socket");
1006  goto close_socket;
1007  }
1008 
1009  {
1010  int rv __attribute__((unused));
1011  /* $$$$ pay attention to rv */
1012  rv = read(uf->file_descriptor, ((char*)&msg) + n, msg.size);
1013  }
1014 
1015  DBG_SOCK("VPP VHOST message %s", vhost_message_str[msg.request]);
1016  switch (msg.request) {
1018  DBG_SOCK("if %d msg VHOST_USER_GET_FEATURES",
1019  xd->vlib_hw_if_index);
1020 
1021  msg.flags |= VHOST_USER_REPLY_MASK;
1022 
1024  msg.u64 &= vui->feature_mask;
1025  msg.size = sizeof(msg.u64);
1026  break;
1027 
1029  DBG_SOCK("if %d msg VHOST_USER_SET_FEATURES features 0x%016lx",
1030  xd->vlib_hw_if_index, msg.u64);
1031 
1033  break;
1034 
1036  DBG_SOCK("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
1037  xd->vlib_hw_if_index, msg.memory.nregions);
1038 
1039  if ((msg.memory.nregions < 1) ||
1040  (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) {
1041 
1042  DBG_SOCK("number of mem regions must be between 1 and %i",
1044 
1045  goto close_socket;
1046  }
1047 
1048  if (msg.memory.nregions != number_of_fds) {
1049  DBG_SOCK("each memory region must have FD");
1050  goto close_socket;
1051  }
1052 
1053  dpdk_vhost_user_set_mem_table(xd->vlib_hw_if_index, &msg.memory, fds);
1054  break;
1055 
1057  DBG_SOCK("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
1058  xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1059 
1060  if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
1061  (msg.state.num == 0) || /* it cannot be zero */
1062  (msg.state.num % 2)) /* must be power of 2 */
1063  goto close_socket;
1064 
1065  dpdk_vhost_user_set_vring_num(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1066  break;
1067 
1069  DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
1070  xd->vlib_hw_if_index, msg.state.index);
1071 
1072  dpdk_vhost_user_set_vring_addr(xd->vlib_hw_if_index, msg.state.index,
1073  msg.addr.desc_user_addr,
1074  msg.addr.used_user_addr,
1075  msg.addr.avail_user_addr,
1076  msg.addr.log_guest_addr);
1077  break;
1078 
1079  case VHOST_USER_SET_OWNER:
1080  DBG_SOCK("if %d msg VHOST_USER_SET_OWNER",
1081  xd->vlib_hw_if_index);
1082  break;
1083 
1085  DBG_SOCK("if %d msg VHOST_USER_RESET_OWNER",
1086  xd->vlib_hw_if_index);
1087  break;
1088 
1090  q = (u8) (msg.u64 & 0xFF);
1091 
1092  DBG_SOCK("if %d msg VHOST_USER_SET_VRING_CALL u64 %lx, idx: %d",
1093  xd->vlib_hw_if_index, msg.u64, q);
1094 
1095  if (!(msg.u64 & 0x100))
1096  {
1097  if (number_of_fds != 1)
1098  goto close_socket;
1099  fd = fds[0];
1100  } else {
1101  fd = -1;
1102  }
1104 
1105  break;
1106 
1108 
1109  q = (u8) (msg.u64 & 0xFF);
1110 
1111  DBG_SOCK("if %d msg VHOST_USER_SET_VRING_KICK u64 %lx, idx: %d",
1112  xd->vlib_hw_if_index, msg.u64, q);
1113 
1114  if (!(msg.u64 & 0x100))
1115  {
1116  if (number_of_fds != 1)
1117  goto close_socket;
1118 
1119  vui->vrings[q].kickfd = fds[0];
1120  }
1121  else
1122  vui->vrings[q].kickfd = -1;
1123 
1125  break;
1126 
1128 
1129  q = (u8) (msg.u64 & 0xFF);
1130 
1131  DBG_SOCK("if %d msg VHOST_USER_SET_VRING_ERR u64 %lx, idx: %d",
1132  xd->vlib_hw_if_index, msg.u64, q);
1133 
1134  if (!(msg.u64 & 0x100))
1135  {
1136  if (number_of_fds != 1)
1137  goto close_socket;
1138 
1139  fd = fds[0];
1140  }
1141  else
1142  fd = -1;
1143 
1144  vui->vrings[q].errfd = fd;
1145  break;
1146 
1148  DBG_SOCK("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1149  xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1150 
1151  dpdk_vhost_user_set_vring_base(xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1152  break;
1153 
1155  DBG_SOCK("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1156  xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1157 
1158  msg.flags |= VHOST_USER_REPLY_MASK;
1159  msg.size = sizeof(msg.state);
1160 
1161  dpdk_vhost_user_get_vring_base(xd->vlib_hw_if_index, msg.state.index, &msg.state.num);
1162  break;
1163 
1164  case VHOST_USER_NONE:
1165  DBG_SOCK("if %d msg VHOST_USER_NONE",
1166  xd->vlib_hw_if_index);
1167  break;
1168 
1170 #if RTE_VERSION >= RTE_VERSION_NUM(16, 4, 0, 0)
1171  DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE",
1172  xd->vlib_hw_if_index);
1173 
1174  if (msg.size != sizeof(msg.log)) {
1175  DBG_SOCK("invalid msg size for VHOST_USER_SET_LOG_BASE: %u instead of %lu",
1176  msg.size, sizeof(msg.log));
1177  goto close_socket;
1178  }
1179 
1180  if (!(xd->vu_vhost_dev.protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD))) {
1181  DBG_SOCK("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1182  goto close_socket;
1183  }
1184 
1185  fd = fds[0];
1186  /* align size to 2M page */
1187  long page_sz = get_huge_page_size(fd);
1188  ssize_t map_sz = (msg.log.size + msg.log.offset + page_sz) & ~(page_sz - 1);
1189 
1190  void *addr = mmap(0, map_sz, PROT_READ | PROT_WRITE,
1191  MAP_SHARED, fd, 0);
1192 
1193  DBG_SOCK("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped %p",
1194  map_sz, msg.log.offset, fd, addr);
1195 
1196  if (addr == MAP_FAILED) {
1197  clib_warning("failed to map memory. errno is %d", errno);
1198  goto close_socket;
1199  }
1200 
1201  xd->vu_vhost_dev.log_base += (u64)addr + msg.log.offset;
1202  xd->vu_vhost_dev.log_size = msg.log.size;
1203  msg.flags |= VHOST_USER_REPLY_MASK;
1204  msg.size = sizeof(msg.u64);
1205 #else
1206  DBG_SOCK("if %d msg VHOST_USER_SET_LOG_BASE Not-Implemented",
1207  xd->vlib_hw_if_index);
1208 #endif
1209  break;
1210 
1211  case VHOST_USER_SET_LOG_FD:
1212  DBG_SOCK("if %d msg VHOST_USER_SET_LOG_FD",
1213  xd->vlib_hw_if_index);
1214  break;
1215 
1216 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
1218  DBG_SOCK("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1219  xd->vlib_hw_if_index);
1220 
1221  msg.flags |= VHOST_USER_REPLY_MASK;
1222  msg.u64 = VHOST_USER_PROTOCOL_FEATURES;
1223  DBG_SOCK("VHOST_USER_PROTOCOL_FEATURES: %llx", VHOST_USER_PROTOCOL_FEATURES);
1224  msg.size = sizeof(msg.u64);
1225  break;
1226 
1228  DBG_SOCK("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES",
1229  xd->vlib_hw_if_index);
1230 
1231  DBG_SOCK("VHOST_USER_SET_PROTOCOL_FEATURES: 0x%lx",
1232  msg.u64);
1234  msg.u64);
1235  break;
1236 
1238  DBG_SOCK("%d VPP VHOST_USER_SET_VRING_ENABLE IDX: %d, Enable: %d",
1239  xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1241  (xd->vlib_hw_if_index, msg.state.index, msg.state.num);
1242  break;
1243 
1245  DBG_SOCK("if %d msg VHOST_USER_GET_QUEUE_NUM:",
1246  xd->vlib_hw_if_index);
1247 
1248  msg.flags |= VHOST_USER_REPLY_MASK;
1249  msg.u64 = xd->vu_vhost_dev.virt_qp_nb;
1250  msg.size = sizeof(msg.u64);
1251  break;
1252 #endif
1253 
1254  default:
1255  DBG_SOCK("unknown vhost-user message %d received. closing socket",
1256  msg.request);
1257  goto close_socket;
1258  }
1259 
1260  /* if we have pointers to descriptor table, go up*/
1261  if (!vui->is_up &&
1262  xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_TX]->desc &&
1263  xd->vu_vhost_dev.virtqueue[VHOST_NET_VRING_IDX_RX]->desc) {
1264 
1265  DBG_SOCK("interface %d connected", xd->vlib_sw_if_index);
1266 
1268  vui->is_up = 1;
1269  xd->admin_up = 1;
1270  }
1271 
1272  /* if we need to reply */
1273  if (msg.flags & VHOST_USER_REPLY_MASK)
1274  {
1275  n = send(uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
1276  if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
1277  goto close_socket;
1278  }
1279 
1280  return 0;
1281 
1282 close_socket:
1283  DBG_SOCK("error: close_socket");
1285  return 0;
1286 }
1287 
1289 {
1290  dpdk_main_t * dm = &dpdk_main;
1291  dpdk_device_t *xd;
1292  uword * p;
1293 
1295  if (p == 0) {
1296  DBG_SOCK ("FD %d doesn't belong to any interface",
1297  uf->file_descriptor);
1298  return 0;
1299  }
1300  else
1302 
1304  return 0;
1305 }
1306 
1308 {
1309  int client_fd, client_len;
1310  struct sockaddr_un client;
1311  unix_file_t template = {0};
1312  dpdk_main_t * dm = &dpdk_main;
1313  dpdk_device_t * xd = NULL;
1314  dpdk_vu_intf_t * vui;
1315  uword * p;
1316 
1318  uf->file_descriptor);
1319  if (p == 0) {
1320  DBG_SOCK ("fd %d doesn't belong to any interface",
1321  uf->file_descriptor);
1322  return 0;
1323  }
1324 
1326  ASSERT(xd != NULL);
1327  vui = xd->vu_intf;
1328 
1329  client_len = sizeof(client);
1330  client_fd = accept (uf->file_descriptor,
1331  (struct sockaddr *)&client,
1332  (socklen_t *)&client_len);
1333 
1334  if (client_fd < 0)
1335  return clib_error_return_unix (0, "accept");
1336 
1337  template.read_function = dpdk_vhost_user_socket_read;
1338  template.error_function = dpdk_vhost_user_socket_error;
1339  template.file_descriptor = client_fd;
1340  vui->unix_file_index = unix_file_add (&unix_main, &template);
1341 
1342  vui->client_fd = client_fd;
1344  xd->vlib_sw_if_index);
1345 
1346  return 0;
1347 }
1348 
1349 // init server socket on specified sock_filename
1350 static int dpdk_vhost_user_init_server_sock(const char * sock_filename, int *sockfd)
1351 {
1352  int rv = 0, len;
1353  struct sockaddr_un un;
1354  int fd;
1355  /* create listening socket */
1356  fd = socket(AF_UNIX, SOCK_STREAM, 0);
1357 
1358  if (fd < 0) {
1359  return VNET_API_ERROR_SYSCALL_ERROR_1;
1360  }
1361 
1362  un.sun_family = AF_UNIX;
1363  strcpy((char *) un.sun_path, (char *) sock_filename);
1364 
1365  /* remove if exists */
1366  unlink( (char *) sock_filename);
1367 
1368  len = strlen((char *) un.sun_path) + strlen((char *) sock_filename);
1369 
1370  if (bind(fd, (struct sockaddr *) &un, len) == -1) {
1371  rv = VNET_API_ERROR_SYSCALL_ERROR_2;
1372  goto error;
1373  }
1374 
1375  if (listen(fd, 1) == -1) {
1376  rv = VNET_API_ERROR_SYSCALL_ERROR_3;
1377  goto error;
1378  }
1379 
1380  unix_file_t template = {0};
1382  template.file_descriptor = fd;
1383  unix_file_add (&unix_main, &template);
1384  *sockfd = fd;
1385  return rv;
1386 
1387 error:
1388  close(fd);
1389  return rv;
1390 }
1391 
1392 /*
1393  * vhost-user interface control functions used from vpe api
1394  */
1395 
1397  const char * sock_filename,
1398  u8 is_server,
1399  u32 * sw_if_index,
1400  u64 feature_mask,
1401  u8 renumber, u32 custom_dev_instance,
1402  u8 *hwaddr)
1403 {
1404  dpdk_main_t * dm = &dpdk_main;
1405  dpdk_device_t *xd;
1406  u32 hw_if_idx = ~0;
1407  int sockfd = -1;
1408  int rv = 0;
1409 
1410  // using virtio vhost user?
1411  if (dm->use_virtio_vhost) {
1412  return vhost_user_create_if(vnm, vm, sock_filename, is_server,
1413  sw_if_index, feature_mask, renumber, custom_dev_instance, hwaddr);
1414  }
1415 
1416  if (is_server) {
1417  if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1418  return rv;
1419  }
1420  }
1421 
1422  if (renumber) {
1423  // set next vhost-user if id if custom one is higher or equal
1424  if (custom_dev_instance >= dm->next_vu_if_id)
1425  dm->next_vu_if_id = custom_dev_instance + 1;
1426 
1427  dpdk_create_vhost_user_if_internal(&hw_if_idx, custom_dev_instance, hwaddr);
1428  } else
1429  dpdk_create_vhost_user_if_internal(&hw_if_idx, (u32)~0, hwaddr);
1430  DBG_SOCK("dpdk vhost-user interface created hw_if_index %d", hw_if_idx);
1431 
1433  ASSERT(xd != NULL);
1434 
1435  dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1436  feature_mask, sw_if_index);
1437 
1439  return rv;
1440 }
1441 
1443  const char * sock_filename,
1444  u8 is_server,
1445  u32 sw_if_index,
1446  u64 feature_mask,
1447  u8 renumber, u32 custom_dev_instance)
1448 {
1449  dpdk_main_t * dm = &dpdk_main;
1450  dpdk_device_t * xd;
1451  dpdk_vu_intf_t * vui = NULL;
1452  u32 sw_if_idx = ~0;
1453  int sockfd = -1;
1454  int rv = 0;
1455 
1456  // using virtio vhost user?
1457  if (dm->use_virtio_vhost) {
1458  return vhost_user_modify_if(vnm, vm, sock_filename, is_server,
1459  sw_if_index, feature_mask, renumber, custom_dev_instance);
1460  }
1461 
1462  xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1463 
1464  if (xd == NULL)
1465  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1466 
1467  vui = xd->vu_intf;
1468 
1469  // interface is inactive
1470  vui->active = 0;
1471  // disconnect interface sockets
1473 
1474  if (is_server) {
1475  if ((rv = dpdk_vhost_user_init_server_sock (sock_filename, &sockfd)) != 0) {
1476  return rv;
1477  }
1478  }
1479 
1480  dpdk_vhost_user_vui_init (vnm, xd, sockfd, sock_filename, is_server,
1481  feature_mask, &sw_if_idx);
1482 
1483  if (renumber) {
1484  vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
1485  }
1486 
1488 
1489  return rv;
1490 }
1491 
1493  u32 sw_if_index)
1494 {
1495  dpdk_main_t * dm = &dpdk_main;
1496  dpdk_device_t * xd = NULL;
1497  dpdk_vu_intf_t * vui;
1498  int rv = 0;
1499 
1500  // using virtio vhost user?
1501  if (dm->use_virtio_vhost) {
1502  return vhost_user_delete_if(vnm, vm, sw_if_index);
1503  }
1504 
1505  xd = dpdk_vhost_user_device_from_sw_if_index(sw_if_index);
1506 
1507  if (xd == NULL)
1508  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1509 
1510  vui = xd->vu_intf;
1511 
1512  // interface is inactive
1513  vui->active = 0;
1514  // disconnect interface sockets
1516  // add to inactive interface list
1518 
1520  DBG_SOCK ("deleted (deactivated) vhost-user interface sw_if_index %d", sw_if_index);
1521 
1522  return rv;
1523 }
1524 
1526 {
1527  int rv = 0;
1528  dpdk_main_t * dm = &dpdk_main;
1529  dpdk_device_t * xd;
1530  dpdk_vu_intf_t * vui;
1531  struct virtio_net * vhost_dev;
1532  vhost_user_intf_details_t * r_vuids = NULL;
1534  u32 * hw_if_indices = 0;
1536  u8 *s = NULL;
1537  int i;
1538 
1539  if (!out_vuids)
1540  return -1;
1541 
1542  // using virtio vhost user?
1543  if (dm->use_virtio_vhost) {
1544  return vhost_user_dump_ifs(vnm, vm, out_vuids);
1545  }
1546 
1547  vec_foreach (xd, dm->devices) {
1548  if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER &&
1549  xd->vu_intf->active)
1550  vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1551  }
1552 
1553  for (i = 0; i < vec_len (hw_if_indices); i++) {
1554  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1555  xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]);
1556  if (!xd) {
1557  clib_warning("invalid vhost-user interface hw_if_index %d", hw_if_indices[i]);
1558  continue;
1559  }
1560 
1561  vui = xd->vu_intf;
1562  ASSERT(vui != NULL);
1563  vhost_dev = &xd->vu_vhost_dev;
1564  u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1565  vhost_dev->virtqueue[0]->vhost_hlen : 0);
1566 
1567  vec_add2(r_vuids, vuid, 1);
1568  vuid->sw_if_index = xd->vlib_sw_if_index;
1569  vuid->virtio_net_hdr_sz = virtio_net_hdr_sz;
1570  vuid->features = vhost_dev->features;
1571  vuid->is_server = vui->sock_is_server;
1572  vuid->num_regions = (vhost_dev->mem != NULL ? vhost_dev->mem->nregions : 0);
1573  vuid->sock_errno = vui->sock_errno;
1574  strncpy((char *)vuid->sock_filename, (char *)vui->sock_filename,
1575  ARRAY_LEN(vuid->sock_filename)-1);
1576 
1577  s = format (s, "%v%c", hi->name, 0);
1578 
1579  strncpy((char *)vuid->if_name, (char *)s,
1580  ARRAY_LEN(vuid->if_name)-1);
1581  _vec_len(s) = 0;
1582  }
1583 
1584  vec_free (s);
1585  vec_free (hw_if_indices);
1586 
1587  *out_vuids = r_vuids;
1588 
1589  return rv;
1590 }
1591 
1592 /*
1593  * Processing functions called from dpdk process fn
1594  */
1595 
1596 typedef struct {
1597  struct sockaddr_un sun;
1598  int sockfd;
1599  unix_file_t template;
1602 
1604 {
1606  memset(state, 0, sizeof(*state));
1607  state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1608  state->sun.sun_family = AF_UNIX;
1611  state->event_data = 0;
1612  *ctx = state;
1613 }
1614 
1616 {
1617  clib_mem_free(ctx);
1618 }
1619 
1621 {
1622  dpdk_main_t * dm = &dpdk_main;
1624  dpdk_vu_intf_t *vui = xd->vu_intf;
1625 
1626  if (vui->sock_is_server || !vui->active)
1627  return 0;
1628 
1629  if (vui->unix_fd == -1) {
1630  /* try to connect */
1631  strncpy(state->sun.sun_path, (char *) vui->sock_filename, sizeof(state->sun.sun_path) - 1);
1632 
1633  if (connect(state->sockfd, (struct sockaddr *) &(state->sun), sizeof(struct sockaddr_un)) == 0) {
1634  vui->sock_errno = 0;
1635  vui->unix_fd = state->sockfd;
1636  state->template.file_descriptor = state->sockfd;
1637  vui->unix_file_index = unix_file_add (&unix_main, &(state->template));
1639 
1640  state->sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1641  if (state->sockfd < 0)
1642  return -1;
1643  } else {
1644  vui->sock_errno = errno;
1645  }
1646  } else {
1647  /* check if socket is alive */
1648  int error = 0;
1649  socklen_t len = sizeof (error);
1650  int retval = getsockopt(vui->unix_fd, SOL_SOCKET, SO_ERROR, &error, &len);
1651 
1652  if (retval)
1654  }
1655  return 0;
1656 }
1657 
1658 /*
1659  * CLI functions
1660  */
1661 
1662 static clib_error_t *
1664  unformat_input_t * input,
1665  vlib_cli_command_t * cmd)
1666 {
1667  dpdk_main_t * dm = &dpdk_main;
1668  unformat_input_t _line_input, * line_input = &_line_input;
1669  u8 * sock_filename = NULL;
1670  u32 sw_if_index;
1671  u8 is_server = 0;
1672  u64 feature_mask = (u64)~0;
1673  u8 renumber = 0;
1674  u32 custom_dev_instance = ~0;
1675  u8 hwaddr[6];
1676  u8 *hw = NULL;
1677 
1678  if (dm->use_virtio_vhost) {
1679  return vhost_user_connect_command_fn(vm, input, cmd);
1680  }
1681 
1682  /* Get a line of input. */
1683  if (! unformat_user (input, unformat_line_input, line_input))
1684  return 0;
1685 
1686  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1687  if (unformat (line_input, "socket %s", &sock_filename))
1688  ;
1689  else if (unformat (line_input, "server"))
1690  is_server = 1;
1691  else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
1692  ;
1693  else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
1694  hw = hwaddr;
1695  else if (unformat (line_input, "renumber %d", &custom_dev_instance)) {
1696  renumber = 1;
1697  }
1698  else
1699  return clib_error_return (0, "unknown input `%U'",
1700  format_unformat_error, input);
1701  }
1702  unformat_free (line_input);
1703 
1704  vnet_main_t *vnm = vnet_get_main();
1705  if (sock_filename == NULL)
1706  return clib_error_return (0, "missing socket file");
1707 
1708  dpdk_vhost_user_create_if(vnm, vm, (char *)sock_filename,
1709  is_server, &sw_if_index, feature_mask,
1710  renumber, custom_dev_instance, hw);
1711 
1712  vec_free(sock_filename);
1713  return 0;
1714 }
1715 
1716 VLIB_CLI_COMMAND (dpdk_vhost_user_connect_command, static) = {
1717  .path = "create vhost-user",
1718  .short_help = "create vhost-user socket <socket-filename> [server] [feature-mask <hex>] [renumber <dev_instance>]",
1720 };
1721 
1722 static clib_error_t *
1724  unformat_input_t * input,
1725  vlib_cli_command_t * cmd)
1726 {
1727  dpdk_main_t * dm = &dpdk_main;
1728  clib_error_t * error = 0;
1729  unformat_input_t _line_input, * line_input = &_line_input;
1730  u32 sw_if_index = ~0;
1731 
1732  if (dm->use_virtio_vhost) {
1733  return vhost_user_delete_command_fn(vm, input, cmd);
1734  }
1735 
1736  /* Get a line of input. */
1737  if (! unformat_user (input, unformat_line_input, line_input))
1738  return 0;
1739 
1740  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
1741  if (unformat (line_input, "sw_if_index %d", &sw_if_index))
1742  ;
1743  else
1744  return clib_error_return (0, "unknown input `%U'",
1745  format_unformat_error, input);
1746  }
1747  unformat_free (line_input);
1748 
1749  if (sw_if_index == ~0) {
1750  error = clib_error_return (0, "invalid sw_if_index",
1751  format_unformat_error, input);
1752  return error;
1753  }
1754 
1755  vnet_main_t *vnm = vnet_get_main();
1756 
1757  dpdk_vhost_user_delete_if(vnm, vm, sw_if_index);
1758 
1759  return 0;
1760 }
1761 
1762 VLIB_CLI_COMMAND (dpdk_vhost_user_delete_command, static) = {
1763  .path = "delete vhost-user",
1764  .short_help = "delete vhost-user sw_if_index <nn>",
1766 };
1767 
1768 #define foreach_dpdk_vhost_feature \
1769  _ (VIRTIO_NET_F_MRG_RXBUF) \
1770  _ (VIRTIO_NET_F_CTRL_VQ) \
1771  _ (VIRTIO_NET_F_CTRL_RX)
1772 
1773 static clib_error_t *
1775  unformat_input_t * input,
1776  vlib_cli_command_t * cmd)
1777 {
1778  clib_error_t * error = 0;
1779  dpdk_main_t * dm = &dpdk_main;
1780  vnet_main_t * vnm = vnet_get_main();
1781  dpdk_device_t * xd;
1782  dpdk_vu_intf_t * vui;
1783  struct virtio_net * vhost_dev;
1784  u32 hw_if_index, * hw_if_indices = 0;
1786  int i, j, q;
1787  int show_descr = 0;
1788  struct virtio_memory * mem;
1789  struct feat_struct { u8 bit; char *str;};
1790  struct feat_struct *feat_entry;
1791 
1792  static struct feat_struct feat_array[] = {
1793 #define _(f) { .str = #f, .bit = f, },
1795 #undef _
1796  { .str = NULL }
1797  };
1798 
1799  if (dm->use_virtio_vhost) {
1800  return show_vhost_user_command_fn(vm, input, cmd);
1801  }
1802 
1803  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1804  if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) {
1805  vec_add1 (hw_if_indices, hw_if_index);
1806  vlib_cli_output(vm, "add %d", hw_if_index);
1807  }
1808  else if (unformat (input, "descriptors") || unformat (input, "desc") )
1809  show_descr = 1;
1810  else {
1811  error = clib_error_return (0, "unknown input `%U'",
1812  format_unformat_error, input);
1813  goto done;
1814  }
1815  }
1816  if (vec_len (hw_if_indices) == 0) {
1817  vec_foreach (xd, dm->devices) {
1818  if (xd->dev_type == VNET_DPDK_DEV_VHOST_USER && xd->vu_intf->active)
1819  vec_add1(hw_if_indices, xd->vlib_hw_if_index);
1820  }
1821  }
1822 
1823  vlib_cli_output (vm, "DPDK vhost-user interfaces");
1824  vlib_cli_output (vm, "Global:\n coalesce frames %d time %e\n\n",
1826 
1827  for (i = 0; i < vec_len (hw_if_indices); i++) {
1828  hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
1829 
1830  if (!(xd = dpdk_vhost_user_device_from_hw_if_index(hw_if_indices[i]))) {
1831  error = clib_error_return (0, "not dpdk vhost-user interface: '%s'",
1832  hi->name);
1833  goto done;
1834  }
1835  vui = xd->vu_intf;
1836  vhost_dev = &xd->vu_vhost_dev;
1837  mem = vhost_dev->mem;
1838  u32 virtio_net_hdr_sz = (vui->num_vrings > 0 ?
1839  vhost_dev->virtqueue[0]->vhost_hlen : 0);
1840 
1841  vlib_cli_output (vm, "Interface: %v (ifindex %d)",
1842  hi->name, hw_if_indices[i]);
1843 
1844  vlib_cli_output (vm, "virtio_net_hdr_sz %d\n features (0x%llx): \n",
1845  virtio_net_hdr_sz, xd->vu_vhost_dev.features);
1846 
1847  feat_entry = (struct feat_struct *) &feat_array;
1848  while(feat_entry->str) {
1849  if (xd->vu_vhost_dev.features & (1 << feat_entry->bit))
1850  vlib_cli_output (vm, " %s (%d)", feat_entry->str, feat_entry->bit);
1851  feat_entry++;
1852  }
1853 
1854  vlib_cli_output (vm, "\n");
1855 
1856  vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
1857  vui->sock_filename, vui->sock_is_server ? "server" : "client",
1858  strerror(vui->sock_errno));
1859 
1860  vlib_cli_output (vm, " Memory regions (total %d)\n", mem->nregions);
1861 
1862  if (mem->nregions){
1863  vlib_cli_output(vm, " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
1864  vlib_cli_output(vm, " ====== ===== ================== ================== ================== ================== ==================\n");
1865  }
1866  for (j = 0; j < mem->nregions; j++) {
1867  vlib_cli_output(vm, " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", j,
1868  vui->region_fd[j],
1869  mem->regions[j].guest_phys_address,
1870  mem->regions[j].memory_size,
1871  mem->regions[j].userspace_address,
1872  mem->regions[j].address_offset,
1873  vui->region_addr[j]);
1874  }
1875  for (q = 0; q < vui->num_vrings; q++) {
1876  struct vhost_virtqueue *vq = vhost_dev->virtqueue[q];
1877  const char *qtype = (q & 1) ? "TX" : "RX";
1878 
1879  vlib_cli_output(vm, "\n Virtqueue %d (%s)\n", q/2, qtype);
1880 
1881  vlib_cli_output(vm, " qsz %d last_used_idx %d last_used_idx_res %d\n",
1882  vq->size, vq->last_used_idx, vq->last_used_idx_res);
1883 
1884  if (vq->avail && vq->used)
1885  vlib_cli_output(vm, " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
1886  vq->avail->flags, vq->avail->idx, vq->used->flags, vq->used->idx);
1887 
1888 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
1889  vlib_cli_output(vm, " kickfd %d callfd %d errfd %d enabled %d\n",
1890  vq->kickfd, vq->callfd, vui->vrings[q].errfd, vq->enabled);
1891 
1892  if (show_descr && vq->enabled) {
1893 #else
1894  vlib_cli_output(vm, " kickfd %d callfd %d errfd\n",
1895  vq->kickfd, vq->callfd, vui->vrings[q].errfd);
1896 
1897  if (show_descr) {
1898 #endif
1899  vlib_cli_output(vm, "\n descriptor table:\n");
1900  vlib_cli_output(vm, " id addr len flags next user_addr\n");
1901  vlib_cli_output(vm, " ===== ================== ===== ====== ===== ==================\n");
1902  for(j = 0; j < vq->size; j++) {
1903  vlib_cli_output(vm, " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
1904  j,
1905  vq->desc[j].addr,
1906  vq->desc[j].len,
1907  vq->desc[j].flags,
1908  vq->desc[j].next,
1909  (u64) map_guest_mem(xd, vq->desc[j].addr));}
1910  }
1911  }
1912  vlib_cli_output (vm, "\n");
1913  }
1914 done:
1915  vec_free (hw_if_indices);
1916  return error;
1917 }
1918 
1919 VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
1920  .path = "show vhost-user",
1921  .short_help = "show vhost-user interface",
1922  .function = show_dpdk_vhost_user_command_fn,
1923 };
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:394
unix_file_t * file_pool
Definition: unix.h:85
void dpdk_device_lock_free(dpdk_device_t *xd)
Definition: init.c:219
vmrglw vmrglh hi
static clib_error_t * dpdk_vhost_user_socket_read(unix_file_t *uf)
Definition: vhost_user.c:943
static dpdk_device_t * dpdk_vhost_user_device_from_hw_if_index(u32 hw_if_index)
Definition: vhost_user.c:132
#define hash_set(h, key, value)
Definition: hash.h:237
always_inline vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
vlib_node_registration_t dpdk_io_input_node
(constructor) VLIB_REGISTER_NODE (dpdk_io_input_node)
Definition: node.c:1934
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:454
static void dpdk_vhost_user_vui_init(vnet_main_t *vnm, dpdk_device_t *xd, int sockfd, const char *sock_filename, u8 is_server, u64 feature_mask, u32 *sw_if_index)
Definition: vhost_user.c:878
u32 n_since_last_int
Definition: dpdk.h:147
u8 dpdk_vhost_user_want_interrupt(dpdk_device_t *xd, int idx)
Definition: vhost_user.c:836
unix_file_function_t * read_function
Definition: unix.h:61
#define hash_unset(h, key)
Definition: hash.h:243
u32 region_fd[VHOST_MEMORY_MAX_NREGIONS]
Definition: dpdk.h:171
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface.c:205
dpdk_main_t dpdk_main
Definition: dpdk.h:415
unsigned int uint32_t
Definition: fix_types.h:29
vnet_device_class_t dpdk_device_class
static clib_error_t * dpdk_vhost_user_get_vring_base(u32 hw_if_index, u8 idx, u32 *num)
Definition: vhost_user.c:615
always_inline void clib_mem_free(void *p)
Definition: mem.h:149
u8 need_txlock
Definition: dpdk.h:257
always_inline void unformat_free(unformat_input_t *i)
Definition: format.h:160
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
#define NULL
Definition: clib.h:55
u32 vhost_coalesce_frames
Definition: dpdk.h:376
#define vec_add2_aligned(V, P, N, A)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:531
f64 vhost_coalesce_time
Definition: dpdk.h:377
static clib_error_t * dpdk_vhost_user_socket_error(unix_file_t *uf)
Definition: vhost_user.c:1288
static clib_error_t * dpdk_vhost_user_set_features(u32 hw_if_index, u64 features)
Definition: vhost_user.c:454
always_inline uword unix_file_add(unix_main_t *um, unix_file_t *template)
Definition: unix.h:131
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
static clib_error_t * dpdk_vhost_user_callfd_read_ready(unix_file_t *uf)
Definition: vhost_user.c:794
int vnet_interface_name_renumber(u32 sw_if_index, u32 new_show_dev_instance)
Definition: interface.c:1028
struct virtio_net vu_vhost_dev
Definition: dpdk.h:238
#define VHOST_USER_MSG_HDR_SZ
Definition: vhost-user.h:20
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
u32 per_interface_next_index
Definition: dpdk.h:199
#define VHOST_USER_PROTOCOL_FEATURES
Definition: vhost-user.h:36
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:1871
static clib_error_t * dpdk_vhost_user_set_vring_call(u32 hw_if_index, u8 idx, int fd)
Definition: vhost_user.c:803
u32 next_vu_if_id
Definition: dpdk.h:385
static clib_error_t * dpdk_vhost_user_set_vring_kick(u32 hw_if_index, u8 idx, int fd)
Definition: vhost_user.c:693
#define VNET_HW_INTERFACE_FLAG_LINK_UP
Definition: interface.h:241
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:405
u64 packets
Definition: dpdk.h:149
always_inline vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
#define VHOST_USER_REPLY_MASK
Definition: vhost-user.h:27
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
u8 admin_up
Definition: dpdk.h:218
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
struct rte_mbuf *** tx_vectors
Definition: dpdk.h:202
static void dpdk_vhost_user_if_disconnect(dpdk_device_t *xd)
Definition: vhost_user.c:920
vlib_node_registration_t dpdk_input_node
(constructor) VLIB_REGISTER_NODE (dpdk_input_node)
Definition: node.c:829
dpdk_vu_vring vrings[VHOST_MAX_QUEUE_PAIRS *2]
Definition: dpdk.h:166
struct sockaddr_un sun
Definition: vhost_user.c:1597
u32 vu_is_running
Definition: dpdk.h:239
int input_cpu_first_index
Definition: dpdk.h:399
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
always_inline void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Definition: node_funcs.h:100
static clib_error_t * dpdk_vhost_user_get_features(u32 hw_if_index, u64 *features)
Definition: vhost_user.c:430
u16 rx_q_used
Definition: dpdk.h:225
int sock_errno
Definition: dpdk.h:159
#define foreach_dpdk_vhost_feature
Definition: vhost_user.c:1768
#define clib_warning(format, args...)
Definition: error.h:59
unsigned long u64
Definition: types.h:89
static clib_error_t * dpdk_vhost_user_set_vring_num(u32 hw_if_index, u8 idx, u32 num)
Definition: vhost_user.c:546
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
vhost_user_log_t log
Definition: vhost-user.h:80
always_inline u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:68
u32 device_index
Definition: dpdk.h:193
static uword pointer_to_uword(const void *p)
Definition: types.h:131
uword dpdk_vhost_user_process_if(vlib_main_t *vm, dpdk_device_t *xd, void *ctx)
Definition: vhost_user.c:1620
u32 vlib_sw_if_index
Definition: dpdk.h:196
#define hash_get(h, key)
Definition: hash.h:231
#define pool_elt_at_index(p, i)
Definition: pool.h:346
unix_file_t template
Definition: vhost_user.c:1599
u32 file_descriptor
Definition: unix.h:51
dpdk_device_and_queue_t ** devices_by_cpu
Definition: dpdk.h:317
int kickfd
Definition: dpdk.h:141
int vhost_user_delete_if(vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index)
Definition: vhost-user.c:1449
u32 vlib_hw_if_index
Definition: dpdk.h:195
u8 use_virtio_vhost
Definition: dpdk.h:373
unix_file_function_t * error_function
Definition: unix.h:61
static clib_error_t * dpdk_create_vhost_user_if_internal(u32 *hw_if_index, u32 if_id, u8 *hwaddr)
Definition: vhost_user.c:195
#define clib_error_return_unix(e, args...)
Definition: error.h:115
u64 bytes
Definition: dpdk.h:150
#define PREDICT_FALSE(x)
Definition: clib.h:97
static void stop_processing_packets(u32 hw_if_index, u8 idx)
Definition: vhost_user.c:155
int input_cpu_count
Definition: dpdk.h:400
#define VLIB_FRAME_SIZE
Definition: node.h:292
u16 tx_q_used
Definition: dpdk.h:224
u32 main_thread_is_io_node
Definition: threads.h:268
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:538
int vhost_user_dump_ifs(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_details_t **out_vuids)
Definition: vhost-user.c:1821
int enabled
Definition: dpdk.h:144
#define uword_to_pointer(u, type)
Definition: types.h:134
static void disable_interface(dpdk_device_t *xd)
Definition: vhost_user.c:167
u64 region_addr[VHOST_MEMORY_MAX_NREGIONS]
Definition: dpdk.h:170
#define OFFLOAD_FEATURES
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:1654
static int dpdk_vhost_user_set_vring_enable(u32 hw_if_index, u8 idx, int enable)
Definition: vhost_user.c:754
int dpdk_vhost_user_delete_if(vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index)
Definition: vhost_user.c:1492
always_inline void * clib_mem_alloc(uword size)
Definition: mem.h:109
dpdk_device_t * devices
Definition: dpdk.h:316
u16 * cpu_socket_id_by_queue
Definition: dpdk.h:228
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
volatile u32 ** lockp
Definition: dpdk.h:190
vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS]
Definition: vhost-user.h:83
int dpdk_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:1396
struct rte_mbuf *** rx_vectors
Definition: dpdk.h:203
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:1700
u32 use_rss
Definition: dpdk.h:355
char sock_filename[256]
Definition: dpdk.h:158
#define clib_memcpy(a, b, c)
Definition: string.h:63
#define VHOST_MEMORY_MAX_NREGIONS
Definition: vhost-user.h:19
static void dpdk_vhost_user_vui_register(vlib_main_t *vm, dpdk_device_t *xd)
Definition: vhost_user.c:910
static clib_error_t * dpdk_vhost_user_set_vring_base(u32 hw_if_index, u8 idx, u32 num)
Definition: vhost_user.c:671
static clib_error_t * dpdk_vhost_user_set_protocol_features(u32 hw_if_index, u64 prot_features)
Definition: vhost_user.c:419
static dpdk_device_t * dpdk_vhost_user_device_from_sw_if_index(u32 sw_if_index)
Definition: vhost_user.c:146
static clib_error_t * dpdk_vhost_user_connect_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost_user.c:1663
#define ARRAY_LEN(x)
Definition: clib.h:59
int dpdk_vhost_user_dump_ifs(vnet_main_t *vnm, vlib_main_t *vm, vhost_user_intf_details_t **out_vuids)
Definition: vhost_user.c:1525
always_inline vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
static int dpdk_vhost_user_init_server_sock(const char *sock_filename, int *sockfd)
Definition: vhost_user.c:1350
u32 num_vrings
Definition: dpdk.h:164
static clib_error_t * show_dpdk_vhost_user_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost_user.c:1774
#define VHOST_USER_PROTOCOL_F_LOG_SHMFD
Definition: vhost-user.h:30
#define vec_validate_ha(V, I, H, A)
Make sure vector is long enough for given index (general version).
Definition: vec.h:374
f64 int_deadline
Definition: dpdk.h:148
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
vlib_worker_thread_t * vlib_worker_threads
Definition: threads.h:106
int callfd
Definition: dpdk.h:140
void dpdk_vhost_user_process_init(void **ctx)
Definition: vhost_user.c:1603
always_inline void unix_file_del(unix_main_t *um, unix_file_t *f)
Definition: unix.h:141
u32 vu_if_id
Definition: dpdk.h:237
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:187
#define DPDK_TX_RING_SIZE
Definition: dpdk.h:277
#define ASSERT(truth)
void vlib_worker_thread_barrier_sync(vlib_main_t *vm)
Definition: threads.c:1100
unsigned int u32
Definition: types.h:88
u32 * vu_inactive_interfaces_device_index
Definition: dpdk.h:383
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
dpdk_frame_t * frames
Definition: dpdk.h:209
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
void dpdk_vhost_user_send_interrupt(vlib_main_t *vm, dpdk_device_t *xd, int idx)
Definition: vhost_user.c:852
vhost_vring_state_t state
Definition: vhost-user.h:77
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:1794
void dpdk_device_lock_init(dpdk_device_t *xd)
Definition: init.c:205
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:157
u8 sock_is_server
Definition: dpdk.h:160
u32 callfd_idx
Definition: dpdk.h:146
unix_main_t unix_main
Definition: main.c:57
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1131
u32 unix_file_index
Definition: dpdk.h:156
u64 uword
Definition: types.h:112
static void * map_guest_mem(dpdk_device_t *xd, u64 addr)
Definition: vhost_user.c:179
unsigned short u16
Definition: types.h:57
static long get_huge_page_size(int fd)
Definition: vhost_user.c:409
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:140
dpdk_device_type_t dev_type
Definition: dpdk.h:214
unsigned char u8
Definition: types.h:56
static const char * vhost_message_str[]
Definition: vhost_user.c:42
always_inline vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
#define VHOST_NET_VRING_IDX_RX
Definition: vhost-user.h:22
static clib_error_t * dpdk_vhost_user_set_mem_table(u32 hw_if_index, vhost_user_memory_t *vum, int fd[])
Definition: vhost_user.c:495
Definition: unix.h:49
void dpdk_vhost_user_process_cleanup(void *ctx)
Definition: vhost_user.c:1615
u32 client_fd
Definition: dpdk.h:157
#define VHOST_USER_F_PROTOCOL_FEATURES
Definition: vhost-user.h:34
#define VHOST_NET_VRING_IDX_TX
Definition: vhost-user.h:23
vnet_sw_interface_type_t type
Definition: interface.h:368
static clib_error_t * dpdk_vhost_user_socksvr_accept_ready(unix_file_t *uf)
Definition: vhost_user.c:1307
#define vec_foreach(var, vec)
Vector iterator.
always_inline f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
static uint64_t qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
Definition: vhost_user.c:110
u32 unix_fd
Definition: dpdk.h:155
u64 feature_mask
Definition: dpdk.h:163
static clib_error_t * dpdk_vhost_user_set_vring_addr(u32 hw_if_index, u8 idx, u64 desc, u64 used, u64 avail, u64 log)
Definition: vhost_user.c:566
vhost_vring_addr_t addr
Definition: vhost-user.h:78
dpdk_vu_intf_t * vu_intf
Definition: dpdk.h:240
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
int dpdk_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:1442
u32 is_up
Definition: dpdk.h:154
vnet_main_t * vnet_main
Definition: dpdk.h:412
unformat_function_t unformat_line_input
Definition: format.h:279
static clib_error_t * dpdk_vhost_user_delete_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: vhost_user.c:1723
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:1746
int errfd
Definition: dpdk.h:142
vlib_main_t ** vlib_mains
Definition: buffer.c:244
#define DBG_SOCK(args...)
Definition: vhost_user.c:39
uword * vu_sw_if_index_by_listener_fd
Definition: dpdk.h:381
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
always_inline vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
uword * vu_sw_if_index_by_sock_fd
Definition: dpdk.h:382