use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.
the class VirtualRouter method propagateBgpRoutes.
int propagateBgpRoutes(Map<Ip, Set<String>> ipOwners, int dependentRoutesIterations, SortedSet<Prefix> oscillatingPrefixes, Map<String, Node> nodes) {
int numRoutes = 0;
_receivedBgpAdvertisements = new LinkedHashSet<>();
_prevSentBgpAdvertisements = _sentBgpAdvertisements != null ? _sentBgpAdvertisements : new LinkedHashSet<>();
_sentBgpAdvertisements = new LinkedHashSet<>();
// If we have no BGP process, nothing to do
if (_vrf.getBgpProcess() == null) {
return numRoutes;
}
int ebgpAdminCost = RoutingProtocol.BGP.getDefaultAdministrativeCost(_c.getConfigurationFormat());
int ibgpAdminCost = RoutingProtocol.IBGP.getDefaultAdministrativeCost(_c.getConfigurationFormat());
for (BgpNeighbor neighbor : _vrf.getBgpProcess().getNeighbors().values()) {
Ip localIp = neighbor.getLocalIp();
Set<String> localIpOwners = ipOwners.get(localIp);
String hostname = _c.getHostname();
if (localIpOwners == null || !localIpOwners.contains(hostname)) {
continue;
}
BgpNeighbor remoteBgpNeighbor = neighbor.getRemoteBgpNeighbor();
if (remoteBgpNeighbor == null) {
continue;
}
int localAs = neighbor.getLocalAs();
int remoteAs = neighbor.getRemoteAs();
Configuration remoteConfig = remoteBgpNeighbor.getOwner();
String remoteHostname = remoteConfig.getHostname();
String remoteVrfName = remoteBgpNeighbor.getVrf();
Vrf remoteVrf = remoteConfig.getVrfs().get(remoteVrfName);
VirtualRouter remoteVirtualRouter = nodes.get(remoteHostname)._virtualRouters.get(remoteVrfName);
RoutingPolicy remoteExportPolicy = remoteConfig.getRoutingPolicies().get(remoteBgpNeighbor.getExportPolicy());
boolean ebgpSession = localAs != remoteAs;
BgpMultipathRib targetRib = ebgpSession ? _ebgpStagingRib : _ibgpStagingRib;
RoutingProtocol targetProtocol = ebgpSession ? RoutingProtocol.BGP : RoutingProtocol.IBGP;
Set<AbstractRoute> remoteCandidateRoutes = Collections.newSetFromMap(new IdentityHashMap<>());
// Add IGP routes
Set<AbstractRoute> activeRemoteRoutes = Collections.newSetFromMap(new IdentityHashMap<>());
activeRemoteRoutes.addAll(remoteVirtualRouter._prevMainRib.getRoutes());
for (AbstractRoute remoteCandidateRoute : activeRemoteRoutes) {
if (remoteCandidateRoute.getProtocol() != RoutingProtocol.BGP && remoteCandidateRoute.getProtocol() != RoutingProtocol.IBGP) {
remoteCandidateRoutes.add(remoteCandidateRoute);
}
}
/*
* bgp advertise-external
*
* When this is set, add best eBGP path independently of whether
* it is preempted by an iBGP or IGP route. Only applicable to
* iBGP sessions.
*/
boolean advertiseExternal = !ebgpSession && remoteBgpNeighbor.getAdvertiseExternal();
if (advertiseExternal) {
remoteCandidateRoutes.addAll(remoteVirtualRouter._prevEbgpBestPathRib.getRoutes());
}
/*
* bgp advertise-inactive
*
* When this is set, add best BGP path independently of whether
* it is preempted by an IGP route. Only applicable to eBGP
* sessions.
*/
boolean advertiseInactive = ebgpSession && remoteBgpNeighbor.getAdvertiseInactive();
/* Add best bgp paths if they are active, or if advertise-inactive */
for (AbstractRoute remoteCandidateRoute : remoteVirtualRouter._prevBgpBestPathRib.getRoutes()) {
if (advertiseInactive || activeRemoteRoutes.contains(remoteCandidateRoute)) {
remoteCandidateRoutes.add(remoteCandidateRoute);
}
}
/* Add all bgp paths if additional-paths active for this session */
boolean additionalPaths = !ebgpSession && neighbor.getAdditionalPathsReceive() && remoteBgpNeighbor.getAdditionalPathsSend() && remoteBgpNeighbor.getAdditionalPathsSelectAll();
if (additionalPaths) {
remoteCandidateRoutes.addAll(remoteVirtualRouter._prevBgpMultipathRib.getRoutes());
}
for (AbstractRoute remoteRoute : remoteCandidateRoutes) {
BgpRoute.Builder transformedOutgoingRouteBuilder = new BgpRoute.Builder();
transformedOutgoingRouteBuilder.setReceivedFromIp(remoteBgpNeighbor.getLocalIp());
RoutingProtocol remoteRouteProtocol = remoteRoute.getProtocol();
boolean remoteRouteIsBgp = remoteRouteProtocol == RoutingProtocol.IBGP || remoteRouteProtocol == RoutingProtocol.BGP;
// originatorIP
Ip originatorIp;
if (!ebgpSession && remoteRouteProtocol.equals(RoutingProtocol.IBGP)) {
BgpRoute bgpRemoteRoute = (BgpRoute) remoteRoute;
originatorIp = bgpRemoteRoute.getOriginatorIp();
} else {
originatorIp = remoteVrf.getBgpProcess().getRouterId();
}
transformedOutgoingRouteBuilder.setOriginatorIp(originatorIp);
// note whether new route is received from route reflector client
transformedOutgoingRouteBuilder.setReceivedFromRouteReflectorClient(!ebgpSession && neighbor.getRouteReflectorClient());
// for bgp remote route)
if (remoteRouteIsBgp) {
BgpRoute bgpRemoteRoute = (BgpRoute) remoteRoute;
transformedOutgoingRouteBuilder.setOriginType(bgpRemoteRoute.getOriginType());
if (ebgpSession && bgpRemoteRoute.getAsPath().containsAs(remoteBgpNeighbor.getRemoteAs()) && !remoteBgpNeighbor.getAllowRemoteAsOut()) {
// disable-peer-as-check (getAllowRemoteAsOut) is set
continue;
}
/*
* route reflection: reflect everything received from
* clients to clients and non-clients. reflect everything
* received from non-clients to clients. Do not reflect to
* originator
*/
Ip remoteOriginatorIp = bgpRemoteRoute.getOriginatorIp();
/*
* iBGP speaker should not send out routes to iBGP neighbor whose router-id is
* same as originator id of advertisement
*/
if (!ebgpSession && remoteOriginatorIp != null && _vrf.getBgpProcess().getRouterId().equals(remoteOriginatorIp)) {
continue;
}
if (remoteRouteProtocol.equals(RoutingProtocol.IBGP) && !ebgpSession) {
/*
* The remote route is iBGP. The session is iBGP. We consider whether to reflect, and
* modify the outgoing route as appropriate.
*/
boolean remoteRouteReceivedFromRouteReflectorClient = bgpRemoteRoute.getReceivedFromRouteReflectorClient();
boolean sendingToRouteReflectorClient = remoteBgpNeighbor.getRouteReflectorClient();
Ip remoteReceivedFromIp = bgpRemoteRoute.getReceivedFromIp();
boolean remoteRouteOriginatedByRemoteNeighbor = remoteReceivedFromIp.equals(Ip.ZERO);
if (!remoteRouteReceivedFromRouteReflectorClient && !sendingToRouteReflectorClient && !remoteRouteOriginatedByRemoteNeighbor) {
/*
* Neither reflecting nor originating this iBGP route, so don't send
*/
continue;
}
transformedOutgoingRouteBuilder.getClusterList().addAll(bgpRemoteRoute.getClusterList());
if (!remoteRouteOriginatedByRemoteNeighbor) {
// we are reflecting, so we need to get the clusterid associated with the remoteRoute
BgpNeighbor remoteReceivedFromSession = remoteVrf.getBgpProcess().getNeighbors().get(new Prefix(remoteReceivedFromIp, Prefix.MAX_PREFIX_LENGTH));
long newClusterId = remoteReceivedFromSession.getClusterId();
transformedOutgoingRouteBuilder.getClusterList().add(newClusterId);
}
Set<Long> localClusterIds = _vrf.getBgpProcess().getClusterIds();
Set<Long> outgoingClusterList = transformedOutgoingRouteBuilder.getClusterList();
if (localClusterIds.stream().anyMatch(outgoingClusterList::contains)) {
/*
* receiver will reject new route if it contains any of its local cluster ids
*/
continue;
}
}
}
// Outgoing communities
if (remoteRouteIsBgp) {
BgpRoute bgpRemoteRoute = (BgpRoute) remoteRoute;
transformedOutgoingRouteBuilder.setAsPath(bgpRemoteRoute.getAsPath().getAsSets());
if (remoteBgpNeighbor.getSendCommunity()) {
transformedOutgoingRouteBuilder.getCommunities().addAll(bgpRemoteRoute.getCommunities());
}
}
if (ebgpSession) {
SortedSet<Integer> newAsPathElement = new TreeSet<>();
newAsPathElement.add(remoteAs);
transformedOutgoingRouteBuilder.getAsPath().add(0, newAsPathElement);
}
// Outgoing protocol
transformedOutgoingRouteBuilder.setProtocol(targetProtocol);
transformedOutgoingRouteBuilder.setNetwork(remoteRoute.getNetwork());
// Outgoing metric
if (remoteRouteIsBgp) {
transformedOutgoingRouteBuilder.setMetric(remoteRoute.getMetric());
}
// Outgoing nextHopIp
// Outgoing localPreference
Ip nextHopIp;
int localPreference;
if (ebgpSession || !remoteRouteIsBgp) {
nextHopIp = remoteBgpNeighbor.getLocalIp();
localPreference = BgpRoute.DEFAULT_LOCAL_PREFERENCE;
} else {
nextHopIp = remoteRoute.getNextHopIp();
BgpRoute remoteIbgpRoute = (BgpRoute) remoteRoute;
localPreference = remoteIbgpRoute.getLocalPreference();
}
if (nextHopIp.equals(Route.UNSET_ROUTE_NEXT_HOP_IP)) {
// should only happen for ibgp
String nextHopInterface = remoteRoute.getNextHopInterface();
InterfaceAddress nextHopAddress = remoteVrf.getInterfaces().get(nextHopInterface).getAddress();
if (nextHopAddress == null) {
throw new BatfishException("remote route's nextHopInterface has no address");
}
nextHopIp = nextHopAddress.getIp();
}
transformedOutgoingRouteBuilder.setNextHopIp(nextHopIp);
transformedOutgoingRouteBuilder.setLocalPreference(localPreference);
// Outgoing srcProtocol
transformedOutgoingRouteBuilder.setSrcProtocol(remoteRoute.getProtocol());
/*
* CREATE OUTGOING ROUTE
*/
boolean acceptOutgoing = remoteExportPolicy.process(remoteRoute, transformedOutgoingRouteBuilder, localIp, remoteVrfName, Direction.OUT);
if (acceptOutgoing) {
BgpRoute transformedOutgoingRoute = transformedOutgoingRouteBuilder.build();
// Record sent advertisement
BgpAdvertisementType sentType = ebgpSession ? BgpAdvertisementType.EBGP_SENT : BgpAdvertisementType.IBGP_SENT;
Ip sentReceivedFromIp = transformedOutgoingRoute.getReceivedFromIp();
Ip sentOriginatorIp = transformedOutgoingRoute.getOriginatorIp();
SortedSet<Long> sentClusterList = transformedOutgoingRoute.getClusterList();
boolean sentReceivedFromRouteReflectorClient = transformedOutgoingRoute.getReceivedFromRouteReflectorClient();
AsPath sentAsPath = transformedOutgoingRoute.getAsPath();
SortedSet<Long> sentCommunities = transformedOutgoingRoute.getCommunities();
Prefix sentNetwork = remoteRoute.getNetwork();
Ip sentNextHopIp;
String sentSrcNode = remoteHostname;
String sentSrcVrf = remoteVrfName;
Ip sentSrcIp = remoteBgpNeighbor.getLocalIp();
String sentDstNode = hostname;
String sentDstVrf = _vrf.getName();
Ip sentDstIp = neighbor.getLocalIp();
int sentWeight = -1;
if (ebgpSession) {
sentNextHopIp = nextHopIp;
} else {
sentNextHopIp = transformedOutgoingRoute.getNextHopIp();
}
int sentLocalPreference = transformedOutgoingRoute.getLocalPreference();
long sentMed = transformedOutgoingRoute.getMetric();
OriginType sentOriginType = transformedOutgoingRoute.getOriginType();
RoutingProtocol sentSrcProtocol = targetProtocol;
BgpRoute.Builder transformedIncomingRouteBuilder = new BgpRoute.Builder();
// Incoming originatorIp
transformedIncomingRouteBuilder.setOriginatorIp(sentOriginatorIp);
// Incoming receivedFromIp
transformedIncomingRouteBuilder.setReceivedFromIp(sentReceivedFromIp);
// Incoming clusterList
transformedIncomingRouteBuilder.getClusterList().addAll(sentClusterList);
// Incoming receivedFromRouteReflectorClient
transformedIncomingRouteBuilder.setReceivedFromRouteReflectorClient(sentReceivedFromRouteReflectorClient);
// Incoming asPath
transformedIncomingRouteBuilder.setAsPath(sentAsPath.getAsSets());
// Incoming communities
transformedIncomingRouteBuilder.getCommunities().addAll(sentCommunities);
// Incoming protocol
transformedIncomingRouteBuilder.setProtocol(targetProtocol);
// Incoming network
transformedIncomingRouteBuilder.setNetwork(sentNetwork);
// Incoming nextHopIp
transformedIncomingRouteBuilder.setNextHopIp(sentNextHopIp);
// Incoming localPreference
transformedIncomingRouteBuilder.setLocalPreference(sentLocalPreference);
// Incoming admin
int admin = ebgpSession ? ebgpAdminCost : ibgpAdminCost;
transformedIncomingRouteBuilder.setAdmin(admin);
// Incoming metric
transformedIncomingRouteBuilder.setMetric(sentMed);
// Incoming originType
transformedIncomingRouteBuilder.setOriginType(sentOriginType);
// Incoming srcProtocol
transformedIncomingRouteBuilder.setSrcProtocol(sentSrcProtocol);
String importPolicyName = neighbor.getImportPolicy();
if (transformedOutgoingRoute.getAsPath().containsAs(neighbor.getLocalAs()) && !neighbor.getAllowLocalAsIn()) {
// disable-peer-as-check (getAllowRemoteAsOut) is set
continue;
}
BgpAdvertisement sentAdvert = new BgpAdvertisement(sentType, sentNetwork, sentNextHopIp, sentSrcNode, sentSrcVrf, sentSrcIp, sentDstNode, sentDstVrf, sentDstIp, sentSrcProtocol, sentOriginType, sentLocalPreference, sentMed, sentOriginatorIp, sentAsPath, sentCommunities, sentClusterList, sentWeight);
Prefix prefix = remoteRoute.getNetwork();
boolean isOscillatingPrefix = oscillatingPrefixes.contains(prefix);
boolean hasAdvertisementPriorityDuringRecovery = hasAdvertisementPriorityDuringRecovery(remoteRoute, dependentRoutesIterations, oscillatingPrefixes, neighbor, remoteBgpNeighbor);
if (isOscillatingPrefix && !hasAdvertisementPriorityDuringRecovery && !_prevSentBgpAdvertisements.contains(sentAdvert)) {
continue;
}
_sentBgpAdvertisements.add(sentAdvert);
/*
* CREATE INCOMING ROUTE
*/
boolean acceptIncoming = true;
if (importPolicyName != null) {
RoutingPolicy importPolicy = _c.getRoutingPolicies().get(importPolicyName);
if (importPolicy != null) {
acceptIncoming = importPolicy.process(transformedOutgoingRoute, transformedIncomingRouteBuilder, remoteBgpNeighbor.getLocalIp(), _key, Direction.IN);
}
}
if (acceptIncoming) {
BgpRoute transformedIncomingRoute = transformedIncomingRouteBuilder.build();
BgpAdvertisementType receivedType = ebgpSession ? BgpAdvertisementType.EBGP_RECEIVED : BgpAdvertisementType.IBGP_RECEIVED;
Prefix receivedNetwork = sentNetwork;
Ip receivedNextHopIp = sentNextHopIp;
String receivedSrcNode = sentSrcNode;
String receivedSrcVrf = sentSrcVrf;
Ip receivedSrcIp = sentSrcIp;
String receivedDstNode = sentDstNode;
String receivedDstVrf = sentDstVrf;
Ip receivedDstIp = sentDstIp;
RoutingProtocol receivedSrcProtocol = sentSrcProtocol;
OriginType receivedOriginType = transformedIncomingRoute.getOriginType();
int receivedLocalPreference = transformedIncomingRoute.getLocalPreference();
long receivedMed = transformedIncomingRoute.getMetric();
Ip receivedOriginatorIp = sentOriginatorIp;
AsPath receivedAsPath = transformedIncomingRoute.getAsPath();
SortedSet<Long> receivedCommunities = transformedIncomingRoute.getCommunities();
SortedSet<Long> receivedClusterList = sentClusterList;
int receivedWeight = transformedIncomingRoute.getWeight();
BgpAdvertisement receivedAdvert = new BgpAdvertisement(receivedType, receivedNetwork, receivedNextHopIp, receivedSrcNode, receivedSrcVrf, receivedSrcIp, receivedDstNode, receivedDstVrf, receivedDstIp, receivedSrcProtocol, receivedOriginType, receivedLocalPreference, receivedMed, receivedOriginatorIp, receivedAsPath, receivedCommunities, receivedClusterList, receivedWeight);
if (targetRib.mergeRoute(transformedIncomingRoute)) {
numRoutes++;
}
_receivedBgpAdvertisements.add(receivedAdvert);
}
}
}
}
return numRoutes;
}
use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.
the class VirtualRouter method initEbgpTopology.
private void initEbgpTopology(Map<Ip, Set<String>> ipOwners) {
if (_vrf.getBgpProcess() == null) {
// Nothing to do
return;
}
String hostname = _c.getHostname();
for (BgpNeighbor neighbor : _vrf.getBgpProcess().getNeighbors().values()) {
// Ebgp iff the neighbor AS is different from our AS
boolean ebgpSession = !neighbor.getRemoteAs().equals(neighbor.getLocalAs());
// We don't handle IBGP here
if (!ebgpSession) {
continue;
}
BgpNeighbor remoteNeighbor = neighbor.getRemoteBgpNeighbor();
if (remoteNeighbor == null) {
continue;
}
Set<String> localIpOwners = ipOwners.get(neighbor.getLocalIp());
boolean nodeOwnsLocalIp = localIpOwners.contains(hostname);
Set<String> remoteLocalIpOwners = ipOwners.get(remoteNeighbor.getLocalIp());
String remoteHostname = remoteNeighbor.getOwner().getHostname();
boolean remoteNeighborOwnsRemoteLocalIp = remoteLocalIpOwners.contains(remoteHostname);
if (nodeOwnsLocalIp && remoteNeighborOwnsRemoteLocalIp) {
// pretty confident we have a bgp connection here
// for now, just leave it alone
} else {
neighbor.setRemoteBgpNeighbor(null);
remoteNeighbor.setRemoteBgpNeighbor(null);
}
}
}
use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.
the class CiscoConfiguration method toBgpProcess.
private org.batfish.datamodel.BgpProcess toBgpProcess(final Configuration c, BgpProcess proc, String vrfName) {
org.batfish.datamodel.BgpProcess newBgpProcess = new org.batfish.datamodel.BgpProcess();
org.batfish.datamodel.Vrf v = c.getVrfs().get(vrfName);
BgpTieBreaker tieBreaker = proc.getTieBreaker();
if (tieBreaker != null) {
newBgpProcess.setTieBreaker(tieBreaker);
}
MultipathEquivalentAsPathMatchMode multipathEquivalentAsPathMatchMode = proc.getAsPathMultipathRelax() ? MultipathEquivalentAsPathMatchMode.PATH_LENGTH : MultipathEquivalentAsPathMatchMode.EXACT_PATH;
newBgpProcess.setMultipathEquivalentAsPathMatchMode(multipathEquivalentAsPathMatchMode);
Integer maximumPaths = proc.getMaximumPaths();
Integer maximumPathsEbgp = proc.getMaximumPathsEbgp();
Integer maximumPathsIbgp = proc.getMaximumPathsIbgp();
boolean multipathEbgp = false;
boolean multipathIbgp = false;
if (maximumPaths != null && maximumPaths > 1) {
multipathEbgp = true;
multipathIbgp = true;
}
if (maximumPathsEbgp != null && maximumPathsEbgp > 1) {
multipathEbgp = true;
}
if (maximumPathsIbgp != null && maximumPathsIbgp > 1) {
multipathIbgp = true;
}
newBgpProcess.setMultipathEbgp(multipathEbgp);
newBgpProcess.setMultipathIbgp(multipathIbgp);
Map<Prefix, BgpNeighbor> newBgpNeighbors = newBgpProcess.getNeighbors();
int defaultMetric = proc.getDefaultMetric();
Ip bgpRouterId = getBgpRouterId(c, vrfName, proc);
MatchPrefixSet matchDefaultRoute = new MatchPrefixSet(new DestinationNetwork(), new ExplicitPrefixSet(new PrefixSpace(Collections.singleton(new PrefixRange(Prefix.ZERO, new SubRange(0, 0))))));
matchDefaultRoute.setComment("match default route");
MatchPrefix6Set matchDefaultRoute6 = new MatchPrefix6Set(new DestinationNetwork6(), new ExplicitPrefix6Set(new Prefix6Space(Collections.singleton(new Prefix6Range(Prefix6.ZERO, new SubRange(0, 0))))));
matchDefaultRoute.setComment("match default route");
newBgpProcess.setRouterId(bgpRouterId);
Set<BgpAggregateIpv4Network> summaryOnlyNetworks = new HashSet<>();
Set<BgpAggregateIpv6Network> summaryOnlyIpv6Networks = new HashSet<>();
List<BooleanExpr> attributeMapPrefilters = new ArrayList<>();
// add generated routes for aggregate ipv4 addresses
for (Entry<Prefix, BgpAggregateIpv4Network> e : proc.getAggregateNetworks().entrySet()) {
Prefix prefix = e.getKey();
BgpAggregateIpv4Network aggNet = e.getValue();
boolean summaryOnly = aggNet.getSummaryOnly();
int prefixLength = prefix.getPrefixLength();
SubRange prefixRange = new SubRange(prefixLength + 1, Prefix.MAX_PREFIX_LENGTH);
if (summaryOnly) {
summaryOnlyNetworks.add(aggNet);
}
// create generation policy for aggregate network
String generationPolicyName = "~AGGREGATE_ROUTE_GEN:" + vrfName + ":" + prefix + "~";
RoutingPolicy currentGeneratedRoutePolicy = new RoutingPolicy(generationPolicyName, c);
If currentGeneratedRouteConditional = new If();
currentGeneratedRoutePolicy.getStatements().add(currentGeneratedRouteConditional);
currentGeneratedRouteConditional.setGuard(new MatchPrefixSet(new DestinationNetwork(), new ExplicitPrefixSet(new PrefixSpace(Collections.singleton(new PrefixRange(prefix, prefixRange))))));
currentGeneratedRouteConditional.getTrueStatements().add(Statements.ReturnTrue.toStaticStatement());
c.getRoutingPolicies().put(generationPolicyName, currentGeneratedRoutePolicy);
GeneratedRoute.Builder gr = new GeneratedRoute.Builder();
gr.setNetwork(prefix);
gr.setAdmin(CISCO_AGGREGATE_ROUTE_ADMIN_COST);
gr.setGenerationPolicy(generationPolicyName);
gr.setDiscard(true);
// set attribute map for aggregate network
String attributeMapName = aggNet.getAttributeMap();
Conjunction applyCurrentAggregateAttributesConditions = new Conjunction();
applyCurrentAggregateAttributesConditions.getConjuncts().add(new MatchPrefixSet(new DestinationNetwork(), new ExplicitPrefixSet(new PrefixSpace(Collections.singleton(PrefixRange.fromPrefix(prefix))))));
applyCurrentAggregateAttributesConditions.getConjuncts().add(new MatchProtocol(RoutingProtocol.AGGREGATE));
BooleanExpr weInterior = BooleanExprs.True.toStaticBooleanExpr();
if (attributeMapName != null) {
int attributeMapLine = aggNet.getAttributeMapLine();
RouteMap attributeMap = _routeMaps.get(attributeMapName);
if (attributeMap != null) {
// need to apply attribute changes if this specific route is
// matched
weInterior = new CallExpr(attributeMapName);
attributeMap.getReferers().put(aggNet, "attribute-map of aggregate route: " + prefix);
gr.setAttributePolicy(attributeMapName);
} else {
undefined(CiscoStructureType.ROUTE_MAP, attributeMapName, CiscoStructureUsage.BGP_AGGREGATE_ATTRIBUTE_MAP, attributeMapLine);
}
}
v.getGeneratedRoutes().add(gr.build());
BooleanExpr we = bgpRedistributeWithEnvironmentExpr(weInterior, OriginType.IGP);
applyCurrentAggregateAttributesConditions.getConjuncts().add(we);
attributeMapPrefilters.add(applyCurrentAggregateAttributesConditions);
}
// TODO: merge with above to make cleaner
for (Entry<Prefix6, BgpAggregateIpv6Network> e : proc.getAggregateIpv6Networks().entrySet()) {
Prefix6 prefix6 = e.getKey();
BgpAggregateIpv6Network aggNet = e.getValue();
boolean summaryOnly = aggNet.getSummaryOnly();
int prefixLength = prefix6.getPrefixLength();
SubRange prefixRange = new SubRange(prefixLength + 1, Prefix.MAX_PREFIX_LENGTH);
if (summaryOnly) {
summaryOnlyIpv6Networks.add(aggNet);
}
// create generation policy for aggregate network
String generationPolicyName = "~AGGREGATE_ROUTE6_GEN:" + vrfName + ":" + prefix6 + "~";
RoutingPolicy currentGeneratedRoutePolicy = new RoutingPolicy(generationPolicyName, c);
If currentGeneratedRouteConditional = new If();
currentGeneratedRoutePolicy.getStatements().add(currentGeneratedRouteConditional);
currentGeneratedRouteConditional.setGuard(new MatchPrefix6Set(new DestinationNetwork6(), new ExplicitPrefix6Set(new Prefix6Space(Collections.singleton(new Prefix6Range(prefix6, prefixRange))))));
currentGeneratedRouteConditional.getTrueStatements().add(Statements.ReturnTrue.toStaticStatement());
c.getRoutingPolicies().put(generationPolicyName, currentGeneratedRoutePolicy);
GeneratedRoute6 gr = new GeneratedRoute6(prefix6, CISCO_AGGREGATE_ROUTE_ADMIN_COST);
gr.setGenerationPolicy(generationPolicyName);
gr.setDiscard(true);
v.getGeneratedIpv6Routes().add(gr);
// set attribute map for aggregate network
String attributeMapName = aggNet.getAttributeMap();
if (attributeMapName != null) {
int attributeMapLine = aggNet.getAttributeMapLine();
RouteMap attributeMap = _routeMaps.get(attributeMapName);
if (attributeMap != null) {
attributeMap.getReferers().put(aggNet, "attribute-map of aggregate ipv6 route: " + prefix6);
gr.setAttributePolicy(attributeMapName);
} else {
undefined(CiscoStructureType.ROUTE_MAP, attributeMapName, CiscoStructureUsage.BGP_AGGREGATE_ATTRIBUTE_MAP, attributeMapLine);
}
}
}
/*
* Create common bgp export policy. This policy encompasses network
* statements, aggregate-address with/without summary-only, redistribution
* from other protocols, and default-origination
*/
String bgpCommonExportPolicyName = "~BGP_COMMON_EXPORT_POLICY:" + vrfName + "~";
RoutingPolicy bgpCommonExportPolicy = new RoutingPolicy(bgpCommonExportPolicyName, c);
c.getRoutingPolicies().put(bgpCommonExportPolicyName, bgpCommonExportPolicy);
List<Statement> bgpCommonExportStatements = bgpCommonExportPolicy.getStatements();
// create policy for denying suppressed summary-only networks
if (summaryOnlyNetworks.size() > 0) {
If suppressSummaryOnly = new If();
bgpCommonExportStatements.add(suppressSummaryOnly);
suppressSummaryOnly.setComment("Suppress summarized of summary-only aggregate-address networks");
String matchSuppressedSummaryOnlyRoutesName = "~MATCH_SUPPRESSED_SUMMARY_ONLY:" + vrfName + "~";
RouteFilterList matchSuppressedSummaryOnlyRoutes = new RouteFilterList(matchSuppressedSummaryOnlyRoutesName);
c.getRouteFilterLists().put(matchSuppressedSummaryOnlyRoutesName, matchSuppressedSummaryOnlyRoutes);
for (BgpAggregateIpv4Network summaryOnlyNetwork : summaryOnlyNetworks) {
Prefix prefix = summaryOnlyNetwork.getPrefix();
int prefixLength = prefix.getPrefixLength();
RouteFilterLine line = new RouteFilterLine(LineAction.ACCEPT, prefix, new SubRange(prefixLength + 1, Prefix.MAX_PREFIX_LENGTH));
matchSuppressedSummaryOnlyRoutes.addLine(line);
}
suppressSummaryOnly.setGuard(new MatchPrefixSet(new DestinationNetwork(), new NamedPrefixSet(matchSuppressedSummaryOnlyRoutesName)));
suppressSummaryOnly.getTrueStatements().add(Statements.ReturnFalse.toStaticStatement());
}
If preFilter = new If();
bgpCommonExportStatements.add(preFilter);
bgpCommonExportStatements.add(Statements.ReturnFalse.toStaticStatement());
Disjunction preFilterConditions = new Disjunction();
preFilter.setGuard(preFilterConditions);
preFilter.getTrueStatements().add(Statements.ReturnTrue.toStaticStatement());
preFilterConditions.getDisjuncts().addAll(attributeMapPrefilters);
// create redistribution origination policies
// redistribute rip
BgpRedistributionPolicy redistributeRipPolicy = proc.getRedistributionPolicies().get(RoutingProtocol.RIP);
if (redistributeRipPolicy != null) {
BooleanExpr weInterior = BooleanExprs.True.toStaticBooleanExpr();
Conjunction exportRipConditions = new Conjunction();
exportRipConditions.setComment("Redistribute RIP routes into BGP");
exportRipConditions.getConjuncts().add(new MatchProtocol(RoutingProtocol.RIP));
String mapName = redistributeRipPolicy.getRouteMap();
if (mapName != null) {
int mapLine = redistributeRipPolicy.getRouteMapLine();
RouteMap redistributeRipRouteMap = _routeMaps.get(mapName);
if (redistributeRipRouteMap != null) {
redistributeRipRouteMap.getReferers().put(proc, "RIP redistribution route-map");
weInterior = new CallExpr(mapName);
} else {
undefined(CiscoStructureType.ROUTE_MAP, mapName, CiscoStructureUsage.BGP_REDISTRIBUTE_RIP_MAP, mapLine);
}
}
BooleanExpr we = bgpRedistributeWithEnvironmentExpr(weInterior, OriginType.INCOMPLETE);
exportRipConditions.getConjuncts().add(we);
preFilterConditions.getDisjuncts().add(exportRipConditions);
}
// redistribute static
BgpRedistributionPolicy redistributeStaticPolicy = proc.getRedistributionPolicies().get(RoutingProtocol.STATIC);
if (redistributeStaticPolicy != null) {
BooleanExpr weInterior = BooleanExprs.True.toStaticBooleanExpr();
Conjunction exportStaticConditions = new Conjunction();
exportStaticConditions.setComment("Redistribute static routes into BGP");
exportStaticConditions.getConjuncts().add(new MatchProtocol(RoutingProtocol.STATIC));
String mapName = redistributeStaticPolicy.getRouteMap();
if (mapName != null) {
int mapLine = redistributeStaticPolicy.getRouteMapLine();
RouteMap redistributeStaticRouteMap = _routeMaps.get(mapName);
if (redistributeStaticRouteMap != null) {
redistributeStaticRouteMap.getReferers().put(proc, "static redistribution route-map");
weInterior = new CallExpr(mapName);
} else {
undefined(CiscoStructureType.ROUTE_MAP, mapName, CiscoStructureUsage.BGP_REDISTRIBUTE_STATIC_MAP, mapLine);
}
}
BooleanExpr we = bgpRedistributeWithEnvironmentExpr(weInterior, OriginType.INCOMPLETE);
exportStaticConditions.getConjuncts().add(we);
preFilterConditions.getDisjuncts().add(exportStaticConditions);
}
// redistribute connected
BgpRedistributionPolicy redistributeConnectedPolicy = proc.getRedistributionPolicies().get(RoutingProtocol.CONNECTED);
if (redistributeConnectedPolicy != null) {
BooleanExpr weInterior = BooleanExprs.True.toStaticBooleanExpr();
Conjunction exportConnectedConditions = new Conjunction();
exportConnectedConditions.setComment("Redistribute connected routes into BGP");
exportConnectedConditions.getConjuncts().add(new MatchProtocol(RoutingProtocol.CONNECTED));
String mapName = redistributeConnectedPolicy.getRouteMap();
if (mapName != null) {
int mapLine = redistributeConnectedPolicy.getRouteMapLine();
RouteMap redistributeConnectedRouteMap = _routeMaps.get(mapName);
if (redistributeConnectedRouteMap != null) {
redistributeConnectedRouteMap.getReferers().put(proc, "connected redistribution route-map");
weInterior = new CallExpr(mapName);
} else {
undefined(CiscoStructureType.ROUTE_MAP, mapName, CiscoStructureUsage.BGP_REDISTRIBUTE_CONNECTED_MAP, mapLine);
}
}
BooleanExpr we = bgpRedistributeWithEnvironmentExpr(weInterior, OriginType.INCOMPLETE);
exportConnectedConditions.getConjuncts().add(we);
preFilterConditions.getDisjuncts().add(exportConnectedConditions);
}
// redistribute ospf
BgpRedistributionPolicy redistributeOspfPolicy = proc.getRedistributionPolicies().get(RoutingProtocol.OSPF);
if (redistributeOspfPolicy != null) {
BooleanExpr weInterior = BooleanExprs.True.toStaticBooleanExpr();
Conjunction exportOspfConditions = new Conjunction();
exportOspfConditions.setComment("Redistribute OSPF routes into BGP");
exportOspfConditions.getConjuncts().add(new MatchProtocol(RoutingProtocol.OSPF));
String mapName = redistributeOspfPolicy.getRouteMap();
if (mapName != null) {
int mapLine = redistributeOspfPolicy.getRouteMapLine();
RouteMap redistributeOspfRouteMap = _routeMaps.get(mapName);
if (redistributeOspfRouteMap != null) {
redistributeOspfRouteMap.getReferers().put(proc, "ospf redistribution route-map");
weInterior = new CallExpr(mapName);
} else {
undefined(CiscoStructureType.ROUTE_MAP, mapName, CiscoStructureUsage.BGP_REDISTRIBUTE_OSPF_MAP, mapLine);
}
}
BooleanExpr we = bgpRedistributeWithEnvironmentExpr(weInterior, OriginType.INCOMPLETE);
exportOspfConditions.getConjuncts().add(we);
preFilterConditions.getDisjuncts().add(exportOspfConditions);
}
// cause ip peer groups to inherit unset fields from owning named peer
// group if it exists, and then always from process master peer group
Set<LeafBgpPeerGroup> leafGroups = new LinkedHashSet<>();
leafGroups.addAll(proc.getIpPeerGroups().values());
leafGroups.addAll(proc.getIpv6PeerGroups().values());
leafGroups.addAll(proc.getDynamicIpPeerGroups().values());
leafGroups.addAll(proc.getDynamicIpv6PeerGroups().values());
for (LeafBgpPeerGroup lpg : leafGroups) {
lpg.inheritUnsetFields(proc, this);
}
_unusedPeerGroups = new TreeMap<>();
int fakePeerCounter = -1;
// peer groups / peer templates
for (Entry<String, NamedBgpPeerGroup> e : proc.getNamedPeerGroups().entrySet()) {
String name = e.getKey();
NamedBgpPeerGroup namedPeerGroup = e.getValue();
if (!namedPeerGroup.getInherited()) {
_unusedPeerGroups.put(name, namedPeerGroup.getDefinitionLine());
Ip fakeIp = new Ip(fakePeerCounter);
IpBgpPeerGroup fakePg = new IpBgpPeerGroup(fakeIp);
fakePg.setGroupName(name);
fakePg.setActive(false);
fakePg.setShutdown(true);
leafGroups.add(fakePg);
fakePg.inheritUnsetFields(proc, this);
fakePeerCounter--;
}
namedPeerGroup.inheritUnsetFields(proc, this);
}
// separate because peer sessions can inherit from other peer sessions
_unusedPeerSessions = new TreeMap<>();
int fakeGroupCounter = 1;
for (NamedBgpPeerGroup namedPeerGroup : proc.getPeerSessions().values()) {
namedPeerGroup.getParentSession(proc, this).inheritUnsetFields(proc, this);
}
for (Entry<String, NamedBgpPeerGroup> e : proc.getPeerSessions().entrySet()) {
String name = e.getKey();
NamedBgpPeerGroup namedPeerGroup = e.getValue();
if (!namedPeerGroup.getInherited()) {
_unusedPeerSessions.put(name, namedPeerGroup.getDefinitionLine());
String fakeNamedPgName = "~FAKE_PG_" + fakeGroupCounter + "~";
NamedBgpPeerGroup fakeNamedPg = new NamedBgpPeerGroup(fakeNamedPgName, -1);
fakeNamedPg.setPeerSession(name);
proc.getNamedPeerGroups().put(fakeNamedPgName, fakeNamedPg);
Ip fakeIp = new Ip(fakePeerCounter);
IpBgpPeerGroup fakePg = new IpBgpPeerGroup(fakeIp);
fakePg.setGroupName(fakeNamedPgName);
fakePg.setActive(false);
fakePg.setShutdown(true);
leafGroups.add(fakePg);
fakePg.inheritUnsetFields(proc, this);
fakeGroupCounter++;
fakePeerCounter--;
}
}
// create origination prefilter from listed advertised networks
proc.getIpNetworks().forEach((prefix, bgpNetwork) -> {
String mapName = bgpNetwork.getRouteMapName();
BooleanExpr weExpr = BooleanExprs.True.toStaticBooleanExpr();
if (mapName != null) {
int mapLine = bgpNetwork.getRouteMapLine();
RouteMap routeMap = _routeMaps.get(mapName);
if (routeMap != null) {
weExpr = new CallExpr(mapName);
routeMap.getReferers().put(proc, "bgp ipv4 advertised network route-map");
} else {
undefined(CiscoStructureType.ROUTE_MAP, mapName, CiscoStructureUsage.BGP_NETWORK_ORIGINATION_ROUTE_MAP, mapLine);
}
}
BooleanExpr we = bgpRedistributeWithEnvironmentExpr(weExpr, OriginType.IGP);
Conjunction exportNetworkConditions = new Conjunction();
PrefixSpace space = new PrefixSpace();
space.addPrefix(prefix);
exportNetworkConditions.getConjuncts().add(new MatchPrefixSet(new DestinationNetwork(), new ExplicitPrefixSet(space)));
exportNetworkConditions.getConjuncts().add(new Not(new MatchProtocol(RoutingProtocol.BGP)));
exportNetworkConditions.getConjuncts().add(new Not(new MatchProtocol(RoutingProtocol.IBGP)));
// TODO: ban aggregates?
exportNetworkConditions.getConjuncts().add(new Not(new MatchProtocol(RoutingProtocol.AGGREGATE)));
exportNetworkConditions.getConjuncts().add(we);
preFilterConditions.getDisjuncts().add(exportNetworkConditions);
});
String localFilter6Name = "~BGP_NETWORK6_NETWORKS_FILTER:" + vrfName + "~";
Route6FilterList localFilter6 = new Route6FilterList(localFilter6Name);
proc.getIpv6Networks().forEach((prefix6, bgpNetwork6) -> {
int prefixLen = prefix6.getPrefixLength();
Route6FilterLine line = new Route6FilterLine(LineAction.ACCEPT, prefix6, new SubRange(prefixLen, prefixLen));
localFilter6.addLine(line);
String mapName = bgpNetwork6.getRouteMapName();
if (mapName != null) {
int mapLine = bgpNetwork6.getRouteMapLine();
RouteMap routeMap = _routeMaps.get(mapName);
if (routeMap != null) {
routeMap.getReferers().put(proc, "bgp ipv6 advertised network route-map");
BooleanExpr we = bgpRedistributeWithEnvironmentExpr(new CallExpr(mapName), OriginType.IGP);
Conjunction exportNetwork6Conditions = new Conjunction();
Prefix6Space space6 = new Prefix6Space();
space6.addPrefix6(prefix6);
exportNetwork6Conditions.getConjuncts().add(new MatchPrefix6Set(new DestinationNetwork6(), new ExplicitPrefix6Set(space6)));
exportNetwork6Conditions.getConjuncts().add(new Not(new MatchProtocol(RoutingProtocol.BGP)));
exportNetwork6Conditions.getConjuncts().add(new Not(new MatchProtocol(RoutingProtocol.IBGP)));
// TODO: ban aggregates?
exportNetwork6Conditions.getConjuncts().add(new Not(new MatchProtocol(RoutingProtocol.AGGREGATE)));
exportNetwork6Conditions.getConjuncts().add(we);
preFilterConditions.getDisjuncts().add(exportNetwork6Conditions);
} else {
undefined(CiscoStructureType.ROUTE_MAP, mapName, CiscoStructureUsage.BGP_NETWORK6_ORIGINATION_ROUTE_MAP, mapLine);
}
}
});
c.getRoute6FilterLists().put(localFilter6Name, localFilter6);
MatchProtocol isEbgp = new MatchProtocol(RoutingProtocol.BGP);
MatchProtocol isIbgp = new MatchProtocol(RoutingProtocol.IBGP);
preFilterConditions.getDisjuncts().add(isEbgp);
preFilterConditions.getDisjuncts().add(isIbgp);
for (LeafBgpPeerGroup lpg : leafGroups) {
// update source
String updateSourceInterface = lpg.getUpdateSource();
boolean ipv4 = lpg.getNeighborPrefix() != null;
Ip updateSource = getUpdateSource(c, vrfName, lpg, updateSourceInterface, ipv4);
RoutingPolicy importPolicy = null;
String inboundRouteMapName = lpg.getInboundRouteMap();
if (inboundRouteMapName != null) {
int inboundRouteMapLine = lpg.getInboundRouteMapLine();
importPolicy = c.getRoutingPolicies().get(inboundRouteMapName);
if (importPolicy == null) {
undefined(CiscoStructureType.ROUTE_MAP, inboundRouteMapName, CiscoStructureUsage.BGP_INBOUND_ROUTE_MAP, inboundRouteMapLine);
} else {
RouteMap inboundRouteMap = _routeMaps.get(inboundRouteMapName);
inboundRouteMap.getReferers().put(lpg, "inbound route-map for leaf peer-group: " + lpg.getName());
}
}
String inboundRoute6MapName = lpg.getInboundRoute6Map();
RoutingPolicy importPolicy6 = null;
if (inboundRoute6MapName != null) {
int inboundRoute6MapLine = lpg.getInboundRoute6MapLine();
importPolicy6 = c.getRoutingPolicies().get(inboundRoute6MapName);
if (importPolicy6 == null) {
undefined(CiscoStructureType.ROUTE_MAP, inboundRoute6MapName, CiscoStructureUsage.BGP_INBOUND_ROUTE6_MAP, inboundRoute6MapLine);
} else {
RouteMap inboundRouteMap = _routeMaps.get(inboundRoute6MapName);
inboundRouteMap.getReferers().put(lpg, "inbound route-map for leaf peer-group: " + lpg.getName());
}
}
String peerExportPolicyName = "~BGP_PEER_EXPORT_POLICY:" + vrfName + ":" + lpg.getName() + "~";
RoutingPolicy peerExportPolicy = new RoutingPolicy(peerExportPolicyName, c);
if (lpg.getActive() && !lpg.getShutdown()) {
c.getRoutingPolicies().put(peerExportPolicyName, peerExportPolicy);
}
if (lpg.getNextHopSelf() != null && lpg.getNextHopSelf()) {
peerExportPolicy.getStatements().add(new SetNextHop(new SelfNextHop(), false));
}
if (lpg.getRemovePrivateAs() != null && lpg.getRemovePrivateAs()) {
peerExportPolicy.getStatements().add(Statements.RemovePrivateAs.toStaticStatement());
}
If peerExportConditional = new If();
peerExportConditional.setComment("peer-export policy main conditional: exitAccept if true / exitReject if false");
peerExportPolicy.getStatements().add(peerExportConditional);
Conjunction peerExportConditions = new Conjunction();
peerExportConditional.setGuard(peerExportConditions);
peerExportConditional.getTrueStatements().add(Statements.ExitAccept.toStaticStatement());
peerExportConditional.getFalseStatements().add(Statements.ExitReject.toStaticStatement());
Disjunction localOrCommonOrigination = new Disjunction();
peerExportConditions.getConjuncts().add(localOrCommonOrigination);
localOrCommonOrigination.getDisjuncts().add(new CallExpr(bgpCommonExportPolicyName));
String outboundRouteMapName = lpg.getOutboundRouteMap();
if (outboundRouteMapName != null) {
int outboundRouteMapLine = lpg.getOutboundRouteMapLine();
RouteMap outboundRouteMap = _routeMaps.get(outboundRouteMapName);
if (outboundRouteMap == null) {
undefined(CiscoStructureType.ROUTE_MAP, outboundRouteMapName, CiscoStructureUsage.BGP_OUTBOUND_ROUTE_MAP, outboundRouteMapLine);
} else {
outboundRouteMap.getReferers().put(lpg, "outbound route-map for leaf peer-group: " + lpg.getName());
peerExportConditions.getConjuncts().add(new CallExpr(outboundRouteMapName));
}
}
String outboundRoute6MapName = lpg.getOutboundRoute6Map();
if (outboundRoute6MapName != null) {
int outboundRoute6MapLine = lpg.getOutboundRoute6MapLine();
RouteMap outboundRoute6Map = _routeMaps.get(outboundRoute6MapName);
if (outboundRoute6Map == null) {
undefined(CiscoStructureType.ROUTE_MAP, outboundRoute6MapName, CiscoStructureUsage.BGP_OUTBOUND_ROUTE6_MAP, outboundRoute6MapLine);
} else {
outboundRoute6Map.getReferers().put(lpg, "outbound ipv6 route-map for leaf peer-group: " + lpg.getName());
}
}
// set up default export policy for this peer group
GeneratedRoute.Builder defaultRoute = null;
GeneratedRoute6.Builder defaultRoute6 = null;
if (lpg.getDefaultOriginate()) {
if (ipv4) {
localOrCommonOrigination.getDisjuncts().add(matchDefaultRoute);
} else {
localOrCommonOrigination.getDisjuncts().add(matchDefaultRoute6);
}
defaultRoute = new GeneratedRoute.Builder();
defaultRoute.setNetwork(Prefix.ZERO);
defaultRoute.setAdmin(MAX_ADMINISTRATIVE_COST);
defaultRoute6 = new GeneratedRoute6.Builder();
defaultRoute6.setNetwork(Prefix6.ZERO);
defaultRoute6.setAdmin(MAX_ADMINISTRATIVE_COST);
String defaultOriginateMapName = lpg.getDefaultOriginateMap();
if (defaultOriginateMapName != null) {
// originate contingent on
// generation policy
int defaultOriginateMapLine = lpg.getDefaultOriginateMapLine();
RoutingPolicy defaultRouteGenerationPolicy = c.getRoutingPolicies().get(defaultOriginateMapName);
if (defaultRouteGenerationPolicy == null) {
undefined(CiscoStructureType.ROUTE_MAP, defaultOriginateMapName, CiscoStructureUsage.BGP_DEFAULT_ORIGINATE_ROUTE_MAP, defaultOriginateMapLine);
} else {
RouteMap defaultRouteGenerationRouteMap = _routeMaps.get(defaultOriginateMapName);
defaultRouteGenerationRouteMap.getReferers().put(lpg, "default route generation policy for leaf peer-group: " + lpg.getName());
defaultRoute.setGenerationPolicy(defaultOriginateMapName);
}
} else {
String defaultRouteGenerationPolicyName = "~BGP_DEFAULT_ROUTE_GENERATION_POLICY:" + vrfName + ":" + lpg.getName() + "~";
RoutingPolicy defaultRouteGenerationPolicy = new RoutingPolicy(defaultRouteGenerationPolicyName, c);
If defaultRouteGenerationConditional = new If();
defaultRouteGenerationPolicy.getStatements().add(defaultRouteGenerationConditional);
if (ipv4) {
defaultRouteGenerationConditional.setGuard(matchDefaultRoute);
} else {
defaultRouteGenerationConditional.setGuard(matchDefaultRoute6);
}
defaultRouteGenerationConditional.getTrueStatements().add(Statements.ReturnTrue.toStaticStatement());
if (lpg.getActive() && !lpg.getShutdown()) {
c.getRoutingPolicies().put(defaultRouteGenerationPolicyName, defaultRouteGenerationPolicy);
}
if (ipv4) {
defaultRoute.setGenerationPolicy(defaultRouteGenerationPolicyName);
} else {
defaultRoute6.setGenerationPolicy(defaultRouteGenerationPolicyName);
}
}
}
Ip clusterId = lpg.getClusterId();
if (clusterId == null) {
clusterId = bgpRouterId;
}
boolean routeReflectorClient = lpg.getRouteReflectorClient();
boolean sendCommunity = lpg.getSendCommunity();
boolean additionalPathsReceive = lpg.getAdditionalPathsReceive();
boolean additionalPathsSelectAll = lpg.getAdditionalPathsSelectAll();
boolean additionalPathsSend = lpg.getAdditionalPathsSend();
boolean advertiseInactive = lpg.getAdvertiseInactive();
boolean ebgpMultihop = lpg.getEbgpMultihop();
boolean allowasIn = lpg.getAllowAsIn();
boolean disablePeerAsCheck = lpg.getDisablePeerAsCheck();
String inboundPrefixListName = lpg.getInboundPrefixList();
if (inboundPrefixListName != null) {
int inboundPrefixListLine = lpg.getInboundPrefixListLine();
ReferenceCountedStructure inboundPrefixList;
if (ipv4) {
inboundPrefixList = _prefixLists.get(inboundPrefixListName);
} else {
inboundPrefixList = _prefix6Lists.get(inboundPrefixListName);
}
if (inboundPrefixList != null) {
inboundPrefixList.getReferers().put(lpg, "inbound prefix-list for neighbor: '" + lpg.getName() + "'");
} else {
if (ipv4) {
undefined(CiscoStructureType.PREFIX_LIST, inboundPrefixListName, CiscoStructureUsage.BGP_INBOUND_PREFIX_LIST, inboundPrefixListLine);
} else {
undefined(CiscoStructureType.PREFIX6_LIST, inboundPrefixListName, CiscoStructureUsage.BGP_INBOUND_PREFIX6_LIST, inboundPrefixListLine);
}
}
}
String outboundPrefixListName = lpg.getOutboundPrefixList();
if (outboundPrefixListName != null) {
int outboundPrefixListLine = lpg.getOutboundPrefixListLine();
ReferenceCountedStructure outboundPrefixList;
if (ipv4) {
outboundPrefixList = _prefixLists.get(outboundPrefixListName);
} else {
outboundPrefixList = _prefix6Lists.get(outboundPrefixListName);
}
if (outboundPrefixList != null) {
outboundPrefixList.getReferers().put(lpg, "outbound prefix-list for neighbor: '" + lpg.getName() + "'");
} else {
if (ipv4) {
undefined(CiscoStructureType.PREFIX_LIST, outboundPrefixListName, CiscoStructureUsage.BGP_OUTBOUND_PREFIX_LIST, outboundPrefixListLine);
} else {
undefined(CiscoStructureType.PREFIX6_LIST, outboundPrefixListName, CiscoStructureUsage.BGP_OUTBOUND_PREFIX6_LIST, outboundPrefixListLine);
}
}
}
String description = lpg.getDescription();
if (lpg.getActive() && !lpg.getShutdown()) {
if (lpg.getRemoteAs() == null) {
_w.redFlag("No remote-as set for peer: " + lpg.getName());
continue;
}
Integer pgLocalAs = lpg.getLocalAs();
int localAs = pgLocalAs != null ? pgLocalAs : proc.getName();
BgpNeighbor newNeighbor;
if (lpg instanceof IpBgpPeerGroup) {
IpBgpPeerGroup ipg = (IpBgpPeerGroup) lpg;
Ip neighborAddress = ipg.getIp();
newNeighbor = new BgpNeighbor(neighborAddress, c);
} else if (lpg instanceof DynamicIpBgpPeerGroup) {
DynamicIpBgpPeerGroup dpg = (DynamicIpBgpPeerGroup) lpg;
Prefix neighborAddressRange = dpg.getPrefix();
newNeighbor = new BgpNeighbor(neighborAddressRange, c);
} else if (lpg instanceof Ipv6BgpPeerGroup || lpg instanceof DynamicIpv6BgpPeerGroup) {
// TODO: implement ipv6 bgp neighbors
continue;
} else {
throw new VendorConversionException("Invalid BGP leaf neighbor type");
}
newBgpNeighbors.put(newNeighbor.getPrefix(), newNeighbor);
newNeighbor.setAdditionalPathsReceive(additionalPathsReceive);
newNeighbor.setAdditionalPathsSelectAll(additionalPathsSelectAll);
newNeighbor.setAdditionalPathsSend(additionalPathsSend);
newNeighbor.setAdvertiseInactive(advertiseInactive);
newNeighbor.setAllowLocalAsIn(allowasIn);
newNeighbor.setAllowRemoteAsOut(disablePeerAsCheck);
newNeighbor.setRouteReflectorClient(routeReflectorClient);
newNeighbor.setClusterId(clusterId.asLong());
newNeighbor.setDefaultMetric(defaultMetric);
newNeighbor.setDescription(description);
newNeighbor.setEbgpMultihop(ebgpMultihop);
if (defaultRoute != null) {
newNeighbor.getGeneratedRoutes().add(defaultRoute.build());
}
newNeighbor.setGroup(lpg.getGroupName());
if (importPolicy != null) {
newNeighbor.setImportPolicy(inboundRouteMapName);
}
newNeighbor.setLocalAs(localAs);
newNeighbor.setLocalIp(updateSource);
newNeighbor.setExportPolicy(peerExportPolicyName);
newNeighbor.setRemoteAs(lpg.getRemoteAs());
newNeighbor.setSendCommunity(sendCommunity);
newNeighbor.setVrf(vrfName);
}
}
return newBgpProcess;
}
use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.
the class AutoAs method evaluate.
@Override
public int evaluate(Environment environment) {
BgpProcess proc = environment.getVrf().getBgpProcess();
if (proc == null) {
throw new BatfishException("Expected BGP process");
}
Direction direction = environment.getDirection();
int as;
Ip peerAddress = environment.getPeerAddress();
if (peerAddress == null) {
throw new BatfishException("Expected a peer address");
}
Prefix peerPrefix = new Prefix(peerAddress, Prefix.MAX_PREFIX_LENGTH);
BgpNeighbor neighbor = proc.getNeighbors().get(peerPrefix);
if (neighbor == null) {
throw new BatfishException("Expected a peer with address: " + peerAddress);
}
if (direction == Direction.IN) {
as = neighbor.getRemoteAs();
} else if (direction == Direction.OUT) {
as = neighbor.getLocalAs();
} else {
throw new BatfishException("Expected to be applied in a direction");
}
return as;
}
use of org.batfish.datamodel.BgpNeighbor in project batfish by batfish.
the class Encoder method initSlices.
/*
* Initialize each encoding slice.
* For iBGP, we also add reachability information for each pair of neighbors,
* to determine if messages sent to/from a neighbor will arrive.
*/
private void initSlices(HeaderSpace h, Graph g) {
if (g.getIbgpNeighbors().isEmpty() || !_modelIgp) {
_slices.put(MAIN_SLICE_NAME, new EncoderSlice(this, h, g, ""));
} else {
_slices.put(MAIN_SLICE_NAME, new EncoderSlice(this, h, g, MAIN_SLICE_NAME));
}
if (_modelIgp) {
SortedSet<Pair<String, Ip>> ibgpRouters = new TreeSet<>();
for (Entry<GraphEdge, BgpNeighbor> entry : g.getIbgpNeighbors().entrySet()) {
GraphEdge ge = entry.getKey();
BgpNeighbor n = entry.getValue();
String router = ge.getRouter();
Ip ip = n.getLocalIp();
Pair<String, Ip> pair = new Pair<>(router, ip);
// Add one slice per (router, source ip) pair
if (!ibgpRouters.contains(pair)) {
ibgpRouters.add(pair);
// Create a control plane slice only for this ip
HeaderSpace hs = new HeaderSpace();
// Make sure messages are sent to this destination IP
SortedSet<IpWildcard> ips = new TreeSet<>();
ips.add(new IpWildcard(n.getLocalIp()));
hs.setDstIps(ips);
// Make sure messages use TCP port 179
SortedSet<SubRange> dstPorts = new TreeSet<>();
dstPorts.add(new SubRange(179, 179));
hs.setDstPorts(dstPorts);
// Make sure messages use the TCP protocol
SortedSet<IpProtocol> protocols = new TreeSet<>();
protocols.add(IpProtocol.TCP);
hs.setIpProtocols(protocols);
// TODO: create domains once
Graph gNew = new Graph(g.getBatfish(), null, g.getDomain(router));
String sliceName = "SLICE-" + router + "_";
EncoderSlice slice = new EncoderSlice(this, hs, gNew, sliceName);
_slices.put(sliceName, slice);
PropertyAdder pa = new PropertyAdder(slice);
Map<String, BoolExpr> reachVars = pa.instrumentReachability(router);
_sliceReachability.put(router, reachVars);
}
}
}
}
Aggregations