use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class PropertyChecker method checkRoutingLoop.
/*
* Checks for routing loops in the network. For efficiency reasons,
* we only check for loops with routers that use static routes since
* these can override the usual loop-prevention mechanisms.
*/
public AnswerElement checkRoutingLoop(HeaderQuestion q) {
Graph graph = new Graph(_batfish);
// Collect all relevant destinations
List<Prefix> prefixes = new ArrayList<>();
graph.getStaticRoutes().forEach((router, ifaceName, srs) -> {
for (StaticRoute sr : srs) {
prefixes.add(sr.getNetwork());
}
});
SortedSet<IpWildcard> pfxs = new TreeSet<>();
for (Prefix prefix : prefixes) {
pfxs.add(new IpWildcard(prefix));
}
q.getHeaderSpace().setDstIps(pfxs);
// Collect all routers that use static routes as a
// potential node along a loop
List<String> routers = new ArrayList<>();
for (Entry<String, Configuration> entry : graph.getConfigurations().entrySet()) {
String router = entry.getKey();
Configuration conf = entry.getValue();
if (conf.getDefaultVrf().getStaticRoutes().size() > 0) {
routers.add(router);
}
}
Encoder enc = new Encoder(_settings, graph, q);
enc.computeEncoding();
Context ctx = enc.getCtx();
EncoderSlice slice = enc.getMainSlice();
PropertyAdder pa = new PropertyAdder(slice);
BoolExpr someLoop = ctx.mkBool(false);
for (String router : routers) {
BoolExpr hasLoop = pa.instrumentLoop(router);
someLoop = ctx.mkOr(someLoop, hasLoop);
}
enc.add(someLoop);
VerificationResult result = enc.verify().getFirst();
return new SmtOneAnswerElement(result);
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class SynthesizerInputImpl method computeIpsByHostname.
private Map<String, Set<Ip>> computeIpsByHostname() {
Map<String, Map<String, Interface>> enabledInterfaces = toImmutableMap(_enabledInterfaces, Entry::getKey, enabledInterfacesEntry -> {
Configuration c = _configurations.get(enabledInterfacesEntry.getKey());
return toImmutableMap(enabledInterfacesEntry.getValue(), Function.identity(), c.getInterfaces()::get);
});
Map<Ip, Set<String>> ipOwners = computeIpOwners(true, enabledInterfaces);
Map<String, Set<Ip>> map = new HashMap<>();
/*
* ipOwners may not contain all nodes (i.e. a node may not own any IPs),
* so first initialize to make sure there is an entry for each node.
*/
_enabledInterfaces.keySet().forEach(node -> map.put(node, new HashSet<>()));
ipOwners.forEach((ip, owners) -> {
for (String owner : owners) {
map.get(owner).add(ip);
}
});
// freeze
return toImmutableMap(map, Entry::getKey, e -> ImmutableSet.copyOf(e.getValue()));
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class SynthesizerInputImpl method computeEnabledInterfacesByNodeVrf.
private Map<String, Map<String, Set<String>>> computeEnabledInterfacesByNodeVrf() {
return toImmutableMap(_enabledVrfs, Entry::getKey, enabledVrfsEntry -> {
String hostname = enabledVrfsEntry.getKey();
Set<String> disabledInterfaces = _disabledInterfaces.get(hostname);
Configuration c = _configurations.get(hostname);
return toImmutableMap(enabledVrfsEntry.getValue(), Function.identity(), vrfName -> c.getVrfs().get(vrfName).getInterfaces().entrySet().stream().filter(interfaceEntry -> disabledInterfaces == null || !disabledInterfaces.contains(interfaceEntry.getKey())).filter(interfaceEntry -> interfaceEntry.getValue().getActive()).filter(interfaceEntry -> !interfaceEntry.getValue().getBlacklisted()).map(Entry::getKey).collect(ImmutableSet.toImmutableSet()));
});
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class IpsecVpnStatusAnswerer method answer.
@Override
public AnswerElement answer() {
IpsecVpnStatusQuestion question = (IpsecVpnStatusQuestion) _question;
Map<String, Configuration> configurations = _batfish.loadConfigurations();
Set<String> includeNodes1 = question.getNode1Regex().getMatchingNodes(configurations);
Set<String> includeNodes2 = question.getNode2Regex().getMatchingNodes(configurations);
CommonUtil.initRemoteIpsecVpns(configurations);
IpsecVpnStatusAnswerElement answerElement = new IpsecVpnStatusAnswerElement();
for (Configuration c : configurations.values()) {
if (!includeNodes1.contains(c.getHostname())) {
continue;
}
for (IpsecVpn ipsecVpn : c.getIpsecVpns().values()) {
IpsecVpnInfo vpnInfo = analyzeIpsecVpn(ipsecVpn);
if ((vpnInfo.getRemoteEndpoint() == null || includeNodes2.contains(vpnInfo.getRemoteEndpoint().getHostname())) && vpnInfo.getProblems().stream().anyMatch(v -> question.matchesProblem(v))) {
answerElement.getIpsecVpns().add(vpnInfo);
}
}
}
return answerElement;
}
use of org.batfish.datamodel.Configuration in project batfish by batfish.
the class BdpDataPlanePluginTest method testBgpTieBreaker.
@Test
public void testBgpTieBreaker() {
String hostname = "r1";
Configuration c = BatfishTestUtils.createTestConfiguration(hostname, ConfigurationFormat.CISCO_IOS);
BgpProcess proc = new BgpProcess();
c.getVrfs().computeIfAbsent(DEFAULT_VRF_NAME, Vrf::new).setBgpProcess(proc);
Map<String, Node> nodes = new HashMap<String, Node>();
Node node = new Node(c);
nodes.put(hostname, node);
VirtualRouter vr = new VirtualRouter(DEFAULT_VRF_NAME, c);
// good for both ebgp and ibgp
BgpMultipathRib bmr = new BgpMultipathRib(vr);
// ebgp
BgpBestPathRib ebgpOldBbr = BgpBestPathRib.initial(vr);
BgpBestPathRib ebgpNewBbr = new BgpBestPathRib(vr, ebgpOldBbr, false);
BgpRoute.Builder ebgpBuilder = new BgpRoute.Builder().setNetwork(Prefix.ZERO).setOriginType(OriginType.INCOMPLETE).setOriginatorIp(Ip.ZERO).setProtocol(RoutingProtocol.BGP).setReceivedFromIp(Ip.ZERO);
BgpRoute ebgpOlderHigherOriginator = ebgpBuilder.setOriginatorIp(Ip.MAX).build();
BgpRoute ebgpNewerHigherOriginator = ebgpBuilder.setOriginatorIp(Ip.MAX).build();
BgpRoute ebgpLowerOriginator = ebgpBuilder.setOriginatorIp(Ip.ZERO).build();
// ibgp
BgpBestPathRib ibgpOldBbr = BgpBestPathRib.initial(vr);
BgpBestPathRib ibgpNewBbr = new BgpBestPathRib(vr, ibgpOldBbr, false);
BgpRoute.Builder ibgpBuilder = new BgpRoute.Builder().setNetwork(Prefix.ZERO).setOriginType(OriginType.INCOMPLETE).setOriginatorIp(Ip.ZERO).setProtocol(RoutingProtocol.IBGP).setReceivedFromIp(Ip.ZERO);
BgpRoute ibgpOlderHigherOriginator = ibgpBuilder.setOriginatorIp(Ip.MAX).build();
BgpRoute ibgpNewerHigherOriginator = ibgpBuilder.setOriginatorIp(Ip.MAX).build();
BgpRoute ibgpLowerOriginator = ibgpBuilder.setOriginatorIp(Ip.ZERO).build();
ebgpOldBbr.mergeRoute(ebgpOlderHigherOriginator);
ibgpOldBbr.mergeRoute(ibgpOlderHigherOriginator);
/*
* Given default tie-breaking, and all more important attributes being equivalent:
* - When comparing two eBGP adverts, best-path rib prefers older advert.
* - If neither is older, or one is iBGP, best-path rib prefers advert with higher router-id.
* - Multipath RIB ignores both age and router-id, seeing both adverts as equal.
*/
assertThat(ebgpOldBbr.comparePreference(ebgpNewerHigherOriginator, ebgpLowerOriginator), lessThan(0));
assertThat(ebgpNewBbr.comparePreference(ebgpNewerHigherOriginator, ebgpLowerOriginator), greaterThan(0));
assertThat(bmr.comparePreference(ebgpNewerHigherOriginator, ebgpLowerOriginator), equalTo(0));
assertThat(ibgpOldBbr.comparePreference(ibgpNewerHigherOriginator, ibgpLowerOriginator), lessThan(0));
assertThat(ibgpNewBbr.comparePreference(ibgpNewerHigherOriginator, ibgpLowerOriginator), lessThan(0));
assertThat(bmr.comparePreference(ibgpNewerHigherOriginator, ibgpLowerOriginator), equalTo(0));
}
Aggregations