blob: 4d261a2170641413e387304f99cc5064cfccf371 [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 Harta18e4792013-10-31 10:10:54 -07008import java.util.HashSet;
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 Hart5afde492013-10-01 12:30:53 +130023import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120024import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120025import net.floodlightcontroller.util.MACAddress;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080026import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120027import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070028import net.onrc.onos.ofcontroller.core.IDeviceStorage;
29import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080030import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService;
31import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoSwitchService;
Jonathan Hartebba1e12013-10-29 11:37:02 -070032import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070033import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120034
35import org.openflow.protocol.OFMessage;
36import org.openflow.protocol.OFPacketIn;
37import org.openflow.protocol.OFPacketOut;
38import org.openflow.protocol.OFPort;
39import org.openflow.protocol.OFType;
40import org.openflow.protocol.action.OFAction;
41import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120042import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
Jonathan Hart4dfc3652013-08-02 20:22:36 +120046import com.google.common.collect.HashMultimap;
47import com.google.common.collect.Multimaps;
48import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070049import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120050
Jonathan Hart18ad55c2013-11-11 22:49:55 -080051public class ProxyArpManager implements IProxyArpService, IOFMessageListener,
52 IArpEventHandler {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120053 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120054
Jonathan Hartdf6ec332013-08-04 01:37:14 +120055 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130056
57 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120058
Jonathan Harta8887642013-10-28 13:46:54 -070059 private IFloodlightProviderService floodlightProvider;
60 private ITopologyService topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080061 private IDatagridService datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -070062 private IConfigInfoService configService;
63 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120064
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070065 private IDeviceStorage deviceStorage;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080066 private ITopoSwitchService topoSwitchService;
67 private ITopoLinkService topoLinkService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070068
Jonathan Hart1cf9de02013-10-21 17:42:29 -070069 private short vlan;
70 private static final short NO_VLAN = 0;
71
Jonathan Harta8887642013-10-28 13:46:54 -070072 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120073
Jonathan Harta8887642013-10-28 13:46:54 -070074 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120075
Jonathan Hartabad6a52013-09-30 18:17:21 +130076 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120077 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130078 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120079 private long requestTime;
80
Jonathan Hart4dfc3652013-08-02 20:22:36 +120081 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120082 this.requester = requester;
83 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120084 this.requestTime = System.currentTimeMillis();
85 }
86
Jonathan Hart4dfc3652013-08-02 20:22:36 +120087 public ArpRequest(ArpRequest old) {
88 this.requester = old.requester;
89 this.retry = old.retry;
90 this.requestTime = System.currentTimeMillis();
91 }
92
Jonathan Hart4dfc3652013-08-02 20:22:36 +120093 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +130094 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120095 }
96
Jonathan Hart4dfc3652013-08-02 20:22:36 +120097 public boolean shouldRetry() {
98 return retry;
99 }
100
Jonathan Hartabad6a52013-09-30 18:17:21 +1300101 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200102 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200103 }
104 }
105
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300106 private class HostArpRequester implements IArpRequester {
107 private final ARP arpRequest;
108 private final long dpid;
109 private final short port;
110
111 public HostArpRequester(ARP arpRequest, long dpid, short port) {
112 this.arpRequest = arpRequest;
113 this.dpid = dpid;
114 this.port = port;
115 }
116
117 @Override
118 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
119 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
120 }
121 }
122
Jonathan Harta8887642013-10-28 13:46:54 -0700123 /*
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200124 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Harta8887642013-10-28 13:46:54 -0700125 ITopologyService topology, IConfigInfoService configService,
Jonathan Hart5afde492013-10-01 12:30:53 +1300126 IRestApiService restApi){
Jonathan Harta8887642013-10-28 13:46:54 -0700127
128 }
129 */
130
131 public void init(IFloodlightProviderService floodlightProvider,
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800132 ITopologyService topology, IDatagridService datagrid,
Jonathan Harta18e4792013-10-31 10:10:54 -0700133 IConfigInfoService config, IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200134 this.floodlightProvider = floodlightProvider;
135 this.topology = topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800136 this.datagrid = datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -0700137 this.configService = config;
Jonathan Hart5afde492013-10-01 12:30:53 +1300138 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200139
Jonathan Hartabad6a52013-09-30 18:17:21 +1300140 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200141
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200142 arpRequests = Multimaps.synchronizedSetMultimap(
143 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200144 }
145
Jonathan Harta8887642013-10-28 13:46:54 -0700146 public void startUp() {
147 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700148 log.info("vlan set to {}", this.vlan);
149
Jonathan Hart5afde492013-10-01 12:30:53 +1300150 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700151 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300152
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800153 datagrid.registerArpEventHandler(this);
154
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700155 deviceStorage = new DeviceStorageImpl();
156 deviceStorage.init("");
157
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200158 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200159 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200160 @Override
161 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200162 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200163 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200164 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200165 }
166
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200167 /*
168 * Function that runs periodically to manage the asynchronous request mechanism.
169 * It basically cleans up old ARP requests if we don't get a response for them.
170 * The caller can designate that a request should be retried indefinitely, and
171 * this task will handle that as well.
172 */
173 private void doPeriodicArpProcessing() {
174 SetMultimap<InetAddress, ArpRequest> retryList
175 = HashMultimap.<InetAddress, ArpRequest>create();
176
177 //Have to synchronize externally on the Multimap while using an iterator,
178 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200179 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200180 log.debug("Current have {} outstanding requests",
181 arpRequests.size());
182
183 Iterator<Map.Entry<InetAddress, ArpRequest>> it
184 = arpRequests.entries().iterator();
185
186 while (it.hasNext()) {
187 Map.Entry<InetAddress, ArpRequest> entry
188 = it.next();
189 ArpRequest request = entry.getValue();
190 if (request.isExpired()) {
191 log.debug("Cleaning expired ARP request for {}",
192 entry.getKey().getHostAddress());
193
194 it.remove();
195
196 if (request.shouldRetry()) {
197 retryList.put(entry.getKey(), request);
198 }
199 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200200 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200201 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200202
203 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
204 : retryList.asMap().entrySet()) {
205
206 InetAddress address = entry.getKey();
207
208 log.debug("Resending ARP request for {}", address.getHostAddress());
209
210 sendArpRequestForAddress(address);
211
212 for (ArpRequest request : entry.getValue()) {
213 arpRequests.put(address, new ArpRequest(request));
214 }
215 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200216 }
217
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200218 @Override
219 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700220 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200221 }
222
223 @Override
224 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700225 if (type == OFType.PACKET_IN) {
226 return "devicemanager".equals(name);
227 }
228 else {
229 return false;
230 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200231 }
232
233 @Override
234 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200235 return false;
236 }
237
238 @Override
239 public Command receive(
240 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
241
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800242 //if (msg.getType() != OFType.PACKET_IN){
243 //return Command.CONTINUE;
244 //}
245 log.debug("received packet");
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200246
247 OFPacketIn pi = (OFPacketIn) msg;
248
249 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
250 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
251
252 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200253 ARP arp = (ARP) eth.getPayload();
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800254 log.debug("etharp {}", arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200255 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700256 //TODO check what the DeviceManager does about propagating
257 //or swallowing ARPs. We want to go after DeviceManager in the
258 //chain but we really need it to CONTINUE ARP packets so we can
259 //get them.
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800260 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200261 }
262 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700263 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200264 }
265 }
266
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200267 //TODO should we propagate ARP or swallow it?
268 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200269 return Command.CONTINUE;
270 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200271
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800272 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200273 if (log.isTraceEnabled()) {
274 log.trace("ARP request received for {}",
275 inetAddressToString(arp.getTargetProtocolAddress()));
276 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200277
278 InetAddress target;
279 try {
280 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
281 } catch (UnknownHostException e) {
282 log.debug("Invalid address in ARP request", e);
283 return;
284 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200285
Jonathan Harta8887642013-10-28 13:46:54 -0700286 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200287 //If the request came from outside our network, we only care if
288 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700289 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200290 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700291 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200292
293 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700294 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200295 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200296
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200297 return;
298 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200299
Jonathan Harta18e4792013-10-31 10:10:54 -0700300 //MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200301
Jonathan Harta18e4792013-10-31 10:10:54 -0700302 //IDevice dstDevice = deviceService.fcStore.get(cntx, IDeviceService.CONTEXT_DST_DEVICE);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700303 //Iterator<? extends IDevice> it = deviceService.queryDevices(
304 //null, null, InetAddresses.coerceToInteger(target), null, null);
Jonathan Harta18e4792013-10-31 10:10:54 -0700305
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700306 //IDevice targetDevice = null;
307 //if (it.hasNext()) {
308 //targetDevice = it.next();
309 //}
310 IDeviceObject targetDevice =
311 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
Jonathan Harta18e4792013-10-31 10:10:54 -0700312
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800313 log.debug("targetDevice: {}", targetDevice);
314
Jonathan Harta18e4792013-10-31 10:10:54 -0700315 if (targetDevice != null) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800316 // We have the device in our database, so send a reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700317 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
318
319 if (log.isTraceEnabled()) {
320 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
321 inetAddressToString(arp.getTargetProtocolAddress()),
322 macAddress.toString(),
323 HexString.toHexString(sw.getId()), pi.getInPort()});
324 }
325
326 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
327 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800328 else {
329 // We don't know the device so broadcast the request out
330 // the edge of the network
331
332 //Record where the request came from so we know where to send the reply
333 arpRequests.put(target, new ArpRequest(
334 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
335
336 sendArpRequestToEdge(eth, pi);
337 }
Jonathan Harta18e4792013-10-31 10:10:54 -0700338
339 /*if (macAddress == null){
Jonathan Hartabad6a52013-09-30 18:17:21 +1300340 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200341
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200342 //Record where the request came from so we know where to send the reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700343 //arpRequests.put(target, new ArpRequest(
344 //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200345
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200346 //Flood the request out edge ports
Jonathan Harta18e4792013-10-31 10:10:54 -0700347 //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200348 }
349 else {
350 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200351 if (log.isTraceEnabled()) {
352 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
353 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300354 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200355 HexString.toHexString(sw.getId()), pi.getInPort()});
356 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200357
Jonathan Hartabad6a52013-09-30 18:17:21 +1300358 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Harta18e4792013-10-31 10:10:54 -0700359 }*/
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200360 }
361
Jonathan Hart1912afc2013-10-11 12:02:44 +1300362 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200363 if (log.isTraceEnabled()) {
364 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
365 inetAddressToString(arp.getSenderProtocolAddress()),
366 HexString.toHexString(arp.getSenderHardwareAddress()),
367 HexString.toHexString(sw.getId()), pi.getInPort()});
368 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200369
Jonathan Hartabad6a52013-09-30 18:17:21 +1300370 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200371 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300372 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200373 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200374 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200375 return;
376 }
377
Jonathan Hartabad6a52013-09-30 18:17:21 +1300378 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
379
380 arpCache.update(senderIpAddress, senderMacAddress);
381
382 //See if anyone's waiting for this ARP reply
383 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200384
385 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200386 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200387 synchronized (arpRequests) {
388 Iterator<ArpRequest> it = requests.iterator();
389 while (it.hasNext()) {
390 ArpRequest request = it.next();
391 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200392 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200393 }
394 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200395
396 //Don't hold an ARP lock while dispatching requests
397 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300398 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200399 }
400 }
401
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200402 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200403 //TODO what should the sender IP address and MAC address be if no
404 //IP addresses are configured? Will there ever be a need to send
405 //ARP requests from the controller in that case?
406 //All-zero MAC address doesn't seem to work - hosts don't respond to it
407
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200408 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
409 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200410 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200411 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200412 (byte)0xff, (byte)0xff, (byte)0xff};
413
414 ARP arpRequest = new ARP();
415
416 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
417 .setProtocolType(ARP.PROTO_TYPE_IP)
418 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200419 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200420 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200421 .setTargetHardwareAddress(zeroMac)
422 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200423
Jonathan Harta8887642013-10-28 13:46:54 -0700424 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200425 //TODO hack for now as it's unclear what the MAC address should be
426 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200427 if (routerMacAddress != null) {
428 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200429 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200430 arpRequest.setSenderHardwareAddress(senderMacAddress);
431
432 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700433 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200434 if (intf != null) {
435 senderIPAddress = intf.getIpAddress().getAddress();
436 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200437
438 arpRequest.setSenderProtocolAddress(senderIPAddress);
439
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200440 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200441 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200442 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200443 .setEtherType(Ethernet.TYPE_ARP)
444 .setPayload(arpRequest);
445
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700446 if (vlan != NO_VLAN) {
447 eth.setVlanID(vlan)
448 .setPriorityCode((byte)0);
449 }
450
Jonathan Hart2f790d22013-08-15 14:01:24 +1200451 sendArpRequestToSwitches(ipAddress, eth.serialize());
452 }
453
454 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200455 sendArpRequestToSwitches(dstAddress, arpRequest,
456 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200457 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200458
Jonathan Hart2f790d22013-08-15 14:01:24 +1200459 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
460 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200461
Jonathan Harta8887642013-10-28 13:46:54 -0700462 if (configService.hasLayer3Configuration()) {
463 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200464 if (intf != null) {
465 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
466 }
467 else {
468 //TODO here it should be broadcast out all non-interface edge ports.
469 //I think we can assume that if it's not a request for an external
470 //network, it's an ARP for a host in our own network. So we want to
471 //send it out all edge ports that don't have an interface configured
472 //to ensure it reaches all hosts in our network.
473 log.debug("No interface found to send ARP request for {}",
474 dstAddress.getHostAddress());
475 }
476 }
477 else {
478 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
479 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200480 }
481
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800482 private void sendArpRequestToEdge(Ethernet arpRequest, OFPacketIn pi) {
483 // Pull the topology from the network map and find edge ports
484 /*for (ISwitchObject swObject : topoSwitchService.getActiveSwitches()) {
485 //List<Link> linksOnSwitch = topoLinkService.getLinksOnSwitch(swObject.getDPID());
486
487 Set<IPortObject> edgePorts = new HashSet<IPortObject>();
488
489 for (IPortObject portObject : swObject.getPorts()) {
490 if (!portObject.getLinkedPorts().iterator().hasNext()) {
491 edgePorts.add(portObject);
492 }
493 }
494
495
496 }*/
497 //log.debug("Sending ARP request for {} to other ONOS instances",
498 //HexString.toHexString(arpRequest.getTargetProtocolAddress()));
499 //log.debug("Sent request bytes: {}", );
500 datagrid.sendArpRequest(arpRequest.serialize());
501 }
502
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200503 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200504 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
505 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
506 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
507
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200508 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700509 //I think this means the switch doesn't have any links.
510 //continue;
511 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200512 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200513
Jonathan Harta18e4792013-10-31 10:10:54 -0700514
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200515 OFPacketOut po = new OFPacketOut();
516 po.setInPort(OFPort.OFPP_NONE)
517 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200518 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200519
520 List<OFAction> actions = new ArrayList<OFAction>();
521
522 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200523 if (linkPorts.contains(portNum) ||
524 (sw.getId() == inSwitch && portNum == inPort)){
525 //If this port isn't an edge port or is the ingress port
526 //for the ARP, don't broadcast out it
527 continue;
528 }
529
530 actions.add(new OFActionOutput(portNum));
531 }
532
533 po.setActions(actions);
534 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
535 po.setActionsLength(actionsLength);
536 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200537 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200538
539 List<OFMessage> msgList = new ArrayList<OFMessage>();
540 msgList.add(po);
541
542 try {
543 sw.write(msgList, null);
544 sw.flush();
545 } catch (IOException e) {
546 log.error("Failure writing packet out to switch", e);
547 }
548 }
549 }
550
Jonathan Hart2f790d22013-08-15 14:01:24 +1200551 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200552 if (log.isTraceEnabled()) {
553 log.trace("Sending ARP request out {}/{}",
554 HexString.toHexString(dpid), port);
555 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200556
557 OFPacketOut po = new OFPacketOut();
558 po.setInPort(OFPort.OFPP_NONE)
559 .setBufferId(-1)
560 .setPacketData(arpRequest);
561
562 List<OFAction> actions = new ArrayList<OFAction>();
563 actions.add(new OFActionOutput(port));
564 po.setActions(actions);
565 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
566 po.setActionsLength(actionsLength);
567 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
568 + arpRequest.length);
569
570 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
571
572 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200573 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200574 return;
575 }
576
577 try {
578 sw.write(po, null);
579 sw.flush();
580 } catch (IOException e) {
581 log.error("Failure writing packet out to switch", e);
582 }
583 }
584
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300585 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200586 if (log.isTraceEnabled()) {
587 log.trace("Sending reply {} => {} to {}", new Object[] {
588 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300589 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200590 inetAddressToString(arpRequest.getSenderProtocolAddress())});
591 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200592
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200593 ARP arpReply = new ARP();
594 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
595 .setProtocolType(ARP.PROTO_TYPE_IP)
596 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200597 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200598 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300599 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200600 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
601 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
602 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
603
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700604
605
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200606 Ethernet eth = new Ethernet();
607 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300608 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200609 .setEtherType(Ethernet.TYPE_ARP)
610 .setPayload(arpReply);
611
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700612 if (vlan != NO_VLAN) {
613 eth.setVlanID(vlan)
614 .setPriorityCode((byte)0);
615 }
616
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200617 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200618 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200619
620 OFPacketOut po = new OFPacketOut();
621 po.setInPort(OFPort.OFPP_NONE)
622 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200623 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200624 .setActions(actions)
625 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
626 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
627 + po.getPacketData().length);
628
629 List<OFMessage> msgList = new ArrayList<OFMessage>();
630 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200631
632 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
633
634 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200635 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200636 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200637 return;
638 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200639
640 try {
641 sw.write(msgList, null);
642 sw.flush();
643 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200644 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200645 }
646 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300647
648 private String inetAddressToString(byte[] bytes) {
649 try {
650 return InetAddress.getByAddress(bytes).getHostAddress();
651 } catch (UnknownHostException e) {
652 log.debug("Invalid IP address", e);
653 return "";
654 }
655 }
656
657 /*
658 * IProxyArpService methods
659 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200660
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200661 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300662 public MACAddress getMacAddress(InetAddress ipAddress) {
663 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200664 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200665
666 @Override
667 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
668 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200669 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200670
Jonathan Hart6e618212013-08-21 22:28:43 +1200671 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700672 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200673 sendArpRequestForAddress(ipAddress);
674 }
675 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300676
677 @Override
678 public List<String> getMappings() {
679 return arpCache.getMappings();
680 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800681
682 /*
683 * IArpEventHandler methods
684 */
685
686 @Override
687 public void arpRequestNotification(byte[] arpRequest) {
688 log.debug("Received ARP notification from other instances");
689 broadcastArpRequestOutEdge(arpRequest, Long.MAX_VALUE, Short.MAX_VALUE);
690 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200691}