blob: 04fda1d6c8f8559dac23e34824b8fc07d443a6b3 [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"
Laszlo Ersek2be64a62012-07-17 16:17:12 +020010#include "qapi-types.h"
aliguorifbe78f42008-12-17 19:13:11 +000011
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +020012struct MACAddr {
13 uint8_t a[6];
14};
15
Gerd Hoffmanned16ab52009-10-21 15:25:26 +020016/* qdev nic properties */
17
18typedef struct NICConf {
19 MACAddr macaddr;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010020 NetClientState *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), \
Zhi Yong Wu606c10e2012-07-24 16:35:09 +010026 DEFINE_PROP_VLAN("vlan", _state, _conf.peer), \
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
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010030/* Net clients */
pbrook87ecb682007-11-17 17:14:51 +000031
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010032typedef void (NetPoll)(NetClientState *, bool enable);
33typedef int (NetCanReceive)(NetClientState *);
34typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
35typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
36typedef void (NetCleanup) (NetClientState *);
37typedef void (LinkStatusChanged)(NetClientState *);
aliguori34b25ca2009-01-08 19:45:03 +000038
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000039typedef struct NetClientInfo {
Laszlo Ersek2be64a62012-07-17 16:17:12 +020040 NetClientOptionsKind type;
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000041 size_t size;
42 NetReceive *receive;
43 NetReceive *receive_raw;
44 NetReceiveIOV *receive_iov;
45 NetCanReceive *can_receive;
46 NetCleanup *cleanup;
47 LinkStatusChanged *link_status_changed;
Michael S. Tsirkinceb69612009-12-24 14:46:29 +020048 NetPoll *poll;
Mark McLoughlin3ed79cc2009-11-25 18:49:01 +000049} NetClientInfo;
50
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010051struct NetClientState {
Mark McLoughlin665a3b02009-11-25 18:49:30 +000052 NetClientInfo *info;
aliguori436e5e52009-01-08 19:44:06 +000053 int link_down;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010054 QTAILQ_ENTRY(NetClientState) next;
55 NetClientState *peer;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +010056 NetQueue *send_queue;
aliguoribf38c1a2009-01-07 17:42:25 +000057 char *model;
aliguori676cff22009-01-07 17:43:44 +000058 char *name;
pbrook87ecb682007-11-17 17:14:51 +000059 char info_str[256];
Mark McLoughlin893379e2009-10-27 18:16:36 +000060 unsigned receive_disabled : 1;
pbrook87ecb682007-11-17 17:14:51 +000061};
62
Mark McLoughlinebef2c02009-11-25 18:49:10 +000063typedef struct NICState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010064 NetClientState nc;
Mark McLoughlinebef2c02009-11-25 18:49:10 +000065 NICConf *conf;
66 void *opaque;
Michael S. Tsirkina083a892010-09-20 18:08:41 +020067 bool peer_deleted;
Mark McLoughlinebef2c02009-11-25 18:49:10 +000068} NICState;
69
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010070NetClientState *qemu_find_netdev(const char *id);
71NetClientState *qemu_new_net_client(NetClientInfo *info,
72 NetClientState *peer,
73 const char *model,
74 const char *name);
Mark McLoughlinebef2c02009-11-25 18:49:10 +000075NICState *qemu_new_nic(NetClientInfo *info,
76 NICConf *conf,
77 const char *model,
78 const char *name,
79 void *opaque);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +010080void qemu_del_net_client(NetClientState *nc);
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010081NetClientState *qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
82 const char *client_str);
Mark McLoughlin57f9ef12009-11-25 18:49:31 +000083typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque);
84void qemu_foreach_nic(qemu_nic_foreach func, void *opaque);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +010085int qemu_can_send_packet(NetClientState *nc);
86ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +000087 int iovcnt);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +010088ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +010089 int iovcnt, NetPacketSent *sent_cb);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +010090void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size);
91ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size);
92ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +010093 int size, NetPacketSent *sent_cb);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +010094void qemu_purge_queued_packets(NetClientState *nc);
95void qemu_flush_queued_packets(NetClientState *nc);
96void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +020097void qemu_macaddr_default_if_unset(MACAddr *macaddr);
Markus Armbruster07caea32009-09-25 03:53:51 +020098int qemu_show_nic_models(const char *arg, const char *const *models);
aliguorid07f22c2009-01-13 19:03:57 +000099void qemu_check_nic_model(NICInfo *nd, const char *model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200100int qemu_find_nic_model(NICInfo *nd, const char * const *models,
101 const char *default_model);
pbrook87ecb682007-11-17 17:14:51 +0000102
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100103ssize_t qemu_deliver_packet(NetClientState *sender,
104 unsigned flags,
105 const uint8_t *data,
106 size_t size,
107 void *opaque);
108ssize_t qemu_deliver_packet_iov(NetClientState *sender,
109 unsigned flags,
110 const struct iovec *iov,
111 int iovcnt,
112 void *opaque);
113
Zhi Yong Wu1a859592012-07-24 16:35:16 +0100114void print_net_client(Monitor *mon, NetClientState *nc);
aliguori376253e2009-03-05 23:01:23 +0000115void do_info_network(Monitor *mon);
pbrook87ecb682007-11-17 17:14:51 +0000116
117/* NIC info */
118
119#define MAX_NICS 8
120
121struct NICInfo {
Jan Kiszka6eed1852011-07-20 12:20:22 +0200122 MACAddr macaddr;
Mark McLoughlin9203f522009-10-06 12:16:53 +0100123 char *model;
124 char *name;
125 char *devaddr;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100126 NetClientState *netdev;
Peter Maydell48e2faf2011-05-20 16:50:01 +0100127 int used; /* is this slot in nd_table[] being used? */
128 int instantiated; /* does this NICInfo correspond to an instantiated NIC? */
Michael S. Tsirkinffe63702009-06-21 19:51:18 +0300129 int nvectors;
pbrook87ecb682007-11-17 17:14:51 +0000130};
131
132extern int nb_nics;
133extern NICInfo nd_table[MAX_NICS];
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +0100134extern int default_net;
pbrook87ecb682007-11-17 17:14:51 +0000135
balrog1ae26a12008-09-28 23:19:47 +0000136/* BT HCI info */
137
138struct HCIInfo {
139 int (*bdaddr_set)(struct HCIInfo *hci, const uint8_t *bd_addr);
140 void (*cmd_send)(struct HCIInfo *hci, const uint8_t *data, int len);
141 void (*sco_send)(struct HCIInfo *hci, const uint8_t *data, int len);
142 void (*acl_send)(struct HCIInfo *hci, const uint8_t *data, int len);
143 void *opaque;
144 void (*evt_recv)(void *opaque, const uint8_t *data, int len);
145 void (*acl_recv)(void *opaque, const uint8_t *data, int len);
146};
147
148struct HCIInfo *qemu_next_hci(void);
149
aliguori63a01ef2008-10-31 19:10:00 +0000150/* from net.c */
Jan Kiszkaad196a92009-06-24 14:42:28 +0200151extern const char *legacy_tftp_prefix;
152extern const char *legacy_bootp_filename;
153
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300154int net_client_init(QemuOpts *opts, int is_netdev, Error **errp);
Mark McLoughlin7f161aa2009-10-08 19:58:25 +0100155int net_client_parse(QemuOptsList *opts_list, const char *str);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +0100156int net_init_clients(void);
Markus Armbruster668680f2010-02-11 14:44:58 +0100157void net_check_clients(void);
aliguori63a01ef2008-10-31 19:10:00 +0000158void net_cleanup(void);
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300159void net_host_device_add(Monitor *mon, const QDict *qdict);
160void net_host_device_remove(Monitor *mon, const QDict *qdict);
Luiz Capitulino928059a2012-04-18 17:34:15 -0300161void netdev_add(QemuOpts *opts, Error **errp);
162int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret);
aliguori63a01ef2008-10-31 19:10:00 +0000163
aurel32f54825c2008-12-18 22:43:48 +0000164#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
165#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500166#define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper"
167#define DEFAULT_BRIDGE_INTERFACE "br0"
aurel32f54825c2008-12-18 22:43:48 +0000168
Gerd Hoffmanned16ab52009-10-21 15:25:26 +0200169void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd);
Paul Brook9d07d752009-05-14 22:35:07 +0100170
Jason Wang7fc8d912012-03-05 11:08:50 +0800171#define POLYNOMIAL 0x04c11db6
172unsigned compute_mcast_idx(const uint8_t *ep);
173
Paolo Bonzini701a8f72012-01-13 17:07:20 +0100174#define vmstate_offset_macaddr(_state, _field) \
175 vmstate_offset_array(_state, _field.a, uint8_t, \
176 sizeof(typeof_field(_state, _field)))
177
178#define VMSTATE_MACADDR(_field, _state) { \
179 .name = (stringify(_field)), \
180 .size = sizeof(MACAddr), \
181 .info = &vmstate_info_buffer, \
182 .flags = VMS_BUFFER, \
183 .offset = vmstate_offset_macaddr(_state, _field), \
184}
185
pbrook87ecb682007-11-17 17:14:51 +0000186#endif