blob: ac919e1e8aa0d88668edd4af8e4ecb48f822c5b2 [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"
aliguorifbe78f42008-12-17 19:13:11 +000017#include "qemu-timer.h"
18#include "virtio-net.h"
19
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010020#define VIRTIO_NET_VM_VERSION 11
aliguorib6503ed2009-02-05 22:36:28 +000021
Alex Williamson4ffb17f2009-06-05 14:47:23 -060022#define MAC_TABLE_ENTRIES 64
aliguorif21c0ed2009-02-05 22:36:32 +000023#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
aliguori9d6271b2009-02-05 22:36:04 +000024
aliguorifbe78f42008-12-17 19:13:11 +000025typedef struct VirtIONet
26{
27 VirtIODevice vdev;
aliguori79674062009-02-05 22:36:12 +000028 uint8_t mac[ETH_ALEN];
aliguori554c97d2009-01-08 19:46:33 +000029 uint16_t status;
aliguorifbe78f42008-12-17 19:13:11 +000030 VirtQueue *rx_vq;
31 VirtQueue *tx_vq;
aliguori3d11d362009-02-05 22:36:16 +000032 VirtQueue *ctrl_vq;
aliguorifbe78f42008-12-17 19:13:11 +000033 VLANClientState *vc;
34 QEMUTimer *tx_timer;
35 int tx_timer_active;
Mark McLoughlin3a330132009-10-22 17:43:45 +010036 uint32_t has_vnet_hdr;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010037 uint8_t has_ufo;
Mark McLoughlin62433752009-06-18 18:21:36 +010038 struct {
39 VirtQueueElement elem;
40 ssize_t len;
41 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000042 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060043 uint8_t promisc;
44 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060045 uint8_t alluni;
46 uint8_t nomulti;
47 uint8_t nouni;
48 uint8_t nobcast;
aliguorib6503ed2009-02-05 22:36:28 +000049 struct {
50 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060051 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060052 uint8_t multi_overflow;
53 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000054 uint8_t *macs;
55 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000056 uint32_t *vlans;
aliguorifbe78f42008-12-17 19:13:11 +000057} VirtIONet;
58
59/* TODO
60 * - we could suppress RX interrupt if we were so inclined.
61 */
62
63static VirtIONet *to_virtio_net(VirtIODevice *vdev)
64{
65 return (VirtIONet *)vdev;
66}
67
aliguori0f03eca2009-02-05 22:36:08 +000068static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000069{
70 VirtIONet *n = to_virtio_net(vdev);
71 struct virtio_net_config netcfg;
72
aliguori554c97d2009-01-08 19:46:33 +000073 netcfg.status = n->status;
aliguori79674062009-02-05 22:36:12 +000074 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000075 memcpy(config, &netcfg, sizeof(netcfg));
76}
77
aliguori0f03eca2009-02-05 22:36:08 +000078static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
79{
80 VirtIONet *n = to_virtio_net(vdev);
81 struct virtio_net_config netcfg;
82
83 memcpy(&netcfg, config, sizeof(netcfg));
84
aliguori79674062009-02-05 22:36:12 +000085 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
86 memcpy(n->mac, netcfg.mac, ETH_ALEN);
aliguori0f03eca2009-02-05 22:36:08 +000087 qemu_format_nic_info_str(n->vc, n->mac);
88 }
89}
90
aliguori554c97d2009-01-08 19:46:33 +000091static void virtio_net_set_link_status(VLANClientState *vc)
92{
93 VirtIONet *n = vc->opaque;
94 uint16_t old_status = n->status;
95
96 if (vc->link_down)
97 n->status &= ~VIRTIO_NET_S_LINK_UP;
98 else
99 n->status |= VIRTIO_NET_S_LINK_UP;
100
101 if (n->status != old_status)
102 virtio_notify_config(&n->vdev);
103}
104
aliguori002437c2009-02-05 22:36:20 +0000105static void virtio_net_reset(VirtIODevice *vdev)
106{
107 VirtIONet *n = to_virtio_net(vdev);
108
109 /* Reset back to compatibility mode */
110 n->promisc = 1;
111 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600112 n->alluni = 0;
113 n->nomulti = 0;
114 n->nouni = 0;
115 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000116
aliguorif21c0ed2009-02-05 22:36:32 +0000117 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000118 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600119 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600120 n->mac_table.multi_overflow = 0;
121 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000122 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000123 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000124}
125
Mark McLoughlin3a330132009-10-22 17:43:45 +0100126static int peer_has_vnet_hdr(VirtIONet *n)
127{
128 if (!n->vc->peer)
129 return 0;
130
131 if (n->vc->peer->type != NET_CLIENT_TYPE_TAP)
132 return 0;
133
134 n->has_vnet_hdr = tap_has_vnet_hdr(n->vc->peer);
135
136 return n->has_vnet_hdr;
137}
138
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100139static int peer_has_ufo(VirtIONet *n)
140{
141 if (!peer_has_vnet_hdr(n))
142 return 0;
143
144 n->has_ufo = tap_has_ufo(n->vc->peer);
145
146 return n->has_ufo;
147}
148
aliguorifbe78f42008-12-17 19:13:11 +0000149static uint32_t virtio_net_get_features(VirtIODevice *vdev)
150{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100151 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000152 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
Mark McLoughline16044e2009-06-18 11:31:07 +0100153 (1 << VIRTIO_NET_F_MRG_RXBUF) |
aliguori3d11d362009-02-05 22:36:16 +0000154 (1 << VIRTIO_NET_F_STATUS) |
aliguorib6503ed2009-02-05 22:36:28 +0000155 (1 << VIRTIO_NET_F_CTRL_VQ) |
aliguorif21c0ed2009-02-05 22:36:32 +0000156 (1 << VIRTIO_NET_F_CTRL_RX) |
Alex Williamson015cb162009-06-05 14:47:18 -0600157 (1 << VIRTIO_NET_F_CTRL_VLAN) |
158 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
aliguorifbe78f42008-12-17 19:13:11 +0000159
Mark McLoughlin3a330132009-10-22 17:43:45 +0100160 if (peer_has_vnet_hdr(n)) {
161 tap_using_vnet_hdr(n->vc->peer, 1);
162
163 features |= (1 << VIRTIO_NET_F_CSUM);
164 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
165 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
166 features |= (1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100167
168 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
169 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
170 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
171 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100172
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100173 if (peer_has_ufo(n)) {
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100174 features |= (1 << VIRTIO_NET_F_GUEST_UFO);
175 features |= (1 << VIRTIO_NET_F_HOST_UFO);
176 }
Mark McLoughlin3a330132009-10-22 17:43:45 +0100177 }
178
aliguorifbe78f42008-12-17 19:13:11 +0000179 return features;
180}
181
aliguori8eca6b12009-04-05 17:40:08 +0000182static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
183{
184 uint32_t features = 0;
185
186 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
187 * but also these: */
188 features |= (1 << VIRTIO_NET_F_MAC);
189 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
190 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
191 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
192 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
193
194 return features & virtio_net_get_features(vdev);
195}
196
aliguorifbe78f42008-12-17 19:13:11 +0000197static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
198{
199 VirtIONet *n = to_virtio_net(vdev);
200
201 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100202
203 if (n->has_vnet_hdr) {
204 tap_set_offload(n->vc->peer,
205 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
206 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
207 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100208 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
209 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100210 }
aliguorifbe78f42008-12-17 19:13:11 +0000211}
212
aliguori002437c2009-02-05 22:36:20 +0000213static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
214 VirtQueueElement *elem)
215{
216 uint8_t on;
217
218 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
219 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
220 exit(1);
221 }
222
223 on = ldub_p(elem->out_sg[1].iov_base);
224
225 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
226 n->promisc = on;
227 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
228 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600229 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
230 n->alluni = on;
231 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
232 n->nomulti = on;
233 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
234 n->nouni = on;
235 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
236 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000237 else
238 return VIRTIO_NET_ERR;
239
240 return VIRTIO_NET_OK;
241}
242
aliguorib6503ed2009-02-05 22:36:28 +0000243static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
244 VirtQueueElement *elem)
245{
246 struct virtio_net_ctrl_mac mac_data;
247
248 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
249 elem->out_sg[1].iov_len < sizeof(mac_data) ||
250 elem->out_sg[2].iov_len < sizeof(mac_data))
251 return VIRTIO_NET_ERR;
252
253 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600254 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600255 n->mac_table.uni_overflow = 0;
256 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000257 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
258
259 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
260
261 if (sizeof(mac_data.entries) +
262 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
263 return VIRTIO_NET_ERR;
264
265 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
266 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
267 mac_data.entries * ETH_ALEN);
268 n->mac_table.in_use += mac_data.entries;
269 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600270 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000271 }
272
Alex Williamson2d9aba32009-06-05 14:47:13 -0600273 n->mac_table.first_multi = n->mac_table.in_use;
274
aliguorib6503ed2009-02-05 22:36:28 +0000275 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
276
277 if (sizeof(mac_data.entries) +
278 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
279 return VIRTIO_NET_ERR;
280
281 if (mac_data.entries) {
282 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
283 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
284 elem->out_sg[2].iov_base + sizeof(mac_data),
285 mac_data.entries * ETH_ALEN);
286 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600287 } else {
288 n->mac_table.multi_overflow = 1;
289 }
aliguorib6503ed2009-02-05 22:36:28 +0000290 }
291
292 return VIRTIO_NET_OK;
293}
294
aliguorif21c0ed2009-02-05 22:36:32 +0000295static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
296 VirtQueueElement *elem)
297{
298 uint16_t vid;
299
300 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
301 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
302 return VIRTIO_NET_ERR;
303 }
304
305 vid = lduw_le_p(elem->out_sg[1].iov_base);
306
307 if (vid >= MAX_VLAN)
308 return VIRTIO_NET_ERR;
309
310 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
311 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
312 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
313 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
314 else
315 return VIRTIO_NET_ERR;
316
317 return VIRTIO_NET_OK;
318}
319
aliguori3d11d362009-02-05 22:36:16 +0000320static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
321{
aliguori002437c2009-02-05 22:36:20 +0000322 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000323 struct virtio_net_ctrl_hdr ctrl;
324 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
325 VirtQueueElement elem;
326
327 while (virtqueue_pop(vq, &elem)) {
328 if ((elem.in_num < 1) || (elem.out_num < 1)) {
329 fprintf(stderr, "virtio-net ctrl missing headers\n");
330 exit(1);
331 }
332
333 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000334 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000335 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
336 exit(1);
337 }
338
339 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
340 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
341
aliguori002437c2009-02-05 22:36:20 +0000342 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
343 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000344 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
345 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000346 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
347 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000348
aliguori3d11d362009-02-05 22:36:16 +0000349 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
350
351 virtqueue_push(vq, &elem, sizeof(status));
352 virtio_notify(vdev, vq);
353 }
354}
355
aliguorifbe78f42008-12-17 19:13:11 +0000356/* RX */
357
358static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
359{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100360 VirtIONet *n = to_virtio_net(vdev);
361
362 qemu_flush_queued_packets(n->vc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400363
364 /* We now have RX buffers, signal to the IO thread to break out of the
365 * select to re-poll the tap file descriptor */
366 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000367}
368
369static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
370{
371 if (!virtio_queue_ready(n->rx_vq) ||
372 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
373 return 0;
374
375 if (virtio_queue_empty(n->rx_vq) ||
376 (n->mergeable_rx_bufs &&
377 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
378 virtio_queue_set_notification(n->rx_vq, 1);
379 return 0;
380 }
381
382 virtio_queue_set_notification(n->rx_vq, 0);
383 return 1;
384}
385
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100386static int virtio_net_can_receive(VLANClientState *vc)
aliguorifbe78f42008-12-17 19:13:11 +0000387{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100388 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000389
390 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
391}
392
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100393/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
394 * it never finds out that the packets don't have valid checksums. This
395 * causes dhclient to get upset. Fedora's carried a patch for ages to
396 * fix this with Xen but it hasn't appeared in an upstream release of
397 * dhclient yet.
398 *
399 * To avoid breaking existing guests, we catch udp packets and add
400 * checksums. This is terrible but it's better than hacking the guest
401 * kernels.
402 *
403 * N.B. if we introduce a zero-copy API, this operation is no longer free so
404 * we should provide a mechanism to disable it to avoid polluting the host
405 * cache.
406 */
407static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
408 const uint8_t *buf, size_t size)
409{
410 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
411 (size > 27 && size < 1500) && /* normal sized MTU */
412 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
413 (buf[23] == 17) && /* ip.protocol == UDP */
414 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
415 /* FIXME this cast is evil */
416 net_checksum_calculate((uint8_t *)buf, size);
417 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
418 }
419}
420
aliguorifbe78f42008-12-17 19:13:11 +0000421static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
422{
423 int offset, i;
424
425 offset = i = 0;
426 while (offset < count && i < iovcnt) {
427 int len = MIN(iov[i].iov_len, count - offset);
428 memcpy(iov[i].iov_base, buf + offset, len);
429 offset += len;
430 i++;
431 }
432
433 return offset;
434}
435
436static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000437 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000438{
blueswir13f4cb3d2009-04-13 16:31:01 +0000439 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000440 int offset = 0;
441
442 hdr->flags = 0;
443 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
444
Mark McLoughlin3a330132009-10-22 17:43:45 +0100445 if (n->has_vnet_hdr) {
446 memcpy(hdr, buf, sizeof(*hdr));
447 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100448 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100449 }
450
aliguorifbe78f42008-12-17 19:13:11 +0000451 /* We only ever receive a struct virtio_net_hdr from the tapfd,
452 * but we may be passing along a larger header to the guest.
453 */
454 iov[0].iov_base += hdr_len;
455 iov[0].iov_len -= hdr_len;
456
457 return offset;
458}
459
aliguori3831ab22009-02-05 22:36:24 +0000460static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
461{
462 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000463 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000464 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000465 int i;
aliguori3831ab22009-02-05 22:36:24 +0000466
467 if (n->promisc)
468 return 1;
469
Mark McLoughlin3a330132009-10-22 17:43:45 +0100470 if (n->has_vnet_hdr) {
471 ptr += sizeof(struct virtio_net_hdr);
472 }
473
aliguorif21c0ed2009-02-05 22:36:32 +0000474 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
475 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
476 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
477 return 0;
478 }
479
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600480 if (ptr[0] & 1) { // multicast
481 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600482 return !n->nobcast;
483 } else if (n->nomulti) {
484 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600485 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600486 return 1;
487 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600488
489 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
490 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
491 return 1;
492 }
493 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600494 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600495 if (n->nouni) {
496 return 0;
497 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600498 return 1;
499 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600500 return 1;
501 }
aliguori3831ab22009-02-05 22:36:24 +0000502
Alex Williamson2d9aba32009-06-05 14:47:13 -0600503 for (i = 0; i < n->mac_table.first_multi; i++) {
504 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
505 return 1;
506 }
507 }
aliguorib6503ed2009-02-05 22:36:28 +0000508 }
509
aliguori3831ab22009-02-05 22:36:24 +0000510 return 0;
511}
512
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100513static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000514{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100515 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000516 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000517 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000518
519 if (!do_virtio_net_can_receive(n, size))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100520 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000521
aliguori3831ab22009-02-05 22:36:24 +0000522 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100523 return size;
aliguori3831ab22009-02-05 22:36:24 +0000524
aliguorifbe78f42008-12-17 19:13:11 +0000525 /* hdr_len refers to the header we supply to the guest */
526 hdr_len = n->mergeable_rx_bufs ?
527 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
528
529 offset = i = 0;
530
531 while (offset < size) {
532 VirtQueueElement elem;
533 int len, total;
534 struct iovec sg[VIRTQUEUE_MAX_SIZE];
535
536 len = total = 0;
537
538 if ((i != 0 && !n->mergeable_rx_bufs) ||
539 virtqueue_pop(n->rx_vq, &elem) == 0) {
540 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100541 return -1;
aliguorifbe78f42008-12-17 19:13:11 +0000542 fprintf(stderr, "virtio-net truncating packet\n");
543 exit(1);
544 }
545
546 if (elem.in_num < 1) {
547 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
548 exit(1);
549 }
550
551 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
552 fprintf(stderr, "virtio-net header not in first element\n");
553 exit(1);
554 }
555
556 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
557
558 if (i == 0) {
559 if (n->mergeable_rx_bufs)
560 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
561
562 offset += receive_header(n, sg, elem.in_num,
563 buf + offset, size - offset, hdr_len);
564 total += hdr_len;
565 }
566
567 /* copy in packet. ugh */
568 len = iov_fill(sg, elem.in_num,
569 buf + offset, size - offset);
570 total += len;
571
572 /* signal other side */
573 virtqueue_fill(n->rx_vq, &elem, total, i++);
574
575 offset += len;
576 }
577
578 if (mhdr)
579 mhdr->num_buffers = i;
580
581 virtqueue_flush(n->rx_vq, i);
582 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100583
584 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000585}
586
Mark McLoughlin62433752009-06-18 18:21:36 +0100587static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
588
589static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len)
590{
591 VirtIONet *n = vc->opaque;
592
593 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
594 virtio_notify(&n->vdev, n->tx_vq);
595
596 n->async_tx.elem.out_num = n->async_tx.len = 0;
597
598 virtio_queue_set_notification(n->tx_vq, 1);
599 virtio_net_flush_tx(n, n->tx_vq);
600}
601
aliguorifbe78f42008-12-17 19:13:11 +0000602/* TX */
603static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
604{
605 VirtQueueElement elem;
aliguorifbe78f42008-12-17 19:13:11 +0000606
607 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
608 return;
609
Mark McLoughlin62433752009-06-18 18:21:36 +0100610 if (n->async_tx.elem.out_num) {
611 virtio_queue_set_notification(n->tx_vq, 0);
612 return;
613 }
614
aliguorifbe78f42008-12-17 19:13:11 +0000615 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100616 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000617 unsigned int out_num = elem.out_num;
618 struct iovec *out_sg = &elem.out_sg[0];
619 unsigned hdr_len;
620
621 /* hdr_len refers to the header received from the guest */
622 hdr_len = n->mergeable_rx_bufs ?
623 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
624 sizeof(struct virtio_net_hdr);
625
626 if (out_num < 1 || out_sg->iov_len != hdr_len) {
627 fprintf(stderr, "virtio-net header not in first element\n");
628 exit(1);
629 }
630
631 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100632 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000633 out_num--;
634 out_sg++;
635 len += hdr_len;
636 } else if (n->mergeable_rx_bufs) {
637 /* tapfd expects a struct virtio_net_hdr */
638 hdr_len -= sizeof(struct virtio_net_hdr);
639 out_sg->iov_len -= hdr_len;
640 len += hdr_len;
641 }
642
Mark McLoughlin62433752009-06-18 18:21:36 +0100643 ret = qemu_sendv_packet_async(n->vc, out_sg, out_num,
644 virtio_net_tx_complete);
645 if (ret == 0) {
646 virtio_queue_set_notification(n->tx_vq, 0);
647 n->async_tx.elem = elem;
648 n->async_tx.len = len;
649 return;
650 }
651
652 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000653
654 virtqueue_push(vq, &elem, len);
655 virtio_notify(&n->vdev, vq);
656 }
657}
658
659static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
660{
661 VirtIONet *n = to_virtio_net(vdev);
662
663 if (n->tx_timer_active) {
664 virtio_queue_set_notification(vq, 1);
665 qemu_del_timer(n->tx_timer);
666 n->tx_timer_active = 0;
667 virtio_net_flush_tx(n, vq);
668 } else {
669 qemu_mod_timer(n->tx_timer,
670 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
671 n->tx_timer_active = 1;
672 virtio_queue_set_notification(vq, 0);
673 }
674}
675
676static void virtio_net_tx_timer(void *opaque)
677{
678 VirtIONet *n = opaque;
679
680 n->tx_timer_active = 0;
681
682 /* Just in case the driver is not ready on more */
683 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
684 return;
685
686 virtio_queue_set_notification(n->tx_vq, 1);
687 virtio_net_flush_tx(n, n->tx_vq);
688}
689
690static void virtio_net_save(QEMUFile *f, void *opaque)
691{
692 VirtIONet *n = opaque;
693
694 virtio_save(&n->vdev, f);
695
aliguori79674062009-02-05 22:36:12 +0000696 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000697 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000698 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000699 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600700 qemu_put_byte(f, n->promisc);
701 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000702 qemu_put_be32(f, n->mac_table.in_use);
703 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000704 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100705 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600706 qemu_put_byte(f, n->mac_table.multi_overflow);
707 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600708 qemu_put_byte(f, n->alluni);
709 qemu_put_byte(f, n->nomulti);
710 qemu_put_byte(f, n->nouni);
711 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100712 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000713}
714
715static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
716{
717 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600718 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000719
aliguori9d6271b2009-02-05 22:36:04 +0000720 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000721 return -EINVAL;
722
723 virtio_load(&n->vdev, f);
724
aliguori79674062009-02-05 22:36:12 +0000725 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000726 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000727 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000728
aliguori9d6271b2009-02-05 22:36:04 +0000729 if (version_id >= 3)
730 n->status = qemu_get_be16(f);
731
aliguori002437c2009-02-05 22:36:20 +0000732 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600733 if (version_id < 8) {
734 n->promisc = qemu_get_be32(f);
735 n->allmulti = qemu_get_be32(f);
736 } else {
737 n->promisc = qemu_get_byte(f);
738 n->allmulti = qemu_get_byte(f);
739 }
aliguori002437c2009-02-05 22:36:20 +0000740 }
741
aliguorib6503ed2009-02-05 22:36:28 +0000742 if (version_id >= 5) {
743 n->mac_table.in_use = qemu_get_be32(f);
744 /* MAC_TABLE_ENTRIES may be different from the saved image */
745 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
746 qemu_get_buffer(f, n->mac_table.macs,
747 n->mac_table.in_use * ETH_ALEN);
748 } else if (n->mac_table.in_use) {
749 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600750 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000751 n->mac_table.in_use = 0;
752 }
753 }
754
aliguorif21c0ed2009-02-05 22:36:32 +0000755 if (version_id >= 6)
756 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
757
Mark McLoughlin3a330132009-10-22 17:43:45 +0100758 if (version_id >= 7) {
759 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
760 qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
761 return -1;
762 }
763
764 if (n->has_vnet_hdr) {
765 tap_using_vnet_hdr(n->vc->peer, 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100766 tap_set_offload(n->vc->peer,
767 (n->vdev.features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
768 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
769 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100770 (n->vdev.features >> VIRTIO_NET_F_GUEST_ECN) & 1,
771 (n->vdev.features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100772 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600773 }
774
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600775 if (version_id >= 9) {
776 n->mac_table.multi_overflow = qemu_get_byte(f);
777 n->mac_table.uni_overflow = qemu_get_byte(f);
778 }
779
Alex Williamson015cb162009-06-05 14:47:18 -0600780 if (version_id >= 10) {
781 n->alluni = qemu_get_byte(f);
782 n->nomulti = qemu_get_byte(f);
783 n->nouni = qemu_get_byte(f);
784 n->nobcast = qemu_get_byte(f);
785 }
786
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100787 if (version_id >= 11) {
788 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
789 qemu_error("virtio-net: saved image requires TUN_F_UFO support\n");
790 return -1;
791 }
792 }
793
Alex Williamson2d9aba32009-06-05 14:47:13 -0600794 /* Find the first multicast entry in the saved MAC filter */
795 for (i = 0; i < n->mac_table.in_use; i++) {
796 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
797 break;
798 }
799 }
800 n->mac_table.first_multi = i;
801
aliguorifbe78f42008-12-17 19:13:11 +0000802 if (n->tx_timer_active) {
803 qemu_mod_timer(n->tx_timer,
804 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
805 }
806
807 return 0;
808}
809
aliguorib946a152009-04-17 17:11:08 +0000810static void virtio_net_cleanup(VLANClientState *vc)
811{
812 VirtIONet *n = vc->opaque;
813
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200814 n->vc = NULL;
aliguorib946a152009-04-17 17:11:08 +0000815}
816
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200817VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
aliguorifbe78f42008-12-17 19:13:11 +0000818{
819 VirtIONet *n;
820 static int virtio_net_id;
821
Paul Brook53c25ce2009-05-18 14:51:59 +0100822 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
823 sizeof(struct virtio_net_config),
824 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000825
aliguori0f03eca2009-02-05 22:36:08 +0000826 n->vdev.get_config = virtio_net_get_config;
827 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000828 n->vdev.get_features = virtio_net_get_features;
829 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000830 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000831 n->vdev.reset = virtio_net_reset;
aliguorifbe78f42008-12-17 19:13:11 +0000832 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
833 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
Alex Williamson4ffb17f2009-06-05 14:47:23 -0600834 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200835 qemu_macaddr_default_if_unset(&conf->macaddr);
aliguori554c97d2009-01-08 19:46:33 +0000836 n->status = VIRTIO_NET_S_LINK_UP;
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200837 n->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_NIC, conf->vlan, conf->peer,
838 dev->info->name, dev->id,
aliguorib946a152009-04-17 17:11:08 +0000839 virtio_net_can_receive,
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200840 virtio_net_receive, NULL, NULL,
aliguorib946a152009-04-17 17:11:08 +0000841 virtio_net_cleanup, n);
aliguori554c97d2009-01-08 19:46:33 +0000842 n->vc->link_status_changed = virtio_net_set_link_status;
aliguorifbe78f42008-12-17 19:13:11 +0000843
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200844 qemu_format_nic_info_str(n->vc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +0000845
aliguorifbe78f42008-12-17 19:13:11 +0000846 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
847 n->tx_timer_active = 0;
848 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000849 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000850
aliguorib6503ed2009-02-05 22:36:28 +0000851 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000852
aliguorif21c0ed2009-02-05 22:36:32 +0000853 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000854
aliguori9d6271b2009-02-05 22:36:04 +0000855 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000856 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100857
Paul Brook53c25ce2009-05-18 14:51:59 +0100858 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100859}
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200860
861void virtio_net_exit(VirtIODevice *vdev)
862{
863 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
864
865 qemu_purge_queued_packets(n->vc);
866
867 unregister_savevm("virtio-net", n);
868
869 qemu_free(n->mac_table.macs);
870 qemu_free(n->vlans);
871
872 qemu_del_timer(n->tx_timer);
873 qemu_free_timer(n->tx_timer);
874
875 virtio_cleanup(&n->vdev);
876 qemu_del_vlan_client(n->vc);
877}