blob: 02d9180f743b46d49f6c8129aa569d4182934df0 [file] [log] [blame]
aliguorifbe78f42008-12-17 19:13:11 +00001/*
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"
Mark McLoughlin7200ac32009-10-22 17:49:03 +010016#include "net/checksum.h"
Mark McLoughlina8ed73f2009-10-22 17:49:05 +010017#include "net/tap.h"
aliguorifbe78f42008-12-17 19:13:11 +000018#include "qemu-timer.h"
19#include "virtio-net.h"
20
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010021#define VIRTIO_NET_VM_VERSION 11
aliguorib6503ed2009-02-05 22:36:28 +000022
Alex Williamson4ffb17f2009-06-05 14:47:23 -060023#define MAC_TABLE_ENTRIES 64
aliguorif21c0ed2009-02-05 22:36:32 +000024#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
aliguori9d6271b2009-02-05 22:36:04 +000025
aliguorifbe78f42008-12-17 19:13:11 +000026typedef struct VirtIONet
27{
28 VirtIODevice vdev;
aliguori79674062009-02-05 22:36:12 +000029 uint8_t mac[ETH_ALEN];
aliguori554c97d2009-01-08 19:46:33 +000030 uint16_t status;
aliguorifbe78f42008-12-17 19:13:11 +000031 VirtQueue *rx_vq;
32 VirtQueue *tx_vq;
aliguori3d11d362009-02-05 22:36:16 +000033 VirtQueue *ctrl_vq;
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000034 NICState *nic;
aliguorifbe78f42008-12-17 19:13:11 +000035 QEMUTimer *tx_timer;
36 int tx_timer_active;
Mark McLoughlin3a330132009-10-22 17:43:45 +010037 uint32_t has_vnet_hdr;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010038 uint8_t has_ufo;
Mark McLoughlin62433752009-06-18 18:21:36 +010039 struct {
40 VirtQueueElement elem;
41 ssize_t len;
42 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000043 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060044 uint8_t promisc;
45 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060046 uint8_t alluni;
47 uint8_t nomulti;
48 uint8_t nouni;
49 uint8_t nobcast;
aliguorib6503ed2009-02-05 22:36:28 +000050 struct {
51 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060052 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060053 uint8_t multi_overflow;
54 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000055 uint8_t *macs;
56 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000057 uint32_t *vlans;
aliguorifbe78f42008-12-17 19:13:11 +000058} VirtIONet;
59
60/* TODO
61 * - we could suppress RX interrupt if we were so inclined.
62 */
63
64static VirtIONet *to_virtio_net(VirtIODevice *vdev)
65{
66 return (VirtIONet *)vdev;
67}
68
aliguori0f03eca2009-02-05 22:36:08 +000069static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000070{
71 VirtIONet *n = to_virtio_net(vdev);
72 struct virtio_net_config netcfg;
73
aliguori554c97d2009-01-08 19:46:33 +000074 netcfg.status = n->status;
aliguori79674062009-02-05 22:36:12 +000075 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000076 memcpy(config, &netcfg, sizeof(netcfg));
77}
78
aliguori0f03eca2009-02-05 22:36:08 +000079static 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
aliguori79674062009-02-05 22:36:12 +000086 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
87 memcpy(n->mac, netcfg.mac, ETH_ALEN);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000088 qemu_format_nic_info_str(&n->nic->nc, n->mac);
aliguori0f03eca2009-02-05 22:36:08 +000089 }
90}
91
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000092static void virtio_net_set_link_status(VLANClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +000093{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000094 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +000095 uint16_t old_status = n->status;
96
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000097 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +000098 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
aliguori002437c2009-02-05 22:36:20 +0000106static 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;
Alex Williamson015cb162009-06-05 14:47:18 -0600113 n->alluni = 0;
114 n->nomulti = 0;
115 n->nouni = 0;
116 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000117
aliguorif21c0ed2009-02-05 22:36:32 +0000118 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000119 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600120 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600121 n->mac_table.multi_overflow = 0;
122 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000123 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000124 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000125}
126
Mark McLoughlin3a330132009-10-22 17:43:45 +0100127static int peer_has_vnet_hdr(VirtIONet *n)
128{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000129 if (!n->nic->nc.peer)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100130 return 0;
131
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000132 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100133 return 0;
134
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000135 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100136
137 return n->has_vnet_hdr;
138}
139
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100140static int peer_has_ufo(VirtIONet *n)
141{
142 if (!peer_has_vnet_hdr(n))
143 return 0;
144
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000145 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100146
147 return n->has_ufo;
148}
149
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200150static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000151{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100152 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000153
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200154 features |= (1 << VIRTIO_NET_F_MAC);
155
Mark McLoughlin3a330132009-10-22 17:43:45 +0100156 if (peer_has_vnet_hdr(n)) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000157 tap_using_vnet_hdr(n->nic->nc.peer, 1);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200158 } 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);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100163
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200164 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 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100169
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200170 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);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100173 }
174
aliguorifbe78f42008-12-17 19:13:11 +0000175 return features;
176}
177
aliguori8eca6b12009-04-05 17:40:08 +0000178static 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);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500185 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);
aliguori8eca6b12009-04-05 17:40:08 +0000189
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200190 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000191}
192
aliguorifbe78f42008-12-17 19:13:11 +0000193static 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));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100198
199 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000200 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100201 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
202 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
203 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100204 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
205 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100206 }
aliguorifbe78f42008-12-17 19:13:11 +0000207}
208
aliguori002437c2009-02-05 22:36:20 +0000209static 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;
Alex Williamson015cb162009-06-05 14:47:18 -0600225 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;
aliguori002437c2009-02-05 22:36:20 +0000233 else
234 return VIRTIO_NET_ERR;
235
236 return VIRTIO_NET_OK;
237}
238
aliguorib6503ed2009-02-05 22:36:28 +0000239static 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;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600250 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600251 n->mac_table.uni_overflow = 0;
252 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000253 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 {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600266 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000267 }
268
Alex Williamson2d9aba32009-06-05 14:47:13 -0600269 n->mac_table.first_multi = n->mac_table.in_use;
270
aliguorib6503ed2009-02-05 22:36:28 +0000271 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;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600283 } else {
284 n->mac_table.multi_overflow = 1;
285 }
aliguorib6503ed2009-02-05 22:36:28 +0000286 }
287
288 return VIRTIO_NET_OK;
289}
290
aliguorif21c0ed2009-02-05 22:36:32 +0000291static 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
aliguori3d11d362009-02-05 22:36:16 +0000316static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
317{
aliguori002437c2009-02-05 22:36:20 +0000318 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000319 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) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000330 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000331 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
aliguori002437c2009-02-05 22:36:20 +0000338 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
339 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000340 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
341 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000342 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
343 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000344
aliguori3d11d362009-02-05 22:36:16 +0000345 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
aliguorifbe78f42008-12-17 19:13:11 +0000352/* RX */
353
354static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
355{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100356 VirtIONet *n = to_virtio_net(vdev);
357
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000358 qemu_flush_queued_packets(&n->nic->nc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400359
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();
aliguorifbe78f42008-12-17 19:13:11 +0000363}
364
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000365static int virtio_net_can_receive(VLANClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000366{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000367 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000368
aliguorifbe78f42008-12-17 19:13:11 +0000369 if (!virtio_queue_ready(n->rx_vq) ||
370 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
371 return 0;
372
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000373 return 1;
374}
375
376static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
377{
aliguorifbe78f42008-12-17 19:13:11 +0000378 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
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100389/* 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 */
403static 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
aliguorifbe78f42008-12-17 19:13:11 +0000417static 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
432static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000433 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000434{
blueswir13f4cb3d2009-04-13 16:31:01 +0000435 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000436 int offset = 0;
437
438 hdr->flags = 0;
439 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
440
Mark McLoughlin3a330132009-10-22 17:43:45 +0100441 if (n->has_vnet_hdr) {
442 memcpy(hdr, buf, sizeof(*hdr));
443 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100444 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100445 }
446
aliguorifbe78f42008-12-17 19:13:11 +0000447 /* 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
aliguori3831ab22009-02-05 22:36:24 +0000456static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
457{
458 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000459 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000460 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000461 int i;
aliguori3831ab22009-02-05 22:36:24 +0000462
463 if (n->promisc)
464 return 1;
465
Mark McLoughlin3a330132009-10-22 17:43:45 +0100466 if (n->has_vnet_hdr) {
467 ptr += sizeof(struct virtio_net_hdr);
468 }
469
aliguorif21c0ed2009-02-05 22:36:32 +0000470 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
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600476 if (ptr[0] & 1) { // multicast
477 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600478 return !n->nobcast;
479 } else if (n->nomulti) {
480 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600481 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600482 return 1;
483 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600484
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 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600490 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600491 if (n->nouni) {
492 return 0;
493 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600494 return 1;
495 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600496 return 1;
497 }
aliguori3831ab22009-02-05 22:36:24 +0000498
Alex Williamson2d9aba32009-06-05 14:47:13 -0600499 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 }
aliguorib6503ed2009-02-05 22:36:28 +0000504 }
505
aliguori3831ab22009-02-05 22:36:24 +0000506 return 0;
507}
508
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000509static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000510{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000511 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000512 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000513 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000514
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000515 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000516 return -1;
517
518 if (!virtio_net_has_buffers(n, size))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100519 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000520
aliguori3831ab22009-02-05 22:36:24 +0000521 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100522 return size;
aliguori3831ab22009-02-05 22:36:24 +0000523
aliguorifbe78f42008-12-17 19:13:11 +0000524 /* 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
535 len = total = 0;
536
537 if ((i != 0 && !n->mergeable_rx_bufs) ||
538 virtqueue_pop(n->rx_vq, &elem) == 0) {
539 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100540 return -1;
aliguorifbe78f42008-12-17 19:13:11 +0000541 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);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100582
583 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000584}
585
Mark McLoughlin62433752009-06-18 18:21:36 +0100586static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
587
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000588static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100589{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000590 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100591
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
aliguorifbe78f42008-12-17 19:13:11 +0000601/* TX */
602static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
603{
604 VirtQueueElement elem;
aliguorifbe78f42008-12-17 19:13:11 +0000605
606 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
607 return;
608
Mark McLoughlin62433752009-06-18 18:21:36 +0100609 if (n->async_tx.elem.out_num) {
610 virtio_queue_set_notification(n->tx_vq, 0);
611 return;
612 }
613
aliguorifbe78f42008-12-17 19:13:11 +0000614 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100615 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000616 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 */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100631 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000632 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
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000642 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100643 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;
aliguorifbe78f42008-12-17 19:13:11 +0000652
653 virtqueue_push(vq, &elem, len);
654 virtio_notify(&n->vdev, vq);
655 }
656}
657
658static 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
675static 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
689static void virtio_net_save(QEMUFile *f, void *opaque)
690{
691 VirtIONet *n = opaque;
692
693 virtio_save(&n->vdev, f);
694
aliguori79674062009-02-05 22:36:12 +0000695 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000696 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000697 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000698 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600699 qemu_put_byte(f, n->promisc);
700 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000701 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);
aliguorif21c0ed2009-02-05 22:36:32 +0000703 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100704 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600705 qemu_put_byte(f, n->mac_table.multi_overflow);
706 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600707 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);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100711 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000712}
713
714static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
715{
716 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600717 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000718
aliguori9d6271b2009-02-05 22:36:04 +0000719 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000720 return -EINVAL;
721
722 virtio_load(&n->vdev, f);
723
aliguori79674062009-02-05 22:36:12 +0000724 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000725 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000726 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000727
aliguori9d6271b2009-02-05 22:36:04 +0000728 if (version_id >= 3)
729 n->status = qemu_get_be16(f);
730
aliguori002437c2009-02-05 22:36:20 +0000731 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600732 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 }
aliguori002437c2009-02-05 22:36:20 +0000739 }
740
aliguorib6503ed2009-02-05 22:36:28 +0000741 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);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600749 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000750 n->mac_table.in_use = 0;
751 }
752 }
753
aliguorif21c0ed2009-02-05 22:36:32 +0000754 if (version_id >= 6)
755 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
756
Mark McLoughlin3a330132009-10-22 17:43:45 +0100757 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) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000764 tap_using_vnet_hdr(n->nic->nc.peer, 1);
765 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200766 (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);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100771 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600772 }
773
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600774 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
Alex Williamson015cb162009-06-05 14:47:18 -0600779 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
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100786 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
Alex Williamson2d9aba32009-06-05 14:47:13 -0600793 /* 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
aliguorifbe78f42008-12-17 19:13:11 +0000801 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
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000809static void virtio_net_cleanup(VLANClientState *nc)
aliguorib946a152009-04-17 17:11:08 +0000810{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000811 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +0000812
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000813 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +0000814}
815
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000816static 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
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200825VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
aliguorifbe78f42008-12-17 19:13:11 +0000826{
827 VirtIONet *n;
828 static int virtio_net_id;
829
Paul Brook53c25ce2009-05-18 14:51:59 +0100830 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
831 sizeof(struct virtio_net_config),
832 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000833
aliguori0f03eca2009-02-05 22:36:08 +0000834 n->vdev.get_config = virtio_net_get_config;
835 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000836 n->vdev.get_features = virtio_net_get_features;
837 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000838 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000839 n->vdev.reset = virtio_net_reset;
aliguorifbe78f42008-12-17 19:13:11 +0000840 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);
Alex Williamson4ffb17f2009-06-05 14:47:23 -0600842 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200843 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +0000844 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +0000845 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +0000846
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000847 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);
aliguori96d5e202009-01-07 17:47:15 +0000850
aliguorifbe78f42008-12-17 19:13:11 +0000851 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;
aliguori002437c2009-02-05 22:36:20 +0000854 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000855
aliguorib6503ed2009-02-05 22:36:28 +0000856 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000857
aliguorif21c0ed2009-02-05 22:36:32 +0000858 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000859
aliguori9d6271b2009-02-05 22:36:04 +0000860 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000861 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100862
Paul Brook53c25ce2009-05-18 14:51:59 +0100863 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100864}
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200865
866void virtio_net_exit(VirtIODevice *vdev)
867{
868 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
869
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000870 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200871
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);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000881 qemu_del_vlan_client(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200882}