use of org.batfish.datamodel.answers.AnswerElement in project batfish by batfish.
the class Batfish method reducedReachability.
@Override
public AnswerElement reducedReachability(ReachabilitySettings reachabilitySettings) {
Settings settings = getSettings();
checkDifferentialDataPlaneQuestionDependencies();
String tag = getDifferentialFlowTag();
// load base configurations and generate base data plane
pushBaseEnvironment();
Map<String, Configuration> baseConfigurations = loadConfigurations();
Synthesizer baseDataPlaneSynthesizer = synthesizeDataPlane();
popEnvironment();
// load diff configurations and generate diff data plane
pushDeltaEnvironment();
Map<String, Configuration> diffConfigurations = loadConfigurations();
Synthesizer diffDataPlaneSynthesizer = synthesizeDataPlane();
popEnvironment();
Set<String> ingressNodes;
try {
ingressNodes = ImmutableSet.copyOf(Sets.intersection(reachabilitySettings.computeActiveIngressNodes(baseConfigurations), reachabilitySettings.computeActiveIngressNodes(diffConfigurations)));
} catch (InvalidReachabilitySettingsException e) {
return e.getInvalidSettingsAnswer();
}
pushDeltaEnvironment();
SortedSet<String> blacklistNodes = getNodeBlacklist();
Set<NodeInterfacePair> blacklistInterfaces = getInterfaceBlacklist();
SortedSet<Edge> blacklistEdges = getEdgeBlacklist();
popEnvironment();
BlacklistDstIpQuerySynthesizer blacklistQuery = new BlacklistDstIpQuerySynthesizer(null, blacklistNodes, blacklistInterfaces, blacklistEdges, baseConfigurations);
// compute composite program and flows
List<Synthesizer> synthesizers = ImmutableList.of(baseDataPlaneSynthesizer, diffDataPlaneSynthesizer, baseDataPlaneSynthesizer);
// generate base reachability and diff blackhole and blacklist queries
List<CompositeNodJob> jobs = ingressNodes.stream().flatMap(node -> baseConfigurations.get(node).getVrfs().keySet().stream().map(vrf -> {
Map<String, Set<String>> ingressNodeVrfs = ImmutableMap.of(node, ImmutableSet.of(vrf));
StandardReachabilityQuerySynthesizer acceptQuery = StandardReachabilityQuerySynthesizer.builder().setActions(ImmutableSet.of(ForwardingAction.ACCEPT, ForwardingAction.NEIGHBOR_UNREACHABLE_OR_EXITS_NETWORK)).setHeaderSpace(reachabilitySettings.getHeaderSpace()).setIngressNodeVrfs(ingressNodeVrfs).setFinalNodes(ImmutableSet.of()).setTransitNodes(ImmutableSet.of()).setNonTransitNodes(ImmutableSet.of()).setSrcNatted(reachabilitySettings.getSrcNatted()).build();
StandardReachabilityQuerySynthesizer notAcceptQuery = StandardReachabilityQuerySynthesizer.builder().setActions(ImmutableSet.of(ForwardingAction.ACCEPT, ForwardingAction.NEIGHBOR_UNREACHABLE_OR_EXITS_NETWORK)).setHeaderSpace(new HeaderSpace()).setIngressNodeVrfs(ingressNodeVrfs).setFinalNodes(ImmutableSet.of()).setTransitNodes(ImmutableSet.of()).setNonTransitNodes(ImmutableSet.of()).build();
notAcceptQuery.setNegate(true);
SortedSet<Pair<String, String>> nodes = ImmutableSortedSet.of(new Pair<>(node, vrf));
List<QuerySynthesizer> queries = ImmutableList.of(acceptQuery, notAcceptQuery, blacklistQuery);
return new CompositeNodJob(settings, synthesizers, queries, nodes, tag);
})).collect(Collectors.toList());
// TODO: maybe do something with nod answer element
Set<Flow> flows = computeCompositeNodOutput(jobs, new NodAnswerElement());
pushBaseEnvironment();
getDataPlanePlugin().processFlows(flows, loadDataPlane());
popEnvironment();
pushDeltaEnvironment();
getDataPlanePlugin().processFlows(flows, loadDataPlane());
popEnvironment();
AnswerElement answerElement = getHistory();
return answerElement;
}
use of org.batfish.datamodel.answers.AnswerElement in project batfish by batfish.
the class Batfish method singleReachability.
private AnswerElement singleReachability(ReachabilitySettings reachabilitySettings, ReachabilityQuerySynthesizer.Builder<?, ?> builder) {
Settings settings = getSettings();
String tag = getFlowTag(_testrigSettings);
Set<ForwardingAction> actions = reachabilitySettings.getActions();
boolean useCompression = reachabilitySettings.getUseCompression();
// specialized compression
/*
CompressDataPlaneResult compressionResult =
useCompression ? computeCompressedDataPlane(headerSpace) : null;
Map<String, Configuration> configurations =
useCompression ? compressionResult._compressedConfigs : loadConfigurations();
DataPlane dataPlane = useCompression ? compressionResult._compressedDataPlane : loadDataPlane();
*/
// general compression
Snapshot snapshot = getSnapshot();
Map<String, Configuration> configurations = useCompression ? loadCompressedConfigurations(snapshot) : loadConfigurations(snapshot);
DataPlane dataPlane = loadDataPlane(useCompression);
if (configurations == null) {
throw new BatfishException("error loading configurations");
}
if (dataPlane == null) {
throw new BatfishException("error loading data plane");
}
Set<String> activeIngressNodes;
Set<String> activeFinalNodes;
HeaderSpace headerSpace;
Set<String> transitNodes;
Set<String> nonTransitNodes;
int maxChunkSize;
try {
activeIngressNodes = reachabilitySettings.computeActiveIngressNodes(configurations);
activeFinalNodes = reachabilitySettings.computeActiveFinalNodes(configurations);
headerSpace = reachabilitySettings.getHeaderSpace();
transitNodes = reachabilitySettings.computeActiveTransitNodes(configurations);
nonTransitNodes = reachabilitySettings.computeActiveNonTransitNodes(configurations);
maxChunkSize = reachabilitySettings.getMaxChunkSize();
reachabilitySettings.validateTransitNodes(configurations);
} catch (InvalidReachabilitySettingsException e) {
return e.getInvalidSettingsAnswer();
}
List<Pair<String, String>> originateNodeVrfs = activeIngressNodes.stream().flatMap(ingressNode -> configurations.get(ingressNode).getVrfs().keySet().stream().map(ingressVrf -> new Pair<>(ingressNode, ingressVrf))).collect(Collectors.toList());
int chunkSize = Math.max(1, Math.min(maxChunkSize, originateNodeVrfs.size() / _settings.getAvailableThreads()));
// partition originateNodeVrfs into chunks
List<List<Pair<String, String>>> originateNodeVrfChunks = Lists.partition(originateNodeVrfs, chunkSize);
Synthesizer dataPlaneSynthesizer = synthesizeDataPlane(configurations, dataPlane, loadForwardingAnalysis(configurations, dataPlane), headerSpace, reachabilitySettings.getSpecialize());
// build query jobs
List<NodJob> jobs = originateNodeVrfChunks.stream().map(ImmutableSortedSet::copyOf).map(nodeVrfs -> {
SortedMap<String, Set<String>> vrfsByNode = new TreeMap<>();
nodeVrfs.forEach(nodeVrf -> {
String node = nodeVrf.getFirst();
String vrf = nodeVrf.getSecond();
vrfsByNode.computeIfAbsent(node, key -> new TreeSet<>());
vrfsByNode.get(node).add(vrf);
});
ReachabilityQuerySynthesizer query = builder.setActions(actions).setHeaderSpace(headerSpace).setFinalNodes(activeFinalNodes).setIngressNodeVrfs(vrfsByNode).setTransitNodes(transitNodes).setNonTransitNodes(nonTransitNodes).setSrcNatted(reachabilitySettings.getSrcNatted()).build();
return new NodJob(settings, dataPlaneSynthesizer, query, nodeVrfs, tag, reachabilitySettings.getSpecialize());
}).collect(Collectors.toList());
// run jobs and get resulting flows
Set<Flow> flows = computeNodOutput(jobs);
getDataPlanePlugin().processFlows(flows, loadDataPlane());
AnswerElement answerElement = getHistory();
return answerElement;
}
use of org.batfish.datamodel.answers.AnswerElement in project batfish by batfish.
the class PropertyChecker method checkProperty.
/*
* General purpose logic for checking a property that holds that
* handles the various flags and parameters for a query with endpoints
*
* q is the question from the user.
* instrument instruments each router in the graph as needed to check the property.
* answer takes the result from Z3 and produces the answer for the user.
*
*/
private AnswerElement checkProperty(HeaderLocationQuestion q, TriFunction<Encoder, Set<String>, Set<GraphEdge>, Map<String, BoolExpr>> instrument, Function<VerifyParam, AnswerElement> answer) {
long totalTime = System.currentTimeMillis();
PathRegexes p = new PathRegexes(q);
Graph graph = new Graph(_batfish);
Set<GraphEdge> destPorts = findFinalInterfaces(graph, p);
List<String> sourceRouters = PatternUtils.findMatchingSourceNodes(graph, p);
if (destPorts.isEmpty()) {
throw new BatfishException("Set of valid destination interfaces is empty");
}
if (sourceRouters.isEmpty()) {
throw new BatfishException("Set of valid ingress nodes is empty");
}
inferDestinationHeaderSpace(graph, destPorts, q);
Set<GraphEdge> failOptions = failLinkSet(graph, q);
Tuple<Stream<Supplier<NetworkSlice>>, Long> ecs = findAllNetworkSlices(q, graph, true);
Stream<Supplier<NetworkSlice>> stream = ecs.getFirst();
Long timeAbstraction = ecs.getSecond();
AnswerElement[] answerElement = new AnswerElement[1];
VerificationResult[] result = new VerificationResult[2];
List<VerificationStats> ecStats = new ArrayList<>();
// Checks ECs in parallel, but short circuits when a counterexample is found
boolean hasCounterExample = stream.anyMatch(lazyEc -> {
long timeEc = System.currentTimeMillis();
NetworkSlice slice = lazyEc.get();
timeEc = System.currentTimeMillis() - timeEc;
synchronized (_lock) {
// Make sure the headerspace is correct
HeaderLocationQuestion question = new HeaderLocationQuestion(q);
question.setHeaderSpace(slice.getHeaderSpace());
// Get the EC graph and mapping
Graph g = slice.getGraph();
Set<String> srcRouters = mapConcreteToAbstract(slice, sourceRouters);
long timeEncoding = System.currentTimeMillis();
Encoder enc = new Encoder(_settings, g, question);
enc.computeEncoding();
timeEncoding = System.currentTimeMillis() - timeEncoding;
// Add environment constraints for base case
if (question.getDiffType() != null) {
if (question.getEnvDiff()) {
addEnvironmentConstraints(enc, question.getDeltaEnvironmentType());
}
} else {
addEnvironmentConstraints(enc, question.getBaseEnvironmentType());
}
Map<String, BoolExpr> prop = instrument.apply(enc, srcRouters, destPorts);
// If this is a equivalence query, we create a second copy of the network
Encoder enc2 = null;
Map<String, BoolExpr> prop2 = null;
if (question.getDiffType() != null) {
HeaderLocationQuestion q2 = new HeaderLocationQuestion(question);
q2.setFailures(0);
long timeDiffEncoding = System.currentTimeMillis();
enc2 = new Encoder(enc, g, q2);
enc2.computeEncoding();
timeDiffEncoding = System.currentTimeMillis() - timeDiffEncoding;
timeEncoding += timeDiffEncoding;
}
if (question.getDiffType() != null) {
assert (enc2 != null);
// create a map for enc2 to lookup a related environment variable from enc
Table2<GraphEdge, EdgeType, SymbolicRoute> relatedEnv = new Table2<>();
enc2.getMainSlice().getLogicalGraph().getEnvironmentVars().forEach((lge, r) -> relatedEnv.put(lge.getEdge(), lge.getEdgeType(), r));
BoolExpr related = enc.mkTrue();
addEnvironmentConstraints(enc2, question.getBaseEnvironmentType());
if (!question.getEnvDiff()) {
related = relateEnvironments(enc, enc2);
}
prop2 = instrument.apply(enc2, srcRouters, destPorts);
// Add diff constraints
BoolExpr required = enc.mkTrue();
for (String source : srcRouters) {
BoolExpr sourceProp1 = prop.get(source);
BoolExpr sourceProp2 = prop2.get(source);
BoolExpr val;
switch(q.getDiffType()) {
case INCREASED:
val = enc.mkImplies(sourceProp1, sourceProp2);
break;
case REDUCED:
val = enc.mkImplies(sourceProp2, sourceProp1);
break;
case ANY:
val = enc.mkEq(sourceProp1, sourceProp2);
break;
default:
throw new BatfishException("Missing case: " + q.getDiffType());
}
required = enc.mkAnd(required, val);
}
related = enc.mkAnd(related, relatePackets(enc, enc2));
enc.add(related);
enc.add(enc.mkNot(required));
} else {
// Not a differential query; just a query on a single version of the network.
BoolExpr allProp = enc.mkTrue();
for (String router : srcRouters) {
BoolExpr r = prop.get(router);
if (q.getNegate()) {
r = enc.mkNot(r);
}
allProp = enc.mkAnd(allProp, r);
}
enc.add(enc.mkNot(allProp));
}
addFailureConstraints(enc, destPorts, failOptions);
Tuple<VerificationResult, Model> tup = enc.verify();
VerificationResult res = tup.getFirst();
Model model = tup.getSecond();
if (q.getBenchmark()) {
VerificationStats stats = res.getStats();
stats.setAvgComputeEcTime(timeEc);
stats.setMaxComputeEcTime(timeEc);
stats.setMinComputeEcTime(timeEc);
stats.setAvgEncodingTime(timeEncoding);
stats.setMaxEncodingTime(timeEncoding);
stats.setMinEncodingTime(timeEncoding);
stats.setTimeCreateBdds((double) timeAbstraction);
synchronized (_lock) {
ecStats.add(stats);
}
}
if (!res.isVerified()) {
VerifyParam vp = new VerifyParam(res, model, srcRouters, enc, enc2, prop, prop2);
AnswerElement ae = answer.apply(vp);
synchronized (_lock) {
answerElement[0] = ae;
result[0] = res;
}
return true;
}
synchronized (_lock) {
result[1] = res;
}
return false;
}
});
totalTime = (System.currentTimeMillis() - totalTime);
VerificationResult res;
AnswerElement ae;
if (hasCounterExample) {
res = result[0];
ae = answerElement[0];
} else {
res = result[1];
VerifyParam vp = new VerifyParam(res, null, null, null, null, null, null);
ae = answer.apply(vp);
}
if (q.getBenchmark()) {
VerificationStats stats = VerificationStats.combineAll(ecStats, totalTime);
res.setStats(stats);
}
return ae;
}
use of org.batfish.datamodel.answers.AnswerElement in project batfish by batfish.
the class Answerer method answerDiff.
// this is the default differential answerer
// if you want a custom one for a subclass, override this function in the
// subclass
public AnswerElement answerDiff() {
_batfish.pushBaseEnvironment();
_batfish.checkEnvironmentExists();
_batfish.popEnvironment();
_batfish.pushDeltaEnvironment();
_batfish.checkEnvironmentExists();
_batfish.popEnvironment();
_batfish.pushBaseEnvironment();
AnswerElement before = create(_question, _batfish).answer();
_batfish.popEnvironment();
_batfish.pushDeltaEnvironment();
AnswerElement after = create(_question, _batfish).answer();
_batfish.popEnvironment();
try {
String beforeJsonStr = BatfishObjectMapper.writePrettyString(before);
String afterJsonStr = BatfishObjectMapper.writePrettyString(after);
JSONObject beforeJson = new JSONObject(beforeJsonStr);
JSONObject afterJson = new JSONObject(afterJsonStr);
JsonDiff diff = new JsonDiff(beforeJson, afterJson);
return new JsonDiffAnswerElement(diff);
} catch (JsonProcessingException | JSONException e) {
throw new BatfishException("Could not convert diff element to json string", e);
}
}
Aggregations