]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/net/virtio-net.c
Merge tag 'pull-request-2025-07-02' of https://gitlab.com/thuth/qemu into staging
[thirdparty/qemu.git] / hw / net / virtio-net.c
CommitLineData
fbe78f4f
AL
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
9b8bfe21 14#include "qemu/osdep.h"
9711cd0d 15#include "qemu/atomic.h"
1de7afc9 16#include "qemu/iov.h"
68b0a639 17#include "qemu/log.h"
db725815 18#include "qemu/main-loop.h"
0b8fa32f 19#include "qemu/module.h"
0d09e41a 20#include "hw/virtio/virtio.h"
1422e32d 21#include "net/net.h"
7200ac3c 22#include "net/checksum.h"
a8ed73f7 23#include "net/tap.h"
1de7afc9
PB
24#include "qemu/error-report.h"
25#include "qemu/timer.h"
9711cd0d
JF
26#include "qemu/option.h"
27#include "qemu/option_int.h"
28#include "qemu/config-file.h"
407bc4bf 29#include "qobject/qdict.h"
0d09e41a
PB
30#include "hw/virtio/virtio-net.h"
31#include "net/vhost_net.h"
9d8c6a25 32#include "net/announce.h"
17ec5a86 33#include "hw/virtio/virtio-bus.h"
e688df6b 34#include "qapi/error.h"
9af23989 35#include "qapi/qapi-events-net.h"
a27bd6c7 36#include "hw/qdev-properties.h"
9711cd0d
JF
37#include "qapi/qapi-types-migration.h"
38#include "qapi/qapi-events-migration.h"
1399c60d 39#include "hw/virtio/virtio-access.h"
f8d806c9 40#include "migration/misc.h"
9473939e 41#include "standard-headers/linux/ethtool.h"
32cad1ff
PMD
42#include "system/system.h"
43#include "system/replay.h"
9d8c6a25 44#include "trace.h"
9711cd0d 45#include "monitor/qdev.h"
6b230b7d 46#include "monitor/monitor.h"
edf5ca5d 47#include "hw/pci/pci_device.h"
4474e37a 48#include "net_rx_pkt.h"
108a6481 49#include "hw/virtio/vhost.h"
32cad1ff 50#include "system/qtest.h"
fbe78f4f 51
0ce0e8f4 52#define VIRTIO_NET_VM_VERSION 11
b6503ed9 53
1c0fbfa3
MT
54/* previously fixed value */
55#define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
9b02e161
WW
56#define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256
57
441537f1 58/* for now, only allow larger queue_pairs; with virtio-1, guest can downsize */
1c0fbfa3 59#define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
9b02e161 60#define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
1c0fbfa3 61
2974e916
YB
62#define VIRTIO_NET_IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */
63
64#define VIRTIO_NET_TCP_FLAG 0x3F
65#define VIRTIO_NET_TCP_HDR_LENGTH 0xF000
66
67/* IPv4 max payload, 16 bits in the header */
68#define VIRTIO_NET_MAX_IP4_PAYLOAD (65535 - sizeof(struct ip_header))
69#define VIRTIO_NET_MAX_TCP_PAYLOAD 65535
70
71/* header length value in ip header without option */
72#define VIRTIO_NET_IP4_HEADER_LENGTH 5
73
74#define VIRTIO_NET_IP6_ADDR_SIZE 32 /* ipv6 saddr + daddr */
75#define VIRTIO_NET_MAX_IP6_PAYLOAD VIRTIO_NET_MAX_TCP_PAYLOAD
76
77/* Purge coalesced packets timer interval, This value affects the performance
78 a lot, and should be tuned carefully, '300000'(300us) is the recommended
79 value to pass the WHQL test, '50000' can gain 2x netperf throughput with
80 tso/gso/gro 'off'. */
81#define VIRTIO_NET_RSC_DEFAULT_INTERVAL 300000
82
59079029
YB
83#define VIRTIO_NET_RSS_SUPPORTED_HASHES (VIRTIO_NET_RSS_HASH_TYPE_IPv4 | \
84 VIRTIO_NET_RSS_HASH_TYPE_TCPv4 | \
85 VIRTIO_NET_RSS_HASH_TYPE_UDPv4 | \
86 VIRTIO_NET_RSS_HASH_TYPE_IPv6 | \
87 VIRTIO_NET_RSS_HASH_TYPE_TCPv6 | \
88 VIRTIO_NET_RSS_HASH_TYPE_UDPv6 | \
89 VIRTIO_NET_RSS_HASH_TYPE_IP_EX | \
90 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX | \
91 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX)
92
ad6461ad 93static const VirtIOFeature feature_sizes[] = {
127833ee 94 {.flags = 1ULL << VIRTIO_NET_F_MAC,
5d5b33c0 95 .end = endof(struct virtio_net_config, mac)},
127833ee 96 {.flags = 1ULL << VIRTIO_NET_F_STATUS,
5d5b33c0 97 .end = endof(struct virtio_net_config, status)},
127833ee 98 {.flags = 1ULL << VIRTIO_NET_F_MQ,
5d5b33c0 99 .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
127833ee 100 {.flags = 1ULL << VIRTIO_NET_F_MTU,
5d5b33c0 101 .end = endof(struct virtio_net_config, mtu)},
9473939e 102 {.flags = 1ULL << VIRTIO_NET_F_SPEED_DUPLEX,
5d5b33c0 103 .end = endof(struct virtio_net_config, duplex)},
e22f0603 104 {.flags = (1ULL << VIRTIO_NET_F_RSS) | (1ULL << VIRTIO_NET_F_HASH_REPORT),
59079029 105 .end = endof(struct virtio_net_config, supported_hash_types)},
14f9b664
JL
106 {}
107};
108
d74c30c8
DT
109static const VirtIOConfigSizeParams cfg_size_params = {
110 .min_size = endof(struct virtio_net_config, mac),
111 .max_size = sizeof(struct virtio_net_config),
112 .feature_sizes = feature_sizes
113};
114
fed699f9 115static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
0c87e93e
JW
116{
117 VirtIONet *n = qemu_get_nic_opaque(nc);
118
fed699f9 119 return &n->vqs[nc->queue_index];
0c87e93e 120}
fed699f9
JW
121
122static int vq2q(int queue_index)
123{
124 return queue_index / 2;
125}
126
4fdf69ab
KX
127static void flush_or_purge_queued_packets(NetClientState *nc)
128{
129 if (!nc->peer) {
130 return;
131 }
132
133 qemu_flush_or_purge_queued_packets(nc->peer, true);
134 assert(!virtio_net_get_subqueue(nc)->async_tx.elem);
135}
136
fbe78f4f
AL
137/* TODO
138 * - we could suppress RX interrupt if we were so inclined.
139 */
140
0f03eca6 141static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
fbe78f4f 142{
17a0ca55 143 VirtIONet *n = VIRTIO_NET(vdev);
fbe78f4f 144 struct virtio_net_config netcfg;
c546ecf2 145 NetClientState *nc = qemu_get_queue(n->nic);
fb592882 146 static const MACAddr zero = { .a = { 0, 0, 0, 0, 0, 0 } };
fbe78f4f 147
108a6481
CL
148 int ret = 0;
149 memset(&netcfg, 0 , sizeof(struct virtio_net_config));
1399c60d 150 virtio_stw_p(vdev, &netcfg.status, n->status);
441537f1 151 virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queue_pairs);
a93e599d 152 virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu);
79674068 153 memcpy(netcfg.mac, n->mac, ETH_ALEN);
9473939e
JB
154 virtio_stl_p(vdev, &netcfg.speed, n->net_conf.speed);
155 netcfg.duplex = n->net_conf.duplex;
59079029
YB
156 netcfg.rss_max_key_size = VIRTIO_NET_RSS_MAX_KEY_SIZE;
157 virtio_stw_p(vdev, &netcfg.rss_max_indirection_table_length,
e22f0603
YB
158 virtio_host_has_feature(vdev, VIRTIO_NET_F_RSS) ?
159 VIRTIO_NET_RSS_MAX_TABLE_LEN : 1);
59079029
YB
160 virtio_stl_p(vdev, &netcfg.supported_hash_types,
161 VIRTIO_NET_RSS_SUPPORTED_HASHES);
14f9b664 162 memcpy(config, &netcfg, n->config_size);
108a6481 163
c546ecf2
JW
164 /*
165 * Is this VDPA? No peer means not VDPA: there's no way to
166 * disconnect/reconnect a VDPA peer.
167 */
168 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
108a6481 169 ret = vhost_net_get_config(get_vhost_net(nc->peer), (uint8_t *)&netcfg,
c546ecf2 170 n->config_size);
ebc141a6
EP
171 if (ret == -1) {
172 return;
c546ecf2 173 }
ebc141a6
EP
174
175 /*
176 * Some NIC/kernel combinations present 0 as the mac address. As that
177 * is not a legal address, try to proceed with the address from the
178 * QEMU command line in the hope that the address has been configured
179 * correctly elsewhere - just not reported by the device.
180 */
181 if (memcmp(&netcfg.mac, &zero, sizeof(zero)) == 0) {
182 info_report("Zero hardware mac address detected. Ignoring.");
183 memcpy(netcfg.mac, n->mac, ETH_ALEN);
184 }
185
4f93aafc
EP
186 netcfg.status |= virtio_tswap16(vdev,
187 n->status & VIRTIO_NET_S_ANNOUNCE);
ebc141a6 188 memcpy(config, &netcfg, n->config_size);
108a6481 189 }
fbe78f4f
AL
190}
191
0f03eca6
AL
192static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
193{
17a0ca55 194 VirtIONet *n = VIRTIO_NET(vdev);
14f9b664 195 struct virtio_net_config netcfg = {};
c546ecf2 196 NetClientState *nc = qemu_get_queue(n->nic);
0f03eca6 197
14f9b664 198 memcpy(&netcfg, config, n->config_size);
0f03eca6 199
95129d6f
CH
200 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
201 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
c1943a3f 202 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
79674068 203 memcpy(n->mac, netcfg.mac, ETH_ALEN);
b356f76d 204 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
0f03eca6 205 }
108a6481 206
c546ecf2
JW
207 /*
208 * Is this VDPA? No peer means not VDPA: there's no way to
209 * disconnect/reconnect a VDPA peer.
210 */
211 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
212 vhost_net_set_config(get_vhost_net(nc->peer),
213 (uint8_t *)&netcfg, 0, n->config_size,
f8ed3648 214 VHOST_SET_CONFIG_TYPE_FRONTEND);
108a6481 215 }
0f03eca6
AL
216}
217
783e7706
MT
218static bool virtio_net_started(VirtIONet *n, uint8_t status)
219{
17a0ca55 220 VirtIODevice *vdev = VIRTIO_DEVICE(n);
783e7706 221 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
17a0ca55 222 (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
783e7706
MT
223}
224
b2c929f0
DDAG
225static void virtio_net_announce_notify(VirtIONet *net)
226{
227 VirtIODevice *vdev = VIRTIO_DEVICE(net);
228 trace_virtio_net_announce_notify();
229
230 net->status |= VIRTIO_NET_S_ANNOUNCE;
231 virtio_notify_config(vdev);
232}
233
f57fcf70
JW
234static void virtio_net_announce_timer(void *opaque)
235{
236 VirtIONet *n = opaque;
9d8c6a25 237 trace_virtio_net_announce_timer(n->announce_timer.round);
f57fcf70 238
9d8c6a25 239 n->announce_timer.round--;
b2c929f0
DDAG
240 virtio_net_announce_notify(n);
241}
242
243static void virtio_net_announce(NetClientState *nc)
244{
245 VirtIONet *n = qemu_get_nic_opaque(nc);
246 VirtIODevice *vdev = VIRTIO_DEVICE(n);
247
248 /*
249 * Make sure the virtio migration announcement timer isn't running
250 * If it is, let it trigger announcement so that we do not cause
251 * confusion.
252 */
253 if (n->announce_timer.round) {
254 return;
255 }
256
257 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
258 virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
259 virtio_net_announce_notify(n);
260 }
f57fcf70
JW
261}
262
783e7706 263static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
afbaa7b4 264{
17a0ca55 265 VirtIODevice *vdev = VIRTIO_DEVICE(n);
b356f76d 266 NetClientState *nc = qemu_get_queue(n->nic);
441537f1 267 int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
aa858194
SWL
268 int cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ?
269 n->max_ncs - n->max_queue_pairs : 0;
b356f76d 270
ed8b4afe 271 if (!get_vhost_net(nc->peer)) {
afbaa7b4
MT
272 return;
273 }
fed699f9 274
8c1ac475
RK
275 if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
276 !!n->vhost_started) {
afbaa7b4
MT
277 return;
278 }
279 if (!n->vhost_started) {
086abc1c
MT
280 int r, i;
281
1bfa316c
GK
282 if (n->needs_vnet_hdr_swap) {
283 error_report("backend does not support %s vnet headers; "
284 "falling back on userspace virtio",
285 virtio_is_big_endian(vdev) ? "BE" : "LE");
286 return;
287 }
288
086abc1c
MT
289 /* Any packets outstanding? Purge them to avoid touching rings
290 * when vhost is running.
291 */
441537f1 292 for (i = 0; i < queue_pairs; i++) {
086abc1c
MT
293 NetClientState *qnc = qemu_get_subqueue(n->nic, i);
294
295 /* Purge both directions: TX and RX. */
296 qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
297 qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
298 }
299
a93e599d
MC
300 if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) {
301 r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu);
302 if (r < 0) {
303 error_report("%uBytes MTU not supported by the backend",
304 n->net_conf.mtu);
305
306 return;
307 }
308 }
309
1830b80f 310 n->vhost_started = 1;
22288fe5 311 r = vhost_net_start(vdev, n->nic->ncs, queue_pairs, cvq);
afbaa7b4 312 if (r < 0) {
e7b43f7e
SH
313 error_report("unable to start vhost net: %d: "
314 "falling back on userspace virtio", -r);
1830b80f 315 n->vhost_started = 0;
afbaa7b4
MT
316 }
317 } else {
22288fe5 318 vhost_net_stop(vdev, n->nic->ncs, queue_pairs, cvq);
afbaa7b4
MT
319 n->vhost_started = 0;
320 }
321}
322
1bfa316c
GK
323static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
324 NetClientState *peer,
325 bool enable)
326{
327 if (virtio_is_big_endian(vdev)) {
328 return qemu_set_vnet_be(peer, enable);
329 } else {
330 return qemu_set_vnet_le(peer, enable);
331 }
332}
333
334static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
441537f1 335 int queue_pairs, bool enable)
1bfa316c
GK
336{
337 int i;
338
441537f1 339 for (i = 0; i < queue_pairs; i++) {
1bfa316c
GK
340 if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
341 enable) {
342 while (--i >= 0) {
343 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
344 }
345
346 return true;
347 }
348 }
349
350 return false;
351}
352
353static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
354{
355 VirtIODevice *vdev = VIRTIO_DEVICE(n);
441537f1 356 int queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
1bfa316c
GK
357
358 if (virtio_net_started(n, status)) {
359 /* Before using the device, we tell the network backend about the
360 * endianness to use when parsing vnet headers. If the backend
361 * can't do it, we fallback onto fixing the headers in the core
362 * virtio-net code.
363 */
ad57f700
AO
364 n->needs_vnet_hdr_swap = n->has_vnet_hdr &&
365 virtio_net_set_vnet_endian(vdev, n->nic->ncs,
441537f1 366 queue_pairs, true);
1bfa316c
GK
367 } else if (virtio_net_started(n, vdev->status)) {
368 /* After using the device, we need to reset the network backend to
369 * the default (guest native endianness), otherwise the guest may
370 * lose network connectivity if it is rebooted into a different
371 * endianness.
372 */
441537f1 373 virtio_net_set_vnet_endian(vdev, n->nic->ncs, queue_pairs, false);
1bfa316c
GK
374 }
375}
376
283e2c2a
YB
377static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
378{
379 unsigned int dropped = virtqueue_drop_all(vq);
380 if (dropped) {
381 virtio_notify(vdev, vq);
382 }
383}
384
bc85aae4 385static int virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
783e7706 386{
17a0ca55 387 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9
JW
388 VirtIONetQueue *q;
389 int i;
390 uint8_t queue_status;
783e7706 391
1bfa316c 392 virtio_net_vnet_endian_status(n, status);
783e7706
MT
393 virtio_net_vhost_status(n, status);
394
441537f1 395 for (i = 0; i < n->max_queue_pairs; i++) {
38705bb5
FZ
396 NetClientState *ncs = qemu_get_subqueue(n->nic, i);
397 bool queue_started;
fed699f9 398 q = &n->vqs[i];
783e7706 399
441537f1 400 if ((!n->multiqueue && i != 0) || i >= n->curr_queue_pairs) {
fed699f9 401 queue_status = 0;
783e7706 402 } else {
fed699f9 403 queue_status = status;
783e7706 404 }
38705bb5
FZ
405 queue_started =
406 virtio_net_started(n, queue_status) && !n->vhost_started;
407
408 if (queue_started) {
409 qemu_flush_queued_packets(ncs);
410 }
fed699f9
JW
411
412 if (!q->tx_waiting) {
413 continue;
414 }
415
38705bb5 416 if (queue_started) {
fed699f9 417 if (q->tx_timer) {
bc72ad67
AB
418 timer_mod(q->tx_timer,
419 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
fed699f9 420 } else {
a0bf401b 421 replay_bh_schedule_event(q->tx_bh);
fed699f9 422 }
783e7706 423 } else {
fed699f9 424 if (q->tx_timer) {
bc72ad67 425 timer_del(q->tx_timer);
fed699f9
JW
426 } else {
427 qemu_bh_cancel(q->tx_bh);
428 }
283e2c2a 429 if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 &&
70e53e6e
JW
430 (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) &&
431 vdev->vm_running) {
283e2c2a
YB
432 /* if tx is waiting we are likely have some packets in tx queue
433 * and disabled notification */
434 q->tx_waiting = 0;
435 virtio_queue_set_notification(q->tx_vq, 1);
436 virtio_net_drop_tx_queue_data(vdev, q->tx_vq);
437 }
783e7706
MT
438 }
439 }
bc85aae4 440 return 0;
783e7706
MT
441}
442
4e68f7a0 443static void virtio_net_set_link_status(NetClientState *nc)
554c97dd 444{
cc1f0f45 445 VirtIONet *n = qemu_get_nic_opaque(nc);
17a0ca55 446 VirtIODevice *vdev = VIRTIO_DEVICE(n);
554c97dd
AL
447 uint16_t old_status = n->status;
448
eb6b6c12 449 if (nc->link_down)
554c97dd
AL
450 n->status &= ~VIRTIO_NET_S_LINK_UP;
451 else
452 n->status |= VIRTIO_NET_S_LINK_UP;
453
454 if (n->status != old_status)
17a0ca55 455 virtio_notify_config(vdev);
afbaa7b4 456
17a0ca55 457 virtio_net_set_status(vdev, vdev->status);
554c97dd
AL
458}
459
b1be4280
AK
460static void rxfilter_notify(NetClientState *nc)
461{
b1be4280
AK
462 VirtIONet *n = qemu_get_nic_opaque(nc);
463
464 if (nc->rxfilter_notify_enabled) {
ddfb0baa 465 char *path = object_get_canonical_path(OBJECT(n->qdev));
7480874a 466 qapi_event_send_nic_rx_filter_changed(n->netclient_name, path);
96e35046 467 g_free(path);
b1be4280
AK
468
469 /* disable event notification to avoid events flooding */
470 nc->rxfilter_notify_enabled = 0;
471 }
472}
473
f7bc8ef8
AK
474static intList *get_vlan_table(VirtIONet *n)
475{
54aa3de7 476 intList *list;
f7bc8ef8
AK
477 int i, j;
478
479 list = NULL;
480 for (i = 0; i < MAX_VLAN >> 5; i++) {
481 for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
482 if (n->vlans[i] & (1U << j)) {
54aa3de7 483 QAPI_LIST_PREPEND(list, (i << 5) + j);
f7bc8ef8
AK
484 }
485 }
486 }
487
488 return list;
489}
490
b1be4280
AK
491static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
492{
493 VirtIONet *n = qemu_get_nic_opaque(nc);
f7bc8ef8 494 VirtIODevice *vdev = VIRTIO_DEVICE(n);
b1be4280 495 RxFilterInfo *info;
54aa3de7 496 strList *str_list;
f7bc8ef8 497 int i;
b1be4280
AK
498
499 info = g_malloc0(sizeof(*info));
500 info->name = g_strdup(nc->name);
501 info->promiscuous = n->promisc;
502
503 if (n->nouni) {
504 info->unicast = RX_STATE_NONE;
505 } else if (n->alluni) {
506 info->unicast = RX_STATE_ALL;
507 } else {
508 info->unicast = RX_STATE_NORMAL;
509 }
510
511 if (n->nomulti) {
512 info->multicast = RX_STATE_NONE;
513 } else if (n->allmulti) {
514 info->multicast = RX_STATE_ALL;
515 } else {
516 info->multicast = RX_STATE_NORMAL;
517 }
518
519 info->broadcast_allowed = n->nobcast;
520 info->multicast_overflow = n->mac_table.multi_overflow;
521 info->unicast_overflow = n->mac_table.uni_overflow;
522
b0575ba4 523 info->main_mac = qemu_mac_strdup_printf(n->mac);
b1be4280
AK
524
525 str_list = NULL;
526 for (i = 0; i < n->mac_table.first_multi; i++) {
54aa3de7
EB
527 QAPI_LIST_PREPEND(str_list,
528 qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
b1be4280
AK
529 }
530 info->unicast_table = str_list;
531
532 str_list = NULL;
533 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
54aa3de7
EB
534 QAPI_LIST_PREPEND(str_list,
535 qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN));
b1be4280
AK
536 }
537 info->multicast_table = str_list;
f7bc8ef8 538 info->vlan_table = get_vlan_table(n);
b1be4280 539
95129d6f 540 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
f7bc8ef8
AK
541 info->vlan = RX_STATE_ALL;
542 } else if (!info->vlan_table) {
543 info->vlan = RX_STATE_NONE;
544 } else {
545 info->vlan = RX_STATE_NORMAL;
b1be4280 546 }
b1be4280
AK
547
548 /* enable event notification after query */
549 nc->rxfilter_notify_enabled = 1;
550
551 return info;
552}
553
7dc6be52
XZ
554static void virtio_net_queue_reset(VirtIODevice *vdev, uint32_t queue_index)
555{
556 VirtIONet *n = VIRTIO_NET(vdev);
f47af0af
XZ
557 NetClientState *nc;
558
559 /* validate queue_index and skip for cvq */
560 if (queue_index >= n->max_queue_pairs * 2) {
561 return;
562 }
563
564 nc = qemu_get_subqueue(n->nic, vq2q(queue_index));
7dc6be52
XZ
565
566 if (!nc->peer) {
567 return;
568 }
569
570 if (get_vhost_net(nc->peer) &&
571 nc->peer->info->type == NET_CLIENT_DRIVER_TAP) {
572 vhost_net_virtqueue_reset(vdev, nc, queue_index);
573 }
574
575 flush_or_purge_queued_packets(nc);
576}
577
7f863302
KX
578static void virtio_net_queue_enable(VirtIODevice *vdev, uint32_t queue_index)
579{
580 VirtIONet *n = VIRTIO_NET(vdev);
f47af0af 581 NetClientState *nc;
7f863302
KX
582 int r;
583
f47af0af
XZ
584 /* validate queue_index and skip for cvq */
585 if (queue_index >= n->max_queue_pairs * 2) {
586 return;
587 }
588
589 nc = qemu_get_subqueue(n->nic, vq2q(queue_index));
590
7f863302
KX
591 if (!nc->peer || !vdev->vhost_started) {
592 return;
593 }
594
595 if (get_vhost_net(nc->peer) &&
596 nc->peer->info->type == NET_CLIENT_DRIVER_TAP) {
597 r = vhost_net_virtqueue_restart(vdev, nc, queue_index);
598 if (r < 0) {
599 error_report("unable to restart vhost net virtqueue: %d, "
600 "when resetting the queue", queue_index);
601 }
602 }
603}
604
6e371ab8 605static void peer_test_vnet_hdr(VirtIONet *n)
3a330134 606{
b356f76d
JW
607 NetClientState *nc = qemu_get_queue(n->nic);
608 if (!nc->peer) {
6e371ab8 609 return;
b356f76d 610 }
3a330134 611
d6085e3a 612 n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
6e371ab8 613}
3a330134 614
6e371ab8
MT
615static int peer_has_vnet_hdr(VirtIONet *n)
616{
3a330134
MM
617 return n->has_vnet_hdr;
618}
619
0ce0e8f4
MM
620static int peer_has_ufo(VirtIONet *n)
621{
622 if (!peer_has_vnet_hdr(n))
623 return 0;
624
d6085e3a 625 n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
0ce0e8f4
MM
626
627 return n->has_ufo;
628}
629
53da8b5a
YB
630static int peer_has_uso(VirtIONet *n)
631{
632 if (!peer_has_vnet_hdr(n)) {
633 return 0;
634 }
635
636 return qemu_has_uso(qemu_get_queue(n->nic)->peer);
637}
638
bb9d17f8 639static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
e22f0603 640 int version_1, int hash_report)
ff3a8066 641{
fed699f9
JW
642 int i;
643 NetClientState *nc;
644
ff3a8066
MT
645 n->mergeable_rx_bufs = mergeable_rx_bufs;
646
bb9d17f8 647 if (version_1) {
e22f0603
YB
648 n->guest_hdr_len = hash_report ?
649 sizeof(struct virtio_net_hdr_v1_hash) :
650 sizeof(struct virtio_net_hdr_mrg_rxbuf);
651 n->rss_data.populate_hash = !!hash_report;
bb9d17f8
CH
652 } else {
653 n->guest_hdr_len = n->mergeable_rx_bufs ?
654 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
655 sizeof(struct virtio_net_hdr);
13d40aa8 656 n->rss_data.populate_hash = false;
bb9d17f8 657 }
ff3a8066 658
441537f1 659 for (i = 0; i < n->max_queue_pairs; i++) {
fed699f9
JW
660 nc = qemu_get_subqueue(n->nic, i);
661
662 if (peer_has_vnet_hdr(n) &&
d6085e3a
SH
663 qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
664 qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
fed699f9
JW
665 n->host_hdr_len = n->guest_hdr_len;
666 }
ff3a8066
MT
667 }
668}
669
2eef278b
MT
670static int virtio_net_max_tx_queue_size(VirtIONet *n)
671{
672 NetClientState *peer = n->nic_conf.peers.ncs[0];
673
674 /*
0ea5778f
EP
675 * Backends other than vhost-user or vhost-vdpa don't support max queue
676 * size.
2eef278b
MT
677 */
678 if (!peer) {
679 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
680 }
681
0ea5778f
EP
682 switch(peer->info->type) {
683 case NET_CLIENT_DRIVER_VHOST_USER:
684 case NET_CLIENT_DRIVER_VHOST_VDPA:
685 return VIRTQUEUE_MAX_SIZE;
686 default:
2eef278b 687 return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
0ea5778f 688 };
2eef278b
MT
689}
690
fed699f9
JW
691static int peer_attach(VirtIONet *n, int index)
692{
693 NetClientState *nc = qemu_get_subqueue(n->nic, index);
694
695 if (!nc->peer) {
696 return 0;
697 }
698
f394b2e2 699 if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
7263a0ad
CO
700 vhost_set_vring_enable(nc->peer, 1);
701 }
702
f394b2e2 703 if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
fed699f9
JW
704 return 0;
705 }
706
441537f1 707 if (n->max_queue_pairs == 1) {
1074b879
JW
708 return 0;
709 }
710
fed699f9
JW
711 return tap_enable(nc->peer);
712}
713
714static int peer_detach(VirtIONet *n, int index)
715{
716 NetClientState *nc = qemu_get_subqueue(n->nic, index);
717
718 if (!nc->peer) {
719 return 0;
720 }
721
f394b2e2 722 if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
7263a0ad
CO
723 vhost_set_vring_enable(nc->peer, 0);
724 }
725
f394b2e2 726 if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
fed699f9
JW
727 return 0;
728 }
729
730 return tap_disable(nc->peer);
731}
732
441537f1 733static void virtio_net_set_queue_pairs(VirtIONet *n)
fed699f9
JW
734{
735 int i;
ddfa83ea 736 int r;
fed699f9 737
68b5f314
YB
738 if (n->nic->peer_deleted) {
739 return;
740 }
741
441537f1
JW
742 for (i = 0; i < n->max_queue_pairs; i++) {
743 if (i < n->curr_queue_pairs) {
ddfa83ea
JS
744 r = peer_attach(n, i);
745 assert(!r);
fed699f9 746 } else {
ddfa83ea
JS
747 r = peer_detach(n, i);
748 assert(!r);
fed699f9
JW
749 }
750 }
751}
752
ec57db16 753static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
fed699f9 754
9d5b731d
JW
755static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
756 Error **errp)
fbe78f4f 757{
17a0ca55 758 VirtIONet *n = VIRTIO_NET(vdev);
b356f76d 759 NetClientState *nc = qemu_get_queue(n->nic);
fbe78f4f 760
da3e8a23
SZ
761 /* Firstly sync all virtio-net possible supported features */
762 features |= n->host_features;
763
0cd09c3a 764 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
c9f79a3f 765
6e371ab8 766 if (!peer_has_vnet_hdr(n)) {
0cd09c3a
CH
767 virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
768 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
769 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
770 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
8172539d 771
0cd09c3a
CH
772 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
773 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
774 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
775 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
e22f0603 776
53da8b5a
YB
777 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO);
778 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4);
779 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6);
780
e22f0603 781 virtio_clear_feature(&features, VIRTIO_NET_F_HASH_REPORT);
8172539d 782 }
3a330134 783
8172539d 784 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
0cd09c3a
CH
785 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
786 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
3a330134
MM
787 }
788
53da8b5a
YB
789 if (!peer_has_uso(n)) {
790 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_USO);
791 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO4);
792 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_USO6);
793 }
794
ed8b4afe 795 if (!get_vhost_net(nc->peer)) {
9bc6304c
MT
796 return features;
797 }
2974e916 798
0145c393
AM
799 if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
800 virtio_clear_feature(&features, VIRTIO_NET_F_RSS);
801 }
75ebec11
MC
802 features = vhost_net_get_features(get_vhost_net(nc->peer), features);
803 vdev->backend_features = features;
804
805 if (n->mtu_bypass_backend &&
806 (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) {
807 features |= (1ULL << VIRTIO_NET_F_MTU);
808 }
809
cd69d47c
EP
810 /*
811 * Since GUEST_ANNOUNCE is emulated the feature bit could be set without
812 * enabled. This happens in the vDPA case.
813 *
814 * Make sure the feature set is not incoherent, as the driver could refuse
815 * to start.
816 *
817 * TODO: QEMU is able to emulate a CVQ just for guest_announce purposes,
818 * helping guest to notify the new location with vDPA devices that does not
819 * support it.
820 */
821 if (!virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_CTRL_VQ)) {
822 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ANNOUNCE);
823 }
824
75ebec11 825 return features;
fbe78f4f
AL
826}
827
019a3edb 828static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
8eca6b1b 829{
019a3edb 830 uint64_t features = 0;
8eca6b1b
AL
831
832 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
833 * but also these: */
0cd09c3a
CH
834 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
835 virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
836 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
837 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
838 virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
8eca6b1b 839
8172539d 840 return features;
8eca6b1b
AL
841}
842
644c9858
DF
843static void virtio_net_apply_guest_offloads(VirtIONet *n)
844{
ad37bb3b 845 qemu_set_offload(qemu_get_queue(n->nic)->peer,
644c9858
DF
846 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
847 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
848 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
849 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
2ab0ec31
AM
850 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)),
851 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO4)),
852 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_USO6)));
644c9858
DF
853}
854
53da8b5a 855static uint64_t virtio_net_guest_offloads_by_features(uint64_t features)
644c9858
DF
856{
857 static const uint64_t guest_offloads_mask =
858 (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
859 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
860 (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
861 (1ULL << VIRTIO_NET_F_GUEST_ECN) |
53da8b5a
YB
862 (1ULL << VIRTIO_NET_F_GUEST_UFO) |
863 (1ULL << VIRTIO_NET_F_GUEST_USO4) |
864 (1ULL << VIRTIO_NET_F_GUEST_USO6);
644c9858
DF
865
866 return guest_offloads_mask & features;
867}
868
0b545b1e 869uint64_t virtio_net_supported_guest_offloads(const VirtIONet *n)
644c9858
DF
870{
871 VirtIODevice *vdev = VIRTIO_DEVICE(n);
872 return virtio_net_guest_offloads_by_features(vdev->guest_features);
873}
874
f5e1847b
JQ
875typedef struct {
876 VirtIONet *n;
12b2fad7
KW
877 DeviceState *dev;
878} FailoverDevice;
f5e1847b
JQ
879
880/**
12b2fad7 881 * Set the failover primary device
f5e1847b
JQ
882 *
883 * @opaque: FailoverId to setup
884 * @opts: opts for device we are handling
885 * @errp: returns an error if this function fails
886 */
12b2fad7 887static int failover_set_primary(DeviceState *dev, void *opaque)
f5e1847b 888{
12b2fad7
KW
889 FailoverDevice *fdev = opaque;
890 PCIDevice *pci_dev = (PCIDevice *)
891 object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE);
f5e1847b 892
12b2fad7
KW
893 if (!pci_dev) {
894 return 0;
895 }
896
897 if (!g_strcmp0(pci_dev->failover_pair_id, fdev->n->netclient_name)) {
898 fdev->dev = dev;
f5e1847b
JQ
899 return 1;
900 }
901
902 return 0;
903}
904
21e8709b
JQ
905/**
906 * Find the primary device for this failover virtio-net
907 *
908 * @n: VirtIONet device
909 * @errp: returns an error if this function fails
910 */
911static DeviceState *failover_find_primary_device(VirtIONet *n)
912{
12b2fad7
KW
913 FailoverDevice fdev = {
914 .n = n,
915 };
21e8709b 916
12b2fad7
KW
917 qbus_walk_children(sysbus_get_default(), failover_set_primary, NULL,
918 NULL, NULL, &fdev);
919 return fdev.dev;
21e8709b
JQ
920}
921
9711cd0d
JF
922static void failover_add_primary(VirtIONet *n, Error **errp)
923{
924 Error *err = NULL;
21e8709b 925 DeviceState *dev = failover_find_primary_device(n);
9711cd0d 926
21e8709b 927 if (dev) {
117378bf
JF
928 return;
929 }
930
259a10db 931 if (!n->primary_opts) {
97ca9c59
LV
932 error_setg(errp, "Primary device not found");
933 error_append_hint(errp, "Virtio-net failover will not work. Make "
934 "sure primary device has parameter"
935 " failover_pair_id=%s\n", n->netclient_name);
3abad4a2
JQ
936 return;
937 }
259a10db 938
f3558b1b
KW
939 dev = qdev_device_add_from_qdict(n->primary_opts,
940 n->primary_opts_from_json,
941 &err);
97ca9c59 942 if (err) {
f3558b1b 943 qobject_unref(n->primary_opts);
259a10db 944 n->primary_opts = NULL;
9711cd0d 945 } else {
97ca9c59 946 object_unref(OBJECT(dev));
1c775d65 947 }
2155ceaf 948 error_propagate(errp, err);
9711cd0d
JF
949}
950
d5aaa1b0 951static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
fbe78f4f 952{
17a0ca55 953 VirtIONet *n = VIRTIO_NET(vdev);
9711cd0d 954 Error *err = NULL;
fed699f9
JW
955 int i;
956
75ebec11
MC
957 if (n->mtu_bypass_backend &&
958 !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
959 features &= ~(1ULL << VIRTIO_NET_F_MTU);
960 }
961
ef546f12 962 virtio_net_set_multiqueue(n,
59079029 963 virtio_has_feature(features, VIRTIO_NET_F_RSS) ||
95129d6f 964 virtio_has_feature(features, VIRTIO_NET_F_MQ));
fbe78f4f 965
ef546f12 966 virtio_net_set_mrg_rx_bufs(n,
95129d6f
CH
967 virtio_has_feature(features,
968 VIRTIO_NET_F_MRG_RXBUF),
969 virtio_has_feature(features,
e22f0603
YB
970 VIRTIO_F_VERSION_1),
971 virtio_has_feature(features,
972 VIRTIO_NET_F_HASH_REPORT));
f5436dd9 973
2974e916
YB
974 n->rsc4_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
975 virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO4);
976 n->rsc6_enabled = virtio_has_feature(features, VIRTIO_NET_F_RSC_EXT) &&
977 virtio_has_feature(features, VIRTIO_NET_F_GUEST_TSO6);
e22f0603 978 n->rss_data.redirect = virtio_has_feature(features, VIRTIO_NET_F_RSS);
2974e916 979
f5436dd9 980 if (n->has_vnet_hdr) {
644c9858
DF
981 n->curr_guest_offloads =
982 virtio_net_guest_offloads_by_features(features);
983 virtio_net_apply_guest_offloads(n);
f5436dd9 984 }
fed699f9 985
441537f1 986 for (i = 0; i < n->max_queue_pairs; i++) {
fed699f9
JW
987 NetClientState *nc = qemu_get_subqueue(n->nic, i);
988
ed8b4afe 989 if (!get_vhost_net(nc->peer)) {
fed699f9
JW
990 continue;
991 }
ed8b4afe 992 vhost_net_ack_features(get_vhost_net(nc->peer), features);
c9bdc449
HH
993
994 /*
995 * keep acked_features in NetVhostUserState up-to-date so it
996 * can't miss any features configured by guest virtio driver.
997 */
998 vhost_net_save_acked_features(nc->peer);
dc14a397 999 }
0b1eaa88 1000
06b636a1 1001 if (!virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
0b1eaa88
SF
1002 memset(n->vlans, 0xff, MAX_VLAN >> 3);
1003 }
9711cd0d
JF
1004
1005 if (virtio_has_feature(features, VIRTIO_NET_F_STANDBY)) {
1006 qapi_event_send_failover_negotiated(n->netclient_name);
e2bde83e 1007 qatomic_set(&n->failover_primary_hidden, false);
9711cd0d
JF
1008 failover_add_primary(n, &err);
1009 if (err) {
1b529d90
LV
1010 if (!qtest_enabled()) {
1011 warn_report_err(err);
1012 } else {
1013 error_free(err);
1014 }
9711cd0d
JF
1015 }
1016 }
fbe78f4f
AL
1017}
1018
002437cd 1019static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
921ac5d0 1020 struct iovec *iov, unsigned int iov_cnt)
002437cd
AL
1021{
1022 uint8_t on;
921ac5d0 1023 size_t s;
b1be4280 1024 NetClientState *nc = qemu_get_queue(n->nic);
002437cd 1025
921ac5d0
MT
1026 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
1027 if (s != sizeof(on)) {
1028 return VIRTIO_NET_ERR;
002437cd
AL
1029 }
1030
dd23454b 1031 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
002437cd 1032 n->promisc = on;
dd23454b 1033 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
002437cd 1034 n->allmulti = on;
dd23454b 1035 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
015cb166 1036 n->alluni = on;
dd23454b 1037 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
015cb166 1038 n->nomulti = on;
dd23454b 1039 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
015cb166 1040 n->nouni = on;
dd23454b 1041 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
015cb166 1042 n->nobcast = on;
921ac5d0 1043 } else {
002437cd 1044 return VIRTIO_NET_ERR;
921ac5d0 1045 }
002437cd 1046
b1be4280
AK
1047 rxfilter_notify(nc);
1048
002437cd
AL
1049 return VIRTIO_NET_OK;
1050}
1051
644c9858
DF
1052static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
1053 struct iovec *iov, unsigned int iov_cnt)
1054{
1055 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1056 uint64_t offloads;
1057 size_t s;
1058
95129d6f 1059 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
644c9858
DF
1060 return VIRTIO_NET_ERR;
1061 }
1062
1063 s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
1064 if (s != sizeof(offloads)) {
1065 return VIRTIO_NET_ERR;
1066 }
1067
1068 if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
1069 uint64_t supported_offloads;
1070
189ae6bb
JW
1071 offloads = virtio_ldq_p(vdev, &offloads);
1072
644c9858
DF
1073 if (!n->has_vnet_hdr) {
1074 return VIRTIO_NET_ERR;
1075 }
1076
2974e916
YB
1077 n->rsc4_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
1078 virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO4);
1079 n->rsc6_enabled = virtio_has_feature(offloads, VIRTIO_NET_F_RSC_EXT) &&
1080 virtio_has_feature(offloads, VIRTIO_NET_F_GUEST_TSO6);
1081 virtio_clear_feature(&offloads, VIRTIO_NET_F_RSC_EXT);
1082
644c9858
DF
1083 supported_offloads = virtio_net_supported_guest_offloads(n);
1084 if (offloads & ~supported_offloads) {
1085 return VIRTIO_NET_ERR;
1086 }
1087
1088 n->curr_guest_offloads = offloads;
1089 virtio_net_apply_guest_offloads(n);
1090
1091 return VIRTIO_NET_OK;
1092 } else {
1093 return VIRTIO_NET_ERR;
1094 }
1095}
1096
b6503ed9 1097static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
921ac5d0 1098 struct iovec *iov, unsigned int iov_cnt)
b6503ed9 1099{
1399c60d 1100 VirtIODevice *vdev = VIRTIO_DEVICE(n);
b6503ed9 1101 struct virtio_net_ctrl_mac mac_data;
921ac5d0 1102 size_t s;
b1be4280 1103 NetClientState *nc = qemu_get_queue(n->nic);
b6503ed9 1104
c1943a3f
AK
1105 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
1106 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
1107 return VIRTIO_NET_ERR;
1108 }
1109 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
1110 assert(s == sizeof(n->mac));
b356f76d 1111 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
b1be4280
AK
1112 rxfilter_notify(nc);
1113
c1943a3f
AK
1114 return VIRTIO_NET_OK;
1115 }
1116
921ac5d0 1117 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
b6503ed9 1118 return VIRTIO_NET_ERR;
921ac5d0 1119 }
b6503ed9 1120
cae2e556
AK
1121 int in_use = 0;
1122 int first_multi = 0;
1123 uint8_t uni_overflow = 0;
1124 uint8_t multi_overflow = 0;
1125 uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
b6503ed9 1126
921ac5d0
MT
1127 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
1128 sizeof(mac_data.entries));
1399c60d 1129 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
921ac5d0 1130 if (s != sizeof(mac_data.entries)) {
b1be4280 1131 goto error;
921ac5d0
MT
1132 }
1133 iov_discard_front(&iov, &iov_cnt, s);
b6503ed9 1134
921ac5d0 1135 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
b1be4280 1136 goto error;
921ac5d0 1137 }
b6503ed9
AL
1138
1139 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
cae2e556 1140 s = iov_to_buf(iov, iov_cnt, 0, macs,
921ac5d0
MT
1141 mac_data.entries * ETH_ALEN);
1142 if (s != mac_data.entries * ETH_ALEN) {
b1be4280 1143 goto error;
921ac5d0 1144 }
cae2e556 1145 in_use += mac_data.entries;
b6503ed9 1146 } else {
cae2e556 1147 uni_overflow = 1;
b6503ed9
AL
1148 }
1149
921ac5d0
MT
1150 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
1151
cae2e556 1152 first_multi = in_use;
2d9aba39 1153
921ac5d0
MT
1154 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
1155 sizeof(mac_data.entries));
1399c60d 1156 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
921ac5d0 1157 if (s != sizeof(mac_data.entries)) {
b1be4280 1158 goto error;
921ac5d0
MT
1159 }
1160
1161 iov_discard_front(&iov, &iov_cnt, s);
b6503ed9 1162
921ac5d0 1163 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
b1be4280 1164 goto error;
921ac5d0 1165 }
b6503ed9 1166
edc24385 1167 if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
cae2e556 1168 s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
921ac5d0
MT
1169 mac_data.entries * ETH_ALEN);
1170 if (s != mac_data.entries * ETH_ALEN) {
b1be4280 1171 goto error;
8fd2a2f1 1172 }
cae2e556 1173 in_use += mac_data.entries;
921ac5d0 1174 } else {
cae2e556 1175 multi_overflow = 1;
b6503ed9
AL
1176 }
1177
cae2e556
AK
1178 n->mac_table.in_use = in_use;
1179 n->mac_table.first_multi = first_multi;
1180 n->mac_table.uni_overflow = uni_overflow;
1181 n->mac_table.multi_overflow = multi_overflow;
1182 memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
1183 g_free(macs);
b1be4280
AK
1184 rxfilter_notify(nc);
1185
b6503ed9 1186 return VIRTIO_NET_OK;
b1be4280
AK
1187
1188error:
cae2e556 1189 g_free(macs);
b1be4280 1190 return VIRTIO_NET_ERR;
b6503ed9
AL
1191}
1192
f21c0ed9 1193static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
921ac5d0 1194 struct iovec *iov, unsigned int iov_cnt)
f21c0ed9 1195{
1399c60d 1196 VirtIODevice *vdev = VIRTIO_DEVICE(n);
f21c0ed9 1197 uint16_t vid;
921ac5d0 1198 size_t s;
b1be4280 1199 NetClientState *nc = qemu_get_queue(n->nic);
f21c0ed9 1200
921ac5d0 1201 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
1399c60d 1202 vid = virtio_lduw_p(vdev, &vid);
921ac5d0 1203 if (s != sizeof(vid)) {
f21c0ed9
AL
1204 return VIRTIO_NET_ERR;
1205 }
1206
f21c0ed9
AL
1207 if (vid >= MAX_VLAN)
1208 return VIRTIO_NET_ERR;
1209
1210 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
1211 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
1212 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
1213 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
1214 else
1215 return VIRTIO_NET_ERR;
1216
b1be4280
AK
1217 rxfilter_notify(nc);
1218
f21c0ed9
AL
1219 return VIRTIO_NET_OK;
1220}
1221
f57fcf70
JW
1222static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
1223 struct iovec *iov, unsigned int iov_cnt)
1224{
9d8c6a25 1225 trace_virtio_net_handle_announce(n->announce_timer.round);
f57fcf70
JW
1226 if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
1227 n->status & VIRTIO_NET_S_ANNOUNCE) {
1228 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
9d8c6a25
DDAG
1229 if (n->announce_timer.round) {
1230 qemu_announce_timer_step(&n->announce_timer);
f57fcf70
JW
1231 }
1232 return VIRTIO_NET_OK;
1233 } else {
1234 return VIRTIO_NET_ERR;
1235 }
1236}
1237
0145c393
AM
1238static bool virtio_net_attach_ebpf_to_backend(NICState *nic, int prog_fd)
1239{
1240 NetClientState *nc = qemu_get_peer(qemu_get_queue(nic), 0);
1241 if (nc == NULL || nc->info->set_steering_ebpf == NULL) {
1242 return false;
1243 }
1244
ae311fb3 1245 trace_virtio_net_rss_attach_ebpf(nic, prog_fd);
0145c393
AM
1246 return nc->info->set_steering_ebpf(nc, prog_fd);
1247}
1248
1249static void rss_data_to_rss_config(struct VirtioNetRssData *data,
1250 struct EBPFRSSConfig *config)
1251{
1252 config->redirect = data->redirect;
1253 config->populate_hash = data->populate_hash;
1254 config->hash_types = data->hash_types;
1255 config->indirections_len = data->indirections_len;
1256 config->default_queue = data->default_queue;
1257}
1258
493a2403 1259static bool virtio_net_attach_ebpf_rss(VirtIONet *n)
0145c393
AM
1260{
1261 struct EBPFRSSConfig config = {};
1262
1263 if (!ebpf_rss_is_loaded(&n->ebpf_rss)) {
1264 return false;
1265 }
1266
1267 rss_data_to_rss_config(&n->rss_data, &config);
1268
1269 if (!ebpf_rss_set_all(&n->ebpf_rss, &config,
00b69f1d
DB
1270 n->rss_data.indirections_table, n->rss_data.key,
1271 NULL)) {
0145c393
AM
1272 return false;
1273 }
1274
1275 if (!virtio_net_attach_ebpf_to_backend(n->nic, n->ebpf_rss.program_fd)) {
1276 return false;
1277 }
1278
1279 return true;
1280}
1281
493a2403 1282static void virtio_net_detach_ebpf_rss(VirtIONet *n)
0145c393
AM
1283{
1284 virtio_net_attach_ebpf_to_backend(n->nic, -1);
1285}
1286
0e07198e
AO
1287static void virtio_net_commit_rss_config(VirtIONet *n)
1288{
1289 if (n->rss_data.enabled) {
1290 n->rss_data.enabled_software_rss = n->rss_data.populate_hash;
1291 if (n->rss_data.populate_hash) {
493a2403
DB
1292 virtio_net_detach_ebpf_rss(n);
1293 } else if (!virtio_net_attach_ebpf_rss(n)) {
0e07198e
AO
1294 if (get_vhost_net(qemu_get_queue(n->nic)->peer)) {
1295 warn_report("Can't load eBPF RSS for vhost");
1296 } else {
1297 warn_report("Can't load eBPF RSS - fallback to software RSS");
1298 n->rss_data.enabled_software_rss = true;
1299 }
1300 }
1301
ae311fb3
DB
1302 trace_virtio_net_rss_enable(n,
1303 n->rss_data.hash_types,
0e07198e
AO
1304 n->rss_data.indirections_len,
1305 sizeof(n->rss_data.key));
1306 } else {
493a2403 1307 virtio_net_detach_ebpf_rss(n);
ae311fb3 1308 trace_virtio_net_rss_disable(n);
0e07198e
AO
1309 }
1310}
1311
1312static void virtio_net_disable_rss(VirtIONet *n)
1313{
1314 if (!n->rss_data.enabled) {
1315 return;
1316 }
1317
1318 n->rss_data.enabled = false;
1319 virtio_net_commit_rss_config(n);
1320}
1321
b5900dff 1322static bool virtio_net_load_ebpf_fds(VirtIONet *n, Error **errp)
0145c393 1323{
6b230b7d
AM
1324 int fds[EBPF_RSS_MAX_FDS] = { [0 ... EBPF_RSS_MAX_FDS - 1] = -1};
1325 int ret = true;
1326 int i = 0;
1327
6b230b7d 1328 if (n->nr_ebpf_rss_fds != EBPF_RSS_MAX_FDS) {
b5900dff
DB
1329 error_setg(errp, "Expected %d file descriptors but got %d",
1330 EBPF_RSS_MAX_FDS, n->nr_ebpf_rss_fds);
1331 return false;
1332 }
6b230b7d
AM
1333
1334 for (i = 0; i < n->nr_ebpf_rss_fds; i++) {
b5900dff 1335 fds[i] = monitor_fd_param(monitor_cur(), n->ebpf_rss_fds[i], errp);
283be596 1336 if (fds[i] < 0) {
6b230b7d
AM
1337 ret = false;
1338 goto exit;
1339 }
1340 }
1341
b5900dff 1342 ret = ebpf_rss_load_fds(&n->ebpf_rss, fds[0], fds[1], fds[2], fds[3], errp);
6b230b7d
AM
1343
1344exit:
283be596 1345 if (!ret) {
6b230b7d
AM
1346 for (i = 0; i < n->nr_ebpf_rss_fds && fds[i] != -1; i++) {
1347 close(fds[i]);
1348 }
0145c393
AM
1349 }
1350
6b230b7d
AM
1351 return ret;
1352}
1353
b5900dff 1354static bool virtio_net_load_ebpf(VirtIONet *n, Error **errp)
6b230b7d 1355{
bc82af6b
AO
1356 if (!virtio_net_attach_ebpf_to_backend(n->nic, -1)) {
1357 return true;
1358 }
6b230b7d 1359
bc82af6b
AO
1360 trace_virtio_net_rss_load(n, n->nr_ebpf_rss_fds, n->ebpf_rss_fds);
1361
1362 /*
1363 * If user explicitly gave QEMU RSS FDs to use, then
1364 * failing to use them must be considered a fatal
1365 * error. If no RSS FDs were provided, QEMU is trying
1366 * eBPF on a "best effort" basis only, so report a
1367 * warning and allow fallback to software RSS.
1368 */
1369 if (n->ebpf_rss_fds) {
1370 return virtio_net_load_ebpf_fds(n, errp);
6b230b7d
AM
1371 }
1372
bc82af6b
AO
1373 ebpf_rss_load(&n->ebpf_rss, &error_warn);
1374 return true;
0145c393
AM
1375}
1376
1377static void virtio_net_unload_ebpf(VirtIONet *n)
1378{
1379 virtio_net_attach_ebpf_to_backend(n->nic, -1);
1380 ebpf_rss_unload(&n->ebpf_rss);
59079029
YB
1381}
1382
1383static uint16_t virtio_net_handle_rss(VirtIONet *n,
e22f0603
YB
1384 struct iovec *iov,
1385 unsigned int iov_cnt,
1386 bool do_rss)
59079029
YB
1387{
1388 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1389 struct virtio_net_rss_config cfg;
1390 size_t s, offset = 0, size_get;
441537f1 1391 uint16_t queue_pairs, i;
59079029
YB
1392 struct {
1393 uint16_t us;
1394 uint8_t b;
1395 } QEMU_PACKED temp;
1396 const char *err_msg = "";
1397 uint32_t err_value = 0;
1398
e22f0603 1399 if (do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_RSS)) {
59079029
YB
1400 err_msg = "RSS is not negotiated";
1401 goto error;
1402 }
e22f0603
YB
1403 if (!do_rss && !virtio_vdev_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
1404 err_msg = "Hash report is not negotiated";
1405 goto error;
1406 }
59079029
YB
1407 size_get = offsetof(struct virtio_net_rss_config, indirection_table);
1408 s = iov_to_buf(iov, iov_cnt, offset, &cfg, size_get);
1409 if (s != size_get) {
1410 err_msg = "Short command buffer";
1411 err_value = (uint32_t)s;
1412 goto error;
1413 }
1414 n->rss_data.hash_types = virtio_ldl_p(vdev, &cfg.hash_types);
1415 n->rss_data.indirections_len =
1416 virtio_lduw_p(vdev, &cfg.indirection_table_mask);
e22f0603 1417 if (!do_rss) {
cd76e8fc 1418 n->rss_data.indirections_len = 0;
e22f0603 1419 }
cd76e8fc
AO
1420 if (n->rss_data.indirections_len >= VIRTIO_NET_RSS_MAX_TABLE_LEN) {
1421 err_msg = "Too large indirection table";
59079029
YB
1422 err_value = n->rss_data.indirections_len;
1423 goto error;
1424 }
cd76e8fc
AO
1425 n->rss_data.indirections_len++;
1426 if (!is_power_of_2(n->rss_data.indirections_len)) {
1427 err_msg = "Invalid size of indirection table";
59079029
YB
1428 err_value = n->rss_data.indirections_len;
1429 goto error;
1430 }
e22f0603
YB
1431 n->rss_data.default_queue = do_rss ?
1432 virtio_lduw_p(vdev, &cfg.unclassified_queue) : 0;
441537f1 1433 if (n->rss_data.default_queue >= n->max_queue_pairs) {
59079029
YB
1434 err_msg = "Invalid default queue";
1435 err_value = n->rss_data.default_queue;
1436 goto error;
1437 }
1438 offset += size_get;
1439 size_get = sizeof(uint16_t) * n->rss_data.indirections_len;
1440 g_free(n->rss_data.indirections_table);
1441 n->rss_data.indirections_table = g_malloc(size_get);
1442 if (!n->rss_data.indirections_table) {
1443 err_msg = "Can't allocate indirections table";
1444 err_value = n->rss_data.indirections_len;
1445 goto error;
1446 }
1447 s = iov_to_buf(iov, iov_cnt, offset,
1448 n->rss_data.indirections_table, size_get);
1449 if (s != size_get) {
1450 err_msg = "Short indirection table buffer";
1451 err_value = (uint32_t)s;
1452 goto error;
1453 }
1454 for (i = 0; i < n->rss_data.indirections_len; ++i) {
1455 uint16_t val = n->rss_data.indirections_table[i];
1456 n->rss_data.indirections_table[i] = virtio_lduw_p(vdev, &val);
1457 }
1458 offset += size_get;
1459 size_get = sizeof(temp);
1460 s = iov_to_buf(iov, iov_cnt, offset, &temp, size_get);
1461 if (s != size_get) {
441537f1 1462 err_msg = "Can't get queue_pairs";
59079029
YB
1463 err_value = (uint32_t)s;
1464 goto error;
1465 }
441537f1
JW
1466 queue_pairs = do_rss ? virtio_lduw_p(vdev, &temp.us) : n->curr_queue_pairs;
1467 if (queue_pairs == 0 || queue_pairs > n->max_queue_pairs) {
1468 err_msg = "Invalid number of queue_pairs";
1469 err_value = queue_pairs;
59079029
YB
1470 goto error;
1471 }
1472 if (temp.b > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
1473 err_msg = "Invalid key size";
1474 err_value = temp.b;
1475 goto error;
1476 }
1477 if (!temp.b && n->rss_data.hash_types) {
1478 err_msg = "No key provided";
1479 err_value = 0;
1480 goto error;
1481 }
1482 if (!temp.b && !n->rss_data.hash_types) {
1483 virtio_net_disable_rss(n);
441537f1 1484 return queue_pairs;
59079029
YB
1485 }
1486 offset += size_get;
1487 size_get = temp.b;
1488 s = iov_to_buf(iov, iov_cnt, offset, n->rss_data.key, size_get);
1489 if (s != size_get) {
1490 err_msg = "Can get key buffer";
1491 err_value = (uint32_t)s;
1492 goto error;
1493 }
1494 n->rss_data.enabled = true;
0e07198e 1495 virtio_net_commit_rss_config(n);
441537f1 1496 return queue_pairs;
59079029 1497error:
ae311fb3 1498 trace_virtio_net_rss_error(n, err_msg, err_value);
59079029
YB
1499 virtio_net_disable_rss(n);
1500 return 0;
1501}
1502
fed699f9 1503static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
f8f7c533 1504 struct iovec *iov, unsigned int iov_cnt)
fed699f9 1505{
17a0ca55 1506 VirtIODevice *vdev = VIRTIO_DEVICE(n);
441537f1 1507 uint16_t queue_pairs;
2a7888cc 1508 NetClientState *nc = qemu_get_queue(n->nic);
fed699f9 1509
59079029 1510 virtio_net_disable_rss(n);
e22f0603 1511 if (cmd == VIRTIO_NET_CTRL_MQ_HASH_CONFIG) {
441537f1
JW
1512 queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, false);
1513 return queue_pairs ? VIRTIO_NET_OK : VIRTIO_NET_ERR;
e22f0603 1514 }
59079029 1515 if (cmd == VIRTIO_NET_CTRL_MQ_RSS_CONFIG) {
441537f1 1516 queue_pairs = virtio_net_handle_rss(n, iov, iov_cnt, true);
59079029
YB
1517 } else if (cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
1518 struct virtio_net_ctrl_mq mq;
1519 size_t s;
1520 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_MQ)) {
1521 return VIRTIO_NET_ERR;
1522 }
1523 s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
1524 if (s != sizeof(mq)) {
1525 return VIRTIO_NET_ERR;
1526 }
441537f1 1527 queue_pairs = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
fed699f9 1528
59079029 1529 } else {
fed699f9
JW
1530 return VIRTIO_NET_ERR;
1531 }
1532
441537f1
JW
1533 if (queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1534 queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1535 queue_pairs > n->max_queue_pairs ||
fed699f9
JW
1536 !n->multiqueue) {
1537 return VIRTIO_NET_ERR;
1538 }
1539
ca8717f9 1540 n->curr_queue_pairs = queue_pairs;
2a7888cc 1541 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
ca8717f9
EP
1542 /*
1543 * Avoid updating the backend for a vdpa device: We're only interested
1544 * in updating the device model queues.
1545 */
1546 return VIRTIO_NET_OK;
2a7888cc 1547 }
441537f1 1548 /* stop the backend before changing the number of queue_pairs to avoid handling a
fed699f9 1549 * disabled queue */
17a0ca55 1550 virtio_net_set_status(vdev, vdev->status);
441537f1 1551 virtio_net_set_queue_pairs(n);
fed699f9
JW
1552
1553 return VIRTIO_NET_OK;
1554}
ba7eadb5 1555
640b8a1c
EP
1556size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
1557 const struct iovec *in_sg, unsigned in_num,
1558 const struct iovec *out_sg,
1559 unsigned out_num)
3d11d36c 1560{
17a0ca55 1561 VirtIONet *n = VIRTIO_NET(vdev);
3d11d36c
AL
1562 struct virtio_net_ctrl_hdr ctrl;
1563 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
921ac5d0 1564 size_t s;
771b6ed3 1565 struct iovec *iov, *iov2;
640b8a1c
EP
1566
1567 if (iov_size(in_sg, in_num) < sizeof(status) ||
1568 iov_size(out_sg, out_num) < sizeof(ctrl)) {
1569 virtio_error(vdev, "virtio-net ctrl missing headers");
1570 return 0;
1571 }
1572
1573 iov2 = iov = g_memdup2(out_sg, sizeof(struct iovec) * out_num);
1574 s = iov_to_buf(iov, out_num, 0, &ctrl, sizeof(ctrl));
1575 iov_discard_front(&iov, &out_num, sizeof(ctrl));
1576 if (s != sizeof(ctrl)) {
1577 status = VIRTIO_NET_ERR;
1578 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
1579 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, out_num);
1580 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
1581 status = virtio_net_handle_mac(n, ctrl.cmd, iov, out_num);
1582 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
1583 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, out_num);
1584 } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
1585 status = virtio_net_handle_announce(n, ctrl.cmd, iov, out_num);
1586 } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
1587 status = virtio_net_handle_mq(n, ctrl.cmd, iov, out_num);
1588 } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
1589 status = virtio_net_handle_offloads(n, ctrl.cmd, iov, out_num);
1590 }
1591
1592 s = iov_from_buf(in_sg, in_num, 0, &status, sizeof(status));
1593 assert(s == sizeof(status));
1594
1595 g_free(iov2);
1596 return sizeof(status);
1597}
1598
1599static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
1600{
1601 VirtQueueElement *elem;
3d11d36c 1602
51b19ebe 1603 for (;;) {
640b8a1c 1604 size_t written;
51b19ebe
PB
1605 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
1606 if (!elem) {
1607 break;
1608 }
640b8a1c
EP
1609
1610 written = virtio_net_handle_ctrl_iov(vdev, elem->in_sg, elem->in_num,
1611 elem->out_sg, elem->out_num);
1612 if (written > 0) {
1613 virtqueue_push(vq, elem, written);
1614 virtio_notify(vdev, vq);
1615 g_free(elem);
1616 } else {
ba7eadb5
GK
1617 virtqueue_detach_element(vq, elem, 0);
1618 g_free(elem);
1619 break;
3d11d36c 1620 }
3d11d36c
AL
1621 }
1622}
1623
fbe78f4f
AL
1624/* RX */
1625
1626static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
1627{
17a0ca55 1628 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9 1629 int queue_index = vq2q(virtio_get_queue_index(vq));
8aeff62d 1630
fed699f9 1631 qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
fbe78f4f
AL
1632}
1633
b8c4b67e 1634static bool virtio_net_can_receive(NetClientState *nc)
fbe78f4f 1635{
cc1f0f45 1636 VirtIONet *n = qemu_get_nic_opaque(nc);
17a0ca55 1637 VirtIODevice *vdev = VIRTIO_DEVICE(n);
fed699f9 1638 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
0c87e93e 1639
17a0ca55 1640 if (!vdev->vm_running) {
b8c4b67e 1641 return false;
95477323 1642 }
cdd5cc12 1643
441537f1 1644 if (nc->queue_index >= n->curr_queue_pairs) {
b8c4b67e 1645 return false;
fed699f9
JW
1646 }
1647
0c87e93e 1648 if (!virtio_queue_ready(q->rx_vq) ||
17a0ca55 1649 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
b8c4b67e 1650 return false;
0c87e93e 1651 }
fbe78f4f 1652
b8c4b67e 1653 return true;
cdd5cc12
MM
1654}
1655
0c87e93e 1656static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
cdd5cc12 1657{
f937309f 1658 int opaque;
1659 unsigned int in_bytes;
0c87e93e 1660 VirtIONet *n = q->n;
f937309f 1661
1662 while (virtio_queue_empty(q->rx_vq) || n->mergeable_rx_bufs) {
1663 opaque = virtqueue_get_avail_bytes(q->rx_vq, &in_bytes, NULL,
1664 bufsize, 0);
1665 /* Buffer is enough, disable notifiaction */
1666 if (bufsize <= in_bytes) {
1667 break;
1668 }
1669
1670 if (virtio_queue_enable_notification_and_check(q->rx_vq, opaque)) {
1671 /* Guest has added some buffers, try again */
1672 continue;
1673 } else {
06b12970 1674 return 0;
0c87e93e 1675 }
fbe78f4f
AL
1676 }
1677
0c87e93e 1678 virtio_queue_set_notification(q->rx_vq, 0);
f937309f 1679
fbe78f4f
AL
1680 return 1;
1681}
1682
1399c60d 1683static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
032a74a1 1684{
1399c60d
RR
1685 virtio_tswap16s(vdev, &hdr->hdr_len);
1686 virtio_tswap16s(vdev, &hdr->gso_size);
1687 virtio_tswap16s(vdev, &hdr->csum_start);
1688 virtio_tswap16s(vdev, &hdr->csum_offset);
032a74a1
CLG
1689}
1690
1d41b0c1
AL
1691/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1692 * it never finds out that the packets don't have valid checksums. This
1693 * causes dhclient to get upset. Fedora's carried a patch for ages to
1694 * fix this with Xen but it hasn't appeared in an upstream release of
1695 * dhclient yet.
1696 *
1697 * To avoid breaking existing guests, we catch udp packets and add
1698 * checksums. This is terrible but it's better than hacking the guest
1699 * kernels.
1700 *
1701 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1702 * we should provide a mechanism to disable it to avoid polluting the host
1703 * cache.
1704 */
1705static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
e28fbd1c 1706 uint8_t *buf, size_t size)
1d41b0c1 1707{
a8575f7f
AO
1708 size_t csum_size = ETH_HLEN + sizeof(struct ip_header) +
1709 sizeof(struct udp_header);
1710
1d41b0c1 1711 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
e28fbd1c 1712 (size >= csum_size && size < 1500) && /* normal sized MTU */
1d41b0c1
AL
1713 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
1714 (buf[23] == 17) && /* ip.protocol == UDP */
1715 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
e28fbd1c 1716 net_checksum_calculate(buf, size, CSUM_UDP);
1d41b0c1
AL
1717 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
1718 }
1719}
1720
e28fbd1c
AD
1721static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
1722 const void *buf, size_t size)
fbe78f4f 1723{
e28fbd1c
AD
1724 if (n->has_vnet_hdr) {
1725 /* FIXME this cast is evil */
1726 void *wbuf = (void *)buf;
1727 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
1728 size - n->host_hdr_len);
7987d2be 1729
e28fbd1c
AD
1730 if (n->needs_vnet_hdr_swap) {
1731 virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
1732 }
1733 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
1734 } else {
1735 struct virtio_net_hdr hdr = {
1736 .flags = 0,
1737 .gso_type = VIRTIO_NET_HDR_GSO_NONE
1738 };
1739 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
3a330134 1740 }
fbe78f4f
AL
1741}
1742
3831ab20
AL
1743static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
1744{
1745 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
f21c0ed9 1746 static const uint8_t vlan[] = {0x81, 0x00};
3831ab20 1747 uint8_t *ptr = (uint8_t *)buf;
b6503ed9 1748 int i;
3831ab20
AL
1749
1750 if (n->promisc)
1751 return 1;
1752
e043ebc6 1753 ptr += n->host_hdr_len;
3a330134 1754
f21c0ed9 1755 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
7542d3e7 1756 int vid = lduw_be_p(ptr + 14) & 0xfff;
f21c0ed9
AL
1757 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
1758 return 0;
1759 }
1760
bbe2f399
AW
1761 if (ptr[0] & 1) { // multicast
1762 if (!memcmp(ptr, bcast, sizeof(bcast))) {
015cb166
AW
1763 return !n->nobcast;
1764 } else if (n->nomulti) {
1765 return 0;
8fd2a2f1 1766 } else if (n->allmulti || n->mac_table.multi_overflow) {
bbe2f399
AW
1767 return 1;
1768 }
2d9aba39
AW
1769
1770 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
1771 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1772 return 1;
1773 }
1774 }
bbe2f399 1775 } else { // unicast
015cb166
AW
1776 if (n->nouni) {
1777 return 0;
1778 } else if (n->alluni || n->mac_table.uni_overflow) {
8fd2a2f1
AW
1779 return 1;
1780 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
bbe2f399
AW
1781 return 1;
1782 }
3831ab20 1783
2d9aba39
AW
1784 for (i = 0; i < n->mac_table.first_multi; i++) {
1785 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1786 return 1;
1787 }
1788 }
b6503ed9
AL
1789 }
1790
3831ab20
AL
1791 return 0;
1792}
1793
69ff5ef8
AO
1794static uint8_t virtio_net_get_hash_type(bool hasip4,
1795 bool hasip6,
65f474bb 1796 EthL4HdrProto l4hdr_proto,
4474e37a
YB
1797 uint32_t types)
1798{
69ff5ef8 1799 if (hasip4) {
65f474bb
AO
1800 switch (l4hdr_proto) {
1801 case ETH_L4_HDR_PROTO_TCP:
1802 if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
1803 return NetPktRssIpV4Tcp;
1804 }
1805 break;
1806
1807 case ETH_L4_HDR_PROTO_UDP:
1808 if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
1809 return NetPktRssIpV4Udp;
1810 }
1811 break;
1812
1813 default:
1814 break;
4474e37a 1815 }
65f474bb 1816
4474e37a
YB
1817 if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
1818 return NetPktRssIpV4;
1819 }
69ff5ef8 1820 } else if (hasip6) {
65f474bb
AO
1821 switch (l4hdr_proto) {
1822 case ETH_L4_HDR_PROTO_TCP:
1823 if (types & VIRTIO_NET_RSS_HASH_TYPE_TCP_EX) {
1824 return NetPktRssIpV6TcpEx;
1825 }
1826 if (types & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
1827 return NetPktRssIpV6Tcp;
1828 }
1829 break;
4474e37a 1830
65f474bb
AO
1831 case ETH_L4_HDR_PROTO_UDP:
1832 if (types & VIRTIO_NET_RSS_HASH_TYPE_UDP_EX) {
1833 return NetPktRssIpV6UdpEx;
1834 }
1835 if (types & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
1836 return NetPktRssIpV6Udp;
1837 }
1838 break;
1839
1840 default:
1841 break;
4474e37a 1842 }
65f474bb
AO
1843
1844 if (types & VIRTIO_NET_RSS_HASH_TYPE_IP_EX) {
1845 return NetPktRssIpV6Ex;
4474e37a 1846 }
65f474bb
AO
1847 if (types & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
1848 return NetPktRssIpV6;
4474e37a
YB
1849 }
1850 }
1851 return 0xff;
1852}
1853
1854static int virtio_net_process_rss(NetClientState *nc, const uint8_t *buf,
a4c960ee
AO
1855 size_t size,
1856 struct virtio_net_hdr_v1_hash *hdr)
4474e37a
YB
1857{
1858 VirtIONet *n = qemu_get_nic_opaque(nc);
e22f0603 1859 unsigned int index = nc->queue_index, new_index = index;
4474e37a
YB
1860 struct NetRxPkt *pkt = n->rx_pkt;
1861 uint8_t net_hash_type;
1862 uint32_t hash;
65f474bb
AO
1863 bool hasip4, hasip6;
1864 EthL4HdrProto l4hdr_proto;
e22f0603
YB
1865 static const uint8_t reports[NetPktRssIpV6UdpEx + 1] = {
1866 VIRTIO_NET_HASH_REPORT_IPv4,
1867 VIRTIO_NET_HASH_REPORT_TCPv4,
1868 VIRTIO_NET_HASH_REPORT_TCPv6,
1869 VIRTIO_NET_HASH_REPORT_IPv6,
1870 VIRTIO_NET_HASH_REPORT_IPv6_EX,
1871 VIRTIO_NET_HASH_REPORT_TCPv6_EX,
1872 VIRTIO_NET_HASH_REPORT_UDPv4,
1873 VIRTIO_NET_HASH_REPORT_UDPv6,
1874 VIRTIO_NET_HASH_REPORT_UDPv6_EX
1875 };
2f0fa232
AO
1876 struct iovec iov = {
1877 .iov_base = (void *)buf,
1878 .iov_len = size
1879 };
4474e37a 1880
2f0fa232 1881 net_rx_pkt_set_protocols(pkt, &iov, 1, n->host_hdr_len);
65f474bb
AO
1882 net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
1883 net_hash_type = virtio_net_get_hash_type(hasip4, hasip6, l4hdr_proto,
4474e37a
YB
1884 n->rss_data.hash_types);
1885 if (net_hash_type > NetPktRssIpV6UdpEx) {
e22f0603 1886 if (n->rss_data.populate_hash) {
a4c960ee
AO
1887 hdr->hash_value = VIRTIO_NET_HASH_REPORT_NONE;
1888 hdr->hash_report = 0;
e22f0603
YB
1889 }
1890 return n->rss_data.redirect ? n->rss_data.default_queue : -1;
4474e37a
YB
1891 }
1892
1893 hash = net_rx_pkt_calc_rss_hash(pkt, net_hash_type, n->rss_data.key);
e22f0603
YB
1894
1895 if (n->rss_data.populate_hash) {
a4c960ee
AO
1896 hdr->hash_value = hash;
1897 hdr->hash_report = reports[net_hash_type];
4474e37a 1898 }
e22f0603
YB
1899
1900 if (n->rss_data.redirect) {
1901 new_index = hash & (n->rss_data.indirections_len - 1);
1902 new_index = n->rss_data.indirections_table[new_index];
1903 }
1904
1905 return (index == new_index) ? -1 : new_index;
4474e37a
YB
1906}
1907
97cd965c 1908static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
1981fa9d 1909 size_t size)
fbe78f4f 1910{
cc1f0f45 1911 VirtIONet *n = qemu_get_nic_opaque(nc);
1981fa9d 1912 VirtIONetQueue *q;
17a0ca55 1913 VirtIODevice *vdev = VIRTIO_DEVICE(n);
21cf31c5
DB
1914 QEMU_UNINITIALIZED VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE];
1915 QEMU_UNINITIALIZED size_t lens[VIRTQUEUE_MAX_SIZE];
1916 QEMU_UNINITIALIZED struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
e28fbd1c 1917 struct virtio_net_hdr_v1_hash extra_hdr;
63c58728 1918 unsigned mhdr_cnt = 0;
bedd7e93
JW
1919 size_t offset, i, guest_offset, j;
1920 ssize_t err;
fbe78f4f 1921
e28fbd1c 1922 memset(&extra_hdr, 0, sizeof(extra_hdr));
17437418 1923
1981fa9d 1924 if (n->rss_data.enabled && n->rss_data.enabled_software_rss) {
e28fbd1c 1925 int index = virtio_net_process_rss(nc, buf, size, &extra_hdr);
4474e37a 1926 if (index >= 0) {
1981fa9d 1927 nc = qemu_get_subqueue(n->nic, index % n->curr_queue_pairs);
4474e37a
YB
1928 }
1929 }
1930
162bdb81
AO
1931 if (!virtio_net_can_receive(nc)) {
1932 return -1;
1933 }
1934
1981fa9d
AO
1935 q = virtio_net_get_subqueue(nc);
1936
940cda94 1937 /* hdr_len refers to the header we supply to the guest */
0c87e93e 1938 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
8aeff62d 1939 return 0;
0c87e93e 1940 }
fbe78f4f 1941
3831ab20 1942 if (!receive_filter(n, buf, size))
4f1c942b 1943 return size;
3831ab20 1944
fbe78f4f
AL
1945 offset = i = 0;
1946
1947 while (offset < size) {
51b19ebe 1948 VirtQueueElement *elem;
fbe78f4f 1949 int len, total;
51b19ebe 1950 const struct iovec *sg;
fbe78f4f 1951
22c253d9 1952 total = 0;
fbe78f4f 1953
bedd7e93
JW
1954 if (i == VIRTQUEUE_MAX_SIZE) {
1955 virtio_error(vdev, "virtio-net unexpected long buffer chain");
1956 err = size;
1957 goto err;
1958 }
1959
51b19ebe
PB
1960 elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
1961 if (!elem) {
ba10b9c0
GK
1962 if (i) {
1963 virtio_error(vdev, "virtio-net unexpected empty queue: "
1964 "i %zd mergeable %d offset %zd, size %zd, "
1965 "guest hdr len %zd, host hdr len %zd "
1966 "guest features 0x%" PRIx64,
1967 i, n->mergeable_rx_bufs, offset, size,
1968 n->guest_hdr_len, n->host_hdr_len,
1969 vdev->guest_features);
1970 }
bedd7e93
JW
1971 err = -1;
1972 goto err;
fbe78f4f
AL
1973 }
1974
51b19ebe 1975 if (elem->in_num < 1) {
ba10b9c0
GK
1976 virtio_error(vdev,
1977 "virtio-net receive queue contains no in buffers");
1978 virtqueue_detach_element(q->rx_vq, elem, 0);
1979 g_free(elem);
bedd7e93
JW
1980 err = -1;
1981 goto err;
fbe78f4f
AL
1982 }
1983
51b19ebe 1984 sg = elem->in_sg;
fbe78f4f 1985 if (i == 0) {
c8d28e7e 1986 assert(offset == 0);
63c58728
MT
1987 if (n->mergeable_rx_bufs) {
1988 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
51b19ebe 1989 sg, elem->in_num,
e28fbd1c
AD
1990 offsetof(typeof(extra_hdr), hdr.num_buffers),
1991 sizeof(extra_hdr.hdr.num_buffers));
c17ad4b1 1992 } else {
e28fbd1c 1993 extra_hdr.hdr.num_buffers = cpu_to_le16(1);
63c58728 1994 }
fbe78f4f 1995
e28fbd1c
AD
1996 receive_header(n, sg, elem->in_num, buf, size);
1997 if (n->rss_data.populate_hash) {
1998 offset = offsetof(typeof(extra_hdr), hash_value);
1999 iov_from_buf(sg, elem->in_num, offset,
2000 (char *)&extra_hdr + offset,
2001 sizeof(extra_hdr.hash_value) +
2002 sizeof(extra_hdr.hash_report));
2003 }
2004 offset = n->host_hdr_len;
2005 total += n->guest_hdr_len;
2006 guest_offset = n->guest_hdr_len;
22cc84db
MT
2007 } else {
2008 guest_offset = 0;
fbe78f4f
AL
2009 }
2010
2011 /* copy in packet. ugh */
51b19ebe 2012 len = iov_from_buf(sg, elem->in_num, guest_offset,
dcf6f5e1 2013 buf + offset, size - offset);
fbe78f4f 2014 total += len;
279a4253
MT
2015 offset += len;
2016 /* If buffers can't be merged, at this point we
2017 * must have consumed the complete packet.
2018 * Otherwise, drop it. */
2019 if (!n->mergeable_rx_bufs && offset < size) {
27e57efe 2020 virtqueue_unpop(q->rx_vq, elem, total);
51b19ebe 2021 g_free(elem);
bedd7e93
JW
2022 err = size;
2023 goto err;
279a4253 2024 }
fbe78f4f 2025
bedd7e93
JW
2026 elems[i] = elem;
2027 lens[i] = total;
2028 i++;
fbe78f4f
AL
2029 }
2030
63c58728 2031 if (mhdr_cnt) {
e28fbd1c 2032 virtio_stw_p(vdev, &extra_hdr.hdr.num_buffers, i);
63c58728
MT
2033 iov_from_buf(mhdr_sg, mhdr_cnt,
2034 0,
e28fbd1c
AD
2035 &extra_hdr.hdr.num_buffers,
2036 sizeof extra_hdr.hdr.num_buffers);
44b15bc5 2037 }
fbe78f4f 2038
bedd7e93
JW
2039 for (j = 0; j < i; j++) {
2040 /* signal other side */
2041 virtqueue_fill(q->rx_vq, elems[j], lens[j], j);
2042 g_free(elems[j]);
2043 }
2044
0c87e93e 2045 virtqueue_flush(q->rx_vq, i);
17a0ca55 2046 virtio_notify(vdev, q->rx_vq);
4f1c942b
MM
2047
2048 return size;
bedd7e93
JW
2049
2050err:
2051 for (j = 0; j < i; j++) {
abe300d9 2052 virtqueue_detach_element(q->rx_vq, elems[j], lens[j]);
bedd7e93
JW
2053 g_free(elems[j]);
2054 }
2055
2056 return err;
fbe78f4f
AL
2057}
2058
2974e916 2059static ssize_t virtio_net_do_receive(NetClientState *nc, const uint8_t *buf,
97cd965c
PB
2060 size_t size)
2061{
068ddfa9 2062 RCU_READ_LOCK_GUARD();
97cd965c 2063
1981fa9d 2064 return virtio_net_receive_rcu(nc, buf, size);
97cd965c
PB
2065}
2066
5814c084
PM
2067/*
2068 * Accessors to read and write the IP packet data length field. This
2069 * is a potentially unaligned network-byte-order 16 bit unsigned integer
2070 * pointed to by unit->ip_len.
2071 */
2072static uint16_t read_unit_ip_len(VirtioNetRscUnit *unit)
2073{
2074 return lduw_be_p(unit->ip_plen);
2075}
2076
2077static void write_unit_ip_len(VirtioNetRscUnit *unit, uint16_t l)
2078{
2079 stw_be_p(unit->ip_plen, l);
2080}
2081
2974e916
YB
2082static void virtio_net_rsc_extract_unit4(VirtioNetRscChain *chain,
2083 const uint8_t *buf,
2084 VirtioNetRscUnit *unit)
2085{
2086 uint16_t ip_hdrlen;
2087 struct ip_header *ip;
2088
2089 ip = (struct ip_header *)(buf + chain->n->guest_hdr_len
2090 + sizeof(struct eth_header));
2091 unit->ip = (void *)ip;
2092 ip_hdrlen = (ip->ip_ver_len & 0xF) << 2;
2093 unit->ip_plen = &ip->ip_len;
2094 unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
2095 unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
5814c084 2096 unit->payload = read_unit_ip_len(unit) - ip_hdrlen - unit->tcp_hdrlen;
2974e916
YB
2097}
2098
2099static void virtio_net_rsc_extract_unit6(VirtioNetRscChain *chain,
2100 const uint8_t *buf,
2101 VirtioNetRscUnit *unit)
2102{
2103 struct ip6_header *ip6;
2104
2105 ip6 = (struct ip6_header *)(buf + chain->n->guest_hdr_len
2106 + sizeof(struct eth_header));
2107 unit->ip = ip6;
2108 unit->ip_plen = &(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
78ee6bd0 2109 unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip)
2974e916
YB
2110 + sizeof(struct ip6_header));
2111 unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
2112
2431f4f1 2113 /* There is a difference between payload length in ipv4 and v6,
2974e916 2114 ip header is excluded in ipv6 */
5814c084 2115 unit->payload = read_unit_ip_len(unit) - unit->tcp_hdrlen;
2974e916
YB
2116}
2117
2118static size_t virtio_net_rsc_drain_seg(VirtioNetRscChain *chain,
2119 VirtioNetRscSeg *seg)
2120{
2121 int ret;
dd3d85e8 2122 struct virtio_net_hdr_v1 *h;
2974e916 2123
dd3d85e8 2124 h = (struct virtio_net_hdr_v1 *)seg->buf;
2974e916
YB
2125 h->flags = 0;
2126 h->gso_type = VIRTIO_NET_HDR_GSO_NONE;
2127
2128 if (seg->is_coalesced) {
dd3d85e8
YB
2129 h->rsc.segments = seg->packets;
2130 h->rsc.dup_acks = seg->dup_ack;
2974e916
YB
2131 h->flags = VIRTIO_NET_HDR_F_RSC_INFO;
2132 if (chain->proto == ETH_P_IP) {
2133 h->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
2134 } else {
2135 h->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
2136 }
2137 }
2138
2139 ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
2140 QTAILQ_REMOVE(&chain->buffers, seg, next);
2141 g_free(seg->buf);
2142 g_free(seg);
2143
2144 return ret;
2145}
2146
2147static void virtio_net_rsc_purge(void *opq)
2148{
2149 VirtioNetRscSeg *seg, *rn;
2150 VirtioNetRscChain *chain = (VirtioNetRscChain *)opq;
2151
2152 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
2153 if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
2154 chain->stat.purge_failed++;
2155 continue;
2156 }
2157 }
2158
2159 chain->stat.timer++;
2160 if (!QTAILQ_EMPTY(&chain->buffers)) {
2161 timer_mod(chain->drain_timer,
44bc14fa 2162 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + chain->n->rsc_timeout);
2974e916
YB
2163 }
2164}
2165
2166static void virtio_net_rsc_cleanup(VirtIONet *n)
2167{
2168 VirtioNetRscChain *chain, *rn_chain;
2169 VirtioNetRscSeg *seg, *rn_seg;
2170
2171 QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
2172 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
2173 QTAILQ_REMOVE(&chain->buffers, seg, next);
2174 g_free(seg->buf);
2175 g_free(seg);
2176 }
2177
2974e916
YB
2178 timer_free(chain->drain_timer);
2179 QTAILQ_REMOVE(&n->rsc_chains, chain, next);
2180 g_free(chain);
2181 }
2182}
2183
2184static void virtio_net_rsc_cache_buf(VirtioNetRscChain *chain,
2185 NetClientState *nc,
2186 const uint8_t *buf, size_t size)
2187{
2188 uint16_t hdr_len;
2189 VirtioNetRscSeg *seg;
2190
2191 hdr_len = chain->n->guest_hdr_len;
b21e2380 2192 seg = g_new(VirtioNetRscSeg, 1);
2974e916
YB
2193 seg->buf = g_malloc(hdr_len + sizeof(struct eth_header)
2194 + sizeof(struct ip6_header) + VIRTIO_NET_MAX_TCP_PAYLOAD);
2195 memcpy(seg->buf, buf, size);
2196 seg->size = size;
2197 seg->packets = 1;
2198 seg->dup_ack = 0;
2199 seg->is_coalesced = 0;
2200 seg->nc = nc;
2201
2202 QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
2203 chain->stat.cache++;
2204
2205 switch (chain->proto) {
2206 case ETH_P_IP:
2207 virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
2208 break;
2209 case ETH_P_IPV6:
2210 virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
2211 break;
2212 default:
2213 g_assert_not_reached();
2214 }
2215}
2216
2217static int32_t virtio_net_rsc_handle_ack(VirtioNetRscChain *chain,
2218 VirtioNetRscSeg *seg,
2219 const uint8_t *buf,
2220 struct tcp_header *n_tcp,
2221 struct tcp_header *o_tcp)
2222{
2223 uint32_t nack, oack;
2224 uint16_t nwin, owin;
2225
2226 nack = htonl(n_tcp->th_ack);
2227 nwin = htons(n_tcp->th_win);
2228 oack = htonl(o_tcp->th_ack);
2229 owin = htons(o_tcp->th_win);
2230
2231 if ((nack - oack) >= VIRTIO_NET_MAX_TCP_PAYLOAD) {
2232 chain->stat.ack_out_of_win++;
2233 return RSC_FINAL;
2234 } else if (nack == oack) {
2235 /* duplicated ack or window probe */
2236 if (nwin == owin) {
2237 /* duplicated ack, add dup ack count due to whql test up to 1 */
2238 chain->stat.dup_ack++;
2239 return RSC_FINAL;
2240 } else {
2241 /* Coalesce window update */
2242 o_tcp->th_win = n_tcp->th_win;
2243 chain->stat.win_update++;
2244 return RSC_COALESCE;
2245 }
2246 } else {
2247 /* pure ack, go to 'C', finalize*/
2248 chain->stat.pure_ack++;
2249 return RSC_FINAL;
2250 }
2251}
2252
2253static int32_t virtio_net_rsc_coalesce_data(VirtioNetRscChain *chain,
2254 VirtioNetRscSeg *seg,
2255 const uint8_t *buf,
2256 VirtioNetRscUnit *n_unit)
2257{
2258 void *data;
2259 uint16_t o_ip_len;
2260 uint32_t nseq, oseq;
2261 VirtioNetRscUnit *o_unit;
2262
2263 o_unit = &seg->unit;
5814c084 2264 o_ip_len = read_unit_ip_len(o_unit);
2974e916
YB
2265 nseq = htonl(n_unit->tcp->th_seq);
2266 oseq = htonl(o_unit->tcp->th_seq);
2267
2268 /* out of order or retransmitted. */
2269 if ((nseq - oseq) > VIRTIO_NET_MAX_TCP_PAYLOAD) {
2270 chain->stat.data_out_of_win++;
2271 return RSC_FINAL;
2272 }
2273
2274 data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
2275 if (nseq == oseq) {
2276 if ((o_unit->payload == 0) && n_unit->payload) {
2277 /* From no payload to payload, normal case, not a dup ack or etc */
2278 chain->stat.data_after_pure_ack++;
2279 goto coalesce;
2280 } else {
2281 return virtio_net_rsc_handle_ack(chain, seg, buf,
2282 n_unit->tcp, o_unit->tcp);
2283 }
2284 } else if ((nseq - oseq) != o_unit->payload) {
2285 /* Not a consistent packet, out of order */
2286 chain->stat.data_out_of_order++;
2287 return RSC_FINAL;
2288 } else {
2289coalesce:
2290 if ((o_ip_len + n_unit->payload) > chain->max_payload) {
2291 chain->stat.over_size++;
2292 return RSC_FINAL;
2293 }
2294
2295 /* Here comes the right data, the payload length in v4/v6 is different,
2296 so use the field value to update and record the new data len */
2297 o_unit->payload += n_unit->payload; /* update new data len */
2298
2299 /* update field in ip header */
5814c084 2300 write_unit_ip_len(o_unit, o_ip_len + n_unit->payload);
2974e916
YB
2301
2302 /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
2303 for windows guest, while this may change the behavior for linux
2304 guest (only if it uses RSC feature). */
2305 o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
2306
2307 o_unit->tcp->th_ack = n_unit->tcp->th_ack;
2308 o_unit->tcp->th_win = n_unit->tcp->th_win;
2309
2310 memmove(seg->buf + seg->size, data, n_unit->payload);
2311 seg->size += n_unit->payload;
2312 seg->packets++;
2313 chain->stat.coalesced++;
2314 return RSC_COALESCE;
2315 }
2316}
2317
2318static int32_t virtio_net_rsc_coalesce4(VirtioNetRscChain *chain,
2319 VirtioNetRscSeg *seg,
2320 const uint8_t *buf, size_t size,
2321 VirtioNetRscUnit *unit)
2322{
2323 struct ip_header *ip1, *ip2;
2324
2325 ip1 = (struct ip_header *)(unit->ip);
2326 ip2 = (struct ip_header *)(seg->unit.ip);
2327 if ((ip1->ip_src ^ ip2->ip_src) || (ip1->ip_dst ^ ip2->ip_dst)
2328 || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
2329 || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
2330 chain->stat.no_match++;
2331 return RSC_NO_MATCH;
2332 }
2333
2334 return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
2335}
2336
2337static int32_t virtio_net_rsc_coalesce6(VirtioNetRscChain *chain,
2338 VirtioNetRscSeg *seg,
2339 const uint8_t *buf, size_t size,
2340 VirtioNetRscUnit *unit)
2341{
2342 struct ip6_header *ip1, *ip2;
2343
2344 ip1 = (struct ip6_header *)(unit->ip);
2345 ip2 = (struct ip6_header *)(seg->unit.ip);
2346 if (memcmp(&ip1->ip6_src, &ip2->ip6_src, sizeof(struct in6_address))
2347 || memcmp(&ip1->ip6_dst, &ip2->ip6_dst, sizeof(struct in6_address))
2348 || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
2349 || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
2350 chain->stat.no_match++;
2351 return RSC_NO_MATCH;
2352 }
2353
2354 return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
2355}
2356
2357/* Packets with 'SYN' should bypass, other flag should be sent after drain
2358 * to prevent out of order */
2359static int virtio_net_rsc_tcp_ctrl_check(VirtioNetRscChain *chain,
2360 struct tcp_header *tcp)
2361{
2362 uint16_t tcp_hdr;
2363 uint16_t tcp_flag;
2364
2365 tcp_flag = htons(tcp->th_offset_flags);
2366 tcp_hdr = (tcp_flag & VIRTIO_NET_TCP_HDR_LENGTH) >> 10;
2367 tcp_flag &= VIRTIO_NET_TCP_FLAG;
2974e916
YB
2368 if (tcp_flag & TH_SYN) {
2369 chain->stat.tcp_syn++;
2370 return RSC_BYPASS;
2371 }
2372
2373 if (tcp_flag & (TH_FIN | TH_URG | TH_RST | TH_ECE | TH_CWR)) {
2374 chain->stat.tcp_ctrl_drain++;
2375 return RSC_FINAL;
2376 }
2377
2378 if (tcp_hdr > sizeof(struct tcp_header)) {
2379 chain->stat.tcp_all_opt++;
2380 return RSC_FINAL;
2381 }
2382
2383 return RSC_CANDIDATE;
2384}
2385
2386static size_t virtio_net_rsc_do_coalesce(VirtioNetRscChain *chain,
2387 NetClientState *nc,
2388 const uint8_t *buf, size_t size,
2389 VirtioNetRscUnit *unit)
2390{
2391 int ret;
2392 VirtioNetRscSeg *seg, *nseg;
2393
2394 if (QTAILQ_EMPTY(&chain->buffers)) {
2395 chain->stat.empty_cache++;
2396 virtio_net_rsc_cache_buf(chain, nc, buf, size);
2397 timer_mod(chain->drain_timer,
44bc14fa 2398 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + chain->n->rsc_timeout);
2974e916
YB
2399 return size;
2400 }
2401
2402 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
2403 if (chain->proto == ETH_P_IP) {
2404 ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
2405 } else {
2406 ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
2407 }
2408
2409 if (ret == RSC_FINAL) {
2410 if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
2411 /* Send failed */
2412 chain->stat.final_failed++;
2413 return 0;
2414 }
2415
2416 /* Send current packet */
2417 return virtio_net_do_receive(nc, buf, size);
2418 } else if (ret == RSC_NO_MATCH) {
2419 continue;
2420 } else {
2421 /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
2422 seg->is_coalesced = 1;
2423 return size;
2424 }
2425 }
2426
2427 chain->stat.no_match_cache++;
2428 virtio_net_rsc_cache_buf(chain, nc, buf, size);
2429 return size;
2430}
2431
2432/* Drain a connection data, this is to avoid out of order segments */
2433static size_t virtio_net_rsc_drain_flow(VirtioNetRscChain *chain,
2434 NetClientState *nc,
2435 const uint8_t *buf, size_t size,
2436 uint16_t ip_start, uint16_t ip_size,
2437 uint16_t tcp_port)
2438{
2439 VirtioNetRscSeg *seg, *nseg;
2440 uint32_t ppair1, ppair2;
2441
2442 ppair1 = *(uint32_t *)(buf + tcp_port);
2443 QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
2444 ppair2 = *(uint32_t *)(seg->buf + tcp_port);
2445 if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size)
2446 || (ppair1 != ppair2)) {
2447 continue;
2448 }
2449 if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
2450 chain->stat.drain_failed++;
2451 }
2452
2453 break;
2454 }
2455
2456 return virtio_net_do_receive(nc, buf, size);
2457}
2458
2459static int32_t virtio_net_rsc_sanity_check4(VirtioNetRscChain *chain,
2460 struct ip_header *ip,
2461 const uint8_t *buf, size_t size)
2462{
2463 uint16_t ip_len;
2464
2465 /* Not an ipv4 packet */
2466 if (((ip->ip_ver_len & 0xF0) >> 4) != IP_HEADER_VERSION_4) {
2467 chain->stat.ip_option++;
2468 return RSC_BYPASS;
2469 }
2470
2471 /* Don't handle packets with ip option */
2472 if ((ip->ip_ver_len & 0xF) != VIRTIO_NET_IP4_HEADER_LENGTH) {
2473 chain->stat.ip_option++;
2474 return RSC_BYPASS;
2475 }
2476
2477 if (ip->ip_p != IPPROTO_TCP) {
2478 chain->stat.bypass_not_tcp++;
2479 return RSC_BYPASS;
2480 }
2481
2482 /* Don't handle packets with ip fragment */
2483 if (!(htons(ip->ip_off) & IP_DF)) {
2484 chain->stat.ip_frag++;
2485 return RSC_BYPASS;
2486 }
2487
2488 /* Don't handle packets with ecn flag */
2489 if (IPTOS_ECN(ip->ip_tos)) {
2490 chain->stat.ip_ecn++;
2491 return RSC_BYPASS;
2492 }
2493
2494 ip_len = htons(ip->ip_len);
2495 if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header))
2496 || ip_len > (size - chain->n->guest_hdr_len -
2497 sizeof(struct eth_header))) {
2498 chain->stat.ip_hacked++;
2499 return RSC_BYPASS;
2500 }
2501
2502 return RSC_CANDIDATE;
2503}
2504
2505static size_t virtio_net_rsc_receive4(VirtioNetRscChain *chain,
2506 NetClientState *nc,
2507 const uint8_t *buf, size_t size)
2508{
2509 int32_t ret;
2510 uint16_t hdr_len;
2511 VirtioNetRscUnit unit;
2512
2513 hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
2514
2515 if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header)
2516 + sizeof(struct tcp_header))) {
2517 chain->stat.bypass_not_tcp++;
2518 return virtio_net_do_receive(nc, buf, size);
2519 }
2520
2521 virtio_net_rsc_extract_unit4(chain, buf, &unit);
2522 if (virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)
2523 != RSC_CANDIDATE) {
2524 return virtio_net_do_receive(nc, buf, size);
2525 }
2526
2527 ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
2528 if (ret == RSC_BYPASS) {
2529 return virtio_net_do_receive(nc, buf, size);
2530 } else if (ret == RSC_FINAL) {
2531 return virtio_net_rsc_drain_flow(chain, nc, buf, size,
2532 ((hdr_len + sizeof(struct eth_header)) + 12),
2533 VIRTIO_NET_IP4_ADDR_SIZE,
2534 hdr_len + sizeof(struct eth_header) + sizeof(struct ip_header));
2535 }
2536
2537 return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
2538}
2539
2540static int32_t virtio_net_rsc_sanity_check6(VirtioNetRscChain *chain,
2541 struct ip6_header *ip6,
2542 const uint8_t *buf, size_t size)
2543{
2544 uint16_t ip_len;
2545
2546 if (((ip6->ip6_ctlun.ip6_un1.ip6_un1_flow & 0xF0) >> 4)
2547 != IP_HEADER_VERSION_6) {
2548 return RSC_BYPASS;
2549 }
2550
2551 /* Both option and protocol is checked in this */
2552 if (ip6->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
2553 chain->stat.bypass_not_tcp++;
2554 return RSC_BYPASS;
2555 }
2556
2557 ip_len = htons(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
2558 if (ip_len < sizeof(struct tcp_header) ||
2559 ip_len > (size - chain->n->guest_hdr_len - sizeof(struct eth_header)
2560 - sizeof(struct ip6_header))) {
2561 chain->stat.ip_hacked++;
2562 return RSC_BYPASS;
2563 }
2564
2565 /* Don't handle packets with ecn flag */
2566 if (IP6_ECN(ip6->ip6_ctlun.ip6_un3.ip6_un3_ecn)) {
2567 chain->stat.ip_ecn++;
2568 return RSC_BYPASS;
2569 }
2570
2571 return RSC_CANDIDATE;
2572}
2573
2574static size_t virtio_net_rsc_receive6(void *opq, NetClientState *nc,
2575 const uint8_t *buf, size_t size)
2576{
2577 int32_t ret;
2578 uint16_t hdr_len;
2579 VirtioNetRscChain *chain;
2580 VirtioNetRscUnit unit;
2581
3d558330 2582 chain = opq;
2974e916
YB
2583 hdr_len = ((VirtIONet *)(chain->n))->guest_hdr_len;
2584
2585 if (size < (hdr_len + sizeof(struct eth_header) + sizeof(struct ip6_header)
2586 + sizeof(tcp_header))) {
2587 return virtio_net_do_receive(nc, buf, size);
2588 }
2589
2590 virtio_net_rsc_extract_unit6(chain, buf, &unit);
2591 if (RSC_CANDIDATE != virtio_net_rsc_sanity_check6(chain,
2592 unit.ip, buf, size)) {
2593 return virtio_net_do_receive(nc, buf, size);
2594 }
2595
2596 ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
2597 if (ret == RSC_BYPASS) {
2598 return virtio_net_do_receive(nc, buf, size);
2599 } else if (ret == RSC_FINAL) {
2600 return virtio_net_rsc_drain_flow(chain, nc, buf, size,
2601 ((hdr_len + sizeof(struct eth_header)) + 8),
2602 VIRTIO_NET_IP6_ADDR_SIZE,
2603 hdr_len + sizeof(struct eth_header)
2604 + sizeof(struct ip6_header));
2605 }
2606
2607 return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
2608}
2609
2610static VirtioNetRscChain *virtio_net_rsc_lookup_chain(VirtIONet *n,
2611 NetClientState *nc,
2612 uint16_t proto)
2613{
2614 VirtioNetRscChain *chain;
2615
2616 if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
2617 return NULL;
2618 }
2619
2620 QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
2621 if (chain->proto == proto) {
2622 return chain;
2623 }
2624 }
2625
2626 chain = g_malloc(sizeof(*chain));
2627 chain->n = n;
2628 chain->proto = proto;
2629 if (proto == (uint16_t)ETH_P_IP) {
2630 chain->max_payload = VIRTIO_NET_MAX_IP4_PAYLOAD;
2631 chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
2632 } else {
2633 chain->max_payload = VIRTIO_NET_MAX_IP6_PAYLOAD;
2634 chain->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
2635 }
44bc14fa 2636 chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2974e916
YB
2637 virtio_net_rsc_purge, chain);
2638 memset(&chain->stat, 0, sizeof(chain->stat));
2639
2640 QTAILQ_INIT(&chain->buffers);
2641 QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
2642
2643 return chain;
2644}
2645
2646static ssize_t virtio_net_rsc_receive(NetClientState *nc,
2647 const uint8_t *buf,
2648 size_t size)
2649{
2650 uint16_t proto;
2651 VirtioNetRscChain *chain;
2652 struct eth_header *eth;
2653 VirtIONet *n;
2654
2655 n = qemu_get_nic_opaque(nc);
2656 if (size < (n->host_hdr_len + sizeof(struct eth_header))) {
2657 return virtio_net_do_receive(nc, buf, size);
2658 }
2659
2660 eth = (struct eth_header *)(buf + n->guest_hdr_len);
2661 proto = htons(eth->h_proto);
2662
2663 chain = virtio_net_rsc_lookup_chain(n, nc, proto);
2664 if (chain) {
2665 chain->stat.received++;
2666 if (proto == (uint16_t)ETH_P_IP && n->rsc4_enabled) {
2667 return virtio_net_rsc_receive4(chain, nc, buf, size);
2668 } else if (proto == (uint16_t)ETH_P_IPV6 && n->rsc6_enabled) {
2669 return virtio_net_rsc_receive6(chain, nc, buf, size);
2670 }
2671 }
2672 return virtio_net_do_receive(nc, buf, size);
2673}
2674
2675static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
2676 size_t size)
2677{
2678 VirtIONet *n = qemu_get_nic_opaque(nc);
2679 if ((n->rsc4_enabled || n->rsc6_enabled)) {
2680 return virtio_net_rsc_receive(nc, buf, size);
2681 } else {
2682 return virtio_net_do_receive(nc, buf, size);
2683 }
2684}
2685
0c87e93e 2686static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
6243375f 2687
4e68f7a0 2688static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
6243375f 2689{
cc1f0f45 2690 VirtIONet *n = qemu_get_nic_opaque(nc);
fed699f9 2691 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
17a0ca55 2692 VirtIODevice *vdev = VIRTIO_DEVICE(n);
df8d0708 2693 int ret;
6243375f 2694
51b19ebe 2695 virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
17a0ca55 2696 virtio_notify(vdev, q->tx_vq);
6243375f 2697
51b19ebe
PB
2698 g_free(q->async_tx.elem);
2699 q->async_tx.elem = NULL;
6243375f 2700
0c87e93e 2701 virtio_queue_set_notification(q->tx_vq, 1);
df8d0708 2702 ret = virtio_net_flush_tx(q);
7550a822 2703 if (ret >= n->tx_burst) {
df8d0708
LV
2704 /*
2705 * the flush has been stopped by tx_burst
2706 * we will not receive notification for the
2707 * remainining part, so re-schedule
2708 */
2709 virtio_queue_set_notification(q->tx_vq, 0);
7550a822 2710 if (q->tx_bh) {
a0bf401b 2711 replay_bh_schedule_event(q->tx_bh);
7550a822
LV
2712 } else {
2713 timer_mod(q->tx_timer,
2714 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
2715 }
df8d0708
LV
2716 q->tx_waiting = 1;
2717 }
6243375f
MM
2718}
2719
fbe78f4f 2720/* TX */
0c87e93e 2721static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
fbe78f4f 2722{
0c87e93e 2723 VirtIONet *n = q->n;
17a0ca55 2724 VirtIODevice *vdev = VIRTIO_DEVICE(n);
51b19ebe 2725 VirtQueueElement *elem;
e3f30488 2726 int32_t num_packets = 0;
fed699f9 2727 int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
17a0ca55 2728 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
e3f30488
AW
2729 return num_packets;
2730 }
fbe78f4f 2731
51b19ebe 2732 if (q->async_tx.elem) {
0c87e93e 2733 virtio_queue_set_notification(q->tx_vq, 0);
e3f30488 2734 return num_packets;
6243375f
MM
2735 }
2736
51b19ebe 2737 for (;;) {
bd89dd98 2738 ssize_t ret;
51b19ebe
PB
2739 unsigned int out_num;
2740 struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
942f420e 2741 struct virtio_net_hdr vhdr;
fbe78f4f 2742
51b19ebe
PB
2743 elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
2744 if (!elem) {
2745 break;
2746 }
2747
2748 out_num = elem->out_num;
2749 out_sg = elem->out_sg;
7b80d08e 2750 if (out_num < 1) {
fa5e56c2 2751 virtio_error(vdev, "virtio-net header not in first element");
2c3e4e2d 2752 goto detach;
fbe78f4f
AL
2753 }
2754
ad57f700 2755 if (n->needs_vnet_hdr_swap) {
942f420e
AO
2756 if (iov_to_buf(out_sg, out_num, 0, &vhdr, sizeof(vhdr)) <
2757 sizeof(vhdr)) {
fa5e56c2 2758 virtio_error(vdev, "virtio-net header incorrect");
2c3e4e2d 2759 goto detach;
032a74a1 2760 }
942f420e 2761 virtio_net_hdr_swap(vdev, &vhdr);
ad57f700 2762 sg2[0].iov_base = &vhdr;
942f420e 2763 sg2[0].iov_len = sizeof(vhdr);
ad57f700 2764 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, out_sg, out_num,
942f420e 2765 sizeof(vhdr), -1);
ad57f700
AO
2766 if (out_num == VIRTQUEUE_MAX_SIZE) {
2767 goto drop;
7d37435b 2768 }
ad57f700
AO
2769 out_num += 1;
2770 out_sg = sg2;
032a74a1 2771 }
14761f9c
MT
2772 /*
2773 * If host wants to see the guest header as is, we can
2774 * pass it on unchanged. Otherwise, copy just the parts
2775 * that host is interested in.
2776 */
2777 assert(n->host_hdr_len <= n->guest_hdr_len);
2778 if (n->host_hdr_len != n->guest_hdr_len) {
d4f471eb
DF
2779 if (iov_size(out_sg, out_num) < n->guest_hdr_len) {
2780 virtio_error(vdev, "virtio-net header is invalid");
2781 goto detach;
2782 }
14761f9c
MT
2783 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
2784 out_sg, out_num,
2785 0, n->host_hdr_len);
2786 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
2787 out_sg, out_num,
2788 n->guest_hdr_len, -1);
2789 out_num = sg_num;
2790 out_sg = sg;
2c3e4e2d
AD
2791
2792 if (out_num < 1) {
2793 virtio_error(vdev, "virtio-net nothing to send");
2794 goto detach;
2795 }
fbe78f4f
AL
2796 }
2797
fed699f9
JW
2798 ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
2799 out_sg, out_num, virtio_net_tx_complete);
6243375f 2800 if (ret == 0) {
0c87e93e
JW
2801 virtio_queue_set_notification(q->tx_vq, 0);
2802 q->async_tx.elem = elem;
e3f30488 2803 return -EBUSY;
6243375f
MM
2804 }
2805
feb93f36 2806drop:
51b19ebe 2807 virtqueue_push(q->tx_vq, elem, 0);
17a0ca55 2808 virtio_notify(vdev, q->tx_vq);
51b19ebe 2809 g_free(elem);
e3f30488
AW
2810
2811 if (++num_packets >= n->tx_burst) {
2812 break;
2813 }
fbe78f4f 2814 }
e3f30488 2815 return num_packets;
2c3e4e2d
AD
2816
2817detach:
2818 virtqueue_detach_element(q->tx_vq, elem, 0);
2819 g_free(elem);
2820 return -EINVAL;
fbe78f4f
AL
2821}
2822
7550a822
LV
2823static void virtio_net_tx_timer(void *opaque);
2824
a697a334 2825static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
fbe78f4f 2826{
17a0ca55 2827 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9 2828 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
fbe78f4f 2829
283e2c2a
YB
2830 if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2831 virtio_net_drop_tx_queue_data(vdev, vq);
2832 return;
2833 }
2834
783e7706 2835 /* This happens when device was stopped but VCPU wasn't. */
17a0ca55 2836 if (!vdev->vm_running) {
0c87e93e 2837 q->tx_waiting = 1;
783e7706
MT
2838 return;
2839 }
2840
0c87e93e 2841 if (q->tx_waiting) {
7550a822 2842 /* We already have queued packets, immediately flush */
bc72ad67 2843 timer_del(q->tx_timer);
7550a822 2844 virtio_net_tx_timer(q);
fbe78f4f 2845 } else {
7550a822 2846 /* re-arm timer to flush it (and more) on next tick */
bc72ad67 2847 timer_mod(q->tx_timer,
7550a822 2848 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
0c87e93e 2849 q->tx_waiting = 1;
fbe78f4f
AL
2850 virtio_queue_set_notification(vq, 0);
2851 }
2852}
2853
a697a334
AW
2854static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
2855{
17a0ca55 2856 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9 2857 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
a697a334 2858
4c54f5bc
YW
2859 if (unlikely(n->vhost_started)) {
2860 return;
2861 }
2862
283e2c2a
YB
2863 if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
2864 virtio_net_drop_tx_queue_data(vdev, vq);
2865 return;
2866 }
2867
0c87e93e 2868 if (unlikely(q->tx_waiting)) {
a697a334
AW
2869 return;
2870 }
0c87e93e 2871 q->tx_waiting = 1;
783e7706 2872 /* This happens when device was stopped but VCPU wasn't. */
17a0ca55 2873 if (!vdev->vm_running) {
783e7706
MT
2874 return;
2875 }
a697a334 2876 virtio_queue_set_notification(vq, 0);
a0bf401b 2877 replay_bh_schedule_event(q->tx_bh);
a697a334
AW
2878}
2879
fbe78f4f
AL
2880static void virtio_net_tx_timer(void *opaque)
2881{
0c87e93e
JW
2882 VirtIONetQueue *q = opaque;
2883 VirtIONet *n = q->n;
17a0ca55 2884 VirtIODevice *vdev = VIRTIO_DEVICE(n);
7550a822
LV
2885 int ret;
2886
e8bcf842
MT
2887 /* This happens when device was stopped but BH wasn't. */
2888 if (!vdev->vm_running) {
2889 /* Make sure tx waiting is set, so we'll run when restarted. */
2890 assert(q->tx_waiting);
2891 return;
2892 }
fbe78f4f 2893
0c87e93e 2894 q->tx_waiting = 0;
fbe78f4f
AL
2895
2896 /* Just in case the driver is not ready on more */
17a0ca55 2897 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
fbe78f4f 2898 return;
17a0ca55 2899 }
fbe78f4f 2900
7550a822
LV
2901 ret = virtio_net_flush_tx(q);
2902 if (ret == -EBUSY || ret == -EINVAL) {
2903 return;
2904 }
2905 /*
2906 * If we flush a full burst of packets, assume there are
2907 * more coming and immediately rearm
2908 */
2909 if (ret >= n->tx_burst) {
2910 q->tx_waiting = 1;
2911 timer_mod(q->tx_timer,
2912 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
2913 return;
2914 }
2915 /*
2916 * If less than a full burst, re-enable notification and flush
2917 * anything that may have come in while we weren't looking. If
2918 * we find something, assume the guest is still active and rearm
2919 */
0c87e93e 2920 virtio_queue_set_notification(q->tx_vq, 1);
7550a822
LV
2921 ret = virtio_net_flush_tx(q);
2922 if (ret > 0) {
2923 virtio_queue_set_notification(q->tx_vq, 0);
2924 q->tx_waiting = 1;
2925 timer_mod(q->tx_timer,
2926 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
2927 }
fbe78f4f
AL
2928}
2929
a697a334
AW
2930static void virtio_net_tx_bh(void *opaque)
2931{
0c87e93e
JW
2932 VirtIONetQueue *q = opaque;
2933 VirtIONet *n = q->n;
17a0ca55 2934 VirtIODevice *vdev = VIRTIO_DEVICE(n);
a697a334
AW
2935 int32_t ret;
2936
e8bcf842
MT
2937 /* This happens when device was stopped but BH wasn't. */
2938 if (!vdev->vm_running) {
2939 /* Make sure tx waiting is set, so we'll run when restarted. */
2940 assert(q->tx_waiting);
2941 return;
2942 }
783e7706 2943
0c87e93e 2944 q->tx_waiting = 0;
a697a334
AW
2945
2946 /* Just in case the driver is not ready on more */
17a0ca55 2947 if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
a697a334 2948 return;
17a0ca55 2949 }
a697a334 2950
0c87e93e 2951 ret = virtio_net_flush_tx(q);
fa5e56c2
GK
2952 if (ret == -EBUSY || ret == -EINVAL) {
2953 return; /* Notification re-enable handled by tx_complete or device
2954 * broken */
a697a334
AW
2955 }
2956
2957 /* If we flush a full burst of packets, assume there are
2958 * more coming and immediately reschedule */
2959 if (ret >= n->tx_burst) {
a0bf401b 2960 replay_bh_schedule_event(q->tx_bh);
0c87e93e 2961 q->tx_waiting = 1;
a697a334
AW
2962 return;
2963 }
2964
2965 /* If less than a full burst, re-enable notification and flush
2966 * anything that may have come in while we weren't looking. If
2967 * we find something, assume the guest is still active and reschedule */
0c87e93e 2968 virtio_queue_set_notification(q->tx_vq, 1);
fa5e56c2
GK
2969 ret = virtio_net_flush_tx(q);
2970 if (ret == -EINVAL) {
2971 return;
2972 } else if (ret > 0) {
0c87e93e 2973 virtio_queue_set_notification(q->tx_vq, 0);
a0bf401b 2974 replay_bh_schedule_event(q->tx_bh);
0c87e93e 2975 q->tx_waiting = 1;
a697a334
AW
2976 }
2977}
2978
f9d6dbf0
WC
2979static void virtio_net_add_queue(VirtIONet *n, int index)
2980{
2981 VirtIODevice *vdev = VIRTIO_DEVICE(n);
2982
1c0fbfa3
MT
2983 n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
2984 virtio_net_handle_rx);
9b02e161 2985
f9d6dbf0
WC
2986 if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
2987 n->vqs[index].tx_vq =
9b02e161
WW
2988 virtio_add_queue(vdev, n->net_conf.tx_queue_size,
2989 virtio_net_handle_tx_timer);
f9d6dbf0
WC
2990 n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2991 virtio_net_tx_timer,
2992 &n->vqs[index]);
2993 } else {
2994 n->vqs[index].tx_vq =
9b02e161
WW
2995 virtio_add_queue(vdev, n->net_conf.tx_queue_size,
2996 virtio_net_handle_tx_bh);
f63192b0
AB
2997 n->vqs[index].tx_bh = qemu_bh_new_guarded(virtio_net_tx_bh, &n->vqs[index],
2998 &DEVICE(vdev)->mem_reentrancy_guard);
f9d6dbf0
WC
2999 }
3000
3001 n->vqs[index].tx_waiting = 0;
3002 n->vqs[index].n = n;
3003}
3004
3005static void virtio_net_del_queue(VirtIONet *n, int index)
3006{
3007 VirtIODevice *vdev = VIRTIO_DEVICE(n);
3008 VirtIONetQueue *q = &n->vqs[index];
3009 NetClientState *nc = qemu_get_subqueue(n->nic, index);
3010
3011 qemu_purge_queued_packets(nc);
3012
3013 virtio_del_queue(vdev, index * 2);
3014 if (q->tx_timer) {
f9d6dbf0 3015 timer_free(q->tx_timer);
f989c30c 3016 q->tx_timer = NULL;
f9d6dbf0
WC
3017 } else {
3018 qemu_bh_delete(q->tx_bh);
f989c30c 3019 q->tx_bh = NULL;
f9d6dbf0 3020 }
f989c30c 3021 q->tx_waiting = 0;
f9d6dbf0
WC
3022 virtio_del_queue(vdev, index * 2 + 1);
3023}
3024
441537f1 3025static void virtio_net_change_num_queue_pairs(VirtIONet *n, int new_max_queue_pairs)
f9d6dbf0
WC
3026{
3027 VirtIODevice *vdev = VIRTIO_DEVICE(n);
3028 int old_num_queues = virtio_get_num_queues(vdev);
441537f1 3029 int new_num_queues = new_max_queue_pairs * 2 + 1;
f9d6dbf0
WC
3030 int i;
3031
3032 assert(old_num_queues >= 3);
3033 assert(old_num_queues % 2 == 1);
3034
3035 if (old_num_queues == new_num_queues) {
3036 return;
3037 }
3038
3039 /*
3040 * We always need to remove and add ctrl vq if
3041 * old_num_queues != new_num_queues. Remove ctrl_vq first,
20f86a75 3042 * and then we only enter one of the following two loops.
f9d6dbf0
WC
3043 */
3044 virtio_del_queue(vdev, old_num_queues - 1);
3045
3046 for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
3047 /* new_num_queues < old_num_queues */
3048 virtio_net_del_queue(n, i / 2);
3049 }
3050
3051 for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
3052 /* new_num_queues > old_num_queues */
3053 virtio_net_add_queue(n, i / 2);
3054 }
3055
3056 /* add ctrl_vq last */
3057 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
3058}
3059
ec57db16 3060static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
fed699f9 3061{
441537f1 3062 int max = multiqueue ? n->max_queue_pairs : 1;
f9d6dbf0 3063
fed699f9 3064 n->multiqueue = multiqueue;
441537f1 3065 virtio_net_change_num_queue_pairs(n, max);
fed699f9 3066
441537f1 3067 virtio_net_set_queue_pairs(n);
fed699f9
JW
3068}
3069
9379ea9d
AO
3070static int virtio_net_pre_load_queues(VirtIODevice *vdev)
3071{
3072 virtio_net_set_multiqueue(VIRTIO_NET(vdev),
3073 virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_RSS) ||
3074 virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MQ));
3075
3076 return 0;
3077}
3078
982b78c5 3079static int virtio_net_post_load_device(void *opaque, int version_id)
037dab2f 3080{
982b78c5
DDAG
3081 VirtIONet *n = opaque;
3082 VirtIODevice *vdev = VIRTIO_DEVICE(n);
037dab2f 3083 int i, link_down;
fbe78f4f 3084
9d8c6a25 3085 trace_virtio_net_post_load_device();
982b78c5 3086 virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
95129d6f 3087 virtio_vdev_has_feature(vdev,
e22f0603
YB
3088 VIRTIO_F_VERSION_1),
3089 virtio_vdev_has_feature(vdev,
3090 VIRTIO_NET_F_HASH_REPORT));
fbe78f4f 3091
76010cb3 3092 /* MAC_TABLE_ENTRIES may be different from the saved image */
982b78c5 3093 if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
76010cb3 3094 n->mac_table.in_use = 0;
b6503ed9 3095 }
0ce0e8f4 3096
982b78c5 3097 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
6c666823
MT
3098 n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
3099 }
3100
7788c3f2
MS
3101 /*
3102 * curr_guest_offloads will be later overwritten by the
3103 * virtio_set_features_nocheck call done from the virtio_load.
3104 * Here we make sure it is preserved and restored accordingly
3105 * in the virtio_net_post_load_virtio callback.
3106 */
3107 n->saved_guest_offloads = n->curr_guest_offloads;
6c666823 3108
441537f1 3109 virtio_net_set_queue_pairs(n);
5f800801 3110
2d9aba39
AW
3111 /* Find the first multicast entry in the saved MAC filter */
3112 for (i = 0; i < n->mac_table.in_use; i++) {
3113 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
3114 break;
3115 }
3116 }
3117 n->mac_table.first_multi = i;
98991481
AK
3118
3119 /* nc.link_down can't be migrated, so infer link_down according
3120 * to link status bit in n->status */
5f800801 3121 link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
441537f1 3122 for (i = 0; i < n->max_queue_pairs; i++) {
5f800801
JW
3123 qemu_get_subqueue(n->nic, i)->link_down = link_down;
3124 }
98991481 3125
6c666823
MT
3126 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
3127 virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
9d8c6a25
DDAG
3128 qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
3129 QEMU_CLOCK_VIRTUAL,
3130 virtio_net_announce_timer, n);
3131 if (n->announce_timer.round) {
3132 timer_mod(n->announce_timer.tm,
3133 qemu_clock_get_ms(n->announce_timer.type));
3134 } else {
944458b6 3135 qemu_announce_timer_del(&n->announce_timer, false);
9d8c6a25 3136 }
6c666823
MT
3137 }
3138
0e07198e 3139 virtio_net_commit_rss_config(n);
fbe78f4f
AL
3140 return 0;
3141}
3142
7788c3f2
MS
3143static int virtio_net_post_load_virtio(VirtIODevice *vdev)
3144{
3145 VirtIONet *n = VIRTIO_NET(vdev);
3146 /*
3147 * The actual needed state is now in saved_guest_offloads,
3148 * see virtio_net_post_load_device for detail.
3149 * Restore it back and apply the desired offloads.
3150 */
3151 n->curr_guest_offloads = n->saved_guest_offloads;
3152 if (peer_has_vnet_hdr(n)) {
3153 virtio_net_apply_guest_offloads(n);
3154 }
3155
3156 return 0;
3157}
3158
982b78c5
DDAG
3159/* tx_waiting field of a VirtIONetQueue */
3160static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
3161 .name = "virtio-net-queue-tx_waiting",
1de81b42 3162 .fields = (const VMStateField[]) {
982b78c5
DDAG
3163 VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
3164 VMSTATE_END_OF_LIST()
3165 },
3166};
3167
441537f1 3168static bool max_queue_pairs_gt_1(void *opaque, int version_id)
982b78c5 3169{
441537f1 3170 return VIRTIO_NET(opaque)->max_queue_pairs > 1;
982b78c5
DDAG
3171}
3172
3173static bool has_ctrl_guest_offloads(void *opaque, int version_id)
3174{
3175 return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
3176 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
3177}
3178
3179static bool mac_table_fits(void *opaque, int version_id)
3180{
3181 return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
3182}
3183
3184static bool mac_table_doesnt_fit(void *opaque, int version_id)
3185{
3186 return !mac_table_fits(opaque, version_id);
3187}
3188
3189/* This temporary type is shared by all the WITH_TMP methods
3190 * although only some fields are used by each.
3191 */
3192struct VirtIONetMigTmp {
3193 VirtIONet *parent;
3194 VirtIONetQueue *vqs_1;
441537f1 3195 uint16_t curr_queue_pairs_1;
982b78c5
DDAG
3196 uint8_t has_ufo;
3197 uint32_t has_vnet_hdr;
3198};
3199
3200/* The 2nd and subsequent tx_waiting flags are loaded later than
441537f1 3201 * the 1st entry in the queue_pairs and only if there's more than one
982b78c5
DDAG
3202 * entry. We use the tmp mechanism to calculate a temporary
3203 * pointer and count and also validate the count.
3204 */
3205
44b1ff31 3206static int virtio_net_tx_waiting_pre_save(void *opaque)
982b78c5
DDAG
3207{
3208 struct VirtIONetMigTmp *tmp = opaque;
3209
3210 tmp->vqs_1 = tmp->parent->vqs + 1;
441537f1
JW
3211 tmp->curr_queue_pairs_1 = tmp->parent->curr_queue_pairs - 1;
3212 if (tmp->parent->curr_queue_pairs == 0) {
3213 tmp->curr_queue_pairs_1 = 0;
982b78c5 3214 }
44b1ff31
DDAG
3215
3216 return 0;
982b78c5
DDAG
3217}
3218
3219static int virtio_net_tx_waiting_pre_load(void *opaque)
3220{
3221 struct VirtIONetMigTmp *tmp = opaque;
3222
3223 /* Reuse the pointer setup from save */
3224 virtio_net_tx_waiting_pre_save(opaque);
3225
441537f1
JW
3226 if (tmp->parent->curr_queue_pairs > tmp->parent->max_queue_pairs) {
3227 error_report("virtio-net: curr_queue_pairs %x > max_queue_pairs %x",
3228 tmp->parent->curr_queue_pairs, tmp->parent->max_queue_pairs);
982b78c5
DDAG
3229
3230 return -EINVAL;
3231 }
3232
3233 return 0; /* all good */
3234}
3235
3236static const VMStateDescription vmstate_virtio_net_tx_waiting = {
3237 .name = "virtio-net-tx_waiting",
3238 .pre_load = virtio_net_tx_waiting_pre_load,
3239 .pre_save = virtio_net_tx_waiting_pre_save,
1de81b42 3240 .fields = (const VMStateField[]) {
982b78c5 3241 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
441537f1 3242 curr_queue_pairs_1,
982b78c5
DDAG
3243 vmstate_virtio_net_queue_tx_waiting,
3244 struct VirtIONetQueue),
3245 VMSTATE_END_OF_LIST()
3246 },
3247};
3248
3249/* the 'has_ufo' flag is just tested; if the incoming stream has the
3250 * flag set we need to check that we have it
3251 */
3252static int virtio_net_ufo_post_load(void *opaque, int version_id)
3253{
3254 struct VirtIONetMigTmp *tmp = opaque;
3255
3256 if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
3257 error_report("virtio-net: saved image requires TUN_F_UFO support");
3258 return -EINVAL;
3259 }
3260
3261 return 0;
3262}
3263
44b1ff31 3264static int virtio_net_ufo_pre_save(void *opaque)
982b78c5
DDAG
3265{
3266 struct VirtIONetMigTmp *tmp = opaque;
3267
3268 tmp->has_ufo = tmp->parent->has_ufo;
44b1ff31
DDAG
3269
3270 return 0;
982b78c5
DDAG
3271}
3272
3273static const VMStateDescription vmstate_virtio_net_has_ufo = {
3274 .name = "virtio-net-ufo",
3275 .post_load = virtio_net_ufo_post_load,
3276 .pre_save = virtio_net_ufo_pre_save,
1de81b42 3277 .fields = (const VMStateField[]) {
982b78c5
DDAG
3278 VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
3279 VMSTATE_END_OF_LIST()
3280 },
3281};
3282
3283/* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
3284 * flag set we need to check that we have it
3285 */
3286static int virtio_net_vnet_post_load(void *opaque, int version_id)
3287{
3288 struct VirtIONetMigTmp *tmp = opaque;
3289
3290 if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
3291 error_report("virtio-net: saved image requires vnet_hdr=on");
3292 return -EINVAL;
3293 }
3294
3295 return 0;
3296}
3297
44b1ff31 3298static int virtio_net_vnet_pre_save(void *opaque)
982b78c5
DDAG
3299{
3300 struct VirtIONetMigTmp *tmp = opaque;
3301
3302 tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
44b1ff31
DDAG
3303
3304 return 0;
982b78c5
DDAG
3305}
3306
3307static const VMStateDescription vmstate_virtio_net_has_vnet = {
3308 .name = "virtio-net-vnet",
3309 .post_load = virtio_net_vnet_post_load,
3310 .pre_save = virtio_net_vnet_pre_save,
1de81b42 3311 .fields = (const VMStateField[]) {
982b78c5
DDAG
3312 VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
3313 VMSTATE_END_OF_LIST()
3314 },
3315};
3316
e41b7114
YB
3317static bool virtio_net_rss_needed(void *opaque)
3318{
3319 return VIRTIO_NET(opaque)->rss_data.enabled;
3320}
3321
3322static const VMStateDescription vmstate_virtio_net_rss = {
3323 .name = "virtio-net-device/rss",
3324 .version_id = 1,
3325 .minimum_version_id = 1,
3326 .needed = virtio_net_rss_needed,
1de81b42 3327 .fields = (const VMStateField[]) {
e41b7114
YB
3328 VMSTATE_BOOL(rss_data.enabled, VirtIONet),
3329 VMSTATE_BOOL(rss_data.redirect, VirtIONet),
3330 VMSTATE_BOOL(rss_data.populate_hash, VirtIONet),
3331 VMSTATE_UINT32(rss_data.hash_types, VirtIONet),
3332 VMSTATE_UINT16(rss_data.indirections_len, VirtIONet),
3333 VMSTATE_UINT16(rss_data.default_queue, VirtIONet),
3334 VMSTATE_UINT8_ARRAY(rss_data.key, VirtIONet,
3335 VIRTIO_NET_RSS_MAX_KEY_SIZE),
3336 VMSTATE_VARRAY_UINT16_ALLOC(rss_data.indirections_table, VirtIONet,
3337 rss_data.indirections_len, 0,
3338 vmstate_info_uint16, uint16_t),
3339 VMSTATE_END_OF_LIST()
3340 },
3341};
3342
60f543ad
LV
3343static struct vhost_dev *virtio_net_get_vhost(VirtIODevice *vdev)
3344{
3345 VirtIONet *n = VIRTIO_NET(vdev);
3346 NetClientState *nc;
3347 struct vhost_net *net;
3348
3349 if (!n->nic) {
3350 return NULL;
3351 }
3352
3353 nc = qemu_get_queue(n->nic);
3354 if (!nc) {
3355 return NULL;
3356 }
3357
3358 net = get_vhost_net(nc->peer);
3359 if (!net) {
3360 return NULL;
3361 }
3362
3363 return &net->dev;
3364}
3365
3366static int vhost_user_net_save_state(QEMUFile *f, void *pv, size_t size,
3367 const VMStateField *field,
3368 JSONWriter *vmdesc)
3369{
3370 VirtIONet *n = pv;
3371 VirtIODevice *vdev = VIRTIO_DEVICE(n);
3372 struct vhost_dev *vhdev;
3373 Error *local_error = NULL;
3374 int ret;
3375
3376 vhdev = virtio_net_get_vhost(vdev);
3377 if (vhdev == NULL) {
3378 error_reportf_err(local_error,
3379 "Error getting vhost back-end of %s device %s: ",
3380 vdev->name, vdev->parent_obj.canonical_path);
3381 return -1;
3382 }
3383
3384 ret = vhost_save_backend_state(vhdev, f, &local_error);
3385 if (ret < 0) {
3386 error_reportf_err(local_error,
3387 "Error saving back-end state of %s device %s: ",
3388 vdev->name, vdev->parent_obj.canonical_path);
3389 return ret;
3390 }
3391
3392 return 0;
3393}
3394
3395static int vhost_user_net_load_state(QEMUFile *f, void *pv, size_t size,
3396 const VMStateField *field)
3397{
3398 VirtIONet *n = pv;
3399 VirtIODevice *vdev = VIRTIO_DEVICE(n);
3400 struct vhost_dev *vhdev;
3401 Error *local_error = NULL;
3402 int ret;
3403
3404 vhdev = virtio_net_get_vhost(vdev);
3405 if (vhdev == NULL) {
3406 error_reportf_err(local_error,
3407 "Error getting vhost back-end of %s device %s: ",
3408 vdev->name, vdev->parent_obj.canonical_path);
3409 return -1;
3410 }
3411
3412 ret = vhost_load_backend_state(vhdev, f, &local_error);
3413 if (ret < 0) {
3414 error_reportf_err(local_error,
3415 "Error loading back-end state of %s device %s: ",
3416 vdev->name, vdev->parent_obj.canonical_path);
3417 return ret;
3418 }
3419
3420 return 0;
3421}
3422
3423static bool vhost_user_net_is_internal_migration(void *opaque)
3424{
3425 VirtIONet *n = opaque;
3426 VirtIODevice *vdev = VIRTIO_DEVICE(n);
3427 struct vhost_dev *vhdev;
3428
3429 vhdev = virtio_net_get_vhost(vdev);
3430 if (vhdev == NULL) {
3431 return false;
3432 }
3433
3434 return vhost_supports_device_state(vhdev);
3435}
3436
3437static const VMStateDescription vhost_user_net_backend_state = {
3438 .name = "virtio-net-device/backend",
3439 .version_id = 0,
3440 .needed = vhost_user_net_is_internal_migration,
3441 .fields = (const VMStateField[]) {
3442 {
3443 .name = "backend",
3444 .info = &(const VMStateInfo) {
3445 .name = "virtio-net vhost-user backend state",
3446 .get = vhost_user_net_load_state,
3447 .put = vhost_user_net_save_state,
3448 },
3449 },
3450 VMSTATE_END_OF_LIST()
3451 }
3452};
3453
982b78c5
DDAG
3454static const VMStateDescription vmstate_virtio_net_device = {
3455 .name = "virtio-net-device",
3456 .version_id = VIRTIO_NET_VM_VERSION,
3457 .minimum_version_id = VIRTIO_NET_VM_VERSION,
3458 .post_load = virtio_net_post_load_device,
1de81b42 3459 .fields = (const VMStateField[]) {
982b78c5
DDAG
3460 VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
3461 VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
3462 vmstate_virtio_net_queue_tx_waiting,
3463 VirtIONetQueue),
3464 VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
3465 VMSTATE_UINT16(status, VirtIONet),
3466 VMSTATE_UINT8(promisc, VirtIONet),
3467 VMSTATE_UINT8(allmulti, VirtIONet),
3468 VMSTATE_UINT32(mac_table.in_use, VirtIONet),
3469
3470 /* Guarded pair: If it fits we load it, else we throw it away
3471 * - can happen if source has a larger MAC table.; post-load
3472 * sets flags in this case.
3473 */
3474 VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
3475 0, mac_table_fits, mac_table.in_use,
3476 ETH_ALEN),
3477 VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
3478 mac_table.in_use, ETH_ALEN),
3479
3480 /* Note: This is an array of uint32's that's always been saved as a
3481 * buffer; hold onto your endiannesses; it's actually used as a bitmap
3482 * but based on the uint.
3483 */
3484 VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
3485 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3486 vmstate_virtio_net_has_vnet),
3487 VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
3488 VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
3489 VMSTATE_UINT8(alluni, VirtIONet),
3490 VMSTATE_UINT8(nomulti, VirtIONet),
3491 VMSTATE_UINT8(nouni, VirtIONet),
3492 VMSTATE_UINT8(nobcast, VirtIONet),
3493 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3494 vmstate_virtio_net_has_ufo),
441537f1 3495 VMSTATE_SINGLE_TEST(max_queue_pairs, VirtIONet, max_queue_pairs_gt_1, 0,
982b78c5 3496 vmstate_info_uint16_equal, uint16_t),
441537f1 3497 VMSTATE_UINT16_TEST(curr_queue_pairs, VirtIONet, max_queue_pairs_gt_1),
982b78c5
DDAG
3498 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
3499 vmstate_virtio_net_tx_waiting),
3500 VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
3501 has_ctrl_guest_offloads),
3502 VMSTATE_END_OF_LIST()
1de81b42
RH
3503 },
3504 .subsections = (const VMStateDescription * const []) {
e41b7114 3505 &vmstate_virtio_net_rss,
60f543ad 3506 &vhost_user_net_backend_state,
e41b7114
YB
3507 NULL
3508 }
982b78c5
DDAG
3509};
3510
eb6b6c12 3511static NetClientInfo net_virtio_info = {
f394b2e2 3512 .type = NET_CLIENT_DRIVER_NIC,
eb6b6c12
MM
3513 .size = sizeof(NICState),
3514 .can_receive = virtio_net_can_receive,
3515 .receive = virtio_net_receive,
eb6b6c12 3516 .link_status_changed = virtio_net_set_link_status,
b1be4280 3517 .query_rx_filter = virtio_net_query_rxfilter,
b2c929f0 3518 .announce = virtio_net_announce,
eb6b6c12
MM
3519};
3520
f56a1247
MT
3521static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
3522{
17a0ca55 3523 VirtIONet *n = VIRTIO_NET(vdev);
68b0a639 3524 NetClientState *nc;
f56a1247 3525 assert(n->vhost_started);
1c188fc8 3526 if (!n->multiqueue && idx == 2) {
68b0a639
SWL
3527 /* Must guard against invalid features and bogus queue index
3528 * from being set by malicious guest, or penetrated through
3529 * buggy migration stream.
3530 */
3531 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
3532 qemu_log_mask(LOG_GUEST_ERROR,
3533 "%s: bogus vq index ignored\n", __func__);
3534 return false;
3535 }
3536 nc = qemu_get_subqueue(n->nic, n->max_queue_pairs);
3537 } else {
3538 nc = qemu_get_subqueue(n->nic, vq2q(idx));
3539 }
544f0278
CL
3540 /*
3541 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
7e8094f0 3542 * as the macro of configure interrupt's IDX, If this driver does not
544f0278
CL
3543 * support, the function will return false
3544 */
3545
3546 if (idx == VIRTIO_CONFIG_IRQ_IDX) {
8aab0d1d 3547 return vhost_net_config_pending(get_vhost_net(nc->peer));
544f0278 3548 }
ed8b4afe 3549 return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
f56a1247
MT
3550}
3551
3552static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
3553 bool mask)
3554{
17a0ca55 3555 VirtIONet *n = VIRTIO_NET(vdev);
68b0a639 3556 NetClientState *nc;
f56a1247 3557 assert(n->vhost_started);
1c188fc8 3558 if (!n->multiqueue && idx == 2) {
68b0a639
SWL
3559 /* Must guard against invalid features and bogus queue index
3560 * from being set by malicious guest, or penetrated through
3561 * buggy migration stream.
3562 */
3563 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
3564 qemu_log_mask(LOG_GUEST_ERROR,
3565 "%s: bogus vq index ignored\n", __func__);
3566 return;
3567 }
3568 nc = qemu_get_subqueue(n->nic, n->max_queue_pairs);
3569 } else {
3570 nc = qemu_get_subqueue(n->nic, vq2q(idx));
3571 }
544f0278
CL
3572 /*
3573 *Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
7e8094f0 3574 * as the macro of configure interrupt's IDX, If this driver does not
544f0278
CL
3575 * support, the function will return
3576 */
3577
3578 if (idx == VIRTIO_CONFIG_IRQ_IDX) {
8aab0d1d 3579 vhost_net_config_mask(get_vhost_net(nc->peer), vdev, mask);
544f0278
CL
3580 return;
3581 }
544f0278 3582 vhost_net_virtqueue_mask(get_vhost_net(nc->peer), vdev, idx, mask);
f56a1247
MT
3583}
3584
019a3edb 3585static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
fbe78f4f 3586{
0cd09c3a 3587 virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
a93e599d 3588
d74c30c8 3589 n->config_size = virtio_get_config_size(&cfg_size_params, host_features);
17ec5a86
FK
3590}
3591
8a253ec2
FK
3592void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
3593 const char *type)
3594{
3595 /*
3596 * The name can be NULL, the netclient name will be type.x.
3597 */
3598 assert(type != NULL);
3599
9e288406 3600 g_free(n->netclient_name);
9e288406 3601 g_free(n->netclient_type);
80e0090a 3602 n->netclient_name = g_strdup(name);
8a253ec2
FK
3603 n->netclient_type = g_strdup(type);
3604}
3605
0e9a65c5 3606static bool failover_unplug_primary(VirtIONet *n, DeviceState *dev)
9711cd0d
JF
3607{
3608 HotplugHandler *hotplug_ctrl;
3609 PCIDevice *pci_dev;
3610 Error *err = NULL;
3611
0e9a65c5 3612 hotplug_ctrl = qdev_get_hotplug_handler(dev);
9711cd0d 3613 if (hotplug_ctrl) {
0e9a65c5 3614 pci_dev = PCI_DEVICE(dev);
9711cd0d 3615 pci_dev->partially_hotplugged = true;
0e9a65c5 3616 hotplug_handler_unplug_request(hotplug_ctrl, dev, &err);
9711cd0d
JF
3617 if (err) {
3618 error_report_err(err);
3619 return false;
3620 }
3621 } else {
3622 return false;
3623 }
3624 return true;
3625}
3626
0e9a65c5
JQ
3627static bool failover_replug_primary(VirtIONet *n, DeviceState *dev,
3628 Error **errp)
9711cd0d 3629{
5a0948d3 3630 Error *err = NULL;
9711cd0d 3631 HotplugHandler *hotplug_ctrl;
0e9a65c5 3632 PCIDevice *pdev = PCI_DEVICE(dev);
78274682 3633 BusState *primary_bus;
9711cd0d
JF
3634
3635 if (!pdev->partially_hotplugged) {
3636 return true;
3637 }
0e9a65c5 3638 primary_bus = dev->parent_bus;
78274682 3639 if (!primary_bus) {
150ab54a 3640 error_setg(errp, "virtio_net: couldn't find primary bus");
5a0948d3 3641 return false;
9711cd0d 3642 }
0e9a65c5 3643 qdev_set_parent_bus(dev, primary_bus, &error_abort);
e2bde83e 3644 qatomic_set(&n->failover_primary_hidden, false);
0e9a65c5 3645 hotplug_ctrl = qdev_get_hotplug_handler(dev);
150ab54a 3646 if (hotplug_ctrl) {
0e9a65c5 3647 hotplug_handler_pre_plug(hotplug_ctrl, dev, &err);
5a0948d3
MA
3648 if (err) {
3649 goto out;
3650 }
0e9a65c5 3651 hotplug_handler_plug(hotplug_ctrl, dev, &err);
150ab54a 3652 }
109c20ea 3653 pdev->partially_hotplugged = false;
150ab54a
JF
3654
3655out:
5a0948d3
MA
3656 error_propagate(errp, err);
3657 return !err;
9711cd0d
JF
3658}
3659
9d9babf7 3660static void virtio_net_handle_migration_primary(VirtIONet *n, MigrationEvent *e)
9711cd0d
JF
3661{
3662 bool should_be_hidden;
3663 Error *err = NULL;
07a5d816 3664 DeviceState *dev = failover_find_primary_device(n);
9711cd0d 3665
07a5d816
JQ
3666 if (!dev) {
3667 return;
9711cd0d
JF
3668 }
3669
07a5d816
JQ
3670 should_be_hidden = qatomic_read(&n->failover_primary_hidden);
3671
9d9babf7 3672 if (e->type == MIG_EVENT_PRECOPY_SETUP && !should_be_hidden) {
07a5d816
JQ
3673 if (failover_unplug_primary(n, dev)) {
3674 vmstate_unregister(VMSTATE_IF(dev), qdev_get_vmsd(dev), dev);
3675 qapi_event_send_unplug_primary(dev->id);
e2bde83e 3676 qatomic_set(&n->failover_primary_hidden, true);
9711cd0d
JF
3677 } else {
3678 warn_report("couldn't unplug primary device");
3679 }
9d9babf7 3680 } else if (e->type == MIG_EVENT_PRECOPY_FAILED) {
150ab54a 3681 /* We already unplugged the device let's plug it back */
07a5d816 3682 if (!failover_replug_primary(n, dev, &err)) {
9711cd0d
JF
3683 if (err) {
3684 error_report_err(err);
3685 }
3686 }
3687 }
3688}
3689
3e775730 3690static int virtio_net_migration_state_notifier(NotifierWithReturn *notifier,
5663dd3f 3691 MigrationEvent *e, Error **errp)
9711cd0d 3692{
9711cd0d 3693 VirtIONet *n = container_of(notifier, VirtIONet, migration_state);
9d9babf7 3694 virtio_net_handle_migration_primary(n, e);
3e775730 3695 return 0;
9711cd0d
JF
3696}
3697
b91ad981 3698static bool failover_hide_primary_device(DeviceListener *listener,
f3558b1b
KW
3699 const QDict *device_opts,
3700 bool from_json,
3701 Error **errp)
9711cd0d
JF
3702{
3703 VirtIONet *n = container_of(listener, VirtIONet, primary_listener);
4f0303ae 3704 const char *standby_id;
9711cd0d 3705
4d0e59ac 3706 if (!device_opts) {
89631fed 3707 return false;
4d0e59ac 3708 }
bcfc906b
LV
3709
3710 if (!qdict_haskey(device_opts, "failover_pair_id")) {
3711 return false;
3712 }
3713
3714 if (!qdict_haskey(device_opts, "id")) {
3715 error_setg(errp, "Device with failover_pair_id needs to have id");
3716 return false;
3717 }
3718
3719 standby_id = qdict_get_str(device_opts, "failover_pair_id");
89631fed
JQ
3720 if (g_strcmp0(standby_id, n->netclient_name) != 0) {
3721 return false;
9711cd0d
JF
3722 }
3723
7fe7791e
LV
3724 /*
3725 * The hide helper can be called several times for a given device.
3726 * Check there is only one primary for a virtio-net device but
3727 * don't duplicate the qdict several times if it's called for the same
3728 * device.
3729 */
259a10db 3730 if (n->primary_opts) {
7fe7791e
LV
3731 const char *old, *new;
3732 /* devices with failover_pair_id always have an id */
3733 old = qdict_get_str(n->primary_opts, "id");
3734 new = qdict_get_str(device_opts, "id");
3735 if (strcmp(old, new) != 0) {
3736 error_setg(errp, "Cannot attach more than one primary device to "
3737 "'%s': '%s' and '%s'", n->netclient_name, old, new);
3738 return false;
3739 }
3740 } else {
3741 n->primary_opts = qdict_clone_shallow(device_opts);
3742 n->primary_opts_from_json = from_json;
259a10db
KW
3743 }
3744
e2bde83e 3745 /* failover_primary_hidden is set during feature negotiation */
3abad4a2 3746 return qatomic_read(&n->failover_primary_hidden);
9711cd0d
JF
3747}
3748
e6f746b3 3749static void virtio_net_device_realize(DeviceState *dev, Error **errp)
17ec5a86 3750{
e6f746b3 3751 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
284a32f0 3752 VirtIONet *n = VIRTIO_NET(dev);
b1be4280 3753 NetClientState *nc;
284a32f0 3754 int i;
1773d9ee 3755
a93e599d 3756 if (n->net_conf.mtu) {
127833ee 3757 n->host_features |= (1ULL << VIRTIO_NET_F_MTU);
a93e599d
MC
3758 }
3759
9473939e
JB
3760 if (n->net_conf.duplex_str) {
3761 if (strncmp(n->net_conf.duplex_str, "half", 5) == 0) {
3762 n->net_conf.duplex = DUPLEX_HALF;
3763 } else if (strncmp(n->net_conf.duplex_str, "full", 5) == 0) {
3764 n->net_conf.duplex = DUPLEX_FULL;
3765 } else {
3766 error_setg(errp, "'duplex' must be 'half' or 'full'");
843c4cfc 3767 return;
9473939e
JB
3768 }
3769 n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
3770 } else {
3771 n->net_conf.duplex = DUPLEX_UNKNOWN;
3772 }
3773
3774 if (n->net_conf.speed < SPEED_UNKNOWN) {
3775 error_setg(errp, "'speed' must be between 0 and INT_MAX");
843c4cfc
MA
3776 return;
3777 }
3778 if (n->net_conf.speed >= 0) {
9473939e
JB
3779 n->host_features |= (1ULL << VIRTIO_NET_F_SPEED_DUPLEX);
3780 }
3781
9711cd0d 3782 if (n->failover) {
b91ad981 3783 n->primary_listener.hide_device = failover_hide_primary_device;
e2bde83e 3784 qatomic_set(&n->failover_primary_hidden, true);
9711cd0d 3785 device_listener_register(&n->primary_listener);
d9cda213
SS
3786 migration_add_notifier(&n->migration_state,
3787 virtio_net_migration_state_notifier);
9711cd0d
JF
3788 n->host_features |= (1ULL << VIRTIO_NET_F_STANDBY);
3789 }
3790
da3e8a23 3791 virtio_net_set_config_size(n, n->host_features);
3857cd5c 3792 virtio_init(vdev, VIRTIO_ID_NET, n->config_size);
fbe78f4f 3793
1c0fbfa3
MT
3794 /*
3795 * We set a lower limit on RX queue size to what it always was.
3796 * Guests that want a smaller ring can always resize it without
3797 * help from us (using virtio 1 and up).
3798 */
3799 if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
3800 n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
5f997fd1 3801 !is_power_of_2(n->net_conf.rx_queue_size)) {
1c0fbfa3
MT
3802 error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
3803 "must be a power of 2 between %d and %d.",
3804 n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
3805 VIRTQUEUE_MAX_SIZE);
3806 virtio_cleanup(vdev);
3807 return;
3808 }
3809
9b02e161 3810 if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
4271f403 3811 n->net_conf.tx_queue_size > virtio_net_max_tx_queue_size(n) ||
9b02e161
WW
3812 !is_power_of_2(n->net_conf.tx_queue_size)) {
3813 error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
3814 "must be a power of 2 between %d and %d",
3815 n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
4271f403 3816 virtio_net_max_tx_queue_size(n));
9b02e161
WW
3817 virtio_cleanup(vdev);
3818 return;
3819 }
3820
22288fe5
JW
3821 n->max_ncs = MAX(n->nic_conf.peers.queues, 1);
3822
3823 /*
3824 * Figure out the datapath queue pairs since the backend could
3825 * provide control queue via peers as well.
3826 */
3827 if (n->nic_conf.peers.queues) {
3828 for (i = 0; i < n->max_ncs; i++) {
3829 if (n->nic_conf.peers.ncs[i]->is_datapath) {
3830 ++n->max_queue_pairs;
3831 }
3832 }
3833 }
3834 n->max_queue_pairs = MAX(n->max_queue_pairs, 1);
3835
441537f1 3836 if (n->max_queue_pairs * 2 + 1 > VIRTIO_QUEUE_MAX) {
22288fe5 3837 error_setg(errp, "Invalid number of queue pairs (= %" PRIu32 "), "
631b22ea 3838 "must be a positive integer less than %d.",
441537f1 3839 n->max_queue_pairs, (VIRTIO_QUEUE_MAX - 1) / 2);
7e0e736e
JW
3840 virtio_cleanup(vdev);
3841 return;
3842 }
b21e2380 3843 n->vqs = g_new0(VirtIONetQueue, n->max_queue_pairs);
441537f1 3844 n->curr_queue_pairs = 1;
1773d9ee 3845 n->tx_timeout = n->net_conf.txtimer;
a697a334 3846
1773d9ee
FK
3847 if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
3848 && strcmp(n->net_conf.tx, "bh")) {
0765691e
MA
3849 warn_report("virtio-net: "
3850 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
3851 n->net_conf.tx);
3852 error_printf("Defaulting to \"bh\"");
a697a334
AW
3853 }
3854
2eef278b
MT
3855 n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
3856 n->net_conf.tx_queue_size);
9b02e161 3857
8c497568 3858 virtio_net_add_queue(n, 0);
da51a335 3859
17a0ca55 3860 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1773d9ee
FK
3861 qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
3862 memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
554c97dd 3863 n->status = VIRTIO_NET_S_LINK_UP;
9d8c6a25
DDAG
3864 qemu_announce_timer_reset(&n->announce_timer, migrate_announce_params(),
3865 QEMU_CLOCK_VIRTUAL,
3866 virtio_net_announce_timer, n);
b2c929f0 3867 n->announce_timer.round = 0;
fbe78f4f 3868
8a253ec2
FK
3869 if (n->netclient_type) {
3870 /*
3871 * Happen when virtio_net_set_netclient_name has been called.
3872 */
3873 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
7d0fefdf
AO
3874 n->netclient_type, n->netclient_name,
3875 &dev->mem_reentrancy_guard, n);
8a253ec2
FK
3876 } else {
3877 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
7d0fefdf
AO
3878 object_get_typename(OBJECT(dev)), dev->id,
3879 &dev->mem_reentrancy_guard, n);
8a253ec2
FK
3880 }
3881
441537f1 3882 for (i = 0; i < n->max_queue_pairs; i++) {
d4c62930
BM
3883 n->nic->ncs[i].do_not_pad = true;
3884 }
3885
6e371ab8
MT
3886 peer_test_vnet_hdr(n);
3887 if (peer_has_vnet_hdr(n)) {
6e371ab8
MT
3888 n->host_hdr_len = sizeof(struct virtio_net_hdr);
3889 } else {
3890 n->host_hdr_len = 0;
3891 }
eb6b6c12 3892
1773d9ee 3893 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
96d5e201 3894
fed699f9 3895 n->vqs[0].tx_waiting = 0;
1773d9ee 3896 n->tx_burst = n->net_conf.txburst;
e22f0603 3897 virtio_net_set_mrg_rx_bufs(n, 0, 0, 0);
002437cd 3898 n->promisc = 1; /* for compatibility */
fbe78f4f 3899
7267c094 3900 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
b6503ed9 3901
7267c094 3902 n->vlans = g_malloc0(MAX_VLAN >> 3);
f21c0ed9 3903
b1be4280
AK
3904 nc = qemu_get_queue(n->nic);
3905 nc->rxfilter_notify_enabled = 1;
3906
e87936ea
CL
3907 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_VDPA) {
3908 struct virtio_net_config netcfg = {};
3909 memcpy(&netcfg.mac, &n->nic_conf.macaddr, ETH_ALEN);
3910 vhost_net_set_config(get_vhost_net(nc->peer),
f8ed3648 3911 (uint8_t *)&netcfg, 0, ETH_ALEN, VHOST_SET_CONFIG_TYPE_FRONTEND);
e87936ea 3912 }
2974e916 3913 QTAILQ_INIT(&n->rsc_chains);
284a32f0 3914 n->qdev = dev;
4474e37a 3915
aac8f89d 3916 net_rx_pkt_init(&n->rx_pkt);
0145c393
AM
3917
3918 if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
bc82af6b 3919 virtio_net_load_ebpf(n, errp);
0145c393 3920 }
17ec5a86
FK
3921}
3922
b69c3c21 3923static void virtio_net_device_unrealize(DeviceState *dev)
17ec5a86 3924{
306ec6c3
AF
3925 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3926 VirtIONet *n = VIRTIO_NET(dev);
441537f1 3927 int i, max_queue_pairs;
17ec5a86 3928
0145c393
AM
3929 if (virtio_has_feature(n->host_features, VIRTIO_NET_F_RSS)) {
3930 virtio_net_unload_ebpf(n);
3931 }
3932
17ec5a86
FK
3933 /* This will stop vhost backend if appropriate. */
3934 virtio_net_set_status(vdev, 0);
3935
9e288406
MA
3936 g_free(n->netclient_name);
3937 n->netclient_name = NULL;
3938 g_free(n->netclient_type);
3939 n->netclient_type = NULL;
8a253ec2 3940
17ec5a86
FK
3941 g_free(n->mac_table.macs);
3942 g_free(n->vlans);
3943
9711cd0d 3944 if (n->failover) {
f3558b1b 3945 qobject_unref(n->primary_opts);
65018100 3946 device_listener_unregister(&n->primary_listener);
d9cda213 3947 migration_remove_notifier(&n->migration_state);
f3558b1b
KW
3948 } else {
3949 assert(n->primary_opts == NULL);
9711cd0d
JF
3950 }
3951
441537f1
JW
3952 max_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
3953 for (i = 0; i < max_queue_pairs; i++) {
f9d6dbf0 3954 virtio_net_del_queue(n, i);
17ec5a86 3955 }
d945d9f1 3956 /* delete also control vq */
441537f1 3957 virtio_del_queue(vdev, max_queue_pairs * 2);
944458b6 3958 qemu_announce_timer_del(&n->announce_timer, false);
17ec5a86
FK
3959 g_free(n->vqs);
3960 qemu_del_nic(n->nic);
2974e916 3961 virtio_net_rsc_cleanup(n);
59079029 3962 g_free(n->rss_data.indirections_table);
4474e37a 3963 net_rx_pkt_uninit(n->rx_pkt);
6a1a8cc7 3964 virtio_cleanup(vdev);
17ec5a86
FK
3965}
3966
cef776c0
AO
3967static void virtio_net_reset(VirtIODevice *vdev)
3968{
3969 VirtIONet *n = VIRTIO_NET(vdev);
3970 int i;
3971
3972 /* Reset back to compatibility mode */
3973 n->promisc = 1;
3974 n->allmulti = 0;
3975 n->alluni = 0;
3976 n->nomulti = 0;
3977 n->nouni = 0;
3978 n->nobcast = 0;
3979 /* multiqueue is disabled by default */
3980 n->curr_queue_pairs = 1;
3981 timer_del(n->announce_timer.tm);
3982 n->announce_timer.round = 0;
3983 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
3984
3985 /* Flush any MAC and VLAN filter table state */
3986 n->mac_table.in_use = 0;
3987 n->mac_table.first_multi = 0;
3988 n->mac_table.multi_overflow = 0;
3989 n->mac_table.uni_overflow = 0;
3990 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
3991 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
3992 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
3993 memset(n->vlans, 0, MAX_VLAN >> 3);
3994
3995 /* Flush any async TX */
3996 for (i = 0; i < n->max_queue_pairs; i++) {
3997 flush_or_purge_queued_packets(qemu_get_subqueue(n->nic, i));
3998 }
3999
4000 virtio_net_disable_rss(n);
4001}
4002
17ec5a86
FK
4003static void virtio_net_instance_init(Object *obj)
4004{
4005 VirtIONet *n = VIRTIO_NET(obj);
4006
4007 /*
4008 * The default config_size is sizeof(struct virtio_net_config).
2431f4f1 4009 * Can be overridden with virtio_net_set_config_size.
17ec5a86
FK
4010 */
4011 n->config_size = sizeof(struct virtio_net_config);
aa4197c3
GA
4012 device_add_bootindex_property(obj, &n->nic_conf.bootindex,
4013 "bootindex", "/ethernet-phy@0",
40c2281c 4014 DEVICE(n));
0145c393
AM
4015
4016 ebpf_rss_init(&n->ebpf_rss);
17ec5a86
FK
4017}
4018
44b1ff31 4019static int virtio_net_pre_save(void *opaque)
4d45dcfb
HP
4020{
4021 VirtIONet *n = opaque;
4022
4023 /* At this point, backend must be stopped, otherwise
4024 * it might keep writing to memory. */
4025 assert(!n->vhost_started);
44b1ff31
DDAG
4026
4027 return 0;
4d45dcfb
HP
4028}
4029
9711cd0d
JF
4030static bool primary_unplug_pending(void *opaque)
4031{
4032 DeviceState *dev = opaque;
21e8709b 4033 DeviceState *primary;
9711cd0d
JF
4034 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
4035 VirtIONet *n = VIRTIO_NET(vdev);
4036
284f42a5
JF
4037 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
4038 return false;
4039 }
21e8709b
JQ
4040 primary = failover_find_primary_device(n);
4041 return primary ? primary->pending_deleted_event : false;
9711cd0d
JF
4042}
4043
4044static bool dev_unplug_pending(void *opaque)
4045{
4046 DeviceState *dev = opaque;
4047 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
4048
4049 return vdc->primary_unplug_pending(dev);
4050}
4051
4d45dcfb
HP
4052static const VMStateDescription vmstate_virtio_net = {
4053 .name = "virtio-net",
4054 .minimum_version_id = VIRTIO_NET_VM_VERSION,
4055 .version_id = VIRTIO_NET_VM_VERSION,
1de81b42 4056 .fields = (const VMStateField[]) {
4d45dcfb
HP
4057 VMSTATE_VIRTIO_DEVICE,
4058 VMSTATE_END_OF_LIST()
4059 },
4060 .pre_save = virtio_net_pre_save,
9711cd0d 4061 .dev_unplug_pending = dev_unplug_pending,
4d45dcfb 4062};
290c2428 4063
e732f00f 4064static const Property virtio_net_properties[] = {
127833ee
JB
4065 DEFINE_PROP_BIT64("csum", VirtIONet, host_features,
4066 VIRTIO_NET_F_CSUM, true),
4067 DEFINE_PROP_BIT64("guest_csum", VirtIONet, host_features,
87108bb2 4068 VIRTIO_NET_F_GUEST_CSUM, true),
127833ee
JB
4069 DEFINE_PROP_BIT64("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
4070 DEFINE_PROP_BIT64("guest_tso4", VirtIONet, host_features,
87108bb2 4071 VIRTIO_NET_F_GUEST_TSO4, true),
127833ee 4072 DEFINE_PROP_BIT64("guest_tso6", VirtIONet, host_features,
87108bb2 4073 VIRTIO_NET_F_GUEST_TSO6, true),
127833ee 4074 DEFINE_PROP_BIT64("guest_ecn", VirtIONet, host_features,
87108bb2 4075 VIRTIO_NET_F_GUEST_ECN, true),
127833ee 4076 DEFINE_PROP_BIT64("guest_ufo", VirtIONet, host_features,
87108bb2 4077 VIRTIO_NET_F_GUEST_UFO, true),
127833ee 4078 DEFINE_PROP_BIT64("guest_announce", VirtIONet, host_features,
87108bb2 4079 VIRTIO_NET_F_GUEST_ANNOUNCE, true),
127833ee 4080 DEFINE_PROP_BIT64("host_tso4", VirtIONet, host_features,
87108bb2 4081 VIRTIO_NET_F_HOST_TSO4, true),
127833ee 4082 DEFINE_PROP_BIT64("host_tso6", VirtIONet, host_features,
87108bb2 4083 VIRTIO_NET_F_HOST_TSO6, true),
127833ee 4084 DEFINE_PROP_BIT64("host_ecn", VirtIONet, host_features,
87108bb2 4085 VIRTIO_NET_F_HOST_ECN, true),
127833ee 4086 DEFINE_PROP_BIT64("host_ufo", VirtIONet, host_features,
87108bb2 4087 VIRTIO_NET_F_HOST_UFO, true),
127833ee 4088 DEFINE_PROP_BIT64("mrg_rxbuf", VirtIONet, host_features,
87108bb2 4089 VIRTIO_NET_F_MRG_RXBUF, true),
127833ee 4090 DEFINE_PROP_BIT64("status", VirtIONet, host_features,
87108bb2 4091 VIRTIO_NET_F_STATUS, true),
127833ee 4092 DEFINE_PROP_BIT64("ctrl_vq", VirtIONet, host_features,
87108bb2 4093 VIRTIO_NET_F_CTRL_VQ, true),
127833ee 4094 DEFINE_PROP_BIT64("ctrl_rx", VirtIONet, host_features,
87108bb2 4095 VIRTIO_NET_F_CTRL_RX, true),
127833ee 4096 DEFINE_PROP_BIT64("ctrl_vlan", VirtIONet, host_features,
87108bb2 4097 VIRTIO_NET_F_CTRL_VLAN, true),
127833ee 4098 DEFINE_PROP_BIT64("ctrl_rx_extra", VirtIONet, host_features,
87108bb2 4099 VIRTIO_NET_F_CTRL_RX_EXTRA, true),
127833ee 4100 DEFINE_PROP_BIT64("ctrl_mac_addr", VirtIONet, host_features,
87108bb2 4101 VIRTIO_NET_F_CTRL_MAC_ADDR, true),
127833ee 4102 DEFINE_PROP_BIT64("ctrl_guest_offloads", VirtIONet, host_features,
87108bb2 4103 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
127833ee 4104 DEFINE_PROP_BIT64("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
59079029
YB
4105 DEFINE_PROP_BIT64("rss", VirtIONet, host_features,
4106 VIRTIO_NET_F_RSS, false),
e22f0603
YB
4107 DEFINE_PROP_BIT64("hash", VirtIONet, host_features,
4108 VIRTIO_NET_F_HASH_REPORT, false),
6b230b7d
AM
4109 DEFINE_PROP_ARRAY("ebpf-rss-fds", VirtIONet, nr_ebpf_rss_fds,
4110 ebpf_rss_fds, qdev_prop_string, char*),
2974e916
YB
4111 DEFINE_PROP_BIT64("guest_rsc_ext", VirtIONet, host_features,
4112 VIRTIO_NET_F_RSC_EXT, false),
4113 DEFINE_PROP_UINT32("rsc_interval", VirtIONet, rsc_timeout,
4114 VIRTIO_NET_RSC_DEFAULT_INTERVAL),
17ec5a86
FK
4115 DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
4116 DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
87108bb2 4117 TX_TIMER_INTERVAL),
17ec5a86
FK
4118 DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
4119 DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
1c0fbfa3
MT
4120 DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
4121 VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
9b02e161
WW
4122 DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
4123 VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
a93e599d 4124 DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
75ebec11
MC
4125 DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
4126 true),
9473939e
JB
4127 DEFINE_PROP_INT32("speed", VirtIONet, net_conf.speed, SPEED_UNKNOWN),
4128 DEFINE_PROP_STRING("duplex", VirtIONet, net_conf.duplex_str),
9711cd0d 4129 DEFINE_PROP_BOOL("failover", VirtIONet, failover, false),
53da8b5a
YB
4130 DEFINE_PROP_BIT64("guest_uso4", VirtIONet, host_features,
4131 VIRTIO_NET_F_GUEST_USO4, true),
4132 DEFINE_PROP_BIT64("guest_uso6", VirtIONet, host_features,
4133 VIRTIO_NET_F_GUEST_USO6, true),
4134 DEFINE_PROP_BIT64("host_uso", VirtIONet, host_features,
4135 VIRTIO_NET_F_HOST_USO, true),
17ec5a86
FK
4136};
4137
12d1a768 4138static void virtio_net_class_init(ObjectClass *klass, const void *data)
17ec5a86
FK
4139{
4140 DeviceClass *dc = DEVICE_CLASS(klass);
4141 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
e6f746b3 4142
4f67d30b 4143 device_class_set_props(dc, virtio_net_properties);
290c2428 4144 dc->vmsd = &vmstate_virtio_net;
125ee0ed 4145 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
e6f746b3 4146 vdc->realize = virtio_net_device_realize;
306ec6c3 4147 vdc->unrealize = virtio_net_device_unrealize;
17ec5a86
FK
4148 vdc->get_config = virtio_net_get_config;
4149 vdc->set_config = virtio_net_set_config;
4150 vdc->get_features = virtio_net_get_features;
4151 vdc->set_features = virtio_net_set_features;
4152 vdc->bad_features = virtio_net_bad_features;
4153 vdc->reset = virtio_net_reset;
7dc6be52 4154 vdc->queue_reset = virtio_net_queue_reset;
7f863302 4155 vdc->queue_enable = virtio_net_queue_enable;
17ec5a86
FK
4156 vdc->set_status = virtio_net_set_status;
4157 vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
4158 vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
2a083ffd 4159 vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
9379ea9d 4160 vdc->pre_load_queues = virtio_net_pre_load_queues;
7788c3f2 4161 vdc->post_load = virtio_net_post_load_virtio;
982b78c5 4162 vdc->vmsd = &vmstate_virtio_net_device;
9711cd0d 4163 vdc->primary_unplug_pending = primary_unplug_pending;
c255488d 4164 vdc->get_vhost = virtio_net_get_vhost;
cd9b8346 4165 vdc->toggle_device_iotlb = vhost_toggle_device_iotlb;
17ec5a86
FK
4166}
4167
4168static const TypeInfo virtio_net_info = {
4169 .name = TYPE_VIRTIO_NET,
4170 .parent = TYPE_VIRTIO_DEVICE,
4171 .instance_size = sizeof(VirtIONet),
4172 .instance_init = virtio_net_instance_init,
4173 .class_init = virtio_net_class_init,
4174};
4175
4176static void virtio_register_types(void)
4177{
4178 type_register_static(&virtio_net_info);
4179}
4180
4181type_init(virtio_register_types)