use of org.batfish.datamodel.Topology in project batfish by batfish.
the class CommonUtil method synthesizeTopology.
public static Topology synthesizeTopology(Map<String, Configuration> configurations) {
SortedSet<Edge> edges = new TreeSet<>();
Map<Prefix, Set<NodeInterfacePair>> prefixInterfaces = new HashMap<>();
configurations.forEach((nodeName, node) -> {
for (Entry<String, Interface> e : node.getInterfaces().entrySet()) {
String ifaceName = e.getKey();
Interface iface = e.getValue();
if (!iface.isLoopback(node.getConfigurationFormat()) && iface.getActive()) {
for (InterfaceAddress address : iface.getAllAddresses()) {
if (address.getNetworkBits() < Prefix.MAX_PREFIX_LENGTH) {
Prefix prefix = address.getPrefix();
NodeInterfacePair pair = new NodeInterfacePair(nodeName, ifaceName);
Set<NodeInterfacePair> interfaceBucket = prefixInterfaces.computeIfAbsent(prefix, k -> new HashSet<>());
interfaceBucket.add(pair);
}
}
}
}
});
for (Set<NodeInterfacePair> bucket : prefixInterfaces.values()) {
for (NodeInterfacePair p1 : bucket) {
for (NodeInterfacePair p2 : bucket) {
if (!p1.equals(p2)) {
Edge edge = new Edge(p1, p2);
edges.add(edge);
}
}
}
}
return new Topology(edges);
}
use of org.batfish.datamodel.Topology in project batfish by batfish.
the class BdpEngine method computeFixedPoint.
/**
* Attempt to compute the fixed point of the data plane.
*
* @param nodes A dictionary of configuration-wrapping Bdp nodes keyed by name
* @param topology The topology representing physical adjacencies between interface of the nodes
* @param dp The output data plane
* @param externalAdverts Optional external BGP advertisements fed into the data plane computation
* @param ae The output answer element in which to store a report of the computation. Also
* contains the current recovery iteration.
* @param recoveryIterationHashCodes Dependent-route computation iteration hash-code dictionaries,
* themselves keyed by outer recovery iteration.
* @return true iff the computation is oscillating
*/
private boolean computeFixedPoint(SortedMap<String, Node> nodes, Topology topology, BdpDataPlane dp, Set<BgpAdvertisement> externalAdverts, BdpAnswerElement ae, SortedMap<Integer, SortedMap<Integer, Integer>> recoveryIterationHashCodes, SortedMap<Integer, SortedSet<Prefix>> iterationOscillatingPrefixes) {
try (ActiveSpan computeFixedPointSpan = GlobalTracer.get().buildSpan("Computing fixed point").startActive()) {
// avoid unused warning
assert computeFixedPointSpan != null;
SortedSet<Prefix> oscillatingPrefixes = ae.getOscillatingPrefixes();
// BEGIN DONE ONCE (except main rib)
// For each virtual router, setup the initial easy-to-do routes and init protocol-based RIBs:
AtomicInteger initialCompleted = _newBatch.apply("Compute initial connected and static routes, ospf setup, bgp setup", nodes.size());
try (ActiveSpan initRibsBdpSpan = GlobalTracer.get().buildSpan("Initializing easy routes for BDP").startActive()) {
// avoid unused warning
assert initRibsBdpSpan != null;
nodes.values().parallelStream().forEach(n -> {
for (VirtualRouter vr : n._virtualRouters.values()) {
vr.initRibsForBdp(dp.getIpOwners(), externalAdverts);
}
initialCompleted.incrementAndGet();
});
}
// OSPF internal routes
int numOspfInternalIterations;
try (ActiveSpan ospfInternalRoutesSpan = GlobalTracer.get().buildSpan("Initializing OSPF internal routes").startActive()) {
assert ospfInternalRoutesSpan != null;
numOspfInternalIterations = initOspfInternalRoutes(nodes, topology);
}
// RIP internal routes
try (ActiveSpan ripInternalRoutesSpan = GlobalTracer.get().buildSpan("Initializing RIP internal routes").startActive()) {
assert ripInternalRoutesSpan != null;
initRipInternalRoutes(nodes, topology);
}
// Prep for traceroutes
nodes.values().parallelStream().forEach(n -> n._virtualRouters.values().forEach(vr -> {
vr.importRib(vr._mainRib, vr._independentRib);
// Needed for activateStaticRoutes
vr._prevMainRib = vr._mainRib;
vr.activateStaticRoutes();
}));
// Update bgp neighbors with reachability
dp.setNodes(nodes);
computeFibs(nodes);
dp.setTopology(topology);
initRemoteBgpNeighbors(nodes.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, e -> e.getValue().getConfiguration())), dp.getIpOwners(), true, this, dp);
// END DONE ONCE
/*
* Setup maps to track iterations. We need this for oscillation detection.
* Specifically, if we detect that an iteration hashcode (a hash of all the nodes' RIBs)
* has been previously encountered, we go into recovery mode.
* Recovery mode means enabling "lockstep route propagation" for oscillating prefixes.
*
* Lockstep route propagation only allows one of the neighbors to propagate routes for
* oscillating prefixes in a given iteration.
* E.g., lexicographically lower neighbor propagates routes during
* odd iterations, and lex-higher neighbor during even iterations.
*/
Map<Integer, SortedSet<Integer>> iterationsByHashCode = new HashMap<>();
SortedMap<Integer, Integer> iterationHashCodes = new TreeMap<>();
Map<Integer, SortedSet<Route>> iterationRoutes = null;
Map<Integer, SortedMap<String, SortedMap<String, SortedSet<AbstractRoute>>>> iterationAbstractRoutes = null;
if (_settings.getBdpRecordAllIterations()) {
if (_settings.getBdpDetail()) {
iterationAbstractRoutes = new TreeMap<>();
} else {
iterationRoutes = new TreeMap<>();
}
} else if (_maxRecordedIterations > 0) {
if (_settings.getBdpDetail()) {
iterationAbstractRoutes = new LRUMap<>(_maxRecordedIterations);
} else {
iterationRoutes = new LRUMap<>(_maxRecordedIterations);
}
}
AtomicBoolean dependentRoutesChanged = new AtomicBoolean(false);
AtomicBoolean evenDependentRoutesChanged = new AtomicBoolean(false);
AtomicBoolean oddDependentRoutesChanged = new AtomicBoolean(false);
int numDependentRoutesIterations = 0;
// Go into iteration mode, until the routes converge (or oscillation is detected)
do {
numDependentRoutesIterations++;
AtomicBoolean currentChangedMonitor;
if (oscillatingPrefixes.isEmpty()) {
currentChangedMonitor = dependentRoutesChanged;
} else if (numDependentRoutesIterations % 2 == 0) {
currentChangedMonitor = evenDependentRoutesChanged;
} else {
currentChangedMonitor = oddDependentRoutesChanged;
}
currentChangedMonitor.set(false);
computeDependentRoutesIteration(nodes, topology, dp, numDependentRoutesIterations, oscillatingPrefixes);
/* Collect sizes of certain RIBs this iteration */
computeIterationStatistics(nodes, ae, numDependentRoutesIterations);
recordIterationDebugInfo(nodes, dp, iterationRoutes, iterationAbstractRoutes, numDependentRoutesIterations);
// Check to see if hash has changed
AtomicInteger checkFixedPointCompleted = _newBatch.apply("Iteration " + numDependentRoutesIterations + ": Check if fixed-point reached", nodes.size());
// This hashcode uniquely identifies the iteration (i.e., network state)
int iterationHashCode = computeIterationHashCode(nodes);
SortedSet<Integer> iterationsWithThisHashCode = iterationsByHashCode.computeIfAbsent(iterationHashCode, h -> new TreeSet<>());
iterationHashCodes.put(numDependentRoutesIterations, iterationHashCode);
int minNumberOfUnchangedIterationsForConvergence = oscillatingPrefixes.isEmpty() ? 1 : 2;
if (iterationsWithThisHashCode.isEmpty() || (!oscillatingPrefixes.isEmpty() && iterationsWithThisHashCode.equals(Collections.singleton(numDependentRoutesIterations - 1)))) {
iterationsWithThisHashCode.add(numDependentRoutesIterations);
} else if (!iterationsWithThisHashCode.contains(numDependentRoutesIterations - minNumberOfUnchangedIterationsForConvergence)) {
int lowestIterationWithThisHashCode = iterationsWithThisHashCode.first();
int completedOscillationRecoveryAttempts = ae.getCompletedOscillationRecoveryAttempts();
if (!oscillatingPrefixes.isEmpty()) {
completedOscillationRecoveryAttempts++;
ae.setCompletedOscillationRecoveryAttempts(completedOscillationRecoveryAttempts);
}
recoveryIterationHashCodes.put(completedOscillationRecoveryAttempts, iterationHashCodes);
handleOscillation(recoveryIterationHashCodes, iterationRoutes, iterationAbstractRoutes, lowestIterationWithThisHashCode, numDependentRoutesIterations, iterationOscillatingPrefixes, ae);
return true;
}
compareToPreviousIteration(nodes, currentChangedMonitor, checkFixedPointCompleted);
computeFibs(nodes);
initRemoteBgpNeighbors(nodes.entrySet().stream().collect(ImmutableMap.toImmutableMap(Entry::getKey, e -> e.getValue().getConfiguration())), dp.getIpOwners(), true, this, dp);
} while (checkDependentRoutesChanged(dependentRoutesChanged, evenDependentRoutesChanged, oddDependentRoutesChanged, oscillatingPrefixes, numDependentRoutesIterations));
AtomicInteger computeBgpAdvertisementsToOutsideCompleted = _newBatch.apply("Compute BGP advertisements sent to outside", nodes.size());
nodes.values().parallelStream().forEach(n -> {
for (VirtualRouter vr : n._virtualRouters.values()) {
vr.computeBgpAdvertisementsToOutside(dp.getIpOwners());
}
computeBgpAdvertisementsToOutsideCompleted.incrementAndGet();
});
// Set iteration stats in the answer
ae.setOspfInternalIterations(numOspfInternalIterations);
ae.setDependentRoutesIterations(numDependentRoutesIterations);
return false;
}
}
use of org.batfish.datamodel.Topology in project batfish by batfish.
the class Batfish method computeEnvironmentTopology.
Topology computeEnvironmentTopology(Map<String, Configuration> configurations) {
_logger.resetTimer();
Topology topology = computeTestrigTopology(_testrigSettings.getTestRigPath(), configurations);
topology.prune(getEdgeBlacklist(), getNodeBlacklist(), getInterfaceBlacklist());
_logger.printElapsedTime();
return topology;
}
use of org.batfish.datamodel.Topology in project batfish by batfish.
the class Batfish method pathDiff.
@Override
public AnswerElement pathDiff(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();
Topology baseTopology = getEnvironmentTopology();
popEnvironment();
// load diff configurations and generate diff data plane
pushDeltaEnvironment();
Map<String, Configuration> diffConfigurations = loadConfigurations();
Synthesizer diffDataPlaneSynthesizer = synthesizeDataPlane();
Topology diffTopology = getEnvironmentTopology();
popEnvironment();
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> commonEdgeSynthesizers = ImmutableList.of(baseDataPlaneSynthesizer, diffDataPlaneSynthesizer, baseDataPlaneSynthesizer);
List<CompositeNodJob> jobs = new ArrayList<>();
// generate local edge reachability and black hole queries
SortedSet<Edge> diffEdges = diffTopology.getEdges();
for (Edge edge : diffEdges) {
String ingressNode = edge.getNode1();
String outInterface = edge.getInt1();
String vrf = diffConfigurations.get(ingressNode).getInterfaces().get(outInterface).getVrf().getName();
ReachEdgeQuerySynthesizer reachQuery = new ReachEdgeQuerySynthesizer(ingressNode, vrf, edge, true, reachabilitySettings.getHeaderSpace());
ReachEdgeQuerySynthesizer noReachQuery = new ReachEdgeQuerySynthesizer(ingressNode, vrf, edge, true, new HeaderSpace());
noReachQuery.setNegate(true);
List<QuerySynthesizer> queries = ImmutableList.of(reachQuery, noReachQuery, blacklistQuery);
SortedSet<Pair<String, String>> nodes = ImmutableSortedSet.of(new Pair<>(ingressNode, vrf));
CompositeNodJob job = new CompositeNodJob(settings, commonEdgeSynthesizers, queries, nodes, tag);
jobs.add(job);
}
// we also need queries for nodes next to edges that are now missing,
// in the case that those nodes still exist
List<Synthesizer> missingEdgeSynthesizers = ImmutableList.of(baseDataPlaneSynthesizer, baseDataPlaneSynthesizer);
SortedSet<Edge> baseEdges = baseTopology.getEdges();
SortedSet<Edge> missingEdges = ImmutableSortedSet.copyOf(Sets.difference(baseEdges, diffEdges));
for (Edge missingEdge : missingEdges) {
String ingressNode = missingEdge.getNode1();
String outInterface = missingEdge.getInt1();
if (diffConfigurations.containsKey(ingressNode) && diffConfigurations.get(ingressNode).getInterfaces().containsKey(outInterface)) {
String vrf = diffConfigurations.get(ingressNode).getInterfaces().get(outInterface).getVrf().getName();
ReachEdgeQuerySynthesizer reachQuery = new ReachEdgeQuerySynthesizer(ingressNode, vrf, missingEdge, true, reachabilitySettings.getHeaderSpace());
List<QuerySynthesizer> queries = ImmutableList.of(reachQuery, blacklistQuery);
SortedSet<Pair<String, String>> nodes = ImmutableSortedSet.of(new Pair<>(ingressNode, vrf));
CompositeNodJob job = new CompositeNodJob(settings, missingEdgeSynthesizers, queries, nodes, tag);
jobs.add(job);
}
}
// 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.Topology in project batfish by batfish.
the class Batfish method validateEnvironment.
private Answer validateEnvironment() {
Answer answer = new Answer();
ValidateEnvironmentAnswerElement ae = loadValidateEnvironmentAnswerElement();
answer.addAnswerElement(ae);
Topology envTopology = computeEnvironmentTopology(loadConfigurations());
serializeAsJson(_testrigSettings.getEnvironmentSettings().getSerializedTopologyPath(), envTopology, "environment topology");
return answer;
}
Aggregations