xref: /f-stack/dpdk/drivers/net/tap/tap_bpf.h (revision 2d9fd380)
1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2  * Copyright 2017 Mellanox Technologies, Ltd
3  */
4 
5 #ifndef __TAP_BPF_H__
6 #define __TAP_BPF_H__
7 
8 #include <tap_autoconf.h>
9 
10 /* Do not #include <linux/bpf.h> since eBPF must compile on different
11  * distros which may include partial definitions for eBPF (while the
12  * kernel itself may support eBPF). Instead define here all that is needed
13  */
14 
15 /* BPF_MAP_UPDATE_ELEM command flags */
16 #define	BPF_ANY	0 /* create a new element or update an existing */
17 
18 /* BPF architecture instruction struct */
19 struct bpf_insn {
20 	__u8	code;
21 	__u8	dst_reg:4;
22 	__u8	src_reg:4;
23 	__s16	off;
24 	__s32	imm; /* immediate value */
25 };
26 
27 /* BPF program types */
28 enum bpf_prog_type {
29 	BPF_PROG_TYPE_UNSPEC,
30 	BPF_PROG_TYPE_SOCKET_FILTER,
31 	BPF_PROG_TYPE_KPROBE,
32 	BPF_PROG_TYPE_SCHED_CLS,
33 	BPF_PROG_TYPE_SCHED_ACT,
34 };
35 
36 /* BPF commands types */
37 enum bpf_cmd {
38 	BPF_MAP_CREATE,
39 	BPF_MAP_LOOKUP_ELEM,
40 	BPF_MAP_UPDATE_ELEM,
41 	BPF_MAP_DELETE_ELEM,
42 	BPF_MAP_GET_NEXT_KEY,
43 	BPF_PROG_LOAD,
44 };
45 
46 /* BPF maps types */
47 enum bpf_map_type {
48 	BPF_MAP_TYPE_UNSPEC,
49 	BPF_MAP_TYPE_HASH,
50 };
51 
52 /* union of anonymous structs used with TAP BPF commands */
53 union bpf_attr {
54 	/* BPF_MAP_CREATE command */
55 	struct {
56 		__u32	map_type;
57 		__u32	key_size;
58 		__u32	value_size;
59 		__u32	max_entries;
60 		__u32	map_flags;
61 		__u32	inner_map_fd;
62 	};
63 
64 	/* BPF_MAP_UPDATE_ELEM, BPF_MAP_DELETE_ELEM commands */
65 	struct {
66 		__u32		map_fd;
67 		__aligned_u64	key;
68 		union {
69 			__aligned_u64 value;
70 			__aligned_u64 next_key;
71 		};
72 		__u64		flags;
73 	};
74 
75 	/* BPF_PROG_LOAD command */
76 	struct {
77 		__u32		prog_type;
78 		__u32		insn_cnt;
79 		__aligned_u64	insns;
80 		__aligned_u64	license;
81 		__u32		log_level;
82 		__u32		log_size;
83 		__aligned_u64	log_buf;
84 		__u32		kern_version;
85 		__u32		prog_flags;
86 	};
87 } __rte_aligned(8);
88 
89 #ifndef __NR_bpf
90 # if defined(__i386__)
91 #  define __NR_bpf 357
92 # elif defined(__x86_64__)
93 #  define __NR_bpf 321
94 # elif defined(__arm__)
95 #  define __NR_bpf 386
96 # elif defined(__aarch64__)
97 #  define __NR_bpf 280
98 # elif defined(__sparc__)
99 #  define __NR_bpf 349
100 # elif defined(__s390__)
101 #  define __NR_bpf 351
102 # elif defined(__powerpc__)
103 #  define __NR_bpf 361
104 # else
105 #  error __NR_bpf not defined
106 # endif
107 #endif
108 
109 enum {
110 	BPF_MAP_ID_KEY,
111 	BPF_MAP_ID_SIMPLE,
112 };
113 
114 static int bpf_load(enum bpf_prog_type type, const struct bpf_insn *insns,
115 		size_t insns_cnt, const char *license);
116 
117 #endif /* __TAP_BPF_H__ */
118