Commit ea7da1d5 authored by Alexei Starovoitov's avatar Alexei Starovoitov
Browse files

Merge branch 'Various BPF helper improvements'



Daniel Borkmann says:

====================
This series adds two BPF helpers, that is, one for retrieving the classid
of an skb and another one to redirect via the neigh subsystem, and improves
also the cookie helpers by removing the atomic counter. I've also added
the bpf_tail_call_static() helper to the libbpf API that we've been using
in Cilium for a while now, and last but not least the series adds a few
selftests. For details, please check individual patches, thanks!

v3 -> v4:
  - Removed out_rec error path (Martin)
  - Integrate BPF_F_NEIGH flag into rejecting invalid flags (Martin)
    - I think this way it's better to avoid bit overlaps given it's
      right in the place that would need to be extended on new flags
v2 -> v3:
  - Removed double skb->dev = dev assignment (David)
  - Added headroom check for v6 path (David)
  - Set set flowi4_proto for ip_route_output_flow (David)
  - Rebased onto latest bpf-next
v1 -> v2:
  - Rework cookie generator to support nested contexts (Eric)
  - Use ip_neigh_gw6() and container_of() (David)
  - Rename __throw_build_bug() and improve comments (Andrii)
  - Use bpf_tail_call_static() also in BPF samples (Maciej)
====================

Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 963ec27a eef4a011
Loading
Loading
Loading
Loading

include/linux/cookie.h

0 → 100644
+51 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LINUX_COOKIE_H
#define __LINUX_COOKIE_H

#include <linux/atomic.h>
#include <linux/percpu.h>
#include <asm/local.h>

struct pcpu_gen_cookie {
	local_t nesting;
	u64 last;
} __aligned(16);

struct gen_cookie {
	struct pcpu_gen_cookie __percpu *local;
	atomic64_t forward_last ____cacheline_aligned_in_smp;
	atomic64_t reverse_last;
};

#define COOKIE_LOCAL_BATCH	4096

#define DEFINE_COOKIE(name)						\
	static DEFINE_PER_CPU(struct pcpu_gen_cookie, __##name);	\
	static struct gen_cookie name = {				\
		.local		= &__##name,				\
		.forward_last	= ATOMIC64_INIT(0),			\
		.reverse_last	= ATOMIC64_INIT(0),			\
	}

static __always_inline u64 gen_cookie_next(struct gen_cookie *gc)
{
	struct pcpu_gen_cookie *local = this_cpu_ptr(gc->local);
	u64 val;

	if (likely(local_inc_return(&local->nesting) == 1)) {
		val = local->last;
		if (__is_defined(CONFIG_SMP) &&
		    unlikely((val & (COOKIE_LOCAL_BATCH - 1)) == 0)) {
			s64 next = atomic64_add_return(COOKIE_LOCAL_BATCH,
						       &gc->forward_last);
			val = next - COOKIE_LOCAL_BATCH;
		}
		local->last = ++val;
	} else {
		val = atomic64_dec_return(&gc->reverse_last);
	}
	local_dec(&local->nesting);
	return val;
}

#endif /* __LINUX_COOKIE_H */
+5 −0
Original line number Diff line number Diff line
@@ -2548,6 +2548,11 @@ static inline int skb_mac_header_was_set(const struct sk_buff *skb)
	return skb->mac_header != (typeof(skb->mac_header))~0U;
}

static inline void skb_unset_mac_header(struct sk_buff *skb)
{
	skb->mac_header = (typeof(skb->mac_header))~0U;
}

static inline void skb_reset_mac_header(struct sk_buff *skb)
{
	skb->mac_header = skb->data - skb->head;
+13 −1
Original line number Diff line number Diff line
@@ -25,7 +25,19 @@ void sock_diag_unregister(const struct sock_diag_handler *h);
void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));
void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh));

u64 sock_gen_cookie(struct sock *sk);
u64 __sock_gen_cookie(struct sock *sk);

static inline u64 sock_gen_cookie(struct sock *sk)
{
	u64 cookie;

	preempt_disable();
	cookie = __sock_gen_cookie(sk);
	preempt_enable();

	return cookie;
}

int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie);
void sock_diag_save_cookie(struct sock *sk, __u32 *cookie);

+1 −1
Original line number Diff line number Diff line
@@ -230,7 +230,7 @@ extern struct list_head net_namespace_list;
struct net *get_net_ns_by_pid(pid_t pid);
struct net *get_net_ns_by_fd(int fd);

u64 net_gen_cookie(struct net *net);
u64 __net_gen_cookie(struct net *net);

#ifdef CONFIG_SYSCTL
void ipx_register_sysctl(void);
+24 −0
Original line number Diff line number Diff line
@@ -3643,6 +3643,28 @@ union bpf_attr {
 *		*flags* are identical to those used for bpf_snprintf_btf.
 *	Return
 *		0 on success or a negative error in case of failure.
 *
 * u64 bpf_skb_cgroup_classid(struct sk_buff *skb)
 * 	Description
 * 		See **bpf_get_cgroup_classid**\ () for the main description.
 * 		This helper differs from **bpf_get_cgroup_classid**\ () in that
 * 		the cgroup v1 net_cls class is retrieved only from the *skb*'s
 * 		associated socket instead of the current process.
 * 	Return
 * 		The id is returned or 0 in case the id could not be retrieved.
 *
 * long bpf_redirect_neigh(u32 ifindex, u64 flags)
 * 	Description
 * 		Redirect the packet to another net device of index *ifindex*
 * 		and fill in L2 addresses from neighboring subsystem. This helper
 * 		is somewhat similar to **bpf_redirect**\ (), except that it
 * 		fills in e.g. MAC addresses based on the L3 information from
 * 		the packet. This helper is supported for IPv4 and IPv6 protocols.
 * 		The *flags* argument is reserved and must be 0. The helper is
 * 		currently only supported for tc BPF program types.
 * 	Return
 * 		The helper returns **TC_ACT_REDIRECT** on success or
 * 		**TC_ACT_SHOT** on error.
 */
#define __BPF_FUNC_MAPPER(FN)		\
	FN(unspec),			\
@@ -3796,6 +3818,8 @@ union bpf_attr {
	FN(copy_from_user),		\
	FN(snprintf_btf),		\
	FN(seq_printf_btf),		\
	FN(skb_cgroup_classid),		\
	FN(redirect_neigh),		\
	/* */

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
Loading