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