Skip to contentSkip to conversation

test-pmd: Fix pointer aliasing error

PATCH In review Bruce Richardson 8 participants 32 messages Dec 3, 2014 – Dec 12, 2014 15 messages not in the archive

lore
Share & tools
lore
#3 lore raw
In reply to a message not in the archive
Quoted 10 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

	...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

What compiler version is this with? Is there any other way to fix this other than disabling the compiler warnings. Turning off strict aliasing may affect performance as it reduces the number of optimizations that the compiler can perform.

/Bruce

Quoted 23 lines

Need to add CFLAG '-Wno-strict-aliasing' to avoid this issue.

---
 app/test-pmd/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
app/test-pmd/Makefile +1 -1
index 97dc2e6..995c874 100644
@@ -38,7 +38,7 @@ ifeq ($(CONFIG_RTE_TEST_PMD),y)
 #
 APP = testpmd
 
-CFLAGS += -O3
+CFLAGS += -O3 -Wno-strict-aliasing
 CFLAGS += $(WERROR_FLAGS)
 
 ifeq ($(CONFIG_RTE_LIBRTE_PMD_PCAP),y)
Signature · 1 line
1.9.3
#4 lore raw
Quoted 7 lines · Bruce
Quoted 6 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

	...
Quoted 8 lines
Quoted 4 lines

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

What compiler version is this with? Is there any other way to fix this other than disabling the compiler warnings. Turning off strict aliasing may affect performance as it reduces the number of optimizations that the compiler can perform.

The compile version is:

$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
...
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

The OS is centos6.5 x86_64

Actually, another possible solution is, as gcc manual shows, use union. In function rte_raw_cksum() of lib/librte_net/rte_ip.h:

static inline uint16_t

rte_raw_cksum(const char *buf, size_t len){
    union {
        const char *ubuf;
        const uint16_t *uu16;
    } convert;

    convert.ubuf = buf;
    const uint16_t *u16 = convert.uu16;
    ...
}

This may be work, but not test yet.

Any comments about this solution?

Thanks,
Michael

Quoted 26 lines

/Bruce

Quoted 23 lines

Need to add CFLAG '-Wno-strict-aliasing' to avoid this issue.

---
 app/test-pmd/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
app/test-pmd/Makefile +1 -1
index 97dc2e6..995c874 100644
@@ -38,7 +38,7 @@ ifeq ($(CONFIG_RTE_TEST_PMD),y)
 #
 APP = testpmd
 
-CFLAGS += -O3
+CFLAGS += -O3 -Wno-strict-aliasing
 CFLAGS += $(WERROR_FLAGS)
 
 ifeq ($(CONFIG_RTE_LIBRTE_PMD_PCAP),y)
Signature · 1 line
1.9.3
#6 lore raw
In reply to a message not in the archive
Quoted 44 lines · Michael
Quoted 7 lines · Bruce
Quoted 6 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

	...
Quoted 8 lines
Quoted 4 lines

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

What compiler version is this with? Is there any other way to fix this other than disabling the compiler warnings. Turning off strict aliasing may affect performance as it reduces the number of optimizations that the compiler can perform.

The compile version is:

$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
...
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

The OS is centos6.5 x86_64

Actually, another possible solution is, as gcc manual shows, use union. In function rte_raw_cksum() of lib/librte_net/rte_ip.h:

static inline uint16_t

rte_raw_cksum(const char *buf, size_t len){
    union {
        const char *ubuf;
        const uint16_t *uu16;
    } convert;

    convert.ubuf = buf;
    const uint16_t *u16 = convert.uu16;
    ...
}
Quoted 3 lines

This may be work, but not test yet.

Any comments about this solution?

what happens if you make rte_raw_cksum take a void * (or const void *) parameter instead of "const char *"?

/Bruce

#7 lore raw
Quoted 44 lines · Bruce
Quoted 43 lines · Michael
Quoted 7 lines · Bruce
Quoted 6 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

	...
Quoted 8 lines
Quoted 4 lines

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

What compiler version is this with? Is there any other way to fix this other than disabling the compiler warnings. Turning off strict aliasing may affect performance as it reduces the number of optimizations that the compiler can perform.

The compile version is:

$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
...
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

The OS is centos6.5 x86_64

Actually, another possible solution is, as gcc manual shows, use union. In function rte_raw_cksum() of lib/librte_net/rte_ip.h:

static inline uint16_t

rte_raw_cksum(const char *buf, size_t len){
    union {
        const char *ubuf;
        const uint16_t *uu16;
    } convert;

    convert.ubuf = buf;
    const uint16_t *u16 = convert.uu16;
    ...
}
Quoted 5 lines
Quoted 3 lines

This may be work, but not test yet.

Any comments about this solution?

what happens if you make rte_raw_cksum take a void * (or const void *) parameter instead of "const char *"?

"size_t len" is for type char, it is the the array size(for char array is byte numbers), if we use void *, the meaning maybe confuse I think. But it should work with other code change.

Thanks,
Michael

Quoted 3 lines

/Bruce

#26 lore raw
In reply to Bruce Richardson

Hi Bruce,

Quoted 12 lines · Bruce Richardson
Quoted 10 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

	...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

Quoted 4 lines

What compiler version is this with? Is there any other way to fix this other than disabling the compiler warnings. Turning off strict aliasing may affect performance as it reduces the number of optimizations that the compiler can perform.

I can reproduce the issue with a gcc-4.4.6 toolchain. But I think it's a toolchain bug as the warning does not occur with other versions I've tested.

If it's the case, we could either:

- do nothing: in this case the user need to upgrade its toolchain, or pass the -Wno-strict-aliasing manually in EXTRA_CFLAGS

