test_cgroup_storage.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <assert.h>
  3. #include <bpf/bpf.h>
  4. #include <linux/filter.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "bpf_rlimit.h"
  8. #include "cgroup_helpers.h"
  9. char bpf_log_buf[BPF_LOG_BUF_SIZE];
  10. #define TEST_CGROUP "/test-bpf-cgroup-storage-buf/"
  11. int main(int argc, char **argv)
  12. {
  13. struct bpf_insn prog[] = {
  14. BPF_LD_MAP_FD(BPF_REG_1, 0), /* map fd */
  15. BPF_MOV64_IMM(BPF_REG_2, 0), /* flags, not used */
  16. BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
  17. BPF_FUNC_get_local_storage),
  18. BPF_MOV64_IMM(BPF_REG_1, 1),
  19. BPF_STX_XADD(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
  20. BPF_LDX_MEM(BPF_W, BPF_REG_1, BPF_REG_0, 0),
  21. BPF_ALU64_IMM(BPF_AND, BPF_REG_1, 0x1),
  22. BPF_MOV64_REG(BPF_REG_0, BPF_REG_1),
  23. BPF_EXIT_INSN(),
  24. };
  25. size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn);
  26. int error = EXIT_FAILURE;
  27. int map_fd, prog_fd, cgroup_fd;
  28. struct bpf_cgroup_storage_key key;
  29. unsigned long long value;
  30. map_fd = bpf_create_map(BPF_MAP_TYPE_CGROUP_STORAGE, sizeof(key),
  31. sizeof(value), 0, 0);
  32. if (map_fd < 0) {
  33. printf("Failed to create map: %s\n", strerror(errno));
  34. goto out;
  35. }
  36. prog[0].imm = map_fd;
  37. prog_fd = bpf_load_program(BPF_PROG_TYPE_CGROUP_SKB,
  38. prog, insns_cnt, "GPL", 0,
  39. bpf_log_buf, BPF_LOG_BUF_SIZE);
  40. if (prog_fd < 0) {
  41. printf("Failed to load bpf program: %s\n", bpf_log_buf);
  42. goto out;
  43. }
  44. if (setup_cgroup_environment()) {
  45. printf("Failed to setup cgroup environment\n");
  46. goto err;
  47. }
  48. /* Create a cgroup, get fd, and join it */
  49. cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
  50. if (!cgroup_fd) {
  51. printf("Failed to create test cgroup\n");
  52. goto err;
  53. }
  54. if (join_cgroup(TEST_CGROUP)) {
  55. printf("Failed to join cgroup\n");
  56. goto err;
  57. }
  58. /* Attach the bpf program */
  59. if (bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS, 0)) {
  60. printf("Failed to attach bpf program\n");
  61. goto err;
  62. }
  63. if (bpf_map_get_next_key(map_fd, NULL, &key)) {
  64. printf("Failed to get the first key in cgroup storage\n");
  65. goto err;
  66. }
  67. if (bpf_map_lookup_elem(map_fd, &key, &value)) {
  68. printf("Failed to lookup cgroup storage\n");
  69. goto err;
  70. }
  71. /* Every second packet should be dropped */
  72. assert(system("ping localhost -c 1 -W 1 -q > /dev/null") == 0);
  73. assert(system("ping localhost -c 1 -W 1 -q > /dev/null"));
  74. assert(system("ping localhost -c 1 -W 1 -q > /dev/null") == 0);
  75. /* Check the counter in the cgroup local storage */
  76. if (bpf_map_lookup_elem(map_fd, &key, &value)) {
  77. printf("Failed to lookup cgroup storage\n");
  78. goto err;
  79. }
  80. if (value != 3) {
  81. printf("Unexpected data in the cgroup storage: %llu\n", value);
  82. goto err;
  83. }
  84. /* Bump the counter in the cgroup local storage */
  85. value++;
  86. if (bpf_map_update_elem(map_fd, &key, &value, 0)) {
  87. printf("Failed to update the data in the cgroup storage\n");
  88. goto err;
  89. }
  90. /* Every second packet should be dropped */
  91. assert(system("ping localhost -c 1 -W 1 -q > /dev/null") == 0);
  92. assert(system("ping localhost -c 1 -W 1 -q > /dev/null"));
  93. assert(system("ping localhost -c 1 -W 1 -q > /dev/null") == 0);
  94. /* Check the final value of the counter in the cgroup local storage */
  95. if (bpf_map_lookup_elem(map_fd, &key, &value)) {
  96. printf("Failed to lookup the cgroup storage\n");
  97. goto err;
  98. }
  99. if (value != 7) {
  100. printf("Unexpected data in the cgroup storage: %llu\n", value);
  101. goto err;
  102. }
  103. error = 0;
  104. printf("test_cgroup_storage:PASS\n");
  105. err:
  106. cleanup_cgroup_environment();
  107. out:
  108. return error;
  109. }