[dpdk-dev] [PATCH] net: introduce big and little endian types

Ananyev, Konstantin konstantin.ananyev at intel.com
Mon Dec 5 11:09:05 CET 2016


Hi Neilo,

> 
> This commit introduces new rte_{le,be}{16,32,64}_t types and updates
> rte_{le,be,cpu}_to_{le,be,cpu}_*() and network header structures
> accordingly.
> 
> Specific big/little endian types avoid uncertainty and conversion mistakes.
> 
> No ABI change since these are simply typedefs to the original types.

It seems like quite a lot of changes...
Could you probably explain what will be the benefit in return?
Konstantin

> 
> Signed-off-by: Nelio Laranjeiro <nelio.laranjeiro at 6wind.com>
> ---
>  .../common/include/generic/rte_byteorder.h         | 31 +++++++++++-------
>  lib/librte_net/rte_arp.h                           | 15 +++++----
>  lib/librte_net/rte_ether.h                         | 10 +++---
>  lib/librte_net/rte_gre.h                           | 30 ++++++++---------
>  lib/librte_net/rte_icmp.h                          | 11 ++++---
>  lib/librte_net/rte_ip.h                            | 38 +++++++++++-----------
>  lib/librte_net/rte_net.c                           | 10 +++---
>  lib/librte_net/rte_sctp.h                          |  9 ++---
>  lib/librte_net/rte_tcp.h                           | 19 ++++++-----
>  lib/librte_net/rte_udp.h                           |  9 ++---
>  10 files changed, 97 insertions(+), 85 deletions(-)
> 
> diff --git a/lib/librte_eal/common/include/generic/rte_byteorder.h b/lib/librte_eal/common/include/generic/rte_byteorder.h
> index e00bccb..059c2a5 100644
> --- a/lib/librte_eal/common/include/generic/rte_byteorder.h
> +++ b/lib/librte_eal/common/include/generic/rte_byteorder.h
> @@ -75,6 +75,13 @@
>  #define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
>  #endif
> 
> +typedef uint16_t rte_be16_t;
> +typedef uint32_t rte_be32_t;
> +typedef uint64_t rte_be64_t;
> +typedef uint16_t rte_le16_t;
> +typedef uint32_t rte_le32_t;
> +typedef uint64_t rte_le64_t;
> +
>  /*
>   * An internal function to swap bytes in a 16-bit value.
>   *
> @@ -143,65 +150,65 @@ static uint64_t rte_bswap64(uint64_t x);
>  /**
>   * Convert a 16-bit value from CPU order to little endian.
>   */
> -static uint16_t rte_cpu_to_le_16(uint16_t x);
> +static rte_le16_t rte_cpu_to_le_16(uint16_t x);
> 
>  /**
>   * Convert a 32-bit value from CPU order to little endian.
>   */
> -static uint32_t rte_cpu_to_le_32(uint32_t x);
> +static rte_le32_t rte_cpu_to_le_32(uint32_t x);
> 
>  /**
>   * Convert a 64-bit value from CPU order to little endian.
>   */
> -static uint64_t rte_cpu_to_le_64(uint64_t x);
> +static rte_le64_t rte_cpu_to_le_64(uint64_t x);
> 
> 
>  /**
>   * Convert a 16-bit value from CPU order to big endian.
>   */
> -static uint16_t rte_cpu_to_be_16(uint16_t x);
> +static rte_be16_t rte_cpu_to_be_16(uint16_t x);
> 
>  /**
>   * Convert a 32-bit value from CPU order to big endian.
>   */
> -static uint32_t rte_cpu_to_be_32(uint32_t x);
> +static rte_be32_t rte_cpu_to_be_32(uint32_t x);
> 
>  /**
>   * Convert a 64-bit value from CPU order to big endian.
>   */
> -static uint64_t rte_cpu_to_be_64(uint64_t x);
> +static rte_be64_t rte_cpu_to_be_64(uint64_t x);
> 
> 
>  /**
>   * Convert a 16-bit value from little endian to CPU order.
>   */
> -static uint16_t rte_le_to_cpu_16(uint16_t x);
> +static uint16_t rte_le_to_cpu_16(rte_le16_t x);
> 
>  /**
>   * Convert a 32-bit value from little endian to CPU order.
>   */
> -static uint32_t rte_le_to_cpu_32(uint32_t x);
> +static uint32_t rte_le_to_cpu_32(rte_le32_t x);
> 
>  /**
>   * Convert a 64-bit value from little endian to CPU order.
>   */
> -static uint64_t rte_le_to_cpu_64(uint64_t x);
> +static uint64_t rte_le_to_cpu_64(rte_le64_t x);
> 
> 
>  /**
>   * Convert a 16-bit value from big endian to CPU order.
>   */
> -static uint16_t rte_be_to_cpu_16(uint16_t x);
> +static uint16_t rte_be_to_cpu_16(rte_be16_t x);
> 
>  /**
>   * Convert a 32-bit value from big endian to CPU order.
>   */
> -static uint32_t rte_be_to_cpu_32(uint32_t x);
> +static uint32_t rte_be_to_cpu_32(rte_be32_t x);
> 
>  /**
>   * Convert a 64-bit value from big endian to CPU order.
>   */
> -static uint64_t rte_be_to_cpu_64(uint64_t x);
> +static uint64_t rte_be_to_cpu_64(rte_be64_t x);
> 
>  #endif /* __DOXYGEN__ */
> 
> diff --git a/lib/librte_net/rte_arp.h b/lib/librte_net/rte_arp.h
> index 1836418..95f123e 100644
> --- a/lib/librte_net/rte_arp.h
> +++ b/lib/librte_net/rte_arp.h
> @@ -40,6 +40,7 @@
> 
>  #include <stdint.h>
>  #include <rte_ether.h>
> +#include <rte_byteorder.h>
> 
>  #ifdef __cplusplus
>  extern "C" {
> @@ -50,22 +51,22 @@ extern "C" {
>   */
>  struct arp_ipv4 {
>  	struct ether_addr arp_sha;  /**< sender hardware address */
> -	uint32_t          arp_sip;  /**< sender IP address */
> +	rte_be32_t        arp_sip;  /**< sender IP address */
>  	struct ether_addr arp_tha;  /**< target hardware address */
> -	uint32_t          arp_tip;  /**< target IP address */
> +	rte_be32_t        arp_tip;  /**< target IP address */
>  } __attribute__((__packed__));
> 
>  /**
>   * ARP header.
>   */
>  struct arp_hdr {
> -	uint16_t arp_hrd;    /* format of hardware address */
> +	rte_be16_t arp_hrd;  /* format of hardware address */
>  #define ARP_HRD_ETHER     1  /* ARP Ethernet address format */
> 
> -	uint16_t arp_pro;    /* format of protocol address */
> -	uint8_t  arp_hln;    /* length of hardware address */
> -	uint8_t  arp_pln;    /* length of protocol address */
> -	uint16_t arp_op;     /* ARP opcode (command) */
> +	rte_be16_t arp_pro;  /* format of protocol address */
> +	uint8_t    arp_hln;  /* length of hardware address */
> +	uint8_t    arp_pln;  /* length of protocol address */
> +	rte_be16_t arp_op;   /* ARP opcode (command) */
>  #define	ARP_OP_REQUEST    1 /* request to resolve address */
>  #define	ARP_OP_REPLY      2 /* response to previous request */
>  #define	ARP_OP_REVREQUEST 3 /* request proto addr given hardware */
> diff --git a/lib/librte_net/rte_ether.h b/lib/librte_net/rte_ether.h
> index ff3d065..159e061 100644
> --- a/lib/librte_net/rte_ether.h
> +++ b/lib/librte_net/rte_ether.h
> @@ -300,7 +300,7 @@ ether_format_addr(char *buf, uint16_t size,
>  struct ether_hdr {
>  	struct ether_addr d_addr; /**< Destination address. */
>  	struct ether_addr s_addr; /**< Source address. */
> -	uint16_t ether_type;      /**< Frame type. */
> +	rte_be16_t ether_type;    /**< Frame type. */
>  } __attribute__((__packed__));
> 
>  /**
> @@ -309,8 +309,8 @@ struct ether_hdr {
>   * of the encapsulated frame.
>   */
>  struct vlan_hdr {
> -	uint16_t vlan_tci; /**< Priority (3) + CFI (1) + Identifier Code (12) */
> -	uint16_t eth_proto;/**< Ethernet type of encapsulated frame. */
> +	rte_be16_t vlan_tci; /**< Priority (3) + CFI (1) + Identifier Code (12) */
> +	rte_be16_t eth_proto;/**< Ethernet type of encapsulated frame. */
>  } __attribute__((__packed__));
> 
>  /**
> @@ -319,8 +319,8 @@ struct vlan_hdr {
>   * Reserved fields (24 bits and 8 bits)
>   */
>  struct vxlan_hdr {
> -	uint32_t vx_flags; /**< flag (8) + Reserved (24). */
> -	uint32_t vx_vni;   /**< VNI (24) + Reserved (8). */
> +	rte_be32_t vx_flags; /**< flag (8) + Reserved (24). */
> +	rte_be32_t vx_vni;   /**< VNI (24) + Reserved (8). */
>  } __attribute__((__packed__));
> 
>  /* Ethernet frame types */
> diff --git a/lib/librte_net/rte_gre.h b/lib/librte_net/rte_gre.h
> index 46568ff..b651af0 100644
> --- a/lib/librte_net/rte_gre.h
> +++ b/lib/librte_net/rte_gre.h
> @@ -45,23 +45,23 @@ extern "C" {
>   */
>  struct gre_hdr {
>  #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
> -	uint16_t res2:4; /**< Reserved */
> -	uint16_t s:1;    /**< Sequence Number Present bit */
> -	uint16_t k:1;    /**< Key Present bit */
> -	uint16_t res1:1; /**< Reserved */
> -	uint16_t c:1;    /**< Checksum Present bit */
> -	uint16_t ver:3;  /**< Version Number */
> -	uint16_t res3:5; /**< Reserved */
> +	uint16_t res2:4;  /**< Reserved */
> +	uint16_t s:1;     /**< Sequence Number Present bit */
> +	uint16_t k:1;     /**< Key Present bit */
> +	uint16_t res1:1;  /**< Reserved */
> +	uint16_t c:1;     /**< Checksum Present bit */
> +	uint16_t ver:3;   /**< Version Number */
> +	uint16_t res3:5;  /**< Reserved */
>  #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN
> -	uint16_t c:1;    /**< Checksum Present bit */
> -	uint16_t res1:1; /**< Reserved */
> -	uint16_t k:1;    /**< Key Present bit */
> -	uint16_t s:1;    /**< Sequence Number Present bit */
> -	uint16_t res2:4; /**< Reserved */
> -	uint16_t res3:5; /**< Reserved */
> -	uint16_t ver:3;  /**< Version Number */
> +	uint16_t c:1;     /**< Checksum Present bit */
> +	uint16_t res1:1;  /**< Reserved */
> +	uint16_t k:1;     /**< Key Present bit */
> +	uint16_t s:1;     /**< Sequence Number Present bit */
> +	uint16_t res2:4;  /**< Reserved */
> +	uint16_t res3:5;  /**< Reserved */
> +	uint16_t ver:3;   /**< Version Number */
>  #endif
> -	uint16_t proto;  /**< Protocol Type */
> +	rte_be16_t proto; /**< Protocol Type */
>  } __attribute__((__packed__));
> 
>  #ifdef __cplusplus
> diff --git a/lib/librte_net/rte_icmp.h b/lib/librte_net/rte_icmp.h
> index 8b287f6..81bd907 100644
> --- a/lib/librte_net/rte_icmp.h
> +++ b/lib/librte_net/rte_icmp.h
> @@ -74,6 +74,7 @@
>   */
> 
>  #include <stdint.h>
> +#include <rte_byteorder.h>
> 
>  #ifdef __cplusplus
>  extern "C" {
> @@ -83,11 +84,11 @@ extern "C" {
>   * ICMP Header
>   */
>  struct icmp_hdr {
> -	uint8_t  icmp_type;   /* ICMP packet type. */
> -	uint8_t  icmp_code;   /* ICMP packet code. */
> -	uint16_t icmp_cksum;  /* ICMP packet checksum. */
> -	uint16_t icmp_ident;  /* ICMP packet identifier. */
> -	uint16_t icmp_seq_nb; /* ICMP packet sequence number. */
> +	uint8_t  icmp_type;     /* ICMP packet type. */
> +	uint8_t  icmp_code;     /* ICMP packet code. */
> +	rte_be16_t icmp_cksum;  /* ICMP packet checksum. */
> +	rte_be16_t icmp_ident;  /* ICMP packet identifier. */
> +	rte_be16_t icmp_seq_nb; /* ICMP packet sequence number. */
>  } __attribute__((__packed__));
> 
>  /* ICMP packet types */
> diff --git a/lib/librte_net/rte_ip.h b/lib/librte_net/rte_ip.h
> index 4491b86..6f7da36 100644
> --- a/lib/librte_net/rte_ip.h
> +++ b/lib/librte_net/rte_ip.h
> @@ -93,14 +93,14 @@ extern "C" {
>  struct ipv4_hdr {
>  	uint8_t  version_ihl;		/**< version and header length */
>  	uint8_t  type_of_service;	/**< type of service */
> -	uint16_t total_length;		/**< length of packet */
> -	uint16_t packet_id;		/**< packet ID */
> -	uint16_t fragment_offset;	/**< fragmentation offset */
> +	rte_be16_t total_length;	/**< length of packet */
> +	rte_be16_t packet_id;		/**< packet ID */
> +	rte_be16_t fragment_offset;	/**< fragmentation offset */
>  	uint8_t  time_to_live;		/**< time to live */
>  	uint8_t  next_proto_id;		/**< protocol ID */
> -	uint16_t hdr_checksum;		/**< header checksum */
> -	uint32_t src_addr;		/**< source address */
> -	uint32_t dst_addr;		/**< destination address */
> +	rte_be16_t hdr_checksum;	/**< header checksum */
> +	rte_be32_t src_addr;		/**< source address */
> +	rte_be32_t dst_addr;		/**< destination address */
>  } __attribute__((__packed__));
> 
>  /** Create IPv4 address */
> @@ -340,11 +340,11 @@ static inline uint16_t
>  rte_ipv4_phdr_cksum(const struct ipv4_hdr *ipv4_hdr, uint64_t ol_flags)
>  {
>  	struct ipv4_psd_header {
> -		uint32_t src_addr; /* IP address of source host. */
> -		uint32_t dst_addr; /* IP address of destination host. */
> -		uint8_t  zero;     /* zero. */
> -		uint8_t  proto;    /* L4 protocol type. */
> -		uint16_t len;      /* L4 length. */
> +		rte_be32_t src_addr; /* IP address of source host. */
> +		rte_be32_t dst_addr; /* IP address of destination host. */
> +		uint8_t    zero;     /* zero. */
> +		uint8_t    proto;    /* L4 protocol type. */
> +		rte_be16_t len;      /* L4 length. */
>  	} psd_hdr;
> 
>  	psd_hdr.src_addr = ipv4_hdr->src_addr;
> @@ -398,12 +398,12 @@ rte_ipv4_udptcp_cksum(const struct ipv4_hdr *ipv4_hdr, const void *l4_hdr)
>   * IPv6 Header
>   */
>  struct ipv6_hdr {
> -	uint32_t vtc_flow;     /**< IP version, traffic class & flow label. */
> -	uint16_t payload_len;  /**< IP packet length - includes sizeof(ip_header). */
> -	uint8_t  proto;        /**< Protocol, next header. */
> -	uint8_t  hop_limits;   /**< Hop limits. */
> -	uint8_t  src_addr[16]; /**< IP address of source host. */
> -	uint8_t  dst_addr[16]; /**< IP address of destination host(s). */
> +	rte_be32_t vtc_flow;     /**< IP version, traffic class & flow label. */
> +	rte_be16_t payload_len;  /**< IP packet length - includes sizeof(ip_header). */
> +	uint8_t    proto;        /**< Protocol, next header. */
> +	uint8_t    hop_limits;   /**< Hop limits. */
> +	uint8_t    src_addr[16]; /**< IP address of source host. */
> +	uint8_t    dst_addr[16]; /**< IP address of destination host(s). */
>  } __attribute__((__packed__));
> 
>  /**
> @@ -427,8 +427,8 @@ rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
>  {
>  	uint32_t sum;
>  	struct {
> -		uint32_t len;   /* L4 length. */
> -		uint32_t proto; /* L4 protocol - top 3 bytes must be zero */
> +		rte_be32_t len;   /* L4 length. */
> +		rte_be32_t proto; /* L4 protocol - top 3 bytes must be zero */
>  	} psd_hdr;
> 
>  	psd_hdr.proto = (ipv6_hdr->proto << 24);
> diff --git a/lib/librte_net/rte_net.c b/lib/librte_net/rte_net.c
> index a8c7aff..9014ca5 100644
> --- a/lib/librte_net/rte_net.c
> +++ b/lib/librte_net/rte_net.c
> @@ -153,8 +153,8 @@ ptype_inner_l4(uint8_t proto)
> 
>  /* get the tunnel packet type if any, update proto and off. */
>  static uint32_t
> -ptype_tunnel(uint16_t *proto, const struct rte_mbuf *m,
> -	uint32_t *off)
> +ptype_tunnel(rte_be16_t *proto, const struct rte_mbuf *m,
> +	     uint32_t *off)
>  {
>  	switch (*proto) {
>  	case IPPROTO_GRE: {
> @@ -208,8 +208,8 @@ ip4_hlen(const struct ipv4_hdr *hdr)
> 
>  /* parse ipv6 extended headers, update offset and return next proto */
>  static uint16_t
> -skip_ip6_ext(uint16_t proto, const struct rte_mbuf *m, uint32_t *off,
> -	int *frag)
> +skip_ip6_ext(rte_be16_t proto, const struct rte_mbuf *m, uint32_t *off,
> +	     int *frag)
>  {
>  	struct ext_hdr {
>  		uint8_t next_hdr;
> @@ -261,7 +261,7 @@ uint32_t rte_net_get_ptype(const struct rte_mbuf *m,
>  	struct ether_hdr eh_copy;
>  	uint32_t pkt_type = RTE_PTYPE_L2_ETHER;
>  	uint32_t off = 0;
> -	uint16_t proto;
> +	rte_be16_t proto;
> 
>  	if (hdr_lens == NULL)
>  		hdr_lens = &local_hdr_lens;
> diff --git a/lib/librte_net/rte_sctp.h b/lib/librte_net/rte_sctp.h
> index 688e126..8c646c7 100644
> --- a/lib/librte_net/rte_sctp.h
> +++ b/lib/librte_net/rte_sctp.h
> @@ -81,15 +81,16 @@ extern "C" {
>  #endif
> 
>  #include <stdint.h>
> +#include <rte_byteorder.h>
> 
>  /**
>   * SCTP Header
>   */
>  struct sctp_hdr {
> -	uint16_t src_port; /**< Source port. */
> -	uint16_t dst_port; /**< Destin port. */
> -	uint32_t tag;      /**< Validation tag. */
> -	uint32_t cksum;    /**< Checksum. */
> +	rte_be16_t src_port; /**< Source port. */
> +	rte_be16_t dst_port; /**< Destin port. */
> +	rte_be32_t tag;      /**< Validation tag. */
> +	rte_le32_t cksum;    /**< Checksum. */
>  } __attribute__((__packed__));
> 
>  #ifdef __cplusplus
> diff --git a/lib/librte_net/rte_tcp.h b/lib/librte_net/rte_tcp.h
> index 28b61e6..545d4ab 100644
> --- a/lib/librte_net/rte_tcp.h
> +++ b/lib/librte_net/rte_tcp.h
> @@ -77,6 +77,7 @@
>   */
> 
>  #include <stdint.h>
> +#include <rte_byteorder.h>
> 
>  #ifdef __cplusplus
>  extern "C" {
> @@ -86,15 +87,15 @@ extern "C" {
>   * TCP Header
>   */
>  struct tcp_hdr {
> -	uint16_t src_port;  /**< TCP source port. */
> -	uint16_t dst_port;  /**< TCP destination port. */
> -	uint32_t sent_seq;  /**< TX data sequence number. */
> -	uint32_t recv_ack;  /**< RX data acknowledgement sequence number. */
> -	uint8_t  data_off;  /**< Data offset. */
> -	uint8_t  tcp_flags; /**< TCP flags */
> -	uint16_t rx_win;    /**< RX flow control window. */
> -	uint16_t cksum;     /**< TCP checksum. */
> -	uint16_t tcp_urp;   /**< TCP urgent pointer, if any. */
> +	rte_be16_t src_port;  /**< TCP source port. */
> +	rte_be16_t dst_port;  /**< TCP destination port. */
> +	rte_be32_t sent_seq;  /**< TX data sequence number. */
> +	rte_be32_t recv_ack;  /**< RX data acknowledgement sequence number. */
> +	uint8_t    data_off;  /**< Data offset. */
> +	uint8_t    tcp_flags; /**< TCP flags */
> +	rte_be16_t rx_win;    /**< RX flow control window. */
> +	rte_be16_t cksum;     /**< TCP checksum. */
> +	rte_be16_t tcp_urp;   /**< TCP urgent pointer, if any. */
>  } __attribute__((__packed__));
> 
>  #ifdef __cplusplus
> diff --git a/lib/librte_net/rte_udp.h b/lib/librte_net/rte_udp.h
> index bc5be4a..89fdded 100644
> --- a/lib/librte_net/rte_udp.h
> +++ b/lib/librte_net/rte_udp.h
> @@ -77,6 +77,7 @@
>   */
> 
>  #include <stdint.h>
> +#include <rte_byteorder.h>
> 
>  #ifdef __cplusplus
>  extern "C" {
> @@ -86,10 +87,10 @@ extern "C" {
>   * UDP Header
>   */
>  struct udp_hdr {
> -	uint16_t src_port;    /**< UDP source port. */
> -	uint16_t dst_port;    /**< UDP destination port. */
> -	uint16_t dgram_len;   /**< UDP datagram length */
> -	uint16_t dgram_cksum; /**< UDP datagram checksum */
> +	rte_be16_t src_port;    /**< UDP source port. */
> +	rte_be16_t dst_port;    /**< UDP destination port. */
> +	rte_be16_t dgram_len;   /**< UDP datagram length */
> +	rte_be16_t dgram_cksum; /**< UDP datagram checksum */
>  } __attribute__((__packed__));
> 
>  #ifdef __cplusplus
> --
> 2.1.4



More information about the dev mailing list