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