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 #include "ebpf/ebpf_rss.h" 25 26 #define TYPE_VIRTIO_NET "virtio-net-device" 27 OBJECT_DECLARE_SIMPLE_TYPE(VirtIONet, 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 /* Maximum VIRTIO_NET_CTRL_MAC_TABLE_SET unicast + multicast entries. */ 39 #define MAC_TABLE_ENTRIES 64 40 41 /* 42 * The maximum number of VLANs in the VLAN filter table 43 * added by VIRTIO_NET_CTRL_VLAN_ADD 44 */ 45 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ 46 47 typedef struct virtio_net_conf 48 { 49 uint32_t txtimer; 50 int32_t txburst; 51 char *tx; 52 uint16_t rx_queue_size; 53 uint16_t tx_queue_size; 54 uint16_t mtu; 55 int32_t speed; 56 char *duplex_str; 57 uint8_t duplex; 58 char *primary_id_str; 59 } virtio_net_conf; 60 61 /* Coalesced packets type & status */ 62 typedef enum { 63 RSC_COALESCE, /* Data been coalesced */ 64 RSC_FINAL, /* Will terminate current connection */ 65 RSC_NO_MATCH, /* No matched in the buffer pool */ 66 RSC_BYPASS, /* Packet to be bypass, not tcp, tcp ctrl, etc */ 67 RSC_CANDIDATE /* Data want to be coalesced */ 68 } CoalesceStatus; 69 70 typedef struct VirtioNetRscStat { 71 uint32_t received; 72 uint32_t coalesced; 73 uint32_t over_size; 74 uint32_t cache; 75 uint32_t empty_cache; 76 uint32_t no_match_cache; 77 uint32_t win_update; 78 uint32_t no_match; 79 uint32_t tcp_syn; 80 uint32_t tcp_ctrl_drain; 81 uint32_t dup_ack; 82 uint32_t dup_ack1; 83 uint32_t dup_ack2; 84 uint32_t pure_ack; 85 uint32_t ack_out_of_win; 86 uint32_t data_out_of_win; 87 uint32_t data_out_of_order; 88 uint32_t data_after_pure_ack; 89 uint32_t bypass_not_tcp; 90 uint32_t tcp_option; 91 uint32_t tcp_all_opt; 92 uint32_t ip_frag; 93 uint32_t ip_ecn; 94 uint32_t ip_hacked; 95 uint32_t ip_option; 96 uint32_t purge_failed; 97 uint32_t drain_failed; 98 uint32_t final_failed; 99 int64_t timer; 100 } VirtioNetRscStat; 101 102 /* Rsc unit general info used to checking if can coalescing */ 103 typedef struct VirtioNetRscUnit { 104 void *ip; /* ip header */ 105 void *ip_plen; /* pointer to unaligned uint16_t data len in ip header */ 106 struct tcp_header *tcp; /* tcp header */ 107 uint16_t tcp_hdrlen; /* tcp header len */ 108 uint16_t payload; /* pure payload without virtio/eth/ip/tcp */ 109 } VirtioNetRscUnit; 110 111 /* Coalesced segment */ 112 typedef struct VirtioNetRscSeg { 113 QTAILQ_ENTRY(VirtioNetRscSeg) next; 114 void *buf; 115 size_t size; 116 uint16_t packets; 117 uint16_t dup_ack; 118 bool is_coalesced; /* need recall ipv4 header checksum, mark here */ 119 VirtioNetRscUnit unit; 120 NetClientState *nc; 121 } VirtioNetRscSeg; 122 123 124 /* Chain is divided by protocol(ipv4/v6) and NetClientInfo */ 125 typedef struct VirtioNetRscChain { 126 QTAILQ_ENTRY(VirtioNetRscChain) next; 127 VirtIONet *n; /* VirtIONet */ 128 uint16_t proto; 129 uint8_t gso_type; 130 uint16_t max_payload; 131 QEMUTimer *drain_timer; 132 QTAILQ_HEAD(, VirtioNetRscSeg) buffers; 133 VirtioNetRscStat stat; 134 } VirtioNetRscChain; 135 136 /* Maximum packet size we can receive from tap device: header + 64k */ 137 #define VIRTIO_NET_MAX_BUFSIZE (sizeof(struct virtio_net_hdr) + (64 * KiB)) 138 139 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40 140 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128 141 142 typedef struct VirtioNetRssData { 143 bool enabled; 144 bool enabled_software_rss; 145 bool redirect; 146 bool populate_hash; 147 bool peer_hash_available; 148 uint32_t runtime_hash_types; 149 uint32_t supported_hash_types; 150 uint32_t peer_hash_types; 151 OnOffAutoBit64 specified_hash_types; 152 uint8_t key[VIRTIO_NET_RSS_MAX_KEY_SIZE]; 153 uint16_t indirections_len; 154 uint16_t *indirections_table; 155 uint16_t default_queue; 156 } VirtioNetRssData; 157 158 typedef struct VirtIONetQueue { 159 VirtQueue *rx_vq; 160 VirtQueue *tx_vq; 161 QEMUTimer *tx_timer; 162 QEMUBH *tx_bh; 163 uint32_t tx_waiting; 164 struct { 165 VirtQueueElement *elem; 166 } async_tx; 167 struct VirtIONet *n; 168 } VirtIONetQueue; 169 170 struct VirtIONet { 171 VirtIODevice parent_obj; 172 uint8_t mac[ETH_ALEN]; 173 uint16_t status; 174 VirtIONetQueue *vqs; 175 VirtQueue *ctrl_vq; 176 NICState *nic; 177 /* RSC Chains - temporary storage of coalesced data, 178 all these data are lost in case of migration */ 179 QTAILQ_HEAD(, VirtioNetRscChain) rsc_chains; 180 uint32_t tx_timeout; 181 int32_t tx_burst; 182 uint32_t has_vnet_hdr; 183 size_t host_hdr_len; 184 size_t guest_hdr_len; 185 uint64_t host_features; 186 uint32_t rsc_timeout; 187 uint8_t rsc4_enabled; 188 uint8_t rsc6_enabled; 189 uint8_t has_ufo; 190 uint32_t mergeable_rx_bufs; 191 uint8_t promisc; 192 uint8_t allmulti; 193 uint8_t alluni; 194 uint8_t nomulti; 195 uint8_t nouni; 196 uint8_t nobcast; 197 uint8_t vhost_started; 198 struct { 199 uint32_t in_use; 200 uint32_t first_multi; 201 uint8_t multi_overflow; 202 uint8_t uni_overflow; 203 uint8_t *macs; 204 } mac_table; 205 uint32_t *vlans; 206 virtio_net_conf net_conf; 207 NICConf nic_conf; 208 DeviceState *qdev; 209 int multiqueue; 210 uint16_t max_queue_pairs; 211 uint16_t curr_queue_pairs; 212 uint16_t max_ncs; 213 size_t config_size; 214 char *netclient_name; 215 char *netclient_type; 216 uint64_t curr_guest_offloads; 217 /* used on saved state restore phase to preserve the curr_guest_offloads */ 218 uint64_t saved_guest_offloads; 219 AnnounceTimer announce_timer; 220 bool needs_vnet_hdr_swap; 221 bool mtu_bypass_backend; 222 /* primary failover device is hidden*/ 223 bool failover_primary_hidden; 224 bool failover; 225 DeviceListener primary_listener; 226 QDict *primary_opts; 227 bool primary_opts_from_json; 228 NotifierWithReturn migration_state; 229 VirtioNetRssData rss_data; 230 struct NetRxPkt *rx_pkt; 231 struct EBPFRSSContext ebpf_rss; 232 uint32_t nr_ebpf_rss_fds; 233 char **ebpf_rss_fds; 234 }; 235 236 size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev, 237 const struct iovec *in_sg, unsigned in_num, 238 const struct iovec *out_sg, 239 unsigned out_num); 240 void virtio_net_set_netclient_name(VirtIONet *n, const char *name, 241 const char *type); 242 uint64_t virtio_net_supported_guest_offloads(const VirtIONet *n); 243 244 #endif 245