diff mbox series

[v1,bpf-next,1/2] bpf: add support for passing mark with bpf_fib_lookup

Message ID 20240322140244.50971-2-aspsk@isovalent.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series BPF: support mark in bpf_fib_lookup | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 7482 this patch: 7482
netdev/build_tools success Errors and warnings before: 2 this patch: 2
netdev/cc_maintainers warning 9 maintainers not CCed: haoluo@google.com pabeni@redhat.com john.fastabend@gmail.com eddyz87@gmail.com song@kernel.org kpsingh@kernel.org yonghong.song@linux.dev kuba@kernel.org edumazet@google.com
netdev/build_clang success Errors and warnings before: 2244 this patch: 2244
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 7870 this patch: 7870
netdev/checkpatch warning WARNING: line length of 82 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization

Commit Message

Anton Protopopov March 22, 2024, 2:02 p.m. UTC
Extend the bpf_fib_lookup() helper by making it to utilize mark if
the BPF_FIB_LOOKUP_MARK flag is set. In order to pass the mark the
four bytes of struct bpf_fib_lookup are used, shared with the
output-only smac/dmac fields.

Signed-off-by: Anton Protopopov <aspsk@isovalent.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
---
 include/uapi/linux/bpf.h       | 20 ++++++++++++++++++--
 net/core/filter.c              | 12 +++++++++---
 tools/include/uapi/linux/bpf.h | 20 ++++++++++++++++++--
 3 files changed, 45 insertions(+), 7 deletions(-)

Comments

