blob: 493d58e529e9f6492333dc037c45fe13c8857ed3 [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;
Jonathan Hart1633a402013-08-24 11:38:56 +120063 private Collection<Interface> interfaces = null;
Jonathan Hart2f790d22013-08-15 14:01:24 +120064 private MACAddress routerMacAddress = null;
65 //private SwitchPort bgpdAttachmentPoint = null;
66
Jonathan Hart6261dcd2013-07-22 17:58:35 +120067 private class ArpRequest {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120068 private IArpRequester requester;
69 private boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120070 private long requestTime;
71
Jonathan Hart4dfc3652013-08-02 20:22:36 +120072 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120073 this.requester = requester;
74 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120075 this.requestTime = System.currentTimeMillis();
76 }
77
Jonathan Hart4dfc3652013-08-02 20:22:36 +120078 public ArpRequest(ArpRequest old) {
79 this.requester = old.requester;
80 this.retry = old.retry;
81 this.requestTime = System.currentTimeMillis();
82 }
83
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
Jonathan Hart32e18222013-08-07 22:05:42 +120093 public void dispatchReply(InetAddress ipAddress, byte[] replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120094 log.debug("Dispatching reply for {} to {}", ipAddress.getHostAddress(),
95 requester);
96 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120097 }
98 }
99
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200100 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart2f790d22013-08-15 14:01:24 +1200101 ITopologyService topology){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200102 this.floodlightProvider = floodlightProvider;
103 this.topology = topology;
104
105 arpTable = new HashMap<InetAddress, ArpTableEntry>();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200106
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200107 arpRequests = Multimaps.synchronizedSetMultimap(
108 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200109
Jonathan Hart2f790d22013-08-15 14:01:24 +1200110 mode = Mode.L2_MODE;
111 }
112
Jonathan Hart1633a402013-08-24 11:38:56 +1200113 public void setL3Mode(IPatriciaTrie<Interface> interfacePtrie,
114 Collection<Interface> interfaces, MACAddress routerMacAddress) {
Jonathan Hart2f790d22013-08-15 14:01:24 +1200115 this.interfacePtrie = interfacePtrie;
Jonathan Hart1633a402013-08-24 11:38:56 +1200116 this.interfaces = interfaces;
Jonathan Hart2f790d22013-08-15 14:01:24 +1200117 this.routerMacAddress = routerMacAddress;
Jonathan Hart1633a402013-08-24 11:38:56 +1200118
Jonathan Hart2f790d22013-08-15 14:01:24 +1200119 mode = Mode.L3_MODE;
120 }
121
122 public void startUp() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200123 Timer arpTimer = new Timer();
124 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200125 @Override
126 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200127 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200128 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200129 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200130 }
131
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200132 /*
133 * Function that runs periodically to manage the asynchronous request mechanism.
134 * It basically cleans up old ARP requests if we don't get a response for them.
135 * The caller can designate that a request should be retried indefinitely, and
136 * this task will handle that as well.
137 */
138 private void doPeriodicArpProcessing() {
139 SetMultimap<InetAddress, ArpRequest> retryList
140 = HashMultimap.<InetAddress, ArpRequest>create();
141
142 //Have to synchronize externally on the Multimap while using an iterator,
143 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200144 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200145 log.debug("Current have {} outstanding requests",
146 arpRequests.size());
147
148 Iterator<Map.Entry<InetAddress, ArpRequest>> it
149 = arpRequests.entries().iterator();
150
151 while (it.hasNext()) {
152 Map.Entry<InetAddress, ArpRequest> entry
153 = it.next();
154 ArpRequest request = entry.getValue();
155 if (request.isExpired()) {
156 log.debug("Cleaning expired ARP request for {}",
157 entry.getKey().getHostAddress());
158
159 it.remove();
160
161 if (request.shouldRetry()) {
162 retryList.put(entry.getKey(), request);
163 }
164 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200165 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200166 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200167
168 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
169 : retryList.asMap().entrySet()) {
170
171 InetAddress address = entry.getKey();
172
173 log.debug("Resending ARP request for {}", address.getHostAddress());
174
175 sendArpRequestForAddress(address);
176
177 for (ArpRequest request : entry.getValue()) {
178 arpRequests.put(address, new ArpRequest(request));
179 }
180 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200181 }
182
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200183 @Override
184 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200185 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200186 }
187
188 @Override
189 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200190 return false;
191 }
192
193 @Override
194 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200195 return false;
196 }
197
198 @Override
199 public Command receive(
200 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
201
202 if (msg.getType() != OFType.PACKET_IN){
203 return Command.CONTINUE;
204 }
205
206 OFPacketIn pi = (OFPacketIn) msg;
207
208 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
209 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
210
211 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200212 ARP arp = (ARP) eth.getPayload();
213
214 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200215 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200216 }
217 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200218 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200219 }
220 }
221
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200222 //TODO should we propagate ARP or swallow it?
223 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200224 return Command.CONTINUE;
225 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200226
227 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200228 log.trace("ARP request received for {}",
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200229 bytesToStringAddr(arp.getTargetProtocolAddress()));
Jonathan Hart2f790d22013-08-15 14:01:24 +1200230
231 InetAddress target;
Jonathan Hart6e618212013-08-21 22:28:43 +1200232 InetAddress source;
Jonathan Hart2f790d22013-08-15 14:01:24 +1200233 try {
234 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
Jonathan Hart6e618212013-08-21 22:28:43 +1200235 source = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200236 } catch (UnknownHostException e) {
237 log.debug("Invalid address in ARP request", e);
238 return;
239 }
240
241 if (mode == Mode.L3_MODE) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200242
Jonathan Hart1633a402013-08-24 11:38:56 +1200243 //if (originatedOutsideNetwork(source)) {
244 if (originatedOutsideNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200245 //If the request came from outside our network, we only care if
246 //it was a request for one of our interfaces.
247 if (isInterfaceAddress(target)) {
Jonathan Hart1633a402013-08-24 11:38:56 +1200248 log.trace("ARP request for our interface. Sending reply {} => {}",
249 target.getHostAddress(), routerMacAddress.toString());
Jonathan Hart6e618212013-08-21 22:28:43 +1200250 sendArpReply(arp, sw.getId(), pi.getInPort(), routerMacAddress.toBytes());
251 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200252 return;
253 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200254
255 /*
256 Interface intf = interfacePtrie.match(new Prefix(target.getAddress(), 32));
257 //if (intf != null && target.equals(intf.getIpAddress())) {
258 if (intf != null) {
259 if (target.equals(intf.getIpAddress())) {
260 //ARP request for one of our interfaces, we can reply straight away
261 sendArpReply(arp, sw.getId(), pi.getInPort(), routerMacAddress.toBytes());
262 }
263 // If we didn't enter the above if block, then we found a matching
264 // interface for the target IP but the request wasn't for us.
265 // This is someone else ARPing for a different host in the subnet.
266 // We shouldn't do anything in this case - if we let processing continue
267 // we'll end up erroneously re-broadcasting an ARP for someone else.
268 return;
269 }
270 */
Jonathan Hart2f790d22013-08-15 14:01:24 +1200271 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200272
273 byte[] mac = lookupArpTable(arp.getTargetProtocolAddress());
274
275 if (mac == null){
276 //Mac address is not in our arp table.
277
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200278 //Record where the request came from so we know where to send the reply
Jonathan Hart9ea31212013-08-12 21:40:34 +1200279 arpRequests.put(target, new ArpRequest(
280 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200281
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200282 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200283 //broadcastArpRequestOutEdge(pi.getPacketData(), sw.getId(), pi.getInPort());
284 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200285 }
286 else {
287 //We know the address, so send a reply
Jonathan Hart1633a402013-08-24 11:38:56 +1200288 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
289 bytesToStringAddr(arp.getTargetProtocolAddress()),
290 MACAddress.valueOf(mac).toString(),
291 HexString.toHexString(sw.getId()), pi.getInPort()});
292
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200293 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
294 }
295 }
296
297 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart1633a402013-08-24 11:38:56 +1200298 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200299 bytesToStringAddr(arp.getSenderProtocolAddress()),
300 HexString.toHexString(arp.getSenderHardwareAddress()),
301 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200302
303 updateArpTable(arp);
304
305 //See if anyone's waiting for this ARP reply
306 InetAddress addr;
307 try {
308 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
309 } catch (UnknownHostException e) {
310 return;
311 }
312
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200313 Set<ArpRequest> requests = arpRequests.get(addr);
314
315 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200316 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200317 synchronized (arpRequests) {
318 Iterator<ArpRequest> it = requests.iterator();
319 while (it.hasNext()) {
320 ArpRequest request = it.next();
321 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200322 //request.dispatchReply(addr, arp.getSenderHardwareAddress());
323 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200324 }
325 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200326
327 //Don't hold an ARP lock while dispatching requests
328 for (ArpRequest request : requestsToSend) {
329 request.dispatchReply(addr, arp.getSenderHardwareAddress());
330 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200331 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200332
333 private synchronized byte[] lookupArpTable(byte[] ipAddress){
334 InetAddress addr;
335 try {
336 addr = InetAddress.getByAddress(ipAddress);
337 } catch (UnknownHostException e) {
338 log.warn("Unable to create InetAddress", e);
339 return null;
340 }
341
342 ArpTableEntry arpEntry = arpTable.get(addr);
343
344 if (arpEntry == null){
Jonathan Hart2f790d22013-08-15 14:01:24 +1200345 //log.debug("MAC for {} unknown", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200346 return null;
347 }
348
349 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
350 > ARP_ENTRY_TIMEOUT){
351 //Entry has timed out so we'll remove it and return null
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200352 log.debug("Timing out old ARP entry for {}", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200353 arpTable.remove(addr);
354 return null;
355 }
356
357 return arpEntry.getMacAddress();
358 }
359
360 private synchronized void updateArpTable(ARP arp){
361 InetAddress addr;
362 try {
363 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
364 } catch (UnknownHostException e) {
365 log.warn("Unable to create InetAddress", e);
366 return;
367 }
368
369 ArpTableEntry arpEntry = arpTable.get(addr);
370
371 if (arpEntry != null
372 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
373 arpEntry.setTimeLastSeen(System.currentTimeMillis());
374 }
375 else {
376 arpTable.put(addr,
377 new ArpTableEntry(arp.getSenderHardwareAddress(),
378 System.currentTimeMillis()));
379 }
380 }
381
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200382 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200383 //TODO what should the sender IP address be? Probably not 0.0.0.0
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200384 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
385 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart6e618212013-08-21 22:28:43 +1200386 //byte[] bgpdMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200387 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200388 (byte)0xff, (byte)0xff, (byte)0xff};
389
390 ARP arpRequest = new ARP();
391
392 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
393 .setProtocolType(ARP.PROTO_TYPE_IP)
394 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
395 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
396 .setOpCode(ARP.OP_REQUEST)
Jonathan Harte97fa642013-08-21 13:37:38 +1200397 //.setSenderHardwareAddress(bgpdMac)
398 .setSenderHardwareAddress(routerMacAddress.toBytes())
Jonathan Hart2f790d22013-08-15 14:01:24 +1200399 //.setSenderProtocolAddress(zeroIpv4)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200400 .setTargetHardwareAddress(zeroMac)
401 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200402
403 byte[] senderIPAddress = zeroIpv4;
404 if (mode == Mode.L3_MODE) {
405 Interface intf = interfacePtrie.match(new Prefix(ipAddress.getAddress(), 32));
406 if (intf != null) {
407 senderIPAddress = intf.getIpAddress().getAddress();
408 }
409 }
410
411 arpRequest.setSenderProtocolAddress(senderIPAddress);
412
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200413 Ethernet eth = new Ethernet();
Jonathan Hartb8c21532013-08-21 14:01:38 +1200414 eth.setSourceMACAddress(routerMacAddress.toBytes())
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200415 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200416 .setEtherType(Ethernet.TYPE_ARP)
417 .setPayload(arpRequest);
418
Jonathan Hart2f790d22013-08-15 14:01:24 +1200419 //broadcastArpRequestOutEdge(eth.serialize(), 0, OFPort.OFPP_NONE.getValue());
420 sendArpRequestToSwitches(ipAddress, eth.serialize());
421 }
422
423 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
424 sendArpRequestToSwitches(dstAddress, arpRequest, 0, OFPort.OFPP_NONE.getValue());
425 }
426 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
427 long inSwitch, short inPort) {
428 if (mode == Mode.L2_MODE) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200429 //log.debug("mode is l2");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200430 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
431 }
432 else if (mode == Mode.L3_MODE) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200433 //log.debug("mode is l3");
Jonathan Hart6e618212013-08-21 22:28:43 +1200434 //TODO the case where it should be broadcast out all non-interface
435 //edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200436 Interface intf = interfacePtrie.match(new Prefix(dstAddress.getAddress(), 32));
437 if (intf != null) {
438 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
439 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200440 else {
441 log.debug("No interface found to send ARP request for {}",
442 dstAddress.getHostAddress());
443 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200444 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200445 }
446
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200447 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200448 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
449 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
450 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
451
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200452 if (linkPorts == null){
453 //I think this means the switch isn't known to topology yet.
454 //Maybe it only just joined.
455 continue;
456 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200457
458 OFPacketOut po = new OFPacketOut();
459 po.setInPort(OFPort.OFPP_NONE)
460 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200461 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200462
463 List<OFAction> actions = new ArrayList<OFAction>();
464
465 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200466 if (linkPorts.contains(portNum) ||
467 (sw.getId() == inSwitch && portNum == inPort)){
468 //If this port isn't an edge port or is the ingress port
469 //for the ARP, don't broadcast out it
470 continue;
471 }
472
473 actions.add(new OFActionOutput(portNum));
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200474 //log.debug("Broadcasting out {}/{}", HexString.toHexString(sw.getId()), portNum);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200475 }
476
477 po.setActions(actions);
478 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
479 po.setActionsLength(actionsLength);
480 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200481 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200482
483 List<OFMessage> msgList = new ArrayList<OFMessage>();
484 msgList.add(po);
485
486 try {
487 sw.write(msgList, null);
488 sw.flush();
489 } catch (IOException e) {
490 log.error("Failure writing packet out to switch", e);
491 }
492 }
493 }
494
Jonathan Hart2f790d22013-08-15 14:01:24 +1200495 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200496 log.debug("Sending ARP request out {}/{}", HexString.toHexString(dpid), port);
Jonathan Hart2f790d22013-08-15 14:01:24 +1200497
498 OFPacketOut po = new OFPacketOut();
499 po.setInPort(OFPort.OFPP_NONE)
500 .setBufferId(-1)
501 .setPacketData(arpRequest);
502
503 List<OFAction> actions = new ArrayList<OFAction>();
504 actions.add(new OFActionOutput(port));
505 po.setActions(actions);
506 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
507 po.setActionsLength(actionsLength);
508 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
509 + arpRequest.length);
510
511 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
512
513 if (sw == null) {
514 log.debug("Switch not found when sending ARP request");
515 return;
516 }
517
518 try {
519 sw.write(po, null);
520 sw.flush();
521 } catch (IOException e) {
522 log.error("Failure writing packet out to switch", e);
523 }
524 }
525
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200526 public void sendArpReply(ARP arpRequest, long dpid, short port, byte[] targetMac) {
Jonathan Hart1633a402013-08-24 11:38:56 +1200527 log.trace("Sending reply {} => {} to {}", new Object[] {
528 bytesToStringAddr(arpRequest.getTargetProtocolAddress()),
529 HexString.toHexString(targetMac),
530 bytesToStringAddr(arpRequest.getSenderProtocolAddress())});
531
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200532 ARP arpReply = new ARP();
533 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
534 .setProtocolType(ARP.PROTO_TYPE_IP)
535 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
536 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
537 .setOpCode(ARP.OP_REPLY)
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200538 .setSenderHardwareAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200539 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
540 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
541 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
542
543 Ethernet eth = new Ethernet();
544 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200545 .setSourceMACAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200546 .setEtherType(Ethernet.TYPE_ARP)
547 .setPayload(arpReply);
548
549 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200550 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200551
552 OFPacketOut po = new OFPacketOut();
553 po.setInPort(OFPort.OFPP_NONE)
554 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200555 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200556 .setActions(actions)
557 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
558 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
559 + po.getPacketData().length);
560
561 List<OFMessage> msgList = new ArrayList<OFMessage>();
562 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200563
564 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
565
566 if (sw == null) {
Jonathan Hart1633a402013-08-24 11:38:56 +1200567 log.error("Switch {} not found when sending ARP reply",
568 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200569 return;
570 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200571
572 try {
Jonathan Hart6e618212013-08-21 22:28:43 +1200573 log.debug("Sending ARP reply to {}/{}", HexString.toHexString(sw.getId()), port);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200574 sw.write(msgList, null);
575 sw.flush();
576 } catch (IOException e) {
577 log.warn("Failure writing packet out to switch", e);
578 }
579 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200580
581 //TODO this should be put somewhere more central. I use it in BgpRoute as well.
582 //We need a HexString.toHexString() equivalent.
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200583 private String bytesToStringAddr(byte[] bytes) {
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200584 InetAddress addr;
585 try {
586 addr = InetAddress.getByAddress(bytes);
587 } catch (UnknownHostException e) {
Jonathan Hartc824ad02013-07-03 15:58:45 +1200588 log.warn(" ", e);
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200589 return "";
590 }
591 if (addr == null) return "";
592 else return addr.getHostAddress();
593 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200594
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200595 @Override
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200596 public byte[] getMacAddress(InetAddress ipAddress) {
597 return lookupArpTable(ipAddress.getAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200598 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200599
600 @Override
601 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
602 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200603 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
604 //storeRequester(ipAddress, requester, retry);
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200605
Jonathan Hart6e618212013-08-21 22:28:43 +1200606 //Sanity check to make sure we don't send a request for our own address
607 if (!isInterfaceAddress(ipAddress)) {
608 sendArpRequestForAddress(ipAddress);
609 }
610 }
611
612 /*
613 * TODO These methods might be more suited to some kind of L3 information service
614 * that ProxyArpManager could query, rather than having the information
615 * embedded in ProxyArpManager. There may be many modules that need L3 information.
616 */
617
618 private boolean originatedOutsideNetwork(InetAddress source) {
619 Interface intf = interfacePtrie.match(new Prefix(source.getAddress(), 32));
620 if (intf != null) {
621 if (intf.getIpAddress().equals(source)) {
622 // This request must have been originated by us (the controller)
623 return false;
624 }
625 else {
626 // Source was in one of our interface subnets, but wasn't us.
627 // It must be external.
628 return true;
629 }
630 }
631 else {
632 // Source is not in one of our interface subnets. It's probably a host
633 // in our network as we should only receive ARPs broadcast by external
634 // hosts if they're in the same subnet.
635 return false;
636 }
637 }
638
Jonathan Hart1633a402013-08-24 11:38:56 +1200639 private boolean originatedOutsideNetwork(long inDpid, short inPort) {
640 for (Interface intf : interfaces) {
641 if (intf.getDpid() == inDpid && intf.getPort() == inPort) {
642 return true;
643 }
644 }
645 return false;
646 }
647
Jonathan Hart6e618212013-08-21 22:28:43 +1200648 private boolean isInterfaceAddress(InetAddress address) {
649 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
650 return (intf != null && intf.getIpAddress().equals(address));
651 }
652
653 private boolean inInterfaceSubnet(InetAddress address) {
654 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
655 return (intf != null && !intf.getIpAddress().equals(address));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200656 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200657}