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