intro-specs.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. .. SPDX-License-Identifier: BSD-3-Clause
  2. =====================================
  3. Using Netlink protocol specifications
  4. =====================================
  5. This document is a quick starting guide for using Netlink protocol
  6. specifications. For more detailed description of the specs see :doc:`specs`.
  7. Simple CLI
  8. ==========
  9. Kernel comes with a simple CLI tool which should be useful when
  10. developing Netlink related code. The tool is implemented in Python
  11. and can use a YAML specification to issue Netlink requests
  12. to the kernel. Only Generic Netlink is supported.
  13. The tool is located at ``tools/net/ynl/cli.py``. It accepts
  14. a handul of arguments, the most important ones are:
  15. - ``--spec`` - point to the spec file
  16. - ``--do $name`` / ``--dump $name`` - issue request ``$name``
  17. - ``--json $attrs`` - provide attributes for the request
  18. - ``--subscribe $group`` - receive notifications from ``$group``
  19. YAML specs can be found under ``Documentation/netlink/specs/``.
  20. Example use::
  21. $ ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/ethtool.yaml \
  22. --do rings-get \
  23. --json '{"header":{"dev-index": 18}}'
  24. {'header': {'dev-index': 18, 'dev-name': 'eni1np1'},
  25. 'rx': 0,
  26. 'rx-jumbo': 0,
  27. 'rx-jumbo-max': 4096,
  28. 'rx-max': 4096,
  29. 'rx-mini': 0,
  30. 'rx-mini-max': 4096,
  31. 'tx': 0,
  32. 'tx-max': 4096,
  33. 'tx-push': 0}
  34. The input arguments are parsed as JSON, while the output is only
  35. Python-pretty-printed. This is because some Netlink types can't
  36. be expressed as JSON directly. If such attributes are needed in
  37. the input some hacking of the script will be necessary.
  38. The spec and Netlink internals are factored out as a standalone
  39. library - it should be easy to write Python tools / tests reusing
  40. code from ``cli.py``.
  41. Generating kernel code
  42. ======================
  43. ``tools/net/ynl/ynl-regen.sh`` scans the kernel tree in search of
  44. auto-generated files which need to be updated. Using this tool is the easiest
  45. way to generate / update auto-generated code.
  46. By default code is re-generated only if spec is newer than the source,
  47. to force regeneration use ``-f``.
  48. ``ynl-regen.sh`` searches for ``YNL-GEN`` in the contents of files
  49. (note that it only scans files in the git index, that is only files
  50. tracked by git!) For instance the ``fou_nl.c`` kernel source contains::
  51. /* Documentation/netlink/specs/fou.yaml */
  52. /* YNL-GEN kernel source */
  53. ``ynl-regen.sh`` will find this marker and replace the file with
  54. kernel source based on fou.yaml.
  55. The simplest way to generate a new file based on a spec is to add
  56. the two marker lines like above to a file, add that file to git,
  57. and run the regeneration tool. Grep the tree for ``YNL-GEN``
  58. to see other examples.
  59. The code generation itself is performed by ``tools/net/ynl/ynl-gen-c.py``
  60. but it takes a few arguments so calling it directly for each file
  61. quickly becomes tedious.
  62. YNL lib
  63. =======
  64. ``tools/net/ynl/lib/`` contains an implementation of a C library
  65. (based on libmnl) which integrates with code generated by
  66. ``tools/net/ynl/ynl-gen-c.py`` to create easy to use netlink wrappers.
  67. YNL basics
  68. ----------
  69. The YNL library consists of two parts - the generic code (functions
  70. prefix by ``ynl_``) and per-family auto-generated code (prefixed
  71. with the name of the family).
  72. To create a YNL socket call ynl_sock_create() passing the family
  73. struct (family structs are exported by the auto-generated code).
  74. ynl_sock_destroy() closes the socket.
  75. YNL requests
  76. ------------
  77. Steps for issuing YNL requests are best explained on an example.
  78. All the functions and types in this example come from the auto-generated
  79. code (for the netdev family in this case):
  80. .. code-block:: c
  81. // 0. Request and response pointers
  82. struct netdev_dev_get_req *req;
  83. struct netdev_dev_get_rsp *d;
  84. // 1. Allocate a request
  85. req = netdev_dev_get_req_alloc();
  86. // 2. Set request parameters (as needed)
  87. netdev_dev_get_req_set_ifindex(req, ifindex);
  88. // 3. Issues the request
  89. d = netdev_dev_get(ys, req);
  90. // 4. Free the request arguments
  91. netdev_dev_get_req_free(req);
  92. // 5. Error check (the return value from step 3)
  93. if (!d) {
  94. // 6. Print the YNL-generated error
  95. fprintf(stderr, "YNL: %s\n", ys->err.msg);
  96. return -1;
  97. }
  98. // ... do stuff with the response @d
  99. // 7. Free response
  100. netdev_dev_get_rsp_free(d);
  101. YNL dumps
  102. ---------
  103. Performing dumps follows similar pattern as requests.
  104. Dumps return a list of objects terminated by a special marker,
  105. or NULL on error. Use ``ynl_dump_foreach()`` to iterate over
  106. the result.
  107. YNL notifications
  108. -----------------
  109. YNL lib supports using the same socket for notifications and
  110. requests. In case notifications arrive during processing of a request
  111. they are queued internally and can be retrieved at a later time.
  112. To subscribed to notifications use ``ynl_subscribe()``.
  113. The notifications have to be read out from the socket,
  114. ``ynl_socket_get_fd()`` returns the underlying socket fd which can
  115. be plugged into appropriate asynchronous IO API like ``poll``,
  116. or ``select``.
  117. Notifications can be retrieved using ``ynl_ntf_dequeue()`` and have
  118. to be freed using ``ynl_ntf_free()``. Since we don't know the notification
  119. type upfront the notifications are returned as ``struct ynl_ntf_base_type *``
  120. and user is expected to cast them to the appropriate full type based
  121. on the ``cmd`` member.