Skip to content

Commit

Permalink
Make sockops cgroup a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ccanel committed May 8, 2024
1 parent 72e2c6d commit 2e90fb6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions ratemon/runtime/c/libratemon_interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,14 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
return new_fd;
}
unsigned int monitor_port_start_;
if (!read_env_uint(RM_MONITOR_PORT_START, &monitor_port_start_) ||
if (!read_env_uint(RM_MONITOR_PORT_START_KEY, &monitor_port_start_) ||
monitor_port_start_ >= 65536) {
lock_setup.unlock();
return new_fd;
}
monitor_port_start = (unsigned short)monitor_port_start_;
unsigned int monitor_port_end_;
if (!read_env_uint(RM_MONITOR_PORT_END, &monitor_port_end_) ||
if (!read_env_uint(RM_MONITOR_PORT_END_KEY, &monitor_port_end_) ||
monitor_port_end_ >= 65536) {
lock_setup.unlock();
return new_fd;
Expand Down
6 changes: 4 additions & 2 deletions ratemon/runtime/c/ratemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
#define RM_NUM_TO_SCHEDULE_KEY "RM_NUM_TO_SCHEDULE"
// Environment variable that specifies the start range of REMOTE ports to manage
// using scheduled RWND tuning.
#define RM_MONITOR_PORT_START "RM_MONITOR_PORT_START"
#define RM_MONITOR_PORT_END "RM_MONITOR_PORT_END"
#define RM_MONITOR_PORT_START_KEY "RM_MONITOR_PORT_START"
#define RM_MONITOR_PORT_END_KEY "RM_MONITOR_PORT_END"
// Path to cgroup for attaching sockops programs.
#define RM_CGROUP_KEY "RM_CGROUP"

// Key for use in flow-based maps.
struct rm_flow {
Expand Down
33 changes: 24 additions & 9 deletions ratemon/runtime/c/ratemon_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
// Signals whether the program should continue running.
static volatile bool run = true;

#define CG_PATH "/test_cg"

struct bpf_link *structops_link;
struct ratemon_sockops_bpf *sockops_skel;
struct ratemon_structops_bpf *structops_skel;
Expand Down Expand Up @@ -75,25 +73,25 @@ static int join_cgroup(const char *cgroup_path) {
return rc;
}

int prepare_sockops() {
int prepare_sockops(char *cg_path) {
// Open skeleton and load programs and maps.
sockops_skel = ratemon_sockops_bpf__open_and_load();
if (!sockops_skel) {
printf("ERROR: failed to open/load 'ratemon_sockops' BPF skeleton\n");
return 1;
}
// Join cgroup for sock_ops.
if (join_cgroup(CG_PATH) < 0) {
printf("ERROR: failed to join cgroup: %s\n", CG_PATH);
if (join_cgroup(cg_path) < 0) {
printf("ERROR: failed to join cgroup: %s\n", cg_path);
return 1;
}
int cg_fd = open(CG_PATH, O_RDONLY);
int cg_fd = open(cg_path, O_RDONLY);
if (cg_fd <= 0) {
printf("ERROR: failed to open cgroup: %s cg_fd: %d errno: %d\n", CG_PATH,
printf("ERROR: failed to open cgroup: %s cg_fd: %d errno: %d\n", cg_path,
cg_fd, errno);
return 1;
}
printf("INFO: opened cgroup: %s, cg_fd: %d, pid: %d\n", CG_PATH, cg_fd,
printf("INFO: opened cgroup: %s, cg_fd: %d, pid: %d\n", cg_path, cg_fd,
getpid());
// Attach sock_ops.
struct bpf_link *skops_link_win_scale =
Expand Down Expand Up @@ -122,6 +120,17 @@ int prepare_structops() {
return 0;
}

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);
return false;
}
*dest = val_str;
return true;
}

int main(int argc, char **argv) {
// Catch SIGINT to end the program.
struct sigaction action;
Expand All @@ -133,7 +142,13 @@ int main(int argc, char **argv) {
/* Set up libbpf errors and debug info callback */
libbpf_set_print(libbpf_print_fn);

if (prepare_sockops()) {
char cg_path[1024];
if (!read_env_str(RM_CGROUP_KEY, &cg_path)) {
RM_PRINTF("ERROR: failed to read cgroup path\n");
goto cleanup;
}

if (prepare_sockops(cg_path)) {
RM_PRINTF("ERROR: failed to set up sockops\n");
goto cleanup;
}
Expand Down

0 comments on commit 2e90fb6

Please sign in to comment.