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