4 #include "qemu/queue.h"
5 #include "qapi/qapi-types-net.h"
7 #include "hw/qdev-properties-system.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_NETDEV("netdev", _state, _conf.peers)
45 typedef void (NetPoll
)(NetClientState
*, bool enable
);
46 typedef bool (NetCanReceive
)(NetClientState
*);
47 typedef ssize_t (NetReceive
)(NetClientState
*, const uint8_t *, size_t);
48 typedef ssize_t (NetReceiveIOV
)(NetClientState
*, const struct iovec
*, int);
49 typedef void (NetCleanup
) (NetClientState
*);
50 typedef void (LinkStatusChanged
)(NetClientState
*);
51 typedef void (NetClientDestructor
)(NetClientState
*);
52 typedef RxFilterInfo
*(QueryRxFilter
)(NetClientState
*);
53 typedef bool (HasUfo
)(NetClientState
*);
54 typedef bool (HasVnetHdr
)(NetClientState
*);
55 typedef bool (HasVnetHdrLen
)(NetClientState
*, int);
56 typedef void (UsingVnetHdr
)(NetClientState
*, bool);
57 typedef void (SetOffload
)(NetClientState
*, int, int, int, int, int);
58 typedef void (SetVnetHdrLen
)(NetClientState
*, int);
59 typedef int (SetVnetLE
)(NetClientState
*, bool);
60 typedef int (SetVnetBE
)(NetClientState
*, bool);
61 typedef struct SocketReadState SocketReadState
;
62 typedef void (SocketReadStateFinalize
)(SocketReadState
*rs
);
63 typedef void (NetAnnounce
)(NetClientState
*);
64 typedef bool (SetSteeringEBPF
)(NetClientState
*, int);
65 typedef bool (NetCheckPeerType
)(NetClientState
*, ObjectClass
*, Error
**);
67 typedef struct NetClientInfo
{
71 NetReceive
*receive_raw
;
72 NetReceiveIOV
*receive_iov
;
73 NetCanReceive
*can_receive
;
75 LinkStatusChanged
*link_status_changed
;
76 QueryRxFilter
*query_rx_filter
;
79 HasVnetHdr
*has_vnet_hdr
;
80 HasVnetHdrLen
*has_vnet_hdr_len
;
81 UsingVnetHdr
*using_vnet_hdr
;
82 SetOffload
*set_offload
;
83 SetVnetHdrLen
*set_vnet_hdr_len
;
84 SetVnetLE
*set_vnet_le
;
85 SetVnetBE
*set_vnet_be
;
86 NetAnnounce
*announce
;
87 SetSteeringEBPF
*set_steering_ebpf
;
88 NetCheckPeerType
*check_peer_type
;
91 struct NetClientState
{
94 QTAILQ_ENTRY(NetClientState
) next
;
96 NetQueue
*incoming_queue
;
100 unsigned receive_disabled
: 1;
101 NetClientDestructor
*destructor
;
102 unsigned int queue_index
;
103 unsigned rxfilter_notify_enabled
:1;
107 bool do_not_pad
; /* do not pad to the minimum ethernet frame length */
109 QTAILQ_HEAD(, NetFilterState
) filters
;
112 typedef struct NICState
{
119 struct SocketReadState
{
120 /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
122 /* This flag decide whether to read the vnet_hdr_len field */
126 uint32_t vnet_hdr_len
;
127 uint8_t buf
[NET_BUFSIZE
];
128 SocketReadStateFinalize
*finalize
;
131 int net_fill_rstate(SocketReadState
*rs
, const uint8_t *buf
, int size
);
132 char *qemu_mac_strdup_printf(const uint8_t *macaddr
);
133 NetClientState
*qemu_find_netdev(const char *id
);
134 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
135 NetClientDriver type
, int max
);
136 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
137 NetClientState
*peer
,
140 NetClientState
*qemu_new_net_control_client(NetClientInfo
*info
,
141 NetClientState
*peer
,
144 NICState
*qemu_new_nic(NetClientInfo
*info
,
149 void qemu_del_nic(NICState
*nic
);
150 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
);
151 NetClientState
*qemu_get_queue(NICState
*nic
);
152 NICState
*qemu_get_nic(NetClientState
*nc
);
153 void *qemu_get_nic_opaque(NetClientState
*nc
);
154 void qemu_del_net_client(NetClientState
*nc
);
155 typedef void (*qemu_nic_foreach
)(NICState
*nic
, void *opaque
);
156 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
);
157 int qemu_can_receive_packet(NetClientState
*nc
);
158 int qemu_can_send_packet(NetClientState
*nc
);
159 ssize_t
qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
,
161 ssize_t
qemu_sendv_packet_async(NetClientState
*nc
, const struct iovec
*iov
,
162 int iovcnt
, NetPacketSent
*sent_cb
);
163 ssize_t
qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
);
164 ssize_t
qemu_receive_packet(NetClientState
*nc
, const uint8_t *buf
, int size
);
165 ssize_t
qemu_receive_packet_iov(NetClientState
*nc
,
166 const struct iovec
*iov
,
168 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
);
169 ssize_t
qemu_send_packet_async(NetClientState
*nc
, const uint8_t *buf
,
170 int size
, NetPacketSent
*sent_cb
);
171 void qemu_purge_queued_packets(NetClientState
*nc
);
172 void qemu_flush_queued_packets(NetClientState
*nc
);
173 void qemu_flush_or_purge_queued_packets(NetClientState
*nc
, bool purge
);
174 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6]);
175 bool qemu_has_ufo(NetClientState
*nc
);
176 bool qemu_has_vnet_hdr(NetClientState
*nc
);
177 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
);
178 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
);
179 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
181 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
);
182 int qemu_set_vnet_le(NetClientState
*nc
, bool is_le
);
183 int qemu_set_vnet_be(NetClientState
*nc
, bool is_be
);
184 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
);
185 int qemu_show_nic_models(const char *arg
, const char *const *models
);
186 void qemu_check_nic_model(NICInfo
*nd
, const char *model
);
187 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
188 const char *default_model
);
190 void print_net_client(Monitor
*mon
, NetClientState
*nc
);
191 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
);
192 void net_socket_rs_init(SocketReadState
*rs
,
193 SocketReadStateFinalize
*finalize
,
195 NetClientState
*qemu_get_peer(NetClientState
*nc
, int queue_index
);
206 NetClientState
*netdev
;
207 int used
; /* is this slot in nd_table[] being used? */
208 int instantiated
; /* does this NICInfo correspond to an instantiated NIC? */
213 extern NICInfo nd_table
[MAX_NICS
];
214 extern const char *host_net_devices
[];
217 int net_client_parse(QemuOptsList
*opts_list
, const char *str
);
218 void show_netdevs(void);
219 int net_init_clients(Error
**errp
);
220 void net_check_clients(void);
221 void net_cleanup(void);
222 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
);
223 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
);
224 void netdev_add(QemuOpts
*opts
, Error
**errp
);
226 int net_hub_id_for_client(NetClientState
*nc
, int *id
);
227 NetClientState
*net_hub_port_find(int hub_id
);
229 #define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup"
230 #define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown"
231 #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
232 #define DEFAULT_BRIDGE_INTERFACE "br0"
234 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
);
236 #define POLYNOMIAL_BE 0x04c11db6
237 #define POLYNOMIAL_LE 0xedb88320
238 uint32_t net_crc32(const uint8_t *p
, int len
);
239 uint32_t net_crc32_le(const uint8_t *p
, int len
);
241 #define vmstate_offset_macaddr(_state, _field) \
242 vmstate_offset_array(_state, _field.a, uint8_t, \
243 sizeof(typeof_field(_state, _field)))
245 #define VMSTATE_MACADDR(_field, _state) { \
246 .name = (stringify(_field)), \
247 .size = sizeof(MACAddr), \
248 .info = &vmstate_info_buffer, \
249 .flags = VMS_BUFFER, \
250 .offset = vmstate_offset_macaddr(_state, _field), \
253 static inline bool net_peer_needs_padding(NetClientState
*nc
)
255 return nc
->peer
&& !nc
->peer
->do_not_pad
;