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