Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frr max med administrative #6086

Merged
merged 5 commits into from Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,6 +35,11 @@ ADDITIVE
'additive'
;

ADMINISTRATIVE
:
'administrative'
;

ADVERTISE
:
'advertise'
Expand Down Expand Up @@ -459,6 +464,11 @@ MAXIMUM_PATHS
'maximum-paths'
;

MAX_MED
:
'max-med'
;

MESSAGE_DIGEST
:
'message-digest'
Expand Down
Expand Up @@ -29,6 +29,7 @@ sb_bgp
| sbb_log_neighbor_changes
| sbb_router_id
| sbb_cluster_id
| sbb_max_med_administrative
)
;

Expand Down Expand Up @@ -76,6 +77,11 @@ sbb_log_neighbor_changes
LOG_NEIGHBOR_CHANGES NEWLINE
;

sbb_max_med_administrative
:
MAX_MED ADMINISTRATIVE (med = uint32)? NEWLINE
;

sbbb_aspath_multipath_relax
:
AS_PATH MULTIPATH_RELAX NEWLINE
Expand Down
Expand Up @@ -4,6 +4,7 @@
import static java.lang.Long.parseLong;
import static org.batfish.datamodel.Configuration.DEFAULT_VRF_NAME;
import static org.batfish.grammar.cumulus_frr.CumulusFrrParser.Int_exprContext;
import static org.batfish.representation.cumulus.CumulusConversions.DEFAULT_MAX_MED;
import static org.batfish.representation.cumulus.CumulusRoutingProtocol.CONNECTED;
import static org.batfish.representation.cumulus.CumulusRoutingProtocol.STATIC;
import static org.batfish.representation.cumulus.CumulusStructureType.ABSTRACT_INTERFACE;
Expand Down Expand Up @@ -113,6 +114,7 @@
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Sbafln_route_reflector_clientContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Sbb_cluster_idContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Sbb_confederationContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Sbb_max_med_administrativeContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Sbb_router_idContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Sbbb_aspath_multipath_relaxContext;
import org.batfish.grammar.cumulus_frr.CumulusFrrParser.Sbn_interfaceContext;
Expand Down Expand Up @@ -752,6 +754,15 @@ public void exitSbb_confederation(Sbb_confederationContext ctx) {
_currentBgpVrf.setConfederationId(id);
}

@Override
public void exitSbb_max_med_administrative(Sbb_max_med_administrativeContext ctx) {
if (ctx.med != null) {
_currentBgpVrf.setMaxMedAdministrative(Long.parseLong(ctx.med.getText()));
} else {
_currentBgpVrf.setMaxMedAdministrative(DEFAULT_MAX_MED);
}
}