- add the -Wno-strict-aliasing only for gcc 4.4 in the Makefile

What do you think?

Regards,
Olivier

#9 lore raw
In reply to a message not in the archive
Quoted 45 lines · Michael
Quoted 44 lines · Bruce
Quoted 43 lines · Michael
Quoted 7 lines · Bruce
Quoted 6 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

	...
Quoted 8 lines
Quoted 4 lines

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

What compiler version is this with? Is there any other way to fix this other than disabling the compiler warnings. Turning off strict aliasing may affect performance as it reduces the number of optimizations that the compiler can perform.

The compile version is:

$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
...
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

The OS is centos6.5 x86_64

Actually, another possible solution is, as gcc manual shows, use union. In function rte_raw_cksum() of lib/librte_net/rte_ip.h:

static inline uint16_t

rte_raw_cksum(const char *buf, size_t len){
    union {
        const char *ubuf;
        const uint16_t *uu16;
    } convert;

    convert.ubuf = buf;
    const uint16_t *u16 = convert.uu16;
    ...
}
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

This may be work, but not test yet.

Any comments about this solution?

what happens if you make rte_raw_cksum take a void * (or const void *) parameter instead of "const char *"?

"size_t len" is for type char, it is the the array size(for char array is byte numbers), if we use void *, the meaning maybe confuse I think.

That shouldn't be a big issue. We can rename it to "size" instead of "len" if needed.

Quoted 8 lines

But it should work with other code change.

Thanks,
Michael

Quoted 3 lines

/Bruce

#29 lore raw
In reply to Bruce Richardson

Just re-post this mail as Thomas said it missed in mail list.

On Wed, Dec 3, 2014 at 7:42 PM, Bruce Richardson <[email protected]

Quoted 14 lines

wrote:

Quoted 10 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

      ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

Quoted 7 lines

What compiler version is this with? Is there any other way to fix this other than disabling the compiler warnings. Turning off strict aliasing may affect performance as it reduces the number of optimizations that the compiler
can perform.

The compile version is:

$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
...
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

The OS is centos6.5 x86_64

Actually, another possible solution is, as gcc manual shows, use union. In function rte_raw_cksum() of lib/librte_net/rte_ip.h:

static inline uint16_t

rte_raw_cksum(const char *buf, size_t len){
    union {
        const char *ubuf;
        const uint16_t *uu16;
    } convert;

    convert.ubuf = buf;
    const uint16_t *u16 = convert.uu16;
    ...
}

This may be work, but not test yet.

Thanks,
Michael

Quoted 26 lines

/Bruce

Quoted 23 lines

Need to add CFLAG '-Wno-strict-aliasing' to avoid this issue.

---
 app/test-pmd/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
app/test-pmd/Makefile +1 -2
index 97dc2e6..995c874 100644
@@ -38,7 +38,7 @@ ifeq ($(CONFIG_RTE_TEST_PMD),y)
 #
 APP = testpmd
 
-CFLAGS += -O3
+CFLAGS += -O3 -Wno-strict-aliasing
 CFLAGS += $(WERROR_FLAGS)
 
 ifeq ($(CONFIG_RTE_LIBRTE_PMD_PCAP),y)
--

1.9.3

Signature · 2 lines
Thanks & Best Regards
Mike
#28 lore raw
In reply to a message not in the archive

Hi Olivier,

You can check gcc manual.

-fstrict-aliasing
Allow the compiler to assume the strictest aliasing rules
applicable to the language being
compiled. For C (and C++), this activates optimizations based
on the type of expressions. In
particular, an object of one type is assumed never to reside at
the same address as an object of a
different type, unless the types are almost the same. For
example, an "unsigned int" can alias an
"int", but not a "void*" or a "double". A character type may
alias any other type.

So it should not be a bug, but I have no idea about why other version does not reproduce.

Thanks,
Michael

Quoted 42 lines · Olivier MATZ

Hi Bruce,

Quoted 13 lines · Bruce Richardson
Quoted 11 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

Quoted 7 lines

What compiler version is this with? Is there any other way to fix this other than disabling the compiler warnings. Turning off strict aliasing may
affect performance as it reduces the number of optimizations that the compiler
can perform.

I can reproduce the issue with a gcc-4.4.6 toolchain. But I think it's a toolchain bug as the warning does not occur with other versions I've tested.

If it's the case, we could either:

- do nothing: in this case the user need to upgrade its toolchain, or pass the -Wno-strict-aliasing manually in EXTRA_CFLAGS

- add the -Wno-strict-aliasing only for gcc 4.4 in the Makefile

What do you think?

Regards,
Olivier

Signature · 2 lines
Thanks & Best Regards
Mike
#30 lore raw

Re: [PATCH] test-pmd: Fix "__BYTE_ORDER__" not defined error

In reply to a message not in the archive

Hi all,

What about this patch?

It may be some network or mail client issue of me, so not sure this patch posted to mail list successful.

If failed, I will re-post it later.

Thanks,
Michael

Quoted 41 lines · Michael Qiu

app/test-pmd/csumonly.c:84:5: error: "__BYTE_ORDER__" is not defined app/test-pmd/csumonly.c:84:23: error: "__ORDER_LITTLE_ENDIAN__" is not defined

This because old gcc version, like 4.4.7, does not has these buildin macros.

$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
...
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
$ echo | gcc -dM -E -| grep "LITTLE"
(none)

In this situation, we should back to use the macros defined in <endian.h>

---
 app/test-pmd/csumonly.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
app/test-pmd/csumonly.c +7 -1
index 6f43761..3fa81a2 100644
@@ -81,7 +81,13 @@
 
 /* we cannot use htons() from arpa/inet.h due to name conflicts, and we
  * cannot use rte_cpu_to_be_16() on a constant in a switch/case */
