Skip to content

Commit 824bd0c

Browse files
4astdavem330
authored andcommitted
bpf: introduce BPF_MAP_TYPE_PERCPU_HASH map
Introduce BPF_MAP_TYPE_PERCPU_HASH map type which is used to do accurate counters without need to use BPF_XADD instruction which turned out to be too costly for high-performance network monitoring. In the typical use case the 'key' is the flow tuple or other long living object that sees a lot of events per second. bpf_map_lookup_elem() returns per-cpu area. Example: struct { u32 packets; u32 bytes; } * ptr = bpf_map_lookup_elem(&map, &key); /* ptr points to this_cpu area of the value, so the following * increments will not collide with other cpus */ ptr->packets ++; ptr->bytes += skb->len; bpf_update_elem() atomically creates a new element where all per-cpu values are zero initialized and this_cpu value is populated with given 'value'. Note that non-per-cpu hash map always allocates new element and then deletes old after rcu grace period to maintain atomicity of update. Per-cpu hash map updates element values in-place. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ba905f5 commit 824bd0c

File tree

2 files changed

+229
-47
lines changed

2 files changed

+229
-47
lines changed

include/uapi/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ enum bpf_map_type {
8181
BPF_MAP_TYPE_ARRAY,
8282
BPF_MAP_TYPE_PROG_ARRAY,
8383
BPF_MAP_TYPE_PERF_EVENT_ARRAY,
84+
BPF_MAP_TYPE_PERCPU_HASH,
8485
};
8586

8687
enum bpf_prog_type {

0 commit comments

Comments
 (0)