Skip to content

Commit 71fc156

Browse files
author
Alexei Starovoitov
committed
Merge branch 'xdp1-improvements'
Matteo Croce says: ==================== Small improvements to improve the readability and easiness to use of the xdp1 sample. ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 9ffd05d + dc378a1 commit 71fc156

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

samples/bpf/xdp1_user.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <unistd.h>
1616
#include <libgen.h>
1717
#include <sys/resource.h>
18+
#include <net/if.h>
1819

1920
#include "bpf_util.h"
2021
#include "bpf/bpf.h"
@@ -34,34 +35,32 @@ static void int_exit(int sig)
3435
static void poll_stats(int map_fd, int interval)
3536
{
3637
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;
38+
__u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
4039
int i;
4140

42-
memset(prev, 0, sizeof(prev));
43-
4441
while (1) {
42+
__u32 key = UINT32_MAX;
43+
4544
sleep(interval);
4645

47-
for (key = 0; key < nr_keys; key++) {
46+
while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
4847
__u64 sum = 0;
4948

5049
assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
5150
for (i = 0; i < nr_cpus; i++)
52-
sum += (values[i] - prev[key][i]);
53-
if (sum)
51+
sum += values[i];
52+
if (sum > prev[key])
5453
printf("proto %u: %10llu pkt/s\n",
55-
key, sum / interval);
56-
memcpy(prev[key], values, sizeof(values));
54+
key, (sum - prev[key]) / interval);
55+
prev[key] = sum;
5756
}
5857
}
5958
}
6059

6160
static void usage(const char *prog)
6261
{
6362
fprintf(stderr,
64-
"usage: %s [OPTS] IFINDEX\n\n"
63+
"usage: %s [OPTS] IFACE\n\n"
6564
"OPTS:\n"
6665
" -S use skb-mode\n"
6766
" -N enforce native mode\n",
@@ -104,7 +103,11 @@ int main(int argc, char **argv)
104103
return 1;
105104
}
106105

107-
ifindex = strtoul(argv[optind], NULL, 0);
106+
ifindex = if_nametoindex(argv[1]);
107+
if (!ifindex) {
108+
perror("if_nametoindex");
109+
return 1;
110+
}
108111

109112
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
110113
prog_load_attr.file = filename;

0 commit comments

Comments
 (0)