Commit 4939b284 authored by John Fastabend's avatar John Fastabend Committed by Alexei Starovoitov
Browse files

bpf, selftests: Use single cgroup helpers for both test_sockmap/progs



Nearly every user of cgroup helpers does the same sequence of API calls. So
push these into a single helper cgroup_setup_and_join. The cases that do
a bit of extra logic are test_progs which currently uses an env variable
to decide if it needs to setup the cgroup environment or can use an
existingi environment. And then tests that are doing cgroup tests
themselves. We skip these cases for now.

Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/159623335418.30208.15807461815525100199.stgit@john-XPS-13-9370
parent ffba964e
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -290,3 +290,26 @@ unsigned long long get_cgroup_id(const char *path)
	free(fhp);
	return ret;
}

int cgroup_setup_and_join(const char *path) {
	int cg_fd;

	if (setup_cgroup_environment()) {
		fprintf(stderr, "Failed to setup cgroup environment\n");
		return -EINVAL;
	}

	cg_fd = create_and_get_cgroup(path);
	if (cg_fd < 0) {
		fprintf(stderr, "Failed to create test cgroup\n");
		cleanup_cgroup_environment();
		return cg_fd;
	}

	if (join_cgroup(path)) {
		fprintf(stderr, "Failed to join cgroup\n");
		cleanup_cgroup_environment();
		return -EINVAL;
	}
	return cg_fd;
}
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
	__FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)


int cgroup_setup_and_join(const char *path);
int create_and_get_cgroup(const char *path);
int join_cgroup(const char *path);
int setup_cgroup_environment(void);
+2 −12
Original line number Diff line number Diff line
@@ -58,20 +58,10 @@ int main(int argc, char **argv)
	int exit_code = 1;
	char buf[256];

	err = setup_cgroup_environment();
	if (CHECK(err, "setup_cgroup_environment", "err %d errno %d\n", err,
		  errno))
	cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
	if (CHECK(cgroup_fd < 0, "cgroup_setup_and_join", "err %d errno %d\n", cgroup_fd, errno))
		return 1;

	cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
	if (CHECK(cgroup_fd < 0, "create_and_get_cgroup", "err %d errno %d\n",
		  cgroup_fd, errno))
		goto cleanup_cgroup_env;

	err = join_cgroup(TEST_CGROUP);
	if (CHECK(err, "join_cgroup", "err %d errno %d\n", err, errno))
		goto cleanup_cgroup_env;

	err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
	if (CHECK(err, "bpf_prog_load", "err %d errno %d\n", err, errno))
		goto cleanup_cgroup_env;
+1 −16
Original line number Diff line number Diff line
@@ -74,22 +74,7 @@ int main(int argc, char **argv)
		goto out;
	}

	if (setup_cgroup_environment()) {
		printf("Failed to setup cgroup environment\n");
		goto err;
	}

	/* Create a cgroup, get fd, and join it */
	cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
	if (cgroup_fd < 0) {
		printf("Failed to create test cgroup\n");
		goto err;
	}

	if (join_cgroup(TEST_CGROUP)) {
		printf("Failed to join cgroup\n");
		goto err;
	}
	cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);

	/* Attach the bpf program */
	if (bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS, 0)) {
+2 −13
Original line number Diff line number Diff line
@@ -33,21 +33,10 @@ int main(int argc, char **argv)
		goto out;
	}

	if (setup_cgroup_environment()) {
		printf("Failed to load DEV_CGROUP program\n");
		goto err;
	}

	/* Create a cgroup, get fd, and join it */
	cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
	cgroup_fd = cgroup_setup_and_join(TEST_CGROUP);
	if (cgroup_fd < 0) {
		printf("Failed to create test cgroup\n");
		goto err;
	}

	if (join_cgroup(TEST_CGROUP)) {
		printf("Failed to join cgroup\n");
		goto err;
		goto out;
	}

	/* Attach bpf program */
Loading