]> git.proxmox.com Git - qemu.git/blob - hw/virtio-net.c
Update for 0.12.4 release
[qemu.git] / hw / virtio-net.c
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 "net/checksum.h"
17 #include "net/tap.h"
18 #include "qemu-timer.h"
19 #include "virtio-net.h"
20
21 #define VIRTIO_NET_VM_VERSION 11
22
23 #define MAC_TABLE_ENTRIES 64
24 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
25
26 typedef struct VirtIONet
27 {
28 VirtIODevice vdev;
29 uint8_t mac[ETH_ALEN];
30 uint16_t status;
31 VirtQueue *rx_vq;
32 VirtQueue *tx_vq;
33 VirtQueue *ctrl_vq;
34 NICState *nic;
35 QEMUTimer *tx_timer;
36 int tx_timer_active;
37 uint32_t has_vnet_hdr;
38 uint8_t has_ufo;
39 struct {
40 VirtQueueElement elem;
41 ssize_t len;
42 } async_tx;
43 int mergeable_rx_bufs;
44 uint8_t promisc;
45 uint8_t allmulti;
46 uint8_t alluni;
47 uint8_t nomulti;
48 uint8_t nouni;
49 uint8_t nobcast;
50 struct {
51 int in_use;
52 int first_multi;
53 uint8_t multi_overflow;
54 uint8_t uni_overflow;
55 uint8_t *macs;
56 } mac_table;
57 uint32_t *vlans;
58 } VirtIONet;
59
60 /* TODO
61 * - we could suppress RX interrupt if we were so inclined.
62 */
63
64 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
65 {
66 return (VirtIONet *)vdev;
67 }
68
69 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
70 {
71 VirtIONet *n = to_virtio_net(vdev);
72 struct virtio_net_config netcfg;
73
74 netcfg.status = n->status;
75 memcpy(netcfg.mac, n->mac, ETH_ALEN);
76 memcpy(config, &netcfg, sizeof(netcfg));
77 }
78
79 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
80 {
81 VirtIONet *n = to_virtio_net(vdev);
82 struct virtio_net_config netcfg;
83
84 memcpy(&netcfg, config, sizeof(netcfg));
85
86 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
87 memcpy(n->mac, netcfg.mac, ETH_ALEN);
88 qemu_format_nic_info_str(&n->nic->nc, n->mac);
89 }
90 }
91
92 static void virtio_net_set_link_status(VLANClientState *nc)
93 {
94 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
95 uint16_t old_status = n->status;
96
97 if (nc->link_down)
98 n->status &= ~VIRTIO_NET_S_LINK_UP;
99 else
100 n->status |= VIRTIO_NET_S_LINK_UP;
101
102 if (n->status != old_status)
103 virtio_notify_config(&n->vdev);
104 }
105
106 static void virtio_net_reset(VirtIODevice *vdev)
107 {
108 VirtIONet *n = to_virtio_net(vdev);
109
110 /* Reset back to compatibility mode */
111 n->promisc = 1;
112 n->allmulti = 0;
113 n->alluni = 0;
114 n->nomulti = 0;
115 n->nouni = 0;
116 n->nobcast = 0;
117
118 /* Flush any MAC and VLAN filter table state */
119 n->mac_table.in_use = 0;
120 n->mac_table.first_multi = 0;
121 n->mac_table.multi_overflow = 0;
122 n->mac_table.uni_overflow = 0;
123 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
124 memset(n->vlans, 0, MAX_VLAN >> 3);
125 }
126
127 static int peer_has_vnet_hdr(VirtIONet *n)
128 {
129 if (!n->nic->nc.peer)
130 return 0;
131
132 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
133 return 0;
134
135 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
136
137 return n->has_vnet_hdr;
138 }
139
140 static int peer_has_ufo(VirtIONet *n)
141 {
142 if (!peer_has_vnet_hdr(n))
143 return 0;
144
145 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
146
147 return n->has_ufo;
148 }
149
150 static uint32_t virtio_net_get_features(VirtIODevice *vdev)
151 {
152 VirtIONet *n = to_virtio_net(vdev);
153 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
154 (1 << VIRTIO_NET_F_MRG_RXBUF) |
155 (1 << VIRTIO_NET_F_STATUS) |
156 (1 << VIRTIO_NET_F_CTRL_VQ) |
157 (1 << VIRTIO_NET_F_CTRL_RX) |
158 (1 << VIRTIO_NET_F_CTRL_VLAN) |
159 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
160
161 if (peer_has_vnet_hdr(n)) {
162 tap_using_vnet_hdr(n->nic->nc.peer, 1);
163
164 features |= (1 << VIRTIO_NET_F_CSUM);
165 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
166 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
167 features |= (1 << VIRTIO_NET_F_HOST_ECN);
168
169 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
170 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
171 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
172 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
173
174 if (peer_has_ufo(n)) {
175 features |= (1 << VIRTIO_NET_F_GUEST_UFO);
176 features |= (1 << VIRTIO_NET_F_HOST_UFO);
177 }
178 }
179
180 return features;
181 }
182
183 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
184 {
185 uint32_t features = 0;
186
187 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
188 * but also these: */
189 features |= (1 << VIRTIO_NET_F_MAC);
190 features |= (1 << VIRTIO_NET_F_CSUM);
191 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
192 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
193 features |= (1 << VIRTIO_NET_F_HOST_ECN);
194
195 return features & virtio_net_get_features(vdev);
196 }
197
198 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
199 {
200 VirtIONet *n = to_virtio_net(vdev);
201
202 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
203
204 if (n->has_vnet_hdr) {
205 tap_set_offload(n->nic->nc.peer,
206 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
207 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
208 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
209 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
210 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
211 }
212 }
213
214 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
215 VirtQueueElement *elem)
216 {
217 uint8_t on;
218
219 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
220 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
221 exit(1);
222 }
223
224 on = ldub_p(elem->out_sg[1].iov_base);
225
226 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
227 n->promisc = on;
228 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
229 n->allmulti = on;
230 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
231 n->alluni = on;
232 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
233 n->nomulti = on;
234 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
235 n->nouni = on;
236 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
237 n->nobcast = on;
238 else
239 return VIRTIO_NET_ERR;
240
241 return VIRTIO_NET_OK;
242 }
243
244 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
245 VirtQueueElement *elem)
246 {
247 struct virtio_net_ctrl_mac mac_data;
248
249 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
250 elem->out_sg[1].iov_len < sizeof(mac_data) ||
251 elem->out_sg[2].iov_len < sizeof(mac_data))
252 return VIRTIO_NET_ERR;
253
254 n->mac_table.in_use = 0;
255 n->mac_table.first_multi = 0;
256 n->mac_table.uni_overflow = 0;
257 n->mac_table.multi_overflow = 0;
258 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
259
260 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
261
262 if (sizeof(mac_data.entries) +
263 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
264 return VIRTIO_NET_ERR;
265
266 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
267 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
268 mac_data.entries * ETH_ALEN);
269 n->mac_table.in_use += mac_data.entries;
270 } else {
271 n->mac_table.uni_overflow = 1;
272 }
273
274 n->mac_table.first_multi = n->mac_table.in_use;
275
276 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
277
278 if (sizeof(mac_data.entries) +
279 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
280 return VIRTIO_NET_ERR;
281
282 if (mac_data.entries) {
283 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
284 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
285 elem->out_sg[2].iov_base + sizeof(mac_data),
286 mac_data.entries * ETH_ALEN);
287 n->mac_table.in_use += mac_data.entries;
288 } else {
289 n->mac_table.multi_overflow = 1;
290 }
291 }
292
293 return VIRTIO_NET_OK;
294 }
295
296 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
297 VirtQueueElement *elem)
298 {
299 uint16_t vid;
300
301 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
302 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
303 return VIRTIO_NET_ERR;
304 }
305
306 vid = lduw_le_p(elem->out_sg[1].iov_base);
307
308 if (vid >= MAX_VLAN)
309 return VIRTIO_NET_ERR;
310
311 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
312 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
313 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
314 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
315 else
316 return VIRTIO_NET_ERR;
317
318 return VIRTIO_NET_OK;
319 }
320
321 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
322 {
323 VirtIONet *n = to_virtio_net(vdev);
324 struct virtio_net_ctrl_hdr ctrl;
325 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
326 VirtQueueElement elem;
327
328 while (virtqueue_pop(vq, &elem)) {
329 if ((elem.in_num < 1) || (elem.out_num < 1)) {
330 fprintf(stderr, "virtio-net ctrl missing headers\n");
331 exit(1);
332 }
333
334 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
335 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
336 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
337 exit(1);
338 }
339
340 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
341 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
342
343 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
344 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
345 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
346 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
347 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
348 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
349
350 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
351
352 virtqueue_push(vq, &elem, sizeof(status));
353 virtio_notify(vdev, vq);
354 }
355 }
356
357 /* RX */
358
359 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
360 {
361 VirtIONet *n = to_virtio_net(vdev);
362
363 qemu_flush_queued_packets(&n->nic->nc);
364
365 /* We now have RX buffers, signal to the IO thread to break out of the
366 * select to re-poll the tap file descriptor */
367 qemu_notify_event();
368 }
369
370 static int virtio_net_can_receive(VLANClientState *nc)
371 {
372 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
373
374 if (!virtio_queue_ready(n->rx_vq) ||
375 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
376 return 0;
377
378 return 1;
379 }
380
381 static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
382 {
383 if (virtio_queue_empty(n->rx_vq) ||
384 (n->mergeable_rx_bufs &&
385 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
386 virtio_queue_set_notification(n->rx_vq, 1);
387
388 /* To avoid a race condition where the guest has made some buffers
389 * available after the above check but before notification was
390 * enabled, check for available buffers again.
391 */
392 if (virtio_queue_empty(n->rx_vq) ||
393 (n->mergeable_rx_bufs &&
394 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
395 return 0;
396 }
397
398 virtio_queue_set_notification(n->rx_vq, 0);
399 return 1;
400 }
401
402 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
403 * it never finds out that the packets don't have valid checksums. This
404 * causes dhclient to get upset. Fedora's carried a patch for ages to
405 * fix this with Xen but it hasn't appeared in an upstream release of
406 * dhclient yet.
407 *
408 * To avoid breaking existing guests, we catch udp packets and add
409 * checksums. This is terrible but it's better than hacking the guest
410 * kernels.
411 *
412 * N.B. if we introduce a zero-copy API, this operation is no longer free so
413 * we should provide a mechanism to disable it to avoid polluting the host
414 * cache.
415 */
416 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
417 const uint8_t *buf, size_t size)
418 {
419 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
420 (size > 27 && size < 1500) && /* normal sized MTU */
421 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
422 (buf[23] == 17) && /* ip.protocol == UDP */
423 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
424 /* FIXME this cast is evil */
425 net_checksum_calculate((uint8_t *)buf, size);
426 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
427 }
428 }
429
430 static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
431 {
432 int offset, i;
433
434 offset = i = 0;
435 while (offset < count && i < iovcnt) {
436 int len = MIN(iov[i].iov_len, count - offset);
437 memcpy(iov[i].iov_base, buf + offset, len);
438 offset += len;
439 i++;
440 }
441
442 return offset;
443 }
444
445 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
446 const void *buf, size_t size, size_t hdr_len)
447 {
448 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
449 int offset = 0;
450
451 hdr->flags = 0;
452 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
453
454 if (n->has_vnet_hdr) {
455 memcpy(hdr, buf, sizeof(*hdr));
456 offset = sizeof(*hdr);
457 work_around_broken_dhclient(hdr, buf + offset, size - offset);
458 }
459
460 /* We only ever receive a struct virtio_net_hdr from the tapfd,
461 * but we may be passing along a larger header to the guest.
462 */
463 iov[0].iov_base += hdr_len;
464 iov[0].iov_len -= hdr_len;
465
466 return offset;
467 }
468
469 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
470 {
471 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
472 static const uint8_t vlan[] = {0x81, 0x00};
473 uint8_t *ptr = (uint8_t *)buf;
474 int i;
475
476 if (n->promisc)
477 return 1;
478
479 if (n->has_vnet_hdr) {
480 ptr += sizeof(struct virtio_net_hdr);
481 }
482
483 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
484 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
485 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
486 return 0;
487 }
488
489 if (ptr[0] & 1) { // multicast
490 if (!memcmp(ptr, bcast, sizeof(bcast))) {
491 return !n->nobcast;
492 } else if (n->nomulti) {
493 return 0;
494 } else if (n->allmulti || n->mac_table.multi_overflow) {
495 return 1;
496 }
497
498 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
499 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
500 return 1;
501 }
502 }
503 } else { // unicast
504 if (n->nouni) {
505 return 0;
506 } else if (n->alluni || n->mac_table.uni_overflow) {
507 return 1;
508 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
509 return 1;
510 }
511
512 for (i = 0; i < n->mac_table.first_multi; i++) {
513 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
514 return 1;
515 }
516 }
517 }
518
519 return 0;
520 }
521
522 static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
523 {
524 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
525 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
526 size_t hdr_len, offset, i;
527
528 if (!virtio_net_can_receive(&n->nic->nc))
529 return -1;
530
531 if (!virtio_net_has_buffers(n, size))
532 return 0;
533
534 if (!receive_filter(n, buf, size))
535 return size;
536
537 /* hdr_len refers to the header we supply to the guest */
538 hdr_len = n->mergeable_rx_bufs ?
539 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
540
541 offset = i = 0;
542
543 while (offset < size) {
544 VirtQueueElement elem;
545 int len, total;
546 struct iovec sg[VIRTQUEUE_MAX_SIZE];
547
548 len = total = 0;
549
550 if ((i != 0 && !n->mergeable_rx_bufs) ||
551 virtqueue_pop(n->rx_vq, &elem) == 0) {
552 if (i == 0)
553 return -1;
554 fprintf(stderr, "virtio-net truncating packet\n");
555 exit(1);
556 }
557
558 if (elem.in_num < 1) {
559 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
560 exit(1);
561 }
562
563 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
564 fprintf(stderr, "virtio-net header not in first element\n");
565 exit(1);
566 }
567
568 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
569
570 if (i == 0) {
571 if (n->mergeable_rx_bufs)
572 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
573
574 offset += receive_header(n, sg, elem.in_num,
575 buf + offset, size - offset, hdr_len);
576 total += hdr_len;
577 }
578
579 /* copy in packet. ugh */
580 len = iov_fill(sg, elem.in_num,
581 buf + offset, size - offset);
582 total += len;
583
584 /* signal other side */
585 virtqueue_fill(n->rx_vq, &elem, total, i++);
586
587 offset += len;
588 }
589
590 if (mhdr)
591 mhdr->num_buffers = i;
592
593 virtqueue_flush(n->rx_vq, i);
594 virtio_notify(&n->vdev, n->rx_vq);
595
596 return size;
597 }
598
599 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
600
601 static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
602 {
603 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
604
605 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
606 virtio_notify(&n->vdev, n->tx_vq);
607
608 n->async_tx.elem.out_num = n->async_tx.len = 0;
609
610 virtio_queue_set_notification(n->tx_vq, 1);
611 virtio_net_flush_tx(n, n->tx_vq);
612 }
613
614 /* TX */
615 static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
616 {
617 VirtQueueElement elem;
618
619 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
620 return;
621
622 if (n->async_tx.elem.out_num) {
623 virtio_queue_set_notification(n->tx_vq, 0);
624 return;
625 }
626
627 while (virtqueue_pop(vq, &elem)) {
628 ssize_t ret, len = 0;
629 unsigned int out_num = elem.out_num;
630 struct iovec *out_sg = &elem.out_sg[0];
631 unsigned hdr_len;
632
633 /* hdr_len refers to the header received from the guest */
634 hdr_len = n->mergeable_rx_bufs ?
635 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
636 sizeof(struct virtio_net_hdr);
637
638 if (out_num < 1 || out_sg->iov_len != hdr_len) {
639 fprintf(stderr, "virtio-net header not in first element\n");
640 exit(1);
641 }
642
643 /* ignore the header if GSO is not supported */
644 if (!n->has_vnet_hdr) {
645 out_num--;
646 out_sg++;
647 len += hdr_len;
648 } else if (n->mergeable_rx_bufs) {
649 /* tapfd expects a struct virtio_net_hdr */
650 hdr_len -= sizeof(struct virtio_net_hdr);
651 out_sg->iov_len -= hdr_len;
652 len += hdr_len;
653 }
654
655 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
656 virtio_net_tx_complete);
657 if (ret == 0) {
658 virtio_queue_set_notification(n->tx_vq, 0);
659 n->async_tx.elem = elem;
660 n->async_tx.len = len;
661 return;
662 }
663
664 len += ret;
665
666 virtqueue_push(vq, &elem, len);
667 virtio_notify(&n->vdev, vq);
668 }
669 }
670
671 static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
672 {
673 VirtIONet *n = to_virtio_net(vdev);
674
675 if (n->tx_timer_active) {
676 virtio_queue_set_notification(vq, 1);
677 qemu_del_timer(n->tx_timer);
678 n->tx_timer_active = 0;
679 virtio_net_flush_tx(n, vq);
680 } else {
681 qemu_mod_timer(n->tx_timer,
682 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
683 n->tx_timer_active = 1;
684 virtio_queue_set_notification(vq, 0);
685 }
686 }
687
688 static void virtio_net_tx_timer(void *opaque)
689 {
690 VirtIONet *n = opaque;
691
692 n->tx_timer_active = 0;
693
694 /* Just in case the driver is not ready on more */
695 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
696 return;
697
698 virtio_queue_set_notification(n->tx_vq, 1);
699 virtio_net_flush_tx(n, n->tx_vq);
700 }
701
702 static void virtio_net_save(QEMUFile *f, void *opaque)
703 {
704 VirtIONet *n = opaque;
705
706 virtio_save(&n->vdev, f);
707
708 qemu_put_buffer(f, n->mac, ETH_ALEN);
709 qemu_put_be32(f, n->tx_timer_active);
710 qemu_put_be32(f, n->mergeable_rx_bufs);
711 qemu_put_be16(f, n->status);
712 qemu_put_byte(f, n->promisc);
713 qemu_put_byte(f, n->allmulti);
714 qemu_put_be32(f, n->mac_table.in_use);
715 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
716 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
717 qemu_put_be32(f, n->has_vnet_hdr);
718 qemu_put_byte(f, n->mac_table.multi_overflow);
719 qemu_put_byte(f, n->mac_table.uni_overflow);
720 qemu_put_byte(f, n->alluni);
721 qemu_put_byte(f, n->nomulti);
722 qemu_put_byte(f, n->nouni);
723 qemu_put_byte(f, n->nobcast);
724 qemu_put_byte(f, n->has_ufo);
725 }
726
727 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
728 {
729 VirtIONet *n = opaque;
730 int i;
731
732 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
733 return -EINVAL;
734
735 virtio_load(&n->vdev, f);
736
737 qemu_get_buffer(f, n->mac, ETH_ALEN);
738 n->tx_timer_active = qemu_get_be32(f);
739 n->mergeable_rx_bufs = qemu_get_be32(f);
740
741 if (version_id >= 3)
742 n->status = qemu_get_be16(f);
743
744 if (version_id >= 4) {
745 if (version_id < 8) {
746 n->promisc = qemu_get_be32(f);
747 n->allmulti = qemu_get_be32(f);
748 } else {
749 n->promisc = qemu_get_byte(f);
750 n->allmulti = qemu_get_byte(f);
751 }
752 }
753
754 if (version_id >= 5) {
755 n->mac_table.in_use = qemu_get_be32(f);
756 /* MAC_TABLE_ENTRIES may be different from the saved image */
757 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
758 qemu_get_buffer(f, n->mac_table.macs,
759 n->mac_table.in_use * ETH_ALEN);
760 } else if (n->mac_table.in_use) {
761 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
762 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
763 n->mac_table.in_use = 0;
764 }
765 }
766
767 if (version_id >= 6)
768 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
769
770 if (version_id >= 7) {
771 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
772 qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
773 return -1;
774 }
775
776 if (n->has_vnet_hdr) {
777 tap_using_vnet_hdr(n->nic->nc.peer, 1);
778 tap_set_offload(n->nic->nc.peer,
779 (n->vdev.features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
780 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
781 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
782 (n->vdev.features >> VIRTIO_NET_F_GUEST_ECN) & 1,
783 (n->vdev.features >> VIRTIO_NET_F_GUEST_UFO) & 1);
784 }
785 }
786
787 if (version_id >= 9) {
788 n->mac_table.multi_overflow = qemu_get_byte(f);
789 n->mac_table.uni_overflow = qemu_get_byte(f);
790 }
791
792 if (version_id >= 10) {
793 n->alluni = qemu_get_byte(f);
794 n->nomulti = qemu_get_byte(f);
795 n->nouni = qemu_get_byte(f);
796 n->nobcast = qemu_get_byte(f);
797 }
798
799 if (version_id >= 11) {
800 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
801 qemu_error("virtio-net: saved image requires TUN_F_UFO support\n");
802 return -1;
803 }
804 }
805
806 /* Find the first multicast entry in the saved MAC filter */
807 for (i = 0; i < n->mac_table.in_use; i++) {
808 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
809 break;
810 }
811 }
812 n->mac_table.first_multi = i;
813
814 if (n->tx_timer_active) {
815 qemu_mod_timer(n->tx_timer,
816 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
817 }
818
819 return 0;
820 }
821
822 static void virtio_net_cleanup(VLANClientState *nc)
823 {
824 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
825
826 n->nic = NULL;
827 }
828
829 static NetClientInfo net_virtio_info = {
830 .type = NET_CLIENT_TYPE_NIC,
831 .size = sizeof(NICState),
832 .can_receive = virtio_net_can_receive,
833 .receive = virtio_net_receive,
834 .cleanup = virtio_net_cleanup,
835 .link_status_changed = virtio_net_set_link_status,
836 };
837
838 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
839 {
840 VirtIONet *n;
841 static int virtio_net_id;
842
843 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
844 sizeof(struct virtio_net_config),
845 sizeof(VirtIONet));
846
847 n->vdev.get_config = virtio_net_get_config;
848 n->vdev.set_config = virtio_net_set_config;
849 n->vdev.get_features = virtio_net_get_features;
850 n->vdev.set_features = virtio_net_set_features;
851 n->vdev.bad_features = virtio_net_bad_features;
852 n->vdev.reset = virtio_net_reset;
853 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
854 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
855 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
856 qemu_macaddr_default_if_unset(&conf->macaddr);
857 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
858 n->status = VIRTIO_NET_S_LINK_UP;
859
860 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
861
862 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
863
864 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
865 n->tx_timer_active = 0;
866 n->mergeable_rx_bufs = 0;
867 n->promisc = 1; /* for compatibility */
868
869 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
870
871 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
872
873 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
874 virtio_net_save, virtio_net_load, n);
875
876 return &n->vdev;
877 }
878
879 void virtio_net_exit(VirtIODevice *vdev)
880 {
881 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
882
883 qemu_purge_queued_packets(&n->nic->nc);
884
885 unregister_savevm("virtio-net", n);
886
887 qemu_free(n->mac_table.macs);
888 qemu_free(n->vlans);
889
890 qemu_del_timer(n->tx_timer);
891 qemu_free_timer(n->tx_timer);
892
893 virtio_cleanup(&n->vdev);
894 qemu_del_vlan_client(&n->nic->nc);
895 }