monitor: only run coroutine commands in qemu_aio_context
[qemu/kevin.git] / net / vhost-vdpa.c
blob3726ee5d67634b70ae9cc5ff2424494c521fec70
1 /*
2 * vhost-vdpa.c
4 * Copyright(c) 2017-2018 Intel Corporation.
5 * Copyright(c) 2020 Red Hat, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "clients.h"
14 #include "hw/virtio/virtio-net.h"
15 #include "net/vhost_net.h"
16 #include "net/vhost-vdpa.h"
17 #include "hw/virtio/vhost-vdpa.h"
18 #include "qemu/config-file.h"
19 #include "qemu/error-report.h"
20 #include "qemu/log.h"
21 #include "qemu/memalign.h"
22 #include "qemu/option.h"
23 #include "qapi/error.h"
24 #include <linux/vhost.h>
25 #include <sys/ioctl.h>
26 #include <err.h>
27 #include "standard-headers/linux/virtio_net.h"
28 #include "monitor/monitor.h"
29 #include "migration/migration.h"
30 #include "migration/misc.h"
31 #include "hw/virtio/vhost.h"
33 /* Todo:need to add the multiqueue support here */
34 typedef struct VhostVDPAState {
35 NetClientState nc;
36 struct vhost_vdpa vhost_vdpa;
37 Notifier migration_state;
38 VHostNetState *vhost_net;
40 /* Control commands shadow buffers */
41 void *cvq_cmd_out_buffer;
42 virtio_net_ctrl_ack *status;
44 /* The device always have SVQ enabled */
45 bool always_svq;
47 /* The device can isolate CVQ in its own ASID */
48 bool cvq_isolated;
50 bool started;
51 } VhostVDPAState;
54 * The array is sorted alphabetically in ascending order,
55 * with the exception of VHOST_INVALID_FEATURE_BIT,
56 * which should always be the last entry.
58 const int vdpa_feature_bits[] = {
59 VIRTIO_F_ANY_LAYOUT,
60 VIRTIO_F_IOMMU_PLATFORM,
61 VIRTIO_F_NOTIFY_ON_EMPTY,
62 VIRTIO_F_RING_PACKED,
63 VIRTIO_F_RING_RESET,
64 VIRTIO_F_VERSION_1,
65 VIRTIO_NET_F_CSUM,
66 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS,
67 VIRTIO_NET_F_CTRL_MAC_ADDR,
68 VIRTIO_NET_F_CTRL_RX,
69 VIRTIO_NET_F_CTRL_RX_EXTRA,
70 VIRTIO_NET_F_CTRL_VLAN,
71 VIRTIO_NET_F_CTRL_VQ,
72 VIRTIO_NET_F_GSO,
73 VIRTIO_NET_F_GUEST_CSUM,
74 VIRTIO_NET_F_GUEST_ECN,
75 VIRTIO_NET_F_GUEST_TSO4,
76 VIRTIO_NET_F_GUEST_TSO6,
77 VIRTIO_NET_F_GUEST_UFO,
78 VIRTIO_NET_F_GUEST_USO4,
79 VIRTIO_NET_F_GUEST_USO6,
80 VIRTIO_NET_F_HASH_REPORT,
81 VIRTIO_NET_F_HOST_ECN,
82 VIRTIO_NET_F_HOST_TSO4,
83 VIRTIO_NET_F_HOST_TSO6,
84 VIRTIO_NET_F_HOST_UFO,
85 VIRTIO_NET_F_HOST_USO,
86 VIRTIO_NET_F_MQ,
87 VIRTIO_NET_F_MRG_RXBUF,
88 VIRTIO_NET_F_MTU,
89 VIRTIO_NET_F_RSS,
90 VIRTIO_NET_F_STATUS,
91 VIRTIO_RING_F_EVENT_IDX,
92 VIRTIO_RING_F_INDIRECT_DESC,
94 /* VHOST_INVALID_FEATURE_BIT should always be the last entry */
95 VHOST_INVALID_FEATURE_BIT
98 /** Supported device specific feature bits with SVQ */
99 static const uint64_t vdpa_svq_device_features =
100 BIT_ULL(VIRTIO_NET_F_CSUM) |
101 BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) |
102 BIT_ULL(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
103 BIT_ULL(VIRTIO_NET_F_MTU) |
104 BIT_ULL(VIRTIO_NET_F_MAC) |
105 BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) |
106 BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) |
107 BIT_ULL(VIRTIO_NET_F_GUEST_ECN) |
108 BIT_ULL(VIRTIO_NET_F_GUEST_UFO) |
109 BIT_ULL(VIRTIO_NET_F_HOST_TSO4) |
110 BIT_ULL(VIRTIO_NET_F_HOST_TSO6) |
111 BIT_ULL(VIRTIO_NET_F_HOST_ECN) |
112 BIT_ULL(VIRTIO_NET_F_HOST_UFO) |
113 BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) |
114 BIT_ULL(VIRTIO_NET_F_STATUS) |
115 BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
116 BIT_ULL(VIRTIO_NET_F_CTRL_RX) |
117 BIT_ULL(VIRTIO_NET_F_CTRL_VLAN) |
118 BIT_ULL(VIRTIO_NET_F_CTRL_RX_EXTRA) |
119 BIT_ULL(VIRTIO_NET_F_MQ) |
120 BIT_ULL(VIRTIO_F_ANY_LAYOUT) |
121 BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) |
122 /* VHOST_F_LOG_ALL is exposed by SVQ */
123 BIT_ULL(VHOST_F_LOG_ALL) |
124 BIT_ULL(VIRTIO_NET_F_HASH_REPORT) |
125 BIT_ULL(VIRTIO_NET_F_RSS) |
126 BIT_ULL(VIRTIO_NET_F_RSC_EXT) |
127 BIT_ULL(VIRTIO_NET_F_STANDBY) |
128 BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX);
130 #define VHOST_VDPA_NET_CVQ_ASID 1
132 VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
134 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
135 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
136 return s->vhost_net;
139 static size_t vhost_vdpa_net_cvq_cmd_len(void)
142 * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
143 * In buffer is always 1 byte, so it should fit here
145 return sizeof(struct virtio_net_ctrl_hdr) +
146 2 * sizeof(struct virtio_net_ctrl_mac) +
147 MAC_TABLE_ENTRIES * ETH_ALEN;
150 static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
152 return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
155 static bool vhost_vdpa_net_valid_svq_features(uint64_t features, Error **errp)
157 uint64_t invalid_dev_features =
158 features & ~vdpa_svq_device_features &
159 /* Transport are all accepted at this point */
160 ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START,
161 VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START);
163 if (invalid_dev_features) {
164 error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
165 invalid_dev_features);
166 return false;
169 return vhost_svq_valid_features(features, errp);
172 static int vhost_vdpa_net_check_device_id(struct vhost_net *net)
174 uint32_t device_id;
175 int ret;
176 struct vhost_dev *hdev;
178 hdev = (struct vhost_dev *)&net->dev;
179 ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id);
180 if (device_id != VIRTIO_ID_NET) {
181 return -ENOTSUP;
183 return ret;
186 static int vhost_vdpa_add(NetClientState *ncs, void *be,
187 int queue_pair_index, int nvqs)
189 VhostNetOptions options;
190 struct vhost_net *net = NULL;
191 VhostVDPAState *s;
192 int ret;
194 options.backend_type = VHOST_BACKEND_TYPE_VDPA;
195 assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
196 s = DO_UPCAST(VhostVDPAState, nc, ncs);
197 options.net_backend = ncs;
198 options.opaque = be;
199 options.busyloop_timeout = 0;
200 options.nvqs = nvqs;
202 net = vhost_net_init(&options);
203 if (!net) {
204 error_report("failed to init vhost_net for queue");
205 goto err_init;
207 s->vhost_net = net;
208 ret = vhost_vdpa_net_check_device_id(net);
209 if (ret) {
210 goto err_check;
212 return 0;
213 err_check:
214 vhost_net_cleanup(net);
215 g_free(net);
216 err_init:
217 return -1;
220 static void vhost_vdpa_cleanup(NetClientState *nc)
222 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
225 * If a peer NIC is attached, do not cleanup anything.
226 * Cleanup will happen as a part of qemu_cleanup() -> net_cleanup()
227 * when the guest is shutting down.
229 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
230 return;
232 munmap(s->cvq_cmd_out_buffer, vhost_vdpa_net_cvq_cmd_page_len());
233 munmap(s->status, vhost_vdpa_net_cvq_cmd_page_len());
234 if (s->vhost_net) {
235 vhost_net_cleanup(s->vhost_net);
236 g_free(s->vhost_net);
237 s->vhost_net = NULL;
239 if (s->vhost_vdpa.index != 0) {
240 return;
242 qemu_close(s->vhost_vdpa.shared->device_fd);
243 g_free(s->vhost_vdpa.shared);
246 /** Dummy SetSteeringEBPF to support RSS for vhost-vdpa backend */
247 static bool vhost_vdpa_set_steering_ebpf(NetClientState *nc, int prog_fd)
249 return true;
252 static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
254 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
256 return true;
259 static bool vhost_vdpa_has_ufo(NetClientState *nc)
261 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
262 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
263 uint64_t features = 0;
264 features |= (1ULL << VIRTIO_NET_F_HOST_UFO);
265 features = vhost_net_get_features(s->vhost_net, features);
266 return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO));
270 static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc,
271 Error **errp)
273 const char *driver = object_class_get_name(oc);
275 if (!g_str_has_prefix(driver, "virtio-net-")) {
276 error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*");
277 return false;
280 return true;
283 /** Dummy receive in case qemu falls back to userland tap networking */
284 static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf,
285 size_t size)
287 return size;
290 static void vhost_vdpa_net_log_global_enable(VhostVDPAState *s, bool enable)
292 struct vhost_vdpa *v = &s->vhost_vdpa;
293 VirtIONet *n;
294 VirtIODevice *vdev;
295 int data_queue_pairs, cvq, r;
297 /* We are only called on the first data vqs and only if x-svq is not set */
298 if (s->vhost_vdpa.shadow_vqs_enabled == enable) {
299 return;
302 vdev = v->dev->vdev;
303 n = VIRTIO_NET(vdev);
304 if (!n->vhost_started) {
305 return;
308 data_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
309 cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ?
310 n->max_ncs - n->max_queue_pairs : 0;
312 * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
313 * in the future and resume the device if read-only operations between
314 * suspend and reset goes wrong.
316 vhost_net_stop(vdev, n->nic->ncs, data_queue_pairs, cvq);
318 /* Start will check migration setup_or_active to configure or not SVQ */
319 r = vhost_net_start(vdev, n->nic->ncs, data_queue_pairs, cvq);
320 if (unlikely(r < 0)) {
321 error_report("unable to start vhost net: %s(%d)", g_strerror(-r), -r);
325 static void vdpa_net_migration_state_notifier(Notifier *notifier, void *data)
327 MigrationState *migration = data;
328 VhostVDPAState *s = container_of(notifier, VhostVDPAState,
329 migration_state);
331 if (migration_in_setup(migration)) {
332 vhost_vdpa_net_log_global_enable(s, true);
333 } else if (migration_has_failed(migration)) {
334 vhost_vdpa_net_log_global_enable(s, false);
338 static void vhost_vdpa_net_data_start_first(VhostVDPAState *s)
340 struct vhost_vdpa *v = &s->vhost_vdpa;
342 migration_add_notifier(&s->migration_state,
343 vdpa_net_migration_state_notifier);
344 if (v->shadow_vqs_enabled) {
345 v->shared->iova_tree = vhost_iova_tree_new(v->shared->iova_range.first,
346 v->shared->iova_range.last);
350 static int vhost_vdpa_net_data_start(NetClientState *nc)
352 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
353 struct vhost_vdpa *v = &s->vhost_vdpa;
355 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
357 if (s->always_svq ||
358 migration_is_setup_or_active(migrate_get_current()->state)) {
359 v->shadow_vqs_enabled = true;
360 } else {
361 v->shadow_vqs_enabled = false;
364 if (v->index == 0) {
365 v->shared->shadow_data = v->shadow_vqs_enabled;
366 vhost_vdpa_net_data_start_first(s);
367 return 0;
370 return 0;
373 static int vhost_vdpa_net_data_load(NetClientState *nc)
375 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
376 struct vhost_vdpa *v = &s->vhost_vdpa;
377 bool has_cvq = v->dev->vq_index_end % 2;
379 if (has_cvq) {
380 return 0;
383 for (int i = 0; i < v->dev->nvqs; ++i) {
384 vhost_vdpa_set_vring_ready(v, i + v->dev->vq_index);
386 return 0;
389 static void vhost_vdpa_net_client_stop(NetClientState *nc)
391 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
392 struct vhost_dev *dev;
394 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
396 if (s->vhost_vdpa.index == 0) {
397 migration_remove_notifier(&s->migration_state);
400 dev = s->vhost_vdpa.dev;
401 if (dev->vq_index + dev->nvqs == dev->vq_index_end) {
402 g_clear_pointer(&s->vhost_vdpa.shared->iova_tree,
403 vhost_iova_tree_delete);
407 static NetClientInfo net_vhost_vdpa_info = {
408 .type = NET_CLIENT_DRIVER_VHOST_VDPA,
409 .size = sizeof(VhostVDPAState),
410 .receive = vhost_vdpa_receive,
411 .start = vhost_vdpa_net_data_start,
412 .load = vhost_vdpa_net_data_load,
413 .stop = vhost_vdpa_net_client_stop,
414 .cleanup = vhost_vdpa_cleanup,
415 .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
416 .has_ufo = vhost_vdpa_has_ufo,
417 .check_peer_type = vhost_vdpa_check_peer_type,
418 .set_steering_ebpf = vhost_vdpa_set_steering_ebpf,
421 static int64_t vhost_vdpa_get_vring_group(int device_fd, unsigned vq_index,
422 Error **errp)
424 struct vhost_vring_state state = {
425 .index = vq_index,
427 int r = ioctl(device_fd, VHOST_VDPA_GET_VRING_GROUP, &state);
429 if (unlikely(r < 0)) {
430 r = -errno;
431 error_setg_errno(errp, errno, "Cannot get VQ %u group", vq_index);
432 return r;
435 return state.num;
438 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa *v,
439 unsigned vq_group,
440 unsigned asid_num)
442 struct vhost_vring_state asid = {
443 .index = vq_group,
444 .num = asid_num,
446 int r;
448 r = ioctl(v->shared->device_fd, VHOST_VDPA_SET_GROUP_ASID, &asid);
449 if (unlikely(r < 0)) {
450 error_report("Can't set vq group %u asid %u, errno=%d (%s)",
451 asid.index, asid.num, errno, g_strerror(errno));
453 return r;
456 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)
458 VhostIOVATree *tree = v->shared->iova_tree;
459 DMAMap needle = {
461 * No need to specify size or to look for more translations since
462 * this contiguous chunk was allocated by us.
464 .translated_addr = (hwaddr)(uintptr_t)addr,
466 const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle);
467 int r;
469 if (unlikely(!map)) {
470 error_report("Cannot locate expected map");
471 return;
474 r = vhost_vdpa_dma_unmap(v->shared, v->address_space_id, map->iova,
475 map->size + 1);
476 if (unlikely(r != 0)) {
477 error_report("Device cannot unmap: %s(%d)", g_strerror(r), r);
480 vhost_iova_tree_remove(tree, *map);
483 /** Map CVQ buffer. */
484 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,
485 bool write)
487 DMAMap map = {};
488 int r;
490 map.translated_addr = (hwaddr)(uintptr_t)buf;
491 map.size = size - 1;
492 map.perm = write ? IOMMU_RW : IOMMU_RO,
493 r = vhost_iova_tree_map_alloc(v->shared->iova_tree, &map);
494 if (unlikely(r != IOVA_OK)) {
495 error_report("Cannot map injected element");
496 return r;
499 r = vhost_vdpa_dma_map(v->shared, v->address_space_id, map.iova,
500 vhost_vdpa_net_cvq_cmd_page_len(), buf, !write);
501 if (unlikely(r < 0)) {
502 goto dma_map_err;
505 return 0;
507 dma_map_err:
508 vhost_iova_tree_remove(v->shared->iova_tree, map);
509 return r;
512 static int vhost_vdpa_net_cvq_start(NetClientState *nc)
514 VhostVDPAState *s;
515 struct vhost_vdpa *v;
516 int64_t cvq_group;
517 int r;
518 Error *err = NULL;
520 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
522 s = DO_UPCAST(VhostVDPAState, nc, nc);
523 v = &s->vhost_vdpa;
525 v->shadow_vqs_enabled = v->shared->shadow_data;
526 s->vhost_vdpa.address_space_id = VHOST_VDPA_GUEST_PA_ASID;
528 if (v->shared->shadow_data) {
529 /* SVQ is already configured for all virtqueues */
530 goto out;
534 * If we early return in these cases SVQ will not be enabled. The migration
535 * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
537 if (!vhost_vdpa_net_valid_svq_features(v->dev->features, NULL)) {
538 return 0;
541 if (!s->cvq_isolated) {
542 return 0;
545 cvq_group = vhost_vdpa_get_vring_group(v->shared->device_fd,
546 v->dev->vq_index_end - 1,
547 &err);
548 if (unlikely(cvq_group < 0)) {
549 error_report_err(err);
550 return cvq_group;
553 r = vhost_vdpa_set_address_space_id(v, cvq_group, VHOST_VDPA_NET_CVQ_ASID);
554 if (unlikely(r < 0)) {
555 return r;
558 v->shadow_vqs_enabled = true;
559 s->vhost_vdpa.address_space_id = VHOST_VDPA_NET_CVQ_ASID;
561 out:
562 if (!s->vhost_vdpa.shadow_vqs_enabled) {
563 return 0;
567 * If other vhost_vdpa already have an iova_tree, reuse it for simplicity,
568 * whether CVQ shares ASID with guest or not, because:
569 * - Memory listener need access to guest's memory addresses allocated in
570 * the IOVA tree.
571 * - There should be plenty of IOVA address space for both ASID not to
572 * worry about collisions between them. Guest's translations are still
573 * validated with virtio virtqueue_pop so there is no risk for the guest
574 * to access memory that it shouldn't.
576 * To allocate a iova tree per ASID is doable but it complicates the code
577 * and it is not worth it for the moment.
579 if (!v->shared->iova_tree) {
580 v->shared->iova_tree = vhost_iova_tree_new(v->shared->iova_range.first,
581 v->shared->iova_range.last);
584 r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer,
585 vhost_vdpa_net_cvq_cmd_page_len(), false);
586 if (unlikely(r < 0)) {
587 return r;
590 r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->status,
591 vhost_vdpa_net_cvq_cmd_page_len(), true);
592 if (unlikely(r < 0)) {
593 vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
596 return r;
599 static void vhost_vdpa_net_cvq_stop(NetClientState *nc)
601 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
603 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
605 if (s->vhost_vdpa.shadow_vqs_enabled) {
606 vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
607 vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status);
610 vhost_vdpa_net_client_stop(nc);
613 static ssize_t vhost_vdpa_net_cvq_add(VhostVDPAState *s,
614 const struct iovec *out_sg, size_t out_num,
615 const struct iovec *in_sg, size_t in_num)
617 VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
618 int r;
620 r = vhost_svq_add(svq, out_sg, out_num, in_sg, in_num, NULL);
621 if (unlikely(r != 0)) {
622 if (unlikely(r == -ENOSPC)) {
623 qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n",
624 __func__);
628 return r;
632 * Convenience wrapper to poll SVQ for multiple control commands.
634 * Caller should hold the BQL when invoking this function, and should take
635 * the answer before SVQ pulls by itself when BQL is released.
637 static ssize_t vhost_vdpa_net_svq_poll(VhostVDPAState *s, size_t cmds_in_flight)
639 VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
640 return vhost_svq_poll(svq, cmds_in_flight);
643 static void vhost_vdpa_net_load_cursor_reset(VhostVDPAState *s,
644 struct iovec *out_cursor,
645 struct iovec *in_cursor)
647 /* reset the cursor of the output buffer for the device */
648 out_cursor->iov_base = s->cvq_cmd_out_buffer;
649 out_cursor->iov_len = vhost_vdpa_net_cvq_cmd_page_len();
651 /* reset the cursor of the in buffer for the device */
652 in_cursor->iov_base = s->status;
653 in_cursor->iov_len = vhost_vdpa_net_cvq_cmd_page_len();
657 * Poll SVQ for multiple pending control commands and check the device's ack.
659 * Caller should hold the BQL when invoking this function.
661 * @s: The VhostVDPAState
662 * @len: The length of the pending status shadow buffer
664 static ssize_t vhost_vdpa_net_svq_flush(VhostVDPAState *s, size_t len)
666 /* device uses a one-byte length ack for each control command */
667 ssize_t dev_written = vhost_vdpa_net_svq_poll(s, len);
668 if (unlikely(dev_written != len)) {
669 return -EIO;
672 /* check the device's ack */
673 for (int i = 0; i < len; ++i) {
674 if (s->status[i] != VIRTIO_NET_OK) {
675 return -EIO;
678 return 0;
681 static ssize_t vhost_vdpa_net_load_cmd(VhostVDPAState *s,
682 struct iovec *out_cursor,
683 struct iovec *in_cursor, uint8_t class,
684 uint8_t cmd, const struct iovec *data_sg,
685 size_t data_num)
687 const struct virtio_net_ctrl_hdr ctrl = {
688 .class = class,
689 .cmd = cmd,
691 size_t data_size = iov_size(data_sg, data_num), cmd_size;
692 struct iovec out, in;
693 ssize_t r;
694 unsigned dummy_cursor_iov_cnt;
695 VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
697 assert(data_size < vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl));
698 cmd_size = sizeof(ctrl) + data_size;
699 if (vhost_svq_available_slots(svq) < 2 ||
700 iov_size(out_cursor, 1) < cmd_size) {
702 * It is time to flush all pending control commands if SVQ is full
703 * or control commands shadow buffers are full.
705 * We can poll here since we've had BQL from the time
706 * we sent the descriptor.
708 r = vhost_vdpa_net_svq_flush(s, in_cursor->iov_base -
709 (void *)s->status);
710 if (unlikely(r < 0)) {
711 return r;
714 vhost_vdpa_net_load_cursor_reset(s, out_cursor, in_cursor);
717 /* pack the CVQ command header */
718 iov_from_buf(out_cursor, 1, 0, &ctrl, sizeof(ctrl));
719 /* pack the CVQ command command-specific-data */
720 iov_to_buf(data_sg, data_num, 0,
721 out_cursor->iov_base + sizeof(ctrl), data_size);
723 /* extract the required buffer from the cursor for output */
724 iov_copy(&out, 1, out_cursor, 1, 0, cmd_size);
725 /* extract the required buffer from the cursor for input */
726 iov_copy(&in, 1, in_cursor, 1, 0, sizeof(*s->status));
728 r = vhost_vdpa_net_cvq_add(s, &out, 1, &in, 1);
729 if (unlikely(r < 0)) {
730 return r;
733 /* iterate the cursors */
734 dummy_cursor_iov_cnt = 1;
735 iov_discard_front(&out_cursor, &dummy_cursor_iov_cnt, cmd_size);
736 dummy_cursor_iov_cnt = 1;
737 iov_discard_front(&in_cursor, &dummy_cursor_iov_cnt, sizeof(*s->status));
739 return 0;
742 static int vhost_vdpa_net_load_mac(VhostVDPAState *s, const VirtIONet *n,
743 struct iovec *out_cursor,
744 struct iovec *in_cursor)
746 if (virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
747 const struct iovec data = {
748 .iov_base = (void *)n->mac,
749 .iov_len = sizeof(n->mac),
751 ssize_t r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
752 VIRTIO_NET_CTRL_MAC,
753 VIRTIO_NET_CTRL_MAC_ADDR_SET,
754 &data, 1);
755 if (unlikely(r < 0)) {
756 return r;
761 * According to VirtIO standard, "The device MUST have an
762 * empty MAC filtering table on reset.".
764 * Therefore, there is no need to send this CVQ command if the
765 * driver also sets an empty MAC filter table, which aligns with
766 * the device's defaults.
768 * Note that the device's defaults can mismatch the driver's
769 * configuration only at live migration.
771 if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_RX) ||
772 n->mac_table.in_use == 0) {
773 return 0;
776 uint32_t uni_entries = n->mac_table.first_multi,
777 uni_macs_size = uni_entries * ETH_ALEN,
778 mul_entries = n->mac_table.in_use - uni_entries,
779 mul_macs_size = mul_entries * ETH_ALEN;
780 struct virtio_net_ctrl_mac uni = {
781 .entries = cpu_to_le32(uni_entries),
783 struct virtio_net_ctrl_mac mul = {
784 .entries = cpu_to_le32(mul_entries),
786 const struct iovec data[] = {
788 .iov_base = &uni,
789 .iov_len = sizeof(uni),
790 }, {
791 .iov_base = n->mac_table.macs,
792 .iov_len = uni_macs_size,
793 }, {
794 .iov_base = &mul,
795 .iov_len = sizeof(mul),
796 }, {
797 .iov_base = &n->mac_table.macs[uni_macs_size],
798 .iov_len = mul_macs_size,
801 ssize_t r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
802 VIRTIO_NET_CTRL_MAC,
803 VIRTIO_NET_CTRL_MAC_TABLE_SET,
804 data, ARRAY_SIZE(data));
805 if (unlikely(r < 0)) {
806 return r;
809 return 0;
812 static int vhost_vdpa_net_load_rss(VhostVDPAState *s, const VirtIONet *n,
813 struct iovec *out_cursor,
814 struct iovec *in_cursor, bool do_rss)
816 struct virtio_net_rss_config cfg = {};
817 ssize_t r;
818 g_autofree uint16_t *table = NULL;
821 * According to VirtIO standard, "Initially the device has all hash
822 * types disabled and reports only VIRTIO_NET_HASH_REPORT_NONE.".
824 * Therefore, there is no need to send this CVQ command if the
825 * driver disables the all hash types, which aligns with
826 * the device's defaults.
828 * Note that the device's defaults can mismatch the driver's
829 * configuration only at live migration.
831 if (!n->rss_data.enabled ||
832 n->rss_data.hash_types == VIRTIO_NET_HASH_REPORT_NONE) {
833 return 0;
836 table = g_malloc_n(n->rss_data.indirections_len,
837 sizeof(n->rss_data.indirections_table[0]));
838 cfg.hash_types = cpu_to_le32(n->rss_data.hash_types);
840 if (do_rss) {
842 * According to VirtIO standard, "Number of entries in indirection_table
843 * is (indirection_table_mask + 1)".
845 cfg.indirection_table_mask = cpu_to_le16(n->rss_data.indirections_len -
847 cfg.unclassified_queue = cpu_to_le16(n->rss_data.default_queue);
848 for (int i = 0; i < n->rss_data.indirections_len; ++i) {
849 table[i] = cpu_to_le16(n->rss_data.indirections_table[i]);
851 cfg.max_tx_vq = cpu_to_le16(n->curr_queue_pairs);
852 } else {
854 * According to VirtIO standard, "Field reserved MUST contain zeroes.
855 * It is defined to make the structure to match the layout of
856 * virtio_net_rss_config structure, defined in 5.1.6.5.7.".
858 * Therefore, we need to zero the fields in
859 * struct virtio_net_rss_config, which corresponds to the
860 * `reserved` field in struct virtio_net_hash_config.
862 * Note that all other fields are zeroed at their definitions,
863 * except for the `indirection_table` field, where the actual data
864 * is stored in the `table` variable to ensure compatibility
865 * with RSS case. Therefore, we need to zero the `table` variable here.
867 table[0] = 0;
871 * Considering that virtio_net_handle_rss() currently does not restore
872 * the hash key length parsed from the CVQ command sent from the guest
873 * into n->rss_data and uses the maximum key length in other code, so
874 * we also employ the maximum key length here.
876 cfg.hash_key_length = sizeof(n->rss_data.key);
878 const struct iovec data[] = {
880 .iov_base = &cfg,
881 .iov_len = offsetof(struct virtio_net_rss_config,
882 indirection_table),
883 }, {
884 .iov_base = table,
885 .iov_len = n->rss_data.indirections_len *
886 sizeof(n->rss_data.indirections_table[0]),
887 }, {
888 .iov_base = &cfg.max_tx_vq,
889 .iov_len = offsetof(struct virtio_net_rss_config, hash_key_data) -
890 offsetof(struct virtio_net_rss_config, max_tx_vq),
891 }, {
892 .iov_base = (void *)n->rss_data.key,
893 .iov_len = sizeof(n->rss_data.key),
897 r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
898 VIRTIO_NET_CTRL_MQ,
899 do_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG :
900 VIRTIO_NET_CTRL_MQ_HASH_CONFIG,
901 data, ARRAY_SIZE(data));
902 if (unlikely(r < 0)) {
903 return r;
906 return 0;
909 static int vhost_vdpa_net_load_mq(VhostVDPAState *s,
910 const VirtIONet *n,
911 struct iovec *out_cursor,
912 struct iovec *in_cursor)
914 struct virtio_net_ctrl_mq mq;
915 ssize_t r;
917 if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_MQ)) {
918 return 0;
921 mq.virtqueue_pairs = cpu_to_le16(n->curr_queue_pairs);
922 const struct iovec data = {
923 .iov_base = &mq,
924 .iov_len = sizeof(mq),
926 r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
927 VIRTIO_NET_CTRL_MQ,
928 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET,
929 &data, 1);
930 if (unlikely(r < 0)) {
931 return r;
934 if (virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_RSS)) {
935 /* load the receive-side scaling state */
936 r = vhost_vdpa_net_load_rss(s, n, out_cursor, in_cursor, true);
937 if (unlikely(r < 0)) {
938 return r;
940 } else if (virtio_vdev_has_feature(&n->parent_obj,
941 VIRTIO_NET_F_HASH_REPORT)) {
942 /* load the hash calculation state */
943 r = vhost_vdpa_net_load_rss(s, n, out_cursor, in_cursor, false);
944 if (unlikely(r < 0)) {
945 return r;
949 return 0;
952 static int vhost_vdpa_net_load_offloads(VhostVDPAState *s,
953 const VirtIONet *n,
954 struct iovec *out_cursor,
955 struct iovec *in_cursor)
957 uint64_t offloads;
958 ssize_t r;
960 if (!virtio_vdev_has_feature(&n->parent_obj,
961 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
962 return 0;
965 if (n->curr_guest_offloads == virtio_net_supported_guest_offloads(n)) {
967 * According to VirtIO standard, "Upon feature negotiation
968 * corresponding offload gets enabled to preserve
969 * backward compatibility.".
971 * Therefore, there is no need to send this CVQ command if the
972 * driver also enables all supported offloads, which aligns with
973 * the device's defaults.
975 * Note that the device's defaults can mismatch the driver's
976 * configuration only at live migration.
978 return 0;
981 offloads = cpu_to_le64(n->curr_guest_offloads);
982 const struct iovec data = {
983 .iov_base = &offloads,
984 .iov_len = sizeof(offloads),
986 r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
987 VIRTIO_NET_CTRL_GUEST_OFFLOADS,
988 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET,
989 &data, 1);
990 if (unlikely(r < 0)) {
991 return r;
994 return 0;
997 static int vhost_vdpa_net_load_rx_mode(VhostVDPAState *s,
998 struct iovec *out_cursor,
999 struct iovec *in_cursor,
1000 uint8_t cmd,
1001 uint8_t on)
1003 const struct iovec data = {
1004 .iov_base = &on,
1005 .iov_len = sizeof(on),
1007 ssize_t r;
1009 r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
1010 VIRTIO_NET_CTRL_RX, cmd, &data, 1);
1011 if (unlikely(r < 0)) {
1012 return r;
1015 return 0;
1018 static int vhost_vdpa_net_load_rx(VhostVDPAState *s,
1019 const VirtIONet *n,
1020 struct iovec *out_cursor,
1021 struct iovec *in_cursor)
1023 ssize_t r;
1025 if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_RX)) {
1026 return 0;
1030 * According to virtio_net_reset(), device turns promiscuous mode
1031 * on by default.
1033 * Additionally, according to VirtIO standard, "Since there are
1034 * no guarantees, it can use a hash filter or silently switch to
1035 * allmulti or promiscuous mode if it is given too many addresses.".
1036 * QEMU marks `n->mac_table.uni_overflow` if guest sets too many
1037 * non-multicast MAC addresses, indicating that promiscuous mode
1038 * should be enabled.
1040 * Therefore, QEMU should only send this CVQ command if the
1041 * `n->mac_table.uni_overflow` is not marked and `n->promisc` is off,
1042 * which sets promiscuous mode on, different from the device's defaults.
1044 * Note that the device's defaults can mismatch the driver's
1045 * configuration only at live migration.
1047 if (!n->mac_table.uni_overflow && !n->promisc) {
1048 r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1049 VIRTIO_NET_CTRL_RX_PROMISC, 0);
1050 if (unlikely(r < 0)) {
1051 return r;
1056 * According to virtio_net_reset(), device turns all-multicast mode
1057 * off by default.
1059 * According to VirtIO standard, "Since there are no guarantees,
1060 * it can use a hash filter or silently switch to allmulti or
1061 * promiscuous mode if it is given too many addresses.". QEMU marks
1062 * `n->mac_table.multi_overflow` if guest sets too many
1063 * non-multicast MAC addresses.
1065 * Therefore, QEMU should only send this CVQ command if the
1066 * `n->mac_table.multi_overflow` is marked or `n->allmulti` is on,
1067 * which sets all-multicast mode on, different from the device's defaults.
1069 * Note that the device's defaults can mismatch the driver's
1070 * configuration only at live migration.
1072 if (n->mac_table.multi_overflow || n->allmulti) {
1073 r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1074 VIRTIO_NET_CTRL_RX_ALLMULTI, 1);
1075 if (unlikely(r < 0)) {
1076 return r;
1080 if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_RX_EXTRA)) {
1081 return 0;
1085 * According to virtio_net_reset(), device turns all-unicast mode
1086 * off by default.
1088 * Therefore, QEMU should only send this CVQ command if the driver
1089 * sets all-unicast mode on, different from the device's defaults.
1091 * Note that the device's defaults can mismatch the driver's
1092 * configuration only at live migration.
1094 if (n->alluni) {
1095 r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1096 VIRTIO_NET_CTRL_RX_ALLUNI, 1);
1097 if (r < 0) {
1098 return r;
1103 * According to virtio_net_reset(), device turns non-multicast mode
1104 * off by default.
1106 * Therefore, QEMU should only send this CVQ command if the driver
1107 * sets non-multicast mode on, different from the device's defaults.
1109 * Note that the device's defaults can mismatch the driver's
1110 * configuration only at live migration.
1112 if (n->nomulti) {
1113 r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1114 VIRTIO_NET_CTRL_RX_NOMULTI, 1);
1115 if (r < 0) {
1116 return r;
1121 * According to virtio_net_reset(), device turns non-unicast mode
1122 * off by default.
1124 * Therefore, QEMU should only send this CVQ command if the driver
1125 * sets non-unicast mode on, different from the device's defaults.
1127 * Note that the device's defaults can mismatch the driver's
1128 * configuration only at live migration.
1130 if (n->nouni) {
1131 r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1132 VIRTIO_NET_CTRL_RX_NOUNI, 1);
1133 if (r < 0) {
1134 return r;
1139 * According to virtio_net_reset(), device turns non-broadcast mode
1140 * off by default.
1142 * Therefore, QEMU should only send this CVQ command if the driver
1143 * sets non-broadcast mode on, different from the device's defaults.
1145 * Note that the device's defaults can mismatch the driver's
1146 * configuration only at live migration.
1148 if (n->nobcast) {
1149 r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1150 VIRTIO_NET_CTRL_RX_NOBCAST, 1);
1151 if (r < 0) {
1152 return r;
1156 return 0;
1159 static int vhost_vdpa_net_load_single_vlan(VhostVDPAState *s,
1160 const VirtIONet *n,
1161 struct iovec *out_cursor,
1162 struct iovec *in_cursor,
1163 uint16_t vid)
1165 const struct iovec data = {
1166 .iov_base = &vid,
1167 .iov_len = sizeof(vid),
1169 ssize_t r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
1170 VIRTIO_NET_CTRL_VLAN,
1171 VIRTIO_NET_CTRL_VLAN_ADD,
1172 &data, 1);
1173 if (unlikely(r < 0)) {
1174 return r;
1177 return 0;
1180 static int vhost_vdpa_net_load_vlan(VhostVDPAState *s,
1181 const VirtIONet *n,
1182 struct iovec *out_cursor,
1183 struct iovec *in_cursor)
1185 int r;
1187 if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_VLAN)) {
1188 return 0;
1191 for (int i = 0; i < MAX_VLAN >> 5; i++) {
1192 for (int j = 0; n->vlans[i] && j <= 0x1f; j++) {
1193 if (n->vlans[i] & (1U << j)) {
1194 r = vhost_vdpa_net_load_single_vlan(s, n, out_cursor,
1195 in_cursor, (i << 5) + j);
1196 if (unlikely(r != 0)) {
1197 return r;
1203 return 0;
1206 static int vhost_vdpa_net_cvq_load(NetClientState *nc)
1208 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1209 struct vhost_vdpa *v = &s->vhost_vdpa;
1210 const VirtIONet *n;
1211 int r;
1212 struct iovec out_cursor, in_cursor;
1214 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1216 vhost_vdpa_set_vring_ready(v, v->dev->vq_index);
1218 if (v->shadow_vqs_enabled) {
1219 n = VIRTIO_NET(v->dev->vdev);
1220 vhost_vdpa_net_load_cursor_reset(s, &out_cursor, &in_cursor);
1221 r = vhost_vdpa_net_load_mac(s, n, &out_cursor, &in_cursor);
1222 if (unlikely(r < 0)) {
1223 return r;
1225 r = vhost_vdpa_net_load_mq(s, n, &out_cursor, &in_cursor);
1226 if (unlikely(r)) {
1227 return r;
1229 r = vhost_vdpa_net_load_offloads(s, n, &out_cursor, &in_cursor);
1230 if (unlikely(r)) {
1231 return r;
1233 r = vhost_vdpa_net_load_rx(s, n, &out_cursor, &in_cursor);
1234 if (unlikely(r)) {
1235 return r;
1237 r = vhost_vdpa_net_load_vlan(s, n, &out_cursor, &in_cursor);
1238 if (unlikely(r)) {
1239 return r;
1243 * We need to poll and check all pending device's used buffers.
1245 * We can poll here since we've had BQL from the time
1246 * we sent the descriptor.
1248 r = vhost_vdpa_net_svq_flush(s, in_cursor.iov_base - (void *)s->status);
1249 if (unlikely(r)) {
1250 return r;
1254 for (int i = 0; i < v->dev->vq_index; ++i) {
1255 vhost_vdpa_set_vring_ready(v, i);
1258 return 0;
1261 static NetClientInfo net_vhost_vdpa_cvq_info = {
1262 .type = NET_CLIENT_DRIVER_VHOST_VDPA,
1263 .size = sizeof(VhostVDPAState),
1264 .receive = vhost_vdpa_receive,
1265 .start = vhost_vdpa_net_cvq_start,
1266 .load = vhost_vdpa_net_cvq_load,
1267 .stop = vhost_vdpa_net_cvq_stop,
1268 .cleanup = vhost_vdpa_cleanup,
1269 .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
1270 .has_ufo = vhost_vdpa_has_ufo,
1271 .check_peer_type = vhost_vdpa_check_peer_type,
1272 .set_steering_ebpf = vhost_vdpa_set_steering_ebpf,
1276 * Forward the excessive VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command to
1277 * vdpa device.
1279 * Considering that QEMU cannot send the entire filter table to the
1280 * vdpa device, it should send the VIRTIO_NET_CTRL_RX_PROMISC CVQ
1281 * command to enable promiscuous mode to receive all packets,
1282 * according to VirtIO standard, "Since there are no guarantees,
1283 * it can use a hash filter or silently switch to allmulti or
1284 * promiscuous mode if it is given too many addresses.".
1286 * Since QEMU ignores MAC addresses beyond `MAC_TABLE_ENTRIES` and
1287 * marks `n->mac_table.x_overflow` accordingly, it should have
1288 * the same effect on the device model to receive
1289 * (`MAC_TABLE_ENTRIES` + 1) or more non-multicast MAC addresses.
1290 * The same applies to multicast MAC addresses.
1292 * Therefore, QEMU can provide the device model with a fake
1293 * VIRTIO_NET_CTRL_MAC_TABLE_SET command with (`MAC_TABLE_ENTRIES` + 1)
1294 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1) multicast
1295 * MAC addresses. This ensures that the device model marks
1296 * `n->mac_table.uni_overflow` and `n->mac_table.multi_overflow`,
1297 * allowing all packets to be received, which aligns with the
1298 * state of the vdpa device.
1300 static int vhost_vdpa_net_excessive_mac_filter_cvq_add(VhostVDPAState *s,
1301 VirtQueueElement *elem,
1302 struct iovec *out,
1303 const struct iovec *in)
1305 struct virtio_net_ctrl_mac mac_data, *mac_ptr;
1306 struct virtio_net_ctrl_hdr *hdr_ptr;
1307 uint32_t cursor;
1308 ssize_t r;
1309 uint8_t on = 1;
1311 /* parse the non-multicast MAC address entries from CVQ command */
1312 cursor = sizeof(*hdr_ptr);
1313 r = iov_to_buf(elem->out_sg, elem->out_num, cursor,
1314 &mac_data, sizeof(mac_data));
1315 if (unlikely(r != sizeof(mac_data))) {
1317 * If the CVQ command is invalid, we should simulate the vdpa device
1318 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1320 *s->status = VIRTIO_NET_ERR;
1321 return sizeof(*s->status);
1323 cursor += sizeof(mac_data) + le32_to_cpu(mac_data.entries) * ETH_ALEN;
1325 /* parse the multicast MAC address entries from CVQ command */
1326 r = iov_to_buf(elem->out_sg, elem->out_num, cursor,
1327 &mac_data, sizeof(mac_data));
1328 if (r != sizeof(mac_data)) {
1330 * If the CVQ command is invalid, we should simulate the vdpa device
1331 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1333 *s->status = VIRTIO_NET_ERR;
1334 return sizeof(*s->status);
1336 cursor += sizeof(mac_data) + le32_to_cpu(mac_data.entries) * ETH_ALEN;
1338 /* validate the CVQ command */
1339 if (iov_size(elem->out_sg, elem->out_num) != cursor) {
1341 * If the CVQ command is invalid, we should simulate the vdpa device
1342 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1344 *s->status = VIRTIO_NET_ERR;
1345 return sizeof(*s->status);
1349 * According to VirtIO standard, "Since there are no guarantees,
1350 * it can use a hash filter or silently switch to allmulti or
1351 * promiscuous mode if it is given too many addresses.".
1353 * Therefore, considering that QEMU is unable to send the entire
1354 * filter table to the vdpa device, it should send the
1355 * VIRTIO_NET_CTRL_RX_PROMISC CVQ command to enable promiscuous mode
1357 hdr_ptr = out->iov_base;
1358 out->iov_len = sizeof(*hdr_ptr) + sizeof(on);
1360 hdr_ptr->class = VIRTIO_NET_CTRL_RX;
1361 hdr_ptr->cmd = VIRTIO_NET_CTRL_RX_PROMISC;
1362 iov_from_buf(out, 1, sizeof(*hdr_ptr), &on, sizeof(on));
1363 r = vhost_vdpa_net_cvq_add(s, out, 1, in, 1);
1364 if (unlikely(r < 0)) {
1365 return r;
1369 * We can poll here since we've had BQL from the time
1370 * we sent the descriptor.
1372 r = vhost_vdpa_net_svq_poll(s, 1);
1373 if (unlikely(r < sizeof(*s->status))) {
1374 return r;
1376 if (*s->status != VIRTIO_NET_OK) {
1377 return sizeof(*s->status);
1381 * QEMU should also send a fake VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ
1382 * command to the device model, including (`MAC_TABLE_ENTRIES` + 1)
1383 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1)
1384 * multicast MAC addresses.
1386 * By doing so, the device model can mark `n->mac_table.uni_overflow`
1387 * and `n->mac_table.multi_overflow`, enabling all packets to be
1388 * received, which aligns with the state of the vdpa device.
1390 cursor = 0;
1391 uint32_t fake_uni_entries = MAC_TABLE_ENTRIES + 1,
1392 fake_mul_entries = MAC_TABLE_ENTRIES + 1,
1393 fake_cvq_size = sizeof(struct virtio_net_ctrl_hdr) +
1394 sizeof(mac_data) + fake_uni_entries * ETH_ALEN +
1395 sizeof(mac_data) + fake_mul_entries * ETH_ALEN;
1397 assert(fake_cvq_size < vhost_vdpa_net_cvq_cmd_page_len());
1398 out->iov_len = fake_cvq_size;
1400 /* pack the header for fake CVQ command */
1401 hdr_ptr = out->iov_base + cursor;
1402 hdr_ptr->class = VIRTIO_NET_CTRL_MAC;
1403 hdr_ptr->cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1404 cursor += sizeof(*hdr_ptr);
1407 * Pack the non-multicast MAC addresses part for fake CVQ command.
1409 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1410 * addresses provided in CVQ command. Therefore, only the entries
1411 * field need to be prepared in the CVQ command.
1413 mac_ptr = out->iov_base + cursor;
1414 mac_ptr->entries = cpu_to_le32(fake_uni_entries);
1415 cursor += sizeof(*mac_ptr) + fake_uni_entries * ETH_ALEN;
1418 * Pack the multicast MAC addresses part for fake CVQ command.
1420 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1421 * addresses provided in CVQ command. Therefore, only the entries
1422 * field need to be prepared in the CVQ command.
1424 mac_ptr = out->iov_base + cursor;
1425 mac_ptr->entries = cpu_to_le32(fake_mul_entries);
1428 * Simulating QEMU poll a vdpa device used buffer
1429 * for VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1431 return sizeof(*s->status);
1435 * Validate and copy control virtqueue commands.
1437 * Following QEMU guidelines, we offer a copy of the buffers to the device to
1438 * prevent TOCTOU bugs.
1440 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
1441 VirtQueueElement *elem,
1442 void *opaque)
1444 VhostVDPAState *s = opaque;
1445 size_t in_len;
1446 const struct virtio_net_ctrl_hdr *ctrl;
1447 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
1448 /* Out buffer sent to both the vdpa device and the device model */
1449 struct iovec out = {
1450 .iov_base = s->cvq_cmd_out_buffer,
1452 /* in buffer used for device model */
1453 const struct iovec model_in = {
1454 .iov_base = &status,
1455 .iov_len = sizeof(status),
1457 /* in buffer used for vdpa device */
1458 const struct iovec vdpa_in = {
1459 .iov_base = s->status,
1460 .iov_len = sizeof(*s->status),
1462 ssize_t dev_written = -EINVAL;
1464 out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0,
1465 s->cvq_cmd_out_buffer,
1466 vhost_vdpa_net_cvq_cmd_page_len());
1468 ctrl = s->cvq_cmd_out_buffer;
1469 if (ctrl->class == VIRTIO_NET_CTRL_ANNOUNCE) {
1471 * Guest announce capability is emulated by qemu, so don't forward to
1472 * the device.
1474 dev_written = sizeof(status);
1475 *s->status = VIRTIO_NET_OK;
1476 } else if (unlikely(ctrl->class == VIRTIO_NET_CTRL_MAC &&
1477 ctrl->cmd == VIRTIO_NET_CTRL_MAC_TABLE_SET &&
1478 iov_size(elem->out_sg, elem->out_num) > out.iov_len)) {
1480 * Due to the size limitation of the out buffer sent to the vdpa device,
1481 * which is determined by vhost_vdpa_net_cvq_cmd_page_len(), excessive
1482 * MAC addresses set by the driver for the filter table can cause
1483 * truncation of the CVQ command in QEMU. As a result, the vdpa device
1484 * rejects the flawed CVQ command.
1486 * Therefore, QEMU must handle this situation instead of sending
1487 * the CVQ command directly.
1489 dev_written = vhost_vdpa_net_excessive_mac_filter_cvq_add(s, elem,
1490 &out, &vdpa_in);
1491 if (unlikely(dev_written < 0)) {
1492 goto out;
1494 } else {
1495 ssize_t r;
1496 r = vhost_vdpa_net_cvq_add(s, &out, 1, &vdpa_in, 1);
1497 if (unlikely(r < 0)) {
1498 dev_written = r;
1499 goto out;
1503 * We can poll here since we've had BQL from the time
1504 * we sent the descriptor.
1506 dev_written = vhost_vdpa_net_svq_poll(s, 1);
1509 if (unlikely(dev_written < sizeof(status))) {
1510 error_report("Insufficient written data (%zu)", dev_written);
1511 goto out;
1514 if (*s->status != VIRTIO_NET_OK) {
1515 goto out;
1518 status = VIRTIO_NET_ERR;
1519 virtio_net_handle_ctrl_iov(svq->vdev, &model_in, 1, &out, 1);
1520 if (status != VIRTIO_NET_OK) {
1521 error_report("Bad CVQ processing in model");
1524 out:
1525 in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status,
1526 sizeof(status));
1527 if (unlikely(in_len < sizeof(status))) {
1528 error_report("Bad device CVQ written length");
1530 vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status)));
1532 * `elem` belongs to vhost_vdpa_net_handle_ctrl_avail() only when
1533 * the function successfully forwards the CVQ command, indicated
1534 * by a non-negative value of `dev_written`. Otherwise, it still
1535 * belongs to SVQ.
1536 * This function should only free the `elem` when it owns.
1538 if (dev_written >= 0) {
1539 g_free(elem);
1541 return dev_written < 0 ? dev_written : 0;
1544 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = {
1545 .avail_handler = vhost_vdpa_net_handle_ctrl_avail,
1549 * Probe if CVQ is isolated
1551 * @device_fd The vdpa device fd
1552 * @features Features offered by the device.
1553 * @cvq_index The control vq pair index
1555 * Returns <0 in case of failure, 0 if false and 1 if true.
1557 static int vhost_vdpa_probe_cvq_isolation(int device_fd, uint64_t features,
1558 int cvq_index, Error **errp)
1560 uint64_t backend_features;
1561 int64_t cvq_group;
1562 uint8_t status = VIRTIO_CONFIG_S_ACKNOWLEDGE |
1563 VIRTIO_CONFIG_S_DRIVER;
1564 int r;
1566 ERRP_GUARD();
1568 r = ioctl(device_fd, VHOST_GET_BACKEND_FEATURES, &backend_features);
1569 if (unlikely(r < 0)) {
1570 error_setg_errno(errp, errno, "Cannot get vdpa backend_features");
1571 return r;
1574 if (!(backend_features & BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID))) {
1575 return 0;
1578 r = ioctl(device_fd, VHOST_VDPA_SET_STATUS, &status);
1579 if (unlikely(r)) {
1580 error_setg_errno(errp, -r, "Cannot set device status");
1581 goto out;
1584 r = ioctl(device_fd, VHOST_SET_FEATURES, &features);
1585 if (unlikely(r)) {
1586 error_setg_errno(errp, -r, "Cannot set features");
1587 goto out;
1590 status |= VIRTIO_CONFIG_S_FEATURES_OK;
1591 r = ioctl(device_fd, VHOST_VDPA_SET_STATUS, &status);
1592 if (unlikely(r)) {
1593 error_setg_errno(errp, -r, "Cannot set device status");
1594 goto out;
1597 cvq_group = vhost_vdpa_get_vring_group(device_fd, cvq_index, errp);
1598 if (unlikely(cvq_group < 0)) {
1599 if (cvq_group != -ENOTSUP) {
1600 r = cvq_group;
1601 goto out;
1605 * The kernel report VHOST_BACKEND_F_IOTLB_ASID if the vdpa frontend
1606 * support ASID even if the parent driver does not. The CVQ cannot be
1607 * isolated in this case.
1609 error_free(*errp);
1610 *errp = NULL;
1611 r = 0;
1612 goto out;
1615 for (int i = 0; i < cvq_index; ++i) {
1616 int64_t group = vhost_vdpa_get_vring_group(device_fd, i, errp);
1617 if (unlikely(group < 0)) {
1618 r = group;
1619 goto out;
1622 if (group == (int64_t)cvq_group) {
1623 r = 0;
1624 goto out;
1628 r = 1;
1630 out:
1631 status = 0;
1632 ioctl(device_fd, VHOST_VDPA_SET_STATUS, &status);
1633 return r;
1636 static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
1637 const char *device,
1638 const char *name,
1639 int vdpa_device_fd,
1640 int queue_pair_index,
1641 int nvqs,
1642 bool is_datapath,
1643 bool svq,
1644 struct vhost_vdpa_iova_range iova_range,
1645 uint64_t features,
1646 VhostVDPAShared *shared,
1647 Error **errp)
1649 NetClientState *nc = NULL;
1650 VhostVDPAState *s;
1651 int ret = 0;
1652 assert(name);
1653 int cvq_isolated = 0;
1655 if (is_datapath) {
1656 nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device,
1657 name);
1658 } else {
1659 cvq_isolated = vhost_vdpa_probe_cvq_isolation(vdpa_device_fd, features,
1660 queue_pair_index * 2,
1661 errp);
1662 if (unlikely(cvq_isolated < 0)) {
1663 return NULL;
1666 nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer,
1667 device, name);
1669 qemu_set_info_str(nc, TYPE_VHOST_VDPA);
1670 s = DO_UPCAST(VhostVDPAState, nc, nc);
1672 s->vhost_vdpa.index = queue_pair_index;
1673 s->always_svq = svq;
1674 s->migration_state.notify = NULL;
1675 s->vhost_vdpa.shadow_vqs_enabled = svq;
1676 if (queue_pair_index == 0) {
1677 vhost_vdpa_net_valid_svq_features(features,
1678 &s->vhost_vdpa.migration_blocker);
1679 s->vhost_vdpa.shared = g_new0(VhostVDPAShared, 1);
1680 s->vhost_vdpa.shared->device_fd = vdpa_device_fd;
1681 s->vhost_vdpa.shared->iova_range = iova_range;
1682 s->vhost_vdpa.shared->shadow_data = svq;
1683 } else if (!is_datapath) {
1684 s->cvq_cmd_out_buffer = mmap(NULL, vhost_vdpa_net_cvq_cmd_page_len(),
1685 PROT_READ | PROT_WRITE,
1686 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1687 s->status = mmap(NULL, vhost_vdpa_net_cvq_cmd_page_len(),
1688 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,
1689 -1, 0);
1691 s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops;
1692 s->vhost_vdpa.shadow_vq_ops_opaque = s;
1693 s->cvq_isolated = cvq_isolated;
1695 if (queue_pair_index != 0) {
1696 s->vhost_vdpa.shared = shared;
1699 ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);
1700 if (ret) {
1701 qemu_del_net_client(nc);
1702 return NULL;
1705 return nc;
1708 static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp)
1710 int ret = ioctl(fd, VHOST_GET_FEATURES, features);
1711 if (unlikely(ret < 0)) {
1712 error_setg_errno(errp, errno,
1713 "Fail to query features from vhost-vDPA device");
1715 return ret;
1718 static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features,
1719 int *has_cvq, Error **errp)
1721 unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
1722 g_autofree struct vhost_vdpa_config *config = NULL;
1723 __virtio16 *max_queue_pairs;
1724 int ret;
1726 if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) {
1727 *has_cvq = 1;
1728 } else {
1729 *has_cvq = 0;
1732 if (features & (1 << VIRTIO_NET_F_MQ)) {
1733 config = g_malloc0(config_size + sizeof(*max_queue_pairs));
1734 config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs);
1735 config->len = sizeof(*max_queue_pairs);
1737 ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config);
1738 if (ret) {
1739 error_setg(errp, "Fail to get config from vhost-vDPA device");
1740 return -ret;
1743 max_queue_pairs = (__virtio16 *)&config->buf;
1745 return lduw_le_p(max_queue_pairs);
1748 return 1;
1751 int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
1752 NetClientState *peer, Error **errp)
1754 const NetdevVhostVDPAOptions *opts;
1755 uint64_t features;
1756 int vdpa_device_fd;
1757 g_autofree NetClientState **ncs = NULL;
1758 struct vhost_vdpa_iova_range iova_range;
1759 NetClientState *nc;
1760 int queue_pairs, r, i = 0, has_cvq = 0;
1762 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1763 opts = &netdev->u.vhost_vdpa;
1764 if (!opts->vhostdev && !opts->vhostfd) {
1765 error_setg(errp,
1766 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
1767 return -1;
1770 if (opts->vhostdev && opts->vhostfd) {
1771 error_setg(errp,
1772 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
1773 return -1;
1776 if (opts->vhostdev) {
1777 vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
1778 if (vdpa_device_fd == -1) {
1779 return -errno;
1781 } else {
1782 /* has_vhostfd */
1783 vdpa_device_fd = monitor_fd_param(monitor_cur(), opts->vhostfd, errp);
1784 if (vdpa_device_fd == -1) {
1785 error_prepend(errp, "vhost-vdpa: unable to parse vhostfd: ");
1786 return -1;
1790 r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
1791 if (unlikely(r < 0)) {
1792 goto err;
1795 queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
1796 &has_cvq, errp);
1797 if (queue_pairs < 0) {
1798 qemu_close(vdpa_device_fd);
1799 return queue_pairs;
1802 r = vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);
1803 if (unlikely(r < 0)) {
1804 error_setg(errp, "vhost-vdpa: get iova range failed: %s",
1805 strerror(-r));
1806 goto err;
1809 if (opts->x_svq && !vhost_vdpa_net_valid_svq_features(features, errp)) {
1810 goto err;
1813 ncs = g_malloc0(sizeof(*ncs) * queue_pairs);
1815 for (i = 0; i < queue_pairs; i++) {
1816 VhostVDPAShared *shared = NULL;
1818 if (i) {
1819 shared = DO_UPCAST(VhostVDPAState, nc, ncs[0])->vhost_vdpa.shared;
1821 ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
1822 vdpa_device_fd, i, 2, true, opts->x_svq,
1823 iova_range, features, shared, errp);
1824 if (!ncs[i])
1825 goto err;
1828 if (has_cvq) {
1829 VhostVDPAState *s0 = DO_UPCAST(VhostVDPAState, nc, ncs[0]);
1830 VhostVDPAShared *shared = s0->vhost_vdpa.shared;
1832 nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
1833 vdpa_device_fd, i, 1, false,
1834 opts->x_svq, iova_range, features, shared,
1835 errp);
1836 if (!nc)
1837 goto err;
1840 return 0;
1842 err:
1843 if (i) {
1844 for (i--; i >= 0; i--) {
1845 qemu_del_net_client(ncs[i]);
1849 qemu_close(vdpa_device_fd);
1851 return -1;