Skip to content

Commit 923a932

Browse files
joestringerAlexei Starovoitov
authored andcommitted
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
1 parent 0cb8045 commit 923a932

7 files changed

Lines changed: 69 additions & 33 deletions

File tree

‎MAINTAINERS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3223,6 +3223,7 @@ F: net/core/filter.c
32233223
F: net/sched/act_bpf.c
32243224
F: net/sched/cls_bpf.c
32253225
F: samples/bpf/
3226+
F: scripts/bpf_doc.py
32263227
F: tools/bpf/
32273228
F: tools/lib/bpf/
32283229
F: tools/testing/selftests/bpf/

‎include/uapi/linux/bpf.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ union bpf_attr {
14391439
* parsed and used to produce a manual page. The workflow is the following,
14401440
* and requires the rst2man utility:
14411441
*
1442-
* $ ./scripts/bpf_helpers_doc.py \
1442+
* $ ./scripts/bpf_doc.py \
14431443
* --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst
14441444
* $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7
14451445
* $ man /tmp/bpf-helpers.7
Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: GPL-2.0-only
33
#
44
# Copyright (C) 2018-2019 Netronome Systems, Inc.
5+
# Copyright (C) 2021 Isovalent, Inc.
56

67
# In case user attempts to run with Python 2.
78
from __future__ import print_function
@@ -165,10 +166,11 @@ class Printer(object):
165166
"""
166167
A generic class for printers. Printers should be created with an array of
167168
Helper objects, and implement a way to print them in the desired fashion.
168-
@helpers: array of Helper objects to print to standard output
169+
@parser: A HeaderParser with objects to print to standard output
169170
"""
170-
def __init__(self, helpers):
171-
self.helpers = helpers
171+
def __init__(self, parser):
172+
self.parser = parser
173+
self.elements = []
172174

173175
def print_header(self):
174176
pass
@@ -181,19 +183,23 @@ def print_one(self, helper):
181183

182184
def print_all(self):
183185
self.print_header()
184-
for helper in self.helpers:
185-
self.print_one(helper)
186+
for elem in self.elements:
187+
self.print_one(elem)
186188
self.print_footer()
187189

190+
188191
class PrinterRST(Printer):
189192
"""
190-
A printer for dumping collected information about helpers as a ReStructured
191-
Text page compatible with the rst2man program, which can be used to
192-
generate a manual page for the helpers.
193-
@helpers: array of Helper objects to print to standard output
193+
A generic class for printers that print ReStructured Text. Printers should
194+
be created with a HeaderParser object, and implement a way to print API
195+
elements in the desired fashion.
196+
@parser: A HeaderParser with objects to print to standard output
194197
"""
195-
def print_header(self):
196-
header = '''\
198+
def __init__(self, parser):
199+
self.parser = parser
200+
201+
def print_license(self):
202+
license = '''\
197203
.. Copyright (C) All BPF authors and contributors from 2014 to present.
198204
.. See git log include/uapi/linux/bpf.h in kernel tree for details.
199205
..
@@ -221,9 +227,39 @@ def print_header(self):
221227
..
222228
.. Please do not edit this file. It was generated from the documentation
223229
.. located in file include/uapi/linux/bpf.h of the Linux kernel sources
224-
.. (helpers description), and from scripts/bpf_helpers_doc.py in the same
230+
.. (helpers description), and from scripts/bpf_doc.py in the same
225231
.. repository (header and footer).
232+
'''
233+
print(license)
234+
235+
def print_elem(self, elem):
236+
if (elem.desc):
237+
print('\tDescription')
238+
# Do not strip all newline characters: formatted code at the end of
239+
# a section must be followed by a blank line.
240+
for line in re.sub('\n$', '', elem.desc, count=1).split('\n'):
241+
print('{}{}'.format('\t\t' if line else '', line))
242+
243+
if (elem.ret):
244+
print('\tReturn')
245+
for line in elem.ret.rstrip().split('\n'):
246+
print('{}{}'.format('\t\t' if line else '', line))
247+
248+
print('')
226249

250+
251+
class PrinterHelpersRST(PrinterRST):
252+
"""
253+
A printer for dumping collected information about helpers as a ReStructured
254+
Text page compatible with the rst2man program, which can be used to
255+
generate a manual page for the helpers.
256+
@parser: A HeaderParser with Helper objects to print to standard output
257+
"""
258+
def __init__(self, parser):
259+
self.elements = parser.helpers
260+
261+
def print_header(self):
262+
header = '''\
227263
===========
228264
BPF-HELPERS
229265
===========
@@ -264,6 +300,7 @@ def print_header(self):
264300
HELPERS
265301
=======
266302
'''
303+
PrinterRST.print_license(self)
267304
print(header)
268305

269306
def print_footer(self):
@@ -380,27 +417,19 @@ def print_proto(self, helper):
380417

381418
def print_one(self, helper):
382419
self.print_proto(helper)
420+
self.print_elem(helper)
383421

384-
if (helper.desc):
385-
print('\tDescription')
386-
# Do not strip all newline characters: formatted code at the end of
387-
# a section must be followed by a blank line.
388-
for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
389-
print('{}{}'.format('\t\t' if line else '', line))
390422

391-
if (helper.ret):
392-
print('\tReturn')
393-
for line in helper.ret.rstrip().split('\n'):
394-
print('{}{}'.format('\t\t' if line else '', line))
395423

396-
print('')
397424

398425
class PrinterHelpers(Printer):
399426
"""
400427
A printer for dumping collected information about helpers as C header to
401428
be included from BPF program.
402-
@helpers: array of Helper objects to print to standard output
429+
@parser: A HeaderParser with Helper objects to print to standard output
403430
"""
431+
def __init__(self, parser):
432+
self.elements = parser.helpers
404433

405434
type_fwds = [
406435
'struct bpf_fib_lookup',
@@ -511,7 +540,7 @@ class PrinterHelpers(Printer):
511540

512541
def print_header(self):
513542
header = '''\
514-
/* This is auto-generated file. See bpf_helpers_doc.py for details. */
543+
/* This is auto-generated file. See bpf_doc.py for details. */
515544
516545
/* Forward declarations of BPF structs */'''
517546

@@ -589,8 +618,12 @@ def print_one(self, helper):
589618
linuxRoot = os.path.dirname(os.path.dirname(script))
590619
bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
591620

621+
printers = {
622+
'helpers': PrinterHelpersRST,
623+
}
624+
592625
argParser = argparse.ArgumentParser(description="""
593-
Parse eBPF header file and generate documentation for eBPF helper functions.
626+
Parse eBPF header file and generate documentation for the eBPF API.
594627
The RST-formatted output produced can be turned into a manual page with the
595628
rst2man utility.
596629
""")
@@ -601,6 +634,8 @@ def print_one(self, helper):
601634
default=bpfh)
602635
else:
603636
argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
637+
argParser.add_argument('target', nargs='?', default='helpers',
638+
choices=printers.keys(), help='eBPF API target')
604639
args = argParser.parse_args()
605640

606641
# Parse file.
@@ -609,7 +644,7 @@ def print_one(self, helper):
609644

610645
# Print formatted output to standard output.
611646
if args.header:
612-
printer = PrinterHelpers(headerParser.helpers)
647+
printer = PrinterHelpers(headerParser)
613648
else:
614-
printer = PrinterRST(headerParser.helpers)
649+
printer = printers[args.target](headerParser)
615650
printer.print_all()

‎tools/bpf/Makefile.helpers‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ man7: $(DOC_MAN7)
3535
RST2MAN_DEP := $(shell command -v rst2man 2>/dev/null)
3636

3737
$(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 $< > $@
3939

4040
$(OUTPUT)%.7: $(OUTPUT)%.rst
4141
ifndef RST2MAN_DEP

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ union bpf_attr {
729729
* parsed and used to produce a manual page. The workflow is the following,
730730
* and requires the rst2man utility:
731731
*
732-
* $ ./scripts/bpf_helpers_doc.py \
732+
* $ ./scripts/bpf_doc.py \
733733
* --filename include/uapi/linux/bpf.h > /tmp/bpf-helpers.rst
734734
* $ rst2man /tmp/bpf-helpers.rst > /tmp/bpf-helpers.7
735735
* $ man /tmp/bpf-helpers.7

‎tools/lib/bpf/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ $(BPF_IN_STATIC): force $(BPF_HELPER_DEFS)
158158
$(Q)$(MAKE) $(build)=libbpf OUTPUT=$(STATIC_OBJDIR)
159159

160160
$(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 \
162162
--file $(srctree)/tools/include/uapi/linux/bpf.h > $(BPF_HELPER_DEFS)
163163

164164
$(OUTPUT)libbpf.so: $(OUTPUT)libbpf.so.$(LIBBPF_VERSION)

‎tools/perf/MANIFEST‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ tools/lib/bitmap.c
2020
tools/lib/str_error_r.c
2121
tools/lib/vsprintf.c
2222
tools/lib/zalloc.c
23-
scripts/bpf_helpers_doc.py
23+
scripts/bpf_doc.py

0 commit comments

Comments
 (0)