net.h (9284B)
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 21struct MACAddr { 22 uint8_t a[6]; 23}; 24 25/* qdev nic properties */ 26 27typedef struct NICPeers { 28 NetClientState *ncs[MAX_QUEUE_NUM]; 29 int32_t queues; 30} NICPeers; 31 32typedef 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 45typedef void (NetPoll)(NetClientState *, bool enable); 46typedef bool (NetCanReceive)(NetClientState *); 47typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t); 48typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int); 49typedef void (NetCleanup) (NetClientState *); 50typedef void (LinkStatusChanged)(NetClientState *); 51typedef void (NetClientDestructor)(NetClientState *); 52typedef RxFilterInfo *(QueryRxFilter)(NetClientState *); 53typedef bool (HasUfo)(NetClientState *); 54typedef bool (HasVnetHdr)(NetClientState *); 55typedef bool (HasVnetHdrLen)(NetClientState *, int); 56typedef void (UsingVnetHdr)(NetClientState *, bool); 57typedef void (SetOffload)(NetClientState *, int, int, int, int, int); 58typedef void (SetVnetHdrLen)(NetClientState *, int); 59typedef int (SetVnetLE)(NetClientState *, bool); 60typedef int (SetVnetBE)(NetClientState *, bool); 61typedef struct SocketReadState SocketReadState; 62typedef void (SocketReadStateFinalize)(SocketReadState *rs); 63typedef void (NetAnnounce)(NetClientState *); 64typedef bool (SetSteeringEBPF)(NetClientState *, int); 65 66typedef struct NetClientInfo { 67 NetClientDriver type; 68 size_t size; 69 NetReceive *receive; 70 NetReceive *receive_raw; 71 NetReceiveIOV *receive_iov; 72 NetCanReceive *can_receive; 73 NetCleanup *cleanup; 74 LinkStatusChanged *link_status_changed; 75 QueryRxFilter *query_rx_filter; 76 NetPoll *poll; 77 HasUfo *has_ufo; 78 HasVnetHdr *has_vnet_hdr; 79 HasVnetHdrLen *has_vnet_hdr_len; 80 UsingVnetHdr *using_vnet_hdr; 81 SetOffload *set_offload; 82 SetVnetHdrLen *set_vnet_hdr_len; 83 SetVnetLE *set_vnet_le; 84 SetVnetBE *set_vnet_be; 85 NetAnnounce *announce; 86 SetSteeringEBPF *set_steering_ebpf; 87} NetClientInfo; 88 89struct NetClientState { 90 NetClientInfo *info; 91 int link_down; 92 QTAILQ_ENTRY(NetClientState) next; 93 NetClientState *peer; 94 NetQueue *incoming_queue; 95 char *model; 96 char *name; 97 char info_str[256]; 98 unsigned receive_disabled : 1; 99 NetClientDestructor *destructor; 100 unsigned int queue_index; 101 unsigned rxfilter_notify_enabled:1; 102 int vring_enable; 103 int vnet_hdr_len; 104 bool is_netdev; 105 bool do_not_pad; /* do not pad to the minimum ethernet frame length */ 106 QTAILQ_HEAD(, NetFilterState) filters; 107}; 108 109typedef struct NICState { 110 NetClientState *ncs; 111 NICConf *conf; 112 void *opaque; 113 bool peer_deleted; 114} NICState; 115 116struct SocketReadState { 117 /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */ 118 int state; 119 /* This flag decide whether to read the vnet_hdr_len field */ 120 bool vnet_hdr; 121 uint32_t index; 122 uint32_t packet_len; 123 uint32_t vnet_hdr_len; 124 uint8_t buf[NET_BUFSIZE]; 125 SocketReadStateFinalize *finalize; 126}; 127 128int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size); 129char *qemu_mac_strdup_printf(const uint8_t *macaddr); 130NetClientState *qemu_find_netdev(const char *id); 131int qemu_find_net_clients_except(const char *id, NetClientState **ncs, 132 NetClientDriver type, int max); 133NetClientState *qemu_new_net_client(NetClientInfo *info, 134 NetClientState *peer, 135 const char *model, 136 const char *name); 137NICState *qemu_new_nic(NetClientInfo *info, 138 NICConf *conf, 139 const char *model, 140 const char *name, 141 void *opaque); 142void qemu_del_nic(NICState *nic); 143NetClientState *qemu_get_subqueue(NICState *nic, int queue_index); 144NetClientState *qemu_get_queue(NICState *nic); 145NICState *qemu_get_nic(NetClientState *nc); 146void *qemu_get_nic_opaque(NetClientState *nc); 147void qemu_del_net_client(NetClientState *nc); 148typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque); 149void qemu_foreach_nic(qemu_nic_foreach func, void *opaque); 150int qemu_can_receive_packet(NetClientState *nc); 151int qemu_can_send_packet(NetClientState *nc); 152ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, 153 int iovcnt); 154ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov, 155 int iovcnt, NetPacketSent *sent_cb); 156ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size); 157ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size); 158ssize_t qemu_receive_packet_iov(NetClientState *nc, 159 const struct iovec *iov, 160 int iovcnt); 161ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size); 162ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf, 163 int size, NetPacketSent *sent_cb); 164void qemu_purge_queued_packets(NetClientState *nc); 165void qemu_flush_queued_packets(NetClientState *nc); 166void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge); 167void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]); 168bool qemu_has_ufo(NetClientState *nc); 169bool qemu_has_vnet_hdr(NetClientState *nc); 170bool qemu_has_vnet_hdr_len(NetClientState *nc, int len); 171void qemu_using_vnet_hdr(NetClientState *nc, bool enable); 172void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6, 173 int ecn, int ufo); 174void qemu_set_vnet_hdr_len(NetClientState *nc, int len); 175int qemu_set_vnet_le(NetClientState *nc, bool is_le); 176int qemu_set_vnet_be(NetClientState *nc, bool is_be); 177void qemu_macaddr_default_if_unset(MACAddr *macaddr); 178int qemu_show_nic_models(const char *arg, const char *const *models); 179void qemu_check_nic_model(NICInfo *nd, const char *model); 180int qemu_find_nic_model(NICInfo *nd, const char * const *models, 181 const char *default_model); 182 183void print_net_client(Monitor *mon, NetClientState *nc); 184void hmp_info_network(Monitor *mon, const QDict *qdict); 185void net_socket_rs_init(SocketReadState *rs, 186 SocketReadStateFinalize *finalize, 187 bool vnet_hdr); 188NetClientState *qemu_get_peer(NetClientState *nc, int queue_index); 189 190/* NIC info */ 191 192#define MAX_NICS 8 193 194struct NICInfo { 195 MACAddr macaddr; 196 char *model; 197 char *name; 198 char *devaddr; 199 NetClientState *netdev; 200 int used; /* is this slot in nd_table[] being used? */ 201 int instantiated; /* does this NICInfo correspond to an instantiated NIC? */ 202 int nvectors; 203}; 204 205extern int nb_nics; 206extern NICInfo nd_table[MAX_NICS]; 207extern const char *host_net_devices[]; 208 209/* from net.c */ 210int net_client_parse(QemuOptsList *opts_list, const char *str); 211void show_netdevs(void); 212int net_init_clients(Error **errp); 213void net_check_clients(void); 214void net_cleanup(void); 215void hmp_host_net_add(Monitor *mon, const QDict *qdict); 216void hmp_host_net_remove(Monitor *mon, const QDict *qdict); 217void netdev_add(QemuOpts *opts, Error **errp); 218 219int net_hub_id_for_client(NetClientState *nc, int *id); 220NetClientState *net_hub_port_find(int hub_id); 221 222#define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup" 223#define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown" 224#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper" 225#define DEFAULT_BRIDGE_INTERFACE "br0" 226 227void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd); 228 229#define POLYNOMIAL_BE 0x04c11db6 230#define POLYNOMIAL_LE 0xedb88320 231uint32_t net_crc32(const uint8_t *p, int len); 232uint32_t net_crc32_le(const uint8_t *p, int len); 233 234#define vmstate_offset_macaddr(_state, _field) \ 235 vmstate_offset_array(_state, _field.a, uint8_t, \ 236 sizeof(typeof_field(_state, _field))) 237 238#define VMSTATE_MACADDR(_field, _state) { \ 239 .name = (stringify(_field)), \ 240 .size = sizeof(MACAddr), \ 241 .info = &vmstate_info_buffer, \ 242 .flags = VMS_BUFFER, \ 243 .offset = vmstate_offset_macaddr(_state, _field), \ 244} 245 246static inline bool net_peer_needs_padding(NetClientState *nc) 247{ 248 return nc->peer && !nc->peer->do_not_pad; 249} 250 251#endif