History

bpf: add documentation for eBPF helpers (01-11)

Add documentation for eBPF helper functions to bpf.h user header file. This documentation can be parsed with the Python script provided in another commit of the patch series, in order to provide a RST document that can later be converted into a man page. The objective is to make the documentation easily understandable and accessible to all eBPF developers, including beginners. This patch contains descriptions for the following helper functions, all written by Alexei: - bpf_map_lookup_elem() - bpf_map_update_elem() - bpf_map_delete_elem() - bpf_probe_read() - bpf_ktime_get_ns() - bpf_trace_printk() - bpf_skb_store_bytes() - bpf_l3_csum_replace() - bpf_l4_csum_replace() - bpf_tail_call() - bpf_clone_redirect() v4: - bpf_map_lookup_elem(): Add "const" qualifier for key. - bpf_map_update_elem(): Add "const" qualifier for key and value. - bpf_map_lookup_elem(): Add "const" qualifier for key. - bpf_skb_store_bytes(): Clarify comment about invalidated verifier checks. - bpf_l3_csum_replace(): Mention L3 instead of just IP, and add a note about bpf_csum_diff(). - bpf_l4_csum_replace(): Mention L4 instead of just TCP/UDP, and add a note about bpf_csum_diff(). - bpf_tail_call(): Bring minor edits to description. - bpf_clone_redirect(): Add a note about the relation with bpf_redirect(). Also clarify comment about invalidated verifier checks. v3: - bpf_map_lookup_elem(): Fix description of restrictions for flags related to the existence of the entry. - bpf_trace_printk(): State that trace_pipe can be configured. Fix return value in case an unknown format specifier is met. Add a note on kernel log notice when the helper is used. Edit example. - bpf_tail_call(): Improve comment on stack inheritance. - bpf_clone_redirect(): Improve description of BPF_F_INGRESS flag.

Quentin Monnet committed 8 years ago

Commit ad4a5221 parent 56a092c1 file · +230 −0Browse files

Showing the changes to include/uapi/linux (1 of 1 file). Show all

Unified · Split

