1 /* $NetBSD: in_cksum.c,v 1.5 2015/10/18 18:27:25 christos Exp $ */
3 * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: in_cksum.c,v 1.5 2015/10/18 18:27:25 christos Exp $");
34 #include <sys/param.h>
36 #include <sys/resource.h>
45 #define cpu_in_cksum portable_cpu_in_cksum
46 #include "cpu_in_cksum.c"
48 #ifdef HAVE_CPU_IN_CKSUM
50 int cpu_in_cksum(struct mbuf*, int, int, uint32_t);
53 static bool random_aligned;
55 void panic(const char *, ...) __printflike(1, 2);
57 panic(const char *fmt, ...)
66 free_mbuf_chain(struct mbuf *m)
75 free_mbuf_chain(next);
79 allocate_mbuf_chain(char **lens)
88 off = random_aligned ? rand() % 64 : 0;
90 m = malloc(sizeof(struct m_hdr) + len + off);
92 err(EXIT_FAILURE, "malloc failed");
94 m->m_data = (char *)m + sizeof(struct m_hdr) + off;
97 m->m_next = allocate_mbuf_chain(lens + 1);
104 dump_mbuf(const struct mbuf *m, int len, int off)
110 printf("Starting len=%d off=%d:\n", len, off);
112 for (; m; m = m->m_next)
117 if (m == NULL || off > m->m_len)
118 errx(1, "out of data");
121 unsigned char *ptr = mtod(m, unsigned char *) + off;
122 unsigned char *eptr = ptr + m->m_len;
128 errx(1, "out of data");
129 ptr = mtod(m, unsigned char *);
130 eptr = ptr + m->m_len;
134 printf("%.2x ", *ptr++);
146 randomise_mbuf_chain(struct mbuf *m)
150 for (i = 0; i < m->m_len; i += sizeof(int)) {
152 if (i + sizeof(int) < (size_t)m->m_len)
156 memcpy(m->m_data + i, &data, len);
159 randomise_mbuf_chain(m->m_next);
163 mbuf_len(struct mbuf *m)
165 return m == NULL ? 0 : m->m_len + mbuf_len(m->m_next);
168 int in_cksum_portable(struct mbuf *, int);
169 int in_cksum(struct mbuf *, int);
172 main(int argc, char **argv)
175 struct timeval tv, old_tv;
176 int loops, old_sum, off, len;
177 #ifdef HAVE_CPU_IN_CKSUM
191 while ((c = getopt(argc, argv, "i:l:u:v")) != -1) {
194 iterations = atoi(optarg);
197 loops = atoi(optarg);
200 random_aligned = atoi(optarg);
206 errx(1, "%s [-l <loops>] [-u <unalign> [-i <iterations> "
207 "[<mbuf-size> ...]", getprogname());
211 for (; loops; --loops) {
212 if ((m = allocate_mbuf_chain(argv + 4)) == NULL)
214 randomise_mbuf_chain(m);
218 /* force one loop over all data */
222 off = len ? rand() % len : 0;
225 old_sum = portable_cpu_in_cksum(m, len, off, init_sum);
226 #ifdef HAVE_CPU_IN_CKSUM
228 printf("m->m_len=%d len=%d off=%d\n", m->m_len, len, off);
229 dump_mbuf(m, len, off);
231 new_sum = cpu_in_cksum(m, len, off, init_sum);
232 if (old_sum != new_sum)
233 errx(1, "comparison failed: %x %x", old_sum, new_sum);
241 getrusage(RUSAGE_SELF, &res);
243 for (i = iterations; i; --i)
244 (void)portable_cpu_in_cksum(m, len, off, init_sum);
245 getrusage(RUSAGE_SELF, &res);
246 timersub(&res.ru_utime, &tv, &old_tv);
248 printf("portable version: %jd.%06jd\n",
249 (intmax_t)old_tv.tv_sec, (intmax_t)old_tv.tv_usec);
251 #ifdef HAVE_CPU_IN_CKSUM
252 getrusage(RUSAGE_SELF, &res);
254 for (i = iterations; i; --i)
255 (void)cpu_in_cksum(m, len, off, init_sum);
256 getrusage(RUSAGE_SELF, &res);
257 timersub(&res.ru_utime, &tv, &tv);
259 printf("test version: %jd.%06jd\n",
260 (intmax_t)tv.tv_sec, (intmax_t)tv.tv_usec);
261 printf("relative time: %3.g%%\n",
262 100 * ((double)tv.tv_sec * 1e6 + tv.tv_usec) /
263 ((double)old_tv.tv_sec * 1e6 + old_tv.tv_usec + 1));