blob: be33c68bbc76d95a714bda49ec5a6cf9d5ba5660 [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"
Markus Armbruster2f792012010-02-18 16:24:31 +010018#include "qemu-error.h"
aliguorifbe78f42008-12-17 19:13:11 +000019#include "qemu-timer.h"
20#include "virtio-net.h"
21
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010022#define VIRTIO_NET_VM_VERSION 11
aliguorib6503ed2009-02-05 22:36:28 +000023
Alex Williamson4ffb17f2009-06-05 14:47:23 -060024#define MAC_TABLE_ENTRIES 64
aliguorif21c0ed2009-02-05 22:36:32 +000025#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
aliguori9d6271b2009-02-05 22:36:04 +000026
aliguorifbe78f42008-12-17 19:13:11 +000027typedef struct VirtIONet
28{
29 VirtIODevice vdev;
aliguori79674062009-02-05 22:36:12 +000030 uint8_t mac[ETH_ALEN];
aliguori554c97d2009-01-08 19:46:33 +000031 uint16_t status;
aliguorifbe78f42008-12-17 19:13:11 +000032 VirtQueue *rx_vq;
33 VirtQueue *tx_vq;
aliguori3d11d362009-02-05 22:36:16 +000034 VirtQueue *ctrl_vq;
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000035 NICState *nic;
aliguorifbe78f42008-12-17 19:13:11 +000036 QEMUTimer *tx_timer;
37 int tx_timer_active;
Mark McLoughlin3a330132009-10-22 17:43:45 +010038 uint32_t has_vnet_hdr;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010039 uint8_t has_ufo;
Mark McLoughlin62433752009-06-18 18:21:36 +010040 struct {
41 VirtQueueElement elem;
42 ssize_t len;
43 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000044 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060045 uint8_t promisc;
46 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060047 uint8_t alluni;
48 uint8_t nomulti;
49 uint8_t nouni;
50 uint8_t nobcast;
aliguorib6503ed2009-02-05 22:36:28 +000051 struct {
52 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060053 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060054 uint8_t multi_overflow;
55 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000056 uint8_t *macs;
57 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000058 uint32_t *vlans;
aliguorifbe78f42008-12-17 19:13:11 +000059} VirtIONet;
60
61/* TODO
62 * - we could suppress RX interrupt if we were so inclined.
63 */
64
65static VirtIONet *to_virtio_net(VirtIODevice *vdev)
66{
67 return (VirtIONet *)vdev;
68}
69
aliguori0f03eca2009-02-05 22:36:08 +000070static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000071{
72 VirtIONet *n = to_virtio_net(vdev);
73 struct virtio_net_config netcfg;
74
aliguori554c97d2009-01-08 19:46:33 +000075 netcfg.status = n->status;
aliguori79674062009-02-05 22:36:12 +000076 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000077 memcpy(config, &netcfg, sizeof(netcfg));
78}
79
aliguori0f03eca2009-02-05 22:36:08 +000080static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
81{
82 VirtIONet *n = to_virtio_net(vdev);
83 struct virtio_net_config netcfg;
84
85 memcpy(&netcfg, config, sizeof(netcfg));
86
aliguori79674062009-02-05 22:36:12 +000087 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
88 memcpy(n->mac, netcfg.mac, ETH_ALEN);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000089 qemu_format_nic_info_str(&n->nic->nc, n->mac);
aliguori0f03eca2009-02-05 22:36:08 +000090 }
91}
92
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000093static void virtio_net_set_link_status(VLANClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +000094{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000095 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +000096 uint16_t old_status = n->status;
97
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000098 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +000099 n->status &= ~VIRTIO_NET_S_LINK_UP;
100 else
101 n->status |= VIRTIO_NET_S_LINK_UP;
102
103 if (n->status != old_status)
104 virtio_notify_config(&n->vdev);
105}
106
aliguori002437c2009-02-05 22:36:20 +0000107static void virtio_net_reset(VirtIODevice *vdev)
108{
109 VirtIONet *n = to_virtio_net(vdev);
110
111 /* Reset back to compatibility mode */
112 n->promisc = 1;
113 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600114 n->alluni = 0;
115 n->nomulti = 0;
116 n->nouni = 0;
117 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000118
aliguorif21c0ed2009-02-05 22:36:32 +0000119 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000120 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600121 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600122 n->mac_table.multi_overflow = 0;
123 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000124 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000125 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000126}
127
Mark McLoughlin3a330132009-10-22 17:43:45 +0100128static int peer_has_vnet_hdr(VirtIONet *n)
129{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000130 if (!n->nic->nc.peer)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100131 return 0;
132
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000133 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100134 return 0;
135
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000136 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100137
138 return n->has_vnet_hdr;
139}
140
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100141static int peer_has_ufo(VirtIONet *n)
142{
143 if (!peer_has_vnet_hdr(n))
144 return 0;
145
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000146 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100147
148 return n->has_ufo;
149}
150
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200151static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000152{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100153 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000154
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200155 features |= (1 << VIRTIO_NET_F_MAC);
156
Mark McLoughlin3a330132009-10-22 17:43:45 +0100157 if (peer_has_vnet_hdr(n)) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000158 tap_using_vnet_hdr(n->nic->nc.peer, 1);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200159 } else {
160 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
161 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
162 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
163 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100164
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200165 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
166 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
167 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
168 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
169 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100170
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200171 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
172 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
173 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100174 }
175
aliguorifbe78f42008-12-17 19:13:11 +0000176 return features;
177}
178
aliguori8eca6b12009-04-05 17:40:08 +0000179static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
180{
181 uint32_t features = 0;
182
183 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
184 * but also these: */
185 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500186 features |= (1 << VIRTIO_NET_F_CSUM);
187 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
188 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
189 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000190
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200191 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000192}
193
aliguorifbe78f42008-12-17 19:13:11 +0000194static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
195{
196 VirtIONet *n = to_virtio_net(vdev);
197
198 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100199
200 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000201 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100202 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
203 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
204 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100205 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
206 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100207 }
aliguorifbe78f42008-12-17 19:13:11 +0000208}
209
aliguori002437c2009-02-05 22:36:20 +0000210static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
211 VirtQueueElement *elem)
212{
213 uint8_t on;
214
215 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
216 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
217 exit(1);
218 }
219
220 on = ldub_p(elem->out_sg[1].iov_base);
221
222 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
223 n->promisc = on;
224 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
225 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600226 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
227 n->alluni = on;
228 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
229 n->nomulti = on;
230 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
231 n->nouni = on;
232 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
233 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000234 else
235 return VIRTIO_NET_ERR;
236
237 return VIRTIO_NET_OK;
238}
239
aliguorib6503ed2009-02-05 22:36:28 +0000240static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
241 VirtQueueElement *elem)
242{
243 struct virtio_net_ctrl_mac mac_data;
244
245 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
246 elem->out_sg[1].iov_len < sizeof(mac_data) ||
247 elem->out_sg[2].iov_len < sizeof(mac_data))
248 return VIRTIO_NET_ERR;
249
250 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600251 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600252 n->mac_table.uni_overflow = 0;
253 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000254 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
255
256 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
257
258 if (sizeof(mac_data.entries) +
259 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
260 return VIRTIO_NET_ERR;
261
262 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
263 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
264 mac_data.entries * ETH_ALEN);
265 n->mac_table.in_use += mac_data.entries;
266 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600267 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000268 }
269
Alex Williamson2d9aba32009-06-05 14:47:13 -0600270 n->mac_table.first_multi = n->mac_table.in_use;
271
aliguorib6503ed2009-02-05 22:36:28 +0000272 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
273
274 if (sizeof(mac_data.entries) +
275 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
276 return VIRTIO_NET_ERR;
277
278 if (mac_data.entries) {
279 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
280 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
281 elem->out_sg[2].iov_base + sizeof(mac_data),
282 mac_data.entries * ETH_ALEN);
283 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600284 } else {
285 n->mac_table.multi_overflow = 1;
286 }
aliguorib6503ed2009-02-05 22:36:28 +0000287 }
288
289 return VIRTIO_NET_OK;
290}
291
aliguorif21c0ed2009-02-05 22:36:32 +0000292static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
293 VirtQueueElement *elem)
294{
295 uint16_t vid;
296
297 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
298 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
299 return VIRTIO_NET_ERR;
300 }
301
302 vid = lduw_le_p(elem->out_sg[1].iov_base);
303
304 if (vid >= MAX_VLAN)
305 return VIRTIO_NET_ERR;
306
307 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
308 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
309 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
310 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
311 else
312 return VIRTIO_NET_ERR;
313
314 return VIRTIO_NET_OK;
315}
316
aliguori3d11d362009-02-05 22:36:16 +0000317static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
318{
aliguori002437c2009-02-05 22:36:20 +0000319 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000320 struct virtio_net_ctrl_hdr ctrl;
321 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
322 VirtQueueElement elem;
323
324 while (virtqueue_pop(vq, &elem)) {
325 if ((elem.in_num < 1) || (elem.out_num < 1)) {
326 fprintf(stderr, "virtio-net ctrl missing headers\n");
327 exit(1);
328 }
329
330 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000331 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000332 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
333 exit(1);
334 }
335
336 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
337 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
338
aliguori002437c2009-02-05 22:36:20 +0000339 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
340 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000341 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
342 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000343 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
344 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000345
aliguori3d11d362009-02-05 22:36:16 +0000346 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
347
348 virtqueue_push(vq, &elem, sizeof(status));
349 virtio_notify(vdev, vq);
350 }
351}
352
aliguorifbe78f42008-12-17 19:13:11 +0000353/* RX */
354
355static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
356{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100357 VirtIONet *n = to_virtio_net(vdev);
358
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000359 qemu_flush_queued_packets(&n->nic->nc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400360
361 /* We now have RX buffers, signal to the IO thread to break out of the
362 * select to re-poll the tap file descriptor */
363 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000364}
365
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000366static int virtio_net_can_receive(VLANClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000367{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000368 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000369
aliguorifbe78f42008-12-17 19:13:11 +0000370 if (!virtio_queue_ready(n->rx_vq) ||
371 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
372 return 0;
373
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000374 return 1;
375}
376
377static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
378{
aliguorifbe78f42008-12-17 19:13:11 +0000379 if (virtio_queue_empty(n->rx_vq) ||
380 (n->mergeable_rx_bufs &&
381 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
382 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600383
384 /* To avoid a race condition where the guest has made some buffers
385 * available after the above check but before notification was
386 * enabled, check for available buffers again.
387 */
388 if (virtio_queue_empty(n->rx_vq) ||
389 (n->mergeable_rx_bufs &&
390 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
391 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000392 }
393
394 virtio_queue_set_notification(n->rx_vq, 0);
395 return 1;
396}
397
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100398/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
399 * it never finds out that the packets don't have valid checksums. This
400 * causes dhclient to get upset. Fedora's carried a patch for ages to
401 * fix this with Xen but it hasn't appeared in an upstream release of
402 * dhclient yet.
403 *
404 * To avoid breaking existing guests, we catch udp packets and add
405 * checksums. This is terrible but it's better than hacking the guest
406 * kernels.
407 *
408 * N.B. if we introduce a zero-copy API, this operation is no longer free so
409 * we should provide a mechanism to disable it to avoid polluting the host
410 * cache.
411 */
412static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
413 const uint8_t *buf, size_t size)
414{
415 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
416 (size > 27 && size < 1500) && /* normal sized MTU */
417 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
418 (buf[23] == 17) && /* ip.protocol == UDP */
419 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
420 /* FIXME this cast is evil */
421 net_checksum_calculate((uint8_t *)buf, size);
422 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
423 }
424}
425
aliguorifbe78f42008-12-17 19:13:11 +0000426static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
427{
428 int offset, i;
429
430 offset = i = 0;
431 while (offset < count && i < iovcnt) {
432 int len = MIN(iov[i].iov_len, count - offset);
433 memcpy(iov[i].iov_base, buf + offset, len);
434 offset += len;
435 i++;
436 }
437
438 return offset;
439}
440
441static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000442 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000443{
blueswir13f4cb3d2009-04-13 16:31:01 +0000444 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000445 int offset = 0;
446
447 hdr->flags = 0;
448 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
449
Mark McLoughlin3a330132009-10-22 17:43:45 +0100450 if (n->has_vnet_hdr) {
451 memcpy(hdr, buf, sizeof(*hdr));
452 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100453 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100454 }
455
aliguorifbe78f42008-12-17 19:13:11 +0000456 /* We only ever receive a struct virtio_net_hdr from the tapfd,
457 * but we may be passing along a larger header to the guest.
458 */
459 iov[0].iov_base += hdr_len;
460 iov[0].iov_len -= hdr_len;
461
462 return offset;
463}
464
aliguori3831ab22009-02-05 22:36:24 +0000465static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
466{
467 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000468 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000469 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000470 int i;
aliguori3831ab22009-02-05 22:36:24 +0000471
472 if (n->promisc)
473 return 1;
474
Mark McLoughlin3a330132009-10-22 17:43:45 +0100475 if (n->has_vnet_hdr) {
476 ptr += sizeof(struct virtio_net_hdr);
477 }
478
aliguorif21c0ed2009-02-05 22:36:32 +0000479 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
480 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
481 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
482 return 0;
483 }
484
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600485 if (ptr[0] & 1) { // multicast
486 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600487 return !n->nobcast;
488 } else if (n->nomulti) {
489 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600490 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600491 return 1;
492 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600493
494 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
495 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
496 return 1;
497 }
498 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600499 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600500 if (n->nouni) {
501 return 0;
502 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600503 return 1;
504 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600505 return 1;
506 }
aliguori3831ab22009-02-05 22:36:24 +0000507
Alex Williamson2d9aba32009-06-05 14:47:13 -0600508 for (i = 0; i < n->mac_table.first_multi; i++) {
509 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
510 return 1;
511 }
512 }
aliguorib6503ed2009-02-05 22:36:28 +0000513 }
514
aliguori3831ab22009-02-05 22:36:24 +0000515 return 0;
516}
517
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000518static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000519{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000520 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000521 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000522 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000523
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000524 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000525 return -1;
526
527 if (!virtio_net_has_buffers(n, size))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100528 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000529
aliguori3831ab22009-02-05 22:36:24 +0000530 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100531 return size;
aliguori3831ab22009-02-05 22:36:24 +0000532
aliguorifbe78f42008-12-17 19:13:11 +0000533 /* hdr_len refers to the header we supply to the guest */
534 hdr_len = n->mergeable_rx_bufs ?
535 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
536
537 offset = i = 0;
538
539 while (offset < size) {
540 VirtQueueElement elem;
541 int len, total;
542 struct iovec sg[VIRTQUEUE_MAX_SIZE];
543
Amit Shah22c253d2010-01-13 16:24:43 +0530544 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000545
546 if ((i != 0 && !n->mergeable_rx_bufs) ||
547 virtqueue_pop(n->rx_vq, &elem) == 0) {
548 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100549 return -1;
aliguorifbe78f42008-12-17 19:13:11 +0000550 fprintf(stderr, "virtio-net truncating packet\n");
551 exit(1);
552 }
553
554 if (elem.in_num < 1) {
555 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
556 exit(1);
557 }
558
559 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
560 fprintf(stderr, "virtio-net header not in first element\n");
561 exit(1);
562 }
563
564 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
565
566 if (i == 0) {
567 if (n->mergeable_rx_bufs)
568 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
569
570 offset += receive_header(n, sg, elem.in_num,
571 buf + offset, size - offset, hdr_len);
572 total += hdr_len;
573 }
574
575 /* copy in packet. ugh */
576 len = iov_fill(sg, elem.in_num,
577 buf + offset, size - offset);
578 total += len;
579
580 /* signal other side */
581 virtqueue_fill(n->rx_vq, &elem, total, i++);
582
583 offset += len;
584 }
585
586 if (mhdr)
587 mhdr->num_buffers = i;
588
589 virtqueue_flush(n->rx_vq, i);
590 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100591
592 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000593}
594
Mark McLoughlin62433752009-06-18 18:21:36 +0100595static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
596
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000597static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100598{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000599 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100600
601 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
602 virtio_notify(&n->vdev, n->tx_vq);
603
604 n->async_tx.elem.out_num = n->async_tx.len = 0;
605
606 virtio_queue_set_notification(n->tx_vq, 1);
607 virtio_net_flush_tx(n, n->tx_vq);
608}
609
aliguorifbe78f42008-12-17 19:13:11 +0000610/* TX */
611static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
612{
613 VirtQueueElement elem;
aliguorifbe78f42008-12-17 19:13:11 +0000614
615 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
616 return;
617
Mark McLoughlin62433752009-06-18 18:21:36 +0100618 if (n->async_tx.elem.out_num) {
619 virtio_queue_set_notification(n->tx_vq, 0);
620 return;
621 }
622
aliguorifbe78f42008-12-17 19:13:11 +0000623 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100624 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000625 unsigned int out_num = elem.out_num;
626 struct iovec *out_sg = &elem.out_sg[0];
627 unsigned hdr_len;
628
629 /* hdr_len refers to the header received from the guest */
630 hdr_len = n->mergeable_rx_bufs ?
631 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
632 sizeof(struct virtio_net_hdr);
633
634 if (out_num < 1 || out_sg->iov_len != hdr_len) {
635 fprintf(stderr, "virtio-net header not in first element\n");
636 exit(1);
637 }
638
639 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100640 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000641 out_num--;
642 out_sg++;
643 len += hdr_len;
644 } else if (n->mergeable_rx_bufs) {
645 /* tapfd expects a struct virtio_net_hdr */
646 hdr_len -= sizeof(struct virtio_net_hdr);
647 out_sg->iov_len -= hdr_len;
648 len += hdr_len;
649 }
650
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000651 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100652 virtio_net_tx_complete);
653 if (ret == 0) {
654 virtio_queue_set_notification(n->tx_vq, 0);
655 n->async_tx.elem = elem;
656 n->async_tx.len = len;
657 return;
658 }
659
660 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000661
662 virtqueue_push(vq, &elem, len);
663 virtio_notify(&n->vdev, vq);
664 }
665}
666
667static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
668{
669 VirtIONet *n = to_virtio_net(vdev);
670
671 if (n->tx_timer_active) {
672 virtio_queue_set_notification(vq, 1);
673 qemu_del_timer(n->tx_timer);
674 n->tx_timer_active = 0;
675 virtio_net_flush_tx(n, vq);
676 } else {
677 qemu_mod_timer(n->tx_timer,
678 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
679 n->tx_timer_active = 1;
680 virtio_queue_set_notification(vq, 0);
681 }
682}
683
684static void virtio_net_tx_timer(void *opaque)
685{
686 VirtIONet *n = opaque;
687
688 n->tx_timer_active = 0;
689
690 /* Just in case the driver is not ready on more */
691 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
692 return;
693
694 virtio_queue_set_notification(n->tx_vq, 1);
695 virtio_net_flush_tx(n, n->tx_vq);
696}
697
698static void virtio_net_save(QEMUFile *f, void *opaque)
699{
700 VirtIONet *n = opaque;
701
702 virtio_save(&n->vdev, f);
703
aliguori79674062009-02-05 22:36:12 +0000704 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000705 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000706 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000707 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600708 qemu_put_byte(f, n->promisc);
709 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000710 qemu_put_be32(f, n->mac_table.in_use);
711 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000712 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100713 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600714 qemu_put_byte(f, n->mac_table.multi_overflow);
715 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600716 qemu_put_byte(f, n->alluni);
717 qemu_put_byte(f, n->nomulti);
718 qemu_put_byte(f, n->nouni);
719 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100720 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000721}
722
723static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
724{
725 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600726 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000727
aliguori9d6271b2009-02-05 22:36:04 +0000728 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000729 return -EINVAL;
730
731 virtio_load(&n->vdev, f);
732
aliguori79674062009-02-05 22:36:12 +0000733 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000734 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000735 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000736
aliguori9d6271b2009-02-05 22:36:04 +0000737 if (version_id >= 3)
738 n->status = qemu_get_be16(f);
739
aliguori002437c2009-02-05 22:36:20 +0000740 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600741 if (version_id < 8) {
742 n->promisc = qemu_get_be32(f);
743 n->allmulti = qemu_get_be32(f);
744 } else {
745 n->promisc = qemu_get_byte(f);
746 n->allmulti = qemu_get_byte(f);
747 }
aliguori002437c2009-02-05 22:36:20 +0000748 }
749
aliguorib6503ed2009-02-05 22:36:28 +0000750 if (version_id >= 5) {
751 n->mac_table.in_use = qemu_get_be32(f);
752 /* MAC_TABLE_ENTRIES may be different from the saved image */
753 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
754 qemu_get_buffer(f, n->mac_table.macs,
755 n->mac_table.in_use * ETH_ALEN);
756 } else if (n->mac_table.in_use) {
757 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600758 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000759 n->mac_table.in_use = 0;
760 }
761 }
762
aliguorif21c0ed2009-02-05 22:36:32 +0000763 if (version_id >= 6)
764 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
765
Mark McLoughlin3a330132009-10-22 17:43:45 +0100766 if (version_id >= 7) {
767 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100768 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100769 return -1;
770 }
771
772 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000773 tap_using_vnet_hdr(n->nic->nc.peer, 1);
774 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200775 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
776 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
777 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
778 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
779 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100780 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600781 }
782
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600783 if (version_id >= 9) {
784 n->mac_table.multi_overflow = qemu_get_byte(f);
785 n->mac_table.uni_overflow = qemu_get_byte(f);
786 }
787
Alex Williamson015cb162009-06-05 14:47:18 -0600788 if (version_id >= 10) {
789 n->alluni = qemu_get_byte(f);
790 n->nomulti = qemu_get_byte(f);
791 n->nouni = qemu_get_byte(f);
792 n->nobcast = qemu_get_byte(f);
793 }
794
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100795 if (version_id >= 11) {
796 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100797 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100798 return -1;
799 }
800 }
801
Alex Williamson2d9aba32009-06-05 14:47:13 -0600802 /* Find the first multicast entry in the saved MAC filter */
803 for (i = 0; i < n->mac_table.in_use; i++) {
804 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
805 break;
806 }
807 }
808 n->mac_table.first_multi = i;
809
aliguorifbe78f42008-12-17 19:13:11 +0000810 if (n->tx_timer_active) {
811 qemu_mod_timer(n->tx_timer,
812 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
813 }
814
815 return 0;
816}
817
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000818static void virtio_net_cleanup(VLANClientState *nc)
aliguorib946a152009-04-17 17:11:08 +0000819{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000820 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +0000821
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000822 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +0000823}
824
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000825static NetClientInfo net_virtio_info = {
826 .type = NET_CLIENT_TYPE_NIC,
827 .size = sizeof(NICState),
828 .can_receive = virtio_net_can_receive,
829 .receive = virtio_net_receive,
830 .cleanup = virtio_net_cleanup,
831 .link_status_changed = virtio_net_set_link_status,
832};
833
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200834VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
aliguorifbe78f42008-12-17 19:13:11 +0000835{
836 VirtIONet *n;
837 static int virtio_net_id;
838
Paul Brook53c25ce2009-05-18 14:51:59 +0100839 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
840 sizeof(struct virtio_net_config),
841 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000842
aliguori0f03eca2009-02-05 22:36:08 +0000843 n->vdev.get_config = virtio_net_get_config;
844 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000845 n->vdev.get_features = virtio_net_get_features;
846 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000847 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000848 n->vdev.reset = virtio_net_reset;
aliguorifbe78f42008-12-17 19:13:11 +0000849 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
850 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
Alex Williamson4ffb17f2009-06-05 14:47:23 -0600851 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200852 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +0000853 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +0000854 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +0000855
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000856 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
857
858 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +0000859
aliguorifbe78f42008-12-17 19:13:11 +0000860 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
861 n->tx_timer_active = 0;
862 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000863 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000864
aliguorib6503ed2009-02-05 22:36:28 +0000865 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000866
aliguorif21c0ed2009-02-05 22:36:32 +0000867 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000868
aliguori9d6271b2009-02-05 22:36:04 +0000869 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000870 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100871
Paul Brook53c25ce2009-05-18 14:51:59 +0100872 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100873}
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200874
875void virtio_net_exit(VirtIODevice *vdev)
876{
877 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
878
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000879 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200880
881 unregister_savevm("virtio-net", n);
882
883 qemu_free(n->mac_table.macs);
884 qemu_free(n->vlans);
885
886 qemu_del_timer(n->tx_timer);
887 qemu_free_timer(n->tx_timer);
888
889 virtio_cleanup(&n->vdev);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000890 qemu_del_vlan_client(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200891}