1 /* $NetBSD: in_cksum.c,v 1.3 2015/01/06 21:36:38 joerg 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.3 2015/01/06 21:36:38 joerg 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);
103 randomise_mbuf_chain(struct mbuf
*m
)
107 for (i
= 0; i
< m
->m_len
; i
+= sizeof(int)) {
109 if (i
+ sizeof(int) < (size_t)m
->m_len
)
113 memcpy(m
->m_data
+ i
, &data
, len
);
116 randomise_mbuf_chain(m
->m_next
);
120 mbuf_len(struct mbuf
*m
)
122 return m
== NULL
? 0 : m
->m_len
+ mbuf_len(m
->m_next
);
125 int in_cksum_portable(struct mbuf
*, int);
126 int in_cksum(struct mbuf
*, int);
129 main(int argc
, char **argv
)
132 struct timeval tv
, old_tv
;
133 int loops
, old_sum
, off
, len
;
134 #ifdef HAVE_CPU_IN_CKSUM
148 while ((c
= getopt(argc
, argv
, "i:l:u:v")) != -1) {
151 iterations
= atoi(optarg
);
154 loops
= atoi(optarg
);
157 random_aligned
= atoi(optarg
);
163 errx(1, "%s [-l <loops>] [-u <unalign> [-i <iterations> "
164 "[<mbuf-size> ...]", getprogname());
168 for (; loops
; --loops
) {
169 if ((m
= allocate_mbuf_chain(argv
+ 4)) == NULL
)
171 randomise_mbuf_chain(m
);
175 /* force one loop over all data */
179 off
= len
? rand() % len
: 0;
182 old_sum
= portable_cpu_in_cksum(m
, len
, off
, init_sum
);
183 #ifdef HAVE_CPU_IN_CKSUM
184 new_sum
= cpu_in_cksum(m
, len
, off
, init_sum
);
185 if (old_sum
!= new_sum
)
186 errx(1, "comparison failed: %x %x", old_sum
, new_sum
);
194 getrusage(RUSAGE_SELF
, &res
);
196 for (i
= iterations
; i
; --i
)
197 (void)portable_cpu_in_cksum(m
, len
, off
, init_sum
);
198 getrusage(RUSAGE_SELF
, &res
);
199 timersub(&res
.ru_utime
, &tv
, &old_tv
);
201 printf("portable version: %jd.%06jd\n",
202 (intmax_t)old_tv
.tv_sec
, (intmax_t)old_tv
.tv_usec
);
204 #ifdef HAVE_CPU_IN_CKSUM
205 getrusage(RUSAGE_SELF
, &res
);
207 for (i
= iterations
; i
; --i
)
208 (void)cpu_in_cksum(m
, len
, off
, init_sum
);
209 getrusage(RUSAGE_SELF
, &res
);
210 timersub(&res
.ru_utime
, &tv
, &tv
);
212 printf("test version: %jd.%06jd\n",
213 (intmax_t)tv
.tv_sec
, (intmax_t)tv
.tv_usec
);
214 printf("relative time: %3.g%%\n",
215 100 * ((double)tv
.tv_sec
* 1e6
+ tv
.tv_usec
) /
216 ((double)old_tv
.tv_sec
* 1e6
+ old_tv
.tv_usec
+ 1));