use of org.batfish.datamodel.BgpPeerConfig 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.BgpPeerConfig in project batfish by batfish.
the class BgpTopologyUtils method addEdges.
/**
* Adds edges in {@code graph} between the given {@link BgpPeerConfigId}s in both directions.
*/
private static void addEdges(BgpPeerConfig p1, BgpPeerConfigId id1, Ip p1LocalIp, BgpPeerConfigId id2, MutableValueGraph<BgpPeerConfigId, BgpSessionProperties> graph, NetworkConfigurations networkConfigurations) {
BgpPeerConfig remotePeer = Objects.requireNonNull(networkConfigurations.getBgpPeerConfig(id2));
AsPair asPair = computeAsPair(p1.getLocalAs(), p1.getConfederationAsn(), p1.getRemoteAsns(), remotePeer.getLocalAs(), remotePeer.getConfederationAsn(), remotePeer.getRemoteAsns());
assert asPair != null;
graph.putEdgeValue(id1, id2, BgpSessionProperties.from(p1, p1LocalIp, remotePeer, false, asPair.getLocalAs(), asPair.getRemoteAs(), asPair.getConfedSessionType()));
graph.putEdgeValue(id2, id1, BgpSessionProperties.from(p1, p1LocalIp, remotePeer, true, asPair.getLocalAs(), asPair.getRemoteAs(), asPair.getConfedSessionType()));
}
use of org.batfish.datamodel.BgpPeerConfig in project batfish by batfish.
the class Graph method findExportRoutingPolicy.
/*
* Find the export routing policy for a given edge
*/
@Nullable
public RoutingPolicy findExportRoutingPolicy(String router, Protocol proto, GraphEdge ge) {
Configuration conf = _configurations.get(router);
if (proto.isConnected()) {
return null;
}
if (proto.isStatic()) {
return null;
}
if (proto.isOspf()) {
OspfProcess p = getFirstOspfProcess(conf.getDefaultVrf());
if (p == null) {
return null;
}
String exp = p.getExportPolicy();
return conf.getRoutingPolicies().get(exp);
}
if (proto.isBgp()) {
BgpPeerConfig n = findBgpNeighbor(ge);
// if no neighbor (e.g., loopback) or no export policy, return null
return Optional.ofNullable(n).map(BgpPeerConfig::getIpv4UnicastAddressFamily).map(AddressFamily::getExportPolicy).map(policy -> conf.getRoutingPolicies().get(policy)).orElse(null);
}
throw new BatfishException("TODO: findExportRoutingPolicy for " + proto.name());
}
use of org.batfish.datamodel.BgpPeerConfig in project batfish by batfish.
the class BgpSessionCompatibilityAnswererTest method createConfigurations.
/**
* Given equally sized lists of {@link BgpPeerConfigId}s and {@link BgpPeerConfig}s, creates one
* {@link Configuration} per ID/peer pair and returns the resulting configurations.
*/
static SortedMap<String, Configuration> createConfigurations(List<BgpPeerConfigId> ids, List<BgpPeerConfig> peers) {
assert ids.size() == peers.size();
SortedMap<String, Configuration> configs = new TreeMap<>();
NetworkFactory nf = new NetworkFactory();
for (int i = 0; i < ids.size(); i++) {
BgpPeerConfigId id = ids.get(i);
BgpPeerConfig peer = peers.get(i);
// Create a configuration with a BgpProcess
Configuration c = nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CISCO_IOS).setHostname(id.getHostname()).build();
Vrf vrf = nf.vrfBuilder().setOwner(c).setName(id.getVrfName()).build();
BgpProcess bgpProc = BgpProcess.testBgpProcess(Ip.ZERO);
vrf.setBgpProcess(bgpProc);
configs.put(c.getHostname(), c);
// Add interface to make IpOwners accurate
if (peer.getLocalIp() != null && peer.getLocalIp() != Ip.AUTO) {
nf.interfaceBuilder().setOwner(c).setVrf(vrf).setAddress(ConcreteInterfaceAddress.create(peer.getLocalIp(), 30)).build();
}
// Add peer in the appropriate map in the BgpProcess
if (id.getType() == BgpPeerConfigType.ACTIVE) {
assert id.getRemotePeerPrefix().getPrefixLength() == Prefix.MAX_PREFIX_LENGTH;
Ip remotePeerAddress = id.getRemotePeerPrefix().getStartIp();
bgpProc.setNeighbors(ImmutableSortedMap.of(remotePeerAddress, (BgpActivePeerConfig) peer));
} else if (id.getType() == BgpPeerConfigType.DYNAMIC) {
bgpProc.setPassiveNeighbors(ImmutableSortedMap.of(id.getRemotePeerPrefix(), (BgpPassivePeerConfig) peer));
} else if (id.getType() == BgpPeerConfigType.UNNUMBERED) {
bgpProc.setInterfaceNeighbors(ImmutableSortedMap.of(id.getPeerInterface(), (BgpUnnumberedPeerConfig) peer));
} else {
throw new BatfishException(String.format("Unhandled peer type %s", id.getType()));
}
}
return configs;
}
use of org.batfish.datamodel.BgpPeerConfig in project batfish by batfish.
the class CiscoNxosGrammarTest method testEvpnL2L3Vni.
@Test
public void testEvpnL2L3Vni() throws IOException {
Configuration c = parseConfig("nxos_l2_l3_vnis");
String tenantVrfName = "tenant1";
Ip routerId = Ip.parse("10.1.1.1");
int tenantVrfPosition = 3;
// All defined VXLAN Vnis
ImmutableSortedSet<Layer2VniConfig> expectedL2Vnis = ImmutableSortedSet.of(Layer2VniConfig.builder().setVni(1111).setVrf(DEFAULT_VRF_NAME).setRouteDistinguisher(RouteDistinguisher.from(routerId, 32768)).setRouteTarget(ExtendedCommunity.target(1, 1111)).setImportRouteTarget(ExtendedCommunity.target(1, 1111).matchString()).build(), Layer2VniConfig.builder().setVni(2222).setVrf(DEFAULT_VRF_NAME).setRouteDistinguisher(RouteDistinguisher.from(routerId, 32769)).setRouteTarget(ExtendedCommunity.target(1, 2222)).setImportRouteTarget(ExtendedCommunity.target(1, 2222).matchString()).build());
ImmutableSortedSet<Layer3VniConfig> expectedL3Vnis = ImmutableSortedSet.of(Layer3VniConfig.builder().setVni(3333).setVrf(tenantVrfName).setAdvertiseV4Unicast(true).setRouteDistinguisher(RouteDistinguisher.from(routerId, tenantVrfPosition)).setRouteTarget(ExtendedCommunity.target(1, 3333)).setImportRouteTarget(ExtendedCommunity.target(1, 3333).matchString()).build());
BgpPeerConfig peer = c.getDefaultVrf().getBgpProcess().getActiveNeighbors().get(Ip.parse("1.1.1.1"));
assertThat(peer.getEvpnAddressFamily(), notNullValue());
assertThat(peer.getEvpnAddressFamily().getL2VNIs(), equalTo(expectedL2Vnis));
assertThat(peer.getEvpnAddressFamily().getL3VNIs(), equalTo(expectedL3Vnis));
assertThat(peer.getEvpnAddressFamily().getNveIp(), equalTo(Ip.parse("1.1.1.1")));
assertThat(c.getVrfs().get(tenantVrfName).getBgpProcess(), notNullValue());
// check leak configs
{
// bgpv4 -> evpn
VrfLeakConfig leak = c.getDefaultVrf().getVrfLeakConfig();
assertNotNull(leak);
assertTrue(leak.getLeakAsBgp());
assertThat(leak.getBgpv4ToEvpnVrfLeakConfigs(), contains(Bgpv4ToEvpnVrfLeakConfig.builder().setAttachRouteTargets(ExtendedCommunity.target(1, 3333)).setImportFromVrf(tenantVrfName).setSrcVrfRouteDistinguisher(RouteDistinguisher.from(routerId, tenantVrfPosition)).build()));
}
{
// evpn -> bgpv4
VrfLeakConfig leak = c.getVrfs().get(tenantVrfName).getVrfLeakConfig();
assertNotNull(leak);
assertTrue(leak.getLeakAsBgp());
String importPolicyName = generatedEvpnToBgpv4VrfLeakPolicyName(tenantVrfName);
assertThat(leak.getEvpnToBgpv4VrfLeakConfigs(), contains(EvpnToBgpv4VrfLeakConfig.builder().setImportFromVrf(DEFAULT_VRF_NAME).setImportPolicy(importPolicyName).build()));
EvpnType5Route.Builder rb = EvpnType5Route.builder().setNetwork(Prefix.strict("10.0.0.0/24")).setNextHop(NextHopVtep.of(3333, Ip.parse("5.6.7.8"))).setVni(3333).setProtocol(RoutingProtocol.BGP).setOriginMechanism(OriginMechanism.LEARNED).setOriginType(OriginType.IGP).setOriginatorIp(Ip.parse("5.6.7.8")).setRouteDistinguisher(RouteDistinguisher.from(routerId, tenantVrfPosition));
EvpnType5Route permittedRouteSingleRouteTarget = rb.setCommunities(CommunitySet.of(ExtendedCommunity.target(1, 3333))).build();
EvpnType5Route permittedRouteMultipleRouteTargets = rb.setCommunities(CommunitySet.of(ExtendedCommunity.target(1, 3333), ExtendedCommunity.target(5, 3333))).build();
EvpnType5Route deniedRouteWrongRouteTarget = rb.setCommunities(CommunitySet.of(ExtendedCommunity.target(5, 3333))).build();
EvpnType5Route deniedRouteNoRouteTarget = rb.setCommunities(CommunitySet.of()).build();
RoutingPolicy importPolicy = c.getRoutingPolicies().get(importPolicyName);
assertRoutingPolicyPermitsRoute(importPolicy, permittedRouteSingleRouteTarget);
assertRoutingPolicyPermitsRoute(importPolicy, permittedRouteMultipleRouteTargets);
assertRoutingPolicyDeniesRoute(importPolicy, deniedRouteWrongRouteTarget);
assertRoutingPolicyDeniesRoute(importPolicy, deniedRouteNoRouteTarget);
}
}
Aggregations