blob: f56934dfbff1ad4fa6a95681316a9cf641648d05 [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 Hart6261dcd2013-07-22 17:58:35 +120041public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120042 private static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
43
Jonathan Hart6261dcd2013-07-22 17:58:35 +120044 private final long ARP_ENTRY_TIMEOUT = 600000; //ms (== 10 mins)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120045
Jonathan Hartdf6ec332013-08-04 01:37:14 +120046 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hart6261dcd2013-07-22 17:58:35 +120047
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120048 protected IFloodlightProviderService floodlightProvider;
49 protected ITopologyService topology;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120050 protected IDeviceService devices;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120051
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120052 protected Map<InetAddress, ArpTableEntry> arpTable;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120053
Jonathan Hart4dfc3652013-08-02 20:22:36 +120054 protected SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120055
56 private class ArpRequest {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120057 private IArpRequester requester;
58 private boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120059 private long requestTime;
60
Jonathan Hart4dfc3652013-08-02 20:22:36 +120061 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120062 this.requester = requester;
63 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120064 this.requestTime = System.currentTimeMillis();
65 }
66
Jonathan Hart4dfc3652013-08-02 20:22:36 +120067 public ArpRequest(ArpRequest old) {
68 this.requester = old.requester;
69 this.retry = old.retry;
70 this.requestTime = System.currentTimeMillis();
71 }
72
Jonathan Hart4dfc3652013-08-02 20:22:36 +120073 public boolean isExpired() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +120074 return (System.currentTimeMillis() - requestTime)
75 > IProxyArpService.ARP_REQUEST_TIMEOUT;
76 }
77
Jonathan Hart4dfc3652013-08-02 20:22:36 +120078 public boolean shouldRetry() {
79 return retry;
80 }
81
82 public synchronized void dispatchReply(InetAddress ipAddress, byte[] replyMacAddress) {
83 log.debug("Dispatching reply for {} to {}", ipAddress.getHostAddress(),
84 requester);
85 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120086 }
87 }
88
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120089 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart6261dcd2013-07-22 17:58:35 +120090 ITopologyService topology, IDeviceService devices){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120091 this.floodlightProvider = floodlightProvider;
92 this.topology = topology;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120093 this.devices = devices;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120094
95 arpTable = new HashMap<InetAddress, ArpTableEntry>();
Jonathan Hartdf6ec332013-08-04 01:37:14 +120096
Jonathan Hart4dfc3652013-08-02 20:22:36 +120097 arpRequests = Multimaps.synchronizedSetMultimap(
98 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart6261dcd2013-07-22 17:58:35 +120099
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200100 Timer arpTimer = new Timer();
101 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200102 @Override
103 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200104 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200105 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200106 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200107 }
108
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200109 /*
110 * Function that runs periodically to manage the asynchronous request mechanism.
111 * It basically cleans up old ARP requests if we don't get a response for them.
112 * The caller can designate that a request should be retried indefinitely, and
113 * this task will handle that as well.
114 */
115 private void doPeriodicArpProcessing() {
116 SetMultimap<InetAddress, ArpRequest> retryList
117 = HashMultimap.<InetAddress, ArpRequest>create();
118
119 //Have to synchronize externally on the Multimap while using an iterator,
120 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200121 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200122 log.debug("Current have {} outstanding requests",
123 arpRequests.size());
124
125 Iterator<Map.Entry<InetAddress, ArpRequest>> it
126 = arpRequests.entries().iterator();
127
128 while (it.hasNext()) {
129 Map.Entry<InetAddress, ArpRequest> entry
130 = it.next();
131 ArpRequest request = entry.getValue();
132 if (request.isExpired()) {
133 log.debug("Cleaning expired ARP request for {}",
134 entry.getKey().getHostAddress());
135
136 it.remove();
137
138 if (request.shouldRetry()) {
139 retryList.put(entry.getKey(), request);
140 }
141 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200142 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200143 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200144
145 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
146 : retryList.asMap().entrySet()) {
147
148 InetAddress address = entry.getKey();
149
150 log.debug("Resending ARP request for {}", address.getHostAddress());
151
152 sendArpRequestForAddress(address);
153
154 for (ArpRequest request : entry.getValue()) {
155 arpRequests.put(address, new ArpRequest(request));
156 }
157 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200158 }
159
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200160 @Override
161 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200162 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200163 }
164
165 @Override
166 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200167 return false;
168 }
169
170 @Override
171 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200172 return false;
173 }
174
175 @Override
176 public Command receive(
177 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
178
179 if (msg.getType() != OFType.PACKET_IN){
180 return Command.CONTINUE;
181 }
182
183 OFPacketIn pi = (OFPacketIn) msg;
184
185 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
186 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
187
188 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200189 ARP arp = (ARP) eth.getPayload();
190
191 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200192 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200193 }
194 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200195 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200196 }
197 }
198
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200199 //TODO should we propagate ARP or swallow it?
200 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200201 return Command.CONTINUE;
202 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200203
204 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
205 log.debug("ARP request received for {}",
206 bytesToStringAddr(arp.getTargetProtocolAddress()));
207
208 byte[] mac = lookupArpTable(arp.getTargetProtocolAddress());
209
210 if (mac == null){
211 //Mac address is not in our arp table.
212
213 //TODO check what the DeviceManager thinks
214
215 //Record where the request came from so we know where to send the reply
216 InetAddress target;
217 try {
218 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
219 } catch (UnknownHostException e) {
220 log.debug("Invalid address in ARP request", e);
221 //return Command.CONTINUE; //Continue or stop?
222 return;
223 }
224
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200225 boolean shouldBroadcastRequest = false;
226 synchronized (arpRequests) {
227 if (!arpRequests.containsKey(target)) {
228 shouldBroadcastRequest = true;
229 }
230 arpRequests.put(target, new ArpRequest(
231 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
232 }
233
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200234 //Flood the request out edge ports
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200235 if (shouldBroadcastRequest) {
236 broadcastArpRequestOutEdge(pi.getPacketData(), sw.getId(), pi.getInPort());
237 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200238 }
239 else {
240 //We know the address, so send a reply
241 log.debug("Sending reply of {}", MACAddress.valueOf(mac).toString());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200242 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
243 }
244 }
245
246 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200247 log.debug("ARP reply recieved for {}, is {}, on {}/{}", new Object[] {
248 bytesToStringAddr(arp.getSenderProtocolAddress()),
249 HexString.toHexString(arp.getSenderHardwareAddress()),
250 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200251
252 updateArpTable(arp);
253
254 //See if anyone's waiting for this ARP reply
255 InetAddress addr;
256 try {
257 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
258 } catch (UnknownHostException e) {
259 return;
260 }
261
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200262 Set<ArpRequest> requests = arpRequests.get(addr);
263
264 //Synchronize on the Multimap while using an iterator for one of the sets
265 synchronized (arpRequests) {
266 Iterator<ArpRequest> it = requests.iterator();
267 while (it.hasNext()) {
268 ArpRequest request = it.next();
269 it.remove();
270 request.dispatchReply(addr, arp.getSenderHardwareAddress());
271 }
272 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200273 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200274
275 private synchronized byte[] lookupArpTable(byte[] ipAddress){
276 InetAddress addr;
277 try {
278 addr = InetAddress.getByAddress(ipAddress);
279 } catch (UnknownHostException e) {
280 log.warn("Unable to create InetAddress", e);
281 return null;
282 }
283
284 ArpTableEntry arpEntry = arpTable.get(addr);
285
286 if (arpEntry == null){
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200287 log.debug("MAC for {} unknown", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200288 return null;
289 }
290
291 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
292 > ARP_ENTRY_TIMEOUT){
293 //Entry has timed out so we'll remove it and return null
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200294 log.debug("Timing out old ARP entry for {}", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200295 arpTable.remove(addr);
296 return null;
297 }
298
299 return arpEntry.getMacAddress();
300 }
301
302 private synchronized void updateArpTable(ARP arp){
303 InetAddress addr;
304 try {
305 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
306 } catch (UnknownHostException e) {
307 log.warn("Unable to create InetAddress", e);
308 return;
309 }
310
311 ArpTableEntry arpEntry = arpTable.get(addr);
312
313 if (arpEntry != null
314 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
315 arpEntry.setTimeLastSeen(System.currentTimeMillis());
316 }
317 else {
318 arpTable.put(addr,
319 new ArpTableEntry(arp.getSenderHardwareAddress(),
320 System.currentTimeMillis()));
321 }
322 }
323
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200324 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200325 //TODO what should the sender IP address be? Probably not 0.0.0.0
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200326 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
327 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200328 byte[] bgpdMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
329 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200330 (byte)0xff, (byte)0xff, (byte)0xff};
331
332 ARP arpRequest = new ARP();
333
334 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
335 .setProtocolType(ARP.PROTO_TYPE_IP)
336 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
337 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
338 .setOpCode(ARP.OP_REQUEST)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200339 .setSenderHardwareAddress(bgpdMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200340 .setSenderProtocolAddress(zeroIpv4)
341 .setTargetHardwareAddress(zeroMac)
342 .setTargetProtocolAddress(ipAddress.getAddress());
343
344 Ethernet eth = new Ethernet();
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200345 //eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
346 eth.setSourceMACAddress(bgpdMac)
347 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200348 .setEtherType(Ethernet.TYPE_ARP)
349 .setPayload(arpRequest);
350
351 broadcastArpRequestOutEdge(eth.serialize(), 0, OFPort.OFPP_NONE.getValue());
352 }
353
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200354 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200355 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
356 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
357 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
358
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200359 if (linkPorts == null){
360 //I think this means the switch isn't known to topology yet.
361 //Maybe it only just joined.
362 continue;
363 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200364
365 OFPacketOut po = new OFPacketOut();
366 po.setInPort(OFPort.OFPP_NONE)
367 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200368 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200369
370 List<OFAction> actions = new ArrayList<OFAction>();
371
372 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200373 if (linkPorts.contains(portNum) ||
374 (sw.getId() == inSwitch && portNum == inPort)){
375 //If this port isn't an edge port or is the ingress port
376 //for the ARP, don't broadcast out it
377 continue;
378 }
379
380 actions.add(new OFActionOutput(portNum));
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200381 //log.debug("Broadcasting out {}/{}", HexString.toHexString(sw.getId()), portNum);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200382 }
383
384 po.setActions(actions);
385 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
386 po.setActionsLength(actionsLength);
387 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200388 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200389
390 List<OFMessage> msgList = new ArrayList<OFMessage>();
391 msgList.add(po);
392
393 try {
394 sw.write(msgList, null);
395 sw.flush();
396 } catch (IOException e) {
397 log.error("Failure writing packet out to switch", e);
398 }
399 }
400 }
401
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200402 public void sendArpReply(ARP arpRequest, long dpid, short port, byte[] targetMac) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200403 ARP arpReply = new ARP();
404 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
405 .setProtocolType(ARP.PROTO_TYPE_IP)
406 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
407 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
408 .setOpCode(ARP.OP_REPLY)
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200409 .setSenderHardwareAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200410 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
411 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
412 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
413
414 Ethernet eth = new Ethernet();
415 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200416 .setSourceMACAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200417 .setEtherType(Ethernet.TYPE_ARP)
418 .setPayload(arpReply);
419
420 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200421 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200422
423 OFPacketOut po = new OFPacketOut();
424 po.setInPort(OFPort.OFPP_NONE)
425 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200426 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200427 .setActions(actions)
428 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
429 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
430 + po.getPacketData().length);
431
432 List<OFMessage> msgList = new ArrayList<OFMessage>();
433 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200434
435 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
436
437 if (sw == null) {
438 return;
439 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200440
441 try {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200442 log.debug("Sending ARP reply to {}/{}", HexString.toHexString(sw.getId()), port);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200443 sw.write(msgList, null);
444 sw.flush();
445 } catch (IOException e) {
446 log.warn("Failure writing packet out to switch", e);
447 }
448 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200449
450 //TODO this should be put somewhere more central. I use it in BgpRoute as well.
451 //We need a HexString.toHexString() equivalent.
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200452 private String bytesToStringAddr(byte[] bytes) {
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200453 InetAddress addr;
454 try {
455 addr = InetAddress.getByAddress(bytes);
456 } catch (UnknownHostException e) {
Jonathan Hartc824ad02013-07-03 15:58:45 +1200457 log.warn(" ", e);
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200458 return "";
459 }
460 if (addr == null) return "";
461 else return addr.getHostAddress();
462 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200463
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200464 @Override
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200465 public byte[] getMacAddress(InetAddress ipAddress) {
466 return lookupArpTable(ipAddress.getAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200467 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200468
469 @Override
470 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
471 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200472 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
473 //storeRequester(ipAddress, requester, retry);
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200474
475 sendArpRequestForAddress(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200476 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200477}