Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (c) 2016 PLUMgrid
0003  */
0004 #include <linux/bpf.h>
0005 #include <linux/if_link.h>
0006 #include <assert.h>
0007 #include <errno.h>
0008 #include <signal.h>
0009 #include <stdio.h>
0010 #include <stdlib.h>
0011 #include <string.h>
0012 #include <unistd.h>
0013 #include <libgen.h>
0014 #include <net/if.h>
0015 
0016 #include "bpf_util.h"
0017 #include <bpf/bpf.h>
0018 #include <bpf/libbpf.h>
0019 
0020 static int ifindex;
0021 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
0022 static __u32 prog_id;
0023 
0024 static void int_exit(int sig)
0025 {
0026     __u32 curr_prog_id = 0;
0027 
0028     if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
0029         printf("bpf_xdp_query_id failed\n");
0030         exit(1);
0031     }
0032     if (prog_id == curr_prog_id)
0033         bpf_xdp_detach(ifindex, xdp_flags, NULL);
0034     else if (!curr_prog_id)
0035         printf("couldn't find a prog id on a given interface\n");
0036     else
0037         printf("program on interface changed, not removing\n");
0038     exit(0);
0039 }
0040 
0041 /* simple per-protocol drop counter
0042  */
0043 static void poll_stats(int map_fd, int interval)
0044 {
0045     unsigned int nr_cpus = bpf_num_possible_cpus();
0046     __u64 values[nr_cpus], prev[UINT8_MAX] = { 0 };
0047     int i;
0048 
0049     while (1) {
0050         __u32 key = UINT32_MAX;
0051 
0052         sleep(interval);
0053 
0054         while (bpf_map_get_next_key(map_fd, &key, &key) != -1) {
0055             __u64 sum = 0;
0056 
0057             assert(bpf_map_lookup_elem(map_fd, &key, values) == 0);
0058             for (i = 0; i < nr_cpus; i++)
0059                 sum += values[i];
0060             if (sum > prev[key])
0061                 printf("proto %u: %10llu pkt/s\n",
0062                        key, (sum - prev[key]) / interval);
0063             prev[key] = sum;
0064         }
0065     }
0066 }
0067 
0068 static void usage(const char *prog)
0069 {
0070     fprintf(stderr,
0071         "usage: %s [OPTS] IFACE\n\n"
0072         "OPTS:\n"
0073         "    -S    use skb-mode\n"
0074         "    -N    enforce native mode\n"
0075         "    -F    force loading prog\n",
0076         prog);
0077 }
0078 
0079 int main(int argc, char **argv)
0080 {
0081     struct bpf_prog_info info = {};
0082     __u32 info_len = sizeof(info);
0083     const char *optstr = "FSN";
0084     int prog_fd, map_fd, opt;
0085     struct bpf_program *prog;
0086     struct bpf_object *obj;
0087     struct bpf_map *map;
0088     char filename[256];
0089     int err;
0090 
0091     while ((opt = getopt(argc, argv, optstr)) != -1) {
0092         switch (opt) {
0093         case 'S':
0094             xdp_flags |= XDP_FLAGS_SKB_MODE;
0095             break;
0096         case 'N':
0097             /* default, set below */
0098             break;
0099         case 'F':
0100             xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
0101             break;
0102         default:
0103             usage(basename(argv[0]));
0104             return 1;
0105         }
0106     }
0107 
0108     if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
0109         xdp_flags |= XDP_FLAGS_DRV_MODE;
0110 
0111     if (optind == argc) {
0112         usage(basename(argv[0]));
0113         return 1;
0114     }
0115 
0116     ifindex = if_nametoindex(argv[optind]);
0117     if (!ifindex) {
0118         perror("if_nametoindex");
0119         return 1;
0120     }
0121 
0122     snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0123     obj = bpf_object__open_file(filename, NULL);
0124     if (libbpf_get_error(obj))
0125         return 1;
0126 
0127     prog = bpf_object__next_program(obj, NULL);
0128     bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
0129 
0130     err = bpf_object__load(obj);
0131     if (err)
0132         return 1;
0133 
0134     prog_fd = bpf_program__fd(prog);
0135 
0136     map = bpf_object__next_map(obj, NULL);
0137     if (!map) {
0138         printf("finding a map in obj file failed\n");
0139         return 1;
0140     }
0141     map_fd = bpf_map__fd(map);
0142 
0143     if (!prog_fd) {
0144         printf("bpf_prog_load_xattr: %s\n", strerror(errno));
0145         return 1;
0146     }
0147 
0148     signal(SIGINT, int_exit);
0149     signal(SIGTERM, int_exit);
0150 
0151     if (bpf_xdp_attach(ifindex, prog_fd, xdp_flags, NULL) < 0) {
0152         printf("link set xdp fd failed\n");
0153         return 1;
0154     }
0155 
0156     err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
0157     if (err) {
0158         printf("can't get prog info - %s\n", strerror(errno));
0159         return err;
0160     }
0161     prog_id = info.id;
0162 
0163     poll_stats(map_fd, 1);
0164 
0165     return 0;
0166 }