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