1755 lines · 55.1 KB · C
RawDownload
/* SPDX-License-Identifier: GPL-2.0-only *//* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com */#ifndef _LINUX_BPF_H#define _LINUX_BPF_H 1 #include <uapi/linux/bpf.h> #include <linux/workqueue.h>#include <linux/file.h>#include <linux/percpu.h>#include <linux/err.h>#include <linux/rbtree_latch.h>#include <linux/numa.h>#include <linux/mm_types.h>#include <linux/wait.h>#include <linux/u64_stats_sync.h>#include <linux/refcount.h>#include <linux/mutex.h>#include <linux/module.h>#include <linux/kallsyms.h>#include <linux/capability.h> struct bpf_verifier_env;struct bpf_verifier_log;struct perf_event;struct bpf_prog;struct bpf_prog_aux;struct bpf_map;struct sock;struct seq_file;struct btf;struct btf_type;struct exception_table_entry;struct seq_operations; extern struct idr btf_idr;extern spinlock_t btf_idr_lock; /* map is generic key/value storage optionally accesible by eBPF programs */struct bpf_map_ops {	/* funcs callable from userspace (via syscall) */	int (*map_alloc_check)(union bpf_attr *attr);	struct bpf_map *(*map_alloc)(union bpf_attr *attr);	void (*map_release)(struct bpf_map *map, struct file *map_file);	void (*map_free)(struct bpf_map *map);	int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);	void (*map_release_uref)(struct bpf_map *map);	void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);	int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,				union bpf_attr __user *uattr);	int (*map_lookup_and_delete_batch)(struct bpf_map *map,					   const union bpf_attr *attr,					   union bpf_attr __user *uattr);	int (*map_update_batch)(struct bpf_map *map, const union bpf_attr *attr,				union bpf_attr __user *uattr);	int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,				union bpf_attr __user *uattr); 	/* funcs callable from userspace and from eBPF programs */	void *(*map_lookup_elem)(struct bpf_map *map, void *key);	int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);	int (*map_delete_elem)(struct bpf_map *map, void *key);	int (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);	int (*map_pop_elem)(struct bpf_map *map, void *value);	int (*map_peek_elem)(struct bpf_map *map, void *value); 	/* funcs called by prog_array and perf_event_array map */	void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,				int fd);	void (*map_fd_put_ptr)(void *ptr);	u32 (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);	u32 (*map_fd_sys_lookup_elem)(void *ptr);	void (*map_seq_show_elem)(struct bpf_map *map, void *key,				  struct seq_file *m);	int (*map_check_btf)(const struct bpf_map *map,			     const struct btf *btf,			     const struct btf_type *key_type,			     const struct btf_type *value_type); 	/* Prog poke tracking helpers. */	int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);	void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);	void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,			     struct bpf_prog *new); 	/* Direct value access helpers. */	int (*map_direct_value_addr)(const struct bpf_map *map,				     u64 *imm, u32 off);	int (*map_direct_value_meta)(const struct bpf_map *map,				     u64 imm, u32 *off);	int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);	__poll_t (*map_poll)(struct bpf_map *map, struct file *filp,			     struct poll_table_struct *pts);}; struct bpf_map_memory {	u32 pages;	struct user_struct *user;}; struct bpf_map {	/* The first two cachelines with read-mostly members of which some	 * are also accessed in fast-path (e.g. ops, max_entries).	 */	const struct bpf_map_ops *ops ____cacheline_aligned;	struct bpf_map *inner_map_meta;#ifdef CONFIG_SECURITY	void *security;#endif	enum bpf_map_type map_type;	u32 key_size;	u32 value_size;	u32 max_entries;	u32 map_flags;	int spin_lock_off; /* >=0 valid offset, <0 error */	u32 id;	int numa_node;	u32 btf_key_type_id;	u32 btf_value_type_id;	struct btf *btf;	struct bpf_map_memory memory;	char name[BPF_OBJ_NAME_LEN];	u32 btf_vmlinux_value_type_id;	bool bypass_spec_v1;	bool frozen; /* write-once; write-protected by freeze_mutex */	/* 22 bytes hole */ 	/* The 3rd and 4th cacheline with misc members to avoid false sharing	 * particularly with refcounting.	 */	atomic64_t refcnt ____cacheline_aligned;	atomic64_t usercnt;	struct work_struct work;	struct mutex freeze_mutex;	u64 writecnt; /* writable mmap cnt; protected by freeze_mutex */}; static inline bool map_value_has_spin_lock(const struct bpf_map *map){	return map->spin_lock_off >= 0;} static inline void check_and_init_map_lock(struct bpf_map *map, void *dst){	if (likely(!map_value_has_spin_lock(map)))		return;	*(struct bpf_spin_lock *)(dst + map->spin_lock_off) =		(struct bpf_spin_lock){};} /* copy everything but bpf_spin_lock */static inline void copy_map_value(struct bpf_map *map, void *dst, void *src){	if (unlikely(map_value_has_spin_lock(map))) {		u32 off = map->spin_lock_off; 		memcpy(dst, src, off);		memcpy(dst + off + sizeof(struct bpf_spin_lock),		       src + off + sizeof(struct bpf_spin_lock),		       map->value_size - off - sizeof(struct bpf_spin_lock));	} else {		memcpy(dst, src, map->value_size);	}}void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,			   bool lock_src);int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size); struct bpf_offload_dev;struct bpf_offloaded_map; struct bpf_map_dev_ops {	int (*map_get_next_key)(struct bpf_offloaded_map *map,				void *key, void *next_key);	int (*map_lookup_elem)(struct bpf_offloaded_map *map,			       void *key, void *value);	int (*map_update_elem)(struct bpf_offloaded_map *map,			       void *key, void *value, u64 flags);	int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);}; struct bpf_offloaded_map {	struct bpf_map map;	struct net_device *netdev;	const struct bpf_map_dev_ops *dev_ops;	void *dev_priv;	struct list_head offloads;}; static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map){	return container_of(map, struct bpf_offloaded_map, map);} static inline bool bpf_map_offload_neutral(const struct bpf_map *map){	return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;} static inline bool bpf_map_support_seq_show(const struct bpf_map *map){	return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&		map->ops->map_seq_show_elem;} int map_check_no_btf(const struct bpf_map *map,		     const struct btf *btf,		     const struct btf_type *key_type,		     const struct btf_type *value_type); extern const struct bpf_map_ops bpf_map_offload_ops; /* function argument constraints */enum bpf_arg_type {	ARG_DONTCARE = 0,	/* unused argument in helper function */ 	/* the following constraints used to prototype	 * bpf_map_lookup/update/delete_elem() functions	 */	ARG_CONST_MAP_PTR,	/* const argument used as pointer to bpf_map */	ARG_PTR_TO_MAP_KEY,	/* pointer to stack used as map key */	ARG_PTR_TO_MAP_VALUE,	/* pointer to stack used as map value */	ARG_PTR_TO_UNINIT_MAP_VALUE,	/* pointer to valid memory used to store a map value */	ARG_PTR_TO_MAP_VALUE_OR_NULL,	/* pointer to stack used as map value or NULL */ 	/* the following constraints used to prototype bpf_memcmp() and other	 * functions that access data on eBPF program stack	 */	ARG_PTR_TO_MEM,		/* pointer to valid memory (stack, packet, map value) */	ARG_PTR_TO_MEM_OR_NULL, /* pointer to valid memory or NULL */	ARG_PTR_TO_UNINIT_MEM,	/* pointer to memory does not need to be initialized,				 * helper function must fill all bytes or clear				 * them in error case.				 */ 	ARG_CONST_SIZE,		/* number of bytes accessed from memory */	ARG_CONST_SIZE_OR_ZERO,	/* number of bytes accessed from memory or 0 */ 	ARG_PTR_TO_CTX,		/* pointer to context */	ARG_PTR_TO_CTX_OR_NULL,	/* pointer to context or NULL */	ARG_ANYTHING,		/* any (initialized) argument is ok */	ARG_PTR_TO_SPIN_LOCK,	/* pointer to bpf_spin_lock */	ARG_PTR_TO_SOCK_COMMON,	/* pointer to sock_common */	ARG_PTR_TO_INT,		/* pointer to int */	ARG_PTR_TO_LONG,	/* pointer to long */	ARG_PTR_TO_SOCKET,	/* pointer to bpf_sock (fullsock) */	ARG_PTR_TO_BTF_ID,	/* pointer to in-kernel struct */	ARG_PTR_TO_ALLOC_MEM,	/* pointer to dynamically allocated memory */	ARG_PTR_TO_ALLOC_MEM_OR_NULL,	/* pointer to dynamically allocated memory or NULL */	ARG_CONST_ALLOC_SIZE_OR_ZERO,	/* number of allocated bytes requested */}; /* type of values returned from helper functions */enum bpf_return_type {	RET_INTEGER,			/* function returns integer */	RET_VOID,			/* function doesn't return anything */	RET_PTR_TO_MAP_VALUE,		/* returns a pointer to map elem value */	RET_PTR_TO_MAP_VALUE_OR_NULL,	/* returns a pointer to map elem value or NULL */	RET_PTR_TO_SOCKET_OR_NULL,	/* returns a pointer to a socket or NULL */	RET_PTR_TO_TCP_SOCK_OR_NULL,	/* returns a pointer to a tcp_sock or NULL */	RET_PTR_TO_SOCK_COMMON_OR_NULL,	/* returns a pointer to a sock_common or NULL */	RET_PTR_TO_ALLOC_MEM_OR_NULL,	/* returns a pointer to dynamically allocated memory or NULL */}; /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL * instructions after verifying */struct bpf_func_proto {	u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);	bool gpl_only;	bool pkt_access;	enum bpf_return_type ret_type;	union {		struct {			enum bpf_arg_type arg1_type;			enum bpf_arg_type arg2_type;			enum bpf_arg_type arg3_type;			enum bpf_arg_type arg4_type;			enum bpf_arg_type arg5_type;		};		enum bpf_arg_type arg_type[5];	};	int *btf_id; /* BTF ids of arguments */}; /* bpf_context is intentionally undefined structure. Pointer to bpf_context is * the first argument to eBPF programs. * For socket filters: 'struct bpf_context *' == 'struct sk_buff *' */struct bpf_context; enum bpf_access_type {	BPF_READ = 1,	BPF_WRITE = 2}; /* types of values stored in eBPF registers *//* Pointer types represent: * pointer * pointer + imm * pointer + (u16) var * pointer + (u16) var + imm * if (range > 0) then [ptr, ptr + range - off) is safe to access * if (id > 0) means that some 'var' was added * if (off > 0) means that 'imm' was added */enum bpf_reg_type {	NOT_INIT = 0,		 /* nothing was written into register */	SCALAR_VALUE,		 /* reg doesn't contain a valid pointer */	PTR_TO_CTX,		 /* reg points to bpf_context */	CONST_PTR_TO_MAP,	 /* reg points to struct bpf_map */	PTR_TO_MAP_VALUE,	 /* reg points to map element value */	PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */	PTR_TO_STACK,		 /* reg == frame_pointer + offset */	PTR_TO_PACKET_META,	 /* skb->data - meta_len */	PTR_TO_PACKET,		 /* reg points to skb->data */	PTR_TO_PACKET_END,	 /* skb->data + headlen */	PTR_TO_FLOW_KEYS,	 /* reg points to bpf_flow_keys */	PTR_TO_SOCKET,		 /* reg points to struct bpf_sock */	PTR_TO_SOCKET_OR_NULL,	 /* reg points to struct bpf_sock or NULL */	PTR_TO_SOCK_COMMON,	 /* reg points to sock_common */	PTR_TO_SOCK_COMMON_OR_NULL, /* reg points to sock_common or NULL */	PTR_TO_TCP_SOCK,	 /* reg points to struct tcp_sock */	PTR_TO_TCP_SOCK_OR_NULL, /* reg points to struct tcp_sock or NULL */	PTR_TO_TP_BUFFER,	 /* reg points to a writable raw tp's buffer */	PTR_TO_XDP_SOCK,	 /* reg points to struct xdp_sock */	PTR_TO_BTF_ID,		 /* reg points to kernel struct */	PTR_TO_BTF_ID_OR_NULL,	 /* reg points to kernel struct or NULL */	PTR_TO_MEM,		 /* reg points to valid memory region */	PTR_TO_MEM_OR_NULL,	 /* reg points to valid memory region or NULL */}; /* The information passed from prog-specific *_is_valid_access * back to the verifier. */struct bpf_insn_access_aux {	enum bpf_reg_type reg_type;	union {		int ctx_field_size;		u32 btf_id;	};	struct bpf_verifier_log *log; /* for verbose logs */}; static inline voidbpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size){	aux->ctx_field_size = size;} struct bpf_prog_ops {	int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,			union bpf_attr __user *uattr);}; struct bpf_verifier_ops {	/* return eBPF function prototype for verification */	const struct bpf_func_proto *	(*get_func_proto)(enum bpf_func_id func_id,			  const struct bpf_prog *prog); 	/* return true if 'size' wide access at offset 'off' within bpf_context	 * with 'type' (read or write) is allowed	 */	bool (*is_valid_access)(int off, int size, enum bpf_access_type type,				const struct bpf_prog *prog,				struct bpf_insn_access_aux *info);	int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,			    const struct bpf_prog *prog);	int (*gen_ld_abs)(const struct bpf_insn *orig,			  struct bpf_insn *insn_buf);	u32 (*convert_ctx_access)(enum bpf_access_type type,				  const struct bpf_insn *src,				  struct bpf_insn *dst,				  struct bpf_prog *prog, u32 *target_size);	int (*btf_struct_access)(struct bpf_verifier_log *log,				 const struct btf_type *t, int off, int size,				 enum bpf_access_type atype,				 u32 *next_btf_id);}; struct bpf_prog_offload_ops {	/* verifier basic callbacks */	int (*insn_hook)(struct bpf_verifier_env *env,			 int insn_idx, int prev_insn_idx);	int (*finalize)(struct bpf_verifier_env *env);	/* verifier optimization callbacks (called after .finalize) */	int (*replace_insn)(struct bpf_verifier_env *env, u32 off,			    struct bpf_insn *insn);	int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);	/* program management callbacks */	int (*prepare)(struct bpf_prog *prog);	int (*translate)(struct bpf_prog *prog);	void (*destroy)(struct bpf_prog *prog);}; struct bpf_prog_offload {	struct bpf_prog		*prog;	struct net_device	*netdev;	struct bpf_offload_dev	*offdev;	void			*dev_priv;	struct list_head	offloads;	bool			dev_state;	bool			opt_failed;	void			*jited_image;	u32			jited_len;}; enum bpf_cgroup_storage_type {	BPF_CGROUP_STORAGE_SHARED,	BPF_CGROUP_STORAGE_PERCPU,	__BPF_CGROUP_STORAGE_MAX}; #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX /* The longest tracepoint has 12 args. * See include/trace/bpf_probe.h */#define MAX_BPF_FUNC_ARGS 12 struct bpf_prog_stats {	u64 cnt;	u64 nsecs;	struct u64_stats_sync syncp;} __aligned(2 * sizeof(u64)); struct btf_func_model {	u8 ret_size;	u8 nr_args;	u8 arg_size[MAX_BPF_FUNC_ARGS];}; /* Restore arguments before returning from trampoline to let original function * continue executing. This flag is used for fentry progs when there are no * fexit progs. */#define BPF_TRAMP_F_RESTORE_REGS	BIT(0)/* Call original function after fentry progs, but before fexit progs. * Makes sense for fentry/fexit, normal calls and indirect calls. */#define BPF_TRAMP_F_CALL_ORIG		BIT(1)/* Skip current frame and return to parent.  Makes sense for fentry/fexit * programs only. Should not be used with normal calls and indirect calls. */#define BPF_TRAMP_F_SKIP_FRAME		BIT(2) /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 * bytes on x86.  Pick a number to fit into BPF_IMAGE_SIZE / 2 */#define BPF_MAX_TRAMP_PROGS 40 struct bpf_tramp_progs {	struct bpf_prog *progs[BPF_MAX_TRAMP_PROGS];	int nr_progs;}; /* Different use cases for BPF trampoline: * 1. replace nop at the function entry (kprobe equivalent) *    flags = BPF_TRAMP_F_RESTORE_REGS *    fentry = a set of programs to run before returning from trampoline * * 2. replace nop at the function entry (kprobe + kretprobe equivalent) *    flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME *    orig_call = fentry_ip + MCOUNT_INSN_SIZE *    fentry = a set of program to run before calling original function *    fexit = a set of program to run after original function * * 3. replace direct call instruction anywhere in the function body *    or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid) *    With flags = 0 *      fentry = a set of programs to run before returning from trampoline *    With flags = BPF_TRAMP_F_CALL_ORIG *      orig_call = original callback addr or direct function addr *      fentry = a set of program to run before calling original function *      fexit = a set of program to run after original function */int arch_prepare_bpf_trampoline(void *image, void *image_end,				const struct btf_func_model *m, u32 flags,				struct bpf_tramp_progs *tprogs,				void *orig_call);/* these two functions are called from generated trampoline */u64 notrace __bpf_prog_enter(void);void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start); struct bpf_ksym {	unsigned long		 start;	unsigned long		 end;	char			 name[KSYM_NAME_LEN];	struct list_head	 lnode;	struct latch_tree_node	 tnode;	bool			 prog;}; enum bpf_tramp_prog_type {	BPF_TRAMP_FENTRY,	BPF_TRAMP_FEXIT,	BPF_TRAMP_MODIFY_RETURN,

Lines 1–500 of 1755.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t