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