Search in sources :

Example 1 with OriginateVrf

use of org.batfish.z3.state.OriginateVrf in project batfish by batfish.

the class AbstractNodJob method call.

@Override
public final NodJobResult call() {
    long startTime = System.currentTimeMillis();
    try (Context ctx = new Context()) {
        SmtInput smtInput = computeSmtInput(startTime, ctx);
        Map<OriginateVrf, Map<String, Long>> originateVrfConstraints = getOriginateVrfConstraints(ctx, smtInput);
        Set<Flow> flows = getFlows(originateVrfConstraints);
        return new NodJobResult(startTime, _logger.getHistory(), flows);
    } catch (Z3Exception e) {
        return new NodJobResult(startTime, _logger.getHistory(), new BatfishException("Error running NoD on concatenated data plane", e));
    }
}
Also used : Context(com.microsoft.z3.Context) BatfishException(org.batfish.common.BatfishException) OriginateVrf(org.batfish.z3.state.OriginateVrf) Z3Exception(com.microsoft.z3.Z3Exception) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Flow(org.batfish.datamodel.Flow)

Example 2 with OriginateVrf

use of org.batfish.z3.state.OriginateVrf in project batfish by batfish.

the class DefaultTransitionGeneratorTest method testVisitPostInVrf.

@Test
public void testVisitPostInVrf() {
    SynthesizerInput input = MockSynthesizerInput.builder().setEnabledInterfacesByNodeVrf(ImmutableMap.of(NODE1, ImmutableMap.of(VRF1, ImmutableSet.of(INTERFACE1, INTERFACE2), VRF2, ImmutableSet.of(INTERFACE3, INTERFACE4)), NODE2, ImmutableMap.of(VRF1, ImmutableSet.of(INTERFACE1, INTERFACE2), VRF2, ImmutableSet.of(INTERFACE3, INTERFACE4)))).build();
    Set<RuleStatement> rules = ImmutableSet.copyOf(DefaultTransitionGenerator.generateTransitions(input, ImmutableSet.of(PostInVrf.State.INSTANCE)));
    // CopyOriginateVrf
    assertThat(rules, hasItem(new BasicRuleStatement(new OriginateVrf(NODE1, VRF1), new PostInVrf(NODE1, VRF1))));
    assertThat(rules, hasItem(new BasicRuleStatement(new OriginateVrf(NODE1, VRF2), new PostInVrf(NODE1, VRF2))));
    assertThat(rules, hasItem(new BasicRuleStatement(new OriginateVrf(NODE2, VRF1), new PostInVrf(NODE2, VRF1))));
    assertThat(rules, hasItem(new BasicRuleStatement(new OriginateVrf(NODE2, VRF2), new PostInVrf(NODE2, VRF2))));
    // PostInInterfaceCorrespondingVrf
    assertThat(rules, hasItem(new BasicRuleStatement(new PostInInterface(NODE1, INTERFACE1), new PostInVrf(NODE1, VRF1))));
    assertThat(rules, hasItem(new BasicRuleStatement(new PostInInterface(NODE1, INTERFACE2), new PostInVrf(NODE1, VRF1))));
    assertThat(rules, hasItem(new BasicRuleStatement(new PostInInterface(NODE1, INTERFACE3), new PostInVrf(NODE1, VRF2))));
    assertThat(rules, hasItem(new BasicRuleStatement(new PostInInterface(NODE1, INTERFACE4), new PostInVrf(NODE1, VRF2))));
    assertThat(rules, hasItem(new BasicRuleStatement(new PostInInterface(NODE2, INTERFACE1), new PostInVrf(NODE2, VRF1))));
    assertThat(rules, hasItem(new BasicRuleStatement(new PostInInterface(NODE2, INTERFACE2), new PostInVrf(NODE2, VRF1))));
    assertThat(rules, hasItem(new BasicRuleStatement(new PostInInterface(NODE2, INTERFACE3), new PostInVrf(NODE2, VRF2))));
    assertThat(rules, hasItem(new BasicRuleStatement(new PostInInterface(NODE2, INTERFACE4), new PostInVrf(NODE2, VRF2))));
}
Also used : MockSynthesizerInput(org.batfish.z3.MockSynthesizerInput) SynthesizerInput(org.batfish.z3.SynthesizerInput) TransformationRuleStatement(org.batfish.z3.expr.TransformationRuleStatement) RuleStatement(org.batfish.z3.expr.RuleStatement) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement) PostInInterface(org.batfish.z3.state.PostInInterface) OriginateVrf(org.batfish.z3.state.OriginateVrf) PostInVrf(org.batfish.z3.state.PostInVrf) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement) Test(org.junit.Test)

