blob: 116e39f6b66662193cc461bd226345701ba90ff0 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Brenden Blanco86af8b42016-07-19 12:16:51 -07002/* Copyright (c) 2016 PLUMgrid
Brenden Blanco86af8b42016-07-19 12:16:51 -07003 */
4#include <linux/bpf.h>
David Ahern3993f2c2017-04-27 09:11:13 -07005#include <linux/if_link.h>
Brenden Blanco86af8b42016-07-19 12:16:51 -07006#include <assert.h>
7#include <errno.h>
8#include <signal.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
Brenden Blanco86af8b42016-07-19 12:16:51 -070012#include <unistd.h>
David Ahern3993f2c2017-04-27 09:11:13 -070013#include <libgen.h>
Tushar Dave6dfca8312017-10-27 16:12:30 -070014#include <sys/resource.h>
Matteo Crocedc378a1a2018-12-01 01:23:06 +010015#include <net/if.h>
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010016
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010017#include "bpf_util.h"
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010018#include <bpf/bpf.h>
19#include <bpf/libbpf.h>
Brenden Blanco86af8b42016-07-19 12:16:51 -070020
Brenden Blanco86af8b42016-07-19 12:16:51 -070021static int ifindex;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010022static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010023static __u32 prog_id;
Brenden Blanco86af8b42016-07-19 12:16:51 -070024
25static void int_exit(int sig)
26{
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010027 __u32 curr_prog_id = 0;
28
29 if (bpf_get_link_xdp_id(ifindex, &curr_prog_id, xdp_flags)) {
30 printf("bpf_get_link_xdp_id failed\n");
31 exit(1);
32 }
33 if (prog_id == curr_prog_id)
34 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
35 else if (!curr_prog_id)
36 printf("couldn't find a prog id on a given interface\n");
37 else
38 printf("program on interface changed, not removing\n");
Brenden Blanco86af8b42016-07-19 12:16:51 -070039 exit(0);
40}
41
42/* simple per-protocol drop counter
43 */
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070044static void poll_stats(int map_fd, int interval)
Brenden Blanco86af8b42016-07-19 12:16:51 -070045{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010046 unsigned int nr_cpus = bpf_num_possible_cpus();
Matteo Croced606ee52018-12-01 01:23:05 +010047 __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
Brenden Blanco86af8b42016-07-19 12:16:51 -070048 int i;
49
Brenden Blanco86af8b42016-07-19 12:16:51 -070050 while (1) {
Matteo Croced606ee52018-12-01 01:23:05 +010051 __u32 key = UINT32_MAX;
52
Brenden Blanco86af8b42016-07-19 12:16:51 -070053 sleep(interval);
54
Matteo Croced606ee52018-12-01 01:23:05 +010055 while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
Brenden Blanco86af8b42016-07-19 12:16:51 -070056 __u64 sum = 0;
57
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070058 assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
Brenden Blanco86af8b42016-07-19 12:16:51 -070059 for (i = 0; i < nr_cpus; i++)
Matteo Croced606ee52018-12-01 01:23:05 +010060 sum += values[i];
61 if (sum > prev[key])
Brenden Blanco86af8b42016-07-19 12:16:51 -070062 printf("proto %u: %10llu pkt/s\n",
Matteo Croced606ee52018-12-01 01:23:05 +010063 key, (sum - prev[key]) / interval);
64 prev[key] = sum;
Brenden Blanco86af8b42016-07-19 12:16:51 -070065 }
66 }
67}
68
David Ahern3993f2c2017-04-27 09:11:13 -070069static void usage(const char *prog)
Brenden Blanco86af8b42016-07-19 12:16:51 -070070{
David Ahern3993f2c2017-04-27 09:11:13 -070071 fprintf(stderr,
Matteo Crocedc378a1a2018-12-01 01:23:06 +010072 "usage: %s [OPTS] IFACE\n\n"
David Ahern3993f2c2017-04-27 09:11:13 -070073 "OPTS:\n"
Daniel Borkmann0489df92017-05-12 01:04:45 +020074 " -S use skb-mode\n"
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010075 " -N enforce native mode\n"
76 " -F force loading prog\n",
David Ahern3993f2c2017-04-27 09:11:13 -070077 prog);
78}
79
80int main(int argc, char **argv)
81{
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070082 struct bpf_prog_load_attr prog_load_attr = {
83 .prog_type = BPF_PROG_TYPE_XDP,
84 };
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010085 struct bpf_prog_info info = {};
86 __u32 info_len = sizeof(info);
Maciej Fijalkowski743e5682019-02-01 22:42:28 +010087 const char *optstr = "FSN";
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070088 int prog_fd, map_fd, opt;
89 struct bpf_object *obj;
90 struct bpf_map *map;
Brenden Blanco86af8b42016-07-19 12:16:51 -070091 char filename[256];
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +010092 int err;
Brenden Blanco86af8b42016-07-19 12:16:51 -070093
David Ahern3993f2c2017-04-27 09:11:13 -070094 while ((opt = getopt(argc, argv, optstr)) != -1) {
95 switch (opt) {
96 case 'S':
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +020097 xdp_flags |= XDP_FLAGS_SKB_MODE;
David Ahern3993f2c2017-04-27 09:11:13 -070098 break;
Daniel Borkmann0489df92017-05-12 01:04:45 +020099 case 'N':
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100100 /* default, set below */
Daniel Borkmann0489df92017-05-12 01:04:45 +0200101 break;
Maciej Fijalkowski743e5682019-02-01 22:42:28 +0100102 case 'F':
103 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
104 break;
David Ahern3993f2c2017-04-27 09:11:13 -0700105 default:
106 usage(basename(argv[0]));
107 return 1;
108 }
Brenden Blanco86af8b42016-07-19 12:16:51 -0700109 }
110
Toke Høiland-Jørgensend50ecc42019-12-16 12:07:42 +0100111 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
112 xdp_flags |= XDP_FLAGS_DRV_MODE;
113
David Ahern3993f2c2017-04-27 09:11:13 -0700114 if (optind == argc) {
115 usage(basename(argv[0]));
116 return 1;
117 }
Tushar Dave6dfca8312017-10-27 16:12:30 -0700118
Ioana Ciornei11b36ab2019-01-09 17:06:55 +0000119 ifindex = if_nametoindex(argv[optind]);
Matteo Crocedc378a1a2018-12-01 01:23:06 +0100120 if (!ifindex) {
121 perror("if_nametoindex");
122 return 1;
123 }
David Ahern3993f2c2017-04-27 09:11:13 -0700124
125 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700126 prog_load_attr.file = filename;
Brenden Blanco86af8b42016-07-19 12:16:51 -0700127
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700128 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
129 return 1;
130
131 map = bpf_map__next(NULL, obj);
132 if (!map) {
133 printf("finding a map in obj file failed\n");
Brenden Blanco86af8b42016-07-19 12:16:51 -0700134 return 1;
135 }
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700136 map_fd = bpf_map__fd(map);
Brenden Blanco86af8b42016-07-19 12:16:51 -0700137
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700138 if (!prog_fd) {
Daniel T. Leeafbe3c22019-11-07 09:51:52 +0900139 printf("bpf_prog_load_xattr: %s\n", strerror(errno));
Brenden Blanco86af8b42016-07-19 12:16:51 -0700140 return 1;
141 }
142
143 signal(SIGINT, int_exit);
Andy Gospodarekad990db2017-05-11 15:52:30 -0400144 signal(SIGTERM, int_exit);
Brenden Blanco86af8b42016-07-19 12:16:51 -0700145
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700146 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
Brenden Blanco86af8b42016-07-19 12:16:51 -0700147 printf("link set xdp fd failed\n");
148 return 1;
149 }
150
Maciej Fijalkowski3b7a8ec2019-02-01 22:42:30 +0100151 err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
152 if (err) {
153 printf("can't get prog info - %s\n", strerror(errno));
154 return err;
155 }
156 prog_id = info.id;
157
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700158 poll_stats(map_fd, 2);
Brenden Blanco86af8b42016-07-19 12:16:51 -0700159
160 return 0;
161}