bpf_helpers_doc.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #!/usr/bin/python3
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. # Copyright (C) 2018 Netronome Systems, Inc.
  5. # In case user attempts to run with Python 2.
  6. from __future__ import print_function
  7. import argparse
  8. import re
  9. import sys, os
  10. class NoHelperFound(BaseException):
  11. pass
  12. class ParsingError(BaseException):
  13. def __init__(self, line='<line not provided>', reader=None):
  14. if reader:
  15. BaseException.__init__(self,
  16. 'Error at file offset %d, parsing line: %s' %
  17. (reader.tell(), line))
  18. else:
  19. BaseException.__init__(self, 'Error parsing line: %s' % line)
  20. class Helper(object):
  21. """
  22. An object representing the description of an eBPF helper function.
  23. @proto: function prototype of the helper function
  24. @desc: textual description of the helper function
  25. @ret: description of the return value of the helper function
  26. """
  27. def __init__(self, proto='', desc='', ret=''):
  28. self.proto = proto
  29. self.desc = desc
  30. self.ret = ret
  31. def proto_break_down(self):
  32. """
  33. Break down helper function protocol into smaller chunks: return type,
  34. name, distincts arguments.
  35. """
  36. arg_re = re.compile('((const )?(struct )?(\w+|...))( (\**)(\w+))?$')
  37. res = {}
  38. proto_re = re.compile('(.+) (\**)(\w+)\(((([^,]+)(, )?){1,5})\)$')
  39. capture = proto_re.match(self.proto)
  40. res['ret_type'] = capture.group(1)
  41. res['ret_star'] = capture.group(2)
  42. res['name'] = capture.group(3)
  43. res['args'] = []
  44. args = capture.group(4).split(', ')
  45. for a in args:
  46. capture = arg_re.match(a)
  47. res['args'].append({
  48. 'type' : capture.group(1),
  49. 'star' : capture.group(6),
  50. 'name' : capture.group(7)
  51. })
  52. return res
  53. class HeaderParser(object):
  54. """
  55. An object used to parse a file in order to extract the documentation of a
  56. list of eBPF helper functions. All the helpers that can be retrieved are
  57. stored as Helper object, in the self.helpers() array.
  58. @filename: name of file to parse, usually include/uapi/linux/bpf.h in the
  59. kernel tree
  60. """
  61. def __init__(self, filename):
  62. self.reader = open(filename, 'r')
  63. self.line = ''
  64. self.helpers = []
  65. def parse_helper(self):
  66. proto = self.parse_proto()
  67. desc = self.parse_desc()
  68. ret = self.parse_ret()
  69. return Helper(proto=proto, desc=desc, ret=ret)
  70. def parse_proto(self):
  71. # Argument can be of shape:
  72. # - "void"
  73. # - "type name"
  74. # - "type *name"
  75. # - Same as above, with "const" and/or "struct" in front of type
  76. # - "..." (undefined number of arguments, for bpf_trace_printk())
  77. # There is at least one term ("void"), and at most five arguments.
  78. p = re.compile(' \* ?((.+) \**\w+\((((const )?(struct )?(\w+|\.\.\.)( \**\w+)?)(, )?){1,5}\))$')
  79. capture = p.match(self.line)
  80. if not capture:
  81. raise NoHelperFound
  82. self.line = self.reader.readline()
  83. return capture.group(1)
  84. def parse_desc(self):
  85. p = re.compile(' \* ?(?:\t| {5,8})Description$')
  86. capture = p.match(self.line)
  87. if not capture:
  88. # Helper can have empty description and we might be parsing another
  89. # attribute: return but do not consume.
  90. return ''
  91. # Description can be several lines, some of them possibly empty, and it
  92. # stops when another subsection title is met.
  93. desc = ''
  94. while True:
  95. self.line = self.reader.readline()
  96. if self.line == ' *\n':
  97. desc += '\n'
  98. else:
  99. p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
  100. capture = p.match(self.line)
  101. if capture:
  102. desc += capture.group(1) + '\n'
  103. else:
  104. break
  105. return desc
  106. def parse_ret(self):
  107. p = re.compile(' \* ?(?:\t| {5,8})Return$')
  108. capture = p.match(self.line)
  109. if not capture:
  110. # Helper can have empty retval and we might be parsing another
  111. # attribute: return but do not consume.
  112. return ''
  113. # Return value description can be several lines, some of them possibly
  114. # empty, and it stops when another subsection title is met.
  115. ret = ''
  116. while True:
  117. self.line = self.reader.readline()
  118. if self.line == ' *\n':
  119. ret += '\n'
  120. else:
  121. p = re.compile(' \* ?(?:\t| {5,8})(?:\t| {8})(.*)')
  122. capture = p.match(self.line)
  123. if capture:
  124. ret += capture.group(1) + '\n'
  125. else:
  126. break
  127. return ret
  128. def run(self):
  129. # Advance to start of helper function descriptions.
  130. offset = self.reader.read().find('* Start of BPF helper function descriptions:')
  131. if offset == -1:
  132. raise Exception('Could not find start of eBPF helper descriptions list')
  133. self.reader.seek(offset)
  134. self.reader.readline()
  135. self.reader.readline()
  136. self.line = self.reader.readline()
  137. while True:
  138. try:
  139. helper = self.parse_helper()
  140. self.helpers.append(helper)
  141. except NoHelperFound:
  142. break
  143. self.reader.close()
  144. print('Parsed description of %d helper function(s)' % len(self.helpers),
  145. file=sys.stderr)
  146. ###############################################################################
  147. class Printer(object):
  148. """
  149. A generic class for printers. Printers should be created with an array of
  150. Helper objects, and implement a way to print them in the desired fashion.
  151. @helpers: array of Helper objects to print to standard output
  152. """
  153. def __init__(self, helpers):
  154. self.helpers = helpers
  155. def print_header(self):
  156. pass
  157. def print_footer(self):
  158. pass
  159. def print_one(self, helper):
  160. pass
  161. def print_all(self):
  162. self.print_header()
  163. for helper in self.helpers:
  164. self.print_one(helper)
  165. self.print_footer()
  166. class PrinterRST(Printer):
  167. """
  168. A printer for dumping collected information about helpers as a ReStructured
  169. Text page compatible with the rst2man program, which can be used to
  170. generate a manual page for the helpers.
  171. @helpers: array of Helper objects to print to standard output
  172. """
  173. def print_header(self):
  174. header = '''\
  175. .. Copyright (C) All BPF authors and contributors from 2014 to present.
  176. .. See git log include/uapi/linux/bpf.h in kernel tree for details.
  177. ..
  178. .. %%%LICENSE_START(VERBATIM)
  179. .. Permission is granted to make and distribute verbatim copies of this
  180. .. manual provided the copyright notice and this permission notice are
  181. .. preserved on all copies.
  182. ..
  183. .. Permission is granted to copy and distribute modified versions of this
  184. .. manual under the conditions for verbatim copying, provided that the
  185. .. entire resulting derived work is distributed under the terms of a
  186. .. permission notice identical to this one.
  187. ..
  188. .. Since the Linux kernel and libraries are constantly changing, this
  189. .. manual page may be incorrect or out-of-date. The author(s) assume no
  190. .. responsibility for errors or omissions, or for damages resulting from
  191. .. the use of the information contained herein. The author(s) may not
  192. .. have taken the same level of care in the production of this manual,
  193. .. which is licensed free of charge, as they might when working
  194. .. professionally.
  195. ..
  196. .. Formatted or processed versions of this manual, if unaccompanied by
  197. .. the source, must acknowledge the copyright and authors of this work.
  198. .. %%%LICENSE_END
  199. ..
  200. .. Please do not edit this file. It was generated from the documentation
  201. .. located in file include/uapi/linux/bpf.h of the Linux kernel sources
  202. .. (helpers description), and from scripts/bpf_helpers_doc.py in the same
  203. .. repository (header and footer).
  204. ===========
  205. BPF-HELPERS
  206. ===========
  207. -------------------------------------------------------------------------------
  208. list of eBPF helper functions
  209. -------------------------------------------------------------------------------
  210. :Manual section: 7
  211. DESCRIPTION
  212. ===========
  213. The extended Berkeley Packet Filter (eBPF) subsystem consists in programs
  214. written in a pseudo-assembly language, then attached to one of the several
  215. kernel hooks and run in reaction of specific events. This framework differs
  216. from the older, "classic" BPF (or "cBPF") in several aspects, one of them being
  217. the ability to call special functions (or "helpers") from within a program.
  218. These functions are restricted to a white-list of helpers defined in the
  219. kernel.
  220. These helpers are used by eBPF programs to interact with the system, or with
  221. the context in which they work. For instance, they can be used to print
  222. debugging messages, to get the time since the system was booted, to interact
  223. with eBPF maps, or to manipulate network packets. Since there are several eBPF
  224. program types, and that they do not run in the same context, each program type
  225. can only call a subset of those helpers.
  226. Due to eBPF conventions, a helper can not have more than five arguments.
  227. Internally, eBPF programs call directly into the compiled helper functions
  228. without requiring any foreign-function interface. As a result, calling helpers
  229. introduces no overhead, thus offering excellent performance.
  230. This document is an attempt to list and document the helpers available to eBPF
  231. developers. They are sorted by chronological order (the oldest helpers in the
  232. kernel at the top).
  233. HELPERS
  234. =======
  235. '''
  236. print(header)
  237. def print_footer(self):
  238. footer = '''
  239. EXAMPLES
  240. ========
  241. Example usage for most of the eBPF helpers listed in this manual page are
  242. available within the Linux kernel sources, at the following locations:
  243. * *samples/bpf/*
  244. * *tools/testing/selftests/bpf/*
  245. LICENSE
  246. =======
  247. eBPF programs can have an associated license, passed along with the bytecode
  248. instructions to the kernel when the programs are loaded. The format for that
  249. string is identical to the one in use for kernel modules (Dual licenses, such
  250. as "Dual BSD/GPL", may be used). Some helper functions are only accessible to
  251. programs that are compatible with the GNU Privacy License (GPL).
  252. In order to use such helpers, the eBPF program must be loaded with the correct
  253. license string passed (via **attr**) to the **bpf**\ () system call, and this
  254. generally translates into the C source code of the program containing a line
  255. similar to the following:
  256. ::
  257. char ____license[] __attribute__((section("license"), used)) = "GPL";
  258. IMPLEMENTATION
  259. ==============
  260. This manual page is an effort to document the existing eBPF helper functions.
  261. But as of this writing, the BPF sub-system is under heavy development. New eBPF
  262. program or map types are added, along with new helper functions. Some helpers
  263. are occasionally made available for additional program types. So in spite of
  264. the efforts of the community, this page might not be up-to-date. If you want to
  265. check by yourself what helper functions exist in your kernel, or what types of
  266. programs they can support, here are some files among the kernel tree that you
  267. may be interested in:
  268. * *include/uapi/linux/bpf.h* is the main BPF header. It contains the full list
  269. of all helper functions, as well as many other BPF definitions including most
  270. of the flags, structs or constants used by the helpers.
  271. * *net/core/filter.c* contains the definition of most network-related helper
  272. functions, and the list of program types from which they can be used.
  273. * *kernel/trace/bpf_trace.c* is the equivalent for most tracing program-related
  274. helpers.
  275. * *kernel/bpf/verifier.c* contains the functions used to check that valid types
  276. of eBPF maps are used with a given helper function.
  277. * *kernel/bpf/* directory contains other files in which additional helpers are
  278. defined (for cgroups, sockmaps, etc.).
  279. Compatibility between helper functions and program types can generally be found
  280. in the files where helper functions are defined. Look for the **struct
  281. bpf_func_proto** objects and for functions returning them: these functions
  282. contain a list of helpers that a given program type can call. Note that the
  283. **default:** label of the **switch ... case** used to filter helpers can call
  284. other functions, themselves allowing access to additional helpers. The
  285. requirement for GPL license is also in those **struct bpf_func_proto**.
  286. Compatibility between helper functions and map types can be found in the
  287. **check_map_func_compatibility**\ () function in file *kernel/bpf/verifier.c*.
  288. Helper functions that invalidate the checks on **data** and **data_end**
  289. pointers for network processing are listed in function
  290. **bpf_helper_changes_pkt_data**\ () in file *net/core/filter.c*.
  291. SEE ALSO
  292. ========
  293. **bpf**\ (2),
  294. **cgroups**\ (7),
  295. **ip**\ (8),
  296. **perf_event_open**\ (2),
  297. **sendmsg**\ (2),
  298. **socket**\ (7),
  299. **tc-bpf**\ (8)'''
  300. print(footer)
  301. def print_proto(self, helper):
  302. """
  303. Format function protocol with bold and italics markers. This makes RST
  304. file less readable, but gives nice results in the manual page.
  305. """
  306. proto = helper.proto_break_down()
  307. print('**%s %s%s(' % (proto['ret_type'],
  308. proto['ret_star'].replace('*', '\\*'),
  309. proto['name']),
  310. end='')
  311. comma = ''
  312. for a in proto['args']:
  313. one_arg = '{}{}'.format(comma, a['type'])
  314. if a['name']:
  315. if a['star']:
  316. one_arg += ' {}**\ '.format(a['star'].replace('*', '\\*'))
  317. else:
  318. one_arg += '** '
  319. one_arg += '*{}*\\ **'.format(a['name'])
  320. comma = ', '
  321. print(one_arg, end='')
  322. print(')**')
  323. def print_one(self, helper):
  324. self.print_proto(helper)
  325. if (helper.desc):
  326. print('\tDescription')
  327. # Do not strip all newline characters: formatted code at the end of
  328. # a section must be followed by a blank line.
  329. for line in re.sub('\n$', '', helper.desc, count=1).split('\n'):
  330. print('{}{}'.format('\t\t' if line else '', line))
  331. if (helper.ret):
  332. print('\tReturn')
  333. for line in helper.ret.rstrip().split('\n'):
  334. print('{}{}'.format('\t\t' if line else '', line))
  335. print('')
  336. ###############################################################################
  337. # If script is launched from scripts/ from kernel tree and can access
  338. # ../include/uapi/linux/bpf.h, use it as a default name for the file to parse,
  339. # otherwise the --filename argument will be required from the command line.
  340. script = os.path.abspath(sys.argv[0])
  341. linuxRoot = os.path.dirname(os.path.dirname(script))
  342. bpfh = os.path.join(linuxRoot, 'include/uapi/linux/bpf.h')
  343. argParser = argparse.ArgumentParser(description="""
  344. Parse eBPF header file and generate documentation for eBPF helper functions.
  345. The RST-formatted output produced can be turned into a manual page with the
  346. rst2man utility.
  347. """)
  348. if (os.path.isfile(bpfh)):
  349. argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h',
  350. default=bpfh)
  351. else:
  352. argParser.add_argument('--filename', help='path to include/uapi/linux/bpf.h')
  353. args = argParser.parse_args()
  354. # Parse file.
  355. headerParser = HeaderParser(args.filename)
  356. headerParser.run()
  357. # Print formatted output to standard output.
  358. printer = PrinterRST(headerParser.helpers)
  359. printer.print_all()