blob: 451ae4342b6c7a777021ed29a4b62a66e44befba [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
Ed Warnickecb9cada2015-12-08 15:45:58 -070052#define VHOST_USER_DEBUG_SOCKET 0
Pierre Pfister116ea4b2016-11-08 15:49:28 +000053#define VHOST_DEBUG_VQ 0
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
55#if VHOST_USER_DEBUG_SOCKET == 1
56#define DBG_SOCK(args...) clib_warning(args);
57#else
58#define DBG_SOCK(args...)
59#endif
60
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) ? \
92 pool_elt_at_index (unix_main.file_pool, \
93 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
365static void
366vhost_user_rx_thread_placement ()
367{
368 vhost_user_main_t *vum = &vhost_user_main;
369 vhost_user_intf_t *vui;
370 vhost_cpu_t *vhc;
371 u32 *workers = 0;
Damjan Marion586afd72017-04-05 19:18:20 +0200372 u32 thread_index;
Steven7312cc72017-03-15 21:18:55 -0700373 vlib_main_t *vm;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100374
375 //Let's list all workers cpu indexes
376 u32 i;
377 for (i = vum->input_cpu_first_index;
378 i < vum->input_cpu_first_index + vum->input_cpu_count; i++)
379 {
Dave Barach80f54e22017-03-08 19:08:56 -0500380 vlib_node_set_state (vlib_mains[i], vhost_user_input_node.index,
Pierre Pfistere21c5282016-09-21 08:04:59 +0100381 VLIB_NODE_STATE_DISABLED);
382 vec_add1 (workers, i);
383 }
384
385 vec_foreach (vhc, vum->cpus)
386 {
387 vec_reset_length (vhc->rx_queues);
388 }
389
390 i = 0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000391 vhost_iface_and_queue_t iaq;
392 /* *INDENT-OFF* */
393 pool_foreach (vui, vum->vhost_user_interfaces, {
394 u32 *vui_workers = vec_len (vui->workers) ? vui->workers : workers;
395 u32 qid;
396 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
397 {
398 vhost_user_vring_t *txvq =
399 &vui->vrings[VHOST_VRING_IDX_TX (qid)];
400 if (!txvq->started)
401 continue;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100402
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000403 i %= vec_len (vui_workers);
Damjan Marion586afd72017-04-05 19:18:20 +0200404 thread_index = vui_workers[i];
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000405 i++;
Damjan Marion586afd72017-04-05 19:18:20 +0200406 vhc = &vum->cpus[thread_index];
Stevene4dcba82017-04-04 16:56:54 -0700407 txvq->interrupt_thread_index = thread_index;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100408
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000409 iaq.qid = qid;
410 iaq.vhost_iface_index = vui - vum->vhost_user_interfaces;
411 vec_add1 (vhc->rx_queues, iaq);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000412 }
413 });
414 /* *INDENT-ON* */
Steven7312cc72017-03-15 21:18:55 -0700415
416 vec_foreach (vhc, vum->cpus)
417 {
418 vhost_iface_and_queue_t *vhiq;
419 u8 mode = VHOST_USER_INTERRUPT_MODE;
420
421 vec_foreach (vhiq, vhc->rx_queues)
422 {
423 vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
424 if (vui->operation_mode == VHOST_USER_POLLING_MODE)
425 {
426 /* At least one interface is polling, cpu is set to polling */
427 mode = VHOST_USER_POLLING_MODE;
428 break;
429 }
430 }
431 vhc->operation_mode = mode;
432 }
433
Damjan Marion586afd72017-04-05 19:18:20 +0200434 for (thread_index = vum->input_cpu_first_index;
435 thread_index < vum->input_cpu_first_index + vum->input_cpu_count;
436 thread_index++)
Steven7312cc72017-03-15 21:18:55 -0700437 {
438 vlib_node_state_t state = VLIB_NODE_STATE_POLLING;
439
Damjan Marion586afd72017-04-05 19:18:20 +0200440 vhc = &vum->cpus[thread_index];
441 vm = vlib_mains ? vlib_mains[thread_index] : &vlib_global_main;
Steven7312cc72017-03-15 21:18:55 -0700442 switch (vhc->operation_mode)
443 {
444 case VHOST_USER_INTERRUPT_MODE:
445 state = VLIB_NODE_STATE_INTERRUPT;
446 break;
447 case VHOST_USER_POLLING_MODE:
448 state = VLIB_NODE_STATE_POLLING;
449 break;
450 default:
451 clib_warning ("BUG: bad operation mode %d", vhc->operation_mode);
452 break;
453 }
454 vlib_node_set_state (vm, vhost_user_input_node.index, state);
455 }
456
457 vec_free (workers);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100458}
459
460static int
461vhost_user_thread_placement (u32 sw_if_index, u32 worker_thread_index, u8 del)
462{
463 vhost_user_main_t *vum = &vhost_user_main;
464 vhost_user_intf_t *vui;
465 vnet_hw_interface_t *hw;
466
467 if (worker_thread_index < vum->input_cpu_first_index ||
468 worker_thread_index >=
469 vum->input_cpu_first_index + vum->input_cpu_count)
470 return -1;
471
472 if (!(hw = vnet_get_sup_hw_interface (vnet_get_main (), sw_if_index)))
473 return -2;
474
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000475 vui = pool_elt_at_index (vum->vhost_user_interfaces, hw->dev_instance);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100476 u32 found = ~0, *w;
477 vec_foreach (w, vui->workers)
478 {
479 if (*w == worker_thread_index)
480 {
481 found = w - vui->workers;
482 break;
483 }
484 }
485
486 if (del)
487 {
488 if (found == ~0)
489 return -3;
490 vec_del1 (vui->workers, found);
491 }
492 else if (found == ~0)
493 {
494 vec_add1 (vui->workers, worker_thread_index);
495 }
496
497 vhost_user_rx_thread_placement ();
498 return 0;
499}
500
501/** @brief Returns whether at least one TX and one RX vring are enabled */
502int
503vhost_user_intf_ready (vhost_user_intf_t * vui)
504{
505 int i, found[2] = { }; //RX + TX
506
507 for (i = 0; i < VHOST_VRING_MAX_N; i++)
508 if (vui->vrings[i].started && vui->vrings[i].enabled)
509 found[i & 1] = 1;
510
511 return found[0] && found[1];
512}
513
514static void
515vhost_user_update_iface_state (vhost_user_intf_t * vui)
516{
517 /* if we have pointers to descriptor table, go up */
518 int is_up = vhost_user_intf_ready (vui);
519 if (is_up != vui->is_up)
520 {
521 DBG_SOCK ("interface %d %s", vui->sw_if_index,
522 is_up ? "ready" : "down");
523 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
524 is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP :
525 0);
526 vui->is_up = is_up;
527 }
528 vhost_user_rx_thread_placement ();
529 vhost_user_tx_thread_placement (vui);
530}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
Steven7312cc72017-03-15 21:18:55 -0700532static void
533vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
534{
535 vhost_user_main_t *vum = &vhost_user_main;
536 vhost_cpu_t *vhc;
Damjan Marion586afd72017-04-05 19:18:20 +0200537 u32 thread_index;
Steven7312cc72017-03-15 21:18:55 -0700538 vlib_main_t *vm;
Stevene4dcba82017-04-04 16:56:54 -0700539 u32 ifq2, qid;
540 vhost_user_vring_t *txvq;
541
542 qid = ifq & 0xff;
543 if ((qid % 2) == 0)
544 /* Only care about the odd number virtqueue which is TX */
545 return;
Steven7312cc72017-03-15 21:18:55 -0700546
547 if (vhost_user_intf_ready (vui))
548 {
Stevene4dcba82017-04-04 16:56:54 -0700549 txvq = &vui->vrings[qid];
550 thread_index = txvq->interrupt_thread_index;
551 vhc = &vum->cpus[thread_index];
552 if (vhc->operation_mode == VHOST_USER_INTERRUPT_MODE)
Steven7312cc72017-03-15 21:18:55 -0700553 {
Stevene4dcba82017-04-04 16:56:54 -0700554 vm = vlib_mains ? vlib_mains[thread_index] : &vlib_global_main;
Steven7312cc72017-03-15 21:18:55 -0700555 /*
Stevene4dcba82017-04-04 16:56:54 -0700556 * Convert virtqueue number in the lower byte to vring
557 * queue index for the input node process. Top bytes contain
558 * the interface, lower byte contains the queue index.
Steven7312cc72017-03-15 21:18:55 -0700559 */
Stevene4dcba82017-04-04 16:56:54 -0700560 ifq2 = ((ifq >> 8) << 8) | qid / 2;
561 vhc->pending_input_bitmap =
562 clib_bitmap_set (vhc->pending_input_bitmap, ifq2, 1);
563 vlib_node_set_interrupt_pending (vm, vhost_user_input_node.index);
Steven7312cc72017-03-15 21:18:55 -0700564 }
Steven7312cc72017-03-15 21:18:55 -0700565 }
566}
567
Damjan Marion00a9dca2016-08-17 17:05:46 +0200568static clib_error_t *
569vhost_user_callfd_read_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200571 __attribute__ ((unused)) int n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 u8 buff[8];
Steven7312cc72017-03-15 21:18:55 -0700573 vhost_user_intf_t *vui =
574 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
575 uf->private_data >> 8);
576
Damjan Marion00a9dca2016-08-17 17:05:46 +0200577 n = read (uf->file_descriptor, ((char *) &buff), 8);
Steven7312cc72017-03-15 21:18:55 -0700578 DBG_SOCK ("if %d CALL queue %d", uf->private_data >> 8,
579 uf->private_data & 0xff);
580 vhost_user_set_interrupt_pending (vui, uf->private_data);
581
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 return 0;
583}
584
Pierre Pfistere21c5282016-09-21 08:04:59 +0100585static clib_error_t *
586vhost_user_kickfd_read_ready (unix_file_t * uf)
587{
588 __attribute__ ((unused)) int n;
589 u8 buff[8];
590 vhost_user_intf_t *vui =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000591 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
592 uf->private_data >> 8);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100593 u32 qid = uf->private_data & 0xff;
Steven7312cc72017-03-15 21:18:55 -0700594
Pierre Pfistere21c5282016-09-21 08:04:59 +0100595 n = read (uf->file_descriptor, ((char *) &buff), 8);
596 DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
Steven7312cc72017-03-15 21:18:55 -0700597 if (!vui->vrings[qid].started ||
598 (vhost_user_intf_ready (vui) != vui->is_up))
599 {
Stevene4dcba82017-04-04 16:56:54 -0700600 vlib_worker_thread_barrier_sync (vlib_get_main ());
Steven7312cc72017-03-15 21:18:55 -0700601 vui->vrings[qid].started = 1;
602 vhost_user_update_iface_state (vui);
Stevene4dcba82017-04-04 16:56:54 -0700603 vlib_worker_thread_barrier_release (vlib_get_main ());
Steven7312cc72017-03-15 21:18:55 -0700604 }
Steven7312cc72017-03-15 21:18:55 -0700605
606 vhost_user_set_interrupt_pending (vui, uf->private_data);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100607 return 0;
608}
609
610/**
611 * @brief Try once to lock the vring
612 * @return 0 on success, non-zero on failure.
613 */
614static inline int
615vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
616{
617 return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
618}
619
620/**
621 * @brief Spin until the vring is successfully locked
622 */
623static inline void
624vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
625{
626 while (vhost_user_vring_try_lock (vui, qid))
627 ;
628}
629
630/**
631 * @brief Unlock the vring lock
632 */
633static inline void
634vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
635{
636 *vui->vring_locks[qid] = 0;
637}
638
639static inline void
640vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
641{
642 vhost_user_vring_t *vring = &vui->vrings[qid];
643 memset (vring, 0, sizeof (*vring));
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000644 vring->kickfd_idx = ~0;
645 vring->callfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100646 vring->errfd = -1;
647
648 /*
649 * We have a bug with some qemu 2.5, and this may be a fix.
650 * Feel like interpretation holy text, but this is from vhost-user.txt.
651 * "
652 * One queue pair is enabled initially. More queues are enabled
653 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
654 * "
655 * Don't know who's right, but this is what DPDK does.
656 */
657 if (qid == 0 || qid == 1)
658 vring->enabled = 1;
659}
660
661static inline void
662vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
663{
664 vhost_user_vring_t *vring = &vui->vrings[qid];
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000665 if (vring->kickfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100666 {
667 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
668 vring->kickfd_idx);
669 unix_file_del (&unix_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000670 vring->kickfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100671 }
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000672 if (vring->callfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100673 {
674 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
675 vring->callfd_idx);
676 unix_file_del (&unix_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000677 vring->callfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100678 }
679 if (vring->errfd != -1)
Stevenf6dae052017-03-09 23:49:32 -0800680 {
681 close (vring->errfd);
682 vring->errfd = -1;
683 }
Pierre Pfistere21c5282016-09-21 08:04:59 +0100684 vhost_user_vring_init (vui, qid);
685}
686
Damjan Marion00a9dca2016-08-17 17:05:46 +0200687static inline void
688vhost_user_if_disconnect (vhost_user_intf_t * vui)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200690 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691 int q;
692
Damjan Marion00a9dca2016-08-17 17:05:46 +0200693 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694
Damjan Marion00a9dca2016-08-17 17:05:46 +0200695 if (vui->unix_file_index != ~0)
696 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
698 vui->unix_file_index = ~0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200699 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701 vui->is_up = 0;
Steve Shin44489572016-09-22 12:08:55 -0700702
Pierre Pfistere21c5282016-09-21 08:04:59 +0100703 for (q = 0; q < VHOST_VRING_MAX_N; q++)
704 vhost_user_vring_close (vui, q);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Damjan Marion00a9dca2016-08-17 17:05:46 +0200706 unmap_all_mem_regions (vui);
707 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700708}
709
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100710#define VHOST_LOG_PAGE 0x1000
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000711static_always_inline void
712vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
713 u64 addr, u64 len, u8 is_host_address)
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100714{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200715 if (PREDICT_TRUE (vui->log_base_addr == 0
716 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
717 {
718 return;
719 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000720 if (is_host_address)
721 {
Damjan Marion7bee80c2017-04-26 15:32:12 +0200722 addr = pointer_to_uword (map_user_mem (vui, (uword) addr));
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000723 }
Damjan Marion00a9dca2016-08-17 17:05:46 +0200724 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
725 {
726 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
727 return;
728 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100729
Damjan Marion00a9dca2016-08-17 17:05:46 +0200730 CLIB_MEMORY_BARRIER ();
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100731 u64 page = addr / VHOST_LOG_PAGE;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200732 while (page * VHOST_LOG_PAGE < addr + len)
733 {
734 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
735 page++;
736 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100737}
738
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000739static_always_inline void
740vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
741{
742 vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
743}
744
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100745#define vhost_user_log_dirty_ring(vui, vq, member) \
Yoann Desmouceauxfe2da0e2016-03-08 14:54:28 +0100746 if (PREDICT_FALSE(vq->log_used)) { \
Damjan Marion8d281b32016-08-24 14:32:39 +0200747 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
Yoann Desmouceauxfe2da0e2016-03-08 14:54:28 +0100748 sizeof(vq->used->member)); \
749 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100750
Damjan Marion00a9dca2016-08-17 17:05:46 +0200751static clib_error_t *
752vhost_user_socket_read (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753{
754 int n, i;
755 int fd, number_of_fds = 0;
756 int fds[VHOST_MEMORY_MAX_NREGIONS];
757 vhost_user_msg_t msg;
758 struct msghdr mh;
759 struct iovec iov[1];
Damjan Marion00a9dca2016-08-17 17:05:46 +0200760 vhost_user_main_t *vum = &vhost_user_main;
761 vhost_user_intf_t *vui;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762 struct cmsghdr *cmsg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763 u8 q;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200764 unix_file_t template = { 0 };
765 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000767 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768
Damjan Marion00a9dca2016-08-17 17:05:46 +0200769 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770
Damjan Marion00a9dca2016-08-17 17:05:46 +0200771 memset (&mh, 0, sizeof (mh));
772 memset (control, 0, sizeof (control));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773
Damjan Marion00a9dca2016-08-17 17:05:46 +0200774 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
Damjan Mariona290d7c2016-08-16 12:37:24 +0200775 fds[i] = -1;
776
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 /* set the payload */
778 iov[0].iov_base = (void *) &msg;
779 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
780
781 mh.msg_iov = iov;
782 mh.msg_iovlen = 1;
783 mh.msg_control = control;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200784 mh.msg_controllen = sizeof (control);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785
Damjan Marion00a9dca2016-08-17 17:05:46 +0200786 n = recvmsg (uf->file_descriptor, &mh, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787
Pierre Pfistere21c5282016-09-21 08:04:59 +0100788 /* Stop workers to avoid end of the world */
789 vlib_worker_thread_barrier_sync (vlib_get_main ());
790
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791 if (n != VHOST_USER_MSG_HDR_SZ)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100792 {
793 if (n == -1)
794 {
795 DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
796 }
797 else
798 {
799 DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
800 n, VHOST_USER_MSG_HDR_SZ);
801 }
802 goto close_socket;
803 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804
Damjan Marion00a9dca2016-08-17 17:05:46 +0200805 if (mh.msg_flags & MSG_CTRUNC)
806 {
Pierre Pfistere21c5282016-09-21 08:04:59 +0100807 DBG_SOCK ("MSG_CTRUNC is set");
Damjan Marion00a9dca2016-08-17 17:05:46 +0200808 goto close_socket;
809 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810
Damjan Marion00a9dca2016-08-17 17:05:46 +0200811 cmsg = CMSG_FIRSTHDR (&mh);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812
813 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
814 (cmsg->cmsg_type == SCM_RIGHTS) &&
Damjan Marion00a9dca2016-08-17 17:05:46 +0200815 (cmsg->cmsg_len - CMSG_LEN (0) <=
816 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
817 {
818 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
819 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
820 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821
Damjan Marion00a9dca2016-08-17 17:05:46 +0200822 /* version 1, no reply bit set */
823 if ((msg.flags & 7) != 1)
824 {
825 DBG_SOCK ("malformed message received. closing socket");
826 goto close_socket;
827 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828
829 {
Pierre Pfistere21c5282016-09-21 08:04:59 +0100830 int rv;
831 rv =
832 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
833 msg.size);
834 if (rv < 0)
835 {
836 DBG_SOCK ("read failed %s", strerror (errno));
837 goto close_socket;
838 }
839 else if (rv != msg.size)
840 {
841 DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
842 goto close_socket;
843 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700844 }
845
Damjan Marion00a9dca2016-08-17 17:05:46 +0200846 switch (msg.request)
847 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848 case VHOST_USER_GET_FEATURES:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700849 msg.flags |= 4;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100850 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
851 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
852 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
853 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
854 (1ULL << FEAT_VHOST_F_LOG_ALL) |
855 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
856 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
857 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
858 (1ULL << FEAT_VIRTIO_F_VERSION_1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859 msg.u64 &= vui->feature_mask;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200860 msg.size = sizeof (msg.u64);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100861 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
862 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863 break;
864
865 case VHOST_USER_SET_FEATURES:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200866 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
867 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868
869 vui->features = msg.u64;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100870
Pierre Pfistere21c5282016-09-21 08:04:59 +0100871 if (vui->features &
872 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
873 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200874 vui->virtio_net_hdr_sz = 12;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875 else
Damjan Marion00a9dca2016-08-17 17:05:46 +0200876 vui->virtio_net_hdr_sz = 10;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877
Damjan Marion00a9dca2016-08-17 17:05:46 +0200878 vui->is_any_layout =
879 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880
881 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200882 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883 vui->is_up = 0;
884
Pierre Pfistere21c5282016-09-21 08:04:59 +0100885 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
886 vhost_user_vring_close(&vui->vrings[q]); */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887
888 break;
889
890 case VHOST_USER_SET_MEM_TABLE:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200891 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
892 vui->hw_if_index, msg.memory.nregions);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893
894 if ((msg.memory.nregions < 1) ||
Damjan Marion00a9dca2016-08-17 17:05:46 +0200895 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
896 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897
Damjan Marion00a9dca2016-08-17 17:05:46 +0200898 DBG_SOCK ("number of mem regions must be between 1 and %i",
899 VHOST_MEMORY_MAX_NREGIONS);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900
Damjan Marion00a9dca2016-08-17 17:05:46 +0200901 goto close_socket;
902 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903
Damjan Marion00a9dca2016-08-17 17:05:46 +0200904 if (msg.memory.nregions != number_of_fds)
905 {
906 DBG_SOCK ("each memory region must have FD");
907 goto close_socket;
908 }
909 unmap_all_mem_regions (vui);
910 for (i = 0; i < msg.memory.nregions; i++)
911 {
912 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
913 sizeof (vhost_user_memory_region_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914
Damjan Marion00a9dca2016-08-17 17:05:46 +0200915 long page_sz = get_huge_page_size (fds[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916
Damjan Marion00a9dca2016-08-17 17:05:46 +0200917 /* align size to 2M page */
918 ssize_t map_sz = (vui->regions[i].memory_size +
919 vui->regions[i].mmap_offset +
Pierre Pfisterbed54892017-04-20 15:34:00 +0200920 page_sz - 1) & ~(page_sz - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921
Damjan Marion00a9dca2016-08-17 17:05:46 +0200922 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
923 MAP_SHARED, fds[i], 0);
Damjan Marion37623702016-09-20 11:25:27 +0200924 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
925 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
926 vui->regions[i].memory_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927
Damjan Marion00a9dca2016-08-17 17:05:46 +0200928 DBG_SOCK
929 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
930 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
931 page_sz);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932
Damjan Marion00a9dca2016-08-17 17:05:46 +0200933 if (vui->region_mmap_addr[i] == MAP_FAILED)
934 {
935 clib_warning ("failed to map memory. errno is %d", errno);
936 goto close_socket;
937 }
938 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
939 vui->region_mmap_fd[i] = fds[i];
940 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700941 vui->nregions = msg.memory.nregions;
942 break;
943
944 case VHOST_USER_SET_VRING_NUM:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200945 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
946 vui->hw_if_index, msg.state.index, msg.state.num);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947
Damjan Marion00a9dca2016-08-17 17:05:46 +0200948 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
949 (msg.state.num == 0) || /* it cannot be zero */
Pierre Pfistere21c5282016-09-21 08:04:59 +0100950 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200951 goto close_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952 vui->vrings[msg.state.index].qsz = msg.state.num;
953 break;
954
955 case VHOST_USER_SET_VRING_ADDR:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200956 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
957 vui->hw_if_index, msg.state.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700958
Pierre Pfistere21c5282016-09-21 08:04:59 +0100959 if (msg.state.index >= VHOST_VRING_MAX_N)
960 {
961 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
962 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
963 goto close_socket;
964 }
965
966 if (msg.size < sizeof (msg.addr))
967 {
968 DBG_SOCK ("vhost message is too short (%d < %d)",
969 msg.size, sizeof (msg.addr));
970 goto close_socket;
971 }
972
Damjan Marion00a9dca2016-08-17 17:05:46 +0200973 vui->vrings[msg.state.index].desc = (vring_desc_t *)
974 map_user_mem (vui, msg.addr.desc_user_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700975 vui->vrings[msg.state.index].used = (vring_used_t *)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200976 map_user_mem (vui, msg.addr.used_user_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977 vui->vrings[msg.state.index].avail = (vring_avail_t *)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200978 map_user_mem (vui, msg.addr.avail_user_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700979
980 if ((vui->vrings[msg.state.index].desc == NULL) ||
Damjan Marion00a9dca2016-08-17 17:05:46 +0200981 (vui->vrings[msg.state.index].used == NULL) ||
982 (vui->vrings[msg.state.index].avail == NULL))
983 {
984 DBG_SOCK ("failed to map user memory for hw_if_index %d",
985 vui->hw_if_index);
986 goto close_socket;
987 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100989 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
Yoann Desmouceauxfe2da0e2016-03-08 14:54:28 +0100990 vui->vrings[msg.state.index].log_used =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200991 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100992
993 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200994 the ring is initialized in an enabled state. */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200995 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
996 {
997 vui->vrings[msg.state.index].enabled = 1;
998 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100999
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000 vui->vrings[msg.state.index].last_used_idx =
Damjan Marion10eb1ea2016-10-13 10:02:19 +02001001 vui->vrings[msg.state.index].last_avail_idx =
Damjan Marion00a9dca2016-08-17 17:05:46 +02001002 vui->vrings[msg.state.index].used->idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003
Steven7312cc72017-03-15 21:18:55 -07001004 if (vui->operation_mode == VHOST_USER_POLLING_MODE)
1005 /* tell driver that we don't want interrupts */
1006 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
1007 else
1008 /* tell driver that we want interrupts */
1009 vui->vrings[msg.state.index].used->flags = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010 break;
1011
1012 case VHOST_USER_SET_OWNER:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001013 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014 break;
1015
1016 case VHOST_USER_RESET_OWNER:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001017 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018 break;
1019
1020 case VHOST_USER_SET_VRING_CALL:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001021 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
1022 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023
1024 q = (u8) (msg.u64 & 0xFF);
1025
Pierre Pfistere21c5282016-09-21 08:04:59 +01001026 /* if there is old fd, delete and close it */
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001027 if (vui->vrings[q].callfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +01001028 {
1029 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
1030 vui->vrings[q].callfd_idx);
1031 unix_file_del (&unix_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001032 vui->vrings[q].callfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001033 }
1034
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035 if (!(msg.u64 & 0x100))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001036 {
1037 if (number_of_fds != 1)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001038 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01001039 DBG_SOCK ("More than one fd received !");
1040 goto close_socket;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001041 }
Pierre Pfistere21c5282016-09-21 08:04:59 +01001042
Damjan Marion00a9dca2016-08-17 17:05:46 +02001043 template.read_function = vhost_user_callfd_read_ready;
1044 template.file_descriptor = fds[0];
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001045 template.private_data =
1046 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001047 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
1048 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049 else
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001050 vui->vrings[q].callfd_idx = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051 break;
1052
1053 case VHOST_USER_SET_VRING_KICK:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001054 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
1055 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056
1057 q = (u8) (msg.u64 & 0xFF);
1058
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001059 if (vui->vrings[q].kickfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +01001060 {
1061 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001062 vui->vrings[q].kickfd_idx);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001063 unix_file_del (&unix_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001064 vui->vrings[q].kickfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001065 }
1066
Ed Warnickecb9cada2015-12-08 15:45:58 -07001067 if (!(msg.u64 & 0x100))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001068 {
1069 if (number_of_fds != 1)
Pierre Pfistere21c5282016-09-21 08:04:59 +01001070 {
1071 DBG_SOCK ("More than one fd received !");
1072 goto close_socket;
1073 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074
Pierre Pfistere21c5282016-09-21 08:04:59 +01001075 template.read_function = vhost_user_kickfd_read_ready;
1076 template.file_descriptor = fds[0];
1077 template.private_data =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001078 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
1079 q;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001080 vui->vrings[q].kickfd_idx = unix_file_add (&unix_main, &template);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001081 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001082 else
Pierre Pfistere21c5282016-09-21 08:04:59 +01001083 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001084 //When no kickfd is set, the queue is initialized as started
1085 vui->vrings[q].kickfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001086 vui->vrings[q].started = 1;
1087 }
1088
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089 break;
1090
1091 case VHOST_USER_SET_VRING_ERR:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001092 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
1093 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094
1095 q = (u8) (msg.u64 & 0xFF);
1096
Pierre Pfistere21c5282016-09-21 08:04:59 +01001097 if (vui->vrings[q].errfd != -1)
1098 close (vui->vrings[q].errfd);
1099
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100 if (!(msg.u64 & 0x100))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001101 {
1102 if (number_of_fds != 1)
1103 goto close_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104
Pierre Pfistere21c5282016-09-21 08:04:59 +01001105 vui->vrings[q].errfd = fds[0];
Damjan Marion00a9dca2016-08-17 17:05:46 +02001106 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001107 else
Pierre Pfistere21c5282016-09-21 08:04:59 +01001108 vui->vrings[q].errfd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109
Ed Warnickecb9cada2015-12-08 15:45:58 -07001110 break;
1111
1112 case VHOST_USER_SET_VRING_BASE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001113 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1114 vui->hw_if_index, msg.state.index, msg.state.num);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115
1116 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
1117 break;
1118
1119 case VHOST_USER_GET_VRING_BASE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001120 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1121 vui->hw_if_index, msg.state.index, msg.state.num);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122
Pierre Pfistere21c5282016-09-21 08:04:59 +01001123 if (msg.state.index >= VHOST_VRING_MAX_N)
1124 {
1125 DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
1126 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1127 goto close_socket;
1128 }
1129
Stevenf6dae052017-03-09 23:49:32 -08001130 /*
1131 * Copy last_avail_idx from the vring before closing it because
1132 * closing the vring also initializes the vring last_avail_idx
1133 */
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001134 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001135 msg.flags |= 4;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001136 msg.size = sizeof (msg.state);
Stevenf6dae052017-03-09 23:49:32 -08001137
1138 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
1139 vhost_user_vring_close (vui, msg.state.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140 break;
1141
1142 case VHOST_USER_NONE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001143 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144
1145 break;
1146
1147 case VHOST_USER_SET_LOG_BASE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001148 {
1149 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001150
Damjan Marion00a9dca2016-08-17 17:05:46 +02001151 if (msg.size != sizeof (msg.log))
1152 {
1153 DBG_SOCK
1154 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
1155 msg.size, sizeof (msg.log));
1156 goto close_socket;
1157 }
1158
1159 if (!
1160 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
1161 {
1162 DBG_SOCK
1163 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1164 goto close_socket;
1165 }
1166
1167 fd = fds[0];
1168 /* align size to 2M page */
1169 long page_sz = get_huge_page_size (fd);
1170 ssize_t map_sz =
Pierre Pfisterbed54892017-04-20 15:34:00 +02001171 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001172
1173 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1174 MAP_SHARED, fd, 0);
1175
1176 DBG_SOCK
1177 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
1178 map_sz, msg.log.offset, fd, vui->log_base_addr);
1179
1180 if (vui->log_base_addr == MAP_FAILED)
1181 {
1182 clib_warning ("failed to map memory. errno is %d", errno);
1183 goto close_socket;
1184 }
1185
1186 vui->log_base_addr += msg.log.offset;
1187 vui->log_size = msg.log.size;
1188
1189 msg.flags |= 4;
1190 msg.size = sizeof (msg.u64);
1191
1192 break;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001193 }
1194
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195 case VHOST_USER_SET_LOG_FD:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001196 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197
1198 break;
1199
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001200 case VHOST_USER_GET_PROTOCOL_FEATURES:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001201 DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1202 vui->hw_if_index);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001203
1204 msg.flags |= 4;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001205 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1206 (1 << VHOST_USER_PROTOCOL_F_MQ);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001207 msg.size = sizeof (msg.u64);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001208 break;
1209
1210 case VHOST_USER_SET_PROTOCOL_FEATURES:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001211 DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
1212 vui->hw_if_index, msg.u64);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001213
1214 vui->protocol_features = msg.u64;
1215
1216 break;
1217
Pierre Pfistere21c5282016-09-21 08:04:59 +01001218 case VHOST_USER_GET_QUEUE_NUM:
1219 DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM", vui->hw_if_index);
1220 msg.flags |= 4;
1221 msg.u64 = VHOST_VRING_MAX_N;
1222 msg.size = sizeof (msg.u64);
1223 break;
1224
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001225 case VHOST_USER_SET_VRING_ENABLE:
Pierre Pfistere21c5282016-09-21 08:04:59 +01001226 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1227 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1228 msg.state.index);
1229 if (msg.state.index >= VHOST_VRING_MAX_N)
1230 {
1231 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
1232 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1233 goto close_socket;
1234 }
1235
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001236 vui->vrings[msg.state.index].enabled = msg.state.num;
1237 break;
1238
Ed Warnickecb9cada2015-12-08 15:45:58 -07001239 default:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001240 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1241 msg.request);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242 goto close_socket;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001243 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001244
Ed Warnickecb9cada2015-12-08 15:45:58 -07001245 /* if we need to reply */
1246 if (msg.flags & 4)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001247 {
1248 n =
1249 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
Pierre Pfistere21c5282016-09-21 08:04:59 +01001251 {
1252 DBG_SOCK ("could not send message response");
1253 goto close_socket;
1254 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02001255 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001256
Pierre Pfistere21c5282016-09-21 08:04:59 +01001257 vhost_user_update_iface_state (vui);
1258 vlib_worker_thread_barrier_release (vlib_get_main ());
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259 return 0;
1260
1261close_socket:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001262 vhost_user_if_disconnect (vui);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001263 vhost_user_update_iface_state (vui);
1264 vlib_worker_thread_barrier_release (vlib_get_main ());
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265 return 0;
1266}
1267
Damjan Marion00a9dca2016-08-17 17:05:46 +02001268static clib_error_t *
1269vhost_user_socket_error (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001270{
Pierre Pfistere21c5282016-09-21 08:04:59 +01001271 vlib_main_t *vm = vlib_get_main ();
Damjan Marion00a9dca2016-08-17 17:05:46 +02001272 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001273 vhost_user_intf_t *vui =
1274 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001275
Pierre Pfistere21c5282016-09-21 08:04:59 +01001276 DBG_SOCK ("socket error on if %d", vui->sw_if_index);
1277 vlib_worker_thread_barrier_sync (vm);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001278 vhost_user_if_disconnect (vui);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001279 vhost_user_rx_thread_placement ();
1280 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001281 return 0;
1282}
1283
Damjan Marion00a9dca2016-08-17 17:05:46 +02001284static clib_error_t *
1285vhost_user_socksvr_accept_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001286{
1287 int client_fd, client_len;
1288 struct sockaddr_un client;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001289 unix_file_t template = { 0 };
1290 vhost_user_main_t *vum = &vhost_user_main;
1291 vhost_user_intf_t *vui;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001292
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001293 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001294
Damjan Marion00a9dca2016-08-17 17:05:46 +02001295 client_len = sizeof (client);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001296 client_fd = accept (uf->file_descriptor,
Damjan Marion00a9dca2016-08-17 17:05:46 +02001297 (struct sockaddr *) &client,
1298 (socklen_t *) & client_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001299
1300 if (client_fd < 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001301 return clib_error_return_unix (0, "accept");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302
Pierre Pfistere21c5282016-09-21 08:04:59 +01001303 DBG_SOCK ("New client socket for vhost interface %d", vui->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304 template.read_function = vhost_user_socket_read;
1305 template.error_function = vhost_user_socket_error;
1306 template.file_descriptor = client_fd;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001307 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001308 vui->unix_file_index = unix_file_add (&unix_main, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001309 return 0;
1310}
1311
1312static clib_error_t *
1313vhost_user_init (vlib_main_t * vm)
1314{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001315 clib_error_t *error;
1316 vhost_user_main_t *vum = &vhost_user_main;
1317 vlib_thread_main_t *tm = vlib_get_thread_main ();
Damjan Marion0dafaa72016-09-20 23:21:02 +02001318 vlib_thread_registration_t *tr;
1319 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001320
1321 error = vlib_call_init_function (vm, ip4_init);
1322 if (error)
1323 return error;
1324
Ed Warnickecb9cada2015-12-08 15:45:58 -07001325 vum->coalesce_frames = 32;
1326 vum->coalesce_time = 1e-3;
1327
Pierre Pfistere21c5282016-09-21 08:04:59 +01001328 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001329
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001330 vhost_cpu_t *cpu;
1331 vec_foreach (cpu, vum->cpus)
1332 {
1333 /* This is actually not necessary as validate already zeroes it
1334 * Just keeping the loop here for later because I am lazy. */
1335 cpu->rx_buffers_len = 0;
1336 }
1337
Damjan Marion0dafaa72016-09-20 23:21:02 +02001338 /* find out which cpus will be used for input */
1339 vum->input_cpu_first_index = 0;
1340 vum->input_cpu_count = 1;
1341 p = hash_get_mem (tm->thread_registrations_by_name, "workers");
1342 tr = p ? (vlib_thread_registration_t *) p[0] : 0;
1343
1344 if (tr && tr->count > 0)
1345 {
1346 vum->input_cpu_first_index = tr->first_index;
1347 vum->input_cpu_count = tr->count;
1348 }
1349
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001350 vum->random = random_default_seed ();
1351
Steven5445f5f2017-04-25 16:16:00 -07001352 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1353
Ed Warnickecb9cada2015-12-08 15:45:58 -07001354 return 0;
1355}
1356
1357VLIB_INIT_FUNCTION (vhost_user_init);
1358
Damjan Marion00a9dca2016-08-17 17:05:46 +02001359static u8 *
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001360format_vhost_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001361{
1362 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1363 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001364 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
1365 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001366 vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001367 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
1368 t->device_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001369
Damjan Marion00a9dca2016-08-17 17:05:46 +02001370 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371
Ed Warnickecb9cada2015-12-08 15:45:58 -07001372 uword indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001373
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001374 s = format (s, "%U %U queue %d\n", format_white_space, indent,
Pierre Pfistere21c5282016-09-21 08:04:59 +01001375 format_vnet_sw_interface_name, vnm, sw, t->qid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001377 s = format (s, "%U virtio flags:\n", format_white_space, indent);
1378#define _(n,i,st) \
1379 if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
1380 s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
1381 foreach_virtio_trace_flags
1382#undef _
1383 s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
1384 format_white_space, indent, t->first_desc_len);
1385
1386 s = format (s, "%U flags 0x%02x gso_type %u\n",
Damjan Marion00a9dca2016-08-17 17:05:46 +02001387 format_white_space, indent,
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001388 t->hdr.hdr.flags, t->hdr.hdr.gso_type);
1389
1390 if (vui->virtio_net_hdr_sz == 12)
1391 s = format (s, "%U num_buff %u",
1392 format_white_space, indent, t->hdr.num_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001393
1394 return s;
1395}
1396
Damjan Marion00a9dca2016-08-17 17:05:46 +02001397void
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001398vhost_user_rx_trace (vhost_trace_t * t,
1399 vhost_user_intf_t * vui, u16 qid,
1400 vlib_buffer_t * b, vhost_user_vring_t * txvq)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001402 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001403 u32 qsz_mask = txvq->qsz - 1;
1404 u32 last_avail_idx = txvq->last_avail_idx;
1405 u32 desc_current = txvq->avail->ring[last_avail_idx & qsz_mask];
1406 vring_desc_t *hdr_desc = 0;
1407 virtio_net_hdr_mrg_rxbuf_t *hdr;
1408 u32 hint = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001409
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001410 memset (t, 0, sizeof (*t));
1411 t->device_index = vui - vum->vhost_user_interfaces;
1412 t->qid = qid;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001413
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001414 hdr_desc = &txvq->desc[desc_current];
1415 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001416 {
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001417 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001418 /* Header is the first here */
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001419 hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
1420 }
1421 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1422 {
1423 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1424 }
1425 if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1426 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1427 {
1428 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1429 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001430
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001431 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001432
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001433 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
1434 {
1435 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
1436 }
1437 else
1438 {
1439 u32 len = vui->virtio_net_hdr_sz;
1440 memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001441 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001442}
1443
Damjan Marion00a9dca2016-08-17 17:05:46 +02001444static inline void
1445vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001446{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001447 vhost_user_main_t *vum = &vhost_user_main;
1448 u64 x = 1;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001449 int fd = UNIX_GET_FD (vq->callfd_idx);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001450 int rv __attribute__ ((unused));
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001451 /* TODO: pay attention to rv */
1452 rv = write (fd, &x, sizeof (x));
Damjan Marion00a9dca2016-08-17 17:05:46 +02001453 vq->n_since_last_int = 0;
1454 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001455}
1456
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001457static_always_inline u32
1458vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1459 u16 copy_len, u32 * map_hint)
1460{
1461 void *src0, *src1, *src2, *src3;
1462 if (PREDICT_TRUE (copy_len >= 4))
1463 {
1464 if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
1465 return 1;
1466 if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
1467 return 1;
1468
1469 while (PREDICT_TRUE (copy_len >= 4))
1470 {
1471 src0 = src2;
1472 src1 = src3;
1473
1474 if (PREDICT_FALSE
1475 (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
1476 return 1;
1477 if (PREDICT_FALSE
1478 (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
1479 return 1;
1480
1481 CLIB_PREFETCH (src2, 64, LOAD);
1482 CLIB_PREFETCH (src3, 64, LOAD);
1483
1484 clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
1485 clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
1486 copy_len -= 2;
1487 cpy += 2;
1488 }
1489 }
1490 while (copy_len)
1491 {
1492 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
1493 return 1;
1494 clib_memcpy ((void *) cpy->dst, src0, cpy->len);
1495 copy_len -= 1;
1496 cpy += 1;
1497 }
1498 return 0;
1499}
1500
1501/**
1502 * Try to discard packets from the tx ring (VPP RX path).
1503 * Returns the number of discarded packets.
1504 */
1505u32
1506vhost_user_rx_discard_packet (vlib_main_t * vm,
1507 vhost_user_intf_t * vui,
1508 vhost_user_vring_t * txvq, u32 discard_max)
1509{
1510 /*
1511 * On the RX side, each packet corresponds to one descriptor
1512 * (it is the same whether it is a shallow descriptor, chained, or indirect).
1513 * Therefore, discarding a packet is like discarding a descriptor.
1514 */
1515 u32 discarded_packets = 0;
1516 u32 avail_idx = txvq->avail->idx;
1517 u16 qsz_mask = txvq->qsz - 1;
1518 while (discarded_packets != discard_max)
1519 {
1520 if (avail_idx == txvq->last_avail_idx)
1521 goto out;
1522
1523 u16 desc_chain_head =
1524 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1525 txvq->last_avail_idx++;
1526 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
1527 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1528 vhost_user_log_dirty_ring (vui, txvq,
1529 ring[txvq->last_used_idx & qsz_mask]);
1530 txvq->last_used_idx++;
1531 discarded_packets++;
1532 }
1533
1534out:
1535 CLIB_MEMORY_BARRIER ();
1536 txvq->used->idx = txvq->last_used_idx;
1537 vhost_user_log_dirty_ring (vui, txvq, idx);
1538 return discarded_packets;
1539}
1540
1541/*
1542 * In case of overflow, we need to rewind the array of allocated buffers.
1543 */
1544static void
1545vhost_user_input_rewind_buffers (vlib_main_t * vm,
1546 vhost_cpu_t * cpu, vlib_buffer_t * b_head)
1547{
1548 u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1549 vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
1550 b_current->current_length = 0;
1551 b_current->flags = 0;
1552 while (b_current != b_head)
1553 {
1554 cpu->rx_buffers_len++;
1555 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1556 b_current = vlib_get_buffer (vm, bi_current);
1557 b_current->current_length = 0;
1558 b_current->flags = 0;
1559 }
Steven95827e42017-05-18 21:22:00 -07001560 cpu->rx_buffers_len++;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001561}
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001562
Damjan Marion00a9dca2016-08-17 17:05:46 +02001563static u32
1564vhost_user_if_input (vlib_main_t * vm,
1565 vhost_user_main_t * vum,
Pierre Pfistere21c5282016-09-21 08:04:59 +01001566 vhost_user_intf_t * vui,
1567 u16 qid, vlib_node_runtime_t * node)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001568{
Pierre Pfistere21c5282016-09-21 08:04:59 +01001569 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001570 u16 n_rx_packets = 0;
1571 u32 n_rx_bytes = 0;
1572 u16 n_left;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001573 u32 n_left_to_next, *to_next;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001574 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1575 u32 n_trace = vlib_get_trace_count (vm, node);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001576 u16 qsz_mask;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001577 u32 map_hint = 0;
Damjan Marion586afd72017-04-05 19:18:20 +02001578 u16 thread_index = vlib_get_thread_index ();
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001579 u16 copy_len = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001580
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001581 {
1582 /* do we have pending interrupts ? */
1583 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1584 f64 now = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001585
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001586 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1587 vhost_user_send_call (vm, txvq);
1588
1589 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1590 vhost_user_send_call (vm, rxvq);
1591 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001592
Damjan Marion00a9dca2016-08-17 17:05:46 +02001593 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001594 return 0;
1595
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001596 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1597
Ed Warnickecb9cada2015-12-08 15:45:58 -07001598 /* nothing to do */
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001599 if (PREDICT_FALSE (n_left == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001600 return 0;
1601
Pierre Pfistere21c5282016-09-21 08:04:59 +01001602 if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001603 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01001604 /*
1605 * Discard input packet if interface is admin down or vring is not
1606 * enabled.
1607 * "For example, for a networking device, in the disabled state
1608 * client must not supply any new RX packets, but must process
1609 * and discard any TX packets."
1610 */
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001611 vhost_user_rx_discard_packet (vm, vui, txvq,
1612 VHOST_USER_DOWN_DISCARD_COUNT);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001613 return 0;
1614 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001615
Pierre Pfistere21c5282016-09-21 08:04:59 +01001616 if (PREDICT_FALSE (n_left == txvq->qsz))
1617 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001618 /*
1619 * Informational error logging when VPP is not
1620 * receiving packets fast enough.
1621 */
Pierre Pfistere21c5282016-09-21 08:04:59 +01001622 vlib_error_count (vm, node->node_index,
1623 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1624 }
1625
Ed Warnickecb9cada2015-12-08 15:45:58 -07001626 qsz_mask = txvq->qsz - 1;
1627
Pierre Pfister328e99b2016-02-12 13:18:42 +00001628 if (n_left > VLIB_FRAME_SIZE)
1629 n_left = VLIB_FRAME_SIZE;
1630
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001631 /*
1632 * For small packets (<2kB), we will not need more than one vlib buffer
1633 * per packet. In case packets are bigger, we will just yeld at some point
1634 * in the loop and come back later. This is not an issue as for big packet,
1635 * processing cost really comes from the memory copy.
Pierre Pfister328e99b2016-02-12 13:18:42 +00001636 */
Damjan Marion586afd72017-04-05 19:18:20 +02001637 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len < n_left + 1))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001638 {
Damjan Marion586afd72017-04-05 19:18:20 +02001639 u32 curr_len = vum->cpus[thread_index].rx_buffers_len;
1640 vum->cpus[thread_index].rx_buffers_len +=
Damjan Marion00a9dca2016-08-17 17:05:46 +02001641 vlib_buffer_alloc_from_free_list (vm,
Damjan Marion586afd72017-04-05 19:18:20 +02001642 vum->cpus[thread_index].rx_buffers +
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001643 curr_len,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001644 VHOST_USER_RX_BUFFERS_N - curr_len,
Damjan Marion00a9dca2016-08-17 17:05:46 +02001645 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001646
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001647 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001648 (vum->cpus[thread_index].rx_buffers_len <
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001649 VHOST_USER_RX_BUFFER_STARVATION))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001650 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001651 /* In case of buffer starvation, discard some packets from the queue
1652 * and log the event.
1653 * We keep doing best effort for the remaining packets. */
Damjan Marion586afd72017-04-05 19:18:20 +02001654 u32 flush = (n_left + 1 > vum->cpus[thread_index].rx_buffers_len) ?
1655 n_left + 1 - vum->cpus[thread_index].rx_buffers_len : 1;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001656 flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1657
1658 n_left -= flush;
1659 vlib_increment_simple_counter (vnet_main.
1660 interface_main.sw_if_counters +
1661 VNET_INTERFACE_COUNTER_DROP,
Damjan Marion586afd72017-04-05 19:18:20 +02001662 vlib_get_thread_index (),
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001663 vui->sw_if_index, flush);
1664
1665 vlib_error_count (vm, vhost_user_input_node.index,
1666 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001667 }
1668 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001669
Damjan Marion00a9dca2016-08-17 17:05:46 +02001670 while (n_left > 0)
1671 {
1672 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001673
Damjan Marion00a9dca2016-08-17 17:05:46 +02001674 while (n_left > 0 && n_left_to_next > 0)
1675 {
1676 vlib_buffer_t *b_head, *b_current;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001677 u32 bi_current;
1678 u16 desc_current;
1679 u32 desc_data_offset;
1680 vring_desc_t *desc_table = txvq->desc;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001681
Damjan Marion586afd72017-04-05 19:18:20 +02001682 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len <= 1))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001683 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001684 /* Not enough rx_buffers
1685 * Note: We yeld on 1 so we don't need to do an additional
1686 * check for the next buffer prefetch.
1687 */
1688 n_left = 0;
1689 break;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001690 }
1691
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001692 desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
Damjan Marion586afd72017-04-05 19:18:20 +02001693 vum->cpus[thread_index].rx_buffers_len--;
1694 bi_current = (vum->cpus[thread_index].rx_buffers)
1695 [vum->cpus[thread_index].rx_buffers_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001696 b_head = b_current = vlib_get_buffer (vm, bi_current);
1697 to_next[0] = bi_current; //We do that now so we can forget about bi_current
1698 to_next++;
1699 n_left_to_next--;
1700
1701 vlib_prefetch_buffer_with_index (vm,
Damjan Marion586afd72017-04-05 19:18:20 +02001702 (vum->
1703 cpus[thread_index].rx_buffers)
1704 [vum->cpus[thread_index].
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001705 rx_buffers_len - 1], LOAD);
1706
1707 /* Just preset the used descriptor id and length for later */
1708 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_current;
1709 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1710 vhost_user_log_dirty_ring (vui, txvq,
1711 ring[txvq->last_used_idx & qsz_mask]);
1712
1713 /* The buffer should already be initialized */
1714 b_head->total_length_not_including_first_buffer = 0;
1715 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1716
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001717 if (PREDICT_FALSE (n_trace))
1718 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001719 //TODO: next_index is not exactly known at that point
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001720 vlib_trace_buffer (vm, node, next_index, b_head,
1721 /* follow_chain */ 0);
1722 vhost_trace_t *t0 =
1723 vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1724 vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1725 n_trace--;
1726 vlib_set_trace_count (vm, node, n_trace);
1727 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02001728
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001729 /* This depends on the setup but is very consistent
1730 * So I think the CPU branch predictor will make a pretty good job
1731 * at optimizing the decision. */
1732 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1733 {
1734 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1735 &map_hint);
1736 desc_current = 0;
1737 if (PREDICT_FALSE (desc_table == 0))
1738 {
Steven95827e42017-05-18 21:22:00 -07001739 vlib_error_count (vm, node->node_index,
1740 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001741 goto out;
1742 }
1743 }
1744
Damjan Marion00a9dca2016-08-17 17:05:46 +02001745 if (PREDICT_TRUE (vui->is_any_layout) ||
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001746 (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001747 {
1748 /* ANYLAYOUT or single buffer */
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001749 desc_data_offset = vui->virtio_net_hdr_sz;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001750 }
1751 else
1752 {
1753 /* CSR case without ANYLAYOUT, skip 1st buffer */
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001754 desc_data_offset = desc_table[desc_current].len;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001755 }
1756
Damjan Marion00a9dca2016-08-17 17:05:46 +02001757 while (1)
1758 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001759 /* Get more input if necessary. Or end of packet. */
1760 if (desc_data_offset == desc_table[desc_current].len)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001761 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001762 if (PREDICT_FALSE (desc_table[desc_current].flags &
1763 VIRTQ_DESC_F_NEXT))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001764 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001765 desc_current = desc_table[desc_current].next;
1766 desc_data_offset = 0;
1767 }
1768 else
1769 {
1770 goto out;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001771 }
1772 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001773
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001774 /* Get more output if necessary. Or end of packet. */
1775 if (PREDICT_FALSE
1776 (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1777 {
1778 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001779 (vum->cpus[thread_index].rx_buffers_len == 0))
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001780 {
Steven62411e72017-02-03 09:30:37 -08001781 /* Cancel speculation */
1782 to_next--;
1783 n_left_to_next++;
1784
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001785 /*
1786 * Checking if there are some left buffers.
1787 * If not, just rewind the used buffers and stop.
1788 * Note: Scheduled copies are not cancelled. This is
1789 * not an issue as they would still be valid. Useless,
1790 * but valid.
1791 */
1792 vhost_user_input_rewind_buffers (vm,
Damjan Marion586afd72017-04-05 19:18:20 +02001793 &vum->cpus
1794 [thread_index],
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001795 b_head);
1796 n_left = 0;
1797 goto stop;
1798 }
1799
1800 /* Get next output */
Damjan Marion586afd72017-04-05 19:18:20 +02001801 vum->cpus[thread_index].rx_buffers_len--;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001802 u32 bi_next =
Damjan Marion586afd72017-04-05 19:18:20 +02001803 (vum->cpus[thread_index].rx_buffers)[vum->cpus
1804 [thread_index].rx_buffers_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001805 b_current->next_buffer = bi_next;
1806 b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1807 bi_current = bi_next;
1808 b_current = vlib_get_buffer (vm, bi_current);
1809 }
1810
1811 /* Prepare a copy order executed later for the data */
Damjan Marion586afd72017-04-05 19:18:20 +02001812 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001813 copy_len++;
1814 u32 desc_data_l =
1815 desc_table[desc_current].len - desc_data_offset;
1816 cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1817 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
Steven025d4152017-05-16 21:26:13 -07001818 cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
1819 b_current->current_length);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001820 cpy->src = desc_table[desc_current].addr + desc_data_offset;
1821
1822 desc_data_offset += cpy->len;
1823
1824 b_current->current_length += cpy->len;
1825 b_head->total_length_not_including_first_buffer += cpy->len;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001826 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001827
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001828 out:
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001829 CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1830
1831 n_rx_bytes += b_head->total_length_not_including_first_buffer;
1832 n_rx_packets++;
1833
1834 b_head->total_length_not_including_first_buffer -=
1835 b_head->current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001836
Damjan Marion00a9dca2016-08-17 17:05:46 +02001837 /* consume the descriptor and return it as used */
1838 txvq->last_avail_idx++;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001839 txvq->last_used_idx++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001840
Damjan Marion00a9dca2016-08-17 17:05:46 +02001841 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001842
Damjan Marion00a9dca2016-08-17 17:05:46 +02001843 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1844 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001845 b_head->error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001846
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001847 {
1848 u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001849
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001850 /* redirect if feature path enabled */
1851 vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
Damjan Marion35af9e52017-03-06 12:02:50 +01001852 b_head);
Damjan Marion22311502016-10-28 20:30:15 +02001853
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001854 u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1855 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1856 to_next, n_left_to_next,
1857 bi, next0);
1858 }
Damjan Marion22311502016-10-28 20:30:15 +02001859
Damjan Marion00a9dca2016-08-17 17:05:46 +02001860 n_left--;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001861
1862 /*
1863 * Although separating memory copies from virtio ring parsing
1864 * is beneficial, we can offer to perform the copies from time
1865 * to time in order to free some space in the ring.
1866 */
1867 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
Pierre Pfistere21c5282016-09-21 08:04:59 +01001868 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001869 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001870 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001871 copy_len, &map_hint)))
1872 {
Steven95827e42017-05-18 21:22:00 -07001873 vlib_error_count (vm, node->node_index,
1874 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001875 }
1876 copy_len = 0;
1877
1878 /* give buffers back to driver */
1879 CLIB_MEMORY_BARRIER ();
1880 txvq->used->idx = txvq->last_used_idx;
1881 vhost_user_log_dirty_ring (vui, txvq, idx);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001882 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02001883 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001884 stop:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001885 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001886 }
1887
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001888 /* Do the memory copies */
1889 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001890 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001891 copy_len, &map_hint)))
1892 {
Steven95827e42017-05-18 21:22:00 -07001893 vlib_error_count (vm, node->node_index,
1894 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001895 }
Pierre Pfister328e99b2016-02-12 13:18:42 +00001896
1897 /* give buffers back to driver */
Damjan Marion00a9dca2016-08-17 17:05:46 +02001898 CLIB_MEMORY_BARRIER ();
Pierre Pfister328e99b2016-02-12 13:18:42 +00001899 txvq->used->idx = txvq->last_used_idx;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001900 vhost_user_log_dirty_ring (vui, txvq, idx);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001901
Ed Warnickecb9cada2015-12-08 15:45:58 -07001902 /* interrupt (call) handling */
Steven7312cc72017-03-15 21:18:55 -07001903 if ((txvq->callfd_idx != ~0) &&
1904 !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001905 {
1906 txvq->n_since_last_int += n_rx_packets;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001907
Damjan Marion00a9dca2016-08-17 17:05:46 +02001908 if (txvq->n_since_last_int > vum->coalesce_frames)
1909 vhost_user_send_call (vm, txvq);
1910 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001911
1912 /* increase rx counters */
1913 vlib_increment_combined_counter
Damjan Marion00a9dca2016-08-17 17:05:46 +02001914 (vnet_main.interface_main.combined_sw_if_counters
1915 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +02001916 vlib_get_thread_index (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001917
Damjan Marion586afd72017-04-05 19:18:20 +02001918 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
Damjan Marionb3bb1012017-02-28 21:55:28 +01001919
Ed Warnickecb9cada2015-12-08 15:45:58 -07001920 return n_rx_packets;
1921}
1922
1923static uword
1924vhost_user_input (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02001925 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001926{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001927 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001928 uword n_rx_packets = 0;
Damjan Marion586afd72017-04-05 19:18:20 +02001929 u32 thread_index = vlib_get_thread_index ();
Pierre Pfistere21c5282016-09-21 08:04:59 +01001930 vhost_iface_and_queue_t *vhiq;
Steven7312cc72017-03-15 21:18:55 -07001931 vhost_user_intf_t *vui;
1932 vhost_cpu_t *vhc;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001933
Damjan Marion586afd72017-04-05 19:18:20 +02001934 vhc = &vum->cpus[thread_index];
Steven7312cc72017-03-15 21:18:55 -07001935 if (PREDICT_TRUE (vhc->operation_mode == VHOST_USER_POLLING_MODE))
1936 {
Damjan Marion586afd72017-04-05 19:18:20 +02001937 vec_foreach (vhiq, vum->cpus[thread_index].rx_queues)
Steven7312cc72017-03-15 21:18:55 -07001938 {
1939 vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
1940 n_rx_packets += vhost_user_if_input (vm, vum, vui, vhiq->qid, node);
1941 }
1942 }
1943 else
1944 {
1945 int i;
1946
1947 /* *INDENT-OFF* */
1948 clib_bitmap_foreach (i, vhc->pending_input_bitmap, ({
1949 int qid = i & 0xff;
1950
1951 clib_bitmap_set (vhc->pending_input_bitmap, i, 0);
1952 vui = pool_elt_at_index (vum->vhost_user_interfaces, i >> 8);
1953 n_rx_packets += vhost_user_if_input (vm, vum, vui, qid, node);
1954 }));
1955 /* *INDENT-ON* */
1956 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001957 return n_rx_packets;
1958}
1959
Damjan Marion00a9dca2016-08-17 17:05:46 +02001960/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001961VLIB_REGISTER_NODE (vhost_user_input_node) = {
1962 .function = vhost_user_input,
1963 .type = VLIB_NODE_TYPE_INPUT,
1964 .name = "vhost-user-input",
Damjan Marion51327ac2016-11-09 11:59:42 +01001965 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001966
1967 /* Will be enabled if/when hardware is detected. */
1968 .state = VLIB_NODE_STATE_DISABLED,
1969
1970 .format_buffer = format_ethernet_header_with_length,
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001971 .format_trace = format_vhost_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001972
1973 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1974 .error_strings = vhost_user_input_func_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001975};
1976
Damjan Marion1c80e832016-05-11 23:07:18 +02001977VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001978/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +02001979
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001980
1981void
1982vhost_user_tx_trace (vhost_trace_t * t,
1983 vhost_user_intf_t * vui, u16 qid,
1984 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
1985{
1986 vhost_user_main_t *vum = &vhost_user_main;
1987 u32 qsz_mask = rxvq->qsz - 1;
1988 u32 last_avail_idx = rxvq->last_avail_idx;
1989 u32 desc_current = rxvq->avail->ring[last_avail_idx & qsz_mask];
1990 vring_desc_t *hdr_desc = 0;
1991 u32 hint = 0;
1992
1993 memset (t, 0, sizeof (*t));
1994 t->device_index = vui - vum->vhost_user_interfaces;
1995 t->qid = qid;
1996
1997 hdr_desc = &rxvq->desc[desc_current];
1998 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1999 {
2000 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002001 /* Header is the first here */
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002002 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
2003 }
2004 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
2005 {
2006 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
2007 }
2008 if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
2009 !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
2010 {
2011 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
2012 }
2013
2014 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
2015}
2016
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002017static_always_inline u32
2018vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
2019 u16 copy_len, u32 * map_hint)
2020{
2021 void *dst0, *dst1, *dst2, *dst3;
2022 if (PREDICT_TRUE (copy_len >= 4))
2023 {
2024 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
2025 return 1;
2026 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
2027 return 1;
2028 while (PREDICT_TRUE (copy_len >= 4))
2029 {
2030 dst0 = dst2;
2031 dst1 = dst3;
2032
2033 if (PREDICT_FALSE
2034 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
2035 return 1;
2036 if (PREDICT_FALSE
2037 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
2038 return 1;
2039
2040 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
2041 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
2042
2043 clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
2044 clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
2045
2046 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
2047 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
2048 copy_len -= 2;
2049 cpy += 2;
2050 }
2051 }
2052 while (copy_len)
2053 {
2054 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
2055 return 1;
2056 clib_memcpy (dst0, (void *) cpy->src, cpy->len);
2057 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
2058 copy_len -= 1;
2059 cpy += 1;
2060 }
2061 return 0;
2062}
2063
2064
Ed Warnickecb9cada2015-12-08 15:45:58 -07002065static uword
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002066vhost_user_tx (vlib_main_t * vm,
2067 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002068{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002069 u32 *buffers = vlib_frame_args (frame);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002070 u32 n_left = frame->n_vectors;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002071 vhost_user_main_t *vum = &vhost_user_main;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002072 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
2073 vhost_user_intf_t *vui =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002074 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
Pierre Pfistere21c5282016-09-21 08:04:59 +01002075 u32 qid = ~0;
2076 vhost_user_vring_t *rxvq;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002077 u16 qsz_mask;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002078 u8 error;
Damjan Marion586afd72017-04-05 19:18:20 +02002079 u32 thread_index = vlib_get_thread_index ();
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002080 u32 map_hint = 0;
2081 u8 retry = 8;
2082 u16 copy_len;
2083 u16 tx_headers_len;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002084
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002085 if (PREDICT_FALSE (!vui->admin_up))
2086 {
2087 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
2088 goto done3;
2089 }
2090
2091 if (PREDICT_FALSE (!vui->is_up))
Damjan Marion00a9dca2016-08-17 17:05:46 +02002092 {
2093 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
Pierre Pfistere21c5282016-09-21 08:04:59 +01002094 goto done3;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002095 }
Damjan Marion920ecc22016-01-12 18:34:24 +01002096
Pierre Pfistere21c5282016-09-21 08:04:59 +01002097 qid =
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002098 VHOST_VRING_IDX_RX (*vec_elt_at_index
Damjan Marion586afd72017-04-05 19:18:20 +02002099 (vui->per_cpu_tx_qid, vlib_get_thread_index ()));
Pierre Pfistere21c5282016-09-21 08:04:59 +01002100 rxvq = &vui->vrings[qid];
2101 if (PREDICT_FALSE (vui->use_tx_spinlock))
2102 vhost_user_vring_lock (vui, qid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002103
Damjan Marion00a9dca2016-08-17 17:05:46 +02002104 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002105
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002106retry:
2107 error = VHOST_USER_TX_FUNC_ERROR_NONE;
2108 tx_headers_len = 0;
2109 copy_len = 0;
2110 while (n_left > 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002111 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002112 vlib_buffer_t *b0, *current_b0;
2113 u16 desc_head, desc_index, desc_len;
2114 vring_desc_t *desc_table;
2115 uword buffer_map_addr;
2116 u32 buffer_len;
2117 u16 bytes_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002118
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002119 if (PREDICT_TRUE (n_left > 1))
2120 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2121
2122 b0 = vlib_get_buffer (vm, buffers[0]);
2123
2124 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002125 {
Damjan Marion586afd72017-04-05 19:18:20 +02002126 vum->cpus[thread_index].current_trace =
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002127 vlib_add_trace (vm, node, b0,
Damjan Marion586afd72017-04-05 19:18:20 +02002128 sizeof (*vum->cpus[thread_index].current_trace));
2129 vhost_user_tx_trace (vum->cpus[thread_index].current_trace,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002130 vui, qid / 2, b0, rxvq);
2131 }
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002132
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002133 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2134 {
2135 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2136 goto done;
2137 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002138
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002139 desc_table = rxvq->desc;
2140 desc_head = desc_index =
2141 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2142
2143 /* Go deeper in case of indirect descriptor
2144 * I don't know of any driver providing indirect for RX. */
2145 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2146 {
2147 if (PREDICT_FALSE
2148 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002149 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002150 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002151 goto done;
2152 }
2153 if (PREDICT_FALSE
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002154 (!(desc_table =
2155 map_guest_mem (vui, rxvq->desc[desc_index].addr,
2156 &map_hint))))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002157 {
2158 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2159 goto done;
2160 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002161 desc_index = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002162 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002163
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002164 desc_len = vui->virtio_net_hdr_sz;
2165 buffer_map_addr = desc_table[desc_index].addr;
2166 buffer_len = desc_table[desc_index].len;
2167
2168 {
2169 // Get a header from the header array
2170 virtio_net_hdr_mrg_rxbuf_t *hdr =
Damjan Marion586afd72017-04-05 19:18:20 +02002171 &vum->cpus[thread_index].tx_headers[tx_headers_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002172 tx_headers_len++;
2173 hdr->hdr.flags = 0;
2174 hdr->hdr.gso_type = 0;
2175 hdr->num_buffers = 1; //This is local, no need to check
2176
2177 // Prepare a copy order executed later for the header
Damjan Marion586afd72017-04-05 19:18:20 +02002178 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002179 copy_len++;
2180 cpy->len = vui->virtio_net_hdr_sz;
2181 cpy->dst = buffer_map_addr;
2182 cpy->src = (uword) hdr;
2183 }
2184
2185 buffer_map_addr += vui->virtio_net_hdr_sz;
2186 buffer_len -= vui->virtio_net_hdr_sz;
2187 bytes_left = b0->current_length;
2188 current_b0 = b0;
2189 while (1)
2190 {
2191 if (buffer_len == 0)
2192 { //Get new output
2193 if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2194 {
2195 //Next one is chained
2196 desc_index = desc_table[desc_index].next;
2197 buffer_map_addr = desc_table[desc_index].addr;
2198 buffer_len = desc_table[desc_index].len;
2199 }
2200 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2201 {
2202 virtio_net_hdr_mrg_rxbuf_t *hdr =
Damjan Marion586afd72017-04-05 19:18:20 +02002203 &vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002204
2205 //Move from available to used buffer
2206 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id =
2207 desc_head;
2208 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len =
2209 desc_len;
2210 vhost_user_log_dirty_ring (vui, rxvq,
2211 ring[rxvq->last_used_idx &
2212 qsz_mask]);
2213
2214 rxvq->last_avail_idx++;
2215 rxvq->last_used_idx++;
2216 hdr->num_buffers++;
2217 desc_len = 0;
2218
2219 if (PREDICT_FALSE
2220 (rxvq->last_avail_idx == rxvq->avail->idx))
2221 {
2222 //Dequeue queued descriptors for this packet
2223 rxvq->last_used_idx -= hdr->num_buffers - 1;
2224 rxvq->last_avail_idx -= hdr->num_buffers - 1;
2225 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2226 goto done;
2227 }
2228
2229 desc_table = rxvq->desc;
2230 desc_head = desc_index =
2231 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2232 if (PREDICT_FALSE
2233 (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2234 {
2235 //It is seriously unlikely that a driver will put indirect descriptor
2236 //after non-indirect descriptor.
2237 if (PREDICT_FALSE
2238 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2239 {
2240 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2241 goto done;
2242 }
2243 if (PREDICT_FALSE
2244 (!(desc_table =
2245 map_guest_mem (vui,
2246 rxvq->desc[desc_index].addr,
2247 &map_hint))))
2248 {
2249 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2250 goto done;
2251 }
2252 desc_index = 0;
2253 }
2254 buffer_map_addr = desc_table[desc_index].addr;
2255 buffer_len = desc_table[desc_index].len;
2256 }
2257 else
2258 {
2259 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2260 goto done;
2261 }
2262 }
2263
2264 {
Damjan Marion586afd72017-04-05 19:18:20 +02002265 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002266 copy_len++;
2267 cpy->len = bytes_left;
2268 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2269 cpy->dst = buffer_map_addr;
2270 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2271 current_b0->current_length - bytes_left;
2272
2273 bytes_left -= cpy->len;
2274 buffer_len -= cpy->len;
2275 buffer_map_addr += cpy->len;
2276 desc_len += cpy->len;
2277
Pierre Pfister14ac8012016-12-08 07:58:47 +00002278 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002279 }
2280
2281 // Check if vlib buffer has more data. If not, get more or break.
2282 if (PREDICT_TRUE (!bytes_left))
2283 {
2284 if (PREDICT_FALSE
2285 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2286 {
2287 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2288 bytes_left = current_b0->current_length;
2289 }
2290 else
2291 {
2292 //End of packet
2293 break;
2294 }
2295 }
2296 }
2297
2298 //Move from available to used ring
2299 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
2300 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
2301 vhost_user_log_dirty_ring (vui, rxvq,
2302 ring[rxvq->last_used_idx & qsz_mask]);
2303 rxvq->last_avail_idx++;
2304 rxvq->last_used_idx++;
2305
2306 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2307 {
Damjan Marion586afd72017-04-05 19:18:20 +02002308 vum->cpus[thread_index].current_trace->hdr =
2309 vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002310 }
2311
2312 n_left--; //At the end for error counting when 'goto done' is invoked
2313 buffers++;
2314 }
2315
2316done:
2317 //Do the memory copies
2318 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02002319 (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002320 copy_len, &map_hint)))
2321 {
Steven95827e42017-05-18 21:22:00 -07002322 vlib_error_count (vm, node->node_index,
2323 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002324 }
2325
2326 CLIB_MEMORY_BARRIER ();
2327 rxvq->used->idx = rxvq->last_used_idx;
2328 vhost_user_log_dirty_ring (vui, rxvq, idx);
2329
2330 /*
2331 * When n_left is set, error is always set to something too.
2332 * In case error is due to lack of remaining buffers, we go back up and
2333 * retry.
2334 * The idea is that it is better to waste some time on packets
2335 * that have been processed already than dropping them and get
2336 * more fresh packets with a good likelyhood that they will be dropped too.
2337 * This technique also gives more time to VM driver to pick-up packets.
2338 * In case the traffic flows from physical to virtual interfaces, this
2339 * technique will end-up leveraging the physical NIC buffer in order to
2340 * absorb the VM's CPU jitter.
2341 */
2342 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2343 {
2344 retry--;
2345 goto retry;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002346 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002347
Ed Warnickecb9cada2015-12-08 15:45:58 -07002348 /* interrupt (call) handling */
Steven7312cc72017-03-15 21:18:55 -07002349 if ((rxvq->callfd_idx != ~0) &&
2350 !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
Damjan Marion00a9dca2016-08-17 17:05:46 +02002351 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002352 rxvq->n_since_last_int += frame->n_vectors - n_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002353
Damjan Marion00a9dca2016-08-17 17:05:46 +02002354 if (rxvq->n_since_last_int > vum->coalesce_frames)
2355 vhost_user_send_call (vm, rxvq);
2356 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002357
Pierre Pfistere21c5282016-09-21 08:04:59 +01002358 vhost_user_vring_unlock (vui, qid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002359
Pierre Pfistere21c5282016-09-21 08:04:59 +01002360done3:
Damjan Marion00a9dca2016-08-17 17:05:46 +02002361 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2362 {
2363 vlib_error_count (vm, node->node_index, error, n_left);
2364 vlib_increment_simple_counter
2365 (vnet_main.interface_main.sw_if_counters
2366 + VNET_INTERFACE_COUNTER_DROP,
Damjan Marion586afd72017-04-05 19:18:20 +02002367 vlib_get_thread_index (), vui->sw_if_index, n_left);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002368 }
Pierre Pfister328e99b2016-02-12 13:18:42 +00002369
Ed Warnickecb9cada2015-12-08 15:45:58 -07002370 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2371 return frame->n_vectors;
2372}
2373
2374static clib_error_t *
Damjan Marion00a9dca2016-08-17 17:05:46 +02002375vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
2376 u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002377{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002378 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002379 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002380 vhost_user_main_t *vum = &vhost_user_main;
2381 vhost_user_intf_t *vui =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002382 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002383
2384 vui->admin_up = is_up;
2385
2386 if (is_up)
2387 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002388 VNET_HW_INTERFACE_FLAG_LINK_UP);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002389
2390 return /* no error */ 0;
2391}
2392
Damjan Marion00a9dca2016-08-17 17:05:46 +02002393/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002394VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2395 .name = "vhost-user",
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002396 .tx_function = vhost_user_tx,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002397 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2398 .tx_function_error_strings = vhost_user_tx_func_error_strings,
2399 .format_device_name = format_vhost_user_interface_name,
2400 .name_renumber = vhost_user_name_renumber,
2401 .admin_up_down_function = vhost_user_interface_admin_up_down,
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002402 .format_tx_trace = format_vhost_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002403};
2404
Damjan Marion1c80e832016-05-11 23:07:18 +02002405VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002406 vhost_user_tx)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002407/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +02002408
Ed Warnickecb9cada2015-12-08 15:45:58 -07002409static uword
2410vhost_user_process (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002411 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002412{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002413 vhost_user_main_t *vum = &vhost_user_main;
2414 vhost_user_intf_t *vui;
2415 struct sockaddr_un sun;
2416 int sockfd;
2417 unix_file_t template = { 0 };
2418 f64 timeout = 3153600000.0 /* 100 years */ ;
2419 uword *event_data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002420
Steven0d150bb2017-03-22 12:05:19 -07002421 sockfd = -1;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002422 sun.sun_family = AF_UNIX;
2423 template.read_function = vhost_user_socket_read;
2424 template.error_function = vhost_user_socket_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002425
Damjan Marion00a9dca2016-08-17 17:05:46 +02002426 while (1)
2427 {
2428 vlib_process_wait_for_event_or_clock (vm, timeout);
2429 vlib_process_get_events (vm, &event_data);
2430 vec_reset_length (event_data);
2431
2432 timeout = 3.0;
2433
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002434 /* *INDENT-OFF* */
2435 pool_foreach (vui, vum->vhost_user_interfaces, {
Damjan Marion00a9dca2016-08-17 17:05:46 +02002436
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002437 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
2438 if (vui->unix_file_index == ~0)
2439 {
Steven0d150bb2017-03-22 12:05:19 -07002440 if ((sockfd < 0) &&
2441 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
2442 {
2443 /*
2444 * 1st time error or new error for this interface,
2445 * spit out the message and record the error
2446 */
2447 if (!vui->sock_errno || (vui->sock_errno != errno))
2448 {
2449 clib_unix_warning
2450 ("Error: Could not open unix socket for %s",
2451 vui->sock_filename);
2452 vui->sock_errno = errno;
2453 }
2454 continue;
2455 }
2456
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002457 /* try to connect */
2458 strncpy (sun.sun_path, (char *) vui->sock_filename,
2459 sizeof (sun.sun_path) - 1);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002460
Andrew Yourtchenko0c3d4672017-01-03 16:52:22 +00002461 /* Avoid hanging VPP if the other end does not accept */
Dave Barach8f544962017-01-18 10:23:22 -05002462 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2463 clib_unix_warning ("fcntl");
2464
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002465 if (connect (sockfd, (struct sockaddr *) &sun,
2466 sizeof (struct sockaddr_un)) == 0)
2467 {
Andrew Yourtchenko0c3d4672017-01-03 16:52:22 +00002468 /* Set the socket to blocking as it was before */
Dave Barach8f544962017-01-18 10:23:22 -05002469 if (fcntl(sockfd, F_SETFL, 0) < 0)
2470 clib_unix_warning ("fcntl2");
2471
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002472 vui->sock_errno = 0;
2473 template.file_descriptor = sockfd;
2474 template.private_data =
2475 vui - vhost_user_main.vhost_user_interfaces;
2476 vui->unix_file_index = unix_file_add (&unix_main, &template);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002477
Steven0d150bb2017-03-22 12:05:19 -07002478 /* This sockfd is considered consumed */
2479 sockfd = -1;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002480 }
2481 else
2482 {
2483 vui->sock_errno = errno;
2484 }
2485 }
2486 else
2487 {
2488 /* check if socket is alive */
2489 int error = 0;
2490 socklen_t len = sizeof (error);
2491 int fd = UNIX_GET_FD(vui->unix_file_index);
2492 int retval =
2493 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002494
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002495 if (retval)
2496 {
2497 DBG_SOCK ("getsockopt returned %d", retval);
2498 vhost_user_if_disconnect (vui);
2499 }
2500 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02002501 }
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002502 });
2503 /* *INDENT-ON* */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002504 }
2505 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002506}
2507
Damjan Marion00a9dca2016-08-17 17:05:46 +02002508/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002509VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
2510 .function = vhost_user_process,
2511 .type = VLIB_NODE_TYPE_PROCESS,
2512 .name = "vhost-user-process",
2513};
Damjan Marion00a9dca2016-08-17 17:05:46 +02002514/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002515
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002516/**
2517 * Disables and reset interface structure.
2518 * It can then be either init again, or removed from used interfaces.
2519 */
2520static void
2521vhost_user_term_if (vhost_user_intf_t * vui)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002522{
Ole Troan553a4112017-01-10 10:07:04 +01002523 int q;
Steven5445f5f2017-04-25 16:16:00 -07002524 vhost_user_main_t *vum = &vhost_user_main;
Ole Troan553a4112017-01-10 10:07:04 +01002525
Pierre Pfister7a91b462016-11-21 12:50:38 +00002526 // Delete configured thread pinning
2527 vec_reset_length (vui->workers);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002528 // disconnect interface sockets
Damjan Marion00a9dca2016-08-17 17:05:46 +02002529 vhost_user_if_disconnect (vui);
Pierre Pfisterfbb2ef62016-11-16 02:43:29 +00002530 vhost_user_update_iface_state (vui);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002531
Ole Troan553a4112017-01-10 10:07:04 +01002532 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2533 {
2534 clib_mem_free ((void *) vui->vring_locks[q]);
2535 }
2536
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002537 if (vui->unix_server_index != ~0)
2538 {
2539 //Close server socket
2540 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
2541 vui->unix_server_index);
2542 unix_file_del (&unix_main, uf);
2543 vui->unix_server_index = ~0;
Steven53129422017-04-21 13:31:50 -07002544 unlink (vui->sock_filename);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002545 }
Steven5445f5f2017-04-25 16:16:00 -07002546
2547 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
2548 &vui->if_index);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002549}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002550
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002551int
2552vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
2553{
2554 vhost_user_main_t *vum = &vhost_user_main;
2555 vhost_user_intf_t *vui;
2556 int rv = 0;
2557 vnet_hw_interface_t *hwif;
2558
2559 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2560 hwif->dev_class_index != vhost_user_dev_class.index)
2561 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2562
2563 DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2564 hwif->name, hwif->dev_instance);
2565
2566 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2567
2568 // Disable and reset interface
2569 vhost_user_term_if (vui);
2570
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002571 // Reset renumbered iface
2572 if (hwif->dev_instance <
2573 vec_len (vum->show_dev_instance_by_real_dev_instance))
2574 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
2575
2576 // Delete ethernet interface
Ed Warnickecb9cada2015-12-08 15:45:58 -07002577 ethernet_delete_interface (vnm, vui->hw_if_index);
Wojciech Decd8e47872017-01-17 21:45:11 +01002578
2579 // Back to pool
2580 pool_put (vum->vhost_user_interfaces, vui);
2581
Ed Warnickecb9cada2015-12-08 15:45:58 -07002582 return rv;
2583}
2584
Steven53129422017-04-21 13:31:50 -07002585static clib_error_t *
2586vhost_user_exit (vlib_main_t * vm)
2587{
2588 vnet_main_t *vnm = vnet_get_main ();
2589 vhost_user_main_t *vum = &vhost_user_main;
2590 vhost_user_intf_t *vui;
2591
Steven41748862017-04-25 13:49:51 -07002592 vlib_worker_thread_barrier_sync (vlib_get_main ());
Steven53129422017-04-21 13:31:50 -07002593 /* *INDENT-OFF* */
2594 pool_foreach (vui, vum->vhost_user_interfaces, {
2595 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
2596 });
2597 /* *INDENT-ON* */
Steven41748862017-04-25 13:49:51 -07002598 vlib_worker_thread_barrier_release (vlib_get_main ());
Steven53129422017-04-21 13:31:50 -07002599 return 0;
2600}
2601
2602VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
2603
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002604/**
2605 * Open server unix socket on specified sock_filename.
2606 */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002607static int
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002608vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002609{
Pierre Pfister5afccb22016-07-25 14:32:02 +01002610 int rv = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002611 struct sockaddr_un un = { };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002612 int fd;
2613 /* create listening socket */
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002614 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2615 return VNET_API_ERROR_SYSCALL_ERROR_1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002616
2617 un.sun_family = AF_UNIX;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002618 strncpy ((char *) un.sun_path, (char *) sock_filename,
2619 sizeof (un.sun_path) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002620
2621 /* remove if exists */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002622 unlink ((char *) sock_filename);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002623
Damjan Marion00a9dca2016-08-17 17:05:46 +02002624 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2625 {
2626 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2627 goto error;
2628 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002629
Damjan Marion00a9dca2016-08-17 17:05:46 +02002630 if (listen (fd, 1) == -1)
2631 {
2632 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2633 goto error;
2634 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002635
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002636 *sock_fd = fd;
2637 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002638
2639error:
Damjan Marion00a9dca2016-08-17 17:05:46 +02002640 close (fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002641 return rv;
2642}
2643
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002644/**
2645 * Create ethernet interface for vhost user interface.
2646 */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002647static void
2648vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
2649 vhost_user_intf_t * vui, u8 * hwaddress)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002650{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002651 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002652 u8 hwaddr[6];
Damjan Marion00a9dca2016-08-17 17:05:46 +02002653 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002654
2655 /* create hw and sw interface */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002656 if (hwaddress)
2657 {
2658 clib_memcpy (hwaddr, hwaddress, 6);
2659 }
2660 else
2661 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002662 random_u32 (&vum->random);
2663 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
Damjan Marion00a9dca2016-08-17 17:05:46 +02002664 hwaddr[0] = 2;
2665 hwaddr[1] = 0xfe;
2666 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002667
2668 error = ethernet_register_interface
2669 (vnm,
2670 vhost_user_dev_class.index,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002671 vui - vum->vhost_user_interfaces /* device instance */ ,
2672 hwaddr /* ethernet address */ ,
2673 &vui->hw_if_index, 0 /* flag change */ );
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002674
Ed Warnickecb9cada2015-12-08 15:45:58 -07002675 if (error)
2676 clib_error_report (error);
Pierre Pfister328e99b2016-02-12 13:18:42 +00002677
2678 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
2679 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002680}
2681
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002682/*
2683 * Initialize vui with specified attributes
2684 */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002685static void
2686vhost_user_vui_init (vnet_main_t * vnm,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002687 vhost_user_intf_t * vui,
2688 int server_sock_fd,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002689 const char *sock_filename,
Stevena1a09012017-03-08 00:23:13 -08002690 u64 feature_mask, u32 * sw_if_index, u8 operation_mode)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002691{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002692 vnet_sw_interface_t *sw;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002693 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01002694 int q;
Steven5445f5f2017-04-25 16:16:00 -07002695 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002696
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002697 if (server_sock_fd != -1)
2698 {
2699 unix_file_t template = { 0 };
2700 template.read_function = vhost_user_socksvr_accept_ready;
2701 template.file_descriptor = server_sock_fd;
Steven5445f5f2017-04-25 16:16:00 -07002702 template.private_data = vui - vum->vhost_user_interfaces; //hw index
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002703 vui->unix_server_index = unix_file_add (&unix_main, &template);
2704 }
2705 else
2706 {
2707 vui->unix_server_index = ~0;
2708 }
2709
Ed Warnickecb9cada2015-12-08 15:45:58 -07002710 vui->sw_if_index = sw->sw_if_index;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002711 strncpy (vui->sock_filename, sock_filename,
2712 ARRAY_LEN (vui->sock_filename) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002713 vui->sock_errno = 0;
2714 vui->is_up = 0;
2715 vui->feature_mask = feature_mask;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002716 vui->unix_file_index = ~0;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01002717 vui->log_base_addr = 0;
Stevena1a09012017-03-08 00:23:13 -08002718 vui->operation_mode = operation_mode;
Steven5445f5f2017-04-25 16:16:00 -07002719 vui->if_index = vui - vum->vhost_user_interfaces;
2720 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
2721 &vui->if_index, 0);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01002722
Pierre Pfistere21c5282016-09-21 08:04:59 +01002723 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2724 vhost_user_vring_init (vui, q);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002725
Damjan Marion00a9dca2016-08-17 17:05:46 +02002726 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002727
2728 if (sw_if_index)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002729 *sw_if_index = vui->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002730
Pierre Pfistere21c5282016-09-21 08:04:59 +01002731 for (q = 0; q < VHOST_VRING_MAX_N; q++)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002732 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01002733 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
2734 CLIB_CACHE_LINE_BYTES);
2735 memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002736 }
Pierre Pfistere21c5282016-09-21 08:04:59 +01002737
2738 vec_validate (vui->per_cpu_tx_qid,
2739 vlib_get_thread_main ()->n_vlib_mains - 1);
2740 vhost_user_tx_thread_placement (vui);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002741}
2742
Steven7312cc72017-03-15 21:18:55 -07002743static uword
2744vhost_user_send_interrupt_process (vlib_main_t * vm,
2745 vlib_node_runtime_t * rt, vlib_frame_t * f)
2746{
2747 vhost_user_intf_t *vui;
2748 f64 timeout = 3153600000.0 /* 100 years */ ;
2749 uword event_type, *event_data = 0;
2750 vhost_user_main_t *vum = &vhost_user_main;
2751 vhost_iface_and_queue_t *vhiq;
2752 vhost_cpu_t *vhc;
2753 f64 now, poll_time_remaining;
2754
2755 while (1)
2756 {
2757 poll_time_remaining =
2758 vlib_process_wait_for_event_or_clock (vm, timeout);
2759 event_type = vlib_process_get_events (vm, &event_data);
2760 vec_reset_length (event_data);
2761
2762 /*
2763 * Use the remaining timeout if it is less than coalesce time to avoid
2764 * resetting the existing timer in the middle of expiration
2765 */
2766 timeout = poll_time_remaining;
2767 if (vlib_process_suspend_time_is_zero (timeout) ||
2768 (timeout > vum->coalesce_time))
2769 timeout = vum->coalesce_time;
2770
2771 now = vlib_time_now (vm);
2772 switch (event_type)
2773 {
2774 case VHOST_USER_EVENT_START_TIMER:
2775 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
2776 break;
2777 /* fall through */
2778
2779 case ~0:
2780 vec_foreach (vhc, vum->cpus)
2781 {
Damjan Marion586afd72017-04-05 19:18:20 +02002782 u32 thread_index = vhc - vum->cpus;
Steven7312cc72017-03-15 21:18:55 -07002783 f64 next_timeout;
2784
2785 next_timeout = timeout;
Damjan Marion586afd72017-04-05 19:18:20 +02002786 vec_foreach (vhiq, vum->cpus[thread_index].rx_queues)
Steven7312cc72017-03-15 21:18:55 -07002787 {
2788 vui = &vum->vhost_user_interfaces[vhiq->vhost_iface_index];
2789 vhost_user_vring_t *rxvq =
2790 &vui->vrings[VHOST_VRING_IDX_RX (vhiq->qid)];
2791 vhost_user_vring_t *txvq =
2792 &vui->vrings[VHOST_VRING_IDX_TX (vhiq->qid)];
2793
2794 if (txvq->n_since_last_int)
2795 {
2796 if (now >= txvq->int_deadline)
2797 vhost_user_send_call (vm, txvq);
2798 else
2799 next_timeout = txvq->int_deadline - now;
2800 }
2801
2802 if (rxvq->n_since_last_int)
2803 {
2804 if (now >= rxvq->int_deadline)
2805 vhost_user_send_call (vm, rxvq);
2806 else
2807 next_timeout = rxvq->int_deadline - now;
2808 }
2809
2810 if ((next_timeout < timeout) && (next_timeout > 0.0))
2811 timeout = next_timeout;
2812 }
2813 }
2814 break;
2815
2816 default:
2817 clib_warning ("BUG: unhandled event type %d", event_type);
2818 break;
2819 }
Stevene4dcba82017-04-04 16:56:54 -07002820 /* No less than 1 millisecond */
2821 if (timeout < 1e-3)
2822 timeout = 1e-3;
Steven7312cc72017-03-15 21:18:55 -07002823 }
2824 return 0;
2825}
2826
2827/* *INDENT-OFF* */
2828VLIB_REGISTER_NODE (vhost_user_send_interrupt_node,static) = {
2829 .function = vhost_user_send_interrupt_process,
2830 .type = VLIB_NODE_TYPE_PROCESS,
2831 .name = "vhost-user-send-interrupt-process",
2832};
2833/* *INDENT-ON* */
2834
Damjan Marion00a9dca2016-08-17 17:05:46 +02002835int
2836vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2837 const char *sock_filename,
2838 u8 is_server,
2839 u32 * sw_if_index,
2840 u64 feature_mask,
Stevena1a09012017-03-08 00:23:13 -08002841 u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
2842 u8 operation_mode)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002843{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002844 vhost_user_intf_t *vui = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002845 u32 sw_if_idx = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002846 int rv = 0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002847 int server_sock_fd = -1;
Steven7312cc72017-03-15 21:18:55 -07002848 vhost_user_main_t *vum = &vhost_user_main;
Steven5445f5f2017-04-25 16:16:00 -07002849 uword *if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002850
Steven7312cc72017-03-15 21:18:55 -07002851 if ((operation_mode != VHOST_USER_POLLING_MODE) &&
2852 (operation_mode != VHOST_USER_INTERRUPT_MODE))
Stevena1a09012017-03-08 00:23:13 -08002853 return VNET_API_ERROR_UNIMPLEMENTED;
2854
Wojciech Dec3cd9eed2017-01-03 10:38:37 +01002855 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2856 {
2857 return VNET_API_ERROR_INVALID_ARGUMENT;
2858 }
2859
Steven5445f5f2017-04-25 16:16:00 -07002860 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2861 if (if_index)
2862 {
2863 if (sw_if_index)
2864 {
2865 vui = &vum->vhost_user_interfaces[*if_index];
2866 *sw_if_index = vui->sw_if_index;
2867 }
2868 return VNET_API_ERROR_IF_ALREADY_EXISTS;
2869 }
2870
Damjan Marion00a9dca2016-08-17 17:05:46 +02002871 if (is_server)
2872 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002873 if ((rv =
2874 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002875 {
2876 return rv;
2877 }
2878 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002879
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002880 pool_get (vhost_user_main.vhost_user_interfaces, vui);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002881
Pierre Pfisteref65cb02016-02-19 13:52:44 +00002882 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002883 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
Stevena1a09012017-03-08 00:23:13 -08002884 feature_mask, &sw_if_idx, operation_mode);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002885
Damjan Marion00a9dca2016-08-17 17:05:46 +02002886 if (renumber)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002887 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002888
2889 if (sw_if_index)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002890 *sw_if_index = sw_if_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002891
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002892 // Process node must connect
2893 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
Steven7312cc72017-03-15 21:18:55 -07002894
2895 if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
2896 !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
2897 (vum->coalesce_frames > 0))
2898 {
2899 vum->interrupt_mode = 1;
2900 vlib_process_signal_event (vm, vhost_user_send_interrupt_node.index,
2901 VHOST_USER_EVENT_START_TIMER, 0);
2902 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002903 return rv;
2904}
2905
Damjan Marion00a9dca2016-08-17 17:05:46 +02002906int
2907vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2908 const char *sock_filename,
2909 u8 is_server,
2910 u32 sw_if_index,
Stevena1a09012017-03-08 00:23:13 -08002911 u64 feature_mask, u8 renumber, u32 custom_dev_instance,
2912 u8 operation_mode)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002913{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002914 vhost_user_main_t *vum = &vhost_user_main;
2915 vhost_user_intf_t *vui = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002916 u32 sw_if_idx = ~0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002917 int server_sock_fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002918 int rv = 0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002919 vnet_hw_interface_t *hwif;
Steven5445f5f2017-04-25 16:16:00 -07002920 uword *if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002921
Steven7312cc72017-03-15 21:18:55 -07002922 if ((operation_mode != VHOST_USER_POLLING_MODE) &&
2923 (operation_mode != VHOST_USER_INTERRUPT_MODE))
Stevena1a09012017-03-08 00:23:13 -08002924 return VNET_API_ERROR_UNIMPLEMENTED;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002925 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2926 hwif->dev_class_index != vhost_user_dev_class.index)
2927 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002928
Steven5445f5f2017-04-25 16:16:00 -07002929 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2930 return VNET_API_ERROR_INVALID_ARGUMENT;
2931
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002932 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002933
Steven5445f5f2017-04-25 16:16:00 -07002934 /*
2935 * Disallow changing the interface to have the same path name
2936 * as other interface
2937 */
2938 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2939 if (if_index && (*if_index != vui->if_index))
2940 return VNET_API_ERROR_IF_ALREADY_EXISTS;
2941
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002942 // First try to open server socket
Damjan Marion00a9dca2016-08-17 17:05:46 +02002943 if (is_server)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002944 if ((rv = vhost_user_init_server_sock (sock_filename,
2945 &server_sock_fd)) != 0)
2946 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002947
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002948 vhost_user_term_if (vui);
2949 vhost_user_vui_init (vnm, vui, server_sock_fd,
Stevena1a09012017-03-08 00:23:13 -08002950 sock_filename, feature_mask, &sw_if_idx,
2951 operation_mode);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002952
Damjan Marion00a9dca2016-08-17 17:05:46 +02002953 if (renumber)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002954 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002955
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002956 // Process node must connect
2957 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
Steven7312cc72017-03-15 21:18:55 -07002958
2959 if ((operation_mode == VHOST_USER_INTERRUPT_MODE) &&
2960 !vum->interrupt_mode && (vum->coalesce_time > 0.0) &&
2961 (vum->coalesce_frames > 0))
2962 {
2963 vum->interrupt_mode = 1;
2964 vlib_process_signal_event (vm, vhost_user_send_interrupt_node.index,
2965 VHOST_USER_EVENT_START_TIMER, 0);
2966 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002967 return rv;
2968}
2969
Steven7312cc72017-03-15 21:18:55 -07002970static uword
2971unformat_vhost_user_operation_mode (unformat_input_t * input, va_list * args)
2972{
2973 u8 *operation_mode = va_arg (*args, u8 *);
2974 uword rc = 1;
2975
2976 if (unformat (input, "interrupt"))
2977 *operation_mode = VHOST_USER_INTERRUPT_MODE;
2978 else if (unformat (input, "polling"))
2979 *operation_mode = VHOST_USER_POLLING_MODE;
2980 else
2981 rc = 0;
2982
2983 return rc;
2984}
2985
Ed Warnickecb9cada2015-12-08 15:45:58 -07002986clib_error_t *
2987vhost_user_connect_command_fn (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002988 unformat_input_t * input,
2989 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002990{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002991 unformat_input_t _line_input, *line_input = &_line_input;
2992 u8 *sock_filename = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002993 u32 sw_if_index;
2994 u8 is_server = 0;
Pierre Pfistere21c5282016-09-21 08:04:59 +01002995 u64 feature_mask = (u64) ~ (0ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002996 u8 renumber = 0;
2997 u32 custom_dev_instance = ~0;
Pierre Pfisteref65cb02016-02-19 13:52:44 +00002998 u8 hwaddr[6];
2999 u8 *hw = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -05003000 clib_error_t *error = NULL;
Stevena1a09012017-03-08 00:23:13 -08003001 u8 operation_mode = VHOST_USER_POLLING_MODE;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003002
3003 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +02003004 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003005 return 0;
3006
Damjan Marion00a9dca2016-08-17 17:05:46 +02003007 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3008 {
3009 if (unformat (line_input, "socket %s", &sock_filename))
3010 ;
3011 else if (unformat (line_input, "server"))
3012 is_server = 1;
3013 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
3014 ;
3015 else
3016 if (unformat
3017 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
3018 hw = hwaddr;
3019 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
3020 {
3021 renumber = 1;
3022 }
Steven7312cc72017-03-15 21:18:55 -07003023 else if (unformat (line_input, "mode %U",
3024 unformat_vhost_user_operation_mode, &operation_mode))
3025 ;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003026 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003027 {
3028 error = clib_error_return (0, "unknown input `%U'",
3029 format_unformat_error, line_input);
3030 goto done;
3031 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003032 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003033
Damjan Marion00a9dca2016-08-17 17:05:46 +02003034 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07003035
Pierre Pfister5afccb22016-07-25 14:32:02 +01003036 int rv;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003037 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
3038 is_server, &sw_if_index, feature_mask,
Stevena1a09012017-03-08 00:23:13 -08003039 renumber, custom_dev_instance, hw,
3040 operation_mode)))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003041 {
Billy McFalla9a20e72017-02-15 11:39:12 -05003042 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
3043 goto done;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003044 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003045
Damjan Marion00a9dca2016-08-17 17:05:46 +02003046 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
3047 sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -05003048
3049done:
3050 vec_free (sock_filename);
3051 unformat_free (line_input);
3052
3053 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003054}
3055
3056clib_error_t *
3057vhost_user_delete_command_fn (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02003058 unformat_input_t * input,
3059 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003060{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003061 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003062 u32 sw_if_index = ~0;
Pierre Pfisterece983d2016-11-21 12:52:22 +00003063 vnet_main_t *vnm = vnet_get_main ();
Billy McFalla9a20e72017-02-15 11:39:12 -05003064 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003065
3066 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +02003067 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003068 return 0;
3069
Damjan Marion00a9dca2016-08-17 17:05:46 +02003070 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3071 {
3072 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
3073 ;
Pierre Pfisterece983d2016-11-21 12:52:22 +00003074 else if (unformat
3075 (line_input, "%U", unformat_vnet_sw_interface, vnm,
3076 &sw_if_index))
3077 {
3078 vnet_hw_interface_t *hwif =
3079 vnet_get_sup_hw_interface (vnm, sw_if_index);
3080 if (hwif == NULL ||
3081 vhost_user_dev_class.index != hwif->dev_class_index)
Billy McFalla9a20e72017-02-15 11:39:12 -05003082 {
3083 error = clib_error_return (0, "Not a vhost interface");
3084 goto done;
3085 }
Pierre Pfisterece983d2016-11-21 12:52:22 +00003086 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003087 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003088 {
3089 error = clib_error_return (0, "unknown input `%U'",
3090 format_unformat_error, line_input);
3091 goto done;
3092 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003093 }
Billy McFalla9a20e72017-02-15 11:39:12 -05003094
Damjan Marion00a9dca2016-08-17 17:05:46 +02003095 vhost_user_delete_if (vnm, vm, sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -05003096
3097done:
3098 unformat_free (line_input);
3099
3100 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003101}
3102
Damjan Marion00a9dca2016-08-17 17:05:46 +02003103int
3104vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
3105 vhost_user_intf_details_t ** out_vuids)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003106{
3107 int rv = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003108 vhost_user_main_t *vum = &vhost_user_main;
3109 vhost_user_intf_t *vui;
3110 vhost_user_intf_details_t *r_vuids = NULL;
3111 vhost_user_intf_details_t *vuid = NULL;
3112 u32 *hw_if_indices = 0;
3113 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003114 u8 *s = NULL;
3115 int i;
3116
3117 if (!out_vuids)
Damjan Marion00a9dca2016-08-17 17:05:46 +02003118 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003119
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003120 pool_foreach (vui, vum->vhost_user_interfaces,
3121 vec_add1 (hw_if_indices, vui->hw_if_index);
3122 );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003123
Damjan Marion00a9dca2016-08-17 17:05:46 +02003124 for (i = 0; i < vec_len (hw_if_indices); i++)
3125 {
3126 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003127 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003128
Damjan Marion00a9dca2016-08-17 17:05:46 +02003129 vec_add2 (r_vuids, vuid, 1);
Stevena1a09012017-03-08 00:23:13 -08003130 vuid->operation_mode = vui->operation_mode;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003131 vuid->sw_if_index = vui->sw_if_index;
3132 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
3133 vuid->features = vui->features;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003134 vuid->num_regions = vui->nregions;
Marek Gradzki0578cd12017-02-13 14:19:51 +01003135 vuid->is_server = vui->unix_server_index != ~0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003136 vuid->sock_errno = vui->sock_errno;
3137 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
3138 ARRAY_LEN (vuid->sock_filename) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003139
Damjan Marion00a9dca2016-08-17 17:05:46 +02003140 s = format (s, "%v%c", hi->name, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003141
Damjan Marion00a9dca2016-08-17 17:05:46 +02003142 strncpy ((char *) vuid->if_name, (char *) s,
3143 ARRAY_LEN (vuid->if_name) - 1);
3144 _vec_len (s) = 0;
3145 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003146
3147 vec_free (s);
3148 vec_free (hw_if_indices);
3149
3150 *out_vuids = r_vuids;
3151
3152 return rv;
3153}
3154
Steven7312cc72017-03-15 21:18:55 -07003155static u8 *
3156format_vhost_user_operation_mode (u8 * s, va_list * va)
3157{
3158 int operation_mode = va_arg (*va, int);
3159
3160 switch (operation_mode)
3161 {
3162 case VHOST_USER_POLLING_MODE:
3163 s = format (s, "%s", "polling");
3164 break;
3165 case VHOST_USER_INTERRUPT_MODE:
3166 s = format (s, "%s", "interrupt");
3167 break;
3168 default:
3169 s = format (s, "%s", "invalid");
3170 }
3171 return s;
3172}
3173
Ed Warnickecb9cada2015-12-08 15:45:58 -07003174clib_error_t *
3175show_vhost_user_command_fn (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02003176 unformat_input_t * input,
3177 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003178{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003179 clib_error_t *error = 0;
3180 vnet_main_t *vnm = vnet_get_main ();
3181 vhost_user_main_t *vum = &vhost_user_main;
3182 vhost_user_intf_t *vui;
3183 u32 hw_if_index, *hw_if_indices = 0;
3184 vnet_hw_interface_t *hi;
Pierre Pfistere21c5282016-09-21 08:04:59 +01003185 vhost_cpu_t *vhc;
3186 vhost_iface_and_queue_t *vhiq;
3187 u32 ci;
3188
Ed Warnickecb9cada2015-12-08 15:45:58 -07003189 int i, j, q;
3190 int show_descr = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003191 struct feat_struct
3192 {
3193 u8 bit;
3194 char *str;
3195 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07003196 struct feat_struct *feat_entry;
3197
3198 static struct feat_struct feat_array[] = {
3199#define _(s,b) { .str = #s, .bit = b, },
Damjan Marion00a9dca2016-08-17 17:05:46 +02003200 foreach_virtio_net_feature
Ed Warnickecb9cada2015-12-08 15:45:58 -07003201#undef _
Damjan Marion00a9dca2016-08-17 17:05:46 +02003202 {.str = NULL}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003203 };
3204
Pierre Pfistere21c5282016-09-21 08:04:59 +01003205#define foreach_protocol_feature \
3206 _(VHOST_USER_PROTOCOL_F_MQ) \
3207 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
3208
3209 static struct feat_struct proto_feat_array[] = {
3210#define _(s) { .str = #s, .bit = s},
3211 foreach_protocol_feature
3212#undef _
3213 {.str = NULL}
3214 };
3215
Damjan Marion00a9dca2016-08-17 17:05:46 +02003216 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3217 {
3218 if (unformat
3219 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
3220 {
3221 vec_add1 (hw_if_indices, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003222 }
3223 else if (unformat (input, "descriptors") || unformat (input, "desc"))
3224 show_descr = 1;
3225 else
3226 {
3227 error = clib_error_return (0, "unknown input `%U'",
3228 format_unformat_error, input);
3229 goto done;
3230 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003231 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003232 if (vec_len (hw_if_indices) == 0)
3233 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003234 pool_foreach (vui, vum->vhost_user_interfaces,
3235 vec_add1 (hw_if_indices, vui->hw_if_index);
3236 );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003237 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003238 vlib_cli_output (vm, "Virtio vhost-user interfaces");
Pierre Pfistere21c5282016-09-21 08:04:59 +01003239 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
Damjan Marion00a9dca2016-08-17 17:05:46 +02003240 vum->coalesce_frames, vum->coalesce_time);
3241
3242 for (i = 0; i < vec_len (hw_if_indices); i++)
3243 {
3244 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003245 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003246 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
3247 hi->name, hw_if_indices[i]);
3248
Pierre Pfistere21c5282016-09-21 08:04:59 +01003249 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
3250 " features mask (0x%llx): \n"
3251 " features (0x%llx): \n",
3252 vui->virtio_net_hdr_sz, vui->feature_mask,
3253 vui->features);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003254
3255 feat_entry = (struct feat_struct *) &feat_array;
3256 while (feat_entry->str)
3257 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01003258 if (vui->features & (1ULL << feat_entry->bit))
3259 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3260 feat_entry->bit);
3261 feat_entry++;
3262 }
3263
3264 vlib_cli_output (vm, " protocol features (0x%llx)",
3265 vui->protocol_features);
3266 feat_entry = (struct feat_struct *) &proto_feat_array;
3267 while (feat_entry->str)
3268 {
3269 if (vui->protocol_features & (1ULL << feat_entry->bit))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003270 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3271 feat_entry->bit);
3272 feat_entry++;
3273 }
3274
3275 vlib_cli_output (vm, "\n");
3276
Damjan Marion00a9dca2016-08-17 17:05:46 +02003277 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
3278 vui->sock_filename,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003279 (vui->unix_server_index != ~0) ? "server" : "client",
Damjan Marion00a9dca2016-08-17 17:05:46 +02003280 strerror (vui->sock_errno));
3281
Steven7312cc72017-03-15 21:18:55 -07003282 vlib_cli_output (vm, " configured mode: %U\n",
3283 format_vhost_user_operation_mode, vui->operation_mode);
Pierre Pfistere21c5282016-09-21 08:04:59 +01003284 vlib_cli_output (vm, " rx placement: ");
3285 vec_foreach (vhc, vum->cpus)
3286 {
3287 vec_foreach (vhiq, vhc->rx_queues)
3288 {
3289 if (vhiq->vhost_iface_index == vui - vum->vhost_user_interfaces)
Steven7312cc72017-03-15 21:18:55 -07003290 {
3291 vlib_cli_output (vm, " thread %d on vring %d\n",
3292 vhc - vum->cpus,
3293 VHOST_VRING_IDX_TX (vhiq->qid));
3294 vlib_cli_output (vm, " mode: %U\n",
3295 format_vhost_user_operation_mode,
3296 vhc->operation_mode);
3297 }
Pierre Pfistere21c5282016-09-21 08:04:59 +01003298 }
3299 }
3300
3301 vlib_cli_output (vm, " tx placement: %s\n",
3302 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
3303
3304 vec_foreach_index (ci, vui->per_cpu_tx_qid)
3305 {
3306 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
3307 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
3308 }
3309
3310 vlib_cli_output (vm, "\n");
3311
Damjan Marion00a9dca2016-08-17 17:05:46 +02003312 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
3313
3314 if (vui->nregions)
3315 {
3316 vlib_cli_output (vm,
3317 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
3318 vlib_cli_output (vm,
3319 " ====== ===== ================== ================== ================== ================== ==================\n");
3320 }
3321 for (j = 0; j < vui->nregions; j++)
3322 {
3323 vlib_cli_output (vm,
3324 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
3325 j, vui->region_mmap_fd[j],
3326 vui->regions[j].guest_phys_addr,
3327 vui->regions[j].memory_size,
3328 vui->regions[j].userspace_addr,
3329 vui->regions[j].mmap_offset,
3330 pointer_to_uword (vui->region_mmap_addr[j]));
3331 }
Pierre Pfistere21c5282016-09-21 08:04:59 +01003332 for (q = 0; q < VHOST_VRING_MAX_N; q++)
Damjan Marion00a9dca2016-08-17 17:05:46 +02003333 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01003334 if (!vui->vrings[q].started)
3335 continue;
3336
3337 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
3338 (q & 1) ? "RX" : "TX",
3339 vui->vrings[q].enabled ? "" : " disabled");
Damjan Marion00a9dca2016-08-17 17:05:46 +02003340
3341 vlib_cli_output (vm,
3342 " qsz %d last_avail_idx %d last_used_idx %d\n",
3343 vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
3344 vui->vrings[q].last_used_idx);
3345
3346 if (vui->vrings[q].avail && vui->vrings[q].used)
3347 vlib_cli_output (vm,
3348 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
3349 vui->vrings[q].avail->flags,
3350 vui->vrings[q].avail->idx,
3351 vui->vrings[q].used->flags,
3352 vui->vrings[q].used->idx);
3353
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003354 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
3355 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003356 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003357 kickfd, callfd, vui->vrings[q].errfd);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003358
3359 if (show_descr)
3360 {
3361 vlib_cli_output (vm, "\n descriptor table:\n");
3362 vlib_cli_output (vm,
3363 " id addr len flags next user_addr\n");
3364 vlib_cli_output (vm,
3365 " ===== ================== ===== ====== ===== ==================\n");
3366 for (j = 0; j < vui->vrings[q].qsz; j++)
3367 {
Pierre Pfister11f92052016-09-21 08:08:55 +01003368 u32 mem_hint = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003369 vlib_cli_output (vm,
3370 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3371 j, vui->vrings[q].desc[j].addr,
3372 vui->vrings[q].desc[j].len,
3373 vui->vrings[q].desc[j].flags,
3374 vui->vrings[q].desc[j].next,
3375 pointer_to_uword (map_guest_mem
3376 (vui,
Pierre Pfisterba1d0462016-07-27 16:38:20 +01003377 vui->vrings[q].desc[j].
Pierre Pfister11f92052016-09-21 08:08:55 +01003378 addr, &mem_hint)));
Damjan Marion00a9dca2016-08-17 17:05:46 +02003379 }
3380 }
3381 }
3382 vlib_cli_output (vm, "\n");
3383 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003384done:
3385 vec_free (hw_if_indices);
3386 return error;
3387}
3388
Damjan Marion8d281b32016-08-24 14:32:39 +02003389/*
3390 * CLI functions
3391 */
3392
Billy McFalla92501a2016-11-23 12:45:29 -05003393/*?
3394 * Create a vHost User interface. Once created, a new virtual interface
3395 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3396 * is the next free index.
3397 *
3398 * There are several parameters associated with a vHost interface:
3399 *
3400 * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3401 * VPP to manage the vHost interface. If socket does not already exist, VPP will
3402 * create the socket.
3403 *
3404 * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3405 * linux socket. If not provided, VPP will be the client.
3406 *
3407 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3408 * startup. By default, all supported features will be advertised. Otherwise,
3409 * provide the set of features desired.
3410 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3411 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3412 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3413 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3414 * - 0x004000000 (26) - VHOST_F_LOG_ALL
3415 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3416 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3417 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3418 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3419 *
3420 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3421 * X:X:X:X:X:X unix or X.X.X cisco format.
3422 *
3423 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3424 * in the name to be specified. If instance already exists, name will be used
3425 * anyway and multiple instances will have the same name. Use with caution.
3426 *
Steven7312cc72017-03-15 21:18:55 -07003427 * - <b>mode [interrupt | polling]</b> - Optional parameter specifying
3428 * the input thread polling policy.
3429 *
Billy McFalla92501a2016-11-23 12:45:29 -05003430 * @cliexpar
3431 * Example of how to create a vhost interface with VPP as the client and all features enabled:
3432 * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3433 * VirtualEthernet0/0/0
3434 * @cliexend
3435 * Example of how to create a vhost interface with VPP as the server and with just
3436 * multiple queues enabled:
3437 * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3438 * VirtualEthernet0/0/1
3439 * @cliexend
3440 * Once the vHost interface is created, enable the interface using:
3441 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3442?*/
Damjan Marion8d281b32016-08-24 14:32:39 +02003443/* *INDENT-OFF* */
3444VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3445 .path = "create vhost-user",
Steven7312cc72017-03-15 21:18:55 -07003446 .short_help = "create vhost-user socket <socket-filename> [server] "
3447 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] "
3448 "[mode {interrupt | polling}]",
Damjan Marion8d281b32016-08-24 14:32:39 +02003449 .function = vhost_user_connect_command_fn,
3450};
Billy McFalla92501a2016-11-23 12:45:29 -05003451/* *INDENT-ON* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003452
Billy McFalla92501a2016-11-23 12:45:29 -05003453/*?
3454 * Delete a vHost User interface using the interface name or the
Dave Barach13ad1f02017-03-26 19:36:18 -04003455 * software interface index. Use the '<em>show interface</em>'
Billy McFalla92501a2016-11-23 12:45:29 -05003456 * command to determine the software interface index. On deletion,
3457 * the linux socket will not be deleted.
3458 *
3459 * @cliexpar
3460 * Example of how to delete a vhost interface by name:
3461 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3462 * Example of how to delete a vhost interface by software interface index:
3463 * @cliexcmd{delete vhost-user sw_if_index 1}
3464?*/
3465/* *INDENT-OFF* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003466VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3467 .path = "delete vhost-user",
Billy McFalla92501a2016-11-23 12:45:29 -05003468 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
Damjan Marion8d281b32016-08-24 14:32:39 +02003469 .function = vhost_user_delete_command_fn,
3470};
3471
Billy McFalla92501a2016-11-23 12:45:29 -05003472/*?
3473 * Display the attributes of a single vHost User interface (provide interface
3474 * name), multiple vHost User interfaces (provide a list of interface names seperated
3475 * by spaces) or all Vhost User interfaces (omit an interface name to display all
3476 * vHost interfaces).
3477 *
3478 * @cliexpar
3479 * @parblock
3480 * Example of how to display a vhost interface:
3481 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3482 * Virtio vhost-user interfaces
3483 * Global:
3484 * coalesce frames 32 time 1e-3
3485 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3486 * virtio_net_hdr_sz 12
3487 * features mask (0xffffffffffffffff):
3488 * features (0x50408000):
3489 * VIRTIO_NET_F_MRG_RXBUF (15)
3490 * VIRTIO_NET_F_MQ (22)
3491 * VIRTIO_F_INDIRECT_DESC (28)
3492 * VHOST_USER_F_PROTOCOL_FEATURES (30)
3493 * protocol features (0x3)
3494 * VHOST_USER_PROTOCOL_F_MQ (0)
3495 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3496 *
3497 * socket filename /tmp/vhost1.sock type client errno "Success"
3498 *
3499 * rx placement:
3500 * thread 1 on vring 1
3501 * thread 1 on vring 5
3502 * thread 2 on vring 3
3503 * thread 2 on vring 7
3504 * tx placement: spin-lock
3505 * thread 0 on vring 0
3506 * thread 1 on vring 2
3507 * thread 2 on vring 0
3508 *
3509 * Memory regions (total 2)
3510 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3511 * ====== ===== ================== ================== ================== ================== ==================
3512 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3513 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3514 *
3515 * Virtqueue 0 (TX)
3516 * qsz 256 last_avail_idx 0 last_used_idx 0
3517 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3518 * kickfd 62 callfd 64 errfd -1
3519 *
3520 * Virtqueue 1 (RX)
3521 * qsz 256 last_avail_idx 0 last_used_idx 0
3522 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3523 * kickfd 65 callfd 66 errfd -1
3524 *
3525 * Virtqueue 2 (TX)
3526 * qsz 256 last_avail_idx 0 last_used_idx 0
3527 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3528 * kickfd 63 callfd 70 errfd -1
3529 *
3530 * Virtqueue 3 (RX)
3531 * qsz 256 last_avail_idx 0 last_used_idx 0
3532 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3533 * kickfd 72 callfd 74 errfd -1
3534 *
3535 * Virtqueue 4 (TX disabled)
3536 * qsz 256 last_avail_idx 0 last_used_idx 0
3537 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3538 * kickfd 76 callfd 78 errfd -1
3539 *
3540 * Virtqueue 5 (RX disabled)
3541 * qsz 256 last_avail_idx 0 last_used_idx 0
3542 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3543 * kickfd 80 callfd 82 errfd -1
3544 *
3545 * Virtqueue 6 (TX disabled)
3546 * qsz 256 last_avail_idx 0 last_used_idx 0
3547 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3548 * kickfd 84 callfd 86 errfd -1
3549 *
3550 * Virtqueue 7 (RX disabled)
3551 * qsz 256 last_avail_idx 0 last_used_idx 0
3552 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3553 * kickfd 88 callfd 90 errfd -1
3554 *
3555 * @cliexend
3556 *
3557 * The optional '<em>descriptors</em>' parameter will display the same output as
3558 * the previous example but will include the descriptor table for each queue.
3559 * The output is truncated below:
3560 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3561 * Virtio vhost-user interfaces
3562 * Global:
3563 * coalesce frames 32 time 1e-3
3564 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3565 * virtio_net_hdr_sz 12
3566 * features mask (0xffffffffffffffff):
3567 * features (0x50408000):
3568 * VIRTIO_NET_F_MRG_RXBUF (15)
3569 * VIRTIO_NET_F_MQ (22)
3570 * :
3571 * Virtqueue 0 (TX)
3572 * qsz 256 last_avail_idx 0 last_used_idx 0
3573 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3574 * kickfd 62 callfd 64 errfd -1
3575 *
3576 * descriptor table:
3577 * id addr len flags next user_addr
3578 * ===== ================== ===== ====== ===== ==================
3579 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3580 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3581 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3582 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3583 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3584 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3585 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3586 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3587 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3588 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3589 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3590 * :
3591 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3592 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3593 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3594 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3595 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3596 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3597 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3598 *
3599 * Virtqueue 1 (RX)
3600 * qsz 256 last_avail_idx 0 last_used_idx 0
3601 * :
3602 * @cliexend
3603 * @endparblock
3604?*/
3605/* *INDENT-OFF* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003606VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3607 .path = "show vhost-user",
Billy McFalla92501a2016-11-23 12:45:29 -05003608 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
Damjan Marion8d281b32016-08-24 14:32:39 +02003609 .function = show_vhost_user_command_fn,
3610};
3611/* *INDENT-ON* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003612
Ed Warnickecb9cada2015-12-08 15:45:58 -07003613static clib_error_t *
3614vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
3615{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003616 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003617
3618 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3619 {
3620 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003621 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003622 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003623 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003624 else if (unformat (input, "dont-dump-memory"))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003625 vum->dont_dump_vhost_user_memory = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003626 else
Damjan Marion00a9dca2016-08-17 17:05:46 +02003627 return clib_error_return (0, "unknown input `%U'",
3628 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003629 }
3630
3631 return 0;
3632}
3633
3634/* vhost-user { ... } configuration. */
3635VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3636
3637void
3638vhost_user_unmap_all (void)
3639{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003640 vhost_user_main_t *vum = &vhost_user_main;
3641 vhost_user_intf_t *vui;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003642
3643 if (vum->dont_dump_vhost_user_memory)
3644 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003645 pool_foreach (vui, vum->vhost_user_interfaces,
3646 unmap_all_mem_regions (vui);
3647 );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003648 }
3649}
Damjan Marion00a9dca2016-08-17 17:05:46 +02003650
Pierre Pfistere21c5282016-09-21 08:04:59 +01003651static clib_error_t *
3652vhost_thread_command_fn (vlib_main_t * vm,
3653 unformat_input_t * input, vlib_cli_command_t * cmd)
3654{
3655 unformat_input_t _line_input, *line_input = &_line_input;
3656 u32 worker_thread_index;
3657 u32 sw_if_index;
3658 u8 del = 0;
3659 int rv;
Billy McFalla9a20e72017-02-15 11:39:12 -05003660 clib_error_t *error = NULL;
Pierre Pfistere21c5282016-09-21 08:04:59 +01003661
3662 /* Get a line of input. */
3663 if (!unformat_user (input, unformat_line_input, line_input))
3664 return 0;
3665
3666 if (!unformat
3667 (line_input, "%U %d", unformat_vnet_sw_interface, vnet_get_main (),
3668 &sw_if_index, &worker_thread_index))
3669 {
Billy McFalla9a20e72017-02-15 11:39:12 -05003670 error = clib_error_return (0, "unknown input `%U'",
3671 format_unformat_error, line_input);
3672 goto done;
Pierre Pfistere21c5282016-09-21 08:04:59 +01003673 }
3674
3675 if (unformat (line_input, "del"))
3676 del = 1;
3677
3678 if ((rv =
3679 vhost_user_thread_placement (sw_if_index, worker_thread_index, del)))
Billy McFalla9a20e72017-02-15 11:39:12 -05003680 {
3681 error = clib_error_return (0, "vhost_user_thread_placement returned %d",
3682 rv);
3683 goto done;
3684 }
3685
3686done:
3687 unformat_free (line_input);
3688
3689 return error;
Pierre Pfistere21c5282016-09-21 08:04:59 +01003690}
3691
3692
Billy McFalla92501a2016-11-23 12:45:29 -05003693/*?
3694 * This command is used to move the RX processing for the given
3695 * interfaces to the provided thread. If the '<em>del</em>' option is used,
3696 * the forced thread assignment is removed and the thread assigment is
3697 * reassigned automatically. Use '<em>show vhost-user <interface></em>'
3698 * to see the thread assignment.
3699 *
3700 * @cliexpar
3701 * Example of how to move the RX processing for a given interface to a given thread:
3702 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1}
3703 * Example of how to remove the forced thread assignment for a given interface:
3704 * @cliexcmd{vhost thread VirtualEthernet0/0/0 1 del}
3705?*/
Pierre Pfistere21c5282016-09-21 08:04:59 +01003706/* *INDENT-OFF* */
3707VLIB_CLI_COMMAND (vhost_user_thread_command, static) = {
3708 .path = "vhost thread",
3709 .short_help = "vhost thread <iface> <worker-index> [del]",
3710 .function = vhost_thread_command_fn,
3711};
3712/* *INDENT-ON* */
3713
Damjan Marion00a9dca2016-08-17 17:05:46 +02003714/*
3715 * fd.io coding-style-patch-verification: ON
3716 *
3717 * Local Variables:
3718 * eval: (c-set-style "gnu")
3719 * End:
3720 */