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