VMXNET3 paravirtual NIC device implementation
Subsystems (2)
v5 cover VMXNET3 paravirtual NIC device implementation
This set of patches implements VMWare VMXNET3 paravirtual NIC device. The device supports of all the device features including offload capabilties, VLANs and etc.
The device is tested on different OSes:
Fedora 15
Ubuntu 10.4
Centos 6.2
Windows 2008R2
Windows 2008 64bit
Windows 2008 32bit
Windows 2003 64bit
Windows 2003 32bitChanges in V5:
MSI-X save/load implemented in the device instead of pci bus as suggested by Michael S. Tsirkin
Patches regrouped as suggested by Paolo Bonzini
Changes in V4:
Fixed a few problems uncovered by NETIO test suit Assertion on failure to initialize MSI/MSI-X replaced with warning message and fallback to Legacy/MSI respectively
Various coding style adjustments and patch split-up as suggested by Anthony
Liguori
Live migration support added
Changes in V3:
Fixed crash when net device that is used as network fronted has no virtio HDR support.
Task offloads emulation for cases when net device that is used as network fronted has no virtio HDR support.
Changes in V2:
License text changed accoring to community suggestions Standard license header from GPLv2+ - licensed QEMU files used
Dmitry Fleytman (7):
Adding missing flag VIRTIO_NET_HDR_F_DATA_VALID from Linux kernel
source tre Reformatting comments according to checkpatch.pl
requirements
Adding utility function net_checksum_add_cont() that allows checksum
calculation of scattered data with odd chunk sizes
Adding utility function iov_net_csum_add() for iovec checksum
calculation
Header with various utility functions shared by VMWARE SCSI and
network devices
Various utility functions used by VMWARE network devices Packet abstraction used by VMWARE network devices
VMXNET3 paravirtualized device implementation Interface type
"vmxnet3" added.
Makefile.objs | 1 + default-configs/pci.mak | 1 + hw/pci.c | 2 + hw/pci.h | 1 + hw/virtio-net.h | 13 +- hw/vmware_utils.h | 126 +++ hw/vmxnet3.c | 2454 +++++++++++++++++++++++++++++++++++++++++++++++ hw/vmxnet3.h | 757 +++++++++++++++ hw/vmxnet_debug.h | 121 +++ hw/vmxnet_pkt.c | 1243 ++++++++++++++++++++++++ hw/vmxnet_pkt.h | 479 +++++++++ hw/vmxnet_utils.c | 165 ++++ hw/vmxnet_utils.h | 320 ++++++ iov.c | 29 + iov.h | 3 + net.c | 2 +- net/checksum.c | 13 +- net/checksum.h | 14 +- 18 files changed, 5730 insertions(+), 14 deletions(-) create mode 100644 hw/vmware_utils.h create mode 100644 hw/vmxnet3.c create mode 100644 hw/vmxnet3.h create mode 100644 hw/vmxnet_debug.h create mode 100644 hw/vmxnet_pkt.c create mode 100644 hw/vmxnet_pkt.h create mode 100644 hw/vmxnet_utils.c create mode 100644 hw/vmxnet_utils.h
Signature · 1 line
1.7.7.6
PATCH v5 1/7 Adding missing flag VIRTIO_NET_HDR_F_DATA_VALID from Linux kernel source tre Reformatting comments according to checkpatch.pl requirements
--- hw/virtio-net.h | 13 +++++++------ 1 files changed, 7 insertions(+), 6 deletions(-)
hw/virtio-net.h +7 -6
@@ -78,13 +78,14 @@ struct virtio_net_config* specify GSO or CSUM features, you can simply ignore the header. */ struct virtio_net_hdr { -#define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 // Use csum_start, csum_offset +#define VIRTIO_NET_HDR_F_NEEDS_CSUM 1/* Use csum_start, csum_offset */ +#define VIRTIO_NET_HDR_F_DATA_VALID 2/* Csum is valid */ uint8_t flags; -#define VIRTIO_NET_HDR_GSO_NONE 0 // Not a GSO frame -#define VIRTIO_NET_HDR_GSO_TCPV4 1 // GSO frame, IPv4 TCP (TSO) -#define VIRTIO_NET_HDR_GSO_UDP 3 // GSO frame, IPv4 UDP (UFO) -#define VIRTIO_NET_HDR_GSO_TCPV6 4 // GSO frame, IPv6 TCP -#define VIRTIO_NET_HDR_GSO_ECN 0x80 // TCP has ECN set +#define VIRTIO_NET_HDR_GSO_NONE 0/* Not a GSO frame */ +#define VIRTIO_NET_HDR_GSO_TCPV4 1/* GSO frame, IPv4 TCP (TSO) */ +#define VIRTIO_NET_HDR_GSO_UDP 3/* GSO frame, IPv4 UDP (UFO) */ +#define VIRTIO_NET_HDR_GSO_TCPV6 4/* GSO frame, IPv6 TCP */ +#define VIRTIO_NET_HDR_GSO_ECN 0x80/* TCP has ECN set */ uint8_t gso_type;uint16_t hdr_len;uint16_t gso_size;
Signature · 1 line
1.7.7.6
PATCH v5 2/7 Adding utility function net_checksum_add_cont() that allows checksum calculation of scattered data with odd chunk sizes
In reply to Dmitry FleytmanAdding utility function net_raw_checksum() that calculates checksum of buffer given
--- net/checksum.c | 13 +++++++------ net/checksum.h | 14 +++++++++++++- 2 files changed, 20 insertions(+), 7 deletions(-)
net/checksum.c +7 -6
@@ -20,16 +20,17 @@#define PROTO_TCP 6#define PROTO_UDP 17 -uint32_t net_checksum_add (int len,uint8_t *buf) +uint32_t net_checksum_add_cont (int len,uint8_t *buf,int seq) {uint32_t sum =0 ;int i; -for (i =0 ; i < len; i++) { -if (i &1 ) - sum += (uint32_t )buf[i]; -else - sum += (uint32_t )buf[i] <<8 ; +for (i = seq; i < seq + len; i++) { +if (i &1 ) { + sum += (uint32_t )buf[i - seq]; + }else { + sum += (uint32_t )buf[i - seq] <<8 ; + } }return sum; }
net/checksum.h +13 -1
@@ -20,10 +20,22 @@#include <stdint.h> -uint32_t net_checksum_add (int len,uint8_t *buf); +uint32_t net_checksum_add_cont (int len,uint8_t *buf,int seq);uint16_t net_checksum_finish (uint32_t sum);uint16_t net_checksum_tcpudp (uint16_t length,uint16_t proto,uint8_t *addrs,uint8_t *buf);void net_checksum_calculate (uint8_t *data,int length); +static inline uint32_t +net_checksum_add (int len,uint8_t *buf) +{ +return net_checksum_add_cont (len, buf,0 ); +} + +static inline uint16_t +net_raw_checksum (uint8_t *data,int length) +{ +return net_checksum_finish (net_checksum_add (length, data)); +} +#endif /* QEMU_NET_CHECKSUM_H */
Signature · 1 line
1.7.7.6
PATCH v5 3/7 Adding utility function iov_net_csum_add() for iovec checksum calculation
In reply to Dmitry Fleytman--- iov.c | 29 +++++++++++++++++++++++++++++ iov.h | 3 +++ 2 files changed, 32 insertions(+), 0 deletions(-)
iov.c +29 -0
@@ -16,6 +16,7 @@*/ #include "iov.h" +#include "net/checksum.h" size_t iov_from_buf (struct iovec *iov,unsigned int iov_cnt,const void *buf,size_t iov_off,size_t size)
@@ -130,3 +131,31 @@ void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,fprintf (fp,"\n" ); } } + +uint32_t +iov_net_csum_add (const struct iovec *iov,const unsigned int iov_cnt, +size_t iov_off,size_t size) +{ +size_t iovec_off, buf_off; +unsigned int i; +uint32_t res =0 ; +uint32_t seq =0 ; + + iovec_off =0 ; + buf_off =0 ; +for (i =0 ; i < iov_cnt && size; i++) { +if (iov_off < (iovec_off + iov[i].iov_len)) { +size_t len =MIN ((iovec_off + iov[i].iov_len) - iov_off , size); +void *chunk_buf = iov[i].iov_base + (iov_off - iovec_off); + + res +=net_checksum_add_cont (len, chunk_buf, seq); + seq += len; + + buf_off += len; + iov_off += len; + size -= len; + } + iovec_off += iov[i].iov_len; + } +return res; +}
iov.h +3 -0
@@ -21,3 +21,6 @@ size_t iov_clear(const struct iovec *iov, const unsigned int iov_cnt, size_t iov_off, size_t size);void iov_hexdump (const struct iovec *iov,const unsigned int iov_cnt,FILE *fp,const char *prefix,size_t limit); +uint32_t +iov_net_csum_add (const struct iovec *iov,const unsigned int iov_cnt, +size_t iov_off,size_t size);
Signature · 1 line
1.7.7.6
PATCH v5 4/7 Header with various utility functions shared by VMWARE SCSI and network devices
In reply to Dmitry Fleytman--- hw/vmware_utils.h | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 126 insertions(+), 0 deletions(-) create mode 100644 hw/vmware_utils.h
hw/vmware_utils.h new file +126 -0
@@ -0,0 +1,126 @@ +/* +* QEMU VMWARE paravirtual devices - auxiliary code +* +* Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com) +* +* Developed by Daynix Computing LTD (http://www.daynix.com) +* +* Authors: +* Dmitry Fleytman <[email protected]> +* Yan Vugenfirer <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef VMWARE_UTILS_H +#define VMWARE_UTILS_H + +#ifndef DSHPRINTF +#define DSHPRINTF (fmt, ...) do {} while (0) +#endif + +/* Shared memory access functions with byte swap support */ +static inline void +vmw_shmem_read (target_phys_addr_t addr,void *buf,int len) +{ +DSHPRINTF ("SHMEM r: %" PRIx64", len: %d to %p" , addr, len, buf); +cpu_physical_memory_read (addr, buf, len); +} + +static inline void +vmw_shmem_write (target_phys_addr_t addr,void *buf,int len) +{ +DSHPRINTF ("SHMEM w: %" PRIx64", len: %d to %p" , addr, len, buf); +cpu_physical_memory_write (addr, buf, len); +} + +static inline void +vmw_shmem_rw (target_phys_addr_t addr,void *buf,int len,int is_write) +{ +DSHPRINTF ("SHMEM r/w: %" PRIx64", len: %d (to %p), is write: %d" , + addr, len, buf, is_write); + +cpu_physical_memory_rw (addr, buf, len, is_write); +} + +static inline void +vmw_shmem_set (target_phys_addr_t addr,uint8 val,int len) +{ +int i; +DSHPRINTF ("SHMEM set: %" PRIx64", len: %d (value 0x%X)" , addr, len, val); + +for (i =0 ; i < len; i++) { +cpu_physical_memory_write (addr + i, &val,1 ); + } +} + +static inline uint32_t +vmw_shmem_ld8 (target_phys_addr_t addr) +{ +uint8_t res =ldub_phys (addr); +DSHPRINTF ("SHMEM load8: %" PRIx64" (value 0x%X)" , addr, res); +return res; +} + +static inline void +vmw_shmem_st8 (target_phys_addr_t addr,uint8_t value) +{ +DSHPRINTF ("SHMEM store8: %" PRIx64" (value 0x%X)" , addr, value); +stb_phys (addr, value); +} + +static inline uint32_t +vmw_shmem_ld16 (target_phys_addr_t addr) +{ +uint16_t res =lduw_le_phys (addr); +DSHPRINTF ("SHMEM load16: %" PRIx64" (value 0x%X)" , addr, res); +return res; +} + +static inline void +vmw_shmem_st16 (target_phys_addr_t addr,uint16_t value) +{ +DSHPRINTF ("SHMEM store16: %" PRIx64" (value 0x%X)" , addr, value); +stw_le_phys (addr, value); +} + +static inline uint32_t +vmw_shmem_ld32 (target_phys_addr_t addr) +{ +uint32_t res =ldl_le_phys (addr); +DSHPRINTF ("SHMEM load32: %" PRIx64" (value 0x%X)" , addr, res); +return res; +} + +static inline void +vmw_shmem_st32 (target_phys_addr_t addr,uint32_t value) +{ +DSHPRINTF ("SHMEM store32: %" PRIx64" (value 0x%X)" , addr, value); +stl_le_phys (addr, value); +} + +static inline uint64_t +vmw_shmem_ld64 (target_phys_addr_t addr) +{ +uint64_t res =ldq_le_phys (addr); +DSHPRINTF ("SHMEM load64: %" PRIx64" (value %" PRIx64")" , addr, res); +return res; +} + +static inline void +vmw_shmem_st64 (target_phys_addr_t addr,uint64_t value) +{ +DSHPRINTF ("SHMEM store64: %" PRIx64" (value %" PRIx64")" , addr, value); +stq_le_phys (addr, value); +} + +/* MACROS for simplification of operations on array-style registers */ +#define IS_MULTIREG_ADDR (addr, base, cnt, regsize) \ + (((addr) >= (base)) && ((addr) < (base) + (cnt) * (regsize))) + +#define MULTIREG_IDX_BY_ADDR (addr, base, regsize) \ + (((addr) - (base)) / (regsize)) + +#endif
Signature · 1 line
1.7.7.6
PATCH v5 5/7 Various utility functions used by VMWARE network devices
In reply to Dmitry Fleytman--- hw/vmxnet_debug.h | 121 ++++++++++++++++++++ hw/vmxnet_utils.c | 165 +++++++++++++++++++++++++++ hw/vmxnet_utils.h | 320 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 606 insertions(+), 0 deletions(-) create mode 100644 hw/vmxnet_debug.h create mode 100644 hw/vmxnet_utils.c create mode 100644 hw/vmxnet_utils.h
hw/vmxnet_debug.h new file +121 -0
@@ -0,0 +1,121 @@ +/* +* QEMU VMWARE VMXNET* paravirtual NICs - debugging facilities +* +* Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com) +* +* Developed by Daynix Computing LTD (http://www.daynix.com) +* +* Authors: +* Dmitry Fleytman <[email protected]> +* Tamir Shomer <[email protected]> +* Yan Vugenfirer <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef _QEMU_VMXNET_DEBUG_H +#define _QEMU_VMXNET_DEBUG_H + +#ifdef VMXNET_VERSION_2 +#define VMXNET_DEVICE_NAME "vmxnet" +#elif definedVMXNET_VERSION_3 +#define VMXNET_DEVICE_NAME "vmxnet3" +#else +#error "VMXNET version is not defined" +#endif + +/* #define DEBUG_VMXNET_CB */ +#define DEBUG_VMXNET_WARNINGS +#define DEBUG_VMXNET_ERRORS +/* #define DEBUG_VMXNET_INTERRUPTS */ +/* #define DEBUG_VMXNET_CONFIG */ +/* #define DEBUG_VMXNET_RINGS */ +/* #define DEBUG_VMXNET_PACKETS */ +/* #define DEBUG_VMXNET_SHMEM_ACCESS */ + +#ifdef DEBUG_VMXNET_SHMEM_ACCESS +#define DSHPRINTF (fmt, ...) \ + do { \ + printf("[%s][SH][%s]: " fmt "\n", VMXNET_DEVICE_NAME, __func__, \ + ## __VA_ARGS__); \ + } while (0) +#else +#define DSHPRINTF (fmt, ...) do {} while (0) +#endif + +#ifdef DEBUG_VMXNET_CB +#define DCBPRINTF (fmt, ...) \ + do { \ + printf("[%s][CB][%s]: " fmt "\n", VMXNET_DEVICE_NAME, __func__, \ + ## __VA_ARGS__); \ + } while (0) +#else +#define DCBPRINTF (fmt, ...) do {} while (0) +#endif + +#ifdef DEBUG_VMXNET_PACKETS +#define DPKPRINTF (fmt, ...) \ + do { \ + printf("[%s][PK][%s]: " fmt "\n", VMXNET_DEVICE_NAME, __func__, \ + ## __VA_ARGS__); \ + } while (0) +#else +#define DPKPRINTF (fmt, ...) do {} while (0) +#endif + +#ifdef DEBUG_VMXNET_WARNINGS +#define DWRPRINTF (fmt, ...) \ + do { \ + printf("[%s][WR][%s]: " fmt "\n", VMXNET_DEVICE_NAME, __func__, \ + ## __VA_ARGS__); \ + } while (0) +#else +#define DWRPRINTF (fmt, ...) do {} while (0) +#endif + +#ifdef DEBUG_VMXNET_ERRORS +#define DERPRINTF (fmt, ...) \ + do { \ + printf("[%s][ER][%s]: " fmt "\n", VMXNET_DEVICE_NAME, __func__, \ + ## __VA_ARGS__); \ + } while (0) +#else +#define DERPRINTF (fmt, ...) do {} while (0) +#endif + +#ifdef DEBUG_VMXNET_INTERRUPTS +#define DIRPRINTF (fmt, ...) \ + do { \ + printf("[%s][IR][%s]: " fmt "\n", VMXNET_DEVICE_NAME, __func__, \ + ## __VA_ARGS__); \ + } while (0) +#else +#define DIRPRINTF (fmt, ...) do {} while (0) +#endif + +#ifdef DEBUG_VMXNET_CONFIG +#define DCFPRINTF (fmt, ...) \ + do { \ + printf("[%s][CF][%s]: " fmt "\n", VMXNET_DEVICE_NAME, __func__, \ + ## __VA_ARGS__); \ + } while (0) +#else +#define DCFPRINTF (fmt, ...) do {} while (0) +#endif + +#ifdef DEBUG_VMXNET_RINGS +#define DRIPRINTF (fmt, ...) \ + do { \ + printf("[%s][RI][%s]: " fmt "\n", VMXNET_DEVICE_NAME, __func__, \ + ## __VA_ARGS__); \ + } while (0) +#else +#define DRIPRINTF (fmt, ...) do {} while (0) +#endif + +#define MAC_FMT "%02X:%02X:%02X:%02X:%02X:%02X" +#define MAC_ARG (a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] + +#endif /* _QEMU_VMXNET3_DEBUG_H */
hw/vmxnet_utils.c new file +165 -0
165 lines not on this page · raw message
hw/vmxnet_utils.h new file +320 -0
320 lines not on this page · raw message
Signature · 1 line
1.7.7.6
PATCH v5 6/7 Packet abstraction used by VMWARE network devices
In reply to Dmitry Fleytman--- hw/vmxnet_pkt.c | 1243 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vmxnet_pkt.h | 479 +++++++++++++++++++++ 2 files changed, 1722 insertions(+), 0 deletions(-) create mode 100644 hw/vmxnet_pkt.c create mode 100644 hw/vmxnet_pkt.h
hw/vmxnet_pkt.c new file +1243 -0
@@ -0,0 +1,1243 @@ +/* +* QEMU VMWARE VMXNET* paravirtual NICs - packets abstractions +* +* Copyright (c) 2012 Ravello Systems LTD (http://ravellosystems.com) +* +* Developed by Daynix Computing LTD (http://www.daynix.com) +* +* Authors: +* Dmitry Fleytman <[email protected]> +* Tamir Shomer <[email protected]> +* Yan Vugenfirer <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#include "vmxnet_pkt.h" +#include "vmxnet_utils.h" +#include "iov.h" + +#include "net/checksum.h" + +/*============================================================================= +*============================================================================= +* +* TX CODE +* +*============================================================================= +*===========================================================================*/ + +enum { +VMXNET_TX_PKT_VHDR_FRAG =0 , +VMXNET_TX_PKT_L2HDR_FRAG , +VMXNET_TX_PKT_L3HDR_FRAG , +VMXNET_TX_PKT_PL_START_FRAG +}; + +/* TX packet private context */ +typedef struct _Vmxnet_TxPkt { +struct virtio_net_hdr virt_hdr; +bool has_virt_hdr; + +struct iovec *vec; + +uint8_t l2_hdr[ETH_MAX_L2_HDR_LEN ]; +uint8_t l3_hdr[ETH_MAX_L3_HDR_LEN ]; + +uint32_t payload_len; + +uint32_t payload_frags; +uint32_t max_payload_frags; + +uint16_t hdr_len; +eth_pkt_types_e packet_type; +uint16_t l3_proto; +}Vmxnet_TxPkt ; + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_init +* +* Desc: Init function for tx packet functionality. +* +* Params: (OUT) pkt - private handle. +* (IN) max_frags - max tx ip fragments. +* (IN) has_virt_hdr - device uses virtio header. +* +* Return: 0 on success, -1 on error +* +* Scope: Global +* +*****************************************************************************/ +int vmxnet_tx_pkt_init (Vmxnet_TxPkt_h *pkt,uint32_t max_frags, +bool has_virt_hdr) +{ +int rc =0 ; + +Vmxnet_TxPkt *p =g_malloc (sizeof *p); +if (!p) { + rc =-1 ; + goto Exit; + } + +memset (p,0 ,sizeof *p); + + p->vec =g_malloc ((sizeof *p->vec) * + (max_frags +VMXNET_TX_PKT_PL_START_FRAG )); +if (!p->vec) { + rc =-1 ; + goto Exit; + } + + p->max_payload_frags = max_frags; + p->has_virt_hdr = has_virt_hdr; + p->vec[VMXNET_TX_PKT_VHDR_FRAG ].iov_base = &p->virt_hdr; + p->vec[VMXNET_TX_PKT_VHDR_FRAG ].iov_len = + p->has_virt_hdr ?sizeof p->virt_hdr :0 ; + p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_base = &p->l2_hdr; + p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_base = &p->l3_hdr; + + *pkt = p; + +Exit: +if (rc) { +vmxnet_tx_pkt_uninit (p); + } +return rc; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_uninit +* +* Desc: Clean all tx packet resources. +* +* Params: (IN) pkt - private handle. +* +* Return: nothing +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_tx_pkt_uninit (Vmxnet_TxPkt_h pkt) +{ +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; + +if (p) { +if (p->vec) { +g_free (p->vec); + } + +g_free (p); + } +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_update_ip_checksums +* +* Desc: fix ip header fields and calculate checksums needed. +* +* Params: (IN) pkt - private handle. +* +* Return: Nothing. +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_tx_pkt_update_ip_checksums (Vmxnet_TxPkt_h pkt) +{ +uint16_t csum; +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +assert (p); +uint8_t gso_type = p->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN ; +struct ip_header *ip_hdr; +target_phys_addr_t payload = (target_phys_addr_t ) + (uint64_t ) p->vec[VMXNET_TX_PKT_PL_START_FRAG ].iov_base; + +if (VIRTIO_NET_HDR_GSO_TCPV4 != gso_type && +VIRTIO_NET_HDR_GSO_UDP != gso_type) { +return ; + } + + ip_hdr = p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_base; + +if (p->payload_len + p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_len > +ETH_MAX_IP_DGRAM_LEN ) { +return ; + } + + ip_hdr->ip_len =cpu_to_be16 (p->payload_len + + p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_len); + +/* Calculate IP header checksum */ + ip_hdr->ip_sum =0 ; + csum =net_raw_checksum ((uint8_t *)ip_hdr, + p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_len); + ip_hdr->ip_sum =cpu_to_be16 (csum); + +/* Calculate IP pseudo header checksum */ + csum =cpu_to_be16 (eth_calc_pseudo_hdr_csum (ip_hdr, p->payload_len)); +cpu_physical_memory_write (payload + p->virt_hdr.csum_offset, + &csum,sizeof (csum)); +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_get_l4_proto +* +* Desc: get l4 protocol. +* +* Params: (IN) p - module context +* +* Return: l4 protocol +* +* Scope: Local +* +*****************************************************************************/ +static uint8_t vmxnet_tx_pkt_get_l4_proto (Vmxnet_TxPkt *p) +{ +struct ip_header *ip_hdr; +struct ip6_header *ip6_hdr; + +if (!p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_len) { +return 0 ; + } + +if (ETH_P_IP == p->l3_proto) { + ip_hdr = p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_base; +return ip_hdr->ip_p; + }else if (p->l3_proto ==ETH_P_IPV6 ) { + ip6_hdr = p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_base; +return ip6_hdr->ip6_nxt; + } + +return 0 ; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_calculate_hdr_len +* +* Desc: store l2 and l3 headers length. +* +* Params: (IN) p - module context +* +* Return: nothing +* +* Scope: Local +* +*****************************************************************************/ +static void vmxnet_tx_pkt_calculate_hdr_len (Vmxnet_TxPkt *p) +{ + p->hdr_len = p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_len + + p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_len; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_prepare +* +* Desc: populates headers and parses them to gether some metadata for later +* use. +* +* Params: (IN) pkt - private handle. +* (IN) pa - physical address of tx data +* (IN) len - length of data +* +* Return: number of bytes populated by the function. +* +* Scope: Global +* +*****************************************************************************/ +size_t vmxnet_tx_pkt_prepare (Vmxnet_TxPkt_h pkt,target_phys_addr_t pa, +size_t len) +{ +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +/* some pointers that my lines stay nice and short. */ +void *l2_iov_base =NULL , *l3_iov_base =NULL ; +size_t *l2_iov_len =NULL , *l3_iov_len =NULL ; +assert (p); + + l2_iov_base = p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_base; + l2_iov_len = &p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_len; + l3_iov_base = p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_base; + l3_iov_len = &p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_len; + +cpu_physical_memory_read (pa, l2_iov_base,ETH_MAX_L2_HDR_LEN ); + + *l2_iov_len =eth_get_l2_hdr_length (l2_iov_base); + + p->l3_proto =eth_get_l3_proto (l2_iov_base, *l2_iov_len); + +switch (p->l3_proto) { +case ETH_P_IP : +assert (len >= *l2_iov_len +sizeof (struct ip_header )); + +cpu_physical_memory_read (pa + *l2_iov_len, l3_iov_base, +sizeof (struct ip_header )); + *l3_iov_len =IP_HDR_GET_LEN (l3_iov_base); + +/* handle optional header data */ +if (*l3_iov_len >sizeof (struct ip_header )) { +cpu_physical_memory_read ( + pa + *l2_iov_len +sizeof (struct ip_header ), + l3_iov_base +sizeof (struct ip_header ), + *l3_iov_len -sizeof (struct ip_header )); + } +break ; + +case ETH_P_IPV6 : + *l3_iov_len =sizeof (struct ip6_header ); +if (len < *l2_iov_len + *l3_iov_len) { +/* +* no ipv6 header in this buffer. can happen in vmxnet, +* when zero copy is enabled +*/ + *l3_iov_len =0 ; +break ; + } +cpu_physical_memory_read (pa + *l2_iov_len, l3_iov_base, *l3_iov_len); +break ; + +default : + *l3_iov_len =0 ; +break ; + } + +vmxnet_tx_pkt_calculate_hdr_len (p); + p->packet_type =get_eth_packet_type (l2_iov_base); + +/* Return amount of data populated into vec */ +return *l2_iov_len + *l3_iov_len; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_get_vhdr +* +* Desc: get virtio header. +* +* Params: (IN) pkt - private handle. +* +* Return: virtio header +* +* Scope: Global +* +*****************************************************************************/ +struct virtio_net_hdr *vmxnet_tx_pkt_get_vhdr (Vmxnet_TxPkt_h pkt) +{ +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +assert (p); +return &p->virt_hdr; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_get_gso_type +* +* Desc: calculate gso type. +* +* Params: (IN) pkt - private handle. +* (IN) tso_enable +* +* Return: gso type. +* +* Scope: Local +* +*****************************************************************************/ +static uint8_t vmxnet_tx_pkt_get_gso_type (Vmxnet_TxPkt *p,bool tso_enable) +{ +uint8_t rc =VIRTIO_NET_HDR_GSO_NONE ; +uint16_t l3_proto; + + l3_proto =eth_get_l3_proto (p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_base, + p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_len); + +if (!tso_enable) { + goto Exit; + } + + rc =eth_get_gso_type (l3_proto, p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_base); + +Exit: +return rc; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_build_vheader +* +* Desc: build virtio header. (will be stored in module context) +* +* Params: (IN) pkt - private handle. +* (IN) tso_enable +* (IN) csum_enable (has to be enabled if tso_enable is true) +* (IN) gso_size - (relevent only if tso enabled) +* +* Return: nothing +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_tx_pkt_build_vheader (Vmxnet_TxPkt_h pkt,bool tso_enable, +bool csum_enable,uint32_t gso_size) +{ +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +struct tcp_hdr l4hdr; +target_phys_addr_t l4hdr_pa; +assert (p); + +/* csum has to be enabled if tso is. */ +assert (csum_enable || !tso_enable); + + p->virt_hdr.gso_type =vmxnet_tx_pkt_get_gso_type (p, tso_enable); + +switch (p->virt_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN ) { +case VIRTIO_NET_HDR_GSO_NONE : + p->virt_hdr.hdr_len =0 ; + p->virt_hdr.gso_size =0 ; +break ; + +case VIRTIO_NET_HDR_GSO_UDP : + p->virt_hdr.gso_size =IP_FRAG_ALIGN_SIZE (gso_size); + p->virt_hdr.hdr_len = p->hdr_len +sizeof (struct udp_header ); +break ; + +case VIRTIO_NET_HDR_GSO_TCPV4 : +case VIRTIO_NET_HDR_GSO_TCPV6 : + l4hdr_pa = (target_phys_addr_t ) + (uint64_t ) p->vec[VMXNET_TX_PKT_PL_START_FRAG ].iov_base; +cpu_physical_memory_read (l4hdr_pa, &l4hdr,sizeof (l4hdr)); + p->virt_hdr.hdr_len = p->hdr_len + l4hdr.th_off *sizeof (uint32_t ); + p->virt_hdr.gso_size =IP_FRAG_ALIGN_SIZE (gso_size); +break ; + +default : +assert (false); + } + +if (csum_enable) { + p->virt_hdr.flags =VIRTIO_NET_HDR_F_NEEDS_CSUM ; + + p->virt_hdr.csum_start = p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_len + + p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_len; +switch (vmxnet_tx_pkt_get_l4_proto (p)) { +case IP_PROTO_TCP : + p->virt_hdr.csum_offset = offsetof(struct tcp_hdr , th_sum); +break ; +case IP_PROTO_UDP : + p->virt_hdr.csum_offset = offsetof(struct udp_hdr , uh_sum); +break ; +default : +assert (false); +break ; + } + } +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_setup_vlan_header +* +* Desc: updates vlan tag, and adds vlan header in case it is missing. +* +* Params: (IN) pkt - private handle. +* (IN) vlan +* +* Return: Nothing +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_tx_pkt_setup_vlan_header (Vmxnet_TxPkt_h pkt,uint16_t vlan) +{ +bool is_new; +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +assert (p); + +eth_setup_vlan_headers (p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_base, + vlan, &is_new); + +/* update l2hdrlen */ +if (is_new) { + p->hdr_len +=sizeof (struct vlan_header ); + p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_len +=sizeof (struct vlan_header ); + } +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_add_fragment +* +* Desc: populate fragment into pkt context. +* +* Params: (IN) pkt - private handle. +* (IN) pa - physical address of fragment +* (IN) len - length of fragment +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_tx_pkt_add_fragment (Vmxnet_TxPkt_h pkt,target_phys_addr_t pa, +size_t len) +{ +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +int abs_frag_num = p->payload_frags +VMXNET_TX_PKT_PL_START_FRAG ; +assert (p); + +if (!len) { +return ; + } + +assert (p->max_payload_frags > p->payload_frags); + + p->vec[abs_frag_num].iov_base = (void *) (uint64_t ) pa; + p->vec[abs_frag_num].iov_len = len; + p->payload_frags++; + p->payload_len += len; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_get_packet_type +* +* Desc: returns packet type +* +* Params: (IN) pkt - private handle. +* +* Return: packet type. +* +* Scope: Global +* +*****************************************************************************/ +eth_pkt_types_e vmxnet_tx_pkt_get_packet_type (Vmxnet_TxPkt_h pkt) +{ +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +assert (p); + +return p->packet_type; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_get_total_len +* +* Desc: Returns the length of all populated data. +* +* Params: (IN) pkt - private handle. +* +* Return: length of populated data. +* +* Scope: Global +* +*****************************************************************************/ +size_t vmxnet_tx_pkt_get_total_len (Vmxnet_TxPkt_h pkt) +{ +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +assert (p); + +return p->hdr_len + p->payload_len; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_unmap +* +* Desc: unmap all populated headers and fragments from iovec. +* +* Params: (IN) p - private context. +* +* Return: Nothing. +* +* Scope: Local +* +*****************************************************************************/ +static void vmxnet_tx_pkt_unmap (Vmxnet_TxPkt *p) +{ +int i; + +for (i =VMXNET_TX_PKT_PL_START_FRAG ; + i < p->payload_frags +VMXNET_TX_PKT_PL_START_FRAG ; i++) { +cpu_physical_memory_unmap (p->vec[i].iov_base, p->vec[i].iov_len, + false, p->vec[i].iov_len); + } +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_map +* +* Desc: map all populated headers and fragments into iovec. +* +* Params: (IN) p - private context. +* (OUT) mapped_fragments - length of iovec. +* +* Return: iovec head. NULL on error. +* +* Scope: local +* +*****************************************************************************/ +static void *vmxnet_tx_pkt_map (Vmxnet_TxPkt *p,uint32_t *mapped_fragments) +{ +int i; + +for (i =VMXNET_TX_PKT_PL_START_FRAG ; + i < p->payload_frags +VMXNET_TX_PKT_PL_START_FRAG ; i++) { +target_phys_addr_t mapped_len = p->vec[i].iov_len; +size_t orig_len = p->vec[i].iov_len; + p->vec[i].iov_base = +cpu_physical_memory_map ((target_phys_addr_t ) (uint64_t ) + p->vec[i].iov_base, &mapped_len, false); + p->vec[i].iov_len = mapped_len; + +if ((NULL == p->vec[i].iov_base) || (orig_len != mapped_len)) { + p->payload_frags = i + !!p->vec[i].iov_base; +vmxnet_tx_pkt_unmap (p); +return NULL ; + } + } + + *mapped_fragments = p->payload_frags +VMXNET_TX_PKT_PL_START_FRAG ; +return p->vec; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_dump +* +* Desc: Prints packet data if debug is enabled +* +* Params: (IN) pkt - private handle. +* +* Return: Nothing. +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_tx_pkt_dump (Vmxnet_TxPkt_h pkt) +{ +#ifdef VMXNET_TX_PKT_DEBUG +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +assert (p); + +printf ("TX PKT: hdr_len: %d, pkt_type: 0x%X, l2hdr_len: %lu, " +"l3hdr_len: %lu, payload_len: %u\n" , p->hdr_len, p->packet_type, + p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_len, + p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_len, p->payload_len); +#endif +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_reset +* +* Desc: reset tx packet private context. (needed to be called between packets) +* +* Params: (IN) pkt - private handle. +* +* Return: Nothing. +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_tx_pkt_reset (Vmxnet_TxPkt_h pkt) +{ +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +int i; + +/* no assert, as reset can be called before tx_pkt_init */ +if (!p) { +return ; + } + +memset (&p->virt_hdr,0 ,sizeof (p->virt_hdr)); + +assert (p->vec); +for (i =VMXNET_TX_PKT_L2HDR_FRAG ; i < p->max_payload_frags; i++) { + p->vec[i].iov_len =0 ; + } + + p->payload_len =0 ; + p->payload_frags =0 ; + p->hdr_len =0 ; + p->packet_type =0 ; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_do_sw_csum +* +* Desc: Calculate l4 csum and put it in hdr. +* +* Params: (IN) p - private handle. +* +* Return: nothing. +* +* Scope: Local +* +*****************************************************************************/ +static void vmxnet_tx_pkt_do_sw_csum (Vmxnet_TxPkt *p) +{ +struct iovec *iov = &p->vec[VMXNET_TX_PKT_L2HDR_FRAG ]; +uint32_t csum_cntr; +uint16_t csum =0 ; +/* num of iovec without vhdr */ +uint32_t iov_len = p->payload_frags +VMXNET_TX_PKT_PL_START_FRAG -1 ; +uint16_t csl; +struct ip_header *iphdr; +size_t csum_offset = p->virt_hdr.csum_start + p->virt_hdr.csum_offset; + +/* Put zero to checksum field */ +iov_from_buf (iov, iov_len, &csum, csum_offset,sizeof csum); + +/* Calculate L4 TCP/UDP checksum */ + csl = p->payload_len; + +/* data checksum */ + csum_cntr =iov_net_csum_add (iov, iov_len, p->virt_hdr.csum_start, csl); +/* add pseudo header to csum */ + iphdr = p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_base; + csum_cntr +=eth_calc_pseudo_hdr_csum (iphdr, csl); + +/* Put the checksum obtained into the packet */ + csum =cpu_to_be16 (net_checksum_finish (csum_cntr)); +iov_from_buf (iov, iov_len, &csum, csum_offset,sizeof csum); +} + +enum { +VMXNET_TX_PKT_FRAGMENT_L2_HDR_POS =0 , +VMXNET_TX_PKT_FRAGMENT_L3_HDR_POS , +VMXNET_TX_PKT_FRAGMENT_HEADER_NUM +}; + +#define VMXNET_MAX_FRAG_SG_LIST (64) + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_fetch_fragment +* +* Desc: Copy data from packet to fragment iov. +* +* Params: (IN) p - private context. +* (IN/OUT) src_idx - current index in packet iov +* (IN/OUT) src_offset - current offset in iov member +* (OUT) dst - fragment iov +* (OUT) dst_idx - the used length of the fragment iov +* (+1 as this is an index) +* +* Return: number of bytes sent. +* +* Scope: Local +* +*****************************************************************************/ +static size_t vmxnet_tx_pkt_fetch_fragment (Vmxnet_TxPkt *p,int *src_idx, +size_t *src_offset,struct iovec *dst,int *dst_idx) +{ +size_t fetched =0 ; +struct iovec *src = p->vec; + + *dst_idx =VMXNET_TX_PKT_FRAGMENT_HEADER_NUM ; + +while (fetched < p->virt_hdr.gso_size) { + +/* no more place in fragment iov */ +if (*dst_idx ==VMXNET_MAX_FRAG_SG_LIST ) { +break ; + } + +/* no more data in iovec */ +if (*src_idx == (p->payload_frags +VMXNET_TX_PKT_PL_START_FRAG )) { +break ; + } + + + dst[*dst_idx].iov_base = src[*src_idx].iov_base + *src_offset; + dst[*dst_idx].iov_len =MIN (src[*src_idx].iov_len - *src_offset, + p->virt_hdr.gso_size - fetched); + + *src_offset += dst[*dst_idx].iov_len; + fetched += dst[*dst_idx].iov_len; + +if (*src_offset == src[*src_idx].iov_len) { + *src_offset =0 ; + (*src_idx)++; + } + + (*dst_idx)++; + } + +return fetched; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_do_sw_fragmentation +* +* Desc: Preformes softwere fragmentation of the packet and sends in to qemu +* +* Params: (IN) p - private context. +* (IN) vc - VLANClientState. +* +* Return: number of bytes sent. +* +* Scope: Local +* +*****************************************************************************/ +static size_t vmxnet_tx_pkt_do_sw_fragmentation (Vmxnet_TxPkt *p, +VLANClientState *vc) +{ +struct iovec fragment[VMXNET_MAX_FRAG_SG_LIST ]; +size_t fragment_len =0 ; +bool more_frags = false; +/* some poiners for shorter code */ +void *l2_iov_base, *l3_iov_base; +size_t l2_iov_len, l3_iov_len; +int src_idx =VMXNET_TX_PKT_PL_START_FRAG , dst_idx; +size_t src_offset =0 ; +size_t bytes_sent =0 ; +size_t fragment_offset =0 ; + + l2_iov_base = p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_base; + l2_iov_len = p->vec[VMXNET_TX_PKT_L2HDR_FRAG ].iov_len; + l3_iov_base = p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_base; + l3_iov_len = p->vec[VMXNET_TX_PKT_L3HDR_FRAG ].iov_len; + +/* Copy headers */ + fragment[VMXNET_TX_PKT_FRAGMENT_L2_HDR_POS ].iov_base = l2_iov_base; + fragment[VMXNET_TX_PKT_FRAGMENT_L2_HDR_POS ].iov_len = l2_iov_len; + fragment[VMXNET_TX_PKT_FRAGMENT_L3_HDR_POS ].iov_base = l3_iov_base; + fragment[VMXNET_TX_PKT_FRAGMENT_L3_HDR_POS ].iov_len = l3_iov_len; + + +/* Put as much data as possible and send */ +do { + fragment_len =vmxnet_tx_pkt_fetch_fragment (p, &src_idx, &src_offset, + fragment, &dst_idx); + + more_frags = (fragment_offset + fragment_len < p->payload_len); + +eth_setup_ip4_fragmentation (l2_iov_base, l2_iov_len, l3_iov_base, + l3_iov_len, fragment_len, fragment_offset, more_frags); + +eth_fix_ip4_checksum (l3_iov_base, l3_iov_len); + + bytes_sent +=qemu_sendv_packet (vc, fragment, dst_idx); + + fragment_offset += fragment_len; + + }while (more_frags); + +return bytes_sent; +} + +/****************************************************************************** +* +* Function: vmxnet_tx_pkt_send +* +* Desc: Send packet to qemu. handles sw offloads if vhdr is not supported. +* +* Params: (IN) pkt - private handle. +* (IN) vc - VLANClientState. +* +* Return: number of bytes sent. +* +* Scope: Global +* +*****************************************************************************/ +size_t vmxnet_tx_pkt_send (Vmxnet_TxPkt_h pkt,VLANClientState *vc) +{ +void *iovec =NULL ; +uint32_t iovec_size; +size_t bytes_sent =0 ; +Vmxnet_TxPkt *p = (Vmxnet_TxPkt *)pkt; +assert (p); + + iovec =vmxnet_tx_pkt_map (p, &iovec_size); +if (!iovec) { + goto Exit; + } + +if (!p->has_virt_hdr && p->virt_hdr.flags &VIRTIO_NET_HDR_F_NEEDS_CSUM ) { +vmxnet_tx_pkt_do_sw_csum (p); + } + +if (p->has_virt_hdr ||VIRTIO_NET_HDR_GSO_NONE == p->virt_hdr.gso_type) { + bytes_sent =qemu_sendv_packet (vc, iovec, iovec_size); + goto Exit; + } + + bytes_sent =vmxnet_tx_pkt_do_sw_fragmentation (p, vc); + +Exit: +vmxnet_tx_pkt_unmap (p); +return bytes_sent; +} + +/*============================================================================= +*============================================================================= +* +* RX CODE +* +*============================================================================= +*===========================================================================*/ + +/* +* RX packet may contain up to 2 fragments - rebuilt eth header +* in case of VLAN tag stripping +* and payload received from QEMU - in any case +*/ +#define VMXNET_MAX_RX_PACKET_FRAGMENTS (2) + +typedef struct Vmxnet_RxPkt { +struct virtio_net_hdr virt_hdr; +struct eth_header eth_hdr; +struct iovec vec[VMXNET_MAX_RX_PACKET_FRAGMENTS ]; +uint16 vec_len; +uint32_t tot_len; +uint16_t vlan_tag; +bool vlan_stripped; +bool has_virt_hdr; +eth_pkt_types_e packet_type; +}Vmxnet_RxPkt ; + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_init +* +* Desc: Init function for rx packet functionality. +* +* Params: (OUT) pkt - private handle. +* (IN) has_virt_hdr - device uses virtio header. +* +* Return: 0 on success, -1 on error +* +* Scope: Global +* +*****************************************************************************/ +int vmxnet_rx_pkt_init (Vmxnet_RxPkt_h *pkt,bool has_virt_hdr) +{ +int rc =0 ; + +Vmxnet_RxPkt *p =g_malloc (sizeof *p); +if (!p) { + rc =-1 ; + goto Exit; + } + +memset (p,0 ,sizeof *p); + p->has_virt_hdr = has_virt_hdr; + + *pkt = p; + +Exit: +if (rc) { +vmxnet_rx_pkt_uninit (p); + } +return rc; +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_uninit +* +* Desc: Clean all rx packet resources. +* +* Params: (IN) pkt - private handle. +* +* Return: nothing +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_rx_pkt_uninit (Vmxnet_RxPkt_h pkt) +{ +if (pkt) { +g_free (pkt); + } +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_get_vhdr +* +* Desc: returns virtio header stored in rx context. +* +* Params: (IN) pkt - private handle. +* +* Return: virtio header +* +* Scope: Global +* +*****************************************************************************/ +struct virtio_net_hdr *vmxnet_rx_pkt_get_vhdr (Vmxnet_RxPkt_h pkt) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); +return &p->virt_hdr; +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_attach_data +* +* Desc: Attach data to rx packet. +* +* Params: (IN) pkt - private handle. +* (IN) data +* (IN) len +* (IN) strip_vlan - should the module strip vlan from data. +* +* Return: nothing. +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_rx_pkt_attach_data (Vmxnet_RxPkt_h pkt,const void *data,size_t len, +bool strip_vlan) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +uint16_t vtag =0 ; +uint16_t ploff; +assert (p); + p->vlan_stripped = false; + +if (strip_vlan) { + p->vlan_stripped =eth_strip_vlan (&data, &p->eth_hdr, &ploff, &vtag); + } + +if (p->vlan_stripped) { + p->vec[0 ].iov_base = &p->eth_hdr; + p->vec[0 ].iov_len =sizeof p->eth_hdr; + p->vec[1 ].iov_base = (uint8_t *) data + ploff; + p->vec[1 ].iov_len = len - ploff; + p->vec_len =2 ; + p->tot_len = len - ploff +sizeof (struct eth_header ); + }else { + p->vec[0 ].iov_base = (void *)data; + p->vec[0 ].iov_len = len; + p->vec_len =1 ; + p->tot_len = len; + } + + p->vlan_tag = vtag; +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_dump +* +* Desc: prints rx packet data if debug is enabled. +* +* Params: (IN) pkt - private handle. +* +* Return: Nothing. +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_rx_pkt_dump (Vmxnet_RxPkt_h pkt) +{ +#ifdef VMXNET_RX_PKT_DEBUG +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + +printf ("RX PKT: tot_len: %d, vlan_stripped: %d, vlan_tag: %d\n" , + p->tot_len, p->vlan_stripped, p->vlan_tag); +#endif +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_set_packet_type +* +* Desc: save packet type in packet context +* +* Params: (IN) pkt - private handle. +* (IN) packet_type. +* +* Return: Nothing. +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_rx_pkt_set_packet_type (Vmxnet_RxPkt_h pkt, +eth_pkt_types_e packet_type) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + + p->packet_type = packet_type; + +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_get_packet_type +* +* Desc: returns packet type. +* +* Params: (IN) pkt - private handle. +* +* Return: packet type. +* +* Scope: Global +* +*****************************************************************************/ +eth_pkt_types_e vmxnet_rx_pkt_get_packet_type (Vmxnet_RxPkt_h pkt) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + +return p->packet_type; +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_get_total_len +* +* Desc: returns total length of data attached to rx context. +* +* Params: (IN) pkt - private handle. +* +* Return: nothing +* +* Scope: Global +* +*****************************************************************************/ +size_t vmxnet_rx_pkt_get_total_len (Vmxnet_RxPkt_h pkt) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + +return p->tot_len; +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_get_iovec +* +* Desc: returns io vector that holds the attached data. +* +* Params: (IN) pkt - private handle. +* +* Return: iovector. +* +* Scope: Global +* +*****************************************************************************/ +struct iovec *vmxnet_rx_pkt_get_iovec (Vmxnet_RxPkt_h pkt) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + +return p->vec; +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_set_vhdr +* +* Desc: copy passed phdr data to packet context. +* +* Params: (IN) pkt - private handle. +* (IN) vhdr - new vhdr data. +* +* Return: Nothing. +* +* Scope: Global +* +*****************************************************************************/ +void vmxnet_rx_pkt_set_vhdr (Vmxnet_RxPkt_h pkt,struct virtio_net_hdr *vhdr) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + +memcpy (&p->virt_hdr, vhdr,sizeof p->virt_hdr); +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_is_vlan_stripped +* +* Desc: notifies caller if a vlan was stripped from the packet. +* +* Params: (IN) pkt - private handle. +* +* Return: true if vlan vas stripped form the packet, false otherwize. +* +* Scope: Global +* +*****************************************************************************/ +bool vmxnet_rx_pkt_is_vlan_stripped (Vmxnet_RxPkt_h pkt) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + +return p->vlan_stripped; +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_has_virt_hdr +* +* Desc: notifies caller if the packet has virtio header +* +* Params: (IN) pkt - private handle. +* +* Return: true if packet has virtio header, false otherwize. +* +* Scope: Global +* +*****************************************************************************/ +bool vmxnet_rx_pkt_has_virt_hdr (Vmxnet_RxPkt_h pkt) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + +return p->has_virt_hdr; +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_get_num_frags +* +* Desc: returns number of frags attached to the packet. +* +* Params: (IN) pkt - private handle. +* +* Return: number of frags. +* +* Scope: Global +* +*****************************************************************************/ +uint16_t vmxnet_rx_pkt_get_num_frags (Vmxnet_RxPkt_h pkt) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + +return p->vec_len; +} + +/****************************************************************************** +* +* Function: vmxnet_rx_pkt_get_vlan_tag +* +* Desc: returns vlan tag. +* +* Params: (IN) pkt - private handle. +* +* Return: vlan tag. +* +* Scope: Global +* +*****************************************************************************/ +uint16_t vmxnet_rx_pkt_get_vlan_tag (Vmxnet_RxPkt_h pkt) +{ +Vmxnet_RxPkt *p = (Vmxnet_RxPkt *)pkt; +assert (p); + +return p->vlan_tag; +}
hw/vmxnet_pkt.h new file +479 -0
479 lines not on this page · raw message
Signature · 1 line
1.7.7.6
PATCH v5 7/7 VMXNET3 paravirtualized device implementation Interface type "vmxnet3" added.
In reply to Dmitry Fleytman--- Makefile.objs | 1 + default-configs/pci.mak | 1 + hw/pci.c | 2 + hw/pci.h | 1 + hw/vmxnet3.c | 2454 +++++++++++++++++++++++++++++++++++++++++++++++ hw/vmxnet3.h | 757 +++++++++++++++ net.c | 2 +- 7 files changed, 3217 insertions(+), 1 deletions(-) create mode 100644 hw/vmxnet3.c create mode 100644 hw/vmxnet3.h
Makefile.objs +1 -0
@@ -284,6 +284,7 @@ hw-obj-$(CONFIG_PCNET_PCI) += pcnet-pci.o hw-obj-$(CONFIG_PCNET_COMMON) += pcnet.o hw-obj-$(CONFIG_E1000_PCI) += e1000.o hw-obj-$(CONFIG_RTL8139_PCI) += rtl8139.o +hw-obj-$(CONFIG_VMXNET3_PCI) += vmxnet3.o vmxnet_utils.o vmxnet_pkt.o hw-obj-$(CONFIG_SMC91C111) += smc91c111.o hw-obj-$(CONFIG_LAN9118) += lan9118.o
default-configs/pci.mak +1 -0
@@ -13,6 +13,7 @@ CONFIG_PCNET_COMMON=y CONFIG_LSI_SCSI_PCI=y CONFIG_RTL8139_PCI=y CONFIG_E1000_PCI=y +CONFIG_VMXNET3_PCI=y CONFIG_IDE_CORE=y CONFIG_IDE_QDEV=y CONFIG_IDE_PCI=y
hw/pci.c +2 -0
@@ -1350,6 +1350,7 @@ static const char * const pci_nic_models[] = {"e1000" ,"pcnet" ,"virtio" , +"vmxnet3" , NULL };
@@ -1362,6 +1363,7 @@ static const char * const pci_nic_names[] = {"e1000" ,"pcnet" ,"virtio-net-pci" , +"vmxnet3" , NULL };
hw/pci.h +1 -0
@@ -60,6 +60,7 @@#define PCI_DEVICE_ID_VMWARE_NET 0x0720#define PCI_DEVICE_ID_VMWARE_SCSI 0x0730#define PCI_DEVICE_ID_VMWARE_IDE 0x1729 +#define PCI_DEVICE_ID_VMWARE_VMXNET3 0x07B0/* Intel (0x8086) */ #define PCI_DEVICE_ID_INTEL_82551IT 0x1209
hw/vmxnet3.c new file +2454 -0
2,454 lines not on this page · raw message
hw/vmxnet3.h new file +757 -0
511 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
1.7.7.6
Part of the diff is not shown here. View the raw message.
Re: [Qemu-devel] [PATCH 7/7 v5] VMXNET3 paravirtualized device implementation Interface type "vmxnet3" added.
Hi,
What about this patch?, everything that was asked from Dmitry was accomplished... What prevent us from progressing with merging this patch?
Thanks.
Quoted 14 lines · Dmitry Fleytman
Signed-off-by Dmitry Fleytman<[email protected]>Signed-off-by Yan Vugenfirer<[email protected]>--- Makefile.objs | 1 + default-configs/pci.mak | 1 + hw/pci.c | 2 + hw/pci.h | 1 + hw/vmxnet3.c | 2454 +++++++++++++++++++++++++++++++++++++++++++++++ hw/vmxnet3.h | 757 +++++++++++++++ net.c | 2 +- 7 files changed, 3217 insertions(+), 1 deletions(-)create mode 100644
hw/vmxnet3.c
create mode 100644hw/vmxnet3.h
Re: [Qemu-devel] [PATCH 7/7 v5] VMXNET3 paravirtualized device implementation Interface type "vmxnet3" added.
Quoted 7 lines · Izik Eidus
Hi,
What about this patch?, everything that was asked from Dmitry was accomplished...
What prevent us from progressing with merging this patch?Thanks.
Off the top of my head: issues with v5:
polluting global namespace, must scope names
appropriately with vmxnet_ VMXNET_ unless they have file scope. Don't use names with _ followed by an upper case letter
or that star with two underscores. Don't mix underscores and mixed case. Don't stick any new types in net.c/pci.c - new devices should use -device not -net. Global stuff like ethernet header size
should move to central place instead of copy paste.
Quoted 15 lines · Dmitry Fleytman
Quoted 14 lines
Signed-off-by Dmitry Fleytman<[email protected]>Signed-off-by Yan Vugenfirer<[email protected]>--- Makefile.objs | 1 + default-configs/pci.mak | 1 + hw/pci.c | 2 + hw/pci.h | 1 + hw/vmxnet3.c | 2454 +++++++++++++++++++++++++++++++++++++++++++++++ hw/vmxnet3.h | 757 +++++++++++++++ net.c | 2 +- 7 files changed, 3217 insertions(+), 1 deletions(-) create mode 100644 hw/vmxnet3.c create mode 100644 hw/vmxnet3.h
Re: [Qemu-devel] [PATCH 7/7 v5] VMXNET3 paravirtualized device implementation Interface type "vmxnet3" added.
In reply to Izik EidusQuoted 2 lines · Izik Eidus
What about this patch?, everything that was asked from Dmitry was accomplished... What prevent us from progressing with merging this patch?
As already discussed on the list patch v5 doesn't work at least for me. Previous patches worked better but were not stable under load.
I'm also appreciating a working version and integrating the patch fast.
Ciao,
Gerhard
Re: [Qemu-devel] [PATCH 7/7 v5] VMXNET3 paravirtualized device implementation Interface type "vmxnet3" added.
Quoted 9 lines · Gerhard Wiesinger
Quoted 3 lines · Izik Eidus
What about this patch?, everything that was asked from Dmitry was accomplished... What prevent us from progressing with merging this patch?
As already discussed on the list patch v5 doesn't work at least for me. Previous patches worked better but were not stable under load.
Quoted 7 lines
I'm also appreciating a working version and integrating the patch fast.
Ciao,
Gerhard
Hello Gerhard,
1. I understand that you send your exact command line to Dmitry and your test scripts, but we cannot reproduce the issues. Can you send us host network configuration?
2. Is it possible for you to run tcpdump on the guest and on the host and send us the files for review?
Best regards,
Yan.
Re: [Qemu-devel] [PATCH 7/7 v5] VMXNET3 paravirtualized device implementation Interface type "vmxnet3" added.
Quoted 18 lines · Yan Vugenfirer
Quoted 9 lines · Gerhard Wiesinger
Quoted 3 lines · Izik Eidus
What about this patch?, everything that was asked from Dmitry was accomplished... What prevent us from progressing with merging this patch?
As already discussed on the list patch v5 doesn't work at least for me. Previous patches worked better but were not stable under load.
Quoted 7 lines
I'm also appreciating a working version and integrating the patch fast.
Ciao,
Gerhard
Quoted 8 lines
Hello Gerhard,
1. I understand that you send your exact command line to Dmitry and your test scripts, but we cannot reproduce the issues. Can you send us host network configuration?
2. Is it possible for you to run tcpdump on the guest and on the host and send us the files for review?
Since there was no difference for v5 patch, I already sent this for v4 patch:
See:
https://lists.gnu.org/archive/html/qemu-devel/2012-03/msg04062.html
https://lists.gnu.org/archive/html/qemu-devel/2012-03/msg04634.html
I also sniffed between working NIC (rtl8139) and non working NIC (vmxnet3) and as already thought in //lists.gnu.org/archive/html/qemu-devel/2012-03/msg04062.html TCP offloading checksum is NOT correct.
Quoted 1 line
From wireshark sniff:
Checksum SYN1: 0xe937 [incorrect, should be 0xe936 (maybe caused by "TCP checksum offload"?)] Checksum SYN2: [incorrect, should be 0xe5af (maybe caused by "TCP checksum offload"?)] => checksum is always too high by 1.
Please fix it.
Ciao,
Gerhard