Skip to contentSkip to conversation

Add skb + xdp dynptrs

PATCH Superseded Joanne Koong 2 participants 9 messages Sep 7, 2022 – Oct 20, 2022

Subsystems (8)

v6 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v6 cover Add skb + xdp dynptrs

This patchset is the 2nd in the dynptr series. The 1st can be found here [0].

This patchset adds skb and xdp type dynptrs, which have two main benefits for packet parsing:
* allowing operations on sizes that are not statically known at compile-time (eg variable-sized accesses).
* more ergonomic and less brittle iteration through data (eg does not need manual if checking for being within bounds of data_end)

When comparing the differences in runtime for packet parsing without dynptrs vs. with dynptrs for the more simple cases, there is no noticeable difference. For the more complex cases where lengths are non-statically known at compile time, there can be a significant speed-up when using dynptrs (eg a 2x speed up for cls redirection). Patch 3 contains more details as well as examples of how to use skb and xdp dynptrs.

[0] https://lore.kernel.org/bpf/[email protected]/

--
Changelog:

v5 = https://lore.kernel.org/bpf/[email protected]/
v5 -> v6
* Address kernel test robot errors by static inlining

v4 = https://lore.kernel.org/bpf/[email protected]/
v4 -> v5
* Address kernel test robot errors for configs w/out CONFIG_NET set
* For data slices, return PTR_TO_MEM instead of PTR_TO_PACKET (Kumar)
* Split selftests into subtests (Andrii)
* Remove insn patching. Use rdonly and rdwr protos for dynptr skb construction (Andrii)
* bpf_dynptr_data() returns NULL for rd-only dynptrs. There will be a separate bpf_dynptr_data_rdonly() added later (Andrii and Kumar)

v3 = https://lore.kernel.org/bpf/[email protected]/
v3 -> v4
* Forgot to commit --amend the kernel test robot error fixups

v2 = https://lore.kernel.org/bpf/[email protected]/
v2 -> v3
* Fix kernel test robot build test errors

v1 = https://lore.kernel.org/bpf/[email protected]/
v1 -> v2
* Return data slices to rd-only skb dynptrs (Martin)
* bpf_dynptr_write allows writes to frags for skb dynptrs, but always invalidates associated data slices (Martin)
* Use switch casing instead of ifs (Andrii)
* Use 0xFD for experimental kind number in the selftest (Zvi)
* Put selftest conversions w/ dynptrs into new files (Alexei)
* Add new selftest "test_cls_redirect_dynptr.c"

Joanne Koong (3):
bpf: Add skb dynptrs
bpf: Add xdp dynptrs
selftests/bpf: tests for using dynptrs to parse skb and xdp buffers

 include/linux/bpf.h                           |  86 +-
 include/linux/filter.h                        |  38 +
 include/uapi/linux/bpf.h                      |  62 +-
 kernel/bpf/helpers.c                          |  83 +-
 kernel/bpf/verifier.c                         |  96 +-
 net/core/filter.c                             | 118 ++-
 tools/include/uapi/linux/bpf.h                |  62 +-
 .../selftests/bpf/prog_tests/cls_redirect.c   |  25 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |  73 +-
 .../selftests/bpf/prog_tests/l4lb_all.c       |   2 +
 .../bpf/prog_tests/parse_tcp_hdr_opt.c        |  93 ++
 .../selftests/bpf/prog_tests/xdp_attach.c     |  11 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c |  92 ++
 .../selftests/bpf/progs/dynptr_success.c      |  32 +
 .../bpf/progs/test_cls_redirect_dynptr.c      | 968 ++++++++++++++++++
 .../bpf/progs/test_l4lb_noinline_dynptr.c     | 469 +++++++++
 .../bpf/progs/test_parse_tcp_hdr_opt.c        | 119 +++
 .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 110 ++
 .../selftests/bpf/progs/test_xdp_dynptr.c     | 235 +++++
 .../selftests/bpf/test_tcp_hdr_options.h      |   1 +
 20 files changed, 2689 insertions(+), 86 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_dynptr.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v6 1/3 bpf: Add skb dynptrs

Add skb dynptrs, which are dynptrs whose underlying pointer points to a skb. The dynptr acts on skb data. skb dynptrs have two main benefits. One is that they allow operations on sizes that are not statically known at compile-time (eg variable-sized accesses). Another is that parsing the packet data through dynptrs (instead of through direct access of skb->data and skb->data_end) can be more ergonomic and less brittle (eg does not need manual if checking for being within bounds of data_end).

For bpf prog types that don't support writes on skb data, the dynptr is read-only (bpf_dynptr_write() will return an error and bpf_dynptr_data() will return NULL; for a read-only data slice, there will be a separate API bpf_dynptr_data_rdonly(), which will be added in the near future).

For reads and writes through the bpf_dynptr_read() and bpf_dynptr_write() interfaces, reading and writing from/to data in the head as well as from/to non-linear paged buffers is supported. For data slices (through the bpf_dynptr_data() interface), if the data is in a paged buffer, the user must first call bpf_skb_pull_data() to pull the data into the linear portion.

Any bpf_dynptr_write() automatically invalidates any prior data slices to the skb dynptr. This is because a bpf_dynptr_write() may be writing to data in a paged buffer, so it will need to pull the buffer first into the head. The reason it needs to be pulled instead of writing directly to the paged buffers is because they may be cloned (only the head of the skb is by default uncloned). As such, any bpf_dynptr_write() will automatically have its prior data slices invalidated, even if the write is to data in the skb head (the verifier has no way of differentiating whether the write is to the head or paged buffers during program load time). Please note as well that any other helper calls that change the underlying packet buffer (eg bpf_skb_pull_data()) invalidates any data slices of the skb dynptr as well. The stack trace for this is check_helper_call() -> clear_all_pkt_pointers() -> __clear_all_pkt_pointers() -> mark_reg_unknown().

For examples of how skb dynptrs can be used, please see the attached selftests.

