Source-Changes-HG archive

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

[src/trunk]: src Adjust NPF to the recent BPF / BPF JIT changes and make it w...



details:   https://anonhg.NetBSD.org/src/rev/085c99b298e5
branches:  trunk
changeset: 796911:085c99b298e5
user:      rmind <rmind%NetBSD.org@localhost>
date:      Wed Jun 25 00:20:06 2014 +0000

description:
Adjust NPF to the recent BPF / BPF JIT changes and make it work again.
All regression tests are happy now (hi alnsn!).

diffstat:

 sys/net/npf/npf.h                              |   4 +++-
 sys/net/npf/npf_bpf.c                          |  24 +++++++++++++++++++++---
 sys/net/npf/npf_impl.h                         |   3 ++-
 sys/net/npf/npf_ruleset.c                      |  18 +++++++++---------
 usr.sbin/npf/npftest/libnpftest/npf_bpf_test.c |   8 +++++---
 5 files changed, 40 insertions(+), 17 deletions(-)

diffs (169 lines):

diff -r 91227063a9bb -r 085c99b298e5 sys/net/npf/npf.h
--- a/sys/net/npf/npf.h Tue Jun 24 23:25:33 2014 +0000
+++ b/sys/net/npf/npf.h Wed Jun 25 00:20:06 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf.h,v 1.40 2014/05/30 23:26:06 rmind Exp $   */
+/*     $NetBSD: npf.h,v 1.41 2014/06/25 00:20:06 rmind Exp $   */
 
 /*-
  * Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
@@ -67,6 +67,8 @@
 #define        BPF_MW_L4OFF            1
 #define        BPF_MW_L4PROTO          2
 #endif
+/* The number of words used. */
+#define        NPF_BPF_NWORDS          3
 
 #if defined(_KERNEL)
 
