test_cgrp2_attach.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #define _GNU_SOURCE
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stddef.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <assert.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <linux/bpf.h>
  28. #include <bpf/bpf.h>
  29. #include "bpf_insn.h"
  30. #include "bpf_util.h"
  31. enum {
  32. MAP_KEY_PACKETS,
  33. MAP_KEY_BYTES,
  34. };
  35. char bpf_log_buf[BPF_LOG_BUF_SIZE];
  36. static int prog_load(int map_fd, int verdict)
  37. {
  38. struct bpf_insn prog[] = {
  39. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), /* save r6 so it's not clobbered by BPF_CALL */
  40. /* Count packets */
  41. BPF_MOV64_IMM(BPF_REG_0, MAP_KEY_PACKETS), /* r0 = 0 */
  42. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */
  43. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  44. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */
  45. BPF_LD_MAP_FD(BPF_REG_1, map_fd), /* load map fd to r1 */
  46. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
  47. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  48. BPF_MOV64_IMM(BPF_REG_1, 1), /* r1 = 1 */
  49. BPF_ATOMIC_OP(BPF_DW, BPF_ADD, BPF_REG_0, BPF_REG_1, 0),
  50. /* Count bytes */
  51. BPF_MOV64_IMM(BPF_REG_0, MAP_KEY_BYTES), /* r0 = 1 */
  52. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */
  53. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  54. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */
  55. BPF_LD_MAP_FD(BPF_REG_1, map_fd),
  56. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
  57. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  58. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6, offsetof(struct __sk_buff, len)), /* r1 = skb->len */
  59. BPF_ATOMIC_OP(BPF_DW, BPF_ADD, BPF_REG_0, BPF_REG_1, 0),
  60. BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
  61. BPF_EXIT_INSN(),
  62. };
  63. size_t insns_cnt = ARRAY_SIZE(prog);
  64. LIBBPF_OPTS(bpf_prog_load_opts, opts,
  65. .log_buf = bpf_log_buf,
  66. .log_size = BPF_LOG_BUF_SIZE,
  67. );
  68. return bpf_prog_load(BPF_PROG_TYPE_CGROUP_SKB, NULL, "GPL",
  69. prog, insns_cnt, &opts);
  70. }
  71. static int usage(const char *argv0)
  72. {
  73. printf("Usage: %s [-d] [-D] <cg-path> <egress|ingress>\n", argv0);
  74. printf(" -d Drop Traffic\n");
  75. printf(" -D Detach filter, and exit\n");
  76. return EXIT_FAILURE;
  77. }
  78. static int attach_filter(int cg_fd, int type, int verdict)
  79. {
  80. int prog_fd, map_fd, ret, key;
  81. long long pkt_cnt, byte_cnt;
  82. map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL,
  83. sizeof(key), sizeof(byte_cnt),
  84. 256, NULL);
  85. if (map_fd < 0) {
  86. printf("Failed to create map: '%s'\n", strerror(errno));
  87. return EXIT_FAILURE;
  88. }
  89. prog_fd = prog_load(map_fd, verdict);
  90. printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf);
  91. if (prog_fd < 0) {
  92. printf("Failed to load prog: '%s'\n", strerror(errno));
  93. return EXIT_FAILURE;
  94. }
  95. ret = bpf_prog_attach(prog_fd, cg_fd, type, 0);
  96. if (ret < 0) {
  97. printf("Failed to attach prog to cgroup: '%s'\n",
  98. strerror(errno));
  99. return EXIT_FAILURE;
  100. }
  101. while (1) {
  102. key = MAP_KEY_PACKETS;
  103. assert(bpf_map_lookup_elem(map_fd, &key, &pkt_cnt) == 0);
  104. key = MAP_KEY_BYTES;
  105. assert(bpf_map_lookup_elem(map_fd, &key, &byte_cnt) == 0);
  106. printf("cgroup received %lld packets, %lld bytes\n",
  107. pkt_cnt, byte_cnt);
  108. sleep(1);
  109. }
  110. return EXIT_SUCCESS;
  111. }
  112. int main(int argc, char **argv)
  113. {
  114. int detach_only = 0, verdict = 1;
  115. enum bpf_attach_type type;
  116. int opt, cg_fd, ret;
  117. while ((opt = getopt(argc, argv, "Dd")) != -1) {
  118. switch (opt) {
  119. case 'd':
  120. verdict = 0;
  121. break;
  122. case 'D':
  123. detach_only = 1;
  124. break;
  125. default:
  126. return usage(argv[0]);
  127. }
  128. }
  129. if (argc - optind < 2)
  130. return usage(argv[0]);
  131. if (strcmp(argv[optind + 1], "ingress") == 0)
  132. type = BPF_CGROUP_INET_INGRESS;
  133. else if (strcmp(argv[optind + 1], "egress") == 0)
  134. type = BPF_CGROUP_INET_EGRESS;
  135. else
  136. return usage(argv[0]);
  137. cg_fd = open(argv[optind], O_DIRECTORY | O_RDONLY);
  138. if (cg_fd < 0) {
  139. printf("Failed to open cgroup path: '%s'\n", strerror(errno));
  140. return EXIT_FAILURE;
  141. }
  142. if (detach_only) {
  143. ret = bpf_prog_detach(cg_fd, type);
  144. printf("bpf_prog_detach() returned '%s' (%d)\n",
  145. strerror(errno), errno);
  146. } else
  147. ret = attach_filter(cg_fd, type, verdict);
  148. return ret;
  149. }