blob: ae9b7d92c76b2dd4e93509021212bada1136be6f [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
aliguorif21c0ed2009-02-05 22:36:32 +000019#define VIRTIO_NET_VM_VERSION 6
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;
aliguori002437c2009-02-05 22:36:20 +000036 int promisc;
37 int allmulti;
aliguorib6503ed2009-02-05 22:36:28 +000038 struct {
39 int in_use;
40 uint8_t *macs;
41 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000042 uint32_t *vlans;
aliguorifbe78f42008-12-17 19:13:11 +000043} VirtIONet;
44
45/* TODO
46 * - we could suppress RX interrupt if we were so inclined.
47 */
48
49static VirtIONet *to_virtio_net(VirtIODevice *vdev)
50{
51 return (VirtIONet *)vdev;
52}
53
aliguori0f03eca2009-02-05 22:36:08 +000054static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000055{
56 VirtIONet *n = to_virtio_net(vdev);
57 struct virtio_net_config netcfg;
58
aliguori554c97d2009-01-08 19:46:33 +000059 netcfg.status = n->status;
aliguori79674062009-02-05 22:36:12 +000060 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000061 memcpy(config, &netcfg, sizeof(netcfg));
62}
63
aliguori0f03eca2009-02-05 22:36:08 +000064static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
65{
66 VirtIONet *n = to_virtio_net(vdev);
67 struct virtio_net_config netcfg;
68
69 memcpy(&netcfg, config, sizeof(netcfg));
70
aliguori79674062009-02-05 22:36:12 +000071 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
72 memcpy(n->mac, netcfg.mac, ETH_ALEN);
aliguori0f03eca2009-02-05 22:36:08 +000073 qemu_format_nic_info_str(n->vc, n->mac);
74 }
75}
76
aliguori554c97d2009-01-08 19:46:33 +000077static void virtio_net_set_link_status(VLANClientState *vc)
78{
79 VirtIONet *n = vc->opaque;
80 uint16_t old_status = n->status;
81
82 if (vc->link_down)
83 n->status &= ~VIRTIO_NET_S_LINK_UP;
84 else
85 n->status |= VIRTIO_NET_S_LINK_UP;
86
87 if (n->status != old_status)
88 virtio_notify_config(&n->vdev);
89}
90
aliguori002437c2009-02-05 22:36:20 +000091static void virtio_net_reset(VirtIODevice *vdev)
92{
93 VirtIONet *n = to_virtio_net(vdev);
94
95 /* Reset back to compatibility mode */
96 n->promisc = 1;
97 n->allmulti = 0;
aliguorib6503ed2009-02-05 22:36:28 +000098
aliguorif21c0ed2009-02-05 22:36:32 +000099 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000100 n->mac_table.in_use = 0;
101 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000102 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000103}
104
aliguorifbe78f42008-12-17 19:13:11 +0000105static uint32_t virtio_net_get_features(VirtIODevice *vdev)
106{
aliguori3d11d362009-02-05 22:36:16 +0000107 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
108 (1 << VIRTIO_NET_F_STATUS) |
aliguorib6503ed2009-02-05 22:36:28 +0000109 (1 << VIRTIO_NET_F_CTRL_VQ) |
aliguorif21c0ed2009-02-05 22:36:32 +0000110 (1 << VIRTIO_NET_F_CTRL_RX) |
111 (1 << VIRTIO_NET_F_CTRL_VLAN);
aliguorifbe78f42008-12-17 19:13:11 +0000112
113 return features;
114}
115
aliguori8eca6b12009-04-05 17:40:08 +0000116static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
117{
118 uint32_t features = 0;
119
120 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
121 * but also these: */
122 features |= (1 << VIRTIO_NET_F_MAC);
123 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
124 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
125 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
126 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
127
128 return features & virtio_net_get_features(vdev);
129}
130
aliguorifbe78f42008-12-17 19:13:11 +0000131static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
132{
133 VirtIONet *n = to_virtio_net(vdev);
134
135 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
136}
137
aliguori002437c2009-02-05 22:36:20 +0000138static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
139 VirtQueueElement *elem)
140{
141 uint8_t on;
142
143 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
144 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
145 exit(1);
146 }
147
148 on = ldub_p(elem->out_sg[1].iov_base);
149
150 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
151 n->promisc = on;
152 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
153 n->allmulti = on;
154 else
155 return VIRTIO_NET_ERR;
156
157 return VIRTIO_NET_OK;
158}
159
aliguorib6503ed2009-02-05 22:36:28 +0000160static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
161 VirtQueueElement *elem)
162{
163 struct virtio_net_ctrl_mac mac_data;
164
165 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
166 elem->out_sg[1].iov_len < sizeof(mac_data) ||
167 elem->out_sg[2].iov_len < sizeof(mac_data))
168 return VIRTIO_NET_ERR;
169
170 n->mac_table.in_use = 0;
171 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
172
173 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
174
175 if (sizeof(mac_data.entries) +
176 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
177 return VIRTIO_NET_ERR;
178
179 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
180 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
181 mac_data.entries * ETH_ALEN);
182 n->mac_table.in_use += mac_data.entries;
183 } else {
184 n->promisc = 1;
185 return VIRTIO_NET_OK;
186 }
187
188 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
189
190 if (sizeof(mac_data.entries) +
191 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
192 return VIRTIO_NET_ERR;
193
194 if (mac_data.entries) {
195 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
196 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
197 elem->out_sg[2].iov_base + sizeof(mac_data),
198 mac_data.entries * ETH_ALEN);
199 n->mac_table.in_use += mac_data.entries;
200 } else
201 n->allmulti = 1;
202 }
203
204 return VIRTIO_NET_OK;
205}
206
aliguorif21c0ed2009-02-05 22:36:32 +0000207static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
208 VirtQueueElement *elem)
209{
210 uint16_t vid;
211
212 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
213 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
214 return VIRTIO_NET_ERR;
215 }
216
217 vid = lduw_le_p(elem->out_sg[1].iov_base);
218
219 if (vid >= MAX_VLAN)
220 return VIRTIO_NET_ERR;
221
222 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
223 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
224 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
225 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
226 else
227 return VIRTIO_NET_ERR;
228
229 return VIRTIO_NET_OK;
230}
231
aliguori3d11d362009-02-05 22:36:16 +0000232static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
233{
aliguori002437c2009-02-05 22:36:20 +0000234 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000235 struct virtio_net_ctrl_hdr ctrl;
236 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
237 VirtQueueElement elem;
238
239 while (virtqueue_pop(vq, &elem)) {
240 if ((elem.in_num < 1) || (elem.out_num < 1)) {
241 fprintf(stderr, "virtio-net ctrl missing headers\n");
242 exit(1);
243 }
244
245 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000246 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000247 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
248 exit(1);
249 }
250
251 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
252 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
253
aliguori002437c2009-02-05 22:36:20 +0000254 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
255 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000256 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
257 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000258 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
259 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000260
aliguori3d11d362009-02-05 22:36:16 +0000261 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
262
263 virtqueue_push(vq, &elem, sizeof(status));
264 virtio_notify(vdev, vq);
265 }
266}
267
aliguorifbe78f42008-12-17 19:13:11 +0000268/* RX */
269
270static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
271{
272}
273
274static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
275{
276 if (!virtio_queue_ready(n->rx_vq) ||
277 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
278 return 0;
279
280 if (virtio_queue_empty(n->rx_vq) ||
281 (n->mergeable_rx_bufs &&
282 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
283 virtio_queue_set_notification(n->rx_vq, 1);
284 return 0;
285 }
286
287 virtio_queue_set_notification(n->rx_vq, 0);
288 return 1;
289}
290
291static int virtio_net_can_receive(void *opaque)
292{
293 VirtIONet *n = opaque;
294
295 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
296}
297
298static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
299{
300 int offset, i;
301
302 offset = i = 0;
303 while (offset < count && i < iovcnt) {
304 int len = MIN(iov[i].iov_len, count - offset);
305 memcpy(iov[i].iov_base, buf + offset, len);
306 offset += len;
307 i++;
308 }
309
310 return offset;
311}
312
313static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000314 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000315{
316 struct virtio_net_hdr *hdr = iov[0].iov_base;
317 int offset = 0;
318
319 hdr->flags = 0;
320 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
321
322 /* We only ever receive a struct virtio_net_hdr from the tapfd,
323 * but we may be passing along a larger header to the guest.
324 */
325 iov[0].iov_base += hdr_len;
326 iov[0].iov_len -= hdr_len;
327
328 return offset;
329}
330
aliguori3831ab22009-02-05 22:36:24 +0000331static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
332{
333 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000334 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000335 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000336 int i;
aliguori3831ab22009-02-05 22:36:24 +0000337
338 if (n->promisc)
339 return 1;
340
341#ifdef TAP_VNET_HDR
342 if (tap_has_vnet_hdr(n->vc->vlan->first_client))
343 ptr += sizeof(struct virtio_net_hdr);
344#endif
345
aliguorif21c0ed2009-02-05 22:36:32 +0000346 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
347 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
348 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
349 return 0;
350 }
351
aliguori3831ab22009-02-05 22:36:24 +0000352 if ((ptr[0] & 1) && n->allmulti)
353 return 1;
354
355 if (!memcmp(ptr, bcast, sizeof(bcast)))
356 return 1;
357
358 if (!memcmp(ptr, n->mac, ETH_ALEN))
359 return 1;
360
aliguorib6503ed2009-02-05 22:36:28 +0000361 for (i = 0; i < n->mac_table.in_use; i++) {
362 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
363 return 1;
364 }
365
aliguori3831ab22009-02-05 22:36:24 +0000366 return 0;
367}
368
aliguorifbe78f42008-12-17 19:13:11 +0000369static void virtio_net_receive(void *opaque, const uint8_t *buf, int size)
370{
371 VirtIONet *n = opaque;
372 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000373 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000374
375 if (!do_virtio_net_can_receive(n, size))
376 return;
377
aliguori3831ab22009-02-05 22:36:24 +0000378 if (!receive_filter(n, buf, size))
379 return;
380
aliguorifbe78f42008-12-17 19:13:11 +0000381 /* hdr_len refers to the header we supply to the guest */
382 hdr_len = n->mergeable_rx_bufs ?
383 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
384
385 offset = i = 0;
386
387 while (offset < size) {
388 VirtQueueElement elem;
389 int len, total;
390 struct iovec sg[VIRTQUEUE_MAX_SIZE];
391
392 len = total = 0;
393
394 if ((i != 0 && !n->mergeable_rx_bufs) ||
395 virtqueue_pop(n->rx_vq, &elem) == 0) {
396 if (i == 0)
397 return;
398 fprintf(stderr, "virtio-net truncating packet\n");
399 exit(1);
400 }
401
402 if (elem.in_num < 1) {
403 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
404 exit(1);
405 }
406
407 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
408 fprintf(stderr, "virtio-net header not in first element\n");
409 exit(1);
410 }
411
412 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
413
414 if (i == 0) {
415 if (n->mergeable_rx_bufs)
416 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
417
418 offset += receive_header(n, sg, elem.in_num,
419 buf + offset, size - offset, hdr_len);
420 total += hdr_len;
421 }
422
423 /* copy in packet. ugh */
424 len = iov_fill(sg, elem.in_num,
425 buf + offset, size - offset);
426 total += len;
427
428 /* signal other side */
429 virtqueue_fill(n->rx_vq, &elem, total, i++);
430
431 offset += len;
432 }
433
434 if (mhdr)
435 mhdr->num_buffers = i;
436
437 virtqueue_flush(n->rx_vq, i);
438 virtio_notify(&n->vdev, n->rx_vq);
439}
440
441/* TX */
442static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
443{
444 VirtQueueElement elem;
445 int has_vnet_hdr = 0;
446
447 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
448 return;
449
450 while (virtqueue_pop(vq, &elem)) {
451 ssize_t len = 0;
452 unsigned int out_num = elem.out_num;
453 struct iovec *out_sg = &elem.out_sg[0];
454 unsigned hdr_len;
455
456 /* hdr_len refers to the header received from the guest */
457 hdr_len = n->mergeable_rx_bufs ?
458 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
459 sizeof(struct virtio_net_hdr);
460
461 if (out_num < 1 || out_sg->iov_len != hdr_len) {
462 fprintf(stderr, "virtio-net header not in first element\n");
463 exit(1);
464 }
465
466 /* ignore the header if GSO is not supported */
467 if (!has_vnet_hdr) {
468 out_num--;
469 out_sg++;
470 len += hdr_len;
471 } else if (n->mergeable_rx_bufs) {
472 /* tapfd expects a struct virtio_net_hdr */
473 hdr_len -= sizeof(struct virtio_net_hdr);
474 out_sg->iov_len -= hdr_len;
475 len += hdr_len;
476 }
477
478 len += qemu_sendv_packet(n->vc, out_sg, out_num);
479
480 virtqueue_push(vq, &elem, len);
481 virtio_notify(&n->vdev, vq);
482 }
483}
484
485static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
486{
487 VirtIONet *n = to_virtio_net(vdev);
488
489 if (n->tx_timer_active) {
490 virtio_queue_set_notification(vq, 1);
491 qemu_del_timer(n->tx_timer);
492 n->tx_timer_active = 0;
493 virtio_net_flush_tx(n, vq);
494 } else {
495 qemu_mod_timer(n->tx_timer,
496 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
497 n->tx_timer_active = 1;
498 virtio_queue_set_notification(vq, 0);
499 }
500}
501
502static void virtio_net_tx_timer(void *opaque)
503{
504 VirtIONet *n = opaque;
505
506 n->tx_timer_active = 0;
507
508 /* Just in case the driver is not ready on more */
509 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
510 return;
511
512 virtio_queue_set_notification(n->tx_vq, 1);
513 virtio_net_flush_tx(n, n->tx_vq);
514}
515
516static void virtio_net_save(QEMUFile *f, void *opaque)
517{
518 VirtIONet *n = opaque;
519
520 virtio_save(&n->vdev, f);
521
aliguori79674062009-02-05 22:36:12 +0000522 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000523 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000524 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000525 qemu_put_be16(f, n->status);
aliguori002437c2009-02-05 22:36:20 +0000526 qemu_put_be32(f, n->promisc);
527 qemu_put_be32(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000528 qemu_put_be32(f, n->mac_table.in_use);
529 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000530 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
aliguorifbe78f42008-12-17 19:13:11 +0000531}
532
533static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
534{
535 VirtIONet *n = opaque;
536
aliguori9d6271b2009-02-05 22:36:04 +0000537 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000538 return -EINVAL;
539
540 virtio_load(&n->vdev, f);
541
aliguori79674062009-02-05 22:36:12 +0000542 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000543 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000544 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000545
aliguori9d6271b2009-02-05 22:36:04 +0000546 if (version_id >= 3)
547 n->status = qemu_get_be16(f);
548
aliguori002437c2009-02-05 22:36:20 +0000549 if (version_id >= 4) {
550 n->promisc = qemu_get_be32(f);
551 n->allmulti = qemu_get_be32(f);
552 }
553
aliguorib6503ed2009-02-05 22:36:28 +0000554 if (version_id >= 5) {
555 n->mac_table.in_use = qemu_get_be32(f);
556 /* MAC_TABLE_ENTRIES may be different from the saved image */
557 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
558 qemu_get_buffer(f, n->mac_table.macs,
559 n->mac_table.in_use * ETH_ALEN);
560 } else if (n->mac_table.in_use) {
561 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
562 n->promisc = 1;
563 n->mac_table.in_use = 0;
564 }
565 }
566
aliguorif21c0ed2009-02-05 22:36:32 +0000567 if (version_id >= 6)
568 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
569
aliguorifbe78f42008-12-17 19:13:11 +0000570 if (n->tx_timer_active) {
571 qemu_mod_timer(n->tx_timer,
572 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
573 }
574
575 return 0;
576}
577
aliguori72da4202009-02-11 15:19:52 +0000578PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int devfn)
aliguorifbe78f42008-12-17 19:13:11 +0000579{
580 VirtIONet *n;
581 static int virtio_net_id;
582
aliguoria7c49962009-01-26 15:22:41 +0000583 n = (VirtIONet *)virtio_init_pci(bus, "virtio-net",
584 PCI_VENDOR_ID_REDHAT_QUMRANET,
585 PCI_DEVICE_ID_VIRTIO_NET,
aliguori99b37182009-01-26 15:22:57 +0000586 PCI_VENDOR_ID_REDHAT_QUMRANET,
587 VIRTIO_ID_NET,
blueswir1173a5432009-02-01 19:26:20 +0000588 PCI_CLASS_NETWORK_ETHERNET, 0x00,
aliguori554c97d2009-01-08 19:46:33 +0000589 sizeof(struct virtio_net_config),
590 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000591 if (!n)
aliguori72da4202009-02-11 15:19:52 +0000592 return NULL;
aliguorifbe78f42008-12-17 19:13:11 +0000593
aliguori0f03eca2009-02-05 22:36:08 +0000594 n->vdev.get_config = virtio_net_get_config;
595 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000596 n->vdev.get_features = virtio_net_get_features;
597 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000598 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000599 n->vdev.reset = virtio_net_reset;
aliguorifbe78f42008-12-17 19:13:11 +0000600 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
601 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
aliguori3d11d362009-02-05 22:36:16 +0000602 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
aliguori79674062009-02-05 22:36:12 +0000603 memcpy(n->mac, nd->macaddr, ETH_ALEN);
aliguori554c97d2009-01-08 19:46:33 +0000604 n->status = VIRTIO_NET_S_LINK_UP;
aliguori7a9f6e42009-01-07 17:48:51 +0000605 n->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name,
aliguoribf38c1a2009-01-07 17:42:25 +0000606 virtio_net_receive, virtio_net_can_receive, n);
aliguori554c97d2009-01-08 19:46:33 +0000607 n->vc->link_status_changed = virtio_net_set_link_status;
aliguorifbe78f42008-12-17 19:13:11 +0000608
aliguori96d5e202009-01-07 17:47:15 +0000609 qemu_format_nic_info_str(n->vc, n->mac);
610
aliguorifbe78f42008-12-17 19:13:11 +0000611 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
612 n->tx_timer_active = 0;
613 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000614 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000615
aliguorib6503ed2009-02-05 22:36:28 +0000616 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000617
aliguorif21c0ed2009-02-05 22:36:32 +0000618 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000619
aliguori9d6271b2009-02-05 22:36:04 +0000620 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000621 virtio_net_save, virtio_net_load, n);
aliguori72da4202009-02-11 15:19:52 +0000622 return (PCIDevice *)n;
aliguorifbe78f42008-12-17 19:13:11 +0000623}