]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/net/virtio-net.c
block: Simplify drive-mirror
[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"
1de7afc9 15#include "qemu/iov.h"
0d09e41a 16#include "hw/virtio/virtio.h"
1422e32d 17#include "net/net.h"
7200ac3c 18#include "net/checksum.h"
a8ed73f7 19#include "net/tap.h"
1de7afc9
PB
20#include "qemu/error-report.h"
21#include "qemu/timer.h"
0d09e41a
PB
22#include "hw/virtio/virtio-net.h"
23#include "net/vhost_net.h"
17ec5a86 24#include "hw/virtio/virtio-bus.h"
b1be4280 25#include "qapi/qmp/qjson.h"
06150279 26#include "qapi-event.h"
1399c60d 27#include "hw/virtio/virtio-access.h"
fbe78f4f 28
0ce0e8f4 29#define VIRTIO_NET_VM_VERSION 11
b6503ed9 30
4ffb17f5 31#define MAC_TABLE_ENTRIES 64
f21c0ed9 32#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
9d6271b8 33
14f9b664
JL
34/*
35 * Calculate the number of bytes up to and including the given 'field' of
36 * 'container'.
37 */
38#define endof(container, field) \
39 (offsetof(container, field) + sizeof(((container *)0)->field))
40
41typedef struct VirtIOFeature {
42 uint32_t flags;
43 size_t end;
44} VirtIOFeature;
45
46static VirtIOFeature feature_sizes[] = {
47 {.flags = 1 << VIRTIO_NET_F_MAC,
48 .end = endof(struct virtio_net_config, mac)},
49 {.flags = 1 << VIRTIO_NET_F_STATUS,
50 .end = endof(struct virtio_net_config, status)},
51 {.flags = 1 << VIRTIO_NET_F_MQ,
52 .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
53 {}
54};
55
fed699f9 56static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
0c87e93e
JW
57{
58 VirtIONet *n = qemu_get_nic_opaque(nc);
59
fed699f9 60 return &n->vqs[nc->queue_index];
0c87e93e 61}
fed699f9
JW
62
63static int vq2q(int queue_index)
64{
65 return queue_index / 2;
66}
67
fbe78f4f
AL
68/* TODO
69 * - we could suppress RX interrupt if we were so inclined.
70 */
71
0f03eca6 72static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
fbe78f4f 73{
17a0ca55 74 VirtIONet *n = VIRTIO_NET(vdev);
fbe78f4f
AL
75 struct virtio_net_config netcfg;
76
1399c60d
RR
77 virtio_stw_p(vdev, &netcfg.status, n->status);
78 virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
79674068 79 memcpy(netcfg.mac, n->mac, ETH_ALEN);
14f9b664 80 memcpy(config, &netcfg, n->config_size);
fbe78f4f
AL
81}
82
0f03eca6
AL
83static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
84{
17a0ca55 85 VirtIONet *n = VIRTIO_NET(vdev);
14f9b664 86 struct virtio_net_config netcfg = {};
0f03eca6 87
14f9b664 88 memcpy(&netcfg, config, n->config_size);
0f03eca6 89
95129d6f
CH
90 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
91 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
c1943a3f 92 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
79674068 93 memcpy(n->mac, netcfg.mac, ETH_ALEN);
b356f76d 94 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
0f03eca6
AL
95 }
96}
97
783e7706
MT
98static bool virtio_net_started(VirtIONet *n, uint8_t status)
99{
17a0ca55 100 VirtIODevice *vdev = VIRTIO_DEVICE(n);
783e7706 101 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
17a0ca55 102 (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
783e7706
MT
103}
104
f57fcf70
JW
105static void virtio_net_announce_timer(void *opaque)
106{
107 VirtIONet *n = opaque;
108 VirtIODevice *vdev = VIRTIO_DEVICE(n);
109
110 n->announce_counter--;
111 n->status |= VIRTIO_NET_S_ANNOUNCE;
112 virtio_notify_config(vdev);
113}
114
783e7706 115static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
afbaa7b4 116{
17a0ca55 117 VirtIODevice *vdev = VIRTIO_DEVICE(n);
b356f76d 118 NetClientState *nc = qemu_get_queue(n->nic);
fed699f9 119 int queues = n->multiqueue ? n->max_queues : 1;
b356f76d 120
ed8b4afe 121 if (!get_vhost_net(nc->peer)) {
afbaa7b4
MT
122 return;
123 }
fed699f9 124
8c1ac475
RK
125 if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
126 !!n->vhost_started) {
afbaa7b4
MT
127 return;
128 }
129 if (!n->vhost_started) {
086abc1c
MT
130 int r, i;
131
1bfa316c
GK
132 if (n->needs_vnet_hdr_swap) {
133 error_report("backend does not support %s vnet headers; "
134 "falling back on userspace virtio",
135 virtio_is_big_endian(vdev) ? "BE" : "LE");
136 return;
137 }
138
086abc1c
MT
139 /* Any packets outstanding? Purge them to avoid touching rings
140 * when vhost is running.
141 */
142 for (i = 0; i < queues; i++) {
143 NetClientState *qnc = qemu_get_subqueue(n->nic, i);
144
145 /* Purge both directions: TX and RX. */
146 qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
147 qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
148 }
149
1830b80f 150 n->vhost_started = 1;
17a0ca55 151 r = vhost_net_start(vdev, n->nic->ncs, queues);
afbaa7b4 152 if (r < 0) {
e7b43f7e
SH
153 error_report("unable to start vhost net: %d: "
154 "falling back on userspace virtio", -r);
1830b80f 155 n->vhost_started = 0;
afbaa7b4
MT
156 }
157 } else {
17a0ca55 158 vhost_net_stop(vdev, n->nic->ncs, queues);
afbaa7b4
MT
159 n->vhost_started = 0;
160 }
161}
162
1bfa316c
GK
163static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
164 NetClientState *peer,
165 bool enable)
166{
167 if (virtio_is_big_endian(vdev)) {
168 return qemu_set_vnet_be(peer, enable);
169 } else {
170 return qemu_set_vnet_le(peer, enable);
171 }
172}
173
174static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
175 int queues, bool enable)
176{
177 int i;
178
179 for (i = 0; i < queues; i++) {
180 if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
181 enable) {
182 while (--i >= 0) {
183 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
184 }
185
186 return true;
187 }
188 }
189
190 return false;
191}
192
193static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
194{
195 VirtIODevice *vdev = VIRTIO_DEVICE(n);
196 int queues = n->multiqueue ? n->max_queues : 1;
197
198 if (virtio_net_started(n, status)) {
199 /* Before using the device, we tell the network backend about the
200 * endianness to use when parsing vnet headers. If the backend
201 * can't do it, we fallback onto fixing the headers in the core
202 * virtio-net code.
203 */
204 n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
205 queues, true);
206 } else if (virtio_net_started(n, vdev->status)) {
207 /* After using the device, we need to reset the network backend to
208 * the default (guest native endianness), otherwise the guest may
209 * lose network connectivity if it is rebooted into a different
210 * endianness.
211 */
212 virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false);
213 }
214}
215
783e7706
MT
216static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
217{
17a0ca55 218 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9
JW
219 VirtIONetQueue *q;
220 int i;
221 uint8_t queue_status;
783e7706 222
1bfa316c 223 virtio_net_vnet_endian_status(n, status);
783e7706
MT
224 virtio_net_vhost_status(n, status);
225
fed699f9 226 for (i = 0; i < n->max_queues; i++) {
38705bb5
FZ
227 NetClientState *ncs = qemu_get_subqueue(n->nic, i);
228 bool queue_started;
fed699f9 229 q = &n->vqs[i];
783e7706 230
fed699f9
JW
231 if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
232 queue_status = 0;
783e7706 233 } else {
fed699f9 234 queue_status = status;
783e7706 235 }
38705bb5
FZ
236 queue_started =
237 virtio_net_started(n, queue_status) && !n->vhost_started;
238
239 if (queue_started) {
240 qemu_flush_queued_packets(ncs);
241 }
fed699f9
JW
242
243 if (!q->tx_waiting) {
244 continue;
245 }
246
38705bb5 247 if (queue_started) {
fed699f9 248 if (q->tx_timer) {
bc72ad67
AB
249 timer_mod(q->tx_timer,
250 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
fed699f9
JW
251 } else {
252 qemu_bh_schedule(q->tx_bh);
253 }
783e7706 254 } else {
fed699f9 255 if (q->tx_timer) {
bc72ad67 256 timer_del(q->tx_timer);
fed699f9
JW
257 } else {
258 qemu_bh_cancel(q->tx_bh);
259 }
783e7706
MT
260 }
261 }
262}
263
4e68f7a0 264static void virtio_net_set_link_status(NetClientState *nc)
554c97dd 265{
cc1f0f45 266 VirtIONet *n = qemu_get_nic_opaque(nc);
17a0ca55 267 VirtIODevice *vdev = VIRTIO_DEVICE(n);
554c97dd
AL
268 uint16_t old_status = n->status;
269
eb6b6c12 270 if (nc->link_down)
554c97dd
AL
271 n->status &= ~VIRTIO_NET_S_LINK_UP;
272 else
273 n->status |= VIRTIO_NET_S_LINK_UP;
274
275 if (n->status != old_status)
17a0ca55 276 virtio_notify_config(vdev);
afbaa7b4 277
17a0ca55 278 virtio_net_set_status(vdev, vdev->status);
554c97dd
AL
279}
280
b1be4280
AK
281static void rxfilter_notify(NetClientState *nc)
282{
b1be4280
AK
283 VirtIONet *n = qemu_get_nic_opaque(nc);
284
285 if (nc->rxfilter_notify_enabled) {
96e35046 286 gchar *path = object_get_canonical_path(OBJECT(n->qdev));
06150279
WX
287 qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
288 n->netclient_name, path, &error_abort);
96e35046 289 g_free(path);
b1be4280
AK
290
291 /* disable event notification to avoid events flooding */
292 nc->rxfilter_notify_enabled = 0;
293 }
294}
295
f7bc8ef8
AK
296static intList *get_vlan_table(VirtIONet *n)
297{
298 intList *list, *entry;
299 int i, j;
300
301 list = NULL;
302 for (i = 0; i < MAX_VLAN >> 5; i++) {
303 for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
304 if (n->vlans[i] & (1U << j)) {
305 entry = g_malloc0(sizeof(*entry));
306 entry->value = (i << 5) + j;
307 entry->next = list;
308 list = entry;
309 }
310 }
311 }
312
313 return list;
314}
315
b1be4280
AK
316static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
317{
318 VirtIONet *n = qemu_get_nic_opaque(nc);
f7bc8ef8 319 VirtIODevice *vdev = VIRTIO_DEVICE(n);
b1be4280
AK
320 RxFilterInfo *info;
321 strList *str_list, *entry;
f7bc8ef8 322 int i;
b1be4280
AK
323
324 info = g_malloc0(sizeof(*info));
325 info->name = g_strdup(nc->name);
326 info->promiscuous = n->promisc;
327
328 if (n->nouni) {
329 info->unicast = RX_STATE_NONE;
330 } else if (n->alluni) {
331 info->unicast = RX_STATE_ALL;
332 } else {
333 info->unicast = RX_STATE_NORMAL;
334 }
335
336 if (n->nomulti) {
337 info->multicast = RX_STATE_NONE;
338 } else if (n->allmulti) {
339 info->multicast = RX_STATE_ALL;
340 } else {
341 info->multicast = RX_STATE_NORMAL;
342 }
343
344 info->broadcast_allowed = n->nobcast;
345 info->multicast_overflow = n->mac_table.multi_overflow;
346 info->unicast_overflow = n->mac_table.uni_overflow;
347
b0575ba4 348 info->main_mac = qemu_mac_strdup_printf(n->mac);
b1be4280
AK
349
350 str_list = NULL;
351 for (i = 0; i < n->mac_table.first_multi; i++) {
352 entry = g_malloc0(sizeof(*entry));
b0575ba4 353 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
b1be4280
AK
354 entry->next = str_list;
355 str_list = entry;
356 }
357 info->unicast_table = str_list;
358
359 str_list = NULL;
360 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
361 entry = g_malloc0(sizeof(*entry));
b0575ba4 362 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
b1be4280
AK
363 entry->next = str_list;
364 str_list = entry;
365 }
366 info->multicast_table = str_list;
f7bc8ef8 367 info->vlan_table = get_vlan_table(n);
b1be4280 368
95129d6f 369 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
f7bc8ef8
AK
370 info->vlan = RX_STATE_ALL;
371 } else if (!info->vlan_table) {
372 info->vlan = RX_STATE_NONE;
373 } else {
374 info->vlan = RX_STATE_NORMAL;
b1be4280 375 }
b1be4280
AK
376
377 /* enable event notification after query */
378 nc->rxfilter_notify_enabled = 1;
379
380 return info;
381}
382
002437cd
AL
383static void virtio_net_reset(VirtIODevice *vdev)
384{
17a0ca55 385 VirtIONet *n = VIRTIO_NET(vdev);
002437cd
AL
386
387 /* Reset back to compatibility mode */
388 n->promisc = 1;
389 n->allmulti = 0;
015cb166
AW
390 n->alluni = 0;
391 n->nomulti = 0;
392 n->nouni = 0;
393 n->nobcast = 0;
fed699f9
JW
394 /* multiqueue is disabled by default */
395 n->curr_queues = 1;
f57fcf70
JW
396 timer_del(n->announce_timer);
397 n->announce_counter = 0;
398 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
b6503ed9 399
f21c0ed9 400 /* Flush any MAC and VLAN filter table state */
b6503ed9 401 n->mac_table.in_use = 0;
2d9aba39 402 n->mac_table.first_multi = 0;
8fd2a2f1
AW
403 n->mac_table.multi_overflow = 0;
404 n->mac_table.uni_overflow = 0;
b6503ed9 405 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
41dc8a67 406 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
702d66a8 407 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
f21c0ed9 408 memset(n->vlans, 0, MAX_VLAN >> 3);
002437cd
AL
409}
410
6e371ab8 411static void peer_test_vnet_hdr(VirtIONet *n)
3a330134 412{
b356f76d
JW
413 NetClientState *nc = qemu_get_queue(n->nic);
414 if (!nc->peer) {
6e371ab8 415 return;
b356f76d 416 }
3a330134 417
d6085e3a 418 n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
6e371ab8 419}
3a330134 420
6e371ab8
MT
421static int peer_has_vnet_hdr(VirtIONet *n)
422{
3a330134
MM
423 return n->has_vnet_hdr;
424}
425
0ce0e8f4
MM
426static int peer_has_ufo(VirtIONet *n)
427{
428 if (!peer_has_vnet_hdr(n))
429 return 0;
430
d6085e3a 431 n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
0ce0e8f4
MM
432
433 return n->has_ufo;
434}
435
bb9d17f8
CH
436static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
437 int version_1)
ff3a8066 438{
fed699f9
JW
439 int i;
440 NetClientState *nc;
441
ff3a8066
MT
442 n->mergeable_rx_bufs = mergeable_rx_bufs;
443
bb9d17f8
CH
444 if (version_1) {
445 n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
446 } else {
447 n->guest_hdr_len = n->mergeable_rx_bufs ?
448 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
449 sizeof(struct virtio_net_hdr);
450 }
ff3a8066 451
fed699f9
JW
452 for (i = 0; i < n->max_queues; i++) {
453 nc = qemu_get_subqueue(n->nic, i);
454
455 if (peer_has_vnet_hdr(n) &&
d6085e3a
SH
456 qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
457 qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
fed699f9
JW
458 n->host_hdr_len = n->guest_hdr_len;
459 }
ff3a8066
MT
460 }
461}
462
fed699f9
JW
463static int peer_attach(VirtIONet *n, int index)
464{
465 NetClientState *nc = qemu_get_subqueue(n->nic, index);
466
467 if (!nc->peer) {
468 return 0;
469 }
470
7263a0ad
CO
471 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) {
472 vhost_set_vring_enable(nc->peer, 1);
473 }
474
fed699f9
JW
475 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
476 return 0;
477 }
478
479 return tap_enable(nc->peer);
480}
481
482static int peer_detach(VirtIONet *n, int index)
483{
484 NetClientState *nc = qemu_get_subqueue(n->nic, index);
485
486 if (!nc->peer) {
487 return 0;
488 }
489
7263a0ad
CO
490 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) {
491 vhost_set_vring_enable(nc->peer, 0);
492 }
493
fed699f9
JW
494 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
495 return 0;
496 }
497
498 return tap_disable(nc->peer);
499}
500
501static void virtio_net_set_queues(VirtIONet *n)
502{
503 int i;
ddfa83ea 504 int r;
fed699f9
JW
505
506 for (i = 0; i < n->max_queues; i++) {
507 if (i < n->curr_queues) {
ddfa83ea
JS
508 r = peer_attach(n, i);
509 assert(!r);
fed699f9 510 } else {
ddfa83ea
JS
511 r = peer_detach(n, i);
512 assert(!r);
fed699f9
JW
513 }
514 }
515}
516
ec57db16 517static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
fed699f9 518
9d5b731d
JW
519static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
520 Error **errp)
fbe78f4f 521{
17a0ca55 522 VirtIONet *n = VIRTIO_NET(vdev);
b356f76d 523 NetClientState *nc = qemu_get_queue(n->nic);
fbe78f4f 524
da3e8a23
SZ
525 /* Firstly sync all virtio-net possible supported features */
526 features |= n->host_features;
527
0cd09c3a 528 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
c9f79a3f 529
6e371ab8 530 if (!peer_has_vnet_hdr(n)) {
0cd09c3a
CH
531 virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
532 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
533 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
534 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
8172539d 535
0cd09c3a
CH
536 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
537 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
538 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
539 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
8172539d 540 }
3a330134 541
8172539d 542 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
0cd09c3a
CH
543 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
544 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
3a330134
MM
545 }
546
ed8b4afe 547 if (!get_vhost_net(nc->peer)) {
9bc6304c
MT
548 return features;
549 }
ed8b4afe 550 return vhost_net_get_features(get_vhost_net(nc->peer), features);
fbe78f4f
AL
551}
552
019a3edb 553static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
8eca6b1b 554{
019a3edb 555 uint64_t features = 0;
8eca6b1b
AL
556
557 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
558 * but also these: */
0cd09c3a
CH
559 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
560 virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
561 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
562 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
563 virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
8eca6b1b 564
8172539d 565 return features;
8eca6b1b
AL
566}
567
644c9858
DF
568static void virtio_net_apply_guest_offloads(VirtIONet *n)
569{
ad37bb3b 570 qemu_set_offload(qemu_get_queue(n->nic)->peer,
644c9858
DF
571 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
572 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
573 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
574 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
575 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
576}
577
578static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
579{
580 static const uint64_t guest_offloads_mask =
581 (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
582 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
583 (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
584 (1ULL << VIRTIO_NET_F_GUEST_ECN) |
585 (1ULL << VIRTIO_NET_F_GUEST_UFO);
586
587 return guest_offloads_mask & features;
588}
589
590static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
591{
592 VirtIODevice *vdev = VIRTIO_DEVICE(n);
593 return virtio_net_guest_offloads_by_features(vdev->guest_features);
594}
595
d5aaa1b0 596static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
fbe78f4f 597{
17a0ca55 598 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9
JW
599 int i;
600
ef546f12 601 virtio_net_set_multiqueue(n,
95129d6f 602 virtio_has_feature(features, VIRTIO_NET_F_MQ));
fbe78f4f 603
ef546f12 604 virtio_net_set_mrg_rx_bufs(n,
95129d6f
CH
605 virtio_has_feature(features,
606 VIRTIO_NET_F_MRG_RXBUF),
607 virtio_has_feature(features,
608 VIRTIO_F_VERSION_1));
f5436dd9
MM
609
610 if (n->has_vnet_hdr) {
644c9858
DF
611 n->curr_guest_offloads =
612 virtio_net_guest_offloads_by_features(features);
613 virtio_net_apply_guest_offloads(n);
f5436dd9 614 }
fed699f9
JW
615
616 for (i = 0; i < n->max_queues; i++) {
617 NetClientState *nc = qemu_get_subqueue(n->nic, i);
618
ed8b4afe 619 if (!get_vhost_net(nc->peer)) {
fed699f9
JW
620 continue;
621 }
ed8b4afe 622 vhost_net_ack_features(get_vhost_net(nc->peer), features);
dc14a397 623 }
0b1eaa88 624
95129d6f 625 if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
0b1eaa88
SF
626 memset(n->vlans, 0, MAX_VLAN >> 3);
627 } else {
628 memset(n->vlans, 0xff, MAX_VLAN >> 3);
629 }
fbe78f4f
AL
630}
631
002437cd 632static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
921ac5d0 633 struct iovec *iov, unsigned int iov_cnt)
002437cd
AL
634{
635 uint8_t on;
921ac5d0 636 size_t s;
b1be4280 637 NetClientState *nc = qemu_get_queue(n->nic);
002437cd 638
921ac5d0
MT
639 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
640 if (s != sizeof(on)) {
641 return VIRTIO_NET_ERR;
002437cd
AL
642 }
643
dd23454b 644 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
002437cd 645 n->promisc = on;
dd23454b 646 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
002437cd 647 n->allmulti = on;
dd23454b 648 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
015cb166 649 n->alluni = on;
dd23454b 650 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
015cb166 651 n->nomulti = on;
dd23454b 652 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
015cb166 653 n->nouni = on;
dd23454b 654 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
015cb166 655 n->nobcast = on;
921ac5d0 656 } else {
002437cd 657 return VIRTIO_NET_ERR;
921ac5d0 658 }
002437cd 659
b1be4280
AK
660 rxfilter_notify(nc);
661
002437cd
AL
662 return VIRTIO_NET_OK;
663}
664
644c9858
DF
665static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
666 struct iovec *iov, unsigned int iov_cnt)
667{
668 VirtIODevice *vdev = VIRTIO_DEVICE(n);
669 uint64_t offloads;
670 size_t s;
671
95129d6f 672 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
644c9858
DF
673 return VIRTIO_NET_ERR;
674 }
675
676 s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
677 if (s != sizeof(offloads)) {
678 return VIRTIO_NET_ERR;
679 }
680
681 if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
682 uint64_t supported_offloads;
683
684 if (!n->has_vnet_hdr) {
685 return VIRTIO_NET_ERR;
686 }
687
688 supported_offloads = virtio_net_supported_guest_offloads(n);
689 if (offloads & ~supported_offloads) {
690 return VIRTIO_NET_ERR;
691 }
692
693 n->curr_guest_offloads = offloads;
694 virtio_net_apply_guest_offloads(n);
695
696 return VIRTIO_NET_OK;
697 } else {
698 return VIRTIO_NET_ERR;
699 }
700}
701
b6503ed9 702static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
921ac5d0 703 struct iovec *iov, unsigned int iov_cnt)
b6503ed9 704{
1399c60d 705 VirtIODevice *vdev = VIRTIO_DEVICE(n);
b6503ed9 706 struct virtio_net_ctrl_mac mac_data;
921ac5d0 707 size_t s;
b1be4280 708 NetClientState *nc = qemu_get_queue(n->nic);
b6503ed9 709
c1943a3f
AK
710 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
711 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
712 return VIRTIO_NET_ERR;
713 }
714 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
715 assert(s == sizeof(n->mac));
b356f76d 716 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
b1be4280
AK
717 rxfilter_notify(nc);
718
c1943a3f
AK
719 return VIRTIO_NET_OK;
720 }
721
921ac5d0 722 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
b6503ed9 723 return VIRTIO_NET_ERR;
921ac5d0 724 }
b6503ed9 725
cae2e556
AK
726 int in_use = 0;
727 int first_multi = 0;
728 uint8_t uni_overflow = 0;
729 uint8_t multi_overflow = 0;
730 uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
b6503ed9 731
921ac5d0
MT
732 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
733 sizeof(mac_data.entries));
1399c60d 734 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
921ac5d0 735 if (s != sizeof(mac_data.entries)) {
b1be4280 736 goto error;
921ac5d0
MT
737 }
738 iov_discard_front(&iov, &iov_cnt, s);
b6503ed9 739
921ac5d0 740 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
b1be4280 741 goto error;
921ac5d0 742 }
b6503ed9
AL
743
744 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
cae2e556 745 s = iov_to_buf(iov, iov_cnt, 0, macs,
921ac5d0
MT
746 mac_data.entries * ETH_ALEN);
747 if (s != mac_data.entries * ETH_ALEN) {
b1be4280 748 goto error;
921ac5d0 749 }
cae2e556 750 in_use += mac_data.entries;
b6503ed9 751 } else {
cae2e556 752 uni_overflow = 1;
b6503ed9
AL
753 }
754
921ac5d0
MT
755 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
756
cae2e556 757 first_multi = in_use;
2d9aba39 758
921ac5d0
MT
759 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
760 sizeof(mac_data.entries));
1399c60d 761 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
921ac5d0 762 if (s != sizeof(mac_data.entries)) {
b1be4280 763 goto error;
921ac5d0
MT
764 }
765
766 iov_discard_front(&iov, &iov_cnt, s);
b6503ed9 767
921ac5d0 768 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
b1be4280 769 goto error;
921ac5d0 770 }
b6503ed9 771
edc24385 772 if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
cae2e556 773 s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
921ac5d0
MT
774 mac_data.entries * ETH_ALEN);
775 if (s != mac_data.entries * ETH_ALEN) {
b1be4280 776 goto error;
8fd2a2f1 777 }
cae2e556 778 in_use += mac_data.entries;
921ac5d0 779 } else {
cae2e556 780 multi_overflow = 1;
b6503ed9
AL
781 }
782
cae2e556
AK
783 n->mac_table.in_use = in_use;
784 n->mac_table.first_multi = first_multi;
785 n->mac_table.uni_overflow = uni_overflow;
786 n->mac_table.multi_overflow = multi_overflow;
787 memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
788 g_free(macs);
b1be4280
AK
789 rxfilter_notify(nc);
790
b6503ed9 791 return VIRTIO_NET_OK;
b1be4280
AK
792
793error:
cae2e556 794 g_free(macs);
b1be4280 795 return VIRTIO_NET_ERR;
b6503ed9
AL
796}
797
f21c0ed9 798static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
921ac5d0 799 struct iovec *iov, unsigned int iov_cnt)
f21c0ed9 800{
1399c60d 801 VirtIODevice *vdev = VIRTIO_DEVICE(n);
f21c0ed9 802 uint16_t vid;
921ac5d0 803 size_t s;
b1be4280 804 NetClientState *nc = qemu_get_queue(n->nic);
f21c0ed9 805
921ac5d0 806 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
1399c60d 807 vid = virtio_lduw_p(vdev, &vid);
921ac5d0 808 if (s != sizeof(vid)) {
f21c0ed9
AL
809 return VIRTIO_NET_ERR;
810 }
811
f21c0ed9
AL
812 if (vid >= MAX_VLAN)
813 return VIRTIO_NET_ERR;
814
815 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
816 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
817 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
818 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
819 else
820 return VIRTIO_NET_ERR;
821
b1be4280
AK
822 rxfilter_notify(nc);
823
f21c0ed9
AL
824 return VIRTIO_NET_OK;
825}
826
f57fcf70
JW
827static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
828 struct iovec *iov, unsigned int iov_cnt)
829{
830 if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
831 n->status & VIRTIO_NET_S_ANNOUNCE) {
832 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
833 if (n->announce_counter) {
834 timer_mod(n->announce_timer,
835 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
836 self_announce_delay(n->announce_counter));
837 }
838 return VIRTIO_NET_OK;
839 } else {
840 return VIRTIO_NET_ERR;
841 }
842}
843
fed699f9 844static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
f8f7c533 845 struct iovec *iov, unsigned int iov_cnt)
fed699f9 846{
17a0ca55 847 VirtIODevice *vdev = VIRTIO_DEVICE(n);
f8f7c533
JW
848 struct virtio_net_ctrl_mq mq;
849 size_t s;
850 uint16_t queues;
fed699f9 851
f8f7c533
JW
852 s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
853 if (s != sizeof(mq)) {
fed699f9
JW
854 return VIRTIO_NET_ERR;
855 }
856
857 if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
858 return VIRTIO_NET_ERR;
859 }
860
1399c60d 861 queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
fed699f9 862
f8f7c533
JW
863 if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
864 queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
865 queues > n->max_queues ||
fed699f9
JW
866 !n->multiqueue) {
867 return VIRTIO_NET_ERR;
868 }
869
f8f7c533 870 n->curr_queues = queues;
fed699f9
JW
871 /* stop the backend before changing the number of queues to avoid handling a
872 * disabled queue */
17a0ca55 873 virtio_net_set_status(vdev, vdev->status);
fed699f9
JW
874 virtio_net_set_queues(n);
875
876 return VIRTIO_NET_OK;
877}
3d11d36c
AL
878static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
879{
17a0ca55 880 VirtIONet *n = VIRTIO_NET(vdev);
3d11d36c
AL
881 struct virtio_net_ctrl_hdr ctrl;
882 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
51b19ebe 883 VirtQueueElement *elem;
921ac5d0 884 size_t s;
771b6ed3 885 struct iovec *iov, *iov2;
921ac5d0 886 unsigned int iov_cnt;
3d11d36c 887
51b19ebe
PB
888 for (;;) {
889 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
890 if (!elem) {
891 break;
892 }
893 if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
894 iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
e7b43f7e 895 error_report("virtio-net ctrl missing headers");
3d11d36c
AL
896 exit(1);
897 }
898
51b19ebe
PB
899 iov_cnt = elem->out_num;
900 iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
921ac5d0
MT
901 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
902 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
903 if (s != sizeof(ctrl)) {
904 status = VIRTIO_NET_ERR;
dd23454b 905 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
921ac5d0
MT
906 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
907 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
908 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
909 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
910 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
f57fcf70
JW
911 } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
912 status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
fed699f9 913 } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
f8f7c533 914 status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
644c9858
DF
915 } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
916 status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
3d11d36c
AL
917 }
918
51b19ebe 919 s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
921ac5d0 920 assert(s == sizeof(status));
3d11d36c 921
51b19ebe 922 virtqueue_push(vq, elem, sizeof(status));
3d11d36c 923 virtio_notify(vdev, vq);
771b6ed3 924 g_free(iov2);
51b19ebe 925 g_free(elem);
3d11d36c
AL
926 }
927}
928
fbe78f4f
AL
929/* RX */
930
931static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
932{
17a0ca55 933 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9 934 int queue_index = vq2q(virtio_get_queue_index(vq));
8aeff62d 935
fed699f9 936 qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
fbe78f4f
AL
937}
938
4e68f7a0 939static int virtio_net_can_receive(NetClientState *nc)
fbe78f4f 940{
cc1f0f45 941 VirtIONet *n = qemu_get_nic_opaque(nc);
17a0ca55 942 VirtIODevice *vdev = VIRTIO_DEVICE(n);
fed699f9 943 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
0c87e93e 944
17a0ca55 945 if (!vdev->vm_running) {
95477323
MT
946 return 0;
947 }
cdd5cc12 948
fed699f9
JW
949 if (nc->queue_index >= n->curr_queues) {
950 return 0;
951 }
952
0c87e93e 953 if (!virtio_queue_ready(q->rx_vq) ||
17a0ca55 954 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
fbe78f4f 955 return 0;
0c87e93e 956 }
fbe78f4f 957
cdd5cc12
MM
958 return 1;
959}
960
0c87e93e 961static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
cdd5cc12 962{
0c87e93e
JW
963 VirtIONet *n = q->n;
964 if (virtio_queue_empty(q->rx_vq) ||
fbe78f4f 965 (n->mergeable_rx_bufs &&
0c87e93e
JW
966 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
967 virtio_queue_set_notification(q->rx_vq, 1);
06b12970
TL
968
969 /* To avoid a race condition where the guest has made some buffers
970 * available after the above check but before notification was
971 * enabled, check for available buffers again.
972 */
0c87e93e 973 if (virtio_queue_empty(q->rx_vq) ||
06b12970 974 (n->mergeable_rx_bufs &&
0c87e93e 975 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
06b12970 976 return 0;
0c87e93e 977 }
fbe78f4f
AL
978 }
979
0c87e93e 980 virtio_queue_set_notification(q->rx_vq, 0);
fbe78f4f
AL
981 return 1;
982}
983
1399c60d 984static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
032a74a1 985{
1399c60d
RR
986 virtio_tswap16s(vdev, &hdr->hdr_len);
987 virtio_tswap16s(vdev, &hdr->gso_size);
988 virtio_tswap16s(vdev, &hdr->csum_start);
989 virtio_tswap16s(vdev, &hdr->csum_offset);
032a74a1
CLG
990}
991
1d41b0c1
AL
992/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
993 * it never finds out that the packets don't have valid checksums. This
994 * causes dhclient to get upset. Fedora's carried a patch for ages to
995 * fix this with Xen but it hasn't appeared in an upstream release of
996 * dhclient yet.
997 *
998 * To avoid breaking existing guests, we catch udp packets and add
999 * checksums. This is terrible but it's better than hacking the guest
1000 * kernels.
1001 *
1002 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1003 * we should provide a mechanism to disable it to avoid polluting the host
1004 * cache.
1005 */
1006static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
22cc84db 1007 uint8_t *buf, size_t size)
1d41b0c1
AL
1008{
1009 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
1010 (size > 27 && size < 1500) && /* normal sized MTU */
1011 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
1012 (buf[23] == 17) && /* ip.protocol == UDP */
1013 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
22cc84db 1014 net_checksum_calculate(buf, size);
1d41b0c1
AL
1015 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
1016 }
1017}
1018
280598b7
MT
1019static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
1020 const void *buf, size_t size)
fbe78f4f 1021{
3a330134 1022 if (n->has_vnet_hdr) {
22cc84db
MT
1023 /* FIXME this cast is evil */
1024 void *wbuf = (void *)buf;
280598b7
MT
1025 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
1026 size - n->host_hdr_len);
1bfa316c
GK
1027
1028 if (n->needs_vnet_hdr_swap) {
1029 virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
1030 }
280598b7 1031 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
22cc84db
MT
1032 } else {
1033 struct virtio_net_hdr hdr = {
1034 .flags = 0,
1035 .gso_type = VIRTIO_NET_HDR_GSO_NONE
1036 };
1037 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
3a330134 1038 }
fbe78f4f
AL
1039}
1040
3831ab20
AL
1041static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
1042{
1043 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
f21c0ed9 1044 static const uint8_t vlan[] = {0x81, 0x00};
3831ab20 1045 uint8_t *ptr = (uint8_t *)buf;
b6503ed9 1046 int i;
3831ab20
AL
1047
1048 if (n->promisc)
1049 return 1;
1050
e043ebc6 1051 ptr += n->host_hdr_len;
3a330134 1052
f21c0ed9 1053 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
7542d3e7 1054 int vid = lduw_be_p(ptr + 14) & 0xfff;
f21c0ed9
AL
1055 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
1056 return 0;
1057 }
1058
bbe2f399
AW
1059 if (ptr[0] & 1) { // multicast
1060 if (!memcmp(ptr, bcast, sizeof(bcast))) {
015cb166
AW
1061 return !n->nobcast;
1062 } else if (n->nomulti) {
1063 return 0;
8fd2a2f1 1064 } else if (n->allmulti || n->mac_table.multi_overflow) {
bbe2f399
AW
1065 return 1;
1066 }
2d9aba39
AW
1067
1068 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
1069 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1070 return 1;
1071 }
1072 }
bbe2f399 1073 } else { // unicast
015cb166
AW
1074 if (n->nouni) {
1075 return 0;
1076 } else if (n->alluni || n->mac_table.uni_overflow) {
8fd2a2f1
AW
1077 return 1;
1078 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
bbe2f399
AW
1079 return 1;
1080 }
3831ab20 1081
2d9aba39
AW
1082 for (i = 0; i < n->mac_table.first_multi; i++) {
1083 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1084 return 1;
1085 }
1086 }
b6503ed9
AL
1087 }
1088
3831ab20
AL
1089 return 0;
1090}
1091
4e68f7a0 1092static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
fbe78f4f 1093{
cc1f0f45 1094 VirtIONet *n = qemu_get_nic_opaque(nc);
fed699f9 1095 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
17a0ca55 1096 VirtIODevice *vdev = VIRTIO_DEVICE(n);
63c58728
MT
1097 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
1098 struct virtio_net_hdr_mrg_rxbuf mhdr;
1099 unsigned mhdr_cnt = 0;
22cc84db 1100 size_t offset, i, guest_offset;
fbe78f4f 1101
fed699f9 1102 if (!virtio_net_can_receive(nc)) {
cdd5cc12 1103 return -1;
b356f76d 1104 }
cdd5cc12 1105
940cda94 1106 /* hdr_len refers to the header we supply to the guest */
0c87e93e 1107 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
8aeff62d 1108 return 0;
0c87e93e 1109 }
fbe78f4f 1110
3831ab20 1111 if (!receive_filter(n, buf, size))
4f1c942b 1112 return size;
3831ab20 1113
fbe78f4f
AL
1114 offset = i = 0;
1115
1116 while (offset < size) {
51b19ebe 1117 VirtQueueElement *elem;
fbe78f4f 1118 int len, total;
51b19ebe 1119 const struct iovec *sg;
fbe78f4f 1120
22c253d9 1121 total = 0;
fbe78f4f 1122
51b19ebe
PB
1123 elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
1124 if (!elem) {
fbe78f4f 1125 if (i == 0)
4f1c942b 1126 return -1;
e7b43f7e 1127 error_report("virtio-net unexpected empty queue: "
019a3edb
GH
1128 "i %zd mergeable %d offset %zd, size %zd, "
1129 "guest hdr len %zd, host hdr len %zd "
1130 "guest features 0x%" PRIx64,
1131 i, n->mergeable_rx_bufs, offset, size,
1132 n->guest_hdr_len, n->host_hdr_len,
1133 vdev->guest_features);
fbe78f4f
AL
1134 exit(1);
1135 }
1136
51b19ebe 1137 if (elem->in_num < 1) {
e7b43f7e 1138 error_report("virtio-net receive queue contains no in buffers");
fbe78f4f
AL
1139 exit(1);
1140 }
1141
51b19ebe 1142 sg = elem->in_sg;
fbe78f4f 1143 if (i == 0) {
c8d28e7e 1144 assert(offset == 0);
63c58728
MT
1145 if (n->mergeable_rx_bufs) {
1146 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
51b19ebe 1147 sg, elem->in_num,
63c58728
MT
1148 offsetof(typeof(mhdr), num_buffers),
1149 sizeof(mhdr.num_buffers));
1150 }
fbe78f4f 1151
51b19ebe 1152 receive_header(n, sg, elem->in_num, buf, size);
c8d28e7e 1153 offset = n->host_hdr_len;
e35e23f6 1154 total += n->guest_hdr_len;
22cc84db
MT
1155 guest_offset = n->guest_hdr_len;
1156 } else {
1157 guest_offset = 0;
fbe78f4f
AL
1158 }
1159
1160 /* copy in packet. ugh */
51b19ebe 1161 len = iov_from_buf(sg, elem->in_num, guest_offset,
dcf6f5e1 1162 buf + offset, size - offset);
fbe78f4f 1163 total += len;
279a4253
MT
1164 offset += len;
1165 /* If buffers can't be merged, at this point we
1166 * must have consumed the complete packet.
1167 * Otherwise, drop it. */
1168 if (!n->mergeable_rx_bufs && offset < size) {
51b19ebe
PB
1169 virtqueue_discard(q->rx_vq, elem, total);
1170 g_free(elem);
279a4253
MT
1171 return size;
1172 }
fbe78f4f
AL
1173
1174 /* signal other side */
51b19ebe
PB
1175 virtqueue_fill(q->rx_vq, elem, total, i++);
1176 g_free(elem);
fbe78f4f
AL
1177 }
1178
63c58728 1179 if (mhdr_cnt) {
1399c60d 1180 virtio_stw_p(vdev, &mhdr.num_buffers, i);
63c58728
MT
1181 iov_from_buf(mhdr_sg, mhdr_cnt,
1182 0,
1183 &mhdr.num_buffers, sizeof mhdr.num_buffers);
44b15bc5 1184 }
fbe78f4f 1185
0c87e93e 1186 virtqueue_flush(q->rx_vq, i);
17a0ca55 1187 virtio_notify(vdev, q->rx_vq);
4f1c942b
MM
1188
1189 return size;
fbe78f4f
AL
1190}
1191
0c87e93e 1192static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
6243375f 1193
4e68f7a0 1194static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
6243375f 1195{
cc1f0f45 1196 VirtIONet *n = qemu_get_nic_opaque(nc);
fed699f9 1197 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
17a0ca55 1198 VirtIODevice *vdev = VIRTIO_DEVICE(n);
6243375f 1199
51b19ebe 1200 virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
17a0ca55 1201 virtio_notify(vdev, q->tx_vq);
6243375f 1202
51b19ebe
PB
1203 g_free(q->async_tx.elem);
1204 q->async_tx.elem = NULL;
6243375f 1205
0c87e93e
JW
1206 virtio_queue_set_notification(q->tx_vq, 1);
1207 virtio_net_flush_tx(q);
6243375f
MM
1208}
1209
fbe78f4f 1210/* TX */
0c87e93e 1211static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
fbe78f4f 1212{
0c87e93e 1213 VirtIONet *n = q->n;
17a0ca55 1214 VirtIODevice *vdev = VIRTIO_DEVICE(n);
51b19ebe 1215 VirtQueueElement *elem;
e3f30488 1216 int32_t num_packets = 0;
fed699f9 1217 int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
17a0ca55 1218 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
e3f30488
AW
1219 return num_packets;
1220 }
fbe78f4f 1221
51b19ebe 1222 if (q->async_tx.elem) {
0c87e93e 1223 virtio_queue_set_notification(q->tx_vq, 0);
e3f30488 1224 return num_packets;
6243375f
MM
1225 }
1226
51b19ebe 1227 for (;;) {
bd89dd98 1228 ssize_t ret;
51b19ebe
PB
1229 unsigned int out_num;
1230 struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
feb93f36 1231 struct virtio_net_hdr_mrg_rxbuf mhdr;
fbe78f4f 1232
51b19ebe
PB
1233 elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
1234 if (!elem) {
1235 break;
1236 }
1237
1238 out_num = elem->out_num;
1239 out_sg = elem->out_sg;
7b80d08e 1240 if (out_num < 1) {
e7b43f7e 1241 error_report("virtio-net header not in first element");
fbe78f4f
AL
1242 exit(1);
1243 }
1244
032a74a1 1245 if (n->has_vnet_hdr) {
feb93f36
JW
1246 if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
1247 n->guest_hdr_len) {
032a74a1
CLG
1248 error_report("virtio-net header incorrect");
1249 exit(1);
1250 }
1bfa316c 1251 if (n->needs_vnet_hdr_swap) {
feb93f36
JW
1252 virtio_net_hdr_swap(vdev, (void *) &mhdr);
1253 sg2[0].iov_base = &mhdr;
1254 sg2[0].iov_len = n->guest_hdr_len;
1255 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
1256 out_sg, out_num,
1257 n->guest_hdr_len, -1);
1258 if (out_num == VIRTQUEUE_MAX_SIZE) {
1259 goto drop;
1260 }
1261 out_num += 1;
1262 out_sg = sg2;
1263 }
032a74a1 1264 }
14761f9c
MT
1265 /*
1266 * If host wants to see the guest header as is, we can
1267 * pass it on unchanged. Otherwise, copy just the parts
1268 * that host is interested in.
1269 */
1270 assert(n->host_hdr_len <= n->guest_hdr_len);
1271 if (n->host_hdr_len != n->guest_hdr_len) {
1272 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
1273 out_sg, out_num,
1274 0, n->host_hdr_len);
1275 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
1276 out_sg, out_num,
1277 n->guest_hdr_len, -1);
1278 out_num = sg_num;
1279 out_sg = sg;
fbe78f4f
AL
1280 }
1281
fed699f9
JW
1282 ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
1283 out_sg, out_num, virtio_net_tx_complete);
6243375f 1284 if (ret == 0) {
0c87e93e
JW
1285 virtio_queue_set_notification(q->tx_vq, 0);
1286 q->async_tx.elem = elem;
e3f30488 1287 return -EBUSY;
6243375f
MM
1288 }
1289
feb93f36 1290drop:
51b19ebe 1291 virtqueue_push(q->tx_vq, elem, 0);
17a0ca55 1292 virtio_notify(vdev, q->tx_vq);
51b19ebe 1293 g_free(elem);
e3f30488
AW
1294
1295 if (++num_packets >= n->tx_burst) {
1296 break;
1297 }
fbe78f4f 1298 }
e3f30488 1299 return num_packets;
fbe78f4f
AL
1300}
1301
a697a334 1302static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
fbe78f4f 1303{
17a0ca55 1304 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9 1305 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
fbe78f4f 1306
783e7706 1307 /* This happens when device was stopped but VCPU wasn't. */
17a0ca55 1308 if (!vdev->vm_running) {
0c87e93e 1309 q->tx_waiting = 1;
783e7706
MT
1310 return;
1311 }
1312
0c87e93e 1313 if (q->tx_waiting) {
fbe78f4f 1314 virtio_queue_set_notification(vq, 1);
bc72ad67 1315 timer_del(q->tx_timer);
0c87e93e
JW
1316 q->tx_waiting = 0;
1317 virtio_net_flush_tx(q);
fbe78f4f 1318 } else {
bc72ad67
AB
1319 timer_mod(q->tx_timer,
1320 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
0c87e93e 1321 q->tx_waiting = 1;
fbe78f4f
AL
1322 virtio_queue_set_notification(vq, 0);
1323 }
1324}
1325
a697a334
AW
1326static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
1327{
17a0ca55 1328 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9 1329 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
a697a334 1330
0c87e93e 1331 if (unlikely(q->tx_waiting)) {
a697a334
AW
1332 return;
1333 }
0c87e93e 1334 q->tx_waiting = 1;
783e7706 1335 /* This happens when device was stopped but VCPU wasn't. */
17a0ca55 1336 if (!vdev->vm_running) {
783e7706
MT
1337 return;
1338 }
a697a334 1339 virtio_queue_set_notification(vq, 0);
0c87e93e 1340 qemu_bh_schedule(q->tx_bh);
a697a334
AW
1341}
1342
fbe78f4f
AL
1343static void virtio_net_tx_timer(void *opaque)
1344{
0c87e93e
JW
1345 VirtIONetQueue *q = opaque;
1346 VirtIONet *n = q->n;
17a0ca55 1347 VirtIODevice *vdev = VIRTIO_DEVICE(n);
e8bcf842
MT
1348 /* This happens when device was stopped but BH wasn't. */
1349 if (!vdev->vm_running) {
1350 /* Make sure tx waiting is set, so we'll run when restarted. */
1351 assert(q->tx_waiting);
1352 return;
1353 }
fbe78f4f 1354
0c87e93e 1355 q->tx_waiting = 0;
fbe78f4f
AL
1356
1357 /* Just in case the driver is not ready on more */
17a0ca55 1358 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
fbe78f4f 1359 return;
17a0ca55 1360 }
fbe78f4f 1361
0c87e93e
JW
1362 virtio_queue_set_notification(q->tx_vq, 1);
1363 virtio_net_flush_tx(q);
fbe78f4f
AL
1364}
1365
a697a334
AW
1366static void virtio_net_tx_bh(void *opaque)
1367{
0c87e93e
JW
1368 VirtIONetQueue *q = opaque;
1369 VirtIONet *n = q->n;
17a0ca55 1370 VirtIODevice *vdev = VIRTIO_DEVICE(n);
a697a334
AW
1371 int32_t ret;
1372
e8bcf842
MT
1373 /* This happens when device was stopped but BH wasn't. */
1374 if (!vdev->vm_running) {
1375 /* Make sure tx waiting is set, so we'll run when restarted. */
1376 assert(q->tx_waiting);
1377 return;
1378 }
783e7706 1379
0c87e93e 1380 q->tx_waiting = 0;
a697a334
AW
1381
1382 /* Just in case the driver is not ready on more */
17a0ca55 1383 if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
a697a334 1384 return;
17a0ca55 1385 }
a697a334 1386
0c87e93e 1387 ret = virtio_net_flush_tx(q);
a697a334
AW
1388 if (ret == -EBUSY) {
1389 return; /* Notification re-enable handled by tx_complete */
1390 }
1391
1392 /* If we flush a full burst of packets, assume there are
1393 * more coming and immediately reschedule */
1394 if (ret >= n->tx_burst) {
0c87e93e
JW
1395 qemu_bh_schedule(q->tx_bh);
1396 q->tx_waiting = 1;
a697a334
AW
1397 return;
1398 }
1399
1400 /* If less than a full burst, re-enable notification and flush
1401 * anything that may have come in while we weren't looking. If
1402 * we find something, assume the guest is still active and reschedule */
0c87e93e
JW
1403 virtio_queue_set_notification(q->tx_vq, 1);
1404 if (virtio_net_flush_tx(q) > 0) {
1405 virtio_queue_set_notification(q->tx_vq, 0);
1406 qemu_bh_schedule(q->tx_bh);
1407 q->tx_waiting = 1;
a697a334
AW
1408 }
1409}
1410
f9d6dbf0
WC
1411static void virtio_net_add_queue(VirtIONet *n, int index)
1412{
1413 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1414
1415 n->vqs[index].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx);
1416 if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
1417 n->vqs[index].tx_vq =
1418 virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
1419 n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1420 virtio_net_tx_timer,
1421 &n->vqs[index]);
1422 } else {
1423 n->vqs[index].tx_vq =
1424 virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
1425 n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
1426 }
1427
1428 n->vqs[index].tx_waiting = 0;
1429 n->vqs[index].n = n;
1430}
1431
1432static void virtio_net_del_queue(VirtIONet *n, int index)
1433{
1434 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1435 VirtIONetQueue *q = &n->vqs[index];
1436 NetClientState *nc = qemu_get_subqueue(n->nic, index);
1437
1438 qemu_purge_queued_packets(nc);
1439
1440 virtio_del_queue(vdev, index * 2);
1441 if (q->tx_timer) {
1442 timer_del(q->tx_timer);
1443 timer_free(q->tx_timer);
1444 } else {
1445 qemu_bh_delete(q->tx_bh);
1446 }
1447 virtio_del_queue(vdev, index * 2 + 1);
1448}
1449
1450static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
1451{
1452 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1453 int old_num_queues = virtio_get_num_queues(vdev);
1454 int new_num_queues = new_max_queues * 2 + 1;
1455 int i;
1456
1457 assert(old_num_queues >= 3);
1458 assert(old_num_queues % 2 == 1);
1459
1460 if (old_num_queues == new_num_queues) {
1461 return;
1462 }
1463
1464 /*
1465 * We always need to remove and add ctrl vq if
1466 * old_num_queues != new_num_queues. Remove ctrl_vq first,
1467 * and then we only enter one of the following too loops.
1468 */
1469 virtio_del_queue(vdev, old_num_queues - 1);
1470
1471 for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
1472 /* new_num_queues < old_num_queues */
1473 virtio_net_del_queue(n, i / 2);
1474 }
1475
1476 for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
1477 /* new_num_queues > old_num_queues */
1478 virtio_net_add_queue(n, i / 2);
1479 }
1480
1481 /* add ctrl_vq last */
1482 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1483}
1484
ec57db16 1485static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
fed699f9 1486{
f9d6dbf0
WC
1487 int max = multiqueue ? n->max_queues : 1;
1488
fed699f9 1489 n->multiqueue = multiqueue;
f9d6dbf0 1490 virtio_net_change_num_queues(n, max);
fed699f9 1491
fed699f9
JW
1492 virtio_net_set_queues(n);
1493}
1494
fbe78f4f
AL
1495static void virtio_net_save(QEMUFile *f, void *opaque)
1496{
1497 VirtIONet *n = opaque;
17a0ca55 1498 VirtIODevice *vdev = VIRTIO_DEVICE(n);
fbe78f4f 1499
afbaa7b4
MT
1500 /* At this point, backend must be stopped, otherwise
1501 * it might keep writing to memory. */
1502 assert(!n->vhost_started);
17a0ca55 1503 virtio_save(vdev, f);
037dab2f
GK
1504}
1505
1506static void virtio_net_save_device(VirtIODevice *vdev, QEMUFile *f)
1507{
1508 VirtIONet *n = VIRTIO_NET(vdev);
1509 int i;
fbe78f4f 1510
79674068 1511 qemu_put_buffer(f, n->mac, ETH_ALEN);
5f800801 1512 qemu_put_be32(f, n->vqs[0].tx_waiting);
e46cb38f 1513 qemu_put_be32(f, n->mergeable_rx_bufs);
9d6271b8 1514 qemu_put_be16(f, n->status);
f10c592e
AW
1515 qemu_put_byte(f, n->promisc);
1516 qemu_put_byte(f, n->allmulti);
b6503ed9
AL
1517 qemu_put_be32(f, n->mac_table.in_use);
1518 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
f21c0ed9 1519 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
3a330134 1520 qemu_put_be32(f, n->has_vnet_hdr);
8fd2a2f1
AW
1521 qemu_put_byte(f, n->mac_table.multi_overflow);
1522 qemu_put_byte(f, n->mac_table.uni_overflow);
015cb166
AW
1523 qemu_put_byte(f, n->alluni);
1524 qemu_put_byte(f, n->nomulti);
1525 qemu_put_byte(f, n->nouni);
1526 qemu_put_byte(f, n->nobcast);
0ce0e8f4 1527 qemu_put_byte(f, n->has_ufo);
5f800801
JW
1528 if (n->max_queues > 1) {
1529 qemu_put_be16(f, n->max_queues);
1530 qemu_put_be16(f, n->curr_queues);
1531 for (i = 1; i < n->curr_queues; i++) {
1532 qemu_put_be32(f, n->vqs[i].tx_waiting);
1533 }
1534 }
644c9858 1535
95129d6f 1536 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
644c9858
DF
1537 qemu_put_be64(f, n->curr_guest_offloads);
1538 }
fbe78f4f
AL
1539}
1540
1541static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
1542{
1543 VirtIONet *n = opaque;
17a0ca55 1544 VirtIODevice *vdev = VIRTIO_DEVICE(n);
fbe78f4f 1545
9d6271b8 1546 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
fbe78f4f
AL
1547 return -EINVAL;
1548
6c666823 1549 return virtio_load(vdev, f, version_id);
037dab2f
GK
1550}
1551
1552static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
1553 int version_id)
1554{
1555 VirtIONet *n = VIRTIO_NET(vdev);
1556 int i, link_down;
fbe78f4f 1557
79674068 1558 qemu_get_buffer(f, n->mac, ETH_ALEN);
5f800801 1559 n->vqs[0].tx_waiting = qemu_get_be32(f);
ff3a8066 1560
bb9d17f8 1561 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f),
95129d6f
CH
1562 virtio_vdev_has_feature(vdev,
1563 VIRTIO_F_VERSION_1));
fbe78f4f 1564
9d6271b8
AL
1565 if (version_id >= 3)
1566 n->status = qemu_get_be16(f);
1567
002437cd 1568 if (version_id >= 4) {
f10c592e
AW
1569 if (version_id < 8) {
1570 n->promisc = qemu_get_be32(f);
1571 n->allmulti = qemu_get_be32(f);
1572 } else {
1573 n->promisc = qemu_get_byte(f);
1574 n->allmulti = qemu_get_byte(f);
1575 }
002437cd
AL
1576 }
1577
b6503ed9
AL
1578 if (version_id >= 5) {
1579 n->mac_table.in_use = qemu_get_be32(f);
1580 /* MAC_TABLE_ENTRIES may be different from the saved image */
1581 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
1582 qemu_get_buffer(f, n->mac_table.macs,
1583 n->mac_table.in_use * ETH_ALEN);
98f93ddd
MT
1584 } else {
1585 int64_t i;
1586
1587 /* Overflow detected - can happen if source has a larger MAC table.
1588 * We simply set overflow flag so there's no need to maintain the
1589 * table of addresses, discard them all.
1590 * Note: 64 bit math to avoid integer overflow.
1591 */
1592 for (i = 0; i < (int64_t)n->mac_table.in_use * ETH_ALEN; ++i) {
1593 qemu_get_byte(f);
1594 }
8fd2a2f1 1595 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
b6503ed9
AL
1596 n->mac_table.in_use = 0;
1597 }
1598 }
1599
f21c0ed9
AL
1600 if (version_id >= 6)
1601 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1602
3a330134
MM
1603 if (version_id >= 7) {
1604 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
1ecda02b 1605 error_report("virtio-net: saved image requires vnet_hdr=on");
3a330134
MM
1606 return -1;
1607 }
6c042c16
AW
1608 }
1609
8fd2a2f1
AW
1610 if (version_id >= 9) {
1611 n->mac_table.multi_overflow = qemu_get_byte(f);
1612 n->mac_table.uni_overflow = qemu_get_byte(f);
1613 }
1614
015cb166
AW
1615 if (version_id >= 10) {
1616 n->alluni = qemu_get_byte(f);
1617 n->nomulti = qemu_get_byte(f);
1618 n->nouni = qemu_get_byte(f);
1619 n->nobcast = qemu_get_byte(f);
1620 }
1621
0ce0e8f4
MM
1622 if (version_id >= 11) {
1623 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
1ecda02b 1624 error_report("virtio-net: saved image requires TUN_F_UFO support");
0ce0e8f4
MM
1625 return -1;
1626 }
1627 }
1628
5f800801
JW
1629 if (n->max_queues > 1) {
1630 if (n->max_queues != qemu_get_be16(f)) {
1631 error_report("virtio-net: different max_queues ");
1632 return -1;
1633 }
1634
1635 n->curr_queues = qemu_get_be16(f);
eea750a5
MT
1636 if (n->curr_queues > n->max_queues) {
1637 error_report("virtio-net: curr_queues %x > max_queues %x",
1638 n->curr_queues, n->max_queues);
1639 return -1;
1640 }
5f800801
JW
1641 for (i = 1; i < n->curr_queues; i++) {
1642 n->vqs[i].tx_waiting = qemu_get_be32(f);
1643 }
1644 }
1645
6c666823
MT
1646 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1647 n->curr_guest_offloads = qemu_get_be64(f);
1648 } else {
1649 n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
1650 }
1651
1652 if (peer_has_vnet_hdr(n)) {
1653 virtio_net_apply_guest_offloads(n);
1654 }
1655
5f800801
JW
1656 virtio_net_set_queues(n);
1657
2d9aba39
AW
1658 /* Find the first multicast entry in the saved MAC filter */
1659 for (i = 0; i < n->mac_table.in_use; i++) {
1660 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1661 break;
1662 }
1663 }
1664 n->mac_table.first_multi = i;
98991481
AK
1665
1666 /* nc.link_down can't be migrated, so infer link_down according
1667 * to link status bit in n->status */
5f800801
JW
1668 link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
1669 for (i = 0; i < n->max_queues; i++) {
1670 qemu_get_subqueue(n->nic, i)->link_down = link_down;
1671 }
98991481 1672
6c666823
MT
1673 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
1674 virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
1675 n->announce_counter = SELF_ANNOUNCE_ROUNDS;
1676 timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL));
1677 }
1678
fbe78f4f
AL
1679 return 0;
1680}
1681
eb6b6c12 1682static NetClientInfo net_virtio_info = {
2be64a68 1683 .type = NET_CLIENT_OPTIONS_KIND_NIC,
eb6b6c12
MM
1684 .size = sizeof(NICState),
1685 .can_receive = virtio_net_can_receive,
1686 .receive = virtio_net_receive,
eb6b6c12 1687 .link_status_changed = virtio_net_set_link_status,
b1be4280 1688 .query_rx_filter = virtio_net_query_rxfilter,
eb6b6c12
MM
1689};
1690
f56a1247
MT
1691static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1692{
17a0ca55 1693 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9 1694 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
f56a1247 1695 assert(n->vhost_started);
ed8b4afe 1696 return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
f56a1247
MT
1697}
1698
1699static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1700 bool mask)
1701{
17a0ca55 1702 VirtIONet *n = VIRTIO_NET(vdev);
fed699f9 1703 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
f56a1247 1704 assert(n->vhost_started);
ed8b4afe 1705 vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
f56a1247
MT
1706 vdev, idx, mask);
1707}
1708
019a3edb 1709static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
fbe78f4f 1710{
14f9b664 1711 int i, config_size = 0;
0cd09c3a 1712 virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
14f9b664
JL
1713 for (i = 0; feature_sizes[i].flags != 0; i++) {
1714 if (host_features & feature_sizes[i].flags) {
1715 config_size = MAX(feature_sizes[i].end, config_size);
1716 }
1717 }
17ec5a86
FK
1718 n->config_size = config_size;
1719}
1720
8a253ec2
FK
1721void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
1722 const char *type)
1723{
1724 /*
1725 * The name can be NULL, the netclient name will be type.x.
1726 */
1727 assert(type != NULL);
1728
9e288406 1729 g_free(n->netclient_name);
9e288406 1730 g_free(n->netclient_type);
80e0090a 1731 n->netclient_name = g_strdup(name);
8a253ec2
FK
1732 n->netclient_type = g_strdup(type);
1733}
1734
e6f746b3 1735static void virtio_net_device_realize(DeviceState *dev, Error **errp)
17ec5a86 1736{
e6f746b3 1737 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
284a32f0 1738 VirtIONet *n = VIRTIO_NET(dev);
b1be4280 1739 NetClientState *nc;
284a32f0 1740 int i;
1773d9ee 1741
da3e8a23 1742 virtio_net_set_config_size(n, n->host_features);
284a32f0 1743 virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
fbe78f4f 1744
575a1c0e 1745 n->max_queues = MAX(n->nic_conf.peers.queues, 1);
87b3bd1c 1746 if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
7e0e736e 1747 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
631b22ea 1748 "must be a positive integer less than %d.",
87b3bd1c 1749 n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
7e0e736e
JW
1750 virtio_cleanup(vdev);
1751 return;
1752 }
f6b26cf2 1753 n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
fed699f9 1754 n->curr_queues = 1;
1773d9ee 1755 n->tx_timeout = n->net_conf.txtimer;
a697a334 1756
1773d9ee
FK
1757 if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
1758 && strcmp(n->net_conf.tx, "bh")) {
e7b43f7e
SH
1759 error_report("virtio-net: "
1760 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1773d9ee 1761 n->net_conf.tx);
e7b43f7e 1762 error_report("Defaulting to \"bh\"");
a697a334
AW
1763 }
1764
da51a335 1765 for (i = 0; i < n->max_queues; i++) {
f9d6dbf0 1766 virtio_net_add_queue(n, i);
a697a334 1767 }
da51a335 1768
17a0ca55 1769 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1773d9ee
FK
1770 qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
1771 memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
554c97dd 1772 n->status = VIRTIO_NET_S_LINK_UP;
f57fcf70
JW
1773 n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1774 virtio_net_announce_timer, n);
fbe78f4f 1775
8a253ec2
FK
1776 if (n->netclient_type) {
1777 /*
1778 * Happen when virtio_net_set_netclient_name has been called.
1779 */
1780 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1781 n->netclient_type, n->netclient_name, n);
1782 } else {
1783 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
284a32f0 1784 object_get_typename(OBJECT(dev)), dev->id, n);
8a253ec2
FK
1785 }
1786
6e371ab8
MT
1787 peer_test_vnet_hdr(n);
1788 if (peer_has_vnet_hdr(n)) {
fed699f9 1789 for (i = 0; i < n->max_queues; i++) {
d6085e3a 1790 qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
fed699f9 1791 }
6e371ab8
MT
1792 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1793 } else {
1794 n->host_hdr_len = 0;
1795 }
eb6b6c12 1796
1773d9ee 1797 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
96d5e201 1798
fed699f9 1799 n->vqs[0].tx_waiting = 0;
1773d9ee 1800 n->tx_burst = n->net_conf.txburst;
bb9d17f8 1801 virtio_net_set_mrg_rx_bufs(n, 0, 0);
002437cd 1802 n->promisc = 1; /* for compatibility */
fbe78f4f 1803
7267c094 1804 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
b6503ed9 1805
7267c094 1806 n->vlans = g_malloc0(MAX_VLAN >> 3);
f21c0ed9 1807
b1be4280
AK
1808 nc = qemu_get_queue(n->nic);
1809 nc->rxfilter_notify_enabled = 1;
1810
284a32f0
AF
1811 n->qdev = dev;
1812 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
fbe78f4f 1813 virtio_net_save, virtio_net_load, n);
17ec5a86
FK
1814}
1815
306ec6c3 1816static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
17ec5a86 1817{
306ec6c3
AF
1818 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1819 VirtIONet *n = VIRTIO_NET(dev);
f9d6dbf0 1820 int i, max_queues;
17ec5a86
FK
1821
1822 /* This will stop vhost backend if appropriate. */
1823 virtio_net_set_status(vdev, 0);
1824
306ec6c3 1825 unregister_savevm(dev, "virtio-net", n);
17ec5a86 1826
9e288406
MA
1827 g_free(n->netclient_name);
1828 n->netclient_name = NULL;
1829 g_free(n->netclient_type);
1830 n->netclient_type = NULL;
8a253ec2 1831
17ec5a86
FK
1832 g_free(n->mac_table.macs);
1833 g_free(n->vlans);
1834
f9d6dbf0
WC
1835 max_queues = n->multiqueue ? n->max_queues : 1;
1836 for (i = 0; i < max_queues; i++) {
1837 virtio_net_del_queue(n, i);
17ec5a86
FK
1838 }
1839
f57fcf70
JW
1840 timer_del(n->announce_timer);
1841 timer_free(n->announce_timer);
17ec5a86
FK
1842 g_free(n->vqs);
1843 qemu_del_nic(n->nic);
6a1a8cc7 1844 virtio_cleanup(vdev);
17ec5a86
FK
1845}
1846
1847static void virtio_net_instance_init(Object *obj)
1848{
1849 VirtIONet *n = VIRTIO_NET(obj);
1850
1851 /*
1852 * The default config_size is sizeof(struct virtio_net_config).
1853 * Can be overriden with virtio_net_set_config_size.
1854 */
1855 n->config_size = sizeof(struct virtio_net_config);
aa4197c3
GA
1856 device_add_bootindex_property(obj, &n->nic_conf.bootindex,
1857 "bootindex", "/ethernet-phy@0",
1858 DEVICE(n), NULL);
17ec5a86
FK
1859}
1860
1861static Property virtio_net_properties[] = {
87108bb2
SZ
1862 DEFINE_PROP_BIT("csum", VirtIONet, host_features, VIRTIO_NET_F_CSUM, true),
1863 DEFINE_PROP_BIT("guest_csum", VirtIONet, host_features,
1864 VIRTIO_NET_F_GUEST_CSUM, true),
1865 DEFINE_PROP_BIT("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
1866 DEFINE_PROP_BIT("guest_tso4", VirtIONet, host_features,
1867 VIRTIO_NET_F_GUEST_TSO4, true),
1868 DEFINE_PROP_BIT("guest_tso6", VirtIONet, host_features,
1869 VIRTIO_NET_F_GUEST_TSO6, true),
1870 DEFINE_PROP_BIT("guest_ecn", VirtIONet, host_features,
1871 VIRTIO_NET_F_GUEST_ECN, true),
1872 DEFINE_PROP_BIT("guest_ufo", VirtIONet, host_features,
1873 VIRTIO_NET_F_GUEST_UFO, true),
1874 DEFINE_PROP_BIT("guest_announce", VirtIONet, host_features,
1875 VIRTIO_NET_F_GUEST_ANNOUNCE, true),
1876 DEFINE_PROP_BIT("host_tso4", VirtIONet, host_features,
1877 VIRTIO_NET_F_HOST_TSO4, true),
1878 DEFINE_PROP_BIT("host_tso6", VirtIONet, host_features,
1879 VIRTIO_NET_F_HOST_TSO6, true),
1880 DEFINE_PROP_BIT("host_ecn", VirtIONet, host_features,
1881 VIRTIO_NET_F_HOST_ECN, true),
1882 DEFINE_PROP_BIT("host_ufo", VirtIONet, host_features,
1883 VIRTIO_NET_F_HOST_UFO, true),
1884 DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet, host_features,
1885 VIRTIO_NET_F_MRG_RXBUF, true),
1886 DEFINE_PROP_BIT("status", VirtIONet, host_features,
1887 VIRTIO_NET_F_STATUS, true),
1888 DEFINE_PROP_BIT("ctrl_vq", VirtIONet, host_features,
1889 VIRTIO_NET_F_CTRL_VQ, true),
1890 DEFINE_PROP_BIT("ctrl_rx", VirtIONet, host_features,
1891 VIRTIO_NET_F_CTRL_RX, true),
1892 DEFINE_PROP_BIT("ctrl_vlan", VirtIONet, host_features,
1893 VIRTIO_NET_F_CTRL_VLAN, true),
1894 DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet, host_features,
1895 VIRTIO_NET_F_CTRL_RX_EXTRA, true),
1896 DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet, host_features,
1897 VIRTIO_NET_F_CTRL_MAC_ADDR, true),
1898 DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet, host_features,
1899 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
1900 DEFINE_PROP_BIT("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
17ec5a86
FK
1901 DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
1902 DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
87108bb2 1903 TX_TIMER_INTERVAL),
17ec5a86
FK
1904 DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
1905 DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
1906 DEFINE_PROP_END_OF_LIST(),
1907};
1908
1909static void virtio_net_class_init(ObjectClass *klass, void *data)
1910{
1911 DeviceClass *dc = DEVICE_CLASS(klass);
1912 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
e6f746b3 1913
17ec5a86 1914 dc->props = virtio_net_properties;
125ee0ed 1915 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
e6f746b3 1916 vdc->realize = virtio_net_device_realize;
306ec6c3 1917 vdc->unrealize = virtio_net_device_unrealize;
17ec5a86
FK
1918 vdc->get_config = virtio_net_get_config;
1919 vdc->set_config = virtio_net_set_config;
1920 vdc->get_features = virtio_net_get_features;
1921 vdc->set_features = virtio_net_set_features;
1922 vdc->bad_features = virtio_net_bad_features;
1923 vdc->reset = virtio_net_reset;
1924 vdc->set_status = virtio_net_set_status;
1925 vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
1926 vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
037dab2f
GK
1927 vdc->load = virtio_net_load_device;
1928 vdc->save = virtio_net_save_device;
17ec5a86
FK
1929}
1930
1931static const TypeInfo virtio_net_info = {
1932 .name = TYPE_VIRTIO_NET,
1933 .parent = TYPE_VIRTIO_DEVICE,
1934 .instance_size = sizeof(VirtIONet),
1935 .instance_init = virtio_net_instance_init,
1936 .class_init = virtio_net_class_init,
1937};
1938
1939static void virtio_register_types(void)
1940{
1941 type_register_static(&virtio_net_info);
1942}
1943
1944type_init(virtio_register_types)