Source-Changes-HG archive

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

[src/trunk]: src/tests/net/bpf Add some helper functions for bpf/bpfjit rump ...



details:   https://anonhg.NetBSD.org/src/rev/bab0f97e579d
branches:  trunk
changeset: 330405:bab0f97e579d
user:      alnsn <alnsn%NetBSD.org@localhost>
date:      Mon Jul 07 19:34:32 2014 +0000

description:
Add some helper functions for bpf/bpfjit rump tests.

diffstat:

 tests/net/bpf/h_bpf.h |  143 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 143 insertions(+), 0 deletions(-)

diffs (147 lines):

diff -r a10055d182ed -r bab0f97e579d tests/net/bpf/h_bpf.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/net/bpf/h_bpf.h     Mon Jul 07 19:34:32 2014 +0000
@@ -0,0 +1,143 @@
+/*     $NetBSD: h_bpf.h,v 1.1 2014/07/07 19:34:32 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.
+ */
+
+#ifndef _TESTS_NET_BPF_H_BPF_H_
+#define _TESTS_NET_BPF_H_BPF_H_
+
+#include <sys/param.h>
+#include <sys/mbuf.h>
+
+#include <net/bpf.h>
+#include <net/bpfjit.h>
+
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+#include <stdint.h>
+#include <string.h>
+
+/* XXX These declarations don't look kosher. */
+int rumpns_bpf_validate(const struct bpf_insn *, int);
+unsigned int rumpns_bpf_filter_ext(const bpf_ctx_t *,
+    const struct bpf_insn *, bpf_args_t *);
+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);
+
+/*
+ * Init mbuf chain with one or two chunks. The first chunk holds
+ * [pkt, pkt + split] bytes, the second chunk (if it's not empty)
+ * holds (pkt + split, pkt + pktsize) bytes.
+ * The function returns (const uint8_t *)mb1.
+ */
+static inline const uint8_t *
+init_mchain2(struct mbuf *mb1, struct mbuf *mb2,
+    char pkt[], size_t pktsize, size_t split)
+{
+
+       (void)memset(mb1, 0, sizeof(*mb1));
+       mb1->m_data = pkt;
+       mb1->m_next = (split < pktsize) ? mb2 : NULL;
+       mb1->m_len = (split < pktsize) ? split : pktsize;
+
+       if (split < pktsize) {
+               (void)memset(mb2, 0, sizeof(*mb2));
+               mb2->m_next = NULL;
+               mb2->m_data = &pkt[split];
+               mb2->m_len = pktsize - split;
+       }
+
+       return (const uint8_t*)mb1;
+}
+
+/*
+ * Interpret a filter program with mbuf chain passed to bpf_filter_ext().
+ */
+static inline unsigned int
+interp_prog_mchain2(struct bpf_insn *insns,
+    char *pkt, size_t pktsize, size_t split)
+{
+       uint32_t mem[BPF_MEMWORDS];
+       struct mbuf mb1, mb2;
+       bpf_args_t args;
+       unsigned int res;
+
+       args.pkt = init_mchain2(&mb1, &mb2, pkt, pktsize, split);
+       args.buflen = 0;
+       args.wirelen = pktsize;
+       args.mem = mem;
+
+       rump_schedule();
+       res = rumpns_bpf_filter_ext(NULL, insns, &args);
+       rump_unschedule();
+
+       return res;
+}
+
+/*
+ * Compile and run a filter program with mbuf chain passed to compiled function.
+ */
+static inline unsigned int
+exec_prog_mchain2(struct bpf_insn *insns, size_t insn_count,
+    char *pkt, size_t pktsize, size_t split)
+{
+       bpfjit_func_t fn;
+       struct mbuf mb1, mb2;
+       bpf_args_t args;
+       unsigned int res;
+
+       args.pkt = init_mchain2(&mb1, &mb2, pkt, pktsize, split);
+       args.buflen = 0;
+       args.wirelen = pktsize;
+
+       rump_schedule();
+       fn = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
+       rump_unschedule();
+
+       res = fn(NULL, &args);
+
+       rump_schedule();
+       rumpns_bpfjit_free_code(fn);
+       rump_unschedule();
+
+       return res;
+}
+
+static inline bool
+prog_validate(struct bpf_insn *insns, size_t insn_count)
+{
+       bool res;
+
+       rump_schedule();
+       res = rumpns_bpf_validate(insns, insn_count);
+       rump_unschedule();
+
+       return res;
+}
+
+#endif /* _TESTS_NET_BPF_H_BPF_H_ */



Home | Main Index | Thread Index | Old Index