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