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