Search in sources :

Example 21 with Disjunction

use of org.batfish.datamodel.routing_policy.expr.Disjunction 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)

Example 22 with Disjunction

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

the class AstVisitor method visit.

/*
   * Walk starting from an AST boolean expression
   */
public void visit(Configuration conf, BooleanExpr e, Consumer<Statement> fs, Consumer<BooleanExpr> fe) {
    fe.accept(e);
    if (e instanceof Conjunction) {
        Conjunction c = (Conjunction) e;
        for (BooleanExpr be : c.getConjuncts()) {
            visit(conf, be, fs, fe);
        }
    } else if (e instanceof Disjunction) {
        Disjunction d = (Disjunction) e;
        for (BooleanExpr be : d.getDisjuncts()) {
            visit(conf, be, fs, fe);
        }
    } else if (e instanceof ConjunctionChain) {
        ConjunctionChain c = (ConjunctionChain) e;
        for (BooleanExpr be : c.getSubroutines()) {
            visit(conf, be, fs, fe);
        }
    } else if (e instanceof DisjunctionChain) {
        DisjunctionChain d = (DisjunctionChain) e;
        for (BooleanExpr be : d.getSubroutines()) {
            visit(conf, be, fs, fe);
        }
    } else if (e instanceof Not) {
        Not n = (Not) e;
        visit(conf, n.getExpr(), fs, fe);
    } else if (e instanceof CallExpr) {
        CallExpr c = (CallExpr) e;
        RoutingPolicy rp = conf.getRoutingPolicies().get(c.getCalledPolicyName());
        visit(conf, rp.getStatements(), fs, fe);
    }
}
Also used : Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) Not(org.batfish.datamodel.routing_policy.expr.Not) Conjunction(org.batfish.datamodel.routing_policy.expr.Conjunction) CallExpr(org.batfish.datamodel.routing_policy.expr.CallExpr) RoutingPolicy(org.batfish.datamodel.routing_policy.RoutingPolicy) ConjunctionChain(org.batfish.datamodel.routing_policy.expr.ConjunctionChain) DisjunctionChain(org.batfish.datamodel.routing_policy.expr.DisjunctionChain) BooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExpr)

Example 23 with Disjunction

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

the class BatfishCompressor method applyFilters.

/**
 * Update a list of RoutingPolicy statements by filtering traffic according to the input filter.
 *
 * @param statements The list of RoutingPolicy statements.
 * @param filter The filter used to restrict traffic
 * @return A new list of RoutingPolicy statements
 */
// PrefixTrie: capture the prefixes you are installing to allow traffic through. Restrict
// to those prefixes
// Boolean: are the prefixes for the default equivalence class?
private List<Statement> applyFilters(List<Statement> statements, @Nullable EquivalenceClassFilter filter) {
    If i = new If();
    List<Statement> newStatements = new ArrayList<>();
    List<Statement> falseStatements = new ArrayList<>();
    Statement reject = new StaticStatement(Statements.ExitReject);
    falseStatements.add(reject);
    if (filter == null) {
        StaticBooleanExpr sbe = new StaticBooleanExpr(BooleanExprs.False);
        i.setGuard(sbe);
    } else {
        AbstractionPrefixSet eps = new AbstractionPrefixSet(filter._prefixTrie);
        MatchPrefixSet match = new MatchPrefixSet(new DestinationNetwork(), eps);
        if (filter._isForDefaultSlice) {
            // Let traffic through if it passes the filter or was advertised from outside the network.
            Disjunction pfxOrExternal = new Disjunction();
            pfxOrExternal.setDisjuncts(ImmutableList.of(match, matchExternalTraffic()));
            i.setGuard(pfxOrExternal);
        } else {
            // Not default equivalence class, so just let traffic through if dest matches the filter
            i.setGuard(match);
        }
    }
    i.setFalseStatements(falseStatements);
    i.setTrueStatements(statements);
    newStatements.add(i);
    return newStatements;
}
Also used : Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) DestinationNetwork(org.batfish.datamodel.routing_policy.expr.DestinationNetwork) AbstractionPrefixSet(org.batfish.datamodel.routing_policy.expr.AbstractionPrefixSet) StaticStatement(org.batfish.datamodel.routing_policy.statement.Statements.StaticStatement) Statement(org.batfish.datamodel.routing_policy.statement.Statement) StaticStatement(org.batfish.datamodel.routing_policy.statement.Statements.StaticStatement) MatchPrefixSet(org.batfish.datamodel.routing_policy.expr.MatchPrefixSet) ArrayList(java.util.ArrayList) If(org.batfish.datamodel.routing_policy.statement.If) StaticBooleanExpr(org.batfish.datamodel.routing_policy.expr.BooleanExprs.StaticBooleanExpr)

