virtio-net: convert VMSTATE_VIRTIO_DEVICE
[qemu/ar7.git] / hw / net / virtio-net.c
blobb2198a550eff7c6e40cf206049d1352e3f1f70ca
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 #define VMSTATE_VIRTIO_DEVICE_USE_NEW
16 #include "qemu/osdep.h"
17 #include "qemu/iov.h"
18 #include "hw/virtio/virtio.h"
19 #include "net/net.h"
20 #include "net/checksum.h"
21 #include "net/tap.h"
22 #include "qemu/error-report.h"
23 #include "qemu/timer.h"
24 #include "hw/virtio/virtio-net.h"
25 #include "net/vhost_net.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "qapi/qmp/qjson.h"
28 #include "qapi-event.h"
29 #include "hw/virtio/virtio-access.h"
31 #define VIRTIO_NET_VM_VERSION 11
33 #define MAC_TABLE_ENTRIES 64
34 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
36 /* previously fixed value */
37 #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
38 /* for now, only allow larger queues; with virtio-1, guest can downsize */
39 #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
42 * Calculate the number of bytes up to and including the given 'field' of
43 * 'container'.
45 #define endof(container, field) \
46 (offsetof(container, field) + sizeof(((container *)0)->field))
48 typedef struct VirtIOFeature {
49 uint32_t flags;
50 size_t end;
51 } VirtIOFeature;
53 static VirtIOFeature feature_sizes[] = {
54 {.flags = 1 << VIRTIO_NET_F_MAC,
55 .end = endof(struct virtio_net_config, mac)},
56 {.flags = 1 << VIRTIO_NET_F_STATUS,
57 .end = endof(struct virtio_net_config, status)},
58 {.flags = 1 << VIRTIO_NET_F_MQ,
59 .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
63 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
65 VirtIONet *n = qemu_get_nic_opaque(nc);
67 return &n->vqs[nc->queue_index];
70 static int vq2q(int queue_index)
72 return queue_index / 2;
75 /* TODO
76 * - we could suppress RX interrupt if we were so inclined.
79 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
81 VirtIONet *n = VIRTIO_NET(vdev);
82 struct virtio_net_config netcfg;
84 virtio_stw_p(vdev, &netcfg.status, n->status);
85 virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
86 memcpy(netcfg.mac, n->mac, ETH_ALEN);
87 memcpy(config, &netcfg, n->config_size);
90 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
92 VirtIONet *n = VIRTIO_NET(vdev);
93 struct virtio_net_config netcfg = {};
95 memcpy(&netcfg, config, n->config_size);
97 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
98 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
99 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
100 memcpy(n->mac, netcfg.mac, ETH_ALEN);
101 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
105 static bool virtio_net_started(VirtIONet *n, uint8_t status)
107 VirtIODevice *vdev = VIRTIO_DEVICE(n);
108 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
109 (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
112 static void virtio_net_announce_timer(void *opaque)
114 VirtIONet *n = opaque;
115 VirtIODevice *vdev = VIRTIO_DEVICE(n);
117 n->announce_counter--;
118 n->status |= VIRTIO_NET_S_ANNOUNCE;
119 virtio_notify_config(vdev);
122 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
124 VirtIODevice *vdev = VIRTIO_DEVICE(n);
125 NetClientState *nc = qemu_get_queue(n->nic);
126 int queues = n->multiqueue ? n->max_queues : 1;
128 if (!get_vhost_net(nc->peer)) {
129 return;
132 if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
133 !!n->vhost_started) {
134 return;
136 if (!n->vhost_started) {
137 int r, i;
139 if (n->needs_vnet_hdr_swap) {
140 error_report("backend does not support %s vnet headers; "
141 "falling back on userspace virtio",
142 virtio_is_big_endian(vdev) ? "BE" : "LE");
143 return;
146 /* Any packets outstanding? Purge them to avoid touching rings
147 * when vhost is running.
149 for (i = 0; i < queues; i++) {
150 NetClientState *qnc = qemu_get_subqueue(n->nic, i);
152 /* Purge both directions: TX and RX. */
153 qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
154 qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
157 n->vhost_started = 1;
158 r = vhost_net_start(vdev, n->nic->ncs, queues);
159 if (r < 0) {
160 error_report("unable to start vhost net: %d: "
161 "falling back on userspace virtio", -r);
162 n->vhost_started = 0;
164 } else {
165 vhost_net_stop(vdev, n->nic->ncs, queues);
166 n->vhost_started = 0;
170 static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
171 NetClientState *peer,
172 bool enable)
174 if (virtio_is_big_endian(vdev)) {
175 return qemu_set_vnet_be(peer, enable);
176 } else {
177 return qemu_set_vnet_le(peer, enable);
181 static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
182 int queues, bool enable)
184 int i;
186 for (i = 0; i < queues; i++) {
187 if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
188 enable) {
189 while (--i >= 0) {
190 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
193 return true;
197 return false;
200 static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
202 VirtIODevice *vdev = VIRTIO_DEVICE(n);
203 int queues = n->multiqueue ? n->max_queues : 1;
205 if (virtio_net_started(n, status)) {
206 /* Before using the device, we tell the network backend about the
207 * endianness to use when parsing vnet headers. If the backend
208 * can't do it, we fallback onto fixing the headers in the core
209 * virtio-net code.
211 n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
212 queues, true);
213 } else if (virtio_net_started(n, vdev->status)) {
214 /* After using the device, we need to reset the network backend to
215 * the default (guest native endianness), otherwise the guest may
216 * lose network connectivity if it is rebooted into a different
217 * endianness.
219 virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false);
223 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
225 VirtIONet *n = VIRTIO_NET(vdev);
226 VirtIONetQueue *q;
227 int i;
228 uint8_t queue_status;
230 virtio_net_vnet_endian_status(n, status);
231 virtio_net_vhost_status(n, status);
233 for (i = 0; i < n->max_queues; i++) {
234 NetClientState *ncs = qemu_get_subqueue(n->nic, i);
235 bool queue_started;
236 q = &n->vqs[i];
238 if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
239 queue_status = 0;
240 } else {
241 queue_status = status;
243 queue_started =
244 virtio_net_started(n, queue_status) && !n->vhost_started;
246 if (queue_started) {
247 qemu_flush_queued_packets(ncs);
250 if (!q->tx_waiting) {
251 continue;
254 if (queue_started) {
255 if (q->tx_timer) {
256 timer_mod(q->tx_timer,
257 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
258 } else {
259 qemu_bh_schedule(q->tx_bh);
261 } else {
262 if (q->tx_timer) {
263 timer_del(q->tx_timer);
264 } else {
265 qemu_bh_cancel(q->tx_bh);
271 static void virtio_net_set_link_status(NetClientState *nc)
273 VirtIONet *n = qemu_get_nic_opaque(nc);
274 VirtIODevice *vdev = VIRTIO_DEVICE(n);
275 uint16_t old_status = n->status;
277 if (nc->link_down)
278 n->status &= ~VIRTIO_NET_S_LINK_UP;
279 else
280 n->status |= VIRTIO_NET_S_LINK_UP;
282 if (n->status != old_status)
283 virtio_notify_config(vdev);
285 virtio_net_set_status(vdev, vdev->status);
288 static void rxfilter_notify(NetClientState *nc)
290 VirtIONet *n = qemu_get_nic_opaque(nc);
292 if (nc->rxfilter_notify_enabled) {
293 gchar *path = object_get_canonical_path(OBJECT(n->qdev));
294 qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
295 n->netclient_name, path, &error_abort);
296 g_free(path);
298 /* disable event notification to avoid events flooding */
299 nc->rxfilter_notify_enabled = 0;
303 static intList *get_vlan_table(VirtIONet *n)
305 intList *list, *entry;
306 int i, j;
308 list = NULL;
309 for (i = 0; i < MAX_VLAN >> 5; i++) {
310 for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
311 if (n->vlans[i] & (1U << j)) {
312 entry = g_malloc0(sizeof(*entry));
313 entry->value = (i << 5) + j;
314 entry->next = list;
315 list = entry;
320 return list;
323 static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
325 VirtIONet *n = qemu_get_nic_opaque(nc);
326 VirtIODevice *vdev = VIRTIO_DEVICE(n);
327 RxFilterInfo *info;
328 strList *str_list, *entry;
329 int i;
331 info = g_malloc0(sizeof(*info));
332 info->name = g_strdup(nc->name);
333 info->promiscuous = n->promisc;
335 if (n->nouni) {
336 info->unicast = RX_STATE_NONE;
337 } else if (n->alluni) {
338 info->unicast = RX_STATE_ALL;
339 } else {
340 info->unicast = RX_STATE_NORMAL;
343 if (n->nomulti) {
344 info->multicast = RX_STATE_NONE;
345 } else if (n->allmulti) {
346 info->multicast = RX_STATE_ALL;
347 } else {
348 info->multicast = RX_STATE_NORMAL;
351 info->broadcast_allowed = n->nobcast;
352 info->multicast_overflow = n->mac_table.multi_overflow;
353 info->unicast_overflow = n->mac_table.uni_overflow;
355 info->main_mac = qemu_mac_strdup_printf(n->mac);
357 str_list = NULL;
358 for (i = 0; i < n->mac_table.first_multi; i++) {
359 entry = g_malloc0(sizeof(*entry));
360 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
361 entry->next = str_list;
362 str_list = entry;
364 info->unicast_table = str_list;
366 str_list = NULL;
367 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
368 entry = g_malloc0(sizeof(*entry));
369 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
370 entry->next = str_list;
371 str_list = entry;
373 info->multicast_table = str_list;
374 info->vlan_table = get_vlan_table(n);
376 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
377 info->vlan = RX_STATE_ALL;
378 } else if (!info->vlan_table) {
379 info->vlan = RX_STATE_NONE;
380 } else {
381 info->vlan = RX_STATE_NORMAL;
384 /* enable event notification after query */
385 nc->rxfilter_notify_enabled = 1;
387 return info;
390 static void virtio_net_reset(VirtIODevice *vdev)
392 VirtIONet *n = VIRTIO_NET(vdev);
394 /* Reset back to compatibility mode */
395 n->promisc = 1;
396 n->allmulti = 0;
397 n->alluni = 0;
398 n->nomulti = 0;
399 n->nouni = 0;
400 n->nobcast = 0;
401 /* multiqueue is disabled by default */
402 n->curr_queues = 1;
403 timer_del(n->announce_timer);
404 n->announce_counter = 0;
405 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
407 /* Flush any MAC and VLAN filter table state */
408 n->mac_table.in_use = 0;
409 n->mac_table.first_multi = 0;
410 n->mac_table.multi_overflow = 0;
411 n->mac_table.uni_overflow = 0;
412 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
413 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
414 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
415 memset(n->vlans, 0, MAX_VLAN >> 3);
418 static void peer_test_vnet_hdr(VirtIONet *n)
420 NetClientState *nc = qemu_get_queue(n->nic);
421 if (!nc->peer) {
422 return;
425 n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
428 static int peer_has_vnet_hdr(VirtIONet *n)
430 return n->has_vnet_hdr;
433 static int peer_has_ufo(VirtIONet *n)
435 if (!peer_has_vnet_hdr(n))
436 return 0;
438 n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
440 return n->has_ufo;
443 static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
444 int version_1)
446 int i;
447 NetClientState *nc;
449 n->mergeable_rx_bufs = mergeable_rx_bufs;
451 if (version_1) {
452 n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
453 } else {
454 n->guest_hdr_len = n->mergeable_rx_bufs ?
455 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
456 sizeof(struct virtio_net_hdr);
459 for (i = 0; i < n->max_queues; i++) {
460 nc = qemu_get_subqueue(n->nic, i);
462 if (peer_has_vnet_hdr(n) &&
463 qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
464 qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
465 n->host_hdr_len = n->guest_hdr_len;
470 static int peer_attach(VirtIONet *n, int index)
472 NetClientState *nc = qemu_get_subqueue(n->nic, index);
474 if (!nc->peer) {
475 return 0;
478 if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
479 vhost_set_vring_enable(nc->peer, 1);
482 if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
483 return 0;
486 return tap_enable(nc->peer);
489 static int peer_detach(VirtIONet *n, int index)
491 NetClientState *nc = qemu_get_subqueue(n->nic, index);
493 if (!nc->peer) {
494 return 0;
497 if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
498 vhost_set_vring_enable(nc->peer, 0);
501 if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
502 return 0;
505 return tap_disable(nc->peer);
508 static void virtio_net_set_queues(VirtIONet *n)
510 int i;
511 int r;
513 for (i = 0; i < n->max_queues; i++) {
514 if (i < n->curr_queues) {
515 r = peer_attach(n, i);
516 assert(!r);
517 } else {
518 r = peer_detach(n, i);
519 assert(!r);
524 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
526 static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
527 Error **errp)
529 VirtIONet *n = VIRTIO_NET(vdev);
530 NetClientState *nc = qemu_get_queue(n->nic);
532 /* Firstly sync all virtio-net possible supported features */
533 features |= n->host_features;
535 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
537 if (!peer_has_vnet_hdr(n)) {
538 virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
539 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
540 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
541 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
543 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
544 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
545 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
546 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
549 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
550 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
551 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
554 if (!get_vhost_net(nc->peer)) {
555 return features;
557 return vhost_net_get_features(get_vhost_net(nc->peer), features);
560 static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
562 uint64_t features = 0;
564 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
565 * but also these: */
566 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
567 virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
568 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
569 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
570 virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
572 return features;
575 static void virtio_net_apply_guest_offloads(VirtIONet *n)
577 qemu_set_offload(qemu_get_queue(n->nic)->peer,
578 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
579 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
580 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
581 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
582 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
585 static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
587 static const uint64_t guest_offloads_mask =
588 (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
589 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
590 (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
591 (1ULL << VIRTIO_NET_F_GUEST_ECN) |
592 (1ULL << VIRTIO_NET_F_GUEST_UFO);
594 return guest_offloads_mask & features;
597 static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
599 VirtIODevice *vdev = VIRTIO_DEVICE(n);
600 return virtio_net_guest_offloads_by_features(vdev->guest_features);
603 static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
605 VirtIONet *n = VIRTIO_NET(vdev);
606 int i;
608 virtio_net_set_multiqueue(n,
609 virtio_has_feature(features, VIRTIO_NET_F_MQ));
611 virtio_net_set_mrg_rx_bufs(n,
612 virtio_has_feature(features,
613 VIRTIO_NET_F_MRG_RXBUF),
614 virtio_has_feature(features,
615 VIRTIO_F_VERSION_1));
617 if (n->has_vnet_hdr) {
618 n->curr_guest_offloads =
619 virtio_net_guest_offloads_by_features(features);
620 virtio_net_apply_guest_offloads(n);
623 for (i = 0; i < n->max_queues; i++) {
624 NetClientState *nc = qemu_get_subqueue(n->nic, i);
626 if (!get_vhost_net(nc->peer)) {
627 continue;
629 vhost_net_ack_features(get_vhost_net(nc->peer), features);
632 if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
633 memset(n->vlans, 0, MAX_VLAN >> 3);
634 } else {
635 memset(n->vlans, 0xff, MAX_VLAN >> 3);
639 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
640 struct iovec *iov, unsigned int iov_cnt)
642 uint8_t on;
643 size_t s;
644 NetClientState *nc = qemu_get_queue(n->nic);
646 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
647 if (s != sizeof(on)) {
648 return VIRTIO_NET_ERR;
651 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
652 n->promisc = on;
653 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
654 n->allmulti = on;
655 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
656 n->alluni = on;
657 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
658 n->nomulti = on;
659 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
660 n->nouni = on;
661 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
662 n->nobcast = on;
663 } else {
664 return VIRTIO_NET_ERR;
667 rxfilter_notify(nc);
669 return VIRTIO_NET_OK;
672 static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
673 struct iovec *iov, unsigned int iov_cnt)
675 VirtIODevice *vdev = VIRTIO_DEVICE(n);
676 uint64_t offloads;
677 size_t s;
679 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
680 return VIRTIO_NET_ERR;
683 s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
684 if (s != sizeof(offloads)) {
685 return VIRTIO_NET_ERR;
688 if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
689 uint64_t supported_offloads;
691 if (!n->has_vnet_hdr) {
692 return VIRTIO_NET_ERR;
695 supported_offloads = virtio_net_supported_guest_offloads(n);
696 if (offloads & ~supported_offloads) {
697 return VIRTIO_NET_ERR;
700 n->curr_guest_offloads = offloads;
701 virtio_net_apply_guest_offloads(n);
703 return VIRTIO_NET_OK;
704 } else {
705 return VIRTIO_NET_ERR;
709 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
710 struct iovec *iov, unsigned int iov_cnt)
712 VirtIODevice *vdev = VIRTIO_DEVICE(n);
713 struct virtio_net_ctrl_mac mac_data;
714 size_t s;
715 NetClientState *nc = qemu_get_queue(n->nic);
717 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
718 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
719 return VIRTIO_NET_ERR;
721 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
722 assert(s == sizeof(n->mac));
723 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
724 rxfilter_notify(nc);
726 return VIRTIO_NET_OK;
729 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
730 return VIRTIO_NET_ERR;
733 int in_use = 0;
734 int first_multi = 0;
735 uint8_t uni_overflow = 0;
736 uint8_t multi_overflow = 0;
737 uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
739 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
740 sizeof(mac_data.entries));
741 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
742 if (s != sizeof(mac_data.entries)) {
743 goto error;
745 iov_discard_front(&iov, &iov_cnt, s);
747 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
748 goto error;
751 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
752 s = iov_to_buf(iov, iov_cnt, 0, macs,
753 mac_data.entries * ETH_ALEN);
754 if (s != mac_data.entries * ETH_ALEN) {
755 goto error;
757 in_use += mac_data.entries;
758 } else {
759 uni_overflow = 1;
762 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
764 first_multi = in_use;
766 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
767 sizeof(mac_data.entries));
768 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
769 if (s != sizeof(mac_data.entries)) {
770 goto error;
773 iov_discard_front(&iov, &iov_cnt, s);
775 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
776 goto error;
779 if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
780 s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
781 mac_data.entries * ETH_ALEN);
782 if (s != mac_data.entries * ETH_ALEN) {
783 goto error;
785 in_use += mac_data.entries;
786 } else {
787 multi_overflow = 1;
790 n->mac_table.in_use = in_use;
791 n->mac_table.first_multi = first_multi;
792 n->mac_table.uni_overflow = uni_overflow;
793 n->mac_table.multi_overflow = multi_overflow;
794 memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
795 g_free(macs);
796 rxfilter_notify(nc);
798 return VIRTIO_NET_OK;
800 error:
801 g_free(macs);
802 return VIRTIO_NET_ERR;
805 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
806 struct iovec *iov, unsigned int iov_cnt)
808 VirtIODevice *vdev = VIRTIO_DEVICE(n);
809 uint16_t vid;
810 size_t s;
811 NetClientState *nc = qemu_get_queue(n->nic);
813 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
814 vid = virtio_lduw_p(vdev, &vid);
815 if (s != sizeof(vid)) {
816 return VIRTIO_NET_ERR;
819 if (vid >= MAX_VLAN)
820 return VIRTIO_NET_ERR;
822 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
823 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
824 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
825 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
826 else
827 return VIRTIO_NET_ERR;
829 rxfilter_notify(nc);
831 return VIRTIO_NET_OK;
834 static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
835 struct iovec *iov, unsigned int iov_cnt)
837 if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
838 n->status & VIRTIO_NET_S_ANNOUNCE) {
839 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
840 if (n->announce_counter) {
841 timer_mod(n->announce_timer,
842 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
843 self_announce_delay(n->announce_counter));
845 return VIRTIO_NET_OK;
846 } else {
847 return VIRTIO_NET_ERR;
851 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
852 struct iovec *iov, unsigned int iov_cnt)
854 VirtIODevice *vdev = VIRTIO_DEVICE(n);
855 struct virtio_net_ctrl_mq mq;
856 size_t s;
857 uint16_t queues;
859 s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
860 if (s != sizeof(mq)) {
861 return VIRTIO_NET_ERR;
864 if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
865 return VIRTIO_NET_ERR;
868 queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
870 if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
871 queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
872 queues > n->max_queues ||
873 !n->multiqueue) {
874 return VIRTIO_NET_ERR;
877 n->curr_queues = queues;
878 /* stop the backend before changing the number of queues to avoid handling a
879 * disabled queue */
880 virtio_net_set_status(vdev, vdev->status);
881 virtio_net_set_queues(n);
883 return VIRTIO_NET_OK;
886 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
888 VirtIONet *n = VIRTIO_NET(vdev);
889 struct virtio_net_ctrl_hdr ctrl;
890 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
891 VirtQueueElement *elem;
892 size_t s;
893 struct iovec *iov, *iov2;
894 unsigned int iov_cnt;
896 for (;;) {
897 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
898 if (!elem) {
899 break;
901 if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
902 iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
903 virtio_error(vdev, "virtio-net ctrl missing headers");
904 virtqueue_detach_element(vq, elem, 0);
905 g_free(elem);
906 break;
909 iov_cnt = elem->out_num;
910 iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
911 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
912 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
913 if (s != sizeof(ctrl)) {
914 status = VIRTIO_NET_ERR;
915 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
916 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
917 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
918 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
919 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
920 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
921 } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
922 status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
923 } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
924 status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
925 } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
926 status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
929 s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
930 assert(s == sizeof(status));
932 virtqueue_push(vq, elem, sizeof(status));
933 virtio_notify(vdev, vq);
934 g_free(iov2);
935 g_free(elem);
939 /* RX */
941 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
943 VirtIONet *n = VIRTIO_NET(vdev);
944 int queue_index = vq2q(virtio_get_queue_index(vq));
946 qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
949 static int virtio_net_can_receive(NetClientState *nc)
951 VirtIONet *n = qemu_get_nic_opaque(nc);
952 VirtIODevice *vdev = VIRTIO_DEVICE(n);
953 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
955 if (!vdev->vm_running) {
956 return 0;
959 if (nc->queue_index >= n->curr_queues) {
960 return 0;
963 if (!virtio_queue_ready(q->rx_vq) ||
964 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
965 return 0;
968 return 1;
971 static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
973 VirtIONet *n = q->n;
974 if (virtio_queue_empty(q->rx_vq) ||
975 (n->mergeable_rx_bufs &&
976 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
977 virtio_queue_set_notification(q->rx_vq, 1);
979 /* To avoid a race condition where the guest has made some buffers
980 * available after the above check but before notification was
981 * enabled, check for available buffers again.
983 if (virtio_queue_empty(q->rx_vq) ||
984 (n->mergeable_rx_bufs &&
985 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
986 return 0;
990 virtio_queue_set_notification(q->rx_vq, 0);
991 return 1;
994 static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
996 virtio_tswap16s(vdev, &hdr->hdr_len);
997 virtio_tswap16s(vdev, &hdr->gso_size);
998 virtio_tswap16s(vdev, &hdr->csum_start);
999 virtio_tswap16s(vdev, &hdr->csum_offset);
1002 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1003 * it never finds out that the packets don't have valid checksums. This
1004 * causes dhclient to get upset. Fedora's carried a patch for ages to
1005 * fix this with Xen but it hasn't appeared in an upstream release of
1006 * dhclient yet.
1008 * To avoid breaking existing guests, we catch udp packets and add
1009 * checksums. This is terrible but it's better than hacking the guest
1010 * kernels.
1012 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1013 * we should provide a mechanism to disable it to avoid polluting the host
1014 * cache.
1016 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
1017 uint8_t *buf, size_t size)
1019 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
1020 (size > 27 && size < 1500) && /* normal sized MTU */
1021 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
1022 (buf[23] == 17) && /* ip.protocol == UDP */
1023 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1024 net_checksum_calculate(buf, size);
1025 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
1029 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
1030 const void *buf, size_t size)
1032 if (n->has_vnet_hdr) {
1033 /* FIXME this cast is evil */
1034 void *wbuf = (void *)buf;
1035 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
1036 size - n->host_hdr_len);
1038 if (n->needs_vnet_hdr_swap) {
1039 virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
1041 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
1042 } else {
1043 struct virtio_net_hdr hdr = {
1044 .flags = 0,
1045 .gso_type = VIRTIO_NET_HDR_GSO_NONE
1047 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
1051 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
1053 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1054 static const uint8_t vlan[] = {0x81, 0x00};
1055 uint8_t *ptr = (uint8_t *)buf;
1056 int i;
1058 if (n->promisc)
1059 return 1;
1061 ptr += n->host_hdr_len;
1063 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
1064 int vid = lduw_be_p(ptr + 14) & 0xfff;
1065 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
1066 return 0;
1069 if (ptr[0] & 1) { // multicast
1070 if (!memcmp(ptr, bcast, sizeof(bcast))) {
1071 return !n->nobcast;
1072 } else if (n->nomulti) {
1073 return 0;
1074 } else if (n->allmulti || n->mac_table.multi_overflow) {
1075 return 1;
1078 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
1079 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1080 return 1;
1083 } else { // unicast
1084 if (n->nouni) {
1085 return 0;
1086 } else if (n->alluni || n->mac_table.uni_overflow) {
1087 return 1;
1088 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
1089 return 1;
1092 for (i = 0; i < n->mac_table.first_multi; i++) {
1093 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1094 return 1;
1099 return 0;
1102 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
1104 VirtIONet *n = qemu_get_nic_opaque(nc);
1105 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1106 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1107 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
1108 struct virtio_net_hdr_mrg_rxbuf mhdr;
1109 unsigned mhdr_cnt = 0;
1110 size_t offset, i, guest_offset;
1112 if (!virtio_net_can_receive(nc)) {
1113 return -1;
1116 /* hdr_len refers to the header we supply to the guest */
1117 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
1118 return 0;
1121 if (!receive_filter(n, buf, size))
1122 return size;
1124 offset = i = 0;
1126 while (offset < size) {
1127 VirtQueueElement *elem;
1128 int len, total;
1129 const struct iovec *sg;
1131 total = 0;
1133 elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
1134 if (!elem) {
1135 if (i) {
1136 virtio_error(vdev, "virtio-net unexpected empty queue: "
1137 "i %zd mergeable %d offset %zd, size %zd, "
1138 "guest hdr len %zd, host hdr len %zd "
1139 "guest features 0x%" PRIx64,
1140 i, n->mergeable_rx_bufs, offset, size,
1141 n->guest_hdr_len, n->host_hdr_len,
1142 vdev->guest_features);
1144 return -1;
1147 if (elem->in_num < 1) {
1148 virtio_error(vdev,
1149 "virtio-net receive queue contains no in buffers");
1150 virtqueue_detach_element(q->rx_vq, elem, 0);
1151 g_free(elem);
1152 return -1;
1155 sg = elem->in_sg;
1156 if (i == 0) {
1157 assert(offset == 0);
1158 if (n->mergeable_rx_bufs) {
1159 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
1160 sg, elem->in_num,
1161 offsetof(typeof(mhdr), num_buffers),
1162 sizeof(mhdr.num_buffers));
1165 receive_header(n, sg, elem->in_num, buf, size);
1166 offset = n->host_hdr_len;
1167 total += n->guest_hdr_len;
1168 guest_offset = n->guest_hdr_len;
1169 } else {
1170 guest_offset = 0;
1173 /* copy in packet. ugh */
1174 len = iov_from_buf(sg, elem->in_num, guest_offset,
1175 buf + offset, size - offset);
1176 total += len;
1177 offset += len;
1178 /* If buffers can't be merged, at this point we
1179 * must have consumed the complete packet.
1180 * Otherwise, drop it. */
1181 if (!n->mergeable_rx_bufs && offset < size) {
1182 virtqueue_discard(q->rx_vq, elem, total);
1183 g_free(elem);
1184 return size;
1187 /* signal other side */
1188 virtqueue_fill(q->rx_vq, elem, total, i++);
1189 g_free(elem);
1192 if (mhdr_cnt) {
1193 virtio_stw_p(vdev, &mhdr.num_buffers, i);
1194 iov_from_buf(mhdr_sg, mhdr_cnt,
1196 &mhdr.num_buffers, sizeof mhdr.num_buffers);
1199 virtqueue_flush(q->rx_vq, i);
1200 virtio_notify(vdev, q->rx_vq);
1202 return size;
1205 static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
1207 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
1209 VirtIONet *n = qemu_get_nic_opaque(nc);
1210 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1211 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1213 virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
1214 virtio_notify(vdev, q->tx_vq);
1216 g_free(q->async_tx.elem);
1217 q->async_tx.elem = NULL;
1219 virtio_queue_set_notification(q->tx_vq, 1);
1220 virtio_net_flush_tx(q);
1223 /* TX */
1224 static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
1226 VirtIONet *n = q->n;
1227 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1228 VirtQueueElement *elem;
1229 int32_t num_packets = 0;
1230 int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
1231 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1232 return num_packets;
1235 if (q->async_tx.elem) {
1236 virtio_queue_set_notification(q->tx_vq, 0);
1237 return num_packets;
1240 for (;;) {
1241 ssize_t ret;
1242 unsigned int out_num;
1243 struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
1244 struct virtio_net_hdr_mrg_rxbuf mhdr;
1246 elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
1247 if (!elem) {
1248 break;
1251 out_num = elem->out_num;
1252 out_sg = elem->out_sg;
1253 if (out_num < 1) {
1254 virtio_error(vdev, "virtio-net header not in first element");
1255 virtqueue_detach_element(q->tx_vq, elem, 0);
1256 g_free(elem);
1257 return -EINVAL;
1260 if (n->has_vnet_hdr) {
1261 if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
1262 n->guest_hdr_len) {
1263 virtio_error(vdev, "virtio-net header incorrect");
1264 virtqueue_detach_element(q->tx_vq, elem, 0);
1265 g_free(elem);
1266 return -EINVAL;
1268 if (n->needs_vnet_hdr_swap) {
1269 virtio_net_hdr_swap(vdev, (void *) &mhdr);
1270 sg2[0].iov_base = &mhdr;
1271 sg2[0].iov_len = n->guest_hdr_len;
1272 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
1273 out_sg, out_num,
1274 n->guest_hdr_len, -1);
1275 if (out_num == VIRTQUEUE_MAX_SIZE) {
1276 goto drop;
1278 out_num += 1;
1279 out_sg = sg2;
1283 * If host wants to see the guest header as is, we can
1284 * pass it on unchanged. Otherwise, copy just the parts
1285 * that host is interested in.
1287 assert(n->host_hdr_len <= n->guest_hdr_len);
1288 if (n->host_hdr_len != n->guest_hdr_len) {
1289 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
1290 out_sg, out_num,
1291 0, n->host_hdr_len);
1292 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
1293 out_sg, out_num,
1294 n->guest_hdr_len, -1);
1295 out_num = sg_num;
1296 out_sg = sg;
1299 ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
1300 out_sg, out_num, virtio_net_tx_complete);
1301 if (ret == 0) {
1302 virtio_queue_set_notification(q->tx_vq, 0);
1303 q->async_tx.elem = elem;
1304 return -EBUSY;
1307 drop:
1308 virtqueue_push(q->tx_vq, elem, 0);
1309 virtio_notify(vdev, q->tx_vq);
1310 g_free(elem);
1312 if (++num_packets >= n->tx_burst) {
1313 break;
1316 return num_packets;
1319 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
1321 VirtIONet *n = VIRTIO_NET(vdev);
1322 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
1324 /* This happens when device was stopped but VCPU wasn't. */
1325 if (!vdev->vm_running) {
1326 q->tx_waiting = 1;
1327 return;
1330 if (q->tx_waiting) {
1331 virtio_queue_set_notification(vq, 1);
1332 timer_del(q->tx_timer);
1333 q->tx_waiting = 0;
1334 if (virtio_net_flush_tx(q) == -EINVAL) {
1335 return;
1337 } else {
1338 timer_mod(q->tx_timer,
1339 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
1340 q->tx_waiting = 1;
1341 virtio_queue_set_notification(vq, 0);
1345 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
1347 VirtIONet *n = VIRTIO_NET(vdev);
1348 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
1350 if (unlikely(q->tx_waiting)) {
1351 return;
1353 q->tx_waiting = 1;
1354 /* This happens when device was stopped but VCPU wasn't. */
1355 if (!vdev->vm_running) {
1356 return;
1358 virtio_queue_set_notification(vq, 0);
1359 qemu_bh_schedule(q->tx_bh);
1362 static void virtio_net_tx_timer(void *opaque)
1364 VirtIONetQueue *q = opaque;
1365 VirtIONet *n = q->n;
1366 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1367 /* This happens when device was stopped but BH wasn't. */
1368 if (!vdev->vm_running) {
1369 /* Make sure tx waiting is set, so we'll run when restarted. */
1370 assert(q->tx_waiting);
1371 return;
1374 q->tx_waiting = 0;
1376 /* Just in case the driver is not ready on more */
1377 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1378 return;
1381 virtio_queue_set_notification(q->tx_vq, 1);
1382 virtio_net_flush_tx(q);
1385 static void virtio_net_tx_bh(void *opaque)
1387 VirtIONetQueue *q = opaque;
1388 VirtIONet *n = q->n;
1389 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1390 int32_t ret;
1392 /* This happens when device was stopped but BH wasn't. */
1393 if (!vdev->vm_running) {
1394 /* Make sure tx waiting is set, so we'll run when restarted. */
1395 assert(q->tx_waiting);
1396 return;
1399 q->tx_waiting = 0;
1401 /* Just in case the driver is not ready on more */
1402 if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
1403 return;
1406 ret = virtio_net_flush_tx(q);
1407 if (ret == -EBUSY || ret == -EINVAL) {
1408 return; /* Notification re-enable handled by tx_complete or device
1409 * broken */
1412 /* If we flush a full burst of packets, assume there are
1413 * more coming and immediately reschedule */
1414 if (ret >= n->tx_burst) {
1415 qemu_bh_schedule(q->tx_bh);
1416 q->tx_waiting = 1;
1417 return;
1420 /* If less than a full burst, re-enable notification and flush
1421 * anything that may have come in while we weren't looking. If
1422 * we find something, assume the guest is still active and reschedule */
1423 virtio_queue_set_notification(q->tx_vq, 1);
1424 ret = virtio_net_flush_tx(q);
1425 if (ret == -EINVAL) {
1426 return;
1427 } else if (ret > 0) {
1428 virtio_queue_set_notification(q->tx_vq, 0);
1429 qemu_bh_schedule(q->tx_bh);
1430 q->tx_waiting = 1;
1434 static void virtio_net_add_queue(VirtIONet *n, int index)
1436 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1438 n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
1439 virtio_net_handle_rx);
1440 if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
1441 n->vqs[index].tx_vq =
1442 virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
1443 n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1444 virtio_net_tx_timer,
1445 &n->vqs[index]);
1446 } else {
1447 n->vqs[index].tx_vq =
1448 virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
1449 n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
1452 n->vqs[index].tx_waiting = 0;
1453 n->vqs[index].n = n;
1456 static void virtio_net_del_queue(VirtIONet *n, int index)
1458 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1459 VirtIONetQueue *q = &n->vqs[index];
1460 NetClientState *nc = qemu_get_subqueue(n->nic, index);
1462 qemu_purge_queued_packets(nc);
1464 virtio_del_queue(vdev, index * 2);
1465 if (q->tx_timer) {
1466 timer_del(q->tx_timer);
1467 timer_free(q->tx_timer);
1468 } else {
1469 qemu_bh_delete(q->tx_bh);
1471 virtio_del_queue(vdev, index * 2 + 1);
1474 static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
1476 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1477 int old_num_queues = virtio_get_num_queues(vdev);
1478 int new_num_queues = new_max_queues * 2 + 1;
1479 int i;
1481 assert(old_num_queues >= 3);
1482 assert(old_num_queues % 2 == 1);
1484 if (old_num_queues == new_num_queues) {
1485 return;
1489 * We always need to remove and add ctrl vq if
1490 * old_num_queues != new_num_queues. Remove ctrl_vq first,
1491 * and then we only enter one of the following too loops.
1493 virtio_del_queue(vdev, old_num_queues - 1);
1495 for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
1496 /* new_num_queues < old_num_queues */
1497 virtio_net_del_queue(n, i / 2);
1500 for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
1501 /* new_num_queues > old_num_queues */
1502 virtio_net_add_queue(n, i / 2);
1505 /* add ctrl_vq last */
1506 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1509 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
1511 int max = multiqueue ? n->max_queues : 1;
1513 n->multiqueue = multiqueue;
1514 virtio_net_change_num_queues(n, max);
1516 virtio_net_set_queues(n);
1519 static void virtio_net_save_device(VirtIODevice *vdev, QEMUFile *f)
1521 VirtIONet *n = VIRTIO_NET(vdev);
1522 int i;
1524 qemu_put_buffer(f, n->mac, ETH_ALEN);
1525 qemu_put_be32(f, n->vqs[0].tx_waiting);
1526 qemu_put_be32(f, n->mergeable_rx_bufs);
1527 qemu_put_be16(f, n->status);
1528 qemu_put_byte(f, n->promisc);
1529 qemu_put_byte(f, n->allmulti);
1530 qemu_put_be32(f, n->mac_table.in_use);
1531 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
1532 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1533 qemu_put_be32(f, n->has_vnet_hdr);
1534 qemu_put_byte(f, n->mac_table.multi_overflow);
1535 qemu_put_byte(f, n->mac_table.uni_overflow);
1536 qemu_put_byte(f, n->alluni);
1537 qemu_put_byte(f, n->nomulti);
1538 qemu_put_byte(f, n->nouni);
1539 qemu_put_byte(f, n->nobcast);
1540 qemu_put_byte(f, n->has_ufo);
1541 if (n->max_queues > 1) {
1542 qemu_put_be16(f, n->max_queues);
1543 qemu_put_be16(f, n->curr_queues);
1544 for (i = 1; i < n->curr_queues; i++) {
1545 qemu_put_be32(f, n->vqs[i].tx_waiting);
1549 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1550 qemu_put_be64(f, n->curr_guest_offloads);
1554 static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
1555 int version_id)
1557 VirtIONet *n = VIRTIO_NET(vdev);
1558 int i, link_down;
1560 qemu_get_buffer(f, n->mac, ETH_ALEN);
1561 n->vqs[0].tx_waiting = qemu_get_be32(f);
1563 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f),
1564 virtio_vdev_has_feature(vdev,
1565 VIRTIO_F_VERSION_1));
1567 n->status = qemu_get_be16(f);
1569 n->promisc = qemu_get_byte(f);
1570 n->allmulti = qemu_get_byte(f);
1572 n->mac_table.in_use = qemu_get_be32(f);
1573 /* MAC_TABLE_ENTRIES may be different from the saved image */
1574 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
1575 qemu_get_buffer(f, n->mac_table.macs,
1576 n->mac_table.in_use * ETH_ALEN);
1577 } else {
1578 int64_t i;
1580 /* Overflow detected - can happen if source has a larger MAC table.
1581 * We simply set overflow flag so there's no need to maintain the
1582 * table of addresses, discard them all.
1583 * Note: 64 bit math to avoid integer overflow.
1585 for (i = 0; i < (int64_t)n->mac_table.in_use * ETH_ALEN; ++i) {
1586 qemu_get_byte(f);
1588 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
1589 n->mac_table.in_use = 0;
1592 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1594 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
1595 error_report("virtio-net: saved image requires vnet_hdr=on");
1596 return -1;
1599 n->mac_table.multi_overflow = qemu_get_byte(f);
1600 n->mac_table.uni_overflow = qemu_get_byte(f);
1602 n->alluni = qemu_get_byte(f);
1603 n->nomulti = qemu_get_byte(f);
1604 n->nouni = qemu_get_byte(f);
1605 n->nobcast = qemu_get_byte(f);
1607 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
1608 error_report("virtio-net: saved image requires TUN_F_UFO support");
1609 return -1;
1612 if (n->max_queues > 1) {
1613 if (n->max_queues != qemu_get_be16(f)) {
1614 error_report("virtio-net: different max_queues ");
1615 return -1;
1618 n->curr_queues = qemu_get_be16(f);
1619 if (n->curr_queues > n->max_queues) {
1620 error_report("virtio-net: curr_queues %x > max_queues %x",
1621 n->curr_queues, n->max_queues);
1622 return -1;
1624 for (i = 1; i < n->curr_queues; i++) {
1625 n->vqs[i].tx_waiting = qemu_get_be32(f);
1629 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1630 n->curr_guest_offloads = qemu_get_be64(f);
1631 } else {
1632 n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
1635 if (peer_has_vnet_hdr(n)) {
1636 virtio_net_apply_guest_offloads(n);
1639 virtio_net_set_queues(n);
1641 /* Find the first multicast entry in the saved MAC filter */
1642 for (i = 0; i < n->mac_table.in_use; i++) {
1643 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1644 break;
1647 n->mac_table.first_multi = i;
1649 /* nc.link_down can't be migrated, so infer link_down according
1650 * to link status bit in n->status */
1651 link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
1652 for (i = 0; i < n->max_queues; i++) {
1653 qemu_get_subqueue(n->nic, i)->link_down = link_down;
1656 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
1657 virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
1658 n->announce_counter = SELF_ANNOUNCE_ROUNDS;
1659 timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL));
1662 return 0;
1665 static NetClientInfo net_virtio_info = {
1666 .type = NET_CLIENT_DRIVER_NIC,
1667 .size = sizeof(NICState),
1668 .can_receive = virtio_net_can_receive,
1669 .receive = virtio_net_receive,
1670 .link_status_changed = virtio_net_set_link_status,
1671 .query_rx_filter = virtio_net_query_rxfilter,
1674 static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1676 VirtIONet *n = VIRTIO_NET(vdev);
1677 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1678 assert(n->vhost_started);
1679 return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
1682 static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1683 bool mask)
1685 VirtIONet *n = VIRTIO_NET(vdev);
1686 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1687 assert(n->vhost_started);
1688 vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
1689 vdev, idx, mask);
1692 static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
1694 int i, config_size = 0;
1695 virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
1696 for (i = 0; feature_sizes[i].flags != 0; i++) {
1697 if (host_features & feature_sizes[i].flags) {
1698 config_size = MAX(feature_sizes[i].end, config_size);
1701 n->config_size = config_size;
1704 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
1705 const char *type)
1708 * The name can be NULL, the netclient name will be type.x.
1710 assert(type != NULL);
1712 g_free(n->netclient_name);
1713 g_free(n->netclient_type);
1714 n->netclient_name = g_strdup(name);
1715 n->netclient_type = g_strdup(type);
1718 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
1720 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1721 VirtIONet *n = VIRTIO_NET(dev);
1722 NetClientState *nc;
1723 int i;
1725 virtio_net_set_config_size(n, n->host_features);
1726 virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
1729 * We set a lower limit on RX queue size to what it always was.
1730 * Guests that want a smaller ring can always resize it without
1731 * help from us (using virtio 1 and up).
1733 if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
1734 n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
1735 (n->net_conf.rx_queue_size & (n->net_conf.rx_queue_size - 1))) {
1736 error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
1737 "must be a power of 2 between %d and %d.",
1738 n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
1739 VIRTQUEUE_MAX_SIZE);
1740 virtio_cleanup(vdev);
1741 return;
1744 n->max_queues = MAX(n->nic_conf.peers.queues, 1);
1745 if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
1746 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
1747 "must be a positive integer less than %d.",
1748 n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
1749 virtio_cleanup(vdev);
1750 return;
1752 n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
1753 n->curr_queues = 1;
1754 n->tx_timeout = n->net_conf.txtimer;
1756 if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
1757 && strcmp(n->net_conf.tx, "bh")) {
1758 error_report("virtio-net: "
1759 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1760 n->net_conf.tx);
1761 error_report("Defaulting to \"bh\"");
1764 for (i = 0; i < n->max_queues; i++) {
1765 virtio_net_add_queue(n, i);
1768 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1769 qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
1770 memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
1771 n->status = VIRTIO_NET_S_LINK_UP;
1772 n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1773 virtio_net_announce_timer, n);
1775 if (n->netclient_type) {
1777 * Happen when virtio_net_set_netclient_name has been called.
1779 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1780 n->netclient_type, n->netclient_name, n);
1781 } else {
1782 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1783 object_get_typename(OBJECT(dev)), dev->id, n);
1786 peer_test_vnet_hdr(n);
1787 if (peer_has_vnet_hdr(n)) {
1788 for (i = 0; i < n->max_queues; i++) {
1789 qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
1791 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1792 } else {
1793 n->host_hdr_len = 0;
1796 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
1798 n->vqs[0].tx_waiting = 0;
1799 n->tx_burst = n->net_conf.txburst;
1800 virtio_net_set_mrg_rx_bufs(n, 0, 0);
1801 n->promisc = 1; /* for compatibility */
1803 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1805 n->vlans = g_malloc0(MAX_VLAN >> 3);
1807 nc = qemu_get_queue(n->nic);
1808 nc->rxfilter_notify_enabled = 1;
1810 n->qdev = dev;
1813 static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
1815 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1816 VirtIONet *n = VIRTIO_NET(dev);
1817 int i, max_queues;
1819 /* This will stop vhost backend if appropriate. */
1820 virtio_net_set_status(vdev, 0);
1822 g_free(n->netclient_name);
1823 n->netclient_name = NULL;
1824 g_free(n->netclient_type);
1825 n->netclient_type = NULL;
1827 g_free(n->mac_table.macs);
1828 g_free(n->vlans);
1830 max_queues = n->multiqueue ? n->max_queues : 1;
1831 for (i = 0; i < max_queues; i++) {
1832 virtio_net_del_queue(n, i);
1835 timer_del(n->announce_timer);
1836 timer_free(n->announce_timer);
1837 g_free(n->vqs);
1838 qemu_del_nic(n->nic);
1839 virtio_cleanup(vdev);
1842 static void virtio_net_instance_init(Object *obj)
1844 VirtIONet *n = VIRTIO_NET(obj);
1847 * The default config_size is sizeof(struct virtio_net_config).
1848 * Can be overriden with virtio_net_set_config_size.
1850 n->config_size = sizeof(struct virtio_net_config);
1851 device_add_bootindex_property(obj, &n->nic_conf.bootindex,
1852 "bootindex", "/ethernet-phy@0",
1853 DEVICE(n), NULL);
1856 static void virtio_net_pre_save(void *opaque)
1858 VirtIONet *n = opaque;
1860 /* At this point, backend must be stopped, otherwise
1861 * it might keep writing to memory. */
1862 assert(!n->vhost_started);
1865 static const VMStateDescription vmstate_virtio_net = {
1866 .name = "virtio-net",
1867 .minimum_version_id = VIRTIO_NET_VM_VERSION,
1868 .version_id = VIRTIO_NET_VM_VERSION,
1869 .fields = (VMStateField[]) {
1870 VMSTATE_VIRTIO_DEVICE,
1871 VMSTATE_END_OF_LIST()
1873 .pre_save = virtio_net_pre_save,
1876 static Property virtio_net_properties[] = {
1877 DEFINE_PROP_BIT("csum", VirtIONet, host_features, VIRTIO_NET_F_CSUM, true),
1878 DEFINE_PROP_BIT("guest_csum", VirtIONet, host_features,
1879 VIRTIO_NET_F_GUEST_CSUM, true),
1880 DEFINE_PROP_BIT("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
1881 DEFINE_PROP_BIT("guest_tso4", VirtIONet, host_features,
1882 VIRTIO_NET_F_GUEST_TSO4, true),
1883 DEFINE_PROP_BIT("guest_tso6", VirtIONet, host_features,
1884 VIRTIO_NET_F_GUEST_TSO6, true),
1885 DEFINE_PROP_BIT("guest_ecn", VirtIONet, host_features,
1886 VIRTIO_NET_F_GUEST_ECN, true),
1887 DEFINE_PROP_BIT("guest_ufo", VirtIONet, host_features,
1888 VIRTIO_NET_F_GUEST_UFO, true),
1889 DEFINE_PROP_BIT("guest_announce", VirtIONet, host_features,
1890 VIRTIO_NET_F_GUEST_ANNOUNCE, true),
1891 DEFINE_PROP_BIT("host_tso4", VirtIONet, host_features,
1892 VIRTIO_NET_F_HOST_TSO4, true),
1893 DEFINE_PROP_BIT("host_tso6", VirtIONet, host_features,
1894 VIRTIO_NET_F_HOST_TSO6, true),
1895 DEFINE_PROP_BIT("host_ecn", VirtIONet, host_features,
1896 VIRTIO_NET_F_HOST_ECN, true),
1897 DEFINE_PROP_BIT("host_ufo", VirtIONet, host_features,
1898 VIRTIO_NET_F_HOST_UFO, true),
1899 DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet, host_features,
1900 VIRTIO_NET_F_MRG_RXBUF, true),
1901 DEFINE_PROP_BIT("status", VirtIONet, host_features,
1902 VIRTIO_NET_F_STATUS, true),
1903 DEFINE_PROP_BIT("ctrl_vq", VirtIONet, host_features,
1904 VIRTIO_NET_F_CTRL_VQ, true),
1905 DEFINE_PROP_BIT("ctrl_rx", VirtIONet, host_features,
1906 VIRTIO_NET_F_CTRL_RX, true),
1907 DEFINE_PROP_BIT("ctrl_vlan", VirtIONet, host_features,
1908 VIRTIO_NET_F_CTRL_VLAN, true),
1909 DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet, host_features,
1910 VIRTIO_NET_F_CTRL_RX_EXTRA, true),
1911 DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet, host_features,
1912 VIRTIO_NET_F_CTRL_MAC_ADDR, true),
1913 DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet, host_features,
1914 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
1915 DEFINE_PROP_BIT("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
1916 DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
1917 DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
1918 TX_TIMER_INTERVAL),
1919 DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
1920 DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
1921 DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
1922 VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
1923 DEFINE_PROP_END_OF_LIST(),
1926 static void virtio_net_class_init(ObjectClass *klass, void *data)
1928 DeviceClass *dc = DEVICE_CLASS(klass);
1929 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1931 dc->props = virtio_net_properties;
1932 dc->vmsd = &vmstate_virtio_net;
1933 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1934 vdc->realize = virtio_net_device_realize;
1935 vdc->unrealize = virtio_net_device_unrealize;
1936 vdc->get_config = virtio_net_get_config;
1937 vdc->set_config = virtio_net_set_config;
1938 vdc->get_features = virtio_net_get_features;
1939 vdc->set_features = virtio_net_set_features;
1940 vdc->bad_features = virtio_net_bad_features;
1941 vdc->reset = virtio_net_reset;
1942 vdc->set_status = virtio_net_set_status;
1943 vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
1944 vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
1945 vdc->load = virtio_net_load_device;
1946 vdc->save = virtio_net_save_device;
1949 static const TypeInfo virtio_net_info = {
1950 .name = TYPE_VIRTIO_NET,
1951 .parent = TYPE_VIRTIO_DEVICE,
1952 .instance_size = sizeof(VirtIONet),
1953 .instance_init = virtio_net_instance_init,
1954 .class_init = virtio_net_class_init,
1957 static void virtio_register_types(void)
1959 type_register_static(&virtio_net_info);
1962 type_init(virtio_register_types)