---
 include/linux/bpf.h            | 82 +++++++++++++++++++------------
 include/linux/filter.h         | 18 +++++++
 include/uapi/linux/bpf.h       | 43 +++++++++++++++--
 kernel/bpf/helpers.c           | 71 ++++++++++++++++++++++++---
 kernel/bpf/verifier.c          | 88 +++++++++++++++++++++++++++-------
 net/core/filter.c              | 72 ++++++++++++++++++++++++++--
 tools/include/uapi/linux/bpf.h | 43 +++++++++++++++--
 7 files changed, 353 insertions(+), 64 deletions(-)
include/linux/bpf.h +52 -30
index 9c1674973e03..26ad1422a157 100644
@@ -408,11 +408,14 @@ enum bpf_type_flag {
 	/* Size is known at compile time. */
 	MEM_FIXED_SIZE		= BIT(10 + BPF_BASE_TYPE_BITS),
 
+	/* DYNPTR points to sk_buff */
+	DYNPTR_TYPE_SKB		= BIT(11 + BPF_BASE_TYPE_BITS),
+
 	__BPF_TYPE_FLAG_MAX,
 	__BPF_TYPE_LAST_FLAG	= __BPF_TYPE_FLAG_MAX - 1,
 };
 
-#define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF)
+#define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB)
 
 /* Max number of base types. */
 #define BPF_BASE_TYPE_LIMIT	(1UL << BPF_BASE_TYPE_BITS)
@@ -904,6 +907,36 @@ static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(
 	return bpf_func(ctx, insnsi);
 }
 
+/* the implementation of the opaque uapi struct bpf_dynptr */
+struct bpf_dynptr_kern {
+	void *data;
+	/* Size represents the number of usable bytes of dynptr data.
+	 * If for example the offset is at 4 for a local dynptr whose data is
+	 * of type u64, the number of usable bytes is 4.
+	 *
+	 * The upper 8 bits are reserved. It is as follows:
+	 * Bits 0 - 23 = size
+	 * Bits 24 - 30 = dynptr type
+	 * Bit 31 = whether dynptr is read-only
+	 */
+	u32 size;
+	u32 offset;
+} __aligned(8);
+
+enum bpf_dynptr_type {
+	BPF_DYNPTR_TYPE_INVALID,
+	/* Points to memory that is local to the bpf program */
+	BPF_DYNPTR_TYPE_LOCAL,
+	/* Underlying data is a ringbuf record */
+	BPF_DYNPTR_TYPE_RINGBUF,
+	/* Underlying data is a sk_buff */
+	BPF_DYNPTR_TYPE_SKB,
+	/* Underlying data is a xdp_buff */
+	BPF_DYNPTR_TYPE_XDP,
+};
+
+int bpf_dynptr_check_size(u32 size);
+
 #ifdef CONFIG_BPF_JIT
 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
@@ -1983,6 +2016,11 @@ static inline bool has_current_bpf_ctx(void)
 {
 	return !!current->bpf_ctx;
 }
+
+void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
+		     enum bpf_dynptr_type type, u32 offset, u32 size);
+void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
+void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr);
 #else /* !CONFIG_BPF_SYSCALL */
 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
 {
@@ -2196,6 +2234,19 @@ static inline bool has_current_bpf_ctx(void)
 {
 	return false;
 }
+
+static inline void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
+				   enum bpf_dynptr_type type, u32 offset, u32 size)
+{
+}
+
+static inline void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
+{
+}
+
+static inline void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr)
+{
+}
 #endif /* CONFIG_BPF_SYSCALL */
 
 void __bpf_free_used_btfs(struct bpf_prog_aux *aux,
@@ -2557,35 +2608,6 @@ int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
 			u32 **bin_buf, u32 num_args);
 void bpf_bprintf_cleanup(void);
 
-/* the implementation of the opaque uapi struct bpf_dynptr */
-struct bpf_dynptr_kern {
-	void *data;
-	/* Size represents the number of usable bytes of dynptr data.
-	 * If for example the offset is at 4 for a local dynptr whose data is
-	 * of type u64, the number of usable bytes is 4.
-	 *
-	 * The upper 8 bits are reserved. It is as follows:
-	 * Bits 0 - 23 = size
-	 * Bits 24 - 30 = dynptr type
-	 * Bit 31 = whether dynptr is read-only
-	 */
-	u32 size;
-	u32 offset;
-} __aligned(8);
-
-enum bpf_dynptr_type {
-	BPF_DYNPTR_TYPE_INVALID,
-	/* Points to memory that is local to the bpf program */
-	BPF_DYNPTR_TYPE_LOCAL,
-	/* Underlying data is a ringbuf record */
-	BPF_DYNPTR_TYPE_RINGBUF,
-};
-
-void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
-		     enum bpf_dynptr_type type, u32 offset, u32 size);
-void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
-int bpf_dynptr_check_size(u32 size);
-
 #ifdef CONFIG_BPF_LSM
 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
 void bpf_cgroup_atype_put(int cgroup_atype);
include/linux/filter.h +18 -0
index a5f21dc3c432..caa11c996309 100644
@@ -1532,4 +1532,22 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u32 ifind
 	return XDP_REDIRECT;
 }
 
+#ifdef CONFIG_NET
+int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len);
+int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
+			  u32 len, u64 flags);
+#else /* CONFIG_NET */
+static inline int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset,
+				       void *to, u32 len)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset,
+					const void *from, u32 len, u64 flags)
+{
+	return -EOPNOTSUPP;
+}
+#endif /* CONFIG_NET */
+
 #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +39 -4

73 lines not on this page · raw message

kernel/bpf/helpers.c +65 -6

122 lines not on this page · raw message

kernel/bpf/verifier.c +72 -16

156 lines not on this page · raw message

net/core/filter.c +68 -4

126 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +39 -4

73 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH bpf-next v6 1/3] bpf: Add skb dynptrs

Quoted 5 lines · Joanne Koong

For bpf prog types that don't support writes on skb data, the dynptr is read-only (bpf_dynptr_write() will return an error and bpf_dynptr_data() will return NULL; for a read-only data slice, there will be a separate API bpf_dynptr_data_rdonly(), which will be added in the near future).

I just caught up on the v4 discussion about loadtime-vs-runtime error on write. From a user perspective, I am not concerned on which error. Either way, I will quickly find out the packet header is not changed.

