blob: 64993b43ef35e4964e5cd16503ac649142310511 [file] [log] [blame]
pbrook87ecb682007-11-17 17:14:51 +00001#ifndef QEMU_NET_H
2#define QEMU_NET_H
3
Blue Swirl72cf2d42009-09-12 07:36:22 +00004#include "qemu-queue.h"
aliguorifbe78f42008-12-17 19:13:11 +00005#include "qemu-common.h"
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03006#include "qdict.h"
Mark McLoughlin13cf8f22009-10-06 12:17:14 +01007#include "qemu-option.h"
Mark McLoughline1144d02009-10-23 17:52:16 +01008#include "net/queue.h"
Paolo Bonzini701a8f72012-01-13 17:07:20 +01009#include "vmstate.h"
aliguorifbe78f42008-12-17 19:13:11 +000010
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +020011struct MACAddr {
12 uint8_t a[6];
13};
14
Gerd Hoffmanned16ab52009-10-21 15:25:26 +020015/* qdev nic properties */
16
17typedef struct NICConf {
18 MACAddr macaddr;
19 VLANState *vlan;
20 VLANClientState *peer;
Gleb Natapov1ca4d092010-12-08 13:35:05 +020021 int32_t bootindex;
Gerd Hoffmanned16ab52009-10-21 15:25:26 +020022} NICConf;
23
24#define DEFINE_NIC_PROPERTIES(_state, _conf) \
25 DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \
26 DEFINE_PROP_VLAN("vlan", _state, _conf.vlan), \
Gleb Natapov1ca4d092010-12-08 13:35:05 +020027 DEFINE_PROP_NETDEV("netdev", _state, _conf.peer), \
28 DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1)
Gerd Hoffmanned16ab52009-10-21 15:25:26 +020029
pbrook87ecb682007-11-17 17:14:51 +000030/* VLANs support */
31
Mark McLoughlinbb6e6362009-10-22 17:43:38 +010032typedef enum {
33 NET_CLIENT_TYPE_NONE,
34 NET_CLIENT_TYPE_NIC,
Jan Kiszka6f7b3b12011-07-20 12:20:20 +020035 NET_CLIENT_TYPE_USER,
Mark McLoughlinbb6e6362009-10-22 17:43:38 +010036 NET_CLIENT_TYPE_TAP,
37 NET_CLIENT_TYPE_SOCKET,
38 NET_CLIENT_TYPE_VDE,
Jan Kiszka6f7b3b12011-07-20 12:20:20 +020039 NET_CLIENT_TYPE_DUMP,
Corey Bryanta7c36ee2012-01-26 09:42:27 -050040 NET_CLIENT_TYPE_BRIDGE,
Jan Kiszka6f7b3b12011-07-20 12:20:20 +020041
42 NET_CLIENT_TYPE_MAX
Mark McLoughlinbb6e6362009-10-22 17:43:38 +010043} net_client_type;
44
Michael S. Tsirkinceb69612009-12-24 14:46:29 +020045typedef void (NetPoll)(VLANClientState *, bool enable);
Mark McLoughline3f5ec22009-05-18 13:33:03 +010046typedef int (NetCanReceive)(VLANClientState *);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +010047typedef ssize_t (NetReceive)(VLANClientState *, const uint8_t *, size_t);
Mark McLoughline3f5ec22009-05-18 13:33:03 +010048typedef ssize_t (NetReceiveIOV)(VLANClientState *, const struct iovec *, int);
aliguorib946a152009-04-17 17:11:08 +000049typedef void (NetCleanup) (VLANClientState *);
aliguori34b25ca2009-01-08 19:45:03 +000050typedef void (LinkStatusChanged)(VLANClientState *);
51
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000052typedef struct NetClientInfo {
53 net_client_type type;
54 size_t size;
55 NetReceive *receive;
56 NetReceive *receive_raw;
57 NetReceiveIOV *receive_iov;
58 NetCanReceive *can_receive;
59 NetCleanup *cleanup;
60 LinkStatusChanged *link_status_changed;
Michael S. Tsirkinceb69612009-12-24 14:46:29 +020061 NetPoll *poll;
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000062} NetClientInfo;
63
pbrook87ecb682007-11-17 17:14:51 +000064struct VLANClientState {
Mark McLoughlin665a3b02009-11-25 18:49:30 +000065 NetClientInfo *info;
aliguori436e5e52009-01-08 19:44:06 +000066 int link_down;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +010067 QTAILQ_ENTRY(VLANClientState) next;
pbrook87ecb682007-11-17 17:14:51 +000068 struct VLANState *vlan;
Mark McLoughlin283c7c62009-10-08 19:58:30 +010069 VLANClientState *peer;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +010070 NetQueue *send_queue;
aliguoribf38c1a2009-01-07 17:42:25 +000071 char *model;
aliguori676cff22009-01-07 17:43:44 +000072 char *name;
pbrook87ecb682007-11-17 17:14:51 +000073 char info_str[256];
Mark McLoughlin893379e2009-10-27 18:16:36 +000074 unsigned receive_disabled : 1;
pbrook87ecb682007-11-17 17:14:51 +000075};
76
Mark McLoughlinebef2c02009-11-25 18:49:10 +000077typedef struct NICState {
78 VLANClientState nc;
79 NICConf *conf;
80 void *opaque;
Michael S. Tsirkina083a892010-09-20 18:08:41 +020081 bool peer_deleted;
Mark McLoughlinebef2c02009-11-25 18:49:10 +000082} NICState;
83
pbrook87ecb682007-11-17 17:14:51 +000084struct VLANState {
85 int id;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +010086 QTAILQ_HEAD(, VLANClientState) clients;
87 QTAILQ_ENTRY(VLANState) next;
Mark McLoughlinf7105842009-10-08 19:58:31 +010088 NetQueue *send_queue;
pbrook87ecb682007-11-17 17:14:51 +000089};
90
Jan Kiszka1a609522009-06-24 14:42:31 +020091VLANState *qemu_find_vlan(int id, int allocate);
Gerd Hoffmann2ef924b2009-10-21 15:25:24 +020092VLANClientState *qemu_find_netdev(const char *id);
Mark McLoughlin45460d12009-11-25 18:49:02 +000093VLANClientState *qemu_new_net_client(NetClientInfo *info,
94 VLANState *vlan,
95 VLANClientState *peer,
96 const char *model,
97 const char *name);
Mark McLoughlinebef2c02009-11-25 18:49:10 +000098NICState *qemu_new_nic(NetClientInfo *info,
99 NICConf *conf,
100 const char *model,
101 const char *name,
102 void *opaque);
balrogdcf414d2008-07-17 21:00:05 +0000103void qemu_del_vlan_client(VLANClientState *vc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000104VLANClientState *qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
105 const char *client_str);
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000106typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
107void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
pbrook87ecb682007-11-17 17:14:51 +0000108int qemu_can_send_packet(VLANClientState *vc);
aliguorifbe78f42008-12-17 19:13:11 +0000109ssize_t qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov,
110 int iovcnt);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100111ssize_t qemu_sendv_packet_async(VLANClientState *vc, const struct iovec *iov,
112 int iovcnt, NetPacketSent *sent_cb);
pbrook87ecb682007-11-17 17:14:51 +0000113void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size);
Mark McLoughlinca77d172009-10-22 17:43:41 +0100114ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100115ssize_t qemu_send_packet_async(VLANClientState *vc, const uint8_t *buf,
116 int size, NetPacketSent *sent_cb);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100117void qemu_purge_queued_packets(VLANClientState *vc);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100118void qemu_flush_queued_packets(VLANClientState *vc);
aliguori7cb7434b2009-01-07 17:46:21 +0000119void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200120void qemu_macaddr_default_if_unset(MACAddr *macaddr);
Markus Armbruster07caea32009-09-25 03:53:51 +0200121int qemu_show_nic_models(const char *arg, const char *const *models);
aliguorid07f22c2009-01-13 19:03:57 +0000122void qemu_check_nic_model(NICInfo *nd, const char *model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200123int qemu_find_nic_model(NICInfo *nd, const char * const *models,
124 const char *default_model);
pbrook87ecb682007-11-17 17:14:51 +0000125
aliguori376253e2009-03-05 23:01:23 +0000126void do_info_network(Monitor *mon);
pbrook87ecb682007-11-17 17:14:51 +0000127
128/* NIC info */
129
130#define MAX_NICS 8
131
132struct NICInfo {
Jan Kiszka6eed1852011-07-20 12:20:22 +0200133 MACAddr macaddr;
Mark McLoughlin9203f522009-10-06 12:16:53 +0100134 char *model;
135 char *name;
136 char *devaddr;
pbrook87ecb682007-11-17 17:14:51 +0000137 VLANState *vlan;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100138 VLANClientState *netdev;
Peter Maydell48e2faf2011-05-20 16:50:01 +0100139 int used; /* is this slot in nd_table[] being used? */
140 int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
Michael S. Tsirkinffe63702009-06-21 19:51:18 +0300141 int nvectors;
pbrook87ecb682007-11-17 17:14:51 +0000142};
143
144extern int nb_nics;
145extern NICInfo nd_table[MAX_NICS];
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +0100146extern int default_net;
pbrook87ecb682007-11-17 17:14:51 +0000147
balrog1ae26a12008-09-28 23:19:47 +0000148/* BT HCI info */
149
150struct HCIInfo {
151 int (*bdaddr_set)(struct HCIInfo *hci, const uint8_t *bd_addr);
152 void (*cmd_send)(struct HCIInfo *hci, const uint8_t *data, int len);
153 void (*sco_send)(struct HCIInfo *hci, const uint8_t *data, int len);
154 void (*acl_send)(struct HCIInfo *hci, const uint8_t *data, int len);
155 void *opaque;
156 void (*evt_recv)(void *opaque, const uint8_t *data, int len);
157 void (*acl_recv)(void *opaque, const uint8_t *data, int len);
158};
159
160struct HCIInfo *qemu_next_hci(void);
161
aliguori63a01ef2008-10-31 19:10:00 +0000162/* from net.c */
Jan Kiszkaad196a92009-06-24 14:42:28 +0200163extern const char *legacy_tftp_prefix;
164extern const char *legacy_bootp_filename;
165
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100166int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev);
Mark McLoughlin7f161aa2009-10-08 19:58:25 +0100167int net_client_parse(QemuOptsList *opts_list, const char *str);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +0100168int net_init_clients(void);
Markus Armbruster668680f2010-02-11 14:44:58 +0100169void net_check_clients(void);
aliguori63a01ef2008-10-31 19:10:00 +0000170void net_cleanup(void);
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300171void net_host_device_add(Monitor *mon, const QDict *qdict);
172void net_host_device_remove(Monitor *mon, const QDict *qdict);
Markus Armbrusterae82d322010-03-25 17:22:40 +0100173int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
174int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
aliguori63a01ef2008-10-31 19:10:00 +0000175
aurel32f54825c2008-12-18 22:43:48 +0000176#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
177#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500178#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
179#define DEFAULT_BRIDGE_INTERFACE "br0"
aurel32f54825c2008-12-18 22:43:48 +0000180
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200181void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
Paul Brook9d07d752009-05-14 22:35:07 +0100182
Mark McLoughlin5281d752009-10-22 17:49:07 +0100183int net_handle_fd_param(Monitor *mon, const char *param);
184
Jason Wang7fc8d912012-03-05 11:08:50 +0800185#define POLYNOMIAL 0x04c11db6
186unsigned compute_mcast_idx(const uint8_t *ep);
187
Paolo Bonzini701a8f72012-01-13 17:07:20 +0100188#define vmstate_offset_macaddr(_state, _field) \
189 vmstate_offset_array(_state, _field.a, uint8_t, \
190 sizeof(typeof_field(_state, _field)))
191
192#define VMSTATE_MACADDR(_field, _state) { \
193 .name = (stringify(_field)), \
194 .size = sizeof(MACAddr), \
195 .info = &vmstate_info_buffer, \
196 .flags = VMS_BUFFER, \
197 .offset = vmstate_offset_macaddr(_state, _field), \
198}
199
pbrook87ecb682007-11-17 17:14:51 +0000200#endif