808 lines · 28.4 KB · Python
RawDownload
#!/usr/bin/env python3# SPDX-License-Identifier: GPL-2.0-only## Copyright (C) 2018-2019 Netronome Systems, Inc.# Copyright (C) 2021 Isovalent, Inc. # In case user attempts to run with Python 2.from __future__ import print_function import argparseimport reimport sys, os class NoHelperFound(BaseException):    pass class NoSyscallCommandFound(BaseException):    pass class ParsingError(BaseException):    def __init__(self, line='<line not provided>', reader=None):        if reader:            BaseException.__init__(self,                                   'Error at file offset %d, parsing line: %s' %                                   (reader.tell(), line))        else:            BaseException.__init__(self, 'Error parsing line: %s' % line)  class APIElement(object):    """    An object representing the description of an aspect of the eBPF API.    @proto: prototype of the API symbol    @desc: textual description of the symbol    @ret: (optional) description of any associated return value    """    def __init__(self, proto='', desc='', ret=''):        self.proto = proto        self.desc = desc        self.ret = ret  class Helper(APIElement):    """    An object representing the description of an eBPF helper function.    @proto: function prototype of the helper function    @desc: textual description of the helper function    @ret: description of the return value of the helper function    """    def proto_break_down(self):        """        Break down helper function protocol into smaller chunks: return type,        name, distincts arguments.        """        arg_re = re.compile('((\w+ )*?(\w+|...))( (\**)(\w+))?$')        res = {}        proto_re = re.compile('(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$')         capture = proto_re.match(self.proto)        res['ret_type'] = capture.group(1)        res['ret_star'] = capture.group(2)        res['name']     = capture.group(3)        res['args'] = []         args    = capture.group(4).split(', ')        for a in args:            capture = arg_re.match(a)            res['args'].append({                'type' : capture.group(1),                'star' : capture.group(5),                'name' : capture.group(6)            })         return res  class HeaderParser(object):    """    An object used to parse a file in order to extract the documentation of a    list of eBPF helper functions. All the helpers that can be retrieved are    stored as Helper object, in the self.helpers() array.    @filename: name of file to parse, usually include/uapi/linux/bpf.h in the               kernel tree    """    def __init__(self, filename):        self.reader = open(filename, 'r')        self.line = ''        self.helpers = []        self.commands = []        self.desc_unique_helpers = set()        self.define_unique_helpers = []        self.desc_syscalls = []        self.enum_syscalls = []     def parse_element(self):        proto    = self.parse_symbol()        desc     = self.parse_desc(proto)        ret      = self.parse_ret(proto)        return APIElement(proto=proto, desc=desc, ret=ret)     def parse_helper(self):        proto    = self.parse_proto()        desc     = self.parse_desc(proto)        ret      = self.parse_ret(proto)        return Helper(proto=proto, desc=desc, ret=ret)     def parse_symbol(self):        p = re.compile(' \* ?(BPF\w+)$')        capture = p.match(self.line)        if not capture:            raise NoSyscallCommandFound        end_re = re.compile(' \* ?NOTES$')        end = end_re.match(self.line)        if end:            raise NoSyscallCommandFound        self.line = self.reader.readline()        return capture.group(1)     def parse_proto(self):        # Argument can be of shape:        #   - "void"        #   - "type  name"        #   - "type *name"        #   - Same as above, with "const" and/or "struct" in front of type        #   - "..." (undefined number of arguments, for bpf_trace_printk())        # There is at least one term ("void"), and at most five arguments.        p = re.compile(' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$')        capture = p.match(self.line)        if not capture:            raise NoHelperFound        self.line = self.reader.readline()        return capture.group(1)     def parse_desc(self, proto):        p = re.compile(' \* ?(?:\t| {5,8})Description$')        capture = p.match(self.line)        if not capture:            raise Exception("No description section found for " + proto)        # Description can be several lines, some of them possibly empty, and it        # stops when another subsection title is met.        desc = ''        desc_present = False        while True:            self.line = self.reader.readline()            if self.line == ' *\n':                desc += '\n'            else:                p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')                capture = p.match(self.line)                if capture:                    desc_present = True                    desc += capture.group(1) + '\n'                else:                    break         if not desc_present:            raise Exception("No description found for " + proto)        return desc     def parse_ret(self, proto):        p = re.compile(' \* ?(?:\t| {5,8})Return$')        capture = p.match(self.line)        if not capture:            raise Exception("No return section found for " + proto)        # Return value description can be several lines, some of them possibly        # empty, and it stops when another subsection title is met.        ret = ''        ret_present = False        while True:            self.line = self.reader.readline()            if self.line == ' *\n':                ret += '\n'            else:                p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')                capture = p.match(self.line)                if capture:                    ret_present = True                    ret += capture.group(1) + '\n'                else:                    break         if not ret_present:            raise Exception("No return found for " + proto)        return ret     def seek_to(self, target, help_message, discard_lines = 1):        self.reader.seek(0)        offset = self.reader.read().find(target)        if offset == -1:            raise Exception(help_message)        self.reader.seek(offset)        self.reader.readline()        for _ in range(discard_lines):            self.reader.readline()        self.line = self.reader.readline()     def parse_desc_syscall(self):        self.seek_to('* DOC: eBPF Syscall Commands',                     'Could not find start of eBPF syscall descriptions list')        while True:            try:                command = self.parse_element()                self.commands.append(command)                self.desc_syscalls.append(command.proto)             except NoSyscallCommandFound:                break     def parse_enum_syscall(self):        self.seek_to('enum bpf_cmd {',                     'Could not find start of bpf_cmd enum', 0)        # Searches for either one or more BPF\w+ enums        bpf_p = re.compile('\s*(BPF\w+)+')        # Searches for an enum entry assigned to another entry,        # for e.g. BPF_PROG_RUN = BPF_PROG_TEST_RUN, which is        # not documented hence should be skipped in check to        # determine if the right number of syscalls are documented        assign_p = re.compile('\s*(BPF\w+)\s*=\s*(BPF\w+)')        bpf_cmd_str = ''        while True:            capture = assign_p.match(self.line)            if capture:                # Skip line if an enum entry is assigned to another entry                self.line = self.reader.readline()                continue            capture = bpf_p.match(self.line)            if capture:                bpf_cmd_str += self.line            else:                break            self.line = self.reader.readline()        # Find the number of occurences of BPF\w+        self.enum_syscalls = re.findall('(BPF\w+)+', bpf_cmd_str)     def parse_desc_helpers(self):        self.seek_to('* Start of BPF helper function descriptions:',                     'Could not find start of eBPF helper descriptions list')        while True:            try:                helper = self.parse_helper()                self.helpers.append(helper)                proto = helper.proto_break_down()                self.desc_unique_helpers.add(proto['name'])            except NoHelperFound:                break     def parse_define_helpers(self):        # Parse the number of FN(...) in #define __BPF_FUNC_MAPPER to compare        # later with the number of unique function names present in description.        # Note: seek_to(..) discards the first line below the target search text,        # resulting in FN(unspec) being skipped and not added to self.define_unique_helpers.        self.seek_to('#define __BPF_FUNC_MAPPER(FN)',                     'Could not find start of eBPF helper definition list')        # Searches for either one or more FN(\w+) defines or a backslash for newline        p = re.compile('\s*(FN\(\w+\))+|\\\\')        fn_defines_str = ''        while True:            capture = p.match(self.line)            if capture:                fn_defines_str += self.line            else:                break            self.line = self.reader.readline()        # Find the number of occurences of FN(\w+)        self.define_unique_helpers = re.findall('FN\(\w+\)', fn_defines_str)     def run(self):        self.parse_desc_syscall()        self.parse_enum_syscall()        self.parse_desc_helpers()        self.parse_define_helpers()        self.reader.close() ############################################################################### class Printer(object):    """    A generic class for printers. Printers should be created with an array of    Helper objects, and implement a way to print them in the desired fashion.    @parser: A HeaderParser with objects to print to standard output    """    def __init__(self, parser):        self.parser = parser        self.elements = []     def print_header(self):        pass     def print_footer(self):        pass     def print_one(self, helper):        pass     def print_all(self):        self.print_header()        for elem in self.elements:            self.print_one(elem)        self.print_footer()     def elem_number_check(self, desc_unique_elem, define_unique_elem, type, instance):        """        Checks the number of helpers/syscalls documented within the header file        description with those defined as part of enum/macro and raise an        Exception if they don't match.        """        nr_desc_unique_elem = len(desc_unique_elem)        nr_define_unique_elem = len(define_unique_elem)        if nr_desc_unique_elem != nr_define_unique_elem:            exception_msg = '''The number of unique %s in description (%d) doesn\'t match the number of unique %s defined in %s (%d)''' % (type, nr_desc_unique_elem, type, instance, nr_define_unique_elem)            if nr_desc_unique_elem < nr_define_unique_elem:                # Function description is parsed until no helper is found (which can be due to                # misformatting). Hence, only print the first missing/misformatted helper/enum.                exception_msg += '''The description for %s is not present or formatted correctly.''' % (define_unique_elem[nr_desc_unique_elem])            raise Exception(exception_msg) class PrinterRST(Printer):    """    A generic class for printers that print ReStructured Text. Printers should    be created with a HeaderParser object, and implement a way to print API    elements in the desired fashion.    @parser: A HeaderParser with objects to print to standard output    """    def __init__(self, parser):        self.parser = parser     def print_license(self):        license = '''\.. Copyright (C) All BPF authors and contributors from 2014 to present... See git log include/uapi/linux/bpf.h in kernel tree for details... .. SPDX-License-Identifier:  Linux-man-pages-copyleft.. .. Please do not edit this file. It was generated from the documentation.. located in file include/uapi/linux/bpf.h of the Linux kernel sources.. (helpers description), and from scripts/bpf_doc.py in the same.. repository (header and footer).'''        print(license)     def print_elem(self, elem):        if (elem.desc):            print('\tDescription')            # Do not strip all newline characters: formatted code at the end of            # a section must be followed by a blank line.            for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):                print('{}{}'.format('\t\t' if line else '', line))         if (elem.ret):            print('\tReturn')            for line in elem.ret.rstrip().split('\n'):                print('{}{}'.format('\t\t' if line else '', line))         print('') class PrinterHelpersRST(PrinterRST):    """    A printer for dumping collected information about helpers as a ReStructured    Text page compatible with the rst2man program, which can be used to    generate a manual page for the helpers.    @parser: A HeaderParser with Helper objects to print to standard output    """    def __init__(self, parser):        self.elements = parser.helpers        self.elem_number_check(parser.desc_unique_helpers, parser.define_unique_helpers, 'helper', '__BPF_FUNC_MAPPER')     def print_header(self):        header = '''\===========BPF-HELPERS===========-------------------------------------------------------------------------------list of eBPF helper functions------------------------------------------------------------------------------- :Manual section: 7 DESCRIPTION=========== The extended Berkeley Packet Filter (eBPF) subsystem consists in programswritten in a pseudo-assembly language, then attached to one of the severalkernel hooks and run in reaction of specific events. This framework differsfrom the older, "classic" BPF (or "cBPF") in several aspects, one of them beingthe ability to call special functions (or "helpers") from within a program.These functions are restricted to a white-list of helpers defined in thekernel. These helpers are used by eBPF programs to interact with the system, or withthe context in which they work. For instance, they can be used to printdebugging messages, to get the time since the system was booted, to interactwith eBPF maps, or to manipulate network packets. Since there are several eBPFprogram types, and that they do not run in the same context, each program typecan only call a subset of those helpers. Due to eBPF conventions, a helper can not have more than five arguments. Internally, eBPF programs call directly into the compiled helper functionswithout requiring any foreign-function interface. As a result, calling helpersintroduces no overhead, thus offering excellent performance. This document is an attempt to list and document the helpers available to eBPFdevelopers. They are sorted by chronological order (the oldest helpers in thekernel at the top). HELPERS======='''        PrinterRST.print_license(self)        print(header)     def print_footer(self):        footer = '''EXAMPLES======== Example usage for most of the eBPF helpers listed in this manual page areavailable within the Linux kernel sources, at the following locations: * *samples/bpf/** *tools/testing/selftests/bpf/* LICENSE======= eBPF programs can have an associated license, passed along with the bytecodeinstructions to the kernel when the programs are loaded. The format for thatstring is identical to the one in use for kernel modules (Dual licenses, suchas "Dual BSD/GPL", may be used). Some helper functions are only accessible toprograms that are compatible with the GNU Privacy License (GPL). In order to use such helpers, the eBPF program must be loaded with the correctlicense string passed (via **attr**) to the **bpf**\ () system call, and thisgenerally translates into the C source code of the program containing a linesimilar to the following: :: 	char ____license[] __attribute__((section("license"), used)) = "GPL"; IMPLEMENTATION============== This manual page is an effort to document the existing eBPF helper functions.But as of this writing, the BPF sub-system is under heavy development. New eBPFprogram or map types are added, along with new helper functions. Some helpersare occasionally made available for additional program types. So in spite ofthe efforts of the community, this page might not be up-to-date. If you want tocheck by yourself what helper functions exist in your kernel, or what types ofprograms they can support, here are some files among the kernel tree that youmay be interested in: * *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list  of all helper functions, as well as many other BPF definitions including most  of the flags, structs or constants used by the helpers.* *net/core/filter.c* contains the definition of most network-related helper  functions, and the list of program types from which they can be used.* *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related  helpers.* *kernel/bpf/verifier.c* contains the functions used to check that valid types  of eBPF maps are used with a given helper function.* *kernel/bpf/* directory contains other files in which additional helpers are  defined (for cgroups, sockmaps, etc.).* The bpftool utility can be used to probe the availability of helper functions  on the system (as well as supported program and map types, and a number of  other parameters). To do so, run **bpftool feature probe** (see  **bpftool-feature**\ (8) for details). Add the **unprivileged** keyword to  list features available to unprivileged users. Compatibility between helper functions and program types can generally be foundin the files where helper functions are defined. Look for the **structbpf_func_proto** objects and for functions returning them: these functionscontain a list of helpers that a given program type can call. Note that the**default:** label of the **switch ... case** used to filter helpers can callother functions, themselves allowing access to additional helpers. Therequirement for GPL license is also in those **struct bpf_func_proto**. Compatibility between helper functions and map types can be found in the**check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*. Helper functions that invalidate the checks on **data** and **data_end**pointers for network processing are listed in function**bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*. SEE ALSO======== **bpf**\ (2),**bpftool**\ (8),**cgroups**\ (7),**ip**\ (8),**perf_event_open**\ (2),**sendmsg**\ (2),**socket**\ (7),**tc-bpf**\ (8)'''        print(footer)

Lines 1–500 of 808.