From a4d7aacfd3ea2f8b1247e150ba93a1eb91620713 Mon Sep 17 00:00:00 2001 From: Christopher Canel Date: Wed, 8 May 2024 18:07:10 +0000 Subject: [PATCH] Fix cgroup bug, always print in ratemon_main --- ratemon/runtime/c/Makefile | 3 ++- ratemon/runtime/c/ratemon_main.c | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/ratemon/runtime/c/Makefile b/ratemon/runtime/c/Makefile index e2b8ba5..65eec2b 100644 --- a/ratemon/runtime/c/Makefile +++ b/ratemon/runtime/c/Makefile @@ -25,6 +25,7 @@ CFLAGS := -g -std=c17 -Wall -Wextra ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS) # If this is not set with an environment variable, then hardcode it. RM_IFACE ?= eno4 +RM_CGROUP ?= /test_cg CXX := g++ CXXFLAGS := -g -std=c++20 -Wall -Wextra @@ -149,7 +150,7 @@ attach_tc_and_run: ratemon_main $(OUTPUT)/ratemon_tc.bpf.o sudo tc filter add dev $(RM_IFACE) egress bpf direct-action obj $(OUTPUT)/ratemon_tc.bpf.o sec tc/egress sudo bpftool map pin name flow_to_rwnd /sys/fs/bpf/flow_to_rwnd sudo bpftool map pin name flow_to_win_sca /sys/fs/bpf/flow_to_win_scale - sudo ./ratemon_main || true + sudo RM_CGROUP=$(RM_CGROUP) ./ratemon_main || true for id in `sudo bpftool struct_ops list | cut -d":" -f1`; do sudo bpftool struct_ops unregister id $$id; done sudo tc filter del dev $(RM_IFACE) egress sudo rm -f /sys/fs/bpf/flow_to_rwnd diff --git a/ratemon/runtime/c/ratemon_main.c b/ratemon/runtime/c/ratemon_main.c index 4609a43..39e7d0f 100644 --- a/ratemon/runtime/c/ratemon_main.c +++ b/ratemon/runtime/c/ratemon_main.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "ratemon.h" @@ -35,16 +36,16 @@ static int libbpf_print_fn(enum libbpf_print_level level, const char *format, void sigint_handler(int signum) { switch (signum) { case SIGINT: - RM_PRINTF("INFO: caught SIGINT\n"); + printf("INFO: caught SIGINT\n"); run = false; - RM_PRINTF("Resetting old SIGINT handler\n"); + printf("Resetting old SIGINT handler\n"); sigaction(SIGINT, &oldact, NULL); break; default: - RM_PRINTF("ERROR: caught signal %d\n", signum); + printf("ERROR: caught signal %d\n", signum); break; } - RM_PRINTF("INFO: re-raising signal %d\n", signum); + printf("INFO: re-raising signal %d\n", signum); raise(signum); } @@ -120,14 +121,14 @@ int prepare_structops() { return 0; } -bool read_env_str(const char *key, char **dest) { +bool read_env_str(const char *key, char *dest) { // Read an environment variable a char *. char *val_str = getenv(key); if (val_str == NULL) { - RM_PRINTF("ERROR: failed to query environment variable '%s'\n", key); + printf("ERROR: failed to query environment variable '%s'\n", key); return false; } - *dest = val_str; + strcpy(dest, val_str); return true; } @@ -143,17 +144,17 @@ int main(int argc, char **argv) { libbpf_set_print(libbpf_print_fn); char cg_path[1024]; - if (!read_env_str(RM_CGROUP_KEY, &cg_path)) { - RM_PRINTF("ERROR: failed to read cgroup path\n"); + if (!read_env_str(RM_CGROUP_KEY, cg_path)) { + printf("ERROR: failed to read cgroup path\n"); goto cleanup; } if (prepare_sockops(cg_path)) { - RM_PRINTF("ERROR: failed to set up sockops\n"); + printf("ERROR: failed to set up sockops\n"); goto cleanup; } if (prepare_structops()) { - RM_PRINTF("ERROR: failed to set up structops\n"); + printf("ERROR: failed to set up structops\n"); goto cleanup; } @@ -174,5 +175,5 @@ int main(int argc, char **argv) { bpf_link__destroy(structops_link); ratemon_sockops_bpf__destroy(sockops_skel); ratemon_structops_bpf__destroy(structops_skel); - return 1; + return 0; }