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