Example 24 with Disjunction

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

the class RouteReflectionTest method setup.

/**
 * Initialize builders with values common to all tests
 */
@Before
public void setup() {
    _ab = new BgpAdvertisement.Builder().setClusterList(ImmutableSortedSet.of()).setCommunities(ImmutableSortedSet.of()).setDstVrf(Configuration.DEFAULT_VRF_NAME).setOriginType(OriginType.INCOMPLETE).setSrcProtocol(RoutingProtocol.AGGREGATE).setSrcVrf(Configuration.DEFAULT_VRF_NAME).setType(BgpAdvertisementType.EBGP_SENT);
    _nf = new NetworkFactory();
    _cb = _nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS);
    _ib = _nf.interfaceBuilder().setActive(true);
    _nb = _nf.bgpNeighborBuilder().setLocalAs(2);
    _pb = _nf.bgpProcessBuilder();
    _vb = _nf.vrfBuilder().setName(Configuration.DEFAULT_VRF_NAME);
    If acceptIffBgp = new If();
    Disjunction guard = new Disjunction();
    guard.setDisjuncts(ImmutableList.of(new MatchProtocol(RoutingProtocol.BGP), new MatchProtocol(RoutingProtocol.IBGP)));
    acceptIffBgp.setGuard(guard);
    acceptIffBgp.setTrueStatements(ImmutableList.of(Statements.ExitAccept.toStaticStatement()));
    acceptIffBgp.setFalseStatements(ImmutableList.of(Statements.ExitReject.toStaticStatement()));
    /* Builder that creates default BGP export policy */
    _defaultExportPolicyBuilder = _nf.routingPolicyBuilder().setStatements(ImmutableList.of(acceptIffBgp));
    /* Builder that creates BGP export policy that rejects everything */
    _nullExportPolicyBuilder = _nf.routingPolicyBuilder().setStatements(ImmutableList.of(Statements.ExitReject.toStaticStatement()));
}
Also used : Disjunction(org.batfish.datamodel.routing_policy.expr.Disjunction) NetworkFactory(org.batfish.datamodel.NetworkFactory) If(org.batfish.datamodel.routing_policy.statement.If) MatchProtocol(org.batfish.datamodel.routing_policy.expr.MatchProtocol) Before(org.junit.Before)

Aggregations

Disjunction (org.batfish.datamodel.routing_policy.expr.Disjunction)24 BooleanExpr (org.batfish.datamodel.routing_policy.expr.BooleanExpr)18 RoutingPolicy (org.batfish.datamodel.routing_policy.RoutingPolicy)10 If (org.batfish.datamodel.routing_policy.statement.If)10 Conjunction (org.batfish.datamodel.routing_policy.expr.Conjunction)8 MatchProtocol (org.batfish.datamodel.routing_policy.expr.MatchProtocol)8 CallExpr (org.batfish.datamodel.routing_policy.expr.CallExpr)7 MatchPrefixSet (org.batfish.datamodel.routing_policy.expr.MatchPrefixSet)7 ArrayList (java.util.ArrayList)6 MatchPrefix6Set (org.batfish.datamodel.routing_policy.expr.MatchPrefix6Set)6 Not (org.batfish.datamodel.routing_policy.expr.Not)6 DestinationNetwork (org.batfish.datamodel.routing_policy.expr.DestinationNetwork)5 DisjunctionChain (org.batfish.datamodel.routing_policy.expr.DisjunctionChain)5 BatfishException (org.batfish.common.BatfishException)4 ConjunctionChain (org.batfish.datamodel.routing_policy.expr.ConjunctionChain)4 DestinationNetwork6 (org.batfish.datamodel.routing_policy.expr.DestinationNetwork6)4 MatchAsPath (org.batfish.datamodel.routing_policy.expr.MatchAsPath)4 NamedPrefixSet (org.batfish.datamodel.routing_policy.expr.NamedPrefixSet)4 Statement (org.batfish.datamodel.routing_policy.statement.Statement)4 BigInteger (java.math.BigInteger)3