0001
0002
0003 #include <sys/sysinfo.h>
0004 #include <test_progs.h>
0005 #include "network_helpers.h"
0006 #include "netcnt_prog.skel.h"
0007 #include "netcnt_common.h"
0008
0009 #define CG_NAME "/netcnt"
0010
0011 void serial_test_netcnt(void)
0012 {
0013 union percpu_net_cnt *percpu_netcnt = NULL;
0014 struct bpf_cgroup_storage_key key;
0015 int map_fd, percpu_map_fd;
0016 struct netcnt_prog *skel;
0017 unsigned long packets;
0018 union net_cnt netcnt;
0019 unsigned long bytes;
0020 int cpu, nproc;
0021 int cg_fd = -1;
0022 char cmd[128];
0023
0024 skel = netcnt_prog__open_and_load();
0025 if (!ASSERT_OK_PTR(skel, "netcnt_prog__open_and_load"))
0026 return;
0027
0028 nproc = bpf_num_possible_cpus();
0029 percpu_netcnt = malloc(sizeof(*percpu_netcnt) * nproc);
0030 if (!ASSERT_OK_PTR(percpu_netcnt, "malloc(percpu_netcnt)"))
0031 goto err;
0032
0033 cg_fd = test__join_cgroup(CG_NAME);
0034 if (!ASSERT_GE(cg_fd, 0, "test__join_cgroup"))
0035 goto err;
0036
0037 skel->links.bpf_nextcnt = bpf_program__attach_cgroup(skel->progs.bpf_nextcnt, cg_fd);
0038 if (!ASSERT_OK_PTR(skel->links.bpf_nextcnt,
0039 "attach_cgroup(bpf_nextcnt)"))
0040 goto err;
0041
0042 snprintf(cmd, sizeof(cmd), "%s ::1 -A -c 10000 -q > /dev/null", ping_command(AF_INET6));
0043 ASSERT_OK(system(cmd), cmd);
0044
0045 map_fd = bpf_map__fd(skel->maps.netcnt);
0046 if (!ASSERT_OK(bpf_map_get_next_key(map_fd, NULL, &key), "bpf_map_get_next_key"))
0047 goto err;
0048
0049 if (!ASSERT_OK(bpf_map_lookup_elem(map_fd, &key, &netcnt), "bpf_map_lookup_elem(netcnt)"))
0050 goto err;
0051
0052 percpu_map_fd = bpf_map__fd(skel->maps.percpu_netcnt);
0053 if (!ASSERT_OK(bpf_map_lookup_elem(percpu_map_fd, &key, &percpu_netcnt[0]),
0054 "bpf_map_lookup_elem(percpu_netcnt)"))
0055 goto err;
0056
0057
0058
0059
0060 packets = netcnt.packets;
0061 bytes = netcnt.bytes;
0062 for (cpu = 0; cpu < nproc; cpu++) {
0063 ASSERT_LE(percpu_netcnt[cpu].packets, MAX_PERCPU_PACKETS, "MAX_PERCPU_PACKETS");
0064
0065 packets += percpu_netcnt[cpu].packets;
0066 bytes += percpu_netcnt[cpu].bytes;
0067 }
0068
0069
0070 ASSERT_EQ(packets, 10000, "packets");
0071
0072
0073
0074
0075 ASSERT_EQ(bytes, packets * 104, "bytes");
0076
0077 err:
0078 if (cg_fd != -1)
0079 close(cg_fd);
0080 free(percpu_netcnt);
0081 netcnt_prog__destroy(skel);
0082 }