Source-Changes-HG archive

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

[src/trunk]: src - Add bpf_args_t and convert bpf_filter_ext() to use it. Th...



details:   https://anonhg.NetBSD.org/src/rev/230807f405e6
branches:  trunk
changeset: 791381:230807f405e6
user:      rmind <rmind%NetBSD.org@localhost>
date:      Fri Nov 15 00:12:44 2013 +0000

description:
- Add bpf_args_t and convert bpf_filter_ext() to use it.  This allows the
  caller to initialise (and re-use) the memory store.
- Add bpf_jit_generate() and bpf_jit_freecode() wrappers.

diffstat:

 sys/net/bpf.c                  |   37 +++-
 sys/net/bpf.h                  |   49 ++++-
 sys/net/bpf_filter.c           |  110 +++++++-----
 sys/net/bpfdesc.h              |    4 +-
 sys/net/bpfjit.c               |   15 +-
 sys/net/bpfjit.h               |   15 +-
 sys/net/npf/npf_bpf.c          |   37 ++--
 sys/net/npf/npf_impl.h         |    4 +-
 sys/net/npf/npf_ruleset.c      |    6 +-
 tests/lib/libbpfjit/t_bpfjit.c |  331 ++++++++++++++++++++--------------------
 10 files changed, 334 insertions(+), 274 deletions(-)

diffs (truncated from 2076 to 300 lines):

diff -r 9594d71943de -r 230807f405e6 sys/net/bpf.c
--- a/sys/net/bpf.c     Thu Nov 14 18:54:40 2013 +0000
+++ b/sys/net/bpf.c     Fri Nov 15 00:12:44 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bpf.c,v 1.177 2013/09/18 23:34:55 rmind Exp $  */
+/*     $NetBSD: bpf.c,v 1.178 2013/11/15 00:12:44 rmind Exp $  */
 
 /*
  * Copyright (c) 1990, 1991, 1993
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.177 2013/09/18 23:34:55 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.178 2013/11/15 00:12:44 rmind Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_bpf.h"
@@ -185,6 +185,23 @@
        nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
 };
 
+bpfjit_func_t
+bpf_jit_generate(bpf_ctx_t *bc, void *code, size_t size)
+{
+       membar_consumer();
+       if (bpfjit_module_ops.bj_generate_code != NULL) {
+               return bpfjit_module_ops.bj_generate_code(bc, code, size);
+       }
+       return NULL;
+}
+
+void
+bpf_jit_freecode(bpfjit_func_t jcode)
+{
+       KASSERT(bpfjit_module_ops.bj_free_code != NULL);
+       bpfjit_module_ops.bj_free_code(jcode);
+}
+
 static int
 bpf_movein(struct uio *uio, int linktype, uint64_t mtu, struct mbuf **mp,
           struct sockaddr *sockp)
@@ -1062,7 +1079,7 @@
 bpf_setf(struct bpf_d *d, struct bpf_program *fp)
 {
        struct bpf_insn *fcode, *old;
-       bpfjit_function_t jcode, oldj;
+       bpfjit_func_t jcode, oldj;
        size_t flen, size;
        int s;
 
@@ -1086,8 +1103,9 @@
                        return EINVAL;
                }
                membar_consumer();
-               if (bpf_jit && bpfjit_module_ops.bj_generate_code != NULL) {
-                       jcode = bpfjit_module_ops.bj_generate_code(fcode, flen);
+               if (bpf_jit) {
+                       bpf_ctx_t *bc = bpf_default_ctx();
+                       jcode = bpf_jit_generate(bc, fcode, flen);
                }
        } else {
                fcode = NULL;
@@ -1104,10 +1122,8 @@
        if (old) {
                free(old, M_DEVBUF);
        }
-
-       if (oldj != NULL) {
-               KASSERT(bpfjit_module_ops.bj_free_code != NULL);
-               bpfjit_module_ops.bj_free_code(oldj);
+       if (oldj) {
+               bpf_jit_freecode(oldj);
        }
 
        return 0;
@@ -1719,8 +1735,7 @@
                free(d->bd_filter, M_DEVBUF);
 
        if (d->bd_jitcode != NULL) {
-               KASSERT(bpfjit_module_ops.bj_free_code != NULL);
-               bpfjit_module_ops.bj_free_code(d->bd_jitcode);
+               bpf_jit_freecode(d->bd_jitcode);
        }
 }
 
diff -r 9594d71943de -r 230807f405e6 sys/net/bpf.h
--- a/sys/net/bpf.h     Thu Nov 14 18:54:40 2013 +0000
+++ b/sys/net/bpf.h     Fri Nov 15 00:12:44 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bpf.h,v 1.62 2013/09/18 23:34:55 rmind Exp $   */
+/*     $NetBSD: bpf.h,v 1.63 2013/11/15 00:12:44 rmind Exp $   */
 
 /*
  * Copyright (c) 1990, 1991, 1993
@@ -275,6 +275,11 @@
 #define BPF_JUMP(code, k, jt, jf) { (uint16_t)(code), jt, jf, k }
 
 /*
+ * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
+ */
+#define        BPF_MEMWORDS    16
+
+/*
  * Structure to retrieve available DLTs for the interface.
  */
 struct bpf_dltlist {
@@ -282,8 +287,33 @@
        u_int   *bfl_list;      /* array of DLTs */
 };
 
