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 proto_break_down(self):
55 Break down helper function protocol into smaller chunks: return type,
56 name, distincts arguments.
58 arg_re = re.compile('((\w+ )*?(\w+|...))( (\**)(\w+))?$')
60 proto_re = re.compile('(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$')
62 capture = proto_re.match(self.proto)
63 res['ret_type'] = capture.group(1)
64 res['ret_star'] = capture.group(2)
65 res['name'] = capture.group(3)
68 args = capture.group(4).split(', ')
70 capture = arg_re.match(a)
72 'type' : capture.group(1),
73 'star' : capture.group(5),
74 'name' : capture.group(6)
80 class HeaderParser(object):
82 An object used to parse a file in order to extract the documentation of a
83 list of eBPF helper functions. All the helpers that can be retrieved are
84 stored as Helper object, in the self.helpers() array.
85 @filename: name of file to parse, usually include/uapi/linux/bpf.h in the
88 def __init__(self, filename):
89 self.reader = open(filename, 'r')
93 self.desc_unique_helpers = set()
94 self.define_unique_helpers = []
95 self.desc_syscalls = []
96 self.enum_syscalls = []
98 def parse_element(self):
99 proto = self.parse_symbol()
100 desc = self.parse_desc(proto)
101 ret = self.parse_ret(proto)
102 return APIElement(proto=proto, desc=desc, ret=ret)
104 def parse_helper(self):
105 proto = self.parse_proto()
106 desc = self.parse_desc(proto)
107 ret = self.parse_ret(proto)
108 return Helper(proto=proto, desc=desc, ret=ret)
110 def parse_symbol(self):
111 p = re.compile(' \* ?(BPF\w+)$')
112 capture = p.match(self.line)
114 raise NoSyscallCommandFound
115 end_re = re.compile(' \* ?NOTES$')
116 end = end_re.match(self.line)
118 raise NoSyscallCommandFound
119 self.line = self.reader.readline()
120 return capture.group(1)
122 def parse_proto(self):
123 # Argument can be of shape:
127 # - Same as above, with "const" and/or "struct" in front of type
128 # - "..." (undefined number of arguments, for bpf_trace_printk())
129 # There is at least one term ("void"), and at most five arguments.
130 p = re.compile(' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$')
131 capture = p.match(self.line)
134 self.line = self.reader.readline()
135 return capture.group(1)
137 def parse_desc(self, proto):
138 p = re.compile(' \* ?(?:\t| {5,8})Description$')
139 capture = p.match(self.line)
141 raise Exception("No description section found for " + proto)
142 # Description can be several lines, some of them possibly empty, and it
143 # stops when another subsection title is met.
147 self.line = self.reader.readline()
148 if self.line == ' *\n':
151 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
152 capture = p.match(self.line)
155 desc += capture.group(1) + '\n'
160 raise Exception("No description found for " + proto)
163 def parse_ret(self, proto):
164 p = re.compile(' \* ?(?:\t| {5,8})Return$')
165 capture = p.match(self.line)
167 raise Exception("No return section found for " + proto)
168 # Return value description can be several lines, some of them possibly
169 # empty, and it stops when another subsection title is met.
173 self.line = self.reader.readline()
174 if self.line == ' *\n':
177 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
178 capture = p.match(self.line)
181 ret += capture.group(1) + '\n'
186 raise Exception("No return found for " + proto)
189 def seek_to(self, target, help_message, discard_lines = 1):
191 offset = self.reader.read().find(target)
193 raise Exception(help_message)
194 self.reader.seek(offset)
195 self.reader.readline()
196 for _ in range(discard_lines):
197 self.reader.readline()
198 self.line = self.reader.readline()
200 def parse_desc_syscall(self):
201 self.seek_to('* DOC: eBPF Syscall Commands',
202 'Could not find start of eBPF syscall descriptions list')
205 command = self.parse_element()
206 self.commands.append(command)
207 self.desc_syscalls.append(command.proto)
209 except NoSyscallCommandFound:
212 def parse_enum_syscall(self):
213 self.seek_to('enum bpf_cmd {',
214 'Could not find start of bpf_cmd enum', 0)
215 # Searches for either one or more BPF\w+ enums
216 bpf_p = re.compile('\s*(BPF\w+)+')
217 # Searches for an enum entry assigned to another entry,
218 # for e.g. BPF_PROG_RUN = BPF_PROG_TEST_RUN, which is
219 # not documented hence should be skipped in check to
220 # determine if the right number of syscalls are documented
221 assign_p = re.compile('\s*(BPF\w+)\s*=\s*(BPF\w+)')
224 capture = assign_p.match(self.line)
226 # Skip line if an enum entry is assigned to another entry
227 self.line = self.reader.readline()
229 capture = bpf_p.match(self.line)
231 bpf_cmd_str += self.line
234 self.line = self.reader.readline()
235 # Find the number of occurences of BPF\w+
236 self.enum_syscalls = re.findall('(BPF\w+)+', bpf_cmd_str)
238 def parse_desc_helpers(self):
239 self.seek_to(helpersDocStart,
240 'Could not find start of eBPF helper descriptions list')
243 helper = self.parse_helper()
244 self.helpers.append(helper)
245 proto = helper.proto_break_down()
246 self.desc_unique_helpers.add(proto['name'])
247 except NoHelperFound:
250 def parse_define_helpers(self):
251 # Parse the number of FN(...) in #define __BPF_FUNC_MAPPER to compare
252 # later with the number of unique function names present in description.
253 # Note: seek_to(..) discards the first line below the target search text,
254 # resulting in FN(unspec) being skipped and not added to self.define_unique_helpers.
255 self.seek_to('#define __BPF_FUNC_MAPPER(FN)',
256 'Could not find start of eBPF helper definition list')
257 # Searches for either one or more FN(\w+) defines or a backslash for newline
258 p = re.compile('\s*(FN\(\w+\))+|\\\\')
261 capture = p.match(self.line)
263 fn_defines_str += self.line
266 self.line = self.reader.readline()
267 # Find the number of occurences of FN(\w+)
268 self.define_unique_helpers = re.findall('FN\(\w+\)', fn_defines_str)
271 self.parse_desc_syscall()
272 self.parse_enum_syscall()
273 self.parse_desc_helpers()
274 self.parse_define_helpers()
277 ###############################################################################
279 class Printer(object):
281 A generic class for printers. Printers should be created with an array of
282 Helper objects, and implement a way to print them in the desired fashion.
283 @parser: A HeaderParser with objects to print to standard output
285 def __init__(self, parser):
289 def print_header(self):
292 def print_footer(self):
295 def print_one(self, helper):
300 for elem in self.elements:
304 def elem_number_check(self, desc_unique_elem, define_unique_elem, type, instance):
306 Checks the number of helpers/syscalls documented within the header file
307 description with those defined as part of enum/macro and raise an
308 Exception if they don't match.
310 nr_desc_unique_elem = len(desc_unique_elem)
311 nr_define_unique_elem = len(define_unique_elem)
312 if nr_desc_unique_elem != nr_define_unique_elem:
314 The number of unique %s in description (%d) doesn\'t match the number of unique %s defined in %s (%d)
315 ''' % (type, nr_desc_unique_elem, type, instance, nr_define_unique_elem)
316 if nr_desc_unique_elem < nr_define_unique_elem:
317 # Function description is parsed until no helper is found (which can be due to
318 # misformatting). Hence, only print the first missing/misformatted helper/enum.
320 The description for %s is not present or formatted correctly.
321 ''' % (define_unique_elem[nr_desc_unique_elem])
322 raise Exception(exception_msg)
324 class PrinterRST(Printer):
326 A generic class for printers that print ReStructured Text. Printers should
327 be created with a HeaderParser object, and implement a way to print API
328 elements in the desired fashion.
329 @parser: A HeaderParser with objects to print to standard output
331 def __init__(self, parser):
334 def print_license(self):
336 .. Copyright (C) All BPF authors and contributors from 2014 to present.
337 .. See git log include/uapi/linux/bpf.h in kernel tree for details.
339 .. SPDX-License-Identifier: Linux-man-pages-copyleft
341 .. Please do not edit this file. It was generated from the documentation
342 .. located in file include/uapi/linux/bpf.h of the Linux kernel sources
343 .. (helpers description), and from scripts/bpf_doc.py in the same
344 .. repository (header and footer).
348 def print_elem(self, elem):
350 print('\tDescription')
351 # Do not strip all newline characters: formatted code at the end of
352 # a section must be followed by a blank line.
353 for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):
354 print('{}{}'.format('\t\t' if line else '', line))
358 for line in elem.ret.rstrip().split('\n'):
359 print('{}{}'.format('\t\t' if line else '', line))
363 def get_kernel_version(self):
365 version = subprocess.run(['git', 'describe'], cwd=linuxRoot,
366 capture_output=True, check=True)
367 version = version.stdout.decode().rstrip()
370 version = subprocess.run(['make', 'kernelversion'], cwd=linuxRoot,
371 capture_output=True, check=True)
372 version = version.stdout.decode().rstrip()
375 return 'Linux {version}'.format(version=version)
377 def get_last_doc_update(self, delimiter):
379 cmd = ['git', 'log', '-1', '--pretty=format:%cs', '--no-patch',
381 '/{}/,/\*\//:include/uapi/linux/bpf.h'.format(delimiter)]
382 date = subprocess.run(cmd, cwd=linuxRoot,
383 capture_output=True, check=True)
384 return date.stdout.decode().rstrip()
388 class PrinterHelpersRST(PrinterRST):
390 A printer for dumping collected information about helpers as a ReStructured
391 Text page compatible with the rst2man program, which can be used to
392 generate a manual page for the helpers.
393 @parser: A HeaderParser with Helper objects to print to standard output
395 def __init__(self, parser):
396 self.elements = parser.helpers
397 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
399 def print_header(self):
404 -------------------------------------------------------------------------------
405 list of eBPF helper functions
406 -------------------------------------------------------------------------------
415 The extended Berkeley Packet Filter (eBPF) subsystem consists in programs
416 written in a pseudo-assembly language, then attached to one of the several
417 kernel hooks and run in reaction of specific events. This framework differs
418 from the older, "classic" BPF (or "cBPF") in several aspects, one of them being
419 the ability to call special functions (or "helpers") from within a program.
420 These functions are restricted to a white-list of helpers defined in the
423 These helpers are used by eBPF programs to interact with the system, or with
424 the context in which they work. For instance, they can be used to print
425 debugging messages, to get the time since the system was booted, to interact
426 with eBPF maps, or to manipulate network packets. Since there are several eBPF
427 program types, and that they do not run in the same context, each program type
428 can only call a subset of those helpers.
430 Due to eBPF conventions, a helper can not have more than five arguments.
432 Internally, eBPF programs call directly into the compiled helper functions
433 without requiring any foreign-function interface. As a result, calling helpers
434 introduces no overhead, thus offering excellent performance.
436 This document is an attempt to list and document the helpers available to eBPF
437 developers. They are sorted by chronological order (the oldest helpers in the
443 kernelVersion = self.get_kernel_version()
444 lastUpdate = self.get_last_doc_update(helpersDocStart)
446 PrinterRST.print_license(self)
447 print(header.format(version=kernelVersion,
448 date_field = ':Date: ' if lastUpdate else '',
451 def print_footer(self):
456 Example usage for most of the eBPF helpers listed in this manual page are
457 available within the Linux kernel sources, at the following locations:
460 * *tools/testing/selftests/bpf/*
465 eBPF programs can have an associated license, passed along with the bytecode
466 instructions to the kernel when the programs are loaded. The format for that
467 string is identical to the one in use for kernel modules (Dual licenses, such
468 as "Dual BSD/GPL", may be used). Some helper functions are only accessible to
469 programs that are compatible with the GNU Privacy License (GPL).
471 In order to use such helpers, the eBPF program must be loaded with the correct
472 license string passed (via **attr**) to the **bpf**\ () system call, and this
473 generally translates into the C source code of the program containing a line
474 similar to the following:
478 char ____license[] __attribute__((section("license"), used)) = "GPL";
483 This manual page is an effort to document the existing eBPF helper functions.
484 But as of this writing, the BPF sub-system is under heavy development. New eBPF
485 program or map types are added, along with new helper functions. Some helpers
486 are occasionally made available for additional program types. So in spite of
487 the efforts of the community, this page might not be up-to-date. If you want to
488 check by yourself what helper functions exist in your kernel, or what types of
489 programs they can support, here are some files among the kernel tree that you
490 may be interested in:
492 * *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list
493 of all helper functions, as well as many other BPF definitions including most
494 of the flags, structs or constants used by the helpers.
495 * *net/core/filter.c* contains the definition of most network-related helper
496 functions, and the list of program types from which they can be used.
497 * *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related
499 * *kernel/bpf/verifier.c* contains the functions used to check that valid types
500 of eBPF maps are used with a given helper function.
501 * *kernel/bpf/* directory contains other files in which additional helpers are
502 defined (for cgroups, sockmaps, etc.).
503 * The bpftool utility can be used to probe the availability of helper functions
504 on the system (as well as supported program and map types, and a number of
505 other parameters). To do so, run **bpftool feature probe** (see
506 **bpftool-feature**\ (8) for details). Add the **unprivileged** keyword to
507 list features available to unprivileged users.
509 Compatibility between helper functions and program types can generally be found
510 in the files where helper functions are defined. Look for the **struct
511 bpf_func_proto** objects and for functions returning them: these functions
512 contain a list of helpers that a given program type can call. Note that the
513 **default:** label of the **switch ... case** used to filter helpers can call
514 other functions, themselves allowing access to additional helpers. The
515 requirement for GPL license is also in those **struct bpf_func_proto**.
517 Compatibility between helper functions and map types can be found in the
518 **check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*.
520 Helper functions that invalidate the checks on **data** and **data_end**
521 pointers for network processing are listed in function
522 **bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*.
531 **perf_event_open**\ (2),
537 def print_proto(self, helper):
539 Format function protocol with bold and italics markers. This makes RST
540 file less readable, but gives nice results in the manual page.
542 proto = helper.proto_break_down()
544 print('**%s %s%s(' % (proto['ret_type'],
545 proto['ret_star'].replace('*', '\\*'),
550 for a in proto['args']:
551 one_arg = '{}{}'.format(comma, a['type'])
554 one_arg += ' {}**\ '.format(a['star'].replace('*', '\\*'))
557 one_arg += '*{}*\\ **'.format(a['name'])
559 print(one_arg, end='')
563 def print_one(self, helper):
564 self.print_proto(helper)
565 self.print_elem(helper)
568 class PrinterSyscallRST(PrinterRST):
570 A printer for dumping collected information about the syscall API as a
571 ReStructured Text page compatible with the rst2man program, which can be
572 used to generate a manual page for the syscall.
573 @parser: A HeaderParser with APIElement objects to print to standard
576 def __init__(self, parser):
577 self.elements = parser.commands
578 self.elem_number_check(parser.desc_syscalls, parser.enum_syscalls, 'syscall', 'bpf_cmd')
580 def print_header(self):
585 -------------------------------------------------------------------------------
586 Perform a command on an extended BPF object
587 -------------------------------------------------------------------------------
594 PrinterRST.print_license(self)
597 def print_one(self, command):
598 print('**%s**' % (command.proto))
599 self.print_elem(command)
602 class PrinterHelpers(Printer):
604 A printer for dumping collected information about helpers as C header to
605 be included from BPF program.
606 @parser: A HeaderParser with Helper objects to print to standard output
608 def __init__(self, parser):
609 self.elements = parser.helpers
610 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
613 'struct bpf_fib_lookup',
614 'struct bpf_sk_lookup',
615 'struct bpf_perf_event_data',
616 'struct bpf_perf_event_value',
617 'struct bpf_pidns_info',
618 'struct bpf_redir_neigh',
620 'struct bpf_sock_addr',
621 'struct bpf_sock_ops',
622 'struct bpf_sock_tuple',
623 'struct bpf_spin_lock',
625 'struct bpf_tcp_sock',
626 'struct bpf_tunnel_key',
627 'struct bpf_xfrm_state',
628 'struct linux_binprm',
630 'struct sk_reuseport_md',
636 'struct tcp_timewait_sock',
637 'struct tcp_request_sock',
640 'struct task_struct',
670 'struct bpf_fib_lookup',
671 'struct bpf_perf_event_data',
672 'struct bpf_perf_event_value',
673 'struct bpf_pidns_info',
674 'struct bpf_redir_neigh',
675 'struct bpf_sk_lookup',
677 'struct bpf_sock_addr',
678 'struct bpf_sock_ops',
679 'struct bpf_sock_tuple',
680 'struct bpf_spin_lock',
682 'struct bpf_tcp_sock',
683 'struct bpf_tunnel_key',
684 'struct bpf_xfrm_state',
685 'struct linux_binprm',
687 'struct sk_reuseport_md',
693 'struct tcp_timewait_sock',
694 'struct tcp_request_sock',
697 'struct task_struct',
718 'size_t': 'unsigned long',
719 'struct bpf_map': 'void',
720 'struct sk_buff': 'struct __sk_buff',
721 'const struct sk_buff': 'const struct __sk_buff',
722 'struct sk_msg_buff': 'struct sk_msg_md',
723 'struct xdp_buff': 'struct xdp_md',
725 # Helpers overloaded for different context types.
726 overloaded_helpers = [
727 'bpf_get_socket_cookie',
731 def print_header(self):
733 /* This is auto-generated file. See bpf_doc.py for details. */
735 /* Forward declarations of BPF structs */'''
738 for fwd in self.type_fwds:
742 def print_footer(self):
746 def map_type(self, t):
747 if t in self.known_types:
749 if t in self.mapped_types:
750 return self.mapped_types[t]
751 print("Unrecognized type '%s', please add it to known types!" % t,
757 def print_one(self, helper):
758 proto = helper.proto_break_down()
760 if proto['name'] in self.seen_helpers:
762 self.seen_helpers.add(proto['name'])
765 print(" * %s" % proto['name'])
768 # Do not strip all newline characters: formatted code at the end of
769 # a section must be followed by a blank line.
770 for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
771 print(' *{}{}'.format(' \t' if line else '', line))
776 for line in helper.ret.rstrip().split('\n'):
777 print(' *{}{}'.format(' \t' if line else '', line))
780 print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
781 proto['ret_star'], proto['name']), end='')
783 for i, a in enumerate(proto['args']):
786 if proto['name'] in self.overloaded_helpers and i == 0:
789 one_arg = '{}{}'.format(comma, self.map_type(t))
792 one_arg += ' {}'.format(a['star'])
795 one_arg += '{}'.format(n)
797 print(one_arg, end='')
799 print(') = (void *) %d;' % len(self.seen_helpers))
802 ###############################################################################
804 # If script is launched from scripts/ from kernel tree and can access
805 # ../include/uapi/linux/bpf.h, use it as a default name for the file to parse,
806 # otherwise the --filename argument will be required from the command line.
807 script = os.path.abspath(sys.argv[0])
808 linuxRoot = os.path.dirname(os.path.dirname(script))
809 bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
812 'helpers': PrinterHelpersRST,
813 'syscall': PrinterSyscallRST,
816 argParser = argparse.ArgumentParser(description="""
817 Parse eBPF header file and generate documentation for the eBPF API.
818 The RST-formatted output produced can be turned into a manual page with the
821 argParser.add_argument('--header', action='store_true',
822 help='generate C header file')
823 if (os.path.isfile(bpfh)):
824 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h',
827 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
828 argParser.add_argument('target', nargs='?', default='helpers',
829 choices=printers.keys(), help='eBPF API target')
830 args = argParser.parse_args()
833 headerParser = HeaderParser(args.filename)
836 # Print formatted output to standard output.
838 if args.target != 'helpers':
839 raise NotImplementedError('Only helpers header generation is supported')
840 printer = PrinterHelpers(headerParser)
842 printer = printers[args.target](headerParser)