Skip to contentSkip to conversation

tc: bpf: add checksum helpers

PATCH Applied Alexei Starovoitov 7 participants 9 messages Apr 2, 2015 – Apr 6, 2015 3 messages not in the archive

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tc: bpf: add checksum helpers

Commit 608cd71a9c7c ("tc: bpf: generalize pedit action") has added the possibility to mangle packet data to BPF programs in the tc pipeline. This patch adds two helpers bpf_l3_csum_replace() and bpf_l4_csum_replace() for fixing up the protocol checksums after the packet mangling.

It also adds 'flags' argument to bpf_skb_store_bytes() helper to avoid unnecessary checksum recomputations when BPF programs adjusting l3/l4 checksums and documents all three helpers in uapi header.

Moreover, a sample program is added to show how BPF programs can make use of the mangle and csum helpers.

---
 include/uapi/linux/bpf.h  |   38 +++++++++++++++-
 net/core/filter.c         |  108 +++++++++++++++++++++++++++++++++++++++++++--
 samples/bpf/Makefile      |    1 +
 samples/bpf/bpf_helpers.h |    7 +++
 samples/bpf/tcbpf1_kern.c |   71 +++++++++++++++++++++++++++++
 5 files changed, 220 insertions(+), 5 deletions(-)
 create mode 100644 samples/bpf/tcbpf1_kern.c
include/uapi/linux/bpf.h +37 -1
index 74aab6e0d964..c8e3c29a080e 100644
@@ -168,7 +168,43 @@ enum bpf_func_id {
 	BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
 	BPF_FUNC_get_prandom_u32, /* u32 prandom_u32(void) */
 	BPF_FUNC_get_smp_processor_id, /* u32 raw_smp_processor_id(void) */
-	BPF_FUNC_skb_store_bytes, /* int skb_store_bytes(skb, offset, from, len) */
+
+	/**
+	 * skb_store_bytes(skb, offset, from, len, flags) - store bytes into packet
+	 * @skb: pointer to skb
+	 * @offset: offset within packet from skb->data
+	 * @from: pointer where to copy bytes from
+	 * @len: number of bytes to store into packet
+	 * @flags: bit 0 - if true, recompute skb->csum
+	 *         other bits - reserved
+	 * Return: 0 on success
+	 */
+	BPF_FUNC_skb_store_bytes,
+
+	/**
+	 * l3_csum_replace(skb, offset, from, to, flags) - recompute IP checksum
+	 * @skb: pointer to skb
+	 * @offset: offset within packet where IP checksum is located
+	 * @from: old value of header field
+	 * @to: new value of header field
+	 * @flags: bits 0-3 - size of header field
+	 *         other bits - reserved
+	 * Return: 0 on success
+	 */
+	BPF_FUNC_l3_csum_replace,
+
+	/**
+	 * l4_csum_replace(skb, offset, from, to, flags) - recompute TCP/UDP checksum
+	 * @skb: pointer to skb
+	 * @offset: offset within packet where TCP/UDP checksum is located
+	 * @from: old value of header field
+	 * @to: new value of header field
+	 * @flags: bits 0-3 - size of header field
+	 *         bit 4 - is pseudo header
+	 *         other bits - reserved
+	 * Return: 0 on success
+	 */
+	BPF_FUNC_l4_csum_replace,
 	__BPF_FUNC_MAX_ID,
 };
 
net/core/filter.c +104 -4

144 lines not on this page · raw message

samples/bpf/Makefile +1 -0
index b5b3600dcdf5..d24f51bca465 100644
@@ -17,6 +17,7 @@ sockex2-objs := bpf_load.o libbpf.o sockex2_user.o
 always := $(hostprogs-y)
 always += sockex1_kern.o
 always += sockex2_kern.o
+always += tcbpf1_kern.o
 
 HOSTCFLAGS += -I$(objtree)/usr/include
 
samples/bpf/bpf_helpers.h +7 -0
index ca0333146006..72540ec1f003 100644
@@ -37,4 +37,11 @@ struct bpf_map_def {
 	unsigned int max_entries;
 };
 
+static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) =
+	(void *) BPF_FUNC_skb_store_bytes;
+static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) =
+	(void *) BPF_FUNC_l3_csum_replace;
+static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) =
+	(void *) BPF_FUNC_l4_csum_replace;
+
 #endif
samples/bpf/tcbpf1_kern.c new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
1.7.9.5

A message that is not in the archive (sent off-list or not yet received): 1427933533-14394-1-git-send-email-ast-uqk4Ao+rVK5Wk0Htik3J/[email protected]

#2 lore raw
Quoted 5 lines · Alexei Starovoitov

Commit 608cd71a9c7c ("tc: bpf: generalize pedit action") has added the possibility to mangle packet data to BPF programs in the tc pipeline. This patch adds two helpers bpf_l3_csum_replace() and bpf_l4_csum_replace() for fixing up the protocol checksums after the packet mangling.

Quoted 8 lines

It also adds 'flags' argument to bpf_skb_store_bytes() helper to avoid unnecessary checksum recomputations when BPF programs adjusting l3/l4 checksums and documents all three helpers in uapi header.

