blob: 231889342e83d95f99fe371aece5e1fa7a00533f [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
Stevenf3b53642017-05-01 14:03:02 -0700365/**
366 * @brief Unassign existing interface/queue to thread mappings and re-assign
367 * new interface/queue to thread mappings
368 */
Pierre Pfistere21c5282016-09-21 08:04:59 +0100369static void
370vhost_user_rx_thread_placement ()
371{
372 vhost_user_main_t *vum = &vhost_user_main;
373 vhost_user_intf_t *vui;
Stevenf3b53642017-05-01 14:03:02 -0700374 vhost_user_vring_t *txvq;
375 vnet_main_t *vnm = vnet_get_main ();
376 u32 qid;
377 int rv;
378 u16 *queue;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100379
Stevenf3b53642017-05-01 14:03:02 -0700380 // Scrap all existing mappings for all interfaces/queues
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000381 /* *INDENT-OFF* */
382 pool_foreach (vui, vum->vhost_user_interfaces, {
Stevenf3b53642017-05-01 14:03:02 -0700383 vec_foreach (queue, vui->rx_queues)
384 {
385 rv = vnet_hw_interface_unassign_rx_thread (vnm, vui->hw_if_index,
386 *queue);
387 if (rv)
388 clib_warning ("Warning: unable to unassign interface %d, "
389 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
390 }
391 vec_reset_length (vui->rx_queues);
392 });
393 /* *INDENT-ON* */
394
395 // Create the rx_queues for all interfaces
396 /* *INDENT-OFF* */
397 pool_foreach (vui, vum->vhost_user_interfaces, {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000398 for (qid = 0; qid < VHOST_VRING_MAX_N / 2; qid++)
399 {
Stevenf3b53642017-05-01 14:03:02 -0700400 txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
401 if (txvq->started)
402 {
403 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
404 /* Set polling as the default */
405 txvq->mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
406 vec_add1 (vui->rx_queues, qid);
407 }
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000408 }
409 });
410 /* *INDENT-ON* */
Steven7312cc72017-03-15 21:18:55 -0700411
Stevenf3b53642017-05-01 14:03:02 -0700412 // Assign new mappings for all interfaces/queues
413 /* *INDENT-OFF* */
414 pool_foreach (vui, vum->vhost_user_interfaces, {
415 vnet_hw_interface_set_input_node (vnm, vui->hw_if_index,
416 vhost_user_input_node.index);
417 vec_foreach (queue, vui->rx_queues)
Steven7312cc72017-03-15 21:18:55 -0700418 {
Stevenf3b53642017-05-01 14:03:02 -0700419 vnet_hw_interface_assign_rx_thread (vnm, vui->hw_if_index, *queue,
420 ~0);
421 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
422 rv = vnet_hw_interface_set_rx_mode (vnm, vui->hw_if_index, *queue,
423 txvq->mode);
424 if (rv)
425 clib_warning ("Warning: unable to set rx mode for interface %d, "
426 "queue %d: rc=%d", vui->hw_if_index, *queue, rv);
Steven7312cc72017-03-15 21:18:55 -0700427 }
Stevenf3b53642017-05-01 14:03:02 -0700428 });
429 /* *INDENT-ON* */
Pierre Pfistere21c5282016-09-21 08:04:59 +0100430}
431
432/** @brief Returns whether at least one TX and one RX vring are enabled */
433int
434vhost_user_intf_ready (vhost_user_intf_t * vui)
435{
436 int i, found[2] = { }; //RX + TX
437
438 for (i = 0; i < VHOST_VRING_MAX_N; i++)
439 if (vui->vrings[i].started && vui->vrings[i].enabled)
440 found[i & 1] = 1;
441
442 return found[0] && found[1];
443}
444
445static void
446vhost_user_update_iface_state (vhost_user_intf_t * vui)
447{
448 /* if we have pointers to descriptor table, go up */
449 int is_up = vhost_user_intf_ready (vui);
450 if (is_up != vui->is_up)
451 {
452 DBG_SOCK ("interface %d %s", vui->sw_if_index,
453 is_up ? "ready" : "down");
454 vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index,
455 is_up ? VNET_HW_INTERFACE_FLAG_LINK_UP :
456 0);
457 vui->is_up = is_up;
458 }
459 vhost_user_rx_thread_placement ();
460 vhost_user_tx_thread_placement (vui);
461}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Steven7312cc72017-03-15 21:18:55 -0700463static void
464vhost_user_set_interrupt_pending (vhost_user_intf_t * vui, u32 ifq)
465{
Stevenf3b53642017-05-01 14:03:02 -0700466 u32 qid;
467 vnet_main_t *vnm = vnet_get_main ();
Stevene4dcba82017-04-04 16:56:54 -0700468
469 qid = ifq & 0xff;
Stevenf3b53642017-05-01 14:03:02 -0700470 if ((qid & 1) == 0)
471 /* Only care about the odd number, or TX, virtqueue */
Stevene4dcba82017-04-04 16:56:54 -0700472 return;
Steven7312cc72017-03-15 21:18:55 -0700473
474 if (vhost_user_intf_ready (vui))
Stevenf3b53642017-05-01 14:03:02 -0700475 // qid >> 1 is to convert virtqueue number to vring queue index
476 vnet_device_input_set_interrupt_pending (vnm, vui->hw_if_index, qid >> 1);
Steven7312cc72017-03-15 21:18:55 -0700477}
478
Damjan Marion00a9dca2016-08-17 17:05:46 +0200479static clib_error_t *
480vhost_user_callfd_read_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200482 __attribute__ ((unused)) int n;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483 u8 buff[8];
Steven7312cc72017-03-15 21:18:55 -0700484
Damjan Marion00a9dca2016-08-17 17:05:46 +0200485 n = read (uf->file_descriptor, ((char *) &buff), 8);
Steven7312cc72017-03-15 21:18:55 -0700486 DBG_SOCK ("if %d CALL queue %d", uf->private_data >> 8,
487 uf->private_data & 0xff);
Steven7312cc72017-03-15 21:18:55 -0700488
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489 return 0;
490}
491
Pierre Pfistere21c5282016-09-21 08:04:59 +0100492static clib_error_t *
493vhost_user_kickfd_read_ready (unix_file_t * uf)
494{
495 __attribute__ ((unused)) int n;
496 u8 buff[8];
497 vhost_user_intf_t *vui =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000498 pool_elt_at_index (vhost_user_main.vhost_user_interfaces,
499 uf->private_data >> 8);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100500 u32 qid = uf->private_data & 0xff;
Steven7312cc72017-03-15 21:18:55 -0700501
Pierre Pfistere21c5282016-09-21 08:04:59 +0100502 n = read (uf->file_descriptor, ((char *) &buff), 8);
503 DBG_SOCK ("if %d KICK queue %d", uf->private_data >> 8, qid);
Steven7312cc72017-03-15 21:18:55 -0700504 if (!vui->vrings[qid].started ||
505 (vhost_user_intf_ready (vui) != vui->is_up))
506 {
Stevene4dcba82017-04-04 16:56:54 -0700507 vlib_worker_thread_barrier_sync (vlib_get_main ());
Steven7312cc72017-03-15 21:18:55 -0700508 vui->vrings[qid].started = 1;
509 vhost_user_update_iface_state (vui);
Stevene4dcba82017-04-04 16:56:54 -0700510 vlib_worker_thread_barrier_release (vlib_get_main ());
Steven7312cc72017-03-15 21:18:55 -0700511 }
Steven7312cc72017-03-15 21:18:55 -0700512
513 vhost_user_set_interrupt_pending (vui, uf->private_data);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100514 return 0;
515}
516
517/**
518 * @brief Try once to lock the vring
519 * @return 0 on success, non-zero on failure.
520 */
521static inline int
522vhost_user_vring_try_lock (vhost_user_intf_t * vui, u32 qid)
523{
524 return __sync_lock_test_and_set (vui->vring_locks[qid], 1);
525}
526
527/**
528 * @brief Spin until the vring is successfully locked
529 */
530static inline void
531vhost_user_vring_lock (vhost_user_intf_t * vui, u32 qid)
532{
533 while (vhost_user_vring_try_lock (vui, qid))
534 ;
535}
536
537/**
538 * @brief Unlock the vring lock
539 */
540static inline void
541vhost_user_vring_unlock (vhost_user_intf_t * vui, u32 qid)
542{
543 *vui->vring_locks[qid] = 0;
544}
545
546static inline void
547vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid)
548{
549 vhost_user_vring_t *vring = &vui->vrings[qid];
550 memset (vring, 0, sizeof (*vring));
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000551 vring->kickfd_idx = ~0;
552 vring->callfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100553 vring->errfd = -1;
554
555 /*
556 * We have a bug with some qemu 2.5, and this may be a fix.
557 * Feel like interpretation holy text, but this is from vhost-user.txt.
558 * "
559 * One queue pair is enabled initially. More queues are enabled
560 * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE.
561 * "
562 * Don't know who's right, but this is what DPDK does.
563 */
564 if (qid == 0 || qid == 1)
565 vring->enabled = 1;
566}
567
568static inline void
569vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid)
570{
571 vhost_user_vring_t *vring = &vui->vrings[qid];
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000572 if (vring->kickfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100573 {
574 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
575 vring->kickfd_idx);
576 unix_file_del (&unix_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000577 vring->kickfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100578 }
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000579 if (vring->callfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100580 {
581 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
582 vring->callfd_idx);
583 unix_file_del (&unix_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000584 vring->callfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100585 }
586 if (vring->errfd != -1)
Stevenf6dae052017-03-09 23:49:32 -0800587 {
588 close (vring->errfd);
589 vring->errfd = -1;
590 }
Pierre Pfistere21c5282016-09-21 08:04:59 +0100591 vhost_user_vring_init (vui, qid);
592}
593
Damjan Marion00a9dca2016-08-17 17:05:46 +0200594static inline void
595vhost_user_if_disconnect (vhost_user_intf_t * vui)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200597 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598 int q;
599
Damjan Marion00a9dca2016-08-17 17:05:46 +0200600 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601
Damjan Marion00a9dca2016-08-17 17:05:46 +0200602 if (vui->unix_file_index != ~0)
603 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700604 unix_file_del (&unix_main, unix_main.file_pool + vui->unix_file_index);
605 vui->unix_file_index = ~0;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200606 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 vui->is_up = 0;
Steve Shin44489572016-09-22 12:08:55 -0700609
Pierre Pfistere21c5282016-09-21 08:04:59 +0100610 for (q = 0; q < VHOST_VRING_MAX_N; q++)
611 vhost_user_vring_close (vui, q);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612
Damjan Marion00a9dca2016-08-17 17:05:46 +0200613 unmap_all_mem_regions (vui);
614 DBG_SOCK ("interface ifindex %d disconnected", vui->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615}
616
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100617#define VHOST_LOG_PAGE 0x1000
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000618static_always_inline void
619vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui,
620 u64 addr, u64 len, u8 is_host_address)
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100621{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200622 if (PREDICT_TRUE (vui->log_base_addr == 0
623 || !(vui->features & (1 << FEAT_VHOST_F_LOG_ALL))))
624 {
625 return;
626 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000627 if (is_host_address)
628 {
Damjan Marion7bee80c2017-04-26 15:32:12 +0200629 addr = pointer_to_uword (map_user_mem (vui, (uword) addr));
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000630 }
Damjan Marion00a9dca2016-08-17 17:05:46 +0200631 if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size))
632 {
633 DBG_SOCK ("vhost_user_log_dirty_pages(): out of range\n");
634 return;
635 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100636
Damjan Marion00a9dca2016-08-17 17:05:46 +0200637 CLIB_MEMORY_BARRIER ();
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100638 u64 page = addr / VHOST_LOG_PAGE;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200639 while (page * VHOST_LOG_PAGE < addr + len)
640 {
641 ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8;
642 page++;
643 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100644}
645
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +0000646static_always_inline void
647vhost_user_log_dirty_pages (vhost_user_intf_t * vui, u64 addr, u64 len)
648{
649 vhost_user_log_dirty_pages_2 (vui, addr, len, 0);
650}
651
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100652#define vhost_user_log_dirty_ring(vui, vq, member) \
Yoann Desmouceauxfe2da0e2016-03-08 14:54:28 +0100653 if (PREDICT_FALSE(vq->log_used)) { \
Damjan Marion8d281b32016-08-24 14:32:39 +0200654 vhost_user_log_dirty_pages(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \
Yoann Desmouceauxfe2da0e2016-03-08 14:54:28 +0100655 sizeof(vq->used->member)); \
656 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100657
Damjan Marion00a9dca2016-08-17 17:05:46 +0200658static clib_error_t *
659vhost_user_socket_read (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660{
661 int n, i;
662 int fd, number_of_fds = 0;
663 int fds[VHOST_MEMORY_MAX_NREGIONS];
664 vhost_user_msg_t msg;
665 struct msghdr mh;
666 struct iovec iov[1];
Damjan Marion00a9dca2016-08-17 17:05:46 +0200667 vhost_user_main_t *vum = &vhost_user_main;
668 vhost_user_intf_t *vui;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669 struct cmsghdr *cmsg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670 u8 q;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200671 unix_file_t template = { 0 };
672 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000674 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675
Damjan Marion00a9dca2016-08-17 17:05:46 +0200676 char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677
Damjan Marion00a9dca2016-08-17 17:05:46 +0200678 memset (&mh, 0, sizeof (mh));
679 memset (control, 0, sizeof (control));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
Damjan Marion00a9dca2016-08-17 17:05:46 +0200681 for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++)
Damjan Mariona290d7c2016-08-16 12:37:24 +0200682 fds[i] = -1;
683
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684 /* set the payload */
685 iov[0].iov_base = (void *) &msg;
686 iov[0].iov_len = VHOST_USER_MSG_HDR_SZ;
687
688 mh.msg_iov = iov;
689 mh.msg_iovlen = 1;
690 mh.msg_control = control;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200691 mh.msg_controllen = sizeof (control);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692
Damjan Marion00a9dca2016-08-17 17:05:46 +0200693 n = recvmsg (uf->file_descriptor, &mh, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700694
Pierre Pfistere21c5282016-09-21 08:04:59 +0100695 /* Stop workers to avoid end of the world */
696 vlib_worker_thread_barrier_sync (vlib_get_main ());
697
Ed Warnickecb9cada2015-12-08 15:45:58 -0700698 if (n != VHOST_USER_MSG_HDR_SZ)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100699 {
700 if (n == -1)
701 {
702 DBG_SOCK ("recvmsg returned error %d %s", errno, strerror (errno));
703 }
704 else
705 {
706 DBG_SOCK ("n (%d) != VHOST_USER_MSG_HDR_SZ (%d)",
707 n, VHOST_USER_MSG_HDR_SZ);
708 }
709 goto close_socket;
710 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
Damjan Marion00a9dca2016-08-17 17:05:46 +0200712 if (mh.msg_flags & MSG_CTRUNC)
713 {
Pierre Pfistere21c5282016-09-21 08:04:59 +0100714 DBG_SOCK ("MSG_CTRUNC is set");
Damjan Marion00a9dca2016-08-17 17:05:46 +0200715 goto close_socket;
716 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717
Damjan Marion00a9dca2016-08-17 17:05:46 +0200718 cmsg = CMSG_FIRSTHDR (&mh);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719
720 if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) &&
721 (cmsg->cmsg_type == SCM_RIGHTS) &&
Damjan Marion00a9dca2016-08-17 17:05:46 +0200722 (cmsg->cmsg_len - CMSG_LEN (0) <=
723 VHOST_MEMORY_MAX_NREGIONS * sizeof (int)))
724 {
725 number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int);
726 clib_memcpy (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int));
727 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728
Damjan Marion00a9dca2016-08-17 17:05:46 +0200729 /* version 1, no reply bit set */
730 if ((msg.flags & 7) != 1)
731 {
732 DBG_SOCK ("malformed message received. closing socket");
733 goto close_socket;
734 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735
736 {
Pierre Pfistere21c5282016-09-21 08:04:59 +0100737 int rv;
738 rv =
739 read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ,
740 msg.size);
741 if (rv < 0)
742 {
743 DBG_SOCK ("read failed %s", strerror (errno));
744 goto close_socket;
745 }
746 else if (rv != msg.size)
747 {
748 DBG_SOCK ("message too short (read %dB should be %dB)", rv, msg.size);
749 goto close_socket;
750 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751 }
752
Damjan Marion00a9dca2016-08-17 17:05:46 +0200753 switch (msg.request)
754 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 case VHOST_USER_GET_FEATURES:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700756 msg.flags |= 4;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100757 msg.u64 = (1ULL << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
758 (1ULL << FEAT_VIRTIO_NET_F_CTRL_VQ) |
759 (1ULL << FEAT_VIRTIO_F_ANY_LAYOUT) |
760 (1ULL << FEAT_VIRTIO_F_INDIRECT_DESC) |
761 (1ULL << FEAT_VHOST_F_LOG_ALL) |
762 (1ULL << FEAT_VIRTIO_NET_F_GUEST_ANNOUNCE) |
763 (1ULL << FEAT_VIRTIO_NET_F_MQ) |
764 (1ULL << FEAT_VHOST_USER_F_PROTOCOL_FEATURES) |
765 (1ULL << FEAT_VIRTIO_F_VERSION_1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766 msg.u64 &= vui->feature_mask;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200767 msg.size = sizeof (msg.u64);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100768 DBG_SOCK ("if %d msg VHOST_USER_GET_FEATURES - reply 0x%016llx",
769 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770 break;
771
772 case VHOST_USER_SET_FEATURES:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200773 DBG_SOCK ("if %d msg VHOST_USER_SET_FEATURES features 0x%016llx",
774 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775
776 vui->features = msg.u64;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100777
Pierre Pfistere21c5282016-09-21 08:04:59 +0100778 if (vui->features &
779 ((1 << FEAT_VIRTIO_NET_F_MRG_RXBUF) |
780 (1ULL << FEAT_VIRTIO_F_VERSION_1)))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200781 vui->virtio_net_hdr_sz = 12;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 else
Damjan Marion00a9dca2016-08-17 17:05:46 +0200783 vui->virtio_net_hdr_sz = 10;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700784
Damjan Marion00a9dca2016-08-17 17:05:46 +0200785 vui->is_any_layout =
786 (vui->features & (1 << FEAT_VIRTIO_F_ANY_LAYOUT)) ? 1 : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787
788 ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200789 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790 vui->is_up = 0;
791
Pierre Pfistere21c5282016-09-21 08:04:59 +0100792 /*for (q = 0; q < VHOST_VRING_MAX_N; q++)
793 vhost_user_vring_close(&vui->vrings[q]); */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700794
795 break;
796
797 case VHOST_USER_SET_MEM_TABLE:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200798 DBG_SOCK ("if %d msg VHOST_USER_SET_MEM_TABLE nregions %d",
799 vui->hw_if_index, msg.memory.nregions);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800
801 if ((msg.memory.nregions < 1) ||
Damjan Marion00a9dca2016-08-17 17:05:46 +0200802 (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS))
803 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804
Damjan Marion00a9dca2016-08-17 17:05:46 +0200805 DBG_SOCK ("number of mem regions must be between 1 and %i",
806 VHOST_MEMORY_MAX_NREGIONS);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807
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 if (msg.memory.nregions != number_of_fds)
812 {
813 DBG_SOCK ("each memory region must have FD");
814 goto close_socket;
815 }
816 unmap_all_mem_regions (vui);
817 for (i = 0; i < msg.memory.nregions; i++)
818 {
819 clib_memcpy (&(vui->regions[i]), &msg.memory.regions[i],
820 sizeof (vhost_user_memory_region_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821
Damjan Marion00a9dca2016-08-17 17:05:46 +0200822 long page_sz = get_huge_page_size (fds[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823
Damjan Marion00a9dca2016-08-17 17:05:46 +0200824 /* align size to 2M page */
825 ssize_t map_sz = (vui->regions[i].memory_size +
826 vui->regions[i].mmap_offset +
Pierre Pfisterbed54892017-04-20 15:34:00 +0200827 page_sz - 1) & ~(page_sz - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828
Damjan Marion00a9dca2016-08-17 17:05:46 +0200829 vui->region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE,
830 MAP_SHARED, fds[i], 0);
Damjan Marion37623702016-09-20 11:25:27 +0200831 vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr;
832 vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr +
833 vui->regions[i].memory_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700834
Damjan Marion00a9dca2016-08-17 17:05:46 +0200835 DBG_SOCK
836 ("map memory region %d addr 0 len 0x%lx fd %d mapped 0x%lx "
837 "page_sz 0x%x", i, map_sz, fds[i], vui->region_mmap_addr[i],
838 page_sz);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839
Damjan Marion00a9dca2016-08-17 17:05:46 +0200840 if (vui->region_mmap_addr[i] == MAP_FAILED)
841 {
842 clib_warning ("failed to map memory. errno is %d", errno);
843 goto close_socket;
844 }
845 vui->region_mmap_addr[i] += vui->regions[i].mmap_offset;
846 vui->region_mmap_fd[i] = fds[i];
847 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848 vui->nregions = msg.memory.nregions;
849 break;
850
851 case VHOST_USER_SET_VRING_NUM:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200852 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d",
853 vui->hw_if_index, msg.state.index, msg.state.num);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854
Damjan Marion00a9dca2016-08-17 17:05:46 +0200855 if ((msg.state.num > 32768) || /* maximum ring size is 32768 */
856 (msg.state.num == 0) || /* it cannot be zero */
Pierre Pfistere21c5282016-09-21 08:04:59 +0100857 ((msg.state.num - 1) & msg.state.num)) /* must be power of 2 */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200858 goto close_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700859 vui->vrings[msg.state.index].qsz = msg.state.num;
860 break;
861
862 case VHOST_USER_SET_VRING_ADDR:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200863 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ADDR idx %d",
864 vui->hw_if_index, msg.state.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865
Pierre Pfistere21c5282016-09-21 08:04:59 +0100866 if (msg.state.index >= VHOST_VRING_MAX_N)
867 {
868 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ADDR:"
869 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
870 goto close_socket;
871 }
872
873 if (msg.size < sizeof (msg.addr))
874 {
875 DBG_SOCK ("vhost message is too short (%d < %d)",
876 msg.size, sizeof (msg.addr));
877 goto close_socket;
878 }
879
Damjan Marion00a9dca2016-08-17 17:05:46 +0200880 vui->vrings[msg.state.index].desc = (vring_desc_t *)
881 map_user_mem (vui, msg.addr.desc_user_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882 vui->vrings[msg.state.index].used = (vring_used_t *)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200883 map_user_mem (vui, msg.addr.used_user_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 vui->vrings[msg.state.index].avail = (vring_avail_t *)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200885 map_user_mem (vui, msg.addr.avail_user_addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886
887 if ((vui->vrings[msg.state.index].desc == NULL) ||
Damjan Marion00a9dca2016-08-17 17:05:46 +0200888 (vui->vrings[msg.state.index].used == NULL) ||
889 (vui->vrings[msg.state.index].avail == NULL))
890 {
891 DBG_SOCK ("failed to map user memory for hw_if_index %d",
892 vui->hw_if_index);
893 goto close_socket;
894 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100896 vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr;
Yoann Desmouceauxfe2da0e2016-03-08 14:54:28 +0100897 vui->vrings[msg.state.index].log_used =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200898 (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100899
900 /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200901 the ring is initialized in an enabled state. */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200902 if (!(vui->features & (1 << FEAT_VHOST_USER_F_PROTOCOL_FEATURES)))
903 {
904 vui->vrings[msg.state.index].enabled = 1;
905 }
Yoann Desmouceaux4667c222016-02-24 22:51:00 +0100906
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907 vui->vrings[msg.state.index].last_used_idx =
Damjan Marion10eb1ea2016-10-13 10:02:19 +0200908 vui->vrings[msg.state.index].last_avail_idx =
Damjan Marion00a9dca2016-08-17 17:05:46 +0200909 vui->vrings[msg.state.index].used->idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910
Stevenf3b53642017-05-01 14:03:02 -0700911 /* tell driver that we don't want interrupts */
912 vui->vrings[msg.state.index].used->flags = VRING_USED_F_NO_NOTIFY;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913 break;
914
915 case VHOST_USER_SET_OWNER:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200916 DBG_SOCK ("if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917 break;
918
919 case VHOST_USER_RESET_OWNER:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200920 DBG_SOCK ("if %d msg VHOST_USER_RESET_OWNER", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921 break;
922
923 case VHOST_USER_SET_VRING_CALL:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200924 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_CALL u64 %d",
925 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926
927 q = (u8) (msg.u64 & 0xFF);
928
Pierre Pfistere21c5282016-09-21 08:04:59 +0100929 /* if there is old fd, delete and close it */
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000930 if (vui->vrings[q].callfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100931 {
932 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
933 vui->vrings[q].callfd_idx);
934 unix_file_del (&unix_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000935 vui->vrings[q].callfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100936 }
937
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938 if (!(msg.u64 & 0x100))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200939 {
940 if (number_of_fds != 1)
Damjan Marion00a9dca2016-08-17 17:05:46 +0200941 {
Pierre Pfistere21c5282016-09-21 08:04:59 +0100942 DBG_SOCK ("More than one fd received !");
943 goto close_socket;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200944 }
Pierre Pfistere21c5282016-09-21 08:04:59 +0100945
Damjan Marion00a9dca2016-08-17 17:05:46 +0200946 template.read_function = vhost_user_callfd_read_ready;
947 template.file_descriptor = fds[0];
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000948 template.private_data =
949 ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q;
Damjan Marion00a9dca2016-08-17 17:05:46 +0200950 vui->vrings[q].callfd_idx = unix_file_add (&unix_main, &template);
951 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952 else
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000953 vui->vrings[q].callfd_idx = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954 break;
955
956 case VHOST_USER_SET_VRING_KICK:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200957 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_KICK u64 %d",
958 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959
960 q = (u8) (msg.u64 & 0xFF);
961
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000962 if (vui->vrings[q].kickfd_idx != ~0)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100963 {
964 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000965 vui->vrings[q].kickfd_idx);
Pierre Pfistere21c5282016-09-21 08:04:59 +0100966 unix_file_del (&unix_main, uf);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000967 vui->vrings[q].kickfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100968 }
969
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970 if (!(msg.u64 & 0x100))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200971 {
972 if (number_of_fds != 1)
Pierre Pfistere21c5282016-09-21 08:04:59 +0100973 {
974 DBG_SOCK ("More than one fd received !");
975 goto close_socket;
976 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977
Pierre Pfistere21c5282016-09-21 08:04:59 +0100978 template.read_function = vhost_user_kickfd_read_ready;
979 template.file_descriptor = fds[0];
980 template.private_data =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000981 (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) +
982 q;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100983 vui->vrings[q].kickfd_idx = unix_file_add (&unix_main, &template);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200984 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985 else
Pierre Pfistere21c5282016-09-21 08:04:59 +0100986 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +0000987 //When no kickfd is set, the queue is initialized as started
988 vui->vrings[q].kickfd_idx = ~0;
Pierre Pfistere21c5282016-09-21 08:04:59 +0100989 vui->vrings[q].started = 1;
990 }
991
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992 break;
993
994 case VHOST_USER_SET_VRING_ERR:
Damjan Marion00a9dca2016-08-17 17:05:46 +0200995 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_ERR u64 %d",
996 vui->hw_if_index, msg.u64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997
998 q = (u8) (msg.u64 & 0xFF);
999
Pierre Pfistere21c5282016-09-21 08:04:59 +01001000 if (vui->vrings[q].errfd != -1)
1001 close (vui->vrings[q].errfd);
1002
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003 if (!(msg.u64 & 0x100))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001004 {
1005 if (number_of_fds != 1)
1006 goto close_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007
Pierre Pfistere21c5282016-09-21 08:04:59 +01001008 vui->vrings[q].errfd = fds[0];
Damjan Marion00a9dca2016-08-17 17:05:46 +02001009 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010 else
Pierre Pfistere21c5282016-09-21 08:04:59 +01001011 vui->vrings[q].errfd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013 break;
1014
1015 case VHOST_USER_SET_VRING_BASE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001016 DBG_SOCK ("if %d msg VHOST_USER_SET_VRING_BASE idx %d num %d",
1017 vui->hw_if_index, msg.state.index, msg.state.num);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018
1019 vui->vrings[msg.state.index].last_avail_idx = msg.state.num;
1020 break;
1021
1022 case VHOST_USER_GET_VRING_BASE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001023 DBG_SOCK ("if %d msg VHOST_USER_GET_VRING_BASE idx %d num %d",
1024 vui->hw_if_index, msg.state.index, msg.state.num);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025
Pierre Pfistere21c5282016-09-21 08:04:59 +01001026 if (msg.state.index >= VHOST_VRING_MAX_N)
1027 {
1028 DBG_SOCK ("invalid vring index VHOST_USER_GET_VRING_BASE:"
1029 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1030 goto close_socket;
1031 }
1032
Stevenf6dae052017-03-09 23:49:32 -08001033 /*
1034 * Copy last_avail_idx from the vring before closing it because
1035 * closing the vring also initializes the vring last_avail_idx
1036 */
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001037 msg.state.num = vui->vrings[msg.state.index].last_avail_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038 msg.flags |= 4;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001039 msg.size = sizeof (msg.state);
Stevenf6dae052017-03-09 23:49:32 -08001040
1041 /* Spec says: Client must [...] stop ring upon receiving VHOST_USER_GET_VRING_BASE. */
1042 vhost_user_vring_close (vui, msg.state.index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043 break;
1044
1045 case VHOST_USER_NONE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001046 DBG_SOCK ("if %d msg VHOST_USER_NONE", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047
1048 break;
1049
1050 case VHOST_USER_SET_LOG_BASE:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001051 {
1052 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_BASE", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053
Damjan Marion00a9dca2016-08-17 17:05:46 +02001054 if (msg.size != sizeof (msg.log))
1055 {
1056 DBG_SOCK
1057 ("invalid msg size for VHOST_USER_SET_LOG_BASE: %d instead of %d",
1058 msg.size, sizeof (msg.log));
1059 goto close_socket;
1060 }
1061
1062 if (!
1063 (vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD)))
1064 {
1065 DBG_SOCK
1066 ("VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but VHOST_USER_SET_LOG_BASE received");
1067 goto close_socket;
1068 }
1069
1070 fd = fds[0];
1071 /* align size to 2M page */
1072 long page_sz = get_huge_page_size (fd);
1073 ssize_t map_sz =
Pierre Pfisterbed54892017-04-20 15:34:00 +02001074 (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001075
1076 vui->log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE,
1077 MAP_SHARED, fd, 0);
1078
1079 DBG_SOCK
1080 ("map log region addr 0 len 0x%lx off 0x%lx fd %d mapped 0x%lx",
1081 map_sz, msg.log.offset, fd, vui->log_base_addr);
1082
1083 if (vui->log_base_addr == MAP_FAILED)
1084 {
1085 clib_warning ("failed to map memory. errno is %d", errno);
1086 goto close_socket;
1087 }
1088
1089 vui->log_base_addr += msg.log.offset;
1090 vui->log_size = msg.log.size;
1091
1092 msg.flags |= 4;
1093 msg.size = sizeof (msg.u64);
1094
1095 break;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001096 }
1097
Ed Warnickecb9cada2015-12-08 15:45:58 -07001098 case VHOST_USER_SET_LOG_FD:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001099 DBG_SOCK ("if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100
1101 break;
1102
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001103 case VHOST_USER_GET_PROTOCOL_FEATURES:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001104 DBG_SOCK ("if %d msg VHOST_USER_GET_PROTOCOL_FEATURES",
1105 vui->hw_if_index);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001106
1107 msg.flags |= 4;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001108 msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
1109 (1 << VHOST_USER_PROTOCOL_F_MQ);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001110 msg.size = sizeof (msg.u64);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001111 break;
1112
1113 case VHOST_USER_SET_PROTOCOL_FEATURES:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001114 DBG_SOCK ("if %d msg VHOST_USER_SET_PROTOCOL_FEATURES features 0x%lx",
1115 vui->hw_if_index, msg.u64);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001116
1117 vui->protocol_features = msg.u64;
1118
1119 break;
1120
Pierre Pfistere21c5282016-09-21 08:04:59 +01001121 case VHOST_USER_GET_QUEUE_NUM:
1122 DBG_SOCK ("if %d msg VHOST_USER_GET_QUEUE_NUM", vui->hw_if_index);
1123 msg.flags |= 4;
1124 msg.u64 = VHOST_VRING_MAX_N;
1125 msg.size = sizeof (msg.u64);
1126 break;
1127
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001128 case VHOST_USER_SET_VRING_ENABLE:
Pierre Pfistere21c5282016-09-21 08:04:59 +01001129 DBG_SOCK ("if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d",
1130 vui->hw_if_index, msg.state.num ? "enable" : "disable",
1131 msg.state.index);
1132 if (msg.state.index >= VHOST_VRING_MAX_N)
1133 {
1134 DBG_SOCK ("invalid vring index VHOST_USER_SET_VRING_ENABLE:"
1135 " %d >= %d", msg.state.index, VHOST_VRING_MAX_N);
1136 goto close_socket;
1137 }
1138
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001139 vui->vrings[msg.state.index].enabled = msg.state.num;
1140 break;
1141
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 default:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001143 DBG_SOCK ("unknown vhost-user message %d received. closing socket",
1144 msg.request);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001145 goto close_socket;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001146 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001147
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148 /* if we need to reply */
1149 if (msg.flags & 4)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001150 {
1151 n =
1152 send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153 if (n != (msg.size + VHOST_USER_MSG_HDR_SZ))
Pierre Pfistere21c5282016-09-21 08:04:59 +01001154 {
1155 DBG_SOCK ("could not send message response");
1156 goto close_socket;
1157 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02001158 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159
Pierre Pfistere21c5282016-09-21 08:04:59 +01001160 vhost_user_update_iface_state (vui);
1161 vlib_worker_thread_barrier_release (vlib_get_main ());
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162 return 0;
1163
1164close_socket:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001165 vhost_user_if_disconnect (vui);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001166 vhost_user_update_iface_state (vui);
1167 vlib_worker_thread_barrier_release (vlib_get_main ());
Ed Warnickecb9cada2015-12-08 15:45:58 -07001168 return 0;
1169}
1170
Damjan Marion00a9dca2016-08-17 17:05:46 +02001171static clib_error_t *
1172vhost_user_socket_error (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001173{
Pierre Pfistere21c5282016-09-21 08:04:59 +01001174 vlib_main_t *vm = vlib_get_main ();
Damjan Marion00a9dca2016-08-17 17:05:46 +02001175 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001176 vhost_user_intf_t *vui =
1177 pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178
Pierre Pfistere21c5282016-09-21 08:04:59 +01001179 DBG_SOCK ("socket error on if %d", vui->sw_if_index);
1180 vlib_worker_thread_barrier_sync (vm);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001181 vhost_user_if_disconnect (vui);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001182 vhost_user_rx_thread_placement ();
1183 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 return 0;
1185}
1186
Damjan Marion00a9dca2016-08-17 17:05:46 +02001187static clib_error_t *
1188vhost_user_socksvr_accept_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189{
1190 int client_fd, client_len;
1191 struct sockaddr_un client;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001192 unix_file_t template = { 0 };
1193 vhost_user_main_t *vum = &vhost_user_main;
1194 vhost_user_intf_t *vui;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001195
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001196 vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001197
Damjan Marion00a9dca2016-08-17 17:05:46 +02001198 client_len = sizeof (client);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001199 client_fd = accept (uf->file_descriptor,
Damjan Marion00a9dca2016-08-17 17:05:46 +02001200 (struct sockaddr *) &client,
1201 (socklen_t *) & client_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001202
1203 if (client_fd < 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001204 return clib_error_return_unix (0, "accept");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001205
Pierre Pfistere21c5282016-09-21 08:04:59 +01001206 DBG_SOCK ("New client socket for vhost interface %d", vui->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001207 template.read_function = vhost_user_socket_read;
1208 template.error_function = vhost_user_socket_error;
1209 template.file_descriptor = client_fd;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001210 template.private_data = vui - vhost_user_main.vhost_user_interfaces;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211 vui->unix_file_index = unix_file_add (&unix_main, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001212 return 0;
1213}
1214
1215static clib_error_t *
1216vhost_user_init (vlib_main_t * vm)
1217{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001218 clib_error_t *error;
1219 vhost_user_main_t *vum = &vhost_user_main;
1220 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001221
1222 error = vlib_call_init_function (vm, ip4_init);
1223 if (error)
1224 return error;
1225
Ed Warnickecb9cada2015-12-08 15:45:58 -07001226 vum->coalesce_frames = 32;
1227 vum->coalesce_time = 1e-3;
1228
Pierre Pfistere21c5282016-09-21 08:04:59 +01001229 vec_validate (vum->cpus, tm->n_vlib_mains - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001230
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001231 vhost_cpu_t *cpu;
1232 vec_foreach (cpu, vum->cpus)
1233 {
1234 /* This is actually not necessary as validate already zeroes it
1235 * Just keeping the loop here for later because I am lazy. */
1236 cpu->rx_buffers_len = 0;
1237 }
1238
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001239 vum->random = random_default_seed ();
1240
Steven5445f5f2017-04-25 16:16:00 -07001241 mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword));
1242
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243 return 0;
1244}
1245
1246VLIB_INIT_FUNCTION (vhost_user_init);
1247
Damjan Marion00a9dca2016-08-17 17:05:46 +02001248static u8 *
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001249format_vhost_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250{
1251 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
1252 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001253 CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main ();
1254 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001255 vhost_trace_t *t = va_arg (*va, vhost_trace_t *);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001256 vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces,
1257 t->device_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001258
Damjan Marion00a9dca2016-08-17 17:05:46 +02001259 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, vui->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260
Ed Warnickecb9cada2015-12-08 15:45:58 -07001261 uword indent = format_get_indent (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001262
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001263 s = format (s, "%U %U queue %d\n", format_white_space, indent,
Pierre Pfistere21c5282016-09-21 08:04:59 +01001264 format_vnet_sw_interface_name, vnm, sw, t->qid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001265
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001266 s = format (s, "%U virtio flags:\n", format_white_space, indent);
1267#define _(n,i,st) \
1268 if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \
1269 s = format (s, "%U %s %s\n", format_white_space, indent, #n, st);
1270 foreach_virtio_trace_flags
1271#undef _
1272 s = format (s, "%U virtio_net_hdr first_desc_len %u\n",
1273 format_white_space, indent, t->first_desc_len);
1274
1275 s = format (s, "%U flags 0x%02x gso_type %u\n",
Damjan Marion00a9dca2016-08-17 17:05:46 +02001276 format_white_space, indent,
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001277 t->hdr.hdr.flags, t->hdr.hdr.gso_type);
1278
1279 if (vui->virtio_net_hdr_sz == 12)
1280 s = format (s, "%U num_buff %u",
1281 format_white_space, indent, t->hdr.num_buffers);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001282
1283 return s;
1284}
1285
Damjan Marion00a9dca2016-08-17 17:05:46 +02001286void
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001287vhost_user_rx_trace (vhost_trace_t * t,
1288 vhost_user_intf_t * vui, u16 qid,
1289 vlib_buffer_t * b, vhost_user_vring_t * txvq)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001290{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001291 vhost_user_main_t *vum = &vhost_user_main;
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001292 u32 qsz_mask = txvq->qsz - 1;
1293 u32 last_avail_idx = txvq->last_avail_idx;
1294 u32 desc_current = txvq->avail->ring[last_avail_idx & qsz_mask];
1295 vring_desc_t *hdr_desc = 0;
1296 virtio_net_hdr_mrg_rxbuf_t *hdr;
1297 u32 hint = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001298
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001299 memset (t, 0, sizeof (*t));
1300 t->device_index = vui - vum->vhost_user_interfaces;
1301 t->qid = qid;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001303 hdr_desc = &txvq->desc[desc_current];
1304 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001305 {
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001306 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001307 /* Header is the first here */
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001308 hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint);
1309 }
1310 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1311 {
1312 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1313 }
1314 if (!(txvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1315 !(txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1316 {
1317 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1318 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001319
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001320 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001321
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001322 if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint)))
1323 {
1324 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR;
1325 }
1326 else
1327 {
1328 u32 len = vui->virtio_net_hdr_sz;
1329 memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001330 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001331}
1332
Damjan Marion00a9dca2016-08-17 17:05:46 +02001333static inline void
1334vhost_user_send_call (vlib_main_t * vm, vhost_user_vring_t * vq)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001335{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001336 vhost_user_main_t *vum = &vhost_user_main;
1337 u64 x = 1;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001338 int fd = UNIX_GET_FD (vq->callfd_idx);
Stevenf3b53642017-05-01 14:03:02 -07001339 int rv;
1340
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001341 rv = write (fd, &x, sizeof (x));
Stevenf3b53642017-05-01 14:03:02 -07001342 if (rv <= 0)
1343 {
1344 clib_unix_warning
1345 ("Error: Could not write to unix socket for callfd %d", fd);
1346 return;
1347 }
1348
Damjan Marion00a9dca2016-08-17 17:05:46 +02001349 vq->n_since_last_int = 0;
1350 vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001351}
1352
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001353static_always_inline u32
1354vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1355 u16 copy_len, u32 * map_hint)
1356{
1357 void *src0, *src1, *src2, *src3;
1358 if (PREDICT_TRUE (copy_len >= 4))
1359 {
1360 if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint))))
1361 return 1;
1362 if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint))))
1363 return 1;
1364
1365 while (PREDICT_TRUE (copy_len >= 4))
1366 {
1367 src0 = src2;
1368 src1 = src3;
1369
1370 if (PREDICT_FALSE
1371 (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint))))
1372 return 1;
1373 if (PREDICT_FALSE
1374 (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint))))
1375 return 1;
1376
1377 CLIB_PREFETCH (src2, 64, LOAD);
1378 CLIB_PREFETCH (src3, 64, LOAD);
1379
1380 clib_memcpy ((void *) cpy[0].dst, src0, cpy[0].len);
1381 clib_memcpy ((void *) cpy[1].dst, src1, cpy[1].len);
1382 copy_len -= 2;
1383 cpy += 2;
1384 }
1385 }
1386 while (copy_len)
1387 {
1388 if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint))))
1389 return 1;
1390 clib_memcpy ((void *) cpy->dst, src0, cpy->len);
1391 copy_len -= 1;
1392 cpy += 1;
1393 }
1394 return 0;
1395}
1396
1397/**
1398 * Try to discard packets from the tx ring (VPP RX path).
1399 * Returns the number of discarded packets.
1400 */
1401u32
1402vhost_user_rx_discard_packet (vlib_main_t * vm,
1403 vhost_user_intf_t * vui,
1404 vhost_user_vring_t * txvq, u32 discard_max)
1405{
1406 /*
1407 * On the RX side, each packet corresponds to one descriptor
1408 * (it is the same whether it is a shallow descriptor, chained, or indirect).
1409 * Therefore, discarding a packet is like discarding a descriptor.
1410 */
1411 u32 discarded_packets = 0;
1412 u32 avail_idx = txvq->avail->idx;
1413 u16 qsz_mask = txvq->qsz - 1;
1414 while (discarded_packets != discard_max)
1415 {
1416 if (avail_idx == txvq->last_avail_idx)
1417 goto out;
1418
1419 u16 desc_chain_head =
1420 txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
1421 txvq->last_avail_idx++;
1422 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_chain_head;
1423 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1424 vhost_user_log_dirty_ring (vui, txvq,
1425 ring[txvq->last_used_idx & qsz_mask]);
1426 txvq->last_used_idx++;
1427 discarded_packets++;
1428 }
1429
1430out:
1431 CLIB_MEMORY_BARRIER ();
1432 txvq->used->idx = txvq->last_used_idx;
1433 vhost_user_log_dirty_ring (vui, txvq, idx);
1434 return discarded_packets;
1435}
1436
1437/*
1438 * In case of overflow, we need to rewind the array of allocated buffers.
1439 */
1440static void
1441vhost_user_input_rewind_buffers (vlib_main_t * vm,
1442 vhost_cpu_t * cpu, vlib_buffer_t * b_head)
1443{
1444 u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1445 vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current);
1446 b_current->current_length = 0;
1447 b_current->flags = 0;
1448 while (b_current != b_head)
1449 {
1450 cpu->rx_buffers_len++;
1451 bi_current = cpu->rx_buffers[cpu->rx_buffers_len];
1452 b_current = vlib_get_buffer (vm, bi_current);
1453 b_current->current_length = 0;
1454 b_current->flags = 0;
1455 }
Steven95827e42017-05-18 21:22:00 -07001456 cpu->rx_buffers_len++;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001457}
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01001458
Damjan Marion00a9dca2016-08-17 17:05:46 +02001459static u32
1460vhost_user_if_input (vlib_main_t * vm,
1461 vhost_user_main_t * vum,
Pierre Pfistere21c5282016-09-21 08:04:59 +01001462 vhost_user_intf_t * vui,
Stevenf3b53642017-05-01 14:03:02 -07001463 u16 qid, vlib_node_runtime_t * node,
1464 vnet_hw_interface_rx_mode mode)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001465{
Pierre Pfistere21c5282016-09-21 08:04:59 +01001466 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001467 u16 n_rx_packets = 0;
1468 u32 n_rx_bytes = 0;
1469 u16 n_left;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001470 u32 n_left_to_next, *to_next;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001471 u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
1472 u32 n_trace = vlib_get_trace_count (vm, node);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001473 u16 qsz_mask;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001474 u32 map_hint = 0;
Damjan Marion586afd72017-04-05 19:18:20 +02001475 u16 thread_index = vlib_get_thread_index ();
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001476 u16 copy_len = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001477
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001478 {
1479 /* do we have pending interrupts ? */
1480 vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)];
1481 f64 now = vlib_time_now (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001482
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001483 if ((txvq->n_since_last_int) && (txvq->int_deadline < now))
1484 vhost_user_send_call (vm, txvq);
1485
1486 if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now))
1487 vhost_user_send_call (vm, rxvq);
1488 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001489
Stevenf3b53642017-05-01 14:03:02 -07001490 /*
1491 * For adaptive mode, it is optimized to reduce interrupts.
1492 * If the scheduler switches the input node to polling due
1493 * to burst of traffic, we tell the driver no interrupt.
1494 * When the traffic subsides, the scheduler switches the node back to
1495 * interrupt mode. We must tell the driver we want interrupt.
1496 */
1497 if (PREDICT_FALSE (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
1498 {
1499 if ((node->flags &
1500 VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) ||
1501 !(node->flags &
1502 VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE))
1503 /* Tell driver we want notification */
1504 txvq->used->flags = 0;
1505 else
1506 /* Tell driver we don't want notification */
1507 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
1508 }
1509
Damjan Marion00a9dca2016-08-17 17:05:46 +02001510 if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001511 return 0;
1512
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001513 n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx);
1514
Ed Warnickecb9cada2015-12-08 15:45:58 -07001515 /* nothing to do */
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001516 if (PREDICT_FALSE (n_left == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001517 return 0;
1518
Pierre Pfistere21c5282016-09-21 08:04:59 +01001519 if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled)))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001520 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01001521 /*
1522 * Discard input packet if interface is admin down or vring is not
1523 * enabled.
1524 * "For example, for a networking device, in the disabled state
1525 * client must not supply any new RX packets, but must process
1526 * and discard any TX packets."
1527 */
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001528 vhost_user_rx_discard_packet (vm, vui, txvq,
1529 VHOST_USER_DOWN_DISCARD_COUNT);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001530 return 0;
1531 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001532
Pierre Pfistere21c5282016-09-21 08:04:59 +01001533 if (PREDICT_FALSE (n_left == txvq->qsz))
1534 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001535 /*
1536 * Informational error logging when VPP is not
1537 * receiving packets fast enough.
1538 */
Pierre Pfistere21c5282016-09-21 08:04:59 +01001539 vlib_error_count (vm, node->node_index,
1540 VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1);
1541 }
1542
Ed Warnickecb9cada2015-12-08 15:45:58 -07001543 qsz_mask = txvq->qsz - 1;
1544
Pierre Pfister328e99b2016-02-12 13:18:42 +00001545 if (n_left > VLIB_FRAME_SIZE)
1546 n_left = VLIB_FRAME_SIZE;
1547
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001548 /*
1549 * For small packets (<2kB), we will not need more than one vlib buffer
1550 * per packet. In case packets are bigger, we will just yeld at some point
1551 * in the loop and come back later. This is not an issue as for big packet,
1552 * processing cost really comes from the memory copy.
Pierre Pfister328e99b2016-02-12 13:18:42 +00001553 */
Damjan Marion586afd72017-04-05 19:18:20 +02001554 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len < n_left + 1))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001555 {
Damjan Marion586afd72017-04-05 19:18:20 +02001556 u32 curr_len = vum->cpus[thread_index].rx_buffers_len;
1557 vum->cpus[thread_index].rx_buffers_len +=
Damjan Marion00a9dca2016-08-17 17:05:46 +02001558 vlib_buffer_alloc_from_free_list (vm,
Damjan Marion586afd72017-04-05 19:18:20 +02001559 vum->cpus[thread_index].rx_buffers +
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001560 curr_len,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001561 VHOST_USER_RX_BUFFERS_N - curr_len,
Damjan Marion00a9dca2016-08-17 17:05:46 +02001562 VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001563
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001564 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001565 (vum->cpus[thread_index].rx_buffers_len <
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001566 VHOST_USER_RX_BUFFER_STARVATION))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001567 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001568 /* In case of buffer starvation, discard some packets from the queue
1569 * and log the event.
1570 * We keep doing best effort for the remaining packets. */
Damjan Marion586afd72017-04-05 19:18:20 +02001571 u32 flush = (n_left + 1 > vum->cpus[thread_index].rx_buffers_len) ?
1572 n_left + 1 - vum->cpus[thread_index].rx_buffers_len : 1;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001573 flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush);
1574
1575 n_left -= flush;
1576 vlib_increment_simple_counter (vnet_main.
1577 interface_main.sw_if_counters +
1578 VNET_INTERFACE_COUNTER_DROP,
Damjan Marion586afd72017-04-05 19:18:20 +02001579 vlib_get_thread_index (),
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001580 vui->sw_if_index, flush);
1581
1582 vlib_error_count (vm, vhost_user_input_node.index,
1583 VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush);
Damjan Marion00a9dca2016-08-17 17:05:46 +02001584 }
1585 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001586
Damjan Marion00a9dca2016-08-17 17:05:46 +02001587 while (n_left > 0)
1588 {
1589 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001590
Damjan Marion00a9dca2016-08-17 17:05:46 +02001591 while (n_left > 0 && n_left_to_next > 0)
1592 {
1593 vlib_buffer_t *b_head, *b_current;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001594 u32 bi_current;
1595 u16 desc_current;
1596 u32 desc_data_offset;
1597 vring_desc_t *desc_table = txvq->desc;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001598
Damjan Marion586afd72017-04-05 19:18:20 +02001599 if (PREDICT_FALSE (vum->cpus[thread_index].rx_buffers_len <= 1))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001600 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001601 /* Not enough rx_buffers
1602 * Note: We yeld on 1 so we don't need to do an additional
1603 * check for the next buffer prefetch.
1604 */
1605 n_left = 0;
1606 break;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001607 }
1608
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001609 desc_current = txvq->avail->ring[txvq->last_avail_idx & qsz_mask];
Damjan Marion586afd72017-04-05 19:18:20 +02001610 vum->cpus[thread_index].rx_buffers_len--;
1611 bi_current = (vum->cpus[thread_index].rx_buffers)
1612 [vum->cpus[thread_index].rx_buffers_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001613 b_head = b_current = vlib_get_buffer (vm, bi_current);
1614 to_next[0] = bi_current; //We do that now so we can forget about bi_current
1615 to_next++;
1616 n_left_to_next--;
1617
1618 vlib_prefetch_buffer_with_index (vm,
Damjan Marion586afd72017-04-05 19:18:20 +02001619 (vum->
1620 cpus[thread_index].rx_buffers)
1621 [vum->cpus[thread_index].
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001622 rx_buffers_len - 1], LOAD);
1623
1624 /* Just preset the used descriptor id and length for later */
1625 txvq->used->ring[txvq->last_used_idx & qsz_mask].id = desc_current;
1626 txvq->used->ring[txvq->last_used_idx & qsz_mask].len = 0;
1627 vhost_user_log_dirty_ring (vui, txvq,
1628 ring[txvq->last_used_idx & qsz_mask]);
1629
1630 /* The buffer should already be initialized */
1631 b_head->total_length_not_including_first_buffer = 0;
1632 b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
1633
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001634 if (PREDICT_FALSE (n_trace))
1635 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001636 //TODO: next_index is not exactly known at that point
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001637 vlib_trace_buffer (vm, node, next_index, b_head,
1638 /* follow_chain */ 0);
1639 vhost_trace_t *t0 =
1640 vlib_add_trace (vm, node, b_head, sizeof (t0[0]));
1641 vhost_user_rx_trace (t0, vui, qid, b_head, txvq);
1642 n_trace--;
1643 vlib_set_trace_count (vm, node, n_trace);
1644 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02001645
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001646 /* This depends on the setup but is very consistent
1647 * So I think the CPU branch predictor will make a pretty good job
1648 * at optimizing the decision. */
1649 if (txvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1650 {
1651 desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr,
1652 &map_hint);
1653 desc_current = 0;
1654 if (PREDICT_FALSE (desc_table == 0))
1655 {
Steven95827e42017-05-18 21:22:00 -07001656 vlib_error_count (vm, node->node_index,
1657 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001658 goto out;
1659 }
1660 }
1661
Damjan Marion00a9dca2016-08-17 17:05:46 +02001662 if (PREDICT_TRUE (vui->is_any_layout) ||
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001663 (!(desc_table[desc_current].flags & VIRTQ_DESC_F_NEXT)))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001664 {
1665 /* ANYLAYOUT or single buffer */
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001666 desc_data_offset = vui->virtio_net_hdr_sz;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001667 }
1668 else
1669 {
1670 /* CSR case without ANYLAYOUT, skip 1st buffer */
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001671 desc_data_offset = desc_table[desc_current].len;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001672 }
1673
Damjan Marion00a9dca2016-08-17 17:05:46 +02001674 while (1)
1675 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001676 /* Get more input if necessary. Or end of packet. */
1677 if (desc_data_offset == desc_table[desc_current].len)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001678 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001679 if (PREDICT_FALSE (desc_table[desc_current].flags &
1680 VIRTQ_DESC_F_NEXT))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001681 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001682 desc_current = desc_table[desc_current].next;
1683 desc_data_offset = 0;
1684 }
1685 else
1686 {
1687 goto out;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001688 }
1689 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001690
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001691 /* Get more output if necessary. Or end of packet. */
1692 if (PREDICT_FALSE
1693 (b_current->current_length == VLIB_BUFFER_DATA_SIZE))
1694 {
1695 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001696 (vum->cpus[thread_index].rx_buffers_len == 0))
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001697 {
Steven62411e72017-02-03 09:30:37 -08001698 /* Cancel speculation */
1699 to_next--;
1700 n_left_to_next++;
1701
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001702 /*
1703 * Checking if there are some left buffers.
1704 * If not, just rewind the used buffers and stop.
1705 * Note: Scheduled copies are not cancelled. This is
1706 * not an issue as they would still be valid. Useless,
1707 * but valid.
1708 */
1709 vhost_user_input_rewind_buffers (vm,
Damjan Marion586afd72017-04-05 19:18:20 +02001710 &vum->cpus
1711 [thread_index],
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001712 b_head);
1713 n_left = 0;
1714 goto stop;
1715 }
1716
1717 /* Get next output */
Damjan Marion586afd72017-04-05 19:18:20 +02001718 vum->cpus[thread_index].rx_buffers_len--;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001719 u32 bi_next =
Damjan Marion586afd72017-04-05 19:18:20 +02001720 (vum->cpus[thread_index].rx_buffers)[vum->cpus
1721 [thread_index].rx_buffers_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001722 b_current->next_buffer = bi_next;
1723 b_current->flags |= VLIB_BUFFER_NEXT_PRESENT;
1724 bi_current = bi_next;
1725 b_current = vlib_get_buffer (vm, bi_current);
1726 }
1727
1728 /* Prepare a copy order executed later for the data */
Damjan Marion586afd72017-04-05 19:18:20 +02001729 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001730 copy_len++;
1731 u32 desc_data_l =
1732 desc_table[desc_current].len - desc_data_offset;
1733 cpy->len = VLIB_BUFFER_DATA_SIZE - b_current->current_length;
1734 cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len;
Steven025d4152017-05-16 21:26:13 -07001735 cpy->dst = (uword) (vlib_buffer_get_current (b_current) +
1736 b_current->current_length);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001737 cpy->src = desc_table[desc_current].addr + desc_data_offset;
1738
1739 desc_data_offset += cpy->len;
1740
1741 b_current->current_length += cpy->len;
1742 b_head->total_length_not_including_first_buffer += cpy->len;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001743 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001744
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001745 out:
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001746 CLIB_PREFETCH (&n_left, sizeof (n_left), LOAD);
1747
1748 n_rx_bytes += b_head->total_length_not_including_first_buffer;
1749 n_rx_packets++;
1750
1751 b_head->total_length_not_including_first_buffer -=
1752 b_head->current_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001753
Damjan Marion00a9dca2016-08-17 17:05:46 +02001754 /* consume the descriptor and return it as used */
1755 txvq->last_avail_idx++;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001756 txvq->last_used_idx++;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001757
Damjan Marion00a9dca2016-08-17 17:05:46 +02001758 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_head);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001759
Damjan Marion00a9dca2016-08-17 17:05:46 +02001760 vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index;
1761 vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001762 b_head->error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001763
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001764 {
1765 u32 next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT;
Pierre Pfister328e99b2016-02-12 13:18:42 +00001766
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001767 /* redirect if feature path enabled */
1768 vnet_feature_start_device_input_x1 (vui->sw_if_index, &next0,
Damjan Marion35af9e52017-03-06 12:02:50 +01001769 b_head);
Damjan Marion22311502016-10-28 20:30:15 +02001770
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001771 u32 bi = to_next[-1]; //Cannot use to_next[-1] in the macro
1772 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1773 to_next, n_left_to_next,
1774 bi, next0);
1775 }
Damjan Marion22311502016-10-28 20:30:15 +02001776
Damjan Marion00a9dca2016-08-17 17:05:46 +02001777 n_left--;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001778
1779 /*
1780 * Although separating memory copies from virtio ring parsing
1781 * is beneficial, we can offer to perform the copies from time
1782 * to time in order to free some space in the ring.
1783 */
1784 if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD))
Pierre Pfistere21c5282016-09-21 08:04:59 +01001785 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001786 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001787 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001788 copy_len, &map_hint)))
1789 {
Steven95827e42017-05-18 21:22:00 -07001790 vlib_error_count (vm, node->node_index,
1791 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001792 }
1793 copy_len = 0;
1794
1795 /* give buffers back to driver */
1796 CLIB_MEMORY_BARRIER ();
1797 txvq->used->idx = txvq->last_used_idx;
1798 vhost_user_log_dirty_ring (vui, txvq, idx);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001799 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02001800 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001801 stop:
Damjan Marion00a9dca2016-08-17 17:05:46 +02001802 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001803 }
1804
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001805 /* Do the memory copies */
1806 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02001807 (vhost_user_input_copy (vui, vum->cpus[thread_index].copy,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001808 copy_len, &map_hint)))
1809 {
Steven95827e42017-05-18 21:22:00 -07001810 vlib_error_count (vm, node->node_index,
1811 VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001812 }
Pierre Pfister328e99b2016-02-12 13:18:42 +00001813
1814 /* give buffers back to driver */
Damjan Marion00a9dca2016-08-17 17:05:46 +02001815 CLIB_MEMORY_BARRIER ();
Pierre Pfister328e99b2016-02-12 13:18:42 +00001816 txvq->used->idx = txvq->last_used_idx;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001817 vhost_user_log_dirty_ring (vui, txvq, idx);
Pierre Pfister328e99b2016-02-12 13:18:42 +00001818
Ed Warnickecb9cada2015-12-08 15:45:58 -07001819 /* interrupt (call) handling */
Steven7312cc72017-03-15 21:18:55 -07001820 if ((txvq->callfd_idx != ~0) &&
1821 !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001822 {
1823 txvq->n_since_last_int += n_rx_packets;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001824
Damjan Marion00a9dca2016-08-17 17:05:46 +02001825 if (txvq->n_since_last_int > vum->coalesce_frames)
1826 vhost_user_send_call (vm, txvq);
1827 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001828
1829 /* increase rx counters */
1830 vlib_increment_combined_counter
Damjan Marion00a9dca2016-08-17 17:05:46 +02001831 (vnet_main.interface_main.combined_sw_if_counters
1832 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +02001833 vlib_get_thread_index (), vui->sw_if_index, n_rx_packets, n_rx_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001834
Damjan Marion586afd72017-04-05 19:18:20 +02001835 vnet_device_increment_rx_packets (thread_index, n_rx_packets);
Damjan Marionb3bb1012017-02-28 21:55:28 +01001836
Ed Warnickecb9cada2015-12-08 15:45:58 -07001837 return n_rx_packets;
1838}
1839
1840static uword
1841vhost_user_input (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02001842 vlib_node_runtime_t * node, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001843{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001844 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001845 uword n_rx_packets = 0;
Steven7312cc72017-03-15 21:18:55 -07001846 vhost_user_intf_t *vui;
Stevenf3b53642017-05-01 14:03:02 -07001847 vnet_device_input_runtime_t *rt =
1848 (vnet_device_input_runtime_t *) node->runtime_data;
1849 vnet_device_and_queue_t *dq;
Pierre Pfistere21c5282016-09-21 08:04:59 +01001850
Stevenf3b53642017-05-01 14:03:02 -07001851 vec_foreach (dq, rt->devices_and_queues)
1852 {
1853 if (clib_smp_swap (&dq->interrupt_pending, 0) ||
1854 (node->state == VLIB_NODE_STATE_POLLING))
Steven7312cc72017-03-15 21:18:55 -07001855 {
Stevenf3b53642017-05-01 14:03:02 -07001856 vui =
1857 pool_elt_at_index (vum->vhost_user_interfaces, dq->dev_instance);
1858 n_rx_packets = vhost_user_if_input (vm, vum, vui, dq->queue_id, node,
1859 dq->mode);
Steven7312cc72017-03-15 21:18:55 -07001860 }
Stevenf3b53642017-05-01 14:03:02 -07001861 }
Steven7312cc72017-03-15 21:18:55 -07001862
Ed Warnickecb9cada2015-12-08 15:45:58 -07001863 return n_rx_packets;
1864}
1865
Damjan Marion00a9dca2016-08-17 17:05:46 +02001866/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001867VLIB_REGISTER_NODE (vhost_user_input_node) = {
1868 .function = vhost_user_input,
1869 .type = VLIB_NODE_TYPE_INPUT,
1870 .name = "vhost-user-input",
Damjan Marion51327ac2016-11-09 11:59:42 +01001871 .sibling_of = "device-input",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001872
1873 /* Will be enabled if/when hardware is detected. */
1874 .state = VLIB_NODE_STATE_DISABLED,
1875
1876 .format_buffer = format_ethernet_header_with_length,
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001877 .format_trace = format_vhost_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001878
1879 .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR,
1880 .error_strings = vhost_user_input_func_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001881};
1882
Damjan Marion1c80e832016-05-11 23:07:18 +02001883VLIB_NODE_FUNCTION_MULTIARCH (vhost_user_input_node, vhost_user_input)
Damjan Marion00a9dca2016-08-17 17:05:46 +02001884/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +02001885
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001886
1887void
1888vhost_user_tx_trace (vhost_trace_t * t,
1889 vhost_user_intf_t * vui, u16 qid,
1890 vlib_buffer_t * b, vhost_user_vring_t * rxvq)
1891{
1892 vhost_user_main_t *vum = &vhost_user_main;
1893 u32 qsz_mask = rxvq->qsz - 1;
1894 u32 last_avail_idx = rxvq->last_avail_idx;
1895 u32 desc_current = rxvq->avail->ring[last_avail_idx & qsz_mask];
1896 vring_desc_t *hdr_desc = 0;
1897 u32 hint = 0;
1898
1899 memset (t, 0, sizeof (*t));
1900 t->device_index = vui - vum->vhost_user_interfaces;
1901 t->qid = qid;
1902
1903 hdr_desc = &rxvq->desc[desc_current];
1904 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT)
1905 {
1906 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001907 /* Header is the first here */
Pierre Pfister116ea4b2016-11-08 15:49:28 +00001908 hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint);
1909 }
1910 if (rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT)
1911 {
1912 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED;
1913 }
1914 if (!(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_NEXT) &&
1915 !(rxvq->desc[desc_current].flags & VIRTQ_DESC_F_INDIRECT))
1916 {
1917 t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC;
1918 }
1919
1920 t->first_desc_len = hdr_desc ? hdr_desc->len : 0;
1921}
1922
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001923static_always_inline u32
1924vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy,
1925 u16 copy_len, u32 * map_hint)
1926{
1927 void *dst0, *dst1, *dst2, *dst3;
1928 if (PREDICT_TRUE (copy_len >= 4))
1929 {
1930 if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint))))
1931 return 1;
1932 if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint))))
1933 return 1;
1934 while (PREDICT_TRUE (copy_len >= 4))
1935 {
1936 dst0 = dst2;
1937 dst1 = dst3;
1938
1939 if (PREDICT_FALSE
1940 (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint))))
1941 return 1;
1942 if (PREDICT_FALSE
1943 (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint))))
1944 return 1;
1945
1946 CLIB_PREFETCH ((void *) cpy[2].src, 64, LOAD);
1947 CLIB_PREFETCH ((void *) cpy[3].src, 64, LOAD);
1948
1949 clib_memcpy (dst0, (void *) cpy[0].src, cpy[0].len);
1950 clib_memcpy (dst1, (void *) cpy[1].src, cpy[1].len);
1951
1952 vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1);
1953 vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1);
1954 copy_len -= 2;
1955 cpy += 2;
1956 }
1957 }
1958 while (copy_len)
1959 {
1960 if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint))))
1961 return 1;
1962 clib_memcpy (dst0, (void *) cpy->src, cpy->len);
1963 vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1);
1964 copy_len -= 1;
1965 cpy += 1;
1966 }
1967 return 0;
1968}
1969
1970
Ed Warnickecb9cada2015-12-08 15:45:58 -07001971static uword
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001972vhost_user_tx (vlib_main_t * vm,
1973 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001974{
Damjan Marion00a9dca2016-08-17 17:05:46 +02001975 u32 *buffers = vlib_frame_args (frame);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001976 u32 n_left = frame->n_vectors;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001977 vhost_user_main_t *vum = &vhost_user_main;
Damjan Marion00a9dca2016-08-17 17:05:46 +02001978 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
1979 vhost_user_intf_t *vui =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00001980 pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance);
Pierre Pfistere21c5282016-09-21 08:04:59 +01001981 u32 qid = ~0;
1982 vhost_user_vring_t *rxvq;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001983 u16 qsz_mask;
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001984 u8 error;
Damjan Marion586afd72017-04-05 19:18:20 +02001985 u32 thread_index = vlib_get_thread_index ();
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001986 u32 map_hint = 0;
1987 u8 retry = 8;
1988 u16 copy_len;
1989 u16 tx_headers_len;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01001990
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00001991 if (PREDICT_FALSE (!vui->admin_up))
1992 {
1993 error = VHOST_USER_TX_FUNC_ERROR_DOWN;
1994 goto done3;
1995 }
1996
1997 if (PREDICT_FALSE (!vui->is_up))
Damjan Marion00a9dca2016-08-17 17:05:46 +02001998 {
1999 error = VHOST_USER_TX_FUNC_ERROR_NOT_READY;
Pierre Pfistere21c5282016-09-21 08:04:59 +01002000 goto done3;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002001 }
Damjan Marion920ecc22016-01-12 18:34:24 +01002002
Pierre Pfistere21c5282016-09-21 08:04:59 +01002003 qid =
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002004 VHOST_VRING_IDX_RX (*vec_elt_at_index
Damjan Marion586afd72017-04-05 19:18:20 +02002005 (vui->per_cpu_tx_qid, vlib_get_thread_index ()));
Pierre Pfistere21c5282016-09-21 08:04:59 +01002006 rxvq = &vui->vrings[qid];
2007 if (PREDICT_FALSE (vui->use_tx_spinlock))
2008 vhost_user_vring_lock (vui, qid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002009
Damjan Marion00a9dca2016-08-17 17:05:46 +02002010 qsz_mask = rxvq->qsz - 1; /* qsz is always power of 2 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002011
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002012retry:
2013 error = VHOST_USER_TX_FUNC_ERROR_NONE;
2014 tx_headers_len = 0;
2015 copy_len = 0;
2016 while (n_left > 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002017 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002018 vlib_buffer_t *b0, *current_b0;
2019 u16 desc_head, desc_index, desc_len;
2020 vring_desc_t *desc_table;
2021 uword buffer_map_addr;
2022 u32 buffer_len;
2023 u16 bytes_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002024
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002025 if (PREDICT_TRUE (n_left > 1))
2026 vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD);
2027
2028 b0 = vlib_get_buffer (vm, buffers[0]);
2029
2030 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002031 {
Damjan Marion586afd72017-04-05 19:18:20 +02002032 vum->cpus[thread_index].current_trace =
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002033 vlib_add_trace (vm, node, b0,
Damjan Marion586afd72017-04-05 19:18:20 +02002034 sizeof (*vum->cpus[thread_index].current_trace));
2035 vhost_user_tx_trace (vum->cpus[thread_index].current_trace,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002036 vui, qid / 2, b0, rxvq);
2037 }
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002038
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002039 if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx))
2040 {
2041 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2042 goto done;
2043 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002044
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002045 desc_table = rxvq->desc;
2046 desc_head = desc_index =
2047 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2048
2049 /* Go deeper in case of indirect descriptor
2050 * I don't know of any driver providing indirect for RX. */
2051 if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2052 {
2053 if (PREDICT_FALSE
2054 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002055 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002056 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002057 goto done;
2058 }
2059 if (PREDICT_FALSE
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002060 (!(desc_table =
2061 map_guest_mem (vui, rxvq->desc[desc_index].addr,
2062 &map_hint))))
Pierre Pfisterba1d0462016-07-27 16:38:20 +01002063 {
2064 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2065 goto done;
2066 }
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002067 desc_index = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002068 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002069
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002070 desc_len = vui->virtio_net_hdr_sz;
2071 buffer_map_addr = desc_table[desc_index].addr;
2072 buffer_len = desc_table[desc_index].len;
2073
2074 {
2075 // Get a header from the header array
2076 virtio_net_hdr_mrg_rxbuf_t *hdr =
Damjan Marion586afd72017-04-05 19:18:20 +02002077 &vum->cpus[thread_index].tx_headers[tx_headers_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002078 tx_headers_len++;
2079 hdr->hdr.flags = 0;
2080 hdr->hdr.gso_type = 0;
2081 hdr->num_buffers = 1; //This is local, no need to check
2082
2083 // Prepare a copy order executed later for the header
Damjan Marion586afd72017-04-05 19:18:20 +02002084 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002085 copy_len++;
2086 cpy->len = vui->virtio_net_hdr_sz;
2087 cpy->dst = buffer_map_addr;
2088 cpy->src = (uword) hdr;
2089 }
2090
2091 buffer_map_addr += vui->virtio_net_hdr_sz;
2092 buffer_len -= vui->virtio_net_hdr_sz;
2093 bytes_left = b0->current_length;
2094 current_b0 = b0;
2095 while (1)
2096 {
2097 if (buffer_len == 0)
2098 { //Get new output
2099 if (desc_table[desc_index].flags & VIRTQ_DESC_F_NEXT)
2100 {
2101 //Next one is chained
2102 desc_index = desc_table[desc_index].next;
2103 buffer_map_addr = desc_table[desc_index].addr;
2104 buffer_len = desc_table[desc_index].len;
2105 }
2106 else if (vui->virtio_net_hdr_sz == 12) //MRG is available
2107 {
2108 virtio_net_hdr_mrg_rxbuf_t *hdr =
Damjan Marion586afd72017-04-05 19:18:20 +02002109 &vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002110
2111 //Move from available to used buffer
2112 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id =
2113 desc_head;
2114 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len =
2115 desc_len;
2116 vhost_user_log_dirty_ring (vui, rxvq,
2117 ring[rxvq->last_used_idx &
2118 qsz_mask]);
2119
2120 rxvq->last_avail_idx++;
2121 rxvq->last_used_idx++;
2122 hdr->num_buffers++;
2123 desc_len = 0;
2124
2125 if (PREDICT_FALSE
2126 (rxvq->last_avail_idx == rxvq->avail->idx))
2127 {
2128 //Dequeue queued descriptors for this packet
2129 rxvq->last_used_idx -= hdr->num_buffers - 1;
2130 rxvq->last_avail_idx -= hdr->num_buffers - 1;
2131 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF;
2132 goto done;
2133 }
2134
2135 desc_table = rxvq->desc;
2136 desc_head = desc_index =
2137 rxvq->avail->ring[rxvq->last_avail_idx & qsz_mask];
2138 if (PREDICT_FALSE
2139 (rxvq->desc[desc_head].flags & VIRTQ_DESC_F_INDIRECT))
2140 {
2141 //It is seriously unlikely that a driver will put indirect descriptor
2142 //after non-indirect descriptor.
2143 if (PREDICT_FALSE
2144 (rxvq->desc[desc_head].len < sizeof (vring_desc_t)))
2145 {
2146 error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW;
2147 goto done;
2148 }
2149 if (PREDICT_FALSE
2150 (!(desc_table =
2151 map_guest_mem (vui,
2152 rxvq->desc[desc_index].addr,
2153 &map_hint))))
2154 {
2155 error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL;
2156 goto done;
2157 }
2158 desc_index = 0;
2159 }
2160 buffer_map_addr = desc_table[desc_index].addr;
2161 buffer_len = desc_table[desc_index].len;
2162 }
2163 else
2164 {
2165 error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG;
2166 goto done;
2167 }
2168 }
2169
2170 {
Damjan Marion586afd72017-04-05 19:18:20 +02002171 vhost_copy_t *cpy = &vum->cpus[thread_index].copy[copy_len];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002172 copy_len++;
2173 cpy->len = bytes_left;
2174 cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len;
2175 cpy->dst = buffer_map_addr;
2176 cpy->src = (uword) vlib_buffer_get_current (current_b0) +
2177 current_b0->current_length - bytes_left;
2178
2179 bytes_left -= cpy->len;
2180 buffer_len -= cpy->len;
2181 buffer_map_addr += cpy->len;
2182 desc_len += cpy->len;
2183
Pierre Pfister14ac8012016-12-08 07:58:47 +00002184 CLIB_PREFETCH (&rxvq->desc, CLIB_CACHE_LINE_BYTES, LOAD);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002185 }
2186
2187 // Check if vlib buffer has more data. If not, get more or break.
2188 if (PREDICT_TRUE (!bytes_left))
2189 {
2190 if (PREDICT_FALSE
2191 (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT))
2192 {
2193 current_b0 = vlib_get_buffer (vm, current_b0->next_buffer);
2194 bytes_left = current_b0->current_length;
2195 }
2196 else
2197 {
2198 //End of packet
2199 break;
2200 }
2201 }
2202 }
2203
2204 //Move from available to used ring
2205 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].id = desc_head;
2206 rxvq->used->ring[rxvq->last_used_idx & qsz_mask].len = desc_len;
2207 vhost_user_log_dirty_ring (vui, rxvq,
2208 ring[rxvq->last_used_idx & qsz_mask]);
2209 rxvq->last_avail_idx++;
2210 rxvq->last_used_idx++;
2211
2212 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
2213 {
Damjan Marion586afd72017-04-05 19:18:20 +02002214 vum->cpus[thread_index].current_trace->hdr =
2215 vum->cpus[thread_index].tx_headers[tx_headers_len - 1];
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002216 }
2217
2218 n_left--; //At the end for error counting when 'goto done' is invoked
2219 buffers++;
2220 }
2221
2222done:
2223 //Do the memory copies
2224 if (PREDICT_FALSE
Damjan Marion586afd72017-04-05 19:18:20 +02002225 (vhost_user_tx_copy (vui, vum->cpus[thread_index].copy,
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002226 copy_len, &map_hint)))
2227 {
Steven95827e42017-05-18 21:22:00 -07002228 vlib_error_count (vm, node->node_index,
2229 VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1);
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002230 }
2231
2232 CLIB_MEMORY_BARRIER ();
2233 rxvq->used->idx = rxvq->last_used_idx;
2234 vhost_user_log_dirty_ring (vui, rxvq, idx);
2235
2236 /*
2237 * When n_left is set, error is always set to something too.
2238 * In case error is due to lack of remaining buffers, we go back up and
2239 * retry.
2240 * The idea is that it is better to waste some time on packets
2241 * that have been processed already than dropping them and get
2242 * more fresh packets with a good likelyhood that they will be dropped too.
2243 * This technique also gives more time to VM driver to pick-up packets.
2244 * In case the traffic flows from physical to virtual interfaces, this
2245 * technique will end-up leveraging the physical NIC buffer in order to
2246 * absorb the VM's CPU jitter.
2247 */
2248 if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry)
2249 {
2250 retry--;
2251 goto retry;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002252 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002253
Ed Warnickecb9cada2015-12-08 15:45:58 -07002254 /* interrupt (call) handling */
Steven7312cc72017-03-15 21:18:55 -07002255 if ((rxvq->callfd_idx != ~0) &&
2256 !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
Damjan Marion00a9dca2016-08-17 17:05:46 +02002257 {
Pierre Pfisterd3eb90e2016-11-29 15:36:14 +00002258 rxvq->n_since_last_int += frame->n_vectors - n_left;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002259
Damjan Marion00a9dca2016-08-17 17:05:46 +02002260 if (rxvq->n_since_last_int > vum->coalesce_frames)
2261 vhost_user_send_call (vm, rxvq);
2262 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002263
Pierre Pfistere21c5282016-09-21 08:04:59 +01002264 vhost_user_vring_unlock (vui, qid);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002265
Pierre Pfistere21c5282016-09-21 08:04:59 +01002266done3:
Damjan Marion00a9dca2016-08-17 17:05:46 +02002267 if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE))
2268 {
2269 vlib_error_count (vm, node->node_index, error, n_left);
2270 vlib_increment_simple_counter
2271 (vnet_main.interface_main.sw_if_counters
2272 + VNET_INTERFACE_COUNTER_DROP,
Damjan Marion586afd72017-04-05 19:18:20 +02002273 vlib_get_thread_index (), vui->sw_if_index, n_left);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002274 }
Pierre Pfister328e99b2016-02-12 13:18:42 +00002275
Ed Warnickecb9cada2015-12-08 15:45:58 -07002276 vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors);
2277 return frame->n_vectors;
2278}
2279
Stevenf3b53642017-05-01 14:03:02 -07002280static uword
2281vhost_user_send_interrupt_process (vlib_main_t * vm,
2282 vlib_node_runtime_t * rt, vlib_frame_t * f)
2283{
2284 vhost_user_intf_t *vui;
2285 f64 timeout = 3153600000.0 /* 100 years */ ;
2286 uword event_type, *event_data = 0;
2287 vhost_user_main_t *vum = &vhost_user_main;
2288 u16 *queue;
2289 f64 now, poll_time_remaining;
2290 f64 next_timeout;
2291 u8 stop_timer = 0;
2292
2293 while (1)
2294 {
2295 poll_time_remaining =
2296 vlib_process_wait_for_event_or_clock (vm, timeout);
2297 event_type = vlib_process_get_events (vm, &event_data);
2298 vec_reset_length (event_data);
2299
2300 /*
2301 * Use the remaining timeout if it is less than coalesce time to avoid
2302 * resetting the existing timer in the middle of expiration
2303 */
2304 timeout = poll_time_remaining;
2305 if (vlib_process_suspend_time_is_zero (timeout) ||
2306 (timeout > vum->coalesce_time))
2307 timeout = vum->coalesce_time;
2308
2309 now = vlib_time_now (vm);
2310 switch (event_type)
2311 {
2312 case VHOST_USER_EVENT_STOP_TIMER:
2313 stop_timer = 1;
2314 break;
2315
2316 case VHOST_USER_EVENT_START_TIMER:
2317 stop_timer = 0;
2318 if (!vlib_process_suspend_time_is_zero (poll_time_remaining))
2319 break;
2320 /* fall through */
2321
2322 case ~0:
2323 /* *INDENT-OFF* */
2324 pool_foreach (vui, vum->vhost_user_interfaces, {
2325 next_timeout = timeout;
2326 vec_foreach (queue, vui->rx_queues)
2327 {
2328 vhost_user_vring_t *rxvq =
2329 &vui->vrings[VHOST_VRING_IDX_RX (*queue)];
2330 vhost_user_vring_t *txvq =
2331 &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
2332
2333 if (txvq->n_since_last_int)
2334 {
2335 if (now >= txvq->int_deadline)
2336 vhost_user_send_call (vm, txvq);
2337 else
2338 next_timeout = txvq->int_deadline - now;
2339 }
2340
2341 if (rxvq->n_since_last_int)
2342 {
2343 if (now >= rxvq->int_deadline)
2344 vhost_user_send_call (vm, rxvq);
2345 else
2346 next_timeout = rxvq->int_deadline - now;
2347 }
2348
2349 if ((next_timeout < timeout) && (next_timeout > 0.0))
2350 timeout = next_timeout;
2351 }
2352 });
2353 /* *INDENT-ON* */
2354 break;
2355
2356 default:
2357 clib_warning ("BUG: unhandled event type %d", event_type);
2358 break;
2359 }
2360 /* No less than 1 millisecond */
2361 if (timeout < 1e-3)
2362 timeout = 1e-3;
2363 if (stop_timer)
2364 timeout = 3153600000.0;
2365 }
2366 return 0;
2367}
2368
2369/* *INDENT-OFF* */
2370VLIB_REGISTER_NODE (vhost_user_send_interrupt_node,static) = {
2371 .function = vhost_user_send_interrupt_process,
2372 .type = VLIB_NODE_TYPE_PROCESS,
2373 .name = "vhost-user-send-interrupt-process",
2374};
2375/* *INDENT-ON* */
2376
2377static clib_error_t *
2378vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index,
2379 u32 qid, vnet_hw_interface_rx_mode mode)
2380{
2381 vlib_main_t *vm = vnm->vlib_main;
2382 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
2383 vhost_user_main_t *vum = &vhost_user_main;
2384 vhost_user_intf_t *vui =
2385 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
2386 vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)];
2387
2388 if ((mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2389 (mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE))
2390 {
2391 if (txvq->mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2392 {
2393 vum->ifq_count++;
2394 // Start the timer if this is the first encounter on interrupt
2395 // interface/queue
2396 if ((vum->ifq_count == 1) &&
2397 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2398 vlib_process_signal_event (vm,
2399 vhost_user_send_interrupt_node.index,
2400 VHOST_USER_EVENT_START_TIMER, 0);
2401 }
2402 }
2403 else if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2404 {
2405 if (((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2406 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)) &&
2407 vum->ifq_count)
2408 {
2409 vum->ifq_count--;
2410 // Stop the timer if there is no more interrupt interface/queue
2411 if ((vum->ifq_count == 0) &&
2412 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2413 vlib_process_signal_event (vm,
2414 vhost_user_send_interrupt_node.index,
2415 VHOST_USER_EVENT_STOP_TIMER, 0);
2416 }
2417 }
2418
2419 txvq->mode = mode;
2420 if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
2421 txvq->used->flags = VRING_USED_F_NO_NOTIFY;
2422 else if ((mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE) ||
2423 (mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT))
2424 txvq->used->flags = 0;
2425 else
2426 {
2427 clib_warning ("BUG: unhandled mode %d changed for if %d queue %d", mode,
2428 hw_if_index, qid);
2429 return clib_error_return (0, "unsupported");
2430 }
2431
2432 return 0;
2433}
2434
Ed Warnickecb9cada2015-12-08 15:45:58 -07002435static clib_error_t *
Damjan Marion00a9dca2016-08-17 17:05:46 +02002436vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
2437 u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002438{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002439 vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002440 uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002441 vhost_user_main_t *vum = &vhost_user_main;
2442 vhost_user_intf_t *vui =
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002443 pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002444
2445 vui->admin_up = is_up;
2446
2447 if (is_up)
2448 vnet_hw_interface_set_flags (vnm, vui->hw_if_index,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002449 VNET_HW_INTERFACE_FLAG_LINK_UP);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002450
2451 return /* no error */ 0;
2452}
2453
Damjan Marion00a9dca2016-08-17 17:05:46 +02002454/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002455VNET_DEVICE_CLASS (vhost_user_dev_class,static) = {
2456 .name = "vhost-user",
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002457 .tx_function = vhost_user_tx,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002458 .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR,
2459 .tx_function_error_strings = vhost_user_tx_func_error_strings,
2460 .format_device_name = format_vhost_user_interface_name,
2461 .name_renumber = vhost_user_name_renumber,
2462 .admin_up_down_function = vhost_user_interface_admin_up_down,
Stevenf3b53642017-05-01 14:03:02 -07002463 .rx_mode_change_function = vhost_user_interface_rx_mode_change,
Pierre Pfister116ea4b2016-11-08 15:49:28 +00002464 .format_tx_trace = format_vhost_trace,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002465};
2466
Damjan Marion1c80e832016-05-11 23:07:18 +02002467VLIB_DEVICE_TX_FUNCTION_MULTIARCH (vhost_user_dev_class,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002468 vhost_user_tx)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002469/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +02002470
Ed Warnickecb9cada2015-12-08 15:45:58 -07002471static uword
2472vhost_user_process (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002473 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002474{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002475 vhost_user_main_t *vum = &vhost_user_main;
2476 vhost_user_intf_t *vui;
2477 struct sockaddr_un sun;
2478 int sockfd;
2479 unix_file_t template = { 0 };
2480 f64 timeout = 3153600000.0 /* 100 years */ ;
2481 uword *event_data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002482
Steven0d150bb2017-03-22 12:05:19 -07002483 sockfd = -1;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002484 sun.sun_family = AF_UNIX;
2485 template.read_function = vhost_user_socket_read;
2486 template.error_function = vhost_user_socket_error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002487
Damjan Marion00a9dca2016-08-17 17:05:46 +02002488 while (1)
2489 {
2490 vlib_process_wait_for_event_or_clock (vm, timeout);
2491 vlib_process_get_events (vm, &event_data);
2492 vec_reset_length (event_data);
2493
2494 timeout = 3.0;
2495
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002496 /* *INDENT-OFF* */
2497 pool_foreach (vui, vum->vhost_user_interfaces, {
Damjan Marion00a9dca2016-08-17 17:05:46 +02002498
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002499 if (vui->unix_server_index == ~0) { //Nothing to do for server sockets
2500 if (vui->unix_file_index == ~0)
2501 {
Steven0d150bb2017-03-22 12:05:19 -07002502 if ((sockfd < 0) &&
2503 ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0))
2504 {
2505 /*
2506 * 1st time error or new error for this interface,
2507 * spit out the message and record the error
2508 */
2509 if (!vui->sock_errno || (vui->sock_errno != errno))
2510 {
2511 clib_unix_warning
2512 ("Error: Could not open unix socket for %s",
2513 vui->sock_filename);
2514 vui->sock_errno = errno;
2515 }
2516 continue;
2517 }
2518
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002519 /* try to connect */
2520 strncpy (sun.sun_path, (char *) vui->sock_filename,
2521 sizeof (sun.sun_path) - 1);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002522
Andrew Yourtchenko0c3d4672017-01-03 16:52:22 +00002523 /* Avoid hanging VPP if the other end does not accept */
Dave Barach8f544962017-01-18 10:23:22 -05002524 if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0)
2525 clib_unix_warning ("fcntl");
2526
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002527 if (connect (sockfd, (struct sockaddr *) &sun,
2528 sizeof (struct sockaddr_un)) == 0)
2529 {
Andrew Yourtchenko0c3d4672017-01-03 16:52:22 +00002530 /* Set the socket to blocking as it was before */
Dave Barach8f544962017-01-18 10:23:22 -05002531 if (fcntl(sockfd, F_SETFL, 0) < 0)
2532 clib_unix_warning ("fcntl2");
2533
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002534 vui->sock_errno = 0;
2535 template.file_descriptor = sockfd;
2536 template.private_data =
2537 vui - vhost_user_main.vhost_user_interfaces;
2538 vui->unix_file_index = unix_file_add (&unix_main, &template);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002539
Steven0d150bb2017-03-22 12:05:19 -07002540 /* This sockfd is considered consumed */
2541 sockfd = -1;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002542 }
2543 else
2544 {
2545 vui->sock_errno = errno;
2546 }
2547 }
2548 else
2549 {
2550 /* check if socket is alive */
2551 int error = 0;
2552 socklen_t len = sizeof (error);
2553 int fd = UNIX_GET_FD(vui->unix_file_index);
2554 int retval =
2555 getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002556
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002557 if (retval)
2558 {
2559 DBG_SOCK ("getsockopt returned %d", retval);
2560 vhost_user_if_disconnect (vui);
2561 }
2562 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02002563 }
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002564 });
2565 /* *INDENT-ON* */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002566 }
2567 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002568}
2569
Damjan Marion00a9dca2016-08-17 17:05:46 +02002570/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002571VLIB_REGISTER_NODE (vhost_user_process_node,static) = {
2572 .function = vhost_user_process,
2573 .type = VLIB_NODE_TYPE_PROCESS,
2574 .name = "vhost-user-process",
2575};
Damjan Marion00a9dca2016-08-17 17:05:46 +02002576/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002577
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002578/**
2579 * Disables and reset interface structure.
2580 * It can then be either init again, or removed from used interfaces.
2581 */
2582static void
2583vhost_user_term_if (vhost_user_intf_t * vui)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002584{
Ole Troan553a4112017-01-10 10:07:04 +01002585 int q;
Steven5445f5f2017-04-25 16:16:00 -07002586 vhost_user_main_t *vum = &vhost_user_main;
Ole Troan553a4112017-01-10 10:07:04 +01002587
Ed Warnickecb9cada2015-12-08 15:45:58 -07002588 // disconnect interface sockets
Damjan Marion00a9dca2016-08-17 17:05:46 +02002589 vhost_user_if_disconnect (vui);
Pierre Pfisterfbb2ef62016-11-16 02:43:29 +00002590 vhost_user_update_iface_state (vui);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002591
Ole Troan553a4112017-01-10 10:07:04 +01002592 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2593 {
2594 clib_mem_free ((void *) vui->vring_locks[q]);
2595 }
2596
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002597 if (vui->unix_server_index != ~0)
2598 {
2599 //Close server socket
2600 unix_file_t *uf = pool_elt_at_index (unix_main.file_pool,
2601 vui->unix_server_index);
2602 unix_file_del (&unix_main, uf);
2603 vui->unix_server_index = ~0;
Steven53129422017-04-21 13:31:50 -07002604 unlink (vui->sock_filename);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002605 }
Steven5445f5f2017-04-25 16:16:00 -07002606
2607 mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename,
2608 &vui->if_index);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002609}
Ed Warnickecb9cada2015-12-08 15:45:58 -07002610
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002611int
2612vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index)
2613{
2614 vhost_user_main_t *vum = &vhost_user_main;
2615 vhost_user_intf_t *vui;
2616 int rv = 0;
2617 vnet_hw_interface_t *hwif;
Stevenf3b53642017-05-01 14:03:02 -07002618 u16 *queue;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002619
2620 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2621 hwif->dev_class_index != vhost_user_dev_class.index)
2622 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
2623
2624 DBG_SOCK ("Deleting vhost-user interface %s (instance %d)",
2625 hwif->name, hwif->dev_instance);
2626
2627 vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
2628
Stevenf3b53642017-05-01 14:03:02 -07002629 vec_foreach (queue, vui->rx_queues)
2630 {
2631 vhost_user_vring_t *txvq;
2632
2633 txvq = &vui->vrings[VHOST_VRING_IDX_TX (*queue)];
2634 if ((vum->ifq_count > 0) &&
2635 ((txvq->mode == VNET_HW_INTERFACE_RX_MODE_INTERRUPT) ||
2636 (txvq->mode == VNET_HW_INTERFACE_RX_MODE_ADAPTIVE)))
2637 {
2638 vum->ifq_count--;
2639 // Stop the timer if there is no more interrupt interface/queue
2640 if ((vum->ifq_count == 0) &&
2641 (vum->coalesce_time > 0.0) && (vum->coalesce_frames > 0))
2642 {
2643 vlib_process_signal_event (vm,
2644 vhost_user_send_interrupt_node.index,
2645 VHOST_USER_EVENT_STOP_TIMER, 0);
2646 break;
2647 }
2648 }
2649 }
2650
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002651 // Disable and reset interface
2652 vhost_user_term_if (vui);
2653
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002654 // Reset renumbered iface
2655 if (hwif->dev_instance <
2656 vec_len (vum->show_dev_instance_by_real_dev_instance))
2657 vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0;
2658
2659 // Delete ethernet interface
Ed Warnickecb9cada2015-12-08 15:45:58 -07002660 ethernet_delete_interface (vnm, vui->hw_if_index);
Wojciech Decd8e47872017-01-17 21:45:11 +01002661
2662 // Back to pool
2663 pool_put (vum->vhost_user_interfaces, vui);
2664
Ed Warnickecb9cada2015-12-08 15:45:58 -07002665 return rv;
2666}
2667
Steven53129422017-04-21 13:31:50 -07002668static clib_error_t *
2669vhost_user_exit (vlib_main_t * vm)
2670{
2671 vnet_main_t *vnm = vnet_get_main ();
2672 vhost_user_main_t *vum = &vhost_user_main;
2673 vhost_user_intf_t *vui;
2674
Steven41748862017-04-25 13:49:51 -07002675 vlib_worker_thread_barrier_sync (vlib_get_main ());
Steven53129422017-04-21 13:31:50 -07002676 /* *INDENT-OFF* */
2677 pool_foreach (vui, vum->vhost_user_interfaces, {
2678 vhost_user_delete_if (vnm, vm, vui->sw_if_index);
2679 });
2680 /* *INDENT-ON* */
Steven41748862017-04-25 13:49:51 -07002681 vlib_worker_thread_barrier_release (vlib_get_main ());
Steven53129422017-04-21 13:31:50 -07002682 return 0;
2683}
2684
2685VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit);
2686
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002687/**
2688 * Open server unix socket on specified sock_filename.
2689 */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002690static int
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002691vhost_user_init_server_sock (const char *sock_filename, int *sock_fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002692{
Pierre Pfister5afccb22016-07-25 14:32:02 +01002693 int rv = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002694 struct sockaddr_un un = { };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002695 int fd;
2696 /* create listening socket */
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002697 if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
2698 return VNET_API_ERROR_SYSCALL_ERROR_1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002699
2700 un.sun_family = AF_UNIX;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002701 strncpy ((char *) un.sun_path, (char *) sock_filename,
2702 sizeof (un.sun_path) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002703
2704 /* remove if exists */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002705 unlink ((char *) sock_filename);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002706
Damjan Marion00a9dca2016-08-17 17:05:46 +02002707 if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1)
2708 {
2709 rv = VNET_API_ERROR_SYSCALL_ERROR_2;
2710 goto error;
2711 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002712
Damjan Marion00a9dca2016-08-17 17:05:46 +02002713 if (listen (fd, 1) == -1)
2714 {
2715 rv = VNET_API_ERROR_SYSCALL_ERROR_3;
2716 goto error;
2717 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002718
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002719 *sock_fd = fd;
2720 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002721
2722error:
Damjan Marion00a9dca2016-08-17 17:05:46 +02002723 close (fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002724 return rv;
2725}
2726
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002727/**
2728 * Create ethernet interface for vhost user interface.
2729 */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002730static void
2731vhost_user_create_ethernet (vnet_main_t * vnm, vlib_main_t * vm,
2732 vhost_user_intf_t * vui, u8 * hwaddress)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002733{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002734 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002735 u8 hwaddr[6];
Damjan Marion00a9dca2016-08-17 17:05:46 +02002736 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002737
2738 /* create hw and sw interface */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002739 if (hwaddress)
2740 {
2741 clib_memcpy (hwaddr, hwaddress, 6);
2742 }
2743 else
2744 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002745 random_u32 (&vum->random);
2746 clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random));
Damjan Marion00a9dca2016-08-17 17:05:46 +02002747 hwaddr[0] = 2;
2748 hwaddr[1] = 0xfe;
2749 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002750
2751 error = ethernet_register_interface
2752 (vnm,
2753 vhost_user_dev_class.index,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002754 vui - vum->vhost_user_interfaces /* device instance */ ,
2755 hwaddr /* ethernet address */ ,
2756 &vui->hw_if_index, 0 /* flag change */ );
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002757
Ed Warnickecb9cada2015-12-08 15:45:58 -07002758 if (error)
2759 clib_error_report (error);
Pierre Pfister328e99b2016-02-12 13:18:42 +00002760
2761 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, vui->hw_if_index);
2762 hi->max_l3_packet_bytes[VLIB_RX] = hi->max_l3_packet_bytes[VLIB_TX] = 9000;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002763}
2764
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002765/*
2766 * Initialize vui with specified attributes
2767 */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002768static void
2769vhost_user_vui_init (vnet_main_t * vnm,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002770 vhost_user_intf_t * vui,
2771 int server_sock_fd,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002772 const char *sock_filename,
Stevenf3b53642017-05-01 14:03:02 -07002773 u64 feature_mask, u32 * sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002774{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002775 vnet_sw_interface_t *sw;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01002776 int q;
Steven5445f5f2017-04-25 16:16:00 -07002777 vhost_user_main_t *vum = &vhost_user_main;
Stevenf3b53642017-05-01 14:03:02 -07002778 vnet_hw_interface_t *hw;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002779
Stevenf3b53642017-05-01 14:03:02 -07002780 hw = vnet_get_hw_interface (vnm, vui->hw_if_index);
2781 sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002782 if (server_sock_fd != -1)
2783 {
2784 unix_file_t template = { 0 };
2785 template.read_function = vhost_user_socksvr_accept_ready;
2786 template.file_descriptor = server_sock_fd;
Steven5445f5f2017-04-25 16:16:00 -07002787 template.private_data = vui - vum->vhost_user_interfaces; //hw index
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002788 vui->unix_server_index = unix_file_add (&unix_main, &template);
2789 }
2790 else
2791 {
2792 vui->unix_server_index = ~0;
2793 }
2794
Ed Warnickecb9cada2015-12-08 15:45:58 -07002795 vui->sw_if_index = sw->sw_if_index;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002796 strncpy (vui->sock_filename, sock_filename,
2797 ARRAY_LEN (vui->sock_filename) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002798 vui->sock_errno = 0;
2799 vui->is_up = 0;
2800 vui->feature_mask = feature_mask;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002801 vui->unix_file_index = ~0;
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01002802 vui->log_base_addr = 0;
Steven5445f5f2017-04-25 16:16:00 -07002803 vui->if_index = vui - vum->vhost_user_interfaces;
2804 mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename,
2805 &vui->if_index, 0);
Yoann Desmouceaux4667c222016-02-24 22:51:00 +01002806
Pierre Pfistere21c5282016-09-21 08:04:59 +01002807 for (q = 0; q < VHOST_VRING_MAX_N; q++)
2808 vhost_user_vring_init (vui, q);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002809
Stevenf3b53642017-05-01 14:03:02 -07002810 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002811 vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002812
2813 if (sw_if_index)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002814 *sw_if_index = vui->sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002815
Pierre Pfistere21c5282016-09-21 08:04:59 +01002816 for (q = 0; q < VHOST_VRING_MAX_N; q++)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002817 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01002818 vui->vring_locks[q] = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
2819 CLIB_CACHE_LINE_BYTES);
2820 memset ((void *) vui->vring_locks[q], 0, CLIB_CACHE_LINE_BYTES);
Damjan Marion00a9dca2016-08-17 17:05:46 +02002821 }
Pierre Pfistere21c5282016-09-21 08:04:59 +01002822
2823 vec_validate (vui->per_cpu_tx_qid,
2824 vlib_get_thread_main ()->n_vlib_mains - 1);
2825 vhost_user_tx_thread_placement (vui);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002826}
2827
Damjan Marion00a9dca2016-08-17 17:05:46 +02002828int
2829vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
2830 const char *sock_filename,
2831 u8 is_server,
2832 u32 * sw_if_index,
2833 u64 feature_mask,
Stevenf3b53642017-05-01 14:03:02 -07002834 u8 renumber, u32 custom_dev_instance, u8 * hwaddr)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002835{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002836 vhost_user_intf_t *vui = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002837 u32 sw_if_idx = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002838 int rv = 0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002839 int server_sock_fd = -1;
Steven7312cc72017-03-15 21:18:55 -07002840 vhost_user_main_t *vum = &vhost_user_main;
Steven5445f5f2017-04-25 16:16:00 -07002841 uword *if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002842
Wojciech Dec3cd9eed2017-01-03 10:38:37 +01002843 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2844 {
2845 return VNET_API_ERROR_INVALID_ARGUMENT;
2846 }
2847
Steven5445f5f2017-04-25 16:16:00 -07002848 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2849 if (if_index)
2850 {
2851 if (sw_if_index)
2852 {
2853 vui = &vum->vhost_user_interfaces[*if_index];
2854 *sw_if_index = vui->sw_if_index;
2855 }
2856 return VNET_API_ERROR_IF_ALREADY_EXISTS;
2857 }
2858
Damjan Marion00a9dca2016-08-17 17:05:46 +02002859 if (is_server)
2860 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002861 if ((rv =
2862 vhost_user_init_server_sock (sock_filename, &server_sock_fd)) != 0)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002863 {
2864 return rv;
2865 }
2866 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002867
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002868 pool_get (vhost_user_main.vhost_user_interfaces, vui);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002869
Pierre Pfisteref65cb02016-02-19 13:52:44 +00002870 vhost_user_create_ethernet (vnm, vm, vui, hwaddr);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002871 vhost_user_vui_init (vnm, vui, server_sock_fd, sock_filename,
Stevenf3b53642017-05-01 14:03:02 -07002872 feature_mask, &sw_if_idx);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002873
Damjan Marion00a9dca2016-08-17 17:05:46 +02002874 if (renumber)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002875 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002876
2877 if (sw_if_index)
Damjan Marion00a9dca2016-08-17 17:05:46 +02002878 *sw_if_index = sw_if_idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002879
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002880 // Process node must connect
2881 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
Steven7312cc72017-03-15 21:18:55 -07002882
Ed Warnickecb9cada2015-12-08 15:45:58 -07002883 return rv;
2884}
2885
Damjan Marion00a9dca2016-08-17 17:05:46 +02002886int
2887vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
2888 const char *sock_filename,
2889 u8 is_server,
2890 u32 sw_if_index,
Stevenf3b53642017-05-01 14:03:02 -07002891 u64 feature_mask, u8 renumber, u32 custom_dev_instance)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002892{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002893 vhost_user_main_t *vum = &vhost_user_main;
2894 vhost_user_intf_t *vui = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002895 u32 sw_if_idx = ~0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002896 int server_sock_fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002897 int rv = 0;
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002898 vnet_hw_interface_t *hwif;
Steven5445f5f2017-04-25 16:16:00 -07002899 uword *if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002900
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002901 if (!(hwif = vnet_get_sup_hw_interface (vnm, sw_if_index)) ||
2902 hwif->dev_class_index != vhost_user_dev_class.index)
2903 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002904
Steven5445f5f2017-04-25 16:16:00 -07002905 if (sock_filename == NULL || !(strlen (sock_filename) > 0))
2906 return VNET_API_ERROR_INVALID_ARGUMENT;
2907
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002908 vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002909
Steven5445f5f2017-04-25 16:16:00 -07002910 /*
2911 * Disallow changing the interface to have the same path name
2912 * as other interface
2913 */
2914 if_index = mhash_get (&vum->if_index_by_sock_name, (void *) sock_filename);
2915 if (if_index && (*if_index != vui->if_index))
2916 return VNET_API_ERROR_IF_ALREADY_EXISTS;
2917
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002918 // First try to open server socket
Damjan Marion00a9dca2016-08-17 17:05:46 +02002919 if (is_server)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002920 if ((rv = vhost_user_init_server_sock (sock_filename,
2921 &server_sock_fd)) != 0)
2922 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002923
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002924 vhost_user_term_if (vui);
2925 vhost_user_vui_init (vnm, vui, server_sock_fd,
Stevenf3b53642017-05-01 14:03:02 -07002926 sock_filename, feature_mask, &sw_if_idx);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002927
Damjan Marion00a9dca2016-08-17 17:05:46 +02002928 if (renumber)
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002929 vnet_interface_name_renumber (sw_if_idx, custom_dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002930
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00002931 // Process node must connect
2932 vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0);
Steven7312cc72017-03-15 21:18:55 -07002933
Ed Warnickecb9cada2015-12-08 15:45:58 -07002934 return rv;
2935}
2936
2937clib_error_t *
2938vhost_user_connect_command_fn (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02002939 unformat_input_t * input,
2940 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002941{
Damjan Marion00a9dca2016-08-17 17:05:46 +02002942 unformat_input_t _line_input, *line_input = &_line_input;
2943 u8 *sock_filename = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002944 u32 sw_if_index;
2945 u8 is_server = 0;
Pierre Pfistere21c5282016-09-21 08:04:59 +01002946 u64 feature_mask = (u64) ~ (0ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002947 u8 renumber = 0;
2948 u32 custom_dev_instance = ~0;
Pierre Pfisteref65cb02016-02-19 13:52:44 +00002949 u8 hwaddr[6];
2950 u8 *hw = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -05002951 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002952
2953 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +02002954 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002955 return 0;
2956
Damjan Marion00a9dca2016-08-17 17:05:46 +02002957 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2958 {
2959 if (unformat (line_input, "socket %s", &sock_filename))
2960 ;
2961 else if (unformat (line_input, "server"))
2962 is_server = 1;
2963 else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
2964 ;
2965 else
2966 if (unformat
2967 (line_input, "hwaddr %U", unformat_ethernet_address, hwaddr))
2968 hw = hwaddr;
2969 else if (unformat (line_input, "renumber %d", &custom_dev_instance))
2970 {
2971 renumber = 1;
2972 }
2973 else
Billy McFalla9a20e72017-02-15 11:39:12 -05002974 {
2975 error = clib_error_return (0, "unknown input `%U'",
2976 format_unformat_error, line_input);
2977 goto done;
2978 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002979 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002980
Damjan Marion00a9dca2016-08-17 17:05:46 +02002981 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07002982
Pierre Pfister5afccb22016-07-25 14:32:02 +01002983 int rv;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002984 if ((rv = vhost_user_create_if (vnm, vm, (char *) sock_filename,
2985 is_server, &sw_if_index, feature_mask,
Stevenf3b53642017-05-01 14:03:02 -07002986 renumber, custom_dev_instance, hw)))
Damjan Marion00a9dca2016-08-17 17:05:46 +02002987 {
Billy McFalla9a20e72017-02-15 11:39:12 -05002988 error = clib_error_return (0, "vhost_user_create_if returned %d", rv);
2989 goto done;
Damjan Marion00a9dca2016-08-17 17:05:46 +02002990 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002991
Damjan Marion00a9dca2016-08-17 17:05:46 +02002992 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
2993 sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -05002994
2995done:
2996 vec_free (sock_filename);
2997 unformat_free (line_input);
2998
2999 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003000}
3001
3002clib_error_t *
3003vhost_user_delete_command_fn (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02003004 unformat_input_t * input,
3005 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003006{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003007 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003008 u32 sw_if_index = ~0;
Pierre Pfisterece983d2016-11-21 12:52:22 +00003009 vnet_main_t *vnm = vnet_get_main ();
Billy McFalla9a20e72017-02-15 11:39:12 -05003010 clib_error_t *error = NULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003011
3012 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +02003013 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003014 return 0;
3015
Damjan Marion00a9dca2016-08-17 17:05:46 +02003016 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3017 {
3018 if (unformat (line_input, "sw_if_index %d", &sw_if_index))
3019 ;
Pierre Pfisterece983d2016-11-21 12:52:22 +00003020 else if (unformat
3021 (line_input, "%U", unformat_vnet_sw_interface, vnm,
3022 &sw_if_index))
3023 {
3024 vnet_hw_interface_t *hwif =
3025 vnet_get_sup_hw_interface (vnm, sw_if_index);
3026 if (hwif == NULL ||
3027 vhost_user_dev_class.index != hwif->dev_class_index)
Billy McFalla9a20e72017-02-15 11:39:12 -05003028 {
3029 error = clib_error_return (0, "Not a vhost interface");
3030 goto done;
3031 }
Pierre Pfisterece983d2016-11-21 12:52:22 +00003032 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003033 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003034 {
3035 error = clib_error_return (0, "unknown input `%U'",
3036 format_unformat_error, line_input);
3037 goto done;
3038 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003039 }
Billy McFalla9a20e72017-02-15 11:39:12 -05003040
Damjan Marion00a9dca2016-08-17 17:05:46 +02003041 vhost_user_delete_if (vnm, vm, sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -05003042
3043done:
3044 unformat_free (line_input);
3045
3046 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003047}
3048
Damjan Marion00a9dca2016-08-17 17:05:46 +02003049int
3050vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
3051 vhost_user_intf_details_t ** out_vuids)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003052{
3053 int rv = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003054 vhost_user_main_t *vum = &vhost_user_main;
3055 vhost_user_intf_t *vui;
3056 vhost_user_intf_details_t *r_vuids = NULL;
3057 vhost_user_intf_details_t *vuid = NULL;
3058 u32 *hw_if_indices = 0;
3059 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003060 u8 *s = NULL;
3061 int i;
3062
3063 if (!out_vuids)
Damjan Marion00a9dca2016-08-17 17:05:46 +02003064 return -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003065
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003066 pool_foreach (vui, vum->vhost_user_interfaces,
3067 vec_add1 (hw_if_indices, vui->hw_if_index);
3068 );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003069
Damjan Marion00a9dca2016-08-17 17:05:46 +02003070 for (i = 0; i < vec_len (hw_if_indices); i++)
3071 {
3072 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003073 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003074
Damjan Marion00a9dca2016-08-17 17:05:46 +02003075 vec_add2 (r_vuids, vuid, 1);
3076 vuid->sw_if_index = vui->sw_if_index;
3077 vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz;
3078 vuid->features = vui->features;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003079 vuid->num_regions = vui->nregions;
Marek Gradzki0578cd12017-02-13 14:19:51 +01003080 vuid->is_server = vui->unix_server_index != ~0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003081 vuid->sock_errno = vui->sock_errno;
3082 strncpy ((char *) vuid->sock_filename, (char *) vui->sock_filename,
3083 ARRAY_LEN (vuid->sock_filename) - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003084
Damjan Marion00a9dca2016-08-17 17:05:46 +02003085 s = format (s, "%v%c", hi->name, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003086
Damjan Marion00a9dca2016-08-17 17:05:46 +02003087 strncpy ((char *) vuid->if_name, (char *) s,
3088 ARRAY_LEN (vuid->if_name) - 1);
3089 _vec_len (s) = 0;
3090 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003091
3092 vec_free (s);
3093 vec_free (hw_if_indices);
3094
3095 *out_vuids = r_vuids;
3096
3097 return rv;
3098}
3099
3100clib_error_t *
3101show_vhost_user_command_fn (vlib_main_t * vm,
Damjan Marion00a9dca2016-08-17 17:05:46 +02003102 unformat_input_t * input,
3103 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003104{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003105 clib_error_t *error = 0;
3106 vnet_main_t *vnm = vnet_get_main ();
3107 vhost_user_main_t *vum = &vhost_user_main;
3108 vhost_user_intf_t *vui;
3109 u32 hw_if_index, *hw_if_indices = 0;
3110 vnet_hw_interface_t *hi;
Stevenf3b53642017-05-01 14:03:02 -07003111 u16 *queue;
Pierre Pfistere21c5282016-09-21 08:04:59 +01003112 u32 ci;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003113 int i, j, q;
3114 int show_descr = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003115 struct feat_struct
3116 {
3117 u8 bit;
3118 char *str;
3119 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07003120 struct feat_struct *feat_entry;
3121
3122 static struct feat_struct feat_array[] = {
3123#define _(s,b) { .str = #s, .bit = b, },
Damjan Marion00a9dca2016-08-17 17:05:46 +02003124 foreach_virtio_net_feature
Ed Warnickecb9cada2015-12-08 15:45:58 -07003125#undef _
Damjan Marion00a9dca2016-08-17 17:05:46 +02003126 {.str = NULL}
Ed Warnickecb9cada2015-12-08 15:45:58 -07003127 };
3128
Pierre Pfistere21c5282016-09-21 08:04:59 +01003129#define foreach_protocol_feature \
3130 _(VHOST_USER_PROTOCOL_F_MQ) \
3131 _(VHOST_USER_PROTOCOL_F_LOG_SHMFD)
3132
3133 static struct feat_struct proto_feat_array[] = {
3134#define _(s) { .str = #s, .bit = s},
3135 foreach_protocol_feature
3136#undef _
3137 {.str = NULL}
3138 };
3139
Damjan Marion00a9dca2016-08-17 17:05:46 +02003140 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3141 {
3142 if (unformat
3143 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
3144 {
3145 vec_add1 (hw_if_indices, hw_if_index);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003146 }
3147 else if (unformat (input, "descriptors") || unformat (input, "desc"))
3148 show_descr = 1;
3149 else
3150 {
3151 error = clib_error_return (0, "unknown input `%U'",
3152 format_unformat_error, input);
3153 goto done;
3154 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003155 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003156 if (vec_len (hw_if_indices) == 0)
3157 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003158 pool_foreach (vui, vum->vhost_user_interfaces,
3159 vec_add1 (hw_if_indices, vui->hw_if_index);
3160 );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003161 }
Damjan Marion00a9dca2016-08-17 17:05:46 +02003162 vlib_cli_output (vm, "Virtio vhost-user interfaces");
Pierre Pfistere21c5282016-09-21 08:04:59 +01003163 vlib_cli_output (vm, "Global:\n coalesce frames %d time %e",
Damjan Marion00a9dca2016-08-17 17:05:46 +02003164 vum->coalesce_frames, vum->coalesce_time);
Stevenf3b53642017-05-01 14:03:02 -07003165 vlib_cli_output (vm, " number of rx virtqueues in interrupt mode: %d",
3166 vum->ifq_count);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003167
3168 for (i = 0; i < vec_len (hw_if_indices); i++)
3169 {
3170 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003171 vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003172 vlib_cli_output (vm, "Interface: %s (ifindex %d)",
3173 hi->name, hw_if_indices[i]);
3174
Pierre Pfistere21c5282016-09-21 08:04:59 +01003175 vlib_cli_output (vm, "virtio_net_hdr_sz %d\n"
3176 " features mask (0x%llx): \n"
3177 " features (0x%llx): \n",
3178 vui->virtio_net_hdr_sz, vui->feature_mask,
3179 vui->features);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003180
3181 feat_entry = (struct feat_struct *) &feat_array;
3182 while (feat_entry->str)
3183 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01003184 if (vui->features & (1ULL << feat_entry->bit))
3185 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3186 feat_entry->bit);
3187 feat_entry++;
3188 }
3189
3190 vlib_cli_output (vm, " protocol features (0x%llx)",
3191 vui->protocol_features);
3192 feat_entry = (struct feat_struct *) &proto_feat_array;
3193 while (feat_entry->str)
3194 {
3195 if (vui->protocol_features & (1ULL << feat_entry->bit))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003196 vlib_cli_output (vm, " %s (%d)", feat_entry->str,
3197 feat_entry->bit);
3198 feat_entry++;
3199 }
3200
3201 vlib_cli_output (vm, "\n");
3202
Damjan Marion00a9dca2016-08-17 17:05:46 +02003203 vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n",
3204 vui->sock_filename,
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003205 (vui->unix_server_index != ~0) ? "server" : "client",
Damjan Marion00a9dca2016-08-17 17:05:46 +02003206 strerror (vui->sock_errno));
3207
Pierre Pfistere21c5282016-09-21 08:04:59 +01003208 vlib_cli_output (vm, " rx placement: ");
Stevenf3b53642017-05-01 14:03:02 -07003209
3210 vec_foreach (queue, vui->rx_queues)
Pierre Pfistere21c5282016-09-21 08:04:59 +01003211 {
Stevenf3b53642017-05-01 14:03:02 -07003212 vnet_main_t *vnm = vnet_get_main ();
3213 uword thread_index;
3214 vnet_hw_interface_rx_mode mode;
3215
3216 thread_index = vnet_get_device_input_thread_index (vnm,
3217 vui->hw_if_index,
3218 *queue);
3219 vnet_hw_interface_get_rx_mode (vnm, vui->hw_if_index, *queue, &mode);
3220 vlib_cli_output (vm, " thread %d on vring %d, %U\n",
3221 thread_index, VHOST_VRING_IDX_TX (*queue),
3222 format_vnet_hw_interface_rx_mode, mode);
Pierre Pfistere21c5282016-09-21 08:04:59 +01003223 }
3224
3225 vlib_cli_output (vm, " tx placement: %s\n",
3226 vui->use_tx_spinlock ? "spin-lock" : "lock-free");
3227
3228 vec_foreach_index (ci, vui->per_cpu_tx_qid)
3229 {
3230 vlib_cli_output (vm, " thread %d on vring %d\n", ci,
3231 VHOST_VRING_IDX_RX (vui->per_cpu_tx_qid[ci]));
3232 }
3233
3234 vlib_cli_output (vm, "\n");
3235
Damjan Marion00a9dca2016-08-17 17:05:46 +02003236 vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions);
3237
3238 if (vui->nregions)
3239 {
3240 vlib_cli_output (vm,
3241 " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n");
3242 vlib_cli_output (vm,
3243 " ====== ===== ================== ================== ================== ================== ==================\n");
3244 }
3245 for (j = 0; j < vui->nregions; j++)
3246 {
3247 vlib_cli_output (vm,
3248 " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
3249 j, vui->region_mmap_fd[j],
3250 vui->regions[j].guest_phys_addr,
3251 vui->regions[j].memory_size,
3252 vui->regions[j].userspace_addr,
3253 vui->regions[j].mmap_offset,
3254 pointer_to_uword (vui->region_mmap_addr[j]));
3255 }
Pierre Pfistere21c5282016-09-21 08:04:59 +01003256 for (q = 0; q < VHOST_VRING_MAX_N; q++)
Damjan Marion00a9dca2016-08-17 17:05:46 +02003257 {
Pierre Pfistere21c5282016-09-21 08:04:59 +01003258 if (!vui->vrings[q].started)
3259 continue;
3260
3261 vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q,
3262 (q & 1) ? "RX" : "TX",
3263 vui->vrings[q].enabled ? "" : " disabled");
Damjan Marion00a9dca2016-08-17 17:05:46 +02003264
3265 vlib_cli_output (vm,
3266 " qsz %d last_avail_idx %d last_used_idx %d\n",
3267 vui->vrings[q].qsz, vui->vrings[q].last_avail_idx,
3268 vui->vrings[q].last_used_idx);
3269
3270 if (vui->vrings[q].avail && vui->vrings[q].used)
3271 vlib_cli_output (vm,
3272 " avail.flags %x avail.idx %d used.flags %x used.idx %d\n",
3273 vui->vrings[q].avail->flags,
3274 vui->vrings[q].avail->idx,
3275 vui->vrings[q].used->flags,
3276 vui->vrings[q].used->idx);
3277
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003278 int kickfd = UNIX_GET_FD (vui->vrings[q].kickfd_idx);
3279 int callfd = UNIX_GET_FD (vui->vrings[q].callfd_idx);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003280 vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n",
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003281 kickfd, callfd, vui->vrings[q].errfd);
Damjan Marion00a9dca2016-08-17 17:05:46 +02003282
3283 if (show_descr)
3284 {
3285 vlib_cli_output (vm, "\n descriptor table:\n");
3286 vlib_cli_output (vm,
3287 " id addr len flags next user_addr\n");
3288 vlib_cli_output (vm,
3289 " ===== ================== ===== ====== ===== ==================\n");
3290 for (j = 0; j < vui->vrings[q].qsz; j++)
3291 {
Pierre Pfister11f92052016-09-21 08:08:55 +01003292 u32 mem_hint = 0;
Damjan Marion00a9dca2016-08-17 17:05:46 +02003293 vlib_cli_output (vm,
3294 " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
3295 j, vui->vrings[q].desc[j].addr,
3296 vui->vrings[q].desc[j].len,
3297 vui->vrings[q].desc[j].flags,
3298 vui->vrings[q].desc[j].next,
3299 pointer_to_uword (map_guest_mem
3300 (vui,
Pierre Pfisterba1d0462016-07-27 16:38:20 +01003301 vui->vrings[q].desc[j].
Pierre Pfister11f92052016-09-21 08:08:55 +01003302 addr, &mem_hint)));
Damjan Marion00a9dca2016-08-17 17:05:46 +02003303 }
3304 }
3305 }
3306 vlib_cli_output (vm, "\n");
3307 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003308done:
3309 vec_free (hw_if_indices);
3310 return error;
3311}
3312
Damjan Marion8d281b32016-08-24 14:32:39 +02003313/*
3314 * CLI functions
3315 */
3316
Billy McFalla92501a2016-11-23 12:45:29 -05003317/*?
3318 * Create a vHost User interface. Once created, a new virtual interface
3319 * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>'
3320 * is the next free index.
3321 *
3322 * There are several parameters associated with a vHost interface:
3323 *
3324 * - <b>socket <socket-filename></b> - Name of the linux socket used by QEMU/VM and
3325 * VPP to manage the vHost interface. If socket does not already exist, VPP will
3326 * create the socket.
3327 *
3328 * - <b>server</b> - Optional flag to indicate that VPP should be the server for the
3329 * linux socket. If not provided, VPP will be the client.
3330 *
3331 * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at
3332 * startup. By default, all supported features will be advertised. Otherwise,
3333 * provide the set of features desired.
3334 * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF
3335 * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ
3336 * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE
3337 * - 0x000400000 (22) - VIRTIO_NET_F_MQ
3338 * - 0x004000000 (26) - VHOST_F_LOG_ALL
3339 * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT
3340 * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC
3341 * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES
3342 * - 0x100000000 (32) - VIRTIO_F_VERSION_1
3343 *
3344 * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either
3345 * X:X:X:X:X:X unix or X.X.X cisco format.
3346 *
3347 * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance
3348 * in the name to be specified. If instance already exists, name will be used
3349 * anyway and multiple instances will have the same name. Use with caution.
3350 *
Steven7312cc72017-03-15 21:18:55 -07003351 * - <b>mode [interrupt | polling]</b> - Optional parameter specifying
3352 * the input thread polling policy.
3353 *
Billy McFalla92501a2016-11-23 12:45:29 -05003354 * @cliexpar
3355 * Example of how to create a vhost interface with VPP as the client and all features enabled:
3356 * @cliexstart{create vhost-user socket /tmp/vhost1.sock}
3357 * VirtualEthernet0/0/0
3358 * @cliexend
3359 * Example of how to create a vhost interface with VPP as the server and with just
3360 * multiple queues enabled:
3361 * @cliexstart{create vhost-user socket /tmp/vhost2.sock server feature-mask 0x40400000}
3362 * VirtualEthernet0/0/1
3363 * @cliexend
3364 * Once the vHost interface is created, enable the interface using:
3365 * @cliexcmd{set interface state VirtualEthernet0/0/0 up}
3366?*/
Damjan Marion8d281b32016-08-24 14:32:39 +02003367/* *INDENT-OFF* */
3368VLIB_CLI_COMMAND (vhost_user_connect_command, static) = {
3369 .path = "create vhost-user",
Steven7312cc72017-03-15 21:18:55 -07003370 .short_help = "create vhost-user socket <socket-filename> [server] "
Stevenf3b53642017-05-01 14:03:02 -07003371 "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] ",
Damjan Marion8d281b32016-08-24 14:32:39 +02003372 .function = vhost_user_connect_command_fn,
3373};
Billy McFalla92501a2016-11-23 12:45:29 -05003374/* *INDENT-ON* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003375
Billy McFalla92501a2016-11-23 12:45:29 -05003376/*?
3377 * Delete a vHost User interface using the interface name or the
Dave Barach13ad1f02017-03-26 19:36:18 -04003378 * software interface index. Use the '<em>show interface</em>'
Billy McFalla92501a2016-11-23 12:45:29 -05003379 * command to determine the software interface index. On deletion,
3380 * the linux socket will not be deleted.
3381 *
3382 * @cliexpar
3383 * Example of how to delete a vhost interface by name:
3384 * @cliexcmd{delete vhost-user VirtualEthernet0/0/1}
3385 * Example of how to delete a vhost interface by software interface index:
3386 * @cliexcmd{delete vhost-user sw_if_index 1}
3387?*/
3388/* *INDENT-OFF* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003389VLIB_CLI_COMMAND (vhost_user_delete_command, static) = {
3390 .path = "delete vhost-user",
Billy McFalla92501a2016-11-23 12:45:29 -05003391 .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}",
Damjan Marion8d281b32016-08-24 14:32:39 +02003392 .function = vhost_user_delete_command_fn,
3393};
3394
Billy McFalla92501a2016-11-23 12:45:29 -05003395/*?
3396 * Display the attributes of a single vHost User interface (provide interface
3397 * name), multiple vHost User interfaces (provide a list of interface names seperated
3398 * by spaces) or all Vhost User interfaces (omit an interface name to display all
3399 * vHost interfaces).
3400 *
3401 * @cliexpar
3402 * @parblock
3403 * Example of how to display a vhost interface:
3404 * @cliexstart{show vhost-user VirtualEthernet0/0/0}
3405 * Virtio vhost-user interfaces
3406 * Global:
3407 * coalesce frames 32 time 1e-3
3408 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3409 * virtio_net_hdr_sz 12
3410 * features mask (0xffffffffffffffff):
3411 * features (0x50408000):
3412 * VIRTIO_NET_F_MRG_RXBUF (15)
3413 * VIRTIO_NET_F_MQ (22)
3414 * VIRTIO_F_INDIRECT_DESC (28)
3415 * VHOST_USER_F_PROTOCOL_FEATURES (30)
3416 * protocol features (0x3)
3417 * VHOST_USER_PROTOCOL_F_MQ (0)
3418 * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1)
3419 *
3420 * socket filename /tmp/vhost1.sock type client errno "Success"
3421 *
3422 * rx placement:
3423 * thread 1 on vring 1
3424 * thread 1 on vring 5
3425 * thread 2 on vring 3
3426 * thread 2 on vring 7
3427 * tx placement: spin-lock
3428 * thread 0 on vring 0
3429 * thread 1 on vring 2
3430 * thread 2 on vring 0
3431 *
3432 * Memory regions (total 2)
3433 * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr
3434 * ====== ===== ================== ================== ================== ================== ==================
3435 * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000
3436 * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000
3437 *
3438 * Virtqueue 0 (TX)
3439 * qsz 256 last_avail_idx 0 last_used_idx 0
3440 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3441 * kickfd 62 callfd 64 errfd -1
3442 *
3443 * Virtqueue 1 (RX)
3444 * qsz 256 last_avail_idx 0 last_used_idx 0
3445 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3446 * kickfd 65 callfd 66 errfd -1
3447 *
3448 * Virtqueue 2 (TX)
3449 * qsz 256 last_avail_idx 0 last_used_idx 0
3450 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3451 * kickfd 63 callfd 70 errfd -1
3452 *
3453 * Virtqueue 3 (RX)
3454 * qsz 256 last_avail_idx 0 last_used_idx 0
3455 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3456 * kickfd 72 callfd 74 errfd -1
3457 *
3458 * Virtqueue 4 (TX disabled)
3459 * qsz 256 last_avail_idx 0 last_used_idx 0
3460 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3461 * kickfd 76 callfd 78 errfd -1
3462 *
3463 * Virtqueue 5 (RX disabled)
3464 * qsz 256 last_avail_idx 0 last_used_idx 0
3465 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3466 * kickfd 80 callfd 82 errfd -1
3467 *
3468 * Virtqueue 6 (TX disabled)
3469 * qsz 256 last_avail_idx 0 last_used_idx 0
3470 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3471 * kickfd 84 callfd 86 errfd -1
3472 *
3473 * Virtqueue 7 (RX disabled)
3474 * qsz 256 last_avail_idx 0 last_used_idx 0
3475 * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0
3476 * kickfd 88 callfd 90 errfd -1
3477 *
3478 * @cliexend
3479 *
3480 * The optional '<em>descriptors</em>' parameter will display the same output as
3481 * the previous example but will include the descriptor table for each queue.
3482 * The output is truncated below:
3483 * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors}
3484 * Virtio vhost-user interfaces
3485 * Global:
3486 * coalesce frames 32 time 1e-3
3487 * Interface: VirtualEthernet0/0/0 (ifindex 1)
3488 * virtio_net_hdr_sz 12
3489 * features mask (0xffffffffffffffff):
3490 * features (0x50408000):
3491 * VIRTIO_NET_F_MRG_RXBUF (15)
3492 * VIRTIO_NET_F_MQ (22)
3493 * :
3494 * Virtqueue 0 (TX)
3495 * qsz 256 last_avail_idx 0 last_used_idx 0
3496 * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0
3497 * kickfd 62 callfd 64 errfd -1
3498 *
3499 * descriptor table:
3500 * id addr len flags next user_addr
3501 * ===== ================== ===== ====== ===== ==================
3502 * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974
3503 * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034
3504 * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4
3505 * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4
3506 * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474
3507 * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34
3508 * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4
3509 * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4
3510 * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74
3511 * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634
3512 * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4
3513 * :
3514 * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000
3515 * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000
3516 * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000
3517 * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000
3518 * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000
3519 * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000
3520 * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000
3521 *
3522 * Virtqueue 1 (RX)
3523 * qsz 256 last_avail_idx 0 last_used_idx 0
3524 * :
3525 * @cliexend
3526 * @endparblock
3527?*/
3528/* *INDENT-OFF* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003529VLIB_CLI_COMMAND (show_vhost_user_command, static) = {
3530 .path = "show vhost-user",
Billy McFalla92501a2016-11-23 12:45:29 -05003531 .short_help = "show vhost-user [<interface> [<interface> [..]]] [descriptors]",
Damjan Marion8d281b32016-08-24 14:32:39 +02003532 .function = show_vhost_user_command_fn,
3533};
3534/* *INDENT-ON* */
Damjan Marion8d281b32016-08-24 14:32:39 +02003535
Ed Warnickecb9cada2015-12-08 15:45:58 -07003536static clib_error_t *
3537vhost_user_config (vlib_main_t * vm, unformat_input_t * input)
3538{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003539 vhost_user_main_t *vum = &vhost_user_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003540
3541 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3542 {
3543 if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003544 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003545 else if (unformat (input, "coalesce-time %f", &vum->coalesce_time))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003546 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003547 else if (unformat (input, "dont-dump-memory"))
Damjan Marion00a9dca2016-08-17 17:05:46 +02003548 vum->dont_dump_vhost_user_memory = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003549 else
Damjan Marion00a9dca2016-08-17 17:05:46 +02003550 return clib_error_return (0, "unknown input `%U'",
3551 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003552 }
3553
3554 return 0;
3555}
3556
3557/* vhost-user { ... } configuration. */
3558VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user");
3559
3560void
3561vhost_user_unmap_all (void)
3562{
Damjan Marion00a9dca2016-08-17 17:05:46 +02003563 vhost_user_main_t *vum = &vhost_user_main;
3564 vhost_user_intf_t *vui;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003565
3566 if (vum->dont_dump_vhost_user_memory)
3567 {
Pierre Pfisterdbb3c252016-11-22 10:33:34 +00003568 pool_foreach (vui, vum->vhost_user_interfaces,
3569 unmap_all_mem_regions (vui);
3570 );
Ed Warnickecb9cada2015-12-08 15:45:58 -07003571 }
3572}
Damjan Marion00a9dca2016-08-17 17:05:46 +02003573
3574/*
3575 * fd.io coding-style-patch-verification: ON
3576 *
3577 * Local Variables:
3578 * eval: (c-set-style "gnu")
3579 * End:
3580 */