1 #ifndef QEMU_NET_H 2 #define QEMU_NET_H 3 4 #include "qemu/queue.h" 5 #include "qemu-common.h" 6 #include "qapi/qmp/qdict.h" 7 #include "qemu/option.h" 8 #include "net/queue.h" 9 #include "migration/vmstate.h" 10 #include "qapi-types.h" 11 12 #define MAX_QUEUE_NUM 1024 13 14 /* Maximum GSO packet size (64k) plus plenty of room for 15 * the ethernet and virtio_net headers 16 */ 17 #define NET_BUFSIZE (4096 + 65536) 18 19 struct MACAddr { 20 uint8_t a[6]; 21 }; 22 23 /* qdev nic properties */ 24 25 typedef struct NICPeers { 26 NetClientState *ncs[MAX_QUEUE_NUM]; 27 } NICPeers; 28 29 typedef struct NICConf { 30 MACAddr macaddr; 31 NICPeers peers; 32 int32_t bootindex; 33 int32_t queues; 34 } NICConf; 35 36 #define DEFINE_NIC_PROPERTIES(_state, _conf) \ 37 DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \ 38 DEFINE_PROP_VLAN("vlan", _state, _conf.peers), \ 39 DEFINE_PROP_NETDEV("netdev", _state, _conf.peers), \ 40 DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1) 41 42 43 /* Net clients */ 44 45 typedef void (NetPoll)(NetClientState *, bool enable); 46 typedef int (NetCanReceive)(NetClientState *); 47 typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t); 48 typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int); 49 typedef void (NetCleanup) (NetClientState *); 50 typedef void (LinkStatusChanged)(NetClientState *); 51 typedef void (NetClientDestructor)(NetClientState *); 52 53 typedef struct NetClientInfo { 54 NetClientOptionsKind type; 55 size_t size; 56 NetReceive *receive; 57 NetReceive *receive_raw; 58 NetReceiveIOV *receive_iov; 59 NetCanReceive *can_receive; 60 NetCleanup *cleanup; 61 LinkStatusChanged *link_status_changed; 62 NetPoll *poll; 63 } NetClientInfo; 64 65 struct NetClientState { 66 NetClientInfo *info; 67 int link_down; 68 QTAILQ_ENTRY(NetClientState) next; 69 NetClientState *peer; 70 NetQueue *send_queue; 71 char *model; 72 char *name; 73 char info_str[256]; 74 unsigned receive_disabled : 1; 75 NetClientDestructor *destructor; 76 unsigned int queue_index; 77 }; 78 79 typedef struct NICState { 80 NetClientState *ncs; 81 NICConf *conf; 82 void *opaque; 83 bool peer_deleted; 84 } NICState; 85 86 NetClientState *qemu_find_netdev(const char *id); 87 int qemu_find_net_clients_except(const char *id, NetClientState **ncs, 88 NetClientOptionsKind type, int max); 89 NetClientState *qemu_new_net_client(NetClientInfo *info, 90 NetClientState *peer, 91 const char *model, 92 const char *name); 93 NICState *qemu_new_nic(NetClientInfo *info, 94 NICConf *conf, 95 const char *model, 96 const char *name, 97 void *opaque); 98 void qemu_del_nic(NICState *nic); 99 NetClientState *qemu_get_subqueue(NICState *nic, int queue_index); 100 NetClientState *qemu_get_queue(NICState *nic); 101 NICState *qemu_get_nic(NetClientState *nc); 102 void *qemu_get_nic_opaque(NetClientState *nc); 103 void qemu_del_net_client(NetClientState *nc); 104 NetClientState *qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, 105 const char *client_str); 106 typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque); 107 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque); 108 int qemu_can_send_packet(NetClientState *nc); 109 ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, 110 int iovcnt); 111 ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov, 112 int iovcnt, NetPacketSent *sent_cb); 113 void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size); 114 ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size); 115 ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf, 116 int size, NetPacketSent *sent_cb); 117 void qemu_purge_queued_packets(NetClientState *nc); 118 void qemu_flush_queued_packets(NetClientState *nc); 119 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]); 120 void qemu_macaddr_default_if_unset(MACAddr *macaddr); 121 int qemu_show_nic_models(const char *arg, const char *const *models); 122 void qemu_check_nic_model(NICInfo *nd, const char *model); 123 int qemu_find_nic_model(NICInfo *nd, const char * const *models, 124 const char *default_model); 125 126 ssize_t qemu_deliver_packet(NetClientState *sender, 127 unsigned flags, 128 const uint8_t *data, 129 size_t size, 130 void *opaque); 131 ssize_t qemu_deliver_packet_iov(NetClientState *sender, 132 unsigned flags, 133 const struct iovec *iov, 134 int iovcnt, 135 void *opaque); 136 137 void print_net_client(Monitor *mon, NetClientState *nc); 138 void do_info_network(Monitor *mon, const QDict *qdict); 139 140 /* NIC info */ 141 142 #define MAX_NICS 8 143 144 struct NICInfo { 145 MACAddr macaddr; 146 char *model; 147 char *name; 148 char *devaddr; 149 NetClientState *netdev; 150 int used; /* is this slot in nd_table[] being used? */ 151 int instantiated; /* does this NICInfo correspond to an instantiated NIC? */ 152 int nvectors; 153 }; 154 155 extern int nb_nics; 156 extern NICInfo nd_table[MAX_NICS]; 157 extern int default_net; 158 159 /* from net.c */ 160 extern const char *legacy_tftp_prefix; 161 extern const char *legacy_bootp_filename; 162 163 int net_client_init(QemuOpts *opts, int is_netdev, Error **errp); 164 int net_client_parse(QemuOptsList *opts_list, const char *str); 165 int net_init_clients(void); 166 void net_check_clients(void); 167 void net_cleanup(void); 168 void net_host_device_add(Monitor *mon, const QDict *qdict); 169 void net_host_device_remove(Monitor *mon, const QDict *qdict); 170 void netdev_add(QemuOpts *opts, Error **errp); 171 int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret); 172 173 int net_hub_id_for_client(NetClientState *nc, int *id); 174 NetClientState *net_hub_port_find(int hub_id); 175 176 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" 177 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" 178 #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper" 179 #define DEFAULT_BRIDGE_INTERFACE "br0" 180 181 void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd); 182 183 #define POLYNOMIAL 0x04c11db6 184 unsigned compute_mcast_idx(const uint8_t *ep); 185 186 #define vmstate_offset_macaddr(_state, _field) \ 187 vmstate_offset_array(_state, _field.a, uint8_t, \ 188 sizeof(typeof_field(_state, _field))) 189 190 #define VMSTATE_MACADDR(_field, _state) { \ 191 .name = (stringify(_field)), \ 192 .size = sizeof(MACAddr), \ 193 .info = &vmstate_info_buffer, \ 194 .flags = VMS_BUFFER, \ 195 .offset = vmstate_offset_macaddr(_state, _field), \ 196 } 197 198 #endif 199