]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/virtio-net.c
qemu:virtio-net: Save status and add some save infrastructure (Alex Williamson)
[thirdparty/qemu.git] / hw / virtio-net.c
CommitLineData
fbe78f4f
AL
1/*
2 * Virtio Network Device
3 *
4 * Copyright IBM, Corp. 2007
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#include "virtio.h"
15#include "net.h"
16#include "qemu-timer.h"
17#include "virtio-net.h"
18
9d6271b8
AL
19#define VIRTIO_NET_VM_VERSION 3
20
fbe78f4f
AL
21typedef struct VirtIONet
22{
23 VirtIODevice vdev;
24 uint8_t mac[6];
554c97dd 25 uint16_t status;
fbe78f4f
AL
26 VirtQueue *rx_vq;
27 VirtQueue *tx_vq;
28 VLANClientState *vc;
29 QEMUTimer *tx_timer;
30 int tx_timer_active;
31 int mergeable_rx_bufs;
32} VirtIONet;
33
34/* TODO
35 * - we could suppress RX interrupt if we were so inclined.
36 */
37
38static VirtIONet *to_virtio_net(VirtIODevice *vdev)
39{
40 return (VirtIONet *)vdev;
41}
42
43static void virtio_net_update_config(VirtIODevice *vdev, uint8_t *config)
44{
45 VirtIONet *n = to_virtio_net(vdev);
46 struct virtio_net_config netcfg;
47
554c97dd 48 netcfg.status = n->status;
fbe78f4f
AL
49 memcpy(netcfg.mac, n->mac, 6);
50 memcpy(config, &netcfg, sizeof(netcfg));
51}
52
554c97dd
AL
53static void virtio_net_set_link_status(VLANClientState *vc)
54{
55 VirtIONet *n = vc->opaque;
56 uint16_t old_status = n->status;
57
58 if (vc->link_down)
59 n->status &= ~VIRTIO_NET_S_LINK_UP;
60 else
61 n->status |= VIRTIO_NET_S_LINK_UP;
62
63 if (n->status != old_status)
64 virtio_notify_config(&n->vdev);
65}
66
fbe78f4f
AL
67static uint32_t virtio_net_get_features(VirtIODevice *vdev)
68{
554c97dd 69 uint32_t features = (1 << VIRTIO_NET_F_MAC) | (1 << VIRTIO_NET_F_STATUS);
fbe78f4f
AL
70
71 return features;
72}
73
74static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
75{
76 VirtIONet *n = to_virtio_net(vdev);
77
78 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
79}
80
81/* RX */
82
83static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
84{
85}
86
87static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
88{
89 if (!virtio_queue_ready(n->rx_vq) ||
90 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
91 return 0;
92
93 if (virtio_queue_empty(n->rx_vq) ||
94 (n->mergeable_rx_bufs &&
95 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
96 virtio_queue_set_notification(n->rx_vq, 1);
97 return 0;
98 }
99
100 virtio_queue_set_notification(n->rx_vq, 0);
101 return 1;
102}
103
104static int virtio_net_can_receive(void *opaque)
105{
106 VirtIONet *n = opaque;
107
108 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
109}
110
111static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
112{
113 int offset, i;
114
115 offset = i = 0;
116 while (offset < count && i < iovcnt) {
117 int len = MIN(iov[i].iov_len, count - offset);
118 memcpy(iov[i].iov_base, buf + offset, len);
119 offset += len;
120 i++;
121 }
122
123 return offset;
124}
125
126static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
4689f4b3 127 const void *buf, size_t size, size_t hdr_len)
fbe78f4f
AL
128{
129 struct virtio_net_hdr *hdr = iov[0].iov_base;
130 int offset = 0;
131
132 hdr->flags = 0;
133 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
134
135 /* We only ever receive a struct virtio_net_hdr from the tapfd,
136 * but we may be passing along a larger header to the guest.
137 */
138 iov[0].iov_base += hdr_len;
139 iov[0].iov_len -= hdr_len;
140
141 return offset;
142}
143
144static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
145{
146 VirtIONet *n = opaque;
147 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
4689f4b3 148 size_t hdr_len, offset, i;
fbe78f4f
AL
149
150 if (!do_virtio_net_can_receive(n, size))
151 return;
152
153 /* hdr_len refers to the header we supply to the guest */
154 hdr_len = n->mergeable_rx_bufs ?
155 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
156
157 offset = i = 0;
158
159 while (offset < size) {
160 VirtQueueElement elem;
161 int len, total;
162 struct iovec sg[VIRTQUEUE_MAX_SIZE];
163
164 len = total = 0;
165
166 if ((i != 0 && !n->mergeable_rx_bufs) ||
167 virtqueue_pop(n->rx_vq, &elem) == 0) {
168 if (i == 0)
169 return;
170 fprintf(stderr, "virtio-net truncating packet\n");
171 exit(1);
172 }
173
174 if (elem.in_num < 1) {
175 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
176 exit(1);
177 }
178
179 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
180 fprintf(stderr, "virtio-net header not in first element\n");
181 exit(1);
182 }
183
184 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
185
186 if (i == 0) {
187 if (n->mergeable_rx_bufs)
188 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
189
190 offset += receive_header(n, sg, elem.in_num,
191 buf + offset, size - offset, hdr_len);
192 total += hdr_len;
193 }
194
195 /* copy in packet. ugh */
196 len = iov_fill(sg, elem.in_num,
197 buf + offset, size - offset);
198 total += len;
199
200 /* signal other side */
201 virtqueue_fill(n->rx_vq, &elem, total, i++);
202
203 offset += len;
204 }
205
206 if (mhdr)
207 mhdr->num_buffers = i;
208
209 virtqueue_flush(n->rx_vq, i);
210 virtio_notify(&n->vdev, n->rx_vq);
211}
212
213/* TX */
214static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
215{
216 VirtQueueElement elem;
217 int has_vnet_hdr = 0;
218
219 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
220 return;
221
222 while (virtqueue_pop(vq, &elem)) {
223 ssize_t len = 0;
224 unsigned int out_num = elem.out_num;
225 struct iovec *out_sg = &elem.out_sg[0];
226 unsigned hdr_len;
227
228 /* hdr_len refers to the header received from the guest */
229 hdr_len = n->mergeable_rx_bufs ?
230 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
231 sizeof(struct virtio_net_hdr);
232
233 if (out_num < 1 || out_sg->iov_len != hdr_len) {
234 fprintf(stderr, "virtio-net header not in first element\n");
235 exit(1);
236 }
237
238 /* ignore the header if GSO is not supported */
239 if (!has_vnet_hdr) {
240 out_num--;
241 out_sg++;
242 len += hdr_len;
243 } else if (n->mergeable_rx_bufs) {
244 /* tapfd expects a struct virtio_net_hdr */
245 hdr_len -= sizeof(struct virtio_net_hdr);
246 out_sg->iov_len -= hdr_len;
247 len += hdr_len;
248 }
249
250 len += qemu_sendv_packet(n->vc, out_sg, out_num);
251
252 virtqueue_push(vq, &elem, len);
253 virtio_notify(&n->vdev, vq);
254 }
255}
256
257static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
258{
259 VirtIONet *n = to_virtio_net(vdev);
260
261 if (n->tx_timer_active) {
262 virtio_queue_set_notification(vq, 1);
263 qemu_del_timer(n->tx_timer);
264 n->tx_timer_active = 0;
265 virtio_net_flush_tx(n, vq);
266 } else {
267 qemu_mod_timer(n->tx_timer,
268 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
269 n->tx_timer_active = 1;
270 virtio_queue_set_notification(vq, 0);
271 }
272}
273
274static void virtio_net_tx_timer(void *opaque)
275{
276 VirtIONet *n = opaque;
277
278 n->tx_timer_active = 0;
279
280 /* Just in case the driver is not ready on more */
281 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
282 return;
283
284 virtio_queue_set_notification(n->tx_vq, 1);
285 virtio_net_flush_tx(n, n->tx_vq);
286}
287
288static void virtio_net_save(QEMUFile *f, void *opaque)
289{
290 VirtIONet *n = opaque;
291
292 virtio_save(&n->vdev, f);
293
294 qemu_put_buffer(f, n->mac, 6);
295 qemu_put_be32(f, n->tx_timer_active);
e46cb38f 296 qemu_put_be32(f, n->mergeable_rx_bufs);
9d6271b8 297 qemu_put_be16(f, n->status);
fbe78f4f
AL
298}
299
300static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
301{
302 VirtIONet *n = opaque;
303
9d6271b8 304 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
fbe78f4f
AL
305 return -EINVAL;
306
307 virtio_load(&n->vdev, f);
308
309 qemu_get_buffer(f, n->mac, 6);
310 n->tx_timer_active = qemu_get_be32(f);
e46cb38f 311 n->mergeable_rx_bufs = qemu_get_be32(f);
fbe78f4f 312
9d6271b8
AL
313 if (version_id >= 3)
314 n->status = qemu_get_be16(f);
315
fbe78f4f
AL
316 if (n->tx_timer_active) {
317 qemu_mod_timer(n->tx_timer,
318 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
319 }
320
321 return 0;
322}
323
291c6ff9 324void virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
fbe78f4f
AL
325{
326 VirtIONet *n;
327 static int virtio_net_id;
328
a7c4996b
AL
329 n = (VirtIONet *)virtio_init_pci(bus, "virtio-net",
330 PCI_VENDOR_ID_REDHAT_QUMRANET,
331 PCI_DEVICE_ID_VIRTIO_NET,
99b3718e
AL
332 PCI_VENDOR_ID_REDHAT_QUMRANET,
333 VIRTIO_ID_NET,
173a543b 334 PCI_CLASS_NETWORK_ETHERNET, 0x00,
554c97dd
AL
335 sizeof(struct virtio_net_config),
336 sizeof(VirtIONet));
fbe78f4f 337 if (!n)
291c6ff9 338 return;
fbe78f4f
AL
339
340 n->vdev.get_config = virtio_net_update_config;
341 n->vdev.get_features = virtio_net_get_features;
342 n->vdev.set_features = virtio_net_set_features;
343 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
344 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
345 memcpy(n->mac, nd->macaddr, 6);
554c97dd 346 n->status = VIRTIO_NET_S_LINK_UP;
7a9f6e4a 347 n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
bf38c1a0 348 virtio_net_receive, virtio_net_can_receive, n);
554c97dd 349 n->vc->link_status_changed = virtio_net_set_link_status;
fbe78f4f 350
96d5e201
AL
351 qemu_format_nic_info_str(n->vc, n->mac);
352
fbe78f4f
AL
353 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
354 n->tx_timer_active = 0;
355 n->mergeable_rx_bufs = 0;
356
9d6271b8 357 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
fbe78f4f 358 virtio_net_save, virtio_net_load, n);
fbe78f4f 359}