]>
Commit | Line | Data |
---|---|---|
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 int (NetStart)(NetClientState *); | |
48 | typedef int (NetLoad)(NetClientState *); | |
49 | typedef void (NetStop)(NetClientState *); | |
50 | typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t); | |
51 | typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int); | |
52 | typedef void (NetCleanup) (NetClientState *); | |
53 | typedef void (LinkStatusChanged)(NetClientState *); | |
54 | typedef void (NetClientDestructor)(NetClientState *); | |
55 | typedef RxFilterInfo *(QueryRxFilter)(NetClientState *); | |
56 | typedef bool (HasUfo)(NetClientState *); | |
57 | typedef bool (HasUso)(NetClientState *); | |
58 | typedef bool (HasVnetHdr)(NetClientState *); | |
59 | typedef bool (HasVnetHdrLen)(NetClientState *, int); | |
60 | typedef bool (GetUsingVnetHdr)(NetClientState *); | |
61 | typedef void (UsingVnetHdr)(NetClientState *, bool); | |
62 | typedef void (SetOffload)(NetClientState *, int, int, int, int, int, int, int); | |
63 | typedef int (GetVnetHdrLen)(NetClientState *); | |
64 | typedef void (SetVnetHdrLen)(NetClientState *, int); | |
65 | typedef int (SetVnetLE)(NetClientState *, bool); | |
66 | typedef int (SetVnetBE)(NetClientState *, bool); | |
67 | typedef struct SocketReadState SocketReadState; | |
68 | typedef void (SocketReadStateFinalize)(SocketReadState *rs); | |
69 | typedef void (NetAnnounce)(NetClientState *); | |
70 | typedef bool (SetSteeringEBPF)(NetClientState *, int); | |
71 | typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **); | |
72 | ||
73 | typedef struct NetClientInfo { | |
74 | NetClientDriver type; | |
75 | size_t size; | |
76 | NetReceive *receive; | |
77 | NetReceive *receive_raw; | |
78 | NetReceiveIOV *receive_iov; | |
79 | NetCanReceive *can_receive; | |
80 | NetStart *start; | |
81 | NetLoad *load; | |
82 | NetStop *stop; | |
83 | NetCleanup *cleanup; | |
84 | LinkStatusChanged *link_status_changed; | |
85 | QueryRxFilter *query_rx_filter; | |
86 | NetPoll *poll; | |
87 | HasUfo *has_ufo; | |
88 | HasUso *has_uso; | |
89 | HasVnetHdr *has_vnet_hdr; | |
90 | HasVnetHdrLen *has_vnet_hdr_len; | |
91 | GetUsingVnetHdr *get_using_vnet_hdr; | |
92 | UsingVnetHdr *using_vnet_hdr; | |
93 | SetOffload *set_offload; | |
94 | GetVnetHdrLen *get_vnet_hdr_len; | |
95 | SetVnetHdrLen *set_vnet_hdr_len; | |
96 | SetVnetLE *set_vnet_le; | |
97 | SetVnetBE *set_vnet_be; | |
98 | NetAnnounce *announce; | |
99 | SetSteeringEBPF *set_steering_ebpf; | |
100 | NetCheckPeerType *check_peer_type; | |
101 | } NetClientInfo; | |
102 | ||
103 | struct NetClientState { | |
104 | NetClientInfo *info; | |
105 | int link_down; | |
106 | QTAILQ_ENTRY(NetClientState) next; | |
107 | NetClientState *peer; | |
108 | NetQueue *incoming_queue; | |
109 | char *model; | |
110 | char *name; | |
111 | char info_str[256]; | |
112 | unsigned receive_disabled : 1; | |
113 | NetClientDestructor *destructor; | |
114 | unsigned int queue_index; | |
115 | unsigned rxfilter_notify_enabled:1; | |
116 | int vring_enable; | |
117 | int vnet_hdr_len; | |
118 | bool is_netdev; | |
119 | bool do_not_pad; /* do not pad to the minimum ethernet frame length */ | |
120 | bool is_datapath; | |
121 | QTAILQ_HEAD(, NetFilterState) filters; | |
122 | }; | |
123 | ||
124 | typedef QTAILQ_HEAD(NetClientStateList, NetClientState) NetClientStateList; | |
125 | ||
126 | typedef struct NICState { | |
127 | NetClientState *ncs; | |
128 | NICConf *conf; | |
129 | MemReentrancyGuard *reentrancy_guard; | |
130 | void *opaque; | |
131 | bool peer_deleted; | |
132 | } NICState; | |
133 | ||
134 | struct SocketReadState { | |
135 | /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */ | |
136 | int state; | |
137 | /* This flag decide whether to read the vnet_hdr_len field */ | |
138 | bool vnet_hdr; | |
139 | uint32_t index; | |
140 | uint32_t packet_len; | |
141 | uint32_t vnet_hdr_len; | |
142 | uint8_t buf[NET_BUFSIZE]; | |
143 | SocketReadStateFinalize *finalize; | |
144 | }; | |
145 | ||
146 | int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size); | |
147 | char *qemu_mac_strdup_printf(const uint8_t *macaddr); | |
148 | NetClientState *qemu_find_netdev(const char *id); | |
149 | int qemu_find_net_clients_except(const char *id, NetClientState **ncs, | |
150 | NetClientDriver type, int max); | |
151 | NetClientState *qemu_new_net_client(NetClientInfo *info, | |
152 | NetClientState *peer, | |
153 | const char *model, | |
154 | const char *name); | |
155 | NetClientState *qemu_new_net_control_client(NetClientInfo *info, | |
156 | NetClientState *peer, | |
157 | const char *model, | |
158 | const char *name); | |
159 | NICState *qemu_new_nic(NetClientInfo *info, | |
160 | NICConf *conf, | |
161 | const char *model, | |
162 | const char *name, | |
163 | MemReentrancyGuard *reentrancy_guard, | |
164 | void *opaque); | |
165 | void qemu_del_nic(NICState *nic); | |
166 | NetClientState *qemu_get_subqueue(NICState *nic, int queue_index); | |
167 | NetClientState *qemu_get_queue(NICState *nic); | |
168 | NICState *qemu_get_nic(NetClientState *nc); | |
169 | void *qemu_get_nic_opaque(NetClientState *nc); | |
170 | void qemu_del_net_client(NetClientState *nc); | |
171 | typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque); | |
172 | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque); | |
173 | int qemu_can_receive_packet(NetClientState *nc); | |
174 | int qemu_can_send_packet(NetClientState *nc); | |
175 | ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, | |
176 | int iovcnt); | |
177 | ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov, | |
178 | int iovcnt, NetPacketSent *sent_cb); | |
179 | ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size); | |
180 | ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size); | |
181 | ssize_t qemu_receive_packet_iov(NetClientState *nc, | |
182 | const struct iovec *iov, | |
183 | int iovcnt); | |
184 | ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size); | |
185 | ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf, | |
186 | int size, NetPacketSent *sent_cb); | |
187 | void qemu_purge_queued_packets(NetClientState *nc); | |
188 | void qemu_flush_queued_packets(NetClientState *nc); | |
189 | void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge); | |
190 | void qemu_set_info_str(NetClientState *nc, | |
191 | const char *fmt, ...) G_GNUC_PRINTF(2, 3); | |
192 | void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]); | |
193 | bool qemu_has_ufo(NetClientState *nc); | |
194 | bool qemu_has_uso(NetClientState *nc); | |
195 | bool qemu_has_vnet_hdr(NetClientState *nc); | |
196 | bool qemu_has_vnet_hdr_len(NetClientState *nc, int len); | |
197 | bool qemu_get_using_vnet_hdr(NetClientState *nc); | |
198 | void qemu_using_vnet_hdr(NetClientState *nc, bool enable); | |
199 | void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6, | |
200 | int ecn, int ufo, int uso4, int uso6); | |
201 | int qemu_get_vnet_hdr_len(NetClientState *nc); | |
202 | void qemu_set_vnet_hdr_len(NetClientState *nc, int len); | |
203 | int qemu_set_vnet_le(NetClientState *nc, bool is_le); | |
204 | int qemu_set_vnet_be(NetClientState *nc, bool is_be); | |
205 | void qemu_macaddr_default_if_unset(MACAddr *macaddr); | |
206 | /** | |
207 | * qemu_find_nic_info: Obtain NIC configuration information | |
208 | * @typename: Name of device object type | |
209 | * @match_default: Match NIC configurations with no model specified | |
210 | * @alias: Additional model string to match (for user convenience and | |
211 | * backward compatibility). | |
212 | * | |
213 | * Search for a NIC configuration matching the NIC model constraints. | |
214 | */ | |
215 | NICInfo *qemu_find_nic_info(const char *typename, bool match_default, | |
216 | const char *alias); | |
217 | /** | |
218 | * qemu_configure_nic_device: Apply NIC configuration to a given device | |
219 | * @dev: Network device to be configured | |
220 | * @match_default: Match NIC configurations with no model specified | |
221 | * @alias: Additional model string to match | |
222 | * | |
223 | * Search for a NIC configuration for the provided device, using the | |
224 | * additionally specified matching constraints. If found, apply the | |
225 | * configuration using qdev_set_nic_properties() and return %true. | |
226 | * | |
227 | * This is used by platform code which creates the device anyway, | |
228 | * regardless of whether there is a configuration for it. This tends | |
229 | * to be platforms which ignore `--nodefaults` and create net devices | |
230 | * anyway, for example because the Ethernet device on that board is | |
231 | * always physically present. | |
232 | */ | |
233 | bool qemu_configure_nic_device(DeviceState *dev, bool match_default, | |
234 | const char *alias); | |
235 | ||
236 | /** | |
237 | * qemu_create_nic_device: Create a NIC device if a configuration exists for it | |
238 | * @typename: Object typename of network device | |
239 | * @match_default: Match NIC configurations with no model specified | |
240 | * @alias: Additional model string to match | |
241 | * | |
242 | * Search for a NIC configuration for the provided device type. If found, | |
243 | * create an object of the corresponding type and return it. | |
244 | */ | |
245 | DeviceState *qemu_create_nic_device(const char *typename, bool match_default, | |
246 | const char *alias); | |
247 | ||
248 | /* | |
249 | * qemu_create_nic_bus_devices: Create configured NIC devices for a given bus | |
250 | * @bus: Bus on which to create devices | |
251 | * @parent_type: Object type for devices to be created (e.g. TYPE_PCI_DEVICE) | |
252 | * @default_model: Object type name for default NIC model (or %NULL) | |
253 | * @alias: Additional model string to replace, for user convenience | |
254 | * @alias_target: Actual object type name to be used in place of @alias | |
255 | * | |
256 | * Instantiate dynamic NICs on a given bus, typically a PCI bus. This scans | |
257 | * for available NIC configurations which either specify a model which is | |
258 | * a child type of @parent_type, or which do not specify a model when | |
259 | * @default_model is non-NULL. Each device is instantiated on the given @bus. | |
260 | * | |
261 | * A single substitution is supported, e.g. "xen" → "xen-net-device" for the | |
262 | * Xen bus, or "virtio" → "virtio-net-pci" for PCI. This allows the user to | |
263 | * specify a more understandable "model=" parameter on the command line, not | |
264 | * only the real object typename. | |
265 | */ | |
266 | void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type, | |
267 | const char *default_model, | |
268 | const char *alias, const char *alias_target); | |
269 | void print_net_client(Monitor *mon, NetClientState *nc); | |
270 | void net_socket_rs_init(SocketReadState *rs, | |
271 | SocketReadStateFinalize *finalize, | |
272 | bool vnet_hdr); | |
273 | NetClientState *qemu_get_peer(NetClientState *nc, int queue_index); | |
274 | ||
275 | /** | |
276 | * qemu_get_nic_models: | |
277 | * @device_type: Defines which devices should be taken into consideration | |
278 | * (e.g. TYPE_DEVICE for all devices, or TYPE_PCI_DEVICE for PCI) | |
279 | * | |
280 | * Get an array of pointers to names of NIC devices that are available in | |
281 | * the QEMU binary. The array is terminated with a NULL pointer entry. | |
282 | * The caller is responsible for freeing the memory when it is not required | |
283 | * anymore, e.g. with g_ptr_array_free(..., true). | |
284 | * | |
285 | * Returns: Pointer to the array that contains the pointers to the names. | |
286 | */ | |
287 | GPtrArray *qemu_get_nic_models(const char *device_type); | |
288 | ||
289 | /* NIC info */ | |
290 | ||
291 | #define MAX_NICS 8 | |
292 | ||
293 | struct NICInfo { | |
294 | MACAddr macaddr; | |
295 | char *model; | |
296 | char *name; | |
297 | char *devaddr; | |
298 | NetClientState *netdev; | |
299 | int used; /* is this slot in nd_table[] being used? */ | |
300 | int instantiated; /* does this NICInfo correspond to an instantiated NIC? */ | |
301 | int nvectors; | |
302 | }; | |
303 | ||
304 | /* from net.c */ | |
305 | extern NetClientStateList net_clients; | |
306 | bool netdev_is_modern(const char *optstr); | |
307 | void netdev_parse_modern(const char *optstr); | |
308 | void net_client_parse(QemuOptsList *opts_list, const char *optstr); | |
309 | void show_netdevs(void); | |
310 | void net_init_clients(void); | |
311 | void net_check_clients(void); | |
312 | void net_cleanup(void); | |
313 | void hmp_host_net_add(Monitor *mon, const QDict *qdict); | |
314 | void hmp_host_net_remove(Monitor *mon, const QDict *qdict); | |
315 | void netdev_add(QemuOpts *opts, Error **errp); | |
316 | ||
317 | int net_hub_id_for_client(NetClientState *nc, int *id); | |
318 | NetClientState *net_hub_port_find(int hub_id); | |
319 | ||
320 | #define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup" | |
321 | #define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown" | |
322 | #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper" | |
323 | #define DEFAULT_BRIDGE_INTERFACE "br0" | |
324 | ||
325 | void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd); | |
326 | ||
327 | #define POLYNOMIAL_BE 0x04c11db6 | |
328 | #define POLYNOMIAL_LE 0xedb88320 | |
329 | uint32_t net_crc32(const uint8_t *p, int len); | |
330 | uint32_t net_crc32_le(const uint8_t *p, int len); | |
331 | ||
332 | #define vmstate_offset_macaddr(_state, _field) \ | |
333 | vmstate_offset_array(_state, _field.a, uint8_t, \ | |
334 | sizeof(typeof_field(_state, _field))) | |
335 | ||
336 | #define VMSTATE_MACADDR(_field, _state) { \ | |
337 | .name = (stringify(_field)), \ | |
338 | .size = sizeof(MACAddr), \ | |
339 | .info = &vmstate_info_buffer, \ | |
340 | .flags = VMS_BUFFER, \ | |
341 | .offset = vmstate_offset_macaddr(_state, _field), \ | |
342 | } | |
343 | ||
344 | static inline bool net_peer_needs_padding(NetClientState *nc) | |
345 | { | |
346 | return nc->peer && !nc->peer->do_not_pad; | |
347 | } | |
348 | ||
349 | #endif |