0001
0002
0003 #include <linux/stddef.h>
0004 #include <linux/bpf.h>
0005 #include <bpf/bpf_helpers.h>
0006 #include <bpf/bpf_tracing.h>
0007 #include <../../../tools/include/linux/filter.h>
0008 #include <linux/btf.h>
0009
0010 char _license[] SEC("license") = "GPL";
0011
0012 struct args {
0013 __u64 log_buf;
0014 __u32 log_size;
0015 int max_entries;
0016 int map_fd;
0017 int prog_fd;
0018 int btf_fd;
0019 };
0020
0021 #define BTF_INFO_ENC(kind, kind_flag, vlen) \
0022 ((!!(kind_flag) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
0023 #define BTF_TYPE_ENC(name, info, size_or_type) (name), (info), (size_or_type)
0024 #define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
0025 ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
0026 #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
0027 BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
0028 BTF_INT_ENC(encoding, bits_offset, bits)
0029
0030 static int btf_load(void)
0031 {
0032 struct btf_blob {
0033 struct btf_header btf_hdr;
0034 __u32 types[8];
0035 __u32 str;
0036 } raw_btf = {
0037 .btf_hdr = {
0038 .magic = BTF_MAGIC,
0039 .version = BTF_VERSION,
0040 .hdr_len = sizeof(struct btf_header),
0041 .type_len = sizeof(__u32) * 8,
0042 .str_off = sizeof(__u32) * 8,
0043 .str_len = sizeof(__u32),
0044 },
0045 .types = {
0046
0047 BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),
0048
0049 BTF_TYPE_INT_ENC(0, 0, 0, 64, 8),
0050 },
0051 };
0052 static union bpf_attr btf_load_attr = {
0053 .btf_size = sizeof(raw_btf),
0054 };
0055
0056 btf_load_attr.btf = (long)&raw_btf;
0057 return bpf_sys_bpf(BPF_BTF_LOAD, &btf_load_attr, sizeof(btf_load_attr));
0058 }
0059
0060 SEC("syscall")
0061 int bpf_prog(struct args *ctx)
0062 {
0063 static char license[] = "GPL";
0064 static struct bpf_insn insns[] = {
0065 BPF_ST_MEM(BPF_DW, BPF_REG_10, -8, 0),
0066 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
0067 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -8),
0068 BPF_LD_MAP_FD(BPF_REG_1, 0),
0069 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
0070 BPF_MOV64_IMM(BPF_REG_0, 0),
0071 BPF_EXIT_INSN(),
0072 };
0073 static union bpf_attr map_create_attr = {
0074 .map_type = BPF_MAP_TYPE_HASH,
0075 .key_size = 8,
0076 .value_size = 8,
0077 .btf_key_type_id = 1,
0078 .btf_value_type_id = 2,
0079 };
0080 static union bpf_attr map_update_attr = { .map_fd = 1, };
0081 static __u64 key = 12;
0082 static __u64 value = 34;
0083 static union bpf_attr prog_load_attr = {
0084 .prog_type = BPF_PROG_TYPE_XDP,
0085 .insn_cnt = sizeof(insns) / sizeof(insns[0]),
0086 };
0087 int ret;
0088
0089 ret = btf_load();
0090 if (ret <= 0)
0091 return ret;
0092
0093 ctx->btf_fd = ret;
0094 map_create_attr.max_entries = ctx->max_entries;
0095 map_create_attr.btf_fd = ret;
0096
0097 prog_load_attr.license = (long) license;
0098 prog_load_attr.insns = (long) insns;
0099 prog_load_attr.log_buf = ctx->log_buf;
0100 prog_load_attr.log_size = ctx->log_size;
0101 prog_load_attr.log_level = 1;
0102
0103 ret = bpf_sys_bpf(BPF_MAP_CREATE, &map_create_attr, sizeof(map_create_attr));
0104 if (ret <= 0)
0105 return ret;
0106 ctx->map_fd = ret;
0107 insns[3].imm = ret;
0108
0109 map_update_attr.map_fd = ret;
0110 map_update_attr.key = (long) &key;
0111 map_update_attr.value = (long) &value;
0112 ret = bpf_sys_bpf(BPF_MAP_UPDATE_ELEM, &map_update_attr, sizeof(map_update_attr));
0113 if (ret < 0)
0114 return ret;
0115
0116 ret = bpf_sys_bpf(BPF_PROG_LOAD, &prog_load_attr, sizeof(prog_load_attr));
0117 if (ret <= 0)
0118 return ret;
0119 ctx->prog_fd = ret;
0120 return 1;
0121 }