map_cgrp_storage.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. Copyright (C) 2022 Meta Platforms, Inc. and affiliates.
  3. =========================
  4. BPF_MAP_TYPE_CGRP_STORAGE
  5. =========================
  6. The ``BPF_MAP_TYPE_CGRP_STORAGE`` map type represents a local fix-sized
  7. storage for cgroups. It is only available with ``CONFIG_CGROUPS``.
  8. The programs are made available by the same Kconfig. The
  9. data for a particular cgroup can be retrieved by looking up the map
  10. with that cgroup.
  11. This document describes the usage and semantics of the
  12. ``BPF_MAP_TYPE_CGRP_STORAGE`` map type.
  13. Usage
  14. =====
  15. The map key must be ``sizeof(int)`` representing a cgroup fd.
  16. To access the storage in a program, use ``bpf_cgrp_storage_get``::
  17. void *bpf_cgrp_storage_get(struct bpf_map *map, struct cgroup *cgroup, void *value, u64 flags)
  18. ``flags`` could be 0 or ``BPF_LOCAL_STORAGE_GET_F_CREATE`` which indicates that
  19. a new local storage will be created if one does not exist.
  20. The local storage can be removed with ``bpf_cgrp_storage_delete``::
  21. long bpf_cgrp_storage_delete(struct bpf_map *map, struct cgroup *cgroup)
  22. The map is available to all program types.
  23. Examples
  24. ========
  25. A BPF program example with BPF_MAP_TYPE_CGRP_STORAGE::
  26. #include <vmlinux.h>
  27. #include <bpf/bpf_helpers.h>
  28. #include <bpf/bpf_tracing.h>
  29. struct {
  30. __uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
  31. __uint(map_flags, BPF_F_NO_PREALLOC);
  32. __type(key, int);
  33. __type(value, long);
  34. } cgrp_storage SEC(".maps");
  35. SEC("tp_btf/sys_enter")
  36. int BPF_PROG(on_enter, struct pt_regs *regs, long id)
  37. {
  38. struct task_struct *task = bpf_get_current_task_btf();
  39. long *ptr;
  40. ptr = bpf_cgrp_storage_get(&cgrp_storage, task->cgroups->dfl_cgrp, 0,
  41. BPF_LOCAL_STORAGE_GET_F_CREATE);
  42. if (ptr)
  43. __sync_fetch_and_add(ptr, 1);
  44. return 0;
  45. }
  46. Userspace accessing map declared above::
  47. #include <linux/bpf.h>
  48. #include <linux/libbpf.h>
  49. __u32 map_lookup(struct bpf_map *map, int cgrp_fd)
  50. {
  51. __u32 *value;
  52. value = bpf_map_lookup_elem(bpf_map__fd(map), &cgrp_fd);
  53. if (value)
  54. return *value;
  55. return 0;
  56. }
  57. Difference Between BPF_MAP_TYPE_CGRP_STORAGE and BPF_MAP_TYPE_CGROUP_STORAGE
  58. ============================================================================
  59. The old cgroup storage map ``BPF_MAP_TYPE_CGROUP_STORAGE`` has been marked as
  60. deprecated (renamed to ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``). The new
  61. ``BPF_MAP_TYPE_CGRP_STORAGE`` map should be used instead. The following
  62. illusates the main difference between ``BPF_MAP_TYPE_CGRP_STORAGE`` and
  63. ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``.
  64. (1). ``BPF_MAP_TYPE_CGRP_STORAGE`` can be used by all program types while
  65. ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` is available only to cgroup program types
  66. like BPF_CGROUP_INET_INGRESS or BPF_CGROUP_SOCK_OPS, etc.
  67. (2). ``BPF_MAP_TYPE_CGRP_STORAGE`` supports local storage for more than one
  68. cgroup while ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` only supports one cgroup
  69. which is attached by a BPF program.
  70. (3). ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` allocates local storage at attach time so
  71. ``bpf_get_local_storage()`` always returns non-NULL local storage.
  72. ``BPF_MAP_TYPE_CGRP_STORAGE`` allocates local storage at runtime so
  73. it is possible that ``bpf_cgrp_storage_get()`` may return null local storage.
  74. To avoid such null local storage issue, user space can do
  75. ``bpf_map_update_elem()`` to pre-allocate local storage before a BPF program
  76. is attached.
  77. (4). ``BPF_MAP_TYPE_CGRP_STORAGE`` supports deleting local storage by a BPF program
  78. while ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` only deletes storage during
  79. prog detach time.
  80. So overall, ``BPF_MAP_TYPE_CGRP_STORAGE`` supports all ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``
  81. functionality and beyond. It is recommended to use ``BPF_MAP_TYPE_CGRP_STORAGE``
  82. instead of ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``.