map_sk_storage.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. Copyright (C) 2022 Red Hat, Inc.
  3. =======================
  4. BPF_MAP_TYPE_SK_STORAGE
  5. =======================
  6. .. note::
  7. - ``BPF_MAP_TYPE_SK_STORAGE`` was introduced in kernel version 5.2
  8. ``BPF_MAP_TYPE_SK_STORAGE`` is used to provide socket-local storage for BPF
  9. programs. A map of type ``BPF_MAP_TYPE_SK_STORAGE`` declares the type of storage
  10. to be provided and acts as the handle for accessing the socket-local
  11. storage. The values for maps of type ``BPF_MAP_TYPE_SK_STORAGE`` are stored
  12. locally with each socket instead of with the map. The kernel is responsible for
  13. allocating storage for a socket when requested and for freeing the storage when
  14. either the map or the socket is deleted.
  15. .. note::
  16. - The key type must be ``int`` and ``max_entries`` must be set to ``0``.
  17. - The ``BPF_F_NO_PREALLOC`` flag must be used when creating a map for
  18. socket-local storage.
  19. Usage
  20. =====
  21. Kernel BPF
  22. ----------
  23. bpf_sk_storage_get()
  24. ~~~~~~~~~~~~~~~~~~~~
  25. .. code-block:: c
  26. void *bpf_sk_storage_get(struct bpf_map *map, void *sk, void *value, u64 flags)
  27. Socket-local storage for ``map`` can be retrieved from socket ``sk`` using the
  28. ``bpf_sk_storage_get()`` helper. If the ``BPF_LOCAL_STORAGE_GET_F_CREATE``
  29. flag is used then ``bpf_sk_storage_get()`` will create the storage for ``sk``
  30. if it does not already exist. ``value`` can be used together with
  31. ``BPF_LOCAL_STORAGE_GET_F_CREATE`` to initialize the storage value, otherwise
  32. it will be zero initialized. Returns a pointer to the storage on success, or
  33. ``NULL`` in case of failure.
  34. .. note::
  35. - ``sk`` is a kernel ``struct sock`` pointer for LSM or tracing programs.
  36. - ``sk`` is a ``struct bpf_sock`` pointer for other program types.
  37. bpf_sk_storage_delete()
  38. ~~~~~~~~~~~~~~~~~~~~~~~
  39. .. code-block:: c
  40. long bpf_sk_storage_delete(struct bpf_map *map, void *sk)
  41. Socket-local storage for ``map`` can be deleted from socket ``sk`` using the
  42. ``bpf_sk_storage_delete()`` helper. Returns ``0`` on success, or negative
  43. error in case of failure.
  44. User space
  45. ----------
  46. bpf_map_update_elem()
  47. ~~~~~~~~~~~~~~~~~~~~~
  48. .. code-block:: c
  49. int bpf_map_update_elem(int map_fd, const void *key, const void *value, __u64 flags)
  50. Socket-local storage for map ``map_fd`` can be added or updated locally to a
  51. socket using the ``bpf_map_update_elem()`` libbpf function. The socket is
  52. identified by a `socket` ``fd`` stored in the pointer ``key``. The pointer
  53. ``value`` has the data to be added or updated to the socket ``fd``. The type
  54. and size of ``value`` should be the same as the value type of the map
  55. definition.
  56. The ``flags`` parameter can be used to control the update behaviour:
  57. - ``BPF_ANY`` will create storage for `socket` ``fd`` or update existing storage.
  58. - ``BPF_NOEXIST`` will create storage for `socket` ``fd`` only if it did not
  59. already exist, otherwise the call will fail with ``-EEXIST``.
  60. - ``BPF_EXIST`` will update existing storage for `socket` ``fd`` if it already
  61. exists, otherwise the call will fail with ``-ENOENT``.
  62. Returns ``0`` on success, or negative error in case of failure.
  63. bpf_map_lookup_elem()
  64. ~~~~~~~~~~~~~~~~~~~~~
  65. .. code-block:: c
  66. int bpf_map_lookup_elem(int map_fd, const void *key, void *value)
  67. Socket-local storage for map ``map_fd`` can be retrieved from a socket using
  68. the ``bpf_map_lookup_elem()`` libbpf function. The storage is retrieved from
  69. the socket identified by a `socket` ``fd`` stored in the pointer
  70. ``key``. Returns ``0`` on success, or negative error in case of failure.
  71. bpf_map_delete_elem()
  72. ~~~~~~~~~~~~~~~~~~~~~
  73. .. code-block:: c
  74. int bpf_map_delete_elem(int map_fd, const void *key)
  75. Socket-local storage for map ``map_fd`` can be deleted from a socket using the
  76. ``bpf_map_delete_elem()`` libbpf function. The storage is deleted from the
  77. socket identified by a `socket` ``fd`` stored in the pointer ``key``. Returns
  78. ``0`` on success, or negative error in case of failure.
  79. Examples
  80. ========
  81. Kernel BPF
  82. ----------
  83. This snippet shows how to declare socket-local storage in a BPF program:
  84. .. code-block:: c
  85. struct {
  86. __uint(type, BPF_MAP_TYPE_SK_STORAGE);
  87. __uint(map_flags, BPF_F_NO_PREALLOC);
  88. __type(key, int);
  89. __type(value, struct my_storage);
  90. } socket_storage SEC(".maps");
  91. This snippet shows how to retrieve socket-local storage in a BPF program:
  92. .. code-block:: c
  93. SEC("sockops")
  94. int _sockops(struct bpf_sock_ops *ctx)
  95. {
  96. struct my_storage *storage;
  97. struct bpf_sock *sk;
  98. sk = ctx->sk;
  99. if (!sk)
  100. return 1;
  101. storage = bpf_sk_storage_get(&socket_storage, sk, 0,
  102. BPF_LOCAL_STORAGE_GET_F_CREATE);
  103. if (!storage)
  104. return 1;
  105. /* Use 'storage' here */
  106. return 1;
  107. }
  108. Please see the ``tools/testing/selftests/bpf`` directory for functional
  109. examples.
  110. References
  111. ==========
  112. https://lwn.net/ml/netdev/20190426171103.61892-1-kafai@fb.com/