xref: /openbmc/qemu/net/vhost-vdpa.c (revision 6e7c96ae)
1 /*
2  * vhost-vdpa.c
3  *
4  * Copyright(c) 2017-2018 Intel Corporation.
5  * Copyright(c) 2020 Red Hat, Inc.
6  *
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.
9  *
10  */
11 
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/misc.h"
30 #include "hw/virtio/vhost.h"
31 #include "trace.h"
32 
33 /* Todo:need to add the multiqueue support here */
34 typedef struct VhostVDPAState {
35     NetClientState nc;
36     struct vhost_vdpa vhost_vdpa;
37     NotifierWithReturn migration_state;
38     VHostNetState *vhost_net;
39 
40     /* Control commands shadow buffers */
41     void *cvq_cmd_out_buffer;
42     virtio_net_ctrl_ack *status;
43 
44     /* The device always have SVQ enabled */
45     bool always_svq;
46 
47     /* The device can isolate CVQ in its own ASID */
48     bool cvq_isolated;
49 
50     bool started;
51 } VhostVDPAState;
52 
53 /*
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.
57  */
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_F_IN_ORDER,
66     VIRTIO_F_NOTIFICATION_DATA,
67     VIRTIO_NET_F_CSUM,
68     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS,
69     VIRTIO_NET_F_CTRL_MAC_ADDR,
70     VIRTIO_NET_F_CTRL_RX,
71     VIRTIO_NET_F_CTRL_RX_EXTRA,
72     VIRTIO_NET_F_CTRL_VLAN,
73     VIRTIO_NET_F_CTRL_VQ,
74     VIRTIO_NET_F_GSO,
75     VIRTIO_NET_F_GUEST_CSUM,
76     VIRTIO_NET_F_GUEST_ECN,
77     VIRTIO_NET_F_GUEST_TSO4,
78     VIRTIO_NET_F_GUEST_TSO6,
79     VIRTIO_NET_F_GUEST_UFO,
80     VIRTIO_NET_F_GUEST_USO4,
81     VIRTIO_NET_F_GUEST_USO6,
82     VIRTIO_NET_F_HASH_REPORT,
83     VIRTIO_NET_F_HOST_ECN,
84     VIRTIO_NET_F_HOST_TSO4,
85     VIRTIO_NET_F_HOST_TSO6,
86     VIRTIO_NET_F_HOST_UFO,
87     VIRTIO_NET_F_HOST_USO,
88     VIRTIO_NET_F_MQ,
89     VIRTIO_NET_F_MRG_RXBUF,
90     VIRTIO_NET_F_MTU,
91     VIRTIO_NET_F_RSC_EXT,
92     VIRTIO_NET_F_RSS,
93     VIRTIO_NET_F_STATUS,
94     VIRTIO_RING_F_EVENT_IDX,
95     VIRTIO_RING_F_INDIRECT_DESC,
96 
97     /* VHOST_INVALID_FEATURE_BIT should always be the last entry */
98     VHOST_INVALID_FEATURE_BIT
99 };
100 
101 /** Supported device specific feature bits with SVQ */
102 static const uint64_t vdpa_svq_device_features =
103     BIT_ULL(VIRTIO_NET_F_CSUM) |
104     BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) |
105     BIT_ULL(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) |
106     BIT_ULL(VIRTIO_NET_F_MTU) |
107     BIT_ULL(VIRTIO_NET_F_MAC) |
108     BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) |
109     BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) |
110     BIT_ULL(VIRTIO_NET_F_GUEST_ECN) |
111     BIT_ULL(VIRTIO_NET_F_GUEST_UFO) |
112     BIT_ULL(VIRTIO_NET_F_HOST_TSO4) |
113     BIT_ULL(VIRTIO_NET_F_HOST_TSO6) |
114     BIT_ULL(VIRTIO_NET_F_HOST_ECN) |
115     BIT_ULL(VIRTIO_NET_F_HOST_UFO) |
116     BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) |
117     BIT_ULL(VIRTIO_NET_F_STATUS) |
118     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
119     BIT_ULL(VIRTIO_NET_F_CTRL_RX) |
120     BIT_ULL(VIRTIO_NET_F_CTRL_VLAN) |
121     BIT_ULL(VIRTIO_NET_F_CTRL_RX_EXTRA) |
122     BIT_ULL(VIRTIO_NET_F_MQ) |
123     BIT_ULL(VIRTIO_F_ANY_LAYOUT) |
124     BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) |
125     /* VHOST_F_LOG_ALL is exposed by SVQ */
126     BIT_ULL(VHOST_F_LOG_ALL) |
127     BIT_ULL(VIRTIO_NET_F_HASH_REPORT) |
128     BIT_ULL(VIRTIO_NET_F_RSS) |
129     BIT_ULL(VIRTIO_NET_F_RSC_EXT) |
130     BIT_ULL(VIRTIO_NET_F_STANDBY) |
131     BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX);
132 
133 #define VHOST_VDPA_NET_CVQ_ASID 1
134 
135 VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
136 {
137     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
138     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
139     return s->vhost_net;
140 }
141 
142 static size_t vhost_vdpa_net_cvq_cmd_len(void)
143 {
144     /*
145      * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
146      * In buffer is always 1 byte, so it should fit here
147      */
148     return sizeof(struct virtio_net_ctrl_hdr) +
149            2 * sizeof(struct virtio_net_ctrl_mac) +
150            MAC_TABLE_ENTRIES * ETH_ALEN;
151 }
152 
153 static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
154 {
155     return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
156 }
157 
158 static bool vhost_vdpa_net_valid_svq_features(uint64_t features, Error **errp)
159 {
160     uint64_t invalid_dev_features =
161         features & ~vdpa_svq_device_features &
162         /* Transport are all accepted at this point */
163         ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START,
164                          VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START);
165 
166     if (invalid_dev_features) {
167         error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
168                    invalid_dev_features);
169         return false;
170     }
171 
172     return vhost_svq_valid_features(features, errp);
173 }
174 
175 static int vhost_vdpa_net_check_device_id(struct vhost_net *net)
176 {
177     uint32_t device_id;
178     int ret;
179     struct vhost_dev *hdev;
180 
181     hdev = (struct vhost_dev *)&net->dev;
182     ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id);
183     if (device_id != VIRTIO_ID_NET) {
184         return -ENOTSUP;
185     }
186     return ret;
187 }
188 
189 static int vhost_vdpa_add(NetClientState *ncs, void *be,
190                           int queue_pair_index, int nvqs)
191 {
192     VhostNetOptions options;
193     struct vhost_net *net = NULL;
194     VhostVDPAState *s;
195     int ret;
196 
197     options.backend_type = VHOST_BACKEND_TYPE_VDPA;
198     assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
199     s = DO_UPCAST(VhostVDPAState, nc, ncs);
200     options.net_backend = ncs;
201     options.opaque      = be;
202     options.busyloop_timeout = 0;
203     options.nvqs = nvqs;
204 
205     net = vhost_net_init(&options);
206     if (!net) {
207         error_report("failed to init vhost_net for queue");
208         goto err_init;
209     }
210     s->vhost_net = net;
211     ret = vhost_vdpa_net_check_device_id(net);
212     if (ret) {
213         goto err_check;
214     }
215     return 0;
216 err_check:
217     vhost_net_cleanup(net);
218     g_free(net);
219 err_init:
220     return -1;
221 }
222 
223 static void vhost_vdpa_cleanup(NetClientState *nc)
224 {
225     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
226 
227     /*
228      * If a peer NIC is attached, do not cleanup anything.
229      * Cleanup will happen as a part of qemu_cleanup() -> net_cleanup()
230      * when the guest is shutting down.
231      */
232     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
233         return;
234     }
235     munmap(s->cvq_cmd_out_buffer, vhost_vdpa_net_cvq_cmd_page_len());
236     munmap(s->status, vhost_vdpa_net_cvq_cmd_page_len());
237     if (s->vhost_net) {
238         vhost_net_cleanup(s->vhost_net);
239         g_free(s->vhost_net);
240         s->vhost_net = NULL;
241     }
242     if (s->vhost_vdpa.index != 0) {
243         return;
244     }
245     qemu_close(s->vhost_vdpa.shared->device_fd);
246     g_free(s->vhost_vdpa.shared);
247 }
248 
249 /** Dummy SetSteeringEBPF to support RSS for vhost-vdpa backend  */
250 static bool vhost_vdpa_set_steering_ebpf(NetClientState *nc, int prog_fd)
251 {
252     return true;
253 }
254 
255 static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
256 {
257     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
258 
259     return true;
260 }
261 
262 static bool vhost_vdpa_has_ufo(NetClientState *nc)
263 {
264     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
265     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
266     uint64_t features = 0;
267     features |= (1ULL << VIRTIO_NET_F_HOST_UFO);
268     features = vhost_net_get_features(s->vhost_net, features);
269     return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO));
270 
271 }
272 
273 static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc,
274                                        Error **errp)
275 {
276     const char *driver = object_class_get_name(oc);
277 
278     if (!g_str_has_prefix(driver, "virtio-net-")) {
279         error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*");
280         return false;
281     }
282 
283     return true;
284 }
285 
286 /** Dummy receive in case qemu falls back to userland tap networking */
287 static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf,
288                                   size_t size)
289 {
290     return size;
291 }
292 
293 
294 /** From any vdpa net client, get the netclient of the i-th queue pair */
295 static VhostVDPAState *vhost_vdpa_net_get_nc_vdpa(VhostVDPAState *s, int i)
296 {
297     NICState *nic = qemu_get_nic(s->nc.peer);
298     NetClientState *nc_i = qemu_get_peer(nic->ncs, i);
299 
300     return DO_UPCAST(VhostVDPAState, nc, nc_i);
301 }
302 
303 static VhostVDPAState *vhost_vdpa_net_first_nc_vdpa(VhostVDPAState *s)
304 {
305     return vhost_vdpa_net_get_nc_vdpa(s, 0);
306 }
307 
308 static void vhost_vdpa_net_log_global_enable(VhostVDPAState *s, bool enable)
309 {
310     struct vhost_vdpa *v = &s->vhost_vdpa;
311     VirtIONet *n;
312     VirtIODevice *vdev;
313     int data_queue_pairs, cvq, r;
314 
315     /* We are only called on the first data vqs and only if x-svq is not set */
316     if (s->vhost_vdpa.shadow_vqs_enabled == enable) {
317         return;
318     }
319 
320     vdev = v->dev->vdev;
321     n = VIRTIO_NET(vdev);
322     if (!n->vhost_started) {
323         return;
324     }
325 
326     data_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
327     cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ?
328                                   n->max_ncs - n->max_queue_pairs : 0;
329     v->shared->svq_switching = enable ?
330         SVQ_TSTATE_ENABLING : SVQ_TSTATE_DISABLING;
331     /*
332      * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
333      * in the future and resume the device if read-only operations between
334      * suspend and reset goes wrong.
335      */
336     vhost_net_stop(vdev, n->nic->ncs, data_queue_pairs, cvq);
337 
338     /* Start will check migration setup_or_active to configure or not SVQ */
339     r = vhost_net_start(vdev, n->nic->ncs, data_queue_pairs, cvq);
340     if (unlikely(r < 0)) {
341         error_report("unable to start vhost net: %s(%d)", g_strerror(-r), -r);
342     }
343     v->shared->svq_switching = SVQ_TSTATE_DONE;
344 }
345 
346 static int vdpa_net_migration_state_notifier(NotifierWithReturn *notifier,
347                                              MigrationEvent *e, Error **errp)
348 {
349     VhostVDPAState *s = container_of(notifier, VhostVDPAState, migration_state);
350 
351     if (e->type == MIG_EVENT_PRECOPY_SETUP) {
352         vhost_vdpa_net_log_global_enable(s, true);
353     } else if (e->type == MIG_EVENT_PRECOPY_FAILED) {
354         vhost_vdpa_net_log_global_enable(s, false);
355     }
356     return 0;
357 }
358 
359 static void vhost_vdpa_net_data_start_first(VhostVDPAState *s)
360 {
361     struct vhost_vdpa *v = &s->vhost_vdpa;
362 
363     migration_add_notifier(&s->migration_state,
364                            vdpa_net_migration_state_notifier);
365     if (v->shadow_vqs_enabled) {
366         v->shared->iova_tree = vhost_iova_tree_new(v->shared->iova_range.first,
367                                                    v->shared->iova_range.last);
368     }
369 }
370 
371 static int vhost_vdpa_net_data_start(NetClientState *nc)
372 {
373     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
374     struct vhost_vdpa *v = &s->vhost_vdpa;
375 
376     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
377 
378     if (s->always_svq || migration_is_running()) {
379         v->shadow_vqs_enabled = true;
380     } else {
381         v->shadow_vqs_enabled = false;
382     }
383 
384     if (v->index == 0) {
385         v->shared->shadow_data = v->shadow_vqs_enabled;
386         vhost_vdpa_net_data_start_first(s);
387         return 0;
388     }
389 
390     return 0;
391 }
392 
393 static int vhost_vdpa_net_data_load(NetClientState *nc)
394 {
395     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
396     struct vhost_vdpa *v = &s->vhost_vdpa;
397     bool has_cvq = v->dev->vq_index_end % 2;
398 
399     if (has_cvq) {
400         return 0;
401     }
402 
403     for (int i = 0; i < v->dev->nvqs; ++i) {
404         int ret = vhost_vdpa_set_vring_ready(v, i + v->dev->vq_index);
405         if (ret < 0) {
406             return ret;
407         }
408     }
409     return 0;
410 }
411 
412 static void vhost_vdpa_net_client_stop(NetClientState *nc)
413 {
414     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
415     struct vhost_dev *dev;
416 
417     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
418 
419     if (s->vhost_vdpa.index == 0) {
420         migration_remove_notifier(&s->migration_state);
421     }
422 
423     dev = s->vhost_vdpa.dev;
424     if (dev->vq_index + dev->nvqs == dev->vq_index_end) {
425         g_clear_pointer(&s->vhost_vdpa.shared->iova_tree,
426                         vhost_iova_tree_delete);
427     }
428 }
429 
430 static NetClientInfo net_vhost_vdpa_info = {
431         .type = NET_CLIENT_DRIVER_VHOST_VDPA,
432         .size = sizeof(VhostVDPAState),
433         .receive = vhost_vdpa_receive,
434         .start = vhost_vdpa_net_data_start,
435         .load = vhost_vdpa_net_data_load,
436         .stop = vhost_vdpa_net_client_stop,
437         .cleanup = vhost_vdpa_cleanup,
438         .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
439         .has_ufo = vhost_vdpa_has_ufo,
440         .check_peer_type = vhost_vdpa_check_peer_type,
441         .set_steering_ebpf = vhost_vdpa_set_steering_ebpf,
442 };
443 
444 static int64_t vhost_vdpa_get_vring_group(int device_fd, unsigned vq_index,
445                                           Error **errp)
446 {
447     struct vhost_vring_state state = {
448         .index = vq_index,
449     };
450     int r = ioctl(device_fd, VHOST_VDPA_GET_VRING_GROUP, &state);
451 
452     if (unlikely(r < 0)) {
453         r = -errno;
454         error_setg_errno(errp, errno, "Cannot get VQ %u group", vq_index);
455         return r;
456     }
457 
458     return state.num;
459 }
460 
461 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa *v,
462                                            unsigned vq_group,
463                                            unsigned asid_num)
464 {
465     struct vhost_vring_state asid = {
466         .index = vq_group,
467         .num = asid_num,
468     };
469     int r;
470 
471     trace_vhost_vdpa_set_address_space_id(v, vq_group, asid_num);
472 
473     r = ioctl(v->shared->device_fd, VHOST_VDPA_SET_GROUP_ASID, &asid);
474     if (unlikely(r < 0)) {
475         error_report("Can't set vq group %u asid %u, errno=%d (%s)",
476                      asid.index, asid.num, errno, g_strerror(errno));
477     }
478     return r;
479 }
480 
481 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)
482 {
483     VhostIOVATree *tree = v->shared->iova_tree;
484     DMAMap needle = {
485         /*
486          * No need to specify size or to look for more translations since
487          * this contiguous chunk was allocated by us.
488          */
489         .translated_addr = (hwaddr)(uintptr_t)addr,
490     };
491     const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle);
492     int r;
493 
494     if (unlikely(!map)) {
495         error_report("Cannot locate expected map");
496         return;
497     }
498 
499     r = vhost_vdpa_dma_unmap(v->shared, v->address_space_id, map->iova,
500                              map->size + 1);
501     if (unlikely(r != 0)) {
502         error_report("Device cannot unmap: %s(%d)", g_strerror(r), r);
503     }
504 
505     vhost_iova_tree_remove(tree, *map);
506 }
507 
508 /** Map CVQ buffer. */
509 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,
510                                   bool write)
511 {
512     DMAMap map = {};
513     int r;
514 
515     map.translated_addr = (hwaddr)(uintptr_t)buf;
516     map.size = size - 1;
517     map.perm = write ? IOMMU_RW : IOMMU_RO,
518     r = vhost_iova_tree_map_alloc(v->shared->iova_tree, &map);
519     if (unlikely(r != IOVA_OK)) {
520         error_report("Cannot map injected element");
521         return r;
522     }
523 
524     r = vhost_vdpa_dma_map(v->shared, v->address_space_id, map.iova,
525                            vhost_vdpa_net_cvq_cmd_page_len(), buf, !write);
526     if (unlikely(r < 0)) {
527         goto dma_map_err;
528     }
529 
530     return 0;
531 
532 dma_map_err:
533     vhost_iova_tree_remove(v->shared->iova_tree, map);
534     return r;
535 }
536 
537 static int vhost_vdpa_net_cvq_start(NetClientState *nc)
538 {
539     VhostVDPAState *s, *s0;
540     struct vhost_vdpa *v;
541     int64_t cvq_group;
542     int r;
543     Error *err = NULL;
544 
545     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
546 
547     s = DO_UPCAST(VhostVDPAState, nc, nc);
548     v = &s->vhost_vdpa;
549 
550     s0 = vhost_vdpa_net_first_nc_vdpa(s);
551     v->shadow_vqs_enabled = s0->vhost_vdpa.shadow_vqs_enabled;
552     s->vhost_vdpa.address_space_id = VHOST_VDPA_GUEST_PA_ASID;
553 
554     if (v->shared->shadow_data) {
555         /* SVQ is already configured for all virtqueues */
556         goto out;
557     }
558 
559     /*
560      * If we early return in these cases SVQ will not be enabled. The migration
561      * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
562      */
563     if (!vhost_vdpa_net_valid_svq_features(v->dev->features, NULL)) {
564         return 0;
565     }
566 
567     if (!s->cvq_isolated) {
568         return 0;
569     }
570 
571     cvq_group = vhost_vdpa_get_vring_group(v->shared->device_fd,
572                                            v->dev->vq_index_end - 1,
573                                            &err);
574     if (unlikely(cvq_group < 0)) {
575         error_report_err(err);
576         return cvq_group;
577     }
578 
579     r = vhost_vdpa_set_address_space_id(v, cvq_group, VHOST_VDPA_NET_CVQ_ASID);
580     if (unlikely(r < 0)) {
581         return r;
582     }
583 
584     v->shadow_vqs_enabled = true;
585     s->vhost_vdpa.address_space_id = VHOST_VDPA_NET_CVQ_ASID;
586 
587 out:
588     if (!s->vhost_vdpa.shadow_vqs_enabled) {
589         return 0;
590     }
591 
592     /*
593      * If other vhost_vdpa already have an iova_tree, reuse it for simplicity,
594      * whether CVQ shares ASID with guest or not, because:
595      * - Memory listener need access to guest's memory addresses allocated in
596      *   the IOVA tree.
597      * - There should be plenty of IOVA address space for both ASID not to
598      *   worry about collisions between them.  Guest's translations are still
599      *   validated with virtio virtqueue_pop so there is no risk for the guest
600      *   to access memory that it shouldn't.
601      *
602      * To allocate a iova tree per ASID is doable but it complicates the code
603      * and it is not worth it for the moment.
604      */
605     if (!v->shared->iova_tree) {
606         v->shared->iova_tree = vhost_iova_tree_new(v->shared->iova_range.first,
607                                                    v->shared->iova_range.last);
608     }
609 
610     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer,
611                                vhost_vdpa_net_cvq_cmd_page_len(), false);
612     if (unlikely(r < 0)) {
613         return r;
614     }
615 
616     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->status,
617                                vhost_vdpa_net_cvq_cmd_page_len(), true);
618     if (unlikely(r < 0)) {
619         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
620     }
621 
622     return r;
623 }
624 
625 static void vhost_vdpa_net_cvq_stop(NetClientState *nc)
626 {
627     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
628 
629     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
630 
631     if (s->vhost_vdpa.shadow_vqs_enabled) {
632         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
633         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status);
634     }
635 
636     vhost_vdpa_net_client_stop(nc);
637 }
638 
639 static ssize_t vhost_vdpa_net_cvq_add(VhostVDPAState *s,
640                                     const struct iovec *out_sg, size_t out_num,
641                                     const struct iovec *in_sg, size_t in_num)
642 {
643     VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
644     int r;
645 
646     r = vhost_svq_add(svq, out_sg, out_num, in_sg, in_num, NULL);
647     if (unlikely(r != 0)) {
648         if (unlikely(r == -ENOSPC)) {
649             qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n",
650                           __func__);
651         }
652     }
653 
654     return r;
655 }
656 
657 /*
658  * Convenience wrapper to poll SVQ for multiple control commands.
659  *
660  * Caller should hold the BQL when invoking this function, and should take
661  * the answer before SVQ pulls by itself when BQL is released.
662  */
663 static ssize_t vhost_vdpa_net_svq_poll(VhostVDPAState *s, size_t cmds_in_flight)
664 {
665     VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
666     return vhost_svq_poll(svq, cmds_in_flight);
667 }
668 
669 static void vhost_vdpa_net_load_cursor_reset(VhostVDPAState *s,
670                                              struct iovec *out_cursor,
671                                              struct iovec *in_cursor)
672 {
673     /* reset the cursor of the output buffer for the device */
674     out_cursor->iov_base = s->cvq_cmd_out_buffer;
675     out_cursor->iov_len = vhost_vdpa_net_cvq_cmd_page_len();
676 
677     /* reset the cursor of the in buffer for the device */
678     in_cursor->iov_base = s->status;
679     in_cursor->iov_len = vhost_vdpa_net_cvq_cmd_page_len();
680 }
681 
682 /*
683  * Poll SVQ for multiple pending control commands and check the device's ack.
684  *
685  * Caller should hold the BQL when invoking this function.
686  *
687  * @s: The VhostVDPAState
688  * @len: The length of the pending status shadow buffer
689  */
690 static ssize_t vhost_vdpa_net_svq_flush(VhostVDPAState *s, size_t len)
691 {
692     /* device uses a one-byte length ack for each control command */
693     ssize_t dev_written = vhost_vdpa_net_svq_poll(s, len);
694     if (unlikely(dev_written != len)) {
695         return -EIO;
696     }
697 
698     /* check the device's ack */
699     for (int i = 0; i < len; ++i) {
700         if (s->status[i] != VIRTIO_NET_OK) {
701             return -EIO;
702         }
703     }
704     return 0;
705 }
706 
707 static ssize_t vhost_vdpa_net_load_cmd(VhostVDPAState *s,
708                                        struct iovec *out_cursor,
709                                        struct iovec *in_cursor, uint8_t class,
710                                        uint8_t cmd, const struct iovec *data_sg,
711                                        size_t data_num)
712 {
713     const struct virtio_net_ctrl_hdr ctrl = {
714         .class = class,
715         .cmd = cmd,
716     };
717     size_t data_size = iov_size(data_sg, data_num), cmd_size;
718     struct iovec out, in;
719     ssize_t r;
720     unsigned dummy_cursor_iov_cnt;
721     VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
722 
723     assert(data_size < vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl));
724     cmd_size = sizeof(ctrl) + data_size;
725     trace_vhost_vdpa_net_load_cmd(s, class, cmd, data_num, data_size);
726     if (vhost_svq_available_slots(svq) < 2 ||
727         iov_size(out_cursor, 1) < cmd_size) {
728         /*
729          * It is time to flush all pending control commands if SVQ is full
730          * or control commands shadow buffers are full.
731          *
732          * We can poll here since we've had BQL from the time
733          * we sent the descriptor.
734          */
735         r = vhost_vdpa_net_svq_flush(s, in_cursor->iov_base -
736                                      (void *)s->status);
737         if (unlikely(r < 0)) {
738             return r;
739         }
740 
741         vhost_vdpa_net_load_cursor_reset(s, out_cursor, in_cursor);
742     }
743 
744     /* pack the CVQ command header */
745     iov_from_buf(out_cursor, 1, 0, &ctrl, sizeof(ctrl));
746     /* pack the CVQ command command-specific-data */
747     iov_to_buf(data_sg, data_num, 0,
748                out_cursor->iov_base + sizeof(ctrl), data_size);
749 
750     /* extract the required buffer from the cursor for output */
751     iov_copy(&out, 1, out_cursor, 1, 0, cmd_size);
752     /* extract the required buffer from the cursor for input */
753     iov_copy(&in, 1, in_cursor, 1, 0, sizeof(*s->status));
754 
755     r = vhost_vdpa_net_cvq_add(s, &out, 1, &in, 1);
756     if (unlikely(r < 0)) {
757         trace_vhost_vdpa_net_load_cmd_retval(s, class, cmd, r);
758         return r;
759     }
760 
761     /* iterate the cursors */
762     dummy_cursor_iov_cnt = 1;
763     iov_discard_front(&out_cursor, &dummy_cursor_iov_cnt, cmd_size);
764     dummy_cursor_iov_cnt = 1;
765     iov_discard_front(&in_cursor, &dummy_cursor_iov_cnt, sizeof(*s->status));
766 
767     return 0;
768 }
769 
770 static int vhost_vdpa_net_load_mac(VhostVDPAState *s, const VirtIONet *n,
771                                    struct iovec *out_cursor,
772                                    struct iovec *in_cursor)
773 {
774     if (virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
775         const struct iovec data = {
776             .iov_base = (void *)n->mac,
777             .iov_len = sizeof(n->mac),
778         };
779         ssize_t r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
780                                             VIRTIO_NET_CTRL_MAC,
781                                             VIRTIO_NET_CTRL_MAC_ADDR_SET,
782                                             &data, 1);
783         if (unlikely(r < 0)) {
784             return r;
785         }
786     }
787 
788     /*
789      * According to VirtIO standard, "The device MUST have an
790      * empty MAC filtering table on reset.".
791      *
792      * Therefore, there is no need to send this CVQ command if the
793      * driver also sets an empty MAC filter table, which aligns with
794      * the device's defaults.
795      *
796      * Note that the device's defaults can mismatch the driver's
797      * configuration only at live migration.
798      */
799     if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_RX) ||
800         n->mac_table.in_use == 0) {
801         return 0;
802     }
803 
804     uint32_t uni_entries = n->mac_table.first_multi,
805              uni_macs_size = uni_entries * ETH_ALEN,
806              mul_entries = n->mac_table.in_use - uni_entries,
807              mul_macs_size = mul_entries * ETH_ALEN;
808     struct virtio_net_ctrl_mac uni = {
809         .entries = cpu_to_le32(uni_entries),
810     };
811     struct virtio_net_ctrl_mac mul = {
812         .entries = cpu_to_le32(mul_entries),
813     };
814     const struct iovec data[] = {
815         {
816             .iov_base = &uni,
817             .iov_len = sizeof(uni),
818         }, {
819             .iov_base = n->mac_table.macs,
820             .iov_len = uni_macs_size,
821         }, {
822             .iov_base = &mul,
823             .iov_len = sizeof(mul),
824         }, {
825             .iov_base = &n->mac_table.macs[uni_macs_size],
826             .iov_len = mul_macs_size,
827         },
828     };
829     ssize_t r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
830                                         VIRTIO_NET_CTRL_MAC,
831                                         VIRTIO_NET_CTRL_MAC_TABLE_SET,
832                                         data, ARRAY_SIZE(data));
833     if (unlikely(r < 0)) {
834         return r;
835     }
836 
837     return 0;
838 }
839 
840 static int vhost_vdpa_net_load_rss(VhostVDPAState *s, const VirtIONet *n,
841                                    struct iovec *out_cursor,
842                                    struct iovec *in_cursor, bool do_rss)
843 {
844     struct virtio_net_rss_config cfg = {};
845     ssize_t r;
846     g_autofree uint16_t *table = NULL;
847 
848     /*
849      * According to VirtIO standard, "Initially the device has all hash
850      * types disabled and reports only VIRTIO_NET_HASH_REPORT_NONE.".
851      *
852      * Therefore, there is no need to send this CVQ command if the
853      * driver disables the all hash types, which aligns with
854      * the device's defaults.
855      *
856      * Note that the device's defaults can mismatch the driver's
857      * configuration only at live migration.
858      */
859     if (!n->rss_data.enabled ||
860         n->rss_data.hash_types == VIRTIO_NET_HASH_REPORT_NONE) {
861         return 0;
862     }
863 
864     table = g_malloc_n(n->rss_data.indirections_len,
865                        sizeof(n->rss_data.indirections_table[0]));
866     cfg.hash_types = cpu_to_le32(n->rss_data.hash_types);
867 
868     if (do_rss) {
869         /*
870          * According to VirtIO standard, "Number of entries in indirection_table
871          * is (indirection_table_mask + 1)".
872          */
873         cfg.indirection_table_mask = cpu_to_le16(n->rss_data.indirections_len -
874                                                  1);
875         cfg.unclassified_queue = cpu_to_le16(n->rss_data.default_queue);
876         for (int i = 0; i < n->rss_data.indirections_len; ++i) {
877             table[i] = cpu_to_le16(n->rss_data.indirections_table[i]);
878         }
879         cfg.max_tx_vq = cpu_to_le16(n->curr_queue_pairs);
880     } else {
881         /*
882          * According to VirtIO standard, "Field reserved MUST contain zeroes.
883          * It is defined to make the structure to match the layout of
884          * virtio_net_rss_config structure, defined in 5.1.6.5.7.".
885          *
886          * Therefore, we need to zero the fields in
887          * struct virtio_net_rss_config, which corresponds to the
888          * `reserved` field in struct virtio_net_hash_config.
889          *
890          * Note that all other fields are zeroed at their definitions,
891          * except for the `indirection_table` field, where the actual data
892          * is stored in the `table` variable to ensure compatibility
893          * with RSS case. Therefore, we need to zero the `table` variable here.
894          */
895         table[0] = 0;
896     }
897 
898     /*
899      * Considering that virtio_net_handle_rss() currently does not restore
900      * the hash key length parsed from the CVQ command sent from the guest
901      * into n->rss_data and uses the maximum key length in other code, so
902      * we also employ the maximum key length here.
903      */
904     cfg.hash_key_length = sizeof(n->rss_data.key);
905 
906     const struct iovec data[] = {
907         {
908             .iov_base = &cfg,
909             .iov_len = offsetof(struct virtio_net_rss_config,
910                                 indirection_table),
911         }, {
912             .iov_base = table,
913             .iov_len = n->rss_data.indirections_len *
914                        sizeof(n->rss_data.indirections_table[0]),
915         }, {
916             .iov_base = &cfg.max_tx_vq,
917             .iov_len = offsetof(struct virtio_net_rss_config, hash_key_data) -
918                        offsetof(struct virtio_net_rss_config, max_tx_vq),
919         }, {
920             .iov_base = (void *)n->rss_data.key,
921             .iov_len = sizeof(n->rss_data.key),
922         }
923     };
924 
925     r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
926                                 VIRTIO_NET_CTRL_MQ,
927                                 do_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG :
928                                 VIRTIO_NET_CTRL_MQ_HASH_CONFIG,
929                                 data, ARRAY_SIZE(data));
930     if (unlikely(r < 0)) {
931         return r;
932     }
933 
934     return 0;
935 }
936 
937 static int vhost_vdpa_net_load_mq(VhostVDPAState *s,
938                                   const VirtIONet *n,
939                                   struct iovec *out_cursor,
940                                   struct iovec *in_cursor)
941 {
942     struct virtio_net_ctrl_mq mq;
943     ssize_t r;
944 
945     if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_MQ)) {
946         return 0;
947     }
948 
949     trace_vhost_vdpa_net_load_mq(s, n->curr_queue_pairs);
950 
951     mq.virtqueue_pairs = cpu_to_le16(n->curr_queue_pairs);
952     const struct iovec data = {
953         .iov_base = &mq,
954         .iov_len = sizeof(mq),
955     };
956     r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
957                                 VIRTIO_NET_CTRL_MQ,
958                                 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET,
959                                 &data, 1);
960     if (unlikely(r < 0)) {
961         return r;
962     }
963 
964     if (virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_RSS)) {
965         /* load the receive-side scaling state */
966         r = vhost_vdpa_net_load_rss(s, n, out_cursor, in_cursor, true);
967         if (unlikely(r < 0)) {
968             return r;
969         }
970     } else if (virtio_vdev_has_feature(&n->parent_obj,
971                                        VIRTIO_NET_F_HASH_REPORT)) {
972         /* load the hash calculation state */
973         r = vhost_vdpa_net_load_rss(s, n, out_cursor, in_cursor, false);
974         if (unlikely(r < 0)) {
975             return r;
976         }
977     }
978 
979     return 0;
980 }
981 
982 static int vhost_vdpa_net_load_offloads(VhostVDPAState *s,
983                                         const VirtIONet *n,
984                                         struct iovec *out_cursor,
985                                         struct iovec *in_cursor)
986 {
987     uint64_t offloads;
988     ssize_t r;
989 
990     if (!virtio_vdev_has_feature(&n->parent_obj,
991                                  VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
992         return 0;
993     }
994 
995     if (n->curr_guest_offloads == virtio_net_supported_guest_offloads(n)) {
996         /*
997          * According to VirtIO standard, "Upon feature negotiation
998          * corresponding offload gets enabled to preserve
999          * backward compatibility.".
1000          *
1001          * Therefore, there is no need to send this CVQ command if the
1002          * driver also enables all supported offloads, which aligns with
1003          * the device's defaults.
1004          *
1005          * Note that the device's defaults can mismatch the driver's
1006          * configuration only at live migration.
1007          */
1008         return 0;
1009     }
1010 
1011     offloads = cpu_to_le64(n->curr_guest_offloads);
1012     const struct iovec data = {
1013         .iov_base = &offloads,
1014         .iov_len = sizeof(offloads),
1015     };
1016     r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
1017                                 VIRTIO_NET_CTRL_GUEST_OFFLOADS,
1018                                 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET,
1019                                 &data, 1);
1020     if (unlikely(r < 0)) {
1021         return r;
1022     }
1023 
1024     return 0;
1025 }
1026 
1027 static int vhost_vdpa_net_load_rx_mode(VhostVDPAState *s,
1028                                        struct iovec *out_cursor,
1029                                        struct iovec *in_cursor,
1030                                        uint8_t cmd,
1031                                        uint8_t on)
1032 {
1033     const struct iovec data = {
1034         .iov_base = &on,
1035         .iov_len = sizeof(on),
1036     };
1037     ssize_t r;
1038 
1039     r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
1040                                 VIRTIO_NET_CTRL_RX, cmd, &data, 1);
1041     if (unlikely(r < 0)) {
1042         return r;
1043     }
1044 
1045     return 0;
1046 }
1047 
1048 static int vhost_vdpa_net_load_rx(VhostVDPAState *s,
1049                                   const VirtIONet *n,
1050                                   struct iovec *out_cursor,
1051                                   struct iovec *in_cursor)
1052 {
1053     ssize_t r;
1054 
1055     if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_RX)) {
1056         return 0;
1057     }
1058 
1059     /*
1060      * According to virtio_net_reset(), device turns promiscuous mode
1061      * on by default.
1062      *
1063      * Additionally, according to VirtIO standard, "Since there are
1064      * no guarantees, it can use a hash filter or silently switch to
1065      * allmulti or promiscuous mode if it is given too many addresses.".
1066      * QEMU marks `n->mac_table.uni_overflow` if guest sets too many
1067      * non-multicast MAC addresses, indicating that promiscuous mode
1068      * should be enabled.
1069      *
1070      * Therefore, QEMU should only send this CVQ command if the
1071      * `n->mac_table.uni_overflow` is not marked and `n->promisc` is off,
1072      * which sets promiscuous mode on, different from the device's defaults.
1073      *
1074      * Note that the device's defaults can mismatch the driver's
1075      * configuration only at live migration.
1076      */
1077     if (!n->mac_table.uni_overflow && !n->promisc) {
1078         r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1079                                         VIRTIO_NET_CTRL_RX_PROMISC, 0);
1080         if (unlikely(r < 0)) {
1081             return r;
1082         }
1083     }
1084 
1085     /*
1086      * According to virtio_net_reset(), device turns all-multicast mode
1087      * off by default.
1088      *
1089      * According to VirtIO standard, "Since there are no guarantees,
1090      * it can use a hash filter or silently switch to allmulti or
1091      * promiscuous mode if it is given too many addresses.". QEMU marks
1092      * `n->mac_table.multi_overflow` if guest sets too many
1093      * non-multicast MAC addresses.
1094      *
1095      * Therefore, QEMU should only send this CVQ command if the
1096      * `n->mac_table.multi_overflow` is marked or `n->allmulti` is on,
1097      * which sets all-multicast mode on, different from the device's defaults.
1098      *
1099      * Note that the device's defaults can mismatch the driver's
1100      * configuration only at live migration.
1101      */
1102     if (n->mac_table.multi_overflow || n->allmulti) {
1103         r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1104                                         VIRTIO_NET_CTRL_RX_ALLMULTI, 1);
1105         if (unlikely(r < 0)) {
1106             return r;
1107         }
1108     }
1109 
1110     if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_RX_EXTRA)) {
1111         return 0;
1112     }
1113 
1114     /*
1115      * According to virtio_net_reset(), device turns all-unicast mode
1116      * off by default.
1117      *
1118      * Therefore, QEMU should only send this CVQ command if the driver
1119      * sets all-unicast mode on, different from the device's defaults.
1120      *
1121      * Note that the device's defaults can mismatch the driver's
1122      * configuration only at live migration.
1123      */
1124     if (n->alluni) {
1125         r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1126                                         VIRTIO_NET_CTRL_RX_ALLUNI, 1);
1127         if (r < 0) {
1128             return r;
1129         }
1130     }
1131 
1132     /*
1133      * According to virtio_net_reset(), device turns non-multicast mode
1134      * off by default.
1135      *
1136      * Therefore, QEMU should only send this CVQ command if the driver
1137      * sets non-multicast mode on, different from the device's defaults.
1138      *
1139      * Note that the device's defaults can mismatch the driver's
1140      * configuration only at live migration.
1141      */
1142     if (n->nomulti) {
1143         r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1144                                         VIRTIO_NET_CTRL_RX_NOMULTI, 1);
1145         if (r < 0) {
1146             return r;
1147         }
1148     }
1149 
1150     /*
1151      * According to virtio_net_reset(), device turns non-unicast mode
1152      * off by default.
1153      *
1154      * Therefore, QEMU should only send this CVQ command if the driver
1155      * sets non-unicast mode on, different from the device's defaults.
1156      *
1157      * Note that the device's defaults can mismatch the driver's
1158      * configuration only at live migration.
1159      */
1160     if (n->nouni) {
1161         r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1162                                         VIRTIO_NET_CTRL_RX_NOUNI, 1);
1163         if (r < 0) {
1164             return r;
1165         }
1166     }
1167 
1168     /*
1169      * According to virtio_net_reset(), device turns non-broadcast mode
1170      * off by default.
1171      *
1172      * Therefore, QEMU should only send this CVQ command if the driver
1173      * sets non-broadcast mode on, different from the device's defaults.
1174      *
1175      * Note that the device's defaults can mismatch the driver's
1176      * configuration only at live migration.
1177      */
1178     if (n->nobcast) {
1179         r = vhost_vdpa_net_load_rx_mode(s, out_cursor, in_cursor,
1180                                         VIRTIO_NET_CTRL_RX_NOBCAST, 1);
1181         if (r < 0) {
1182             return r;
1183         }
1184     }
1185 
1186     return 0;
1187 }
1188 
1189 static int vhost_vdpa_net_load_single_vlan(VhostVDPAState *s,
1190                                            const VirtIONet *n,
1191                                            struct iovec *out_cursor,
1192                                            struct iovec *in_cursor,
1193                                            uint16_t vid)
1194 {
1195     const struct iovec data = {
1196         .iov_base = &vid,
1197         .iov_len = sizeof(vid),
1198     };
1199     ssize_t r = vhost_vdpa_net_load_cmd(s, out_cursor, in_cursor,
1200                                         VIRTIO_NET_CTRL_VLAN,
1201                                         VIRTIO_NET_CTRL_VLAN_ADD,
1202                                         &data, 1);
1203     if (unlikely(r < 0)) {
1204         return r;
1205     }
1206 
1207     return 0;
1208 }
1209 
1210 static int vhost_vdpa_net_load_vlan(VhostVDPAState *s,
1211                                     const VirtIONet *n,
1212                                     struct iovec *out_cursor,
1213                                     struct iovec *in_cursor)
1214 {
1215     int r;
1216 
1217     if (!virtio_vdev_has_feature(&n->parent_obj, VIRTIO_NET_F_CTRL_VLAN)) {
1218         return 0;
1219     }
1220 
1221     for (int i = 0; i < MAX_VLAN >> 5; i++) {
1222         for (int j = 0; n->vlans[i] && j <= 0x1f; j++) {
1223             if (n->vlans[i] & (1U << j)) {
1224                 r = vhost_vdpa_net_load_single_vlan(s, n, out_cursor,
1225                                                     in_cursor, (i << 5) + j);
1226                 if (unlikely(r != 0)) {
1227                     return r;
1228                 }
1229             }
1230         }
1231     }
1232 
1233     return 0;
1234 }
1235 
1236 static int vhost_vdpa_net_cvq_load(NetClientState *nc)
1237 {
1238     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1239     struct vhost_vdpa *v = &s->vhost_vdpa;
1240     const VirtIONet *n;
1241     int r;
1242     struct iovec out_cursor, in_cursor;
1243 
1244     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1245 
1246     r = vhost_vdpa_set_vring_ready(v, v->dev->vq_index);
1247     if (unlikely(r < 0)) {
1248         return r;
1249     }
1250 
1251     if (v->shadow_vqs_enabled) {
1252         n = VIRTIO_NET(v->dev->vdev);
1253         vhost_vdpa_net_load_cursor_reset(s, &out_cursor, &in_cursor);
1254         r = vhost_vdpa_net_load_mac(s, n, &out_cursor, &in_cursor);
1255         if (unlikely(r < 0)) {
1256             return r;
1257         }
1258         r = vhost_vdpa_net_load_mq(s, n, &out_cursor, &in_cursor);
1259         if (unlikely(r)) {
1260             return r;
1261         }
1262         r = vhost_vdpa_net_load_offloads(s, n, &out_cursor, &in_cursor);
1263         if (unlikely(r)) {
1264             return r;
1265         }
1266         r = vhost_vdpa_net_load_rx(s, n, &out_cursor, &in_cursor);
1267         if (unlikely(r)) {
1268             return r;
1269         }
1270         r = vhost_vdpa_net_load_vlan(s, n, &out_cursor, &in_cursor);
1271         if (unlikely(r)) {
1272             return r;
1273         }
1274 
1275         /*
1276          * We need to poll and check all pending device's used buffers.
1277          *
1278          * We can poll here since we've had BQL from the time
1279          * we sent the descriptor.
1280          */
1281         r = vhost_vdpa_net_svq_flush(s, in_cursor.iov_base - (void *)s->status);
1282         if (unlikely(r)) {
1283             return r;
1284         }
1285     }
1286 
1287     for (int i = 0; i < v->dev->vq_index; ++i) {
1288         r = vhost_vdpa_set_vring_ready(v, i);
1289         if (unlikely(r < 0)) {
1290             return r;
1291         }
1292     }
1293 
1294     return 0;
1295 }
1296 
1297 static NetClientInfo net_vhost_vdpa_cvq_info = {
1298     .type = NET_CLIENT_DRIVER_VHOST_VDPA,
1299     .size = sizeof(VhostVDPAState),
1300     .receive = vhost_vdpa_receive,
1301     .start = vhost_vdpa_net_cvq_start,
1302     .load = vhost_vdpa_net_cvq_load,
1303     .stop = vhost_vdpa_net_cvq_stop,
1304     .cleanup = vhost_vdpa_cleanup,
1305     .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
1306     .has_ufo = vhost_vdpa_has_ufo,
1307     .check_peer_type = vhost_vdpa_check_peer_type,
1308     .set_steering_ebpf = vhost_vdpa_set_steering_ebpf,
1309 };
1310 
1311 /*
1312  * Forward the excessive VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command to
1313  * vdpa device.
1314  *
1315  * Considering that QEMU cannot send the entire filter table to the
1316  * vdpa device, it should send the VIRTIO_NET_CTRL_RX_PROMISC CVQ
1317  * command to enable promiscuous mode to receive all packets,
1318  * according to VirtIO standard, "Since there are no guarantees,
1319  * it can use a hash filter or silently switch to allmulti or
1320  * promiscuous mode if it is given too many addresses.".
1321  *
1322  * Since QEMU ignores MAC addresses beyond `MAC_TABLE_ENTRIES` and
1323  * marks `n->mac_table.x_overflow` accordingly, it should have
1324  * the same effect on the device model to receive
1325  * (`MAC_TABLE_ENTRIES` + 1) or more non-multicast MAC addresses.
1326  * The same applies to multicast MAC addresses.
1327  *
1328  * Therefore, QEMU can provide the device model with a fake
1329  * VIRTIO_NET_CTRL_MAC_TABLE_SET command with (`MAC_TABLE_ENTRIES` + 1)
1330  * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1) multicast
1331  * MAC addresses. This ensures that the device model marks
1332  * `n->mac_table.uni_overflow` and `n->mac_table.multi_overflow`,
1333  * allowing all packets to be received, which aligns with the
1334  * state of the vdpa device.
1335  */
1336 static int vhost_vdpa_net_excessive_mac_filter_cvq_add(VhostVDPAState *s,
1337                                                        VirtQueueElement *elem,
1338                                                        struct iovec *out,
1339                                                        const struct iovec *in)
1340 {
1341     struct virtio_net_ctrl_mac mac_data, *mac_ptr;
1342     struct virtio_net_ctrl_hdr *hdr_ptr;
1343     uint32_t cursor;
1344     ssize_t r;
1345     uint8_t on = 1;
1346 
1347     /* parse the non-multicast MAC address entries from CVQ command */
1348     cursor = sizeof(*hdr_ptr);
1349     r = iov_to_buf(elem->out_sg, elem->out_num, cursor,
1350                    &mac_data, sizeof(mac_data));
1351     if (unlikely(r != sizeof(mac_data))) {
1352         /*
1353          * If the CVQ command is invalid, we should simulate the vdpa device
1354          * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1355          */
1356         *s->status = VIRTIO_NET_ERR;
1357         return sizeof(*s->status);
1358     }
1359     cursor += sizeof(mac_data) + le32_to_cpu(mac_data.entries) * ETH_ALEN;
1360 
1361     /* parse the multicast MAC address entries from CVQ command */
1362     r = iov_to_buf(elem->out_sg, elem->out_num, cursor,
1363                    &mac_data, sizeof(mac_data));
1364     if (r != sizeof(mac_data)) {
1365         /*
1366          * If the CVQ command is invalid, we should simulate the vdpa device
1367          * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1368          */
1369         *s->status = VIRTIO_NET_ERR;
1370         return sizeof(*s->status);
1371     }
1372     cursor += sizeof(mac_data) + le32_to_cpu(mac_data.entries) * ETH_ALEN;
1373 
1374     /* validate the CVQ command */
1375     if (iov_size(elem->out_sg, elem->out_num) != cursor) {
1376         /*
1377          * If the CVQ command is invalid, we should simulate the vdpa device
1378          * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1379          */
1380         *s->status = VIRTIO_NET_ERR;
1381         return sizeof(*s->status);
1382     }
1383 
1384     /*
1385      * According to VirtIO standard, "Since there are no guarantees,
1386      * it can use a hash filter or silently switch to allmulti or
1387      * promiscuous mode if it is given too many addresses.".
1388      *
1389      * Therefore, considering that QEMU is unable to send the entire
1390      * filter table to the vdpa device, it should send the
1391      * VIRTIO_NET_CTRL_RX_PROMISC CVQ command to enable promiscuous mode
1392      */
1393     hdr_ptr = out->iov_base;
1394     out->iov_len = sizeof(*hdr_ptr) + sizeof(on);
1395 
1396     hdr_ptr->class = VIRTIO_NET_CTRL_RX;
1397     hdr_ptr->cmd = VIRTIO_NET_CTRL_RX_PROMISC;
1398     iov_from_buf(out, 1, sizeof(*hdr_ptr), &on, sizeof(on));
1399     r = vhost_vdpa_net_cvq_add(s, out, 1, in, 1);
1400     if (unlikely(r < 0)) {
1401         return r;
1402     }
1403 
1404     /*
1405      * We can poll here since we've had BQL from the time
1406      * we sent the descriptor.
1407      */
1408     r = vhost_vdpa_net_svq_poll(s, 1);
1409     if (unlikely(r < sizeof(*s->status))) {
1410         return r;
1411     }
1412     if (*s->status != VIRTIO_NET_OK) {
1413         return sizeof(*s->status);
1414     }
1415 
1416     /*
1417      * QEMU should also send a fake VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ
1418      * command to the device model, including (`MAC_TABLE_ENTRIES` + 1)
1419      * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1)
1420      * multicast MAC addresses.
1421      *
1422      * By doing so, the device model can mark `n->mac_table.uni_overflow`
1423      * and `n->mac_table.multi_overflow`, enabling all packets to be
1424      * received, which aligns with the state of the vdpa device.
1425      */
1426     cursor = 0;
1427     uint32_t fake_uni_entries = MAC_TABLE_ENTRIES + 1,
1428              fake_mul_entries = MAC_TABLE_ENTRIES + 1,
1429              fake_cvq_size = sizeof(struct virtio_net_ctrl_hdr) +
1430                              sizeof(mac_data) + fake_uni_entries * ETH_ALEN +
1431                              sizeof(mac_data) + fake_mul_entries * ETH_ALEN;
1432 
1433     assert(fake_cvq_size < vhost_vdpa_net_cvq_cmd_page_len());
1434     out->iov_len = fake_cvq_size;
1435 
1436     /* pack the header for fake CVQ command */
1437     hdr_ptr = out->iov_base + cursor;
1438     hdr_ptr->class = VIRTIO_NET_CTRL_MAC;
1439     hdr_ptr->cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1440     cursor += sizeof(*hdr_ptr);
1441 
1442     /*
1443      * Pack the non-multicast MAC addresses part for fake CVQ command.
1444      *
1445      * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1446      * addresses provided in CVQ command. Therefore, only the entries
1447      * field need to be prepared in the CVQ command.
1448      */
1449     mac_ptr = out->iov_base + cursor;
1450     mac_ptr->entries = cpu_to_le32(fake_uni_entries);
1451     cursor += sizeof(*mac_ptr) + fake_uni_entries * ETH_ALEN;
1452 
1453     /*
1454      * Pack the multicast MAC addresses part for fake CVQ command.
1455      *
1456      * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1457      * addresses provided in CVQ command. Therefore, only the entries
1458      * field need to be prepared in the CVQ command.
1459      */
1460     mac_ptr = out->iov_base + cursor;
1461     mac_ptr->entries = cpu_to_le32(fake_mul_entries);
1462 
1463     /*
1464      * Simulating QEMU poll a vdpa device used buffer
1465      * for VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1466      */
1467     return sizeof(*s->status);
1468 }
1469 
1470 /**
1471  * Validate and copy control virtqueue commands.
1472  *
1473  * Following QEMU guidelines, we offer a copy of the buffers to the device to
1474  * prevent TOCTOU bugs.
1475  */
1476 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
1477                                             VirtQueueElement *elem,
1478                                             void *opaque)
1479 {
1480     VhostVDPAState *s = opaque;
1481     size_t in_len;
1482     const struct virtio_net_ctrl_hdr *ctrl;
1483     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
1484     /* Out buffer sent to both the vdpa device and the device model */
1485     struct iovec out = {
1486         .iov_base = s->cvq_cmd_out_buffer,
1487     };
1488     /* in buffer used for device model */
1489     const struct iovec model_in = {
1490         .iov_base = &status,
1491         .iov_len = sizeof(status),
1492     };
1493     /* in buffer used for vdpa device */
1494     const struct iovec vdpa_in = {
1495         .iov_base = s->status,
1496         .iov_len = sizeof(*s->status),
1497     };
1498     ssize_t dev_written = -EINVAL;
1499 
1500     out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0,
1501                              s->cvq_cmd_out_buffer,
1502                              vhost_vdpa_net_cvq_cmd_page_len());
1503 
1504     ctrl = s->cvq_cmd_out_buffer;
1505     if (ctrl->class == VIRTIO_NET_CTRL_ANNOUNCE) {
1506         /*
1507          * Guest announce capability is emulated by qemu, so don't forward to
1508          * the device.
1509          */
1510         dev_written = sizeof(status);
1511         *s->status = VIRTIO_NET_OK;
1512     } else if (unlikely(ctrl->class == VIRTIO_NET_CTRL_MAC &&
1513                         ctrl->cmd == VIRTIO_NET_CTRL_MAC_TABLE_SET &&
1514                         iov_size(elem->out_sg, elem->out_num) > out.iov_len)) {
1515         /*
1516          * Due to the size limitation of the out buffer sent to the vdpa device,
1517          * which is determined by vhost_vdpa_net_cvq_cmd_page_len(), excessive
1518          * MAC addresses set by the driver for the filter table can cause
1519          * truncation of the CVQ command in QEMU. As a result, the vdpa device
1520          * rejects the flawed CVQ command.
1521          *
1522          * Therefore, QEMU must handle this situation instead of sending
1523          * the CVQ command directly.
1524          */
1525         dev_written = vhost_vdpa_net_excessive_mac_filter_cvq_add(s, elem,
1526                                                             &out, &vdpa_in);
1527         if (unlikely(dev_written < 0)) {
1528             goto out;
1529         }
1530     } else {
1531         ssize_t r;
1532         r = vhost_vdpa_net_cvq_add(s, &out, 1, &vdpa_in, 1);
1533         if (unlikely(r < 0)) {
1534             dev_written = r;
1535             goto out;
1536         }
1537 
1538         /*
1539          * We can poll here since we've had BQL from the time
1540          * we sent the descriptor.
1541          */
1542         dev_written = vhost_vdpa_net_svq_poll(s, 1);
1543     }
1544 
1545     if (unlikely(dev_written < sizeof(status))) {
1546         error_report("Insufficient written data (%zu)", dev_written);
1547         goto out;
1548     }
1549 
1550     if (*s->status != VIRTIO_NET_OK) {
1551         goto out;
1552     }
1553 
1554     status = VIRTIO_NET_ERR;
1555     virtio_net_handle_ctrl_iov(svq->vdev, &model_in, 1, &out, 1);
1556     if (status != VIRTIO_NET_OK) {
1557         error_report("Bad CVQ processing in model");
1558     }
1559 
1560 out:
1561     in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status,
1562                           sizeof(status));
1563     if (unlikely(in_len < sizeof(status))) {
1564         error_report("Bad device CVQ written length");
1565     }
1566     vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status)));
1567     /*
1568      * `elem` belongs to vhost_vdpa_net_handle_ctrl_avail() only when
1569      * the function successfully forwards the CVQ command, indicated
1570      * by a non-negative value of `dev_written`. Otherwise, it still
1571      * belongs to SVQ.
1572      * This function should only free the `elem` when it owns.
1573      */
1574     if (dev_written >= 0) {
1575         g_free(elem);
1576     }
1577     return dev_written < 0 ? dev_written : 0;
1578 }
1579 
1580 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = {
1581     .avail_handler = vhost_vdpa_net_handle_ctrl_avail,
1582 };
1583 
1584 /**
1585  * Probe if CVQ is isolated
1586  *
1587  * @device_fd         The vdpa device fd
1588  * @features          Features offered by the device.
1589  * @cvq_index         The control vq pair index
1590  *
1591  * Returns <0 in case of failure, 0 if false and 1 if true.
1592  */
1593 static int vhost_vdpa_probe_cvq_isolation(int device_fd, uint64_t features,
1594                                           int cvq_index, Error **errp)
1595 {
1596     ERRP_GUARD();
1597     uint64_t backend_features;
1598     int64_t cvq_group;
1599     uint8_t status = VIRTIO_CONFIG_S_ACKNOWLEDGE |
1600                      VIRTIO_CONFIG_S_DRIVER;
1601     int r;
1602 
1603     r = ioctl(device_fd, VHOST_GET_BACKEND_FEATURES, &backend_features);
1604     if (unlikely(r < 0)) {
1605         error_setg_errno(errp, errno, "Cannot get vdpa backend_features");
1606         return r;
1607     }
1608 
1609     if (!(backend_features & BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID))) {
1610         return 0;
1611     }
1612 
1613     r = ioctl(device_fd, VHOST_VDPA_SET_STATUS, &status);
1614     if (unlikely(r)) {
1615         error_setg_errno(errp, -r, "Cannot set device status");
1616         goto out;
1617     }
1618 
1619     r = ioctl(device_fd, VHOST_SET_FEATURES, &features);
1620     if (unlikely(r)) {
1621         error_setg_errno(errp, -r, "Cannot set features");
1622         goto out;
1623     }
1624 
1625     status |= VIRTIO_CONFIG_S_FEATURES_OK;
1626     r = ioctl(device_fd, VHOST_VDPA_SET_STATUS, &status);
1627     if (unlikely(r)) {
1628         error_setg_errno(errp, -r, "Cannot set device status");
1629         goto out;
1630     }
1631 
1632     cvq_group = vhost_vdpa_get_vring_group(device_fd, cvq_index, errp);
1633     if (unlikely(cvq_group < 0)) {
1634         if (cvq_group != -ENOTSUP) {
1635             r = cvq_group;
1636             goto out;
1637         }
1638 
1639         /*
1640          * The kernel report VHOST_BACKEND_F_IOTLB_ASID if the vdpa frontend
1641          * support ASID even if the parent driver does not.  The CVQ cannot be
1642          * isolated in this case.
1643          */
1644         error_free(*errp);
1645         *errp = NULL;
1646         r = 0;
1647         goto out;
1648     }
1649 
1650     for (int i = 0; i < cvq_index; ++i) {
1651         int64_t group = vhost_vdpa_get_vring_group(device_fd, i, errp);
1652         if (unlikely(group < 0)) {
1653             r = group;
1654             goto out;
1655         }
1656 
1657         if (group == (int64_t)cvq_group) {
1658             r = 0;
1659             goto out;
1660         }
1661     }
1662 
1663     r = 1;
1664 
1665 out:
1666     status = 0;
1667     ioctl(device_fd, VHOST_VDPA_SET_STATUS, &status);
1668     return r;
1669 }
1670 
1671 static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
1672                                        const char *device,
1673                                        const char *name,
1674                                        int vdpa_device_fd,
1675                                        int queue_pair_index,
1676                                        int nvqs,
1677                                        bool is_datapath,
1678                                        bool svq,
1679                                        struct vhost_vdpa_iova_range iova_range,
1680                                        uint64_t features,
1681                                        VhostVDPAShared *shared,
1682                                        Error **errp)
1683 {
1684     NetClientState *nc = NULL;
1685     VhostVDPAState *s;
1686     int ret = 0;
1687     assert(name);
1688     int cvq_isolated = 0;
1689 
1690     if (is_datapath) {
1691         nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device,
1692                                  name);
1693     } else {
1694         cvq_isolated = vhost_vdpa_probe_cvq_isolation(vdpa_device_fd, features,
1695                                                       queue_pair_index * 2,
1696                                                       errp);
1697         if (unlikely(cvq_isolated < 0)) {
1698             return NULL;
1699         }
1700 
1701         nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer,
1702                                          device, name);
1703     }
1704     qemu_set_info_str(nc, TYPE_VHOST_VDPA);
1705     s = DO_UPCAST(VhostVDPAState, nc, nc);
1706 
1707     s->vhost_vdpa.index = queue_pair_index;
1708     s->always_svq = svq;
1709     s->migration_state.notify = NULL;
1710     s->vhost_vdpa.shadow_vqs_enabled = svq;
1711     if (queue_pair_index == 0) {
1712         vhost_vdpa_net_valid_svq_features(features,
1713                                           &s->vhost_vdpa.migration_blocker);
1714         s->vhost_vdpa.shared = g_new0(VhostVDPAShared, 1);
1715         s->vhost_vdpa.shared->device_fd = vdpa_device_fd;
1716         s->vhost_vdpa.shared->iova_range = iova_range;
1717         s->vhost_vdpa.shared->shadow_data = svq;
1718     } else if (!is_datapath) {
1719         s->cvq_cmd_out_buffer = mmap(NULL, vhost_vdpa_net_cvq_cmd_page_len(),
1720                                      PROT_READ | PROT_WRITE,
1721                                      MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1722         s->status = mmap(NULL, vhost_vdpa_net_cvq_cmd_page_len(),
1723                          PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,
1724                          -1, 0);
1725 
1726         s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops;
1727         s->vhost_vdpa.shadow_vq_ops_opaque = s;
1728         s->cvq_isolated = cvq_isolated;
1729     }
1730     if (queue_pair_index != 0) {
1731         s->vhost_vdpa.shared = shared;
1732     }
1733 
1734     ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);
1735     if (ret) {
1736         qemu_del_net_client(nc);
1737         return NULL;
1738     }
1739 
1740     return nc;
1741 }
1742 
1743 static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp)
1744 {
1745     int ret = ioctl(fd, VHOST_GET_FEATURES, features);
1746     if (unlikely(ret < 0)) {
1747         error_setg_errno(errp, errno,
1748                          "Fail to query features from vhost-vDPA device");
1749     }
1750     return ret;
1751 }
1752 
1753 static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features,
1754                                           int *has_cvq, Error **errp)
1755 {
1756     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
1757     g_autofree struct vhost_vdpa_config *config = NULL;
1758     __virtio16 *max_queue_pairs;
1759     int ret;
1760 
1761     if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) {
1762         *has_cvq = 1;
1763     } else {
1764         *has_cvq = 0;
1765     }
1766 
1767     if (features & (1 << VIRTIO_NET_F_MQ)) {
1768         config = g_malloc0(config_size + sizeof(*max_queue_pairs));
1769         config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs);
1770         config->len = sizeof(*max_queue_pairs);
1771 
1772         ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config);
1773         if (ret) {
1774             error_setg(errp, "Fail to get config from vhost-vDPA device");
1775             return -ret;
1776         }
1777 
1778         max_queue_pairs = (__virtio16 *)&config->buf;
1779 
1780         return lduw_le_p(max_queue_pairs);
1781     }
1782 
1783     return 1;
1784 }
1785 
1786 int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
1787                         NetClientState *peer, Error **errp)
1788 {
1789     ERRP_GUARD();
1790     const NetdevVhostVDPAOptions *opts;
1791     uint64_t features;
1792     int vdpa_device_fd;
1793     g_autofree NetClientState **ncs = NULL;
1794     struct vhost_vdpa_iova_range iova_range;
1795     NetClientState *nc;
1796     int queue_pairs, r, i = 0, has_cvq = 0;
1797 
1798     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1799     opts = &netdev->u.vhost_vdpa;
1800     if (!opts->vhostdev && !opts->vhostfd) {
1801         error_setg(errp,
1802                    "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
1803         return -1;
1804     }
1805 
1806     if (opts->vhostdev && opts->vhostfd) {
1807         error_setg(errp,
1808                    "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
1809         return -1;
1810     }
1811 
1812     if (opts->vhostdev) {
1813         vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
1814         if (vdpa_device_fd == -1) {
1815             return -errno;
1816         }
1817     } else {
1818         /* has_vhostfd */
1819         vdpa_device_fd = monitor_fd_param(monitor_cur(), opts->vhostfd, errp);
1820         if (vdpa_device_fd == -1) {
1821             error_prepend(errp, "vhost-vdpa: unable to parse vhostfd: ");
1822             return -1;
1823         }
1824     }
1825 
1826     r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
1827     if (unlikely(r < 0)) {
1828         goto err;
1829     }
1830 
1831     queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
1832                                                  &has_cvq, errp);
1833     if (queue_pairs < 0) {
1834         qemu_close(vdpa_device_fd);
1835         return queue_pairs;
1836     }
1837 
1838     r = vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);
1839     if (unlikely(r < 0)) {
1840         error_setg(errp, "vhost-vdpa: get iova range failed: %s",
1841                    strerror(-r));
1842         goto err;
1843     }
1844 
1845     if (opts->x_svq && !vhost_vdpa_net_valid_svq_features(features, errp)) {
1846         goto err;
1847     }
1848 
1849     ncs = g_malloc0(sizeof(*ncs) * queue_pairs);
1850 
1851     for (i = 0; i < queue_pairs; i++) {
1852         VhostVDPAShared *shared = NULL;
1853 
1854         if (i) {
1855             shared = DO_UPCAST(VhostVDPAState, nc, ncs[0])->vhost_vdpa.shared;
1856         }
1857         ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
1858                                      vdpa_device_fd, i, 2, true, opts->x_svq,
1859                                      iova_range, features, shared, errp);
1860         if (!ncs[i])
1861             goto err;
1862     }
1863 
1864     if (has_cvq) {
1865         VhostVDPAState *s0 = DO_UPCAST(VhostVDPAState, nc, ncs[0]);
1866         VhostVDPAShared *shared = s0->vhost_vdpa.shared;
1867 
1868         nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
1869                                  vdpa_device_fd, i, 1, false,
1870                                  opts->x_svq, iova_range, features, shared,
1871                                  errp);
1872         if (!nc)
1873             goto err;
1874     }
1875 
1876     return 0;
1877 
1878 err:
1879     if (i) {
1880         for (i--; i >= 0; i--) {
1881             qemu_del_net_client(ncs[i]);
1882         }
1883     }
1884 
1885     qemu_close(vdpa_device_fd);
1886 
1887     return -1;
1888 }
1889