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