blob: 6e12f285dcef5df11360c111ba0f53be95005a8f [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 Hart5b803bc2013-09-23 14:46:11 +120045 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120046
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
59 private class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120060 private final IArpRequester requester;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120061 private boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120062 private long requestTime;
63
Jonathan Hart4dfc3652013-08-02 20:22:36 +120064 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120065 this.requester = requester;
66 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120067 this.requestTime = System.currentTimeMillis();
68 }
69
Jonathan Hart4dfc3652013-08-02 20:22:36 +120070 public ArpRequest(ArpRequest old) {
71 this.requester = old.requester;
72 this.retry = old.retry;
73 this.requestTime = System.currentTimeMillis();
74 }
75
Jonathan Hart4dfc3652013-08-02 20:22:36 +120076 public boolean isExpired() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +120077 return (System.currentTimeMillis() - requestTime)
78 > IProxyArpService.ARP_REQUEST_TIMEOUT;
79 }
80
Jonathan Hart4dfc3652013-08-02 20:22:36 +120081 public boolean shouldRetry() {
82 return retry;
83 }
84
Jonathan Hart32e18222013-08-07 22:05:42 +120085 public void dispatchReply(InetAddress ipAddress, byte[] replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120086 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120087 }
88 }
89
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120090 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart08ee8522013-09-22 17:34:43 +120091 ITopologyService topology, ILayer3InfoService layer3){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120092 this.floodlightProvider = floodlightProvider;
93 this.topology = topology;
Jonathan Hart08ee8522013-09-22 17:34:43 +120094 this.layer3 = layer3;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120095
96 arpTable = new HashMap<InetAddress, ArpTableEntry>();
Jonathan Hartdf6ec332013-08-04 01:37:14 +120097
Jonathan Hart4dfc3652013-08-02 20:22:36 +120098 arpRequests = Multimaps.synchronizedSetMultimap(
99 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200100 }
101
Jonathan Hart2f790d22013-08-15 14:01:24 +1200102 public void startUp() {
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200103 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200104 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200105 @Override
106 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200107 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200108 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200109 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200110 }
111
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200112 /*
113 * Function that runs periodically to manage the asynchronous request mechanism.
114 * It basically cleans up old ARP requests if we don't get a response for them.
115 * The caller can designate that a request should be retried indefinitely, and
116 * this task will handle that as well.
117 */
118 private void doPeriodicArpProcessing() {
119 SetMultimap<InetAddress, ArpRequest> retryList
120 = HashMultimap.<InetAddress, ArpRequest>create();
121
122 //Have to synchronize externally on the Multimap while using an iterator,
123 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200124 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200125 log.debug("Current have {} outstanding requests",
126 arpRequests.size());
127
128 Iterator<Map.Entry<InetAddress, ArpRequest>> it
129 = arpRequests.entries().iterator();
130
131 while (it.hasNext()) {
132 Map.Entry<InetAddress, ArpRequest> entry
133 = it.next();
134 ArpRequest request = entry.getValue();
135 if (request.isExpired()) {
136 log.debug("Cleaning expired ARP request for {}",
137 entry.getKey().getHostAddress());
138
139 it.remove();
140
141 if (request.shouldRetry()) {
142 retryList.put(entry.getKey(), request);
143 }
144 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200145 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200146 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200147
148 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
149 : retryList.asMap().entrySet()) {
150
151 InetAddress address = entry.getKey();
152
153 log.debug("Resending ARP request for {}", address.getHostAddress());
154
155 sendArpRequestForAddress(address);
156
157 for (ArpRequest request : entry.getValue()) {
158 arpRequests.put(address, new ArpRequest(request));
159 }
160 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200161 }
162
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200163 @Override
164 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200165 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200166 }
167
168 @Override
169 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200170 return false;
171 }
172
173 @Override
174 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200175 return false;
176 }
177
178 @Override
179 public Command receive(
180 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
181
182 if (msg.getType() != OFType.PACKET_IN){
183 return Command.CONTINUE;
184 }
185
186 OFPacketIn pi = (OFPacketIn) msg;
187
188 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
189 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
190
191 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200192 ARP arp = (ARP) eth.getPayload();
193
194 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200195 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200196 }
197 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200198 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200199 }
200 }
201
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200202 //TODO should we propagate ARP or swallow it?
203 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200204 return Command.CONTINUE;
205 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200206
207 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200208 if (log.isTraceEnabled()) {
209 log.trace("ARP request received for {}",
210 inetAddressToString(arp.getTargetProtocolAddress()));
211 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200212
213 InetAddress target;
214 try {
215 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
216 } catch (UnknownHostException e) {
217 log.debug("Invalid address in ARP request", e);
218 return;
219 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200220
221 if (layer3.fromExternalNetwork(sw.getId(), pi.getInPort())) {
222 //If the request came from outside our network, we only care if
223 //it was a request for one of our interfaces.
224 if (layer3.isInterfaceAddress(target)) {
225 log.trace("ARP request for our interface. Sending reply {} => {}",
226 target.getHostAddress(), layer3.getRouterMacAddress());
227
228 sendArpReply(arp, sw.getId(), pi.getInPort(),
229 layer3.getRouterMacAddress().toBytes());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200230 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200231
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200232 return;
233 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200234
235 byte[] mac = lookupArpTable(arp.getTargetProtocolAddress());
236
237 if (mac == null){
238 //Mac address is not in our arp table.
239
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200240 //Record where the request came from so we know where to send the reply
Jonathan Hart9ea31212013-08-12 21:40:34 +1200241 arpRequests.put(target, new ArpRequest(
242 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200243
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200244 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200245 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200246 }
247 else {
248 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200249 if (log.isTraceEnabled()) {
250 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
251 inetAddressToString(arp.getTargetProtocolAddress()),
252 MACAddress.valueOf(mac).toString(),
253 HexString.toHexString(sw.getId()), pi.getInPort()});
254 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200255
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200256 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
257 }
258 }
259
260 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200261 if (log.isTraceEnabled()) {
262 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
263 inetAddressToString(arp.getSenderProtocolAddress()),
264 HexString.toHexString(arp.getSenderHardwareAddress()),
265 HexString.toHexString(sw.getId()), pi.getInPort()});
266 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200267
268 updateArpTable(arp);
269
270 //See if anyone's waiting for this ARP reply
271 InetAddress addr;
272 try {
273 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
274 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200275 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200276 return;
277 }
278
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200279 Set<ArpRequest> requests = arpRequests.get(addr);
280
281 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200282 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200283 synchronized (arpRequests) {
284 Iterator<ArpRequest> it = requests.iterator();
285 while (it.hasNext()) {
286 ArpRequest request = it.next();
287 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200288 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200289 }
290 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200291
292 //Don't hold an ARP lock while dispatching requests
293 for (ArpRequest request : requestsToSend) {
294 request.dispatchReply(addr, arp.getSenderHardwareAddress());
295 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200296 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200297
298 private synchronized byte[] lookupArpTable(byte[] ipAddress){
299 InetAddress addr;
300 try {
301 addr = InetAddress.getByAddress(ipAddress);
302 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200303 log.debug("Unable to create InetAddress", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200304 return null;
305 }
306
307 ArpTableEntry arpEntry = arpTable.get(addr);
308
309 if (arpEntry == null){
310 return null;
311 }
312
313 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
314 > ARP_ENTRY_TIMEOUT){
315 //Entry has timed out so we'll remove it and return null
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200316 log.trace("Removing expired ARP entry for {}",
317 inetAddressToString(ipAddress));
318
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200319 arpTable.remove(addr);
320 return null;
321 }
322
323 return arpEntry.getMacAddress();
324 }
325
326 private synchronized void updateArpTable(ARP arp){
327 InetAddress addr;
328 try {
329 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
330 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200331 log.debug("Unable to create InetAddress", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200332 return;
333 }
334
335 ArpTableEntry arpEntry = arpTable.get(addr);
336
337 if (arpEntry != null
338 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
339 arpEntry.setTimeLastSeen(System.currentTimeMillis());
340 }
341 else {
342 arpTable.put(addr,
343 new ArpTableEntry(arp.getSenderHardwareAddress(),
344 System.currentTimeMillis()));
345 }
346 }
347
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200348 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200349 //TODO what should the sender IP address and MAC address be if no
350 //IP addresses are configured? Will there ever be a need to send
351 //ARP requests from the controller in that case?
352 //All-zero MAC address doesn't seem to work - hosts don't respond to it
353
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200354 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
355 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200356 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200357 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200358 (byte)0xff, (byte)0xff, (byte)0xff};
359
360 ARP arpRequest = new ARP();
361
362 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
363 .setProtocolType(ARP.PROTO_TYPE_IP)
364 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200365 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200366 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200367 .setTargetHardwareAddress(zeroMac)
368 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200369
Jonathan Hart08ee8522013-09-22 17:34:43 +1200370 MACAddress routerMacAddress = layer3.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200371 //TODO hack for now as it's unclear what the MAC address should be
372 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200373 if (routerMacAddress != null) {
374 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200375 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200376 arpRequest.setSenderHardwareAddress(senderMacAddress);
377
378 byte[] senderIPAddress = zeroIpv4;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200379 Interface intf = layer3.getOutgoingInterface(ipAddress);
380 if (intf != null) {
381 senderIPAddress = intf.getIpAddress().getAddress();
382 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200383
384 arpRequest.setSenderProtocolAddress(senderIPAddress);
385
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200386 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200387 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200388 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200389 .setEtherType(Ethernet.TYPE_ARP)
390 .setPayload(arpRequest);
391
Jonathan Hart2f790d22013-08-15 14:01:24 +1200392 sendArpRequestToSwitches(ipAddress, eth.serialize());
393 }
394
395 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200396 sendArpRequestToSwitches(dstAddress, arpRequest,
397 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200398 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200399
Jonathan Hart2f790d22013-08-15 14:01:24 +1200400 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
401 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200402
Jonathan Hart08ee8522013-09-22 17:34:43 +1200403 if (layer3.hasLayer3Configuration()) {
404 Interface intf = layer3.getOutgoingInterface(dstAddress);
405 if (intf != null) {
406 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
407 }
408 else {
409 //TODO here it should be broadcast out all non-interface edge ports.
410 //I think we can assume that if it's not a request for an external
411 //network, it's an ARP for a host in our own network. So we want to
412 //send it out all edge ports that don't have an interface configured
413 //to ensure it reaches all hosts in our network.
414 log.debug("No interface found to send ARP request for {}",
415 dstAddress.getHostAddress());
416 }
417 }
418 else {
419 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
420 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200421 }
422
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200423 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200424 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
425 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
426 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
427
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200428 if (linkPorts == null){
429 //I think this means the switch isn't known to topology yet.
430 //Maybe it only just joined.
431 continue;
432 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200433
434 OFPacketOut po = new OFPacketOut();
435 po.setInPort(OFPort.OFPP_NONE)
436 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200437 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200438
439 List<OFAction> actions = new ArrayList<OFAction>();
440
441 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200442 if (linkPorts.contains(portNum) ||
443 (sw.getId() == inSwitch && portNum == inPort)){
444 //If this port isn't an edge port or is the ingress port
445 //for the ARP, don't broadcast out it
446 continue;
447 }
448
449 actions.add(new OFActionOutput(portNum));
450 }
451
452 po.setActions(actions);
453 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
454 po.setActionsLength(actionsLength);
455 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200456 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200457
458 List<OFMessage> msgList = new ArrayList<OFMessage>();
459 msgList.add(po);
460
461 try {
462 sw.write(msgList, null);
463 sw.flush();
464 } catch (IOException e) {
465 log.error("Failure writing packet out to switch", e);
466 }
467 }
468 }
469
Jonathan Hart2f790d22013-08-15 14:01:24 +1200470 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200471 if (log.isTraceEnabled()) {
472 log.trace("Sending ARP request out {}/{}",
473 HexString.toHexString(dpid), port);
474 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200475
476 OFPacketOut po = new OFPacketOut();
477 po.setInPort(OFPort.OFPP_NONE)
478 .setBufferId(-1)
479 .setPacketData(arpRequest);
480
481 List<OFAction> actions = new ArrayList<OFAction>();
482 actions.add(new OFActionOutput(port));
483 po.setActions(actions);
484 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
485 po.setActionsLength(actionsLength);
486 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
487 + arpRequest.length);
488
489 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
490
491 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200492 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200493 return;
494 }
495
496 try {
497 sw.write(po, null);
498 sw.flush();
499 } catch (IOException e) {
500 log.error("Failure writing packet out to switch", e);
501 }
502 }
503
Jonathan Hart08ee8522013-09-22 17:34:43 +1200504 private String inetAddressToString(byte[] bytes) {
505 try {
506 return InetAddress.getByAddress(bytes).getHostAddress();
507 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200508 log.debug("Invalid IP address", e);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200509 return "";
510 }
511 }
512
513 /*
514 * IProxyArpService methods
515 */
516
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200517 public void sendArpReply(ARP arpRequest, long dpid, short port, byte[] targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200518 if (log.isTraceEnabled()) {
519 log.trace("Sending reply {} => {} to {}", new Object[] {
520 inetAddressToString(arpRequest.getTargetProtocolAddress()),
521 HexString.toHexString(targetMac),
522 inetAddressToString(arpRequest.getSenderProtocolAddress())});
523 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200524
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200525 ARP arpReply = new ARP();
526 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
527 .setProtocolType(ARP.PROTO_TYPE_IP)
528 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200529 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200530 .setOpCode(ARP.OP_REPLY)
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200531 .setSenderHardwareAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200532 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
533 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
534 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
535
536 Ethernet eth = new Ethernet();
537 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200538 .setSourceMACAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200539 .setEtherType(Ethernet.TYPE_ARP)
540 .setPayload(arpReply);
541
542 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200543 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200544
545 OFPacketOut po = new OFPacketOut();
546 po.setInPort(OFPort.OFPP_NONE)
547 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200548 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200549 .setActions(actions)
550 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
551 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
552 + po.getPacketData().length);
553
554 List<OFMessage> msgList = new ArrayList<OFMessage>();
555 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200556
557 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
558
559 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200560 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200561 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200562 return;
563 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200564
565 try {
566 sw.write(msgList, null);
567 sw.flush();
568 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200569 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200570 }
571 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200572
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200573 @Override
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200574 public byte[] getMacAddress(InetAddress ipAddress) {
575 return lookupArpTable(ipAddress.getAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200576 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200577
578 @Override
579 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
580 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200581 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200582
Jonathan Hart6e618212013-08-21 22:28:43 +1200583 //Sanity check to make sure we don't send a request for our own address
Jonathan Hart08ee8522013-09-22 17:34:43 +1200584 if (!layer3.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200585 sendArpRequestForAddress(ipAddress);
586 }
587 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200588}