@Override public Ip getEnd() { return _subnet.getEndIp(); }
/** * Returns the last ip in the prefix that is not a broadcast address. When the prefix is /32 or * /31, returns the getEndIp(), otherwise, returns the ip before getEndIp(). */ public Ip getLastHostIp() { if (_prefixLength >= Prefix.MAX_PREFIX_LENGTH - 1) { return getEndIp(); } else { Ip broadcastIp = getEndIp(); return Ip.create(broadcastIp.asLong() - 1); } }
public NatPool() { _fromAddress = Prefix.ZERO.getStartIp(); _toAddress = Prefix.ZERO.getEndIp(); }
Ip getNextIp() { for (Long ipAsLong = _lastGeneratedIp + 1; ipAsLong < _cidrBlock.getEndIp().asLong(); ipAsLong++) { if (!_allocatedIps.contains(ipAsLong)) { _allocatedIps.add(ipAsLong); _lastGeneratedIp = ipAsLong; return Ip.create(ipAsLong); } } // subnet's CIDR block out of IPs throw new BatfishException(String.format("%s subnet ran out of IPs", _subnetId)); }
public static boolean overlap(Prefix p1, Prefix p2) { long l1 = p1.getStartIp().asLong(); long l2 = p2.getStartIp().asLong(); long u1 = p1.getEndIp().asLong(); long u2 = p2.getEndIp().asLong(); return (l1 >= l2 && l1 <= u2) || (u1 <= u2 && u1 >= l2) || (u2 >= l1 && u2 <= u1) || (l2 >= l1 && l2 <= u1); }
ImmutableMap.of(r1.getNextHopIp(), ImmutableSet.of(ifaceRoute))))) .build())); _someoneReplies = ImmutableMap.of(c1, ImmutableMap.of(i1, P2.getEndIp().toIpSpace())); ForwardingAnalysisImpl forwardingAnalysisImpl = initForwardingAnalysisImpl(); Map<String, Map<String, Map<String, Set<AbstractRoute>>>> result =
@Test public void testComputeNeighborUnreachableOrExitsNetwork() { String c1 = "c1"; String v1 = "v1"; String i1 = "i1"; _arpFalseDestIp = ImmutableMap.of(c1, ImmutableMap.of(v1, ImmutableMap.of(i1, P1.getStartIp().toIpSpace()))); _arpFalseNextHopIp = ImmutableMap.of(c1, ImmutableMap.of(v1, ImmutableMap.of(i1, P1.getEndIp().toIpSpace()))); ForwardingAnalysisImpl forwardingAnalysisImpl = initForwardingAnalysisImpl(); Map<String, Map<String, Map<String, IpSpace>>> result = forwardingAnalysisImpl.computeArpFalse(); /* Should contain both IPs. */ assertThat( result, hasEntry( equalTo(c1), hasEntry(equalTo(v1), hasEntry(equalTo(i1), containsIp(P1.getStartIp()))))); assertThat( result, hasEntry( equalTo(c1), hasEntry(equalTo(v1), hasEntry(equalTo(i1), containsIp(P1.getEndIp()))))); /* Should not contain unrelated IPs. */ assertThat( result, hasEntry( equalTo(c1), hasEntry(equalTo(v1), hasEntry(equalTo(i1), not(containsIp(P2.getEndIp())))))); }
@Test public void testComputeSomeoneReplies() { String c1 = "c1"; String i1 = "i1"; String c2 = "c2"; String i2 = "i2"; Edge e1 = Edge.of(c1, i1, c2, i2); _arpReplies = ImmutableMap.of(c2, ImmutableMap.of(i2, P1.toIpSpace())); Topology topology = new Topology(ImmutableSortedSet.of(e1)); ForwardingAnalysisImpl forwardingAnalysisImpl = initForwardingAnalysisImpl(); Map<String, Map<String, IpSpace>> result = forwardingAnalysisImpl.computeSomeoneReplies(topology); /* IPs allowed by neighbor should appear */ assertThat(result, hasEntry(equalTo(c1), hasEntry(equalTo(i1), containsIp(P1.getStartIp())))); assertThat(result, hasEntry(equalTo(c1), hasEntry(equalTo(i1), containsIp(P1.getEndIp())))); /* IPs not allowed by neighbor should not appear */ assertThat( result, hasEntry(equalTo(c1), hasEntry(equalTo(i1), not(containsIp(P2.getStartIp()))))); }
_routesWhereDstIpCanBeArpIp = ImmutableMap.of(c1, ImmutableMap.of(v1, ImmutableMap.of(i1, ImmutableSet.of(ifaceRoute)))); _someoneReplies = ImmutableMap.of(c1, ImmutableMap.of(i1, P1.getEndIp().toIpSpace())); ForwardingAnalysisImpl forwardingAnalysisImpl = initForwardingAnalysisImpl(); Map<String, Map<String, Map<String, IpSpace>>> result = hasEntry( equalTo(c1), hasEntry(equalTo(v1), hasEntry(equalTo(i1), not(containsIp(P1.getEndIp())))))); hasEntry( equalTo(c1), hasEntry(equalTo(v1), hasEntry(equalTo(i1), not(containsIp(P2.getEndIp()))))));
StaticRoute.builder() .setNetwork(P1) .setNextHopIp(P2.getEndIp()) .setAdministrativeCost(1) .build(); ImmutableMap.of(r2.getNextHopIp(), ImmutableSet.of(ifaceRoute))))) .build())); _someoneReplies = ImmutableMap.of(c1, ImmutableMap.of(i1, P2.getEndIp().toIpSpace())); ForwardingAnalysisImpl forwardingAnalysisImpl = initForwardingAnalysisImpl(); Map<Edge, Set<AbstractRoute>> result =
/** * Returns an {@link IpSpace} that contains this prefix except for the network and broadcast * addresses, if applicable. Following RFC 3021, the entire {@code /31} is treated as host IP * space. A prefix of length {@code /32} is preserved, though may be a degenerate case. */ public IpSpace toHostIpSpace() { if (_prefixLength >= Prefix.MAX_PREFIX_LENGTH - 1) { return toIpSpace(); } return AclIpSpace.builder() .thenRejecting(getStartIp().toIpSpace()) .thenRejecting(getEndIp().toIpSpace()) .thenPermitting(toIpSpace()) .build(); }
assertThat(result, hasEntry(equalTo(c1), hasEntry(equalTo(i1), containsIp(P1.getEndIp()))));
assertThat(noProxyArpResult, containsIp(P2.getStartIp())); assertThat(noProxyArpResult, not(containsIp(P1.getEndIp()))); assertThat(noProxyArpResult, not(containsIp(P2.getEndIp()))); assertThat(noProxyArpResult, not(containsIp(P3.getStartIp()))); assertThat(proxyArpResult, containsIp(P2.getStartIp())); assertThat(proxyArpResult, not(containsIp(P1.getEndIp()))); assertThat(proxyArpResult, not(containsIp(P2.getEndIp())));
@Test public void testComputeIpsAssignedToThisInterface() { Configuration config = _cb.build(); Map<String, Configuration> configs = ImmutableMap.of(config.getHostname(), config); _ib.setOwner(config); InterfaceAddress primary = new InterfaceAddress(P1.getStartIp(), P1.getPrefixLength()); InterfaceAddress secondary = new InterfaceAddress(P2.getStartIp(), P2.getPrefixLength()); Interface i = _ib.setAddresses(primary, secondary).build(); _interfaceOwnedIps = TopologyUtil.computeInterfaceOwnedIps(configs, false); ForwardingAnalysisImpl forwardingAnalysisImpl = initForwardingAnalysisImpl(); IpSpace result = forwardingAnalysisImpl.computeIpsAssignedToThisInterface(i); assertThat(result, containsIp(P1.getStartIp())); assertThat(result, containsIp(P2.getStartIp())); assertThat(result, not(containsIp(P2.getEndIp()))); }
hasEntry( equalTo(vrf1.getName()), hasEntry(equalTo(i1.getName()), not(containsIp(P1.getEndIp()))))));
@Test public void testComputeInterfaceHostSubnetIpsWithPrefixLength31() { Configuration c1 = _cb.build(); Map<String, Configuration> configs = ImmutableMap.of(c1.getHostname(), c1); Vrf vrf1 = _vb.setOwner(c1).build(); Prefix prefix = Prefix.parse("1.0.0.1/31"); Interface i1 = _ib.setOwner(c1) .setVrf(vrf1) .setAddress(new InterfaceAddress(prefix.getStartIp(), prefix.getPrefixLength())) .build(); Map<String, Map<String, Map<String, IpSpace>>> interfaceHostSubnetIps = computeInterfaceHostSubnetIps(configs); assertThat( interfaceHostSubnetIps, hasEntry( equalTo(c1.getHostname()), hasEntry( equalTo(vrf1.getName()), hasEntry(equalTo(i1.getName()), containsIp(prefix.getStartIp()))))); assertThat( interfaceHostSubnetIps, hasEntry( equalTo(c1.getHostname()), hasEntry( equalTo(vrf1.getName()), hasEntry(equalTo(i1.getName()), containsIp(prefix.getEndIp()))))); }
Prefix p = Prefix.parse("1.0.0.0/31"); Ip ip1 = p.getStartIp(); Ip ip2 = p.getEndIp(); int pl = p.getPrefixLength();
.setAddress(new InterfaceAddress(P1.getEndIp(), P1.getPrefixLength())) .setVrrpGroups( ImmutableSortedMap.of(
.setVrf(dstVrf) .setAddress( new InterfaceAddress(LINK_1_NETWORK.getEndIp(), LINK_1_NETWORK.getPrefixLength())) .build(); .setVrf(dstVrf) .setAddress( new InterfaceAddress(LINK_2_NETWORK.getEndIp(), LINK_2_NETWORK.getPrefixLength())) .build(); srcVrf.setStaticRoutes( ImmutableSortedSet.of( bld.setNetwork(DST_PREFIX_1).setNextHopIp(LINK_1_NETWORK.getEndIp()).build(), bld.setNetwork(DST_PREFIX_2).setNextHopIp(LINK_2_NETWORK.getEndIp()).build()));