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