Skip to contentSkip to conversation

bpf: Support setting variable-length tunnel options

PATCH Superseded Shmulik Ladkani 3 participants 10 messages Aug 22, 2022 – Sep 2, 2022

Subsystems (5)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover bpf: Support setting variable-length tunnel options

Introduce 'skb_set_var_tunnel_opt' to allow setting tunnel options of dynamic length.

v2:
- Place test_tunnel's local route in a custom table, to ensure the IP isn't considered assigned to a device.
v3:
- Avoid 'inline' for the __bpf_skb_set_tunopt helper function

Shmulik Ladkani (3):
bpf: Support setting variable-length tunnel options selftests/bpf: Simplify test_tunnel setup for allowing non-local
tunnel traffic
selftests/bpf: Add geneve with bpf_skb_set_var_tunnel_opt test-case to
test_progs

 include/uapi/linux/bpf.h                      |  12 ++
 net/core/filter.c                             |  34 +++-
 tools/include/uapi/linux/bpf.h                |  12 ++
 .../selftests/bpf/prog_tests/test_tunnel.c    | 131 +++++++++++--
 .../selftests/bpf/progs/test_tunnel_kern.c    | 182 ++++++++++++------
 5 files changed, 290 insertions(+), 81 deletions(-)
Signature · 1 line
2.37.2
#1 lore raw

PATCH v3 1/3 bpf: Support setting variable-length tunnel options

Existing 'bpf_skb_set_tunnel_opt' allows setting tunnel options given an option buffer (ARG_PTR_TO_MEM|MEM_RDONLY) and the compile-time fixed buffer size (ARG_CONST_SIZE).

However, in certain cases we wish to set tunnel options of dynamic length.

For example, we have an ebpf program that gets geneve options on incoming packets, stores them into a map (using a key representing the incoming flow), and later needs to assign *same* options to reply packets (belonging to same flow).

This is currently imposssibly without knowing sender's exact geneve options length, which unfortunately is dymamic.

Introduce 'skb_set_var_tunnel_opt'. This is a variant of 'bpf_skb_set_tunnel_opt' which gets an *additional* parameter 'len', which is the byte length from 'opt' buffer to copy into ip_tunnnel_info.

The 'size' parameter is kept ARG_CONST_SIZE. This way, verifier can still safe-guard buffer access. 'len' must never exceed 'size', o/w EINVAL is returned.

---
v3: Avoid 'inline' for the __bpf_skb_set_tunopt helper function

---
 include/uapi/linux/bpf.h       | 12 ++++++++++++
 net/core/filter.c              | 34 +++++++++++++++++++++++++++++++---
 tools/include/uapi/linux/bpf.h | 12 ++++++++++++
 3 files changed, 55 insertions(+), 3 deletions(-)
include/uapi/linux/bpf.h +12 -0
index 934a2a8beb87..1b965dfd0c80 100644
@@ -5355,6 +5355,17 @@ union bpf_attr {
  *	Return
  *		Current *ktime*.
  *
+ * long bpf_skb_set_var_tunnel_opt(struct sk_buff *skb, void *opt, u32 size, u32 len)
+ *	Description
+ *		Set tunnel options metadata for the packet associated to *skb*
+ *		to the variable length *len* bytes of option data contained in
+ *		the raw buffer *opt* sized *size*.
+ *
+ *		See also the description of the **bpf_skb_get_tunnel_opt**\ ()
+ *		helper for additional information.
+ *	Return
+ *		0 on success, or a negative error in case of failure.
+ *
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5566,6 +5577,7 @@ union bpf_attr {
 	FN(tcp_raw_check_syncookie_ipv4),	\
 	FN(tcp_raw_check_syncookie_ipv6),	\
 	FN(ktime_get_tai_ns),		\
+	FN(skb_set_var_tunnel_opt),	\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
net/core/filter.c +31 -3
index 1acfaffeaf32..02161a3344fd 100644
@@ -4669,8 +4669,8 @@ static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
 	.arg4_type	= ARG_ANYTHING,
 };
 
-BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
-	   const u8 *, from, u32, size)
+static u64 __bpf_skb_set_tunopt(struct sk_buff *skb,
+				const u8 *from, u32 size, u32 len)
 {
 	struct ip_tunnel_info *info = skb_tunnel_info(skb);
 	const struct metadata_dst *md = this_cpu_ptr(md_dst);
@@ -4679,12 +4679,26 @@ BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
 		return -EINVAL;
 	if (unlikely(size > IP_TUNNEL_OPTS_MAX))
 		return -ENOMEM;
+	if (unlikely(len > size))
+		return -EINVAL;
 
-	ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
+	ip_tunnel_info_opts_set(info, from, len, TUNNEL_OPTIONS_PRESENT);
 
 	return 0;
 }
 
+BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
+	   const u8 *, from, u32, size)
+{
+	return __bpf_skb_set_tunopt(skb, from, size, size);
+}
+
+BPF_CALL_4(bpf_skb_set_var_tunnel_opt, struct sk_buff *, skb,
+	   const u8 *, from, u32, size, u32, len)
+{
+	return __bpf_skb_set_tunopt(skb, from, size, len);
+}
+
 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
 	.func		= bpf_skb_set_tunnel_opt,
 	.gpl_only	= false,
@@ -4694,6 +4708,16 @@ static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
 	.arg3_type	= ARG_CONST_SIZE,
 };
 
+static const struct bpf_func_proto bpf_skb_set_var_tunnel_opt_proto = {
+	.func		= bpf_skb_set_var_tunnel_opt,
+	.gpl_only	= false,
+	.ret_type	= RET_INTEGER,
+	.arg1_type	= ARG_PTR_TO_CTX,
+	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
+	.arg3_type	= ARG_CONST_SIZE,
+	.arg4_type	= ARG_ANYTHING,
+};
+
 static const struct bpf_func_proto *
 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
 {
@@ -4714,6 +4738,8 @@ bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
 		return &bpf_skb_set_tunnel_key_proto;
 	case BPF_FUNC_skb_set_tunnel_opt:
 		return &bpf_skb_set_tunnel_opt_proto;
+	case BPF_FUNC_skb_set_var_tunnel_opt:
+		return &bpf_skb_set_var_tunnel_opt_proto;
 	default:
 		return NULL;
 	}
@@ -7826,6 +7852,7 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 	case BPF_FUNC_skb_get_tunnel_opt:
 		return &bpf_skb_get_tunnel_opt_proto;
 	case BPF_FUNC_skb_set_tunnel_opt:
+	case BPF_FUNC_skb_set_var_tunnel_opt:
 		return bpf_get_skb_set_tunnel_proto(func_id);
 	case BPF_FUNC_redirect:
 		return &bpf_redirect_proto;
@@ -8169,6 +8196,7 @@ lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 	case BPF_FUNC_skb_get_tunnel_opt:
 		return &bpf_skb_get_tunnel_opt_proto;
 	case BPF_FUNC_skb_set_tunnel_opt:
+	case BPF_FUNC_skb_set_var_tunnel_opt:
 		return bpf_get_skb_set_tunnel_proto(func_id);
 	case BPF_FUNC_redirect:
 		return &bpf_redirect_proto;
