blob: 8eb64a27035e0f27b522f4c3321e820a5f7f6e93 [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;
Jonathan Hart08ee8522013-09-22 17:34:43 +120022import net.floodlightcontroller.packet.IPv4;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120023import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120024import net.floodlightcontroller.util.MACAddress;
Jonathan Hart08ee8522013-09-22 17:34:43 +120025import net.onrc.onos.ofcontroller.bgproute.ILayer3InfoService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120026import net.onrc.onos.ofcontroller.bgproute.Interface;
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 Harte751e1c2013-08-23 00:48:47 +120043//TODO REST API to inspect ARP table
Jonathan Hart6261dcd2013-07-22 17:58:35 +120044public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120045 private static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
46
Jonathan Hart6261dcd2013-07-22 17:58:35 +120047 private final long ARP_ENTRY_TIMEOUT = 600000; //ms (== 10 mins)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120048
Jonathan Hartdf6ec332013-08-04 01:37:14 +120049 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hart6261dcd2013-07-22 17:58:35 +120050
Jonathan Hart08ee8522013-09-22 17:34:43 +120051 private IFloodlightProviderService floodlightProvider;
52 private ITopologyService topology;
53 private ILayer3InfoService layer3;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120054
Jonathan Hart08ee8522013-09-22 17:34:43 +120055 private Map<InetAddress, ArpTableEntry> arpTable;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120056
Jonathan Hart08ee8522013-09-22 17:34:43 +120057 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120058
Jonathan Hart08ee8522013-09-22 17:34:43 +120059 //public enum Mode {L2_MODE, L3_MODE}
Jonathan Hart2f790d22013-08-15 14:01:24 +120060
Jonathan Hart08ee8522013-09-22 17:34:43 +120061 //private Mode mode;
62 //private IPatriciaTrie<Interface> interfacePtrie = null;
63 //private Collection<Interface> interfaces = null;
64 //private MACAddress routerMacAddress = null;
Jonathan Hart2f790d22013-08-15 14:01:24 +120065 //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 Hart08ee8522013-09-22 17:34:43 +1200101 ITopologyService topology, ILayer3InfoService layer3){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200102 this.floodlightProvider = floodlightProvider;
103 this.topology = topology;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200104 this.layer3 = layer3;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200105
106 arpTable = new HashMap<InetAddress, ArpTableEntry>();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200107
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200108 arpRequests = Multimaps.synchronizedSetMultimap(
109 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200110
Jonathan Hart08ee8522013-09-22 17:34:43 +1200111 //mode = Mode.L2_MODE;
Jonathan Hart2f790d22013-08-15 14:01:24 +1200112 }
113
Jonathan Hart08ee8522013-09-22 17:34:43 +1200114 /*
Jonathan Hart1633a402013-08-24 11:38:56 +1200115 public void setL3Mode(IPatriciaTrie<Interface> interfacePtrie,
116 Collection<Interface> interfaces, MACAddress routerMacAddress) {
Jonathan Hart2f790d22013-08-15 14:01:24 +1200117 this.interfacePtrie = interfacePtrie;
Jonathan Hart1633a402013-08-24 11:38:56 +1200118 this.interfaces = interfaces;
Jonathan Hart2f790d22013-08-15 14:01:24 +1200119 this.routerMacAddress = routerMacAddress;
Jonathan Hart1633a402013-08-24 11:38:56 +1200120
Jonathan Hart2f790d22013-08-15 14:01:24 +1200121 mode = Mode.L3_MODE;
122 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200123 */
Jonathan Hart2f790d22013-08-15 14:01:24 +1200124
125 public void startUp() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200126 Timer arpTimer = new Timer();
127 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200128 @Override
129 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200130 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200131 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200132 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200133 }
134
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200135 /*
136 * Function that runs periodically to manage the asynchronous request mechanism.
137 * It basically cleans up old ARP requests if we don't get a response for them.
138 * The caller can designate that a request should be retried indefinitely, and
139 * this task will handle that as well.
140 */
141 private void doPeriodicArpProcessing() {
142 SetMultimap<InetAddress, ArpRequest> retryList
143 = HashMultimap.<InetAddress, ArpRequest>create();
144
145 //Have to synchronize externally on the Multimap while using an iterator,
146 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200147 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200148 log.debug("Current have {} outstanding requests",
149 arpRequests.size());
150
151 Iterator<Map.Entry<InetAddress, ArpRequest>> it
152 = arpRequests.entries().iterator();
153
154 while (it.hasNext()) {
155 Map.Entry<InetAddress, ArpRequest> entry
156 = it.next();
157 ArpRequest request = entry.getValue();
158 if (request.isExpired()) {
159 log.debug("Cleaning expired ARP request for {}",
160 entry.getKey().getHostAddress());
161
162 it.remove();
163
164 if (request.shouldRetry()) {
165 retryList.put(entry.getKey(), request);
166 }
167 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200168 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200169 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200170
171 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
172 : retryList.asMap().entrySet()) {
173
174 InetAddress address = entry.getKey();
175
176 log.debug("Resending ARP request for {}", address.getHostAddress());
177
178 sendArpRequestForAddress(address);
179
180 for (ArpRequest request : entry.getValue()) {
181 arpRequests.put(address, new ArpRequest(request));
182 }
183 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200184 }
185
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200186 @Override
187 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200188 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200189 }
190
191 @Override
192 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200193 return false;
194 }
195
196 @Override
197 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200198 return false;
199 }
200
201 @Override
202 public Command receive(
203 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
204
205 if (msg.getType() != OFType.PACKET_IN){
206 return Command.CONTINUE;
207 }
208
209 OFPacketIn pi = (OFPacketIn) msg;
210
211 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
212 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
213
214 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200215 ARP arp = (ARP) eth.getPayload();
216
217 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200218 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200219 }
220 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200221 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200222 }
223 }
224
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200225 //TODO should we propagate ARP or swallow it?
226 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200227 return Command.CONTINUE;
228 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200229
230 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200231 if (log.isTraceEnabled()) {
232 log.trace("ARP request received for {}",
233 inetAddressToString(arp.getTargetProtocolAddress()));
234 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200235
236 InetAddress target;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200237 //InetAddress source;
Jonathan Hart2f790d22013-08-15 14:01:24 +1200238 try {
239 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
Jonathan Hart08ee8522013-09-22 17:34:43 +1200240 //source = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200241 } catch (UnknownHostException e) {
242 log.debug("Invalid address in ARP request", e);
243 return;
244 }
245
Jonathan Hart08ee8522013-09-22 17:34:43 +1200246 //if (mode == Mode.L3_MODE) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200247
Jonathan Hart1633a402013-08-24 11:38:56 +1200248 //if (originatedOutsideNetwork(source)) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200249 //if (originatedOutsideNetwork(sw.getId(), pi.getInPort())) {
250 if (layer3.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200251 //If the request came from outside our network, we only care if
252 //it was a request for one of our interfaces.
Jonathan Hart08ee8522013-09-22 17:34:43 +1200253 //if (isInterfaceAddress(target)) {
254 if (layer3.isInterfaceAddress(target)) {
Jonathan Hart1633a402013-08-24 11:38:56 +1200255 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Hart08ee8522013-09-22 17:34:43 +1200256 target.getHostAddress(), layer3.getRouterMacAddress().toString());
257 sendArpReply(arp, sw.getId(), pi.getInPort(), layer3.getRouterMacAddress().toBytes());
Jonathan Hart6e618212013-08-21 22:28:43 +1200258 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200259 return;
260 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200261
262 /*
263 Interface intf = interfacePtrie.match(new Prefix(target.getAddress(), 32));
264 //if (intf != null && target.equals(intf.getIpAddress())) {
265 if (intf != null) {
266 if (target.equals(intf.getIpAddress())) {
267 //ARP request for one of our interfaces, we can reply straight away
268 sendArpReply(arp, sw.getId(), pi.getInPort(), routerMacAddress.toBytes());
269 }
270 // If we didn't enter the above if block, then we found a matching
271 // interface for the target IP but the request wasn't for us.
272 // This is someone else ARPing for a different host in the subnet.
273 // We shouldn't do anything in this case - if we let processing continue
274 // we'll end up erroneously re-broadcasting an ARP for someone else.
275 return;
276 }
277 */
Jonathan Hart08ee8522013-09-22 17:34:43 +1200278 //}
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200279
280 byte[] mac = lookupArpTable(arp.getTargetProtocolAddress());
281
282 if (mac == null){
283 //Mac address is not in our arp table.
284
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200285 //Record where the request came from so we know where to send the reply
Jonathan Hart9ea31212013-08-12 21:40:34 +1200286 arpRequests.put(target, new ArpRequest(
287 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200288
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200289 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200290 //broadcastArpRequestOutEdge(pi.getPacketData(), sw.getId(), pi.getInPort());
291 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200292 }
293 else {
294 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200295 if (log.isTraceEnabled()) {
296 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
297 inetAddressToString(arp.getTargetProtocolAddress()),
298 MACAddress.valueOf(mac).toString(),
299 HexString.toHexString(sw.getId()), pi.getInPort()});
300 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200301
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200302 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
303 }
304 }
305
306 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200307 if (log.isTraceEnabled()) {
308 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
309 inetAddressToString(arp.getSenderProtocolAddress()),
310 HexString.toHexString(arp.getSenderHardwareAddress()),
311 HexString.toHexString(sw.getId()), pi.getInPort()});
312 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200313
314 updateArpTable(arp);
315
316 //See if anyone's waiting for this ARP reply
317 InetAddress addr;
318 try {
319 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
320 } catch (UnknownHostException e) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200321 log.debug("Invalid address in ARP request", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200322 return;
323 }
324
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200325 Set<ArpRequest> requests = arpRequests.get(addr);
326
327 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200328 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200329 synchronized (arpRequests) {
330 Iterator<ArpRequest> it = requests.iterator();
331 while (it.hasNext()) {
332 ArpRequest request = it.next();
333 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200334 //request.dispatchReply(addr, arp.getSenderHardwareAddress());
335 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200336 }
337 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200338
339 //Don't hold an ARP lock while dispatching requests
340 for (ArpRequest request : requestsToSend) {
341 request.dispatchReply(addr, arp.getSenderHardwareAddress());
342 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200343 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200344
345 private synchronized byte[] lookupArpTable(byte[] ipAddress){
346 InetAddress addr;
347 try {
348 addr = InetAddress.getByAddress(ipAddress);
349 } catch (UnknownHostException e) {
350 log.warn("Unable to create InetAddress", e);
351 return null;
352 }
353
354 ArpTableEntry arpEntry = arpTable.get(addr);
355
356 if (arpEntry == null){
Jonathan Hart2f790d22013-08-15 14:01:24 +1200357 //log.debug("MAC for {} unknown", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200358 return null;
359 }
360
361 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
362 > ARP_ENTRY_TIMEOUT){
363 //Entry has timed out so we'll remove it and return null
Jonathan Hart08ee8522013-09-22 17:34:43 +1200364 log.debug("Timing out old ARP entry for {}", inetAddressToString(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200365 arpTable.remove(addr);
366 return null;
367 }
368
369 return arpEntry.getMacAddress();
370 }
371
372 private synchronized void updateArpTable(ARP arp){
373 InetAddress addr;
374 try {
375 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
376 } catch (UnknownHostException e) {
377 log.warn("Unable to create InetAddress", e);
378 return;
379 }
380
381 ArpTableEntry arpEntry = arpTable.get(addr);
382
383 if (arpEntry != null
384 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
385 arpEntry.setTimeLastSeen(System.currentTimeMillis());
386 }
387 else {
388 arpTable.put(addr,
389 new ArpTableEntry(arp.getSenderHardwareAddress(),
390 System.currentTimeMillis()));
391 }
392 }
393
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200394 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200395 //TODO what should the sender IP address and MAC address be if no
396 //IP addresses are configured? Will there ever be a need to send
397 //ARP requests from the controller in that case?
398 //All-zero MAC address doesn't seem to work - hosts don't respond to it
399
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200400 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
401 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200402 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200403 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200404 (byte)0xff, (byte)0xff, (byte)0xff};
405
406 ARP arpRequest = new ARP();
407
408 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
409 .setProtocolType(ARP.PROTO_TYPE_IP)
410 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200411 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200412 .setOpCode(ARP.OP_REQUEST)
Jonathan Harte97fa642013-08-21 13:37:38 +1200413 //.setSenderHardwareAddress(bgpdMac)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200414 //.setSenderHardwareAddress(routerMacAddress.toBytes())
Jonathan Hart2f790d22013-08-15 14:01:24 +1200415 //.setSenderProtocolAddress(zeroIpv4)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200416 .setTargetHardwareAddress(zeroMac)
417 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200418
Jonathan Hart08ee8522013-09-22 17:34:43 +1200419 MACAddress routerMacAddress = layer3.getRouterMacAddress();
420 byte[] senderMacAddress = null;
421 if (routerMacAddress != null) {
422 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200423 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200424 else {
425 //TODO hack for now as it's unclear what the MAC address should be
426 senderMacAddress = genericNonZeroMac;
427 }
428 arpRequest.setSenderHardwareAddress(senderMacAddress);
429
430 byte[] senderIPAddress = zeroIpv4;
431 //if (mode == Mode.L3_MODE) {
432 //Interface intf = interfacePtrie.match(new Prefix(ipAddress.getAddress(), 32));
433 Interface intf = layer3.getOutgoingInterface(ipAddress);
434 if (intf != null) {
435 senderIPAddress = intf.getIpAddress().getAddress();
436 }
437 //}
Jonathan Hart2f790d22013-08-15 14:01:24 +1200438
439 arpRequest.setSenderProtocolAddress(senderIPAddress);
440
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200441 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200442 //eth.setSourceMACAddress(routerMacAddress.toBytes())
443 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200444 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200445 .setEtherType(Ethernet.TYPE_ARP)
446 .setPayload(arpRequest);
447
Jonathan Hart2f790d22013-08-15 14:01:24 +1200448 //broadcastArpRequestOutEdge(eth.serialize(), 0, OFPort.OFPP_NONE.getValue());
449 sendArpRequestToSwitches(ipAddress, eth.serialize());
450 }
451
452 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
453 sendArpRequestToSwitches(dstAddress, arpRequest, 0, OFPort.OFPP_NONE.getValue());
454 }
455 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
456 long inSwitch, short inPort) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200457 /*
Jonathan Hart2f790d22013-08-15 14:01:24 +1200458 if (mode == Mode.L2_MODE) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200459 //log.debug("mode is l2");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200460 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
461 }
462 else if (mode == Mode.L3_MODE) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200463 //log.debug("mode is l3");
Jonathan Hart6e618212013-08-21 22:28:43 +1200464 //TODO the case where it should be broadcast out all non-interface
465 //edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200466 Interface intf = interfacePtrie.match(new Prefix(dstAddress.getAddress(), 32));
467 if (intf != null) {
468 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
469 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200470 else {
471 log.debug("No interface found to send ARP request for {}",
472 dstAddress.getHostAddress());
473 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200474 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200475 */
476 if (layer3.hasLayer3Configuration()) {
477 Interface intf = layer3.getOutgoingInterface(dstAddress);
478 if (intf != null) {
479 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
480 }
481 else {
482 //TODO here it should be broadcast out all non-interface edge ports.
483 //I think we can assume that if it's not a request for an external
484 //network, it's an ARP for a host in our own network. So we want to
485 //send it out all edge ports that don't have an interface configured
486 //to ensure it reaches all hosts in our network.
487 log.debug("No interface found to send ARP request for {}",
488 dstAddress.getHostAddress());
489 }
490 }
491 else {
492 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
493 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200494 }
495
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200496 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200497 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
498 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
499 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
500
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200501 if (linkPorts == null){
502 //I think this means the switch isn't known to topology yet.
503 //Maybe it only just joined.
504 continue;
505 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200506
507 OFPacketOut po = new OFPacketOut();
508 po.setInPort(OFPort.OFPP_NONE)
509 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200510 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200511
512 List<OFAction> actions = new ArrayList<OFAction>();
513
514 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200515 if (linkPorts.contains(portNum) ||
516 (sw.getId() == inSwitch && portNum == inPort)){
517 //If this port isn't an edge port or is the ingress port
518 //for the ARP, don't broadcast out it
519 continue;
520 }
521
522 actions.add(new OFActionOutput(portNum));
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200523 //log.debug("Broadcasting out {}/{}", HexString.toHexString(sw.getId()), portNum);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200524 }
525
526 po.setActions(actions);
527 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
528 po.setActionsLength(actionsLength);
529 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200530 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200531
532 List<OFMessage> msgList = new ArrayList<OFMessage>();
533 msgList.add(po);
534
535 try {
536 sw.write(msgList, null);
537 sw.flush();
538 } catch (IOException e) {
539 log.error("Failure writing packet out to switch", e);
540 }
541 }
542 }
543
Jonathan Hart2f790d22013-08-15 14:01:24 +1200544 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200545 log.debug("Sending ARP request out {}/{}", HexString.toHexString(dpid), port);
Jonathan Hart2f790d22013-08-15 14:01:24 +1200546
547 OFPacketOut po = new OFPacketOut();
548 po.setInPort(OFPort.OFPP_NONE)
549 .setBufferId(-1)
550 .setPacketData(arpRequest);
551
552 List<OFAction> actions = new ArrayList<OFAction>();
553 actions.add(new OFActionOutput(port));
554 po.setActions(actions);
555 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
556 po.setActionsLength(actionsLength);
557 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
558 + arpRequest.length);
559
560 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
561
562 if (sw == null) {
563 log.debug("Switch not found when sending ARP request");
564 return;
565 }
566
567 try {
568 sw.write(po, null);
569 sw.flush();
570 } catch (IOException e) {
571 log.error("Failure writing packet out to switch", e);
572 }
573 }
574
Jonathan Hart08ee8522013-09-22 17:34:43 +1200575 private String inetAddressToString(byte[] bytes) {
576 try {
577 return InetAddress.getByAddress(bytes).getHostAddress();
578 } catch (UnknownHostException e) {
579 log.warn("Invalid IP address", e);
580 return "";
581 }
582 }
583
584 /*
585 * IProxyArpService methods
586 */
587
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200588 public void sendArpReply(ARP arpRequest, long dpid, short port, byte[] targetMac) {
Jonathan Hart1633a402013-08-24 11:38:56 +1200589 log.trace("Sending reply {} => {} to {}", new Object[] {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200590 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hart1633a402013-08-24 11:38:56 +1200591 HexString.toHexString(targetMac),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200592 inetAddressToString(arpRequest.getSenderProtocolAddress())});
Jonathan Hart1633a402013-08-24 11:38:56 +1200593
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200594 ARP arpReply = new ARP();
595 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
596 .setProtocolType(ARP.PROTO_TYPE_IP)
597 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200598 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200599 .setOpCode(ARP.OP_REPLY)
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200600 .setSenderHardwareAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200601 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
602 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
603 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
604
605 Ethernet eth = new Ethernet();
606 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200607 .setSourceMACAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200608 .setEtherType(Ethernet.TYPE_ARP)
609 .setPayload(arpReply);
610
611 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200612 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200613
614 OFPacketOut po = new OFPacketOut();
615 po.setInPort(OFPort.OFPP_NONE)
616 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200617 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200618 .setActions(actions)
619 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
620 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
621 + po.getPacketData().length);
622
623 List<OFMessage> msgList = new ArrayList<OFMessage>();
624 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200625
626 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
627
628 if (sw == null) {
Jonathan Hart1633a402013-08-24 11:38:56 +1200629 log.error("Switch {} not found when sending ARP reply",
630 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200631 return;
632 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200633
634 try {
Jonathan Hart6e618212013-08-21 22:28:43 +1200635 log.debug("Sending ARP reply to {}/{}", HexString.toHexString(sw.getId()), port);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200636 sw.write(msgList, null);
637 sw.flush();
638 } catch (IOException e) {
639 log.warn("Failure writing packet out to switch", e);
640 }
641 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200642
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200643 @Override
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200644 public byte[] getMacAddress(InetAddress ipAddress) {
645 return lookupArpTable(ipAddress.getAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200646 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200647
648 @Override
649 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
650 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200651 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200652
Jonathan Hart6e618212013-08-21 22:28:43 +1200653 //Sanity check to make sure we don't send a request for our own address
Jonathan Hart08ee8522013-09-22 17:34:43 +1200654 //if (!isInterfaceAddress(ipAddress)) {
655 if (!layer3.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200656 sendArpRequestForAddress(ipAddress);
657 }
658 }
659
660 /*
661 * TODO These methods might be more suited to some kind of L3 information service
662 * that ProxyArpManager could query, rather than having the information
663 * embedded in ProxyArpManager. There may be many modules that need L3 information.
664 */
Jonathan Hart08ee8522013-09-22 17:34:43 +1200665 /*
Jonathan Hart6e618212013-08-21 22:28:43 +1200666 private boolean originatedOutsideNetwork(InetAddress source) {
667 Interface intf = interfacePtrie.match(new Prefix(source.getAddress(), 32));
668 if (intf != null) {
669 if (intf.getIpAddress().equals(source)) {
670 // This request must have been originated by us (the controller)
671 return false;
672 }
673 else {
674 // Source was in one of our interface subnets, but wasn't us.
675 // It must be external.
676 return true;
677 }
678 }
679 else {
680 // Source is not in one of our interface subnets. It's probably a host
681 // in our network as we should only receive ARPs broadcast by external
682 // hosts if they're in the same subnet.
683 return false;
684 }
685 }
686
Jonathan Hart1633a402013-08-24 11:38:56 +1200687 private boolean originatedOutsideNetwork(long inDpid, short inPort) {
688 for (Interface intf : interfaces) {
689 if (intf.getDpid() == inDpid && intf.getPort() == inPort) {
690 return true;
691 }
692 }
693 return false;
694 }
695
Jonathan Hart6e618212013-08-21 22:28:43 +1200696 private boolean isInterfaceAddress(InetAddress address) {
697 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
698 return (intf != null && intf.getIpAddress().equals(address));
699 }
700
701 private boolean inInterfaceSubnet(InetAddress address) {
702 Interface intf = interfacePtrie.match(new Prefix(address.getAddress(), 32));
703 return (intf != null && !intf.getIpAddress().equals(address));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200704 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200705 */
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200706}