Skip to contentSkip to conversation

fix packing of structs when building with MSVC

PATCH Superseded Tyler Retzlaff 9 participants 318 messages Mar 20, 2024 – Jan 15, 2025 Page 4 of 4

Subsystems (6)
lore
Share & tools
lore
#311 lore raw

PATCH v11 27/30 net: replace packed attributes

In reply to Andre Muezerie

MSVC struct packing is not compatible with GCC. Replace macro __rte_packed with __rte_packed_begin to push existing pack value and set packing to 1-byte and macro __rte_packed_end to restore the pack value prior to the push.

Macro __rte_packed_end is deliberately utilized to trigger a MSVC compiler warning if no existing packing has been pushed allowing easy identification of locations where the __rte_packed_begin is missing.

This change affects the storage size of a variable of enum rte_ipv6_mc_scope (at least with gcc). It should be OK from an ABI POV
though: there is one (inline) helper using this type, and nothing else in DPDK takes a IPv6 multicast scope as input.

---
 lib/net/rte_arp.h      |  8 ++++----
 lib/net/rte_dtls.h     |  4 ++--
 lib/net/rte_esp.h      |  8 ++++----
 lib/net/rte_geneve.h   |  4 ++--
 lib/net/rte_gre.h      | 16 ++++++++--------
 lib/net/rte_gtp.h      | 20 ++++++++++----------
 lib/net/rte_ib.h       |  4 ++--
 lib/net/rte_icmp.h     | 12 ++++++------
 lib/net/rte_ip4.h      |  4 ++--
 lib/net/rte_ip6.h      | 14 +++++++-------
 lib/net/rte_l2tpv2.h   | 16 ++++++++--------
 lib/net/rte_macsec.h   |  8 ++++----
 lib/net/rte_mpls.h     |  4 ++--
 lib/net/rte_pdcp_hdr.h | 16 ++++++++--------
 lib/net/rte_ppp.h      |  4 ++--
 lib/net/rte_sctp.h     |  4 ++--
 lib/net/rte_tcp.h      |  4 ++--
 lib/net/rte_tls.h      |  4 ++--
 lib/net/rte_udp.h      |  4 ++--
 lib/net/rte_vxlan.h    | 28 ++++++++++++++--------------
 20 files changed, 93 insertions(+), 93 deletions(-)
