mirror of
https://git.kernel.org/pub/scm/network/iproute2/iproute2.git
synced 2024-11-16 06:25:35 +08:00
32e93fb7f6
This larger work addresses one of the bigger remaining issues on tc's eBPF frontend, that is, to allow for persistent file descriptors. Whenever tc parses the ELF object, extracts and loads maps into the kernel, these file descriptors will be out of reach after the tc instance exits. Meaning, for simple (unnested) programs which contain one or multiple maps, the kernel holds a reference, and they will live on inside the kernel until the program holding them is unloaded, but they will be out of reach for user space, even worse with (also multiple nested) tail calls. For this issue, we introduced the concept of an agent that can receive the set of file descriptors from the tc instance creating them, in order to be able to further inspect/update map data for a specific use case. However, while that is more tied towards specific applications, it still doesn't easily allow for sharing maps accross multiple tc instances and would require a daemon to be running in the background. F.e. when a map should be shared by two eBPF programs, one attached to ingress, one to egress, this currently doesn't work with the tc frontend. This work solves exactly that, i.e. if requested, maps can now be _arbitrarily_ shared between object files (PIN_GLOBAL_NS) or within a single object (but various program sections, PIN_OBJECT_NS) without "loosing" the file descriptor set. To make that happen, we use eBPF object pinning introduced in kernel commit b2197755b263 ("bpf: add support for persistent maps/progs") for exactly this purpose. The shipped examples/bpf/bpf_shared.c code from this patch can be easily applied, for instance, as: - classifier-classifier shared: tc filter add dev foo parent 1: bpf obj shared.o sec egress tc filter add dev foo parent ffff: bpf obj shared.o sec ingress - classifier-action shared (here: late binding to a dummy classifier): tc actions add action bpf obj shared.o sec egress pass index 42 tc filter add dev foo parent ffff: bpf obj shared.o sec ingress tc filter add dev foo parent 1: bpf bytecode '1,6 0 0 4294967295,' \ action bpf index 42 The toy example increments a shared counter on egress and dumps its value on ingress (if no sharing (PIN_NONE) would have been chosen, map value is 0, of course, due to the two map instances being created): [...] <idle>-0 [002] ..s. 38264.788234: : map val: 4 <idle>-0 [002] ..s. 38264.788919: : map val: 4 <idle>-0 [002] ..s. 38264.789599: : map val: 5 [...] ... thus if both sections reference the pinned map(s) in question, tc will take care of fetching the appropriate file descriptor. The patch has been tested extensively on both, classifier and action sides. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
152 lines
3.2 KiB
C
152 lines
3.2 KiB
C
/*
|
|
* e_bpf.c BPF exec proxy
|
|
*
|
|
* This program is free software; you can distribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*
|
|
* Authors: Daniel Borkmann <daniel@iogearbox.net>
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
#include "utils.h"
|
|
|
|
#include "tc_util.h"
|
|
#include "tc_bpf.h"
|
|
|
|
#include "bpf_elf.h"
|
|
#include "bpf_scm.h"
|
|
|
|
#define BPF_DEFAULT_CMD "/bin/sh"
|
|
|
|
static char *argv_default[] = { BPF_DEFAULT_CMD, NULL };
|
|
|
|
static void explain(void)
|
|
{
|
|
fprintf(stderr, "Usage: ... bpf [ import UDS_FILE ] [ run CMD ] [ debug ]\n\n");
|
|
fprintf(stderr, "Where UDS_FILE provides the name of a unix domain socket file\n");
|
|
fprintf(stderr, "to import eBPF maps and the optional CMD denotes the command\n");
|
|
fprintf(stderr, "to be executed (default: \'%s\').\n", BPF_DEFAULT_CMD);
|
|
}
|
|
|
|
static int bpf_num_env_entries(void)
|
|
{
|
|
char **envp;
|
|
int num;
|
|
|
|
for (num = 0, envp = environ; *envp != NULL; envp++)
|
|
num++;
|
|
return num;
|
|
}
|
|
|
|
static int parse_bpf(struct exec_util *eu, int argc, char **argv)
|
|
{
|
|
char **argv_run = argv_default, **envp_run, *tmp;
|
|
int ret, i, env_old, env_num, env_map;
|
|
const char *bpf_uds_name = NULL;
|
|
int fds[BPF_SCM_MAX_FDS];
|
|
struct bpf_map_aux aux;
|
|
|
|
if (argc == 0)
|
|
return 0;
|
|
|
|
while (argc > 0) {
|
|
if (matches(*argv, "run") == 0) {
|
|
NEXT_ARG();
|
|
argv_run = argv;
|
|
break;
|
|
} else if (matches(*argv, "import") == 0) {
|
|
NEXT_ARG();
|
|
bpf_uds_name = *argv;
|
|
} else if (matches(*argv, "debug") == 0 ||
|
|
matches(*argv, "dbg") == 0) {
|
|
if (bpf_trace_pipe())
|
|
fprintf(stderr,
|
|
"No trace pipe, tracefs not mounted?\n");
|
|
return -1;
|
|
} else {
|
|
explain();
|
|
return -1;
|
|
}
|
|
|
|
NEXT_ARG_FWD();
|
|
}
|
|
|
|
if (!bpf_uds_name) {
|
|
fprintf(stderr, "bpf: No import parameter provided!\n");
|
|
explain();
|
|
return -1;
|
|
}
|
|
|
|
if (argv_run != argv_default && argc == 0) {
|
|
fprintf(stderr, "bpf: No run command provided!\n");
|
|
explain();
|
|
return -1;
|
|
}
|
|
|
|
memset(fds, 0, sizeof(fds));
|
|
memset(&aux, 0, sizeof(aux));
|
|
|
|
ret = bpf_recv_map_fds(bpf_uds_name, fds, &aux, ARRAY_SIZE(fds));
|
|
if (ret < 0) {
|
|
fprintf(stderr, "bpf: Could not receive fds!\n");
|
|
return -1;
|
|
}
|
|
|
|
if (aux.num_ent == 0) {
|
|
envp_run = environ;
|
|
goto out;
|
|
}
|
|
|
|
env_old = bpf_num_env_entries();
|
|
env_num = env_old + aux.num_ent + 2;
|
|
env_map = env_old + 1;
|
|
|
|
envp_run = malloc(sizeof(*envp_run) * env_num);
|
|
if (!envp_run) {
|
|
fprintf(stderr, "bpf: No memory left to allocate env!\n");
|
|
goto err;
|
|
}
|
|
|
|
for (i = 0; i < env_old; i++)
|
|
envp_run[i] = environ[i];
|
|
|
|
ret = asprintf(&tmp, "BPF_NUM_MAPS=%u", aux.num_ent);
|
|
if (ret < 0)
|
|
goto err_free;
|
|
|
|
envp_run[env_old] = tmp;
|
|
|
|
for (i = env_map; i < env_num - 1; i++) {
|
|
ret = asprintf(&tmp, "BPF_MAP%u=%u",
|
|
aux.ent[i - env_map].id,
|
|
fds[i - env_map]);
|
|
if (ret < 0)
|
|
goto err_free_env;
|
|
|
|
envp_run[i] = tmp;
|
|
}
|
|
|
|
envp_run[env_num - 1] = NULL;
|
|
out:
|
|
return execvpe(argv_run[0], argv_run, envp_run);
|
|
|
|
err_free_env:
|
|
for (--i; i >= env_old; i--)
|
|
free(envp_run[i]);
|
|
err_free:
|
|
free(envp_run);
|
|
err:
|
|
for (i = 0; i < aux.num_ent; i++)
|
|
close(fds[i]);
|
|
return -1;
|
|
}
|
|
|
|
struct exec_util bpf_exec_util = {
|
|
.id = "bpf",
|
|
.parse_eopt = parse_bpf,
|
|
};
|