For the dynptr init helper bpf_dynptr_from_skb(), the user does not need to know its skb is read-only or not and uses the same helper. The verifier in this case uses its knowledge on the skb context and uses bpf_dynptr_from_skb_rdonly_proto or bpf_dynptr_from_skb_rdwr_proto accordingly.

Now for the slice helper, the user needs to remember its skb is read only (or not) and uses bpf_dynptr_data() vs bpf_dynptr_data_rdonly() accordingly. Yes, if it only needs to read, the user can always stay with bpf_dynptr_data_rdonly (which is not the initially supported one though). However, it is still unnecessary burden and surprise to user. It is likely it will silently turn everything into bpf_dynptr_read() against the user intention. eg:

if (bpf_dynptr_from_skb(skb, 0, &dynptr))

	return 0;
ip6h = bpf_dynptr_data(&dynptr, 0, sizeof(*ip6h));
if (!ip6h) {
	/* Unlikely case, in non-linear section, just bpf_dynptr_read()
	 * Oops...actually bpf_dynptr_data_rdonly() should be used.
	 */
	bpf_dynptr_read(buf, sizeof(*ip6h), &dynptr, 0, 0);
	ip6h = buf;
}

kernel/bpf/helpers.c BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len · line 1597

Quoted 6 lines
kernel/bpf/helpers.c +6 -0
+	case BPF_DYNPTR_TYPE_SKB:
+	{
+		struct sk_buff *skb = ptr->data;
+
+		/* if the data is paged, the caller needs to pull it first */
+		if (ptr->offset + offset + len > skb->len - skb->data_len)

nit. skb_headlen(skb)

The patches can't be applied cleanly also. Please remember to rebase. eg. commit afef88e65554 ("selftests/bpf: Store BPF object files with .bpf.o extension") has landed on Sep 2.

#3 lore raw

Re: [PATCH bpf-next v6 1/3] bpf: Add skb dynptrs

Quoted 26 lines · Martin KaFai Lau
Quoted 5 lines · Joanne Koong

For bpf prog types that don't support writes on skb data, the dynptr is read-only (bpf_dynptr_write() will return an error and bpf_dynptr_data() will return NULL; for a read-only data slice, there will be a separate API bpf_dynptr_data_rdonly(), which will be added in the near future).

I just caught up on the v4 discussion about loadtime-vs-runtime error on write. From a user perspective, I am not concerned on which error. Either way, I will quickly find out the packet header is not changed.

For the dynptr init helper bpf_dynptr_from_skb(), the user does not need to know its skb is read-only or not and uses the same helper. The verifier in this case uses its knowledge on the skb context and uses bpf_dynptr_from_skb_rdonly_proto or bpf_dynptr_from_skb_rdwr_proto accordingly.

Now for the slice helper, the user needs to remember its skb is read only (or not) and uses bpf_dynptr_data() vs bpf_dynptr_data_rdonly() accordingly. Yes, if it only needs to read, the user can always stay with bpf_dynptr_data_rdonly (which is not the initially supported one though). However, it is still unnecessary burden and surprise to user. It is likely it will silently turn everything into bpf_dynptr_read() against the user intention. eg:

if (bpf_dynptr_from_skb(skb, 0, &dynptr))

Quoted 10 lines
        return 0;
ip6h = bpf_dynptr_data(&dynptr, 0, sizeof(*ip6h));
if (!ip6h) {
        /* Unlikely case, in non-linear section, just bpf_dynptr_read()
         * Oops...actually bpf_dynptr_data_rdonly() should be used.
         */
        bpf_dynptr_read(buf, sizeof(*ip6h), &dynptr, 0, 0);
        ip6h = buf;
}

I see your point. I agree that it'd be best if we could prevent this burden on the user, but I think the trade-off would be that if we have bpf_dynptr_data return data slices that are read-only and data slices that are writable (where rd-only vs. writable is tracked by verifier), then in the future we won't be able to support dynptrs that are dynamically read-only (since to reject at load time, the verifier must know statically whether the dynptr is read-only or not). I'm not sure how likely it is that we'd run into a case where we'll need dynamic read-only dynptrs though. What are your thoughts on this?

Quoted 8 lines

kernel/bpf/helpers.c BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len · line 1597

Quoted 7 lines
kernel/bpf/helpers.c +6 -0
+     case BPF_DYNPTR_TYPE_SKB:
+     {
+             struct sk_buff *skb = ptr->data;
+
+             /* if the data is paged, the caller needs to pull it first */
+             if (ptr->offset + offset + len > skb->len - skb->data_len)
Quoted 5 lines

nit. skb_headlen(skb)

The patches can't be applied cleanly also. Please remember to rebase. eg. commit afef88e65554 ("selftests/bpf: Store BPF object files with .bpf.o extension") has landed on Sep 2.

I will use skb_headlen(skb) and rebase for the next iteration :) Thanks for reviewing this!

Quoted 2 lines
#4 lore raw

Re: [PATCH bpf-next v6 1/3] bpf: Add skb dynptrs

Quoted 38 lines · Joanne Koong
Quoted 26 lines · Martin KaFai Lau
Quoted 5 lines · Joanne Koong

For bpf prog types that don't support writes on skb data, the dynptr is read-only (bpf_dynptr_write() will return an error and bpf_dynptr_data() will return NULL; for a read-only data slice, there will be a separate API bpf_dynptr_data_rdonly(), which will be added in the near future).

I just caught up on the v4 discussion about loadtime-vs-runtime error on write. From a user perspective, I am not concerned on which error. Either way, I will quickly find out the packet header is not changed.

For the dynptr init helper bpf_dynptr_from_skb(), the user does not need to know its skb is read-only or not and uses the same helper. The verifier in this case uses its knowledge on the skb context and uses bpf_dynptr_from_skb_rdonly_proto or bpf_dynptr_from_skb_rdwr_proto accordingly.

Now for the slice helper, the user needs to remember its skb is read only (or not) and uses bpf_dynptr_data() vs bpf_dynptr_data_rdonly() accordingly. Yes, if it only needs to read, the user can always stay with bpf_dynptr_data_rdonly (which is not the initially supported one though). However, it is still unnecessary burden and surprise to user. It is likely it will silently turn everything into bpf_dynptr_read() against the user intention. eg:

if (bpf_dynptr_from_skb(skb, 0, &dynptr))

Quoted 10 lines
         return 0;
ip6h = bpf_dynptr_data(&dynptr, 0, sizeof(*ip6h));
if (!ip6h) {
         /* Unlikely case, in non-linear section, just bpf_dynptr_read()
          * Oops...actually bpf_dynptr_data_rdonly() should be used.
          */
         bpf_dynptr_read(buf, sizeof(*ip6h), &dynptr, 0, 0);
         ip6h = buf;
}
Quoted 9 lines

I see your point. I agree that it'd be best if we could prevent this burden on the user, but I think the trade-off would be that if we have bpf_dynptr_data return data slices that are read-only and data slices that are writable (where rd-only vs. writable is tracked by verifier), then in the future we won't be able to support dynptrs that are dynamically read-only (since to reject at load time, the verifier must know statically whether the dynptr is read-only or not). I'm not sure how likely it is that we'd run into a case where we'll need dynamic read-only dynptrs though. What are your thoughts on this?

Out of all dynptr helpers, bpf_dynptr_data() is pretty much the only important function for header parsing because of the runtime offset. This offset is good to be tracked in runtime to avoid smart compiler getting in the way. imo, making this helper less usage surprise is important. If the verifier can help, then static checking is useful here.

It is hard to comment without a real use case on when we want to flip a dynptr to rdonly in a dynamic/runtime way. Thus, comparing with the example like the skb here, my preference is pretty obvious :) Beside, a quick thought is doing this static checking now should now stop the dynamic rdonly flip later. I imagine it will be a helper call like bpf_dynptr_set_rdonly(). The verifier should be able to track this helper call.

