blob: a3596b617c4ce836f8908efa4d3ad54d66093db9 [file] [log] [blame]
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -07001/* 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>
Daniel T. Lee9e859e82019-06-25 09:55:36 +090016#include <net/if.h>
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070017#include <sys/resource.h>
18#include <arpa/inet.h>
19#include <netinet/ether.h>
20#include <unistd.h>
21#include <time.h>
Daniel T. Lee4d18f6d2019-06-16 00:14:47 +090022#include "bpf.h"
23#include "libbpf.h"
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070024
25#define STATS_INTERVAL_S 2U
26
27static int ifindex = -1;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010028static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010029static __u32 prog_id;
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070030
31static void int_exit(int sig)
32{
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010033 __u32 curr_prog_id = 0;
34
35 if (ifindex > -1) {
36 if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
37 printf("bpf_get_link_xdp_id failed\n");
38 exit(1);
39 }
40 if (prog_id == curr_prog_id)
41 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
42 else if (!curr_prog_id)
43 printf("couldn't find a prog id on a given iface\n");
44 else
45 printf("program on interface changed, not removing\n");
46 }
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070047 exit(0);
48}
49
50/* simple "icmp packet too big sent" counter
51 */
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070052static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070053{
54 time_t started_at = time(NULL);
55 __u64 value = 0;
56 int key = 0;
57
58
59 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
60 sleep(STATS_INTERVAL_S);
61
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070062 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070063
64 printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
65 }
66}
67
68static void usage(const char *cmd)
69{
70 printf("Start a XDP prog which send ICMP \"packet too big\" \n"
71 "messages if ingress packet is bigger then MAX_SIZE bytes\n");
72 printf("Usage: %s [...]\n", cmd);
Daniel T. Lee9e859e82019-06-25 09:55:36 +090073 printf(" -i <ifname|ifindex> Interface\n");
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070074 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
75 printf(" -S use skb-mode\n");
76 printf(" -N enforce native mode\n");
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010077 printf(" -F force loading prog\n");
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070078 printf(" -h Display this help\n");
79}
80
81int main(int argc, char **argv)
82{
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070083 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
84 struct bpf_prog_load_attr prog_load_attr = {
85 .prog_type = BPF_PROG_TYPE_XDP,
86 };
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070087 unsigned char opt_flags[256] = {};
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010088 const char *optstr = "i:T:SNFh";
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010089 struct bpf_prog_info info = {};
90 __u32 info_len = sizeof(info);
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070091 unsigned int kill_after_s = 0;
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070092 int i, prog_fd, map_fd, opt;
93 struct bpf_object *obj;
94 struct bpf_map *map;
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070095 char filename[256];
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010096 int err;
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070097
98 for (i = 0; i < strlen(optstr); i++)
99 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
100 opt_flags[(unsigned char)optstr[i]] = 1;
101
102 while ((opt = getopt(argc, argv, optstr)) != -1) {
103
104 switch (opt) {
105 case 'i':
Daniel T. Lee9e859e82019-06-25 09:55:36 +0900106 ifindex = if_nametoindex(optarg);
107 if (!ifindex)
108 ifindex = atoi(optarg);
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700109 break;
110 case 'T':
111 kill_after_s = atoi(optarg);
112 break;
113 case 'S':
114 xdp_flags |= XDP_FLAGS_SKB_MODE;
115 break;
116 case 'N':
117 xdp_flags |= XDP_FLAGS_DRV_MODE;
118 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100119 case 'F':
120 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
121 break;
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700122 default:
123 usage(argv[0]);
124 return 1;
125 }
126 opt_flags[opt] = 0;
127 }
128
129 for (i = 0; i < strlen(optstr); i++) {
130 if (opt_flags[(unsigned int)optstr[i]]) {
131 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
132 usage(argv[0]);
133 return 1;
134 }
135 }
136
137 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
138 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
139 return 1;
140 }
141
Daniel T. Lee9e859e82019-06-25 09:55:36 +0900142 if (!ifindex) {
143 fprintf(stderr, "Invalid ifname\n");
144 return 1;
145 }
146
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700147 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700148 prog_load_attr.file = filename;
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700149
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700150 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
151 return 1;
152
153 map = bpf_map__next(NULL, obj);
154 if (!map) {
155 printf("finding a map in obj file failed\n");
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700156 return 1;
157 }
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700158 map_fd = bpf_map__fd(map);
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700159
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700160 if (!prog_fd) {
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700161 printf("load_bpf_file: %s\n", strerror(errno));
162 return 1;
163 }
164
165 signal(SIGINT, int_exit);
166 signal(SIGTERM, int_exit);
167
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700168 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700169 printf("link set xdp fd failed\n");
170 return 1;
171 }
172
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100173 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
174 if (err) {
175 printf("can't get prog info - %s\n", strerror(errno));
176 return 1;
177 }
178 prog_id = info.id;
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700179
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100180 poll_stats(map_fd, kill_after_s);
181 int_exit(0);
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700182
183 return 0;
184}