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