xref: /qemu/hw/net/virtio-net.c (revision f917eed3)
1 /*
2  * Virtio Network Device
3  *
4  * Copyright IBM, Corp. 2007
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qemu/atomic.h"
16 #include "qemu/iov.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/module.h"
19 #include "hw/virtio/virtio.h"
20 #include "net/net.h"
21 #include "net/checksum.h"
22 #include "net/tap.h"
23 #include "qemu/error-report.h"
24 #include "qemu/timer.h"
25 #include "qemu/option.h"
26 #include "qemu/option_int.h"
27 #include "qemu/config-file.h"
28 #include "qapi/qmp/qdict.h"
29 #include "hw/virtio/virtio-net.h"
30 #include "net/vhost_net.h"
31 #include "net/announce.h"
32 #include "hw/virtio/virtio-bus.h"
33 #include "qapi/error.h"
34 #include "qapi/qapi-events-net.h"
35 #include "hw/qdev-properties.h"
36 #include "qapi/qapi-types-migration.h"
37 #include "qapi/qapi-events-migration.h"
38 #include "hw/virtio/virtio-access.h"
39 #include "migration/misc.h"
40 #include "standard-headers/linux/ethtool.h"
41 #include "sysemu/sysemu.h"
42 #include "trace.h"
43 #include "monitor/qdev.h"
44 #include "hw/pci/pci.h"
45 #include "net_rx_pkt.h"
46 #include "hw/virtio/vhost.h"
47 
48 #define VIRTIO_NET_VM_VERSION    11
49 
50 #define MAC_TABLE_ENTRIES    64
51 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
52 
53 /* previously fixed value */
54 #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
55 #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256
56 
57 /* for now, only allow larger queues; with virtio-1, guest can downsize */
58 #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
59 #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
60 
61 #define VIRTIO_NET_IP4_ADDR_SIZE   8        /* ipv4 saddr + daddr */
62 
63 #define VIRTIO_NET_TCP_FLAG         0x3F
64 #define VIRTIO_NET_TCP_HDR_LENGTH   0xF000
65 
66 /* IPv4 max payload, 16 bits in the header */
67 #define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header))
68 #define VIRTIO_NET_MAX_TCP_PAYLOAD 65535
69 
70 /* header length value in ip header without option */
71 #define VIRTIO_NET_IP4_HEADER_LENGTH 5
72 
73 #define VIRTIO_NET_IP6_ADDR_SIZE   32      /* ipv6 saddr + daddr */
74 #define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD
75 
76 /* Purge coalesced packets timer interval, This value affects the performance
77    a lot, and should be tuned carefully, '300000'(300us) is the recommended
78    value to pass the WHQL test, '50000' can gain 2x netperf throughput with
79    tso/gso/gro 'off'. */
80 #define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000
81 
82 #define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \
83                                          VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \
84                                          VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \
85                                          VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \
86                                          VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \
87                                          VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \
88                                          VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \
89                                          VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \
90                                          VIRTIO_NET_RSS_HASH_TYPE_UDP_EX)
91 
92 static VirtIOFeature feature_sizes[] = {
93     {.flags = 1ULL << VIRTIO_NET_F_MAC,
94      .end = endof(struct virtio_net_config, mac)},
95     {.flags = 1ULL << VIRTIO_NET_F_STATUS,
96      .end = endof(struct virtio_net_config, status)},
97     {.flags = 1ULL << VIRTIO_NET_F_MQ,
98      .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
99     {.flags = 1ULL << VIRTIO_NET_F_MTU,
100      .end = endof(struct virtio_net_config, mtu)},
101     {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX,
102      .end = endof(struct virtio_net_config, duplex)},
103     {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT),
104      .end = endof(struct virtio_net_config, supported_hash_types)},
105     {}
106 };
107 
108 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
109 {
110     VirtIONet *n = qemu_get_nic_opaque(nc);
111 
112     return &n->vqs[nc->queue_index];
113 }
114 
115 static int vq2q(int queue_index)
116 {
117     return queue_index / 2;
118 }
119 
120 /* TODO
121  * - we could suppress RX interrupt if we were so inclined.
122  */
123 
124 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
125 {
126     VirtIONet *n = VIRTIO_NET(vdev);
127     struct virtio_net_config netcfg;
128     NetClientState *nc = qemu_get_queue(n->nic);
129 
130     int ret = 0;
131     memset(&netcfg, 0 , sizeof(struct virtio_net_config));
132     virtio_stw_p(vdev, &netcfg.status, n->status);
133     virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
134     virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu);
135     memcpy(netcfg.mac, n->mac, ETH_ALEN);
136     virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed);
137     netcfg.duplex = n->net_conf.duplex;
138     netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE;
139     virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length,
140                  virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ?
141                  VIRTIO_NET_RSS_MAX_TABLE_LEN : 1);
142     virtio_stl_p(vdev, &netcfg.supported_hash_types,
143                  VIRTIO_NET_RSS_SUPPORTED_HASHES);
144     memcpy(config, &netcfg, n->config_size);
145 
146     /*
147      * Is this VDPA? No peer means not VDPA: there's no way to
148      * disconnect/reconnect a VDPA peer.
149      */
150     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
151         ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
152                                    n->config_size);
153         if (ret != -1) {
154             memcpy(config, &netcfg, n->config_size);
155         }
156     }
157 }
158 
159 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
160 {
161     VirtIONet *n = VIRTIO_NET(vdev);
162     struct virtio_net_config netcfg = {};
163     NetClientState *nc = qemu_get_queue(n->nic);
164 
165     memcpy(&netcfg, config, n->config_size);
166 
167     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
168         !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
169         memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
170         memcpy(n->mac, netcfg.mac, ETH_ALEN);
171         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
172     }
173 
174     /*
175      * Is this VDPA? No peer means not VDPA: there's no way to
176      * disconnect/reconnect a VDPA peer.
177      */
178     if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
179         vhost_net_set_config(get_vhost_net(nc->peer),
180                              (uint8_t *)&netcfg, 0, n->config_size,
181                              VHOST_SET_CONFIG_TYPE_MASTER);
182       }
183 }
184 
185 static bool virtio_net_started(VirtIONet *n, uint8_t status)
186 {
187     VirtIODevice *vdev = VIRTIO_DEVICE(n);
188     return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
189         (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
190 }
191 
192 static void virtio_net_announce_notify(VirtIONet *net)
193 {
194     VirtIODevice *vdev = VIRTIO_DEVICE(net);
195     trace_virtio_net_announce_notify();
196 
197     net->status |= VIRTIO_NET_S_ANNOUNCE;
198     virtio_notify_config(vdev);
199 }
200 
201 static void virtio_net_announce_timer(void *opaque)
202 {
203     VirtIONet *n = opaque;
204     trace_virtio_net_announce_timer(n->announce_timer.round);
205 
206     n->announce_timer.round--;
207     virtio_net_announce_notify(n);
208 }
209 
210 static void virtio_net_announce(NetClientState *nc)
211 {
212     VirtIONet *n = qemu_get_nic_opaque(nc);
213     VirtIODevice *vdev = VIRTIO_DEVICE(n);
214 
215     /*
216      * Make sure the virtio migration announcement timer isn't running
217      * If it is, let it trigger announcement so that we do not cause
218      * confusion.
219      */
220     if (n->announce_timer.round) {
221         return;
222     }
223 
224     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
225         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
226             virtio_net_announce_notify(n);
227     }
228 }
229 
230 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
231 {
232     VirtIODevice *vdev = VIRTIO_DEVICE(n);
233     NetClientState *nc = qemu_get_queue(n->nic);
234     int queues = n->multiqueue ? n->max_queues : 1;
235 
236     if (!get_vhost_net(nc->peer)) {
237         return;
238     }
239 
240     if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
241         !!n->vhost_started) {
242         return;
243     }
244     if (!n->vhost_started) {
245         int r, i;
246 
247         if (n->needs_vnet_hdr_swap) {
248             error_report("backend does not support %s vnet headers; "
249                          "falling back on userspace virtio",
250                          virtio_is_big_endian(vdev) ? "BE" : "LE");
251             return;
252         }
253 
254         /* Any packets outstanding? Purge them to avoid touching rings
255          * when vhost is running.
256          */
257         for (i = 0;  i < queues; i++) {
258             NetClientState *qnc = qemu_get_subqueue(n->nic, i);
259 
260             /* Purge both directions: TX and RX. */
261             qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
262             qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
263         }
264 
265         if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) {
266             r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu);
267             if (r < 0) {
268                 error_report("%uBytes MTU not supported by the backend",
269                              n->net_conf.mtu);
270 
271                 return;
272             }
273         }
274 
275         n->vhost_started = 1;
276         r = vhost_net_start(vdev, n->nic->ncs, queues);
277         if (r < 0) {
278             error_report("unable to start vhost net: %d: "
279                          "falling back on userspace virtio", -r);
280             n->vhost_started = 0;
281         }
282     } else {
283         vhost_net_stop(vdev, n->nic->ncs, queues);
284         n->vhost_started = 0;
285     }
286 }
287 
288 static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
289                                           NetClientState *peer,
290                                           bool enable)
291 {
292     if (virtio_is_big_endian(vdev)) {
293         return qemu_set_vnet_be(peer, enable);
294     } else {
295         return qemu_set_vnet_le(peer, enable);
296     }
297 }
298 
299 static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
300                                        int queues, bool enable)
301 {
302     int i;
303 
304     for (i = 0; i < queues; i++) {
305         if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
306             enable) {
307             while (--i >= 0) {
308                 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
309             }
310 
311             return true;
312         }
313     }
314 
315     return false;
316 }
317 
318 static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
319 {
320     VirtIODevice *vdev = VIRTIO_DEVICE(n);
321     int queues = n->multiqueue ? n->max_queues : 1;
322 
323     if (virtio_net_started(n, status)) {
324         /* Before using the device, we tell the network backend about the
325          * endianness to use when parsing vnet headers. If the backend
326          * can't do it, we fallback onto fixing the headers in the core
327          * virtio-net code.
328          */
329         n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
330                                                             queues, true);
331     } else if (virtio_net_started(n, vdev->status)) {
332         /* After using the device, we need to reset the network backend to
333          * the default (guest native endianness), otherwise the guest may
334          * lose network connectivity if it is rebooted into a different
335          * endianness.
336          */
337         virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false);
338     }
339 }
340 
341 static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
342 {
343     unsigned int dropped = virtqueue_drop_all(vq);
344     if (dropped) {
345         virtio_notify(vdev, vq);
346     }
347 }
348 
349 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
350 {
351     VirtIONet *n = VIRTIO_NET(vdev);
352     VirtIONetQueue *q;
353     int i;
354     uint8_t queue_status;
355 
356     virtio_net_vnet_endian_status(n, status);
357     virtio_net_vhost_status(n, status);
358 
359     for (i = 0; i < n->max_queues; i++) {
360         NetClientState *ncs = qemu_get_subqueue(n->nic, i);
361         bool queue_started;
362         q = &n->vqs[i];
363 
364         if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
365             queue_status = 0;
366         } else {
367             queue_status = status;
368         }
369         queue_started =
370             virtio_net_started(n, queue_status) && !n->vhost_started;
371 
372         if (queue_started) {
373             qemu_flush_queued_packets(ncs);
374         }
375 
376         if (!q->tx_waiting) {
377             continue;
378         }
379 
380         if (queue_started) {
381             if (q->tx_timer) {
382                 timer_mod(q->tx_timer,
383                                qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
384             } else {
385                 qemu_bh_schedule(q->tx_bh);
386             }
387         } else {
388             if (q->tx_timer) {
389                 timer_del(q->tx_timer);
390             } else {
391                 qemu_bh_cancel(q->tx_bh);
392             }
393             if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 &&
394                 (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) &&
395                 vdev->vm_running) {
396                 /* if tx is waiting we are likely have some packets in tx queue
397                  * and disabled notification */
398                 q->tx_waiting = 0;
399                 virtio_queue_set_notification(q->tx_vq, 1);
400                 virtio_net_drop_tx_queue_data(vdev, q->tx_vq);
401             }
402         }
403     }
404 }
405 
406 static void virtio_net_set_link_status(NetClientState *nc)
407 {
408     VirtIONet *n = qemu_get_nic_opaque(nc);
409     VirtIODevice *vdev = VIRTIO_DEVICE(n);
410     uint16_t old_status = n->status;
411 
412     if (nc->link_down)
413         n->status &= ~VIRTIO_NET_S_LINK_UP;
414     else
415         n->status |= VIRTIO_NET_S_LINK_UP;
416 
417     if (n->status != old_status)
418         virtio_notify_config(vdev);
419 
420     virtio_net_set_status(vdev, vdev->status);
421 }
422 
423 static void rxfilter_notify(NetClientState *nc)
424 {
425     VirtIONet *n = qemu_get_nic_opaque(nc);
426 
427     if (nc->rxfilter_notify_enabled) {
428         char *path = object_get_canonical_path(OBJECT(n->qdev));
429         qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
430                                               n->netclient_name, path);
431         g_free(path);
432 
433         /* disable event notification to avoid events flooding */
434         nc->rxfilter_notify_enabled = 0;
435     }
436 }
437 
438 static intList *get_vlan_table(VirtIONet *n)
439 {
440     intList *list;
441     int i, j;
442 
443     list = NULL;
444     for (i = 0; i < MAX_VLAN >> 5; i++) {
445         for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
446             if (n->vlans[i] & (1U << j)) {
447                 QAPI_LIST_PREPEND(list, (i << 5) + j);
448             }
449         }
450     }
451 
452     return list;
453 }
454 
455 static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
456 {
457     VirtIONet *n = qemu_get_nic_opaque(nc);
458     VirtIODevice *vdev = VIRTIO_DEVICE(n);
459     RxFilterInfo *info;
460     strList *str_list;
461     int i;
462 
463     info = g_malloc0(sizeof(*info));
464     info->name = g_strdup(nc->name);
465     info->promiscuous = n->promisc;
466 
467     if (n->nouni) {
468         info->unicast = RX_STATE_NONE;
469     } else if (n->alluni) {
470         info->unicast = RX_STATE_ALL;
471     } else {
472         info->unicast = RX_STATE_NORMAL;
473     }
474 
475     if (n->nomulti) {
476         info->multicast = RX_STATE_NONE;
477     } else if (n->allmulti) {
478         info->multicast = RX_STATE_ALL;
479     } else {
480         info->multicast = RX_STATE_NORMAL;
481     }
482 
483     info->broadcast_allowed = n->nobcast;
484     info->multicast_overflow = n->mac_table.multi_overflow;
485     info->unicast_overflow = n->mac_table.uni_overflow;
486 
487     info->main_mac = qemu_mac_strdup_printf(n->mac);
488 
489     str_list = NULL;
490     for (i = 0; i < n->mac_table.first_multi; i++) {
491         QAPI_LIST_PREPEND(str_list,
492                       qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
493     }
494     info->unicast_table = str_list;
495 
496     str_list = NULL;
497     for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
498         QAPI_LIST_PREPEND(str_list,
499                       qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
500     }
501     info->multicast_table = str_list;
502     info->vlan_table = get_vlan_table(n);
503 
504     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
505         info->vlan = RX_STATE_ALL;
506     } else if (!info->vlan_table) {
507         info->vlan = RX_STATE_NONE;
508     } else {
509         info->vlan = RX_STATE_NORMAL;
510     }
511 
512     /* enable event notification after query */
513     nc->rxfilter_notify_enabled = 1;
514 
515     return info;
516 }
517 
518 static void virtio_net_reset(VirtIODevice *vdev)
519 {
520     VirtIONet *n = VIRTIO_NET(vdev);
521     int i;
522 
523     /* Reset back to compatibility mode */
524     n->promisc = 1;
525     n->allmulti = 0;
526     n->alluni = 0;
527     n->nomulti = 0;
528     n->nouni = 0;
529     n->nobcast = 0;
530     /* multiqueue is disabled by default */
531     n->curr_queues = 1;
532     timer_del(n->announce_timer.tm);
533     n->announce_timer.round = 0;
534     n->status &= ~VIRTIO_NET_S_ANNOUNCE;
535 
536     /* Flush any MAC and VLAN filter table state */
537     n->mac_table.in_use = 0;
538     n->mac_table.first_multi = 0;
539     n->mac_table.multi_overflow = 0;
540     n->mac_table.uni_overflow = 0;
541     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
542     memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
543     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
544     memset(n->vlans, 0, MAX_VLAN >> 3);
545 
546     /* Flush any async TX */
547     for (i = 0;  i < n->max_queues; i++) {
548         NetClientState *nc = qemu_get_subqueue(n->nic, i);
549 
550         if (nc->peer) {
551             qemu_flush_or_purge_queued_packets(nc->peer, true);
552             assert(!virtio_net_get_subqueue(nc)->async_tx.elem);
553         }
554     }
555 }
556 
557 static void peer_test_vnet_hdr(VirtIONet *n)
558 {
559     NetClientState *nc = qemu_get_queue(n->nic);
560     if (!nc->peer) {
561         return;
562     }
563 
564     n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
565 }
566 
567 static int peer_has_vnet_hdr(VirtIONet *n)
568 {
569     return n->has_vnet_hdr;
570 }
571 
572 static int peer_has_ufo(VirtIONet *n)
573 {
574     if (!peer_has_vnet_hdr(n))
575         return 0;
576 
577     n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
578 
579     return n->has_ufo;
580 }
581 
582 static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
583                                        int version_1, int hash_report)
584 {
585     int i;
586     NetClientState *nc;
587 
588     n->mergeable_rx_bufs = mergeable_rx_bufs;
589 
590     if (version_1) {
591         n->guest_hdr_len = hash_report ?
592             sizeof(struct virtio_net_hdr_v1_hash) :
593             sizeof(struct virtio_net_hdr_mrg_rxbuf);
594         n->rss_data.populate_hash = !!hash_report;
595     } else {
596         n->guest_hdr_len = n->mergeable_rx_bufs ?
597             sizeof(struct virtio_net_hdr_mrg_rxbuf) :
598             sizeof(struct virtio_net_hdr);
599     }
600 
601     for (i = 0; i < n->max_queues; i++) {
602         nc = qemu_get_subqueue(n->nic, i);
603 
604         if (peer_has_vnet_hdr(n) &&
605             qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
606             qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
607             n->host_hdr_len = n->guest_hdr_len;
608         }
609     }
610 }
611 
612 static int virtio_net_max_tx_queue_size(VirtIONet *n)
613 {
614     NetClientState *peer = n->nic_conf.peers.ncs[0];
615 
616     /*
617      * Backends other than vhost-user don't support max queue size.
618      */
619     if (!peer) {
620         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
621     }
622 
623     if (peer->info->type != NET_CLIENT_DRIVER_VHOST_USER) {
624         return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
625     }
626 
627     return VIRTQUEUE_MAX_SIZE;
628 }
629 
630 static int peer_attach(VirtIONet *n, int index)
631 {
632     NetClientState *nc = qemu_get_subqueue(n->nic, index);
633 
634     if (!nc->peer) {
635         return 0;
636     }
637 
638     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
639         vhost_set_vring_enable(nc->peer, 1);
640     }
641 
642     if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
643         return 0;
644     }
645 
646     if (n->max_queues == 1) {
647         return 0;
648     }
649 
650     return tap_enable(nc->peer);
651 }
652 
653 static int peer_detach(VirtIONet *n, int index)
654 {
655     NetClientState *nc = qemu_get_subqueue(n->nic, index);
656 
657     if (!nc->peer) {
658         return 0;
659     }
660 
661     if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
662         vhost_set_vring_enable(nc->peer, 0);
663     }
664 
665     if (nc->peer->info->type !=  NET_CLIENT_DRIVER_TAP) {
666         return 0;
667     }
668 
669     return tap_disable(nc->peer);
670 }
671 
672 static void virtio_net_set_queues(VirtIONet *n)
673 {
674     int i;
675     int r;
676 
677     if (n->nic->peer_deleted) {
678         return;
679     }
680 
681     for (i = 0; i < n->max_queues; i++) {
682         if (i < n->curr_queues) {
683             r = peer_attach(n, i);
684             assert(!r);
685         } else {
686             r = peer_detach(n, i);
687             assert(!r);
688         }
689     }
690 }
691 
692 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
693 
694 static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
695                                         Error **errp)
696 {
697     VirtIONet *n = VIRTIO_NET(vdev);
698     NetClientState *nc = qemu_get_queue(n->nic);
699 
700     /* Firstly sync all virtio-net possible supported features */
701     features |= n->host_features;
702 
703     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
704 
705     if (!peer_has_vnet_hdr(n)) {
706         virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
707         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
708         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
709         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
710 
711         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
712         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
713         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
714         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
715 
716         virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
717     }
718 
719     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
720         virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
721         virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
722     }
723 
724     if (!get_vhost_net(nc->peer)) {
725         return features;
726     }
727 
728     virtio_clear_feature(&features, VIRTIO_NET_F_RSS);
729     virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
730     features = vhost_net_get_features(get_vhost_net(nc->peer), features);
731     vdev->backend_features = features;
732 
733     if (n->mtu_bypass_backend &&
734             (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) {
735         features |= (1ULL << VIRTIO_NET_F_MTU);
736     }
737 
738     return features;
739 }
740 
741 static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
742 {
743     uint64_t features = 0;
744 
745     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
746      * but also these: */
747     virtio_add_feature(&features, VIRTIO_NET_F_MAC);
748     virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
749     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
750     virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
751     virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
752 
753     return features;
754 }
755 
756 static void virtio_net_apply_guest_offloads(VirtIONet *n)
757 {
758     qemu_set_offload(qemu_get_queue(n->nic)->peer,
759             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
760             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
761             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
762             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
763             !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
764 }
765 
766 static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
767 {
768     static const uint64_t guest_offloads_mask =
769         (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
770         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
771         (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
772         (1ULL << VIRTIO_NET_F_GUEST_ECN)  |
773         (1ULL << VIRTIO_NET_F_GUEST_UFO);
774 
775     return guest_offloads_mask & features;
776 }
777 
778 static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
779 {
780     VirtIODevice *vdev = VIRTIO_DEVICE(n);
781     return virtio_net_guest_offloads_by_features(vdev->guest_features);
782 }
783 
784 typedef struct {
785     VirtIONet *n;
786     char *id;
787 } FailoverId;
788 
789 /**
790  * Set the id of the failover primary device
791  *
792  * @opaque: FailoverId to setup
793  * @opts: opts for device we are handling
794  * @errp: returns an error if this function fails
795  */
796 static int failover_set_primary(void *opaque, QemuOpts *opts, Error **errp)
797 {
798     FailoverId *fid = opaque;
799     const char *standby_id = qemu_opt_get(opts, "failover_pair_id");
800 
801     if (g_strcmp0(standby_id, fid->n->netclient_name) == 0) {
802         fid->id = g_strdup(opts->id);
803         return 1;
804     }
805 
806     return 0;
807 }
808 
809 /**
810  * Find the primary device id for this failover virtio-net
811  *
812  * @n: VirtIONet device
813  * @errp: returns an error if this function fails
814  */
815 static char *failover_find_primary_device_id(VirtIONet *n)
816 {
817     Error *err = NULL;
818     FailoverId fid;
819 
820     fid.n = n;
821     if (!qemu_opts_foreach(qemu_find_opts("device"),
822                            failover_set_primary, &fid, &err)) {
823         return NULL;
824     }
825     return fid.id;
826 }
827 
828 /**
829  * Find the primary device for this failover virtio-net
830  *
831  * @n: VirtIONet device
832  * @errp: returns an error if this function fails
833  */
834 static DeviceState *failover_find_primary_device(VirtIONet *n)
835 {
836     char *id = failover_find_primary_device_id(n);
837 
838     if (!id) {
839         return NULL;
840     }
841 
842     return qdev_find_recursive(sysbus_get_default(), id);
843 }
844 
845 static void failover_add_primary(VirtIONet *n, Error **errp)
846 {
847     Error *err = NULL;
848     QemuOpts *opts;
849     char *id;
850     DeviceState *dev = failover_find_primary_device(n);
851 
852     if (dev) {
853         return;
854     }
855 
856     id = failover_find_primary_device_id(n);
857     if (!id) {
858         return;
859     }
860     opts = qemu_opts_find(qemu_find_opts("device"), id);
861     if (opts) {
862         dev = qdev_device_add(opts, &err);
863         if (err) {
864             qemu_opts_del(opts);
865         }
866     } else {
867         error_setg(errp, "Primary device not found");
868         error_append_hint(errp, "Virtio-net failover will not work. Make "
869                           "sure primary device has parameter"
870                           " failover_pair_id=<virtio-net-id>\n");
871     }
872     error_propagate(errp, err);
873 }
874 
875 static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
876 {
877     VirtIONet *n = VIRTIO_NET(vdev);
878     Error *err = NULL;
879     int i;
880 
881     if (n->mtu_bypass_backend &&
882             !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
883         features &= ~(1ULL << VIRTIO_NET_F_MTU);
884     }
885 
886     virtio_net_set_multiqueue(n,
887                               virtio_has_feature(features, VIRTIO_NET_F_RSS) ||
888                               virtio_has_feature(features, VIRTIO_NET_F_MQ));
889 
890     virtio_net_set_mrg_rx_bufs(n,
891                                virtio_has_feature(features,
892                                                   VIRTIO_NET_F_MRG_RXBUF),
893                                virtio_has_feature(features,
894                                                   VIRTIO_F_VERSION_1),
895                                virtio_has_feature(features,
896                                                   VIRTIO_NET_F_HASH_REPORT));
897 
898     n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
899         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4);
900     n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
901         virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6);
902     n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS);
903 
904     if (n->has_vnet_hdr) {
905         n->curr_guest_offloads =
906             virtio_net_guest_offloads_by_features(features);
907         virtio_net_apply_guest_offloads(n);
908     }
909 
910     for (i = 0;  i < n->max_queues; i++) {
911         NetClientState *nc = qemu_get_subqueue(n->nic, i);
912 
913         if (!get_vhost_net(nc->peer)) {
914             continue;
915         }
916         vhost_net_ack_features(get_vhost_net(nc->peer), features);
917     }
918 
919     if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
920         memset(n->vlans, 0, MAX_VLAN >> 3);
921     } else {
922         memset(n->vlans, 0xff, MAX_VLAN >> 3);
923     }
924 
925     if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) {
926         qapi_event_send_failover_negotiated(n->netclient_name);
927         qatomic_set(&n->failover_primary_hidden, false);
928         failover_add_primary(n, &err);
929         if (err) {
930             warn_report_err(err);
931         }
932     }
933 }
934 
935 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
936                                      struct iovec *iov, unsigned int iov_cnt)
937 {
938     uint8_t on;
939     size_t s;
940     NetClientState *nc = qemu_get_queue(n->nic);
941 
942     s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
943     if (s != sizeof(on)) {
944         return VIRTIO_NET_ERR;
945     }
946 
947     if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
948         n->promisc = on;
949     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
950         n->allmulti = on;
951     } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
952         n->alluni = on;
953     } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
954         n->nomulti = on;
955     } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
956         n->nouni = on;
957     } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
958         n->nobcast = on;
959     } else {
960         return VIRTIO_NET_ERR;
961     }
962 
963     rxfilter_notify(nc);
964 
965     return VIRTIO_NET_OK;
966 }
967 
968 static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
969                                      struct iovec *iov, unsigned int iov_cnt)
970 {
971     VirtIODevice *vdev = VIRTIO_DEVICE(n);
972     uint64_t offloads;
973     size_t s;
974 
975     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
976         return VIRTIO_NET_ERR;
977     }
978 
979     s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
980     if (s != sizeof(offloads)) {
981         return VIRTIO_NET_ERR;
982     }
983 
984     if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
985         uint64_t supported_offloads;
986 
987         offloads = virtio_ldq_p(vdev, &offloads);
988 
989         if (!n->has_vnet_hdr) {
990             return VIRTIO_NET_ERR;
991         }
992 
993         n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
994             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4);
995         n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
996             virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6);
997         virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT);
998 
999         supported_offloads = virtio_net_supported_guest_offloads(n);
1000         if (offloads & ~supported_offloads) {
1001             return VIRTIO_NET_ERR;
1002         }
1003 
1004         n->curr_guest_offloads = offloads;
1005         virtio_net_apply_guest_offloads(n);
1006 
1007         return VIRTIO_NET_OK;
1008     } else {
1009         return VIRTIO_NET_ERR;
1010     }
1011 }
1012 
1013 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
1014                                  struct iovec *iov, unsigned int iov_cnt)
1015 {
1016     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1017     struct virtio_net_ctrl_mac mac_data;
1018     size_t s;
1019     NetClientState *nc = qemu_get_queue(n->nic);
1020 
1021     if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
1022         if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
1023             return VIRTIO_NET_ERR;
1024         }
1025         s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
1026         assert(s == sizeof(n->mac));
1027         qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
1028         rxfilter_notify(nc);
1029 
1030         return VIRTIO_NET_OK;
1031     }
1032 
1033     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
1034         return VIRTIO_NET_ERR;
1035     }
1036 
1037     int in_use = 0;
1038     int first_multi = 0;
1039     uint8_t uni_overflow = 0;
1040     uint8_t multi_overflow = 0;
1041     uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1042 
1043     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
1044                    sizeof(mac_data.entries));
1045     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
1046     if (s != sizeof(mac_data.entries)) {
1047         goto error;
1048     }
1049     iov_discard_front(&iov, &iov_cnt, s);
1050 
1051     if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
1052         goto error;
1053     }
1054 
1055     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
1056         s = iov_to_buf(iov, iov_cnt, 0, macs,
1057                        mac_data.entries * ETH_ALEN);
1058         if (s != mac_data.entries * ETH_ALEN) {
1059             goto error;
1060         }
1061         in_use += mac_data.entries;
1062     } else {
1063         uni_overflow = 1;
1064     }
1065 
1066     iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
1067 
1068     first_multi = in_use;
1069 
1070     s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
1071                    sizeof(mac_data.entries));
1072     mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
1073     if (s != sizeof(mac_data.entries)) {
1074         goto error;
1075     }
1076 
1077     iov_discard_front(&iov, &iov_cnt, s);
1078 
1079     if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
1080         goto error;
1081     }
1082 
1083     if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
1084         s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
1085                        mac_data.entries * ETH_ALEN);
1086         if (s != mac_data.entries * ETH_ALEN) {
1087             goto error;
1088         }
1089         in_use += mac_data.entries;
1090     } else {
1091         multi_overflow = 1;
1092     }
1093 
1094     n->mac_table.in_use = in_use;
1095     n->mac_table.first_multi = first_multi;
1096     n->mac_table.uni_overflow = uni_overflow;
1097     n->mac_table.multi_overflow = multi_overflow;
1098     memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
1099     g_free(macs);
1100     rxfilter_notify(nc);
1101 
1102     return VIRTIO_NET_OK;
1103 
1104 error:
1105     g_free(macs);
1106     return VIRTIO_NET_ERR;
1107 }
1108 
1109 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
1110                                         struct iovec *iov, unsigned int iov_cnt)
1111 {
1112     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1113     uint16_t vid;
1114     size_t s;
1115     NetClientState *nc = qemu_get_queue(n->nic);
1116 
1117     s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
1118     vid = virtio_lduw_p(vdev, &vid);
1119     if (s != sizeof(vid)) {
1120         return VIRTIO_NET_ERR;
1121     }
1122 
1123     if (vid >= MAX_VLAN)
1124         return VIRTIO_NET_ERR;
1125 
1126     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
1127         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
1128     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
1129         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
1130     else
1131         return VIRTIO_NET_ERR;
1132 
1133     rxfilter_notify(nc);
1134 
1135     return VIRTIO_NET_OK;
1136 }
1137 
1138 static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
1139                                       struct iovec *iov, unsigned int iov_cnt)
1140 {
1141     trace_virtio_net_handle_announce(n->announce_timer.round);
1142     if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
1143         n->status & VIRTIO_NET_S_ANNOUNCE) {
1144         n->status &= ~VIRTIO_NET_S_ANNOUNCE;
1145         if (n->announce_timer.round) {
1146             qemu_announce_timer_step(&n->announce_timer);
1147         }
1148         return VIRTIO_NET_OK;
1149     } else {
1150         return VIRTIO_NET_ERR;
1151     }
1152 }
1153 
1154 static void virtio_net_disable_rss(VirtIONet *n)
1155 {
1156     if (n->rss_data.enabled) {
1157         trace_virtio_net_rss_disable();
1158     }
1159     n->rss_data.enabled = false;
1160 }
1161 
1162 static uint16_t virtio_net_handle_rss(VirtIONet *n,
1163                                       struct iovec *iov,
1164                                       unsigned int iov_cnt,
1165                                       bool do_rss)
1166 {
1167     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1168     struct virtio_net_rss_config cfg;
1169     size_t s, offset = 0, size_get;
1170     uint16_t queues, i;
1171     struct {
1172         uint16_t us;
1173         uint8_t b;
1174     } QEMU_PACKED temp;
1175     const char *err_msg = "";
1176     uint32_t err_value = 0;
1177 
1178     if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) {
1179         err_msg = "RSS is not negotiated";
1180         goto error;
1181     }
1182     if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
1183         err_msg = "Hash report is not negotiated";
1184         goto error;
1185     }
1186     size_get = offsetof(struct virtio_net_rss_config, indirection_table);
1187     s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get);
1188     if (s != size_get) {
1189         err_msg = "Short command buffer";
1190         err_value = (uint32_t)s;
1191         goto error;
1192     }
1193     n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types);
1194     n->rss_data.indirections_len =
1195         virtio_lduw_p(vdev, &cfg.indirection_table_mask);
1196     n->rss_data.indirections_len++;
1197     if (!do_rss) {
1198         n->rss_data.indirections_len = 1;
1199     }
1200     if (!is_power_of_2(n->rss_data.indirections_len)) {
1201         err_msg = "Invalid size of indirection table";
1202         err_value = n->rss_data.indirections_len;
1203         goto error;
1204     }
1205     if (n->rss_data.indirections_len > VIRTIO_NET_RSS_MAX_TABLE_LEN) {
1206         err_msg = "Too large indirection table";
1207         err_value = n->rss_data.indirections_len;
1208         goto error;
1209     }
1210     n->rss_data.default_queue = do_rss ?
1211         virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0;
1212     if (n->rss_data.default_queue >= n->max_queues) {
1213         err_msg = "Invalid default queue";
1214         err_value = n->rss_data.default_queue;
1215         goto error;
1216     }
1217     offset += size_get;
1218     size_get = sizeof(uint16_t) * n->rss_data.indirections_len;
1219     g_free(n->rss_data.indirections_table);
1220     n->rss_data.indirections_table = g_malloc(size_get);
1221     if (!n->rss_data.indirections_table) {
1222         err_msg = "Can't allocate indirections table";
1223         err_value = n->rss_data.indirections_len;
1224         goto error;
1225     }
1226     s = iov_to_buf(iov, iov_cnt, offset,
1227                    n->rss_data.indirections_table, size_get);
1228     if (s != size_get) {
1229         err_msg = "Short indirection table buffer";
1230         err_value = (uint32_t)s;
1231         goto error;
1232     }
1233     for (i = 0; i < n->rss_data.indirections_len; ++i) {
1234         uint16_t val = n->rss_data.indirections_table[i];
1235         n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val);
1236     }
1237     offset += size_get;
1238     size_get = sizeof(temp);
1239     s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get);
1240     if (s != size_get) {
1241         err_msg = "Can't get queues";
1242         err_value = (uint32_t)s;
1243         goto error;
1244     }
1245     queues = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queues;
1246     if (queues == 0 || queues > n->max_queues) {
1247         err_msg = "Invalid number of queues";
1248         err_value = queues;
1249         goto error;
1250     }
1251     if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
1252         err_msg = "Invalid key size";
1253         err_value = temp.b;
1254         goto error;
1255     }
1256     if (!temp.b && n->rss_data.hash_types) {
1257         err_msg = "No key provided";
1258         err_value = 0;
1259         goto error;
1260     }
1261     if (!temp.b && !n->rss_data.hash_types) {
1262         virtio_net_disable_rss(n);
1263         return queues;
1264     }
1265     offset += size_get;
1266     size_get = temp.b;
1267     s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get);
1268     if (s != size_get) {
1269         err_msg = "Can get key buffer";
1270         err_value = (uint32_t)s;
1271         goto error;
1272     }
1273     n->rss_data.enabled = true;
1274     trace_virtio_net_rss_enable(n->rss_data.hash_types,
1275                                 n->rss_data.indirections_len,
1276                                 temp.b);
1277     return queues;
1278 error:
1279     trace_virtio_net_rss_error(err_msg, err_value);
1280     virtio_net_disable_rss(n);
1281     return 0;
1282 }
1283 
1284 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
1285                                 struct iovec *iov, unsigned int iov_cnt)
1286 {
1287     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1288     uint16_t queues;
1289 
1290     virtio_net_disable_rss(n);
1291     if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) {
1292         queues = virtio_net_handle_rss(n, iov, iov_cnt, false);
1293         return queues ? VIRTIO_NET_OK : VIRTIO_NET_ERR;
1294     }
1295     if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) {
1296         queues = virtio_net_handle_rss(n, iov, iov_cnt, true);
1297     } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
1298         struct virtio_net_ctrl_mq mq;
1299         size_t s;
1300         if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) {
1301             return VIRTIO_NET_ERR;
1302         }
1303         s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
1304         if (s != sizeof(mq)) {
1305             return VIRTIO_NET_ERR;
1306         }
1307         queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
1308 
1309     } else {
1310         return VIRTIO_NET_ERR;
1311     }
1312 
1313     if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1314         queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1315         queues > n->max_queues ||
1316         !n->multiqueue) {
1317         return VIRTIO_NET_ERR;
1318     }
1319 
1320     n->curr_queues = queues;
1321     /* stop the backend before changing the number of queues to avoid handling a
1322      * disabled queue */
1323     virtio_net_set_status(vdev, vdev->status);
1324     virtio_net_set_queues(n);
1325 
1326     return VIRTIO_NET_OK;
1327 }
1328 
1329 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
1330 {
1331     VirtIONet *n = VIRTIO_NET(vdev);
1332     struct virtio_net_ctrl_hdr ctrl;
1333     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
1334     VirtQueueElement *elem;
1335     size_t s;
1336     struct iovec *iov, *iov2;
1337     unsigned int iov_cnt;
1338 
1339     for (;;) {
1340         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
1341         if (!elem) {
1342             break;
1343         }
1344         if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
1345             iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
1346             virtio_error(vdev, "virtio-net ctrl missing headers");
1347             virtqueue_detach_element(vq, elem, 0);
1348             g_free(elem);
1349             break;
1350         }
1351 
1352         iov_cnt = elem->out_num;
1353         iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
1354         s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
1355         iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
1356         if (s != sizeof(ctrl)) {
1357             status = VIRTIO_NET_ERR;
1358         } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
1359             status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
1360         } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
1361             status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
1362         } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
1363             status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
1364         } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
1365             status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
1366         } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
1367             status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
1368         } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
1369             status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
1370         }
1371 
1372         s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
1373         assert(s == sizeof(status));
1374 
1375         virtqueue_push(vq, elem, sizeof(status));
1376         virtio_notify(vdev, vq);
1377         g_free(iov2);
1378         g_free(elem);
1379     }
1380 }
1381 
1382 /* RX */
1383 
1384 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
1385 {
1386     VirtIONet *n = VIRTIO_NET(vdev);
1387     int queue_index = vq2q(virtio_get_queue_index(vq));
1388 
1389     qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
1390 }
1391 
1392 static bool virtio_net_can_receive(NetClientState *nc)
1393 {
1394     VirtIONet *n = qemu_get_nic_opaque(nc);
1395     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1396     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1397 
1398     if (!vdev->vm_running) {
1399         return false;
1400     }
1401 
1402     if (nc->queue_index >= n->curr_queues) {
1403         return false;
1404     }
1405 
1406     if (!virtio_queue_ready(q->rx_vq) ||
1407         !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1408         return false;
1409     }
1410 
1411     return true;
1412 }
1413 
1414 static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
1415 {
1416     VirtIONet *n = q->n;
1417     if (virtio_queue_empty(q->rx_vq) ||
1418         (n->mergeable_rx_bufs &&
1419          !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
1420         virtio_queue_set_notification(q->rx_vq, 1);
1421 
1422         /* To avoid a race condition where the guest has made some buffers
1423          * available after the above check but before notification was
1424          * enabled, check for available buffers again.
1425          */
1426         if (virtio_queue_empty(q->rx_vq) ||
1427             (n->mergeable_rx_bufs &&
1428              !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
1429             return 0;
1430         }
1431     }
1432 
1433     virtio_queue_set_notification(q->rx_vq, 0);
1434     return 1;
1435 }
1436 
1437 static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1438 {
1439     virtio_tswap16s(vdev, &hdr->hdr_len);
1440     virtio_tswap16s(vdev, &hdr->gso_size);
1441     virtio_tswap16s(vdev, &hdr->csum_start);
1442     virtio_tswap16s(vdev, &hdr->csum_offset);
1443 }
1444 
1445 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1446  * it never finds out that the packets don't have valid checksums.  This
1447  * causes dhclient to get upset.  Fedora's carried a patch for ages to
1448  * fix this with Xen but it hasn't appeared in an upstream release of
1449  * dhclient yet.
1450  *
1451  * To avoid breaking existing guests, we catch udp packets and add
1452  * checksums.  This is terrible but it's better than hacking the guest
1453  * kernels.
1454  *
1455  * N.B. if we introduce a zero-copy API, this operation is no longer free so
1456  * we should provide a mechanism to disable it to avoid polluting the host
1457  * cache.
1458  */
1459 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
1460                                         uint8_t *buf, size_t size)
1461 {
1462     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
1463         (size > 27 && size < 1500) && /* normal sized MTU */
1464         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
1465         (buf[23] == 17) && /* ip.protocol == UDP */
1466         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1467         net_checksum_calculate(buf, size);
1468         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
1469     }
1470 }
1471 
1472 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
1473                            const void *buf, size_t size)
1474 {
1475     if (n->has_vnet_hdr) {
1476         /* FIXME this cast is evil */
1477         void *wbuf = (void *)buf;
1478         work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
1479                                     size - n->host_hdr_len);
1480 
1481         if (n->needs_vnet_hdr_swap) {
1482             virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
1483         }
1484         iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
1485     } else {
1486         struct virtio_net_hdr hdr = {
1487             .flags = 0,
1488             .gso_type = VIRTIO_NET_HDR_GSO_NONE
1489         };
1490         iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
1491     }
1492 }
1493 
1494 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
1495 {
1496     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1497     static const uint8_t vlan[] = {0x81, 0x00};
1498     uint8_t *ptr = (uint8_t *)buf;
1499     int i;
1500 
1501     if (n->promisc)
1502         return 1;
1503 
1504     ptr += n->host_hdr_len;
1505 
1506     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
1507         int vid = lduw_be_p(ptr + 14) & 0xfff;
1508         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
1509             return 0;
1510     }
1511 
1512     if (ptr[0] & 1) { // multicast
1513         if (!memcmp(ptr, bcast, sizeof(bcast))) {
1514             return !n->nobcast;
1515         } else if (n->nomulti) {
1516             return 0;
1517         } else if (n->allmulti || n->mac_table.multi_overflow) {
1518             return 1;
1519         }
1520 
1521         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
1522             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1523                 return 1;
1524             }
1525         }
1526     } else { // unicast
1527         if (n->nouni) {
1528             return 0;
1529         } else if (n->alluni || n->mac_table.uni_overflow) {
1530             return 1;
1531         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
1532             return 1;
1533         }
1534 
1535         for (i = 0; i < n->mac_table.first_multi; i++) {
1536             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1537                 return 1;
1538             }
1539         }
1540     }
1541 
1542     return 0;
1543 }
1544 
1545 static uint8_t virtio_net_get_hash_type(bool isip4,
1546                                         bool isip6,
1547                                         bool isudp,
1548                                         bool istcp,
1549                                         uint32_t types)
1550 {
1551     if (isip4) {
1552         if (istcp && (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4)) {
1553             return NetPktRssIpV4Tcp;
1554         }
1555         if (isudp && (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4)) {
1556             return NetPktRssIpV4Udp;
1557         }
1558         if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
1559             return NetPktRssIpV4;
1560         }
1561     } else if (isip6) {
1562         uint32_t mask = VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
1563                         VIRTIO_NET_RSS_HASH_TYPE_TCPv6;
1564 
1565         if (istcp && (types & mask)) {
1566             return (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) ?
1567                 NetPktRssIpV6TcpEx : NetPktRssIpV6Tcp;
1568         }
1569         mask = VIRTIO_NET_RSS_HASH_TYPE_UDP_EX | VIRTIO_NET_RSS_HASH_TYPE_UDPv6;
1570         if (isudp && (types & mask)) {
1571             return (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) ?
1572                 NetPktRssIpV6UdpEx : NetPktRssIpV6Udp;
1573         }
1574         mask = VIRTIO_NET_RSS_HASH_TYPE_IP_EX | VIRTIO_NET_RSS_HASH_TYPE_IPv6;
1575         if (types & mask) {
1576             return (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) ?
1577                 NetPktRssIpV6Ex : NetPktRssIpV6;
1578         }
1579     }
1580     return 0xff;
1581 }
1582 
1583 static void virtio_set_packet_hash(const uint8_t *buf, uint8_t report,
1584                                    uint32_t hash)
1585 {
1586     struct virtio_net_hdr_v1_hash *hdr = (void *)buf;
1587     hdr->hash_value = hash;
1588     hdr->hash_report = report;
1589 }
1590 
1591 static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
1592                                   size_t size)
1593 {
1594     VirtIONet *n = qemu_get_nic_opaque(nc);
1595     unsigned int index = nc->queue_index, new_index = index;
1596     struct NetRxPkt *pkt = n->rx_pkt;
1597     uint8_t net_hash_type;
1598     uint32_t hash;
1599     bool isip4, isip6, isudp, istcp;
1600     static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = {
1601         VIRTIO_NET_HASH_REPORT_IPv4,
1602         VIRTIO_NET_HASH_REPORT_TCPv4,
1603         VIRTIO_NET_HASH_REPORT_TCPv6,
1604         VIRTIO_NET_HASH_REPORT_IPv6,
1605         VIRTIO_NET_HASH_REPORT_IPv6_EX,
1606         VIRTIO_NET_HASH_REPORT_TCPv6_EX,
1607         VIRTIO_NET_HASH_REPORT_UDPv4,
1608         VIRTIO_NET_HASH_REPORT_UDPv6,
1609         VIRTIO_NET_HASH_REPORT_UDPv6_EX
1610     };
1611 
1612     net_rx_pkt_set_protocols(pkt, buf + n->host_hdr_len,
1613                              size - n->host_hdr_len);
1614     net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp);
1615     if (isip4 && (net_rx_pkt_get_ip4_info(pkt)->fragment)) {
1616         istcp = isudp = false;
1617     }
1618     if (isip6 && (net_rx_pkt_get_ip6_info(pkt)->fragment)) {
1619         istcp = isudp = false;
1620     }
1621     net_hash_type = virtio_net_get_hash_type(isip4, isip6, isudp, istcp,
1622                                              n->rss_data.hash_types);
1623     if (net_hash_type > NetPktRssIpV6UdpEx) {
1624         if (n->rss_data.populate_hash) {
1625             virtio_set_packet_hash(buf, VIRTIO_NET_HASH_REPORT_NONE, 0);
1626         }
1627         return n->rss_data.redirect ? n->rss_data.default_queue : -1;
1628     }
1629 
1630     hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key);
1631 
1632     if (n->rss_data.populate_hash) {
1633         virtio_set_packet_hash(buf, reports[net_hash_type], hash);
1634     }
1635 
1636     if (n->rss_data.redirect) {
1637         new_index = hash & (n->rss_data.indirections_len - 1);
1638         new_index = n->rss_data.indirections_table[new_index];
1639     }
1640 
1641     return (index == new_index) ? -1 : new_index;
1642 }
1643 
1644 static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
1645                                       size_t size, bool no_rss)
1646 {
1647     VirtIONet *n = qemu_get_nic_opaque(nc);
1648     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1649     VirtIODevice *vdev = VIRTIO_DEVICE(n);
1650     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
1651     struct virtio_net_hdr_mrg_rxbuf mhdr;
1652     unsigned mhdr_cnt = 0;
1653     size_t offset, i, guest_offset;
1654 
1655     if (!virtio_net_can_receive(nc)) {
1656         return -1;
1657     }
1658 
1659     if (!no_rss && n->rss_data.enabled) {
1660         int index = virtio_net_process_rss(nc, buf, size);
1661         if (index >= 0) {
1662             NetClientState *nc2 = qemu_get_subqueue(n->nic, index);
1663             return virtio_net_receive_rcu(nc2, buf, size, true);
1664         }
1665     }
1666 
1667     /* hdr_len refers to the header we supply to the guest */
1668     if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
1669         return 0;
1670     }
1671 
1672     if (!receive_filter(n, buf, size))
1673         return size;
1674 
1675     offset = i = 0;
1676 
1677     while (offset < size) {
1678         VirtQueueElement *elem;
1679         int len, total;
1680         const struct iovec *sg;
1681 
1682         total = 0;
1683 
1684         elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
1685         if (!elem) {
1686             if (i) {
1687                 virtio_error(vdev, "virtio-net unexpected empty queue: "
1688                              "i %zd mergeable %d offset %zd, size %zd, "
1689                              "guest hdr len %zd, host hdr len %zd "
1690                              "guest features 0x%" PRIx64,
1691                              i, n->mergeable_rx_bufs, offset, size,
1692                              n->guest_hdr_len, n->host_hdr_len,
1693                              vdev->guest_features);
1694             }
1695             return -1;
1696         }
1697 
1698         if (elem->in_num < 1) {
1699             virtio_error(vdev,
1700                          "virtio-net receive queue contains no in buffers");
1701             virtqueue_detach_element(q->rx_vq, elem, 0);
1702             g_free(elem);
1703             return -1;
1704         }
1705 
1706         sg = elem->in_sg;
1707         if (i == 0) {
1708             assert(offset == 0);
1709             if (n->mergeable_rx_bufs) {
1710                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
1711                                     sg, elem->in_num,
1712                                     offsetof(typeof(mhdr), num_buffers),
1713                                     sizeof(mhdr.num_buffers));
1714             }
1715 
1716             receive_header(n, sg, elem->in_num, buf, size);
1717             if (n->rss_data.populate_hash) {
1718                 offset = sizeof(mhdr);
1719                 iov_from_buf(sg, elem->in_num, offset,
1720                              buf + offset, n->host_hdr_len - sizeof(mhdr));
1721             }
1722             offset = n->host_hdr_len;
1723             total += n->guest_hdr_len;
1724             guest_offset = n->guest_hdr_len;
1725         } else {
1726             guest_offset = 0;
1727         }
1728 
1729         /* copy in packet.  ugh */
1730         len = iov_from_buf(sg, elem->in_num, guest_offset,
1731                            buf + offset, size - offset);
1732         total += len;
1733         offset += len;
1734         /* If buffers can't be merged, at this point we
1735          * must have consumed the complete packet.
1736          * Otherwise, drop it. */
1737         if (!n->mergeable_rx_bufs && offset < size) {
1738             virtqueue_unpop(q->rx_vq, elem, total);
1739             g_free(elem);
1740             return size;
1741         }
1742 
1743         /* signal other side */
1744         virtqueue_fill(q->rx_vq, elem, total, i++);
1745         g_free(elem);
1746     }
1747 
1748     if (mhdr_cnt) {
1749         virtio_stw_p(vdev, &mhdr.num_buffers, i);
1750         iov_from_buf(mhdr_sg, mhdr_cnt,
1751                      0,
1752                      &mhdr.num_buffers, sizeof mhdr.num_buffers);
1753     }
1754 
1755     virtqueue_flush(q->rx_vq, i);
1756     virtio_notify(vdev, q->rx_vq);
1757 
1758     return size;
1759 }
1760 
1761 static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
1762                                   size_t size)
1763 {
1764     RCU_READ_LOCK_GUARD();
1765 
1766     return virtio_net_receive_rcu(nc, buf, size, false);
1767 }
1768 
1769 static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain,
1770                                          const uint8_t *buf,
1771                                          VirtioNetRscUnit *unit)
1772 {
1773     uint16_t ip_hdrlen;
1774     struct ip_header *ip;
1775 
1776     ip = (struct ip_header *)(buf + chain->n->guest_hdr_len
1777                               + sizeof(struct eth_header));
1778     unit->ip = (void *)ip;
1779     ip_hdrlen = (ip->ip_ver_len & 0xF) << 2;
1780     unit->ip_plen = &ip->ip_len;
1781     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
1782     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
1783     unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
1784 }
1785 
1786 static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain,
1787                                          const uint8_t *buf,
1788                                          VirtioNetRscUnit *unit)
1789 {
1790     struct ip6_header *ip6;
1791 
1792     ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len
1793                                  + sizeof(struct eth_header));
1794     unit->ip = ip6;
1795     unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
1796     unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip)
1797                                         + sizeof(struct ip6_header));
1798     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
1799 
1800     /* There is a difference between payload lenght in ipv4 and v6,
1801        ip header is excluded in ipv6 */
1802     unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
1803 }
1804 
1805 static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain,
1806                                        VirtioNetRscSeg *seg)
1807 {
1808     int ret;
1809     struct virtio_net_hdr_v1 *h;
1810 
1811     h = (struct virtio_net_hdr_v1 *)seg->buf;
1812     h->flags = 0;
1813     h->gso_type = VIRTIO_NET_HDR_GSO_NONE;
1814 
1815     if (seg->is_coalesced) {
1816         h->rsc.segments = seg->packets;
1817         h->rsc.dup_acks = seg->dup_ack;
1818         h->flags = VIRTIO_NET_HDR_F_RSC_INFO;
1819         if (chain->proto == ETH_P_IP) {
1820             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1821         } else {
1822             h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1823         }
1824     }
1825 
1826     ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
1827     QTAILQ_REMOVE(&chain->buffers, seg, next);
1828     g_free(seg->buf);
1829     g_free(seg);
1830 
1831     return ret;
1832 }
1833 
1834 static void virtio_net_rsc_purge(void *opq)
1835 {
1836     VirtioNetRscSeg *seg, *rn;
1837     VirtioNetRscChain *chain = (VirtioNetRscChain *)opq;
1838 
1839     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
1840         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
1841             chain->stat.purge_failed++;
1842             continue;
1843         }
1844     }
1845 
1846     chain->stat.timer++;
1847     if (!QTAILQ_EMPTY(&chain->buffers)) {
1848         timer_mod(chain->drain_timer,
1849               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
1850     }
1851 }
1852 
1853 static void virtio_net_rsc_cleanup(VirtIONet *n)
1854 {
1855     VirtioNetRscChain *chain, *rn_chain;
1856     VirtioNetRscSeg *seg, *rn_seg;
1857 
1858     QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
1859         QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
1860             QTAILQ_REMOVE(&chain->buffers, seg, next);
1861             g_free(seg->buf);
1862             g_free(seg);
1863         }
1864 
1865         timer_del(chain->drain_timer);
1866         timer_free(chain->drain_timer);
1867         QTAILQ_REMOVE(&n->rsc_chains, chain, next);
1868         g_free(chain);
1869     }
1870 }
1871 
1872 static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain,
1873                                      NetClientState *nc,
1874                                      const uint8_t *buf, size_t size)
1875 {
1876     uint16_t hdr_len;
1877     VirtioNetRscSeg *seg;
1878 
1879     hdr_len = chain->n->guest_hdr_len;
1880     seg = g_malloc(sizeof(VirtioNetRscSeg));
1881     seg->buf = g_malloc(hdr_len + sizeof(struct eth_header)
1882         + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD);
1883     memcpy(seg->buf, buf, size);
1884     seg->size = size;
1885     seg->packets = 1;
1886     seg->dup_ack = 0;
1887     seg->is_coalesced = 0;
1888     seg->nc = nc;
1889 
1890     QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
1891     chain->stat.cache++;
1892 
1893     switch (chain->proto) {
1894     case ETH_P_IP:
1895         virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
1896         break;
1897     case ETH_P_IPV6:
1898         virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
1899         break;
1900     default:
1901         g_assert_not_reached();
1902     }
1903 }
1904 
1905 static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain,
1906                                          VirtioNetRscSeg *seg,
1907                                          const uint8_t *buf,
1908                                          struct tcp_header *n_tcp,
1909                                          struct tcp_header *o_tcp)
1910 {
1911     uint32_t nack, oack;
1912     uint16_t nwin, owin;
1913 
1914     nack = htonl(n_tcp->th_ack);
1915     nwin = htons(n_tcp->th_win);
1916     oack = htonl(o_tcp->th_ack);
1917     owin = htons(o_tcp->th_win);
1918 
1919     if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) {
1920         chain->stat.ack_out_of_win++;
1921         return RSC_FINAL;
1922     } else if (nack == oack) {
1923         /* duplicated ack or window probe */
1924         if (nwin == owin) {
1925             /* duplicated ack, add dup ack count due to whql test up to 1 */
1926             chain->stat.dup_ack++;
1927             return RSC_FINAL;
1928         } else {
1929             /* Coalesce window update */
1930             o_tcp->th_win = n_tcp->th_win;
1931             chain->stat.win_update++;
1932             return RSC_COALESCE;
1933         }
1934     } else {
1935         /* pure ack, go to 'C', finalize*/
1936         chain->stat.pure_ack++;
1937         return RSC_FINAL;
1938     }
1939 }
1940 
1941 static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain,
1942                                             VirtioNetRscSeg *seg,
1943                                             const uint8_t *buf,
1944                                             VirtioNetRscUnit *n_unit)
1945 {
1946     void *data;
1947     uint16_t o_ip_len;
1948     uint32_t nseq, oseq;
1949     VirtioNetRscUnit *o_unit;
1950 
1951     o_unit = &seg->unit;
1952     o_ip_len = htons(*o_unit->ip_plen);
1953     nseq = htonl(n_unit->tcp->th_seq);
1954     oseq = htonl(o_unit->tcp->th_seq);
1955 
1956     /* out of order or retransmitted. */
1957     if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) {
1958         chain->stat.data_out_of_win++;
1959         return RSC_FINAL;
1960     }
1961 
1962     data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
1963     if (nseq == oseq) {
1964         if ((o_unit->payload == 0) && n_unit->payload) {
1965             /* From no payload to payload, normal case, not a dup ack or etc */
1966             chain->stat.data_after_pure_ack++;
1967             goto coalesce;
1968         } else {
1969             return virtio_net_rsc_handle_ack(chain, seg, buf,
1970                                              n_unit->tcp, o_unit->tcp);
1971         }
1972     } else if ((nseq - oseq) != o_unit->payload) {
1973         /* Not a consistent packet, out of order */
1974         chain->stat.data_out_of_order++;
1975         return RSC_FINAL;
1976     } else {
1977 coalesce:
1978         if ((o_ip_len + n_unit->payload) > chain->max_payload) {
1979             chain->stat.over_size++;
1980             return RSC_FINAL;
1981         }
1982 
1983         /* Here comes the right data, the payload length in v4/v6 is different,
1984            so use the field value to update and record the new data len */
1985         o_unit->payload += n_unit->payload; /* update new data len */
1986 
1987         /* update field in ip header */
1988         *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
1989 
1990         /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
1991            for windows guest, while this may change the behavior for linux
1992            guest (only if it uses RSC feature). */
1993         o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
1994 
1995         o_unit->tcp->th_ack = n_unit->tcp->th_ack;
1996         o_unit->tcp->th_win = n_unit->tcp->th_win;
1997 
1998         memmove(seg->buf + seg->size, data, n_unit->payload);
1999         seg->size += n_unit->payload;
2000         seg->packets++;
2001         chain->stat.coalesced++;
2002         return RSC_COALESCE;
2003     }
2004 }
2005 
2006 static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain,
2007                                         VirtioNetRscSeg *seg,
2008                                         const uint8_t *buf, size_t size,
2009                                         VirtioNetRscUnit *unit)
2010 {
2011     struct ip_header *ip1, *ip2;
2012 
2013     ip1 = (struct ip_header *)(unit->ip);
2014     ip2 = (struct ip_header *)(seg->unit.ip);
2015     if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst)
2016         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
2017         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
2018         chain->stat.no_match++;
2019         return RSC_NO_MATCH;
2020     }
2021 
2022     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
2023 }
2024 
2025 static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain,
2026                                         VirtioNetRscSeg *seg,
2027                                         const uint8_t *buf, size_t size,
2028                                         VirtioNetRscUnit *unit)
2029 {
2030     struct ip6_header *ip1, *ip2;
2031 
2032     ip1 = (struct ip6_header *)(unit->ip);
2033     ip2 = (struct ip6_header *)(seg->unit.ip);
2034     if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address))
2035         || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address))
2036         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
2037         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
2038             chain->stat.no_match++;
2039             return RSC_NO_MATCH;
2040     }
2041 
2042     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
2043 }
2044 
2045 /* Packets with 'SYN' should bypass, other flag should be sent after drain
2046  * to prevent out of order */
2047 static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,
2048                                          struct tcp_header *tcp)
2049 {
2050     uint16_t tcp_hdr;
2051     uint16_t tcp_flag;
2052 
2053     tcp_flag = htons(tcp->th_offset_flags);
2054     tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10;
2055     tcp_flag &= VIRTIO_NET_TCP_FLAG;
2056     if (tcp_flag & TH_SYN) {
2057         chain->stat.tcp_syn++;
2058         return RSC_BYPASS;
2059     }
2060 
2061     if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) {
2062         chain->stat.tcp_ctrl_drain++;
2063         return RSC_FINAL;
2064     }
2065 
2066     if (tcp_hdr > sizeof(struct tcp_header)) {
2067         chain->stat.tcp_all_opt++;
2068         return RSC_FINAL;
2069     }
2070 
2071     return RSC_CANDIDATE;
2072 }
2073 
2074 static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain,
2075                                          NetClientState *nc,
2076                                          const uint8_t *buf, size_t size,
2077                                          VirtioNetRscUnit *unit)
2078 {
2079     int ret;
2080     VirtioNetRscSeg *seg, *nseg;
2081 
2082     if (QTAILQ_EMPTY(&chain->buffers)) {
2083         chain->stat.empty_cache++;
2084         virtio_net_rsc_cache_buf(chain, nc, buf, size);
2085         timer_mod(chain->drain_timer,
2086               qemu_clock_get_ns(QEMU_CLOCK_HOST) + chain->n->rsc_timeout);
2087         return size;
2088     }
2089 
2090     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
2091         if (chain->proto == ETH_P_IP) {
2092             ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
2093         } else {
2094             ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
2095         }
2096 
2097         if (ret == RSC_FINAL) {
2098             if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
2099                 /* Send failed */
2100                 chain->stat.final_failed++;
2101                 return 0;
2102             }
2103 
2104             /* Send current packet */
2105             return virtio_net_do_receive(nc, buf, size);
2106         } else if (ret == RSC_NO_MATCH) {
2107             continue;
2108         } else {
2109             /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
2110             seg->is_coalesced = 1;
2111             return size;
2112         }
2113     }
2114 
2115     chain->stat.no_match_cache++;
2116     virtio_net_rsc_cache_buf(chain, nc, buf, size);
2117     return size;
2118 }
2119 
2120 /* Drain a connection data, this is to avoid out of order segments */
2121 static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain,
2122                                         NetClientState *nc,
2123                                         const uint8_t *buf, size_t size,
2124                                         uint16_t ip_start, uint16_t ip_size,
2125                                         uint16_t tcp_port)
2126 {
2127     VirtioNetRscSeg *seg, *nseg;
2128     uint32_t ppair1, ppair2;
2129 
2130     ppair1 = *(uint32_t *)(buf + tcp_port);
2131     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
2132         ppair2 = *(uint32_t *)(seg->buf + tcp_port);
2133         if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size)
2134             || (ppair1 != ppair2)) {
2135             continue;
2136         }
2137         if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
2138             chain->stat.drain_failed++;
2139         }
2140 
2141         break;
2142     }
2143 
2144     return virtio_net_do_receive(nc, buf, size);
2145 }
2146 
2147 static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain,
2148                                             struct ip_header *ip,
2149                                             const uint8_t *buf, size_t size)
2150 {
2151     uint16_t ip_len;
2152 
2153     /* Not an ipv4 packet */
2154     if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) {
2155         chain->stat.ip_option++;
2156         return RSC_BYPASS;
2157     }
2158 
2159     /* Don't handle packets with ip option */
2160     if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) {
2161         chain->stat.ip_option++;
2162         return RSC_BYPASS;
2163     }
2164 
2165     if (ip->ip_p != IPPROTO_TCP) {
2166         chain->stat.bypass_not_tcp++;
2167         return RSC_BYPASS;
2168     }
2169 
2170     /* Don't handle packets with ip fragment */
2171     if (!(htons(ip->ip_off) & IP_DF)) {
2172         chain->stat.ip_frag++;
2173         return RSC_BYPASS;
2174     }
2175 
2176     /* Don't handle packets with ecn flag */
2177     if (IPTOS_ECN(ip->ip_tos)) {
2178         chain->stat.ip_ecn++;
2179         return RSC_BYPASS;
2180     }
2181 
2182     ip_len = htons(ip->ip_len);
2183     if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header))
2184         || ip_len > (size - chain->n->guest_hdr_len -
2185                      sizeof(struct eth_header))) {
2186         chain->stat.ip_hacked++;
2187         return RSC_BYPASS;
2188     }
2189 
2190     return RSC_CANDIDATE;
2191 }
2192 
2193 static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain,
2194                                       NetClientState *nc,
2195                                       const uint8_t *buf, size_t size)
2196 {
2197     int32_t ret;
2198     uint16_t hdr_len;
2199     VirtioNetRscUnit unit;
2200 
2201     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
2202 
2203     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)
2204         + sizeof(struct tcp_header))) {
2205         chain->stat.bypass_not_tcp++;
2206         return virtio_net_do_receive(nc, buf, size);
2207     }
2208 
2209     virtio_net_rsc_extract_unit4(chain, buf, &unit);
2210     if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)
2211         != RSC_CANDIDATE) {
2212         return virtio_net_do_receive(nc, buf, size);
2213     }
2214 
2215     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
2216     if (ret == RSC_BYPASS) {
2217         return virtio_net_do_receive(nc, buf, size);
2218     } else if (ret == RSC_FINAL) {
2219         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
2220                 ((hdr_len + sizeof(struct eth_header)) + 12),
2221                 VIRTIO_NET_IP4_ADDR_SIZE,
2222                 hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header));
2223     }
2224 
2225     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
2226 }
2227 
2228 static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain,
2229                                             struct ip6_header *ip6,
2230                                             const uint8_t *buf, size_t size)
2231 {
2232     uint16_t ip_len;
2233 
2234     if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4)
2235         != IP_HEADER_VERSION_6) {
2236         return RSC_BYPASS;
2237     }
2238 
2239     /* Both option and protocol is checked in this */
2240     if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
2241         chain->stat.bypass_not_tcp++;
2242         return RSC_BYPASS;
2243     }
2244 
2245     ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
2246     if (ip_len < sizeof(struct tcp_header) ||
2247         ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header)
2248                   - sizeof(struct ip6_header))) {
2249         chain->stat.ip_hacked++;
2250         return RSC_BYPASS;
2251     }
2252 
2253     /* Don't handle packets with ecn flag */
2254     if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) {
2255         chain->stat.ip_ecn++;
2256         return RSC_BYPASS;
2257     }
2258 
2259     return RSC_CANDIDATE;
2260 }
2261 
2262 static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc,
2263                                       const uint8_t *buf, size_t size)
2264 {
2265     int32_t ret;
2266     uint16_t hdr_len;
2267     VirtioNetRscChain *chain;
2268     VirtioNetRscUnit unit;
2269 
2270     chain = (VirtioNetRscChain *)opq;
2271     hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
2272 
2273     if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header)
2274         + sizeof(tcp_header))) {
2275         return virtio_net_do_receive(nc, buf, size);
2276     }
2277 
2278     virtio_net_rsc_extract_unit6(chain, buf, &unit);
2279     if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain,
2280                                                  unit.ip, buf, size)) {
2281         return virtio_net_do_receive(nc, buf, size);
2282     }
2283 
2284     ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
2285     if (ret == RSC_BYPASS) {
2286         return virtio_net_do_receive(nc, buf, size);
2287     } else if (ret == RSC_FINAL) {
2288         return virtio_net_rsc_drain_flow(chain, nc, buf, size,
2289                 ((hdr_len + sizeof(struct eth_header)) + 8),
2290                 VIRTIO_NET_IP6_ADDR_SIZE,
2291                 hdr_len + sizeof(struct eth_header)
2292                 + sizeof(struct ip6_header));
2293     }
2294 
2295     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
2296 }
2297 
2298 static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n,
2299                                                       NetClientState *nc,
2300                                                       uint16_t proto)
2301 {
2302     VirtioNetRscChain *chain;
2303 
2304     if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
2305         return NULL;
2306     }
2307 
2308     QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
2309         if (chain->proto == proto) {
2310             return chain;
2311         }
2312     }
2313 
2314     chain = g_malloc(sizeof(*chain));
2315     chain->n = n;
2316     chain->proto = proto;
2317     if (proto == (uint16_t)ETH_P_IP) {
2318         chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD;
2319         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
2320     } else {
2321         chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD;
2322         chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
2323     }
2324     chain->drain_timer = timer_new_ns(QEMU_CLOCK_HOST,
2325                                       virtio_net_rsc_purge, chain);
2326     memset(&chain->stat, 0, sizeof(chain->stat));
2327 
2328     QTAILQ_INIT(&chain->buffers);
2329     QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
2330 
2331     return chain;
2332 }
2333 
2334 static ssize_t virtio_net_rsc_receive(NetClientState *nc,
2335                                       const uint8_t *buf,
2336                                       size_t size)
2337 {
2338     uint16_t proto;
2339     VirtioNetRscChain *chain;
2340     struct eth_header *eth;
2341     VirtIONet *n;
2342 
2343     n = qemu_get_nic_opaque(nc);
2344     if (size < (n->host_hdr_len + sizeof(struct eth_header))) {
2345         return virtio_net_do_receive(nc, buf, size);
2346     }
2347 
2348     eth = (struct eth_header *)(buf + n->guest_hdr_len);
2349     proto = htons(eth->h_proto);
2350 
2351     chain = virtio_net_rsc_lookup_chain(n, nc, proto);
2352     if (chain) {
2353         chain->stat.received++;
2354         if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) {
2355             return virtio_net_rsc_receive4(chain, nc, buf, size);
2356         } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) {
2357             return virtio_net_rsc_receive6(chain, nc, buf, size);
2358         }
2359     }
2360     return virtio_net_do_receive(nc, buf, size);
2361 }
2362 
2363 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
2364                                   size_t size)
2365 {
2366     VirtIONet *n = qemu_get_nic_opaque(nc);
2367     if ((n->rsc4_enabled || n->rsc6_enabled)) {
2368         return virtio_net_rsc_receive(nc, buf, size);
2369     } else {
2370         return virtio_net_do_receive(nc, buf, size);
2371     }
2372 }
2373 
2374 static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
2375 
2376 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
2377 {
2378     VirtIONet *n = qemu_get_nic_opaque(nc);
2379     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
2380     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2381 
2382     virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
2383     virtio_notify(vdev, q->tx_vq);
2384 
2385     g_free(q->async_tx.elem);
2386     q->async_tx.elem = NULL;
2387 
2388     virtio_queue_set_notification(q->tx_vq, 1);
2389     virtio_net_flush_tx(q);
2390 }
2391 
2392 /* TX */
2393 static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
2394 {
2395     VirtIONet *n = q->n;
2396     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2397     VirtQueueElement *elem;
2398     int32_t num_packets = 0;
2399     int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
2400     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
2401         return num_packets;
2402     }
2403 
2404     if (q->async_tx.elem) {
2405         virtio_queue_set_notification(q->tx_vq, 0);
2406         return num_packets;
2407     }
2408 
2409     for (;;) {
2410         ssize_t ret;
2411         unsigned int out_num;
2412         struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
2413         struct virtio_net_hdr_mrg_rxbuf mhdr;
2414 
2415         elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
2416         if (!elem) {
2417             break;
2418         }
2419 
2420         out_num = elem->out_num;
2421         out_sg = elem->out_sg;
2422         if (out_num < 1) {
2423             virtio_error(vdev, "virtio-net header not in first element");
2424             virtqueue_detach_element(q->tx_vq, elem, 0);
2425             g_free(elem);
2426             return -EINVAL;
2427         }
2428 
2429         if (n->has_vnet_hdr) {
2430             if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
2431                 n->guest_hdr_len) {
2432                 virtio_error(vdev, "virtio-net header incorrect");
2433                 virtqueue_detach_element(q->tx_vq, elem, 0);
2434                 g_free(elem);
2435                 return -EINVAL;
2436             }
2437             if (n->needs_vnet_hdr_swap) {
2438                 virtio_net_hdr_swap(vdev, (void *) &mhdr);
2439                 sg2[0].iov_base = &mhdr;
2440                 sg2[0].iov_len = n->guest_hdr_len;
2441                 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
2442                                    out_sg, out_num,
2443                                    n->guest_hdr_len, -1);
2444                 if (out_num == VIRTQUEUE_MAX_SIZE) {
2445                     goto drop;
2446                 }
2447                 out_num += 1;
2448                 out_sg = sg2;
2449             }
2450         }
2451         /*
2452          * If host wants to see the guest header as is, we can
2453          * pass it on unchanged. Otherwise, copy just the parts
2454          * that host is interested in.
2455          */
2456         assert(n->host_hdr_len <= n->guest_hdr_len);
2457         if (n->host_hdr_len != n->guest_hdr_len) {
2458             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
2459                                        out_sg, out_num,
2460                                        0, n->host_hdr_len);
2461             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
2462                              out_sg, out_num,
2463                              n->guest_hdr_len, -1);
2464             out_num = sg_num;
2465             out_sg = sg;
2466         }
2467 
2468         ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
2469                                       out_sg, out_num, virtio_net_tx_complete);
2470         if (ret == 0) {
2471             virtio_queue_set_notification(q->tx_vq, 0);
2472             q->async_tx.elem = elem;
2473             return -EBUSY;
2474         }
2475 
2476 drop:
2477         virtqueue_push(q->tx_vq, elem, 0);
2478         virtio_notify(vdev, q->tx_vq);
2479         g_free(elem);
2480 
2481         if (++num_packets >= n->tx_burst) {
2482             break;
2483         }
2484     }
2485     return num_packets;
2486 }
2487 
2488 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
2489 {
2490     VirtIONet *n = VIRTIO_NET(vdev);
2491     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
2492 
2493     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2494         virtio_net_drop_tx_queue_data(vdev, vq);
2495         return;
2496     }
2497 
2498     /* This happens when device was stopped but VCPU wasn't. */
2499     if (!vdev->vm_running) {
2500         q->tx_waiting = 1;
2501         return;
2502     }
2503 
2504     if (q->tx_waiting) {
2505         virtio_queue_set_notification(vq, 1);
2506         timer_del(q->tx_timer);
2507         q->tx_waiting = 0;
2508         if (virtio_net_flush_tx(q) == -EINVAL) {
2509             return;
2510         }
2511     } else {
2512         timer_mod(q->tx_timer,
2513                        qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
2514         q->tx_waiting = 1;
2515         virtio_queue_set_notification(vq, 0);
2516     }
2517 }
2518 
2519 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
2520 {
2521     VirtIONet *n = VIRTIO_NET(vdev);
2522     VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
2523 
2524     if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2525         virtio_net_drop_tx_queue_data(vdev, vq);
2526         return;
2527     }
2528 
2529     if (unlikely(q->tx_waiting)) {
2530         return;
2531     }
2532     q->tx_waiting = 1;
2533     /* This happens when device was stopped but VCPU wasn't. */
2534     if (!vdev->vm_running) {
2535         return;
2536     }
2537     virtio_queue_set_notification(vq, 0);
2538     qemu_bh_schedule(q->tx_bh);
2539 }
2540 
2541 static void virtio_net_tx_timer(void *opaque)
2542 {
2543     VirtIONetQueue *q = opaque;
2544     VirtIONet *n = q->n;
2545     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2546     /* This happens when device was stopped but BH wasn't. */
2547     if (!vdev->vm_running) {
2548         /* Make sure tx waiting is set, so we'll run when restarted. */
2549         assert(q->tx_waiting);
2550         return;
2551     }
2552 
2553     q->tx_waiting = 0;
2554 
2555     /* Just in case the driver is not ready on more */
2556     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
2557         return;
2558     }
2559 
2560     virtio_queue_set_notification(q->tx_vq, 1);
2561     virtio_net_flush_tx(q);
2562 }
2563 
2564 static void virtio_net_tx_bh(void *opaque)
2565 {
2566     VirtIONetQueue *q = opaque;
2567     VirtIONet *n = q->n;
2568     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2569     int32_t ret;
2570 
2571     /* This happens when device was stopped but BH wasn't. */
2572     if (!vdev->vm_running) {
2573         /* Make sure tx waiting is set, so we'll run when restarted. */
2574         assert(q->tx_waiting);
2575         return;
2576     }
2577 
2578     q->tx_waiting = 0;
2579 
2580     /* Just in case the driver is not ready on more */
2581     if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
2582         return;
2583     }
2584 
2585     ret = virtio_net_flush_tx(q);
2586     if (ret == -EBUSY || ret == -EINVAL) {
2587         return; /* Notification re-enable handled by tx_complete or device
2588                  * broken */
2589     }
2590 
2591     /* If we flush a full burst of packets, assume there are
2592      * more coming and immediately reschedule */
2593     if (ret >= n->tx_burst) {
2594         qemu_bh_schedule(q->tx_bh);
2595         q->tx_waiting = 1;
2596         return;
2597     }
2598 
2599     /* If less than a full burst, re-enable notification and flush
2600      * anything that may have come in while we weren't looking.  If
2601      * we find something, assume the guest is still active and reschedule */
2602     virtio_queue_set_notification(q->tx_vq, 1);
2603     ret = virtio_net_flush_tx(q);
2604     if (ret == -EINVAL) {
2605         return;
2606     } else if (ret > 0) {
2607         virtio_queue_set_notification(q->tx_vq, 0);
2608         qemu_bh_schedule(q->tx_bh);
2609         q->tx_waiting = 1;
2610     }
2611 }
2612 
2613 static void virtio_net_add_queue(VirtIONet *n, int index)
2614 {
2615     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2616 
2617     n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
2618                                            virtio_net_handle_rx);
2619 
2620     if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
2621         n->vqs[index].tx_vq =
2622             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
2623                              virtio_net_handle_tx_timer);
2624         n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2625                                               virtio_net_tx_timer,
2626                                               &n->vqs[index]);
2627     } else {
2628         n->vqs[index].tx_vq =
2629             virtio_add_queue(vdev, n->net_conf.tx_queue_size,
2630                              virtio_net_handle_tx_bh);
2631         n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
2632     }
2633 
2634     n->vqs[index].tx_waiting = 0;
2635     n->vqs[index].n = n;
2636 }
2637 
2638 static void virtio_net_del_queue(VirtIONet *n, int index)
2639 {
2640     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2641     VirtIONetQueue *q = &n->vqs[index];
2642     NetClientState *nc = qemu_get_subqueue(n->nic, index);
2643 
2644     qemu_purge_queued_packets(nc);
2645 
2646     virtio_del_queue(vdev, index * 2);
2647     if (q->tx_timer) {
2648         timer_del(q->tx_timer);
2649         timer_free(q->tx_timer);
2650         q->tx_timer = NULL;
2651     } else {
2652         qemu_bh_delete(q->tx_bh);
2653         q->tx_bh = NULL;
2654     }
2655     q->tx_waiting = 0;
2656     virtio_del_queue(vdev, index * 2 + 1);
2657 }
2658 
2659 static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
2660 {
2661     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2662     int old_num_queues = virtio_get_num_queues(vdev);
2663     int new_num_queues = new_max_queues * 2 + 1;
2664     int i;
2665 
2666     assert(old_num_queues >= 3);
2667     assert(old_num_queues % 2 == 1);
2668 
2669     if (old_num_queues == new_num_queues) {
2670         return;
2671     }
2672 
2673     /*
2674      * We always need to remove and add ctrl vq if
2675      * old_num_queues != new_num_queues. Remove ctrl_vq first,
2676      * and then we only enter one of the following two loops.
2677      */
2678     virtio_del_queue(vdev, old_num_queues - 1);
2679 
2680     for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
2681         /* new_num_queues < old_num_queues */
2682         virtio_net_del_queue(n, i / 2);
2683     }
2684 
2685     for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
2686         /* new_num_queues > old_num_queues */
2687         virtio_net_add_queue(n, i / 2);
2688     }
2689 
2690     /* add ctrl_vq last */
2691     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
2692 }
2693 
2694 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
2695 {
2696     int max = multiqueue ? n->max_queues : 1;
2697 
2698     n->multiqueue = multiqueue;
2699     virtio_net_change_num_queues(n, max);
2700 
2701     virtio_net_set_queues(n);
2702 }
2703 
2704 static int virtio_net_post_load_device(void *opaque, int version_id)
2705 {
2706     VirtIONet *n = opaque;
2707     VirtIODevice *vdev = VIRTIO_DEVICE(n);
2708     int i, link_down;
2709 
2710     trace_virtio_net_post_load_device();
2711     virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
2712                                virtio_vdev_has_feature(vdev,
2713                                                        VIRTIO_F_VERSION_1),
2714                                virtio_vdev_has_feature(vdev,
2715                                                        VIRTIO_NET_F_HASH_REPORT));
2716 
2717     /* MAC_TABLE_ENTRIES may be different from the saved image */
2718     if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
2719         n->mac_table.in_use = 0;
2720     }
2721 
2722     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
2723         n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
2724     }
2725 
2726     /*
2727      * curr_guest_offloads will be later overwritten by the
2728      * virtio_set_features_nocheck call done from the virtio_load.
2729      * Here we make sure it is preserved and restored accordingly
2730      * in the virtio_net_post_load_virtio callback.
2731      */
2732     n->saved_guest_offloads = n->curr_guest_offloads;
2733 
2734     virtio_net_set_queues(n);
2735 
2736     /* Find the first multicast entry in the saved MAC filter */
2737     for (i = 0; i < n->mac_table.in_use; i++) {
2738         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
2739             break;
2740         }
2741     }
2742     n->mac_table.first_multi = i;
2743 
2744     /* nc.link_down can't be migrated, so infer link_down according
2745      * to link status bit in n->status */
2746     link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
2747     for (i = 0; i < n->max_queues; i++) {
2748         qemu_get_subqueue(n->nic, i)->link_down = link_down;
2749     }
2750 
2751     if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
2752         virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
2753         qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
2754                                   QEMU_CLOCK_VIRTUAL,
2755                                   virtio_net_announce_timer, n);
2756         if (n->announce_timer.round) {
2757             timer_mod(n->announce_timer.tm,
2758                       qemu_clock_get_ms(n->announce_timer.type));
2759         } else {
2760             qemu_announce_timer_del(&n->announce_timer, false);
2761         }
2762     }
2763 
2764     if (n->rss_data.enabled) {
2765         trace_virtio_net_rss_enable(n->rss_data.hash_types,
2766                                     n->rss_data.indirections_len,
2767                                     sizeof(n->rss_data.key));
2768     } else {
2769         trace_virtio_net_rss_disable();
2770     }
2771     return 0;
2772 }
2773 
2774 static int virtio_net_post_load_virtio(VirtIODevice *vdev)
2775 {
2776     VirtIONet *n = VIRTIO_NET(vdev);
2777     /*
2778      * The actual needed state is now in saved_guest_offloads,
2779      * see virtio_net_post_load_device for detail.
2780      * Restore it back and apply the desired offloads.
2781      */
2782     n->curr_guest_offloads = n->saved_guest_offloads;
2783     if (peer_has_vnet_hdr(n)) {
2784         virtio_net_apply_guest_offloads(n);
2785     }
2786 
2787     return 0;
2788 }
2789 
2790 /* tx_waiting field of a VirtIONetQueue */
2791 static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
2792     .name = "virtio-net-queue-tx_waiting",
2793     .fields = (VMStateField[]) {
2794         VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
2795         VMSTATE_END_OF_LIST()
2796    },
2797 };
2798 
2799 static bool max_queues_gt_1(void *opaque, int version_id)
2800 {
2801     return VIRTIO_NET(opaque)->max_queues > 1;
2802 }
2803 
2804 static bool has_ctrl_guest_offloads(void *opaque, int version_id)
2805 {
2806     return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
2807                                    VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
2808 }
2809 
2810 static bool mac_table_fits(void *opaque, int version_id)
2811 {
2812     return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
2813 }
2814 
2815 static bool mac_table_doesnt_fit(void *opaque, int version_id)
2816 {
2817     return !mac_table_fits(opaque, version_id);
2818 }
2819 
2820 /* This temporary type is shared by all the WITH_TMP methods
2821  * although only some fields are used by each.
2822  */
2823 struct VirtIONetMigTmp {
2824     VirtIONet      *parent;
2825     VirtIONetQueue *vqs_1;
2826     uint16_t        curr_queues_1;
2827     uint8_t         has_ufo;
2828     uint32_t        has_vnet_hdr;
2829 };
2830 
2831 /* The 2nd and subsequent tx_waiting flags are loaded later than
2832  * the 1st entry in the queues and only if there's more than one
2833  * entry.  We use the tmp mechanism to calculate a temporary
2834  * pointer and count and also validate the count.
2835  */
2836 
2837 static int virtio_net_tx_waiting_pre_save(void *opaque)
2838 {
2839     struct VirtIONetMigTmp *tmp = opaque;
2840 
2841     tmp->vqs_1 = tmp->parent->vqs + 1;
2842     tmp->curr_queues_1 = tmp->parent->curr_queues - 1;
2843     if (tmp->parent->curr_queues == 0) {
2844         tmp->curr_queues_1 = 0;
2845     }
2846 
2847     return 0;
2848 }
2849 
2850 static int virtio_net_tx_waiting_pre_load(void *opaque)
2851 {
2852     struct VirtIONetMigTmp *tmp = opaque;
2853 
2854     /* Reuse the pointer setup from save */
2855     virtio_net_tx_waiting_pre_save(opaque);
2856 
2857     if (tmp->parent->curr_queues > tmp->parent->max_queues) {
2858         error_report("virtio-net: curr_queues %x > max_queues %x",
2859             tmp->parent->curr_queues, tmp->parent->max_queues);
2860 
2861         return -EINVAL;
2862     }
2863 
2864     return 0; /* all good */
2865 }
2866 
2867 static const VMStateDescription vmstate_virtio_net_tx_waiting = {
2868     .name      = "virtio-net-tx_waiting",
2869     .pre_load  = virtio_net_tx_waiting_pre_load,
2870     .pre_save  = virtio_net_tx_waiting_pre_save,
2871     .fields    = (VMStateField[]) {
2872         VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
2873                                      curr_queues_1,
2874                                      vmstate_virtio_net_queue_tx_waiting,
2875                                      struct VirtIONetQueue),
2876         VMSTATE_END_OF_LIST()
2877     },
2878 };
2879 
2880 /* the 'has_ufo' flag is just tested; if the incoming stream has the
2881  * flag set we need to check that we have it
2882  */
2883 static int virtio_net_ufo_post_load(void *opaque, int version_id)
2884 {
2885     struct VirtIONetMigTmp *tmp = opaque;
2886 
2887     if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
2888         error_report("virtio-net: saved image requires TUN_F_UFO support");
2889         return -EINVAL;
2890     }
2891 
2892     return 0;
2893 }
2894 
2895 static int virtio_net_ufo_pre_save(void *opaque)
2896 {
2897     struct VirtIONetMigTmp *tmp = opaque;
2898 
2899     tmp->has_ufo = tmp->parent->has_ufo;
2900 
2901     return 0;
2902 }
2903 
2904 static const VMStateDescription vmstate_virtio_net_has_ufo = {
2905     .name      = "virtio-net-ufo",
2906     .post_load = virtio_net_ufo_post_load,
2907     .pre_save  = virtio_net_ufo_pre_save,
2908     .fields    = (VMStateField[]) {
2909         VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
2910         VMSTATE_END_OF_LIST()
2911     },
2912 };
2913 
2914 /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
2915  * flag set we need to check that we have it
2916  */
2917 static int virtio_net_vnet_post_load(void *opaque, int version_id)
2918 {
2919     struct VirtIONetMigTmp *tmp = opaque;
2920 
2921     if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
2922         error_report("virtio-net: saved image requires vnet_hdr=on");
2923         return -EINVAL;
2924     }
2925 
2926     return 0;
2927 }
2928 
2929 static int virtio_net_vnet_pre_save(void *opaque)
2930 {
2931     struct VirtIONetMigTmp *tmp = opaque;
2932 
2933     tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
2934 
2935     return 0;
2936 }
2937 
2938 static const VMStateDescription vmstate_virtio_net_has_vnet = {
2939     .name      = "virtio-net-vnet",
2940     .post_load = virtio_net_vnet_post_load,
2941     .pre_save  = virtio_net_vnet_pre_save,
2942     .fields    = (VMStateField[]) {
2943         VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
2944         VMSTATE_END_OF_LIST()
2945     },
2946 };
2947 
2948 static bool virtio_net_rss_needed(void *opaque)
2949 {
2950     return VIRTIO_NET(opaque)->rss_data.enabled;
2951 }
2952 
2953 static const VMStateDescription vmstate_virtio_net_rss = {
2954     .name      = "virtio-net-device/rss",
2955     .version_id = 1,
2956     .minimum_version_id = 1,
2957     .needed = virtio_net_rss_needed,
2958     .fields = (VMStateField[]) {
2959         VMSTATE_BOOL(rss_data.enabled, VirtIONet),
2960         VMSTATE_BOOL(rss_data.redirect, VirtIONet),
2961         VMSTATE_BOOL(rss_data.populate_hash, VirtIONet),
2962         VMSTATE_UINT32(rss_data.hash_types, VirtIONet),
2963         VMSTATE_UINT16(rss_data.indirections_len, VirtIONet),
2964         VMSTATE_UINT16(rss_data.default_queue, VirtIONet),
2965         VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet,
2966                             VIRTIO_NET_RSS_MAX_KEY_SIZE),
2967         VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet,
2968                                     rss_data.indirections_len, 0,
2969                                     vmstate_info_uint16, uint16_t),
2970         VMSTATE_END_OF_LIST()
2971     },
2972 };
2973 
2974 static const VMStateDescription vmstate_virtio_net_device = {
2975     .name = "virtio-net-device",
2976     .version_id = VIRTIO_NET_VM_VERSION,
2977     .minimum_version_id = VIRTIO_NET_VM_VERSION,
2978     .post_load = virtio_net_post_load_device,
2979     .fields = (VMStateField[]) {
2980         VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
2981         VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
2982                                vmstate_virtio_net_queue_tx_waiting,
2983                                VirtIONetQueue),
2984         VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
2985         VMSTATE_UINT16(status, VirtIONet),
2986         VMSTATE_UINT8(promisc, VirtIONet),
2987         VMSTATE_UINT8(allmulti, VirtIONet),
2988         VMSTATE_UINT32(mac_table.in_use, VirtIONet),
2989 
2990         /* Guarded pair: If it fits we load it, else we throw it away
2991          * - can happen if source has a larger MAC table.; post-load
2992          *  sets flags in this case.
2993          */
2994         VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
2995                                 0, mac_table_fits, mac_table.in_use,
2996                                  ETH_ALEN),
2997         VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
2998                                      mac_table.in_use, ETH_ALEN),
2999 
3000         /* Note: This is an array of uint32's that's always been saved as a
3001          * buffer; hold onto your endiannesses; it's actually used as a bitmap
3002          * but based on the uint.
3003          */
3004         VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
3005         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3006                          vmstate_virtio_net_has_vnet),
3007         VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
3008         VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
3009         VMSTATE_UINT8(alluni, VirtIONet),
3010         VMSTATE_UINT8(nomulti, VirtIONet),
3011         VMSTATE_UINT8(nouni, VirtIONet),
3012         VMSTATE_UINT8(nobcast, VirtIONet),
3013         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3014                          vmstate_virtio_net_has_ufo),
3015         VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0,
3016                             vmstate_info_uint16_equal, uint16_t),
3017         VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1),
3018         VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3019                          vmstate_virtio_net_tx_waiting),
3020         VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
3021                             has_ctrl_guest_offloads),
3022         VMSTATE_END_OF_LIST()
3023    },
3024     .subsections = (const VMStateDescription * []) {
3025         &vmstate_virtio_net_rss,
3026         NULL
3027     }
3028 };
3029 
3030 static NetClientInfo net_virtio_info = {
3031     .type = NET_CLIENT_DRIVER_NIC,
3032     .size = sizeof(NICState),
3033     .can_receive = virtio_net_can_receive,
3034     .receive = virtio_net_receive,
3035     .link_status_changed = virtio_net_set_link_status,
3036     .query_rx_filter = virtio_net_query_rxfilter,
3037     .announce = virtio_net_announce,
3038 };
3039 
3040 static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
3041 {
3042     VirtIONet *n = VIRTIO_NET(vdev);
3043     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
3044     assert(n->vhost_started);
3045     return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
3046 }
3047 
3048 static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
3049                                            bool mask)
3050 {
3051     VirtIONet *n = VIRTIO_NET(vdev);
3052     NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
3053     assert(n->vhost_started);
3054     vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
3055                              vdev, idx, mask);
3056 }
3057 
3058 static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
3059 {
3060     virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
3061 
3062     n->config_size = virtio_feature_get_config_size(feature_sizes,
3063                                                     host_features);
3064 }
3065 
3066 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
3067                                    const char *type)
3068 {
3069     /*
3070      * The name can be NULL, the netclient name will be type.x.
3071      */
3072     assert(type != NULL);
3073 
3074     g_free(n->netclient_name);
3075     g_free(n->netclient_type);
3076     n->netclient_name = g_strdup(name);
3077     n->netclient_type = g_strdup(type);
3078 }
3079 
3080 static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev)
3081 {
3082     HotplugHandler *hotplug_ctrl;
3083     PCIDevice *pci_dev;
3084     Error *err = NULL;
3085 
3086     hotplug_ctrl = qdev_get_hotplug_handler(dev);
3087     if (hotplug_ctrl) {
3088         pci_dev = PCI_DEVICE(dev);
3089         pci_dev->partially_hotplugged = true;
3090         hotplug_handler_unplug_request(hotplug_ctrl, dev, &err);
3091         if (err) {
3092             error_report_err(err);
3093             return false;
3094         }
3095     } else {
3096         return false;
3097     }
3098     return true;
3099 }
3100 
3101 static bool failover_replug_primary(VirtIONet *n, DeviceState *dev,
3102                                     Error **errp)
3103 {
3104     Error *err = NULL;
3105     HotplugHandler *hotplug_ctrl;
3106     PCIDevice *pdev = PCI_DEVICE(dev);
3107     BusState *primary_bus;
3108 
3109     if (!pdev->partially_hotplugged) {
3110         return true;
3111     }
3112     primary_bus = dev->parent_bus;
3113     if (!primary_bus) {
3114         error_setg(errp, "virtio_net: couldn't find primary bus");
3115         return false;
3116     }
3117     qdev_set_parent_bus(dev, primary_bus, &error_abort);
3118     qatomic_set(&n->failover_primary_hidden, false);
3119     hotplug_ctrl = qdev_get_hotplug_handler(dev);
3120     if (hotplug_ctrl) {
3121         hotplug_handler_pre_plug(hotplug_ctrl, dev, &err);
3122         if (err) {
3123             goto out;
3124         }
3125         hotplug_handler_plug(hotplug_ctrl, dev, &err);
3126     }
3127 
3128 out:
3129     error_propagate(errp, err);
3130     return !err;
3131 }
3132 
3133 static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationState *s)
3134 {
3135     bool should_be_hidden;
3136     Error *err = NULL;
3137     DeviceState *dev = failover_find_primary_device(n);
3138 
3139     if (!dev) {
3140         return;
3141     }
3142 
3143     should_be_hidden = qatomic_read(&n->failover_primary_hidden);
3144 
3145     if (migration_in_setup(s) && !should_be_hidden) {
3146         if (failover_unplug_primary(n, dev)) {
3147             vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
3148             qapi_event_send_unplug_primary(dev->id);
3149             qatomic_set(&n->failover_primary_hidden, true);
3150         } else {
3151             warn_report("couldn't unplug primary device");
3152         }
3153     } else if (migration_has_failed(s)) {
3154         /* We already unplugged the device let's plug it back */
3155         if (!failover_replug_primary(n, dev, &err)) {
3156             if (err) {
3157                 error_report_err(err);
3158             }
3159         }
3160     }
3161 }
3162 
3163 static void virtio_net_migration_state_notifier(Notifier *notifier, void *data)
3164 {
3165     MigrationState *s = data;
3166     VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
3167     virtio_net_handle_migration_primary(n, s);
3168 }
3169 
3170 static bool failover_hide_primary_device(DeviceListener *listener,
3171                                          QemuOpts *device_opts)
3172 {
3173     VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
3174     const char *standby_id;
3175 
3176     if (!device_opts) {
3177         return false;
3178     }
3179     standby_id = qemu_opt_get(device_opts, "failover_pair_id");
3180     if (g_strcmp0(standby_id, n->netclient_name) != 0) {
3181         return false;
3182     }
3183 
3184     /* failover_primary_hidden is set during feature negotiation */
3185     return qatomic_read(&n->failover_primary_hidden);
3186 }
3187 
3188 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
3189 {
3190     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3191     VirtIONet *n = VIRTIO_NET(dev);
3192     NetClientState *nc;
3193     int i;
3194 
3195     if (n->net_conf.mtu) {
3196         n->host_features |= (1ULL << VIRTIO_NET_F_MTU);
3197     }
3198 
3199     if (n->net_conf.duplex_str) {
3200         if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) {
3201             n->net_conf.duplex = DUPLEX_HALF;
3202         } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) {
3203             n->net_conf.duplex = DUPLEX_FULL;
3204         } else {
3205             error_setg(errp, "'duplex' must be 'half' or 'full'");
3206             return;
3207         }
3208         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
3209     } else {
3210         n->net_conf.duplex = DUPLEX_UNKNOWN;
3211     }
3212 
3213     if (n->net_conf.speed < SPEED_UNKNOWN) {
3214         error_setg(errp, "'speed' must be between 0 and INT_MAX");
3215         return;
3216     }
3217     if (n->net_conf.speed >= 0) {
3218         n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
3219     }
3220 
3221     if (n->failover) {
3222         n->primary_listener.hide_device = failover_hide_primary_device;
3223         qatomic_set(&n->failover_primary_hidden, true);
3224         device_listener_register(&n->primary_listener);
3225         n->migration_state.notify = virtio_net_migration_state_notifier;
3226         add_migration_state_change_notifier(&n->migration_state);
3227         n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
3228     }
3229 
3230     virtio_net_set_config_size(n, n->host_features);
3231     virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
3232 
3233     /*
3234      * We set a lower limit on RX queue size to what it always was.
3235      * Guests that want a smaller ring can always resize it without
3236      * help from us (using virtio 1 and up).
3237      */
3238     if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
3239         n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
3240         !is_power_of_2(n->net_conf.rx_queue_size)) {
3241         error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
3242                    "must be a power of 2 between %d and %d.",
3243                    n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
3244                    VIRTQUEUE_MAX_SIZE);
3245         virtio_cleanup(vdev);
3246         return;
3247     }
3248 
3249     if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
3250         n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE ||
3251         !is_power_of_2(n->net_conf.tx_queue_size)) {
3252         error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
3253                    "must be a power of 2 between %d and %d",
3254                    n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
3255                    VIRTQUEUE_MAX_SIZE);
3256         virtio_cleanup(vdev);
3257         return;
3258     }
3259 
3260     n->max_queues = MAX(n->nic_conf.peers.queues, 1);
3261     if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
3262         error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
3263                    "must be a positive integer less than %d.",
3264                    n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
3265         virtio_cleanup(vdev);
3266         return;
3267     }
3268     n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
3269     n->curr_queues = 1;
3270     n->tx_timeout = n->net_conf.txtimer;
3271 
3272     if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
3273                        && strcmp(n->net_conf.tx, "bh")) {
3274         warn_report("virtio-net: "
3275                     "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
3276                     n->net_conf.tx);
3277         error_printf("Defaulting to \"bh\"");
3278     }
3279 
3280     n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
3281                                     n->net_conf.tx_queue_size);
3282 
3283     for (i = 0; i < n->max_queues; i++) {
3284         virtio_net_add_queue(n, i);
3285     }
3286 
3287     n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
3288     qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
3289     memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
3290     n->status = VIRTIO_NET_S_LINK_UP;
3291     qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
3292                               QEMU_CLOCK_VIRTUAL,
3293                               virtio_net_announce_timer, n);
3294     n->announce_timer.round = 0;
3295 
3296     if (n->netclient_type) {
3297         /*
3298          * Happen when virtio_net_set_netclient_name has been called.
3299          */
3300         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3301                               n->netclient_type, n->netclient_name, n);
3302     } else {
3303         n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
3304                               object_get_typename(OBJECT(dev)), dev->id, n);
3305     }
3306 
3307     peer_test_vnet_hdr(n);
3308     if (peer_has_vnet_hdr(n)) {
3309         for (i = 0; i < n->max_queues; i++) {
3310             qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
3311         }
3312         n->host_hdr_len = sizeof(struct virtio_net_hdr);
3313     } else {
3314         n->host_hdr_len = 0;
3315     }
3316 
3317     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
3318 
3319     n->vqs[0].tx_waiting = 0;
3320     n->tx_burst = n->net_conf.txburst;
3321     virtio_net_set_mrg_rx_bufs(n, 0, 0, 0);
3322     n->promisc = 1; /* for compatibility */
3323 
3324     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
3325 
3326     n->vlans = g_malloc0(MAX_VLAN >> 3);
3327 
3328     nc = qemu_get_queue(n->nic);
3329     nc->rxfilter_notify_enabled = 1;
3330 
3331    if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
3332         struct virtio_net_config netcfg = {};
3333         memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
3334         vhost_net_set_config(get_vhost_net(nc->peer),
3335             (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_MASTER);
3336     }
3337     QTAILQ_INIT(&n->rsc_chains);
3338     n->qdev = dev;
3339 
3340     net_rx_pkt_init(&n->rx_pkt, false);
3341 }
3342 
3343 static void virtio_net_device_unrealize(DeviceState *dev)
3344 {
3345     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3346     VirtIONet *n = VIRTIO_NET(dev);
3347     int i, max_queues;
3348 
3349     /* This will stop vhost backend if appropriate. */
3350     virtio_net_set_status(vdev, 0);
3351 
3352     g_free(n->netclient_name);
3353     n->netclient_name = NULL;
3354     g_free(n->netclient_type);
3355     n->netclient_type = NULL;
3356 
3357     g_free(n->mac_table.macs);
3358     g_free(n->vlans);
3359 
3360     if (n->failover) {
3361         device_listener_unregister(&n->primary_listener);
3362     }
3363 
3364     max_queues = n->multiqueue ? n->max_queues : 1;
3365     for (i = 0; i < max_queues; i++) {
3366         virtio_net_del_queue(n, i);
3367     }
3368     /* delete also control vq */
3369     virtio_del_queue(vdev, max_queues * 2);
3370     qemu_announce_timer_del(&n->announce_timer, false);
3371     g_free(n->vqs);
3372     qemu_del_nic(n->nic);
3373     virtio_net_rsc_cleanup(n);
3374     g_free(n->rss_data.indirections_table);
3375     net_rx_pkt_uninit(n->rx_pkt);
3376     virtio_cleanup(vdev);
3377 }
3378 
3379 static void virtio_net_instance_init(Object *obj)
3380 {
3381     VirtIONet *n = VIRTIO_NET(obj);
3382 
3383     /*
3384      * The default config_size is sizeof(struct virtio_net_config).
3385      * Can be overriden with virtio_net_set_config_size.
3386      */
3387     n->config_size = sizeof(struct virtio_net_config);
3388     device_add_bootindex_property(obj, &n->nic_conf.bootindex,
3389                                   "bootindex", "/ethernet-phy@0",
3390                                   DEVICE(n));
3391 }
3392 
3393 static int virtio_net_pre_save(void *opaque)
3394 {
3395     VirtIONet *n = opaque;
3396 
3397     /* At this point, backend must be stopped, otherwise
3398      * it might keep writing to memory. */
3399     assert(!n->vhost_started);
3400 
3401     return 0;
3402 }
3403 
3404 static bool primary_unplug_pending(void *opaque)
3405 {
3406     DeviceState *dev = opaque;
3407     DeviceState *primary;
3408     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3409     VirtIONet *n = VIRTIO_NET(vdev);
3410 
3411     if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3412         return false;
3413     }
3414     primary = failover_find_primary_device(n);
3415     return primary ? primary->pending_deleted_event : false;
3416 }
3417 
3418 static bool dev_unplug_pending(void *opaque)
3419 {
3420     DeviceState *dev = opaque;
3421     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
3422 
3423     return vdc->primary_unplug_pending(dev);
3424 }
3425 
3426 static const VMStateDescription vmstate_virtio_net = {
3427     .name = "virtio-net",
3428     .minimum_version_id = VIRTIO_NET_VM_VERSION,
3429     .version_id = VIRTIO_NET_VM_VERSION,
3430     .fields = (VMStateField[]) {
3431         VMSTATE_VIRTIO_DEVICE,
3432         VMSTATE_END_OF_LIST()
3433     },
3434     .pre_save = virtio_net_pre_save,
3435     .dev_unplug_pending = dev_unplug_pending,
3436 };
3437 
3438 static Property virtio_net_properties[] = {
3439     DEFINE_PROP_BIT64("csum", VirtIONet, host_features,
3440                     VIRTIO_NET_F_CSUM, true),
3441     DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features,
3442                     VIRTIO_NET_F_GUEST_CSUM, true),
3443     DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
3444     DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features,
3445                     VIRTIO_NET_F_GUEST_TSO4, true),
3446     DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features,
3447                     VIRTIO_NET_F_GUEST_TSO6, true),
3448     DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features,
3449                     VIRTIO_NET_F_GUEST_ECN, true),
3450     DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features,
3451                     VIRTIO_NET_F_GUEST_UFO, true),
3452     DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features,
3453                     VIRTIO_NET_F_GUEST_ANNOUNCE, true),
3454     DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features,
3455                     VIRTIO_NET_F_HOST_TSO4, true),
3456     DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features,
3457                     VIRTIO_NET_F_HOST_TSO6, true),
3458     DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features,
3459                     VIRTIO_NET_F_HOST_ECN, true),
3460     DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features,
3461                     VIRTIO_NET_F_HOST_UFO, true),
3462     DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features,
3463                     VIRTIO_NET_F_MRG_RXBUF, true),
3464     DEFINE_PROP_BIT64("status", VirtIONet, host_features,
3465                     VIRTIO_NET_F_STATUS, true),
3466     DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features,
3467                     VIRTIO_NET_F_CTRL_VQ, true),
3468     DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features,
3469                     VIRTIO_NET_F_CTRL_RX, true),
3470     DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features,
3471                     VIRTIO_NET_F_CTRL_VLAN, true),
3472     DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features,
3473                     VIRTIO_NET_F_CTRL_RX_EXTRA, true),
3474     DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features,
3475                     VIRTIO_NET_F_CTRL_MAC_ADDR, true),
3476     DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features,
3477                     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
3478     DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
3479     DEFINE_PROP_BIT64("rss", VirtIONet, host_features,
3480                     VIRTIO_NET_F_RSS, false),
3481     DEFINE_PROP_BIT64("hash", VirtIONet, host_features,
3482                     VIRTIO_NET_F_HASH_REPORT, false),
3483     DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features,
3484                     VIRTIO_NET_F_RSC_EXT, false),
3485     DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout,
3486                        VIRTIO_NET_RSC_DEFAULT_INTERVAL),
3487     DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
3488     DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
3489                        TX_TIMER_INTERVAL),
3490     DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
3491     DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
3492     DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
3493                        VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
3494     DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
3495                        VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
3496     DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
3497     DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
3498                      true),
3499     DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN),
3500     DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str),
3501     DEFINE_PROP_BOOL("failover", VirtIONet, failover, false),
3502     DEFINE_PROP_END_OF_LIST(),
3503 };
3504 
3505 static void virtio_net_class_init(ObjectClass *klass, void *data)
3506 {
3507     DeviceClass *dc = DEVICE_CLASS(klass);
3508     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
3509 
3510     device_class_set_props(dc, virtio_net_properties);
3511     dc->vmsd = &vmstate_virtio_net;
3512     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
3513     vdc->realize = virtio_net_device_realize;
3514     vdc->unrealize = virtio_net_device_unrealize;
3515     vdc->get_config = virtio_net_get_config;
3516     vdc->set_config = virtio_net_set_config;
3517     vdc->get_features = virtio_net_get_features;
3518     vdc->set_features = virtio_net_set_features;
3519     vdc->bad_features = virtio_net_bad_features;
3520     vdc->reset = virtio_net_reset;
3521     vdc->set_status = virtio_net_set_status;
3522     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
3523     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
3524     vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
3525     vdc->post_load = virtio_net_post_load_virtio;
3526     vdc->vmsd = &vmstate_virtio_net_device;
3527     vdc->primary_unplug_pending = primary_unplug_pending;
3528 }
3529 
3530 static const TypeInfo virtio_net_info = {
3531     .name = TYPE_VIRTIO_NET,
3532     .parent = TYPE_VIRTIO_DEVICE,
3533     .instance_size = sizeof(VirtIONet),
3534     .instance_init = virtio_net_instance_init,
3535     .class_init = virtio_net_class_init,
3536 };
3537 
3538 static void virtio_register_types(void)
3539 {
3540     type_register_static(&virtio_net_info);
3541 }
3542 
3543 type_init(virtio_register_types)
3544