2 * Virtio Network Device
4 * Copyright IBM, Corp. 2007
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
16 #include "qemu-timer.h"
17 #include "virtio-net.h"
19 #define VIRTIO_NET_VM_VERSION 9
21 #define MAC_TABLE_ENTRIES 32
22 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
24 typedef struct VirtIONet
27 uint8_t mac
[ETH_ALEN
];
35 int mergeable_rx_bufs
;
40 uint8_t multi_overflow
;
48 * - we could suppress RX interrupt if we were so inclined.
51 static VirtIONet
*to_virtio_net(VirtIODevice
*vdev
)
53 return (VirtIONet
*)vdev
;
56 static void virtio_net_get_config(VirtIODevice
*vdev
, uint8_t *config
)
58 VirtIONet
*n
= to_virtio_net(vdev
);
59 struct virtio_net_config netcfg
;
61 netcfg
.status
= n
->status
;
62 memcpy(netcfg
.mac
, n
->mac
, ETH_ALEN
);
63 memcpy(config
, &netcfg
, sizeof(netcfg
));
66 static void virtio_net_set_config(VirtIODevice
*vdev
, const uint8_t *config
)
68 VirtIONet
*n
= to_virtio_net(vdev
);
69 struct virtio_net_config netcfg
;
71 memcpy(&netcfg
, config
, sizeof(netcfg
));
73 if (memcmp(netcfg
.mac
, n
->mac
, ETH_ALEN
)) {
74 memcpy(n
->mac
, netcfg
.mac
, ETH_ALEN
);
75 qemu_format_nic_info_str(n
->vc
, n
->mac
);
79 static void virtio_net_set_link_status(VLANClientState
*vc
)
81 VirtIONet
*n
= vc
->opaque
;
82 uint16_t old_status
= n
->status
;
85 n
->status
&= ~VIRTIO_NET_S_LINK_UP
;
87 n
->status
|= VIRTIO_NET_S_LINK_UP
;
89 if (n
->status
!= old_status
)
90 virtio_notify_config(&n
->vdev
);
93 static void virtio_net_reset(VirtIODevice
*vdev
)
95 VirtIONet
*n
= to_virtio_net(vdev
);
97 /* Reset back to compatibility mode */
101 /* Flush any MAC and VLAN filter table state */
102 n
->mac_table
.in_use
= 0;
103 n
->mac_table
.multi_overflow
= 0;
104 n
->mac_table
.uni_overflow
= 0;
105 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
106 memset(n
->vlans
, 0, MAX_VLAN
>> 3);
109 static uint32_t virtio_net_get_features(VirtIODevice
*vdev
)
111 uint32_t features
= (1 << VIRTIO_NET_F_MAC
) |
112 (1 << VIRTIO_NET_F_STATUS
) |
113 (1 << VIRTIO_NET_F_CTRL_VQ
) |
114 (1 << VIRTIO_NET_F_CTRL_RX
) |
115 (1 << VIRTIO_NET_F_CTRL_VLAN
);
120 static uint32_t virtio_net_bad_features(VirtIODevice
*vdev
)
122 uint32_t features
= 0;
124 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
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
);
132 return features
& virtio_net_get_features(vdev
);
135 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
137 VirtIONet
*n
= to_virtio_net(vdev
);
139 n
->mergeable_rx_bufs
= !!(features
& (1 << VIRTIO_NET_F_MRG_RXBUF
));
142 static int virtio_net_handle_rx_mode(VirtIONet
*n
, uint8_t cmd
,
143 VirtQueueElement
*elem
)
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");
152 on
= ldub_p(elem
->out_sg
[1].iov_base
);
154 if (cmd
== VIRTIO_NET_CTRL_RX_MODE_PROMISC
)
156 else if (cmd
== VIRTIO_NET_CTRL_RX_MODE_ALLMULTI
)
159 return VIRTIO_NET_ERR
;
161 return VIRTIO_NET_OK
;
164 static int virtio_net_handle_mac(VirtIONet
*n
, uint8_t cmd
,
165 VirtQueueElement
*elem
)
167 struct virtio_net_ctrl_mac mac_data
;
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
;
174 n
->mac_table
.in_use
= 0;
175 n
->mac_table
.uni_overflow
= 0;
176 n
->mac_table
.multi_overflow
= 0;
177 memset(n
->mac_table
.macs
, 0, MAC_TABLE_ENTRIES
* ETH_ALEN
);
179 mac_data
.entries
= ldl_le_p(elem
->out_sg
[1].iov_base
);
181 if (sizeof(mac_data
.entries
) +
182 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[1].iov_len
)
183 return VIRTIO_NET_ERR
;
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
;
190 n
->mac_table
.uni_overflow
= 1;
193 mac_data
.entries
= ldl_le_p(elem
->out_sg
[2].iov_base
);
195 if (sizeof(mac_data
.entries
) +
196 (mac_data
.entries
* ETH_ALEN
) > elem
->out_sg
[2].iov_len
)
197 return VIRTIO_NET_ERR
;
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
;
206 n
->mac_table
.multi_overflow
= 1;
210 return VIRTIO_NET_OK
;
213 static int virtio_net_handle_vlan_table(VirtIONet
*n
, uint8_t cmd
,
214 VirtQueueElement
*elem
)
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
;
223 vid
= lduw_le_p(elem
->out_sg
[1].iov_base
);
226 return VIRTIO_NET_ERR
;
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));
233 return VIRTIO_NET_ERR
;
235 return VIRTIO_NET_OK
;
238 static void virtio_net_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
240 VirtIONet
*n
= to_virtio_net(vdev
);
241 struct virtio_net_ctrl_hdr ctrl
;
242 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
243 VirtQueueElement elem
;
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");
251 if (elem
.out_sg
[0].iov_len
< sizeof(ctrl
) ||
252 elem
.in_sg
[elem
.in_num
- 1].iov_len
< sizeof(status
)) {
253 fprintf(stderr
, "virtio-net ctrl header not in correct element\n");
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));
260 if (ctrl
.class == VIRTIO_NET_CTRL_RX_MODE
)
261 status
= virtio_net_handle_rx_mode(n
, ctrl
.cmd
, &elem
);
262 else if (ctrl
.class == VIRTIO_NET_CTRL_MAC
)
263 status
= virtio_net_handle_mac(n
, ctrl
.cmd
, &elem
);
264 else if (ctrl
.class == VIRTIO_NET_CTRL_VLAN
)
265 status
= virtio_net_handle_vlan_table(n
, ctrl
.cmd
, &elem
);
267 stb_p(elem
.in_sg
[elem
.in_num
- 1].iov_base
, status
);
269 virtqueue_push(vq
, &elem
, sizeof(status
));
270 virtio_notify(vdev
, vq
);
276 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
278 VirtIONet
*n
= to_virtio_net(vdev
);
280 qemu_flush_queued_packets(n
->vc
);
283 static int do_virtio_net_can_receive(VirtIONet
*n
, int bufsize
)
285 if (!virtio_queue_ready(n
->rx_vq
) ||
286 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
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);
296 virtio_queue_set_notification(n
->rx_vq
, 0);
300 static int virtio_net_can_receive(VLANClientState
*vc
)
302 VirtIONet
*n
= vc
->opaque
;
304 return do_virtio_net_can_receive(n
, VIRTIO_NET_MAX_BUFSIZE
);
307 static int iov_fill(struct iovec
*iov
, int iovcnt
, const void *buf
, int count
)
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
);
322 static int receive_header(VirtIONet
*n
, struct iovec
*iov
, int iovcnt
,
323 const void *buf
, size_t size
, size_t hdr_len
)
325 struct virtio_net_hdr
*hdr
= (struct virtio_net_hdr
*)iov
[0].iov_base
;
329 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
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.
334 iov
[0].iov_base
+= hdr_len
;
335 iov
[0].iov_len
-= hdr_len
;
340 static int receive_filter(VirtIONet
*n
, const uint8_t *buf
, int size
)
342 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
343 static const uint8_t vlan
[] = {0x81, 0x00};
344 uint8_t *ptr
= (uint8_t *)buf
;
350 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))))
356 if (ptr
[0] & 1) { // multicast
357 if (!memcmp(ptr
, bcast
, sizeof(bcast
))) {
359 } else if (n
->allmulti
|| n
->mac_table
.multi_overflow
) {
363 if (n
->mac_table
.uni_overflow
) {
365 } else if (!memcmp(ptr
, n
->mac
, ETH_ALEN
)) {
370 for (i
= 0; i
< n
->mac_table
.in_use
; i
++) {
371 if (!memcmp(ptr
, &n
->mac_table
.macs
[i
* ETH_ALEN
], ETH_ALEN
))
378 static ssize_t
virtio_net_receive(VLANClientState
*vc
, const uint8_t *buf
, size_t size
)
380 VirtIONet
*n
= vc
->opaque
;
381 struct virtio_net_hdr_mrg_rxbuf
*mhdr
= NULL
;
382 size_t hdr_len
, offset
, i
;
384 if (!do_virtio_net_can_receive(n
, size
))
387 if (!receive_filter(n
, buf
, size
))
390 /* 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
);
396 while (offset
< size
) {
397 VirtQueueElement elem
;
399 struct iovec sg
[VIRTQUEUE_MAX_SIZE
];
403 if ((i
!= 0 && !n
->mergeable_rx_bufs
) ||
404 virtqueue_pop(n
->rx_vq
, &elem
) == 0) {
407 fprintf(stderr
, "virtio-net truncating packet\n");
411 if (elem
.in_num
< 1) {
412 fprintf(stderr
, "virtio-net receive queue contains no in buffers\n");
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");
421 memcpy(&sg
, &elem
.in_sg
[0], sizeof(sg
[0]) * elem
.in_num
);
424 if (n
->mergeable_rx_bufs
)
425 mhdr
= (struct virtio_net_hdr_mrg_rxbuf
*)sg
[0].iov_base
;
427 offset
+= receive_header(n
, sg
, elem
.in_num
,
428 buf
+ offset
, size
- offset
, hdr_len
);
432 /* copy in packet. ugh */
433 len
= iov_fill(sg
, elem
.in_num
,
434 buf
+ offset
, size
- offset
);
437 /* signal other side */
438 virtqueue_fill(n
->rx_vq
, &elem
, total
, i
++);
444 mhdr
->num_buffers
= i
;
446 virtqueue_flush(n
->rx_vq
, i
);
447 virtio_notify(&n
->vdev
, n
->rx_vq
);
453 static void virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
)
455 VirtQueueElement elem
;
456 int has_vnet_hdr
= 0;
458 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
461 while (virtqueue_pop(vq
, &elem
)) {
463 unsigned int out_num
= elem
.out_num
;
464 struct iovec
*out_sg
= &elem
.out_sg
[0];
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
);
472 if (out_num
< 1 || out_sg
->iov_len
!= hdr_len
) {
473 fprintf(stderr
, "virtio-net header not in first element\n");
477 /* ignore the header if GSO is not supported */
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
;
489 len
+= qemu_sendv_packet(n
->vc
, out_sg
, out_num
);
491 virtqueue_push(vq
, &elem
, len
);
492 virtio_notify(&n
->vdev
, vq
);
496 static void virtio_net_handle_tx(VirtIODevice
*vdev
, VirtQueue
*vq
)
498 VirtIONet
*n
= to_virtio_net(vdev
);
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
);
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);
513 static void virtio_net_tx_timer(void *opaque
)
515 VirtIONet
*n
= opaque
;
517 n
->tx_timer_active
= 0;
519 /* Just in case the driver is not ready on more */
520 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
523 virtio_queue_set_notification(n
->tx_vq
, 1);
524 virtio_net_flush_tx(n
, n
->tx_vq
);
527 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
529 VirtIONet
*n
= opaque
;
531 virtio_save(&n
->vdev
, f
);
533 qemu_put_buffer(f
, n
->mac
, ETH_ALEN
);
534 qemu_put_be32(f
, n
->tx_timer_active
);
535 qemu_put_be32(f
, n
->mergeable_rx_bufs
);
536 qemu_put_be16(f
, n
->status
);
537 qemu_put_byte(f
, n
->promisc
);
538 qemu_put_byte(f
, n
->allmulti
);
539 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
);
541 qemu_put_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
542 qemu_put_be32(f
, 0); /* vnet-hdr placeholder */
543 qemu_put_byte(f
, n
->mac_table
.multi_overflow
);
544 qemu_put_byte(f
, n
->mac_table
.uni_overflow
);
547 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
549 VirtIONet
*n
= opaque
;
551 if (version_id
< 2 || version_id
> VIRTIO_NET_VM_VERSION
)
554 virtio_load(&n
->vdev
, f
);
556 qemu_get_buffer(f
, n
->mac
, ETH_ALEN
);
557 n
->tx_timer_active
= qemu_get_be32(f
);
558 n
->mergeable_rx_bufs
= qemu_get_be32(f
);
561 n
->status
= qemu_get_be16(f
);
563 if (version_id
>= 4) {
564 if (version_id
< 8) {
565 n
->promisc
= qemu_get_be32(f
);
566 n
->allmulti
= qemu_get_be32(f
);
568 n
->promisc
= qemu_get_byte(f
);
569 n
->allmulti
= qemu_get_byte(f
);
573 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
);
581 n
->mac_table
.multi_overflow
= n
->mac_table
.uni_overflow
= 1;
582 n
->mac_table
.in_use
= 0;
587 qemu_get_buffer(f
, (uint8_t *)n
->vlans
, MAX_VLAN
>> 3);
589 if (version_id
>= 7 && qemu_get_be32(f
)) {
591 "virtio-net: saved image requires vnet header support\n");
595 if (version_id
>= 9) {
596 n
->mac_table
.multi_overflow
= qemu_get_byte(f
);
597 n
->mac_table
.uni_overflow
= qemu_get_byte(f
);
600 if (n
->tx_timer_active
) {
601 qemu_mod_timer(n
->tx_timer
,
602 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
608 static void virtio_net_cleanup(VLANClientState
*vc
)
610 VirtIONet
*n
= vc
->opaque
;
612 unregister_savevm("virtio-net", n
);
614 qemu_free(n
->mac_table
.macs
);
617 qemu_del_timer(n
->tx_timer
);
618 qemu_free_timer(n
->tx_timer
);
620 virtio_cleanup(&n
->vdev
);
623 VirtIODevice
*virtio_net_init(DeviceState
*dev
)
626 static int virtio_net_id
;
628 n
= (VirtIONet
*)virtio_common_init("virtio-net", VIRTIO_ID_NET
,
629 sizeof(struct virtio_net_config
),
632 n
->vdev
.get_config
= virtio_net_get_config
;
633 n
->vdev
.set_config
= virtio_net_set_config
;
634 n
->vdev
.get_features
= virtio_net_get_features
;
635 n
->vdev
.set_features
= virtio_net_set_features
;
636 n
->vdev
.bad_features
= virtio_net_bad_features
;
637 n
->vdev
.reset
= virtio_net_reset
;
638 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
);
640 n
->ctrl_vq
= virtio_add_queue(&n
->vdev
, 16, virtio_net_handle_ctrl
);
641 qdev_get_macaddr(dev
, n
->mac
);
642 n
->status
= VIRTIO_NET_S_LINK_UP
;
643 n
->vc
= qdev_get_vlan_client(dev
,
644 virtio_net_can_receive
,
645 virtio_net_receive
, NULL
,
646 virtio_net_cleanup
, n
);
647 n
->vc
->link_status_changed
= virtio_net_set_link_status
;
649 qemu_format_nic_info_str(n
->vc
, n
->mac
);
651 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;
654 n
->promisc
= 1; /* for compatibility */
656 n
->mac_table
.macs
= qemu_mallocz(MAC_TABLE_ENTRIES
* ETH_ALEN
);
658 n
->vlans
= qemu_mallocz(MAX_VLAN
>> 3);
660 register_savevm("virtio-net", virtio_net_id
++, VIRTIO_NET_VM_VERSION
,
661 virtio_net_save
, virtio_net_load
, n
);