]> git.proxmox.com Git - mirror_qemu.git/blame - include/net/net.h
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / include / net / net.h
CommitLineData
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"
ce35e229 7#include "hw/qdev-properties-system.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
21struct MACAddr {
22 uint8_t a[6];
23};
24
ed16ab5a
GH
25/* qdev nic properties */
26
1ceef9f2
JW
27typedef struct NICPeers {
28 NetClientState *ncs[MAX_QUEUE_NUM];
575a1c0e 29 int32_t queues;
1ceef9f2
JW
30} NICPeers;
31
ed16ab5a
GH
32typedef 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 45typedef void (NetPoll)(NetClientState *, bool enable);
b8c4b67e 46typedef bool (NetCanReceive)(NetClientState *);
eb92b753 47typedef int (NetStart)(NetClientState *);
539573c3 48typedef int (NetLoad)(NetClientState *);
c5e5269d 49typedef void (NetStop)(NetClientState *);
4e68f7a0
SH
50typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
51typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
52typedef void (NetCleanup) (NetClientState *);
53typedef void (LinkStatusChanged)(NetClientState *);
f7860455 54typedef void (NetClientDestructor)(NetClientState *);
b1be4280 55typedef RxFilterInfo *(QueryRxFilter)(NetClientState *);
1f55ac45 56typedef bool (HasUfo)(NetClientState *);
f03e0cf6 57typedef bool (HasUso)(NetClientState *);
1f55ac45
VM
58typedef bool (HasVnetHdr)(NetClientState *);
59typedef bool (HasVnetHdrLen)(NetClientState *, int);
481c5232 60typedef bool (GetUsingVnetHdr)(NetClientState *);
1f55ac45 61typedef void (UsingVnetHdr)(NetClientState *, bool);
2ab0ec31 62typedef void (SetOffload)(NetClientState *, int, int, int, int, int, int, int);
481c5232 63typedef int (GetVnetHdrLen)(NetClientState *);
1f55ac45 64typedef void (SetVnetHdrLen)(NetClientState *, int);
c80cd6bb
GK
65typedef int (SetVnetLE)(NetClientState *, bool);
66typedef int (SetVnetBE)(NetClientState *, bool);
16a3df40
ZC
67typedef struct SocketReadState SocketReadState;
68typedef void (SocketReadStateFinalize)(SocketReadState *rs);
44b416ad 69typedef void (NetAnnounce)(NetClientState *);
8f364e34 70typedef bool (SetSteeringEBPF)(NetClientState *, int);
e287bf7b 71typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **);
34b25ca7 72
3ed79cc9 73typedef struct NetClientInfo {
f394b2e2 74 NetClientDriver type;
3ed79cc9
MM
75 size_t size;
76 NetReceive *receive;
77 NetReceive *receive_raw;
78 NetReceiveIOV *receive_iov;
79 NetCanReceive *can_receive;
eb92b753 80 NetStart *start;
539573c3 81 NetLoad *load;
c5e5269d 82 NetStop *stop;
3ed79cc9
MM
83 NetCleanup *cleanup;
84 LinkStatusChanged *link_status_changed;
b1be4280 85 QueryRxFilter *query_rx_filter;
ceb69615 86 NetPoll *poll;
1f55ac45 87 HasUfo *has_ufo;
f03e0cf6 88 HasUso *has_uso;
1f55ac45
VM
89 HasVnetHdr *has_vnet_hdr;
90 HasVnetHdrLen *has_vnet_hdr_len;
481c5232 91 GetUsingVnetHdr *get_using_vnet_hdr;
1f55ac45
VM
92 UsingVnetHdr *using_vnet_hdr;
93 SetOffload *set_offload;
481c5232 94 GetVnetHdrLen *get_vnet_hdr_len;
1f55ac45 95 SetVnetHdrLen *set_vnet_hdr_len;
c80cd6bb
GK
96 SetVnetLE *set_vnet_le;
97 SetVnetBE *set_vnet_be;
44b416ad 98 NetAnnounce *announce;
8f364e34 99 SetSteeringEBPF *set_steering_ebpf;
e287bf7b 100 NetCheckPeerType *check_peer_type;
3ed79cc9
MM
101} NetClientInfo;
102
4e68f7a0 103struct NetClientState {
665a3b07 104 NetClientInfo *info;
436e5e53 105 int link_down;
4e68f7a0
SH
106 QTAILQ_ENTRY(NetClientState) next;
107 NetClientState *peer;
067404be 108 NetQueue *incoming_queue;
bf38c1a0 109 char *model;
676cff29 110 char *name;
56e6f594 111 char info_str[256];
893379ef 112 unsigned receive_disabled : 1;
f7860455 113 NetClientDestructor *destructor;
1ceef9f2 114 unsigned int queue_index;
b1be4280 115 unsigned rxfilter_notify_enabled:1;
bfc6cf31 116 int vring_enable;
d6b732e9 117 int vnet_hdr_len;
08712fcb 118 bool is_netdev;
935344be 119 bool do_not_pad; /* do not pad to the minimum ethernet frame length */
2f849dbd 120 bool is_datapath;
eae3eb3e 121 QTAILQ_HEAD(, NetFilterState) filters;
87ecb68b
PB
122};
123
ae71d13d
MA
124typedef QTAILQ_HEAD(NetClientStateList, NetClientState) NetClientStateList;
125
ebef2c09 126typedef struct NICState {
f6b26cf2 127 NetClientState *ncs;
ebef2c09 128 NICConf *conf;
9050f976 129 MemReentrancyGuard *reentrancy_guard;
ebef2c09 130 void *opaque;
a083a89d 131 bool peer_deleted;
ebef2c09
MM
132} NICState;
133
16a3df40 134struct SocketReadState {
3cde5ea2
ZC
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;
16a3df40
ZC
139 uint32_t index;
140 uint32_t packet_len;
3cde5ea2 141 uint32_t vnet_hdr_len;
16a3df40
ZC
142 uint8_t buf[NET_BUFSIZE];
143 SocketReadStateFinalize *finalize;
144};
145
146int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size);
890ee6ab 147char *qemu_mac_strdup_printf(const uint8_t *macaddr);
4e68f7a0 148NetClientState *qemu_find_netdev(const char *id);
6c51ae73 149int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
f394b2e2 150 NetClientDriver type, int max);
4e68f7a0
SH
151NetClientState *qemu_new_net_client(NetClientInfo *info,
152 NetClientState *peer,
153 const char *model,
154 const char *name);
2f849dbd
JW
155NetClientState *qemu_new_net_control_client(NetClientInfo *info,
156 NetClientState *peer,
157 const char *model,
158 const char *name);
ebef2c09
MM
159NICState *qemu_new_nic(NetClientInfo *info,
160 NICConf *conf,
161 const char *model,
162 const char *name,
7d0fefdf 163 MemReentrancyGuard *reentrancy_guard,
ebef2c09 164 void *opaque);
948ecf21 165void qemu_del_nic(NICState *nic);
1ceef9f2 166NetClientState *qemu_get_subqueue(NICState *nic, int queue_index);
b356f76d 167NetClientState *qemu_get_queue(NICState *nic);
cc1f0f45
JW
168NICState *qemu_get_nic(NetClientState *nc);
169void *qemu_get_nic_opaque(NetClientState *nc);
b20c6b9e 170void qemu_del_net_client(NetClientState *nc);
57f9ef17
MM
171typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
172void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
705df546 173int qemu_can_receive_packet(NetClientState *nc);
35277d14
SH
174int qemu_can_send_packet(NetClientState *nc);
175ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
fbe78f4f 176 int iovcnt);
35277d14 177ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
f3b6c7fc 178 int iovcnt, NetPacketSent *sent_cb);
625a526b 179ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
705df546
JW
180ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size);
181ssize_t qemu_receive_packet_iov(NetClientState *nc,
182 const struct iovec *iov,
183 int iovcnt);
35277d14
SH
184ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
185ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
f3b6c7fc 186 int size, NetPacketSent *sent_cb);
35277d14
SH
187void qemu_purge_queued_packets(NetClientState *nc);
188void qemu_flush_queued_packets(NetClientState *nc);
94b52958 189void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge);
ac149498
SW
190void qemu_set_info_str(NetClientState *nc,
191 const char *fmt, ...) G_GNUC_PRINTF(2, 3);
35277d14 192void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
d6085e3a 193bool qemu_has_ufo(NetClientState *nc);
f03e0cf6 194bool qemu_has_uso(NetClientState *nc);
d6085e3a
SH
195bool qemu_has_vnet_hdr(NetClientState *nc);
196bool qemu_has_vnet_hdr_len(NetClientState *nc, int len);
481c5232 197bool qemu_get_using_vnet_hdr(NetClientState *nc);
d6085e3a
SH
198void qemu_using_vnet_hdr(NetClientState *nc, bool enable);
199void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
2ab0ec31 200 int ecn, int ufo, int uso4, int uso6);
481c5232 201int qemu_get_vnet_hdr_len(NetClientState *nc);
d6085e3a 202void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
c80cd6bb
GK
203int qemu_set_vnet_le(NetClientState *nc, bool is_le);
204int qemu_set_vnet_be(NetClientState *nc, bool is_be);
76d32cba 205void qemu_macaddr_default_if_unset(MACAddr *macaddr);
93e9d730
DW
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 */
215NICInfo *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 */
233bool 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 */
245DeviceState *qemu_create_nic_device(const char *typename, bool match_default,
246 const char *alias);
93125e4b
DW
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 */
266void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type,
267 const char *default_model,
268 const char *alias, const char *alias_target);
1a859593 269void print_net_client(Monitor *mon, NetClientState *nc);
16a3df40 270void net_socket_rs_init(SocketReadState *rs,
3cde5ea2
ZC
271 SocketReadStateFinalize *finalize,
272 bool vnet_hdr);
0165daae 273NetClientState *qemu_get_peer(NetClientState *nc, int queue_index);
87ecb68b 274
c6941b3b
TH
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 */
287GPtrArray *qemu_get_nic_models(const char *device_type);
288
87ecb68b
PB
289/* NIC info */
290
291#define MAX_NICS 8
292
293struct NICInfo {
6eed1856 294 MACAddr macaddr;
9203f520
MM
295 char *model;
296 char *name;
297 char *devaddr;
4e68f7a0 298 NetClientState *netdev;
48e2faf2
PM
299 int used; /* is this slot in nd_table[] being used? */
300 int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
ffe6370c 301 int nvectors;
87ecb68b
PB
302};
303
63a01ef8 304/* from net.c */
ae71d13d 305extern NetClientStateList net_clients;
73071f19
PMD
306bool netdev_is_modern(const char *optstr);
307void netdev_parse_modern(const char *optstr);
308void net_client_parse(QemuOptsList *opts_list, const char *optstr);
ad6f932f 309void show_netdevs(void);
d63ef17b 310void net_init_clients(void);
668680f7 311void net_check_clients(void);
63a01ef8 312void net_cleanup(void);
3e5a50d6
MA
313void hmp_host_net_add(Monitor *mon, const QDict *qdict);
314void hmp_host_net_remove(Monitor *mon, const QDict *qdict);
928059a3 315void netdev_add(QemuOpts *opts, Error **errp);
63a01ef8 316
1422e32d
PB
317int net_hub_id_for_client(NetClientState *nc, int *id);
318NetClientState *net_hub_port_find(int hub_id);
319
63c4db4c
PB
320#define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup"
321#define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown"
a7c36ee4
CB
322#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
323#define DEFAULT_BRIDGE_INTERFACE "br0"
f54825cc 324
ed16ab5a 325void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
9d07d757 326
eaba8f34 327#define POLYNOMIAL_BE 0x04c11db6
f1a7deb9 328#define POLYNOMIAL_LE 0xedb88320
eaba8f34 329uint32_t net_crc32(const uint8_t *p, int len);
f1a7deb9 330uint32_t net_crc32_le(const uint8_t *p, int len);
7fc8d918 331
701a8f76
PB
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
bc38e31b
JW
344static inline bool net_peer_needs_padding(NetClientState *nc)
345{
346 return nc->peer && !nc->peer->do_not_pad;
347}
348
87ecb68b 349#endif