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
libbpftune: support quiet mode for bpftune_netns_set()
when matching netns cookies with netns, we iterate over all
netns and setns() to retrieve the cookie. This can fail but
currently we dump error messages in such cases. Error messaging
should be reserved for the case where we are entering the netns
to either read or write sysctls.  So add a bool quiet param
to bpftune_netns_set().

Signed-off-by: Alan Maguire <[email protected]>
Reported-by: Rob Landers <[email protected]>
  • Loading branch information
alan-maguire committed Nov 20, 2024
commit 28a51e14e8f8d17aae812314be0049424fb2c8c2
2 changes: 1 addition & 1 deletion include/bpftune/libbpftune.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ int bpftune_sysctl_read(int netns_fd, const char *name, long *values);
int bpftune_sysctl_write(int netns_fd, const char *name, __u8 num_values, long *values);

bool bpftune_netns_cookie_supported(void);
int bpftune_netns_set(int fd, int *orig_fd);
int bpftune_netns_set(int fd, int *orig_fd, bool quiet);
int bpftune_netns_info(int pid, int *fd, unsigned long *cookie);
int bpftune_netns_init_all(void);
void bpftuner_netns_init(struct bpftuner *tuner, unsigned long cookie);
Expand Down
17 changes: 9 additions & 8 deletions src/libbpftune.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ int bpftune_sysctl_read(int netns_fd, const char *name, long *values)

bpftune_sysctl_name_to_path(name, path, sizeof(path));

err = bpftune_netns_set(netns_fd, &orig_netns_fd);
err = bpftune_netns_set(netns_fd, &orig_netns_fd, false);
if (err < 0)
goto out_unset;

Expand Down Expand Up @@ -898,7 +898,7 @@ int bpftune_sysctl_read(int netns_fd, const char *name, long *values)
}

out:
bpftune_netns_set(orig_netns_fd, NULL);
bpftune_netns_set(orig_netns_fd, NULL, true);
out_unset:
bpftune_cap_drop();
return err ? err : num_values;
Expand All @@ -920,7 +920,7 @@ int bpftune_sysctl_write(int netns_fd, const char *name, __u8 num_values, long *
err = bpftune_cap_add();
if (err)
return err;
err = bpftune_netns_set(netns_fd, &orig_netns_fd);
err = bpftune_netns_set(netns_fd, &orig_netns_fd, false);
if (err < 0)
goto out_unset;

Expand Down Expand Up @@ -955,7 +955,7 @@ int bpftune_sysctl_write(int netns_fd, const char *name, __u8 num_values, long *
name, i, values[i]);
}
out:
bpftune_netns_set(orig_netns_fd, NULL);
bpftune_netns_set(orig_netns_fd, NULL, true);
out_unset:
bpftune_cap_drop();
return err;
Expand Down Expand Up @@ -1217,7 +1217,7 @@ static int bpftune_netns_fd(int netns_pid)
}

/* sets original netns fd if orig_fd is non-NULL */
int bpftune_netns_set(int new_fd, int *orig_fd)
int bpftune_netns_set(int new_fd, int *orig_fd, bool quiet)
{
int fd = 0, err = 0;

Expand All @@ -1241,7 +1241,8 @@ int bpftune_netns_set(int new_fd, int *orig_fd)
err = setns(new_fd, CLONE_NEWNET);
if (err < 0) {
err = -errno;
bpftune_log(LOG_ERR, "could not %s ns(%d): %s\n",
bpftune_log(quiet ? LOG_DEBUG : LOG_ERR,
"could not %s ns(%d): %s\n",
orig_fd ? "set" : "restore",
new_fd, strerror(-err));
}
Expand Down Expand Up @@ -1276,7 +1277,7 @@ int bpftune_netns_info(int pid, int *fd, unsigned long *cookie)
return netns_fd;
}

err = bpftune_netns_set(netns_fd, &orig_netns_fd);
err = bpftune_netns_set(netns_fd, &orig_netns_fd, true);
if (!err) {
int s = socket(AF_INET, SOCK_STREAM, 0);

Expand All @@ -1299,7 +1300,7 @@ int bpftune_netns_info(int pid, int *fd, unsigned long *cookie)

close(s);
}
bpftune_netns_set(orig_netns_fd, NULL);
bpftune_netns_set(orig_netns_fd, NULL, true);

if (ret == 0) {
if (fdnew && fd)
Expand Down