]> git.proxmox.com Git - qemu.git/blame - hw/virtio-net.c
oss: refactor code around policy setting
[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
14#include "virtio.h"
15#include "net.h"
7200ac3c 16#include "net/checksum.h"
a8ed73f7 17#include "net/tap.h"
fbe78f4f
AL
18#include "qemu-timer.h"
19#include "virtio-net.h"
20
0ce0e8f4 21#define VIRTIO_NET_VM_VERSION 11
b6503ed9 22
4ffb17f5 23#define MAC_TABLE_ENTRIES 64
f21c0ed9 24#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
9d6271b8 25
fbe78f4f
AL
26typedef struct VirtIONet
27{
28 VirtIODevice vdev;
79674068 29 uint8_t mac[ETH_ALEN];
554c97dd 30 uint16_t status;
fbe78f4f
AL
31 VirtQueue *rx_vq;
32 VirtQueue *tx_vq;
3d11d36c 33 VirtQueue *ctrl_vq;
eb6b6c12 34 NICState *nic;
fbe78f4f
AL
35 QEMUTimer *tx_timer;
36 int tx_timer_active;
3a330134 37 uint32_t has_vnet_hdr;
0ce0e8f4 38 uint8_t has_ufo;
6243375f
MM
39 struct {
40 VirtQueueElement elem;
41 ssize_t len;
42 } async_tx;
fbe78f4f 43 int mergeable_rx_bufs;
f10c592e
AW
44 uint8_t promisc;
45 uint8_t allmulti;
015cb166
AW
46 uint8_t alluni;
47 uint8_t nomulti;
48 uint8_t nouni;
49 uint8_t nobcast;
b6503ed9
AL
50 struct {
51 int in_use;
2d9aba39 52 int first_multi;
8fd2a2f1
AW
53 uint8_t multi_overflow;
54 uint8_t uni_overflow;
b6503ed9
AL
55 uint8_t *macs;
56 } mac_table;
f21c0ed9 57 uint32_t *vlans;
fbe78f4f
AL
58} VirtIONet;
59
60/* TODO
61 * - we could suppress RX interrupt if we were so inclined.
62 */
63
64static VirtIONet *to_virtio_net(VirtIODevice *vdev)
65{
66 return (VirtIONet *)vdev;
67}
68
0f03eca6 69static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
fbe78f4f
AL
70{
71 VirtIONet *n = to_virtio_net(vdev);
72 struct virtio_net_config netcfg;
73
554c97dd 74 netcfg.status = n->status;
79674068 75 memcpy(netcfg.mac, n->mac, ETH_ALEN);
fbe78f4f
AL
76 memcpy(config, &netcfg, sizeof(netcfg));
77}
78
0f03eca6
AL
79static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
80{
81 VirtIONet *n = to_virtio_net(vdev);
82 struct virtio_net_config netcfg;
83
84 memcpy(&netcfg, config, sizeof(netcfg));
85
79674068
AL
86 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
87 memcpy(n->mac, netcfg.mac, ETH_ALEN);
eb6b6c12 88 qemu_format_nic_info_str(&n->nic->nc, n->mac);
0f03eca6
AL
89 }
90}
91
eb6b6c12 92static void virtio_net_set_link_status(VLANClientState *nc)
554c97dd 93{
eb6b6c12 94 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
554c97dd
AL
95 uint16_t old_status = n->status;
96
eb6b6c12 97 if (nc->link_down)
554c97dd
AL
98 n->status &= ~VIRTIO_NET_S_LINK_UP;
99 else
100 n->status |= VIRTIO_NET_S_LINK_UP;
101
102 if (n->status != old_status)
103 virtio_notify_config(&n->vdev);
104}
105
002437cd
AL
106static void virtio_net_reset(VirtIODevice *vdev)
107{
108 VirtIONet *n = to_virtio_net(vdev);
109
110 /* Reset back to compatibility mode */
111 n->promisc = 1;
112 n->allmulti = 0;
015cb166
AW
113 n->alluni = 0;
114 n->nomulti = 0;
115 n->nouni = 0;
116 n->nobcast = 0;
b6503ed9 117
f21c0ed9 118 /* Flush any MAC and VLAN filter table state */
b6503ed9 119 n->mac_table.in_use = 0;
2d9aba39 120 n->mac_table.first_multi = 0;
8fd2a2f1
AW
121 n->mac_table.multi_overflow = 0;
122 n->mac_table.uni_overflow = 0;
b6503ed9 123 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
f21c0ed9 124 memset(n->vlans, 0, MAX_VLAN >> 3);
002437cd
AL
125}
126
3a330134
MM
127static int peer_has_vnet_hdr(VirtIONet *n)
128{
eb6b6c12 129 if (!n->nic->nc.peer)
3a330134
MM
130 return 0;
131
665a3b07 132 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
3a330134
MM
133 return 0;
134
eb6b6c12 135 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
3a330134
MM
136
137 return n->has_vnet_hdr;
138}
139
0ce0e8f4
MM
140static int peer_has_ufo(VirtIONet *n)
141{
142 if (!peer_has_vnet_hdr(n))
143 return 0;
144
eb6b6c12 145 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
0ce0e8f4
MM
146
147 return n->has_ufo;
148}
149
fbe78f4f
AL
150static uint32_t virtio_net_get_features(VirtIODevice *vdev)
151{
3a330134 152 VirtIONet *n = to_virtio_net(vdev);
3d11d36c 153 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
e16044ef 154 (1 << VIRTIO_NET_F_MRG_RXBUF) |
3d11d36c 155 (1 << VIRTIO_NET_F_STATUS) |
b6503ed9 156 (1 << VIRTIO_NET_F_CTRL_VQ) |
f21c0ed9 157 (1 << VIRTIO_NET_F_CTRL_RX) |
015cb166
AW
158 (1 << VIRTIO_NET_F_CTRL_VLAN) |
159 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
fbe78f4f 160
3a330134 161 if (peer_has_vnet_hdr(n)) {
eb6b6c12 162 tap_using_vnet_hdr(n->nic->nc.peer, 1);
3a330134
MM
163
164 features |= (1 << VIRTIO_NET_F_CSUM);
165 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
166 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
167 features |= (1 << VIRTIO_NET_F_HOST_ECN);
f5436dd9
MM
168
169 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
170 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
171 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
172 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
6c9f58ba 173
0ce0e8f4 174 if (peer_has_ufo(n)) {
6c9f58ba
SS
175 features |= (1 << VIRTIO_NET_F_GUEST_UFO);
176 features |= (1 << VIRTIO_NET_F_HOST_UFO);
177 }
3a330134
MM
178 }
179
fbe78f4f
AL
180 return features;
181}
182
8eca6b1b
AL
183static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
184{
185 uint32_t features = 0;
186
187 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
188 * but also these: */
189 features |= (1 << VIRTIO_NET_F_MAC);
184bd048
DK
190 features |= (1 << VIRTIO_NET_F_CSUM);
191 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
192 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
193 features |= (1 << VIRTIO_NET_F_HOST_ECN);
8eca6b1b
AL
194
195 return features & virtio_net_get_features(vdev);
196}
197
fbe78f4f
AL
198static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
199{
200 VirtIONet *n = to_virtio_net(vdev);
201
202 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
f5436dd9
MM
203
204 if (n->has_vnet_hdr) {
eb6b6c12 205 tap_set_offload(n->nic->nc.peer,
f5436dd9
MM
206 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
207 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
208 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
6c9f58ba
SS
209 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
210 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
f5436dd9 211 }
fbe78f4f
AL
212}
213
002437cd
AL
214static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
215 VirtQueueElement *elem)
216{
217 uint8_t on;
218
219 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
220 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
221 exit(1);
222 }
223
224 on = ldub_p(elem->out_sg[1].iov_base);
225
226 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
227 n->promisc = on;
228 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
229 n->allmulti = on;
015cb166
AW
230 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
231 n->alluni = on;
232 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
233 n->nomulti = on;
234 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
235 n->nouni = on;
236 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
237 n->nobcast = on;
002437cd
AL
238 else
239 return VIRTIO_NET_ERR;
240
241 return VIRTIO_NET_OK;
242}
243
b6503ed9
AL
244static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
245 VirtQueueElement *elem)
246{
247 struct virtio_net_ctrl_mac mac_data;
248
249 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
250 elem->out_sg[1].iov_len < sizeof(mac_data) ||
251 elem->out_sg[2].iov_len < sizeof(mac_data))
252 return VIRTIO_NET_ERR;
253
254 n->mac_table.in_use = 0;
2d9aba39 255 n->mac_table.first_multi = 0;
8fd2a2f1
AW
256 n->mac_table.uni_overflow = 0;
257 n->mac_table.multi_overflow = 0;
b6503ed9
AL
258 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
259
260 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
261
262 if (sizeof(mac_data.entries) +
263 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
264 return VIRTIO_NET_ERR;
265
266 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
267 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
268 mac_data.entries * ETH_ALEN);
269 n->mac_table.in_use += mac_data.entries;
270 } else {
8fd2a2f1 271 n->mac_table.uni_overflow = 1;
b6503ed9
AL
272 }
273
2d9aba39
AW
274 n->mac_table.first_multi = n->mac_table.in_use;
275
b6503ed9
AL
276 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
277
278 if (sizeof(mac_data.entries) +
279 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
280 return VIRTIO_NET_ERR;
281
282 if (mac_data.entries) {
283 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
284 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
285 elem->out_sg[2].iov_base + sizeof(mac_data),
286 mac_data.entries * ETH_ALEN);
287 n->mac_table.in_use += mac_data.entries;
8fd2a2f1
AW
288 } else {
289 n->mac_table.multi_overflow = 1;
290 }
b6503ed9
AL
291 }
292
293 return VIRTIO_NET_OK;
294}
295
f21c0ed9
AL
296static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
297 VirtQueueElement *elem)
298{
299 uint16_t vid;
300
301 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
302 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
303 return VIRTIO_NET_ERR;
304 }
305
306 vid = lduw_le_p(elem->out_sg[1].iov_base);
307
308 if (vid >= MAX_VLAN)
309 return VIRTIO_NET_ERR;
310
311 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
312 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
313 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
314 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
315 else
316 return VIRTIO_NET_ERR;
317
318 return VIRTIO_NET_OK;
319}
320
3d11d36c
AL
321static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
322{
002437cd 323 VirtIONet *n = to_virtio_net(vdev);
3d11d36c
AL
324 struct virtio_net_ctrl_hdr ctrl;
325 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
326 VirtQueueElement elem;
327
328 while (virtqueue_pop(vq, &elem)) {
329 if ((elem.in_num < 1) || (elem.out_num < 1)) {
330 fprintf(stderr, "virtio-net ctrl missing headers\n");
331 exit(1);
332 }
333
334 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
c6bb9a32 335 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
3d11d36c
AL
336 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
337 exit(1);
338 }
339
340 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
341 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
342
002437cd
AL
343 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
344 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
b6503ed9
AL
345 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
346 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
f21c0ed9
AL
347 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
348 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
002437cd 349
3d11d36c
AL
350 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
351
352 virtqueue_push(vq, &elem, sizeof(status));
353 virtio_notify(vdev, vq);
354 }
355}
356
fbe78f4f
AL
357/* RX */
358
359static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
360{
8aeff62d
MM
361 VirtIONet *n = to_virtio_net(vdev);
362
eb6b6c12 363 qemu_flush_queued_packets(&n->nic->nc);
a61d1f67
GC
364
365 /* We now have RX buffers, signal to the IO thread to break out of the
366 * select to re-poll the tap file descriptor */
367 qemu_notify_event();
fbe78f4f
AL
368}
369
eb6b6c12 370static int virtio_net_can_receive(VLANClientState *nc)
fbe78f4f 371{
eb6b6c12 372 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
cdd5cc12 373
fbe78f4f
AL
374 if (!virtio_queue_ready(n->rx_vq) ||
375 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
376 return 0;
377
cdd5cc12
MM
378 return 1;
379}
380
381static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
382{
fbe78f4f
AL
383 if (virtio_queue_empty(n->rx_vq) ||
384 (n->mergeable_rx_bufs &&
385 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
386 virtio_queue_set_notification(n->rx_vq, 1);
387 return 0;
388 }
389
390 virtio_queue_set_notification(n->rx_vq, 0);
391 return 1;
392}
393
1d41b0c1
AL
394/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
395 * it never finds out that the packets don't have valid checksums. This
396 * causes dhclient to get upset. Fedora's carried a patch for ages to
397 * fix this with Xen but it hasn't appeared in an upstream release of
398 * dhclient yet.
399 *
400 * To avoid breaking existing guests, we catch udp packets and add
401 * checksums. This is terrible but it's better than hacking the guest
402 * kernels.
403 *
404 * N.B. if we introduce a zero-copy API, this operation is no longer free so
405 * we should provide a mechanism to disable it to avoid polluting the host
406 * cache.
407 */
408static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
409 const uint8_t *buf, size_t size)
410{
411 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
412 (size > 27 && size < 1500) && /* normal sized MTU */
413 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
414 (buf[23] == 17) && /* ip.protocol == UDP */
415 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
416 /* FIXME this cast is evil */
417 net_checksum_calculate((uint8_t *)buf, size);
418 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
419 }
420}
421
fbe78f4f
AL
422static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
423{
424 int offset, i;
425
426 offset = i = 0;
427 while (offset < count && i < iovcnt) {
428 int len = MIN(iov[i].iov_len, count - offset);
429 memcpy(iov[i].iov_base, buf + offset, len);
430 offset += len;
431 i++;
432 }
433
434 return offset;
435}
436
437static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
4689f4b3 438 const void *buf, size_t size, size_t hdr_len)
fbe78f4f 439{
3f4cb3d3 440 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
fbe78f4f
AL
441 int offset = 0;
442
443 hdr->flags = 0;
444 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
445
3a330134
MM
446 if (n->has_vnet_hdr) {
447 memcpy(hdr, buf, sizeof(*hdr));
448 offset = sizeof(*hdr);
1d41b0c1 449 work_around_broken_dhclient(hdr, buf + offset, size - offset);
3a330134
MM
450 }
451
fbe78f4f
AL
452 /* We only ever receive a struct virtio_net_hdr from the tapfd,
453 * but we may be passing along a larger header to the guest.
454 */
455 iov[0].iov_base += hdr_len;
456 iov[0].iov_len -= hdr_len;
457
458 return offset;
459}
460
3831ab20
AL
461static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
462{
463 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
f21c0ed9 464 static const uint8_t vlan[] = {0x81, 0x00};
3831ab20 465 uint8_t *ptr = (uint8_t *)buf;
b6503ed9 466 int i;
3831ab20
AL
467
468 if (n->promisc)
469 return 1;
470
3a330134
MM
471 if (n->has_vnet_hdr) {
472 ptr += sizeof(struct virtio_net_hdr);
473 }
474
f21c0ed9
AL
475 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
476 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
477 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
478 return 0;
479 }
480
bbe2f399
AW
481 if (ptr[0] & 1) { // multicast
482 if (!memcmp(ptr, bcast, sizeof(bcast))) {
015cb166
AW
483 return !n->nobcast;
484 } else if (n->nomulti) {
485 return 0;
8fd2a2f1 486 } else if (n->allmulti || n->mac_table.multi_overflow) {
bbe2f399
AW
487 return 1;
488 }
2d9aba39
AW
489
490 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
491 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
492 return 1;
493 }
494 }
bbe2f399 495 } else { // unicast
015cb166
AW
496 if (n->nouni) {
497 return 0;
498 } else if (n->alluni || n->mac_table.uni_overflow) {
8fd2a2f1
AW
499 return 1;
500 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
bbe2f399
AW
501 return 1;
502 }
3831ab20 503
2d9aba39
AW
504 for (i = 0; i < n->mac_table.first_multi; i++) {
505 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
506 return 1;
507 }
508 }
b6503ed9
AL
509 }
510
3831ab20
AL
511 return 0;
512}
513
eb6b6c12 514static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
fbe78f4f 515{
eb6b6c12 516 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
fbe78f4f 517 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
4689f4b3 518 size_t hdr_len, offset, i;
fbe78f4f 519
eb6b6c12 520 if (!virtio_net_can_receive(&n->nic->nc))
cdd5cc12
MM
521 return -1;
522
523 if (!virtio_net_has_buffers(n, size))
8aeff62d 524 return 0;
fbe78f4f 525
3831ab20 526 if (!receive_filter(n, buf, size))
4f1c942b 527 return size;
3831ab20 528
fbe78f4f
AL
529 /* hdr_len refers to the header we supply to the guest */
530 hdr_len = n->mergeable_rx_bufs ?
531 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
532
533 offset = i = 0;
534
535 while (offset < size) {
536 VirtQueueElement elem;
537 int len, total;
538 struct iovec sg[VIRTQUEUE_MAX_SIZE];
539
540 len = total = 0;
541
542 if ((i != 0 && !n->mergeable_rx_bufs) ||
543 virtqueue_pop(n->rx_vq, &elem) == 0) {
544 if (i == 0)
4f1c942b 545 return -1;
fbe78f4f
AL
546 fprintf(stderr, "virtio-net truncating packet\n");
547 exit(1);
548 }
549
550 if (elem.in_num < 1) {
551 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
552 exit(1);
553 }
554
555 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
556 fprintf(stderr, "virtio-net header not in first element\n");
557 exit(1);
558 }
559
560 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
561
562 if (i == 0) {
563 if (n->mergeable_rx_bufs)
564 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
565
566 offset += receive_header(n, sg, elem.in_num,
567 buf + offset, size - offset, hdr_len);
568 total += hdr_len;
569 }
570
571 /* copy in packet. ugh */
572 len = iov_fill(sg, elem.in_num,
573 buf + offset, size - offset);
574 total += len;
575
576 /* signal other side */
577 virtqueue_fill(n->rx_vq, &elem, total, i++);
578
579 offset += len;
580 }
581
582 if (mhdr)
583 mhdr->num_buffers = i;
584
585 virtqueue_flush(n->rx_vq, i);
586 virtio_notify(&n->vdev, n->rx_vq);
4f1c942b
MM
587
588 return size;
fbe78f4f
AL
589}
590
6243375f
MM
591static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
592
eb6b6c12 593static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
6243375f 594{
eb6b6c12 595 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
6243375f
MM
596
597 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
598 virtio_notify(&n->vdev, n->tx_vq);
599
600 n->async_tx.elem.out_num = n->async_tx.len = 0;
601
602 virtio_queue_set_notification(n->tx_vq, 1);
603 virtio_net_flush_tx(n, n->tx_vq);
604}
605
fbe78f4f
AL
606/* TX */
607static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
608{
609 VirtQueueElement elem;
fbe78f4f
AL
610
611 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
612 return;
613
6243375f
MM
614 if (n->async_tx.elem.out_num) {
615 virtio_queue_set_notification(n->tx_vq, 0);
616 return;
617 }
618
fbe78f4f 619 while (virtqueue_pop(vq, &elem)) {
6243375f 620 ssize_t ret, len = 0;
fbe78f4f
AL
621 unsigned int out_num = elem.out_num;
622 struct iovec *out_sg = &elem.out_sg[0];
623 unsigned hdr_len;
624
625 /* hdr_len refers to the header received from the guest */
626 hdr_len = n->mergeable_rx_bufs ?
627 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
628 sizeof(struct virtio_net_hdr);
629
630 if (out_num < 1 || out_sg->iov_len != hdr_len) {
631 fprintf(stderr, "virtio-net header not in first element\n");
632 exit(1);
633 }
634
635 /* ignore the header if GSO is not supported */
3a330134 636 if (!n->has_vnet_hdr) {
fbe78f4f
AL
637 out_num--;
638 out_sg++;
639 len += hdr_len;
640 } else if (n->mergeable_rx_bufs) {
641 /* tapfd expects a struct virtio_net_hdr */
642 hdr_len -= sizeof(struct virtio_net_hdr);
643 out_sg->iov_len -= hdr_len;
644 len += hdr_len;
645 }
646
eb6b6c12 647 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
6243375f
MM
648 virtio_net_tx_complete);
649 if (ret == 0) {
650 virtio_queue_set_notification(n->tx_vq, 0);
651 n->async_tx.elem = elem;
652 n->async_tx.len = len;
653 return;
654 }
655
656 len += ret;
fbe78f4f
AL
657
658 virtqueue_push(vq, &elem, len);
659 virtio_notify(&n->vdev, vq);
660 }
661}
662
663static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
664{
665 VirtIONet *n = to_virtio_net(vdev);
666
667 if (n->tx_timer_active) {
668 virtio_queue_set_notification(vq, 1);
669 qemu_del_timer(n->tx_timer);
670 n->tx_timer_active = 0;
671 virtio_net_flush_tx(n, vq);
672 } else {
673 qemu_mod_timer(n->tx_timer,
674 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
675 n->tx_timer_active = 1;
676 virtio_queue_set_notification(vq, 0);
677 }
678}
679
680static void virtio_net_tx_timer(void *opaque)
681{
682 VirtIONet *n = opaque;
683
684 n->tx_timer_active = 0;
685
686 /* Just in case the driver is not ready on more */
687 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
688 return;
689
690 virtio_queue_set_notification(n->tx_vq, 1);
691 virtio_net_flush_tx(n, n->tx_vq);
692}
693
694static void virtio_net_save(QEMUFile *f, void *opaque)
695{
696 VirtIONet *n = opaque;
697
698 virtio_save(&n->vdev, f);
699
79674068 700 qemu_put_buffer(f, n->mac, ETH_ALEN);
fbe78f4f 701 qemu_put_be32(f, n->tx_timer_active);
e46cb38f 702 qemu_put_be32(f, n->mergeable_rx_bufs);
9d6271b8 703 qemu_put_be16(f, n->status);
f10c592e
AW
704 qemu_put_byte(f, n->promisc);
705 qemu_put_byte(f, n->allmulti);
b6503ed9
AL
706 qemu_put_be32(f, n->mac_table.in_use);
707 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
f21c0ed9 708 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
3a330134 709 qemu_put_be32(f, n->has_vnet_hdr);
8fd2a2f1
AW
710 qemu_put_byte(f, n->mac_table.multi_overflow);
711 qemu_put_byte(f, n->mac_table.uni_overflow);
015cb166
AW
712 qemu_put_byte(f, n->alluni);
713 qemu_put_byte(f, n->nomulti);
714 qemu_put_byte(f, n->nouni);
715 qemu_put_byte(f, n->nobcast);
0ce0e8f4 716 qemu_put_byte(f, n->has_ufo);
fbe78f4f
AL
717}
718
719static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
720{
721 VirtIONet *n = opaque;
2d9aba39 722 int i;
fbe78f4f 723
9d6271b8 724 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
fbe78f4f
AL
725 return -EINVAL;
726
727 virtio_load(&n->vdev, f);
728
79674068 729 qemu_get_buffer(f, n->mac, ETH_ALEN);
fbe78f4f 730 n->tx_timer_active = qemu_get_be32(f);
e46cb38f 731 n->mergeable_rx_bufs = qemu_get_be32(f);
fbe78f4f 732
9d6271b8
AL
733 if (version_id >= 3)
734 n->status = qemu_get_be16(f);
735
002437cd 736 if (version_id >= 4) {
f10c592e
AW
737 if (version_id < 8) {
738 n->promisc = qemu_get_be32(f);
739 n->allmulti = qemu_get_be32(f);
740 } else {
741 n->promisc = qemu_get_byte(f);
742 n->allmulti = qemu_get_byte(f);
743 }
002437cd
AL
744 }
745
b6503ed9
AL
746 if (version_id >= 5) {
747 n->mac_table.in_use = qemu_get_be32(f);
748 /* MAC_TABLE_ENTRIES may be different from the saved image */
749 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
750 qemu_get_buffer(f, n->mac_table.macs,
751 n->mac_table.in_use * ETH_ALEN);
752 } else if (n->mac_table.in_use) {
753 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
8fd2a2f1 754 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
b6503ed9
AL
755 n->mac_table.in_use = 0;
756 }
757 }
758
f21c0ed9
AL
759 if (version_id >= 6)
760 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
761
3a330134
MM
762 if (version_id >= 7) {
763 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
764 qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
765 return -1;
766 }
767
768 if (n->has_vnet_hdr) {
eb6b6c12
MM
769 tap_using_vnet_hdr(n->nic->nc.peer, 1);
770 tap_set_offload(n->nic->nc.peer,
f5436dd9
MM
771 (n->vdev.features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
772 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
773 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
6c9f58ba
SS
774 (n->vdev.features >> VIRTIO_NET_F_GUEST_ECN) & 1,
775 (n->vdev.features >> VIRTIO_NET_F_GUEST_UFO) & 1);
3a330134 776 }
6c042c16
AW
777 }
778
8fd2a2f1
AW
779 if (version_id >= 9) {
780 n->mac_table.multi_overflow = qemu_get_byte(f);
781 n->mac_table.uni_overflow = qemu_get_byte(f);
782 }
783
015cb166
AW
784 if (version_id >= 10) {
785 n->alluni = qemu_get_byte(f);
786 n->nomulti = qemu_get_byte(f);
787 n->nouni = qemu_get_byte(f);
788 n->nobcast = qemu_get_byte(f);
789 }
790
0ce0e8f4
MM
791 if (version_id >= 11) {
792 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
793 qemu_error("virtio-net: saved image requires TUN_F_UFO support\n");
794 return -1;
795 }
796 }
797
2d9aba39
AW
798 /* Find the first multicast entry in the saved MAC filter */
799 for (i = 0; i < n->mac_table.in_use; i++) {
800 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
801 break;
802 }
803 }
804 n->mac_table.first_multi = i;
805
fbe78f4f
AL
806 if (n->tx_timer_active) {
807 qemu_mod_timer(n->tx_timer,
808 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
809 }
810
811 return 0;
812}
813
eb6b6c12 814static void virtio_net_cleanup(VLANClientState *nc)
b946a153 815{
eb6b6c12 816 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
b946a153 817
eb6b6c12 818 n->nic = NULL;
b946a153
AL
819}
820
eb6b6c12
MM
821static NetClientInfo net_virtio_info = {
822 .type = NET_CLIENT_TYPE_NIC,
823 .size = sizeof(NICState),
824 .can_receive = virtio_net_can_receive,
825 .receive = virtio_net_receive,
826 .cleanup = virtio_net_cleanup,
827 .link_status_changed = virtio_net_set_link_status,
828};
829
97b15621 830VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
fbe78f4f
AL
831{
832 VirtIONet *n;
833 static int virtio_net_id;
834
53c25cea
PB
835 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
836 sizeof(struct virtio_net_config),
837 sizeof(VirtIONet));
fbe78f4f 838
0f03eca6
AL
839 n->vdev.get_config = virtio_net_get_config;
840 n->vdev.set_config = virtio_net_set_config;
fbe78f4f
AL
841 n->vdev.get_features = virtio_net_get_features;
842 n->vdev.set_features = virtio_net_set_features;
8eca6b1b 843 n->vdev.bad_features = virtio_net_bad_features;
002437cd 844 n->vdev.reset = virtio_net_reset;
fbe78f4f
AL
845 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
846 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
4ffb17f5 847 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
97b15621 848 qemu_macaddr_default_if_unset(&conf->macaddr);
3cbe04c4 849 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
554c97dd 850 n->status = VIRTIO_NET_S_LINK_UP;
fbe78f4f 851
eb6b6c12
MM
852 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
853
854 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
96d5e201 855
fbe78f4f
AL
856 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
857 n->tx_timer_active = 0;
858 n->mergeable_rx_bufs = 0;
002437cd 859 n->promisc = 1; /* for compatibility */
fbe78f4f 860
b6503ed9 861 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
b6503ed9 862
f21c0ed9 863 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
f21c0ed9 864
9d6271b8 865 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
fbe78f4f 866 virtio_net_save, virtio_net_load, n);
cf21e106 867
53c25cea 868 return &n->vdev;
cf21e106 869}
97b15621
GH
870
871void virtio_net_exit(VirtIODevice *vdev)
872{
873 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
874
eb6b6c12 875 qemu_purge_queued_packets(&n->nic->nc);
97b15621
GH
876
877 unregister_savevm("virtio-net", n);
878
879 qemu_free(n->mac_table.macs);
880 qemu_free(n->vlans);
881
882 qemu_del_timer(n->tx_timer);
883 qemu_free_timer(n->tx_timer);
884
885 virtio_cleanup(&n->vdev);
eb6b6c12 886 qemu_del_vlan_client(&n->nic->nc);
97b15621 887}