0001 #include <linux/unistd.h>
0002 #include <linux/bpf.h>
0003
0004 #include <stdio.h>
0005 #include <stdlib.h>
0006 #include <stdint.h>
0007 #include <unistd.h>
0008 #include <string.h>
0009 #include <assert.h>
0010 #include <errno.h>
0011
0012 #include <sys/types.h>
0013 #include <sys/socket.h>
0014
0015 #include <bpf/bpf.h>
0016
0017 #include <bpf/libbpf.h>
0018 #include "bpf_insn.h"
0019 #include "sock_example.h"
0020 #include "bpf_util.h"
0021
0022 #define BPF_F_PIN (1 << 0)
0023 #define BPF_F_GET (1 << 1)
0024 #define BPF_F_PIN_GET (BPF_F_PIN | BPF_F_GET)
0025
0026 #define BPF_F_KEY (1 << 2)
0027 #define BPF_F_VAL (1 << 3)
0028 #define BPF_F_KEY_VAL (BPF_F_KEY | BPF_F_VAL)
0029
0030 #define BPF_M_UNSPEC 0
0031 #define BPF_M_MAP 1
0032 #define BPF_M_PROG 2
0033
0034 char bpf_log_buf[BPF_LOG_BUF_SIZE];
0035
0036 static void usage(void)
0037 {
0038 printf("Usage: fds_example [...]\n");
0039 printf(" -F <file> File to pin/get object\n");
0040 printf(" -P |- pin object\n");
0041 printf(" -G `- get object\n");
0042 printf(" -m eBPF map mode\n");
0043 printf(" -k <key> |- map key\n");
0044 printf(" -v <value> `- map value\n");
0045 printf(" -p eBPF prog mode\n");
0046 printf(" -o <object> `- object file\n");
0047 printf(" -h Display this help.\n");
0048 }
0049
0050 static int bpf_prog_create(const char *object)
0051 {
0052 static struct bpf_insn insns[] = {
0053 BPF_MOV64_IMM(BPF_REG_0, 1),
0054 BPF_EXIT_INSN(),
0055 };
0056 size_t insns_cnt = ARRAY_SIZE(insns);
0057 struct bpf_object *obj;
0058 int err;
0059
0060 if (object) {
0061 obj = bpf_object__open_file(object, NULL);
0062 assert(!libbpf_get_error(obj));
0063 err = bpf_object__load(obj);
0064 assert(!err);
0065 return bpf_program__fd(bpf_object__next_program(obj, NULL));
0066 } else {
0067 LIBBPF_OPTS(bpf_prog_load_opts, opts,
0068 .log_buf = bpf_log_buf,
0069 .log_size = BPF_LOG_BUF_SIZE,
0070 );
0071
0072 return bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
0073 insns, insns_cnt, &opts);
0074 }
0075 }
0076
0077 static int bpf_do_map(const char *file, uint32_t flags, uint32_t key,
0078 uint32_t value)
0079 {
0080 int fd, ret;
0081
0082 if (flags & BPF_F_PIN) {
0083 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(uint32_t),
0084 sizeof(uint32_t), 1024, NULL);
0085 printf("bpf: map fd:%d (%s)\n", fd, strerror(errno));
0086 assert(fd > 0);
0087
0088 ret = bpf_obj_pin(fd, file);
0089 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
0090 assert(ret == 0);
0091 } else {
0092 fd = bpf_obj_get(file);
0093 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
0094 assert(fd > 0);
0095 }
0096
0097 if ((flags & BPF_F_KEY_VAL) == BPF_F_KEY_VAL) {
0098 ret = bpf_map_update_elem(fd, &key, &value, 0);
0099 printf("bpf: fd:%d u->(%u:%u) ret:(%d,%s)\n", fd, key, value,
0100 ret, strerror(errno));
0101 assert(ret == 0);
0102 } else if (flags & BPF_F_KEY) {
0103 ret = bpf_map_lookup_elem(fd, &key, &value);
0104 printf("bpf: fd:%d l->(%u):%u ret:(%d,%s)\n", fd, key, value,
0105 ret, strerror(errno));
0106 assert(ret == 0);
0107 }
0108
0109 return 0;
0110 }
0111
0112 static int bpf_do_prog(const char *file, uint32_t flags, const char *object)
0113 {
0114 int fd, sock, ret;
0115
0116 if (flags & BPF_F_PIN) {
0117 fd = bpf_prog_create(object);
0118 printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
0119 assert(fd > 0);
0120
0121 ret = bpf_obj_pin(fd, file);
0122 printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
0123 assert(ret == 0);
0124 } else {
0125 fd = bpf_obj_get(file);
0126 printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
0127 assert(fd > 0);
0128 }
0129
0130 sock = open_raw_sock("lo");
0131 assert(sock > 0);
0132
0133 ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
0134 printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)\n", sock, fd,
0135 ret, strerror(errno));
0136 assert(ret == 0);
0137
0138 return 0;
0139 }
0140
0141 int main(int argc, char **argv)
0142 {
0143 const char *file = NULL, *object = NULL;
0144 uint32_t key = 0, value = 0, flags = 0;
0145 int opt, mode = BPF_M_UNSPEC;
0146
0147 while ((opt = getopt(argc, argv, "F:PGmk:v:po:")) != -1) {
0148 switch (opt) {
0149
0150 case 'F':
0151 file = optarg;
0152 break;
0153 case 'P':
0154 flags |= BPF_F_PIN;
0155 break;
0156 case 'G':
0157 flags |= BPF_F_GET;
0158 break;
0159
0160 case 'm':
0161 mode = BPF_M_MAP;
0162 break;
0163 case 'k':
0164 key = strtoul(optarg, NULL, 0);
0165 flags |= BPF_F_KEY;
0166 break;
0167 case 'v':
0168 value = strtoul(optarg, NULL, 0);
0169 flags |= BPF_F_VAL;
0170 break;
0171
0172 case 'p':
0173 mode = BPF_M_PROG;
0174 break;
0175 case 'o':
0176 object = optarg;
0177 break;
0178 default:
0179 goto out;
0180 }
0181 }
0182
0183 if (!(flags & BPF_F_PIN_GET) || !file)
0184 goto out;
0185
0186 switch (mode) {
0187 case BPF_M_MAP:
0188 return bpf_do_map(file, flags, key, value);
0189 case BPF_M_PROG:
0190 return bpf_do_prog(file, flags, object);
0191 }
0192 out:
0193 usage();
0194 return -1;
0195 }