11422e32dSPaolo Bonzini #ifndef QEMU_NET_H 21422e32dSPaolo Bonzini #define QEMU_NET_H 31422e32dSPaolo Bonzini 41de7afc9SPaolo Bonzini #include "qemu/queue.h" 59af23989SMarkus Armbruster #include "qapi/qapi-types-net.h" 61422e32dSPaolo Bonzini #include "net/queue.h" 7ce35e229SEduardo Habkost #include "hw/qdev-properties-system.h" 81422e32dSPaolo Bonzini 96d1d4939SDmitry Fleytman #define MAC_FMT "%02X:%02X:%02X:%02X:%02X:%02X" 106d1d4939SDmitry Fleytman #define MAC_ARG(x) ((uint8_t *)(x))[0], ((uint8_t *)(x))[1], \ 116d1d4939SDmitry Fleytman ((uint8_t *)(x))[2], ((uint8_t *)(x))[3], \ 126d1d4939SDmitry Fleytman ((uint8_t *)(x))[4], ((uint8_t *)(x))[5] 136d1d4939SDmitry Fleytman 141ceef9f2SJason Wang #define MAX_QUEUE_NUM 1024 151ceef9f2SJason Wang 16d32fcad3SScott Feldman /* Maximum GSO packet size (64k) plus plenty of room for 17d32fcad3SScott Feldman * the ethernet and virtio_net headers 18d32fcad3SScott Feldman */ 19d32fcad3SScott Feldman #define NET_BUFSIZE (4096 + 65536) 20d32fcad3SScott Feldman 211422e32dSPaolo Bonzini struct MACAddr { 221422e32dSPaolo Bonzini uint8_t a[6]; 231422e32dSPaolo Bonzini }; 241422e32dSPaolo Bonzini 251422e32dSPaolo Bonzini /* qdev nic properties */ 261422e32dSPaolo Bonzini 271ceef9f2SJason Wang typedef struct NICPeers { 281ceef9f2SJason Wang NetClientState *ncs[MAX_QUEUE_NUM]; 29575a1c0eSJiri Pirko int32_t queues; 301ceef9f2SJason Wang } NICPeers; 311ceef9f2SJason Wang 321422e32dSPaolo Bonzini typedef struct NICConf { 331422e32dSPaolo Bonzini MACAddr macaddr; 341ceef9f2SJason Wang NICPeers peers; 351422e32dSPaolo Bonzini int32_t bootindex; 361422e32dSPaolo Bonzini } NICConf; 371422e32dSPaolo Bonzini 381422e32dSPaolo Bonzini #define DEFINE_NIC_PROPERTIES(_state, _conf) \ 391422e32dSPaolo Bonzini DEFINE_PROP_MACADDR("mac", _state, _conf.macaddr), \ 4045e8a9e1SGonglei DEFINE_PROP_NETDEV("netdev", _state, _conf.peers) 411422e32dSPaolo Bonzini 421ceef9f2SJason Wang 431422e32dSPaolo Bonzini /* Net clients */ 441422e32dSPaolo Bonzini 451422e32dSPaolo Bonzini typedef void (NetPoll)(NetClientState *, bool enable); 46b8c4b67eSPhilippe Mathieu-Daudé typedef bool (NetCanReceive)(NetClientState *); 47eb92b753SEugenio Pérez typedef int (NetStart)(NetClientState *); 48539573c3SEugenio Pérez typedef int (NetLoad)(NetClientState *); 49c5e5269dSEugenio Pérez typedef void (NetStop)(NetClientState *); 501422e32dSPaolo Bonzini typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t); 511422e32dSPaolo Bonzini typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int); 521422e32dSPaolo Bonzini typedef void (NetCleanup) (NetClientState *); 531422e32dSPaolo Bonzini typedef void (LinkStatusChanged)(NetClientState *); 54f7860455SJason Wang typedef void (NetClientDestructor)(NetClientState *); 55b1be4280SAmos Kong typedef RxFilterInfo *(QueryRxFilter)(NetClientState *); 561f55ac45SVincenzo Maffione typedef bool (HasUfo)(NetClientState *); 57f03e0cf6SYuri Benditovich typedef bool (HasUso)(NetClientState *); 581f55ac45SVincenzo Maffione typedef bool (HasVnetHdr)(NetClientState *); 591f55ac45SVincenzo Maffione typedef bool (HasVnetHdrLen)(NetClientState *, int); 60481c5232SAkihiko Odaki typedef bool (GetUsingVnetHdr)(NetClientState *); 611f55ac45SVincenzo Maffione typedef void (UsingVnetHdr)(NetClientState *, bool); 622ab0ec31SAndrew Melnychenko typedef void (SetOffload)(NetClientState *, int, int, int, int, int, int, int); 63481c5232SAkihiko Odaki typedef int (GetVnetHdrLen)(NetClientState *); 641f55ac45SVincenzo Maffione typedef void (SetVnetHdrLen)(NetClientState *, int); 65c80cd6bbSGreg Kurz typedef int (SetVnetLE)(NetClientState *, bool); 66c80cd6bbSGreg Kurz typedef int (SetVnetBE)(NetClientState *, bool); 6716a3df40SZhang Chen typedef struct SocketReadState SocketReadState; 6816a3df40SZhang Chen typedef void (SocketReadStateFinalize)(SocketReadState *rs); 6944b416adSDr. David Alan Gilbert typedef void (NetAnnounce)(NetClientState *); 708f364e34SAndrew Melnychenko typedef bool (SetSteeringEBPF)(NetClientState *, int); 71e287bf7bSKevin Wolf typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **); 721422e32dSPaolo Bonzini 731422e32dSPaolo Bonzini typedef struct NetClientInfo { 74f394b2e2SEric Blake NetClientDriver type; 751422e32dSPaolo Bonzini size_t size; 761422e32dSPaolo Bonzini NetReceive *receive; 771422e32dSPaolo Bonzini NetReceive *receive_raw; 781422e32dSPaolo Bonzini NetReceiveIOV *receive_iov; 791422e32dSPaolo Bonzini NetCanReceive *can_receive; 80eb92b753SEugenio Pérez NetStart *start; 81539573c3SEugenio Pérez NetLoad *load; 82c5e5269dSEugenio Pérez NetStop *stop; 831422e32dSPaolo Bonzini NetCleanup *cleanup; 841422e32dSPaolo Bonzini LinkStatusChanged *link_status_changed; 85b1be4280SAmos Kong QueryRxFilter *query_rx_filter; 861422e32dSPaolo Bonzini NetPoll *poll; 871f55ac45SVincenzo Maffione HasUfo *has_ufo; 88f03e0cf6SYuri Benditovich HasUso *has_uso; 891f55ac45SVincenzo Maffione HasVnetHdr *has_vnet_hdr; 901f55ac45SVincenzo Maffione HasVnetHdrLen *has_vnet_hdr_len; 91481c5232SAkihiko Odaki GetUsingVnetHdr *get_using_vnet_hdr; 921f55ac45SVincenzo Maffione UsingVnetHdr *using_vnet_hdr; 931f55ac45SVincenzo Maffione SetOffload *set_offload; 94481c5232SAkihiko Odaki GetVnetHdrLen *get_vnet_hdr_len; 951f55ac45SVincenzo Maffione SetVnetHdrLen *set_vnet_hdr_len; 96c80cd6bbSGreg Kurz SetVnetLE *set_vnet_le; 97c80cd6bbSGreg Kurz SetVnetBE *set_vnet_be; 9844b416adSDr. David Alan Gilbert NetAnnounce *announce; 998f364e34SAndrew Melnychenko SetSteeringEBPF *set_steering_ebpf; 100e287bf7bSKevin Wolf NetCheckPeerType *check_peer_type; 1011422e32dSPaolo Bonzini } NetClientInfo; 1021422e32dSPaolo Bonzini 1031422e32dSPaolo Bonzini struct NetClientState { 1041422e32dSPaolo Bonzini NetClientInfo *info; 1051422e32dSPaolo Bonzini int link_down; 1061422e32dSPaolo Bonzini QTAILQ_ENTRY(NetClientState) next; 1071422e32dSPaolo Bonzini NetClientState *peer; 108067404beSJan Kiszka NetQueue *incoming_queue; 1091422e32dSPaolo Bonzini char *model; 1101422e32dSPaolo Bonzini char *name; 11156e6f594SJason Wang char info_str[256]; 1121422e32dSPaolo Bonzini unsigned receive_disabled : 1; 113f7860455SJason Wang NetClientDestructor *destructor; 1141ceef9f2SJason Wang unsigned int queue_index; 115b1be4280SAmos Kong unsigned rxfilter_notify_enabled:1; 116bfc6cf31SMarc-André Lureau int vring_enable; 117d6b732e9SZhang Chen int vnet_hdr_len; 11808712fcbSEric Blake bool is_netdev; 119935344beSBin Meng bool do_not_pad; /* do not pad to the minimum ethernet frame length */ 1202f849dbdSJason Wang bool is_datapath; 121eae3eb3eSPaolo Bonzini QTAILQ_HEAD(, NetFilterState) filters; 1221422e32dSPaolo Bonzini }; 1231422e32dSPaolo Bonzini 124ae71d13dSMarkus Armbruster typedef QTAILQ_HEAD(NetClientStateList, NetClientState) NetClientStateList; 125ae71d13dSMarkus Armbruster 1261422e32dSPaolo Bonzini typedef struct NICState { 127f6b26cf2SJason Wang NetClientState *ncs; 1281422e32dSPaolo Bonzini NICConf *conf; 1291422e32dSPaolo Bonzini void *opaque; 1301422e32dSPaolo Bonzini bool peer_deleted; 1311422e32dSPaolo Bonzini } NICState; 1321422e32dSPaolo Bonzini 13316a3df40SZhang Chen struct SocketReadState { 1343cde5ea2SZhang Chen /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */ 1353cde5ea2SZhang Chen int state; 1363cde5ea2SZhang Chen /* This flag decide whether to read the vnet_hdr_len field */ 1373cde5ea2SZhang Chen bool vnet_hdr; 13816a3df40SZhang Chen uint32_t index; 13916a3df40SZhang Chen uint32_t packet_len; 1403cde5ea2SZhang Chen uint32_t vnet_hdr_len; 14116a3df40SZhang Chen uint8_t buf[NET_BUFSIZE]; 14216a3df40SZhang Chen SocketReadStateFinalize *finalize; 14316a3df40SZhang Chen }; 14416a3df40SZhang Chen 14516a3df40SZhang Chen int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size); 146890ee6abSScott Feldman char *qemu_mac_strdup_printf(const uint8_t *macaddr); 1471422e32dSPaolo Bonzini NetClientState *qemu_find_netdev(const char *id); 1486c51ae73SJason Wang int qemu_find_net_clients_except(const char *id, NetClientState **ncs, 149f394b2e2SEric Blake NetClientDriver type, int max); 1501422e32dSPaolo Bonzini NetClientState *qemu_new_net_client(NetClientInfo *info, 1511422e32dSPaolo Bonzini NetClientState *peer, 1521422e32dSPaolo Bonzini const char *model, 1531422e32dSPaolo Bonzini const char *name); 1542f849dbdSJason Wang NetClientState *qemu_new_net_control_client(NetClientInfo *info, 1552f849dbdSJason Wang NetClientState *peer, 1562f849dbdSJason Wang const char *model, 1572f849dbdSJason Wang const char *name); 1581422e32dSPaolo Bonzini NICState *qemu_new_nic(NetClientInfo *info, 1591422e32dSPaolo Bonzini NICConf *conf, 1601422e32dSPaolo Bonzini const char *model, 1611422e32dSPaolo Bonzini const char *name, 1621422e32dSPaolo Bonzini void *opaque); 163948ecf21SJason Wang void qemu_del_nic(NICState *nic); 1641ceef9f2SJason Wang NetClientState *qemu_get_subqueue(NICState *nic, int queue_index); 165b356f76dSJason Wang NetClientState *qemu_get_queue(NICState *nic); 166cc1f0f45SJason Wang NICState *qemu_get_nic(NetClientState *nc); 167cc1f0f45SJason Wang void *qemu_get_nic_opaque(NetClientState *nc); 1681422e32dSPaolo Bonzini void qemu_del_net_client(NetClientState *nc); 1691422e32dSPaolo Bonzini typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque); 1701422e32dSPaolo Bonzini void qemu_foreach_nic(qemu_nic_foreach func, void *opaque); 171705df546SJason Wang int qemu_can_receive_packet(NetClientState *nc); 1721422e32dSPaolo Bonzini int qemu_can_send_packet(NetClientState *nc); 1731422e32dSPaolo Bonzini ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, 1741422e32dSPaolo Bonzini int iovcnt); 1751422e32dSPaolo Bonzini ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov, 1761422e32dSPaolo Bonzini int iovcnt, NetPacketSent *sent_cb); 177625a526bSMarc-André Lureau ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size); 178705df546SJason Wang ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size); 179705df546SJason Wang ssize_t qemu_receive_packet_iov(NetClientState *nc, 180705df546SJason Wang const struct iovec *iov, 181705df546SJason Wang int iovcnt); 1821422e32dSPaolo Bonzini ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size); 1831422e32dSPaolo Bonzini ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf, 1841422e32dSPaolo Bonzini int size, NetPacketSent *sent_cb); 1851422e32dSPaolo Bonzini void qemu_purge_queued_packets(NetClientState *nc); 1861422e32dSPaolo Bonzini void qemu_flush_queued_packets(NetClientState *nc); 18794b52958SGreg Kurz void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge); 188ac149498SStefan Weil via void qemu_set_info_str(NetClientState *nc, 189ac149498SStefan Weil via const char *fmt, ...) G_GNUC_PRINTF(2, 3); 1901422e32dSPaolo Bonzini void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]); 191d6085e3aSStefan Hajnoczi bool qemu_has_ufo(NetClientState *nc); 192f03e0cf6SYuri Benditovich bool qemu_has_uso(NetClientState *nc); 193d6085e3aSStefan Hajnoczi bool qemu_has_vnet_hdr(NetClientState *nc); 194d6085e3aSStefan Hajnoczi bool qemu_has_vnet_hdr_len(NetClientState *nc, int len); 195481c5232SAkihiko Odaki bool qemu_get_using_vnet_hdr(NetClientState *nc); 196d6085e3aSStefan Hajnoczi void qemu_using_vnet_hdr(NetClientState *nc, bool enable); 197d6085e3aSStefan Hajnoczi void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6, 1982ab0ec31SAndrew Melnychenko int ecn, int ufo, int uso4, int uso6); 199481c5232SAkihiko Odaki int qemu_get_vnet_hdr_len(NetClientState *nc); 200d6085e3aSStefan Hajnoczi void qemu_set_vnet_hdr_len(NetClientState *nc, int len); 201c80cd6bbSGreg Kurz int qemu_set_vnet_le(NetClientState *nc, bool is_le); 202c80cd6bbSGreg Kurz int qemu_set_vnet_be(NetClientState *nc, bool is_be); 2031422e32dSPaolo Bonzini void qemu_macaddr_default_if_unset(MACAddr *macaddr); 2041422e32dSPaolo Bonzini int qemu_show_nic_models(const char *arg, const char *const *models); 2051422e32dSPaolo Bonzini void qemu_check_nic_model(NICInfo *nd, const char *model); 2061422e32dSPaolo Bonzini int qemu_find_nic_model(NICInfo *nd, const char * const *models, 2071422e32dSPaolo Bonzini const char *default_model); 2081422e32dSPaolo Bonzini 2091422e32dSPaolo Bonzini void print_net_client(Monitor *mon, NetClientState *nc); 21016a3df40SZhang Chen void net_socket_rs_init(SocketReadState *rs, 2113cde5ea2SZhang Chen SocketReadStateFinalize *finalize, 2123cde5ea2SZhang Chen bool vnet_hdr); 2130165daaeSCindy Lu NetClientState *qemu_get_peer(NetClientState *nc, int queue_index); 2141422e32dSPaolo Bonzini 215c6941b3bSThomas Huth /** 216c6941b3bSThomas Huth * qemu_get_nic_models: 217c6941b3bSThomas Huth * @device_type: Defines which devices should be taken into consideration 218c6941b3bSThomas Huth * (e.g. TYPE_DEVICE for all devices, or TYPE_PCI_DEVICE for PCI) 219c6941b3bSThomas Huth * 220c6941b3bSThomas Huth * Get an array of pointers to names of NIC devices that are available in 221c6941b3bSThomas Huth * the QEMU binary. The array is terminated with a NULL pointer entry. 222c6941b3bSThomas Huth * The caller is responsible for freeing the memory when it is not required 223c6941b3bSThomas Huth * anymore, e.g. with g_ptr_array_free(..., true). 224c6941b3bSThomas Huth * 225c6941b3bSThomas Huth * Returns: Pointer to the array that contains the pointers to the names. 226c6941b3bSThomas Huth */ 227c6941b3bSThomas Huth GPtrArray *qemu_get_nic_models(const char *device_type); 228c6941b3bSThomas Huth 2291422e32dSPaolo Bonzini /* NIC info */ 2301422e32dSPaolo Bonzini 2311422e32dSPaolo Bonzini #define MAX_NICS 8 2321422e32dSPaolo Bonzini 2331422e32dSPaolo Bonzini struct NICInfo { 2341422e32dSPaolo Bonzini MACAddr macaddr; 2351422e32dSPaolo Bonzini char *model; 2361422e32dSPaolo Bonzini char *name; 2371422e32dSPaolo Bonzini char *devaddr; 2381422e32dSPaolo Bonzini NetClientState *netdev; 2391422e32dSPaolo Bonzini int used; /* is this slot in nd_table[] being used? */ 2401422e32dSPaolo Bonzini int instantiated; /* does this NICInfo correspond to an instantiated NIC? */ 2411422e32dSPaolo Bonzini int nvectors; 2421422e32dSPaolo Bonzini }; 2431422e32dSPaolo Bonzini 2441422e32dSPaolo Bonzini extern int nb_nics; 2451422e32dSPaolo Bonzini extern NICInfo nd_table[MAX_NICS]; 24684007e81SHani Benhabiles extern const char *host_net_devices[]; 2471422e32dSPaolo Bonzini 2481422e32dSPaolo Bonzini /* from net.c */ 249ae71d13dSMarkus Armbruster extern NetClientStateList net_clients; 250*73071f19SPhilippe Mathieu-Daudé bool netdev_is_modern(const char *optstr); 251*73071f19SPhilippe Mathieu-Daudé void netdev_parse_modern(const char *optstr); 252*73071f19SPhilippe Mathieu-Daudé void net_client_parse(QemuOptsList *opts_list, const char *optstr); 253ad6f932fSPaolo Bonzini void show_netdevs(void); 254d63ef17bSLaurent Vivier void net_init_clients(void); 2551422e32dSPaolo Bonzini void net_check_clients(void); 2561422e32dSPaolo Bonzini void net_cleanup(void); 2573e5a50d6SMarkus Armbruster void hmp_host_net_add(Monitor *mon, const QDict *qdict); 2583e5a50d6SMarkus Armbruster void hmp_host_net_remove(Monitor *mon, const QDict *qdict); 2591422e32dSPaolo Bonzini void netdev_add(QemuOpts *opts, Error **errp); 2601422e32dSPaolo Bonzini 2611422e32dSPaolo Bonzini int net_hub_id_for_client(NetClientState *nc, int *id); 2621422e32dSPaolo Bonzini NetClientState *net_hub_port_find(int hub_id); 2631422e32dSPaolo Bonzini 26463c4db4cSPaolo Bonzini #define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup" 26563c4db4cSPaolo Bonzini #define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown" 2661422e32dSPaolo Bonzini #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper" 2671422e32dSPaolo Bonzini #define DEFAULT_BRIDGE_INTERFACE "br0" 2681422e32dSPaolo Bonzini 2691422e32dSPaolo Bonzini void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd); 2701422e32dSPaolo Bonzini 271eaba8f34SMark Cave-Ayland #define POLYNOMIAL_BE 0x04c11db6 272f1a7deb9SMark Cave-Ayland #define POLYNOMIAL_LE 0xedb88320 273eaba8f34SMark Cave-Ayland uint32_t net_crc32(const uint8_t *p, int len); 274f1a7deb9SMark Cave-Ayland uint32_t net_crc32_le(const uint8_t *p, int len); 2751422e32dSPaolo Bonzini 2761422e32dSPaolo Bonzini #define vmstate_offset_macaddr(_state, _field) \ 2771422e32dSPaolo Bonzini vmstate_offset_array(_state, _field.a, uint8_t, \ 2781422e32dSPaolo Bonzini sizeof(typeof_field(_state, _field))) 2791422e32dSPaolo Bonzini 2801422e32dSPaolo Bonzini #define VMSTATE_MACADDR(_field, _state) { \ 2811422e32dSPaolo Bonzini .name = (stringify(_field)), \ 2821422e32dSPaolo Bonzini .size = sizeof(MACAddr), \ 2831422e32dSPaolo Bonzini .info = &vmstate_info_buffer, \ 2841422e32dSPaolo Bonzini .flags = VMS_BUFFER, \ 2851422e32dSPaolo Bonzini .offset = vmstate_offset_macaddr(_state, _field), \ 2861422e32dSPaolo Bonzini } 2871422e32dSPaolo Bonzini 288bc38e31bSJason Wang static inline bool net_peer_needs_padding(NetClientState *nc) 289bc38e31bSJason Wang { 290bc38e31bSJason Wang return nc->peer && !nc->peer->do_not_pad; 291bc38e31bSJason Wang } 292bc38e31bSJason Wang 2931422e32dSPaolo Bonzini #endif 294