blob: 6b2c898acd52bd82cece1ddf4e9f66ae33797bb2 [file] [log] [blame]
Damjan Marion00a9dca2016-08-17 17:05:46 +02001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
3 * vhost.c - vhost-user
4 *
5 * Copyright (c) 2014 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <fcntl.h> /* for open */
21#include <sys/ioctl.h>
22#include <sys/socket.h>
23#include <sys/un.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/uio.h> /* for iovec */
27#include <netinet/in.h>
28#include <sys/vfs.h>
29
30#include <linux/if_arp.h>
31#include <linux/if_tun.h>
32
33#include <vlib/vlib.h>
34#include <vlib/unix/unix.h>
35
36#include <vnet/ip/ip.h>
37
38#include <vnet/ethernet/ethernet.h>
Damjan Marion8bdc63b2016-11-02 14:48:21 +010039#include <vnet/devices/devices.h>
Damjan Marion22311502016-10-28 20:30:15 +020040#include <vnet/feature/feature.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070041
42#include <vnet/devices/virtio/vhost-user.h>
43
Billy McFalla92501a2016-11-23 12:45:29 -050044/**
45 * @file
46 * @brief vHost User Device Driver.
47 *
48 * This file contains the source code for vHost User interface.
49 */
50
51
Pierre Pfister116ea4b2016-11-08 15:49:28 +000052#define VHOST_DEBUG_VQ 0
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Steven388e51a2017-06-01 12:49:23 -070054#define DBG_SOCK(args...) \
55 { \
56 vhost_user_main_t *_vum = &vhost_user_main; \
57 if (_vum->debug) \
58 clib_warning(args); \
59 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
Pierre Pfister116ea4b2016-11-08 15:49:28 +000061#if VHOST_DEBUG_VQ == 1
Ed Warnickecb9cada2015-12-08 15:45:58 -070062#define DBG_VQ(args...) clib_warning(args);
63#else
64#define DBG_VQ(args...)
65#endif
66
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +000067/*
68 * When an RX queue is down but active, received packets
69 * must be discarded. This value controls up to how many
70 * packets will be discarded during each round.
71 */
72#define VHOST_USER_DOWN_DISCARD_COUNT 256
73
74/*
75 * When the number of available buffers gets under this threshold,
76 * RX node will start discarding packets.
77 */
78#define VHOST_USER_RX_BUFFER_STARVATION 32
79
80/*
81 * On the receive side, the host should free descriptors as soon
82 * as possible in order to avoid TX drop in the VM.
83 * This value controls the number of copy operations that are stacked
84 * before copy is done for all and descriptors are given back to
85 * the guest.
86 * The value 64 was obtained by testing (48 and 128 were not as good).
87 */
88#define VHOST_USER_RX_COPY_THRESHOLD 64
89
Pierre Pfisterdbb3c252016-11-22 10:33:34 +000090#define UNIX_GET_FD(unixfd_idx) \
91 (unixfd_idx != ~0) ? \
Damjan Marion56dd5432017-09-08 19:52:02 +020092 pool_elt_at_index (file_main.file_pool, \
Pierre Pfisterdbb3c252016-11-22 10:33:34 +000093 unixfd_idx)->file_descriptor : -1;
94
Pierre Pfister116ea4b2016-11-08 15:49:28 +000095#define foreach_virtio_trace_flags \
96 _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
97 _ (SINGLE_DESC, 1, "Single descriptor packet") \
98 _ (INDIRECT, 2, "Indirect descriptor") \
99 _ (MAP_ERROR, 4, "Memory mapping error")
100
101typedef enum
102{
103#define _(n,i,s) VIRTIO_TRACE_F_##n,
104 foreach_virtio_trace_flags
105#undef _
106} virtio_trace_flag_t;
107
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108vlib_node_registration_t vhost_user_input_node;
109
110#define foreach_vhost_user_tx_func_error \
Pierre Pfister328e99b2016-02-12 13:18:42 +0000111 _(NONE, "no error") \
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000112 _(NOT_READY, "vhost vring not ready") \
113 _(DOWN, "vhost interface is down") \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \
Pierre Pfisterba1d0462016-07-27 16:38:20 +0100115 _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \
116 _(MMAP_FAIL, "mmap failure") \
117 _(INDIRECT_OVERFLOW, "indirect descriptor table overflow")
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Damjan Marion00a9dca2016-08-17 17:05:46 +0200119typedef enum
120{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121#define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f,
122 foreach_vhost_user_tx_func_error
123#undef _
Damjan Marion00a9dca2016-08-17 17:05:46 +0200124 VHOST_USER_TX_FUNC_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125} vhost_user_tx_func_error_t;
126
Damjan Marion00a9dca2016-08-17 17:05:46 +0200127static char *vhost_user_tx_func_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128#define _(n,s) s,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200129 foreach_vhost_user_tx_func_error
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130#undef _
131};
132
133#define foreach_vhost_user_input_func_error \
134 _(NO_ERROR, "no error") \
Pierre Pfister328e99b2016-02-12 13:18:42 +0000135 _(NO_BUFFER, "no available buffer") \
136 _(MMAP_FAIL, "mmap failure") \
Pierre Pfisterba1d0462016-07-27 16:38:20 +0100137 _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \
138 _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \
139 _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)")
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Damjan Marion00a9dca2016-08-17 17:05:46 +0200141typedef enum
142{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143#define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f,
144 foreach_vhost_user_input_func_error
145#undef _
Damjan Marion00a9dca2016-08-17 17:05:46 +0200146 VHOST_USER_INPUT_FUNC_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147} vhost_user_input_func_error_t;
148
Damjan Marion00a9dca2016-08-17 17:05:46 +0200149static char *vhost_user_input_func_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150#define _(n,s) s,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200151 foreach_vhost_user_input_func_error
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152#undef _
153};
154
Damjan Marion00a9dca2016-08-17 17:05:46 +0200155/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156static vhost_user_main_t vhost_user_main = {
157 .mtu_bytes = 1518,
158};
159
160VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = {
161 .name = "vhost-user",
162};
Damjan Marion00a9dca2016-08-17 17:05:46 +0200163/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
Damjan Marion00a9dca2016-08-17 17:05:46 +0200165static u8 *
166format_vhost_user_interface_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167{
168 u32 i = va_arg (*args, u32);
169 u32 show_dev_instance = ~0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200170 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
172 if (i < vec_len (vum->show_dev_instance_by_real_dev_instance))
173 show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i];
174
175 if (show_dev_instance != ~0)
176 i = show_dev_instance;
177
178 s = format (s, "VirtualEthernet0/0/%d", i);
179 return s;
180}
181
Damjan Marion00a9dca2016-08-17 17:05:46 +0200182static int
183vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184{
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000185 // FIXME: check if the new dev instance is already used
Damjan Marion00a9dca2016-08-17 17:05:46 +0200186 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200188 hi->dev_instance, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
Damjan Marion00a9dca2016-08-17 17:05:46 +0200190 vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] =
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191 new_dev_instance;
192
Damjan Marion00a9dca2016-08-17 17:05:46 +0200193 DBG_SOCK ("renumbered vhost-user interface dev_instance %d to %d",
194 hi->dev_instance, new_dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195
196 return 0;
197}
198
Pierre Pfister11f92052016-09-21 08:08:55 +0100199static_always_inline void *
200map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201{
Pierre Pfister11f92052016-09-21 08:08:55 +0100202 int i = *hint;
203 if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) &&
204 ((vui->regions[i].guest_phys_addr +
205 vui->regions[i].memory_size) > addr)))
206 {
207 return (void *) (vui->region_mmap_addr[i] + addr -
208 vui->regions[i].guest_phys_addr);
209 }
Damjan Marion37623702016-09-20 11:25:27 +0200210#if __SSE4_2__
211 __m128i rl, rh, al, ah, r;
212 al = _mm_set1_epi64x (addr + 1);
213 ah = _mm_set1_epi64x (addr);
214
215 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]);
216 rl = _mm_cmpgt_epi64 (al, rl);
217 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]);
218 rh = _mm_cmpgt_epi64 (rh, ah);
219 r = _mm_and_si128 (rl, rh);
220
221 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]);
222 rl = _mm_cmpgt_epi64 (al, rl);
223 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]);
224 rh = _mm_cmpgt_epi64 (rh, ah);
225 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22);
226
227 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]);
228 rl = _mm_cmpgt_epi64 (al, rl);
229 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]);
230 rh = _mm_cmpgt_epi64 (rh, ah);
231 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44);
232
233 rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]);
234 rl = _mm_cmpgt_epi64 (al, rl);
235 rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]);
236 rh = _mm_cmpgt_epi64 (rh, ah);
237 r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88);
238
239 r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800));
Damjan Marion0b49e2b2017-02-09 21:49:06 +0100240 i = __builtin_ctzll (_mm_movemask_epi8 (r) |
241 (1 << VHOST_MEMORY_MAX_NREGIONS));
Damjan Marion37623702016-09-20 11:25:27 +0200242
243 if (i < vui->nregions)
244 {
Pierre Pfister11f92052016-09-21 08:08:55 +0100245 *hint = i;
Damjan Marion37623702016-09-20 11:25:27 +0200246 return (void *) (vui->region_mmap_addr[i] + addr -
247 vui->regions[i].guest_phys_addr);
248 }
249
250#else
Damjan Marion00a9dca2016-08-17 17:05:46 +0200251 for (i = 0; i < vui->nregions; i++)
252 {
253 if ((vui->regions[i].guest_phys_addr <= addr) &&
254 ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) >
255 addr))
256 {
Pierre Pfister11f92052016-09-21 08:08:55 +0100257 *hint = i;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200258 return (void *) (vui->region_mmap_addr[i] + addr -
259 vui->regions[i].guest_phys_addr);
260 }
261 }
Damjan Marion37623702016-09-20 11:25:27 +0200262#endif
Damjan Marion00a9dca2016-08-17 17:05:46 +0200263 DBG_VQ ("failed to map guest mem addr %llx", addr);
Pierre Pfister11f92052016-09-21 08:08:55 +0100264 *hint = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265 return 0;
266}
267
Damjan Marion00a9dca2016-08-17 17:05:46 +0200268static inline void *
269map_user_mem (vhost_user_intf_t * vui, uword addr)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270{
271 int i;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200272 for (i = 0; i < vui->nregions; i++)
273 {
274 if ((vui->regions[i].userspace_addr <= addr) &&
275 ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) >
276 addr))
277 {
278 return (void *) (vui->region_mmap_addr[i] + addr -
279 vui->regions[i].userspace_addr);
280 }
281 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282 return 0;
283}
284
Damjan Marion00a9dca2016-08-17 17:05:46 +0200285static long
286get_huge_page_size (int fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287{
288 struct statfs s;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200289 fstatfs (fd, &s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 return s.f_bsize;
291}
292
Damjan Marion00a9dca2016-08-17 17:05:46 +0200293static void
294unmap_all_mem_regions (vhost_user_intf_t * vui)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200296 int i, r;
297 for (i = 0; i < vui->nregions; i++)
298 {
299 if (vui->region_mmap_addr[i] != (void *) -1)
300 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
Damjan Marion00a9dca2016-08-17 17:05:46 +0200302 long page_sz = get_huge_page_size (vui->region_mmap_fd[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303
Damjan Marion00a9dca2016-08-17 17:05:46 +0200304 ssize_t map_sz = (vui->regions[i].memory_size +
305 vui->regions[i].mmap_offset +
Pierre Pfisterbed54892017-04-20 15:34:00 +0200306 page_sz - 1) & ~(page_sz - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
Damjan Marion00a9dca2016-08-17 17:05:46 +0200308 r =
309 munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset,
310 map_sz);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
Damjan Marion00a9dca2016-08-17 17:05:46 +0200312 DBG_SOCK
313 ("unmap memory region %d addr 0x%lx len 0x%lx page_sz 0x%x", i,
314 vui->region_mmap_addr[i], map_sz, page_sz);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Damjan Marion00a9dca2016-08-17 17:05:46 +0200316 vui->region_mmap_addr[i] = (void *) -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
Damjan Marion00a9dca2016-08-17 17:05:46 +0200318 if (r == -1)
319 {
320 clib_warning ("failed to unmap memory region (errno %d)",
321 errno);
322 }
323 close (vui->region_mmap_fd[i]);
324 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 vui->nregions = 0;
327}
328
Pierre Pfistere21c5282016-09-21 08:04:59 +0100329static void
330vhost_user_tx_thread_placement (vhost_user_intf_t * vui)
331{
332 //Let's try to assign one queue to each thread
333 u32 qid = 0;
Damjan Marion586afd72017-04-05 19:18:20 +0200334 u32 thread_index = 0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100335 vui->use_tx_spinlock = 0;
336 while (1)
337 {
338 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
339 {
340 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
341 if (!rxvq->started || !rxvq->enabled)
342 continue;
343
Damjan Marion586afd72017-04-05 19:18:20 +0200344 vui->per_cpu_tx_qid[thread_index] = qid;
345 thread_index++;
346 if (thread_index == vlib_get_thread_main ()->n_vlib_mains)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100347 return;
348 }
349 //We need to loop, meaning the spinlock has to be used
350 vui->use_tx_spinlock = 1;
Damjan Marion586afd72017-04-05 19:18:20 +0200351 if (thread_index == 0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100352 {
353 //Could not find a single valid one
Damjan Marion586afd72017-04-05 19:18:20 +0200354 for (thread_index = 0;
355 thread_index < vlib_get_thread_main ()->n_vlib_mains;
356 thread_index++)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100357 {
Damjan Marion586afd72017-04-05 19:18:20 +0200358 vui->per_cpu_tx_qid[thread_index] = 0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100359 }
360 return;
361 }
362 }
363}
364
Stevenf3b53642017-05-01 14:03:02 -0700365/**
366 * @brief Unassign existing interface/queue to thread mappings and re-assign
367 * new interface/queue to thread mappings
368 */
Pierre Pfistere21c5282016-09-21 08:04:59 +0100369static void
370vhost_user_rx_thread_placement ()
371{
372 vhost_user_main_t *vum = &vhost_user_main;
373 vhost_user_intf_t *vui;
Stevenf3b53642017-05-01 14:03:02 -0700374 vhost_user_vring_t *txvq;
375 vnet_main_t *vnm = vnet_get_main ();
376 u32 qid;
377 int rv;
378 u16 *queue;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100379
Stevenf3b53642017-05-01 14:03:02 -0700380 // Scrap all existing mappings for all interfaces/queues
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000381 /* *INDENT-OFF* */
382 pool_foreach (vui, vum->vhost_user_interfaces, {
Stevenf3b53642017-05-01 14:03:02 -0700383 vec_foreach (queue, vui->rx_queues)
384 {
385 rv = vnet_hw_interface_unassign_rx_thread (vnm, vui->hw_if_index,
386 *queue);
387 if (rv)
388 clib_warning ("Warning: unable to unassign interface %d, "
389 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
390 }
391 vec_reset_length (vui->rx_queues);
392 });
393 /* *INDENT-ON* */
394
395 // Create the rx_queues for all interfaces
396 /* *INDENT-OFF* */
397 pool_foreach (vui, vum->vhost_user_interfaces, {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000398 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
399 {
Stevenf3b53642017-05-01 14:03:02 -0700400 txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
401 if (txvq->started)
402 {
403 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
404 /* Set polling as the default */
405 txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
406 vec_add1 (vui->rx_queues, qid);
407 }
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000408 }
409 });
410 /* *INDENT-ON* */
Steven7312cc72017-03-15 21:18:55 -0700411
Stevenf3b53642017-05-01 14:03:02 -0700412 // Assign new mappings for all interfaces/queues
413 /* *INDENT-OFF* */
414 pool_foreach (vui, vum->vhost_user_interfaces, {
415 vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
416 vhost_user_input_node.index);
417 vec_foreach (queue, vui->rx_queues)
Steven7312cc72017-03-15 21:18:55 -0700418 {
Stevenf3b53642017-05-01 14:03:02 -0700419 vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, *queue,
420 ~0);
421 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
422 rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, *queue,
423 txvq->mode);
424 if (rv)
425 clib_warning ("Warning: unable to set rx mode for interface %d, "
426 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
Steven7312cc72017-03-15 21:18:55 -0700427 }
Stevenf3b53642017-05-01 14:03:02 -0700428 });
429 /* *INDENT-ON* */
Pierre Pfistere21c5282016-09-21 08:04:59 +0100430}
431
432/** @brief Returns whether at least one TX and one RX vring are enabled */
433int
434vhost_user_intf_ready (vhost_user_intf_t * vui)
435{
436 int i, found[2] = { }; //RX + TX
437
438 for (i = 0; i < VHOST_VRING_MAX_N; i++)
439 if (vui->vrings[i].started && vui->vrings[i].enabled)
440 found[i & 1] = 1;
441
442 return found[0] && found[1];
443}
444
445static void
446vhost_user_update_iface_state (vhost_user_intf_t * vui)
447{
448 /* if we have pointers to descriptor table, go up */
449 int is_up = vhost_user_intf_ready (vui);
450 if (is_up != vui->is_up)
451 {
452 DBG_SOCK ("interface %d %s", vui->sw_if_index,
453 is_up ? "ready" : "down");
454 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
455 is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP :
456 0);
457 vui->is_up = is_up;
458 }
459 vhost_user_rx_thread_placement ();
460 vhost_user_tx_thread_placement (vui);
461}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Steven7312cc72017-03-15 21:18:55 -0700463static void
464vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
465{
Stevenf3b53642017-05-01 14:03:02 -0700466 u32 qid;
467 vnet_main_t *vnm = vnet_get_main ();
Stevene4dcba82017-04-04 16:56:54 -0700468
469 qid = ifq & 0xff;
Stevenf3b53642017-05-01 14:03:02 -0700470 if ((qid & 1) == 0)
471 /* Only care about the odd number, or TX, virtqueue */
Stevene4dcba82017-04-04 16:56:54 -0700472 return;
Steven7312cc72017-03-15 21:18:55 -0700473
474 if (vhost_user_intf_ready (vui))
Stevenf3b53642017-05-01 14:03:02 -0700475 // qid >> 1 is to convert virtqueue number to vring queue index
476 vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1);
Steven7312cc72017-03-15 21:18:55 -0700477}
478
Damjan Marion00a9dca2016-08-17 17:05:46 +0200479static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200480vhost_user_callfd_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200482 __attribute__ ((unused)) int n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483 u8 buff[8];
Steven7312cc72017-03-15 21:18:55 -0700484
Damjan Marion00a9dca2016-08-17 17:05:46 +0200485 n = read (uf->file_descriptor, ((char *) &buff), 8);
Steven7312cc72017-03-15 21:18:55 -0700486
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 return 0;
488}
489
Pierre Pfistere21c5282016-09-21 08:04:59 +0100490static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200491vhost_user_kickfd_read_ready (clib_file_t * uf)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100492{
493 __attribute__ ((unused)) int n;
494 u8 buff[8];
495 vhost_user_intf_t *vui =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000496 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
497 uf->private_data >> 8);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100498 u32 qid = uf->private_data & 0xff;
Steven7312cc72017-03-15 21:18:55 -0700499
Pierre Pfistere21c5282016-09-21 08:04:59 +0100500 n = read (uf->file_descriptor, ((char *) &buff), 8);
501 DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
Steven7312cc72017-03-15 21:18:55 -0700502 if (!vui->vrings[qid].started ||
503 (vhost_user_intf_ready (vui) != vui->is_up))
504 {
Stevene4dcba82017-04-04 16:56:54 -0700505 vlib_worker_thread_barrier_sync (vlib_get_main ());
Steven7312cc72017-03-15 21:18:55 -0700506 vui->vrings[qid].started = 1;
507 vhost_user_update_iface_state (vui);
Stevene4dcba82017-04-04 16:56:54 -0700508 vlib_worker_thread_barrier_release (vlib_get_main ());
Steven7312cc72017-03-15 21:18:55 -0700509 }
Steven7312cc72017-03-15 21:18:55 -0700510
511 vhost_user_set_interrupt_pending (vui, uf->private_data);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100512 return 0;
513}
514
515/**
516 * @brief Try once to lock the vring
517 * @return 0 on success, non-zero on failure.
518 */
519static inline int
520vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
521{
522 return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
523}
524
525/**
526 * @brief Spin until the vring is successfully locked
527 */
528static inline void
529vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
530{
531 while (vhost_user_vring_try_lock (vui, qid))
532 ;
533}
534
535/**
536 * @brief Unlock the vring lock
537 */
538static inline void
539vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
540{
541 *vui->vring_locks[qid] = 0;
542}
543
544static inline void
545vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
546{
547 vhost_user_vring_t *vring = &vui->vrings[qid];
548 memset (vring, 0, sizeof (*vring));
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000549 vring->kickfd_idx = ~0;
550 vring->callfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100551 vring->errfd = -1;
552
553 /*
554 * We have a bug with some qemu 2.5, and this may be a fix.
555 * Feel like interpretation holy text, but this is from vhost-user.txt.
556 * "
557 * One queue pair is enabled initially. More queues are enabled
558 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
559 * "
560 * Don't know who's right, but this is what DPDK does.
561 */
562 if (qid == 0 || qid == 1)
563 vring->enabled = 1;
564}
565
566static inline void
567vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
568{
569 vhost_user_vring_t *vring = &vui->vrings[qid];
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000570 if (vring->kickfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100571 {
Damjan Marion56dd5432017-09-08 19:52:02 +0200572 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
Pierre Pfistere21c5282016-09-21 08:04:59 +0100573 vring->kickfd_idx);
Damjan Marion56dd5432017-09-08 19:52:02 +0200574 clib_file_del (&file_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000575 vring->kickfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100576 }
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000577 if (vring->callfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100578 {
Damjan Marion56dd5432017-09-08 19:52:02 +0200579 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
Pierre Pfistere21c5282016-09-21 08:04:59 +0100580 vring->callfd_idx);
Damjan Marion56dd5432017-09-08 19:52:02 +0200581 clib_file_del (&file_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000582 vring->callfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100583 }
584 if (vring->errfd != -1)
Stevenf6dae052017-03-09 23:49:32 -0800585 {
586 close (vring->errfd);
587 vring->errfd = -1;
588 }
Pierre Pfistere21c5282016-09-21 08:04:59 +0100589 vhost_user_vring_init (vui, qid);
590}
591
Damjan Marion00a9dca2016-08-17 17:05:46 +0200592static inline void
593vhost_user_if_disconnect (vhost_user_intf_t * vui)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200595 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596 int q;
597
Damjan Marion00a9dca2016-08-17 17:05:46 +0200598 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
Damjan Marion56dd5432017-09-08 19:52:02 +0200600 if (vui->clib_file_index != ~0)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200601 {
Damjan Marion56dd5432017-09-08 19:52:02 +0200602 clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index);
603 vui->clib_file_index = ~0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200604 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606 vui->is_up = 0;
Steve Shin44489572016-09-22 12:08:55 -0700607
Pierre Pfistere21c5282016-09-21 08:04:59 +0100608 for (q = 0; q < VHOST_VRING_MAX_N; q++)
609 vhost_user_vring_close (vui, q);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610
Damjan Marion00a9dca2016-08-17 17:05:46 +0200611 unmap_all_mem_regions (vui);
612 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613}
614
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100615#define VHOST_LOG_PAGE 0x1000
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000616static_always_inline void
617vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
618 u64 addr, u64 len, u8 is_host_address)
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100619{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200620 if (PREDICT_TRUE (vui->log_base_addr == 0
621 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
622 {
623 return;
624 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000625 if (is_host_address)
626 {
Damjan Marion7bee80c2017-04-26 15:32:12 +0200627 addr = pointer_to_uword (map_user_mem (vui, (uword) addr));
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000628 }
Damjan Marion00a9dca2016-08-17 17:05:46 +0200629 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
630 {
631 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
632 return;
633 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100634
Damjan Marion00a9dca2016-08-17 17:05:46 +0200635 CLIB_MEMORY_BARRIER ();
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100636 u64 page = addr / VHOST_LOG_PAGE;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200637 while (page * VHOST_LOG_PAGE < addr + len)
638 {
639 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
640 page++;
641 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100642}
643
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000644static_always_inline void
645vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
646{
647 vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
648}
649
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100650#define vhost_user_log_dirty_ring(vui, vq, member) \
Yoann Desmouceauxfe2da0e2016-03-08 14:54:28 +0100651 if (PREDICT_FALSE(vq->log_used)) { \
Damjan Marion8d281b32016-08-24 14:32:39 +0200652 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
Yoann Desmouceauxfe2da0e2016-03-08 14:54:28 +0100653 sizeof(vq->used->member)); \
654 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100655
Damjan Marion00a9dca2016-08-17 17:05:46 +0200656static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200657vhost_user_socket_read (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658{
659 int n, i;
660 int fd, number_of_fds = 0;
661 int fds[VHOST_MEMORY_MAX_NREGIONS];
662 vhost_user_msg_t msg;
663 struct msghdr mh;
664 struct iovec iov[1];
Damjan Marion00a9dca2016-08-17 17:05:46 +0200665 vhost_user_main_t *vum = &vhost_user_main;
666 vhost_user_intf_t *vui;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667 struct cmsghdr *cmsg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 u8 q;
Damjan Marion56dd5432017-09-08 19:52:02 +0200669 clib_file_t template = { 0 };
Damjan Marion00a9dca2016-08-17 17:05:46 +0200670 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000672 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Damjan Marion00a9dca2016-08-17 17:05:46 +0200674 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675
Damjan Marion00a9dca2016-08-17 17:05:46 +0200676 memset (&mh, 0, sizeof (mh));
677 memset (control, 0, sizeof (control));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
Damjan Marion00a9dca2016-08-17 17:05:46 +0200679 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
Damjan Mariona290d7c2016-08-16 12:37:24 +0200680 fds[i] = -1;
681
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682 /* set the payload */
683 iov[0].iov_base = (void *) &msg;
684 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
685
686 mh.msg_iov = iov;
687 mh.msg_iovlen = 1;
688 mh.msg_control = control;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200689 mh.msg_controllen = sizeof (control);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690
Damjan Marion00a9dca2016-08-17 17:05:46 +0200691 n = recvmsg (uf->file_descriptor, &mh, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692
Pierre Pfistere21c5282016-09-21 08:04:59 +0100693 /* Stop workers to avoid end of the world */
694 vlib_worker_thread_barrier_sync (vlib_get_main ());
695
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696 if (n != VHOST_USER_MSG_HDR_SZ)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100697 {
698 if (n == -1)
699 {
700 DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
701 }
702 else
703 {
704 DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
705 n, VHOST_USER_MSG_HDR_SZ);
706 }
707 goto close_socket;
708 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709
Damjan Marion00a9dca2016-08-17 17:05:46 +0200710 if (mh.msg_flags & MSG_CTRUNC)
711 {
Pierre Pfistere21c5282016-09-21 08:04:59 +0100712 DBG_SOCK ("MSG_CTRUNC is set");
Damjan Marion00a9dca2016-08-17 17:05:46 +0200713 goto close_socket;
714 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715
Damjan Marion00a9dca2016-08-17 17:05:46 +0200716 cmsg = CMSG_FIRSTHDR (&mh);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717
718 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
719 (cmsg->cmsg_type == SCM_RIGHTS) &&
Damjan Marion00a9dca2016-08-17 17:05:46 +0200720 (cmsg->cmsg_len - CMSG_LEN (0) <=
721 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
722 {
723 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
724 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
725 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Damjan Marion00a9dca2016-08-17 17:05:46 +0200727 /* version 1, no reply bit set */
728 if ((msg.flags & 7) != 1)
729 {
730 DBG_SOCK ("malformed message received. closing socket");
731 goto close_socket;
732 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733
734 {
Pierre Pfistere21c5282016-09-21 08:04:59 +0100735 int rv;
736 rv =
737 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
738 msg.size);
739 if (rv < 0)
740 {
741 DBG_SOCK ("read failed %s", strerror (errno));
742 goto close_socket;
743 }
744 else if (rv != msg.size)
745 {
746 DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
747 goto close_socket;
748 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 }
750
Damjan Marion00a9dca2016-08-17 17:05:46 +0200751 switch (msg.request)
752 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753 case VHOST_USER_GET_FEATURES:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700754 msg.flags |= 4;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100755 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
756 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
757 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
758 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
759 (1ULL << FEAT_VHOST_F_LOG_ALL) |
760 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
761 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
762 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
763 (1ULL << FEAT_VIRTIO_F_VERSION_1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764 msg.u64 &= vui->feature_mask;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200765 msg.size = sizeof (msg.u64);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100766 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
767 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768 break;
769
770 case VHOST_USER_SET_FEATURES:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200771 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
772 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773
774 vui->features = msg.u64;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100775
Pierre Pfistere21c5282016-09-21 08:04:59 +0100776 if (vui->features &
777 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
778 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200779 vui->virtio_net_hdr_sz = 12;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780 else
Damjan Marion00a9dca2016-08-17 17:05:46 +0200781 vui->virtio_net_hdr_sz = 10;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782
Damjan Marion00a9dca2016-08-17 17:05:46 +0200783 vui->is_any_layout =
784 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785
786 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200787 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788 vui->is_up = 0;
789
Pierre Pfistere21c5282016-09-21 08:04:59 +0100790 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
791 vhost_user_vring_close(&vui->vrings[q]); */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792
793 break;
794
795 case VHOST_USER_SET_MEM_TABLE:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200796 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
797 vui->hw_if_index, msg.memory.nregions);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798
799 if ((msg.memory.nregions < 1) ||
Damjan Marion00a9dca2016-08-17 17:05:46 +0200800 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
801 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802
Damjan Marion00a9dca2016-08-17 17:05:46 +0200803 DBG_SOCK ("number of mem regions must be between 1 and %i",
804 VHOST_MEMORY_MAX_NREGIONS);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805
Damjan Marion00a9dca2016-08-17 17:05:46 +0200806 goto close_socket;
807 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808
Damjan Marion00a9dca2016-08-17 17:05:46 +0200809 if (msg.memory.nregions != number_of_fds)
810 {
811 DBG_SOCK ("each memory region must have FD");
812 goto close_socket;
813 }
814 unmap_all_mem_regions (vui);
815 for (i = 0; i < msg.memory.nregions; i++)
816 {
817 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
818 sizeof (vhost_user_memory_region_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819
Damjan Marion00a9dca2016-08-17 17:05:46 +0200820 long page_sz = get_huge_page_size (fds[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821
Damjan Marion00a9dca2016-08-17 17:05:46 +0200822 /* align size to 2M page */
823 ssize_t map_sz = (vui->regions[i].memory_size +
824 vui->regions[i].mmap_offset +
Pierre Pfisterbed54892017-04-20 15:34:00 +0200825 page_sz - 1) & ~(page_sz - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700826
Damjan Marion00a9dca2016-08-17 17:05:46 +0200827 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
828 MAP_SHARED, fds[i], 0);
Damjan Marion37623702016-09-20 11:25:27 +0200829 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
830 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
831 vui->regions[i].memory_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832
Damjan Marion00a9dca2016-08-17 17:05:46 +0200833 DBG_SOCK
834 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
835 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
836 page_sz);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837
Damjan Marion00a9dca2016-08-17 17:05:46 +0200838 if (vui->region_mmap_addr[i] == MAP_FAILED)
839 {
840 clib_warning ("failed to map memory. errno is %d", errno);
841 goto close_socket;
842 }
843 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
844 vui->region_mmap_fd[i] = fds[i];
845 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700846 vui->nregions = msg.memory.nregions;
847 break;
848
849 case VHOST_USER_SET_VRING_NUM:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200850 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
851 vui->hw_if_index, msg.state.index, msg.state.num);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852
Damjan Marion00a9dca2016-08-17 17:05:46 +0200853 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
854 (msg.state.num == 0) || /* it cannot be zero */
Pierre Pfistere21c5282016-09-21 08:04:59 +0100855 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200856 goto close_socket;
Steven97878892017-08-29 09:23:26 -0700857 vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858 break;
859
860 case VHOST_USER_SET_VRING_ADDR:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200861 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
862 vui->hw_if_index, msg.state.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863
Pierre Pfistere21c5282016-09-21 08:04:59 +0100864 if (msg.state.index >= VHOST_VRING_MAX_N)
865 {
866 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
867 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
868 goto close_socket;
869 }
870
871 if (msg.size < sizeof (msg.addr))
872 {
873 DBG_SOCK ("vhost message is too short (%d < %d)",
874 msg.size, sizeof (msg.addr));
875 goto close_socket;
876 }
877
Damjan Marion00a9dca2016-08-17 17:05:46 +0200878 vui->vrings[msg.state.index].desc = (vring_desc_t *)
879 map_user_mem (vui, msg.addr.desc_user_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 vui->vrings[msg.state.index].used = (vring_used_t *)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200881 map_user_mem (vui, msg.addr.used_user_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882 vui->vrings[msg.state.index].avail = (vring_avail_t *)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200883 map_user_mem (vui, msg.addr.avail_user_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884
885 if ((vui->vrings[msg.state.index].desc == NULL) ||
Damjan Marion00a9dca2016-08-17 17:05:46 +0200886 (vui->vrings[msg.state.index].used == NULL) ||
887 (vui->vrings[msg.state.index].avail == NULL))
888 {
889 DBG_SOCK ("failed to map user memory for hw_if_index %d",
890 vui->hw_if_index);
891 goto close_socket;
892 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100894 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
Yoann Desmouceauxfe2da0e2016-03-08 14:54:28 +0100895 vui->vrings[msg.state.index].log_used =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200896 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100897
898 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200899 the ring is initialized in an enabled state. */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200900 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
901 {
902 vui->vrings[msg.state.index].enabled = 1;
903 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100904
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905 vui->vrings[msg.state.index].last_used_idx =
Damjan Marion10eb1ea2016-10-13 10:02:19 +0200906 vui->vrings[msg.state.index].last_avail_idx =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200907 vui->vrings[msg.state.index].used->idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908
Stevenf3b53642017-05-01 14:03:02 -0700909 /* tell driver that we don't want interrupts */
910 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911 break;
912
913 case VHOST_USER_SET_OWNER:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200914 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700915 break;
916
917 case VHOST_USER_RESET_OWNER:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200918 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919 break;
920
921 case VHOST_USER_SET_VRING_CALL:
Steven388e51a2017-06-01 12:49:23 -0700922 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL %d",
Damjan Marion00a9dca2016-08-17 17:05:46 +0200923 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924
925 q = (u8) (msg.u64 & 0xFF);
926
Pierre Pfistere21c5282016-09-21 08:04:59 +0100927 /* if there is old fd, delete and close it */
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000928 if (vui->vrings[q].callfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100929 {
Damjan Marion56dd5432017-09-08 19:52:02 +0200930 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
Pierre Pfistere21c5282016-09-21 08:04:59 +0100931 vui->vrings[q].callfd_idx);
Damjan Marion56dd5432017-09-08 19:52:02 +0200932 clib_file_del (&file_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000933 vui->vrings[q].callfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100934 }
935
Steven49a04b92017-07-29 08:56:08 -0700936 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200937 {
938 if (number_of_fds != 1)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200939 {
Pierre Pfistere21c5282016-09-21 08:04:59 +0100940 DBG_SOCK ("More than one fd received !");
941 goto close_socket;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200942 }
Pierre Pfistere21c5282016-09-21 08:04:59 +0100943
Damjan Marion00a9dca2016-08-17 17:05:46 +0200944 template.read_function = vhost_user_callfd_read_ready;
945 template.file_descriptor = fds[0];
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000946 template.private_data =
947 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
Damjan Marion56dd5432017-09-08 19:52:02 +0200948 vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200949 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950 else
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000951 vui->vrings[q].callfd_idx = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952 break;
953
954 case VHOST_USER_SET_VRING_KICK:
Steven388e51a2017-06-01 12:49:23 -0700955 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK %d",
Damjan Marion00a9dca2016-08-17 17:05:46 +0200956 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957
958 q = (u8) (msg.u64 & 0xFF);
959
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000960 if (vui->vrings[q].kickfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100961 {
Damjan Marion56dd5432017-09-08 19:52:02 +0200962 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000963 vui->vrings[q].kickfd_idx);
Damjan Marion56dd5432017-09-08 19:52:02 +0200964 clib_file_del (&file_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000965 vui->vrings[q].kickfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100966 }
967
Steven49a04b92017-07-29 08:56:08 -0700968 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200969 {
970 if (number_of_fds != 1)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100971 {
972 DBG_SOCK ("More than one fd received !");
973 goto close_socket;
974 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975
Pierre Pfistere21c5282016-09-21 08:04:59 +0100976 template.read_function = vhost_user_kickfd_read_ready;
977 template.file_descriptor = fds[0];
978 template.private_data =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000979 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
980 q;
Damjan Marion56dd5432017-09-08 19:52:02 +0200981 vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200982 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983 else
Pierre Pfistere21c5282016-09-21 08:04:59 +0100984 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000985 //When no kickfd is set, the queue is initialized as started
986 vui->vrings[q].kickfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100987 vui->vrings[q].started = 1;
988 }
989
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990 break;
991
992 case VHOST_USER_SET_VRING_ERR:
Steven388e51a2017-06-01 12:49:23 -0700993 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR %d",
Damjan Marion00a9dca2016-08-17 17:05:46 +0200994 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995
996 q = (u8) (msg.u64 & 0xFF);
997
Pierre Pfistere21c5282016-09-21 08:04:59 +0100998 if (vui->vrings[q].errfd != -1)
999 close (vui->vrings[q].errfd);
1000
Steven49a04b92017-07-29 08:56:08 -07001001 if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001002 {
1003 if (number_of_fds != 1)
1004 goto close_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005
Pierre Pfistere21c5282016-09-21 08:04:59 +01001006 vui->vrings[q].errfd = fds[0];
Damjan Marion00a9dca2016-08-17 17:05:46 +02001007 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008 else
Pierre Pfistere21c5282016-09-21 08:04:59 +01001009 vui->vrings[q].errfd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011 break;
1012
1013 case VHOST_USER_SET_VRING_BASE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001014 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1015 vui->hw_if_index, msg.state.index, msg.state.num);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016
1017 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
1018 break;
1019
1020 case VHOST_USER_GET_VRING_BASE:
Pierre Pfistere21c5282016-09-21 08:04:59 +01001021 if (msg.state.index >= VHOST_VRING_MAX_N)
1022 {
1023 DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
1024 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1025 goto close_socket;
1026 }
1027
Stevenf6dae052017-03-09 23:49:32 -08001028 /*
1029 * Copy last_avail_idx from the vring before closing it because
1030 * closing the vring also initializes the vring last_avail_idx
1031 */
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001032 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033 msg.flags |= 4;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001034 msg.size = sizeof (msg.state);
Stevenf6dae052017-03-09 23:49:32 -08001035
1036 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
1037 vhost_user_vring_close (vui, msg.state.index);
Steven388e51a2017-06-01 12:49:23 -07001038 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1039 vui->hw_if_index, msg.state.index, msg.state.num);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040 break;
1041
1042 case VHOST_USER_NONE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001043 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044
1045 break;
1046
1047 case VHOST_USER_SET_LOG_BASE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001048 {
1049 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
Damjan Marion00a9dca2016-08-17 17:05:46 +02001051 if (msg.size != sizeof (msg.log))
1052 {
1053 DBG_SOCK
1054 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
1055 msg.size, sizeof (msg.log));
1056 goto close_socket;
1057 }
1058
1059 if (!
1060 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
1061 {
1062 DBG_SOCK
1063 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1064 goto close_socket;
1065 }
1066
1067 fd = fds[0];
1068 /* align size to 2M page */
1069 long page_sz = get_huge_page_size (fd);
1070 ssize_t map_sz =
Pierre Pfisterbed54892017-04-20 15:34:00 +02001071 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001072
1073 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1074 MAP_SHARED, fd, 0);
1075
1076 DBG_SOCK
1077 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
1078 map_sz, msg.log.offset, fd, vui->log_base_addr);
1079
1080 if (vui->log_base_addr == MAP_FAILED)
1081 {
1082 clib_warning ("failed to map memory. errno is %d", errno);
1083 goto close_socket;
1084 }
1085
1086 vui->log_base_addr += msg.log.offset;
1087 vui->log_size = msg.log.size;
1088
1089 msg.flags |= 4;
1090 msg.size = sizeof (msg.u64);
1091
1092 break;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001093 }
1094
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 case VHOST_USER_SET_LOG_FD:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001096 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097
1098 break;
1099
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001100 case VHOST_USER_GET_PROTOCOL_FEATURES:
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001101 msg.flags |= 4;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001102 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1103 (1 << VHOST_USER_PROTOCOL_F_MQ);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001104 msg.size = sizeof (msg.u64);
Steven388e51a2017-06-01 12:49:23 -07001105 DBG_SOCK
1106 ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - reply 0x%016llx",
1107 vui->hw_if_index, msg.u64);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001108 break;
1109
1110 case VHOST_USER_SET_PROTOCOL_FEATURES:
Steven388e51a2017-06-01 12:49:23 -07001111 DBG_SOCK
1112 ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%016llx",
1113 vui->hw_if_index, msg.u64);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001114
1115 vui->protocol_features = msg.u64;
1116
1117 break;
1118
Pierre Pfistere21c5282016-09-21 08:04:59 +01001119 case VHOST_USER_GET_QUEUE_NUM:
Pierre Pfistere21c5282016-09-21 08:04:59 +01001120 msg.flags |= 4;
1121 msg.u64 = VHOST_VRING_MAX_N;
1122 msg.size = sizeof (msg.u64);
Steven388e51a2017-06-01 12:49:23 -07001123 DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d",
1124 vui->hw_if_index, msg.u64);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001125 break;
1126
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001127 case VHOST_USER_SET_VRING_ENABLE:
Pierre Pfistere21c5282016-09-21 08:04:59 +01001128 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1129 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1130 msg.state.index);
1131 if (msg.state.index >= VHOST_VRING_MAX_N)
1132 {
1133 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
1134 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1135 goto close_socket;
1136 }
1137
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001138 vui->vrings[msg.state.index].enabled = msg.state.num;
1139 break;
1140
Ed Warnickecb9cada2015-12-08 15:45:58 -07001141 default:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001142 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1143 msg.request);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144 goto close_socket;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001145 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147 /* if we need to reply */
1148 if (msg.flags & 4)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001149 {
1150 n =
1151 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001152 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
Pierre Pfistere21c5282016-09-21 08:04:59 +01001153 {
1154 DBG_SOCK ("could not send message response");
1155 goto close_socket;
1156 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02001157 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001158
Pierre Pfistere21c5282016-09-21 08:04:59 +01001159 vhost_user_update_iface_state (vui);
1160 vlib_worker_thread_barrier_release (vlib_get_main ());
Ed Warnickecb9cada2015-12-08 15:45:58 -07001161 return 0;
1162
1163close_socket:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001164 vhost_user_if_disconnect (vui);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001165 vhost_user_update_iface_state (vui);
1166 vlib_worker_thread_barrier_release (vlib_get_main ());
Ed Warnickecb9cada2015-12-08 15:45:58 -07001167 return 0;
1168}
1169
Damjan Marion00a9dca2016-08-17 17:05:46 +02001170static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02001171vhost_user_socket_error (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001172{
Pierre Pfistere21c5282016-09-21 08:04:59 +01001173 vlib_main_t *vm = vlib_get_main ();
Damjan Marion00a9dca2016-08-17 17:05:46 +02001174 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001175 vhost_user_intf_t *vui =
1176 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001177
Pierre Pfistere21c5282016-09-21 08:04:59 +01001178 DBG_SOCK ("socket error on if %d", vui->sw_if_index);
1179 vlib_worker_thread_barrier_sync (vm);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001180 vhost_user_if_disconnect (vui);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001181 vhost_user_rx_thread_placement ();
1182 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183 return 0;
1184}
1185
Damjan Marion00a9dca2016-08-17 17:05:46 +02001186static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02001187vhost_user_socksvr_accept_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188{
1189 int client_fd, client_len;
1190 struct sockaddr_un client;
Damjan Marion56dd5432017-09-08 19:52:02 +02001191 clib_file_t template = { 0 };
Damjan Marion00a9dca2016-08-17 17:05:46 +02001192 vhost_user_main_t *vum = &vhost_user_main;
1193 vhost_user_intf_t *vui;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001194
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001195 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196
Damjan Marion00a9dca2016-08-17 17:05:46 +02001197 client_len = sizeof (client);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198 client_fd = accept (uf->file_descriptor,
Damjan Marion00a9dca2016-08-17 17:05:46 +02001199 (struct sockaddr *) &client,
1200 (socklen_t *) & client_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001201
1202 if (client_fd < 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001203 return clib_error_return_unix (0, "accept");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001204
Pierre Pfistere21c5282016-09-21 08:04:59 +01001205 DBG_SOCK ("New client socket for vhost interface %d", vui->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206 template.read_function = vhost_user_socket_read;
1207 template.error_function = vhost_user_socket_error;
1208 template.file_descriptor = client_fd;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001209 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
Damjan Marion56dd5432017-09-08 19:52:02 +02001210 vui->clib_file_index = clib_file_add (&file_main, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211 return 0;
1212}
1213
1214static clib_error_t *
1215vhost_user_init (vlib_main_t * vm)
1216{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001217 clib_error_t *error;
1218 vhost_user_main_t *vum = &vhost_user_main;
1219 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001220
1221 error = vlib_call_init_function (vm, ip4_init);
1222 if (error)
1223 return error;
1224
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225 vum->coalesce_frames = 32;
1226 vum->coalesce_time = 1e-3;
1227
Pierre Pfistere21c5282016-09-21 08:04:59 +01001228 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001229
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001230 vhost_cpu_t *cpu;
1231 vec_foreach (cpu, vum->cpus)
1232 {
1233 /* This is actually not necessary as validate already zeroes it
1234 * Just keeping the loop here for later because I am lazy. */
1235 cpu->rx_buffers_len = 0;
1236 }
1237
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001238 vum->random = random_default_seed ();
1239
Steven5445f5f2017-04-25 16:16:00 -07001240 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1241
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 return 0;
1243}
1244
1245VLIB_INIT_FUNCTION (vhost_user_init);
1246
Damjan Marion00a9dca2016-08-17 17:05:46 +02001247static u8 *
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001248format_vhost_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001249{
1250 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1251 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001252 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
1253 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001254 vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001255 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
1256 t->device_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001257
Damjan Marion00a9dca2016-08-17 17:05:46 +02001258 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259
Christophe Fontained3c008d2017-10-02 18:10:54 +02001260 u32 indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001261
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001262 s = format (s, "%U %U queue %d\n", format_white_space, indent,
Pierre Pfistere21c5282016-09-21 08:04:59 +01001263 format_vnet_sw_interface_name, vnm, sw, t->qid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001264
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001265 s = format (s, "%U virtio flags:\n", format_white_space, indent);
1266#define _(n,i,st) \
1267 if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
1268 s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
1269 foreach_virtio_trace_flags
1270#undef _
1271 s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
1272 format_white_space, indent, t->first_desc_len);
1273
1274 s = format (s, "%U flags 0x%02x gso_type %u\n",
Damjan Marion00a9dca2016-08-17 17:05:46 +02001275 format_white_space, indent,
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001276 t->hdr.hdr.flags, t->hdr.hdr.gso_type);
1277
1278 if (vui->virtio_net_hdr_sz == 12)
1279 s = format (s, "%U num_buff %u",
1280 format_white_space, indent, t->hdr.num_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001281
1282 return s;
1283}
1284
Damjan Marion00a9dca2016-08-17 17:05:46 +02001285void
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001286vhost_user_rx_trace (vhost_trace_t * t,
1287 vhost_user_intf_t * vui, u16 qid,
1288 vlib_buffer_t * b, vhost_user_vring_t * txvq)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001289{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001290 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001291 u32 last_avail_idx = txvq->last_avail_idx;
Steven97878892017-08-29 09:23:26 -07001292 u32 desc_current = txvq->avail->ring[last_avail_idx & txvq->qsz_mask];
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001293 vring_desc_t *hdr_desc = 0;
1294 virtio_net_hdr_mrg_rxbuf_t *hdr;
1295 u32 hint = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001296
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001297 memset (t, 0, sizeof (*t));
1298 t->device_index = vui - vum->vhost_user_interfaces;
1299 t->qid = qid;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001300
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001301 hdr_desc = &txvq->desc[desc_current];
1302 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001303 {
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001304 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001305 /* Header is the first here */
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001306 hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
1307 }
1308 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1309 {
1310 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1311 }
1312 if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1313 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1314 {
1315 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1316 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001317
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001318 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001319
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001320 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
1321 {
1322 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
1323 }
1324 else
1325 {
1326 u32 len = vui->virtio_net_hdr_sz;
1327 memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001328 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001329}
1330
Damjan Marion00a9dca2016-08-17 17:05:46 +02001331static inline void
1332vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001333{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001334 vhost_user_main_t *vum = &vhost_user_main;
1335 u64 x = 1;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001336 int fd = UNIX_GET_FD (vq->callfd_idx);
Stevenf3b53642017-05-01 14:03:02 -07001337 int rv;
1338
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001339 rv = write (fd, &x, sizeof (x));
Stevenf3b53642017-05-01 14:03:02 -07001340 if (rv <= 0)
1341 {
1342 clib_unix_warning
1343 ("Error: Could not write to unix socket for callfd %d", fd);
1344 return;
1345 }
1346
Damjan Marion00a9dca2016-08-17 17:05:46 +02001347 vq->n_since_last_int = 0;
1348 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001349}
1350
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001351static_always_inline u32
1352vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1353 u16 copy_len, u32 * map_hint)
1354{
1355 void *src0, *src1, *src2, *src3;
1356 if (PREDICT_TRUE (copy_len >= 4))
1357 {
1358 if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
1359 return 1;
1360 if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
1361 return 1;
1362
1363 while (PREDICT_TRUE (copy_len >= 4))
1364 {
1365 src0 = src2;
1366 src1 = src3;
1367
1368 if (PREDICT_FALSE
1369 (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
1370 return 1;
1371 if (PREDICT_FALSE
1372 (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
1373 return 1;
1374
1375 CLIB_PREFETCH (src2, 64, LOAD);
1376 CLIB_PREFETCH (src3, 64, LOAD);
1377
1378 clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
1379 clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
1380 copy_len -= 2;
1381 cpy += 2;
1382 }
1383 }
1384 while (copy_len)
1385 {
1386 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
1387 return 1;
1388 clib_memcpy ((void *) cpy->dst, src0, cpy->len);
1389 copy_len -= 1;
1390 cpy += 1;
1391 }
1392 return 0;
1393}
1394
1395/**
1396 * Try to discard packets from the tx ring (VPP RX path).
1397 * Returns the number of discarded packets.
1398 */
1399u32
1400vhost_user_rx_discard_packet (vlib_main_t * vm,
1401 vhost_user_intf_t * vui,
1402 vhost_user_vring_t * txvq, u32 discard_max)
1403{
1404 /*
1405 * On the RX side, each packet corresponds to one descriptor
1406 * (it is the same whether it is a shallow descriptor, chained, or indirect).
1407 * Therefore, discarding a packet is like discarding a descriptor.
1408 */
1409 u32 discarded_packets = 0;
1410 u32 avail_idx = txvq->avail->idx;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001411 while (discarded_packets != discard_max)
1412 {
1413 if (avail_idx == txvq->last_avail_idx)
1414 goto out;
1415
1416 u16 desc_chain_head =
Steven97878892017-08-29 09:23:26 -07001417 txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001418 txvq->last_avail_idx++;
Steven97878892017-08-29 09:23:26 -07001419 txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id =
1420 desc_chain_head;
1421 txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001422 vhost_user_log_dirty_ring (vui, txvq,
Steven97878892017-08-29 09:23:26 -07001423 ring[txvq->last_used_idx & txvq->qsz_mask]);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001424 txvq->last_used_idx++;
1425 discarded_packets++;
1426 }
1427
1428out:
1429 CLIB_MEMORY_BARRIER ();
1430 txvq->used->idx = txvq->last_used_idx;
1431 vhost_user_log_dirty_ring (vui, txvq, idx);
1432 return discarded_packets;
1433}
1434
1435/*
1436 * In case of overflow, we need to rewind the array of allocated buffers.
1437 */
1438static void
1439vhost_user_input_rewind_buffers (vlib_main_t * vm,
1440 vhost_cpu_t * cpu, vlib_buffer_t * b_head)
1441{
1442 u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1443 vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
1444 b_current->current_length = 0;
1445 b_current->flags = 0;
1446 while (b_current != b_head)
1447 {
1448 cpu->rx_buffers_len++;
1449 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1450 b_current = vlib_get_buffer (vm, bi_current);
1451 b_current->current_length = 0;
1452 b_current->flags = 0;
1453 }
Steven95827e42017-05-18 21:22:00 -07001454 cpu->rx_buffers_len++;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001455}
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001456
Damjan Marion00a9dca2016-08-17 17:05:46 +02001457static u32
1458vhost_user_if_input (vlib_main_t * vm,
1459 vhost_user_main_t * vum,
Pierre Pfistere21c5282016-09-21 08:04:59 +01001460 vhost_user_intf_t * vui,
Stevenf3b53642017-05-01 14:03:02 -07001461 u16 qid, vlib_node_runtime_t * node,
1462 vnet_hw_interface_rx_mode mode)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001463{
Pierre Pfistere21c5282016-09-21 08:04:59 +01001464 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001465 u16 n_rx_packets = 0;
1466 u32 n_rx_bytes = 0;
1467 u16 n_left;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001468 u32 n_left_to_next, *to_next;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001469 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1470 u32 n_trace = vlib_get_trace_count (vm, node);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001471 u32 map_hint = 0;
Damjan Marion586afd72017-04-05 19:18:20 +02001472 u16 thread_index = vlib_get_thread_index ();
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001473 u16 copy_len = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001474
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001475 {
1476 /* do we have pending interrupts ? */
1477 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1478 f64 now = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001479
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001480 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1481 vhost_user_send_call (vm, txvq);
1482
1483 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1484 vhost_user_send_call (vm, rxvq);
1485 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001486
Stevenf3b53642017-05-01 14:03:02 -07001487 /*
1488 * For adaptive mode, it is optimized to reduce interrupts.
1489 * If the scheduler switches the input node to polling due
1490 * to burst of traffic, we tell the driver no interrupt.
1491 * When the traffic subsides, the scheduler switches the node back to
1492 * interrupt mode. We must tell the driver we want interrupt.
1493 */
1494 if (PREDICT_FALSE (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
1495 {
1496 if ((node->flags &
1497 VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) ||
1498 !(node->flags &
1499 VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
1500 /* Tell driver we want notification */
1501 txvq->used->flags = 0;
1502 else
1503 /* Tell driver we don't want notification */
1504 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
1505 }
1506
Damjan Marion00a9dca2016-08-17 17:05:46 +02001507 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001508 return 0;
1509
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001510 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1511
Ed Warnickecb9cada2015-12-08 15:45:58 -07001512 /* nothing to do */
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001513 if (PREDICT_FALSE (n_left == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001514 return 0;
1515
Pierre Pfistere21c5282016-09-21 08:04:59 +01001516 if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001517 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01001518 /*
1519 * Discard input packet if interface is admin down or vring is not
1520 * enabled.
1521 * "For example, for a networking device, in the disabled state
1522 * client must not supply any new RX packets, but must process
1523 * and discard any TX packets."
1524 */
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001525 vhost_user_rx_discard_packet (vm, vui, txvq,
1526 VHOST_USER_DOWN_DISCARD_COUNT);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001527 return 0;
1528 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001529
Steven97878892017-08-29 09:23:26 -07001530 if (PREDICT_FALSE (n_left == (txvq->qsz_mask + 1)))
Pierre Pfistere21c5282016-09-21 08:04:59 +01001531 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001532 /*
1533 * Informational error logging when VPP is not
1534 * receiving packets fast enough.
1535 */
Pierre Pfistere21c5282016-09-21 08:04:59 +01001536 vlib_error_count (vm, node->node_index,
1537 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1538 }
1539
Pierre Pfister328e99b2016-02-12 13:18:42 +00001540 if (n_left > VLIB_FRAME_SIZE)
1541 n_left = VLIB_FRAME_SIZE;
1542
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001543 /*
1544 * For small packets (<2kB), we will not need more than one vlib buffer
1545 * per packet. In case packets are bigger, we will just yeld at some point
1546 * in the loop and come back later. This is not an issue as for big packet,
1547 * processing cost really comes from the memory copy.
Pierre Pfister56a86842017-10-05 14:24:05 +02001548 * The assumption is that big packets will fit in 40 buffers.
Pierre Pfister328e99b2016-02-12 13:18:42 +00001549 */
Pierre Pfister56a86842017-10-05 14:24:05 +02001550 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len < n_left + 1 ||
1551 vum->cpus[thread_index].rx_buffers_len < 40))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001552 {
Damjan Marion586afd72017-04-05 19:18:20 +02001553 u32 curr_len = vum->cpus[thread_index].rx_buffers_len;
1554 vum->cpus[thread_index].rx_buffers_len +=
Damjan Marion00a9dca2016-08-17 17:05:46 +02001555 vlib_buffer_alloc_from_free_list (vm,
Damjan Marion586afd72017-04-05 19:18:20 +02001556 vum->cpus[thread_index].rx_buffers +
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001557 curr_len,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001558 VHOST_USER_RX_BUFFERS_N - curr_len,
Damjan Marion00a9dca2016-08-17 17:05:46 +02001559 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001560
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001561 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001562 (vum->cpus[thread_index].rx_buffers_len <
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001563 VHOST_USER_RX_BUFFER_STARVATION))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001564 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001565 /* In case of buffer starvation, discard some packets from the queue
1566 * and log the event.
1567 * We keep doing best effort for the remaining packets. */
Damjan Marion586afd72017-04-05 19:18:20 +02001568 u32 flush = (n_left + 1 > vum->cpus[thread_index].rx_buffers_len) ?
1569 n_left + 1 - vum->cpus[thread_index].rx_buffers_len : 1;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001570 flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1571
1572 n_left -= flush;
1573 vlib_increment_simple_counter (vnet_main.
1574 interface_main.sw_if_counters +
1575 VNET_INTERFACE_COUNTER_DROP,
Damjan Marion586afd72017-04-05 19:18:20 +02001576 vlib_get_thread_index (),
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001577 vui->sw_if_index, flush);
1578
1579 vlib_error_count (vm, vhost_user_input_node.index,
1580 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001581 }
1582 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001583
Damjan Marion00a9dca2016-08-17 17:05:46 +02001584 while (n_left > 0)
1585 {
1586 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001587
Damjan Marion00a9dca2016-08-17 17:05:46 +02001588 while (n_left > 0 && n_left_to_next > 0)
1589 {
1590 vlib_buffer_t *b_head, *b_current;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001591 u32 bi_current;
1592 u16 desc_current;
1593 u32 desc_data_offset;
1594 vring_desc_t *desc_table = txvq->desc;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001595
Damjan Marion586afd72017-04-05 19:18:20 +02001596 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len <= 1))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001597 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001598 /* Not enough rx_buffers
1599 * Note: We yeld on 1 so we don't need to do an additional
1600 * check for the next buffer prefetch.
1601 */
1602 n_left = 0;
1603 break;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001604 }
1605
Steven97878892017-08-29 09:23:26 -07001606 desc_current =
1607 txvq->avail->ring[txvq->last_avail_idx & txvq->qsz_mask];
Damjan Marion586afd72017-04-05 19:18:20 +02001608 vum->cpus[thread_index].rx_buffers_len--;
1609 bi_current = (vum->cpus[thread_index].rx_buffers)
1610 [vum->cpus[thread_index].rx_buffers_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001611 b_head = b_current = vlib_get_buffer (vm, bi_current);
1612 to_next[0] = bi_current; //We do that now so we can forget about bi_current
1613 to_next++;
1614 n_left_to_next--;
1615
1616 vlib_prefetch_buffer_with_index (vm,
Damjan Marion586afd72017-04-05 19:18:20 +02001617 (vum->
1618 cpus[thread_index].rx_buffers)
1619 [vum->cpus[thread_index].
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001620 rx_buffers_len - 1], LOAD);
1621
1622 /* Just preset the used descriptor id and length for later */
Steven97878892017-08-29 09:23:26 -07001623 txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].id =
1624 desc_current;
1625 txvq->used->ring[txvq->last_used_idx & txvq->qsz_mask].len = 0;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001626 vhost_user_log_dirty_ring (vui, txvq,
Steven97878892017-08-29 09:23:26 -07001627 ring[txvq->last_used_idx &
1628 txvq->qsz_mask]);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001629
1630 /* The buffer should already be initialized */
1631 b_head->total_length_not_including_first_buffer = 0;
1632 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1633
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001634 if (PREDICT_FALSE (n_trace))
1635 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001636 //TODO: next_index is not exactly known at that point
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001637 vlib_trace_buffer (vm, node, next_index, b_head,
1638 /* follow_chain */ 0);
1639 vhost_trace_t *t0 =
1640 vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1641 vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1642 n_trace--;
1643 vlib_set_trace_count (vm, node, n_trace);
1644 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02001645
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001646 /* This depends on the setup but is very consistent
1647 * So I think the CPU branch predictor will make a pretty good job
1648 * at optimizing the decision. */
1649 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1650 {
1651 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1652 &map_hint);
1653 desc_current = 0;
1654 if (PREDICT_FALSE (desc_table == 0))
1655 {
Steven95827e42017-05-18 21:22:00 -07001656 vlib_error_count (vm, node->node_index,
1657 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001658 goto out;
1659 }
1660 }
1661
Damjan Marion00a9dca2016-08-17 17:05:46 +02001662 if (PREDICT_TRUE (vui->is_any_layout) ||
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001663 (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001664 {
1665 /* ANYLAYOUT or single buffer */
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001666 desc_data_offset = vui->virtio_net_hdr_sz;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001667 }
1668 else
1669 {
1670 /* CSR case without ANYLAYOUT, skip 1st buffer */
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001671 desc_data_offset = desc_table[desc_current].len;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001672 }
1673
Damjan Marion00a9dca2016-08-17 17:05:46 +02001674 while (1)
1675 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001676 /* Get more input if necessary. Or end of packet. */
1677 if (desc_data_offset == desc_table[desc_current].len)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001678 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001679 if (PREDICT_FALSE (desc_table[desc_current].flags &
1680 VIRTQ_DESC_F_NEXT))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001681 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001682 desc_current = desc_table[desc_current].next;
1683 desc_data_offset = 0;
1684 }
1685 else
1686 {
1687 goto out;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001688 }
1689 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001690
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001691 /* Get more output if necessary. Or end of packet. */
1692 if (PREDICT_FALSE
1693 (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1694 {
1695 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001696 (vum->cpus[thread_index].rx_buffers_len == 0))
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001697 {
Steven62411e72017-02-03 09:30:37 -08001698 /* Cancel speculation */
1699 to_next--;
1700 n_left_to_next++;
1701
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001702 /*
1703 * Checking if there are some left buffers.
1704 * If not, just rewind the used buffers and stop.
1705 * Note: Scheduled copies are not cancelled. This is
1706 * not an issue as they would still be valid. Useless,
1707 * but valid.
1708 */
1709 vhost_user_input_rewind_buffers (vm,
Damjan Marion586afd72017-04-05 19:18:20 +02001710 &vum->cpus
1711 [thread_index],
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001712 b_head);
1713 n_left = 0;
1714 goto stop;
1715 }
1716
1717 /* Get next output */
Damjan Marion586afd72017-04-05 19:18:20 +02001718 vum->cpus[thread_index].rx_buffers_len--;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001719 u32 bi_next =
Damjan Marion586afd72017-04-05 19:18:20 +02001720 (vum->cpus[thread_index].rx_buffers)[vum->cpus
1721 [thread_index].rx_buffers_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001722 b_current->next_buffer = bi_next;
1723 b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1724 bi_current = bi_next;
1725 b_current = vlib_get_buffer (vm, bi_current);
1726 }
1727
1728 /* Prepare a copy order executed later for the data */
Damjan Marion586afd72017-04-05 19:18:20 +02001729 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001730 copy_len++;
1731 u32 desc_data_l =
1732 desc_table[desc_current].len - desc_data_offset;
1733 cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1734 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
Steven025d4152017-05-16 21:26:13 -07001735 cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
1736 b_current->current_length);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001737 cpy->src = desc_table[desc_current].addr + desc_data_offset;
1738
1739 desc_data_offset += cpy->len;
1740
1741 b_current->current_length += cpy->len;
1742 b_head->total_length_not_including_first_buffer += cpy->len;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001743 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001744
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001745 out:
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001746 CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1747
1748 n_rx_bytes += b_head->total_length_not_including_first_buffer;
1749 n_rx_packets++;
1750
1751 b_head->total_length_not_including_first_buffer -=
1752 b_head->current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001753
Damjan Marion00a9dca2016-08-17 17:05:46 +02001754 /* consume the descriptor and return it as used */
1755 txvq->last_avail_idx++;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001756 txvq->last_used_idx++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001757
Damjan Marion00a9dca2016-08-17 17:05:46 +02001758 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001759
Damjan Marion00a9dca2016-08-17 17:05:46 +02001760 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1761 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001762 b_head->error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001763
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001764 {
1765 u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001766
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001767 /* redirect if feature path enabled */
1768 vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
Damjan Marion35af9e52017-03-06 12:02:50 +01001769 b_head);
Damjan Marion22311502016-10-28 20:30:15 +02001770
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001771 u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1772 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1773 to_next, n_left_to_next,
1774 bi, next0);
1775 }
Damjan Marion22311502016-10-28 20:30:15 +02001776
Damjan Marion00a9dca2016-08-17 17:05:46 +02001777 n_left--;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001778
1779 /*
1780 * Although separating memory copies from virtio ring parsing
1781 * is beneficial, we can offer to perform the copies from time
1782 * to time in order to free some space in the ring.
1783 */
1784 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
Pierre Pfistere21c5282016-09-21 08:04:59 +01001785 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001786 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001787 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001788 copy_len, &map_hint)))
1789 {
Steven95827e42017-05-18 21:22:00 -07001790 vlib_error_count (vm, node->node_index,
1791 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001792 }
1793 copy_len = 0;
1794
1795 /* give buffers back to driver */
1796 CLIB_MEMORY_BARRIER ();
1797 txvq->used->idx = txvq->last_used_idx;
1798 vhost_user_log_dirty_ring (vui, txvq, idx);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001799 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02001800 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001801 stop:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001802 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001803 }
1804
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001805 /* Do the memory copies */
1806 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001807 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001808 copy_len, &map_hint)))
1809 {
Steven95827e42017-05-18 21:22:00 -07001810 vlib_error_count (vm, node->node_index,
1811 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001812 }
Pierre Pfister328e99b2016-02-12 13:18:42 +00001813
1814 /* give buffers back to driver */
Damjan Marion00a9dca2016-08-17 17:05:46 +02001815 CLIB_MEMORY_BARRIER ();
Pierre Pfister328e99b2016-02-12 13:18:42 +00001816 txvq->used->idx = txvq->last_used_idx;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001817 vhost_user_log_dirty_ring (vui, txvq, idx);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001818
Ed Warnickecb9cada2015-12-08 15:45:58 -07001819 /* interrupt (call) handling */
Steven7312cc72017-03-15 21:18:55 -07001820 if ((txvq->callfd_idx != ~0) &&
1821 !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001822 {
1823 txvq->n_since_last_int += n_rx_packets;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001824
Damjan Marion00a9dca2016-08-17 17:05:46 +02001825 if (txvq->n_since_last_int > vum->coalesce_frames)
1826 vhost_user_send_call (vm, txvq);
1827 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001828
1829 /* increase rx counters */
1830 vlib_increment_combined_counter
Damjan Marion00a9dca2016-08-17 17:05:46 +02001831 (vnet_main.interface_main.combined_sw_if_counters
1832 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +02001833 vlib_get_thread_index (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001834
Damjan Marion586afd72017-04-05 19:18:20 +02001835 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
Damjan Marionb3bb1012017-02-28 21:55:28 +01001836
Ed Warnickecb9cada2015-12-08 15:45:58 -07001837 return n_rx_packets;
1838}
1839
1840static uword
1841vhost_user_input (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02001842 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001843{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001844 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001845 uword n_rx_packets = 0;
Steven7312cc72017-03-15 21:18:55 -07001846 vhost_user_intf_t *vui;
Stevenf3b53642017-05-01 14:03:02 -07001847 vnet_device_input_runtime_t *rt =
1848 (vnet_device_input_runtime_t *) node->runtime_data;
1849 vnet_device_and_queue_t *dq;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001850
Stevenf3b53642017-05-01 14:03:02 -07001851 vec_foreach (dq, rt->devices_and_queues)
1852 {
1853 if (clib_smp_swap (&dq->interrupt_pending, 0) ||
1854 (node->state == VLIB_NODE_STATE_POLLING))
Steven7312cc72017-03-15 21:18:55 -07001855 {
Stevenf3b53642017-05-01 14:03:02 -07001856 vui =
1857 pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance);
1858 n_rx_packets = vhost_user_if_input (vm, vum, vui, dq->queue_id, node,
1859 dq->mode);
Steven7312cc72017-03-15 21:18:55 -07001860 }
Stevenf3b53642017-05-01 14:03:02 -07001861 }
Steven7312cc72017-03-15 21:18:55 -07001862
Ed Warnickecb9cada2015-12-08 15:45:58 -07001863 return n_rx_packets;
1864}
1865
Damjan Marion00a9dca2016-08-17 17:05:46 +02001866/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001867VLIB_REGISTER_NODE (vhost_user_input_node) = {
1868 .function = vhost_user_input,
1869 .type = VLIB_NODE_TYPE_INPUT,
1870 .name = "vhost-user-input",
Damjan Marion51327ac2016-11-09 11:59:42 +01001871 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001872
1873 /* Will be enabled if/when hardware is detected. */
1874 .state = VLIB_NODE_STATE_DISABLED,
1875
1876 .format_buffer = format_ethernet_header_with_length,
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001877 .format_trace = format_vhost_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001878
1879 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1880 .error_strings = vhost_user_input_func_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001881};
1882
Damjan Marion1c80e832016-05-11 23:07:18 +02001883VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001884/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +02001885
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001886
1887void
1888vhost_user_tx_trace (vhost_trace_t * t,
1889 vhost_user_intf_t * vui, u16 qid,
1890 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
1891{
1892 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001893 u32 last_avail_idx = rxvq->last_avail_idx;
Steven97878892017-08-29 09:23:26 -07001894 u32 desc_current = rxvq->avail->ring[last_avail_idx & rxvq->qsz_mask];
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001895 vring_desc_t *hdr_desc = 0;
1896 u32 hint = 0;
1897
1898 memset (t, 0, sizeof (*t));
1899 t->device_index = vui - vum->vhost_user_interfaces;
1900 t->qid = qid;
1901
1902 hdr_desc = &rxvq->desc[desc_current];
1903 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1904 {
1905 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001906 /* Header is the first here */
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001907 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
1908 }
1909 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1910 {
1911 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1912 }
1913 if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1914 !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1915 {
1916 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1917 }
1918
1919 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1920}
1921
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001922static_always_inline u32
1923vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1924 u16 copy_len, u32 * map_hint)
1925{
1926 void *dst0, *dst1, *dst2, *dst3;
1927 if (PREDICT_TRUE (copy_len >= 4))
1928 {
1929 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
1930 return 1;
1931 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
1932 return 1;
1933 while (PREDICT_TRUE (copy_len >= 4))
1934 {
1935 dst0 = dst2;
1936 dst1 = dst3;
1937
1938 if (PREDICT_FALSE
1939 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
1940 return 1;
1941 if (PREDICT_FALSE
1942 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
1943 return 1;
1944
1945 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
1946 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
1947
1948 clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
1949 clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
1950
1951 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
1952 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
1953 copy_len -= 2;
1954 cpy += 2;
1955 }
1956 }
1957 while (copy_len)
1958 {
1959 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
1960 return 1;
1961 clib_memcpy (dst0, (void *) cpy->src, cpy->len);
1962 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
1963 copy_len -= 1;
1964 cpy += 1;
1965 }
1966 return 0;
1967}
1968
1969
Ed Warnickecb9cada2015-12-08 15:45:58 -07001970static uword
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001971vhost_user_tx (vlib_main_t * vm,
1972 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001973{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001974 u32 *buffers = vlib_frame_args (frame);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001975 u32 n_left = frame->n_vectors;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001976 vhost_user_main_t *vum = &vhost_user_main;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001977 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
1978 vhost_user_intf_t *vui =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001979 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001980 u32 qid = ~0;
1981 vhost_user_vring_t *rxvq;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001982 u8 error;
Damjan Marion586afd72017-04-05 19:18:20 +02001983 u32 thread_index = vlib_get_thread_index ();
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001984 u32 map_hint = 0;
1985 u8 retry = 8;
1986 u16 copy_len;
1987 u16 tx_headers_len;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001988
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001989 if (PREDICT_FALSE (!vui->admin_up))
1990 {
1991 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
1992 goto done3;
1993 }
1994
1995 if (PREDICT_FALSE (!vui->is_up))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001996 {
1997 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001998 goto done3;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001999 }
Damjan Marion920ecc22016-01-12 18:34:24 +01002000
Pierre Pfistere21c5282016-09-21 08:04:59 +01002001 qid =
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002002 VHOST_VRING_IDX_RX (*vec_elt_at_index
Damjan Marion586afd72017-04-05 19:18:20 +02002003 (vui->per_cpu_tx_qid, vlib_get_thread_index ()));
Pierre Pfistere21c5282016-09-21 08:04:59 +01002004 rxvq = &vui->vrings[qid];
2005 if (PREDICT_FALSE (vui->use_tx_spinlock))
2006 vhost_user_vring_lock (vui, qid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002007
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002008retry:
2009 error = VHOST_USER_TX_FUNC_ERROR_NONE;
2010 tx_headers_len = 0;
2011 copy_len = 0;
2012 while (n_left > 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002013 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002014 vlib_buffer_t *b0, *current_b0;
2015 u16 desc_head, desc_index, desc_len;
2016 vring_desc_t *desc_table;
2017 uword buffer_map_addr;
2018 u32 buffer_len;
2019 u16 bytes_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002020
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002021 if (PREDICT_TRUE (n_left > 1))
2022 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2023
2024 b0 = vlib_get_buffer (vm, buffers[0]);
2025
2026 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002027 {
Damjan Marion586afd72017-04-05 19:18:20 +02002028 vum->cpus[thread_index].current_trace =
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002029 vlib_add_trace (vm, node, b0,
Damjan Marion586afd72017-04-05 19:18:20 +02002030 sizeof (*vum->cpus[thread_index].current_trace));
2031 vhost_user_tx_trace (vum->cpus[thread_index].current_trace,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002032 vui, qid / 2, b0, rxvq);
2033 }
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002034
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002035 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2036 {
2037 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2038 goto done;
2039 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002040
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002041 desc_table = rxvq->desc;
2042 desc_head = desc_index =
Steven97878892017-08-29 09:23:26 -07002043 rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002044
2045 /* Go deeper in case of indirect descriptor
2046 * I don't know of any driver providing indirect for RX. */
2047 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2048 {
2049 if (PREDICT_FALSE
2050 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002051 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002052 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002053 goto done;
2054 }
2055 if (PREDICT_FALSE
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002056 (!(desc_table =
2057 map_guest_mem (vui, rxvq->desc[desc_index].addr,
2058 &map_hint))))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002059 {
2060 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2061 goto done;
2062 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002063 desc_index = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002064 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002065
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002066 desc_len = vui->virtio_net_hdr_sz;
2067 buffer_map_addr = desc_table[desc_index].addr;
2068 buffer_len = desc_table[desc_index].len;
2069
2070 {
2071 // Get a header from the header array
2072 virtio_net_hdr_mrg_rxbuf_t *hdr =
Damjan Marion586afd72017-04-05 19:18:20 +02002073 &vum->cpus[thread_index].tx_headers[tx_headers_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002074 tx_headers_len++;
2075 hdr->hdr.flags = 0;
2076 hdr->hdr.gso_type = 0;
2077 hdr->num_buffers = 1; //This is local, no need to check
2078
2079 // Prepare a copy order executed later for the header
Damjan Marion586afd72017-04-05 19:18:20 +02002080 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002081 copy_len++;
2082 cpy->len = vui->virtio_net_hdr_sz;
2083 cpy->dst = buffer_map_addr;
2084 cpy->src = (uword) hdr;
2085 }
2086
2087 buffer_map_addr += vui->virtio_net_hdr_sz;
2088 buffer_len -= vui->virtio_net_hdr_sz;
2089 bytes_left = b0->current_length;
2090 current_b0 = b0;
2091 while (1)
2092 {
2093 if (buffer_len == 0)
2094 { //Get new output
2095 if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2096 {
2097 //Next one is chained
2098 desc_index = desc_table[desc_index].next;
2099 buffer_map_addr = desc_table[desc_index].addr;
2100 buffer_len = desc_table[desc_index].len;
2101 }
2102 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2103 {
2104 virtio_net_hdr_mrg_rxbuf_t *hdr =
Damjan Marion586afd72017-04-05 19:18:20 +02002105 &vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002106
2107 //Move from available to used buffer
Steven97878892017-08-29 09:23:26 -07002108 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id =
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002109 desc_head;
Steven97878892017-08-29 09:23:26 -07002110 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len =
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002111 desc_len;
2112 vhost_user_log_dirty_ring (vui, rxvq,
2113 ring[rxvq->last_used_idx &
Steven97878892017-08-29 09:23:26 -07002114 rxvq->qsz_mask]);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002115
2116 rxvq->last_avail_idx++;
2117 rxvq->last_used_idx++;
2118 hdr->num_buffers++;
2119 desc_len = 0;
2120
2121 if (PREDICT_FALSE
2122 (rxvq->last_avail_idx == rxvq->avail->idx))
2123 {
2124 //Dequeue queued descriptors for this packet
2125 rxvq->last_used_idx -= hdr->num_buffers - 1;
2126 rxvq->last_avail_idx -= hdr->num_buffers - 1;
2127 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2128 goto done;
2129 }
2130
2131 desc_table = rxvq->desc;
2132 desc_head = desc_index =
Steven97878892017-08-29 09:23:26 -07002133 rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002134 if (PREDICT_FALSE
2135 (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2136 {
2137 //It is seriously unlikely that a driver will put indirect descriptor
2138 //after non-indirect descriptor.
2139 if (PREDICT_FALSE
2140 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2141 {
2142 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2143 goto done;
2144 }
2145 if (PREDICT_FALSE
2146 (!(desc_table =
2147 map_guest_mem (vui,
2148 rxvq->desc[desc_index].addr,
2149 &map_hint))))
2150 {
2151 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2152 goto done;
2153 }
2154 desc_index = 0;
2155 }
2156 buffer_map_addr = desc_table[desc_index].addr;
2157 buffer_len = desc_table[desc_index].len;
2158 }
2159 else
2160 {
2161 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2162 goto done;
2163 }
2164 }
2165
2166 {
Damjan Marion586afd72017-04-05 19:18:20 +02002167 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002168 copy_len++;
2169 cpy->len = bytes_left;
2170 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2171 cpy->dst = buffer_map_addr;
2172 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2173 current_b0->current_length - bytes_left;
2174
2175 bytes_left -= cpy->len;
2176 buffer_len -= cpy->len;
2177 buffer_map_addr += cpy->len;
2178 desc_len += cpy->len;
2179
Pierre Pfister14ac8012016-12-08 07:58:47 +00002180 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002181 }
2182
2183 // Check if vlib buffer has more data. If not, get more or break.
2184 if (PREDICT_TRUE (!bytes_left))
2185 {
2186 if (PREDICT_FALSE
2187 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2188 {
2189 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2190 bytes_left = current_b0->current_length;
2191 }
2192 else
2193 {
2194 //End of packet
2195 break;
2196 }
2197 }
2198 }
2199
2200 //Move from available to used ring
Steven97878892017-08-29 09:23:26 -07002201 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = desc_head;
2202 rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = desc_len;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002203 vhost_user_log_dirty_ring (vui, rxvq,
Steven97878892017-08-29 09:23:26 -07002204 ring[rxvq->last_used_idx & rxvq->qsz_mask]);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002205 rxvq->last_avail_idx++;
2206 rxvq->last_used_idx++;
2207
2208 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2209 {
Damjan Marion586afd72017-04-05 19:18:20 +02002210 vum->cpus[thread_index].current_trace->hdr =
2211 vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002212 }
2213
2214 n_left--; //At the end for error counting when 'goto done' is invoked
2215 buffers++;
2216 }
2217
2218done:
2219 //Do the memory copies
2220 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02002221 (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002222 copy_len, &map_hint)))
2223 {
Steven95827e42017-05-18 21:22:00 -07002224 vlib_error_count (vm, node->node_index,
2225 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002226 }
2227
2228 CLIB_MEMORY_BARRIER ();
2229 rxvq->used->idx = rxvq->last_used_idx;
2230 vhost_user_log_dirty_ring (vui, rxvq, idx);
2231
2232 /*
2233 * When n_left is set, error is always set to something too.
2234 * In case error is due to lack of remaining buffers, we go back up and
2235 * retry.
2236 * The idea is that it is better to waste some time on packets
2237 * that have been processed already than dropping them and get
2238 * more fresh packets with a good likelyhood that they will be dropped too.
2239 * This technique also gives more time to VM driver to pick-up packets.
2240 * In case the traffic flows from physical to virtual interfaces, this
2241 * technique will end-up leveraging the physical NIC buffer in order to
2242 * absorb the VM's CPU jitter.
2243 */
2244 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2245 {
2246 retry--;
2247 goto retry;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002248 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002249
Ed Warnickecb9cada2015-12-08 15:45:58 -07002250 /* interrupt (call) handling */
Steven7312cc72017-03-15 21:18:55 -07002251 if ((rxvq->callfd_idx != ~0) &&
2252 !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
Damjan Marion00a9dca2016-08-17 17:05:46 +02002253 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002254 rxvq->n_since_last_int += frame->n_vectors - n_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002255
Damjan Marion00a9dca2016-08-17 17:05:46 +02002256 if (rxvq->n_since_last_int > vum->coalesce_frames)
2257 vhost_user_send_call (vm, rxvq);
2258 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002259
Pierre Pfistere21c5282016-09-21 08:04:59 +01002260 vhost_user_vring_unlock (vui, qid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002261
Pierre Pfistere21c5282016-09-21 08:04:59 +01002262done3:
Damjan Marion00a9dca2016-08-17 17:05:46 +02002263 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2264 {
2265 vlib_error_count (vm, node->node_index, error, n_left);
2266 vlib_increment_simple_counter
2267 (vnet_main.interface_main.sw_if_counters
2268 + VNET_INTERFACE_COUNTER_DROP,
Damjan Marion586afd72017-04-05 19:18:20 +02002269 vlib_get_thread_index (), vui->sw_if_index, n_left);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002270 }
Pierre Pfister328e99b2016-02-12 13:18:42 +00002271
Ed Warnickecb9cada2015-12-08 15:45:58 -07002272 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2273 return frame->n_vectors;
2274}
2275
Stevenf3b53642017-05-01 14:03:02 -07002276static uword
2277vhost_user_send_interrupt_process (vlib_main_t * vm,
2278 vlib_node_runtime_t * rt, vlib_frame_t * f)
2279{
2280 vhost_user_intf_t *vui;
2281 f64 timeout = 3153600000.0 /* 100 years */ ;
2282 uword event_type, *event_data = 0;
2283 vhost_user_main_t *vum = &vhost_user_main;
2284 u16 *queue;
2285 f64 now, poll_time_remaining;
2286 f64 next_timeout;
2287 u8 stop_timer = 0;
2288
2289 while (1)
2290 {
2291 poll_time_remaining =
2292 vlib_process_wait_for_event_or_clock (vm, timeout);
2293 event_type = vlib_process_get_events (vm, &event_data);
2294 vec_reset_length (event_data);
2295
2296 /*
2297 * Use the remaining timeout if it is less than coalesce time to avoid
2298 * resetting the existing timer in the middle of expiration
2299 */
2300 timeout = poll_time_remaining;
2301 if (vlib_process_suspend_time_is_zero (timeout) ||
2302 (timeout > vum->coalesce_time))
2303 timeout = vum->coalesce_time;
2304
2305 now = vlib_time_now (vm);
2306 switch (event_type)
2307 {
2308 case VHOST_USER_EVENT_STOP_TIMER:
2309 stop_timer = 1;
2310 break;
2311
2312 case VHOST_USER_EVENT_START_TIMER:
2313 stop_timer = 0;
2314 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
2315 break;
2316 /* fall through */
2317
2318 case ~0:
2319 /* *INDENT-OFF* */
2320 pool_foreach (vui, vum->vhost_user_interfaces, {
2321 next_timeout = timeout;
2322 vec_foreach (queue, vui->rx_queues)
2323 {
2324 vhost_user_vring_t *rxvq =
2325 &vui->vrings[VHOST_VRING_IDX_RX (*queue)];
2326 vhost_user_vring_t *txvq =
2327 &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
2328
2329 if (txvq->n_since_last_int)
2330 {
2331 if (now >= txvq->int_deadline)
2332 vhost_user_send_call (vm, txvq);
2333 else
2334 next_timeout = txvq->int_deadline - now;
2335 }
2336
2337 if (rxvq->n_since_last_int)
2338 {
2339 if (now >= rxvq->int_deadline)
2340 vhost_user_send_call (vm, rxvq);
2341 else
2342 next_timeout = rxvq->int_deadline - now;
2343 }
2344
2345 if ((next_timeout < timeout) && (next_timeout > 0.0))
2346 timeout = next_timeout;
2347 }
2348 });
2349 /* *INDENT-ON* */
2350 break;
2351
2352 default:
2353 clib_warning ("BUG: unhandled event type %d", event_type);
2354 break;
2355 }
2356 /* No less than 1 millisecond */
2357 if (timeout < 1e-3)
2358 timeout = 1e-3;
2359 if (stop_timer)
2360 timeout = 3153600000.0;
2361 }
2362 return 0;
2363}
2364
2365/* *INDENT-OFF* */
2366VLIB_REGISTER_NODE (vhost_user_send_interrupt_node,static) = {
2367 .function = vhost_user_send_interrupt_process,
2368 .type = VLIB_NODE_TYPE_PROCESS,
2369 .name = "vhost-user-send-interrupt-process",
2370};
2371/* *INDENT-ON* */
2372
2373static clib_error_t *
2374vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index,
2375 u32 qid, vnet_hw_interface_rx_mode mode)
2376{
2377 vlib_main_t *vm = vnm->vlib_main;
2378 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2379 vhost_user_main_t *vum = &vhost_user_main;
2380 vhost_user_intf_t *vui =
2381 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
2382 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
2383
2384 if ((mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2385 (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
2386 {
Steven49a04b92017-07-29 08:56:08 -07002387 if (txvq->kickfd_idx == ~0)
2388 {
2389 // We cannot support interrupt mode if the driver opts out
2390 return clib_error_return (0, "Driver does not support interrupt");
2391 }
Stevenf3b53642017-05-01 14:03:02 -07002392 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2393 {
2394 vum->ifq_count++;
2395 // Start the timer if this is the first encounter on interrupt
2396 // interface/queue
2397 if ((vum->ifq_count == 1) &&
2398 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2399 vlib_process_signal_event (vm,
2400 vhost_user_send_interrupt_node.index,
2401 VHOST_USER_EVENT_START_TIMER, 0);
2402 }
2403 }
2404 else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2405 {
2406 if (((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2407 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) &&
2408 vum->ifq_count)
2409 {
2410 vum->ifq_count--;
2411 // Stop the timer if there is no more interrupt interface/queue
2412 if ((vum->ifq_count == 0) &&
2413 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2414 vlib_process_signal_event (vm,
2415 vhost_user_send_interrupt_node.index,
2416 VHOST_USER_EVENT_STOP_TIMER, 0);
2417 }
2418 }
2419
2420 txvq->mode = mode;
2421 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2422 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
2423 else if ((mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE) ||
2424 (mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT))
2425 txvq->used->flags = 0;
2426 else
2427 {
2428 clib_warning ("BUG: unhandled mode %d changed for if %d queue %d", mode,
2429 hw_if_index, qid);
2430 return clib_error_return (0, "unsupported");
2431 }
2432
2433 return 0;
2434}
2435
Ed Warnickecb9cada2015-12-08 15:45:58 -07002436static clib_error_t *
Damjan Marion00a9dca2016-08-17 17:05:46 +02002437vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
2438 u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002439{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002440 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002441 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002442 vhost_user_main_t *vum = &vhost_user_main;
2443 vhost_user_intf_t *vui =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002444 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002445
2446 vui->admin_up = is_up;
2447
Yoann Desmouceaux35df2e12017-09-20 11:00:42 +02002448 if (is_up && vui->is_up)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002449 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002450 VNET_HW_INTERFACE_FLAG_LINK_UP);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002451
2452 return /* no error */ 0;
2453}
2454
Damjan Marion00a9dca2016-08-17 17:05:46 +02002455/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002456VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2457 .name = "vhost-user",
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002458 .tx_function = vhost_user_tx,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002459 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2460 .tx_function_error_strings = vhost_user_tx_func_error_strings,
2461 .format_device_name = format_vhost_user_interface_name,
2462 .name_renumber = vhost_user_name_renumber,
2463 .admin_up_down_function = vhost_user_interface_admin_up_down,
Stevenf3b53642017-05-01 14:03:02 -07002464 .rx_mode_change_function = vhost_user_interface_rx_mode_change,
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002465 .format_tx_trace = format_vhost_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002466};
2467
Damjan Marion1c80e832016-05-11 23:07:18 +02002468VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002469 vhost_user_tx)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002470/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +02002471
Ed Warnickecb9cada2015-12-08 15:45:58 -07002472static uword
2473vhost_user_process (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002474 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002475{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002476 vhost_user_main_t *vum = &vhost_user_main;
2477 vhost_user_intf_t *vui;
2478 struct sockaddr_un sun;
2479 int sockfd;
Damjan Marion56dd5432017-09-08 19:52:02 +02002480 clib_file_t template = { 0 };
Damjan Marion00a9dca2016-08-17 17:05:46 +02002481 f64 timeout = 3153600000.0 /* 100 years */ ;
2482 uword *event_data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002483
Steven0d150bb2017-03-22 12:05:19 -07002484 sockfd = -1;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002485 sun.sun_family = AF_UNIX;
2486 template.read_function = vhost_user_socket_read;
2487 template.error_function = vhost_user_socket_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002488
Damjan Marion00a9dca2016-08-17 17:05:46 +02002489 while (1)
2490 {
2491 vlib_process_wait_for_event_or_clock (vm, timeout);
2492 vlib_process_get_events (vm, &event_data);
2493 vec_reset_length (event_data);
2494
2495 timeout = 3.0;
2496
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002497 /* *INDENT-OFF* */
2498 pool_foreach (vui, vum->vhost_user_interfaces, {
Damjan Marion00a9dca2016-08-17 17:05:46 +02002499
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002500 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
Damjan Marion56dd5432017-09-08 19:52:02 +02002501 if (vui->clib_file_index == ~0)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002502 {
Steven0d150bb2017-03-22 12:05:19 -07002503 if ((sockfd < 0) &&
2504 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
2505 {
2506 /*
2507 * 1st time error or new error for this interface,
2508 * spit out the message and record the error
2509 */
2510 if (!vui->sock_errno || (vui->sock_errno != errno))
2511 {
2512 clib_unix_warning
2513 ("Error: Could not open unix socket for %s",
2514 vui->sock_filename);
2515 vui->sock_errno = errno;
2516 }
2517 continue;
2518 }
2519
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002520 /* try to connect */
2521 strncpy (sun.sun_path, (char *) vui->sock_filename,
2522 sizeof (sun.sun_path) - 1);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002523
Andrew Yourtchenko0c3d4672017-01-03 16:52:22 +00002524 /* Avoid hanging VPP if the other end does not accept */
Dave Barach8f544962017-01-18 10:23:22 -05002525 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2526 clib_unix_warning ("fcntl");
2527
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002528 if (connect (sockfd, (struct sockaddr *) &sun,
2529 sizeof (struct sockaddr_un)) == 0)
2530 {
Andrew Yourtchenko0c3d4672017-01-03 16:52:22 +00002531 /* Set the socket to blocking as it was before */
Dave Barach8f544962017-01-18 10:23:22 -05002532 if (fcntl(sockfd, F_SETFL, 0) < 0)
2533 clib_unix_warning ("fcntl2");
2534
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002535 vui->sock_errno = 0;
2536 template.file_descriptor = sockfd;
2537 template.private_data =
2538 vui - vhost_user_main.vhost_user_interfaces;
Damjan Marion56dd5432017-09-08 19:52:02 +02002539 vui->clib_file_index = clib_file_add (&file_main, &template);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002540
Steven0d150bb2017-03-22 12:05:19 -07002541 /* This sockfd is considered consumed */
2542 sockfd = -1;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002543 }
2544 else
2545 {
2546 vui->sock_errno = errno;
2547 }
2548 }
2549 else
2550 {
2551 /* check if socket is alive */
2552 int error = 0;
2553 socklen_t len = sizeof (error);
Damjan Marion56dd5432017-09-08 19:52:02 +02002554 int fd = UNIX_GET_FD(vui->clib_file_index);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002555 int retval =
2556 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002557
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002558 if (retval)
2559 {
2560 DBG_SOCK ("getsockopt returned %d", retval);
2561 vhost_user_if_disconnect (vui);
2562 }
2563 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02002564 }
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002565 });
2566 /* *INDENT-ON* */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002567 }
2568 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002569}
2570
Damjan Marion00a9dca2016-08-17 17:05:46 +02002571/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002572VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
2573 .function = vhost_user_process,
2574 .type = VLIB_NODE_TYPE_PROCESS,
2575 .name = "vhost-user-process",
2576};
Damjan Marion00a9dca2016-08-17 17:05:46 +02002577/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002578
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002579/**
2580 * Disables and reset interface structure.
2581 * It can then be either init again, or removed from used interfaces.
2582 */
2583static void
2584vhost_user_term_if (vhost_user_intf_t * vui)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002585{
Ole Troan553a4112017-01-10 10:07:04 +01002586 int q;
Steven5445f5f2017-04-25 16:16:00 -07002587 vhost_user_main_t *vum = &vhost_user_main;
Ole Troan553a4112017-01-10 10:07:04 +01002588
Ed Warnickecb9cada2015-12-08 15:45:58 -07002589 // disconnect interface sockets
Damjan Marion00a9dca2016-08-17 17:05:46 +02002590 vhost_user_if_disconnect (vui);
Pierre Pfisterfbb2ef62016-11-16 02:43:29 +00002591 vhost_user_update_iface_state (vui);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002592
Ole Troan553a4112017-01-10 10:07:04 +01002593 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2594 {
2595 clib_mem_free ((void *) vui->vring_locks[q]);
2596 }
2597
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002598 if (vui->unix_server_index != ~0)
2599 {
2600 //Close server socket
Damjan Marion56dd5432017-09-08 19:52:02 +02002601 clib_file_t *uf = pool_elt_at_index (file_main.file_pool,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002602 vui->unix_server_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002603 clib_file_del (&file_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002604 vui->unix_server_index = ~0;
Steven53129422017-04-21 13:31:50 -07002605 unlink (vui->sock_filename);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002606 }
Steven5445f5f2017-04-25 16:16:00 -07002607
2608 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
2609 &vui->if_index);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002610}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002611
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002612int
2613vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
2614{
2615 vhost_user_main_t *vum = &vhost_user_main;
2616 vhost_user_intf_t *vui;
2617 int rv = 0;
2618 vnet_hw_interface_t *hwif;
Stevenf3b53642017-05-01 14:03:02 -07002619 u16 *queue;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002620
2621 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2622 hwif->dev_class_index != vhost_user_dev_class.index)
2623 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2624
2625 DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2626 hwif->name, hwif->dev_instance);
2627
2628 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2629
Stevenf3b53642017-05-01 14:03:02 -07002630 vec_foreach (queue, vui->rx_queues)
2631 {
2632 vhost_user_vring_t *txvq;
2633
2634 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
2635 if ((vum->ifq_count > 0) &&
2636 ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2637 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
2638 {
2639 vum->ifq_count--;
2640 // Stop the timer if there is no more interrupt interface/queue
2641 if ((vum->ifq_count == 0) &&
2642 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2643 {
2644 vlib_process_signal_event (vm,
2645 vhost_user_send_interrupt_node.index,
2646 VHOST_USER_EVENT_STOP_TIMER, 0);
2647 break;
2648 }
2649 }
2650 }
2651
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002652 // Disable and reset interface
2653 vhost_user_term_if (vui);
2654
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002655 // Reset renumbered iface
2656 if (hwif->dev_instance <
2657 vec_len (vum->show_dev_instance_by_real_dev_instance))
2658 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
2659
2660 // Delete ethernet interface
Ed Warnickecb9cada2015-12-08 15:45:58 -07002661 ethernet_delete_interface (vnm, vui->hw_if_index);
Wojciech Decd8e47872017-01-17 21:45:11 +01002662
2663 // Back to pool
2664 pool_put (vum->vhost_user_interfaces, vui);
2665
Ed Warnickecb9cada2015-12-08 15:45:58 -07002666 return rv;
2667}
2668
Steven53129422017-04-21 13:31:50 -07002669static clib_error_t *
2670vhost_user_exit (vlib_main_t * vm)
2671{
2672 vnet_main_t *vnm = vnet_get_main ();
2673 vhost_user_main_t *vum = &vhost_user_main;
2674 vhost_user_intf_t *vui;
2675
Steven41748862017-04-25 13:49:51 -07002676 vlib_worker_thread_barrier_sync (vlib_get_main ());
Steven53129422017-04-21 13:31:50 -07002677 /* *INDENT-OFF* */
2678 pool_foreach (vui, vum->vhost_user_interfaces, {
2679 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
2680 });
2681 /* *INDENT-ON* */
Steven41748862017-04-25 13:49:51 -07002682 vlib_worker_thread_barrier_release (vlib_get_main ());
Steven53129422017-04-21 13:31:50 -07002683 return 0;
2684}
2685
2686VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
2687
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002688/**
2689 * Open server unix socket on specified sock_filename.
2690 */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002691static int
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002692vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002693{
Pierre Pfister5afccb22016-07-25 14:32:02 +01002694 int rv = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002695 struct sockaddr_un un = { };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002696 int fd;
2697 /* create listening socket */
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002698 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2699 return VNET_API_ERROR_SYSCALL_ERROR_1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002700
2701 un.sun_family = AF_UNIX;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002702 strncpy ((char *) un.sun_path, (char *) sock_filename,
2703 sizeof (un.sun_path) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002704
2705 /* remove if exists */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002706 unlink ((char *) sock_filename);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002707
Damjan Marion00a9dca2016-08-17 17:05:46 +02002708 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2709 {
2710 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2711 goto error;
2712 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002713
Damjan Marion00a9dca2016-08-17 17:05:46 +02002714 if (listen (fd, 1) == -1)
2715 {
2716 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2717 goto error;
2718 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002719
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002720 *sock_fd = fd;
2721 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002722
2723error:
Damjan Marion00a9dca2016-08-17 17:05:46 +02002724 close (fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002725 return rv;
2726}
2727
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002728/**
2729 * Create ethernet interface for vhost user interface.
2730 */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002731static void
2732vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
2733 vhost_user_intf_t * vui, u8 * hwaddress)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002734{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002735 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002736 u8 hwaddr[6];
Damjan Marion00a9dca2016-08-17 17:05:46 +02002737 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002738
2739 /* create hw and sw interface */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002740 if (hwaddress)
2741 {
2742 clib_memcpy (hwaddr, hwaddress, 6);
2743 }
2744 else
2745 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002746 random_u32 (&vum->random);
2747 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
Damjan Marion00a9dca2016-08-17 17:05:46 +02002748 hwaddr[0] = 2;
2749 hwaddr[1] = 0xfe;
2750 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002751
2752 error = ethernet_register_interface
2753 (vnm,
2754 vhost_user_dev_class.index,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002755 vui - vum->vhost_user_interfaces /* device instance */ ,
2756 hwaddr /* ethernet address */ ,
2757 &vui->hw_if_index, 0 /* flag change */ );
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002758
Ed Warnickecb9cada2015-12-08 15:45:58 -07002759 if (error)
2760 clib_error_report (error);
Pierre Pfister328e99b2016-02-12 13:18:42 +00002761
2762 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
2763 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002764}
2765
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002766/*
2767 * Initialize vui with specified attributes
2768 */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002769static void
2770vhost_user_vui_init (vnet_main_t * vnm,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002771 vhost_user_intf_t * vui,
2772 int server_sock_fd,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002773 const char *sock_filename,
Stevenf3b53642017-05-01 14:03:02 -07002774 u64 feature_mask, u32 * sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002775{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002776 vnet_sw_interface_t *sw;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01002777 int q;
Steven5445f5f2017-04-25 16:16:00 -07002778 vhost_user_main_t *vum = &vhost_user_main;
Stevenf3b53642017-05-01 14:03:02 -07002779 vnet_hw_interface_t *hw;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002780
Stevenf3b53642017-05-01 14:03:02 -07002781 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
2782 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002783 if (server_sock_fd != -1)
2784 {
Damjan Marion56dd5432017-09-08 19:52:02 +02002785 clib_file_t template = { 0 };
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002786 template.read_function = vhost_user_socksvr_accept_ready;
2787 template.file_descriptor = server_sock_fd;
Steven5445f5f2017-04-25 16:16:00 -07002788 template.private_data = vui - vum->vhost_user_interfaces; //hw index
Damjan Marion56dd5432017-09-08 19:52:02 +02002789 vui->unix_server_index = clib_file_add (&file_main, &template);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002790 }
2791 else
2792 {
2793 vui->unix_server_index = ~0;
2794 }
2795
Ed Warnickecb9cada2015-12-08 15:45:58 -07002796 vui->sw_if_index = sw->sw_if_index;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002797 strncpy (vui->sock_filename, sock_filename,
2798 ARRAY_LEN (vui->sock_filename) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002799 vui->sock_errno = 0;
2800 vui->is_up = 0;
2801 vui->feature_mask = feature_mask;
Damjan Marion56dd5432017-09-08 19:52:02 +02002802 vui->clib_file_index = ~0;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01002803 vui->log_base_addr = 0;
Steven5445f5f2017-04-25 16:16:00 -07002804 vui->if_index = vui - vum->vhost_user_interfaces;
2805 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
2806 &vui->if_index, 0);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01002807
Pierre Pfistere21c5282016-09-21 08:04:59 +01002808 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2809 vhost_user_vring_init (vui, q);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002810
Stevenf3b53642017-05-01 14:03:02 -07002811 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002812 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002813
2814 if (sw_if_index)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002815 *sw_if_index = vui->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002816
Pierre Pfistere21c5282016-09-21 08:04:59 +01002817 for (q = 0; q < VHOST_VRING_MAX_N; q++)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002818 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01002819 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
2820 CLIB_CACHE_LINE_BYTES);
2821 memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002822 }
Pierre Pfistere21c5282016-09-21 08:04:59 +01002823
2824 vec_validate (vui->per_cpu_tx_qid,
2825 vlib_get_thread_main ()->n_vlib_mains - 1);
2826 vhost_user_tx_thread_placement (vui);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002827}
2828
Damjan Marion00a9dca2016-08-17 17:05:46 +02002829int
2830vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2831 const char *sock_filename,
2832 u8 is_server,
2833 u32 * sw_if_index,
2834 u64 feature_mask,
Stevenf3b53642017-05-01 14:03:02 -07002835 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002836{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002837 vhost_user_intf_t *vui = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002838 u32 sw_if_idx = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002839 int rv = 0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002840 int server_sock_fd = -1;
Steven7312cc72017-03-15 21:18:55 -07002841 vhost_user_main_t *vum = &vhost_user_main;
Steven5445f5f2017-04-25 16:16:00 -07002842 uword *if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002843
Wojciech Dec3cd9eed2017-01-03 10:38:37 +01002844 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2845 {
2846 return VNET_API_ERROR_INVALID_ARGUMENT;
2847 }
2848
Steven5445f5f2017-04-25 16:16:00 -07002849 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2850 if (if_index)
2851 {
2852 if (sw_if_index)
2853 {
2854 vui = &vum->vhost_user_interfaces[*if_index];
2855 *sw_if_index = vui->sw_if_index;
2856 }
2857 return VNET_API_ERROR_IF_ALREADY_EXISTS;
2858 }
2859
Damjan Marion00a9dca2016-08-17 17:05:46 +02002860 if (is_server)
2861 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002862 if ((rv =
2863 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002864 {
2865 return rv;
2866 }
2867 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002868
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002869 pool_get (vhost_user_main.vhost_user_interfaces, vui);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002870
Pierre Pfisteref65cb02016-02-19 13:52:44 +00002871 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002872 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
Stevenf3b53642017-05-01 14:03:02 -07002873 feature_mask, &sw_if_idx);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002874
Damjan Marion00a9dca2016-08-17 17:05:46 +02002875 if (renumber)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002876 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002877
2878 if (sw_if_index)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002879 *sw_if_index = sw_if_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002880
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002881 // Process node must connect
2882 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
Steven7312cc72017-03-15 21:18:55 -07002883
Ed Warnickecb9cada2015-12-08 15:45:58 -07002884 return rv;
2885}
2886
Damjan Marion00a9dca2016-08-17 17:05:46 +02002887int
2888vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2889 const char *sock_filename,
2890 u8 is_server,
2891 u32 sw_if_index,
Stevenf3b53642017-05-01 14:03:02 -07002892 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002893{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002894 vhost_user_main_t *vum = &vhost_user_main;
2895 vhost_user_intf_t *vui = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002896 u32 sw_if_idx = ~0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002897 int server_sock_fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002898 int rv = 0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002899 vnet_hw_interface_t *hwif;
Steven5445f5f2017-04-25 16:16:00 -07002900 uword *if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002901
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002902 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2903 hwif->dev_class_index != vhost_user_dev_class.index)
2904 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002905
Steven5445f5f2017-04-25 16:16:00 -07002906 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2907 return VNET_API_ERROR_INVALID_ARGUMENT;
2908
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002909 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002910
Steven5445f5f2017-04-25 16:16:00 -07002911 /*
2912 * Disallow changing the interface to have the same path name
2913 * as other interface
2914 */
2915 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2916 if (if_index && (*if_index != vui->if_index))
2917 return VNET_API_ERROR_IF_ALREADY_EXISTS;
2918
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002919 // First try to open server socket
Damjan Marion00a9dca2016-08-17 17:05:46 +02002920 if (is_server)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002921 if ((rv = vhost_user_init_server_sock (sock_filename,
2922 &server_sock_fd)) != 0)
2923 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002924
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002925 vhost_user_term_if (vui);
2926 vhost_user_vui_init (vnm, vui, server_sock_fd,
Stevenf3b53642017-05-01 14:03:02 -07002927 sock_filename, feature_mask, &sw_if_idx);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002928
Damjan Marion00a9dca2016-08-17 17:05:46 +02002929 if (renumber)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002930 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002931
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002932 // Process node must connect
2933 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
Steven7312cc72017-03-15 21:18:55 -07002934
Ed Warnickecb9cada2015-12-08 15:45:58 -07002935 return rv;
2936}
2937
2938clib_error_t *
2939vhost_user_connect_command_fn (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002940 unformat_input_t * input,
2941 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002942{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002943 unformat_input_t _line_input, *line_input = &_line_input;
2944 u8 *sock_filename = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002945 u32 sw_if_index;
2946 u8 is_server = 0;
Pierre Pfistere21c5282016-09-21 08:04:59 +01002947 u64 feature_mask = (u64) ~ (0ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002948 u8 renumber = 0;
2949 u32 custom_dev_instance = ~0;
Pierre Pfisteref65cb02016-02-19 13:52:44 +00002950 u8 hwaddr[6];
2951 u8 *hw = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -05002952 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002953
2954 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002955 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002956 return 0;
2957
Damjan Marion00a9dca2016-08-17 17:05:46 +02002958 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2959 {
2960 if (unformat (line_input, "socket %s", &sock_filename))
2961 ;
2962 else if (unformat (line_input, "server"))
2963 is_server = 1;
2964 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2965 ;
2966 else
2967 if (unformat
2968 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
2969 hw = hwaddr;
2970 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
2971 {
2972 renumber = 1;
2973 }
2974 else
Billy McFalla9a20e72017-02-15 11:39:12 -05002975 {
2976 error = clib_error_return (0, "unknown input `%U'",
2977 format_unformat_error, line_input);
2978 goto done;
2979 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002980 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002981
Damjan Marion00a9dca2016-08-17 17:05:46 +02002982 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002983
Pierre Pfister5afccb22016-07-25 14:32:02 +01002984 int rv;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002985 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
2986 is_server, &sw_if_index, feature_mask,
Stevenf3b53642017-05-01 14:03:02 -07002987 renumber, custom_dev_instance, hw)))
Damjan Marion00a9dca2016-08-17 17:05:46 +02002988 {
Billy McFalla9a20e72017-02-15 11:39:12 -05002989 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
2990 goto done;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002991 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002992
Damjan Marion00a9dca2016-08-17 17:05:46 +02002993 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
2994 sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -05002995
2996done:
2997 vec_free (sock_filename);
2998 unformat_free (line_input);
2999
3000 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003001}
3002
3003clib_error_t *
3004vhost_user_delete_command_fn (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02003005 unformat_input_t * input,
3006 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003007{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003008 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003009 u32 sw_if_index = ~0;
Pierre Pfisterece983d2016-11-21 12:52:22 +00003010 vnet_main_t *vnm = vnet_get_main ();
Billy McFalla9a20e72017-02-15 11:39:12 -05003011 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003012
3013 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +02003014 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003015 return 0;
3016
Damjan Marion00a9dca2016-08-17 17:05:46 +02003017 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3018 {
3019 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
3020 ;
Pierre Pfisterece983d2016-11-21 12:52:22 +00003021 else if (unformat
3022 (line_input, "%U", unformat_vnet_sw_interface, vnm,
3023 &sw_if_index))
3024 {
3025 vnet_hw_interface_t *hwif =
3026 vnet_get_sup_hw_interface (vnm, sw_if_index);
3027 if (hwif == NULL ||
3028 vhost_user_dev_class.index != hwif->dev_class_index)
Billy McFalla9a20e72017-02-15 11:39:12 -05003029 {
3030 error = clib_error_return (0, "Not a vhost interface");
3031 goto done;
3032 }
Pierre Pfisterece983d2016-11-21 12:52:22 +00003033 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003034 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003035 {
3036 error = clib_error_return (0, "unknown input `%U'",
3037 format_unformat_error, line_input);
3038 goto done;
3039 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003040 }
Billy McFalla9a20e72017-02-15 11:39:12 -05003041
Damjan Marion00a9dca2016-08-17 17:05:46 +02003042 vhost_user_delete_if (vnm, vm, sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -05003043
3044done:
3045 unformat_free (line_input);
3046
3047 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003048}
3049
Damjan Marion00a9dca2016-08-17 17:05:46 +02003050int
3051vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
3052 vhost_user_intf_details_t ** out_vuids)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003053{
3054 int rv = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003055 vhost_user_main_t *vum = &vhost_user_main;
3056 vhost_user_intf_t *vui;
3057 vhost_user_intf_details_t *r_vuids = NULL;
3058 vhost_user_intf_details_t *vuid = NULL;
3059 u32 *hw_if_indices = 0;
3060 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003061 u8 *s = NULL;
3062 int i;
3063
3064 if (!out_vuids)
Damjan Marion00a9dca2016-08-17 17:05:46 +02003065 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003066
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003067 pool_foreach (vui, vum->vhost_user_interfaces,
3068 vec_add1 (hw_if_indices, vui->hw_if_index);
3069 );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003070
Damjan Marion00a9dca2016-08-17 17:05:46 +02003071 for (i = 0; i < vec_len (hw_if_indices); i++)
3072 {
3073 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003074 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003075
Damjan Marion00a9dca2016-08-17 17:05:46 +02003076 vec_add2 (r_vuids, vuid, 1);
3077 vuid->sw_if_index = vui->sw_if_index;
3078 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
3079 vuid->features = vui->features;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003080 vuid->num_regions = vui->nregions;
Marek Gradzki0578cd12017-02-13 14:19:51 +01003081 vuid->is_server = vui->unix_server_index != ~0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003082 vuid->sock_errno = vui->sock_errno;
3083 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
3084 ARRAY_LEN (vuid->sock_filename) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003085
Damjan Marion00a9dca2016-08-17 17:05:46 +02003086 s = format (s, "%v%c", hi->name, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003087
Damjan Marion00a9dca2016-08-17 17:05:46 +02003088 strncpy ((char *) vuid->if_name, (char *) s,
3089 ARRAY_LEN (vuid->if_name) - 1);
3090 _vec_len (s) = 0;
3091 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003092
3093 vec_free (s);
3094 vec_free (hw_if_indices);
3095
3096 *out_vuids = r_vuids;
3097
3098 return rv;
3099}
3100
3101clib_error_t *
3102show_vhost_user_command_fn (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02003103 unformat_input_t * input,
3104 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003105{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003106 clib_error_t *error = 0;
3107 vnet_main_t *vnm = vnet_get_main ();
3108 vhost_user_main_t *vum = &vhost_user_main;
3109 vhost_user_intf_t *vui;
3110 u32 hw_if_index, *hw_if_indices = 0;
3111 vnet_hw_interface_t *hi;
Stevenf3b53642017-05-01 14:03:02 -07003112 u16 *queue;
Pierre Pfistere21c5282016-09-21 08:04:59 +01003113 u32 ci;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003114 int i, j, q;
3115 int show_descr = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003116 struct feat_struct
3117 {
3118 u8 bit;
3119 char *str;
3120 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07003121 struct feat_struct *feat_entry;
3122
3123 static struct feat_struct feat_array[] = {
3124#define _(s,b) { .str = #s, .bit = b, },
Damjan Marion00a9dca2016-08-17 17:05:46 +02003125 foreach_virtio_net_feature
Ed Warnickecb9cada2015-12-08 15:45:58 -07003126#undef _
Damjan Marion00a9dca2016-08-17 17:05:46 +02003127 {.str = NULL}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003128 };
3129
Pierre Pfistere21c5282016-09-21 08:04:59 +01003130#define foreach_protocol_feature \
3131 _(VHOST_USER_PROTOCOL_F_MQ) \
3132 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
3133
3134 static struct feat_struct proto_feat_array[] = {
3135#define _(s) { .str = #s, .bit = s},
3136 foreach_protocol_feature
3137#undef _
3138 {.str = NULL}
3139 };
3140
Damjan Marion00a9dca2016-08-17 17:05:46 +02003141 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3142 {
3143 if (unformat
3144 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
3145 {
3146 vec_add1 (hw_if_indices, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003147 }
3148 else if (unformat (input, "descriptors") || unformat (input, "desc"))
3149 show_descr = 1;
3150 else
3151 {
3152 error = clib_error_return (0, "unknown input `%U'",
3153 format_unformat_error, input);
3154 goto done;
3155 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003156 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003157 if (vec_len (hw_if_indices) == 0)
3158 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003159 pool_foreach (vui, vum->vhost_user_interfaces,
3160 vec_add1 (hw_if_indices, vui->hw_if_index);
3161 );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003162 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003163 vlib_cli_output (vm, "Virtio vhost-user interfaces");
Pierre Pfistere21c5282016-09-21 08:04:59 +01003164 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
Damjan Marion00a9dca2016-08-17 17:05:46 +02003165 vum->coalesce_frames, vum->coalesce_time);
Stevenf3b53642017-05-01 14:03:02 -07003166 vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d",
3167 vum->ifq_count);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003168
3169 for (i = 0; i < vec_len (hw_if_indices); i++)
3170 {
3171 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003172 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003173 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
3174 hi->name, hw_if_indices[i]);
3175
Pierre Pfistere21c5282016-09-21 08:04:59 +01003176 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
3177 " features mask (0x%llx): \n"
3178 " features (0x%llx): \n",
3179 vui->virtio_net_hdr_sz, vui->feature_mask,
3180 vui->features);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003181
3182 feat_entry = (struct feat_struct *) &feat_array;
3183 while (feat_entry->str)
3184 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01003185 if (vui->features & (1ULL << feat_entry->bit))
3186 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3187 feat_entry->bit);
3188 feat_entry++;
3189 }
3190
3191 vlib_cli_output (vm, " protocol features (0x%llx)",
3192 vui->protocol_features);
3193 feat_entry = (struct feat_struct *) &proto_feat_array;
3194 while (feat_entry->str)
3195 {
3196 if (vui->protocol_features & (1ULL << feat_entry->bit))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003197 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3198 feat_entry->bit);
3199 feat_entry++;
3200 }
3201
3202 vlib_cli_output (vm, "\n");
3203
Damjan Marion00a9dca2016-08-17 17:05:46 +02003204 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
3205 vui->sock_filename,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003206 (vui->unix_server_index != ~0) ? "server" : "client",
Damjan Marion00a9dca2016-08-17 17:05:46 +02003207 strerror (vui->sock_errno));
3208
Pierre Pfistere21c5282016-09-21 08:04:59 +01003209 vlib_cli_output (vm, " rx placement: ");
Stevenf3b53642017-05-01 14:03:02 -07003210
3211 vec_foreach (queue, vui->rx_queues)
Pierre Pfistere21c5282016-09-21 08:04:59 +01003212 {
Stevenf3b53642017-05-01 14:03:02 -07003213 vnet_main_t *vnm = vnet_get_main ();
3214 uword thread_index;
3215 vnet_hw_interface_rx_mode mode;
3216
3217 thread_index = vnet_get_device_input_thread_index (vnm,
3218 vui->hw_if_index,
3219 *queue);
3220 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, *queue, &mode);
3221 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
3222 thread_index, VHOST_VRING_IDX_TX (*queue),
3223 format_vnet_hw_interface_rx_mode, mode);
Pierre Pfistere21c5282016-09-21 08:04:59 +01003224 }
3225
3226 vlib_cli_output (vm, " tx placement: %s\n",
3227 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
3228
3229 vec_foreach_index (ci, vui->per_cpu_tx_qid)
3230 {
3231 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
3232 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
3233 }
3234
3235 vlib_cli_output (vm, "\n");
3236
Damjan Marion00a9dca2016-08-17 17:05:46 +02003237 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
3238
3239 if (vui->nregions)
3240 {
3241 vlib_cli_output (vm,
3242 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
3243 vlib_cli_output (vm,
3244 " ====== ===== ================== ================== ================== ================== ==================\n");
3245 }
3246 for (j = 0; j < vui->nregions; j++)
3247 {
3248 vlib_cli_output (vm,
3249 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
3250 j, vui->region_mmap_fd[j],
3251 vui->regions[j].guest_phys_addr,
3252 vui->regions[j].memory_size,
3253 vui->regions[j].userspace_addr,
3254 vui->regions[j].mmap_offset,
3255 pointer_to_uword (vui->region_mmap_addr[j]));
3256 }
Pierre Pfistere21c5282016-09-21 08:04:59 +01003257 for (q = 0; q < VHOST_VRING_MAX_N; q++)
Damjan Marion00a9dca2016-08-17 17:05:46 +02003258 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01003259 if (!vui->vrings[q].started)
3260 continue;
3261
3262 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
3263 (q & 1) ? "RX" : "TX",
3264 vui->vrings[q].enabled ? "" : " disabled");
Damjan Marion00a9dca2016-08-17 17:05:46 +02003265
3266 vlib_cli_output (vm,
3267 " qsz %d last_avail_idx %d last_used_idx %d\n",
Steven97878892017-08-29 09:23:26 -07003268 vui->vrings[q].qsz_mask + 1,
3269 vui->vrings[q].last_avail_idx,
Damjan Marion00a9dca2016-08-17 17:05:46 +02003270 vui->vrings[q].last_used_idx);
3271
3272 if (vui->vrings[q].avail && vui->vrings[q].used)
3273 vlib_cli_output (vm,
3274 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
3275 vui->vrings[q].avail->flags,
3276 vui->vrings[q].avail->idx,
3277 vui->vrings[q].used->flags,
3278 vui->vrings[q].used->idx);
3279
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003280 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
3281 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003282 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003283 kickfd, callfd, vui->vrings[q].errfd);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003284
3285 if (show_descr)
3286 {
3287 vlib_cli_output (vm, "\n descriptor table:\n");
3288 vlib_cli_output (vm,
3289 " id addr len flags next user_addr\n");
3290 vlib_cli_output (vm,
3291 " ===== ================== ===== ====== ===== ==================\n");
Steven97878892017-08-29 09:23:26 -07003292 for (j = 0; j < vui->vrings[q].qsz_mask + 1; j++)
Damjan Marion00a9dca2016-08-17 17:05:46 +02003293 {
Pierre Pfister11f92052016-09-21 08:08:55 +01003294 u32 mem_hint = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003295 vlib_cli_output (vm,
3296 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3297 j, vui->vrings[q].desc[j].addr,
3298 vui->vrings[q].desc[j].len,
3299 vui->vrings[q].desc[j].flags,
3300 vui->vrings[q].desc[j].next,
3301 pointer_to_uword (map_guest_mem
3302 (vui,
Pierre Pfisterba1d0462016-07-27 16:38:20 +01003303 vui->vrings[q].desc[j].
Pierre Pfister11f92052016-09-21 08:08:55 +01003304 addr, &mem_hint)));
Damjan Marion00a9dca2016-08-17 17:05:46 +02003305 }
3306 }
3307 }
3308 vlib_cli_output (vm, "\n");
3309 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003310done:
3311 vec_free (hw_if_indices);
3312 return error;
3313}
3314
Damjan Marion8d281b32016-08-24 14:32:39 +02003315/*
3316 * CLI functions
3317 */
3318
Billy McFalla92501a2016-11-23 12:45:29 -05003319/*?
3320 * Create a vHost User interface. Once created, a new virtual interface
3321 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3322 * is the next free index.
3323 *
3324 * There are several parameters associated with a vHost interface:
3325 *
3326 * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3327 * VPP to manage the vHost interface. If socket does not already exist, VPP will
3328 * create the socket.
3329 *
3330 * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3331 * linux socket. If not provided, VPP will be the client.
3332 *
3333 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3334 * startup. By default, all supported features will be advertised. Otherwise,
3335 * provide the set of features desired.
3336 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3337 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3338 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3339 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3340 * - 0x004000000 (26) - VHOST_F_LOG_ALL
3341 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3342 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3343 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3344 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3345 *
3346 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3347 * X:X:X:X:X:X unix or X.X.X cisco format.
3348 *
3349 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3350 * in the name to be specified. If instance already exists, name will be used
3351 * anyway and multiple instances will have the same name. Use with caution.
3352 *
Steven7312cc72017-03-15 21:18:55 -07003353 * - <b>mode [interrupt | polling]</b> - Optional parameter specifying
3354 * the input thread polling policy.
3355 *
Billy McFalla92501a2016-11-23 12:45:29 -05003356 * @cliexpar
3357 * Example of how to create a vhost interface with VPP as the client and all features enabled:
3358 * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3359 * VirtualEthernet0/0/0
3360 * @cliexend
3361 * Example of how to create a vhost interface with VPP as the server and with just
3362 * multiple queues enabled:
3363 * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3364 * VirtualEthernet0/0/1
3365 * @cliexend
3366 * Once the vHost interface is created, enable the interface using:
3367 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3368?*/
Damjan Marion8d281b32016-08-24 14:32:39 +02003369/* *INDENT-OFF* */
3370VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3371 .path = "create vhost-user",
Steven7312cc72017-03-15 21:18:55 -07003372 .short_help = "create vhost-user socket <socket-filename> [server] "
Stevenf3b53642017-05-01 14:03:02 -07003373 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ",
Damjan Marion8d281b32016-08-24 14:32:39 +02003374 .function = vhost_user_connect_command_fn,
3375};
Billy McFalla92501a2016-11-23 12:45:29 -05003376/* *INDENT-ON* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003377
Billy McFalla92501a2016-11-23 12:45:29 -05003378/*?
3379 * Delete a vHost User interface using the interface name or the
Dave Barach13ad1f02017-03-26 19:36:18 -04003380 * software interface index. Use the '<em>show interface</em>'
Billy McFalla92501a2016-11-23 12:45:29 -05003381 * command to determine the software interface index. On deletion,
3382 * the linux socket will not be deleted.
3383 *
3384 * @cliexpar
3385 * Example of how to delete a vhost interface by name:
3386 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3387 * Example of how to delete a vhost interface by software interface index:
3388 * @cliexcmd{delete vhost-user sw_if_index 1}
3389?*/
3390/* *INDENT-OFF* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003391VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3392 .path = "delete vhost-user",
Billy McFalla92501a2016-11-23 12:45:29 -05003393 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
Damjan Marion8d281b32016-08-24 14:32:39 +02003394 .function = vhost_user_delete_command_fn,
3395};
3396
Billy McFalla92501a2016-11-23 12:45:29 -05003397/*?
3398 * Display the attributes of a single vHost User interface (provide interface
3399 * name), multiple vHost User interfaces (provide a list of interface names seperated
3400 * by spaces) or all Vhost User interfaces (omit an interface name to display all
3401 * vHost interfaces).
3402 *
3403 * @cliexpar
3404 * @parblock
3405 * Example of how to display a vhost interface:
3406 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3407 * Virtio vhost-user interfaces
3408 * Global:
3409 * coalesce frames 32 time 1e-3
3410 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3411 * virtio_net_hdr_sz 12
3412 * features mask (0xffffffffffffffff):
3413 * features (0x50408000):
3414 * VIRTIO_NET_F_MRG_RXBUF (15)
3415 * VIRTIO_NET_F_MQ (22)
3416 * VIRTIO_F_INDIRECT_DESC (28)
3417 * VHOST_USER_F_PROTOCOL_FEATURES (30)
3418 * protocol features (0x3)
3419 * VHOST_USER_PROTOCOL_F_MQ (0)
3420 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3421 *
3422 * socket filename /tmp/vhost1.sock type client errno "Success"
3423 *
3424 * rx placement:
3425 * thread 1 on vring 1
3426 * thread 1 on vring 5
3427 * thread 2 on vring 3
3428 * thread 2 on vring 7
3429 * tx placement: spin-lock
3430 * thread 0 on vring 0
3431 * thread 1 on vring 2
3432 * thread 2 on vring 0
3433 *
3434 * Memory regions (total 2)
3435 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3436 * ====== ===== ================== ================== ================== ================== ==================
3437 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3438 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3439 *
3440 * Virtqueue 0 (TX)
3441 * qsz 256 last_avail_idx 0 last_used_idx 0
3442 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3443 * kickfd 62 callfd 64 errfd -1
3444 *
3445 * Virtqueue 1 (RX)
3446 * qsz 256 last_avail_idx 0 last_used_idx 0
3447 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3448 * kickfd 65 callfd 66 errfd -1
3449 *
3450 * Virtqueue 2 (TX)
3451 * qsz 256 last_avail_idx 0 last_used_idx 0
3452 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3453 * kickfd 63 callfd 70 errfd -1
3454 *
3455 * Virtqueue 3 (RX)
3456 * qsz 256 last_avail_idx 0 last_used_idx 0
3457 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3458 * kickfd 72 callfd 74 errfd -1
3459 *
3460 * Virtqueue 4 (TX disabled)
3461 * qsz 256 last_avail_idx 0 last_used_idx 0
3462 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3463 * kickfd 76 callfd 78 errfd -1
3464 *
3465 * Virtqueue 5 (RX disabled)
3466 * qsz 256 last_avail_idx 0 last_used_idx 0
3467 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3468 * kickfd 80 callfd 82 errfd -1
3469 *
3470 * Virtqueue 6 (TX disabled)
3471 * qsz 256 last_avail_idx 0 last_used_idx 0
3472 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3473 * kickfd 84 callfd 86 errfd -1
3474 *
3475 * Virtqueue 7 (RX disabled)
3476 * qsz 256 last_avail_idx 0 last_used_idx 0
3477 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3478 * kickfd 88 callfd 90 errfd -1
3479 *
3480 * @cliexend
3481 *
3482 * The optional '<em>descriptors</em>' parameter will display the same output as
3483 * the previous example but will include the descriptor table for each queue.
3484 * The output is truncated below:
3485 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3486 * Virtio vhost-user interfaces
3487 * Global:
3488 * coalesce frames 32 time 1e-3
3489 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3490 * virtio_net_hdr_sz 12
3491 * features mask (0xffffffffffffffff):
3492 * features (0x50408000):
3493 * VIRTIO_NET_F_MRG_RXBUF (15)
3494 * VIRTIO_NET_F_MQ (22)
3495 * :
3496 * Virtqueue 0 (TX)
3497 * qsz 256 last_avail_idx 0 last_used_idx 0
3498 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3499 * kickfd 62 callfd 64 errfd -1
3500 *
3501 * descriptor table:
3502 * id addr len flags next user_addr
3503 * ===== ================== ===== ====== ===== ==================
3504 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3505 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3506 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3507 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3508 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3509 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3510 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3511 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3512 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3513 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3514 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3515 * :
3516 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3517 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3518 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3519 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3520 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3521 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3522 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3523 *
3524 * Virtqueue 1 (RX)
3525 * qsz 256 last_avail_idx 0 last_used_idx 0
3526 * :
3527 * @cliexend
3528 * @endparblock
3529?*/
3530/* *INDENT-OFF* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003531VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3532 .path = "show vhost-user",
Billy McFalla92501a2016-11-23 12:45:29 -05003533 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
Damjan Marion8d281b32016-08-24 14:32:39 +02003534 .function = show_vhost_user_command_fn,
3535};
3536/* *INDENT-ON* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003537
Steven388e51a2017-06-01 12:49:23 -07003538clib_error_t *
3539debug_vhost_user_command_fn (vlib_main_t * vm,
3540 unformat_input_t * input,
3541 vlib_cli_command_t * cmd)
3542{
3543 unformat_input_t _line_input, *line_input = &_line_input;
3544 clib_error_t *error = NULL;
3545 vhost_user_main_t *vum = &vhost_user_main;
Steven2ee2d572017-07-21 16:38:41 -07003546 u8 onoff = 0;
3547 u8 input_found = 0;
Steven388e51a2017-06-01 12:49:23 -07003548
3549 /* Get a line of input. */
3550 if (!unformat_user (input, unformat_line_input, line_input))
Steven2ee2d572017-07-21 16:38:41 -07003551 return clib_error_return (0, "missing argument");
Steven388e51a2017-06-01 12:49:23 -07003552
3553 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3554 {
Steven2ee2d572017-07-21 16:38:41 -07003555 if (input_found)
3556 {
3557 error = clib_error_return (0, "unknown input `%U'",
3558 format_unformat_error, line_input);
3559 goto done;
3560 }
3561
Steven388e51a2017-06-01 12:49:23 -07003562 if (unformat (line_input, "on"))
Steven2ee2d572017-07-21 16:38:41 -07003563 {
3564 input_found = 1;
3565 onoff = 1;
3566 }
Steven388e51a2017-06-01 12:49:23 -07003567 else if (unformat (line_input, "off"))
Steven2ee2d572017-07-21 16:38:41 -07003568 {
3569 input_found = 1;
3570 onoff = 0;
3571 }
Steven388e51a2017-06-01 12:49:23 -07003572 else
Steven2ee2d572017-07-21 16:38:41 -07003573 {
3574 error = clib_error_return (0, "unknown input `%U'",
3575 format_unformat_error, line_input);
3576 goto done;
3577 }
Steven388e51a2017-06-01 12:49:23 -07003578 }
3579
Steven2ee2d572017-07-21 16:38:41 -07003580 vum->debug = onoff;
3581
3582done:
Steven388e51a2017-06-01 12:49:23 -07003583 unformat_free (line_input);
3584
3585 return error;
3586}
3587
3588/* *INDENT-OFF* */
3589VLIB_CLI_COMMAND (debug_vhost_user_command, static) = {
3590 .path = "debug vhost-user",
3591 .short_help = "debug vhost-user <on | off>",
3592 .function = debug_vhost_user_command_fn,
3593};
3594/* *INDENT-ON* */
3595
Ed Warnickecb9cada2015-12-08 15:45:58 -07003596static clib_error_t *
3597vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
3598{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003599 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003600
3601 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3602 {
3603 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003604 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003605 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003606 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003607 else if (unformat (input, "dont-dump-memory"))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003608 vum->dont_dump_vhost_user_memory = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003609 else
Damjan Marion00a9dca2016-08-17 17:05:46 +02003610 return clib_error_return (0, "unknown input `%U'",
3611 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003612 }
3613
3614 return 0;
3615}
3616
3617/* vhost-user { ... } configuration. */
3618VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3619
3620void
3621vhost_user_unmap_all (void)
3622{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003623 vhost_user_main_t *vum = &vhost_user_main;
3624 vhost_user_intf_t *vui;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003625
3626 if (vum->dont_dump_vhost_user_memory)
3627 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003628 pool_foreach (vui, vum->vhost_user_interfaces,
3629 unmap_all_mem_regions (vui);
3630 );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003631 }
3632}
Damjan Marion00a9dca2016-08-17 17:05:46 +02003633
3634/*
3635 * fd.io coding-style-patch-verification: ON
3636 *
3637 * Local Variables:
3638 * eval: (c-set-style "gnu")
3639 * End:
3640 */