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"
19 /* from Linux's virtio_net.h */
21 /* The ID for virtio_net */
22 #define VIRTIO_ID_NET 1
24 /* The feature bitmap for virtio net */
25 #define VIRTIO_NET_F_CSUM 0 /* Host handles pkts w/ partial csum */
26 #define VIRTIO_NET_F_GUEST_CSUM 1 /* Guest handles pkts w/ partial csum */
27 #define VIRTIO_NET_F_MAC 5 /* Host has given MAC address. */
28 #define VIRTIO_NET_F_GSO 6 /* Host handles pkts w/ any GSO type */
29 #define VIRTIO_NET_F_GUEST_TSO4 7 /* Guest can handle TSOv4 in. */
30 #define VIRTIO_NET_F_GUEST_TSO6 8 /* Guest can handle TSOv6 in. */
31 #define VIRTIO_NET_F_GUEST_ECN 9 /* Guest can handle TSO[6] w/ ECN in. */
32 #define VIRTIO_NET_F_GUEST_UFO 10 /* Guest can handle UFO in. */
33 #define VIRTIO_NET_F_HOST_TSO4 11 /* Host can handle TSOv4 in. */
34 #define VIRTIO_NET_F_HOST_TSO6 12 /* Host can handle TSOv6 in. */
35 #define VIRTIO_NET_F_HOST_ECN 13 /* Host can handle TSO[6] w/ ECN in. */
36 #define VIRTIO_NET_F_HOST_UFO 14 /* Host can handle UFO in. */
38 #define TX_TIMER_INTERVAL 250000 /* 250 us */
40 /* The config defining mac address (6 bytes) */
41 struct virtio_net_config
44 } __attribute__((packed
));
46 /* This is the first element of the scatter-gather list. If you don't
47 * specify GSO or CSUM features, you can simply ignore the header. */
50 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 // Use csum_start, csum_offset
52 #define VIRTIO_NET_HDR_GSO_NONE 0 // Not a GSO frame
53 #define VIRTIO_NET_HDR_GSO_TCPV4 1 // GSO frame, IPv4 TCP (TSO)
54 #define VIRTIO_NET_HDR_GSO_UDP 3 // GSO frame, IPv4 UDP (UFO)
55 #define VIRTIO_NET_HDR_GSO_TCPV6 4 // GSO frame, IPv6 TCP
56 #define VIRTIO_NET_HDR_GSO_ECN 0x80 // TCP has ECN set
64 typedef struct VirtIONet
76 * - we could suppress RX interrupt if we were so inclined.
79 static VirtIONet
*to_virtio_net(VirtIODevice
*vdev
)
81 return (VirtIONet
*)vdev
;
84 static void virtio_net_update_config(VirtIODevice
*vdev
, uint8_t *config
)
86 VirtIONet
*n
= to_virtio_net(vdev
);
87 struct virtio_net_config netcfg
;
89 memcpy(netcfg
.mac
, n
->mac
, 6);
90 memcpy(config
, &netcfg
, sizeof(netcfg
));
93 static uint32_t virtio_net_get_features(VirtIODevice
*vdev
)
95 VirtIONet
*n
= to_virtio_net(vdev
);
96 VLANClientState
*host
= n
->vc
->vlan
->first_client
;
97 uint32_t features
= (1 << VIRTIO_NET_F_MAC
);
99 if (tap_has_vnet_hdr(host
)) {
100 features
|= (1 << VIRTIO_NET_F_CSUM
);
101 features
|= (1 << VIRTIO_NET_F_GUEST_CSUM
);
102 features
|= (1 << VIRTIO_NET_F_GUEST_TSO4
);
103 features
|= (1 << VIRTIO_NET_F_GUEST_TSO6
);
104 features
|= (1 << VIRTIO_NET_F_GUEST_ECN
);
105 features
|= (1 << VIRTIO_NET_F_HOST_TSO4
);
106 features
|= (1 << VIRTIO_NET_F_HOST_TSO6
);
107 features
|= (1 << VIRTIO_NET_F_HOST_ECN
);
108 /* Kernel can't actually handle UFO in software currently. */
114 static void virtio_net_set_features(VirtIODevice
*vdev
, uint32_t features
)
116 VirtIONet
*n
= to_virtio_net(vdev
);
117 VLANClientState
*host
= n
->vc
->vlan
->first_client
;
119 if (!tap_has_vnet_hdr(host
) || !host
->set_offload
)
122 host
->set_offload(host
,
123 (features
>> VIRTIO_NET_F_GUEST_CSUM
) & 1,
124 (features
>> VIRTIO_NET_F_GUEST_TSO4
) & 1,
125 (features
>> VIRTIO_NET_F_GUEST_TSO6
) & 1,
126 (features
>> VIRTIO_NET_F_GUEST_ECN
) & 1);
131 static void virtio_net_handle_rx(VirtIODevice
*vdev
, VirtQueue
*vq
)
133 /* We now have RX buffers, signal to the IO thread to break out of the
134 select to re-poll the tap file descriptor */
136 qemu_kvm_notify_work();
139 static int virtio_net_can_receive(void *opaque
)
141 VirtIONet
*n
= opaque
;
143 if (n
->rx_vq
->vring
.avail
== NULL
||
144 !(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
147 if (n
->rx_vq
->vring
.avail
->idx
== n
->rx_vq
->last_avail_idx
) {
148 n
->rx_vq
->vring
.used
->flags
&= ~VRING_USED_F_NO_NOTIFY
;
152 n
->rx_vq
->vring
.used
->flags
|= VRING_USED_F_NO_NOTIFY
;
156 static void virtio_net_receive(void *opaque
, const uint8_t *buf
, int size
)
158 VirtIONet
*n
= opaque
;
159 VirtQueueElement elem
;
160 struct virtio_net_hdr
*hdr
;
164 if (virtqueue_pop(n
->rx_vq
, &elem
) == 0)
167 if (elem
.in_num
< 1 || elem
.in_sg
[0].iov_len
!= sizeof(*hdr
)) {
168 fprintf(stderr
, "virtio-net header not in first element\n");
172 hdr
= (void *)elem
.in_sg
[0].iov_base
;
174 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
177 total
= sizeof(*hdr
);
179 if (tap_has_vnet_hdr(n
->vc
->vlan
->first_client
)) {
180 memcpy(hdr
, buf
, sizeof(*hdr
));
184 /* copy in packet. ugh */
186 while (offset
< size
&& i
< elem
.in_num
) {
187 int len
= MIN(elem
.in_sg
[i
].iov_len
, size
- offset
);
188 memcpy(elem
.in_sg
[i
].iov_base
, buf
+ offset
, len
);
194 /* signal other side */
195 virtqueue_push(n
->rx_vq
, &elem
, total
);
196 virtio_notify(&n
->vdev
, n
->rx_vq
);
200 static void virtio_net_flush_tx(VirtIONet
*n
, VirtQueue
*vq
)
202 VirtQueueElement elem
;
203 int has_vnet_hdr
= tap_has_vnet_hdr(n
->vc
->vlan
->first_client
);
205 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
208 while (virtqueue_pop(vq
, &elem
)) {
210 unsigned int out_num
= elem
.out_num
;
211 struct iovec
*out_sg
= &elem
.out_sg
[0];
213 if (out_num
< 1 || out_sg
->iov_len
!= sizeof(struct virtio_net_hdr
)) {
214 fprintf(stderr
, "virtio-net header not in first element\n");
218 /* ignore the header if GSO is not supported */
222 len
+= sizeof(struct virtio_net_hdr
);
225 len
+= qemu_sendv_packet(n
->vc
, out_sg
, out_num
);
227 virtqueue_push(vq
, &elem
, len
);
228 virtio_notify(&n
->vdev
, vq
);
232 static void virtio_net_handle_tx(VirtIODevice
*vdev
, VirtQueue
*vq
)
234 VirtIONet
*n
= to_virtio_net(vdev
);
236 if (n
->tx_timer_active
) {
237 vq
->vring
.used
->flags
&= ~VRING_USED_F_NO_NOTIFY
;
238 qemu_del_timer(n
->tx_timer
);
239 n
->tx_timer_active
= 0;
240 virtio_net_flush_tx(n
, vq
);
242 qemu_mod_timer(n
->tx_timer
,
243 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
244 n
->tx_timer_active
= 1;
245 vq
->vring
.used
->flags
|= VRING_USED_F_NO_NOTIFY
;
249 static void virtio_net_tx_timer(void *opaque
)
251 VirtIONet
*n
= opaque
;
253 n
->tx_timer_active
= 0;
255 /* Just in case the driver is not ready on more */
256 if (!(n
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
))
259 n
->tx_vq
->vring
.used
->flags
&= ~VRING_USED_F_NO_NOTIFY
;
260 virtio_net_flush_tx(n
, n
->tx_vq
);
263 static void virtio_net_save(QEMUFile
*f
, void *opaque
)
265 VirtIONet
*n
= opaque
;
267 virtio_save(&n
->vdev
, f
);
269 qemu_put_buffer(f
, n
->mac
, 6);
270 qemu_put_be32(f
, n
->tx_timer_active
);
273 static int virtio_net_load(QEMUFile
*f
, void *opaque
, int version_id
)
275 VirtIONet
*n
= opaque
;
280 virtio_load(&n
->vdev
, f
);
282 qemu_get_buffer(f
, n
->mac
, 6);
283 n
->tx_timer_active
= qemu_get_be32(f
);
285 if (n
->tx_timer_active
) {
286 qemu_mod_timer(n
->tx_timer
,
287 qemu_get_clock(vm_clock
) + TX_TIMER_INTERVAL
);
293 PCIDevice
*virtio_net_init(PCIBus
*bus
, NICInfo
*nd
, int devfn
)
296 static int virtio_net_id
;
298 n
= (VirtIONet
*)virtio_init_pci(bus
, "virtio-net", 6900, 0x1000,
301 6, sizeof(VirtIONet
));
305 n
->vdev
.update_config
= virtio_net_update_config
;
306 n
->vdev
.get_features
= virtio_net_get_features
;
307 n
->vdev
.set_features
= virtio_net_set_features
;
308 n
->rx_vq
= virtio_add_queue(&n
->vdev
, 128, virtio_net_handle_rx
);
309 n
->tx_vq
= virtio_add_queue(&n
->vdev
, 128, virtio_net_handle_tx
);
310 memcpy(n
->mac
, nd
->macaddr
, 6);
311 n
->vc
= qemu_new_vlan_client(nd
->vlan
, virtio_net_receive
,
312 virtio_net_can_receive
, n
);
314 n
->tx_timer
= qemu_new_timer(vm_clock
, virtio_net_tx_timer
, n
);
315 n
->tx_timer_active
= 0;
317 register_savevm("virtio-net", virtio_net_id
++, 1,
318 virtio_net_save
, virtio_net_load
, n
);
320 return (PCIDevice
*)n
;