4 #include "qemu/queue.h"
5 #include "qapi-types.h"
7 #include "migration/vmstate.h"
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]
14 #define MAX_QUEUE_NUM 1024
16 /* Maximum GSO packet size (64k) plus plenty of room for
17 * the ethernet and virtio_net headers
19 #define NET_BUFSIZE (4096 + 65536)
25 /* qdev nic properties */
27 typedef struct NICPeers
{
28 NetClientState
*ncs
[MAX_QUEUE_NUM
];
32 typedef struct NICConf
{
38 #define DEFINE_NIC_PROPERTIES(_state, _conf) \
39 DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \
40 DEFINE_PROP_VLAN("vlan", _state, _conf.peers), \
41 DEFINE_PROP_NETDEV("netdev", _state, _conf.peers)
46 typedef void (NetPoll
)(NetClientState
*, bool enable
);
47 typedef int (NetCanReceive
)(NetClientState
*);
48 typedef ssize_t (NetReceive
)(NetClientState
*, const uint8_t *, size_t);
49 typedef ssize_t (NetReceiveIOV
)(NetClientState
*, const struct iovec
*, int);
50 typedef void (NetCleanup
) (NetClientState
*);
51 typedef void (LinkStatusChanged
)(NetClientState
*);
52 typedef void (NetClientDestructor
)(NetClientState
*);
53 typedef RxFilterInfo
*(QueryRxFilter
)(NetClientState
*);
54 typedef bool (HasUfo
)(NetClientState
*);
55 typedef bool (HasVnetHdr
)(NetClientState
*);
56 typedef bool (HasVnetHdrLen
)(NetClientState
*, int);
57 typedef void (UsingVnetHdr
)(NetClientState
*, bool);
58 typedef void (SetOffload
)(NetClientState
*, int, int, int, int, int);
59 typedef void (SetVnetHdrLen
)(NetClientState
*, int);
60 typedef int (SetVnetLE
)(NetClientState
*, bool);
61 typedef int (SetVnetBE
)(NetClientState
*, bool);
62 typedef struct SocketReadState SocketReadState
;
63 typedef void (SocketReadStateFinalize
)(SocketReadState
*rs
);
65 typedef struct NetClientInfo
{
69 NetReceive
*receive_raw
;
70 NetReceiveIOV
*receive_iov
;
71 NetCanReceive
*can_receive
;
73 LinkStatusChanged
*link_status_changed
;
74 QueryRxFilter
*query_rx_filter
;
77 HasVnetHdr
*has_vnet_hdr
;
78 HasVnetHdrLen
*has_vnet_hdr_len
;
79 UsingVnetHdr
*using_vnet_hdr
;
80 SetOffload
*set_offload
;
81 SetVnetHdrLen
*set_vnet_hdr_len
;
82 SetVnetLE
*set_vnet_le
;
83 SetVnetBE
*set_vnet_be
;
86 struct NetClientState
{
89 QTAILQ_ENTRY(NetClientState
) next
;
91 NetQueue
*incoming_queue
;
95 unsigned receive_disabled
: 1;
96 NetClientDestructor
*destructor
;
97 unsigned int queue_index
;
98 unsigned rxfilter_notify_enabled
:1;
101 QTAILQ_HEAD(NetFilterHead
, NetFilterState
) filters
;
104 typedef struct NICState
{
111 struct SocketReadState
{
112 /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
114 /* This flag decide whether to read the vnet_hdr_len field */
118 uint32_t vnet_hdr_len
;
119 uint8_t buf
[NET_BUFSIZE
];
120 SocketReadStateFinalize
*finalize
;
123 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
);
124 char *qemu_mac_strdup_printf(const uint8_t *macaddr
);
125 NetClientState
*qemu_find_netdev(const char *id
);
126 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
127 NetClientDriver type
, int max
);
128 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
129 NetClientState
*peer
,
132 NICState
*qemu_new_nic(NetClientInfo
*info
,
137 void qemu_del_nic(NICState
*nic
);
138 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
);
139 NetClientState
*qemu_get_queue(NICState
*nic
);
140 NICState
*qemu_get_nic(NetClientState
*nc
);
141 void *qemu_get_nic_opaque(NetClientState
*nc
);
142 void qemu_del_net_client(NetClientState
*nc
);
143 typedef void (*qemu_nic_foreach
)(NICState
*nic
, void *opaque
);
144 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
);
145 int qemu_can_send_packet(NetClientState
*nc
);
146 ssize_t
qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
,
148 ssize_t
qemu_sendv_packet_async(NetClientState
*nc
, const struct iovec
*iov
,
149 int iovcnt
, NetPacketSent
*sent_cb
);
150 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
);
151 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
);
152 ssize_t
qemu_send_packet_async(NetClientState
*nc
, const uint8_t *buf
,
153 int size
, NetPacketSent
*sent_cb
);
154 void qemu_purge_queued_packets(NetClientState
*nc
);
155 void qemu_flush_queued_packets(NetClientState
*nc
);
156 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6]);
157 bool qemu_has_ufo(NetClientState
*nc
);
158 bool qemu_has_vnet_hdr(NetClientState
*nc
);
159 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
);
160 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
);
161 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
163 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
);
164 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
);
165 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
);
166 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
);
167 int qemu_show_nic_models(const char *arg
, const char *const *models
);
168 void qemu_check_nic_model(NICInfo
*nd
, const char *model
);
169 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
170 const char *default_model
);
172 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
174 const struct iovec
*iov
,
178 void print_net_client(Monitor
*mon
, NetClientState
*nc
);
179 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
);
180 void net_socket_rs_init(SocketReadState
*rs
,
181 SocketReadStateFinalize
*finalize
,
193 NetClientState
*netdev
;
194 int used
; /* is this slot in nd_table[] being used? */
195 int instantiated
; /* does this NICInfo correspond to an instantiated NIC? */
200 extern NICInfo nd_table
[MAX_NICS
];
201 extern const char *host_net_devices
[];
204 extern const char *legacy_tftp_prefix
;
205 extern const char *legacy_bootp_filename
;
207 int net_client_parse(QemuOptsList
*opts_list
, const char *str
);
208 int net_init_clients(Error
**errp
);
209 void net_check_clients(void);
210 void net_cleanup(void);
211 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
);
212 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
);
213 void netdev_add(QemuOpts
*opts
, Error
**errp
);
214 void qmp_netdev_add(QDict
*qdict
, QObject
**ret
, Error
**errp
);
216 int net_hub_id_for_client(NetClientState
*nc
, int *id
);
217 NetClientState
*net_hub_port_find(int hub_id
);
219 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
220 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
221 #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
222 #define DEFAULT_BRIDGE_INTERFACE "br0"
224 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
);
226 #define POLYNOMIAL_BE 0x04c11db6
227 #define POLYNOMIAL_LE 0xedb88320
228 uint32_t net_crc32(const uint8_t *p
, int len
);
229 uint32_t net_crc32_le(const uint8_t *p
, int len
);
231 #define vmstate_offset_macaddr(_state, _field) \
232 vmstate_offset_array(_state, _field.a, uint8_t, \
233 sizeof(typeof_field(_state, _field)))
235 #define VMSTATE_MACADDR(_field, _state) { \
236 .name = (stringify(_field)), \
237 .size = sizeof(MACAddr), \
238 .info = &vmstate_info_buffer, \
239 .flags = VMS_BUFFER, \
240 .offset = vmstate_offset_macaddr(_state, _field), \