blob: a943e968a3dc4a0618cb772e9513f4959439b64c [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"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +01007#include "migration/vmstate.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), \
Jason Wang1ceef9f2013-01-30 19:12:28 +080040 DEFINE_PROP_VLAN("vlan", _state, _conf.peers), \
Gonglei45e8a9e2014-10-07 16:00:21 +080041 DEFINE_PROP_NETDEV("netdev", _state, _conf.peers)
Gerd Hoffmanned16ab52009-10-21 15:25:26 +020042
Jason Wang1ceef9f2013-01-30 19:12:28 +080043
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010044/* Net clients */
pbrook87ecb682007-11-17 17:14:51 +000045
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010046typedef void (NetPoll)(NetClientState *, bool enable);
47typedef int (NetCanReceive)(NetClientState *);
48typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
49typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
50typedef void (NetCleanup) (NetClientState *);
51typedef void (LinkStatusChanged)(NetClientState *);
Jason Wangf7860452013-01-30 19:12:27 +080052typedef void (NetClientDestructor)(NetClientState *);
Amos Kongb1be4282013-06-14 15:45:52 +080053typedef RxFilterInfo *(QueryRxFilter)(NetClientState *);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +010054typedef bool (HasUfo)(NetClientState *);
55typedef bool (HasVnetHdr)(NetClientState *);
56typedef bool (HasVnetHdrLen)(NetClientState *, int);
57typedef void (UsingVnetHdr)(NetClientState *, bool);
58typedef void (SetOffload)(NetClientState *, int, int, int, int, int);
59typedef void (SetVnetHdrLen)(NetClientState *, int);
Greg Kurzc80cd6b2015-06-17 15:23:44 +020060typedef int (SetVnetLE)(NetClientState *, bool);
61typedef int (SetVnetBE)(NetClientState *, bool);
Zhang Chen16a3df42016-05-13 15:35:19 +080062typedef struct SocketReadState SocketReadState;
63typedef void (SocketReadStateFinalize)(SocketReadState *rs);
aliguori34b25ca2009-01-08 19:45:03 +000064
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000065typedef struct NetClientInfo {
Eric Blakef394b2e2016-07-13 21:50:23 -060066 NetClientDriver type;
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000067 size_t size;
68 NetReceive *receive;
69 NetReceive *receive_raw;
70 NetReceiveIOV *receive_iov;
71 NetCanReceive *can_receive;
72 NetCleanup *cleanup;
73 LinkStatusChanged *link_status_changed;
Amos Kongb1be4282013-06-14 15:45:52 +080074 QueryRxFilter *query_rx_filter;
Michael S. Tsirkinceb69612009-12-24 14:46:29 +020075 NetPoll *poll;
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +010076 HasUfo *has_ufo;
77 HasVnetHdr *has_vnet_hdr;
78 HasVnetHdrLen *has_vnet_hdr_len;
79 UsingVnetHdr *using_vnet_hdr;
80 SetOffload *set_offload;
81 SetVnetHdrLen *set_vnet_hdr_len;
Greg Kurzc80cd6b2015-06-17 15:23:44 +020082 SetVnetLE *set_vnet_le;
83 SetVnetBE *set_vnet_be;
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000084} NetClientInfo;
85
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010086struct NetClientState {
Mark McLoughlin665a3b02009-11-25 18:49:30 +000087 NetClientInfo *info;
aliguori436e5e52009-01-08 19:44:06 +000088 int link_down;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010089 QTAILQ_ENTRY(NetClientState) next;
90 NetClientState *peer;
Jan Kiszka067404b2013-08-02 21:47:08 +020091 NetQueue *incoming_queue;
aliguoribf38c1a2009-01-07 17:42:25 +000092 char *model;
aliguori676cff22009-01-07 17:43:44 +000093 char *name;
pbrook87ecb682007-11-17 17:14:51 +000094 char info_str[256];
Mark McLoughlin893379e2009-10-27 18:16:36 +000095 unsigned receive_disabled : 1;
Jason Wangf7860452013-01-30 19:12:27 +080096 NetClientDestructor *destructor;
Jason Wang1ceef9f2013-01-30 19:12:28 +080097 unsigned int queue_index;
Amos Kongb1be4282013-06-14 15:45:52 +080098 unsigned rxfilter_notify_enabled:1;
Marc-André Lureaubfc6cf32016-06-06 18:45:06 +020099 int vring_enable;
Zhang Chend6b732e2017-07-04 14:53:45 +0800100 int vnet_hdr_len;
Li Zhijian25aaadf2016-01-26 13:00:22 +0800101 QTAILQ_HEAD(NetFilterHead, NetFilterState) filters;
pbrook87ecb682007-11-17 17:14:51 +0000102};
103
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000104typedef struct NICState {
Jason Wangf6b26cf2013-02-22 23:15:06 +0800105 NetClientState *ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000106 NICConf *conf;
107 void *opaque;
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200108 bool peer_deleted;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000109} NICState;
110
Zhang Chen16a3df42016-05-13 15:35:19 +0800111struct SocketReadState {
Zhang Chen3cde5ea2017-07-04 14:53:46 +0800112 /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */
113 int state;
114 /* This flag decide whether to read the vnet_hdr_len field */
115 bool vnet_hdr;
Zhang Chen16a3df42016-05-13 15:35:19 +0800116 uint32_t index;
117 uint32_t packet_len;
Zhang Chen3cde5ea2017-07-04 14:53:46 +0800118 uint32_t vnet_hdr_len;
Zhang Chen16a3df42016-05-13 15:35:19 +0800119 uint8_t buf[NET_BUFSIZE];
120 SocketReadStateFinalize *finalize;
121};
122
123int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size);
Scott Feldman890ee6a2015-03-13 21:09:25 -0700124char *qemu_mac_strdup_printf(const uint8_t *macaddr);
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100125NetClientState *qemu_find_netdev(const char *id);
Jason Wang6c51ae72013-01-30 19:12:25 +0800126int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600127 NetClientDriver type, int max);
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100128NetClientState *qemu_new_net_client(NetClientInfo *info,
129 NetClientState *peer,
130 const char *model,
131 const char *name);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000132NICState *qemu_new_nic(NetClientInfo *info,
133 NICConf *conf,
134 const char *model,
135 const char *name,
136 void *opaque);
Jason Wang948ecf22013-01-30 19:12:24 +0800137void qemu_del_nic(NICState *nic);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800138NetClientState *qemu_get_subqueue(NICState *nic, int queue_index);
Jason Wangb356f762013-01-30 19:12:22 +0800139NetClientState *qemu_get_queue(NICState *nic);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800140NICState *qemu_get_nic(NetClientState *nc);
141void *qemu_get_nic_opaque(NetClientState *nc);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100142void qemu_del_net_client(NetClientState *nc);
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000143typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
144void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100145int qemu_can_send_packet(NetClientState *nc);
146ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +0000147 int iovcnt);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100148ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100149 int iovcnt, NetPacketSent *sent_cb);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100150void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
151ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
152ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100153 int size, NetPacketSent *sent_cb);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100154void qemu_purge_queued_packets(NetClientState *nc);
155void qemu_flush_queued_packets(NetClientState *nc);
156void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100157bool qemu_has_ufo(NetClientState *nc);
158bool qemu_has_vnet_hdr(NetClientState *nc);
159bool qemu_has_vnet_hdr_len(NetClientState *nc, int len);
160void qemu_using_vnet_hdr(NetClientState *nc, bool enable);
161void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
162 int ecn, int ufo);
163void qemu_set_vnet_hdr_len(NetClientState *nc, int len);
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200164int qemu_set_vnet_le(NetClientState *nc, bool is_le);
165int qemu_set_vnet_be(NetClientState *nc, bool is_be);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200166void qemu_macaddr_default_if_unset(MACAddr *macaddr);
Markus Armbruster07caea32009-09-25 03:53:51 +0200167int qemu_show_nic_models(const char *arg, const char *const *models);
aliguorid07f22c2009-01-13 19:03:57 +0000168void qemu_check_nic_model(NICInfo *nd, const char *model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200169int qemu_find_nic_model(NICInfo *nd, const char * const *models,
170 const char *default_model);
pbrook87ecb682007-11-17 17:14:51 +0000171
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100172ssize_t qemu_deliver_packet_iov(NetClientState *sender,
173 unsigned flags,
174 const struct iovec *iov,
175 int iovcnt,
176 void *opaque);
177
Zhi Yong Wu1a859592012-07-24 16:35:16 +0100178void print_net_client(Monitor *mon, NetClientState *nc);
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100179void hmp_info_network(Monitor *mon, const QDict *qdict);
Zhang Chen16a3df42016-05-13 15:35:19 +0800180void net_socket_rs_init(SocketReadState *rs,
Zhang Chen3cde5ea2017-07-04 14:53:46 +0800181 SocketReadStateFinalize *finalize,
182 bool vnet_hdr);
pbrook87ecb682007-11-17 17:14:51 +0000183
184/* NIC info */
185
186#define MAX_NICS 8
187
188struct NICInfo {
Jan Kiszka6eed1852011-07-20 12:20:22 +0200189 MACAddr macaddr;
Mark McLoughlin9203f522009-10-06 12:16:53 +0100190 char *model;
191 char *name;
192 char *devaddr;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100193 NetClientState *netdev;
Peter Maydell48e2faf2011-05-20 16:50:01 +0100194 int used; /* is this slot in nd_table[] being used? */
195 int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
Michael S. Tsirkinffe63702009-06-21 19:51:18 +0300196 int nvectors;
pbrook87ecb682007-11-17 17:14:51 +0000197};
198
199extern int nb_nics;
200extern NICInfo nd_table[MAX_NICS];
Hani Benhabiles84007e82014-05-27 23:39:33 +0100201extern const char *host_net_devices[];
pbrook87ecb682007-11-17 17:14:51 +0000202
aliguori63a01ef2008-10-31 19:10:00 +0000203/* from net.c */
Jan Kiszkaad196a92009-06-24 14:42:28 +0200204extern const char *legacy_tftp_prefix;
205extern const char *legacy_bootp_filename;
206
Mark McLoughlin7f161aa2009-10-08 19:58:25 +0100207int net_client_parse(QemuOptsList *opts_list, const char *str);
Thomas Huth34f708b2018-02-21 11:18:30 +0100208int net_init_clients(Error **errp);
Markus Armbruster668680f2010-02-11 14:44:58 +0100209void net_check_clients(void);
aliguori63a01ef2008-10-31 19:10:00 +0000210void net_cleanup(void);
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100211void hmp_host_net_add(Monitor *mon, const QDict *qdict);
212void hmp_host_net_remove(Monitor *mon, const QDict *qdict);
Luiz Capitulino928059a2012-04-18 17:34:15 -0300213void netdev_add(QemuOpts *opts, Error **errp);
Markus Armbruster485febc2015-03-13 17:25:50 +0100214void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp);
aliguori63a01ef2008-10-31 19:10:00 +0000215
Paolo Bonzini1422e322012-10-24 08:43:34 +0200216int net_hub_id_for_client(NetClientState *nc, int *id);
217NetClientState *net_hub_port_find(int hub_id);
218
aurel32f54825c2008-12-18 22:43:48 +0000219#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
220#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500221#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
222#define DEFAULT_BRIDGE_INTERFACE "br0"
aurel32f54825c2008-12-18 22:43:48 +0000223
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200224void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
Paul Brook9d07d752009-05-14 22:35:07 +0100225
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +0000226#define POLYNOMIAL_BE 0x04c11db6
Mark Cave-Aylandf1a7deb2017-12-15 18:41:44 +0000227#define POLYNOMIAL_LE 0xedb88320
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +0000228uint32_t net_crc32(const uint8_t *p, int len);
Mark Cave-Aylandf1a7deb2017-12-15 18:41:44 +0000229uint32_t net_crc32_le(const uint8_t *p, int len);
Jason Wang7fc8d912012-03-05 11:08:50 +0800230
Paolo Bonzini701a8f72012-01-13 17:07:20 +0100231#define vmstate_offset_macaddr(_state, _field) \
232 vmstate_offset_array(_state, _field.a, uint8_t, \
233 sizeof(typeof_field(_state, _field)))
234
235#define VMSTATE_MACADDR(_field, _state) { \
236 .name = (stringify(_field)), \
237 .size = sizeof(MACAddr), \
238 .info = &vmstate_info_buffer, \
239 .flags = VMS_BUFFER, \
240 .offset = vmstate_offset_macaddr(_state, _field), \
241}
242
pbrook87ecb682007-11-17 17:14:51 +0000243#endif