1 /* 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 #ifndef QEMU_VIRTIO_NET_H 15 #define QEMU_VIRTIO_NET_H 16 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" 23 24 #define TYPE_VIRTIO_NET "virtio-net-device" 25 typedef struct VirtIONet VirtIONet; 26 DECLARE_INSTANCE_CHECKER(VirtIONet, VIRTIO_NET, 27 TYPE_VIRTIO_NET) 28 29 #define TX_TIMER_INTERVAL 150000 /* 150 us */ 30 31 /* Limit the number of packets that can be sent via a single flush 32 * of the TX queue. This gives us a guaranteed exit condition and 33 * ensures fairness in the io path. 256 conveniently matches the 34 * length of the TX queue and shows a good balance of performance 35 * and latency. */ 36 #define TX_BURST 256 37 38 typedef struct virtio_net_conf 39 { 40 uint32_t txtimer; 41 int32_t txburst; 42 char *tx; 43 uint16_t rx_queue_size; 44 uint16_t tx_queue_size; 45 uint16_t mtu; 46 int32_t speed; 47 char *duplex_str; 48 uint8_t duplex; 49 char *primary_id_str; 50 } virtio_net_conf; 51 52 /* Coalesced packets type & status */ 53 typedef enum { 54 RSC_COALESCE, /* Data been coalesced */ 55 RSC_FINAL, /* Will terminate current connection */ 56 RSC_NO_MATCH, /* No matched in the buffer pool */ 57 RSC_BYPASS, /* Packet to be bypass, not tcp, tcp ctrl, etc */ 58 RSC_CANDIDATE /* Data want to be coalesced */ 59 } CoalesceStatus; 60 61 typedef struct VirtioNetRscStat { 62 uint32_t received; 63 uint32_t coalesced; 64 uint32_t over_size; 65 uint32_t cache; 66 uint32_t empty_cache; 67 uint32_t no_match_cache; 68 uint32_t win_update; 69 uint32_t no_match; 70 uint32_t tcp_syn; 71 uint32_t tcp_ctrl_drain; 72 uint32_t dup_ack; 73 uint32_t dup_ack1; 74 uint32_t dup_ack2; 75 uint32_t pure_ack; 76 uint32_t ack_out_of_win; 77 uint32_t data_out_of_win; 78 uint32_t data_out_of_order; 79 uint32_t data_after_pure_ack; 80 uint32_t bypass_not_tcp; 81 uint32_t tcp_option; 82 uint32_t tcp_all_opt; 83 uint32_t ip_frag; 84 uint32_t ip_ecn; 85 uint32_t ip_hacked; 86 uint32_t ip_option; 87 uint32_t purge_failed; 88 uint32_t drain_failed; 89 uint32_t final_failed; 90 int64_t timer; 91 } VirtioNetRscStat; 92 93 /* Rsc unit general info used to checking if can coalescing */ 94 typedef struct VirtioNetRscUnit { 95 void *ip; /* ip header */ 96 uint16_t *ip_plen; /* data len pointer in ip header field */ 97 struct tcp_header *tcp; /* tcp header */ 98 uint16_t tcp_hdrlen; /* tcp header len */ 99 uint16_t payload; /* pure payload without virtio/eth/ip/tcp */ 100 } VirtioNetRscUnit; 101 102 /* Coalesced segment */ 103 typedef struct VirtioNetRscSeg { 104 QTAILQ_ENTRY(VirtioNetRscSeg) next; 105 void *buf; 106 size_t size; 107 uint16_t packets; 108 uint16_t dup_ack; 109 bool is_coalesced; /* need recal ipv4 header checksum, mark here */ 110 VirtioNetRscUnit unit; 111 NetClientState *nc; 112 } VirtioNetRscSeg; 113 114 115 /* Chain is divided by protocol(ipv4/v6) and NetClientInfo */ 116 typedef struct VirtioNetRscChain { 117 QTAILQ_ENTRY(VirtioNetRscChain) next; 118 VirtIONet *n; /* VirtIONet */ 119 uint16_t proto; 120 uint8_t gso_type; 121 uint16_t max_payload; 122 QEMUTimer *drain_timer; 123 QTAILQ_HEAD(, VirtioNetRscSeg) buffers; 124 VirtioNetRscStat stat; 125 } VirtioNetRscChain; 126 127 /* Maximum packet size we can receive from tap device: header + 64k */ 128 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 * KiB)) 129 130 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40 131 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128 132 133 typedef struct VirtioNetRssData { 134 bool enabled; 135 bool redirect; 136 bool populate_hash; 137 uint32_t hash_types; 138 uint8_t key[VIRTIO_NET_RSS_MAX_KEY_SIZE]; 139 uint16_t indirections_len; 140 uint16_t *indirections_table; 141 uint16_t default_queue; 142 } VirtioNetRssData; 143 144 typedef struct VirtIONetQueue { 145 VirtQueue *rx_vq; 146 VirtQueue *tx_vq; 147 QEMUTimer *tx_timer; 148 QEMUBH *tx_bh; 149 uint32_t tx_waiting; 150 struct { 151 VirtQueueElement *elem; 152 } async_tx; 153 struct VirtIONet *n; 154 } VirtIONetQueue; 155 156 struct VirtIONet { 157 VirtIODevice parent_obj; 158 uint8_t mac[ETH_ALEN]; 159 uint16_t status; 160 VirtIONetQueue *vqs; 161 VirtQueue *ctrl_vq; 162 NICState *nic; 163 /* RSC Chains - temporary storage of coalesced data, 164 all these data are lost in case of migration */ 165 QTAILQ_HEAD(, VirtioNetRscChain) rsc_chains; 166 uint32_t tx_timeout; 167 int32_t tx_burst; 168 uint32_t has_vnet_hdr; 169 size_t host_hdr_len; 170 size_t guest_hdr_len; 171 uint64_t host_features; 172 uint32_t rsc_timeout; 173 uint8_t rsc4_enabled; 174 uint8_t rsc6_enabled; 175 uint8_t has_ufo; 176 uint32_t mergeable_rx_bufs; 177 uint8_t promisc; 178 uint8_t allmulti; 179 uint8_t alluni; 180 uint8_t nomulti; 181 uint8_t nouni; 182 uint8_t nobcast; 183 uint8_t vhost_started; 184 struct { 185 uint32_t in_use; 186 uint32_t first_multi; 187 uint8_t multi_overflow; 188 uint8_t uni_overflow; 189 uint8_t *macs; 190 } mac_table; 191 uint32_t *vlans; 192 virtio_net_conf net_conf; 193 NICConf nic_conf; 194 DeviceState *qdev; 195 int multiqueue; 196 uint16_t max_queues; 197 uint16_t curr_queues; 198 size_t config_size; 199 char *netclient_name; 200 char *netclient_type; 201 uint64_t curr_guest_offloads; 202 /* used on saved state restore phase to preserve the curr_guest_offloads */ 203 uint64_t saved_guest_offloads; 204 AnnounceTimer announce_timer; 205 bool needs_vnet_hdr_swap; 206 bool mtu_bypass_backend; 207 QemuOpts *primary_device_opts; 208 QDict *primary_device_dict; 209 DeviceState *primary_dev; 210 BusState *primary_bus; 211 char *primary_device_id; 212 char *standby_id; 213 bool primary_should_be_hidden; 214 bool failover; 215 DeviceListener primary_listener; 216 Notifier migration_state; 217 VirtioNetRssData rss_data; 218 struct NetRxPkt *rx_pkt; 219 }; 220 221 void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 222 const char *type); 223 224 #endif 225