blob: 30e2fe9627fcee977a3238517a4660cfe335e197 [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;
aliguorifbe78f42008-12-17 19:13:11 +000034 VLANClientState *vc;
35 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);
aliguori0f03eca2009-02-05 22:36:08 +000088 qemu_format_nic_info_str(n->vc, n->mac);
89 }
90}
91
aliguori554c97d2009-01-08 19:46:33 +000092static void virtio_net_set_link_status(VLANClientState *vc)
93{
94 VirtIONet *n = vc->opaque;
95 uint16_t old_status = n->status;
96
97 if (vc->link_down)
98 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{
129 if (!n->vc->peer)
130 return 0;
131
132 if (n->vc->peer->type != NET_CLIENT_TYPE_TAP)
133 return 0;
134
135 n->has_vnet_hdr = tap_has_vnet_hdr(n->vc->peer);
136
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
145 n->has_ufo = tap_has_ufo(n->vc->peer);
146
147 return n->has_ufo;
148}
149
aliguorifbe78f42008-12-17 19:13:11 +0000150static uint32_t virtio_net_get_features(VirtIODevice *vdev)
151{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100152 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000153 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
Mark McLoughline16044e2009-06-18 11:31:07 +0100154 (1 << VIRTIO_NET_F_MRG_RXBUF) |
aliguori3d11d362009-02-05 22:36:16 +0000155 (1 << VIRTIO_NET_F_STATUS) |
aliguorib6503ed2009-02-05 22:36:28 +0000156 (1 << VIRTIO_NET_F_CTRL_VQ) |
aliguorif21c0ed2009-02-05 22:36:32 +0000157 (1 << VIRTIO_NET_F_CTRL_RX) |
Alex Williamson015cb162009-06-05 14:47:18 -0600158 (1 << VIRTIO_NET_F_CTRL_VLAN) |
159 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
aliguorifbe78f42008-12-17 19:13:11 +0000160
Mark McLoughlin3a330132009-10-22 17:43:45 +0100161 if (peer_has_vnet_hdr(n)) {
162 tap_using_vnet_hdr(n->vc->peer, 1);
163
164 features |= (1 << VIRTIO_NET_F_CSUM);
165 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
166 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
167 features |= (1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100168
169 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
170 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
171 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
172 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100173
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100174 if (peer_has_ufo(n)) {
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100175 features |= (1 << VIRTIO_NET_F_GUEST_UFO);
176 features |= (1 << VIRTIO_NET_F_HOST_UFO);
177 }
Mark McLoughlin3a330132009-10-22 17:43:45 +0100178 }
179
aliguorifbe78f42008-12-17 19:13:11 +0000180 return features;
181}
182
aliguori8eca6b12009-04-05 17:40:08 +0000183static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
184{
185 uint32_t features = 0;
186
187 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
188 * but also these: */
189 features |= (1 << VIRTIO_NET_F_MAC);
190 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
191 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
192 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
193 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
194
195 return features & virtio_net_get_features(vdev);
196}
197
aliguorifbe78f42008-12-17 19:13:11 +0000198static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
199{
200 VirtIONet *n = to_virtio_net(vdev);
201
202 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100203
204 if (n->has_vnet_hdr) {
205 tap_set_offload(n->vc->peer,
206 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
207 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
208 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100209 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
210 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100211 }
aliguorifbe78f42008-12-17 19:13:11 +0000212}
213
aliguori002437c2009-02-05 22:36:20 +0000214static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
215 VirtQueueElement *elem)
216{
217 uint8_t on;
218
219 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
220 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
221 exit(1);
222 }
223
224 on = ldub_p(elem->out_sg[1].iov_base);
225
226 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
227 n->promisc = on;
228 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
229 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600230 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
231 n->alluni = on;
232 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
233 n->nomulti = on;
234 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
235 n->nouni = on;
236 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
237 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000238 else
239 return VIRTIO_NET_ERR;
240
241 return VIRTIO_NET_OK;
242}
243
aliguorib6503ed2009-02-05 22:36:28 +0000244static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
245 VirtQueueElement *elem)
246{
247 struct virtio_net_ctrl_mac mac_data;
248
249 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
250 elem->out_sg[1].iov_len < sizeof(mac_data) ||
251 elem->out_sg[2].iov_len < sizeof(mac_data))
252 return VIRTIO_NET_ERR;
253
254 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600255 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600256 n->mac_table.uni_overflow = 0;
257 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000258 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
259
260 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
261
262 if (sizeof(mac_data.entries) +
263 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
264 return VIRTIO_NET_ERR;
265
266 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
267 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
268 mac_data.entries * ETH_ALEN);
269 n->mac_table.in_use += mac_data.entries;
270 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600271 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000272 }
273
Alex Williamson2d9aba32009-06-05 14:47:13 -0600274 n->mac_table.first_multi = n->mac_table.in_use;
275
aliguorib6503ed2009-02-05 22:36:28 +0000276 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
277
278 if (sizeof(mac_data.entries) +
279 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
280 return VIRTIO_NET_ERR;
281
282 if (mac_data.entries) {
283 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
284 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
285 elem->out_sg[2].iov_base + sizeof(mac_data),
286 mac_data.entries * ETH_ALEN);
287 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600288 } else {
289 n->mac_table.multi_overflow = 1;
290 }
aliguorib6503ed2009-02-05 22:36:28 +0000291 }
292
293 return VIRTIO_NET_OK;
294}
295
aliguorif21c0ed2009-02-05 22:36:32 +0000296static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
297 VirtQueueElement *elem)
298{
299 uint16_t vid;
300
301 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
302 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
303 return VIRTIO_NET_ERR;
304 }
305
306 vid = lduw_le_p(elem->out_sg[1].iov_base);
307
308 if (vid >= MAX_VLAN)
309 return VIRTIO_NET_ERR;
310
311 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
312 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
313 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
314 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
315 else
316 return VIRTIO_NET_ERR;
317
318 return VIRTIO_NET_OK;
319}
320
aliguori3d11d362009-02-05 22:36:16 +0000321static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
322{
aliguori002437c2009-02-05 22:36:20 +0000323 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000324 struct virtio_net_ctrl_hdr ctrl;
325 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
326 VirtQueueElement elem;
327
328 while (virtqueue_pop(vq, &elem)) {
329 if ((elem.in_num < 1) || (elem.out_num < 1)) {
330 fprintf(stderr, "virtio-net ctrl missing headers\n");
331 exit(1);
332 }
333
334 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000335 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000336 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
337 exit(1);
338 }
339
340 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
341 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
342
aliguori002437c2009-02-05 22:36:20 +0000343 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
344 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000345 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
346 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000347 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
348 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000349
aliguori3d11d362009-02-05 22:36:16 +0000350 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
351
352 virtqueue_push(vq, &elem, sizeof(status));
353 virtio_notify(vdev, vq);
354 }
355}
356
aliguorifbe78f42008-12-17 19:13:11 +0000357/* RX */
358
359static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
360{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100361 VirtIONet *n = to_virtio_net(vdev);
362
363 qemu_flush_queued_packets(n->vc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400364
365 /* We now have RX buffers, signal to the IO thread to break out of the
366 * select to re-poll the tap file descriptor */
367 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000368}
369
370static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
371{
372 if (!virtio_queue_ready(n->rx_vq) ||
373 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
374 return 0;
375
376 if (virtio_queue_empty(n->rx_vq) ||
377 (n->mergeable_rx_bufs &&
378 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
379 virtio_queue_set_notification(n->rx_vq, 1);
380 return 0;
381 }
382
383 virtio_queue_set_notification(n->rx_vq, 0);
384 return 1;
385}
386
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100387static int virtio_net_can_receive(VLANClientState *vc)
aliguorifbe78f42008-12-17 19:13:11 +0000388{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100389 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000390
391 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
392}
393
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100394/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
395 * it never finds out that the packets don't have valid checksums. This
396 * causes dhclient to get upset. Fedora's carried a patch for ages to
397 * fix this with Xen but it hasn't appeared in an upstream release of
398 * dhclient yet.
399 *
400 * To avoid breaking existing guests, we catch udp packets and add
401 * checksums. This is terrible but it's better than hacking the guest
402 * kernels.
403 *
404 * N.B. if we introduce a zero-copy API, this operation is no longer free so
405 * we should provide a mechanism to disable it to avoid polluting the host
406 * cache.
407 */
408static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
409 const uint8_t *buf, size_t size)
410{
411 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
412 (size > 27 && size < 1500) && /* normal sized MTU */
413 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
414 (buf[23] == 17) && /* ip.protocol == UDP */
415 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
416 /* FIXME this cast is evil */
417 net_checksum_calculate((uint8_t *)buf, size);
418 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
419 }
420}
421
aliguorifbe78f42008-12-17 19:13:11 +0000422static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
423{
424 int offset, i;
425
426 offset = i = 0;
427 while (offset < count && i < iovcnt) {
428 int len = MIN(iov[i].iov_len, count - offset);
429 memcpy(iov[i].iov_base, buf + offset, len);
430 offset += len;
431 i++;
432 }
433
434 return offset;
435}
436
437static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000438 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000439{
blueswir13f4cb3d2009-04-13 16:31:01 +0000440 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000441 int offset = 0;
442
443 hdr->flags = 0;
444 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
445
Mark McLoughlin3a330132009-10-22 17:43:45 +0100446 if (n->has_vnet_hdr) {
447 memcpy(hdr, buf, sizeof(*hdr));
448 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100449 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100450 }
451
aliguorifbe78f42008-12-17 19:13:11 +0000452 /* We only ever receive a struct virtio_net_hdr from the tapfd,
453 * but we may be passing along a larger header to the guest.
454 */
455 iov[0].iov_base += hdr_len;
456 iov[0].iov_len -= hdr_len;
457
458 return offset;
459}
460
aliguori3831ab22009-02-05 22:36:24 +0000461static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
462{
463 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000464 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000465 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000466 int i;
aliguori3831ab22009-02-05 22:36:24 +0000467
468 if (n->promisc)
469 return 1;
470
Mark McLoughlin3a330132009-10-22 17:43:45 +0100471 if (n->has_vnet_hdr) {
472 ptr += sizeof(struct virtio_net_hdr);
473 }
474
aliguorif21c0ed2009-02-05 22:36:32 +0000475 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
476 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
477 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
478 return 0;
479 }
480
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600481 if (ptr[0] & 1) { // multicast
482 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600483 return !n->nobcast;
484 } else if (n->nomulti) {
485 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600486 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600487 return 1;
488 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600489
490 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
491 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
492 return 1;
493 }
494 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600495 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600496 if (n->nouni) {
497 return 0;
498 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600499 return 1;
500 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600501 return 1;
502 }
aliguori3831ab22009-02-05 22:36:24 +0000503
Alex Williamson2d9aba32009-06-05 14:47:13 -0600504 for (i = 0; i < n->mac_table.first_multi; i++) {
505 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
506 return 1;
507 }
508 }
aliguorib6503ed2009-02-05 22:36:28 +0000509 }
510
aliguori3831ab22009-02-05 22:36:24 +0000511 return 0;
512}
513
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100514static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000515{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100516 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000517 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000518 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000519
520 if (!do_virtio_net_can_receive(n, size))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100521 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000522
aliguori3831ab22009-02-05 22:36:24 +0000523 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100524 return size;
aliguori3831ab22009-02-05 22:36:24 +0000525
aliguorifbe78f42008-12-17 19:13:11 +0000526 /* hdr_len refers to the header we supply to the guest */
527 hdr_len = n->mergeable_rx_bufs ?
528 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
529
530 offset = i = 0;
531
532 while (offset < size) {
533 VirtQueueElement elem;
534 int len, total;
535 struct iovec sg[VIRTQUEUE_MAX_SIZE];
536
537 len = total = 0;
538
539 if ((i != 0 && !n->mergeable_rx_bufs) ||
540 virtqueue_pop(n->rx_vq, &elem) == 0) {
541 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100542 return -1;
aliguorifbe78f42008-12-17 19:13:11 +0000543 fprintf(stderr, "virtio-net truncating packet\n");
544 exit(1);
545 }
546
547 if (elem.in_num < 1) {
548 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
549 exit(1);
550 }
551
552 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
553 fprintf(stderr, "virtio-net header not in first element\n");
554 exit(1);
555 }
556
557 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
558
559 if (i == 0) {
560 if (n->mergeable_rx_bufs)
561 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
562
563 offset += receive_header(n, sg, elem.in_num,
564 buf + offset, size - offset, hdr_len);
565 total += hdr_len;
566 }
567
568 /* copy in packet. ugh */
569 len = iov_fill(sg, elem.in_num,
570 buf + offset, size - offset);
571 total += len;
572
573 /* signal other side */
574 virtqueue_fill(n->rx_vq, &elem, total, i++);
575
576 offset += len;
577 }
578
579 if (mhdr)
580 mhdr->num_buffers = i;
581
582 virtqueue_flush(n->rx_vq, i);
583 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100584
585 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000586}
587
Mark McLoughlin62433752009-06-18 18:21:36 +0100588static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
589
590static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len)
591{
592 VirtIONet *n = vc->opaque;
593
594 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
595 virtio_notify(&n->vdev, n->tx_vq);
596
597 n->async_tx.elem.out_num = n->async_tx.len = 0;
598
599 virtio_queue_set_notification(n->tx_vq, 1);
600 virtio_net_flush_tx(n, n->tx_vq);
601}
602
aliguorifbe78f42008-12-17 19:13:11 +0000603/* TX */
604static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
605{
606 VirtQueueElement elem;
aliguorifbe78f42008-12-17 19:13:11 +0000607
608 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
609 return;
610
Mark McLoughlin62433752009-06-18 18:21:36 +0100611 if (n->async_tx.elem.out_num) {
612 virtio_queue_set_notification(n->tx_vq, 0);
613 return;
614 }
615
aliguorifbe78f42008-12-17 19:13:11 +0000616 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100617 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000618 unsigned int out_num = elem.out_num;
619 struct iovec *out_sg = &elem.out_sg[0];
620 unsigned hdr_len;
621
622 /* hdr_len refers to the header received from the guest */
623 hdr_len = n->mergeable_rx_bufs ?
624 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
625 sizeof(struct virtio_net_hdr);
626
627 if (out_num < 1 || out_sg->iov_len != hdr_len) {
628 fprintf(stderr, "virtio-net header not in first element\n");
629 exit(1);
630 }
631
632 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100633 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000634 out_num--;
635 out_sg++;
636 len += hdr_len;
637 } else if (n->mergeable_rx_bufs) {
638 /* tapfd expects a struct virtio_net_hdr */
639 hdr_len -= sizeof(struct virtio_net_hdr);
640 out_sg->iov_len -= hdr_len;
641 len += hdr_len;
642 }
643
Mark McLoughlin62433752009-06-18 18:21:36 +0100644 ret = qemu_sendv_packet_async(n->vc, out_sg, out_num,
645 virtio_net_tx_complete);
646 if (ret == 0) {
647 virtio_queue_set_notification(n->tx_vq, 0);
648 n->async_tx.elem = elem;
649 n->async_tx.len = len;
650 return;
651 }
652
653 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000654
655 virtqueue_push(vq, &elem, len);
656 virtio_notify(&n->vdev, vq);
657 }
658}
659
660static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
661{
662 VirtIONet *n = to_virtio_net(vdev);
663
664 if (n->tx_timer_active) {
665 virtio_queue_set_notification(vq, 1);
666 qemu_del_timer(n->tx_timer);
667 n->tx_timer_active = 0;
668 virtio_net_flush_tx(n, vq);
669 } else {
670 qemu_mod_timer(n->tx_timer,
671 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
672 n->tx_timer_active = 1;
673 virtio_queue_set_notification(vq, 0);
674 }
675}
676
677static void virtio_net_tx_timer(void *opaque)
678{
679 VirtIONet *n = opaque;
680
681 n->tx_timer_active = 0;
682
683 /* Just in case the driver is not ready on more */
684 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
685 return;
686
687 virtio_queue_set_notification(n->tx_vq, 1);
688 virtio_net_flush_tx(n, n->tx_vq);
689}
690
691static void virtio_net_save(QEMUFile *f, void *opaque)
692{
693 VirtIONet *n = opaque;
694
695 virtio_save(&n->vdev, f);
696
aliguori79674062009-02-05 22:36:12 +0000697 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000698 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000699 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000700 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600701 qemu_put_byte(f, n->promisc);
702 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000703 qemu_put_be32(f, n->mac_table.in_use);
704 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000705 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100706 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600707 qemu_put_byte(f, n->mac_table.multi_overflow);
708 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600709 qemu_put_byte(f, n->alluni);
710 qemu_put_byte(f, n->nomulti);
711 qemu_put_byte(f, n->nouni);
712 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100713 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000714}
715
716static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
717{
718 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600719 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000720
aliguori9d6271b2009-02-05 22:36:04 +0000721 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000722 return -EINVAL;
723
724 virtio_load(&n->vdev, f);
725
aliguori79674062009-02-05 22:36:12 +0000726 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000727 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000728 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000729
aliguori9d6271b2009-02-05 22:36:04 +0000730 if (version_id >= 3)
731 n->status = qemu_get_be16(f);
732
aliguori002437c2009-02-05 22:36:20 +0000733 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600734 if (version_id < 8) {
735 n->promisc = qemu_get_be32(f);
736 n->allmulti = qemu_get_be32(f);
737 } else {
738 n->promisc = qemu_get_byte(f);
739 n->allmulti = qemu_get_byte(f);
740 }
aliguori002437c2009-02-05 22:36:20 +0000741 }
742
aliguorib6503ed2009-02-05 22:36:28 +0000743 if (version_id >= 5) {
744 n->mac_table.in_use = qemu_get_be32(f);
745 /* MAC_TABLE_ENTRIES may be different from the saved image */
746 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
747 qemu_get_buffer(f, n->mac_table.macs,
748 n->mac_table.in_use * ETH_ALEN);
749 } else if (n->mac_table.in_use) {
750 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600751 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000752 n->mac_table.in_use = 0;
753 }
754 }
755
aliguorif21c0ed2009-02-05 22:36:32 +0000756 if (version_id >= 6)
757 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
758
Mark McLoughlin3a330132009-10-22 17:43:45 +0100759 if (version_id >= 7) {
760 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
761 qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
762 return -1;
763 }
764
765 if (n->has_vnet_hdr) {
766 tap_using_vnet_hdr(n->vc->peer, 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100767 tap_set_offload(n->vc->peer,
768 (n->vdev.features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
769 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
770 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100771 (n->vdev.features >> VIRTIO_NET_F_GUEST_ECN) & 1,
772 (n->vdev.features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100773 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600774 }
775
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600776 if (version_id >= 9) {
777 n->mac_table.multi_overflow = qemu_get_byte(f);
778 n->mac_table.uni_overflow = qemu_get_byte(f);
779 }
780
Alex Williamson015cb162009-06-05 14:47:18 -0600781 if (version_id >= 10) {
782 n->alluni = qemu_get_byte(f);
783 n->nomulti = qemu_get_byte(f);
784 n->nouni = qemu_get_byte(f);
785 n->nobcast = qemu_get_byte(f);
786 }
787
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100788 if (version_id >= 11) {
789 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
790 qemu_error("virtio-net: saved image requires TUN_F_UFO support\n");
791 return -1;
792 }
793 }
794
Alex Williamson2d9aba32009-06-05 14:47:13 -0600795 /* Find the first multicast entry in the saved MAC filter */
796 for (i = 0; i < n->mac_table.in_use; i++) {
797 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
798 break;
799 }
800 }
801 n->mac_table.first_multi = i;
802
aliguorifbe78f42008-12-17 19:13:11 +0000803 if (n->tx_timer_active) {
804 qemu_mod_timer(n->tx_timer,
805 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
806 }
807
808 return 0;
809}
810
aliguorib946a152009-04-17 17:11:08 +0000811static void virtio_net_cleanup(VLANClientState *vc)
812{
813 VirtIONet *n = vc->opaque;
814
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200815 n->vc = NULL;
aliguorib946a152009-04-17 17:11:08 +0000816}
817
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200818VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
aliguorifbe78f42008-12-17 19:13:11 +0000819{
820 VirtIONet *n;
821 static int virtio_net_id;
822
Paul Brook53c25ce2009-05-18 14:51:59 +0100823 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
824 sizeof(struct virtio_net_config),
825 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000826
aliguori0f03eca2009-02-05 22:36:08 +0000827 n->vdev.get_config = virtio_net_get_config;
828 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000829 n->vdev.get_features = virtio_net_get_features;
830 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000831 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000832 n->vdev.reset = virtio_net_reset;
aliguorifbe78f42008-12-17 19:13:11 +0000833 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
834 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
Alex Williamson4ffb17f2009-06-05 14:47:23 -0600835 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200836 qemu_macaddr_default_if_unset(&conf->macaddr);
aliguori554c97d2009-01-08 19:46:33 +0000837 n->status = VIRTIO_NET_S_LINK_UP;
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200838 n->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_NIC, conf->vlan, conf->peer,
839 dev->info->name, dev->id,
aliguorib946a152009-04-17 17:11:08 +0000840 virtio_net_can_receive,
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200841 virtio_net_receive, NULL, NULL,
aliguorib946a152009-04-17 17:11:08 +0000842 virtio_net_cleanup, n);
aliguori554c97d2009-01-08 19:46:33 +0000843 n->vc->link_status_changed = virtio_net_set_link_status;
aliguorifbe78f42008-12-17 19:13:11 +0000844
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200845 qemu_format_nic_info_str(n->vc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +0000846
aliguorifbe78f42008-12-17 19:13:11 +0000847 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
848 n->tx_timer_active = 0;
849 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000850 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000851
aliguorib6503ed2009-02-05 22:36:28 +0000852 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000853
aliguorif21c0ed2009-02-05 22:36:32 +0000854 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000855
aliguori9d6271b2009-02-05 22:36:04 +0000856 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000857 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100858
Paul Brook53c25ce2009-05-18 14:51:59 +0100859 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100860}
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200861
862void virtio_net_exit(VirtIODevice *vdev)
863{
864 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
865
866 qemu_purge_queued_packets(n->vc);
867
868 unregister_savevm("virtio-net", n);
869
870 qemu_free(n->mac_table.macs);
871 qemu_free(n->vlans);
872
873 qemu_del_timer(n->tx_timer);
874 qemu_free_timer(n->tx_timer);
875
876 virtio_cleanup(&n->vdev);
877 qemu_del_vlan_client(n->vc);
878}