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