#5 lore raw

Re: [PATCH bpf-next v6 1/3] bpf: Add skb dynptrs

Quoted 49 lines · Martin KaFai Lau
Quoted 38 lines · Joanne Koong
Quoted 25 lines · Martin KaFai Lau
Quoted 5 lines · Joanne Koong

For bpf prog types that don't support writes on skb data, the dynptr is read-only (bpf_dynptr_write() will return an error and bpf_dynptr_data() will return NULL; for a read-only data slice, there will be a separate API bpf_dynptr_data_rdonly(), which will be added in the near future).

I just caught up on the v4 discussion about loadtime-vs-runtime error on write.  From a user perspective, I am not concerned on which error. Either way, I will quickly find out the packet header is not changed.

For the dynptr init helper bpf_dynptr_from_skb(), the user does not need to know its skb is read-only or not and uses the same helper.  The verifier in this case uses its knowledge on the skb context and uses bpf_dynptr_from_skb_rdonly_proto or bpf_dynptr_from_skb_rdwr_proto accordingly.

Now for the slice helper, the user needs to remember its skb is read only (or not) and uses bpf_dynptr_data() vs bpf_dynptr_data_rdonly() accordingly.  Yes, if it only needs to read, the user can always stay with bpf_dynptr_data_rdonly (which is not the initially supported one though).  However, it is still unnecessary burden and surprise to user. It is likely it will silently turn everything into bpf_dynptr_read() against the user intention. eg:

Quoted 11 lines

if (bpf_dynptr_from_skb(skb, 0, &dynptr))
         return 0;
ip6h = bpf_dynptr_data(&dynptr, 0, sizeof(*ip6h));
if (!ip6h) {
         /* Unlikely case, in non-linear section, just bpf_dynptr_read()
          * Oops...actually bpf_dynptr_data_rdonly() should be used. */
         bpf_dynptr_read(buf, sizeof(*ip6h), &dynptr, 0, 0); ip6h = buf;
}

Quoted 9 lines

I see your point. I agree that it'd be best if we could prevent this burden on the user, but I think the trade-off would be that if we have bpf_dynptr_data return data slices that are read-only and data slices that are writable (where rd-only vs. writable is tracked by verifier), then in the future we won't be able to support dynptrs that are dynamically read-only (since to reject at load time, the verifier must know statically whether the dynptr is read-only or not). I'm not sure how likely it is that we'd run into a case where we'll need dynamic read-only dynptrs though. What are your thoughts on this?

Quoted 4 lines

Out of all dynptr helpers, bpf_dynptr_data() is pretty much the only important function for header parsing because of the runtime offset.  This offset is good to be tracked in runtime to avoid smart compiler getting in the way.  imo, making this helper less usage surprise is important.  If the verifier can help, then static checking is useful here.

It is hard to comment without a real use case on when we want to flip a dynptr to rdonly in a dynamic/runtime way.  Thus, comparing with the example like the skb here, my preference is pretty obvious :) Beside, a quick thought is doing this static checking now should now stop the

