test_tcpbpf_user.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <inttypes.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <linux/bpf.h>
  9. #include <sys/types.h>
  10. #include <bpf/bpf.h>
  11. #include <bpf/libbpf.h>
  12. #include "bpf_rlimit.h"
  13. #include "bpf_util.h"
  14. #include "cgroup_helpers.h"
  15. #include "test_tcpbpf.h"
  16. #define EXPECT_EQ(expected, actual, fmt) \
  17. do { \
  18. if ((expected) != (actual)) { \
  19. printf(" Value of: " #actual "\n" \
  20. " Actual: %" fmt "\n" \
  21. " Expected: %" fmt "\n", \
  22. (actual), (expected)); \
  23. goto err; \
  24. } \
  25. } while (0)
  26. int verify_result(const struct tcpbpf_globals *result)
  27. {
  28. __u32 expected_events;
  29. expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
  30. (1 << BPF_SOCK_OPS_RWND_INIT) |
  31. (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) |
  32. (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) |
  33. (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) |
  34. (1 << BPF_SOCK_OPS_NEEDS_ECN) |
  35. (1 << BPF_SOCK_OPS_STATE_CB) |
  36. (1 << BPF_SOCK_OPS_TCP_LISTEN_CB));
  37. EXPECT_EQ(expected_events, result->event_map, "#" PRIx32);
  38. EXPECT_EQ(501ULL, result->bytes_received, "llu");
  39. EXPECT_EQ(1002ULL, result->bytes_acked, "llu");
  40. EXPECT_EQ(1, result->data_segs_in, PRIu32);
  41. EXPECT_EQ(1, result->data_segs_out, PRIu32);
  42. EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32);
  43. EXPECT_EQ(0, result->good_cb_test_rv, PRIu32);
  44. EXPECT_EQ(1, result->num_listen, PRIu32);
  45. return 0;
  46. err:
  47. return -1;
  48. }
  49. int verify_sockopt_result(int sock_map_fd)
  50. {
  51. __u32 key = 0;
  52. int res;
  53. int rv;
  54. /* check setsockopt for SAVE_SYN */
  55. rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
  56. EXPECT_EQ(0, rv, "d");
  57. EXPECT_EQ(0, res, "d");
  58. key = 1;
  59. /* check getsockopt for SAVED_SYN */
  60. rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
  61. EXPECT_EQ(0, rv, "d");
  62. EXPECT_EQ(1, res, "d");
  63. return 0;
  64. err:
  65. return -1;
  66. }
  67. static int bpf_find_map(const char *test, struct bpf_object *obj,
  68. const char *name)
  69. {
  70. struct bpf_map *map;
  71. map = bpf_object__find_map_by_name(obj, name);
  72. if (!map) {
  73. printf("%s:FAIL:map '%s' not found\n", test, name);
  74. return -1;
  75. }
  76. return bpf_map__fd(map);
  77. }
  78. int main(int argc, char **argv)
  79. {
  80. const char *file = "test_tcpbpf_kern.o";
  81. int prog_fd, map_fd, sock_map_fd;
  82. struct tcpbpf_globals g = {0};
  83. const char *cg_path = "/foo";
  84. int error = EXIT_FAILURE;
  85. struct bpf_object *obj;
  86. int cg_fd = -1;
  87. __u32 key = 0;
  88. int rv;
  89. if (setup_cgroup_environment())
  90. goto err;
  91. cg_fd = create_and_get_cgroup(cg_path);
  92. if (!cg_fd)
  93. goto err;
  94. if (join_cgroup(cg_path))
  95. goto err;
  96. if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
  97. printf("FAILED: load_bpf_file failed for: %s\n", file);
  98. goto err;
  99. }
  100. rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0);
  101. if (rv) {
  102. printf("FAILED: bpf_prog_attach: %d (%s)\n",
  103. error, strerror(errno));
  104. goto err;
  105. }
  106. if (system("./tcp_server.py")) {
  107. printf("FAILED: TCP server\n");
  108. goto err;
  109. }
  110. map_fd = bpf_find_map(__func__, obj, "global_map");
  111. if (map_fd < 0)
  112. goto err;
  113. sock_map_fd = bpf_find_map(__func__, obj, "sockopt_results");
  114. if (sock_map_fd < 0)
  115. goto err;
  116. rv = bpf_map_lookup_elem(map_fd, &key, &g);
  117. if (rv != 0) {
  118. printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
  119. goto err;
  120. }
  121. if (verify_result(&g)) {
  122. printf("FAILED: Wrong stats\n");
  123. goto err;
  124. }
  125. if (verify_sockopt_result(sock_map_fd)) {
  126. printf("FAILED: Wrong sockopt stats\n");
  127. goto err;
  128. }
  129. printf("PASSED!\n");
  130. error = 0;
  131. err:
  132. bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
  133. close(cg_fd);
  134. cleanup_cgroup_environment();
  135. return error;
  136. }