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