blob: b6a95915972d40ea3979f36ecb47ef822b565603 [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;
Jonathan Hart6261dcd2013-07-22 17:58:35 +12008import java.util.Iterator;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +12009import java.util.List;
10import java.util.Map;
11import java.util.Set;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120012import java.util.Timer;
13import java.util.TimerTask;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120014
15import net.floodlightcontroller.core.FloodlightContext;
16import net.floodlightcontroller.core.IFloodlightProviderService;
17import net.floodlightcontroller.core.IOFMessageListener;
18import net.floodlightcontroller.core.IOFSwitch;
19import net.floodlightcontroller.packet.ARP;
20import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart08ee8522013-09-22 17:34:43 +120021import net.floodlightcontroller.packet.IPv4;
Jonathan Hart5afde492013-10-01 12:30:53 +130022import net.floodlightcontroller.restserver.IRestApiService;
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 Hart6261dcd2013-07-22 17:58:35 +120043public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120044 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120045
Jonathan Hartdf6ec332013-08-04 01:37:14 +120046 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130047
48 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120049
Jonathan Hartabad6a52013-09-30 18:17:21 +130050 private final IFloodlightProviderService floodlightProvider;
51 private final ITopologyService topology;
52 private final ILayer3InfoService layer3;
Jonathan Hart5afde492013-10-01 12:30:53 +130053 private final IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120054
Jonathan Hartabad6a52013-09-30 18:17:21 +130055 private final ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120056
Jonathan Hartabad6a52013-09-30 18:17:21 +130057 private final SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120058
Jonathan Hartabad6a52013-09-30 18:17:21 +130059 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120060 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130061 private final 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 Hartda4d0e12013-09-30 21:00:20 +130077 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120078 }
79
Jonathan Hart4dfc3652013-08-02 20:22:36 +120080 public boolean shouldRetry() {
81 return retry;
82 }
83
Jonathan Hartabad6a52013-09-30 18:17:21 +130084 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120085 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120086 }
87 }
88
Jonathan Hartda4d0e12013-09-30 21:00:20 +130089 private class HostArpRequester implements IArpRequester {
90 private final ARP arpRequest;
91 private final long dpid;
92 private final short port;
93
94 public HostArpRequester(ARP arpRequest, long dpid, short port) {
95 this.arpRequest = arpRequest;
96 this.dpid = dpid;
97 this.port = port;
98 }
99
100 @Override
101 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
102 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
103 }
104 }
105
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200106 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart5afde492013-10-01 12:30:53 +1300107 ITopologyService topology, ILayer3InfoService layer3,
108 IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200109 this.floodlightProvider = floodlightProvider;
110 this.topology = topology;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200111 this.layer3 = layer3;
Jonathan Hart5afde492013-10-01 12:30:53 +1300112 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200113
Jonathan Hartabad6a52013-09-30 18:17:21 +1300114 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200115
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200116 arpRequests = Multimaps.synchronizedSetMultimap(
117 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200118 }
119
Jonathan Hart2f790d22013-08-15 14:01:24 +1200120 public void startUp() {
Jonathan Hart5afde492013-10-01 12:30:53 +1300121 restApi.addRestletRoutable(new ArpWebRoutable());
122
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200123 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200124 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
Jonathan Hart1912afc2013-10-11 12:02:44 +1300227 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200228 if (log.isTraceEnabled()) {
229 log.trace("ARP request received for {}",
230 inetAddressToString(arp.getTargetProtocolAddress()));
231 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200232
233 InetAddress target;
234 try {
235 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
236 } catch (UnknownHostException e) {
237 log.debug("Invalid address in ARP request", e);
238 return;
239 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200240
241 if (layer3.fromExternalNetwork(sw.getId(), pi.getInPort())) {
242 //If the request came from outside our network, we only care if
243 //it was a request for one of our interfaces.
244 if (layer3.isInterfaceAddress(target)) {
245 log.trace("ARP request for our interface. Sending reply {} => {}",
246 target.getHostAddress(), layer3.getRouterMacAddress());
247
248 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300249 layer3.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200250 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200251
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200252 return;
253 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200254
Jonathan Hartabad6a52013-09-30 18:17:21 +1300255 MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200256
Jonathan Hartabad6a52013-09-30 18:17:21 +1300257 if (macAddress == null){
258 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200259
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200260 //Record where the request came from so we know where to send the reply
Jonathan Hart9ea31212013-08-12 21:40:34 +1200261 arpRequests.put(target, new ArpRequest(
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300262 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200263
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200264 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200265 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200266 }
267 else {
268 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200269 if (log.isTraceEnabled()) {
270 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
271 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300272 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200273 HexString.toHexString(sw.getId()), pi.getInPort()});
274 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200275
Jonathan Hartabad6a52013-09-30 18:17:21 +1300276 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200277 }
278 }
279
Jonathan Hart1912afc2013-10-11 12:02:44 +1300280 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200281 if (log.isTraceEnabled()) {
282 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
283 inetAddressToString(arp.getSenderProtocolAddress()),
284 HexString.toHexString(arp.getSenderHardwareAddress()),
285 HexString.toHexString(sw.getId()), pi.getInPort()});
286 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200287
Jonathan Hartabad6a52013-09-30 18:17:21 +1300288 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200289 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300290 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200291 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200292 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200293 return;
294 }
295
Jonathan Hartabad6a52013-09-30 18:17:21 +1300296 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
297
298 arpCache.update(senderIpAddress, senderMacAddress);
299
300 //See if anyone's waiting for this ARP reply
301 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200302
303 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200304 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200305 synchronized (arpRequests) {
306 Iterator<ArpRequest> it = requests.iterator();
307 while (it.hasNext()) {
308 ArpRequest request = it.next();
309 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200310 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200311 }
312 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200313
314 //Don't hold an ARP lock while dispatching requests
315 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300316 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200317 }
318 }
319
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200320 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200321 //TODO what should the sender IP address and MAC address be if no
322 //IP addresses are configured? Will there ever be a need to send
323 //ARP requests from the controller in that case?
324 //All-zero MAC address doesn't seem to work - hosts don't respond to it
325
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200326 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
327 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200328 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200329 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200330 (byte)0xff, (byte)0xff, (byte)0xff};
331
332 ARP arpRequest = new ARP();
333
334 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
335 .setProtocolType(ARP.PROTO_TYPE_IP)
336 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200337 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200338 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200339 .setTargetHardwareAddress(zeroMac)
340 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200341
Jonathan Hart08ee8522013-09-22 17:34:43 +1200342 MACAddress routerMacAddress = layer3.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200343 //TODO hack for now as it's unclear what the MAC address should be
344 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200345 if (routerMacAddress != null) {
346 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200347 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200348 arpRequest.setSenderHardwareAddress(senderMacAddress);
349
350 byte[] senderIPAddress = zeroIpv4;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200351 Interface intf = layer3.getOutgoingInterface(ipAddress);
352 if (intf != null) {
353 senderIPAddress = intf.getIpAddress().getAddress();
354 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200355
356 arpRequest.setSenderProtocolAddress(senderIPAddress);
357
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200358 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200359 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200360 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200361 .setEtherType(Ethernet.TYPE_ARP)
362 .setPayload(arpRequest);
363
Jonathan Hart2f790d22013-08-15 14:01:24 +1200364 sendArpRequestToSwitches(ipAddress, eth.serialize());
365 }
366
367 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200368 sendArpRequestToSwitches(dstAddress, arpRequest,
369 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200370 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200371
Jonathan Hart2f790d22013-08-15 14:01:24 +1200372 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
373 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200374
Jonathan Hart08ee8522013-09-22 17:34:43 +1200375 if (layer3.hasLayer3Configuration()) {
376 Interface intf = layer3.getOutgoingInterface(dstAddress);
377 if (intf != null) {
378 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
379 }
380 else {
381 //TODO here it should be broadcast out all non-interface edge ports.
382 //I think we can assume that if it's not a request for an external
383 //network, it's an ARP for a host in our own network. So we want to
384 //send it out all edge ports that don't have an interface configured
385 //to ensure it reaches all hosts in our network.
386 log.debug("No interface found to send ARP request for {}",
387 dstAddress.getHostAddress());
388 }
389 }
390 else {
391 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
392 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200393 }
394
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200395 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200396 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
397 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
398 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
399
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200400 if (linkPorts == null){
401 //I think this means the switch isn't known to topology yet.
402 //Maybe it only just joined.
403 continue;
404 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200405
406 OFPacketOut po = new OFPacketOut();
407 po.setInPort(OFPort.OFPP_NONE)
408 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200409 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200410
411 List<OFAction> actions = new ArrayList<OFAction>();
412
413 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200414 if (linkPorts.contains(portNum) ||
415 (sw.getId() == inSwitch && portNum == inPort)){
416 //If this port isn't an edge port or is the ingress port
417 //for the ARP, don't broadcast out it
418 continue;
419 }
420
421 actions.add(new OFActionOutput(portNum));
422 }
423
424 po.setActions(actions);
425 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
426 po.setActionsLength(actionsLength);
427 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200428 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200429
430 List<OFMessage> msgList = new ArrayList<OFMessage>();
431 msgList.add(po);
432
433 try {
434 sw.write(msgList, null);
435 sw.flush();
436 } catch (IOException e) {
437 log.error("Failure writing packet out to switch", e);
438 }
439 }
440 }
441
Jonathan Hart2f790d22013-08-15 14:01:24 +1200442 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200443 if (log.isTraceEnabled()) {
444 log.trace("Sending ARP request out {}/{}",
445 HexString.toHexString(dpid), port);
446 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200447
448 OFPacketOut po = new OFPacketOut();
449 po.setInPort(OFPort.OFPP_NONE)
450 .setBufferId(-1)
451 .setPacketData(arpRequest);
452
453 List<OFAction> actions = new ArrayList<OFAction>();
454 actions.add(new OFActionOutput(port));
455 po.setActions(actions);
456 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
457 po.setActionsLength(actionsLength);
458 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
459 + arpRequest.length);
460
461 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
462
463 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200464 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200465 return;
466 }
467
468 try {
469 sw.write(po, null);
470 sw.flush();
471 } catch (IOException e) {
472 log.error("Failure writing packet out to switch", e);
473 }
474 }
475
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300476 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200477 if (log.isTraceEnabled()) {
478 log.trace("Sending reply {} => {} to {}", new Object[] {
479 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300480 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200481 inetAddressToString(arpRequest.getSenderProtocolAddress())});
482 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200483
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200484 ARP arpReply = new ARP();
485 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
486 .setProtocolType(ARP.PROTO_TYPE_IP)
487 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200488 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200489 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300490 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200491 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
492 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
493 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
494
495 Ethernet eth = new Ethernet();
496 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300497 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200498 .setEtherType(Ethernet.TYPE_ARP)
499 .setPayload(arpReply);
500
501 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200502 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200503
504 OFPacketOut po = new OFPacketOut();
505 po.setInPort(OFPort.OFPP_NONE)
506 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200507 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200508 .setActions(actions)
509 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
510 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
511 + po.getPacketData().length);
512
513 List<OFMessage> msgList = new ArrayList<OFMessage>();
514 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200515
516 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
517
518 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200519 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200520 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200521 return;
522 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200523
524 try {
525 sw.write(msgList, null);
526 sw.flush();
527 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200528 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200529 }
530 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300531
532 private String inetAddressToString(byte[] bytes) {
533 try {
534 return InetAddress.getByAddress(bytes).getHostAddress();
535 } catch (UnknownHostException e) {
536 log.debug("Invalid IP address", e);
537 return "";
538 }
539 }
540
541 /*
542 * IProxyArpService methods
543 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200544
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200545 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300546 public MACAddress getMacAddress(InetAddress ipAddress) {
547 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200548 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200549
550 @Override
551 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
552 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200553 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200554
Jonathan Hart6e618212013-08-21 22:28:43 +1200555 //Sanity check to make sure we don't send a request for our own address
Jonathan Hart08ee8522013-09-22 17:34:43 +1200556 if (!layer3.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200557 sendArpRequestForAddress(ipAddress);
558 }
559 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300560
561 @Override
562 public List<String> getMappings() {
563 return arpCache.getMappings();
564 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200565}