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