0001
0002 #include <stdio.h>
0003 #include <stdlib.h>
0004 #include <string.h>
0005 #include <linux/perf_event.h>
0006 #include <linux/bpf.h>
0007 #include <net/if.h>
0008 #include <errno.h>
0009 #include <assert.h>
0010 #include <sys/sysinfo.h>
0011 #include <sys/ioctl.h>
0012 #include <signal.h>
0013 #include <bpf/libbpf.h>
0014 #include <bpf/bpf.h>
0015 #include <libgen.h>
0016 #include <linux/if_link.h>
0017
0018 #include "perf-sys.h"
0019
0020 static int if_idx;
0021 static char *if_name;
0022 static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
0023 static __u32 prog_id;
0024 static struct perf_buffer *pb = NULL;
0025
0026 static int do_attach(int idx, int fd, const char *name)
0027 {
0028 struct bpf_prog_info info = {};
0029 __u32 info_len = sizeof(info);
0030 int err;
0031
0032 err = bpf_xdp_attach(idx, fd, xdp_flags, NULL);
0033 if (err < 0) {
0034 printf("ERROR: failed to attach program to %s\n", name);
0035 return err;
0036 }
0037
0038 err = bpf_obj_get_info_by_fd(fd, &info, &info_len);
0039 if (err) {
0040 printf("can't get prog info - %s\n", strerror(errno));
0041 return err;
0042 }
0043 prog_id = info.id;
0044
0045 return err;
0046 }
0047
0048 static int do_detach(int idx, const char *name)
0049 {
0050 __u32 curr_prog_id = 0;
0051 int err = 0;
0052
0053 err = bpf_xdp_query_id(idx, xdp_flags, &curr_prog_id);
0054 if (err) {
0055 printf("bpf_xdp_query_id failed\n");
0056 return err;
0057 }
0058 if (prog_id == curr_prog_id) {
0059 err = bpf_xdp_detach(idx, xdp_flags, NULL);
0060 if (err < 0)
0061 printf("ERROR: failed to detach prog from %s\n", name);
0062 } else if (!curr_prog_id) {
0063 printf("couldn't find a prog id on a %s\n", name);
0064 } else {
0065 printf("program on interface changed, not removing\n");
0066 }
0067
0068 return err;
0069 }
0070
0071 #define SAMPLE_SIZE 64
0072
0073 static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size)
0074 {
0075 struct {
0076 __u16 cookie;
0077 __u16 pkt_len;
0078 __u8 pkt_data[SAMPLE_SIZE];
0079 } __packed *e = data;
0080 int i;
0081
0082 if (e->cookie != 0xdead) {
0083 printf("BUG cookie %x sized %d\n", e->cookie, size);
0084 return;
0085 }
0086
0087 printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len);
0088 for (i = 0; i < 14 && i < e->pkt_len; i++)
0089 printf("%02x ", e->pkt_data[i]);
0090 printf("\n");
0091 }
0092
0093 static void sig_handler(int signo)
0094 {
0095 do_detach(if_idx, if_name);
0096 perf_buffer__free(pb);
0097 exit(0);
0098 }
0099
0100 static void usage(const char *prog)
0101 {
0102 fprintf(stderr,
0103 "%s: %s [OPTS] <ifname|ifindex>\n\n"
0104 "OPTS:\n"
0105 " -F force loading prog\n"
0106 " -S use skb-mode\n",
0107 __func__, prog);
0108 }
0109
0110 int main(int argc, char **argv)
0111 {
0112 const char *optstr = "FS";
0113 int prog_fd, map_fd, opt;
0114 struct bpf_program *prog;
0115 struct bpf_object *obj;
0116 struct bpf_map *map;
0117 char filename[256];
0118 int ret, err;
0119
0120 while ((opt = getopt(argc, argv, optstr)) != -1) {
0121 switch (opt) {
0122 case 'F':
0123 xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
0124 break;
0125 case 'S':
0126 xdp_flags |= XDP_FLAGS_SKB_MODE;
0127 break;
0128 default:
0129 usage(basename(argv[0]));
0130 return 1;
0131 }
0132 }
0133
0134 if (!(xdp_flags & XDP_FLAGS_SKB_MODE))
0135 xdp_flags |= XDP_FLAGS_DRV_MODE;
0136
0137 if (optind == argc) {
0138 usage(basename(argv[0]));
0139 return 1;
0140 }
0141
0142 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
0143
0144 obj = bpf_object__open_file(filename, NULL);
0145 if (libbpf_get_error(obj))
0146 return 1;
0147
0148 prog = bpf_object__next_program(obj, NULL);
0149 bpf_program__set_type(prog, BPF_PROG_TYPE_XDP);
0150
0151 err = bpf_object__load(obj);
0152 if (err)
0153 return 1;
0154
0155 prog_fd = bpf_program__fd(prog);
0156
0157 map = bpf_object__next_map(obj, NULL);
0158 if (!map) {
0159 printf("finding a map in obj file failed\n");
0160 return 1;
0161 }
0162 map_fd = bpf_map__fd(map);
0163
0164 if_idx = if_nametoindex(argv[optind]);
0165 if (!if_idx)
0166 if_idx = strtoul(argv[optind], NULL, 0);
0167
0168 if (!if_idx) {
0169 fprintf(stderr, "Invalid ifname\n");
0170 return 1;
0171 }
0172 if_name = argv[optind];
0173 err = do_attach(if_idx, prog_fd, if_name);
0174 if (err)
0175 return err;
0176
0177 if (signal(SIGINT, sig_handler) ||
0178 signal(SIGHUP, sig_handler) ||
0179 signal(SIGTERM, sig_handler)) {
0180 perror("signal");
0181 return 1;
0182 }
0183
0184 pb = perf_buffer__new(map_fd, 8, print_bpf_output, NULL, NULL, NULL);
0185 err = libbpf_get_error(pb);
0186 if (err) {
0187 perror("perf_buffer setup failed");
0188 return 1;
0189 }
0190
0191 while ((ret = perf_buffer__poll(pb, 1000)) >= 0) {
0192 }
0193
0194 kill(0, SIGINT);
0195 return ret;
0196 }