console: Avoid dereferencing NULL active_console
[qemu-kvm/stefanha.git] / hw / virtio-net.c
blob0a9cae22c45ead87c34d5383868c379be1ace5ba
1 /*
2 * Virtio Network Device
4 * Copyright IBM, Corp. 2007
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "iov.h"
15 #include "virtio.h"
16 #include "net.h"
17 #include "net/checksum.h"
18 #include "net/tap.h"
19 #include "qemu-error.h"
20 #include "qemu-timer.h"
21 #include "virtio-net.h"
22 #include "vhost_net.h"
24 #define VIRTIO_NET_VM_VERSION 11
26 #define MAC_TABLE_ENTRIES 64
27 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
29 typedef struct VirtIONet
31 VirtIODevice vdev;
32 uint8_t mac[ETH_ALEN];
33 uint16_t status;
34 VirtQueue *rx_vq;
35 VirtQueue *tx_vq;
36 VirtQueue *ctrl_vq;
37 NICState *nic;
38 QEMUTimer *tx_timer;
39 QEMUBH *tx_bh;
40 uint32_t tx_timeout;
41 int32_t tx_burst;
42 int tx_waiting;
43 uint32_t has_vnet_hdr;
44 uint8_t has_ufo;
45 struct {
46 VirtQueueElement elem;
47 ssize_t len;
48 } async_tx;
49 int mergeable_rx_bufs;
50 uint8_t promisc;
51 uint8_t allmulti;
52 uint8_t alluni;
53 uint8_t nomulti;
54 uint8_t nouni;
55 uint8_t nobcast;
56 uint8_t vhost_started;
57 VMChangeStateEntry *vmstate;
58 struct {
59 int in_use;
60 int first_multi;
61 uint8_t multi_overflow;
62 uint8_t uni_overflow;
63 uint8_t *macs;
64 } mac_table;
65 uint32_t *vlans;
66 DeviceState *qdev;
67 } VirtIONet;
69 /* TODO
70 * - we could suppress RX interrupt if we were so inclined.
73 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
75 return (VirtIONet *)vdev;
78 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
80 VirtIONet *n = to_virtio_net(vdev);
81 struct virtio_net_config netcfg;
83 netcfg.status = n->status;
84 memcpy(netcfg.mac, n->mac, ETH_ALEN);
85 memcpy(config, &netcfg, sizeof(netcfg));
88 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
90 VirtIONet *n = to_virtio_net(vdev);
91 struct virtio_net_config netcfg;
93 memcpy(&netcfg, config, sizeof(netcfg));
95 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
96 memcpy(n->mac, netcfg.mac, ETH_ALEN);
97 qemu_format_nic_info_str(&n->nic->nc, n->mac);
101 static void virtio_net_set_link_status(VLANClientState *nc)
103 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
104 uint16_t old_status = n->status;
106 if (nc->link_down)
107 n->status &= ~VIRTIO_NET_S_LINK_UP;
108 else
109 n->status |= VIRTIO_NET_S_LINK_UP;
111 if (n->status != old_status)
112 virtio_notify_config(&n->vdev);
115 static void virtio_net_reset(VirtIODevice *vdev)
117 VirtIONet *n = to_virtio_net(vdev);
119 /* Reset back to compatibility mode */
120 n->promisc = 1;
121 n->allmulti = 0;
122 n->alluni = 0;
123 n->nomulti = 0;
124 n->nouni = 0;
125 n->nobcast = 0;
126 if (n->vhost_started) {
127 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
128 n->vhost_started = 0;
131 /* Flush any MAC and VLAN filter table state */
132 n->mac_table.in_use = 0;
133 n->mac_table.first_multi = 0;
134 n->mac_table.multi_overflow = 0;
135 n->mac_table.uni_overflow = 0;
136 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
137 memset(n->vlans, 0, MAX_VLAN >> 3);
140 static int peer_has_vnet_hdr(VirtIONet *n)
142 if (!n->nic->nc.peer)
143 return 0;
145 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
146 return 0;
148 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
150 return n->has_vnet_hdr;
153 static int peer_has_ufo(VirtIONet *n)
155 if (!peer_has_vnet_hdr(n))
156 return 0;
158 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
160 return n->has_ufo;
163 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
165 VirtIONet *n = to_virtio_net(vdev);
167 features |= (1 << VIRTIO_NET_F_MAC);
169 if (peer_has_vnet_hdr(n)) {
170 tap_using_vnet_hdr(n->nic->nc.peer, 1);
171 } else {
172 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
173 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
174 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
175 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
177 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
178 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
179 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
180 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
183 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
184 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
185 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
188 if (!n->nic->nc.peer ||
189 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
190 return features;
192 if (!tap_get_vhost_net(n->nic->nc.peer)) {
193 return features;
195 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
198 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
200 uint32_t features = 0;
202 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
203 * but also these: */
204 features |= (1 << VIRTIO_NET_F_MAC);
205 features |= (1 << VIRTIO_NET_F_CSUM);
206 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
207 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
208 features |= (1 << VIRTIO_NET_F_HOST_ECN);
210 return features;
213 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
215 VirtIONet *n = to_virtio_net(vdev);
217 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
219 if (n->has_vnet_hdr) {
220 tap_set_offload(n->nic->nc.peer,
221 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
222 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
223 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
224 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
225 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
227 if (!n->nic->nc.peer ||
228 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
229 return;
231 if (!tap_get_vhost_net(n->nic->nc.peer)) {
232 return;
234 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
237 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
238 VirtQueueElement *elem)
240 uint8_t on;
242 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
243 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
244 exit(1);
247 on = ldub_p(elem->out_sg[1].iov_base);
249 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
250 n->promisc = on;
251 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
252 n->allmulti = on;
253 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
254 n->alluni = on;
255 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
256 n->nomulti = on;
257 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
258 n->nouni = on;
259 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
260 n->nobcast = on;
261 else
262 return VIRTIO_NET_ERR;
264 return VIRTIO_NET_OK;
267 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
268 VirtQueueElement *elem)
270 struct virtio_net_ctrl_mac mac_data;
272 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
273 elem->out_sg[1].iov_len < sizeof(mac_data) ||
274 elem->out_sg[2].iov_len < sizeof(mac_data))
275 return VIRTIO_NET_ERR;
277 n->mac_table.in_use = 0;
278 n->mac_table.first_multi = 0;
279 n->mac_table.uni_overflow = 0;
280 n->mac_table.multi_overflow = 0;
281 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
283 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
285 if (sizeof(mac_data.entries) +
286 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
287 return VIRTIO_NET_ERR;
289 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
290 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
291 mac_data.entries * ETH_ALEN);
292 n->mac_table.in_use += mac_data.entries;
293 } else {
294 n->mac_table.uni_overflow = 1;
297 n->mac_table.first_multi = n->mac_table.in_use;
299 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
301 if (sizeof(mac_data.entries) +
302 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
303 return VIRTIO_NET_ERR;
305 if (mac_data.entries) {
306 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
307 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
308 elem->out_sg[2].iov_base + sizeof(mac_data),
309 mac_data.entries * ETH_ALEN);
310 n->mac_table.in_use += mac_data.entries;
311 } else {
312 n->mac_table.multi_overflow = 1;
316 return VIRTIO_NET_OK;
319 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
320 VirtQueueElement *elem)
322 uint16_t vid;
324 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
325 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
326 return VIRTIO_NET_ERR;
329 vid = lduw_le_p(elem->out_sg[1].iov_base);
331 if (vid >= MAX_VLAN)
332 return VIRTIO_NET_ERR;
334 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
335 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
336 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
337 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
338 else
339 return VIRTIO_NET_ERR;
341 return VIRTIO_NET_OK;
344 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
346 VirtIONet *n = to_virtio_net(vdev);
347 struct virtio_net_ctrl_hdr ctrl;
348 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
349 VirtQueueElement elem;
351 while (virtqueue_pop(vq, &elem)) {
352 if ((elem.in_num < 1) || (elem.out_num < 1)) {
353 fprintf(stderr, "virtio-net ctrl missing headers\n");
354 exit(1);
357 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
358 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
359 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
360 exit(1);
363 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
364 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
366 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
367 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
368 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
369 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
370 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
371 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
373 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
375 virtqueue_push(vq, &elem, sizeof(status));
376 virtio_notify(vdev, vq);
380 /* RX */
382 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
384 VirtIONet *n = to_virtio_net(vdev);
386 qemu_flush_queued_packets(&n->nic->nc);
388 /* We now have RX buffers, signal to the IO thread to break out of the
389 * select to re-poll the tap file descriptor */
390 qemu_notify_event();
393 static int virtio_net_can_receive(VLANClientState *nc)
395 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
397 if (!virtio_queue_ready(n->rx_vq) ||
398 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
399 return 0;
401 return 1;
404 static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
406 if (virtio_queue_empty(n->rx_vq) ||
407 (n->mergeable_rx_bufs &&
408 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
409 virtio_queue_set_notification(n->rx_vq, 1);
411 /* To avoid a race condition where the guest has made some buffers
412 * available after the above check but before notification was
413 * enabled, check for available buffers again.
415 if (virtio_queue_empty(n->rx_vq) ||
416 (n->mergeable_rx_bufs &&
417 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
418 return 0;
421 virtio_queue_set_notification(n->rx_vq, 0);
422 return 1;
425 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
426 * it never finds out that the packets don't have valid checksums. This
427 * causes dhclient to get upset. Fedora's carried a patch for ages to
428 * fix this with Xen but it hasn't appeared in an upstream release of
429 * dhclient yet.
431 * To avoid breaking existing guests, we catch udp packets and add
432 * checksums. This is terrible but it's better than hacking the guest
433 * kernels.
435 * N.B. if we introduce a zero-copy API, this operation is no longer free so
436 * we should provide a mechanism to disable it to avoid polluting the host
437 * cache.
439 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
440 const uint8_t *buf, size_t size)
442 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
443 (size > 27 && size < 1500) && /* normal sized MTU */
444 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
445 (buf[23] == 17) && /* ip.protocol == UDP */
446 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
447 /* FIXME this cast is evil */
448 net_checksum_calculate((uint8_t *)buf, size);
449 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
453 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
454 const void *buf, size_t size, size_t hdr_len)
456 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
457 int offset = 0;
459 hdr->flags = 0;
460 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
462 if (n->has_vnet_hdr) {
463 memcpy(hdr, buf, sizeof(*hdr));
464 offset = sizeof(*hdr);
465 work_around_broken_dhclient(hdr, buf + offset, size - offset);
468 /* We only ever receive a struct virtio_net_hdr from the tapfd,
469 * but we may be passing along a larger header to the guest.
471 iov[0].iov_base += hdr_len;
472 iov[0].iov_len -= hdr_len;
474 return offset;
477 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
479 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
480 static const uint8_t vlan[] = {0x81, 0x00};
481 uint8_t *ptr = (uint8_t *)buf;
482 int i;
484 if (n->promisc)
485 return 1;
487 if (n->has_vnet_hdr) {
488 ptr += sizeof(struct virtio_net_hdr);
491 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
492 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
493 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
494 return 0;
497 if (ptr[0] & 1) { // multicast
498 if (!memcmp(ptr, bcast, sizeof(bcast))) {
499 return !n->nobcast;
500 } else if (n->nomulti) {
501 return 0;
502 } else if (n->allmulti || n->mac_table.multi_overflow) {
503 return 1;
506 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
507 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
508 return 1;
511 } else { // unicast
512 if (n->nouni) {
513 return 0;
514 } else if (n->alluni || n->mac_table.uni_overflow) {
515 return 1;
516 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
517 return 1;
520 for (i = 0; i < n->mac_table.first_multi; i++) {
521 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
522 return 1;
527 return 0;
530 static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
532 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
533 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
534 size_t guest_hdr_len, offset, i, host_hdr_len;
536 if (!virtio_net_can_receive(&n->nic->nc))
537 return -1;
539 /* hdr_len refers to the header we supply to the guest */
540 guest_hdr_len = n->mergeable_rx_bufs ?
541 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
544 host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
545 if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
546 return 0;
548 if (!receive_filter(n, buf, size))
549 return size;
551 offset = i = 0;
553 while (offset < size) {
554 VirtQueueElement elem;
555 int len, total;
556 struct iovec sg[VIRTQUEUE_MAX_SIZE];
558 total = 0;
560 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
561 if (i == 0)
562 return -1;
563 fprintf(stderr, "virtio-net unexpected empty queue: "
564 "i %zd mergeable %d offset %zd, size %zd, "
565 "guest hdr len %zd, host hdr len %zd guest features 0x%x\n",
566 i, n->mergeable_rx_bufs, offset, size,
567 guest_hdr_len, host_hdr_len, n->vdev.guest_features);
568 exit(1);
571 if (elem.in_num < 1) {
572 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
573 exit(1);
576 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
577 fprintf(stderr, "virtio-net header not in first element\n");
578 exit(1);
581 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
583 if (i == 0) {
584 if (n->mergeable_rx_bufs)
585 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
587 offset += receive_header(n, sg, elem.in_num,
588 buf + offset, size - offset, guest_hdr_len);
589 total += guest_hdr_len;
592 /* copy in packet. ugh */
593 len = iov_from_buf(sg, elem.in_num,
594 buf + offset, size - offset);
595 total += len;
596 offset += len;
597 /* If buffers can't be merged, at this point we
598 * must have consumed the complete packet.
599 * Otherwise, drop it. */
600 if (!n->mergeable_rx_bufs && offset < size) {
601 #if 0
602 fprintf(stderr, "virtio-net truncated non-mergeable packet: "
604 "i %zd mergeable %d offset %zd, size %zd, "
605 "guest hdr len %zd, host hdr len %zd\n",
606 i, n->mergeable_rx_bufs,
607 offset, size, guest_hdr_len, host_hdr_len);
608 #endif
609 return size;
612 /* signal other side */
613 virtqueue_fill(n->rx_vq, &elem, total, i++);
616 if (mhdr)
617 mhdr->num_buffers = i;
619 virtqueue_flush(n->rx_vq, i);
620 virtio_notify(&n->vdev, n->rx_vq);
622 return size;
625 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
627 static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
629 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
631 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
632 virtio_notify(&n->vdev, n->tx_vq);
634 n->async_tx.elem.out_num = n->async_tx.len = 0;
636 virtio_queue_set_notification(n->tx_vq, 1);
637 virtio_net_flush_tx(n, n->tx_vq);
640 /* TX */
641 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
643 VirtQueueElement elem;
644 int32_t num_packets = 0;
646 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
647 return num_packets;
650 if (n->async_tx.elem.out_num) {
651 virtio_queue_set_notification(n->tx_vq, 0);
652 return num_packets;
655 while (virtqueue_pop(vq, &elem)) {
656 ssize_t ret, len = 0;
657 unsigned int out_num = elem.out_num;
658 struct iovec *out_sg = &elem.out_sg[0];
659 unsigned hdr_len;
661 /* hdr_len refers to the header received from the guest */
662 hdr_len = n->mergeable_rx_bufs ?
663 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
664 sizeof(struct virtio_net_hdr);
666 if (out_num < 1 || out_sg->iov_len != hdr_len) {
667 fprintf(stderr, "virtio-net header not in first element\n");
668 exit(1);
671 /* ignore the header if GSO is not supported */
672 if (!n->has_vnet_hdr) {
673 out_num--;
674 out_sg++;
675 len += hdr_len;
676 } else if (n->mergeable_rx_bufs) {
677 /* tapfd expects a struct virtio_net_hdr */
678 hdr_len -= sizeof(struct virtio_net_hdr);
679 out_sg->iov_len -= hdr_len;
680 len += hdr_len;
683 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
684 virtio_net_tx_complete);
685 if (ret == 0) {
686 virtio_queue_set_notification(n->tx_vq, 0);
687 n->async_tx.elem = elem;
688 n->async_tx.len = len;
689 return -EBUSY;
692 len += ret;
694 virtqueue_push(vq, &elem, len);
695 virtio_notify(&n->vdev, vq);
697 if (++num_packets >= n->tx_burst) {
698 break;
701 return num_packets;
704 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
706 VirtIONet *n = to_virtio_net(vdev);
708 if (n->tx_waiting) {
709 virtio_queue_set_notification(vq, 1);
710 qemu_del_timer(n->tx_timer);
711 n->tx_waiting = 0;
712 virtio_net_flush_tx(n, vq);
713 } else {
714 qemu_mod_timer(n->tx_timer,
715 qemu_get_clock(vm_clock) + n->tx_timeout);
716 n->tx_waiting = 1;
717 virtio_queue_set_notification(vq, 0);
721 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
723 VirtIONet *n = to_virtio_net(vdev);
725 if (unlikely(n->tx_waiting)) {
726 return;
728 virtio_queue_set_notification(vq, 0);
729 qemu_bh_schedule(n->tx_bh);
730 n->tx_waiting = 1;
733 static void virtio_net_tx_timer(void *opaque)
735 VirtIONet *n = opaque;
737 n->tx_waiting = 0;
739 /* Just in case the driver is not ready on more */
740 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
741 return;
743 virtio_queue_set_notification(n->tx_vq, 1);
744 virtio_net_flush_tx(n, n->tx_vq);
747 static void virtio_net_tx_bh(void *opaque)
749 VirtIONet *n = opaque;
750 int32_t ret;
752 n->tx_waiting = 0;
754 /* Just in case the driver is not ready on more */
755 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
756 return;
758 ret = virtio_net_flush_tx(n, n->tx_vq);
759 if (ret == -EBUSY) {
760 return; /* Notification re-enable handled by tx_complete */
763 /* If we flush a full burst of packets, assume there are
764 * more coming and immediately reschedule */
765 if (ret >= n->tx_burst) {
766 qemu_bh_schedule(n->tx_bh);
767 n->tx_waiting = 1;
768 return;
771 /* If less than a full burst, re-enable notification and flush
772 * anything that may have come in while we weren't looking. If
773 * we find something, assume the guest is still active and reschedule */
774 virtio_queue_set_notification(n->tx_vq, 1);
775 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
776 virtio_queue_set_notification(n->tx_vq, 0);
777 qemu_bh_schedule(n->tx_bh);
778 n->tx_waiting = 1;
782 static void virtio_net_save(QEMUFile *f, void *opaque)
784 VirtIONet *n = opaque;
786 if (n->vhost_started) {
787 /* TODO: should we really stop the backend?
788 * If we don't, it might keep writing to memory. */
789 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
790 n->vhost_started = 0;
792 virtio_save(&n->vdev, f);
794 qemu_put_buffer(f, n->mac, ETH_ALEN);
795 qemu_put_be32(f, n->tx_waiting);
796 qemu_put_be32(f, n->mergeable_rx_bufs);
797 qemu_put_be16(f, n->status);
798 qemu_put_byte(f, n->promisc);
799 qemu_put_byte(f, n->allmulti);
800 qemu_put_be32(f, n->mac_table.in_use);
801 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
802 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
803 qemu_put_be32(f, n->has_vnet_hdr);
804 qemu_put_byte(f, n->mac_table.multi_overflow);
805 qemu_put_byte(f, n->mac_table.uni_overflow);
806 qemu_put_byte(f, n->alluni);
807 qemu_put_byte(f, n->nomulti);
808 qemu_put_byte(f, n->nouni);
809 qemu_put_byte(f, n->nobcast);
810 qemu_put_byte(f, n->has_ufo);
813 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
815 VirtIONet *n = opaque;
816 int i;
818 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
819 return -EINVAL;
821 virtio_load(&n->vdev, f);
823 qemu_get_buffer(f, n->mac, ETH_ALEN);
824 n->tx_waiting = qemu_get_be32(f);
825 n->mergeable_rx_bufs = qemu_get_be32(f);
827 if (version_id >= 3)
828 n->status = qemu_get_be16(f);
830 if (version_id >= 4) {
831 if (version_id < 8) {
832 n->promisc = qemu_get_be32(f);
833 n->allmulti = qemu_get_be32(f);
834 } else {
835 n->promisc = qemu_get_byte(f);
836 n->allmulti = qemu_get_byte(f);
840 if (version_id >= 5) {
841 n->mac_table.in_use = qemu_get_be32(f);
842 /* MAC_TABLE_ENTRIES may be different from the saved image */
843 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
844 qemu_get_buffer(f, n->mac_table.macs,
845 n->mac_table.in_use * ETH_ALEN);
846 } else if (n->mac_table.in_use) {
847 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
848 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
849 n->mac_table.in_use = 0;
853 if (version_id >= 6)
854 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
856 if (version_id >= 7) {
857 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
858 error_report("virtio-net: saved image requires vnet_hdr=on");
859 return -1;
862 if (n->has_vnet_hdr) {
863 tap_using_vnet_hdr(n->nic->nc.peer, 1);
864 tap_set_offload(n->nic->nc.peer,
865 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
866 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
867 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
868 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
869 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
873 if (version_id >= 9) {
874 n->mac_table.multi_overflow = qemu_get_byte(f);
875 n->mac_table.uni_overflow = qemu_get_byte(f);
878 if (version_id >= 10) {
879 n->alluni = qemu_get_byte(f);
880 n->nomulti = qemu_get_byte(f);
881 n->nouni = qemu_get_byte(f);
882 n->nobcast = qemu_get_byte(f);
885 if (version_id >= 11) {
886 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
887 error_report("virtio-net: saved image requires TUN_F_UFO support");
888 return -1;
892 /* Find the first multicast entry in the saved MAC filter */
893 for (i = 0; i < n->mac_table.in_use; i++) {
894 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
895 break;
898 n->mac_table.first_multi = i;
900 if (n->tx_waiting) {
901 if (n->tx_timer) {
902 qemu_mod_timer(n->tx_timer,
903 qemu_get_clock(vm_clock) + n->tx_timeout);
904 } else {
905 qemu_bh_schedule(n->tx_bh);
908 return 0;
911 static void virtio_net_cleanup(VLANClientState *nc)
913 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
915 n->nic = NULL;
918 static NetClientInfo net_virtio_info = {
919 .type = NET_CLIENT_TYPE_NIC,
920 .size = sizeof(NICState),
921 .can_receive = virtio_net_can_receive,
922 .receive = virtio_net_receive,
923 .cleanup = virtio_net_cleanup,
924 .link_status_changed = virtio_net_set_link_status,
927 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
929 VirtIONet *n = to_virtio_net(vdev);
930 if (!n->nic->nc.peer) {
931 return;
933 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
934 return;
937 if (!tap_get_vhost_net(n->nic->nc.peer)) {
938 return;
940 if (!!n->vhost_started == !!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
941 return;
943 if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
944 int r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), vdev);
945 if (r < 0) {
946 fprintf(stderr, "unable to start vhost net: %d: "
947 "falling back on userspace virtio\n", -r);
948 } else {
949 n->vhost_started = 1;
951 } else {
952 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
953 n->vhost_started = 0;
957 static void virtio_net_vmstate_change(void *opaque, int running, int reason)
959 VirtIONet *n = opaque;
960 uint8_t status = running ? VIRTIO_CONFIG_S_DRIVER_OK : 0;
961 /* This is called when vm is started/stopped,
962 * it will start/stop vhost backend if * appropriate
963 * e.g. after migration. */
964 virtio_net_set_status(&n->vdev, n->vdev.status & status);
967 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
968 virtio_net_conf *net)
970 VirtIONet *n;
972 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
973 sizeof(struct virtio_net_config),
974 sizeof(VirtIONet));
976 n->vdev.get_config = virtio_net_get_config;
977 n->vdev.set_config = virtio_net_set_config;
978 n->vdev.get_features = virtio_net_get_features;
979 n->vdev.set_features = virtio_net_set_features;
980 n->vdev.bad_features = virtio_net_bad_features;
981 n->vdev.reset = virtio_net_reset;
982 n->vdev.set_status = virtio_net_set_status;
983 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
985 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
986 fprintf(stderr, "virtio-net: "
987 "Unknown option tx=%s, valid options: \"timer\" \"bh\"\n",
988 net->tx);
989 fprintf(stderr, "Defaulting to \"bh\"\n");
992 if (net->tx && !strcmp(net->tx, "timer")) {
993 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
994 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
995 n->tx_timeout = net->txtimer;
996 } else {
997 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
998 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1000 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
1001 qemu_macaddr_default_if_unset(&conf->macaddr);
1002 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
1003 n->status = VIRTIO_NET_S_LINK_UP;
1005 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
1007 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
1009 n->tx_waiting = 0;
1010 n->tx_burst = net->txburst;
1011 n->mergeable_rx_bufs = 0;
1012 n->promisc = 1; /* for compatibility */
1014 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
1016 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
1018 n->qdev = dev;
1019 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
1020 virtio_net_save, virtio_net_load, n);
1021 n->vmstate = qemu_add_vm_change_state_handler(virtio_net_vmstate_change, n);
1023 return &n->vdev;
1026 void virtio_net_exit(VirtIODevice *vdev)
1028 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
1029 qemu_del_vm_change_state_handler(n->vmstate);
1031 if (n->vhost_started) {
1032 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
1035 qemu_purge_queued_packets(&n->nic->nc);
1037 unregister_savevm(n->qdev, "virtio-net", n);
1039 qemu_free(n->mac_table.macs);
1040 qemu_free(n->vlans);
1042 if (n->tx_timer) {
1043 qemu_del_timer(n->tx_timer);
1044 qemu_free_timer(n->tx_timer);
1045 } else {
1046 qemu_bh_delete(n->tx_bh);
1049 virtio_cleanup(&n->vdev);
1050 qemu_del_vlan_client(&n->nic->nc);