|
| 1 | +# Object pinning |
| 2 | + |
| 3 | +BPF has a persistent view of maps and programs under its own filesystem |
| 4 | +`/sys/fs/bpf`. Users are able to make each object visible under the bpffs. |
| 5 | +We call it `object pinning`. This is done by calling syscall `bpf(2)` with |
| 6 | +a command `BPF_OBJ_PIN`. After doing that, users are able to use the object |
| 7 | +with commands such as `BPF_OBJ_GET`, or remove the object with an ordinary |
| 8 | +VFS syscall `unlink(2)`. |
| 9 | + |
| 10 | +Doing that, we can make maps and programs stay alive across process |
| 11 | +terminations. This mechanism provides a much more consistent way of sharing |
| 12 | +objects with other processes, compared to other solutions such as `tc`, |
| 13 | +where objects are shared via Unix domain sockets. |
| 14 | + |
| 15 | +## Different pinning options |
| 16 | + |
| 17 | +`C.bpf_map_def.pinning` (defined in |
| 18 | +[bpf.h](https://github.com/iovisor/gobpf/blob/446e57e0e24e/elf/include/bpf.h#L616)) |
| 19 | +can be set to one the following pinning options. |
| 20 | + |
| 21 | +* `PIN_NONE` : object is not pinned |
| 22 | +* `PIN_OBJECT_NS` : pinning that is local to an object (to-be-implemented) |
| 23 | +* `PIN_GLOBAL_NS` : pinning with a global namespace under e.g. `/sys/fs/bpf/ns1/globals` |
| 24 | +* `PIN_CUSTOM_NS` : pinning with a custom path given as section parameter |
| 25 | + |
| 26 | +### Pinning with `PIN_CUSTOM_NS` |
| 27 | + |
| 28 | +When loading a module with `C.bpf_map_def.pinning` set to `PIN_CUSTOM_NS`, |
| 29 | +an additional path must be set in the `elf.SectionParams.PinPath` parameter |
| 30 | +to `Load()`. For example: |
| 31 | + |
| 32 | +(C source file for an ELF object) |
| 33 | +``` |
| 34 | +struct bpf_map_def SEC("maps/dummy_array_custom") dummy_array_custom = { |
| 35 | + .type = BPF_MAP_TYPE_ARRAY, |
| 36 | + .key_size = sizeof(int), |
| 37 | + .value_size = sizeof(unsigned int), |
| 38 | + .max_entries = 1024, |
| 39 | + .pinning = PIN_CUSTOM_NS, |
| 40 | +}; |
| 41 | +``` |
| 42 | + |
| 43 | +(Go source file that actually uses the ELF object) |
| 44 | +``` |
| 45 | +b := elf.NewModule(customELFFileName) |
| 46 | +var secParams = map[string]elf.SectionParams{ |
| 47 | + "maps/dummy_array_custom": elf.SectionParams{ |
| 48 | + PinPath: "ns1/test1", |
| 49 | + }, |
| 50 | +} |
| 51 | +if err := b.Load(secParams); err != nil { |
| 52 | + fmt.Println(err) |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +Then you can check if the object is pinned like below: |
| 57 | + |
| 58 | +``` |
| 59 | +$ ls -l /sys/fs/bpf/ns1/test1 |
| 60 | +``` |
| 61 | + |
| 62 | +### Unpinning with `PIN_CUSTOM_NS` |
| 63 | + |
| 64 | +To unpin a custom pinned map, we need an additional path |
| 65 | +`elf.CloseOptions.PinPath` as parameter to `CloseExt()`. For example: |
| 66 | + |
| 67 | +``` |
| 68 | +var closeOptions = map[string]elf.CloseOptions{ |
| 69 | + "maps/dummy_array_custom": elf.CloseOptions{ |
| 70 | + Unpin: true, |
| 71 | + PinPath: "ns1/test1", |
| 72 | + }, |
| 73 | +} |
| 74 | +if err := b.CloseExt(closeOptions); err != nil { |
| 75 | + fmt.Println(err) |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +Or you can also remove the file just like below: |
| 80 | + |
| 81 | +``` |
| 82 | +os.Remove("/sys/fs/bpf/ns1/test1") |
| 83 | +``` |
0 commit comments