~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/netcnt.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 = bpf_num_possible_cpus();
 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_GE(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_GE(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 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php