1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <sys/sysinfo.h> 4 #include <test_progs.h> 5 #include "network_helpers.h" 6 #include "netcnt_prog.skel.h" 7 #include "netcnt_common.h" 8 9 #define CG_NAME "/netcnt" 10 11 void serial_test_netcnt(void) 12 { 13 union percpu_net_cnt *percpu_netcnt = NULL; 14 struct bpf_cgroup_storage_key key; 15 int map_fd, percpu_map_fd; 16 struct netcnt_prog *skel; 17 unsigned long packets; 18 union net_cnt netcnt; 19 unsigned long bytes; 20 int cpu, nproc; 21 int cg_fd = -1; 22 char cmd[128]; 23 24 skel = netcnt_prog__open_and_load(); 25 if (!ASSERT_OK_PTR(skel, "netcnt_prog__open_and_load")) 26 return; 27 28 nproc = get_nprocs_conf(); 29 percpu_netcnt = malloc(sizeof(*percpu_netcnt) * nproc); 30 if (!ASSERT_OK_PTR(percpu_netcnt, "malloc(percpu_netcnt)")) 31 goto err; 32 33 cg_fd = test__join_cgroup(CG_NAME); 34 if (!ASSERT_GE(cg_fd, 0, "test__join_cgroup")) 35 goto err; 36 37 skel->links.bpf_nextcnt = bpf_program__attach_cgroup(skel->progs.bpf_nextcnt, cg_fd); 38 if (!ASSERT_OK_PTR(skel->links.bpf_nextcnt, 39 "attach_cgroup(bpf_nextcnt)")) 40 goto err; 41 42 snprintf(cmd, sizeof(cmd), "%s ::1 -A -c 10000 -q > /dev/null", ping_command(AF_INET6)); 43 ASSERT_OK(system(cmd), cmd); 44 45 map_fd = bpf_map__fd(skel->maps.netcnt); 46 if (!ASSERT_OK(bpf_map_get_next_key(map_fd, NULL, &key), "bpf_map_get_next_key")) 47 goto err; 48 49 if (!ASSERT_OK(bpf_map_lookup_elem(map_fd, &key, &netcnt), "bpf_map_lookup_elem(netcnt)")) 50 goto err; 51 52 percpu_map_fd = bpf_map__fd(skel->maps.percpu_netcnt); 53 if (!ASSERT_OK(bpf_map_lookup_elem(percpu_map_fd, &key, &percpu_netcnt[0]), 54 "bpf_map_lookup_elem(percpu_netcnt)")) 55 goto err; 56 57 /* Some packets can be still in per-cpu cache, but not more than 58 * MAX_PERCPU_PACKETS. 59 */ 60 packets = netcnt.packets; 61 bytes = netcnt.bytes; 62 for (cpu = 0; cpu < nproc; cpu++) { 63 ASSERT_LE(percpu_netcnt[cpu].packets, MAX_PERCPU_PACKETS, "MAX_PERCPU_PACKETS"); 64 65 packets += percpu_netcnt[cpu].packets; 66 bytes += percpu_netcnt[cpu].bytes; 67 } 68 69 /* No packets should be lost */ 70 ASSERT_EQ(packets, 10000, "packets"); 71 72 /* Let's check that bytes counter matches the number of packets 73 * multiplied by the size of ipv6 ICMP packet. 74 */ 75 ASSERT_EQ(bytes, packets * 104, "bytes"); 76 77 err: 78 if (cg_fd != -1) 79 close(cg_fd); 80 free(percpu_netcnt); 81 netcnt_prog__destroy(skel); 82 } 83