samples: bpf: improve xdp1 example
[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>
e00c7b21 18
e00c7b21 19#include "bpf_util.h"
be5bca44
JK
20#include "bpf/bpf.h"
21#include "bpf/libbpf.h"
86af8b41 22
86af8b41 23static int ifindex;
6387d011 24static __u32 xdp_flags;
86af8b41
BB
25
26static void int_exit(int sig)
27{
b259c2ff 28 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
86af8b41
BB
29 exit(0);
30}
31
32/* simple per-protocol drop counter
33 */
be5bca44 34static void poll_stats(int map_fd, int interval)
86af8b41 35{
e00c7b21 36 unsigned int nr_cpus = bpf_num_possible_cpus();
d606ee5c 37 __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
86af8b41
BB
38 int i;
39
86af8b41 40 while (1) {
d606ee5c
MC
41 __u32 key = UINT32_MAX;
42
86af8b41
BB
43 sleep(interval);
44
d606ee5c 45 while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
86af8b41
BB
46 __u64 sum = 0;
47
be5bca44 48 assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
86af8b41 49 for (i = 0; i < nr_cpus; i++)
d606ee5c
MC
50 sum += values[i];
51 if (sum > prev[key])
86af8b41 52 printf("proto %u: %10llu pkt/s\n",
d606ee5c
MC
53 key, (sum - prev[key]) / interval);
54 prev[key] = sum;
86af8b41
BB
55 }
56 }
57}
58
3993f2cb 59static void usage(const char *prog)
86af8b41 60{
3993f2cb
DA
61 fprintf(stderr,
62 "usage: %s [OPTS] IFINDEX\n\n"
63 "OPTS:\n"
0489df9a
DB
64 " -S use skb-mode\n"
65 " -N enforce native mode\n",
3993f2cb
DA
66 prog);
67}
86af8b41 68
3993f2cb
DA
69int main(int argc, char **argv)
70{
6dfca831 71 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
be5bca44
JK
72 struct bpf_prog_load_attr prog_load_attr = {
73 .prog_type = BPF_PROG_TYPE_XDP,
74 };
0489df9a 75 const char *optstr = "SN";
be5bca44
JK
76 int prog_fd, map_fd, opt;
77 struct bpf_object *obj;
78 struct bpf_map *map;
3993f2cb 79 char filename[256];
3993f2cb
DA
80
81 while ((opt = getopt(argc, argv, optstr)) != -1) {
82 switch (opt) {
83 case 'S':
6387d011 84 xdp_flags |= XDP_FLAGS_SKB_MODE;
3993f2cb 85 break;
0489df9a
DB
86 case 'N':
87 xdp_flags |= XDP_FLAGS_DRV_MODE;
88 break;
3993f2cb
DA
89 default:
90 usage(basename(argv[0]));
91 return 1;
92 }
93 }
86af8b41 94
3993f2cb
DA
95 if (optind == argc) {
96 usage(basename(argv[0]));
86af8b41
BB
97 return 1;
98 }
6dfca831
TD
99
100 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
101 perror("setrlimit(RLIMIT_MEMLOCK)");
102 return 1;
103 }
104
3993f2cb 105 ifindex = strtoul(argv[optind], NULL, 0);
86af8b41 106
3993f2cb 107 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
be5bca44 108 prog_load_attr.file = filename;
86af8b41 109
be5bca44
JK
110 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
111 return 1;
112
113 map = bpf_map__next(NULL, obj);
114 if (!map) {
115 printf("finding a map in obj file failed\n");
86af8b41
BB
116 return 1;
117 }
be5bca44 118 map_fd = bpf_map__fd(map);
86af8b41 119
be5bca44 120 if (!prog_fd) {
86af8b41
BB
121 printf("load_bpf_file: %s\n", strerror(errno));
122 return 1;
123 }
124
125 signal(SIGINT, int_exit);
ad990dbe 126 signal(SIGTERM, int_exit);
86af8b41 127
be5bca44 128 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
86af8b41
BB
129 printf("link set xdp fd failed\n");
130 return 1;
131 }
132
be5bca44 133 poll_stats(map_fd, 2);
86af8b41
BB
134
135 return 0;
136}