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));
}
}
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))));
}
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")));
});
}
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();
}
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))));
}
Aggregations