]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/bpf-helpers.7
standards.7: Add some more standards
[thirdparty/man-pages.git] / man7 / bpf-helpers.7
CommitLineData
53666f6c 1.\" Man page generated from reStructuredText.
e6107b29 2.
4e2b112c 3.TH BPF-HELPERS 7 "" "" ""
53666f6c
MK
4.SH NAME
5BPF-HELPERS \- list of eBPF helper functions
e6107b29 6.
53666f6c 7.nr rst2man-indent-level 0
e6107b29 8.
53666f6c
MK
9.de1 rstReportMargin
10\\$1 \\n[an-margin]
11level \\n[rst2man-indent-level]
12level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
e6107b29 13-
53666f6c
MK
14\\n[rst2man-indent0]
15\\n[rst2man-indent1]
16\\n[rst2man-indent2]
17..
18.de1 INDENT
19.\" .rstReportMargin pre:
20. RS \\$1
21. nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
22. nr rst2man-indent-level +1
23.\" .rstReportMargin post:
24..
25.de UNINDENT
26. RE
27.\" indent \\n[an-margin]
28.\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
29.nr rst2man-indent-level -1
30.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
31.in \\n[rst2man-indent\\n[rst2man-indent-level]]u
32..
e6107b29 33.\" Copyright (C) All BPF authors and contributors from 2014 to present.
e6107b29 34.\" See git log include/uapi/linux/bpf.h in kernel tree for details.
4e2b112c 35.\"
e6107b29 36.\" %%%LICENSE_START(VERBATIM)
e6107b29 37.\" Permission is granted to make and distribute verbatim copies of this
e6107b29 38.\" manual provided the copyright notice and this permission notice are
e6107b29 39.\" preserved on all copies.
4e2b112c 40.\"
e6107b29 41.\" Permission is granted to copy and distribute modified versions of this
e6107b29 42.\" manual under the conditions for verbatim copying, provided that the
e6107b29 43.\" entire resulting derived work is distributed under the terms of a
e6107b29 44.\" permission notice identical to this one.
4e2b112c 45.\"
e6107b29 46.\" Since the Linux kernel and libraries are constantly changing, this
e6107b29 47.\" manual page may be incorrect or out-of-date. The author(s) assume no
e6107b29 48.\" responsibility for errors or omissions, or for damages resulting from
e6107b29 49.\" the use of the information contained herein. The author(s) may not
e6107b29 50.\" have taken the same level of care in the production of this manual,
e6107b29 51.\" which is licensed free of charge, as they might when working
e6107b29 52.\" professionally.
4e2b112c 53.\"
e6107b29 54.\" Formatted or processed versions of this manual, if unaccompanied by
e6107b29 55.\" the source, must acknowledge the copyright and authors of this work.
e6107b29 56.\" %%%LICENSE_END
4e2b112c 57.\"
e6107b29 58.\" Please do not edit this file. It was generated from the documentation
e6107b29 59.\" located in file include/uapi/linux/bpf.h of the Linux kernel sources
e6107b29 60.\" (helpers description), and from scripts/bpf_helpers_doc.py in the same
e6107b29 61.\" repository (header and footer).
4e2b112c 62.
53666f6c
MK
63.SH DESCRIPTION
64.sp
65The extended Berkeley Packet Filter (eBPF) subsystem consists in programs
66written in a pseudo\-assembly language, then attached to one of the several
67kernel hooks and run in reaction of specific events. This framework differs
68from the older, "classic" BPF (or "cBPF") in several aspects, one of them being
69the ability to call special functions (or "helpers") from within a program.
70These functions are restricted to a white\-list of helpers defined in the
71kernel.
72.sp
73These helpers are used by eBPF programs to interact with the system, or with
74the context in which they work. For instance, they can be used to print
75debugging messages, to get the time since the system was booted, to interact
76with eBPF maps, or to manipulate network packets. Since there are several eBPF
77program types, and that they do not run in the same context, each program type
78can only call a subset of those helpers.
79.sp
80Due to eBPF conventions, a helper can not have more than five arguments.
81.sp
82Internally, eBPF programs call directly into the compiled helper functions
83without requiring any foreign\-function interface. As a result, calling helpers
84introduces no overhead, thus offering excellent performance.
85.sp
86This document is an attempt to list and document the helpers available to eBPF
87developers. They are sorted by chronological order (the oldest helpers in the
88kernel at the top).
89.SH HELPERS
90.INDENT 0.0
91.TP
92.B \fBvoid *bpf_map_lookup_elem(struct bpf_map *\fP\fImap\fP\fB, const void *\fP\fIkey\fP\fB)\fP
93.INDENT 7.0
94.TP
95.B Description
96Perform a lookup in \fImap\fP for an entry associated to \fIkey\fP\&.
97.TP
98.B Return
99Map value associated to \fIkey\fP, or \fBNULL\fP if no entry was
100found.
101.UNINDENT
102.TP
103.B \fBint bpf_map_update_elem(struct bpf_map *\fP\fImap\fP\fB, const void *\fP\fIkey\fP\fB, const void *\fP\fIvalue\fP\fB, u64\fP \fIflags\fP\fB)\fP
104.INDENT 7.0
105.TP
106.B Description
107Add or update the value of the entry associated to \fIkey\fP in
108\fImap\fP with \fIvalue\fP\&. \fIflags\fP is one of:
109.INDENT 7.0
110.TP
111.B \fBBPF_NOEXIST\fP
112The entry for \fIkey\fP must not exist in the map.
113.TP
114.B \fBBPF_EXIST\fP
115The entry for \fIkey\fP must already exist in the map.
116.TP
117.B \fBBPF_ANY\fP
118No condition on the existence of the entry for \fIkey\fP\&.
119.UNINDENT
120.sp
121Flag value \fBBPF_NOEXIST\fP cannot be used for maps of types
122\fBBPF_MAP_TYPE_ARRAY\fP or \fBBPF_MAP_TYPE_PERCPU_ARRAY\fP (all
123elements always exist), the helper would return an error.
124.TP
125.B Return
1260 on success, or a negative error in case of failure.
127.UNINDENT
128.TP
129.B \fBint bpf_map_delete_elem(struct bpf_map *\fP\fImap\fP\fB, const void *\fP\fIkey\fP\fB)\fP
130.INDENT 7.0
131.TP
132.B Description
133Delete entry with \fIkey\fP from \fImap\fP\&.
134.TP
135.B Return
1360 on success, or a negative error in case of failure.
137.UNINDENT
138.TP
4e2b112c 139.B \fBint bpf_probe_read(void *\fP\fIdst\fP\fB, u32\fP \fIsize\fP\fB, const void *\fP\fIunsafe_ptr\fP\fB)\fP
53666f6c
MK
140.INDENT 7.0
141.TP
142.B Description
143For tracing programs, safely attempt to read \fIsize\fP bytes from
4e2b112c
MK
144kernel space address \fIunsafe_ptr\fP and store the data in \fIdst\fP\&.
145.sp
146Generally, use bpf_probe_read_user() or bpf_probe_read_kernel()
147instead.
53666f6c
MK
148.TP
149.B Return
1500 on success, or a negative error in case of failure.
151.UNINDENT
152.TP
153.B \fBu64 bpf_ktime_get_ns(void)\fP
154.INDENT 7.0
155.TP
156.B Description
157Return the time elapsed since system boot, in nanoseconds.
158.TP
159.B Return
160Current \fIktime\fP\&.
161.UNINDENT
162.TP
163.B \fBint bpf_trace_printk(const char *\fP\fIfmt\fP\fB, u32\fP \fIfmt_size\fP\fB, ...)\fP
164.INDENT 7.0
165.TP
166.B Description
167This helper is a "printk()\-like" facility for debugging. It
168prints a message defined by format \fIfmt\fP (of size \fIfmt_size\fP)
169to file \fI/sys/kernel/debug/tracing/trace\fP from DebugFS, if
170available. It can take up to three additional \fBu64\fP
171arguments (as an eBPF helpers, the total number of arguments is
172limited to five).
173.sp
174Each time the helper is called, it appends a line to the trace.
e6107b29
MK
175Lines are discarded while \fI/sys/kernel/debug/tracing/trace\fP is
176open, use \fI/sys/kernel/debug/tracing/trace_pipe\fP to avoid this.
53666f6c
MK
177The format of the trace is customizable, and the exact output
178one will get depends on the options set in
179\fI/sys/kernel/debug/tracing/trace_options\fP (see also the
180\fIREADME\fP file under the same directory). However, it usually
181defaults to something like:
182.INDENT 7.0
183.INDENT 3.5
184.sp
185.nf
186.ft C
187telnet\-470 [001] .N.. 419421.045894: 0x00000001: <formatted msg>
188.ft P
189.fi
190.UNINDENT
191.UNINDENT
192.sp
193In the above:
194.INDENT 7.0
195.INDENT 3.5
196.INDENT 0.0
197.IP \(bu 2
198\fBtelnet\fP is the name of the current task.
199.IP \(bu 2
200\fB470\fP is the PID of the current task.
201.IP \(bu 2
202\fB001\fP is the CPU number on which the task is
203running.
204.IP \(bu 2
205In \fB\&.N..\fP, each character refers to a set of
206options (whether irqs are enabled, scheduling
207options, whether hard/softirqs are running, level of
208preempt_disabled respectively). \fBN\fP means that
209\fBTIF_NEED_RESCHED\fP and \fBPREEMPT_NEED_RESCHED\fP
210are set.
211.IP \(bu 2
212\fB419421.045894\fP is a timestamp.
213.IP \(bu 2
214\fB0x00000001\fP is a fake value used by BPF for the
215instruction pointer register.
216.IP \(bu 2
217\fB<formatted msg>\fP is the message formatted with
218\fIfmt\fP\&.
219.UNINDENT
220.UNINDENT
221.UNINDENT
222.sp
223The conversion specifiers supported by \fIfmt\fP are similar, but
224more limited than for printk(). They are \fB%d\fP, \fB%i\fP,
225\fB%u\fP, \fB%x\fP, \fB%ld\fP, \fB%li\fP, \fB%lu\fP, \fB%lx\fP, \fB%lld\fP,
226\fB%lli\fP, \fB%llu\fP, \fB%llx\fP, \fB%p\fP, \fB%s\fP\&. No modifier (size
227of field, padding with zeroes, etc.) is available, and the
228helper will return \fB\-EINVAL\fP (but print nothing) if it
229encounters an unknown specifier.
230.sp
231Also, note that \fBbpf_trace_printk\fP() is slow, and should
232only be used for debugging purposes. For this reason, a notice
233bloc (spanning several lines) is printed to kernel logs and
234states that the helper should not be used "for production use"
235the first time this helper is used (or more precisely, when
236\fBtrace_printk\fP() buffers are allocated). For passing values
237to user space, perf events should be preferred.
238.TP
239.B Return
240The number of bytes written to the buffer, or a negative error
241in case of failure.
242.UNINDENT
243.TP
244.B \fBu32 bpf_get_prandom_u32(void)\fP
245.INDENT 7.0
246.TP
247.B Description
248Get a pseudo\-random number.
249.sp
250From a security point of view, this helper uses its own
251pseudo\-random internal state, and cannot be used to infer the
252seed of other random functions in the kernel. However, it is
253essential to note that the generator used by the helper is not
254cryptographically secure.
255.TP
256.B Return
257A random 32\-bit unsigned value.
258.UNINDENT
259.TP
260.B \fBu32 bpf_get_smp_processor_id(void)\fP
261.INDENT 7.0
262.TP
263.B Description
264Get the SMP (symmetric multiprocessing) processor id. Note that
265all programs run with preemption disabled, which means that the
266SMP processor id is stable during all the execution of the
267program.
268.TP
269.B Return
270The SMP id of the processor running the program.
271.UNINDENT
272.TP
273.B \fBint bpf_skb_store_bytes(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIoffset\fP\fB, const void *\fP\fIfrom\fP\fB, u32\fP \fIlen\fP\fB, u64\fP \fIflags\fP\fB)\fP
274.INDENT 7.0
275.TP
276.B Description
277Store \fIlen\fP bytes from address \fIfrom\fP into the packet
278associated to \fIskb\fP, at \fIoffset\fP\&. \fIflags\fP are a combination of
279\fBBPF_F_RECOMPUTE_CSUM\fP (automatically recompute the
280checksum for the packet after storing the bytes) and
281\fBBPF_F_INVALIDATE_HASH\fP (set \fIskb\fP\fB\->hash\fP, \fIskb\fP\fB\->swhash\fP and \fIskb\fP\fB\->l4hash\fP to 0).
282.sp
e6107b29 283A call to this helper is susceptible to change the underlying
53666f6c
MK
284packet buffer. Therefore, at load time, all checks on pointers
285previously done by the verifier are invalidated and must be
286performed again, if the helper is used in combination with
287direct packet access.
288.TP
289.B Return
2900 on success, or a negative error in case of failure.
291.UNINDENT
292.TP
293.B \fBint bpf_l3_csum_replace(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIoffset\fP\fB, u64\fP \fIfrom\fP\fB, u64\fP \fIto\fP\fB, u64\fP \fIsize\fP\fB)\fP
294.INDENT 7.0
295.TP
296.B Description
297Recompute the layer 3 (e.g. IP) checksum for the packet
298associated to \fIskb\fP\&. Computation is incremental, so the helper
299must know the former value of the header field that was
300modified (\fIfrom\fP), the new value of this field (\fIto\fP), and the
301number of bytes (2 or 4) for this field, stored in \fIsize\fP\&.
302Alternatively, it is possible to store the difference between
303the previous and the new values of the header field in \fIto\fP, by
304setting \fIfrom\fP and \fIsize\fP to 0. For both methods, \fIoffset\fP
305indicates the location of the IP checksum within the packet.
306.sp
307This helper works in combination with \fBbpf_csum_diff\fP(),
308which does not update the checksum in\-place, but offers more
309flexibility and can handle sizes larger than 2 or 4 for the
310checksum to update.
311.sp
e6107b29 312A call to this helper is susceptible to change the underlying
53666f6c
MK
313packet buffer. Therefore, at load time, all checks on pointers
314previously done by the verifier are invalidated and must be
315performed again, if the helper is used in combination with
316direct packet access.
317.TP
318.B Return
3190 on success, or a negative error in case of failure.
320.UNINDENT
321.TP
322.B \fBint bpf_l4_csum_replace(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIoffset\fP\fB, u64\fP \fIfrom\fP\fB, u64\fP \fIto\fP\fB, u64\fP \fIflags\fP\fB)\fP
323.INDENT 7.0
324.TP
325.B Description
326Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the
327packet associated to \fIskb\fP\&. Computation is incremental, so the
328helper must know the former value of the header field that was
329modified (\fIfrom\fP), the new value of this field (\fIto\fP), and the
330number of bytes (2 or 4) for this field, stored on the lowest
331four bits of \fIflags\fP\&. Alternatively, it is possible to store
332the difference between the previous and the new values of the
333header field in \fIto\fP, by setting \fIfrom\fP and the four lowest
334bits of \fIflags\fP to 0. For both methods, \fIoffset\fP indicates the
335location of the IP checksum within the packet. In addition to
336the size of the field, \fIflags\fP can be added (bitwise OR) actual
337flags. With \fBBPF_F_MARK_MANGLED_0\fP, a null checksum is left
338untouched (unless \fBBPF_F_MARK_ENFORCE\fP is added as well), and
339for updates resulting in a null checksum the value is set to
340\fBCSUM_MANGLED_0\fP instead. Flag \fBBPF_F_PSEUDO_HDR\fP indicates
341the checksum is to be computed against a pseudo\-header.
342.sp
343This helper works in combination with \fBbpf_csum_diff\fP(),
344which does not update the checksum in\-place, but offers more
345flexibility and can handle sizes larger than 2 or 4 for the
346checksum to update.
347.sp
e6107b29 348A call to this helper is susceptible to change the underlying
53666f6c
MK
349packet buffer. Therefore, at load time, all checks on pointers
350previously done by the verifier are invalidated and must be
351performed again, if the helper is used in combination with
352direct packet access.
353.TP
354.B Return
3550 on success, or a negative error in case of failure.
356.UNINDENT
357.TP
358.B \fBint bpf_tail_call(void *\fP\fIctx\fP\fB, struct bpf_map *\fP\fIprog_array_map\fP\fB, u32\fP \fIindex\fP\fB)\fP
359.INDENT 7.0
360.TP
361.B Description
362This special helper is used to trigger a "tail call", or in
363other words, to jump into another eBPF program. The same stack
364frame is used (but values on stack and in registers for the
365caller are not accessible to the callee). This mechanism allows
366for program chaining, either for raising the maximum number of
367available eBPF instructions, or to execute given programs in
368conditional blocks. For security reasons, there is an upper
369limit to the number of successive tail calls that can be
370performed.
371.sp
372Upon call of this helper, the program attempts to jump into a
373program referenced at index \fIindex\fP in \fIprog_array_map\fP, a
374special map of type \fBBPF_MAP_TYPE_PROG_ARRAY\fP, and passes
375\fIctx\fP, a pointer to the context.
376.sp
377If the call succeeds, the kernel immediately runs the first
378instruction of the new program. This is not a function call,
379and it never returns to the previous program. If the call
380fails, then the helper has no effect, and the caller continues
381to run its subsequent instructions. A call can fail if the
382destination program for the jump does not exist (i.e. \fIindex\fP
383is superior to the number of entries in \fIprog_array_map\fP), or
384if the maximum number of tail calls has been reached for this
385chain of programs. This limit is defined in the kernel by the
386macro \fBMAX_TAIL_CALL_CNT\fP (not accessible to user space),
387which is currently set to 32.
388.TP
389.B Return
3900 on success, or a negative error in case of failure.
391.UNINDENT
392.TP
393.B \fBint bpf_clone_redirect(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIifindex\fP\fB, u64\fP \fIflags\fP\fB)\fP
394.INDENT 7.0
395.TP
396.B Description
397Clone and redirect the packet associated to \fIskb\fP to another
398net device of index \fIifindex\fP\&. Both ingress and egress
399interfaces can be used for redirection. The \fBBPF_F_INGRESS\fP
400value in \fIflags\fP is used to make the distinction (ingress path
401is selected if the flag is present, egress path otherwise).
402This is the only flag supported for now.
403.sp
404In comparison with \fBbpf_redirect\fP() helper,
405\fBbpf_clone_redirect\fP() has the associated cost of
406duplicating the packet buffer, but this can be executed out of
407the eBPF program. Conversely, \fBbpf_redirect\fP() is more
408efficient, but it is handled through an action code where the
409redirection happens only after the eBPF program has returned.
410.sp
e6107b29 411A call to this helper is susceptible to change the underlying
53666f6c
MK
412packet buffer. Therefore, at load time, all checks on pointers
413previously done by the verifier are invalidated and must be
414performed again, if the helper is used in combination with
415direct packet access.
416.TP
417.B Return
4180 on success, or a negative error in case of failure.
419.UNINDENT
420.TP
421.B \fBu64 bpf_get_current_pid_tgid(void)\fP
422.INDENT 7.0
423.TP
424.B Return
425A 64\-bit integer containing the current tgid and pid, and
426created as such:
427\fIcurrent_task\fP\fB\->tgid << 32 |\fP
428\fIcurrent_task\fP\fB\->pid\fP\&.
429.UNINDENT
430.TP
431.B \fBu64 bpf_get_current_uid_gid(void)\fP
432.INDENT 7.0
433.TP
434.B Return
435A 64\-bit integer containing the current GID and UID, and
436created as such: \fIcurrent_gid\fP \fB<< 32 |\fP \fIcurrent_uid\fP\&.
437.UNINDENT
438.TP
4e2b112c 439.B \fBint bpf_get_current_comm(void *\fP\fIbuf\fP\fB, u32\fP \fIsize_of_buf\fP\fB)\fP
53666f6c
MK
440.INDENT 7.0
441.TP
442.B Description
443Copy the \fBcomm\fP attribute of the current task into \fIbuf\fP of
444\fIsize_of_buf\fP\&. The \fBcomm\fP attribute contains the name of
445the executable (excluding the path) for the current task. The
446\fIsize_of_buf\fP must be strictly positive. On success, the
447helper makes sure that the \fIbuf\fP is NUL\-terminated. On failure,
448it is filled with zeroes.
449.TP
450.B Return
4510 on success, or a negative error in case of failure.
452.UNINDENT
453.TP
454.B \fBu32 bpf_get_cgroup_classid(struct sk_buff *\fP\fIskb\fP\fB)\fP
455.INDENT 7.0
456.TP
457.B Description
458Retrieve the classid for the current task, i.e. for the net_cls
459cgroup to which \fIskb\fP belongs.
460.sp
461This helper can be used on TC egress path, but not on ingress.
462.sp
463The net_cls cgroup provides an interface to tag network packets
464based on a user\-provided identifier for all traffic coming from
465the tasks belonging to the related cgroup. See also the related
466kernel documentation, available from the Linux sources in file
e6107b29 467\fIDocumentation/admin\-guide/cgroup\-v1/net_cls.rst\fP\&.
53666f6c
MK
468.sp
469The Linux kernel has two versions for cgroups: there are
470cgroups v1 and cgroups v2. Both are available to users, who can
471use a mixture of them, but note that the net_cls cgroup is for
472cgroup v1 only. This makes it incompatible with BPF programs
473run on cgroups, which is a cgroup\-v2\-only feature (a socket can
474only hold data for one version of cgroups at a time).
475.sp
476This helper is only available is the kernel was compiled with
477the \fBCONFIG_CGROUP_NET_CLASSID\fP configuration option set to
478"\fBy\fP" or to "\fBm\fP".
479.TP
480.B Return
481The classid, or 0 for the default unconfigured classid.
482.UNINDENT
483.TP
484.B \fBint bpf_skb_vlan_push(struct sk_buff *\fP\fIskb\fP\fB, __be16\fP \fIvlan_proto\fP\fB, u16\fP \fIvlan_tci\fP\fB)\fP
485.INDENT 7.0
486.TP
487.B Description
488Push a \fIvlan_tci\fP (VLAN tag control information) of protocol
489\fIvlan_proto\fP to the packet associated to \fIskb\fP, then update
490the checksum. Note that if \fIvlan_proto\fP is different from
491\fBETH_P_8021Q\fP and \fBETH_P_8021AD\fP, it is considered to
492be \fBETH_P_8021Q\fP\&.
493.sp
e6107b29 494A call to this helper is susceptible to change the underlying
53666f6c
MK
495packet buffer. Therefore, at load time, all checks on pointers
496previously done by the verifier are invalidated and must be
497performed again, if the helper is used in combination with
498direct packet access.
499.TP
500.B Return
5010 on success, or a negative error in case of failure.
502.UNINDENT
503.TP
504.B \fBint bpf_skb_vlan_pop(struct sk_buff *\fP\fIskb\fP\fB)\fP
505.INDENT 7.0
506.TP
507.B Description
508Pop a VLAN header from the packet associated to \fIskb\fP\&.
509.sp
e6107b29 510A call to this helper is susceptible to change the underlying
53666f6c
MK
511packet buffer. Therefore, at load time, all checks on pointers
512previously done by the verifier are invalidated and must be
513performed again, if the helper is used in combination with
514direct packet access.
515.TP
516.B Return
5170 on success, or a negative error in case of failure.
518.UNINDENT
519.TP
520.B \fBint bpf_skb_get_tunnel_key(struct sk_buff *\fP\fIskb\fP\fB, struct bpf_tunnel_key *\fP\fIkey\fP\fB, u32\fP \fIsize\fP\fB, u64\fP \fIflags\fP\fB)\fP
521.INDENT 7.0
522.TP
523.B Description
524Get tunnel metadata. This helper takes a pointer \fIkey\fP to an
525empty \fBstruct bpf_tunnel_key\fP of \fBsize\fP, that will be
526filled with tunnel metadata for the packet associated to \fIskb\fP\&.
527The \fIflags\fP can be set to \fBBPF_F_TUNINFO_IPV6\fP, which
528indicates that the tunnel is based on IPv6 protocol instead of
529IPv4.
530.sp
531The \fBstruct bpf_tunnel_key\fP is an object that generalizes the
532principal parameters used by various tunneling protocols into a
533single struct. This way, it can be used to easily make a
534decision based on the contents of the encapsulation header,
535"summarized" in this struct. In particular, it holds the IP
536address of the remote end (IPv4 or IPv6, depending on the case)
537in \fIkey\fP\fB\->remote_ipv4\fP or \fIkey\fP\fB\->remote_ipv6\fP\&. Also,
538this struct exposes the \fIkey\fP\fB\->tunnel_id\fP, which is
539generally mapped to a VNI (Virtual Network Identifier), making
540it programmable together with the \fBbpf_skb_set_tunnel_key\fP() helper.
541.sp
542Let\(aqs imagine that the following code is part of a program
543attached to the TC ingress interface, on one end of a GRE
544tunnel, and is supposed to filter out all messages coming from
545remote ends with IPv4 address other than 10.0.0.1:
546.INDENT 7.0
547.INDENT 3.5
548.sp
549.nf
550.ft C
551int ret;
552struct bpf_tunnel_key key = {};
553
554ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
555if (ret < 0)
556 return TC_ACT_SHOT; // drop packet
557
558if (key.remote_ipv4 != 0x0a000001)
559 return TC_ACT_SHOT; // drop packet
560
561return TC_ACT_OK; // accept packet
562.ft P
563.fi
564.UNINDENT
565.UNINDENT
566.sp
567This interface can also be used with all encapsulation devices
568that can operate in "collect metadata" mode: instead of having
569one network device per specific configuration, the "collect
570metadata" mode only requires a single device where the
571configuration can be extracted from this helper.
572.sp
573This can be used together with various tunnels such as VXLan,
574Geneve, GRE or IP in IP (IPIP).
575.TP
576.B Return
5770 on success, or a negative error in case of failure.
578.UNINDENT
579.TP
580.B \fBint bpf_skb_set_tunnel_key(struct sk_buff *\fP\fIskb\fP\fB, struct bpf_tunnel_key *\fP\fIkey\fP\fB, u32\fP \fIsize\fP\fB, u64\fP \fIflags\fP\fB)\fP
581.INDENT 7.0
582.TP
583.B Description
584Populate tunnel metadata for packet associated to \fIskb.\fP The
585tunnel metadata is set to the contents of \fIkey\fP, of \fIsize\fP\&. The
586\fIflags\fP can be set to a combination of the following values:
587.INDENT 7.0
588.TP
589.B \fBBPF_F_TUNINFO_IPV6\fP
590Indicate that the tunnel is based on IPv6 protocol
591instead of IPv4.
592.TP
593.B \fBBPF_F_ZERO_CSUM_TX\fP
594For IPv4 packets, add a flag to tunnel metadata
595indicating that checksum computation should be skipped
596and checksum set to zeroes.
597.TP
598.B \fBBPF_F_DONT_FRAGMENT\fP
599Add a flag to tunnel metadata indicating that the
600packet should not be fragmented.
601.TP
602.B \fBBPF_F_SEQ_NUMBER\fP
603Add a flag to tunnel metadata indicating that a
604sequence number should be added to tunnel header before
605sending the packet. This flag was added for GRE
606encapsulation, but might be used with other protocols
607as well in the future.
608.UNINDENT
609.sp
610Here is a typical usage on the transmit path:
611.INDENT 7.0
612.INDENT 3.5
613.sp
614.nf
615.ft C
616struct bpf_tunnel_key key;
617 populate key ...
618bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0);
619bpf_clone_redirect(skb, vxlan_dev_ifindex, 0);
620.ft P
621.fi
622.UNINDENT
623.UNINDENT
624.sp
625See also the description of the \fBbpf_skb_get_tunnel_key\fP()
626helper for additional information.
627.TP
628.B Return
6290 on success, or a negative error in case of failure.
630.UNINDENT
631.TP
632.B \fBu64 bpf_perf_event_read(struct bpf_map *\fP\fImap\fP\fB, u64\fP \fIflags\fP\fB)\fP
633.INDENT 7.0
634.TP
635.B Description
636Read the value of a perf event counter. This helper relies on a
637\fImap\fP of type \fBBPF_MAP_TYPE_PERF_EVENT_ARRAY\fP\&. The nature of
638the perf event counter is selected when \fImap\fP is updated with
639perf event file descriptors. The \fImap\fP is an array whose size
640is the number of available CPUs, and each cell contains a value
641relative to one CPU. The value to retrieve is indicated by
642\fIflags\fP, that contains the index of the CPU to look up, masked
643with \fBBPF_F_INDEX_MASK\fP\&. Alternatively, \fIflags\fP can be set to
644\fBBPF_F_CURRENT_CPU\fP to indicate that the value for the
645current CPU should be retrieved.
646.sp
647Note that before Linux 4.13, only hardware perf event can be
648retrieved.
649.sp
650Also, be aware that the newer helper
651\fBbpf_perf_event_read_value\fP() is recommended over
652\fBbpf_perf_event_read\fP() in general. The latter has some ABI
653quirks where error and counter value are used as a return code
654(which is wrong to do since ranges may overlap). This issue is
655fixed with \fBbpf_perf_event_read_value\fP(), which at the same
656time provides more features over the \fBbpf_perf_event_read\fP() interface. Please refer to the description of
657\fBbpf_perf_event_read_value\fP() for details.
658.TP
659.B Return
660The value of the perf event counter read from the map, or a
661negative error code in case of failure.
662.UNINDENT
663.TP
664.B \fBint bpf_redirect(u32\fP \fIifindex\fP\fB, u64\fP \fIflags\fP\fB)\fP
665.INDENT 7.0
666.TP
667.B Description
668Redirect the packet to another net device of index \fIifindex\fP\&.
669This helper is somewhat similar to \fBbpf_clone_redirect\fP(), except that the packet is not cloned, which provides
670increased performance.
671.sp
672Except for XDP, both ingress and egress interfaces can be used
673for redirection. The \fBBPF_F_INGRESS\fP value in \fIflags\fP is used
674to make the distinction (ingress path is selected if the flag
675is present, egress path otherwise). Currently, XDP only
676supports redirection to the egress interface, and accepts no
677flag at all.
678.sp
4e2b112c
MK
679The same effect can also be attained with the more generic
680\fBbpf_redirect_map\fP(), which uses a BPF map to store the
681redirect target instead of providing it directly to the helper.
53666f6c
MK
682.TP
683.B Return
684For XDP, the helper returns \fBXDP_REDIRECT\fP on success or
685\fBXDP_ABORTED\fP on error. For other program types, the values
686are \fBTC_ACT_REDIRECT\fP on success or \fBTC_ACT_SHOT\fP on
687error.
688.UNINDENT
689.TP
690.B \fBu32 bpf_get_route_realm(struct sk_buff *\fP\fIskb\fP\fB)\fP
691.INDENT 7.0
692.TP
693.B Description
694Retrieve the realm or the route, that is to say the
695\fBtclassid\fP field of the destination for the \fIskb\fP\&. The
696indentifier retrieved is a user\-provided tag, similar to the
697one used with the net_cls cgroup (see description for
698\fBbpf_get_cgroup_classid\fP() helper), but here this tag is
699held by a route (a destination entry), not by a task.
700.sp
701Retrieving this identifier works with the clsact TC egress hook
702(see also \fBtc\-bpf(8)\fP), or alternatively on conventional
703classful egress qdiscs, but not on TC ingress path. In case of
704clsact TC egress hook, this has the advantage that, internally,
705the destination entry has not been dropped yet in the transmit
706path. Therefore, the destination entry does not need to be
707artificially held via \fBnetif_keep_dst\fP() for a classful
708qdisc until the \fIskb\fP is freed.
709.sp
710This helper is available only if the kernel was compiled with
711\fBCONFIG_IP_ROUTE_CLASSID\fP configuration option.
712.TP
713.B Return
714The realm of the route for the packet associated to \fIskb\fP, or 0
715if none was found.
716.UNINDENT
717.TP
4e2b112c 718.B \fBint bpf_perf_event_output(void *\fP\fIctx\fP\fB, struct bpf_map *\fP\fImap\fP\fB, u64\fP \fIflags\fP\fB, void *\fP\fIdata\fP\fB, u64\fP \fIsize\fP\fB)\fP
53666f6c
MK
719.INDENT 7.0
720.TP
721.B Description
722Write raw \fIdata\fP blob into a special BPF perf event held by
723\fImap\fP of type \fBBPF_MAP_TYPE_PERF_EVENT_ARRAY\fP\&. This perf
724event must have the following attributes: \fBPERF_SAMPLE_RAW\fP
725as \fBsample_type\fP, \fBPERF_TYPE_SOFTWARE\fP as \fBtype\fP, and
726\fBPERF_COUNT_SW_BPF_OUTPUT\fP as \fBconfig\fP\&.
727.sp
728The \fIflags\fP are used to indicate the index in \fImap\fP for which
729the value must be put, masked with \fBBPF_F_INDEX_MASK\fP\&.
730Alternatively, \fIflags\fP can be set to \fBBPF_F_CURRENT_CPU\fP
731to indicate that the index of the current CPU core should be
732used.
733.sp
734The value to write, of \fIsize\fP, is passed through eBPF stack and
735pointed by \fIdata\fP\&.
736.sp
737The context of the program \fIctx\fP needs also be passed to the
738helper.
739.sp
740On user space, a program willing to read the values needs to
741call \fBperf_event_open\fP() on the perf event (either for
742one or for all CPUs) and to store the file descriptor into the
743\fImap\fP\&. This must be done before the eBPF program can send data
744into it. An example is available in file
745\fIsamples/bpf/trace_output_user.c\fP in the Linux kernel source
746tree (the eBPF program counterpart is in
747\fIsamples/bpf/trace_output_kern.c\fP).
748.sp
749\fBbpf_perf_event_output\fP() achieves better performance
750than \fBbpf_trace_printk\fP() for sharing data with user
751space, and is much better suitable for streaming data from eBPF
752programs.
753.sp
754Note that this helper is not restricted to tracing use cases
755and can be used with programs attached to TC or XDP as well,
756where it allows for passing data to user space listeners. Data
757can be:
758.INDENT 7.0
759.IP \(bu 2
760Only custom structs,
761.IP \(bu 2
762Only the packet payload, or
763.IP \(bu 2
764A combination of both.
765.UNINDENT
766.TP
767.B Return
7680 on success, or a negative error in case of failure.
769.UNINDENT
770.TP
4e2b112c 771.B \fBint bpf_skb_load_bytes(const void *\fP\fIskb\fP\fB, u32\fP \fIoffset\fP\fB, void *\fP\fIto\fP\fB, u32\fP \fIlen\fP\fB)\fP
53666f6c
MK
772.INDENT 7.0
773.TP
774.B Description
775This helper was provided as an easy way to load data from a
776packet. It can be used to load \fIlen\fP bytes from \fIoffset\fP from
777the packet associated to \fIskb\fP, into the buffer pointed by
778\fIto\fP\&.
779.sp
780Since Linux 4.7, usage of this helper has mostly been replaced
781by "direct packet access", enabling packet data to be
782manipulated with \fIskb\fP\fB\->data\fP and \fIskb\fP\fB\->data_end\fP
783pointing respectively to the first byte of packet data and to
784the byte after the last byte of packet data. However, it
785remains useful if one wishes to read large quantities of data
786at once from a packet into the eBPF stack.
787.TP
788.B Return
7890 on success, or a negative error in case of failure.
790.UNINDENT
791.TP
4e2b112c 792.B \fBint bpf_get_stackid(void *\fP\fIctx\fP\fB, struct bpf_map *\fP\fImap\fP\fB, u64\fP \fIflags\fP\fB)\fP
53666f6c
MK
793.INDENT 7.0
794.TP
795.B Description
796Walk a user or a kernel stack and return its id. To achieve
797this, the helper needs \fIctx\fP, which is a pointer to the context
798on which the tracing program is executed, and a pointer to a
799\fImap\fP of type \fBBPF_MAP_TYPE_STACK_TRACE\fP\&.
800.sp
801The last argument, \fIflags\fP, holds the number of stack frames to
802skip (from 0 to 255), masked with
803\fBBPF_F_SKIP_FIELD_MASK\fP\&. The next bits can be used to set
804a combination of the following flags:
805.INDENT 7.0
806.TP
807.B \fBBPF_F_USER_STACK\fP
808Collect a user space stack instead of a kernel stack.
809.TP
810.B \fBBPF_F_FAST_STACK_CMP\fP
811Compare stacks by hash only.
812.TP
813.B \fBBPF_F_REUSE_STACKID\fP
814If two different stacks hash into the same \fIstackid\fP,
815discard the old one.
816.UNINDENT
817.sp
818The stack id retrieved is a 32 bit long integer handle which
819can be further combined with other data (including other stack
820ids) and used as a key into maps. This can be useful for
821generating a variety of graphs (such as flame graphs or off\-cpu
822graphs).
823.sp
824For walking a stack, this helper is an improvement over
825\fBbpf_probe_read\fP(), which can be used with unrolled loops
826but is not efficient and consumes a lot of eBPF instructions.
827Instead, \fBbpf_get_stackid\fP() can collect up to
828\fBPERF_MAX_STACK_DEPTH\fP both kernel and user frames. Note that
829this limit can be controlled with the \fBsysctl\fP program, and
830that it should be manually increased in order to profile long
831user stacks (such as stacks for Java programs). To do so, use:
832.INDENT 7.0
833.INDENT 3.5
834.sp
835.nf
836.ft C
837# sysctl kernel.perf_event_max_stack=<new value>
838.ft P
839.fi
840.UNINDENT
841.UNINDENT
842.TP
843.B Return
844The positive or null stack id on success, or a negative error
845in case of failure.
846.UNINDENT
847.TP
848.B \fBs64 bpf_csum_diff(__be32 *\fP\fIfrom\fP\fB, u32\fP \fIfrom_size\fP\fB, __be32 *\fP\fIto\fP\fB, u32\fP \fIto_size\fP\fB, __wsum\fP \fIseed\fP\fB)\fP
849.INDENT 7.0
850.TP
851.B Description
852Compute a checksum difference, from the raw buffer pointed by
853\fIfrom\fP, of length \fIfrom_size\fP (that must be a multiple of 4),
854towards the raw buffer pointed by \fIto\fP, of size \fIto_size\fP
855(same remark). An optional \fIseed\fP can be added to the value
856(this can be cascaded, the seed may come from a previous call
857to the helper).
858.sp
859This is flexible enough to be used in several ways:
860.INDENT 7.0
861.IP \(bu 2
862With \fIfrom_size\fP == 0, \fIto_size\fP > 0 and \fIseed\fP set to
863checksum, it can be used when pushing new data.
864.IP \(bu 2
865With \fIfrom_size\fP > 0, \fIto_size\fP == 0 and \fIseed\fP set to
866checksum, it can be used when removing data from a packet.
867.IP \(bu 2
868With \fIfrom_size\fP > 0, \fIto_size\fP > 0 and \fIseed\fP set to 0, it
869can be used to compute a diff. Note that \fIfrom_size\fP and
870\fIto_size\fP do not need to be equal.
871.UNINDENT
872.sp
873This helper can be used in combination with
874\fBbpf_l3_csum_replace\fP() and \fBbpf_l4_csum_replace\fP(), to
875which one can feed in the difference computed with
876\fBbpf_csum_diff\fP().
877.TP
878.B Return
879The checksum result, or a negative error code in case of
880failure.
881.UNINDENT
882.TP
4e2b112c 883.B \fBint bpf_skb_get_tunnel_opt(struct sk_buff *\fP\fIskb\fP\fB, void *\fP\fIopt\fP\fB, u32\fP \fIsize\fP\fB)\fP
53666f6c
MK
884.INDENT 7.0
885.TP
886.B Description
887Retrieve tunnel options metadata for the packet associated to
888\fIskb\fP, and store the raw tunnel option data to the buffer \fIopt\fP
889of \fIsize\fP\&.
890.sp
891This helper can be used with encapsulation devices that can
892operate in "collect metadata" mode (please refer to the related
893note in the description of \fBbpf_skb_get_tunnel_key\fP() for
894more details). A particular example where this can be used is
895in combination with the Geneve encapsulation protocol, where it
896allows for pushing (with \fBbpf_skb_get_tunnel_opt\fP() helper)
897and retrieving arbitrary TLVs (Type\-Length\-Value headers) from
898the eBPF program. This allows for full customization of these
899headers.
900.TP
901.B Return
902The size of the option data retrieved.
903.UNINDENT
904.TP
4e2b112c 905.B \fBint bpf_skb_set_tunnel_opt(struct sk_buff *\fP\fIskb\fP\fB, void *\fP\fIopt\fP\fB, u32\fP \fIsize\fP\fB)\fP
53666f6c
MK
906.INDENT 7.0
907.TP
908.B Description
909Set tunnel options metadata for the packet associated to \fIskb\fP
910to the option data contained in the raw buffer \fIopt\fP of \fIsize\fP\&.
911.sp
912See also the description of the \fBbpf_skb_get_tunnel_opt\fP()
913helper for additional information.
914.TP
915.B Return
9160 on success, or a negative error in case of failure.
917.UNINDENT
918.TP
919.B \fBint bpf_skb_change_proto(struct sk_buff *\fP\fIskb\fP\fB, __be16\fP \fIproto\fP\fB, u64\fP \fIflags\fP\fB)\fP
920.INDENT 7.0
921.TP
922.B Description
923Change the protocol of the \fIskb\fP to \fIproto\fP\&. Currently
924supported are transition from IPv4 to IPv6, and from IPv6 to
925IPv4. The helper takes care of the groundwork for the
926transition, including resizing the socket buffer. The eBPF
927program is expected to fill the new headers, if any, via
928\fBskb_store_bytes\fP() and to recompute the checksums with
929\fBbpf_l3_csum_replace\fP() and \fBbpf_l4_csum_replace\fP(). The main case for this helper is to perform NAT64
930operations out of an eBPF program.
931.sp
932Internally, the GSO type is marked as dodgy so that headers are
933checked and segments are recalculated by the GSO/GRO engine.
934The size for GSO target is adapted as well.
935.sp
936All values for \fIflags\fP are reserved for future usage, and must
937be left at zero.
938.sp
e6107b29 939A call to this helper is susceptible to change the underlying
53666f6c
MK
940packet buffer. Therefore, at load time, all checks on pointers
941previously done by the verifier are invalidated and must be
942performed again, if the helper is used in combination with
943direct packet access.
944.TP
945.B Return
9460 on success, or a negative error in case of failure.
947.UNINDENT
948.TP
949.B \fBint bpf_skb_change_type(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fItype\fP\fB)\fP
950.INDENT 7.0
951.TP
952.B Description
953Change the packet type for the packet associated to \fIskb\fP\&. This
954comes down to setting \fIskb\fP\fB\->pkt_type\fP to \fItype\fP, except
955the eBPF program does not have a write access to \fIskb\fP\fB\->pkt_type\fP beside this helper. Using a helper here allows
956for graceful handling of errors.
957.sp
958The major use case is to change incoming \fIskb*s to
959**PACKET_HOST*\fP in a programmatic way instead of having to
960recirculate via \fBredirect\fP(..., \fBBPF_F_INGRESS\fP), for
961example.
962.sp
963Note that \fItype\fP only allows certain values. At this time, they
964are:
965.INDENT 7.0
966.TP
967.B \fBPACKET_HOST\fP
968Packet is for us.
969.TP
970.B \fBPACKET_BROADCAST\fP
971Send packet to all.
972.TP
973.B \fBPACKET_MULTICAST\fP
974Send packet to group.
975.TP
976.B \fBPACKET_OTHERHOST\fP
977Send packet to someone else.
978.UNINDENT
979.TP
980.B Return
9810 on success, or a negative error in case of failure.
982.UNINDENT
983.TP
984.B \fBint bpf_skb_under_cgroup(struct sk_buff *\fP\fIskb\fP\fB, struct bpf_map *\fP\fImap\fP\fB, u32\fP \fIindex\fP\fB)\fP
985.INDENT 7.0
986.TP
987.B Description
988Check whether \fIskb\fP is a descendant of the cgroup2 held by
989\fImap\fP of type \fBBPF_MAP_TYPE_CGROUP_ARRAY\fP, at \fIindex\fP\&.
990.TP
991.B Return
992The return value depends on the result of the test, and can be:
993.INDENT 7.0
994.IP \(bu 2
9950, if the \fIskb\fP failed the cgroup2 descendant test.
996.IP \(bu 2
9971, if the \fIskb\fP succeeded the cgroup2 descendant test.
998.IP \(bu 2
999A negative error code, if an error occurred.
1000.UNINDENT
1001.UNINDENT
1002.TP
1003.B \fBu32 bpf_get_hash_recalc(struct sk_buff *\fP\fIskb\fP\fB)\fP
1004.INDENT 7.0
1005.TP
1006.B Description
1007Retrieve the hash of the packet, \fIskb\fP\fB\->hash\fP\&. If it is
1008not set, in particular if the hash was cleared due to mangling,
1009recompute this hash. Later accesses to the hash can be done
1010directly with \fIskb\fP\fB\->hash\fP\&.
1011.sp
1012Calling \fBbpf_set_hash_invalid\fP(), changing a packet
1013prototype with \fBbpf_skb_change_proto\fP(), or calling
1014\fBbpf_skb_store_bytes\fP() with the
1015\fBBPF_F_INVALIDATE_HASH\fP are actions susceptible to clear
1016the hash and to trigger a new computation for the next call to
1017\fBbpf_get_hash_recalc\fP().
1018.TP
1019.B Return
1020The 32\-bit hash.
1021.UNINDENT
1022.TP
1023.B \fBu64 bpf_get_current_task(void)\fP
1024.INDENT 7.0
1025.TP
1026.B Return
1027A pointer to the current task struct.
1028.UNINDENT
1029.TP
1030.B \fBint bpf_probe_write_user(void *\fP\fIdst\fP\fB, const void *\fP\fIsrc\fP\fB, u32\fP \fIlen\fP\fB)\fP
1031.INDENT 7.0
1032.TP
1033.B Description
1034Attempt in a safe way to write \fIlen\fP bytes from the buffer
1035\fIsrc\fP to \fIdst\fP in memory. It only works for threads that are in
1036user context, and \fIdst\fP must be a valid user space address.
1037.sp
1038This helper should not be used to implement any kind of
1039security mechanism because of TOC\-TOU attacks, but rather to
1040debug, divert, and manipulate execution of semi\-cooperative
1041processes.
1042.sp
1043Keep in mind that this feature is meant for experiments, and it
1044has a risk of crashing the system and running programs.
1045Therefore, when an eBPF program using this helper is attached,
1046a warning including PID and process name is printed to kernel
1047logs.
1048.TP
1049.B Return
10500 on success, or a negative error in case of failure.
1051.UNINDENT
1052.TP
1053.B \fBint bpf_current_task_under_cgroup(struct bpf_map *\fP\fImap\fP\fB, u32\fP \fIindex\fP\fB)\fP
1054.INDENT 7.0
1055.TP
1056.B Description
1057Check whether the probe is being run is the context of a given
1058subset of the cgroup2 hierarchy. The cgroup2 to test is held by
1059\fImap\fP of type \fBBPF_MAP_TYPE_CGROUP_ARRAY\fP, at \fIindex\fP\&.
1060.TP
1061.B Return
1062The return value depends on the result of the test, and can be:
1063.INDENT 7.0
1064.IP \(bu 2
10650, if the \fIskb\fP task belongs to the cgroup2.
1066.IP \(bu 2
10671, if the \fIskb\fP task does not belong to the cgroup2.
1068.IP \(bu 2
1069A negative error code, if an error occurred.
1070.UNINDENT
1071.UNINDENT
1072.TP
1073.B \fBint bpf_skb_change_tail(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIlen\fP\fB, u64\fP \fIflags\fP\fB)\fP
1074.INDENT 7.0
1075.TP
1076.B Description
1077Resize (trim or grow) the packet associated to \fIskb\fP to the
1078new \fIlen\fP\&. The \fIflags\fP are reserved for future usage, and must
1079be left at zero.
1080.sp
1081The basic idea is that the helper performs the needed work to
1082change the size of the packet, then the eBPF program rewrites
1083the rest via helpers like \fBbpf_skb_store_bytes\fP(),
1084\fBbpf_l3_csum_replace\fP(), \fBbpf_l3_csum_replace\fP()
1085and others. This helper is a slow path utility intended for
1086replies with control messages. And because it is targeted for
1087slow path, the helper itself can afford to be slow: it
1088implicitly linearizes, unclones and drops offloads from the
1089\fIskb\fP\&.
1090.sp
e6107b29 1091A call to this helper is susceptible to change the underlying
53666f6c
MK
1092packet buffer. Therefore, at load time, all checks on pointers
1093previously done by the verifier are invalidated and must be
1094performed again, if the helper is used in combination with
1095direct packet access.
1096.TP
1097.B Return
10980 on success, or a negative error in case of failure.
1099.UNINDENT
1100.TP
1101.B \fBint bpf_skb_pull_data(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIlen\fP\fB)\fP
1102.INDENT 7.0
1103.TP
1104.B Description
1105Pull in non\-linear data in case the \fIskb\fP is non\-linear and not
1106all of \fIlen\fP are part of the linear section. Make \fIlen\fP bytes
1107from \fIskb\fP readable and writable. If a zero value is passed for
1108\fIlen\fP, then the whole length of the \fIskb\fP is pulled.
1109.sp
1110This helper is only needed for reading and writing with direct
1111packet access.
1112.sp
1113For direct packet access, testing that offsets to access
1114are within packet boundaries (test on \fIskb\fP\fB\->data_end\fP) is
1115susceptible to fail if offsets are invalid, or if the requested
1116data is in non\-linear parts of the \fIskb\fP\&. On failure the
1117program can just bail out, or in the case of a non\-linear
1118buffer, use a helper to make the data available. The
1119\fBbpf_skb_load_bytes\fP() helper is a first solution to access
1120the data. Another one consists in using \fBbpf_skb_pull_data\fP
1121to pull in once the non\-linear parts, then retesting and
1122eventually access the data.
1123.sp
1124At the same time, this also makes sure the \fIskb\fP is uncloned,
1125which is a necessary condition for direct write. As this needs
1126to be an invariant for the write part only, the verifier
1127detects writes and adds a prologue that is calling
1128\fBbpf_skb_pull_data()\fP to effectively unclone the \fIskb\fP from
1129the very beginning in case it is indeed cloned.
1130.sp
e6107b29 1131A call to this helper is susceptible to change the underlying
53666f6c
MK
1132packet buffer. Therefore, at load time, all checks on pointers
1133previously done by the verifier are invalidated and must be
1134performed again, if the helper is used in combination with
1135direct packet access.
1136.TP
1137.B Return
11380 on success, or a negative error in case of failure.
1139.UNINDENT
1140.TP
1141.B \fBs64 bpf_csum_update(struct sk_buff *\fP\fIskb\fP\fB, __wsum\fP \fIcsum\fP\fB)\fP
1142.INDENT 7.0
1143.TP
1144.B Description
1145Add the checksum \fIcsum\fP into \fIskb\fP\fB\->csum\fP in case the
1146driver has supplied a checksum for the entire packet into that
1147field. Return an error otherwise. This helper is intended to be
1148used in combination with \fBbpf_csum_diff\fP(), in particular
1149when the checksum needs to be updated after data has been
1150written into the packet through direct packet access.
1151.TP
1152.B Return
1153The checksum on success, or a negative error code in case of
1154failure.
1155.UNINDENT
1156.TP
1157.B \fBvoid bpf_set_hash_invalid(struct sk_buff *\fP\fIskb\fP\fB)\fP
1158.INDENT 7.0
1159.TP
1160.B Description
1161Invalidate the current \fIskb\fP\fB\->hash\fP\&. It can be used after
1162mangling on headers through direct packet access, in order to
1163indicate that the hash is outdated and to trigger a
1164recalculation the next time the kernel tries to access this
1165hash or when the \fBbpf_get_hash_recalc\fP() helper is called.
1166.UNINDENT
1167.TP
1168.B \fBint bpf_get_numa_node_id(void)\fP
1169.INDENT 7.0
1170.TP
1171.B Description
1172Return the id of the current NUMA node. The primary use case
1173for this helper is the selection of sockets for the local NUMA
1174node, when the program is attached to sockets using the
1175\fBSO_ATTACH_REUSEPORT_EBPF\fP option (see also \fBsocket(7)\fP),
1176but the helper is also available to other eBPF program types,
1177similarly to \fBbpf_get_smp_processor_id\fP().
1178.TP
1179.B Return
1180The id of current NUMA node.
1181.UNINDENT
1182.TP
1183.B \fBint bpf_skb_change_head(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIlen\fP\fB, u64\fP \fIflags\fP\fB)\fP
1184.INDENT 7.0
1185.TP
1186.B Description
1187Grows headroom of packet associated to \fIskb\fP and adjusts the
1188offset of the MAC header accordingly, adding \fIlen\fP bytes of
1189space. It automatically extends and reallocates memory as
1190required.
1191.sp
1192This helper can be used on a layer 3 \fIskb\fP to push a MAC header
1193for redirection into a layer 2 device.
1194.sp
1195All values for \fIflags\fP are reserved for future usage, and must
1196be left at zero.
1197.sp
e6107b29 1198A call to this helper is susceptible to change the underlying
53666f6c
MK
1199packet buffer. Therefore, at load time, all checks on pointers
1200previously done by the verifier are invalidated and must be
1201performed again, if the helper is used in combination with
1202direct packet access.
1203.TP
1204.B Return
12050 on success, or a negative error in case of failure.
1206.UNINDENT
1207.TP
1208.B \fBint bpf_xdp_adjust_head(struct xdp_buff *\fP\fIxdp_md\fP\fB, int\fP \fIdelta\fP\fB)\fP
1209.INDENT 7.0
1210.TP
1211.B Description
1212Adjust (move) \fIxdp_md\fP\fB\->data\fP by \fIdelta\fP bytes. Note that
1213it is possible to use a negative value for \fIdelta\fP\&. This helper
1214can be used to prepare the packet for pushing or popping
1215headers.
1216.sp
e6107b29 1217A call to this helper is susceptible to change the underlying
53666f6c
MK
1218packet buffer. Therefore, at load time, all checks on pointers
1219previously done by the verifier are invalidated and must be
1220performed again, if the helper is used in combination with
1221direct packet access.
1222.TP
1223.B Return
12240 on success, or a negative error in case of failure.
1225.UNINDENT
1226.TP
4e2b112c 1227.B \fBint bpf_probe_read_str(void *\fP\fIdst\fP\fB, u32\fP \fIsize\fP\fB, const void *\fP\fIunsafe_ptr\fP\fB)\fP
53666f6c
MK
1228.INDENT 7.0
1229.TP
1230.B Description
4e2b112c
MK
1231Copy a NUL terminated string from an unsafe kernel address
1232\fIunsafe_ptr\fP to \fIdst\fP\&. See bpf_probe_read_kernel_str() for
1233more details.
53666f6c 1234.sp
4e2b112c
MK
1235Generally, use bpf_probe_read_user_str() or bpf_probe_read_kernel_str()
1236instead.
53666f6c
MK
1237.TP
1238.B Return
1239On success, the strictly positive length of the string,
1240including the trailing NUL character. On error, a negative
1241value.
1242.UNINDENT
1243.TP
1244.B \fBu64 bpf_get_socket_cookie(struct sk_buff *\fP\fIskb\fP\fB)\fP
1245.INDENT 7.0
1246.TP
1247.B Description
1248If the \fBstruct sk_buff\fP pointed by \fIskb\fP has a known socket,
1249retrieve the cookie (generated by the kernel) of this socket.
1250If no cookie has been set yet, generate a new cookie. Once
1251generated, the socket cookie remains stable for the life of the
1252socket. This helper can be useful for monitoring per socket
e6107b29
MK
1253networking traffic statistics as it provides a global socket
1254identifier that can be assumed unique.
53666f6c
MK
1255.TP
1256.B Return
1257A 8\-byte long non\-decreasing number on success, or 0 if the
1258socket field is missing inside \fIskb\fP\&.
1259.UNINDENT
1260.TP
1261.B \fBu64 bpf_get_socket_cookie(struct bpf_sock_addr *\fP\fIctx\fP\fB)\fP
1262.INDENT 7.0
1263.TP
1264.B Description
1265Equivalent to bpf_get_socket_cookie() helper that accepts
e6107b29 1266\fIskb\fP, but gets socket from \fBstruct bpf_sock_addr\fP context.
53666f6c
MK
1267.TP
1268.B Return
1269A 8\-byte long non\-decreasing number.
1270.UNINDENT
1271.TP
1272.B \fBu64 bpf_get_socket_cookie(struct bpf_sock_ops *\fP\fIctx\fP\fB)\fP
1273.INDENT 7.0
1274.TP
1275.B Description
1276Equivalent to bpf_get_socket_cookie() helper that accepts
e6107b29 1277\fIskb\fP, but gets socket from \fBstruct bpf_sock_ops\fP context.
53666f6c
MK
1278.TP
1279.B Return
1280A 8\-byte long non\-decreasing number.
1281.UNINDENT
1282.TP
1283.B \fBu32 bpf_get_socket_uid(struct sk_buff *\fP\fIskb\fP\fB)\fP
1284.INDENT 7.0
1285.TP
1286.B Return
1287The owner UID of the socket associated to \fIskb\fP\&. If the socket
1288is \fBNULL\fP, or if it is not a full socket (i.e. if it is a
1289time\-wait or a request socket instead), \fBoverflowuid\fP value
1290is returned (note that \fBoverflowuid\fP might also be the actual
1291UID value for the socket).
1292.UNINDENT
1293.TP
1294.B \fBu32 bpf_set_hash(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIhash\fP\fB)\fP
1295.INDENT 7.0
1296.TP
1297.B Description
1298Set the full hash for \fIskb\fP (set the field \fIskb\fP\fB\->hash\fP)
1299to value \fIhash\fP\&.
1300.TP
1301.B Return
e6107b29 13020
53666f6c
MK
1303.UNINDENT
1304.TP
4e2b112c 1305.B \fBint bpf_setsockopt(struct bpf_sock_ops *\fP\fIbpf_socket\fP\fB, int\fP \fIlevel\fP\fB, int\fP \fIoptname\fP\fB, void *\fP\fIoptval\fP\fB, int\fP \fIoptlen\fP\fB)\fP
53666f6c
MK
1306.INDENT 7.0
1307.TP
1308.B Description
1309Emulate a call to \fBsetsockopt()\fP on the socket associated to
1310\fIbpf_socket\fP, which must be a full socket. The \fIlevel\fP at
1311which the option resides and the name \fIoptname\fP of the option
1312must be specified, see \fBsetsockopt(2)\fP for more information.
1313The option value of length \fIoptlen\fP is pointed by \fIoptval\fP\&.
1314.sp
1315This helper actually implements a subset of \fBsetsockopt()\fP\&.
1316It supports the following \fIlevel\fPs:
1317.INDENT 7.0
1318.IP \(bu 2
1319\fBSOL_SOCKET\fP, which supports the following \fIoptname\fPs:
1320\fBSO_RCVBUF\fP, \fBSO_SNDBUF\fP, \fBSO_MAX_PACING_RATE\fP,
1321\fBSO_PRIORITY\fP, \fBSO_RCVLOWAT\fP, \fBSO_MARK\fP\&.
1322.IP \(bu 2
1323\fBIPPROTO_TCP\fP, which supports the following \fIoptname\fPs:
1324\fBTCP_CONGESTION\fP, \fBTCP_BPF_IW\fP,
1325\fBTCP_BPF_SNDCWND_CLAMP\fP\&.
1326.IP \(bu 2
1327\fBIPPROTO_IP\fP, which supports \fIoptname\fP \fBIP_TOS\fP\&.
1328.IP \(bu 2
1329\fBIPPROTO_IPV6\fP, which supports \fIoptname\fP \fBIPV6_TCLASS\fP\&.
1330.UNINDENT
1331.TP
1332.B Return
13330 on success, or a negative error in case of failure.
1334.UNINDENT
1335.TP
2223d7df 1336.B \fBint bpf_skb_adjust_room(struct sk_buff *\fP\fIskb\fP\fB, s32\fP \fIlen_diff\fP\fB, u32\fP \fImode\fP\fB, u64\fP \fIflags\fP\fB)\fP
53666f6c
MK
1337.INDENT 7.0
1338.TP
1339.B Description
1340Grow or shrink the room for data in the packet associated to
1341\fIskb\fP by \fIlen_diff\fP, and according to the selected \fImode\fP\&.
1342.sp
e6107b29 1343There are two supported modes at this time:
53666f6c
MK
1344.INDENT 7.0
1345.IP \(bu 2
e6107b29
MK
1346\fBBPF_ADJ_ROOM_MAC\fP: Adjust room at the mac layer
1347(room space is added or removed below the layer 2 header).
1348.IP \(bu 2
53666f6c
MK
1349\fBBPF_ADJ_ROOM_NET\fP: Adjust room at the network layer
1350(room space is added or removed below the layer 3 header).
1351.UNINDENT
1352.sp
e6107b29
MK
1353The following flags are supported at this time:
1354.INDENT 7.0
1355.IP \(bu 2
1356\fBBPF_F_ADJ_ROOM_FIXED_GSO\fP: Do not adjust gso_size.
1357Adjusting mss in this way is not allowed for datagrams.
1358.IP \(bu 2
1359\fBBPF_F_ADJ_ROOM_ENCAP_L3_IPV4\fP,
1360\fBBPF_F_ADJ_ROOM_ENCAP_L3_IPV6\fP:
1361Any new space is reserved to hold a tunnel header.
1362Configure skb offsets and other fields accordingly.
1363.IP \(bu 2
1364\fBBPF_F_ADJ_ROOM_ENCAP_L4_GRE\fP,
1365\fBBPF_F_ADJ_ROOM_ENCAP_L4_UDP\fP:
1366Use with ENCAP_L3 flags to further specify the tunnel type.
1367.IP \(bu 2
1368\fBBPF_F_ADJ_ROOM_ENCAP_L2\fP(\fIlen\fP):
1369Use with ENCAP_L3/L4 flags to further specify the tunnel
1370type; \fIlen\fP is the length of the inner MAC header.
1371.UNINDENT
53666f6c 1372.sp
e6107b29 1373A call to this helper is susceptible to change the underlying
53666f6c
MK
1374packet buffer. Therefore, at load time, all checks on pointers
1375previously done by the verifier are invalidated and must be
1376performed again, if the helper is used in combination with
1377direct packet access.
1378.TP
1379.B Return
13800 on success, or a negative error in case of failure.
1381.UNINDENT
1382.TP
1383.B \fBint bpf_redirect_map(struct bpf_map *\fP\fImap\fP\fB, u32\fP \fIkey\fP\fB, u64\fP \fIflags\fP\fB)\fP
1384.INDENT 7.0
1385.TP
1386.B Description
1387Redirect the packet to the endpoint referenced by \fImap\fP at
1388index \fIkey\fP\&. Depending on its type, this \fImap\fP can contain
1389references to net devices (for forwarding packets through other
1390ports), or to CPUs (for redirecting XDP frames to another CPU;
1391but this is only implemented for native XDP (with driver
1392support) as of this writing).
1393.sp
e6107b29
MK
1394The lower two bits of \fIflags\fP are used as the return code if
1395the map lookup fails. This is so that the return value can be
1396one of the XDP program return codes up to XDP_TX, as chosen by
1397the caller. Any higher bits in the \fIflags\fP argument must be
1398unset.
53666f6c 1399.sp
4e2b112c
MK
1400See also bpf_redirect(), which only supports redirecting to an
1401ifindex, but doesn\(aqt require a map to do so.
53666f6c
MK
1402.TP
1403.B Return
4e2b112c
MK
1404\fBXDP_REDIRECT\fP on success, or the value of the two lower bits
1405of the
1406.nf
1407**
1408.fi
1409flags* argument on error.
1410.IP "System Message: WARNING/2 (/tmp/bpf-helpers.rst:, line 1105)"
1411Inline strong start\-string without end\-string.
53666f6c
MK
1412.UNINDENT
1413.TP
4e2b112c 1414.B \fBint bpf_sk_redirect_map(struct sk_buff *\fP\fIskb\fP\fB, struct bpf_map *\fP\fImap\fP\fB, u32\fP \fIkey\fP\fB, u64\fP \fIflags\fP\fB)\fP
53666f6c
MK
1415.INDENT 7.0
1416.TP
1417.B Description
1418Redirect the packet to the socket referenced by \fImap\fP (of type
1419\fBBPF_MAP_TYPE_SOCKMAP\fP) at index \fIkey\fP\&. Both ingress and
1420egress interfaces can be used for redirection. The
1421\fBBPF_F_INGRESS\fP value in \fIflags\fP is used to make the
1422distinction (ingress path is selected if the flag is present,
1423egress path otherwise). This is the only flag supported for now.
1424.TP
1425.B Return
1426\fBSK_PASS\fP on success, or \fBSK_DROP\fP on error.
1427.UNINDENT
1428.TP
1429.B \fBint bpf_sock_map_update(struct bpf_sock_ops *\fP\fIskops\fP\fB, struct bpf_map *\fP\fImap\fP\fB, void *\fP\fIkey\fP\fB, u64\fP \fIflags\fP\fB)\fP
1430.INDENT 7.0
1431.TP
1432.B Description
1433Add an entry to, or update a \fImap\fP referencing sockets. The
1434\fIskops\fP is used as a new value for the entry associated to
1435\fIkey\fP\&. \fIflags\fP is one of:
1436.INDENT 7.0
1437.TP
1438.B \fBBPF_NOEXIST\fP
1439The entry for \fIkey\fP must not exist in the map.
1440.TP
1441.B \fBBPF_EXIST\fP
1442The entry for \fIkey\fP must already exist in the map.
1443.TP
1444.B \fBBPF_ANY\fP
1445No condition on the existence of the entry for \fIkey\fP\&.
1446.UNINDENT
1447.sp
1448If the \fImap\fP has eBPF programs (parser and verdict), those will
1449be inherited by the socket being added. If the socket is
1450already attached to eBPF programs, this results in an error.
1451.TP
1452.B Return
14530 on success, or a negative error in case of failure.
1454.UNINDENT
1455.TP
1456.B \fBint bpf_xdp_adjust_meta(struct xdp_buff *\fP\fIxdp_md\fP\fB, int\fP \fIdelta\fP\fB)\fP
1457.INDENT 7.0
1458.TP
1459.B Description
1460Adjust the address pointed by \fIxdp_md\fP\fB\->data_meta\fP by
1461\fIdelta\fP (which can be positive or negative). Note that this
1462operation modifies the address stored in \fIxdp_md\fP\fB\->data\fP,
1463so the latter must be loaded only after the helper has been
1464called.
1465.sp
1466The use of \fIxdp_md\fP\fB\->data_meta\fP is optional and programs
1467are not required to use it. The rationale is that when the
1468packet is processed with XDP (e.g. as DoS filter), it is
1469possible to push further meta data along with it before passing
1470to the stack, and to give the guarantee that an ingress eBPF
1471program attached as a TC classifier on the same device can pick
1472this up for further post\-processing. Since TC works with socket
1473buffers, it remains possible to set from XDP the \fBmark\fP or
1474\fBpriority\fP pointers, or other pointers for the socket buffer.
1475Having this scratch space generic and programmable allows for
1476more flexibility as the user is free to store whatever meta
1477data they need.
1478.sp
e6107b29 1479A call to this helper is susceptible to change the underlying
53666f6c
MK
1480packet buffer. Therefore, at load time, all checks on pointers
1481previously done by the verifier are invalidated and must be
1482performed again, if the helper is used in combination with
1483direct packet access.
1484.TP
1485.B Return
14860 on success, or a negative error in case of failure.
1487.UNINDENT
1488.TP
1489.B \fBint bpf_perf_event_read_value(struct bpf_map *\fP\fImap\fP\fB, u64\fP \fIflags\fP\fB, struct bpf_perf_event_value *\fP\fIbuf\fP\fB, u32\fP \fIbuf_size\fP\fB)\fP
1490.INDENT 7.0
1491.TP
1492.B Description
1493Read the value of a perf event counter, and store it into \fIbuf\fP
1494of size \fIbuf_size\fP\&. This helper relies on a \fImap\fP of type
1495\fBBPF_MAP_TYPE_PERF_EVENT_ARRAY\fP\&. The nature of the perf event
1496counter is selected when \fImap\fP is updated with perf event file
1497descriptors. The \fImap\fP is an array whose size is the number of
1498available CPUs, and each cell contains a value relative to one
1499CPU. The value to retrieve is indicated by \fIflags\fP, that
1500contains the index of the CPU to look up, masked with
1501\fBBPF_F_INDEX_MASK\fP\&. Alternatively, \fIflags\fP can be set to
1502\fBBPF_F_CURRENT_CPU\fP to indicate that the value for the
1503current CPU should be retrieved.
1504.sp
1505This helper behaves in a way close to
1506\fBbpf_perf_event_read\fP() helper, save that instead of
1507just returning the value observed, it fills the \fIbuf\fP
1508structure. This allows for additional data to be retrieved: in
1509particular, the enabled and running times (in \fIbuf\fP\fB\->enabled\fP and \fIbuf\fP\fB\->running\fP, respectively) are
1510copied. In general, \fBbpf_perf_event_read_value\fP() is
1511recommended over \fBbpf_perf_event_read\fP(), which has some
1512ABI issues and provides fewer functionalities.
1513.sp
1514These values are interesting, because hardware PMU (Performance
1515Monitoring Unit) counters are limited resources. When there are
1516more PMU based perf events opened than available counters,
1517kernel will multiplex these events so each event gets certain
1518percentage (but not all) of the PMU time. In case that
1519multiplexing happens, the number of samples or counter value
1520will not reflect the case compared to when no multiplexing
1521occurs. This makes comparison between different runs difficult.
1522Typically, the counter value should be normalized before
1523comparing to other experiments. The usual normalization is done
1524as follows.
1525.INDENT 7.0
1526.INDENT 3.5
1527.sp
1528.nf
1529.ft C
1530normalized_counter = counter * t_enabled / t_running
1531.ft P
1532.fi
1533.UNINDENT
1534.UNINDENT
1535.sp
1536Where t_enabled is the time enabled for event and t_running is
1537the time running for event since last normalization. The
1538enabled and running times are accumulated since the perf event
1539open. To achieve scaling factor between two invocations of an
1540eBPF program, users can can use CPU id as the key (which is
1541typical for perf array usage model) to remember the previous
1542value and do the calculation inside the eBPF program.
1543.TP
1544.B Return
15450 on success, or a negative error in case of failure.
1546.UNINDENT
1547.TP
1548.B \fBint bpf_perf_prog_read_value(struct bpf_perf_event_data *\fP\fIctx\fP\fB, struct bpf_perf_event_value *\fP\fIbuf\fP\fB, u32\fP \fIbuf_size\fP\fB)\fP
1549.INDENT 7.0
1550.TP
1551.B Description
1552For en eBPF program attached to a perf event, retrieve the
1553value of the event counter associated to \fIctx\fP and store it in
1554the structure pointed by \fIbuf\fP and of size \fIbuf_size\fP\&. Enabled
1555and running times are also stored in the structure (see
1556description of helper \fBbpf_perf_event_read_value\fP() for
1557more details).
1558.TP
1559.B Return
15600 on success, or a negative error in case of failure.
1561.UNINDENT
1562.TP
4e2b112c 1563.B \fBint bpf_getsockopt(struct bpf_sock_ops *\fP\fIbpf_socket\fP\fB, int\fP \fIlevel\fP\fB, int\fP \fIoptname\fP\fB, void *\fP\fIoptval\fP\fB, int\fP \fIoptlen\fP\fB)\fP
53666f6c
MK
1564.INDENT 7.0
1565.TP
1566.B Description
1567Emulate a call to \fBgetsockopt()\fP on the socket associated to
1568\fIbpf_socket\fP, which must be a full socket. The \fIlevel\fP at
1569which the option resides and the name \fIoptname\fP of the option
1570must be specified, see \fBgetsockopt(2)\fP for more information.
1571The retrieved value is stored in the structure pointed by
1572\fIopval\fP and of length \fIoptlen\fP\&.
1573.sp
1574This helper actually implements a subset of \fBgetsockopt()\fP\&.
1575It supports the following \fIlevel\fPs:
1576.INDENT 7.0
1577.IP \(bu 2
1578\fBIPPROTO_TCP\fP, which supports \fIoptname\fP
1579\fBTCP_CONGESTION\fP\&.
1580.IP \(bu 2
1581\fBIPPROTO_IP\fP, which supports \fIoptname\fP \fBIP_TOS\fP\&.
1582.IP \(bu 2
1583\fBIPPROTO_IPV6\fP, which supports \fIoptname\fP \fBIPV6_TCLASS\fP\&.
1584.UNINDENT
1585.TP
1586.B Return
15870 on success, or a negative error in case of failure.
1588.UNINDENT
1589.TP
e6107b29 1590.B \fBint bpf_override_return(struct pt_regs *\fP\fIregs\fP\fB, u64\fP \fIrc\fP\fB)\fP
53666f6c
MK
1591.INDENT 7.0
1592.TP
1593.B Description
1594Used for error injection, this helper uses kprobes to override
1595the return value of the probed function, and to set it to \fIrc\fP\&.
1596The first argument is the context \fIregs\fP on which the kprobe
1597works.
1598.sp
1599This helper works by setting setting the PC (program counter)
1600to an override function which is run in place of the original
1601probed function. This means the probed function is not run at
1602all. The replacement function just returns with the required
1603value.
1604.sp
1605This helper has security implications, and thus is subject to
1606restrictions. It is only available if the kernel was compiled
1607with the \fBCONFIG_BPF_KPROBE_OVERRIDE\fP configuration
1608option, and in this case it only works on functions tagged with
1609\fBALLOW_ERROR_INJECTION\fP in the kernel code.
1610.sp
1611Also, the helper is only available for the architectures having
1612the CONFIG_FUNCTION_ERROR_INJECTION option. As of this writing,
1613x86 architecture is the only one to support this feature.
1614.TP
1615.B Return
e6107b29 16160
53666f6c
MK
1617.UNINDENT
1618.TP
1619.B \fBint bpf_sock_ops_cb_flags_set(struct bpf_sock_ops *\fP\fIbpf_sock\fP\fB, int\fP \fIargval\fP\fB)\fP
1620.INDENT 7.0
1621.TP
1622.B Description
1623Attempt to set the value of the \fBbpf_sock_ops_cb_flags\fP field
1624for the full TCP socket associated to \fIbpf_sock_ops\fP to
1625\fIargval\fP\&.
1626.sp
1627The primary use of this field is to determine if there should
1628be calls to eBPF programs of type
1629\fBBPF_PROG_TYPE_SOCK_OPS\fP at various points in the TCP
1630code. A program of the same type can change its value, per
1631connection and as necessary, when the connection is
1632established. This field is directly accessible for reading, but
1633this helper must be used for updates in order to return an
1634error if an eBPF program tries to set a callback that is not
1635supported in the current kernel.
1636.sp
e6107b29 1637\fIargval\fP is a flag array which can combine these flags:
53666f6c
MK
1638.INDENT 7.0
1639.IP \(bu 2
1640\fBBPF_SOCK_OPS_RTO_CB_FLAG\fP (retransmission time out)
1641.IP \(bu 2
1642\fBBPF_SOCK_OPS_RETRANS_CB_FLAG\fP (retransmission)
1643.IP \(bu 2
1644\fBBPF_SOCK_OPS_STATE_CB_FLAG\fP (TCP state change)
e6107b29
MK
1645.IP \(bu 2
1646\fBBPF_SOCK_OPS_RTT_CB_FLAG\fP (every RTT)
1647.UNINDENT
1648.sp
1649Therefore, this function can be used to clear a callback flag by
1650setting the appropriate bit to zero. e.g. to disable the RTO
1651callback:
1652.INDENT 7.0
1653.TP
1654.B \fBbpf_sock_ops_cb_flags_set(bpf_sock,\fP
1655\fBbpf_sock\->bpf_sock_ops_cb_flags & ~BPF_SOCK_OPS_RTO_CB_FLAG)\fP
53666f6c
MK
1656.UNINDENT
1657.sp
1658Here are some examples of where one could call such eBPF
1659program:
1660.INDENT 7.0
1661.IP \(bu 2
1662When RTO fires.
1663.IP \(bu 2
1664When a packet is retransmitted.
1665.IP \(bu 2
1666When the connection terminates.
1667.IP \(bu 2
1668When a packet is sent.
1669.IP \(bu 2
1670When a packet is received.
1671.UNINDENT
1672.TP
1673.B Return
1674Code \fB\-EINVAL\fP if the socket is not a full TCP socket;
1675otherwise, a positive number containing the bits that could not
1676be set is returned (which comes down to 0 if all bits were set
1677as required).
1678.UNINDENT
1679.TP
1680.B \fBint bpf_msg_redirect_map(struct sk_msg_buff *\fP\fImsg\fP\fB, struct bpf_map *\fP\fImap\fP\fB, u32\fP \fIkey\fP\fB, u64\fP \fIflags\fP\fB)\fP
1681.INDENT 7.0
1682.TP
1683.B Description
1684This helper is used in programs implementing policies at the
1685socket level. If the message \fImsg\fP is allowed to pass (i.e. if
1686the verdict eBPF program returns \fBSK_PASS\fP), redirect it to
1687the socket referenced by \fImap\fP (of type
1688\fBBPF_MAP_TYPE_SOCKMAP\fP) at index \fIkey\fP\&. Both ingress and
1689egress interfaces can be used for redirection. The
1690\fBBPF_F_INGRESS\fP value in \fIflags\fP is used to make the
1691distinction (ingress path is selected if the flag is present,
1692egress path otherwise). This is the only flag supported for now.
1693.TP
1694.B Return
1695\fBSK_PASS\fP on success, or \fBSK_DROP\fP on error.
1696.UNINDENT
1697.TP
1698.B \fBint bpf_msg_apply_bytes(struct sk_msg_buff *\fP\fImsg\fP\fB, u32\fP \fIbytes\fP\fB)\fP
1699.INDENT 7.0
1700.TP
1701.B Description
1702For socket policies, apply the verdict of the eBPF program to
1703the next \fIbytes\fP (number of bytes) of message \fImsg\fP\&.
1704.sp
1705For example, this helper can be used in the following cases:
1706.INDENT 7.0
1707.IP \(bu 2
1708A single \fBsendmsg\fP() or \fBsendfile\fP() system call
1709contains multiple logical messages that the eBPF program is
1710supposed to read and for which it should apply a verdict.
1711.IP \(bu 2
1712An eBPF program only cares to read the first \fIbytes\fP of a
1713\fImsg\fP\&. If the message has a large payload, then setting up
1714and calling the eBPF program repeatedly for all bytes, even
1715though the verdict is already known, would create unnecessary
1716overhead.
1717.UNINDENT
1718.sp
1719When called from within an eBPF program, the helper sets a
1720counter internal to the BPF infrastructure, that is used to
1721apply the last verdict to the next \fIbytes\fP\&. If \fIbytes\fP is
1722smaller than the current data being processed from a
1723\fBsendmsg\fP() or \fBsendfile\fP() system call, the first
1724\fIbytes\fP will be sent and the eBPF program will be re\-run with
1725the pointer for start of data pointing to byte number \fIbytes\fP
1726\fB+ 1\fP\&. If \fIbytes\fP is larger than the current data being
1727processed, then the eBPF verdict will be applied to multiple
1728\fBsendmsg\fP() or \fBsendfile\fP() calls until \fIbytes\fP are
1729consumed.
1730.sp
1731Note that if a socket closes with the internal counter holding
1732a non\-zero value, this is not a problem because data is not
1733being buffered for \fIbytes\fP and is sent as it is received.
1734.TP
1735.B Return
e6107b29 17360
53666f6c
MK
1737.UNINDENT
1738.TP
1739.B \fBint bpf_msg_cork_bytes(struct sk_msg_buff *\fP\fImsg\fP\fB, u32\fP \fIbytes\fP\fB)\fP
1740.INDENT 7.0
1741.TP
1742.B Description
1743For socket policies, prevent the execution of the verdict eBPF
1744program for message \fImsg\fP until \fIbytes\fP (byte number) have been
1745accumulated.
1746.sp
1747This can be used when one needs a specific number of bytes
1748before a verdict can be assigned, even if the data spans
1749multiple \fBsendmsg\fP() or \fBsendfile\fP() calls. The extreme
1750case would be a user calling \fBsendmsg\fP() repeatedly with
17511\-byte long message segments. Obviously, this is bad for
1752performance, but it is still valid. If the eBPF program needs
1753\fIbytes\fP bytes to validate a header, this helper can be used to
1754prevent the eBPF program to be called again until \fIbytes\fP have
1755been accumulated.
1756.TP
1757.B Return
e6107b29 17580
53666f6c
MK
1759.UNINDENT
1760.TP
1761.B \fBint bpf_msg_pull_data(struct sk_msg_buff *\fP\fImsg\fP\fB, u32\fP \fIstart\fP\fB, u32\fP \fIend\fP\fB, u64\fP \fIflags\fP\fB)\fP
1762.INDENT 7.0
1763.TP
1764.B Description
1765For socket policies, pull in non\-linear data from user space
1766for \fImsg\fP and set pointers \fImsg\fP\fB\->data\fP and \fImsg\fP\fB\->data_end\fP to \fIstart\fP and \fIend\fP bytes offsets into \fImsg\fP,
1767respectively.
1768.sp
1769If a program of type \fBBPF_PROG_TYPE_SK_MSG\fP is run on a
1770\fImsg\fP it can only parse data that the (\fBdata\fP, \fBdata_end\fP)
1771pointers have already consumed. For \fBsendmsg\fP() hooks this
1772is likely the first scatterlist element. But for calls relying
1773on the \fBsendpage\fP handler (e.g. \fBsendfile\fP()) this will
1774be the range (\fB0\fP, \fB0\fP) because the data is shared with
1775user space and by default the objective is to avoid allowing
1776user space to modify data while (or after) eBPF verdict is
1777being decided. This helper can be used to pull in data and to
1778set the start and end pointer to given values. Data will be
1779copied if necessary (i.e. if data was not linear and if start
1780and end pointers do not point to the same chunk).
1781.sp
e6107b29 1782A call to this helper is susceptible to change the underlying
53666f6c
MK
1783packet buffer. Therefore, at load time, all checks on pointers
1784previously done by the verifier are invalidated and must be
1785performed again, if the helper is used in combination with
1786direct packet access.
1787.sp
1788All values for \fIflags\fP are reserved for future usage, and must
1789be left at zero.
1790.TP
1791.B Return
17920 on success, or a negative error in case of failure.
1793.UNINDENT
1794.TP
1795.B \fBint bpf_bind(struct bpf_sock_addr *\fP\fIctx\fP\fB, struct sockaddr *\fP\fIaddr\fP\fB, int\fP \fIaddr_len\fP\fB)\fP
1796.INDENT 7.0
1797.TP
1798.B Description
1799Bind the socket associated to \fIctx\fP to the address pointed by
1800\fIaddr\fP, of length \fIaddr_len\fP\&. This allows for making outgoing
1801connection from the desired IP address, which can be useful for
1802example when all processes inside a cgroup should use one
1803single IP address on a host that has multiple IP configured.
1804.sp
1805This helper works for IPv4 and IPv6, TCP and UDP sockets. The
1806domain (\fIaddr\fP\fB\->sa_family\fP) must be \fBAF_INET\fP (or
1807\fBAF_INET6\fP). Looking for a free port to bind to can be
1808expensive, therefore binding to port is not permitted by the
1809helper: \fIaddr\fP\fB\->sin_port\fP (or \fBsin6_port\fP, respectively)
1810must be set to zero.
1811.TP
1812.B Return
18130 on success, or a negative error in case of failure.
1814.UNINDENT
1815.TP
1816.B \fBint bpf_xdp_adjust_tail(struct xdp_buff *\fP\fIxdp_md\fP\fB, int\fP \fIdelta\fP\fB)\fP
1817.INDENT 7.0
1818.TP
1819.B Description
1820Adjust (move) \fIxdp_md\fP\fB\->data_end\fP by \fIdelta\fP bytes. It is
1821only possible to shrink the packet as of this writing,
1822therefore \fIdelta\fP must be a negative integer.
1823.sp
e6107b29 1824A call to this helper is susceptible to change the underlying
53666f6c
MK
1825packet buffer. Therefore, at load time, all checks on pointers
1826previously done by the verifier are invalidated and must be
1827performed again, if the helper is used in combination with
1828direct packet access.
1829.TP
1830.B Return
18310 on success, or a negative error in case of failure.
1832.UNINDENT
1833.TP
1834.B \fBint bpf_skb_get_xfrm_state(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIindex\fP\fB, struct bpf_xfrm_state *\fP\fIxfrm_state\fP\fB, u32\fP \fIsize\fP\fB, u64\fP \fIflags\fP\fB)\fP
1835.INDENT 7.0
1836.TP
1837.B Description
1838Retrieve the XFRM state (IP transform framework, see also
1839\fBip\-xfrm(8)\fP) at \fIindex\fP in XFRM "security path" for \fIskb\fP\&.
1840.sp
1841The retrieved value is stored in the \fBstruct bpf_xfrm_state\fP
1842pointed by \fIxfrm_state\fP and of length \fIsize\fP\&.
1843.sp
1844All values for \fIflags\fP are reserved for future usage, and must
1845be left at zero.
1846.sp
1847This helper is available only if the kernel was compiled with
1848\fBCONFIG_XFRM\fP configuration option.
1849.TP
1850.B Return
18510 on success, or a negative error in case of failure.
1852.UNINDENT
1853.TP
4e2b112c 1854.B \fBint bpf_get_stack(void *\fP\fIctx\fP\fB, void *\fP\fIbuf\fP\fB, u32\fP \fIsize\fP\fB, u64\fP \fIflags\fP\fB)\fP
53666f6c
MK
1855.INDENT 7.0
1856.TP
1857.B Description
1858Return a user or a kernel stack in bpf program provided buffer.
1859To achieve this, the helper needs \fIctx\fP, which is a pointer
1860to the context on which the tracing program is executed.
1861To store the stacktrace, the bpf program provides \fIbuf\fP with
1862a nonnegative \fIsize\fP\&.
1863.sp
1864The last argument, \fIflags\fP, holds the number of stack frames to
1865skip (from 0 to 255), masked with
1866\fBBPF_F_SKIP_FIELD_MASK\fP\&. The next bits can be used to set
1867the following flags:
1868.INDENT 7.0
1869.TP
1870.B \fBBPF_F_USER_STACK\fP
1871Collect a user space stack instead of a kernel stack.
1872.TP
1873.B \fBBPF_F_USER_BUILD_ID\fP
1874Collect buildid+offset instead of ips for user stack,
1875only valid if \fBBPF_F_USER_STACK\fP is also specified.
1876.UNINDENT
1877.sp
1878\fBbpf_get_stack\fP() can collect up to
1879\fBPERF_MAX_STACK_DEPTH\fP both kernel and user frames, subject
1880to sufficient large buffer size. Note that
1881this limit can be controlled with the \fBsysctl\fP program, and
1882that it should be manually increased in order to profile long
1883user stacks (such as stacks for Java programs). To do so, use:
1884.INDENT 7.0
1885.INDENT 3.5
1886.sp
1887.nf
1888.ft C
1889# sysctl kernel.perf_event_max_stack=<new value>
1890.ft P
1891.fi
1892.UNINDENT
1893.UNINDENT
1894.TP
1895.B Return
1896A non\-negative value equal to or less than \fIsize\fP on success,
1897or a negative error in case of failure.
1898.UNINDENT
1899.TP
4e2b112c 1900.B \fBint bpf_skb_load_bytes_relative(const void *\fP\fIskb\fP\fB, u32\fP \fIoffset\fP\fB, void *\fP\fIto\fP\fB, u32\fP \fIlen\fP\fB, u32\fP \fIstart_header\fP\fB)\fP
53666f6c
MK
1901.INDENT 7.0
1902.TP
1903.B Description
1904This helper is similar to \fBbpf_skb_load_bytes\fP() in that
1905it provides an easy way to load \fIlen\fP bytes from \fIoffset\fP
1906from the packet associated to \fIskb\fP, into the buffer pointed
1907by \fIto\fP\&. The difference to \fBbpf_skb_load_bytes\fP() is that
1908a fifth argument \fIstart_header\fP exists in order to select a
1909base offset to start from. \fIstart_header\fP can be one of:
1910.INDENT 7.0
1911.TP
1912.B \fBBPF_HDR_START_MAC\fP
1913Base offset to load data from is \fIskb\fP\(aqs mac header.
1914.TP
1915.B \fBBPF_HDR_START_NET\fP
1916Base offset to load data from is \fIskb\fP\(aqs network header.
1917.UNINDENT
1918.sp
1919In general, "direct packet access" is the preferred method to
1920access packet data, however, this helper is in particular useful
1921in socket filters where \fIskb\fP\fB\->data\fP does not always point
1922to the start of the mac header and where "direct packet access"
1923is not available.
1924.TP
1925.B Return
19260 on success, or a negative error in case of failure.
1927.UNINDENT
1928.TP
1929.B \fBint bpf_fib_lookup(void *\fP\fIctx\fP\fB, struct bpf_fib_lookup *\fP\fIparams\fP\fB, int\fP \fIplen\fP\fB, u32\fP \fIflags\fP\fB)\fP
1930.INDENT 7.0
1931.TP
1932.B Description
1933Do FIB lookup in kernel tables using parameters in \fIparams\fP\&.
1934If lookup is successful and result shows packet is to be
1935forwarded, the neighbor tables are searched for the nexthop.
1936If successful (ie., FIB lookup shows forwarding and nexthop
1937is resolved), the nexthop address is returned in ipv4_dst
1938or ipv6_dst based on family, smac is set to mac address of
1939egress device, dmac is set to nexthop mac address, rt_metric
1940is set to metric from route (IPv4/IPv6 only), and ifindex
1941is set to the device index of the nexthop from the FIB lookup.
1942.sp
1943\fIplen\fP argument is the size of the passed in struct.
1944\fIflags\fP argument can be a combination of one or more of the
1945following values:
1946.INDENT 7.0
1947.TP
1948.B \fBBPF_FIB_LOOKUP_DIRECT\fP
1949Do a direct table lookup vs full lookup using FIB
1950rules.
1951.TP
1952.B \fBBPF_FIB_LOOKUP_OUTPUT\fP
1953Perform lookup from an egress perspective (default is
1954ingress).
1955.UNINDENT
1956.sp
1957\fIctx\fP is either \fBstruct xdp_md\fP for XDP programs or
1958\fBstruct sk_buff\fP tc cls_act programs.
1959.TP
1960.B Return
1961.INDENT 7.0
1962.IP \(bu 2
1963< 0 if any input argument is invalid
1964.IP \(bu 2
19650 on success (packet is forwarded, nexthop neighbor exists)
1966.IP \(bu 2
1967> 0 one of \fBBPF_FIB_LKUP_RET_\fP codes explaining why the
1968packet is not forwarded or needs assist from full stack
1969.UNINDENT
1970.UNINDENT
1971.TP
4e2b112c 1972.B \fBint bpf_sock_hash_update(struct bpf_sock_ops *\fP\fIskops\fP\fB, struct bpf_map *\fP\fImap\fP\fB, void *\fP\fIkey\fP\fB, u64\fP \fIflags\fP\fB)\fP
53666f6c
MK
1973.INDENT 7.0
1974.TP
1975.B Description
1976Add an entry to, or update a sockhash \fImap\fP referencing sockets.
1977The \fIskops\fP is used as a new value for the entry associated to
1978\fIkey\fP\&. \fIflags\fP is one of:
1979.INDENT 7.0
1980.TP
1981.B \fBBPF_NOEXIST\fP
1982The entry for \fIkey\fP must not exist in the map.
1983.TP
1984.B \fBBPF_EXIST\fP
1985The entry for \fIkey\fP must already exist in the map.
1986.TP
1987.B \fBBPF_ANY\fP
1988No condition on the existence of the entry for \fIkey\fP\&.
1989.UNINDENT
1990.sp
1991If the \fImap\fP has eBPF programs (parser and verdict), those will
1992be inherited by the socket being added. If the socket is
1993already attached to eBPF programs, this results in an error.
1994.TP
1995.B Return
19960 on success, or a negative error in case of failure.
1997.UNINDENT
1998.TP
1999.B \fBint bpf_msg_redirect_hash(struct sk_msg_buff *\fP\fImsg\fP\fB, struct bpf_map *\fP\fImap\fP\fB, void *\fP\fIkey\fP\fB, u64\fP \fIflags\fP\fB)\fP
2000.INDENT 7.0
2001.TP
2002.B Description
2003This helper is used in programs implementing policies at the
2004socket level. If the message \fImsg\fP is allowed to pass (i.e. if
2005the verdict eBPF program returns \fBSK_PASS\fP), redirect it to
2006the socket referenced by \fImap\fP (of type
2007\fBBPF_MAP_TYPE_SOCKHASH\fP) using hash \fIkey\fP\&. Both ingress and
2008egress interfaces can be used for redirection. The
2009\fBBPF_F_INGRESS\fP value in \fIflags\fP is used to make the
2010distinction (ingress path is selected if the flag is present,
2011egress path otherwise). This is the only flag supported for now.
2012.TP
2013.B Return
2014\fBSK_PASS\fP on success, or \fBSK_DROP\fP on error.
2015.UNINDENT
2016.TP
2017.B \fBint bpf_sk_redirect_hash(struct sk_buff *\fP\fIskb\fP\fB, struct bpf_map *\fP\fImap\fP\fB, void *\fP\fIkey\fP\fB, u64\fP \fIflags\fP\fB)\fP
2018.INDENT 7.0
2019.TP
2020.B Description
2021This helper is used in programs implementing policies at the
2022skb socket level. If the sk_buff \fIskb\fP is allowed to pass (i.e.
2023if the verdeict eBPF program returns \fBSK_PASS\fP), redirect it
2024to the socket referenced by \fImap\fP (of type
2025\fBBPF_MAP_TYPE_SOCKHASH\fP) using hash \fIkey\fP\&. Both ingress and
2026egress interfaces can be used for redirection. The
2027\fBBPF_F_INGRESS\fP value in \fIflags\fP is used to make the
2028distinction (ingress path is selected if the flag is present,
2029egress otherwise). This is the only flag supported for now.
2030.TP
2031.B Return
2032\fBSK_PASS\fP on success, or \fBSK_DROP\fP on error.
2033.UNINDENT
2034.TP
2035.B \fBint bpf_lwt_push_encap(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fItype\fP\fB, void *\fP\fIhdr\fP\fB, u32\fP \fIlen\fP\fB)\fP
2036.INDENT 7.0
2037.TP
2038.B Description
2039Encapsulate the packet associated to \fIskb\fP within a Layer 3
2040protocol header. This header is provided in the buffer at
2041address \fIhdr\fP, with \fIlen\fP its size in bytes. \fItype\fP indicates
2042the protocol of the header and can be one of:
2043.INDENT 7.0
2044.TP
2045.B \fBBPF_LWT_ENCAP_SEG6\fP
2046IPv6 encapsulation with Segment Routing Header
2047(\fBstruct ipv6_sr_hdr\fP). \fIhdr\fP only contains the SRH,
2048the IPv6 header is computed by the kernel.
2049.TP
2050.B \fBBPF_LWT_ENCAP_SEG6_INLINE\fP
2051Only works if \fIskb\fP contains an IPv6 packet. Insert a
2052Segment Routing Header (\fBstruct ipv6_sr_hdr\fP) inside
2053the IPv6 header.
e6107b29
MK
2054.TP
2055.B \fBBPF_LWT_ENCAP_IP\fP
2056IP encapsulation (GRE/GUE/IPIP/etc). The outer header
2057must be IPv4 or IPv6, followed by zero or more
2058additional headers, up to \fBLWT_BPF_MAX_HEADROOM\fP
2059total bytes in all prepended headers. Please note that
2060if \fBskb_is_gso\fP(\fIskb\fP) is true, no more than two
2061headers can be prepended, and the inner header, if
2062present, should be either GRE or UDP/GUE.
53666f6c
MK
2063.UNINDENT
2064.sp
e6107b29
MK
2065\fBBPF_LWT_ENCAP_SEG6\fP* types can be called by BPF programs
2066of type \fBBPF_PROG_TYPE_LWT_IN\fP; \fBBPF_LWT_ENCAP_IP\fP type can
2067be called by bpf programs of types \fBBPF_PROG_TYPE_LWT_IN\fP and
2068\fBBPF_PROG_TYPE_LWT_XMIT\fP\&.
2069.sp
2070A call to this helper is susceptible to change the underlying
53666f6c
MK
2071packet buffer. Therefore, at load time, all checks on pointers
2072previously done by the verifier are invalidated and must be
2073performed again, if the helper is used in combination with
2074direct packet access.
2075.TP
2076.B Return
20770 on success, or a negative error in case of failure.
2078.UNINDENT
2079.TP
2080.B \fBint bpf_lwt_seg6_store_bytes(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIoffset\fP\fB, const void *\fP\fIfrom\fP\fB, u32\fP \fIlen\fP\fB)\fP
2081.INDENT 7.0
2082.TP
2083.B Description
2084Store \fIlen\fP bytes from address \fIfrom\fP into the packet
2085associated to \fIskb\fP, at \fIoffset\fP\&. Only the flags, tag and TLVs
2086inside the outermost IPv6 Segment Routing Header can be
2087modified through this helper.
2088.sp
e6107b29 2089A call to this helper is susceptible to change the underlying
53666f6c
MK
2090packet buffer. Therefore, at load time, all checks on pointers
2091previously done by the verifier are invalidated and must be
2092performed again, if the helper is used in combination with
2093direct packet access.
2094.TP
2095.B Return
20960 on success, or a negative error in case of failure.
2097.UNINDENT
2098.TP
2099.B \fBint bpf_lwt_seg6_adjust_srh(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIoffset\fP\fB, s32\fP \fIdelta\fP\fB)\fP
2100.INDENT 7.0
2101.TP
2102.B Description
2103Adjust the size allocated to TLVs in the outermost IPv6
2104Segment Routing Header contained in the packet associated to
2105\fIskb\fP, at position \fIoffset\fP by \fIdelta\fP bytes. Only offsets
2106after the segments are accepted. \fIdelta\fP can be as well
2107positive (growing) as negative (shrinking).
2108.sp
e6107b29 2109A call to this helper is susceptible to change the underlying
53666f6c
MK
2110packet buffer. Therefore, at load time, all checks on pointers
2111previously done by the verifier are invalidated and must be
2112performed again, if the helper is used in combination with
2113direct packet access.
2114.TP
2115.B Return
21160 on success, or a negative error in case of failure.
2117.UNINDENT
2118.TP
2119.B \fBint bpf_lwt_seg6_action(struct sk_buff *\fP\fIskb\fP\fB, u32\fP \fIaction\fP\fB, void *\fP\fIparam\fP\fB, u32\fP \fIparam_len\fP\fB)\fP
2120.INDENT 7.0
2121.TP
2122.B Description
2123Apply an IPv6 Segment Routing action of type \fIaction\fP to the
2124packet associated to \fIskb\fP\&. Each action takes a parameter
2125contained at address \fIparam\fP, and of length \fIparam_len\fP bytes.
2126\fIaction\fP can be one of:
2127.INDENT 7.0
2128.TP
2129.B \fBSEG6_LOCAL_ACTION_END_X\fP
2130End.X action: Endpoint with Layer\-3 cross\-connect.
2131Type of \fIparam\fP: \fBstruct in6_addr\fP\&.
2132.TP
2133.B \fBSEG6_LOCAL_ACTION_END_T\fP
2134End.T action: Endpoint with specific IPv6 table lookup.
2135Type of \fIparam\fP: \fBint\fP\&.
2136.TP
2137.B \fBSEG6_LOCAL_ACTION_END_B6\fP
2138End.B6 action: Endpoint bound to an SRv6 policy.
e6107b29 2139Type of \fIparam\fP: \fBstruct ipv6_sr_hdr\fP\&.
53666f6c
MK
2140.TP
2141.B \fBSEG6_LOCAL_ACTION_END_B6_ENCAP\fP
2142End.B6.Encap action: Endpoint bound to an SRv6
2143encapsulation policy.
e6107b29 2144Type of \fIparam\fP: \fBstruct ipv6_sr_hdr\fP\&.
53666f6c
MK
2145.UNINDENT
2146.sp
e6107b29 2147A call to this helper is susceptible to change the underlying
53666f6c
MK
2148packet buffer. Therefore, at load time, all checks on pointers
2149previously done by the verifier are invalidated and must be
2150performed again, if the helper is used in combination with
2151direct packet access.
2152.TP
2153.B Return
21540 on success, or a negative error in case of failure.
2155.UNINDENT
2156.TP
e6107b29 2157.B \fBint bpf_rc_repeat(void *\fP\fIctx\fP\fB)\fP
53666f6c
MK
2158.INDENT 7.0
2159.TP
2160.B Description
2161This helper is used in programs implementing IR decoding, to
e6107b29
MK
2162report a successfully decoded repeat key message. This delays
2163the generation of a key up event for previously generated
2164key down event.
53666f6c 2165.sp
e6107b29
MK
2166Some IR protocols like NEC have a special IR message for
2167repeating last button, for when a button is held down.
53666f6c
MK
2168.sp
2169The \fIctx\fP should point to the lirc sample as passed into
2170the program.
2171.sp
53666f6c
MK
2172This helper is only available is the kernel was compiled with
2173the \fBCONFIG_BPF_LIRC_MODE2\fP configuration option set to
2174"\fBy\fP".
2175.TP
2176.B Return
e6107b29 21770
53666f6c
MK
2178.UNINDENT
2179.TP
e6107b29 2180.B \fBint bpf_rc_keydown(void *\fP\fIctx\fP\fB, u32\fP \fIprotocol\fP\fB, u64\fP \fIscancode\fP\fB, u32\fP \fItoggle\fP\fB)\fP
53666f6c
MK
2181.INDENT 7.0
2182.TP
2183.B Description
2184This helper is used in programs implementing IR decoding, to
e6107b29
MK
2185report a successfully decoded key press with \fIscancode\fP,
2186\fItoggle\fP value in the given \fIprotocol\fP\&. The scancode will be
2187translated to a keycode using the rc keymap, and reported as
2188an input key down event. After a period a key up event is
2189generated. This period can be extended by calling either
2190\fBbpf_rc_keydown\fP() again with the same values, or calling
2191\fBbpf_rc_repeat\fP().
53666f6c 2192.sp
e6107b29
MK
2193Some protocols include a toggle bit, in case the button was
2194released and pressed again between consecutive scancodes.
53666f6c
MK
2195.sp
2196The \fIctx\fP should point to the lirc sample as passed into
2197the program.
2198.sp
e6107b29
MK
2199The \fIprotocol\fP is the decoded protocol number (see
2200\fBenum rc_proto\fP for some predefined values).
2201.sp
53666f6c
MK
2202This helper is only available is the kernel was compiled with
2203the \fBCONFIG_BPF_LIRC_MODE2\fP configuration option set to
2204"\fBy\fP".
2205.TP
2206.B Return
e6107b29 22070
53666f6c
MK
2208.UNINDENT
2209.TP
e6107b29 2210.B \fBu64 bpf_skb_cgroup_id(struct sk_buff *\fP\fIskb\fP\fB)\fP
53666f6c
MK
2211.INDENT 7.0
2212.TP
2213.B Description
2214Return the cgroup v2 id of the socket associated with the \fIskb\fP\&.
2215This is roughly similar to the \fBbpf_get_cgroup_classid\fP()
2216helper for cgroup v1 by providing a tag resp. identifier that
2217can be matched on or used for map lookups e.g. to implement
2218policy. The cgroup v2 id of a given path in the hierarchy is
2219exposed in user space through the f_handle API in order to get
2220to the same 64\-bit id.
2221.sp
2222This helper can be used on TC egress path, but not on ingress,
2223and is available only if the kernel was compiled with the
2224\fBCONFIG_SOCK_CGROUP_DATA\fP configuration option.
2225.TP
2226.B Return
2227The id is returned or 0 in case the id could not be retrieved.
2228.UNINDENT
2229.TP
53666f6c
MK
2230.B \fBu64 bpf_get_current_cgroup_id(void)\fP
2231.INDENT 7.0
2232.TP
2233.B Return
2234A 64\-bit integer containing the current cgroup id based
2235on the cgroup within which the current task is running.
2236.UNINDENT
2237.TP
e6107b29 2238.B \fBvoid *bpf_get_local_storage(void *\fP\fImap\fP\fB, u64\fP \fIflags\fP\fB)\fP
53666f6c
MK
2239.INDENT 7.0
2240.TP
2241.B Description
2242Get the pointer to the local storage area.
2243The type and the size of the local storage is defined
2244by the \fImap\fP argument.
2245The \fIflags\fP meaning is specific for each map type,
2246and has to be 0 for cgroup local storage.
2247.sp
2223d7df
MK
2248Depending on the BPF program type, a local storage area
2249can be shared between multiple instances of the BPF program,
53666f6c
MK
2250running simultaneously.
2251.sp
e6107b29 2252A user should care about the synchronization by himself.
2223d7df 2253For example, by using the \fBBPF_STX_XADD\fP instruction to alter
53666f6c
MK
2254the shared data.
2255.TP
2256.B Return
2223d7df 2257A pointer to the local storage area.
53666f6c
MK
2258.UNINDENT
2259.TP
2260.B \fBint bpf_sk_select_reuseport(struct sk_reuseport_md *\fP\fIreuse\fP\fB, struct bpf_map *\fP\fImap\fP\fB, void *\fP\fIkey\fP\fB, u64\fP \fIflags\fP\fB)\fP
2261.INDENT 7.0
2262.TP
2263.B Description
2223d7df
MK
2264Select a \fBSO_REUSEPORT\fP socket from a
2265\fBBPF_MAP_TYPE_REUSEPORT_ARRAY\fP \fImap\fP\&.
2266It checks the selected socket is matching the incoming
2267request in the socket buffer.
53666f6c
MK
2268.TP
2269.B Return
22700 on success, or a negative error in case of failure.
2271.UNINDENT
2223d7df 2272.TP
e6107b29
MK
2273.B \fBu64 bpf_skb_ancestor_cgroup_id(struct sk_buff *\fP\fIskb\fP\fB, int\fP \fIancestor_level\fP\fB)\fP
2274.INDENT 7.0
2275.TP
2276.B Description
2277Return id of cgroup v2 that is ancestor of cgroup associated
2278with the \fIskb\fP at the \fIancestor_level\fP\&. The root cgroup is at
2279\fIancestor_level\fP zero and each step down the hierarchy
2280increments the level. If \fIancestor_level\fP == level of cgroup
2281associated with \fIskb\fP, then return value will be same as that
2282of \fBbpf_skb_cgroup_id\fP().
2283.sp
2284The helper is useful to implement policies based on cgroups
2285that are upper in hierarchy than immediate cgroup associated
2286with \fIskb\fP\&.
2287.sp
2288The format of returned id and helper limitations are same as in
2289\fBbpf_skb_cgroup_id\fP().
2290.TP
2291.B Return
2292The id is returned or 0 in case the id could not be retrieved.
2293.UNINDENT
2294.TP
2223d7df
MK
2295.B \fBstruct bpf_sock *bpf_sk_lookup_tcp(void *\fP\fIctx\fP\fB, struct bpf_sock_tuple *\fP\fItuple\fP\fB, u32\fP \fItuple_size\fP\fB, u64\fP \fInetns\fP\fB, u64\fP \fIflags\fP\fB)\fP
2296.INDENT 7.0
2297.TP
2298.B Description
2299Look for TCP socket matching \fItuple\fP, optionally in a child
2300network namespace \fInetns\fP\&. The return value must be checked,
2301and if non\-\fBNULL\fP, released via \fBbpf_sk_release\fP().
2302.sp
2303The \fIctx\fP should point to the context of the program, such as
2304the skb or socket (depending on the hook in use). This is used
2305to determine the base network namespace for the lookup.
2306.sp
2307\fItuple_size\fP must be one of:
2308.INDENT 7.0
2309.TP
2310.B \fBsizeof\fP(\fItuple\fP\fB\->ipv4\fP)
2311Look for an IPv4 socket.
2312.TP
2313.B \fBsizeof\fP(\fItuple\fP\fB\->ipv6\fP)
2314Look for an IPv6 socket.
2315.UNINDENT
2316.sp
2317If the \fInetns\fP is a negative signed 32\-bit integer, then the
2318socket lookup table in the netns associated with the \fIctx\fP will
2319will be used. For the TC hooks, this is the netns of the device
2320in the skb. For socket hooks, this is the netns of the socket.
2321If \fInetns\fP is any other signed 32\-bit value greater than or
2322equal to zero then it specifies the ID of the netns relative to
2323the netns associated with the \fIctx\fP\&. \fInetns\fP values beyond the
2324range of 32\-bit integers are reserved for future use.
2325.sp
2326All values for \fIflags\fP are reserved for future usage, and must
2327be left at zero.
2328.sp
2329This helper is available only if the kernel was compiled with
2330\fBCONFIG_NET\fP configuration option.
2331.TP
2332.B Return
2333Pointer to \fBstruct bpf_sock\fP, or \fBNULL\fP in case of failure.
2334For sockets with reuseport option, the \fBstruct bpf_sock\fP
e6107b29
MK
2335result is from \fIreuse\fP\fB\->socks\fP[] using the hash of the
2336tuple.
2223d7df
MK
2337.UNINDENT
2338.TP
2339.B \fBstruct bpf_sock *bpf_sk_lookup_udp(void *\fP\fIctx\fP\fB, struct bpf_sock_tuple *\fP\fItuple\fP\fB, u32\fP \fItuple_size\fP\fB, u64\fP \fInetns\fP\fB, u64\fP \fIflags\fP\fB)\fP
2340.INDENT 7.0
2341.TP
2342.B Description
2343Look for UDP socket matching \fItuple\fP, optionally in a child
2344network namespace \fInetns\fP\&. The return value must be checked,
2345and if non\-\fBNULL\fP, released via \fBbpf_sk_release\fP().
2346.sp
2347The \fIctx\fP should point to the context of the program, such as
2348the skb or socket (depending on the hook in use). This is used
2349to determine the base network namespace for the lookup.
2350.sp
2351\fItuple_size\fP must be one of:
2352.INDENT 7.0
2353.TP
2354.B \fBsizeof\fP(\fItuple\fP\fB\->ipv4\fP)
2355Look for an IPv4 socket.
2356.TP
2357.B \fBsizeof\fP(\fItuple\fP\fB\->ipv6\fP)
2358Look for an IPv6 socket.
2359.UNINDENT
2360.sp
2361If the \fInetns\fP is a negative signed 32\-bit integer, then the
2362socket lookup table in the netns associated with the \fIctx\fP will
2363will be used. For the TC hooks, this is the netns of the device
2364in the skb. For socket hooks, this is the netns of the socket.
2365If \fInetns\fP is any other signed 32\-bit value greater than or
2366equal to zero then it specifies the ID of the netns relative to
2367the netns associated with the \fIctx\fP\&. \fInetns\fP values beyond the
2368range of 32\-bit integers are reserved for future use.
2369.sp
2370All values for \fIflags\fP are reserved for future usage, and must
2371be left at zero.
2372.sp
2373This helper is available only if the kernel was compiled with
2374\fBCONFIG_NET\fP configuration option.
2375.TP
2376.B Return
2377Pointer to \fBstruct bpf_sock\fP, or \fBNULL\fP in case of failure.
2378For sockets with reuseport option, the \fBstruct bpf_sock\fP
e6107b29
MK
2379result is from \fIreuse\fP\fB\->socks\fP[] using the hash of the
2380tuple.
2223d7df
MK
2381.UNINDENT
2382.TP
2383.B \fBint bpf_sk_release(struct bpf_sock *\fP\fIsock\fP\fB)\fP
2384.INDENT 7.0
2385.TP
2386.B Description
2387Release the reference held by \fIsock\fP\&. \fIsock\fP must be a
2388non\-\fBNULL\fP pointer that was returned from
2389\fBbpf_sk_lookup_xxx\fP().
2390.TP
2391.B Return
23920 on success, or a negative error in case of failure.
2393.UNINDENT
2394.TP
e6107b29
MK
2395.B \fBint bpf_map_push_elem(struct bpf_map *\fP\fImap\fP\fB, const void *\fP\fIvalue\fP\fB, u64\fP \fIflags\fP\fB)\fP
2396.INDENT 7.0
2397.TP
2398.B Description
2399Push an element \fIvalue\fP in \fImap\fP\&. \fIflags\fP is one of:
2400.INDENT 7.0
2401.TP
2402.B \fBBPF_EXIST\fP
2403If the queue/stack is full, the oldest element is
2404removed to make room for this.
2405.UNINDENT
2406.TP
2407.B Return
24080 on success, or a negative error in case of failure.
2409.UNINDENT
2410.TP
2223d7df
MK
2411.B \fBint bpf_map_pop_elem(struct bpf_map *\fP\fImap\fP\fB, void *\fP\fIvalue\fP\fB)\fP
2412.INDENT 7.0
2413.TP
2414.B Description
2415Pop an element from \fImap\fP\&.
2416.TP
2417.B Return
24180 on success, or a negative error in case of failure.
2419.UNINDENT
2420.TP
2421.B \fBint bpf_map_peek_elem(struct bpf_map *\fP\fImap\fP\fB, void *\fP\fIvalue\fP\fB)\fP
2422.INDENT 7.0
2423.TP
2424.B Description
2425Get an element from \fImap\fP without removing it.
2426.TP
2427.B Return
24280 on success, or a negative error in case of failure.
2429.UNINDENT
2430.TP
4e2b112c 2431.B \fBint bpf_msg_push_data(struct sk_msg_buff *\fP\fImsg\fP\fB, u32\fP \fIstart\fP\fB, u32\fP \fIlen\fP\fB, u64\fP \fIflags\fP\fB)\fP
2223d7df
MK
2432.INDENT 7.0
2433.TP
2434.B Description
2435For socket policies, insert \fIlen\fP bytes into \fImsg\fP at offset
2436\fIstart\fP\&.
2437.sp
2438If a program of type \fBBPF_PROG_TYPE_SK_MSG\fP is run on a
2439\fImsg\fP it may want to insert metadata or options into the \fImsg\fP\&.
2440This can later be read and used by any of the lower layer BPF
2441hooks.
2442.sp
2443This helper may fail if under memory pressure (a malloc
2444fails) in these cases BPF programs will get an appropriate
2445error and BPF programs will need to handle them.
2446.TP
2447.B Return
24480 on success, or a negative error in case of failure.
2449.UNINDENT
2450.TP
4e2b112c 2451.B \fBint bpf_msg_pop_data(struct sk_msg_buff *\fP\fImsg\fP\fB, u32\fP \fIstart\fP\fB, u32\fP \fIlen\fP\fB, u64\fP \fIflags\fP\fB)\fP
2223d7df
MK
2452.INDENT 7.0
2453.TP
2454.B Description
4e2b112c 2455Will remove \fIlen\fP bytes from a \fImsg\fP starting at byte \fIstart\fP\&.
2223d7df
MK
2456This may result in \fBENOMEM\fP errors under certain situations if
2457an allocation and copy are required due to a full ring buffer.
2458However, the helper will try to avoid doing the allocation
2459if possible. Other errors can occur if input parameters are
2460invalid either due to \fIstart\fP byte not being valid part of \fImsg\fP
2461payload and/or \fIpop\fP value being to large.
2462.TP
2463.B Return
24640 on success, or a negative error in case of failure.
2465.UNINDENT
2466.TP
2467.B \fBint bpf_rc_pointer_rel(void *\fP\fIctx\fP\fB, s32\fP \fIrel_x\fP\fB, s32\fP \fIrel_y\fP\fB)\fP
2468.INDENT 7.0
2469.TP
2470.B Description
2471This helper is used in programs implementing IR decoding, to
2472report a successfully decoded pointer movement.
2473.sp
2474The \fIctx\fP should point to the lirc sample as passed into
2475the program.
2476.sp
2477This helper is only available is the kernel was compiled with
2478the \fBCONFIG_BPF_LIRC_MODE2\fP configuration option set to
2479"\fBy\fP".
2480.TP
2481.B Return
e6107b29
MK
24820
2483.UNINDENT
2484.TP
2485.B \fBint bpf_spin_lock(struct bpf_spin_lock *\fP\fIlock\fP\fB)\fP
2486.INDENT 7.0
2487.TP
2488.B Description
2489Acquire a spinlock represented by the pointer \fIlock\fP, which is
2490stored as part of a value of a map. Taking the lock allows to
2491safely update the rest of the fields in that value. The
2492spinlock can (and must) later be released with a call to
2493\fBbpf_spin_unlock\fP(\fIlock\fP).
2494.sp
2495Spinlocks in BPF programs come with a number of restrictions
2496and constraints:
2497.INDENT 7.0
2498.IP \(bu 2
2499\fBbpf_spin_lock\fP objects are only allowed inside maps of
2500types \fBBPF_MAP_TYPE_HASH\fP and \fBBPF_MAP_TYPE_ARRAY\fP (this
2501list could be extended in the future).
2502.IP \(bu 2
2503BTF description of the map is mandatory.
2504.IP \(bu 2
2505The BPF program can take ONE lock at a time, since taking two
2506or more could cause dead locks.
2507.IP \(bu 2
2508Only one \fBstruct bpf_spin_lock\fP is allowed per map element.
2509.IP \(bu 2
2510When the lock is taken, calls (either BPF to BPF or helpers)
2511are not allowed.
2512.IP \(bu 2
2513The \fBBPF_LD_ABS\fP and \fBBPF_LD_IND\fP instructions are not
2514allowed inside a spinlock\-ed region.
2515.IP \(bu 2
2516The BPF program MUST call \fBbpf_spin_unlock\fP() to release
2517the lock, on all execution paths, before it returns.
2518.IP \(bu 2
2519The BPF program can access \fBstruct bpf_spin_lock\fP only via
2520the \fBbpf_spin_lock\fP() and \fBbpf_spin_unlock\fP()
2521helpers. Loading or storing data into the \fBstruct
2522bpf_spin_lock\fP \fIlock\fP\fB;\fP field of a map is not allowed.
2523.IP \(bu 2
2524To use the \fBbpf_spin_lock\fP() helper, the BTF description
2525of the map value must be a struct and have \fBstruct
2526bpf_spin_lock\fP \fIanyname\fP\fB;\fP field at the top level.
2527Nested lock inside another struct is not allowed.
2528.IP \(bu 2
2529The \fBstruct bpf_spin_lock\fP \fIlock\fP field in a map value must
2530be aligned on a multiple of 4 bytes in that value.
2531.IP \(bu 2
2532Syscall with command \fBBPF_MAP_LOOKUP_ELEM\fP does not copy
2533the \fBbpf_spin_lock\fP field to user space.
2534.IP \(bu 2
2535Syscall with command \fBBPF_MAP_UPDATE_ELEM\fP, or update from
2536a BPF program, do not update the \fBbpf_spin_lock\fP field.
2537.IP \(bu 2
2538\fBbpf_spin_lock\fP cannot be on the stack or inside a
2539networking packet (it can only be inside of a map values).
2540.IP \(bu 2
2541\fBbpf_spin_lock\fP is available to root only.
2542.IP \(bu 2
2543Tracing programs and socket filter programs cannot use
2544\fBbpf_spin_lock\fP() due to insufficient preemption checks
2545(but this may change in the future).
2546.IP \(bu 2
2547\fBbpf_spin_lock\fP is not allowed in inner maps of map\-in\-map.
2548.UNINDENT
2549.TP
2550.B Return
25510
2552.UNINDENT
2553.TP
2554.B \fBint bpf_spin_unlock(struct bpf_spin_lock *\fP\fIlock\fP\fB)\fP
2555.INDENT 7.0
2556.TP
2557.B Description
2558Release the \fIlock\fP previously locked by a call to
2559\fBbpf_spin_lock\fP(\fIlock\fP).
2560.TP
2561.B Return
25620
2563.UNINDENT
2564.TP
2565.B \fBstruct bpf_sock *bpf_sk_fullsock(struct bpf_sock *\fP\fIsk\fP\fB)\fP
2566.INDENT 7.0
2567.TP
2568.B Description
2569This helper gets a \fBstruct bpf_sock\fP pointer such
2570that all the fields in this \fBbpf_sock\fP can be accessed.
2571.TP
2572.B Return
2573A \fBstruct bpf_sock\fP pointer on success, or \fBNULL\fP in
2574case of failure.
2575.UNINDENT
2576.TP
2577.B \fBstruct bpf_tcp_sock *bpf_tcp_sock(struct bpf_sock *\fP\fIsk\fP\fB)\fP
2578.INDENT 7.0
2579.TP
2580.B Description
2581This helper gets a \fBstruct bpf_tcp_sock\fP pointer from a
2582\fBstruct bpf_sock\fP pointer.
2583.TP
2584.B Return
2585A \fBstruct bpf_tcp_sock\fP pointer on success, or \fBNULL\fP in
2586case of failure.
2587.UNINDENT
2588.TP
4e2b112c 2589.B \fBint bpf_skb_ecn_set_ce(struct sk_buff *\fP\fIskb\fP\fB)\fP
e6107b29
MK
2590.INDENT 7.0
2591.TP
2592.B Description
2593Set ECN (Explicit Congestion Notification) field of IP header
2594to \fBCE\fP (Congestion Encountered) if current value is \fBECT\fP
2595(ECN Capable Transport). Otherwise, do nothing. Works with IPv6
2596and IPv4.
2597.TP
2598.B Return
25991 if the \fBCE\fP flag is set (either by the current helper call
2600or because it was already present), 0 if it is not set.
2601.UNINDENT
2602.TP
2603.B \fBstruct bpf_sock *bpf_get_listener_sock(struct bpf_sock *\fP\fIsk\fP\fB)\fP
2604.INDENT 7.0
2605.TP
2606.B Description
2607Return a \fBstruct bpf_sock\fP pointer in \fBTCP_LISTEN\fP state.
2608\fBbpf_sk_release\fP() is unnecessary and not allowed.
2609.TP
2610.B Return
2611A \fBstruct bpf_sock\fP pointer on success, or \fBNULL\fP in
2612case of failure.
2613.UNINDENT
2614.TP
2615.B \fBstruct bpf_sock *bpf_skc_lookup_tcp(void *\fP\fIctx\fP\fB, struct bpf_sock_tuple *\fP\fItuple\fP\fB, u32\fP \fItuple_size\fP\fB, u64\fP \fInetns\fP\fB, u64\fP \fIflags\fP\fB)\fP
2616.INDENT 7.0
2617.TP
2618.B Description
2619Look for TCP socket matching \fItuple\fP, optionally in a child
2620network namespace \fInetns\fP\&. The return value must be checked,
2621and if non\-\fBNULL\fP, released via \fBbpf_sk_release\fP().
2622.sp
2623This function is identical to \fBbpf_sk_lookup_tcp\fP(), except
2624that it also returns timewait or request sockets. Use
2625\fBbpf_sk_fullsock\fP() or \fBbpf_tcp_sock\fP() to access the
2626full structure.
2627.sp
2628This helper is available only if the kernel was compiled with
2629\fBCONFIG_NET\fP configuration option.
2630.TP
2631.B Return
2632Pointer to \fBstruct bpf_sock\fP, or \fBNULL\fP in case of failure.
2633For sockets with reuseport option, the \fBstruct bpf_sock\fP
2634result is from \fIreuse\fP\fB\->socks\fP[] using the hash of the
2635tuple.
2636.UNINDENT
2637.TP
2638.B \fBint bpf_tcp_check_syncookie(struct bpf_sock *\fP\fIsk\fP\fB, void *\fP\fIiph\fP\fB, u32\fP \fIiph_len\fP\fB, struct tcphdr *\fP\fIth\fP\fB, u32\fP \fIth_len\fP\fB)\fP
2639.INDENT 7.0
2640.TP
2641.B Description
2642Check whether \fIiph\fP and \fIth\fP contain a valid SYN cookie ACK for
2643the listening socket in \fIsk\fP\&.
2644.sp
2645\fIiph\fP points to the start of the IPv4 or IPv6 header, while
2646\fIiph_len\fP contains \fBsizeof\fP(\fBstruct iphdr\fP) or
2647\fBsizeof\fP(\fBstruct ip6hdr\fP).
2648.sp
2649\fIth\fP points to the start of the TCP header, while \fIth_len\fP
2650contains \fBsizeof\fP(\fBstruct tcphdr\fP).
2651.TP
2652.B Return
26530 if \fIiph\fP and \fIth\fP are a valid SYN cookie ACK, or a negative
2654error otherwise.
2655.UNINDENT
2656.TP
2657.B \fBint bpf_sysctl_get_name(struct bpf_sysctl *\fP\fIctx\fP\fB, char *\fP\fIbuf\fP\fB, size_t\fP \fIbuf_len\fP\fB, u64\fP \fIflags\fP\fB)\fP
2658.INDENT 7.0
2659.TP
2660.B Description
2661Get name of sysctl in /proc/sys/ and copy it into provided by
2662program buffer \fIbuf\fP of size \fIbuf_len\fP\&.
2663.sp
2664The buffer is always NUL terminated, unless it\(aqs zero\-sized.
2665.sp
2666If \fIflags\fP is zero, full name (e.g. "net/ipv4/tcp_mem") is
2667copied. Use \fBBPF_F_SYSCTL_BASE_NAME\fP flag to copy base name
2668only (e.g. "tcp_mem").
2669.TP
2670.B Return
2671Number of character copied (not including the trailing NUL).
2672.sp
2673\fB\-E2BIG\fP if the buffer wasn\(aqt big enough (\fIbuf\fP will contain
2674truncated name in this case).
2675.UNINDENT
2676.TP
2677.B \fBint bpf_sysctl_get_current_value(struct bpf_sysctl *\fP\fIctx\fP\fB, char *\fP\fIbuf\fP\fB, size_t\fP \fIbuf_len\fP\fB)\fP
2678.INDENT 7.0
2679.TP
2680.B Description
2681Get current value of sysctl as it is presented in /proc/sys
2682(incl. newline, etc), and copy it as a string into provided
2683by program buffer \fIbuf\fP of size \fIbuf_len\fP\&.
2684.sp
2685The whole value is copied, no matter what file position user
2686space issued e.g. sys_read at.
2687.sp
2688The buffer is always NUL terminated, unless it\(aqs zero\-sized.
2689.TP
2690.B Return
2691Number of character copied (not including the trailing NUL).
2692.sp
2693\fB\-E2BIG\fP if the buffer wasn\(aqt big enough (\fIbuf\fP will contain
2694truncated name in this case).
2695.sp
2696\fB\-EINVAL\fP if current value was unavailable, e.g. because
2697sysctl is uninitialized and read returns \-EIO for it.
2698.UNINDENT
2699.TP
2700.B \fBint bpf_sysctl_get_new_value(struct bpf_sysctl *\fP\fIctx\fP\fB, char *\fP\fIbuf\fP\fB, size_t\fP \fIbuf_len\fP\fB)\fP
2701.INDENT 7.0
2702.TP
2703.B Description
2704Get new value being written by user space to sysctl (before
2705the actual write happens) and copy it as a string into
2706provided by program buffer \fIbuf\fP of size \fIbuf_len\fP\&.
2707.sp
2708User space may write new value at file position > 0.
2709.sp
2710The buffer is always NUL terminated, unless it\(aqs zero\-sized.
2711.TP
2712.B Return
2713Number of character copied (not including the trailing NUL).
2714.sp
2715\fB\-E2BIG\fP if the buffer wasn\(aqt big enough (\fIbuf\fP will contain
2716truncated name in this case).
2717.sp
2718\fB\-EINVAL\fP if sysctl is being read.
2719.UNINDENT
2720.TP
2721.B \fBint bpf_sysctl_set_new_value(struct bpf_sysctl *\fP\fIctx\fP\fB, const char *\fP\fIbuf\fP\fB, size_t\fP \fIbuf_len\fP\fB)\fP
2722.INDENT 7.0
2723.TP
2724.B Description
2725Override new value being written by user space to sysctl with
2726value provided by program in buffer \fIbuf\fP of size \fIbuf_len\fP\&.
2727.sp
2728\fIbuf\fP should contain a string in same form as provided by user
2729space on sysctl write.
2730.sp
2731User space may write new value at file position > 0. To override
2732the whole sysctl value file position should be set to zero.
2733.TP
2734.B Return
27350 on success.
2736.sp
2737\fB\-E2BIG\fP if the \fIbuf_len\fP is too big.
2738.sp
2739\fB\-EINVAL\fP if sysctl is being read.
2740.UNINDENT
2741.TP
2742.B \fBint bpf_strtol(const char *\fP\fIbuf\fP\fB, size_t\fP \fIbuf_len\fP\fB, u64\fP \fIflags\fP\fB, long *\fP\fIres\fP\fB)\fP
2743.INDENT 7.0
2744.TP
2745.B Description
2746Convert the initial part of the string from buffer \fIbuf\fP of
2747size \fIbuf_len\fP to a long integer according to the given base
2748and save the result in \fIres\fP\&.
2749.sp
2750The string may begin with an arbitrary amount of white space
2751(as determined by \fBisspace\fP(3)) followed by a single
2752optional \(aq\fB\-\fP\(aq sign.
2753.sp
2754Five least significant bits of \fIflags\fP encode base, other bits
2755are currently unused.
2756.sp
2757Base must be either 8, 10, 16 or 0 to detect it automatically
2758similar to user space \fBstrtol\fP(3).
2759.TP
2760.B Return
2761Number of characters consumed on success. Must be positive but
2762no more than \fIbuf_len\fP\&.
2763.sp
2764\fB\-EINVAL\fP if no valid digits were found or unsupported base
2765was provided.
2766.sp
2767\fB\-ERANGE\fP if resulting value was out of range.
2768.UNINDENT
2769.TP
2770.B \fBint bpf_strtoul(const char *\fP\fIbuf\fP\fB, size_t\fP \fIbuf_len\fP\fB, u64\fP \fIflags\fP\fB, unsigned long *\fP\fIres\fP\fB)\fP
2771.INDENT 7.0
2772.TP
2773.B Description
2774Convert the initial part of the string from buffer \fIbuf\fP of
2775size \fIbuf_len\fP to an unsigned long integer according to the
2776given base and save the result in \fIres\fP\&.
2777.sp
2778The string may begin with an arbitrary amount of white space
2779(as determined by \fBisspace\fP(3)).
2780.sp
2781Five least significant bits of \fIflags\fP encode base, other bits
2782are currently unused.
2783.sp
2784Base must be either 8, 10, 16 or 0 to detect it automatically
2785similar to user space \fBstrtoul\fP(3).
2786.TP
2787.B Return
2788Number of characters consumed on success. Must be positive but
2789no more than \fIbuf_len\fP\&.
2790.sp
2791\fB\-EINVAL\fP if no valid digits were found or unsupported base
2792was provided.
2793.sp
2794\fB\-ERANGE\fP if resulting value was out of range.
2795.UNINDENT
2796.TP
2797.B \fBvoid *bpf_sk_storage_get(struct bpf_map *\fP\fImap\fP\fB, struct bpf_sock *\fP\fIsk\fP\fB, void *\fP\fIvalue\fP\fB, u64\fP \fIflags\fP\fB)\fP
2798.INDENT 7.0
2799.TP
2800.B Description
2801Get a bpf\-local\-storage from a \fIsk\fP\&.
2802.sp
2803Logically, it could be thought of getting the value from
2804a \fImap\fP with \fIsk\fP as the \fBkey\fP\&. From this
2805perspective, the usage is not much different from
2806\fBbpf_map_lookup_elem\fP(\fImap\fP, \fB&\fP\fIsk\fP) except this
2807helper enforces the key must be a full socket and the map must
2808be a \fBBPF_MAP_TYPE_SK_STORAGE\fP also.
2809.sp
2810Underneath, the value is stored locally at \fIsk\fP instead of
2811the \fImap\fP\&. The \fImap\fP is used as the bpf\-local\-storage
2812"type". The bpf\-local\-storage "type" (i.e. the \fImap\fP) is
2813searched against all bpf\-local\-storages residing at \fIsk\fP\&.
2814.sp
2815An optional \fIflags\fP (\fBBPF_SK_STORAGE_GET_F_CREATE\fP) can be
2816used such that a new bpf\-local\-storage will be
2817created if one does not exist. \fIvalue\fP can be used
2818together with \fBBPF_SK_STORAGE_GET_F_CREATE\fP to specify
2819the initial value of a bpf\-local\-storage. If \fIvalue\fP is
2820\fBNULL\fP, the new bpf\-local\-storage will be zero initialized.
2821.TP
2822.B Return
2823A bpf\-local\-storage pointer is returned on success.
2824.sp
2825\fBNULL\fP if not found or there was an error in adding
2826a new bpf\-local\-storage.
2827.UNINDENT
2828.TP
2829.B \fBint bpf_sk_storage_delete(struct bpf_map *\fP\fImap\fP\fB, struct bpf_sock *\fP\fIsk\fP\fB)\fP
2830.INDENT 7.0
2831.TP
2832.B Description
2833Delete a bpf\-local\-storage from a \fIsk\fP\&.
2834.TP
2835.B Return
28360 on success.
2837.sp
2838\fB\-ENOENT\fP if the bpf\-local\-storage cannot be found.
2839.UNINDENT
2840.TP
2841.B \fBint bpf_send_signal(u32\fP \fIsig\fP\fB)\fP
2842.INDENT 7.0
2843.TP
2844.B Description
4e2b112c
MK
2845Send signal \fIsig\fP to the process of the current task.
2846The signal may be delivered to any of this process\(aqs threads.
e6107b29
MK
2847.TP
2848.B Return
28490 on success or successfully queued.
2850.sp
2851\fB\-EBUSY\fP if work queue under nmi is full.
2852.sp
2853\fB\-EINVAL\fP if \fIsig\fP is invalid.
2854.sp
2855\fB\-EPERM\fP if no permission to send the \fIsig\fP\&.
2856.sp
2857\fB\-EAGAIN\fP if bpf program can try again.
2858.UNINDENT
2859.TP
2860.B \fBs64 bpf_tcp_gen_syncookie(struct bpf_sock *\fP\fIsk\fP\fB, void *\fP\fIiph\fP\fB, u32\fP \fIiph_len\fP\fB, struct tcphdr *\fP\fIth\fP\fB, u32\fP \fIth_len\fP\fB)\fP
2861.INDENT 7.0
2862.TP
2863.B Description
2864Try to issue a SYN cookie for the packet with corresponding
2865IP/TCP headers, \fIiph\fP and \fIth\fP, on the listening socket in \fIsk\fP\&.
2866.sp
2867\fIiph\fP points to the start of the IPv4 or IPv6 header, while
2868\fIiph_len\fP contains \fBsizeof\fP(\fBstruct iphdr\fP) or
2869\fBsizeof\fP(\fBstruct ip6hdr\fP).
2870.sp
2871\fIth\fP points to the start of the TCP header, while \fIth_len\fP
2872contains the length of the TCP header.
2873.TP
2874.B Return
2875On success, lower 32 bits hold the generated SYN cookie in
2876followed by 16 bits which hold the MSS value for that cookie,
2877and the top 16 bits are unused.
2878.sp
2879On failure, the returned value is one of the following:
2880.sp
2881\fB\-EINVAL\fP SYN cookie cannot be issued due to error
2882.sp
2883\fB\-ENOENT\fP SYN cookie should not be issued (no SYN flood)
2884.sp
2885\fB\-EOPNOTSUPP\fP kernel configuration does not enable SYN cookies
2886.sp
2887\fB\-EPROTONOSUPPORT\fP IP packet version is not 4 or 6
2223d7df 2888.UNINDENT
4e2b112c
MK
2889.TP
2890.B \fBint bpf_skb_output(void *\fP\fIctx\fP\fB, struct bpf_map *\fP\fImap\fP\fB, u64\fP \fIflags\fP\fB, void *\fP\fIdata\fP\fB, u64\fP \fIsize\fP\fB)\fP
2891.INDENT 7.0
2892.TP
2893.B Description
2894Write raw \fIdata\fP blob into a special BPF perf event held by
2895\fImap\fP of type \fBBPF_MAP_TYPE_PERF_EVENT_ARRAY\fP\&. This perf
2896event must have the following attributes: \fBPERF_SAMPLE_RAW\fP
2897as \fBsample_type\fP, \fBPERF_TYPE_SOFTWARE\fP as \fBtype\fP, and
2898\fBPERF_COUNT_SW_BPF_OUTPUT\fP as \fBconfig\fP\&.
2899.sp
2900The \fIflags\fP are used to indicate the index in \fImap\fP for which
2901the value must be put, masked with \fBBPF_F_INDEX_MASK\fP\&.
2902Alternatively, \fIflags\fP can be set to \fBBPF_F_CURRENT_CPU\fP
2903to indicate that the index of the current CPU core should be
2904used.
2905.sp
2906The value to write, of \fIsize\fP, is passed through eBPF stack and
2907pointed by \fIdata\fP\&.
2908.sp
2909\fIctx\fP is a pointer to in\-kernel struct sk_buff.
2910.sp
2911This helper is similar to \fBbpf_perf_event_output\fP() but
2912restricted to raw_tracepoint bpf programs.
2913.TP
2914.B Return
29150 on success, or a negative error in case of failure.
2916.UNINDENT
2917.TP
2918.B \fBint bpf_probe_read_user(void *\fP\fIdst\fP\fB, u32\fP \fIsize\fP\fB, const void *\fP\fIunsafe_ptr\fP\fB)\fP
2919.INDENT 7.0
2920.TP
2921.B Description
2922Safely attempt to read \fIsize\fP bytes from user space address
2923\fIunsafe_ptr\fP and store the data in \fIdst\fP\&.
2924.TP
2925.B Return
29260 on success, or a negative error in case of failure.
2927.UNINDENT
2928.TP
2929.B \fBint bpf_probe_read_kernel(void *\fP\fIdst\fP\fB, u32\fP \fIsize\fP\fB, const void *\fP\fIunsafe_ptr\fP\fB)\fP
2930.INDENT 7.0
2931.TP
2932.B Description
2933Safely attempt to read \fIsize\fP bytes from kernel space address
2934\fIunsafe_ptr\fP and store the data in \fIdst\fP\&.
2935.TP
2936.B Return
29370 on success, or a negative error in case of failure.
2938.UNINDENT
2939.TP
2940.B \fBint bpf_probe_read_user_str(void *\fP\fIdst\fP\fB, u32\fP \fIsize\fP\fB, const void *\fP\fIunsafe_ptr\fP\fB)\fP
2941.INDENT 7.0
2942.TP
2943.B Description
2944Copy a NUL terminated string from an unsafe user address
2945\fIunsafe_ptr\fP to \fIdst\fP\&. The \fIsize\fP should include the
2946terminating NUL byte. In case the string length is smaller than
2947\fIsize\fP, the target is not padded with further NUL bytes. If the
2948string length is larger than \fIsize\fP, just \fIsize\fP\-1 bytes are
2949copied and the last byte is set to NUL.
2950.sp
2951On success, the length of the copied string is returned. This
2952makes this helper useful in tracing programs for reading
2953strings, and more importantly to get its length at runtime. See
2954the following snippet:
2955.INDENT 7.0
2956.INDENT 3.5
2957.sp
2958.nf
2959.ft C
2960SEC("kprobe/sys_open")
2961void bpf_sys_open(struct pt_regs *ctx)
2962{
2963 char buf[PATHLEN]; // PATHLEN is defined to 256
2964 int res = bpf_probe_read_user_str(buf, sizeof(buf),
2965 ctx\->di);
2966
2967 // Consume buf, for example push it to
2968 // userspace via bpf_perf_event_output(); we
2969 // can use res (the string length) as event
2970 // size, after checking its boundaries.
2971}
2972.ft P
2973.fi
2974.UNINDENT
2975.UNINDENT
2976.sp
2977In comparison, using \fBbpf_probe_read_user()\fP helper here
2978instead to read the string would require to estimate the length
2979at compile time, and would often result in copying more memory
2980than necessary.
2981.sp
2982Another useful use case is when parsing individual process
2983arguments or individual environment variables navigating
2984\fIcurrent\fP\fB\->mm\->arg_start\fP and \fIcurrent\fP\fB\->mm\->env_start\fP: using this helper and the return value,
2985one can quickly iterate at the right offset of the memory area.
2986.TP
2987.B Return
2988On success, the strictly positive length of the string,
2989including the trailing NUL character. On error, a negative
2990value.
2991.UNINDENT
2992.TP
2993.B \fBint bpf_probe_read_kernel_str(void *\fP\fIdst\fP\fB, u32\fP \fIsize\fP\fB, const void *\fP\fIunsafe_ptr\fP\fB)\fP
2994.INDENT 7.0
2995.TP
2996.B Description
2997Copy a NUL terminated string from an unsafe kernel address \fIunsafe_ptr\fP
2998to \fIdst\fP\&. Same semantics as with bpf_probe_read_user_str() apply.
2999.TP
3000.B Return
3001On success, the strictly positive length of the string, including
3002the trailing NUL character. On error, a negative value.
3003.UNINDENT
3004.TP
3005.B \fBint bpf_tcp_send_ack(void *\fP\fItp\fP\fB, u32\fP \fIrcv_nxt\fP\fB)\fP
3006.INDENT 7.0
3007.TP
3008.B Description
3009Send out a tcp\-ack. \fItp\fP is the in\-kernel struct tcp_sock.
3010\fIrcv_nxt\fP is the ack_seq to be sent out.
3011.TP
3012.B Return
30130 on success, or a negative error in case of failure.
3014.UNINDENT
3015.TP
3016.B \fBint bpf_send_signal_thread(u32\fP \fIsig\fP\fB)\fP
3017.INDENT 7.0
3018.TP
3019.B Description
3020Send signal \fIsig\fP to the thread corresponding to the current task.
3021.TP
3022.B Return
30230 on success or successfully queued.
3024.sp
3025\fB\-EBUSY\fP if work queue under nmi is full.
3026.sp
3027\fB\-EINVAL\fP if \fIsig\fP is invalid.
3028.sp
3029\fB\-EPERM\fP if no permission to send the \fIsig\fP\&.
3030.sp
3031\fB\-EAGAIN\fP if bpf program can try again.
3032.UNINDENT
3033.TP
3034.B \fBu64 bpf_jiffies64(void)\fP
3035.INDENT 7.0
3036.TP
3037.B Description
3038Obtain the 64bit jiffies
3039.TP
3040.B Return
3041The 64 bit jiffies
3042.UNINDENT
3043.TP
3044.B \fBint bpf_read_branch_records(struct bpf_perf_event_data *\fP\fIctx\fP\fB, void *\fP\fIbuf\fP\fB, u32\fP \fIsize\fP\fB, u64\fP \fIflags\fP\fB)\fP
3045.INDENT 7.0
3046.TP
3047.B Description
3048For an eBPF program attached to a perf event, retrieve the
3049branch records (struct perf_branch_entry) associated to \fIctx\fP
3050and store it in the buffer pointed by \fIbuf\fP up to size
3051\fIsize\fP bytes.
3052.TP
3053.B Return
3054On success, number of bytes written to \fIbuf\fP\&. On error, a
3055negative value.
3056.sp
3057The \fIflags\fP can be set to \fBBPF_F_GET_BRANCH_RECORDS_SIZE\fP to
3058instead return the number of bytes required to store all the
3059branch entries. If this flag is set, \fIbuf\fP may be NULL.
3060.sp
3061\fB\-EINVAL\fP if arguments invalid or \fBsize\fP not a multiple
3062of sizeof(struct perf_branch_entry).
3063.sp
3064\fB\-ENOENT\fP if architecture does not support branch records.
3065.UNINDENT
3066.TP
3067.B \fBint bpf_get_ns_current_pid_tgid(u64\fP \fIdev\fP\fB, u64\fP \fIino\fP\fB, struct bpf_pidns_info *\fP\fInsdata\fP\fB, u32\fP \fIsize\fP\fB)\fP
3068.INDENT 7.0
3069.TP
3070.B Description
3071Returns 0 on success, values for \fIpid\fP and \fItgid\fP as seen from the current
3072\fInamespace\fP will be returned in \fInsdata\fP\&.
3073.sp
3074On failure, the returned value is one of the following:
3075.sp
3076\fB\-EINVAL\fP if dev and inum supplied don\(aqt match dev_t and inode number
3077with nsfs of current task, or if dev conversion to dev_t lost high bits.
3078.sp
3079\fB\-ENOENT\fP if pidns does not exists for the current task.
3080.UNINDENT
3081.TP
3082.B \fBint bpf_xdp_output(void *\fP\fIctx\fP\fB, struct bpf_map *\fP\fImap\fP\fB, u64\fP \fIflags\fP\fB, void *\fP\fIdata\fP\fB, u64\fP \fIsize\fP\fB)\fP
3083.INDENT 7.0
3084.TP
3085.B Description
3086Write raw \fIdata\fP blob into a special BPF perf event held by
3087\fImap\fP of type \fBBPF_MAP_TYPE_PERF_EVENT_ARRAY\fP\&. This perf
3088event must have the following attributes: \fBPERF_SAMPLE_RAW\fP
3089as \fBsample_type\fP, \fBPERF_TYPE_SOFTWARE\fP as \fBtype\fP, and
3090\fBPERF_COUNT_SW_BPF_OUTPUT\fP as \fBconfig\fP\&.
3091.sp
3092The \fIflags\fP are used to indicate the index in \fImap\fP for which
3093the value must be put, masked with \fBBPF_F_INDEX_MASK\fP\&.
3094Alternatively, \fIflags\fP can be set to \fBBPF_F_CURRENT_CPU\fP
3095to indicate that the index of the current CPU core should be
3096used.
3097.sp
3098The value to write, of \fIsize\fP, is passed through eBPF stack and
3099pointed by \fIdata\fP\&.
3100.sp
3101\fIctx\fP is a pointer to in\-kernel struct xdp_buff.
3102.sp
3103This helper is similar to \fBbpf_perf_eventoutput\fP() but
3104restricted to raw_tracepoint bpf programs.
3105.TP
3106.B Return
31070 on success, or a negative error in case of failure.
3108.UNINDENT
3109.TP
3110.B \fBu64 bpf_get_netns_cookie(void *\fP\fIctx\fP\fB)\fP
3111.INDENT 7.0
3112.TP
3113.B Description
3114Retrieve the cookie (generated by the kernel) of the network
3115namespace the input \fIctx\fP is associated with. The network
3116namespace cookie remains stable for its lifetime and provides
3117a global identifier that can be assumed unique. If \fIctx\fP is
3118NULL, then the helper returns the cookie for the initial
3119network namespace. The cookie itself is very similar to that
3120of bpf_get_socket_cookie() helper, but for network namespaces
3121instead of sockets.
3122.TP
3123.B Return
3124A 8\-byte long opaque number.
3125.UNINDENT
3126.TP
3127.B \fBu64 bpf_get_current_ancestor_cgroup_id(int\fP \fIancestor_level\fP\fB)\fP
3128.INDENT 7.0
3129.TP
3130.B Description
3131Return id of cgroup v2 that is ancestor of the cgroup associated
3132with the current task at the \fIancestor_level\fP\&. The root cgroup
3133is at \fIancestor_level\fP zero and each step down the hierarchy
3134increments the level. If \fIancestor_level\fP == level of cgroup
3135associated with the current task, then return value will be the
3136same as that of \fBbpf_get_current_cgroup_id\fP().
3137.sp
3138The helper is useful to implement policies based on cgroups
3139that are upper in hierarchy than immediate cgroup associated
3140with the current task.
3141.sp
3142The format of returned id and helper limitations are same as in
3143\fBbpf_get_current_cgroup_id\fP().
3144.TP
3145.B Return
3146The id is returned or 0 in case the id could not be retrieved.
3147.UNINDENT
3148.TP
3149.B \fBint bpf_sk_assign(struct sk_buff *\fP\fIskb\fP\fB, struct bpf_sock *\fP\fIsk\fP\fB, u64\fP \fIflags\fP\fB)\fP
3150.INDENT 7.0
3151.TP
3152.B Description
3153Assign the \fIsk\fP to the \fIskb\fP\&. When combined with appropriate
3154routing configuration to receive the packet towards the socket,
3155will cause \fIskb\fP to be delivered to the specified socket.
3156Subsequent redirection of \fIskb\fP via \fBbpf_redirect\fP(),
3157\fBbpf_clone_redirect\fP() or other methods outside of BPF may
3158interfere with successful delivery to the socket.
3159.sp
3160This operation is only valid from TC ingress path.
3161.sp
3162The \fIflags\fP argument must be zero.
3163.TP
3164.B Return
31650 on success, or a negative errno in case of failure.
3166.INDENT 7.0
3167.IP \(bu 2
3168\fB\-EINVAL\fP Unsupported flags specified.
3169.IP \(bu 2
3170\fB\-ENOENT\fP Socket is unavailable for assignment.
3171.IP \(bu 2
3172\fB\-ENETUNREACH\fP Socket is unreachable (wrong netns).
3173.IP \(bu 2
3174.INDENT 2.0
3175.TP
3176.B \fB\-EOPNOTSUPP\fP Unsupported operation, for example a
3177call from outside of TC ingress.
3178.UNINDENT
3179.IP \(bu 2
3180\fB\-ESOCKTNOSUPPORT\fP Socket type not supported (reuseport).
3181.UNINDENT
3182.UNINDENT
53666f6c
MK
3183.UNINDENT
3184.SH EXAMPLES
3185.sp
3186Example usage for most of the eBPF helpers listed in this manual page are
3187available within the Linux kernel sources, at the following locations:
3188.INDENT 0.0
3189.IP \(bu 2
3190\fIsamples/bpf/\fP
3191.IP \(bu 2
3192\fItools/testing/selftests/bpf/\fP
3193.UNINDENT
3194.SH LICENSE
3195.sp
3196eBPF programs can have an associated license, passed along with the bytecode
3197instructions to the kernel when the programs are loaded. The format for that
3198string is identical to the one in use for kernel modules (Dual licenses, such
3199as "Dual BSD/GPL", may be used). Some helper functions are only accessible to
3200programs that are compatible with the GNU Privacy License (GPL).
3201.sp
3202In order to use such helpers, the eBPF program must be loaded with the correct
3203license string passed (via \fBattr\fP) to the \fBbpf\fP() system call, and this
3204generally translates into the C source code of the program containing a line
3205similar to the following:
3206.INDENT 0.0
3207.INDENT 3.5
3208.sp
3209.nf
3210.ft C
3211char ____license[] __attribute__((section("license"), used)) = "GPL";
3212.ft P
3213.fi
3214.UNINDENT
3215.UNINDENT
3216.SH IMPLEMENTATION
3217.sp
3218This manual page is an effort to document the existing eBPF helper functions.
3219But as of this writing, the BPF sub\-system is under heavy development. New eBPF
3220program or map types are added, along with new helper functions. Some helpers
3221are occasionally made available for additional program types. So in spite of
3222the efforts of the community, this page might not be up\-to\-date. If you want to
3223check by yourself what helper functions exist in your kernel, or what types of
3224programs they can support, here are some files among the kernel tree that you
3225may be interested in:
3226.INDENT 0.0
3227.IP \(bu 2
3228\fIinclude/uapi/linux/bpf.h\fP is the main BPF header. It contains the full list
3229of all helper functions, as well as many other BPF definitions including most
3230of the flags, structs or constants used by the helpers.
3231.IP \(bu 2
3232\fInet/core/filter.c\fP contains the definition of most network\-related helper
3233functions, and the list of program types from which they can be used.
3234.IP \(bu 2
3235\fIkernel/trace/bpf_trace.c\fP is the equivalent for most tracing program\-related
3236helpers.
3237.IP \(bu 2
3238\fIkernel/bpf/verifier.c\fP contains the functions used to check that valid types
3239of eBPF maps are used with a given helper function.
3240.IP \(bu 2
3241\fIkernel/bpf/\fP directory contains other files in which additional helpers are
3242defined (for cgroups, sockmaps, etc.).
3243.UNINDENT
3244.sp
3245Compatibility between helper functions and program types can generally be found
3246in the files where helper functions are defined. Look for the \fBstruct
3247bpf_func_proto\fP objects and for functions returning them: these functions
3248contain a list of helpers that a given program type can call. Note that the
3249\fBdefault:\fP label of the \fBswitch ... case\fP used to filter helpers can call
3250other functions, themselves allowing access to additional helpers. The
3251requirement for GPL license is also in those \fBstruct bpf_func_proto\fP\&.
3252.sp
3253Compatibility between helper functions and map types can be found in the
3254\fBcheck_map_func_compatibility\fP() function in file \fIkernel/bpf/verifier.c\fP\&.
3255.sp
3256Helper functions that invalidate the checks on \fBdata\fP and \fBdata_end\fP
3257pointers for network processing are listed in function
3258\fBbpf_helper_changes_pkt_data\fP() in file \fInet/core/filter.c\fP\&.
3259.SH SEE ALSO
3260.sp
3261\fBbpf\fP(2),
3262\fBcgroups\fP(7),
3263\fBip\fP(8),
3264\fBperf_event_open\fP(2),
3265\fBsendmsg\fP(2),
3266\fBsocket\fP(7),
3267\fBtc\-bpf\fP(8)
3268.\" Generated by docutils manpage writer.
e6107b29 3269.