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