blob: 8c38454ae12b1f9babee7abbe933a08799eb9a66 [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 Williamson8fd2a2f2009-06-05 14:47:08 -060040 uint8_t multi_overflow;
41 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000042 uint8_t *macs;
43 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000044 uint32_t *vlans;
aliguorifbe78f42008-12-17 19:13:11 +000045} VirtIONet;
46
47/* TODO
48 * - we could suppress RX interrupt if we were so inclined.
49 */
50
51static VirtIONet *to_virtio_net(VirtIODevice *vdev)
52{
53 return (VirtIONet *)vdev;
54}
55
aliguori0f03eca2009-02-05 22:36:08 +000056static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000057{
58 VirtIONet *n = to_virtio_net(vdev);
59 struct virtio_net_config netcfg;
60
aliguori554c97d2009-01-08 19:46:33 +000061 netcfg.status = n->status;
aliguori79674062009-02-05 22:36:12 +000062 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000063 memcpy(config, &netcfg, sizeof(netcfg));
64}
65
aliguori0f03eca2009-02-05 22:36:08 +000066static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
67{
68 VirtIONet *n = to_virtio_net(vdev);
69 struct virtio_net_config netcfg;
70
71 memcpy(&netcfg, config, sizeof(netcfg));
72
aliguori79674062009-02-05 22:36:12 +000073 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
74 memcpy(n->mac, netcfg.mac, ETH_ALEN);
aliguori0f03eca2009-02-05 22:36:08 +000075 qemu_format_nic_info_str(n->vc, n->mac);
76 }
77}
78
aliguori554c97d2009-01-08 19:46:33 +000079static void virtio_net_set_link_status(VLANClientState *vc)
80{
81 VirtIONet *n = vc->opaque;
82 uint16_t old_status = n->status;
83
84 if (vc->link_down)
85 n->status &= ~VIRTIO_NET_S_LINK_UP;
86 else
87 n->status |= VIRTIO_NET_S_LINK_UP;
88
89 if (n->status != old_status)
90 virtio_notify_config(&n->vdev);
91}
92
aliguori002437c2009-02-05 22:36:20 +000093static void virtio_net_reset(VirtIODevice *vdev)
94{
95 VirtIONet *n = to_virtio_net(vdev);
96
97 /* Reset back to compatibility mode */
98 n->promisc = 1;
99 n->allmulti = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000100
aliguorif21c0ed2009-02-05 22:36:32 +0000101 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000102 n->mac_table.in_use = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600103 n->mac_table.multi_overflow = 0;
104 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000105 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000106 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000107}
108
aliguorifbe78f42008-12-17 19:13:11 +0000109static uint32_t virtio_net_get_features(VirtIODevice *vdev)
110{
aliguori3d11d362009-02-05 22:36:16 +0000111 uint32_t features = (1 << VIRTIO_NET_F_MAC) |
112 (1 << VIRTIO_NET_F_STATUS) |
aliguorib6503ed2009-02-05 22:36:28 +0000113 (1 << VIRTIO_NET_F_CTRL_VQ) |
aliguorif21c0ed2009-02-05 22:36:32 +0000114 (1 << VIRTIO_NET_F_CTRL_RX) |
115 (1 << VIRTIO_NET_F_CTRL_VLAN);
aliguorifbe78f42008-12-17 19:13:11 +0000116
117 return features;
118}
119
aliguori8eca6b12009-04-05 17:40:08 +0000120static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
121{
122 uint32_t features = 0;
123
124 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
125 * but also these: */
126 features |= (1 << VIRTIO_NET_F_MAC);
127 features |= (1 << VIRTIO_NET_F_GUEST_CSUM);
128 features |= (1 << VIRTIO_NET_F_GUEST_TSO4);
129 features |= (1 << VIRTIO_NET_F_GUEST_TSO6);
130 features |= (1 << VIRTIO_NET_F_GUEST_ECN);
131
132 return features & virtio_net_get_features(vdev);
133}
134
aliguorifbe78f42008-12-17 19:13:11 +0000135static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
136{
137 VirtIONet *n = to_virtio_net(vdev);
138
139 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
140}
141
aliguori002437c2009-02-05 22:36:20 +0000142static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
143 VirtQueueElement *elem)
144{
145 uint8_t on;
146
147 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
148 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
149 exit(1);
150 }
151
152 on = ldub_p(elem->out_sg[1].iov_base);
153
154 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
155 n->promisc = on;
156 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
157 n->allmulti = on;
158 else
159 return VIRTIO_NET_ERR;
160
161 return VIRTIO_NET_OK;
162}
163
aliguorib6503ed2009-02-05 22:36:28 +0000164static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
165 VirtQueueElement *elem)
166{
167 struct virtio_net_ctrl_mac mac_data;
168
169 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
170 elem->out_sg[1].iov_len < sizeof(mac_data) ||
171 elem->out_sg[2].iov_len < sizeof(mac_data))
172 return VIRTIO_NET_ERR;
173
174 n->mac_table.in_use = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600175 n->mac_table.uni_overflow = 0;
176 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000177 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
178
179 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
180
181 if (sizeof(mac_data.entries) +
182 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
183 return VIRTIO_NET_ERR;
184
185 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
186 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
187 mac_data.entries * ETH_ALEN);
188 n->mac_table.in_use += mac_data.entries;
189 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600190 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000191 }
192
193 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
194
195 if (sizeof(mac_data.entries) +
196 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
197 return VIRTIO_NET_ERR;
198
199 if (mac_data.entries) {
200 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
201 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
202 elem->out_sg[2].iov_base + sizeof(mac_data),
203 mac_data.entries * ETH_ALEN);
204 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600205 } else {
206 n->mac_table.multi_overflow = 1;
207 }
aliguorib6503ed2009-02-05 22:36:28 +0000208 }
209
210 return VIRTIO_NET_OK;
211}
212
aliguorif21c0ed2009-02-05 22:36:32 +0000213static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
214 VirtQueueElement *elem)
215{
216 uint16_t vid;
217
218 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
219 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
220 return VIRTIO_NET_ERR;
221 }
222
223 vid = lduw_le_p(elem->out_sg[1].iov_base);
224
225 if (vid >= MAX_VLAN)
226 return VIRTIO_NET_ERR;
227
228 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
229 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
230 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
231 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
232 else
233 return VIRTIO_NET_ERR;
234
235 return VIRTIO_NET_OK;
236}
237
aliguori3d11d362009-02-05 22:36:16 +0000238static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
239{
aliguori002437c2009-02-05 22:36:20 +0000240 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000241 struct virtio_net_ctrl_hdr ctrl;
242 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
243 VirtQueueElement elem;
244
245 while (virtqueue_pop(vq, &elem)) {
246 if ((elem.in_num < 1) || (elem.out_num < 1)) {
247 fprintf(stderr, "virtio-net ctrl missing headers\n");
248 exit(1);
249 }
250
251 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000252 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000253 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
254 exit(1);
255 }
256
257 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
258 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
259
aliguori002437c2009-02-05 22:36:20 +0000260 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
261 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000262 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
263 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000264 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
265 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000266
aliguori3d11d362009-02-05 22:36:16 +0000267 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
268
269 virtqueue_push(vq, &elem, sizeof(status));
270 virtio_notify(vdev, vq);
271 }
272}
273
aliguorifbe78f42008-12-17 19:13:11 +0000274/* RX */
275
276static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
277{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100278 VirtIONet *n = to_virtio_net(vdev);
279
280 qemu_flush_queued_packets(n->vc);
aliguorifbe78f42008-12-17 19:13:11 +0000281}
282
283static int do_virtio_net_can_receive(VirtIONet *n, int bufsize)
284{
285 if (!virtio_queue_ready(n->rx_vq) ||
286 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
287 return 0;
288
289 if (virtio_queue_empty(n->rx_vq) ||
290 (n->mergeable_rx_bufs &&
291 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
292 virtio_queue_set_notification(n->rx_vq, 1);
293 return 0;
294 }
295
296 virtio_queue_set_notification(n->rx_vq, 0);
297 return 1;
298}
299
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100300static int virtio_net_can_receive(VLANClientState *vc)
aliguorifbe78f42008-12-17 19:13:11 +0000301{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100302 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000303
304 return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE);
305}
306
307static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count)
308{
309 int offset, i;
310
311 offset = i = 0;
312 while (offset < count && i < iovcnt) {
313 int len = MIN(iov[i].iov_len, count - offset);
314 memcpy(iov[i].iov_base, buf + offset, len);
315 offset += len;
316 i++;
317 }
318
319 return offset;
320}
321
322static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000323 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000324{
blueswir13f4cb3d2009-04-13 16:31:01 +0000325 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000326 int offset = 0;
327
328 hdr->flags = 0;
329 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
330
331 /* We only ever receive a struct virtio_net_hdr from the tapfd,
332 * but we may be passing along a larger header to the guest.
333 */
334 iov[0].iov_base += hdr_len;
335 iov[0].iov_len -= hdr_len;
336
337 return offset;
338}
339
aliguori3831ab22009-02-05 22:36:24 +0000340static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
341{
342 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000343 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000344 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000345 int i;
aliguori3831ab22009-02-05 22:36:24 +0000346
347 if (n->promisc)
348 return 1;
349
aliguorif21c0ed2009-02-05 22:36:32 +0000350 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
351 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
352 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
353 return 0;
354 }
355
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600356 if (ptr[0] & 1) { // multicast
357 if (!memcmp(ptr, bcast, sizeof(bcast))) {
358 return 1;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600359 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600360 return 1;
361 }
362 } else { // unicast
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600363 if (n->mac_table.uni_overflow) {
364 return 1;
365 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600366 return 1;
367 }
368 }
aliguori3831ab22009-02-05 22:36:24 +0000369
aliguorib6503ed2009-02-05 22:36:28 +0000370 for (i = 0; i < n->mac_table.in_use; i++) {
371 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN))
372 return 1;
373 }
374
aliguori3831ab22009-02-05 22:36:24 +0000375 return 0;
376}
377
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100378static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000379{
Mark McLoughline3f5ec22009-05-18 13:33:03 +0100380 VirtIONet *n = vc->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000381 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
aliguori4689f4b2008-12-17 19:45:40 +0000382 size_t hdr_len, offset, i;
aliguorifbe78f42008-12-17 19:13:11 +0000383
384 if (!do_virtio_net_can_receive(n, size))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100385 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000386
aliguori3831ab22009-02-05 22:36:24 +0000387 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100388 return size;
aliguori3831ab22009-02-05 22:36:24 +0000389
aliguorifbe78f42008-12-17 19:13:11 +0000390 /* hdr_len refers to the header we supply to the guest */
391 hdr_len = n->mergeable_rx_bufs ?
392 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
393
394 offset = i = 0;
395
396 while (offset < size) {
397 VirtQueueElement elem;
398 int len, total;
399 struct iovec sg[VIRTQUEUE_MAX_SIZE];
400
401 len = total = 0;
402
403 if ((i != 0 && !n->mergeable_rx_bufs) ||
404 virtqueue_pop(n->rx_vq, &elem) == 0) {
405 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100406 return -1;
aliguorifbe78f42008-12-17 19:13:11 +0000407 fprintf(stderr, "virtio-net truncating packet\n");
408 exit(1);
409 }
410
411 if (elem.in_num < 1) {
412 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
413 exit(1);
414 }
415
416 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) {
417 fprintf(stderr, "virtio-net header not in first element\n");
418 exit(1);
419 }
420
421 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
422
423 if (i == 0) {
424 if (n->mergeable_rx_bufs)
425 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
426
427 offset += receive_header(n, sg, elem.in_num,
428 buf + offset, size - offset, hdr_len);
429 total += hdr_len;
430 }
431
432 /* copy in packet. ugh */
433 len = iov_fill(sg, elem.in_num,
434 buf + offset, size - offset);
435 total += len;
436
437 /* signal other side */
438 virtqueue_fill(n->rx_vq, &elem, total, i++);
439
440 offset += len;
441 }
442
443 if (mhdr)
444 mhdr->num_buffers = i;
445
446 virtqueue_flush(n->rx_vq, i);
447 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100448
449 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000450}
451
452/* TX */
453static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
454{
455 VirtQueueElement elem;
456 int has_vnet_hdr = 0;
457
458 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
459 return;
460
461 while (virtqueue_pop(vq, &elem)) {
462 ssize_t len = 0;
463 unsigned int out_num = elem.out_num;
464 struct iovec *out_sg = &elem.out_sg[0];
465 unsigned hdr_len;
466
467 /* hdr_len refers to the header received from the guest */
468 hdr_len = n->mergeable_rx_bufs ?
469 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
470 sizeof(struct virtio_net_hdr);
471
472 if (out_num < 1 || out_sg->iov_len != hdr_len) {
473 fprintf(stderr, "virtio-net header not in first element\n");
474 exit(1);
475 }
476
477 /* ignore the header if GSO is not supported */
478 if (!has_vnet_hdr) {
479 out_num--;
480 out_sg++;
481 len += hdr_len;
482 } else if (n->mergeable_rx_bufs) {
483 /* tapfd expects a struct virtio_net_hdr */
484 hdr_len -= sizeof(struct virtio_net_hdr);
485 out_sg->iov_len -= hdr_len;
486 len += hdr_len;
487 }
488
489 len += qemu_sendv_packet(n->vc, out_sg, out_num);
490
491 virtqueue_push(vq, &elem, len);
492 virtio_notify(&n->vdev, vq);
493 }
494}
495
496static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
497{
498 VirtIONet *n = to_virtio_net(vdev);
499
500 if (n->tx_timer_active) {
501 virtio_queue_set_notification(vq, 1);
502 qemu_del_timer(n->tx_timer);
503 n->tx_timer_active = 0;
504 virtio_net_flush_tx(n, vq);
505 } else {
506 qemu_mod_timer(n->tx_timer,
507 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
508 n->tx_timer_active = 1;
509 virtio_queue_set_notification(vq, 0);
510 }
511}
512
513static void virtio_net_tx_timer(void *opaque)
514{
515 VirtIONet *n = opaque;
516
517 n->tx_timer_active = 0;
518
519 /* Just in case the driver is not ready on more */
520 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
521 return;
522
523 virtio_queue_set_notification(n->tx_vq, 1);
524 virtio_net_flush_tx(n, n->tx_vq);
525}
526
527static void virtio_net_save(QEMUFile *f, void *opaque)
528{
529 VirtIONet *n = opaque;
530
531 virtio_save(&n->vdev, f);
532
aliguori79674062009-02-05 22:36:12 +0000533 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000534 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000535 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000536 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600537 qemu_put_byte(f, n->promisc);
538 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000539 qemu_put_be32(f, n->mac_table.in_use);
540 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000541 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Alex Williamson6c042c12009-06-05 14:46:52 -0600542 qemu_put_be32(f, 0); /* vnet-hdr placeholder */
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600543 qemu_put_byte(f, n->mac_table.multi_overflow);
544 qemu_put_byte(f, n->mac_table.uni_overflow);
aliguorifbe78f42008-12-17 19:13:11 +0000545}
546
547static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
548{
549 VirtIONet *n = opaque;
550
aliguori9d6271b2009-02-05 22:36:04 +0000551 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000552 return -EINVAL;
553
554 virtio_load(&n->vdev, f);
555
aliguori79674062009-02-05 22:36:12 +0000556 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000557 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000558 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000559
aliguori9d6271b2009-02-05 22:36:04 +0000560 if (version_id >= 3)
561 n->status = qemu_get_be16(f);
562
aliguori002437c2009-02-05 22:36:20 +0000563 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600564 if (version_id < 8) {
565 n->promisc = qemu_get_be32(f);
566 n->allmulti = qemu_get_be32(f);
567 } else {
568 n->promisc = qemu_get_byte(f);
569 n->allmulti = qemu_get_byte(f);
570 }
aliguori002437c2009-02-05 22:36:20 +0000571 }
572
aliguorib6503ed2009-02-05 22:36:28 +0000573 if (version_id >= 5) {
574 n->mac_table.in_use = qemu_get_be32(f);
575 /* MAC_TABLE_ENTRIES may be different from the saved image */
576 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
577 qemu_get_buffer(f, n->mac_table.macs,
578 n->mac_table.in_use * ETH_ALEN);
579 } else if (n->mac_table.in_use) {
580 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600581 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000582 n->mac_table.in_use = 0;
583 }
584 }
585
aliguorif21c0ed2009-02-05 22:36:32 +0000586 if (version_id >= 6)
587 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
588
Alex Williamson6c042c12009-06-05 14:46:52 -0600589 if (version_id >= 7 && qemu_get_be32(f)) {
590 fprintf(stderr,
591 "virtio-net: saved image requires vnet header support\n");
592 exit(1);
593 }
594
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600595 if (version_id >= 9) {
596 n->mac_table.multi_overflow = qemu_get_byte(f);
597 n->mac_table.uni_overflow = qemu_get_byte(f);
598 }
599
aliguorifbe78f42008-12-17 19:13:11 +0000600 if (n->tx_timer_active) {
601 qemu_mod_timer(n->tx_timer,
602 qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
603 }
604
605 return 0;
606}
607
aliguorib946a152009-04-17 17:11:08 +0000608static void virtio_net_cleanup(VLANClientState *vc)
609{
610 VirtIONet *n = vc->opaque;
611
612 unregister_savevm("virtio-net", n);
613
614 qemu_free(n->mac_table.macs);
615 qemu_free(n->vlans);
616
617 qemu_del_timer(n->tx_timer);
618 qemu_free_timer(n->tx_timer);
619
620 virtio_cleanup(&n->vdev);
621}
622
Paul Brook53c25ce2009-05-18 14:51:59 +0100623VirtIODevice *virtio_net_init(DeviceState *dev)
aliguorifbe78f42008-12-17 19:13:11 +0000624{
625 VirtIONet *n;
626 static int virtio_net_id;
627
Paul Brook53c25ce2009-05-18 14:51:59 +0100628 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
629 sizeof(struct virtio_net_config),
630 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000631
aliguori0f03eca2009-02-05 22:36:08 +0000632 n->vdev.get_config = virtio_net_get_config;
633 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000634 n->vdev.get_features = virtio_net_get_features;
635 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000636 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000637 n->vdev.reset = virtio_net_reset;
aliguorifbe78f42008-12-17 19:13:11 +0000638 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
639 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
aliguori3d11d362009-02-05 22:36:16 +0000640 n->ctrl_vq = virtio_add_queue(&n->vdev, 16, virtio_net_handle_ctrl);
Paul Brook53c25ce2009-05-18 14:51:59 +0100641 qdev_get_macaddr(dev, n->mac);
aliguori554c97d2009-01-08 19:46:33 +0000642 n->status = VIRTIO_NET_S_LINK_UP;
Paul Brook53c25ce2009-05-18 14:51:59 +0100643 n->vc = qdev_get_vlan_client(dev,
aliguorib946a152009-04-17 17:11:08 +0000644 virtio_net_can_receive,
Mark McLoughlin463af532009-05-18 12:55:27 +0100645 virtio_net_receive, NULL,
aliguorib946a152009-04-17 17:11:08 +0000646 virtio_net_cleanup, n);
aliguori554c97d2009-01-08 19:46:33 +0000647 n->vc->link_status_changed = virtio_net_set_link_status;
aliguorifbe78f42008-12-17 19:13:11 +0000648
aliguori96d5e202009-01-07 17:47:15 +0000649 qemu_format_nic_info_str(n->vc, n->mac);
650
aliguorifbe78f42008-12-17 19:13:11 +0000651 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
652 n->tx_timer_active = 0;
653 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000654 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000655
aliguorib6503ed2009-02-05 22:36:28 +0000656 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000657
aliguorif21c0ed2009-02-05 22:36:32 +0000658 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000659
aliguori9d6271b2009-02-05 22:36:04 +0000660 register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000661 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100662
Paul Brook53c25ce2009-05-18 14:51:59 +0100663 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100664}