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