]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - samples/bpf/test_cgrp2_attach.c
8283ef86d39292247ba5d811f0d9ef2133b19228
1 /* eBPF example program:
3 * - Creates arraymap in kernel with 4 bytes keys and 8 byte values
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
13 * - Attaches the new program to a cgroup using BPF_PROG_ATTACH
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.
30 #include <linux/bpf.h>
39 char bpf_log_buf
[BPF_LOG_BUF_SIZE
];
41 static int prog_load(int map_fd
, int verdict
)
43 struct bpf_insn prog
[] = {
44 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
), /* save r6 so it's not clobbered by BPF_CALL */
47 BPF_MOV64_IMM(BPF_REG_0
, MAP_KEY_PACKETS
), /* r0 = 0 */
48 BPF_STX_MEM(BPF_W
, BPF_REG_10
, BPF_REG_0
, -4), /* *(u32 *)(fp - 4) = r0 */
49 BPF_MOV64_REG(BPF_REG_2
, BPF_REG_10
),
50 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, -4), /* r2 = fp - 4 */
51 BPF_LD_MAP_FD(BPF_REG_1
, map_fd
), /* load map fd to r1 */
52 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_map_lookup_elem
),
53 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 2),
54 BPF_MOV64_IMM(BPF_REG_1
, 1), /* r1 = 1 */
55 BPF_RAW_INSN(BPF_STX
| BPF_XADD
| BPF_DW
, BPF_REG_0
, BPF_REG_1
, 0, 0), /* xadd r0 += r1 */
58 BPF_MOV64_IMM(BPF_REG_0
, MAP_KEY_BYTES
), /* r0 = 1 */
59 BPF_STX_MEM(BPF_W
, BPF_REG_10
, BPF_REG_0
, -4), /* *(u32 *)(fp - 4) = r0 */
60 BPF_MOV64_REG(BPF_REG_2
, BPF_REG_10
),
61 BPF_ALU64_IMM(BPF_ADD
, BPF_REG_2
, -4), /* r2 = fp - 4 */
62 BPF_LD_MAP_FD(BPF_REG_1
, map_fd
),
63 BPF_RAW_INSN(BPF_JMP
| BPF_CALL
, 0, 0, 0, BPF_FUNC_map_lookup_elem
),
64 BPF_JMP_IMM(BPF_JEQ
, BPF_REG_0
, 0, 2),
65 BPF_LDX_MEM(BPF_W
, BPF_REG_1
, BPF_REG_6
, offsetof(struct __sk_buff
, len
)), /* r1 = skb->len */
66 BPF_RAW_INSN(BPF_STX
| BPF_XADD
| BPF_DW
, BPF_REG_0
, BPF_REG_1
, 0, 0), /* xadd r0 += r1 */
68 BPF_MOV64_IMM(BPF_REG_0
, verdict
), /* r0 = verdict */
72 return bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB
,
73 prog
, sizeof(prog
), "GPL", 0,
74 bpf_log_buf
, BPF_LOG_BUF_SIZE
);
77 static int usage(const char *argv0
)
79 printf("Usage: %s [-d] [-D] <cg-path> <egress|ingress>\n", argv0
);
80 printf(" -d Drop Traffic\n");
81 printf(" -D Detach filter, and exit\n");
85 static int attach_filter(int cg_fd
, int type
, int verdict
)
87 int prog_fd
, map_fd
, ret
, key
;
88 long long pkt_cnt
, byte_cnt
;
90 map_fd
= bpf_create_map(BPF_MAP_TYPE_ARRAY
,
91 sizeof(key
), sizeof(byte_cnt
),
94 printf("Failed to create map: '%s'\n", strerror(errno
));
98 prog_fd
= prog_load(map_fd
, verdict
);
99 printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf
);
102 printf("Failed to load prog: '%s'\n", strerror(errno
));
106 ret
= bpf_prog_attach(prog_fd
, cg_fd
, type
);
108 printf("Failed to attach prog to cgroup: '%s'\n",
113 key
= MAP_KEY_PACKETS
;
114 assert(bpf_map_lookup_elem(map_fd
, &key
, &pkt_cnt
) == 0);
117 assert(bpf_map_lookup_elem(map_fd
, &key
, &byte_cnt
) == 0);
119 printf("cgroup received %lld packets, %lld bytes\n",
127 int main(int argc
, char **argv
)
129 int detach_only
= 0, verdict
= 1;
130 enum bpf_attach_type type
;
133 while ((opt
= getopt(argc
, argv
, "Dd")) != -1) {
142 return usage(argv
[0]);
146 if (argc
- optind
< 2)
147 return usage(argv
[0]);
149 if (strcmp(argv
[optind
+ 1], "ingress") == 0)
150 type
= BPF_CGROUP_INET_INGRESS
;
151 else if (strcmp(argv
[optind
+ 1], "egress") == 0)
152 type
= BPF_CGROUP_INET_EGRESS
;
154 return usage(argv
[0]);
156 cg_fd
= open(argv
[optind
], O_DIRECTORY
| O_RDONLY
);
158 printf("Failed to open cgroup path: '%s'\n", strerror(errno
));
163 ret
= bpf_prog_detach(cg_fd
, type
);
164 printf("bpf_prog_detach() returned '%s' (%d)\n",
165 strerror(errno
), errno
);
167 ret
= attach_filter(cg_fd
, type
, verdict
);