use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.
the class BgpUpdate method parsePackedIp4Prefixes.
/**
* Parses a message that contains encoded IPv4 network prefixes.
* <p>
* The IPv4 prefixes are encoded in the form:
* <Length, Prefix> where Length is the length in bits of the IPv4 prefix,
* and Prefix is the IPv4 prefix (padded with trailing bits to the end
* of an octet).
*
* @param totalLength the total length of the data to parse
* @param message the message with data to parse
* @return a collection of parsed IPv4 network prefixes
* @throws BgpMessage.BgpParseException
*/
private static Collection<Ip4Prefix> parsePackedIp4Prefixes(int totalLength, ChannelBuffer message) throws BgpMessage.BgpParseException {
Collection<Ip4Prefix> result = new ArrayList<>();
if (totalLength == 0) {
return result;
}
// Parse the data
byte[] buffer = new byte[Ip4Address.BYTE_LENGTH];
int dataEnd = message.readerIndex() + totalLength;
while (message.readerIndex() < dataEnd) {
int prefixBitlen = message.readUnsignedByte();
// Round-up
int prefixBytelen = (prefixBitlen + 7) / 8;
if (message.readerIndex() + prefixBytelen > dataEnd) {
String errorMsg = "Malformed Network Prefixes";
throw new BgpMessage.BgpParseException(errorMsg);
}
message.readBytes(buffer, 0, prefixBytelen);
Ip4Prefix prefix = Ip4Prefix.valueOf(Ip4Address.valueOf(buffer), prefixBitlen);
result.add(prefix);
}
return result;
}
use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.
the class BgpSession method addBgpRoute.
/**
* Adds a BGP route. The route can be either IPv4 or IPv6.
*
* @param bgpRouteEntry the BGP route entry to use
*/
void addBgpRoute(BgpRouteEntry bgpRouteEntry) {
if (bgpRouteEntry.isIp4()) {
// IPv4 route
Ip4Prefix ip4Prefix = bgpRouteEntry.prefix().getIp4Prefix();
bgpRibIn4.put(ip4Prefix, bgpRouteEntry);
} else {
// IPv6 route
Ip6Prefix ip6Prefix = bgpRouteEntry.prefix().getIp6Prefix();
bgpRibIn6.put(ip6Prefix, bgpRouteEntry);
}
}
use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.
the class BgpSessionManagerTest method testBgpRoutePreference.
/**
* Tests the BGP route preference.
*/
@Test
public void testBgpRoutePreference() throws InterruptedException {
ChannelBuffer message;
BgpRouteEntry bgpRouteEntry;
Collection<BgpRouteEntry> bgpRibIn1;
Collection<BgpRouteEntry> bgpRibIn2;
Collection<BgpRouteEntry> bgpRibIn3;
Collection<BgpRouteEntry> bgpRoutes;
Collection<Ip4Prefix> addedRoutes = new LinkedList<>();
Collection<Ip4Prefix> withdrawnRoutes = new LinkedList<>();
// Initiate the connections
peer1.connect(connectToSocket);
peer2.connect(connectToSocket);
peer3.connect(connectToSocket);
//
// Setup the initial set of routes to Peer1
//
addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8"));
addedRoutes.add(Ip4Prefix.valueOf("30.0.0.0/16"));
// Write the routes
message = peer1.peerChannelHandler.prepareBgpUpdate(NEXT_HOP1_ROUTER, DEFAULT_LOCAL_PREF, DEFAULT_MULTI_EXIT_DISC, asPathLong, addedRoutes, withdrawnRoutes);
peer1.peerChannelHandler.savedCtx.getChannel().write(message);
bgpRoutes = waitForBgpRoutes(2);
assertThat(bgpRoutes, hasSize(2));
//
// Add a route entry to Peer2 with a better LOCAL_PREF
//
addedRoutes = new LinkedList<>();
withdrawnRoutes = new LinkedList<>();
addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8"));
// Write the routes
message = peer2.peerChannelHandler.prepareBgpUpdate(NEXT_HOP2_ROUTER, BETTER_LOCAL_PREF, DEFAULT_MULTI_EXIT_DISC, asPathLong, addedRoutes, withdrawnRoutes);
peer2.peerChannelHandler.savedCtx.getChannel().write(message);
//
// Check that the routes have been received, processed and stored
//
bgpRibIn2 = waitForBgpRibIn(bgpSession2, 1);
assertThat(bgpRibIn2, hasSize(1));
bgpRoutes = waitForBgpRoutes(2);
assertThat(bgpRoutes, hasSize(2));
//
bgpRouteEntry = new BgpRouteEntry(bgpSession2, Ip4Prefix.valueOf("20.0.0.0/8"), NEXT_HOP2_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathLong, BETTER_LOCAL_PREF);
bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
assertThat(bgpRibIn2, hasBgpRouteEntry(bgpRouteEntry));
assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
//
// Add a route entry to Peer3 with a shorter AS path
//
addedRoutes = new LinkedList<>();
withdrawnRoutes = new LinkedList<>();
addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8"));
// Write the routes
message = peer3.peerChannelHandler.prepareBgpUpdate(NEXT_HOP3_ROUTER, BETTER_LOCAL_PREF, DEFAULT_MULTI_EXIT_DISC, asPathShort, addedRoutes, withdrawnRoutes);
peer3.peerChannelHandler.savedCtx.getChannel().write(message);
//
// Check that the routes have been received, processed and stored
//
bgpRibIn3 = waitForBgpRibIn(bgpSession3, 1);
assertThat(bgpRibIn3, hasSize(1));
bgpRoutes = waitForBgpRoutes(2);
assertThat(bgpRoutes, hasSize(2));
//
bgpRouteEntry = new BgpRouteEntry(bgpSession3, Ip4Prefix.valueOf("20.0.0.0/8"), NEXT_HOP3_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathShort, BETTER_LOCAL_PREF);
bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC);
assertThat(bgpRibIn3, hasBgpRouteEntry(bgpRouteEntry));
assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
//
// Cleanup in preparation for next test: delete old route entry from
// Peer2
//
addedRoutes = new LinkedList<>();
withdrawnRoutes = new LinkedList<>();
withdrawnRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8"));
// Write the routes
message = peer2.peerChannelHandler.prepareBgpUpdate(NEXT_HOP2_ROUTER, BETTER_LOCAL_PREF, BETTER_MULTI_EXIT_DISC, asPathShort, addedRoutes, withdrawnRoutes);
peer2.peerChannelHandler.savedCtx.getChannel().write(message);
//
// Check that the routes have been received, processed and stored
//
bgpRibIn2 = waitForBgpRibIn(bgpSession2, 0);
assertThat(bgpRibIn2, hasSize(0));
//
// Add a route entry to Peer2 with a better MED
//
addedRoutes = new LinkedList<>();
withdrawnRoutes = new LinkedList<>();
addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8"));
// Write the routes
message = peer2.peerChannelHandler.prepareBgpUpdate(NEXT_HOP2_ROUTER, BETTER_LOCAL_PREF, BETTER_MULTI_EXIT_DISC, asPathShort, addedRoutes, withdrawnRoutes);
peer2.peerChannelHandler.savedCtx.getChannel().write(message);
//
// Check that the routes have been received, processed and stored
//
bgpRibIn2 = waitForBgpRibIn(bgpSession2, 1);
assertThat(bgpRibIn2, hasSize(1));
bgpRoutes = waitForBgpRoutes(2);
assertThat(bgpRoutes, hasSize(2));
//
bgpRouteEntry = new BgpRouteEntry(bgpSession2, Ip4Prefix.valueOf("20.0.0.0/8"), NEXT_HOP2_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathShort, BETTER_LOCAL_PREF);
bgpRouteEntry.setMultiExitDisc(BETTER_MULTI_EXIT_DISC);
assertThat(bgpRibIn2, hasBgpRouteEntry(bgpRouteEntry));
assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
//
// Add a route entry to Peer1 with a better (lower) BGP ID
//
addedRoutes = new LinkedList<>();
withdrawnRoutes = new LinkedList<>();
addedRoutes.add(Ip4Prefix.valueOf("20.0.0.0/8"));
withdrawnRoutes.add(Ip4Prefix.valueOf("30.0.0.0/16"));
// Write the routes
message = peer1.peerChannelHandler.prepareBgpUpdate(NEXT_HOP1_ROUTER, BETTER_LOCAL_PREF, BETTER_MULTI_EXIT_DISC, asPathShort, addedRoutes, withdrawnRoutes);
peer1.peerChannelHandler.savedCtx.getChannel().write(message);
//
// Check that the routes have been received, processed and stored
//
bgpRibIn1 = waitForBgpRibIn(bgpSession1, 1);
assertThat(bgpRibIn1, hasSize(1));
bgpRoutes = waitForBgpRoutes(1);
assertThat(bgpRoutes, hasSize(1));
//
bgpRouteEntry = new BgpRouteEntry(bgpSession1, Ip4Prefix.valueOf("20.0.0.0/8"), NEXT_HOP1_ROUTER, (byte) BgpConstants.Update.Origin.IGP, asPathShort, BETTER_LOCAL_PREF);
bgpRouteEntry.setMultiExitDisc(BETTER_MULTI_EXIT_DISC);
assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry));
assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue());
// Close the channels and test there are no routes
peer1.peerChannelHandler.closeChannel();
peer2.peerChannelHandler.closeChannel();
peer3.peerChannelHandler.closeChannel();
bgpRoutes = waitForBgpRoutes(0);
assertThat(bgpRoutes, hasSize(0));
}
use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.
the class RouteEntryTest method testInvalidConstructorNullIpv4Prefix.
/**
* Tests invalid class constructor for null IPv4 prefix.
*/
@Test(expected = NullPointerException.class)
public void testInvalidConstructorNullIpv4Prefix() {
Ip4Prefix prefix = null;
Ip4Address nextHop = Ip4Address.valueOf("5.6.7.8");
new RouteEntry(prefix, nextHop);
}
use of org.onlab.packet.Ip4Prefix in project onos by opennetworkinglab.
the class RouteEntryTest method testNonEquality.
/**
* Tests non-equality of {@link RouteEntry}.
*/
@Test
public void testNonEquality() {
Ip4Prefix prefix1 = Ip4Prefix.valueOf("1.2.3.0/24");
Ip4Address nextHop1 = Ip4Address.valueOf("5.6.7.8");
RouteEntry routeEntry1 = new RouteEntry(prefix1, nextHop1);
// Different
Ip4Prefix prefix2 = Ip4Prefix.valueOf("1.2.3.0/25");
Ip4Address nextHop2 = Ip4Address.valueOf("5.6.7.8");
RouteEntry routeEntry2 = new RouteEntry(prefix2, nextHop2);
Ip4Prefix prefix3 = Ip4Prefix.valueOf("1.2.3.0/24");
// Different
Ip4Address nextHop3 = Ip4Address.valueOf("5.6.7.9");
RouteEntry routeEntry3 = new RouteEntry(prefix3, nextHop3);
assertThat(routeEntry1, Matchers.is(Matchers.not(routeEntry2)));
assertThat(routeEntry1, Matchers.is(Matchers.not(routeEntry3)));
Ip6Prefix prefix4 = Ip6Prefix.valueOf("1000::/64");
Ip6Address nextHop4 = Ip6Address.valueOf("2000::1");
RouteEntry routeEntry4 = new RouteEntry(prefix4, nextHop4);
Ip6Prefix prefix5 = Ip6Prefix.valueOf("1000::/65");
Ip6Address nextHop5 = Ip6Address.valueOf("2000::1");
RouteEntry routeEntry5 = new RouteEntry(prefix5, nextHop5);
Ip6Prefix prefix6 = Ip6Prefix.valueOf("1000::/64");
Ip6Address nextHop6 = Ip6Address.valueOf("2000::2");
RouteEntry routeEntry6 = new RouteEntry(prefix6, nextHop6);
assertThat(routeEntry4, Matchers.is(Matchers.not(routeEntry5)));
assertThat(routeEntry4, Matchers.is(Matchers.not(routeEntry6)));
}
Aggregations