Module Name:    src
Committed By:   christos
Date:           Mon Jan  5 22:38:36 UTC 2015

Modified Files:
        src/tests/net: Makefile
Added Files:
        src/tests/net/in_cksum: Makefile in_cksum.c t_in_cksum.sh

Log Message:
Port the in_cksum test from regress.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/tests/net/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/net/in_cksum/Makefile \
    src/tests/net/in_cksum/in_cksum.c src/tests/net/in_cksum/t_in_cksum.sh

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/net/Makefile
diff -u src/tests/net/Makefile:1.19 src/tests/net/Makefile:1.20
--- src/tests/net/Makefile:1.19	Sat Oct 11 19:04:42 2014
+++ src/tests/net/Makefile	Mon Jan  5 17:38:36 2015
@@ -1,10 +1,10 @@
-# $NetBSD: Makefile,v 1.19 2014/10/11 23:04:42 christos Exp $
+# $NetBSD: Makefile,v 1.20 2015/01/05 22:38:36 christos Exp $
 
 .include <bsd.own.mk>
 
 TESTSDIR=	${TESTSBASE}/net
 
-TESTS_SUBDIRS=		fdpass mcast net route sys
+TESTS_SUBDIRS=		fdpass in_cksum mcast net route sys
 .if (${MKRUMP} != "no")
 TESTS_SUBDIRS+=		bpf bpfilter carp icmp if if_bridge if_loop mpls npf
 .if (${MKSLJIT} != "no")

Added files:

