blob: 523136c7acba67ce7fbbf4c8bb46d2eeb8b1116e [file] [log] [blame]
pbrook87ecb682007-11-17 17:14:51 +00001#ifndef QEMU_NET_H
2#define QEMU_NET_H
3
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01004#include "qemu/queue.h"
Markus Armbruster9af23982018-02-11 10:36:01 +01005#include "qapi/qapi-types-net.h"
Mark McLoughline1144d02009-10-23 17:52:16 +01006#include "net/queue.h"
Eduardo Habkostce35e222020-12-11 17:05:12 -05007#include "hw/qdev-properties-system.h"
aliguorifbe78f42008-12-17 19:13:11 +00008
Dmitry Fleytman6d1d4932016-06-01 11:23:37 +03009#define MAC_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
10#define MAC_ARG(x) ((uint8_t *)(x))[0], ((uint8_t *)(x))[1], \
11 ((uint8_t *)(x))[2], ((uint8_t *)(x))[3], \
12 ((uint8_t *)(x))[4], ((uint8_t *)(x))[5]
13
Jason Wang1ceef9f2013-01-30 19:12:28 +080014#define MAX_QUEUE_NUM 1024
15
Scott Feldmand32fcad2013-03-18 11:43:44 -070016/* Maximum GSO packet size (64k) plus plenty of room for
17 * the ethernet and virtio_net headers
18 */
19#define NET_BUFSIZE (4096 + 65536)
20
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +020021struct MACAddr {
22 uint8_t a[6];
23};
24
Gerd Hoffmanned16ab52009-10-21 15:25:26 +020025/* qdev nic properties */
26
Jason Wang1ceef9f2013-01-30 19:12:28 +080027typedef struct NICPeers {
28 NetClientState *ncs[MAX_QUEUE_NUM];
Jiri Pirko575a1c02014-05-26 12:04:08 +020029 int32_t queues;
Jason Wang1ceef9f2013-01-30 19:12:28 +080030} NICPeers;
31
Gerd Hoffmanned16ab52009-10-21 15:25:26 +020032typedef struct NICConf {
33 MACAddr macaddr;
Jason Wang1ceef9f2013-01-30 19:12:28 +080034 NICPeers peers;
Gleb Natapov1ca4d092010-12-08 13:35:05 +020035 int32_t bootindex;
Gerd Hoffmanned16ab52009-10-21 15:25:26 +020036} NICConf;
37
38#define DEFINE_NIC_PROPERTIES(_state, _conf) \
39 DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \
Gonglei45e8a9e2014-10-07 16:00:21 +080040 DEFINE_PROP_NETDEV("netdev", _state, _conf.peers)
Gerd Hoffmanned16ab52009-10-21 15:25:26 +020041
Jason Wang1ceef9f2013-01-30 19:12:28 +080042
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010043/* Net clients */
pbrook87ecb682007-11-17 17:14:51 +000044
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010045typedef void (NetPoll)(NetClientState *, bool enable);
Philippe Mathieu-Daudéb8c4b672020-03-05 18:56:49 +010046typedef bool (NetCanReceive)(NetClientState *);
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010047typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
48typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
49typedef void (NetCleanup) (NetClientState *);
50typedef void (LinkStatusChanged)(NetClientState *);
Jason Wangf7860452013-01-30 19:12:27 +080051typedef void (NetClientDestructor)(NetClientState *);
Amos Kongb1be4282013-06-14 15:45:52 +080052typedef RxFilterInfo *(QueryRxFilter)(NetClientState *);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +010053typedef bool (HasUfo)(NetClientState *);
54typedef bool (HasVnetHdr)(NetClientState *);
55typedef bool (HasVnetHdrLen)(NetClientState *, int);
56typedef void (UsingVnetHdr)(NetClientState *, bool);
57typedef void (SetOffload)(NetClientState *, int, int, int, int, int);
58typedef void (SetVnetHdrLen)(NetClientState *, int);
Greg Kurzc80cd6b2015-06-17 15:23:44 +020059typedef int (SetVnetLE)(NetClientState *, bool);
60typedef int (SetVnetBE)(NetClientState *, bool);
Zhang Chen16a3df42016-05-13 15:35:19 +080061typedef struct SocketReadState SocketReadState;
62typedef void (SocketReadStateFinalize)(SocketReadState *rs);
Dr. David Alan Gilbert44b416a2019-02-27 13:24:09 +000063typedef void (NetAnnounce)(NetClientState *);
Andrew Melnychenko8f364e32021-05-14 14:48:30 +030064typedef bool (SetSteeringEBPF)(NetClientState *, int);
Kevin Wolfe287bf72021-10-08 15:34:28 +020065typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **);
aliguori34b25ca2009-01-08 19:45:03 +000066
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000067typedef struct NetClientInfo {
Eric Blakef394b2e2016-07-13 21:50:23 -060068 NetClientDriver type;
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000069 size_t size;
70 NetReceive *receive;
71 NetReceive *receive_raw;
72 NetReceiveIOV *receive_iov;
73 NetCanReceive *can_receive;
74 NetCleanup *cleanup;
75 LinkStatusChanged *link_status_changed;
Amos Kongb1be4282013-06-14 15:45:52 +080076 QueryRxFilter *query_rx_filter;
Michael S. Tsirkinceb69612009-12-24 14:46:29 +020077 NetPoll *poll;
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +010078 HasUfo *has_ufo;
79 HasVnetHdr *has_vnet_hdr;
80 HasVnetHdrLen *has_vnet_hdr_len;
81 UsingVnetHdr *using_vnet_hdr;
82 SetOffload *set_offload;
83 SetVnetHdrLen *set_vnet_hdr_len;
Greg Kurzc80cd6b2015-06-17 15:23:44 +020084 SetVnetLE *set_vnet_le;
85 SetVnetBE *set_vnet_be;
Dr. David Alan Gilbert44b416a2019-02-27 13:24:09 +000086 NetAnnounce *announce;
Andrew Melnychenko8f364e32021-05-14 14:48:30 +030087 SetSteeringEBPF *set_steering_ebpf;
Kevin Wolfe287bf72021-10-08 15:34:28 +020088 NetCheckPeerType *check_peer_type;
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000089} NetClientInfo;
90
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010091struct NetClientState {
Mark McLoughlin665a3b02009-11-25 18:49:30 +000092 NetClientInfo *info;
aliguori436e5e52009-01-08 19:44:06 +000093 int link_down;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010094 QTAILQ_ENTRY(NetClientState) next;
95 NetClientState *peer;
Jan Kiszka067404b2013-08-02 21:47:08 +020096 NetQueue *incoming_queue;
aliguoribf38c1a2009-01-07 17:42:25 +000097 char *model;
aliguori676cff22009-01-07 17:43:44 +000098 char *name;
Jason Wang56e6f592021-04-02 11:03:33 +080099 char info_str[256];
Mark McLoughlin893379e2009-10-27 18:16:36 +0000100 unsigned receive_disabled : 1;
Jason Wangf7860452013-01-30 19:12:27 +0800101 NetClientDestructor *destructor;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800102 unsigned int queue_index;
Amos Kongb1be4282013-06-14 15:45:52 +0800103 unsigned rxfilter_notify_enabled:1;
Marc-André Lureaubfc6cf32016-06-06 18:45:06 +0200104 int vring_enable;
Zhang Chend6b732e2017-07-04 14:53:45 +0800105 int vnet_hdr_len;
Eric Blake08712fc2020-03-17 15:17:11 -0500106 bool is_netdev;
Bin Meng935344b2021-03-17 14:26:28 +0800107 bool do_not_pad; /* do not pad to the minimum ethernet frame length */
Jason Wang2f849db2021-10-20 12:55:55 +0800108 bool is_datapath;
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100109 QTAILQ_HEAD(, NetFilterState) filters;
pbrook87ecb682007-11-17 17:14:51 +0000110};
111
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000112typedef struct NICState {
Jason Wangf6b26cf2013-02-22 23:15:06 +0800113 NetClientState *ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000114 NICConf *conf;
115 void *opaque;
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200116 bool peer_deleted;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000117} NICState;
118
Zhang Chen16a3df42016-05-13 15:35:19 +0800119struct SocketReadState {
Zhang Chen3cde5ea2017-07-04 14:53:46 +0800120 /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
121 int state;
122 /* This flag decide whether to read the vnet_hdr_len field */
123 bool vnet_hdr;
Zhang Chen16a3df42016-05-13 15:35:19 +0800124 uint32_t index;
125 uint32_t packet_len;
Zhang Chen3cde5ea2017-07-04 14:53:46 +0800126 uint32_t vnet_hdr_len;
Zhang Chen16a3df42016-05-13 15:35:19 +0800127 uint8_t buf[NET_BUFSIZE];
128 SocketReadStateFinalize *finalize;
129};
130
131int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size);
Scott Feldman890ee6a2015-03-13 21:09:25 -0700132char *qemu_mac_strdup_printf(const uint8_t *macaddr);
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100133NetClientState *qemu_find_netdev(const char *id);
Jason Wang6c51ae72013-01-30 19:12:25 +0800134int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600135 NetClientDriver type, int max);
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100136NetClientState *qemu_new_net_client(NetClientInfo *info,
137 NetClientState *peer,
138 const char *model,
139 const char *name);
Jason Wang2f849db2021-10-20 12:55:55 +0800140NetClientState *qemu_new_net_control_client(NetClientInfo *info,
141 NetClientState *peer,
142 const char *model,
143 const char *name);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000144NICState *qemu_new_nic(NetClientInfo *info,
145 NICConf *conf,
146 const char *model,
147 const char *name,
148 void *opaque);
Jason Wang948ecf22013-01-30 19:12:24 +0800149void qemu_del_nic(NICState *nic);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800150NetClientState *qemu_get_subqueue(NICState *nic, int queue_index);
Jason Wangb356f762013-01-30 19:12:22 +0800151NetClientState *qemu_get_queue(NICState *nic);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800152NICState *qemu_get_nic(NetClientState *nc);
153void *qemu_get_nic_opaque(NetClientState *nc);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100154void qemu_del_net_client(NetClientState *nc);
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000155typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
156void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
Jason Wang705df542021-02-24 11:44:36 +0800157int qemu_can_receive_packet(NetClientState *nc);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100158int qemu_can_send_packet(NetClientState *nc);
159ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +0000160 int iovcnt);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100161ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100162 int iovcnt, NetPacketSent *sent_cb);
Marc-André Lureau625a5262019-01-17 15:43:54 +0400163ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
Jason Wang705df542021-02-24 11:44:36 +0800164ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size);
165ssize_t qemu_receive_packet_iov(NetClientState *nc,
166 const struct iovec *iov,
167 int iovcnt);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100168ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
169ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100170 int size, NetPacketSent *sent_cb);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100171void qemu_purge_queued_packets(NetClientState *nc);
172void qemu_flush_queued_packets(NetClientState *nc);
Greg Kurz94b52952018-03-20 11:44:56 +0100173void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100174void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100175bool qemu_has_ufo(NetClientState *nc);
176bool qemu_has_vnet_hdr(NetClientState *nc);
177bool qemu_has_vnet_hdr_len(NetClientState *nc, int len);
178void qemu_using_vnet_hdr(NetClientState *nc, bool enable);
179void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
180 int ecn, int ufo);
181void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200182int qemu_set_vnet_le(NetClientState *nc, bool is_le);
183int qemu_set_vnet_be(NetClientState *nc, bool is_be);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200184void qemu_macaddr_default_if_unset(MACAddr *macaddr);
Markus Armbruster07caea32009-09-25 03:53:51 +0200185int qemu_show_nic_models(const char *arg, const char *const *models);
aliguorid07f22c2009-01-13 19:03:57 +0000186void qemu_check_nic_model(NICInfo *nd, const char *model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200187int qemu_find_nic_model(NICInfo *nd, const char * const *models,
188 const char *default_model);
pbrook87ecb682007-11-17 17:14:51 +0000189
Zhi Yong Wu1a859592012-07-24 16:35:16 +0100190void print_net_client(Monitor *mon, NetClientState *nc);
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100191void hmp_info_network(Monitor *mon, const QDict *qdict);
Zhang Chen16a3df42016-05-13 15:35:19 +0800192void net_socket_rs_init(SocketReadState *rs,
Zhang Chen3cde5ea2017-07-04 14:53:46 +0800193 SocketReadStateFinalize *finalize,
194 bool vnet_hdr);
Cindy Lu0165daa2020-07-01 22:55:25 +0800195NetClientState *qemu_get_peer(NetClientState *nc, int queue_index);
pbrook87ecb682007-11-17 17:14:51 +0000196
197/* NIC info */
198
199#define MAX_NICS 8
200
201struct NICInfo {
Jan Kiszka6eed1852011-07-20 12:20:22 +0200202 MACAddr macaddr;
Mark McLoughlin9203f522009-10-06 12:16:53 +0100203 char *model;
204 char *name;
205 char *devaddr;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100206 NetClientState *netdev;
Peter Maydell48e2faf2011-05-20 16:50:01 +0100207 int used; /* is this slot in nd_table[] being used? */
208 int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
Michael S. Tsirkinffe63702009-06-21 19:51:18 +0300209 int nvectors;
pbrook87ecb682007-11-17 17:14:51 +0000210};
211
212extern int nb_nics;
213extern NICInfo nd_table[MAX_NICS];
Hani Benhabiles84007e82014-05-27 23:39:33 +0100214extern const char *host_net_devices[];
pbrook87ecb682007-11-17 17:14:51 +0000215
aliguori63a01ef2008-10-31 19:10:00 +0000216/* from net.c */
Mark McLoughlin7f161aa2009-10-08 19:58:25 +0100217int net_client_parse(QemuOptsList *opts_list, const char *str);
Paolo Bonziniad6f9322020-11-11 05:52:22 -0500218void show_netdevs(void);
Thomas Huth34f708b2018-02-21 11:18:30 +0100219int net_init_clients(Error **errp);
Markus Armbruster668680f2010-02-11 14:44:58 +0100220void net_check_clients(void);
aliguori63a01ef2008-10-31 19:10:00 +0000221void net_cleanup(void);
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100222void hmp_host_net_add(Monitor *mon, const QDict *qdict);
223void hmp_host_net_remove(Monitor *mon, const QDict *qdict);
Luiz Capitulino928059a2012-04-18 17:34:15 -0300224void netdev_add(QemuOpts *opts, Error **errp);
aliguori63a01ef2008-10-31 19:10:00 +0000225
Paolo Bonzini1422e322012-10-24 08:43:34 +0200226int net_hub_id_for_client(NetClientState *nc, int *id);
227NetClientState *net_hub_port_find(int hub_id);
228
Paolo Bonzini63c4db42020-08-18 11:56:16 +0200229#define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup"
230#define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown"
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500231#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
232#define DEFAULT_BRIDGE_INTERFACE "br0"
aurel32f54825c2008-12-18 22:43:48 +0000233
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200234void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
Paul Brook9d07d752009-05-14 22:35:07 +0100235
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +0000236#define POLYNOMIAL_BE 0x04c11db6
Mark Cave-Aylandf1a7deb2017-12-15 18:41:44 +0000237#define POLYNOMIAL_LE 0xedb88320
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +0000238uint32_t net_crc32(const uint8_t *p, int len);
Mark Cave-Aylandf1a7deb2017-12-15 18:41:44 +0000239uint32_t net_crc32_le(const uint8_t *p, int len);
Jason Wang7fc8d912012-03-05 11:08:50 +0800240
Paolo Bonzini701a8f72012-01-13 17:07:20 +0100241#define vmstate_offset_macaddr(_state, _field) \
242 vmstate_offset_array(_state, _field.a, uint8_t, \
243 sizeof(typeof_field(_state, _field)))
244
245#define VMSTATE_MACADDR(_field, _state) { \
246 .name = (stringify(_field)), \
247 .size = sizeof(MACAddr), \
248 .info = &vmstate_info_buffer, \
249 .flags = VMS_BUFFER, \
250 .offset = vmstate_offset_macaddr(_state, _field), \
251}
252
Jason Wangbc38e312021-04-23 11:18:03 +0800253static inline bool net_peer_needs_padding(NetClientState *nc)
254{
255 return nc->peer && !nc->peer->do_not_pad;
256}
257
pbrook87ecb682007-11-17 17:14:51 +0000258#endif