typo: should *not* stop the... :(

Quoted 1 line

dynamic rdonly flip later.  I imagine it will be a helper call like bpf_dynptr_set_rdonly().  The verifier should be able to track this helper call.

#6 lore raw

Re: [PATCH bpf-next v6 1/3] bpf: Add skb dynptrs

Quoted 57 lines · Martin KaFai Lau
Quoted 55 lines · Martin KaFai Lau
Quoted 38 lines · Joanne Koong
Quoted 26 lines · Martin KaFai Lau
Quoted 5 lines · Joanne Koong

For bpf prog types that don't support writes on skb data, the dynptr is read-only (bpf_dynptr_write() will return an error and bpf_dynptr_data() will return NULL; for a read-only data slice, there will be a separate API bpf_dynptr_data_rdonly(), which will be added in the near future).

I just caught up on the v4 discussion about loadtime-vs-runtime error on write. From a user perspective, I am not concerned on which error. Either way, I will quickly find out the packet header is not changed.

For the dynptr init helper bpf_dynptr_from_skb(), the user does not need to know its skb is read-only or not and uses the same helper. The verifier in this case uses its knowledge on the skb context and uses bpf_dynptr_from_skb_rdonly_proto or bpf_dynptr_from_skb_rdwr_proto accordingly.

Now for the slice helper, the user needs to remember its skb is read only (or not) and uses bpf_dynptr_data() vs bpf_dynptr_data_rdonly() accordingly. Yes, if it only needs to read, the user can always stay with bpf_dynptr_data_rdonly (which is not the initially supported one though). However, it is still unnecessary burden and surprise to user. It is likely it will silently turn everything into bpf_dynptr_read() against the user intention. eg:

if (bpf_dynptr_from_skb(skb, 0, &dynptr))

Quoted 10 lines
         return 0;
ip6h = bpf_dynptr_data(&dynptr, 0, sizeof(*ip6h));
if (!ip6h) {
         /* Unlikely case, in non-linear section, just bpf_dynptr_read()
          * Oops...actually bpf_dynptr_data_rdonly() should be used.
          */
         bpf_dynptr_read(buf, sizeof(*ip6h), &dynptr, 0, 0);
         ip6h = buf;
}
Quoted 9 lines

I see your point. I agree that it'd be best if we could prevent this burden on the user, but I think the trade-off would be that if we have bpf_dynptr_data return data slices that are read-only and data slices that are writable (where rd-only vs. writable is tracked by verifier), then in the future we won't be able to support dynptrs that are dynamically read-only (since to reject at load time, the verifier must know statically whether the dynptr is read-only or not). I'm not sure how likely it is that we'd run into a case where we'll need dynamic read-only dynptrs though. What are your thoughts on this?

Out of all dynptr helpers, bpf_dynptr_data() is pretty much the only important function for header parsing because of the runtime offset. This offset is good to be tracked in runtime to avoid smart compiler getting in the way. imo, making this helper less usage surprise is important. If the verifier can help, then static checking is useful here.

Quoted 10 lines
Quoted 4 lines

It is hard to comment without a real use case on when we want to flip a dynptr to rdonly in a dynamic/runtime way. Thus, comparing with the example like the skb here, my preference is pretty obvious :)
Beside, a quick thought is doing this static checking now should now stop the

typo: should *not* stop the... :(

Quoted 2 lines

dynamic rdonly flip later. I imagine it will be a helper call like bpf_dynptr_set_rdonly(). The verifier should be able to track this helper call.'

Great! I'll change this in v7 to have bpf_dynptr_data() be able to return both read-writable and read-only data slices, where the rd-only property is enforced by the verifier.

#7 lore raw

PATCH v6 2/3 bpf: Add xdp dynptrs

In reply to Joanne Koong

Add xdp dynptrs, which are dynptrs whose underlying pointer points to a xdp_buff. The dynptr acts on xdp data. xdp dynptrs have two main benefits. One is that they allow operations on sizes that are not statically known at compile-time (eg variable-sized accesses). Another is that parsing the packet data through dynptrs (instead of through direct access of xdp->data and xdp->data_end) can be more ergonomic and less brittle (eg does not need manual if checking for being within bounds of data_end).

For reads and writes on the dynptr, this includes reading/writing from/to and across fragments. For data slices, direct access to data in fragments is also permitted, but access across fragments is not.

Any helper calls that change the underlying packet buffer (eg bpf_xdp_adjust_head) invalidates any data slices of the associated dynptr. The stack trace for this is check_helper_call() -> clear_all_pkt_pointers() -> __clear_all_pkt_pointers() -> mark_reg_unknown().

For examples of how xdp dynptrs can be used, please see the attached selftests.

---
 include/linux/bpf.h            |  6 ++++-
 include/linux/filter.h         | 20 +++++++++++++++
 include/uapi/linux/bpf.h       | 25 +++++++++++++++---
 kernel/bpf/helpers.c           | 12 +++++++++
 kernel/bpf/verifier.c          | 18 +++++++++----
 net/core/filter.c              | 46 +++++++++++++++++++++++++++++-----
 tools/include/uapi/linux/bpf.h | 25 +++++++++++++++---
 7 files changed, 134 insertions(+), 18 deletions(-)
include/linux/bpf.h +5 -1
index 26ad1422a157..f07d127f559f 100644
@@ -411,11 +411,15 @@ enum bpf_type_flag {
 	/* DYNPTR points to sk_buff */
 	DYNPTR_TYPE_SKB		= BIT(11 + BPF_BASE_TYPE_BITS),
 
+	/* DYNPTR points to xdp_buff */
+	DYNPTR_TYPE_XDP		= BIT(12 + BPF_BASE_TYPE_BITS),
+
 	__BPF_TYPE_FLAG_MAX,
 	__BPF_TYPE_LAST_FLAG	= __BPF_TYPE_FLAG_MAX - 1,
 };
 
-#define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB)
+#define DYNPTR_TYPE_FLAG_MASK	(DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB \
+				 | DYNPTR_TYPE_XDP)
 
 /* Max number of base types. */
 #define BPF_BASE_TYPE_LIMIT	(1UL << BPF_BASE_TYPE_BITS)
include/linux/filter.h +20 -0
index caa11c996309..47282ed6a5ae 100644
@@ -1536,6 +1536,9 @@ static __always_inline int __bpf_xdp_redirect_map(struct bpf_map *map, u32 ifind
 int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len);
 int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
 			  u32 len, u64 flags);
+int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
+int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len);
+void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len);
 #else /* CONFIG_NET */
 static inline int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset,
 				       void *to, u32 len)
@@ -1548,6 +1551,23 @@ static inline int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset,
 {
 	return -EOPNOTSUPP;
 }
+
+static inline int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset,
+				       void *buf, u32 len)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset,
+					void *buf, u32 len)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
+{
+	return NULL;
+}
 #endif /* CONFIG_NET */
 
 #endif /* __LINUX_FILTER_H__ */
