1
The following changes since commit 187f35512106501fe9a11057f4d8705431e0026d:
1
The following changes since commit d9ccf33f9479201e5add8db0af68ca9ca8da358b:
2
2
3
Merge remote-tracking branch 'remotes/stsquad/tags/pull-testing-next-251019-3' into staging (2019-10-26 10:13:48 +0100)
3
Merge remote-tracking branch 'remotes/lvivier-gitlab/tags/linux-user-for-7.0-pull-request' into staging (2022-03-09 20:01:17 +0000)
4
4
5
are available in the git repository at:
5
are available in the git repository at:
6
6
7
https://github.com/jasowang/qemu.git tags/net-pull-request
7
https://github.com/jasowang/qemu.git tags/net-pull-request
8
8
9
for you to fetch changes up to 1e907a32b77e5d418538453df5945242e43224fa:
9
for you to fetch changes up to eea40402ecf895ed345f8e8eb07dbb484f4542c5:
10
10
11
COLO-compare: Fix incorrect `if` logic (2019-10-29 10:28:07 +0800)
11
vdpa: Expose VHOST_F_LOG_ALL on SVQ (2022-03-10 10:26:32 +0800)
12
12
13
----------------------------------------------------------------
13
----------------------------------------------------------------
14
14
15
Changes from V1:
15
----------------------------------------------------------------
16
Eugenio Pérez (14):
17
vhost: Add VhostShadowVirtqueue
18
vhost: Add Shadow VirtQueue kick forwarding capabilities
19
vhost: Add Shadow VirtQueue call forwarding capabilities
20
vhost: Add vhost_svq_valid_features to shadow vq
21
virtio: Add vhost_svq_get_vring_addr
22
vdpa: adapt vhost_ops callbacks to svq
23
vhost: Shadow virtqueue buffers forwarding
24
util: Add iova_tree_alloc_map
25
util: add iova_tree_find_iova
26
vhost: Add VhostIOVATree
27
vdpa: Add custom IOTLB translations to SVQ
28
vdpa: Adapt vhost_vdpa_get_vring_base to SVQ
29
vdpa: Never set log_base addr if SVQ is enabled
30
vdpa: Expose VHOST_F_LOG_ALL on SVQ
16
31
17
- Fix compling issue
32
Jason Wang (1):
33
virtio-net: fix map leaking on error during receive
18
34
19
----------------------------------------------------------------
35
hw/net/virtio-net.c | 1 +
20
Fan Yang (1):
36
hw/virtio/meson.build | 2 +-
21
COLO-compare: Fix incorrect `if` logic
37
hw/virtio/vhost-iova-tree.c | 110 +++++++
22
38
hw/virtio/vhost-iova-tree.h | 27 ++
23
Michael S. Tsirkin (1):
39
hw/virtio/vhost-shadow-virtqueue.c | 638 +++++++++++++++++++++++++++++++++++++
24
virtio: new post_load hook
40
hw/virtio/vhost-shadow-virtqueue.h | 87 +++++
25
41
hw/virtio/vhost-vdpa.c | 525 +++++++++++++++++++++++++++++-
26
Mikhail Sennikovsky (1):
42
include/hw/virtio/vhost-vdpa.h | 8 +
27
virtio-net: prevent offloads reset on migration
43
include/qemu/iova-tree.h | 38 ++-
28
44
util/iova-tree.c | 169 ++++++++++
29
Sven Schnelle (1):
45
10 files changed, 1588 insertions(+), 17 deletions(-)
30
net: add tulip (dec21143) driver
46
create mode 100644 hw/virtio/vhost-iova-tree.c
31
47
create mode 100644 hw/virtio/vhost-iova-tree.h
32
MAINTAINERS | 6 +
48
create mode 100644 hw/virtio/vhost-shadow-virtqueue.c
33
hw/net/Kconfig | 5 +
49
create mode 100644 hw/virtio/vhost-shadow-virtqueue.h
34
hw/net/Makefile.objs | 1 +
35
hw/net/trace-events | 14 +
36
hw/net/tulip.c | 1029 ++++++++++++++++++++++++++++++++++++++++
37
hw/net/tulip.h | 267 +++++++++++
38
hw/net/virtio-net.c | 27 +-
39
hw/virtio/virtio.c | 7 +
40
include/hw/pci/pci_ids.h | 1 +
41
include/hw/virtio/virtio-net.h | 2 +
42
include/hw/virtio/virtio.h | 6 +
43
net/colo-compare.c | 6 +-
44
12 files changed, 1365 insertions(+), 6 deletions(-)
45
create mode 100644 hw/net/tulip.c
46
create mode 100644 hw/net/tulip.h
47
50
48
51
49
diff view generated by jsdifflib
New patch
1
Commit bedd7e93d0196 ("virtio-net: fix use after unmap/free for sg")
2
tries to fix the use after free of the sg by caching the virtqueue
3
elements in an array and unmap them at once after receiving the
4
packets, But it forgot to unmap the cached elements on error which
5
will lead to leaking of mapping and other unexpected results.
1
6
7
Fixing this by detaching the cached elements on error. This addresses
8
CVE-2022-26353.
9
10
Reported-by: Victor Tom <vv474172261@gmail.com>
11
Cc: qemu-stable@nongnu.org
12
Fixes: CVE-2022-26353
13
Fixes: bedd7e93d0196 ("virtio-net: fix use after unmap/free for sg")
14
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
15
Signed-off-by: Jason Wang <jasowang@redhat.com>
16
---
17
hw/net/virtio-net.c | 1 +
18
1 file changed, 1 insertion(+)
19
20
diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
21
index XXXXXXX..XXXXXXX 100644
22
--- a/hw/net/virtio-net.c
23
+++ b/hw/net/virtio-net.c
24
@@ -XXX,XX +XXX,XX @@ static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
25
26
err:
27
for (j = 0; j < i; j++) {
28
+ virtqueue_detach_element(q->rx_vq, elems[j], lens[j]);
29
g_free(elems[j]);
30
}
31
32
--
33
2.7.4
diff view generated by jsdifflib
New patch
1
From: Eugenio Pérez <eperezma@redhat.com>
1
2
3
Vhost shadow virtqueue (SVQ) is an intermediate jump for virtqueue
4
notifications and buffers, allowing qemu to track them. While qemu is
5
forwarding the buffers and virtqueue changes, it is able to commit the
6
memory it's being dirtied, the same way regular qemu's VirtIO devices
7
do.
8
9
This commit only exposes basic SVQ allocation and free. Next patches of
10
the series add functionality like notifications and buffers forwarding.
11
12
Acked-by: Michael S. Tsirkin <mst@redhat.com>
13
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
14
Signed-off-by: Jason Wang <jasowang@redhat.com>
15
---
16
hw/virtio/meson.build | 2 +-
17
hw/virtio/vhost-shadow-virtqueue.c | 62 ++++++++++++++++++++++++++++++++++++++
18
hw/virtio/vhost-shadow-virtqueue.h | 28 +++++++++++++++++
19
3 files changed, 91 insertions(+), 1 deletion(-)
20
create mode 100644 hw/virtio/vhost-shadow-virtqueue.c
21
create mode 100644 hw/virtio/vhost-shadow-virtqueue.h
22
23
diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build
24
index XXXXXXX..XXXXXXX 100644
25
--- a/hw/virtio/meson.build
26
+++ b/hw/virtio/meson.build
27
@@ -XXX,XX +XXX,XX @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c'))
28
29
virtio_ss = ss.source_set()
30
virtio_ss.add(files('virtio.c'))
31
-virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c'))
32
+virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c'))
33
virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c'))
34
virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c'))
35
virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
36
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
37
new file mode 100644
38
index XXXXXXX..XXXXXXX
39
--- /dev/null
40
+++ b/hw/virtio/vhost-shadow-virtqueue.c
41
@@ -XXX,XX +XXX,XX @@
42
+/*
43
+ * vhost shadow virtqueue
44
+ *
45
+ * SPDX-FileCopyrightText: Red Hat, Inc. 2021
46
+ * SPDX-FileContributor: Author: Eugenio Pérez <eperezma@redhat.com>
47
+ *
48
+ * SPDX-License-Identifier: GPL-2.0-or-later
49
+ */
50
+
51
+#include "qemu/osdep.h"
52
+#include "hw/virtio/vhost-shadow-virtqueue.h"
53
+
54
+#include "qemu/error-report.h"
55
+
56
+/**
57
+ * Creates vhost shadow virtqueue, and instructs the vhost device to use the
58
+ * shadow methods and file descriptors.
59
+ *
60
+ * Returns the new virtqueue or NULL.
61
+ *
62
+ * In case of error, reason is reported through error_report.
63
+ */
64
+VhostShadowVirtqueue *vhost_svq_new(void)
65
+{
66
+ g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1);
67
+ int r;
68
+
69
+ r = event_notifier_init(&svq->hdev_kick, 0);
70
+ if (r != 0) {
71
+ error_report("Couldn't create kick event notifier: %s (%d)",
72
+ g_strerror(errno), errno);
73
+ goto err_init_hdev_kick;
74
+ }
75
+
76
+ r = event_notifier_init(&svq->hdev_call, 0);
77
+ if (r != 0) {
78
+ error_report("Couldn't create call event notifier: %s (%d)",
79
+ g_strerror(errno), errno);
80
+ goto err_init_hdev_call;
81
+ }
82
+
83
+ return g_steal_pointer(&svq);
84
+
85
+err_init_hdev_call:
86
+ event_notifier_cleanup(&svq->hdev_kick);
87
+
88
+err_init_hdev_kick:
89
+ return NULL;
90
+}
91
+
92
+/**
93
+ * Free the resources of the shadow virtqueue.
94
+ *
95
+ * @pvq: gpointer to SVQ so it can be used by autofree functions.
96
+ */
97
+void vhost_svq_free(gpointer pvq)
98
+{
99
+ VhostShadowVirtqueue *vq = pvq;
100
+ event_notifier_cleanup(&vq->hdev_kick);
101
+ event_notifier_cleanup(&vq->hdev_call);
102
+ g_free(vq);
103
+}
104
diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h
105
new file mode 100644
106
index XXXXXXX..XXXXXXX
107
--- /dev/null
108
+++ b/hw/virtio/vhost-shadow-virtqueue.h
109
@@ -XXX,XX +XXX,XX @@
110
+/*
111
+ * vhost shadow virtqueue
112
+ *
113
+ * SPDX-FileCopyrightText: Red Hat, Inc. 2021
114
+ * SPDX-FileContributor: Author: Eugenio Pérez <eperezma@redhat.com>
115
+ *
116
+ * SPDX-License-Identifier: GPL-2.0-or-later
117
+ */
118
+
119
+#ifndef VHOST_SHADOW_VIRTQUEUE_H
120
+#define VHOST_SHADOW_VIRTQUEUE_H
121
+
122
+#include "qemu/event_notifier.h"
123
+
124
+/* Shadow virtqueue to relay notifications */
125
+typedef struct VhostShadowVirtqueue {
126
+ /* Shadow kick notifier, sent to vhost */
127
+ EventNotifier hdev_kick;
128
+ /* Shadow call notifier, sent to vhost */
129
+ EventNotifier hdev_call;
130
+} VhostShadowVirtqueue;
131
+
132
+VhostShadowVirtqueue *vhost_svq_new(void);
133
+
134
+void vhost_svq_free(gpointer vq);
135
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(VhostShadowVirtqueue, vhost_svq_free);
136
+
137
+#endif
138
--
139
2.7.4
140
141
diff view generated by jsdifflib
New patch
1
1
From: Eugenio Pérez <eperezma@redhat.com>
2
3
At this mode no buffer forwarding will be performed in SVQ mode: Qemu
4
will just forward the guest's kicks to the device.
5
6
Host memory notifiers regions are left out for simplicity, and they will
7
not be addressed in this series.
8
9
Acked-by: Michael S. Tsirkin <mst@redhat.com>
10
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
11
Signed-off-by: Jason Wang <jasowang@redhat.com>
12
---
13
hw/virtio/vhost-shadow-virtqueue.c | 56 ++++++++++++++
14
hw/virtio/vhost-shadow-virtqueue.h | 14 ++++
15
hw/virtio/vhost-vdpa.c | 145 ++++++++++++++++++++++++++++++++++++-
16
include/hw/virtio/vhost-vdpa.h | 4 +
17
4 files changed, 217 insertions(+), 2 deletions(-)
18
19
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
20
index XXXXXXX..XXXXXXX 100644
21
--- a/hw/virtio/vhost-shadow-virtqueue.c
22
+++ b/hw/virtio/vhost-shadow-virtqueue.c
23
@@ -XXX,XX +XXX,XX @@
24
#include "hw/virtio/vhost-shadow-virtqueue.h"
25
26
#include "qemu/error-report.h"
27
+#include "qemu/main-loop.h"
28
+#include "linux-headers/linux/vhost.h"
29
+
30
+/**
31
+ * Forward guest notifications.
32
+ *
33
+ * @n: guest kick event notifier, the one that guest set to notify svq.
34
+ */
35
+static void vhost_handle_guest_kick(EventNotifier *n)
36
+{
37
+ VhostShadowVirtqueue *svq = container_of(n, VhostShadowVirtqueue,
38
+ svq_kick);
39
+ event_notifier_test_and_clear(n);
40
+ event_notifier_set(&svq->hdev_kick);
41
+}
42
+
43
+/**
44
+ * Set a new file descriptor for the guest to kick the SVQ and notify for avail
45
+ *
46
+ * @svq: The svq
47
+ * @svq_kick_fd: The svq kick fd
48
+ *
49
+ * Note that the SVQ will never close the old file descriptor.
50
+ */
51
+void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)
52
+{
53
+ EventNotifier *svq_kick = &svq->svq_kick;
54
+ bool poll_stop = VHOST_FILE_UNBIND != event_notifier_get_fd(svq_kick);
55
+ bool poll_start = svq_kick_fd != VHOST_FILE_UNBIND;
56
+
57
+ if (poll_stop) {
58
+ event_notifier_set_handler(svq_kick, NULL);
59
+ }
60
+
61
+ /*
62
+ * event_notifier_set_handler already checks for guest's notifications if
63
+ * they arrive at the new file descriptor in the switch, so there is no
64
+ * need to explicitly check for them.
65
+ */
66
+ if (poll_start) {
67
+ event_notifier_init_fd(svq_kick, svq_kick_fd);
68
+ event_notifier_set(svq_kick);
69
+ event_notifier_set_handler(svq_kick, vhost_handle_guest_kick);
70
+ }
71
+}
72
+
73
+/**
74
+ * Stop the shadow virtqueue operation.
75
+ * @svq: Shadow Virtqueue
76
+ */
77
+void vhost_svq_stop(VhostShadowVirtqueue *svq)
78
+{
79
+ event_notifier_set_handler(&svq->svq_kick, NULL);
80
+}
81
82
/**
83
* Creates vhost shadow virtqueue, and instructs the vhost device to use the
84
@@ -XXX,XX +XXX,XX @@ VhostShadowVirtqueue *vhost_svq_new(void)
85
goto err_init_hdev_call;
86
}
87
88
+ event_notifier_init_fd(&svq->svq_kick, VHOST_FILE_UNBIND);
89
return g_steal_pointer(&svq);
90
91
err_init_hdev_call:
92
@@ -XXX,XX +XXX,XX @@ err_init_hdev_kick:
93
void vhost_svq_free(gpointer pvq)
94
{
95
VhostShadowVirtqueue *vq = pvq;
96
+ vhost_svq_stop(vq);
97
event_notifier_cleanup(&vq->hdev_kick);
98
event_notifier_cleanup(&vq->hdev_call);
99
g_free(vq);
100
diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h
101
index XXXXXXX..XXXXXXX 100644
102
--- a/hw/virtio/vhost-shadow-virtqueue.h
103
+++ b/hw/virtio/vhost-shadow-virtqueue.h
104
@@ -XXX,XX +XXX,XX @@ typedef struct VhostShadowVirtqueue {
105
EventNotifier hdev_kick;
106
/* Shadow call notifier, sent to vhost */
107
EventNotifier hdev_call;
108
+
109
+ /*
110
+ * Borrowed virtqueue's guest to host notifier. To borrow it in this event
111
+ * notifier allows to recover the VhostShadowVirtqueue from the event loop
112
+ * easily. If we use the VirtQueue's one, we don't have an easy way to
113
+ * retrieve VhostShadowVirtqueue.
114
+ *
115
+ * So shadow virtqueue must not clean it, or we would lose VirtQueue one.
116
+ */
117
+ EventNotifier svq_kick;
118
} VhostShadowVirtqueue;
119
120
+void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd);
121
+
122
+void vhost_svq_stop(VhostShadowVirtqueue *svq);
123
+
124
VhostShadowVirtqueue *vhost_svq_new(void);
125
126
void vhost_svq_free(gpointer vq);
127
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
128
index XXXXXXX..XXXXXXX 100644
129
--- a/hw/virtio/vhost-vdpa.c
130
+++ b/hw/virtio/vhost-vdpa.c
131
@@ -XXX,XX +XXX,XX @@
132
#include "hw/virtio/vhost.h"
133
#include "hw/virtio/vhost-backend.h"
134
#include "hw/virtio/virtio-net.h"
135
+#include "hw/virtio/vhost-shadow-virtqueue.h"
136
#include "hw/virtio/vhost-vdpa.h"
137
#include "exec/address-spaces.h"
138
#include "qemu/main-loop.h"
139
#include "cpu.h"
140
#include "trace.h"
141
#include "qemu-common.h"
142
+#include "qapi/error.h"
143
144
/*
145
* Return one past the end of the end of section. Be careful with uint64_t
146
@@ -XXX,XX +XXX,XX @@ static bool vhost_vdpa_one_time_request(struct vhost_dev *dev)
147
return v->index != 0;
148
}
149
150
+static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,
151
+ Error **errp)
152
+{
153
+ g_autoptr(GPtrArray) shadow_vqs = NULL;
154
+
155
+ if (!v->shadow_vqs_enabled) {
156
+ return 0;
157
+ }
158
+
159
+ shadow_vqs = g_ptr_array_new_full(hdev->nvqs, vhost_svq_free);
160
+ for (unsigned n = 0; n < hdev->nvqs; ++n) {
161
+ g_autoptr(VhostShadowVirtqueue) svq = vhost_svq_new();
162
+
163
+ if (unlikely(!svq)) {
164
+ error_setg(errp, "Cannot create svq %u", n);
165
+ return -1;
166
+ }
167
+ g_ptr_array_add(shadow_vqs, g_steal_pointer(&svq));
168
+ }
169
+
170
+ v->shadow_vqs = g_steal_pointer(&shadow_vqs);
171
+ return 0;
172
+}
173
+
174
static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp)
175
{
176
struct vhost_vdpa *v;
177
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp)
178
dev->opaque = opaque ;
179
v->listener = vhost_vdpa_memory_listener;
180
v->msg_type = VHOST_IOTLB_MSG_V2;
181
+ ret = vhost_vdpa_init_svq(dev, v, errp);
182
+ if (ret) {
183
+ goto err;
184
+ }
185
186
vhost_vdpa_get_iova_range(v);
187
188
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp)
189
VIRTIO_CONFIG_S_DRIVER);
190
191
return 0;
192
+
193
+err:
194
+ ram_block_discard_disable(false);
195
+ return ret;
196
}
197
198
static void vhost_vdpa_host_notifier_uninit(struct vhost_dev *dev,
199
@@ -XXX,XX +XXX,XX @@ static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n)
200
201
static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev)
202
{
203
+ struct vhost_vdpa *v = dev->opaque;
204
int i;
205
206
+ if (v->shadow_vqs_enabled) {
207
+ /* FIXME SVQ is not compatible with host notifiers mr */
208
+ return;
209
+ }
210
+
211
for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) {
212
if (vhost_vdpa_host_notifier_init(dev, i)) {
213
goto err;
214
@@ -XXX,XX +XXX,XX @@ err:
215
return;
216
}
217
218
+static void vhost_vdpa_svq_cleanup(struct vhost_dev *dev)
219
+{
220
+ struct vhost_vdpa *v = dev->opaque;
221
+ size_t idx;
222
+
223
+ if (!v->shadow_vqs) {
224
+ return;
225
+ }
226
+
227
+ for (idx = 0; idx < v->shadow_vqs->len; ++idx) {
228
+ vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, idx));
229
+ }
230
+ g_ptr_array_free(v->shadow_vqs, true);
231
+}
232
+
233
static int vhost_vdpa_cleanup(struct vhost_dev *dev)
234
{
235
struct vhost_vdpa *v;
236
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_cleanup(struct vhost_dev *dev)
237
trace_vhost_vdpa_cleanup(dev, v);
238
vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
239
memory_listener_unregister(&v->listener);
240
+ vhost_vdpa_svq_cleanup(dev);
241
242
dev->opaque = NULL;
243
ram_block_discard_disable(false);
244
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_get_device_id(struct vhost_dev *dev,
245
return ret;
246
}
247
248
+static void vhost_vdpa_reset_svq(struct vhost_vdpa *v)
249
+{
250
+ if (!v->shadow_vqs_enabled) {
251
+ return;
252
+ }
253
+
254
+ for (unsigned i = 0; i < v->shadow_vqs->len; ++i) {
255
+ VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i);
256
+ vhost_svq_stop(svq);
257
+ }
258
+}
259
+
260
static int vhost_vdpa_reset_device(struct vhost_dev *dev)
261
{
262
+ struct vhost_vdpa *v = dev->opaque;
263
int ret;
264
uint8_t status = 0;
265
266
+ vhost_vdpa_reset_svq(v);
267
+
268
ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
269
trace_vhost_vdpa_reset_device(dev, status);
270
return ret;
271
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
272
return ret;
273
}
274
275
+static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev,
276
+ struct vhost_vring_file *file)
277
+{
278
+ trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd);
279
+ return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file);
280
+}
281
+
282
+/**
283
+ * Set the shadow virtqueue descriptors to the device
284
+ *
285
+ * @dev: The vhost device model
286
+ * @svq: The shadow virtqueue
287
+ * @idx: The index of the virtqueue in the vhost device
288
+ * @errp: Error
289
+ */
290
+static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
291
+ VhostShadowVirtqueue *svq,
292
+ unsigned idx,
293
+ Error **errp)
294
+{
295
+ struct vhost_vring_file file = {
296
+ .index = dev->vq_index + idx,
297
+ };
298
+ const EventNotifier *event_notifier = &svq->hdev_kick;
299
+ int r;
300
+
301
+ file.fd = event_notifier_get_fd(event_notifier);
302
+ r = vhost_vdpa_set_vring_dev_kick(dev, &file);
303
+ if (unlikely(r != 0)) {
304
+ error_setg_errno(errp, -r, "Can't set device kick fd");
305
+ }
306
+
307
+ return r == 0;
308
+}
309
+
310
+static bool vhost_vdpa_svqs_start(struct vhost_dev *dev)
311
+{
312
+ struct vhost_vdpa *v = dev->opaque;
313
+ Error *err = NULL;
314
+ unsigned i;
315
+
316
+ if (!v->shadow_vqs) {
317
+ return true;
318
+ }
319
+
320
+ for (i = 0; i < v->shadow_vqs->len; ++i) {
321
+ VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i);
322
+ bool ok = vhost_vdpa_svq_setup(dev, svq, i, &err);
323
+ if (unlikely(!ok)) {
324
+ error_reportf_err(err, "Cannot setup SVQ %u: ", i);
325
+ return false;
326
+ }
327
+ }
328
+
329
+ return true;
330
+}
331
+
332
static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
333
{
334
struct vhost_vdpa *v = dev->opaque;
335
+ bool ok;
336
trace_vhost_vdpa_dev_start(dev, started);
337
338
if (started) {
339
vhost_vdpa_host_notifiers_init(dev);
340
+ ok = vhost_vdpa_svqs_start(dev);
341
+ if (unlikely(!ok)) {
342
+ return -1;
343
+ }
344
vhost_vdpa_set_vring_ready(dev);
345
} else {
346
vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
347
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,
348
static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev,
349
struct vhost_vring_file *file)
350
{
351
- trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd);
352
- return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file);
353
+ struct vhost_vdpa *v = dev->opaque;
354
+ int vdpa_idx = file->index - dev->vq_index;
355
+
356
+ if (v->shadow_vqs_enabled) {
357
+ VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx);
358
+ vhost_svq_set_svq_kick_fd(svq, file->fd);
359
+ return 0;
360
+ } else {
361
+ return vhost_vdpa_set_vring_dev_kick(dev, file);
362
+ }
363
}
364
365
static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,
366
diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h
367
index XXXXXXX..XXXXXXX 100644
368
--- a/include/hw/virtio/vhost-vdpa.h
369
+++ b/include/hw/virtio/vhost-vdpa.h
370
@@ -XXX,XX +XXX,XX @@
371
#ifndef HW_VIRTIO_VHOST_VDPA_H
372
#define HW_VIRTIO_VHOST_VDPA_H
373
374
+#include <gmodule.h>
375
+
376
#include "hw/virtio/virtio.h"
377
#include "standard-headers/linux/vhost_types.h"
378
379
@@ -XXX,XX +XXX,XX @@ typedef struct vhost_vdpa {
380
bool iotlb_batch_begin_sent;
381
MemoryListener listener;
382
struct vhost_vdpa_iova_range iova_range;
383
+ bool shadow_vqs_enabled;
384
+ GPtrArray *shadow_vqs;
385
struct vhost_dev *dev;
386
VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX];
387
} VhostVDPA;
388
--
389
2.7.4
390
391
diff view generated by jsdifflib
New patch
1
From: Eugenio Pérez <eperezma@redhat.com>
1
2
3
This will make qemu aware of the device used buffers, allowing it to
4
write the guest memory with its contents if needed.
5
6
Acked-by: Michael S. Tsirkin <mst@redhat.com>
7
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
8
Signed-off-by: Jason Wang <jasowang@redhat.com>
9
---
10
hw/virtio/vhost-shadow-virtqueue.c | 38 ++++++++++++++++++++++++++++++++++++++
11
hw/virtio/vhost-shadow-virtqueue.h | 4 ++++
12
hw/virtio/vhost-vdpa.c | 31 +++++++++++++++++++++++++++++--
13
3 files changed, 71 insertions(+), 2 deletions(-)
14
15
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
16
index XXXXXXX..XXXXXXX 100644
17
--- a/hw/virtio/vhost-shadow-virtqueue.c
18
+++ b/hw/virtio/vhost-shadow-virtqueue.c
19
@@ -XXX,XX +XXX,XX @@ static void vhost_handle_guest_kick(EventNotifier *n)
20
}
21
22
/**
23
+ * Forward vhost notifications
24
+ *
25
+ * @n: hdev call event notifier, the one that device set to notify svq.
26
+ */
27
+static void vhost_svq_handle_call(EventNotifier *n)
28
+{
29
+ VhostShadowVirtqueue *svq = container_of(n, VhostShadowVirtqueue,
30
+ hdev_call);
31
+ event_notifier_test_and_clear(n);
32
+ event_notifier_set(&svq->svq_call);
33
+}
34
+
35
+/**
36
+ * Set the call notifier for the SVQ to call the guest
37
+ *
38
+ * @svq: Shadow virtqueue
39
+ * @call_fd: call notifier
40
+ *
41
+ * Called on BQL context.
42
+ */
43
+void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd)
44
+{
45
+ if (call_fd == VHOST_FILE_UNBIND) {
46
+ /*
47
+ * Fail event_notifier_set if called handling device call.
48
+ *
49
+ * SVQ still needs device notifications, since it needs to keep
50
+ * forwarding used buffers even with the unbind.
51
+ */
52
+ memset(&svq->svq_call, 0, sizeof(svq->svq_call));
53
+ } else {
54
+ event_notifier_init_fd(&svq->svq_call, call_fd);
55
+ }
56
+}
57
+
58
+/**
59
* Set a new file descriptor for the guest to kick the SVQ and notify for avail
60
*
61
* @svq: The svq
62
@@ -XXX,XX +XXX,XX @@ VhostShadowVirtqueue *vhost_svq_new(void)
63
}
64
65
event_notifier_init_fd(&svq->svq_kick, VHOST_FILE_UNBIND);
66
+ event_notifier_set_handler(&svq->hdev_call, vhost_svq_handle_call);
67
return g_steal_pointer(&svq);
68
69
err_init_hdev_call:
70
@@ -XXX,XX +XXX,XX @@ void vhost_svq_free(gpointer pvq)
71
VhostShadowVirtqueue *vq = pvq;
72
vhost_svq_stop(vq);
73
event_notifier_cleanup(&vq->hdev_kick);
74
+ event_notifier_set_handler(&vq->hdev_call, NULL);
75
event_notifier_cleanup(&vq->hdev_call);
76
g_free(vq);
77
}
78
diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h
79
index XXXXXXX..XXXXXXX 100644
80
--- a/hw/virtio/vhost-shadow-virtqueue.h
81
+++ b/hw/virtio/vhost-shadow-virtqueue.h
82
@@ -XXX,XX +XXX,XX @@ typedef struct VhostShadowVirtqueue {
83
* So shadow virtqueue must not clean it, or we would lose VirtQueue one.
84
*/
85
EventNotifier svq_kick;
86
+
87
+ /* Guest's call notifier, where the SVQ calls guest. */
88
+ EventNotifier svq_call;
89
} VhostShadowVirtqueue;
90
91
void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd);
92
+void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd);
93
94
void vhost_svq_stop(VhostShadowVirtqueue *svq);
95
96
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
97
index XXXXXXX..XXXXXXX 100644
98
--- a/hw/virtio/vhost-vdpa.c
99
+++ b/hw/virtio/vhost-vdpa.c
100
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev,
101
return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file);
102
}
103
104
+static int vhost_vdpa_set_vring_dev_call(struct vhost_dev *dev,
105
+ struct vhost_vring_file *file)
106
+{
107
+ trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd);
108
+ return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file);
109
+}
110
+
111
/**
112
* Set the shadow virtqueue descriptors to the device
113
*
114
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev,
115
* @svq: The shadow virtqueue
116
* @idx: The index of the virtqueue in the vhost device
117
* @errp: Error
118
+ *
119
+ * Note that this function does not rewind kick file descriptor if cannot set
120
+ * call one.
121
*/
122
static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
123
VhostShadowVirtqueue *svq,
124
@@ -XXX,XX +XXX,XX @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
125
r = vhost_vdpa_set_vring_dev_kick(dev, &file);
126
if (unlikely(r != 0)) {
127
error_setg_errno(errp, -r, "Can't set device kick fd");
128
+ return false;
129
+ }
130
+
131
+ event_notifier = &svq->hdev_call;
132
+ file.fd = event_notifier_get_fd(event_notifier);
133
+ r = vhost_vdpa_set_vring_dev_call(dev, &file);
134
+ if (unlikely(r != 0)) {
135
+ error_setg_errno(errp, -r, "Can't set device call fd");
136
}
137
138
return r == 0;
139
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev,
140
static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,
141
struct vhost_vring_file *file)
142
{
143
- trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd);
144
- return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file);
145
+ struct vhost_vdpa *v = dev->opaque;
146
+
147
+ if (v->shadow_vqs_enabled) {
148
+ int vdpa_idx = file->index - dev->vq_index;
149
+ VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx);
150
+
151
+ vhost_svq_set_svq_call_fd(svq, file->fd);
152
+ return 0;
153
+ } else {
154
+ return vhost_vdpa_set_vring_dev_call(dev, file);
155
+ }
156
}
157
158
static int vhost_vdpa_get_features(struct vhost_dev *dev,
159
--
160
2.7.4
161
162
diff view generated by jsdifflib
New patch
1
From: Eugenio Pérez <eperezma@redhat.com>
1
2
3
This allows SVQ to negotiate features with the guest and the device. For
4
the device, SVQ is a driver. While this function bypasses all
5
non-transport features, it needs to disable the features that SVQ does
6
not support when forwarding buffers. This includes packed vq layout,
7
indirect descriptors or event idx.
8
9
Future changes can add support to offer more features to the guest,
10
since the use of VirtQueue gives this for free. This is left out at the
11
moment for simplicity.
12
13
Acked-by: Michael S. Tsirkin <mst@redhat.com>
14
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
15
Signed-off-by: Jason Wang <jasowang@redhat.com>
16
---
17
hw/virtio/vhost-shadow-virtqueue.c | 44 ++++++++++++++++++++++++++++++++++++++
18
hw/virtio/vhost-shadow-virtqueue.h | 2 ++
19
hw/virtio/vhost-vdpa.c | 15 +++++++++++++
20
3 files changed, 61 insertions(+)
21
22
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
23
index XXXXXXX..XXXXXXX 100644
24
--- a/hw/virtio/vhost-shadow-virtqueue.c
25
+++ b/hw/virtio/vhost-shadow-virtqueue.c
26
@@ -XXX,XX +XXX,XX @@
27
#include "hw/virtio/vhost-shadow-virtqueue.h"
28
29
#include "qemu/error-report.h"
30
+#include "qapi/error.h"
31
#include "qemu/main-loop.h"
32
#include "linux-headers/linux/vhost.h"
33
34
/**
35
+ * Validate the transport device features that both guests can use with the SVQ
36
+ * and SVQs can use with the device.
37
+ *
38
+ * @dev_features: The features
39
+ * @errp: Error pointer
40
+ */
41
+bool vhost_svq_valid_features(uint64_t features, Error **errp)
42
+{
43
+ bool ok = true;
44
+ uint64_t svq_features = features;
45
+
46
+ for (uint64_t b = VIRTIO_TRANSPORT_F_START; b <= VIRTIO_TRANSPORT_F_END;
47
+ ++b) {
48
+ switch (b) {
49
+ case VIRTIO_F_ANY_LAYOUT:
50
+ continue;
51
+
52
+ case VIRTIO_F_ACCESS_PLATFORM:
53
+ /* SVQ trust in the host's IOMMU to translate addresses */
54
+ case VIRTIO_F_VERSION_1:
55
+ /* SVQ trust that the guest vring is little endian */
56
+ if (!(svq_features & BIT_ULL(b))) {
57
+ set_bit(b, &svq_features);
58
+ ok = false;
59
+ }
60
+ continue;
61
+
62
+ default:
63
+ if (svq_features & BIT_ULL(b)) {
64
+ clear_bit(b, &svq_features);
65
+ ok = false;
66
+ }
67
+ }
68
+ }
69
+
70
+ if (!ok) {
71
+ error_setg(errp, "SVQ Invalid device feature flags, offer: 0x%"PRIx64
72
+ ", ok: 0x%"PRIx64, features, svq_features);
73
+ }
74
+ return ok;
75
+}
76
+
77
+/**
78
* Forward guest notifications.
79
*
80
* @n: guest kick event notifier, the one that guest set to notify svq.
81
diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h
82
index XXXXXXX..XXXXXXX 100644
83
--- a/hw/virtio/vhost-shadow-virtqueue.h
84
+++ b/hw/virtio/vhost-shadow-virtqueue.h
85
@@ -XXX,XX +XXX,XX @@ typedef struct VhostShadowVirtqueue {
86
EventNotifier svq_call;
87
} VhostShadowVirtqueue;
88
89
+bool vhost_svq_valid_features(uint64_t features, Error **errp);
90
+
91
void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd);
92
void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd);
93
94
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
95
index XXXXXXX..XXXXXXX 100644
96
--- a/hw/virtio/vhost-vdpa.c
97
+++ b/hw/virtio/vhost-vdpa.c
98
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,
99
Error **errp)
100
{
101
g_autoptr(GPtrArray) shadow_vqs = NULL;
102
+ uint64_t dev_features, svq_features;
103
+ int r;
104
+ bool ok;
105
106
if (!v->shadow_vqs_enabled) {
107
return 0;
108
}
109
110
+ r = hdev->vhost_ops->vhost_get_features(hdev, &dev_features);
111
+ if (r != 0) {
112
+ error_setg_errno(errp, -r, "Can't get vdpa device features");
113
+ return r;
114
+ }
115
+
116
+ svq_features = dev_features;
117
+ ok = vhost_svq_valid_features(svq_features, errp);
118
+ if (unlikely(!ok)) {
119
+ return -1;
120
+ }
121
+
122
shadow_vqs = g_ptr_array_new_full(hdev->nvqs, vhost_svq_free);
123
for (unsigned n = 0; n < hdev->nvqs; ++n) {
124
g_autoptr(VhostShadowVirtqueue) svq = vhost_svq_new();
125
--
126
2.7.4
127
128
diff view generated by jsdifflib
New patch
1
From: Eugenio Pérez <eperezma@redhat.com>
1
2
3
It reports the shadow virtqueue address from qemu virtual address space.
4
5
Since this will be different from the guest's vaddr, but the device can
6
access it, SVQ takes special care about its alignment & lack of garbage
7
data. It assumes that IOMMU will work in host_page_size ranges for that.
8
9
Acked-by: Michael S. Tsirkin <mst@redhat.com>
10
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
11
Signed-off-by: Jason Wang <jasowang@redhat.com>
12
---
13
hw/virtio/vhost-shadow-virtqueue.c | 29 +++++++++++++++++++++++++++++
14
hw/virtio/vhost-shadow-virtqueue.h | 9 +++++++++
15
2 files changed, 38 insertions(+)
16
17
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
18
index XXXXXXX..XXXXXXX 100644
19
--- a/hw/virtio/vhost-shadow-virtqueue.c
20
+++ b/hw/virtio/vhost-shadow-virtqueue.c
21
@@ -XXX,XX +XXX,XX @@ void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd)
22
}
23
24
/**
25
+ * Get the shadow vq vring address.
26
+ * @svq: Shadow virtqueue
27
+ * @addr: Destination to store address
28
+ */
29
+void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq,
30
+ struct vhost_vring_addr *addr)
31
+{
32
+ addr->desc_user_addr = (uint64_t)svq->vring.desc;
33
+ addr->avail_user_addr = (uint64_t)svq->vring.avail;
34
+ addr->used_user_addr = (uint64_t)svq->vring.used;
35
+}
36
+
37
+size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq)
38
+{
39
+ size_t desc_size = sizeof(vring_desc_t) * svq->vring.num;
40
+ size_t avail_size = offsetof(vring_avail_t, ring) +
41
+ sizeof(uint16_t) * svq->vring.num;
42
+
43
+ return ROUND_UP(desc_size + avail_size, qemu_real_host_page_size);
44
+}
45
+
46
+size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq)
47
+{
48
+ size_t used_size = offsetof(vring_used_t, ring) +
49
+ sizeof(vring_used_elem_t) * svq->vring.num;
50
+ return ROUND_UP(used_size, qemu_real_host_page_size);
51
+}
52
+
53
+/**
54
* Set a new file descriptor for the guest to kick the SVQ and notify for avail
55
*
56
* @svq: The svq
57
diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h
58
index XXXXXXX..XXXXXXX 100644
59
--- a/hw/virtio/vhost-shadow-virtqueue.h
60
+++ b/hw/virtio/vhost-shadow-virtqueue.h
61
@@ -XXX,XX +XXX,XX @@
62
#define VHOST_SHADOW_VIRTQUEUE_H
63
64
#include "qemu/event_notifier.h"
65
+#include "hw/virtio/virtio.h"
66
+#include "standard-headers/linux/vhost_types.h"
67
68
/* Shadow virtqueue to relay notifications */
69
typedef struct VhostShadowVirtqueue {
70
+ /* Shadow vring */
71
+ struct vring vring;
72
+
73
/* Shadow kick notifier, sent to vhost */
74
EventNotifier hdev_kick;
75
/* Shadow call notifier, sent to vhost */
76
@@ -XXX,XX +XXX,XX @@ bool vhost_svq_valid_features(uint64_t features, Error **errp);
77
78
void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd);
79
void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd);
80
+void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq,
81
+ struct vhost_vring_addr *addr);
82
+size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq);
83
+size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq);
84
85
void vhost_svq_stop(VhostShadowVirtqueue *svq);
86
87
--
88
2.7.4
89
90
diff view generated by jsdifflib
New patch
1
From: Eugenio Pérez <eperezma@redhat.com>
1
2
3
First half of the buffers forwarding part, preparing vhost-vdpa
4
callbacks to SVQ to offer it. QEMU cannot enable it at this moment, so
5
this is effectively dead code at the moment, but it helps to reduce
6
patch size.
7
8
Acked-by: Michael S. Tsirkin <mst@redhat.com>
9
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
10
Signed-off-by: Jason Wang <jasowang@redhat.com>
11
---
12
hw/virtio/vhost-vdpa.c | 48 +++++++++++++++++++++++++++++++++++++++++-------
13
1 file changed, 41 insertions(+), 7 deletions(-)
14
15
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
16
index XXXXXXX..XXXXXXX 100644
17
--- a/hw/virtio/vhost-vdpa.c
18
+++ b/hw/virtio/vhost-vdpa.c
19
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
20
return ret;
21
}
22
23
+static int vhost_vdpa_set_dev_vring_base(struct vhost_dev *dev,
24
+ struct vhost_vring_state *ring)
25
+{
26
+ trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num);
27
+ return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring);
28
+}
29
+
30
static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev,
31
struct vhost_vring_file *file)
32
{
33
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_vring_dev_call(struct vhost_dev *dev,
34
return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file);
35
}
36
37
+static int vhost_vdpa_set_vring_dev_addr(struct vhost_dev *dev,
38
+ struct vhost_vring_addr *addr)
39
+{
40
+ trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags,
41
+ addr->desc_user_addr, addr->used_user_addr,
42
+ addr->avail_user_addr,
43
+ addr->log_guest_addr);
44
+
45
+ return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr);
46
+
47
+}
48
+
49
/**
50
* Set the shadow virtqueue descriptors to the device
51
*
52
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base,
53
static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev,
54
struct vhost_vring_addr *addr)
55
{
56
- trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags,
57
- addr->desc_user_addr, addr->used_user_addr,
58
- addr->avail_user_addr,
59
- addr->log_guest_addr);
60
- return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr);
61
+ struct vhost_vdpa *v = dev->opaque;
62
+
63
+ if (v->shadow_vqs_enabled) {
64
+ /*
65
+ * Device vring addr was set at device start. SVQ base is handled by
66
+ * VirtQueue code.
67
+ */
68
+ return 0;
69
+ }
70
+
71
+ return vhost_vdpa_set_vring_dev_addr(dev, addr);
72
}
73
74
static int vhost_vdpa_set_vring_num(struct vhost_dev *dev,
75
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_vring_num(struct vhost_dev *dev,
76
static int vhost_vdpa_set_vring_base(struct vhost_dev *dev,
77
struct vhost_vring_state *ring)
78
{
79
- trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num);
80
- return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring);
81
+ struct vhost_vdpa *v = dev->opaque;
82
+
83
+ if (v->shadow_vqs_enabled) {
84
+ /*
85
+ * Device vring base was set at device start. SVQ base is handled by
86
+ * VirtQueue code.
87
+ */
88
+ return 0;
89
+ }
90
+
91
+ return vhost_vdpa_set_dev_vring_base(dev, ring);
92
}
93
94
static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,
95
--
96
2.7.4
97
98
diff view generated by jsdifflib
New patch
1
1
From: Eugenio Pérez <eperezma@redhat.com>
2
3
Initial version of shadow virtqueue that actually forward buffers. There
4
is no iommu support at the moment, and that will be addressed in future
5
patches of this series. Since all vhost-vdpa devices use forced IOMMU,
6
this means that SVQ is not usable at this point of the series on any
7
device.
8
9
For simplicity it only supports modern devices, that expects vring
10
in little endian, with split ring and no event idx or indirect
11
descriptors. Support for them will not be added in this series.
12
13
It reuses the VirtQueue code for the device part. The driver part is
14
based on Linux's virtio_ring driver, but with stripped functionality
15
and optimizations so it's easier to review.
16
17
However, forwarding buffers have some particular pieces: One of the most
18
unexpected ones is that a guest's buffer can expand through more than
19
one descriptor in SVQ. While this is handled gracefully by qemu's
20
emulated virtio devices, it may cause unexpected SVQ queue full. This
21
patch also solves it by checking for this condition at both guest's
22
kicks and device's calls. The code may be more elegant in the future if
23
SVQ code runs in its own iocontext.
24
25
Acked-by: Michael S. Tsirkin <mst@redhat.com>
26
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
27
Signed-off-by: Jason Wang <jasowang@redhat.com>
28
---
29
hw/virtio/vhost-shadow-virtqueue.c | 354 ++++++++++++++++++++++++++++++++++++-
30
hw/virtio/vhost-shadow-virtqueue.h | 26 +++
31
hw/virtio/vhost-vdpa.c | 159 ++++++++++++++++-
32
3 files changed, 527 insertions(+), 12 deletions(-)
33
34
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
35
index XXXXXXX..XXXXXXX 100644
36
--- a/hw/virtio/vhost-shadow-virtqueue.c
37
+++ b/hw/virtio/vhost-shadow-virtqueue.c
38
@@ -XXX,XX +XXX,XX @@
39
#include "qemu/error-report.h"
40
#include "qapi/error.h"
41
#include "qemu/main-loop.h"
42
+#include "qemu/log.h"
43
+#include "qemu/memalign.h"
44
#include "linux-headers/linux/vhost.h"
45
46
/**
47
@@ -XXX,XX +XXX,XX @@ bool vhost_svq_valid_features(uint64_t features, Error **errp)
48
}
49
50
/**
51
- * Forward guest notifications.
52
+ * Number of descriptors that the SVQ can make available from the guest.
53
+ *
54
+ * @svq: The svq
55
+ */
56
+static uint16_t vhost_svq_available_slots(const VhostShadowVirtqueue *svq)
57
+{
58
+ return svq->vring.num - (svq->shadow_avail_idx - svq->shadow_used_idx);
59
+}
60
+
61
+static void vhost_vring_write_descs(VhostShadowVirtqueue *svq,
62
+ const struct iovec *iovec,
63
+ size_t num, bool more_descs, bool write)
64
+{
65
+ uint16_t i = svq->free_head, last = svq->free_head;
66
+ unsigned n;
67
+ uint16_t flags = write ? cpu_to_le16(VRING_DESC_F_WRITE) : 0;
68
+ vring_desc_t *descs = svq->vring.desc;
69
+
70
+ if (num == 0) {
71
+ return;
72
+ }
73
+
74
+ for (n = 0; n < num; n++) {
75
+ if (more_descs || (n + 1 < num)) {
76
+ descs[i].flags = flags | cpu_to_le16(VRING_DESC_F_NEXT);
77
+ } else {
78
+ descs[i].flags = flags;
79
+ }
80
+ descs[i].addr = cpu_to_le64((hwaddr)iovec[n].iov_base);
81
+ descs[i].len = cpu_to_le32(iovec[n].iov_len);
82
+
83
+ last = i;
84
+ i = cpu_to_le16(descs[i].next);
85
+ }
86
+
87
+ svq->free_head = le16_to_cpu(descs[last].next);
88
+}
89
+
90
+static bool vhost_svq_add_split(VhostShadowVirtqueue *svq,
91
+ VirtQueueElement *elem,
92
+ unsigned *head)
93
+{
94
+ unsigned avail_idx;
95
+ vring_avail_t *avail = svq->vring.avail;
96
+
97
+ *head = svq->free_head;
98
+
99
+ /* We need some descriptors here */
100
+ if (unlikely(!elem->out_num && !elem->in_num)) {
101
+ qemu_log_mask(LOG_GUEST_ERROR,
102
+ "Guest provided element with no descriptors");
103
+ return false;
104
+ }
105
+
106
+ vhost_vring_write_descs(svq, elem->out_sg, elem->out_num,
107
+ elem->in_num > 0, false);
108
+ vhost_vring_write_descs(svq, elem->in_sg, elem->in_num, false, true);
109
+
110
+ /*
111
+ * Put the entry in the available array (but don't update avail->idx until
112
+ * they do sync).
113
+ */
114
+ avail_idx = svq->shadow_avail_idx & (svq->vring.num - 1);
115
+ avail->ring[avail_idx] = cpu_to_le16(*head);
116
+ svq->shadow_avail_idx++;
117
+
118
+ /* Update the avail index after write the descriptor */
119
+ smp_wmb();
120
+ avail->idx = cpu_to_le16(svq->shadow_avail_idx);
121
+
122
+ return true;
123
+}
124
+
125
+static bool vhost_svq_add(VhostShadowVirtqueue *svq, VirtQueueElement *elem)
126
+{
127
+ unsigned qemu_head;
128
+ bool ok = vhost_svq_add_split(svq, elem, &qemu_head);
129
+ if (unlikely(!ok)) {
130
+ return false;
131
+ }
132
+
133
+ svq->ring_id_maps[qemu_head] = elem;
134
+ return true;
135
+}
136
+
137
+static void vhost_svq_kick(VhostShadowVirtqueue *svq)
138
+{
139
+ /*
140
+ * We need to expose the available array entries before checking the used
141
+ * flags
142
+ */
143
+ smp_mb();
144
+ if (svq->vring.used->flags & VRING_USED_F_NO_NOTIFY) {
145
+ return;
146
+ }
147
+
148
+ event_notifier_set(&svq->hdev_kick);
149
+}
150
+
151
+/**
152
+ * Forward available buffers.
153
+ *
154
+ * @svq: Shadow VirtQueue
155
+ *
156
+ * Note that this function does not guarantee that all guest's available
157
+ * buffers are available to the device in SVQ avail ring. The guest may have
158
+ * exposed a GPA / GIOVA contiguous buffer, but it may not be contiguous in
159
+ * qemu vaddr.
160
+ *
161
+ * If that happens, guest's kick notifications will be disabled until the
162
+ * device uses some buffers.
163
+ */
164
+static void vhost_handle_guest_kick(VhostShadowVirtqueue *svq)
165
+{
166
+ /* Clear event notifier */
167
+ event_notifier_test_and_clear(&svq->svq_kick);
168
+
169
+ /* Forward to the device as many available buffers as possible */
170
+ do {
171
+ virtio_queue_set_notification(svq->vq, false);
172
+
173
+ while (true) {
174
+ VirtQueueElement *elem;
175
+ bool ok;
176
+
177
+ if (svq->next_guest_avail_elem) {
178
+ elem = g_steal_pointer(&svq->next_guest_avail_elem);
179
+ } else {
180
+ elem = virtqueue_pop(svq->vq, sizeof(*elem));
181
+ }
182
+
183
+ if (!elem) {
184
+ break;
185
+ }
186
+
187
+ if (elem->out_num + elem->in_num >
188
+ vhost_svq_available_slots(svq)) {
189
+ /*
190
+ * This condition is possible since a contiguous buffer in GPA
191
+ * does not imply a contiguous buffer in qemu's VA
192
+ * scatter-gather segments. If that happens, the buffer exposed
193
+ * to the device needs to be a chain of descriptors at this
194
+ * moment.
195
+ *
196
+ * SVQ cannot hold more available buffers if we are here:
197
+ * queue the current guest descriptor and ignore further kicks
198
+ * until some elements are used.
199
+ */
200
+ svq->next_guest_avail_elem = elem;
201
+ return;
202
+ }
203
+
204
+ ok = vhost_svq_add(svq, elem);
205
+ if (unlikely(!ok)) {
206
+ /* VQ is broken, just return and ignore any other kicks */
207
+ return;
208
+ }
209
+ vhost_svq_kick(svq);
210
+ }
211
+
212
+ virtio_queue_set_notification(svq->vq, true);
213
+ } while (!virtio_queue_empty(svq->vq));
214
+}
215
+
216
+/**
217
+ * Handle guest's kick.
218
*
219
* @n: guest kick event notifier, the one that guest set to notify svq.
220
*/
221
-static void vhost_handle_guest_kick(EventNotifier *n)
222
+static void vhost_handle_guest_kick_notifier(EventNotifier *n)
223
{
224
VhostShadowVirtqueue *svq = container_of(n, VhostShadowVirtqueue,
225
svq_kick);
226
event_notifier_test_and_clear(n);
227
- event_notifier_set(&svq->hdev_kick);
228
+ vhost_handle_guest_kick(svq);
229
+}
230
+
231
+static bool vhost_svq_more_used(VhostShadowVirtqueue *svq)
232
+{
233
+ if (svq->last_used_idx != svq->shadow_used_idx) {
234
+ return true;
235
+ }
236
+
237
+ svq->shadow_used_idx = cpu_to_le16(svq->vring.used->idx);
238
+
239
+ return svq->last_used_idx != svq->shadow_used_idx;
240
}
241
242
/**
243
- * Forward vhost notifications
244
+ * Enable vhost device calls after disable them.
245
+ *
246
+ * @svq: The svq
247
+ *
248
+ * It returns false if there are pending used buffers from the vhost device,
249
+ * avoiding the possible races between SVQ checking for more work and enabling
250
+ * callbacks. True if SVQ used vring has no more pending buffers.
251
+ */
252
+static bool vhost_svq_enable_notification(VhostShadowVirtqueue *svq)
253
+{
254
+ svq->vring.avail->flags &= ~cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT);
255
+ /* Make sure the flag is written before the read of used_idx */
256
+ smp_mb();
257
+ return !vhost_svq_more_used(svq);
258
+}
259
+
260
+static void vhost_svq_disable_notification(VhostShadowVirtqueue *svq)
261
+{
262
+ svq->vring.avail->flags |= cpu_to_le16(VRING_AVAIL_F_NO_INTERRUPT);
263
+}
264
+
265
+static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq,
266
+ uint32_t *len)
267
+{
268
+ vring_desc_t *descs = svq->vring.desc;
269
+ const vring_used_t *used = svq->vring.used;
270
+ vring_used_elem_t used_elem;
271
+ uint16_t last_used;
272
+
273
+ if (!vhost_svq_more_used(svq)) {
274
+ return NULL;
275
+ }
276
+
277
+ /* Only get used array entries after they have been exposed by dev */
278
+ smp_rmb();
279
+ last_used = svq->last_used_idx & (svq->vring.num - 1);
280
+ used_elem.id = le32_to_cpu(used->ring[last_used].id);
281
+ used_elem.len = le32_to_cpu(used->ring[last_used].len);
282
+
283
+ svq->last_used_idx++;
284
+ if (unlikely(used_elem.id >= svq->vring.num)) {
285
+ qemu_log_mask(LOG_GUEST_ERROR, "Device %s says index %u is used",
286
+ svq->vdev->name, used_elem.id);
287
+ return NULL;
288
+ }
289
+
290
+ if (unlikely(!svq->ring_id_maps[used_elem.id])) {
291
+ qemu_log_mask(LOG_GUEST_ERROR,
292
+ "Device %s says index %u is used, but it was not available",
293
+ svq->vdev->name, used_elem.id);
294
+ return NULL;
295
+ }
296
+
297
+ descs[used_elem.id].next = svq->free_head;
298
+ svq->free_head = used_elem.id;
299
+
300
+ *len = used_elem.len;
301
+ return g_steal_pointer(&svq->ring_id_maps[used_elem.id]);
302
+}
303
+
304
+static void vhost_svq_flush(VhostShadowVirtqueue *svq,
305
+ bool check_for_avail_queue)
306
+{
307
+ VirtQueue *vq = svq->vq;
308
+
309
+ /* Forward as many used buffers as possible. */
310
+ do {
311
+ unsigned i = 0;
312
+
313
+ vhost_svq_disable_notification(svq);
314
+ while (true) {
315
+ uint32_t len;
316
+ g_autofree VirtQueueElement *elem = vhost_svq_get_buf(svq, &len);
317
+ if (!elem) {
318
+ break;
319
+ }
320
+
321
+ if (unlikely(i >= svq->vring.num)) {
322
+ qemu_log_mask(LOG_GUEST_ERROR,
323
+ "More than %u used buffers obtained in a %u size SVQ",
324
+ i, svq->vring.num);
325
+ virtqueue_fill(vq, elem, len, i);
326
+ virtqueue_flush(vq, i);
327
+ return;
328
+ }
329
+ virtqueue_fill(vq, elem, len, i++);
330
+ }
331
+
332
+ virtqueue_flush(vq, i);
333
+ event_notifier_set(&svq->svq_call);
334
+
335
+ if (check_for_avail_queue && svq->next_guest_avail_elem) {
336
+ /*
337
+ * Avail ring was full when vhost_svq_flush was called, so it's a
338
+ * good moment to make more descriptors available if possible.
339
+ */
340
+ vhost_handle_guest_kick(svq);
341
+ }
342
+ } while (!vhost_svq_enable_notification(svq));
343
+}
344
+
345
+/**
346
+ * Forward used buffers.
347
*
348
* @n: hdev call event notifier, the one that device set to notify svq.
349
+ *
350
+ * Note that we are not making any buffers available in the loop, there is no
351
+ * way that it runs more than virtqueue size times.
352
*/
353
static void vhost_svq_handle_call(EventNotifier *n)
354
{
355
VhostShadowVirtqueue *svq = container_of(n, VhostShadowVirtqueue,
356
hdev_call);
357
event_notifier_test_and_clear(n);
358
- event_notifier_set(&svq->svq_call);
359
+ vhost_svq_flush(svq, true);
360
}
361
362
/**
363
@@ -XXX,XX +XXX,XX @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)
364
if (poll_start) {
365
event_notifier_init_fd(svq_kick, svq_kick_fd);
366
event_notifier_set(svq_kick);
367
- event_notifier_set_handler(svq_kick, vhost_handle_guest_kick);
368
+ event_notifier_set_handler(svq_kick, vhost_handle_guest_kick_notifier);
369
+ }
370
+}
371
+
372
+/**
373
+ * Start the shadow virtqueue operation.
374
+ *
375
+ * @svq: Shadow Virtqueue
376
+ * @vdev: VirtIO device
377
+ * @vq: Virtqueue to shadow
378
+ */
379
+void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
380
+ VirtQueue *vq)
381
+{
382
+ size_t desc_size, driver_size, device_size;
383
+
384
+ svq->next_guest_avail_elem = NULL;
385
+ svq->shadow_avail_idx = 0;
386
+ svq->shadow_used_idx = 0;
387
+ svq->last_used_idx = 0;
388
+ svq->vdev = vdev;
389
+ svq->vq = vq;
390
+
391
+ svq->vring.num = virtio_queue_get_num(vdev, virtio_get_queue_index(vq));
392
+ driver_size = vhost_svq_driver_area_size(svq);
393
+ device_size = vhost_svq_device_area_size(svq);
394
+ svq->vring.desc = qemu_memalign(qemu_real_host_page_size, driver_size);
395
+ desc_size = sizeof(vring_desc_t) * svq->vring.num;
396
+ svq->vring.avail = (void *)((char *)svq->vring.desc + desc_size);
397
+ memset(svq->vring.desc, 0, driver_size);
398
+ svq->vring.used = qemu_memalign(qemu_real_host_page_size, device_size);
399
+ memset(svq->vring.used, 0, device_size);
400
+ svq->ring_id_maps = g_new0(VirtQueueElement *, svq->vring.num);
401
+ for (unsigned i = 0; i < svq->vring.num - 1; i++) {
402
+ svq->vring.desc[i].next = cpu_to_le16(i + 1);
403
}
404
}
405
406
@@ -XXX,XX +XXX,XX @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd)
407
void vhost_svq_stop(VhostShadowVirtqueue *svq)
408
{
409
event_notifier_set_handler(&svq->svq_kick, NULL);
410
+ g_autofree VirtQueueElement *next_avail_elem = NULL;
411
+
412
+ if (!svq->vq) {
413
+ return;
414
+ }
415
+
416
+ /* Send all pending used descriptors to guest */
417
+ vhost_svq_flush(svq, false);
418
+
419
+ for (unsigned i = 0; i < svq->vring.num; ++i) {
420
+ g_autofree VirtQueueElement *elem = NULL;
421
+ elem = g_steal_pointer(&svq->ring_id_maps[i]);
422
+ if (elem) {
423
+ virtqueue_detach_element(svq->vq, elem, 0);
424
+ }
425
+ }
426
+
427
+ next_avail_elem = g_steal_pointer(&svq->next_guest_avail_elem);
428
+ if (next_avail_elem) {
429
+ virtqueue_detach_element(svq->vq, next_avail_elem, 0);
430
+ }
431
+ svq->vq = NULL;
432
+ g_free(svq->ring_id_maps);
433
+ qemu_vfree(svq->vring.desc);
434
+ qemu_vfree(svq->vring.used);
435
}
436
437
/**
438
diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h
439
index XXXXXXX..XXXXXXX 100644
440
--- a/hw/virtio/vhost-shadow-virtqueue.h
441
+++ b/hw/virtio/vhost-shadow-virtqueue.h
442
@@ -XXX,XX +XXX,XX @@ typedef struct VhostShadowVirtqueue {
443
444
/* Guest's call notifier, where the SVQ calls guest. */
445
EventNotifier svq_call;
446
+
447
+ /* Virtio queue shadowing */
448
+ VirtQueue *vq;
449
+
450
+ /* Virtio device */
451
+ VirtIODevice *vdev;
452
+
453
+ /* Map for use the guest's descriptors */
454
+ VirtQueueElement **ring_id_maps;
455
+
456
+ /* Next VirtQueue element that guest made available */
457
+ VirtQueueElement *next_guest_avail_elem;
458
+
459
+ /* Next head to expose to the device */
460
+ uint16_t shadow_avail_idx;
461
+
462
+ /* Next free descriptor */
463
+ uint16_t free_head;
464
+
465
+ /* Last seen used idx */
466
+ uint16_t shadow_used_idx;
467
+
468
+ /* Next head to consume from the device */
469
+ uint16_t last_used_idx;
470
} VhostShadowVirtqueue;
471
472
bool vhost_svq_valid_features(uint64_t features, Error **errp);
473
@@ -XXX,XX +XXX,XX @@ void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq,
474
size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq);
475
size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq);
476
477
+void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
478
+ VirtQueue *vq);
479
void vhost_svq_stop(VhostShadowVirtqueue *svq);
480
481
VhostShadowVirtqueue *vhost_svq_new(void);
482
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
483
index XXXXXXX..XXXXXXX 100644
484
--- a/hw/virtio/vhost-vdpa.c
485
+++ b/hw/virtio/vhost-vdpa.c
486
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_vring_dev_addr(struct vhost_dev *dev,
487
* Note that this function does not rewind kick file descriptor if cannot set
488
* call one.
489
*/
490
-static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
491
- VhostShadowVirtqueue *svq,
492
- unsigned idx,
493
- Error **errp)
494
+static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev,
495
+ VhostShadowVirtqueue *svq,
496
+ unsigned idx,
497
+ Error **errp)
498
{
499
struct vhost_vring_file file = {
500
.index = dev->vq_index + idx,
501
@@ -XXX,XX +XXX,XX @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
502
r = vhost_vdpa_set_vring_dev_kick(dev, &file);
503
if (unlikely(r != 0)) {
504
error_setg_errno(errp, -r, "Can't set device kick fd");
505
- return false;
506
+ return r;
507
}
508
509
event_notifier = &svq->hdev_call;
510
@@ -XXX,XX +XXX,XX @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
511
error_setg_errno(errp, -r, "Can't set device call fd");
512
}
513
514
+ return r;
515
+}
516
+
517
+/**
518
+ * Unmap a SVQ area in the device
519
+ */
520
+static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, hwaddr iova,
521
+ hwaddr size)
522
+{
523
+ int r;
524
+
525
+ size = ROUND_UP(size, qemu_real_host_page_size);
526
+ r = vhost_vdpa_dma_unmap(v, iova, size);
527
+ return r == 0;
528
+}
529
+
530
+static bool vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,
531
+ const VhostShadowVirtqueue *svq)
532
+{
533
+ struct vhost_vdpa *v = dev->opaque;
534
+ struct vhost_vring_addr svq_addr;
535
+ size_t device_size = vhost_svq_device_area_size(svq);
536
+ size_t driver_size = vhost_svq_driver_area_size(svq);
537
+ bool ok;
538
+
539
+ vhost_svq_get_vring_addr(svq, &svq_addr);
540
+
541
+ ok = vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr, driver_size);
542
+ if (unlikely(!ok)) {
543
+ return false;
544
+ }
545
+
546
+ return vhost_vdpa_svq_unmap_ring(v, svq_addr.used_user_addr, device_size);
547
+}
548
+
549
+/**
550
+ * Map the shadow virtqueue rings in the device
551
+ *
552
+ * @dev: The vhost device
553
+ * @svq: The shadow virtqueue
554
+ * @addr: Assigned IOVA addresses
555
+ * @errp: Error pointer
556
+ */
557
+static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,
558
+ const VhostShadowVirtqueue *svq,
559
+ struct vhost_vring_addr *addr,
560
+ Error **errp)
561
+{
562
+ struct vhost_vdpa *v = dev->opaque;
563
+ size_t device_size = vhost_svq_device_area_size(svq);
564
+ size_t driver_size = vhost_svq_driver_area_size(svq);
565
+ int r;
566
+
567
+ ERRP_GUARD();
568
+ vhost_svq_get_vring_addr(svq, addr);
569
+
570
+ r = vhost_vdpa_dma_map(v, addr->desc_user_addr, driver_size,
571
+ (void *)addr->desc_user_addr, true);
572
+ if (unlikely(r != 0)) {
573
+ error_setg_errno(errp, -r, "Cannot create vq driver region: ");
574
+ return false;
575
+ }
576
+
577
+ r = vhost_vdpa_dma_map(v, addr->used_user_addr, device_size,
578
+ (void *)addr->used_user_addr, false);
579
+ if (unlikely(r != 0)) {
580
+ error_setg_errno(errp, -r, "Cannot create vq device region: ");
581
+ }
582
+
583
+ return r == 0;
584
+}
585
+
586
+static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
587
+ VhostShadowVirtqueue *svq,
588
+ unsigned idx,
589
+ Error **errp)
590
+{
591
+ uint16_t vq_index = dev->vq_index + idx;
592
+ struct vhost_vring_state s = {
593
+ .index = vq_index,
594
+ };
595
+ int r;
596
+
597
+ r = vhost_vdpa_set_dev_vring_base(dev, &s);
598
+ if (unlikely(r)) {
599
+ error_setg_errno(errp, -r, "Cannot set vring base");
600
+ return false;
601
+ }
602
+
603
+ r = vhost_vdpa_svq_set_fds(dev, svq, idx, errp);
604
return r == 0;
605
}
606
607
@@ -XXX,XX +XXX,XX @@ static bool vhost_vdpa_svqs_start(struct vhost_dev *dev)
608
}
609
610
for (i = 0; i < v->shadow_vqs->len; ++i) {
611
+ VirtQueue *vq = virtio_get_queue(dev->vdev, dev->vq_index + i);
612
VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i);
613
+ struct vhost_vring_addr addr = {
614
+ .index = i,
615
+ };
616
+ int r;
617
bool ok = vhost_vdpa_svq_setup(dev, svq, i, &err);
618
if (unlikely(!ok)) {
619
- error_reportf_err(err, "Cannot setup SVQ %u: ", i);
620
+ goto err;
621
+ }
622
+
623
+ vhost_svq_start(svq, dev->vdev, vq);
624
+ ok = vhost_vdpa_svq_map_rings(dev, svq, &addr, &err);
625
+ if (unlikely(!ok)) {
626
+ goto err_map;
627
+ }
628
+
629
+ /* Override vring GPA set by vhost subsystem */
630
+ r = vhost_vdpa_set_vring_dev_addr(dev, &addr);
631
+ if (unlikely(r != 0)) {
632
+ error_setg_errno(&err, -r, "Cannot set device address");
633
+ goto err_set_addr;
634
+ }
635
+ }
636
+
637
+ return true;
638
+
639
+err_set_addr:
640
+ vhost_vdpa_svq_unmap_rings(dev, g_ptr_array_index(v->shadow_vqs, i));
641
+
642
+err_map:
643
+ vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, i));
644
+
645
+err:
646
+ error_reportf_err(err, "Cannot setup SVQ %u: ", i);
647
+ for (unsigned j = 0; j < i; ++j) {
648
+ VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, j);
649
+ vhost_vdpa_svq_unmap_rings(dev, svq);
650
+ vhost_svq_stop(svq);
651
+ }
652
+
653
+ return false;
654
+}
655
+
656
+static bool vhost_vdpa_svqs_stop(struct vhost_dev *dev)
657
+{
658
+ struct vhost_vdpa *v = dev->opaque;
659
+
660
+ if (!v->shadow_vqs) {
661
+ return true;
662
+ }
663
+
664
+ for (unsigned i = 0; i < v->shadow_vqs->len; ++i) {
665
+ VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs,
666
+ i);
667
+ bool ok = vhost_vdpa_svq_unmap_rings(dev, svq);
668
+ if (unlikely(!ok)) {
669
return false;
670
}
671
}
672
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
673
}
674
vhost_vdpa_set_vring_ready(dev);
675
} else {
676
+ ok = vhost_vdpa_svqs_stop(dev);
677
+ if (unlikely(!ok)) {
678
+ return -1;
679
+ }
680
vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
681
}
682
683
--
684
2.7.4
685
686
diff view generated by jsdifflib
New patch
1
1
From: Eugenio Pérez <eperezma@redhat.com>
2
3
This iova tree function allows it to look for a hole in allocated
4
regions and return a totally new translation for a given translated
5
address.
6
7
It's usage is mainly to allow devices to access qemu address space,
8
remapping guest's one into a new iova space where qemu can add chunks of
9
addresses.
10
11
Acked-by: Michael S. Tsirkin <mst@redhat.com>
12
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
13
Reviewed-by: Peter Xu <peterx@redhat.com>
14
Signed-off-by: Jason Wang <jasowang@redhat.com>
15
---
16
include/qemu/iova-tree.h | 18 +++++++
17
util/iova-tree.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++
18
2 files changed, 153 insertions(+)
19
20
diff --git a/include/qemu/iova-tree.h b/include/qemu/iova-tree.h
21
index XXXXXXX..XXXXXXX 100644
22
--- a/include/qemu/iova-tree.h
23
+++ b/include/qemu/iova-tree.h
24
@@ -XXX,XX +XXX,XX @@
25
#define IOVA_OK (0)
26
#define IOVA_ERR_INVALID (-1) /* Invalid parameters */
27
#define IOVA_ERR_OVERLAP (-2) /* IOVA range overlapped */
28
+#define IOVA_ERR_NOMEM (-3) /* Cannot allocate */
29
30
typedef struct IOVATree IOVATree;
31
typedef struct DMAMap {
32
@@ -XXX,XX +XXX,XX @@ const DMAMap *iova_tree_find_address(const IOVATree *tree, hwaddr iova);
33
void iova_tree_foreach(IOVATree *tree, iova_tree_iterator iterator);
34
35
/**
36
+ * iova_tree_alloc_map:
37
+ *
38
+ * @tree: the iova tree to allocate from
39
+ * @map: the new map (as translated addr & size) to allocate in the iova region
40
+ * @iova_begin: the minimum address of the allocation
41
+ * @iova_end: the maximum addressable direction of the allocation
42
+ *
43
+ * Allocates a new region of a given size, between iova_min and iova_max.
44
+ *
45
+ * Return: Same as iova_tree_insert, but cannot overlap and can return error if
46
+ * iova tree is out of free contiguous range. The caller gets the assigned iova
47
+ * in map->iova.
48
+ */
49
+int iova_tree_alloc_map(IOVATree *tree, DMAMap *map, hwaddr iova_begin,
50
+ hwaddr iova_end);
51
+
52
+/**
53
* iova_tree_destroy:
54
*
55
* @tree: the iova tree to destroy
56
diff --git a/util/iova-tree.c b/util/iova-tree.c
57
index XXXXXXX..XXXXXXX 100644
58
--- a/util/iova-tree.c
59
+++ b/util/iova-tree.c
60
@@ -XXX,XX +XXX,XX @@ struct IOVATree {
61
GTree *tree;
62
};
63
64
+/* Args to pass to iova_tree_alloc foreach function. */
65
+struct IOVATreeAllocArgs {
66
+ /* Size of the desired allocation */
67
+ size_t new_size;
68
+
69
+ /* The minimum address allowed in the allocation */
70
+ hwaddr iova_begin;
71
+
72
+ /* Map at the left of the hole, can be NULL if "this" is first one */
73
+ const DMAMap *prev;
74
+
75
+ /* Map at the right of the hole, can be NULL if "prev" is the last one */
76
+ const DMAMap *this;
77
+
78
+ /* If found, we fill in the IOVA here */
79
+ hwaddr iova_result;
80
+
81
+ /* Whether have we found a valid IOVA */
82
+ bool iova_found;
83
+};
84
+
85
+/**
86
+ * Iterate args to the next hole
87
+ *
88
+ * @args: The alloc arguments
89
+ * @next: The next mapping in the tree. Can be NULL to signal the last one
90
+ */
91
+static void iova_tree_alloc_args_iterate(struct IOVATreeAllocArgs *args,
92
+ const DMAMap *next) {
93
+ args->prev = args->this;
94
+ args->this = next;
95
+}
96
+
97
static int iova_tree_compare(gconstpointer a, gconstpointer b, gpointer data)
98
{
99
const DMAMap *m1 = a, *m2 = b;
100
@@ -XXX,XX +XXX,XX @@ int iova_tree_remove(IOVATree *tree, const DMAMap *map)
101
return IOVA_OK;
102
}
103
104
+/**
105
+ * Try to find an unallocated IOVA range between prev and this elements.
106
+ *
107
+ * @args: Arguments to allocation
108
+ *
109
+ * Cases:
110
+ *
111
+ * (1) !prev, !this: No entries allocated, always succeed
112
+ *
113
+ * (2) !prev, this: We're iterating at the 1st element.
114
+ *
115
+ * (3) prev, !this: We're iterating at the last element.
116
+ *
117
+ * (4) prev, this: this is the most common case, we'll try to find a hole
118
+ * between "prev" and "this" mapping.
119
+ *
120
+ * Note that this function assumes the last valid iova is HWADDR_MAX, but it
121
+ * searches linearly so it's easy to discard the result if it's not the case.
122
+ */
123
+static void iova_tree_alloc_map_in_hole(struct IOVATreeAllocArgs *args)
124
+{
125
+ const DMAMap *prev = args->prev, *this = args->this;
126
+ uint64_t hole_start, hole_last;
127
+
128
+ if (this && this->iova + this->size < args->iova_begin) {
129
+ return;
130
+ }
131
+
132
+ hole_start = MAX(prev ? prev->iova + prev->size + 1 : 0, args->iova_begin);
133
+ hole_last = this ? this->iova : HWADDR_MAX;
134
+
135
+ if (hole_last - hole_start > args->new_size) {
136
+ args->iova_result = hole_start;
137
+ args->iova_found = true;
138
+ }
139
+}
140
+
141
+/**
142
+ * Foreach dma node in the tree, compare if there is a hole with its previous
143
+ * node (or minimum iova address allowed) and the node.
144
+ *
145
+ * @key: Node iterating
146
+ * @value: Node iterating
147
+ * @pargs: Struct to communicate with the outside world
148
+ *
149
+ * Return: false to keep iterating, true if needs break.
150
+ */
151
+static gboolean iova_tree_alloc_traverse(gpointer key, gpointer value,
152
+ gpointer pargs)
153
+{
154
+ struct IOVATreeAllocArgs *args = pargs;
155
+ DMAMap *node = value;
156
+
157
+ assert(key == value);
158
+
159
+ iova_tree_alloc_args_iterate(args, node);
160
+ iova_tree_alloc_map_in_hole(args);
161
+ return args->iova_found;
162
+}
163
+
164
+int iova_tree_alloc_map(IOVATree *tree, DMAMap *map, hwaddr iova_begin,
165
+ hwaddr iova_last)
166
+{
167
+ struct IOVATreeAllocArgs args = {
168
+ .new_size = map->size,
169
+ .iova_begin = iova_begin,
170
+ };
171
+
172
+ if (unlikely(iova_last < iova_begin)) {
173
+ return IOVA_ERR_INVALID;
174
+ }
175
+
176
+ /*
177
+ * Find a valid hole for the mapping
178
+ *
179
+ * Assuming low iova_begin, so no need to do a binary search to
180
+ * locate the first node.
181
+ *
182
+ * TODO: Replace all this with g_tree_node_first/next/last when available
183
+ * (from glib since 2.68). To do it with g_tree_foreach complicates the
184
+ * code a lot.
185
+ *
186
+ */
187
+ g_tree_foreach(tree->tree, iova_tree_alloc_traverse, &args);
188
+ if (!args.iova_found) {
189
+ /*
190
+ * Either tree is empty or the last hole is still not checked.
191
+ * g_tree_foreach does not compare (last, iova_last] range, so we check
192
+ * it here.
193
+ */
194
+ iova_tree_alloc_args_iterate(&args, NULL);
195
+ iova_tree_alloc_map_in_hole(&args);
196
+ }
197
+
198
+ if (!args.iova_found || args.iova_result + map->size > iova_last) {
199
+ return IOVA_ERR_NOMEM;
200
+ }
201
+
202
+ map->iova = args.iova_result;
203
+ return iova_tree_insert(tree, map);
204
+}
205
+
206
void iova_tree_destroy(IOVATree *tree)
207
{
208
g_tree_destroy(tree->tree);
209
--
210
2.7.4
211
212
diff view generated by jsdifflib
1
From: Mikhail Sennikovsky <mikhail.sennikovskii@cloud.ionos.com>
1
From: Eugenio Pérez <eperezma@redhat.com>
2
2
3
Currently offloads disabled by guest via the VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
3
This function does the reverse operation of iova_tree_find: To look for
4
command are not preserved on VM migration.
4
a mapping that match a translated address so we can do the reverse.
5
Instead all offloads reported by guest features (via VIRTIO_PCI_GUEST_FEATURES)
6
get enabled.
7
What happens is: first the VirtIONet::curr_guest_offloads gets restored and offloads
8
are getting set correctly:
9
5
10
#0 qemu_set_offload (nc=0x555556a11400, csum=1, tso4=0, tso6=0, ecn=0, ufo=0) at net/net.c:474
6
This have linear complexity instead of logarithmic, but it supports
11
#1 virtio_net_apply_guest_offloads (n=0x555557701ca0) at hw/net/virtio-net.c:720
7
overlapping HVA. Future developments could reduce it.
12
#2 virtio_net_post_load_device (opaque=0x555557701ca0, version_id=11) at hw/net/virtio-net.c:2334
13
#3 vmstate_load_state (f=0x5555569dc010, vmsd=0x555556577c80 <vmstate_virtio_net_device>, opaque=0x555557701ca0, version_id=11)
14
at migration/vmstate.c:168
15
#4 virtio_load (vdev=0x555557701ca0, f=0x5555569dc010, version_id=11) at hw/virtio/virtio.c:2197
16
#5 virtio_device_get (f=0x5555569dc010, opaque=0x555557701ca0, size=0, field=0x55555668cd00 <__compound_literal.5>) at hw/virtio/virtio.c:2036
17
#6 vmstate_load_state (f=0x5555569dc010, vmsd=0x555556577ce0 <vmstate_virtio_net>, opaque=0x555557701ca0, version_id=11) at migration/vmstate.c:143
18
#7 vmstate_load (f=0x5555569dc010, se=0x5555578189e0) at migration/savevm.c:829
19
#8 qemu_loadvm_section_start_full (f=0x5555569dc010, mis=0x5555569eee20) at migration/savevm.c:2211
20
#9 qemu_loadvm_state_main (f=0x5555569dc010, mis=0x5555569eee20) at migration/savevm.c:2395
21
#10 qemu_loadvm_state (f=0x5555569dc010) at migration/savevm.c:2467
22
#11 process_incoming_migration_co (opaque=0x0) at migration/migration.c:449
23
8
24
However later on the features are getting restored, and offloads get reset to
9
Acked-by: Michael S. Tsirkin <mst@redhat.com>
25
everything supported by features:
10
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
26
27
#0 qemu_set_offload (nc=0x555556a11400, csum=1, tso4=1, tso6=1, ecn=0, ufo=0) at net/net.c:474
28
#1 virtio_net_apply_guest_offloads (n=0x555557701ca0) at hw/net/virtio-net.c:720
29
#2 virtio_net_set_features (vdev=0x555557701ca0, features=5104441767) at hw/net/virtio-net.c:773
30
#3 virtio_set_features_nocheck (vdev=0x555557701ca0, val=5104441767) at hw/virtio/virtio.c:2052
31
#4 virtio_load (vdev=0x555557701ca0, f=0x5555569dc010, version_id=11) at hw/virtio/virtio.c:2220
32
#5 virtio_device_get (f=0x5555569dc010, opaque=0x555557701ca0, size=0, field=0x55555668cd00 <__compound_literal.5>) at hw/virtio/virtio.c:2036
33
#6 vmstate_load_state (f=0x5555569dc010, vmsd=0x555556577ce0 <vmstate_virtio_net>, opaque=0x555557701ca0, version_id=11) at migration/vmstate.c:143
34
#7 vmstate_load (f=0x5555569dc010, se=0x5555578189e0) at migration/savevm.c:829
35
#8 qemu_loadvm_section_start_full (f=0x5555569dc010, mis=0x5555569eee20) at migration/savevm.c:2211
36
#9 qemu_loadvm_state_main (f=0x5555569dc010, mis=0x5555569eee20) at migration/savevm.c:2395
37
#10 qemu_loadvm_state (f=0x5555569dc010) at migration/savevm.c:2467
38
#11 process_incoming_migration_co (opaque=0x0) at migration/migration.c:449
39
40
Fix this by preserving the state in saved_guest_offloads field and
41
pushing out offload initialization to the new post load hook.
42
43
Cc: qemu-stable@nongnu.org
44
Signed-off-by: Mikhail Sennikovsky <mikhail.sennikovskii@cloud.ionos.com>
45
Signed-off-by: Jason Wang <jasowang@redhat.com>
11
Signed-off-by: Jason Wang <jasowang@redhat.com>
46
---
12
---
47
hw/net/virtio-net.c | 27 ++++++++++++++++++++++++---
13
include/qemu/iova-tree.h | 20 +++++++++++++++++++-
48
include/hw/virtio/virtio-net.h | 2 ++
14
util/iova-tree.c | 34 ++++++++++++++++++++++++++++++++++
49
2 files changed, 26 insertions(+), 3 deletions(-)
15
2 files changed, 53 insertions(+), 1 deletion(-)
50
16
51
diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
17
diff --git a/include/qemu/iova-tree.h b/include/qemu/iova-tree.h
52
index XXXXXXX..XXXXXXX 100644
18
index XXXXXXX..XXXXXXX 100644
53
--- a/hw/net/virtio-net.c
19
--- a/include/qemu/iova-tree.h
54
+++ b/hw/net/virtio-net.c
20
+++ b/include/qemu/iova-tree.h
55
@@ -XXX,XX +XXX,XX @@ static int virtio_net_post_load_device(void *opaque, int version_id)
21
@@ -XXX,XX +XXX,XX @@ int iova_tree_remove(IOVATree *tree, const DMAMap *map);
56
n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
22
* @tree: the iova tree to search from
57
}
23
* @map: the mapping to search
58
24
*
59
- if (peer_has_vnet_hdr(n)) {
25
- * Search for a mapping in the iova tree that overlaps with the
60
- virtio_net_apply_guest_offloads(n);
26
+ * Search for a mapping in the iova tree that iova overlaps with the
61
- }
27
* mapping range specified. Only the first found mapping will be
62
+ /*
28
* returned.
63
+ * curr_guest_offloads will be later overwritten by the
29
*
64
+ * virtio_set_features_nocheck call done from the virtio_load.
30
@@ -XXX,XX +XXX,XX @@ int iova_tree_remove(IOVATree *tree, const DMAMap *map);
65
+ * Here we make sure it is preserved and restored accordingly
31
const DMAMap *iova_tree_find(const IOVATree *tree, const DMAMap *map);
66
+ * in the virtio_net_post_load_virtio callback.
32
67
+ */
33
/**
68
+ n->saved_guest_offloads = n->curr_guest_offloads;
34
+ * iova_tree_find_iova:
69
35
+ *
70
virtio_net_set_queues(n);
36
+ * @tree: the iova tree to search from
71
37
+ * @map: the mapping to search
72
@@ -XXX,XX +XXX,XX @@ static int virtio_net_post_load_device(void *opaque, int version_id)
38
+ *
73
return 0;
39
+ * Search for a mapping in the iova tree that translated_addr overlaps with the
40
+ * mapping range specified. Only the first found mapping will be
41
+ * returned.
42
+ *
43
+ * Return: DMAMap pointer if found, or NULL if not found. Note that
44
+ * the returned DMAMap pointer is maintained internally. User should
45
+ * only read the content but never modify or free the content. Also,
46
+ * user is responsible to make sure the pointer is valid (say, no
47
+ * concurrent deletion in progress).
48
+ */
49
+const DMAMap *iova_tree_find_iova(const IOVATree *tree, const DMAMap *map);
50
+
51
+/**
52
* iova_tree_find_address:
53
*
54
* @tree: the iova tree to search from
55
diff --git a/util/iova-tree.c b/util/iova-tree.c
56
index XXXXXXX..XXXXXXX 100644
57
--- a/util/iova-tree.c
58
+++ b/util/iova-tree.c
59
@@ -XXX,XX +XXX,XX @@ struct IOVATreeAllocArgs {
60
bool iova_found;
61
};
62
63
+typedef struct IOVATreeFindIOVAArgs {
64
+ const DMAMap *needle;
65
+ const DMAMap *result;
66
+} IOVATreeFindIOVAArgs;
67
+
68
/**
69
* Iterate args to the next hole
70
*
71
@@ -XXX,XX +XXX,XX @@ const DMAMap *iova_tree_find(const IOVATree *tree, const DMAMap *map)
72
return g_tree_lookup(tree->tree, map);
74
}
73
}
75
74
76
+static int virtio_net_post_load_virtio(VirtIODevice *vdev)
75
+static gboolean iova_tree_find_address_iterator(gpointer key, gpointer value,
76
+ gpointer data)
77
+{
77
+{
78
+ VirtIONet *n = VIRTIO_NET(vdev);
78
+ const DMAMap *map = key;
79
+ /*
79
+ IOVATreeFindIOVAArgs *args = data;
80
+ * The actual needed state is now in saved_guest_offloads,
80
+ const DMAMap *needle;
81
+ * see virtio_net_post_load_device for detail.
81
+
82
+ * Restore it back and apply the desired offloads.
82
+ g_assert(key == value);
83
+ */
83
+
84
+ n->curr_guest_offloads = n->saved_guest_offloads;
84
+ needle = args->needle;
85
+ if (peer_has_vnet_hdr(n)) {
85
+ if (map->translated_addr + map->size < needle->translated_addr ||
86
+ virtio_net_apply_guest_offloads(n);
86
+ needle->translated_addr + needle->size < map->translated_addr) {
87
+ return false;
87
+ }
88
+ }
88
+
89
+
89
+ return 0;
90
+ args->result = map;
91
+ return true;
90
+}
92
+}
91
+
93
+
92
/* tx_waiting field of a VirtIONetQueue */
94
+const DMAMap *iova_tree_find_iova(const IOVATree *tree, const DMAMap *map)
93
static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
95
+{
94
.name = "virtio-net-queue-tx_waiting",
96
+ IOVATreeFindIOVAArgs args = {
95
@@ -XXX,XX +XXX,XX @@ static void virtio_net_class_init(ObjectClass *klass, void *data)
97
+ .needle = map,
96
vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
98
+ };
97
vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
99
+
98
vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
100
+ g_tree_foreach(tree->tree, iova_tree_find_address_iterator, &args);
99
+ vdc->post_load = virtio_net_post_load_virtio;
101
+ return args.result;
100
vdc->vmsd = &vmstate_virtio_net_device;
102
+}
101
}
103
+
102
104
const DMAMap *iova_tree_find_address(const IOVATree *tree, hwaddr iova)
103
diff --git a/include/hw/virtio/virtio-net.h b/include/hw/virtio/virtio-net.h
105
{
104
index XXXXXXX..XXXXXXX 100644
106
const DMAMap map = { .iova = iova, .size = 0 };
105
--- a/include/hw/virtio/virtio-net.h
106
+++ b/include/hw/virtio/virtio-net.h
107
@@ -XXX,XX +XXX,XX @@ struct VirtIONet {
108
char *netclient_name;
109
char *netclient_type;
110
uint64_t curr_guest_offloads;
111
+ /* used on saved state restore phase to preserve the curr_guest_offloads */
112
+ uint64_t saved_guest_offloads;
113
AnnounceTimer announce_timer;
114
bool needs_vnet_hdr_swap;
115
bool mtu_bypass_backend;
116
--
107
--
117
2.5.0
108
2.7.4
118
109
119
110
diff view generated by jsdifflib
1
From: Sven Schnelle <svens@stackframe.org>
1
From: Eugenio Pérez <eperezma@redhat.com>
2
2
3
This adds the basic functionality to emulate a Tulip NIC.
3
This tree is able to look for a translated address from an IOVA address.
4
4
5
Implemented are:
5
At first glance it is similar to util/iova-tree. However, SVQ working on
6
devices with limited IOVA space need more capabilities, like allocating
7
IOVA chunks or performing reverse translations (qemu addresses to iova).
6
8
7
- RX and TX functionality
9
The allocation capability, as "assign a free IOVA address to this chunk
8
- Perfect Frame Filtering
10
of memory in qemu's address space" allows shadow virtqueue to create a
9
- Big/Little Endian descriptor support
11
new address space that is not restricted by guest's addressable one, so
10
- 93C46 EEPROM support
12
we can allocate shadow vqs vrings outside of it.
11
- LXT970 PHY
12
13
13
Not implemented, mostly because i had no OS using these functions:
14
It duplicates the tree so it can search efficiently in both directions,
15
and it will signal overlap if iova or the translated address is present
16
in any tree.
14
17
15
- Imperfect frame filtering
18
Acked-by: Michael S. Tsirkin <mst@redhat.com>
16
- General Purpose Timer
19
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
17
- Transmit automatic polling
18
- Boot ROM support
19
- SIA interface
20
- Big/Little Endian data buffer conversion
21
22
Successfully tested with the following Operating Systems:
23
24
- MSDOS with Microsoft Network Client 3.0 and DEC ODI drivers
25
- HPPA Linux
26
- Windows XP
27
- HP-UX
28
29
Signed-off-by: Sven Schnelle <svens@stackframe.org>
30
Message-Id: <20191022155413.4619-1-svens@stackframe.org>
31
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
32
Signed-off-by: Jason Wang <jasowang@redhat.com>
20
Signed-off-by: Jason Wang <jasowang@redhat.com>
33
---
21
---
34
MAINTAINERS | 6 +
22
hw/virtio/meson.build | 2 +-
35
hw/net/Kconfig | 5 +
23
hw/virtio/vhost-iova-tree.c | 110 ++++++++++++++++++++++++++++++++++++++++++++
36
hw/net/Makefile.objs | 1 +
24
hw/virtio/vhost-iova-tree.h | 27 +++++++++++
37
hw/net/trace-events | 14 +
25
3 files changed, 138 insertions(+), 1 deletion(-)
38
hw/net/tulip.c | 1029 ++++++++++++++++++++++++++++++++++++++++++++++
26
create mode 100644 hw/virtio/vhost-iova-tree.c
39
hw/net/tulip.h | 267 ++++++++++++
27
create mode 100644 hw/virtio/vhost-iova-tree.h
40
include/hw/pci/pci_ids.h | 1 +
41
7 files changed, 1323 insertions(+)
42
create mode 100644 hw/net/tulip.c
43
create mode 100644 hw/net/tulip.h
44
28
45
diff --git a/MAINTAINERS b/MAINTAINERS
29
diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build
46
index XXXXXXX..XXXXXXX 100644
30
index XXXXXXX..XXXXXXX 100644
47
--- a/MAINTAINERS
31
--- a/hw/virtio/meson.build
48
+++ b/MAINTAINERS
32
+++ b/hw/virtio/meson.build
49
@@ -XXX,XX +XXX,XX @@ M: Stefan Weil <sw@weilnetz.de>
33
@@ -XXX,XX +XXX,XX @@ softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('vhost-stub.c'))
50
S: Maintained
34
51
F: hw/net/eepro100.c
35
virtio_ss = ss.source_set()
52
36
virtio_ss.add(files('virtio.c'))
53
+tulip
37
-virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c'))
54
+M: Sven Schnelle <svens@stackframe.org>
38
+virtio_ss.add(when: 'CONFIG_VHOST', if_true: files('vhost.c', 'vhost-backend.c', 'vhost-shadow-virtqueue.c', 'vhost-iova-tree.c'))
55
+S: Maintained
39
virtio_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user.c'))
56
+F: hw/net/tulip.c
40
virtio_ss.add(when: 'CONFIG_VHOST_VDPA', if_true: files('vhost-vdpa.c'))
57
+F: hw/net/tulip.h
41
virtio_ss.add(when: 'CONFIG_VIRTIO_BALLOON', if_true: files('virtio-balloon.c'))
58
+
42
diff --git a/hw/virtio/vhost-iova-tree.c b/hw/virtio/vhost-iova-tree.c
59
Generic Loader
60
M: Alistair Francis <alistair@alistair23.me>
61
S: Maintained
62
diff --git a/hw/net/Kconfig b/hw/net/Kconfig
63
index XXXXXXX..XXXXXXX 100644
64
--- a/hw/net/Kconfig
65
+++ b/hw/net/Kconfig
66
@@ -XXX,XX +XXX,XX @@ config PCNET_PCI
67
config PCNET_COMMON
68
bool
69
70
+config TULIP
71
+ bool
72
+ default y if PCI_DEVICES
73
+ depends on PCI
74
+
75
config E1000_PCI
76
bool
77
default y if PCI_DEVICES
78
diff --git a/hw/net/Makefile.objs b/hw/net/Makefile.objs
79
index XXXXXXX..XXXXXXX 100644
80
--- a/hw/net/Makefile.objs
81
+++ b/hw/net/Makefile.objs
82
@@ -XXX,XX +XXX,XX @@ common-obj-$(CONFIG_E1000E_PCI_EXPRESS) += e1000e.o e1000e_core.o e1000x_common.
83
common-obj-$(CONFIG_RTL8139_PCI) += rtl8139.o
84
common-obj-$(CONFIG_VMXNET3_PCI) += net_tx_pkt.o net_rx_pkt.o
85
common-obj-$(CONFIG_VMXNET3_PCI) += vmxnet3.o
86
+common-obj-$(CONFIG_TULIP) += tulip.o
87
88
common-obj-$(CONFIG_SMC91C111) += smc91c111.o
89
common-obj-$(CONFIG_LAN9118) += lan9118.o
90
diff --git a/hw/net/trace-events b/hw/net/trace-events
91
index XXXXXXX..XXXXXXX 100644
92
--- a/hw/net/trace-events
93
+++ b/hw/net/trace-events
94
@@ -XXX,XX +XXX,XX @@ virtio_net_announce_notify(void) ""
95
virtio_net_announce_timer(int round) "%d"
96
virtio_net_handle_announce(int round) "%d"
97
virtio_net_post_load_device(void)
98
+
99
+# tulip.c
100
+tulip_reg_write(uint64_t addr, const char *name, int size, uint64_t val) "addr 0x%02"PRIx64" (%s) size %d value 0x%08"PRIx64
101
+tulip_reg_read(uint64_t addr, const char *name, int size, uint64_t val) "addr 0x%02"PRIx64" (%s) size %d value 0x%08"PRIx64
102
+tulip_receive(const uint8_t *buf, size_t len) "buf %p size %zu"
103
+tulip_descriptor(const char *prefix, uint32_t addr, uint32_t status, uint32_t control, uint32_t len1, uint32_t len2, uint32_t buf1, uint32_t buf2) "%s 0x%08x: status 0x%08x control 0x%03x len1 %4d len2 %4d buf1 0x%08x buf2 0x%08x"
104
+tulip_rx_state(const char *state) "RX %s"
105
+tulip_tx_state(const char *state) "TX %s"
106
+tulip_irq(uint32_t mask, uint32_t en, const char *state) "mask 0x%08x ie 0x%08x %s"
107
+tulip_mii_write(int phy, int reg, uint16_t data) "phy 0x%x reg 0x%x data 0x%04x"
108
+tulip_mii_read(int phy, int reg, uint16_t data) "phy 0x%x, reg 0x%x data 0x%04x"
109
+tulip_reset(void) ""
110
+tulip_setup_frame(void) ""
111
+tulip_setup_filter(int n, uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f) "%d: %02x:%02x:%02x:%02x:%02x:%02x"
112
diff --git a/hw/net/tulip.c b/hw/net/tulip.c
113
new file mode 100644
43
new file mode 100644
114
index XXXXXXX..XXXXXXX
44
index XXXXXXX..XXXXXXX
115
--- /dev/null
45
--- /dev/null
116
+++ b/hw/net/tulip.c
46
+++ b/hw/virtio/vhost-iova-tree.c
117
@@ -XXX,XX +XXX,XX @@
47
@@ -XXX,XX +XXX,XX @@
118
+/*
48
+/*
119
+ * QEMU TULIP Emulation
49
+ * vhost software live migration iova tree
120
+ *
50
+ *
121
+ * Copyright (c) 2019 Sven Schnelle <svens@stackframe.org>
51
+ * SPDX-FileCopyrightText: Red Hat, Inc. 2021
52
+ * SPDX-FileContributor: Author: Eugenio Pérez <eperezma@redhat.com>
122
+ *
53
+ *
123
+ * This work is licensed under the GNU GPL license version 2 or later.
54
+ * SPDX-License-Identifier: GPL-2.0-or-later
124
+ */
55
+ */
125
+
56
+
126
+#include "qemu/osdep.h"
57
+#include "qemu/osdep.h"
127
+#include "qemu/log.h"
58
+#include "qemu/iova-tree.h"
128
+#include "hw/irq.h"
59
+#include "vhost-iova-tree.h"
129
+#include "hw/pci/pci.h"
130
+#include "hw/qdev-properties.h"
131
+#include "hw/nvram/eeprom93xx.h"
132
+#include "migration/vmstate.h"
133
+#include "sysemu/sysemu.h"
134
+#include "tulip.h"
135
+#include "trace.h"
136
+#include "net/eth.h"
137
+
60
+
138
+typedef struct TULIPState {
61
+#define iova_min_addr qemu_real_host_page_size
139
+ PCIDevice dev;
140
+ MemoryRegion io;
141
+ MemoryRegion memory;
142
+ NICConf c;
143
+ qemu_irq irq;
144
+ NICState *nic;
145
+ eeprom_t *eeprom;
146
+ uint32_t csr[16];
147
+
62
+
148
+ /* state for MII */
63
+/**
149
+ uint32_t old_csr9;
64
+ * VhostIOVATree, able to:
150
+ uint32_t mii_word;
65
+ * - Translate iova address
151
+ uint32_t mii_bitcnt;
66
+ * - Reverse translate iova address (from translated to iova)
67
+ * - Allocate IOVA regions for translated range (linear operation)
68
+ */
69
+struct VhostIOVATree {
70
+ /* First addressable iova address in the device */
71
+ uint64_t iova_first;
152
+
72
+
153
+ hwaddr current_rx_desc;
73
+ /* Last addressable iova address in the device */
154
+ hwaddr current_tx_desc;
74
+ uint64_t iova_last;
155
+
75
+
156
+ uint8_t rx_frame[2048];
76
+ /* IOVA address to qemu memory maps. */
157
+ uint8_t tx_frame[2048];
77
+ IOVATree *iova_taddr_map;
158
+ uint16_t tx_frame_len;
159
+ uint16_t rx_frame_len;
160
+ uint16_t rx_frame_size;
161
+
162
+ uint32_t rx_status;
163
+ uint8_t filter[16][6];
164
+} TULIPState;
165
+
166
+static const VMStateDescription vmstate_pci_tulip = {
167
+ .name = "tulip",
168
+ .fields = (VMStateField[]) {
169
+ VMSTATE_PCI_DEVICE(dev, TULIPState),
170
+ VMSTATE_UINT32_ARRAY(csr, TULIPState, 16),
171
+ VMSTATE_UINT32(old_csr9, TULIPState),
172
+ VMSTATE_UINT32(mii_word, TULIPState),
173
+ VMSTATE_UINT32(mii_bitcnt, TULIPState),
174
+ VMSTATE_UINT64(current_rx_desc, TULIPState),
175
+ VMSTATE_UINT64(current_tx_desc, TULIPState),
176
+ VMSTATE_BUFFER(rx_frame, TULIPState),
177
+ VMSTATE_BUFFER(tx_frame, TULIPState),
178
+ VMSTATE_UINT16(rx_frame_len, TULIPState),
179
+ VMSTATE_UINT16(tx_frame_len, TULIPState),
180
+ VMSTATE_UINT16(rx_frame_size, TULIPState),
181
+ VMSTATE_UINT32(rx_status, TULIPState),
182
+ VMSTATE_UINT8_2DARRAY(filter, TULIPState, 16, 6),
183
+ VMSTATE_END_OF_LIST()
184
+ }
185
+};
78
+};
186
+
79
+
187
+static void tulip_desc_read(TULIPState *s, hwaddr p,
80
+/**
188
+ struct tulip_descriptor *desc)
81
+ * Create a new IOVA tree
82
+ *
83
+ * Returns the new IOVA tree
84
+ */
85
+VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwaddr iova_last)
189
+{
86
+{
190
+ if (s->csr[0] & CSR0_DBO) {
87
+ VhostIOVATree *tree = g_new(VhostIOVATree, 1);
191
+ desc->status = ldl_be_pci_dma(&s->dev, p);
88
+
192
+ desc->control = ldl_be_pci_dma(&s->dev, p + 4);
89
+ /* Some devices do not like 0 addresses */
193
+ desc->buf_addr1 = ldl_be_pci_dma(&s->dev, p + 8);
90
+ tree->iova_first = MAX(iova_first, iova_min_addr);
194
+ desc->buf_addr2 = ldl_be_pci_dma(&s->dev, p + 12);
91
+ tree->iova_last = iova_last;
195
+ } else {
92
+
196
+ desc->status = ldl_le_pci_dma(&s->dev, p);
93
+ tree->iova_taddr_map = iova_tree_new();
197
+ desc->control = ldl_le_pci_dma(&s->dev, p + 4);
94
+ return tree;
198
+ desc->buf_addr1 = ldl_le_pci_dma(&s->dev, p + 8);
199
+ desc->buf_addr2 = ldl_le_pci_dma(&s->dev, p + 12);
200
+ }
201
+}
95
+}
202
+
96
+
203
+static void tulip_desc_write(TULIPState *s, hwaddr p,
97
+/**
204
+ struct tulip_descriptor *desc)
98
+ * Delete an iova tree
99
+ */
100
+void vhost_iova_tree_delete(VhostIOVATree *iova_tree)
205
+{
101
+{
206
+ if (s->csr[0] & CSR0_DBO) {
102
+ iova_tree_destroy(iova_tree->iova_taddr_map);
207
+ stl_be_pci_dma(&s->dev, p, desc->status);
103
+ g_free(iova_tree);
208
+ stl_be_pci_dma(&s->dev, p + 4, desc->control);
209
+ stl_be_pci_dma(&s->dev, p + 8, desc->buf_addr1);
210
+ stl_be_pci_dma(&s->dev, p + 12, desc->buf_addr2);
211
+ } else {
212
+ stl_le_pci_dma(&s->dev, p, desc->status);
213
+ stl_le_pci_dma(&s->dev, p + 4, desc->control);
214
+ stl_le_pci_dma(&s->dev, p + 8, desc->buf_addr1);
215
+ stl_le_pci_dma(&s->dev, p + 12, desc->buf_addr2);
216
+ }
217
+}
104
+}
218
+
105
+
219
+static void tulip_update_int(TULIPState *s)
106
+/**
107
+ * Find the IOVA address stored from a memory address
108
+ *
109
+ * @tree: The iova tree
110
+ * @map: The map with the memory address
111
+ *
112
+ * Return the stored mapping, or NULL if not found.
113
+ */
114
+const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *tree,
115
+ const DMAMap *map)
220
+{
116
+{
221
+ uint32_t ie = s->csr[5] & s->csr[7];
117
+ return iova_tree_find_iova(tree->iova_taddr_map, map);
222
+ bool assert = false;
118
+}
223
+
119
+
224
+ s->csr[5] &= ~(CSR5_AIS | CSR5_NIS);
120
+/**
121
+ * Allocate a new mapping
122
+ *
123
+ * @tree: The iova tree
124
+ * @map: The iova map
125
+ *
126
+ * Returns:
127
+ * - IOVA_OK if the map fits in the container
128
+ * - IOVA_ERR_INVALID if the map does not make sense (like size overflow)
129
+ * - IOVA_ERR_NOMEM if tree cannot allocate more space.
130
+ *
131
+ * It returns assignated iova in map->iova if return value is VHOST_DMA_MAP_OK.
132
+ */
133
+int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map)
134
+{
135
+ /* Some vhost devices do not like addr 0. Skip first page */
136
+ hwaddr iova_first = tree->iova_first ?: qemu_real_host_page_size;
225
+
137
+
226
+ if (ie & (CSR5_TI | CSR5_TU | CSR5_RI | CSR5_GTE | CSR5_ERI)) {
138
+ if (map->translated_addr + map->size < map->translated_addr ||
227
+ s->csr[5] |= CSR5_NIS;
139
+ map->perm == IOMMU_NONE) {
140
+ return IOVA_ERR_INVALID;
228
+ }
141
+ }
229
+
142
+
230
+ if (ie & (CSR5_LC | CSR5_GPI | CSR5_FBE | CSR5_LNF | CSR5_ETI | CSR5_RWT |
143
+ /* Allocate a node in IOVA address */
231
+ CSR5_RPS | CSR5_RU | CSR5_UNF | CSR5_LNP_ANC | CSR5_TJT |
144
+ return iova_tree_alloc_map(tree->iova_taddr_map, map, iova_first,
232
+ CSR5_TPS)) {
145
+ tree->iova_last);
233
+ s->csr[5] |= CSR5_AIS;
234
+ }
235
+
236
+ assert = s->csr[5] & s->csr[7] & (CSR5_AIS | CSR5_NIS);
237
+ trace_tulip_irq(s->csr[5], s->csr[7], assert ? "assert" : "deassert");
238
+ qemu_set_irq(s->irq, assert);
239
+}
146
+}
240
+
147
+
241
+static bool tulip_rx_stopped(TULIPState *s)
148
+/**
149
+ * Remove existing mappings from iova tree
150
+ *
151
+ * @iova_tree: The vhost iova tree
152
+ * @map: The map to remove
153
+ */
154
+void vhost_iova_tree_remove(VhostIOVATree *iova_tree, const DMAMap *map)
242
+{
155
+{
243
+ return ((s->csr[5] >> CSR5_RS_SHIFT) & CSR5_RS_MASK) == CSR5_RS_STOPPED;
156
+ iova_tree_remove(iova_tree->iova_taddr_map, map);
244
+}
157
+}
245
+
158
diff --git a/hw/virtio/vhost-iova-tree.h b/hw/virtio/vhost-iova-tree.h
246
+static void tulip_dump_tx_descriptor(TULIPState *s,
247
+ struct tulip_descriptor *desc)
248
+{
249
+ trace_tulip_descriptor("TX ", s->current_tx_desc,
250
+ desc->status, desc->control >> 22,
251
+ desc->control & 0x7ff, (desc->control >> 11) & 0x7ff,
252
+ desc->buf_addr1, desc->buf_addr2);
253
+}
254
+
255
+static void tulip_dump_rx_descriptor(TULIPState *s,
256
+ struct tulip_descriptor *desc)
257
+{
258
+ trace_tulip_descriptor("RX ", s->current_rx_desc,
259
+ desc->status, desc->control >> 22,
260
+ desc->control & 0x7ff, (desc->control >> 11) & 0x7ff,
261
+ desc->buf_addr1, desc->buf_addr2);
262
+}
263
+
264
+static void tulip_next_rx_descriptor(TULIPState *s,
265
+ struct tulip_descriptor *desc)
266
+{
267
+ if (desc->control & RDES1_RER) {
268
+ s->current_rx_desc = s->csr[3];
269
+ } else if (desc->control & RDES1_RCH) {
270
+ s->current_rx_desc = desc->buf_addr2;
271
+ } else {
272
+ s->current_rx_desc += sizeof(struct tulip_descriptor) +
273
+ (((s->csr[0] >> CSR0_DSL_SHIFT) & CSR0_DSL_MASK) << 2);
274
+ }
275
+ s->current_rx_desc &= ~3ULL;
276
+}
277
+
278
+static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc)
279
+{
280
+ int len1 = (desc->control >> RDES1_BUF1_SIZE_SHIFT) & RDES1_BUF1_SIZE_MASK;
281
+ int len2 = (desc->control >> RDES1_BUF2_SIZE_SHIFT) & RDES1_BUF2_SIZE_MASK;
282
+ int len;
283
+
284
+ if (s->rx_frame_len && len1) {
285
+ if (s->rx_frame_len > len1) {
286
+ len = len1;
287
+ } else {
288
+ len = s->rx_frame_len;
289
+ }
290
+ pci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame +
291
+ (s->rx_frame_size - s->rx_frame_len), len);
292
+ s->rx_frame_len -= len;
293
+ }
294
+
295
+ if (s->rx_frame_len && len2) {
296
+ if (s->rx_frame_len > len2) {
297
+ len = len2;
298
+ } else {
299
+ len = s->rx_frame_len;
300
+ }
301
+ pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame +
302
+ (s->rx_frame_size - s->rx_frame_len), len);
303
+ s->rx_frame_len -= len;
304
+ }
305
+}
306
+
307
+static bool tulip_filter_address(TULIPState *s, const uint8_t *addr)
308
+{
309
+ static const char broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
310
+ bool ret = false;
311
+ int i;
312
+
313
+ for (i = 0; i < 16 && ret == false; i++) {
314
+ if (!memcmp(&s->filter[i], addr, ETH_ALEN)) {
315
+ ret = true;
316
+ }
317
+ }
318
+
319
+ if (!memcmp(addr, broadcast, ETH_ALEN)) {
320
+ return true;
321
+ }
322
+
323
+ if (s->csr[6] & (CSR6_PR | CSR6_RA)) {
324
+ /* Promiscuous mode enabled */
325
+ s->rx_status |= RDES0_FF;
326
+ return true;
327
+ }
328
+
329
+ if ((s->csr[6] & CSR6_PM) && (addr[0] & 1)) {
330
+ /* Pass all Multicast enabled */
331
+ s->rx_status |= RDES0_MF;
332
+ return true;
333
+ }
334
+
335
+ if (s->csr[6] & CSR6_IF) {
336
+ ret ^= true;
337
+ }
338
+ return ret;
339
+}
340
+
341
+static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)
342
+{
343
+ struct tulip_descriptor desc;
344
+
345
+ trace_tulip_receive(buf, size);
346
+
347
+ if (size < 14 || size > 2048 || s->rx_frame_len || tulip_rx_stopped(s)) {
348
+ return 0;
349
+ }
350
+
351
+ if (!tulip_filter_address(s, buf)) {
352
+ return size;
353
+ }
354
+
355
+ do {
356
+ tulip_desc_read(s, s->current_rx_desc, &desc);
357
+ tulip_dump_rx_descriptor(s, &desc);
358
+
359
+ if (!(desc.status & RDES0_OWN)) {
360
+ s->csr[5] |= CSR5_RU;
361
+ tulip_update_int(s);
362
+ return s->rx_frame_size - s->rx_frame_len;
363
+ }
364
+ desc.status = 0;
365
+
366
+ if (!s->rx_frame_len) {
367
+ s->rx_frame_size = size + 4;
368
+ s->rx_status = RDES0_LS |
369
+ ((s->rx_frame_size & RDES0_FL_MASK) << RDES0_FL_SHIFT);
370
+ desc.status |= RDES0_FS;
371
+ memcpy(s->rx_frame, buf, size);
372
+ s->rx_frame_len = s->rx_frame_size;
373
+ }
374
+
375
+ tulip_copy_rx_bytes(s, &desc);
376
+
377
+ if (!s->rx_frame_len) {
378
+ desc.status |= s->rx_status;
379
+ s->csr[5] |= CSR5_RI;
380
+ tulip_update_int(s);
381
+ }
382
+ tulip_dump_rx_descriptor(s, &desc);
383
+ tulip_desc_write(s, s->current_rx_desc, &desc);
384
+ tulip_next_rx_descriptor(s, &desc);
385
+ } while (s->rx_frame_len);
386
+ return size;
387
+}
388
+
389
+static ssize_t tulip_receive_nc(NetClientState *nc,
390
+ const uint8_t *buf, size_t size)
391
+{
392
+ return tulip_receive(qemu_get_nic_opaque(nc), buf, size);
393
+}
394
+
395
+
396
+static NetClientInfo net_tulip_info = {
397
+ .type = NET_CLIENT_DRIVER_NIC,
398
+ .size = sizeof(NICState),
399
+ .receive = tulip_receive_nc,
400
+};
401
+
402
+static const char *tulip_reg_name(const hwaddr addr)
403
+{
404
+ switch (addr) {
405
+ case CSR(0):
406
+ return "CSR0";
407
+
408
+ case CSR(1):
409
+ return "CSR1";
410
+
411
+ case CSR(2):
412
+ return "CSR2";
413
+
414
+ case CSR(3):
415
+ return "CSR3";
416
+
417
+ case CSR(4):
418
+ return "CSR4";
419
+
420
+ case CSR(5):
421
+ return "CSR5";
422
+
423
+ case CSR(6):
424
+ return "CSR6";
425
+
426
+ case CSR(7):
427
+ return "CSR7";
428
+
429
+ case CSR(8):
430
+ return "CSR8";
431
+
432
+ case CSR(9):
433
+ return "CSR9";
434
+
435
+ case CSR(10):
436
+ return "CSR10";
437
+
438
+ case CSR(11):
439
+ return "CSR11";
440
+
441
+ case CSR(12):
442
+ return "CSR12";
443
+
444
+ case CSR(13):
445
+ return "CSR13";
446
+
447
+ case CSR(14):
448
+ return "CSR14";
449
+
450
+ case CSR(15):
451
+ return "CSR15";
452
+
453
+ default:
454
+ break;
455
+ }
456
+ return "";
457
+}
458
+
459
+static const char *tulip_rx_state_name(int state)
460
+{
461
+ switch (state) {
462
+ case CSR5_RS_STOPPED:
463
+ return "STOPPED";
464
+
465
+ case CSR5_RS_RUNNING_FETCH:
466
+ return "RUNNING/FETCH";
467
+
468
+ case CSR5_RS_RUNNING_CHECK_EOR:
469
+ return "RUNNING/CHECK EOR";
470
+
471
+ case CSR5_RS_RUNNING_WAIT_RECEIVE:
472
+ return "WAIT RECEIVE";
473
+
474
+ case CSR5_RS_SUSPENDED:
475
+ return "SUSPENDED";
476
+
477
+ case CSR5_RS_RUNNING_CLOSE:
478
+ return "RUNNING/CLOSE";
479
+
480
+ case CSR5_RS_RUNNING_FLUSH:
481
+ return "RUNNING/FLUSH";
482
+
483
+ case CSR5_RS_RUNNING_QUEUE:
484
+ return "RUNNING/QUEUE";
485
+
486
+ default:
487
+ break;
488
+ }
489
+ return "";
490
+}
491
+
492
+static const char *tulip_tx_state_name(int state)
493
+{
494
+ switch (state) {
495
+ case CSR5_TS_STOPPED:
496
+ return "STOPPED";
497
+
498
+ case CSR5_TS_RUNNING_FETCH:
499
+ return "RUNNING/FETCH";
500
+
501
+ case CSR5_TS_RUNNING_WAIT_EOT:
502
+ return "RUNNING/WAIT EOT";
503
+
504
+ case CSR5_TS_RUNNING_READ_BUF:
505
+ return "RUNNING/READ BUF";
506
+
507
+ case CSR5_TS_RUNNING_SETUP:
508
+ return "RUNNING/SETUP";
509
+
510
+ case CSR5_TS_SUSPENDED:
511
+ return "SUSPENDED";
512
+
513
+ case CSR5_TS_RUNNING_CLOSE:
514
+ return "RUNNING/CLOSE";
515
+
516
+ default:
517
+ break;
518
+ }
519
+ return "";
520
+}
521
+
522
+static void tulip_update_rs(TULIPState *s, int state)
523
+{
524
+ s->csr[5] &= ~(CSR5_RS_MASK << CSR5_RS_SHIFT);
525
+ s->csr[5] |= (state & CSR5_RS_MASK) << CSR5_RS_SHIFT;
526
+ trace_tulip_rx_state(tulip_rx_state_name(state));
527
+}
528
+
529
+static uint16_t tulip_mdi_default[] = {
530
+ /* MDI Registers 0 - 6, 7 */
531
+ 0x3100, 0xf02c, 0x7810, 0x0000, 0x0501, 0x4181, 0x0000, 0x0000,
532
+ /* MDI Registers 8 - 15 */
533
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
534
+ /* MDI Registers 16 - 31 */
535
+ 0x0003, 0x0000, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
536
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
537
+};
538
+
539
+/* Readonly mask for MDI (PHY) registers */
540
+static const uint16_t tulip_mdi_mask[] = {
541
+ 0x0000, 0xffff, 0xffff, 0xffff, 0xc01f, 0xffff, 0xffff, 0x0000,
542
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
543
+ 0x0fff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
544
+ 0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
545
+};
546
+
547
+static uint16_t tulip_mii_read(TULIPState *s, int phy, int reg)
548
+{
549
+ uint16_t ret = 0;
550
+ if (phy == 1) {
551
+ ret = tulip_mdi_default[reg];
552
+ }
553
+ trace_tulip_mii_read(phy, reg, ret);
554
+ return ret;
555
+}
556
+
557
+static void tulip_mii_write(TULIPState *s, int phy, int reg, uint16_t data)
558
+{
559
+ trace_tulip_mii_write(phy, reg, data);
560
+
561
+ if (phy != 1) {
562
+ return;
563
+ }
564
+
565
+ tulip_mdi_default[reg] &= ~tulip_mdi_mask[reg];
566
+ tulip_mdi_default[reg] |= (data & tulip_mdi_mask[reg]);
567
+}
568
+
569
+static void tulip_mii(TULIPState *s)
570
+{
571
+ uint32_t changed = s->old_csr9 ^ s->csr[9];
572
+ uint16_t data;
573
+ int op, phy, reg;
574
+
575
+ if (!(changed & CSR9_MDC)) {
576
+ return;
577
+ }
578
+
579
+ if (!(s->csr[9] & CSR9_MDC)) {
580
+ return;
581
+ }
582
+
583
+ s->mii_bitcnt++;
584
+ s->mii_word <<= 1;
585
+
586
+ if (s->csr[9] & CSR9_MDO && (s->mii_bitcnt < 16 ||
587
+ !(s->csr[9] & CSR9_MII))) {
588
+ /* write op or address bits */
589
+ s->mii_word |= 1;
590
+ }
591
+
592
+ if (s->mii_bitcnt >= 16 && (s->csr[9] & CSR9_MII)) {
593
+ if (s->mii_word & 0x8000) {
594
+ s->csr[9] |= CSR9_MDI;
595
+ } else {
596
+ s->csr[9] &= ~CSR9_MDI;
597
+ }
598
+ }
599
+
600
+ if (s->mii_word == 0xffffffff) {
601
+ s->mii_bitcnt = 0;
602
+ } else if (s->mii_bitcnt == 16) {
603
+ op = (s->mii_word >> 12) & 0x0f;
604
+ phy = (s->mii_word >> 7) & 0x1f;
605
+ reg = (s->mii_word >> 2) & 0x1f;
606
+
607
+ if (op == 6) {
608
+ s->mii_word = tulip_mii_read(s, phy, reg);
609
+ }
610
+ } else if (s->mii_bitcnt == 32) {
611
+ op = (s->mii_word >> 28) & 0x0f;
612
+ phy = (s->mii_word >> 23) & 0x1f;
613
+ reg = (s->mii_word >> 18) & 0x1f;
614
+ data = s->mii_word & 0xffff;
615
+
616
+ if (op == 5) {
617
+ tulip_mii_write(s, phy, reg, data);
618
+ }
619
+ }
620
+}
621
+
622
+static uint32_t tulip_csr9_read(TULIPState *s)
623
+{
624
+ if (s->csr[9] & CSR9_SR) {
625
+ if (eeprom93xx_read(s->eeprom)) {
626
+ s->csr[9] |= CSR9_SR_DO;
627
+ } else {
628
+ s->csr[9] &= ~CSR9_SR_DO;
629
+ }
630
+ }
631
+
632
+ tulip_mii(s);
633
+ return s->csr[9];
634
+}
635
+
636
+static void tulip_update_ts(TULIPState *s, int state)
637
+{
638
+ s->csr[5] &= ~(CSR5_TS_MASK << CSR5_TS_SHIFT);
639
+ s->csr[5] |= (state & CSR5_TS_MASK) << CSR5_TS_SHIFT;
640
+ trace_tulip_tx_state(tulip_tx_state_name(state));
641
+}
642
+
643
+static uint64_t tulip_read(void *opaque, hwaddr addr,
644
+ unsigned size)
645
+{
646
+ TULIPState *s = opaque;
647
+ uint64_t data = 0;
648
+
649
+ switch (addr) {
650
+ case CSR(9):
651
+ data = tulip_csr9_read(s);
652
+ break;
653
+
654
+ case CSR(12):
655
+ /* Fake autocompletion complete until we have PHY emulation */
656
+ data = 5 << CSR12_ANS_SHIFT;
657
+ break;
658
+
659
+ default:
660
+ if (addr & 7) {
661
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: read access at unknown address"
662
+ " 0x%"PRIx64"\n", __func__, addr);
663
+ } else {
664
+ data = s->csr[addr >> 3];
665
+ }
666
+ break;
667
+ }
668
+ trace_tulip_reg_read(addr, tulip_reg_name(addr), size, data);
669
+ return data;
670
+}
671
+
672
+static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc)
673
+{
674
+ if (s->tx_frame_len) {
675
+ if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) {
676
+ /* Internal or external Loopback */
677
+ tulip_receive(s, s->tx_frame, s->tx_frame_len);
678
+ } else {
679
+ qemu_send_packet(qemu_get_queue(s->nic),
680
+ s->tx_frame, s->tx_frame_len);
681
+ }
682
+ }
683
+
684
+ if (desc->control & TDES1_IC) {
685
+ s->csr[5] |= CSR5_TI;
686
+ tulip_update_int(s);
687
+ }
688
+}
689
+
690
+static void tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc)
691
+{
692
+ int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK;
693
+ int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT) & TDES1_BUF2_SIZE_MASK;
694
+
695
+ if (len1) {
696
+ pci_dma_read(&s->dev, desc->buf_addr1,
697
+ s->tx_frame + s->tx_frame_len, len1);
698
+ s->tx_frame_len += len1;
699
+ }
700
+
701
+ if (len2) {
702
+ pci_dma_read(&s->dev, desc->buf_addr2,
703
+ s->tx_frame + s->tx_frame_len, len2);
704
+ s->tx_frame_len += len2;
705
+ }
706
+ desc->status = (len1 + len2) ? 0 : 0x7fffffff;
707
+}
708
+
709
+static void tulip_setup_filter_addr(TULIPState *s, uint8_t *buf, int n)
710
+{
711
+ int offset = n * 12;
712
+
713
+ s->filter[n][0] = buf[offset];
714
+ s->filter[n][1] = buf[offset + 1];
715
+
716
+ s->filter[n][2] = buf[offset + 4];
717
+ s->filter[n][3] = buf[offset + 5];
718
+
719
+ s->filter[n][4] = buf[offset + 8];
720
+ s->filter[n][5] = buf[offset + 9];
721
+
722
+ trace_tulip_setup_filter(n, s->filter[n][5], s->filter[n][4],
723
+ s->filter[n][3], s->filter[n][2], s->filter[n][1], s->filter[n][0]);
724
+}
725
+
726
+static void tulip_setup_frame(TULIPState *s,
727
+ struct tulip_descriptor *desc)
728
+{
729
+ uint8_t buf[4096];
730
+ int len = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK;
731
+ int i;
732
+
733
+ trace_tulip_setup_frame();
734
+
735
+ if (len == 192) {
736
+ pci_dma_read(&s->dev, desc->buf_addr1, buf, len);
737
+ for (i = 0; i < 16; i++) {
738
+ tulip_setup_filter_addr(s, buf, i);
739
+ }
740
+ }
741
+
742
+ desc->status = 0x7fffffff;
743
+
744
+ if (desc->control & TDES1_IC) {
745
+ s->csr[5] |= CSR5_TI;
746
+ tulip_update_int(s);
747
+ }
748
+}
749
+
750
+static void tulip_next_tx_descriptor(TULIPState *s,
751
+ struct tulip_descriptor *desc)
752
+{
753
+ if (desc->control & TDES1_TER) {
754
+ s->current_tx_desc = s->csr[4];
755
+ } else if (desc->control & TDES1_TCH) {
756
+ s->current_tx_desc = desc->buf_addr2;
757
+ } else {
758
+ s->current_tx_desc += sizeof(struct tulip_descriptor) +
759
+ (((s->csr[0] >> CSR0_DSL_SHIFT) & CSR0_DSL_MASK) << 2);
760
+ }
761
+ s->current_tx_desc &= ~3ULL;
762
+}
763
+
764
+static uint32_t tulip_ts(TULIPState *s)
765
+{
766
+ return (s->csr[5] >> CSR5_TS_SHIFT) & CSR5_TS_MASK;
767
+}
768
+
769
+static void tulip_xmit_list_update(TULIPState *s)
770
+{
771
+ struct tulip_descriptor desc;
772
+
773
+ if (tulip_ts(s) != CSR5_TS_SUSPENDED) {
774
+ return;
775
+ }
776
+
777
+ for (;;) {
778
+ tulip_desc_read(s, s->current_tx_desc, &desc);
779
+ tulip_dump_tx_descriptor(s, &desc);
780
+
781
+ if (!(desc.status & TDES0_OWN)) {
782
+ tulip_update_ts(s, CSR5_TS_SUSPENDED);
783
+ s->csr[5] |= CSR5_TU;
784
+ tulip_update_int(s);
785
+ return;
786
+ }
787
+
788
+ if (desc.control & TDES1_SET) {
789
+ tulip_setup_frame(s, &desc);
790
+ } else {
791
+ if (desc.control & TDES1_FS) {
792
+ s->tx_frame_len = 0;
793
+ }
794
+
795
+ tulip_copy_tx_buffers(s, &desc);
796
+
797
+ if (desc.control & TDES1_LS) {
798
+ tulip_tx(s, &desc);
799
+ }
800
+ }
801
+ tulip_desc_write(s, s->current_tx_desc, &desc);
802
+ tulip_next_tx_descriptor(s, &desc);
803
+ }
804
+}
805
+
806
+static void tulip_csr9_write(TULIPState *s, uint32_t old_val,
807
+ uint32_t new_val)
808
+{
809
+ if (new_val & CSR9_SR) {
810
+ eeprom93xx_write(s->eeprom,
811
+ !!(new_val & CSR9_SR_CS),
812
+ !!(new_val & CSR9_SR_SK),
813
+ !!(new_val & CSR9_SR_DI));
814
+ }
815
+}
816
+
817
+static void tulip_reset(TULIPState *s)
818
+{
819
+ trace_tulip_reset();
820
+
821
+ s->csr[0] = 0xfe000000;
822
+ s->csr[1] = 0xffffffff;
823
+ s->csr[2] = 0xffffffff;
824
+ s->csr[5] = 0xf0000000;
825
+ s->csr[6] = 0x32000040;
826
+ s->csr[7] = 0xf3fe0000;
827
+ s->csr[8] = 0xe0000000;
828
+ s->csr[9] = 0xfff483ff;
829
+ s->csr[11] = 0xfffe0000;
830
+ s->csr[12] = 0x000000c6;
831
+ s->csr[13] = 0xffff0000;
832
+ s->csr[14] = 0xffffffff;
833
+ s->csr[15] = 0x8ff00000;
834
+}
835
+
836
+static void tulip_qdev_reset(DeviceState *dev)
837
+{
838
+ PCIDevice *d = PCI_DEVICE(dev);
839
+ TULIPState *s = TULIP(d);
840
+
841
+ tulip_reset(s);
842
+}
843
+
844
+static void tulip_write(void *opaque, hwaddr addr,
845
+ uint64_t data, unsigned size)
846
+{
847
+ TULIPState *s = opaque;
848
+ trace_tulip_reg_write(addr, tulip_reg_name(addr), size, data);
849
+
850
+ switch (addr) {
851
+ case CSR(0):
852
+ s->csr[0] = data;
853
+ if (data & CSR0_SWR) {
854
+ tulip_reset(s);
855
+ tulip_update_int(s);
856
+ }
857
+ break;
858
+
859
+ case CSR(1):
860
+ tulip_xmit_list_update(s);
861
+ break;
862
+
863
+ case CSR(2):
864
+ qemu_flush_queued_packets(qemu_get_queue(s->nic));
865
+ break;
866
+
867
+ case CSR(3):
868
+ s->csr[3] = data & ~3ULL;
869
+ s->current_rx_desc = s->csr[3];
870
+ qemu_flush_queued_packets(qemu_get_queue(s->nic));
871
+ break;
872
+
873
+ case CSR(4):
874
+ s->csr[4] = data & ~3ULL;
875
+ s->current_tx_desc = s->csr[4];
876
+ tulip_xmit_list_update(s);
877
+ break;
878
+
879
+ case CSR(5):
880
+ /* Status register, write clears bit */
881
+ s->csr[5] &= ~(data & (CSR5_TI | CSR5_TPS | CSR5_TU | CSR5_TJT |
882
+ CSR5_LNP_ANC | CSR5_UNF | CSR5_RI | CSR5_RU |
883
+ CSR5_RPS | CSR5_RWT | CSR5_ETI | CSR5_GTE |
884
+ CSR5_LNF | CSR5_FBE | CSR5_ERI | CSR5_AIS |
885
+ CSR5_NIS | CSR5_GPI | CSR5_LC));
886
+ tulip_update_int(s);
887
+ break;
888
+
889
+ case CSR(6):
890
+ s->csr[6] = data;
891
+ if (s->csr[6] & CSR6_SR) {
892
+ tulip_update_rs(s, CSR5_RS_RUNNING_WAIT_RECEIVE);
893
+ qemu_flush_queued_packets(qemu_get_queue(s->nic));
894
+ } else {
895
+ tulip_update_rs(s, CSR5_RS_STOPPED);
896
+ }
897
+
898
+ if (s->csr[6] & CSR6_ST) {
899
+ tulip_update_ts(s, CSR5_TS_SUSPENDED);
900
+ tulip_xmit_list_update(s);
901
+ } else {
902
+ tulip_update_ts(s, CSR5_TS_STOPPED);
903
+ }
904
+ break;
905
+
906
+ case CSR(7):
907
+ s->csr[7] = data;
908
+ tulip_update_int(s);
909
+ break;
910
+
911
+ case CSR(8):
912
+ s->csr[9] = data;
913
+ break;
914
+
915
+ case CSR(9):
916
+ tulip_csr9_write(s, s->csr[9], data);
917
+ /* don't clear MII read data */
918
+ s->csr[9] &= CSR9_MDI;
919
+ s->csr[9] |= (data & ~CSR9_MDI);
920
+ tulip_mii(s);
921
+ s->old_csr9 = s->csr[9];
922
+ break;
923
+
924
+ case CSR(10):
925
+ s->csr[10] = data;
926
+ break;
927
+
928
+ case CSR(11):
929
+ s->csr[11] = data;
930
+ break;
931
+
932
+ case CSR(12):
933
+ /* SIA Status register, some bits are cleared by writing 1 */
934
+ s->csr[12] &= ~(data & (CSR12_MRA | CSR12_TRA | CSR12_ARA));
935
+ break;
936
+
937
+ case CSR(13):
938
+ s->csr[13] = data;
939
+ break;
940
+
941
+ case CSR(14):
942
+ s->csr[14] = data;
943
+ break;
944
+
945
+ case CSR(15):
946
+ s->csr[15] = data;
947
+ break;
948
+
949
+ default:
950
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: write to CSR at unknown address "
951
+ "0x%"PRIx64"\n", __func__, addr);
952
+ break;
953
+ }
954
+}
955
+
956
+static const MemoryRegionOps tulip_ops = {
957
+ .read = tulip_read,
958
+ .write = tulip_write,
959
+ .endianness = DEVICE_LITTLE_ENDIAN,
960
+ .impl = {
961
+ .min_access_size = 4,
962
+ .max_access_size = 4,
963
+ },
964
+};
965
+
966
+static void tulip_idblock_crc(TULIPState *s, uint16_t *srom)
967
+{
968
+ int word, n;
969
+ int bit;
970
+ unsigned char bitval, crc;
971
+ const int len = 9;
972
+ n = 0;
973
+ crc = -1;
974
+
975
+ for (word = 0; word < len; word++) {
976
+ for (bit = 15; bit >= 0; bit--) {
977
+ if ((word == (len - 1)) && (bit == 7)) {
978
+ /*
979
+ * Insert the correct CRC result into input data stream
980
+ * in place.
981
+ */
982
+ srom[len - 1] = (srom[len - 1] & 0xff00) | (unsigned short)crc;
983
+ break;
984
+ }
985
+ n++;
986
+ bitval = ((srom[word] >> bit) & 1) ^ ((crc >> 7) & 1);
987
+ crc = crc << 1;
988
+ if (bitval == 1) {
989
+ crc ^= 6;
990
+ crc |= 0x01;
991
+ }
992
+ }
993
+ }
994
+}
995
+
996
+static uint16_t tulip_srom_crc(TULIPState *s, uint8_t *eeprom, size_t len)
997
+{
998
+ unsigned long crc = 0xffffffff;
999
+ unsigned long flippedcrc = 0;
1000
+ unsigned char currentbyte;
1001
+ unsigned int msb, bit, i;
1002
+
1003
+ for (i = 0; i < len; i++) {
1004
+ currentbyte = eeprom[i];
1005
+ for (bit = 0; bit < 8; bit++) {
1006
+ msb = (crc >> 31) & 1;
1007
+ crc <<= 1;
1008
+ if (msb ^ (currentbyte & 1)) {
1009
+ crc ^= 0x04c11db6;
1010
+ crc |= 0x00000001;
1011
+ }
1012
+ currentbyte >>= 1;
1013
+ }
1014
+ }
1015
+
1016
+ for (i = 0; i < 32; i++) {
1017
+ flippedcrc <<= 1;
1018
+ bit = crc & 1;
1019
+ crc >>= 1;
1020
+ flippedcrc += bit;
1021
+ }
1022
+ return (flippedcrc ^ 0xffffffff) & 0xffff;
1023
+}
1024
+
1025
+static const uint8_t eeprom_default[128] = {
1026
+ 0x3c, 0x10, 0x4f, 0x10, 0x00, 0x00, 0x00, 0x00,
1027
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1028
+ 0x56, 0x08, 0x04, 0x01, 0x00, 0x80, 0x48, 0xb3,
1029
+ 0x0e, 0xa7, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x08,
1030
+ 0x01, 0x8d, 0x03, 0x00, 0x00, 0x00, 0x00, 0x78,
1031
+ 0xe0, 0x01, 0x00, 0x50, 0x00, 0x18, 0x00, 0x00,
1032
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1033
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1034
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1035
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1036
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1037
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe8, 0x6b,
1038
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
1039
+ 0x48, 0xb3, 0x0e, 0xa7, 0x40, 0x00, 0x00, 0x00,
1040
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1041
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1042
+};
1043
+
1044
+static void tulip_fill_eeprom(TULIPState *s)
1045
+{
1046
+ uint16_t *eeprom = eeprom93xx_data(s->eeprom);
1047
+ memcpy(eeprom, eeprom_default, 128);
1048
+
1049
+ /* patch in our mac address */
1050
+ eeprom[10] = cpu_to_le16(s->c.macaddr.a[0] | (s->c.macaddr.a[1] << 8));
1051
+ eeprom[11] = cpu_to_le16(s->c.macaddr.a[2] | (s->c.macaddr.a[3] << 8));
1052
+ eeprom[12] = cpu_to_le16(s->c.macaddr.a[4] | (s->c.macaddr.a[5] << 8));
1053
+ tulip_idblock_crc(s, eeprom);
1054
+ eeprom[63] = cpu_to_le16(tulip_srom_crc(s, (uint8_t *)eeprom, 126));
1055
+}
1056
+
1057
+static void pci_tulip_realize(PCIDevice *pci_dev, Error **errp)
1058
+{
1059
+ TULIPState *s = DO_UPCAST(TULIPState, dev, pci_dev);
1060
+ uint8_t *pci_conf;
1061
+
1062
+ pci_conf = s->dev.config;
1063
+ pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
1064
+
1065
+ s->eeprom = eeprom93xx_new(&pci_dev->qdev, 64);
1066
+ tulip_fill_eeprom(s);
1067
+
1068
+ memory_region_init_io(&s->io, OBJECT(&s->dev), &tulip_ops, s,
1069
+ "tulip-io", 128);
1070
+
1071
+ memory_region_init_io(&s->memory, OBJECT(&s->dev), &tulip_ops, s,
1072
+ "tulip-mem", 128);
1073
+
1074
+ pci_register_bar(&s->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
1075
+ pci_register_bar(&s->dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->memory);
1076
+
1077
+ s->irq = pci_allocate_irq(&s->dev);
1078
+
1079
+ qemu_macaddr_default_if_unset(&s->c.macaddr);
1080
+
1081
+ s->nic = qemu_new_nic(&net_tulip_info, &s->c,
1082
+ object_get_typename(OBJECT(pci_dev)),
1083
+ pci_dev->qdev.id, s);
1084
+ qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a);
1085
+}
1086
+
1087
+static void pci_tulip_exit(PCIDevice *pci_dev)
1088
+{
1089
+ TULIPState *s = DO_UPCAST(TULIPState, dev, pci_dev);
1090
+
1091
+ qemu_del_nic(s->nic);
1092
+ qemu_free_irq(s->irq);
1093
+ eeprom93xx_free(&pci_dev->qdev, s->eeprom);
1094
+}
1095
+
1096
+static void tulip_instance_init(Object *obj)
1097
+{
1098
+ PCIDevice *pci_dev = PCI_DEVICE(obj);
1099
+ TULIPState *d = DO_UPCAST(TULIPState, dev, pci_dev);
1100
+
1101
+ device_add_bootindex_property(obj, &d->c.bootindex,
1102
+ "bootindex", "/ethernet-phy@0",
1103
+ &pci_dev->qdev, NULL);
1104
+}
1105
+
1106
+static Property tulip_properties[] = {
1107
+ DEFINE_NIC_PROPERTIES(TULIPState, c),
1108
+ DEFINE_PROP_END_OF_LIST(),
1109
+};
1110
+
1111
+static void tulip_class_init(ObjectClass *klass, void *data)
1112
+{
1113
+ DeviceClass *dc = DEVICE_CLASS(klass);
1114
+ PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1115
+
1116
+ k->realize = pci_tulip_realize;
1117
+ k->exit = pci_tulip_exit;
1118
+ k->vendor_id = PCI_VENDOR_ID_DEC;
1119
+ k->device_id = PCI_DEVICE_ID_DEC_21143;
1120
+ k->subsystem_vendor_id = 0x103c;
1121
+ k->subsystem_id = 0x104f;
1122
+ k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1123
+ dc->vmsd = &vmstate_pci_tulip;
1124
+ dc->props = tulip_properties;
1125
+ dc->reset = tulip_qdev_reset;
1126
+ set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1127
+}
1128
+
1129
+static const TypeInfo tulip_info = {
1130
+ .name = TYPE_TULIP,
1131
+ .parent = TYPE_PCI_DEVICE,
1132
+ .instance_size = sizeof(TULIPState),
1133
+ .class_init = tulip_class_init,
1134
+ .instance_init = tulip_instance_init,
1135
+ .interfaces = (InterfaceInfo[]) {
1136
+ { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1137
+ { },
1138
+ },
1139
+};
1140
+
1141
+static void tulip_register_types(void)
1142
+{
1143
+ type_register_static(&tulip_info);
1144
+}
1145
+
1146
+type_init(tulip_register_types)
1147
diff --git a/hw/net/tulip.h b/hw/net/tulip.h
1148
new file mode 100644
159
new file mode 100644
1149
index XXXXXXX..XXXXXXX
160
index XXXXXXX..XXXXXXX
1150
--- /dev/null
161
--- /dev/null
1151
+++ b/hw/net/tulip.h
162
+++ b/hw/virtio/vhost-iova-tree.h
1152
@@ -XXX,XX +XXX,XX @@
163
@@ -XXX,XX +XXX,XX @@
1153
+#ifndef HW_TULIP_H
164
+/*
1154
+#define HW_TULIP_H
165
+ * vhost software live migration iova tree
166
+ *
167
+ * SPDX-FileCopyrightText: Red Hat, Inc. 2021
168
+ * SPDX-FileContributor: Author: Eugenio Pérez <eperezma@redhat.com>
169
+ *
170
+ * SPDX-License-Identifier: GPL-2.0-or-later
171
+ */
1155
+
172
+
1156
+#include "qemu/units.h"
173
+#ifndef HW_VIRTIO_VHOST_IOVA_TREE_H
1157
+#include "net/net.h"
174
+#define HW_VIRTIO_VHOST_IOVA_TREE_H
1158
+
175
+
1159
+#define TYPE_TULIP "tulip"
176
+#include "qemu/iova-tree.h"
1160
+#define TULIP(obj) OBJECT_CHECK(TULIPState, (obj), TYPE_TULIP)
177
+#include "exec/memory.h"
1161
+
178
+
1162
+#define CSR(_x) ((_x) << 3)
179
+typedef struct VhostIOVATree VhostIOVATree;
1163
+
180
+
1164
+#define CSR0_SWR BIT(0)
181
+VhostIOVATree *vhost_iova_tree_new(uint64_t iova_first, uint64_t iova_last);
1165
+#define CSR0_BAR BIT(1)
182
+void vhost_iova_tree_delete(VhostIOVATree *iova_tree);
1166
+#define CSR0_DSL_SHIFT 2
183
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(VhostIOVATree, vhost_iova_tree_delete);
1167
+#define CSR0_DSL_MASK 0x1f
1168
+#define CSR0_BLE BIT(7)
1169
+#define CSR0_PBL_SHIFT 8
1170
+#define CSR0_PBL_MASK 0x3f
1171
+#define CSR0_CAC_SHIFT 14
1172
+#define CSR0_CAC_MASK 0x3
1173
+#define CSR0_DAS 0x10000
1174
+#define CSR0_TAP_SHIFT 17
1175
+#define CSR0_TAP_MASK 0x7
1176
+#define CSR0_DBO 0x100000
1177
+#define CSR1_TPD 0x01
1178
+#define CSR0_RLE BIT(23)
1179
+#define CSR0_WIE BIT(24)
1180
+
184
+
1181
+#define CSR2_RPD 0x01
185
+const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree,
1182
+
186
+ const DMAMap *map);
1183
+#define CSR5_TI BIT(0)
187
+int vhost_iova_tree_map_alloc(VhostIOVATree *iova_tree, DMAMap *map);
1184
+#define CSR5_TPS BIT(1)
188
+void vhost_iova_tree_remove(VhostIOVATree *iova_tree, const DMAMap *map);
1185
+#define CSR5_TU BIT(2)
1186
+#define CSR5_TJT BIT(3)
1187
+#define CSR5_LNP_ANC BIT(4)
1188
+#define CSR5_UNF BIT(5)
1189
+#define CSR5_RI BIT(6)
1190
+#define CSR5_RU BIT(7)
1191
+#define CSR5_RPS BIT(8)
1192
+#define CSR5_RWT BIT(9)
1193
+#define CSR5_ETI BIT(10)
1194
+#define CSR5_GTE BIT(11)
1195
+#define CSR5_LNF BIT(12)
1196
+#define CSR5_FBE BIT(13)
1197
+#define CSR5_ERI BIT(14)
1198
+#define CSR5_AIS BIT(15)
1199
+#define CSR5_NIS BIT(16)
1200
+#define CSR5_RS_SHIFT 17
1201
+#define CSR5_RS_MASK 7
1202
+#define CSR5_TS_SHIFT 20
1203
+#define CSR5_TS_MASK 7
1204
+
1205
+#define CSR5_TS_STOPPED 0
1206
+#define CSR5_TS_RUNNING_FETCH 1
1207
+#define CSR5_TS_RUNNING_WAIT_EOT 2
1208
+#define CSR5_TS_RUNNING_READ_BUF 3
1209
+#define CSR5_TS_RUNNING_SETUP 5
1210
+#define CSR5_TS_SUSPENDED 6
1211
+#define CSR5_TS_RUNNING_CLOSE 7
1212
+
1213
+#define CSR5_RS_STOPPED 0
1214
+#define CSR5_RS_RUNNING_FETCH 1
1215
+#define CSR5_RS_RUNNING_CHECK_EOR 2
1216
+#define CSR5_RS_RUNNING_WAIT_RECEIVE 3
1217
+#define CSR5_RS_SUSPENDED 4
1218
+#define CSR5_RS_RUNNING_CLOSE 5
1219
+#define CSR5_RS_RUNNING_FLUSH 6
1220
+#define CSR5_RS_RUNNING_QUEUE 7
1221
+
1222
+#define CSR5_EB_SHIFT 23
1223
+#define CSR5_EB_MASK 7
1224
+
1225
+#define CSR5_GPI BIT(26)
1226
+#define CSR5_LC BIT(27)
1227
+
1228
+#define CSR6_HP BIT(0)
1229
+#define CSR6_SR BIT(1)
1230
+#define CSR6_HO BIT(2)
1231
+#define CSR6_PB BIT(3)
1232
+#define CSR6_IF BIT(4)
1233
+#define CSR6_SB BIT(5)
1234
+#define CSR6_PR BIT(6)
1235
+#define CSR6_PM BIT(7)
1236
+#define CSR6_FKD BIT(8)
1237
+#define CSR6_FD BIT(9)
1238
+
1239
+#define CSR6_OM_SHIFT 10
1240
+#define CSR6_OM_MASK 3
1241
+#define CSR6_OM_NORMAL 0
1242
+#define CSR6_OM_INT_LOOPBACK 1
1243
+#define CSR6_OM_EXT_LOOPBACK 2
1244
+
1245
+#define CSR6_FC BIT(12)
1246
+#define CSR6_ST BIT(13)
1247
+
1248
+
1249
+#define CSR6_TR_SHIFT 14
1250
+#define CSR6_TR_MASK 3
1251
+#define CSR6_TR_72 0
1252
+#define CSR6_TR_96 1
1253
+#define CSR6_TR_128 2
1254
+#define CSR6_TR_160 3
1255
+
1256
+#define CSR6_CA BIT(17)
1257
+#define CSR6_RA BIT(30)
1258
+#define CSR6_SC BIT(31)
1259
+
1260
+#define CSR7_TIM BIT(0)
1261
+#define CSR7_TSM BIT(1)
1262
+#define CSR7_TUM BIT(2)
1263
+#define CSR7_TJM BIT(3)
1264
+#define CSR7_LPM BIT(4)
1265
+#define CSR7_UNM BIT(5)
1266
+#define CSR7_RIM BIT(6)
1267
+#define CSR7_RUM BIT(7)
1268
+#define CSR7_RSM BIT(8)
1269
+#define CSR7_RWM BIT(9)
1270
+#define CSR7_TMM BIT(11)
1271
+#define CSR7_LFM BIT(12)
1272
+#define CSR7_SEM BIT(13)
1273
+#define CSR7_ERM BIT(14)
1274
+#define CSR7_AIM BIT(15)
1275
+#define CSR7_NIM BIT(16)
1276
+
1277
+#define CSR8_MISSED_FRAME_OVL BIT(16)
1278
+#define CSR8_MISSED_FRAME_CNT_MASK 0xffff
1279
+
1280
+#define CSR9_DATA_MASK 0xff
1281
+#define CSR9_SR_CS BIT(0)
1282
+#define CSR9_SR_SK BIT(1)
1283
+#define CSR9_SR_DI BIT(2)
1284
+#define CSR9_SR_DO BIT(3)
1285
+#define CSR9_REG BIT(10)
1286
+#define CSR9_SR BIT(11)
1287
+#define CSR9_BR BIT(12)
1288
+#define CSR9_WR BIT(13)
1289
+#define CSR9_RD BIT(14)
1290
+#define CSR9_MOD BIT(15)
1291
+#define CSR9_MDC BIT(16)
1292
+#define CSR9_MDO BIT(17)
1293
+#define CSR9_MII BIT(18)
1294
+#define CSR9_MDI BIT(19)
1295
+
1296
+#define CSR11_CON BIT(16)
1297
+#define CSR11_TIMER_MASK 0xffff
1298
+
1299
+#define CSR12_MRA BIT(0)
1300
+#define CSR12_LS100 BIT(1)
1301
+#define CSR12_LS10 BIT(2)
1302
+#define CSR12_APS BIT(3)
1303
+#define CSR12_ARA BIT(8)
1304
+#define CSR12_TRA BIT(9)
1305
+#define CSR12_NSN BIT(10)
1306
+#define CSR12_TRF BIT(11)
1307
+#define CSR12_ANS_SHIFT 12
1308
+#define CSR12_ANS_MASK 7
1309
+#define CSR12_LPN BIT(15)
1310
+#define CSR12_LPC_SHIFT 16
1311
+#define CSR12_LPC_MASK 0xffff
1312
+
1313
+#define CSR13_SRL BIT(0)
1314
+#define CSR13_CAC BIT(2)
1315
+#define CSR13_AUI BIT(3)
1316
+#define CSR13_SDM_SHIFT 4
1317
+#define CSR13_SDM_MASK 0xfff
1318
+
1319
+#define CSR14_ECEN BIT(0)
1320
+#define CSR14_LBK BIT(1)
1321
+#define CSR14_DREN BIT(2)
1322
+#define CSR14_LSE BIT(3)
1323
+#define CSR14_CPEN_SHIFT 4
1324
+#define CSR14_CPEN_MASK 3
1325
+#define CSR14_MBO BIT(6)
1326
+#define CSR14_ANE BIT(7)
1327
+#define CSR14_RSQ BIT(8)
1328
+#define CSR14_CSQ BIT(9)
1329
+#define CSR14_CLD BIT(10)
1330
+#define CSR14_SQE BIT(11)
1331
+#define CSR14_LTE BIT(12)
1332
+#define CSR14_APE BIT(13)
1333
+#define CSR14_SPP BIT(14)
1334
+#define CSR14_TAS BIT(15)
1335
+
1336
+#define CSR15_JBD BIT(0)
1337
+#define CSR15_HUJ BIT(1)
1338
+#define CSR15_JCK BIT(2)
1339
+#define CSR15_ABM BIT(3)
1340
+#define CSR15_RWD BIT(4)
1341
+#define CSR15_RWR BIT(5)
1342
+#define CSR15_LE1 BIT(6)
1343
+#define CSR15_LV1 BIT(7)
1344
+#define CSR15_TSCK BIT(8)
1345
+#define CSR15_FUSQ BIT(9)
1346
+#define CSR15_FLF BIT(10)
1347
+#define CSR15_LSD BIT(11)
1348
+#define CSR15_DPST BIT(12)
1349
+#define CSR15_FRL BIT(13)
1350
+#define CSR15_LE2 BIT(14)
1351
+#define CSR15_LV2 BIT(15)
1352
+
1353
+#define RDES0_OF BIT(0)
1354
+#define RDES0_CE BIT(1)
1355
+#define RDES0_DB BIT(2)
1356
+#define RDES0_RJ BIT(4)
1357
+#define RDES0_FT BIT(5)
1358
+#define RDES0_CS BIT(6)
1359
+#define RDES0_TL BIT(7)
1360
+#define RDES0_LS BIT(8)
1361
+#define RDES0_FS BIT(9)
1362
+#define RDES0_MF BIT(10)
1363
+#define RDES0_RF BIT(11)
1364
+#define RDES0_DT_SHIFT 12
1365
+#define RDES0_DT_MASK 3
1366
+#define RDES0_LE BIT(14)
1367
+#define RDES0_ES BIT(15)
1368
+#define RDES0_FL_SHIFT 16
1369
+#define RDES0_FL_MASK 0x3fff
1370
+#define RDES0_FF BIT(30)
1371
+#define RDES0_OWN BIT(31)
1372
+
1373
+#define RDES1_BUF1_SIZE_SHIFT 0
1374
+#define RDES1_BUF1_SIZE_MASK 0x7ff
1375
+
1376
+#define RDES1_BUF2_SIZE_SHIFT 11
1377
+#define RDES1_BUF2_SIZE_MASK 0x7ff
1378
+#define RDES1_RCH BIT(24)
1379
+#define RDES1_RER BIT(25)
1380
+
1381
+#define TDES0_DE BIT(0)
1382
+#define TDES0_UF BIT(1)
1383
+#define TDES0_LF BIT(2)
1384
+#define TDES0_CC_SHIFT 3
1385
+#define TDES0_CC_MASK 0xf
1386
+#define TDES0_HF BIT(7)
1387
+#define TDES0_EC BIT(8)
1388
+#define TDES0_LC BIT(9)
1389
+#define TDES0_NC BIT(10)
1390
+#define TDES0_LO BIT(11)
1391
+#define TDES0_TO BIT(14)
1392
+#define TDES0_ES BIT(15)
1393
+#define TDES0_OWN BIT(31)
1394
+
1395
+#define TDES1_BUF1_SIZE_SHIFT 0
1396
+#define TDES1_BUF1_SIZE_MASK 0x7ff
1397
+
1398
+#define TDES1_BUF2_SIZE_SHIFT 11
1399
+#define TDES1_BUF2_SIZE_MASK 0x7ff
1400
+
1401
+#define TDES1_FT0 BIT(22)
1402
+#define TDES1_DPD BIT(23)
1403
+#define TDES1_TCH BIT(24)
1404
+#define TDES1_TER BIT(25)
1405
+#define TDES1_AC BIT(26)
1406
+#define TDES1_SET BIT(27)
1407
+#define TDES1_FT1 BIT(28)
1408
+#define TDES1_FS BIT(29)
1409
+#define TDES1_LS BIT(30)
1410
+#define TDES1_IC BIT(31)
1411
+
1412
+struct tulip_descriptor {
1413
+ uint32_t status;
1414
+ uint32_t control;
1415
+ uint32_t buf_addr1;
1416
+ uint32_t buf_addr2;
1417
+};
1418
+
189
+
1419
+#endif
190
+#endif
1420
diff --git a/include/hw/pci/pci_ids.h b/include/hw/pci/pci_ids.h
1421
index XXXXXXX..XXXXXXX 100644
1422
--- a/include/hw/pci/pci_ids.h
1423
+++ b/include/hw/pci/pci_ids.h
1424
@@ -XXX,XX +XXX,XX @@
1425
#define PCI_DEVICE_ID_LSI_SAS0079 0x0079
1426
1427
#define PCI_VENDOR_ID_DEC 0x1011
1428
+#define PCI_DEVICE_ID_DEC_21143 0x0019
1429
#define PCI_DEVICE_ID_DEC_21154 0x0026
1430
1431
#define PCI_VENDOR_ID_CIRRUS 0x1013
1432
--
191
--
1433
2.5.0
192
2.7.4
1434
193
1435
194
diff view generated by jsdifflib
New patch
1
1
From: Eugenio Pérez <eperezma@redhat.com>
2
3
Use translations added in VhostIOVATree in SVQ.
4
5
Only introduce usage here, not allocation and deallocation. As with
6
previous patches, we use the dead code paths of shadow_vqs_enabled to
7
avoid commiting too many changes at once. These are impossible to take
8
at the moment.
9
10
Acked-by: Michael S. Tsirkin <mst@redhat.com>
11
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
12
Signed-off-by: Jason Wang <jasowang@redhat.com>
13
---
14
hw/virtio/vhost-shadow-virtqueue.c | 75 +++++++++++++++++++++--
15
hw/virtio/vhost-shadow-virtqueue.h | 6 +-
16
hw/virtio/vhost-vdpa.c | 122 +++++++++++++++++++++++++++++++------
17
include/hw/virtio/vhost-vdpa.h | 3 +
18
4 files changed, 181 insertions(+), 25 deletions(-)
19
20
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
21
index XXXXXXX..XXXXXXX 100644
22
--- a/hw/virtio/vhost-shadow-virtqueue.c
23
+++ b/hw/virtio/vhost-shadow-virtqueue.c
24
@@ -XXX,XX +XXX,XX @@ static uint16_t vhost_svq_available_slots(const VhostShadowVirtqueue *svq)
25
return svq->vring.num - (svq->shadow_avail_idx - svq->shadow_used_idx);
26
}
27
28
+/**
29
+ * Translate addresses between the qemu's virtual address and the SVQ IOVA
30
+ *
31
+ * @svq: Shadow VirtQueue
32
+ * @vaddr: Translated IOVA addresses
33
+ * @iovec: Source qemu's VA addresses
34
+ * @num: Length of iovec and minimum length of vaddr
35
+ */
36
+static bool vhost_svq_translate_addr(const VhostShadowVirtqueue *svq,
37
+ void **addrs, const struct iovec *iovec,
38
+ size_t num)
39
+{
40
+ if (num == 0) {
41
+ return true;
42
+ }
43
+
44
+ for (size_t i = 0; i < num; ++i) {
45
+ DMAMap needle = {
46
+ .translated_addr = (hwaddr)iovec[i].iov_base,
47
+ .size = iovec[i].iov_len,
48
+ };
49
+ size_t off;
50
+
51
+ const DMAMap *map = vhost_iova_tree_find_iova(svq->iova_tree, &needle);
52
+ /*
53
+ * Map cannot be NULL since iova map contains all guest space and
54
+ * qemu already has a physical address mapped
55
+ */
56
+ if (unlikely(!map)) {
57
+ qemu_log_mask(LOG_GUEST_ERROR,
58
+ "Invalid address 0x%"HWADDR_PRIx" given by guest",
59
+ needle.translated_addr);
60
+ return false;
61
+ }
62
+
63
+ off = needle.translated_addr - map->translated_addr;
64
+ addrs[i] = (void *)(map->iova + off);
65
+
66
+ if (unlikely(int128_gt(int128_add(needle.translated_addr,
67
+ iovec[i].iov_len),
68
+ map->translated_addr + map->size))) {
69
+ qemu_log_mask(LOG_GUEST_ERROR,
70
+ "Guest buffer expands over iova range");
71
+ return false;
72
+ }
73
+ }
74
+
75
+ return true;
76
+}
77
+
78
static void vhost_vring_write_descs(VhostShadowVirtqueue *svq,
79
+ void * const *sg,
80
const struct iovec *iovec,
81
size_t num, bool more_descs, bool write)
82
{
83
@@ -XXX,XX +XXX,XX @@ static void vhost_vring_write_descs(VhostShadowVirtqueue *svq,
84
} else {
85
descs[i].flags = flags;
86
}
87
- descs[i].addr = cpu_to_le64((hwaddr)iovec[n].iov_base);
88
+ descs[i].addr = cpu_to_le64((hwaddr)sg[n]);
89
descs[i].len = cpu_to_le32(iovec[n].iov_len);
90
91
last = i;
92
@@ -XXX,XX +XXX,XX @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *svq,
93
{
94
unsigned avail_idx;
95
vring_avail_t *avail = svq->vring.avail;
96
+ bool ok;
97
+ g_autofree void **sgs = g_new(void *, MAX(elem->out_num, elem->in_num));
98
99
*head = svq->free_head;
100
101
@@ -XXX,XX +XXX,XX @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *svq,
102
return false;
103
}
104
105
- vhost_vring_write_descs(svq, elem->out_sg, elem->out_num,
106
+ ok = vhost_svq_translate_addr(svq, sgs, elem->out_sg, elem->out_num);
107
+ if (unlikely(!ok)) {
108
+ return false;
109
+ }
110
+ vhost_vring_write_descs(svq, sgs, elem->out_sg, elem->out_num,
111
elem->in_num > 0, false);
112
- vhost_vring_write_descs(svq, elem->in_sg, elem->in_num, false, true);
113
+
114
+
115
+ ok = vhost_svq_translate_addr(svq, sgs, elem->in_sg, elem->in_num);
116
+ if (unlikely(!ok)) {
117
+ return false;
118
+ }
119
+
120
+ vhost_vring_write_descs(svq, sgs, elem->in_sg, elem->in_num, false, true);
121
122
/*
123
* Put the entry in the available array (but don't update avail->idx until
124
@@ -XXX,XX +XXX,XX @@ void vhost_svq_stop(VhostShadowVirtqueue *svq)
125
* Creates vhost shadow virtqueue, and instructs the vhost device to use the
126
* shadow methods and file descriptors.
127
*
128
+ * @iova_tree: Tree to perform descriptors translations
129
+ *
130
* Returns the new virtqueue or NULL.
131
*
132
* In case of error, reason is reported through error_report.
133
*/
134
-VhostShadowVirtqueue *vhost_svq_new(void)
135
+VhostShadowVirtqueue *vhost_svq_new(VhostIOVATree *iova_tree)
136
{
137
g_autofree VhostShadowVirtqueue *svq = g_new0(VhostShadowVirtqueue, 1);
138
int r;
139
@@ -XXX,XX +XXX,XX @@ VhostShadowVirtqueue *vhost_svq_new(void)
140
141
event_notifier_init_fd(&svq->svq_kick, VHOST_FILE_UNBIND);
142
event_notifier_set_handler(&svq->hdev_call, vhost_svq_handle_call);
143
+ svq->iova_tree = iova_tree;
144
return g_steal_pointer(&svq);
145
146
err_init_hdev_call:
147
diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h
148
index XXXXXXX..XXXXXXX 100644
149
--- a/hw/virtio/vhost-shadow-virtqueue.h
150
+++ b/hw/virtio/vhost-shadow-virtqueue.h
151
@@ -XXX,XX +XXX,XX @@
152
#include "qemu/event_notifier.h"
153
#include "hw/virtio/virtio.h"
154
#include "standard-headers/linux/vhost_types.h"
155
+#include "hw/virtio/vhost-iova-tree.h"
156
157
/* Shadow virtqueue to relay notifications */
158
typedef struct VhostShadowVirtqueue {
159
@@ -XXX,XX +XXX,XX @@ typedef struct VhostShadowVirtqueue {
160
/* Virtio device */
161
VirtIODevice *vdev;
162
163
+ /* IOVA mapping */
164
+ VhostIOVATree *iova_tree;
165
+
166
/* Map for use the guest's descriptors */
167
VirtQueueElement **ring_id_maps;
168
169
@@ -XXX,XX +XXX,XX @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev,
170
VirtQueue *vq);
171
void vhost_svq_stop(VhostShadowVirtqueue *svq);
172
173
-VhostShadowVirtqueue *vhost_svq_new(void);
174
+VhostShadowVirtqueue *vhost_svq_new(VhostIOVATree *iova_tree);
175
176
void vhost_svq_free(gpointer vq);
177
G_DEFINE_AUTOPTR_CLEANUP_FUNC(VhostShadowVirtqueue, vhost_svq_free);
178
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
179
index XXXXXXX..XXXXXXX 100644
180
--- a/hw/virtio/vhost-vdpa.c
181
+++ b/hw/virtio/vhost-vdpa.c
182
@@ -XXX,XX +XXX,XX @@ static void vhost_vdpa_listener_region_add(MemoryListener *listener,
183
vaddr, section->readonly);
184
185
llsize = int128_sub(llend, int128_make64(iova));
186
+ if (v->shadow_vqs_enabled) {
187
+ DMAMap mem_region = {
188
+ .translated_addr = (hwaddr)vaddr,
189
+ .size = int128_get64(llsize) - 1,
190
+ .perm = IOMMU_ACCESS_FLAG(true, section->readonly),
191
+ };
192
+
193
+ int r = vhost_iova_tree_map_alloc(v->iova_tree, &mem_region);
194
+ if (unlikely(r != IOVA_OK)) {
195
+ error_report("Can't allocate a mapping (%d)", r);
196
+ goto fail;
197
+ }
198
+
199
+ iova = mem_region.iova;
200
+ }
201
202
vhost_vdpa_iotlb_batch_begin_once(v);
203
ret = vhost_vdpa_dma_map(v, iova, int128_get64(llsize),
204
@@ -XXX,XX +XXX,XX @@ static void vhost_vdpa_listener_region_del(MemoryListener *listener,
205
206
llsize = int128_sub(llend, int128_make64(iova));
207
208
+ if (v->shadow_vqs_enabled) {
209
+ const DMAMap *result;
210
+ const void *vaddr = memory_region_get_ram_ptr(section->mr) +
211
+ section->offset_within_region +
212
+ (iova - section->offset_within_address_space);
213
+ DMAMap mem_region = {
214
+ .translated_addr = (hwaddr)vaddr,
215
+ .size = int128_get64(llsize) - 1,
216
+ };
217
+
218
+ result = vhost_iova_tree_find_iova(v->iova_tree, &mem_region);
219
+ iova = result->iova;
220
+ vhost_iova_tree_remove(v->iova_tree, &mem_region);
221
+ }
222
vhost_vdpa_iotlb_batch_begin_once(v);
223
ret = vhost_vdpa_dma_unmap(v, iova, int128_get64(llsize));
224
if (ret) {
225
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,
226
227
shadow_vqs = g_ptr_array_new_full(hdev->nvqs, vhost_svq_free);
228
for (unsigned n = 0; n < hdev->nvqs; ++n) {
229
- g_autoptr(VhostShadowVirtqueue) svq = vhost_svq_new();
230
+ g_autoptr(VhostShadowVirtqueue) svq = vhost_svq_new(v->iova_tree);
231
232
if (unlikely(!svq)) {
233
error_setg(errp, "Cannot create svq %u", n);
234
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev,
235
/**
236
* Unmap a SVQ area in the device
237
*/
238
-static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, hwaddr iova,
239
- hwaddr size)
240
+static bool vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v,
241
+ const DMAMap *needle)
242
{
243
+ const DMAMap *result = vhost_iova_tree_find_iova(v->iova_tree, needle);
244
+ hwaddr size;
245
int r;
246
247
- size = ROUND_UP(size, qemu_real_host_page_size);
248
- r = vhost_vdpa_dma_unmap(v, iova, size);
249
+ if (unlikely(!result)) {
250
+ error_report("Unable to find SVQ address to unmap");
251
+ return false;
252
+ }
253
+
254
+ size = ROUND_UP(result->size, qemu_real_host_page_size);
255
+ r = vhost_vdpa_dma_unmap(v, result->iova, size);
256
return r == 0;
257
}
258
259
static bool vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,
260
const VhostShadowVirtqueue *svq)
261
{
262
+ DMAMap needle = {};
263
struct vhost_vdpa *v = dev->opaque;
264
struct vhost_vring_addr svq_addr;
265
- size_t device_size = vhost_svq_device_area_size(svq);
266
- size_t driver_size = vhost_svq_driver_area_size(svq);
267
bool ok;
268
269
vhost_svq_get_vring_addr(svq, &svq_addr);
270
271
- ok = vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr, driver_size);
272
+ needle.translated_addr = svq_addr.desc_user_addr;
273
+ ok = vhost_vdpa_svq_unmap_ring(v, &needle);
274
if (unlikely(!ok)) {
275
return false;
276
}
277
278
- return vhost_vdpa_svq_unmap_ring(v, svq_addr.used_user_addr, device_size);
279
+ needle.translated_addr = svq_addr.used_user_addr;
280
+ return vhost_vdpa_svq_unmap_ring(v, &needle);
281
+}
282
+
283
+/**
284
+ * Map the SVQ area in the device
285
+ *
286
+ * @v: Vhost-vdpa device
287
+ * @needle: The area to search iova
288
+ * @errorp: Error pointer
289
+ */
290
+static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle,
291
+ Error **errp)
292
+{
293
+ int r;
294
+
295
+ r = vhost_iova_tree_map_alloc(v->iova_tree, needle);
296
+ if (unlikely(r != IOVA_OK)) {
297
+ error_setg(errp, "Cannot allocate iova (%d)", r);
298
+ return false;
299
+ }
300
+
301
+ r = vhost_vdpa_dma_map(v, needle->iova, needle->size + 1,
302
+ (void *)needle->translated_addr,
303
+ needle->perm == IOMMU_RO);
304
+ if (unlikely(r != 0)) {
305
+ error_setg_errno(errp, -r, "Cannot map region to device");
306
+ vhost_iova_tree_remove(v->iova_tree, needle);
307
+ }
308
+
309
+ return r == 0;
310
}
311
312
/**
313
@@ -XXX,XX +XXX,XX @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,
314
struct vhost_vring_addr *addr,
315
Error **errp)
316
{
317
+ DMAMap device_region, driver_region;
318
+ struct vhost_vring_addr svq_addr;
319
struct vhost_vdpa *v = dev->opaque;
320
size_t device_size = vhost_svq_device_area_size(svq);
321
size_t driver_size = vhost_svq_driver_area_size(svq);
322
- int r;
323
+ size_t avail_offset;
324
+ bool ok;
325
326
ERRP_GUARD();
327
- vhost_svq_get_vring_addr(svq, addr);
328
+ vhost_svq_get_vring_addr(svq, &svq_addr);
329
330
- r = vhost_vdpa_dma_map(v, addr->desc_user_addr, driver_size,
331
- (void *)addr->desc_user_addr, true);
332
- if (unlikely(r != 0)) {
333
- error_setg_errno(errp, -r, "Cannot create vq driver region: ");
334
+ driver_region = (DMAMap) {
335
+ .translated_addr = svq_addr.desc_user_addr,
336
+ .size = driver_size - 1,
337
+ .perm = IOMMU_RO,
338
+ };
339
+ ok = vhost_vdpa_svq_map_ring(v, &driver_region, errp);
340
+ if (unlikely(!ok)) {
341
+ error_prepend(errp, "Cannot create vq driver region: ");
342
return false;
343
}
344
+ addr->desc_user_addr = driver_region.iova;
345
+ avail_offset = svq_addr.avail_user_addr - svq_addr.desc_user_addr;
346
+ addr->avail_user_addr = driver_region.iova + avail_offset;
347
348
- r = vhost_vdpa_dma_map(v, addr->used_user_addr, device_size,
349
- (void *)addr->used_user_addr, false);
350
- if (unlikely(r != 0)) {
351
- error_setg_errno(errp, -r, "Cannot create vq device region: ");
352
+ device_region = (DMAMap) {
353
+ .translated_addr = svq_addr.used_user_addr,
354
+ .size = device_size - 1,
355
+ .perm = IOMMU_RW,
356
+ };
357
+ ok = vhost_vdpa_svq_map_ring(v, &device_region, errp);
358
+ if (unlikely(!ok)) {
359
+ error_prepend(errp, "Cannot create vq device region: ");
360
+ vhost_vdpa_svq_unmap_ring(v, &driver_region);
361
}
362
+ addr->used_user_addr = device_region.iova;
363
364
- return r == 0;
365
+ return ok;
366
}
367
368
static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
369
diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h
370
index XXXXXXX..XXXXXXX 100644
371
--- a/include/hw/virtio/vhost-vdpa.h
372
+++ b/include/hw/virtio/vhost-vdpa.h
373
@@ -XXX,XX +XXX,XX @@
374
375
#include <gmodule.h>
376
377
+#include "hw/virtio/vhost-iova-tree.h"
378
#include "hw/virtio/virtio.h"
379
#include "standard-headers/linux/vhost_types.h"
380
381
@@ -XXX,XX +XXX,XX @@ typedef struct vhost_vdpa {
382
MemoryListener listener;
383
struct vhost_vdpa_iova_range iova_range;
384
bool shadow_vqs_enabled;
385
+ /* IOVA mapping used by the Shadow Virtqueue */
386
+ VhostIOVATree *iova_tree;
387
GPtrArray *shadow_vqs;
388
struct vhost_dev *dev;
389
VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX];
390
--
391
2.7.4
392
393
diff view generated by jsdifflib
New patch
1
From: Eugenio Pérez <eperezma@redhat.com>
1
2
3
This is needed to achieve migration, so the destination can restore its
4
index.
5
6
Setting base as last used idx, so destination will see as available all
7
the entries that the device did not use, including the in-flight
8
processing ones.
9
10
This is ok for networking, but other kinds of devices might have
11
problems with these retransmissions.
12
13
Acked-by: Michael S. Tsirkin <mst@redhat.com>
14
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
15
Signed-off-by: Jason Wang <jasowang@redhat.com>
16
---
17
hw/virtio/vhost-vdpa.c | 17 +++++++++++++++++
18
1 file changed, 17 insertions(+)
19
20
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
21
index XXXXXXX..XXXXXXX 100644
22
--- a/hw/virtio/vhost-vdpa.c
23
+++ b/hw/virtio/vhost-vdpa.c
24
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_vring_base(struct vhost_dev *dev,
25
static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,
26
struct vhost_vring_state *ring)
27
{
28
+ struct vhost_vdpa *v = dev->opaque;
29
int ret;
30
31
+ if (v->shadow_vqs_enabled) {
32
+ VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs,
33
+ ring->index);
34
+
35
+ /*
36
+ * Setting base as last used idx, so destination will see as available
37
+ * all the entries that the device did not use, including the in-flight
38
+ * processing ones.
39
+ *
40
+ * TODO: This is ok for networking, but other kinds of devices might
41
+ * have problems with these retransmissions.
42
+ */
43
+ ring->num = svq->last_used_idx;
44
+ return 0;
45
+ }
46
+
47
ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring);
48
trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num);
49
return ret;
50
--
51
2.7.4
52
53
diff view generated by jsdifflib
1
From: Fan Yang <Fan_Yang@sjtu.edu.cn>
1
From: Eugenio Pérez <eperezma@redhat.com>
2
2
3
'colo_mark_tcp_pkt' should return 'true' when packets are the same, and
3
Setting the log address would make the device start reporting invalid
4
'false' otherwise. However, it returns 'true' when
4
dirty memory because the SVQ vrings are located in qemu's memory.
5
'colo_compare_packet_payload' returns non-zero while
6
'colo_compare_packet_payload' is just a 'memcmp'. The result is that
7
COLO-compare reports inconsistent TCP packets when they are actually
8
the same.
9
5
10
Fixes: f449c9e549c ("colo: compare the packet based on the tcp sequence number")
6
Acked-by: Michael S. Tsirkin <mst@redhat.com>
11
Cc: qemu-stable@nongnu.org
7
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
12
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
13
Signed-off-by: Fan Yang <Fan_Yang@sjtu.edu.cn>
14
Signed-off-by: Jason Wang <jasowang@redhat.com>
8
Signed-off-by: Jason Wang <jasowang@redhat.com>
15
---
9
---
16
net/colo-compare.c | 6 +++---
10
hw/virtio/vhost-vdpa.c | 3 ++-
17
1 file changed, 3 insertions(+), 3 deletions(-)
11
1 file changed, 2 insertions(+), 1 deletion(-)
18
12
19
diff --git a/net/colo-compare.c b/net/colo-compare.c
13
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
20
index XXXXXXX..XXXXXXX 100644
14
index XXXXXXX..XXXXXXX 100644
21
--- a/net/colo-compare.c
15
--- a/hw/virtio/vhost-vdpa.c
22
+++ b/net/colo-compare.c
16
+++ b/hw/virtio/vhost-vdpa.c
23
@@ -XXX,XX +XXX,XX @@ static bool colo_mark_tcp_pkt(Packet *ppkt, Packet *spkt,
17
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
24
*mark = 0;
18
static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base,
25
19
struct vhost_log *log)
26
if (ppkt->tcp_seq == spkt->tcp_seq && ppkt->seq_end == spkt->seq_end) {
20
{
27
- if (colo_compare_packet_payload(ppkt, spkt,
21
- if (vhost_vdpa_one_time_request(dev)) {
28
+ if (!colo_compare_packet_payload(ppkt, spkt,
22
+ struct vhost_vdpa *v = dev->opaque;
29
ppkt->header_size, spkt->header_size,
23
+ if (v->shadow_vqs_enabled || vhost_vdpa_one_time_request(dev)) {
30
ppkt->payload_size)) {
24
return 0;
31
*mark = COLO_COMPARE_FREE_SECONDARY | COLO_COMPARE_FREE_PRIMARY;
25
}
32
@@ -XXX,XX +XXX,XX @@ static bool colo_mark_tcp_pkt(Packet *ppkt, Packet *spkt,
26
33
34
/* one part of secondary packet payload still need to be compared */
35
if (!after(ppkt->seq_end, spkt->seq_end)) {
36
- if (colo_compare_packet_payload(ppkt, spkt,
37
+ if (!colo_compare_packet_payload(ppkt, spkt,
38
ppkt->header_size + ppkt->offset,
39
spkt->header_size + spkt->offset,
40
ppkt->payload_size - ppkt->offset)) {
41
@@ -XXX,XX +XXX,XX @@ static bool colo_mark_tcp_pkt(Packet *ppkt, Packet *spkt,
42
/* primary packet is longer than secondary packet, compare
43
* the same part and mark the primary packet offset
44
*/
45
- if (colo_compare_packet_payload(ppkt, spkt,
46
+ if (!colo_compare_packet_payload(ppkt, spkt,
47
ppkt->header_size + ppkt->offset,
48
spkt->header_size + spkt->offset,
49
spkt->payload_size - spkt->offset)) {
50
--
27
--
51
2.5.0
28
2.7.4
52
29
53
30
diff view generated by jsdifflib
1
From: "Michael S. Tsirkin" <mst@redhat.com>
1
From: Eugenio Pérez <eperezma@redhat.com>
2
2
3
Post load hook in virtio vmsd is called early while device is processed,
3
SVQ is able to log the dirty bits by itself, so let's use it to not
4
and when VirtIODevice core isn't fully initialized. Most device
4
block migration.
5
specific code isn't ready to deal with a device in such state, and
6
behaves weirdly.
7
5
8
Add a new post_load hook in a device class instead. Devices should use
6
Also, ignore set and clear of VHOST_F_LOG_ALL on set_features if SVQ is
9
this unless they specifically want to verify the migration stream as
7
enabled. Even if the device supports it, the reports would be nonsense
10
it's processed, e.g. for bounds checking.
8
because SVQ memory is in the qemu region.
11
9
12
Cc: qemu-stable@nongnu.org
10
The log region is still allocated. Future changes might skip that, but
13
Suggested-by: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
11
this series is already long enough.
14
Cc: Mikhail Sennikovsky <mikhail.sennikovskii@cloud.ionos.com>
12
15
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
13
Acked-by: Michael S. Tsirkin <mst@redhat.com>
14
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
16
Signed-off-by: Jason Wang <jasowang@redhat.com>
15
Signed-off-by: Jason Wang <jasowang@redhat.com>
17
---
16
---
18
hw/virtio/virtio.c | 7 +++++++
17
hw/virtio/vhost-vdpa.c | 39 +++++++++++++++++++++++++++++++++++----
19
include/hw/virtio/virtio.h | 6 ++++++
18
include/hw/virtio/vhost-vdpa.h | 1 +
20
2 files changed, 13 insertions(+)
19
2 files changed, 36 insertions(+), 4 deletions(-)
21
20
22
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
21
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
23
index XXXXXXX..XXXXXXX 100644
22
index XXXXXXX..XXXXXXX 100644
24
--- a/hw/virtio/virtio.c
23
--- a/hw/virtio/vhost-vdpa.c
25
+++ b/hw/virtio/virtio.c
24
+++ b/hw/virtio/vhost-vdpa.c
26
@@ -XXX,XX +XXX,XX @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
25
@@ -XXX,XX +XXX,XX @@ static bool vhost_vdpa_one_time_request(struct vhost_dev *dev)
26
return v->index != 0;
27
}
28
29
+static int vhost_vdpa_get_dev_features(struct vhost_dev *dev,
30
+ uint64_t *features)
31
+{
32
+ int ret;
33
+
34
+ ret = vhost_vdpa_call(dev, VHOST_GET_FEATURES, features);
35
+ trace_vhost_vdpa_get_features(dev, *features);
36
+ return ret;
37
+}
38
+
39
static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,
40
Error **errp)
41
{
42
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v,
43
return 0;
27
}
44
}
28
rcu_read_unlock();
45
29
46
- r = hdev->vhost_ops->vhost_get_features(hdev, &dev_features);
30
+ if (vdc->post_load) {
47
+ r = vhost_vdpa_get_dev_features(hdev, &dev_features);
31
+ ret = vdc->post_load(vdev);
48
if (r != 0) {
32
+ if (ret) {
49
error_setg_errno(errp, -r, "Can't get vdpa device features");
33
+ return ret;
50
return r;
51
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_mem_table(struct vhost_dev *dev,
52
static int vhost_vdpa_set_features(struct vhost_dev *dev,
53
uint64_t features)
54
{
55
+ struct vhost_vdpa *v = dev->opaque;
56
int ret;
57
58
if (vhost_vdpa_one_time_request(dev)) {
59
return 0;
60
}
61
62
+ if (v->shadow_vqs_enabled) {
63
+ if ((v->acked_features ^ features) == BIT_ULL(VHOST_F_LOG_ALL)) {
64
+ /*
65
+ * QEMU is just trying to enable or disable logging. SVQ handles
66
+ * this sepparately, so no need to forward this.
67
+ */
68
+ v->acked_features = features;
69
+ return 0;
34
+ }
70
+ }
71
+
72
+ v->acked_features = features;
73
+
74
+ /* We must not ack _F_LOG if SVQ is enabled */
75
+ features &= ~BIT_ULL(VHOST_F_LOG_ALL);
35
+ }
76
+ }
36
+
77
+
37
return 0;
78
trace_vhost_vdpa_set_features(dev, features);
79
ret = vhost_vdpa_call(dev, VHOST_SET_FEATURES, &features);
80
if (ret) {
81
@@ -XXX,XX +XXX,XX @@ static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,
82
static int vhost_vdpa_get_features(struct vhost_dev *dev,
83
uint64_t *features)
84
{
85
- int ret;
86
+ struct vhost_vdpa *v = dev->opaque;
87
+ int ret = vhost_vdpa_get_dev_features(dev, features);
88
+
89
+ if (ret == 0 && v->shadow_vqs_enabled) {
90
+ /* Add SVQ logging capabilities */
91
+ *features |= BIT_ULL(VHOST_F_LOG_ALL);
92
+ }
93
94
- ret = vhost_vdpa_call(dev, VHOST_GET_FEATURES, features);
95
- trace_vhost_vdpa_get_features(dev, *features);
96
return ret;
38
}
97
}
39
98
40
diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
99
diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h
41
index XXXXXXX..XXXXXXX 100644
100
index XXXXXXX..XXXXXXX 100644
42
--- a/include/hw/virtio/virtio.h
101
--- a/include/hw/virtio/vhost-vdpa.h
43
+++ b/include/hw/virtio/virtio.h
102
+++ b/include/hw/virtio/vhost-vdpa.h
44
@@ -XXX,XX +XXX,XX @@ typedef struct VirtioDeviceClass {
103
@@ -XXX,XX +XXX,XX @@ typedef struct vhost_vdpa {
45
*/
104
bool iotlb_batch_begin_sent;
46
void (*save)(VirtIODevice *vdev, QEMUFile *f);
105
MemoryListener listener;
47
int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
106
struct vhost_vdpa_iova_range iova_range;
48
+ /* Post load hook in vmsd is called early while device is processed, and
107
+ uint64_t acked_features;
49
+ * when VirtIODevice isn't fully initialized. Devices should use this instead,
108
bool shadow_vqs_enabled;
50
+ * unless they specifically want to verify the migration stream as it's
109
/* IOVA mapping used by the Shadow Virtqueue */
51
+ * processed, e.g. for bounds checking.
110
VhostIOVATree *iova_tree;
52
+ */
53
+ int (*post_load)(VirtIODevice *vdev);
54
const VMStateDescription *vmsd;
55
} VirtioDeviceClass;
56
57
--
111
--
58
2.5.0
112
2.7.4
59
113
60
114
diff view generated by jsdifflib