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 .. %%%LICENSE_START(VERBATIM)
337 .. Permission is granted to make and distribute verbatim copies of this
338 .. manual provided the copyright notice and this permission notice are
339 .. preserved on all copies.
341 .. Permission is granted to copy and distribute modified versions of this
342 .. manual under the conditions for verbatim copying, provided that the
343 .. entire resulting derived work is distributed under the terms of a
344 .. permission notice identical to this one.
346 .. Since the Linux kernel and libraries are constantly changing, this
347 .. manual page may be incorrect or out-of-date. The author(s) assume no
348 .. responsibility for errors or omissions, or for damages resulting from
349 .. the use of the information contained herein. The author(s) may not
350 .. have taken the same level of care in the production of this manual,
351 .. which is licensed free of charge, as they might when working
354 .. Formatted or processed versions of this manual, if unaccompanied by
355 .. the source, must acknowledge the copyright and authors of this work.
358 .. Please do not edit this file. It was generated from the documentation
359 .. located in file include/uapi/linux/bpf.h of the Linux kernel sources
360 .. (helpers description), and from scripts/bpf_doc.py in the same
361 .. repository (header and footer).
365 def print_elem(self, elem):
367 print('\tDescription')
368 # Do not strip all newline characters: formatted code at the end of
369 # a section must be followed by a blank line.
370 for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):
371 print('{}{}'.format('\t\t' if line else '', line))
375 for line in elem.ret.rstrip().split('\n'):
376 print('{}{}'.format('\t\t' if line else '', line))
380 class PrinterHelpersRST(PrinterRST):
382 A printer for dumping collected information about helpers as a ReStructured
383 Text page compatible with the rst2man program, which can be used to
384 generate a manual page for the helpers.
385 @parser: A HeaderParser with Helper objects to print to standard output
387 def __init__(self, parser):
388 self.elements = parser.helpers
389 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
391 def print_header(self):
396 -------------------------------------------------------------------------------
397 list of eBPF helper functions
398 -------------------------------------------------------------------------------
405 The extended Berkeley Packet Filter (eBPF) subsystem consists in programs
406 written in a pseudo-assembly language, then attached to one of the several
407 kernel hooks and run in reaction of specific events. This framework differs
408 from the older, "classic" BPF (or "cBPF") in several aspects, one of them being
409 the ability to call special functions (or "helpers") from within a program.
410 These functions are restricted to a white-list of helpers defined in the
413 These helpers are used by eBPF programs to interact with the system, or with
414 the context in which they work. For instance, they can be used to print
415 debugging messages, to get the time since the system was booted, to interact
416 with eBPF maps, or to manipulate network packets. Since there are several eBPF
417 program types, and that they do not run in the same context, each program type
418 can only call a subset of those helpers.
420 Due to eBPF conventions, a helper can not have more than five arguments.
422 Internally, eBPF programs call directly into the compiled helper functions
423 without requiring any foreign-function interface. As a result, calling helpers
424 introduces no overhead, thus offering excellent performance.
426 This document is an attempt to list and document the helpers available to eBPF
427 developers. They are sorted by chronological order (the oldest helpers in the
433 PrinterRST.print_license(self)
436 def print_footer(self):
441 Example usage for most of the eBPF helpers listed in this manual page are
442 available within the Linux kernel sources, at the following locations:
445 * *tools/testing/selftests/bpf/*
450 eBPF programs can have an associated license, passed along with the bytecode
451 instructions to the kernel when the programs are loaded. The format for that
452 string is identical to the one in use for kernel modules (Dual licenses, such
453 as "Dual BSD/GPL", may be used). Some helper functions are only accessible to
454 programs that are compatible with the GNU Privacy License (GPL).
456 In order to use such helpers, the eBPF program must be loaded with the correct
457 license string passed (via **attr**) to the **bpf**\ () system call, and this
458 generally translates into the C source code of the program containing a line
459 similar to the following:
463 char ____license[] __attribute__((section("license"), used)) = "GPL";
468 This manual page is an effort to document the existing eBPF helper functions.
469 But as of this writing, the BPF sub-system is under heavy development. New eBPF
470 program or map types are added, along with new helper functions. Some helpers
471 are occasionally made available for additional program types. So in spite of
472 the efforts of the community, this page might not be up-to-date. If you want to
473 check by yourself what helper functions exist in your kernel, or what types of
474 programs they can support, here are some files among the kernel tree that you
475 may be interested in:
477 * *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list
478 of all helper functions, as well as many other BPF definitions including most
479 of the flags, structs or constants used by the helpers.
480 * *net/core/filter.c* contains the definition of most network-related helper
481 functions, and the list of program types from which they can be used.
482 * *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related
484 * *kernel/bpf/verifier.c* contains the functions used to check that valid types
485 of eBPF maps are used with a given helper function.
486 * *kernel/bpf/* directory contains other files in which additional helpers are
487 defined (for cgroups, sockmaps, etc.).
488 * The bpftool utility can be used to probe the availability of helper functions
489 on the system (as well as supported program and map types, and a number of
490 other parameters). To do so, run **bpftool feature probe** (see
491 **bpftool-feature**\ (8) for details). Add the **unprivileged** keyword to
492 list features available to unprivileged users.
494 Compatibility between helper functions and program types can generally be found
495 in the files where helper functions are defined. Look for the **struct
496 bpf_func_proto** objects and for functions returning them: these functions
497 contain a list of helpers that a given program type can call. Note that the
498 **default:** label of the **switch ... case** used to filter helpers can call
499 other functions, themselves allowing access to additional helpers. The
500 requirement for GPL license is also in those **struct bpf_func_proto**.
502 Compatibility between helper functions and map types can be found in the
503 **check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*.
505 Helper functions that invalidate the checks on **data** and **data_end**
506 pointers for network processing are listed in function
507 **bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*.
516 **perf_event_open**\ (2),
522 def print_proto(self, helper):
524 Format function protocol with bold and italics markers. This makes RST
525 file less readable, but gives nice results in the manual page.
527 proto = helper.proto_break_down()
529 print('**%s %s%s(' % (proto['ret_type'],
530 proto['ret_star'].replace('*', '\\*'),
535 for a in proto['args']:
536 one_arg = '{}{}'.format(comma, a['type'])
539 one_arg += ' {}**\ '.format(a['star'].replace('*', '\\*'))
542 one_arg += '*{}*\\ **'.format(a['name'])
544 print(one_arg, end='')
548 def print_one(self, helper):
549 self.print_proto(helper)
550 self.print_elem(helper)
553 class PrinterSyscallRST(PrinterRST):
555 A printer for dumping collected information about the syscall API as a
556 ReStructured Text page compatible with the rst2man program, which can be
557 used to generate a manual page for the syscall.
558 @parser: A HeaderParser with APIElement objects to print to standard
561 def __init__(self, parser):
562 self.elements = parser.commands
563 self.elem_number_check(parser.desc_syscalls, parser.enum_syscalls, 'syscall', 'bpf_cmd')
565 def print_header(self):
570 -------------------------------------------------------------------------------
571 Perform a command on an extended BPF object
572 -------------------------------------------------------------------------------
579 PrinterRST.print_license(self)
582 def print_one(self, command):
583 print('**%s**' % (command.proto))
584 self.print_elem(command)
587 class PrinterHelpers(Printer):
589 A printer for dumping collected information about helpers as C header to
590 be included from BPF program.
591 @parser: A HeaderParser with Helper objects to print to standard output
593 def __init__(self, parser):
594 self.elements = parser.helpers
595 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
598 'struct bpf_fib_lookup',
599 'struct bpf_sk_lookup',
600 'struct bpf_perf_event_data',
601 'struct bpf_perf_event_value',
602 'struct bpf_pidns_info',
603 'struct bpf_redir_neigh',
605 'struct bpf_sock_addr',
606 'struct bpf_sock_ops',
607 'struct bpf_sock_tuple',
608 'struct bpf_spin_lock',
610 'struct bpf_tcp_sock',
611 'struct bpf_tunnel_key',
612 'struct bpf_xfrm_state',
613 'struct linux_binprm',
615 'struct sk_reuseport_md',
621 'struct tcp_timewait_sock',
622 'struct tcp_request_sock',
625 'struct task_struct',
653 'struct bpf_fib_lookup',
654 'struct bpf_perf_event_data',
655 'struct bpf_perf_event_value',
656 'struct bpf_pidns_info',
657 'struct bpf_redir_neigh',
658 'struct bpf_sk_lookup',
660 'struct bpf_sock_addr',
661 'struct bpf_sock_ops',
662 'struct bpf_sock_tuple',
663 'struct bpf_spin_lock',
665 'struct bpf_tcp_sock',
666 'struct bpf_tunnel_key',
667 'struct bpf_xfrm_state',
668 'struct linux_binprm',
670 'struct sk_reuseport_md',
676 'struct tcp_timewait_sock',
677 'struct tcp_request_sock',
680 'struct task_struct',
699 'size_t': 'unsigned long',
700 'struct bpf_map': 'void',
701 'struct sk_buff': 'struct __sk_buff',
702 'const struct sk_buff': 'const struct __sk_buff',
703 'struct sk_msg_buff': 'struct sk_msg_md',
704 'struct xdp_buff': 'struct xdp_md',
706 # Helpers overloaded for different context types.
707 overloaded_helpers = [
708 'bpf_get_socket_cookie',
712 def print_header(self):
714 /* This is auto-generated file. See bpf_doc.py for details. */
716 /* Forward declarations of BPF structs */'''
719 for fwd in self.type_fwds:
723 def print_footer(self):
727 def map_type(self, t):
728 if t in self.known_types:
730 if t in self.mapped_types:
731 return self.mapped_types[t]
732 print("Unrecognized type '%s', please add it to known types!" % t,
738 def print_one(self, helper):
739 proto = helper.proto_break_down()
741 if proto['name'] in self.seen_helpers:
743 self.seen_helpers.add(proto['name'])
746 print(" * %s" % proto['name'])
749 # Do not strip all newline characters: formatted code at the end of
750 # a section must be followed by a blank line.
751 for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
752 print(' *{}{}'.format(' \t' if line else '', line))
757 for line in helper.ret.rstrip().split('\n'):
758 print(' *{}{}'.format(' \t' if line else '', line))
761 print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
762 proto['ret_star'], proto['name']), end='')
764 for i, a in enumerate(proto['args']):
767 if proto['name'] in self.overloaded_helpers and i == 0:
770 one_arg = '{}{}'.format(comma, self.map_type(t))
773 one_arg += ' {}'.format(a['star'])
776 one_arg += '{}'.format(n)
778 print(one_arg, end='')
780 print(') = (void *) %d;' % len(self.seen_helpers))
783 ###############################################################################
785 # If script is launched from scripts/ from kernel tree and can access
786 # ../include/uapi/linux/bpf.h, use it as a default name for the file to parse,
787 # otherwise the --filename argument will be required from the command line.
788 script = os.path.abspath(sys.argv[0])
789 linuxRoot = os.path.dirname(os.path.dirname(script))
790 bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
793 'helpers': PrinterHelpersRST,
794 'syscall': PrinterSyscallRST,
797 argParser = argparse.ArgumentParser(description="""
798 Parse eBPF header file and generate documentation for the eBPF API.
799 The RST-formatted output produced can be turned into a manual page with the
802 argParser.add_argument('--header', action='store_true',
803 help='generate C header file')
804 if (os.path.isfile(bpfh)):
805 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h',
808 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
809 argParser.add_argument('target', nargs='?', default='helpers',
810 choices=printers.keys(), help='eBPF API target')
811 args = argParser.parse_args()
814 headerParser = HeaderParser(args.filename)
817 # Print formatted output to standard output.
819 if args.target != 'helpers':
820 raise NotImplementedError('Only helpers header generation is supported')
821 printer = PrinterHelpers(headerParser)
823 printer = printers[args.target](headerParser)