Skip to content

Commit d606ee5

Browse files
teknoraverAlexei Starovoitov
authored andcommitted
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>
1 parent 9ffd05d commit d606ee5

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

samples/bpf/xdp1_user.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,24 @@ static void int_exit(int sig)
3434
static void poll_stats(int map_fd, int interval)
3535
{
3636
unsigned int nr_cpus = bpf_num_possible_cpus();
37-
const unsigned int nr_keys = 256;
38-
__u64 values[nr_cpus], prev[nr_keys][nr_cpus];
39-
__u32 key;
37+
__u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
4038
int i;
4139

42-
memset(prev, 0, sizeof(prev));
43-
4440
while (1) {
41+
__u32 key = UINT32_MAX;
42+
4543
sleep(interval);
4644

47-
for (key = 0; key < nr_keys; key++) {
45+
while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
4846
__u64 sum = 0;
4947

5048
assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
5149
for (i = 0; i < nr_cpus; i++)
52-
sum += (values[i] - prev[key][i]);
53-
if (sum)
50+
sum += values[i];
51+
if (sum > prev[key])
5452
printf("proto %u: %10llu pkt/s\n",
55-
key, sum / interval);
56-
memcpy(prev[key], values, sizeof(values));
53+
key, (sum - prev[key]) / interval);
54+
prev[key] = sum;
5755
}
5856
}
5957
}

0 commit comments

Comments
 (0)