# BPF maps

_Peios Kernel / Internals / BPF_

> BPF 'maps' provide generic storage of different types for sharing data between kernel and user space. There are several storage types available, including hash, array, bloom filter and radix-tree. Several of the map types exist to support…

<a id="rst-bpf-maps"></a>

BPF 'maps' provide generic storage of different types for sharing data between kernel and user space. There are several storage types available, including hash, array, bloom filter and radix-tree. Several of the map types exist to support specific BPF helpers that perform actions based on the map contents. The maps are accessed from BPF programs via BPF helpers which are documented in the [man-pages](<https://www.kernel.org/doc/man-pages/>) for [bpf-helpers(7)](<https://man7.org/linux/man-pages/man7/bpf-helpers.7.html>).

BPF maps are accessed from user space via the `bpf` syscall, which provides commands to create maps, lookup elements, update elements and delete elements. More details of the BPF syscall are available in [ebpf-syscall](<https://docs.kernel.org/userspace-api/ebpf/syscall.html>) and in the [man-pages](<https://www.kernel.org/doc/man-pages/>) for [bpf(2)](<https://man7.org/linux/man-pages/man2/bpf.2.html>).

<a id="rst-map-types"></a>

## Map Types

- [BPF_MAP_TYPE_ARRAY and BPF_MAP_TYPE_PERCPU_ARRAY](</kernel/internals/bpf/map-array.md>)
- [BPF_MAP_TYPE_BLOOM_FILTER](</kernel/internals/bpf/map-bloom-filter.md>)
- [BPF_MAP_TYPE_CGROUP_STORAGE](</kernel/internals/bpf/map-cgroup-storage.md>)
- [BPF_MAP_TYPE_CGRP_STORAGE](</kernel/internals/bpf/map-cgrp-storage.md>)
- [BPF_MAP_TYPE_CPUMAP](</kernel/internals/bpf/map-cpumap.md>)
- [map_devmap](<https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/Documentation/bpf/map_devmap.rst?id=9021cc14f7d98b4a1d2c932f52c5343d4d0f6b92>)
- [BPF_MAP_TYPE_HASH, with PERCPU and LRU Variants](</kernel/internals/bpf/map-hash.md>)
- [BPF_MAP_TYPE_LPM_TRIE](</kernel/internals/bpf/map-lpm-trie.md>)
- [BPF_MAP_TYPE_ARRAY_OF_MAPS and BPF_MAP_TYPE_HASH_OF_MAPS](</kernel/internals/bpf/map-of-maps.md>)
- [BPF_MAP_TYPE_QUEUE and BPF_MAP_TYPE_STACK](</kernel/internals/bpf/map-queue-stack.md>)
- [BPF_MAP_TYPE_SK_STORAGE](</kernel/internals/bpf/map-sk-storage.md>)
- [BPF_MAP_TYPE_SOCKMAP and BPF_MAP_TYPE_SOCKHASH](</kernel/internals/bpf/map-sockmap.md>)
- [BPF_MAP_TYPE_XSKMAP](</kernel/internals/bpf/map-xskmap.md>)

<a id="rst-usage-notes"></a>

## Usage Notes





<a id="c-bpf"></a>

```c
int bpf(int command, union bpf_attr *attr, u32 size)
```

Use the `bpf()` system call to perform the operation specified by `command`. The operation takes parameters provided in `attr`. The `size` argument is the size of the `union bpf_attr` in `attr`.

**BPF_MAP_CREATE**

Create a map with the desired type and attributes in `attr`:



```c
int fd;
union bpf_attr attr = {
        .map_type = BPF_MAP_TYPE_ARRAY;  /* mandatory */
        .key_size = sizeof(__u32);       /* mandatory */
        .value_size = sizeof(__u32);     /* mandatory */
        .max_entries = 256;              /* mandatory */
        .map_flags = BPF_F_MMAPABLE;
        .map_name = "example_array";
};

fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
```

Returns a process-local file descriptor on success, or negative error in case of failure. The map can be deleted by calling `close(fd)`. Maps held by open file descriptors will be deleted automatically when a process exits.

> [!NOTE]
>
> Valid characters for `map_name` are `A-Z`, `a-z`, `0-9`, `'_'` and `'.'`.

**BPF_MAP_LOOKUP_ELEM**

Lookup key in a given map using `attr->map_fd`, `attr->key`, `attr->value`. Returns zero and stores found elem into `attr->value` on success, or negative error on failure.

**BPF_MAP_UPDATE_ELEM**

Create or update key/value pair in a given map using `attr->map_fd`, `attr->key`, `attr->value`. Returns zero on success or negative error on failure.

**BPF_MAP_DELETE_ELEM**

Find and delete element by key in a given map using `attr->map_fd`, `attr->key`. Returns zero on success or negative error on failure.

<!-- Links: -->

<a id="rst-man-pages"></a>



<a id="rst-bpf-2"></a>



<a id="rst-bpf-helpers-7"></a>



<a id="rst-ebpf-syscall"></a>
