gaurav
Committed by Gerrit Code Review

using subnet as lease/range selection criteria in DHCP Relay App, addressed review comments

Change-Id: Id2a81faa00592df4ede33f6679c2ba2dd8fb2293
1 COMPILE_DEPS = [ 1 COMPILE_DEPS = [
2 '//lib:CORE_DEPS', 2 '//lib:CORE_DEPS',
3 + '//incubator/api:onos-incubator-api',
3 ] 4 ]
4 5
5 osgi_jar ( 6 osgi_jar (
......
...@@ -49,6 +49,11 @@ ...@@ -49,6 +49,11 @@
49 49
50 <dependency> 50 <dependency>
51 <groupId>org.onosproject</groupId> 51 <groupId>org.onosproject</groupId>
52 + <artifactId>onos-incubator-api</artifactId>
53 + </dependency>
54 +
55 + <dependency>
56 + <groupId>org.onosproject</groupId>
52 <artifactId>onlab-osgi</artifactId> 57 <artifactId>onlab-osgi</artifactId>
53 <version>${project.version}</version> 58 <version>${project.version}</version>
54 </dependency> 59 </dependency>
......
...@@ -16,25 +16,28 @@ ...@@ -16,25 +16,28 @@
16 package org.onosproject.dhcprelay; 16 package org.onosproject.dhcprelay;
17 17
18 import java.nio.ByteBuffer; 18 import java.nio.ByteBuffer;
19 -import java.util.List; 19 +import java.util.Objects;
20 import java.util.Set; 20 import java.util.Set;
21 -
22 import org.apache.felix.scr.annotations.Activate; 21 import org.apache.felix.scr.annotations.Activate;
23 import org.apache.felix.scr.annotations.Component; 22 import org.apache.felix.scr.annotations.Component;
24 import org.apache.felix.scr.annotations.Deactivate; 23 import org.apache.felix.scr.annotations.Deactivate;
25 import org.apache.felix.scr.annotations.Reference; 24 import org.apache.felix.scr.annotations.Reference;
26 import org.apache.felix.scr.annotations.ReferenceCardinality; 25 import org.apache.felix.scr.annotations.ReferenceCardinality;
26 +import org.onlab.packet.ARP;
27 import org.onlab.packet.DHCP; 27 import org.onlab.packet.DHCP;
28 import org.onlab.packet.DHCPOption; 28 import org.onlab.packet.DHCPOption;
29 import org.onlab.packet.DHCPPacketType; 29 import org.onlab.packet.DHCPPacketType;
30 import org.onlab.packet.Ethernet; 30 import org.onlab.packet.Ethernet;
31 import org.onlab.packet.IPv4; 31 import org.onlab.packet.IPv4;
32 +import org.onlab.packet.Ip4Address;
33 +import org.onlab.packet.MacAddress;
32 import org.onlab.packet.TpPort; 34 import org.onlab.packet.TpPort;
33 import org.onlab.packet.UDP; 35 import org.onlab.packet.UDP;
34 import org.onlab.packet.VlanId; 36 import org.onlab.packet.VlanId;
35 -import org.onlab.util.HexString;
36 import org.onosproject.core.ApplicationId; 37 import org.onosproject.core.ApplicationId;
37 import org.onosproject.core.CoreService; 38 import org.onosproject.core.CoreService;
39 +import org.onosproject.incubator.net.intf.Interface;
40 +import org.onosproject.incubator.net.intf.InterfaceService;
38 import org.onosproject.net.ConnectPoint; 41 import org.onosproject.net.ConnectPoint;
39 import org.onosproject.net.Host; 42 import org.onosproject.net.Host;
40 import org.onosproject.net.HostId; 43 import org.onosproject.net.HostId;
...@@ -57,9 +60,10 @@ import org.slf4j.Logger; ...@@ -57,9 +60,10 @@ import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory; 60 import org.slf4j.LoggerFactory;
58 61
59 import com.google.common.collect.ImmutableSet; 62 import com.google.common.collect.ImmutableSet;
63 +
60 import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; 64 import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
61 -import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_CircuitID;
62 import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType; 65 import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType;
66 +import static org.onlab.packet.MacAddress.valueOf;
63 /** 67 /**
64 * DHCP Relay Agent Application Component. 68 * DHCP Relay Agent Application Component.
65 */ 69 */
...@@ -69,6 +73,9 @@ public class DhcpRelay { ...@@ -69,6 +73,9 @@ public class DhcpRelay {
69 public static final String DHCP_RELAY_APP = "org.onosproject.dhcp-relay"; 73 public static final String DHCP_RELAY_APP = "org.onosproject.dhcp-relay";
70 private final Logger log = LoggerFactory.getLogger(getClass()); 74 private final Logger log = LoggerFactory.getLogger(getClass());
71 private final InternalConfigListener cfgListener = new InternalConfigListener(); 75 private final InternalConfigListener cfgListener = new InternalConfigListener();
76 + private static Ip4Address relayAgentIP = null;
77 + private static MacAddress relayAgentMAC = null;
78 + private static MacAddress myMAC = valueOf("4f:4f:4f:4f:4f:4f");
72 79
73 private final Set<ConfigFactory> factories = ImmutableSet.of( 80 private final Set<ConfigFactory> factories = ImmutableSet.of(
74 new ConfigFactory<ApplicationId, DhcpRelayConfig>(APP_SUBJECT_FACTORY, 81 new ConfigFactory<ApplicationId, DhcpRelayConfig>(APP_SUBJECT_FACTORY,
...@@ -93,8 +100,13 @@ public class DhcpRelay { ...@@ -93,8 +100,13 @@ public class DhcpRelay {
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected HostService hostService; 101 protected HostService hostService;
95 102
103 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 + protected InterfaceService interfaceService;
105 +
96 private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor(); 106 private DhcpRelayPacketProcessor dhcpRelayPacketProcessor = new DhcpRelayPacketProcessor();
97 private ConnectPoint dhcpServerConnectPoint = null; 107 private ConnectPoint dhcpServerConnectPoint = null;
108 + private Ip4Address dhcpServerIp = null;
109 + private MacAddress dhcpServerMac = null;
98 private ApplicationId appId; 110 private ApplicationId appId;
99 111
100 @Activate 112 @Activate
...@@ -132,8 +144,11 @@ public class DhcpRelay { ...@@ -132,8 +144,11 @@ public class DhcpRelay {
132 } 144 }
133 145
134 dhcpServerConnectPoint = cfg.getDhcpServerConnectPoint(); 146 dhcpServerConnectPoint = cfg.getDhcpServerConnectPoint();
135 - log.info("Reconfigured the dhcp server info"); 147 + dhcpServerIp = cfg.getDhcpServerIp();
148 + dhcpServerMac = cfg.getDhcpServermac();
136 log.info("dhcp server connect points are " + dhcpServerConnectPoint); 149 log.info("dhcp server connect points are " + dhcpServerConnectPoint);
150 + log.info("dhcp server ipaddress " + dhcpServerIp);
151 + log.info("dhcp server mac address " + dhcpServerMac);
137 } 152 }
138 153
139 /** 154 /**
...@@ -144,16 +159,18 @@ public class DhcpRelay { ...@@ -144,16 +159,18 @@ public class DhcpRelay {
144 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() 159 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
145 .matchEthType(Ethernet.TYPE_IPV4) 160 .matchEthType(Ethernet.TYPE_IPV4)
146 .matchIPProtocol(IPv4.PROTOCOL_UDP) 161 .matchIPProtocol(IPv4.PROTOCOL_UDP)
147 - .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) 162 + .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
148 - .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT));
149 packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId); 163 packetService.requestPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
150 164
151 TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder() 165 TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder()
152 .matchEthType(Ethernet.TYPE_IPV4) 166 .matchEthType(Ethernet.TYPE_IPV4)
153 .matchIPProtocol(IPv4.PROTOCOL_UDP) 167 .matchIPProtocol(IPv4.PROTOCOL_UDP)
154 - .matchUdpDst(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)) 168 + .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT));
155 - .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
156 packetService.requestPackets(selectorClient.build(), PacketPriority.CONTROL, appId); 169 packetService.requestPackets(selectorClient.build(), PacketPriority.CONTROL, appId);
170 +
171 + TrafficSelector.Builder selectorArpServer = DefaultTrafficSelector.builder()
172 + .matchEthType(Ethernet.TYPE_ARP);
173 + packetService.requestPackets(selectorArpServer.build(), PacketPriority.CONTROL, appId);
157 } 174 }
158 175
159 /** 176 /**
...@@ -163,16 +180,18 @@ public class DhcpRelay { ...@@ -163,16 +180,18 @@ public class DhcpRelay {
163 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder() 180 TrafficSelector.Builder selectorServer = DefaultTrafficSelector.builder()
164 .matchEthType(Ethernet.TYPE_IPV4) 181 .matchEthType(Ethernet.TYPE_IPV4)
165 .matchIPProtocol(IPv4.PROTOCOL_UDP) 182 .matchIPProtocol(IPv4.PROTOCOL_UDP)
166 - .matchUdpDst(TpPort.tpPort(UDP.DHCP_SERVER_PORT)) 183 + .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
167 - .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT));
168 packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId); 184 packetService.cancelPackets(selectorServer.build(), PacketPriority.CONTROL, appId);
169 185
170 TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder() 186 TrafficSelector.Builder selectorClient = DefaultTrafficSelector.builder()
171 .matchEthType(Ethernet.TYPE_IPV4) 187 .matchEthType(Ethernet.TYPE_IPV4)
172 .matchIPProtocol(IPv4.PROTOCOL_UDP) 188 .matchIPProtocol(IPv4.PROTOCOL_UDP)
173 - .matchUdpDst(TpPort.tpPort(UDP.DHCP_CLIENT_PORT)) 189 + .matchUdpSrc(TpPort.tpPort(UDP.DHCP_CLIENT_PORT));
174 - .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
175 packetService.cancelPackets(selectorClient.build(), PacketPriority.CONTROL, appId); 190 packetService.cancelPackets(selectorClient.build(), PacketPriority.CONTROL, appId);
191 +
192 + TrafficSelector.Builder selectorArpServer = DefaultTrafficSelector.builder()
193 + .matchEthType(Ethernet.TYPE_ARP);
194 + packetService.cancelPackets(selectorArpServer.build(), PacketPriority.CONTROL, appId);
176 } 195 }
177 196
178 private class DhcpRelayPacketProcessor implements PacketProcessor { 197 private class DhcpRelayPacketProcessor implements PacketProcessor {
...@@ -192,17 +211,38 @@ public class DhcpRelay { ...@@ -192,17 +211,38 @@ public class DhcpRelay {
192 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) { 211 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_UDP) {
193 UDP udpPacket = (UDP) ipv4Packet.getPayload(); 212 UDP udpPacket = (UDP) ipv4Packet.getPayload();
194 DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); 213 DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
195 - if (udpPacket.getDestinationPort() == UDP.DHCP_SERVER_PORT && 214 + if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT ||
196 - udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT || 215 + udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) {
197 - udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT &&
198 - udpPacket.getDestinationPort() == UDP.DHCP_CLIENT_PORT) {
199 //This packet is dhcp. 216 //This packet is dhcp.
200 processDhcpPacket(context, dhcpPayload); 217 processDhcpPacket(context, dhcpPayload);
201 } 218 }
202 } 219 }
220 + } else if (packet.getEtherType() == Ethernet.TYPE_ARP) {
221 + ARP arpPacket = (ARP) packet.getPayload();
222 + Set<Interface> serverInterfaces = interfaceService.
223 + getInterfacesByPort(context.inPacket().receivedFrom());
224 + //ignore the packets if dhcp server interface is not configured on onos.
225 + if (serverInterfaces.isEmpty()) {
226 + log.warn("server virtual interface not configured");
227 + return;
228 + }
229 + if ((arpPacket.getOpCode() == ARP.OP_REQUEST) &&
230 + checkArpRequestFrmDhcpServ(serverInterfaces, arpPacket)) {
231 + processArpPacket(context, packet);
232 + }
203 } 233 }
204 } 234 }
205 235
236 + //method to check the arp request is from dhcp server for default-gateway.
237 + private boolean checkArpRequestFrmDhcpServ(Set<Interface> serverInterfaces, ARP arpPacket) {
238 + if (Objects.equals(serverInterfaces.iterator().next().ipAddressesList().get(0).
239 + ipAddress().getIp4Address(),
240 + Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()))) {
241 + return true;
242 + }
243 + return false;
244 + }
245 +
206 //forward the packet to ConnectPoint where the DHCP server is attached. 246 //forward the packet to ConnectPoint where the DHCP server is attached.
207 private void forwardPacket(Ethernet packet) { 247 private void forwardPacket(Ethernet packet) {
208 248
...@@ -216,15 +256,51 @@ public class DhcpRelay { ...@@ -216,15 +256,51 @@ public class DhcpRelay {
216 } 256 }
217 } 257 }
218 258
259 + /**
260 + * Processes the ARP Payload and initiates a reply to the client.
261 + *
262 + * @param context context of the incoming message
263 + * @param packet the ethernet payload
264 + */
265 + private void processArpPacket(PacketContext context, Ethernet packet) {
266 +
267 + ARP arpPacket = (ARP) packet.getPayload();
268 +
269 + ARP arpReply = (ARP) arpPacket.clone();
270 + arpReply.setOpCode(ARP.OP_REPLY);
271 +
272 + arpReply.setTargetProtocolAddress(arpPacket.getSenderProtocolAddress());
273 + arpReply.setTargetHardwareAddress(arpPacket.getSenderHardwareAddress());
274 + arpReply.setSenderProtocolAddress(arpPacket.getTargetProtocolAddress());
275 + arpReply.setSenderHardwareAddress(myMAC.toBytes());
276 +
277 + // Ethernet Frame.
278 + Ethernet ethReply = new Ethernet();
279 + ethReply.setSourceMACAddress(myMAC);
280 + ethReply.setDestinationMACAddress(packet.getSourceMAC());
281 + ethReply.setEtherType(Ethernet.TYPE_ARP);
282 + ethReply.setVlanID(packet.getVlanID());
283 +
284 + ethReply.setPayload(arpReply);
285 + forwardPacket(ethReply);
286 + }
287 +
219 //process the dhcp packet before sending to server 288 //process the dhcp packet before sending to server
220 private void processDhcpPacket(PacketContext context, DHCP dhcpPayload) { 289 private void processDhcpPacket(PacketContext context, DHCP dhcpPayload) {
221 290
291 + Set<Interface> clientServerInterfaces = interfaceService.
292 + getInterfacesByPort(context.inPacket().receivedFrom());
293 + //ignore the packets if dhcp client interface is not configured on onos.
294 + if (clientServerInterfaces.isEmpty()) {
295 + log.warn("client virtual interface not configured");
296 + return;
297 + }
298 +
222 if (dhcpPayload == null) { 299 if (dhcpPayload == null) {
223 return; 300 return;
224 } 301 }
225 302
226 Ethernet packet = context.inPacket().parsed(); 303 Ethernet packet = context.inPacket().parsed();
227 - String circuitIdFrmClient = context.inPacket().receivedFrom().elementId().toString();
228 DHCPPacketType incomingPacketType = null; 304 DHCPPacketType incomingPacketType = null;
229 for (DHCPOption option : dhcpPayload.getOptions()) { 305 for (DHCPOption option : dhcpPayload.getOptions()) {
230 if (option.getCode() == OptionCode_MessageType.getValue()) { 306 if (option.getCode() == OptionCode_MessageType.getValue()) {
...@@ -232,79 +308,81 @@ public class DhcpRelay { ...@@ -232,79 +308,81 @@ public class DhcpRelay {
232 incomingPacketType = DHCPPacketType.getType(data[0]); 308 incomingPacketType = DHCPPacketType.getType(data[0]);
233 } 309 }
234 } 310 }
311 +
235 switch (incomingPacketType) { 312 switch (incomingPacketType) {
236 case DHCPDISCOVER: 313 case DHCPDISCOVER:
237 - //add the circuit id as switch dpid and forward the packet to dhcp server. 314 + //add the gatewayip as virtual interface ip for server to understand the lease to be assigned
238 - Ethernet ethernetPacketDiscover = processDhcpPacketFrmClient(packet, circuitIdFrmClient, 315 + //and forward the packet to dhcp server.
239 - (byte) DHCPPacketType.DHCPDISCOVER.getValue()); 316 + Ethernet ethernetPacketDiscover = processDhcpPacketFrmClient(context, packet, clientServerInterfaces);
240 forwardPacket(ethernetPacketDiscover); 317 forwardPacket(ethernetPacketDiscover);
241 break; 318 break;
242 case DHCPOFFER: 319 case DHCPOFFER:
243 //reply to dhcp client. 320 //reply to dhcp client.
244 - sendReply(packet); 321 + Ethernet ethernetPacketOffer = processDhcpPacketFrmServer(packet);
322 + sendReply(ethernetPacketOffer, dhcpPayload);
245 break; 323 break;
246 case DHCPREQUEST: 324 case DHCPREQUEST:
247 - //add the circuit id as switch dpid and forward the packet to dhcp server. 325 + //add the gatewayip as virtual interface ip for server to understand the lease to be assigned
248 - Ethernet ethernetPacketRequest = processDhcpPacketFrmClient(packet, circuitIdFrmClient, 326 + //and forward the packet to dhcp server.
249 - (byte) DHCPPacketType.DHCPREQUEST.getValue()); 327 + Ethernet ethernetPacketRequest = processDhcpPacketFrmClient(context, packet, clientServerInterfaces);
250 forwardPacket(ethernetPacketRequest); 328 forwardPacket(ethernetPacketRequest);
251 break; 329 break;
252 case DHCPACK: 330 case DHCPACK:
253 //reply to dhcp client. 331 //reply to dhcp client.
254 - sendReply(packet); 332 + Ethernet ethernetPacketAck = processDhcpPacketFrmServer(packet);
333 + sendReply(ethernetPacketAck, dhcpPayload);
255 break; 334 break;
256 default: 335 default:
257 break; 336 break;
258 } 337 }
259 } 338 }
260 339
261 - //build the DHCP discover/request packet with circuitid(DpId) suboption. 340 + //build the DHCP discover/request packet with gatewayip(unicast packet)
262 - private Ethernet processDhcpPacketFrmClient(Ethernet ethernetPacket, String circuitId, 341 + private Ethernet processDhcpPacketFrmClient(PacketContext context, Ethernet ethernetPacket,
263 - byte incomingPacketType) { 342 + Set<Interface> clientInterfaces) {
264 343
344 + //assuming one interface per port for now.
345 + relayAgentIP = clientInterfaces.iterator().next().ipAddressesList().get(0).
346 + ipAddress().getIp4Address();
347 + relayAgentMAC = clientInterfaces.iterator().next().mac();
265 // get dhcp header. 348 // get dhcp header.
266 Ethernet etherReply = (Ethernet) ethernetPacket.clone(); 349 Ethernet etherReply = (Ethernet) ethernetPacket.clone();
350 + etherReply.setSourceMACAddress(relayAgentMAC);
351 + etherReply.setDestinationMACAddress(dhcpServerMac);
267 IPv4 ipv4Packet = (IPv4) etherReply.getPayload(); 352 IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
353 + ipv4Packet.setSourceAddress(relayAgentIP.toInt());
354 + ipv4Packet.setDestinationAddress(dhcpServerIp.toInt());
268 UDP udpPacket = (UDP) ipv4Packet.getPayload(); 355 UDP udpPacket = (UDP) ipv4Packet.getPayload();
269 DHCP dhcpPacket = (DHCP) udpPacket.getPayload(); 356 DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
357 + dhcpPacket.setGatewayIPAddress(relayAgentIP.toInt());
358 + udpPacket.setPayload(dhcpPacket);
359 + ipv4Packet.setPayload(udpPacket);
360 + etherReply.setPayload(ipv4Packet);
361 + return etherReply;
362 + }
270 363
271 - // DHCP Options. 364 + //build the DHCP offer/ack with proper client port.
272 - List<DHCPOption> optionList = dhcpPacket.getOptions(); 365 + private Ethernet processDhcpPacketFrmServer(Ethernet ethernetPacket) {
273 -
274 - // Dhcp SubOption as CircuitID
275 - DHCPOption option = new DHCPOption();
276 - option.setCode(OptionCode_CircuitID.getValue());
277 - option.setLength((byte) 10);
278 -
279 - // start object for suboption circuit id.
280 - String[] actualDpId = circuitId.split(":", 2);
281 - DHCPOption subOption = new DHCPOption();
282 - subOption.setCode((byte) 1);
283 - byte[] subOptionData = HexString.fromHexString(actualDpId[1], null);
284 - subOption.setData(subOptionData);
285 - subOption.setLength((byte) 8);
286 - // end object for suboption circuit id.
287 -
288 - // converting suboption to byte array
289 - byte[] data = new byte[10];
290 - ByteBuffer bb = ByteBuffer.wrap(data);
291 - DHCP.dhcpOptionToByteArray(subOption, bb);
292 -
293 - option.setData(data);
294 - optionList.add(optionList.size() - 1, option);
295 366
296 - dhcpPacket.setOptions(optionList); 367 + // get dhcp header.
297 - udpPacket.setPayload(dhcpPacket); 368 + Ethernet etherReply = (Ethernet) ethernetPacket.clone();
369 + IPv4 ipv4Packet = (IPv4) etherReply.getPayload();
370 + UDP udpPacket = (UDP) ipv4Packet.getPayload();
371 + DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
372 + //set the ethernet frame.
373 + etherReply.setDestinationMACAddress(dhcpPayload.getClientHardwareAddress());
374 + udpPacket.setDestinationPort(UDP.DHCP_CLIENT_PORT);
375 + udpPacket.setPayload(dhcpPayload);
298 ipv4Packet.setPayload(udpPacket); 376 ipv4Packet.setPayload(udpPacket);
299 etherReply.setPayload(ipv4Packet); 377 etherReply.setPayload(ipv4Packet);
300 return etherReply; 378 return etherReply;
301 } 379 }
302 380
303 //send the response to the requestor host. 381 //send the response to the requestor host.
304 - private void sendReply(Ethernet ethPacket) { 382 + private void sendReply(Ethernet ethPacket, DHCP dhcpPayload) {
305 383
306 - //get the host info 384 + MacAddress descMac = new MacAddress(dhcpPayload.getClientHardwareAddress());
307 - Host host = hostService.getHost(HostId.hostId(ethPacket.getDestinationMAC(), 385 + Host host = hostService.getHost(HostId.hostId(descMac,
308 VlanId.vlanId(ethPacket.getVlanID()))); 386 VlanId.vlanId(ethPacket.getVlanID())));
309 ConnectPoint dhcpRequestor = new ConnectPoint(host.location().elementId(), 387 ConnectPoint dhcpRequestor = new ConnectPoint(host.location().elementId(),
310 host.location().port()); 388 host.location().port());
...@@ -336,4 +414,4 @@ public class DhcpRelay { ...@@ -336,4 +414,4 @@ public class DhcpRelay {
336 } 414 }
337 } 415 }
338 } 416 }
339 -} 417 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -20,18 +20,25 @@ import org.onosproject.net.ConnectPoint; ...@@ -20,18 +20,25 @@ import org.onosproject.net.ConnectPoint;
20 import org.onosproject.net.config.Config; 20 import org.onosproject.net.config.Config;
21 21
22 import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; 22 import static org.onosproject.net.config.Config.FieldPresence.MANDATORY;
23 +
24 +import org.onlab.packet.Ip4Address;
25 +import org.onlab.packet.MacAddress;
23 /** 26 /**
24 * DHCP Relay Config class. 27 * DHCP Relay Config class.
25 */ 28 */
26 public class DhcpRelayConfig extends Config<ApplicationId> { 29 public class DhcpRelayConfig extends Config<ApplicationId> {
27 30
28 private static final String DHCP_CONNECT_POINT = "dhcpserverConnectPoint"; 31 private static final String DHCP_CONNECT_POINT = "dhcpserverConnectPoint";
32 + private static final String DHCP_SERVER_IP = "serverip";
33 + private static final String DHCP_SERVER_MAC = "servermac";
29 34
30 @Override 35 @Override
31 public boolean isValid() { 36 public boolean isValid() {
32 37
33 - return hasOnlyFields(DHCP_CONNECT_POINT) && 38 + return hasOnlyFields(DHCP_CONNECT_POINT, DHCP_SERVER_IP, DHCP_SERVER_MAC) &&
34 - isConnectPoint(DHCP_CONNECT_POINT, MANDATORY); 39 + isConnectPoint(DHCP_CONNECT_POINT, MANDATORY) &&
40 + isIpAddress(DHCP_SERVER_IP, MANDATORY) &&
41 + isMacAddress(DHCP_SERVER_MAC, MANDATORY);
35 } 42 }
36 43
37 /** 44 /**
...@@ -42,4 +49,24 @@ public class DhcpRelayConfig extends Config<ApplicationId> { ...@@ -42,4 +49,24 @@ public class DhcpRelayConfig extends Config<ApplicationId> {
42 public ConnectPoint getDhcpServerConnectPoint() { 49 public ConnectPoint getDhcpServerConnectPoint() {
43 return ConnectPoint.deviceConnectPoint(object.path(DHCP_CONNECT_POINT).asText()); 50 return ConnectPoint.deviceConnectPoint(object.path(DHCP_CONNECT_POINT).asText());
44 } 51 }
52 +
53 + /**
54 + * Returns the dhcp server ip.
55 + *
56 + * @return ip address or null if not set
57 + */
58 + public Ip4Address getDhcpServerIp() {
59 + String ip = get(DHCP_SERVER_IP, null);
60 + return ip != null ? Ip4Address.valueOf(ip) : null;
61 + }
62 +
63 + /**
64 + * Returns the dhcp server mac.
65 + *
66 + * @return server mac or null if not set
67 + */
68 + public MacAddress getDhcpServermac() {
69 + String mac = get(DHCP_SERVER_MAC, null);
70 + return mac != null ? MacAddress.valueOf(mac) : null;
71 + }
45 } 72 }
......
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
2 "apps": { 2 "apps": {
3 "org.onosproject.dhcp-relay" : { 3 "org.onosproject.dhcp-relay" : {
4 "dhcprelay" : { 4 "dhcprelay" : {
5 - "dhcpserverConnectPoint": "of:0000000000000002/2" 5 + "dhcpserverConnectPoint": "of:0000000000000002/2",
6 + "serverip": "172.168.10.2",
7 + "servermac": "d2:70:98:90:8c:44"
6 } 8 }
7 } 9 }
8 } 10 }
......