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