diff options
author | Matteo Croce <mcroce@redhat.com> | 2018-12-01 01:23:05 +0100 |
---|---|---|
committer | Alexei Starovoitov <ast@kernel.org> | 2018-11-30 22:06:41 -0800 |
commit | d606ee5c1d9a9bc4d1bba3c36fae615447f9851c (patch) | |
tree | 23e38205c206765e3920061f596c2e9bd241860f /samples | |
parent | 9ffd05d9b78a2fb393d31936886c320b1d6e438f (diff) | |
download | linux-stable-d606ee5c1d9a9bc4d1bba3c36fae615447f9851c.tar.gz linux-stable-d606ee5c1d9a9bc4d1bba3c36fae615447f9851c.tar.bz2 linux-stable-d606ee5c1d9a9bc4d1bba3c36fae615447f9851c.zip |
samples: bpf: improve xdp1 example
Store only the total packet count for every protocol, instead of the
whole per-cpu array.
Use bpf_map_get_next_key() to iterate the map, instead of looking up
all the protocols.
Signed-off-by: Matteo Croce <mcroce@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/bpf/xdp1_user.c | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/samples/bpf/xdp1_user.c b/samples/bpf/xdp1_user.c index b02c531510ed..4f3d824fc044 100644 --- a/samples/bpf/xdp1_user.c +++ b/samples/bpf/xdp1_user.c @@ -34,26 +34,24 @@ static void int_exit(int sig) static void poll_stats(int map_fd, int interval) { unsigned int nr_cpus = bpf_num_possible_cpus(); - const unsigned int nr_keys = 256; - __u64 values[nr_cpus], prev[nr_keys][nr_cpus]; - __u32 key; + __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 }; int i; - memset(prev, 0, sizeof(prev)); - while (1) { + __u32 key = UINT32_MAX; + sleep(interval); - for (key = 0; key < nr_keys; key++) { + while (bpf_map_get_next_key(map_fd, &key, &key) != -1) { __u64 sum = 0; assert(bpf_map_lookup_elem(map_fd, &key, values) == 0); for (i = 0; i < nr_cpus; i++) - sum += (values[i] - prev[key][i]); - if (sum) + sum += values[i]; + if (sum > prev[key]) printf("proto %u: %10llu pkt/s\n", - key, sum / interval); - memcpy(prev[key], values, sizeof(values)); + key, (sum - prev[key]) / interval); + prev[key] = sum; } } } |