blob: 521ba0f37e95adc2a6d049ddde12575020465da5 [file] [log] [blame]
Jonathan Hartc7ca35d2013-06-25 20:54:25 +12001package net.onrc.onos.ofcontroller.proxyarp;
2
3import java.io.IOException;
4import java.net.InetAddress;
5import java.net.UnknownHostException;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.HashMap;
Jonathan Hart6261dcd2013-07-22 17:58:35 +12009import java.util.Iterator;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120010import java.util.List;
11import java.util.Map;
12import java.util.Set;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120013import java.util.Timer;
14import java.util.TimerTask;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120015
16import net.floodlightcontroller.core.FloodlightContext;
17import net.floodlightcontroller.core.IFloodlightProviderService;
18import net.floodlightcontroller.core.IOFMessageListener;
19import net.floodlightcontroller.core.IOFSwitch;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120020import net.floodlightcontroller.devicemanager.IDeviceService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120021import net.floodlightcontroller.packet.ARP;
22import net.floodlightcontroller.packet.Ethernet;
23import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120024import net.floodlightcontroller.util.MACAddress;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120025
26import org.openflow.protocol.OFMessage;
27import org.openflow.protocol.OFPacketIn;
28import org.openflow.protocol.OFPacketOut;
29import org.openflow.protocol.OFPort;
30import org.openflow.protocol.OFType;
31import org.openflow.protocol.action.OFAction;
32import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120033import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Jonathan Hart4dfc3652013-08-02 20:22:36 +120037import com.google.common.collect.HashMultimap;
38import com.google.common.collect.Multimaps;
39import com.google.common.collect.SetMultimap;
40
Jonathan Hart32e18222013-08-07 22:05:42 +120041//TODO have L2 and also L3 mode, where it takes into account interface addresses
Jonathan Hart6261dcd2013-07-22 17:58:35 +120042public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120043 private static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
44
Jonathan Hart6261dcd2013-07-22 17:58:35 +120045 private final long ARP_ENTRY_TIMEOUT = 600000; //ms (== 10 mins)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120046
Jonathan Hartdf6ec332013-08-04 01:37:14 +120047 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hart6261dcd2013-07-22 17:58:35 +120048
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120049 protected IFloodlightProviderService floodlightProvider;
50 protected ITopologyService topology;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120051 protected IDeviceService devices;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120052
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120053 protected Map<InetAddress, ArpTableEntry> arpTable;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120054
Jonathan Hart4dfc3652013-08-02 20:22:36 +120055 protected SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120056
57 private class ArpRequest {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120058 private IArpRequester requester;
59 private boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120060 private long requestTime;
61
Jonathan Hart4dfc3652013-08-02 20:22:36 +120062 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120063 this.requester = requester;
64 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120065 this.requestTime = System.currentTimeMillis();
66 }
67
Jonathan Hart4dfc3652013-08-02 20:22:36 +120068 public ArpRequest(ArpRequest old) {
69 this.requester = old.requester;
70 this.retry = old.retry;
71 this.requestTime = System.currentTimeMillis();
72 }
73
Jonathan Hart4dfc3652013-08-02 20:22:36 +120074 public boolean isExpired() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +120075 return (System.currentTimeMillis() - requestTime)
76 > IProxyArpService.ARP_REQUEST_TIMEOUT;
77 }
78
Jonathan Hart4dfc3652013-08-02 20:22:36 +120079 public boolean shouldRetry() {
80 return retry;
81 }
82
Jonathan Hart32e18222013-08-07 22:05:42 +120083 public void dispatchReply(InetAddress ipAddress, byte[] replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120084 log.debug("Dispatching reply for {} to {}", ipAddress.getHostAddress(),
85 requester);
86 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120087 }
88 }
89
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120090 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart6261dcd2013-07-22 17:58:35 +120091 ITopologyService topology, IDeviceService devices){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120092 this.floodlightProvider = floodlightProvider;
93 this.topology = topology;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120094 this.devices = devices;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120095
96 arpTable = new HashMap<InetAddress, ArpTableEntry>();
Jonathan Hartdf6ec332013-08-04 01:37:14 +120097
Jonathan Hart4dfc3652013-08-02 20:22:36 +120098 arpRequests = Multimaps.synchronizedSetMultimap(
99 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200100
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200101 Timer arpTimer = new Timer();
102 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200103 @Override
104 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200105 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200106 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200107 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200108 }
109
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200110 /*
111 * Function that runs periodically to manage the asynchronous request mechanism.
112 * It basically cleans up old ARP requests if we don't get a response for them.
113 * The caller can designate that a request should be retried indefinitely, and
114 * this task will handle that as well.
115 */
116 private void doPeriodicArpProcessing() {
117 SetMultimap<InetAddress, ArpRequest> retryList
118 = HashMultimap.<InetAddress, ArpRequest>create();
119
120 //Have to synchronize externally on the Multimap while using an iterator,
121 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200122 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200123 log.debug("Current have {} outstanding requests",
124 arpRequests.size());
125
126 Iterator<Map.Entry<InetAddress, ArpRequest>> it
127 = arpRequests.entries().iterator();
128
129 while (it.hasNext()) {
130 Map.Entry<InetAddress, ArpRequest> entry
131 = it.next();
132 ArpRequest request = entry.getValue();
133 if (request.isExpired()) {
134 log.debug("Cleaning expired ARP request for {}",
135 entry.getKey().getHostAddress());
136
137 it.remove();
138
139 if (request.shouldRetry()) {
140 retryList.put(entry.getKey(), request);
141 }
142 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200143 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200144 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200145
146 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
147 : retryList.asMap().entrySet()) {
148
149 InetAddress address = entry.getKey();
150
151 log.debug("Resending ARP request for {}", address.getHostAddress());
152
153 sendArpRequestForAddress(address);
154
155 for (ArpRequest request : entry.getValue()) {
156 arpRequests.put(address, new ArpRequest(request));
157 }
158 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200159 }
160
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200161 @Override
162 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200163 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200164 }
165
166 @Override
167 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200168 return false;
169 }
170
171 @Override
172 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200173 return false;
174 }
175
176 @Override
177 public Command receive(
178 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
179
180 if (msg.getType() != OFType.PACKET_IN){
181 return Command.CONTINUE;
182 }
183
184 OFPacketIn pi = (OFPacketIn) msg;
185
186 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
187 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
188
189 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200190 ARP arp = (ARP) eth.getPayload();
191
192 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200193 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200194 }
195 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200196 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200197 }
198 }
199
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200200 //TODO should we propagate ARP or swallow it?
201 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200202 return Command.CONTINUE;
203 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200204
205 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
206 log.debug("ARP request received for {}",
207 bytesToStringAddr(arp.getTargetProtocolAddress()));
208
209 byte[] mac = lookupArpTable(arp.getTargetProtocolAddress());
210
211 if (mac == null){
212 //Mac address is not in our arp table.
213
214 //TODO check what the DeviceManager thinks
215
216 //Record where the request came from so we know where to send the reply
217 InetAddress target;
218 try {
219 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
220 } catch (UnknownHostException e) {
221 log.debug("Invalid address in ARP request", e);
222 //return Command.CONTINUE; //Continue or stop?
223 return;
224 }
225
Jonathan Hart9ea31212013-08-12 21:40:34 +1200226 //Should we just broadcast all received requests here? Or rate limit
227 //if we know we just sent an request?
228 arpRequests.put(target, new ArpRequest(
229 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200230
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200231 //Flood the request out edge ports
Jonathan Hart9ea31212013-08-12 21:40:34 +1200232 broadcastArpRequestOutEdge(pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200233 }
234 else {
235 //We know the address, so send a reply
236 log.debug("Sending reply of {}", MACAddress.valueOf(mac).toString());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200237 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
238 }
239 }
240
241 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200242 log.debug("ARP reply recieved for {}, is {}, on {}/{}", new Object[] {
243 bytesToStringAddr(arp.getSenderProtocolAddress()),
244 HexString.toHexString(arp.getSenderHardwareAddress()),
245 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200246
247 updateArpTable(arp);
248
249 //See if anyone's waiting for this ARP reply
250 InetAddress addr;
251 try {
252 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
253 } catch (UnknownHostException e) {
254 return;
255 }
256
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200257 Set<ArpRequest> requests = arpRequests.get(addr);
258
259 //Synchronize on the Multimap while using an iterator for one of the sets
260 synchronized (arpRequests) {
261 Iterator<ArpRequest> it = requests.iterator();
262 while (it.hasNext()) {
263 ArpRequest request = it.next();
264 it.remove();
265 request.dispatchReply(addr, arp.getSenderHardwareAddress());
266 }
267 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200268 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200269
270 private synchronized byte[] lookupArpTable(byte[] ipAddress){
271 InetAddress addr;
272 try {
273 addr = InetAddress.getByAddress(ipAddress);
274 } catch (UnknownHostException e) {
275 log.warn("Unable to create InetAddress", e);
276 return null;
277 }
278
279 ArpTableEntry arpEntry = arpTable.get(addr);
280
281 if (arpEntry == null){
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200282 log.debug("MAC for {} unknown", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200283 return null;
284 }
285
286 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
287 > ARP_ENTRY_TIMEOUT){
288 //Entry has timed out so we'll remove it and return null
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200289 log.debug("Timing out old ARP entry for {}", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200290 arpTable.remove(addr);
291 return null;
292 }
293
294 return arpEntry.getMacAddress();
295 }
296
297 private synchronized void updateArpTable(ARP arp){
298 InetAddress addr;
299 try {
300 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
301 } catch (UnknownHostException e) {
302 log.warn("Unable to create InetAddress", e);
303 return;
304 }
305
306 ArpTableEntry arpEntry = arpTable.get(addr);
307
308 if (arpEntry != null
309 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
310 arpEntry.setTimeLastSeen(System.currentTimeMillis());
311 }
312 else {
313 arpTable.put(addr,
314 new ArpTableEntry(arp.getSenderHardwareAddress(),
315 System.currentTimeMillis()));
316 }
317 }
318
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200319 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200320 //TODO what should the sender IP address be? Probably not 0.0.0.0
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200321 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
322 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200323 byte[] bgpdMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
324 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200325 (byte)0xff, (byte)0xff, (byte)0xff};
326
327 ARP arpRequest = new ARP();
328
329 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
330 .setProtocolType(ARP.PROTO_TYPE_IP)
331 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
332 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
333 .setOpCode(ARP.OP_REQUEST)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200334 .setSenderHardwareAddress(bgpdMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200335 .setSenderProtocolAddress(zeroIpv4)
336 .setTargetHardwareAddress(zeroMac)
337 .setTargetProtocolAddress(ipAddress.getAddress());
338
339 Ethernet eth = new Ethernet();
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200340 //eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
341 eth.setSourceMACAddress(bgpdMac)
342 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200343 .setEtherType(Ethernet.TYPE_ARP)
344 .setPayload(arpRequest);
345
346 broadcastArpRequestOutEdge(eth.serialize(), 0, OFPort.OFPP_NONE.getValue());
347 }
348
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200349 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200350 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
351 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
352 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
353
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200354 if (linkPorts == null){
355 //I think this means the switch isn't known to topology yet.
356 //Maybe it only just joined.
357 continue;
358 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200359
360 OFPacketOut po = new OFPacketOut();
361 po.setInPort(OFPort.OFPP_NONE)
362 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200363 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200364
365 List<OFAction> actions = new ArrayList<OFAction>();
366
367 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200368 if (linkPorts.contains(portNum) ||
369 (sw.getId() == inSwitch && portNum == inPort)){
370 //If this port isn't an edge port or is the ingress port
371 //for the ARP, don't broadcast out it
372 continue;
373 }
374
375 actions.add(new OFActionOutput(portNum));
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200376 //log.debug("Broadcasting out {}/{}", HexString.toHexString(sw.getId()), portNum);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200377 }
378
379 po.setActions(actions);
380 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
381 po.setActionsLength(actionsLength);
382 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200383 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200384
385 List<OFMessage> msgList = new ArrayList<OFMessage>();
386 msgList.add(po);
387
388 try {
389 sw.write(msgList, null);
390 sw.flush();
391 } catch (IOException e) {
392 log.error("Failure writing packet out to switch", e);
393 }
394 }
395 }
396
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200397 public void sendArpReply(ARP arpRequest, long dpid, short port, byte[] targetMac) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200398 ARP arpReply = new ARP();
399 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
400 .setProtocolType(ARP.PROTO_TYPE_IP)
401 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
402 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
403 .setOpCode(ARP.OP_REPLY)
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200404 .setSenderHardwareAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200405 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
406 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
407 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
408
409 Ethernet eth = new Ethernet();
410 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200411 .setSourceMACAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200412 .setEtherType(Ethernet.TYPE_ARP)
413 .setPayload(arpReply);
414
415 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200416 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200417
418 OFPacketOut po = new OFPacketOut();
419 po.setInPort(OFPort.OFPP_NONE)
420 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200421 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200422 .setActions(actions)
423 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
424 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
425 + po.getPacketData().length);
426
427 List<OFMessage> msgList = new ArrayList<OFMessage>();
428 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200429
430 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
431
432 if (sw == null) {
433 return;
434 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200435
436 try {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200437 log.debug("Sending ARP reply to {}/{}", HexString.toHexString(sw.getId()), port);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200438 sw.write(msgList, null);
439 sw.flush();
440 } catch (IOException e) {
441 log.warn("Failure writing packet out to switch", e);
442 }
443 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200444
445 //TODO this should be put somewhere more central. I use it in BgpRoute as well.
446 //We need a HexString.toHexString() equivalent.
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200447 private String bytesToStringAddr(byte[] bytes) {
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200448 InetAddress addr;
449 try {
450 addr = InetAddress.getByAddress(bytes);
451 } catch (UnknownHostException e) {
Jonathan Hartc824ad02013-07-03 15:58:45 +1200452 log.warn(" ", e);
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200453 return "";
454 }
455 if (addr == null) return "";
456 else return addr.getHostAddress();
457 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200458
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200459 @Override
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200460 public byte[] getMacAddress(InetAddress ipAddress) {
461 return lookupArpTable(ipAddress.getAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200462 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200463
464 @Override
465 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
466 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200467 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
468 //storeRequester(ipAddress, requester, retry);
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200469
470 sendArpRequestForAddress(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200471 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200472}