include/uapi/linux/bpf.h +22 -3
index b4e5f7d81a20..992f7565a41e 100644
@@ -5315,13 +5315,18 @@ union bpf_attr {
  *		      and try again.
  *
  *		    * The data slice is automatically invalidated anytime
- *		      **bpf_dynptr_write**\ () or a helper call that changes
- *		      the underlying packet buffer (eg **bpf_skb_pull_data**\ ())
+ *		      **bpf_dynptr_write**\ () is called.
+ *
+ *		For skb-type and xdp-type dynptrs:
+ *		    * The data slice is automatically invalidated anytime a
+ *		      helper call that changes the underlying packet buffer
+ *		      (eg **bpf_skb_pull_data**\ (), **bpf_xdp_adjust_head**\ ())
  *		      is called.
  *	Return
  *		Pointer to the underlying dynptr data, NULL if the dynptr is
  *		read-only, if the dynptr is invalid, or if the offset and length
- *		is out of bounds or in a paged buffer for skb-type dynptrs.
+ *		is out of bounds or in a paged buffer for skb-type dynptrs or
+ *		across fragments for xdp-type dynptrs.
  *
  * s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
  *	Description
@@ -5420,6 +5425,19 @@ union bpf_attr {
  *		*flags* is currently unused, it must be 0 for now.
  *	Return
  *		0 on success or -EINVAL if flags is not 0.
+ *
+ * long bpf_dynptr_from_xdp(struct xdp_buff *xdp_md, u64 flags, struct bpf_dynptr *ptr)
+ *	Description
+ *		Get a dynptr to the data in *xdp_md*. *xdp_md* must be the BPF program
+ *		context.
+ *
+ *		Calls that change the *xdp_md*'s underlying packet buffer
+ *		(eg **bpf_xdp_adjust_head**\ ()) do not invalidate the dynptr, but
+ *		they do invalidate any data slices associated with the dynptr.
+ *
+ *		*flags* is currently unused, it must be 0 for now.
+ *	Return
+ *		0 on success, -EINVAL if flags is not 0.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5632,6 +5650,7 @@ union bpf_attr {
 	FN(tcp_raw_check_syncookie_ipv6),	\
 	FN(ktime_get_tai_ns),		\
 	FN(dynptr_from_skb),		\
+	FN(dynptr_from_xdp),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/helpers.c +12 -0
index 98fcb4704a9e..befafae34a63 100644
@@ -1507,6 +1507,8 @@ BPF_CALL_5(bpf_dynptr_read, void *, dst, u32, len, struct bpf_dynptr_kern *, src
 		return 0;
 	case BPF_DYNPTR_TYPE_SKB:
 		return __bpf_skb_load_bytes(src->data, src->offset + offset, dst, len);
+	case BPF_DYNPTR_TYPE_XDP:
+		return __bpf_xdp_load_bytes(src->data, src->offset + offset, dst, len);
 	default:
 		WARN_ONCE(true, "bpf_dynptr_read: unknown dynptr type %d\n", type);
 		return -EFAULT;
@@ -1549,6 +1551,10 @@ BPF_CALL_5(bpf_dynptr_write, struct bpf_dynptr_kern *, dst, u32, offset, void *,
 	case BPF_DYNPTR_TYPE_SKB:
 		return __bpf_skb_store_bytes(dst->data, dst->offset + offset, src, len,
 					     flags);
+	case BPF_DYNPTR_TYPE_XDP:
+		if (flags)
+			return -EINVAL;
+		return __bpf_xdp_store_bytes(dst->data, dst->offset + offset, src, len);
 	default:
 		WARN_ONCE(true, "bpf_dynptr_write: unknown dynptr type %d\n", type);
 		return -EFAULT;
@@ -1600,6 +1606,12 @@ BPF_CALL_3(bpf_dynptr_data, struct bpf_dynptr_kern *, ptr, u32, offset, u32, len
 		data = skb->data;
 		break;
 	}
+	case BPF_DYNPTR_TYPE_XDP:
+		/* if the requested data in across fragments, then it cannot
+		 * be accessed directly - bpf_xdp_pointer will return NULL
+		 */
+		return (unsigned long)bpf_xdp_pointer(ptr->data,
+						      ptr->offset + offset, len);
 	default:
 		WARN_ONCE(true, "bpf_dynptr_data: unknown dynptr type %d\n", type);
 		return 0;
kernel/bpf/verifier.c +13 -5
index 7f67fac66735..f5ff0f26b7cc 100644
@@ -686,6 +686,8 @@ static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type)
 		return BPF_DYNPTR_TYPE_RINGBUF;
 	case DYNPTR_TYPE_SKB:
 		return BPF_DYNPTR_TYPE_SKB;
+	case DYNPTR_TYPE_XDP:
+		return BPF_DYNPTR_TYPE_XDP;
 	default:
 		return BPF_DYNPTR_TYPE_INVALID;
 	}
@@ -1411,7 +1413,7 @@ static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
 static bool reg_is_dynptr_slice_pkt(const struct bpf_reg_state *reg)
 {
 	return base_type(reg->type) == PTR_TO_MEM &&
-		reg->type & DYNPTR_TYPE_SKB;
+		(reg->type & DYNPTR_TYPE_SKB || reg->type & DYNPTR_TYPE_XDP);
 }
 
 /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
@@ -6088,6 +6090,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
 			case DYNPTR_TYPE_SKB:
 				err_extra = "skb ";
 				break;
+			case DYNPTR_TYPE_XDP:
+				err_extra = "xdp ";
+				break;
 			default:
 				break;
 			}
@@ -6519,7 +6524,7 @@ static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
 /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
  * are now invalid, so turn them into unknown SCALAR_VALUE.
  *
- * This applies to dynptr slices belonging to skb dynptrs,
+ * This applies to dynptr slices belonging to skb or xdp dynptrs,
  * since these slices point to packet data.
  */
 static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
@@ -7470,9 +7475,12 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		mark_reg_known_zero(env, regs, BPF_REG_0);
 		regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
 		regs[BPF_REG_0].mem_size = meta.mem_size;
-		if (func_id == BPF_FUNC_dynptr_data &&
-		    dynptr_type == BPF_DYNPTR_TYPE_SKB)
-			regs[BPF_REG_0].type |= DYNPTR_TYPE_SKB;
+		if (func_id == BPF_FUNC_dynptr_data) {
+			if (dynptr_type == BPF_DYNPTR_TYPE_SKB)
+				regs[BPF_REG_0].type |= DYNPTR_TYPE_SKB;
+			else if (dynptr_type == BPF_DYNPTR_TYPE_XDP)
+				regs[BPF_REG_0].type |= DYNPTR_TYPE_XDP;
+		}
 		break;
 	case RET_PTR_TO_MEM_OR_BTF_ID:
 	{
net/core/filter.c +40 -6

88 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +22 -3
index 52e891b00a35..b22ffbb6f382 100644
@@ -5315,13 +5315,18 @@ union bpf_attr {
  *		      and try again.
  *
  *		    * The data slice is automatically invalidated anytime
- *		      **bpf_dynptr_write**\ () or a helper call that changes
- *		      the underlying packet buffer (eg **bpf_skb_pull_data**\ ())
+ *		      **bpf_dynptr_write**\ () is called.
+ *
+ *		For skb-type and xdp-type dynptrs:
+ *		    * The data slice is automatically invalidated anytime a
+ *		      helper call that changes the underlying packet buffer
+ *		      (eg **bpf_skb_pull_data**\ (), **bpf_xdp_adjust_head**\ ())
  *		      is called.
  *	Return
  *		Pointer to the underlying dynptr data, NULL if the dynptr is
  *		read-only, if the dynptr is invalid, or if the offset and length
- *		is out of bounds or in a paged buffer for skb-type dynptrs.
+ *		is out of bounds or in a paged buffer for skb-type dynptrs or
+ *		across fragments for xdp-type dynptrs.
  *
  * s64 bpf_tcp_raw_gen_syncookie_ipv4(struct iphdr *iph, struct tcphdr *th, u32 th_len)
  *	Description
@@ -5420,6 +5425,19 @@ union bpf_attr {
  *		*flags* is currently unused, it must be 0 for now.
  *	Return
  *		0 on success or -EINVAL if flags is not 0.
+ *
+ * long bpf_dynptr_from_xdp(struct xdp_buff *xdp_md, u64 flags, struct bpf_dynptr *ptr)
+ *	Description
+ *		Get a dynptr to the data in *xdp_md*. *xdp_md* must be the BPF program
+ *		context.
+ *
+ *		Calls that change the *xdp_md*'s underlying packet buffer
+ *		(eg **bpf_xdp_adjust_head**\ ()) do not invalidate the dynptr, but
+ *		they do invalidate any data slices associated with the dynptr.
+ *
+ *		*flags* is currently unused, it must be 0 for now.
+ *	Return
+ *		0 on success, -EINVAL if flags is not 0.
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5632,6 +5650,7 @@ union bpf_attr {
 	FN(tcp_raw_check_syncookie_ipv6),	\
 	FN(ktime_get_tai_ns),		\
 	FN(dynptr_from_skb),		\
+	FN(dynptr_from_xdp),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.30.2
#8 lore raw

PATCH v6 3/3 selftests/bpf: tests for using dynptrs to parse skb and xdp buffers

In reply to Joanne Koong

Test skb and xdp dynptr functionality in the following ways:

1) progs/test_cls_redirect_dynptr.c
* Rewrite "progs/test_cls_redirect.c" test to use dynptrs to parse skb data

* This is a great example of how dynptrs can be used to simplify a lot of the parsing logic for non-statically known values, and speed up execution times.

When measuring the user + system time between the original version vs. using dynptrs, and averaging the time for 10 runs (using "time ./test_progs -t cls_redirect"), there was a 2x speed-up:
original version: 0.053 sec
with dynptrs: 0.025 sec

2) progs/test_xdp_dynptr.c
* Rewrite "progs/test_xdp.c" test to use dynptrs to parse xdp data

There were no noticeable diferences in user + system time between the original version vs. using dynptrs. Averaging the time for 10 runs (run using "time ./test_progs -t xdp_attach"):
original version: 0.011 sec
with dynptrs: 0.011 sec

3) progs/test_l4lb_noinline_dynptr.c
* Rewrite "progs/test_l4lb_noinline.c" test to use dynptrs to parse skb data

There were no noticeable diferences in user + system time between the original version vs. using dynptrs. Averaging the time for 10 runs (run using "time ./test_progs -t l4lb_all"):
original version: 0.0502 sec
with dynptrs: 0.055 sec

For number of processed verifier instructions:
original version: 6284 insns
with dynptrs: 2538 insns

4) progs/test_parse_tcp_hdr_opt_dynptr.c
* Add sample code for parsing tcp hdr opt lookup using dynptrs. This logic is lifted from a real-world use case of packet parsing in katran [0], a layer 4 load balancer. The original version "progs/test_parse_tcp_hdr_opt.c" (not using dynptrs) is included here as well, for comparison.

There were no noticeable diferences in user + system time between the original version vs. using dynptrs. Averaging the time for 10 runs (run using "time ./test_progs -t parse_tcp_hdr_opt"):
original version: 0.007 sec
with dynptrs: 0.008 sec

5) progs/dynptr_success.c
* Add test case "test_skb_readonly" for testing attempts at writes / data slices on a prog type with read-only skb ctx.

6) progs/dynptr_fail.c
* Add test cases "skb_invalid_data_slice{1,2}" and "xdp_invalid_data_slice" for testing that helpers that modify the underlying packet buffer automatically invalidate the associated data slice.
* Add test cases "skb_invalid_ctx" and "xdp_invalid_ctx" for testing that prog types that do not support bpf_dynptr_from_skb/xdp don't have access to the API.

