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