mm, memory_hotplug: try to migrate full pfn range
[linux/fpc-iii.git] / samples / bpf / xdp1_user.c
blob0a197f86ac43a7a28b281d36efeedb5d423dd8dd
1 /* Copyright (c) 2016 PLUMgrid
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>
8 #include <linux/if_link.h>
9 #include <assert.h>
10 #include <errno.h>
11 #include <signal.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <libgen.h>
17 #include <sys/resource.h>
18 #include <net/if.h>
20 #include "bpf_util.h"
21 #include "bpf/bpf.h"
22 #include "bpf/libbpf.h"
24 static int ifindex;
25 static __u32 xdp_flags;
27 static void int_exit(int sig)
29 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
30 exit(0);
33 /* simple per-protocol drop counter
35 static void poll_stats(int map_fd, int interval)
37 unsigned int nr_cpus = bpf_num_possible_cpus();
38 __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
39 int i;
41 while (1) {
42 __u32 key = UINT32_MAX;
44 sleep(interval);
46 while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
47 __u64 sum = 0;
49 assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
50 for (i = 0; i < nr_cpus; i++)
51 sum += values[i];
52 if (sum > prev[key])
53 printf("proto %u: %10llu pkt/s\n",
54 key, (sum - prev[key]) / interval);
55 prev[key] = sum;
60 static void usage(const char *prog)
62 fprintf(stderr,
63 "usage: %s [OPTS] IFACE\n\n"
64 "OPTS:\n"
65 " -S use skb-mode\n"
66 " -N enforce native mode\n",
67 prog);
70 int main(int argc, char **argv)
72 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
73 struct bpf_prog_load_attr prog_load_attr = {
74 .prog_type = BPF_PROG_TYPE_XDP,
76 const char *optstr = "SN";
77 int prog_fd, map_fd, opt;
78 struct bpf_object *obj;
79 struct bpf_map *map;
80 char filename[256];
82 while ((opt = getopt(argc, argv, optstr)) != -1) {
83 switch (opt) {
84 case 'S':
85 xdp_flags |= XDP_FLAGS_SKB_MODE;
86 break;
87 case 'N':
88 xdp_flags |= XDP_FLAGS_DRV_MODE;
89 break;
90 default:
91 usage(basename(argv[0]));
92 return 1;
96 if (optind == argc) {
97 usage(basename(argv[0]));
98 return 1;
101 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
102 perror("setrlimit(RLIMIT_MEMLOCK)");
103 return 1;
106 ifindex = if_nametoindex(argv[1]);
107 if (!ifindex) {
108 perror("if_nametoindex");
109 return 1;
112 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
113 prog_load_attr.file = filename;
115 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
116 return 1;
118 map = bpf_map__next(NULL, obj);
119 if (!map) {
120 printf("finding a map in obj file failed\n");
121 return 1;
123 map_fd = bpf_map__fd(map);
125 if (!prog_fd) {
126 printf("load_bpf_file: %s\n", strerror(errno));
127 return 1;
130 signal(SIGINT, int_exit);
131 signal(SIGTERM, int_exit);
133 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
134 printf("link set xdp fd failed\n");
135 return 1;
138 poll_stats(map_fd, 2);
140 return 0;