Example 3 with OriginateVrf

use of org.batfish.z3.state.OriginateVrf in project batfish by batfish.

the class NodJobTest method testNatted.

/**
 * Test that traffic originating from 3.0.0.0 is NATed
 */
@Test
public void testNatted() {
    HeaderSpace headerSpace = new HeaderSpace();
    headerSpace.setSrcIps(ImmutableList.of(new IpWildcard("3.0.0.0")));
    NodJob nodJob = getNodJob(headerSpace);
    Context z3Context = new Context();
    SmtInput smtInput = nodJob.computeSmtInput(System.currentTimeMillis(), z3Context);
    Map<OriginateVrf, Map<String, Long>> fieldConstraintsByOriginateVrf = nodJob.getOriginateVrfConstraints(z3Context, smtInput);
    assertThat(fieldConstraintsByOriginateVrf.entrySet(), hasSize(1));
    assertThat(fieldConstraintsByOriginateVrf, hasKey(_originateVrf));
    Map<String, Long> fieldConstraints = fieldConstraintsByOriginateVrf.get(_originateVrf);
    // Only one OriginateVrf choice, so this must be 0
    assertThat(fieldConstraints, hasEntry(OriginateVrfInstrumentation.ORIGINATE_VRF_FIELD_NAME, new Long(0)));
    assertThat(fieldConstraints, hasEntry(BasicHeaderField.ORIG_SRC_IP.getName(), new Ip("3.0.0.0").asLong()));
    assertThat(fieldConstraints, hasEntry(equalTo(BasicHeaderField.SRC_IP.getName()), not(equalTo(new Ip("3.0.0.0").asLong()))));
    assertThat(fieldConstraints, hasEntry(BasicHeaderField.SRC_IP.getName(), new Ip("1.0.0.10").asLong()));
    Set<Flow> flows = nodJob.getFlows(fieldConstraintsByOriginateVrf);
    _bdpDataPlanePlugin.processFlows(flows, _dataPlane);
    List<FlowTrace> flowTraces = _bdpDataPlanePlugin.getHistoryFlowTraces(_dataPlane);
    flowTraces.forEach(trace -> {
        assertThat(trace.getNotes(), is("ACCEPTED"));
        List<FlowTraceHop> hops = trace.getHops();
        assertThat(hops, hasSize(1));
        FlowTraceHop hop = hops.get(0);
        assertThat(hop.getTransformedFlow(), notNullValue());
        assertThat(hop.getTransformedFlow().getSrcIp(), equalTo(new Ip("1.0.0.10")));
    });
}
Also used : Context(com.microsoft.z3.Context) Ip(org.batfish.datamodel.Ip) HeaderSpace(org.batfish.datamodel.HeaderSpace) OriginateVrf(org.batfish.z3.state.OriginateVrf) Flow(org.batfish.datamodel.Flow) IpWildcard(org.batfish.datamodel.IpWildcard) FlowTraceHop(org.batfish.datamodel.FlowTraceHop) FlowTrace(org.batfish.datamodel.FlowTrace) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Test(org.junit.Test)

Example 4 with OriginateVrf

use of org.batfish.z3.state.OriginateVrf in project batfish by batfish.

the class AbstractNodJob method getOriginateVrfConstraints.

/**
 * Try to find a model for each OriginateVrf. If an OriginateVrf does not have an entry in the
 * Map, then the query is unsat when originating from there.
 */
