1 // SPDX-License-Identifier: GPL-2.0
7 #include <sys/sysinfo.h>
10 #include <linux/bpf.h>
12 #include <bpf/libbpf.h>
14 #include "cgroup_helpers.h"
15 #include "bpf_rlimit.h"
16 #include "netcnt_common.h"
18 #define BPF_PROG "./netcnt_prog.o"
19 #define TEST_CGROUP "/test-network-counters/"
21 static int bpf_find_map(const char *test, struct bpf_object *obj,
26 map = bpf_object__find_map_by_name(obj, name);
28 printf("%s:FAIL:map '%s' not found\n", test, name);
31 return bpf_map__fd(map);
34 int main(int argc, char **argv)
36 struct percpu_net_cnt *percpu_netcnt;
37 struct bpf_cgroup_storage_key key;
38 int map_fd, percpu_map_fd;
39 int error = EXIT_FAILURE;
40 struct net_cnt netcnt;
41 struct bpf_object *obj;
42 int prog_fd, cgroup_fd;
43 unsigned long packets;
48 nproc = get_nprocs_conf();
49 percpu_netcnt = malloc(sizeof(*percpu_netcnt) * nproc);
51 printf("Not enough memory for per-cpu area (%d cpus)\n", nproc);
55 if (bpf_prog_load(BPF_PROG, BPF_PROG_TYPE_CGROUP_SKB,
57 printf("Failed to load bpf program\n");
61 if (setup_cgroup_environment()) {
62 printf("Failed to load bpf program\n");
66 /* Create a cgroup, get fd, and join it */
67 cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
69 printf("Failed to create test cgroup\n");
73 if (join_cgroup(TEST_CGROUP)) {
74 printf("Failed to join cgroup\n");
78 /* Attach bpf program */
79 if (bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS, 0)) {
80 printf("Failed to attach bpf program");
84 assert(system("ping localhost -6 -c 10000 -f -q > /dev/null") == 0);
86 if (bpf_prog_query(cgroup_fd, BPF_CGROUP_INET_EGRESS, 0, NULL, NULL,
88 printf("Failed to query attached programs");
92 map_fd = bpf_find_map(__func__, obj, "netcnt");
94 printf("Failed to find bpf map with net counters");
98 percpu_map_fd = bpf_find_map(__func__, obj, "percpu_netcnt");
99 if (percpu_map_fd < 0) {
100 printf("Failed to find bpf map with percpu net counters");
104 if (bpf_map_get_next_key(map_fd, NULL, &key)) {
105 printf("Failed to get key in cgroup storage\n");
109 if (bpf_map_lookup_elem(map_fd, &key, &netcnt)) {
110 printf("Failed to lookup cgroup storage\n");
114 if (bpf_map_lookup_elem(percpu_map_fd, &key, &percpu_netcnt[0])) {
115 printf("Failed to lookup percpu cgroup storage\n");
119 /* Some packets can be still in per-cpu cache, but not more than
120 * MAX_PERCPU_PACKETS.
122 packets = netcnt.packets;
123 bytes = netcnt.bytes;
124 for (cpu = 0; cpu < nproc; cpu++) {
125 if (percpu_netcnt[cpu].packets > MAX_PERCPU_PACKETS) {
126 printf("Unexpected percpu value: %llu\n",
127 percpu_netcnt[cpu].packets);
131 packets += percpu_netcnt[cpu].packets;
132 bytes += percpu_netcnt[cpu].bytes;
135 /* No packets should be lost */
136 if (packets != 10000) {
137 printf("Unexpected packet count: %lu\n", packets);
141 /* Let's check that bytes counter matches the number of packets
142 * multiplied by the size of ipv6 ICMP packet.
144 if (bytes != packets * 104) {
145 printf("Unexpected bytes count: %lu\n", bytes);
150 printf("test_netcnt:PASS\n");
153 cleanup_cgroup_environment();