Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scripts/bpf: Abstract eBPF API target parameter

Abstract out the target parameter so that upcoming commits, more than
just the existing "helpers" target can be called to generate specific
portions of docs from the eBPF UAPI headers.

Signed-off-by: Joe Stringer <joe@cilium.io>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Quentin Monnet <quentin@isovalent.com>
Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Link: https://lore.kernel.org/bpf/20210302171947.2268128-10-joe@cilium.io

authored by

Joe Stringer and committed by
Alexei Starovoitov
923a932c 0cb80454

+69 -33
+1
MAINTAINERS
··· 3223 3223 F: net/sched/act_bpf.c 3224 3224 F: net/sched/cls_bpf.c 3225 3225 F: samples/bpf/ 3226 + F: scripts/bpf_doc.py 3226 3227 F: tools/bpf/ 3227 3228 F: tools/lib/bpf/ 3228 3229 F: tools/testing/selftests/bpf/
+1 -1
include/uapi/linux/bpf.h
··· 1439 1439 * parsed and used to produce a manual page. The workflow is the following, 1440 1440 * and requires the rst2man utility: 1441 1441 * 1442 - * $ ./scripts/bpf_helpers_doc.py \ 1442 + * $ ./scripts/bpf_doc.py \ 1443 1443 * --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst 1444 1444 * $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7 1445 1445 * $ man /tmp/bpf-helpers.7
+63 -28
scripts/bpf_helpers_doc.py scripts/bpf_doc.py
··· 2 2 # SPDX-License-Identifier: GPL-2.0-only 3 3 # 4 4 # Copyright (C) 2018-2019 Netronome Systems, Inc. 5 + # Copyright (C) 2021 Isovalent, Inc. 5 6 6 7 # In case user attempts to run with Python 2. 7 8 from __future__ import print_function ··· 166 165 """ 167 166 A generic class for printers. Printers should be created with an array of 168 167 Helper objects, and implement a way to print them in the desired fashion. 169 - @helpers: array of Helper objects to print to standard output 168 + @parser: A HeaderParser with objects to print to standard output 170 169 """ 171 - def __init__(self, helpers): 172 - self.helpers = helpers 170 + def __init__(self, parser): 171 + self.parser = parser 172 + self.elements = [] 173 173 174 174 def print_header(self): 175 175 pass ··· 183 181 184 182 def print_all(self): 185 183 self.print_header() 186 - for helper in self.helpers: 187 - self.print_one(helper) 184 + for elem in self.elements: 185 + self.print_one(elem) 188 186 self.print_footer() 187 + 189 188 190 189 class PrinterRST(Printer): 191 190 """ 192 - A printer for dumping collected information about helpers as a ReStructured 193 - Text page compatible with the rst2man program, which can be used to 194 - generate a manual page for the helpers. 195 - @helpers: array of Helper objects to print to standard output 191 + A generic class for printers that print ReStructured Text. Printers should 192 + be created with a HeaderParser object, and implement a way to print API 193 + elements in the desired fashion. 194 + @parser: A HeaderParser with objects to print to standard output 196 195 """ 197 - def print_header(self): 198 - header = '''\ 196 + def __init__(self, parser): 197 + self.parser = parser 198 + 199 + def print_license(self): 200 + license = '''\ 199 201 .. Copyright (C) All BPF authors and contributors from 2014 to present. 200 202 .. See git log include/uapi/linux/bpf.h in kernel tree for details. 201 203 .. ··· 227 221 .. 228 222 .. Please do not edit this file. It was generated from the documentation 229 223 .. located in file include/uapi/linux/bpf.h of the Linux kernel sources 230 - .. (helpers description), and from scripts/bpf_helpers_doc.py in the same 224 + .. (helpers description), and from scripts/bpf_doc.py in the same 231 225 .. repository (header and footer). 226 + ''' 227 + print(license) 232 228 229 + def print_elem(self, elem): 230 + if (elem.desc): 231 + print('\tDescription') 232 + # Do not strip all newline characters: formatted code at the end of 233 + # a section must be followed by a blank line. 234 + for line in re.sub('\n$', '', elem.desc, count=1).split('\n'): 235 + print('{}{}'.format('\t\t' if line else '', line)) 236 + 237 + if (elem.ret): 238 + print('\tReturn') 239 + for line in elem.ret.rstrip().split('\n'): 240 + print('{}{}'.format('\t\t' if line else '', line)) 241 + 242 + print('') 243 + 244 + 245 + class PrinterHelpersRST(PrinterRST): 246 + """ 247 + A printer for dumping collected information about helpers as a ReStructured 248 + Text page compatible with the rst2man program, which can be used to 249 + generate a manual page for the helpers. 250 + @parser: A HeaderParser with Helper objects to print to standard output 251 + """ 252 + def __init__(self, parser): 253 + self.elements = parser.helpers 254 + 255 + def print_header(self): 256 + header = '''\ 233 257 =========== 234 258 BPF-HELPERS 235 259 =========== ··· 300 264 HELPERS 301 265 ======= 302 266 ''' 267 + PrinterRST.print_license(self) 303 268 print(header) 304 269 305 270 def print_footer(self): ··· 417 380 418 381 def print_one(self, helper): 419 382 self.print_proto(helper) 383 + self.print_elem(helper) 420 384 421 - if (helper.desc): 422 - print('\tDescription') 423 - # Do not strip all newline characters: formatted code at the end of 424 - # a section must be followed by a blank line. 425 - for line in re.sub('\n$', '', helper.desc, count=1).split('\n'): 426 - print('{}{}'.format('\t\t' if line else '', line)) 427 385 428 - if (helper.ret): 429 - print('\tReturn') 430 - for line in helper.ret.rstrip().split('\n'): 431 - print('{}{}'.format('\t\t' if line else '', line)) 432 386 433 - print('') 434 387 435 388 class PrinterHelpers(Printer): 436 389 """ 437 390 A printer for dumping collected information about helpers as C header to 438 391 be included from BPF program. 439 - @helpers: array of Helper objects to print to standard output 392 + @parser: A HeaderParser with Helper objects to print to standard output 440 393 """ 394 + def __init__(self, parser): 395 + self.elements = parser.helpers 441 396 442 397 type_fwds = [ 443 398 'struct bpf_fib_lookup', ··· 540 511 541 512 def print_header(self): 542 513 header = '''\ 543 - /* This is auto-generated file. See bpf_helpers_doc.py for details. */ 514 + /* This is auto-generated file. See bpf_doc.py for details. */ 544 515 545 516 /* Forward declarations of BPF structs */''' 546 517 ··· 618 589 linuxRoot = os.path.dirname(os.path.dirname(script)) 619 590 bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h') 620 591 592 + printers = { 593 + 'helpers': PrinterHelpersRST, 594 + } 595 + 621 596 argParser = argparse.ArgumentParser(description=""" 622 - Parse eBPF header file and generate documentation for eBPF helper functions. 597 + Parse eBPF header file and generate documentation for the eBPF API. 623 598 The RST-formatted output produced can be turned into a manual page with the 624 599 rst2man utility. 625 600 """) ··· 634 601 default=bpfh) 635 602 else: 636 603 argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h') 604 + argParser.add_argument('target', nargs='?', default='helpers', 605 + choices=printers.keys(), help='eBPF API target') 637 606 args = argParser.parse_args() 638 607 639 608 # Parse file. ··· 644 609 645 610 # Print formatted output to standard output. 646 611 if args.header: 647 - printer = PrinterHelpers(headerParser.helpers) 612 + printer = PrinterHelpers(headerParser) 648 613 else: 649 - printer = PrinterRST(headerParser.helpers) 614 + printer = printers[args.target](headerParser) 650 615 printer.print_all()
+1 -1
tools/bpf/Makefile.helpers
··· 35 35 RST2MAN_DEP := $(shell command -v rst2man 2>/dev/null) 36 36 37 37 $(OUTPUT)$(HELPERS_RST): $(UP2DIR)../../include/uapi/linux/bpf.h 38 - $(QUIET_GEN)$(UP2DIR)../../scripts/bpf_helpers_doc.py --filename $< > $@ 38 + $(QUIET_GEN)$(UP2DIR)../../scripts/bpf_doc.py --filename $< > $@ 39 39 40 40 $(OUTPUT)%.7: $(OUTPUT)%.rst 41 41 ifndef RST2MAN_DEP
+1 -1
tools/include/uapi/linux/bpf.h
··· 729 729 * parsed and used to produce a manual page. The workflow is the following, 730 730 * and requires the rst2man utility: 731 731 * 732 - * $ ./scripts/bpf_helpers_doc.py \ 732 + * $ ./scripts/bpf_doc.py \ 733 733 * --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst 734 734 * $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7 735 735 * $ man /tmp/bpf-helpers.7
+1 -1
tools/lib/bpf/Makefile
··· 158 158 $(Q)$(MAKE) $(build)=libbpf OUTPUT=$(STATIC_OBJDIR) 159 159 160 160 $(BPF_HELPER_DEFS): $(srctree)/tools/include/uapi/linux/bpf.h 161 - $(QUIET_GEN)$(srctree)/scripts/bpf_helpers_doc.py --header \ 161 + $(QUIET_GEN)$(srctree)/scripts/bpf_doc.py --header \ 162 162 --file $(srctree)/tools/include/uapi/linux/bpf.h > $(BPF_HELPER_DEFS) 163 163 164 164 $(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)
+1 -1
tools/perf/MANIFEST
··· 20 20 tools/lib/str_error_r.c 21 21 tools/lib/vsprintf.c 22 22 tools/lib/zalloc.c 23 - scripts/bpf_helpers_doc.py 23 + scripts/bpf_doc.py