xref: /openbmc/qemu/include/net/net.h (revision d27532e4)
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 (HasVnetHdr)(NetClientState *);
58 typedef bool (HasVnetHdrLen)(NetClientState *, int);
59 typedef bool (GetUsingVnetHdr)(NetClientState *);
60 typedef void (UsingVnetHdr)(NetClientState *, bool);
61 typedef void (SetOffload)(NetClientState *, int, int, int, int, int);
62 typedef int (GetVnetHdrLen)(NetClientState *);
63 typedef void (SetVnetHdrLen)(NetClientState *, int);
64 typedef int (SetVnetLE)(NetClientState *, bool);
65 typedef int (SetVnetBE)(NetClientState *, bool);
66 typedef struct SocketReadState SocketReadState;
67 typedef void (SocketReadStateFinalize)(SocketReadState *rs);
68 typedef void (NetAnnounce)(NetClientState *);
69 typedef bool (SetSteeringEBPF)(NetClientState *, int);
70 typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **);
71 
72 typedef struct NetClientInfo {
73     NetClientDriver type;
74     size_t size;
75     NetReceive *receive;
76     NetReceive *receive_raw;
77     NetReceiveIOV *receive_iov;
78     NetCanReceive *can_receive;
79     NetStart *start;
80     NetLoad *load;
81     NetStop *stop;
82     NetCleanup *cleanup;
83     LinkStatusChanged *link_status_changed;
84     QueryRxFilter *query_rx_filter;
85     NetPoll *poll;
86     HasUfo *has_ufo;
87     HasVnetHdr *has_vnet_hdr;
88     HasVnetHdrLen *has_vnet_hdr_len;
89     GetUsingVnetHdr *get_using_vnet_hdr;
90     UsingVnetHdr *using_vnet_hdr;
91     SetOffload *set_offload;
92     GetVnetHdrLen *get_vnet_hdr_len;
93     SetVnetHdrLen *set_vnet_hdr_len;
94     SetVnetLE *set_vnet_le;
95     SetVnetBE *set_vnet_be;
96     NetAnnounce *announce;
97     SetSteeringEBPF *set_steering_ebpf;
98     NetCheckPeerType *check_peer_type;
99 } NetClientInfo;
100 
101 struct NetClientState {
102     NetClientInfo *info;
103     int link_down;
104     QTAILQ_ENTRY(NetClientState) next;
105     NetClientState *peer;
106     NetQueue *incoming_queue;
107     char *model;
108     char *name;
109     char info_str[256];
110     unsigned receive_disabled : 1;
111     NetClientDestructor *destructor;
112     unsigned int queue_index;
113     unsigned rxfilter_notify_enabled:1;
114     int vring_enable;
115     int vnet_hdr_len;
116     bool is_netdev;
117     bool do_not_pad; /* do not pad to the minimum ethernet frame length */
118     bool is_datapath;
119     QTAILQ_HEAD(, NetFilterState) filters;
120 };
121 
122 typedef QTAILQ_HEAD(NetClientStateList, NetClientState) NetClientStateList;
123 
124 typedef struct NICState {
125     NetClientState *ncs;
126     NICConf *conf;
127     void *opaque;
128     bool peer_deleted;
129 } NICState;
130 
131 struct SocketReadState {
132     /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
133     int state;
134     /* This flag decide whether to read the vnet_hdr_len field */
135     bool vnet_hdr;
136     uint32_t index;
137     uint32_t packet_len;
138     uint32_t vnet_hdr_len;
139     uint8_t buf[NET_BUFSIZE];
140     SocketReadStateFinalize *finalize;
141 };
142 
143 int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size);
144 char *qemu_mac_strdup_printf(const uint8_t *macaddr);
145 NetClientState *qemu_find_netdev(const char *id);
146 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
147                                  NetClientDriver type, int max);
148 NetClientState *qemu_new_net_client(NetClientInfo *info,
149                                     NetClientState *peer,
150                                     const char *model,
151                                     const char *name);
152 NetClientState *qemu_new_net_control_client(NetClientInfo *info,
153                                         NetClientState *peer,
154                                         const char *model,
155                                         const char *name);
156 NICState *qemu_new_nic(NetClientInfo *info,
157                        NICConf *conf,
158                        const char *model,
159                        const char *name,
160                        void *opaque);
161 void qemu_del_nic(NICState *nic);
162 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index);
163 NetClientState *qemu_get_queue(NICState *nic);
164 NICState *qemu_get_nic(NetClientState *nc);
165 void *qemu_get_nic_opaque(NetClientState *nc);
166 void qemu_del_net_client(NetClientState *nc);
167 typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
168 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
169 int qemu_can_receive_packet(NetClientState *nc);
170 int qemu_can_send_packet(NetClientState *nc);
171 ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
172                           int iovcnt);
173 ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
174                                 int iovcnt, NetPacketSent *sent_cb);
175 ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
176 ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size);
177 ssize_t qemu_receive_packet_iov(NetClientState *nc,
178                                 const struct iovec *iov,
179                                 int iovcnt);
180 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
181 ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
182                                int size, NetPacketSent *sent_cb);
183 void qemu_purge_queued_packets(NetClientState *nc);
184 void qemu_flush_queued_packets(NetClientState *nc);
185 void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge);
186 void qemu_set_info_str(NetClientState *nc,
187                        const char *fmt, ...) G_GNUC_PRINTF(2, 3);
188 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
189 bool qemu_has_ufo(NetClientState *nc);
190 bool qemu_has_vnet_hdr(NetClientState *nc);
191 bool qemu_has_vnet_hdr_len(NetClientState *nc, int len);
192 bool qemu_get_using_vnet_hdr(NetClientState *nc);
193 void qemu_using_vnet_hdr(NetClientState *nc, bool enable);
194 void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
195                       int ecn, int ufo);
196 int qemu_get_vnet_hdr_len(NetClientState *nc);
197 void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
198 int qemu_set_vnet_le(NetClientState *nc, bool is_le);
199 int qemu_set_vnet_be(NetClientState *nc, bool is_be);
200 void qemu_macaddr_default_if_unset(MACAddr *macaddr);
201 int qemu_show_nic_models(const char *arg, const char *const *models);
202 void qemu_check_nic_model(NICInfo *nd, const char *model);
203 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
204                         const char *default_model);
205 
206 void print_net_client(Monitor *mon, NetClientState *nc);
207 void net_socket_rs_init(SocketReadState *rs,
208                         SocketReadStateFinalize *finalize,
209                         bool vnet_hdr);
210 NetClientState *qemu_get_peer(NetClientState *nc, int queue_index);
211 
212 /**
213  * qemu_get_nic_models:
214  * @device_type: Defines which devices should be taken into consideration
215  *               (e.g. TYPE_DEVICE for all devices, or TYPE_PCI_DEVICE for PCI)
216  *
217  * Get an array of pointers to names of NIC devices that are available in
218  * the QEMU binary. The array is terminated with a NULL pointer entry.
219  * The caller is responsible for freeing the memory when it is not required
220  * anymore, e.g. with g_ptr_array_free(..., true).
221  *
222  * Returns: Pointer to the array that contains the pointers to the names.
223  */
224 GPtrArray *qemu_get_nic_models(const char *device_type);
225 
226 /* NIC info */
227 
228 #define MAX_NICS 8
229 
230 struct NICInfo {
231     MACAddr macaddr;
232     char *model;
233     char *name;
234     char *devaddr;
235     NetClientState *netdev;
236     int used;         /* is this slot in nd_table[] being used? */
237     int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
238     int nvectors;
239 };
240 
241 extern int nb_nics;
242 extern NICInfo nd_table[MAX_NICS];
243 extern const char *host_net_devices[];
244 
245 /* from net.c */
246 extern NetClientStateList net_clients;
247 bool netdev_is_modern(const char *optarg);
248 void netdev_parse_modern(const char *optarg);
249 void net_client_parse(QemuOptsList *opts_list, const char *str);
250 void show_netdevs(void);
251 void net_init_clients(void);
252 void net_check_clients(void);
253 void net_cleanup(void);
254 void hmp_host_net_add(Monitor *mon, const QDict *qdict);
255 void hmp_host_net_remove(Monitor *mon, const QDict *qdict);
256 void netdev_add(QemuOpts *opts, Error **errp);
257 
258 int net_hub_id_for_client(NetClientState *nc, int *id);
259 NetClientState *net_hub_port_find(int hub_id);
260 
261 #define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup"
262 #define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown"
263 #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
264 #define DEFAULT_BRIDGE_INTERFACE "br0"
265 
266 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
267 
268 #define POLYNOMIAL_BE 0x04c11db6
269 #define POLYNOMIAL_LE 0xedb88320
270 uint32_t net_crc32(const uint8_t *p, int len);
271 uint32_t net_crc32_le(const uint8_t *p, int len);
272 
273 #define vmstate_offset_macaddr(_state, _field)                       \
274     vmstate_offset_array(_state, _field.a, uint8_t,                \
275                          sizeof(typeof_field(_state, _field)))
276 
277 #define VMSTATE_MACADDR(_field, _state) {                            \
278     .name       = (stringify(_field)),                               \
279     .size       = sizeof(MACAddr),                                   \
280     .info       = &vmstate_info_buffer,                              \
281     .flags      = VMS_BUFFER,                                        \
282     .offset     = vmstate_offset_macaddr(_state, _field),            \
283 }
284 
285 static inline bool net_peer_needs_padding(NetClientState *nc)
286 {
287   return nc->peer && !nc->peer->do_not_pad;
288 }
289 
290 #endif
291