Commit e79b3265 authored by Liu Jian's avatar Liu Jian
Browse files

net: add one bpf prog type for network numa relationship

hulk inclusion
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I9GZAQ


CVE: NA

--------------------------------

Add one bpf prog type for network numa relationship.

Signed-off-by: default avatarLiu Jian <liujian56@huawei.com>
parent bfad4b8e
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -81,6 +81,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_LSM, lsm,
BPF_PROG_TYPE(BPF_PROG_TYPE_SCHED, bpf_sched,
	      void *, void *)
#endif /* CONFIG_BPF_SCHED */
#ifdef CONFIG_BPF_NET_GLOBAL_PROG
BPF_PROG_TYPE(BPF_PROG_TYPE_NET_GLOBAL, bpf_gnet,
	      struct bpf_gnet_ctx, struct bpf_gnet_ctx)
#endif

BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)
BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_ARRAY, percpu_array_map_ops)
+53 −0
Original line number Diff line number Diff line
@@ -1474,4 +1474,57 @@ static inline bool bpf_sk_lookup_run_v6(struct net *net, int protocol,
}
#endif /* IS_ENABLED(CONFIG_IPV6) */

#ifdef CONFIG_BPF_NET_GLOBAL_PROG
struct bpf_gnet_ctx_kern {
	struct sock *sk;
};

enum gnet_bpf_attach_type {
	GNET_BPF_ATTACH_TYPE_INVALID = -1,
	GNET_RESERVE0 = 0,
	MAX_GNET_BPF_ATTACH_TYPE
};

static inline enum gnet_bpf_attach_type
to_gnet_bpf_attach_type(enum bpf_attach_type attach_type)
{
	switch (attach_type) {
	GNET_ATYPE(GNET_RESERVE0);
	case BPF_GNET_RESERVE0:
		return GNET_RESERVE0;
	default:
	return GNET_BPF_ATTACH_TYPE_INVALID;
	}
}

struct gnet_bpf {
	struct bpf_prog __rcu *progs[MAX_GNET_BPF_ATTACH_TYPE];
	u32 flags[MAX_GNET_BPF_ATTACH_TYPE];
};

extern struct static_key_false gnet_bpf_enabled_key[MAX_GNET_BPF_ATTACH_TYPE];
#define gnet_bpf_enabled(atype) static_branch_unlikely(&gnet_bpf_enabled_key[atype])
extern struct gnet_bpf gnet_bpf_progs;

int gnet_bpf_prog_attach(const union bpf_attr *attr,
			 enum bpf_prog_type ptype, struct bpf_prog *prog);
int gnet_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);

static inline void run_gnet_bpf(enum gnet_bpf_attach_type atype,
				struct bpf_gnet_ctx_kern *ctx)
{
	struct bpf_prog *prog;

	rcu_read_lock();
	prog = rcu_dereference(gnet_bpf_progs.progs[atype]);
	if (unlikely(!prog))
		goto out;

	bpf_prog_run_pin_on_cpu(prog, ctx);
out:
	rcu_read_unlock();
}

#endif

#endif /* __LINUX_FILTER_H__ */
+6 −0
Original line number Diff line number Diff line
@@ -201,6 +201,7 @@ enum bpf_prog_type {
	BPF_PROG_TYPE_SK_LOOKUP,
#ifndef __GENKSYMS__
	BPF_PROG_TYPE_SCHED,
	BPF_PROG_TYPE_NET_GLOBAL,
#endif
};

@@ -245,6 +246,7 @@ enum bpf_attach_type {
	BPF_XDP,
#ifndef __GENKSYMS__
	BPF_SCHED,
	BPF_GNET_RESERVE0,
#endif
	__MAX_BPF_ATTACH_TYPE
};
@@ -5250,4 +5252,8 @@ enum {
	BTF_F_ZERO	=	(1ULL << 3),
};

struct bpf_gnet_ctx {
	__bpf_md_ptr(struct bpf_sock *, sk);
};

#endif /* _UAPI__LINUX_BPF_H__ */
+16 −0
Original line number Diff line number Diff line
@@ -2107,6 +2107,9 @@ static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
	case BPF_PROG_TYPE_CGROUP_SYSCTL:
	case BPF_PROG_TYPE_SOCK_OPS:
	case BPF_PROG_TYPE_EXT: /* extends any prog */
#ifdef CONFIG_BPF_NET_GLOBAL_PROG
	case BPF_PROG_TYPE_NET_GLOBAL:
#endif
		return true;
	case BPF_PROG_TYPE_CGROUP_SKB:
		/* always unpriv */
@@ -3017,6 +3020,10 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)
		return BPF_PROG_TYPE_SK_LOOKUP;
	case BPF_XDP:
		return BPF_PROG_TYPE_XDP;
#ifdef CONFIG_BPF_NET_GLOBAL_PROG
	case BPF_GNET_RESERVE0:
		return BPF_PROG_TYPE_NET_GLOBAL;
#endif
	default:
		return BPF_PROG_TYPE_UNSPEC;
	}
@@ -3072,6 +3079,11 @@ static int bpf_prog_attach(const union bpf_attr *attr)
	case BPF_PROG_TYPE_SOCK_OPS:
		ret = cgroup_bpf_prog_attach(attr, ptype, prog);
		break;
#ifdef CONFIG_BPF_NET_GLOBAL_PROG
	case BPF_PROG_TYPE_NET_GLOBAL:
		ret = gnet_bpf_prog_attach(attr, ptype, prog);
		break;
#endif
	default:
		ret = -EINVAL;
	}
@@ -3108,6 +3120,10 @@ static int bpf_prog_detach(const union bpf_attr *attr)
	case BPF_PROG_TYPE_CGROUP_SYSCTL:
	case BPF_PROG_TYPE_SOCK_OPS:
		return cgroup_bpf_prog_detach(attr, ptype);
#ifdef CONFIG_BPF_NET_GLOBAL_PROG
	case BPF_PROG_TYPE_NET_GLOBAL:
		return gnet_bpf_prog_detach(attr, ptype);
#endif
	default:
		return -EINVAL;
	}
+6 −0
Original line number Diff line number Diff line
@@ -470,6 +470,12 @@ config ETHTOOL_NETLINK
	  netlink. It provides better extensibility and some new features,
	  e.g. notification messages.

config BPF_NET_GLOBAL_PROG
	bool "Network global bpf prog type"
	depends on NET
	depends on BPF_SYSCALL
	default n

endif   # if NET

# Used by archs to tell that they support BPF JIT compiler plus which flavour.
Loading