in_cksum.c revision 1.3 1 1.3 joerg /* $NetBSD: in_cksum.c,v 1.3 2015/01/06 21:36:38 joerg Exp $ */
2 1.1 christos /*-
3 1.1 christos * Copyright (c) 2008 Joerg Sonnenberger <joerg (at) NetBSD.org>.
4 1.1 christos * All rights reserved.
5 1.1 christos *
6 1.1 christos * Redistribution and use in source and binary forms, with or without
7 1.1 christos * modification, are permitted provided that the following conditions
8 1.1 christos * are met:
9 1.1 christos *
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in
14 1.1 christos * the documentation and/or other materials provided with the
15 1.1 christos * distribution.
16 1.1 christos *
17 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 1.1 christos * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 1.1 christos * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 christos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 christos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 christos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 christos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 1.1 christos * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 christos * SUCH DAMAGE.
29 1.1 christos */
30 1.1 christos
31 1.1 christos #include <sys/cdefs.h>
32 1.3 joerg __KERNEL_RCSID(0, "$NetBSD: in_cksum.c,v 1.3 2015/01/06 21:36:38 joerg Exp $");
33 1.1 christos
34 1.1 christos #include <sys/param.h>
35 1.1 christos #include <sys/mbuf.h>
36 1.1 christos #include <sys/resource.h>
37 1.1 christos #include <err.h>
38 1.1 christos #include <stdbool.h>
39 1.1 christos #include <stdio.h>
40 1.2 christos #include <stdarg.h>
41 1.1 christos #include <unistd.h>
42 1.1 christos #include <stdlib.h>
43 1.1 christos #include <string.h>
44 1.1 christos
45 1.1 christos #define cpu_in_cksum portable_cpu_in_cksum
46 1.1 christos #include "cpu_in_cksum.c"
47 1.1 christos
48 1.1 christos #ifdef HAVE_CPU_IN_CKSUM
49 1.2 christos #undef cpu_in_cksum
50 1.1 christos int cpu_in_cksum(struct mbuf*, int, int, uint32_t);
51 1.1 christos #endif
52 1.1 christos
53 1.1 christos static bool random_aligned;
54 1.1 christos
55 1.3 joerg void panic(const char *, ...) __printflike(1, 2);
56 1.2 christos void
57 1.2 christos panic(const char *fmt, ...)
58 1.2 christos {
59 1.2 christos va_list ap;
60 1.2 christos va_start(ap, fmt);
61 1.2 christos verrx(1, fmt, ap);
62 1.2 christos va_end(ap);
63 1.2 christos }
64 1.2 christos
65 1.1 christos static void
66 1.1 christos free_mbuf_chain(struct mbuf *m)
67 1.1 christos {
68 1.1 christos struct mbuf *next;
69 1.1 christos
70 1.1 christos if (m == NULL)
71 1.1 christos return;
72 1.1 christos
73 1.1 christos next = m->m_next;
74 1.1 christos free(m);
75 1.1 christos free_mbuf_chain(next);
76 1.1 christos }
77 1.1 christos
78 1.1 christos static struct mbuf *
79 1.1 christos allocate_mbuf_chain(char **lens)
80 1.1 christos {
81 1.1 christos int len, off;
82 1.1 christos struct mbuf *m;
83 1.1 christos
84 1.1 christos if (*lens == NULL)
85 1.1 christos return NULL;
86 1.1 christos
87 1.1 christos len = atoi(*lens);
88 1.1 christos off = random_aligned ? rand() % 64 : 0;
89 1.1 christos
90 1.1 christos m = malloc(sizeof(struct m_hdr) + len + off);
91 1.1 christos if (m == NULL)
92 1.1 christos err(EXIT_FAILURE, "malloc failed");
93 1.1 christos
94 1.1 christos m->m_data = (char *)m + sizeof(struct m_hdr) + off;
95 1.1 christos m->m_len = len;
96 1.1 christos
97 1.1 christos m->m_next = allocate_mbuf_chain(lens + 1);
98 1.1 christos
99 1.1 christos return m;
100 1.1 christos }
101 1.1 christos
102 1.1 christos static void
103 1.1 christos randomise_mbuf_chain(struct mbuf *m)
104 1.1 christos {
105 1.1 christos int i, data, len;
106 1.1 christos
107 1.1 christos for (i = 0; i < m->m_len; i += sizeof(int)) {
108 1.1 christos data = rand();
109 1.1 christos if (i + sizeof(int) < (size_t)m->m_len)
110 1.1 christos len = sizeof(int);
111 1.1 christos else
112 1.1 christos len = m->m_len - i;
113 1.1 christos memcpy(m->m_data + i, &data, len);
114 1.1 christos }
115 1.1 christos if (m->m_next)
116 1.1 christos randomise_mbuf_chain(m->m_next);
117 1.1 christos }
118 1.1 christos
119 1.1 christos static int
120 1.1 christos mbuf_len(struct mbuf *m)
121 1.1 christos {
122 1.1 christos return m == NULL ? 0 : m->m_len + mbuf_len(m->m_next);
123 1.1 christos }
124 1.1 christos
125 1.1 christos int in_cksum_portable(struct mbuf *, int);
126 1.1 christos int in_cksum(struct mbuf *, int);
127 1.1 christos
128 1.1 christos int
129 1.1 christos main(int argc, char **argv)
130 1.1 christos {
131 1.1 christos struct rusage res;
132 1.1 christos struct timeval tv, old_tv;
133 1.1 christos int loops, old_sum, off, len;
134 1.1 christos #ifdef HAVE_CPU_IN_CKSUM
135 1.1 christos int new_sum;
136 1.1 christos #endif
137 1.1 christos long i, iterations;
138 1.1 christos uint32_t init_sum;
139 1.1 christos struct mbuf *m;
140 1.1 christos bool verbose;
141 1.1 christos int c;
142 1.1 christos
143 1.1 christos loops = 16;
144 1.1 christos verbose = false;
145 1.1 christos random_aligned = 0;
146 1.1 christos iterations = 100000;
147 1.1 christos
148 1.1 christos while ((c = getopt(argc, argv, "i:l:u:v")) != -1) {
149 1.1 christos switch (c) {
150 1.1 christos case 'i':
151 1.1 christos iterations = atoi(optarg);
152 1.1 christos break;
153 1.1 christos case 'l':
154 1.1 christos loops = atoi(optarg);
155 1.1 christos break;
156 1.1 christos case 'u':
157 1.1 christos random_aligned = atoi(optarg);
158 1.1 christos break;
159 1.1 christos case 'v':
160 1.1 christos verbose = true;
161 1.1 christos break;
162 1.1 christos default:
163 1.1 christos errx(1, "%s [-l <loops>] [-u <unalign> [-i <iterations> "
164 1.1 christos "[<mbuf-size> ...]", getprogname());
165 1.1 christos }
166 1.1 christos }
167 1.1 christos
168 1.1 christos for (; loops; --loops) {
169 1.1 christos if ((m = allocate_mbuf_chain(argv + 4)) == NULL)
170 1.1 christos continue;
171 1.1 christos randomise_mbuf_chain(m);
172 1.1 christos init_sum = rand();
173 1.1 christos len = mbuf_len(m);
174 1.1 christos
175 1.1 christos /* force one loop over all data */
176 1.1 christos if (loops == 1)
177 1.1 christos off = 0;
178 1.1 christos else
179 1.1 christos off = len ? rand() % len : 0;
180 1.1 christos
181 1.1 christos len -= off;
182 1.1 christos old_sum = portable_cpu_in_cksum(m, len, off, init_sum);
183 1.1 christos #ifdef HAVE_CPU_IN_CKSUM
184 1.1 christos new_sum = cpu_in_cksum(m, len, off, init_sum);
185 1.1 christos if (old_sum != new_sum)
186 1.1 christos errx(1, "comparison failed: %x %x", old_sum, new_sum);
187 1.1 christos #else
188 1.1 christos __USE(old_sum);
189 1.1 christos #endif
190 1.1 christos
191 1.1 christos if (iterations == 0)
192 1.1 christos continue;
193 1.1 christos
194 1.1 christos getrusage(RUSAGE_SELF, &res);
195 1.1 christos tv = res.ru_utime;
196 1.1 christos for (i = iterations; i; --i)
197 1.1 christos (void)portable_cpu_in_cksum(m, len, off, init_sum);
198 1.1 christos getrusage(RUSAGE_SELF, &res);
199 1.1 christos timersub(&res.ru_utime, &tv, &old_tv);
200 1.1 christos if (verbose)
201 1.1 christos printf("portable version: %jd.%06jd\n",
202 1.1 christos (intmax_t)old_tv.tv_sec, (intmax_t)old_tv.tv_usec);
203 1.1 christos
204 1.1 christos #ifdef HAVE_CPU_IN_CKSUM
205 1.1 christos getrusage(RUSAGE_SELF, &res);
206 1.1 christos tv = res.ru_utime;
207 1.1 christos for (i = iterations; i; --i)
208 1.1 christos (void)cpu_in_cksum(m, len, off, init_sum);
209 1.1 christos getrusage(RUSAGE_SELF, &res);
210 1.1 christos timersub(&res.ru_utime, &tv, &tv);
211 1.1 christos if (verbose) {
212 1.1 christos printf("test version: %jd.%06jd\n",
213 1.1 christos (intmax_t)tv.tv_sec, (intmax_t)tv.tv_usec);
214 1.1 christos printf("relative time: %3.g%%\n",
215 1.1 christos 100 * ((double)tv.tv_sec * 1e6 + tv.tv_usec) /
216 1.1 christos ((double)old_tv.tv_sec * 1e6 + old_tv.tv_usec + 1));
217 1.1 christos }
218 1.1 christos #endif
219 1.1 christos free_mbuf_chain(m);
220 1.1 christos }
221 1.1 christos
222 1.1 christos return 0;
223 1.1 christos }
224