use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class TestBgpPeerChannelHandler method encodePackedPrefixes.
/**
* Encodes a collection of IPv4 network prefixes in a packed format.
* <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 prefixes the prefixes to encode
* @return the buffer with the encoded prefixes
*/
private ChannelBuffer encodePackedPrefixes(Collection<Ip4Prefix> prefixes) {
ChannelBuffer message = ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH);
// Write each of the prefixes
for (Ip4Prefix prefix : prefixes) {
int prefixBitlen = prefix.prefixLength();
// Round-up
int prefixBytelen = (prefixBitlen + 7) / 8;
message.writeByte(prefixBitlen);
Ip4Address address = prefix.address();
long value = address.toInt() & 0xffffffffL;
for (int i = 0; i < Ip4Address.BYTE_LENGTH; i++) {
if (prefixBytelen-- == 0) {
break;
}
long nextByte = (value >> ((Ip4Address.BYTE_LENGTH - i - 1) * 8)) & 0xff;
message.writeByte((int) nextByte);
}
}
return message;
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class FpmManager method sendRouteUpdateToChannel.
private void sendRouteUpdateToChannel(boolean isAdd, IpPrefix prefix, Channel ch) {
if (!pdPushEnabled) {
return;
}
int raLength;
short addrFamily;
// Build route attributes.
if (prefix.isIp4()) {
List<Ip4Address> pdPushNextHopList;
if (pdPushNextHopIPv4 == null || pdPushNextHopIPv4.size() == 0) {
log.info("Prefix not pushed because ipv4 next-hop is null.");
return;
}
pdPushNextHopList = pdPushNextHopIPv4;
raLength = Ip4Address.BYTE_LENGTH + RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH;
addrFamily = RtNetlink.RT_ADDRESS_FAMILY_INET;
for (Ip4Address pdPushNextHop : pdPushNextHopList) {
log.trace("IPv4 next hop is:" + pdPushNextHop);
updateRoute(pdPushNextHop, isAdd, prefix, ch, raLength, addrFamily);
}
} else {
List<Ip6Address> pdPushNextHopList;
if (pdPushNextHopIPv6 == null || pdPushNextHopIPv6.size() == 0) {
log.info("Prefix not pushed because ipv6 next-hop is null.");
return;
}
pdPushNextHopList = pdPushNextHopIPv6;
raLength = Ip6Address.BYTE_LENGTH + RouteAttribute.ROUTE_ATTRIBUTE_HEADER_LENGTH;
addrFamily = RtNetlink.RT_ADDRESS_FAMILY_INET6;
for (Ip6Address pdPushNextHop : pdPushNextHopList) {
log.trace("IPv6 next hop is:" + pdPushNextHop);
updateRoute(pdPushNextHop, isAdd, prefix, ch, raLength, addrFamily);
}
}
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class FpmManager method modified.
@Modified
protected void modified(ComponentContext context) {
Ip4Address rurIPv4Address;
Ip6Address rurIPv6Address;
Dictionary<?, ?> properties = context.getProperties();
if (properties == null) {
return;
}
String strClearRoutes = Tools.get(properties, CLEAR_ROUTES);
if (strClearRoutes != null) {
clearRoutes = Boolean.parseBoolean(strClearRoutes);
log.info("clearRoutes is {}", clearRoutes);
}
String strPdPushEnabled = Tools.get(properties, PD_PUSH_ENABLED);
if (strPdPushEnabled != null) {
boolean oldValue = pdPushEnabled;
pdPushEnabled = Boolean.parseBoolean(strPdPushEnabled);
if (pdPushEnabled) {
pdPushNextHopIPv4 = new ArrayList<Ip4Address>();
pdPushNextHopIPv6 = new ArrayList<Ip6Address>();
String strPdPushNextHopIPv4 = Tools.get(properties, PD_PUSH_NEXT_HOP_IPV4);
if (strPdPushNextHopIPv4 != null) {
List<String> strPdPushNextHopIPv4List = Arrays.asList(strPdPushNextHopIPv4.split(","));
for (String nextHop : strPdPushNextHopIPv4List) {
log.trace("IPv4 next hop added is:" + nextHop);
pdPushNextHopIPv4.add(Ip4Address.valueOf(nextHop));
}
}
String strPdPushNextHopIPv6 = Tools.get(properties, PD_PUSH_NEXT_HOP_IPV6);
if (strPdPushNextHopIPv6 != null) {
List<String> strPdPushNextHopIPv6List = Arrays.asList(strPdPushNextHopIPv6.split(","));
for (String nextHop : strPdPushNextHopIPv6List) {
log.trace("IPv6 next hop added is:" + nextHop);
pdPushNextHopIPv6.add(Ip6Address.valueOf(nextHop));
}
}
if (pdPushNextHopIPv4.size() == 0) {
rurIPv4Address = interfaceService.getInterfaces().stream().filter(iface -> iface.name().contains("RUR")).map(Interface::ipAddressesList).flatMap(Collection::stream).map(InterfaceIpAddress::ipAddress).filter(IpAddress::isIp4).map(IpAddress::getIp4Address).findFirst().orElse(null);
log.debug("RUR IPv4 address extracted from netcfg is: {}", rurIPv4Address);
if (rurIPv4Address != null) {
pdPushNextHopIPv4.add(rurIPv4Address);
} else {
log.debug("Unable to extract RUR IPv4 address from netcfg");
}
}
if (pdPushNextHopIPv6 == null || pdPushNextHopIPv6.size() == 0) {
rurIPv6Address = interfaceService.getInterfaces().stream().filter(iface -> iface.name().contains("RUR")).map(Interface::ipAddressesList).flatMap(Collection::stream).map(InterfaceIpAddress::ipAddress).filter(IpAddress::isIp6).map(IpAddress::getIp6Address).findFirst().orElse(null);
log.debug("RUR IPv6 address extracted from netcfg is: {}", rurIPv6Address);
if (rurIPv6Address != null) {
pdPushNextHopIPv6.add(rurIPv6Address);
} else {
log.debug("Unable to extract RUR IPv6 address from netcfg");
}
}
log.info("PD pushing is enabled.");
if (pdPushNextHopIPv4.size() != 0) {
log.info("ipv4 next-hop {} with {} items", pdPushNextHopIPv4.toString(), pdPushNextHopIPv4.size());
} else {
log.info("ipv4 next-hop is null");
}
if (pdPushNextHopIPv6.size() != 0) {
log.info("ipv6 next-hop={} with {} items", pdPushNextHopIPv6.toString(), pdPushNextHopIPv6.size());
} else {
log.info("ipv6 next-hop is null");
}
processStaticRoutes();
} else {
log.info("PD pushing is disabled.");
}
}
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class Dhcp4HandlerImpl method processDhcpPacketFromServer.
/**
* Build the DHCP offer/ack with proper client port.
*
* @param ethernetPacket the original packet comes from server
* @return new packet which will send to the client
*/
private InternalPacket processDhcpPacketFromServer(PacketContext context, Ethernet ethernetPacket) {
// get dhcp header.
Ethernet etherReply = (Ethernet) ethernetPacket.clone();
IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
UDP udpPacket = (UDP) ipv4Packet.getPayload();
DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
// determine the vlanId of the client host - note that this vlan id
// could be different from the vlan in the packet from the server
Interface clientInterface = getClientInterface(ethernetPacket, dhcpPayload).orElse(null);
if (clientInterface == null) {
log.warn("Cannot find the interface for the DHCP {}", dhcpPayload);
return null;
}
VlanId vlanId;
ConnectPoint inPort = context.inPacket().receivedFrom();
boolean directConnFlag = directlyConnected(dhcpPayload);
DhcpServerInfo foundServerInfo = findServerInfoFromServer(directConnFlag, inPort);
if (foundServerInfo == null) {
log.warn("Cannot find server info for {} server, inPort {}", directConnFlag ? "direct" : "indirect", inPort);
return null;
} else {
if (Dhcp4HandlerUtil.isServerIpEmpty(foundServerInfo)) {
log.warn("Cannot find server info's ipaddress");
return null;
}
}
if (clientInterface.vlanTagged().isEmpty()) {
vlanId = clientInterface.vlan();
} else {
// might be multiple vlan in same interface
vlanId = getVlanIdFromRelayAgentOption(dhcpPayload);
}
if (vlanId == null) {
vlanId = VlanId.NONE;
}
etherReply.setVlanID(vlanId.toShort());
etherReply.setSourceMACAddress(clientInterface.mac());
if (!directlyConnected(dhcpPayload)) {
// if client is indirectly connected, try use next hop mac address
MacAddress macAddress = MacAddress.valueOf(dhcpPayload.getClientHardwareAddress());
HostId hostId = HostId.hostId(macAddress, vlanId);
if (((int) dhcpPayload.getFlags() & 0x8000) == 0x0000) {
DhcpRecord record = dhcpRelayStore.getDhcpRecord(hostId).orElse(null);
if (record != null) {
// if next hop can be found, use mac address of next hop
record.nextHop().ifPresent(etherReply::setDestinationMACAddress);
} else {
// otherwise, discard the packet
log.warn("Can't find record for host id {}, discard packet", hostId);
return null;
}
} else {
etherReply.setDestinationMACAddress(MacAddress.BROADCAST);
}
} else {
etherReply.setDestinationMACAddress(dhcpPayload.getClientHardwareAddress());
}
Ip4Address ipFacingClient = getFirstIpFromInterface(clientInterface);
if (directlyConnected(dhcpPayload)) {
// figure out the relay agent IP corresponding to the original request
if (ipFacingClient == null) {
log.warn("Cannot determine relay agent interface Ipv4 addr for host {}/{}. " + "Aborting relay for dhcp packet from server {}", etherReply.getDestinationMAC(), clientInterface.vlan(), ethernetPacket);
return null;
}
// SRC_IP: IP facing client
ipv4Packet.setSourceAddress(ipFacingClient.toInt());
} else {
// Get the IP address of the relay agent
Ip4Address relayAgentIp = foundServerInfo.getRelayAgentIp4(clientInterface.connectPoint().deviceId()).orElse(null);
if (relayAgentIp == null) {
if (ipFacingClient == null) {
log.warn("Cannot determine relay agent interface Ipv4 addr for host {}/{}. " + "Aborting relay for dhcp packet from server for indirect host {}", etherReply.getDestinationMAC(), clientInterface.vlan(), ethernetPacket);
return null;
} else {
// SRC_IP: IP facing client
ipv4Packet.setSourceAddress(ipFacingClient.toInt());
}
} else {
// SRC_IP: relay agent IP
ipv4Packet.setSourceAddress(relayAgentIp.toInt());
}
}
// DST_IP: offered IP
if (((int) dhcpPayload.getFlags() & 0x8000) == 0x0000) {
ipv4Packet.setDestinationAddress(dhcpPayload.getYourIPAddress());
} else {
ipv4Packet.setDestinationAddress(BROADCAST_IP);
}
udpPacket.setSourcePort(UDP.DHCP_SERVER_PORT);
if (directlyConnected(dhcpPayload)) {
udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT);
} else {
// TODO Implement component config to support for both L2 and L3 relay
// L2 relay expects destination port to be CLIENT_PORT while L3 relay expects SERVER_PORT
// Currently we only support L2 relay for DHCPv4
udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT);
}
udpPacket.setPayload(dhcpPayload);
ipv4Packet.setPayload(udpPacket);
etherReply.setPayload(ipv4Packet);
return InternalPacket.internalPacket(etherReply, clientInterface.connectPoint());
}
use of org.onlab.packet.Ip4Address in project onos by opennetworkinglab.
the class Dhcp4HandlerImpl method hostUpdated.
private void hostUpdated(Host host, List<DhcpServerInfo> srverInfoList) {
srverInfoList.stream().forEach(serverInfo -> {
Ip4Address targetIp = serverInfo.getDhcpGatewayIp4().orElse(null);
Ip4Address serverIp = serverInfo.getDhcpServerIp4().orElse(null);
if (targetIp == null) {
targetIp = serverIp;
}
if (targetIp != null) {
if (host.ipAddresses().contains(targetIp)) {
serverInfo.setDhcpConnectMac(host.mac());
serverInfo.setDhcpConnectVlan(host.vlan());
requestDhcpPacket(serverIp);
}
}
});
}
Aggregations