xref: /openbmc/qemu/include/net/net.h (revision 9c489ea6)
1 #ifndef QEMU_NET_H
2 #define QEMU_NET_H
3 
4 #include "qemu/queue.h"
5 #include "qemu-common.h"
6 #include "qapi/qmp/qdict.h"
7 #include "qemu/option.h"
8 #include "net/queue.h"
9 #include "migration/vmstate.h"
10 #include "qapi-types.h"
11 
12 #define MAC_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
13 #define MAC_ARG(x) ((uint8_t *)(x))[0], ((uint8_t *)(x))[1], \
14                    ((uint8_t *)(x))[2], ((uint8_t *)(x))[3], \
15                    ((uint8_t *)(x))[4], ((uint8_t *)(x))[5]
16 
17 #define MAX_QUEUE_NUM 1024
18 
19 /* Maximum GSO packet size (64k) plus plenty of room for
20  * the ethernet and virtio_net headers
21  */
22 #define NET_BUFSIZE (4096 + 65536)
23 
24 struct MACAddr {
25     uint8_t a[6];
26 };
27 
28 /* qdev nic properties */
29 
30 typedef struct NICPeers {
31     NetClientState *ncs[MAX_QUEUE_NUM];
32     int32_t queues;
33 } NICPeers;
34 
35 typedef struct NICConf {
36     MACAddr macaddr;
37     NICPeers peers;
38     int32_t bootindex;
39 } NICConf;
40 
41 #define DEFINE_NIC_PROPERTIES(_state, _conf)                            \
42     DEFINE_PROP_MACADDR("mac",   _state, _conf.macaddr),                \
43     DEFINE_PROP_VLAN("vlan",     _state, _conf.peers),                   \
44     DEFINE_PROP_NETDEV("netdev", _state, _conf.peers)
45 
46 
47 /* Net clients */
48 
49 typedef void (NetPoll)(NetClientState *, bool enable);
50 typedef int (NetCanReceive)(NetClientState *);
51 typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
52 typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
53 typedef void (NetCleanup) (NetClientState *);
54 typedef void (LinkStatusChanged)(NetClientState *);
55 typedef void (NetClientDestructor)(NetClientState *);
56 typedef RxFilterInfo *(QueryRxFilter)(NetClientState *);
57 typedef bool (HasUfo)(NetClientState *);
58 typedef bool (HasVnetHdr)(NetClientState *);
59 typedef bool (HasVnetHdrLen)(NetClientState *, int);
60 typedef void (UsingVnetHdr)(NetClientState *, bool);
61 typedef void (SetOffload)(NetClientState *, int, int, int, int, int);
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 
68 typedef struct NetClientInfo {
69     NetClientDriver type;
70     size_t size;
71     NetReceive *receive;
72     NetReceive *receive_raw;
73     NetReceiveIOV *receive_iov;
74     NetCanReceive *can_receive;
75     NetCleanup *cleanup;
76     LinkStatusChanged *link_status_changed;
77     QueryRxFilter *query_rx_filter;
78     NetPoll *poll;
79     HasUfo *has_ufo;
80     HasVnetHdr *has_vnet_hdr;
81     HasVnetHdrLen *has_vnet_hdr_len;
82     UsingVnetHdr *using_vnet_hdr;
83     SetOffload *set_offload;
84     SetVnetHdrLen *set_vnet_hdr_len;
85     SetVnetLE *set_vnet_le;
86     SetVnetBE *set_vnet_be;
87 } NetClientInfo;
88 
89 struct NetClientState {
90     NetClientInfo *info;
91     int link_down;
92     QTAILQ_ENTRY(NetClientState) next;
93     NetClientState *peer;
94     NetQueue *incoming_queue;
95     char *model;
96     char *name;
97     char info_str[256];
98     unsigned receive_disabled : 1;
99     NetClientDestructor *destructor;
100     unsigned int queue_index;
101     unsigned rxfilter_notify_enabled:1;
102     int vring_enable;
103     int vnet_hdr_len;
104     QTAILQ_HEAD(NetFilterHead, NetFilterState) filters;
105 };
106 
107 typedef struct NICState {
108     NetClientState *ncs;
109     NICConf *conf;
110     void *opaque;
111     bool peer_deleted;
112 } NICState;
113 
114 struct SocketReadState {
115     /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
116     int state;
117     /* This flag decide whether to read the vnet_hdr_len field */
118     bool vnet_hdr;
119     uint32_t index;
120     uint32_t packet_len;
121     uint32_t vnet_hdr_len;
122     uint8_t buf[NET_BUFSIZE];
123     SocketReadStateFinalize *finalize;
124 };
125 
126 int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size);
127 char *qemu_mac_strdup_printf(const uint8_t *macaddr);
128 NetClientState *qemu_find_netdev(const char *id);
129 int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
130                                  NetClientDriver type, int max);
131 NetClientState *qemu_new_net_client(NetClientInfo *info,
132                                     NetClientState *peer,
133                                     const char *model,
134                                     const char *name);
135 NICState *qemu_new_nic(NetClientInfo *info,
136                        NICConf *conf,
137                        const char *model,
138                        const char *name,
139                        void *opaque);
140 void qemu_del_nic(NICState *nic);
141 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index);
142 NetClientState *qemu_get_queue(NICState *nic);
143 NICState *qemu_get_nic(NetClientState *nc);
144 void *qemu_get_nic_opaque(NetClientState *nc);
145 void qemu_del_net_client(NetClientState *nc);
146 typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
147 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
148 int qemu_can_send_packet(NetClientState *nc);
149 ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
150                           int iovcnt);
151 ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
152                                 int iovcnt, NetPacketSent *sent_cb);
153 void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
154 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
155 ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
156                                int size, NetPacketSent *sent_cb);
157 void qemu_purge_queued_packets(NetClientState *nc);
158 void qemu_flush_queued_packets(NetClientState *nc);
159 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
160 bool qemu_has_ufo(NetClientState *nc);
161 bool qemu_has_vnet_hdr(NetClientState *nc);
162 bool qemu_has_vnet_hdr_len(NetClientState *nc, int len);
163 void qemu_using_vnet_hdr(NetClientState *nc, bool enable);
164 void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
165                       int ecn, int ufo);
166 void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
167 int qemu_set_vnet_le(NetClientState *nc, bool is_le);
168 int qemu_set_vnet_be(NetClientState *nc, bool is_be);
169 void qemu_macaddr_default_if_unset(MACAddr *macaddr);
170 int qemu_show_nic_models(const char *arg, const char *const *models);
171 void qemu_check_nic_model(NICInfo *nd, const char *model);
172 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
173                         const char *default_model);
174 
175 ssize_t qemu_deliver_packet_iov(NetClientState *sender,
176                             unsigned flags,
177                             const struct iovec *iov,
178                             int iovcnt,
179                             void *opaque);
180 
181 void print_net_client(Monitor *mon, NetClientState *nc);
182 void hmp_info_network(Monitor *mon, const QDict *qdict);
183 void net_socket_rs_init(SocketReadState *rs,
184                         SocketReadStateFinalize *finalize,
185                         bool vnet_hdr);
186 
187 /* NIC info */
188 
189 #define MAX_NICS 8
190 
191 struct NICInfo {
192     MACAddr macaddr;
193     char *model;
194     char *name;
195     char *devaddr;
196     NetClientState *netdev;
197     int used;         /* is this slot in nd_table[] being used? */
198     int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
199     int nvectors;
200 };
201 
202 extern int nb_nics;
203 extern NICInfo nd_table[MAX_NICS];
204 extern const char *host_net_devices[];
205 
206 /* from net.c */
207 extern const char *legacy_tftp_prefix;
208 extern const char *legacy_bootp_filename;
209 
210 int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp);
211 int net_client_parse(QemuOptsList *opts_list, const char *str);
212 int net_init_clients(void);
213 void net_check_clients(void);
214 void net_cleanup(void);
215 void hmp_host_net_add(Monitor *mon, const QDict *qdict);
216 void hmp_host_net_remove(Monitor *mon, const QDict *qdict);
217 void netdev_add(QemuOpts *opts, Error **errp);
218 void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp);
219 
220 int net_hub_id_for_client(NetClientState *nc, int *id);
221 NetClientState *net_hub_port_find(int hub_id);
222 
223 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
224 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
225 #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
226 #define DEFAULT_BRIDGE_INTERFACE "br0"
227 
228 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
229 
230 #define POLYNOMIAL 0x04c11db6
231 unsigned compute_mcast_idx(const uint8_t *ep);
232 
233 #define vmstate_offset_macaddr(_state, _field)                       \
234     vmstate_offset_array(_state, _field.a, uint8_t,                \
235                          sizeof(typeof_field(_state, _field)))
236 
237 #define VMSTATE_MACADDR(_field, _state) {                            \
238     .name       = (stringify(_field)),                               \
239     .size       = sizeof(MACAddr),                                   \
240     .info       = &vmstate_info_buffer,                              \
241     .flags      = VMS_BUFFER,                                        \
242     .offset     = vmstate_offset_macaddr(_state, _field),            \
243 }
244 
245 #endif
246