]> git.proxmox.com Git - mirror_qemu.git/blame - net/vhost-vdpa.c
vdpa: allocate SVQ array unconditionally
[mirror_qemu.git] / net / vhost-vdpa.c
CommitLineData
1e0a84ea
CL
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"
bd907ae4 14#include "hw/virtio/virtio-net.h"
1e0a84ea
CL
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"
bd907ae4
EP
20#include "qemu/log.h"
21#include "qemu/memalign.h"
1e0a84ea
CL
22#include "qemu/option.h"
23#include "qapi/error.h"
40237840 24#include <linux/vhost.h>
1e0a84ea
CL
25#include <sys/ioctl.h>
26#include <err.h>
27#include "standard-headers/linux/virtio_net.h"
28#include "monitor/monitor.h"
29#include "hw/virtio/vhost.h"
30
31/* Todo:need to add the multiqueue support here */
32typedef struct VhostVDPAState {
33 NetClientState nc;
34 struct vhost_vdpa vhost_vdpa;
35 VHostNetState *vhost_net;
2df4dd31
EP
36
37 /* Control commands shadow buffers */
17fb889f
EP
38 void *cvq_cmd_out_buffer;
39 virtio_net_ctrl_ack *status;
40
1e0a84ea
CL
41 bool started;
42} VhostVDPAState;
43
44const int vdpa_feature_bits[] = {
45 VIRTIO_F_NOTIFY_ON_EMPTY,
46 VIRTIO_RING_F_INDIRECT_DESC,
47 VIRTIO_RING_F_EVENT_IDX,
48 VIRTIO_F_ANY_LAYOUT,
49 VIRTIO_F_VERSION_1,
50 VIRTIO_NET_F_CSUM,
51 VIRTIO_NET_F_GUEST_CSUM,
52 VIRTIO_NET_F_GSO,
53 VIRTIO_NET_F_GUEST_TSO4,
54 VIRTIO_NET_F_GUEST_TSO6,
55 VIRTIO_NET_F_GUEST_ECN,
56 VIRTIO_NET_F_GUEST_UFO,
57 VIRTIO_NET_F_HOST_TSO4,
58 VIRTIO_NET_F_HOST_TSO6,
59 VIRTIO_NET_F_HOST_ECN,
60 VIRTIO_NET_F_HOST_UFO,
61 VIRTIO_NET_F_MRG_RXBUF,
62 VIRTIO_NET_F_MTU,
40237840
JW
63 VIRTIO_NET_F_CTRL_RX,
64 VIRTIO_NET_F_CTRL_RX_EXTRA,
65 VIRTIO_NET_F_CTRL_VLAN,
40237840
JW
66 VIRTIO_NET_F_CTRL_MAC_ADDR,
67 VIRTIO_NET_F_RSS,
68 VIRTIO_NET_F_MQ,
69 VIRTIO_NET_F_CTRL_VQ,
1e0a84ea
CL
70 VIRTIO_F_IOMMU_PLATFORM,
71 VIRTIO_F_RING_PACKED,
562a7d23 72 VIRTIO_F_RING_RESET,
0145c393
AM
73 VIRTIO_NET_F_RSS,
74 VIRTIO_NET_F_HASH_REPORT,
1e0a84ea 75 VIRTIO_NET_F_GUEST_ANNOUNCE,
9aa47edd 76 VIRTIO_NET_F_STATUS,
1e0a84ea
CL
77 VHOST_INVALID_FEATURE_BIT
78};
79
1576dbb5
EP
80/** Supported device specific feature bits with SVQ */
81static const uint64_t vdpa_svq_device_features =
82 BIT_ULL(VIRTIO_NET_F_CSUM) |
83 BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) |
84 BIT_ULL(VIRTIO_NET_F_MTU) |
85 BIT_ULL(VIRTIO_NET_F_MAC) |
86 BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) |
87 BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) |
88 BIT_ULL(VIRTIO_NET_F_GUEST_ECN) |
89 BIT_ULL(VIRTIO_NET_F_GUEST_UFO) |
90 BIT_ULL(VIRTIO_NET_F_HOST_TSO4) |
91 BIT_ULL(VIRTIO_NET_F_HOST_TSO6) |
92 BIT_ULL(VIRTIO_NET_F_HOST_ECN) |
93 BIT_ULL(VIRTIO_NET_F_HOST_UFO) |
94 BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) |
95 BIT_ULL(VIRTIO_NET_F_STATUS) |
96 BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
72b99a87 97 BIT_ULL(VIRTIO_NET_F_MQ) |
1576dbb5
EP
98 BIT_ULL(VIRTIO_F_ANY_LAYOUT) |
99 BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) |
100 BIT_ULL(VIRTIO_NET_F_RSC_EXT) |
101 BIT_ULL(VIRTIO_NET_F_STANDBY);
102
1e0a84ea
CL
103VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
104{
105 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
106 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
107 return s->vhost_net;
108}
109
36e46472
EP
110static bool vhost_vdpa_net_valid_svq_features(uint64_t features, Error **errp)
111{
112 uint64_t invalid_dev_features =
113 features & ~vdpa_svq_device_features &
114 /* Transport are all accepted at this point */
115 ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START,
116 VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START);
117
118 if (invalid_dev_features) {
119 error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
120 invalid_dev_features);
258a0394 121 return false;
36e46472
EP
122 }
123
258a0394 124 return vhost_svq_valid_features(features, errp);
36e46472
EP
125}
126
1e0a84ea
CL
127static int vhost_vdpa_net_check_device_id(struct vhost_net *net)
128{
129 uint32_t device_id;
130 int ret;
131 struct vhost_dev *hdev;
132
133 hdev = (struct vhost_dev *)&net->dev;
134 ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id);
135 if (device_id != VIRTIO_ID_NET) {
136 return -ENOTSUP;
137 }
138 return ret;
139}
140
40237840
JW
141static int vhost_vdpa_add(NetClientState *ncs, void *be,
142 int queue_pair_index, int nvqs)
1e0a84ea
CL
143{
144 VhostNetOptions options;
145 struct vhost_net *net = NULL;
146 VhostVDPAState *s;
147 int ret;
148
149 options.backend_type = VHOST_BACKEND_TYPE_VDPA;
150 assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
151 s = DO_UPCAST(VhostVDPAState, nc, ncs);
152 options.net_backend = ncs;
153 options.opaque = be;
154 options.busyloop_timeout = 0;
40237840 155 options.nvqs = nvqs;
1e0a84ea
CL
156
157 net = vhost_net_init(&options);
158 if (!net) {
159 error_report("failed to init vhost_net for queue");
a97ef87a 160 goto err_init;
1e0a84ea 161 }
1e0a84ea
CL
162 s->vhost_net = net;
163 ret = vhost_vdpa_net_check_device_id(net);
164 if (ret) {
a97ef87a 165 goto err_check;
1e0a84ea
CL
166 }
167 return 0;
a97ef87a
JW
168err_check:
169 vhost_net_cleanup(net);
170 g_free(net);
171err_init:
1e0a84ea
CL
172 return -1;
173}
174
175static void vhost_vdpa_cleanup(NetClientState *nc)
176{
177 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1576dbb5 178 struct vhost_dev *dev = &s->vhost_net->dev;
1e0a84ea 179
2df4dd31 180 qemu_vfree(s->cvq_cmd_out_buffer);
17fb889f 181 qemu_vfree(s->status);
1576dbb5
EP
182 if (dev->vq_index + dev->nvqs == dev->vq_index_end) {
183 g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete);
184 }
1e0a84ea
CL
185 if (s->vhost_net) {
186 vhost_net_cleanup(s->vhost_net);
187 g_free(s->vhost_net);
188 s->vhost_net = NULL;
189 }
57b3a7d8
CL
190 if (s->vhost_vdpa.device_fd >= 0) {
191 qemu_close(s->vhost_vdpa.device_fd);
192 s->vhost_vdpa.device_fd = -1;
193 }
1e0a84ea
CL
194}
195
196static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
197{
198 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
199
200 return true;
201}
202
203static bool vhost_vdpa_has_ufo(NetClientState *nc)
204{
205 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
206 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
207 uint64_t features = 0;
208 features |= (1ULL << VIRTIO_NET_F_HOST_UFO);
209 features = vhost_net_get_features(s->vhost_net, features);
210 return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO));
211
212}
213
ee8a1c63
KW
214static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc,
215 Error **errp)
216{
217 const char *driver = object_class_get_name(oc);
218
219 if (!g_str_has_prefix(driver, "virtio-net-")) {
220 error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*");
221 return false;
222 }
223
224 return true;
225}
226
846a1e85
EP
227/** Dummy receive in case qemu falls back to userland tap networking */
228static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf,
229 size_t size)
230{
bc5add1d 231 return size;
846a1e85
EP
232}
233
1e0a84ea
CL
234static NetClientInfo net_vhost_vdpa_info = {
235 .type = NET_CLIENT_DRIVER_VHOST_VDPA,
236 .size = sizeof(VhostVDPAState),
846a1e85 237 .receive = vhost_vdpa_receive,
1e0a84ea
CL
238 .cleanup = vhost_vdpa_cleanup,
239 .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
240 .has_ufo = vhost_vdpa_has_ufo,
ee8a1c63 241 .check_peer_type = vhost_vdpa_check_peer_type,
1e0a84ea
CL
242};
243
2df4dd31
EP
244static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)
245{
246 VhostIOVATree *tree = v->iova_tree;
247 DMAMap needle = {
248 /*
249 * No need to specify size or to look for more translations since
250 * this contiguous chunk was allocated by us.
251 */
252 .translated_addr = (hwaddr)(uintptr_t)addr,
253 };
254 const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle);
255 int r;
256
257 if (unlikely(!map)) {
258 error_report("Cannot locate expected map");
259 return;
260 }
261
262 r = vhost_vdpa_dma_unmap(v, map->iova, map->size + 1);
263 if (unlikely(r != 0)) {
264 error_report("Device cannot unmap: %s(%d)", g_strerror(r), r);
265 }
266
69292a8e 267 vhost_iova_tree_remove(tree, *map);
2df4dd31
EP
268}
269
270static size_t vhost_vdpa_net_cvq_cmd_len(void)
271{
272 /*
273 * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
274 * In buffer is always 1 byte, so it should fit here
275 */
276 return sizeof(struct virtio_net_ctrl_hdr) +
277 2 * sizeof(struct virtio_net_ctrl_mac) +
278 MAC_TABLE_ENTRIES * ETH_ALEN;
279}
280
281static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
282{
283 return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
284}
285
7a7f87e9
EP
286/** Map CVQ buffer. */
287static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,
288 bool write)
2df4dd31
EP
289{
290 DMAMap map = {};
291 int r;
292
2df4dd31 293 map.translated_addr = (hwaddr)(uintptr_t)buf;
7a7f87e9 294 map.size = size - 1;
2df4dd31
EP
295 map.perm = write ? IOMMU_RW : IOMMU_RO,
296 r = vhost_iova_tree_map_alloc(v->iova_tree, &map);
297 if (unlikely(r != IOVA_OK)) {
298 error_report("Cannot map injected element");
7a7f87e9 299 return r;
2df4dd31
EP
300 }
301
302 r = vhost_vdpa_dma_map(v, map.iova, vhost_vdpa_net_cvq_cmd_page_len(), buf,
303 !write);
304 if (unlikely(r < 0)) {
305 goto dma_map_err;
306 }
307
7a7f87e9 308 return 0;
2df4dd31
EP
309
310dma_map_err:
69292a8e 311 vhost_iova_tree_remove(v->iova_tree, map);
7a7f87e9 312 return r;
2df4dd31
EP
313}
314
7a7f87e9 315static int vhost_vdpa_net_cvq_start(NetClientState *nc)
2df4dd31 316{
7a7f87e9
EP
317 VhostVDPAState *s;
318 int r;
2df4dd31 319
7a7f87e9
EP
320 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
321
322 s = DO_UPCAST(VhostVDPAState, nc, nc);
323 if (!s->vhost_vdpa.shadow_vqs_enabled) {
324 return 0;
2df4dd31
EP
325 }
326
7a7f87e9
EP
327 r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer,
328 vhost_vdpa_net_cvq_cmd_page_len(), false);
329 if (unlikely(r < 0)) {
330 return r;
331 }
332
17fb889f 333 r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->status,
7a7f87e9
EP
334 vhost_vdpa_net_cvq_cmd_page_len(), true);
335 if (unlikely(r < 0)) {
2df4dd31 336 vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
2df4dd31
EP
337 }
338
7a7f87e9
EP
339 return r;
340}
341
342static void vhost_vdpa_net_cvq_stop(NetClientState *nc)
343{
344 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
345
346 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
347
348 if (s->vhost_vdpa.shadow_vqs_enabled) {
349 vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
17fb889f 350 vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status);
7a7f87e9 351 }
2df4dd31
EP
352}
353
be4278b6
EP
354static ssize_t vhost_vdpa_net_cvq_add(VhostVDPAState *s, size_t out_len,
355 size_t in_len)
356{
357 /* Buffers for the device */
358 const struct iovec out = {
359 .iov_base = s->cvq_cmd_out_buffer,
360 .iov_len = out_len,
361 };
362 const struct iovec in = {
17fb889f 363 .iov_base = s->status,
be4278b6
EP
364 .iov_len = sizeof(virtio_net_ctrl_ack),
365 };
366 VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
367 int r;
368
369 r = vhost_svq_add(svq, &out, 1, &in, 1, NULL);
370 if (unlikely(r != 0)) {
371 if (unlikely(r == -ENOSPC)) {
372 qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n",
373 __func__);
374 }
375 return r;
376 }
377
378 /*
379 * We can poll here since we've had BQL from the time we sent the
380 * descriptor. Also, we need to take the answer before SVQ pulls by itself,
381 * when BQL is released
382 */
383 return vhost_svq_poll(svq);
384}
385
f73c0c43
EP
386static ssize_t vhost_vdpa_net_load_cmd(VhostVDPAState *s, uint8_t class,
387 uint8_t cmd, const void *data,
388 size_t data_size)
389{
390 const struct virtio_net_ctrl_hdr ctrl = {
391 .class = class,
392 .cmd = cmd,
393 };
394
395 assert(data_size < vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl));
396
397 memcpy(s->cvq_cmd_out_buffer, &ctrl, sizeof(ctrl));
398 memcpy(s->cvq_cmd_out_buffer + sizeof(ctrl), data, data_size);
399
400 return vhost_vdpa_net_cvq_add(s, sizeof(ctrl) + data_size,
401 sizeof(virtio_net_ctrl_ack));
402}
403
404static int vhost_vdpa_net_load_mac(VhostVDPAState *s, const VirtIONet *n)
405{
406 uint64_t features = n->parent_obj.guest_features;
407 if (features & BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR)) {
408 ssize_t dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MAC,
409 VIRTIO_NET_CTRL_MAC_ADDR_SET,
410 n->mac, sizeof(n->mac));
411 if (unlikely(dev_written < 0)) {
412 return dev_written;
413 }
414
415 return *s->status != VIRTIO_NET_OK;
416 }
417
418 return 0;
419}
420
f64c7cda
EP
421static int vhost_vdpa_net_load_mq(VhostVDPAState *s,
422 const VirtIONet *n)
423{
424 struct virtio_net_ctrl_mq mq;
425 uint64_t features = n->parent_obj.guest_features;
426 ssize_t dev_written;
427
428 if (!(features & BIT_ULL(VIRTIO_NET_F_MQ))) {
429 return 0;
430 }
431
432 mq.virtqueue_pairs = cpu_to_le16(n->curr_queue_pairs);
433 dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MQ,
434 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &mq,
435 sizeof(mq));
436 if (unlikely(dev_written < 0)) {
437 return dev_written;
438 }
439
440 return *s->status != VIRTIO_NET_OK;
441}
442
dd036d8d
EP
443static int vhost_vdpa_net_load(NetClientState *nc)
444{
445 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
f73c0c43 446 struct vhost_vdpa *v = &s->vhost_vdpa;
dd036d8d 447 const VirtIONet *n;
f73c0c43 448 int r;
dd036d8d
EP
449
450 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
451
452 if (!v->shadow_vqs_enabled) {
453 return 0;
454 }
455
456 n = VIRTIO_NET(v->dev->vdev);
f73c0c43
EP
457 r = vhost_vdpa_net_load_mac(s, n);
458 if (unlikely(r < 0)) {
459 return r;
dd036d8d 460 }
f64c7cda
EP
461 r = vhost_vdpa_net_load_mq(s, n);
462 if (unlikely(r)) {
463 return r;
464 }
dd036d8d
EP
465
466 return 0;
467}
468
f8972b56
EP
469static NetClientInfo net_vhost_vdpa_cvq_info = {
470 .type = NET_CLIENT_DRIVER_VHOST_VDPA,
471 .size = sizeof(VhostVDPAState),
472 .receive = vhost_vdpa_receive,
7a7f87e9 473 .start = vhost_vdpa_net_cvq_start,
dd036d8d 474 .load = vhost_vdpa_net_load,
7a7f87e9 475 .stop = vhost_vdpa_net_cvq_stop,
f8972b56
EP
476 .cleanup = vhost_vdpa_cleanup,
477 .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
478 .has_ufo = vhost_vdpa_has_ufo,
479 .check_peer_type = vhost_vdpa_check_peer_type,
480};
481
2df4dd31
EP
482/**
483 * Validate and copy control virtqueue commands.
484 *
485 * Following QEMU guidelines, we offer a copy of the buffers to the device to
486 * prevent TOCTOU bugs.
bd907ae4
EP
487 */
488static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
489 VirtQueueElement *elem,
490 void *opaque)
491{
2df4dd31 492 VhostVDPAState *s = opaque;
be4278b6 493 size_t in_len;
bd907ae4 494 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
7a7f87e9
EP
495 /* Out buffer sent to both the vdpa device and the device model */
496 struct iovec out = {
497 .iov_base = s->cvq_cmd_out_buffer,
498 };
2df4dd31
EP
499 /* in buffer used for device model */
500 const struct iovec in = {
501 .iov_base = &status,
502 .iov_len = sizeof(status),
503 };
be4278b6 504 ssize_t dev_written = -EINVAL;
2df4dd31 505
7a7f87e9
EP
506 out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0,
507 s->cvq_cmd_out_buffer,
508 vhost_vdpa_net_cvq_cmd_len());
be4278b6
EP
509 dev_written = vhost_vdpa_net_cvq_add(s, out.iov_len, sizeof(status));
510 if (unlikely(dev_written < 0)) {
bd907ae4
EP
511 goto out;
512 }
513
bd907ae4
EP
514 if (unlikely(dev_written < sizeof(status))) {
515 error_report("Insufficient written data (%zu)", dev_written);
2df4dd31
EP
516 goto out;
517 }
518
17fb889f 519 if (*s->status != VIRTIO_NET_OK) {
be4278b6 520 return VIRTIO_NET_ERR;
2df4dd31
EP
521 }
522
523 status = VIRTIO_NET_ERR;
7a7f87e9 524 virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, &out, 1);
2df4dd31
EP
525 if (status != VIRTIO_NET_OK) {
526 error_report("Bad CVQ processing in model");
bd907ae4
EP
527 }
528
529out:
530 in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status,
531 sizeof(status));
532 if (unlikely(in_len < sizeof(status))) {
533 error_report("Bad device CVQ written length");
534 }
535 vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status)));
536 g_free(elem);
be4278b6 537 return dev_written < 0 ? dev_written : 0;
bd907ae4
EP
538}
539
540static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = {
541 .avail_handler = vhost_vdpa_net_handle_ctrl_avail,
542};
543
654790b6 544static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
a585fad2
EP
545 const char *device,
546 const char *name,
547 int vdpa_device_fd,
548 int queue_pair_index,
549 int nvqs,
550 bool is_datapath,
551 bool svq,
552 struct vhost_vdpa_iova_range iova_range,
553 VhostIOVATree *iova_tree)
1e0a84ea
CL
554{
555 NetClientState *nc = NULL;
556 VhostVDPAState *s;
1e0a84ea
CL
557 int ret = 0;
558 assert(name);
40237840
JW
559 if (is_datapath) {
560 nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device,
561 name);
562 } else {
f8972b56 563 nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer,
40237840
JW
564 device, name);
565 }
53b85d95 566 qemu_set_info_str(nc, TYPE_VHOST_VDPA);
1e0a84ea 567 s = DO_UPCAST(VhostVDPAState, nc, nc);
7327813d 568
1e0a84ea 569 s->vhost_vdpa.device_fd = vdpa_device_fd;
40237840 570 s->vhost_vdpa.index = queue_pair_index;
1576dbb5 571 s->vhost_vdpa.shadow_vqs_enabled = svq;
a585fad2 572 s->vhost_vdpa.iova_range = iova_range;
1576dbb5 573 s->vhost_vdpa.iova_tree = iova_tree;
bd907ae4 574 if (!is_datapath) {
2df4dd31
EP
575 s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(),
576 vhost_vdpa_net_cvq_cmd_page_len());
577 memset(s->cvq_cmd_out_buffer, 0, vhost_vdpa_net_cvq_cmd_page_len());
17fb889f
EP
578 s->status = qemu_memalign(qemu_real_host_page_size(),
579 vhost_vdpa_net_cvq_cmd_page_len());
580 memset(s->status, 0, vhost_vdpa_net_cvq_cmd_page_len());
2df4dd31 581
bd907ae4
EP
582 s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops;
583 s->vhost_vdpa.shadow_vq_ops_opaque = s;
584 }
40237840 585 ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);
74af5eec 586 if (ret) {
74af5eec 587 qemu_del_net_client(nc);
654790b6 588 return NULL;
74af5eec 589 }
654790b6 590 return nc;
1e0a84ea
CL
591}
592
1576dbb5
EP
593static int vhost_vdpa_get_iova_range(int fd,
594 struct vhost_vdpa_iova_range *iova_range)
595{
596 int ret = ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range);
597
598 return ret < 0 ? -errno : 0;
599}
600
8170ab3f
EP
601static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp)
602{
603 int ret = ioctl(fd, VHOST_GET_FEATURES, features);
604 if (unlikely(ret < 0)) {
605 error_setg_errno(errp, errno,
606 "Fail to query features from vhost-vDPA device");
607 }
608 return ret;
609}
610
611static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features,
612 int *has_cvq, Error **errp)
40237840
JW
613{
614 unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
cd523a41 615 g_autofree struct vhost_vdpa_config *config = NULL;
40237840 616 __virtio16 *max_queue_pairs;
40237840
JW
617 int ret;
618
40237840
JW
619 if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) {
620 *has_cvq = 1;
621 } else {
622 *has_cvq = 0;
623 }
624
625 if (features & (1 << VIRTIO_NET_F_MQ)) {
626 config = g_malloc0(config_size + sizeof(*max_queue_pairs));
627 config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs);
628 config->len = sizeof(*max_queue_pairs);
629
630 ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config);
631 if (ret) {
632 error_setg(errp, "Fail to get config from vhost-vDPA device");
633 return -ret;
634 }
635
636 max_queue_pairs = (__virtio16 *)&config->buf;
637
638 return lduw_le_p(max_queue_pairs);
639 }
640
641 return 1;
642}
643
1e0a84ea
CL
644int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
645 NetClientState *peer, Error **errp)
646{
647 const NetdevVhostVDPAOptions *opts;
8170ab3f 648 uint64_t features;
654790b6 649 int vdpa_device_fd;
eb3cb751 650 g_autofree NetClientState **ncs = NULL;
1576dbb5 651 g_autoptr(VhostIOVATree) iova_tree = NULL;
a585fad2 652 struct vhost_vdpa_iova_range iova_range;
eb3cb751 653 NetClientState *nc;
aed5da45 654 int queue_pairs, r, i = 0, has_cvq = 0;
1e0a84ea
CL
655
656 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
657 opts = &netdev->u.vhost_vdpa;
7480874a 658 if (!opts->vhostdev && !opts->vhostfd) {
8801ccd0
SWL
659 error_setg(errp,
660 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
c8295404
EP
661 return -1;
662 }
7327813d 663
7480874a 664 if (opts->vhostdev && opts->vhostfd) {
8801ccd0
SWL
665 error_setg(errp,
666 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
667 return -1;
668 }
669
7480874a 670 if (opts->vhostdev) {
8801ccd0
SWL
671 vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
672 if (vdpa_device_fd == -1) {
673 return -errno;
674 }
5107fd3e
PM
675 } else {
676 /* has_vhostfd */
8801ccd0
SWL
677 vdpa_device_fd = monitor_fd_param(monitor_cur(), opts->vhostfd, errp);
678 if (vdpa_device_fd == -1) {
679 error_prepend(errp, "vhost-vdpa: unable to parse vhostfd: ");
680 return -1;
681 }
7327813d
JW
682 }
683
8170ab3f
EP
684 r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
685 if (unlikely(r < 0)) {
aed5da45 686 goto err;
8170ab3f
EP
687 }
688
689 queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
40237840
JW
690 &has_cvq, errp);
691 if (queue_pairs < 0) {
7327813d 692 qemu_close(vdpa_device_fd);
40237840
JW
693 return queue_pairs;
694 }
695
a585fad2 696 vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);
1576dbb5 697 if (opts->x_svq) {
36e46472 698 if (!vhost_vdpa_net_valid_svq_features(features, errp)) {
1576dbb5
EP
699 goto err_svq;
700 }
701
1576dbb5
EP
702 iova_tree = vhost_iova_tree_new(iova_range.first, iova_range.last);
703 }
704
40237840
JW
705 ncs = g_malloc0(sizeof(*ncs) * queue_pairs);
706
707 for (i = 0; i < queue_pairs; i++) {
708 ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
1576dbb5 709 vdpa_device_fd, i, 2, true, opts->x_svq,
a585fad2 710 iova_range, iova_tree);
40237840
JW
711 if (!ncs[i])
712 goto err;
7327813d
JW
713 }
714
40237840
JW
715 if (has_cvq) {
716 nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
1576dbb5 717 vdpa_device_fd, i, 1, false,
a585fad2 718 opts->x_svq, iova_range, iova_tree);
40237840
JW
719 if (!nc)
720 goto err;
721 }
722
1576dbb5
EP
723 /* iova_tree ownership belongs to last NetClientState */
724 g_steal_pointer(&iova_tree);
654790b6 725 return 0;
40237840
JW
726
727err:
728 if (i) {
9bd05507
SWL
729 for (i--; i >= 0; i--) {
730 qemu_del_net_client(ncs[i]);
731 }
40237840 732 }
1576dbb5
EP
733
734err_svq:
40237840 735 qemu_close(vdpa_device_fd);
40237840
JW
736
737 return -1;
1e0a84ea 738}