blob: 064ec2e609ab9c89198458a2f0b5904198b3ffca [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"
16#include "qemu-timer.h"
17#include "virtio-net.h"
18
Alex Williamson015cb162009-06-05 14:47:18 -060019#define VIRTIO_NET_VM_VERSION 10
aliguorib6503ed2009-02-05 22:36:28 +000020
Alex Williamson4ffb17f2009-06-05 14:47:23 -060021#define MAC_TABLE_ENTRIES 64
aliguorif21c0ed2009-02-05 22:36:32 +000022#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
aliguori9d6271b2009-02-05 22:36:04 +000023
aliguorifbe78f42008-12-17 19:13:11 +000024typedef struct VirtIONet
25{
26 VirtIODevice vdev;
aliguori79674062009-02-05 22:36:12 +000027 uint8_t mac[ETH_ALEN];
aliguori554c97d2009-01-08 19:46:33 +000028 uint16_t status;
aliguorifbe78f42008-12-17 19:13:11 +000029 VirtQueue *rx_vq;
30 VirtQueue *tx_vq;
aliguori3d11d362009-02-05 22:36:16 +000031 VirtQueue *ctrl_vq;
aliguorifbe78f42008-12-17 19:13:11 +000032 VLANClientState *vc;
33 QEMUTimer *tx_timer;
34 int tx_timer_active;
Mark McLoughlin3a330132009-10-22 17:43:45 +010035 uint32_t has_vnet_hdr;
Mark McLoughlin62433752009-06-18 18:21:36 +010036 struct {
37 VirtQueueElement elem;
38 ssize_t len;
39 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000040 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060041 uint8_t promisc;
42 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060043 uint8_t alluni;
44 uint8_t nomulti;
45 uint8_t nouni;
46 uint8_t nobcast;
aliguorib6503ed2009-02-05 22:36:28 +000047 struct {
48 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060049 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060050 uint8_t multi_overflow;
51 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000052 uint8_t *macs;
53 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000054 uint32_t *vlans;
aliguorifbe78f42008-12-17 19:13:11 +000055} VirtIONet;
56
57/* TODO
58 * - we could suppress RX interrupt if we were so inclined.
59 */
60
61static VirtIONet *to_virtio_net(VirtIODevice *vdev)
62{
63 return (VirtIONet *)vdev;
64}
65
aliguori0f03eca2009-02-05 22:36:08 +000066static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000067{
68 VirtIONet *n = to_virtio_net(vdev);
69 struct virtio_net_config netcfg;
70
aliguori554c97d2009-01-08 19:46:33 +000071 netcfg.status = n->status;
aliguori79674062009-02-05 22:36:12 +000072 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000073 memcpy(config, &netcfg, sizeof(netcfg));
74}
75
aliguori0f03eca2009-02-05 22:36:08 +000076static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
77{
78 VirtIONet *n = to_virtio_net(vdev);
79 struct virtio_net_config netcfg;
80
81 memcpy(&netcfg, config, sizeof(netcfg));
82
aliguori79674062009-02-05 22:36:12 +000083 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
84 memcpy(n->mac, netcfg.mac, ETH_ALEN);
aliguori0f03eca2009-02-05 22:36:08 +000085 qemu_format_nic_info_str(n->vc, n->mac);
86 }
87}
88
aliguori554c97d2009-01-08 19:46:33 +000089static void virtio_net_set_link_status(VLANClientState *vc)
90{
91 VirtIONet *n = vc->opaque;
92 uint16_t old_status = n->status;
93
94 if (vc->link_down)
95 n->status &= ~VIRTIO_NET_S_LINK_UP;
96 else
97 n->status |= VIRTIO_NET_S_LINK_UP;
98
99 if (n->status != old_status)
100 virtio_notify_config(&n->vdev);
101}
102
aliguori002437c2009-02-05 22:36:20 +0000103static void virtio_net_reset(VirtIODevice *vdev)
104{
105 VirtIONet *n = to_virtio_net(vdev);
106
107 /* Reset back to compatibility mode */
108 n->promisc = 1;
109 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600110 n->alluni = 0;
111 n->nomulti = 0;
112 n->nouni = 0;
113 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000114
aliguorif21c0ed2009-02-05 22:36:32 +0000115 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000116 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600117 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600118 n->mac_table.multi_overflow = 0;
119 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000120 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000121 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000122}
123
Mark McLoughlin3a330132009-10-22 17:43:45 +0100124static int peer_has_vnet_hdr(VirtIONet *n)
125{
126 if (!n->vc->peer)
127 return 0;
128
129 if (n->vc->peer->type != NET_CLIENT_TYPE_TAP)
130 return 0;
131
132 n->has_vnet_hdr = tap_has_vnet_hdr(n->vc->peer);
133
134 return n->has_vnet_hdr;
135}
136
aliguorifbe78f42008-12-17 19:13:11 +0000137static uint32_t virtio_net_get_features(VirtIODevice *vdev)
138{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100139 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000140 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
Mark McLoughline16044e2009-06-18 11:31:07 +0100141 (1 << VIRTIO_NET_F_MRG_RXBUF) |
aliguori3d11d362009-02-05 22:36:16 +0000142 (1 << VIRTIO_NET_F_STATUS) |
aliguorib6503ed2009-02-05 22:36:28 +0000143 (1 << VIRTIO_NET_F_CTRL_VQ) |
aliguorif21c0ed2009-02-05 22:36:32 +0000144 (1 << VIRTIO_NET_F_CTRL_RX) |
Alex Williamson015cb162009-06-05 14:47:18 -0600145 (1 << VIRTIO_NET_F_CTRL_VLAN) |
146 (1 << VIRTIO_NET_F_CTRL_RX_EXTRA);
aliguorifbe78f42008-12-17 19:13:11 +0000147
Mark McLoughlin3a330132009-10-22 17:43:45 +0100148 if (peer_has_vnet_hdr(n)) {
149 tap_using_vnet_hdr(n->vc->peer, 1);
150
151 features |= (1 << VIRTIO_NET_F_CSUM);
152 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
153 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
154 features |= (1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100155
156 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
157 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
158 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
159 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100160 }
161
aliguorifbe78f42008-12-17 19:13:11 +0000162 return features;
163}
164
aliguori8eca6b12009-04-05 17:40:08 +0000165static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
166{
167 uint32_t features = 0;
168
169 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
170 * but also these: */
171 features |= (1 << VIRTIO_NET_F_MAC);
172 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
173 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
174 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
175 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
176
177 return features & virtio_net_get_features(vdev);
178}
179
aliguorifbe78f42008-12-17 19:13:11 +0000180static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
181{
182 VirtIONet *n = to_virtio_net(vdev);
183
184 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100185
186 if (n->has_vnet_hdr) {
187 tap_set_offload(n->vc->peer,
188 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
189 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
190 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
191 (features >> VIRTIO_NET_F_GUEST_ECN) & 1);
192 }
aliguorifbe78f42008-12-17 19:13:11 +0000193}
194
aliguori002437c2009-02-05 22:36:20 +0000195static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
196 VirtQueueElement *elem)
197{
198 uint8_t on;
199
200 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
201 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
202 exit(1);
203 }
204
205 on = ldub_p(elem->out_sg[1].iov_base);
206
207 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
208 n->promisc = on;
209 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
210 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600211 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
212 n->alluni = on;
213 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
214 n->nomulti = on;
215 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
216 n->nouni = on;
217 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
218 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000219 else
220 return VIRTIO_NET_ERR;
221
222 return VIRTIO_NET_OK;
223}
224
aliguorib6503ed2009-02-05 22:36:28 +0000225static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
226 VirtQueueElement *elem)
227{
228 struct virtio_net_ctrl_mac mac_data;
229
230 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
231 elem->out_sg[1].iov_len < sizeof(mac_data) ||
232 elem->out_sg[2].iov_len < sizeof(mac_data))
233 return VIRTIO_NET_ERR;
234
235 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600236 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600237 n->mac_table.uni_overflow = 0;
238 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000239 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
240
241 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
242
243 if (sizeof(mac_data.entries) +
244 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
245 return VIRTIO_NET_ERR;
246
247 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
248 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
249 mac_data.entries * ETH_ALEN);
250 n->mac_table.in_use += mac_data.entries;
251 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600252 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000253 }
254
Alex Williamson2d9aba32009-06-05 14:47:13 -0600255 n->mac_table.first_multi = n->mac_table.in_use;
256
aliguorib6503ed2009-02-05 22:36:28 +0000257 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
258
259 if (sizeof(mac_data.entries) +
260 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
261 return VIRTIO_NET_ERR;
262
263 if (mac_data.entries) {
264 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
265 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
266 elem->out_sg[2].iov_base + sizeof(mac_data),
267 mac_data.entries * ETH_ALEN);
268 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600269 } else {
270 n->mac_table.multi_overflow = 1;
271 }
aliguorib6503ed2009-02-05 22:36:28 +0000272 }
273
274 return VIRTIO_NET_OK;
275}
276
aliguorif21c0ed2009-02-05 22:36:32 +0000277static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
278 VirtQueueElement *elem)
279{
280 uint16_t vid;
281
282 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
283 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
284 return VIRTIO_NET_ERR;
285 }
286
287 vid = lduw_le_p(elem->out_sg[1].iov_base);
288
289 if (vid >= MAX_VLAN)
290 return VIRTIO_NET_ERR;
291
292 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
293 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
294 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
295 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
296 else
297 return VIRTIO_NET_ERR;
298
299 return VIRTIO_NET_OK;
300}
301
aliguori3d11d362009-02-05 22:36:16 +0000302static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
303{
aliguori002437c2009-02-05 22:36:20 +0000304 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000305 struct virtio_net_ctrl_hdr ctrl;
306 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
307 VirtQueueElement elem;
308
309 while (virtqueue_pop(vq, &elem)) {
310 if ((elem.in_num < 1) || (elem.out_num < 1)) {
311 fprintf(stderr, "virtio-net ctrl missing headers\n");
312 exit(1);
313 }
314
315 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000316 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000317 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
318 exit(1);
319 }
320
321 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
322 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
323
aliguori002437c2009-02-05 22:36:20 +0000324 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
325 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000326 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
327 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000328 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
329 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000330
aliguori3d11d362009-02-05 22:36:16 +0000331 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
332
333 virtqueue_push(vq, &elem, sizeof(status));
334 virtio_notify(vdev, vq);
335 }
336}
337
aliguorifbe78f42008-12-17 19:13:11 +0000338/* RX */
339
340static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
341{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100342 VirtIONet *n = to_virtio_net(vdev);
343
344 qemu_flush_queued_packets(n->vc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400345
346 /* We now have RX buffers, signal to the IO thread to break out of the
347 * select to re-poll the tap file descriptor */
348 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000349}
350
351static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
352{
353 if (!virtio_queue_ready(n->rx_vq) ||
354 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
355 return 0;
356
357 if (virtio_queue_empty(n->rx_vq) ||
358 (n->mergeable_rx_bufs &&
359 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
360 virtio_queue_set_notification(n->rx_vq, 1);
361 return 0;
362 }
363
364 virtio_queue_set_notification(n->rx_vq, 0);
365 return 1;
366}
367
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100368static int virtio_net_can_receive(VLANClientState *vc)
aliguorifbe78f42008-12-17 19:13:11 +0000369{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100370 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000371
372 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
373}
374
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100375/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
376 * it never finds out that the packets don't have valid checksums. This
377 * causes dhclient to get upset. Fedora's carried a patch for ages to
378 * fix this with Xen but it hasn't appeared in an upstream release of
379 * dhclient yet.
380 *
381 * To avoid breaking existing guests, we catch udp packets and add
382 * checksums. This is terrible but it's better than hacking the guest
383 * kernels.
384 *
385 * N.B. if we introduce a zero-copy API, this operation is no longer free so
386 * we should provide a mechanism to disable it to avoid polluting the host
387 * cache.
388 */
389static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
390 const uint8_t *buf, size_t size)
391{
392 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
393 (size > 27 && size < 1500) && /* normal sized MTU */
394 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
395 (buf[23] == 17) && /* ip.protocol == UDP */
396 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
397 /* FIXME this cast is evil */
398 net_checksum_calculate((uint8_t *)buf, size);
399 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
400 }
401}
402
aliguorifbe78f42008-12-17 19:13:11 +0000403static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
404{
405 int offset, i;
406
407 offset = i = 0;
408 while (offset < count && i < iovcnt) {
409 int len = MIN(iov[i].iov_len, count - offset);
410 memcpy(iov[i].iov_base, buf + offset, len);
411 offset += len;
412 i++;
413 }
414
415 return offset;
416}
417
418static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000419 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000420{
blueswir13f4cb3d2009-04-13 16:31:01 +0000421 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000422 int offset = 0;
423
424 hdr->flags = 0;
425 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
426
Mark McLoughlin3a330132009-10-22 17:43:45 +0100427 if (n->has_vnet_hdr) {
428 memcpy(hdr, buf, sizeof(*hdr));
429 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100430 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100431 }
432
aliguorifbe78f42008-12-17 19:13:11 +0000433 /* We only ever receive a struct virtio_net_hdr from the tapfd,
434 * but we may be passing along a larger header to the guest.
435 */
436 iov[0].iov_base += hdr_len;
437 iov[0].iov_len -= hdr_len;
438
439 return offset;
440}
441
aliguori3831ab22009-02-05 22:36:24 +0000442static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
443{
444 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000445 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000446 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000447 int i;
aliguori3831ab22009-02-05 22:36:24 +0000448
449 if (n->promisc)
450 return 1;
451
Mark McLoughlin3a330132009-10-22 17:43:45 +0100452 if (n->has_vnet_hdr) {
453 ptr += sizeof(struct virtio_net_hdr);
454 }
455
aliguorif21c0ed2009-02-05 22:36:32 +0000456 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
457 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
458 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
459 return 0;
460 }
461
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600462 if (ptr[0] & 1) { // multicast
463 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600464 return !n->nobcast;
465 } else if (n->nomulti) {
466 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600467 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600468 return 1;
469 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600470
471 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
472 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
473 return 1;
474 }
475 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600476 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600477 if (n->nouni) {
478 return 0;
479 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600480 return 1;
481 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600482 return 1;
483 }
aliguori3831ab22009-02-05 22:36:24 +0000484
Alex Williamson2d9aba32009-06-05 14:47:13 -0600485 for (i = 0; i < n->mac_table.first_multi; i++) {
486 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
487 return 1;
488 }
489 }
aliguorib6503ed2009-02-05 22:36:28 +0000490 }
491
aliguori3831ab22009-02-05 22:36:24 +0000492 return 0;
493}
494
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100495static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000496{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100497 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000498 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000499 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000500
501 if (!do_virtio_net_can_receive(n, size))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100502 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000503
aliguori3831ab22009-02-05 22:36:24 +0000504 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100505 return size;
aliguori3831ab22009-02-05 22:36:24 +0000506
aliguorifbe78f42008-12-17 19:13:11 +0000507 /* hdr_len refers to the header we supply to the guest */
508 hdr_len = n->mergeable_rx_bufs ?
509 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
510
511 offset = i = 0;
512
513 while (offset < size) {
514 VirtQueueElement elem;
515 int len, total;
516 struct iovec sg[VIRTQUEUE_MAX_SIZE];
517
518 len = total = 0;
519
520 if ((i != 0 && !n->mergeable_rx_bufs) ||
521 virtqueue_pop(n->rx_vq, &elem) == 0) {
522 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100523 return -1;
aliguorifbe78f42008-12-17 19:13:11 +0000524 fprintf(stderr, "virtio-net truncating packet\n");
525 exit(1);
526 }
527
528 if (elem.in_num < 1) {
529 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
530 exit(1);
531 }
532
533 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
534 fprintf(stderr, "virtio-net header not in first element\n");
535 exit(1);
536 }
537
538 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
539
540 if (i == 0) {
541 if (n->mergeable_rx_bufs)
542 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
543
544 offset += receive_header(n, sg, elem.in_num,
545 buf + offset, size - offset, hdr_len);
546 total += hdr_len;
547 }
548
549 /* copy in packet. ugh */
550 len = iov_fill(sg, elem.in_num,
551 buf + offset, size - offset);
552 total += len;
553
554 /* signal other side */
555 virtqueue_fill(n->rx_vq, &elem, total, i++);
556
557 offset += len;
558 }
559
560 if (mhdr)
561 mhdr->num_buffers = i;
562
563 virtqueue_flush(n->rx_vq, i);
564 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100565
566 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000567}
568
Mark McLoughlin62433752009-06-18 18:21:36 +0100569static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
570
571static void virtio_net_tx_complete(VLANClientState *vc, ssize_t len)
572{
573 VirtIONet *n = vc->opaque;
574
575 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
576 virtio_notify(&n->vdev, n->tx_vq);
577
578 n->async_tx.elem.out_num = n->async_tx.len = 0;
579
580 virtio_queue_set_notification(n->tx_vq, 1);
581 virtio_net_flush_tx(n, n->tx_vq);
582}
583
aliguorifbe78f42008-12-17 19:13:11 +0000584/* TX */
585static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
586{
587 VirtQueueElement elem;
aliguorifbe78f42008-12-17 19:13:11 +0000588
589 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
590 return;
591
Mark McLoughlin62433752009-06-18 18:21:36 +0100592 if (n->async_tx.elem.out_num) {
593 virtio_queue_set_notification(n->tx_vq, 0);
594 return;
595 }
596
aliguorifbe78f42008-12-17 19:13:11 +0000597 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100598 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000599 unsigned int out_num = elem.out_num;
600 struct iovec *out_sg = &elem.out_sg[0];
601 unsigned hdr_len;
602
603 /* hdr_len refers to the header received from the guest */
604 hdr_len = n->mergeable_rx_bufs ?
605 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
606 sizeof(struct virtio_net_hdr);
607
608 if (out_num < 1 || out_sg->iov_len != hdr_len) {
609 fprintf(stderr, "virtio-net header not in first element\n");
610 exit(1);
611 }
612
613 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100614 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000615 out_num--;
616 out_sg++;
617 len += hdr_len;
618 } else if (n->mergeable_rx_bufs) {
619 /* tapfd expects a struct virtio_net_hdr */
620 hdr_len -= sizeof(struct virtio_net_hdr);
621 out_sg->iov_len -= hdr_len;
622 len += hdr_len;
623 }
624
Mark McLoughlin62433752009-06-18 18:21:36 +0100625 ret = qemu_sendv_packet_async(n->vc, out_sg, out_num,
626 virtio_net_tx_complete);
627 if (ret == 0) {
628 virtio_queue_set_notification(n->tx_vq, 0);
629 n->async_tx.elem = elem;
630 n->async_tx.len = len;
631 return;
632 }
633
634 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000635
636 virtqueue_push(vq, &elem, len);
637 virtio_notify(&n->vdev, vq);
638 }
639}
640
641static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
642{
643 VirtIONet *n = to_virtio_net(vdev);
644
645 if (n->tx_timer_active) {
646 virtio_queue_set_notification(vq, 1);
647 qemu_del_timer(n->tx_timer);
648 n->tx_timer_active = 0;
649 virtio_net_flush_tx(n, vq);
650 } else {
651 qemu_mod_timer(n->tx_timer,
652 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
653 n->tx_timer_active = 1;
654 virtio_queue_set_notification(vq, 0);
655 }
656}
657
658static void virtio_net_tx_timer(void *opaque)
659{
660 VirtIONet *n = opaque;
661
662 n->tx_timer_active = 0;
663
664 /* Just in case the driver is not ready on more */
665 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
666 return;
667
668 virtio_queue_set_notification(n->tx_vq, 1);
669 virtio_net_flush_tx(n, n->tx_vq);
670}
671
672static void virtio_net_save(QEMUFile *f, void *opaque)
673{
674 VirtIONet *n = opaque;
675
676 virtio_save(&n->vdev, f);
677
aliguori79674062009-02-05 22:36:12 +0000678 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000679 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000680 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000681 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600682 qemu_put_byte(f, n->promisc);
683 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000684 qemu_put_be32(f, n->mac_table.in_use);
685 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000686 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100687 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600688 qemu_put_byte(f, n->mac_table.multi_overflow);
689 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600690 qemu_put_byte(f, n->alluni);
691 qemu_put_byte(f, n->nomulti);
692 qemu_put_byte(f, n->nouni);
693 qemu_put_byte(f, n->nobcast);
aliguorifbe78f42008-12-17 19:13:11 +0000694}
695
696static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
697{
698 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600699 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000700
aliguori9d6271b2009-02-05 22:36:04 +0000701 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000702 return -EINVAL;
703
704 virtio_load(&n->vdev, f);
705
aliguori79674062009-02-05 22:36:12 +0000706 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000707 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000708 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000709
aliguori9d6271b2009-02-05 22:36:04 +0000710 if (version_id >= 3)
711 n->status = qemu_get_be16(f);
712
aliguori002437c2009-02-05 22:36:20 +0000713 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600714 if (version_id < 8) {
715 n->promisc = qemu_get_be32(f);
716 n->allmulti = qemu_get_be32(f);
717 } else {
718 n->promisc = qemu_get_byte(f);
719 n->allmulti = qemu_get_byte(f);
720 }
aliguori002437c2009-02-05 22:36:20 +0000721 }
722
aliguorib6503ed2009-02-05 22:36:28 +0000723 if (version_id >= 5) {
724 n->mac_table.in_use = qemu_get_be32(f);
725 /* MAC_TABLE_ENTRIES may be different from the saved image */
726 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
727 qemu_get_buffer(f, n->mac_table.macs,
728 n->mac_table.in_use * ETH_ALEN);
729 } else if (n->mac_table.in_use) {
730 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600731 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000732 n->mac_table.in_use = 0;
733 }
734 }
735
aliguorif21c0ed2009-02-05 22:36:32 +0000736 if (version_id >= 6)
737 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
738
Mark McLoughlin3a330132009-10-22 17:43:45 +0100739 if (version_id >= 7) {
740 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
741 qemu_error("virtio-net: saved image requires vnet_hdr=on\n");
742 return -1;
743 }
744
745 if (n->has_vnet_hdr) {
746 tap_using_vnet_hdr(n->vc->peer, 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100747 tap_set_offload(n->vc->peer,
748 (n->vdev.features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
749 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
750 (n->vdev.features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
751 (n->vdev.features >> VIRTIO_NET_F_GUEST_ECN) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100752 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600753 }
754
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600755 if (version_id >= 9) {
756 n->mac_table.multi_overflow = qemu_get_byte(f);
757 n->mac_table.uni_overflow = qemu_get_byte(f);
758 }
759
Alex Williamson015cb162009-06-05 14:47:18 -0600760 if (version_id >= 10) {
761 n->alluni = qemu_get_byte(f);
762 n->nomulti = qemu_get_byte(f);
763 n->nouni = qemu_get_byte(f);
764 n->nobcast = qemu_get_byte(f);
765 }
766
Alex Williamson2d9aba32009-06-05 14:47:13 -0600767 /* Find the first multicast entry in the saved MAC filter */
768 for (i = 0; i < n->mac_table.in_use; i++) {
769 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
770 break;
771 }
772 }
773 n->mac_table.first_multi = i;
774
aliguorifbe78f42008-12-17 19:13:11 +0000775 if (n->tx_timer_active) {
776 qemu_mod_timer(n->tx_timer,
777 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
778 }
779
780 return 0;
781}
782
aliguorib946a152009-04-17 17:11:08 +0000783static void virtio_net_cleanup(VLANClientState *vc)
784{
785 VirtIONet *n = vc->opaque;
786
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200787 n->vc = NULL;
aliguorib946a152009-04-17 17:11:08 +0000788}
789
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200790VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
aliguorifbe78f42008-12-17 19:13:11 +0000791{
792 VirtIONet *n;
793 static int virtio_net_id;
794
Paul Brook53c25ce2009-05-18 14:51:59 +0100795 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
796 sizeof(struct virtio_net_config),
797 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000798
aliguori0f03eca2009-02-05 22:36:08 +0000799 n->vdev.get_config = virtio_net_get_config;
800 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000801 n->vdev.get_features = virtio_net_get_features;
802 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000803 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000804 n->vdev.reset = virtio_net_reset;
aliguorifbe78f42008-12-17 19:13:11 +0000805 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
806 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
Alex Williamson4ffb17f2009-06-05 14:47:23 -0600807 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200808 qemu_macaddr_default_if_unset(&conf->macaddr);
aliguori554c97d2009-01-08 19:46:33 +0000809 n->status = VIRTIO_NET_S_LINK_UP;
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200810 n->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_NIC, conf->vlan, conf->peer,
811 dev->info->name, dev->id,
aliguorib946a152009-04-17 17:11:08 +0000812 virtio_net_can_receive,
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200813 virtio_net_receive, NULL, NULL,
aliguorib946a152009-04-17 17:11:08 +0000814 virtio_net_cleanup, n);
aliguori554c97d2009-01-08 19:46:33 +0000815 n->vc->link_status_changed = virtio_net_set_link_status;
aliguorifbe78f42008-12-17 19:13:11 +0000816
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200817 qemu_format_nic_info_str(n->vc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +0000818
aliguorifbe78f42008-12-17 19:13:11 +0000819 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
820 n->tx_timer_active = 0;
821 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000822 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000823
aliguorib6503ed2009-02-05 22:36:28 +0000824 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000825
aliguorif21c0ed2009-02-05 22:36:32 +0000826 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000827
aliguori9d6271b2009-02-05 22:36:04 +0000828 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000829 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100830
Paul Brook53c25ce2009-05-18 14:51:59 +0100831 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100832}
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200833
834void virtio_net_exit(VirtIODevice *vdev)
835{
836 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
837
838 qemu_purge_queued_packets(n->vc);
839
840 unregister_savevm("virtio-net", n);
841
842 qemu_free(n->mac_table.macs);
843 qemu_free(n->vlans);
844
845 qemu_del_timer(n->tx_timer);
846 qemu_free_timer(n->tx_timer);
847
848 virtio_cleanup(&n->vdev);
849 qemu_del_vlan_client(n->vc);
850}