bpf: Add bpf_seq_printf and bpf_seq_write helpers

Two helpers bpf_seq_printf and bpf_seq_write, are added for
writing data to the seq_file buffer.

bpf_seq_printf supports common format string flag/width/type
fields so at least I can get identical results for
netlink and ipv6_route targets.

For bpf_seq_printf and bpf_seq_write, return value -EOVERFLOW
specifically indicates a write failure due to overflow, which
means the object will be repeated in the next bpf invocation
if object collection stays the same. Note that if the object
collection is changed, depending how collection traversal is
done, even if the object still in the collection, it may not
be visited.

For bpf_seq_printf, format %s, %p{i,I}{4,6} needs to
read kernel memory. Reading kernel memory may fail in
the following two cases:
  - invalid kernel address, or
  - valid kernel address but requiring a major fault
If reading kernel memory failed, the %s string will be
an empty string and %p{i,I}{4,6} will be all 0.
Not returning error to bpf program is consistent with
what bpf_trace_printk() does for now.

bpf_seq_printf may return -EBUSY meaning that internal percpu
buffer for memory copy of strings or other pointees is
not available. Bpf program can return 1 to indicate it
wants the same object to be repeated. Right now, this should not
happen on no-RT kernels since migrate_disable(), which guards
bpf prog call, calls preempt_disable().

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200509175914.2476661-1-yhs@fb.com
This commit is contained in:
Yonghong Song 2020-05-09 10:59:14 -07:00 committed by Andrii Nakryiko
parent 940f4df57b
commit 7112841ade

View File

@ -3077,6 +3077,41 @@ union bpf_attr {
* See: clock_gettime(CLOCK_BOOTTIME) * See: clock_gettime(CLOCK_BOOTTIME)
* Return * Return
* Current *ktime*. * Current *ktime*.
*
* int bpf_seq_printf(struct seq_file *m, const char *fmt, u32 fmt_size, const void *data, u32 data_len)
* Description
* seq_printf uses seq_file seq_printf() to print out the format string.
* The *m* represents the seq_file. The *fmt* and *fmt_size* are for
* the format string itself. The *data* and *data_len* are format string
* arguments. The *data* are a u64 array and corresponding format string
* values are stored in the array. For strings and pointers where pointees
* are accessed, only the pointer values are stored in the *data* array.
* The *data_len* is the *data* size in term of bytes.
*
* Formats **%s**, **%p{i,I}{4,6}** requires to read kernel memory.
* Reading kernel memory may fail due to either invalid address or
* valid address but requiring a major memory fault. If reading kernel memory
* fails, the string for **%s** will be an empty string, and the ip
* address for **%p{i,I}{4,6}** will be 0. Not returning error to
* bpf program is consistent with what bpf_trace_printk() does for now.
* Return
* 0 on success, or a negative errno in case of failure.
*
* * **-EBUSY** Percpu memory copy buffer is busy, can try again
* by returning 1 from bpf program.
* * **-EINVAL** Invalid arguments, or invalid/unsupported formats.
* * **-E2BIG** Too many format specifiers.
* * **-EOVERFLOW** Overflow happens, the same object will be tried again.
*
* int bpf_seq_write(struct seq_file *m, const void *data, u32 len)
* Description
* seq_write uses seq_file seq_write() to write the data.
* The *m* represents the seq_file. The *data* and *len* represent the
* data to write in bytes.
* Return
* 0 on success, or a negative errno in case of failure.
*
* * **-EOVERFLOW** Overflow happens, the same object will be tried again.
*/ */
#define __BPF_FUNC_MAPPER(FN) \ #define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \ FN(unspec), \
@ -3204,7 +3239,9 @@ union bpf_attr {
FN(get_netns_cookie), \ FN(get_netns_cookie), \
FN(get_current_ancestor_cgroup_id), \ FN(get_current_ancestor_cgroup_id), \
FN(sk_assign), \ FN(sk_assign), \
FN(ktime_get_boot_ns), FN(ktime_get_boot_ns), \
FN(seq_printf), \
FN(seq_write),
/* integer value in 'imm' field of BPF_CALL instruction selects which helper /* integer value in 'imm' field of BPF_CALL instruction selects which helper
* function eBPF program intends to call * function eBPF program intends to call