[0] https://github.com/facebookincubator/katran/blob/main/katran/lib/bpf/pckt_parsing.h

---
 .../selftests/bpf/prog_tests/cls_redirect.c   |  25 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |  73 +-
 .../selftests/bpf/prog_tests/l4lb_all.c       |   2 +
 .../bpf/prog_tests/parse_tcp_hdr_opt.c        |  93 ++
 .../selftests/bpf/prog_tests/xdp_attach.c     |  11 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c |  92 ++
 .../selftests/bpf/progs/dynptr_success.c      |  32 +
 .../bpf/progs/test_cls_redirect_dynptr.c      | 968 ++++++++++++++++++
 .../bpf/progs/test_l4lb_noinline_dynptr.c     | 469 +++++++++
 .../bpf/progs/test_parse_tcp_hdr_opt.c        | 119 +++
 .../bpf/progs/test_parse_tcp_hdr_opt_dynptr.c | 110 ++
 .../selftests/bpf/progs/test_xdp_dynptr.c     | 235 +++++
 .../selftests/bpf/test_tcp_hdr_options.h      |   1 +
 13 files changed, 2214 insertions(+), 16 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_dynptr.c
tools/testing/selftests/bpf/prog_tests/cls_redirect.c +25 -0
index 224f016b0a53..2a55f717fc07 100644
@@ -13,6 +13,7 @@
 
 #include "progs/test_cls_redirect.h"
 #include "test_cls_redirect.skel.h"
