Search in sources :

Example 1 with MatchIpv6

use of org.batfish.datamodel.routing_policy.expr.MatchIpv6 in project batfish by batfish.

the class TransferBDD method compute.

/*
   * Convert a Batfish AST boolean expression to a symbolic Z3 boolean expression
   * by performing inlining of stateful side effects.
   */
private TransferResult<TransferReturn, BDD> compute(BooleanExpr expr, TransferParam<BDDRoute> p) {
    // TODO: right now everything is IPV4
    if (expr instanceof MatchIpv4) {
        p.debug("MatchIpv4");
        TransferReturn ret = new TransferReturn(p.getData(), factory.one());
        p.debug("MatchIpv4 Result: " + ret);
        return fromExpr(ret);
    }
    if (expr instanceof MatchIpv6) {
        p.debug("MatchIpv6");
        TransferReturn ret = new TransferReturn(p.getData(), factory.zero());
        return fromExpr(ret);
    }
    if (expr instanceof Conjunction) {
        p.debug("Conjunction");
        Conjunction c = (Conjunction) expr;
        BDD acc = factory.one();
        TransferResult<TransferReturn, BDD> result = new TransferResult<>();
        for (BooleanExpr be : c.getConjuncts()) {
            TransferResult<TransferReturn, BDD> r = compute(be, p.indent());
            acc = acc.and(r.getReturnValue().getSecond());
        }
        TransferReturn ret = new TransferReturn(p.getData(), acc);
        p.debug("Conjunction return: " + acc);
        return result.setReturnValue(ret);
    }
    if (expr instanceof Disjunction) {
        p.debug("Disjunction");
        Disjunction d = (Disjunction) expr;
        BDD acc = factory.zero();
        TransferResult<TransferReturn, BDD> result = new TransferResult<>();
        for (BooleanExpr be : d.getDisjuncts()) {
            TransferResult<TransferReturn, BDD> r = compute(be, p.indent());
            result = result.addChangedVariables(r);
            acc = acc.or(r.getReturnValue().getSecond());
        }
        TransferReturn ret = new TransferReturn(p.getData(), acc);
        p.debug("Disjunction return: " + acc);
        return result.setReturnValue(ret);
    }
    // TODO: thread the BDDRecord through calls
    if (expr instanceof ConjunctionChain) {
        p.debug("ConjunctionChain");
        ConjunctionChain d = (ConjunctionChain) expr;
        List<BooleanExpr> conjuncts = new ArrayList<>(d.getSubroutines());
        if (p.getDefaultPolicy() != null) {
            BooleanExpr be = new CallExpr(p.getDefaultPolicy().getDefaultPolicy());
            conjuncts.add(be);
        }
        if (conjuncts.size() == 0) {
            TransferReturn ret = new TransferReturn(p.getData(), factory.one());
            return fromExpr(ret);
        } else {
            TransferResult<TransferReturn, BDD> result = new TransferResult<>();
            TransferParam<BDDRoute> record = p;
            BDD acc = factory.zero();
            for (int i = conjuncts.size() - 1; i >= 0; i--) {
                BooleanExpr conjunct = conjuncts.get(i);
                TransferParam<BDDRoute> param = record.setDefaultPolicy(null).setChainContext(TransferParam.ChainContext.CONJUNCTION).indent();
                TransferResult<TransferReturn, BDD> r = compute(conjunct, param);
                record = record.setData(r.getReturnValue().getFirst());
                acc = ite(r.getFallthroughValue(), acc, r.getReturnValue().getSecond());
            }
            TransferReturn ret = new TransferReturn(record.getData(), acc);
            return result.setReturnValue(ret);
        }
    }
    if (expr instanceof DisjunctionChain) {
        p.debug("DisjunctionChain");
        DisjunctionChain d = (DisjunctionChain) expr;
        List<BooleanExpr> disjuncts = new ArrayList<>(d.getSubroutines());
        if (p.getDefaultPolicy() != null) {
            BooleanExpr be = new CallExpr(p.getDefaultPolicy().getDefaultPolicy());
            disjuncts.add(be);
        }
        if (disjuncts.size() == 0) {
            TransferReturn ret = new TransferReturn(p.getData(), factory.zero());
            return fromExpr(ret);
        } else {
            TransferResult<TransferReturn, BDD> result = new TransferResult<>();
            TransferParam<BDDRoute> record = p;
            BDD acc = factory.zero();
            for (int i = disjuncts.size() - 1; i >= 0; i--) {
                BooleanExpr disjunct = disjuncts.get(i);
                TransferParam<BDDRoute> param = record.setDefaultPolicy(null).setChainContext(TransferParam.ChainContext.CONJUNCTION).indent();
                TransferResult<TransferReturn, BDD> r = compute(disjunct, param);
                record = record.setData(r.getReturnValue().getFirst());
                acc = ite(r.getFallthroughValue(), acc, r.getReturnValue().getSecond());
            }
            TransferReturn ret = new TransferReturn(record.getData(), acc);
            return result.setReturnValue(ret);
        }
    }
    if (expr instanceof Not) {
        p.debug("mkNot");
        Not n = (Not) expr;
        TransferResult<TransferReturn, BDD> result = compute(n.getExpr(), p);
        TransferReturn r = result.getReturnValue();
        TransferReturn ret = new TransferReturn(r.getFirst(), r.getSecond().not());
        return result.setReturnValue(ret);
    }
    if (expr instanceof MatchProtocol) {
        MatchProtocol mp = (MatchProtocol) expr;
        Protocol proto = Protocol.fromRoutingProtocol(mp.getProtocol());
        if (proto == null) {
            p.debug("MatchProtocol(" + mp.getProtocol().protocolName() + "): false");
            TransferReturn ret = new TransferReturn(p.getData(), factory.zero());
            return fromExpr(ret);
        }
        BDD protoMatch = p.getData().getProtocolHistory().value(proto);
        p.debug("MatchProtocol(" + mp.getProtocol().protocolName() + "): " + protoMatch);
        TransferReturn ret = new TransferReturn(p.getData(), protoMatch);
        return fromExpr(ret);
    }
    if (expr instanceof MatchPrefixSet) {
        p.debug("MatchPrefixSet");
        MatchPrefixSet m = (MatchPrefixSet) expr;
        BDD r = matchPrefixSet(p.indent(), _conf, m.getPrefixSet(), p.getData());
        TransferReturn ret = new TransferReturn(p.getData(), r);
        return fromExpr(ret);
    // TODO: implement me
    } else if (expr instanceof MatchPrefix6Set) {
        p.debug("MatchPrefix6Set");
        TransferReturn ret = new TransferReturn(p.getData(), factory.zero());
        return fromExpr(ret);
    } else if (expr instanceof CallExpr) {
        p.debug("CallExpr");
        CallExpr c = (CallExpr) expr;
        String router = _conf.getName();
        String name = c.getCalledPolicyName();
        TransferResult<TransferReturn, BDD> r = CACHE.get(router, name);
        if (r != null) {
            return r;
        }
        RoutingPolicy pol = _conf.getRoutingPolicies().get(name);
        p = p.setCallContext(TransferParam.CallContext.EXPR_CALL);
        r = compute(pol.getStatements(), p.indent().enterScope(name));
        CACHE.put(router, name, r);
        return r;
    } else if (expr instanceof WithEnvironmentExpr) {
        p.debug("WithEnvironmentExpr");
        // TODO: this is not correct
        WithEnvironmentExpr we = (WithEnvironmentExpr) expr;
        // TODO: postStatements() and preStatements()
        return compute(we.getExpr(), p.deepCopy());
    } else if (expr instanceof MatchCommunitySet) {
        p.debug("MatchCommunitySet");
        MatchCommunitySet mcs = (MatchCommunitySet) expr;
        BDD c = matchCommunitySet(p.indent(), _conf, mcs.getExpr(), p.getData());
        TransferReturn ret = new TransferReturn(p.getData(), c);
        return fromExpr(ret);
    } else if (expr instanceof BooleanExprs.StaticBooleanExpr) {
        BooleanExprs.StaticBooleanExpr b = (BooleanExprs.StaticBooleanExpr) expr;
        TransferReturn ret;
        switch(b.getType()) {
            case CallExprContext:
                p.debug("CallExprContext");
                BDD x1 = mkBDD(p.getCallContext() == TransferParam.CallContext.EXPR_CALL);
                ret = new TransferReturn(p.getData(), x1);
                return fromExpr(ret);
            case CallStatementContext:
                p.debug("CallStmtContext");
                BDD x2 = mkBDD(p.getCallContext() == TransferParam.CallContext.STMT_CALL);
                ret = new TransferReturn(p.getData(), x2);
                return fromExpr(ret);
            case True:
                p.debug("True");
                ret = new TransferReturn(p.getData(), factory.one());
                return fromExpr(ret);
            case False:
                p.debug("False");
                ret = new TransferReturn(p.getData(), factory.zero());
                return fromExpr(ret);
            default:
                throw new BatfishException("Unhandled " + BooleanExprs.class.getCanonicalName() + ": " + b.getType());
        }
    } else if (expr instanceof MatchAsPath) {
        p.debug("MatchAsPath");
        // System.out.println("Warning: use of unimplemented feature MatchAsPath");
        TransferReturn ret = new TransferReturn(p.getData(), factory.one());
        return fromExpr(ret);
    }
    throw new BatfishException("TODO: compute expr transfer function: " + expr);
}
Also used : MatchPrefix6Set(org.batfish.datamodel.routing_policy.expr.MatchPrefix6Set) BDD(net.sf.javabdd.BDD) ArrayList(java.util.ArrayList) MatchCommunitySet(org.batfish.datamodel.routing_policy.expr.MatchCommunitySet) TransferResult(org.batfish.symbolic.TransferResult) WithEnvironmentExpr(org.batfish.datamodel.routing_policy.expr.WithEnvironmentExpr) BooleanExprs(org.batfish.datamodel.routing_policy.expr.BooleanExprs) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) CallExpr(org.batfish.datamodel.routing_policy.expr.CallExpr) DisjunctionChain(org.batfish.datamodel.routing_policy.expr.DisjunctionChain) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) Protocol(org.batfish.symbolic.Protocol) MatchAsPath(org.batfish.datamodel.routing_policy.expr.MatchAsPath) BooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExpr) BatfishException(org.batfish.common.BatfishException) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) MatchIpv6(org.batfish.datamodel.routing_policy.expr.MatchIpv6) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) ConjunctionChain(org.batfish.datamodel.routing_policy.expr.ConjunctionChain) MatchIpv4(org.batfish.datamodel.routing_policy.expr.MatchIpv4) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) Not(org.batfish.datamodel.routing_policy.expr.Not)

