2015-03-25 22:49:24 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <linux/bpf.h>
|
2015-06-13 05:39:12 +03:00
|
|
|
#include <string.h>
|
bpf: fix multiple issues in selftest suite and samples
1) The test_lru_map and test_lru_dist fails building on my machine since
the sys/resource.h header is not included.
2) test_verifier fails in one test case where we try to call an invalid
function, since the verifier log output changed wrt printing function
names.
3) Current selftest suite code relies on sysconf(_SC_NPROCESSORS_CONF) for
retrieving the number of possible CPUs. This is broken at least in our
scenario and really just doesn't work.
glibc tries a number of things for retrieving _SC_NPROCESSORS_CONF.
First it tries equivalent of /sys/devices/system/cpu/cpu[0-9]* | wc -l,
if that fails, depending on the config, it either tries to count CPUs
in /proc/cpuinfo, or returns the _SC_NPROCESSORS_ONLN value instead.
If /proc/cpuinfo has some issue, it returns just 1 worst case. This
oddity is nothing new [1], but semantics/behaviour seems to be settled.
_SC_NPROCESSORS_ONLN will parse /sys/devices/system/cpu/online, if
that fails it looks into /proc/stat for cpuX entries, and if also that
fails for some reason, /proc/cpuinfo is consulted (and returning 1 if
unlikely all breaks down).
While that might match num_possible_cpus() from the kernel in some
cases, it's really not guaranteed with CPU hotplugging, and can result
in a buffer overflow since the array in user space could have too few
number of slots, and on perpcu map lookup, the kernel will write beyond
that memory of the value buffer.
William Tu reported such mismatches:
[...] The fact that sysconf(_SC_NPROCESSORS_CONF) != num_possible_cpu()
happens when CPU hotadd is enabled. For example, in Fusion when
setting vcpu.hotadd = "TRUE" or in KVM, setting ./qemu-system-x86_64
-smp 2, maxcpus=4 ... the num_possible_cpu() will be 4 and sysconf()
will be 2 [2]. [...]
Documentation/cputopology.txt says /sys/devices/system/cpu/possible
outputs cpu_possible_mask. That is the same as in num_possible_cpus(),
so first step would be to fix the _SC_NPROCESSORS_CONF calls with our
own implementation. Later, we could add support to bpf(2) for passing
a mask via CPU_SET(3), for example, to just select a subset of CPUs.
BPF samples code needs this fix as well (at least so that people stop
copying this). Thus, define bpf_num_possible_cpus() once in selftests
and import it from there for the sample code to avoid duplicating it.
The remaining sysconf(_SC_NPROCESSORS_CONF) in samples are unrelated.
After all three issues are fixed, the test suite runs fine again:
# make run_tests | grep self
selftests: test_verifier [PASS]
selftests: test_maps [PASS]
selftests: test_lru_map [PASS]
selftests: test_kmod.sh [PASS]
[1] https://www.sourceware.org/ml/libc-alpha/2011-06/msg00079.html
[2] https://www.mail-archive.com/netdev@vger.kernel.org/msg121183.html
Fixes: 3059303f59cf ("samples/bpf: update tracex[23] examples to use per-cpu maps")
Fixes: 86af8b4191d2 ("Add sample for adding simple drop program to link")
Fixes: df570f577231 ("samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_ARRAY")
Fixes: e15596717948 ("samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_HASH")
Fixes: ebb676daa1a3 ("bpf: Print function name in addition to function id")
Fixes: 5db58faf989f ("bpf: Add tests for the LRU bpf_htab")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Cc: William Tu <u9012063@gmail.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-11-26 03:28:09 +03:00
|
|
|
|
2015-03-25 22:49:24 +03:00
|
|
|
#include "libbpf.h"
|
|
|
|
#include "bpf_load.h"
|
bpf: fix multiple issues in selftest suite and samples
1) The test_lru_map and test_lru_dist fails building on my machine since
the sys/resource.h header is not included.
2) test_verifier fails in one test case where we try to call an invalid
function, since the verifier log output changed wrt printing function
names.
3) Current selftest suite code relies on sysconf(_SC_NPROCESSORS_CONF) for
retrieving the number of possible CPUs. This is broken at least in our
scenario and really just doesn't work.
glibc tries a number of things for retrieving _SC_NPROCESSORS_CONF.
First it tries equivalent of /sys/devices/system/cpu/cpu[0-9]* | wc -l,
if that fails, depending on the config, it either tries to count CPUs
in /proc/cpuinfo, or returns the _SC_NPROCESSORS_ONLN value instead.
If /proc/cpuinfo has some issue, it returns just 1 worst case. This
oddity is nothing new [1], but semantics/behaviour seems to be settled.
_SC_NPROCESSORS_ONLN will parse /sys/devices/system/cpu/online, if
that fails it looks into /proc/stat for cpuX entries, and if also that
fails for some reason, /proc/cpuinfo is consulted (and returning 1 if
unlikely all breaks down).
While that might match num_possible_cpus() from the kernel in some
cases, it's really not guaranteed with CPU hotplugging, and can result
in a buffer overflow since the array in user space could have too few
number of slots, and on perpcu map lookup, the kernel will write beyond
that memory of the value buffer.
William Tu reported such mismatches:
[...] The fact that sysconf(_SC_NPROCESSORS_CONF) != num_possible_cpu()
happens when CPU hotadd is enabled. For example, in Fusion when
setting vcpu.hotadd = "TRUE" or in KVM, setting ./qemu-system-x86_64
-smp 2, maxcpus=4 ... the num_possible_cpu() will be 4 and sysconf()
will be 2 [2]. [...]
Documentation/cputopology.txt says /sys/devices/system/cpu/possible
outputs cpu_possible_mask. That is the same as in num_possible_cpus(),
so first step would be to fix the _SC_NPROCESSORS_CONF calls with our
own implementation. Later, we could add support to bpf(2) for passing
a mask via CPU_SET(3), for example, to just select a subset of CPUs.
BPF samples code needs this fix as well (at least so that people stop
copying this). Thus, define bpf_num_possible_cpus() once in selftests
and import it from there for the sample code to avoid duplicating it.
The remaining sysconf(_SC_NPROCESSORS_CONF) in samples are unrelated.
After all three issues are fixed, the test suite runs fine again:
# make run_tests | grep self
selftests: test_verifier [PASS]
selftests: test_maps [PASS]
selftests: test_lru_map [PASS]
selftests: test_kmod.sh [PASS]
[1] https://www.sourceware.org/ml/libc-alpha/2011-06/msg00079.html
[2] https://www.mail-archive.com/netdev@vger.kernel.org/msg121183.html
Fixes: 3059303f59cf ("samples/bpf: update tracex[23] examples to use per-cpu maps")
Fixes: 86af8b4191d2 ("Add sample for adding simple drop program to link")
Fixes: df570f577231 ("samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_ARRAY")
Fixes: e15596717948 ("samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_HASH")
Fixes: ebb676daa1a3 ("bpf: Print function name in addition to function id")
Fixes: 5db58faf989f ("bpf: Add tests for the LRU bpf_htab")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Cc: William Tu <u9012063@gmail.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-11-26 03:28:09 +03:00
|
|
|
#include "bpf_util.h"
|
2015-03-25 22:49:24 +03:00
|
|
|
|
|
|
|
#define MAX_INDEX 64
|
|
|
|
#define MAX_STARS 38
|
|
|
|
|
|
|
|
static void stars(char *str, long val, long max, int width)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < (width * val / max) - 1 && i < width - 1; i++)
|
|
|
|
str[i] = '*';
|
|
|
|
if (val > max)
|
|
|
|
str[i - 1] = '+';
|
|
|
|
str[i] = '\0';
|
|
|
|
}
|
|
|
|
|
2015-06-13 05:39:12 +03:00
|
|
|
struct task {
|
|
|
|
char comm[16];
|
|
|
|
__u64 pid_tgid;
|
|
|
|
__u64 uid_gid;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hist_key {
|
|
|
|
struct task t;
|
|
|
|
__u32 index;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SIZE sizeof(struct task)
|
|
|
|
|
|
|
|
static void print_hist_for_pid(int fd, void *task)
|
2015-03-25 22:49:24 +03:00
|
|
|
{
|
bpf: fix multiple issues in selftest suite and samples
1) The test_lru_map and test_lru_dist fails building on my machine since
the sys/resource.h header is not included.
2) test_verifier fails in one test case where we try to call an invalid
function, since the verifier log output changed wrt printing function
names.
3) Current selftest suite code relies on sysconf(_SC_NPROCESSORS_CONF) for
retrieving the number of possible CPUs. This is broken at least in our
scenario and really just doesn't work.
glibc tries a number of things for retrieving _SC_NPROCESSORS_CONF.
First it tries equivalent of /sys/devices/system/cpu/cpu[0-9]* | wc -l,
if that fails, depending on the config, it either tries to count CPUs
in /proc/cpuinfo, or returns the _SC_NPROCESSORS_ONLN value instead.
If /proc/cpuinfo has some issue, it returns just 1 worst case. This
oddity is nothing new [1], but semantics/behaviour seems to be settled.
_SC_NPROCESSORS_ONLN will parse /sys/devices/system/cpu/online, if
that fails it looks into /proc/stat for cpuX entries, and if also that
fails for some reason, /proc/cpuinfo is consulted (and returning 1 if
unlikely all breaks down).
While that might match num_possible_cpus() from the kernel in some
cases, it's really not guaranteed with CPU hotplugging, and can result
in a buffer overflow since the array in user space could have too few
number of slots, and on perpcu map lookup, the kernel will write beyond
that memory of the value buffer.
William Tu reported such mismatches:
[...] The fact that sysconf(_SC_NPROCESSORS_CONF) != num_possible_cpu()
happens when CPU hotadd is enabled. For example, in Fusion when
setting vcpu.hotadd = "TRUE" or in KVM, setting ./qemu-system-x86_64
-smp 2, maxcpus=4 ... the num_possible_cpu() will be 4 and sysconf()
will be 2 [2]. [...]
Documentation/cputopology.txt says /sys/devices/system/cpu/possible
outputs cpu_possible_mask. That is the same as in num_possible_cpus(),
so first step would be to fix the _SC_NPROCESSORS_CONF calls with our
own implementation. Later, we could add support to bpf(2) for passing
a mask via CPU_SET(3), for example, to just select a subset of CPUs.
BPF samples code needs this fix as well (at least so that people stop
copying this). Thus, define bpf_num_possible_cpus() once in selftests
and import it from there for the sample code to avoid duplicating it.
The remaining sysconf(_SC_NPROCESSORS_CONF) in samples are unrelated.
After all three issues are fixed, the test suite runs fine again:
# make run_tests | grep self
selftests: test_verifier [PASS]
selftests: test_maps [PASS]
selftests: test_lru_map [PASS]
selftests: test_kmod.sh [PASS]
[1] https://www.sourceware.org/ml/libc-alpha/2011-06/msg00079.html
[2] https://www.mail-archive.com/netdev@vger.kernel.org/msg121183.html
Fixes: 3059303f59cf ("samples/bpf: update tracex[23] examples to use per-cpu maps")
Fixes: 86af8b4191d2 ("Add sample for adding simple drop program to link")
Fixes: df570f577231 ("samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_ARRAY")
Fixes: e15596717948 ("samples/bpf: unit test for BPF_MAP_TYPE_PERCPU_HASH")
Fixes: ebb676daa1a3 ("bpf: Print function name in addition to function id")
Fixes: 5db58faf989f ("bpf: Add tests for the LRU bpf_htab")
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Cc: William Tu <u9012063@gmail.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2016-11-26 03:28:09 +03:00
|
|
|
unsigned int nr_cpus = bpf_num_possible_cpus();
|
2015-06-13 05:39:12 +03:00
|
|
|
struct hist_key key = {}, next_key;
|
2016-02-02 09:39:58 +03:00
|
|
|
long values[nr_cpus];
|
2015-06-13 05:39:12 +03:00
|
|
|
char starstr[MAX_STARS];
|
2015-03-25 22:49:24 +03:00
|
|
|
long value;
|
|
|
|
long data[MAX_INDEX] = {};
|
|
|
|
int max_ind = -1;
|
|
|
|
long max_value = 0;
|
2015-06-13 05:39:12 +03:00
|
|
|
int i, ind;
|
2015-03-25 22:49:24 +03:00
|
|
|
|
2015-06-13 05:39:12 +03:00
|
|
|
while (bpf_get_next_key(fd, &key, &next_key) == 0) {
|
|
|
|
if (memcmp(&next_key, task, SIZE)) {
|
|
|
|
key = next_key;
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-02 09:39:58 +03:00
|
|
|
bpf_lookup_elem(fd, &next_key, values);
|
|
|
|
value = 0;
|
|
|
|
for (i = 0; i < nr_cpus; i++)
|
|
|
|
value += values[i];
|
2015-06-13 05:39:12 +03:00
|
|
|
ind = next_key.index;
|
|
|
|
data[ind] = value;
|
|
|
|
if (value && ind > max_ind)
|
|
|
|
max_ind = ind;
|
2015-03-25 22:49:24 +03:00
|
|
|
if (value > max_value)
|
|
|
|
max_value = value;
|
2015-06-13 05:39:12 +03:00
|
|
|
key = next_key;
|
2015-03-25 22:49:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
printf(" syscall write() stats\n");
|
|
|
|
printf(" byte_size : count distribution\n");
|
|
|
|
for (i = 1; i <= max_ind + 1; i++) {
|
|
|
|
stars(starstr, data[i - 1], max_value, MAX_STARS);
|
|
|
|
printf("%8ld -> %-8ld : %-8ld |%-*s|\n",
|
|
|
|
(1l << i) >> 1, (1l << i) - 1, data[i - 1],
|
|
|
|
MAX_STARS, starstr);
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 05:39:12 +03:00
|
|
|
|
|
|
|
static void print_hist(int fd)
|
|
|
|
{
|
|
|
|
struct hist_key key = {}, next_key;
|
|
|
|
static struct task tasks[1024];
|
|
|
|
int task_cnt = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (bpf_get_next_key(fd, &key, &next_key) == 0) {
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < task_cnt; i++)
|
|
|
|
if (memcmp(&tasks[i], &next_key, SIZE) == 0)
|
|
|
|
found = 1;
|
|
|
|
if (!found)
|
|
|
|
memcpy(&tasks[task_cnt++], &next_key, SIZE);
|
|
|
|
key = next_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < task_cnt; i++) {
|
|
|
|
printf("\npid %d cmd %s uid %d\n",
|
|
|
|
(__u32) tasks[i].pid_tgid,
|
|
|
|
tasks[i].comm,
|
|
|
|
(__u32) tasks[i].uid_gid);
|
|
|
|
print_hist_for_pid(fd, &tasks[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-25 22:49:24 +03:00
|
|
|
static void int_exit(int sig)
|
|
|
|
{
|
|
|
|
print_hist(map_fd[1]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int ac, char **argv)
|
|
|
|
{
|
|
|
|
char filename[256];
|
|
|
|
long key, next_key, value;
|
|
|
|
FILE *f;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
|
|
|
|
|
|
|
signal(SIGINT, int_exit);
|
|
|
|
|
|
|
|
/* start 'ping' in the background to have some kfree_skb events */
|
|
|
|
f = popen("ping -c5 localhost", "r");
|
|
|
|
(void) f;
|
|
|
|
|
|
|
|
/* start 'dd' in the background to have plenty of 'write' syscalls */
|
|
|
|
f = popen("dd if=/dev/zero of=/dev/null count=5000000", "r");
|
|
|
|
(void) f;
|
|
|
|
|
|
|
|
if (load_bpf_file(filename)) {
|
|
|
|
printf("%s", bpf_log_buf);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
key = 0;
|
|
|
|
while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
|
|
|
|
bpf_lookup_elem(map_fd[0], &next_key, &value);
|
|
|
|
printf("location 0x%lx count %ld\n", next_key, value);
|
|
|
|
key = next_key;
|
|
|
|
}
|
|
|
|
if (key)
|
|
|
|
printf("\n");
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
print_hist(map_fd[1]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|