Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpftune: add bpftune -q <query> client/server to allow local queries
create server for bpftune requests using ephemeral port,
port # stored locally in /var/run/bpftune/server-port.
Requests supported are

list: list requests supported
tuners: show tuners,states of each
tunables: show tunables
summary: summary of changes made

example:

$ /sbin/bpftune -q summary
Summary of changes made across all tuners:
Summary: scenario 'need to increase TCP buffer size(s)' occurred 2 times for tunable 'net.ipv4.tcp_wmem' in global ns. Need to increase buffer size(s) to maximize throughput
sysctl 'net.ipv4.tcp_wmem' changed from (4096 16384 16384 ) -> (4096 16384 25600 )

Also add query test

Signed-off-by: Alan Maguire <[email protected]>
  • Loading branch information
alan-maguire committed Dec 20, 2024
commit 2554af924ab37349ffa518c0e9fd7167b44b9546
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ $ sudo bpftune -s

On exit, bpftune will summarize any tuning done.

Queries of bpftune state can be done via `bpftune -q`.

## Tests

Tests are supplied for each tuner in the tests/ subdirectory.
Expand Down
16 changes: 16 additions & 0 deletions docs/bpftune.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ SYNOPSIS
{ [**-r** | **--learning_rate** ] learning_rate}
{ [**-R** | **--rollback** ]}
{ [**-S** | **--support** ]}
{ [**-p** | **--port** ] port}
{ [**-q** | **--query**] query}

DESCRIPTION
===========
Expand Down Expand Up @@ -85,3 +87,17 @@ OPTIONS
to the system. On exit, bpftune summarizes the changes made
and rolls back to the sysctl values that were set prior to
bpftune running.

-p, --port

TCP port to listen on for queries. If not specified, use an
ephemeral localhost port.

-q, --query

Query bpftune. Supported queries include

help - show supported queries
summary - show summary of changes made by tuners
tuners - show loaded tuners and their state
tunables - show supported tunables for loaded tuners
33 changes: 32 additions & 1 deletion include/bpftune/libbpftune.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <libgen.h>
#include <linux/types.h>
#include <pthread.h>

#include <bpftune/bpftune.h>

#include <bpf/bpf.h>
#include <bpf/libbpf.h>

#define BPFTUNE_RUN_DIR "/var/run/bpftune"
#define BPFTUNE_PORT_FILE BPFTUNE_RUN_DIR "/server-port"
#define BPFTUNER_CGROUP_DIR BPFTUNE_RUN_DIR "/cgroupv2"
#ifndef BPFTUNER_PREFIX_DIR
#define BPFTUNER_PREFIX_DIR "/usr"
Expand All @@ -65,19 +69,39 @@
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif

#define BPFTUNE_SERVER_MSG_MAX 65536

char *bpftune_state_string[] = {
"inactive",
"active",
"manual",
"gone",
};

/* level for bpftune tunable updates */
#define BPFTUNE_LOG_LEVEL LOG_NOTICE

/* write to buffer and log using nextlogfn */
struct bpftune_log_ctx_buf {
void (*nextlogfn)(void *ctx, int level, const char *fmt, va_list args);
pthread_t buf_thread;
char *buf;
size_t buf_off;
size_t buf_sz;
};

int bpftune_log_level(void);

void bpftune_log(int level, const char *fmt, ...);

void bpftune_log_stderr(void *ctx, int level, const char *fmt, va_list args);
void bpftune_log_syslog(void *ctx, int level, const char *fmt, va_list args);
void bpftune_log_buf(void *ctx, int level, const char *fmt, va_list args);

void bpftune_set_log(int level,
void (*logfn)(void *ctx, int level, const char *fmt,
va_list args));
va_list args),
void *ctx);
void bpftune_set_bpf_log(bool log);

void bpftune_log_bpf_err(int err, const char *fmt);
Expand All @@ -93,6 +117,13 @@ int bpftune_cgroup_init(const char *cgroup_path);
const char *bpftune_cgroup_name(void);
int bpftune_cgroup_fd(void);
void bpftune_cgroup_fini(void);

int bpftune_server_start(unsigned short port);
int bpftune_server_port(void);
void bpftune_server_stop(void);
int bpftune_server_request(struct sockaddr_in *server, const char *req,
char *buf, size_t buf_sz);

