summaryrefslogtreecommitdiff
path: root/src/shared/bpf-program.c
Commit message (Collapse)AuthorAgeFilesLines
* parse-util: make parse_fd() return -EBADFYu Watanabe2023-05-081-2/+0
| | | | | | | | The previous error code -ERANGE is slightly ambiguous, and use more specific one. This also drops unnecessary error handlings. Follow-up for 754d8b9c330150fdb3767491e24975f7dfe2a203 and e652663a043cb80936bb12ad5c87766fc5150c24.
* tree-wide: use parse_fd()David Tardon2023-05-051-4/+4
|
* shared: reject empty attachment pathFrantisek Sumsal2023-05-031-0/+3
|
* bpf-firewall: give a name to maps usedDominique Martinet2023-04-181-1/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | Running systemd with IP accounting enabled generates many bpf maps (two per unit for accounting, another two if IPAddressAllow/Deny are used). Systemd itself knows which maps belong to what unit and commands like `systemctl status <unit>` can be used to query what service has which map, but monitoring these values all the time costs 4 dbus requests (calling the .IP{E,I}gress{Bytes,Packets} method for each unit) and makes services like the prometheus systemd_exporter[1] somewhat slow when doing that for every units, while less precise information could quickly be obtained by looking directly at the maps. Unfortunately, bpf map names are rather limited: - only 15 characters in length (16, but last byte must be 0) - only allows isalnum(), _ and . characters If it wasn't for the length limit we could use the normal unit escape functions but I've opted to just make any forbidden character into underscores for maximum brievty -- the map prefix is also rather short: This isn't meant as a precise mapping, but as a hint for admins who want to look at these. (Note there is no problem if multiple maps have the same name) Link: https://github.com/povilasv/systemd_exporter [1]
* tree-wide: use -EBADF for fd initializationZbigniew Jędrzejewski-Szmek2022-12-191-5/+5
| | | | | | | | | | | | | | | | -1 was used everywhere, but -EBADF or -EBADFD started being used in various places. Let's make things consistent in the new style. Note that there are two candidates: EBADF 9 Bad file descriptor EBADFD 77 File descriptor in bad state Since we're initializating the fd, we're just assigning a value that means "no fd yet", so it's just a bad file descriptor, and the first errno fits better. If instead we had a valid file descriptor that became invalid because of some operation or state change, the other errno would fit better. In some places, initialization is dropped if unnecessary.
* bpf: name unnamed bpf programsJulia Kartseva2022-01-221-1/+15
| | | | | | | | | | | | | | | | | | bpf-firewall and bpf-devices do not have names. This complicates debugging with bpftool(8). Assign names starting with 'sd_' prefix: * firewall program names are 'sd_fw_ingress' for ingress attach point and 'sd_fw_egress' for egress. * 'sd_devices' for devices prog 'sd_' prefix is already used in source-compiled programs, e.g. sd_restrictif_i, sd_restrictif_e, sd_bind6. The name must not be longer than 15 characters or BPF_OBJ_NAME_LEN - 1. Assign names only to programs loaded to kernel by systemd since programs pinned to bpffs are already loaded.
* tree-wide: use new RET_NERRNO() helper at various placesLennart Poettering2021-11-161-22/+6
|
* core: remove refcount for bpf programalexlzhu2021-10-121-29/+25
| | | | | | | | | | | | Currently ref count of bpf-program is kept in user space. However, the kernel already implements its own ref count. Thus the ref count we keep for bpf-program is redundant. This PR removes ref count for bpf program as part of a task to simplify bpf-program and remove redundancies, which will make the switch to code-compiled BPF programs easier. Part of #19270
* tree-wide: make cunescape*() functions return ssize_tZbigniew Jędrzejewski-Szmek2021-07-091-3/+4
| | | | | Strictly speaking, we are returning the size of a memory chunk of arbitrary size, so ssize_t is more appropriate than int.
* bpf-program: serialize attached BPF programs across daemon reexec/reloadLennart Poettering2021-06-081-0/+138
| | | | Alternative to #17495
* bpf-program: export hash_ops for BPFProgam objectsLennart Poettering2021-06-081-0/+2
|
* bpf-program: use structured initialization when allocating BPFProgram objectsLennart Poettering2021-06-081-4/+6
|
* alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()Lennart Poettering2021-05-191-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | We recently started making more use of malloc_usable_size() and rely on it (see the string_erase() story). Given that we don't really support sytems where malloc_usable_size() cannot be trusted beyond statistics anyway, let's go fully in and rework GREEDY_REALLOC() on top of it: instead of passing around and maintaining the currenly allocated size everywhere, let's just derive it automatically from malloc_usable_size(). I am mostly after this for the simplicity this brings. It also brings minor efficiency improvements I guess, but things become so much nicer to look at if we can avoid these allocation size variables everywhere. Note that the malloc_usable_size() man page says relying on it wasn't "good programming practice", but I think it does this for reasons that don't apply here: the greedy realloc logic specifically doesn't rely on the returned extra size, beyond the fact that it is equal or larger than what was requested. (This commit was supposed to be a quick patch btw, but apparently we use the greedy realloc stuff quite a bit across the codebase, so this ends up touching *a*lot* of code.)
* shared: bpf_attach_type {from,to} stringJulia Kartseva2021-04-091-0/+24
| | | | | | | | | Introduce bpf_cgroup_attach_type_table with accustomed attached type names also used in bpftool. Add bpf_cgroup_attach_type_{from|to}_string helpers to convert from|to string representation of pinned bpf program, e.g. "egress:/sys/fs/bpf/egress-hook" for /sys/fs/bpf/egress-hook path and BPF_CGROUP_INET_EGRESS attach type.
* shared: add bpf-program helpersJulia Kartseva2021-04-091-0/+80
| | | | | | | | Add helpers to: - Create new BPFProgram instance from a path in bpf filesystem and bpf attach type; - Pin a program to bpf fs; - Get BPF program ID by BPF program FD.
* bpf: do not use structured initialization for bpf_attrLuca Boccassi2021-01-101-40/+33
| | | | | | | It looks like zero'ing the struct is not enough, and with some level of optimizations there is still non-zero padding left over. Switch to member-by-member initialization. Also convert all remaining bpf_attr variables in other files.
* bpf: zero bpf_attr before initializationLuca Boccassi2021-01-091-0/+8
| | | | | | | | When building with Clang and using structured initialization, the bpf_attr union is not zero-padded, so the kernel misdetects it as an unsupported extension. zero it until Clang's behaviour matches GCC. Do not skip the test on Github Actions anymore.
* bpf: do not call log_oom() in library functionYu Watanabe2020-11-131-2/+1
|
* license: LGPL-2.1+ -> LGPL-2.1-or-laterYu Watanabe2020-11-091-1/+1
|
* tree-wide: drop missing.hYu Watanabe2019-10-311-1/+1
|
* bpf-firewall: custom BPF programs through IP(Ingress|Egress)FilterPath=Kai Lüke2019-06-251-0/+19
| | | | | | | Takes a single /sys/fs/bpf/pinned_prog string as argument, but may be specified multiple times. An empty assignment resets all previous filters. Closes https://github.com/systemd/systemd/issues/10227
* util: split out memcmp()/memset() related calls into memory-util.[ch]Lennart Poettering2019-03-131-1/+1
| | | | Just some source rearranging.
* Move various files that don't need to be in basic/ to shared/Zbigniew Jędrzejewski-Szmek2018-11-201-0/+237
This doesn't have much effect on the final build, because we link libbasic.a into libsystemd-shared.so, so in the end, all the object built from basic/ end up in libsystemd-shared. And when the static library is linked into binaries, any objects that are included in it but are not used are trimmed. Hence, the size of output artifacts doesn't change: $ du -sb /var/tmp/inst* 54181861 /var/tmp/inst1 (old) 54207441 /var/tmp/inst1s (old split-usr) 54182477 /var/tmp/inst2 (new) 54208041 /var/tmp/inst2s (new split-usr) (The negligible change in size is because libsystemd-shared.so is bigger by a few hundred bytes. I guess it's because symbols are named differently or something like that.) The effect is on the build process, in particular partial builds. This change effectively moves the requirements on some build steps toward the leaves of the dependency tree. Two effects: - when building items that do not depend on libsystemd-shared, we build less stuff for libbasic.a (which wouldn't be used anyway, so it's a net win). - when building items that do depend on libshared, we reduce libbasic.a as a synchronization point, possibly allowing better parallelism. Method: 1. copy list of .h files from src/basic/meson.build to /tmp/basic 2. $ for i in $(grep '.h$' /tmp/basic); do echo $i; git --no-pager grep "include \"$i\"" src/basic/ 'src/lib*' 'src/nss-*' 'src/journal/sd-journal.c' |grep -v "${i%.h}.c";echo ;done | less