]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - samples/bpf/xdp_adjust_tail_user.c
Merge tag 'libnvdimm-for-5.1' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm...
[mirror_ubuntu-hirsute-kernel.git] / samples / bpf / xdp_adjust_tail_user.c
CommitLineData
c6ffd1ff
NS
1/* SPDX-License-Identifier: GPL-2.0
2 * Copyright (c) 2018 Facebook
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 */
8#include <linux/bpf.h>
9#include <linux/if_link.h>
10#include <assert.h>
11#include <errno.h>
12#include <signal.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <sys/resource.h>
17#include <arpa/inet.h>
18#include <netinet/ether.h>
19#include <unistd.h>
20#include <time.h>
be5bca44
JK
21#include "bpf/bpf.h"
22#include "bpf/libbpf.h"
c6ffd1ff
NS
23
24#define STATS_INTERVAL_S 2U
25
26static int ifindex = -1;
743e568c 27static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
3b7a8ec2 28static __u32 prog_id;
c6ffd1ff
NS
29
30static void int_exit(int sig)
31{
3b7a8ec2
MF
32 __u32 curr_prog_id = 0;
33
34 if (ifindex > -1) {
35 if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
36 printf("bpf_get_link_xdp_id failed\n");
37 exit(1);
38 }
39 if (prog_id == curr_prog_id)
40 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
41 else if (!curr_prog_id)
42 printf("couldn't find a prog id on a given iface\n");
43 else
44 printf("program on interface changed, not removing\n");
45 }
c6ffd1ff
NS
46 exit(0);
47}
48
49/* simple "icmp packet too big sent" counter
50 */
be5bca44 51static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
c6ffd1ff
NS
52{
53 time_t started_at = time(NULL);
54 __u64 value = 0;
55 int key = 0;
56
57
58 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
59 sleep(STATS_INTERVAL_S);
60
be5bca44 61 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
c6ffd1ff
NS
62
63 printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
64 }
65}
66
67static void usage(const char *cmd)
68{
69 printf("Start a XDP prog which send ICMP \"packet too big\" \n"
70 "messages if ingress packet is bigger then MAX_SIZE bytes\n");
71 printf("Usage: %s [...]\n", cmd);
72 printf(" -i <ifindex> Interface Index\n");
73 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
74 printf(" -S use skb-mode\n");
75 printf(" -N enforce native mode\n");
743e568c 76 printf(" -F force loading prog\n");
c6ffd1ff
NS
77 printf(" -h Display this help\n");
78}
79
80int main(int argc, char **argv)
81{
be5bca44
JK
82 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
83 struct bpf_prog_load_attr prog_load_attr = {
84 .prog_type = BPF_PROG_TYPE_XDP,
85 };
c6ffd1ff 86 unsigned char opt_flags[256] = {};
743e568c 87 const char *optstr = "i:T:SNFh";
3b7a8ec2
MF
88 struct bpf_prog_info info = {};
89 __u32 info_len = sizeof(info);
c6ffd1ff 90 unsigned int kill_after_s = 0;
be5bca44
JK
91 int i, prog_fd, map_fd, opt;
92 struct bpf_object *obj;
93 struct bpf_map *map;
c6ffd1ff 94 char filename[256];
3b7a8ec2 95 int err;
c6ffd1ff
NS
96
97 for (i = 0; i < strlen(optstr); i++)
98 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
99 opt_flags[(unsigned char)optstr[i]] = 1;
100
101 while ((opt = getopt(argc, argv, optstr)) != -1) {
102
103 switch (opt) {
104 case 'i':
105 ifindex = atoi(optarg);
106 break;
107 case 'T':
108 kill_after_s = atoi(optarg);
109 break;
110 case 'S':
111 xdp_flags |= XDP_FLAGS_SKB_MODE;
112 break;
113 case 'N':
114 xdp_flags |= XDP_FLAGS_DRV_MODE;
115 break;
743e568c
MF
116 case 'F':
117 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
118 break;
c6ffd1ff
NS
119 default:
120 usage(argv[0]);
121 return 1;
122 }
123 opt_flags[opt] = 0;
124 }
125
126 for (i = 0; i < strlen(optstr); i++) {
127 if (opt_flags[(unsigned int)optstr[i]]) {
128 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
129 usage(argv[0]);
130 return 1;
131 }
132 }
133
134 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
135 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
136 return 1;
137 }
138
139 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
be5bca44
JK
140 prog_load_attr.file = filename;
141
142 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
143 return 1;
c6ffd1ff 144
be5bca44
JK
145 map = bpf_map__next(NULL, obj);
146 if (!map) {
147 printf("finding a map in obj file failed\n");
c6ffd1ff
NS
148 return 1;
149 }
be5bca44 150 map_fd = bpf_map__fd(map);
c6ffd1ff 151
be5bca44 152 if (!prog_fd) {
c6ffd1ff
NS
153 printf("load_bpf_file: %s\n", strerror(errno));
154 return 1;
155 }
156
157 signal(SIGINT, int_exit);
158 signal(SIGTERM, int_exit);
159
be5bca44 160 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
c6ffd1ff
NS
161 printf("link set xdp fd failed\n");
162 return 1;
163 }
164
3b7a8ec2
MF
165 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
166 if (err) {
167 printf("can't get prog info - %s\n", strerror(errno));
168 return 1;
169 }
170 prog_id = info.id;
c6ffd1ff 171
3b7a8ec2
MF
172 poll_stats(map_fd, kill_after_s);
173 int_exit(0);
c6ffd1ff
NS
174
175 return 0;
176}