2015-03-13 19:16:32 +00:00
|
|
|
.\" Copyright (C) 2015 Alexei Starovoitov <ast@kernel.org>
|
2015-07-22 14:45:08 +00:00
|
|
|
.\" and Copyright (C) 2015 Michael Kerrisk <mtk.manpages@gmail.com>
|
2015-03-13 19:16:32 +00:00
|
|
|
.\"
|
|
|
|
.\" %%%LICENSE_START(VERBATIM)
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
|
|
|
.\"
|
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
|
|
|
.\"
|
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\" %%%LICENSE_END
|
|
|
|
.\"
|
pldd.1, bpf.2, chdir.2, clone.2, fanotify_init.2, fanotify_mark.2, intro.2, ipc.2, mount.2, mprotect.2, msgctl.2, msgget.2, msgop.2, pivot_root.2, pkey_alloc.2, poll.2, prctl.2, semctl.2, semget.2, semop.2, setxattr.2, shmctl.2, shmget.2, shmop.2, tkill.2, dlopen.3, exec.3, ftok.3, getutent.3, on_exit.3, strcat.3, cpuid.4, proc.5, capabilities.7, cgroup_namespaces.7, credentials.7, fanotify.7, mount_namespaces.7, namespaces.7, sched.7, signal.7, socket.7, unix.7, user_namespaces.7, vdso.7, xattr.7, ld.so.8: tstamp
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-08-02 06:34:32 +00:00
|
|
|
.TH BPF 2 2019-08-02 "Linux" "Linux Programmer's Manual"
|
2015-03-13 19:16:32 +00:00
|
|
|
.SH NAME
|
2016-08-07 16:44:45 +00:00
|
|
|
bpf \- perform a command on an extended BPF map or program
|
2015-03-13 19:16:32 +00:00
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <linux/bpf.h>
|
2018-08-03 13:48:43 +00:00
|
|
|
|
2019-01-01 22:51:34 +00:00
|
|
|
.BI "int bpf(int " cmd ", union bpf_attr *" attr ", unsigned int " size );
|
2018-08-03 13:48:43 +00:00
|
|
|
.fi
|
2015-03-13 19:16:32 +00:00
|
|
|
.SH DESCRIPTION
|
2015-05-24 19:47:35 +00:00
|
|
|
The
|
2015-05-25 17:49:29 +00:00
|
|
|
.BR bpf ()
|
2015-05-26 10:56:34 +00:00
|
|
|
system call performs a range of operations related to extended
|
|
|
|
Berkeley Packet Filters.
|
|
|
|
Extended BPF (or eBPF) is similar to
|
2015-06-04 11:11:19 +00:00
|
|
|
the original ("classic") BPF (cBPF) used to filter network packets.
|
|
|
|
For both cBPF and eBPF programs,
|
2015-05-26 10:56:34 +00:00
|
|
|
the kernel statically analyzes the programs before loading them,
|
|
|
|
in order to ensure that they cannot harm the running system.
|
add_key.2, bpf.2, fcntl.2, futex.2, listxattr.2, perf_event_open.2, prctl.2, request_key.2, sigaltstack.2, __ppc_set_ppr_med.3, __ppc_yield.3, getw.3, setbuf.3, setjmp.3, lirc.4, core.5, securetty.5, inode.7, keyrings.7, process-keyring.7, user-keyring.7, ld.so.8: srcfix: use .PP instead of .P
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2017-08-17 23:13:44 +00:00
|
|
|
.PP
|
2015-06-07 13:33:34 +00:00
|
|
|
eBPF extends cBPF in multiple ways, including the ability to call
|
2015-07-22 12:53:08 +00:00
|
|
|
a fixed set of in-kernel helper functions
|
|
|
|
.\" See 'enum bpf_func_id' in include/uapi/linux/bpf.h
|
|
|
|
(via the
|
2015-05-26 10:56:34 +00:00
|
|
|
.B BPF_CALL
|
|
|
|
opcode extension provided by eBPF)
|
2015-07-22 14:45:08 +00:00
|
|
|
and access shared data structures such as eBPF maps.
|
2015-07-23 13:36:42 +00:00
|
|
|
.\"
|
2015-03-13 19:16:32 +00:00
|
|
|
.SS Extended BPF Design/Architecture
|
2015-07-23 10:11:21 +00:00
|
|
|
eBPF maps are a generic data structure for storage of different data types.
|
2015-07-29 22:25:42 +00:00
|
|
|
Data types are generally treated as binary blobs, so a user just specifies
|
2015-08-05 20:07:09 +00:00
|
|
|
the size of the key and the size of the value at map-creation time.
|
2015-07-29 22:25:42 +00:00
|
|
|
In other words, a key/value for a given map can have an arbitrary structure.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-03-13 19:16:32 +00:00
|
|
|
A user process can create multiple maps (with key/value-pairs being
|
2015-05-25 17:49:29 +00:00
|
|
|
opaque bytes of data) and access them via file descriptors.
|
2015-07-23 09:56:44 +00:00
|
|
|
Different eBPF programs can access the same maps in parallel.
|
2015-06-04 11:11:19 +00:00
|
|
|
It's up to the user process and eBPF program to decide what they store
|
2015-03-13 19:16:32 +00:00
|
|
|
inside maps.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-08-05 20:07:09 +00:00
|
|
|
There's one special map type, called a program array.
|
|
|
|
This type of map stores file descriptors referring to other eBPF programs.
|
|
|
|
When a lookup in the map is performed, the program flow is
|
|
|
|
redirected in-place to the beginning of another eBPF program and does not
|
|
|
|
return back to the calling program.
|
2015-08-07 09:39:07 +00:00
|
|
|
The level of nesting has a fixed limit of 32,
|
|
|
|
.\" Defined by the kernel constant MAX_TAIL_CALL_CNT in include/linux/bpf.h
|
|
|
|
so that infinite loops cannot be crafted.
|
2018-04-27 12:43:23 +00:00
|
|
|
At run time, the program file descriptors stored in the map can be modified,
|
2015-07-29 22:25:42 +00:00
|
|
|
so program functionality can be altered based on specific requirements.
|
2015-08-05 20:07:09 +00:00
|
|
|
All programs referred to in a program-array map must
|
|
|
|
have been previously loaded into the kernel via
|
|
|
|
.BR bpf ().
|
|
|
|
If a map lookup fails, the current program continues its execution.
|
|
|
|
See
|
|
|
|
.B BPF_MAP_TYPE_PROG_ARRAY
|
|
|
|
below for further details.
|
add_key.2, bpf.2, fcntl.2, futex.2, listxattr.2, perf_event_open.2, prctl.2, request_key.2, sigaltstack.2, __ppc_set_ppr_med.3, __ppc_yield.3, getw.3, setbuf.3, setjmp.3, lirc.4, core.5, securetty.5, inode.7, keyrings.7, process-keyring.7, user-keyring.7, ld.so.8: srcfix: use .PP instead of .P
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2017-08-17 23:13:44 +00:00
|
|
|
.PP
|
2015-07-29 22:25:42 +00:00
|
|
|
Generally, eBPF programs are loaded by the user process and automatically
|
2015-08-05 20:07:09 +00:00
|
|
|
unloaded when the process exits.
|
|
|
|
In some cases, for example,
|
2015-07-29 22:25:42 +00:00
|
|
|
.BR tc-bpf (8),
|
|
|
|
the program will continue to stay alive inside the kernel even after the
|
2015-10-22 14:44:11 +00:00
|
|
|
process that loaded the program exits.
|
2015-08-05 20:07:09 +00:00
|
|
|
In that case,
|
|
|
|
the tc subsystem holds a reference to the eBPF program after the
|
|
|
|
file descriptor has been closed by the user-space program.
|
2015-07-29 22:25:42 +00:00
|
|
|
Thus, whether a specific program continues to live inside the kernel
|
|
|
|
depends on how it is further attached to a given kernel subsystem
|
|
|
|
after it was loaded via
|
2015-08-05 20:07:09 +00:00
|
|
|
.BR bpf ().
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-08-05 20:07:09 +00:00
|
|
|
Each eBPF program is a set of instructions that is safe to run until
|
2015-05-24 10:00:38 +00:00
|
|
|
its completion.
|
2015-06-04 11:11:19 +00:00
|
|
|
An in-kernel verifier statically determines that the eBPF program
|
2015-05-24 10:00:38 +00:00
|
|
|
terminates and is safe to execute.
|
2015-06-04 10:48:35 +00:00
|
|
|
During verification, the kernel increments reference counts for each of
|
|
|
|
the maps that the eBPF program uses,
|
2015-07-23 10:11:21 +00:00
|
|
|
so that the attached maps can't be removed until the program is unloaded.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-06-04 11:11:19 +00:00
|
|
|
eBPF programs can be attached to different events.
|
2015-06-07 15:00:20 +00:00
|
|
|
These events can be the arrival of network packets, tracing
|
2015-07-23 10:11:21 +00:00
|
|
|
events, classification events by network queueing disciplines
|
|
|
|
(for eBPF programs attached to a
|
2015-06-07 15:00:20 +00:00
|
|
|
.BR tc (8)
|
|
|
|
classifier), and other types that may be added in the future.
|
2015-06-04 11:11:19 +00:00
|
|
|
A new event triggers execution of the eBPF program, which
|
2015-07-22 12:53:08 +00:00
|
|
|
may store information about the event in eBPF maps.
|
2015-06-04 11:11:19 +00:00
|
|
|
Beyond storing data, eBPF programs may call a fixed set of
|
2015-06-04 10:48:35 +00:00
|
|
|
in-kernel helper functions.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 12:53:08 +00:00
|
|
|
The same eBPF program can be attached to multiple events and different
|
2015-06-07 13:33:34 +00:00
|
|
|
eBPF programs can access the same map:
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-05-24 19:31:01 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-08-05 20:07:09 +00:00
|
|
|
tracing tracing tracing packet packet packet
|
|
|
|
event A event B event C on eth0 on eth1 on eth2
|
|
|
|
| | | | | ^
|
|
|
|
| | | | v |
|
|
|
|
--> tracing <-- tracing socket tc ingress tc egress
|
|
|
|
prog_1 prog_2 prog_3 classifier action
|
|
|
|
| | | | prog_4 prog_5
|
|
|
|
|--- -----| |------| map_3 | |
|
|
|
|
map_1 map_2 --| map_4 |--
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-24 19:31:01 +00:00
|
|
|
.in
|
2015-07-23 13:36:42 +00:00
|
|
|
.\"
|
2015-05-24 19:47:35 +00:00
|
|
|
.SS Arguments
|
2015-05-26 10:56:34 +00:00
|
|
|
The operation to be performed by the
|
2015-05-24 19:31:01 +00:00
|
|
|
.BR bpf ()
|
2015-05-26 10:56:34 +00:00
|
|
|
system call is determined by the
|
2019-01-01 22:51:34 +00:00
|
|
|
.I cmd
|
2015-07-22 12:53:08 +00:00
|
|
|
argument.
|
|
|
|
Each operation takes an accompanying argument,
|
|
|
|
provided via
|
|
|
|
.IR attr ,
|
|
|
|
which is a pointer to a union of type
|
2019-01-01 22:51:34 +00:00
|
|
|
.I bpf_attr
|
2015-07-22 12:53:08 +00:00
|
|
|
(see below).
|
|
|
|
The
|
|
|
|
.I size
|
|
|
|
argument is the size of the union pointed to by
|
|
|
|
.IR attr .
|
_syscall.2, bpf.2, cacheflush.2, capget.2, chdir.2, chmod.2, chroot.2, clock_getres.2, clock_nanosleep.2, clone.2, close.2, connect.2, copy_file_range.2, create_module.2, delete_module.2, dup.2, epoll_create.2, epoll_ctl.2, epoll_wait.2, eventfd.2, execve.2, execveat.2, fallocate.2, flock.2, fork.2, fsync.2, futex.2, futimesat.2, get_kernel_syms.2, get_mempolicy.2, get_robust_list.2, getcpu.2, getdents.2, getdomainname.2, getgid.2, getgroups.2, gethostname.2, getitimer.2, getpagesize.2, getpeername.2, getpriority.2, getrandom.2, getresuid.2, getrlimit.2, getrusage.2, getsid.2, getsockname.2, getsockopt.2, gettid.2, gettimeofday.2, getuid.2, getunwind.2, init_module.2, inotify_add_watch.2, inotify_init.2, inotify_rm_watch.2, intro.2, io_cancel.2, io_destroy.2, io_getevents.2, io_setup.2, io_submit.2, ioctl_fat.2, ioctl_ficlonerange.2, ioctl_fideduperange.2, ioctl_tty.2, ioctl_userfaultfd.2, ioperm.2, iopl.2, ioprio_set.2, kcmp.2, kexec_load.2, keyctl.2, kill.2, link.2, listen.2, listxattr.2, llseek.2, lookup_dcookie.2, lseek.2, madvise.2, mbind.2, membarrier.2, memfd_create.2, migrate_pages.2, mincore.2, mkdir.2, mknod.2, mlock.2, mmap.2, mmap2.2, modify_ldt.2, move_pages.2, mprotect.2, mq_getsetattr.2, mremap.2, msgctl.2, msgget.2, msgop.2, msync.2, nanosleep.2, nfsservctl.2, nice.2, open_by_handle_at.2, outb.2, perf_event_open.2, perfmonctl.2, personality.2, pivot_root.2, pkey_alloc.2, poll.2, posix_fadvise.2, prctl.2, pread.2, process_vm_readv.2, ptrace.2, query_module.2, quotactl.2, read.2, readahead.2, readdir.2, readv.2, reboot.2, recv.2, recvmmsg.2, remap_file_pages.2, rename.2, request_key.2, restart_syscall.2, rt_sigqueueinfo.2, s390_pci_mmio_write.2, s390_runtime_instr.2, sched_get_priority_max.2, sched_rr_get_interval.2, sched_setaffinity.2, sched_setattr.2, sched_setparam.2, sched_setscheduler.2, sched_yield.2, seccomp.2, select.2, select_tut.2, semctl.2, semget.2, semop.2, send.2, sendfile.2, sendmmsg.2, set_mempolicy.2, set_thread_area.2, set_tid_address.2, seteuid.2, setfsgid.2, setfsuid.2, setgid.2, setns.2, setpgid.2, setresuid.2, setreuid.2, setsid.2, setuid.2, sgetmask.2, shmctl.2, shmget.2, shmop.2, sigaction.2, sigaltstack.2, sigpending.2, sigprocmask.2, sigreturn.2, sigsuspend.2, sigwaitinfo.2, socket.2, socketcall.2, socketpair.2, splice.2, spu_create.2, spu_run.2, stat.2, statfs.2, statx.2, subpage_prot.2, swapon.2, symlink.2, sync.2, sync_file_range.2, syscalls.2, sysctl.2, sysinfo.2, syslog.2, tee.2, time.2, timer_create.2, timer_getoverrun.2, timer_settime.2, timerfd_create.2, times.2, tkill.2, truncate.2, umask.2, umount.2, unimplemented.2, unlink.2, unshare.2, uselib.2, userfaultfd.2, utime.2, utimensat.2, vfork.2, vmsplice.2, wait.2, wait4.2, write.2: Formatting fix: replace blank lines with .PP/.IP
Blank lines shouldn't generally appear in *roff source (other
than in code examples), since they create large vertical
spaces between text blocks.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2017-08-16 07:30:51 +00:00
|
|
|
.PP
|
2015-07-22 12:53:08 +00:00
|
|
|
The value provided in
|
2019-01-01 22:51:34 +00:00
|
|
|
.I cmd
|
2015-07-22 12:53:08 +00:00
|
|
|
is one of the following:
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_MAP_CREATE
|
2015-07-23 10:11:21 +00:00
|
|
|
Create a map and return a file descriptor that refers to the map.
|
2016-02-11 13:48:03 +00:00
|
|
|
The close-on-exec file descriptor flag (see
|
|
|
|
.BR fcntl (2))
|
|
|
|
is automatically enabled for the new file descriptor.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_MAP_LOOKUP_ELEM
|
2015-05-26 10:56:34 +00:00
|
|
|
Look up an element by key in a specified map and return its value.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_MAP_UPDATE_ELEM
|
2015-05-26 10:56:34 +00:00
|
|
|
Create or update an element (key/value pair) in a specified map.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_MAP_DELETE_ELEM
|
2015-05-26 10:56:34 +00:00
|
|
|
Look up and delete an element by key in a specified map.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_MAP_GET_NEXT_KEY
|
2015-05-26 10:56:34 +00:00
|
|
|
Look up an element by key in a specified map and return the key
|
|
|
|
of the next element.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_PROG_LOAD
|
2015-06-07 15:00:20 +00:00
|
|
|
Verify and load an eBPF program,
|
|
|
|
returning a new file descriptor associated with the program.
|
2016-02-11 13:48:03 +00:00
|
|
|
The close-on-exec file descriptor flag (see
|
|
|
|
.BR fcntl (2))
|
|
|
|
is automatically enabled for the new file descriptor.
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
The
|
|
|
|
.I bpf_attr
|
|
|
|
union consists of various anonymous structures that are used by different
|
|
|
|
.BR bpf ()
|
|
|
|
commands:
|
2017-08-15 10:14:18 +00:00
|
|
|
.PP
|
2015-05-26 10:56:34 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-03-13 19:16:32 +00:00
|
|
|
union bpf_attr {
|
2015-05-26 10:56:34 +00:00
|
|
|
struct { /* Used by BPF_MAP_CREATE */
|
|
|
|
__u32 map_type;
|
|
|
|
__u32 key_size; /* size of key in bytes */
|
|
|
|
__u32 value_size; /* size of value in bytes */
|
|
|
|
__u32 max_entries; /* maximum number of entries
|
|
|
|
in a map */
|
2015-03-13 19:16:32 +00:00
|
|
|
};
|
|
|
|
|
2015-07-22 12:53:08 +00:00
|
|
|
struct { /* Used by BPF_MAP_*_ELEM and BPF_MAP_GET_NEXT_KEY
|
|
|
|
commands */
|
2015-05-26 10:56:34 +00:00
|
|
|
__u32 map_fd;
|
|
|
|
__aligned_u64 key;
|
2015-03-13 19:16:32 +00:00
|
|
|
union {
|
|
|
|
__aligned_u64 value;
|
|
|
|
__aligned_u64 next_key;
|
|
|
|
};
|
2015-05-26 10:56:34 +00:00
|
|
|
__u64 flags;
|
2015-03-13 19:16:32 +00:00
|
|
|
};
|
|
|
|
|
2015-05-26 10:56:34 +00:00
|
|
|
struct { /* Used by BPF_PROG_LOAD */
|
|
|
|
__u32 prog_type;
|
|
|
|
__u32 insn_cnt;
|
|
|
|
__aligned_u64 insns; /* 'const struct bpf_insn *' */
|
|
|
|
__aligned_u64 license; /* 'const char *' */
|
|
|
|
__u32 log_level; /* verbosity level of verifier */
|
|
|
|
__u32 log_size; /* size of user buffer */
|
|
|
|
__aligned_u64 log_buf; /* user supplied 'char *'
|
|
|
|
buffer */
|
2015-07-22 12:53:08 +00:00
|
|
|
__u32 kern_version;
|
2015-06-07 15:00:20 +00:00
|
|
|
/* checked when prog_type=kprobe
|
|
|
|
(since Linux 4.1) */
|
|
|
|
.\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
|
2015-03-13 19:16:32 +00:00
|
|
|
};
|
|
|
|
} __attribute__((aligned(8)));
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-26 10:56:34 +00:00
|
|
|
.in
|
2015-07-23 13:36:42 +00:00
|
|
|
.\"
|
2015-07-22 14:45:08 +00:00
|
|
|
.SS eBPF maps
|
2015-07-22 15:58:46 +00:00
|
|
|
Maps are a generic data structure for storage of different types of data.
|
|
|
|
They allow sharing of data between eBPF kernel programs,
|
|
|
|
and also between kernel and user-space applications.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-05-25 17:49:29 +00:00
|
|
|
Each map type has the following attributes:
|
|
|
|
.IP * 3
|
|
|
|
type
|
|
|
|
.IP *
|
2015-05-26 06:34:12 +00:00
|
|
|
maximum number of elements
|
2015-05-25 17:49:29 +00:00
|
|
|
.IP *
|
|
|
|
key size in bytes
|
|
|
|
.IP *
|
|
|
|
value size in bytes
|
|
|
|
.PP
|
2015-05-26 10:56:34 +00:00
|
|
|
The following wrapper functions demonstrate how various
|
|
|
|
.BR bpf ()
|
|
|
|
commands can be used to access the maps.
|
2015-05-24 10:00:38 +00:00
|
|
|
The functions use the
|
2019-01-01 22:51:34 +00:00
|
|
|
.I cmd
|
2015-03-13 19:16:32 +00:00
|
|
|
argument to invoke different operations.
|
2015-07-22 14:45:08 +00:00
|
|
|
.TP
|
2015-05-26 10:56:34 +00:00
|
|
|
.B BPF_MAP_CREATE
|
|
|
|
The
|
2015-03-13 19:16:32 +00:00
|
|
|
.B BPF_MAP_CREATE
|
2015-06-09 11:13:13 +00:00
|
|
|
command creates a new map,
|
|
|
|
returning a new file descriptor that refers to the map.
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-05-26 10:56:34 +00:00
|
|
|
int
|
2015-07-23 10:11:21 +00:00
|
|
|
bpf_create_map(enum bpf_map_type map_type,
|
|
|
|
unsigned int key_size,
|
|
|
|
unsigned int value_size,
|
|
|
|
unsigned int max_entries)
|
2015-03-13 19:16:32 +00:00
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
2015-07-23 10:11:21 +00:00
|
|
|
.map_type = map_type,
|
|
|
|
.key_size = key_size,
|
|
|
|
.value_size = value_size,
|
2015-03-13 19:16:32 +00:00
|
|
|
.max_entries = max_entries
|
|
|
|
};
|
|
|
|
|
|
|
|
return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
|
|
|
|
}
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-26 10:56:34 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
The new map has the type specified by
|
|
|
|
.IR map_type ,
|
|
|
|
and attributes as specified in
|
2015-05-24 19:31:01 +00:00
|
|
|
.IR key_size ,
|
|
|
|
.IR value_size ,
|
2015-05-26 10:56:34 +00:00
|
|
|
and
|
2015-05-24 19:31:01 +00:00
|
|
|
.IR max_entries .
|
2015-07-22 20:02:27 +00:00
|
|
|
On success, this operation returns a file descriptor.
|
2015-05-24 10:00:38 +00:00
|
|
|
On error, \-1 is returned and
|
2015-03-13 19:16:32 +00:00
|
|
|
.I errno
|
2015-05-24 19:31:01 +00:00
|
|
|
is set to
|
|
|
|
.BR EINVAL ,
|
|
|
|
.BR EPERM ,
|
|
|
|
or
|
|
|
|
.BR ENOMEM .
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-07-23 10:11:21 +00:00
|
|
|
The
|
2015-03-13 19:16:32 +00:00
|
|
|
.I key_size
|
|
|
|
and
|
|
|
|
.I value_size
|
2015-07-23 10:11:21 +00:00
|
|
|
attributes will be used by the verifier during program loading
|
|
|
|
to check that the program is calling
|
2015-05-24 19:31:01 +00:00
|
|
|
.BR bpf_map_*_elem ()
|
|
|
|
helper functions with a correctly initialized
|
2015-03-13 19:16:32 +00:00
|
|
|
.I key
|
2015-07-22 12:53:08 +00:00
|
|
|
and to check that the program doesn't access the map element
|
2015-03-13 19:16:32 +00:00
|
|
|
.I value
|
|
|
|
beyond the specified
|
2015-05-25 17:49:29 +00:00
|
|
|
.IR value_size .
|
2015-05-26 10:56:34 +00:00
|
|
|
For example, when a map is created with a
|
2019-01-01 22:51:34 +00:00
|
|
|
.I key_size
|
2015-07-22 12:53:08 +00:00
|
|
|
of 8 and the eBPF program calls
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-24 19:31:01 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-03-13 19:16:32 +00:00
|
|
|
bpf_map_lookup_elem(map_fd, fp - 4)
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-24 19:31:01 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-03-13 19:16:32 +00:00
|
|
|
the program will be rejected,
|
2015-05-24 19:31:01 +00:00
|
|
|
since the in-kernel helper function
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
|
|
|
.EX
|
2015-05-26 10:56:34 +00:00
|
|
|
bpf_map_lookup_elem(map_fd, void *key)
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
|
|
|
.IP
|
2015-07-22 20:02:27 +00:00
|
|
|
expects to read 8 bytes from the location pointed to by
|
|
|
|
.IR key ,
|
|
|
|
but the
|
2019-01-01 22:51:34 +00:00
|
|
|
.I fp\ -\ 4
|
2015-07-22 20:02:27 +00:00
|
|
|
(where
|
|
|
|
.I fp
|
|
|
|
is the top of the stack)
|
2015-05-24 19:31:01 +00:00
|
|
|
starting address will cause out-of-bounds stack access.
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
Similarly, when a map is created with a
|
|
|
|
.I value_size
|
2015-07-22 12:53:08 +00:00
|
|
|
of 1 and the eBPF program contains
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-24 19:31:01 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-03-13 19:16:32 +00:00
|
|
|
value = bpf_map_lookup_elem(...);
|
2015-05-24 19:31:01 +00:00
|
|
|
*(u32 *) value = 1;
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-24 19:31:01 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-03-13 19:16:32 +00:00
|
|
|
the program will be rejected, since it accesses the
|
|
|
|
.I value
|
2015-05-24 19:31:01 +00:00
|
|
|
pointer beyond the specified 1 byte
|
|
|
|
.I value_size
|
|
|
|
limit.
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-07-22 12:53:08 +00:00
|
|
|
Currently, the following values are supported for
|
|
|
|
.IR map_type :
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-24 19:31:01 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-03-13 19:16:32 +00:00
|
|
|
enum bpf_map_type {
|
2015-07-22 14:45:08 +00:00
|
|
|
BPF_MAP_TYPE_UNSPEC, /* Reserve 0 as invalid map type */
|
2015-05-26 10:56:34 +00:00
|
|
|
BPF_MAP_TYPE_HASH,
|
|
|
|
BPF_MAP_TYPE_ARRAY,
|
2015-06-09 11:13:13 +00:00
|
|
|
BPF_MAP_TYPE_PROG_ARRAY,
|
2017-12-23 22:02:17 +00:00
|
|
|
BPF_MAP_TYPE_PERF_EVENT_ARRAY,
|
|
|
|
BPF_MAP_TYPE_PERCPU_HASH,
|
|
|
|
BPF_MAP_TYPE_PERCPU_ARRAY,
|
|
|
|
BPF_MAP_TYPE_STACK_TRACE,
|
|
|
|
BPF_MAP_TYPE_CGROUP_ARRAY,
|
|
|
|
BPF_MAP_TYPE_LRU_HASH,
|
|
|
|
BPF_MAP_TYPE_LRU_PERCPU_HASH,
|
|
|
|
BPF_MAP_TYPE_LPM_TRIE,
|
|
|
|
BPF_MAP_TYPE_ARRAY_OF_MAPS,
|
|
|
|
BPF_MAP_TYPE_HASH_OF_MAPS,
|
|
|
|
BPF_MAP_TYPE_DEVMAP,
|
|
|
|
BPF_MAP_TYPE_SOCKMAP,
|
|
|
|
BPF_MAP_TYPE_CPUMAP,
|
2015-03-13 19:16:32 +00:00
|
|
|
};
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-24 19:31:01 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-03-13 19:16:32 +00:00
|
|
|
.I map_type
|
2015-05-24 10:00:38 +00:00
|
|
|
selects one of the available map implementations in the kernel.
|
2015-07-22 12:53:08 +00:00
|
|
|
.\" FIXME We need an explanation of why one might choose each of
|
2016-10-29 09:55:36 +00:00
|
|
|
.\" these map implementations
|
2015-05-25 17:49:29 +00:00
|
|
|
For all map types,
|
2015-07-22 12:53:08 +00:00
|
|
|
eBPF programs access maps with the same
|
|
|
|
.BR bpf_map_lookup_elem ()
|
|
|
|
and
|
2015-05-24 19:31:01 +00:00
|
|
|
.BR bpf_map_update_elem ()
|
2015-03-13 19:16:32 +00:00
|
|
|
helper functions.
|
2015-07-22 14:45:08 +00:00
|
|
|
Further details of the various map types are given below.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_MAP_LOOKUP_ELEM
|
2015-05-26 10:56:34 +00:00
|
|
|
The
|
|
|
|
.B BPF_MAP_LOOKUP_ELEM
|
|
|
|
command looks up an element with a given
|
|
|
|
.I key
|
|
|
|
in the map referred to by the file descriptor
|
|
|
|
.IR fd .
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-05-26 10:56:34 +00:00
|
|
|
int
|
2015-07-23 10:11:21 +00:00
|
|
|
bpf_lookup_elem(int fd, const void *key, void *value)
|
2015-03-13 19:16:32 +00:00
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
|
|
|
.map_fd = fd,
|
2015-07-23 10:11:21 +00:00
|
|
|
.key = ptr_to_u64(key),
|
|
|
|
.value = ptr_to_u64(value),
|
2015-03-13 19:16:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr));
|
|
|
|
}
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-26 10:56:34 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
If an element is found,
|
|
|
|
the operation returns zero and stores the element's value into
|
2015-06-09 11:13:13 +00:00
|
|
|
.IR value ,
|
|
|
|
which must point to a buffer of
|
|
|
|
.I value_size
|
|
|
|
bytes.
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
If no element is found, the operation returns \-1 and sets
|
2015-03-13 19:16:32 +00:00
|
|
|
.I errno
|
2015-05-24 19:31:01 +00:00
|
|
|
to
|
|
|
|
.BR ENOENT .
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_MAP_UPDATE_ELEM
|
2015-05-26 10:56:34 +00:00
|
|
|
The
|
|
|
|
.B BPF_MAP_UPDATE_ELEM
|
|
|
|
command
|
|
|
|
creates or updates an element with a given
|
|
|
|
.I key/value
|
|
|
|
in the map referred to by the file descriptor
|
|
|
|
.IR fd .
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-05-26 10:56:34 +00:00
|
|
|
int
|
2015-07-23 10:11:21 +00:00
|
|
|
bpf_update_elem(int fd, const void *key, const void *value,
|
|
|
|
uint64_t flags)
|
2015-03-13 19:16:32 +00:00
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
|
|
|
.map_fd = fd,
|
2015-07-23 10:11:21 +00:00
|
|
|
.key = ptr_to_u64(key),
|
|
|
|
.value = ptr_to_u64(value),
|
|
|
|
.flags = flags,
|
2015-03-13 19:16:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr));
|
|
|
|
}
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-26 10:56:34 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
The
|
2015-03-13 19:16:32 +00:00
|
|
|
.I flags
|
2015-05-26 10:56:34 +00:00
|
|
|
argument should be specified as one of the following:
|
|
|
|
.RS
|
|
|
|
.TP
|
|
|
|
.B BPF_ANY
|
|
|
|
Create a new element or update an existing element.
|
|
|
|
.TP
|
|
|
|
.B BPF_NOEXIST
|
|
|
|
Create a new element only if it did not exist.
|
|
|
|
.TP
|
|
|
|
.B BPF_EXIST
|
|
|
|
Update an existing element.
|
|
|
|
.RE
|
|
|
|
.IP
|
|
|
|
On success, the operation returns zero.
|
2015-03-13 19:16:32 +00:00
|
|
|
On error, \-1 is returned and
|
|
|
|
.I errno
|
2015-05-24 19:31:01 +00:00
|
|
|
is set to
|
|
|
|
.BR EINVAL ,
|
|
|
|
.BR EPERM ,
|
|
|
|
.BR ENOMEM ,
|
|
|
|
or
|
|
|
|
.BR E2BIG .
|
2015-03-13 19:16:32 +00:00
|
|
|
.B E2BIG
|
2015-05-26 10:56:34 +00:00
|
|
|
indicates that the number of elements in the map reached the
|
2015-03-13 19:16:32 +00:00
|
|
|
.I max_entries
|
|
|
|
limit specified at map creation time.
|
|
|
|
.B EEXIST
|
2015-05-26 10:56:34 +00:00
|
|
|
will be returned if
|
|
|
|
.I flags
|
|
|
|
specifies
|
|
|
|
.B BPF_NOEXIST
|
|
|
|
and the element with
|
2015-05-24 19:31:01 +00:00
|
|
|
.I key
|
|
|
|
already exists in the map.
|
2015-03-13 19:16:32 +00:00
|
|
|
.B ENOENT
|
2015-07-23 10:11:21 +00:00
|
|
|
will be returned if
|
2015-05-26 10:56:34 +00:00
|
|
|
.I flags
|
|
|
|
specifies
|
|
|
|
.B BPF_EXIST
|
|
|
|
and the element with
|
2015-05-24 19:31:01 +00:00
|
|
|
.I key
|
|
|
|
doesn't exist in the map.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_MAP_DELETE_ELEM
|
2015-05-26 10:56:34 +00:00
|
|
|
The
|
|
|
|
.B BPF_MAP_DELETE_ELEM
|
|
|
|
command
|
2019-03-20 07:57:56 +00:00
|
|
|
deletes the element whose key is
|
2015-05-26 10:56:34 +00:00
|
|
|
.I key
|
|
|
|
from the map referred to by the file descriptor
|
|
|
|
.IR fd .
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-05-26 10:56:34 +00:00
|
|
|
int
|
2015-07-23 10:11:21 +00:00
|
|
|
bpf_delete_elem(int fd, const void *key)
|
2015-03-13 19:16:32 +00:00
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
|
|
|
.map_fd = fd,
|
2015-07-23 10:11:21 +00:00
|
|
|
.key = ptr_to_u64(key),
|
2015-03-13 19:16:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return bpf(BPF_MAP_DELETE_ELEM, &attr, sizeof(attr));
|
|
|
|
}
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-26 10:56:34 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
On success, zero is returned.
|
|
|
|
If the element is not found, \-1 is returned and
|
2015-03-13 19:16:32 +00:00
|
|
|
.I errno
|
2015-05-26 10:56:34 +00:00
|
|
|
is set to
|
2015-05-24 19:31:01 +00:00
|
|
|
.BR ENOENT .
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_MAP_GET_NEXT_KEY
|
2015-05-26 10:56:34 +00:00
|
|
|
The
|
|
|
|
.B BPF_MAP_GET_NEXT_KEY
|
|
|
|
command looks up an element by
|
|
|
|
.I key
|
|
|
|
in the map referred to by the file descriptor
|
2019-01-01 22:51:34 +00:00
|
|
|
.I fd
|
2015-05-26 10:56:34 +00:00
|
|
|
and sets the
|
|
|
|
.I next_key
|
|
|
|
pointer to the key of the next element.
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-26 10:56:34 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-05-26 10:56:34 +00:00
|
|
|
int
|
2015-07-23 10:11:21 +00:00
|
|
|
bpf_get_next_key(int fd, const void *key, void *next_key)
|
2015-03-13 19:16:32 +00:00
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
2015-07-23 10:11:21 +00:00
|
|
|
.map_fd = fd,
|
|
|
|
.key = ptr_to_u64(key),
|
2015-03-13 19:16:32 +00:00
|
|
|
.next_key = ptr_to_u64(next_key),
|
|
|
|
};
|
|
|
|
|
|
|
|
return bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
|
|
|
|
}
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-26 10:56:34 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-06-09 11:13:13 +00:00
|
|
|
If
|
|
|
|
.I key
|
|
|
|
is found, the operation returns zero and sets the
|
|
|
|
.I next_key
|
|
|
|
pointer to the key of the next element.
|
2015-03-13 19:16:32 +00:00
|
|
|
If
|
|
|
|
.I key
|
2015-05-26 10:56:34 +00:00
|
|
|
is not found, the operation returns zero and sets the
|
2015-03-13 19:16:32 +00:00
|
|
|
.I next_key
|
|
|
|
pointer to the key of the first element.
|
|
|
|
If
|
|
|
|
.I key
|
2015-05-26 10:56:34 +00:00
|
|
|
is the last element, \-1 is returned and
|
2015-03-13 19:16:32 +00:00
|
|
|
.I errno
|
2015-05-26 10:56:34 +00:00
|
|
|
is set to
|
2015-05-24 19:31:01 +00:00
|
|
|
.BR ENOENT .
|
2015-05-24 10:00:38 +00:00
|
|
|
Other possible
|
2015-03-13 19:16:32 +00:00
|
|
|
.I errno
|
2015-05-24 19:31:01 +00:00
|
|
|
values are
|
|
|
|
.BR ENOMEM ,
|
|
|
|
.BR EFAULT ,
|
|
|
|
.BR EPERM ,
|
|
|
|
and
|
|
|
|
.BR EINVAL .
|
2015-03-13 19:16:32 +00:00
|
|
|
This method can be used to iterate over all elements in the map.
|
|
|
|
.TP
|
|
|
|
.B close(map_fd)
|
2015-05-26 10:56:34 +00:00
|
|
|
Delete the map referred to by the file descriptor
|
2015-05-24 19:31:01 +00:00
|
|
|
.IR map_fd .
|
2015-05-26 10:56:34 +00:00
|
|
|
When the user-space program that created a map exits, all maps will
|
2015-07-22 14:45:08 +00:00
|
|
|
be deleted automatically (but see NOTES).
|
|
|
|
.\"
|
|
|
|
.SS eBPF map types
|
|
|
|
The following map types are supported:
|
|
|
|
.TP
|
|
|
|
.B BPF_MAP_TYPE_HASH
|
|
|
|
.\" commit 0f8e4bd8a1fc8c4185f1630061d0a1f2d197a475
|
|
|
|
Hash-table maps have the following characteristics:
|
|
|
|
.RS
|
|
|
|
.IP * 3
|
|
|
|
Maps are created and destroyed by user-space programs.
|
|
|
|
Both user-space and eBPF programs
|
2015-07-22 20:02:27 +00:00
|
|
|
can perform lookup, update, and delete operations.
|
2015-07-22 14:45:08 +00:00
|
|
|
.IP *
|
|
|
|
The kernel takes care of allocating and freeing key/value pairs.
|
|
|
|
.IP *
|
|
|
|
The
|
|
|
|
.BR map_update_elem ()
|
2016-12-03 04:37:29 +00:00
|
|
|
helper will fail to insert new element when the
|
2015-07-22 14:45:08 +00:00
|
|
|
.I max_entries
|
|
|
|
limit is reached.
|
|
|
|
(This ensures that eBPF programs cannot exhaust memory.)
|
|
|
|
.IP *
|
|
|
|
.BR map_update_elem ()
|
|
|
|
replaces existing elements atomically.
|
|
|
|
.RE
|
|
|
|
.IP
|
2015-07-23 10:11:21 +00:00
|
|
|
Hash-table maps are
|
2015-07-22 14:45:08 +00:00
|
|
|
optimized for speed of lookup.
|
|
|
|
.TP
|
|
|
|
.B BPF_MAP_TYPE_ARRAY
|
|
|
|
.\" commit 28fbcfa08d8ed7c5a50d41a0433aad222835e8e3
|
|
|
|
Array maps have the following characteristics:
|
|
|
|
.RS
|
|
|
|
.IP * 3
|
|
|
|
Optimized for fastest possible lookup.
|
2015-07-22 20:02:27 +00:00
|
|
|
In the future the verifier/JIT compiler
|
2015-07-22 14:45:08 +00:00
|
|
|
may recognize lookup() operations that employ a constant key
|
|
|
|
and optimize it into constant pointer.
|
|
|
|
It is possible to optimize a non-constant
|
|
|
|
key into direct pointer arithmetic as well, since pointers and
|
|
|
|
.I value_size
|
|
|
|
are constant for the life of the eBPF program.
|
|
|
|
In other words,
|
|
|
|
.BR array_map_lookup_elem ()
|
|
|
|
may be 'inlined' by the verifier/JIT compiler
|
|
|
|
while preserving concurrent access to this map from user space.
|
|
|
|
.IP *
|
|
|
|
All array elements pre-allocated and zero initialized at init time
|
|
|
|
.IP *
|
|
|
|
The key is an array index, and must be exactly four bytes.
|
|
|
|
.IP *
|
|
|
|
.BR map_delete_elem ()
|
|
|
|
fails with the error
|
|
|
|
.BR EINVAL ,
|
|
|
|
since elements cannot be deleted.
|
|
|
|
.IP *
|
|
|
|
.BR map_update_elem ()
|
2015-07-23 10:11:21 +00:00
|
|
|
replaces elements in a
|
|
|
|
.B nonatomic
|
|
|
|
fashion;
|
2015-08-05 20:07:09 +00:00
|
|
|
for atomic updates, a hash-table map should be used instead.
|
|
|
|
There is however one special case that can also be used with arrays:
|
|
|
|
the atomic built-in
|
2019-01-01 22:51:34 +00:00
|
|
|
.B __sync_fetch_and_add()
|
2015-08-05 20:07:09 +00:00
|
|
|
can be used on 32 and 64 bit atomic counters.
|
|
|
|
For example, it can be
|
2015-07-29 22:25:42 +00:00
|
|
|
applied on the whole value itself if it represents a single counter,
|
|
|
|
or in case of a structure containing multiple counters, it could be
|
2015-08-05 20:07:09 +00:00
|
|
|
used on individual counters.
|
|
|
|
This is quite often useful for aggregation and accounting of events.
|
2015-07-22 14:45:08 +00:00
|
|
|
.RE
|
|
|
|
.IP
|
|
|
|
Among the uses for array maps are the following:
|
|
|
|
.RS
|
|
|
|
.IP * 3
|
|
|
|
As "global" eBPF variables: an array of 1 element whose key is (index) 0
|
|
|
|
and where the value is a collection of 'global' variables which
|
|
|
|
eBPF programs can use to keep state between events.
|
|
|
|
.IP *
|
|
|
|
Aggregation of tracing events into a fixed set of buckets.
|
2015-07-29 22:25:42 +00:00
|
|
|
.IP *
|
|
|
|
Accounting of networking events, for example, number of packets and packet
|
|
|
|
sizes.
|
2015-07-22 14:45:08 +00:00
|
|
|
.RE
|
|
|
|
.TP
|
|
|
|
.BR BPF_MAP_TYPE_PROG_ARRAY " (since Linux 4.2)"
|
2015-08-05 20:07:09 +00:00
|
|
|
A program array map is a special kind of array map whose map values
|
|
|
|
contain only file descriptors referring to other eBPF programs.
|
|
|
|
Thus, both the
|
|
|
|
.I key_size
|
|
|
|
and
|
|
|
|
.I value_size
|
|
|
|
must be exactly four bytes.
|
2015-07-29 22:25:42 +00:00
|
|
|
This map is used in conjunction with the
|
2015-08-05 20:07:09 +00:00
|
|
|
.BR bpf_tail_call ()
|
2015-07-29 22:25:42 +00:00
|
|
|
helper.
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-07-29 22:25:42 +00:00
|
|
|
This means that an eBPF program with a program array map attached to it
|
|
|
|
can call from kernel side into
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-07-29 22:25:42 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2017-11-09 09:12:24 +00:00
|
|
|
void bpf_tail_call(void *context, void *prog_map,
|
|
|
|
unsigned int index);
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-07-29 22:25:42 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-07-29 22:25:42 +00:00
|
|
|
and therefore replace its own program flow with the one from the program
|
2015-08-05 20:07:09 +00:00
|
|
|
at the given program array slot, if present.
|
|
|
|
This can be regarded as kind of a jump table to a different eBPF program.
|
|
|
|
The invoked program will then reuse the same stack.
|
|
|
|
When a jump into the new program has been performed,
|
|
|
|
it won't return to the old program anymore.
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-08-07 09:39:07 +00:00
|
|
|
If no eBPF program is found at the given index of the program array
|
|
|
|
(because the map slot doesn't contain a valid program file descriptor,
|
|
|
|
the specified lookup index/key is out of bounds,
|
|
|
|
or the limit of 32
|
|
|
|
.\" MAX_TAIL_CALL_CNT
|
|
|
|
nested calls has been exceed),
|
2015-07-29 22:25:42 +00:00
|
|
|
execution continues with the current eBPF program.
|
|
|
|
This can be used as a fall-through for default cases.
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-07-29 22:25:42 +00:00
|
|
|
A program array map is useful, for example, in tracing or networking, to
|
2015-08-05 20:07:09 +00:00
|
|
|
handle individual system calls or protocols in their own subprograms and
|
|
|
|
use their identifiers as an individual map index.
|
|
|
|
This approach may result in performance benefits,
|
|
|
|
and also makes it possible to overcome the maximum
|
|
|
|
instruction limit of a single eBPF program.
|
|
|
|
In dynamic environments,
|
|
|
|
a user-space daemon might atomically replace individual subprograms
|
|
|
|
at run-time with newer versions to alter overall program behavior,
|
|
|
|
for instance, if global policies change.
|
2015-07-22 14:45:08 +00:00
|
|
|
.\"
|
|
|
|
.SS eBPF programs
|
2015-05-26 10:56:34 +00:00
|
|
|
The
|
|
|
|
.B BPF_PROG_LOAD
|
2015-06-04 11:11:19 +00:00
|
|
|
command is used to load an eBPF program into the kernel.
|
2015-06-07 15:00:20 +00:00
|
|
|
The return value for this command is a new file descriptor associated
|
2015-07-22 14:45:08 +00:00
|
|
|
with this eBPF program.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-05-26 10:56:34 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-03-13 19:16:32 +00:00
|
|
|
char bpf_log_buf[LOG_BUF_SIZE];
|
|
|
|
|
2015-05-26 10:56:34 +00:00
|
|
|
int
|
2015-07-23 10:11:21 +00:00
|
|
|
bpf_prog_load(enum bpf_prog_type type,
|
2015-05-26 10:56:34 +00:00
|
|
|
const struct bpf_insn *insns, int insn_cnt,
|
|
|
|
const char *license)
|
2015-03-13 19:16:32 +00:00
|
|
|
{
|
|
|
|
union bpf_attr attr = {
|
2015-07-23 10:11:21 +00:00
|
|
|
.prog_type = type,
|
|
|
|
.insns = ptr_to_u64(insns),
|
|
|
|
.insn_cnt = insn_cnt,
|
|
|
|
.license = ptr_to_u64(license),
|
|
|
|
.log_buf = ptr_to_u64(bpf_log_buf),
|
|
|
|
.log_size = LOG_BUF_SIZE,
|
2015-03-13 19:16:32 +00:00
|
|
|
.log_level = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
return bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
|
|
|
|
}
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-26 10:56:34 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-05-24 19:31:01 +00:00
|
|
|
.I prog_type
|
2015-03-13 19:16:32 +00:00
|
|
|
is one of the available program types:
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-24 19:31:01 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-03-13 19:16:32 +00:00
|
|
|
enum bpf_prog_type {
|
2015-07-22 12:53:08 +00:00
|
|
|
BPF_PROG_TYPE_UNSPEC, /* Reserve 0 as invalid
|
|
|
|
program type */
|
2015-07-22 14:45:08 +00:00
|
|
|
BPF_PROG_TYPE_SOCKET_FILTER,
|
|
|
|
BPF_PROG_TYPE_KPROBE,
|
|
|
|
BPF_PROG_TYPE_SCHED_CLS,
|
|
|
|
BPF_PROG_TYPE_SCHED_ACT,
|
2015-03-13 19:16:32 +00:00
|
|
|
};
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-24 19:31:01 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 14:45:08 +00:00
|
|
|
For further details of eBPF program types, see below.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 14:45:08 +00:00
|
|
|
The remaining fields of
|
2015-05-26 10:56:34 +00:00
|
|
|
.I bpf_attr
|
|
|
|
are set as follows:
|
|
|
|
.IP * 3
|
2015-05-24 19:31:01 +00:00
|
|
|
.I insns
|
2015-05-26 10:56:34 +00:00
|
|
|
is an array of
|
2015-05-24 19:31:01 +00:00
|
|
|
.I "struct bpf_insn"
|
|
|
|
instructions.
|
2015-05-26 10:56:34 +00:00
|
|
|
.IP *
|
2015-05-24 19:31:01 +00:00
|
|
|
.I insn_cnt
|
2015-05-26 10:56:34 +00:00
|
|
|
is the number of instructions in the program referred to by
|
|
|
|
.IR insns .
|
|
|
|
.IP *
|
2015-05-24 19:31:01 +00:00
|
|
|
.I license
|
2015-05-26 10:56:34 +00:00
|
|
|
is a license string, which must be GPL compatible to call helper functions
|
2015-05-24 19:31:01 +00:00
|
|
|
marked
|
|
|
|
.IR gpl_only .
|
2015-07-23 13:36:42 +00:00
|
|
|
(The licensing rules are the same as for kernel modules,
|
2015-07-29 22:25:42 +00:00
|
|
|
so that also dual licenses, such as "Dual BSD/GPL", may be used.)
|
2015-05-26 10:56:34 +00:00
|
|
|
.IP *
|
2015-05-24 19:31:01 +00:00
|
|
|
.I log_buf
|
2015-05-26 10:56:34 +00:00
|
|
|
is a pointer to a caller-allocated buffer in which the in-kernel
|
|
|
|
verifier can store the verification log.
|
2015-05-24 10:00:38 +00:00
|
|
|
This log is a multi-line string that can be checked by
|
2015-03-13 19:16:32 +00:00
|
|
|
the program author in order to understand how the verifier came to
|
2015-07-23 10:11:21 +00:00
|
|
|
the conclusion that the eBPF program is unsafe.
|
2015-03-13 19:16:32 +00:00
|
|
|
The format of the output can change at any time as the verifier evolves.
|
2015-05-26 10:56:34 +00:00
|
|
|
.IP *
|
2015-05-24 19:31:01 +00:00
|
|
|
.I log_size
|
2015-05-26 10:56:34 +00:00
|
|
|
size of the buffer pointed to by
|
2017-04-24 09:20:55 +00:00
|
|
|
.IR log_buf .
|
2015-05-24 10:00:38 +00:00
|
|
|
If the size of the buffer is not large enough to store all
|
2015-03-13 19:16:32 +00:00
|
|
|
verifier messages, \-1 is returned and
|
|
|
|
.I errno
|
2015-05-24 19:31:01 +00:00
|
|
|
is set to
|
|
|
|
.BR ENOSPC .
|
2015-05-26 10:56:34 +00:00
|
|
|
.IP *
|
2015-05-24 19:31:01 +00:00
|
|
|
.I log_level
|
2015-05-24 10:00:38 +00:00
|
|
|
verbosity level of the verifier.
|
2015-07-23 13:36:42 +00:00
|
|
|
A value of zero means that the verifier will not provide a log;
|
|
|
|
in this case,
|
|
|
|
.I log_buf
|
|
|
|
must be a NULL pointer, and
|
|
|
|
.I log_size
|
|
|
|
must be zero.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 14:45:08 +00:00
|
|
|
Applying
|
|
|
|
.BR close (2)
|
|
|
|
to the file descriptor returned by
|
|
|
|
.B BPF_PROG_LOAD
|
|
|
|
will unload the eBPF program (but see NOTES).
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-06-04 11:11:19 +00:00
|
|
|
Maps are accessible from eBPF programs and are used to exchange data between
|
|
|
|
eBPF programs and between eBPF programs and user-space programs.
|
2015-06-09 11:13:13 +00:00
|
|
|
For example,
|
|
|
|
eBPF programs can process various events (like kprobe, packets) and
|
|
|
|
store their data into a map,
|
|
|
|
and user-space programs can then fetch data from the map.
|
|
|
|
Conversely, user-space programs can use a map as a configuration mechanism,
|
|
|
|
populating the map with values checked by the eBPF program,
|
|
|
|
which then modifies its behavior on the fly according to those values.
|
2015-07-23 10:11:21 +00:00
|
|
|
.\"
|
|
|
|
.\"
|
2015-07-22 14:45:08 +00:00
|
|
|
.SS eBPF program types
|
2015-07-23 10:11:21 +00:00
|
|
|
The eBPF program type
|
|
|
|
.RI ( prog_type )
|
2015-07-23 13:36:42 +00:00
|
|
|
determines the subset of kernel helper functions that the program
|
2015-07-23 10:11:21 +00:00
|
|
|
may call.
|
2015-07-23 13:36:42 +00:00
|
|
|
The program type also determines the program input (context)\(emthe
|
2015-07-23 10:11:21 +00:00
|
|
|
format of
|
|
|
|
.I "struct bpf_context"
|
2015-07-22 14:45:08 +00:00
|
|
|
(which is the data blob passed into the eBPF program as the first argument).
|
2015-07-24 09:07:51 +00:00
|
|
|
.\"
|
2015-08-08 16:23:54 +00:00
|
|
|
.\" FIXME
|
2016-09-16 15:26:51 +00:00
|
|
|
.\" Somewhere in this page we need a general introduction to the
|
2015-07-24 09:07:51 +00:00
|
|
|
.\" bpf_context. For example, how does a BPF program access the
|
|
|
|
.\" context?
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-23 10:11:21 +00:00
|
|
|
For example, a tracing program does not have the exact same
|
|
|
|
subset of helper functions as a socket filter program
|
|
|
|
(though they may have some helpers in common).
|
|
|
|
Similarly,
|
|
|
|
the input (context) for a tracing program is a set of register values,
|
|
|
|
while for a socket filter it is a network packet.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 14:45:08 +00:00
|
|
|
The set of functions available to eBPF programs of a given type may increase
|
|
|
|
in the future.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 14:45:08 +00:00
|
|
|
The following program types are supported:
|
|
|
|
.TP
|
|
|
|
.BR BPF_PROG_TYPE_SOCKET_FILTER " (since Linux 3.19)"
|
|
|
|
Currently, the set of functions for
|
|
|
|
.B BPF_PROG_TYPE_SOCKET_FILTER
|
|
|
|
is:
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-05-24 19:31:01 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-07-22 14:45:08 +00:00
|
|
|
bpf_map_lookup_elem(map_fd, void *key)
|
|
|
|
/* look up key in a map_fd */
|
|
|
|
bpf_map_update_elem(map_fd, void *key, void *value)
|
|
|
|
/* update key/value */
|
|
|
|
bpf_map_delete_elem(map_fd, void *key)
|
|
|
|
/* delete key in a map_fd */
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-24 19:31:01 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.IP
|
2015-07-22 14:45:08 +00:00
|
|
|
The
|
|
|
|
.I bpf_context
|
|
|
|
argument is a pointer to a
|
2015-07-23 09:56:44 +00:00
|
|
|
.IR "struct __sk_buff" .
|
2015-07-23 10:11:21 +00:00
|
|
|
.\" FIXME: We need some text here to explain how the program
|
2016-10-29 09:55:36 +00:00
|
|
|
.\" accesses __sk_buff.
|
|
|
|
.\" See 'struct __sk_buff' and commit 9bac3d6d548e5
|
|
|
|
.\"
|
2015-07-23 09:56:44 +00:00
|
|
|
.\" Alexei commented:
|
2016-10-29 09:55:36 +00:00
|
|
|
.\" Actually now in case of SOCKET_FILTER, SCHED_CLS, SCHED_ACT
|
|
|
|
.\" the program can now access skb fields.
|
2015-07-22 14:45:08 +00:00
|
|
|
.\"
|
|
|
|
.TP
|
2019-01-01 22:51:34 +00:00
|
|
|
.BR BPF_PROG_TYPE_KPROBE " (since Linux 4.1)"
|
2015-07-22 14:45:08 +00:00
|
|
|
.\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
|
|
|
|
[To be documented]
|
|
|
|
.\" FIXME Document this program type
|
|
|
|
.\" Describe allowed helper functions for this program type
|
|
|
|
.\" Describe bpf_context for this program type
|
2016-10-29 09:55:36 +00:00
|
|
|
.\"
|
2015-07-22 14:45:08 +00:00
|
|
|
.\" FIXME We need text here to describe 'kern_version'
|
|
|
|
.TP
|
2019-01-01 22:51:34 +00:00
|
|
|
.BR BPF_PROG_TYPE_SCHED_CLS " (since Linux 4.1)"
|
2015-07-22 14:45:08 +00:00
|
|
|
.\" commit 96be4325f443dbbfeb37d2a157675ac0736531a1
|
|
|
|
.\" commit e2e9b6541dd4b31848079da80fe2253daaafb549
|
|
|
|
[To be documented]
|
|
|
|
.\" FIXME Document this program type
|
|
|
|
.\" Describe allowed helper functions for this program type
|
|
|
|
.\" Describe bpf_context for this program type
|
|
|
|
.TP
|
2019-01-01 22:51:34 +00:00
|
|
|
.BR BPF_PROG_TYPE_SCHED_ACT " (since Linux 4.1)"
|
2015-07-22 14:45:08 +00:00
|
|
|
.\" commit 94caee8c312d96522bcdae88791aaa9ebcd5f22c
|
|
|
|
.\" commit a8cb5f556b567974d75ea29c15181c445c541b1f
|
|
|
|
[To be documented]
|
|
|
|
.\" FIXME Document this program type
|
|
|
|
.\" Describe allowed helper functions for this program type
|
|
|
|
.\" Describe bpf_context for this program type
|
|
|
|
.SS Events
|
|
|
|
Once a program is loaded, it can be attached to an event.
|
|
|
|
Various kernel subsystems have different ways to do so.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 14:45:08 +00:00
|
|
|
Since Linux 3.19,
|
|
|
|
.\" commit 89aa075832b0da4402acebd698d0411dcc82d03e
|
|
|
|
the following call will attach the program
|
2015-03-13 19:16:32 +00:00
|
|
|
.I prog_fd
|
2015-05-26 10:56:34 +00:00
|
|
|
to the socket
|
|
|
|
.IR sockfd ,
|
2015-07-22 14:45:08 +00:00
|
|
|
which was created by an earlier call to
|
|
|
|
.BR socket (2):
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-05-24 19:31:01 +00:00
|
|
|
.in +4n
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-07-22 14:45:08 +00:00
|
|
|
setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_BPF,
|
|
|
|
&prog_fd, sizeof(prog_fd));
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
2015-05-24 19:31:01 +00:00
|
|
|
.in
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 14:45:08 +00:00
|
|
|
Since Linux 4.1,
|
|
|
|
.\" commit 2541517c32be2531e0da59dfd7efc1ce844644f5
|
|
|
|
the following call may be used to attach
|
|
|
|
the eBPF program referred to by the file descriptor
|
2015-03-13 19:16:32 +00:00
|
|
|
.I prog_fd
|
2015-07-22 14:45:08 +00:00
|
|
|
to a perf event file descriptor,
|
|
|
|
.IR event_fd ,
|
|
|
|
that was created by a previous call to
|
|
|
|
.BR perf_event_open (2):
|
_syscall.2, bpf.2, cacheflush.2, capget.2, chdir.2, chmod.2, chroot.2, clock_getres.2, clock_nanosleep.2, clone.2, close.2, connect.2, copy_file_range.2, create_module.2, delete_module.2, dup.2, epoll_create.2, epoll_ctl.2, epoll_wait.2, eventfd.2, execve.2, execveat.2, fallocate.2, flock.2, fork.2, fsync.2, futex.2, futimesat.2, get_kernel_syms.2, get_mempolicy.2, get_robust_list.2, getcpu.2, getdents.2, getdomainname.2, getgid.2, getgroups.2, gethostname.2, getitimer.2, getpagesize.2, getpeername.2, getpriority.2, getrandom.2, getresuid.2, getrlimit.2, getrusage.2, getsid.2, getsockname.2, getsockopt.2, gettid.2, gettimeofday.2, getuid.2, getunwind.2, init_module.2, inotify_add_watch.2, inotify_init.2, inotify_rm_watch.2, intro.2, io_cancel.2, io_destroy.2, io_getevents.2, io_setup.2, io_submit.2, ioctl_fat.2, ioctl_ficlonerange.2, ioctl_fideduperange.2, ioctl_tty.2, ioctl_userfaultfd.2, ioperm.2, iopl.2, ioprio_set.2, kcmp.2, kexec_load.2, keyctl.2, kill.2, link.2, listen.2, listxattr.2, llseek.2, lookup_dcookie.2, lseek.2, madvise.2, mbind.2, membarrier.2, memfd_create.2, migrate_pages.2, mincore.2, mkdir.2, mknod.2, mlock.2, mmap.2, mmap2.2, modify_ldt.2, move_pages.2, mprotect.2, mq_getsetattr.2, mremap.2, msgctl.2, msgget.2, msgop.2, msync.2, nanosleep.2, nfsservctl.2, nice.2, open_by_handle_at.2, outb.2, perf_event_open.2, perfmonctl.2, personality.2, pivot_root.2, pkey_alloc.2, poll.2, posix_fadvise.2, prctl.2, pread.2, process_vm_readv.2, ptrace.2, query_module.2, quotactl.2, read.2, readahead.2, readdir.2, readv.2, reboot.2, recv.2, recvmmsg.2, remap_file_pages.2, rename.2, request_key.2, restart_syscall.2, rt_sigqueueinfo.2, s390_pci_mmio_write.2, s390_runtime_instr.2, sched_get_priority_max.2, sched_rr_get_interval.2, sched_setaffinity.2, sched_setattr.2, sched_setparam.2, sched_setscheduler.2, sched_yield.2, seccomp.2, select.2, select_tut.2, semctl.2, semget.2, semop.2, send.2, sendfile.2, sendmmsg.2, set_mempolicy.2, set_thread_area.2, set_tid_address.2, seteuid.2, setfsgid.2, setfsuid.2, setgid.2, setns.2, setpgid.2, setresuid.2, setreuid.2, setsid.2, setuid.2, sgetmask.2, shmctl.2, shmget.2, shmop.2, sigaction.2, sigaltstack.2, sigpending.2, sigprocmask.2, sigreturn.2, sigsuspend.2, sigwaitinfo.2, socket.2, socketcall.2, socketpair.2, splice.2, spu_create.2, spu_run.2, stat.2, statfs.2, statx.2, subpage_prot.2, swapon.2, symlink.2, sync.2, sync_file_range.2, syscalls.2, sysctl.2, sysinfo.2, syslog.2, tee.2, time.2, timer_create.2, timer_getoverrun.2, timer_settime.2, timerfd_create.2, times.2, tkill.2, truncate.2, umask.2, umount.2, unimplemented.2, unlink.2, unshare.2, uselib.2, userfaultfd.2, utime.2, utimensat.2, vfork.2, vmsplice.2, wait.2, wait4.2, write.2: Formatting fix: replace blank lines with .PP/.IP
Blank lines shouldn't generally appear in *roff source (other
than in code examples), since they create large vertical
spaces between text blocks.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2017-08-16 07:30:51 +00:00
|
|
|
.PP
|
2015-07-22 14:45:08 +00:00
|
|
|
.in +4n
|
locale.1, pldd.1, bpf.2, clone.2, copy_file_range.2, dup.2, execve.2, futex.2, get_kernel_syms.2, getdents.2, getrlimit.2, getunwind.2, init_module.2, ioctl_console.2, ioctl_ficlonerange.2, ioctl_fideduperange.2, ioctl_ns.2, ioctl_tty.2, ioctl_userfaultfd.2: ffix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2017-08-18 11:01:10 +00:00
|
|
|
.EX
|
2015-07-22 14:45:08 +00:00
|
|
|
ioctl(event_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
|
locale.1, pldd.1, bpf.2, clone.2, copy_file_range.2, dup.2, execve.2, futex.2, get_kernel_syms.2, getdents.2, getrlimit.2, getunwind.2, init_module.2, ioctl_console.2, ioctl_ficlonerange.2, ioctl_fideduperange.2, ioctl_ns.2, ioctl_tty.2, ioctl_userfaultfd.2: ffix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2017-08-18 11:01:10 +00:00
|
|
|
.EE
|
2015-07-22 14:45:08 +00:00
|
|
|
.in
|
|
|
|
.\"
|
|
|
|
.\"
|
2015-03-13 19:16:32 +00:00
|
|
|
.SH EXAMPLES
|
2017-08-15 10:05:21 +00:00
|
|
|
.EX
|
2015-03-13 19:16:32 +00:00
|
|
|
/* bpf+sockets example:
|
|
|
|
* 1. create array map of 256 elements
|
|
|
|
* 2. load program that counts number of packets received
|
|
|
|
* r0 = skb->data[ETH_HLEN + offsetof(struct iphdr, protocol)]
|
|
|
|
* map[r0]++
|
|
|
|
* 3. attach prog_fd to raw socket via setsockopt()
|
|
|
|
* 4. print number of received TCP/UDP packets every second
|
|
|
|
*/
|
2015-05-26 10:56:34 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2015-03-13 19:16:32 +00:00
|
|
|
{
|
|
|
|
int sock, map_fd, prog_fd, key;
|
|
|
|
long long value = 0, tcp_cnt, udp_cnt;
|
|
|
|
|
2015-05-24 19:31:01 +00:00
|
|
|
map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key),
|
|
|
|
sizeof(value), 256);
|
2015-03-13 19:16:32 +00:00
|
|
|
if (map_fd < 0) {
|
locale.1, memusage.1, pldd.1, _syscall.2, add_key.2, bind.2, bpf.2, chown.2, clone.2, copy_file_range.2, eventfd.2, execve.2, futex.2, getdents.2, getrlimit.2, ioctl_fat.2, ioctl_ns.2, kcmp.2, kexec_load.2, keyctl.2, listxattr.2, lseek.2, membarrier.2, memfd_create.2, mmap.2, mprotect.2, msgop.2, open_by_handle_at.2, perf_event_open.2, pipe.2, readdir.2, readlink.2, readv.2, recvmmsg.2, request_key.2, sched_setaffinity.2, seccomp.2, select.2, select_tut.2, sendmmsg.2, setns.2, signalfd.2, spu_run.2, stat.2, sysctl.2, tee.2, timer_create.2, timerfd_create.2, truncate.2, uname.2, unshare.2, userfaultfd.2, ustat.2, wait.2, CPU_SET.3, __ppc_get_timebase.3, argz_add.3, asprintf.3, backtrace.3, basename.3, bswap.3, bzero.3, cacos.3, cacosh.3, catan.3, catanh.3, catgets.3, clock_getcpuclockid.3, confstr.3, ctime.3, dl_iterate_phdr.3, dlinfo.3, duplocale.3, encrypt.3, end.3, endian.3, fgetws.3, fmemopen.3, fopencookie.3, fputws.3, frexp.3, ftw.3, get_nprocs_conf.3, get_phys_pages.3, getaddrinfo.3, getaddrinfo_a.3, getdate.3, getgrouplist.3, getifaddrs.3, getline.3, getlogin.3, getopt.3, getpass.3, getprotoent_r.3, getpwnam.3, getservent_r.3, getsubopt.3, glob.3, gnu_get_libc_version.3, hsearch.3, if_nameindex.3, index.3, inet.3, inet_net_pton.3, inet_pton.3, insque.3, iswblank.3, iswspace.3, makecontext.3, mallinfo.3, malloc_hook.3, malloc_info.3, mallopt.3, matherr.3, mbrtowc.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc.3, mcheck.3, memchr.3, mq_getattr.3, mq_notify.3, newlocale.3, nl_langinfo.3, offsetof.3, perror.3, posix_spawn.3, printf.3, pthread_attr_init.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_create.3, pthread_getattr_default_np.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_mutexattr_setrobust.3, pthread_setaffinity_np.3, pthread_setname_np.3, pthread_setschedparam.3, pthread_sigmask.3, qsort.3, rand.3, readdir.3, regex.3, rpmatch.3, rtime.3, scanf.3, sem_wait.3, setaliasent.3, setbuf.3, stpcpy.3, stpncpy.3, strcat.3, strchr.3, strcpy.3, strdup.3, strerror.3, strfromd.3, strftime.3, strlen.3, strnlen.3, strsep.3, strstr.3, strtok.3, strtol.3, strtoul.3, strverscmp.3, strxfrm.3, termios.3, tsearch.3, wcpcpy.3, wcpncpy.3, wcrtomb.3, wcscat.3, wcscpy.3, wcslen.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcsnrtombs.3, wcsrtombs.3, wcsstr.3, wcstok.3, wcstombs.3, wcwidth.3, wprintf.3, console_codes.4, dsp56k.4, full.4, initrd.4, loop.4, vcs.4, charmap.5, core.5, elf.5, locale.5, proc.5, repertoiremap.5, sysfs.5, termcap.5, aio.7, bpf-helpers.7, cgroups.7, charsets.7, complex.7, fanotify.7, feature_test_macros.7, inotify.7, locale.7, man-pages.7, man.7, pkeys.7, pthreads.7, rtld-audit.7, sock_diag.7, unix.7, user_namespaces.7, utf-8.7: Use '\e' rather than '\\' to get a backslash
Quoting Branden:
*roff escape sequences may sometimes look like C escapes, but that
is misleading. *roff is in part a macro language and that means
recursive expansion to arbitrary depths.
You can get away with "\\" in a context where no macro expansion
is taking place, but try to spell a literal backslash this way in
the argument to a macro and you will likely be unhappy with
results.
Try viewing the attached file with "man -l".
"\e" is the preferred and portable way to get a portable "escape
literal" going back to CSTR #54, the original Bell Labs troff
paper.
groff(7) discusses the issue:
\\ reduces to a single backslash; useful to delay its
interpretation as escape character in copy mode. For a
printable backslash, use \e, or even better \[rs], to be
independent from the current escape character.
As of groff 1.22.4, groff_man(7) does as well:
\e Widely used in man pages to represent a backslash output
glyph. It works reliably as long as the .ec request is
not used, which should never happen in man pages, and it
is slightly more portable than the more exact ‘\(rs’
(“reverse solidus”) escape sequence.
People not concerned with portability to extremely old troffs should
probably just use \(rs (or \[rs]), as it means "the backslash
glyph", not "the glyph corresponding to whatever the current escape
character is".
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-02-27 11:57:17 +00:00
|
|
|
printf("failed to create map '%s'\en", strerror(errno));
|
2015-03-13 19:16:32 +00:00
|
|
|
/* likely not run as root */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bpf_insn prog[] = {
|
2015-05-24 19:31:01 +00:00
|
|
|
BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), /* r6 = r1 */
|
|
|
|
BPF_LD_ABS(BPF_B, ETH_HLEN + offsetof(struct iphdr, protocol)),
|
|
|
|
/* r0 = ip->proto */
|
|
|
|
BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4),
|
|
|
|
/* *(u32 *)(fp - 4) = r0 */
|
|
|
|
BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), /* r2 = fp */
|
|
|
|
BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = r2 - 4 */
|
|
|
|
BPF_LD_MAP_FD(BPF_REG_1, map_fd), /* r1 = map_fd */
|
|
|
|
BPF_CALL_FUNC(BPF_FUNC_map_lookup_elem),
|
|
|
|
/* r0 = map_lookup(r1, r2) */
|
|
|
|
BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
|
|
|
|
/* if (r0 == 0) goto pc+2 */
|
|
|
|
BPF_MOV64_IMM(BPF_REG_1, 1), /* r1 = 1 */
|
|
|
|
BPF_XADD(BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0),
|
|
|
|
/* lock *(u64 *) r0 += r1 */
|
2015-07-22 17:59:35 +00:00
|
|
|
.\" == atomic64_add
|
2015-05-24 19:31:01 +00:00
|
|
|
BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */
|
|
|
|
BPF_EXIT_INSN(), /* return r0 */
|
2015-03-13 19:16:32 +00:00
|
|
|
};
|
|
|
|
|
2015-05-24 19:31:01 +00:00
|
|
|
prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, prog,
|
2019-02-09 17:32:24 +00:00
|
|
|
sizeof(prog) / sizeof(prog[0]), "GPL");
|
2015-03-13 19:16:32 +00:00
|
|
|
|
|
|
|
sock = open_raw_sock("lo");
|
|
|
|
|
2015-05-24 19:31:01 +00:00
|
|
|
assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd,
|
|
|
|
sizeof(prog_fd)) == 0);
|
2015-03-13 19:16:32 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
key = IPPROTO_TCP;
|
|
|
|
assert(bpf_lookup_elem(map_fd, &key, &tcp_cnt) == 0);
|
2017-07-05 08:50:08 +00:00
|
|
|
key = IPPROTO_UDP;
|
2015-03-13 19:16:32 +00:00
|
|
|
assert(bpf_lookup_elem(map_fd, &key, &udp_cnt) == 0);
|
locale.1, memusage.1, pldd.1, _syscall.2, add_key.2, bind.2, bpf.2, chown.2, clone.2, copy_file_range.2, eventfd.2, execve.2, futex.2, getdents.2, getrlimit.2, ioctl_fat.2, ioctl_ns.2, kcmp.2, kexec_load.2, keyctl.2, listxattr.2, lseek.2, membarrier.2, memfd_create.2, mmap.2, mprotect.2, msgop.2, open_by_handle_at.2, perf_event_open.2, pipe.2, readdir.2, readlink.2, readv.2, recvmmsg.2, request_key.2, sched_setaffinity.2, seccomp.2, select.2, select_tut.2, sendmmsg.2, setns.2, signalfd.2, spu_run.2, stat.2, sysctl.2, tee.2, timer_create.2, timerfd_create.2, truncate.2, uname.2, unshare.2, userfaultfd.2, ustat.2, wait.2, CPU_SET.3, __ppc_get_timebase.3, argz_add.3, asprintf.3, backtrace.3, basename.3, bswap.3, bzero.3, cacos.3, cacosh.3, catan.3, catanh.3, catgets.3, clock_getcpuclockid.3, confstr.3, ctime.3, dl_iterate_phdr.3, dlinfo.3, duplocale.3, encrypt.3, end.3, endian.3, fgetws.3, fmemopen.3, fopencookie.3, fputws.3, frexp.3, ftw.3, get_nprocs_conf.3, get_phys_pages.3, getaddrinfo.3, getaddrinfo_a.3, getdate.3, getgrouplist.3, getifaddrs.3, getline.3, getlogin.3, getopt.3, getpass.3, getprotoent_r.3, getpwnam.3, getservent_r.3, getsubopt.3, glob.3, gnu_get_libc_version.3, hsearch.3, if_nameindex.3, index.3, inet.3, inet_net_pton.3, inet_pton.3, insque.3, iswblank.3, iswspace.3, makecontext.3, mallinfo.3, malloc_hook.3, malloc_info.3, mallopt.3, matherr.3, mbrtowc.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc.3, mcheck.3, memchr.3, mq_getattr.3, mq_notify.3, newlocale.3, nl_langinfo.3, offsetof.3, perror.3, posix_spawn.3, printf.3, pthread_attr_init.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_create.3, pthread_getattr_default_np.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_mutexattr_setrobust.3, pthread_setaffinity_np.3, pthread_setname_np.3, pthread_setschedparam.3, pthread_sigmask.3, qsort.3, rand.3, readdir.3, regex.3, rpmatch.3, rtime.3, scanf.3, sem_wait.3, setaliasent.3, setbuf.3, stpcpy.3, stpncpy.3, strcat.3, strchr.3, strcpy.3, strdup.3, strerror.3, strfromd.3, strftime.3, strlen.3, strnlen.3, strsep.3, strstr.3, strtok.3, strtol.3, strtoul.3, strverscmp.3, strxfrm.3, termios.3, tsearch.3, wcpcpy.3, wcpncpy.3, wcrtomb.3, wcscat.3, wcscpy.3, wcslen.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcsnrtombs.3, wcsrtombs.3, wcsstr.3, wcstok.3, wcstombs.3, wcwidth.3, wprintf.3, console_codes.4, dsp56k.4, full.4, initrd.4, loop.4, vcs.4, charmap.5, core.5, elf.5, locale.5, proc.5, repertoiremap.5, sysfs.5, termcap.5, aio.7, bpf-helpers.7, cgroups.7, charsets.7, complex.7, fanotify.7, feature_test_macros.7, inotify.7, locale.7, man-pages.7, man.7, pkeys.7, pthreads.7, rtld-audit.7, sock_diag.7, unix.7, user_namespaces.7, utf-8.7: Use '\e' rather than '\\' to get a backslash
Quoting Branden:
*roff escape sequences may sometimes look like C escapes, but that
is misleading. *roff is in part a macro language and that means
recursive expansion to arbitrary depths.
You can get away with "\\" in a context where no macro expansion
is taking place, but try to spell a literal backslash this way in
the argument to a macro and you will likely be unhappy with
results.
Try viewing the attached file with "man -l".
"\e" is the preferred and portable way to get a portable "escape
literal" going back to CSTR #54, the original Bell Labs troff
paper.
groff(7) discusses the issue:
\\ reduces to a single backslash; useful to delay its
interpretation as escape character in copy mode. For a
printable backslash, use \e, or even better \[rs], to be
independent from the current escape character.
As of groff 1.22.4, groff_man(7) does as well:
\e Widely used in man pages to represent a backslash output
glyph. It works reliably as long as the .ec request is
not used, which should never happen in man pages, and it
is slightly more portable than the more exact ‘\(rs’
(“reverse solidus”) escape sequence.
People not concerned with portability to extremely old troffs should
probably just use \(rs (or \[rs]), as it means "the backslash
glyph", not "the glyph corresponding to whatever the current escape
character is".
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-02-27 11:57:17 +00:00
|
|
|
printf("TCP %lld UDP %lld packets\en", tcp_cnt, udp_cnt);
|
2015-03-13 19:16:32 +00:00
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-08-15 10:05:21 +00:00
|
|
|
.EE
|
|
|
|
.PP
|
2015-07-22 14:45:08 +00:00
|
|
|
Some complete working code can be found in the
|
2019-01-01 22:51:34 +00:00
|
|
|
.I samples/bpf
|
2015-06-09 11:13:13 +00:00
|
|
|
directory in the kernel source tree.
|
2015-03-13 19:16:32 +00:00
|
|
|
.SH RETURN VALUE
|
|
|
|
For a successful call, the return value depends on the operation:
|
|
|
|
.TP
|
|
|
|
.B BPF_MAP_CREATE
|
2015-07-22 14:45:08 +00:00
|
|
|
The new file descriptor associated with the eBPF map.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B BPF_PROG_LOAD
|
2015-06-04 11:11:19 +00:00
|
|
|
The new file descriptor associated with the eBPF program.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
All other commands
|
|
|
|
Zero.
|
|
|
|
.PP
|
|
|
|
On error, \-1 is returned, and
|
|
|
|
.I errno
|
|
|
|
is set appropriately.
|
|
|
|
.SH ERRORS
|
|
|
|
.TP
|
2019-01-01 22:51:34 +00:00
|
|
|
.B E2BIG
|
2016-08-07 16:53:26 +00:00
|
|
|
The eBPF program is too large or a map reached the
|
|
|
|
.I max_entries
|
|
|
|
limit (maximum number of elements).
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
2019-01-01 22:51:34 +00:00
|
|
|
.B EACCES
|
2016-08-07 16:53:26 +00:00
|
|
|
For
|
2019-01-01 22:51:34 +00:00
|
|
|
.BR BPF_PROG_LOAD ,
|
2016-08-07 16:53:26 +00:00
|
|
|
even though all program instructions are valid, the program has been
|
|
|
|
rejected because it was deemed unsafe.
|
|
|
|
This may be because it may have
|
|
|
|
accessed a disallowed memory region or an uninitialized stack/register or
|
|
|
|
because the function constraints don't match the actual types or because
|
|
|
|
there was a misaligned memory access.
|
|
|
|
In this case, it is recommended to call
|
|
|
|
.BR bpf ()
|
|
|
|
again with
|
|
|
|
.I log_level = 1
|
|
|
|
and examine
|
|
|
|
.I log_buf
|
|
|
|
for the specific reason provided by the verifier.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B EBADF
|
|
|
|
.I fd
|
2015-07-29 05:56:20 +00:00
|
|
|
is not an open file descriptor.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B EFAULT
|
2015-05-24 19:31:01 +00:00
|
|
|
One of the pointers
|
|
|
|
.RI ( key
|
2015-03-13 19:16:32 +00:00
|
|
|
or
|
|
|
|
.I value
|
|
|
|
or
|
|
|
|
.I log_buf
|
|
|
|
or
|
2015-05-24 19:31:01 +00:00
|
|
|
.IR insns )
|
|
|
|
is outside the accessible address space.
|
2015-03-13 19:16:32 +00:00
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
The value specified in
|
|
|
|
.I cmd
|
|
|
|
is not recognized by this kernel.
|
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
For
|
|
|
|
.BR BPF_MAP_CREATE ,
|
|
|
|
either
|
|
|
|
.I map_type
|
|
|
|
or attributes are invalid.
|
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
For
|
2019-01-01 22:51:34 +00:00
|
|
|
.B BPF_MAP_*_ELEM
|
2015-03-13 19:16:32 +00:00
|
|
|
commands,
|
2015-05-24 19:31:01 +00:00
|
|
|
some of the fields of
|
|
|
|
.I "union bpf_attr"
|
|
|
|
that are not used by this command
|
2015-03-13 19:16:32 +00:00
|
|
|
are not set to zero.
|
|
|
|
.TP
|
|
|
|
.B EINVAL
|
|
|
|
For
|
2019-01-01 22:51:34 +00:00
|
|
|
.BR BPF_PROG_LOAD ,
|
2015-05-24 10:00:38 +00:00
|
|
|
indicates an attempt to load an invalid program.
|
2015-07-23 10:11:21 +00:00
|
|
|
eBPF programs can be deemed
|
|
|
|
invalid due to unrecognized instructions, the use of reserved fields, jumps
|
2015-03-13 19:16:32 +00:00
|
|
|
out of range, infinite loops or calls of unknown functions.
|
|
|
|
.TP
|
2019-01-01 22:51:34 +00:00
|
|
|
.B ENOENT
|
2015-03-13 19:16:32 +00:00
|
|
|
For
|
|
|
|
.B BPF_MAP_LOOKUP_ELEM
|
|
|
|
or
|
2015-05-25 17:49:29 +00:00
|
|
|
.BR BPF_MAP_DELETE_ELEM ,
|
2015-03-13 19:16:32 +00:00
|
|
|
indicates that the element with the given
|
|
|
|
.I key
|
|
|
|
was not found.
|
|
|
|
.TP
|
2016-08-07 16:53:26 +00:00
|
|
|
.B ENOMEM
|
|
|
|
Cannot allocate sufficient memory.
|
|
|
|
.TP
|
|
|
|
.B EPERM
|
|
|
|
The call was made without sufficient privilege
|
|
|
|
(without the
|
|
|
|
.B CAP_SYS_ADMIN
|
|
|
|
capability).
|
2015-05-26 07:48:56 +00:00
|
|
|
.SH VERSIONS
|
|
|
|
The
|
|
|
|
.BR bpf ()
|
|
|
|
system call first appeared in Linux 3.18.
|
2015-05-26 07:49:45 +00:00
|
|
|
.SH CONFORMING TO
|
|
|
|
The
|
|
|
|
.BR bpf ()
|
|
|
|
system call is Linux-specific.
|
2015-03-13 19:16:32 +00:00
|
|
|
.SH NOTES
|
2015-05-26 10:56:34 +00:00
|
|
|
In the current implementation, all
|
|
|
|
.BR bpf ()
|
|
|
|
commands require the caller to have the
|
2015-03-13 19:16:32 +00:00
|
|
|
.B CAP_SYS_ADMIN
|
2015-05-26 10:56:34 +00:00
|
|
|
capability.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 12:53:08 +00:00
|
|
|
eBPF objects (maps and programs) can be shared between processes.
|
|
|
|
For example, after
|
|
|
|
.BR fork (2),
|
|
|
|
the child inherits file descriptors referring to the same eBPF objects.
|
|
|
|
In addition, file descriptors referring to eBPF objects can be
|
|
|
|
transferred over UNIX domain sockets.
|
|
|
|
File descriptors referring to eBPF objects can be duplicated
|
|
|
|
in the usual way, using
|
|
|
|
.BR dup (2)
|
|
|
|
and similar calls.
|
|
|
|
An eBPF object is deallocated only after all file descriptors
|
|
|
|
referring to the object have been closed.
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-22 17:59:35 +00:00
|
|
|
eBPF programs can be written in a restricted C that is compiled (using the
|
|
|
|
.B clang
|
2015-07-23 10:11:21 +00:00
|
|
|
compiler) into eBPF bytecode.
|
|
|
|
Various features are omitted from this restricted C, such as loops,
|
2015-07-22 12:53:08 +00:00
|
|
|
global variables, variadic functions, floating-point numbers,
|
2015-07-23 10:11:21 +00:00
|
|
|
and passing structures as function arguments.
|
2015-07-22 17:59:35 +00:00
|
|
|
Some examples can be found in the
|
|
|
|
.I samples/bpf/*_kern.c
|
|
|
|
files in the kernel source tree.
|
2015-07-22 14:45:08 +00:00
|
|
|
.\" There are also examples for the tc classifier, in the iproute2
|
|
|
|
.\" project, in examples/bpf
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-07-23 10:11:21 +00:00
|
|
|
The kernel contains a just-in-time (JIT) compiler that translates
|
|
|
|
eBPF bytecode into native machine code for better performance.
|
2018-03-05 14:36:26 +00:00
|
|
|
In kernels before Linux 4.15,
|
|
|
|
the JIT compiler is disabled by default,
|
2015-07-23 10:11:21 +00:00
|
|
|
but its operation can be controlled by writing one of the
|
|
|
|
following integer strings to the file
|
|
|
|
.IR /proc/sys/net/core/bpf_jit_enable :
|
|
|
|
.IP 0 3
|
|
|
|
Disable JIT compilation (default).
|
|
|
|
.IP 1
|
|
|
|
Normal compilation.
|
|
|
|
.IP 2
|
|
|
|
Debugging mode.
|
|
|
|
The generated opcodes are dumped in hexadecimal into the kernel log.
|
|
|
|
These opcodes can then be disassembled using the program
|
2019-01-01 22:51:34 +00:00
|
|
|
.I tools/net/bpf_jit_disasm.c
|
2015-07-23 10:11:21 +00:00
|
|
|
provided in the kernel source tree.
|
2015-07-23 13:36:42 +00:00
|
|
|
.PP
|
2018-03-05 14:36:26 +00:00
|
|
|
Since Linux 4.15,
|
|
|
|
.\" commit 290af86629b25ffd1ed6232c4e9107da031705cb
|
|
|
|
the kernel may configured with the
|
|
|
|
.B CONFIG_BPF_JIT_ALWAYS_ON
|
|
|
|
option.
|
|
|
|
In this case, the JIT compiler is always enabled, and the
|
|
|
|
.I bpf_jit_enable
|
|
|
|
is initialized to 1 and is immutable.
|
|
|
|
(This kernel configuration option was provided as a mitigation for
|
|
|
|
one of the Spectre attacks against the BPF interpreter.)
|
|
|
|
.PP
|
2018-03-05 14:42:18 +00:00
|
|
|
The JIT compiler for eBPF is currently
|
2018-08-09 19:06:52 +00:00
|
|
|
.\" Last reviewed in Linux 4.18-rc by grepping for BPF_ALU64 in arch/
|
2018-08-09 13:49:18 +00:00
|
|
|
.\" and by checking the documentation for bpf_jit_enable in
|
|
|
|
.\" Documentation/sysctl/net.txt
|
2018-03-05 14:42:18 +00:00
|
|
|
available for the following architectures:
|
|
|
|
.IP * 3
|
2019-04-04 09:53:38 +00:00
|
|
|
x86-64 (since Linux 3.18; cBPF since Linux 3.0);
|
|
|
|
.\" commit 0a14842f5a3c0e88a1e59fac5c3025db39721f74
|
2018-03-05 14:42:18 +00:00
|
|
|
.PD 0
|
|
|
|
.IP *
|
2019-04-04 09:53:38 +00:00
|
|
|
ARM32 (since Linux 3.18; cBPF since Linux 3.4);
|
|
|
|
.\" commit ddecdfcea0ae891f782ae853771c867ab51024c2
|
|
|
|
.IP *
|
|
|
|
SPARC 32 (since Linux 3.18; cBPF since Linux 3.5);
|
|
|
|
.\" commit 2809a2087cc44b55e4377d7b9be3f7f5d2569091
|
2018-03-05 14:42:18 +00:00
|
|
|
.IP *
|
2019-04-04 09:53:38 +00:00
|
|
|
ARM-64 (since Linux 3.18);
|
|
|
|
.\" commit e54bcde3d69d40023ae77727213d14f920eb264a
|
2018-03-05 14:42:18 +00:00
|
|
|
.IP *
|
2019-07-12 12:47:31 +00:00
|
|
|
s390 (since Linux 4.1; cBPF since Linux 3.7);
|
|
|
|
.\" commit c10302efe569bfd646b4c22df29577a4595b4580
|
|
|
|
.IP *
|
2019-04-04 09:53:38 +00:00
|
|
|
PowerPC 64 (since Linux 4.8; cBPF since Linux 3.1);
|
|
|
|
.\" commit 0ca87f05ba8bdc6791c14878464efc901ad71e99
|
|
|
|
.\" commit 156d0e290e969caba25f1851c52417c14d141b24
|
2018-03-05 14:42:18 +00:00
|
|
|
.IP *
|
|
|
|
SPARC 64 (since Linux 4.12);
|
2019-04-04 09:53:38 +00:00
|
|
|
.\" commit 7a12b5031c6b947cc13918237ae652b536243b76
|
2018-03-05 14:42:18 +00:00
|
|
|
.IP *
|
2019-04-04 09:53:38 +00:00
|
|
|
x86-32 (since Linux 4.18);
|
|
|
|
.\" commit 03f5781be2c7b7e728d724ac70ba10799cc710d7
|
2018-03-05 14:42:18 +00:00
|
|
|
.IP *
|
2019-04-04 09:53:38 +00:00
|
|
|
MIPS 64 (since Linux 4.18; cBPF since Linux 3.16);
|
|
|
|
.\" commit c6610de353da5ca6eee5b8960e838a87a90ead0c
|
|
|
|
.\" commit f381bf6d82f032b7410185b35d000ea370ac706b
|
2018-08-08 10:06:38 +00:00
|
|
|
.IP *
|
2019-04-04 09:53:38 +00:00
|
|
|
riscv (since Linux 5.1).
|
|
|
|
.\" commit 2353ecc6f91fd15b893fa01bf85a1c7a823ee4f2
|
2018-03-05 14:42:18 +00:00
|
|
|
.PD
|
2015-03-13 19:16:32 +00:00
|
|
|
.SH SEE ALSO
|
2015-05-26 10:56:34 +00:00
|
|
|
.BR seccomp (2),
|
2018-11-01 20:23:56 +00:00
|
|
|
.BR bpf-helpers (7),
|
2015-06-07 13:33:34 +00:00
|
|
|
.BR socket (7),
|
2015-07-22 15:58:46 +00:00
|
|
|
.BR tc (8),
|
|
|
|
.BR tc-bpf (8)
|
2017-08-15 10:05:21 +00:00
|
|
|
.PP
|
2015-05-24 19:47:35 +00:00
|
|
|
Both classic and extended BPF are explained in the kernel source file
|
2015-05-24 19:31:01 +00:00
|
|
|
.IR Documentation/networking/filter.txt .
|