]>
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" |
a8ed73f7 | 17 | #include "net/tap.h" |
2f792016 | 18 | #include "qemu-error.h" |
fbe78f4f AL |
19 | #include "qemu-timer.h" |
20 | #include "virtio-net.h" | |
21 | ||
0ce0e8f4 | 22 | #define VIRTIO_NET_VM_VERSION 11 |
b6503ed9 | 23 | |
4ffb17f5 | 24 | #define MAC_TABLE_ENTRIES 64 |
f21c0ed9 | 25 | #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ |
9d6271b8 | 26 | |
fbe78f4f AL |
27 | typedef struct VirtIONet |
28 | { | |
29 | VirtIODevice vdev; | |
79674068 | 30 | uint8_t mac[ETH_ALEN]; |
554c97dd | 31 | uint16_t status; |
fbe78f4f AL |
32 | VirtQueue *rx_vq; |
33 | VirtQueue *tx_vq; | |
3d11d36c | 34 | VirtQueue *ctrl_vq; |
eb6b6c12 | 35 | NICState *nic; |
fbe78f4f AL |
36 | QEMUTimer *tx_timer; |
37 | int tx_timer_active; | |
3a330134 | 38 | uint32_t has_vnet_hdr; |
0ce0e8f4 | 39 | uint8_t has_ufo; |
6243375f MM |
40 | struct { |
41 | VirtQueueElement elem; | |
42 | ssize_t len; | |
43 | } async_tx; | |
fbe78f4f | 44 | int mergeable_rx_bufs; |
f10c592e AW |
45 | uint8_t promisc; |
46 | uint8_t allmulti; | |
015cb166 AW |
47 | uint8_t alluni; |
48 | uint8_t nomulti; | |
49 | uint8_t nouni; | |
50 | uint8_t nobcast; | |
b6503ed9 AL |
51 | struct { |
52 | int in_use; | |
2d9aba39 | 53 | int first_multi; |
8fd2a2f1 AW |
54 | uint8_t multi_overflow; |
55 | uint8_t uni_overflow; | |
b6503ed9 AL |
56 | uint8_t *macs; |
57 | } mac_table; | |
f21c0ed9 | 58 | uint32_t *vlans; |
fbe78f4f AL |
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 | ||
0f03eca6 | 70 | static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) |
fbe78f4f AL |
71 | { |
72 | VirtIONet *n = to_virtio_net(vdev); | |
73 | struct virtio_net_config netcfg; | |
74 | ||
554c97dd | 75 | netcfg.status = n->status; |
79674068 | 76 | memcpy(netcfg.mac, n->mac, ETH_ALEN); |
fbe78f4f AL |
77 | memcpy(config, &netcfg, sizeof(netcfg)); |
78 | } | |
79 | ||
0f03eca6 AL |
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 | ||
79674068 AL |
87 | if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) { |
88 | memcpy(n->mac, netcfg.mac, ETH_ALEN); | |
eb6b6c12 | 89 | qemu_format_nic_info_str(&n->nic->nc, n->mac); |
0f03eca6 AL |
90 | } |
91 | } | |
92 | ||
eb6b6c12 | 93 | static void virtio_net_set_link_status(VLANClientState *nc) |
554c97dd | 94 | { |
eb6b6c12 | 95 | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
554c97dd AL |
96 | uint16_t old_status = n->status; |
97 | ||
eb6b6c12 | 98 | if (nc->link_down) |
554c97dd AL |
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 | ||
002437cd AL |
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; | |
015cb166 AW |
114 | n->alluni = 0; |
115 | n->nomulti = 0; | |
116 | n->nouni = 0; | |
117 | n->nobcast = 0; | |
b6503ed9 | 118 | |
f21c0ed9 | 119 | /* Flush any MAC and VLAN filter table state */ |
b6503ed9 | 120 | n->mac_table.in_use = 0; |
2d9aba39 | 121 | n->mac_table.first_multi = 0; |
8fd2a2f1 AW |
122 | n->mac_table.multi_overflow = 0; |
123 | n->mac_table.uni_overflow = 0; | |
b6503ed9 | 124 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
f21c0ed9 | 125 | memset(n->vlans, 0, MAX_VLAN >> 3); |
002437cd AL |
126 | } |
127 | ||
3a330134 MM |
128 | static int peer_has_vnet_hdr(VirtIONet *n) |
129 | { | |
eb6b6c12 | 130 | if (!n->nic->nc.peer) |
3a330134 MM |
131 | return 0; |
132 | ||
665a3b07 | 133 | if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) |
3a330134 MM |
134 | return 0; |
135 | ||
eb6b6c12 | 136 | n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer); |
3a330134 MM |
137 | |
138 | return n->has_vnet_hdr; | |
139 | } | |
140 | ||
0ce0e8f4 MM |
141 | static int peer_has_ufo(VirtIONet *n) |
142 | { | |
143 | if (!peer_has_vnet_hdr(n)) | |
144 | return 0; | |
145 | ||
eb6b6c12 | 146 | n->has_ufo = tap_has_ufo(n->nic->nc.peer); |
0ce0e8f4 MM |
147 | |
148 | return n->has_ufo; | |
149 | } | |
150 | ||
8172539d | 151 | static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features) |
fbe78f4f | 152 | { |
3a330134 | 153 | VirtIONet *n = to_virtio_net(vdev); |
fbe78f4f | 154 | |
c9f79a3f MT |
155 | features |= (1 << VIRTIO_NET_F_MAC); |
156 | ||
3a330134 | 157 | if (peer_has_vnet_hdr(n)) { |
eb6b6c12 | 158 | tap_using_vnet_hdr(n->nic->nc.peer, 1); |
8172539d MT |
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 | } | |
3a330134 | 170 | |
8172539d MT |
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); | |
3a330134 MM |
174 | } |
175 | ||
fbe78f4f AL |
176 | return features; |
177 | } | |
178 | ||
8eca6b1b AL |
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); | |
184bd048 DK |
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); | |
8eca6b1b | 190 | |
8172539d | 191 | return features; |
8eca6b1b AL |
192 | } |
193 | ||
fbe78f4f AL |
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)); | |
f5436dd9 MM |
199 | |
200 | if (n->has_vnet_hdr) { | |
eb6b6c12 | 201 | tap_set_offload(n->nic->nc.peer, |
f5436dd9 MM |
202 | (features >> VIRTIO_NET_F_GUEST_CSUM) & 1, |
203 | (features >> VIRTIO_NET_F_GUEST_TSO4) & 1, | |
204 | (features >> VIRTIO_NET_F_GUEST_TSO6) & 1, | |
6c9f58ba SS |
205 | (features >> VIRTIO_NET_F_GUEST_ECN) & 1, |
206 | (features >> VIRTIO_NET_F_GUEST_UFO) & 1); | |
f5436dd9 | 207 | } |
fbe78f4f AL |
208 | } |
209 | ||
002437cd AL |
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; | |
015cb166 AW |
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; | |
002437cd AL |
234 | else |
235 | return VIRTIO_NET_ERR; | |
236 | ||
237 | return VIRTIO_NET_OK; | |
238 | } | |
239 | ||
b6503ed9 AL |
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; | |
2d9aba39 | 251 | n->mac_table.first_multi = 0; |
8fd2a2f1 AW |
252 | n->mac_table.uni_overflow = 0; |
253 | n->mac_table.multi_overflow = 0; | |
b6503ed9 AL |
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 { | |
8fd2a2f1 | 267 | n->mac_table.uni_overflow = 1; |
b6503ed9 AL |
268 | } |
269 | ||
2d9aba39 AW |
270 | n->mac_table.first_multi = n->mac_table.in_use; |
271 | ||
b6503ed9 AL |
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; | |
8fd2a2f1 AW |
284 | } else { |
285 | n->mac_table.multi_overflow = 1; | |
286 | } | |
b6503ed9 AL |
287 | } |
288 | ||
289 | return VIRTIO_NET_OK; | |
290 | } | |
291 | ||
f21c0ed9 AL |
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 | ||
3d11d36c AL |
317 | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
318 | { | |
002437cd | 319 | VirtIONet *n = to_virtio_net(vdev); |
3d11d36c AL |
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) || | |
c6bb9a32 | 331 | elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) { |
3d11d36c AL |
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 | ||
002437cd AL |
339 | if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE) |
340 | status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem); | |
b6503ed9 AL |
341 | else if (ctrl.class == VIRTIO_NET_CTRL_MAC) |
342 | status = virtio_net_handle_mac(n, ctrl.cmd, &elem); | |
f21c0ed9 AL |
343 | else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) |
344 | status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem); | |
002437cd | 345 | |
3d11d36c AL |
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 | ||
fbe78f4f AL |
353 | /* RX */ |
354 | ||
355 | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) | |
356 | { | |
8aeff62d MM |
357 | VirtIONet *n = to_virtio_net(vdev); |
358 | ||
eb6b6c12 | 359 | qemu_flush_queued_packets(&n->nic->nc); |
a61d1f67 GC |
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(); | |
fbe78f4f AL |
364 | } |
365 | ||
eb6b6c12 | 366 | static int virtio_net_can_receive(VLANClientState *nc) |
fbe78f4f | 367 | { |
eb6b6c12 | 368 | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
cdd5cc12 | 369 | |
fbe78f4f AL |
370 | if (!virtio_queue_ready(n->rx_vq) || |
371 | !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
372 | return 0; | |
373 | ||
cdd5cc12 MM |
374 | return 1; |
375 | } | |
376 | ||
377 | static int virtio_net_has_buffers(VirtIONet *n, int bufsize) | |
378 | { | |
fbe78f4f AL |
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); | |
06b12970 TL |
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; | |
fbe78f4f AL |
392 | } |
393 | ||
394 | virtio_queue_set_notification(n->rx_vq, 0); | |
395 | return 1; | |
396 | } | |
397 | ||
1d41b0c1 AL |
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 | ||
fbe78f4f AL |
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, | |
4689f4b3 | 442 | const void *buf, size_t size, size_t hdr_len) |
fbe78f4f | 443 | { |
3f4cb3d3 | 444 | struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base; |
fbe78f4f AL |
445 | int offset = 0; |
446 | ||
447 | hdr->flags = 0; | |
448 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
449 | ||
3a330134 MM |
450 | if (n->has_vnet_hdr) { |
451 | memcpy(hdr, buf, sizeof(*hdr)); | |
452 | offset = sizeof(*hdr); | |
1d41b0c1 | 453 | work_around_broken_dhclient(hdr, buf + offset, size - offset); |
3a330134 MM |
454 | } |
455 | ||
fbe78f4f AL |
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 | ||
3831ab20 AL |
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}; | |
f21c0ed9 | 468 | static const uint8_t vlan[] = {0x81, 0x00}; |
3831ab20 | 469 | uint8_t *ptr = (uint8_t *)buf; |
b6503ed9 | 470 | int i; |
3831ab20 AL |
471 | |
472 | if (n->promisc) | |
473 | return 1; | |
474 | ||
3a330134 MM |
475 | if (n->has_vnet_hdr) { |
476 | ptr += sizeof(struct virtio_net_hdr); | |
477 | } | |
478 | ||
f21c0ed9 AL |
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 | ||
bbe2f399 AW |
485 | if (ptr[0] & 1) { // multicast |
486 | if (!memcmp(ptr, bcast, sizeof(bcast))) { | |
015cb166 AW |
487 | return !n->nobcast; |
488 | } else if (n->nomulti) { | |
489 | return 0; | |
8fd2a2f1 | 490 | } else if (n->allmulti || n->mac_table.multi_overflow) { |
bbe2f399 AW |
491 | return 1; |
492 | } | |
2d9aba39 AW |
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 | } | |
bbe2f399 | 499 | } else { // unicast |
015cb166 AW |
500 | if (n->nouni) { |
501 | return 0; | |
502 | } else if (n->alluni || n->mac_table.uni_overflow) { | |
8fd2a2f1 AW |
503 | return 1; |
504 | } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { | |
bbe2f399 AW |
505 | return 1; |
506 | } | |
3831ab20 | 507 | |
2d9aba39 AW |
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 | } | |
b6503ed9 AL |
513 | } |
514 | ||
3831ab20 AL |
515 | return 0; |
516 | } | |
517 | ||
eb6b6c12 | 518 | static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
fbe78f4f | 519 | { |
eb6b6c12 | 520 | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
fbe78f4f | 521 | struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL; |
4689f4b3 | 522 | size_t hdr_len, offset, i; |
fbe78f4f | 523 | |
eb6b6c12 | 524 | if (!virtio_net_can_receive(&n->nic->nc)) |
cdd5cc12 MM |
525 | return -1; |
526 | ||
527 | if (!virtio_net_has_buffers(n, size)) | |
8aeff62d | 528 | return 0; |
fbe78f4f | 529 | |
3831ab20 | 530 | if (!receive_filter(n, buf, size)) |
4f1c942b | 531 | return size; |
3831ab20 | 532 | |
fbe78f4f AL |
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 | ||
22c253d9 | 544 | total = 0; |
fbe78f4f AL |
545 | |
546 | if ((i != 0 && !n->mergeable_rx_bufs) || | |
547 | virtqueue_pop(n->rx_vq, &elem) == 0) { | |
548 | if (i == 0) | |
4f1c942b | 549 | return -1; |
fbe78f4f AL |
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); | |
4f1c942b MM |
591 | |
592 | return size; | |
fbe78f4f AL |
593 | } |
594 | ||
6243375f MM |
595 | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq); |
596 | ||
eb6b6c12 | 597 | static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len) |
6243375f | 598 | { |
eb6b6c12 | 599 | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
6243375f MM |
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 | ||
fbe78f4f AL |
610 | /* TX */ |
611 | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq) | |
612 | { | |
613 | VirtQueueElement elem; | |
fbe78f4f AL |
614 | |
615 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
616 | return; | |
617 | ||
6243375f MM |
618 | if (n->async_tx.elem.out_num) { |
619 | virtio_queue_set_notification(n->tx_vq, 0); | |
620 | return; | |
621 | } | |
622 | ||
fbe78f4f | 623 | while (virtqueue_pop(vq, &elem)) { |
6243375f | 624 | ssize_t ret, len = 0; |
fbe78f4f AL |
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 */ | |
3a330134 | 640 | if (!n->has_vnet_hdr) { |
fbe78f4f AL |
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 | ||
eb6b6c12 | 651 | ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num, |
6243375f MM |
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; | |
fbe78f4f AL |
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 | ||
79674068 | 704 | qemu_put_buffer(f, n->mac, ETH_ALEN); |
fbe78f4f | 705 | qemu_put_be32(f, n->tx_timer_active); |
e46cb38f | 706 | qemu_put_be32(f, n->mergeable_rx_bufs); |
9d6271b8 | 707 | qemu_put_be16(f, n->status); |
f10c592e AW |
708 | qemu_put_byte(f, n->promisc); |
709 | qemu_put_byte(f, n->allmulti); | |
b6503ed9 AL |
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); | |
f21c0ed9 | 712 | qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); |
3a330134 | 713 | qemu_put_be32(f, n->has_vnet_hdr); |
8fd2a2f1 AW |
714 | qemu_put_byte(f, n->mac_table.multi_overflow); |
715 | qemu_put_byte(f, n->mac_table.uni_overflow); | |
015cb166 AW |
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); | |
0ce0e8f4 | 720 | qemu_put_byte(f, n->has_ufo); |
fbe78f4f AL |
721 | } |
722 | ||
723 | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) | |
724 | { | |
725 | VirtIONet *n = opaque; | |
2d9aba39 | 726 | int i; |
fbe78f4f | 727 | |
9d6271b8 | 728 | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
fbe78f4f AL |
729 | return -EINVAL; |
730 | ||
731 | virtio_load(&n->vdev, f); | |
732 | ||
79674068 | 733 | qemu_get_buffer(f, n->mac, ETH_ALEN); |
fbe78f4f | 734 | n->tx_timer_active = qemu_get_be32(f); |
e46cb38f | 735 | n->mergeable_rx_bufs = qemu_get_be32(f); |
fbe78f4f | 736 | |
9d6271b8 AL |
737 | if (version_id >= 3) |
738 | n->status = qemu_get_be16(f); | |
739 | ||
002437cd | 740 | if (version_id >= 4) { |
f10c592e AW |
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 | } | |
002437cd AL |
748 | } |
749 | ||
b6503ed9 AL |
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); | |
8fd2a2f1 | 758 | n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1; |
b6503ed9 AL |
759 | n->mac_table.in_use = 0; |
760 | } | |
761 | } | |
762 | ||
f21c0ed9 AL |
763 | if (version_id >= 6) |
764 | qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); | |
765 | ||
3a330134 MM |
766 | if (version_id >= 7) { |
767 | if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) { | |
1ecda02b | 768 | error_report("virtio-net: saved image requires vnet_hdr=on"); |
3a330134 MM |
769 | return -1; |
770 | } | |
771 | ||
772 | if (n->has_vnet_hdr) { | |
eb6b6c12 MM |
773 | tap_using_vnet_hdr(n->nic->nc.peer, 1); |
774 | tap_set_offload(n->nic->nc.peer, | |
704a76fc MT |
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); | |
3a330134 | 780 | } |
6c042c16 AW |
781 | } |
782 | ||
8fd2a2f1 AW |
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 | ||
015cb166 AW |
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 | ||
0ce0e8f4 MM |
795 | if (version_id >= 11) { |
796 | if (qemu_get_byte(f) && !peer_has_ufo(n)) { | |
1ecda02b | 797 | error_report("virtio-net: saved image requires TUN_F_UFO support"); |
0ce0e8f4 MM |
798 | return -1; |
799 | } | |
800 | } | |
801 | ||
2d9aba39 AW |
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 | ||
fbe78f4f AL |
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 | ||
eb6b6c12 | 818 | static void virtio_net_cleanup(VLANClientState *nc) |
b946a153 | 819 | { |
eb6b6c12 | 820 | VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque; |
b946a153 | 821 | |
eb6b6c12 | 822 | n->nic = NULL; |
b946a153 AL |
823 | } |
824 | ||
eb6b6c12 MM |
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 | ||
97b15621 | 834 | VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf) |
fbe78f4f AL |
835 | { |
836 | VirtIONet *n; | |
837 | static int virtio_net_id; | |
838 | ||
53c25cea PB |
839 | n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET, |
840 | sizeof(struct virtio_net_config), | |
841 | sizeof(VirtIONet)); | |
fbe78f4f | 842 | |
0f03eca6 AL |
843 | n->vdev.get_config = virtio_net_get_config; |
844 | n->vdev.set_config = virtio_net_set_config; | |
fbe78f4f AL |
845 | n->vdev.get_features = virtio_net_get_features; |
846 | n->vdev.set_features = virtio_net_set_features; | |
8eca6b1b | 847 | n->vdev.bad_features = virtio_net_bad_features; |
002437cd | 848 | n->vdev.reset = virtio_net_reset; |
fbe78f4f AL |
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); | |
4ffb17f5 | 851 | n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl); |
97b15621 | 852 | qemu_macaddr_default_if_unset(&conf->macaddr); |
3cbe04c4 | 853 | memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac)); |
554c97dd | 854 | n->status = VIRTIO_NET_S_LINK_UP; |
fbe78f4f | 855 | |
eb6b6c12 MM |
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); | |
96d5e201 | 859 | |
fbe78f4f AL |
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; | |
002437cd | 863 | n->promisc = 1; /* for compatibility */ |
fbe78f4f | 864 | |
b6503ed9 | 865 | n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN); |
b6503ed9 | 866 | |
f21c0ed9 | 867 | n->vlans = qemu_mallocz(MAX_VLAN >> 3); |
f21c0ed9 | 868 | |
9d6271b8 | 869 | register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION, |
fbe78f4f | 870 | virtio_net_save, virtio_net_load, n); |
cf21e106 | 871 | |
53c25cea | 872 | return &n->vdev; |
cf21e106 | 873 | } |
97b15621 GH |
874 | |
875 | void virtio_net_exit(VirtIODevice *vdev) | |
876 | { | |
877 | VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev); | |
878 | ||
eb6b6c12 | 879 | qemu_purge_queued_packets(&n->nic->nc); |
97b15621 GH |
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); | |
eb6b6c12 | 890 | qemu_del_vlan_client(&n->nic->nc); |
97b15621 | 891 | } |