test_tcpbpf_user.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. static int bpf_find_map(const char *test, struct bpf_object *obj,
  50. const char *name)
  51. {
  52. struct bpf_map *map;
  53. map = bpf_object__find_map_by_name(obj, name);
  54. if (!map) {
  55. printf("%s:FAIL:map '%s' not found\n", test, name);
  56. return -1;
  57. }
  58. return bpf_map__fd(map);
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. const char *file = "test_tcpbpf_kern.o";
  63. struct tcpbpf_globals g = {0};
  64. const char *cg_path = "/foo";
  65. int error = EXIT_FAILURE;
  66. struct bpf_object *obj;
  67. int prog_fd, map_fd;
  68. int cg_fd = -1;
  69. __u32 key = 0;
  70. int rv;
  71. if (setup_cgroup_environment())
  72. goto err;
  73. cg_fd = create_and_get_cgroup(cg_path);
  74. if (!cg_fd)
  75. goto err;
  76. if (join_cgroup(cg_path))
  77. goto err;
  78. if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
  79. printf("FAILED: load_bpf_file failed for: %s\n", file);
  80. goto err;
  81. }
  82. rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0);
  83. if (rv) {
  84. printf("FAILED: bpf_prog_attach: %d (%s)\n",
  85. error, strerror(errno));
  86. goto err;
  87. }
  88. if (system("./tcp_server.py")) {
  89. printf("FAILED: TCP server\n");
  90. goto err;
  91. }
  92. map_fd = bpf_find_map(__func__, obj, "global_map");
  93. if (map_fd < 0)
  94. goto err;
  95. rv = bpf_map_lookup_elem(map_fd, &key, &g);
  96. if (rv != 0) {
  97. printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
  98. goto err;
  99. }
  100. if (verify_result(&g)) {
  101. printf("FAILED: Wrong stats\n");
  102. goto err;
  103. }
  104. printf("PASSED!\n");
  105. error = 0;
  106. err:
  107. bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
  108. close(cg_fd);
  109. cleanup_cgroup_environment();
  110. return error;
  111. }