-#if  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#ifdef __BYTE_ORDER__
+#define LITTLE_ENDIAN_CHECK (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
+#else
+#define LITTLE_ENDIAN_CHECK (__BYTE_ORDER == __LITTLE_ENDIAN)
+#endif
+
+#if LITTLE_ENDIAN_CHECK
 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
 #else
 #define _htons(x) (x)
#32 lore raw

Re: [PATCH] test-pmd: Fix "__BYTE_ORDER__" not defined error

In reply to a message not in the archive

2014-12-03 16:26, Qiu, Michael:

Quoted 4 lines
Quoted 4 lines
app/test-pmd/csumonly.c +0 -0
@@ -81,7 +81,13 @@
 
Quoted 13 lines

app/test-pmd/csumonly.c line 93

Quoted 13 lines
app/test-pmd/csumonly.c +7 -1
 /* we cannot use htons() from arpa/inet.h due to name conflicts, and we
  * cannot use rte_cpu_to_be_16() on a constant in a switch/case */
-#if  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#ifdef __BYTE_ORDER__
+#define LITTLE_ENDIAN_CHECK (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
+#else
+#define LITTLE_ENDIAN_CHECK (__BYTE_ORDER == __LITTLE_ENDIAN)
+#endif
+
+#if LITTLE_ENDIAN_CHECK
 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
 #else
 #define _htons(x) (x)

I don't agree we should fix it in testpmd.
It's a more general problem which must be solved in EAL. I'm preparing a patchset to fix it.

Signature · 1 line
Thomas
#33 lore raw

cover fix endianness in EAL

It can be hard to implement a reliable detection of endianness. Previous trials in testpmd failed.
The IBM Power patchset introduced a config option.

This patchset try to improve the situation by having a detection in EAL headers.

Please test it (especially on IBM Power) with different toolchains or distributions.

Thomas Monjalon (2):
eal: detect endianness
app/testpmd: fix endianness detection

Signature · 1 line
2.1.3
#35 lore raw

PATCH 1/2 eal: detect endianness

In reply to a message not in the archive

There is no standard to check endianness.
So we need to try different checks.
Previous trials were done in testpmd (see commits 51f694dd40f56 and 64741f237cf29) without full success. This one is not guaranteed to work everywhere so it could evolve when exceptions are found.

If endianness is not detected, there is a fallback on x86 to little endian. It could be forced before doing detection but it would add some arch-dependent code in the generic header.

The option CONFIG_RTE_ARCH_BIG_ENDIAN introduced for IBM Power only (commit a982ec81d84d53) can be removed. A compile-time check is better.

---
 config/defconfig_ppc_64-power8-linuxapp-gcc        |  1 -
 .../common/include/arch/ppc_64/rte_byteorder.h     |  4 ++--
 .../common/include/arch/x86/rte_byteorder.h        |  4 ++++
 .../common/include/generic/rte_byteorder.h         | 28 ++++++++++++++++++++++
 4 files changed, 34 insertions(+), 3 deletions(-)
config/defconfig_ppc_64-power8-linuxapp-gcc +0 -1
index 48018c3..d97a885 100644
@@ -34,7 +34,6 @@ CONFIG_RTE_MACHINE="power8"
 
 CONFIG_RTE_ARCH="ppc_64"
 CONFIG_RTE_ARCH_PPC_64=y
-CONFIG_RTE_ARCH_BIG_ENDIAN=y
 CONFIG_RTE_ARCH_64=y
 
 CONFIG_RTE_TOOLCHAIN="gcc"
lib/librte_eal/common/include/arch/ppc_64/rte_byteorder.h +2 -2
index 1a89051..80436f2 100644
@@ -105,7 +105,7 @@ static inline uint64_t rte_arch_bswap64(uint64_t _x)
 /* Power 8 have both little endian and big endian mode
  * Power 7 only support big endian
  */
-#ifndef RTE_ARCH_BIG_ENDIAN
+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 
 #define rte_cpu_to_le_16(x) (x)
 #define rte_cpu_to_le_32(x) (x)
@@ -123,7 +123,7 @@ static inline uint64_t rte_arch_bswap64(uint64_t _x)
 #define rte_be_to_cpu_32(x) rte_bswap32(x)
 #define rte_be_to_cpu_64(x) rte_bswap64(x)
 
-#else
+#else /* RTE_BIG_ENDIAN */
 
 #define rte_cpu_to_le_16(x) rte_bswap16(x)
 #define rte_cpu_to_le_32(x) rte_bswap32(x)
