test_tcpbpf_user.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <signal.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <linux/perf_event.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/bpf.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/time.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #include <bpf/bpf.h>
  19. #include <bpf/libbpf.h>
  20. #include "bpf_util.h"
  21. #include "bpf_rlimit.h"
  22. #include <linux/perf_event.h>
  23. #include "test_tcpbpf.h"
  24. static int bpf_find_map(const char *test, struct bpf_object *obj,
  25. const char *name)
  26. {
  27. struct bpf_map *map;
  28. map = bpf_object__find_map_by_name(obj, name);
  29. if (!map) {
  30. printf("%s:FAIL:map '%s' not found\n", test, name);
  31. return -1;
  32. }
  33. return bpf_map__fd(map);
  34. }
  35. #define SYSTEM(CMD) \
  36. do { \
  37. if (system(CMD)) { \
  38. printf("system(%s) FAILS!\n", CMD); \
  39. } \
  40. } while (0)
  41. int main(int argc, char **argv)
  42. {
  43. const char *file = "test_tcpbpf_kern.o";
  44. struct tcpbpf_globals g = {0};
  45. int cg_fd, prog_fd, map_fd;
  46. bool debug_flag = false;
  47. int error = EXIT_FAILURE;
  48. struct bpf_object *obj;
  49. char cmd[100], *dir;
  50. struct stat buffer;
  51. __u32 key = 0;
  52. int pid;
  53. int rv;
  54. if (argc > 1 && strcmp(argv[1], "-d") == 0)
  55. debug_flag = true;
  56. dir = "/tmp/cgroupv2/foo";
  57. if (stat(dir, &buffer) != 0) {
  58. SYSTEM("mkdir -p /tmp/cgroupv2");
  59. SYSTEM("mount -t cgroup2 none /tmp/cgroupv2");
  60. SYSTEM("mkdir -p /tmp/cgroupv2/foo");
  61. }
  62. pid = (int) getpid();
  63. sprintf(cmd, "echo %d >> /tmp/cgroupv2/foo/cgroup.procs", pid);
  64. SYSTEM(cmd);
  65. cg_fd = open(dir, O_DIRECTORY, O_RDONLY);
  66. if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
  67. printf("FAILED: load_bpf_file failed for: %s\n", file);
  68. goto err;
  69. }
  70. rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0);
  71. if (rv) {
  72. printf("FAILED: bpf_prog_attach: %d (%s)\n",
  73. error, strerror(errno));
  74. goto err;
  75. }
  76. SYSTEM("./tcp_server.py");
  77. map_fd = bpf_find_map(__func__, obj, "global_map");
  78. if (map_fd < 0)
  79. goto err;
  80. rv = bpf_map_lookup_elem(map_fd, &key, &g);
  81. if (rv != 0) {
  82. printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
  83. goto err;
  84. }
  85. if (g.bytes_received != 501 || g.bytes_acked != 1002 ||
  86. g.data_segs_in != 1 || g.data_segs_out != 1 ||
  87. (g.event_map ^ 0x47e) != 0 || g.bad_cb_test_rv != 0x80 ||
  88. g.good_cb_test_rv != 0) {
  89. printf("FAILED: Wrong stats\n");
  90. if (debug_flag) {
  91. printf("\n");
  92. printf("bytes_received: %d (expecting 501)\n",
  93. (int)g.bytes_received);
  94. printf("bytes_acked: %d (expecting 1002)\n",
  95. (int)g.bytes_acked);
  96. printf("data_segs_in: %d (expecting 1)\n",
  97. g.data_segs_in);
  98. printf("data_segs_out: %d (expecting 1)\n",
  99. g.data_segs_out);
  100. printf("event_map: 0x%x (at least 0x47e)\n",
  101. g.event_map);
  102. printf("bad_cb_test_rv: 0x%x (expecting 0x80)\n",
  103. g.bad_cb_test_rv);
  104. printf("good_cb_test_rv:0x%x (expecting 0)\n",
  105. g.good_cb_test_rv);
  106. }
  107. goto err;
  108. }
  109. printf("PASSED!\n");
  110. error = 0;
  111. err:
  112. bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
  113. return error;
  114. }