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
='', attrs
=[]):
47 class Helper(APIElement
):
49 An object representing the description of an eBPF helper function.
50 @proto: function prototype of the helper function
51 @desc: textual description of the helper function
52 @ret: description of the return value of the helper function
54 def __init__(self
, *args
, **kwargs
):
55 super().__init
__(*args
, **kwargs
)
58 def proto_break_down(self
):
60 Break down helper function protocol into smaller chunks: return type,
61 name, distincts arguments.
63 arg_re
= re
.compile(r
'((\w+ )*?(\w+|...))( (\**)(\w+))?$')
65 proto_re
= re
.compile(r
'(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$')
67 capture
= proto_re
.match(self
.proto
)
68 res
['ret_type'] = capture
.group(1)
69 res
['ret_star'] = capture
.group(2)
70 res
['name'] = capture
.group(3)
73 args
= capture
.group(4).split(', ')
75 capture
= arg_re
.match(a
)
77 'type' : capture
.group(1),
78 'star' : capture
.group(5),
79 'name' : capture
.group(6)
86 '__bpf_fastcall': 'bpf_fastcall'
90 class HeaderParser(object):
92 An object used to parse a file in order to extract the documentation of a
93 list of eBPF helper functions. All the helpers that can be retrieved are
94 stored as Helper object, in the self.helpers() array.
95 @filename: name of file to parse, usually include/uapi/linux/bpf.h in the
98 def __init__(self
, filename
):
99 self
.reader
= open(filename
, 'r')
103 self
.desc_unique_helpers
= set()
104 self
.define_unique_helpers
= []
105 self
.helper_enum_vals
= {}
106 self
.helper_enum_pos
= {}
107 self
.desc_syscalls
= []
108 self
.enum_syscalls
= []
110 def parse_element(self
):
111 proto
= self
.parse_symbol()
112 desc
= self
.parse_desc(proto
)
113 ret
= self
.parse_ret(proto
)
114 return APIElement(proto
=proto
, desc
=desc
, ret
=ret
)
116 def parse_helper(self
):
117 proto
= self
.parse_proto()
118 desc
= self
.parse_desc(proto
)
119 ret
= self
.parse_ret(proto
)
120 attrs
= self
.parse_attrs(proto
)
121 return Helper(proto
=proto
, desc
=desc
, ret
=ret
, attrs
=attrs
)
123 def parse_symbol(self
):
124 p
= re
.compile(r
' \* ?(BPF\w+)$')
125 capture
= p
.match(self
.line
)
127 raise NoSyscallCommandFound
128 end_re
= re
.compile(r
' \* ?NOTES$')
129 end
= end_re
.match(self
.line
)
131 raise NoSyscallCommandFound
132 self
.line
= self
.reader
.readline()
133 return capture
.group(1)
135 def parse_proto(self
):
136 # Argument can be of shape:
140 # - Same as above, with "const" and/or "struct" in front of type
141 # - "..." (undefined number of arguments, for bpf_trace_printk())
142 # There is at least one term ("void"), and at most five arguments.
143 p
= re
.compile(r
' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$')
144 capture
= p
.match(self
.line
)
147 self
.line
= self
.reader
.readline()
148 return capture
.group(1)
150 def parse_desc(self
, proto
):
151 p
= re
.compile(r
' \* ?(?:\t| {5,8})Description$')
152 capture
= p
.match(self
.line
)
154 raise Exception("No description section found for " + proto
)
155 # Description can be several lines, some of them possibly empty, and it
156 # stops when another subsection title is met.
160 self
.line
= self
.reader
.readline()
161 if self
.line
== ' *\n':
164 p
= re
.compile(r
' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
165 capture
= p
.match(self
.line
)
168 desc
+= capture
.group(1) + '\n'
173 raise Exception("No description found for " + proto
)
176 def parse_ret(self
, proto
):
177 p
= re
.compile(r
' \* ?(?:\t| {5,8})Return$')
178 capture
= p
.match(self
.line
)
180 raise Exception("No return section found for " + proto
)
181 # Return value description can be several lines, some of them possibly
182 # empty, and it stops when another subsection title is met.
186 self
.line
= self
.reader
.readline()
187 if self
.line
== ' *\n':
190 p
= re
.compile(r
' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
191 capture
= p
.match(self
.line
)
194 ret
+= capture
.group(1) + '\n'
199 raise Exception("No return found for " + proto
)
202 def parse_attrs(self
, proto
):
203 p
= re
.compile(r
' \* ?(?:\t| {5,8})Attributes$')
204 capture
= p
.match(self
.line
)
207 # Expect a single line with mnemonics for attributes separated by spaces
208 self
.line
= self
.reader
.readline()
209 p
= re
.compile(r
' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
210 capture
= p
.match(self
.line
)
212 raise Exception("Incomplete 'Attributes' section for " + proto
)
213 attrs
= capture
.group(1).split(' ')
215 if attr
not in ATTRS
:
216 raise Exception("Unexpected attribute '" + attr
+ "' specified for " + proto
)
217 self
.line
= self
.reader
.readline()
218 if self
.line
!= ' *\n':
219 raise Exception("Expecting empty line after 'Attributes' section for " + proto
)
220 # Prepare a line for next self.parse_* to consume
221 self
.line
= self
.reader
.readline()
224 def seek_to(self
, target
, help_message
, discard_lines
= 1):
226 offset
= self
.reader
.read().find(target
)
228 raise Exception(help_message
)
229 self
.reader
.seek(offset
)
230 self
.reader
.readline()
231 for _
in range(discard_lines
):
232 self
.reader
.readline()
233 self
.line
= self
.reader
.readline()
235 def parse_desc_syscall(self
):
236 self
.seek_to('* DOC: eBPF Syscall Commands',
237 'Could not find start of eBPF syscall descriptions list')
240 command
= self
.parse_element()
241 self
.commands
.append(command
)
242 self
.desc_syscalls
.append(command
.proto
)
244 except NoSyscallCommandFound
:
247 def parse_enum_syscall(self
):
248 self
.seek_to('enum bpf_cmd {',
249 'Could not find start of bpf_cmd enum', 0)
250 # Searches for either one or more BPF\w+ enums
251 bpf_p
= re
.compile(r
'\s*(BPF\w+)+')
252 # Searches for an enum entry assigned to another entry,
253 # for e.g. BPF_PROG_RUN = BPF_PROG_TEST_RUN, which is
254 # not documented hence should be skipped in check to
255 # determine if the right number of syscalls are documented
256 assign_p
= re
.compile(r
'\s*(BPF\w+)\s*=\s*(BPF\w+)')
259 capture
= assign_p
.match(self
.line
)
261 # Skip line if an enum entry is assigned to another entry
262 self
.line
= self
.reader
.readline()
264 capture
= bpf_p
.match(self
.line
)
266 bpf_cmd_str
+= self
.line
269 self
.line
= self
.reader
.readline()
270 # Find the number of occurences of BPF\w+
271 self
.enum_syscalls
= re
.findall(r
'(BPF\w+)+', bpf_cmd_str
)
273 def parse_desc_helpers(self
):
274 self
.seek_to(helpersDocStart
,
275 'Could not find start of eBPF helper descriptions list')
278 helper
= self
.parse_helper()
279 self
.helpers
.append(helper
)
280 proto
= helper
.proto_break_down()
281 self
.desc_unique_helpers
.add(proto
['name'])
282 except NoHelperFound
:
285 def parse_define_helpers(self
):
286 # Parse FN(...) in #define ___BPF_FUNC_MAPPER to compare later with the
287 # number of unique function names present in description and use the
288 # correct enumeration value.
289 # Note: seek_to(..) discards the first line below the target search text,
290 # resulting in FN(unspec, 0, ##ctx) being skipped and not added to
291 # self.define_unique_helpers.
292 self
.seek_to('#define ___BPF_FUNC_MAPPER(FN, ctx...)',
293 'Could not find start of eBPF helper definition list')
294 # Searches for one FN(\w+) define or a backslash for newline
295 p
= re
.compile(r
'\s*FN\((\w+), (\d+), ##ctx\)|\\\\')
299 capture
= p
.match(self
.line
)
301 fn_defines_str
+= self
.line
302 helper_name
= capture
.expand(r
'bpf_\1')
303 self
.helper_enum_vals
[helper_name
] = int(capture
.group(2))
304 self
.helper_enum_pos
[helper_name
] = i
308 self
.line
= self
.reader
.readline()
309 # Find the number of occurences of FN(\w+)
310 self
.define_unique_helpers
= re
.findall(r
'FN\(\w+, \d+, ##ctx\)', fn_defines_str
)
312 def validate_helpers(self
):
315 seen_enum_vals
= set()
317 for helper
in self
.helpers
:
318 proto
= helper
.proto_break_down()
321 enum_val
= self
.helper_enum_vals
[name
]
322 enum_pos
= self
.helper_enum_pos
[name
]
324 raise Exception("Helper %s is missing from enum bpf_func_id" % name
)
326 if name
in seen_helpers
:
327 if last_helper
!= name
:
328 raise Exception("Helper %s has multiple descriptions which are not grouped together" % name
)
331 # Enforce current practice of having the descriptions ordered
334 raise Exception("Helper %s (ID %d) comment order (#%d) must be aligned with its position (#%d) in enum bpf_func_id" % (name
, enum_val
, i
+ 1, enum_pos
+ 1))
335 if enum_val
in seen_enum_vals
:
336 raise Exception("Helper %s has duplicated value %d" % (name
, enum_val
))
338 seen_helpers
.add(name
)
340 seen_enum_vals
.add(enum_val
)
342 helper
.enum_val
= enum_val
346 self
.parse_desc_syscall()
347 self
.parse_enum_syscall()
348 self
.parse_desc_helpers()
349 self
.parse_define_helpers()
350 self
.validate_helpers()
353 ###############################################################################
355 class Printer(object):
357 A generic class for printers. Printers should be created with an array of
358 Helper objects, and implement a way to print them in the desired fashion.
359 @parser: A HeaderParser with objects to print to standard output
361 def __init__(self
, parser
):
365 def print_header(self
):
368 def print_footer(self
):
371 def print_one(self
, helper
):
376 for elem
in self
.elements
:
380 def elem_number_check(self
, desc_unique_elem
, define_unique_elem
, type, instance
):
382 Checks the number of helpers/syscalls documented within the header file
383 description with those defined as part of enum/macro and raise an
384 Exception if they don't match.
386 nr_desc_unique_elem
= len(desc_unique_elem
)
387 nr_define_unique_elem
= len(define_unique_elem
)
388 if nr_desc_unique_elem
!= nr_define_unique_elem
:
390 The number of unique %s in description (%d) doesn\'t match the number of unique %s defined in %s (%d)
391 ''' % (type, nr_desc_unique_elem
, type, instance
, nr_define_unique_elem
)
392 if nr_desc_unique_elem
< nr_define_unique_elem
:
393 # Function description is parsed until no helper is found (which can be due to
394 # misformatting). Hence, only print the first missing/misformatted helper/enum.
396 The description for %s is not present or formatted correctly.
397 ''' % (define_unique_elem
[nr_desc_unique_elem
])
398 raise Exception(exception_msg
)
400 class PrinterRST(Printer
):
402 A generic class for printers that print ReStructured Text. Printers should
403 be created with a HeaderParser object, and implement a way to print API
404 elements in the desired fashion.
405 @parser: A HeaderParser with objects to print to standard output
407 def __init__(self
, parser
):
410 def print_license(self
):
412 .. Copyright (C) All BPF authors and contributors from 2014 to present.
413 .. See git log include/uapi/linux/bpf.h in kernel tree for details.
415 .. SPDX-License-Identifier: Linux-man-pages-copyleft
417 .. Please do not edit this file. It was generated from the documentation
418 .. located in file include/uapi/linux/bpf.h of the Linux kernel sources
419 .. (helpers description), and from scripts/bpf_doc.py in the same
420 .. repository (header and footer).
424 def print_elem(self
, elem
):
426 print('\tDescription')
427 # Do not strip all newline characters: formatted code at the end of
428 # a section must be followed by a blank line.
429 for line
in re
.sub('\n$', '', elem
.desc
, count
=1).split('\n'):
430 print('{}{}'.format('\t\t' if line
else '', line
))
434 for line
in elem
.ret
.rstrip().split('\n'):
435 print('{}{}'.format('\t\t' if line
else '', line
))
439 def get_kernel_version(self
):
441 version
= subprocess
.run(['git', 'describe'], cwd
=linuxRoot
,
442 capture_output
=True, check
=True)
443 version
= version
.stdout
.decode().rstrip()
446 version
= subprocess
.run(['make', '-s', '--no-print-directory', 'kernelversion'],
447 cwd
=linuxRoot
, capture_output
=True, check
=True)
448 version
= version
.stdout
.decode().rstrip()
451 return 'Linux {version}'.format(version
=version
)
453 def get_last_doc_update(self
, delimiter
):
455 cmd
= ['git', 'log', '-1', '--pretty=format:%cs', '--no-patch',
457 '/{}/,/\\*\\//:include/uapi/linux/bpf.h'.format(delimiter
)]
458 date
= subprocess
.run(cmd
, cwd
=linuxRoot
,
459 capture_output
=True, check
=True)
460 return date
.stdout
.decode().rstrip()
464 class PrinterHelpersRST(PrinterRST
):
466 A printer for dumping collected information about helpers as a ReStructured
467 Text page compatible with the rst2man program, which can be used to
468 generate a manual page for the helpers.
469 @parser: A HeaderParser with Helper objects to print to standard output
471 def __init__(self
, parser
):
472 self
.elements
= parser
.helpers
473 self
.elem_number_check(parser
.desc_unique_helpers
, parser
.define_unique_helpers
, 'helper', '___BPF_FUNC_MAPPER')
475 def print_header(self
):
480 -------------------------------------------------------------------------------
481 list of eBPF helper functions
482 -------------------------------------------------------------------------------
491 The extended Berkeley Packet Filter (eBPF) subsystem consists in programs
492 written in a pseudo-assembly language, then attached to one of the several
493 kernel hooks and run in reaction of specific events. This framework differs
494 from the older, "classic" BPF (or "cBPF") in several aspects, one of them being
495 the ability to call special functions (or "helpers") from within a program.
496 These functions are restricted to a white-list of helpers defined in the
499 These helpers are used by eBPF programs to interact with the system, or with
500 the context in which they work. For instance, they can be used to print
501 debugging messages, to get the time since the system was booted, to interact
502 with eBPF maps, or to manipulate network packets. Since there are several eBPF
503 program types, and that they do not run in the same context, each program type
504 can only call a subset of those helpers.
506 Due to eBPF conventions, a helper can not have more than five arguments.
508 Internally, eBPF programs call directly into the compiled helper functions
509 without requiring any foreign-function interface. As a result, calling helpers
510 introduces no overhead, thus offering excellent performance.
512 This document is an attempt to list and document the helpers available to eBPF
513 developers. They are sorted by chronological order (the oldest helpers in the
519 kernelVersion
= self
.get_kernel_version()
520 lastUpdate
= self
.get_last_doc_update(helpersDocStart
)
522 PrinterRST
.print_license(self
)
523 print(header
.format(version
=kernelVersion
,
524 date_field
= ':Date: ' if lastUpdate
else '',
527 def print_footer(self
):
532 Example usage for most of the eBPF helpers listed in this manual page are
533 available within the Linux kernel sources, at the following locations:
536 * *tools/testing/selftests/bpf/*
541 eBPF programs can have an associated license, passed along with the bytecode
542 instructions to the kernel when the programs are loaded. The format for that
543 string is identical to the one in use for kernel modules (Dual licenses, such
544 as "Dual BSD/GPL", may be used). Some helper functions are only accessible to
545 programs that are compatible with the GNU General Public License (GNU GPL).
547 In order to use such helpers, the eBPF program must be loaded with the correct
548 license string passed (via **attr**) to the **bpf**\\ () system call, and this
549 generally translates into the C source code of the program containing a line
550 similar to the following:
554 char ____license[] __attribute__((section("license"), used)) = "GPL";
559 This manual page is an effort to document the existing eBPF helper functions.
560 But as of this writing, the BPF sub-system is under heavy development. New eBPF
561 program or map types are added, along with new helper functions. Some helpers
562 are occasionally made available for additional program types. So in spite of
563 the efforts of the community, this page might not be up-to-date. If you want to
564 check by yourself what helper functions exist in your kernel, or what types of
565 programs they can support, here are some files among the kernel tree that you
566 may be interested in:
568 * *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list
569 of all helper functions, as well as many other BPF definitions including most
570 of the flags, structs or constants used by the helpers.
571 * *net/core/filter.c* contains the definition of most network-related helper
572 functions, and the list of program types from which they can be used.
573 * *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related
575 * *kernel/bpf/verifier.c* contains the functions used to check that valid types
576 of eBPF maps are used with a given helper function.
577 * *kernel/bpf/* directory contains other files in which additional helpers are
578 defined (for cgroups, sockmaps, etc.).
579 * The bpftool utility can be used to probe the availability of helper functions
580 on the system (as well as supported program and map types, and a number of
581 other parameters). To do so, run **bpftool feature probe** (see
582 **bpftool-feature**\\ (8) for details). Add the **unprivileged** keyword to
583 list features available to unprivileged users.
585 Compatibility between helper functions and program types can generally be found
586 in the files where helper functions are defined. Look for the **struct
587 bpf_func_proto** objects and for functions returning them: these functions
588 contain a list of helpers that a given program type can call. Note that the
589 **default:** label of the **switch ... case** used to filter helpers can call
590 other functions, themselves allowing access to additional helpers. The
591 requirement for GPL license is also in those **struct bpf_func_proto**.
593 Compatibility between helper functions and map types can be found in the
594 **check_map_func_compatibility**\\ () function in file *kernel/bpf/verifier.c*.
596 Helper functions that invalidate the checks on **data** and **data_end**
597 pointers for network processing are listed in function
598 **bpf_helper_changes_pkt_data**\\ () in file *net/core/filter.c*.
607 **perf_event_open**\\ (2),
613 def print_proto(self
, helper
):
615 Format function protocol with bold and italics markers. This makes RST
616 file less readable, but gives nice results in the manual page.
618 proto
= helper
.proto_break_down()
620 print('**%s %s%s(' % (proto
['ret_type'],
621 proto
['ret_star'].replace('*', '\\*'),
626 for a
in proto
['args']:
627 one_arg
= '{}{}'.format(comma
, a
['type'])
630 one_arg
+= ' {}**\\ '.format(a
['star'].replace('*', '\\*'))
633 one_arg
+= '*{}*\\ **'.format(a
['name'])
635 print(one_arg
, end
='')
639 def print_one(self
, helper
):
640 self
.print_proto(helper
)
641 self
.print_elem(helper
)
644 class PrinterSyscallRST(PrinterRST
):
646 A printer for dumping collected information about the syscall API as a
647 ReStructured Text page compatible with the rst2man program, which can be
648 used to generate a manual page for the syscall.
649 @parser: A HeaderParser with APIElement objects to print to standard
652 def __init__(self
, parser
):
653 self
.elements
= parser
.commands
654 self
.elem_number_check(parser
.desc_syscalls
, parser
.enum_syscalls
, 'syscall', 'bpf_cmd')
656 def print_header(self
):
661 -------------------------------------------------------------------------------
662 Perform a command on an extended BPF object
663 -------------------------------------------------------------------------------
670 PrinterRST
.print_license(self
)
673 def print_one(self
, command
):
674 print('**%s**' % (command
.proto
))
675 self
.print_elem(command
)
678 class PrinterHelpers(Printer
):
680 A printer for dumping collected information about helpers as C header to
681 be included from BPF program.
682 @parser: A HeaderParser with Helper objects to print to standard output
684 def __init__(self
, parser
):
685 self
.elements
= parser
.helpers
686 self
.elem_number_check(parser
.desc_unique_helpers
, parser
.define_unique_helpers
, 'helper', '___BPF_FUNC_MAPPER')
689 'struct bpf_fib_lookup',
690 'struct bpf_sk_lookup',
691 'struct bpf_perf_event_data',
692 'struct bpf_perf_event_value',
693 'struct bpf_pidns_info',
694 'struct bpf_redir_neigh',
696 'struct bpf_sock_addr',
697 'struct bpf_sock_ops',
698 'struct bpf_sock_tuple',
699 'struct bpf_spin_lock',
701 'struct bpf_tcp_sock',
702 'struct bpf_tunnel_key',
703 'struct bpf_xfrm_state',
704 'struct linux_binprm',
706 'struct sk_reuseport_md',
712 'struct tcp_timewait_sock',
713 'struct tcp_request_sock',
716 'struct task_struct',
747 'struct bpf_fib_lookup',
748 'struct bpf_perf_event_data',
749 'struct bpf_perf_event_value',
750 'struct bpf_pidns_info',
751 'struct bpf_redir_neigh',
752 'struct bpf_sk_lookup',
754 'struct bpf_sock_addr',
755 'struct bpf_sock_ops',
756 'struct bpf_sock_tuple',
757 'struct bpf_spin_lock',
759 'struct bpf_tcp_sock',
760 'struct bpf_tunnel_key',
761 'struct bpf_xfrm_state',
762 'struct linux_binprm',
764 'struct sk_reuseport_md',
770 'struct tcp_timewait_sock',
771 'struct tcp_request_sock',
774 'struct task_struct',
784 'const struct bpf_dynptr',
797 'size_t': 'unsigned long',
798 'struct bpf_map': 'void',
799 'struct sk_buff': 'struct __sk_buff',
800 'const struct sk_buff': 'const struct __sk_buff',
801 'struct sk_msg_buff': 'struct sk_msg_md',
802 'struct xdp_buff': 'struct xdp_md',
804 # Helpers overloaded for different context types.
805 overloaded_helpers
= [
806 'bpf_get_socket_cookie',
810 def print_header(self
):
812 /* This is auto-generated file. See bpf_doc.py for details. */
814 /* Forward declarations of BPF structs */'''
817 for fwd
in self
.type_fwds
:
822 for helper
in self
.elements
:
823 for attr
in helper
.attrs
:
825 for attr
in sorted(used_attrs
):
826 print('#ifndef %s' % attr
)
827 print('#if __has_attribute(%s)' % ATTRS
[attr
])
828 print('#define %s __attribute__((%s))' % (attr
, ATTRS
[attr
]))
830 print('#define %s' % attr
)
836 def print_footer(self
):
840 def map_type(self
, t
):
841 if t
in self
.known_types
:
843 if t
in self
.mapped_types
:
844 return self
.mapped_types
[t
]
845 print("Unrecognized type '%s', please add it to known types!" % t
,
851 def print_one(self
, helper
):
852 proto
= helper
.proto_break_down()
854 if proto
['name'] in self
.seen_helpers
:
856 self
.seen_helpers
.add(proto
['name'])
859 print(" * %s" % proto
['name'])
862 # Do not strip all newline characters: formatted code at the end of
863 # a section must be followed by a blank line.
864 for line
in re
.sub('\n$', '', helper
.desc
, count
=1).split('\n'):
865 print(' *{}{}'.format(' \t' if line
else '', line
))
870 for line
in helper
.ret
.rstrip().split('\n'):
871 print(' *{}{}'.format(' \t' if line
else '', line
))
874 print('static ', end
='')
876 print('%s ' % (" ".join(helper
.attrs
)), end
='')
877 print('%s %s(* const %s)(' % (self
.map_type(proto
['ret_type']),
878 proto
['ret_star'], proto
['name']), end
='')
880 for i
, a
in enumerate(proto
['args']):
883 if proto
['name'] in self
.overloaded_helpers
and i
== 0:
886 one_arg
= '{}{}'.format(comma
, self
.map_type(t
))
889 one_arg
+= ' {}'.format(a
['star'])
892 one_arg
+= '{}'.format(n
)
894 print(one_arg
, end
='')
896 print(') = (void *) %d;' % helper
.enum_val
)
899 ###############################################################################
901 # If script is launched from scripts/ from kernel tree and can access
902 # ../include/uapi/linux/bpf.h, use it as a default name for the file to parse,
903 # otherwise the --filename argument will be required from the command line.
904 script
= os
.path
.abspath(sys
.argv
[0])
905 linuxRoot
= os
.path
.dirname(os
.path
.dirname(script
))
906 bpfh
= os
.path
.join(linuxRoot
, 'include/uapi/linux/bpf.h')
909 'helpers': PrinterHelpersRST
,
910 'syscall': PrinterSyscallRST
,
913 argParser
= argparse
.ArgumentParser(description
="""
914 Parse eBPF header file and generate documentation for the eBPF API.
915 The RST-formatted output produced can be turned into a manual page with the
918 argParser
.add_argument('--header', action
='store_true',
919 help='generate C header file')
920 if (os
.path
.isfile(bpfh
)):
921 argParser
.add_argument('--filename', help='path to include/uapi/linux/bpf.h',
924 argParser
.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
925 argParser
.add_argument('target', nargs
='?', default
='helpers',
926 choices
=printers
.keys(), help='eBPF API target')
927 args
= argParser
.parse_args()
930 headerParser
= HeaderParser(args
.filename
)
933 # Print formatted output to standard output.
935 if args
.target
!= 'helpers':
936 raise NotImplementedError('Only helpers header generation is supported')
937 printer
= PrinterHelpers(headerParser
)
939 printer
= printers
[args
.target
](headerParser
)