lib/net/rte_arp.h +4 -4
index 668cea1704..e885a71292 100644
@@ -21,17 +21,17 @@ extern "C" {
 /**
  * ARP header IPv4 payload.
  */
-struct __rte_aligned(2) rte_arp_ipv4 {
+struct __rte_aligned(2) __rte_packed_begin rte_arp_ipv4 {
 	struct rte_ether_addr arp_sha;  /**< sender hardware address */
 	rte_be32_t            arp_sip;  /**< sender IP address */
 	struct rte_ether_addr arp_tha;  /**< target hardware address */
 	rte_be32_t            arp_tip;  /**< target IP address */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * ARP header.
  */
-struct __rte_aligned(2) rte_arp_hdr {
+struct __rte_aligned(2) __rte_packed_begin rte_arp_hdr {
 	rte_be16_t arp_hardware; /**< format of hardware address */
 #define RTE_ARP_HRD_ETHER     1  /**< ARP Ethernet address format */
 
@@ -47,7 +47,7 @@ struct __rte_aligned(2) rte_arp_hdr {
 #define	RTE_ARP_OP_INVREPLY   9  /**< response identifying peer */
 
 	struct rte_arp_ipv4 arp_data;
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * Make a RARP packet based on MAC addr.
lib/net/rte_dtls.h +2 -2
index 246cd8a72d..1dd95ce899 100644
@@ -30,7 +30,7 @@
  * DTLS Header
  */
 __extension__
-struct rte_dtls_hdr {
+struct __rte_packed_begin rte_dtls_hdr {
 	/** Content type of DTLS packet. Defined as RTE_DTLS_TYPE_*. */
 	uint8_t type;
 	/** DTLS Version defined as RTE_DTLS_VERSION*. */
@@ -48,6 +48,6 @@ struct rte_dtls_hdr {
 #endif
 	/** The length (in bytes) of the following DTLS packet. */
 	rte_be16_t length;
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* RTE_DTLS_H */
lib/net/rte_esp.h +4 -4
index 745a9847fe..2a0002f4d9 100644
@@ -16,17 +16,17 @@
 /**
  * ESP Header
  */
-struct rte_esp_hdr {
+struct __rte_packed_begin rte_esp_hdr {
 	rte_be32_t spi;  /**< Security Parameters Index */
 	rte_be32_t seq;  /**< packet sequence number */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * ESP Trailer
  */
-struct rte_esp_tail {
+struct __rte_packed_begin rte_esp_tail {
 	uint8_t pad_len;     /**< number of pad bytes (0-255) */
 	uint8_t next_proto;  /**< IPv4 or IPv6 or next layer header */
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* RTE_ESP_H_ */
lib/net/rte_geneve.h +2 -2
index eb2c85f1e9..f962c587ee 100644
@@ -34,7 +34,7 @@
  * More-bits (optional) variable length options.
  */
 __extension__
-struct rte_geneve_hdr {
+struct __rte_packed_begin rte_geneve_hdr {
 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
 	uint8_t ver:2;		/**< Version. */
 	uint8_t opt_len:6;	/**< Options length. */
@@ -52,7 +52,7 @@ struct rte_geneve_hdr {
 	uint8_t vni[3];		/**< Virtual network identifier. */
 	uint8_t reserved2;	/**< Reserved. */
 	uint32_t opts[];	/**< Variable length options. */
-} __rte_packed;
+} __rte_packed_end;
 
 /* GENEVE ETH next protocol types */
 #define RTE_GENEVE_TYPE_ETH	0x6558 /**< Ethernet Protocol. */
lib/net/rte_gre.h +8 -8
index 1483e1b42d..768c4ce7b5 100644
@@ -23,7 +23,7 @@
  * GRE Header
  */
 __extension__
-struct rte_gre_hdr {
+struct __rte_packed_begin rte_gre_hdr {
 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 	uint16_t res2:4; /**< Reserved */
 	uint16_t s:1;    /**< Sequence Number Present bit */
@@ -42,28 +42,28 @@ struct rte_gre_hdr {
 	uint16_t ver:3;  /**< Version Number */
 #endif
 	rte_be16_t proto;  /**< Protocol Type */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * Optional field checksum in GRE header
  */
-struct rte_gre_hdr_opt_checksum_rsvd {
+struct __rte_packed_begin rte_gre_hdr_opt_checksum_rsvd {
 	rte_be16_t checksum;
 	rte_be16_t reserved1;
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * Optional field key in GRE header
  */
-struct rte_gre_hdr_opt_key {
+struct __rte_packed_begin rte_gre_hdr_opt_key {
 	rte_be32_t key;
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * Optional field sequence in GRE header
  */
-struct rte_gre_hdr_opt_sequence {
+struct __rte_packed_begin rte_gre_hdr_opt_sequence {
 	rte_be32_t sequence;
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* RTE_GRE_H_ */
lib/net/rte_gtp.h +10 -10
index ab06e23a6e..0332d35c16 100644
@@ -24,7 +24,7 @@
  * No optional fields and next extension header.
  */
 __extension__
-struct rte_gtp_hdr {
+struct __rte_packed_begin rte_gtp_hdr {
 	union {
 		uint8_t gtp_hdr_info; /**< GTP header info */
 		struct {
@@ -48,21 +48,21 @@ struct rte_gtp_hdr {
 	uint8_t msg_type;     /**< GTP message type */
 	rte_be16_t plen;      /**< Total payload length */
 	rte_be32_t teid;      /**< Tunnel endpoint ID */
-} __rte_packed;
+} __rte_packed_end;
 
 /* Optional word of GTP header, present if any of E, S, PN is set. */
-struct rte_gtp_hdr_ext_word {
+struct __rte_packed_begin rte_gtp_hdr_ext_word {
 	rte_be16_t sqn;	      /**< Sequence Number. */
 	uint8_t npdu;	      /**< N-PDU number. */
 	uint8_t next_ext;     /**< Next Extension Header Type. */
-}  __rte_packed;
+}  __rte_packed_end;
 
 /**
  * Optional extension for GTP with next_ext set to 0x85
  * defined based on RFC 38415-g30.
  */
 __extension__
-struct rte_gtp_psc_generic_hdr {
+struct __rte_packed_begin rte_gtp_psc_generic_hdr {
 	uint8_t ext_hdr_len;	/**< PDU ext hdr len in multiples of 4 bytes */
 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
 	uint8_t type:4;		/**< PDU type */
@@ -78,14 +78,14 @@ struct rte_gtp_psc_generic_hdr {
 	uint8_t spare:2;	/**< type specific spare bits */
 #endif
 	uint8_t data[0];	/**< variable length data fields */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * Optional extension for GTP with next_ext set to 0x85
  * type0 defined based on RFC 38415-g30
  */
 __extension__
-struct rte_gtp_psc_type0_hdr {
+struct __rte_packed_begin rte_gtp_psc_type0_hdr {
 	uint8_t ext_hdr_len;	/**< PDU ext hdr len in multiples of 4 bytes */
 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
 	uint8_t type:4;		/**< PDU type */
@@ -105,14 +105,14 @@ struct rte_gtp_psc_type0_hdr {
 	uint8_t ppp:1;		/**< Paging policy presence */
 #endif
 	uint8_t data[0];	/**< variable length data fields */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * Optional extension for GTP with next_ext set to 0x85
  * type1 defined based on RFC 38415-g30
  */
 __extension__
-struct rte_gtp_psc_type1_hdr {
+struct __rte_packed_begin rte_gtp_psc_type1_hdr {
 	uint8_t ext_hdr_len;	/**< PDU ext hdr len in multiples of 4 bytes */
 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
 	uint8_t type:4;		/**< PDU type */
@@ -134,7 +134,7 @@ struct rte_gtp_psc_type1_hdr {
 	uint8_t n_delay_ind:1;	/**< N3/N9 delay result presence */
 #endif
 	uint8_t data[0];	/**< variable length data fields */
-} __rte_packed;
+} __rte_packed_end;
 
 /** GTP header length */
 #define RTE_ETHER_GTP_HLEN \
lib/net/rte_ib.h +2 -2
index a551f3753f..f1b455cea0 100644
@@ -22,7 +22,7 @@
  * IB Specification Vol 1-Release-1.4.
  */
 __extension__
-struct rte_ib_bth {
+struct __rte_packed_begin rte_ib_bth {
 	uint8_t	opcode;		/**< Opcode. */
 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 	uint8_t	tver:4;		/**< Transport Header Version. */
@@ -54,7 +54,7 @@ struct rte_ib_bth {
 	uint8_t	rsvd1:7;	/**< Reserved. */
 #endif
 	uint8_t	psn[3];		/**< Packet Sequence Number */
-} __rte_packed;
+} __rte_packed_end;
 
 /** RoCEv2 default port. */
 #define RTE_ROCEV2_DEFAULT_PORT 4791
lib/net/rte_icmp.h +6 -6
index e69d68ab6e..cca73b3733 100644
@@ -21,33 +21,33 @@
 /**
  * ICMP base header
  */
-struct rte_icmp_base_hdr {
+struct __rte_packed_begin rte_icmp_base_hdr {
 	uint8_t type;
 	uint8_t code;
 	rte_be16_t checksum;
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * ICMP echo header
  */
-struct rte_icmp_echo_hdr {
+struct __rte_packed_begin rte_icmp_echo_hdr {
 	struct rte_icmp_base_hdr base;
 	rte_be16_t identifier;
 	rte_be16_t sequence;
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * ICMP Header
  *
  * @see rte_icmp_echo_hdr which is similar.
  */
-struct rte_icmp_hdr {
+struct __rte_packed_begin rte_icmp_hdr {
 	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. */
-} __rte_packed;
+} __rte_packed_end;
 
 /* ICMP packet types */
 #define RTE_ICMP_TYPE_ECHO_REPLY 0
lib/net/rte_ip4.h +2 -2
index f9b8333332..d4b38c513c 100644
@@ -39,7 +39,7 @@ extern "C" {
 /**
  * IPv4 Header
  */
-struct __rte_aligned(2) rte_ipv4_hdr {
+struct __rte_aligned(2) __rte_packed_begin rte_ipv4_hdr {
 	__extension__
 	union {
 		uint8_t version_ihl;    /**< version and header length */
@@ -62,7 +62,7 @@ struct __rte_aligned(2) rte_ipv4_hdr {
 	rte_be16_t hdr_checksum;	/**< header checksum */
 	rte_be32_t src_addr;		/**< source address */
 	rte_be32_t dst_addr;		/**< destination address */
-} __rte_packed;
+} __rte_packed_end;
 
 /** Create IPv4 address */
 #define RTE_IPV4(a, b, c, d) ((uint32_t)(((a) & 0xff) << 24) | \
lib/net/rte_ip6.h +7 -7
index 992ab5ee1f..92558a124a 100644
@@ -358,7 +358,7 @@ enum rte_ipv6_mc_scope {
 	RTE_IPV6_MC_SCOPE_ORGLOCAL = 0x08,
 	/** Global multicast scope. */
 	RTE_IPV6_MC_SCOPE_GLOBAL = 0x0e,
-} __rte_packed;
+};
 
 /**
  * Extract the IPv6 multicast scope value as defined in RFC 4291, section 2.7.
@@ -461,7 +461,7 @@ rte_ether_mcast_from_ipv6(struct rte_ether_addr *mac, const struct rte_ipv6_addr
 /**
  * IPv6 Header
  */
-struct __rte_aligned(2) rte_ipv6_hdr {
+struct __rte_aligned(2) __rte_packed_begin rte_ipv6_hdr {
 	union {
 		rte_be32_t vtc_flow;        /**< IP version, traffic class & flow label. */
 		__extension__
@@ -484,7 +484,7 @@ struct __rte_aligned(2) rte_ipv6_hdr {
 	uint8_t  hop_limits;	/**< Hop limits. */
 	struct rte_ipv6_addr src_addr;	/**< IP address of source host. */
 	struct rte_ipv6_addr dst_addr;	/**< IP address of destination host(s). */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * Check that the IPv6 header version field is valid according to RFC 8200 section 3.
@@ -508,7 +508,7 @@ static inline int rte_ipv6_check_version(const struct rte_ipv6_hdr *ip)
 /**
  * IPv6 Routing Extension Header
  */
-struct __rte_aligned(2) rte_ipv6_routing_ext {
+struct __rte_aligned(2) __rte_packed_begin rte_ipv6_routing_ext {
 	uint8_t next_hdr;			/**< Protocol, next header. */
 	uint8_t hdr_len;			/**< Header length. */
 	uint8_t type;				/**< Extension header type. */
@@ -523,7 +523,7 @@ struct __rte_aligned(2) rte_ipv6_routing_ext {
 		};
 	};
 	/* Next are 128-bit IPv6 address fields to describe segments. */
-} __rte_packed;
+} __rte_packed_end;
 
 /* IPv6 vtc_flow: IPv / TC / flow_label */
 #define RTE_IPV6_HDR_FL_SHIFT 0
@@ -752,12 +752,12 @@ rte_ipv6_udptcp_cksum_mbuf_verify(const struct rte_mbuf *m,
 #define RTE_IPV6_SET_FRAG_DATA(fo, mf)	\
 	(((fo) & RTE_IPV6_EHDR_FO_MASK) | ((mf) & RTE_IPV6_EHDR_MF_MASK))
 
-struct __rte_aligned(2) rte_ipv6_fragment_ext {
+struct __rte_aligned(2) __rte_packed_begin rte_ipv6_fragment_ext {
 	uint8_t next_header;	/**< Next header type */
 	uint8_t reserved;	/**< Reserved */
 	rte_be16_t frag_data;	/**< All fragmentation data */
 	rte_be32_t id;		/**< Packet ID */
-} __rte_packed;
+} __rte_packed_end;
 
 /* IPv6 fragment extension header size */
 #define RTE_IPV6_FRAG_HDR_SIZE	sizeof(struct rte_ipv6_fragment_ext)
lib/net/rte_l2tpv2.h +8 -8
index ac16657856..728dc01506 100644
@@ -125,7 +125,7 @@ struct rte_l2tpv2_common_hdr {
  * L2TPv2 message Header contains all options(length, ns, nr,
  * offset size, offset padding).
  */
-struct rte_l2tpv2_msg_with_all_options {
+struct __rte_packed_begin rte_l2tpv2_msg_with_all_options {
 	rte_be16_t length;		/**< length(16) */
 	rte_be16_t tunnel_id;		/**< tunnel ID(16) */
 	rte_be16_t session_id;		/**< session ID(16) */
@@ -133,20 +133,20 @@ struct rte_l2tpv2_msg_with_all_options {
 	rte_be16_t nr;			/**< Nr(16) */
 	rte_be16_t offset_size;		/**< offset size(16) */
 	uint8_t   *offset_padding;	/**< offset padding(variable length) */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * L2TPv2 message Header contains all options except length(ns, nr,
  * offset size, offset padding).
  */
-struct rte_l2tpv2_msg_without_length {
+struct __rte_packed_begin rte_l2tpv2_msg_without_length {
 	rte_be16_t tunnel_id;		/**< tunnel ID(16) */
 	rte_be16_t session_id;		/**< session ID(16) */
 	rte_be16_t ns;			/**< Ns(16) */
 	rte_be16_t nr;			/**< Nr(16) */
 	rte_be16_t offset_size;		/**< offset size(16) */
 	uint8_t   *offset_padding;	/**< offset padding(variable length) */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * L2TPv2 message Header contains all options except ns_nr(length,
@@ -176,12 +176,12 @@ struct rte_l2tpv2_msg_without_offset {
 /**
  * L2TPv2 message Header contains options offset size and offset padding.
  */
-struct rte_l2tpv2_msg_with_offset {
+struct __rte_packed_begin rte_l2tpv2_msg_with_offset {
 	rte_be16_t tunnel_id;		/**< tunnel ID(16) */
 	rte_be16_t session_id;		/**< session ID(16) */
 	rte_be16_t offset_size;		/**< offset size(16) */
 	uint8_t   *offset_padding;	/**< offset padding(variable length) */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * L2TPv2 message Header contains options ns and nr.
@@ -213,7 +213,7 @@ struct rte_l2tpv2_msg_without_all_options {
 /**
  * L2TPv2 Combined Message Header Format: Common Header + Options
  */
-struct rte_l2tpv2_combined_msg_hdr {
+struct __rte_packed_begin rte_l2tpv2_combined_msg_hdr {
 	struct rte_l2tpv2_common_hdr common; /**< common header */
 	union {
 		/** header with all options */
@@ -233,6 +233,6 @@ struct rte_l2tpv2_combined_msg_hdr {
 		/** header without all options */
 		struct rte_l2tpv2_msg_without_all_options type7;
 	};
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* _RTE_L2TPV2_H_ */
lib/net/rte_macsec.h +4 -4
index beeeb8effe..c694c37b4b 100644
@@ -25,7 +25,7 @@
  * MACsec Header (SecTAG)
  */
 __extension__
-struct rte_macsec_hdr {
+struct __rte_packed_begin rte_macsec_hdr {
 	/**
 	 * Tag control information and Association number of secure channel.
 	 * Various bits of TCI and AN are masked using RTE_MACSEC_TCI_* and RTE_MACSEC_AN_MASK.
@@ -39,7 +39,7 @@ struct rte_macsec_hdr {
 	uint8_t short_length:6; /**< Short Length. */
 #endif
 	rte_be32_t packet_number; /**< Packet number to support replay protection. */
-} __rte_packed;
+} __rte_packed_end;
 
 /** SCI length in MACsec header if present. */
 #define RTE_MACSEC_SCI_LEN 8
@@ -48,8 +48,8 @@ struct rte_macsec_hdr {
  * MACsec SCI header (8 bytes) after the MACsec header
  * which is present if SC bit is set in tci_an.
  */
-struct rte_macsec_sci_hdr {
+struct __rte_packed_begin rte_macsec_sci_hdr {
 	uint8_t sci[RTE_MACSEC_SCI_LEN]; /**< Optional secure channel ID. */
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* RTE_MACSEC_H */
lib/net/rte_mpls.h +2 -2
index 35a356efd3..53614a0b88 100644
@@ -18,7 +18,7 @@
  * MPLS header.
  */
 __extension__
-struct rte_mpls_hdr {
+struct __rte_packed_begin rte_mpls_hdr {
 	rte_be16_t tag_msb; /**< Label(msb). */
 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
 	uint8_t tag_lsb:4;  /**< Label(lsb). */
@@ -30,6 +30,6 @@ struct rte_mpls_hdr {
 	uint8_t tag_lsb:4;  /**< label(lsb) */
 #endif
 	uint8_t  ttl;       /**< Time to live. */
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* RTE_MPLS_H_ */
lib/net/rte_pdcp_hdr.h +8 -8
index c22b66bf93..2e8da1e1d3 100644
@@ -56,7 +56,7 @@ enum rte_pdcp_pdu_type {
  * 6.2.2.1 Data PDU for SRBs
  */
 __extension__
-struct rte_pdcp_cp_data_pdu_sn_12_hdr {
+struct __rte_packed_begin rte_pdcp_cp_data_pdu_sn_12_hdr {
 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 	uint8_t sn_11_8 : 4;	/**< Sequence number bits 8-11 */
 	uint8_t r : 4;		/**< Reserved */
@@ -65,13 +65,13 @@ struct rte_pdcp_cp_data_pdu_sn_12_hdr {
 	uint8_t sn_11_8 : 4;	/**< Sequence number bits 8-11 */
 #endif
 	uint8_t sn_7_0;		/**< Sequence number bits 0-7 */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * 6.2.2.2 Data PDU for DRBs and MRBs with 12 bits PDCP SN
  */
 __extension__
-struct rte_pdcp_up_data_pdu_sn_12_hdr {
+struct __rte_packed_begin rte_pdcp_up_data_pdu_sn_12_hdr {
 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 	uint8_t sn_11_8 : 4;	/**< Sequence number bits 8-11 */
 	uint8_t r : 3;		/**< Reserved */
@@ -82,13 +82,13 @@ struct rte_pdcp_up_data_pdu_sn_12_hdr {
 	uint8_t sn_11_8 : 4;	/**< Sequence number bits 8-11 */
 #endif
 	uint8_t sn_7_0;		/**< Sequence number bits 0-7 */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * 6.2.2.3 Data PDU for DRBs and MRBs with 18 bits PDCP SN
  */
 __extension__
-struct rte_pdcp_up_data_pdu_sn_18_hdr {
+struct __rte_packed_begin rte_pdcp_up_data_pdu_sn_18_hdr {
 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 	uint8_t sn_17_16 : 2;	/**< Sequence number bits 16-17 */
 	uint8_t r : 5;		/**< Reserved */
@@ -100,13 +100,13 @@ struct rte_pdcp_up_data_pdu_sn_18_hdr {
 #endif
 	uint8_t sn_15_8;	/**< Sequence number bits 8-15 */
 	uint8_t sn_7_0;		/**< Sequence number bits 0-7 */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * 6.2.3.1 Control PDU for PDCP status report
  */
 __extension__
-struct rte_pdcp_up_ctrl_pdu_hdr {
+struct __rte_packed_begin rte_pdcp_up_ctrl_pdu_hdr {
 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 	uint8_t r : 4;		/**< Reserved */
 	uint8_t pdu_type : 3;	/**< Control PDU type */
@@ -134,6 +134,6 @@ struct rte_pdcp_up_ctrl_pdu_hdr {
 	 * in the Bitmap is 1.
 	 */
 	uint8_t bitmap[];
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* RTE_PDCP_HDR_H */
lib/net/rte_ppp.h +2 -2
index 63c72a9392..02bfb03c03 100644
@@ -17,10 +17,10 @@
 /**
  * PPP Header
  */
-struct rte_ppp_hdr {
+struct __rte_packed_begin rte_ppp_hdr {
 	uint8_t addr; /**< PPP address(8) */
 	uint8_t ctrl; /**< PPP control(8) */
 	rte_be16_t proto_id; /**< PPP protocol identifier(16) */
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* _RTE_PPP_H_ */
lib/net/rte_sctp.h +2 -2
index e757c57db3..73051b94fd 100644
@@ -21,11 +21,11 @@
 /**
  * SCTP Header
  */
-struct rte_sctp_hdr {
+struct __rte_packed_begin rte_sctp_hdr {
 	rte_be16_t src_port; /**< Source port. */
 	rte_be16_t dst_port; /**< Destin port. */
 	rte_be32_t tag;      /**< Validation tag. */
 	rte_be32_t cksum;    /**< Checksum. */
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* RTE_SCTP_H_ */
lib/net/rte_tcp.h +2 -2
index 1bcacbf038..fb0eb308f5 100644
@@ -21,7 +21,7 @@
 /**
  * TCP Header
  */
-struct rte_tcp_hdr {
+struct __rte_packed_begin rte_tcp_hdr {
 	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. */
@@ -31,7 +31,7 @@ struct rte_tcp_hdr {
 	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. */
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * TCP Flags
lib/net/rte_tls.h +2 -2
index 595567e3e9..f27db3acb1 100644
@@ -28,13 +28,13 @@
  * TLS Header
  */
 __extension__
-struct rte_tls_hdr {
+struct __rte_packed_begin rte_tls_hdr {
 	/** Content type of TLS packet. Defined as RTE_TLS_TYPE_*. */
 	uint8_t type;
 	/** TLS Version defined as RTE_TLS_VERSION*. */
 	rte_be16_t version;
 	/** The length (in bytes) of the following TLS packet. */
 	rte_be16_t length;
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* RTE_TLS_H */
lib/net/rte_udp.h +2 -2
index c01dad9c9b..94f5304e6d 100644
@@ -21,11 +21,11 @@
 /**
  * UDP Header
  */
-struct rte_udp_hdr {
+struct __rte_packed_begin rte_udp_hdr {
 	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 */
-} __rte_packed;
+} __rte_packed_end;
 
 #endif /* RTE_UDP_H_ */
lib/net/rte_vxlan.h +14 -14
index bd1c89835e..f59829b182 100644
@@ -27,13 +27,13 @@
  * Reserved fields (24 bits and 8 bits)
  */
 __extension__ /* no named member in struct */
-struct rte_vxlan_hdr {
+struct __rte_packed_begin rte_vxlan_hdr {
 	union {
 		rte_be32_t vx_flags; /**< flags (8 bits) + extensions (24 bits). */
-		struct {
+		struct __rte_packed_begin {
 			union {
 				uint8_t flags; /**< Default is I bit, others are extensions. */
-				struct {
+				struct __rte_packed_begin {
 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
 					uint8_t flag_g:1,     /**< GBP bit. */
 						flag_rsvd:1,  /*   Reserved. */
@@ -51,11 +51,11 @@ struct rte_vxlan_hdr {
 						flag_rsvd:1,
 						flag_g:1;
 #endif
-				} __rte_packed;
+				} __rte_packed_end;
 			}; /* end of 1st byte */
 			union {
 				uint8_t rsvd0[3]; /* Reserved for extensions. */
-				struct {
+				struct __rte_packed_begin {
 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
 					uint8_t rsvd0_gbp1:1, /*   Reserved. */
 						flag_d:1,     /**< GBP Don't Learn bit. */
@@ -71,7 +71,7 @@ struct rte_vxlan_hdr {
 #endif
 					union {
 						uint16_t policy_id; /**< GBP Identifier. */
-						struct {
+						struct __rte_packed_begin {
 							uint8_t rsvd0_gpe; /* Reserved. */
 							uint8_t proto; /**< GPE Next protocol. */
 								/* 0x01 : IPv4
@@ -79,23 +79,23 @@ struct rte_vxlan_hdr {
 								 * 0x03 : Ethernet
 								 * 0x04 : Network Service Header
 								 */
-						} __rte_packed;
+						} __rte_packed_end;
 					};
-				} __rte_packed;
+				} __rte_packed_end;
 			};
-		} __rte_packed;
+		} __rte_packed_end;
 	}; /* end of 1st 32-bit word */
 	union {
 		rte_be32_t vx_vni; /**< VNI (24 bits) + reserved (8 bits). */
-		struct {
+		struct __rte_packed_begin {
 			uint8_t    vni[3];   /**< VXLAN Identifier. */
 			union {
 				uint8_t    rsvd1;        /**< Reserved. */
 				uint8_t    last_rsvd;    /**< Reserved. */
 			};
-		} __rte_packed;
+		} __rte_packed_end;
 	}; /* end of 2nd 32-bit word */
-} __rte_packed;
+} __rte_packed_end;
 
 /** VXLAN tunnel header length. */
 #define RTE_ETHER_VXLAN_HLEN \
@@ -111,7 +111,7 @@ struct rte_vxlan_hdr {
  * Identifier and Reserved fields (16 bits and 8 bits).
  */
 __extension__ /* no named member in struct */
-struct rte_vxlan_gpe_hdr {
+struct __rte_packed_begin rte_vxlan_gpe_hdr {
 	union {
 		struct {
 			uint8_t vx_flags;    /**< flag (8). */
@@ -127,7 +127,7 @@ struct rte_vxlan_gpe_hdr {
 			uint8_t rsvd1;    /**< Reserved. */
 		};
 	};
-} __rte_packed;
+} __rte_packed_end;
 
 /**
  * @deprecated
Signature · 1 line
2.47.0.vfs.0.3
#312 lore raw

PATCH v11 28/30 pipeline: replace packed attributes

In reply to Andre Muezerie

MSVC struct packing is not compatible with GCC. Replace macro __rte_packed with __rte_packed_begin to push existing pack value and set packing to 1-byte and macro __rte_packed_end to restore the pack value prior to the push.

Macro __rte_packed_end is deliberately utilized to trigger a MSVC compiler warning if no existing packing has been pushed allowing easy identification of locations where the __rte_packed_begin is missing.

---
 lib/pipeline/rte_table_action.c | 64 ++++++++++++++++-----------------
 1 file changed, 32 insertions(+), 32 deletions(-)
lib/pipeline/rte_table_action.c +32 -32
index a431f8f128..3a408e81b8 100644
@@ -60,9 +60,9 @@ lb_cfg_check(struct rte_table_action_lb_config *cfg)
 	return 0;
 }
 
-struct lb_data {
+struct __rte_packed_begin lb_data {
 	uint32_t out[RTE_TABLE_ACTION_LB_TABLE_SIZE];
-} __rte_packed;
+} __rte_packed_end;
 
 static int
 lb_apply(struct lb_data *data,
@@ -356,10 +356,10 @@ tm_cfg_check(struct rte_table_action_tm_config *tm)
 	return 0;
 }
 
-struct tm_data {
+struct __rte_packed_begin tm_data {
 	uint32_t queue_id;
 	uint32_t reserved;
-} __rte_packed;
+} __rte_packed_end;
 
 static int
 tm_apply_check(struct rte_table_action_tm_params *p,
@@ -465,11 +465,11 @@ struct encap_qinq_data {
 	((((uint64_t)(s)) & 0x1LLU) << 8) |                \
 	(((uint64_t)(ttl)) & 0xFFLLU)))
 
-struct __rte_aligned(2) encap_mpls_data {
+struct __rte_aligned(2) __rte_packed_begin encap_mpls_data {
 	struct rte_ether_hdr ether;
 	uint32_t mpls[RTE_TABLE_ACTION_MPLS_LABELS_MAX];
 	uint32_t mpls_count;
-} __rte_packed;
+} __rte_packed_end;
 
 #define PPP_PROTOCOL_IP                                    0x0021
 
@@ -487,42 +487,42 @@ struct encap_pppoe_data {
 
 #define IP_PROTO_UDP                                       17
 
-struct __rte_aligned(2) encap_vxlan_ipv4_data {
+struct __rte_aligned(2) __rte_packed_begin encap_vxlan_ipv4_data {
 	struct rte_ether_hdr ether;
 	struct rte_ipv4_hdr ipv4;
 	struct rte_udp_hdr udp;
 	struct rte_vxlan_hdr vxlan;
-} __rte_packed;
+} __rte_packed_end;
 
-struct __rte_aligned(2) encap_vxlan_ipv4_vlan_data {
+struct __rte_aligned(2) __rte_packed_begin encap_vxlan_ipv4_vlan_data {
 	struct rte_ether_hdr ether;
 	struct rte_vlan_hdr vlan;
 	struct rte_ipv4_hdr ipv4;
 	struct rte_udp_hdr udp;
 	struct rte_vxlan_hdr vxlan;
-} __rte_packed;
+} __rte_packed_end;
 
-struct __rte_aligned(2) encap_vxlan_ipv6_data {
+struct __rte_aligned(2) __rte_packed_begin encap_vxlan_ipv6_data {
 	struct rte_ether_hdr ether;
 	struct rte_ipv6_hdr ipv6;
 	struct rte_udp_hdr udp;
 	struct rte_vxlan_hdr vxlan;
-} __rte_packed;
+} __rte_packed_end;
 
-struct __rte_aligned(2) encap_vxlan_ipv6_vlan_data {
+struct __rte_aligned(2) __rte_packed_begin encap_vxlan_ipv6_vlan_data {
 	struct rte_ether_hdr ether;
 	struct rte_vlan_hdr vlan;
 	struct rte_ipv6_hdr ipv6;
 	struct rte_udp_hdr udp;
 	struct rte_vxlan_hdr vxlan;
-} __rte_packed;
+} __rte_packed_end;
 
-struct __rte_aligned(2) encap_qinq_pppoe_data {
+struct __rte_aligned(2) __rte_packed_begin encap_qinq_pppoe_data {
 	struct rte_ether_hdr ether;
 	struct rte_vlan_hdr svlan;
 	struct rte_vlan_hdr cvlan;
 	struct pppoe_ppp_hdr pppoe_ppp;
-} __rte_packed;
+} __rte_packed_end;
 
 static size_t
 encap_data_size(struct rte_table_action_encap_config *encap)
@@ -1196,15 +1196,15 @@ nat_cfg_check(struct rte_table_action_nat_config *nat)
 	return 0;
 }
 
-struct nat_ipv4_data {
+struct __rte_packed_begin nat_ipv4_data {
 	uint32_t addr;
 	uint16_t port;
-} __rte_packed;
+} __rte_packed_end;
 
-struct nat_ipv6_data {
+struct __rte_packed_begin nat_ipv6_data {
 	struct rte_ipv6_addr addr;
 	uint16_t port;
-} __rte_packed;
+} __rte_packed_end;
 
 static size_t
 nat_data_size(struct rte_table_action_nat_config *nat __rte_unused,
@@ -1493,9 +1493,9 @@ ttl_cfg_check(struct rte_table_action_ttl_config *ttl)
 	return 0;
 }
 
-struct ttl_data {
+struct __rte_packed_begin ttl_data {
 	uint32_t n_packets;
-} __rte_packed;
+} __rte_packed_end;
 
 #define TTL_INIT(data, decrement)                         \
 	((data)->n_packets = (decrement) ? 1 : 0)
@@ -1576,10 +1576,10 @@ stats_cfg_check(struct rte_table_action_stats_config *stats)
 	return 0;
 }
 
-struct stats_data {
+struct __rte_packed_begin stats_data {
 	uint64_t n_packets;
 	uint64_t n_bytes;
-} __rte_packed;
+} __rte_packed_end;
 
 static int
 stats_apply(struct stats_data *data,
@@ -1602,9 +1602,9 @@ pkt_work_stats(struct stats_data *data,
 /**
  * RTE_TABLE_ACTION_TIME
  */
-struct time_data {
+struct __rte_packed_begin time_data {
 	uint64_t time;
-} __rte_packed;
+} __rte_packed_end;
 
 static int
 time_apply(struct time_data *data,
@@ -1649,7 +1649,7 @@ struct crypto_op_sym_iv_aad {
 	} iv_aad;
 };
 
-struct sym_crypto_data {
+struct __rte_packed_begin sym_crypto_data {
 
 	union {
 		struct {
@@ -1717,7 +1717,7 @@ struct sym_crypto_data {
 	/** Private data size to store cipher iv / aad. */
 	uint8_t iv_aad_data[32];
 
-} __rte_packed;
+} __rte_packed_end;
 
 static int
 sym_crypto_cfg_check(struct rte_table_action_sym_crypto_config *cfg)
@@ -2052,9 +2052,9 @@ pkt_work_sym_crypto(struct rte_mbuf *mbuf, struct sym_crypto_data *data,
 /**
  * RTE_TABLE_ACTION_TAG
  */
-struct tag_data {
+struct __rte_packed_begin tag_data {
 	uint32_t tag;
-} __rte_packed;
+} __rte_packed_end;
 
 static int
 tag_apply(struct tag_data *data,
@@ -2096,9 +2096,9 @@ pkt4_work_tag(struct rte_mbuf *mbuf0,
 /**
  * RTE_TABLE_ACTION_DECAP
  */
-struct decap_data {
+struct __rte_packed_begin decap_data {
 	uint16_t n;
-} __rte_packed;
+} __rte_packed_end;
 
 static int
 decap_apply(struct decap_data *data,
Signature · 1 line
2.47.0.vfs.0.3
#313 lore raw

PATCH v11 29/30 vhost: replace packed attributes

In reply to Andre Muezerie

MSVC struct packing is not compatible with GCC. Replace macro __rte_packed with __rte_packed_begin to push existing pack value and set packing to 1-byte and macro __rte_packed_end to restore the pack value prior to the push.

Macro __rte_packed_end is deliberately utilized to trigger a MSVC compiler warning if no existing packing has been pushed allowing easy identification of locations where the __rte_packed_begin is missing.

---
 lib/vhost/vhost_user.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/vhost/vhost_user.h +4 -4
index edf7adb3c0..9a905ee5f4 100644
@@ -143,7 +143,7 @@ struct vhost_user_config {
 	uint8_t region[VHOST_USER_MAX_CONFIG_SIZE];
 };
 
-typedef struct VhostUserMsg {
+typedef struct __rte_packed_begin VhostUserMsg {
 	union {
 		uint32_t frontend; /* a VhostUserRequest value */
 		uint32_t backend;  /* a VhostUserBackendRequest value*/
@@ -169,16 +169,16 @@ typedef struct VhostUserMsg {
 		struct vhost_user_config cfg;
 	} payload;
 	/* Nothing should be added after the payload */
-} __rte_packed VhostUserMsg;
+} __rte_packed_end VhostUserMsg;
 
 /* Note: this structure and VhostUserMsg can't be changed carelessly as
  * external message handlers rely on them.
  */
-struct __rte_packed vhu_msg_context {
+struct __rte_packed_begin vhu_msg_context {
 	VhostUserMsg msg;
 	int fds[VHOST_MEMORY_MAX_NREGIONS];
 	int fd_num;
-};
+} __rte_packed_end;
 
 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
 
Signature · 1 line
2.47.0.vfs.0.3
#314 lore raw

PATCH v11 30/30 eal/include: deprecate macro __rte_packed

In reply to Andre Muezerie

Macro __rte_packed was marked as deprecated.

---
 lib/eal/include/rte_common.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
lib/eal/include/rte_common.h +13 -0
index d72f8590bd..40592f71b1 100644
@@ -99,6 +99,19 @@ typedef uint32_t unaligned_uint32_t;
 typedef uint16_t unaligned_uint16_t;
 #endif
 
+/**
+ * @deprecated
+ * @see __rte_packed_begin
+ * @see __rte_packed_end
+ *
+ * Force a structure to be packed
+ */
+#ifdef RTE_TOOLCHAIN_MSVC
+#define __rte_packed RTE_DEPRECATED(__rte_packed)
+#else
+#define __rte_packed (RTE_DEPRECATED(__rte_packed) __attribute__((__packed__)))
+#endif
+
 /**
  * Force a structure to be packed
  * Usage:
Signature · 1 line
2.47.0.vfs.0.3
#315 lore raw
In reply to Andre Muezerie
Quoted 53 lines · Andre Muezerie

MSVC struct packing is not compatible with GCC. Different alternatives were considered:

1) Have a macro __RTE_PACKED(decl) to which the struct/union is passed and the macro would define the struct/union with the appropriate packing attribute for the compiler in use.

Advantages:
* Can be placed in front of a struct, or even in the middle. Good for readability.
* Does not require a different macro to be placed at the end of the structure.

However, problems can arise when compiler directives are present in the struct, as they become arguments for __RTE_PACKED macro. This is not portable. Two problematic situations observed in the DPDK code:

a) #defines mentioned in the struct. In this situation we could just move the #define out of the struct.

b) #if/#ifdef/#elif mentioned in the struct.
This is a somewhat common pattern in structs where fields change based on endianness and would require code duplication to be handled, which makes the code hard to read and maintain.

2) Have macros __rte_msvc_pack_begin and __rte_msvc_pack_end which would be placed at the beginning and end of the struct/union respectively. Concerns were raised about having macros for specific compilers, or even having compiler names mentioned in the macros' names.

3) Instead of providing macros exclusively for MSVC and for GCC, have a macro __rte_packed_begin and __rte_packed_end which would be placed at the beginning and end of the struct/union respectively. With MSVC both macros end up having a purpose. With GCC and Clang only __rte_packed_end has a purpose, as can be seen below. This makes the solution generic and is the approach taken in this patchset.

   #ifdef RTE_TOOLCHAIN_MSVC
   #define __rte_packed_begin __pragma(pack(push, 1))
   #define __rte_packed_end __pragma(pack(pop))
   #else
   #define __rte_packed_begin
   #define __rte_packed_end __attribute__((__packed__))
   #endif

Macro __rte_packed_end is deliberately utilized to trigger a MSVC compiler warning if no existing packing has been pushed allowing easy identification of locations where the __rte_packed_begin is missing.

Quoted 8 lines

Macro __rte_packed is marked deprecated and the two new macros represent the new way to enable packing in the DPDK code.

Script checkpatches.sh was enhanced to ensure that:
* __rte_packed_begin and __rte_packed_end show up in pairs.
* __rte_packed_begin is not used with enums.
* __rte_packed_begin is only used after struct, union, __rte_cache_aligned, __rte_cache_min_aligned or __rte_aligned

Recheck-request: ci/iol-marvell-Functional

Signature · 1 line
David Marchand
#316 lore raw
In reply to Andre Muezerie

Hello Andre,

Quoted 53 lines · Andre Muezerie

MSVC struct packing is not compatible with GCC. Different alternatives were considered:

1) Have a macro __RTE_PACKED(decl) to which the struct/union is passed and the macro would define the struct/union with the appropriate packing attribute for the compiler in use.

Advantages:
* Can be placed in front of a struct, or even in the middle. Good for readability.
* Does not require a different macro to be placed at the end of the structure.

However, problems can arise when compiler directives are present in the struct, as they become arguments for __RTE_PACKED macro. This is not portable. Two problematic situations observed in the DPDK code:

a) #defines mentioned in the struct. In this situation we could just move the #define out of the struct.

b) #if/#ifdef/#elif mentioned in the struct.
This is a somewhat common pattern in structs where fields change based on endianness and would require code duplication to be handled, which makes the code hard to read and maintain.

2) Have macros __rte_msvc_pack_begin and __rte_msvc_pack_end which would be placed at the beginning and end of the struct/union respectively. Concerns were raised about having macros for specific compilers, or even having compiler names mentioned in the macros' names.

3) Instead of providing macros exclusively for MSVC and for GCC, have a macro __rte_packed_begin and __rte_packed_end which would be placed at the beginning and end of the struct/union respectively. With MSVC both macros end up having a purpose. With GCC and Clang only __rte_packed_end has a purpose, as can be seen below. This makes the solution generic and is the approach taken in this patchset.

   #ifdef RTE_TOOLCHAIN_MSVC
   #define __rte_packed_begin __pragma(pack(push, 1))
   #define __rte_packed_end __pragma(pack(pop))
   #else
   #define __rte_packed_begin
   #define __rte_packed_end __attribute__((__packed__))
   #endif

Macro __rte_packed_end is deliberately utilized to trigger a MSVC compiler warning if no existing packing has been pushed allowing easy identification of locations where the __rte_packed_begin is missing.

Quoted 9 lines

Macro __rte_packed is marked deprecated and the two new macros represent the new way to enable packing in the DPDK code.

Script checkpatches.sh was enhanced to ensure that:
* __rte_packed_begin and __rte_packed_end show up in pairs.
* __rte_packed_begin is not used with enums.
* __rte_packed_begin is only used after struct, union, __rte_cache_aligned, __rte_cache_min_aligned or __rte_aligned

I did a couple of small changes:
- the __rte_packed_macro is kept in the first patch and removed in the last one, to avoid breaking patch per patch compilation,
- fixed some small coding style issues (like double space before __rte_packed_end),
- moved dropping __rte_packed into separate commits for better visibility of those changes,
- squashed all libraries, drivers and examples into one separate commit each. Splitting into many patches did not help get more reviews, and for such global changes I see no advantage to keep those many commits.
- shortened a bit the checkpatch warnings,
- dropped the change on gve base driver, to be on the safe side, since GVE maintainers did not reply.
Other changes on base drivers have been kept when the code was
already using __rte_* macros/attributes,
- added a RN entry,

I did not touch the check on counting __rte_packed_begin/end. I think we will get various false positives as I described in
https://inbox.dpdk.org/dev/CAJFAV8w=s1L-WYk+Qv-B+Mn6eAwKrB=GTz6hU--ZoLrJsz7=DQ@mail.gmail.com/.
Future will tell us.

Series applied, thanks Andre.

Signature · 1 line
David Marchand
#317 lore raw
Quoted 85 lines · David Marchand

Hello Andre,

Quoted 53 lines · Andre Muezerie

MSVC struct packing is not compatible with GCC. Different alternatives were considered:

1) Have a macro __RTE_PACKED(decl) to which the struct/union is passed and the macro would define the struct/union with the appropriate packing attribute for the compiler in use.

Advantages:
* Can be placed in front of a struct, or even in the middle. Good for readability.
* Does not require a different macro to be placed at the end of the structure.

However, problems can arise when compiler directives are present in the struct, as they become arguments for __RTE_PACKED macro. This is not portable. Two problematic situations observed in the DPDK code:

a) #defines mentioned in the struct. In this situation we could just move the #define out of the struct.

b) #if/#ifdef/#elif mentioned in the struct.
This is a somewhat common pattern in structs where fields change based on endianness and would require code duplication to be handled, which makes the code hard to read and maintain.

2) Have macros __rte_msvc_pack_begin and __rte_msvc_pack_end which would be placed at the beginning and end of the struct/union respectively. Concerns were raised about having macros for specific compilers, or even having compiler names mentioned in the macros' names.

3) Instead of providing macros exclusively for MSVC and for GCC, have a macro __rte_packed_begin and __rte_packed_end which would be placed at the beginning and end of the struct/union respectively. With MSVC both macros end up having a purpose. With GCC and Clang only __rte_packed_end has a purpose, as can be seen below. This makes the solution generic and is the approach taken in this patchset.

   #ifdef RTE_TOOLCHAIN_MSVC
   #define __rte_packed_begin __pragma(pack(push, 1))
   #define __rte_packed_end __pragma(pack(pop))
   #else
   #define __rte_packed_begin
   #define __rte_packed_end __attribute__((__packed__))
   #endif

Macro __rte_packed_end is deliberately utilized to trigger a MSVC compiler warning if no existing packing has been pushed allowing easy identification of locations where the __rte_packed_begin is missing.

Quoted 9 lines

Macro __rte_packed is marked deprecated and the two new macros represent the new way to enable packing in the DPDK code.

Script checkpatches.sh was enhanced to ensure that:
* __rte_packed_begin and __rte_packed_end show up in pairs.
* __rte_packed_begin is not used with enums.
* __rte_packed_begin is only used after struct, union, __rte_cache_aligned, __rte_cache_min_aligned or __rte_aligned

I did a couple of small changes:
- the __rte_packed_macro is kept in the first patch and removed in the last one, to avoid breaking patch per patch compilation,
- fixed some small coding style issues (like double space before __rte_packed_end),
- moved dropping __rte_packed into separate commits for better visibility of those changes,
- squashed all libraries, drivers and examples into one separate commit each. Splitting into many patches did not help get more reviews, and for such global changes I see no advantage to keep those many commits.
- shortened a bit the checkpatch warnings,
- dropped the change on gve base driver, to be on the safe side, since GVE maintainers did not reply.
Other changes on base drivers have been kept when the code was
already using __rte_* macros/attributes,
- added a RN entry,

Quoted 10 lines

I did not touch the check on counting __rte_packed_begin/end. I think we will get various false positives as I described in
https://inbox.dpdk.org/dev/CAJFAV8w=s1L-WYk+Qv-B+Mn6eAwKrB=GTz6hU--ZoLrJsz7=DQ@mail.gmail.com/.
Future will tell us.

Series applied, thanks Andre.

Signature · 1 line
David Marchand

That's great news. Thanks for merging this David!