]> git.proxmox.com Git - qemu.git/blame - hw/virtio-net.c
virtio-net: remove layout assumptions for ctrl vq
[qemu.git] / hw / 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"
fbe78f4f 15#include "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"
fbe78f4f 21#include "virtio-net.h"
9bc6304c 22#include "vhost_net.h"
fbe78f4f 23
0ce0e8f4 24#define VIRTIO_NET_VM_VERSION 11
b6503ed9 25
4ffb17f5 26#define MAC_TABLE_ENTRIES 64
f21c0ed9 27#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
9d6271b8 28
fbe78f4f
AL
29typedef struct VirtIONet
30{
31 VirtIODevice vdev;
79674068 32 uint8_t mac[ETH_ALEN];
554c97dd 33 uint16_t status;
fbe78f4f
AL
34 VirtQueue *rx_vq;
35 VirtQueue *tx_vq;
3d11d36c 36 VirtQueue *ctrl_vq;
eb6b6c12 37 NICState *nic;
fbe78f4f 38 QEMUTimer *tx_timer;
a697a334 39 QEMUBH *tx_bh;
f0c07c7c 40 uint32_t tx_timeout;
e3f30488 41 int32_t tx_burst;
4b4b8d36 42 int tx_waiting;
3a330134 43 uint32_t has_vnet_hdr;
e35e23f6
MT
44 size_t host_hdr_len;
45 size_t guest_hdr_len;
0ce0e8f4 46 uint8_t has_ufo;
6243375f
MM
47 struct {
48 VirtQueueElement elem;
49 ssize_t len;
50 } async_tx;
fbe78f4f 51 int mergeable_rx_bufs;
f10c592e
AW
52 uint8_t promisc;
53 uint8_t allmulti;
015cb166
AW
54 uint8_t alluni;
55 uint8_t nomulti;
56 uint8_t nouni;
57 uint8_t nobcast;
9bc6304c 58 uint8_t vhost_started;
b6503ed9
AL
59 struct {
60 int in_use;
2d9aba39 61 int first_multi;
8fd2a2f1
AW
62 uint8_t multi_overflow;
63 uint8_t uni_overflow;
b6503ed9
AL
64 uint8_t *macs;
65 } mac_table;
f21c0ed9 66 uint32_t *vlans;
01657c86 67 DeviceState *qdev;
fbe78f4f
AL
68} VirtIONet;
69
70/* TODO
71 * - we could suppress RX interrupt if we were so inclined.
72 */
73
74static VirtIONet *to_virtio_net(VirtIODevice *vdev)
75{
76 return (VirtIONet *)vdev;
77}
78
0f03eca6 79static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
fbe78f4f
AL
80{
81 VirtIONet *n = to_virtio_net(vdev);
82 struct virtio_net_config netcfg;
83
b46d97f2 84 stw_p(&netcfg.status, n->status);
79674068 85 memcpy(netcfg.mac, n->mac, ETH_ALEN);
fbe78f4f
AL
86 memcpy(config, &netcfg, sizeof(netcfg));
87}
88
0f03eca6
AL
89static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
90{
91 VirtIONet *n = to_virtio_net(vdev);
92 struct virtio_net_config netcfg;
93
94 memcpy(&netcfg, config, sizeof(netcfg));
95
79674068
AL
96 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
97 memcpy(n->mac, netcfg.mac, ETH_ALEN);
eb6b6c12 98 qemu_format_nic_info_str(&n->nic->nc, n->mac);
0f03eca6
AL
99 }
100}
101
783e7706
MT
102static bool virtio_net_started(VirtIONet *n, uint8_t status)
103{
104 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
85cf2a8d 105 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
783e7706
MT
106}
107
108static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
afbaa7b4 109{
afbaa7b4
MT
110 if (!n->nic->nc.peer) {
111 return;
112 }
2be64a68 113 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
afbaa7b4
MT
114 return;
115 }
116
117 if (!tap_get_vhost_net(n->nic->nc.peer)) {
118 return;
119 }
32993698
MT
120 if (!!n->vhost_started == virtio_net_started(n, status) &&
121 !n->nic->nc.peer->link_down) {
afbaa7b4
MT
122 return;
123 }
124 if (!n->vhost_started) {
5430a28f 125 int r;
126 if (!vhost_net_query(tap_get_vhost_net(n->nic->nc.peer), &n->vdev)) {
127 return;
128 }
1830b80f 129 n->vhost_started = 1;
5430a28f 130 r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
afbaa7b4 131 if (r < 0) {
e7b43f7e
SH
132 error_report("unable to start vhost net: %d: "
133 "falling back on userspace virtio", -r);
1830b80f 134 n->vhost_started = 0;
afbaa7b4
MT
135 }
136 } else {
137 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
138 n->vhost_started = 0;
139 }
140}
141
783e7706
MT
142static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
143{
144 VirtIONet *n = to_virtio_net(vdev);
145
146 virtio_net_vhost_status(n, status);
147
148 if (!n->tx_waiting) {
149 return;
150 }
151
152 if (virtio_net_started(n, status) && !n->vhost_started) {
153 if (n->tx_timer) {
154 qemu_mod_timer(n->tx_timer,
74475455 155 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
783e7706
MT
156 } else {
157 qemu_bh_schedule(n->tx_bh);
158 }
159 } else {
160 if (n->tx_timer) {
161 qemu_del_timer(n->tx_timer);
162 } else {
163 qemu_bh_cancel(n->tx_bh);
164 }
165 }
166}
167
4e68f7a0 168static void virtio_net_set_link_status(NetClientState *nc)
554c97dd 169{
eb6b6c12 170 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
554c97dd
AL
171 uint16_t old_status = n->status;
172
eb6b6c12 173 if (nc->link_down)
554c97dd
AL
174 n->status &= ~VIRTIO_NET_S_LINK_UP;
175 else
176 n->status |= VIRTIO_NET_S_LINK_UP;
177
178 if (n->status != old_status)
179 virtio_notify_config(&n->vdev);
afbaa7b4
MT
180
181 virtio_net_set_status(&n->vdev, n->vdev.status);
554c97dd
AL
182}
183
002437cd
AL
184static void virtio_net_reset(VirtIODevice *vdev)
185{
186 VirtIONet *n = to_virtio_net(vdev);
187
188 /* Reset back to compatibility mode */
189 n->promisc = 1;
190 n->allmulti = 0;
015cb166
AW
191 n->alluni = 0;
192 n->nomulti = 0;
193 n->nouni = 0;
194 n->nobcast = 0;
b6503ed9 195
f21c0ed9 196 /* Flush any MAC and VLAN filter table state */
b6503ed9 197 n->mac_table.in_use = 0;
2d9aba39 198 n->mac_table.first_multi = 0;
8fd2a2f1
AW
199 n->mac_table.multi_overflow = 0;
200 n->mac_table.uni_overflow = 0;
b6503ed9 201 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
41dc8a67 202 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
f21c0ed9 203 memset(n->vlans, 0, MAX_VLAN >> 3);
002437cd
AL
204}
205
6e371ab8 206static void peer_test_vnet_hdr(VirtIONet *n)
3a330134 207{
eb6b6c12 208 if (!n->nic->nc.peer)
6e371ab8 209 return;
3a330134 210
2be64a68 211 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP)
6e371ab8 212 return;
3a330134 213
eb6b6c12 214 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
6e371ab8 215}
3a330134 216
6e371ab8
MT
217static int peer_has_vnet_hdr(VirtIONet *n)
218{
3a330134
MM
219 return n->has_vnet_hdr;
220}
221
0ce0e8f4
MM
222static int peer_has_ufo(VirtIONet *n)
223{
224 if (!peer_has_vnet_hdr(n))
225 return 0;
226
eb6b6c12 227 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
0ce0e8f4
MM
228
229 return n->has_ufo;
230}
231
ff3a8066
MT
232static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
233{
234 n->mergeable_rx_bufs = mergeable_rx_bufs;
235
236 n->guest_hdr_len = n->mergeable_rx_bufs ?
237 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
238
239 if (peer_has_vnet_hdr(n) &&
240 tap_has_vnet_hdr_len(n->nic->nc.peer, n->guest_hdr_len)) {
241 tap_set_vnet_hdr_len(n->nic->nc.peer, n->guest_hdr_len);
242 n->host_hdr_len = n->guest_hdr_len;
243 }
244}
245
8172539d 246static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
fbe78f4f 247{
3a330134 248 VirtIONet *n = to_virtio_net(vdev);
fbe78f4f 249
c9f79a3f
MT
250 features |= (1 << VIRTIO_NET_F_MAC);
251
6e371ab8 252 if (!peer_has_vnet_hdr(n)) {
8172539d
MT
253 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
254 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
255 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
256 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
257
258 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
259 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
260 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
261 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
262 }
3a330134 263
8172539d
MT
264 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
265 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
266 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
3a330134
MM
267 }
268
9bc6304c 269 if (!n->nic->nc.peer ||
2be64a68 270 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
9bc6304c
MT
271 return features;
272 }
273 if (!tap_get_vhost_net(n->nic->nc.peer)) {
274 return features;
275 }
276 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
fbe78f4f
AL
277}
278
8eca6b1b
AL
279static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
280{
281 uint32_t features = 0;
282
283 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
284 * but also these: */
285 features |= (1 << VIRTIO_NET_F_MAC);
184bd048
DK
286 features |= (1 << VIRTIO_NET_F_CSUM);
287 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
288 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
289 features |= (1 << VIRTIO_NET_F_HOST_ECN);
8eca6b1b 290
8172539d 291 return features;
8eca6b1b
AL
292}
293
fbe78f4f
AL
294static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
295{
296 VirtIONet *n = to_virtio_net(vdev);
297
ff3a8066 298 virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)));
f5436dd9
MM
299
300 if (n->has_vnet_hdr) {
eb6b6c12 301 tap_set_offload(n->nic->nc.peer,
f5436dd9
MM
302 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
303 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
304 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
6c9f58ba
SS
305 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
306 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
f5436dd9 307 }
dc14a397 308 if (!n->nic->nc.peer ||
2be64a68 309 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
dc14a397
DS
310 return;
311 }
312 if (!tap_get_vhost_net(n->nic->nc.peer)) {
313 return;
314 }
57c3229b 315 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
fbe78f4f
AL
316}
317
002437cd 318static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
921ac5d0 319 struct iovec *iov, unsigned int iov_cnt)
002437cd
AL
320{
321 uint8_t on;
921ac5d0 322 size_t s;
002437cd 323
921ac5d0
MT
324 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
325 if (s != sizeof(on)) {
326 return VIRTIO_NET_ERR;
002437cd
AL
327 }
328
921ac5d0 329 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC) {
002437cd 330 n->promisc = on;
921ac5d0 331 } else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI) {
002437cd 332 n->allmulti = on;
921ac5d0 333 } else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI) {
015cb166 334 n->alluni = on;
921ac5d0 335 } else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI) {
015cb166 336 n->nomulti = on;
921ac5d0 337 } else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI) {
015cb166 338 n->nouni = on;
921ac5d0 339 } else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST) {
015cb166 340 n->nobcast = on;
921ac5d0 341 } else {
002437cd 342 return VIRTIO_NET_ERR;
921ac5d0 343 }
002437cd
AL
344
345 return VIRTIO_NET_OK;
346}
347
b6503ed9 348static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
921ac5d0 349 struct iovec *iov, unsigned int iov_cnt)
b6503ed9
AL
350{
351 struct virtio_net_ctrl_mac mac_data;
921ac5d0 352 size_t s;
b6503ed9 353
921ac5d0 354 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
b6503ed9 355 return VIRTIO_NET_ERR;
921ac5d0 356 }
b6503ed9
AL
357
358 n->mac_table.in_use = 0;
2d9aba39 359 n->mac_table.first_multi = 0;
8fd2a2f1
AW
360 n->mac_table.uni_overflow = 0;
361 n->mac_table.multi_overflow = 0;
b6503ed9
AL
362 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
363
921ac5d0
MT
364 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
365 sizeof(mac_data.entries));
366 mac_data.entries = ldl_p(&mac_data.entries);
367 if (s != sizeof(mac_data.entries)) {
368 return VIRTIO_NET_ERR;
369 }
370 iov_discard_front(&iov, &iov_cnt, s);
b6503ed9 371
921ac5d0 372 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
b6503ed9 373 return VIRTIO_NET_ERR;
921ac5d0 374 }
b6503ed9
AL
375
376 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
921ac5d0
MT
377 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
378 mac_data.entries * ETH_ALEN);
379 if (s != mac_data.entries * ETH_ALEN) {
380 return VIRTIO_NET_ERR;
381 }
b6503ed9
AL
382 n->mac_table.in_use += mac_data.entries;
383 } else {
8fd2a2f1 384 n->mac_table.uni_overflow = 1;
b6503ed9
AL
385 }
386
921ac5d0
MT
387 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
388
2d9aba39
AW
389 n->mac_table.first_multi = n->mac_table.in_use;
390
921ac5d0
MT
391 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
392 sizeof(mac_data.entries));
393 mac_data.entries = ldl_p(&mac_data.entries);
394 if (s != sizeof(mac_data.entries)) {
395 return VIRTIO_NET_ERR;
396 }
397
398 iov_discard_front(&iov, &iov_cnt, s);
b6503ed9 399
921ac5d0 400 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
b6503ed9 401 return VIRTIO_NET_ERR;
921ac5d0 402 }
b6503ed9 403
921ac5d0
MT
404 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
405 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
406 mac_data.entries * ETH_ALEN);
407 if (s != mac_data.entries * ETH_ALEN) {
408 return VIRTIO_NET_ERR;
8fd2a2f1 409 }
921ac5d0
MT
410 n->mac_table.in_use += mac_data.entries;
411 } else {
412 n->mac_table.multi_overflow = 1;
b6503ed9
AL
413 }
414
415 return VIRTIO_NET_OK;
416}
417
f21c0ed9 418static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
921ac5d0 419 struct iovec *iov, unsigned int iov_cnt)
f21c0ed9
AL
420{
421 uint16_t vid;
921ac5d0 422 size_t s;
f21c0ed9 423
921ac5d0
MT
424 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
425 vid = lduw_p(&vid);
426 if (s != sizeof(vid)) {
f21c0ed9
AL
427 return VIRTIO_NET_ERR;
428 }
429
f21c0ed9
AL
430 if (vid >= MAX_VLAN)
431 return VIRTIO_NET_ERR;
432
433 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
434 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
435 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
436 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
437 else
438 return VIRTIO_NET_ERR;
439
440 return VIRTIO_NET_OK;
441}
442
3d11d36c
AL
443static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
444{
002437cd 445 VirtIONet *n = to_virtio_net(vdev);
3d11d36c
AL
446 struct virtio_net_ctrl_hdr ctrl;
447 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
448 VirtQueueElement elem;
921ac5d0
MT
449 size_t s;
450 struct iovec *iov;
451 unsigned int iov_cnt;
3d11d36c
AL
452
453 while (virtqueue_pop(vq, &elem)) {
921ac5d0
MT
454 if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) ||
455 iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) {
e7b43f7e 456 error_report("virtio-net ctrl missing headers");
3d11d36c
AL
457 exit(1);
458 }
459
921ac5d0
MT
460 iov = elem.out_sg;
461 iov_cnt = elem.out_num;
462 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
463 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
464 if (s != sizeof(ctrl)) {
465 status = VIRTIO_NET_ERR;
466 } else if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE) {
467 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
468 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
469 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
470 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
471 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
3d11d36c
AL
472 }
473
921ac5d0
MT
474 s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status));
475 assert(s == sizeof(status));
3d11d36c
AL
476
477 virtqueue_push(vq, &elem, sizeof(status));
478 virtio_notify(vdev, vq);
479 }
480}
481
fbe78f4f
AL
482/* RX */
483
484static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
485{
8aeff62d
MM
486 VirtIONet *n = to_virtio_net(vdev);
487
eb6b6c12 488 qemu_flush_queued_packets(&n->nic->nc);
fbe78f4f
AL
489}
490
4e68f7a0 491static int virtio_net_can_receive(NetClientState *nc)
fbe78f4f 492{
eb6b6c12 493 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
85cf2a8d 494 if (!n->vdev.vm_running) {
95477323
MT
495 return 0;
496 }
cdd5cc12 497
fbe78f4f
AL
498 if (!virtio_queue_ready(n->rx_vq) ||
499 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
500 return 0;
501
cdd5cc12
MM
502 return 1;
503}
504
505static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
506{
fbe78f4f
AL
507 if (virtio_queue_empty(n->rx_vq) ||
508 (n->mergeable_rx_bufs &&
509 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
510 virtio_queue_set_notification(n->rx_vq, 1);
06b12970
TL
511
512 /* To avoid a race condition where the guest has made some buffers
513 * available after the above check but before notification was
514 * enabled, check for available buffers again.
515 */
516 if (virtio_queue_empty(n->rx_vq) ||
517 (n->mergeable_rx_bufs &&
518 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
519 return 0;
fbe78f4f
AL
520 }
521
522 virtio_queue_set_notification(n->rx_vq, 0);
523 return 1;
524}
525
1d41b0c1
AL
526/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
527 * it never finds out that the packets don't have valid checksums. This
528 * causes dhclient to get upset. Fedora's carried a patch for ages to
529 * fix this with Xen but it hasn't appeared in an upstream release of
530 * dhclient yet.
531 *
532 * To avoid breaking existing guests, we catch udp packets and add
533 * checksums. This is terrible but it's better than hacking the guest
534 * kernels.
535 *
536 * N.B. if we introduce a zero-copy API, this operation is no longer free so
537 * we should provide a mechanism to disable it to avoid polluting the host
538 * cache.
539 */
540static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
22cc84db 541 uint8_t *buf, size_t size)
1d41b0c1
AL
542{
543 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
544 (size > 27 && size < 1500) && /* normal sized MTU */
545 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
546 (buf[23] == 17) && /* ip.protocol == UDP */
547 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
22cc84db 548 net_checksum_calculate(buf, size);
1d41b0c1
AL
549 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
550 }
551}
552
280598b7
MT
553static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
554 const void *buf, size_t size)
fbe78f4f 555{
3a330134 556 if (n->has_vnet_hdr) {
22cc84db
MT
557 /* FIXME this cast is evil */
558 void *wbuf = (void *)buf;
280598b7
MT
559 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
560 size - n->host_hdr_len);
561 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
22cc84db
MT
562 } else {
563 struct virtio_net_hdr hdr = {
564 .flags = 0,
565 .gso_type = VIRTIO_NET_HDR_GSO_NONE
566 };
567 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
3a330134 568 }
fbe78f4f
AL
569}
570
3831ab20
AL
571static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
572{
573 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
f21c0ed9 574 static const uint8_t vlan[] = {0x81, 0x00};
3831ab20 575 uint8_t *ptr = (uint8_t *)buf;
b6503ed9 576 int i;
3831ab20
AL
577
578 if (n->promisc)
579 return 1;
580
e043ebc6 581 ptr += n->host_hdr_len;
3a330134 582
f21c0ed9
AL
583 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
584 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
585 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
586 return 0;
587 }
588
bbe2f399
AW
589 if (ptr[0] & 1) { // multicast
590 if (!memcmp(ptr, bcast, sizeof(bcast))) {
015cb166
AW
591 return !n->nobcast;
592 } else if (n->nomulti) {
593 return 0;
8fd2a2f1 594 } else if (n->allmulti || n->mac_table.multi_overflow) {
bbe2f399
AW
595 return 1;
596 }
2d9aba39
AW
597
598 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
599 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
600 return 1;
601 }
602 }
bbe2f399 603 } else { // unicast
015cb166
AW
604 if (n->nouni) {
605 return 0;
606 } else if (n->alluni || n->mac_table.uni_overflow) {
8fd2a2f1
AW
607 return 1;
608 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
bbe2f399
AW
609 return 1;
610 }
3831ab20 611
2d9aba39
AW
612 for (i = 0; i < n->mac_table.first_multi; i++) {
613 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
614 return 1;
615 }
616 }
b6503ed9
AL
617 }
618
3831ab20
AL
619 return 0;
620}
621
4e68f7a0 622static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
fbe78f4f 623{
eb6b6c12 624 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
63c58728
MT
625 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
626 struct virtio_net_hdr_mrg_rxbuf mhdr;
627 unsigned mhdr_cnt = 0;
22cc84db 628 size_t offset, i, guest_offset;
fbe78f4f 629
eb6b6c12 630 if (!virtio_net_can_receive(&n->nic->nc))
cdd5cc12
MM
631 return -1;
632
940cda94 633 /* hdr_len refers to the header we supply to the guest */
e35e23f6 634 if (!virtio_net_has_buffers(n, size + n->guest_hdr_len - n->host_hdr_len))
8aeff62d 635 return 0;
fbe78f4f 636
3831ab20 637 if (!receive_filter(n, buf, size))
4f1c942b 638 return size;
3831ab20 639
fbe78f4f
AL
640 offset = i = 0;
641
642 while (offset < size) {
643 VirtQueueElement elem;
644 int len, total;
22cc84db 645 const struct iovec *sg = elem.in_sg;
fbe78f4f 646
22c253d9 647 total = 0;
fbe78f4f 648
279a4253 649 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
fbe78f4f 650 if (i == 0)
4f1c942b 651 return -1;
e7b43f7e 652 error_report("virtio-net unexpected empty queue: "
279a4253 653 "i %zd mergeable %d offset %zd, size %zd, "
e7b43f7e 654 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
279a4253 655 i, n->mergeable_rx_bufs, offset, size,
e35e23f6 656 n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
fbe78f4f
AL
657 exit(1);
658 }
659
660 if (elem.in_num < 1) {
e7b43f7e 661 error_report("virtio-net receive queue contains no in buffers");
fbe78f4f
AL
662 exit(1);
663 }
664
fbe78f4f 665 if (i == 0) {
c8d28e7e 666 assert(offset == 0);
63c58728
MT
667 if (n->mergeable_rx_bufs) {
668 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
669 sg, elem.in_num,
670 offsetof(typeof(mhdr), num_buffers),
671 sizeof(mhdr.num_buffers));
672 }
fbe78f4f 673
c8d28e7e
MT
674 receive_header(n, sg, elem.in_num, buf, size);
675 offset = n->host_hdr_len;
e35e23f6 676 total += n->guest_hdr_len;
22cc84db
MT
677 guest_offset = n->guest_hdr_len;
678 } else {
679 guest_offset = 0;
fbe78f4f
AL
680 }
681
682 /* copy in packet. ugh */
22cc84db 683 len = iov_from_buf(sg, elem.in_num, guest_offset,
dcf6f5e1 684 buf + offset, size - offset);
fbe78f4f 685 total += len;
279a4253
MT
686 offset += len;
687 /* If buffers can't be merged, at this point we
688 * must have consumed the complete packet.
689 * Otherwise, drop it. */
690 if (!n->mergeable_rx_bufs && offset < size) {
691#if 0
e7b43f7e
SH
692 error_report("virtio-net truncated non-mergeable packet: "
693 "i %zd mergeable %d offset %zd, size %zd, "
694 "guest hdr len %zd, host hdr len %zd",
695 i, n->mergeable_rx_bufs,
e35e23f6 696 offset, size, n->guest_hdr_len, n->host_hdr_len);
279a4253
MT
697#endif
698 return size;
699 }
fbe78f4f
AL
700
701 /* signal other side */
702 virtqueue_fill(n->rx_vq, &elem, total, i++);
fbe78f4f
AL
703 }
704
63c58728
MT
705 if (mhdr_cnt) {
706 stw_p(&mhdr.num_buffers, i);
707 iov_from_buf(mhdr_sg, mhdr_cnt,
708 0,
709 &mhdr.num_buffers, sizeof mhdr.num_buffers);
44b15bc5 710 }
fbe78f4f
AL
711
712 virtqueue_flush(n->rx_vq, i);
713 virtio_notify(&n->vdev, n->rx_vq);
4f1c942b
MM
714
715 return size;
fbe78f4f
AL
716}
717
e3f30488 718static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
6243375f 719
4e68f7a0 720static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
6243375f 721{
eb6b6c12 722 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
6243375f 723
40bad8f3 724 virtqueue_push(n->tx_vq, &n->async_tx.elem, 0);
6243375f
MM
725 virtio_notify(&n->vdev, n->tx_vq);
726
727 n->async_tx.elem.out_num = n->async_tx.len = 0;
728
729 virtio_queue_set_notification(n->tx_vq, 1);
730 virtio_net_flush_tx(n, n->tx_vq);
731}
732
fbe78f4f 733/* TX */
e3f30488 734static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
fbe78f4f
AL
735{
736 VirtQueueElement elem;
e3f30488 737 int32_t num_packets = 0;
e3f30488
AW
738 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
739 return num_packets;
740 }
fbe78f4f 741
85cf2a8d 742 assert(n->vdev.vm_running);
783e7706 743
6243375f
MM
744 if (n->async_tx.elem.out_num) {
745 virtio_queue_set_notification(n->tx_vq, 0);
e3f30488 746 return num_packets;
6243375f
MM
747 }
748
fbe78f4f 749 while (virtqueue_pop(vq, &elem)) {
14761f9c 750 ssize_t ret, len;
fbe78f4f
AL
751 unsigned int out_num = elem.out_num;
752 struct iovec *out_sg = &elem.out_sg[0];
14761f9c 753 struct iovec sg[VIRTQUEUE_MAX_SIZE];
fbe78f4f 754
7b80d08e 755 if (out_num < 1) {
e7b43f7e 756 error_report("virtio-net header not in first element");
fbe78f4f
AL
757 exit(1);
758 }
759
14761f9c
MT
760 /*
761 * If host wants to see the guest header as is, we can
762 * pass it on unchanged. Otherwise, copy just the parts
763 * that host is interested in.
764 */
765 assert(n->host_hdr_len <= n->guest_hdr_len);
766 if (n->host_hdr_len != n->guest_hdr_len) {
767 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
768 out_sg, out_num,
769 0, n->host_hdr_len);
770 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
771 out_sg, out_num,
772 n->guest_hdr_len, -1);
773 out_num = sg_num;
774 out_sg = sg;
fbe78f4f
AL
775 }
776
7b80d08e 777 len = n->guest_hdr_len;
14761f9c 778
eb6b6c12 779 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
6243375f
MM
780 virtio_net_tx_complete);
781 if (ret == 0) {
782 virtio_queue_set_notification(n->tx_vq, 0);
783 n->async_tx.elem = elem;
784 n->async_tx.len = len;
e3f30488 785 return -EBUSY;
6243375f
MM
786 }
787
788 len += ret;
fbe78f4f 789
40bad8f3 790 virtqueue_push(vq, &elem, 0);
fbe78f4f 791 virtio_notify(&n->vdev, vq);
e3f30488
AW
792
793 if (++num_packets >= n->tx_burst) {
794 break;
795 }
fbe78f4f 796 }
e3f30488 797 return num_packets;
fbe78f4f
AL
798}
799
a697a334 800static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
fbe78f4f
AL
801{
802 VirtIONet *n = to_virtio_net(vdev);
803
783e7706 804 /* This happens when device was stopped but VCPU wasn't. */
85cf2a8d 805 if (!n->vdev.vm_running) {
783e7706
MT
806 n->tx_waiting = 1;
807 return;
808 }
809
4b4b8d36 810 if (n->tx_waiting) {
fbe78f4f
AL
811 virtio_queue_set_notification(vq, 1);
812 qemu_del_timer(n->tx_timer);
4b4b8d36 813 n->tx_waiting = 0;
fbe78f4f
AL
814 virtio_net_flush_tx(n, vq);
815 } else {
816 qemu_mod_timer(n->tx_timer,
74475455 817 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
4b4b8d36 818 n->tx_waiting = 1;
fbe78f4f
AL
819 virtio_queue_set_notification(vq, 0);
820 }
821}
822
a697a334
AW
823static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
824{
825 VirtIONet *n = to_virtio_net(vdev);
826
827 if (unlikely(n->tx_waiting)) {
828 return;
829 }
783e7706
MT
830 n->tx_waiting = 1;
831 /* This happens when device was stopped but VCPU wasn't. */
85cf2a8d 832 if (!n->vdev.vm_running) {
783e7706
MT
833 return;
834 }
a697a334
AW
835 virtio_queue_set_notification(vq, 0);
836 qemu_bh_schedule(n->tx_bh);
a697a334
AW
837}
838
fbe78f4f
AL
839static void virtio_net_tx_timer(void *opaque)
840{
841 VirtIONet *n = opaque;
85cf2a8d 842 assert(n->vdev.vm_running);
fbe78f4f 843
4b4b8d36 844 n->tx_waiting = 0;
fbe78f4f
AL
845
846 /* Just in case the driver is not ready on more */
847 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
848 return;
849
850 virtio_queue_set_notification(n->tx_vq, 1);
851 virtio_net_flush_tx(n, n->tx_vq);
852}
853
a697a334
AW
854static void virtio_net_tx_bh(void *opaque)
855{
856 VirtIONet *n = opaque;
857 int32_t ret;
858
85cf2a8d 859 assert(n->vdev.vm_running);
783e7706 860
a697a334
AW
861 n->tx_waiting = 0;
862
863 /* Just in case the driver is not ready on more */
864 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
865 return;
866
867 ret = virtio_net_flush_tx(n, n->tx_vq);
868 if (ret == -EBUSY) {
869 return; /* Notification re-enable handled by tx_complete */
870 }
871
872 /* If we flush a full burst of packets, assume there are
873 * more coming and immediately reschedule */
874 if (ret >= n->tx_burst) {
875 qemu_bh_schedule(n->tx_bh);
876 n->tx_waiting = 1;
877 return;
878 }
879
880 /* If less than a full burst, re-enable notification and flush
881 * anything that may have come in while we weren't looking. If
882 * we find something, assume the guest is still active and reschedule */
883 virtio_queue_set_notification(n->tx_vq, 1);
884 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
885 virtio_queue_set_notification(n->tx_vq, 0);
886 qemu_bh_schedule(n->tx_bh);
887 n->tx_waiting = 1;
888 }
889}
890
fbe78f4f
AL
891static void virtio_net_save(QEMUFile *f, void *opaque)
892{
893 VirtIONet *n = opaque;
894
afbaa7b4
MT
895 /* At this point, backend must be stopped, otherwise
896 * it might keep writing to memory. */
897 assert(!n->vhost_started);
fbe78f4f
AL
898 virtio_save(&n->vdev, f);
899
79674068 900 qemu_put_buffer(f, n->mac, ETH_ALEN);
4b4b8d36 901 qemu_put_be32(f, n->tx_waiting);
e46cb38f 902 qemu_put_be32(f, n->mergeable_rx_bufs);
9d6271b8 903 qemu_put_be16(f, n->status);
f10c592e
AW
904 qemu_put_byte(f, n->promisc);
905 qemu_put_byte(f, n->allmulti);
b6503ed9
AL
906 qemu_put_be32(f, n->mac_table.in_use);
907 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
f21c0ed9 908 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
3a330134 909 qemu_put_be32(f, n->has_vnet_hdr);
8fd2a2f1
AW
910 qemu_put_byte(f, n->mac_table.multi_overflow);
911 qemu_put_byte(f, n->mac_table.uni_overflow);
015cb166
AW
912 qemu_put_byte(f, n->alluni);
913 qemu_put_byte(f, n->nomulti);
914 qemu_put_byte(f, n->nouni);
915 qemu_put_byte(f, n->nobcast);
0ce0e8f4 916 qemu_put_byte(f, n->has_ufo);
fbe78f4f
AL
917}
918
919static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
920{
921 VirtIONet *n = opaque;
2d9aba39 922 int i;
2a633c46 923 int ret;
fbe78f4f 924
9d6271b8 925 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
fbe78f4f
AL
926 return -EINVAL;
927
2a633c46
OW
928 ret = virtio_load(&n->vdev, f);
929 if (ret) {
930 return ret;
931 }
fbe78f4f 932
79674068 933 qemu_get_buffer(f, n->mac, ETH_ALEN);
4b4b8d36 934 n->tx_waiting = qemu_get_be32(f);
ff3a8066
MT
935
936 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f));
fbe78f4f 937
9d6271b8
AL
938 if (version_id >= 3)
939 n->status = qemu_get_be16(f);
940
002437cd 941 if (version_id >= 4) {
f10c592e
AW
942 if (version_id < 8) {
943 n->promisc = qemu_get_be32(f);
944 n->allmulti = qemu_get_be32(f);
945 } else {
946 n->promisc = qemu_get_byte(f);
947 n->allmulti = qemu_get_byte(f);
948 }
002437cd
AL
949 }
950
b6503ed9
AL
951 if (version_id >= 5) {
952 n->mac_table.in_use = qemu_get_be32(f);
953 /* MAC_TABLE_ENTRIES may be different from the saved image */
954 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
955 qemu_get_buffer(f, n->mac_table.macs,
956 n->mac_table.in_use * ETH_ALEN);
957 } else if (n->mac_table.in_use) {
e398d61b
JQ
958 uint8_t *buf = g_malloc0(n->mac_table.in_use);
959 qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
960 g_free(buf);
8fd2a2f1 961 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
b6503ed9
AL
962 n->mac_table.in_use = 0;
963 }
964 }
965
f21c0ed9
AL
966 if (version_id >= 6)
967 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
968
3a330134
MM
969 if (version_id >= 7) {
970 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
1ecda02b 971 error_report("virtio-net: saved image requires vnet_hdr=on");
3a330134
MM
972 return -1;
973 }
974
975 if (n->has_vnet_hdr) {
eb6b6c12 976 tap_set_offload(n->nic->nc.peer,
704a76fc
MT
977 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
978 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
979 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
980 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
981 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
3a330134 982 }
6c042c16
AW
983 }
984
8fd2a2f1
AW
985 if (version_id >= 9) {
986 n->mac_table.multi_overflow = qemu_get_byte(f);
987 n->mac_table.uni_overflow = qemu_get_byte(f);
988 }
989
015cb166
AW
990 if (version_id >= 10) {
991 n->alluni = qemu_get_byte(f);
992 n->nomulti = qemu_get_byte(f);
993 n->nouni = qemu_get_byte(f);
994 n->nobcast = qemu_get_byte(f);
995 }
996
0ce0e8f4
MM
997 if (version_id >= 11) {
998 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
1ecda02b 999 error_report("virtio-net: saved image requires TUN_F_UFO support");
0ce0e8f4
MM
1000 return -1;
1001 }
1002 }
1003
2d9aba39
AW
1004 /* Find the first multicast entry in the saved MAC filter */
1005 for (i = 0; i < n->mac_table.in_use; i++) {
1006 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1007 break;
1008 }
1009 }
1010 n->mac_table.first_multi = i;
98991481
AK
1011
1012 /* nc.link_down can't be migrated, so infer link_down according
1013 * to link status bit in n->status */
1014 n->nic->nc.link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
1015
fbe78f4f
AL
1016 return 0;
1017}
1018
4e68f7a0 1019static void virtio_net_cleanup(NetClientState *nc)
b946a153 1020{
eb6b6c12 1021 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
b946a153 1022
eb6b6c12 1023 n->nic = NULL;
b946a153
AL
1024}
1025
eb6b6c12 1026static NetClientInfo net_virtio_info = {
2be64a68 1027 .type = NET_CLIENT_OPTIONS_KIND_NIC,
eb6b6c12
MM
1028 .size = sizeof(NICState),
1029 .can_receive = virtio_net_can_receive,
1030 .receive = virtio_net_receive,
1031 .cleanup = virtio_net_cleanup,
1032 .link_status_changed = virtio_net_set_link_status,
1033};
1034
f56a1247
MT
1035static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1036{
1037 VirtIONet *n = to_virtio_net(vdev);
1038 assert(n->vhost_started);
1039 return vhost_net_virtqueue_pending(tap_get_vhost_net(n->nic->nc.peer), idx);
1040}
1041
1042static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1043 bool mask)
1044{
1045 VirtIONet *n = to_virtio_net(vdev);
1046 assert(n->vhost_started);
1047 vhost_net_virtqueue_mask(tap_get_vhost_net(n->nic->nc.peer),
1048 vdev, idx, mask);
1049}
1050
f0c07c7c
AW
1051VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1052 virtio_net_conf *net)
fbe78f4f
AL
1053{
1054 VirtIONet *n;
fbe78f4f 1055
53c25cea
PB
1056 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1057 sizeof(struct virtio_net_config),
1058 sizeof(VirtIONet));
fbe78f4f 1059
0f03eca6
AL
1060 n->vdev.get_config = virtio_net_get_config;
1061 n->vdev.set_config = virtio_net_set_config;
fbe78f4f
AL
1062 n->vdev.get_features = virtio_net_get_features;
1063 n->vdev.set_features = virtio_net_set_features;
8eca6b1b 1064 n->vdev.bad_features = virtio_net_bad_features;
002437cd 1065 n->vdev.reset = virtio_net_reset;
9bc6304c 1066 n->vdev.set_status = virtio_net_set_status;
f56a1247
MT
1067 n->vdev.guest_notifier_mask = virtio_net_guest_notifier_mask;
1068 n->vdev.guest_notifier_pending = virtio_net_guest_notifier_pending;
fbe78f4f 1069 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
a697a334
AW
1070
1071 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
e7b43f7e
SH
1072 error_report("virtio-net: "
1073 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1074 net->tx);
1075 error_report("Defaulting to \"bh\"");
a697a334
AW
1076 }
1077
1078 if (net->tx && !strcmp(net->tx, "timer")) {
1079 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
74475455 1080 n->tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, n);
a697a334
AW
1081 n->tx_timeout = net->txtimer;
1082 } else {
1083 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1084 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1085 }
4ffb17f5 1086 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
97b15621 1087 qemu_macaddr_default_if_unset(&conf->macaddr);
3cbe04c4 1088 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
554c97dd 1089 n->status = VIRTIO_NET_S_LINK_UP;
fbe78f4f 1090
f79f2bfc 1091 n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
6e371ab8
MT
1092 peer_test_vnet_hdr(n);
1093 if (peer_has_vnet_hdr(n)) {
1094 tap_using_vnet_hdr(n->nic->nc.peer, 1);
1095 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1096 } else {
1097 n->host_hdr_len = 0;
1098 }
eb6b6c12
MM
1099
1100 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
96d5e201 1101
4b4b8d36 1102 n->tx_waiting = 0;
e3f30488 1103 n->tx_burst = net->txburst;
ff3a8066 1104 virtio_net_set_mrg_rx_bufs(n, 0);
002437cd 1105 n->promisc = 1; /* for compatibility */
fbe78f4f 1106
7267c094 1107 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
b6503ed9 1108
7267c094 1109 n->vlans = g_malloc0(MAX_VLAN >> 3);
f21c0ed9 1110
01657c86
AW
1111 n->qdev = dev;
1112 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
fbe78f4f 1113 virtio_net_save, virtio_net_load, n);
cf21e106 1114
1ca4d09a
GN
1115 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1116
53c25cea 1117 return &n->vdev;
cf21e106 1118}
97b15621
GH
1119
1120void virtio_net_exit(VirtIODevice *vdev)
1121{
1122 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
9bc6304c 1123
afbaa7b4
MT
1124 /* This will stop vhost backend if appropriate. */
1125 virtio_net_set_status(vdev, 0);
97b15621 1126
eb6b6c12 1127 qemu_purge_queued_packets(&n->nic->nc);
97b15621 1128
01657c86 1129 unregister_savevm(n->qdev, "virtio-net", n);
97b15621 1130
7267c094
AL
1131 g_free(n->mac_table.macs);
1132 g_free(n->vlans);
97b15621 1133
a697a334
AW
1134 if (n->tx_timer) {
1135 qemu_del_timer(n->tx_timer);
1136 qemu_free_timer(n->tx_timer);
1137 } else {
1138 qemu_bh_delete(n->tx_bh);
1139 }
97b15621 1140
b20c6b9e 1141 qemu_del_net_client(&n->nic->nc);
b52dfd71 1142 virtio_cleanup(&n->vdev);
97b15621 1143}