4 #include "qemu/queue.h"
5 #include "qemu-common.h"
6 #include "qapi/qmp/qdict.h"
7 #include "qemu/option.h"
9 #include "migration/vmstate.h"
10 #include "qapi-types.h"
12 #define MAX_QUEUE_NUM 1024
14 /* Maximum GSO packet size (64k) plus plenty of room for
15 * the ethernet and virtio_net headers
17 #define NET_BUFSIZE (4096 + 65536)
23 /* qdev nic properties */
25 typedef struct NICPeers
{
26 NetClientState
*ncs
[MAX_QUEUE_NUM
];
30 typedef struct NICConf
{
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)
44 typedef void (NetPoll
)(NetClientState
*, bool enable
);
45 typedef int (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);
59 typedef struct NetClientInfo
{
60 NetClientOptionsKind type
;
63 NetReceive
*receive_raw
;
64 NetReceiveIOV
*receive_iov
;
65 NetCanReceive
*can_receive
;
67 LinkStatusChanged
*link_status_changed
;
68 QueryRxFilter
*query_rx_filter
;
71 HasVnetHdr
*has_vnet_hdr
;
72 HasVnetHdrLen
*has_vnet_hdr_len
;
73 UsingVnetHdr
*using_vnet_hdr
;
74 SetOffload
*set_offload
;
75 SetVnetHdrLen
*set_vnet_hdr_len
;
78 struct NetClientState
{
81 QTAILQ_ENTRY(NetClientState
) next
;
83 NetQueue
*incoming_queue
;
87 unsigned receive_disabled
: 1;
88 NetClientDestructor
*destructor
;
89 unsigned int queue_index
;
90 unsigned rxfilter_notify_enabled
:1;
93 typedef struct NICState
{
100 NetClientState
*qemu_find_netdev(const char *id
);
101 int qemu_find_net_clients_except(const char *id
, NetClientState
**ncs
,
102 NetClientOptionsKind type
, int max
);
103 NetClientState
*qemu_new_net_client(NetClientInfo
*info
,
104 NetClientState
*peer
,
107 NICState
*qemu_new_nic(NetClientInfo
*info
,
112 void qemu_del_nic(NICState
*nic
);
113 NetClientState
*qemu_get_subqueue(NICState
*nic
, int queue_index
);
114 NetClientState
*qemu_get_queue(NICState
*nic
);
115 NICState
*qemu_get_nic(NetClientState
*nc
);
116 void *qemu_get_nic_opaque(NetClientState
*nc
);
117 void qemu_del_net_client(NetClientState
*nc
);
118 NetClientState
*qemu_find_vlan_client_by_name(Monitor
*mon
, int vlan_id
,
119 const char *client_str
);
120 typedef void (*qemu_nic_foreach
)(NICState
*nic
, void *opaque
);
121 void qemu_foreach_nic(qemu_nic_foreach func
, void *opaque
);
122 int qemu_can_send_packet(NetClientState
*nc
);
123 ssize_t
qemu_sendv_packet(NetClientState
*nc
, const struct iovec
*iov
,
125 ssize_t
qemu_sendv_packet_async(NetClientState
*nc
, const struct iovec
*iov
,
126 int iovcnt
, NetPacketSent
*sent_cb
);
127 void qemu_send_packet(NetClientState
*nc
, const uint8_t *buf
, int size
);
128 ssize_t
qemu_send_packet_raw(NetClientState
*nc
, const uint8_t *buf
, int size
);
129 ssize_t
qemu_send_packet_async(NetClientState
*nc
, const uint8_t *buf
,
130 int size
, NetPacketSent
*sent_cb
);
131 void qemu_purge_queued_packets(NetClientState
*nc
);
132 void qemu_flush_queued_packets(NetClientState
*nc
);
133 void qemu_format_nic_info_str(NetClientState
*nc
, uint8_t macaddr
[6]);
134 bool qemu_has_ufo(NetClientState
*nc
);
135 bool qemu_has_vnet_hdr(NetClientState
*nc
);
136 bool qemu_has_vnet_hdr_len(NetClientState
*nc
, int len
);
137 void qemu_using_vnet_hdr(NetClientState
*nc
, bool enable
);
138 void qemu_set_offload(NetClientState
*nc
, int csum
, int tso4
, int tso6
,
140 void qemu_set_vnet_hdr_len(NetClientState
*nc
, int len
);
141 void qemu_macaddr_default_if_unset(MACAddr
*macaddr
);
142 int qemu_show_nic_models(const char *arg
, const char *const *models
);
143 void qemu_check_nic_model(NICInfo
*nd
, const char *model
);
144 int qemu_find_nic_model(NICInfo
*nd
, const char * const *models
,
145 const char *default_model
);
147 ssize_t
qemu_deliver_packet(NetClientState
*sender
,
152 ssize_t
qemu_deliver_packet_iov(NetClientState
*sender
,
154 const struct iovec
*iov
,
158 void print_net_client(Monitor
*mon
, NetClientState
*nc
);
159 void hmp_info_network(Monitor
*mon
, const QDict
*qdict
);
170 NetClientState
*netdev
;
171 int used
; /* is this slot in nd_table[] being used? */
172 int instantiated
; /* does this NICInfo correspond to an instantiated NIC? */
177 extern NICInfo nd_table
[MAX_NICS
];
178 extern int default_net
;
179 extern const char *host_net_devices
[];
182 extern const char *legacy_tftp_prefix
;
183 extern const char *legacy_bootp_filename
;
185 int net_client_init(QemuOpts
*opts
, int is_netdev
, Error
**errp
);
186 int net_client_parse(QemuOptsList
*opts_list
, const char *str
);
187 int net_init_clients(void);
188 void net_check_clients(void);
189 void net_cleanup(void);
190 void hmp_host_net_add(Monitor
*mon
, const QDict
*qdict
);
191 void hmp_host_net_remove(Monitor
*mon
, const QDict
*qdict
);
192 void netdev_add(QemuOpts
*opts
, Error
**errp
);
193 int qmp_netdev_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret
);
195 int net_hub_id_for_client(NetClientState
*nc
, int *id
);
196 NetClientState
*net_hub_port_find(int hub_id
);
198 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
199 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
200 #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
201 #define DEFAULT_BRIDGE_INTERFACE "br0"
203 void qdev_set_nic_properties(DeviceState
*dev
, NICInfo
*nd
);
205 #define POLYNOMIAL 0x04c11db6
206 unsigned compute_mcast_idx(const uint8_t *ep
);
208 #define vmstate_offset_macaddr(_state, _field) \
209 vmstate_offset_array(_state, _field.a, uint8_t, \
210 sizeof(typeof_field(_state, _field)))
212 #define VMSTATE_MACADDR(_field, _state) { \
213 .name = (stringify(_field)), \
214 .size = sizeof(MACAddr), \
215 .info = &vmstate_info_buffer, \
216 .flags = VMS_BUFFER, \
217 .offset = vmstate_offset_macaddr(_state, _field), \