blob: 4f3d824fc044c552a708fb8d2e604139abd37b09 [file] [log] [blame]
Brenden Blanco86af8b42016-07-19 12:16:51 -07001/* Copyright (c) 2016 PLUMgrid
2 *
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>
David Ahern3993f2c2017-04-27 09:11:13 -07008#include <linux/if_link.h>
Brenden Blanco86af8b42016-07-19 12:16:51 -07009#include <assert.h>
10#include <errno.h>
11#include <signal.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
Brenden Blanco86af8b42016-07-19 12:16:51 -070015#include <unistd.h>
David Ahern3993f2c2017-04-27 09:11:13 -070016#include <libgen.h>
Tushar Dave6dfca8312017-10-27 16:12:30 -070017#include <sys/resource.h>
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010018
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010019#include "bpf_util.h"
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070020#include "bpf/bpf.h"
21#include "bpf/libbpf.h"
Brenden Blanco86af8b42016-07-19 12:16:51 -070022
Brenden Blanco86af8b42016-07-19 12:16:51 -070023static int ifindex;
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +020024static __u32 xdp_flags;
Brenden Blanco86af8b42016-07-19 12:16:51 -070025
26static void int_exit(int sig)
27{
Eric Leblondb259c2f2018-01-30 21:55:04 +010028 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
Brenden Blanco86af8b42016-07-19 12:16:51 -070029 exit(0);
30}
31
32/* simple per-protocol drop counter
33 */
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070034static void poll_stats(int map_fd, int interval)
Brenden Blanco86af8b42016-07-19 12:16:51 -070035{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010036 unsigned int nr_cpus = bpf_num_possible_cpus();
Matteo Croced606ee52018-12-01 01:23:05 +010037 __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
Brenden Blanco86af8b42016-07-19 12:16:51 -070038 int i;
39
Brenden Blanco86af8b42016-07-19 12:16:51 -070040 while (1) {
Matteo Croced606ee52018-12-01 01:23:05 +010041 __u32 key = UINT32_MAX;
42
Brenden Blanco86af8b42016-07-19 12:16:51 -070043 sleep(interval);
44
Matteo Croced606ee52018-12-01 01:23:05 +010045 while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
Brenden Blanco86af8b42016-07-19 12:16:51 -070046 __u64 sum = 0;
47
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070048 assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
Brenden Blanco86af8b42016-07-19 12:16:51 -070049 for (i = 0; i < nr_cpus; i++)
Matteo Croced606ee52018-12-01 01:23:05 +010050 sum += values[i];
51 if (sum > prev[key])
Brenden Blanco86af8b42016-07-19 12:16:51 -070052 printf("proto %u: %10llu pkt/s\n",
Matteo Croced606ee52018-12-01 01:23:05 +010053 key, (sum - prev[key]) / interval);
54 prev[key] = sum;
Brenden Blanco86af8b42016-07-19 12:16:51 -070055 }
56 }
57}
58
David Ahern3993f2c2017-04-27 09:11:13 -070059static void usage(const char *prog)
Brenden Blanco86af8b42016-07-19 12:16:51 -070060{
David Ahern3993f2c2017-04-27 09:11:13 -070061 fprintf(stderr,
62 "usage: %s [OPTS] IFINDEX\n\n"
63 "OPTS:\n"
Daniel Borkmann0489df92017-05-12 01:04:45 +020064 " -S use skb-mode\n"
65 " -N enforce native mode\n",
David Ahern3993f2c2017-04-27 09:11:13 -070066 prog);
67}
68
69int main(int argc, char **argv)
70{
Tushar Dave6dfca8312017-10-27 16:12:30 -070071 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070072 struct bpf_prog_load_attr prog_load_attr = {
73 .prog_type = BPF_PROG_TYPE_XDP,
74 };
Daniel Borkmann0489df92017-05-12 01:04:45 +020075 const char *optstr = "SN";
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070076 int prog_fd, map_fd, opt;
77 struct bpf_object *obj;
78 struct bpf_map *map;
Brenden Blanco86af8b42016-07-19 12:16:51 -070079 char filename[256];
80
David Ahern3993f2c2017-04-27 09:11:13 -070081 while ((opt = getopt(argc, argv, optstr)) != -1) {
82 switch (opt) {
83 case 'S':
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +020084 xdp_flags |= XDP_FLAGS_SKB_MODE;
David Ahern3993f2c2017-04-27 09:11:13 -070085 break;
Daniel Borkmann0489df92017-05-12 01:04:45 +020086 case 'N':
87 xdp_flags |= XDP_FLAGS_DRV_MODE;
88 break;
David Ahern3993f2c2017-04-27 09:11:13 -070089 default:
90 usage(basename(argv[0]));
91 return 1;
92 }
Brenden Blanco86af8b42016-07-19 12:16:51 -070093 }
94
David Ahern3993f2c2017-04-27 09:11:13 -070095 if (optind == argc) {
96 usage(basename(argv[0]));
97 return 1;
98 }
Tushar Dave6dfca8312017-10-27 16:12:30 -070099
100 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
101 perror("setrlimit(RLIMIT_MEMLOCK)");
102 return 1;
103 }
104
David Ahern3993f2c2017-04-27 09:11:13 -0700105 ifindex = strtoul(argv[optind], NULL, 0);
106
107 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700108 prog_load_attr.file = filename;
Brenden Blanco86af8b42016-07-19 12:16:51 -0700109
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700110 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
111 return 1;
112
113 map = bpf_map__next(NULL, obj);
114 if (!map) {
115 printf("finding a map in obj file failed\n");
Brenden Blanco86af8b42016-07-19 12:16:51 -0700116 return 1;
117 }
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700118 map_fd = bpf_map__fd(map);
Brenden Blanco86af8b42016-07-19 12:16:51 -0700119
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700120 if (!prog_fd) {
Brenden Blanco86af8b42016-07-19 12:16:51 -0700121 printf("load_bpf_file: %s\n", strerror(errno));
122 return 1;
123 }
124
125 signal(SIGINT, int_exit);
Andy Gospodarekad990db2017-05-11 15:52:30 -0400126 signal(SIGTERM, int_exit);
Brenden Blanco86af8b42016-07-19 12:16:51 -0700127
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700128 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
Brenden Blanco86af8b42016-07-19 12:16:51 -0700129 printf("link set xdp fd failed\n");
130 return 1;
131 }
132
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700133 poll_stats(map_fd, 2);
Brenden Blanco86af8b42016-07-19 12:16:51 -0700134
135 return 0;
136}