protected Map<OriginateVrf, Map<String, Long>> getOriginateVrfConstraints(Context ctx, SmtInput smtInput) {
    Solver solver = ctx.mkSolver();
    solver.add(smtInput._expr);
    int originateVrfBvSize = _originateVrfInstrumentation.getFieldBits();
    BitVecExpr originateVrfFieldConst = ctx.mkBVConst(OriginateVrfInstrumentation.ORIGINATE_VRF_FIELD_NAME, originateVrfBvSize);
    ImmutableMap.Builder<OriginateVrf, Map<String, Long>> models = ImmutableMap.builder();
    // keep refining until no new models
    while (true) {
        try {
            Map<String, Long> constraints = getSolution(solver, smtInput._variablesAsConsts);
            int originateVrfId = Math.toIntExact(constraints.get(OriginateVrfInstrumentation.ORIGINATE_VRF_FIELD_NAME));
            OriginateVrf originateVrf = _originateVrfInstrumentation.getOriginateVrfs().get(originateVrfId);
            models.put(originateVrf, constraints);
            // refine: different OriginateVrf
            solver.add(ctx.mkNot(ctx.mkEq(originateVrfFieldConst, ctx.mkBV(originateVrfId, originateVrfBvSize))));
        } catch (QueryUnsatException e) {
            break;
        }
    }
    return models.build();
}
Also used : Solver(com.microsoft.z3.Solver) BitVecExpr(com.microsoft.z3.BitVecExpr) OriginateVrf(org.batfish.z3.state.OriginateVrf) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 5 with OriginateVrf

use of org.batfish.z3.state.OriginateVrf in project batfish by batfish.

the class DefaultTransitionGeneratorTest method testVisitOriginate.

@Test
public void testVisitOriginate() {
    SynthesizerInput input = MockSynthesizerInput.builder().setEnabledVrfs(ImmutableMap.of(NODE1, ImmutableSet.of(VRF1, VRF2), NODE2, ImmutableSet.of(VRF1, VRF2))).build();
    Set<RuleStatement> rules = ImmutableSet.copyOf(DefaultTransitionGenerator.generateTransitions(input, ImmutableSet.of(Originate.State.INSTANCE)));
    // ProjectOriginateVrf
    assertThat(rules, hasItem(new BasicRuleStatement(new OriginateVrf(NODE1, VRF1), new Originate(NODE1))));
    assertThat(rules, hasItem(new BasicRuleStatement(new OriginateVrf(NODE1, VRF2), new Originate(NODE1))));
    assertThat(rules, hasItem(new BasicRuleStatement(new OriginateVrf(NODE2, VRF1), new Originate(NODE2))));
    assertThat(rules, hasItem(new BasicRuleStatement(new OriginateVrf(NODE2, VRF2), new Originate(NODE2))));
}
Also used : MockSynthesizerInput(org.batfish.z3.MockSynthesizerInput) SynthesizerInput(org.batfish.z3.SynthesizerInput) TransformationRuleStatement(org.batfish.z3.expr.TransformationRuleStatement) RuleStatement(org.batfish.z3.expr.RuleStatement) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement) OriginateVrf(org.batfish.z3.state.OriginateVrf) Originate(org.batfish.z3.state.Originate) BasicRuleStatement(org.batfish.z3.expr.BasicRuleStatement) Test(org.junit.Test)

Aggregations

OriginateVrf (org.batfish.z3.state.OriginateVrf)10 ImmutableMap (com.google.common.collect.ImmutableMap)6 Map (java.util.Map)6 Test (org.junit.Test)5 Context (com.microsoft.z3.Context)4 Ip (org.batfish.datamodel.Ip)4 IpWildcard (org.batfish.datamodel.IpWildcard)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)3 SortedMap (java.util.SortedMap)3 Flow (org.batfish.datamodel.Flow)3 SynthesizerInput (org.batfish.z3.SynthesizerInput)3 BasicRuleStatement (org.batfish.z3.expr.BasicRuleStatement)3 RuleStatement (org.batfish.z3.expr.RuleStatement)3 TransformationRuleStatement (org.batfish.z3.expr.TransformationRuleStatement)3 Configuration (org.batfish.datamodel.Configuration)2 FlowTrace (org.batfish.datamodel.FlowTrace)2 FlowTraceHop (org.batfish.datamodel.FlowTraceHop)2 HeaderSpace (org.batfish.datamodel.HeaderSpace)2 Interface (org.batfish.datamodel.Interface)2 InterfaceAddress (org.batfish.datamodel.InterfaceAddress)2