int bpftuner_cgroup_attach(struct bpftuner *tuner, const char *prog_name,
enum bpf_attach_type attach_type);
void bpftuner_cgroup_detach(struct bpftuner *tuner, const char *prog_name,
Expand Down
44 changes: 41 additions & 3 deletions src/bpftune.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <dirent.h>
#include <libgen.h>
#include <linux/types.h>
#include <linux/limits.h>
#include <netinet/in.h>
#include <pthread.h>
#include <sys/inotify.h>
#include <ftw.h>
Expand Down Expand Up @@ -75,6 +77,7 @@ static void cleanup(int sig)
{
exiting = true;
bpftune_log(LOG_DEBUG, "cleaning up, got signal %d\n", sig);
bpftune_server_stop();
bpftune_ring_buffer_fini(ring_buffer);
if (use_stderr)
fflush(stderr);
Expand Down Expand Up @@ -239,6 +242,8 @@ void do_help(void)
" { -L|--legacy}\n"
" { -h|--help}}\n"
" { -l|--library_path library_path}\n"
" { -p|--port port}\n"
" { -q|--query query}\n"
" { -r|--learning_rate learning_rate}\n"
" { -R|--rollback}\n"
" { -s|--stderr}\n"
Expand Down Expand Up @@ -292,6 +297,8 @@ int main(int argc, char *argv[])
{ "help", no_argument, NULL, 'h' },
{ "libdir", required_argument, NULL, 'l' },
{ "learning_rate", required_argument, NULL, 'r' },
{ "port", required_argument, NULL, 'p' },
{ "query", required_argument, NULL, 'q' },
{ "rollback", no_argument, NULL, 'R' },
{ "stderr", no_argument, NULL, 's' },
{ "support", no_argument, NULL, 'S' },
Expand All @@ -306,12 +313,15 @@ int main(int argc, char *argv[])
int log_level = BPFTUNE_LOG_LEVEL;
struct sigaction sa = {}, oldsa = {};
bool support_only = false;
bool client = false;
char *query = NULL;
int interval = 100;
unsigned short port = 0;
int err, opt;

bin_name = argv[0];

while ((opt = getopt_long(argc, argv, "a:c:dDhl:Lr:RsSV", options, NULL))
while ((opt = getopt_long(argc, argv, "a:c:dDhl:Lr:p:q:RsSV", options, NULL))
>= 0) {
switch (opt) {
case 'a':
Expand Down Expand Up @@ -347,6 +357,14 @@ int main(int argc, char *argv[])
return 1;
}
break;
case 'p':
port = (unsigned short)atoi(optarg);
break;
case 'q':
query = optarg;
client = true;
use_stderr = true;
break;
case 'R':
rollback = true;
break;
Expand All @@ -367,7 +385,24 @@ int main(int argc, char *argv[])
}
}

bpftune_set_log(log_level, use_stderr ? bpftune_log_stderr : bpftune_log_syslog);
bpftune_set_log(log_level,
use_stderr ? bpftune_log_stderr : bpftune_log_syslog,
NULL);

if (client) {
char buf[BPFTUNE_SERVER_MSG_MAX];
struct sockaddr_in server;

memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
server.sin_port = htons(port);

err = bpftune_server_request(&server, query, buf, sizeof(buf));
if (err == 0)
fprintf(stdout, "%s\n", buf);
return err;
}

bpftune_set_learning_rate(rate);

Expand All @@ -382,7 +417,7 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);

/* need to setup cgroup prior to probe as probe program uses sysctl */
err = mkdir(BPFTUNE_RUN_DIR, 0700);
err = mkdir(BPFTUNE_RUN_DIR, 0755);
if (err && errno != EEXIST) {
bpftune_log(BPFTUNE_LOG_LEVEL, "could not create '%s': %s\n",
BPFTUNE_RUN_DIR, strerror(errno));
Expand All @@ -401,6 +436,9 @@ int main(int argc, char *argv[])
if (support_only)
return 0;

if (bpftune_server_start(port) != 0)
exit(EXIT_FAILURE);

bpftune_cap_drop();

err = init(BPFTUNER_LIB_DIR);
Expand Down
Loading