dbdma: reset io->processing flag for unassigned DBDMA channel rw accesses
[qemu/ar7.git] / hw / net / virtio-net.c
blob999989934e18879c524a906179f4f61337b28322
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 "qemu/osdep.h"
15 #include "qemu/iov.h"
16 #include "hw/virtio/virtio.h"
17 #include "net/net.h"
18 #include "net/checksum.h"
19 #include "net/tap.h"
20 #include "qemu/error-report.h"
21 #include "qemu/timer.h"
22 #include "hw/virtio/virtio-net.h"
23 #include "net/vhost_net.h"
24 #include "hw/virtio/virtio-bus.h"
25 #include "qapi/qmp/qjson.h"
26 #include "qapi-event.h"
27 #include "hw/virtio/virtio-access.h"
29 #define VIRTIO_NET_VM_VERSION 11
31 #define MAC_TABLE_ENTRIES 64
32 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
35 * Calculate the number of bytes up to and including the given 'field' of
36 * 'container'.
38 #define endof(container, field) \
39 (offsetof(container, field) + sizeof(((container *)0)->field))
41 typedef struct VirtIOFeature {
42 uint32_t flags;
43 size_t end;
44 } VirtIOFeature;
46 static VirtIOFeature feature_sizes[] = {
47 {.flags = 1 << VIRTIO_NET_F_MAC,
48 .end = endof(struct virtio_net_config, mac)},
49 {.flags = 1 << VIRTIO_NET_F_STATUS,
50 .end = endof(struct virtio_net_config, status)},
51 {.flags = 1 << VIRTIO_NET_F_MQ,
52 .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
56 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
58 VirtIONet *n = qemu_get_nic_opaque(nc);
60 return &n->vqs[nc->queue_index];
63 static int vq2q(int queue_index)
65 return queue_index / 2;
68 /* TODO
69 * - we could suppress RX interrupt if we were so inclined.
72 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
74 VirtIONet *n = VIRTIO_NET(vdev);
75 struct virtio_net_config netcfg;
77 virtio_stw_p(vdev, &netcfg.status, n->status);
78 virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
79 memcpy(netcfg.mac, n->mac, ETH_ALEN);
80 memcpy(config, &netcfg, n->config_size);
83 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
85 VirtIONet *n = VIRTIO_NET(vdev);
86 struct virtio_net_config netcfg = {};
88 memcpy(&netcfg, config, n->config_size);
90 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
91 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
92 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
93 memcpy(n->mac, netcfg.mac, ETH_ALEN);
94 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
98 static bool virtio_net_started(VirtIONet *n, uint8_t status)
100 VirtIODevice *vdev = VIRTIO_DEVICE(n);
101 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
102 (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
105 static void virtio_net_announce_timer(void *opaque)
107 VirtIONet *n = opaque;
108 VirtIODevice *vdev = VIRTIO_DEVICE(n);
110 n->announce_counter--;
111 n->status |= VIRTIO_NET_S_ANNOUNCE;
112 virtio_notify_config(vdev);
115 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
117 VirtIODevice *vdev = VIRTIO_DEVICE(n);
118 NetClientState *nc = qemu_get_queue(n->nic);
119 int queues = n->multiqueue ? n->max_queues : 1;
121 if (!get_vhost_net(nc->peer)) {
122 return;
125 if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
126 !!n->vhost_started) {
127 return;
129 if (!n->vhost_started) {
130 int r, i;
132 if (n->needs_vnet_hdr_swap) {
133 error_report("backend does not support %s vnet headers; "
134 "falling back on userspace virtio",
135 virtio_is_big_endian(vdev) ? "BE" : "LE");
136 return;
139 /* Any packets outstanding? Purge them to avoid touching rings
140 * when vhost is running.
142 for (i = 0; i < queues; i++) {
143 NetClientState *qnc = qemu_get_subqueue(n->nic, i);
145 /* Purge both directions: TX and RX. */
146 qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
147 qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
150 n->vhost_started = 1;
151 r = vhost_net_start(vdev, n->nic->ncs, queues);
152 if (r < 0) {
153 error_report("unable to start vhost net: %d: "
154 "falling back on userspace virtio", -r);
155 n->vhost_started = 0;
157 } else {
158 vhost_net_stop(vdev, n->nic->ncs, queues);
159 n->vhost_started = 0;
163 static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
164 NetClientState *peer,
165 bool enable)
167 if (virtio_is_big_endian(vdev)) {
168 return qemu_set_vnet_be(peer, enable);
169 } else {
170 return qemu_set_vnet_le(peer, enable);
174 static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
175 int queues, bool enable)
177 int i;
179 for (i = 0; i < queues; i++) {
180 if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
181 enable) {
182 while (--i >= 0) {
183 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
186 return true;
190 return false;
193 static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
195 VirtIODevice *vdev = VIRTIO_DEVICE(n);
196 int queues = n->multiqueue ? n->max_queues : 1;
198 if (virtio_net_started(n, status)) {
199 /* Before using the device, we tell the network backend about the
200 * endianness to use when parsing vnet headers. If the backend
201 * can't do it, we fallback onto fixing the headers in the core
202 * virtio-net code.
204 n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
205 queues, true);
206 } else if (virtio_net_started(n, vdev->status)) {
207 /* After using the device, we need to reset the network backend to
208 * the default (guest native endianness), otherwise the guest may
209 * lose network connectivity if it is rebooted into a different
210 * endianness.
212 virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false);
216 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
218 VirtIONet *n = VIRTIO_NET(vdev);
219 VirtIONetQueue *q;
220 int i;
221 uint8_t queue_status;
223 virtio_net_vnet_endian_status(n, status);
224 virtio_net_vhost_status(n, status);
226 for (i = 0; i < n->max_queues; i++) {
227 NetClientState *ncs = qemu_get_subqueue(n->nic, i);
228 bool queue_started;
229 q = &n->vqs[i];
231 if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
232 queue_status = 0;
233 } else {
234 queue_status = status;
236 queue_started =
237 virtio_net_started(n, queue_status) && !n->vhost_started;
239 if (queue_started) {
240 qemu_flush_queued_packets(ncs);
243 if (!q->tx_waiting) {
244 continue;
247 if (queue_started) {
248 if (q->tx_timer) {
249 timer_mod(q->tx_timer,
250 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
251 } else {
252 qemu_bh_schedule(q->tx_bh);
254 } else {
255 if (q->tx_timer) {
256 timer_del(q->tx_timer);
257 } else {
258 qemu_bh_cancel(q->tx_bh);
264 static void virtio_net_set_link_status(NetClientState *nc)
266 VirtIONet *n = qemu_get_nic_opaque(nc);
267 VirtIODevice *vdev = VIRTIO_DEVICE(n);
268 uint16_t old_status = n->status;
270 if (nc->link_down)
271 n->status &= ~VIRTIO_NET_S_LINK_UP;
272 else
273 n->status |= VIRTIO_NET_S_LINK_UP;
275 if (n->status != old_status)
276 virtio_notify_config(vdev);
278 virtio_net_set_status(vdev, vdev->status);
281 static void rxfilter_notify(NetClientState *nc)
283 VirtIONet *n = qemu_get_nic_opaque(nc);
285 if (nc->rxfilter_notify_enabled) {
286 gchar *path = object_get_canonical_path(OBJECT(n->qdev));
287 qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
288 n->netclient_name, path, &error_abort);
289 g_free(path);
291 /* disable event notification to avoid events flooding */
292 nc->rxfilter_notify_enabled = 0;
296 static intList *get_vlan_table(VirtIONet *n)
298 intList *list, *entry;
299 int i, j;
301 list = NULL;
302 for (i = 0; i < MAX_VLAN >> 5; i++) {
303 for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
304 if (n->vlans[i] & (1U << j)) {
305 entry = g_malloc0(sizeof(*entry));
306 entry->value = (i << 5) + j;
307 entry->next = list;
308 list = entry;
313 return list;
316 static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
318 VirtIONet *n = qemu_get_nic_opaque(nc);
319 VirtIODevice *vdev = VIRTIO_DEVICE(n);
320 RxFilterInfo *info;
321 strList *str_list, *entry;
322 int i;
324 info = g_malloc0(sizeof(*info));
325 info->name = g_strdup(nc->name);
326 info->promiscuous = n->promisc;
328 if (n->nouni) {
329 info->unicast = RX_STATE_NONE;
330 } else if (n->alluni) {
331 info->unicast = RX_STATE_ALL;
332 } else {
333 info->unicast = RX_STATE_NORMAL;
336 if (n->nomulti) {
337 info->multicast = RX_STATE_NONE;
338 } else if (n->allmulti) {
339 info->multicast = RX_STATE_ALL;
340 } else {
341 info->multicast = RX_STATE_NORMAL;
344 info->broadcast_allowed = n->nobcast;
345 info->multicast_overflow = n->mac_table.multi_overflow;
346 info->unicast_overflow = n->mac_table.uni_overflow;
348 info->main_mac = qemu_mac_strdup_printf(n->mac);
350 str_list = NULL;
351 for (i = 0; i < n->mac_table.first_multi; i++) {
352 entry = g_malloc0(sizeof(*entry));
353 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
354 entry->next = str_list;
355 str_list = entry;
357 info->unicast_table = str_list;
359 str_list = NULL;
360 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
361 entry = g_malloc0(sizeof(*entry));
362 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
363 entry->next = str_list;
364 str_list = entry;
366 info->multicast_table = str_list;
367 info->vlan_table = get_vlan_table(n);
369 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
370 info->vlan = RX_STATE_ALL;
371 } else if (!info->vlan_table) {
372 info->vlan = RX_STATE_NONE;
373 } else {
374 info->vlan = RX_STATE_NORMAL;
377 /* enable event notification after query */
378 nc->rxfilter_notify_enabled = 1;
380 return info;
383 static void virtio_net_reset(VirtIODevice *vdev)
385 VirtIONet *n = VIRTIO_NET(vdev);
387 /* Reset back to compatibility mode */
388 n->promisc = 1;
389 n->allmulti = 0;
390 n->alluni = 0;
391 n->nomulti = 0;
392 n->nouni = 0;
393 n->nobcast = 0;
394 /* multiqueue is disabled by default */
395 n->curr_queues = 1;
396 timer_del(n->announce_timer);
397 n->announce_counter = 0;
398 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
400 /* Flush any MAC and VLAN filter table state */
401 n->mac_table.in_use = 0;
402 n->mac_table.first_multi = 0;
403 n->mac_table.multi_overflow = 0;
404 n->mac_table.uni_overflow = 0;
405 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
406 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
407 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
408 memset(n->vlans, 0, MAX_VLAN >> 3);
411 static void peer_test_vnet_hdr(VirtIONet *n)
413 NetClientState *nc = qemu_get_queue(n->nic);
414 if (!nc->peer) {
415 return;
418 n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
421 static int peer_has_vnet_hdr(VirtIONet *n)
423 return n->has_vnet_hdr;
426 static int peer_has_ufo(VirtIONet *n)
428 if (!peer_has_vnet_hdr(n))
429 return 0;
431 n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
433 return n->has_ufo;
436 static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
437 int version_1)
439 int i;
440 NetClientState *nc;
442 n->mergeable_rx_bufs = mergeable_rx_bufs;
444 if (version_1) {
445 n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
446 } else {
447 n->guest_hdr_len = n->mergeable_rx_bufs ?
448 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
449 sizeof(struct virtio_net_hdr);
452 for (i = 0; i < n->max_queues; i++) {
453 nc = qemu_get_subqueue(n->nic, i);
455 if (peer_has_vnet_hdr(n) &&
456 qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
457 qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
458 n->host_hdr_len = n->guest_hdr_len;
463 static int peer_attach(VirtIONet *n, int index)
465 NetClientState *nc = qemu_get_subqueue(n->nic, index);
467 if (!nc->peer) {
468 return 0;
471 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) {
472 vhost_set_vring_enable(nc->peer, 1);
475 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
476 return 0;
479 return tap_enable(nc->peer);
482 static int peer_detach(VirtIONet *n, int index)
484 NetClientState *nc = qemu_get_subqueue(n->nic, index);
486 if (!nc->peer) {
487 return 0;
490 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) {
491 vhost_set_vring_enable(nc->peer, 0);
494 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
495 return 0;
498 return tap_disable(nc->peer);
501 static void virtio_net_set_queues(VirtIONet *n)
503 int i;
504 int r;
506 for (i = 0; i < n->max_queues; i++) {
507 if (i < n->curr_queues) {
508 r = peer_attach(n, i);
509 assert(!r);
510 } else {
511 r = peer_detach(n, i);
512 assert(!r);
517 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
519 static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
520 Error **errp)
522 VirtIONet *n = VIRTIO_NET(vdev);
523 NetClientState *nc = qemu_get_queue(n->nic);
525 /* Firstly sync all virtio-net possible supported features */
526 features |= n->host_features;
528 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
530 if (!peer_has_vnet_hdr(n)) {
531 virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
532 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
533 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
534 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
536 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
537 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
538 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
539 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
542 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
543 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
544 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
547 if (!get_vhost_net(nc->peer)) {
548 return features;
550 return vhost_net_get_features(get_vhost_net(nc->peer), features);
553 static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
555 uint64_t features = 0;
557 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
558 * but also these: */
559 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
560 virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
561 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
562 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
563 virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
565 return features;
568 static void virtio_net_apply_guest_offloads(VirtIONet *n)
570 qemu_set_offload(qemu_get_queue(n->nic)->peer,
571 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
572 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
573 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
574 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
575 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
578 static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
580 static const uint64_t guest_offloads_mask =
581 (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
582 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
583 (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
584 (1ULL << VIRTIO_NET_F_GUEST_ECN) |
585 (1ULL << VIRTIO_NET_F_GUEST_UFO);
587 return guest_offloads_mask & features;
590 static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
592 VirtIODevice *vdev = VIRTIO_DEVICE(n);
593 return virtio_net_guest_offloads_by_features(vdev->guest_features);
596 static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
598 VirtIONet *n = VIRTIO_NET(vdev);
599 int i;
601 virtio_net_set_multiqueue(n,
602 virtio_has_feature(features, VIRTIO_NET_F_MQ));
604 virtio_net_set_mrg_rx_bufs(n,
605 virtio_has_feature(features,
606 VIRTIO_NET_F_MRG_RXBUF),
607 virtio_has_feature(features,
608 VIRTIO_F_VERSION_1));
610 if (n->has_vnet_hdr) {
611 n->curr_guest_offloads =
612 virtio_net_guest_offloads_by_features(features);
613 virtio_net_apply_guest_offloads(n);
616 for (i = 0; i < n->max_queues; i++) {
617 NetClientState *nc = qemu_get_subqueue(n->nic, i);
619 if (!get_vhost_net(nc->peer)) {
620 continue;
622 vhost_net_ack_features(get_vhost_net(nc->peer), features);
625 if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
626 memset(n->vlans, 0, MAX_VLAN >> 3);
627 } else {
628 memset(n->vlans, 0xff, MAX_VLAN >> 3);
632 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
633 struct iovec *iov, unsigned int iov_cnt)
635 uint8_t on;
636 size_t s;
637 NetClientState *nc = qemu_get_queue(n->nic);
639 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
640 if (s != sizeof(on)) {
641 return VIRTIO_NET_ERR;
644 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
645 n->promisc = on;
646 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
647 n->allmulti = on;
648 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
649 n->alluni = on;
650 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
651 n->nomulti = on;
652 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
653 n->nouni = on;
654 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
655 n->nobcast = on;
656 } else {
657 return VIRTIO_NET_ERR;
660 rxfilter_notify(nc);
662 return VIRTIO_NET_OK;
665 static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
666 struct iovec *iov, unsigned int iov_cnt)
668 VirtIODevice *vdev = VIRTIO_DEVICE(n);
669 uint64_t offloads;
670 size_t s;
672 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
673 return VIRTIO_NET_ERR;
676 s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
677 if (s != sizeof(offloads)) {
678 return VIRTIO_NET_ERR;
681 if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
682 uint64_t supported_offloads;
684 if (!n->has_vnet_hdr) {
685 return VIRTIO_NET_ERR;
688 supported_offloads = virtio_net_supported_guest_offloads(n);
689 if (offloads & ~supported_offloads) {
690 return VIRTIO_NET_ERR;
693 n->curr_guest_offloads = offloads;
694 virtio_net_apply_guest_offloads(n);
696 return VIRTIO_NET_OK;
697 } else {
698 return VIRTIO_NET_ERR;
702 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
703 struct iovec *iov, unsigned int iov_cnt)
705 VirtIODevice *vdev = VIRTIO_DEVICE(n);
706 struct virtio_net_ctrl_mac mac_data;
707 size_t s;
708 NetClientState *nc = qemu_get_queue(n->nic);
710 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
711 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
712 return VIRTIO_NET_ERR;
714 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
715 assert(s == sizeof(n->mac));
716 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
717 rxfilter_notify(nc);
719 return VIRTIO_NET_OK;
722 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
723 return VIRTIO_NET_ERR;
726 int in_use = 0;
727 int first_multi = 0;
728 uint8_t uni_overflow = 0;
729 uint8_t multi_overflow = 0;
730 uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
732 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
733 sizeof(mac_data.entries));
734 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
735 if (s != sizeof(mac_data.entries)) {
736 goto error;
738 iov_discard_front(&iov, &iov_cnt, s);
740 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
741 goto error;
744 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
745 s = iov_to_buf(iov, iov_cnt, 0, macs,
746 mac_data.entries * ETH_ALEN);
747 if (s != mac_data.entries * ETH_ALEN) {
748 goto error;
750 in_use += mac_data.entries;
751 } else {
752 uni_overflow = 1;
755 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
757 first_multi = in_use;
759 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
760 sizeof(mac_data.entries));
761 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
762 if (s != sizeof(mac_data.entries)) {
763 goto error;
766 iov_discard_front(&iov, &iov_cnt, s);
768 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
769 goto error;
772 if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
773 s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
774 mac_data.entries * ETH_ALEN);
775 if (s != mac_data.entries * ETH_ALEN) {
776 goto error;
778 in_use += mac_data.entries;
779 } else {
780 multi_overflow = 1;
783 n->mac_table.in_use = in_use;
784 n->mac_table.first_multi = first_multi;
785 n->mac_table.uni_overflow = uni_overflow;
786 n->mac_table.multi_overflow = multi_overflow;
787 memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
788 g_free(macs);
789 rxfilter_notify(nc);
791 return VIRTIO_NET_OK;
793 error:
794 g_free(macs);
795 return VIRTIO_NET_ERR;
798 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
799 struct iovec *iov, unsigned int iov_cnt)
801 VirtIODevice *vdev = VIRTIO_DEVICE(n);
802 uint16_t vid;
803 size_t s;
804 NetClientState *nc = qemu_get_queue(n->nic);
806 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
807 vid = virtio_lduw_p(vdev, &vid);
808 if (s != sizeof(vid)) {
809 return VIRTIO_NET_ERR;
812 if (vid >= MAX_VLAN)
813 return VIRTIO_NET_ERR;
815 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
816 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
817 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
818 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
819 else
820 return VIRTIO_NET_ERR;
822 rxfilter_notify(nc);
824 return VIRTIO_NET_OK;
827 static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
828 struct iovec *iov, unsigned int iov_cnt)
830 if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
831 n->status & VIRTIO_NET_S_ANNOUNCE) {
832 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
833 if (n->announce_counter) {
834 timer_mod(n->announce_timer,
835 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
836 self_announce_delay(n->announce_counter));
838 return VIRTIO_NET_OK;
839 } else {
840 return VIRTIO_NET_ERR;
844 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
845 struct iovec *iov, unsigned int iov_cnt)
847 VirtIODevice *vdev = VIRTIO_DEVICE(n);
848 struct virtio_net_ctrl_mq mq;
849 size_t s;
850 uint16_t queues;
852 s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
853 if (s != sizeof(mq)) {
854 return VIRTIO_NET_ERR;
857 if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
858 return VIRTIO_NET_ERR;
861 queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
863 if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
864 queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
865 queues > n->max_queues ||
866 !n->multiqueue) {
867 return VIRTIO_NET_ERR;
870 n->curr_queues = queues;
871 /* stop the backend before changing the number of queues to avoid handling a
872 * disabled queue */
873 virtio_net_set_status(vdev, vdev->status);
874 virtio_net_set_queues(n);
876 return VIRTIO_NET_OK;
878 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
880 VirtIONet *n = VIRTIO_NET(vdev);
881 struct virtio_net_ctrl_hdr ctrl;
882 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
883 VirtQueueElement *elem;
884 size_t s;
885 struct iovec *iov, *iov2;
886 unsigned int iov_cnt;
888 for (;;) {
889 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
890 if (!elem) {
891 break;
893 if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
894 iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
895 error_report("virtio-net ctrl missing headers");
896 exit(1);
899 iov_cnt = elem->out_num;
900 iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
901 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
902 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
903 if (s != sizeof(ctrl)) {
904 status = VIRTIO_NET_ERR;
905 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
906 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
907 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
908 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
909 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
910 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
911 } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
912 status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
913 } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
914 status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
915 } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
916 status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
919 s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
920 assert(s == sizeof(status));
922 virtqueue_push(vq, elem, sizeof(status));
923 virtio_notify(vdev, vq);
924 g_free(iov2);
925 g_free(elem);
929 /* RX */
931 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
933 VirtIONet *n = VIRTIO_NET(vdev);
934 int queue_index = vq2q(virtio_get_queue_index(vq));
936 qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
939 static int virtio_net_can_receive(NetClientState *nc)
941 VirtIONet *n = qemu_get_nic_opaque(nc);
942 VirtIODevice *vdev = VIRTIO_DEVICE(n);
943 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
945 if (!vdev->vm_running) {
946 return 0;
949 if (nc->queue_index >= n->curr_queues) {
950 return 0;
953 if (!virtio_queue_ready(q->rx_vq) ||
954 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
955 return 0;
958 return 1;
961 static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
963 VirtIONet *n = q->n;
964 if (virtio_queue_empty(q->rx_vq) ||
965 (n->mergeable_rx_bufs &&
966 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
967 virtio_queue_set_notification(q->rx_vq, 1);
969 /* To avoid a race condition where the guest has made some buffers
970 * available after the above check but before notification was
971 * enabled, check for available buffers again.
973 if (virtio_queue_empty(q->rx_vq) ||
974 (n->mergeable_rx_bufs &&
975 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
976 return 0;
980 virtio_queue_set_notification(q->rx_vq, 0);
981 return 1;
984 static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
986 virtio_tswap16s(vdev, &hdr->hdr_len);
987 virtio_tswap16s(vdev, &hdr->gso_size);
988 virtio_tswap16s(vdev, &hdr->csum_start);
989 virtio_tswap16s(vdev, &hdr->csum_offset);
992 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
993 * it never finds out that the packets don't have valid checksums. This
994 * causes dhclient to get upset. Fedora's carried a patch for ages to
995 * fix this with Xen but it hasn't appeared in an upstream release of
996 * dhclient yet.
998 * To avoid breaking existing guests, we catch udp packets and add
999 * checksums. This is terrible but it's better than hacking the guest
1000 * kernels.
1002 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1003 * we should provide a mechanism to disable it to avoid polluting the host
1004 * cache.
1006 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
1007 uint8_t *buf, size_t size)
1009 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
1010 (size > 27 && size < 1500) && /* normal sized MTU */
1011 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
1012 (buf[23] == 17) && /* ip.protocol == UDP */
1013 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1014 net_checksum_calculate(buf, size);
1015 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
1019 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
1020 const void *buf, size_t size)
1022 if (n->has_vnet_hdr) {
1023 /* FIXME this cast is evil */
1024 void *wbuf = (void *)buf;
1025 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
1026 size - n->host_hdr_len);
1028 if (n->needs_vnet_hdr_swap) {
1029 virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
1031 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
1032 } else {
1033 struct virtio_net_hdr hdr = {
1034 .flags = 0,
1035 .gso_type = VIRTIO_NET_HDR_GSO_NONE
1037 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
1041 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
1043 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1044 static const uint8_t vlan[] = {0x81, 0x00};
1045 uint8_t *ptr = (uint8_t *)buf;
1046 int i;
1048 if (n->promisc)
1049 return 1;
1051 ptr += n->host_hdr_len;
1053 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
1054 int vid = lduw_be_p(ptr + 14) & 0xfff;
1055 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
1056 return 0;
1059 if (ptr[0] & 1) { // multicast
1060 if (!memcmp(ptr, bcast, sizeof(bcast))) {
1061 return !n->nobcast;
1062 } else if (n->nomulti) {
1063 return 0;
1064 } else if (n->allmulti || n->mac_table.multi_overflow) {
1065 return 1;
1068 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
1069 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1070 return 1;
1073 } else { // unicast
1074 if (n->nouni) {
1075 return 0;
1076 } else if (n->alluni || n->mac_table.uni_overflow) {
1077 return 1;
1078 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
1079 return 1;
1082 for (i = 0; i < n->mac_table.first_multi; i++) {
1083 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1084 return 1;
1089 return 0;
1092 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
1094 VirtIONet *n = qemu_get_nic_opaque(nc);
1095 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1096 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1097 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
1098 struct virtio_net_hdr_mrg_rxbuf mhdr;
1099 unsigned mhdr_cnt = 0;
1100 size_t offset, i, guest_offset;
1102 if (!virtio_net_can_receive(nc)) {
1103 return -1;
1106 /* hdr_len refers to the header we supply to the guest */
1107 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
1108 return 0;
1111 if (!receive_filter(n, buf, size))
1112 return size;
1114 offset = i = 0;
1116 while (offset < size) {
1117 VirtQueueElement *elem;
1118 int len, total;
1119 const struct iovec *sg;
1121 total = 0;
1123 elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
1124 if (!elem) {
1125 if (i == 0)
1126 return -1;
1127 error_report("virtio-net unexpected empty queue: "
1128 "i %zd mergeable %d offset %zd, size %zd, "
1129 "guest hdr len %zd, host hdr len %zd "
1130 "guest features 0x%" PRIx64,
1131 i, n->mergeable_rx_bufs, offset, size,
1132 n->guest_hdr_len, n->host_hdr_len,
1133 vdev->guest_features);
1134 exit(1);
1137 if (elem->in_num < 1) {
1138 error_report("virtio-net receive queue contains no in buffers");
1139 exit(1);
1142 sg = elem->in_sg;
1143 if (i == 0) {
1144 assert(offset == 0);
1145 if (n->mergeable_rx_bufs) {
1146 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
1147 sg, elem->in_num,
1148 offsetof(typeof(mhdr), num_buffers),
1149 sizeof(mhdr.num_buffers));
1152 receive_header(n, sg, elem->in_num, buf, size);
1153 offset = n->host_hdr_len;
1154 total += n->guest_hdr_len;
1155 guest_offset = n->guest_hdr_len;
1156 } else {
1157 guest_offset = 0;
1160 /* copy in packet. ugh */
1161 len = iov_from_buf(sg, elem->in_num, guest_offset,
1162 buf + offset, size - offset);
1163 total += len;
1164 offset += len;
1165 /* If buffers can't be merged, at this point we
1166 * must have consumed the complete packet.
1167 * Otherwise, drop it. */
1168 if (!n->mergeable_rx_bufs && offset < size) {
1169 virtqueue_discard(q->rx_vq, elem, total);
1170 g_free(elem);
1171 return size;
1174 /* signal other side */
1175 virtqueue_fill(q->rx_vq, elem, total, i++);
1176 g_free(elem);
1179 if (mhdr_cnt) {
1180 virtio_stw_p(vdev, &mhdr.num_buffers, i);
1181 iov_from_buf(mhdr_sg, mhdr_cnt,
1183 &mhdr.num_buffers, sizeof mhdr.num_buffers);
1186 virtqueue_flush(q->rx_vq, i);
1187 virtio_notify(vdev, q->rx_vq);
1189 return size;
1192 static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
1194 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
1196 VirtIONet *n = qemu_get_nic_opaque(nc);
1197 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1198 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1200 virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
1201 virtio_notify(vdev, q->tx_vq);
1203 g_free(q->async_tx.elem);
1204 q->async_tx.elem = NULL;
1206 virtio_queue_set_notification(q->tx_vq, 1);
1207 virtio_net_flush_tx(q);
1210 /* TX */
1211 static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
1213 VirtIONet *n = q->n;
1214 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1215 VirtQueueElement *elem;
1216 int32_t num_packets = 0;
1217 int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
1218 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1219 return num_packets;
1222 if (q->async_tx.elem) {
1223 virtio_queue_set_notification(q->tx_vq, 0);
1224 return num_packets;
1227 for (;;) {
1228 ssize_t ret;
1229 unsigned int out_num;
1230 struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
1231 struct virtio_net_hdr_mrg_rxbuf mhdr;
1233 elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
1234 if (!elem) {
1235 break;
1238 out_num = elem->out_num;
1239 out_sg = elem->out_sg;
1240 if (out_num < 1) {
1241 error_report("virtio-net header not in first element");
1242 exit(1);
1245 if (n->has_vnet_hdr) {
1246 if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
1247 n->guest_hdr_len) {
1248 error_report("virtio-net header incorrect");
1249 exit(1);
1251 if (n->needs_vnet_hdr_swap) {
1252 virtio_net_hdr_swap(vdev, (void *) &mhdr);
1253 sg2[0].iov_base = &mhdr;
1254 sg2[0].iov_len = n->guest_hdr_len;
1255 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
1256 out_sg, out_num,
1257 n->guest_hdr_len, -1);
1258 if (out_num == VIRTQUEUE_MAX_SIZE) {
1259 goto drop;
1261 out_num += 1;
1262 out_sg = sg2;
1266 * If host wants to see the guest header as is, we can
1267 * pass it on unchanged. Otherwise, copy just the parts
1268 * that host is interested in.
1270 assert(n->host_hdr_len <= n->guest_hdr_len);
1271 if (n->host_hdr_len != n->guest_hdr_len) {
1272 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
1273 out_sg, out_num,
1274 0, n->host_hdr_len);
1275 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
1276 out_sg, out_num,
1277 n->guest_hdr_len, -1);
1278 out_num = sg_num;
1279 out_sg = sg;
1282 ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
1283 out_sg, out_num, virtio_net_tx_complete);
1284 if (ret == 0) {
1285 virtio_queue_set_notification(q->tx_vq, 0);
1286 q->async_tx.elem = elem;
1287 return -EBUSY;
1290 drop:
1291 virtqueue_push(q->tx_vq, elem, 0);
1292 virtio_notify(vdev, q->tx_vq);
1293 g_free(elem);
1295 if (++num_packets >= n->tx_burst) {
1296 break;
1299 return num_packets;
1302 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
1304 VirtIONet *n = VIRTIO_NET(vdev);
1305 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
1307 /* This happens when device was stopped but VCPU wasn't. */
1308 if (!vdev->vm_running) {
1309 q->tx_waiting = 1;
1310 return;
1313 if (q->tx_waiting) {
1314 virtio_queue_set_notification(vq, 1);
1315 timer_del(q->tx_timer);
1316 q->tx_waiting = 0;
1317 virtio_net_flush_tx(q);
1318 } else {
1319 timer_mod(q->tx_timer,
1320 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
1321 q->tx_waiting = 1;
1322 virtio_queue_set_notification(vq, 0);
1326 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
1328 VirtIONet *n = VIRTIO_NET(vdev);
1329 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
1331 if (unlikely(q->tx_waiting)) {
1332 return;
1334 q->tx_waiting = 1;
1335 /* This happens when device was stopped but VCPU wasn't. */
1336 if (!vdev->vm_running) {
1337 return;
1339 virtio_queue_set_notification(vq, 0);
1340 qemu_bh_schedule(q->tx_bh);
1343 static void virtio_net_tx_timer(void *opaque)
1345 VirtIONetQueue *q = opaque;
1346 VirtIONet *n = q->n;
1347 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1348 /* This happens when device was stopped but BH wasn't. */
1349 if (!vdev->vm_running) {
1350 /* Make sure tx waiting is set, so we'll run when restarted. */
1351 assert(q->tx_waiting);
1352 return;
1355 q->tx_waiting = 0;
1357 /* Just in case the driver is not ready on more */
1358 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1359 return;
1362 virtio_queue_set_notification(q->tx_vq, 1);
1363 virtio_net_flush_tx(q);
1366 static void virtio_net_tx_bh(void *opaque)
1368 VirtIONetQueue *q = opaque;
1369 VirtIONet *n = q->n;
1370 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1371 int32_t ret;
1373 /* This happens when device was stopped but BH wasn't. */
1374 if (!vdev->vm_running) {
1375 /* Make sure tx waiting is set, so we'll run when restarted. */
1376 assert(q->tx_waiting);
1377 return;
1380 q->tx_waiting = 0;
1382 /* Just in case the driver is not ready on more */
1383 if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
1384 return;
1387 ret = virtio_net_flush_tx(q);
1388 if (ret == -EBUSY) {
1389 return; /* Notification re-enable handled by tx_complete */
1392 /* If we flush a full burst of packets, assume there are
1393 * more coming and immediately reschedule */
1394 if (ret >= n->tx_burst) {
1395 qemu_bh_schedule(q->tx_bh);
1396 q->tx_waiting = 1;
1397 return;
1400 /* If less than a full burst, re-enable notification and flush
1401 * anything that may have come in while we weren't looking. If
1402 * we find something, assume the guest is still active and reschedule */
1403 virtio_queue_set_notification(q->tx_vq, 1);
1404 if (virtio_net_flush_tx(q) > 0) {
1405 virtio_queue_set_notification(q->tx_vq, 0);
1406 qemu_bh_schedule(q->tx_bh);
1407 q->tx_waiting = 1;
1411 static void virtio_net_add_queue(VirtIONet *n, int index)
1413 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1415 n->vqs[index].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx);
1416 if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
1417 n->vqs[index].tx_vq =
1418 virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
1419 n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1420 virtio_net_tx_timer,
1421 &n->vqs[index]);
1422 } else {
1423 n->vqs[index].tx_vq =
1424 virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
1425 n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
1428 n->vqs[index].tx_waiting = 0;
1429 n->vqs[index].n = n;
1432 static void virtio_net_del_queue(VirtIONet *n, int index)
1434 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1435 VirtIONetQueue *q = &n->vqs[index];
1436 NetClientState *nc = qemu_get_subqueue(n->nic, index);
1438 qemu_purge_queued_packets(nc);
1440 virtio_del_queue(vdev, index * 2);
1441 if (q->tx_timer) {
1442 timer_del(q->tx_timer);
1443 timer_free(q->tx_timer);
1444 } else {
1445 qemu_bh_delete(q->tx_bh);
1447 virtio_del_queue(vdev, index * 2 + 1);
1450 static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
1452 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1453 int old_num_queues = virtio_get_num_queues(vdev);
1454 int new_num_queues = new_max_queues * 2 + 1;
1455 int i;
1457 assert(old_num_queues >= 3);
1458 assert(old_num_queues % 2 == 1);
1460 if (old_num_queues == new_num_queues) {
1461 return;
1465 * We always need to remove and add ctrl vq if
1466 * old_num_queues != new_num_queues. Remove ctrl_vq first,
1467 * and then we only enter one of the following too loops.
1469 virtio_del_queue(vdev, old_num_queues - 1);
1471 for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
1472 /* new_num_queues < old_num_queues */
1473 virtio_net_del_queue(n, i / 2);
1476 for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
1477 /* new_num_queues > old_num_queues */
1478 virtio_net_add_queue(n, i / 2);
1481 /* add ctrl_vq last */
1482 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1485 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
1487 int max = multiqueue ? n->max_queues : 1;
1489 n->multiqueue = multiqueue;
1490 virtio_net_change_num_queues(n, max);
1492 virtio_net_set_queues(n);
1495 static void virtio_net_save(QEMUFile *f, void *opaque)
1497 VirtIONet *n = opaque;
1498 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1500 /* At this point, backend must be stopped, otherwise
1501 * it might keep writing to memory. */
1502 assert(!n->vhost_started);
1503 virtio_save(vdev, f);
1506 static void virtio_net_save_device(VirtIODevice *vdev, QEMUFile *f)
1508 VirtIONet *n = VIRTIO_NET(vdev);
1509 int i;
1511 qemu_put_buffer(f, n->mac, ETH_ALEN);
1512 qemu_put_be32(f, n->vqs[0].tx_waiting);
1513 qemu_put_be32(f, n->mergeable_rx_bufs);
1514 qemu_put_be16(f, n->status);
1515 qemu_put_byte(f, n->promisc);
1516 qemu_put_byte(f, n->allmulti);
1517 qemu_put_be32(f, n->mac_table.in_use);
1518 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
1519 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1520 qemu_put_be32(f, n->has_vnet_hdr);
1521 qemu_put_byte(f, n->mac_table.multi_overflow);
1522 qemu_put_byte(f, n->mac_table.uni_overflow);
1523 qemu_put_byte(f, n->alluni);
1524 qemu_put_byte(f, n->nomulti);
1525 qemu_put_byte(f, n->nouni);
1526 qemu_put_byte(f, n->nobcast);
1527 qemu_put_byte(f, n->has_ufo);
1528 if (n->max_queues > 1) {
1529 qemu_put_be16(f, n->max_queues);
1530 qemu_put_be16(f, n->curr_queues);
1531 for (i = 1; i < n->curr_queues; i++) {
1532 qemu_put_be32(f, n->vqs[i].tx_waiting);
1536 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1537 qemu_put_be64(f, n->curr_guest_offloads);
1541 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
1543 VirtIONet *n = opaque;
1544 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1546 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
1547 return -EINVAL;
1549 return virtio_load(vdev, f, version_id);
1552 static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
1553 int version_id)
1555 VirtIONet *n = VIRTIO_NET(vdev);
1556 int i, link_down;
1558 qemu_get_buffer(f, n->mac, ETH_ALEN);
1559 n->vqs[0].tx_waiting = qemu_get_be32(f);
1561 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f),
1562 virtio_vdev_has_feature(vdev,
1563 VIRTIO_F_VERSION_1));
1565 if (version_id >= 3)
1566 n->status = qemu_get_be16(f);
1568 if (version_id >= 4) {
1569 if (version_id < 8) {
1570 n->promisc = qemu_get_be32(f);
1571 n->allmulti = qemu_get_be32(f);
1572 } else {
1573 n->promisc = qemu_get_byte(f);
1574 n->allmulti = qemu_get_byte(f);
1578 if (version_id >= 5) {
1579 n->mac_table.in_use = qemu_get_be32(f);
1580 /* MAC_TABLE_ENTRIES may be different from the saved image */
1581 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
1582 qemu_get_buffer(f, n->mac_table.macs,
1583 n->mac_table.in_use * ETH_ALEN);
1584 } else {
1585 int64_t i;
1587 /* Overflow detected - can happen if source has a larger MAC table.
1588 * We simply set overflow flag so there's no need to maintain the
1589 * table of addresses, discard them all.
1590 * Note: 64 bit math to avoid integer overflow.
1592 for (i = 0; i < (int64_t)n->mac_table.in_use * ETH_ALEN; ++i) {
1593 qemu_get_byte(f);
1595 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
1596 n->mac_table.in_use = 0;
1600 if (version_id >= 6)
1601 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1603 if (version_id >= 7) {
1604 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
1605 error_report("virtio-net: saved image requires vnet_hdr=on");
1606 return -1;
1610 if (version_id >= 9) {
1611 n->mac_table.multi_overflow = qemu_get_byte(f);
1612 n->mac_table.uni_overflow = qemu_get_byte(f);
1615 if (version_id >= 10) {
1616 n->alluni = qemu_get_byte(f);
1617 n->nomulti = qemu_get_byte(f);
1618 n->nouni = qemu_get_byte(f);
1619 n->nobcast = qemu_get_byte(f);
1622 if (version_id >= 11) {
1623 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
1624 error_report("virtio-net: saved image requires TUN_F_UFO support");
1625 return -1;
1629 if (n->max_queues > 1) {
1630 if (n->max_queues != qemu_get_be16(f)) {
1631 error_report("virtio-net: different max_queues ");
1632 return -1;
1635 n->curr_queues = qemu_get_be16(f);
1636 if (n->curr_queues > n->max_queues) {
1637 error_report("virtio-net: curr_queues %x > max_queues %x",
1638 n->curr_queues, n->max_queues);
1639 return -1;
1641 for (i = 1; i < n->curr_queues; i++) {
1642 n->vqs[i].tx_waiting = qemu_get_be32(f);
1646 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1647 n->curr_guest_offloads = qemu_get_be64(f);
1648 } else {
1649 n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
1652 if (peer_has_vnet_hdr(n)) {
1653 virtio_net_apply_guest_offloads(n);
1656 virtio_net_set_queues(n);
1658 /* Find the first multicast entry in the saved MAC filter */
1659 for (i = 0; i < n->mac_table.in_use; i++) {
1660 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1661 break;
1664 n->mac_table.first_multi = i;
1666 /* nc.link_down can't be migrated, so infer link_down according
1667 * to link status bit in n->status */
1668 link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
1669 for (i = 0; i < n->max_queues; i++) {
1670 qemu_get_subqueue(n->nic, i)->link_down = link_down;
1673 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
1674 virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
1675 n->announce_counter = SELF_ANNOUNCE_ROUNDS;
1676 timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL));
1679 return 0;
1682 static NetClientInfo net_virtio_info = {
1683 .type = NET_CLIENT_OPTIONS_KIND_NIC,
1684 .size = sizeof(NICState),
1685 .can_receive = virtio_net_can_receive,
1686 .receive = virtio_net_receive,
1687 .link_status_changed = virtio_net_set_link_status,
1688 .query_rx_filter = virtio_net_query_rxfilter,
1691 static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1693 VirtIONet *n = VIRTIO_NET(vdev);
1694 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1695 assert(n->vhost_started);
1696 return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
1699 static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1700 bool mask)
1702 VirtIONet *n = VIRTIO_NET(vdev);
1703 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1704 assert(n->vhost_started);
1705 vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
1706 vdev, idx, mask);
1709 static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
1711 int i, config_size = 0;
1712 virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
1713 for (i = 0; feature_sizes[i].flags != 0; i++) {
1714 if (host_features & feature_sizes[i].flags) {
1715 config_size = MAX(feature_sizes[i].end, config_size);
1718 n->config_size = config_size;
1721 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
1722 const char *type)
1725 * The name can be NULL, the netclient name will be type.x.
1727 assert(type != NULL);
1729 g_free(n->netclient_name);
1730 g_free(n->netclient_type);
1731 n->netclient_name = g_strdup(name);
1732 n->netclient_type = g_strdup(type);
1735 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
1737 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1738 VirtIONet *n = VIRTIO_NET(dev);
1739 NetClientState *nc;
1740 int i;
1742 virtio_net_set_config_size(n, n->host_features);
1743 virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
1745 n->max_queues = MAX(n->nic_conf.peers.queues, 1);
1746 if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
1747 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
1748 "must be a positive integer less than %d.",
1749 n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
1750 virtio_cleanup(vdev);
1751 return;
1753 n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
1754 n->curr_queues = 1;
1755 n->tx_timeout = n->net_conf.txtimer;
1757 if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
1758 && strcmp(n->net_conf.tx, "bh")) {
1759 error_report("virtio-net: "
1760 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1761 n->net_conf.tx);
1762 error_report("Defaulting to \"bh\"");
1765 for (i = 0; i < n->max_queues; i++) {
1766 virtio_net_add_queue(n, i);
1769 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1770 qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
1771 memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
1772 n->status = VIRTIO_NET_S_LINK_UP;
1773 n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1774 virtio_net_announce_timer, n);
1776 if (n->netclient_type) {
1778 * Happen when virtio_net_set_netclient_name has been called.
1780 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1781 n->netclient_type, n->netclient_name, n);
1782 } else {
1783 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1784 object_get_typename(OBJECT(dev)), dev->id, n);
1787 peer_test_vnet_hdr(n);
1788 if (peer_has_vnet_hdr(n)) {
1789 for (i = 0; i < n->max_queues; i++) {
1790 qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
1792 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1793 } else {
1794 n->host_hdr_len = 0;
1797 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
1799 n->vqs[0].tx_waiting = 0;
1800 n->tx_burst = n->net_conf.txburst;
1801 virtio_net_set_mrg_rx_bufs(n, 0, 0);
1802 n->promisc = 1; /* for compatibility */
1804 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1806 n->vlans = g_malloc0(MAX_VLAN >> 3);
1808 nc = qemu_get_queue(n->nic);
1809 nc->rxfilter_notify_enabled = 1;
1811 n->qdev = dev;
1812 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
1813 virtio_net_save, virtio_net_load, n);
1816 static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
1818 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1819 VirtIONet *n = VIRTIO_NET(dev);
1820 int i, max_queues;
1822 /* This will stop vhost backend if appropriate. */
1823 virtio_net_set_status(vdev, 0);
1825 unregister_savevm(dev, "virtio-net", n);
1827 g_free(n->netclient_name);
1828 n->netclient_name = NULL;
1829 g_free(n->netclient_type);
1830 n->netclient_type = NULL;
1832 g_free(n->mac_table.macs);
1833 g_free(n->vlans);
1835 max_queues = n->multiqueue ? n->max_queues : 1;
1836 for (i = 0; i < max_queues; i++) {
1837 virtio_net_del_queue(n, i);
1840 timer_del(n->announce_timer);
1841 timer_free(n->announce_timer);
1842 g_free(n->vqs);
1843 qemu_del_nic(n->nic);
1844 virtio_cleanup(vdev);
1847 static void virtio_net_instance_init(Object *obj)
1849 VirtIONet *n = VIRTIO_NET(obj);
1852 * The default config_size is sizeof(struct virtio_net_config).
1853 * Can be overriden with virtio_net_set_config_size.
1855 n->config_size = sizeof(struct virtio_net_config);
1856 device_add_bootindex_property(obj, &n->nic_conf.bootindex,
1857 "bootindex", "/ethernet-phy@0",
1858 DEVICE(n), NULL);
1861 static Property virtio_net_properties[] = {
1862 DEFINE_PROP_BIT("csum", VirtIONet, host_features, VIRTIO_NET_F_CSUM, true),
1863 DEFINE_PROP_BIT("guest_csum", VirtIONet, host_features,
1864 VIRTIO_NET_F_GUEST_CSUM, true),
1865 DEFINE_PROP_BIT("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
1866 DEFINE_PROP_BIT("guest_tso4", VirtIONet, host_features,
1867 VIRTIO_NET_F_GUEST_TSO4, true),
1868 DEFINE_PROP_BIT("guest_tso6", VirtIONet, host_features,
1869 VIRTIO_NET_F_GUEST_TSO6, true),
1870 DEFINE_PROP_BIT("guest_ecn", VirtIONet, host_features,
1871 VIRTIO_NET_F_GUEST_ECN, true),
1872 DEFINE_PROP_BIT("guest_ufo", VirtIONet, host_features,
1873 VIRTIO_NET_F_GUEST_UFO, true),
1874 DEFINE_PROP_BIT("guest_announce", VirtIONet, host_features,
1875 VIRTIO_NET_F_GUEST_ANNOUNCE, true),
1876 DEFINE_PROP_BIT("host_tso4", VirtIONet, host_features,
1877 VIRTIO_NET_F_HOST_TSO4, true),
1878 DEFINE_PROP_BIT("host_tso6", VirtIONet, host_features,
1879 VIRTIO_NET_F_HOST_TSO6, true),
1880 DEFINE_PROP_BIT("host_ecn", VirtIONet, host_features,
1881 VIRTIO_NET_F_HOST_ECN, true),
1882 DEFINE_PROP_BIT("host_ufo", VirtIONet, host_features,
1883 VIRTIO_NET_F_HOST_UFO, true),
1884 DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet, host_features,
1885 VIRTIO_NET_F_MRG_RXBUF, true),
1886 DEFINE_PROP_BIT("status", VirtIONet, host_features,
1887 VIRTIO_NET_F_STATUS, true),
1888 DEFINE_PROP_BIT("ctrl_vq", VirtIONet, host_features,
1889 VIRTIO_NET_F_CTRL_VQ, true),
1890 DEFINE_PROP_BIT("ctrl_rx", VirtIONet, host_features,
1891 VIRTIO_NET_F_CTRL_RX, true),
1892 DEFINE_PROP_BIT("ctrl_vlan", VirtIONet, host_features,
1893 VIRTIO_NET_F_CTRL_VLAN, true),
1894 DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet, host_features,
1895 VIRTIO_NET_F_CTRL_RX_EXTRA, true),
1896 DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet, host_features,
1897 VIRTIO_NET_F_CTRL_MAC_ADDR, true),
1898 DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet, host_features,
1899 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
1900 DEFINE_PROP_BIT("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
1901 DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
1902 DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
1903 TX_TIMER_INTERVAL),
1904 DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
1905 DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
1906 DEFINE_PROP_END_OF_LIST(),
1909 static void virtio_net_class_init(ObjectClass *klass, void *data)
1911 DeviceClass *dc = DEVICE_CLASS(klass);
1912 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1914 dc->props = virtio_net_properties;
1915 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1916 vdc->realize = virtio_net_device_realize;
1917 vdc->unrealize = virtio_net_device_unrealize;
1918 vdc->get_config = virtio_net_get_config;
1919 vdc->set_config = virtio_net_set_config;
1920 vdc->get_features = virtio_net_get_features;
1921 vdc->set_features = virtio_net_set_features;
1922 vdc->bad_features = virtio_net_bad_features;
1923 vdc->reset = virtio_net_reset;
1924 vdc->set_status = virtio_net_set_status;
1925 vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
1926 vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
1927 vdc->load = virtio_net_load_device;
1928 vdc->save = virtio_net_save_device;
1931 static const TypeInfo virtio_net_info = {
1932 .name = TYPE_VIRTIO_NET,
1933 .parent = TYPE_VIRTIO_DEVICE,
1934 .instance_size = sizeof(VirtIONet),
1935 .instance_init = virtio_net_instance_init,
1936 .class_init = virtio_net_class_init,
1939 static void virtio_register_types(void)
1941 type_register_static(&virtio_net_info);
1944 type_init(virtio_register_types)