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