npf_perf_test.c revision 1.3.4.2 1 1.3.4.2 yamt /* $NetBSD: npf_perf_test.c,v 1.3.4.2 2014/05/22 11:43:07 yamt Exp $ */
2 1.3.4.2 yamt
3 1.3.4.2 yamt /*
4 1.3.4.2 yamt * NPF benchmarking.
5 1.3.4.2 yamt *
6 1.3.4.2 yamt * Public Domain.
7 1.3.4.2 yamt */
8 1.3.4.2 yamt
9 1.3.4.2 yamt #include <sys/types.h>
10 1.3.4.2 yamt #include <sys/param.h>
11 1.3.4.2 yamt
12 1.3.4.2 yamt #include <sys/kernel.h>
13 1.3.4.2 yamt #include <sys/kmem.h>
14 1.3.4.2 yamt #include <sys/kthread.h>
15 1.3.4.2 yamt
16 1.3.4.2 yamt #include "npf_impl.h"
17 1.3.4.2 yamt #include "npf_test.h"
18 1.3.4.2 yamt
19 1.3.4.2 yamt #define NSECS 10 /* seconds */
20 1.3.4.2 yamt
21 1.3.4.2 yamt static volatile int run;
22 1.3.4.2 yamt static volatile int done;
23 1.3.4.2 yamt
24 1.3.4.2 yamt static uint64_t * npackets;
25 1.3.4.2 yamt static bool stateful;
26 1.3.4.2 yamt
27 1.3.4.2 yamt static struct mbuf *
28 1.3.4.2 yamt fill_packet(unsigned i)
29 1.3.4.2 yamt {
30 1.3.4.2 yamt struct mbuf *m;
31 1.3.4.2 yamt struct ip *ip;
32 1.3.4.2 yamt struct udphdr *uh;
33 1.3.4.2 yamt char buf[32];
34 1.3.4.2 yamt
35 1.3.4.2 yamt m = mbuf_construct(IPPROTO_UDP);
36 1.3.4.2 yamt uh = mbuf_return_hdrs(m, false, &ip);
37 1.3.4.2 yamt
38 1.3.4.2 yamt snprintf(buf, sizeof(buf), "192.0.2.%u", i + i);
39 1.3.4.2 yamt ip->ip_src.s_addr = inet_addr(PUB_IP1);
40 1.3.4.2 yamt ip->ip_dst.s_addr = inet_addr(stateful ? LOCAL_IP2 : LOCAL_IP3);
41 1.3.4.2 yamt uh->uh_sport = htons(80);
42 1.3.4.2 yamt uh->uh_dport = htons(15000 + i);
43 1.3.4.2 yamt return m;
44 1.3.4.2 yamt }
45 1.3.4.2 yamt
46 1.3.4.2 yamt __dead static void
47 1.3.4.2 yamt worker(void *arg)
48 1.3.4.2 yamt {
49 1.3.4.2 yamt ifnet_t *ifp = ifunit(IFNAME_INT);
50 1.3.4.2 yamt unsigned int i = (uintptr_t)arg;
51 1.3.4.2 yamt struct mbuf *m = fill_packet(i);
52 1.3.4.2 yamt uint64_t n = 0;
53 1.3.4.2 yamt
54 1.3.4.2 yamt while (!run)
55 1.3.4.2 yamt /* spin-wait */;
56 1.3.4.2 yamt while (!done) {
57 1.3.4.2 yamt int error;
58 1.3.4.2 yamt
59 1.3.4.2 yamt error = npf_packet_handler(NULL, &m, ifp, PFIL_OUT);
60 1.3.4.2 yamt KASSERT(error == 0);
61 1.3.4.2 yamt n++;
62 1.3.4.2 yamt }
63 1.3.4.2 yamt npackets[i] = n;
64 1.3.4.2 yamt kthread_exit(0);
65 1.3.4.2 yamt }
66 1.3.4.2 yamt
67 1.3.4.2 yamt void
68 1.3.4.2 yamt npf_test_conc(bool st, unsigned nthreads)
69 1.3.4.2 yamt {
70 1.3.4.2 yamt uint64_t total = 0;
71 1.3.4.2 yamt int error;
72 1.3.4.2 yamt lwp_t **l;
73 1.3.4.2 yamt
74 1.3.4.2 yamt printf("THREADS\tPKTS\n");
75 1.3.4.2 yamt stateful = st;
76 1.3.4.2 yamt done = false;
77 1.3.4.2 yamt run = false;
78 1.3.4.2 yamt
79 1.3.4.2 yamt npackets = kmem_zalloc(sizeof(uint64_t) * nthreads, KM_SLEEP);
80 1.3.4.2 yamt l = kmem_zalloc(sizeof(lwp_t *) * nthreads, KM_SLEEP);
81 1.3.4.2 yamt
82 1.3.4.2 yamt for (unsigned i = 0; i < nthreads; i++) {
83 1.3.4.2 yamt const int flags = KTHREAD_MUSTJOIN | KTHREAD_MPSAFE;
84 1.3.4.2 yamt error = kthread_create(PRI_NONE, flags, NULL,
85 1.3.4.2 yamt worker, (void *)(uintptr_t)i, &l[i], "npfperf");
86 1.3.4.2 yamt KASSERT(error == 0);
87 1.3.4.2 yamt }
88 1.3.4.2 yamt
89 1.3.4.2 yamt /* Let them spin! */
90 1.3.4.2 yamt run = true;
91 1.3.4.2 yamt kpause("perf", false, NSECS * hz, NULL);
92 1.3.4.2 yamt done = true;
93 1.3.4.2 yamt
94 1.3.4.2 yamt /* Wait until all threads exit and sum the counts. */
95 1.3.4.2 yamt for (unsigned i = 0; i < nthreads; i++) {
96 1.3.4.2 yamt kthread_join(l[i]);
97 1.3.4.2 yamt total += npackets[i];
98 1.3.4.2 yamt }
99 1.3.4.2 yamt kmem_free(npackets, sizeof(uint64_t) * nthreads);
100 1.3.4.2 yamt kmem_free(l, sizeof(lwp_t *) * nthreads);
101 1.3.4.2 yamt
102 1.3.4.2 yamt printf("%u\t%" PRIu64 "\n", nthreads, total);
103 1.3.4.2 yamt }
104