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