mirror_iproute2/tc/tc_exec.c
Daniel Borkmann 4bd624467b tc: built-in eBPF exec proxy
This work follows upon commit 6256f8c9e4 ("tc, bpf: finalize eBPF
support for cls and act front-end") and takes up the idea proposed by
Hannes Frederic Sowa to spawn a shell (or any other command) that holds
generated eBPF map file descriptors.

File descriptors, based on their id, are being fetched from the same
unix domain socket as demonstrated in the bpf_agent, the shell spawned
via execvpe(2) and the map fds passed over the environment, and thus
are made available to applications in the fashion of std{in,out,err}
for read/write access, for example in case of iproute2's examples/bpf/:

  # env | grep BPF
  BPF_NUM_MAPS=3
  BPF_MAP1=6        <- BPF_MAP_ID_QUEUE (id 1)
  BPF_MAP0=5        <- BPF_MAP_ID_PROTO (id 0)
  BPF_MAP2=7        <- BPF_MAP_ID_DROPS (id 2)

  # ls -la /proc/self/fd
  [...]
  lrwx------. 1 root root 64 Apr 14 16:46 0 -> /dev/pts/4
  lrwx------. 1 root root 64 Apr 14 16:46 1 -> /dev/pts/4
  lrwx------. 1 root root 64 Apr 14 16:46 2 -> /dev/pts/4
  [...]
  lrwx------. 1 root root 64 Apr 14 16:46 5 -> anon_inode:bpf-map
  lrwx------. 1 root root 64 Apr 14 16:46 6 -> anon_inode:bpf-map
  lrwx------. 1 root root 64 Apr 14 16:46 7 -> anon_inode:bpf-map

The advantage (as opposed to the direct/native usage) is that now the
shell is map fd owner and applications can terminate and easily reattach
to descriptors w/o any kernel changes. Moreover, multiple applications
can easily read/write eBPF maps simultaneously.

To further allow users for experimenting with that, next step is to add
a small helper that can get along with simple data types, so that also
shell scripts can make use of bpf syscall, f.e to read/write into maps.

Generally, this allows for prepopulating maps, or any runtime altering
which could influence eBPF program behaviour (f.e. different run-time
classifications, skb modifications, ...), dumping of statistics, etc.

Reference: http://thread.gmane.org/gmane.linux.network/357471/focus=357860
Suggested-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
2015-04-27 16:39:23 -07:00

110 lines
2.1 KiB
C

/*
* tc_exec.c "tc exec".
*
* This program is free software; you can redistribute 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 <stdlib.h>
#include <dlfcn.h>
#include "utils.h"
#include "tc_util.h"
#include "tc_common.h"
static struct exec_util *exec_list;
static void *BODY = NULL;
static void usage(void)
{
fprintf(stderr, "Usage: tc exec [ EXEC_TYPE ] [ help | OPTIONS ]\n");
fprintf(stderr, "Where:\n");
fprintf(stderr, "EXEC_TYPE := { bpf | etc. }\n");
fprintf(stderr, "OPTIONS := ... try tc exec <desired EXEC_KIND> help\n");
}
static int parse_noeopt(struct exec_util *eu, int argc, char **argv)
{
if (argc) {
fprintf(stderr, "Unknown exec \"%s\", hence option \"%s\" "
"is unparsable\n", eu->id, *argv);
return -1;
}
return 0;
}
static struct exec_util *get_exec_kind(const char *name)
{
struct exec_util *eu;
char buf[256];
void *dlh;
for (eu = exec_list; eu; eu = eu->next)
if (strcmp(eu->id, name) == 0)
return eu;
snprintf(buf, sizeof(buf), "%s/e_%s.so", get_tc_lib(), name);
dlh = dlopen(buf, RTLD_LAZY);
if (dlh == NULL) {
dlh = BODY;
if (dlh == NULL) {
dlh = BODY = dlopen(NULL, RTLD_LAZY);
if (dlh == NULL)
goto noexist;
}
}
snprintf(buf, sizeof(buf), "%s_exec_util", name);
eu = dlsym(dlh, buf);
if (eu == NULL)
goto noexist;
reg:
eu->next = exec_list;
exec_list = eu;
return eu;
noexist:
eu = malloc(sizeof(*eu));
if (eu) {
memset(eu, 0, sizeof(*eu));
strncpy(eu->id, name, sizeof(eu->id) - 1);
eu->parse_eopt = parse_noeopt;
goto reg;
}
return eu;
}
int do_exec(int argc, char **argv)
{
struct exec_util *eu;
char kind[16];
if (argc < 1) {
fprintf(stderr, "No command given, try \"tc exec help\".\n");
return -1;
}
if (matches(*argv, "help") == 0) {
usage();
return 0;
}
memset(kind, 0, sizeof(kind));
strncpy(kind, *argv, sizeof(kind) - 1);
eu = get_exec_kind(kind);
argc--;
argv++;
return eu->parse_eopt(eu, argc, argv);
}