David Ahern March 24, 2024, 5:38 p.m. UTC | #1
On 3/22/24 8:02 AM, Anton Protopopov wrote:
> Extend the bpf_fib_lookup() helper by making it to utilize mark if
> the BPF_FIB_LOOKUP_MARK flag is set. In order to pass the mark the
> four bytes of struct bpf_fib_lookup are used, shared with the
> output-only smac/dmac fields.
> 
> Signed-off-by: Anton Protopopov <aspsk@isovalent.com>
> Acked-by: Daniel Borkmann <daniel@iogearbox.net>
> ---
>  include/uapi/linux/bpf.h       | 20 ++++++++++++++++++--
>  net/core/filter.c              | 12 +++++++++---
>  tools/include/uapi/linux/bpf.h | 20 ++++++++++++++++++--
>  3 files changed, 45 insertions(+), 7 deletions(-)
> 
> diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> index 9585f5345353..96d57e483133 100644
> --- a/include/uapi/linux/bpf.h
> +++ b/include/uapi/linux/bpf.h
> @@ -3394,6 +3394,10 @@ union bpf_attr {
>   *			for the nexthop. If the src addr cannot be derived,
>   *			**BPF_FIB_LKUP_RET_NO_SRC_ADDR** is returned. In this
>   *			case, *params*->dmac and *params*->smac are not set either.
> + *		**BPF_FIB_LOOKUP_MARK**
> + *			Use the mark present in *params*->mark for the fib lookup.
> + *			This option should not be used with BPF_FIB_LOOKUP_DIRECT,
> + *			as it only has meaning for full lookups.
>   *
>   *		*ctx* is either **struct xdp_md** for XDP programs or
>   *		**struct sk_buff** tc cls_act programs.
> @@ -7120,6 +7124,7 @@ enum {
>  	BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
>  	BPF_FIB_LOOKUP_TBID    = (1U << 3),
>  	BPF_FIB_LOOKUP_SRC     = (1U << 4),
> +	BPF_FIB_LOOKUP_MARK    = (1U << 5),
>  };
>  
>  enum {
> @@ -7197,8 +7202,19 @@ struct bpf_fib_lookup {
>  		__u32	tbid;
>  	};
>  
> -	__u8	smac[6];     /* ETH_ALEN */
> -	__u8	dmac[6];     /* ETH_ALEN */
> +	union {
> +		/* input */
> +		struct {
> +			__u32	mark;   /* policy routing */
> +			/* 2 4-byte holes for input */
> +		};
> +
> +		/* output: source and dest mac */
> +		struct {
> +			__u8	smac[6];	/* ETH_ALEN */
> +			__u8	dmac[6];	/* ETH_ALEN */
> +		};
> +	};
>  };
>  
>  struct bpf_redir_neigh {
> diff --git a/net/core/filter.c b/net/core/filter.c
> index 0c66e4a3fc5b..1205dd777dc2 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -5884,7 +5884,10 @@ static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
>  
>  		err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
>  	} else {
> -		fl4.flowi4_mark = 0;
> +		if (flags & BPF_FIB_LOOKUP_MARK)
> +			fl4.flowi4_mark = params->mark;
> +		else
> +			fl4.flowi4_mark = 0;
>  		fl4.flowi4_secid = 0;
>  		fl4.flowi4_tun_key.tun_id = 0;
>  		fl4.flowi4_uid = sock_net_uid(net, NULL);
> @@ -6027,7 +6030,10 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
>  		err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
>  						   strict);
>  	} else {
> -		fl6.flowi6_mark = 0;
> +		if (flags & BPF_FIB_LOOKUP_MARK)
> +			fl6.flowi6_mark = params->mark;
> +		else
> +			fl6.flowi6_mark = 0;
>  		fl6.flowi6_secid = 0;
>  		fl6.flowi6_tun_key.tun_id = 0;
>  		fl6.flowi6_uid = sock_net_uid(net, NULL);
> @@ -6105,7 +6111,7 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
>  
>  #define BPF_FIB_LOOKUP_MASK (BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT | \
>  			     BPF_FIB_LOOKUP_SKIP_NEIGH | BPF_FIB_LOOKUP_TBID | \
> -			     BPF_FIB_LOOKUP_SRC)
> +			     BPF_FIB_LOOKUP_SRC | BPF_FIB_LOOKUP_MARK)
>  
>  BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
>  	   struct bpf_fib_lookup *, params, int, plen, u32, flags)
> diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
> index bf80b614c4db..4c9b5bfbd9c6 100644
> --- a/tools/include/uapi/linux/bpf.h
> +++ b/tools/include/uapi/linux/bpf.h
> @@ -3393,6 +3393,10 @@ union bpf_attr {
>   *			for the nexthop. If the src addr cannot be derived,
>   *			**BPF_FIB_LKUP_RET_NO_SRC_ADDR** is returned. In this
>   *			case, *params*->dmac and *params*->smac are not set either.
> + *		**BPF_FIB_LOOKUP_MARK**
> + *			Use the mark present in *params*->mark for the fib lookup.
> + *			This option should not be used with BPF_FIB_LOOKUP_DIRECT,
> + *			as it only has meaning for full lookups.
>   *
>   *		*ctx* is either **struct xdp_md** for XDP programs or
>   *		**struct sk_buff** tc cls_act programs.
> @@ -7119,6 +7123,7 @@ enum {
>  	BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
>  	BPF_FIB_LOOKUP_TBID    = (1U << 3),
>  	BPF_FIB_LOOKUP_SRC     = (1U << 4),
> +	BPF_FIB_LOOKUP_MARK    = (1U << 5),
>  };
>  
>  enum {
> @@ -7196,8 +7201,19 @@ struct bpf_fib_lookup {
>  		__u32	tbid;
>  	};
>  
> -	__u8	smac[6];     /* ETH_ALEN */
> -	__u8	dmac[6];     /* ETH_ALEN */
> +	union {
> +		/* input */
> +		struct {
> +			__u32	mark;   /* policy routing */
> +			/* 2 4-byte holes for input */
> +		};
> +
> +		/* output: source and dest mac */
> +		struct {
> +			__u8	smac[6];	/* ETH_ALEN */
> +			__u8	dmac[6];	/* ETH_ALEN */
> +		};
> +	};
>  };
>  
>  struct bpf_redir_neigh {

It would be good to add

static_assert(sizeof(struct bpf_fib_lookup) == 64, "bpf_fib_lookup size
check");

to ensure this struct never exceeds a cacheline.

The patch itself looks good to me:

Reviewed-by: David Ahern <dsahern@kernel.org>
Anton Protopopov March 25, 2024, 12:19 p.m. UTC | #2
On Sun, Mar 24, 2024 at 11:38:44AM -0600, David Ahern wrote:
> On 3/22/24 8:02 AM, Anton Protopopov wrote:
> > Extend the bpf_fib_lookup() helper by making it to utilize mark if
> > the BPF_FIB_LOOKUP_MARK flag is set. In order to pass the mark the
> > four bytes of struct bpf_fib_lookup are used, shared with the
> > output-only smac/dmac fields.
> > 
> > Signed-off-by: Anton Protopopov <aspsk@isovalent.com>
> > Acked-by: Daniel Borkmann <daniel@iogearbox.net>
> > ---
> >  include/uapi/linux/bpf.h       | 20 ++++++++++++++++++--
> >  net/core/filter.c              | 12 +++++++++---
> >  tools/include/uapi/linux/bpf.h | 20 ++++++++++++++++++--
> >  3 files changed, 45 insertions(+), 7 deletions(-)
> > 
> > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
> > index 9585f5345353..96d57e483133 100644
> > --- a/include/uapi/linux/bpf.h
> > +++ b/include/uapi/linux/bpf.h
> > @@ -3394,6 +3394,10 @@ union bpf_attr {
> >   *			for the nexthop. If the src addr cannot be derived,
> >   *			**BPF_FIB_LKUP_RET_NO_SRC_ADDR** is returned. In this
> >   *			case, *params*->dmac and *params*->smac are not set either.
> > + *		**BPF_FIB_LOOKUP_MARK**
> > + *			Use the mark present in *params*->mark for the fib lookup.
> > + *			This option should not be used with BPF_FIB_LOOKUP_DIRECT,
> > + *			as it only has meaning for full lookups.
> >   *
> >   *		*ctx* is either **struct xdp_md** for XDP programs or
> >   *		**struct sk_buff** tc cls_act programs.
> > @@ -7120,6 +7124,7 @@ enum {
> >  	BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
> >  	BPF_FIB_LOOKUP_TBID    = (1U << 3),
> >  	BPF_FIB_LOOKUP_SRC     = (1U << 4),
> > +	BPF_FIB_LOOKUP_MARK    = (1U << 5),
> >  };
> >  
> >  enum {
> > @@ -7197,8 +7202,19 @@ struct bpf_fib_lookup {
> >  		__u32	tbid;
> >  	};
> >  
> > -	__u8	smac[6];     /* ETH_ALEN */
> > -	__u8	dmac[6];     /* ETH_ALEN */
> > +	union {
> > +		/* input */
> > +		struct {
> > +			__u32	mark;   /* policy routing */
> > +			/* 2 4-byte holes for input */
> > +		};
> > +
> > +		/* output: source and dest mac */
> > +		struct {
> > +			__u8	smac[6];	/* ETH_ALEN */
> > +			__u8	dmac[6];	/* ETH_ALEN */
> > +		};
> > +	};
> >  };
> >  
> >  struct bpf_redir_neigh {
> > diff --git a/net/core/filter.c b/net/core/filter.c
> > index 0c66e4a3fc5b..1205dd777dc2 100644
> > --- a/net/core/filter.c
> > +++ b/net/core/filter.c
> > @@ -5884,7 +5884,10 @@ static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
> >  
> >  		err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
> >  	} else {
> > -		fl4.flowi4_mark = 0;
> > +		if (flags & BPF_FIB_LOOKUP_MARK)
> > +			fl4.flowi4_mark = params->mark;
> > +		else
> > +			fl4.flowi4_mark = 0;
> >  		fl4.flowi4_secid = 0;
> >  		fl4.flowi4_tun_key.tun_id = 0;
> >  		fl4.flowi4_uid = sock_net_uid(net, NULL);
> > @@ -6027,7 +6030,10 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
> >  		err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
> >  						   strict);
> >  	} else {
> > -		fl6.flowi6_mark = 0;
> > +		if (flags & BPF_FIB_LOOKUP_MARK)
> > +			fl6.flowi6_mark = params->mark;
> > +		else
> > +			fl6.flowi6_mark = 0;
> >  		fl6.flowi6_secid = 0;
> >  		fl6.flowi6_tun_key.tun_id = 0;
> >  		fl6.flowi6_uid = sock_net_uid(net, NULL);
> > @@ -6105,7 +6111,7 @@ static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
> >  
> >  #define BPF_FIB_LOOKUP_MASK (BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT | \
> >  			     BPF_FIB_LOOKUP_SKIP_NEIGH | BPF_FIB_LOOKUP_TBID | \
> > -			     BPF_FIB_LOOKUP_SRC)
> > +			     BPF_FIB_LOOKUP_SRC | BPF_FIB_LOOKUP_MARK)
> >  
> >  BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
> >  	   struct bpf_fib_lookup *, params, int, plen, u32, flags)
> > diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
> > index bf80b614c4db..4c9b5bfbd9c6 100644
> > --- a/tools/include/uapi/linux/bpf.h
> > +++ b/tools/include/uapi/linux/bpf.h
> > @@ -3393,6 +3393,10 @@ union bpf_attr {
> >   *			for the nexthop. If the src addr cannot be derived,
> >   *			**BPF_FIB_LKUP_RET_NO_SRC_ADDR** is returned. In this
> >   *			case, *params*->dmac and *params*->smac are not set either.
> > + *		**BPF_FIB_LOOKUP_MARK**
> > + *			Use the mark present in *params*->mark for the fib lookup.
> > + *			This option should not be used with BPF_FIB_LOOKUP_DIRECT,
> > + *			as it only has meaning for full lookups.
> >   *
> >   *		*ctx* is either **struct xdp_md** for XDP programs or
> >   *		**struct sk_buff** tc cls_act programs.
> > @@ -7119,6 +7123,7 @@ enum {
> >  	BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
> >  	BPF_FIB_LOOKUP_TBID    = (1U << 3),
> >  	BPF_FIB_LOOKUP_SRC     = (1U << 4),
> > +	BPF_FIB_LOOKUP_MARK    = (1U << 5),
> >  };
> >  
> >  enum {
> > @@ -7196,8 +7201,19 @@ struct bpf_fib_lookup {
> >  		__u32	tbid;
> >  	};
> >  
> > -	__u8	smac[6];     /* ETH_ALEN */
> > -	__u8	dmac[6];     /* ETH_ALEN */
> > +	union {
> > +		/* input */
> > +		struct {
> > +			__u32	mark;   /* policy routing */
> > +			/* 2 4-byte holes for input */
> > +		};
> > +
> > +		/* output: source and dest mac */
> > +		struct {
> > +			__u8	smac[6];	/* ETH_ALEN */
> > +			__u8	dmac[6];	/* ETH_ALEN */
> > +		};
> > +	};
> >  };
> >  
> >  struct bpf_redir_neigh {
> 
> It would be good to add
> 
> static_assert(sizeof(struct bpf_fib_lookup) == 64, "bpf_fib_lookup size
> check");
> 
> to ensure this struct never exceeds a cacheline.

Thanks, added: https://github.com/aspsk/bpf-next/commit/7cd3685e52d5

> 
> The patch itself looks good to me:
> 
> Reviewed-by: David Ahern <dsahern@kernel.org>

Thanks!
diff mbox series

Patch

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 9585f5345353..96d57e483133 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -3394,6 +3394,10 @@  union bpf_attr {
  *			for the nexthop. If the src addr cannot be derived,
  *			**BPF_FIB_LKUP_RET_NO_SRC_ADDR** is returned. In this
  *			case, *params*->dmac and *params*->smac are not set either.
+ *		**BPF_FIB_LOOKUP_MARK**
+ *			Use the mark present in *params*->mark for the fib lookup.
+ *			This option should not be used with BPF_FIB_LOOKUP_DIRECT,
+ *			as it only has meaning for full lookups.
  *
  *		*ctx* is either **struct xdp_md** for XDP programs or
  *		**struct sk_buff** tc cls_act programs.
@@ -7120,6 +7124,7 @@  enum {
 	BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
 	BPF_FIB_LOOKUP_TBID    = (1U << 3),
 	BPF_FIB_LOOKUP_SRC     = (1U << 4),
+	BPF_FIB_LOOKUP_MARK    = (1U << 5),
 };
 
 enum {
@@ -7197,8 +7202,19 @@  struct bpf_fib_lookup {
 		__u32	tbid;
 	};
 
-	__u8	smac[6];     /* ETH_ALEN */
-	__u8	dmac[6];     /* ETH_ALEN */
+	union {
+		/* input */
+		struct {
+			__u32	mark;   /* policy routing */
+			/* 2 4-byte holes for input */
+		};
+
+		/* output: source and dest mac */
+		struct {
+			__u8	smac[6];	/* ETH_ALEN */
+			__u8	dmac[6];	/* ETH_ALEN */
+		};
+	};
 };
 
 struct bpf_redir_neigh {
diff --git a/net/core/filter.c b/net/core/filter.c
index 0c66e4a3fc5b..1205dd777dc2 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -5884,7 +5884,10 @@  static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
 
 		err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
 	} else {
-		fl4.flowi4_mark = 0;
+		if (flags & BPF_FIB_LOOKUP_MARK)
+			fl4.flowi4_mark = params->mark;
+		else
+			fl4.flowi4_mark = 0;
 		fl4.flowi4_secid = 0;
 		fl4.flowi4_tun_key.tun_id = 0;
 		fl4.flowi4_uid = sock_net_uid(net, NULL);
@@ -6027,7 +6030,10 @@  static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
 		err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
 						   strict);
 	} else {
-		fl6.flowi6_mark = 0;
+		if (flags & BPF_FIB_LOOKUP_MARK)
+			fl6.flowi6_mark = params->mark;
+		else
+			fl6.flowi6_mark = 0;
 		fl6.flowi6_secid = 0;
 		fl6.flowi6_tun_key.tun_id = 0;
 		fl6.flowi6_uid = sock_net_uid(net, NULL);
@@ -6105,7 +6111,7 @@  static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
 
 #define BPF_FIB_LOOKUP_MASK (BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT | \
 			     BPF_FIB_LOOKUP_SKIP_NEIGH | BPF_FIB_LOOKUP_TBID | \
-			     BPF_FIB_LOOKUP_SRC)
+			     BPF_FIB_LOOKUP_SRC | BPF_FIB_LOOKUP_MARK)
 
 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
 	   struct bpf_fib_lookup *, params, int, plen, u32, flags)
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index bf80b614c4db..4c9b5bfbd9c6 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -3393,6 +3393,10 @@  union bpf_attr {
  *			for the nexthop. If the src addr cannot be derived,
  *			**BPF_FIB_LKUP_RET_NO_SRC_ADDR** is returned. In this
  *			case, *params*->dmac and *params*->smac are not set either.
+ *		**BPF_FIB_LOOKUP_MARK**
+ *			Use the mark present in *params*->mark for the fib lookup.
+ *			This option should not be used with BPF_FIB_LOOKUP_DIRECT,
+ *			as it only has meaning for full lookups.
  *
  *		*ctx* is either **struct xdp_md** for XDP programs or
  *		**struct sk_buff** tc cls_act programs.
@@ -7119,6 +7123,7 @@  enum {
 	BPF_FIB_LOOKUP_SKIP_NEIGH = (1U << 2),
 	BPF_FIB_LOOKUP_TBID    = (1U << 3),
 	BPF_FIB_LOOKUP_SRC     = (1U << 4),
+	BPF_FIB_LOOKUP_MARK    = (1U << 5),
 };
 
 enum {
@@ -7196,8 +7201,19 @@  struct bpf_fib_lookup {
 		__u32	tbid;
 	};
 
-	__u8	smac[6];     /* ETH_ALEN */
-	__u8	dmac[6];     /* ETH_ALEN */
+	union {
+		/* input */
+		struct {
+			__u32	mark;   /* policy routing */
+			/* 2 4-byte holes for input */
+		};
+
+		/* output: source and dest mac */
+		struct {
+			__u8	smac[6];	/* ETH_ALEN */
+			__u8	dmac[6];	/* ETH_ALEN */
+		};
+	};
 };
 
 struct bpf_redir_neigh {