Home | History | Annotate | Line # | Download | only in in_cksum
in_cksum.c revision 1.1
      1  1.1  christos /*	$NetBSD: in_cksum.c,v 1.1 2015/01/05 22:38:36 christos 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.1  christos __KERNEL_RCSID(0, "$NetBSD: in_cksum.c,v 1.1 2015/01/05 22:38:36 christos 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.1  christos #include <unistd.h>
     41  1.1  christos #include <stdlib.h>
     42  1.1  christos #include <string.h>
     43  1.1  christos 
     44  1.1  christos #define cpu_in_cksum portable_cpu_in_cksum
     45  1.1  christos #include "cpu_in_cksum.c"
     46  1.1  christos 
     47  1.1  christos #ifdef HAVE_CPU_IN_CKSUM
     48  1.1  christos int	cpu_in_cksum(struct mbuf*, int, int, uint32_t);
     49  1.1  christos #endif
     50  1.1  christos 
     51  1.1  christos static bool	random_aligned;
     52  1.1  christos 
     53  1.1  christos static void
     54  1.1  christos free_mbuf_chain(struct mbuf *m)
     55  1.1  christos {
     56  1.1  christos 	struct mbuf *next;
     57  1.1  christos 
     58  1.1  christos 	if (m == NULL)
     59  1.1  christos 		return;
     60  1.1  christos 
     61  1.1  christos 	next = m->m_next;
     62  1.1  christos 	free(m);
     63  1.1  christos 	free_mbuf_chain(next);
     64  1.1  christos }
     65  1.1  christos 
     66  1.1  christos static struct mbuf *
     67  1.1  christos allocate_mbuf_chain(char **lens)
     68  1.1  christos {
     69  1.1  christos 	int len, off;
     70  1.1  christos 	struct mbuf *m;
     71  1.1  christos 
     72  1.1  christos 	if (*lens == NULL)
     73  1.1  christos 		return NULL;
     74  1.1  christos 
     75  1.1  christos 	len = atoi(*lens);
     76  1.1  christos 	off = random_aligned ? rand() % 64 : 0;
     77  1.1  christos 
     78  1.1  christos 	m = malloc(sizeof(struct m_hdr) + len + off);
     79  1.1  christos 	if (m == NULL)
     80  1.1  christos 		err(EXIT_FAILURE, "malloc failed");
     81  1.1  christos 
     82  1.1  christos 	m->m_data = (char *)m + sizeof(struct m_hdr) + off;
     83  1.1  christos 	m->m_len = len;
     84  1.1  christos 
     85  1.1  christos 	m->m_next = allocate_mbuf_chain(lens + 1);
     86  1.1  christos 
     87  1.1  christos 	return m;
     88  1.1  christos }
     89  1.1  christos 
     90  1.1  christos static void
     91  1.1  christos randomise_mbuf_chain(struct mbuf *m)
     92  1.1  christos {
     93  1.1  christos 	int i, data, len;
     94  1.1  christos 
     95  1.1  christos 	for (i = 0; i < m->m_len; i += sizeof(int)) {
     96  1.1  christos 		data = rand();
     97  1.1  christos 		if (i + sizeof(int) < (size_t)m->m_len)
     98  1.1  christos 			len = sizeof(int);
     99  1.1  christos 		else
    100  1.1  christos 			len = m->m_len - i;
    101  1.1  christos 		memcpy(m->m_data + i, &data, len);
    102  1.1  christos 	}
    103  1.1  christos 	if (m->m_next)
    104  1.1  christos 		randomise_mbuf_chain(m->m_next);
    105  1.1  christos }
    106  1.1  christos 
    107  1.1  christos static int
    108  1.1  christos mbuf_len(struct mbuf *m)
    109  1.1  christos {
    110  1.1  christos 	return m == NULL ? 0 : m->m_len + mbuf_len(m->m_next);
    111  1.1  christos }
    112  1.1  christos 
    113  1.1  christos int	in_cksum_portable(struct mbuf *, int);
    114  1.1  christos int	in_cksum(struct mbuf *, int);
    115  1.1  christos 
    116  1.1  christos int
    117  1.1  christos main(int argc, char **argv)
    118  1.1  christos {
    119  1.1  christos 	struct rusage res;
    120  1.1  christos 	struct timeval tv, old_tv;
    121  1.1  christos 	int loops, old_sum, off, len;
    122  1.1  christos #ifdef HAVE_CPU_IN_CKSUM
    123  1.1  christos 	int new_sum;
    124  1.1  christos #endif
    125  1.1  christos 	long i, iterations;
    126  1.1  christos 	uint32_t init_sum;
    127  1.1  christos 	struct mbuf *m;
    128  1.1  christos 	bool verbose;
    129  1.1  christos 	int c;
    130  1.1  christos 
    131  1.1  christos 	loops = 16;
    132  1.1  christos 	verbose = false;
    133  1.1  christos 	random_aligned = 0;
    134  1.1  christos 	iterations = 100000;
    135  1.1  christos 
    136  1.1  christos 	while ((c = getopt(argc, argv, "i:l:u:v")) != -1) {
    137  1.1  christos 		switch (c) {
    138  1.1  christos 		case 'i':
    139  1.1  christos 			iterations = atoi(optarg);
    140  1.1  christos 			break;
    141  1.1  christos 		case 'l':
    142  1.1  christos 			loops = atoi(optarg);
    143  1.1  christos 			break;
    144  1.1  christos 		case 'u':
    145  1.1  christos 			random_aligned = atoi(optarg);
    146  1.1  christos 			break;
    147  1.1  christos 		case 'v':
    148  1.1  christos 			verbose = true;
    149  1.1  christos 			break;
    150  1.1  christos 		default:
    151  1.1  christos 			errx(1, "%s [-l <loops>] [-u <unalign> [-i <iterations> "
    152  1.1  christos 			    "[<mbuf-size> ...]", getprogname());
    153  1.1  christos 		}
    154  1.1  christos 	}
    155  1.1  christos 
    156  1.1  christos 	for (; loops; --loops) {
    157  1.1  christos 		if ((m = allocate_mbuf_chain(argv + 4)) == NULL)
    158  1.1  christos 			continue;
    159  1.1  christos 		randomise_mbuf_chain(m);
    160  1.1  christos 		init_sum = rand();
    161  1.1  christos 		len = mbuf_len(m);
    162  1.1  christos 
    163  1.1  christos 		/* force one loop over all data */
    164  1.1  christos 		if (loops == 1)
    165  1.1  christos 			off = 0;
    166  1.1  christos 		else
    167  1.1  christos 			off = len ? rand() % len : 0;
    168  1.1  christos 
    169  1.1  christos 		len -= off;
    170  1.1  christos 		old_sum = portable_cpu_in_cksum(m, len, off, init_sum);
    171  1.1  christos #ifdef HAVE_CPU_IN_CKSUM
    172  1.1  christos 		new_sum = cpu_in_cksum(m, len, off, init_sum);
    173  1.1  christos 		if (old_sum != new_sum)
    174  1.1  christos 			errx(1, "comparison failed: %x %x", old_sum, new_sum);
    175  1.1  christos #else
    176  1.1  christos 		__USE(old_sum);
    177  1.1  christos #endif
    178  1.1  christos 
    179  1.1  christos 		if (iterations == 0)
    180  1.1  christos 			continue;
    181  1.1  christos 
    182  1.1  christos 		getrusage(RUSAGE_SELF, &res);
    183  1.1  christos 		tv = res.ru_utime;
    184  1.1  christos 		for (i = iterations; i; --i)
    185  1.1  christos 			(void)portable_cpu_in_cksum(m, len, off, init_sum);
    186  1.1  christos 		getrusage(RUSAGE_SELF, &res);
    187  1.1  christos 		timersub(&res.ru_utime, &tv, &old_tv);
    188  1.1  christos 		if (verbose)
    189  1.1  christos 			printf("portable version: %jd.%06jd\n",
    190  1.1  christos 			    (intmax_t)old_tv.tv_sec, (intmax_t)old_tv.tv_usec);
    191  1.1  christos 
    192  1.1  christos #ifdef HAVE_CPU_IN_CKSUM
    193  1.1  christos 		getrusage(RUSAGE_SELF, &res);
    194  1.1  christos 		tv = res.ru_utime;
    195  1.1  christos 		for (i = iterations; i; --i)
    196  1.1  christos 			(void)cpu_in_cksum(m, len, off, init_sum);
    197  1.1  christos 		getrusage(RUSAGE_SELF, &res);
    198  1.1  christos 		timersub(&res.ru_utime, &tv, &tv);
    199  1.1  christos 		if (verbose) {
    200  1.1  christos 			printf("test version:     %jd.%06jd\n",
    201  1.1  christos 			    (intmax_t)tv.tv_sec, (intmax_t)tv.tv_usec);
    202  1.1  christos 			printf("relative time:    %3.g%%\n",
    203  1.1  christos 			    100 * ((double)tv.tv_sec * 1e6 + tv.tv_usec) /
    204  1.1  christos 			    ((double)old_tv.tv_sec * 1e6 + old_tv.tv_usec + 1));
    205  1.1  christos 		}
    206  1.1  christos #endif
    207  1.1  christos 		free_mbuf_chain(m);
    208  1.1  christos 	}
    209  1.1  christos 
    210  1.1  christos 	return 0;
    211  1.1  christos }
    212