+struct bpf_ctx;
+typedef struct bpf_ctx bpf_ctx_t;
+
+struct bpf_args;
+typedef struct bpf_args bpf_args_t;
+
+#if defined(_KERNEL) || defined(__BPF_PRIVATE)
+typedef uint32_t (*bpf_copfunc_t)(bpf_ctx_t *, bpf_args_t *, uint32_t);
+
+struct bpf_args {
+       const struct mbuf *     pkt;
+       size_t                  wirelen;
+       size_t                  buflen;
+       uint32_t                mem[BPF_MEMWORDS];
+       void *                  arg;
+};
+
+struct bpf_ctx {
+       const bpf_copfunc_t *   copfuncs;
+       size_t                  nfuncs;
+};
+#endif
+
 #ifdef _KERNEL
+#include <net/bpfjit.h>
 #include <net/if.h>
+
 struct bpf_if;
 
 struct bpf_ops {
@@ -380,19 +410,17 @@
 
 void    bpfilterattach(int);
 
-struct bpf_ctx;
-typedef struct bpf_ctx bpf_ctx_t;
-typedef uint32_t (*bpf_copfunc_t)(const struct mbuf *, void *,
-    uint32_t, uint32_t *);
-
 bpf_ctx_t *bpf_create(void);
+bpf_ctx_t *bpf_default_ctx(void);
 void   bpf_destroy(bpf_ctx_t *);
 
 int    bpf_set_cop(bpf_ctx_t *, const bpf_copfunc_t *, size_t);
-u_int  bpf_filter_ext(bpf_ctx_t *, void *, const struct bpf_insn *,
-           const u_char *, u_int, u_int);
+u_int  bpf_filter_ext(bpf_ctx_t *, const struct bpf_insn *, bpf_args_t *);
 int    bpf_validate_ext(bpf_ctx_t *, const struct bpf_insn *, int);
 
+bpfjit_func_t bpf_jit_generate(bpf_ctx_t *, void *, size_t);
+void   bpf_jit_freecode(bpfjit_func_t);
+
 #endif
 
 int    bpf_validate(const struct bpf_insn *, int);
@@ -400,9 +428,4 @@
 
 __END_DECLS
 
-/*
- * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
- */
-#define BPF_MEMWORDS 16
-
 #endif /* !_NET_BPF_H_ */
diff -r 9594d71943de -r 230807f405e6 sys/net/bpf_filter.c
--- a/sys/net/bpf_filter.c      Thu Nov 14 18:54:40 2013 +0000
+++ b/sys/net/bpf_filter.c      Fri Nov 15 00:12:44 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bpf_filter.c,v 1.60 2013/10/05 22:38:52 rmind Exp $    */
+/*     $NetBSD: bpf_filter.c,v 1.61 2013/11/15 00:12:44 rmind Exp $    */
 
 /*-
  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: bpf_filter.c,v 1.60 2013/10/05 22:38:52 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: bpf_filter.c,v 1.61 2013/11/15 00:12:44 rmind Exp $");
 
 #if 0
 #if !(defined(lint) || defined(KERNEL))
@@ -51,15 +51,11 @@
 #include <sys/kmem.h>
 #include <sys/endian.h>
 
+#define        __BPF_PRIVATE
 #include <net/bpf.h>
 
 #ifdef _KERNEL
 
-struct bpf_ctx {
-       const bpf_copfunc_t *   copfuncs;
-       size_t                  nfuncs;
-};
-
 /* Default BPF context (zeroed). */
 static bpf_ctx_t               bpf_def_ctx;
 
@@ -83,6 +79,12 @@
        return 0;
 }
 
