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