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 *); 571f55ac45SVincenzo Maffione typedef bool (HasVnetHdr)(NetClientState *); 581f55ac45SVincenzo Maffione typedef bool (HasVnetHdrLen)(NetClientState *, int); 59*481c5232SAkihiko Odaki typedef bool (GetUsingVnetHdr)(NetClientState *); 601f55ac45SVincenzo Maffione typedef void (UsingVnetHdr)(NetClientState *, bool); 611f55ac45SVincenzo Maffione typedef void (SetOffload)(NetClientState *, int, int, int, int, int); 62*481c5232SAkihiko Odaki typedef int (GetVnetHdrLen)(NetClientState *); 631f55ac45SVincenzo Maffione typedef void (SetVnetHdrLen)(NetClientState *, int); 64c80cd6bbSGreg Kurz typedef int (SetVnetLE)(NetClientState *, bool); 65c80cd6bbSGreg Kurz typedef int (SetVnetBE)(NetClientState *, bool); 6616a3df40SZhang Chen typedef struct SocketReadState SocketReadState; 6716a3df40SZhang Chen typedef void (SocketReadStateFinalize)(SocketReadState *rs); 6844b416adSDr. David Alan Gilbert typedef void (NetAnnounce)(NetClientState *); 698f364e34SAndrew Melnychenko typedef bool (SetSteeringEBPF)(NetClientState *, int); 70e287bf7bSKevin Wolf typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **); 711422e32dSPaolo Bonzini 721422e32dSPaolo Bonzini typedef struct NetClientInfo { 73f394b2e2SEric Blake NetClientDriver type; 741422e32dSPaolo Bonzini size_t size; 751422e32dSPaolo Bonzini NetReceive *receive; 761422e32dSPaolo Bonzini NetReceive *receive_raw; 771422e32dSPaolo Bonzini NetReceiveIOV *receive_iov; 781422e32dSPaolo Bonzini NetCanReceive *can_receive; 79eb92b753SEugenio Pérez NetStart *start; 80539573c3SEugenio Pérez NetLoad *load; 81c5e5269dSEugenio Pérez NetStop *stop; 821422e32dSPaolo Bonzini NetCleanup *cleanup; 831422e32dSPaolo Bonzini LinkStatusChanged *link_status_changed; 84b1be4280SAmos Kong QueryRxFilter *query_rx_filter; 851422e32dSPaolo Bonzini NetPoll *poll; 861f55ac45SVincenzo Maffione HasUfo *has_ufo; 871f55ac45SVincenzo Maffione HasVnetHdr *has_vnet_hdr; 881f55ac45SVincenzo Maffione HasVnetHdrLen *has_vnet_hdr_len; 89*481c5232SAkihiko Odaki GetUsingVnetHdr *get_using_vnet_hdr; 901f55ac45SVincenzo Maffione UsingVnetHdr *using_vnet_hdr; 911f55ac45SVincenzo Maffione SetOffload *set_offload; 92*481c5232SAkihiko Odaki GetVnetHdrLen *get_vnet_hdr_len; 931f55ac45SVincenzo Maffione SetVnetHdrLen *set_vnet_hdr_len; 94c80cd6bbSGreg Kurz SetVnetLE *set_vnet_le; 95c80cd6bbSGreg Kurz SetVnetBE *set_vnet_be; 9644b416adSDr. David Alan Gilbert NetAnnounce *announce; 978f364e34SAndrew Melnychenko SetSteeringEBPF *set_steering_ebpf; 98e287bf7bSKevin Wolf NetCheckPeerType *check_peer_type; 991422e32dSPaolo Bonzini } NetClientInfo; 1001422e32dSPaolo Bonzini 1011422e32dSPaolo Bonzini struct NetClientState { 1021422e32dSPaolo Bonzini NetClientInfo *info; 1031422e32dSPaolo Bonzini int link_down; 1041422e32dSPaolo Bonzini QTAILQ_ENTRY(NetClientState) next; 1051422e32dSPaolo Bonzini NetClientState *peer; 106067404beSJan Kiszka NetQueue *incoming_queue; 1071422e32dSPaolo Bonzini char *model; 1081422e32dSPaolo Bonzini char *name; 10956e6f594SJason Wang char info_str[256]; 1101422e32dSPaolo Bonzini unsigned receive_disabled : 1; 111f7860455SJason Wang NetClientDestructor *destructor; 1121ceef9f2SJason Wang unsigned int queue_index; 113b1be4280SAmos Kong unsigned rxfilter_notify_enabled:1; 114bfc6cf31SMarc-André Lureau int vring_enable; 115d6b732e9SZhang Chen int vnet_hdr_len; 11608712fcbSEric Blake bool is_netdev; 117935344beSBin Meng bool do_not_pad; /* do not pad to the minimum ethernet frame length */ 1182f849dbdSJason Wang bool is_datapath; 119eae3eb3eSPaolo Bonzini QTAILQ_HEAD(, NetFilterState) filters; 1201422e32dSPaolo Bonzini }; 1211422e32dSPaolo Bonzini 122ae71d13dSMarkus Armbruster typedef QTAILQ_HEAD(NetClientStateList, NetClientState) NetClientStateList; 123ae71d13dSMarkus Armbruster 1241422e32dSPaolo Bonzini typedef struct NICState { 125f6b26cf2SJason Wang NetClientState *ncs; 1261422e32dSPaolo Bonzini NICConf *conf; 1271422e32dSPaolo Bonzini void *opaque; 1281422e32dSPaolo Bonzini bool peer_deleted; 1291422e32dSPaolo Bonzini } NICState; 1301422e32dSPaolo Bonzini 13116a3df40SZhang Chen struct SocketReadState { 1323cde5ea2SZhang Chen /* 0 = getting length, 1 = getting vnet header length, 2 = getting data */ 1333cde5ea2SZhang Chen int state; 1343cde5ea2SZhang Chen /* This flag decide whether to read the vnet_hdr_len field */ 1353cde5ea2SZhang Chen bool vnet_hdr; 13616a3df40SZhang Chen uint32_t index; 13716a3df40SZhang Chen uint32_t packet_len; 1383cde5ea2SZhang Chen uint32_t vnet_hdr_len; 13916a3df40SZhang Chen uint8_t buf[NET_BUFSIZE]; 14016a3df40SZhang Chen SocketReadStateFinalize *finalize; 14116a3df40SZhang Chen }; 14216a3df40SZhang Chen 14316a3df40SZhang Chen int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size); 144890ee6abSScott Feldman char *qemu_mac_strdup_printf(const uint8_t *macaddr); 1451422e32dSPaolo Bonzini NetClientState *qemu_find_netdev(const char *id); 1466c51ae73SJason Wang int qemu_find_net_clients_except(const char *id, NetClientState **ncs, 147f394b2e2SEric Blake NetClientDriver type, int max); 1481422e32dSPaolo Bonzini NetClientState *qemu_new_net_client(NetClientInfo *info, 1491422e32dSPaolo Bonzini NetClientState *peer, 1501422e32dSPaolo Bonzini const char *model, 1511422e32dSPaolo Bonzini const char *name); 1522f849dbdSJason Wang NetClientState *qemu_new_net_control_client(NetClientInfo *info, 1532f849dbdSJason Wang NetClientState *peer, 1542f849dbdSJason Wang const char *model, 1552f849dbdSJason Wang const char *name); 1561422e32dSPaolo Bonzini NICState *qemu_new_nic(NetClientInfo *info, 1571422e32dSPaolo Bonzini NICConf *conf, 1581422e32dSPaolo Bonzini const char *model, 1591422e32dSPaolo Bonzini const char *name, 1601422e32dSPaolo Bonzini void *opaque); 161948ecf21SJason Wang void qemu_del_nic(NICState *nic); 1621ceef9f2SJason Wang NetClientState *qemu_get_subqueue(NICState *nic, int queue_index); 163b356f76dSJason Wang NetClientState *qemu_get_queue(NICState *nic); 164cc1f0f45SJason Wang NICState *qemu_get_nic(NetClientState *nc); 165cc1f0f45SJason Wang void *qemu_get_nic_opaque(NetClientState *nc); 1661422e32dSPaolo Bonzini void qemu_del_net_client(NetClientState *nc); 1671422e32dSPaolo Bonzini typedef void (*qemu_nic_foreach)(NICState *nic, void *opaque); 1681422e32dSPaolo Bonzini void qemu_foreach_nic(qemu_nic_foreach func, void *opaque); 169705df546SJason Wang int qemu_can_receive_packet(NetClientState *nc); 1701422e32dSPaolo Bonzini int qemu_can_send_packet(NetClientState *nc); 1711422e32dSPaolo Bonzini ssize_t qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, 1721422e32dSPaolo Bonzini int iovcnt); 1731422e32dSPaolo Bonzini ssize_t qemu_sendv_packet_async(NetClientState *nc, const struct iovec *iov, 1741422e32dSPaolo Bonzini int iovcnt, NetPacketSent *sent_cb); 175625a526bSMarc-André Lureau ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size); 176705df546SJason Wang ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size); 177705df546SJason Wang ssize_t qemu_receive_packet_iov(NetClientState *nc, 178705df546SJason Wang const struct iovec *iov, 179705df546SJason Wang int iovcnt); 1801422e32dSPaolo Bonzini ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size); 1811422e32dSPaolo Bonzini ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf, 1821422e32dSPaolo Bonzini int size, NetPacketSent *sent_cb); 1831422e32dSPaolo Bonzini void qemu_purge_queued_packets(NetClientState *nc); 1841422e32dSPaolo Bonzini void qemu_flush_queued_packets(NetClientState *nc); 18594b52958SGreg Kurz void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge); 186ac149498SStefan Weil via void qemu_set_info_str(NetClientState *nc, 187ac149498SStefan Weil via const char *fmt, ...) G_GNUC_PRINTF(2, 3); 1881422e32dSPaolo Bonzini void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]); 189d6085e3aSStefan Hajnoczi bool qemu_has_ufo(NetClientState *nc); 190d6085e3aSStefan Hajnoczi bool qemu_has_vnet_hdr(NetClientState *nc); 191d6085e3aSStefan Hajnoczi bool qemu_has_vnet_hdr_len(NetClientState *nc, int len); 192*481c5232SAkihiko Odaki bool qemu_get_using_vnet_hdr(NetClientState *nc); 193d6085e3aSStefan Hajnoczi void qemu_using_vnet_hdr(NetClientState *nc, bool enable); 194d6085e3aSStefan Hajnoczi void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6, 1951f55ac45SVincenzo Maffione int ecn, int ufo); 196*481c5232SAkihiko Odaki int qemu_get_vnet_hdr_len(NetClientState *nc); 197d6085e3aSStefan Hajnoczi void qemu_set_vnet_hdr_len(NetClientState *nc, int len); 198c80cd6bbSGreg Kurz int qemu_set_vnet_le(NetClientState *nc, bool is_le); 199c80cd6bbSGreg Kurz int qemu_set_vnet_be(NetClientState *nc, bool is_be); 2001422e32dSPaolo Bonzini void qemu_macaddr_default_if_unset(MACAddr *macaddr); 2011422e32dSPaolo Bonzini int qemu_show_nic_models(const char *arg, const char *const *models); 2021422e32dSPaolo Bonzini void qemu_check_nic_model(NICInfo *nd, const char *model); 2031422e32dSPaolo Bonzini int qemu_find_nic_model(NICInfo *nd, const char * const *models, 2041422e32dSPaolo Bonzini const char *default_model); 2051422e32dSPaolo Bonzini 2061422e32dSPaolo Bonzini void print_net_client(Monitor *mon, NetClientState *nc); 20716a3df40SZhang Chen void net_socket_rs_init(SocketReadState *rs, 2083cde5ea2SZhang Chen SocketReadStateFinalize *finalize, 2093cde5ea2SZhang Chen bool vnet_hdr); 2100165daaeSCindy Lu NetClientState *qemu_get_peer(NetClientState *nc, int queue_index); 2111422e32dSPaolo Bonzini 212c6941b3bSThomas Huth /** 213c6941b3bSThomas Huth * qemu_get_nic_models: 214c6941b3bSThomas Huth * @device_type: Defines which devices should be taken into consideration 215c6941b3bSThomas Huth * (e.g. TYPE_DEVICE for all devices, or TYPE_PCI_DEVICE for PCI) 216c6941b3bSThomas Huth * 217c6941b3bSThomas Huth * Get an array of pointers to names of NIC devices that are available in 218c6941b3bSThomas Huth * the QEMU binary. The array is terminated with a NULL pointer entry. 219c6941b3bSThomas Huth * The caller is responsible for freeing the memory when it is not required 220c6941b3bSThomas Huth * anymore, e.g. with g_ptr_array_free(..., true). 221c6941b3bSThomas Huth * 222c6941b3bSThomas Huth * Returns: Pointer to the array that contains the pointers to the names. 223c6941b3bSThomas Huth */ 224c6941b3bSThomas Huth GPtrArray *qemu_get_nic_models(const char *device_type); 225c6941b3bSThomas Huth 2261422e32dSPaolo Bonzini /* NIC info */ 2271422e32dSPaolo Bonzini 2281422e32dSPaolo Bonzini #define MAX_NICS 8 2291422e32dSPaolo Bonzini 2301422e32dSPaolo Bonzini struct NICInfo { 2311422e32dSPaolo Bonzini MACAddr macaddr; 2321422e32dSPaolo Bonzini char *model; 2331422e32dSPaolo Bonzini char *name; 2341422e32dSPaolo Bonzini char *devaddr; 2351422e32dSPaolo Bonzini NetClientState *netdev; 2361422e32dSPaolo Bonzini int used; /* is this slot in nd_table[] being used? */ 2371422e32dSPaolo Bonzini int instantiated; /* does this NICInfo correspond to an instantiated NIC? */ 2381422e32dSPaolo Bonzini int nvectors; 2391422e32dSPaolo Bonzini }; 2401422e32dSPaolo Bonzini 2411422e32dSPaolo Bonzini extern int nb_nics; 2421422e32dSPaolo Bonzini extern NICInfo nd_table[MAX_NICS]; 24384007e81SHani Benhabiles extern const char *host_net_devices[]; 2441422e32dSPaolo Bonzini 2451422e32dSPaolo Bonzini /* from net.c */ 246ae71d13dSMarkus Armbruster extern NetClientStateList net_clients; 247f3eedcddSLaurent Vivier bool netdev_is_modern(const char *optarg); 248f3eedcddSLaurent Vivier void netdev_parse_modern(const char *optarg); 24921fccb2cSLaurent Vivier void net_client_parse(QemuOptsList *opts_list, const char *str); 250ad6f932fSPaolo Bonzini void show_netdevs(void); 251d63ef17bSLaurent Vivier void net_init_clients(void); 2521422e32dSPaolo Bonzini void net_check_clients(void); 2531422e32dSPaolo Bonzini void net_cleanup(void); 2543e5a50d6SMarkus Armbruster void hmp_host_net_add(Monitor *mon, const QDict *qdict); 2553e5a50d6SMarkus Armbruster void hmp_host_net_remove(Monitor *mon, const QDict *qdict); 2561422e32dSPaolo Bonzini void netdev_add(QemuOpts *opts, Error **errp); 2571422e32dSPaolo Bonzini 2581422e32dSPaolo Bonzini int net_hub_id_for_client(NetClientState *nc, int *id); 2591422e32dSPaolo Bonzini NetClientState *net_hub_port_find(int hub_id); 2601422e32dSPaolo Bonzini 26163c4db4cSPaolo Bonzini #define DEFAULT_NETWORK_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifup" 26263c4db4cSPaolo Bonzini #define DEFAULT_NETWORK_DOWN_SCRIPT CONFIG_SYSCONFDIR "/qemu-ifdown" 2631422e32dSPaolo Bonzini #define DEFAULT_BRIDGE_HELPER CONFIG_QEMU_HELPERDIR "/qemu-bridge-helper" 2641422e32dSPaolo Bonzini #define DEFAULT_BRIDGE_INTERFACE "br0" 2651422e32dSPaolo Bonzini 2661422e32dSPaolo Bonzini void qdev_set_nic_properties(DeviceState *dev, NICInfo *nd); 2671422e32dSPaolo Bonzini 268eaba8f34SMark Cave-Ayland #define POLYNOMIAL_BE 0x04c11db6 269f1a7deb9SMark Cave-Ayland #define POLYNOMIAL_LE 0xedb88320 270eaba8f34SMark Cave-Ayland uint32_t net_crc32(const uint8_t *p, int len); 271f1a7deb9SMark Cave-Ayland uint32_t net_crc32_le(const uint8_t *p, int len); 2721422e32dSPaolo Bonzini 2731422e32dSPaolo Bonzini #define vmstate_offset_macaddr(_state, _field) \ 2741422e32dSPaolo Bonzini vmstate_offset_array(_state, _field.a, uint8_t, \ 2751422e32dSPaolo Bonzini sizeof(typeof_field(_state, _field))) 2761422e32dSPaolo Bonzini 2771422e32dSPaolo Bonzini #define VMSTATE_MACADDR(_field, _state) { \ 2781422e32dSPaolo Bonzini .name = (stringify(_field)), \ 2791422e32dSPaolo Bonzini .size = sizeof(MACAddr), \ 2801422e32dSPaolo Bonzini .info = &vmstate_info_buffer, \ 2811422e32dSPaolo Bonzini .flags = VMS_BUFFER, \ 2821422e32dSPaolo Bonzini .offset = vmstate_offset_macaddr(_state, _field), \ 2831422e32dSPaolo Bonzini } 2841422e32dSPaolo Bonzini 285bc38e31bSJason Wang static inline bool net_peer_needs_padding(NetClientState *nc) 286bc38e31bSJason Wang { 287bc38e31bSJason Wang return nc->peer && !nc->peer->do_not_pad; 288bc38e31bSJason Wang } 289bc38e31bSJason Wang 2901422e32dSPaolo Bonzini #endif 291