Hello Alexei,
I took the draft 3 of the bpf(2) man page that you sent back in March
and did some substantial editing to clarify the language and add a
few technical details. Could you please check the revised version
below, to ensure I did not inject any errors.
I also added a number of FIXMEs for pieces of the page that need
further work. Could you take a look at these and let me know your
thoughts, please.
.SH DESCRIPTION
The
.BR bpf ()
system call performs a range of operations related to extended
Berkeley Packet Filters.
Extended BPF (or eBPF) is similar to
the original BPF (or classic BPF) used to filter network packets.
For both BPF and eBPF programs,
.\" FIXME In the next line, what is "a restricted C"? Where does
.\" one get further information about it?
.\" FIXME In the following sentence, what does "takes hold" mean?
During verification, the program takes hold of maps that it intends to use,
so selected maps cannot be removed until the program is unloaded.
BPF programs can be attached to different events.
.\" FIXME: In the next sentence , "packets" are not "events". What
.\" do you really mean to say here? ("the arrival of a network packet"?)
These events can be packets, tracing
events, and other types that may be added in the future.
.\" FIXME Can maps be shared between processes? (E.g., what happens
.\" when fork() is called?)
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 */
};
bpf_create_map(enum bpf_map_type map_type, int key_size,
int value_size, int max_entries)
{
union bpf_attr attr = {
.map_type = map_type,
.key_size = key_size,
.value_size = value_size,
.max_entries = max_entries
};
return bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
}
Currently, two
.I map_type
are supported:
.in +4n
.nf
enum bpf_map_type {
BPF_MAP_TYPE_UNSPEC,
BPF_MAP_TYPE_HASH,
BPF_MAP_TYPE_ARRAY,
.\" FIXME Explain the purpose of BPF_MAP_TYPE_UNSPEC
.\" FIXME We need an explanation of BPF_MAP_TYPE_HASH here
.\" FIXME We need an explanation of BPF_MAP_TYPE_ARRAY here
.\" FIXME We need an explanation of why one might choose HASH versus ARRAY
.\" FIXME Here, I think we need some statement about what 'value' must
.\" point to. Presumable, it must be a buffer at least as large as
.\" the map's 'value_size' attribute?
bpf_get_next_key(int fd, void *key, void *next_key)
{
union bpf_attr attr = {
.map_fd = fd,
.key = ptr_to_u64(key),
.next_key = ptr_to_u64(next_key),
};
return bpf(BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr));
}
.fi
.in
.\" FIXME Need to explain the return value on success here.
When the user-space program that created a map exits, all maps will
be deleted automatically.
.\" FIXME What are the semantics when a file descriptor is duplicated
.\" (dup() etc.)? (I.e., when is a map deallocated automatically?)
enum bpf_prog_type {
BPF_PROG_TYPE_UNSPEC,
.\" FIXME Explain the purpose of BPF_PROG_TYPE_UNSPEC
BPF_PROG_TYPE_SOCKET_FILTER,
BPF_PROG_TYPE_SCHED_CLS,
.\" FIXME BPF_PROG_TYPE_SCHED_CLS appears not to exist?
.\" FIXME The next sentence fragment is incomplete
and
.I bpf_context
is a pointer to a
.IR "struct sk_buff" .
Programs cannot access fields of
.I sk_buff
directly.
More program types may be added in the future.
.\" FIXME The following sentence is grammatically broken.
.\" What should it say?
Like
.B BPF_PROG_TYPE_KPROBE
and
.I bpf_context
for it may be defined as a pointer to a
.IR "struct pt_regs" .
is a license string, which must be GPL compatible to call helper functions
.\" FIXME Maybe we should list the GPL compatible strings that can be
.\" specified?
Maps are accessible from BPF programs and are used to exchange data between
BPF programs and between BPF programs and user-space programs.
Programs process various events (like kprobe, packets) and
store their data into maps.
User-space programs fetch data from the maps.
.\" FIXME We need some elaboration here... What does the next sentence mean?
Either the same or a different map may be used by user space
as a configuration space to alter program behavior on the fly.
.SH EXAMPLES
.\" FIXME It would be nice if this was a complete working example
BPF_MOV64_IMM(BPF_REG_1, 1), /* r1 = 1 */
BPF_XADD(BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0),
.\" FIXME What does 'lock' in the line below mean?
/* lock *(u64 *) r0 += r1 */
BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */
BPF_EXIT_INSN(), /* return r0 */
};
prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, prog,
.\" FIXME The next line looks wrong. Should it not be
.\"
.\" sizeof(prog) / sizeof(struct bpf_insn) ?
sizeof(prog), "GPL");