+bpf_ctx_t *
+bpf_default_ctx(void)
+{
+       return &bpf_def_ctx;
+}
+
 #endif
 
 #define EXTRACT_SHORT(p)       be16dec(p)
@@ -183,12 +185,17 @@
 bpf_filter(const struct bpf_insn *pc, const u_char *p, u_int wirelen,
     u_int buflen)
 {
-       return bpf_filter_ext(&bpf_def_ctx, NULL, pc, p, wirelen, buflen);
+       bpf_args_t args = {
+               .pkt = (const struct mbuf *)p,
+               .wirelen = wirelen,
+               .buflen = buflen,
+               .arg = NULL
+       };
+       return bpf_filter_ext(&bpf_def_ctx, pc, &args);
 }
 
 u_int
-bpf_filter_ext(bpf_ctx_t *bc, void *arg, const struct bpf_insn *pc,
-    const u_char *p, u_int wirelen, u_int buflen)
+bpf_filter_ext(bpf_ctx_t *bc, const struct bpf_insn *pc, bpf_args_t *args)
 #else
 u_int
 bpf_filter(const struct bpf_insn *pc, const u_char *p, u_int wirelen,
@@ -196,13 +203,17 @@
 #endif
 {
        uint32_t A, X, k;
-       uint32_t mem[BPF_MEMWORDS];
-#ifdef _KERNEL
-       const struct mbuf * const m0 = (const struct mbuf *)p;
-
-       KASSERT(bc != NULL);
+#ifndef _KERNEL
+       bpf_args_t args_store = {
+               .pkt = (const struct mbuf *)p,
+               .wirelen = wirelen,
+               .buflen = buflen,
+               .arg = NULL
+       };
+       bpf_args_t * const args = &args_store;
+#else
+       const uint8_t * const p = (const uint8_t *)args->pkt;
 #endif
-
        if (pc == 0) {
                /*
                 * No filter means accept all.
@@ -237,13 +248,14 @@
 
                case BPF_LD|BPF_W|BPF_ABS:
                        k = pc->k;
-                       if (k > buflen || sizeof(int32_t) > buflen - k) {
+                       if (k > args->buflen ||
+                           sizeof(int32_t) > args->buflen - k) {
 #ifdef _KERNEL
                                int merr;
 
-                               if (buflen != 0)
+                               if (args->buflen != 0)
                                        return 0;
-                               A = m_xword(m0, k, &merr);
+                               A = m_xword(args->pkt, k, &merr);
                                if (merr != 0)
                                        return 0;
                                continue;
@@ -256,13 +268,14 @@
 
                case BPF_LD|BPF_H|BPF_ABS:
                        k = pc->k;
-                       if (k > buflen || sizeof(int16_t) > buflen - k) {
+                       if (k > args->buflen ||
+                           sizeof(int16_t) > args->buflen - k) {
 #ifdef _KERNEL
                                int merr;
 
-                               if (buflen != 0)
+                               if (args->buflen != 0)
                                        return 0;



Home | Main Index | Thread Index | Old Index