Skip to content

Commit

Permalink
Merge branch 'xdp1-improvements'
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Alexei Starovoitov committed Dec 1, 2018
2 parents 9ffd05d + dc378a1 commit 71fc156
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions samples/bpf/xdp1_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <unistd.h>
#include <libgen.h>
#include <sys/resource.h>
#include <net/if.h>

#include "bpf_util.h"
#include "bpf/bpf.h"
Expand All @@ -34,34 +35,32 @@ 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;
}
}
}

static void usage(const char *prog)
{
fprintf(stderr,
"usage: %s [OPTS] IFINDEX\n\n"
"usage: %s [OPTS] IFACE\n\n"
"OPTS:\n"
" -S use skb-mode\n"
" -N enforce native mode\n",
Expand Down Expand Up @@ -104,7 +103,11 @@ int main(int argc, char **argv)
return 1;
}

ifindex = strtoul(argv[optind], NULL, 0);
ifindex = if_nametoindex(argv[1]);
if (!ifindex) {
perror("if_nametoindex");
return 1;
}

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

0 comments on commit 71fc156

Please sign in to comment.