blob: 3042ce37dae8ec14c149f21ad9a96191ed49d217 [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>
16#include <sys/resource.h>
17#include <arpa/inet.h>
18#include <netinet/ether.h>
19#include <unistd.h>
20#include <time.h>
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070021#include "bpf/bpf.h"
22#include "bpf/libbpf.h"
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070023
24#define STATS_INTERVAL_S 2U
25
26static int ifindex = -1;
27static __u32 xdp_flags;
28
29static void int_exit(int sig)
30{
31 if (ifindex > -1)
32 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
33 exit(0);
34}
35
36/* simple "icmp packet too big sent" counter
37 */
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070038static void poll_stats(unsigned int map_fd, unsigned int kill_after_s)
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070039{
40 time_t started_at = time(NULL);
41 __u64 value = 0;
42 int key = 0;
43
44
45 while (!kill_after_s || time(NULL) - started_at <= kill_after_s) {
46 sleep(STATS_INTERVAL_S);
47
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070048 assert(bpf_map_lookup_elem(map_fd, &key, &value) == 0);
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070049
50 printf("icmp \"packet too big\" sent: %10llu pkts\n", value);
51 }
52}
53
54static void usage(const char *cmd)
55{
56 printf("Start a XDP prog which send ICMP \"packet too big\" \n"
57 "messages if ingress packet is bigger then MAX_SIZE bytes\n");
58 printf("Usage: %s [...]\n", cmd);
59 printf(" -i <ifindex> Interface Index\n");
60 printf(" -T <stop-after-X-seconds> Default: 0 (forever)\n");
61 printf(" -S use skb-mode\n");
62 printf(" -N enforce native mode\n");
63 printf(" -h Display this help\n");
64}
65
66int main(int argc, char **argv)
67{
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070068 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
69 struct bpf_prog_load_attr prog_load_attr = {
70 .prog_type = BPF_PROG_TYPE_XDP,
71 };
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070072 unsigned char opt_flags[256] = {};
73 unsigned int kill_after_s = 0;
74 const char *optstr = "i:T:SNh";
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070075 int i, prog_fd, map_fd, opt;
76 struct bpf_object *obj;
77 struct bpf_map *map;
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070078 char filename[256];
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -070079
80 for (i = 0; i < strlen(optstr); i++)
81 if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
82 opt_flags[(unsigned char)optstr[i]] = 1;
83
84 while ((opt = getopt(argc, argv, optstr)) != -1) {
85
86 switch (opt) {
87 case 'i':
88 ifindex = atoi(optarg);
89 break;
90 case 'T':
91 kill_after_s = atoi(optarg);
92 break;
93 case 'S':
94 xdp_flags |= XDP_FLAGS_SKB_MODE;
95 break;
96 case 'N':
97 xdp_flags |= XDP_FLAGS_DRV_MODE;
98 break;
99 default:
100 usage(argv[0]);
101 return 1;
102 }
103 opt_flags[opt] = 0;
104 }
105
106 for (i = 0; i < strlen(optstr); i++) {
107 if (opt_flags[(unsigned int)optstr[i]]) {
108 fprintf(stderr, "Missing argument -%c\n", optstr[i]);
109 usage(argv[0]);
110 return 1;
111 }
112 }
113
114 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
115 perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
116 return 1;
117 }
118
119 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700120 prog_load_attr.file = filename;
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700121
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700122 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
123 return 1;
124
125 map = bpf_map__next(NULL, obj);
126 if (!map) {
127 printf("finding a map in obj file failed\n");
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700128 return 1;
129 }
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700130 map_fd = bpf_map__fd(map);
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700131
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700132 if (!prog_fd) {
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700133 printf("load_bpf_file: %s\n", strerror(errno));
134 return 1;
135 }
136
137 signal(SIGINT, int_exit);
138 signal(SIGTERM, int_exit);
139
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700140 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700141 printf("link set xdp fd failed\n");
142 return 1;
143 }
144
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700145 poll_stats(map_fd, kill_after_s);
Nikita V. Shirokovc6ffd1f2018-04-17 21:42:23 -0700146
147 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
148
149 return 0;
150}