summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/dhcp
diff options
context:
space:
mode:
authorAshlee Young <ashlee@onosfw.com>2015-10-09 18:32:44 -0700
committerAshlee Young <ashlee@onosfw.com>2015-10-09 18:32:44 -0700
commit6a07d2d622eaa06953f3353e39c080984076e8de (patch)
treebfb50a2090fce186c2cc545a400c969bf2ea702b /framework/src/onos/apps/dhcp
parente6d71622143ff9b2421a1abbe8434b954b5b1099 (diff)
Updated master to commit id 6ee8aa3e67ce89908a8c93aa9445c6f71a18f986
Change-Id: I94b055ee2f298daf71e2ec794fd0f2495bd8081f
Diffstat (limited to 'framework/src/onos/apps/dhcp')
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java2
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java162
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java4
-rw-r--r--framework/src/onos/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java10
4 files changed, 105 insertions, 73 deletions
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java
index c9fade9e..5615af1a 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java
@@ -65,7 +65,7 @@ public interface DhcpStore {
*
* @param hostId the host ID for which the mapping needs to be changed
*/
- void releaseIP(HostId hostId);
+ Ip4Address releaseIP(HostId hostId);
/**
* Returns a collection of all the MacAddress to IPAddress mapping assigned to the hosts.
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
index 345d5ad0..96d94a2b 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2014 Open Networking Laboratory
+ * Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -154,6 +154,8 @@ public class DhcpManager implements DhcpService {
private static Ip4Address domainServer = Ip4Address.valueOf("10.0.0.2");
+ private static final Ip4Address IP_BROADCAST = Ip4Address.valueOf("255.255.255.255");
+
protected Timeout timeout;
protected static int timerDelay = 2;
@@ -290,12 +292,18 @@ public class DhcpManager implements DhcpService {
DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
DHCP dhcpReply = new DHCP();
dhcpReply.setOpCode(DHCP.OPCODE_REPLY);
-
- dhcpReply.setYourIPAddress(ipOffered.toInt());
- dhcpReply.setServerIPAddress(myIP.toInt());
-
- dhcpReply.setTransactionId(dhcpPacket.getTransactionId());
+ dhcpReply.setFlags(dhcpPacket.getFlags());
+ dhcpReply.setGatewayIPAddress(dhcpPacket.getGatewayIPAddress());
dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress());
+ dhcpReply.setTransactionId(dhcpPacket.getTransactionId());
+
+ if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) {
+ dhcpReply.setYourIPAddress(ipOffered.toInt());
+ dhcpReply.setServerIPAddress(myIP.toInt());
+ if (dhcpPacket.getGatewayIPAddress() == 0) {
+ ipv4Reply.setDestinationAddress(IP_BROADCAST.toInt());
+ }
+ }
dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET);
dhcpReply.setHardwareAddressLength((byte) 6);
@@ -317,54 +325,57 @@ public class DhcpManager implements DhcpService {
option.setData(myIP.toOctets());
optionList.add(option);
- // IP Address Lease Time.
- option = new DHCPOption();
- option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue());
- option.setLength((byte) 4);
- option.setData(ByteBuffer.allocate(4).putInt(leaseTime).array());
- optionList.add(option);
-
- // IP Address Renewal Time.
- option = new DHCPOption();
- option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue());
- option.setLength((byte) 4);
- option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array());
- optionList.add(option);
-
- // IP Address Rebinding Time.
- option = new DHCPOption();
- option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue());
- option.setLength((byte) 4);
- option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array());
- optionList.add(option);
-
- // Subnet Mask.
- option = new DHCPOption();
- option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue());
- option.setLength((byte) 4);
- option.setData(subnetMask.toOctets());
- optionList.add(option);
-
- // Broadcast Address.
- option = new DHCPOption();
- option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue());
- option.setLength((byte) 4);
- option.setData(broadcastAddress.toOctets());
- optionList.add(option);
-
- // Router Address.
- option = new DHCPOption();
- option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue());
- option.setLength((byte) 4);
- option.setData(routerAddress.toOctets());
- optionList.add(option);
-
- // DNS Server Address.
- option = new DHCPOption();
- option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue());
- option.setLength((byte) 4);
- option.setData(domainServer.toOctets());
- optionList.add(option);
+ if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) {
+
+ // IP Address Lease Time.
+ option = new DHCPOption();
+ option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue());
+ option.setLength((byte) 4);
+ option.setData(ByteBuffer.allocate(4).putInt(leaseTime).array());
+ optionList.add(option);
+
+ // IP Address Renewal Time.
+ option = new DHCPOption();
+ option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue());
+ option.setLength((byte) 4);
+ option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array());
+ optionList.add(option);
+
+ // IP Address Rebinding Time.
+ option = new DHCPOption();
+ option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue());
+ option.setLength((byte) 4);
+ option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array());
+ optionList.add(option);
+
+ // Subnet Mask.
+ option = new DHCPOption();
+ option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue());
+ option.setLength((byte) 4);
+ option.setData(subnetMask.toOctets());
+ optionList.add(option);
+
+ // Broadcast Address.
+ option = new DHCPOption();
+ option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue());
+ option.setLength((byte) 4);
+ option.setData(broadcastAddress.toOctets());
+ optionList.add(option);
+
+ // Router Address.
+ option = new DHCPOption();
+ option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue());
+ option.setLength((byte) 4);
+ option.setData(routerAddress.toOctets());
+ optionList.add(option);
+
+ // DNS Server Address.
+ option = new DHCPOption();
+ option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue());
+ option.setLength((byte) 4);
+ option.setData(domainServer.toOctets());
+ optionList.add(option);
+ }
// End Option.
option = new DHCPOption();
@@ -447,41 +458,51 @@ public class DhcpManager implements DhcpService {
} else if (incomingPacketType.getValue() == DHCPPacketType.DHCPREQUEST.getValue()) {
- outgoingPacketType = DHCPPacketType.DHCPACK;
-
if (flagIfServerIP && flagIfRequestedIP) {
// SELECTING state
- if (myIP.equals(serverIP) &&
- dhcpStore.assignIP(hostId, requestedIP, leaseTime)) {
+ if (myIP.equals(serverIP)) {
- Ethernet ethReply = buildReply(packet, requestedIP,
- (byte) outgoingPacketType.getValue());
+ if (dhcpStore.assignIP(hostId, requestedIP, leaseTime)) {
+ outgoingPacketType = DHCPPacketType.DHCPACK;
+ discoverHost(context, requestedIP);
+ } else {
+ outgoingPacketType = DHCPPacketType.DHCPNAK;
+ }
+ Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue());
sendReply(context, ethReply);
- discoverHost(context, requestedIP);
}
} else if (flagIfRequestedIP) {
// INIT-REBOOT state
if (dhcpStore.assignIP(hostId, requestedIP, leaseTime)) {
- Ethernet ethReply = buildReply(packet, requestedIP,
- (byte) outgoingPacketType.getValue());
+ outgoingPacketType = DHCPPacketType.DHCPACK;
+ Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue());
sendReply(context, ethReply);
discoverHost(context, requestedIP);
}
+
} else {
// RENEWING and REBINDING state
int ciaadr = dhcpPayload.getClientIPAddress();
if (ciaadr != 0) {
Ip4Address clientIaddr = Ip4Address.valueOf(ciaadr);
if (dhcpStore.assignIP(hostId, clientIaddr, leaseTime)) {
- Ethernet ethReply = buildReply(packet, clientIaddr,
- (byte) outgoingPacketType.getValue());
- sendReply(context, ethReply);
+ outgoingPacketType = DHCPPacketType.DHCPACK;
discoverHost(context, clientIaddr);
+ } else if (packet.getEtherType() == Ethernet.TYPE_IPV4 &&
+ ((IPv4) packet.getPayload()).getDestinationAddress() == myIP.toInt()) {
+ outgoingPacketType = DHCPPacketType.DHCPNAK;
+ } else {
+ return;
}
+ Ethernet ethReply = buildReply(packet, clientIaddr, (byte) outgoingPacketType.getValue());
+ sendReply(context, ethReply);
}
}
} else if (incomingPacketType.getValue() == DHCPPacketType.DHCPRELEASE.getValue()) {
- dhcpStore.releaseIP(hostId);
+ Ip4Address ip4Address = dhcpStore.releaseIP(hostId);
+ if (ip4Address != null) {
+ hostProviderService.removeIpFromHost(hostId, ip4Address);
+ }
}
}
}
@@ -666,9 +687,10 @@ public class DhcpManager implements DhcpService {
if ((ipAssignment.assignmentStatus() != IpAssignment.AssignmentStatus.Option_Expired) &&
(ipAssignment.leasePeriod() > 0) && (timeLapsed > (ipAssignment.leasePeriodMs()))) {
- dhcpStore.releaseIP(entry.getKey());
- // TODO remove only the IP from the host entry when the API is in place.
- hostProviderService.hostVanished(entry.getKey());
+ Ip4Address ip4Address = dhcpStore.releaseIP(entry.getKey());
+ if (ip4Address != null) {
+ hostProviderService.removeIpFromHost(entry.getKey(), ipAssignment.ipAddress());
+ }
}
}
timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES);
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
index dbdadb34..63f69d40 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
@@ -212,7 +212,7 @@ public class DistributedDhcpStore implements DhcpStore {
}
@Override
- public void releaseIP(HostId hostId) {
+ public Ip4Address releaseIP(HostId hostId) {
if (allocationMap.containsKey(hostId)) {
IpAssignment newAssignment = IpAssignment.builder(allocationMap.get(hostId).value())
.assignmentStatus(IpAssignment.AssignmentStatus.Option_Expired)
@@ -222,7 +222,9 @@ public class DistributedDhcpStore implements DhcpStore {
if (ipWithinRange(freeIP)) {
freeIPPool.add(freeIP);
}
+ return freeIP;
}
+ return null;
}
@Override
diff --git a/framework/src/onos/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java b/framework/src/onos/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java
index 3ea3b1b8..fd4701c6 100644
--- a/framework/src/onos/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java
+++ b/framework/src/onos/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java
@@ -25,6 +25,7 @@ import org.onlab.packet.DHCPPacketType;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.Ip4Address;
+import org.onlab.packet.IpAddress;
import org.onlab.packet.MacAddress;
import org.onlab.packet.UDP;
import org.onosproject.core.CoreServiceAdapter;
@@ -234,7 +235,8 @@ public class DhcpManagerTest {
public void setDefaultTimeoutForPurge(int timeInSeconds) {
}
- public void releaseIP(HostId hostId) {
+ public Ip4Address releaseIP(HostId hostId) {
+ return null;
}
public Map<HostId, IpAssignment> listAssignedMapping() {
@@ -331,12 +333,18 @@ public class DhcpManagerTest {
@Override
public void hostDetected(HostId hostId, HostDescription hostDescription, boolean replaceIps) {
+
}
@Override
public void hostVanished(HostId hostId) {
}
+ @Override
+ public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
+
+ }
+
}
/**