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