Add checkpatch.pl from Linux kernel
[qemu/lumag.git] / hw / virtio-net.c
blobccb3e632a4280a3c5d85f556557127099b58d1c9
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 struct {
58 int in_use;
59 int first_multi;
60 uint8_t multi_overflow;
61 uint8_t uni_overflow;
62 uint8_t *macs;
63 } mac_table;
64 uint32_t *vlans;
65 DeviceState *qdev;
66 } VirtIONet;
68 /* TODO
69 * - we could suppress RX interrupt if we were so inclined.
72 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
74 return (VirtIONet *)vdev;
77 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
79 VirtIONet *n = to_virtio_net(vdev);
80 struct virtio_net_config netcfg;
82 netcfg.status = n->status;
83 memcpy(netcfg.mac, n->mac, ETH_ALEN);
84 memcpy(config, &netcfg, sizeof(netcfg));
87 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
89 VirtIONet *n = to_virtio_net(vdev);
90 struct virtio_net_config netcfg;
92 memcpy(&netcfg, config, sizeof(netcfg));
94 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
95 memcpy(n->mac, netcfg.mac, ETH_ALEN);
96 qemu_format_nic_info_str(&n->nic->nc, n->mac);
100 static bool virtio_net_started(VirtIONet *n, uint8_t status)
102 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
103 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
106 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
108 if (!n->nic->nc.peer) {
109 return;
111 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
112 return;
115 if (!tap_get_vhost_net(n->nic->nc.peer)) {
116 return;
118 if (!!n->vhost_started == virtio_net_started(n, status)) {
119 return;
121 if (!n->vhost_started) {
122 int r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
123 if (r < 0) {
124 error_report("unable to start vhost net: %d: "
125 "falling back on userspace virtio", -r);
126 } else {
127 n->vhost_started = 1;
129 } else {
130 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
131 n->vhost_started = 0;
135 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
137 VirtIONet *n = to_virtio_net(vdev);
139 virtio_net_vhost_status(n, status);
141 if (!n->tx_waiting) {
142 return;
145 if (virtio_net_started(n, status) && !n->vhost_started) {
146 if (n->tx_timer) {
147 qemu_mod_timer(n->tx_timer,
148 qemu_get_clock(vm_clock) + n->tx_timeout);
149 } else {
150 qemu_bh_schedule(n->tx_bh);
152 } else {
153 if (n->tx_timer) {
154 qemu_del_timer(n->tx_timer);
155 } else {
156 qemu_bh_cancel(n->tx_bh);
161 static void virtio_net_set_link_status(VLANClientState *nc)
163 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
164 uint16_t old_status = n->status;
166 if (nc->link_down)
167 n->status &= ~VIRTIO_NET_S_LINK_UP;
168 else
169 n->status |= VIRTIO_NET_S_LINK_UP;
171 if (n->status != old_status)
172 virtio_notify_config(&n->vdev);
174 virtio_net_set_status(&n->vdev, n->vdev.status);
177 static void virtio_net_reset(VirtIODevice *vdev)
179 VirtIONet *n = to_virtio_net(vdev);
181 /* Reset back to compatibility mode */
182 n->promisc = 1;
183 n->allmulti = 0;
184 n->alluni = 0;
185 n->nomulti = 0;
186 n->nouni = 0;
187 n->nobcast = 0;
189 /* Flush any MAC and VLAN filter table state */
190 n->mac_table.in_use = 0;
191 n->mac_table.first_multi = 0;
192 n->mac_table.multi_overflow = 0;
193 n->mac_table.uni_overflow = 0;
194 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
195 memset(n->vlans, 0, MAX_VLAN >> 3);
198 static int peer_has_vnet_hdr(VirtIONet *n)
200 if (!n->nic->nc.peer)
201 return 0;
203 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
204 return 0;
206 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
208 return n->has_vnet_hdr;
211 static int peer_has_ufo(VirtIONet *n)
213 if (!peer_has_vnet_hdr(n))
214 return 0;
216 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
218 return n->has_ufo;
221 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
223 VirtIONet *n = to_virtio_net(vdev);
225 features |= (1 << VIRTIO_NET_F_MAC);
227 if (peer_has_vnet_hdr(n)) {
228 tap_using_vnet_hdr(n->nic->nc.peer, 1);
229 } else {
230 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
231 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
232 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
233 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
235 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
236 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
237 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
238 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
241 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
242 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
243 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
246 if (!n->nic->nc.peer ||
247 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
248 return features;
250 if (!tap_get_vhost_net(n->nic->nc.peer)) {
251 return features;
253 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
256 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
258 uint32_t features = 0;
260 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
261 * but also these: */
262 features |= (1 << VIRTIO_NET_F_MAC);
263 features |= (1 << VIRTIO_NET_F_CSUM);
264 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
265 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
266 features |= (1 << VIRTIO_NET_F_HOST_ECN);
268 return features;
271 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
273 VirtIONet *n = to_virtio_net(vdev);
275 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
277 if (n->has_vnet_hdr) {
278 tap_set_offload(n->nic->nc.peer,
279 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
280 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
281 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
282 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
283 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
285 if (!n->nic->nc.peer ||
286 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
287 return;
289 if (!tap_get_vhost_net(n->nic->nc.peer)) {
290 return;
292 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
295 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
296 VirtQueueElement *elem)
298 uint8_t on;
300 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
301 error_report("virtio-net ctrl invalid rx mode command");
302 exit(1);
305 on = ldub_p(elem->out_sg[1].iov_base);
307 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
308 n->promisc = on;
309 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
310 n->allmulti = on;
311 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
312 n->alluni = on;
313 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
314 n->nomulti = on;
315 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
316 n->nouni = on;
317 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
318 n->nobcast = on;
319 else
320 return VIRTIO_NET_ERR;
322 return VIRTIO_NET_OK;
325 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
326 VirtQueueElement *elem)
328 struct virtio_net_ctrl_mac mac_data;
330 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
331 elem->out_sg[1].iov_len < sizeof(mac_data) ||
332 elem->out_sg[2].iov_len < sizeof(mac_data))
333 return VIRTIO_NET_ERR;
335 n->mac_table.in_use = 0;
336 n->mac_table.first_multi = 0;
337 n->mac_table.uni_overflow = 0;
338 n->mac_table.multi_overflow = 0;
339 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
341 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
343 if (sizeof(mac_data.entries) +
344 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
345 return VIRTIO_NET_ERR;
347 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
348 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
349 mac_data.entries * ETH_ALEN);
350 n->mac_table.in_use += mac_data.entries;
351 } else {
352 n->mac_table.uni_overflow = 1;
355 n->mac_table.first_multi = n->mac_table.in_use;
357 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
359 if (sizeof(mac_data.entries) +
360 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
361 return VIRTIO_NET_ERR;
363 if (mac_data.entries) {
364 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
365 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
366 elem->out_sg[2].iov_base + sizeof(mac_data),
367 mac_data.entries * ETH_ALEN);
368 n->mac_table.in_use += mac_data.entries;
369 } else {
370 n->mac_table.multi_overflow = 1;
374 return VIRTIO_NET_OK;
377 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
378 VirtQueueElement *elem)
380 uint16_t vid;
382 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
383 error_report("virtio-net ctrl invalid vlan command");
384 return VIRTIO_NET_ERR;
387 vid = lduw_le_p(elem->out_sg[1].iov_base);
389 if (vid >= MAX_VLAN)
390 return VIRTIO_NET_ERR;
392 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
393 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
394 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
395 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
396 else
397 return VIRTIO_NET_ERR;
399 return VIRTIO_NET_OK;
402 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
404 VirtIONet *n = to_virtio_net(vdev);
405 struct virtio_net_ctrl_hdr ctrl;
406 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
407 VirtQueueElement elem;
409 while (virtqueue_pop(vq, &elem)) {
410 if ((elem.in_num < 1) || (elem.out_num < 1)) {
411 error_report("virtio-net ctrl missing headers");
412 exit(1);
415 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
416 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
417 error_report("virtio-net ctrl header not in correct element");
418 exit(1);
421 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
422 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
424 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
425 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
426 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
427 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
428 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
429 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
431 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
433 virtqueue_push(vq, &elem, sizeof(status));
434 virtio_notify(vdev, vq);
438 /* RX */
440 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
442 VirtIONet *n = to_virtio_net(vdev);
444 qemu_flush_queued_packets(&n->nic->nc);
446 /* We now have RX buffers, signal to the IO thread to break out of the
447 * select to re-poll the tap file descriptor */
448 qemu_notify_event();
451 static int virtio_net_can_receive(VLANClientState *nc)
453 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
454 if (!n->vdev.vm_running) {
455 return 0;
458 if (!virtio_queue_ready(n->rx_vq) ||
459 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
460 return 0;
462 return 1;
465 static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
467 if (virtio_queue_empty(n->rx_vq) ||
468 (n->mergeable_rx_bufs &&
469 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
470 virtio_queue_set_notification(n->rx_vq, 1);
472 /* To avoid a race condition where the guest has made some buffers
473 * available after the above check but before notification was
474 * enabled, check for available buffers again.
476 if (virtio_queue_empty(n->rx_vq) ||
477 (n->mergeable_rx_bufs &&
478 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
479 return 0;
482 virtio_queue_set_notification(n->rx_vq, 0);
483 return 1;
486 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
487 * it never finds out that the packets don't have valid checksums. This
488 * causes dhclient to get upset. Fedora's carried a patch for ages to
489 * fix this with Xen but it hasn't appeared in an upstream release of
490 * dhclient yet.
492 * To avoid breaking existing guests, we catch udp packets and add
493 * checksums. This is terrible but it's better than hacking the guest
494 * kernels.
496 * N.B. if we introduce a zero-copy API, this operation is no longer free so
497 * we should provide a mechanism to disable it to avoid polluting the host
498 * cache.
500 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
501 const uint8_t *buf, size_t size)
503 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
504 (size > 27 && size < 1500) && /* normal sized MTU */
505 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
506 (buf[23] == 17) && /* ip.protocol == UDP */
507 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
508 /* FIXME this cast is evil */
509 net_checksum_calculate((uint8_t *)buf, size);
510 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
514 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
515 const void *buf, size_t size, size_t hdr_len)
517 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
518 int offset = 0;
520 hdr->flags = 0;
521 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
523 if (n->has_vnet_hdr) {
524 memcpy(hdr, buf, sizeof(*hdr));
525 offset = sizeof(*hdr);
526 work_around_broken_dhclient(hdr, buf + offset, size - offset);
529 /* We only ever receive a struct virtio_net_hdr from the tapfd,
530 * but we may be passing along a larger header to the guest.
532 iov[0].iov_base += hdr_len;
533 iov[0].iov_len -= hdr_len;
535 return offset;
538 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
540 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
541 static const uint8_t vlan[] = {0x81, 0x00};
542 uint8_t *ptr = (uint8_t *)buf;
543 int i;
545 if (n->promisc)
546 return 1;
548 if (n->has_vnet_hdr) {
549 ptr += sizeof(struct virtio_net_hdr);
552 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
553 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
554 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
555 return 0;
558 if (ptr[0] & 1) { // multicast
559 if (!memcmp(ptr, bcast, sizeof(bcast))) {
560 return !n->nobcast;
561 } else if (n->nomulti) {
562 return 0;
563 } else if (n->allmulti || n->mac_table.multi_overflow) {
564 return 1;
567 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
568 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
569 return 1;
572 } else { // unicast
573 if (n->nouni) {
574 return 0;
575 } else if (n->alluni || n->mac_table.uni_overflow) {
576 return 1;
577 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
578 return 1;
581 for (i = 0; i < n->mac_table.first_multi; i++) {
582 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
583 return 1;
588 return 0;
591 static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
593 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
594 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
595 size_t guest_hdr_len, offset, i, host_hdr_len;
597 if (!virtio_net_can_receive(&n->nic->nc))
598 return -1;
600 /* hdr_len refers to the header we supply to the guest */
601 guest_hdr_len = n->mergeable_rx_bufs ?
602 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
605 host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
606 if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
607 return 0;
609 if (!receive_filter(n, buf, size))
610 return size;
612 offset = i = 0;
614 while (offset < size) {
615 VirtQueueElement elem;
616 int len, total;
617 struct iovec sg[VIRTQUEUE_MAX_SIZE];
619 total = 0;
621 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
622 if (i == 0)
623 return -1;
624 error_report("virtio-net unexpected empty queue: "
625 "i %zd mergeable %d offset %zd, size %zd, "
626 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
627 i, n->mergeable_rx_bufs, offset, size,
628 guest_hdr_len, host_hdr_len, n->vdev.guest_features);
629 exit(1);
632 if (elem.in_num < 1) {
633 error_report("virtio-net receive queue contains no in buffers");
634 exit(1);
637 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
638 error_report("virtio-net header not in first element");
639 exit(1);
642 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
644 if (i == 0) {
645 if (n->mergeable_rx_bufs)
646 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
648 offset += receive_header(n, sg, elem.in_num,
649 buf + offset, size - offset, guest_hdr_len);
650 total += guest_hdr_len;
653 /* copy in packet. ugh */
654 len = iov_from_buf(sg, elem.in_num,
655 buf + offset, size - offset);
656 total += len;
657 offset += len;
658 /* If buffers can't be merged, at this point we
659 * must have consumed the complete packet.
660 * Otherwise, drop it. */
661 if (!n->mergeable_rx_bufs && offset < size) {
662 #if 0
663 error_report("virtio-net truncated non-mergeable packet: "
664 "i %zd mergeable %d offset %zd, size %zd, "
665 "guest hdr len %zd, host hdr len %zd",
666 i, n->mergeable_rx_bufs,
667 offset, size, guest_hdr_len, host_hdr_len);
668 #endif
669 return size;
672 /* signal other side */
673 virtqueue_fill(n->rx_vq, &elem, total, i++);
676 if (mhdr)
677 mhdr->num_buffers = i;
679 virtqueue_flush(n->rx_vq, i);
680 virtio_notify(&n->vdev, n->rx_vq);
682 return size;
685 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
687 static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
689 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
691 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
692 virtio_notify(&n->vdev, n->tx_vq);
694 n->async_tx.elem.out_num = n->async_tx.len = 0;
696 virtio_queue_set_notification(n->tx_vq, 1);
697 virtio_net_flush_tx(n, n->tx_vq);
700 /* TX */
701 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
703 VirtQueueElement elem;
704 int32_t num_packets = 0;
705 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
706 return num_packets;
709 assert(n->vdev.vm_running);
711 if (n->async_tx.elem.out_num) {
712 virtio_queue_set_notification(n->tx_vq, 0);
713 return num_packets;
716 while (virtqueue_pop(vq, &elem)) {
717 ssize_t ret, len = 0;
718 unsigned int out_num = elem.out_num;
719 struct iovec *out_sg = &elem.out_sg[0];
720 unsigned hdr_len;
722 /* hdr_len refers to the header received from the guest */
723 hdr_len = n->mergeable_rx_bufs ?
724 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
725 sizeof(struct virtio_net_hdr);
727 if (out_num < 1 || out_sg->iov_len != hdr_len) {
728 error_report("virtio-net header not in first element");
729 exit(1);
732 /* ignore the header if GSO is not supported */
733 if (!n->has_vnet_hdr) {
734 out_num--;
735 out_sg++;
736 len += hdr_len;
737 } else if (n->mergeable_rx_bufs) {
738 /* tapfd expects a struct virtio_net_hdr */
739 hdr_len -= sizeof(struct virtio_net_hdr);
740 out_sg->iov_len -= hdr_len;
741 len += hdr_len;
744 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
745 virtio_net_tx_complete);
746 if (ret == 0) {
747 virtio_queue_set_notification(n->tx_vq, 0);
748 n->async_tx.elem = elem;
749 n->async_tx.len = len;
750 return -EBUSY;
753 len += ret;
755 virtqueue_push(vq, &elem, len);
756 virtio_notify(&n->vdev, vq);
758 if (++num_packets >= n->tx_burst) {
759 break;
762 return num_packets;
765 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
767 VirtIONet *n = to_virtio_net(vdev);
769 /* This happens when device was stopped but VCPU wasn't. */
770 if (!n->vdev.vm_running) {
771 n->tx_waiting = 1;
772 return;
775 if (n->tx_waiting) {
776 virtio_queue_set_notification(vq, 1);
777 qemu_del_timer(n->tx_timer);
778 n->tx_waiting = 0;
779 virtio_net_flush_tx(n, vq);
780 } else {
781 qemu_mod_timer(n->tx_timer,
782 qemu_get_clock(vm_clock) + n->tx_timeout);
783 n->tx_waiting = 1;
784 virtio_queue_set_notification(vq, 0);
788 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
790 VirtIONet *n = to_virtio_net(vdev);
792 if (unlikely(n->tx_waiting)) {
793 return;
795 n->tx_waiting = 1;
796 /* This happens when device was stopped but VCPU wasn't. */
797 if (!n->vdev.vm_running) {
798 return;
800 virtio_queue_set_notification(vq, 0);
801 qemu_bh_schedule(n->tx_bh);
804 static void virtio_net_tx_timer(void *opaque)
806 VirtIONet *n = opaque;
807 assert(n->vdev.vm_running);
809 n->tx_waiting = 0;
811 /* Just in case the driver is not ready on more */
812 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
813 return;
815 virtio_queue_set_notification(n->tx_vq, 1);
816 virtio_net_flush_tx(n, n->tx_vq);
819 static void virtio_net_tx_bh(void *opaque)
821 VirtIONet *n = opaque;
822 int32_t ret;
824 assert(n->vdev.vm_running);
826 n->tx_waiting = 0;
828 /* Just in case the driver is not ready on more */
829 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
830 return;
832 ret = virtio_net_flush_tx(n, n->tx_vq);
833 if (ret == -EBUSY) {
834 return; /* Notification re-enable handled by tx_complete */
837 /* If we flush a full burst of packets, assume there are
838 * more coming and immediately reschedule */
839 if (ret >= n->tx_burst) {
840 qemu_bh_schedule(n->tx_bh);
841 n->tx_waiting = 1;
842 return;
845 /* If less than a full burst, re-enable notification and flush
846 * anything that may have come in while we weren't looking. If
847 * we find something, assume the guest is still active and reschedule */
848 virtio_queue_set_notification(n->tx_vq, 1);
849 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
850 virtio_queue_set_notification(n->tx_vq, 0);
851 qemu_bh_schedule(n->tx_bh);
852 n->tx_waiting = 1;
856 static void virtio_net_save(QEMUFile *f, void *opaque)
858 VirtIONet *n = opaque;
860 /* At this point, backend must be stopped, otherwise
861 * it might keep writing to memory. */
862 assert(!n->vhost_started);
863 virtio_save(&n->vdev, f);
865 qemu_put_buffer(f, n->mac, ETH_ALEN);
866 qemu_put_be32(f, n->tx_waiting);
867 qemu_put_be32(f, n->mergeable_rx_bufs);
868 qemu_put_be16(f, n->status);
869 qemu_put_byte(f, n->promisc);
870 qemu_put_byte(f, n->allmulti);
871 qemu_put_be32(f, n->mac_table.in_use);
872 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
873 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
874 qemu_put_be32(f, n->has_vnet_hdr);
875 qemu_put_byte(f, n->mac_table.multi_overflow);
876 qemu_put_byte(f, n->mac_table.uni_overflow);
877 qemu_put_byte(f, n->alluni);
878 qemu_put_byte(f, n->nomulti);
879 qemu_put_byte(f, n->nouni);
880 qemu_put_byte(f, n->nobcast);
881 qemu_put_byte(f, n->has_ufo);
884 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
886 VirtIONet *n = opaque;
887 int i;
889 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
890 return -EINVAL;
892 virtio_load(&n->vdev, f);
894 qemu_get_buffer(f, n->mac, ETH_ALEN);
895 n->tx_waiting = qemu_get_be32(f);
896 n->mergeable_rx_bufs = qemu_get_be32(f);
898 if (version_id >= 3)
899 n->status = qemu_get_be16(f);
901 if (version_id >= 4) {
902 if (version_id < 8) {
903 n->promisc = qemu_get_be32(f);
904 n->allmulti = qemu_get_be32(f);
905 } else {
906 n->promisc = qemu_get_byte(f);
907 n->allmulti = qemu_get_byte(f);
911 if (version_id >= 5) {
912 n->mac_table.in_use = qemu_get_be32(f);
913 /* MAC_TABLE_ENTRIES may be different from the saved image */
914 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
915 qemu_get_buffer(f, n->mac_table.macs,
916 n->mac_table.in_use * ETH_ALEN);
917 } else if (n->mac_table.in_use) {
918 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
919 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
920 n->mac_table.in_use = 0;
924 if (version_id >= 6)
925 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
927 if (version_id >= 7) {
928 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
929 error_report("virtio-net: saved image requires vnet_hdr=on");
930 return -1;
933 if (n->has_vnet_hdr) {
934 tap_using_vnet_hdr(n->nic->nc.peer, 1);
935 tap_set_offload(n->nic->nc.peer,
936 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
937 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
938 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
939 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
940 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
944 if (version_id >= 9) {
945 n->mac_table.multi_overflow = qemu_get_byte(f);
946 n->mac_table.uni_overflow = qemu_get_byte(f);
949 if (version_id >= 10) {
950 n->alluni = qemu_get_byte(f);
951 n->nomulti = qemu_get_byte(f);
952 n->nouni = qemu_get_byte(f);
953 n->nobcast = qemu_get_byte(f);
956 if (version_id >= 11) {
957 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
958 error_report("virtio-net: saved image requires TUN_F_UFO support");
959 return -1;
963 /* Find the first multicast entry in the saved MAC filter */
964 for (i = 0; i < n->mac_table.in_use; i++) {
965 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
966 break;
969 n->mac_table.first_multi = i;
970 return 0;
973 static void virtio_net_cleanup(VLANClientState *nc)
975 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
977 n->nic = NULL;
980 static NetClientInfo net_virtio_info = {
981 .type = NET_CLIENT_TYPE_NIC,
982 .size = sizeof(NICState),
983 .can_receive = virtio_net_can_receive,
984 .receive = virtio_net_receive,
985 .cleanup = virtio_net_cleanup,
986 .link_status_changed = virtio_net_set_link_status,
989 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
990 virtio_net_conf *net)
992 VirtIONet *n;
994 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
995 sizeof(struct virtio_net_config),
996 sizeof(VirtIONet));
998 n->vdev.get_config = virtio_net_get_config;
999 n->vdev.set_config = virtio_net_set_config;
1000 n->vdev.get_features = virtio_net_get_features;
1001 n->vdev.set_features = virtio_net_set_features;
1002 n->vdev.bad_features = virtio_net_bad_features;
1003 n->vdev.reset = virtio_net_reset;
1004 n->vdev.set_status = virtio_net_set_status;
1005 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
1007 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
1008 error_report("virtio-net: "
1009 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1010 net->tx);
1011 error_report("Defaulting to \"bh\"");
1014 if (net->tx && !strcmp(net->tx, "timer")) {
1015 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
1016 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
1017 n->tx_timeout = net->txtimer;
1018 } else {
1019 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1020 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1022 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
1023 qemu_macaddr_default_if_unset(&conf->macaddr);
1024 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
1025 n->status = VIRTIO_NET_S_LINK_UP;
1027 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
1029 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
1031 n->tx_waiting = 0;
1032 n->tx_burst = net->txburst;
1033 n->mergeable_rx_bufs = 0;
1034 n->promisc = 1; /* for compatibility */
1036 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
1038 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
1040 n->qdev = dev;
1041 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
1042 virtio_net_save, virtio_net_load, n);
1044 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1046 return &n->vdev;
1049 void virtio_net_exit(VirtIODevice *vdev)
1051 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
1053 /* This will stop vhost backend if appropriate. */
1054 virtio_net_set_status(vdev, 0);
1056 qemu_purge_queued_packets(&n->nic->nc);
1058 unregister_savevm(n->qdev, "virtio-net", n);
1060 qemu_free(n->mac_table.macs);
1061 qemu_free(n->vlans);
1063 if (n->tx_timer) {
1064 qemu_del_timer(n->tx_timer);
1065 qemu_free_timer(n->tx_timer);
1066 } else {
1067 qemu_bh_delete(n->tx_bh);
1070 virtio_cleanup(&n->vdev);
1071 qemu_del_vlan_client(&n->nic->nc);