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