Example 2 with MatchIpv6

use of org.batfish.datamodel.routing_policy.expr.MatchIpv6 in project batfish by batfish.

the class TransferSSA method compute.

/*
   * Convert a Batfish AST boolean expression to a symbolic Z3 boolean expression
   * by performing inlining of stateful side effects.
   */
private TransferResult<BoolExpr, BoolExpr> compute(BooleanExpr expr, TransferParam<SymbolicRoute> p) {
    // TODO: right now everything is IPV4
    if (expr instanceof MatchIpv4) {
        p.debug("MatchIpv4");
        return fromExpr(_enc.mkTrue());
    }
    if (expr instanceof MatchIpv6) {
        p.debug("MatchIpv6");
        return fromExpr(_enc.mkFalse());
    }
    if (expr instanceof Conjunction) {
        p.debug("Conjunction");
        Conjunction c = (Conjunction) expr;
        BoolExpr acc = _enc.mkTrue();
        TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
        for (BooleanExpr be : c.getConjuncts()) {
            TransferResult<BoolExpr, BoolExpr> r = compute(be, p.indent());
            result = result.addChangedVariables(r);
            acc = _enc.mkAnd(acc, r.getReturnValue());
        }
        p.debug("has changed variable");
        return result.setReturnValue(acc);
    }
    if (expr instanceof Disjunction) {
        p.debug("Disjunction");
        Disjunction d = (Disjunction) expr;
        BoolExpr acc = _enc.mkFalse();
        TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
        for (BooleanExpr be : d.getDisjuncts()) {
            TransferResult<BoolExpr, BoolExpr> r = compute(be, p.indent());
            result = result.addChangedVariables(r);
            acc = _enc.mkOr(acc, r.getReturnValue());
        }
        p.debug("has changed variable");
        return result.setReturnValue(acc);
    }
    if (expr instanceof ConjunctionChain) {
        p.debug("ConjunctionChain");
        ConjunctionChain d = (ConjunctionChain) expr;
        List<BooleanExpr> conjuncts = new ArrayList<>(d.getSubroutines());
        if (p.getDefaultPolicy() != null) {
            BooleanExpr be = new CallExpr(p.getDefaultPolicy().getDefaultPolicy());
            conjuncts.add(be);
        }
        if (conjuncts.size() == 0) {
            return fromExpr(_enc.mkTrue());
        } else {
            TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
            BoolExpr acc = _enc.mkFalse();
            for (int i = conjuncts.size() - 1; i >= 0; i--) {
                BooleanExpr conjunct = conjuncts.get(i);
                TransferParam<SymbolicRoute> param = p.setDefaultPolicy(null).setChainContext(TransferParam.ChainContext.CONJUNCTION);
                TransferResult<BoolExpr, BoolExpr> r = compute(conjunct, param);
                result = result.addChangedVariables(r);
                acc = _enc.mkIf(r.getFallthroughValue(), acc, r.getReturnValue());
            }
            p.debug("ConjunctionChain Result: " + acc);
            return result.setReturnValue(acc);
        }
    }
    if (expr instanceof DisjunctionChain) {
        p.debug("DisjunctionChain");
        DisjunctionChain d = (DisjunctionChain) expr;
        List<BooleanExpr> disjuncts = new ArrayList<>(d.getSubroutines());
        if (p.getDefaultPolicy() != null) {
            BooleanExpr be = new CallExpr(p.getDefaultPolicy().getDefaultPolicy());
            disjuncts.add(be);
        }
        if (disjuncts.size() == 0) {
            return fromExpr(_enc.mkTrue());
        } else {
            TransferResult<BoolExpr, BoolExpr> result = new TransferResult<>();
            BoolExpr acc = _enc.mkFalse();
            for (int i = disjuncts.size() - 1; i >= 0; i--) {
                BooleanExpr disjunct = disjuncts.get(i);
                TransferParam<SymbolicRoute> param = p.setDefaultPolicy(null).setChainContext(TransferParam.ChainContext.CONJUNCTION);
                TransferResult<BoolExpr, BoolExpr> r = compute(disjunct, param);
                result.addChangedVariables(r);
                acc = _enc.mkIf(r.getFallthroughValue(), acc, r.getReturnValue());
            }
            p.debug("DisjunctionChain Result: " + acc);
            return result.setReturnValue(acc);
        }
    }
    if (expr instanceof Not) {
        p.debug("mkNot");
        Not n = (Not) expr;
        TransferResult<BoolExpr, BoolExpr> result = compute(n.getExpr(), p);
        return result.setReturnValue(_enc.mkNot(result.getReturnValue()));
    }
    if (expr instanceof MatchProtocol) {
        MatchProtocol mp = (MatchProtocol) expr;
        Protocol proto = Protocol.fromRoutingProtocol(mp.getProtocol());
        if (proto == null) {
            p.debug("MatchProtocol(" + mp.getProtocol().protocolName() + "): false");
            return fromExpr(_enc.mkFalse());
        }
        if (_other.getProtocolHistory() == null) {
            BoolExpr protoMatch = _enc.mkBool(proto.equals(_proto));
            p.debug("MatchProtocol(" + mp.getProtocol().protocolName() + "): " + protoMatch);
            return fromExpr(protoMatch);
        }
        BoolExpr protoMatch = _other.getProtocolHistory().checkIfValue(proto);
        p.debug("MatchProtocol(" + mp.getProtocol().protocolName() + "): " + protoMatch);
        return fromExpr(protoMatch);
    }
    if (expr instanceof MatchPrefixSet) {
        p.debug("MatchPrefixSet");
        MatchPrefixSet m = (MatchPrefixSet) expr;
        // For BGP, may change prefix length
        TransferResult<BoolExpr, BoolExpr> result = matchPrefixSet(_conf, m.getPrefixSet(), p.getData());
        return result.setReturnAssignedValue(_enc.mkTrue());
    // TODO: implement me
    } else if (expr instanceof MatchPrefix6Set) {
        p.debug("MatchPrefix6Set");
        return fromExpr(_enc.mkFalse());
    } else if (expr instanceof CallExpr) {
        p.debug("CallExpr");
        // TODO: the call can modify certain fields, need to keep track of these variables
        CallExpr c = (CallExpr) expr;
        String name = c.getCalledPolicyName();
        RoutingPolicy pol = _conf.getRoutingPolicies().get(name);
        p = p.setCallContext(TransferParam.CallContext.EXPR_CALL);
        TransferResult<BoolExpr, BoolExpr> r = compute(pol.getStatements(), p.indent().enterScope(name), initialResult());
        p.debug("CallExpr (return): " + r.getReturnValue());
        p.debug("CallExpr (fallthrough): " + r.getFallthroughValue());
        return r;
    } else if (expr instanceof WithEnvironmentExpr) {
        p.debug("WithEnvironmentExpr");
        // TODO: this is not correct
        WithEnvironmentExpr we = (WithEnvironmentExpr) expr;
        // TODO: postStatements() and preStatements()
        return compute(we.getExpr(), p);
    } else if (expr instanceof MatchCommunitySet) {
        p.debug("MatchCommunitySet");
        MatchCommunitySet mcs = (MatchCommunitySet) expr;
        return fromExpr(matchCommunitySet(_conf, mcs.getExpr(), p.getData()));
    } else if (expr instanceof BooleanExprs.StaticBooleanExpr) {
        BooleanExprs.StaticBooleanExpr b = (BooleanExprs.StaticBooleanExpr) expr;
        switch(b.getType()) {
            case CallExprContext:
                p.debug("CallExprContext");
                return fromExpr(_enc.mkBool(p.getCallContext() == TransferParam.CallContext.EXPR_CALL));
            case CallStatementContext:
                p.debug("CallStmtContext");
                return fromExpr(_enc.mkBool(p.getCallContext() == TransferParam.CallContext.STMT_CALL));
            case True:
                p.debug("True");
                return fromExpr(_enc.mkTrue());
            case False:
                p.debug("False");
                return fromExpr(_enc.mkFalse());
            default:
                throw new BatfishException("Unhandled " + BooleanExprs.class.getCanonicalName() + ": " + b.getType());
        }
    } else if (expr instanceof MatchAsPath) {
        p.debug("MatchAsPath");
        System.out.println("Warning: use of unimplemented feature MatchAsPath");
        return fromExpr(_enc.mkFalse());
    }
    String s = (_isExport ? "export" : "import");
    String msg = String.format("Unimplemented feature %s for %s transfer function on interface %s", expr.toString(), s, _graphEdge.toString());
    throw new BatfishException(msg);
}
Also used : MatchPrefix6Set(org.batfish.datamodel.routing_policy.expr.MatchPrefix6Set) BoolExpr(com.microsoft.z3.BoolExpr) ArrayList(java.util.ArrayList) MatchCommunitySet(org.batfish.datamodel.routing_policy.expr.MatchCommunitySet) TransferResult(org.batfish.symbolic.TransferResult) WithEnvironmentExpr(org.batfish.datamodel.routing_policy.expr.WithEnvironmentExpr) BooleanExprs(org.batfish.datamodel.routing_policy.expr.BooleanExprs) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) CallExpr(org.batfish.datamodel.routing_policy.expr.CallExpr) DisjunctionChain(org.batfish.datamodel.routing_policy.expr.DisjunctionChain) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) Protocol(org.batfish.symbolic.Protocol) MatchAsPath(org.batfish.datamodel.routing_policy.expr.MatchAsPath) BooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExpr) BatfishException(org.batfish.common.BatfishException) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) MatchIpv6(org.batfish.datamodel.routing_policy.expr.MatchIpv6) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) ConjunctionChain(org.batfish.datamodel.routing_policy.expr.ConjunctionChain) MatchIpv4(org.batfish.datamodel.routing_policy.expr.MatchIpv4) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) Not(org.batfish.datamodel.routing_policy.expr.Not)

