1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
4 * ibumad BPF sample user side
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * Copyright(c) 2018 Ira Weiny, Intel Corporation
13 #include <linux/bpf.h>
19 #include <sys/types.h>
22 #include <sys/resource.h>
28 #include <bpf/libbpf.h>
30 static void dump_counts(int fd)
35 for (key = 0; key < 256; key++) {
36 if (bpf_map_lookup_elem(fd, &key, &value)) {
37 printf("failed to read key %u\n", key);
41 printf("0x%02x : %llu\n", key, value);
45 static void dump_all_counts(void)
47 printf("Read 'Class : count'\n");
48 dump_counts(map_fd[0]);
49 printf("Write 'Class : count'\n");
50 dump_counts(map_fd[1]);
53 static void dump_exit(int sig)
59 static const struct option long_options[] = {
60 {"help", no_argument, NULL, 'h'},
61 {"delay", required_argument, NULL, 'd'},
64 static void usage(char *cmd)
66 printf("eBPF test program to count packets from various IP addresses\n"
67 "Usage: %s <options>\n"
68 " --help, -h this menu\n"
69 " --delay, -d <delay> wait <delay> sec between prints [1 - 1000000]\n"
74 int main(int argc, char **argv)
76 unsigned long delay = 5;
81 /* Create the eBPF kernel code path name.
82 * This follows the pattern of all of the other bpf samples
84 snprintf(bpf_file, sizeof(bpf_file), "%s_kern.o", argv[0]);
86 /* Do one final dump when exiting */
87 signal(SIGINT, dump_exit);
88 signal(SIGTERM, dump_exit);
90 while ((opt = getopt_long(argc, argv, "hd:rSw",
91 long_options, &longindex)) != -1) {
94 delay = strtoul(optarg, NULL, 0);
95 if (delay == ULONG_MAX || delay < 0 ||
97 fprintf(stderr, "ERROR: invalid delay : %s\n",
110 if (load_bpf_file(bpf_file)) {
111 fprintf(stderr, "ERROR: failed to load eBPF from file : %s\n",