]> git.proxmox.com Git - qemu.git/blame - hw/virtio-net.c
virtio-net: Add new RX filter controls
[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
015cb166 19#define VIRTIO_NET_VM_VERSION 10
b6503ed9
AL
20
21#define MAC_TABLE_ENTRIES 32
f21c0ed9 22#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
9d6271b8 23
fbe78f4f
AL
24typedef struct VirtIONet
25{
26 VirtIODevice vdev;
79674068 27 uint8_t mac[ETH_ALEN];
554c97dd 28 uint16_t status;
fbe78f4f
AL
29 VirtQueue *rx_vq;
30 VirtQueue *tx_vq;
3d11d36c 31 VirtQueue *ctrl_vq;
fbe78f4f
AL
32 VLANClientState *vc;
33 QEMUTimer *tx_timer;
34 int tx_timer_active;
35 int mergeable_rx_bufs;
f10c592e
AW
36 uint8_t promisc;
37 uint8_t allmulti;
015cb166
AW
38 uint8_t alluni;
39 uint8_t nomulti;
40 uint8_t nouni;
41 uint8_t nobcast;
b6503ed9
AL
42 struct {
43 int in_use;
2d9aba39 44 int first_multi;
8fd2a2f1
AW
45 uint8_t multi_overflow;
46 uint8_t uni_overflow;
b6503ed9
AL
47 uint8_t *macs;
48 } mac_table;
f21c0ed9 49 uint32_t *vlans;
fbe78f4f
AL
50} VirtIONet;
51
52/* TODO
53 * - we could suppress RX interrupt if we were so inclined.
54 */
55
56static VirtIONet *to_virtio_net(VirtIODevice *vdev)
57{
58 return (VirtIONet *)vdev;
59}
60
0f03eca6 61static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
fbe78f4f
AL
62{
63 VirtIONet *n = to_virtio_net(vdev);
64 struct virtio_net_config netcfg;
65
554c97dd 66 netcfg.status = n->status;
79674068 67 memcpy(netcfg.mac, n->mac, ETH_ALEN);
fbe78f4f
AL
68 memcpy(config, &netcfg, sizeof(netcfg));
69}
70
0f03eca6
AL
71static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
72{
73 VirtIONet *n = to_virtio_net(vdev);
74 struct virtio_net_config netcfg;
75
76 memcpy(&netcfg, config, sizeof(netcfg));
77
79674068
AL
78 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
79 memcpy(n->mac, netcfg.mac, ETH_ALEN);
0f03eca6
AL
80 qemu_format_nic_info_str(n->vc, n->mac);
81 }
82}
83
554c97dd
AL
84static void virtio_net_set_link_status(VLANClientState *vc)
85{
86 VirtIONet *n = vc->opaque;
87 uint16_t old_status = n->status;
88
89 if (vc->link_down)
90 n->status &= ~VIRTIO_NET_S_LINK_UP;
91 else
92 n->status |= VIRTIO_NET_S_LINK_UP;
93
94 if (n->status != old_status)
95 virtio_notify_config(&n->vdev);
96}
97
002437cd
AL
98static void virtio_net_reset(VirtIODevice *vdev)
99{
100 VirtIONet *n = to_virtio_net(vdev);
101
102 /* Reset back to compatibility mode */
103 n->promisc = 1;
104 n->allmulti = 0;
015cb166
AW
105 n->alluni = 0;
106 n->nomulti = 0;
107 n->nouni = 0;
108 n->nobcast = 0;
b6503ed9 109
f21c0ed9 110 /* Flush any MAC and VLAN filter table state */
b6503ed9 111 n->mac_table.in_use = 0;
2d9aba39 112 n->mac_table.first_multi = 0;
8fd2a2f1
AW
113 n->mac_table.multi_overflow = 0;
114 n->mac_table.uni_overflow = 0;
b6503ed9 115 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
f21c0ed9 116 memset(n->vlans, 0, MAX_VLAN >> 3);
002437cd
AL
117}
118
fbe78f4f
AL
119static uint32_t virtio_net_get_features(VirtIODevice *vdev)
120{
3d11d36c
AL
121 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
122 (1 << VIRTIO_NET_F_STATUS) |
b6503ed9 123 (1 << VIRTIO_NET_F_CTRL_VQ) |
f21c0ed9 124 (1 << VIRTIO_NET_F_CTRL_RX) |
015cb166
AW
125 (1 << VIRTIO_NET_F_CTRL_VLAN) |
126 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
fbe78f4f
AL
127
128 return features;
129}
130
8eca6b1b
AL
131static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
132{
133 uint32_t features = 0;
134
135 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
136 * but also these: */
137 features |= (1 << VIRTIO_NET_F_MAC);
138 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
139 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
140 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
141 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
142
143 return features & virtio_net_get_features(vdev);
144}
145
fbe78f4f
AL
146static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
147{
148 VirtIONet *n = to_virtio_net(vdev);
149
150 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
151}
152
002437cd
AL
153static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
154 VirtQueueElement *elem)
155{
156 uint8_t on;
157
158 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
159 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
160 exit(1);
161 }
162
163 on = ldub_p(elem->out_sg[1].iov_base);
164
165 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
166 n->promisc = on;
167 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
168 n->allmulti = on;
015cb166
AW
169 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
170 n->alluni = on;
171 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
172 n->nomulti = on;
173 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
174 n->nouni = on;
175 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
176 n->nobcast = on;
002437cd
AL
177 else
178 return VIRTIO_NET_ERR;
179
180 return VIRTIO_NET_OK;
181}
182
b6503ed9
AL
183static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
184 VirtQueueElement *elem)
185{
186 struct virtio_net_ctrl_mac mac_data;
187
188 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
189 elem->out_sg[1].iov_len < sizeof(mac_data) ||
190 elem->out_sg[2].iov_len < sizeof(mac_data))
191 return VIRTIO_NET_ERR;
192
193 n->mac_table.in_use = 0;
2d9aba39 194 n->mac_table.first_multi = 0;
8fd2a2f1
AW
195 n->mac_table.uni_overflow = 0;
196 n->mac_table.multi_overflow = 0;
b6503ed9
AL
197 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
198
199 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
200
201 if (sizeof(mac_data.entries) +
202 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
203 return VIRTIO_NET_ERR;
204
205 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
206 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
207 mac_data.entries * ETH_ALEN);
208 n->mac_table.in_use += mac_data.entries;
209 } else {
8fd2a2f1 210 n->mac_table.uni_overflow = 1;
b6503ed9
AL
211 }
212
2d9aba39
AW
213 n->mac_table.first_multi = n->mac_table.in_use;
214
b6503ed9
AL
215 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
216
217 if (sizeof(mac_data.entries) +
218 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
219 return VIRTIO_NET_ERR;
220
221 if (mac_data.entries) {
222 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
223 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
224 elem->out_sg[2].iov_base + sizeof(mac_data),
225 mac_data.entries * ETH_ALEN);
226 n->mac_table.in_use += mac_data.entries;
8fd2a2f1
AW
227 } else {
228 n->mac_table.multi_overflow = 1;
229 }
b6503ed9
AL
230 }
231
232 return VIRTIO_NET_OK;
233}
234
f21c0ed9
AL
235static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
236 VirtQueueElement *elem)
237{
238 uint16_t vid;
239
240 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
241 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
242 return VIRTIO_NET_ERR;
243 }
244
245 vid = lduw_le_p(elem->out_sg[1].iov_base);
246
247 if (vid >= MAX_VLAN)
248 return VIRTIO_NET_ERR;
249
250 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
251 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
252 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
253 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
254 else
255 return VIRTIO_NET_ERR;
256
257 return VIRTIO_NET_OK;
258}
259
3d11d36c
AL
260static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
261{
002437cd 262 VirtIONet *n = to_virtio_net(vdev);
3d11d36c
AL
263 struct virtio_net_ctrl_hdr ctrl;
264 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
265 VirtQueueElement elem;
266
267 while (virtqueue_pop(vq, &elem)) {
268 if ((elem.in_num < 1) || (elem.out_num < 1)) {
269 fprintf(stderr, "virtio-net ctrl missing headers\n");
270 exit(1);
271 }
272
273 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
c6bb9a32 274 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
3d11d36c
AL
275 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
276 exit(1);
277 }
278
279 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
280 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
281
002437cd
AL
282 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
283 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
b6503ed9
AL
284 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
285 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
f21c0ed9
AL
286 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
287 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
002437cd 288
3d11d36c
AL
289 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
290
291 virtqueue_push(vq, &elem, sizeof(status));
292 virtio_notify(vdev, vq);
293 }
294}
295
fbe78f4f
AL
296/* RX */
297
298static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
299{
8aeff62d
MM
300 VirtIONet *n = to_virtio_net(vdev);
301
302 qemu_flush_queued_packets(n->vc);
fbe78f4f
AL
303}
304
305static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
306{
307 if (!virtio_queue_ready(n->rx_vq) ||
308 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
309 return 0;
310
311 if (virtio_queue_empty(n->rx_vq) ||
312 (n->mergeable_rx_bufs &&
313 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
314 virtio_queue_set_notification(n->rx_vq, 1);
315 return 0;
316 }
317
318 virtio_queue_set_notification(n->rx_vq, 0);
319 return 1;
320}
321
e3f5ec2b 322static int virtio_net_can_receive(VLANClientState *vc)
fbe78f4f 323{
e3f5ec2b 324 VirtIONet *n = vc->opaque;
fbe78f4f
AL
325
326 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
327}
328
329static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
330{
331 int offset, i;
332
333 offset = i = 0;
334 while (offset < count && i < iovcnt) {
335 int len = MIN(iov[i].iov_len, count - offset);
336 memcpy(iov[i].iov_base, buf + offset, len);
337 offset += len;
338 i++;
339 }
340
341 return offset;
342}
343
344static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
4689f4b3 345 const void *buf, size_t size, size_t hdr_len)
fbe78f4f 346{
3f4cb3d3 347 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
fbe78f4f
AL
348 int offset = 0;
349
350 hdr->flags = 0;
351 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
352
353 /* We only ever receive a struct virtio_net_hdr from the tapfd,
354 * but we may be passing along a larger header to the guest.
355 */
356 iov[0].iov_base += hdr_len;
357 iov[0].iov_len -= hdr_len;
358
359 return offset;
360}
361
3831ab20
AL
362static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
363{
364 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
f21c0ed9 365 static const uint8_t vlan[] = {0x81, 0x00};
3831ab20 366 uint8_t *ptr = (uint8_t *)buf;
b6503ed9 367 int i;
3831ab20
AL
368
369 if (n->promisc)
370 return 1;
371
f21c0ed9
AL
372 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
373 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
374 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
375 return 0;
376 }
377
bbe2f399
AW
378 if (ptr[0] & 1) { // multicast
379 if (!memcmp(ptr, bcast, sizeof(bcast))) {
015cb166
AW
380 return !n->nobcast;
381 } else if (n->nomulti) {
382 return 0;
8fd2a2f1 383 } else if (n->allmulti || n->mac_table.multi_overflow) {
bbe2f399
AW
384 return 1;
385 }
2d9aba39
AW
386
387 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
388 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
389 return 1;
390 }
391 }
bbe2f399 392 } else { // unicast
015cb166
AW
393 if (n->nouni) {
394 return 0;
395 } else if (n->alluni || n->mac_table.uni_overflow) {
8fd2a2f1
AW
396 return 1;
397 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
bbe2f399
AW
398 return 1;
399 }
3831ab20 400
2d9aba39
AW
401 for (i = 0; i < n->mac_table.first_multi; i++) {
402 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
403 return 1;
404 }
405 }
b6503ed9
AL
406 }
407
3831ab20
AL
408 return 0;
409}
410
4f1c942b 411static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
fbe78f4f 412{
e3f5ec2b 413 VirtIONet *n = vc->opaque;
fbe78f4f 414 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
4689f4b3 415 size_t hdr_len, offset, i;
fbe78f4f
AL
416
417 if (!do_virtio_net_can_receive(n, size))
8aeff62d 418 return 0;
fbe78f4f 419
3831ab20 420 if (!receive_filter(n, buf, size))
4f1c942b 421 return size;
3831ab20 422
fbe78f4f
AL
423 /* hdr_len refers to the header we supply to the guest */
424 hdr_len = n->mergeable_rx_bufs ?
425 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
426
427 offset = i = 0;
428
429 while (offset < size) {
430 VirtQueueElement elem;
431 int len, total;
432 struct iovec sg[VIRTQUEUE_MAX_SIZE];
433
434 len = total = 0;
435
436 if ((i != 0 && !n->mergeable_rx_bufs) ||
437 virtqueue_pop(n->rx_vq, &elem) == 0) {
438 if (i == 0)
4f1c942b 439 return -1;
fbe78f4f
AL
440 fprintf(stderr, "virtio-net truncating packet\n");
441 exit(1);
442 }
443
444 if (elem.in_num < 1) {
445 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
446 exit(1);
447 }
448
449 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
450 fprintf(stderr, "virtio-net header not in first element\n");
451 exit(1);
452 }
453
454 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
455
456 if (i == 0) {
457 if (n->mergeable_rx_bufs)
458 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
459
460 offset += receive_header(n, sg, elem.in_num,
461 buf + offset, size - offset, hdr_len);
462 total += hdr_len;
463 }
464
465 /* copy in packet. ugh */
466 len = iov_fill(sg, elem.in_num,
467 buf + offset, size - offset);
468 total += len;
469
470 /* signal other side */
471 virtqueue_fill(n->rx_vq, &elem, total, i++);
472
473 offset += len;
474 }
475
476 if (mhdr)
477 mhdr->num_buffers = i;
478
479 virtqueue_flush(n->rx_vq, i);
480 virtio_notify(&n->vdev, n->rx_vq);
4f1c942b
MM
481
482 return size;
fbe78f4f
AL
483}
484
485/* TX */
486static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
487{
488 VirtQueueElement elem;
489 int has_vnet_hdr = 0;
490
491 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
492 return;
493
494 while (virtqueue_pop(vq, &elem)) {
495 ssize_t len = 0;
496 unsigned int out_num = elem.out_num;
497 struct iovec *out_sg = &elem.out_sg[0];
498 unsigned hdr_len;
499
500 /* hdr_len refers to the header received from the guest */
501 hdr_len = n->mergeable_rx_bufs ?
502 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
503 sizeof(struct virtio_net_hdr);
504
505 if (out_num < 1 || out_sg->iov_len != hdr_len) {
506 fprintf(stderr, "virtio-net header not in first element\n");
507 exit(1);
508 }
509
510 /* ignore the header if GSO is not supported */
511 if (!has_vnet_hdr) {
512 out_num--;
513 out_sg++;
514 len += hdr_len;
515 } else if (n->mergeable_rx_bufs) {
516 /* tapfd expects a struct virtio_net_hdr */
517 hdr_len -= sizeof(struct virtio_net_hdr);
518 out_sg->iov_len -= hdr_len;
519 len += hdr_len;
520 }
521
522 len += qemu_sendv_packet(n->vc, out_sg, out_num);
523
524 virtqueue_push(vq, &elem, len);
525 virtio_notify(&n->vdev, vq);
526 }
527}
528
529static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
530{
531 VirtIONet *n = to_virtio_net(vdev);
532
533 if (n->tx_timer_active) {
534 virtio_queue_set_notification(vq, 1);
535 qemu_del_timer(n->tx_timer);
536 n->tx_timer_active = 0;
537 virtio_net_flush_tx(n, vq);
538 } else {
539 qemu_mod_timer(n->tx_timer,
540 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
541 n->tx_timer_active = 1;
542 virtio_queue_set_notification(vq, 0);
543 }
544}
545
546static void virtio_net_tx_timer(void *opaque)
547{
548 VirtIONet *n = opaque;
549
550 n->tx_timer_active = 0;
551
552 /* Just in case the driver is not ready on more */
553 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
554 return;
555
556 virtio_queue_set_notification(n->tx_vq, 1);
557 virtio_net_flush_tx(n, n->tx_vq);
558}
559
560static void virtio_net_save(QEMUFile *f, void *opaque)
561{
562 VirtIONet *n = opaque;
563
564 virtio_save(&n->vdev, f);
565
79674068 566 qemu_put_buffer(f, n->mac, ETH_ALEN);
fbe78f4f 567 qemu_put_be32(f, n->tx_timer_active);
e46cb38f 568 qemu_put_be32(f, n->mergeable_rx_bufs);
9d6271b8 569 qemu_put_be16(f, n->status);
f10c592e
AW
570 qemu_put_byte(f, n->promisc);
571 qemu_put_byte(f, n->allmulti);
b6503ed9
AL
572 qemu_put_be32(f, n->mac_table.in_use);
573 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
f21c0ed9 574 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
6c042c16 575 qemu_put_be32(f, 0); /* vnet-hdr placeholder */
8fd2a2f1
AW
576 qemu_put_byte(f, n->mac_table.multi_overflow);
577 qemu_put_byte(f, n->mac_table.uni_overflow);
015cb166
AW
578 qemu_put_byte(f, n->alluni);
579 qemu_put_byte(f, n->nomulti);
580 qemu_put_byte(f, n->nouni);
581 qemu_put_byte(f, n->nobcast);
fbe78f4f
AL
582}
583
584static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
585{
586 VirtIONet *n = opaque;
2d9aba39 587 int i;
fbe78f4f 588
9d6271b8 589 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
fbe78f4f
AL
590 return -EINVAL;
591
592 virtio_load(&n->vdev, f);
593
79674068 594 qemu_get_buffer(f, n->mac, ETH_ALEN);
fbe78f4f 595 n->tx_timer_active = qemu_get_be32(f);
e46cb38f 596 n->mergeable_rx_bufs = qemu_get_be32(f);
fbe78f4f 597
9d6271b8
AL
598 if (version_id >= 3)
599 n->status = qemu_get_be16(f);
600
002437cd 601 if (version_id >= 4) {
f10c592e
AW
602 if (version_id < 8) {
603 n->promisc = qemu_get_be32(f);
604 n->allmulti = qemu_get_be32(f);
605 } else {
606 n->promisc = qemu_get_byte(f);
607 n->allmulti = qemu_get_byte(f);
608 }
002437cd
AL
609 }
610
b6503ed9
AL
611 if (version_id >= 5) {
612 n->mac_table.in_use = qemu_get_be32(f);
613 /* MAC_TABLE_ENTRIES may be different from the saved image */
614 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
615 qemu_get_buffer(f, n->mac_table.macs,
616 n->mac_table.in_use * ETH_ALEN);
617 } else if (n->mac_table.in_use) {
618 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
8fd2a2f1 619 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
b6503ed9
AL
620 n->mac_table.in_use = 0;
621 }
622 }
623
f21c0ed9
AL
624 if (version_id >= 6)
625 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
626
6c042c16
AW
627 if (version_id >= 7 && qemu_get_be32(f)) {
628 fprintf(stderr,
629 "virtio-net: saved image requires vnet header support\n");
630 exit(1);
631 }
632
8fd2a2f1
AW
633 if (version_id >= 9) {
634 n->mac_table.multi_overflow = qemu_get_byte(f);
635 n->mac_table.uni_overflow = qemu_get_byte(f);
636 }
637
015cb166
AW
638 if (version_id >= 10) {
639 n->alluni = qemu_get_byte(f);
640 n->nomulti = qemu_get_byte(f);
641 n->nouni = qemu_get_byte(f);
642 n->nobcast = qemu_get_byte(f);
643 }
644
2d9aba39
AW
645 /* Find the first multicast entry in the saved MAC filter */
646 for (i = 0; i < n->mac_table.in_use; i++) {
647 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
648 break;
649 }
650 }
651 n->mac_table.first_multi = i;
652
fbe78f4f
AL
653 if (n->tx_timer_active) {
654 qemu_mod_timer(n->tx_timer,
655 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
656 }
657
658 return 0;
659}
660
b946a153
AL
661static void virtio_net_cleanup(VLANClientState *vc)
662{
663 VirtIONet *n = vc->opaque;
664
665 unregister_savevm("virtio-net", n);
666
667 qemu_free(n->mac_table.macs);
668 qemu_free(n->vlans);
669
670 qemu_del_timer(n->tx_timer);
671 qemu_free_timer(n->tx_timer);
672
673 virtio_cleanup(&n->vdev);
674}
675
53c25cea 676VirtIODevice *virtio_net_init(DeviceState *dev)
fbe78f4f
AL
677{
678 VirtIONet *n;
679 static int virtio_net_id;
680
53c25cea
PB
681 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
682 sizeof(struct virtio_net_config),
683 sizeof(VirtIONet));
fbe78f4f 684
0f03eca6
AL
685 n->vdev.get_config = virtio_net_get_config;
686 n->vdev.set_config = virtio_net_set_config;
fbe78f4f
AL
687 n->vdev.get_features = virtio_net_get_features;
688 n->vdev.set_features = virtio_net_set_features;
8eca6b1b 689 n->vdev.bad_features = virtio_net_bad_features;
002437cd 690 n->vdev.reset = virtio_net_reset;
fbe78f4f
AL
691 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
692 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
3d11d36c 693 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
53c25cea 694 qdev_get_macaddr(dev, n->mac);
554c97dd 695 n->status = VIRTIO_NET_S_LINK_UP;
53c25cea 696 n->vc = qdev_get_vlan_client(dev,
b946a153 697 virtio_net_can_receive,
463af534 698 virtio_net_receive, NULL,
b946a153 699 virtio_net_cleanup, n);
554c97dd 700 n->vc->link_status_changed = virtio_net_set_link_status;
fbe78f4f 701
96d5e201
AL
702 qemu_format_nic_info_str(n->vc, n->mac);
703
fbe78f4f
AL
704 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
705 n->tx_timer_active = 0;
706 n->mergeable_rx_bufs = 0;
002437cd 707 n->promisc = 1; /* for compatibility */
fbe78f4f 708
b6503ed9 709 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
b6503ed9 710
f21c0ed9 711 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
f21c0ed9 712
9d6271b8 713 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
fbe78f4f 714 virtio_net_save, virtio_net_load, n);
cf21e106 715
53c25cea 716 return &n->vdev;
cf21e106 717}