[PATCH bpf-next] selftests/bpf: don't use bpf helpers in non-bpf environment

Subsystems: bpf [general] (safe dynamic programs and tools), bpf [selftests] (test runners & infrastructure), kernel selftest framework, the rest

STALE2828d

7 messages, 3 authors, 2018-12-12 · open the first message on its own page

[PATCH bpf-next] selftests/bpf: don't use bpf helpers in non-bpf environment

From: Stanislav Fomichev <hidden>
Date: 2018-12-11 21:28:16

We're using bpf_htons in test_progs.c to initialize some static
global data and I think I hit some weird case on an older compiler
which doesn't have __builtin_bswap16 (and __builtin_constant_p
expands to false).

In this case I see:
error: implicit declaration of function '__builtin_bswap16'

Let's explicitly use __constant_htons which should be exposed by the
linux/byteorder.h uapi header.

Signed-off-by: Stanislav Fomichev <redacted>
---
 tools/testing/selftests/bpf/test_progs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 26f1fdf3e2bf..61593d319c0e 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -51,10 +51,10 @@ static struct {
 	struct iphdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v4 = {
-	.eth.h_proto = bpf_htons(ETH_P_IP),
+	.eth.h_proto = __constant_htons(ETH_P_IP),
 	.iph.ihl = 5,
 	.iph.protocol = 6,
-	.iph.tot_len = bpf_htons(MAGIC_BYTES),
+	.iph.tot_len = __constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
@@ -64,9 +64,9 @@ static struct {
 	struct ipv6hdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v6 = {
-	.eth.h_proto = bpf_htons(ETH_P_IPV6),
+	.eth.h_proto = __constant_htons(ETH_P_IPV6),
 	.iph.nexthdr = 6,
-	.iph.payload_len = bpf_htons(MAGIC_BYTES),
+	.iph.payload_len = __constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
-- 
2.20.0.rc2.403.gdbc3b29805-goog

Re: [PATCH bpf-next] selftests/bpf: don't use bpf helpers in non-bpf environment

From: Stanislav Fomichev <sdf@fomichev.me>
Date: 2018-12-11 21:49:40

On 12/11, Stanislav Fomichev wrote:
We're using bpf_htons in test_progs.c to initialize some static
global data and I think I hit some weird case on an older compiler
which doesn't have __builtin_bswap16 (and __builtin_constant_p
expands to false).

In this case I see:
error: implicit declaration of function '__builtin_bswap16'

Let's explicitly use __constant_htons which should be exposed by the
linux/byteorder.h uapi header.
Forgot to mention, that using simple htons produces the following:
    test_progs.c:54:17: error: braced-group within expression allowed only
    inside a function
      .eth.h_proto = htons(ETH_P_IP),
quoted hunk
Signed-off-by: Stanislav Fomichev <redacted>
---
 tools/testing/selftests/bpf/test_progs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 26f1fdf3e2bf..61593d319c0e 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -51,10 +51,10 @@ static struct {
 	struct iphdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v4 = {
-	.eth.h_proto = bpf_htons(ETH_P_IP),
+	.eth.h_proto = __constant_htons(ETH_P_IP),
 	.iph.ihl = 5,
 	.iph.protocol = 6,
-	.iph.tot_len = bpf_htons(MAGIC_BYTES),
+	.iph.tot_len = __constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
@@ -64,9 +64,9 @@ static struct {
 	struct ipv6hdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v6 = {
-	.eth.h_proto = bpf_htons(ETH_P_IPV6),
+	.eth.h_proto = __constant_htons(ETH_P_IPV6),
 	.iph.nexthdr = 6,
-	.iph.payload_len = bpf_htons(MAGIC_BYTES),
+	.iph.payload_len = __constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
-- 
2.20.0.rc2.403.gdbc3b29805-goog

Re: [PATCH bpf-next] selftests/bpf: don't use bpf helpers in non-bpf environment

From: Daniel Borkmann <daniel@iogearbox.net>
Date: 2018-12-12 01:56:43

On 12/11/2018 10:49 PM, Stanislav Fomichev wrote:
On 12/11, Stanislav Fomichev wrote:
quoted
We're using bpf_htons in test_progs.c to initialize some static
global data and I think I hit some weird case on an older compiler
which doesn't have __builtin_bswap16 (and __builtin_constant_p
expands to false).

In this case I see:
error: implicit declaration of function '__builtin_bswap16'
Is that gcc < 4.8?
quoted
Let's explicitly use __constant_htons which should be exposed by the
linux/byteorder.h uapi header.
Forgot to mention, that using simple htons produces the following:
    test_progs.c:54:17: error: braced-group within expression allowed only
    inside a function
      .eth.h_proto = htons(ETH_P_IP),
quoted
Signed-off-by: Stanislav Fomichev <redacted>
---
 tools/testing/selftests/bpf/test_progs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 26f1fdf3e2bf..61593d319c0e 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -51,10 +51,10 @@ static struct {
 	struct iphdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v4 = {
-	.eth.h_proto = bpf_htons(ETH_P_IP),
+	.eth.h_proto = __constant_htons(ETH_P_IP),
If the __builtin_constant_p() evaluated to false on the constants (?),
wouldn't using the __bpf_constant_htons() directly work as well given
it's not using a builtin either? Should be fine either way though using
the same api/header might be slightly nicer.
quoted
 	.iph.ihl = 5,
 	.iph.protocol = 6,
-	.iph.tot_len = bpf_htons(MAGIC_BYTES),
+	.iph.tot_len = __constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
@@ -64,9 +64,9 @@ static struct {
 	struct ipv6hdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v6 = {
-	.eth.h_proto = bpf_htons(ETH_P_IPV6),
+	.eth.h_proto = __constant_htons(ETH_P_IPV6),
 	.iph.nexthdr = 6,
-	.iph.payload_len = bpf_htons(MAGIC_BYTES),
+	.iph.payload_len = __constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
-- 
2.20.0.rc2.403.gdbc3b29805-goog

Re: [PATCH bpf-next] selftests/bpf: don't use bpf helpers in non-bpf environment

From: Stanislav Fomichev <sdf@fomichev.me>
Date: 2018-12-12 03:13:39

On 12/12, Daniel Borkmann wrote:
On 12/11/2018 10:49 PM, Stanislav Fomichev wrote:
quoted
On 12/11, Stanislav Fomichev wrote:
quoted
We're using bpf_htons in test_progs.c to initialize some static
global data and I think I hit some weird case on an older compiler
which doesn't have __builtin_bswap16 (and __builtin_constant_p
expands to false).

In this case I see:
error: implicit declaration of function '__builtin_bswap16'
Is that gcc < 4.8?
Yes.
quoted
quoted
Let's explicitly use __constant_htons which should be exposed by the
linux/byteorder.h uapi header.
Forgot to mention, that using simple htons produces the following:
    test_progs.c:54:17: error: braced-group within expression allowed only
    inside a function
      .eth.h_proto = htons(ETH_P_IP),
quoted
Signed-off-by: Stanislav Fomichev <redacted>
---
 tools/testing/selftests/bpf/test_progs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 26f1fdf3e2bf..61593d319c0e 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -51,10 +51,10 @@ static struct {
 	struct iphdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v4 = {
-	.eth.h_proto = bpf_htons(ETH_P_IP),
+	.eth.h_proto = __constant_htons(ETH_P_IP),
If the __builtin_constant_p() evaluated to false on the constants (?),
wouldn't using the __bpf_constant_htons() directly work as well given
it's not using a builtin either? Should be fine either way though using
the same api/header might be slightly nicer.
I got it wrong, __builtin_constant_p() evaluates correctly, I played
with it a bit. But for some reason it still complains about that branch
that it doesn't take :-/

Using __bpf_constant_htons() is a good idea, I'll follow up with a v2.
quoted
quoted
 	.iph.ihl = 5,
 	.iph.protocol = 6,
-	.iph.tot_len = bpf_htons(MAGIC_BYTES),
+	.iph.tot_len = __constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
@@ -64,9 +64,9 @@ static struct {
 	struct ipv6hdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v6 = {
-	.eth.h_proto = bpf_htons(ETH_P_IPV6),
+	.eth.h_proto = __constant_htons(ETH_P_IPV6),
 	.iph.nexthdr = 6,
-	.iph.payload_len = bpf_htons(MAGIC_BYTES),
+	.iph.payload_len = __constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
-- 
2.20.0.rc2.403.gdbc3b29805-goog

[PATCH v2 bpf-next] selftests/bpf: use __bpf_constant_htons in test_prog.c

From: Stanislav Fomichev <hidden>
Date: 2018-12-12 03:20:57

For some reason, my older GCC (< 4.8) isn't smart enough to optimize the
!__builtin_constant_p() branch in bpf_htons, I see:
  error: implicit declaration of function '__builtin_bswap16'

Let's use __bpf_constant_htons as suggested by Daniel Borkmann.

I tried to use simple htons, but it produces the following:
  test_progs.c:54:17: error: braced-group within expression allowed only
  inside a function
    .eth.h_proto = htons(ETH_P_IP),

Signed-off-by: Stanislav Fomichev <redacted>
---
 tools/testing/selftests/bpf/test_progs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 26f1fdf3e2bf..126fc624290d 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -51,10 +51,10 @@ static struct {
 	struct iphdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v4 = {
-	.eth.h_proto = bpf_htons(ETH_P_IP),
+	.eth.h_proto = __bpf_constant_htons(ETH_P_IP),
 	.iph.ihl = 5,
 	.iph.protocol = 6,
-	.iph.tot_len = bpf_htons(MAGIC_BYTES),
+	.iph.tot_len = __bpf_constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
@@ -64,9 +64,9 @@ static struct {
 	struct ipv6hdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v6 = {
-	.eth.h_proto = bpf_htons(ETH_P_IPV6),
+	.eth.h_proto = __bpf_constant_htons(ETH_P_IPV6),
 	.iph.nexthdr = 6,
-	.iph.payload_len = bpf_htons(MAGIC_BYTES),
+	.iph.payload_len = __bpf_constant_htons(MAGIC_BYTES),
 	.tcp.urg_ptr = 123,
 };
 
-- 
2.20.0.rc2.403.gdbc3b29805-goog

Re: [PATCH bpf-next] selftests/bpf: don't use bpf helpers in non-bpf environment

From: Daniel Borkmann <daniel@iogearbox.net>
Date: 2018-12-12 09:05:03

On 12/12/2018 04:13 AM, Stanislav Fomichev wrote:
On 12/12, Daniel Borkmann wrote:
quoted
On 12/11/2018 10:49 PM, Stanislav Fomichev wrote:
quoted
On 12/11, Stanislav Fomichev wrote:
quoted
We're using bpf_htons in test_progs.c to initialize some static
global data and I think I hit some weird case on an older compiler
which doesn't have __builtin_bswap16 (and __builtin_constant_p
expands to false).

In this case I see:
error: implicit declaration of function '__builtin_bswap16'
Is that gcc < 4.8?
Yes.
quoted
quoted
quoted
Let's explicitly use __constant_htons which should be exposed by the
linux/byteorder.h uapi header.
Forgot to mention, that using simple htons produces the following:
    test_progs.c:54:17: error: braced-group within expression allowed only
    inside a function
      .eth.h_proto = htons(ETH_P_IP),
quoted
Signed-off-by: Stanislav Fomichev <redacted>
---
 tools/testing/selftests/bpf/test_progs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 26f1fdf3e2bf..61593d319c0e 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -51,10 +51,10 @@ static struct {
 	struct iphdr iph;
 	struct tcphdr tcp;
 } __packed pkt_v4 = {
-	.eth.h_proto = bpf_htons(ETH_P_IP),
+	.eth.h_proto = __constant_htons(ETH_P_IP),
If the __builtin_constant_p() evaluated to false on the constants (?),
wouldn't using the __bpf_constant_htons() directly work as well given
it's not using a builtin either? Should be fine either way though using
the same api/header might be slightly nicer.
I got it wrong, __builtin_constant_p() evaluates correctly, I played
with it a bit. But for some reason it still complains about that branch
that it doesn't take :-/

Using __bpf_constant_htons() is a good idea, I'll follow up with a v2.
Ok, sounds good, thanks!

Re: [PATCH v2 bpf-next] selftests/bpf: use __bpf_constant_htons in test_prog.c

From: Daniel Borkmann <daniel@iogearbox.net>
Date: 2018-12-12 09:50:45

On 12/12/2018 04:20 AM, Stanislav Fomichev wrote:
For some reason, my older GCC (< 4.8) isn't smart enough to optimize the
!__builtin_constant_p() branch in bpf_htons, I see:
  error: implicit declaration of function '__builtin_bswap16'

Let's use __bpf_constant_htons as suggested by Daniel Borkmann.

I tried to use simple htons, but it produces the following:
  test_progs.c:54:17: error: braced-group within expression allowed only
  inside a function
    .eth.h_proto = htons(ETH_P_IP),

Signed-off-by: Stanislav Fomichev <redacted>
Applied, thanks!
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help