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