]>
Commit | Line | Data |
---|---|---|
87ecb68b PB |
1 | #ifndef QEMU_NET_H |
2 | #define QEMU_NET_H | |
3 | ||
1de7afc9 | 4 | #include "qemu/queue.h" |
9af23989 | 5 | #include "qapi/qapi-types-net.h" |
e1144d00 | 6 | #include "net/queue.h" |
caf71f86 | 7 | #include "migration/vmstate.h" |
fbe78f4f | 8 | |
6d1d4939 DF |
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 | ||
1ceef9f2 JW |
14 | #define MAX_QUEUE_NUM 1024 |
15 | ||
d32fcad3 SF |
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 | ||
76d32cba GH |
21 | struct MACAddr { |
22 | uint8_t a[6]; | |
23 | }; | |
24 | ||
ed16ab5a GH |
25 | /* qdev nic properties */ |
26 | ||
1ceef9f2 JW |
27 | typedef struct NICPeers { |
28 | NetClientState *ncs[MAX_QUEUE_NUM]; | |
575a1c0e | 29 | int32_t queues; |
1ceef9f2 JW |
30 | } NICPeers; |
31 | ||
ed16ab5a GH |
32 | typedef struct NICConf { |
33 | MACAddr macaddr; | |
1ceef9f2 | 34 | NICPeers peers; |
1ca4d09a | 35 | int32_t bootindex; |
ed16ab5a GH |
36 | } NICConf; |
37 | ||
38 | #define DEFINE_NIC_PROPERTIES(_state, _conf) \ | |
39 | DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \ | |
45e8a9e1 | 40 | DEFINE_PROP_NETDEV("netdev", _state, _conf.peers) |
ed16ab5a | 41 | |
1ceef9f2 | 42 | |
4e68f7a0 | 43 | /* Net clients */ |
87ecb68b | 44 | |
4e68f7a0 SH |
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 *); | |
f7860455 | 51 | typedef void (NetClientDestructor)(NetClientState *); |
b1be4280 | 52 | typedef RxFilterInfo *(QueryRxFilter)(NetClientState *); |
1f55ac45 VM |
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); | |
c80cd6bb GK |
59 | typedef int (SetVnetLE)(NetClientState *, bool); |
60 | typedef int (SetVnetBE)(NetClientState *, bool); | |
16a3df40 ZC |
61 | typedef struct SocketReadState SocketReadState; |
62 | typedef void (SocketReadStateFinalize)(SocketReadState *rs); | |
44b416ad | 63 | typedef void (NetAnnounce)(NetClientState *); |
34b25ca7 | 64 | |
3ed79cc9 | 65 | typedef struct NetClientInfo { |
f394b2e2 | 66 | NetClientDriver type; |
3ed79cc9 MM |
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; | |
b1be4280 | 74 | QueryRxFilter *query_rx_filter; |
ceb69615 | 75 | NetPoll *poll; |
1f55ac45 VM |
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; | |
c80cd6bb GK |
82 | SetVnetLE *set_vnet_le; |
83 | SetVnetBE *set_vnet_be; | |
44b416ad | 84 | NetAnnounce *announce; |
3ed79cc9 MM |
85 | } NetClientInfo; |
86 | ||
4e68f7a0 | 87 | struct NetClientState { |
665a3b07 | 88 | NetClientInfo *info; |
436e5e53 | 89 | int link_down; |
4e68f7a0 SH |
90 | QTAILQ_ENTRY(NetClientState) next; |
91 | NetClientState *peer; | |
067404be | 92 | NetQueue *incoming_queue; |
bf38c1a0 | 93 | char *model; |
676cff29 | 94 | char *name; |
87ecb68b | 95 | char info_str[256]; |
893379ef | 96 | unsigned receive_disabled : 1; |
f7860455 | 97 | NetClientDestructor *destructor; |
1ceef9f2 | 98 | unsigned int queue_index; |
b1be4280 | 99 | unsigned rxfilter_notify_enabled:1; |
bfc6cf31 | 100 | int vring_enable; |
d6b732e9 | 101 | int vnet_hdr_len; |
eae3eb3e | 102 | QTAILQ_HEAD(, NetFilterState) filters; |
87ecb68b PB |
103 | }; |
104 | ||
ebef2c09 | 105 | typedef struct NICState { |
f6b26cf2 | 106 | NetClientState *ncs; |
ebef2c09 MM |
107 | NICConf *conf; |
108 | void *opaque; | |
a083a89d | 109 | bool peer_deleted; |
ebef2c09 MM |
110 | } NICState; |
111 | ||
16a3df40 | 112 | struct SocketReadState { |
3cde5ea2 ZC |
113 | /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */ |
114 | int state; | |
115 | /* This flag decide whether to read the vnet_hdr_len field */ | |
116 | bool vnet_hdr; | |
16a3df40 ZC |
117 | uint32_t index; |
118 | uint32_t packet_len; | |
3cde5ea2 | 119 | uint32_t vnet_hdr_len; |
16a3df40 ZC |
120 | uint8_t buf[NET_BUFSIZE]; |
121 | SocketReadStateFinalize *finalize; | |
122 | }; | |
123 | ||
124 | int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size); | |
890ee6ab | 125 | char *qemu_mac_strdup_printf(const uint8_t *macaddr); |
4e68f7a0 | 126 | NetClientState *qemu_find_netdev(const char *id); |
6c51ae73 | 127 | int qemu_find_net_clients_except(const char *id, NetClientState **ncs, |
f394b2e2 | 128 | NetClientDriver type, int max); |
4e68f7a0 SH |
129 | NetClientState *qemu_new_net_client(NetClientInfo *info, |
130 | NetClientState *peer, | |
131 | const char *model, | |
132 | const char *name); | |
ebef2c09 MM |
133 | NICState *qemu_new_nic(NetClientInfo *info, |
134 | NICConf *conf, | |
135 | const char *model, | |
136 | const char *name, | |
137 | void *opaque); | |
948ecf21 | 138 | void qemu_del_nic(NICState *nic); |
1ceef9f2 | 139 | NetClientState *qemu_get_subqueue(NICState *nic, int queue_index); |
b356f76d | 140 | NetClientState *qemu_get_queue(NICState *nic); |
cc1f0f45 JW |
141 | NICState *qemu_get_nic(NetClientState *nc); |
142 | void *qemu_get_nic_opaque(NetClientState *nc); | |
b20c6b9e | 143 | void qemu_del_net_client(NetClientState *nc); |
57f9ef17 MM |
144 | typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque); |
145 | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque); | |
35277d14 SH |
146 | int qemu_can_send_packet(NetClientState *nc); |
147 | ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, | |
fbe78f4f | 148 | int iovcnt); |
35277d14 | 149 | ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov, |
f3b6c7fc | 150 | int iovcnt, NetPacketSent *sent_cb); |
625a526b | 151 | ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size); |
35277d14 SH |
152 | ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size); |
153 | ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf, | |
f3b6c7fc | 154 | int size, NetPacketSent *sent_cb); |
35277d14 SH |
155 | void qemu_purge_queued_packets(NetClientState *nc); |
156 | void qemu_flush_queued_packets(NetClientState *nc); | |
94b52958 | 157 | void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge); |
35277d14 | 158 | void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]); |
d6085e3a SH |
159 | bool qemu_has_ufo(NetClientState *nc); |
160 | bool qemu_has_vnet_hdr(NetClientState *nc); | |
161 | bool qemu_has_vnet_hdr_len(NetClientState *nc, int len); | |
162 | void qemu_using_vnet_hdr(NetClientState *nc, bool enable); | |
163 | void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6, | |
164 | int ecn, int ufo); | |
165 | void qemu_set_vnet_hdr_len(NetClientState *nc, int len); | |
c80cd6bb GK |
166 | int qemu_set_vnet_le(NetClientState *nc, bool is_le); |
167 | int qemu_set_vnet_be(NetClientState *nc, bool is_be); | |
76d32cba | 168 | void qemu_macaddr_default_if_unset(MACAddr *macaddr); |
07caea31 | 169 | int qemu_show_nic_models(const char *arg, const char *const *models); |
d07f22c5 | 170 | void qemu_check_nic_model(NICInfo *nd, const char *model); |
07caea31 MA |
171 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
172 | const char *default_model); | |
87ecb68b | 173 | |
1a859593 | 174 | void print_net_client(Monitor *mon, NetClientState *nc); |
1ce6be24 | 175 | void hmp_info_network(Monitor *mon, const QDict *qdict); |
16a3df40 | 176 | void net_socket_rs_init(SocketReadState *rs, |
3cde5ea2 ZC |
177 | SocketReadStateFinalize *finalize, |
178 | bool vnet_hdr); | |
87ecb68b PB |
179 | |
180 | /* NIC info */ | |
181 | ||
182 | #define MAX_NICS 8 | |
183 | ||
184 | struct NICInfo { | |
6eed1856 | 185 | MACAddr macaddr; |
9203f520 MM |
186 | char *model; |
187 | char *name; | |
188 | char *devaddr; | |
4e68f7a0 | 189 | NetClientState *netdev; |
48e2faf2 PM |
190 | int used; /* is this slot in nd_table[] being used? */ |
191 | int instantiated; /* does this NICInfo correspond to an instantiated NIC? */ | |
ffe6370c | 192 | int nvectors; |
87ecb68b PB |
193 | }; |
194 | ||
195 | extern int nb_nics; | |
196 | extern NICInfo nd_table[MAX_NICS]; | |
84007e81 | 197 | extern const char *host_net_devices[]; |
87ecb68b | 198 | |
63a01ef8 | 199 | /* from net.c */ |
7f161aae | 200 | int net_client_parse(QemuOptsList *opts_list, const char *str); |
34f708b0 | 201 | int net_init_clients(Error **errp); |
668680f7 | 202 | void net_check_clients(void); |
63a01ef8 | 203 | void net_cleanup(void); |
3e5a50d6 MA |
204 | void hmp_host_net_add(Monitor *mon, const QDict *qdict); |
205 | void hmp_host_net_remove(Monitor *mon, const QDict *qdict); | |
928059a3 | 206 | void netdev_add(QemuOpts *opts, Error **errp); |
485febc6 | 207 | void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp); |
63a01ef8 | 208 | |
1422e32d PB |
209 | int net_hub_id_for_client(NetClientState *nc, int *id); |
210 | NetClientState *net_hub_port_find(int hub_id); | |
211 | ||
f54825cc AJ |
212 | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
213 | #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" | |
a7c36ee4 CB |
214 | #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper" |
215 | #define DEFAULT_BRIDGE_INTERFACE "br0" | |
f54825cc | 216 | |
ed16ab5a | 217 | void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd); |
9d07d757 | 218 | |
eaba8f34 | 219 | #define POLYNOMIAL_BE 0x04c11db6 |
f1a7deb9 | 220 | #define POLYNOMIAL_LE 0xedb88320 |
eaba8f34 | 221 | uint32_t net_crc32(const uint8_t *p, int len); |
f1a7deb9 | 222 | uint32_t net_crc32_le(const uint8_t *p, int len); |
7fc8d918 | 223 | |
701a8f76 PB |
224 | #define vmstate_offset_macaddr(_state, _field) \ |
225 | vmstate_offset_array(_state, _field.a, uint8_t, \ | |
226 | sizeof(typeof_field(_state, _field))) | |
227 | ||
228 | #define VMSTATE_MACADDR(_field, _state) { \ | |
229 | .name = (stringify(_field)), \ | |
230 | .size = sizeof(MACAddr), \ | |
231 | .info = &vmstate_info_buffer, \ | |
232 | .flags = VMS_BUFFER, \ | |
233 | .offset = vmstate_offset_macaddr(_state, _field), \ | |
234 | } | |
235 | ||
87ecb68b | 236 | #endif |