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.
14 #ifndef QEMU_VIRTIO_NET_H
15 #define QEMU_VIRTIO_NET_H
17 #include "qemu/units.h"
18 #include "standard-headers/linux/virtio_net.h"
19 #include "hw/virtio/virtio.h"
20 #include "net/announce.h"
21 #include "qemu/option_int.h"
22 #include "qom/object.h"
24 #define TYPE_VIRTIO_NET "virtio-net-device"
25 OBJECT_DECLARE_SIMPLE_TYPE(VirtIONet
, VIRTIO_NET
)
27 #define TX_TIMER_INTERVAL 150000 /* 150 us */
29 /* Limit the number of packets that can be sent via a single flush
30 * of the TX queue. This gives us a guaranteed exit condition and
31 * ensures fairness in the io path. 256 conveniently matches the
32 * length of the TX queue and shows a good balance of performance
36 typedef struct virtio_net_conf
41 uint16_t rx_queue_size
;
42 uint16_t tx_queue_size
;
50 /* Coalesced packets type & status */
52 RSC_COALESCE
, /* Data been coalesced */
53 RSC_FINAL
, /* Will terminate current connection */
54 RSC_NO_MATCH
, /* No matched in the buffer pool */
55 RSC_BYPASS
, /* Packet to be bypass, not tcp, tcp ctrl, etc */
56 RSC_CANDIDATE
/* Data want to be coalesced */
59 typedef struct VirtioNetRscStat
{
65 uint32_t no_match_cache
;
69 uint32_t tcp_ctrl_drain
;
74 uint32_t ack_out_of_win
;
75 uint32_t data_out_of_win
;
76 uint32_t data_out_of_order
;
77 uint32_t data_after_pure_ack
;
78 uint32_t bypass_not_tcp
;
85 uint32_t purge_failed
;
86 uint32_t drain_failed
;
87 uint32_t final_failed
;
91 /* Rsc unit general info used to checking if can coalescing */
92 typedef struct VirtioNetRscUnit
{
93 void *ip
; /* ip header */
94 uint16_t *ip_plen
; /* data len pointer in ip header field */
95 struct tcp_header
*tcp
; /* tcp header */
96 uint16_t tcp_hdrlen
; /* tcp header len */
97 uint16_t payload
; /* pure payload without virtio/eth/ip/tcp */
100 /* Coalesced segment */
101 typedef struct VirtioNetRscSeg
{
102 QTAILQ_ENTRY(VirtioNetRscSeg
) next
;
107 bool is_coalesced
; /* need recal ipv4 header checksum, mark here */
108 VirtioNetRscUnit unit
;
113 /* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
114 typedef struct VirtioNetRscChain
{
115 QTAILQ_ENTRY(VirtioNetRscChain
) next
;
116 VirtIONet
*n
; /* VirtIONet */
119 uint16_t max_payload
;
120 QEMUTimer
*drain_timer
;
121 QTAILQ_HEAD(, VirtioNetRscSeg
) buffers
;
122 VirtioNetRscStat stat
;
125 /* Maximum packet size we can receive from tap device: header + 64k */
126 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 * KiB))
128 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
129 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
131 typedef struct VirtioNetRssData
{
136 uint8_t key
[VIRTIO_NET_RSS_MAX_KEY_SIZE
];
137 uint16_t indirections_len
;
138 uint16_t *indirections_table
;
139 uint16_t default_queue
;
142 typedef struct VirtIONetQueue
{
149 VirtQueueElement
*elem
;
155 VirtIODevice parent_obj
;
156 uint8_t mac
[ETH_ALEN
];
161 /* RSC Chains - temporary storage of coalesced data,
162 all these data are lost in case of migration */
163 QTAILQ_HEAD(, VirtioNetRscChain
) rsc_chains
;
166 uint32_t has_vnet_hdr
;
168 size_t guest_hdr_len
;
169 uint64_t host_features
;
170 uint32_t rsc_timeout
;
171 uint8_t rsc4_enabled
;
172 uint8_t rsc6_enabled
;
174 uint32_t mergeable_rx_bufs
;
181 uint8_t vhost_started
;
184 uint32_t first_multi
;
185 uint8_t multi_overflow
;
186 uint8_t uni_overflow
;
190 virtio_net_conf net_conf
;
195 uint16_t curr_queues
;
197 char *netclient_name
;
198 char *netclient_type
;
199 uint64_t curr_guest_offloads
;
200 /* used on saved state restore phase to preserve the curr_guest_offloads */
201 uint64_t saved_guest_offloads
;
202 AnnounceTimer announce_timer
;
203 bool needs_vnet_hdr_swap
;
204 bool mtu_bypass_backend
;
205 DeviceState
*primary_dev
;
206 /* primary failover device is hidden*/
207 bool failover_primary_hidden
;
209 DeviceListener primary_listener
;
210 Notifier migration_state
;
211 VirtioNetRssData rss_data
;
212 struct NetRxPkt
*rx_pkt
;
215 void virtio_net_set_netclient_name(VirtIONet
*n
, const char *name
,