+#include "test_cls_redirect_dynptr.skel.h"
 #include "test_cls_redirect_subprogs.skel.h"
 
 #define ENCAP_IP INADDR_LOOPBACK
@@ -446,6 +447,28 @@ static void test_cls_redirect_common(struct bpf_program *prog)
 	close_fds((int *)conns, sizeof(conns) / sizeof(conns[0][0]));
 }
 
+static void test_cls_redirect_dynptr(void)
+{
+	struct test_cls_redirect_dynptr *skel;
+	int err;
+
+	skel = test_cls_redirect_dynptr__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	skel->rodata->ENCAPSULATION_IP = htonl(ENCAP_IP);
+	skel->rodata->ENCAPSULATION_PORT = htons(ENCAP_PORT);
+
+	err = test_cls_redirect_dynptr__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto cleanup;
+
+	test_cls_redirect_common(skel->progs.cls_redirect);
+
+cleanup:
+	test_cls_redirect_dynptr__destroy(skel);
+}
+
 static void test_cls_redirect_inlined(void)
 {
 	struct test_cls_redirect *skel;
@@ -496,4 +519,6 @@ void test_cls_redirect(void)
 		test_cls_redirect_inlined();
 	if (test__start_subtest("cls_redirect_subprogs"))
 		test_cls_redirect_subprogs();
+	if (test__start_subtest("cls_redirect_dynptr"))
+		test_cls_redirect_dynptr();
 }
tools/testing/selftests/bpf/prog_tests/dynptr.c +59 -14

115 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/l4lb_all.c +2 -0
index 55f733ff4109..94079c89f2e9 100644
@@ -93,4 +93,6 @@ void test_l4lb_all(void)
 		test_l4lb("test_l4lb.o");
 	if (test__start_subtest("l4lb_noinline"))
 		test_l4lb("test_l4lb_noinline.o");
+	if (test__start_subtest("l4lb_noinline_dynptr"))
+		test_l4lb("test_l4lb_noinline_dynptr.o");
 }
tools/testing/selftests/bpf/prog_tests/parse_tcp_hdr_opt.c new file +93 -0

93 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/xdp_attach.c +9 -2
index 62aa3edda5e6..5fb5f03022d3 100644
@@ -4,11 +4,10 @@
 #define IFINDEX_LO 1
 #define XDP_FLAGS_REPLACE		(1U << 4)
 
-void serial_test_xdp_attach(void)
+static void serial_test_xdp_attach(const char *file)
 {
 	__u32 duration = 0, id1, id2, id0 = 0, len;
 	struct bpf_object *obj1, *obj2, *obj3;
-	const char *file = "./test_xdp.o";
 	struct bpf_prog_info info = {};
 	int err, fd1, fd2, fd3;
 	LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
@@ -85,3 +84,11 @@ void serial_test_xdp_attach(void)
 out_1:
 	bpf_object__close(obj1);
 }
+
+void test_xdp_attach(void)
+{
+	if (test__start_subtest("xdp_attach"))
+		serial_test_xdp_attach("./test_xdp.o");
+	if (test__start_subtest("xdp_attach_dynptr"))
+		serial_test_xdp_attach("./test_xdp_dynptr.o");
+}
tools/testing/selftests/bpf/progs/dynptr_fail.c +92 -0

101 lines not on this page · raw message

tools/testing/selftests/bpf/progs/dynptr_success.c +32 -0
index a3a6103c8569..8ed3ef201284 100644
@@ -162,3 +162,35 @@ int test_ringbuf(void *ctx)
 	bpf_ringbuf_discard_dynptr(&ptr, 0);
 	return 0;
 }
+
+SEC("cgroup_skb/egress")
+int test_skb_readonly(struct __sk_buff *skb)
+{
+	__u8 write_data[2] = {1, 2};
+	struct bpf_dynptr ptr;
+	__u64 *data;
+	int ret;
+
+	if (bpf_dynptr_from_skb(skb, 0, &ptr)) {
+		err = 1;
+		return 0;
+	}
+
+	data = bpf_dynptr_data(&ptr, 0, sizeof(*data));
+	/* since cgroup skbs are read only, bpf_dynptr_data
+	 * should always return NULL
+	 */
+	if (data) {
+		err = 2;
+		return 0;
+	}
+
+	/* since cgroup skbs are read only, writes should fail */
+	ret = bpf_dynptr_write(&ptr, 0, write_data, sizeof(write_data), 0);
+	if (ret != -EINVAL) {
+		err = 3;
+		return 0;
+	}
+
+	return 0;
+}
tools/testing/selftests/bpf/progs/test_cls_redirect_dynptr.c new file +968 -0

968 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_l4lb_noinline_dynptr.c new file +469 -0

469 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt.c new file +119 -0

119 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_parse_tcp_hdr_opt_dynptr.c new file +110 -0

110 lines not on this page · raw message

tools/testing/selftests/bpf/progs/test_xdp_dynptr.c new file +235 -0

235 lines not on this page · raw message

tools/testing/selftests/bpf/test_tcp_hdr_options.h +1 -0
index 6118e3ab61fc..56c9f8a3ad3d 100644
@@ -50,6 +50,7 @@ struct linum_err {
 
 #define TCPOPT_EOL		0
 #define TCPOPT_NOP		1
+#define TCPOPT_MSS		2
 #define TCPOPT_WINDOW		3
 #define TCPOPT_EXP		254
 
Signature · 1 line
2.30.2