use of org.batfish.datamodel.BgpPeerConfigId in project batfish by batfish.
the class BgpTopologyUtils method addActivePeerEdges.
private static void addActivePeerEdges(BgpPeerConfigId neighborId, MutableValueGraph<BgpPeerConfigId, BgpSessionProperties> graph, NetworkConfigurations nc, Map<Ip, Map<String, Set<String>>> ipOwners, Map<String, Multimap<String, BgpPeerConfigId>> receivers, Set<Ip> potentialLocalIps, boolean checkReachability, TracerouteEngine tracerouteEngine) {
BgpActivePeerConfig neighbor = nc.getBgpPointToPointPeerConfig(neighborId);
if (neighbor == null || potentialLocalIps.isEmpty() || neighbor.getLocalAs() == null || neighbor.getPeerAddress() == null || neighbor.getRemoteAsns().isEmpty()) {
return;
}
// Find nodes that own the neighbor's peer address
Map<String, Set<String>> possibleVrfs = ipOwners.get(neighbor.getPeerAddress());
if (possibleVrfs == null) {
return;
}
Set<BgpPeerConfigId> alreadyEstablished = graph.adjacentNodes(neighborId);
for (Entry<String, Set<String>> entry : possibleVrfs.entrySet()) {
String node = entry.getKey();
Set<String> vrfs = entry.getValue();
Multimap<String, BgpPeerConfigId> receiversByVrf = receivers.get(node);
if (receiversByVrf == null) {
continue;
}
for (String vrf : vrfs) {
receiversByVrf.get(vrf).stream().filter(candidateId -> !alreadyEstablished.contains(candidateId)).forEach(candidateId -> {
// Ensure candidate has compatible local/remote AS, isn't in same vrf as initiator
BgpPeerConfig candidate = nc.getBgpPeerConfig(candidateId);
if (!bgpCandidatePassesSanityChecks(neighborId, neighbor, candidateId, candidate)) {
return;
}
// Check if neighbor has any feasible local IPs compatible with this candidate
Set<Ip> feasibleLocalIpsForPeeringWithCandidate = getFeasibleLocalIps(potentialLocalIps, candidate);
if (feasibleLocalIpsForPeeringWithCandidate.isEmpty()) {
return;
}
if (!checkReachability) {
feasibleLocalIpsForPeeringWithCandidate.forEach(ip -> addEdges(neighbor, neighborId, ip, candidateId, graph, nc));
} else {
initiateBgpSessions(neighborId, candidateId, neighbor, feasibleLocalIpsForPeeringWithCandidate, tracerouteEngine).stream().filter(BgpSessionInitiationResult::isSuccessful).map(initiationResult -> initiationResult.getFlow().getSrcIp()).forEach(srcIp -> addEdges(neighbor, neighborId, srcIp, candidateId, graph, nc));
}
});
}
}
}
use of org.batfish.datamodel.BgpPeerConfigId in project batfish by batfish.
the class BgpTopologyUtils method addUnnumberedPeerEdges.
private static void addUnnumberedPeerEdges(BgpPeerConfigId neighborId, MutableValueGraph<BgpPeerConfigId, BgpSessionProperties> graph, NetworkConfigurations nc, L3Adjacencies l3Adjacencies) {
// neighbor will be null if neighborId has no peer interface defined
BgpUnnumberedPeerConfig neighbor = nc.getBgpUnnumberedPeerConfig(neighborId);
if (neighbor == null || neighbor.getLocalAs() == null || neighbor.getRemoteAsns().isEmpty()) {
return;
}
Set<BgpPeerConfigId> alreadyEstablished = graph.adjacentNodes(neighborId);
String hostname = neighborId.getHostname();
NodeInterfacePair peerNip = NodeInterfacePair.of(hostname, neighborId.getPeerInterface());
graph.nodes().stream().filter(candidateId -> !alreadyEstablished.contains(candidateId) && // Ensure candidate is unnumbered and has compatible local/remote AS
bgpCandidatePassesSanityChecks(neighborId, neighbor, candidateId, nc) && // Check layer 2 connectivity
l3Adjacencies.inSamePointToPointDomain(peerNip, NodeInterfacePair.of(candidateId.getHostname(), candidateId.getPeerInterface()))).forEach(remoteId -> addEdges(neighbor, neighborId, neighbor.getLocalIp(), remoteId, graph, nc));
}
use of org.batfish.datamodel.BgpPeerConfigId in project batfish by batfish.
the class BgpTopologyUtils method initBgpTopology.
/**
* Compute the BGP topology -- a network of {@link BgpPeerConfigId}s connected by {@link
* BgpSessionProperties}.
*
* @param configurations node configurations, keyed by hostname
* @param ipVrfOwners network Ip owners (see {@link IpOwners#computeIpNodeOwners(Map, boolean)}
* for reference)
* @param keepInvalid whether to keep improperly configured neighbors. If performing configuration
* checks, you probably want this set to {@code true}, otherwise (e.g., computing dataplane)
* you want this to be {@code false}.
* @param checkReachability whether to perform dataplane-level checks to ensure that neighbors are
* reachable and sessions can be established correctly. <b>Note:</b> this is different from
* {@code keepInvalid=false}, which only does filters invalid neighbors at the control-plane
* level
* @param tracerouteEngine an instance of {@link TracerouteEngine} for doing reachability checks.
* @param l3Adjacencies {@link L3Adjacencies} of the network, for checking BGP unnumbered
* reachability.
* @return A graph ({@link Network}) representing all BGP peerings.
*/
@Nonnull
public static BgpTopology initBgpTopology(Map<String, Configuration> configurations, Map<Ip, Map<String, Set<String>>> ipVrfOwners, boolean keepInvalid, boolean checkReachability, @Nullable TracerouteEngine tracerouteEngine, Map<String, Map<String, Fib>> fibs, L3Adjacencies l3Adjacencies) {
checkArgument(!checkReachability || !keepInvalid, "Cannot check reachability while keeping invalid peers");
checkArgument(!checkReachability || tracerouteEngine != null, "Cannot check reachability without a traceroute engine");
// TODO: handle duplicate ips on different vrfs
NetworkConfigurations networkConfigurations = NetworkConfigurations.of(configurations);
/*
* First pass: identify all addresses "owned" by BgpNeighbors, add neighbor ids as vertices to
* the graph; dynamically determine local IPs as needed
*/
MutableValueGraph<BgpPeerConfigId, BgpSessionProperties> graph = ValueGraphBuilder.directed().allowsSelfLoops(false).build();
/*
* Multimap of active peers' BgpPeerConfigIds to all IPs that each peer may use as local IP
* when initiating a session. For a peer with an explicitly configured local IP, that IP is
* the only value associated with the peer in this map. Otherwise:
* - If FIBs are provided, the map contains all local IPs with which the peer may initiate,
* as inferred by getPotentialSrcIps().
* - Else no IPs are associated with the peer.
*/
ImmutableSetMultimap.Builder<BgpPeerConfigId, Ip> localIpsBuilder = ImmutableSetMultimap.builder();
for (Configuration node : configurations.values()) {
String hostname = node.getHostname();
for (Vrf vrf : node.getVrfs().values()) {
String vrfName = vrf.getName();
BgpProcess proc = vrf.getBgpProcess();
if (proc == null) {
// nothing to do if no bgp process on this VRF
continue;
}
Fib fib = fibs.getOrDefault(hostname, ImmutableMap.of()).get(vrfName);
for (Entry<Ip, BgpActivePeerConfig> e : proc.getActiveNeighbors().entrySet()) {
Ip peerAddress = e.getKey();
BgpActivePeerConfig config = e.getValue();
if (!keepInvalid && !bgpConfigPassesSanityChecks(config, hostname, vrfName, ipVrfOwners)) {
continue;
}
BgpPeerConfigId neighborId = new BgpPeerConfigId(hostname, vrfName, peerAddress.toPrefix(), false);
graph.addNode(neighborId);
if (config.getLocalIp() != null) {
localIpsBuilder.put(neighborId, config.getLocalIp());
} else if (fib != null) {
// No explicitly configured local IP. Check for dynamically resolvable local IPs.
localIpsBuilder.putAll(neighborId, getPotentialSrcIps(peerAddress, fib, node));
}
}
// Dynamic peers: map of prefix to BgpPassivePeerConfig
proc.getPassiveNeighbors().entrySet().stream().filter(entry -> keepInvalid || bgpConfigPassesSanityChecks(entry.getValue(), hostname, vrfName, ipVrfOwners)).forEach(entry -> graph.addNode(new BgpPeerConfigId(hostname, vrfName, entry.getKey(), true)));
// Unnumbered BGP peers: map of interface name to BgpUnnumberedPeerConfig
proc.getInterfaceNeighbors().entrySet().stream().filter(e -> keepInvalid || bgpConfigPassesSanityChecks(e.getValue(), hostname, vrfName, ipVrfOwners)).forEach(e -> graph.addNode(new BgpPeerConfigId(hostname, vrf.getName(), e.getKey())));
}
}
// Second pass: add edges to the graph. Note, these are directed edges.
Map<String, Multimap<String, BgpPeerConfigId>> receivers = new HashMap<>();
for (BgpPeerConfigId peer : graph.nodes()) {
if (peer.getType() == BgpPeerConfigType.UNNUMBERED) {
// Unnumbered configs only form sessions with each other
continue;
}
Multimap<String, BgpPeerConfigId> vrf = receivers.computeIfAbsent(peer.getHostname(), name -> LinkedListMultimap.create());
vrf.put(peer.getVrfName(), peer);
}
SetMultimap<BgpPeerConfigId, Ip> localIps = localIpsBuilder.build();
for (BgpPeerConfigId neighborId : graph.nodes()) {
switch(neighborId.getType()) {
case DYNAMIC:
// Passive end of the peering cannot initiate a connection
continue;
case ACTIVE:
addActivePeerEdges(neighborId, graph, networkConfigurations, ipVrfOwners, receivers, localIps.get(neighborId), checkReachability, tracerouteEngine);
break;
case UNNUMBERED:
addUnnumberedPeerEdges(neighborId, graph, networkConfigurations, l3Adjacencies);
break;
default:
throw new IllegalArgumentException(String.format("Unrecognized peer type: %s", neighborId));
}
}
return new BgpTopology(graph);
}
use of org.batfish.datamodel.BgpPeerConfigId in project batfish by batfish.
the class BgpTopologyTest method nonTrivialTopology.
@Nonnull
private static BgpTopology nonTrivialTopology() {
MutableValueGraph<BgpPeerConfigId, BgpSessionProperties> graph = ValueGraphBuilder.directed().allowsSelfLoops(false).build();
graph.putEdgeValue(new BgpPeerConfigId("a", "b", "c"), new BgpPeerConfigId("d", "e", "f"), BgpSessionProperties.builder().setRemoteAs(1L).setLocalAs(2L).setRemoteIp(Ip.FIRST_CLASS_A_PRIVATE_IP).setLocalIp(Ip.FIRST_CLASS_B_PRIVATE_IP).setSessionType(SessionType.EBGP_SINGLEHOP).build());
return new BgpTopology(graph);
}
use of org.batfish.datamodel.BgpPeerConfigId in project batfish by batfish.
the class BgpTopologyUtilsTest method testInitTopologyRemotePrefixNotMatchingLocalIp.
@Test
public void testInitTopologyRemotePrefixNotMatchingLocalIp() {
// Peer 1 on node1 with IP 1.1.1.1 is active, set up to peer with 2.2.2.2
// Peer 2 on node2 with IP 2.2.2.2 is passive, with remote prefix 1.1.1.0/24
// Should see one session come up in BGP topology: peer 1 to peer 2
Ip ip1 = Ip.parse("1.1.1.1");
Ip ip2 = Ip.parse("2.2.2.2");
BgpActivePeerConfig peer1 = BgpActivePeerConfig.builder().setLocalIp(ip1).setLocalAs(1L).setPeerAddress(ip2).setRemoteAs(2L).setIpv4UnicastAddressFamily(Ipv4UnicastAddressFamily.builder().setAddressFamilyCapabilities(AddressFamilyCapabilities.builder().build()).build()).build();
_node1BgpProcess.setNeighbors(ImmutableSortedMap.of(ip2, peer1));
Prefix peer2PeerPrefix = Prefix.create(ip1, 24);
BgpPassivePeerConfig peer2 = BgpPassivePeerConfig.builder().setLocalIp(Ip.AUTO).setLocalAs(2L).setRemoteAs(1L).setPeerPrefix(peer2PeerPrefix).setIpv4UnicastAddressFamily(Ipv4UnicastAddressFamily.builder().setAddressFamilyCapabilities(AddressFamilyCapabilities.builder().build()).build()).build();
_node2BgpProcess.setPassiveNeighbors(ImmutableSortedMap.of(peer2PeerPrefix, peer2));
Map<Ip, Map<String, Set<String>>> ipOwners = ImmutableMap.of(ip1, ImmutableMap.of(NODE1, ImmutableSet.of(DEFAULT_VRF_NAME)), ip2, ImmutableMap.of(NODE2, ImmutableSet.of(DEFAULT_VRF_NAME)));
ValueGraph<BgpPeerConfigId, BgpSessionProperties> bgpTopology = initBgpTopology(_configs, ipOwners, true, null).getGraph();
assertThat(bgpTopology.edges(), hasSize(2));
EndpointPair<BgpPeerConfigId> edge = bgpTopology.edges().iterator().next();
assertThat(edge.source().getHostname(), equalTo(NODE1));
assertThat(edge.target().getHostname(), equalTo(NODE2));
}
Aggregations