blob: 112fb986388c0897d4da57880f497e70dc92f311 [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 Hart6261dcd2013-07-22 17:58:35 +120046 private final long ARP_REQUEST_TIMEOUT_THREAD_PERIOD = 60000; //ms (== 1 min)
47
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;
53
Jonathan Hart6261dcd2013-07-22 17:58:35 +120054 //protected ConcurrentHashMap<InetAddress, Set<ArpRequest>> arpRequests;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120055 //protected ConcurrentHashMap<InetAddress, ArpRequest> arpRequests;
56 protected SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120057
58 private class ArpRequest {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120059 //private Set<IArpRequester> requesters;
60 private IArpRequester requester;
61 private boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120062 private long requestTime;
63
Jonathan Hart4dfc3652013-08-02 20:22:36 +120064 public ArpRequest(IArpRequester requester, boolean retry){
65 //this.requesters = new HashSet<IArpRequester>();
66 this.requester = requester;
67 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120068 this.requestTime = System.currentTimeMillis();
69 }
70
Jonathan Hart4dfc3652013-08-02 20:22:36 +120071 public ArpRequest(ArpRequest old) {
72 this.requester = old.requester;
73 this.retry = old.retry;
74 this.requestTime = System.currentTimeMillis();
75 }
76
77 /*
Jonathan Hart6261dcd2013-07-22 17:58:35 +120078 public synchronized void addRequester(IArpRequester requester){
79 requestTime = System.currentTimeMillis();
80 requesters.add(requester);
81 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +120082 */
Jonathan Hart6261dcd2013-07-22 17:58:35 +120083
Jonathan Hart4dfc3652013-08-02 20:22:36 +120084 public boolean isExpired() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +120085 return (System.currentTimeMillis() - requestTime)
86 > IProxyArpService.ARP_REQUEST_TIMEOUT;
87 }
88
Jonathan Hart4dfc3652013-08-02 20:22:36 +120089 public boolean shouldRetry() {
90 return retry;
91 }
92
93 public synchronized void dispatchReply(InetAddress ipAddress, byte[] replyMacAddress) {
94 log.debug("Dispatching reply for {} to {}", ipAddress.getHostAddress(),
95 requester);
96 requester.arpResponse(ipAddress, replyMacAddress);
97 /*
Jonathan Hart6261dcd2013-07-22 17:58:35 +120098 for (IArpRequester requester : requesters){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120099 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200100 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200101 */
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200102 }
103 }
104
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200105 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200106 ITopologyService topology, IDeviceService devices){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200107 this.floodlightProvider = floodlightProvider;
108 this.topology = topology;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200109 this.devices = devices;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200110
111 arpTable = new HashMap<InetAddress, ArpTableEntry>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200112 //arpRequests = new ConcurrentHashMap<InetAddress, Set<ArpRequest>>();
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200113 //arpRequests = new ConcurrentHashMap<InetAddress, ArpRequest>();
114 arpRequests = Multimaps.synchronizedSetMultimap(
115 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200116
117 Timer arpRequestTimeoutTimer = new Timer();
118 arpRequestTimeoutTimer.scheduleAtFixedRate(new TimerTask() {
119 @Override
120 public void run() {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200121 /*
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200122 synchronized (arpRequests) {
123 log.debug("Current have {} outstanding requests",
124 arpRequests.size());
125
126 Iterator<Map.Entry<InetAddress, ArpRequest>> it
127 = arpRequests.entrySet().iterator();
128
129 while (it.hasNext()){
130 Map.Entry<InetAddress, ArpRequest> entry
131 = it.next();
132
133 if (entry.getValue().isExpired()){
134 log.debug("Cleaning expired ARP request for {}",
135 entry.getKey().getHostAddress());
136 it.remove();
137 }
138 }
139 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200140 */
141
142 //List<ArpRequest> retryList = new ArrayList<ArpRequest>();
143 SetMultimap<InetAddress, ArpRequest> retryList
144 = HashMultimap.<InetAddress, ArpRequest>create();
145
146 //Have to synchronize externally on the Multimap while using an iterator.
147 //But because we're using a synchronizedMultiMap we don't have to
148 //synchronize when adding.
149 synchronized (arpRequests) {
150 log.debug("Current have {} outstanding requests",
151 arpRequests.size());
152
153 Iterator<Map.Entry<InetAddress, ArpRequest>> it
154 = arpRequests.entries().iterator();
155
156 while (it.hasNext()) {
157 Map.Entry<InetAddress, ArpRequest> entry
158 = it.next();
159 ArpRequest request = entry.getValue();
160 if (request.isExpired()) {
161 log.debug("Cleaning expired ARP request for {}",
162 entry.getKey().getHostAddress());
163 //TODO retry here? Caller should specify whether we retry or not
164 //because we obviously won't retry received packets, only
165 //requests from other app modules.
166 it.remove();
167
168 //retryList.add(new ArpRequest(request));
169 if (request.shouldRetry()) {
170 retryList.put(entry.getKey(), request);
171 }
172 }
173 }
174 }
175
176 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
177 : retryList.asMap().entrySet()) {
178
179 InetAddress address = entry.getKey();
180
181 log.debug("Resending ARP request for {}", address.getHostAddress());
182
183 sendArpRequestForAddress(address);
184
185 for (ArpRequest request : entry.getValue()) {
186 arpRequests.put(address, new ArpRequest(request));
187 }
188 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200189 }
190 }, 0, ARP_REQUEST_TIMEOUT_THREAD_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200191 }
192
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200193 private void storeRequester(InetAddress address, IArpRequester requester,
194 boolean retry) {
195 arpRequests.put(address, new ArpRequest(requester, retry));
196 /*
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200197 synchronized (arpRequests) {
198 if (arpRequests.get(address) == null) {
199 arpRequests.put(address, new ArpRequest());
200 }
201 ArpRequest request = arpRequests.get(address);
202
203 request.addRequester(requester);
204 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200205 */
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200206 }
207
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200208 @Override
209 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200210 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200211 }
212
213 @Override
214 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200215 return false;
216 }
217
218 @Override
219 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200220 return false;
221 }
222
223 @Override
224 public Command receive(
225 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
226
227 if (msg.getType() != OFType.PACKET_IN){
228 return Command.CONTINUE;
229 }
230
231 OFPacketIn pi = (OFPacketIn) msg;
232
233 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
234 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
235
236 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200237 ARP arp = (ARP) eth.getPayload();
238
239 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200240 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200241 }
242 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200243 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200244 }
245 }
246
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200247 //TODO should we propagate ARP or swallow it?
248 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200249 return Command.CONTINUE;
250 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200251
252 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
253 log.debug("ARP request received for {}",
254 bytesToStringAddr(arp.getTargetProtocolAddress()));
255
256 byte[] mac = lookupArpTable(arp.getTargetProtocolAddress());
257
258 if (mac == null){
259 //Mac address is not in our arp table.
260
261 //TODO check what the DeviceManager thinks
262
263 //Record where the request came from so we know where to send the reply
264 InetAddress target;
265 try {
266 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
267 } catch (UnknownHostException e) {
268 log.debug("Invalid address in ARP request", e);
269 //return Command.CONTINUE; //Continue or stop?
270 return;
271 }
272
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200273 //storeRequester(target, new HostArpRequester(this, arp, sw.getId(),
274 // pi.getInPort()));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200275
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200276 boolean shouldBroadcastRequest = false;
277 synchronized (arpRequests) {
278 if (!arpRequests.containsKey(target)) {
279 shouldBroadcastRequest = true;
280 }
281 arpRequests.put(target, new ArpRequest(
282 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
283 }
284
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200285 //Flood the request out edge ports
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200286 if (shouldBroadcastRequest) {
287 broadcastArpRequestOutEdge(pi.getPacketData(), sw.getId(), pi.getInPort());
288 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200289 }
290 else {
291 //We know the address, so send a reply
292 log.debug("Sending reply of {}", MACAddress.valueOf(mac).toString());
293 //sendArpReply(arp, pi, mac, sw);
294 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
295 }
296 }
297
298 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200299 log.debug("ARP reply recieved for {}, is {}, on {}/{}", new Object[] {
300 bytesToStringAddr(arp.getSenderProtocolAddress()),
301 HexString.toHexString(arp.getSenderHardwareAddress()),
302 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200303
304 updateArpTable(arp);
305
306 //See if anyone's waiting for this ARP reply
307 InetAddress addr;
308 try {
309 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
310 } catch (UnknownHostException e) {
311 return;
312 }
313
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200314 Set<ArpRequest> requests = arpRequests.get(addr);
315
316 //Synchronize on the Multimap while using an iterator for one of the sets
317 synchronized (arpRequests) {
318 Iterator<ArpRequest> it = requests.iterator();
319 while (it.hasNext()) {
320 ArpRequest request = it.next();
321 it.remove();
322 request.dispatchReply(addr, arp.getSenderHardwareAddress());
323 }
324 }
325 /*
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200326 ArpRequest request = null;
327 synchronized (arpRequests) {
328 request = arpRequests.get(addr);
329 if (request != null) {
330 arpRequests.remove(addr);
331 }
332 }
333 if (request != null && !request.isExpired()) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200334 request.dispatchReply(addr, arp.getSenderHardwareAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200335 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200336 */
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200337
338 /*
339 Set<ArpRequest> requests = arpRequests.get(addr);
340 if (requests != null){
341
342 synchronized (requests) {
343 for (ArpRequest request : requests) {
344 if (!request.isExpired()){
345 request.getRequester().arpResponse(
346 arp.getSenderHardwareAddress());
347 }
348 }
349 }
350 }*/
351 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200352
353 private synchronized byte[] lookupArpTable(byte[] ipAddress){
354 InetAddress addr;
355 try {
356 addr = InetAddress.getByAddress(ipAddress);
357 } catch (UnknownHostException e) {
358 log.warn("Unable to create InetAddress", e);
359 return null;
360 }
361
362 ArpTableEntry arpEntry = arpTable.get(addr);
363
364 if (arpEntry == null){
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200365 log.debug("MAC for {} unknown", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200366 return null;
367 }
368
369 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
370 > ARP_ENTRY_TIMEOUT){
371 //Entry has timed out so we'll remove it and return null
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200372 log.debug("Timing out old ARP entry for {}", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200373 arpTable.remove(addr);
374 return null;
375 }
376
377 return arpEntry.getMacAddress();
378 }
379
380 private synchronized void updateArpTable(ARP arp){
381 InetAddress addr;
382 try {
383 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
384 } catch (UnknownHostException e) {
385 log.warn("Unable to create InetAddress", e);
386 return;
387 }
388
389 ArpTableEntry arpEntry = arpTable.get(addr);
390
391 if (arpEntry != null
392 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
393 arpEntry.setTimeLastSeen(System.currentTimeMillis());
394 }
395 else {
396 arpTable.put(addr,
397 new ArpTableEntry(arp.getSenderHardwareAddress(),
398 System.currentTimeMillis()));
399 }
400 }
401
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200402 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200403 //TODO what should the sender IP address be? Probably not 0.0.0.0
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200404 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
405 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200406 byte[] bgpdMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
407 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200408 (byte)0xff, (byte)0xff, (byte)0xff};
409
410 ARP arpRequest = new ARP();
411
412 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
413 .setProtocolType(ARP.PROTO_TYPE_IP)
414 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
415 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
416 .setOpCode(ARP.OP_REQUEST)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200417 .setSenderHardwareAddress(bgpdMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200418 .setSenderProtocolAddress(zeroIpv4)
419 .setTargetHardwareAddress(zeroMac)
420 .setTargetProtocolAddress(ipAddress.getAddress());
421
422 Ethernet eth = new Ethernet();
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200423 //eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
424 eth.setSourceMACAddress(bgpdMac)
425 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200426 .setEtherType(Ethernet.TYPE_ARP)
427 .setPayload(arpRequest);
428
429 broadcastArpRequestOutEdge(eth.serialize(), 0, OFPort.OFPP_NONE.getValue());
430 }
431
432 //private void broadcastArpRequestOutEdge(OFPacketIn pi, long inSwitch, short inPort){
433 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200434 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
435 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
436 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
437
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200438 if (linkPorts == null){
439 //I think this means the switch isn't known to topology yet.
440 //Maybe it only just joined.
441 continue;
442 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200443
444 OFPacketOut po = new OFPacketOut();
445 po.setInPort(OFPort.OFPP_NONE)
446 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200447 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200448
449 List<OFAction> actions = new ArrayList<OFAction>();
450
451 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200452 if (linkPorts.contains(portNum) ||
453 (sw.getId() == inSwitch && portNum == inPort)){
454 //If this port isn't an edge port or is the ingress port
455 //for the ARP, don't broadcast out it
456 continue;
457 }
458
459 actions.add(new OFActionOutput(portNum));
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200460 log.debug("Broadcasting out {}/{}", HexString.toHexString(sw.getId()), portNum);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200461 }
462
463 po.setActions(actions);
464 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
465 po.setActionsLength(actionsLength);
466 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200467 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200468
469 List<OFMessage> msgList = new ArrayList<OFMessage>();
470 msgList.add(po);
471
472 try {
473 sw.write(msgList, null);
474 sw.flush();
475 } catch (IOException e) {
476 log.error("Failure writing packet out to switch", e);
477 }
478 }
479 }
480
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200481 public void sendArpReply(ARP arpRequest, long dpid, short port, byte[] targetMac) {
482 //private void sendArpReply(ARP arpRequest, OFPacketIn pi, byte[] macRequested, IOFSwitch sw){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200483 ARP arpReply = new ARP();
484 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
485 .setProtocolType(ARP.PROTO_TYPE_IP)
486 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
487 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
488 .setOpCode(ARP.OP_REPLY)
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200489 //.setSenderHardwareAddress(macRequested)
490 .setSenderHardwareAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200491 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
492 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
493 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
494
495 Ethernet eth = new Ethernet();
496 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200497 //.setSourceMACAddress(macRequested)
498 .setSourceMACAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200499 .setEtherType(Ethernet.TYPE_ARP)
500 .setPayload(arpReply);
501
502 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200503 //actions.add(new OFActionOutput(pi.getInPort()));
504 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200505
506 OFPacketOut po = new OFPacketOut();
507 po.setInPort(OFPort.OFPP_NONE)
508 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200509 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200510 .setActions(actions)
511 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
512 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
513 + po.getPacketData().length);
514
515 List<OFMessage> msgList = new ArrayList<OFMessage>();
516 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200517
518 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
519
520 if (sw == null) {
521 return;
522 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200523
524 try {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200525 log.debug("Sending ARP reply to {}/{}", HexString.toHexString(sw.getId()), port);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200526 sw.write(msgList, null);
527 sw.flush();
528 } catch (IOException e) {
529 log.warn("Failure writing packet out to switch", e);
530 }
531 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200532
533 //TODO this should be put somewhere more central. I use it in BgpRoute as well.
534 //We need a HexString.toHexString() equivalent.
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200535 private String bytesToStringAddr(byte[] bytes) {
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200536 InetAddress addr;
537 try {
538 addr = InetAddress.getByAddress(bytes);
539 } catch (UnknownHostException e) {
Jonathan Hartc824ad02013-07-03 15:58:45 +1200540 log.warn(" ", e);
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200541 return "";
542 }
543 if (addr == null) return "";
544 else return addr.getHostAddress();
545 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200546
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200547 @Override
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200548 public byte[] getMacAddress(InetAddress ipAddress) {
549 return lookupArpTable(ipAddress.getAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200550 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200551
552 @Override
553 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
554 boolean retry) {
555 /*
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200556 byte[] lookupMac;
557 if ((lookupMac = lookupArpTable(ipAddress.getAddress())) == null) {
558 return lookupMac;
559 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200560 */
561
562 storeRequester(ipAddress, requester, retry);
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200563
564 sendArpRequestForAddress(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200565 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200566}