diff -r 91227063a9bb -r 085c99b298e5 sys/net/npf/npf_bpf.c
--- a/sys/net/npf/npf_bpf.c     Tue Jun 24 23:25:33 2014 +0000
+++ b/sys/net/npf/npf_bpf.c     Wed Jun 25 00:20:06 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_bpf.c,v 1.7 2014/06/24 11:31:49 alnsn Exp $        */
+/*     $NetBSD: npf_bpf.c,v 1.8 2014/06/25 00:20:06 rmind Exp $        */
 
 /*-
  * Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
@@ -34,11 +34,12 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_bpf.c,v 1.7 2014/06/24 11:31:49 alnsn Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_bpf.c,v 1.8 2014/06/25 00:20:06 rmind Exp $");
 
 #include <sys/types.h>
 #include <sys/param.h>
 
+#include <sys/bitops.h>
 #include <sys/mbuf.h>
 #include <net/bpf.h>
 
@@ -59,12 +60,15 @@
        [NPF_COP_TABLE] = npf_cop_table,
 };
 
+#define        BPF_MW_ALLMASK \
+    ((1U << BPF_MW_IPVER) | (1U << BPF_MW_L4OFF) | (1U << BPF_MW_L4PROTO))
+
 void
 npf_bpf_sysinit(void)
 {
        npf_bpfctx = bpf_create();
-       KASSERT(npf_bpfctx != NULL);
        bpf_set_cop(npf_bpfctx, npf_bpfcop, __arraycount(npf_bpfcop));
+       bpf_set_extmem(npf_bpfctx, NPF_BPF_NWORDS, BPF_MW_ALLMASK);
 }
 
 void
@@ -73,6 +77,20 @@
        bpf_destroy(npf_bpfctx);
 }
 
+void
+npf_bpf_prepare(npf_cache_t *npc, nbuf_t *nbuf, bpf_args_t *args, uint32_t *m)
+{
+       const struct mbuf *mbuf = nbuf_head_mbuf(nbuf);
+       const size_t pktlen = m_length(mbuf);
+
+       /* Prepare the arguments for the BPF programs. */
+       args->pkt = (const uint8_t *)mbuf;
+       args->wirelen = pktlen;
+       args->buflen = 0;
+       args->mem = m;
+       args->arg = npc;
+}
+
 int
 npf_bpf_filter(bpf_args_t *args, const void *code, bpfjit_func_t jcode)
 {
diff -r 91227063a9bb -r 085c99b298e5 sys/net/npf/npf_impl.h
--- a/sys/net/npf/npf_impl.h    Tue Jun 24 23:25:33 2014 +0000
+++ b/sys/net/npf/npf_impl.h    Wed Jun 25 00:20:06 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_impl.h,v 1.52 2014/05/30 23:26:06 rmind Exp $      */
+/*     $NetBSD: npf_impl.h,v 1.53 2014/06/25 00:20:06 rmind Exp $      */
 
 /*-
  * Copyright (c) 2009-2014 The NetBSD Foundation, Inc.
@@ -223,6 +223,7 @@
 /* BPF interface. */
 void           npf_bpf_sysinit(void);
 void           npf_bpf_sysfini(void);
+void           npf_bpf_prepare(npf_cache_t *, nbuf_t *, bpf_args_t *, uint32_t *);
 int            npf_bpf_filter(bpf_args_t *, const void *, bpfjit_func_t);
 void *         npf_bpf_compile(void *, size_t);
 bool           npf_bpf_validate(const void *, size_t);
diff -r 91227063a9bb -r 085c99b298e5 sys/net/npf/npf_ruleset.c
--- a/sys/net/npf/npf_ruleset.c Tue Jun 24 23:25:33 2014 +0000
+++ b/sys/net/npf/npf_ruleset.c Wed Jun 25 00:20:06 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_ruleset.c,v 1.32 2014/06/24 10:53:30 alnsn Exp $   */
+/*     $NetBSD: npf_ruleset.c,v 1.33 2014/06/25 00:20:06 rmind Exp $   */
 
 /*-
  * Copyright (c) 2009-2013 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.32 2014/06/24 10:53:30 alnsn Exp $");
+__KERNEL_RCSID(0, "$NetBSD: npf_ruleset.c,v 1.33 2014/06/25 00:20:06 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -741,17 +741,17 @@
        const u_int nitems = rlset->rs_nitems;
        const u_int ifid = nbuf->nb_ifid;
        npf_rule_t *final_rl = NULL;
-       const struct mbuf *m;
        bpf_args_t bc_args;
        u_int n = 0;
 
-       memset(&bc_args, 0, sizeof(bpf_args_t));
-       m = nbuf_head_mbuf(nbuf);
-       bc_args.pkt = (const uint8_t *)m;
-       bc_args.wirelen = m_length(m);
-       bc_args.arg = npc;
+       KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
 
-       KASSERT(((di & PFIL_IN) != 0) ^ ((di & PFIL_OUT) != 0));
+       /*
+        * Prepare the external memory store and the arguments for
+        * the BPF programs to be executed.
+        */
+       uint32_t bc_words[NPF_BPF_NWORDS];
+       npf_bpf_prepare(npc, nbuf, &bc_args, bc_words);
 
        while (n < nitems) {
                npf_rule_t *rl = rlset->rs_rules[n];
diff -r 91227063a9bb -r 085c99b298e5 usr.sbin/npf/npftest/libnpftest/npf_bpf_test.c
--- a/usr.sbin/npf/npftest/libnpftest/npf_bpf_test.c    Tue Jun 24 23:25:33 2014 +0000
+++ b/usr.sbin/npf/npftest/libnpftest/npf_bpf_test.c    Wed Jun 25 00:20:06 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_bpf_test.c,v 1.5 2014/06/24 10:53:30 alnsn Exp $   */
+/*     $NetBSD: npf_bpf_test.c,v 1.6 2014/06/25 00:20:06 rmind Exp $   */
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -63,6 +63,7 @@
 {
        ifnet_t *dummy_ifp = npf_test_addif(IFNAME_TEST, false, false);
        npf_cache_t npc = { .npc_info = 0 };
+       uint32_t memstore[BPF_MEMWORDS];
        bpf_args_t bc_args;
        struct mbuf *m;
        nbuf_t nbuf;
@@ -74,9 +75,10 @@
        nbuf_init(&nbuf, m, dummy_ifp);
        npf_cache_all(&npc, &nbuf);
 
-       memset(&bc_args, 0, sizeof(bpf_args_t));
        bc_args.pkt = (const uint8_t *)m;
-       bc_args.wirelen = m_length(m);
+       bc_args.buflen = m_length(m);
+       bc_args.wirelen = bc_args.buflen;
+       bc_args.mem = memstore;
        bc_args.arg = &npc;
 
        ret = npf_bpf_filter(&bc_args, code, NULL);



Home | Main Index | Thread Index | Old Index