test_cgrp2_attach.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * - Detaches any eBPF program previously attached to the cgroup
  14. *
  15. * - Attaches the new program to a cgroup using BPF_PROG_ATTACH
  16. *
  17. * - Every second, reads map[0] and map[1] to see how many bytes and
  18. * packets were seen on any socket of tasks in the given cgroup.
  19. */
  20. #define _GNU_SOURCE
  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. #include <linux/bpf.h>
  30. #include "libbpf.h"
  31. enum {
  32. MAP_KEY_PACKETS,
  33. MAP_KEY_BYTES,
  34. };
  35. static int prog_load(int map_fd, int verdict)
  36. {
  37. struct bpf_insn prog[] = {
  38. BPF_MOV64_REG(BPF_REG_6, BPF_REG_1), /* save r6 so it's not clobbered by BPF_CALL */
  39. /* Count packets */
  40. BPF_MOV64_IMM(BPF_REG_0, MAP_KEY_PACKETS), /* r0 = 0 */
  41. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */
  42. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  43. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */
  44. BPF_LD_MAP_FD(BPF_REG_1, map_fd), /* load map fd to r1 */
  45. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
  46. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  47. BPF_MOV64_IMM(BPF_REG_1, 1), /* r1 = 1 */
  48. BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0), /* xadd r0 += r1 */
  49. /* Count bytes */
  50. BPF_MOV64_IMM(BPF_REG_0, MAP_KEY_BYTES), /* r0 = 1 */
  51. BPF_STX_MEM(BPF_W, BPF_REG_10, BPF_REG_0, -4), /* *(u32 *)(fp - 4) = r0 */
  52. BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),
  53. BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), /* r2 = fp - 4 */
  54. BPF_LD_MAP_FD(BPF_REG_1, map_fd),
  55. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
  56. BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2),
  57. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_6, offsetof(struct __sk_buff, len)), /* r1 = skb->len */
  58. BPF_RAW_INSN(BPF_STX | BPF_XADD | BPF_DW, BPF_REG_0, BPF_REG_1, 0, 0), /* xadd r0 += r1 */
  59. BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
  60. BPF_EXIT_INSN(),
  61. };
  62. return bpf_prog_load(BPF_PROG_TYPE_CGROUP_SKB,
  63. prog, sizeof(prog), "GPL", 0);
  64. }
  65. static int usage(const char *argv0)
  66. {
  67. printf("Usage: %s <cg-path> <egress|ingress> [drop]\n", argv0);
  68. return EXIT_FAILURE;
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. int cg_fd, map_fd, prog_fd, key, ret;
  73. long long pkt_cnt, byte_cnt;
  74. enum bpf_attach_type type;
  75. int verdict = 1;
  76. if (argc < 3)
  77. return usage(argv[0]);
  78. if (strcmp(argv[2], "ingress") == 0)
  79. type = BPF_CGROUP_INET_INGRESS;
  80. else if (strcmp(argv[2], "egress") == 0)
  81. type = BPF_CGROUP_INET_EGRESS;
  82. else
  83. return usage(argv[0]);
  84. if (argc > 3 && strcmp(argv[3], "drop") == 0)
  85. verdict = 0;
  86. cg_fd = open(argv[1], O_DIRECTORY | O_RDONLY);
  87. if (cg_fd < 0) {
  88. printf("Failed to open cgroup path: '%s'\n", strerror(errno));
  89. return EXIT_FAILURE;
  90. }
  91. map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY,
  92. sizeof(key), sizeof(byte_cnt),
  93. 256, 0);
  94. if (map_fd < 0) {
  95. printf("Failed to create map: '%s'\n", strerror(errno));
  96. return EXIT_FAILURE;
  97. }
  98. prog_fd = prog_load(map_fd, verdict);
  99. printf("Output from kernel verifier:\n%s\n-------\n", bpf_log_buf);
  100. if (prog_fd < 0) {
  101. printf("Failed to load prog: '%s'\n", strerror(errno));
  102. return EXIT_FAILURE;
  103. }
  104. ret = bpf_prog_detach(cg_fd, type);
  105. printf("bpf_prog_detach() returned '%s' (%d)\n", strerror(errno), errno);
  106. ret = bpf_prog_attach(prog_fd, cg_fd, type);
  107. if (ret < 0) {
  108. printf("Failed to attach prog to cgroup: '%s'\n",
  109. strerror(errno));
  110. return EXIT_FAILURE;
  111. }
  112. while (1) {
  113. key = MAP_KEY_PACKETS;
  114. assert(bpf_lookup_elem(map_fd, &key, &pkt_cnt) == 0);
  115. key = MAP_KEY_BYTES;
  116. assert(bpf_lookup_elem(map_fd, &key, &byte_cnt) == 0);
  117. printf("cgroup received %lld packets, %lld bytes\n",
  118. pkt_cnt, byte_cnt);
  119. sleep(1);
  120. }
  121. return EXIT_SUCCESS;
  122. }