Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/net Add bpfjit kernel tests for loading from mbuf chain.



details:   https://anonhg.NetBSD.org/src/rev/0c2d139f9eb5
branches:  trunk
changeset: 330239:0c2d139f9eb5
user:      alnsn <alnsn%NetBSD.org@localhost>
date:      Mon Jun 30 21:30:51 2014 +0000

description:
Add bpfjit kernel tests for loading from mbuf chain.

diffstat:

 tests/net/Makefile          |    5 +-
 tests/net/bpfjit/Makefile   |   14 +
 tests/net/bpfjit/t_bpfjit.c |  664 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 682 insertions(+), 1 deletions(-)

diffs (truncated from 705 to 300 lines):

diff -r 18e9cc69f8f3 -r 0c2d139f9eb5 tests/net/Makefile
--- a/tests/net/Makefile        Mon Jun 30 21:17:18 2014 +0000
+++ b/tests/net/Makefile        Mon Jun 30 21:30:51 2014 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.16 2014/03/18 18:20:44 riastradh Exp $
+# $NetBSD: Makefile,v 1.17 2014/06/30 21:30:51 alnsn Exp $
 
 .include <bsd.own.mk>
 
@@ -7,6 +7,9 @@
 TESTS_SUBDIRS=         fdpass net route sys
 .if (${MKRUMP} != "no")
 TESTS_SUBDIRS+=                bpf bpfilter carp icmp if if_loop mpls npf
+.if (${MKSLJIT} != "no")
+TESTS_SUBDIRS+=                bpfjit
+.endif
 .endif
 
 .include <bsd.test.mk>
