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
15 helpersDocStart = 'Start of BPF helper function descriptions:'
17 class NoHelperFound(BaseException):
20 class NoSyscallCommandFound(BaseException):
23 class ParsingError(BaseException):
24 def __init__(self, line='<line not provided>', reader=None):
26 BaseException.__init__(self,
27 'Error at file offset %d, parsing line: %s' %
28 (reader.tell(), line))
30 BaseException.__init__(self, 'Error parsing line: %s' % line)
33 class APIElement(object):
35 An object representing the description of an aspect of the eBPF API.
36 @proto: prototype of the API symbol
37 @desc: textual description of the symbol
38 @ret: (optional) description of any associated return value
40 def __init__(self, proto='', desc='', ret=''):
46 class Helper(APIElement):
48 An object representing the description of an eBPF helper function.
49 @proto: function prototype of the helper function
50 @desc: textual description of the helper function
51 @ret: description of the return value of the helper function
53 def __init__(self, *args, **kwargs):
54 super().__init__(*args, **kwargs)
57 def proto_break_down(self):
59 Break down helper function protocol into smaller chunks: return type,
60 name, distincts arguments.
62 arg_re = re.compile('((\w+ )*?(\w+|...))( (\**)(\w+))?$')
64 proto_re = re.compile('(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$')
66 capture = proto_re.match(self.proto)
67 res['ret_type'] = capture.group(1)
68 res['ret_star'] = capture.group(2)
69 res['name'] = capture.group(3)
72 args = capture.group(4).split(', ')
74 capture = arg_re.match(a)
76 'type' : capture.group(1),
77 'star' : capture.group(5),
78 'name' : capture.group(6)
84 class HeaderParser(object):
86 An object used to parse a file in order to extract the documentation of a
87 list of eBPF helper functions. All the helpers that can be retrieved are
88 stored as Helper object, in the self.helpers() array.
89 @filename: name of file to parse, usually include/uapi/linux/bpf.h in the
92 def __init__(self, filename):
93 self.reader = open(filename, 'r')
97 self.desc_unique_helpers = set()
98 self.define_unique_helpers = []
99 self.helper_enum_vals = {}
100 self.desc_syscalls = []
101 self.enum_syscalls = []
103 def parse_element(self):
104 proto = self.parse_symbol()
105 desc = self.parse_desc(proto)
106 ret = self.parse_ret(proto)
107 return APIElement(proto=proto, desc=desc, ret=ret)
109 def parse_helper(self):
110 proto = self.parse_proto()
111 desc = self.parse_desc(proto)
112 ret = self.parse_ret(proto)
113 return Helper(proto=proto, desc=desc, ret=ret)
115 def parse_symbol(self):
116 p = re.compile(' \* ?(BPF\w+)$')
117 capture = p.match(self.line)
119 raise NoSyscallCommandFound
120 end_re = re.compile(' \* ?NOTES$')
121 end = end_re.match(self.line)
123 raise NoSyscallCommandFound
124 self.line = self.reader.readline()
125 return capture.group(1)
127 def parse_proto(self):
128 # Argument can be of shape:
132 # - Same as above, with "const" and/or "struct" in front of type
133 # - "..." (undefined number of arguments, for bpf_trace_printk())
134 # There is at least one term ("void"), and at most five arguments.
135 p = re.compile(' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$')
136 capture = p.match(self.line)
139 self.line = self.reader.readline()
140 return capture.group(1)
142 def parse_desc(self, proto):
143 p = re.compile(' \* ?(?:\t| {5,8})Description$')
144 capture = p.match(self.line)
146 raise Exception("No description section found for " + proto)
147 # Description can be several lines, some of them possibly empty, and it
148 # stops when another subsection title is met.
152 self.line = self.reader.readline()
153 if self.line == ' *\n':
156 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
157 capture = p.match(self.line)
160 desc += capture.group(1) + '\n'
165 raise Exception("No description found for " + proto)
168 def parse_ret(self, proto):
169 p = re.compile(' \* ?(?:\t| {5,8})Return$')
170 capture = p.match(self.line)
172 raise Exception("No return section found for " + proto)
173 # Return value description can be several lines, some of them possibly
174 # empty, and it stops when another subsection title is met.
178 self.line = self.reader.readline()
179 if self.line == ' *\n':
182 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
183 capture = p.match(self.line)
186 ret += capture.group(1) + '\n'
191 raise Exception("No return found for " + proto)
194 def seek_to(self, target, help_message, discard_lines = 1):
196 offset = self.reader.read().find(target)
198 raise Exception(help_message)
199 self.reader.seek(offset)
200 self.reader.readline()
201 for _ in range(discard_lines):
202 self.reader.readline()
203 self.line = self.reader.readline()
205 def parse_desc_syscall(self):
206 self.seek_to('* DOC: eBPF Syscall Commands',
207 'Could not find start of eBPF syscall descriptions list')
210 command = self.parse_element()
211 self.commands.append(command)
212 self.desc_syscalls.append(command.proto)
214 except NoSyscallCommandFound:
217 def parse_enum_syscall(self):
218 self.seek_to('enum bpf_cmd {',
219 'Could not find start of bpf_cmd enum', 0)
220 # Searches for either one or more BPF\w+ enums
221 bpf_p = re.compile('\s*(BPF\w+)+')
222 # Searches for an enum entry assigned to another entry,
223 # for e.g. BPF_PROG_RUN = BPF_PROG_TEST_RUN, which is
224 # not documented hence should be skipped in check to
225 # determine if the right number of syscalls are documented
226 assign_p = re.compile('\s*(BPF\w+)\s*=\s*(BPF\w+)')
229 capture = assign_p.match(self.line)
231 # Skip line if an enum entry is assigned to another entry
232 self.line = self.reader.readline()
234 capture = bpf_p.match(self.line)
236 bpf_cmd_str += self.line
239 self.line = self.reader.readline()
240 # Find the number of occurences of BPF\w+
241 self.enum_syscalls = re.findall('(BPF\w+)+', bpf_cmd_str)
243 def parse_desc_helpers(self):
244 self.seek_to(helpersDocStart,
245 'Could not find start of eBPF helper descriptions list')
248 helper = self.parse_helper()
249 self.helpers.append(helper)
250 proto = helper.proto_break_down()
251 self.desc_unique_helpers.add(proto['name'])
252 except NoHelperFound:
255 def parse_define_helpers(self):
256 # Parse FN(...) in #define __BPF_FUNC_MAPPER to compare later with the
257 # number of unique function names present in description and use the
258 # correct enumeration value.
259 # Note: seek_to(..) discards the first line below the target search text,
260 # resulting in FN(unspec) being skipped and not added to self.define_unique_helpers.
261 self.seek_to('#define __BPF_FUNC_MAPPER(FN)',
262 'Could not find start of eBPF helper definition list')
263 # Searches for one FN(\w+) define or a backslash for newline
264 p = re.compile('\s*FN\((\w+)\)|\\\\')
266 i = 1 # 'unspec' is skipped as mentioned above
268 capture = p.match(self.line)
270 fn_defines_str += self.line
271 self.helper_enum_vals[capture.expand(r'bpf_\1')] = i
275 self.line = self.reader.readline()
276 # Find the number of occurences of FN(\w+)
277 self.define_unique_helpers = re.findall('FN\(\w+\)', fn_defines_str)
279 def assign_helper_values(self):
281 for helper in self.helpers:
282 proto = helper.proto_break_down()
285 enum_val = self.helper_enum_vals[name]
287 raise Exception("Helper %s is missing from enum bpf_func_id" % name)
289 # Enforce current practice of having the descriptions ordered
291 seen_helpers.add(name)
292 desc_val = len(seen_helpers)
293 if desc_val != enum_val:
294 raise Exception("Helper %s comment order (#%d) must be aligned with its position (#%d) in enum bpf_func_id" % (name, desc_val, enum_val))
296 helper.enum_val = enum_val
299 self.parse_desc_syscall()
300 self.parse_enum_syscall()
301 self.parse_desc_helpers()
302 self.parse_define_helpers()
303 self.assign_helper_values()
306 ###############################################################################
308 class Printer(object):
310 A generic class for printers. Printers should be created with an array of
311 Helper objects, and implement a way to print them in the desired fashion.
312 @parser: A HeaderParser with objects to print to standard output
314 def __init__(self, parser):
318 def print_header(self):
321 def print_footer(self):
324 def print_one(self, helper):
329 for elem in self.elements:
333 def elem_number_check(self, desc_unique_elem, define_unique_elem, type, instance):
335 Checks the number of helpers/syscalls documented within the header file
336 description with those defined as part of enum/macro and raise an
337 Exception if they don't match.
339 nr_desc_unique_elem = len(desc_unique_elem)
340 nr_define_unique_elem = len(define_unique_elem)
341 if nr_desc_unique_elem != nr_define_unique_elem:
343 The number of unique %s in description (%d) doesn\'t match the number of unique %s defined in %s (%d)
344 ''' % (type, nr_desc_unique_elem, type, instance, nr_define_unique_elem)
345 if nr_desc_unique_elem < nr_define_unique_elem:
346 # Function description is parsed until no helper is found (which can be due to
347 # misformatting). Hence, only print the first missing/misformatted helper/enum.
349 The description for %s is not present or formatted correctly.
350 ''' % (define_unique_elem[nr_desc_unique_elem])
351 raise Exception(exception_msg)
353 class PrinterRST(Printer):
355 A generic class for printers that print ReStructured Text. Printers should
356 be created with a HeaderParser object, and implement a way to print API
357 elements in the desired fashion.
358 @parser: A HeaderParser with objects to print to standard output
360 def __init__(self, parser):
363 def print_license(self):
365 .. Copyright (C) All BPF authors and contributors from 2014 to present.
366 .. See git log include/uapi/linux/bpf.h in kernel tree for details.
368 .. SPDX-License-Identifier: Linux-man-pages-copyleft
370 .. Please do not edit this file. It was generated from the documentation
371 .. located in file include/uapi/linux/bpf.h of the Linux kernel sources
372 .. (helpers description), and from scripts/bpf_doc.py in the same
373 .. repository (header and footer).
377 def print_elem(self, elem):
379 print('\tDescription')
380 # Do not strip all newline characters: formatted code at the end of
381 # a section must be followed by a blank line.
382 for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):
383 print('{}{}'.format('\t\t' if line else '', line))
387 for line in elem.ret.rstrip().split('\n'):
388 print('{}{}'.format('\t\t' if line else '', line))
392 def get_kernel_version(self):
394 version = subprocess.run(['git', 'describe'], cwd=linuxRoot,
395 capture_output=True, check=True)
396 version = version.stdout.decode().rstrip()
399 version = subprocess.run(['make', 'kernelversion'], cwd=linuxRoot,
400 capture_output=True, check=True)
401 version = version.stdout.decode().rstrip()
404 return 'Linux {version}'.format(version=version)
406 def get_last_doc_update(self, delimiter):
408 cmd = ['git', 'log', '-1', '--pretty=format:%cs', '--no-patch',
410 '/{}/,/\*\//:include/uapi/linux/bpf.h'.format(delimiter)]
411 date = subprocess.run(cmd, cwd=linuxRoot,
412 capture_output=True, check=True)
413 return date.stdout.decode().rstrip()
417 class PrinterHelpersRST(PrinterRST):
419 A printer for dumping collected information about helpers as a ReStructured
420 Text page compatible with the rst2man program, which can be used to
421 generate a manual page for the helpers.
422 @parser: A HeaderParser with Helper objects to print to standard output
424 def __init__(self, parser):
425 self.elements = parser.helpers
426 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
428 def print_header(self):
433 -------------------------------------------------------------------------------
434 list of eBPF helper functions
435 -------------------------------------------------------------------------------
444 The extended Berkeley Packet Filter (eBPF) subsystem consists in programs
445 written in a pseudo-assembly language, then attached to one of the several
446 kernel hooks and run in reaction of specific events. This framework differs
447 from the older, "classic" BPF (or "cBPF") in several aspects, one of them being
448 the ability to call special functions (or "helpers") from within a program.
449 These functions are restricted to a white-list of helpers defined in the
452 These helpers are used by eBPF programs to interact with the system, or with
453 the context in which they work. For instance, they can be used to print
454 debugging messages, to get the time since the system was booted, to interact
455 with eBPF maps, or to manipulate network packets. Since there are several eBPF
456 program types, and that they do not run in the same context, each program type
457 can only call a subset of those helpers.
459 Due to eBPF conventions, a helper can not have more than five arguments.
461 Internally, eBPF programs call directly into the compiled helper functions
462 without requiring any foreign-function interface. As a result, calling helpers
463 introduces no overhead, thus offering excellent performance.
465 This document is an attempt to list and document the helpers available to eBPF
466 developers. They are sorted by chronological order (the oldest helpers in the
472 kernelVersion = self.get_kernel_version()
473 lastUpdate = self.get_last_doc_update(helpersDocStart)
475 PrinterRST.print_license(self)
476 print(header.format(version=kernelVersion,
477 date_field = ':Date: ' if lastUpdate else '',
480 def print_footer(self):
485 Example usage for most of the eBPF helpers listed in this manual page are
486 available within the Linux kernel sources, at the following locations:
489 * *tools/testing/selftests/bpf/*
494 eBPF programs can have an associated license, passed along with the bytecode
495 instructions to the kernel when the programs are loaded. The format for that
496 string is identical to the one in use for kernel modules (Dual licenses, such
497 as "Dual BSD/GPL", may be used). Some helper functions are only accessible to
498 programs that are compatible with the GNU Privacy License (GPL).
500 In order to use such helpers, the eBPF program must be loaded with the correct
501 license string passed (via **attr**) to the **bpf**\ () system call, and this
502 generally translates into the C source code of the program containing a line
503 similar to the following:
507 char ____license[] __attribute__((section("license"), used)) = "GPL";
512 This manual page is an effort to document the existing eBPF helper functions.
513 But as of this writing, the BPF sub-system is under heavy development. New eBPF
514 program or map types are added, along with new helper functions. Some helpers
515 are occasionally made available for additional program types. So in spite of
516 the efforts of the community, this page might not be up-to-date. If you want to
517 check by yourself what helper functions exist in your kernel, or what types of
518 programs they can support, here are some files among the kernel tree that you
519 may be interested in:
521 * *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list
522 of all helper functions, as well as many other BPF definitions including most
523 of the flags, structs or constants used by the helpers.
524 * *net/core/filter.c* contains the definition of most network-related helper
525 functions, and the list of program types from which they can be used.
526 * *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related
528 * *kernel/bpf/verifier.c* contains the functions used to check that valid types
529 of eBPF maps are used with a given helper function.
530 * *kernel/bpf/* directory contains other files in which additional helpers are
531 defined (for cgroups, sockmaps, etc.).
532 * The bpftool utility can be used to probe the availability of helper functions
533 on the system (as well as supported program and map types, and a number of
534 other parameters). To do so, run **bpftool feature probe** (see
535 **bpftool-feature**\ (8) for details). Add the **unprivileged** keyword to
536 list features available to unprivileged users.
538 Compatibility between helper functions and program types can generally be found
539 in the files where helper functions are defined. Look for the **struct
540 bpf_func_proto** objects and for functions returning them: these functions
541 contain a list of helpers that a given program type can call. Note that the
542 **default:** label of the **switch ... case** used to filter helpers can call
543 other functions, themselves allowing access to additional helpers. The
544 requirement for GPL license is also in those **struct bpf_func_proto**.
546 Compatibility between helper functions and map types can be found in the
547 **check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*.
549 Helper functions that invalidate the checks on **data** and **data_end**
550 pointers for network processing are listed in function
551 **bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*.
560 **perf_event_open**\ (2),
566 def print_proto(self, helper):
568 Format function protocol with bold and italics markers. This makes RST
569 file less readable, but gives nice results in the manual page.
571 proto = helper.proto_break_down()
573 print('**%s %s%s(' % (proto['ret_type'],
574 proto['ret_star'].replace('*', '\\*'),
579 for a in proto['args']:
580 one_arg = '{}{}'.format(comma, a['type'])
583 one_arg += ' {}**\ '.format(a['star'].replace('*', '\\*'))
586 one_arg += '*{}*\\ **'.format(a['name'])
588 print(one_arg, end='')
592 def print_one(self, helper):
593 self.print_proto(helper)
594 self.print_elem(helper)
597 class PrinterSyscallRST(PrinterRST):
599 A printer for dumping collected information about the syscall API as a
600 ReStructured Text page compatible with the rst2man program, which can be
601 used to generate a manual page for the syscall.
602 @parser: A HeaderParser with APIElement objects to print to standard
605 def __init__(self, parser):
606 self.elements = parser.commands
607 self.elem_number_check(parser.desc_syscalls, parser.enum_syscalls, 'syscall', 'bpf_cmd')
609 def print_header(self):
614 -------------------------------------------------------------------------------
615 Perform a command on an extended BPF object
616 -------------------------------------------------------------------------------
623 PrinterRST.print_license(self)
626 def print_one(self, command):
627 print('**%s**' % (command.proto))
628 self.print_elem(command)
631 class PrinterHelpers(Printer):
633 A printer for dumping collected information about helpers as C header to
634 be included from BPF program.
635 @parser: A HeaderParser with Helper objects to print to standard output
637 def __init__(self, parser):
638 self.elements = parser.helpers
639 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
642 'struct bpf_fib_lookup',
643 'struct bpf_sk_lookup',
644 'struct bpf_perf_event_data',
645 'struct bpf_perf_event_value',
646 'struct bpf_pidns_info',
647 'struct bpf_redir_neigh',
649 'struct bpf_sock_addr',
650 'struct bpf_sock_ops',
651 'struct bpf_sock_tuple',
652 'struct bpf_spin_lock',
654 'struct bpf_tcp_sock',
655 'struct bpf_tunnel_key',
656 'struct bpf_xfrm_state',
657 'struct linux_binprm',
659 'struct sk_reuseport_md',
665 'struct tcp_timewait_sock',
666 'struct tcp_request_sock',
669 'struct task_struct',
699 'struct bpf_fib_lookup',
700 'struct bpf_perf_event_data',
701 'struct bpf_perf_event_value',
702 'struct bpf_pidns_info',
703 'struct bpf_redir_neigh',
704 'struct bpf_sk_lookup',
706 'struct bpf_sock_addr',
707 'struct bpf_sock_ops',
708 'struct bpf_sock_tuple',
709 'struct bpf_spin_lock',
711 'struct bpf_tcp_sock',
712 'struct bpf_tunnel_key',
713 'struct bpf_xfrm_state',
714 'struct linux_binprm',
716 'struct sk_reuseport_md',
722 'struct tcp_timewait_sock',
723 'struct tcp_request_sock',
726 'struct task_struct',
747 'size_t': 'unsigned long',
748 'struct bpf_map': 'void',
749 'struct sk_buff': 'struct __sk_buff',
750 'const struct sk_buff': 'const struct __sk_buff',
751 'struct sk_msg_buff': 'struct sk_msg_md',
752 'struct xdp_buff': 'struct xdp_md',
754 # Helpers overloaded for different context types.
755 overloaded_helpers = [
756 'bpf_get_socket_cookie',
760 def print_header(self):
762 /* This is auto-generated file. See bpf_doc.py for details. */
764 /* Forward declarations of BPF structs */'''
767 for fwd in self.type_fwds:
771 def print_footer(self):
775 def map_type(self, t):
776 if t in self.known_types:
778 if t in self.mapped_types:
779 return self.mapped_types[t]
780 print("Unrecognized type '%s', please add it to known types!" % t,
786 def print_one(self, helper):
787 proto = helper.proto_break_down()
789 if proto['name'] in self.seen_helpers:
791 self.seen_helpers.add(proto['name'])
794 print(" * %s" % proto['name'])
797 # Do not strip all newline characters: formatted code at the end of
798 # a section must be followed by a blank line.
799 for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
800 print(' *{}{}'.format(' \t' if line else '', line))
805 for line in helper.ret.rstrip().split('\n'):
806 print(' *{}{}'.format(' \t' if line else '', line))
809 print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
810 proto['ret_star'], proto['name']), end='')
812 for i, a in enumerate(proto['args']):
815 if proto['name'] in self.overloaded_helpers and i == 0:
818 one_arg = '{}{}'.format(comma, self.map_type(t))
821 one_arg += ' {}'.format(a['star'])
824 one_arg += '{}'.format(n)
826 print(one_arg, end='')
828 print(') = (void *) %d;' % helper.enum_val)
831 ###############################################################################
833 # If script is launched from scripts/ from kernel tree and can access
834 # ../include/uapi/linux/bpf.h, use it as a default name for the file to parse,
835 # otherwise the --filename argument will be required from the command line.
836 script = os.path.abspath(sys.argv[0])
837 linuxRoot = os.path.dirname(os.path.dirname(script))
838 bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
841 'helpers': PrinterHelpersRST,
842 'syscall': PrinterSyscallRST,
845 argParser = argparse.ArgumentParser(description="""
846 Parse eBPF header file and generate documentation for the eBPF API.
847 The RST-formatted output produced can be turned into a manual page with the
850 argParser.add_argument('--header', action='store_true',
851 help='generate C header file')
852 if (os.path.isfile(bpfh)):
853 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h',
856 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
857 argParser.add_argument('target', nargs='?', default='helpers',
858 choices=printers.keys(), help='eBPF API target')
859 args = argParser.parse_args()
862 headerParser = HeaderParser(args.filename)
865 # Print formatted output to standard output.
867 if args.target != 'helpers':
868 raise NotImplementedError('Only helpers header generation is supported')
869 printer = PrinterHelpers(headerParser)
871 printer = printers[args.target](headerParser)