Moreover, a sample program is added to show how BPF programs can make use of the mangle and csum helpers.

I think in future we need to find a better place for these helpers, as they are for cls_bpf and act_bpf.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 10 lines · Thu, Apr 02, 2015 at 05:00:47PM CEST, [email protected]…
Quoted 9 lines · Alexei Starovoitov

Commit 608cd71a9c7c ("tc: bpf: generalize pedit action") has added the possibility to mangle packet data to BPF programs in the tc pipeline. This patch adds two helpers bpf_l3_csum_replace() and bpf_l4_csum_replace() for fixing up the protocol checksums after the packet mangling.

It also adds 'flags' argument to bpf_skb_store_bytes() helper to avoid unnecessary checksum recomputations when BPF programs adjusting l3/l4 checksums and documents all three helpers in uapi header.

Quoted 7 lines
Quoted 4 lines

Moreover, a sample program is added to show how BPF programs can make use of the mangle and csum helpers.

I think in future we need to find a better place for these helpers, as they are for cls_bpf and act_bpf.

Yeah, makes sense to now have it in net/core/filter.c. How about to create net/bpf dir for these?

Quoted 2 lines
maintainer
#5 lore raw
Quoted 14 lines · Jiri Pirko
Quoted 13 lines · Thu, Apr 02, 2015 at 05:00:47PM CEST, [email protected] wrote:
Quoted 12 lines · Alexei Starovoitov

Commit 608cd71a9c7c ("tc: bpf: generalize pedit action") has added the possibility to mangle packet data to BPF programs in the tc pipeline. This patch adds two helpers bpf_l3_csum_replace() and bpf_l4_csum_replace() for fixing up the protocol checksums after the packet mangling.

It also adds 'flags' argument to bpf_skb_store_bytes() helper to avoid unnecessary checksum recomputations when BPF programs adjusting l3/l4 checksums and documents all three helpers in uapi header.

Moreover, a sample program is added to show how BPF programs can make use of the mangle and csum helpers.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I think in future we need to find a better place for these helpers, as they are for cls_bpf and act_bpf.

Yeah, makes sense to now have it in net/core/filter.c. How about to create net/bpf dir for these?

I was thinking somewhere under net/sched/, f.e. net/sched/bpf.c as probably kernel/bpf/ would end up in too much ifdef pasta. :/

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw
Quoted 19 lines · Daniel Borkmann
Quoted 18 lines · Jiri Pirko
Quoted 17 lines · Thu, Apr 02, 2015 at 05:00:47PM CEST, [email protected]…
Quoted 15 lines · Alexei Starovoitov

Commit 608cd71a9c7c ("tc: bpf: generalize pedit action") has added the possibility to mangle packet data to BPF programs in the tc pipeline. This patch adds two helpers bpf_l3_csum_replace() and bpf_l4_csum_replace()
for fixing up the protocol checksums after the packet mangling.

It also adds 'flags' argument to bpf_skb_store_bytes() helper to avoid unnecessary checksum recomputations when BPF programs adjusting l3/l4 checksums and documents all three helpers in uapi header.

Moreover, a sample program is added to show how BPF programs can make use
of the mangle and csum helpers.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I think in future we need to find a better place for these helpers, as they are for cls_bpf and act_bpf.

Yeah, makes sense to now have it in net/core/filter.c. How about to create net/bpf dir for these?

I was thinking somewhere under net/sched/, f.e. net/sched/bpf.c as probably kernel/bpf/ would end up in too much ifdef pasta. :/

I would prefer to keep mangle and csum helpers in net/core/filter.c for now, since it's not clear what networking subsystems beyond TC would want to use them in the future. Otherwise we'll keep moving them back and forth.

#8 lore raw
Quoted 26 lines · Thu, Apr 02, 2015 at 06:34:59PM CEST, [email protected] wrote:
Quoted 25 lines · Daniel Borkmann
Quoted 23 lines · Jiri Pirko
Quoted 11 lines · Thu, Apr 02, 2015 at 05:00:47PM CEST, [email protected] wrote:
Quoted 10 lines · Alexei Starovoitov

Commit 608cd71a9c7c ("tc: bpf: generalize pedit action") has added the possibility to mangle packet data to BPF programs in the tc pipeline. This patch adds two helpers bpf_l3_csum_replace() and bpf_l4_csum_replace()
for fixing up the protocol checksums after the packet mangling.

It also adds 'flags' argument to bpf_skb_store_bytes() helper to avoid unnecessary checksum recomputations when BPF programs adjusting l3/l4 checksums and documents all three helpers in uapi header.

Quoted 8 lines
Quoted 5 lines

Moreover, a sample program is added to show how BPF programs can make use
of the mangle and csum helpers.

I think in future we need to find a better place for these helpers, as they are for cls_bpf and act_bpf.

Yeah, makes sense to now have it in net/core/filter.c. How about to create net/bpf dir for these?

Quoted 7 lines
Quoted 2 lines

