xref: /openbmc/qemu/include/net/net.h (revision 0b7e89b1)
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 
59 typedef struct NetClientInfo {
60     NetClientOptionsKind type;
61     size_t size;
62     NetReceive *receive;
63     NetReceive *receive_raw;
64     NetReceiveIOV *receive_iov;
65     NetCanReceive *can_receive;
66     NetCleanup *cleanup;
67     LinkStatusChanged *link_status_changed;
68     QueryRxFilter *query_rx_filter;
69     NetPoll *poll;
70     HasUfo *has_ufo;
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;
76 } NetClientInfo;
77 
78 struct NetClientState {
79     NetClientInfo *info;
80     int link_down;
81     QTAILQ_ENTRY(NetClientState) next;
82     NetClientState *peer;
83     NetQueue *incoming_queue;
84     char *model;
85     char *name;
86     char info_str[256];
87     unsigned receive_disabled : 1;
88     NetClientDestructor *destructor;
89     unsigned int queue_index;
90     unsigned rxfilter_notify_enabled:1;
91 };
92 
93 typedef struct NICState {
94     NetClientState *ncs;
95     NICConf *conf;
96     void *opaque;
97     bool peer_deleted;
98 } NICState;
99 
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,
105                                     const char *model,
106                                     const char *name);
107 NICState *qemu_new_nic(NetClientInfo *info,
108                        NICConf *conf,
109                        const char *model,
110                        const char *name,
111                        void *opaque);
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,
124                           int iovcnt);
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,
139                       int ecn, int ufo);
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);
146 
147 ssize_t qemu_deliver_packet(NetClientState *sender,
148                             unsigned flags,
149                             const uint8_t *data,
150                             size_t size,
151                             void *opaque);
152 ssize_t qemu_deliver_packet_iov(NetClientState *sender,
153                             unsigned flags,
154                             const struct iovec *iov,
155                             int iovcnt,
156                             void *opaque);
157 
158 void print_net_client(Monitor *mon, NetClientState *nc);
159 void hmp_info_network(Monitor *mon, const QDict *qdict);
160 
161 /* NIC info */
162 
163 #define MAX_NICS 8
164 
165 struct NICInfo {
166     MACAddr macaddr;
167     char *model;
168     char *name;
169     char *devaddr;
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? */
173     int nvectors;
174 };
175 
176 extern int nb_nics;
177 extern NICInfo nd_table[MAX_NICS];
178 extern int default_net;
179 extern const char *host_net_devices[];
180 
181 /* from net.c */
182 extern const char *legacy_tftp_prefix;
183 extern const char *legacy_bootp_filename;
184 
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);
194 
195 int net_hub_id_for_client(NetClientState *nc, int *id);
196 NetClientState *net_hub_port_find(int hub_id);
197 
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"
202 
203 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
204 
205 #define POLYNOMIAL 0x04c11db6
206 unsigned compute_mcast_idx(const uint8_t *ep);
207 
208 #define vmstate_offset_macaddr(_state, _field)                       \
209     vmstate_offset_array(_state, _field.a, uint8_t,                \
210                          sizeof(typeof_field(_state, _field)))
211 
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),            \
218 }
219 
220 #endif
221