Aggregations

ArrayList (java.util.ArrayList)2 BatfishException (org.batfish.common.BatfishException)2 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)2 BooleanExpr (org.batfish.datamodel.routing_policy.expr.BooleanExpr)2 BooleanExprs (org.batfish.datamodel.routing_policy.expr.BooleanExprs)2 CallExpr (org.batfish.datamodel.routing_policy.expr.CallExpr)2 Conjunction (org.batfish.datamodel.routing_policy.expr.Conjunction)2 ConjunctionChain (org.batfish.datamodel.routing_policy.expr.ConjunctionChain)2 Disjunction (org.batfish.datamodel.routing_policy.expr.Disjunction)2 DisjunctionChain (org.batfish.datamodel.routing_policy.expr.DisjunctionChain)2 MatchAsPath (org.batfish.datamodel.routing_policy.expr.MatchAsPath)2 MatchCommunitySet (org.batfish.datamodel.routing_policy.expr.MatchCommunitySet)2 MatchIpv4 (org.batfish.datamodel.routing_policy.expr.MatchIpv4)2 MatchIpv6 (org.batfish.datamodel.routing_policy.expr.MatchIpv6)2 MatchPrefix6Set (org.batfish.datamodel.routing_policy.expr.MatchPrefix6Set)2 MatchPrefixSet (org.batfish.datamodel.routing_policy.expr.MatchPrefixSet)2 MatchProtocol (org.batfish.datamodel.routing_policy.expr.MatchProtocol)2 Not (org.batfish.datamodel.routing_policy.expr.Not)2 WithEnvironmentExpr (org.batfish.datamodel.routing_policy.expr.WithEnvironmentExpr)2 Protocol (org.batfish.symbolic.Protocol)2