Skip to contentSkip to conversation

net: export pkt_type_offset() helper

PATCH Superseded Denis Kirjanov 4 participants 4 messages Sep 2, 2014 – Sep 3, 2014

Subsystems (9)
Networking Network drivers MIPS BPF +5 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: export pkt_type_offset() helper

Currently we have 2 pkt_type_offset functions doing the same thing and spread across the architecture files. Let's use the generic helper routine.

Cc Markos Chandras <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 arch/mips/net/bpf_jit.c      | 21 ---------------------
 arch/s390/net/bpf_jit_comp.c | 38 ++++----------------------------------
 include/linux/filter.h       |  7 +++++++
 net/core/filter.c            |  7 +------
 4 files changed, 12 insertions(+), 61 deletions(-)
arch/mips/net/bpf_jit.c +0 -21
index 05a5661..08ff823 100644
@@ -765,27 +765,6 @@ static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
 	return (u64)err << 32 | ntohl(ret);
 }
 
-#ifdef __BIG_ENDIAN_BITFIELD
-#define PKT_TYPE_MAX	(7 << 5)
-#else
-#define PKT_TYPE_MAX	7
-#endif
-static int pkt_type_offset(void)
-{
-	struct sk_buff skb_probe = {
-		.pkt_type = ~0,
-	};
-	u8 *ct = (u8 *)&skb_probe;
-	unsigned int off;
-
-	for (off = 0; off < sizeof(struct sk_buff); off++) {
-		if (ct[off] == PKT_TYPE_MAX)
-			return off;
-	}
-	pr_err_once("Please fix pkt_type_offset(), as pkt_type couldn't be found\n");
-	return -1;
-}
-
 static int build_body(struct jit_ctx *ctx)
 {
 	void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
arch/s390/net/bpf_jit_comp.c +4 -34
index 61e45b7..34383d1 100644
@@ -223,37 +223,6 @@ static void bpf_jit_epilogue(struct bpf_jit *jit)
 	EMIT2(0x07fe);
 }
 
-/* Helper to find the offset of pkt_type in sk_buff
- * Make sure its still a 3bit field starting at the MSBs within a byte.
- */
-#define PKT_TYPE_MAX 0xe0
-static int pkt_type_offset;
-
-static int __init bpf_pkt_type_offset_init(void)
-{
-	struct sk_buff skb_probe = {
-		.pkt_type = ~0,
-	};
-	char *ct = (char *)&skb_probe;
-	int off;
-
-	pkt_type_offset = -1;
-	for (off = 0; off < sizeof(struct sk_buff); off++) {
-		if (!ct[off])
-			continue;
-		if (ct[off] == PKT_TYPE_MAX)
-			pkt_type_offset = off;
-		else {
-			/* Found non matching bit pattern, fix needed. */
-			WARN_ON_ONCE(1);
-			pkt_type_offset = -1;
-			return -1;
-		}
-	}
-	return 0;
-}
-device_initcall(bpf_pkt_type_offset_init);
-
 /*
  * make sure we dont leak kernel information to user
  */
@@ -753,12 +722,13 @@ call_fn:	/* lg %r1,<d(function)>(%r13) */
 		}
 		break;
 	case BPF_ANC | SKF_AD_PKTTYPE:
-		if (pkt_type_offset < 0)
+		offset = pkt_type_offset();
+		if (offset < 0)
 			goto out;
 		/* lhi %r5,0 */
 		EMIT4(0xa7580000);
-		/* ic %r5,<d(pkt_type_offset)>(%r2) */
-		EMIT4_DISP(0x43502000, pkt_type_offset);
+		/* ic %r5,<d(offset)>(%r2) */
+		EMIT4_DISP(0x43502000, offset);
 		/* srl %r5,5 */
 		EMIT4_DISP(0x88500000, 5);
 		break;
include/linux/filter.h +7 -0
index a5227ab..97e0549 100644
@@ -424,6 +424,13 @@ static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
 	return bpf_internal_load_pointer_neg_helper(skb, k, size);
 }
 
+#ifdef __BIG_ENDIAN_BITFIELD
+#define PKT_TYPE_MAX	(7 << 5)
+#else
+#define PKT_TYPE_MAX    7
+#endif
+unsigned int pkt_type_offset(void);
+
 #ifdef CONFIG_BPF_JIT
 #include <stdarg.h>
 #include <linux/linkage.h>
net/core/filter.c +1 -6
index d814b8a..b4a2e00 100644
@@ -91,12 +91,7 @@ EXPORT_SYMBOL(sk_filter);
  * to make sure its still a 3bit field starting at a byte boundary;
  * taken from arch/x86/net/bpf_jit_comp.c.
  */
-#ifdef __BIG_ENDIAN_BITFIELD
-#define PKT_TYPE_MAX	(7 << 5)
-#else
-#define PKT_TYPE_MAX	7
-#endif
-static unsigned int pkt_type_offset(void)
+unsigned int pkt_type_offset(void)
 {
 	struct sk_buff skb_probe = { .pkt_type = ~0, };
 	u8 *ct = (u8 *) &skb_probe;
Signature · 1 line
2.0.0
#1 lore raw
Quoted 3 lines · Denis Kirjanov

Currently we have 2 pkt_type_offset functions doing the same thing and spread across the architecture files. Let's use the generic helper routine.

that would be good cleanup.
Please tag your subject as '[PATCH net-next]...'

Quoted 14 lines
Cc Markos Chandras <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 arch/mips/net/bpf_jit.c      | 21 ---------------------
 arch/s390/net/bpf_jit_comp.c | 38 ++++----------------------------------
 include/linux/filter.h       |  7 +++++++
 net/core/filter.c            |  7 +------
 4 files changed, 12 insertions(+), 61 deletions(-)
arch/mips/net/bpf_jit.c +0 -2
-#define PKT_TYPE_MAX 0xe0
-static int pkt_type_offset;

...

arch/mips/net/bpf_jit.c

Quoted 2 lines
arch/mips/net/bpf_jit.c +0 -2
-}
-device_initcall(bpf_pkt_type_offset_init);

