selftests/bpf: add missing executables to .gitignore
[linux-2.6-block.git] / samples / bpf / xdp1_user.c
CommitLineData
86af8b41
BB
1/* Copyright (c) 2016 PLUMgrid
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <linux/bpf.h>
3993f2cb 8#include <linux/if_link.h>
86af8b41
BB
9#include <assert.h>
10#include <errno.h>
11#include <signal.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
86af8b41 15#include <unistd.h>
3993f2cb 16#include <libgen.h>
6dfca831 17#include <sys/resource.h>
dc378a1a 18#include <net/if.h>
e00c7b21 19
e00c7b21 20#include "bpf_util.h"
be5bca44
JK
21#include "bpf/bpf.h"
22#include "bpf/libbpf.h"
86af8b41 23
86af8b41 24static int ifindex;
6387d011 25static __u32 xdp_flags;
86af8b41
BB
26
27static void int_exit(int sig)
28{
b259c2ff 29 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
86af8b41
BB
30 exit(0);
31}
32
33/* simple per-protocol drop counter
34 */
be5bca44 35static void poll_stats(int map_fd, int interval)
86af8b41 36{
e00c7b21 37 unsigned int nr_cpus = bpf_num_possible_cpus();
d606ee5c 38 __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
86af8b41
BB
39 int i;
40
86af8b41 41 while (1) {
d606ee5c
MC
42 __u32 key = UINT32_MAX;
43
86af8b41
BB
44 sleep(interval);
45
d606ee5c 46 while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
86af8b41
BB
47 __u64 sum = 0;
48
be5bca44 49 assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
86af8b41 50 for (i = 0; i < nr_cpus; i++)
d606ee5c
MC
51 sum += values[i];
52 if (sum > prev[key])
86af8b41 53 printf("proto %u: %10llu pkt/s\n",
d606ee5c
MC
54 key, (sum - prev[key]) / interval);
55 prev[key] = sum;
86af8b41
BB
56 }
57 }
58}
59
3993f2cb 60static void usage(const char *prog)
86af8b41 61{
3993f2cb 62 fprintf(stderr,
dc378a1a 63 "usage: %s [OPTS] IFACE\n\n"
3993f2cb 64 "OPTS:\n"
0489df9a
DB
65 " -S use skb-mode\n"
66 " -N enforce native mode\n",
3993f2cb
DA
67 prog);
68}
86af8b41 69
3993f2cb
DA
70int main(int argc, char **argv)
71{
6dfca831 72 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
be5bca44
JK
73 struct bpf_prog_load_attr prog_load_attr = {
74 .prog_type = BPF_PROG_TYPE_XDP,
75 };
0489df9a 76 const char *optstr = "SN";
be5bca44
JK
77 int prog_fd, map_fd, opt;
78 struct bpf_object *obj;
79 struct bpf_map *map;
3993f2cb 80 char filename[256];
3993f2cb
DA
81
82 while ((opt = getopt(argc, argv, optstr)) != -1) {
83 switch (opt) {
84 case 'S':
6387d011 85 xdp_flags |= XDP_FLAGS_SKB_MODE;
3993f2cb 86 break;
0489df9a
DB
87 case 'N':
88 xdp_flags |= XDP_FLAGS_DRV_MODE;
89 break;
3993f2cb
DA
90 default:
91 usage(basename(argv[0]));
92 return 1;
93 }
94 }
86af8b41 95
3993f2cb
DA
96 if (optind == argc) {
97 usage(basename(argv[0]));
86af8b41
BB
98 return 1;
99 }
6dfca831
TD
100
101 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
102 perror("setrlimit(RLIMIT_MEMLOCK)");
103 return 1;
104 }
105
dc378a1a
MC
106 ifindex = if_nametoindex(argv[1]);
107 if (!ifindex) {
108 perror("if_nametoindex");
109 return 1;
110 }
86af8b41 111
3993f2cb 112 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
be5bca44 113 prog_load_attr.file = filename;
86af8b41 114
be5bca44
JK
115 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
116 return 1;
117
118 map = bpf_map__next(NULL, obj);
119 if (!map) {
120 printf("finding a map in obj file failed\n");
86af8b41
BB
121 return 1;
122 }
be5bca44 123 map_fd = bpf_map__fd(map);
86af8b41 124
be5bca44 125 if (!prog_fd) {
86af8b41
BB
126 printf("load_bpf_file: %s\n", strerror(errno));
127 return 1;
128 }
129
130 signal(SIGINT, int_exit);
ad990dbe 131 signal(SIGTERM, int_exit);
86af8b41 132
be5bca44 133 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
86af8b41
BB
134 printf("link set xdp fd failed\n");
135 return 1;
136 }
137
be5bca44 138 poll_stats(map_fd, 2);
86af8b41
BB
139
140 return 0;
141}