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