may be make sense to optimize common case via initcall() as well instead of dropping it for s390?

include/linux/filter.h static inline void *bpf_load_pointer(const struct sk_buff *skb, int k, · line 431

Quoted 13 lines
include/linux/filter.h +5 -0
index a5227ab..97e0549 100644
@@ -424,6 +424,13 @@ static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
 	return bpf_internal_load_pointer_neg_helper(skb, k, size);
 }
 
+#ifdef __BIG_ENDIAN_BITFIELD
+#define PKT_TYPE_MAX	(7 << 5)
+#else
+#define PKT_TYPE_MAX    7
+#endif

these defines don't need to move. keep them in filter.c

#2 lore raw
Quoted 23 lines · Alexei Starovoitov
Quoted 3 lines · Denis Kirjanov

Currently we have 2 pkt_type_offset functions doing the same thing and spread across the architecture files. Let's use the generic helper routine.

that would be good cleanup.
Please tag your subject as '[PATCH net-next]...'

Quoted 14 lines
Cc Markos Chandras <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
  arch/mips/net/bpf_jit.c      | 21 ---------------------
  arch/s390/net/bpf_jit_comp.c | 38 ++++----------------------------------
  include/linux/filter.h       |  7 +++++++
  net/core/filter.c            |  7 +------
  4 files changed, 12 insertions(+), 61 deletions(-)
arch/mips/net/bpf_jit.c +0 -2
-#define PKT_TYPE_MAX 0xe0
-static int pkt_type_offset;

...

Quoted 5 lines

arch/mips/net/bpf_jit.c

Quoted 2 lines
arch/mips/net/bpf_jit.c +0 -2
-}
-device_initcall(bpf_pkt_type_offset_init);

may be make sense to optimize common case via initcall() as well instead of dropping it for s390?

I think that probably might not be a bad idea. pkt_type_offset should be a __read_mostly candidate then. As we have things like PTP classifier that is being loaded in core_initcall(), we might need to initialize pkt_type_offset then via pure_initcall(), for example. I think that should be doable, as it doesn't have any dependencies elsewhere.

Quoted 16 lines

include/linux/filter.h static inline void *bpf_load_pointer(const struct sk_buff *skb, int k, · line 431

Quoted 13 lines
include/linux/filter.h +5 -0
index a5227ab..97e0549 100644
@@ -424,6 +424,13 @@ static inline void *bpf_load_pointer(const struct sk_buff *skb, int k,
  	return bpf_internal_load_pointer_neg_helper(skb, k, size);
  }
 
+#ifdef __BIG_ENDIAN_BITFIELD
+#define PKT_TYPE_MAX	(7 << 5)
+#else
+#define PKT_TYPE_MAX    7
+#endif

these defines don't need to move. keep them in filter.c

#3 lore raw
In reply to Alexei Starovoitov
Quoted 23 lines · Alexei Starovoitov
Quoted 3 lines · Denis Kirjanov

Currently we have 2 pkt_type_offset functions doing the same thing and spread across the architecture files. Let's use the generic helper routine.

that would be good cleanup.
Please tag your subject as '[PATCH net-next]...'

Quoted 14 lines
Cc Markos Chandras <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Daniel Borkmann <[email protected]>
---
 arch/mips/net/bpf_jit.c      | 21 ---------------------
 arch/s390/net/bpf_jit_comp.c | 38 ++++----------------------------------
 include/linux/filter.h       |  7 +++++++
 net/core/filter.c            |  7 +------
 4 files changed, 12 insertions(+), 61 deletions(-)
arch/mips/net/bpf_jit.c +0 -2
-#define PKT_TYPE_MAX 0xe0
-static int pkt_type_offset;

...

Quoted 5 lines

arch/mips/net/bpf_jit.c

Quoted 2 lines
arch/mips/net/bpf_jit.c +0 -2
-}
-device_initcall(bpf_pkt_type_offset_init);

may be make sense to optimize common case via initcall() as well instead of dropping it for s390?

Yeah, it's a better option.

Quoted 10 lines
Quoted 10 lines
include/linux/filter.h +0 -0
index a5227ab..97e0549 100644
@@ -424,6 +424,13 @@ static inline void *bpf_load_pointer(const struct
 sk_buff *skb, int k,
 	return bpf_internal_load_pointer_neg_helper(skb, k, size);
 }
 
Quoted 7 lines

include/linux/filter.h static inline void *bpf_load_pointer(const struct sk_buff *skb, int k, · line 431

Quoted 5 lines
include/linux/filter.h +5 -0
+#ifdef __BIG_ENDIAN_BITFIELD
+#define PKT_TYPE_MAX	(7 << 5)
+#else
+#define PKT_TYPE_MAX    7
+#endif

these defines don't need to move. keep them in filter.c

I'll respin. Thanks!

Quoted 6 lines

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Regards,
Denis