npf_perf_test.c revision 1.1 1 1.1 rmind /* $NetBSD: npf_perf_test.c,v 1.1 2013/09/24 02:04:21 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.1 rmind #define NSECS 1 /* 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.1 rmind static struct mbuf *
25 1.1 rmind fill_packet(void)
26 1.1 rmind {
27 1.1 rmind struct mbuf *m;
28 1.1 rmind struct ip *ip;
29 1.1 rmind struct tcphdr *th;
30 1.1 rmind
31 1.1 rmind m = mbuf_construct(IPPROTO_TCP);
32 1.1 rmind th = mbuf_return_hdrs(m, false, &ip);
33 1.1 rmind ip->ip_src.s_addr = inet_addr(PUB_IP1);
34 1.1 rmind ip->ip_dst.s_addr = inet_addr(LOCAL_IP3);
35 1.1 rmind th->th_sport = htons(80);
36 1.1 rmind th->th_dport = htons(15000);
37 1.1 rmind return m;
38 1.1 rmind }
39 1.1 rmind
40 1.1 rmind static void
41 1.1 rmind worker(void *arg)
42 1.1 rmind {
43 1.1 rmind ifnet_t *ifp = ifunit(IFNAME_INT);
44 1.1 rmind uint64_t n = 0, *npackets = arg;
45 1.1 rmind struct mbuf *m = fill_packet();
46 1.1 rmind
47 1.1 rmind while (!run)
48 1.1 rmind /* spin-wait */;
49 1.1 rmind while (!done) {
50 1.1 rmind int error;
51 1.1 rmind
52 1.1 rmind error = npf_packet_handler(NULL, &m, ifp, PFIL_OUT);
53 1.1 rmind KASSERT(error == 0);
54 1.1 rmind n++;
55 1.1 rmind }
56 1.1 rmind *npackets = n;
57 1.1 rmind kthread_exit(0);
58 1.1 rmind }
59 1.1 rmind
60 1.1 rmind void
61 1.1 rmind npf_test_conc(unsigned nthreads)
62 1.1 rmind {
63 1.1 rmind uint64_t total = 0, *npackets;
64 1.1 rmind int error;
65 1.1 rmind lwp_t **l;
66 1.1 rmind
67 1.1 rmind npackets = kmem_zalloc(sizeof(uint64_t) * nthreads, KM_SLEEP);
68 1.1 rmind l = kmem_zalloc(sizeof(lwp_t *) * nthreads, KM_SLEEP);
69 1.1 rmind
70 1.1 rmind printf("THREADS\tPKTS\n");
71 1.1 rmind done = false;
72 1.1 rmind run = false;
73 1.1 rmind
74 1.1 rmind for (unsigned i = 0; i < nthreads; i++) {
75 1.1 rmind const int flags = KTHREAD_MUSTJOIN | KTHREAD_MPSAFE;
76 1.1 rmind error = kthread_create(PRI_NONE, flags, NULL,
77 1.1 rmind worker, &npackets[i], &l[i], "npfperf");
78 1.1 rmind KASSERT(error == 0);
79 1.1 rmind }
80 1.1 rmind
81 1.1 rmind /* Let them spin! */
82 1.1 rmind run = true;
83 1.1 rmind kpause("perf", false, NSECS * hz, NULL);
84 1.1 rmind done = true;
85 1.1 rmind
86 1.1 rmind /* Wait until all threads exit and sum the counts. */
87 1.1 rmind for (unsigned i = 0; i < nthreads; i++) {
88 1.1 rmind kthread_join(l[i]);
89 1.1 rmind total += npackets[i];
90 1.1 rmind }
91 1.1 rmind kmem_free(npackets, sizeof(uint64_t) * nthreads);
92 1.1 rmind kmem_free(l, sizeof(lwp_t *) * nthreads);
93 1.1 rmind
94 1.1 rmind printf("%u\t%" PRIu64 "\n", nthreads, total);
95 1.1 rmind }
96