blob: 8ef388a2c5584b781b0b44913f6f17dc17219ec8 [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 Hart4dfc3652013-08-02 20:22:36 +1200226 boolean shouldBroadcastRequest = false;
227 synchronized (arpRequests) {
228 if (!arpRequests.containsKey(target)) {
229 shouldBroadcastRequest = true;
230 }
231 arpRequests.put(target, new ArpRequest(
232 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
233 }
234
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200235 //Flood the request out edge ports
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200236 if (shouldBroadcastRequest) {
237 broadcastArpRequestOutEdge(pi.getPacketData(), sw.getId(), pi.getInPort());
238 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200239 }
240 else {
241 //We know the address, so send a reply
242 log.debug("Sending reply of {}", MACAddress.valueOf(mac).toString());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200243 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
244 }
245 }
246
247 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200248 log.debug("ARP reply recieved for {}, is {}, on {}/{}", new Object[] {
249 bytesToStringAddr(arp.getSenderProtocolAddress()),
250 HexString.toHexString(arp.getSenderHardwareAddress()),
251 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200252
253 updateArpTable(arp);
254
255 //See if anyone's waiting for this ARP reply
256 InetAddress addr;
257 try {
258 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
259 } catch (UnknownHostException e) {
260 return;
261 }
262
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200263 Set<ArpRequest> requests = arpRequests.get(addr);
264
265 //Synchronize on the Multimap while using an iterator for one of the sets
266 synchronized (arpRequests) {
267 Iterator<ArpRequest> it = requests.iterator();
268 while (it.hasNext()) {
269 ArpRequest request = it.next();
270 it.remove();
271 request.dispatchReply(addr, arp.getSenderHardwareAddress());
272 }
273 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200274 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200275
276 private synchronized byte[] lookupArpTable(byte[] ipAddress){
277 InetAddress addr;
278 try {
279 addr = InetAddress.getByAddress(ipAddress);
280 } catch (UnknownHostException e) {
281 log.warn("Unable to create InetAddress", e);
282 return null;
283 }
284
285 ArpTableEntry arpEntry = arpTable.get(addr);
286
287 if (arpEntry == null){
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200288 log.debug("MAC for {} unknown", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200289 return null;
290 }
291
292 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
293 > ARP_ENTRY_TIMEOUT){
294 //Entry has timed out so we'll remove it and return null
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200295 log.debug("Timing out old ARP entry for {}", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200296 arpTable.remove(addr);
297 return null;
298 }
299
300 return arpEntry.getMacAddress();
301 }
302
303 private synchronized void updateArpTable(ARP arp){
304 InetAddress addr;
305 try {
306 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
307 } catch (UnknownHostException e) {
308 log.warn("Unable to create InetAddress", e);
309 return;
310 }
311
312 ArpTableEntry arpEntry = arpTable.get(addr);
313
314 if (arpEntry != null
315 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
316 arpEntry.setTimeLastSeen(System.currentTimeMillis());
317 }
318 else {
319 arpTable.put(addr,
320 new ArpTableEntry(arp.getSenderHardwareAddress(),
321 System.currentTimeMillis()));
322 }
323 }
324
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200325 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200326 //TODO what should the sender IP address be? Probably not 0.0.0.0
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200327 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
328 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200329 byte[] bgpdMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
330 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200331 (byte)0xff, (byte)0xff, (byte)0xff};
332
333 ARP arpRequest = new ARP();
334
335 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
336 .setProtocolType(ARP.PROTO_TYPE_IP)
337 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
338 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
339 .setOpCode(ARP.OP_REQUEST)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200340 .setSenderHardwareAddress(bgpdMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200341 .setSenderProtocolAddress(zeroIpv4)
342 .setTargetHardwareAddress(zeroMac)
343 .setTargetProtocolAddress(ipAddress.getAddress());
344
345 Ethernet eth = new Ethernet();
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200346 //eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
347 eth.setSourceMACAddress(bgpdMac)
348 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200349 .setEtherType(Ethernet.TYPE_ARP)
350 .setPayload(arpRequest);
351
352 broadcastArpRequestOutEdge(eth.serialize(), 0, OFPort.OFPP_NONE.getValue());
353 }
354
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200355 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200356 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
357 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
358 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
359
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200360 if (linkPorts == null){
361 //I think this means the switch isn't known to topology yet.
362 //Maybe it only just joined.
363 continue;
364 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200365
366 OFPacketOut po = new OFPacketOut();
367 po.setInPort(OFPort.OFPP_NONE)
368 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200369 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200370
371 List<OFAction> actions = new ArrayList<OFAction>();
372
373 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200374 if (linkPorts.contains(portNum) ||
375 (sw.getId() == inSwitch && portNum == inPort)){
376 //If this port isn't an edge port or is the ingress port
377 //for the ARP, don't broadcast out it
378 continue;
379 }
380
381 actions.add(new OFActionOutput(portNum));
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200382 //log.debug("Broadcasting out {}/{}", HexString.toHexString(sw.getId()), portNum);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200383 }
384
385 po.setActions(actions);
386 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
387 po.setActionsLength(actionsLength);
388 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200389 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200390
391 List<OFMessage> msgList = new ArrayList<OFMessage>();
392 msgList.add(po);
393
394 try {
395 sw.write(msgList, null);
396 sw.flush();
397 } catch (IOException e) {
398 log.error("Failure writing packet out to switch", e);
399 }
400 }
401 }
402
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200403 public void sendArpReply(ARP arpRequest, long dpid, short port, byte[] targetMac) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200404 ARP arpReply = new ARP();
405 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
406 .setProtocolType(ARP.PROTO_TYPE_IP)
407 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
408 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
409 .setOpCode(ARP.OP_REPLY)
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200410 .setSenderHardwareAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200411 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
412 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
413 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
414
415 Ethernet eth = new Ethernet();
416 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200417 .setSourceMACAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200418 .setEtherType(Ethernet.TYPE_ARP)
419 .setPayload(arpReply);
420
421 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200422 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200423
424 OFPacketOut po = new OFPacketOut();
425 po.setInPort(OFPort.OFPP_NONE)
426 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200427 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200428 .setActions(actions)
429 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
430 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
431 + po.getPacketData().length);
432
433 List<OFMessage> msgList = new ArrayList<OFMessage>();
434 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200435
436 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
437
438 if (sw == null) {
439 return;
440 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200441
442 try {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200443 log.debug("Sending ARP reply to {}/{}", HexString.toHexString(sw.getId()), port);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200444 sw.write(msgList, null);
445 sw.flush();
446 } catch (IOException e) {
447 log.warn("Failure writing packet out to switch", e);
448 }
449 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200450
451 //TODO this should be put somewhere more central. I use it in BgpRoute as well.
452 //We need a HexString.toHexString() equivalent.
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200453 private String bytesToStringAddr(byte[] bytes) {
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200454 InetAddress addr;
455 try {
456 addr = InetAddress.getByAddress(bytes);
457 } catch (UnknownHostException e) {
Jonathan Hartc824ad02013-07-03 15:58:45 +1200458 log.warn(" ", e);
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200459 return "";
460 }
461 if (addr == null) return "";
462 else return addr.getHostAddress();
463 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200464
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200465 @Override
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200466 public byte[] getMacAddress(InetAddress ipAddress) {
467 return lookupArpTable(ipAddress.getAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200468 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200469
470 @Override
471 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
472 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200473 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
474 //storeRequester(ipAddress, requester, retry);
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200475
476 sendArpRequestForAddress(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200477 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200478}