test_netcnt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <assert.h>
  7. #include <sys/sysinfo.h>
  8. #include <sys/time.h>
  9. #include <linux/bpf.h>
  10. #include <bpf/bpf.h>
  11. #include <bpf/libbpf.h>
  12. #include "cgroup_helpers.h"
  13. #include "bpf_rlimit.h"
  14. #include "netcnt_common.h"
  15. #define BPF_PROG "./netcnt_prog.o"
  16. #define TEST_CGROUP "/test-network-counters/"
  17. static int bpf_find_map(const char *test, struct bpf_object *obj,
  18. const char *name)
  19. {
  20. struct bpf_map *map;
  21. map = bpf_object__find_map_by_name(obj, name);
  22. if (!map) {
  23. printf("%s:FAIL:map '%s' not found\n", test, name);
  24. return -1;
  25. }
  26. return bpf_map__fd(map);
  27. }
  28. int main(int argc, char **argv)
  29. {
  30. struct percpu_net_cnt *percpu_netcnt;
  31. struct bpf_cgroup_storage_key key;
  32. int map_fd, percpu_map_fd;
  33. int error = EXIT_FAILURE;
  34. struct net_cnt netcnt;
  35. struct bpf_object *obj;
  36. int prog_fd, cgroup_fd;
  37. unsigned long packets;
  38. unsigned long bytes;
  39. int cpu, nproc;
  40. __u32 prog_cnt;
  41. nproc = get_nprocs_conf();
  42. percpu_netcnt = malloc(sizeof(*percpu_netcnt) * nproc);
  43. if (!percpu_netcnt) {
  44. printf("Not enough memory for per-cpu area (%d cpus)\n", nproc);
  45. goto err;
  46. }
  47. if (bpf_prog_load(BPF_PROG, BPF_PROG_TYPE_CGROUP_SKB,
  48. &obj, &prog_fd)) {
  49. printf("Failed to load bpf program\n");
  50. goto out;
  51. }
  52. if (setup_cgroup_environment()) {
  53. printf("Failed to load bpf program\n");
  54. goto err;
  55. }
  56. /* Create a cgroup, get fd, and join it */
  57. cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
  58. if (!cgroup_fd) {
  59. printf("Failed to create test cgroup\n");
  60. goto err;
  61. }
  62. if (join_cgroup(TEST_CGROUP)) {
  63. printf("Failed to join cgroup\n");
  64. goto err;
  65. }
  66. /* Attach bpf program */
  67. if (bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS, 0)) {
  68. printf("Failed to attach bpf program");
  69. goto err;
  70. }
  71. if (system("which ping6 &>/dev/null") == 0)
  72. assert(!system("ping6 localhost -c 10000 -f -q > /dev/null"));
  73. else
  74. assert(!system("ping -6 localhost -c 10000 -f -q > /dev/null"));
  75. if (bpf_prog_query(cgroup_fd, BPF_CGROUP_INET_EGRESS, 0, NULL, NULL,
  76. &prog_cnt)) {
  77. printf("Failed to query attached programs");
  78. goto err;
  79. }
  80. map_fd = bpf_find_map(__func__, obj, "netcnt");
  81. if (map_fd < 0) {
  82. printf("Failed to find bpf map with net counters");
  83. goto err;
  84. }
  85. percpu_map_fd = bpf_find_map(__func__, obj, "percpu_netcnt");
  86. if (percpu_map_fd < 0) {
  87. printf("Failed to find bpf map with percpu net counters");
  88. goto err;
  89. }
  90. if (bpf_map_get_next_key(map_fd, NULL, &key)) {
  91. printf("Failed to get key in cgroup storage\n");
  92. goto err;
  93. }
  94. if (bpf_map_lookup_elem(map_fd, &key, &netcnt)) {
  95. printf("Failed to lookup cgroup storage\n");
  96. goto err;
  97. }
  98. if (bpf_map_lookup_elem(percpu_map_fd, &key, &percpu_netcnt[0])) {
  99. printf("Failed to lookup percpu cgroup storage\n");
  100. goto err;
  101. }
  102. /* Some packets can be still in per-cpu cache, but not more than
  103. * MAX_PERCPU_PACKETS.
  104. */
  105. packets = netcnt.packets;
  106. bytes = netcnt.bytes;
  107. for (cpu = 0; cpu < nproc; cpu++) {
  108. if (percpu_netcnt[cpu].packets > MAX_PERCPU_PACKETS) {
  109. printf("Unexpected percpu value: %llu\n",
  110. percpu_netcnt[cpu].packets);
  111. goto err;
  112. }
  113. packets += percpu_netcnt[cpu].packets;
  114. bytes += percpu_netcnt[cpu].bytes;
  115. }
  116. /* No packets should be lost */
  117. if (packets != 10000) {
  118. printf("Unexpected packet count: %lu\n", packets);
  119. goto err;
  120. }
  121. /* Let's check that bytes counter matches the number of packets
  122. * multiplied by the size of ipv6 ICMP packet.
  123. */
  124. if (bytes != packets * 104) {
  125. printf("Unexpected bytes count: %lu\n", bytes);
  126. goto err;
  127. }
  128. error = 0;
  129. printf("test_netcnt:PASS\n");
  130. err:
  131. cleanup_cgroup_environment();
  132. free(percpu_netcnt);
  133. out:
  134. return error;
  135. }