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