xref: /openbmc/qemu/include/net/net.h (revision 9050f976)
11422e32dSPaolo Bonzini #ifndef QEMU_NET_H
21422e32dSPaolo Bonzini #define QEMU_NET_H
31422e32dSPaolo Bonzini 
41de7afc9SPaolo Bonzini #include "qemu/queue.h"
59af23989SMarkus Armbruster #include "qapi/qapi-types-net.h"
61422e32dSPaolo Bonzini #include "net/queue.h"
7ce35e229SEduardo Habkost #include "hw/qdev-properties-system.h"
81422e32dSPaolo Bonzini 
96d1d4939SDmitry Fleytman #define MAC_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
106d1d4939SDmitry Fleytman #define MAC_ARG(x) ((uint8_t *)(x))[0], ((uint8_t *)(x))[1], \
116d1d4939SDmitry Fleytman                    ((uint8_t *)(x))[2], ((uint8_t *)(x))[3], \
126d1d4939SDmitry Fleytman                    ((uint8_t *)(x))[4], ((uint8_t *)(x))[5]
136d1d4939SDmitry Fleytman 
141ceef9f2SJason Wang #define MAX_QUEUE_NUM 1024
151ceef9f2SJason Wang 
16d32fcad3SScott Feldman /* Maximum GSO packet size (64k) plus plenty of room for
17d32fcad3SScott Feldman  * the ethernet and virtio_net headers
18d32fcad3SScott Feldman  */
19d32fcad3SScott Feldman #define NET_BUFSIZE (4096 + 65536)
20d32fcad3SScott Feldman 
211422e32dSPaolo Bonzini struct MACAddr {
221422e32dSPaolo Bonzini     uint8_t a[6];
231422e32dSPaolo Bonzini };
241422e32dSPaolo Bonzini 
251422e32dSPaolo Bonzini /* qdev nic properties */
261422e32dSPaolo Bonzini 
271ceef9f2SJason Wang typedef struct NICPeers {
281ceef9f2SJason Wang     NetClientState *ncs[MAX_QUEUE_NUM];
29575a1c0eSJiri Pirko     int32_t queues;
301ceef9f2SJason Wang } NICPeers;
311ceef9f2SJason Wang 
321422e32dSPaolo Bonzini typedef struct NICConf {
331422e32dSPaolo Bonzini     MACAddr macaddr;
341ceef9f2SJason Wang     NICPeers peers;
351422e32dSPaolo Bonzini     int32_t bootindex;
361422e32dSPaolo Bonzini } NICConf;
371422e32dSPaolo Bonzini 
381422e32dSPaolo Bonzini #define DEFINE_NIC_PROPERTIES(_state, _conf)                            \
391422e32dSPaolo Bonzini     DEFINE_PROP_MACADDR("mac",   _state, _conf.macaddr),                \
4045e8a9e1SGonglei     DEFINE_PROP_NETDEV("netdev", _state, _conf.peers)
411422e32dSPaolo Bonzini 
421ceef9f2SJason Wang 
431422e32dSPaolo Bonzini /* Net clients */
441422e32dSPaolo Bonzini 
451422e32dSPaolo Bonzini typedef void (NetPoll)(NetClientState *, bool enable);
46b8c4b67eSPhilippe Mathieu-Daudé typedef bool (NetCanReceive)(NetClientState *);
47eb92b753SEugenio Pérez typedef int (NetStart)(NetClientState *);
48539573c3SEugenio Pérez typedef int (NetLoad)(NetClientState *);
49c5e5269dSEugenio Pérez typedef void (NetStop)(NetClientState *);
501422e32dSPaolo Bonzini typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
511422e32dSPaolo Bonzini typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
521422e32dSPaolo Bonzini typedef void (NetCleanup) (NetClientState *);
531422e32dSPaolo Bonzini typedef void (LinkStatusChanged)(NetClientState *);
54f7860455SJason Wang typedef void (NetClientDestructor)(NetClientState *);
55b1be4280SAmos Kong typedef RxFilterInfo *(QueryRxFilter)(NetClientState *);
561f55ac45SVincenzo Maffione typedef bool (HasUfo)(NetClientState *);
57f03e0cf6SYuri Benditovich typedef bool (HasUso)(NetClientState *);
581f55ac45SVincenzo Maffione typedef bool (HasVnetHdr)(NetClientState *);
591f55ac45SVincenzo Maffione typedef bool (HasVnetHdrLen)(NetClientState *, int);
60481c5232SAkihiko Odaki typedef bool (GetUsingVnetHdr)(NetClientState *);
611f55ac45SVincenzo Maffione typedef void (UsingVnetHdr)(NetClientState *, bool);
622ab0ec31SAndrew Melnychenko typedef void (SetOffload)(NetClientState *, int, int, int, int, int, int, int);
63481c5232SAkihiko Odaki typedef int (GetVnetHdrLen)(NetClientState *);
641f55ac45SVincenzo Maffione typedef void (SetVnetHdrLen)(NetClientState *, int);
65c80cd6bbSGreg Kurz typedef int (SetVnetLE)(NetClientState *, bool);
66c80cd6bbSGreg Kurz typedef int (SetVnetBE)(NetClientState *, bool);
6716a3df40SZhang Chen typedef struct SocketReadState SocketReadState;
6816a3df40SZhang Chen typedef void (SocketReadStateFinalize)(SocketReadState *rs);
6944b416adSDr. David Alan Gilbert typedef void (NetAnnounce)(NetClientState *);
708f364e34SAndrew Melnychenko typedef bool (SetSteeringEBPF)(NetClientState *, int);
71e287bf7bSKevin Wolf typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **);
721422e32dSPaolo Bonzini 
731422e32dSPaolo Bonzini typedef struct NetClientInfo {
74f394b2e2SEric Blake     NetClientDriver type;
751422e32dSPaolo Bonzini     size_t size;
761422e32dSPaolo Bonzini     NetReceive *receive;
771422e32dSPaolo Bonzini     NetReceive *receive_raw;
781422e32dSPaolo Bonzini     NetReceiveIOV *receive_iov;
791422e32dSPaolo Bonzini     NetCanReceive *can_receive;
80eb92b753SEugenio Pérez     NetStart *start;
81539573c3SEugenio Pérez     NetLoad *load;
82c5e5269dSEugenio Pérez     NetStop *stop;
831422e32dSPaolo Bonzini     NetCleanup *cleanup;
841422e32dSPaolo Bonzini     LinkStatusChanged *link_status_changed;
85b1be4280SAmos Kong     QueryRxFilter *query_rx_filter;
861422e32dSPaolo Bonzini     NetPoll *poll;
871f55ac45SVincenzo Maffione     HasUfo *has_ufo;
88f03e0cf6SYuri Benditovich     HasUso *has_uso;
891f55ac45SVincenzo Maffione     HasVnetHdr *has_vnet_hdr;
901f55ac45SVincenzo Maffione     HasVnetHdrLen *has_vnet_hdr_len;
91481c5232SAkihiko Odaki     GetUsingVnetHdr *get_using_vnet_hdr;
921f55ac45SVincenzo Maffione     UsingVnetHdr *using_vnet_hdr;
931f55ac45SVincenzo Maffione     SetOffload *set_offload;
94481c5232SAkihiko Odaki     GetVnetHdrLen *get_vnet_hdr_len;
951f55ac45SVincenzo Maffione     SetVnetHdrLen *set_vnet_hdr_len;
96c80cd6bbSGreg Kurz     SetVnetLE *set_vnet_le;
97c80cd6bbSGreg Kurz     SetVnetBE *set_vnet_be;
9844b416adSDr. David Alan Gilbert     NetAnnounce *announce;
998f364e34SAndrew Melnychenko     SetSteeringEBPF *set_steering_ebpf;
100e287bf7bSKevin Wolf     NetCheckPeerType *check_peer_type;
1011422e32dSPaolo Bonzini } NetClientInfo;
1021422e32dSPaolo Bonzini 
1031422e32dSPaolo Bonzini struct NetClientState {
1041422e32dSPaolo Bonzini     NetClientInfo *info;
1051422e32dSPaolo Bonzini     int link_down;
1061422e32dSPaolo Bonzini     QTAILQ_ENTRY(NetClientState) next;
1071422e32dSPaolo Bonzini     NetClientState *peer;
108067404beSJan Kiszka     NetQueue *incoming_queue;
1091422e32dSPaolo Bonzini     char *model;
1101422e32dSPaolo Bonzini     char *name;
11156e6f594SJason Wang     char info_str[256];
1121422e32dSPaolo Bonzini     unsigned receive_disabled : 1;
113f7860455SJason Wang     NetClientDestructor *destructor;
1141ceef9f2SJason Wang     unsigned int queue_index;
115b1be4280SAmos Kong     unsigned rxfilter_notify_enabled:1;
116bfc6cf31SMarc-André Lureau     int vring_enable;
117d6b732e9SZhang Chen     int vnet_hdr_len;
11808712fcbSEric Blake     bool is_netdev;
119935344beSBin Meng     bool do_not_pad; /* do not pad to the minimum ethernet frame length */
1202f849dbdSJason Wang     bool is_datapath;
121eae3eb3eSPaolo Bonzini     QTAILQ_HEAD(, NetFilterState) filters;
1221422e32dSPaolo Bonzini };
1231422e32dSPaolo Bonzini 
124ae71d13dSMarkus Armbruster typedef QTAILQ_HEAD(NetClientStateList, NetClientState) NetClientStateList;
125ae71d13dSMarkus Armbruster 
1261422e32dSPaolo Bonzini typedef struct NICState {
127f6b26cf2SJason Wang     NetClientState *ncs;
1281422e32dSPaolo Bonzini     NICConf *conf;
129*9050f976SAkihiko Odaki     MemReentrancyGuard *reentrancy_guard;
1301422e32dSPaolo Bonzini     void *opaque;
1311422e32dSPaolo Bonzini     bool peer_deleted;
1321422e32dSPaolo Bonzini } NICState;
1331422e32dSPaolo Bonzini 
13416a3df40SZhang Chen struct SocketReadState {
1353cde5ea2SZhang Chen     /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
1363cde5ea2SZhang Chen     int state;
1373cde5ea2SZhang Chen     /* This flag decide whether to read the vnet_hdr_len field */
1383cde5ea2SZhang Chen     bool vnet_hdr;
13916a3df40SZhang Chen     uint32_t index;
14016a3df40SZhang Chen     uint32_t packet_len;
1413cde5ea2SZhang Chen     uint32_t vnet_hdr_len;
14216a3df40SZhang Chen     uint8_t buf[NET_BUFSIZE];
14316a3df40SZhang Chen     SocketReadStateFinalize *finalize;
14416a3df40SZhang Chen };
14516a3df40SZhang Chen 
14616a3df40SZhang Chen int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size);
147890ee6abSScott Feldman char *qemu_mac_strdup_printf(const uint8_t *macaddr);
1481422e32dSPaolo Bonzini NetClientState *qemu_find_netdev(const char *id);
1496c51ae73SJason Wang int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
150f394b2e2SEric Blake                                  NetClientDriver type, int max);
1511422e32dSPaolo Bonzini NetClientState *qemu_new_net_client(NetClientInfo *info,
1521422e32dSPaolo Bonzini                                     NetClientState *peer,
1531422e32dSPaolo Bonzini                                     const char *model,
1541422e32dSPaolo Bonzini                                     const char *name);
1552f849dbdSJason Wang NetClientState *qemu_new_net_control_client(NetClientInfo *info,
1562f849dbdSJason Wang                                         NetClientState *peer,
1572f849dbdSJason Wang                                         const char *model,
1582f849dbdSJason Wang                                         const char *name);
1591422e32dSPaolo Bonzini NICState *qemu_new_nic(NetClientInfo *info,
1601422e32dSPaolo Bonzini                        NICConf *conf,
1611422e32dSPaolo Bonzini                        const char *model,
1621422e32dSPaolo Bonzini                        const char *name,
1637d0fefdfSAkihiko Odaki                        MemReentrancyGuard *reentrancy_guard,
1641422e32dSPaolo Bonzini                        void *opaque);
165948ecf21SJason Wang void qemu_del_nic(NICState *nic);
1661ceef9f2SJason Wang NetClientState *qemu_get_subqueue(NICState *nic, int queue_index);
167b356f76dSJason Wang NetClientState *qemu_get_queue(NICState *nic);
168cc1f0f45SJason Wang NICState *qemu_get_nic(NetClientState *nc);
169cc1f0f45SJason Wang void *qemu_get_nic_opaque(NetClientState *nc);
1701422e32dSPaolo Bonzini void qemu_del_net_client(NetClientState *nc);
1711422e32dSPaolo Bonzini typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
1721422e32dSPaolo Bonzini void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
173705df546SJason Wang int qemu_can_receive_packet(NetClientState *nc);
1741422e32dSPaolo Bonzini int qemu_can_send_packet(NetClientState *nc);
1751422e32dSPaolo Bonzini ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
1761422e32dSPaolo Bonzini                           int iovcnt);
1771422e32dSPaolo Bonzini ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
1781422e32dSPaolo Bonzini                                 int iovcnt, NetPacketSent *sent_cb);
179625a526bSMarc-André Lureau ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
180705df546SJason Wang ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size);
181705df546SJason Wang ssize_t qemu_receive_packet_iov(NetClientState *nc,
182705df546SJason Wang                                 const struct iovec *iov,
183705df546SJason Wang                                 int iovcnt);
1841422e32dSPaolo Bonzini ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
1851422e32dSPaolo Bonzini ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
1861422e32dSPaolo Bonzini                                int size, NetPacketSent *sent_cb);
1871422e32dSPaolo Bonzini void qemu_purge_queued_packets(NetClientState *nc);
1881422e32dSPaolo Bonzini void qemu_flush_queued_packets(NetClientState *nc);
18994b52958SGreg Kurz void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge);
190ac149498SStefan Weil via void qemu_set_info_str(NetClientState *nc,
191ac149498SStefan Weil via                        const char *fmt, ...) G_GNUC_PRINTF(2, 3);
1921422e32dSPaolo Bonzini void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
193d6085e3aSStefan Hajnoczi bool qemu_has_ufo(NetClientState *nc);
194f03e0cf6SYuri Benditovich bool qemu_has_uso(NetClientState *nc);
195d6085e3aSStefan Hajnoczi bool qemu_has_vnet_hdr(NetClientState *nc);
196d6085e3aSStefan Hajnoczi bool qemu_has_vnet_hdr_len(NetClientState *nc, int len);
197481c5232SAkihiko Odaki bool qemu_get_using_vnet_hdr(NetClientState *nc);
198d6085e3aSStefan Hajnoczi void qemu_using_vnet_hdr(NetClientState *nc, bool enable);
199d6085e3aSStefan Hajnoczi void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
2002ab0ec31SAndrew Melnychenko                       int ecn, int ufo, int uso4, int uso6);
201481c5232SAkihiko Odaki int qemu_get_vnet_hdr_len(NetClientState *nc);
202d6085e3aSStefan Hajnoczi void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
203c80cd6bbSGreg Kurz int qemu_set_vnet_le(NetClientState *nc, bool is_le);
204c80cd6bbSGreg Kurz int qemu_set_vnet_be(NetClientState *nc, bool is_be);
2051422e32dSPaolo Bonzini void qemu_macaddr_default_if_unset(MACAddr *macaddr);
2061422e32dSPaolo Bonzini int qemu_show_nic_models(const char *arg, const char *const *models);
2071422e32dSPaolo Bonzini void qemu_check_nic_model(NICInfo *nd, const char *model);
2081422e32dSPaolo Bonzini int qemu_find_nic_model(NICInfo *nd, const char * const *models,
2091422e32dSPaolo Bonzini                         const char *default_model);
2101422e32dSPaolo Bonzini 
2111422e32dSPaolo Bonzini void print_net_client(Monitor *mon, NetClientState *nc);
21216a3df40SZhang Chen void net_socket_rs_init(SocketReadState *rs,
2133cde5ea2SZhang Chen                         SocketReadStateFinalize *finalize,
2143cde5ea2SZhang Chen                         bool vnet_hdr);
2150165daaeSCindy Lu NetClientState *qemu_get_peer(NetClientState *nc, int queue_index);
2161422e32dSPaolo Bonzini 
217c6941b3bSThomas Huth /**
218c6941b3bSThomas Huth  * qemu_get_nic_models:
219c6941b3bSThomas Huth  * @device_type: Defines which devices should be taken into consideration
220c6941b3bSThomas Huth  *               (e.g. TYPE_DEVICE for all devices, or TYPE_PCI_DEVICE for PCI)
221c6941b3bSThomas Huth  *
222c6941b3bSThomas Huth  * Get an array of pointers to names of NIC devices that are available in
223c6941b3bSThomas Huth  * the QEMU binary. The array is terminated with a NULL pointer entry.
224c6941b3bSThomas Huth  * The caller is responsible for freeing the memory when it is not required
225c6941b3bSThomas Huth  * anymore, e.g. with g_ptr_array_free(..., true).
226c6941b3bSThomas Huth  *
227c6941b3bSThomas Huth  * Returns: Pointer to the array that contains the pointers to the names.
228c6941b3bSThomas Huth  */
229c6941b3bSThomas Huth GPtrArray *qemu_get_nic_models(const char *device_type);
230c6941b3bSThomas Huth 
2311422e32dSPaolo Bonzini /* NIC info */
2321422e32dSPaolo Bonzini 
2331422e32dSPaolo Bonzini #define MAX_NICS 8
2341422e32dSPaolo Bonzini 
2351422e32dSPaolo Bonzini struct NICInfo {
2361422e32dSPaolo Bonzini     MACAddr macaddr;
2371422e32dSPaolo Bonzini     char *model;
2381422e32dSPaolo Bonzini     char *name;
2391422e32dSPaolo Bonzini     char *devaddr;
2401422e32dSPaolo Bonzini     NetClientState *netdev;
2411422e32dSPaolo Bonzini     int used;         /* is this slot in nd_table[] being used? */
2421422e32dSPaolo Bonzini     int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
2431422e32dSPaolo Bonzini     int nvectors;
2441422e32dSPaolo Bonzini };
2451422e32dSPaolo Bonzini 
2461422e32dSPaolo Bonzini extern int nb_nics;
2471422e32dSPaolo Bonzini extern NICInfo nd_table[MAX_NICS];
24884007e81SHani Benhabiles extern const char *host_net_devices[];
2491422e32dSPaolo Bonzini 
2501422e32dSPaolo Bonzini /* from net.c */
251ae71d13dSMarkus Armbruster extern NetClientStateList net_clients;
25273071f19SPhilippe Mathieu-Daudé bool netdev_is_modern(const char *optstr);
25373071f19SPhilippe Mathieu-Daudé void netdev_parse_modern(const char *optstr);
25473071f19SPhilippe Mathieu-Daudé void net_client_parse(QemuOptsList *opts_list, const char *optstr);
255ad6f932fSPaolo Bonzini void show_netdevs(void);
256d63ef17bSLaurent Vivier void net_init_clients(void);
2571422e32dSPaolo Bonzini void net_check_clients(void);
2581422e32dSPaolo Bonzini void net_cleanup(void);
2593e5a50d6SMarkus Armbruster void hmp_host_net_add(Monitor *mon, const QDict *qdict);
2603e5a50d6SMarkus Armbruster void hmp_host_net_remove(Monitor *mon, const QDict *qdict);
2611422e32dSPaolo Bonzini void netdev_add(QemuOpts *opts, Error **errp);
2621422e32dSPaolo Bonzini 
2631422e32dSPaolo Bonzini int net_hub_id_for_client(NetClientState *nc, int *id);
2641422e32dSPaolo Bonzini NetClientState *net_hub_port_find(int hub_id);
2651422e32dSPaolo Bonzini 
26663c4db4cSPaolo Bonzini #define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup"
26763c4db4cSPaolo Bonzini #define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown"
2681422e32dSPaolo Bonzini #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
2691422e32dSPaolo Bonzini #define DEFAULT_BRIDGE_INTERFACE "br0"
2701422e32dSPaolo Bonzini 
2711422e32dSPaolo Bonzini void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
2721422e32dSPaolo Bonzini 
273eaba8f34SMark Cave-Ayland #define POLYNOMIAL_BE 0x04c11db6
274f1a7deb9SMark Cave-Ayland #define POLYNOMIAL_LE 0xedb88320
275eaba8f34SMark Cave-Ayland uint32_t net_crc32(const uint8_t *p, int len);
276f1a7deb9SMark Cave-Ayland uint32_t net_crc32_le(const uint8_t *p, int len);
2771422e32dSPaolo Bonzini 
2781422e32dSPaolo Bonzini #define vmstate_offset_macaddr(_state, _field)                       \
2791422e32dSPaolo Bonzini     vmstate_offset_array(_state, _field.a, uint8_t,                \
2801422e32dSPaolo Bonzini                          sizeof(typeof_field(_state, _field)))
2811422e32dSPaolo Bonzini 
2821422e32dSPaolo Bonzini #define VMSTATE_MACADDR(_field, _state) {                            \
2831422e32dSPaolo Bonzini     .name       = (stringify(_field)),                               \
2841422e32dSPaolo Bonzini     .size       = sizeof(MACAddr),                                   \
2851422e32dSPaolo Bonzini     .info       = &vmstate_info_buffer,                              \
2861422e32dSPaolo Bonzini     .flags      = VMS_BUFFER,                                        \
2871422e32dSPaolo Bonzini     .offset     = vmstate_offset_macaddr(_state, _field),            \
2881422e32dSPaolo Bonzini }
2891422e32dSPaolo Bonzini 
net_peer_needs_padding(NetClientState * nc)290bc38e31bSJason Wang static inline bool net_peer_needs_padding(NetClientState *nc)
291bc38e31bSJason Wang {
292bc38e31bSJason Wang   return nc->peer && !nc->peer->do_not_pad;
293bc38e31bSJason Wang }
294bc38e31bSJason Wang 
2951422e32dSPaolo Bonzini #endif
296