blob: 0a197f86ac43a7a28b281d36efeedb5d423dd8dd [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>
Matteo Crocedc378a1a2018-12-01 01:23:06 +010018#include <net/if.h>
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010019
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010020#include "bpf_util.h"
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070021#include "bpf/bpf.h"
22#include "bpf/libbpf.h"
Brenden Blanco86af8b42016-07-19 12:16:51 -070023
Brenden Blanco86af8b42016-07-19 12:16:51 -070024static int ifindex;
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +020025static __u32 xdp_flags;
Brenden Blanco86af8b42016-07-19 12:16:51 -070026
27static void int_exit(int sig)
28{
Eric Leblondb259c2f2018-01-30 21:55:04 +010029 bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);
Brenden Blanco86af8b42016-07-19 12:16:51 -070030 exit(0);
31}
32
33/* simple per-protocol drop counter
34 */
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070035static void poll_stats(int map_fd, int interval)
Brenden Blanco86af8b42016-07-19 12:16:51 -070036{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010037 unsigned int nr_cpus = bpf_num_possible_cpus();
Matteo Croced606ee52018-12-01 01:23:05 +010038 __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
Brenden Blanco86af8b42016-07-19 12:16:51 -070039 int i;
40
Brenden Blanco86af8b42016-07-19 12:16:51 -070041 while (1) {
Matteo Croced606ee52018-12-01 01:23:05 +010042 __u32 key = UINT32_MAX;
43
Brenden Blanco86af8b42016-07-19 12:16:51 -070044 sleep(interval);
45
Matteo Croced606ee52018-12-01 01:23:05 +010046 while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
Brenden Blanco86af8b42016-07-19 12:16:51 -070047 __u64 sum = 0;
48
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070049 assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
Brenden Blanco86af8b42016-07-19 12:16:51 -070050 for (i = 0; i < nr_cpus; i++)
Matteo Croced606ee52018-12-01 01:23:05 +010051 sum += values[i];
52 if (sum > prev[key])
Brenden Blanco86af8b42016-07-19 12:16:51 -070053 printf("proto %u: %10llu pkt/s\n",
Matteo Croced606ee52018-12-01 01:23:05 +010054 key, (sum - prev[key]) / interval);
55 prev[key] = sum;
Brenden Blanco86af8b42016-07-19 12:16:51 -070056 }
57 }
58}
59
David Ahern3993f2c2017-04-27 09:11:13 -070060static void usage(const char *prog)
Brenden Blanco86af8b42016-07-19 12:16:51 -070061{
David Ahern3993f2c2017-04-27 09:11:13 -070062 fprintf(stderr,
Matteo Crocedc378a1a2018-12-01 01:23:06 +010063 "usage: %s [OPTS] IFACE\n\n"
David Ahern3993f2c2017-04-27 09:11:13 -070064 "OPTS:\n"
Daniel Borkmann0489df92017-05-12 01:04:45 +020065 " -S use skb-mode\n"
66 " -N enforce native mode\n",
David Ahern3993f2c2017-04-27 09:11:13 -070067 prog);
68}
69
70int main(int argc, char **argv)
71{
Tushar Dave6dfca8312017-10-27 16:12:30 -070072 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070073 struct bpf_prog_load_attr prog_load_attr = {
74 .prog_type = BPF_PROG_TYPE_XDP,
75 };
Daniel Borkmann0489df92017-05-12 01:04:45 +020076 const char *optstr = "SN";
Jakub Kicinskibe5bca42018-05-10 10:24:43 -070077 int prog_fd, map_fd, opt;
78 struct bpf_object *obj;
79 struct bpf_map *map;
Brenden Blanco86af8b42016-07-19 12:16:51 -070080 char filename[256];
81
David Ahern3993f2c2017-04-27 09:11:13 -070082 while ((opt = getopt(argc, argv, optstr)) != -1) {
83 switch (opt) {
84 case 'S':
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +020085 xdp_flags |= XDP_FLAGS_SKB_MODE;
David Ahern3993f2c2017-04-27 09:11:13 -070086 break;
Daniel Borkmann0489df92017-05-12 01:04:45 +020087 case 'N':
88 xdp_flags |= XDP_FLAGS_DRV_MODE;
89 break;
David Ahern3993f2c2017-04-27 09:11:13 -070090 default:
91 usage(basename(argv[0]));
92 return 1;
93 }
Brenden Blanco86af8b42016-07-19 12:16:51 -070094 }
95
David Ahern3993f2c2017-04-27 09:11:13 -070096 if (optind == argc) {
97 usage(basename(argv[0]));
98 return 1;
99 }
Tushar Dave6dfca8312017-10-27 16:12:30 -0700100
101 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
102 perror("setrlimit(RLIMIT_MEMLOCK)");
103 return 1;
104 }
105
Matteo Crocedc378a1a2018-12-01 01:23:06 +0100106 ifindex = if_nametoindex(argv[1]);
107 if (!ifindex) {
108 perror("if_nametoindex");
109 return 1;
110 }
David Ahern3993f2c2017-04-27 09:11:13 -0700111
112 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700113 prog_load_attr.file = filename;
Brenden Blanco86af8b42016-07-19 12:16:51 -0700114
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700115 if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
116 return 1;
117
118 map = bpf_map__next(NULL, obj);
119 if (!map) {
120 printf("finding a map in obj file failed\n");
Brenden Blanco86af8b42016-07-19 12:16:51 -0700121 return 1;
122 }
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700123 map_fd = bpf_map__fd(map);
Brenden Blanco86af8b42016-07-19 12:16:51 -0700124
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700125 if (!prog_fd) {
Brenden Blanco86af8b42016-07-19 12:16:51 -0700126 printf("load_bpf_file: %s\n", strerror(errno));
127 return 1;
128 }
129
130 signal(SIGINT, int_exit);
Andy Gospodarekad990db2017-05-11 15:52:30 -0400131 signal(SIGTERM, int_exit);
Brenden Blanco86af8b42016-07-19 12:16:51 -0700132
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700133 if (bpf_set_link_xdp_fd(ifindex, prog_fd, xdp_flags) < 0) {
Brenden Blanco86af8b42016-07-19 12:16:51 -0700134 printf("link set xdp fd failed\n");
135 return 1;
136 }
137
Jakub Kicinskibe5bca42018-05-10 10:24:43 -0700138 poll_stats(map_fd, 2);
Brenden Blanco86af8b42016-07-19 12:16:51 -0700139
140 return 0;
141}