tools/include/uapi/linux/bpf.h +12 -0
index 1d6085e15fc8..1a1083db5b7a 100644
@@ -5355,6 +5355,17 @@ union bpf_attr {
  *	Return
  *		Current *ktime*.
  *
+ * long bpf_skb_set_var_tunnel_opt(struct sk_buff *skb, void *opt, u32 size, u32 len)
+ *	Description
+ *		Set tunnel options metadata for the packet associated to *skb*
+ *		to the variable length *len* bytes of option data contained in
+ *		the raw buffer *opt* sized *size*.
+ *
+ *		See also the description of the **bpf_skb_get_tunnel_opt**\ ()
+ *		helper for additional information.
+ *	Return
+ *		0 on success, or a negative error in case of failure.
+ *
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -5566,6 +5577,7 @@ union bpf_attr {
 	FN(tcp_raw_check_syncookie_ipv4),	\
 	FN(tcp_raw_check_syncookie_ipv6),	\
 	FN(ktime_get_tai_ns),		\
+	FN(skb_set_var_tunnel_opt),	\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.37.2
#2 lore raw
Quoted 40 lines · Shmulik Ladkani

Existing 'bpf_skb_set_tunnel_opt' allows setting tunnel options given an option buffer (ARG_PTR_TO_MEM|MEM_RDONLY) and the compile-time fixed buffer size (ARG_CONST_SIZE).

However, in certain cases we wish to set tunnel options of dynamic length.

For example, we have an ebpf program that gets geneve options on incoming packets, stores them into a map (using a key representing the incoming flow), and later needs to assign *same* options to reply packets (belonging to same flow).

This is currently imposssibly without knowing sender's exact geneve options length, which unfortunately is dymamic.

Introduce 'skb_set_var_tunnel_opt'. This is a variant of 'bpf_skb_set_tunnel_opt' which gets an *additional* parameter 'len', which is the byte length from 'opt' buffer to copy into ip_tunnnel_info.

The 'size' parameter is kept ARG_CONST_SIZE. This way, verifier can still safe-guard buffer access. 'len' must never exceed 'size', o/w EINVAL is returned.

---
v3: Avoid 'inline' for the __bpf_skb_set_tunopt helper function

---
 include/uapi/linux/bpf.h       | 12 ++++++++++++
 net/core/filter.c              | 34 +++++++++++++++++++++++++++++++---
 tools/include/uapi/linux/bpf.h | 12 ++++++++++++
 3 files changed, 55 insertions(+), 3 deletions(-)
include/uapi/linux/bpf.h +0 -0
index 934a2a8beb87..1b965dfd0c80 100644
@@ -5355,6 +5355,17 @@ union bpf_attr {
  *	Return
  *		Current *ktime*.
  *

include/uapi/linux/bpf.h union bpf_attr { · line 5367

Quoted 10 lines
include/uapi/linux/bpf.h +10 -0
+ * long bpf_skb_set_var_tunnel_opt(struct sk_buff *skb, void *opt, u32 size, u32 len)
+ *	Description
+ *		Set tunnel options metadata for the packet associated to *skb*
+ *		to the variable length *len* bytes of option data contained in
+ *		the raw buffer *opt* sized *size*.
+ *
+ *		See also the description of the **bpf_skb_get_tunnel_opt**\ ()
+ *		helper for additional information.
+ *	Return
+ *		0 on success, or a negative error in case of failure.

This API feels akward to me. Could you collapse this by using a dynamic pointer, recently added? And drop the ptr_to_mem+const_size part at least? That seems redundant with latest kernels.

And then is there a case where size != len? Probably I guess? Anyways having a signature like tunnel_otpion(skb, opt, len) looks a lot like memcpy to me and feels familiar.

[...]

Quoted 11 lines
include/uapi/linux/bpf.h +10 -0
+static const struct bpf_func_proto bpf_skb_set_var_tunnel_opt_proto = {
+	.func		= bpf_skb_set_var_tunnel_opt,
+	.gpl_only	= false,
+	.ret_type	= RET_INTEGER,
+	.arg1_type	= ARG_PTR_TO_CTX,
+	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
+	.arg3_type	= ARG_CONST_SIZE,
+	.arg4_type	= ARG_ANYTHING,
+};
+
#3 lore raw
Quoted 3 lines · On Tue, 23 Aug 2022 00:59:01 -0700 John Fastabend <[email protected]>…

This API feels akward to me. Could you collapse this by using a dynamic pointer, recently added? And drop the ptr_to_mem+const_size part at least? That seems redundant with latest kernels.

Hoo, nice. Wasn't aware of this new bpf_dynptr_kern, thanks! Will resubmit with a signature that gets 'opts' as ARG_PTR_TO_DYNPTR.

Best
Shmulik

#4 lore raw
In reply to John Fastabend
Quoted 11 lines · On Tue, 23 Aug 2022 00:59:01 -0700 John Fastabend <[email protected]>…
Quoted 10 lines

+ * long bpf_skb_set_var_tunnel_opt(struct sk_buff *skb, void *opt, u32 size, u32 len)
+ * Description
+ * Set tunnel options metadata for the packet associated to *skb*
+ * to the variable length *len* bytes of option data contained in
+ * the raw buffer *opt* sized *size*.
+ *
+ * See also the description of the **bpf_skb_get_tunnel_opt**\ ()
+ * helper for additional information.
+ * Return
+ * 0 on success, or a negative error in case of failure.

Quoted 3 lines

This API feels akward to me. Could you collapse this by using a dynamic pointer, recently added? And drop the ptr_to_mem+const_size part at least? That seems redundant with latest kernels.

Revisiting this decision.

After following that path, seems to me that adding the newly proposed 'bpf_skb_set_tunnel_opt_dynptr' API creates awkwardness in user's bpf program.

Suppose user needs to hold a map of the options received on incoming traffic based on whatever 'bpf_skb_get_tunnel_opt' returns.

Then, when user needs to apply the options on the return traffic, we have the following two alternative APIs:

option A: bpf_skb_set_var_tunnel_opt
------------------------------------

struct tun_opts {
    __u8 data[MAX_OPT_SZ];
    __u32 len;
};

BPF_MAP_TYPE_HASH opts_map; // __type(value, tun_opts)

...

struct tun_opts *opts;

  opts = bpf_map_lookup_elem(&opts_map, &the_flow_key);
  bpf_skb_set_var_tunnel_opt(skb, opts->data, sizeof(opts->data), opts->len);

option B: bpf_skb_set_tunnel_opt_dynptr
---------------------------------------

struct tun_opts {
    __u8 data[MAX_OPT_SZ];
};

BPF_MAP_TYPE_HASH opts_map; // __type(value, tun_opts)
BPF_MAP_TYPE_HASH opts_len_map; // __type(value, __u32)

...

struct bpf_dynptr dptr;
struct tun_opts *opts;
__u32 *opts_len;

  opts = bpf_map_lookup_elem(&opts_map, &the_flow_key);
  opts_len = bpf_map_lookup_elem(&opts_len_map, &the_flow_key);

bpf_dynptr_from_mem(opts, sizeof(*opts), 0, &dptr); // construct a dynptr from the raw option data
bpf_dynptr_trim(&dptr, opts_len); // trim it based on stored option len
bpf_skb_set_tunnel_opt_dynptr(skb, &dptr);

IMO, the 2nd user program is less readable:
- need to store the received options length in a separate map
- 5 bpf function calls instead of 2

Despite the awkwardness of the 'bpf_skb_set_var_tunnel_opt' API (passing both constant size *and* dynamic len), it really creates more simple and readable ebpf programs.

WDYT?

Best
Shmulik

#5 lore raw
Quoted 67 lines · Shmulik Ladkani
Quoted 11 lines · On Tue, 23 Aug 2022 00:59:01 -0700 John Fastabend <[email protected]>…
Quoted 10 lines

+ * long bpf_skb_set_var_tunnel_opt(struct sk_buff *skb, void *opt, u32 size, u32 len)
+ * Description

+ *         Set tunnel options metadata for the packet associated to *skb*
+ *         to the variable length *len* bytes of option data contained in
+ *         the raw buffer *opt* sized *size*.

+ *
+ * See also the description of the **bpf_skb_get_tunnel_opt**\ ()
+ * helper for additional information.
+ * Return
+ * 0 on success, or a negative error in case of failure.

Quoted 3 lines

This API feels akward to me. Could you collapse this by using a dynamic pointer, recently added? And drop the ptr_to_mem+const_size part at least? That seems redundant with latest kernels.

Revisiting this decision.

After following that path, seems to me that adding the newly proposed 'bpf_skb_set_tunnel_opt_dynptr' API creates awkwardness in user's bpf program.

Suppose user needs to hold a map of the options received on incoming traffic based on whatever 'bpf_skb_get_tunnel_opt' returns.

Then, when user needs to apply the options on the return traffic, we have the following two alternative APIs:

option A: bpf_skb_set_var_tunnel_opt
------------------------------------

struct tun_opts {
    __u8 data[MAX_OPT_SZ];
    __u32 len;
};

BPF_MAP_TYPE_HASH opts_map; // __type(value, tun_opts)

...

struct tun_opts *opts;

  opts = bpf_map_lookup_elem(&opts_map, &the_flow_key);
  bpf_skb_set_var_tunnel_opt(skb, opts->data, sizeof(opts->data), opts->len);

option B: bpf_skb_set_tunnel_opt_dynptr
---------------------------------------

struct tun_opts {
    __u8 data[MAX_OPT_SZ];
};

BPF_MAP_TYPE_HASH opts_map; // __type(value, tun_opts)
BPF_MAP_TYPE_HASH opts_len_map; // __type(value, __u32)

...

struct bpf_dynptr dptr;
struct tun_opts *opts;
__u32 *opts_len;

  opts = bpf_map_lookup_elem(&opts_map, &the_flow_key);
  opts_len = bpf_map_lookup_elem(&opts_len_map, &the_flow_key);
Quoted 8 lines

bpf_dynptr_from_mem(opts, sizeof(*opts), 0, &dptr); // construct a dynptr from the raw option data
bpf_dynptr_trim(&dptr, opts_len); // trim it based on stored option len
bpf_skb_set_tunnel_opt_dynptr(skb, &dptr);

IMO, the 2nd user program is less readable:
- need to store the received options length in a separate map
- 5 bpf function calls instead of 2

I don't think you need a separate map to store the opts length. I think you can store

struct tun_opts {
     __u8 data[MAX_OPT_SZ];
     __u32 len;
};

in the map, and then do something like:

struct bpf_dynptr ptr;
struct tun_opts *opts;

opts = bpf_map_lookup_elem(&opts_map, &the_flow_key); bpf_skb_set_tunnel_opt_dynptr(skb, &ptr);

where inside the bpf_skb_set_tunnel_opt_dynptr() helper, you can parse the dynptr data back to a "struct tun_opts" (after doing requisite checks, eg making sure dynptr size matches sizeof(struct tun_opts)) and then access tun_opts->data and tun_opts->len accordingly.

What are your thoughts?

Quoted 10 lines

Despite the awkwardness of the 'bpf_skb_set_var_tunnel_opt' API (passing both constant size *and* dynamic len), it really creates more simple and readable ebpf programs.

WDYT?

Best
Shmulik

#6 lore raw
Quoted 23 lines · On Wed, 31 Aug 2022 12:07:28 -0700 Joanne Koong <[email protected]> wrote:
Quoted 23 lines

option B: bpf_skb_set_tunnel_opt_dynptr
---------------------------------------

struct tun_opts {
    __u8 data[MAX_OPT_SZ];
};

BPF_MAP_TYPE_HASH opts_map; // __type(value, tun_opts)
BPF_MAP_TYPE_HASH opts_len_map; // __type(value, __u32)

...

struct bpf_dynptr dptr;
struct tun_opts *opts;
__u32 *opts_len;

  opts = bpf_map_lookup_elem(&opts_map, &the_flow_key);
  opts_len = bpf_map_lookup_elem(&opts_len_map, &the_flow_key);

bpf_dynptr_from_mem(opts, sizeof(*opts), 0, &dptr); // construct a dynptr from the raw option data
bpf_dynptr_trim(&dptr, opts_len); // trim it based on stored option len
bpf_skb_set_tunnel_opt_dynptr(skb, &dptr);

Quoted 5 lines
Quoted 3 lines

IMO, the 2nd user program is less readable:
- need to store the received options length in a separate map
- 5 bpf function calls instead of 2

I don't think you need a separate map to store the opts length.

You are correct, I was under the impression that 'bpf_dynptr_from_mem' will *only* accept the exact pointer to the map element, due to the verifier checking this:

	case BPF_FUNC_dynptr_from_mem:
		if (regs[BPF_REG_1].type != PTR_TO_MAP_VALUE) {
			verbose(env, "Unsupported reg type %s for bpf_dynptr_from_mem data\n",

However I found out this is also allowed by the verifier:

struct tun_opts {
    __u32 len;
    __u8 data[MAX_OPT_SZ];
};
  opts = bpf_map_lookup_elem(&opts_map, &the_flow_key);
  bpf_dynptr_from_mem(opts->data, sizeof(opts->data), 0, &dptr);
  bpf_dynptr_trim(&dptr, opts->len);
  bpf_skb_set_tunnel_opt_dynptr(skb, &dptr);

Nevertheless, IMO it doesn't change the argument about the better readability and simplicity of the "bpf_skb_set_var_tunnel_opt" approach:

  opts = bpf_map_lookup_elem(&opts_map, &the_flow_key);
  bpf_skb_set_var_tunnel_opt(skb, opts->data, sizeof(opts->data), opts->len);

which is more straight forward.

It even allows the bpf program to set tunnel options *without* using a map element at all, e.g. this usecase:

  __u8 data[MAX_OPT_SZ];

len = bpf_skb_get_tunnel_opt(skb, data, sizeof(data));
...
// fiddle with the skb, the tunnel key, the tunnel remote, or whatever. // then send again on a collect-md interface, setting same tunnel // opts as seen.
...
bpf_skb_set_var_tunnel_opt(skb, data, sizeof(data), len); bpf_redirect()

Best,
Shmulik

#7 lore raw
In reply to Shmulik Ladkani
Quoted 75 lines
Quoted 11 lines · On Tue, 23 Aug 2022 00:59:01 -0700 John Fastabend <[email protected]>…
Quoted 10 lines

+ * long bpf_skb_set_var_tunnel_opt(struct sk_buff *skb, void *opt, u32 size, u32 len)
+ * Description
+ * Set tunnel options metadata for the packet associated to *skb*
+ * to the variable length *len* bytes of option data contained in
+ * the raw buffer *opt* sized *size*.
+ *
+ * See also the description of the **bpf_skb_get_tunnel_opt**\ ()
+ * helper for additional information.
+ * Return
+ * 0 on success, or a negative error in case of failure.

Quoted 3 lines

This API feels akward to me. Could you collapse this by using a dynamic pointer, recently added? And drop the ptr_to_mem+const_size part at least? That seems redundant with latest kernels.

Revisiting this decision.

After following that path, seems to me that adding the newly proposed 'bpf_skb_set_tunnel_opt_dynptr' API creates awkwardness in user's bpf program.

Suppose user needs to hold a map of the options received on incoming traffic based on whatever 'bpf_skb_get_tunnel_opt' returns.

Then, when user needs to apply the options on the return traffic, we have the following two alternative APIs:

option A: bpf_skb_set_var_tunnel_opt
------------------------------------

struct tun_opts {
    __u8 data[MAX_OPT_SZ];
    __u32 len;
};

BPF_MAP_TYPE_HASH opts_map; // __type(value, tun_opts)

...

struct tun_opts *opts;

  opts = bpf_map_lookup_elem(&opts_map, &the_flow_key);
  bpf_skb_set_var_tunnel_opt(skb, opts->data, sizeof(opts->data), opts->len);

option B: bpf_skb_set_tunnel_opt_dynptr
---------------------------------------

struct tun_opts {
    __u8 data[MAX_OPT_SZ];
};

BPF_MAP_TYPE_HASH opts_map; // __type(value, tun_opts)
BPF_MAP_TYPE_HASH opts_len_map; // __type(value, __u32)

...

struct bpf_dynptr dptr;
struct tun_opts *opts;
__u32 *opts_len;

  opts = bpf_map_lookup_elem(&opts_map, &the_flow_key);
  opts_len = bpf_map_lookup_elem(&opts_len_map, &the_flow_key);

bpf_dynptr_from_mem(opts, sizeof(*opts), 0, &dptr); // construct a dynptr from the raw option data
bpf_dynptr_trim(&dptr, opts_len); // trim it based on stored option len
bpf_skb_set_tunnel_opt_dynptr(skb, &dptr);

IMO, the 2nd user program is less readable:
- need to store the received options length in a separate map
- 5 bpf function calls instead of 2

Quoted 5 lines

Despite the awkwardness of the 'bpf_skb_set_var_tunnel_opt' API (passing both constant size *and* dynamic len), it really creates more simple and readable ebpf programs.

WDYT?

John, Daniel, would appreciate your opinion re the prefered BPF API we add to set var-length tunnel options. See the difference in bpf user programs based on the 2 suggestions above.

Thanks,
Shmulik

#8 lore raw

PATCH v3 2/3 selftests/bpf: Simplify test_tunnel setup for allowing non-local tunnel traffic

In reply to Shmulik Ladkani

Commit 1115169f47ae ("selftests/bpf: Don't assign outer source IP to host") removed the secondary IP (IP4_ADDR2_VETH1) assigned to veth1, in order to test bpf_skb_set_tunnel_key's functionality when tunnel destination isn't assigned to an interface.

The chosen setup for testing the "tunnel to unassigned outer IP" scenario was rather complex: (1) static ARP entries in order to bypass ARP (o/w requests will fail as the target address isn't assigned locally), and (2) a BPF program running on veth1 ingress which manipulates the IP header's daddr to the actual IP assigned to the interface (o/w tunnel traffic won't be accepted locally).

This is complex, and adds a dependency on this hidden "dnat"-like eBPF program, that needs to be replicated when new tunnel tests are added.

Instead, we can have a much simpler setup: Add the secondary IP as a *local route* in a table pointed by a custom fib rule. No static arp entries are needed, and the special eBPF program that "dnats" the outer destination can be removed.

This commit is a revert of 1115169f47ae, with the addition of the local route of IP4_ADDR2_VETH1 (instead of the original address assignment).

---
v2: Place the local route for the secondary IP in a custom table
pointed by a custom fib rule; this ensures the IP is not considered assigned to a device.

---
 .../selftests/bpf/prog_tests/test_tunnel.c    | 23 ++----
 .../selftests/bpf/progs/test_tunnel_kern.c    | 80 +++----------------
 2 files changed, 17 insertions(+), 86 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_tunnel.c +8 -15
index eea274110267..852da04ff281 100644
@@ -82,7 +82,6 @@
 
 #define MAC_TUNL_DEV0 "52:54:00:d9:01:00"
 #define MAC_TUNL_DEV1 "52:54:00:d9:02:00"
-#define MAC_VETH1 "52:54:00:d9:03:00"
 
 #define VXLAN_TUNL_DEV0 "vxlan00"
 #define VXLAN_TUNL_DEV1 "vxlan11"
@@ -109,9 +108,15 @@
 static int config_device(void)
 {
 	SYS("ip netns add at_ns0");
-	SYS("ip link add veth0 address " MAC_VETH1 " type veth peer name veth1");
+	SYS("ip link add veth0 type veth peer name veth1");
 	SYS("ip link set veth0 netns at_ns0");
 	SYS("ip addr add " IP4_ADDR1_VETH1 "/24 dev veth1");
+	/* Create a custom rule routing IP4_ADDR2_VETH1 as local.
+	 * Do not place it in "local" table, to avoid this IP being considered
+	 * assigned to a device.
+	 */
+	SYS("ip rule add to " IP4_ADDR2_VETH1 " table 20");
+	SYS("ip route add local " IP4_ADDR2_VETH1 "/32 dev veth1 table 20");
 	SYS("ip link set dev veth1 up mtu 1500");
 	SYS("ip netns exec at_ns0 ip addr add " IP4_ADDR_VETH0 "/24 dev veth0");
 	SYS("ip netns exec at_ns0 ip link set dev veth0 up mtu 1500");
@@ -125,6 +130,7 @@ static void cleanup(void)
 {
 	SYS_NOFAIL("test -f /var/run/netns/at_ns0 && ip netns delete at_ns0");
 	SYS_NOFAIL("ip link del veth1 2> /dev/null");
+	SYS_NOFAIL("ip rule del to %s table 20 2> /dev/null", IP4_ADDR2_VETH1);
 	SYS_NOFAIL("ip link del %s 2> /dev/null", VXLAN_TUNL_DEV1);
 	SYS_NOFAIL("ip link del %s 2> /dev/null", IP6VXLAN_TUNL_DEV1);
 }
@@ -140,8 +146,6 @@ static int add_vxlan_tunnel(void)
 	    VXLAN_TUNL_DEV0, IP4_ADDR_TUNL_DEV0);
 	SYS("ip netns exec at_ns0 ip neigh add %s lladdr %s dev %s",
 	    IP4_ADDR_TUNL_DEV1, MAC_TUNL_DEV1, VXLAN_TUNL_DEV0);
-	SYS("ip netns exec at_ns0 ip neigh add %s lladdr %s dev veth0",
-	    IP4_ADDR2_VETH1, MAC_VETH1);
 
 	/* root namespace */
 	SYS("ip link add dev %s type vxlan external gbp dstport 4789",
@@ -279,17 +283,6 @@ static void test_vxlan_tunnel(void)
 	if (attach_tc_prog(&tc_hook, get_src_prog_fd, set_src_prog_fd))
 		goto done;
 
-	/* load and attach bpf prog to veth dev tc hook point */
-	ifindex = if_nametoindex("veth1");
-	if (!ASSERT_NEQ(ifindex, 0, "veth1 ifindex"))
-		goto done;
-	tc_hook.ifindex = ifindex;
-	set_dst_prog_fd = bpf_program__fd(skel->progs.veth_set_outer_dst);
-	if (!ASSERT_GE(set_dst_prog_fd, 0, "bpf_program__fd"))
-		goto done;
-	if (attach_tc_prog(&tc_hook, set_dst_prog_fd, -1))
-		goto done;
-
 	/* load and attach prog set_md to tunnel dev tc hook point at_ns0 */
 	nstoken = open_netns("at_ns0");
 	if (!ASSERT_OK_PTR(nstoken, "setns src"))
tools/testing/selftests/bpf/progs/test_tunnel_kern.c +9 -71

123 lines not on this page · raw message

Signature · 1 line
2.37.2
#9 lore raw

PATCH v3 3/3 selftests/bpf: Add geneve with bpf_skb_set_var_tunnel_opt test-case to test_progs

In reply to Shmulik Ladkani

Add geneve test to test_tunnel. The test setup and scheme resembles the existing vxlan test.

The test also checks variable length tunnel option assignment using bpf_skb_set_var_tunnel_opt.

---
 .../selftests/bpf/prog_tests/test_tunnel.c    | 108 ++++++++++++++++
 .../selftests/bpf/progs/test_tunnel_kern.c    | 118 ++++++++++++++++++
 2 files changed, 226 insertions(+)
tools/testing/selftests/bpf/prog_tests/test_tunnel.c +108 -0
index 852da04ff281..9aae03c720e9 100644
@@ -87,6 +87,8 @@
 #define VXLAN_TUNL_DEV1 "vxlan11"
 #define IP6VXLAN_TUNL_DEV0 "ip6vxlan00"
 #define IP6VXLAN_TUNL_DEV1 "ip6vxlan11"
+#define GENEVE_TUNL_DEV0 "geneve00"
+#define GENEVE_TUNL_DEV1 "geneve11"
 
 #define PING_ARGS "-i 0.01 -c 3 -w 10 -q"
 
@@ -133,6 +135,38 @@ static void cleanup(void)
 	SYS_NOFAIL("ip rule del to %s table 20 2> /dev/null", IP4_ADDR2_VETH1);
 	SYS_NOFAIL("ip link del %s 2> /dev/null", VXLAN_TUNL_DEV1);
 	SYS_NOFAIL("ip link del %s 2> /dev/null", IP6VXLAN_TUNL_DEV1);
+	SYS_NOFAIL("ip link del %s 2> /dev/null", GENEVE_TUNL_DEV1);
+}
+
+static int add_geneve_tunnel(void)
+{
+	/* at_ns0 namespace */
+	SYS("ip netns exec at_ns0 ip link add dev %s type geneve external",
+	    GENEVE_TUNL_DEV0);
+	SYS("ip netns exec at_ns0 ip link set dev %s address %s up",
+	    GENEVE_TUNL_DEV0, MAC_TUNL_DEV0);
+	SYS("ip netns exec at_ns0 ip addr add dev %s %s/24",
+	    GENEVE_TUNL_DEV0, IP4_ADDR_TUNL_DEV0);
+	SYS("ip netns exec at_ns0 ip neigh add %s lladdr %s dev %s",
+	    IP4_ADDR_TUNL_DEV1, MAC_TUNL_DEV1, GENEVE_TUNL_DEV0);
+
+	/* root namespace */
+	SYS("ip link add dev %s type geneve external", GENEVE_TUNL_DEV1);
+	SYS("ip link set dev %s address %s up", GENEVE_TUNL_DEV1, MAC_TUNL_DEV1);
+	SYS("ip addr add dev %s %s/24", GENEVE_TUNL_DEV1, IP4_ADDR_TUNL_DEV1);
+	SYS("ip neigh add %s lladdr %s dev %s",
+	    IP4_ADDR_TUNL_DEV0, MAC_TUNL_DEV0, GENEVE_TUNL_DEV1);
+
+	return 0;
+fail:
+	return -1;
+}
+
+static void delete_geneve_tunnel(void)
+{
+	SYS_NOFAIL("ip netns exec at_ns0 ip link delete dev %s",
+		   GENEVE_TUNL_DEV0);
+	SYS_NOFAIL("ip link delete dev %s", GENEVE_TUNL_DEV1);
 }
 
 static int add_vxlan_tunnel(void)
@@ -248,6 +282,79 @@ static int attach_tc_prog(struct bpf_tc_hook *hook, int igr_fd, int egr_fd)
 	return 0;
 }
 
+static void test_geneve_tunnel(void)
+{
+	struct test_tunnel_kern *skel = NULL;
+	struct nstoken *nstoken;
+	int local_ip_map_fd = -1;
+	int set_src_prog_fd, get_src_prog_fd;
+	int set_dst_prog_fd;
+	int key = 0, ifindex = -1;
+	uint local_ip;
+	int err;
+	DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook,
+			    .attach_point = BPF_TC_INGRESS);
+
+	/* add genve tunnel */
+	err = add_geneve_tunnel();
+	if (!ASSERT_OK(err, "add geneve tunnel"))
+		goto done;
+
+	/* load and attach bpf prog to tunnel dev tc hook point */
+	skel = test_tunnel_kern__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_tunnel_kern__open_and_load"))
+		goto done;
+	ifindex = if_nametoindex(GENEVE_TUNL_DEV1);
+	if (!ASSERT_NEQ(ifindex, 0, "geneve11 ifindex"))
+		goto done;
+	tc_hook.ifindex = ifindex;
+	get_src_prog_fd = bpf_program__fd(skel->progs.geneve_get_tunnel_src);
+	set_src_prog_fd = bpf_program__fd(skel->progs.geneve_set_tunnel_src);
+	if (!ASSERT_GE(get_src_prog_fd, 0, "bpf_program__fd"))
+		goto done;
+	if (!ASSERT_GE(set_src_prog_fd, 0, "bpf_program__fd"))
+		goto done;
+	if (attach_tc_prog(&tc_hook, get_src_prog_fd, set_src_prog_fd))
+		goto done;
+
+	/* load and attach prog set_md to tunnel dev tc hook point at_ns0 */
+	nstoken = open_netns("at_ns0");
+	if (!ASSERT_OK_PTR(nstoken, "setns src"))
+		goto done;
+	ifindex = if_nametoindex(GENEVE_TUNL_DEV0);
+	if (!ASSERT_NEQ(ifindex, 0, "geneve00 ifindex"))
+		goto done;
+	tc_hook.ifindex = ifindex;
+	set_dst_prog_fd = bpf_program__fd(skel->progs.geneve_set_tunnel_dst);
+	if (!ASSERT_GE(set_dst_prog_fd, 0, "bpf_program__fd"))
+		goto done;
+	if (attach_tc_prog(&tc_hook, -1, set_dst_prog_fd))
+		goto done;
+	close_netns(nstoken);
+
+	/* use veth1 ip 1 as tunnel source ip */
+	local_ip_map_fd = bpf_map__fd(skel->maps.local_ip_map);
+	if (!ASSERT_GE(local_ip_map_fd, 0, "bpf_map__fd"))
+		goto done;
+	local_ip = IP4_ADDR1_HEX_VETH1;
+	err = bpf_map_update_elem(local_ip_map_fd, &key, &local_ip, BPF_ANY);
+	if (!ASSERT_OK(err, "update bpf local_ip_map"))
+		goto done;
+
+	/* ping test */
+	err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV0);
+	if (!ASSERT_OK(err, "test_ping"))
+		goto done;
+
+done:
+	/* delete geneve tunnel */
+	delete_geneve_tunnel();
+	if (local_ip_map_fd >= 0)
+		close(local_ip_map_fd);
+	if (skel)
+		test_tunnel_kern__destroy(skel);
+}
+
 static void test_vxlan_tunnel(void)
 {
 	struct test_tunnel_kern *skel = NULL;
@@ -408,6 +515,7 @@ static void *test_tunnel_run_tests(void *arg)
 
 	RUN_TEST(vxlan_tunnel);
 	RUN_TEST(ip6vxlan_tunnel);
+	RUN_TEST(geneve_tunnel);
 
 	cleanup();
 
tools/testing/selftests/bpf/progs/test_tunnel_kern.c +118 -0

130 lines not on this page · raw message

Signature · 1 line
2.37.2