Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix global namespace disable
when we change a sysctl external to bpftune, the aim is to disable the
auto-tuning for the associated namespace. however in the global ns
we were disabling tuning for global and non-global namespaces via
bpftuner_fini().  Ensure we disable global namespace only, and that we
only send events for tuners that are enabled and - if namespaced -
if the namespace is enabled also.

Reported by: Roger Knobbe (https://github.com/rknobbe)
Signed-off-by: Alan Maguire <[email protected]>
  • Loading branch information
alan-maguire committed Nov 26, 2024
commit 5f6575852f4eac742665cd20e55d28782820e8c9
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ $(TUNER_LIBS): $(OPATH)libbpftune.so $(TUNER_OBJS)
$(CC) $(CFLAGS) -shared -o $(@) $(patsubst $(OPATH)%.so,%.c,$(@)) \
$(LDLIBS) -lbpftune $(LDFLAGS)

$(OPATH)libbpftune.o: probe.skel.h probe.skel.legacy.h probe.skel.nobtf.h
$(OPATH)libbpftune.o: probe.skel.h probe.skel.legacy.h probe.skel.nobtf.h libbpftune.c
$(QUIET_CC)$(CC) $(CFLAGS) -c libbpftune.c -o $@

$(OPATH)bpftune.o: $(OPATH)libbpftune.so
Expand Down
53 changes: 38 additions & 15 deletions src/libbpftune.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,21 @@ void bpftuner_bpf_fini(struct bpftuner *tuner)

static struct bpftuner *bpftune_tuners[BPFTUNE_MAX_TUNERS];

static unsigned long global_netns_cookie;

static void bpftune_global_netns_init(void)
{
unsigned long cookie = 0;

if (global_netns_cookie || !netns_cookie_supported)
return;
if (!bpftune_netns_info(getpid(), NULL, &cookie)) {
global_netns_cookie = cookie;
bpftune_log(LOG_DEBUG, "global netns cookie is %ld\n",
global_netns_cookie);
}
}

/* add a tuner to the list of tuners, or replace existing inactive tuner.
* If successful, call init().
*/
Expand Down Expand Up @@ -703,6 +718,12 @@ struct bpftuner *bpftuner_init(const char *path)
free(tuner);
return NULL;
}
if (!global_netns_cookie)
bpftune_global_netns_init();
if (global_netns_cookie) {
tuner->netns.netns_cookie = global_netns_cookie;
tuner->netns.state = BPFTUNE_ACTIVE;
}
tuner->id = bpftune_num_tuners;
tuner->state = BPFTUNE_ACTIVE;
bpftune_tuners[bpftune_num_tuners++] = tuner;
Expand All @@ -711,8 +732,6 @@ struct bpftuner *bpftuner_init(const char *path)
return tuner;
}

static unsigned long global_netns_cookie;

static void bpftuner_scenario_log(struct bpftuner *tuner, unsigned int tunable,
unsigned int scenario, int netns_fd,
bool summary,
Expand Down Expand Up @@ -761,6 +780,7 @@ void bpftune_set_learning_rate(unsigned short rate)

static int bpftune_ringbuf_event_read(void *ctx, void *data, size_t size)
{
const char *status = "skipped due to inactive tuner/netns";
struct bpftune_event *event = data;
struct bpftuner *tuner;

Expand All @@ -777,16 +797,22 @@ static int bpftune_ringbuf_event_read(void *ctx, void *data, size_t size)
bpftune_log(LOG_ERR, "no tuner for id %d\n", event->tuner_id);
return 0;
}
/* only send events to active tuners/netns */
if (tuner->state == BPFTUNE_ACTIVE) {
struct bpftuner_netns *netns = bpftuner_netns_from_cookie(event->tuner_id, event->netns_cookie);

if (!netns || netns->state != BPFTUNE_MANUAL) {
tuner->event_handler(tuner, event, ctx);
status = "sent";
}
}
bpftune_log(LOG_DEBUG,
"event scenario [%d] for tuner %s[%d] netns %ld (%s)\n",
"event scenario [%d] for tuner %s[%d] netns %lu (%s) %s\n",
event->scenario_id, tuner->name, tuner->id,
event->netns_cookie,
event->netns_cookie && event->netns_cookie != global_netns_cookie ?
"non-global netns" : "global netns");
/* only send events to active tuners */
if (tuner->state == BPFTUNE_ACTIVE)
tuner->event_handler(tuner, event, ctx);

"non-global netns" : "global netns",
status);
return 0;
}

Expand Down Expand Up @@ -1330,7 +1356,7 @@ int bpftune_netns_info(int pid, int *fd, unsigned long *cookie)

static int bpftune_netns_find(unsigned long cookie)
{
unsigned long netns_cookie;
unsigned long netns_cookie = 0;
struct bpftuner *t;
struct mntent *ent;
FILE *mounts;
Expand Down Expand Up @@ -1446,11 +1472,8 @@ int bpftune_netns_init_all(void)
if (!netns_cookie_supported)
return 0;

if (!bpftune_netns_info(getpid(), NULL, &cookie)) {
global_netns_cookie = cookie;
bpftune_log(LOG_DEBUG, "global netns cookie is %ld\n",
global_netns_cookie);
}
bpftune_global_netns_init();

return bpftune_netns_find(0);
}

Expand Down Expand Up @@ -1480,7 +1503,7 @@ void bpftuner_netns_fini(struct bpftuner *tuner, unsigned long cookie, enum bpft
{
struct bpftuner_netns *netns, *prev = NULL;

if (cookie == 0 || cookie == global_netns_cookie) {
if (cookie == 0 || (cookie == global_netns_cookie && !netns_cookie_supported)) {
bpftuner_fini(tuner, state);
return;
}
Expand Down