Index: src/tests/net/in_cksum/Makefile
diff -u /dev/null src/tests/net/in_cksum/Makefile:1.1
--- /dev/null	Mon Jan  5 17:38:36 2015
+++ src/tests/net/in_cksum/Makefile	Mon Jan  5 17:38:36 2015
@@ -0,0 +1,25 @@
+#	$NetBSD: Makefile,v 1.1 2015/01/05 22:38:36 christos Exp $
+
+.include <bsd.own.mk>
+
+MKMAN=no
+TESTSDIR=       ${TESTSBASE}/net/cksum
+
+TESTS_SH=t_in_cksum
+
+ADIR=${NETBSDSRCDIR}/sys/arch/${MACHINE}/${MACHINE}
+CDIR=${NETBSDSRCDIR}/sys/netinet
+.if exists(${ADIR}/cpu_in_cksum.S)
+.PATH.S: ${ADIR}
+SRCS+=cpu_in_cksum.S
+CPPFLAGS+=-DHAVE_CPU_IN_CKSUM
+.endif
+
+PROGS=in_cksum
+
+CPPFLAGS+=-I${CDIR}
+
+.include <bsd.test.mk>
+
+foo:
+	echo ${ADIR}/cpu_in_cksum.S
Index: src/tests/net/in_cksum/in_cksum.c
diff -u /dev/null src/tests/net/in_cksum/in_cksum.c:1.1
--- /dev/null	Mon Jan  5 17:38:36 2015
+++ src/tests/net/in_cksum/in_cksum.c	Mon Jan  5 17:38:36 2015
@@ -0,0 +1,211 @@
+/*	$NetBSD: in_cksum.c,v 1.1 2015/01/05 22:38:36 christos Exp $	*/
+/*-
+ * Copyright (c) 2008 Joerg Sonnenberger <[email protected]>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: in_cksum.c,v 1.1 2015/01/05 22:38:36 christos Exp $");
+
+#include <sys/param.h>
+#include <sys/mbuf.h>
+#include <sys/resource.h>
+#include <err.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define cpu_in_cksum portable_cpu_in_cksum 
+#include "cpu_in_cksum.c"
+
+#ifdef HAVE_CPU_IN_CKSUM
+int	cpu_in_cksum(struct mbuf*, int, int, uint32_t);
+#endif
+
+static bool	random_aligned;
+
+static void
+free_mbuf_chain(struct mbuf *m)
+{
+	struct mbuf *next;
+
+	if (m == NULL)
+		return;
+
+	next = m->m_next;
+	free(m);
+	free_mbuf_chain(next);
+}
+
+static struct mbuf *
+allocate_mbuf_chain(char **lens)
+{
+	int len, off;
+	struct mbuf *m;
+
+	if (*lens == NULL)
+		return NULL;
+
+	len = atoi(*lens);
+	off = random_aligned ? rand() % 64 : 0;
+
+	m = malloc(sizeof(struct m_hdr) + len + off);
+	if (m == NULL)
+		err(EXIT_FAILURE, "malloc failed");
+
+	m->m_data = (char *)m + sizeof(struct m_hdr) + off;
+	m->m_len = len;
+
+	m->m_next = allocate_mbuf_chain(lens + 1);
+
+	return m;
+}
+
+static void
+randomise_mbuf_chain(struct mbuf *m)
+{
+	int i, data, len;
+
+	for (i = 0; i < m->m_len; i += sizeof(int)) {
+		data = rand();
+		if (i + sizeof(int) < (size_t)m->m_len)
+			len = sizeof(int);
+		else
+			len = m->m_len - i;
+		memcpy(m->m_data + i, &data, len);
+	}
+	if (m->m_next)
+		randomise_mbuf_chain(m->m_next);
+}
+
+static int
+mbuf_len(struct mbuf *m)
+{
+	return m == NULL ? 0 : m->m_len + mbuf_len(m->m_next);
+}
+
+int	in_cksum_portable(struct mbuf *, int);
+int	in_cksum(struct mbuf *, int);
+
+int
+main(int argc, char **argv)
+{
+	struct rusage res;
+	struct timeval tv, old_tv;
+	int loops, old_sum, off, len;
+#ifdef HAVE_CPU_IN_CKSUM
+	int new_sum;
+#endif
+	long i, iterations;
+	uint32_t init_sum;
+	struct mbuf *m;
+	bool verbose;
+	int c;
+
+	loops = 16;
+	verbose = false;
+	random_aligned = 0;
+	iterations = 100000;
+
+	while ((c = getopt(argc, argv, "i:l:u:v")) != -1) {
+		switch (c) {
+		case 'i':
+			iterations = atoi(optarg);
+			break;
+		case 'l':
+			loops = atoi(optarg);
+			break;
+		case 'u':
+			random_aligned = atoi(optarg);
+			break;
+		case 'v':
+			verbose = true;
+			break;
+		default:
+			errx(1, "%s [-l <loops>] [-u <unalign> [-i <iterations> "
+			    "[<mbuf-size> ...]", getprogname());
+		}
+	}
+
+	for (; loops; --loops) {
+		if ((m = allocate_mbuf_chain(argv + 4)) == NULL)
+			continue;
+		randomise_mbuf_chain(m);
+		init_sum = rand();
+		len = mbuf_len(m);
+
+		/* force one loop over all data */
+		if (loops == 1)
+			off = 0;
+		else
+			off = len ? rand() % len : 0;
+
+		len -= off;
+		old_sum = portable_cpu_in_cksum(m, len, off, init_sum);
+#ifdef HAVE_CPU_IN_CKSUM
+		new_sum = cpu_in_cksum(m, len, off, init_sum);
+		if (old_sum != new_sum)
+			errx(1, "comparison failed: %x %x", old_sum, new_sum);
+#else
+		__USE(old_sum);
+#endif
+
+		if (iterations == 0)
+			continue;
+
+		getrusage(RUSAGE_SELF, &res);
+		tv = res.ru_utime;
+		for (i = iterations; i; --i)
+			(void)portable_cpu_in_cksum(m, len, off, init_sum);
+		getrusage(RUSAGE_SELF, &res);
+		timersub(&res.ru_utime, &tv, &old_tv);
+		if (verbose)
+			printf("portable version: %jd.%06jd\n",
+			    (intmax_t)old_tv.tv_sec, (intmax_t)old_tv.tv_usec);
+
+#ifdef HAVE_CPU_IN_CKSUM
+		getrusage(RUSAGE_SELF, &res);
+		tv = res.ru_utime;
+		for (i = iterations; i; --i)
+			(void)cpu_in_cksum(m, len, off, init_sum);
+		getrusage(RUSAGE_SELF, &res);
+		timersub(&res.ru_utime, &tv, &tv);
+		if (verbose) {
+			printf("test version:     %jd.%06jd\n",
+			    (intmax_t)tv.tv_sec, (intmax_t)tv.tv_usec);
+			printf("relative time:    %3.g%%\n",
+			    100 * ((double)tv.tv_sec * 1e6 + tv.tv_usec) /
+			    ((double)old_tv.tv_sec * 1e6 + old_tv.tv_usec + 1));
+		}
+#endif
+		free_mbuf_chain(m);
+	}
+
+	return 0;
+}
Index: src/tests/net/in_cksum/t_in_cksum.sh
diff -u /dev/null src/tests/net/in_cksum/t_in_cksum.sh:1.1
--- /dev/null	Mon Jan  5 17:38:36 2015
+++ src/tests/net/in_cksum/t_in_cksum.sh	Mon Jan  5 17:38:36 2015
@@ -0,0 +1,68 @@
+#! /usr/bin/atf-sh
+#	$NetBSD: t_in_cksum.sh,v 1.1 2015/01/05 22:38:36 christos Exp $
+#
+
+TIMING_LOOPS=10000
+
+mbufs() {
+    ./in_cksum -l 16 -u $0 -i ${TIMING_LOOPS} \
+	1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
+    ./in_cksum -l 16 -u $0 -i ${TIMING_LOOPS} \
+	1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 \
+	1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
+    ./in_cksum -l 64 -u $0 -i ${TIMING_LOOPS} \
+	1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 \
+	1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
+    ./in_cksum -l 16 -u $0 -i ${TIMING_LOOPS}	\
+	1 3 1 3 1 3 1
+}
+
+sizes() {
+    ./in_cksum -l 16 -u $1 -i ${TIMING_LOOPS}	2048
+    ./in_cksum -l 16 -u $1 -i ${TIMING_LOOPS}	40
+    ./in_cksum -l 16 -u $1 -i ${TIMING_LOOPS}	1536
+    ./in_cksum -l 16 -u $1 -i ${TIMING_LOOPS}	576
+    ./in_cksum -l 16 -u $1 -i ${TIMING_LOOPS}	1536 1536 1536 1536 1536 640
+}
+
+atf_test_case mbufs_aligned
+
+mbufs_aligned_head() {
+	atf_set "descr" "Test in_cksum mbuf chains aligned"
+}
+
+mbufs_aligned_body() {
+	mbufs 0
+}
+
+mbufs_unaligned_head() {
+	atf_set "descr" "Test in_cksum mbuf chains unaligned"
+}
+
+mbufs_unaligned_body() {
+	mbufs 1
+}
+
+sizes_aligned_head() {
+	atf_set "descr" "Test in_cksum sizes aligned"
+}
+
+sizes_aligned_body() {
+	sizes 0
+}
+
+sizes_unaligned_head() {
+	atf_set "descr" "Test in_cksum sizes unaligned"
+}
+
+sizes_unaligned_body() {
+	sizes 1
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case mbufs_aligned
+	atf_add_test_case mbufs_unaligned
+	atf_add_test_case sizes_aligned
+	atf_add_test_case sizes_unaligned
+}

Reply via email to