scripts/bpf: Set version attribute for bpf-helpers(7) man page
[linux-block.git] / scripts / bpf_doc.py
CommitLineData
3cd046f1 1#!/usr/bin/env python3
56a092c8
QM
2# SPDX-License-Identifier: GPL-2.0-only
3#
748c7c82 4# Copyright (C) 2018-2019 Netronome Systems, Inc.
923a932c 5# Copyright (C) 2021 Isovalent, Inc.
56a092c8
QM
6
7# In case user attempts to run with Python 2.
8from __future__ import print_function
9
10import argparse
11import re
12import sys, os
fd0a38f9
QM
13import subprocess
14
56a092c8
QM
15
16class NoHelperFound(BaseException):
17 pass
18
a67882a2
JS
19class NoSyscallCommandFound(BaseException):
20 pass
21
56a092c8
QM
22class ParsingError(BaseException):
23 def __init__(self, line='<line not provided>', reader=None):
24 if reader:
25 BaseException.__init__(self,
26 'Error at file offset %d, parsing line: %s' %
27 (reader.tell(), line))
28 else:
29 BaseException.__init__(self, 'Error parsing line: %s' % line)
30
a67882a2
JS
31
32class APIElement(object):
56a092c8 33 """
a67882a2
JS
34 An object representing the description of an aspect of the eBPF API.
35 @proto: prototype of the API symbol
36 @desc: textual description of the symbol
37 @ret: (optional) description of any associated return value
56a092c8
QM
38 """
39 def __init__(self, proto='', desc='', ret=''):
40 self.proto = proto
41 self.desc = desc
42 self.ret = ret
43
a67882a2
JS
44
45class Helper(APIElement):
46 """
47 An object representing the description of an eBPF helper function.
48 @proto: function prototype of the helper function
49 @desc: textual description of the helper function
50 @ret: description of the return value of the helper function
51 """
56a092c8
QM
52 def proto_break_down(self):
53 """
54 Break down helper function protocol into smaller chunks: return type,
55 name, distincts arguments.
56 """
748c7c82 57 arg_re = re.compile('((\w+ )*?(\w+|...))( (\**)(\w+))?$')
56a092c8 58 res = {}
6f96674d 59 proto_re = re.compile('(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$')
56a092c8
QM
60
61 capture = proto_re.match(self.proto)
62 res['ret_type'] = capture.group(1)
63 res['ret_star'] = capture.group(2)
64 res['name'] = capture.group(3)
65 res['args'] = []
66
67 args = capture.group(4).split(', ')
68 for a in args:
69 capture = arg_re.match(a)
70 res['args'].append({
71 'type' : capture.group(1),
748c7c82
QM
72 'star' : capture.group(5),
73 'name' : capture.group(6)
56a092c8
QM
74 })
75
76 return res
77
a67882a2 78
56a092c8
QM
79class HeaderParser(object):
80 """
81 An object used to parse a file in order to extract the documentation of a
82 list of eBPF helper functions. All the helpers that can be retrieved are
83 stored as Helper object, in the self.helpers() array.
84 @filename: name of file to parse, usually include/uapi/linux/bpf.h in the
85 kernel tree
86 """
87 def __init__(self, filename):
88 self.reader = open(filename, 'r')
89 self.line = ''
90 self.helpers = []
a67882a2 91 self.commands = []
71a3cdf8
UA
92 self.desc_unique_helpers = set()
93 self.define_unique_helpers = []
0ba3929e
UA
94 self.desc_syscalls = []
95 self.enum_syscalls = []
a67882a2
JS
96
97 def parse_element(self):
98 proto = self.parse_symbol()
f1f3f67f
UA
99 desc = self.parse_desc(proto)
100 ret = self.parse_ret(proto)
a67882a2 101 return APIElement(proto=proto, desc=desc, ret=ret)
56a092c8
QM
102
103 def parse_helper(self):
104 proto = self.parse_proto()
f1f3f67f
UA
105 desc = self.parse_desc(proto)
106 ret = self.parse_ret(proto)
56a092c8
QM
107 return Helper(proto=proto, desc=desc, ret=ret)
108
a67882a2 109 def parse_symbol(self):
0ba3929e 110 p = re.compile(' \* ?(BPF\w+)$')
a67882a2
JS
111 capture = p.match(self.line)
112 if not capture:
113 raise NoSyscallCommandFound
114 end_re = re.compile(' \* ?NOTES$')
115 end = end_re.match(self.line)
116 if end:
117 raise NoSyscallCommandFound
118 self.line = self.reader.readline()
119 return capture.group(1)
120
56a092c8
QM
121 def parse_proto(self):
122 # Argument can be of shape:
123 # - "void"
124 # - "type name"
125 # - "type *name"
126 # - Same as above, with "const" and/or "struct" in front of type
127 # - "..." (undefined number of arguments, for bpf_trace_printk())
128 # There is at least one term ("void"), and at most five arguments.
6f96674d 129 p = re.compile(' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$')
56a092c8
QM
130 capture = p.match(self.line)
131 if not capture:
132 raise NoHelperFound
133 self.line = self.reader.readline()
134 return capture.group(1)
135
f1f3f67f 136 def parse_desc(self, proto):
eeacb716 137 p = re.compile(' \* ?(?:\t| {5,8})Description$')
56a092c8
QM
138 capture = p.match(self.line)
139 if not capture:
f1f3f67f 140 raise Exception("No description section found for " + proto)
56a092c8
QM
141 # Description can be several lines, some of them possibly empty, and it
142 # stops when another subsection title is met.
143 desc = ''
f1f3f67f 144 desc_present = False
56a092c8
QM
145 while True:
146 self.line = self.reader.readline()
147 if self.line == ' *\n':
148 desc += '\n'
149 else:
eeacb716 150 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
56a092c8
QM
151 capture = p.match(self.line)
152 if capture:
f1f3f67f 153 desc_present = True
56a092c8
QM
154 desc += capture.group(1) + '\n'
155 else:
156 break
f1f3f67f
UA
157
158 if not desc_present:
159 raise Exception("No description found for " + proto)
56a092c8
QM
160 return desc
161
f1f3f67f 162 def parse_ret(self, proto):
eeacb716 163 p = re.compile(' \* ?(?:\t| {5,8})Return$')
56a092c8
QM
164 capture = p.match(self.line)
165 if not capture:
f1f3f67f 166 raise Exception("No return section found for " + proto)
56a092c8
QM
167 # Return value description can be several lines, some of them possibly
168 # empty, and it stops when another subsection title is met.
169 ret = ''
f1f3f67f 170 ret_present = False
56a092c8
QM
171 while True:
172 self.line = self.reader.readline()
173 if self.line == ' *\n':
174 ret += '\n'
175 else:
eeacb716 176 p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
56a092c8
QM
177 capture = p.match(self.line)
178 if capture:
f1f3f67f 179 ret_present = True
56a092c8
QM
180 ret += capture.group(1) + '\n'
181 else:
182 break
f1f3f67f
UA
183
184 if not ret_present:
185 raise Exception("No return found for " + proto)
56a092c8
QM
186 return ret
187
0ba3929e 188 def seek_to(self, target, help_message, discard_lines = 1):
a67882a2
JS
189 self.reader.seek(0)
190 offset = self.reader.read().find(target)
56a092c8 191 if offset == -1:
a67882a2 192 raise Exception(help_message)
56a092c8
QM
193 self.reader.seek(offset)
194 self.reader.readline()
0ba3929e
UA
195 for _ in range(discard_lines):
196 self.reader.readline()
56a092c8
QM
197 self.line = self.reader.readline()
198
0ba3929e 199 def parse_desc_syscall(self):
a67882a2
JS
200 self.seek_to('* DOC: eBPF Syscall Commands',
201 'Could not find start of eBPF syscall descriptions list')
202 while True:
203 try:
204 command = self.parse_element()
205 self.commands.append(command)
0ba3929e
UA
206 self.desc_syscalls.append(command.proto)
207
a67882a2
JS
208 except NoSyscallCommandFound:
209 break
210
0ba3929e
UA
211 def parse_enum_syscall(self):
212 self.seek_to('enum bpf_cmd {',
213 'Could not find start of bpf_cmd enum', 0)
214 # Searches for either one or more BPF\w+ enums
215 bpf_p = re.compile('\s*(BPF\w+)+')
216 # Searches for an enum entry assigned to another entry,
217 # for e.g. BPF_PROG_RUN = BPF_PROG_TEST_RUN, which is
218 # not documented hence should be skipped in check to
219 # determine if the right number of syscalls are documented
220 assign_p = re.compile('\s*(BPF\w+)\s*=\s*(BPF\w+)')
221 bpf_cmd_str = ''
222 while True:
223 capture = assign_p.match(self.line)
224 if capture:
225 # Skip line if an enum entry is assigned to another entry
226 self.line = self.reader.readline()
227 continue
228 capture = bpf_p.match(self.line)
229 if capture:
230 bpf_cmd_str += self.line
231 else:
232 break
233 self.line = self.reader.readline()
234 # Find the number of occurences of BPF\w+
235 self.enum_syscalls = re.findall('(BPF\w+)+', bpf_cmd_str)
236
71a3cdf8 237 def parse_desc_helpers(self):
a67882a2
JS
238 self.seek_to('* Start of BPF helper function descriptions:',
239 'Could not find start of eBPF helper descriptions list')
56a092c8
QM
240 while True:
241 try:
242 helper = self.parse_helper()
243 self.helpers.append(helper)
71a3cdf8
UA
244 proto = helper.proto_break_down()
245 self.desc_unique_helpers.add(proto['name'])
56a092c8
QM
246 except NoHelperFound:
247 break
248
71a3cdf8
UA
249 def parse_define_helpers(self):
250 # Parse the number of FN(...) in #define __BPF_FUNC_MAPPER to compare
251 # later with the number of unique function names present in description.
252 # Note: seek_to(..) discards the first line below the target search text,
253 # resulting in FN(unspec) being skipped and not added to self.define_unique_helpers.
254 self.seek_to('#define __BPF_FUNC_MAPPER(FN)',
255 'Could not find start of eBPF helper definition list')
256 # Searches for either one or more FN(\w+) defines or a backslash for newline
257 p = re.compile('\s*(FN\(\w+\))+|\\\\')
258 fn_defines_str = ''
259 while True:
260 capture = p.match(self.line)
261 if capture:
262 fn_defines_str += self.line
263 else:
264 break
265 self.line = self.reader.readline()
266 # Find the number of occurences of FN(\w+)
267 self.define_unique_helpers = re.findall('FN\(\w+\)', fn_defines_str)
268
a67882a2 269 def run(self):
0ba3929e
UA
270 self.parse_desc_syscall()
271 self.parse_enum_syscall()
71a3cdf8
UA
272 self.parse_desc_helpers()
273 self.parse_define_helpers()
56a092c8 274 self.reader.close()
56a092c8
QM
275
276###############################################################################
277
278class Printer(object):
279 """
280 A generic class for printers. Printers should be created with an array of
281 Helper objects, and implement a way to print them in the desired fashion.
923a932c 282 @parser: A HeaderParser with objects to print to standard output
56a092c8 283 """
923a932c
JS
284 def __init__(self, parser):
285 self.parser = parser
286 self.elements = []
56a092c8
QM
287
288 def print_header(self):
289 pass
290
291 def print_footer(self):
292 pass
293
294 def print_one(self, helper):
295 pass
296
297 def print_all(self):
298 self.print_header()
923a932c
JS
299 for elem in self.elements:
300 self.print_one(elem)
56a092c8
QM
301 self.print_footer()
302
0ba3929e
UA
303 def elem_number_check(self, desc_unique_elem, define_unique_elem, type, instance):
304 """
305 Checks the number of helpers/syscalls documented within the header file
306 description with those defined as part of enum/macro and raise an
307 Exception if they don't match.
308 """
309 nr_desc_unique_elem = len(desc_unique_elem)
310 nr_define_unique_elem = len(define_unique_elem)
311 if nr_desc_unique_elem != nr_define_unique_elem:
312 exception_msg = '''
313The number of unique %s in description (%d) doesn\'t match the number of unique %s defined in %s (%d)
314''' % (type, nr_desc_unique_elem, type, instance, nr_define_unique_elem)
315 if nr_desc_unique_elem < nr_define_unique_elem:
316 # Function description is parsed until no helper is found (which can be due to
317 # misformatting). Hence, only print the first missing/misformatted helper/enum.
318 exception_msg += '''
319The description for %s is not present or formatted correctly.
320''' % (define_unique_elem[nr_desc_unique_elem])
321 raise Exception(exception_msg)
923a932c 322
56a092c8
QM
323class PrinterRST(Printer):
324 """
923a932c
JS
325 A generic class for printers that print ReStructured Text. Printers should
326 be created with a HeaderParser object, and implement a way to print API
327 elements in the desired fashion.
328 @parser: A HeaderParser with objects to print to standard output
56a092c8 329 """
923a932c
JS
330 def __init__(self, parser):
331 self.parser = parser
332
333 def print_license(self):
334 license = '''\
56a092c8
QM
335.. Copyright (C) All BPF authors and contributors from 2014 to present.
336.. See git log include/uapi/linux/bpf.h in kernel tree for details.
337..
5cb62b75 338.. SPDX-License-Identifier: Linux-man-pages-copyleft
56a092c8
QM
339..
340.. Please do not edit this file. It was generated from the documentation
341.. located in file include/uapi/linux/bpf.h of the Linux kernel sources
923a932c 342.. (helpers description), and from scripts/bpf_doc.py in the same
56a092c8 343.. repository (header and footer).
923a932c
JS
344'''
345 print(license)
346
347 def print_elem(self, elem):
348 if (elem.desc):
349 print('\tDescription')
350 # Do not strip all newline characters: formatted code at the end of
351 # a section must be followed by a blank line.
352 for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):
353 print('{}{}'.format('\t\t' if line else '', line))
354
355 if (elem.ret):
356 print('\tReturn')
357 for line in elem.ret.rstrip().split('\n'):
358 print('{}{}'.format('\t\t' if line else '', line))
359
360 print('')
56a092c8 361
fd0a38f9
QM
362 def get_kernel_version(self):
363 try:
364 version = subprocess.run(['git', 'describe'], cwd=linuxRoot,
365 capture_output=True, check=True)
366 version = version.stdout.decode().rstrip()
367 except:
368 try:
369 version = subprocess.run(['make', 'kernelversion'], cwd=linuxRoot,
370 capture_output=True, check=True)
371 version = version.stdout.decode().rstrip()
372 except:
373 return 'Linux'
374 return 'Linux {version}'.format(version=version)
375
923a932c
JS
376class PrinterHelpersRST(PrinterRST):
377 """
378 A printer for dumping collected information about helpers as a ReStructured
379 Text page compatible with the rst2man program, which can be used to
380 generate a manual page for the helpers.
381 @parser: A HeaderParser with Helper objects to print to standard output
382 """
383 def __init__(self, parser):
384 self.elements = parser.helpers
0ba3929e 385 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
923a932c
JS
386
387 def print_header(self):
388 header = '''\
56a092c8
QM
389===========
390BPF-HELPERS
391===========
392-------------------------------------------------------------------------------
393list of eBPF helper functions
394-------------------------------------------------------------------------------
395
396:Manual section: 7
fd0a38f9 397:Version: {version}
56a092c8
QM
398
399DESCRIPTION
400===========
401
402The extended Berkeley Packet Filter (eBPF) subsystem consists in programs
403written in a pseudo-assembly language, then attached to one of the several
404kernel hooks and run in reaction of specific events. This framework differs
405from the older, "classic" BPF (or "cBPF") in several aspects, one of them being
406the ability to call special functions (or "helpers") from within a program.
407These functions are restricted to a white-list of helpers defined in the
408kernel.
409
410These helpers are used by eBPF programs to interact with the system, or with
411the context in which they work. For instance, they can be used to print
412debugging messages, to get the time since the system was booted, to interact
413with eBPF maps, or to manipulate network packets. Since there are several eBPF
414program types, and that they do not run in the same context, each program type
415can only call a subset of those helpers.
416
417Due to eBPF conventions, a helper can not have more than five arguments.
418
419Internally, eBPF programs call directly into the compiled helper functions
420without requiring any foreign-function interface. As a result, calling helpers
421introduces no overhead, thus offering excellent performance.
422
423This document is an attempt to list and document the helpers available to eBPF
424developers. They are sorted by chronological order (the oldest helpers in the
425kernel at the top).
426
427HELPERS
428=======
429'''
fd0a38f9
QM
430 kernelVersion = self.get_kernel_version()
431
923a932c 432 PrinterRST.print_license(self)
fd0a38f9 433 print(header.format(version=kernelVersion))
56a092c8
QM
434
435 def print_footer(self):
436 footer = '''
437EXAMPLES
438========
439
440Example usage for most of the eBPF helpers listed in this manual page are
441available within the Linux kernel sources, at the following locations:
442
443* *samples/bpf/*
444* *tools/testing/selftests/bpf/*
445
446LICENSE
447=======
448
449eBPF programs can have an associated license, passed along with the bytecode
450instructions to the kernel when the programs are loaded. The format for that
451string is identical to the one in use for kernel modules (Dual licenses, such
452as "Dual BSD/GPL", may be used). Some helper functions are only accessible to
453programs that are compatible with the GNU Privacy License (GPL).
454
455In order to use such helpers, the eBPF program must be loaded with the correct
456license string passed (via **attr**) to the **bpf**\ () system call, and this
457generally translates into the C source code of the program containing a line
458similar to the following:
459
460::
461
462 char ____license[] __attribute__((section("license"), used)) = "GPL";
463
464IMPLEMENTATION
465==============
466
467This manual page is an effort to document the existing eBPF helper functions.
468But as of this writing, the BPF sub-system is under heavy development. New eBPF
469program or map types are added, along with new helper functions. Some helpers
470are occasionally made available for additional program types. So in spite of
471the efforts of the community, this page might not be up-to-date. If you want to
472check by yourself what helper functions exist in your kernel, or what types of
473programs they can support, here are some files among the kernel tree that you
474may be interested in:
475
476* *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list
477 of all helper functions, as well as many other BPF definitions including most
478 of the flags, structs or constants used by the helpers.
479* *net/core/filter.c* contains the definition of most network-related helper
480 functions, and the list of program types from which they can be used.
481* *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related
482 helpers.
483* *kernel/bpf/verifier.c* contains the functions used to check that valid types
484 of eBPF maps are used with a given helper function.
485* *kernel/bpf/* directory contains other files in which additional helpers are
486 defined (for cgroups, sockmaps, etc.).
ab8d7809
QM
487* The bpftool utility can be used to probe the availability of helper functions
488 on the system (as well as supported program and map types, and a number of
489 other parameters). To do so, run **bpftool feature probe** (see
490 **bpftool-feature**\ (8) for details). Add the **unprivileged** keyword to
491 list features available to unprivileged users.
56a092c8
QM
492
493Compatibility between helper functions and program types can generally be found
494in the files where helper functions are defined. Look for the **struct
495bpf_func_proto** objects and for functions returning them: these functions
496contain a list of helpers that a given program type can call. Note that the
497**default:** label of the **switch ... case** used to filter helpers can call
498other functions, themselves allowing access to additional helpers. The
499requirement for GPL license is also in those **struct bpf_func_proto**.
500
501Compatibility between helper functions and map types can be found in the
502**check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*.
503
504Helper functions that invalidate the checks on **data** and **data_end**
505pointers for network processing are listed in function
506**bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*.
507
508SEE ALSO
509========
510
511**bpf**\ (2),
ab8d7809 512**bpftool**\ (8),
56a092c8
QM
513**cgroups**\ (7),
514**ip**\ (8),
515**perf_event_open**\ (2),
516**sendmsg**\ (2),
517**socket**\ (7),
518**tc-bpf**\ (8)'''
519 print(footer)
520
521 def print_proto(self, helper):
522 """
523 Format function protocol with bold and italics markers. This makes RST
524 file less readable, but gives nice results in the manual page.
525 """
526 proto = helper.proto_break_down()
527
528 print('**%s %s%s(' % (proto['ret_type'],
529 proto['ret_star'].replace('*', '\\*'),
530 proto['name']),
531 end='')
532
533 comma = ''
534 for a in proto['args']:
535 one_arg = '{}{}'.format(comma, a['type'])
536 if a['name']:
537 if a['star']:
538 one_arg += ' {}**\ '.format(a['star'].replace('*', '\\*'))
539 else:
540 one_arg += '** '
541 one_arg += '*{}*\\ **'.format(a['name'])
542 comma = ', '
543 print(one_arg, end='')
544
545 print(')**')
546
547 def print_one(self, helper):
548 self.print_proto(helper)
923a932c 549 self.print_elem(helper)
56a092c8 550
56a092c8 551
a67882a2
JS
552class PrinterSyscallRST(PrinterRST):
553 """
554 A printer for dumping collected information about the syscall API as a
555 ReStructured Text page compatible with the rst2man program, which can be
556 used to generate a manual page for the syscall.
557 @parser: A HeaderParser with APIElement objects to print to standard
558 output
559 """
560 def __init__(self, parser):
561 self.elements = parser.commands
0ba3929e 562 self.elem_number_check(parser.desc_syscalls, parser.enum_syscalls, 'syscall', 'bpf_cmd')
a67882a2
JS
563
564 def print_header(self):
565 header = '''\
566===
567bpf
568===
569-------------------------------------------------------------------------------
570Perform a command on an extended BPF object
571-------------------------------------------------------------------------------
572
573:Manual section: 2
574
575COMMANDS
576========
577'''
578 PrinterRST.print_license(self)
579 print(header)
580
581 def print_one(self, command):
582 print('**%s**' % (command.proto))
583 self.print_elem(command)
56a092c8 584
56a092c8 585
7a387bed
AN
586class PrinterHelpers(Printer):
587 """
588 A printer for dumping collected information about helpers as C header to
589 be included from BPF program.
923a932c 590 @parser: A HeaderParser with Helper objects to print to standard output
7a387bed 591 """
923a932c
JS
592 def __init__(self, parser):
593 self.elements = parser.helpers
0ba3929e 594 self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')
7a387bed
AN
595
596 type_fwds = [
597 'struct bpf_fib_lookup',
e9ddbb77 598 'struct bpf_sk_lookup',
7a387bed
AN
599 'struct bpf_perf_event_data',
600 'struct bpf_perf_event_value',
5996a587 601 'struct bpf_pidns_info',
821f5c90 602 'struct bpf_redir_neigh',
7a387bed
AN
603 'struct bpf_sock',
604 'struct bpf_sock_addr',
605 'struct bpf_sock_ops',
606 'struct bpf_sock_tuple',
607 'struct bpf_spin_lock',
608 'struct bpf_sysctl',
609 'struct bpf_tcp_sock',
610 'struct bpf_tunnel_key',
611 'struct bpf_xfrm_state',
3f6719c7 612 'struct linux_binprm',
7a387bed
AN
613 'struct pt_regs',
614 'struct sk_reuseport_md',
615 'struct sockaddr',
616 'struct tcphdr',
492e639f 617 'struct seq_file',
af7ec138 618 'struct tcp6_sock',
478cfbdf
YS
619 'struct tcp_sock',
620 'struct tcp_timewait_sock',
621 'struct tcp_request_sock',
0d4fad3e 622 'struct udp6_sock',
9eeb3aa3 623 'struct unix_sock',
fa28dcb8 624 'struct task_struct',
7a387bed
AN
625
626 'struct __sk_buff',
627 'struct sk_msg_md',
e0b68fb1 628 'struct xdp_md',
6e22ab9d 629 'struct path',
c4d0bfb4 630 'struct btf_ptr',
27672f0d 631 'struct inode',
4f19cab7
FR
632 'struct socket',
633 'struct file',
b00628b1 634 'struct bpf_timer',
3bc253c2 635 'struct mptcp_sock',
97e03f52 636 'struct bpf_dynptr',
33bf9885
MM
637 'struct iphdr',
638 'struct ipv6hdr',
7a387bed
AN
639 ]
640 known_types = {
641 '...',
642 'void',
643 'const void',
644 'char',
645 'const char',
646 'int',
647 'long',
648 'unsigned long',
649
650 '__be16',
651 '__be32',
652 '__wsum',
653
654 'struct bpf_fib_lookup',
655 'struct bpf_perf_event_data',
656 'struct bpf_perf_event_value',
b4490c5c 657 'struct bpf_pidns_info',
ba452c9e 658 'struct bpf_redir_neigh',
e9ddbb77 659 'struct bpf_sk_lookup',
7a387bed
AN
660 'struct bpf_sock',
661 'struct bpf_sock_addr',
662 'struct bpf_sock_ops',
663 'struct bpf_sock_tuple',
664 'struct bpf_spin_lock',
665 'struct bpf_sysctl',
666 'struct bpf_tcp_sock',
667 'struct bpf_tunnel_key',
668 'struct bpf_xfrm_state',
3f6719c7 669 'struct linux_binprm',
7a387bed
AN
670 'struct pt_regs',
671 'struct sk_reuseport_md',
672 'struct sockaddr',
673 'struct tcphdr',
492e639f 674 'struct seq_file',
af7ec138 675 'struct tcp6_sock',
478cfbdf
YS
676 'struct tcp_sock',
677 'struct tcp_timewait_sock',
678 'struct tcp_request_sock',
0d4fad3e 679 'struct udp6_sock',
9eeb3aa3 680 'struct unix_sock',
fa28dcb8 681 'struct task_struct',
6e22ab9d 682 'struct path',
c4d0bfb4 683 'struct btf_ptr',
27672f0d 684 'struct inode',
4f19cab7
FR
685 'struct socket',
686 'struct file',
b00628b1 687 'struct bpf_timer',
3bc253c2 688 'struct mptcp_sock',
97e03f52 689 'struct bpf_dynptr',
33bf9885
MM
690 'struct iphdr',
691 'struct ipv6hdr',
7a387bed
AN
692 }
693 mapped_types = {
694 'u8': '__u8',
695 'u16': '__u16',
696 'u32': '__u32',
697 'u64': '__u64',
698 's8': '__s8',
699 's16': '__s16',
700 's32': '__s32',
701 's64': '__s64',
702 'size_t': 'unsigned long',
703 'struct bpf_map': 'void',
704 'struct sk_buff': 'struct __sk_buff',
705 'const struct sk_buff': 'const struct __sk_buff',
706 'struct sk_msg_buff': 'struct sk_msg_md',
707 'struct xdp_buff': 'struct xdp_md',
708 }
e9ddbb77
JS
709 # Helpers overloaded for different context types.
710 overloaded_helpers = [
711 'bpf_get_socket_cookie',
712 'bpf_sk_assign',
713 ]
7a387bed
AN
714
715 def print_header(self):
716 header = '''\
923a932c 717/* This is auto-generated file. See bpf_doc.py for details. */
7a387bed
AN
718
719/* Forward declarations of BPF structs */'''
720
721 print(header)
722 for fwd in self.type_fwds:
723 print('%s;' % fwd)
724 print('')
725
726 def print_footer(self):
727 footer = ''
728 print(footer)
729
730 def map_type(self, t):
731 if t in self.known_types:
732 return t
733 if t in self.mapped_types:
734 return self.mapped_types[t]
ab81e203
JS
735 print("Unrecognized type '%s', please add it to known types!" % t,
736 file=sys.stderr)
7a387bed
AN
737 sys.exit(1)
738
739 seen_helpers = set()
740
741 def print_one(self, helper):
742 proto = helper.proto_break_down()
743
744 if proto['name'] in self.seen_helpers:
745 return
746 self.seen_helpers.add(proto['name'])
747
748 print('/*')
749 print(" * %s" % proto['name'])
750 print(" *")
751 if (helper.desc):
752 # Do not strip all newline characters: formatted code at the end of
753 # a section must be followed by a blank line.
754 for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
755 print(' *{}{}'.format(' \t' if line else '', line))
756
757 if (helper.ret):
758 print(' *')
759 print(' * Returns')
760 for line in helper.ret.rstrip().split('\n'):
761 print(' *{}{}'.format(' \t' if line else '', line))
762
763 print(' */')
764 print('static %s %s(*%s)(' % (self.map_type(proto['ret_type']),
765 proto['ret_star'], proto['name']), end='')
766 comma = ''
767 for i, a in enumerate(proto['args']):
768 t = a['type']
769 n = a['name']
e9ddbb77 770 if proto['name'] in self.overloaded_helpers and i == 0:
7a387bed
AN
771 t = 'void'
772 n = 'ctx'
773 one_arg = '{}{}'.format(comma, self.map_type(t))
774 if n:
775 if a['star']:
776 one_arg += ' {}'.format(a['star'])
777 else:
778 one_arg += ' '
779 one_arg += '{}'.format(n)
780 comma = ', '
781 print(one_arg, end='')
782
783 print(') = (void *) %d;' % len(self.seen_helpers))
784 print('')
785
56a092c8
QM
786###############################################################################
787
788# If script is launched from scripts/ from kernel tree and can access
789# ../include/uapi/linux/bpf.h, use it as a default name for the file to parse,
790# otherwise the --filename argument will be required from the command line.
791script = os.path.abspath(sys.argv[0])
792linuxRoot = os.path.dirname(os.path.dirname(script))
793bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
794
923a932c
JS
795printers = {
796 'helpers': PrinterHelpersRST,
a67882a2 797 'syscall': PrinterSyscallRST,
923a932c
JS
798}
799
56a092c8 800argParser = argparse.ArgumentParser(description="""
923a932c 801Parse eBPF header file and generate documentation for the eBPF API.
56a092c8
QM
802The RST-formatted output produced can be turned into a manual page with the
803rst2man utility.
804""")
7a387bed
AN
805argParser.add_argument('--header', action='store_true',
806 help='generate C header file')
56a092c8
QM
807if (os.path.isfile(bpfh)):
808 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h',
809 default=bpfh)
810else:
811 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
923a932c
JS
812argParser.add_argument('target', nargs='?', default='helpers',
813 choices=printers.keys(), help='eBPF API target')
56a092c8
QM
814args = argParser.parse_args()
815
816# Parse file.
817headerParser = HeaderParser(args.filename)
818headerParser.run()
819
820# Print formatted output to standard output.
7a387bed 821if args.header:
a67882a2
JS
822 if args.target != 'helpers':
823 raise NotImplementedError('Only helpers header generation is supported')
923a932c 824 printer = PrinterHelpers(headerParser)
7a387bed 825else:
923a932c 826 printer = printers[args.target](headerParser)
56a092c8 827printer.print_all()