blob: 10aa87a532b5a111d1820434e062acc53d390c89 [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 Hartc7ca35d2013-06-25 20:54:25 +120022import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120023import net.floodlightcontroller.util.MACAddress;
Jonathan Hart08ee8522013-09-22 17:34:43 +120024import net.onrc.onos.ofcontroller.bgproute.ILayer3InfoService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120025import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120026
27import org.openflow.protocol.OFMessage;
28import org.openflow.protocol.OFPacketIn;
29import org.openflow.protocol.OFPacketOut;
30import org.openflow.protocol.OFPort;
31import org.openflow.protocol.OFType;
32import org.openflow.protocol.action.OFAction;
33import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120034import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Jonathan Hart4dfc3652013-08-02 20:22:36 +120038import com.google.common.collect.HashMultimap;
39import com.google.common.collect.Multimaps;
40import com.google.common.collect.SetMultimap;
41
Jonathan Harte751e1c2013-08-23 00:48:47 +120042//TODO REST API to inspect ARP table
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 Hartc7ca35d2013-06-25 20:54:25 +120053
Jonathan Hartabad6a52013-09-30 18:17:21 +130054 private final ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120055
Jonathan Hartabad6a52013-09-30 18:17:21 +130056 private final SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120057
Jonathan Hartabad6a52013-09-30 18:17:21 +130058 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120059 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130060 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120061 private long requestTime;
62
Jonathan Hart4dfc3652013-08-02 20:22:36 +120063 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120064 this.requester = requester;
65 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120066 this.requestTime = System.currentTimeMillis();
67 }
68
Jonathan Hart4dfc3652013-08-02 20:22:36 +120069 public ArpRequest(ArpRequest old) {
70 this.requester = old.requester;
71 this.retry = old.retry;
72 this.requestTime = System.currentTimeMillis();
73 }
74
Jonathan Hart4dfc3652013-08-02 20:22:36 +120075 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +130076 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120077 }
78
Jonathan Hart4dfc3652013-08-02 20:22:36 +120079 public boolean shouldRetry() {
80 return retry;
81 }
82
Jonathan Hartabad6a52013-09-30 18:17:21 +130083 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120084 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120085 }
86 }
87
Jonathan Hartda4d0e12013-09-30 21:00:20 +130088 private class HostArpRequester implements IArpRequester {
89 private final ARP arpRequest;
90 private final long dpid;
91 private final short port;
92
93 public HostArpRequester(ARP arpRequest, long dpid, short port) {
94 this.arpRequest = arpRequest;
95 this.dpid = dpid;
96 this.port = port;
97 }
98
99 @Override
100 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
101 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
102 }
103 }
104
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200105 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart08ee8522013-09-22 17:34:43 +1200106 ITopologyService topology, ILayer3InfoService layer3){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200107 this.floodlightProvider = floodlightProvider;
108 this.topology = topology;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200109 this.layer3 = layer3;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200110
Jonathan Hartabad6a52013-09-30 18:17:21 +1300111 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200112
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200113 arpRequests = Multimaps.synchronizedSetMultimap(
114 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200115 }
116
Jonathan Hart2f790d22013-08-15 14:01:24 +1200117 public void startUp() {
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200118 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200119 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200120 @Override
121 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200122 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200123 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200124 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200125 }
126
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200127 /*
128 * Function that runs periodically to manage the asynchronous request mechanism.
129 * It basically cleans up old ARP requests if we don't get a response for them.
130 * The caller can designate that a request should be retried indefinitely, and
131 * this task will handle that as well.
132 */
133 private void doPeriodicArpProcessing() {
134 SetMultimap<InetAddress, ArpRequest> retryList
135 = HashMultimap.<InetAddress, ArpRequest>create();
136
137 //Have to synchronize externally on the Multimap while using an iterator,
138 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200139 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200140 log.debug("Current have {} outstanding requests",
141 arpRequests.size());
142
143 Iterator<Map.Entry<InetAddress, ArpRequest>> it
144 = arpRequests.entries().iterator();
145
146 while (it.hasNext()) {
147 Map.Entry<InetAddress, ArpRequest> entry
148 = it.next();
149 ArpRequest request = entry.getValue();
150 if (request.isExpired()) {
151 log.debug("Cleaning expired ARP request for {}",
152 entry.getKey().getHostAddress());
153
154 it.remove();
155
156 if (request.shouldRetry()) {
157 retryList.put(entry.getKey(), request);
158 }
159 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200160 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200161 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200162
163 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
164 : retryList.asMap().entrySet()) {
165
166 InetAddress address = entry.getKey();
167
168 log.debug("Resending ARP request for {}", address.getHostAddress());
169
170 sendArpRequestForAddress(address);
171
172 for (ArpRequest request : entry.getValue()) {
173 arpRequests.put(address, new ArpRequest(request));
174 }
175 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200176 }
177
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200178 @Override
179 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200180 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200181 }
182
183 @Override
184 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200185 return false;
186 }
187
188 @Override
189 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200190 return false;
191 }
192
193 @Override
194 public Command receive(
195 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
196
197 if (msg.getType() != OFType.PACKET_IN){
198 return Command.CONTINUE;
199 }
200
201 OFPacketIn pi = (OFPacketIn) msg;
202
203 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
204 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
205
206 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200207 ARP arp = (ARP) eth.getPayload();
208
209 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200210 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200211 }
212 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200213 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200214 }
215 }
216
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200217 //TODO should we propagate ARP or swallow it?
218 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200219 return Command.CONTINUE;
220 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200221
222 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200223 if (log.isTraceEnabled()) {
224 log.trace("ARP request received for {}",
225 inetAddressToString(arp.getTargetProtocolAddress()));
226 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200227
228 InetAddress target;
229 try {
230 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
231 } catch (UnknownHostException e) {
232 log.debug("Invalid address in ARP request", e);
233 return;
234 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200235
236 if (layer3.fromExternalNetwork(sw.getId(), pi.getInPort())) {
237 //If the request came from outside our network, we only care if
238 //it was a request for one of our interfaces.
239 if (layer3.isInterfaceAddress(target)) {
240 log.trace("ARP request for our interface. Sending reply {} => {}",
241 target.getHostAddress(), layer3.getRouterMacAddress());
242
243 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300244 layer3.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200245 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200246
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200247 return;
248 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200249
Jonathan Hartabad6a52013-09-30 18:17:21 +1300250 MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200251
Jonathan Hartabad6a52013-09-30 18:17:21 +1300252 if (macAddress == null){
253 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200254
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200255 //Record where the request came from so we know where to send the reply
Jonathan Hart9ea31212013-08-12 21:40:34 +1200256 arpRequests.put(target, new ArpRequest(
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300257 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200258
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200259 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200260 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200261 }
262 else {
263 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200264 if (log.isTraceEnabled()) {
265 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
266 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300267 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200268 HexString.toHexString(sw.getId()), pi.getInPort()});
269 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200270
Jonathan Hartabad6a52013-09-30 18:17:21 +1300271 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200272 }
273 }
274
275 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200276 if (log.isTraceEnabled()) {
277 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
278 inetAddressToString(arp.getSenderProtocolAddress()),
279 HexString.toHexString(arp.getSenderHardwareAddress()),
280 HexString.toHexString(sw.getId()), pi.getInPort()});
281 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200282
Jonathan Hartabad6a52013-09-30 18:17:21 +1300283 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200284 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300285 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200286 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200287 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200288 return;
289 }
290
Jonathan Hartabad6a52013-09-30 18:17:21 +1300291 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
292
293 arpCache.update(senderIpAddress, senderMacAddress);
294
295 //See if anyone's waiting for this ARP reply
296 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200297
298 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200299 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200300 synchronized (arpRequests) {
301 Iterator<ArpRequest> it = requests.iterator();
302 while (it.hasNext()) {
303 ArpRequest request = it.next();
304 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200305 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200306 }
307 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200308
309 //Don't hold an ARP lock while dispatching requests
310 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300311 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200312 }
313 }
314
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200315 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200316 //TODO what should the sender IP address and MAC address be if no
317 //IP addresses are configured? Will there ever be a need to send
318 //ARP requests from the controller in that case?
319 //All-zero MAC address doesn't seem to work - hosts don't respond to it
320
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200321 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
322 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200323 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200324 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200325 (byte)0xff, (byte)0xff, (byte)0xff};
326
327 ARP arpRequest = new ARP();
328
329 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
330 .setProtocolType(ARP.PROTO_TYPE_IP)
331 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200332 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200333 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200334 .setTargetHardwareAddress(zeroMac)
335 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200336
Jonathan Hart08ee8522013-09-22 17:34:43 +1200337 MACAddress routerMacAddress = layer3.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200338 //TODO hack for now as it's unclear what the MAC address should be
339 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200340 if (routerMacAddress != null) {
341 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200342 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200343 arpRequest.setSenderHardwareAddress(senderMacAddress);
344
345 byte[] senderIPAddress = zeroIpv4;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200346 Interface intf = layer3.getOutgoingInterface(ipAddress);
347 if (intf != null) {
348 senderIPAddress = intf.getIpAddress().getAddress();
349 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200350
351 arpRequest.setSenderProtocolAddress(senderIPAddress);
352
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200353 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200354 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200355 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200356 .setEtherType(Ethernet.TYPE_ARP)
357 .setPayload(arpRequest);
358
Jonathan Hart2f790d22013-08-15 14:01:24 +1200359 sendArpRequestToSwitches(ipAddress, eth.serialize());
360 }
361
362 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200363 sendArpRequestToSwitches(dstAddress, arpRequest,
364 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200365 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200366
Jonathan Hart2f790d22013-08-15 14:01:24 +1200367 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
368 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200369
Jonathan Hart08ee8522013-09-22 17:34:43 +1200370 if (layer3.hasLayer3Configuration()) {
371 Interface intf = layer3.getOutgoingInterface(dstAddress);
372 if (intf != null) {
373 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
374 }
375 else {
376 //TODO here it should be broadcast out all non-interface edge ports.
377 //I think we can assume that if it's not a request for an external
378 //network, it's an ARP for a host in our own network. So we want to
379 //send it out all edge ports that don't have an interface configured
380 //to ensure it reaches all hosts in our network.
381 log.debug("No interface found to send ARP request for {}",
382 dstAddress.getHostAddress());
383 }
384 }
385 else {
386 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
387 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200388 }
389
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200390 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200391 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
392 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
393 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
394
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200395 if (linkPorts == null){
396 //I think this means the switch isn't known to topology yet.
397 //Maybe it only just joined.
398 continue;
399 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200400
401 OFPacketOut po = new OFPacketOut();
402 po.setInPort(OFPort.OFPP_NONE)
403 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200404 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200405
406 List<OFAction> actions = new ArrayList<OFAction>();
407
408 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200409 if (linkPorts.contains(portNum) ||
410 (sw.getId() == inSwitch && portNum == inPort)){
411 //If this port isn't an edge port or is the ingress port
412 //for the ARP, don't broadcast out it
413 continue;
414 }
415
416 actions.add(new OFActionOutput(portNum));
417 }
418
419 po.setActions(actions);
420 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
421 po.setActionsLength(actionsLength);
422 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200423 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200424
425 List<OFMessage> msgList = new ArrayList<OFMessage>();
426 msgList.add(po);
427
428 try {
429 sw.write(msgList, null);
430 sw.flush();
431 } catch (IOException e) {
432 log.error("Failure writing packet out to switch", e);
433 }
434 }
435 }
436
Jonathan Hart2f790d22013-08-15 14:01:24 +1200437 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200438 if (log.isTraceEnabled()) {
439 log.trace("Sending ARP request out {}/{}",
440 HexString.toHexString(dpid), port);
441 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200442
443 OFPacketOut po = new OFPacketOut();
444 po.setInPort(OFPort.OFPP_NONE)
445 .setBufferId(-1)
446 .setPacketData(arpRequest);
447
448 List<OFAction> actions = new ArrayList<OFAction>();
449 actions.add(new OFActionOutput(port));
450 po.setActions(actions);
451 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
452 po.setActionsLength(actionsLength);
453 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
454 + arpRequest.length);
455
456 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
457
458 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200459 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200460 return;
461 }
462
463 try {
464 sw.write(po, null);
465 sw.flush();
466 } catch (IOException e) {
467 log.error("Failure writing packet out to switch", e);
468 }
469 }
470
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300471 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200472 if (log.isTraceEnabled()) {
473 log.trace("Sending reply {} => {} to {}", new Object[] {
474 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300475 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200476 inetAddressToString(arpRequest.getSenderProtocolAddress())});
477 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200478
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200479 ARP arpReply = new ARP();
480 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
481 .setProtocolType(ARP.PROTO_TYPE_IP)
482 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200483 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200484 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300485 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200486 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
487 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
488 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
489
490 Ethernet eth = new Ethernet();
491 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300492 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200493 .setEtherType(Ethernet.TYPE_ARP)
494 .setPayload(arpReply);
495
496 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200497 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200498
499 OFPacketOut po = new OFPacketOut();
500 po.setInPort(OFPort.OFPP_NONE)
501 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200502 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200503 .setActions(actions)
504 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
505 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
506 + po.getPacketData().length);
507
508 List<OFMessage> msgList = new ArrayList<OFMessage>();
509 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200510
511 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
512
513 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200514 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200515 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200516 return;
517 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200518
519 try {
520 sw.write(msgList, null);
521 sw.flush();
522 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200523 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200524 }
525 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300526
527 private String inetAddressToString(byte[] bytes) {
528 try {
529 return InetAddress.getByAddress(bytes).getHostAddress();
530 } catch (UnknownHostException e) {
531 log.debug("Invalid IP address", e);
532 return "";
533 }
534 }
535
536 /*
537 * IProxyArpService methods
538 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200539
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200540 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300541 public MACAddress getMacAddress(InetAddress ipAddress) {
542 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200543 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200544
545 @Override
546 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
547 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200548 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200549
Jonathan Hart6e618212013-08-21 22:28:43 +1200550 //Sanity check to make sure we don't send a request for our own address
Jonathan Hart08ee8522013-09-22 17:34:43 +1200551 if (!layer3.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200552 sendArpRequestForAddress(ipAddress);
553 }
554 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200555}