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 MAX_QUEUE_NUM 1024
13
14
15
16
17#define NET_BUFSIZE (4096 + 65536)
18
19struct MACAddr {
20 uint8_t a[6];
21};
22
23
24
25typedef struct NICPeers {
26 NetClientState *ncs[MAX_QUEUE_NUM];
27 int32_t queues;
28} NICPeers;
29
30typedef struct NICConf {
31 MACAddr macaddr;
32 NICPeers peers;
33 int32_t bootindex;
34} NICConf;
35
36#define DEFINE_NIC_PROPERTIES(_state, _conf) \
37 DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \
38 DEFINE_PROP_VLAN("vlan", _state, _conf.peers), \
39 DEFINE_PROP_NETDEV("netdev", _state, _conf.peers), \
40 DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1)
41
42
43
44
45typedef void (NetPoll)(NetClientState *, bool enable);
46typedef int (NetCanReceive)(NetClientState *);
47typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
48typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
49typedef void (NetCleanup) (NetClientState *);
50typedef void (LinkStatusChanged)(NetClientState *);
51typedef void (NetClientDestructor)(NetClientState *);
52typedef RxFilterInfo *(QueryRxFilter)(NetClientState *);
53typedef bool (HasUfo)(NetClientState *);
54typedef bool (HasVnetHdr)(NetClientState *);
55typedef bool (HasVnetHdrLen)(NetClientState *, int);
56typedef void (UsingVnetHdr)(NetClientState *, bool);
57typedef void (SetOffload)(NetClientState *, int, int, int, int, int);
58typedef void (SetVnetHdrLen)(NetClientState *, int);
59
60typedef struct NetClientInfo {
61 NetClientOptionsKind type;
62 size_t size;
63 NetReceive *receive;
64 NetReceive *receive_raw;
65 NetReceiveIOV *receive_iov;
66 NetCanReceive *can_receive;
67 NetCleanup *cleanup;
68 LinkStatusChanged *link_status_changed;
69 QueryRxFilter *query_rx_filter;
70 NetPoll *poll;
71 HasUfo *has_ufo;
72 HasVnetHdr *has_vnet_hdr;
73 HasVnetHdrLen *has_vnet_hdr_len;
74 UsingVnetHdr *using_vnet_hdr;
75 SetOffload *set_offload;
76 SetVnetHdrLen *set_vnet_hdr_len;
77} NetClientInfo;
78
79struct NetClientState {
80 NetClientInfo *info;
81 int link_down;
82 QTAILQ_ENTRY(NetClientState) next;
83 NetClientState *peer;
84 NetQueue *incoming_queue;
85 char *model;
86 char *name;
87 char info_str[256];
88 unsigned receive_disabled : 1;
89 NetClientDestructor *destructor;
90 unsigned int queue_index;
91 unsigned rxfilter_notify_enabled:1;
92};
93
94typedef struct NICState {
95 NetClientState *ncs;
96 NICConf *conf;
97 void *opaque;
98 bool peer_deleted;
99} NICState;
100
101NetClientState *qemu_find_netdev(const char *id);
102int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
103 NetClientOptionsKind type, int max);
104NetClientState *qemu_new_net_client(NetClientInfo *info,
105 NetClientState *peer,
106 const char *model,
107 const char *name);
108NICState *qemu_new_nic(NetClientInfo *info,
109 NICConf *conf,
110 const char *model,
111 const char *name,
112 void *opaque);
113void qemu_del_nic(NICState *nic);
114NetClientState *qemu_get_subqueue(NICState *nic, int queue_index);
115NetClientState *qemu_get_queue(NICState *nic);
116NICState *qemu_get_nic(NetClientState *nc);
117void *qemu_get_nic_opaque(NetClientState *nc);
118void qemu_del_net_client(NetClientState *nc);
119NetClientState *qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
120 const char *client_str);
121typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
122void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
123int qemu_can_send_packet(NetClientState *nc);
124ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
125 int iovcnt);
126ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
127 int iovcnt, NetPacketSent *sent_cb);
128void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
129ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
130ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
131 int size, NetPacketSent *sent_cb);
132void qemu_purge_queued_packets(NetClientState *nc);
133void qemu_flush_queued_packets(NetClientState *nc);
134void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
135bool qemu_has_ufo(NetClientState *nc);
136bool qemu_has_vnet_hdr(NetClientState *nc);
137bool qemu_has_vnet_hdr_len(NetClientState *nc, int len);
138void qemu_using_vnet_hdr(NetClientState *nc, bool enable);
139void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
140 int ecn, int ufo);
141void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
142void qemu_macaddr_default_if_unset(MACAddr *macaddr);
143int qemu_show_nic_models(const char *arg, const char *const *models);
144void qemu_check_nic_model(NICInfo *nd, const char *model);
145int qemu_find_nic_model(NICInfo *nd, const char * const *models,
146 const char *default_model);
147
148ssize_t qemu_deliver_packet(NetClientState *sender,
149 unsigned flags,
150 const uint8_t *data,
151 size_t size,
152 void *opaque);
153ssize_t qemu_deliver_packet_iov(NetClientState *sender,
154 unsigned flags,
155 const struct iovec *iov,
156 int iovcnt,
157 void *opaque);
158
159void print_net_client(Monitor *mon, NetClientState *nc);
160void do_info_network(Monitor *mon, const QDict *qdict);
161
162
163
164#define MAX_NICS 8
165
166struct NICInfo {
167 MACAddr macaddr;
168 char *model;
169 char *name;
170 char *devaddr;
171 NetClientState *netdev;
172 int used;
173 int instantiated;
174 int nvectors;
175};
176
177extern int nb_nics;
178extern NICInfo nd_table[MAX_NICS];
179extern int default_net;
180extern const char *host_net_devices[];
181
182
183extern const char *legacy_tftp_prefix;
184extern const char *legacy_bootp_filename;
185
186int net_client_init(QemuOpts *opts, int is_netdev, Error **errp);
187int net_client_parse(QemuOptsList *opts_list, const char *str);
188int net_init_clients(void);
189void net_check_clients(void);
190void net_cleanup(void);
191void net_host_device_add(Monitor *mon, const QDict *qdict);
192void net_host_device_remove(Monitor *mon, const QDict *qdict);
193void netdev_add(QemuOpts *opts, Error **errp);
194int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret);
195
196int net_hub_id_for_client(NetClientState *nc, int *id);
197NetClientState *net_hub_port_find(int hub_id);
198
199#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
200#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
201#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
202#define DEFAULT_BRIDGE_INTERFACE "br0"
203
204void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
205
206#define POLYNOMIAL 0x04c11db6
207unsigned compute_mcast_idx(const uint8_t *ep);
208
209#define vmstate_offset_macaddr(_state, _field) \
210 vmstate_offset_array(_state, _field.a, uint8_t, \
211 sizeof(typeof_field(_state, _field)))
212
213#define VMSTATE_MACADDR(_field, _state) { \
214 .name = (stringify(_field)), \
215 .size = sizeof(MACAddr), \
216 .info = &vmstate_info_buffer, \
217 .flags = VMS_BUFFER, \
218 .offset = vmstate_offset_macaddr(_state, _field), \
219}
220
221#endif
222