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