1 /* eBPF example program: 2 * 3 * - Creates arraymap in kernel with 4 bytes keys and 8 byte values 4 * 5 * - Loads eBPF program 6 * 7 * The eBPF program accesses the map passed in to store two pieces of 8 * information. The number of invocations of the program, which maps 9 * to the number of packets received, is stored to key 0. Key 1 is 10 * incremented on each iteration by the number of bytes stored in 11 * the skb. 12 * 13 * - Attaches the new program to a cgroup using BPF_PROG_ATTACH 14 * 15 * - Every second, reads map[0] and map[1] to see how many bytes and 16 * packets were seen on any socket of tasks in the given cgroup. 17 */ 18 19 #define _GNU_SOURCE 20 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <stddef.h> 24 #include <string.h> 25 #include <unistd.h> 26 #include <assert.h> 27 #include <errno.h> 28 #include <fcntl.h> 29 30 #include <linux/bpf.h> 31 32 #include "libbpf.h" 33 34 enum { 35 MAP_KEY_PACKETS, 36 MAP_KEY_BYTES, 37 }; 38 39 static int prog_load(int map_fd, int verdict) 40 { 41 struct bpf_insn prog[] = { 42 BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), /* save r6 so it's not clobbered by BPF_CALL */ 43 44 /* Count packets */ 45 BPF_MOV64_IMM(BPF_REG_0, MAP_KEY_PACKETS), /* r0 = 0 */ 46 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */ 47 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 48 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */ 49 BPF_LD_MAP_FD(BPF_REG_1, map_fd), /* load map fd to r1 */ 50 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 51 BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), 52 BPF_MOV64_IMM(BPF_REG_1, 1), /* r1 = 1 */ 53 BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0), /* xadd r0 += r1 */ 54 55 /* Count bytes */ 56 BPF_MOV64_IMM(BPF_REG_0, MAP_KEY_BYTES), /* r0 = 1 */ 57 BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */ 58 BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), 59 BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */ 60 BPF_LD_MAP_FD(BPF_REG_1, map_fd), 61 BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), 62 BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2), 63 BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6, offsetof(struct __sk_buff, len)), /* r1 = skb->len */ 64 BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0), /* xadd r0 += r1 */ 65 66 BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ 67 BPF_EXIT_INSN(), 68 }; 69 70 return bpf_prog_load(BPF_PROG_TYPE_CGROUP_SKB, 71 prog, sizeof(prog), "GPL", 0); 72 } 73 74 static int usage(const char *argv0) 75 { 76 printf("Usage: %s [-d] [-D] <cg-path> <egress|ingress>\n", argv0); 77 printf(" -d Drop Traffic\n"); 78 printf(" -D Detach filter, and exit\n"); 79 return EXIT_FAILURE; 80 } 81 82 static int attach_filter(int cg_fd, int type, int verdict) 83 { 84 int prog_fd, map_fd, ret, key; 85 long long pkt_cnt, byte_cnt; 86 87 map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, 88 sizeof(key), sizeof(byte_cnt), 89 256, 0); 90 if (map_fd < 0) { 91 printf("Failed to create map: '%s'\n", strerror(errno)); 92 return EXIT_FAILURE; 93 } 94 95 prog_fd = prog_load(map_fd, verdict); 96 printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf); 97 98 if (prog_fd < 0) { 99 printf("Failed to load prog: '%s'\n", strerror(errno)); 100 return EXIT_FAILURE; 101 } 102 103 ret = bpf_prog_attach(prog_fd, cg_fd, type); 104 if (ret < 0) { 105 printf("Failed to attach prog to cgroup: '%s'\n", 106 strerror(errno)); 107 return EXIT_FAILURE; 108 } 109 while (1) { 110 key = MAP_KEY_PACKETS; 111 assert(bpf_lookup_elem(map_fd, &key, &pkt_cnt) == 0); 112 113 key = MAP_KEY_BYTES; 114 assert(bpf_lookup_elem(map_fd, &key, &byte_cnt) == 0); 115 116 printf("cgroup received %lld packets, %lld bytes\n", 117 pkt_cnt, byte_cnt); 118 sleep(1); 119 } 120 121 return EXIT_SUCCESS; 122 } 123 124 int main(int argc, char **argv) 125 { 126 int detach_only = 0, verdict = 1; 127 enum bpf_attach_type type; 128 int opt, cg_fd, ret; 129 130 while ((opt = getopt(argc, argv, "Dd")) != -1) { 131 switch (opt) { 132 case 'd': 133 verdict = 0; 134 break; 135 case 'D': 136 detach_only = 1; 137 break; 138 default: 139 return usage(argv[0]); 140 } 141 } 142 143 if (argc - optind < 2) 144 return usage(argv[0]); 145 146 if (strcmp(argv[optind + 1], "ingress") == 0) 147 type = BPF_CGROUP_INET_INGRESS; 148 else if (strcmp(argv[optind + 1], "egress") == 0) 149 type = BPF_CGROUP_INET_EGRESS; 150 else 151 return usage(argv[0]); 152 153 cg_fd = open(argv[optind], O_DIRECTORY | O_RDONLY); 154 if (cg_fd < 0) { 155 printf("Failed to open cgroup path: '%s'\n", strerror(errno)); 156 return EXIT_FAILURE; 157 } 158 159 if (detach_only) { 160 ret = bpf_prog_detach(cg_fd, type); 161 printf("bpf_prog_detach() returned '%s' (%d)\n", 162 strerror(errno), errno); 163 } else 164 ret = attach_filter(cg_fd, type, verdict); 165 166 return ret; 167 } 168