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