@Override
public void enterSbn_ip(Sbn_ipContext ctx) {
String name = ctx.ip.getText();
Expand Down
Expand Up @@ -23,6 +23,7 @@ public class BgpVrf implements Serializable {
private @Nullable Ip _clusterId;
private final @Nonnull String _vrfName;
private @Nullable Long _confederationId;
private @Nullable Long _maxMedAdministrative;

public BgpVrf(String vrfName) {
// the default is true unless explicitly disabled (via "no bgp default ipv4-unicast")
Expand Down Expand Up @@ -53,6 +54,15 @@ public void setAsPathMultipathRelax(@Nullable Boolean asPathMultipathRelax) {
_asPathMultipathRelax = asPathMultipathRelax;
}

@Nullable
public Long getMaxMedAdministrative() {
return _maxMedAdministrative;
}

public void setMaxMedAdministrative(@Nullable Long maxMedAdministrative) {
_maxMedAdministrative = maxMedAdministrative;
}

public @Nullable Long getAutonomousSystem() {
return _autonomousSystem;
}
Expand Down
Expand Up @@ -107,6 +107,7 @@
import org.batfish.datamodel.routing_policy.expr.DestinationNetwork;
import org.batfish.datamodel.routing_policy.expr.Disjunction;
import org.batfish.datamodel.routing_policy.expr.ExplicitPrefixSet;
import org.batfish.datamodel.routing_policy.expr.LiteralLong;
import org.batfish.datamodel.routing_policy.expr.LiteralOrigin;
import org.batfish.datamodel.routing_policy.expr.MatchPrefixSet;
import org.batfish.datamodel.routing_policy.expr.MatchProtocol;
Expand All @@ -116,6 +117,7 @@
import org.batfish.datamodel.routing_policy.expr.WithEnvironmentExpr;
import org.batfish.datamodel.routing_policy.statement.CallStatement;
import org.batfish.datamodel.routing_policy.statement.If;
import org.batfish.datamodel.routing_policy.statement.SetMetric;
import org.batfish.datamodel.routing_policy.statement.SetNextHop;
import org.batfish.datamodel.routing_policy.statement.SetOrigin;
import org.batfish.datamodel.routing_policy.statement.Statement;
Expand All @@ -137,6 +139,8 @@ public final class CumulusConversions {

public static final Ip CLAG_LINK_LOCAL_IP = Ip.parse("169.254.40.94");

public static final long DEFAULT_MAX_MED = 4294967294L;

@VisibleForTesting
static GeneratedRoute GENERATED_DEFAULT_ROUTE =
GeneratedRoute.builder().setNetwork(Prefix.ZERO).setAdmin(MAX_ADMINISTRATIVE_COST).build();
Expand Down Expand Up @@ -750,10 +754,19 @@ private static BooleanExpr computePeerExportConditions(BgpNeighbor neighbor, Bgp
}

private static List<Statement> getAcceptStatements(BgpNeighbor neighbor, BgpVrf bgpVrf) {
ImmutableList.Builder<Statement> acceptStatements = ImmutableList.builder();
SetNextHop setNextHop = getSetNextHop(neighbor, bgpVrf);
return setNextHop == null
? ImmutableList.of(Statements.ExitAccept.toStaticStatement())
: ImmutableList.of(setNextHop, Statements.ExitAccept.toStaticStatement());
SetMetric setMaxMedMetric = getSetMaxMedMetric(bgpVrf);

if (setNextHop != null) {
acceptStatements.add(setNextHop);
}
if (setMaxMedMetric != null) {
acceptStatements.add(setMaxMedMetric);
}
acceptStatements.add(Statements.ExitAccept.toStaticStatement());

return acceptStatements.build();
}

private static @Nullable CallExpr getBgpNeighborExportPolicyCallExpr(BgpNeighbor neighbor) {
Expand Down Expand Up @@ -798,6 +811,13 @@ private static List<Statement> getAcceptStatements(BgpNeighbor neighbor, BgpVrf
return nextHopSelf ? new SetNextHop(SelfNextHop.getInstance()) : null;
}

@VisibleForTesting
static @Nullable SetMetric getSetMaxMedMetric(BgpVrf bgpVrf) {
return bgpVrf.getMaxMedAdministrative() != null
? new SetMetric(new LiteralLong(bgpVrf.getMaxMedAdministrative()))
: null;
}

@Nullable
@VisibleForTesting
static Ip resolveLocalIpFromUpdateSource(
Expand Down
Expand Up @@ -688,6 +688,25 @@ public void testBgpBestpathAsPathMultipathRelax() {
assertTrue(_frr.getBgpProcess().getDefaultVrf().getAsPathMultipathRelax());
}

@Test
public void testBgpMaxMedAdministrative_value() {
parse("router bgp 1\n bgp max-med administrative 12345\n");
assertThat(_frr.getBgpProcess().getDefaultVrf().getMaxMedAdministrative(), equalTo(12345L));
}

@Test
public void testBgpMaxMedAdministrative_default() {
parse("router bgp 1\n bgp max-med administrative\n");
assertThat(
_frr.getBgpProcess().getDefaultVrf().getMaxMedAdministrative(), equalTo(4294967294L));
}

@Test
public void testBgpMaxMedAdministrative_unset() {
parse("router bgp 1\n");
assertThat(_frr.getBgpProcess().getDefaultVrf().getMaxMedAdministrative(), equalTo(null));
}

@Test
public void testBgpRouterId() {
parse("router bgp 1\n bgp router-id 1.2.3.4\n");
Expand Down
Expand Up @@ -5,6 +5,7 @@
import static org.batfish.common.Warnings.TAG_RED_FLAG;
import static org.batfish.datamodel.Configuration.DEFAULT_VRF_NAME;
import static org.batfish.datamodel.InterfaceType.PHYSICAL;
import static org.batfish.representation.cumulus.CumulusConversions.DEFAULT_MAX_MED;
import static org.batfish.representation.cumulus.CumulusConversions.GENERATED_DEFAULT_ROUTE;
import static org.batfish.representation.cumulus.CumulusConversions.REJECT_DEFAULT_ROUTE;
import static org.batfish.representation.cumulus.CumulusConversions.addBgpNeighbor;
Expand All @@ -22,6 +23,7 @@
import static org.batfish.representation.cumulus.CumulusConversions.generateExportAggregateConditions;
import static org.batfish.representation.cumulus.CumulusConversions.generateGeneratedRoutes;
import static org.batfish.representation.cumulus.CumulusConversions.generateGenerationPolicy;
import static org.batfish.representation.cumulus.CumulusConversions.getSetMaxMedMetric;
import static org.batfish.representation.cumulus.CumulusConversions.getSetNextHop;
import static org.batfish.representation.cumulus.CumulusConversions.inferClusterId;
import static org.batfish.representation.cumulus.CumulusConversions.inferPeerIp;
Expand Down Expand Up @@ -96,11 +98,14 @@
import org.batfish.datamodel.routing_policy.Result;
import org.batfish.datamodel.routing_policy.RoutingPolicy;
import org.batfish.datamodel.routing_policy.expr.BooleanExpr;
import org.batfish.datamodel.routing_policy.expr.CallExpr;
import org.batfish.datamodel.routing_policy.expr.LiteralCommunity;
import org.batfish.datamodel.routing_policy.expr.LiteralLong;
import org.batfish.datamodel.routing_policy.expr.MatchCommunitySet;
import org.batfish.datamodel.routing_policy.expr.Not;
import org.batfish.datamodel.routing_policy.expr.SelfNextHop;
import org.batfish.datamodel.routing_policy.statement.If;
import org.batfish.datamodel.routing_policy.statement.SetMetric;
import org.batfish.datamodel.routing_policy.statement.SetNextHop;
import org.batfish.datamodel.routing_policy.statement.Statement;
import org.batfish.datamodel.routing_policy.statement.Statements;
Expand Down Expand Up @@ -1835,4 +1840,82 @@ public void testNextHopSelfAll() {
assertThat(getSetNextHop(bgpNeighbor, bgpVrf), equalTo(null));
}
}

@Test
public void testGetSetMaxMedMetric() {
BgpVrf bgpVrf = new BgpVrf("bgpVrf");

// Test Default Value
assertThat(getSetMaxMedMetric(bgpVrf), equalTo(null));

// Set the value and test again
bgpVrf.setMaxMedAdministrative(1234L);
assertThat(getSetMaxMedMetric(bgpVrf), equalTo(new SetMetric(new LiteralLong(1234L))));
}

@Test
public void testGenerateBgpCommonPeerConfigMaxMedAdministrative() {
// setup VI model
NetworkFactory nf = new NetworkFactory();
Configuration viConfig =
nf.configurationBuilder().setConfigurationFormat(ConfigurationFormat.CUMULUS_NCLU).build();
nf.vrfBuilder().setOwner(viConfig).setName(DEFAULT_VRF_NAME).build();

// setup VS model
CumulusNcluConfiguration vsConfig = new CumulusNcluConfiguration();
BgpProcess bgpProcess = new BgpProcess();
vsConfig.setBgpProcess(bgpProcess);
vsConfig.setConfiguration(viConfig);

// setup BgpVrf and BgpNeighbor
BgpVrf vrf = bgpProcess.getDefaultVrf();
vrf.setRouterId(Ip.parse("1.1.1.1"));
vrf.setAutonomousSystem(20000L);
BgpNeighbor bgpNeighbor = new BgpIpNeighbor("10.0.0.1");
bgpNeighbor.setRemoteAs(10000L);
bgpNeighbor.setRemoteAsType(RemoteAsType.EXTERNAL);
vrf.getNeighbors().put("10.0.0.1", bgpNeighbor);

org.batfish.datamodel.BgpProcess newProc =
new org.batfish.datamodel.BgpProcess(
Ip.parse("10.0.0.1"), ConfigurationFormat.CUMULUS_NCLU);

BgpActivePeerConfig.Builder peerConfigBuilder =
BgpActivePeerConfig.builder().setPeerAddress(Ip.parse("10.0.0.1"));

// Method under test
generateBgpCommonPeerConfig(
viConfig, vsConfig, bgpNeighbor, 10000L, vrf, newProc, peerConfigBuilder, new Warnings());

// Test that by default, we don't set a metric

assertEquals(
viConfig
.getRoutingPolicies()
.get(computeBgpPeerExportPolicyName(DEFAULT_VRF_NAME, bgpNeighbor.getName()))
.getStatements(),
ImmutableList.of(
new If(
new CallExpr(computeBgpCommonExportPolicyName(DEFAULT_VRF_NAME)),
ImmutableList.of(Statements.ExitAccept.toStaticStatement()),
ImmutableList.of(Statements.ExitReject.toStaticStatement()))));

// Set max-med admin on the vrf, regenerate and test again
vrf.setMaxMedAdministrative(DEFAULT_MAX_MED);
generateBgpCommonPeerConfig(
viConfig, vsConfig, bgpNeighbor, 10000L, vrf, newProc, peerConfigBuilder, new Warnings());

assertEquals(
viConfig
.getRoutingPolicies()
.get(computeBgpPeerExportPolicyName(DEFAULT_VRF_NAME, bgpNeighbor.getName()))
.getStatements(),
ImmutableList.of(
new If(
new CallExpr(computeBgpCommonExportPolicyName(DEFAULT_VRF_NAME)),
ImmutableList.of(
new SetMetric(new LiteralLong(DEFAULT_MAX_MED)),
Statements.ExitAccept.toStaticStatement()),
ImmutableList.of(Statements.ExitReject.toStaticStatement()))));
}
}