I was thinking somewhere under net/sched/, f.e. net/sched/bpf.c as probably kernel/bpf/ would end up in too much ifdef pasta. :/

I would prefer to keep mangle and csum helpers in net/core/filter.c for now, since it's not clear what networking subsystems beyond TC would want to use them in the future. Otherwise we'll keep moving them back and forth.

Well, therefore I suggested net/bpf/ dir as a place to store net-subsystem specific bpf stuff.

Quoted 1 line
#9 lore raw
Quoted 30 lines · Jiri Pirko
Quoted 29 lines · Thu, Apr 02, 2015 at 06:34:59PM CEST, [email protected] wrote:
Quoted 27 lines · Daniel Borkmann
Quoted 16 lines · Jiri Pirko
Quoted 15 lines · Thu, Apr 02, 2015 at 05:00:47PM CEST, [email protected] wrote:
Quoted 14 lines · Alexei Starovoitov

Commit 608cd71a9c7c ("tc: bpf: generalize pedit action") has added the possibility to mangle packet data to BPF programs in the tc pipeline. This patch adds two helpers bpf_l3_csum_replace() and bpf_l4_csum_replace()
for fixing up the protocol checksums after the packet mangling.

It also adds 'flags' argument to bpf_skb_store_bytes() helper to avoid unnecessary checksum recomputations when BPF programs adjusting l3/l4 checksums and documents all three helpers in uapi header.

Moreover, a sample program is added to show how BPF programs can make use
of the mangle and csum helpers.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I think in future we need to find a better place for these helpers, as they are for cls_bpf and act_bpf.

Yeah, makes sense to now have it in net/core/filter.c. How about to create net/bpf dir for these?

I was thinking somewhere under net/sched/, f.e. net/sched/bpf.c as probably kernel/bpf/ would end up in too much ifdef pasta. :/

Quoted 7 lines
Quoted 4 lines

I would prefer to keep mangle and csum helpers in net/core/filter.c for now, since it's not clear what networking subsystems beyond TC would want to use them in the future. Otherwise we'll keep moving them back and forth.

Well, therefore I suggested net/bpf/ dir as a place to store net-subsystem specific bpf stuff.

ahh. misread your earlier statement. yes. makes sense in long term. Right now feels too early to create a dir for just 3 functions. Eventually half of filter.c probably belongs in there.

maintainer
#10 lore raw
Quoted 36 lines · Alexei Starovoitov
Quoted 35 lines · Jiri Pirko
Quoted 33 lines · Thu, Apr 02, 2015 at 06:34:59PM CEST, [email protected] wrote:
Quoted 19 lines · Daniel Borkmann
Quoted 18 lines · Jiri Pirko
Quoted 17 lines · Thu, Apr 02, 2015 at 05:00:47PM CEST, [email protected] wrote:
Quoted 15 lines · Alexei Starovoitov

Commit 608cd71a9c7c ("tc: bpf: generalize pedit action") has added the possibility to mangle packet data to BPF programs in the tc pipeline. This patch adds two helpers bpf_l3_csum_replace() and bpf_l4_csum_replace()
for fixing up the protocol checksums after the packet mangling.

It also adds 'flags' argument to bpf_skb_store_bytes() helper to avoid unnecessary checksum recomputations when BPF programs adjusting l3/l4 checksums and documents all three helpers in uapi header.

Moreover, a sample program is added to show how BPF programs can make use
of the mangle and csum helpers.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I think in future we need to find a better place for these helpers, as they are for cls_bpf and act_bpf.

Yeah, makes sense to now have it in net/core/filter.c. How about to create net/bpf dir for these?

I was thinking somewhere under net/sched/, f.e. net/sched/bpf.c as probably kernel/bpf/ would end up in too much ifdef pasta. :/

I would prefer to keep mangle and csum helpers in net/core/filter.c for now, since it's not clear what networking subsystems beyond TC would want to use them in the future. Otherwise we'll keep moving them back and forth.

Quoted 6 lines
Quoted 2 lines

Well, therefore I suggested net/bpf/ dir as a place to store net-subsystem specific bpf stuff.

ahh. misread your earlier statement. yes. makes sense in long term. Right now feels too early to create a dir for just 3 functions. Eventually half of filter.c probably belongs in there.

Agreed, yeah. I think we should keep that refactoring in mind in the mid-long term, but I guess a single file somewhere under net/core/ might be enough then.

maintainer Applied
#11 lore raw
In reply to Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>
Date: Wed, 1 Apr 2015 17:12:13 -0700

Quoted 5 lines

Commit 608cd71a9c7c ("tc: bpf: generalize pedit action") has added the possibility to mangle packet data to BPF programs in the tc pipeline. This patch adds two helpers bpf_l3_csum_replace() and bpf_l4_csum_replace() for fixing up the protocol checksums after the packet mangling.

Quoted 8 lines

It also adds 'flags' argument to bpf_skb_store_bytes() helper to avoid unnecessary checksum recomputations when BPF programs adjusting l3/l4 checksums and documents all three helpers in uapi header.

Moreover, a sample program is added to show how BPF programs can make use of the mangle and csum helpers.

Applied, thanks.