Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/npf/npfctl npfctl: optimise fetch_l3() to avoid unn...



details:   https://anonhg.NetBSD.org/src/rev/3768881c283c
branches:  trunk
changeset: 791114:3768881c283c
user:      rmind <rmind%NetBSD.org@localhost>
date:      Tue Nov 05 01:50:30 2013 +0000

description:
npfctl: optimise fetch_l3() to avoid unnecessary call to NPF_COP_L3.

diffstat:

 usr.sbin/npf/npfctl/npf_bpf_comp.c |  31 ++++++++++++++++++++-----------
 1 files changed, 20 insertions(+), 11 deletions(-)

diffs (73 lines):

diff -r a76637abbf69 -r 3768881c283c usr.sbin/npf/npfctl/npf_bpf_comp.c
--- a/usr.sbin/npf/npfctl/npf_bpf_comp.c        Tue Nov 05 00:40:33 2013 +0000
+++ b/usr.sbin/npf/npfctl/npf_bpf_comp.c        Tue Nov 05 01:50:30 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: npf_bpf_comp.c,v 1.1 2013/09/19 01:04:45 rmind Exp $   */
+/*     $NetBSD: npf_bpf_comp.c,v 1.2 2013/11/05 01:50:30 rmind Exp $   */
 
 /*-
  * Copyright (c) 2010-2013 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: npf_bpf_comp.c,v 1.1 2013/09/19 01:04:45 rmind Exp $");
+__RCSID("$NetBSD: npf_bpf_comp.c,v 1.2 2013/11/05 01:50:30 rmind Exp $");
 
 #include <stdlib.h>
 #include <stdbool.h>
@@ -307,12 +307,21 @@
                 * A <- IP version; A == expected-version?
                 * If no particular version specified, check for non-zero.
                 */
-               struct bpf_insn insns_l3[] = {
-                       BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
-                       BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ver, jt, jf),
-               };
-               add_insns(ctx, insns_l3, __arraycount(insns_l3));
-               ctx->flags |= FETCHED_L3;
+               if ((ctx->flags & FETCHED_L3) == 0) {
+                       struct bpf_insn insns_l3[] = {
+                               BPF_STMT(BPF_MISC+BPF_COP, NPF_COP_L3),
+                               BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ver, jt, jf),
+                       };
+                       add_insns(ctx, insns_l3, __arraycount(insns_l3));
+                       ctx->flags |= FETCHED_L3;
+               } else {
+                       /* IP version is already fetched in BPF_MW_IPVER. */
+                       struct bpf_insn insns_af[] = {
+                               BPF_STMT(BPF_LD+BPF_W+BPF_MEM, BPF_MW_IPVER),
+                               BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ver, jt, jf),
+                       };
+                       add_insns(ctx, insns_af, __arraycount(insns_af));
+               }
                ctx->af = af;
 
                if (af) {
@@ -471,7 +480,7 @@
        off = (opts & MATCH_SRC) ? sport_off : dport_off;
 
        /* X <- IP header length */
-       fetch_l3(ctx, 0, X_EQ_L4OFF);
+       fetch_l3(ctx, AF_UNSPEC, X_EQ_L4OFF);
 
        struct bpf_insn insns_fetch[] = {
                /* A <- port */
@@ -513,7 +522,7 @@
        const u_int tcpfl_off = offsetof(struct tcphdr, th_flags);
 
        /* X <- IP header length */
-       fetch_l3(ctx, 0, X_EQ_L4OFF);
+       fetch_l3(ctx, AF_UNSPEC, X_EQ_L4OFF);
 
        struct bpf_insn insns_tf[] = {
                /* A <- TCP flags */
@@ -554,7 +563,7 @@
        assert(type != -1 || code != -1);
 
        /* X <- IP header length */
-       fetch_l3(ctx, 0, X_EQ_L4OFF);
+       fetch_l3(ctx, AF_UNSPEC, X_EQ_L4OFF);
 
        if (type != -1) {
                struct bpf_insn insns_type[] = {



Home | Main Index | Thread Index | Old Index