xref: /linux/tools/testing/selftests/bpf/bpftool_helpers.c (revision eb71ab2bf72260054677e348498ba995a057c463)
1f21fae57SAlexis Lothoré (eBPF Foundation) // SPDX-License-Identifier: GPL-2.0-only
2f21fae57SAlexis Lothoré (eBPF Foundation) #include <unistd.h>
3f21fae57SAlexis Lothoré (eBPF Foundation) #include <string.h>
4f21fae57SAlexis Lothoré (eBPF Foundation) #include <stdbool.h>
5f21fae57SAlexis Lothoré (eBPF Foundation) 
63ed0bc2dSIhor Solodrai #include "bpf_util.h"
73ed0bc2dSIhor Solodrai #include "bpftool_helpers.h"
83ed0bc2dSIhor Solodrai 
9f21fae57SAlexis Lothoré (eBPF Foundation) #define BPFTOOL_PATH_MAX_LEN		64
10f21fae57SAlexis Lothoré (eBPF Foundation) #define BPFTOOL_FULL_CMD_MAX_LEN	512
11f21fae57SAlexis Lothoré (eBPF Foundation) 
12f21fae57SAlexis Lothoré (eBPF Foundation) #define BPFTOOL_DEFAULT_PATH		"tools/sbin/bpftool"
13f21fae57SAlexis Lothoré (eBPF Foundation) 
detect_bpftool_path(char * buffer,size_t size)143ed0bc2dSIhor Solodrai static int detect_bpftool_path(char *buffer, size_t size)
15f21fae57SAlexis Lothoré (eBPF Foundation) {
16f21fae57SAlexis Lothoré (eBPF Foundation) 	char tmp[BPFTOOL_PATH_MAX_LEN];
17*a2714e73SIhor Solodrai 	const char *env_path;
18*a2714e73SIhor Solodrai 
19*a2714e73SIhor Solodrai 	/* First, check if BPFTOOL environment variable is set */
20*a2714e73SIhor Solodrai 	env_path = getenv("BPFTOOL");
21*a2714e73SIhor Solodrai 	if (env_path && access(env_path, X_OK) == 0) {
22*a2714e73SIhor Solodrai 		strscpy(buffer, env_path, size);
23*a2714e73SIhor Solodrai 		return 0;
24*a2714e73SIhor Solodrai 	} else if (env_path) {
25*a2714e73SIhor Solodrai 		fprintf(stderr, "bpftool '%s' doesn't exist or is not executable\n", env_path);
26*a2714e73SIhor Solodrai 		return 1;
27*a2714e73SIhor Solodrai 	}
28f21fae57SAlexis Lothoré (eBPF Foundation) 
29f21fae57SAlexis Lothoré (eBPF Foundation) 	/* Check default bpftool location (will work if we are running the
30f21fae57SAlexis Lothoré (eBPF Foundation) 	 * default flavor of test_progs)
31f21fae57SAlexis Lothoré (eBPF Foundation) 	 */
32f21fae57SAlexis Lothoré (eBPF Foundation) 	snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "./%s", BPFTOOL_DEFAULT_PATH);
33f21fae57SAlexis Lothoré (eBPF Foundation) 	if (access(tmp, X_OK) == 0) {
343ed0bc2dSIhor Solodrai 		strscpy(buffer, tmp, size);
35f21fae57SAlexis Lothoré (eBPF Foundation) 		return 0;
36f21fae57SAlexis Lothoré (eBPF Foundation) 	}
37f21fae57SAlexis Lothoré (eBPF Foundation) 
38f21fae57SAlexis Lothoré (eBPF Foundation) 	/* Check alternate bpftool location (will work if we are running a
39f21fae57SAlexis Lothoré (eBPF Foundation) 	 * specific flavor of test_progs, e.g. cpuv4 or no_alu32)
40f21fae57SAlexis Lothoré (eBPF Foundation) 	 */
41f21fae57SAlexis Lothoré (eBPF Foundation) 	snprintf(tmp, BPFTOOL_PATH_MAX_LEN, "../%s", BPFTOOL_DEFAULT_PATH);
42f21fae57SAlexis Lothoré (eBPF Foundation) 	if (access(tmp, X_OK) == 0) {
433ed0bc2dSIhor Solodrai 		strscpy(buffer, tmp, size);
44f21fae57SAlexis Lothoré (eBPF Foundation) 		return 0;
45f21fae57SAlexis Lothoré (eBPF Foundation) 	}
46f21fae57SAlexis Lothoré (eBPF Foundation) 
47*a2714e73SIhor Solodrai 	fprintf(stderr, "Failed to detect bpftool path, use BPFTOOL env var to override\n");
48f21fae57SAlexis Lothoré (eBPF Foundation) 	return 1;
49f21fae57SAlexis Lothoré (eBPF Foundation) }
50f21fae57SAlexis Lothoré (eBPF Foundation) 
run_command(char * args,char * output_buf,size_t output_max_len)51f21fae57SAlexis Lothoré (eBPF Foundation) static int run_command(char *args, char *output_buf, size_t output_max_len)
52f21fae57SAlexis Lothoré (eBPF Foundation) {
53f21fae57SAlexis Lothoré (eBPF Foundation) 	static char bpftool_path[BPFTOOL_PATH_MAX_LEN] = {0};
54f21fae57SAlexis Lothoré (eBPF Foundation) 	bool suppress_output = !(output_buf && output_max_len);
55f21fae57SAlexis Lothoré (eBPF Foundation) 	char command[BPFTOOL_FULL_CMD_MAX_LEN];
56f21fae57SAlexis Lothoré (eBPF Foundation) 	FILE *f;
57f21fae57SAlexis Lothoré (eBPF Foundation) 	int ret;
58f21fae57SAlexis Lothoré (eBPF Foundation) 
59f21fae57SAlexis Lothoré (eBPF Foundation) 	/* Detect and cache bpftool binary location */
603ed0bc2dSIhor Solodrai 	if (bpftool_path[0] == 0 && detect_bpftool_path(bpftool_path, sizeof(bpftool_path)))
61f21fae57SAlexis Lothoré (eBPF Foundation) 		return 1;
62f21fae57SAlexis Lothoré (eBPF Foundation) 
63f21fae57SAlexis Lothoré (eBPF Foundation) 	ret = snprintf(command, BPFTOOL_FULL_CMD_MAX_LEN, "%s %s%s",
64f21fae57SAlexis Lothoré (eBPF Foundation) 		       bpftool_path, args,
65f21fae57SAlexis Lothoré (eBPF Foundation) 		       suppress_output ? " > /dev/null 2>&1" : "");
66f21fae57SAlexis Lothoré (eBPF Foundation) 
67f21fae57SAlexis Lothoré (eBPF Foundation) 	f = popen(command, "r");
68f21fae57SAlexis Lothoré (eBPF Foundation) 	if (!f)
69f21fae57SAlexis Lothoré (eBPF Foundation) 		return 1;
70f21fae57SAlexis Lothoré (eBPF Foundation) 
71f21fae57SAlexis Lothoré (eBPF Foundation) 	if (!suppress_output)
72f21fae57SAlexis Lothoré (eBPF Foundation) 		fread(output_buf, 1, output_max_len, f);
73f21fae57SAlexis Lothoré (eBPF Foundation) 	ret = pclose(f);
74f21fae57SAlexis Lothoré (eBPF Foundation) 
75f21fae57SAlexis Lothoré (eBPF Foundation) 	return ret;
76f21fae57SAlexis Lothoré (eBPF Foundation) }
77f21fae57SAlexis Lothoré (eBPF Foundation) 
run_bpftool_command(char * args)78f21fae57SAlexis Lothoré (eBPF Foundation) int run_bpftool_command(char *args)
79f21fae57SAlexis Lothoré (eBPF Foundation) {
80f21fae57SAlexis Lothoré (eBPF Foundation) 	return run_command(args, NULL, 0);
81f21fae57SAlexis Lothoré (eBPF Foundation) }
82f21fae57SAlexis Lothoré (eBPF Foundation) 
get_bpftool_command_output(char * args,char * output_buf,size_t output_max_len)83f21fae57SAlexis Lothoré (eBPF Foundation) int get_bpftool_command_output(char *args, char *output_buf, size_t output_max_len)
84f21fae57SAlexis Lothoré (eBPF Foundation) {
85f21fae57SAlexis Lothoré (eBPF Foundation) 	return run_command(args, output_buf, output_max_len);
86f21fae57SAlexis Lothoré (eBPF Foundation) }
87f21fae57SAlexis Lothoré (eBPF Foundation) 
88