2 # SPDX-License-Identifier: GPL-2.0-only
4 # Copyright (C) 2018-2019 Netronome Systems, Inc.
5 # Copyright (C) 2021 Isovalent, Inc.
7 # In case user attempts to run with Python 2.
8 from __future__ import print_function
14 class NoHelperFound(BaseException):
17 class NoSyscallCommandFound(BaseException):
20 class ParsingError(BaseException):
21 def __init__(self, line='<line not provided>', reader=None):
23 BaseException.__init__(self,
24 'Error at file offset %d, parsing line: %s' %
25 (reader.tell(), line))
27 BaseException.__init__(self, 'Error parsing line: %s' % line)
30 class APIElement(object):
32 An object representing the description of an aspect of the eBPF API.
33 @proto: prototype of the API symbol
34 @desc: textual description of the symbol
35 @ret: (optional) description of any associated return value
37 def __init__(self, proto='', desc='', ret=''):
43 class Helper(APIElement):
45 An object representing the description of an eBPF helper function.
46 @proto: function prototype of the helper function
47 @desc: textual description of the helper function
48 @ret: description of the return value of the helper function
50 def proto_break_down(self):
52 Break down helper function protocol into smaller chunks: return type,
53 name, distincts arguments.
55 arg_re = re.compile('((\w+ )*?(\w+|...))( (\**)(\w+))?$')
57 proto_re = re.compile('(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$')
59 capture = proto_re.match(self.proto)
60 res['ret_type'] = capture.group(1)
61 res['ret_star'] = capture.group(2)
62 res['name'] = capture.group(3)
65 args = capture.group(4).split(', ')
67 capture = arg_re.match(a)
69 'type' : capture.group(1),
70 'star' : capture.group(5),
71 'name' : capture.group(6)
77 class HeaderParser(object):
79 An object used to parse a file in order to extract the documentation of a
80 list of eBPF helper functions. All the helpers that can be retrieved are
81 stored as Helper object, in the self.helpers() array.
82 @filename: name of file to parse, usually include/uapi/linux/bpf.h in the
85 def __init__(self, filename):
86 self.reader = open(filename, 'r')
90 self.desc_unique_helpers = set()
91 self.define_unique_helpers = []
92 self.desc_syscalls = []
93 self.enum_syscalls = []
95 def parse_element(self):
96 proto = self.parse_symbol()
97 desc = self.parse_desc(proto)
98 ret = self.parse_ret(proto)
99 return APIElement(proto=proto, desc=desc, ret=ret)
101 def parse_helper(self):
102 proto = self.parse_proto()
103 desc = self.parse_desc(proto)
104 ret = self.parse_ret(proto)
105 return Helper(proto=proto, desc=desc, ret=ret)
107 def parse_symbol(self):
108 p = re.compile(' \* ?(BPF\w+)$')
109 capture = p.match(self.line)
111 raise NoSyscallCommandFound
112 end_re = re.compile(' \* ?NOTES$')
113 end = end_re.match(self.line)
115 raise NoSyscallCommandFound
116 self.line = self.reader.readline()
117 return capture.group(1)
119 def parse_proto(self):
120 # Argument can be of shape:
124 # - Same as above, with "const" and/or "struct" in front of type
125 # - "..." (undefined number of arguments, for bpf_trace_printk())
126 # There is at least one term ("void"), and at most five arguments.
127 p = re.compile(' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$')
128 capture = p.match(self.line)
131 self.line = self.reader.readline()
132 return capture.group(1)
134 def parse_desc(self, proto):
135 p = re.compile(' \* ?(?:\t| {5,8})Description$')
136 capture = p.match(self.line)
138 raise Exception("No description section found for " + proto)
139 # Description can be several lines, some of them possibly empty, and it
140 # stops when another subsection title is met.
144 self.line = self.reader.readline()
145 if self.line == ' *\n':
148 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
149 capture = p.match(self.line)
152 desc += capture.group(1) + '\n'
157 raise Exception("No description found for " + proto)
160 def parse_ret(self, proto):
161 p = re.compile(' \* ?(?:\t| {5,8})Return$')
162 capture = p.match(self.line)
164 raise Exception("No return section found for " + proto)
165 # Return value description can be several lines, some of them possibly
166 # empty, and it stops when another subsection title is met.
170 self.line = self.reader.readline()
171 if self.line == ' *\n':
174 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
175 capture = p.match(self.line)
178 ret += capture.group(1) + '\n'
183 raise Exception("No return found for " + proto)
186 def seek_to(self, target, help_message, discard_lines = 1):
188 offset = self.reader.read().find(target)
190 raise Exception(help_message)
191 self.reader.seek(offset)
192 self.reader.readline()
193 for _ in range(discard_lines):
194 self.reader.readline()
195 self.line = self.reader.readline()
197 def parse_desc_syscall(self):
198 self.seek_to('* DOC: eBPF Syscall Commands',
199 'Could not find start of eBPF syscall descriptions list')
202 command = self.parse_element()
203 self.commands.append(command)
204 self.desc_syscalls.append(command.proto)
206 except NoSyscallCommandFound:
209 def parse_enum_syscall(self):
210 self.seek_to('enum bpf_cmd {',
211 'Could not find start of bpf_cmd enum', 0)
212 # Searches for either one or more BPF\w+ enums
213 bpf_p = re.compile('\s*(BPF\w+)+')
214 # Searches for an enum entry assigned to another entry,
215 # for e.g. BPF_PROG_RUN = BPF_PROG_TEST_RUN, which is
216 # not documented hence should be skipped in check to
217 # determine if the right number of syscalls are documented
218 assign_p = re.compile('\s*(BPF\w+)\s*=\s*(BPF\w+)')
221 capture = assign_p.match(self.line)
223 # Skip line if an enum entry is assigned to another entry
224 self.line = self.reader.readline()
226 capture = bpf_p.match(self.line)
228 bpf_cmd_str += self.line
231 self.line = self.reader.readline()
232 # Find the number of occurences of BPF\w+
233 self.enum_syscalls = re.findall('(BPF\w+)+', bpf_cmd_str)
235 def parse_desc_helpers(self):
236 self.seek_to('* Start of BPF helper function descriptions:',
237 'Could not find start of eBPF helper descriptions list')
240 helper = self.parse_helper()
241 self.helpers.append(helper)
242 proto = helper.proto_break_down()
243 self.desc_unique_helpers.add(proto['name'])
244 except NoHelperFound:
247 def parse_define_helpers(self):
248 # Parse the number of FN(...) in #define __BPF_FUNC_MAPPER to compare
249 # later with the number of unique function names present in description.
250 # Note: seek_to(..) discards the first line below the target search text,
251 # resulting in FN(unspec) being skipped and not added to self.define_unique_helpers.
252 self.seek_to('#define __BPF_FUNC_MAPPER(FN)',
253 'Could not find start of eBPF helper definition list')
254 # Searches for either one or more FN(\w+) defines or a backslash for newline
255 p = re.compile('\s*(FN\(\w+\))+|\\\\')
258 capture = p.match(self.line)
260 fn_defines_str += self.line
263 self.line = self.reader.readline()
264 # Find the number of occurences of FN(\w+)
265 self.define_unique_helpers = re.findall('FN\(\w+\)', fn_defines_str)
268 self.parse_desc_syscall()
269 self.parse_enum_syscall()
270 self.parse_desc_helpers()
271 self.parse_define_helpers()
274 ###############################################################################
276 class Printer(object):
278 A generic class for printers. Printers should be created with an array of
279 Helper objects, and implement a way to print them in the desired fashion.
280 @parser: A HeaderParser with objects to print to standard output
282 def __init__(self, parser):
286 def print_header(self):
289 def print_footer(self):
292 def print_one(self, helper):
297 for elem in self.elements:
301 def elem_number_check(self, desc_unique_elem, define_unique_elem, type, instance):
303 Checks the number of helpers/syscalls documented within the header file
304 description with those defined as part of enum/macro and raise an
305 Exception if they don't match.
307 nr_desc_unique_elem = len(desc_unique_elem)
308 nr_define_unique_elem = len(define_unique_elem)
309 if nr_desc_unique_elem != nr_define_unique_elem:
311 The number of unique %s in description (%d) doesn\'t match the number of unique %s defined in %s (%d)
312 ''' % (type, nr_desc_unique_elem, type, instance, nr_define_unique_elem)
313 if nr_desc_unique_elem < nr_define_unique_elem:
314 # Function description is parsed until no helper is found (which can be due to
315 # misformatting). Hence, only print the first missing/misformatted helper/enum.
317 The description for %s is not present or formatted correctly.
318 ''' % (define_unique_elem[nr_desc_unique_elem])
319 raise Exception(exception_msg)
321 class PrinterRST(Printer):
323 A generic class for printers that print ReStructured Text. Printers should
324 be created with a HeaderParser object, and implement a way to print API
325 elements in the desired fashion.
326 @parser: A HeaderParser with objects to print to standard output
328 def __init__(self, parser):
331 def print_license(self):
333 .. Copyright (C) All BPF authors and contributors from 2014 to present.
334 .. See git log include/uapi/linux/bpf.h in kernel tree for details.
336 .. SPDX-License-Identifier: Linux-man-pages-copyleft
338 .. Please do not edit this file. It was generated from the documentation
339 .. located in file include/uapi/linux/bpf.h of the Linux kernel sources
340 .. (helpers description), and from scripts/bpf_doc.py in the same
341 .. repository (header and footer).
345 def print_elem(self, elem):
347 print('\tDescription')
348 # Do not strip all newline characters: formatted code at the end of
349 # a section must be followed by a blank line.
350 for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):
351 print('{}{}'.format('\t\t' if line else '', line))
355 for line in elem.ret.rstrip().split('\n'):
356 print('{}{}'.format('\t\t' if line else '', line))
360 class PrinterHelpersRST(PrinterRST):
362 A printer for dumping collected information about helpers as a ReStructured
363 Text page compatible with the rst2man program, which can be used to
364 generate a manual page for the helpers.
365 @parser: A HeaderParser with Helper objects to print to standard output
367 def __init__(self, parser):
368 self.elements = parser.helpers
369 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
371 def print_header(self):
376 -------------------------------------------------------------------------------
377 list of eBPF helper functions
378 -------------------------------------------------------------------------------
385 The extended Berkeley Packet Filter (eBPF) subsystem consists in programs
386 written in a pseudo-assembly language, then attached to one of the several
387 kernel hooks and run in reaction of specific events. This framework differs
388 from the older, "classic" BPF (or "cBPF") in several aspects, one of them being
389 the ability to call special functions (or "helpers") from within a program.
390 These functions are restricted to a white-list of helpers defined in the
393 These helpers are used by eBPF programs to interact with the system, or with
394 the context in which they work. For instance, they can be used to print
395 debugging messages, to get the time since the system was booted, to interact
396 with eBPF maps, or to manipulate network packets. Since there are several eBPF
397 program types, and that they do not run in the same context, each program type
398 can only call a subset of those helpers.
400 Due to eBPF conventions, a helper can not have more than five arguments.
402 Internally, eBPF programs call directly into the compiled helper functions
403 without requiring any foreign-function interface. As a result, calling helpers
404 introduces no overhead, thus offering excellent performance.
406 This document is an attempt to list and document the helpers available to eBPF
407 developers. They are sorted by chronological order (the oldest helpers in the
413 PrinterRST.print_license(self)
416 def print_footer(self):
421 Example usage for most of the eBPF helpers listed in this manual page are
422 available within the Linux kernel sources, at the following locations:
425 * *tools/testing/selftests/bpf/*
430 eBPF programs can have an associated license, passed along with the bytecode
431 instructions to the kernel when the programs are loaded. The format for that
432 string is identical to the one in use for kernel modules (Dual licenses, such
433 as "Dual BSD/GPL", may be used). Some helper functions are only accessible to
434 programs that are compatible with the GNU Privacy License (GPL).
436 In order to use such helpers, the eBPF program must be loaded with the correct
437 license string passed (via **attr**) to the **bpf**\ () system call, and this
438 generally translates into the C source code of the program containing a line
439 similar to the following:
443 char ____license[] __attribute__((section("license"), used)) = "GPL";
448 This manual page is an effort to document the existing eBPF helper functions.
449 But as of this writing, the BPF sub-system is under heavy development. New eBPF
450 program or map types are added, along with new helper functions. Some helpers
451 are occasionally made available for additional program types. So in spite of
452 the efforts of the community, this page might not be up-to-date. If you want to
453 check by yourself what helper functions exist in your kernel, or what types of
454 programs they can support, here are some files among the kernel tree that you
455 may be interested in:
457 * *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list
458 of all helper functions, as well as many other BPF definitions including most
459 of the flags, structs or constants used by the helpers.
460 * *net/core/filter.c* contains the definition of most network-related helper
461 functions, and the list of program types from which they can be used.
462 * *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related
464 * *kernel/bpf/verifier.c* contains the functions used to check that valid types
465 of eBPF maps are used with a given helper function.
466 * *kernel/bpf/* directory contains other files in which additional helpers are
467 defined (for cgroups, sockmaps, etc.).
468 * The bpftool utility can be used to probe the availability of helper functions
469 on the system (as well as supported program and map types, and a number of
470 other parameters). To do so, run **bpftool feature probe** (see
471 **bpftool-feature**\ (8) for details). Add the **unprivileged** keyword to
472 list features available to unprivileged users.
474 Compatibility between helper functions and program types can generally be found
475 in the files where helper functions are defined. Look for the **struct
476 bpf_func_proto** objects and for functions returning them: these functions
477 contain a list of helpers that a given program type can call. Note that the
478 **default:** label of the **switch ... case** used to filter helpers can call
479 other functions, themselves allowing access to additional helpers. The
480 requirement for GPL license is also in those **struct bpf_func_proto**.
482 Compatibility between helper functions and map types can be found in the
483 **check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*.
485 Helper functions that invalidate the checks on **data** and **data_end**
486 pointers for network processing are listed in function
487 **bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*.
496 **perf_event_open**\ (2),
502 def print_proto(self, helper):
504 Format function protocol with bold and italics markers. This makes RST
505 file less readable, but gives nice results in the manual page.
507 proto = helper.proto_break_down()
509 print('**%s %s%s(' % (proto['ret_type'],
510 proto['ret_star'].replace('*', '\\*'),
515 for a in proto['args']:
516 one_arg = '{}{}'.format(comma, a['type'])
519 one_arg += ' {}**\ '.format(a['star'].replace('*', '\\*'))
522 one_arg += '*{}*\\ **'.format(a['name'])
524 print(one_arg, end='')
528 def print_one(self, helper):
529 self.print_proto(helper)
530 self.print_elem(helper)
533 class PrinterSyscallRST(PrinterRST):
535 A printer for dumping collected information about the syscall API as a
536 ReStructured Text page compatible with the rst2man program, which can be
537 used to generate a manual page for the syscall.
538 @parser: A HeaderParser with APIElement objects to print to standard
541 def __init__(self, parser):
542 self.elements = parser.commands
543 self.elem_number_check(parser.desc_syscalls, parser.enum_syscalls, 'syscall', 'bpf_cmd')
545 def print_header(self):
550 -------------------------------------------------------------------------------
551 Perform a command on an extended BPF object
552 -------------------------------------------------------------------------------
559 PrinterRST.print_license(self)
562 def print_one(self, command):
563 print('**%s**' % (command.proto))
564 self.print_elem(command)
567 class PrinterHelpers(Printer):
569 A printer for dumping collected information about helpers as C header to
570 be included from BPF program.
571 @parser: A HeaderParser with Helper objects to print to standard output
573 def __init__(self, parser):
574 self.elements = parser.helpers
575 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
578 'struct bpf_fib_lookup',
579 'struct bpf_sk_lookup',
580 'struct bpf_perf_event_data',
581 'struct bpf_perf_event_value',
582 'struct bpf_pidns_info',
583 'struct bpf_redir_neigh',
585 'struct bpf_sock_addr',
586 'struct bpf_sock_ops',
587 'struct bpf_sock_tuple',
588 'struct bpf_spin_lock',
590 'struct bpf_tcp_sock',
591 'struct bpf_tunnel_key',
592 'struct bpf_xfrm_state',
593 'struct linux_binprm',
595 'struct sk_reuseport_md',
601 'struct tcp_timewait_sock',
602 'struct tcp_request_sock',
605 'struct task_struct',
635 'struct bpf_fib_lookup',
636 'struct bpf_perf_event_data',
637 'struct bpf_perf_event_value',
638 'struct bpf_pidns_info',
639 'struct bpf_redir_neigh',
640 'struct bpf_sk_lookup',
642 'struct bpf_sock_addr',
643 'struct bpf_sock_ops',
644 'struct bpf_sock_tuple',
645 'struct bpf_spin_lock',
647 'struct bpf_tcp_sock',
648 'struct bpf_tunnel_key',
649 'struct bpf_xfrm_state',
650 'struct linux_binprm',
652 'struct sk_reuseport_md',
658 'struct tcp_timewait_sock',
659 'struct tcp_request_sock',
662 'struct task_struct',
683 'size_t': 'unsigned long',
684 'struct bpf_map': 'void',
685 'struct sk_buff': 'struct __sk_buff',
686 'const struct sk_buff': 'const struct __sk_buff',
687 'struct sk_msg_buff': 'struct sk_msg_md',
688 'struct xdp_buff': 'struct xdp_md',
690 # Helpers overloaded for different context types.
691 overloaded_helpers = [
692 'bpf_get_socket_cookie',
696 def print_header(self):
698 /* This is auto-generated file. See bpf_doc.py for details. */
700 /* Forward declarations of BPF structs */'''
703 for fwd in self.type_fwds:
707 def print_footer(self):
711 def map_type(self, t):
712 if t in self.known_types:
714 if t in self.mapped_types:
715 return self.mapped_types[t]
716 print("Unrecognized type '%s', please add it to known types!" % t,
722 def print_one(self, helper):
723 proto = helper.proto_break_down()
725 if proto['name'] in self.seen_helpers:
727 self.seen_helpers.add(proto['name'])
730 print(" * %s" % proto['name'])
733 # Do not strip all newline characters: formatted code at the end of
734 # a section must be followed by a blank line.
735 for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
736 print(' *{}{}'.format(' \t' if line else '', line))
741 for line in helper.ret.rstrip().split('\n'):
742 print(' *{}{}'.format(' \t' if line else '', line))
745 print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
746 proto['ret_star'], proto['name']), end='')
748 for i, a in enumerate(proto['args']):
751 if proto['name'] in self.overloaded_helpers and i == 0:
754 one_arg = '{}{}'.format(comma, self.map_type(t))
757 one_arg += ' {}'.format(a['star'])
760 one_arg += '{}'.format(n)
762 print(one_arg, end='')
764 print(') = (void *) %d;' % len(self.seen_helpers))
767 ###############################################################################
769 # If script is launched from scripts/ from kernel tree and can access
770 # ../include/uapi/linux/bpf.h, use it as a default name for the file to parse,
771 # otherwise the --filename argument will be required from the command line.
772 script = os.path.abspath(sys.argv[0])
773 linuxRoot = os.path.dirname(os.path.dirname(script))
774 bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
777 'helpers': PrinterHelpersRST,
778 'syscall': PrinterSyscallRST,
781 argParser = argparse.ArgumentParser(description="""
782 Parse eBPF header file and generate documentation for the eBPF API.
783 The RST-formatted output produced can be turned into a manual page with the
786 argParser.add_argument('--header', action='store_true',
787 help='generate C header file')
788 if (os.path.isfile(bpfh)):
789 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h',
792 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
793 argParser.add_argument('target', nargs='?', default='helpers',
794 choices=printers.keys(), help='eBPF API target')
795 args = argParser.parse_args()
798 headerParser = HeaderParser(args.filename)
801 # Print formatted output to standard output.
803 if args.target != 'helpers':
804 raise NotImplementedError('Only helpers header generation is supported')
805 printer = PrinterHelpers(headerParser)
807 printer = printers[args.target](headerParser)