blob: c16ead667d1acbad94912a639681507919ed219a [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;
20import net.floodlightcontroller.packet.ARP;
21import net.floodlightcontroller.packet.Ethernet;
22import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120023import net.floodlightcontroller.util.MACAddress;
Jonathan Hart2f790d22013-08-15 14:01:24 +120024import net.onrc.onos.ofcontroller.bgproute.IPatriciaTrie;
25import net.onrc.onos.ofcontroller.bgproute.Interface;
26import net.onrc.onos.ofcontroller.bgproute.Prefix;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120027
28import org.openflow.protocol.OFMessage;
29import org.openflow.protocol.OFPacketIn;
30import org.openflow.protocol.OFPacketOut;
31import org.openflow.protocol.OFPort;
32import org.openflow.protocol.OFType;
33import org.openflow.protocol.action.OFAction;
34import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120035import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
Jonathan Hart4dfc3652013-08-02 20:22:36 +120039import com.google.common.collect.HashMultimap;
40import com.google.common.collect.Multimaps;
41import com.google.common.collect.SetMultimap;
42
Jonathan Hart32e18222013-08-07 22:05:42 +120043//TODO have L2 and also L3 mode, where it takes into account interface addresses
Jonathan Harte751e1c2013-08-23 00:48:47 +120044//TODO REST API to inspect ARP table
Jonathan Hart6261dcd2013-07-22 17:58:35 +120045public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120046 private static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
47
Jonathan Hart6261dcd2013-07-22 17:58:35 +120048 private final long ARP_ENTRY_TIMEOUT = 600000; //ms (== 10 mins)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120049
Jonathan Hartdf6ec332013-08-04 01:37:14 +120050 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hart6261dcd2013-07-22 17:58:35 +120051
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120052 protected IFloodlightProviderService floodlightProvider;
53 protected ITopologyService topology;
54
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120055 protected Map<InetAddress, ArpTableEntry> arpTable;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120056
Jonathan Hart4dfc3652013-08-02 20:22:36 +120057 protected SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120058
Jonathan Hart2f790d22013-08-15 14:01:24 +120059 public enum Mode {L2_MODE, L3_MODE}
60
61 private Mode mode;
62 private IPatriciaTrie<Interface> interfacePtrie = null;
63 private MACAddress routerMacAddress = null;
64 //private SwitchPort bgpdAttachmentPoint = null;
65
Jonathan Hart6261dcd2013-07-22 17:58:35 +120066 private class ArpRequest {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120067 private IArpRequester requester;
68 private boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120069 private long requestTime;
70
Jonathan Hart4dfc3652013-08-02 20:22:36 +120071 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120072 this.requester = requester;
73 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120074 this.requestTime = System.currentTimeMillis();
75 }
76
Jonathan Hart4dfc3652013-08-02 20:22:36 +120077 public ArpRequest(ArpRequest old) {
78 this.requester = old.requester;
79 this.retry = old.retry;
80 this.requestTime = System.currentTimeMillis();
81 }
82
Jonathan Hart4dfc3652013-08-02 20:22:36 +120083 public boolean isExpired() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +120084 return (System.currentTimeMillis() - requestTime)
85 > IProxyArpService.ARP_REQUEST_TIMEOUT;
86 }
87
Jonathan Hart4dfc3652013-08-02 20:22:36 +120088 public boolean shouldRetry() {
89 return retry;
90 }
91
Jonathan Hart32e18222013-08-07 22:05:42 +120092 public void dispatchReply(InetAddress ipAddress, byte[] replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120093 log.debug("Dispatching reply for {} to {}", ipAddress.getHostAddress(),
94 requester);
95 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120096 }
97 }
98
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120099 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart2f790d22013-08-15 14:01:24 +1200100 ITopologyService topology){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200101 this.floodlightProvider = floodlightProvider;
102 this.topology = topology;
103
104 arpTable = new HashMap<InetAddress, ArpTableEntry>();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200105
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200106 arpRequests = Multimaps.synchronizedSetMultimap(
107 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200108
Jonathan Hart2f790d22013-08-15 14:01:24 +1200109 mode = Mode.L2_MODE;
110 }
111
112 public void setL3Mode(IPatriciaTrie<Interface> interfacePtrie, MACAddress routerMacAddress) {
113 this.interfacePtrie = interfacePtrie;
114 this.routerMacAddress = routerMacAddress;
115 //this.bgpdAttachmentPoint = bgpdAttachmentPoint;
116 mode = Mode.L3_MODE;
117 }
118
119 public void startUp() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200120 Timer arpTimer = new Timer();
121 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200122 @Override
123 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200124 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200125 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200126 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200127 }
128
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200129 /*
130 * Function that runs periodically to manage the asynchronous request mechanism.
131 * It basically cleans up old ARP requests if we don't get a response for them.
132 * The caller can designate that a request should be retried indefinitely, and
133 * this task will handle that as well.
134 */
135 private void doPeriodicArpProcessing() {
136 SetMultimap<InetAddress, ArpRequest> retryList
137 = HashMultimap.<InetAddress, ArpRequest>create();
138
139 //Have to synchronize externally on the Multimap while using an iterator,
140 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200141 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200142 log.debug("Current have {} outstanding requests",
143 arpRequests.size());
144
145 Iterator<Map.Entry<InetAddress, ArpRequest>> it
146 = arpRequests.entries().iterator();
147
148 while (it.hasNext()) {
149 Map.Entry<InetAddress, ArpRequest> entry
150 = it.next();
151 ArpRequest request = entry.getValue();
152 if (request.isExpired()) {
153 log.debug("Cleaning expired ARP request for {}",
154 entry.getKey().getHostAddress());
155
156 it.remove();
157
158 if (request.shouldRetry()) {
159 retryList.put(entry.getKey(), request);
160 }
161 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200162 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200163 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200164
165 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
166 : retryList.asMap().entrySet()) {
167
168 InetAddress address = entry.getKey();
169
170 log.debug("Resending ARP request for {}", address.getHostAddress());
171
172 sendArpRequestForAddress(address);
173
174 for (ArpRequest request : entry.getValue()) {
175 arpRequests.put(address, new ArpRequest(request));
176 }
177 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200178 }
179
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200180 @Override
181 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200182 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200183 }
184
185 @Override
186 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200187 return false;
188 }
189
190 @Override
191 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200192 return false;
193 }
194
195 @Override
196 public Command receive(
197 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
198
199 if (msg.getType() != OFType.PACKET_IN){
200 return Command.CONTINUE;
201 }
202
203 OFPacketIn pi = (OFPacketIn) msg;
204
205 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
206 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
207
208 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200209 ARP arp = (ARP) eth.getPayload();
210
211 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200212 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200213 }
214 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200215 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200216 }
217 }
218
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200219 //TODO should we propagate ARP or swallow it?
220 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200221 return Command.CONTINUE;
222 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200223
224 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200225 log.trace("ARP request received for {}",
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200226 bytesToStringAddr(arp.getTargetProtocolAddress()));
Jonathan Hart2f790d22013-08-15 14:01:24 +1200227
228 InetAddress target;
Jonathan Hart6e618212013-08-21 22:28:43 +1200229 InetAddress source;
Jonathan Hart2f790d22013-08-15 14:01:24 +1200230 try {
231 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
Jonathan Hart6e618212013-08-21 22:28:43 +1200232 source = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200233 } catch (UnknownHostException e) {
234 log.debug("Invalid address in ARP request", e);
235 return;
236 }
237
238 if (mode == Mode.L3_MODE) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200239
240 if (originatedOutsideNetwork(source)) {
241 //If the request came from outside our network, we only care if
242 //it was a request for one of our interfaces.
243 if (isInterfaceAddress(target)) {
244 sendArpReply(arp, sw.getId(), pi.getInPort(), routerMacAddress.toBytes());
245 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200246 return;
247 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200248
249 /*
250 Interface intf = interfacePtrie.match(new Prefix(target.getAddress(), 32));
251 //if (intf != null && target.equals(intf.getIpAddress())) {
252 if (intf != null) {
253 if (target.equals(intf.getIpAddress())) {
254 //ARP request for one of our interfaces, we can reply straight away
255 sendArpReply(arp, sw.getId(), pi.getInPort(), routerMacAddress.toBytes());
256 }
257 // If we didn't enter the above if block, then we found a matching
258 // interface for the target IP but the request wasn't for us.
259 // This is someone else ARPing for a different host in the subnet.
260 // We shouldn't do anything in this case - if we let processing continue
261 // we'll end up erroneously re-broadcasting an ARP for someone else.
262 return;
263 }
264 */
Jonathan Hart2f790d22013-08-15 14:01:24 +1200265 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200266
267 byte[] mac = lookupArpTable(arp.getTargetProtocolAddress());
268
269 if (mac == null){
270 //Mac address is not in our arp table.
271
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200272 //Record where the request came from so we know where to send the reply
Jonathan Hart2f790d22013-08-15 14:01:24 +1200273
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200274
Jonathan Hart9ea31212013-08-12 21:40:34 +1200275 //Should we just broadcast all received requests here? Or rate limit
276 //if we know we just sent an request?
277 arpRequests.put(target, new ArpRequest(
278 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200279
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200280 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200281 //broadcastArpRequestOutEdge(pi.getPacketData(), sw.getId(), pi.getInPort());
282 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200283 }
284 else {
285 //We know the address, so send a reply
Jonathan Hart64c0b202013-08-20 15:45:07 +1200286 log.trace("Sending reply of {}", MACAddress.valueOf(mac).toString());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200287 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
288 }
289 }
290
291 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart64c0b202013-08-20 15:45:07 +1200292 log.trace("ARP reply recieved for {}, is {}, on {}/{}", new Object[] {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200293 bytesToStringAddr(arp.getSenderProtocolAddress()),
294 HexString.toHexString(arp.getSenderHardwareAddress()),
295 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200296
297 updateArpTable(arp);
298
299 //See if anyone's waiting for this ARP reply
300 InetAddress addr;
301 try {
302 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
303 } catch (UnknownHostException e) {
304 return;
305 }
306
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200307 Set<ArpRequest> requests = arpRequests.get(addr);
308
309 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200310 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200311 synchronized (arpRequests) {
312 Iterator<ArpRequest> it = requests.iterator();
313 while (it.hasNext()) {
314 ArpRequest request = it.next();
315 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200316 //request.dispatchReply(addr, arp.getSenderHardwareAddress());
317 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200318 }
319 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200320
321 //Don't hold an ARP lock while dispatching requests
322 for (ArpRequest request : requestsToSend) {
323 request.dispatchReply(addr, arp.getSenderHardwareAddress());
324 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200325 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200326
327 private synchronized byte[] lookupArpTable(byte[] ipAddress){
328 InetAddress addr;
329 try {
330 addr = InetAddress.getByAddress(ipAddress);
331 } catch (UnknownHostException e) {
332 log.warn("Unable to create InetAddress", e);
333 return null;
334 }
335
336 ArpTableEntry arpEntry = arpTable.get(addr);
337
338 if (arpEntry == null){
Jonathan Hart2f790d22013-08-15 14:01:24 +1200339 //log.debug("MAC for {} unknown", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200340 return null;
341 }
342
343 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
344 > ARP_ENTRY_TIMEOUT){
345 //Entry has timed out so we'll remove it and return null
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200346 log.debug("Timing out old ARP entry for {}", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200347 arpTable.remove(addr);
348 return null;
349 }
350
351 return arpEntry.getMacAddress();
352 }
353
354 private synchronized void updateArpTable(ARP arp){
355 InetAddress addr;
356 try {
357 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
358 } catch (UnknownHostException e) {
359 log.warn("Unable to create InetAddress", e);
360 return;
361 }
362
363 ArpTableEntry arpEntry = arpTable.get(addr);
364
365 if (arpEntry != null
366 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
367 arpEntry.setTimeLastSeen(System.currentTimeMillis());
368 }
369 else {
370 arpTable.put(addr,
371 new ArpTableEntry(arp.getSenderHardwareAddress(),
372 System.currentTimeMillis()));
373 }
374 }
375
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200376 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200377 //TODO what should the sender IP address be? Probably not 0.0.0.0
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200378 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
379 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart6e618212013-08-21 22:28:43 +1200380 //byte[] bgpdMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200381 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200382 (byte)0xff, (byte)0xff, (byte)0xff};
383
384 ARP arpRequest = new ARP();
385
386 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
387 .setProtocolType(ARP.PROTO_TYPE_IP)
388 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
389 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
390 .setOpCode(ARP.OP_REQUEST)
Jonathan Harte97fa642013-08-21 13:37:38 +1200391 //.setSenderHardwareAddress(bgpdMac)
392 .setSenderHardwareAddress(routerMacAddress.toBytes())
Jonathan Hart2f790d22013-08-15 14:01:24 +1200393 //.setSenderProtocolAddress(zeroIpv4)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200394 .setTargetHardwareAddress(zeroMac)
395 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200396
397 byte[] senderIPAddress = zeroIpv4;
398 if (mode == Mode.L3_MODE) {
399 Interface intf = interfacePtrie.match(new Prefix(ipAddress.getAddress(), 32));
400 if (intf != null) {
401 senderIPAddress = intf.getIpAddress().getAddress();
402 }
403 }
404
405 arpRequest.setSenderProtocolAddress(senderIPAddress);
406
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200407 Ethernet eth = new Ethernet();
Jonathan Hartb8c21532013-08-21 14:01:38 +1200408 eth.setSourceMACAddress(routerMacAddress.toBytes())
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200409 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200410 .setEtherType(Ethernet.TYPE_ARP)
411 .setPayload(arpRequest);
412
Jonathan Hart2f790d22013-08-15 14:01:24 +1200413 //broadcastArpRequestOutEdge(eth.serialize(), 0, OFPort.OFPP_NONE.getValue());
414 sendArpRequestToSwitches(ipAddress, eth.serialize());
415 }
416
417 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
418 sendArpRequestToSwitches(dstAddress, arpRequest, 0, OFPort.OFPP_NONE.getValue());
419 }
420 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
421 long inSwitch, short inPort) {
422 if (mode == Mode.L2_MODE) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200423 //log.debug("mode is l2");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200424 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
425 }
426 else if (mode == Mode.L3_MODE) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200427 //log.debug("mode is l3");
Jonathan Hart6e618212013-08-21 22:28:43 +1200428 //TODO the case where it should be broadcast out all non-interface
429 //edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200430 Interface intf = interfacePtrie.match(new Prefix(dstAddress.getAddress(), 32));
431 if (intf != null) {
432 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
433 }
434 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200435 }
436
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200437 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200438 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
439 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
440 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
441
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200442 if (linkPorts == null){
443 //I think this means the switch isn't known to topology yet.
444 //Maybe it only just joined.
445 continue;
446 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200447
448 OFPacketOut po = new OFPacketOut();
449 po.setInPort(OFPort.OFPP_NONE)
450 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200451 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200452
453 List<OFAction> actions = new ArrayList<OFAction>();
454
455 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200456 if (linkPorts.contains(portNum) ||
457 (sw.getId() == inSwitch && portNum == inPort)){
458 //If this port isn't an edge port or is the ingress port
459 //for the ARP, don't broadcast out it
460 continue;
461 }
462
463 actions.add(new OFActionOutput(portNum));
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200464 //log.debug("Broadcasting out {}/{}", HexString.toHexString(sw.getId()), portNum);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200465 }
466
467 po.setActions(actions);
468 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
469 po.setActionsLength(actionsLength);
470 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200471 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200472
473 List<OFMessage> msgList = new ArrayList<OFMessage>();
474 msgList.add(po);
475
476 try {
477 sw.write(msgList, null);
478 sw.flush();
479 } catch (IOException e) {
480 log.error("Failure writing packet out to switch", e);
481 }
482 }
483 }
484
Jonathan Hart2f790d22013-08-15 14:01:24 +1200485 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200486 log.debug("Sending ARP request out {}/{}", HexString.toHexString(dpid), port);
Jonathan Hart2f790d22013-08-15 14:01:24 +1200487
488 OFPacketOut po = new OFPacketOut();
489 po.setInPort(OFPort.OFPP_NONE)
490 .setBufferId(-1)
491 .setPacketData(arpRequest);
492
493 List<OFAction> actions = new ArrayList<OFAction>();
494 actions.add(new OFActionOutput(port));
495 po.setActions(actions);
496 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
497 po.setActionsLength(actionsLength);
498 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
499 + arpRequest.length);
500
501 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
502
503 if (sw == null) {
504 log.debug("Switch not found when sending ARP request");
505 return;
506 }
507
508 try {
509 sw.write(po, null);
510 sw.flush();
511 } catch (IOException e) {
512 log.error("Failure writing packet out to switch", e);
513 }
514 }
515
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200516 public void sendArpReply(ARP arpRequest, long dpid, short port, byte[] targetMac) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200517 ARP arpReply = new ARP();
518 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
519 .setProtocolType(ARP.PROTO_TYPE_IP)
520 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
521 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
522 .setOpCode(ARP.OP_REPLY)
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200523 .setSenderHardwareAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200524 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
525 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
526 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
527
528 Ethernet eth = new Ethernet();
529 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200530 .setSourceMACAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200531 .setEtherType(Ethernet.TYPE_ARP)
532 .setPayload(arpReply);
533
534 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200535 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200536
537 OFPacketOut po = new OFPacketOut();
538 po.setInPort(OFPort.OFPP_NONE)
539 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200540 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200541 .setActions(actions)
542 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
543 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
544 + po.getPacketData().length);
545
546 List<OFMessage> msgList = new ArrayList<OFMessage>();
547 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200548
549 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
550
551 if (sw == null) {
552 return;
553 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200554
555 try {
Jonathan Hart6e618212013-08-21 22:28:43 +1200556 log.debug("Sending ARP reply to {}/{}", HexString.toHexString(sw.getId()), port);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200557 sw.write(msgList, null);
558 sw.flush();
559 } catch (IOException e) {
560 log.warn("Failure writing packet out to switch", e);
561 }
562 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200563
564 //TODO this should be put somewhere more central. I use it in BgpRoute as well.
565 //We need a HexString.toHexString() equivalent.
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200566 private String bytesToStringAddr(byte[] bytes) {
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200567 InetAddress addr;
568 try {
569 addr = InetAddress.getByAddress(bytes);
570 } catch (UnknownHostException e) {
Jonathan Hartc824ad02013-07-03 15:58:45 +1200571 log.warn(" ", e);
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200572 return "";
573 }
574 if (addr == null) return "";
575 else return addr.getHostAddress();
576 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200577
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200578 @Override
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200579 public byte[] getMacAddress(InetAddress ipAddress) {
580 return lookupArpTable(ipAddress.getAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200581 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200582
583 @Override
584 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
585 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200586 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
587 //storeRequester(ipAddress, requester, retry);
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200588
Jonathan Hart6e618212013-08-21 22:28:43 +1200589 //Sanity check to make sure we don't send a request for our own address
590 if (!isInterfaceAddress(ipAddress)) {
591 sendArpRequestForAddress(ipAddress);
592 }
593 }
594
595 /*
596 * TODO These methods might be more suited to some kind of L3 information service
597 * that ProxyArpManager could query, rather than having the information
598 * embedded in ProxyArpManager. There may be many modules that need L3 information.
599 */
600
601 private boolean originatedOutsideNetwork(InetAddress source) {
602 Interface intf = interfacePtrie.match(new Prefix(source.getAddress(), 32));
603 if (intf != null) {
604 if (intf.getIpAddress().equals(source)) {
605 // This request must have been originated by us (the controller)
606 return false;
607 }
608 else {
609 // Source was in one of our interface subnets, but wasn't us.
610 // It must be external.
611 return true;
612 }
613 }
614 else {
615 // Source is not in one of our interface subnets. It's probably a host
616 // in our network as we should only receive ARPs broadcast by external
617 // hosts if they're in the same subnet.
618 return false;
619 }
620 }
621
622 private boolean isInterfaceAddress(InetAddress address) {
623 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
624 return (intf != null && intf.getIpAddress().equals(address));
625 }
626
627 private boolean inInterfaceSubnet(InetAddress address) {
628 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
629 return (intf != null && !intf.getIpAddress().equals(address));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200630 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200631}