lib/librte_eal/common/include/arch/x86/rte_byteorder.h +4 -0
index 1aa6985..ffdb6ef 100644
@@ -40,6 +40,10 @@ extern "C" {
 
 #include "generic/rte_byteorder.h"
 
+#ifndef RTE_BYTE_ORDER
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif
+
 /*
  * An architecture-optimized byte swap for a 16-bit value.
  *
lib/librte_eal/common/include/generic/rte_byteorder.h +28 -0
index 9358136..ea23fdf 100644
@@ -44,6 +44,34 @@
  */
 
 #include <stdint.h>
+#ifdef RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+
+/*
+ * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN    1
+#define RTE_LITTLE_ENDIAN 2
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
+#elif defined __BYTE_ORDER__
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif
 
 /*
  * An internal function to swap bytes in a 16-bit value.
Signature · 1 line
2.1.3
#43 lore raw

PATCH 2/2 app/testpmd: fix endianness detection

In reply to a message not in the archive

Use endianness detection factorized in EAL.

The comment about arpa/inet.h is not valid anymore since commit d07180f211c08 ("net: fix conflict with libc").

The macro _htons could also be moved in rte_byteorder.h by providing some constant byte swapping.

---
 app/test-pmd/config.c   | 2 +-
 app/test-pmd/csumonly.c | 5 ++---
 app/test-pmd/testpmd.h  | 2 +-
 3 files changed, 4 insertions(+), 5 deletions(-)
app/test-pmd/config.c +1 -1
index f0b770e..69a83c2 100644
@@ -628,7 +628,7 @@ ring_dma_zone_lookup(const char *ring_name, uint8_t port_id, uint16_t q_id)
 union igb_ring_dword {
 	uint64_t dword;
 	struct {
-#ifdef RTE_ARCH_BIG_ENDIAN
+#if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
 		uint32_t lo;
 		uint32_t hi;
 #else
app/test-pmd/csumonly.c +2 -3
index 6f43761..da4bca4 100644
@@ -79,9 +79,8 @@
 #define IP_HDRLEN  0x05 /* default IP header length == five 32-bits words. */
 #define IP_VHL_DEF (IP_VERSION | IP_HDRLEN)
 
-/* we cannot use htons() from arpa/inet.h due to name conflicts, and we
- * cannot use rte_cpu_to_be_16() on a constant in a switch/case */
-#if  __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+/* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
 #else
 #define _htons(x) (x)
app/test-pmd/testpmd.h +1 -1
index c925910..f8b0740 100644
@@ -567,7 +567,7 @@ int tx_queue_id_is_invalid(queueid_t txq_id);
 #define RTE_BE_TO_CPU_16(be_16_v)  rte_be_to_cpu_16((be_16_v))
 #define RTE_CPU_TO_BE_16(cpu_16_v) rte_cpu_to_be_16((cpu_16_v))
 #else
-#ifdef __big_endian__
+#if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
 #define RTE_BE_TO_CPU_16(be_16_v)  (be_16_v)
 #define RTE_CPU_TO_BE_16(cpu_16_v) (cpu_16_v)
 #else
Signature · 1 line
2.1.3
#36 lore raw

Re: [PATCH 1/2] eal: detect endianness

In reply to Thomas Monjalon
Quoted 97 lines · Thomas Monjalon

There is no standard to check endianness.
So we need to try different checks.
Previous trials were done in testpmd (see commits 51f694dd40f56 and 64741f237cf29) without full success. This one is not guaranteed to work everywhere so it could evolve when exceptions are found.

If endianness is not detected, there is a fallback on x86 to little endian. It could be forced before doing detection but it would add some arch-dependent code in the generic header.

The option CONFIG_RTE_ARCH_BIG_ENDIAN introduced for IBM Power only (commit a982ec81d84d53) can be removed. A compile-time check is better.

---
 config/defconfig_ppc_64-power8-linuxapp-gcc        |  1 -
 .../common/include/arch/ppc_64/rte_byteorder.h     |  4 ++--
 .../common/include/arch/x86/rte_byteorder.h        |  4 ++++
 .../common/include/generic/rte_byteorder.h         | 28 ++++++++++++++++++++++
 4 files changed, 34 insertions(+), 3 deletions(-)
config/defconfig_ppc_64-power8-linuxapp-gcc +0 -1
index 48018c3..d97a885 100644
@@ -34,7 +34,6 @@ CONFIG_RTE_MACHINE="power8"
 
 CONFIG_RTE_ARCH="ppc_64"
 CONFIG_RTE_ARCH_PPC_64=y
-CONFIG_RTE_ARCH_BIG_ENDIAN=y
 CONFIG_RTE_ARCH_64=y
 
 CONFIG_RTE_TOOLCHAIN="gcc"
lib/librte_eal/common/include/arch/ppc_64/rte_byteorder.h +2 -2
index 1a89051..80436f2 100644
@@ -105,7 +105,7 @@ static inline uint64_t rte_arch_bswap64(uint64_t _x)
 /* Power 8 have both little endian and big endian mode
  * Power 7 only support big endian
  */
-#ifndef RTE_ARCH_BIG_ENDIAN
+#if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 
 #define rte_cpu_to_le_16(x) (x)
 #define rte_cpu_to_le_32(x) (x)
@@ -123,7 +123,7 @@ static inline uint64_t rte_arch_bswap64(uint64_t _x)
 #define rte_be_to_cpu_32(x) rte_bswap32(x)
 #define rte_be_to_cpu_64(x) rte_bswap64(x)
 
-#else
+#else /* RTE_BIG_ENDIAN */
 
 #define rte_cpu_to_le_16(x) rte_bswap16(x)
 #define rte_cpu_to_le_32(x) rte_bswap32(x)
lib/librte_eal/common/include/arch/x86/rte_byteorder.h +4 -0
index 1aa6985..ffdb6ef 100644
@@ -40,6 +40,10 @@ extern "C" {
 
 #include "generic/rte_byteorder.h"
 
+#ifndef RTE_BYTE_ORDER
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif
+
 /*
  * An architecture-optimized byte swap for a 16-bit value.
  *
lib/librte_eal/common/include/generic/rte_byteorder.h +18 -0
index 9358136..ea23fdf 100644
@@ -44,6 +44,34 @@
  */
 
 #include <stdint.h>
+#ifdef RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+
+/*
+ * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN    1
+#define RTE_LITTLE_ENDIAN 2
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
+#elif defined __BYTE_ORDER__

lib/librte_eal/common/include/generic/rte_byteorder.h line 74

Quoted 10 lines
lib/librte_eal/common/include/generic/rte_byteorder.h +10 -0
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif

What do you think about :

+/*
+ * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN 1
+#define RTE_LITTLE_ENDIAN 2
+if defined __BYTE_ORDER__ /* Prefer gcc build-in macros */
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#else
+#if defined RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif
+#endif

Quoted 3 lines
lib/librte_eal/common/include/generic/rte_byteorder.h +0 -0
 /*
  * An internal function to swap bytes in a 16-bit value.
#10 lore raw
In reply to Bruce Richardson
Quoted 48 lines · Bruce
Quoted 47 lines · Michael
Quoted 46 lines · Bruce
Quoted 45 lines · Michael
Quoted 7 lines · Bruce
Quoted 6 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

	...
Quoted 8 lines
Quoted 4 lines

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

What compiler version is this with? Is there any other way to fix this other than disabling the compiler warnings. Turning off strict aliasing may affect performance as it reduces the number of optimizations that the compiler can perform.

The compile version is:

$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
...
gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

The OS is centos6.5 x86_64

Actually, another possible solution is, as gcc manual shows, use union. In function rte_raw_cksum() of lib/librte_net/rte_ip.h:

static inline uint16_t

rte_raw_cksum(const char *buf, size_t len){
    union {
        const char *ubuf;
        const uint16_t *uu16;
    } convert;

    convert.ubuf = buf;
    const uint16_t *u16 = convert.uu16;
    ...
}

This may be work, but not test yet.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Any comments about this solution?

what happens if you make rte_raw_cksum take a void * (or const void *) parameter instead of "const char *"?

"size_t len" is for type char, it is the the array size(for char array is byte numbers), if we use void *, the meaning maybe confuse I think.

That shouldn't be a big issue. We can rename it to "size" instead of "len" if needed.

As I tested in my env just, void * is still not work, the same errors post when compile.

Then the solution for DPDK should be:
- As Olivier suggest, add the -Wno-strict-aliasing only for gcc 4.4 in the Makefile
- As gcc manual suggest, use "union" to work around this issue.

Thanks,
Michael

Quoted 6 lines
Quoted 6 lines

But it should work with other code change.

Thanks,
Michael

Quoted 2 lines

/Bruce

#12 lore raw

PATCH v2 1/1 test-pmd: Fix pointer aliasing error

In reply to a message not in the archive

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

This patch is one workaround fix.

---
 lib/librte_net/rte_ip.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_net/rte_ip.h +2 -1
index 61e4457..f1c7087 100644
@@ -154,7 +154,8 @@ struct ipv4_hdr {
 static inline uint16_t
 rte_raw_cksum(const char *buf, size_t len)
 {
-	const uint16_t *u16 = (const uint16_t *)buf;
+	unsigned long ptr = (unsigned long)buf;
+	const uint16_t *u16 = (const uint16_t *)ptr;
 	uint32_t sum = 0;
 
 	while (len >= (sizeof(*u16) * 4)) {
Signature · 1 line
1.9.3
#14 lore raw

PATCH v3 1/1 test-pmd: Fix pointer aliasing error

In reply to a message not in the archive

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

This patch is one workaround fix.

---
v3 --> v2:
use uintptr_t instead of unsigned long to
save pointer.

v2 --> v1:
Workaround solution instead of shut off the gcc params.

 lib/librte_net/rte_ip.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_net/rte_ip.h +2 -1
index 61e4457..cda3436 100644
@@ -154,7 +154,8 @@ struct ipv4_hdr {
 static inline uint16_t
 rte_raw_cksum(const char *buf, size_t len)
 {
-	const uint16_t *u16 = (const uint16_t *)buf;
+	uintptr_t ptr = (uintptr_t)buf;
+	const uint16_t *u16 = (const uint16_t *)ptr;
 	uint32_t sum = 0;
 
 	while (len >= (sizeof(*u16) * 4)) {
Signature · 1 line
1.9.3
#38 lore raw

Re: [PATCH 1/2] eal: detect endianness

In reply to a message not in the archive

2014-12-04 02:28, Qiu, Michael:

Quoted 7 lines · Thomas Monjalon
Quoted 6 lines

There is no standard to check endianness.
So we need to try different checks.
Previous trials were done in testpmd (see commits 51f694dd40f56 and 64741f237cf29) without full success. This one is not guaranteed to work everywhere so it could evolve when exceptions are found.

[...]

Quoted 51 lines
Quoted 19 lines

#include <stdint.h>

lib/librte_eal/common/include/generic/rte_byteorder.h +18 -0
+#ifdef RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+
+/*
+ * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN    1
+#define RTE_LITTLE_ENDIAN 2
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
+#elif defined __BYTE_ORDER__

lib/librte_eal/common/include/generic/rte_byteorder.h line 74

Quoted 10 lines
lib/librte_eal/common/include/generic/rte_byteorder.h +10 -0
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif

What do you think about :

(quoted diff) +19 -0
+/*
+  * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN 1
+#define RTE_LITTLE_ENDIAN 2
+if defined __BYTE_ORDER__    /* Prefer gcc build-in macros */
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#else
+#if defined RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN

Review

Quoted 10 lines
(quoted diff) +10 -0
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif
+#endif

Please, could you give more explanations about your proposal? Why not always try to include endian.h?
Why giving high priority to __BYTE_ORDER__?

Signature · 1 line
Thomas
Acked-by
#44 lore raw

Re: [PATCH 0/2] fix endianness in EAL

In reply to a message not in the archive
Quoted 7 lines · Thomas Monjalon

It can be hard to implement a reliable detection of endianness. Previous trials in testpmd failed.
The IBM Power patchset introduced a config option.

This patchset try to improve the situation by having a detection in EAL headers.

Quoted 7 lines

Please test it (especially on IBM Power) with different toolchains or distributions.

Thomas Monjalon (2):
eal: detect endianness
app/testpmd: fix endianness detection

I tested it on IBM Power with GCC 4.8. It works fine.
If there is no other better way to do the detection, I think this patch is good enough.

#39 lore raw

Re: [PATCH 1/2] eal: detect endianness

In reply to Thomas Monjalon
Quoted 63 lines · Thomas Monjalon

2014-12-04 02:28, Qiu, Michael:

Quoted 7 lines · Thomas Monjalon
Quoted 6 lines

There is no standard to check endianness.
So we need to try different checks.
Previous trials were done in testpmd (see commits 51f694dd40f56 and 64741f237cf29) without full success. This one is not guaranteed to work everywhere so it could evolve when exceptions are found.

[...]

Quoted 54 lines
Quoted 19 lines

#include <stdint.h>

lib/librte_eal/common/include/generic/rte_byteorder.h +18 -0
+#ifdef RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+
+/*
+ * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN    1
+#define RTE_LITTLE_ENDIAN 2
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
+#elif defined __BYTE_ORDER__

lib/librte_eal/common/include/generic/rte_byteorder.h line 74

Quoted 10 lines
lib/librte_eal/common/include/generic/rte_byteorder.h +10 -0
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif

What do you think about :

(quoted diff) +23 -0
+/*
+  * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN 1
+#define RTE_LITTLE_ENDIAN 2
+if defined __BYTE_ORDER__    /* Prefer gcc build-in macros */
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#else
+#if defined RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
Quoted 8 lines

lib/librte_eal/common/include/generic/rte_byteorder.h

Quoted 6 lines
lib/librte_eal/common/include/generic/rte_byteorder.h +6 -0
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif
+#endif

Please, could you give more explanations about your proposal? Why not always try to include endian.h?

I assume that if gcc can handler why we need include that file?

Also it seems that only old version could have this issue, newer versions has build in this marcos.

So that's why I prefer "__BYTE_ORDER__" for high priority.

Thanks,
Michael

Quoted 2 lines

Why giving high priority to __BYTE_ORDER__?

#41 lore raw

Re: [PATCH 1/2] eal: detect endianness

In reply to a message not in the archive

2014-12-04 10:28, Qiu, Michael:

Quoted 74 lines · Thomas Monjalon
Quoted 64 lines

2014-12-04 02:28, Qiu, Michael:

Quoted 7 lines · Thomas Monjalon
Quoted 6 lines

There is no standard to check endianness.
So we need to try different checks.
Previous trials were done in testpmd (see commits 51f694dd40f56 and 64741f237cf29) without full success. This one is not guaranteed to work everywhere so it could evolve when exceptions are found.

[...]

Quoted 55 lines
Quoted 19 lines

#include <stdint.h>

lib/librte_eal/common/include/generic/rte_byteorder.h +18 -0
+#ifdef RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+
+/*
+ * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN    1
+#define RTE_LITTLE_ENDIAN 2
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
+#elif defined __BYTE_ORDER__

lib/librte_eal/common/include/generic/rte_byteorder.h line 74

Quoted 10 lines
lib/librte_eal/common/include/generic/rte_byteorder.h +10 -0
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif

What do you think about :

(quoted diff) +24 -0
+/*
+  * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN 1
+#define RTE_LITTLE_ENDIAN 2
+if defined __BYTE_ORDER__    /* Prefer gcc build-in macros */
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#else
+#if defined RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
+#elif defined __BIG_ENDIAN__
Quoted 8 lines

lib/librte_eal/common/include/generic/rte_byteorder.h

Quoted 5 lines
lib/librte_eal/common/include/generic/rte_byteorder.h +5 -0
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif
+#endif

Please, could you give more explanations about your proposal? Why not always try to include endian.h?

Quoted 1 line

I assume that if gcc can handler why we need include that file?

Separating include on top is easier to read, and I'm not sure it won't be needed for __BYTE_ORDER__ with some toolchains.

Quoted 4 lines

Also it seems that only old version could have this issue, newer versions has build in this marcos.

So that's why I prefer "__BYTE_ORDER__" for high priority.

I have no problem with reversing this priority.

Quoted 1 line
Quoted 1 line

Why giving high priority to __BYTE_ORDER__?

Any other comment? May I apply with above change?

Signature · 1 line
Thomas
Acked-by
#42 lore raw

Re: [PATCH 1/2] eal: detect endianness

Quoted 77 lines · Thomas Monjalon

2014-12-04 10:28, Qiu, Michael:

Quoted 72 lines · Thomas Monjalon
Quoted 63 lines

2014-12-04 02:28, Qiu, Michael:

Quoted 7 lines · Thomas Monjalon
Quoted 6 lines

There is no standard to check endianness.
So we need to try different checks.
Previous trials were done in testpmd (see commits 51f694dd40f56 and 64741f237cf29) without full success. This one is not guaranteed to work everywhere so it could evolve when exceptions are found.

[...]

Quoted 54 lines
Quoted 19 lines

#include <stdint.h>

(quoted diff) +18 -0
+#ifdef RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+
+/*
+ * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN    1
+#define RTE_LITTLE_ENDIAN 2
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
+#elif defined __BYTE_ORDER__

Review

Quoted 10 lines
(quoted diff) +10 -0
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif

What do you think about :

(quoted diff) +23 -0
+/*
+  * Compile-time endianness detection
+ */
+#define RTE_BIG_ENDIAN 1
+#define RTE_LITTLE_ENDIAN 2
+if defined __BYTE_ORDER__    /* Prefer gcc build-in macros */
+#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER__ */
+#else
+#if defined RTE_EXEC_ENV_BSDAPP
+#include <sys/endian.h>
+#else
+#include <endian.h>
+#endif
+#if defined __BYTE_ORDER
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif /* __BYTE_ORDER */
Quoted 8 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+#elif defined __BIG_ENDIAN__
+#define RTE_BYTE_ORDER RTE_BIG_ENDIAN
+#elif defined __LITTLE_ENDIAN__
+#define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
+#endif
+#endif

Please, could you give more explanations about your proposal? Why not always try to include endian.h?

Quoted 1 line

I assume that if gcc can handler why we need include that file?

Separating include on top is easier to read, and I'm not sure it won't be needed for __BYTE_ORDER__ with some toolchains.

Quoted 8 lines
Quoted 4 lines

Also it seems that only old version could have this issue, newer versions has build in this marcos.

So that's why I prefer "__BYTE_ORDER__" for high priority.

I have no problem with reversing this priority.

Quoted 1 line
Quoted 1 line

Why giving high priority to __BYTE_ORDER__?

Any other comment? May I apply with above change?

#25 lore raw
In reply to a message not in the archive
Quoted 31 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Michael Qiu
Sent: Thursday, December 04, 2014 3:29 AM
To: [email protected]
Subject: [dpdk-dev] [PATCH v2] test-pmd: Fix pointer aliasing error

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

This patch is one workaround fix.

---
 lib/librte_net/rte_ip.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_net/rte_ip.h +0 -0
index 61e4457..f1c7087 100644

lib/librte_net/rte_ip.h struct ipv4_hdr { · line 157

Quoted 5 lines
lib/librte_net/rte_ip.h +1 -1
@@ -154,7 +154,8 @@ struct ipv4_hdr {
 static inline uint16_t
 rte_raw_cksum(const char *buf, size_t len)
 {
-	const uint16_t *u16 = (const uint16_t *)buf;
+	unsigned long ptr = (unsigned long)buf;

I think better use uintptr_t here.
It is specially designed to be long enough to hold pointer value.

Quoted 6 lines
lib/librte_net/rte_ip.h +1 -1
+	const uint16_t *u16 = (const uint16_t *)ptr;
 	uint32_t sum = 0;
 
 	while (len >= (sizeof(*u16) * 4)) {
--

1.9.3

#15 lore raw
In reply to Michael Qiu

Any comments about this version? a new workaround solution :)

Thanks,
Michael

Quoted 39 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

This patch is one workaround fix.

---
v3 --> v2:
use uintptr_t instead of unsigned long to
save pointer.

v2 --> v1:
Workaround solution instead of shut off the gcc params.

 lib/librte_net/rte_ip.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_net/rte_ip.h +2 -1
index 61e4457..cda3436 100644
@@ -154,7 +154,8 @@ struct ipv4_hdr {
 static inline uint16_t
 rte_raw_cksum(const char *buf, size_t len)
 {
-	const uint16_t *u16 = (const uint16_t *)buf;
+	uintptr_t ptr = (uintptr_t)buf;
+	const uint16_t *u16 = (const uint16_t *)ptr;
 	uint32_t sum = 0;
 
 	while (len >= (sizeof(*u16) * 4)) {
#17 lore raw
In reply to a message not in the archive

2014-12-05 05:34, Qiu, Michael:

Quoted 1 line

Any comments about this version? a new workaround solution :)

Yes, one comment: I think it's ugly :)
These aliasing errors are not reliable so I think we can disable it (like Linux does).
But in case you don't want to disable the warning, please add a comment to your workaround to explain it is caused by GCC strict-aliasing check.

Quoted 3 lines

lib/librte_net/rte_ip.h struct ipv4_hdr { · line 158

Quoted 3 lines
lib/librte_net/rte_ip.h +2 -1
-	const uint16_t *u16 = (const uint16_t *)buf;
+	uintptr_t ptr = (uintptr_t)buf;
+	const uint16_t *u16 = (const uint16_t *)ptr;

Thanks

Signature · 1 line
Thomas
#46 lore raw

Re: [PATCH 0/2] fix endianness in EAL

In reply to a message not in the archive
Quoted 14 lines
Quoted 7 lines

It can be hard to implement a reliable detection of endianness. Previous trials in testpmd failed.
The IBM Power patchset introduced a config option.

This patchset try to improve the situation by having a detection in EAL headers.

Quoted 7 lines

Please test it (especially on IBM Power) with different toolchains or distributions.

Thomas Monjalon (2):
eal: detect endianness
app/testpmd: fix endianness detection

Quoted 5 lines

I tested it on IBM Power with GCC 4.8. It works fine.
If there is no other better way to do the detection, I think this patch is good enough.

Applied

Signature · 1 line
Thomas
#18 lore raw
In reply to Thomas Monjalon
Quoted 7 lines · Thomas Monjalon

2014-12-05 05:34, Qiu, Michael:

Quoted 1 line

Any comments about this version? a new workaround solution :)

Yes, one comment: I think it's ugly :)
These aliasing errors are not reliable so I think we can disable it (like Linux does).
But in case you don't want to disable the warning, please add a comment to your workaround to explain it is caused by GCC strict-aliasing check.

Yes, really ugly ....

But lots of reviewer voted against with disable it as my first version is disable.

I could not think out a better solution for all reviewers can accept.

I will add the comment with the thread of v3 patch.

Thanks,
Michael

Quoted 5 lines
Quoted 3 lines

lib/librte_net/rte_ip.h struct ipv4_hdr { · line 158

Quoted 3 lines
lib/librte_net/rte_ip.h +2 -1
-	const uint16_t *u16 = (const uint16_t *)buf;
+	uintptr_t ptr = (uintptr_t)buf;
+	const uint16_t *u16 = (const uint16_t *)ptr;

Thanks

#19 lore raw
In reply to Michael Qiu
Quoted 30 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

This patch is one workaround fix.

---
v3 --> v2:
use uintptr_t instead of unsigned long to
save pointer.

v2 --> v1:
Workaround solution instead of shut off the gcc params.

 lib/librte_net/rte_ip.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_net/rte_ip.h +0 -0
index 61e4457..cda3436 100644

lib/librte_net/rte_ip.h struct ipv4_hdr { · line 161

Quoted 9 lines
lib/librte_net/rte_ip.h +2 -1
@@ -154,7 +154,8 @@ struct ipv4_hdr {
 static inline uint16_t
 rte_raw_cksum(const char *buf, size_t len)
 {
-	const uint16_t *u16 = (const uint16_t *)buf;
+	uintptr_t ptr = (uintptr_t)buf;
+	const uint16_t *u16 = (const uint16_t *)ptr;
 	uint32_t sum = 0;
 
 	while (len >= (sizeof(*u16) * 4)) {

This workaround is to solve the compile issue of GCC strict-aliasing(Two different type pointers should not be point to the same memory address).

For GCC 4.4.7 it will definitely occurs if flags "-fstrict-aliasing" and "-Wall" used.

Thanks,
Michael

#20 lore raw

Hi Thomas,

What's going on with this patch?

I really do not have other better solution.

Thanks,
Michael

Quoted 49 lines · Michael
Quoted 30 lines · Michael Qiu

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

This patch is one workaround fix.

---
v3 --> v2:
use uintptr_t instead of unsigned long to
save pointer.

v2 --> v1:
Workaround solution instead of shut off the gcc params.

 lib/librte_net/rte_ip.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/librte_net/rte_ip.h +0 -0
index 61e4457..cda3436 100644

lib/librte_net/rte_ip.h struct ipv4_hdr { · line 161

Quoted 9 lines
lib/librte_net/rte_ip.h +2 -1
@@ -154,7 +154,8 @@ struct ipv4_hdr {
 static inline uint16_t
 rte_raw_cksum(const char *buf, size_t len)
 {
-	const uint16_t *u16 = (const uint16_t *)buf;
+	uintptr_t ptr = (uintptr_t)buf;
+	const uint16_t *u16 = (const uint16_t *)ptr;
 	uint32_t sum = 0;
 
 	while (len >= (sizeof(*u16) * 4)) {

This workaround is to solve the compile issue of GCC strict-aliasing(Two different type pointers should not be point to the same memory address).

For GCC 4.4.7 it will definitely occurs if flags "-fstrict-aliasing" and "-Wall" used.

Thanks,
Michael

#22 lore raw
In reply to a message not in the archive
Quoted 22 lines
Quoted 13 lines

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

This patch is one workaround fix.

Quoted 8 lines

---
v3 --> v2:
use uintptr_t instead of unsigned long to
save pointer.

v2 --> v1:
Workaround solution instead of shut off the gcc params.

Quoted 5 lines

This workaround is to solve the compile issue of GCC strict-aliasing(Two different type pointers should not be point to the same memory address).

For GCC 4.4.7 it will definitely occurs if flags "-fstrict-aliasing" and "-Wall" used.

Applied with a comment in the code.

Thanks

Signature · 1 line
Thomas
#23 lore raw

Thomas, Michael,

Wouldn't it cause unaligned memory access (new changes as well as the previous code)? Wondering if get_unaligned/put_unaligned macros similar to the ones used in kernel be ported to user-space?

Thanks,
Ravi

Quoted 37 lines · Thomas Monjalon
Quoted 23 lines
Quoted 14 lines

app/test-pmd/csumonly.c: In function 'get_psd_sum':
build/include/rte_ip.h:161: error: dereferencing pointer 'u16'
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

This patch is one workaround fix.

Quoted 8 lines

---
v3 --> v2:
use uintptr_t instead of unsigned long to
save pointer.

v2 --> v1:
Workaround solution instead of shut off the
gcc params.

Quoted 5 lines

This workaround is to solve the compile issue of GCC strict-aliasing(Two different type pointers should not be point to the same memory address).

For GCC 4.4.7 it will definitely occurs if flags "-fstrict-aliasing" and "-Wall" used.

Applied with a comment in the code.

Thanks
--
Thomas

Acked-by Thomas Monjalon
#24 lore raw

On 2014/12/12 1:51, r k wrote:
Thomas, Michael,

Wouldn't it cause unaligned memory access (new changes as well as the previous code)? Wondering if get_unaligned/put_unaligned macros similar to the ones used in kernel be ported to user-space?

I think it will not, as all buf point to are struct udp_hdr/struct tcp_hdr/struct ipv6_psd_header/struct ipv4_psd_header, they are all aligned with uint16_t.

Thanks
Michael
Thanks,
Ravi

Quoted 22 lines · On Wed, Dec 10, 2014 at 4:54 PM, Thomas Monjalon…
Quoted 13 lines

app/test-pmd/csumonly.c: In function ‘get_psd_sum’:
build/include/rte_ip.h:161: error: dereferencing pointer ‘u16’
does break strict-aliasing rules
build/include/rte_ip.h:157: note: initialized from here

        ...

The root cause is that, compile enable strict aliasing by default, while in function rte_raw_cksum() try to convert 'const char *' to 'const uint16_t *'.

This patch is one workaround fix.

Quoted 8 lines

---
v3 --> v2:
use uintptr_t instead of unsigned long to
save pointer.

v2 --> v1:
Workaround solution instead of shut off the
gcc params.

Quoted 5 lines

This workaround is to solve the compile issue of GCC strict-aliasing(Two different type pointers should not be point to the same memory address).

For GCC 4.4.7 it will definitely occurs if flags "-fstrict-aliasing" and "-Wall" used.

Applied with a comment in the code.

Thanks
--
Thomas