eBPF for Windows
bpf.h
Go to the documentation of this file.
1 // Copyright (c) eBPF for Windows contributors
2 // SPDX-License-Identifier: MIT
3 #pragma once
4 
8 
9 #include "ebpf_structs.h"
10 
11 #include <stdint.h>
12 
13 #ifdef _MSC_VER
14 // This file is being included by a user-mode Windows application.
15 #include "ebpf_api.h"
16 #include "ebpf_program_types.h"
17 #define LIBBPF_API
18 #include "libbpf/src/libbpf_common.h"
19 #undef LIBBPF_DEPRECATED
20 #define LIBBPF_DEPRECATED(x)
21 #else
22 // This file is being included by an eBPF program.
23 typedef int32_t s32;
24 typedef uint8_t u8;
25 typedef uint32_t u32;
26 typedef uint64_t u64;
27 #endif
28 
29 #define __SIZEOF_SIZE_T__ 8 /* only x64 is supported */
30 #define __SIZEOF_LONG_LONG__ 8 /* only x64 is supported */
31 
32 typedef int32_t __s32;
33 
34 typedef uint8_t __u8;
35 typedef uint32_t __u32;
36 typedef uint64_t __u64;
37 typedef uint32_t pid_t;
38 
40 {
42 };
43 
45 {
47 };
48 
49 // All types below must be ABI compatible with the Linux bpf() syscalls. This means
50 // that the order, size and alignment of the types must match uapi/linux/bpf.h in
51 // a tagged release of https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/.
52 // Constants must also match.
53 //
54 // Names do not have to match, but try to keep them the same as much as possible.
55 // In case of conflicts prefix them with "sys_" or "SYS_".
56 
58 {
81 };
82 
83 #ifdef _MSC_VER
84 #pragma warning(push)
85 #pragma warning(default : 4820) // reject implicit padding
86 #endif
87 
89 typedef struct
90 {
91  enum bpf_map_type map_type;
92  uint32_t key_size;
93  uint32_t value_size;
94  uint32_t max_entries;
95  uint32_t map_flags;
96  uint32_t inner_map_fd;
98 
99 typedef struct
100 {
101  uint32_t map_fd;
102  uint32_t _pad0;
103  uint64_t key;
104  uint64_t value;
105  uint64_t flags;
107 
108 typedef struct
109 {
110  uint32_t map_fd;
111  uint32_t _pad0;
112  uint64_t key;
114 
115 typedef struct
116 {
117  uint32_t map_fd;
118  uint32_t _pad0;
119  uint64_t key;
120  uint64_t next_key;
122 
123 typedef struct
124 {
125  uint32_t start_id;
126  uint32_t next_id;
128 
129 typedef struct
130 {
131  enum bpf_prog_type prog_type;
132  uint32_t insn_cnt;
133  uint64_t insns;
134  uint64_t license;
135  uint32_t log_level;
136  uint32_t log_size;
137  uint64_t log_buf;
138  uint32_t kern_version;
139  uint32_t prog_flags;
141 
142 typedef struct
143 {
144  uint32_t target_fd;
145  uint32_t attach_bpf_fd;
146  enum bpf_attach_type attach_type;
147  uint32_t attach_flags;
149 
150 typedef struct
151 {
152  uint32_t prog_fd;
153  uint32_t retval;
154  uint32_t data_size_in;
155  uint32_t data_size_out;
156  uint64_t data_in;
157  uint64_t data_out;
158  uint32_t repeat;
159  uint32_t duration;
160  uint32_t ctx_size_in;
161  uint32_t ctx_size_out;
162  uint64_t ctx_in;
163  uint64_t ctx_out;
164  uint32_t flags;
165  uint32_t cpu;
166  uint32_t batch_size;
167  uint32_t _pad0;
169 
170 typedef struct
171 {
172  uint64_t pathname;
173  uint32_t bpf_fd;
174  uint32_t flags;
176 
178 typedef struct
179 {
180  uint32_t bpf_fd;
185  uint32_t info_len;
186  uint64_t info;
188 
190 typedef struct
191 {
192  uint32_t link_fd;
194 
196 typedef struct
197 {
198  uint32_t prog_fd;
199  uint32_t map_fd;
200  uint32_t flags;
202 
204 union bpf_attr
205 {
206  // BPF_MAP_CREATE
208 
209  // BPF_MAP_LOOKUP_ELEM
210  // BPF_MAP_UPDATE_ELEM
213 
214  // BPF_MAP_GET_NEXT_KEY
216 
217  // BPF_MAP_DELETE_ELEM
219 
220  // BPF_PROG_LOAD
222 
223  // BPF_PROG_ATTACH
224  // BPF_PROG_DETACH
226 
227  // BPF_OBJ_PIN
228  // BPF_OBJ_GET
230 
231  // BPF_PROG_GET_NEXT_ID
232  // BPF_MAP_GET_NEXT_ID
233  // BPF_LINK_GET_NEXT_ID
236 
237  // BPF_MAP_GET_FD_BY_ID
238  uint32_t map_id;
239 
240  // BPF_PROG_GET_FD_BY_ID
241  uint32_t prog_id;
242 
243  // BPF_LINK_GET_FD_BY_ID
244  uint32_t link_id;
245 
246  // BPF_OBJ_GET_INFO_BY_FD
248 
249  // BPF_LINK_DETACH
251 
252  // BPF_PROG_BIND_MAP
254 
255  // BPF_PROG_TEST_RUN
257 };
258 
259 #ifdef _MSC_VER
260 #pragma warning(pop)
261 #endif
262 
263 int
264 bpf(int cmd, union bpf_attr* attr, unsigned int size);
This file contains eBPF definitions common to eBPF programs, core execution engine as well as eBPF AP...
bpf_prog_type
Definition: ebpf_structs.h:171
bpf_attach_type
Definition: ebpf_structs.h:261
bpf_map_type
Definition: ebpf_structs.h:18
int32_t __s32
Definition: bpf.h:32
uint32_t __u32
Definition: bpf.h:35
uint64_t u64
Definition: bpf.h:26
bpf_func_id
Definition: bpf.h:40
@ BPF_FUNC_ID_UNKNOWN
Definition: bpf.h:41
uint8_t __u8
Definition: bpf.h:34
uint32_t pid_t
Definition: bpf.h:37
uint8_t u8
Definition: bpf.h:24
bpf_cmd_id
Definition: bpf.h:58
@ BPF_MAP_LOOKUP_AND_DELETE_ELEM
Definition: bpf.h:75
@ BPF_PROG_RUN
Definition: bpf.h:80
@ BPF_MAP_CREATE
Definition: bpf.h:59
@ BPF_OBJ_GET
Definition: bpf.h:66
@ BPF_PROG_LOAD
Definition: bpf.h:64
@ BPF_PROG_DETACH
Definition: bpf.h:68
@ BPF_PROG_GET_NEXT_ID
Definition: bpf.h:70
@ BPF_PROG_ATTACH
Definition: bpf.h:67
@ BPF_OBJ_PIN
Definition: bpf.h:65
@ BPF_MAP_GET_FD_BY_ID
Definition: bpf.h:73
@ BPF_LINK_GET_NEXT_ID
Definition: bpf.h:77
@ BPF_LINK_DETACH
Definition: bpf.h:78
@ BPF_PROG_TEST_RUN
Definition: bpf.h:69
@ BPF_PROG_BIND_MAP
Definition: bpf.h:79
@ BPF_MAP_GET_NEXT_KEY
Definition: bpf.h:63
@ BPF_LINK_GET_FD_BY_ID
Definition: bpf.h:76
@ BPF_PROG_GET_FD_BY_ID
Definition: bpf.h:72
@ BPF_OBJ_GET_INFO_BY_FD
Definition: bpf.h:74
@ BPF_MAP_GET_NEXT_ID
Definition: bpf.h:71
@ BPF_MAP_UPDATE_ELEM
Definition: bpf.h:61
@ BPF_MAP_LOOKUP_ELEM
Definition: bpf.h:60
@ BPF_MAP_DELETE_ELEM
Definition: bpf.h:62
uint64_t __u64
Definition: bpf.h:36
int bpf(int cmd, union bpf_attr *attr, unsigned int size)
int32_t s32
Definition: bpf.h:23
bpf_stats_type
Definition: bpf.h:45
@ BPF_STATS_TYPE_UNKNOWN
Definition: bpf.h:46
uint32_t u32
Definition: bpf.h:25
Attributes used by BPF_MAP_CREATE.
Definition: bpf.h:90
uint32_t value_size
Size in bytes of values.
Definition: bpf.h:93
uint32_t max_entries
Maximum number of entries in the map.
Definition: bpf.h:94
uint32_t key_size
Size in bytes of keys.
Definition: bpf.h:92
uint32_t map_flags
Not supported, must be zero.
Definition: bpf.h:95
uint32_t inner_map_fd
File descriptor of inner map.
Definition: bpf.h:96
Definition: bpf.h:109
uint32_t map_fd
File descriptor of map.
Definition: bpf.h:110
uint64_t key
Pointer to key to delete.
Definition: bpf.h:112
uint32_t _pad0
Definition: bpf.h:111
Definition: bpf.h:100
uint32_t map_fd
File descriptor of map.
Definition: bpf.h:101
uint64_t key
Pointer to key to look up.
Definition: bpf.h:103
uint64_t flags
Not supported, must be zero.
Definition: bpf.h:105
uint32_t _pad0
Definition: bpf.h:102
uint64_t value
Pointer to value.
Definition: bpf.h:104
Definition: bpf.h:124
uint32_t start_id
ID to look for an ID after. The start_id need not exist.
Definition: bpf.h:125
uint32_t next_id
On return, contains the next ID.
Definition: bpf.h:126
Definition: bpf.h:116
uint64_t key
Pointer to key to look up.
Definition: bpf.h:119
uint32_t _pad0
Definition: bpf.h:118
uint32_t map_fd
File descriptor of map.
Definition: bpf.h:117
uint64_t next_key
Pointer to next key.
Definition: bpf.h:120
Attributes used by BPF_OBJ_GET_INFO_BY_FD.
Definition: bpf.h:179
uint32_t info_len
On input, contains the maximum number of bytes to write into the info. On output, contains the actual...
Definition: bpf.h:185
uint32_t bpf_fd
File descriptor referring to an eBPF object.
Definition: bpf.h:180
uint64_t info
Pointer to memory in which to write the info obtained.
Definition: bpf.h:186
Definition: bpf.h:171
uint32_t flags
Not supported, must be zero.
Definition: bpf.h:174
uint32_t bpf_fd
File descriptor referring to the program or map.
Definition: bpf.h:173
uint64_t pathname
Path name for pinning.
Definition: bpf.h:172
Definition: bpf.h:143
uint32_t attach_flags
Flags affecting the attach operation.
Definition: bpf.h:147
uint32_t target_fd
eBPF target to attach/detach to/from.
Definition: bpf.h:144
uint32_t attach_bpf_fd
File descriptor of program to attach to.
Definition: bpf.h:145
Attributes used by BPF_PROG_BIND_MAP.
Definition: bpf.h:197
uint32_t map_fd
File descriptor of map to bind.
Definition: bpf.h:199
uint32_t flags
Flags affecting the bind operation.
Definition: bpf.h:200
uint32_t prog_fd
File descriptor of program to bind map to.
Definition: bpf.h:198
Definition: bpf.h:130
uint32_t insn_cnt
Number of instructions in the array.
Definition: bpf.h:132
uint64_t log_buf
Pointer to a buffer in which log info can be written.
Definition: bpf.h:137
uint64_t license
Optional pointer to a string specifying the license (currently ignored on Windows).
Definition: bpf.h:134
uint32_t log_level
Logging level (currently ignored on Windows).
Definition: bpf.h:135
uint32_t kern_version
Kernel version (currently ignored on Windows).
Definition: bpf.h:138
uint32_t log_size
Size in bytes of the log buffer.
Definition: bpf.h:136
uint32_t prog_flags
Not supported, must be zero.
Definition: bpf.h:139
uint64_t insns
Array of instructions.
Definition: bpf.h:133
Definition: bpf.h:151
uint32_t repeat
Number of times to repeat the program.
Definition: bpf.h:158
uint64_t ctx_in
Pointer to input context.
Definition: bpf.h:162
uint32_t data_size_in
Size in bytes of input data.
Definition: bpf.h:154
uint64_t ctx_out
Pointer to output context.
Definition: bpf.h:163
uint32_t flags
Flags (currently 0).
Definition: bpf.h:164
uint32_t duration
Duration in milliseconds to run the program.
Definition: bpf.h:159
uint32_t retval
On return, contains the return value of the program.
Definition: bpf.h:153
uint32_t ctx_size_in
Size in bytes of input context.
Definition: bpf.h:160
uint64_t data_in
Pointer to input data.
Definition: bpf.h:156
uint32_t batch_size
Number of times to run the program in a batch.
Definition: bpf.h:166
uint64_t data_out
Pointer to output data.
Definition: bpf.h:157
uint32_t prog_fd
File descriptor of program to run.
Definition: bpf.h:152
uint32_t ctx_size_out
Size in bytes of output context.
Definition: bpf.h:161
uint32_t _pad0
Definition: bpf.h:167
uint32_t data_size_out
Size in bytes of output data.
Definition: bpf.h:155
uint32_t cpu
CPU to run the program on.
Definition: bpf.h:165
Parameters used by the bpf() API.
Definition: bpf.h:205
sys_bpf_prog_load_attr_t prog_load
Attributes used by BPF_PROG_LOAD.
Definition: bpf.h:221
uint32_t map_id
ID of map for BPF_MAP_GET_FD_BY_ID to find.
Definition: bpf.h:238
sys_bpf_obj_pin_attr_t obj_pin
Definition: bpf.h:229
sys_bpf_map_lookup_attr_t map_update
Attributes used by BPF_MAP_LOOKUP_ELEM, BPF_MAP_UPDATE_ELEM and.
Definition: bpf.h:212
sys_bpf_obj_pin_attr_t obj_get
Attributes used by BPF_OBJ_PIN and BPF_OBJ_GET.
Definition: bpf.h:229
uint32_t prog_id
ID of program for BPF_PROG_GET_FD_BY_ID to find.
Definition: bpf.h:241
sys_bpf_map_next_id_attr_t prog_get_next_id
Definition: bpf.h:234
sys_bpf_prog_bind_map_attr_t prog_bind_map
Attributes used by BPF_PROG_BIND_MAP.
Definition: bpf.h:253
sys_bpf_obj_info_attr_t info
Attributes used by BPF_OBJ_GET_INFO_BY_FD.
Definition: bpf.h:247
uint32_t link_id
ID of link for BPF_LINK_GET_FD_BY_ID to find.
Definition: bpf.h:244
sys_bpf_map_lookup_attr_t map_lookup
Definition: bpf.h:211
sys_bpf_map_next_id_attr_t map_get_next_id
Definition: bpf.h:234
sys_bpf_map_next_id_attr_t link_get_next_id
Attributes used by BPF_PROG_GET_NEXT_ID, BPF_MAP_GET_NEXT_ID, and BPF_LINK_GET_NEXT_ID.
Definition: bpf.h:235
sys_bpf_map_delete_attr_t map_delete
Attributes used by BPF_MAP_DELETE_ELEM.
Definition: bpf.h:218
sys_bpf_prog_attach_attr_t prog_attach
Definition: bpf.h:225
sys_bpf_link_detach_attr_t link_detach
Attributes used by BPF_LINK_DETACH.
Definition: bpf.h:250
sys_bpf_prog_attach_attr_t prog_detach
Attributes used by BPF_PROG_ATTACH/DETACH.
Definition: bpf.h:225
sys_bpf_map_next_key_attr_t map_get_next_key
Attributes used by BPF_MAP_GET_NEXT_KEY.
Definition: bpf.h:215
sys_bpf_map_create_attr_t map_create
Attributes used by BPF_MAP_CREATE.
Definition: bpf.h:207
sys_bpf_prog_run_attr_t test
Attributes used by BPF_PROG_TEST_RUN.
Definition: bpf.h:256