include/uapi/linux/bpf.h+230−0
@@ -393,6 +393,236 @@ union bpf_attr { · 392 unmodified lines
393393 * intentional, removing them would break paragraphs for rst2man.
394394 *
395395 * Start of BPF helper function descriptions:
396+ *
397+ * void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)
398+ * Description
399+ * Perform a lookup in *map* for an entry associated to *key*.
400+ * Return
401+ * Map value associated to *key*, or **NULL** if no entry was
402+ * found.
403+ *
404+ * int bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags)
405+ * Description
406+ * Add or update the value of the entry associated to *key* in
407+ * *map* with *value*. *flags* is one of:
408+ *
409+ * **BPF_NOEXIST**
410+ * The entry for *key* must not exist in the map.
411+ * **BPF_EXIST**
412+ * The entry for *key* must already exist in the map.
413+ * **BPF_ANY**
414+ * No condition on the existence of the entry for *key*.
415+ *
416+ * Flag value **BPF_NOEXIST** cannot be used for maps of types
417+ * **BPF_MAP_TYPE_ARRAY** or **BPF_MAP_TYPE_PERCPU_ARRAY** (all
418+ * elements always exist), the helper would return an error.
419+ * Return
420+ * 0 on success, or a negative error in case of failure.
421+ *
422+ * int bpf_map_delete_elem(struct bpf_map *map, const void *key)
423+ * Description
424+ * Delete entry with *key* from *map*.
425+ * Return
426+ * 0 on success, or a negative error in case of failure.
427+ *
428+ * int bpf_probe_read(void *dst, u32 size, const void *src)
429+ * Description
430+ * For tracing programs, safely attempt to read *size* bytes from
431+ * address *src* and store the data in *dst*.
432+ * Return
433+ * 0 on success, or a negative error in case of failure.
434+ *
435+ * u64 bpf_ktime_get_ns(void)
436+ * Description
437+ * Return the time elapsed since system boot, in nanoseconds.
438+ * Return
439+ * Current *ktime*.
440+ *
441+ * int bpf_trace_printk(const char *fmt, u32 fmt_size, ...)
442+ * Description
443+ * This helper is a "printk()-like" facility for debugging. It
444+ * prints a message defined by format *fmt* (of size *fmt_size*)
445+ * to file *\/sys/kernel/debug/tracing/trace* from DebugFS, if
446+ * available. It can take up to three additional **u64**
447+ * arguments (as an eBPF helpers, the total number of arguments is
448+ * limited to five).
449+ *
450+ * Each time the helper is called, it appends a line to the trace.
451+ * The format of the trace is customizable, and the exact output
452+ * one will get depends on the options set in
453+ * *\/sys/kernel/debug/tracing/trace_options* (see also the
454+ * *README* file under the same directory). However, it usually
455+ * defaults to something like:
456+ *
457+ * ::
458+ *
459+ * telnet-470 [001] .N.. 419421.045894: 0x00000001: <formatted msg>
460+ *
461+ * In the above:
462+ *
463+ * * ``telnet`` is the name of the current task.
464+ * * ``470`` is the PID of the current task.
465+ * * ``001`` is the CPU number on which the task is
466+ * running.
467+ * * In ``.N..``, each character refers to a set of
468+ * options (whether irqs are enabled, scheduling
469+ * options, whether hard/softirqs are running, level of
470+ * preempt_disabled respectively). **N** means that
471+ * **TIF_NEED_RESCHED** and **PREEMPT_NEED_RESCHED**
472+ * are set.
473+ * * ``419421.045894`` is a timestamp.
474+ * * ``0x00000001`` is a fake value used by BPF for the
475+ * instruction pointer register.
476+ * * ``<formatted msg>`` is the message formatted with
477+ * *fmt*.
478+ *
479+ * The conversion specifiers supported by *fmt* are similar, but
480+ * more limited than for printk(). They are **%d**, **%i**,
481+ * **%u**, **%x**, **%ld**, **%li**, **%lu**, **%lx**, **%lld**,
482+ * **%lli**, **%llu**, **%llx**, **%p**, **%s**. No modifier (size
483+ * of field, padding with zeroes, etc.) is available, and the
484+ * helper will return **-EINVAL** (but print nothing) if it
485+ * encounters an unknown specifier.
486+ *
487+ * Also, note that **bpf_trace_printk**\ () is slow, and should
488+ * only be used for debugging purposes. For this reason, a notice
489+ * bloc (spanning several lines) is printed to kernel logs and
490+ * states that the helper should not be used "for production use"
491+ * the first time this helper is used (or more precisely, when
492+ * **trace_printk**\ () buffers are allocated). For passing values
493+ * to user space, perf events should be preferred.
494+ * Return
495+ * The number of bytes written to the buffer, or a negative error
496+ * in case of failure.
497+ *
498+ * int bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len, u64 flags)
499+ * Description
500+ * Store *len* bytes from address *from* into the packet
501+ * associated to *skb*, at *offset*. *flags* are a combination of
502+ * **BPF_F_RECOMPUTE_CSUM** (automatically recompute the
503+ * checksum for the packet after storing the bytes) and
504+ * **BPF_F_INVALIDATE_HASH** (set *skb*\ **->hash**, *skb*\
505+ * **->swhash** and *skb*\ **->l4hash** to 0).
506+ *
507+ * A call to this helper is susceptible to change the underlaying
508+ * packet buffer. Therefore, at load time, all checks on pointers
509+ * previously done by the verifier are invalidated and must be
510+ * performed again, if the helper is used in combination with
511+ * direct packet access.
512+ * Return
513+ * 0 on success, or a negative error in case of failure.
514+ *
515+ * int bpf_l3_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 size)
516+ * Description
517+ * Recompute the layer 3 (e.g. IP) checksum for the packet
518+ * associated to *skb*. Computation is incremental, so the helper
519+ * must know the former value of the header field that was
520+ * modified (*from*), the new value of this field (*to*), and the
521+ * number of bytes (2 or 4) for this field, stored in *size*.
522+ * Alternatively, it is possible to store the difference between
523+ * the previous and the new values of the header field in *to*, by
524+ * setting *from* and *size* to 0. For both methods, *offset*
525+ * indicates the location of the IP checksum within the packet.
526+ *
527+ * This helper works in combination with **bpf_csum_diff**\ (),
528+ * which does not update the checksum in-place, but offers more
529+ * flexibility and can handle sizes larger than 2 or 4 for the
530+ * checksum to update.
531+ *
532+ * A call to this helper is susceptible to change the underlaying
533+ * packet buffer. Therefore, at load time, all checks on pointers
534+ * previously done by the verifier are invalidated and must be
535+ * performed again, if the helper is used in combination with
536+ * direct packet access.
537+ * Return
538+ * 0 on success, or a negative error in case of failure.
539+ *
540+ * int bpf_l4_csum_replace(struct sk_buff *skb, u32 offset, u64 from, u64 to, u64 flags)
541+ * Description
542+ * Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the
543+ * packet associated to *skb*. Computation is incremental, so the
544+ * helper must know the former value of the header field that was
545+ * modified (*from*), the new value of this field (*to*), and the
546+ * number of bytes (2 or 4) for this field, stored on the lowest
547+ * four bits of *flags*. Alternatively, it is possible to store
548+ * the difference between the previous and the new values of the
549+ * header field in *to*, by setting *from* and the four lowest
550+ * bits of *flags* to 0. For both methods, *offset* indicates the
551+ * location of the IP checksum within the packet. In addition to
552+ * the size of the field, *flags* can be added (bitwise OR) actual
553+ * flags. With **BPF_F_MARK_MANGLED_0**, a null checksum is left
554+ * untouched (unless **BPF_F_MARK_ENFORCE** is added as well), and
555+ * for updates resulting in a null checksum the value is set to
556+ * **CSUM_MANGLED_0** instead. Flag **BPF_F_PSEUDO_HDR** indicates
557+ * the checksum is to be computed against a pseudo-header.
558+ *
559+ * This helper works in combination with **bpf_csum_diff**\ (),
560+ * which does not update the checksum in-place, but offers more
561+ * flexibility and can handle sizes larger than 2 or 4 for the
562+ * checksum to update.
563+ *
564+ * A call to this helper is susceptible to change the underlaying
565+ * packet buffer. Therefore, at load time, all checks on pointers
566+ * previously done by the verifier are invalidated and must be
567+ * performed again, if the helper is used in combination with
568+ * direct packet access.
569+ * Return
570+ * 0 on success, or a negative error in case of failure.
571+ *
572+ * int bpf_tail_call(void *ctx, struct bpf_map *prog_array_map, u32 index)
573+ * Description
574+ * This special helper is used to trigger a "tail call", or in
575+ * other words, to jump into another eBPF program. The same stack
576+ * frame is used (but values on stack and in registers for the
577+ * caller are not accessible to the callee). This mechanism allows
578+ * for program chaining, either for raising the maximum number of
579+ * available eBPF instructions, or to execute given programs in
580+ * conditional blocks. For security reasons, there is an upper
581+ * limit to the number of successive tail calls that can be
582+ * performed.
583+ *
584+ * Upon call of this helper, the program attempts to jump into a
585+ * program referenced at index *index* in *prog_array_map*, a
586+ * special map of type **BPF_MAP_TYPE_PROG_ARRAY**, and passes
587+ * *ctx*, a pointer to the context.
588+ *
589+ * If the call succeeds, the kernel immediately runs the first
590+ * instruction of the new program. This is not a function call,
591+ * and it never returns to the previous program. If the call
592+ * fails, then the helper has no effect, and the caller continues
593+ * to run its subsequent instructions. A call can fail if the
594+ * destination program for the jump does not exist (i.e. *index*
595+ * is superior to the number of entries in *prog_array_map*), or
596+ * if the maximum number of tail calls has been reached for this
597+ * chain of programs. This limit is defined in the kernel by the
598+ * macro **MAX_TAIL_CALL_CNT** (not accessible to user space),
599+ * which is currently set to 32.
600+ * Return
601+ * 0 on success, or a negative error in case of failure.
602+ *
603+ * int bpf_clone_redirect(struct sk_buff *skb, u32 ifindex, u64 flags)
604+ * Description
605+ * Clone and redirect the packet associated to *skb* to another
606+ * net device of index *ifindex*. Both ingress and egress
607+ * interfaces can be used for redirection. The **BPF_F_INGRESS**
608+ * value in *flags* is used to make the distinction (ingress path
609+ * is selected if the flag is present, egress path otherwise).
610+ * This is the only flag supported for now.
611+ *
612+ * In comparison with **bpf_redirect**\ () helper,
613+ * **bpf_clone_redirect**\ () has the associated cost of
614+ * duplicating the packet buffer, but this can be executed out of
615+ * the eBPF program. Conversely, **bpf_redirect**\ () is more
616+ * efficient, but it is handled through an action code where the
617+ * redirection happens only after the eBPF program has returned.
618+ *
619+ * A call to this helper is susceptible to change the underlaying
620+ * packet buffer. Therefore, at load time, all checks on pointers
621+ * previously done by the verifier are invalidated and must be
622+ * performed again, if the helper is used in combination with
623+ * direct packet access.
624+ * Return
625+ * 0 on success, or a negative error in case of failure.
396626 */
397627 #define __BPF_FUNC_MAPPER(FN) \
398628 FN(unspec), \