diff -r 18e9cc69f8f3 -r 0c2d139f9eb5 tests/net/bpfjit/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/bpfjit/Makefile Mon Jun 30 21:30:51 2014 +0000
@@ -0,0 +1,14 @@
+# $NetBSD: Makefile,v 1.1 2014/06/30 21:30:51 alnsn Exp $
+#
+
+.include <bsd.own.mk>
+
+TESTSDIR=      ${TESTSBASE}/kernel/bpfjit
+
+TESTS_C=       t_bpfjit
+
+LDADD+=                -lrumpnet_bpfjit -lrumpkern_sljit
+LDADD+=                -lrumpdev_bpf -lrumpnet_net -lrumpnet
+LDADD+=                -lrump -lrumpuser -lpthread
+
+.include <bsd.test.mk>
diff -r 18e9cc69f8f3 -r 0c2d139f9eb5 tests/net/bpfjit/t_bpfjit.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/bpfjit/t_bpfjit.c       Mon Jun 30 21:30:51 2014 +0000
@@ -0,0 +1,664 @@
+/*     $NetBSD: t_bpfjit.c,v 1.1 2014/06/30 21:30:51 alnsn Exp $       */
+
+/*-
+ * Copyright (c) 2014 Alexander Nasonov.
+ * 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 AUTHOR ``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 AUTHOR 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>
+__RCSID("$NetBSD: t_bpfjit.c,v 1.1 2014/06/30 21:30:51 alnsn Exp $");
+
+#include <sys/param.h>
+#include <sys/mbuf.h>
+#include <unistd.h>
+
+#include <net/bpf.h>
+#include <net/bpfjit.h>
+
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+/* XXX: atf-c.h has collisions with mbuf */
+#undef m_type
+#undef m_data
+#include <atf-c.h>
+
+#include "../../h_macros.h"
+
+/* XXX These declarations don't look kosher. */
+bpfjit_func_t rumpns_bpfjit_generate_code(const bpf_ctx_t *,
+    const struct bpf_insn *, size_t);
+void rumpns_bpfjit_free_code(bpfjit_func_t);
+
+static bool
+test_ldb_abs(size_t split)
+{
+       static char P[] = { 1, 2, 3, 4, 5 };
+
+       /* Return a product of all packet bytes. */
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), /* X <- 1     */
+
+               BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 0),  /* A <- P[0]  */
+               BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */
+               BPF_STMT(BPF_MISC+BPF_TAX, 0),      /* X <- A     */
+
+               BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 1),  /* A <- P[1]  */
+               BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */
+               BPF_STMT(BPF_MISC+BPF_TAX, 0),      /* X <- A     */
+
+               BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 2),  /* A <- P[2]  */
+               BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */
+               BPF_STMT(BPF_MISC+BPF_TAX, 0),      /* X <- A     */
+
+               BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3),  /* A <- P[3]  */
+               BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */
+               BPF_STMT(BPF_MISC+BPF_TAX, 0),      /* X <- A     */
+
+               BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 4),  /* A <- P[4]  */
+               BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0), /* A <- A * X */
+               BPF_STMT(BPF_RET+BPF_A, 0),         /* ret A      */
+       };
+
+       const size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       bpfjit_func_t fn;
+       bpf_args_t args;
+       struct mbuf mb1, mb2;
+       unsigned int res;
+
+       (void)memset(&mb1, 0, sizeof(mb1));
+       mb1.m_hdr.mh_data = P;
+       mb1.m_next = (split < sizeof(P)) ? &mb2 : NULL;
+       mb1.m_len = (split < sizeof(P)) ? split : sizeof(P);
+
+       if (split < sizeof(P)) {
+               (void)memset(&mb2, 0, sizeof(mb2));
+               mb2.m_next = NULL;
+               mb2.m_hdr.mh_data = &P[split];
+               mb2.m_len = sizeof(P) - split;
+       }
+
+       args.pkt = (const uint8_t*)&mb1;
+       args.buflen = 0;
+       args.wirelen = sizeof(P);
+
+       rump_schedule();
+       fn = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
+       rump_unschedule();
+
+       ATF_REQUIRE(fn != NULL);
+
+       res = fn(NULL, &args);
+
+       rump_schedule();
+       rumpns_bpfjit_free_code(fn);
+       rump_unschedule();
+
+       return res == 120;
+}
+
+static bool
+test_ldh_abs(size_t split)
+{
+       static char P[] = { 1, 2, 3, 4, 5 };
+
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 0),  /* A <- P[0:2]  */
+               BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X   */
+               BPF_STMT(BPF_MISC+BPF_TAX, 0),      /* X <- A       */
+
+               BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 1),  /* A <- P[1:2]  */
+               BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X   */
+               BPF_STMT(BPF_MISC+BPF_TAX, 0),      /* X <- A       */
+
+               BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 2),  /* A <- P[2:2]  */
+               BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X   */
+               BPF_STMT(BPF_MISC+BPF_TAX, 0),      /* X <- A       */
+
+               BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 3),  /* A <- P[3:2]  */
+               BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X   */
+               BPF_STMT(BPF_RET+BPF_A, 0),         /* ret A        */
+       };
+
+       const size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       bpfjit_func_t fn;
+       bpf_args_t args;
+       struct mbuf mb1, mb2;
+       unsigned int res;
+
+       (void)memset(&mb1, 0, sizeof(mb1));
+       mb1.m_hdr.mh_data = P;
+       mb1.m_next = (split < sizeof(P)) ? &mb2 : NULL;
+       mb1.m_len = (split < sizeof(P)) ? split : sizeof(P);
+
+       if (split < sizeof(P)) {
+               (void)memset(&mb2, 0, sizeof(mb2));
+               mb2.m_next = NULL;
+               mb2.m_hdr.mh_data = &P[split];
+               mb2.m_len = sizeof(P) - split;
+       }
+
+       args.pkt = (const uint8_t*)&mb1;
+       args.buflen = 0;
+       args.wirelen = sizeof(P);
+
+       rump_schedule();
+       fn = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
+       rump_unschedule();
+
+       ATF_REQUIRE(fn != NULL);
+
+       res = fn(NULL, &args);
+
+       rump_schedule();
+       rumpns_bpfjit_free_code(fn);
+       rump_unschedule();
+
+       return res == 0x0a0e; /* 10 14 */
+}
+
+static bool
+test_ldw_abs(size_t split)
+{
+       static char P[] = { 1, 2, 3, 4, 5 };
+
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 0),  /* A <- P[0:4] */
+               BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X  */
+               BPF_STMT(BPF_MISC+BPF_TAX, 0),      /* X <- A       */
+
+               BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 1),  /* A <- P[1:4] */
+               BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X  */
+               BPF_STMT(BPF_RET+BPF_A, 0),         /* ret A       */
+       };
+
+       const size_t insn_count = sizeof(insns) / sizeof(insns[0]);
+
+       bpfjit_func_t fn;
+       bpf_args_t args;
+       struct mbuf mb1, mb2;
+       unsigned int res;
+
+       (void)memset(&mb1, 0, sizeof(mb1));
+       mb1.m_hdr.mh_data = P;
+       mb1.m_next = (split < sizeof(P)) ? &mb2 : NULL;
+       mb1.m_len = (split < sizeof(P)) ? split : sizeof(P);
+
+       if (split < sizeof(P)) {
+               (void)memset(&mb2, 0, sizeof(mb2));
+               mb2.m_next = NULL;
+               mb2.m_hdr.mh_data = &P[split];
+               mb2.m_len = sizeof(P) - split;
+       }
+
+       args.pkt = (const uint8_t*)&mb1;
+       args.buflen = 0;
+       args.wirelen = sizeof(P);
+
+       rump_schedule();
+       fn = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
+       rump_unschedule();
+
+       ATF_REQUIRE(fn != NULL);
+
+       res = fn(NULL, &args);
+       printf("0x%08x\n", res);
+
+       rump_schedule();
+       rumpns_bpfjit_free_code(fn);
+       rump_unschedule();
+
+       return res == 0x03050709;
+}
+
+static bool
+test_ldb_ind(size_t split)
+{
+       static char P[] = { 1, 2, 3, 4, 5 };
+
+       /* Return a sum of all packet bytes. */
+       static struct bpf_insn insns[] = {
+               BPF_STMT(BPF_LD+BPF_B+BPF_IND, 0),  /* A <- P[0+X] */
+               BPF_STMT(BPF_ST, 0),                /* M[0] <- A   */
+
+               BPF_STMT(BPF_LD+BPF_B+BPF_IND, 1),  /* A <- P[1+X] */
+               BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0]   */
+               BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X  */
+               BPF_STMT(BPF_ST, 0),                /* M[0] <- A   */
+
+               BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1), /* X <- 1      */
+               BPF_STMT(BPF_LD+BPF_B+BPF_IND, 1),  /* A <- P[1+X] */
+               BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0), /* X <- M[0]   */
+               BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X  */
+               BPF_STMT(BPF_ST, 0),                /* M[0] <- A   */



Home | Main Index | Thread Index | Old Index