1 #ifndef QEMU_NET_H 2 #define QEMU_NET_H 3 4 #include "qemu/queue.h" 5 #include "qapi/qapi-types-net.h" 6 #include "net/queue.h" 7 #include "hw/qdev-properties-system.h" 8 9 #define MAC_FMT "%02X:%02X:%02X:%02X:%02X:%02X" 10 #define MAC_ARG(x) ((uint8_t *)(x))[0], ((uint8_t *)(x))[1], \ 11 ((uint8_t *)(x))[2], ((uint8_t *)(x))[3], \ 12 ((uint8_t *)(x))[4], ((uint8_t *)(x))[5] 13 14 #define MAX_QUEUE_NUM 1024 15 16 /* Maximum GSO packet size (64k) plus plenty of room for 17 * the ethernet and virtio_net headers 18 */ 19 #define NET_BUFSIZE (4096 + 65536) 20 21 struct MACAddr { 22 uint8_t a[6]; 23 }; 24 25 /* qdev nic properties */ 26 27 typedef struct NICPeers { 28 NetClientState *ncs[MAX_QUEUE_NUM]; 29 int32_t queues; 30 } NICPeers; 31 32 typedef struct NICConf { 33 MACAddr macaddr; 34 NICPeers peers; 35 int32_t bootindex; 36 } NICConf; 37 38 typedef struct NetOffloads { 39 bool csum; 40 bool tso4; 41 bool tso6; 42 bool ecn; 43 bool ufo; 44 bool uso4; 45 bool uso6; 46 bool tnl; 47 bool tnl_csum; 48 } NetOffloads; 49 50 #define DEFINE_NIC_PROPERTIES(_state, _conf) \ 51 DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \ 52 DEFINE_PROP_NETDEV("netdev", _state, _conf.peers) 53 54 55 /* Net clients */ 56 57 typedef void (NetPoll)(NetClientState *, bool enable); 58 typedef bool (NetCanReceive)(NetClientState *); 59 typedef int (NetStart)(NetClientState *); 60 typedef int (NetLoad)(NetClientState *); 61 typedef void (NetStop)(NetClientState *); 62 typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t); 63 typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int); 64 typedef void (NetCleanup) (NetClientState *); 65 typedef void (LinkStatusChanged)(NetClientState *); 66 typedef void (NetClientDestructor)(NetClientState *); 67 typedef RxFilterInfo *(QueryRxFilter)(NetClientState *); 68 typedef bool (HasUfo)(NetClientState *); 69 typedef bool (HasUso)(NetClientState *); 70 typedef bool (HasTunnel)(NetClientState *); 71 typedef bool (HasVnetHdr)(NetClientState *); 72 typedef bool (HasVnetHdrLen)(NetClientState *, int); 73 typedef void (SetOffload)(NetClientState *, const NetOffloads *); 74 typedef int (GetVnetHdrLen)(NetClientState *); 75 typedef void (SetVnetHdrLen)(NetClientState *, int); 76 typedef bool (GetVnetHashSupportedTypes)(NetClientState *, uint32_t *); 77 typedef int (SetVnetLE)(NetClientState *, bool); 78 typedef int (SetVnetBE)(NetClientState *, bool); 79 typedef struct SocketReadState SocketReadState; 80 typedef void (SocketReadStateFinalize)(SocketReadState *rs); 81 typedef void (NetAnnounce)(NetClientState *); 82 typedef bool (SetSteeringEBPF)(NetClientState *, int); 83 typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **); 84 typedef struct vhost_net *(GetVHostNet)(NetClientState *nc); 85 86 typedef struct NetClientInfo { 87 NetClientDriver type; 88 size_t size; 89 NetReceive *receive; 90 NetReceiveIOV *receive_iov; 91 NetCanReceive *can_receive; 92 NetStart *start; 93 NetLoad *load; 94 NetStop *stop; 95 NetCleanup *cleanup; 96 LinkStatusChanged *link_status_changed; 97 QueryRxFilter *query_rx_filter; 98 NetPoll *poll; 99 HasUfo *has_ufo; 100 HasUso *has_uso; 101 HasTunnel *has_tunnel; 102 HasVnetHdr *has_vnet_hdr; 103 HasVnetHdrLen *has_vnet_hdr_len; 104 SetOffload *set_offload; 105 SetVnetHdrLen *set_vnet_hdr_len; 106 SetVnetLE *set_vnet_le; 107 SetVnetBE *set_vnet_be; 108 GetVnetHashSupportedTypes *get_vnet_hash_supported_types; 109 NetAnnounce *announce; 110 SetSteeringEBPF *set_steering_ebpf; 111 NetCheckPeerType *check_peer_type; 112 GetVHostNet *get_vhost_net; 113 } NetClientInfo; 114 115 struct NetClientState { 116 NetClientInfo *info; 117 int link_down; 118 QTAILQ_ENTRY(NetClientState) next; 119 NetClientState *peer; 120 NetQueue *incoming_queue; 121 char *model; 122 char *name; 123 char info_str[256]; 124 unsigned receive_disabled : 1; 125 NetClientDestructor *destructor; 126 unsigned int queue_index; 127 unsigned rxfilter_notify_enabled:1; 128 int vring_enable; 129 int vnet_hdr_len; 130 bool is_netdev; 131 bool do_not_pad; /* do not pad to the minimum ethernet frame length */ 132 bool is_datapath; 133 QTAILQ_HEAD(, NetFilterState) filters; 134 }; 135 136 typedef QTAILQ_HEAD(NetClientStateList, NetClientState) NetClientStateList; 137 138 typedef struct NICState { 139 NetClientState *ncs; 140 NICConf *conf; 141 MemReentrancyGuard *reentrancy_guard; 142 void *opaque; 143 bool peer_deleted; 144 } NICState; 145 146 struct SocketReadState { 147 /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */ 148 int state; 149 /* This flag decide whether to read the vnet_hdr_len field */ 150 bool vnet_hdr; 151 uint32_t index; 152 uint32_t packet_len; 153 uint32_t vnet_hdr_len; 154 uint8_t buf[NET_BUFSIZE]; 155 SocketReadStateFinalize *finalize; 156 }; 157 158 int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size); 159 char *qemu_mac_strdup_printf(const uint8_t *macaddr); 160 NetClientState *qemu_find_netdev(const char *id); 161 int qemu_find_net_clients_except(const char *id, NetClientState **ncs, 162 NetClientDriver type, int max); 163 NetClientState *qemu_new_net_client(NetClientInfo *info, 164 NetClientState *peer, 165 const char *model, 166 const char *name); 167 NetClientState *qemu_new_net_control_client(NetClientInfo *info, 168 NetClientState *peer, 169 const char *model, 170 const char *name); 171 NICState *qemu_new_nic(NetClientInfo *info, 172 NICConf *conf, 173 const char *model, 174 const char *name, 175 MemReentrancyGuard *reentrancy_guard, 176 void *opaque); 177 void qemu_del_nic(NICState *nic); 178 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index); 179 NetClientState *qemu_get_queue(NICState *nic); 180 NICState *qemu_get_nic(NetClientState *nc); 181 void *qemu_get_nic_opaque(NetClientState *nc); 182 void qemu_del_net_client(NetClientState *nc); 183 typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque); 184 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque); 185 int qemu_can_receive_packet(NetClientState *nc); 186 int qemu_can_send_packet(NetClientState *nc); 187 ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, 188 int iovcnt); 189 ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov, 190 int iovcnt, NetPacketSent *sent_cb); 191 ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size); 192 ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size); 193 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size); 194 ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf, 195 int size, NetPacketSent *sent_cb); 196 void qemu_purge_queued_packets(NetClientState *nc); 197 void qemu_flush_queued_packets(NetClientState *nc); 198 void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge); 199 void qemu_set_info_str(NetClientState *nc, 200 const char *fmt, ...) G_GNUC_PRINTF(2, 3); 201 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]); 202 bool qemu_has_ufo(NetClientState *nc); 203 bool qemu_has_uso(NetClientState *nc); 204 bool qemu_has_tunnel(NetClientState *nc); 205 bool qemu_has_vnet_hdr(NetClientState *nc); 206 bool qemu_has_vnet_hdr_len(NetClientState *nc, int len); 207 void qemu_set_offload(NetClientState *nc, const NetOffloads *ol); 208 int qemu_get_vnet_hdr_len(NetClientState *nc); 209 void qemu_set_vnet_hdr_len(NetClientState *nc, int len); 210 bool qemu_get_vnet_hash_supported_types(NetClientState *nc, uint32_t *types); 211 int qemu_set_vnet_le(NetClientState *nc, bool is_le); 212 int qemu_set_vnet_be(NetClientState *nc, bool is_be); 213 void qemu_macaddr_default_if_unset(MACAddr *macaddr); 214 /** 215 * qemu_find_nic_info: Obtain NIC configuration information 216 * @typename: Name of device object type 217 * @match_default: Match NIC configurations with no model specified 218 * @alias: Additional model string to match (for user convenience and 219 * backward compatibility). 220 * 221 * Search for a NIC configuration matching the NIC model constraints. 222 */ 223 NICInfo *qemu_find_nic_info(const char *typename, bool match_default, 224 const char *alias); 225 /** 226 * qemu_configure_nic_device: Apply NIC configuration to a given device 227 * @dev: Network device to be configured 228 * @match_default: Match NIC configurations with no model specified 229 * @alias: Additional model string to match 230 * 231 * Search for a NIC configuration for the provided device, using the 232 * additionally specified matching constraints. If found, apply the 233 * configuration using qdev_set_nic_properties() and return %true. 234 * 235 * This is used by platform code which creates the device anyway, 236 * regardless of whether there is a configuration for it. This tends 237 * to be platforms which ignore `--nodefaults` and create net devices 238 * anyway, for example because the Ethernet device on that board is 239 * always physically present. 240 */ 241 bool qemu_configure_nic_device(DeviceState *dev, bool match_default, 242 const char *alias); 243 244 /** 245 * qemu_create_nic_device: Create a NIC device if a configuration exists for it 246 * @typename: Object typename of network device 247 * @match_default: Match NIC configurations with no model specified 248 * @alias: Additional model string to match 249 * 250 * Search for a NIC configuration for the provided device type. If found, 251 * create an object of the corresponding type and return it. 252 */ 253 DeviceState *qemu_create_nic_device(const char *typename, bool match_default, 254 const char *alias); 255 256 /* 257 * qemu_create_nic_bus_devices: Create configured NIC devices for a given bus 258 * @bus: Bus on which to create devices 259 * @parent_type: Object type for devices to be created (e.g. TYPE_PCI_DEVICE) 260 * @default_model: Object type name for default NIC model (or %NULL) 261 * @alias: Additional model string to replace, for user convenience 262 * @alias_target: Actual object type name to be used in place of @alias 263 * 264 * Instantiate dynamic NICs on a given bus, typically a PCI bus. This scans 265 * for available NIC configurations which either specify a model which is 266 * a child type of @parent_type, or which do not specify a model when 267 * @default_model is non-NULL. Each device is instantiated on the given @bus. 268 * 269 * A single substitution is supported, e.g. "xen" → "xen-net-device" for the 270 * Xen bus, or "virtio" → "virtio-net-pci" for PCI. This allows the user to 271 * specify a more understandable "model=" parameter on the command line, not 272 * only the real object typename. 273 */ 274 void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type, 275 const char *default_model, 276 const char *alias, const char *alias_target); 277 void print_net_client(Monitor *mon, NetClientState *nc); 278 void net_socket_rs_init(SocketReadState *rs, 279 SocketReadStateFinalize *finalize, 280 bool vnet_hdr); 281 NetClientState *qemu_get_peer(NetClientState *nc, int queue_index); 282 283 /** 284 * qemu_get_nic_models: 285 * @device_type: Defines which devices should be taken into consideration 286 * (e.g. TYPE_DEVICE for all devices, or TYPE_PCI_DEVICE for PCI) 287 * 288 * Get an array of pointers to names of NIC devices that are available in 289 * the QEMU binary. The array is terminated with a NULL pointer entry. 290 * The caller is responsible for freeing the memory when it is not required 291 * anymore, e.g. with g_ptr_array_free(..., true). 292 * 293 * Returns: Pointer to the array that contains the pointers to the names. 294 */ 295 GPtrArray *qemu_get_nic_models(const char *device_type); 296 297 /* NIC info */ 298 299 #define MAX_NICS 8 300 301 struct NICInfo { 302 MACAddr macaddr; 303 char *model; 304 char *name; 305 char *devaddr; 306 NetClientState *netdev; 307 int used; /* is this slot in nd_table[] being used? */ 308 int instantiated; /* does this NICInfo correspond to an instantiated NIC? */ 309 int nvectors; 310 }; 311 312 /* from net.c */ 313 extern NetClientStateList net_clients; 314 bool netdev_is_modern(const char *optstr); 315 void netdev_parse_modern(const char *optstr); 316 void net_client_parse(QemuOptsList *opts_list, const char *optstr); 317 void show_netdevs(void); 318 void net_init_clients(void); 319 void net_check_clients(void); 320 void net_client_set_link(NetClientState **ncs, int queues, bool up); 321 void net_cleanup(void); 322 void hmp_host_net_add(Monitor *mon, const QDict *qdict); 323 void hmp_host_net_remove(Monitor *mon, const QDict *qdict); 324 void netdev_add(QemuOpts *opts, Error **errp); 325 326 int net_hub_id_for_client(NetClientState *nc, int *id); 327 328 #define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup" 329 #define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown" 330 #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper" 331 #define DEFAULT_BRIDGE_INTERFACE "br0" 332 333 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd); 334 335 #define POLYNOMIAL_BE 0x04c11db6 336 #define POLYNOMIAL_LE 0xedb88320 337 uint32_t net_crc32(const uint8_t *p, int len); 338 uint32_t net_crc32_le(const uint8_t *p, int len); 339 340 #define vmstate_offset_macaddr(_state, _field) \ 341 vmstate_offset_array(_state, _field.a, uint8_t, \ 342 sizeof(typeof_field(_state, _field))) 343 344 #define VMSTATE_MACADDR(_field, _state) { \ 345 .name = (stringify(_field)), \ 346 .size = sizeof(MACAddr), \ 347 .info = &vmstate_info_buffer, \ 348 .flags = VMS_BUFFER, \ 349 .offset = vmstate_offset_macaddr(_state, _field), \ 350 } 351 352 static inline bool net_peer_needs_padding(NetClientState *nc) 353 { 354 return nc->peer && !nc->peer->do_not_pad; 355 } 356 357 #endif 358