map_cpumap.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. Copyright (C) 2022 Red Hat, Inc.
  3. ===================
  4. BPF_MAP_TYPE_CPUMAP
  5. ===================
  6. .. note::
  7. - ``BPF_MAP_TYPE_CPUMAP`` was introduced in kernel version 4.15
  8. .. kernel-doc:: kernel/bpf/cpumap.c
  9. :doc: cpu map
  10. An example use-case for this map type is software based Receive Side Scaling (RSS).
  11. The CPUMAP represents the CPUs in the system indexed as the map-key, and the
  12. map-value is the config setting (per CPUMAP entry). Each CPUMAP entry has a dedicated
  13. kernel thread bound to the given CPU to represent the remote CPU execution unit.
  14. Starting from Linux kernel version 5.9 the CPUMAP can run a second XDP program
  15. on the remote CPU. This allows an XDP program to split its processing across
  16. multiple CPUs. For example, a scenario where the initial CPU (that sees/receives
  17. the packets) needs to do minimal packet processing and the remote CPU (to which
  18. the packet is directed) can afford to spend more cycles processing the frame. The
  19. initial CPU is where the XDP redirect program is executed. The remote CPU
  20. receives raw ``xdp_frame`` objects.
  21. Usage
  22. =====
  23. Kernel BPF
  24. ----------
  25. bpf_redirect_map()
  26. ^^^^^^^^^^^^^^^^^^
  27. .. code-block:: c
  28. long bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags)
  29. Redirect the packet to the endpoint referenced by ``map`` at index ``key``.
  30. For ``BPF_MAP_TYPE_CPUMAP`` this map contains references to CPUs.
  31. The lower two bits of ``flags`` are used as the return code if the map lookup
  32. fails. This is so that the return value can be one of the XDP program return
  33. codes up to ``XDP_TX``, as chosen by the caller.
  34. User space
  35. ----------
  36. .. note::
  37. CPUMAP entries can only be updated/looked up/deleted from user space and not
  38. from an eBPF program. Trying to call these functions from a kernel eBPF
  39. program will result in the program failing to load and a verifier warning.
  40. bpf_map_update_elem()
  41. ^^^^^^^^^^^^^^^^^^^^^
  42. .. code-block:: c
  43. int bpf_map_update_elem(int fd, const void *key, const void *value, __u64 flags);
  44. CPU entries can be added or updated using the ``bpf_map_update_elem()``
  45. helper. This helper replaces existing elements atomically. The ``value`` parameter
  46. can be ``struct bpf_cpumap_val``.
  47. .. code-block:: c
  48. struct bpf_cpumap_val {
  49. __u32 qsize; /* queue size to remote target CPU */
  50. union {
  51. int fd; /* prog fd on map write */
  52. __u32 id; /* prog id on map read */
  53. } bpf_prog;
  54. };
  55. The flags argument can be one of the following:
  56. - BPF_ANY: Create a new element or update an existing element.
  57. - BPF_NOEXIST: Create a new element only if it did not exist.
  58. - BPF_EXIST: Update an existing element.
  59. bpf_map_lookup_elem()
  60. ^^^^^^^^^^^^^^^^^^^^^
  61. .. code-block:: c
  62. int bpf_map_lookup_elem(int fd, const void *key, void *value);
  63. CPU entries can be retrieved using the ``bpf_map_lookup_elem()``
  64. helper.
  65. bpf_map_delete_elem()
  66. ^^^^^^^^^^^^^^^^^^^^^
  67. .. code-block:: c
  68. int bpf_map_delete_elem(int fd, const void *key);
  69. CPU entries can be deleted using the ``bpf_map_delete_elem()``
  70. helper. This helper will return 0 on success, or negative error in case of
  71. failure.
  72. Examples
  73. ========
  74. Kernel
  75. ------
  76. The following code snippet shows how to declare a ``BPF_MAP_TYPE_CPUMAP`` called
  77. ``cpu_map`` and how to redirect packets to a remote CPU using a round robin scheme.
  78. .. code-block:: c
  79. struct {
  80. __uint(type, BPF_MAP_TYPE_CPUMAP);
  81. __type(key, __u32);
  82. __type(value, struct bpf_cpumap_val);
  83. __uint(max_entries, 12);
  84. } cpu_map SEC(".maps");
  85. struct {
  86. __uint(type, BPF_MAP_TYPE_ARRAY);
  87. __type(key, __u32);
  88. __type(value, __u32);
  89. __uint(max_entries, 12);
  90. } cpus_available SEC(".maps");
  91. struct {
  92. __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  93. __type(key, __u32);
  94. __type(value, __u32);
  95. __uint(max_entries, 1);
  96. } cpus_iterator SEC(".maps");
  97. SEC("xdp")
  98. int xdp_redir_cpu_round_robin(struct xdp_md *ctx)
  99. {
  100. __u32 key = 0;
  101. __u32 cpu_dest = 0;
  102. __u32 *cpu_selected, *cpu_iterator;
  103. __u32 cpu_idx;
  104. cpu_iterator = bpf_map_lookup_elem(&cpus_iterator, &key);
  105. if (!cpu_iterator)
  106. return XDP_ABORTED;
  107. cpu_idx = *cpu_iterator;
  108. *cpu_iterator += 1;
  109. if (*cpu_iterator == bpf_num_possible_cpus())
  110. *cpu_iterator = 0;
  111. cpu_selected = bpf_map_lookup_elem(&cpus_available, &cpu_idx);
  112. if (!cpu_selected)
  113. return XDP_ABORTED;
  114. cpu_dest = *cpu_selected;
  115. if (cpu_dest >= bpf_num_possible_cpus())
  116. return XDP_ABORTED;
  117. return bpf_redirect_map(&cpu_map, cpu_dest, 0);
  118. }
  119. User space
  120. ----------
  121. The following code snippet shows how to dynamically set the max_entries for a
  122. CPUMAP to the max number of cpus available on the system.
  123. .. code-block:: c
  124. int set_max_cpu_entries(struct bpf_map *cpu_map)
  125. {
  126. if (bpf_map__set_max_entries(cpu_map, libbpf_num_possible_cpus()) < 0) {
  127. fprintf(stderr, "Failed to set max entries for cpu_map map: %s",
  128. strerror(errno));
  129. return -1;
  130. }
  131. return 0;
  132. }
  133. References
  134. ===========
  135. - https://developers.redhat.com/blog/2021/05/13/receive-side-scaling-rss-with-ebpf-and-cpumap#redirecting_into_a_cpumap