blob: 7863d5ddbf2a3693040d8f7d406905dea0658a09 [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 Williamson8fd2a2f2009-06-05 14:47:08 -060019#define VIRTIO_NET_VM_VERSION 9
aliguorib6503ed2009-02-05 22:36:28 +000020
21#define MAC_TABLE_ENTRIES 32
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;
35 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060036 uint8_t promisc;
37 uint8_t allmulti;
aliguorib6503ed2009-02-05 22:36:28 +000038 struct {
39 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060040 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060041 uint8_t multi_overflow;
42 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000043 uint8_t *macs;
44 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000045 uint32_t *vlans;
aliguorifbe78f42008-12-17 19:13:11 +000046} VirtIONet;
47
48/* TODO
49 * - we could suppress RX interrupt if we were so inclined.
50 */
51
52static VirtIONet *to_virtio_net(VirtIODevice *vdev)
53{
54 return (VirtIONet *)vdev;
55}
56
aliguori0f03eca2009-02-05 22:36:08 +000057static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000058{
59 VirtIONet *n = to_virtio_net(vdev);
60 struct virtio_net_config netcfg;
61
aliguori554c97d2009-01-08 19:46:33 +000062 netcfg.status = n->status;
aliguori79674062009-02-05 22:36:12 +000063 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000064 memcpy(config, &netcfg, sizeof(netcfg));
65}
66
aliguori0f03eca2009-02-05 22:36:08 +000067static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
68{
69 VirtIONet *n = to_virtio_net(vdev);
70 struct virtio_net_config netcfg;
71
72 memcpy(&netcfg, config, sizeof(netcfg));
73
aliguori79674062009-02-05 22:36:12 +000074 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
75 memcpy(n->mac, netcfg.mac, ETH_ALEN);
aliguori0f03eca2009-02-05 22:36:08 +000076 qemu_format_nic_info_str(n->vc, n->mac);
77 }
78}
79
aliguori554c97d2009-01-08 19:46:33 +000080static void virtio_net_set_link_status(VLANClientState *vc)
81{
82 VirtIONet *n = vc->opaque;
83 uint16_t old_status = n->status;
84
85 if (vc->link_down)
86 n->status &= ~VIRTIO_NET_S_LINK_UP;
87 else
88 n->status |= VIRTIO_NET_S_LINK_UP;
89
90 if (n->status != old_status)
91 virtio_notify_config(&n->vdev);
92}
93
aliguori002437c2009-02-05 22:36:20 +000094static void virtio_net_reset(VirtIODevice *vdev)
95{
96 VirtIONet *n = to_virtio_net(vdev);
97
98 /* Reset back to compatibility mode */
99 n->promisc = 1;
100 n->allmulti = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000101
aliguorif21c0ed2009-02-05 22:36:32 +0000102 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000103 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600104 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600105 n->mac_table.multi_overflow = 0;
106 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000107 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000108 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000109}
110
aliguorifbe78f42008-12-17 19:13:11 +0000111static uint32_t virtio_net_get_features(VirtIODevice *vdev)
112{
aliguori3d11d362009-02-05 22:36:16 +0000113 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
114 (1 << VIRTIO_NET_F_STATUS) |
aliguorib6503ed2009-02-05 22:36:28 +0000115 (1 << VIRTIO_NET_F_CTRL_VQ) |
aliguorif21c0ed2009-02-05 22:36:32 +0000116 (1 << VIRTIO_NET_F_CTRL_RX) |
117 (1 << VIRTIO_NET_F_CTRL_VLAN);
aliguorifbe78f42008-12-17 19:13:11 +0000118
119 return features;
120}
121
aliguori8eca6b12009-04-05 17:40:08 +0000122static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
123{
124 uint32_t features = 0;
125
126 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
127 * but also these: */
128 features |= (1 << VIRTIO_NET_F_MAC);
129 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
130 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
131 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
132 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
133
134 return features & virtio_net_get_features(vdev);
135}
136
aliguorifbe78f42008-12-17 19:13:11 +0000137static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
138{
139 VirtIONet *n = to_virtio_net(vdev);
140
141 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
142}
143
aliguori002437c2009-02-05 22:36:20 +0000144static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
145 VirtQueueElement *elem)
146{
147 uint8_t on;
148
149 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
150 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
151 exit(1);
152 }
153
154 on = ldub_p(elem->out_sg[1].iov_base);
155
156 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
157 n->promisc = on;
158 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
159 n->allmulti = on;
160 else
161 return VIRTIO_NET_ERR;
162
163 return VIRTIO_NET_OK;
164}
165
aliguorib6503ed2009-02-05 22:36:28 +0000166static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
167 VirtQueueElement *elem)
168{
169 struct virtio_net_ctrl_mac mac_data;
170
171 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
172 elem->out_sg[1].iov_len < sizeof(mac_data) ||
173 elem->out_sg[2].iov_len < sizeof(mac_data))
174 return VIRTIO_NET_ERR;
175
176 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600177 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600178 n->mac_table.uni_overflow = 0;
179 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000180 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
181
182 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
183
184 if (sizeof(mac_data.entries) +
185 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
186 return VIRTIO_NET_ERR;
187
188 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
189 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
190 mac_data.entries * ETH_ALEN);
191 n->mac_table.in_use += mac_data.entries;
192 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600193 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000194 }
195
Alex Williamson2d9aba32009-06-05 14:47:13 -0600196 n->mac_table.first_multi = n->mac_table.in_use;
197
aliguorib6503ed2009-02-05 22:36:28 +0000198 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
199
200 if (sizeof(mac_data.entries) +
201 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
202 return VIRTIO_NET_ERR;
203
204 if (mac_data.entries) {
205 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
206 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
207 elem->out_sg[2].iov_base + sizeof(mac_data),
208 mac_data.entries * ETH_ALEN);
209 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600210 } else {
211 n->mac_table.multi_overflow = 1;
212 }
aliguorib6503ed2009-02-05 22:36:28 +0000213 }
214
215 return VIRTIO_NET_OK;
216}
217
aliguorif21c0ed2009-02-05 22:36:32 +0000218static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
219 VirtQueueElement *elem)
220{
221 uint16_t vid;
222
223 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
224 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
225 return VIRTIO_NET_ERR;
226 }
227
228 vid = lduw_le_p(elem->out_sg[1].iov_base);
229
230 if (vid >= MAX_VLAN)
231 return VIRTIO_NET_ERR;
232
233 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
234 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
235 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
236 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
237 else
238 return VIRTIO_NET_ERR;
239
240 return VIRTIO_NET_OK;
241}
242
aliguori3d11d362009-02-05 22:36:16 +0000243static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
244{
aliguori002437c2009-02-05 22:36:20 +0000245 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000246 struct virtio_net_ctrl_hdr ctrl;
247 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
248 VirtQueueElement elem;
249
250 while (virtqueue_pop(vq, &elem)) {
251 if ((elem.in_num < 1) || (elem.out_num < 1)) {
252 fprintf(stderr, "virtio-net ctrl missing headers\n");
253 exit(1);
254 }
255
256 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000257 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000258 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
259 exit(1);
260 }
261
262 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
263 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
264
aliguori002437c2009-02-05 22:36:20 +0000265 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
266 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000267 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
268 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000269 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
270 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000271
aliguori3d11d362009-02-05 22:36:16 +0000272 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
273
274 virtqueue_push(vq, &elem, sizeof(status));
275 virtio_notify(vdev, vq);
276 }
277}
278
aliguorifbe78f42008-12-17 19:13:11 +0000279/* RX */
280
281static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
282{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100283 VirtIONet *n = to_virtio_net(vdev);
284
285 qemu_flush_queued_packets(n->vc);
aliguorifbe78f42008-12-17 19:13:11 +0000286}
287
288static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
289{
290 if (!virtio_queue_ready(n->rx_vq) ||
291 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
292 return 0;
293
294 if (virtio_queue_empty(n->rx_vq) ||
295 (n->mergeable_rx_bufs &&
296 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
297 virtio_queue_set_notification(n->rx_vq, 1);
298 return 0;
299 }
300
301 virtio_queue_set_notification(n->rx_vq, 0);
302 return 1;
303}
304
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100305static int virtio_net_can_receive(VLANClientState *vc)
aliguorifbe78f42008-12-17 19:13:11 +0000306{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100307 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000308
309 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
310}
311
312static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
313{
314 int offset, i;
315
316 offset = i = 0;
317 while (offset < count && i < iovcnt) {
318 int len = MIN(iov[i].iov_len, count - offset);
319 memcpy(iov[i].iov_base, buf + offset, len);
320 offset += len;
321 i++;
322 }
323
324 return offset;
325}
326
327static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000328 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000329{
blueswir13f4cb3d2009-04-13 16:31:01 +0000330 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000331 int offset = 0;
332
333 hdr->flags = 0;
334 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
335
336 /* We only ever receive a struct virtio_net_hdr from the tapfd,
337 * but we may be passing along a larger header to the guest.
338 */
339 iov[0].iov_base += hdr_len;
340 iov[0].iov_len -= hdr_len;
341
342 return offset;
343}
344
aliguori3831ab22009-02-05 22:36:24 +0000345static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
346{
347 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000348 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000349 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000350 int i;
aliguori3831ab22009-02-05 22:36:24 +0000351
352 if (n->promisc)
353 return 1;
354
aliguorif21c0ed2009-02-05 22:36:32 +0000355 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
356 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
357 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
358 return 0;
359 }
360
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600361 if (ptr[0] & 1) { // multicast
362 if (!memcmp(ptr, bcast, sizeof(bcast))) {
363 return 1;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600364 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600365 return 1;
366 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600367
368 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
369 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
370 return 1;
371 }
372 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600373 } else { // unicast
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600374 if (n->mac_table.uni_overflow) {
375 return 1;
376 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600377 return 1;
378 }
aliguori3831ab22009-02-05 22:36:24 +0000379
Alex Williamson2d9aba32009-06-05 14:47:13 -0600380 for (i = 0; i < n->mac_table.first_multi; i++) {
381 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
382 return 1;
383 }
384 }
aliguorib6503ed2009-02-05 22:36:28 +0000385 }
386
aliguori3831ab22009-02-05 22:36:24 +0000387 return 0;
388}
389
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100390static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000391{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100392 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000393 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000394 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000395
396 if (!do_virtio_net_can_receive(n, size))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100397 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000398
aliguori3831ab22009-02-05 22:36:24 +0000399 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100400 return size;
aliguori3831ab22009-02-05 22:36:24 +0000401
aliguorifbe78f42008-12-17 19:13:11 +0000402 /* hdr_len refers to the header we supply to the guest */
403 hdr_len = n->mergeable_rx_bufs ?
404 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
405
406 offset = i = 0;
407
408 while (offset < size) {
409 VirtQueueElement elem;
410 int len, total;
411 struct iovec sg[VIRTQUEUE_MAX_SIZE];
412
413 len = total = 0;
414
415 if ((i != 0 && !n->mergeable_rx_bufs) ||
416 virtqueue_pop(n->rx_vq, &elem) == 0) {
417 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100418 return -1;
aliguorifbe78f42008-12-17 19:13:11 +0000419 fprintf(stderr, "virtio-net truncating packet\n");
420 exit(1);
421 }
422
423 if (elem.in_num < 1) {
424 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
425 exit(1);
426 }
427
428 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
429 fprintf(stderr, "virtio-net header not in first element\n");
430 exit(1);
431 }
432
433 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
434
435 if (i == 0) {
436 if (n->mergeable_rx_bufs)
437 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
438
439 offset += receive_header(n, sg, elem.in_num,
440 buf + offset, size - offset, hdr_len);
441 total += hdr_len;
442 }
443
444 /* copy in packet. ugh */
445 len = iov_fill(sg, elem.in_num,
446 buf + offset, size - offset);
447 total += len;
448
449 /* signal other side */
450 virtqueue_fill(n->rx_vq, &elem, total, i++);
451
452 offset += len;
453 }
454
455 if (mhdr)
456 mhdr->num_buffers = i;
457
458 virtqueue_flush(n->rx_vq, i);
459 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100460
461 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000462}
463
464/* TX */
465static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
466{
467 VirtQueueElement elem;
468 int has_vnet_hdr = 0;
469
470 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
471 return;
472
473 while (virtqueue_pop(vq, &elem)) {
474 ssize_t len = 0;
475 unsigned int out_num = elem.out_num;
476 struct iovec *out_sg = &elem.out_sg[0];
477 unsigned hdr_len;
478
479 /* hdr_len refers to the header received from the guest */
480 hdr_len = n->mergeable_rx_bufs ?
481 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
482 sizeof(struct virtio_net_hdr);
483
484 if (out_num < 1 || out_sg->iov_len != hdr_len) {
485 fprintf(stderr, "virtio-net header not in first element\n");
486 exit(1);
487 }
488
489 /* ignore the header if GSO is not supported */
490 if (!has_vnet_hdr) {
491 out_num--;
492 out_sg++;
493 len += hdr_len;
494 } else if (n->mergeable_rx_bufs) {
495 /* tapfd expects a struct virtio_net_hdr */
496 hdr_len -= sizeof(struct virtio_net_hdr);
497 out_sg->iov_len -= hdr_len;
498 len += hdr_len;
499 }
500
501 len += qemu_sendv_packet(n->vc, out_sg, out_num);
502
503 virtqueue_push(vq, &elem, len);
504 virtio_notify(&n->vdev, vq);
505 }
506}
507
508static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
509{
510 VirtIONet *n = to_virtio_net(vdev);
511
512 if (n->tx_timer_active) {
513 virtio_queue_set_notification(vq, 1);
514 qemu_del_timer(n->tx_timer);
515 n->tx_timer_active = 0;
516 virtio_net_flush_tx(n, vq);
517 } else {
518 qemu_mod_timer(n->tx_timer,
519 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
520 n->tx_timer_active = 1;
521 virtio_queue_set_notification(vq, 0);
522 }
523}
524
525static void virtio_net_tx_timer(void *opaque)
526{
527 VirtIONet *n = opaque;
528
529 n->tx_timer_active = 0;
530
531 /* Just in case the driver is not ready on more */
532 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
533 return;
534
535 virtio_queue_set_notification(n->tx_vq, 1);
536 virtio_net_flush_tx(n, n->tx_vq);
537}
538
539static void virtio_net_save(QEMUFile *f, void *opaque)
540{
541 VirtIONet *n = opaque;
542
543 virtio_save(&n->vdev, f);
544
aliguori79674062009-02-05 22:36:12 +0000545 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000546 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000547 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000548 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600549 qemu_put_byte(f, n->promisc);
550 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000551 qemu_put_be32(f, n->mac_table.in_use);
552 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000553 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Alex Williamson6c042c12009-06-05 14:46:52 -0600554 qemu_put_be32(f, 0); /* vnet-hdr placeholder */
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600555 qemu_put_byte(f, n->mac_table.multi_overflow);
556 qemu_put_byte(f, n->mac_table.uni_overflow);
aliguorifbe78f42008-12-17 19:13:11 +0000557}
558
559static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
560{
561 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600562 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000563
aliguori9d6271b2009-02-05 22:36:04 +0000564 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000565 return -EINVAL;
566
567 virtio_load(&n->vdev, f);
568
aliguori79674062009-02-05 22:36:12 +0000569 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000570 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000571 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000572
aliguori9d6271b2009-02-05 22:36:04 +0000573 if (version_id >= 3)
574 n->status = qemu_get_be16(f);
575
aliguori002437c2009-02-05 22:36:20 +0000576 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600577 if (version_id < 8) {
578 n->promisc = qemu_get_be32(f);
579 n->allmulti = qemu_get_be32(f);
580 } else {
581 n->promisc = qemu_get_byte(f);
582 n->allmulti = qemu_get_byte(f);
583 }
aliguori002437c2009-02-05 22:36:20 +0000584 }
585
aliguorib6503ed2009-02-05 22:36:28 +0000586 if (version_id >= 5) {
587 n->mac_table.in_use = qemu_get_be32(f);
588 /* MAC_TABLE_ENTRIES may be different from the saved image */
589 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
590 qemu_get_buffer(f, n->mac_table.macs,
591 n->mac_table.in_use * ETH_ALEN);
592 } else if (n->mac_table.in_use) {
593 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600594 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000595 n->mac_table.in_use = 0;
596 }
597 }
598
aliguorif21c0ed2009-02-05 22:36:32 +0000599 if (version_id >= 6)
600 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
601
Alex Williamson6c042c12009-06-05 14:46:52 -0600602 if (version_id >= 7 && qemu_get_be32(f)) {
603 fprintf(stderr,
604 "virtio-net: saved image requires vnet header support\n");
605 exit(1);
606 }
607
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600608 if (version_id >= 9) {
609 n->mac_table.multi_overflow = qemu_get_byte(f);
610 n->mac_table.uni_overflow = qemu_get_byte(f);
611 }
612
Alex Williamson2d9aba32009-06-05 14:47:13 -0600613 /* Find the first multicast entry in the saved MAC filter */
614 for (i = 0; i < n->mac_table.in_use; i++) {
615 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
616 break;
617 }
618 }
619 n->mac_table.first_multi = i;
620
aliguorifbe78f42008-12-17 19:13:11 +0000621 if (n->tx_timer_active) {
622 qemu_mod_timer(n->tx_timer,
623 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
624 }
625
626 return 0;
627}
628
aliguorib946a152009-04-17 17:11:08 +0000629static void virtio_net_cleanup(VLANClientState *vc)
630{
631 VirtIONet *n = vc->opaque;
632
633 unregister_savevm("virtio-net", n);
634
635 qemu_free(n->mac_table.macs);
636 qemu_free(n->vlans);
637
638 qemu_del_timer(n->tx_timer);
639 qemu_free_timer(n->tx_timer);
640
641 virtio_cleanup(&n->vdev);
642}
643
Paul Brook53c25ce2009-05-18 14:51:59 +0100644VirtIODevice *virtio_net_init(DeviceState *dev)
aliguorifbe78f42008-12-17 19:13:11 +0000645{
646 VirtIONet *n;
647 static int virtio_net_id;
648
Paul Brook53c25ce2009-05-18 14:51:59 +0100649 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
650 sizeof(struct virtio_net_config),
651 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000652
aliguori0f03eca2009-02-05 22:36:08 +0000653 n->vdev.get_config = virtio_net_get_config;
654 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000655 n->vdev.get_features = virtio_net_get_features;
656 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000657 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000658 n->vdev.reset = virtio_net_reset;
aliguorifbe78f42008-12-17 19:13:11 +0000659 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
660 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
aliguori3d11d362009-02-05 22:36:16 +0000661 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
Paul Brook53c25ce2009-05-18 14:51:59 +0100662 qdev_get_macaddr(dev, n->mac);
aliguori554c97d2009-01-08 19:46:33 +0000663 n->status = VIRTIO_NET_S_LINK_UP;
Paul Brook53c25ce2009-05-18 14:51:59 +0100664 n->vc = qdev_get_vlan_client(dev,
aliguorib946a152009-04-17 17:11:08 +0000665 virtio_net_can_receive,
Mark McLoughlin463af532009-05-18 12:55:27 +0100666 virtio_net_receive, NULL,
aliguorib946a152009-04-17 17:11:08 +0000667 virtio_net_cleanup, n);
aliguori554c97d2009-01-08 19:46:33 +0000668 n->vc->link_status_changed = virtio_net_set_link_status;
aliguorifbe78f42008-12-17 19:13:11 +0000669
aliguori96d5e202009-01-07 17:47:15 +0000670 qemu_format_nic_info_str(n->vc, n->mac);
671
aliguorifbe78f42008-12-17 19:13:11 +0000672 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
673 n->tx_timer_active = 0;
674 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000675 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000676
aliguorib6503ed2009-02-05 22:36:28 +0000677 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000678
aliguorif21c0ed2009-02-05 22:36:32 +0000679 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000680
aliguori9d6271b2009-02-05 22:36:04 +0000681 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000682 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100683
Paul Brook53c25ce2009-05-18 14:51:59 +0100684 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100685}