30eefb34cc3aef92c577a01ee89af400c48af0c2
[dpdk.git] / drivers / net / tap / tap_bpf.h
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 #ifdef HAVE_BPF_PROG_LOAD
11 #include <linux/bpf.h>
12 #else
13 /* BPF_MAP_UPDATE_ELEM command flags */
14 #define BPF_ANY 0 /* create a new element or update an existing */
15
16 /* BPF architecture instruction struct */
17 struct bpf_insn {
18         __u8    code;
19         __u8    dst_reg:4;
20         __u8    src_reg:4;
21         __s16   off;
22         __s32   imm; /* immediate value */
23 };
24
25 /* BPF program types */
26 enum bpf_prog_type {
27         BPF_PROG_TYPE_UNSPEC,
28         BPF_PROG_TYPE_SOCKET_FILTER,
29         BPF_PROG_TYPE_KPROBE,
30         BPF_PROG_TYPE_SCHED_CLS,
31         BPF_PROG_TYPE_SCHED_ACT,
32 };
33
34 /* BPF commands types */
35 enum bpf_cmd {
36         BPF_MAP_CREATE,
37         BPF_MAP_LOOKUP_ELEM,
38         BPF_MAP_UPDATE_ELEM,
39         BPF_MAP_DELETE_ELEM,
40         BPF_MAP_GET_NEXT_KEY,
41         BPF_PROG_LOAD,
42 };
43
44 /* BPF maps types */
45 enum bpf_map_type {
46         BPF_MAP_TYPE_UNSPEC,
47         BPF_MAP_TYPE_HASH,
48 };
49
50 /* union of anonymous structs used with TAP BPF commands */
51 union bpf_attr {
52         /* BPF_MAP_CREATE command */
53         struct {
54                 __u32   map_type;
55                 __u32   key_size;
56                 __u32   value_size;
57                 __u32   max_entries;
58                 __u32   map_flags;
59                 __u32   inner_map_fd;
60         };
61
62         /* BPF_MAP_UPDATE_ELEM, BPF_MAP_DELETE_ELEM commands */
63         struct {
64                 __u32           map_fd;
65                 __aligned_u64   key;
66                 union {
67                         __aligned_u64 value;
68                         __aligned_u64 next_key;
69                 };
70                 __u64           flags;
71         };
72
73         /* BPF_PROG_LOAD command */
74         struct {
75                 __u32           prog_type;
76                 __u32           insn_cnt;
77                 __aligned_u64   insns;
78                 __aligned_u64   license;
79                 __u32           log_level;
80                 __u32           log_size;
81                 __aligned_u64   log_buf;
82                 __u32           kern_version;
83                 __u32           prog_flags;
84         };
85 } __attribute__((aligned(8)));
86 #endif
87
88 #ifndef __NR_bpf
89 # if defined(__i386__)
90 #  define __NR_bpf 357
91 # elif defined(__x86_64__)
92 #  define __NR_bpf 321
93 # elif defined(__aarch64__)
94 #  define __NR_bpf 280
95 # elif defined(__sparc__)
96 #  define __NR_bpf 349
97 # elif defined(__s390__)
98 #  define __NR_bpf 351
99 # else
100 #  error __NR_bpf not defined
101 # endif
102 #endif
103
104 enum {
105         BPF_MAP_ID_KEY,
106         BPF_MAP_ID_SIMPLE,
107 };
108
109 static int bpf_load(enum bpf_prog_type type, const struct bpf_insn *insns,
110                 size_t insns_cnt, const char *license);
111
112 #endif /* __TAP_BPF_H__ */