blob: 614b0e5f501590f8cfcd5d2dd63466b463ef0657 [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 Harte93aed42013-12-05 18:39:50 -08008import java.util.HashMap;
Jonathan Harta18e4792013-10-31 10:10:54 -07009import java.util.HashSet;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120010import java.util.Iterator;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120011import java.util.List;
12import java.util.Map;
13import java.util.Set;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120014import java.util.Timer;
15import java.util.TimerTask;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120016
17import net.floodlightcontroller.core.FloodlightContext;
18import net.floodlightcontroller.core.IFloodlightProviderService;
19import net.floodlightcontroller.core.IOFMessageListener;
20import net.floodlightcontroller.core.IOFSwitch;
Jonathan Harte93aed42013-12-05 18:39:50 -080021import net.floodlightcontroller.core.module.FloodlightModuleContext;
22import net.floodlightcontroller.core.module.IFloodlightModule;
23import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120024import net.floodlightcontroller.packet.ARP;
25import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart08ee8522013-09-22 17:34:43 +120026import net.floodlightcontroller.packet.IPv4;
Jonathan Hart5afde492013-10-01 12:30:53 +130027import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120028import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120029import net.floodlightcontroller.util.MACAddress;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080030import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120031import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070032import net.onrc.onos.ofcontroller.core.IDeviceStorage;
33import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080034import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080035import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoSwitchService;
Jonathan Hartebba1e12013-10-29 11:37:02 -070036import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070037import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080038import net.onrc.onos.ofcontroller.core.internal.TopoSwitchServiceImpl;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120039
40import org.openflow.protocol.OFMessage;
41import org.openflow.protocol.OFPacketIn;
42import org.openflow.protocol.OFPacketOut;
43import org.openflow.protocol.OFPort;
44import org.openflow.protocol.OFType;
45import org.openflow.protocol.action.OFAction;
46import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120047import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Jonathan Hart4dfc3652013-08-02 20:22:36 +120051import com.google.common.collect.HashMultimap;
52import com.google.common.collect.Multimaps;
53import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070054import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120055
Jonathan Hart18ad55c2013-11-11 22:49:55 -080056public class ProxyArpManager implements IProxyArpService, IOFMessageListener,
Jonathan Harte93aed42013-12-05 18:39:50 -080057 IArpEventHandler, IFloodlightModule {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120058 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120059
Jonathan Hartdf6ec332013-08-04 01:37:14 +120060 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130061
62 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120063
Jonathan Harta8887642013-10-28 13:46:54 -070064 private IFloodlightProviderService floodlightProvider;
65 private ITopologyService topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080066 private IDatagridService datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -070067 private IConfigInfoService configService;
68 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120069
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070070 private IDeviceStorage deviceStorage;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080071 private volatile ITopoSwitchService topoSwitchService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070072
Jonathan Hart1cf9de02013-10-21 17:42:29 -070073 private short vlan;
74 private static final short NO_VLAN = 0;
75
Jonathan Harta8887642013-10-28 13:46:54 -070076 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120077
Jonathan Harta8887642013-10-28 13:46:54 -070078 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120079
Jonathan Hartabad6a52013-09-30 18:17:21 +130080 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120081 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130082 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120083 private long requestTime;
84
Jonathan Hart4dfc3652013-08-02 20:22:36 +120085 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120086 this.requester = requester;
87 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120088 this.requestTime = System.currentTimeMillis();
89 }
90
Jonathan Hart4dfc3652013-08-02 20:22:36 +120091 public ArpRequest(ArpRequest old) {
92 this.requester = old.requester;
93 this.retry = old.retry;
94 this.requestTime = System.currentTimeMillis();
95 }
96
Jonathan Hart4dfc3652013-08-02 20:22:36 +120097 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +130098 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120099 }
100
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200101 public boolean shouldRetry() {
102 return retry;
103 }
104
Jonathan Hartabad6a52013-09-30 18:17:21 +1300105 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200106 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200107 }
108 }
109
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300110 private class HostArpRequester implements IArpRequester {
111 private final ARP arpRequest;
112 private final long dpid;
113 private final short port;
114
115 public HostArpRequester(ARP arpRequest, long dpid, short port) {
116 this.arpRequest = arpRequest;
117 this.dpid = dpid;
118 this.port = port;
119 }
120
121 @Override
122 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
123 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
124 }
125 }
126
Jonathan Harte93aed42013-12-05 18:39:50 -0800127 @Override
128 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
129 Collection<Class<? extends IFloodlightService>> l
130 = new ArrayList<Class<? extends IFloodlightService>>();
131 l.add(IProxyArpService.class);
132 return l;
Jonathan Harta8887642013-10-28 13:46:54 -0700133 }
Jonathan Harte93aed42013-12-05 18:39:50 -0800134
135 @Override
136 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
137 Map<Class<? extends IFloodlightService>, IFloodlightService> m
138 = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
139 m.put(IProxyArpService.class, this);
140 return m;
141 }
142
143 @Override
144 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
145 Collection<Class<? extends IFloodlightService>> dependencies
146 = new ArrayList<Class<? extends IFloodlightService>>();
147 dependencies.add(IFloodlightProviderService.class);
148 dependencies.add(ITopologyService.class);
149 dependencies.add(IRestApiService.class);
150 dependencies.add(IDatagridService.class);
151 dependencies.add(IConfigInfoService.class);
152 return dependencies;
153 }
Jonathan Harta8887642013-10-28 13:46:54 -0700154
Jonathan Harte93aed42013-12-05 18:39:50 -0800155 @Override
156 public void init(FloodlightModuleContext context){
157 this.floodlightProvider =
158 context.getServiceImpl(IFloodlightProviderService.class);
159 this.topology = context.getServiceImpl(ITopologyService.class);
160 this.datagrid = context.getServiceImpl(IDatagridService.class);
161 this.configService = context.getServiceImpl(IConfigInfoService.class);
162 this.restApi = context.getServiceImpl(IRestApiService.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200163
Jonathan Hartabad6a52013-09-30 18:17:21 +1300164 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200165
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200166 arpRequests = Multimaps.synchronizedSetMultimap(
167 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800168
169 topoSwitchService = new TopoSwitchServiceImpl();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200170 }
171
Jonathan Harte93aed42013-12-05 18:39:50 -0800172 @Override
173 public void startUp(FloodlightModuleContext context) {
Jonathan Harta8887642013-10-28 13:46:54 -0700174 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700175 log.info("vlan set to {}", this.vlan);
176
Jonathan Hart5afde492013-10-01 12:30:53 +1300177 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700178 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300179
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800180 datagrid.registerArpEventHandler(this);
181
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700182 deviceStorage = new DeviceStorageImpl();
183 deviceStorage.init("");
184
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200185 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200186 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200187 @Override
188 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200189 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200190 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200191 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200192 }
193
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200194 /*
195 * Function that runs periodically to manage the asynchronous request mechanism.
196 * It basically cleans up old ARP requests if we don't get a response for them.
197 * The caller can designate that a request should be retried indefinitely, and
198 * this task will handle that as well.
199 */
200 private void doPeriodicArpProcessing() {
201 SetMultimap<InetAddress, ArpRequest> retryList
202 = HashMultimap.<InetAddress, ArpRequest>create();
203
204 //Have to synchronize externally on the Multimap while using an iterator,
205 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200206 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200207 log.debug("Current have {} outstanding requests",
208 arpRequests.size());
209
210 Iterator<Map.Entry<InetAddress, ArpRequest>> it
211 = arpRequests.entries().iterator();
212
213 while (it.hasNext()) {
214 Map.Entry<InetAddress, ArpRequest> entry
215 = it.next();
216 ArpRequest request = entry.getValue();
217 if (request.isExpired()) {
218 log.debug("Cleaning expired ARP request for {}",
219 entry.getKey().getHostAddress());
220
221 it.remove();
222
223 if (request.shouldRetry()) {
224 retryList.put(entry.getKey(), request);
225 }
226 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200227 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200228 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200229
230 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
231 : retryList.asMap().entrySet()) {
232
233 InetAddress address = entry.getKey();
234
235 log.debug("Resending ARP request for {}", address.getHostAddress());
236
237 sendArpRequestForAddress(address);
238
239 for (ArpRequest request : entry.getValue()) {
240 arpRequests.put(address, new ArpRequest(request));
241 }
242 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200243 }
244
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200245 @Override
246 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700247 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200248 }
249
250 @Override
251 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700252 if (type == OFType.PACKET_IN) {
253 return "devicemanager".equals(name);
254 }
255 else {
256 return false;
257 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200258 }
259
260 @Override
261 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200262 return false;
263 }
264
265 @Override
266 public Command receive(
267 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
268
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800269 //if (msg.getType() != OFType.PACKET_IN){
270 //return Command.CONTINUE;
271 //}
272 log.debug("received packet");
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200273
274 OFPacketIn pi = (OFPacketIn) msg;
275
276 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
277 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
278
279 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800280 log.debug("is arp");
281 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200282 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800283 log.debug("is request");
Jonathan Harta18e4792013-10-31 10:10:54 -0700284 //TODO check what the DeviceManager does about propagating
285 //or swallowing ARPs. We want to go after DeviceManager in the
286 //chain but we really need it to CONTINUE ARP packets so we can
287 //get them.
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800288 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200289 }
290 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800291 log.debug("is reply");
Jonathan Harta18e4792013-10-31 10:10:54 -0700292 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200293 }
294 }
295
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200296 //TODO should we propagate ARP or swallow it?
297 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200298 return Command.CONTINUE;
299 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200300
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800301 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200302 if (log.isTraceEnabled()) {
303 log.trace("ARP request received for {}",
304 inetAddressToString(arp.getTargetProtocolAddress()));
305 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200306
307 InetAddress target;
308 try {
309 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
310 } catch (UnknownHostException e) {
311 log.debug("Invalid address in ARP request", e);
312 return;
313 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200314
Jonathan Harta8887642013-10-28 13:46:54 -0700315 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200316 //If the request came from outside our network, we only care if
317 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700318 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200319 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700320 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200321
322 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700323 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200324 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200325
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200326 return;
327 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200328
Jonathan Harta18e4792013-10-31 10:10:54 -0700329 //MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200330
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700331 IDeviceObject targetDevice =
332 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
Jonathan Harta18e4792013-10-31 10:10:54 -0700333
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800334 log.debug("targetDevice: {}", targetDevice);
335
Jonathan Harta18e4792013-10-31 10:10:54 -0700336 if (targetDevice != null) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800337 // We have the device in our database, so send a reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700338 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
339
340 if (log.isTraceEnabled()) {
341 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
342 inetAddressToString(arp.getTargetProtocolAddress()),
343 macAddress.toString(),
344 HexString.toHexString(sw.getId()), pi.getInPort()});
345 }
346
347 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
348 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800349 else {
350 // We don't know the device so broadcast the request out
351 // the edge of the network
352
353 //Record where the request came from so we know where to send the reply
354 arpRequests.put(target, new ArpRequest(
355 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
356
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800357 sendToOtherNodes(eth, pi);
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800358 }
Jonathan Harta18e4792013-10-31 10:10:54 -0700359
360 /*if (macAddress == null){
Jonathan Hartabad6a52013-09-30 18:17:21 +1300361 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200362
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200363 //Record where the request came from so we know where to send the reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700364 //arpRequests.put(target, new ArpRequest(
365 //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200366
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200367 //Flood the request out edge ports
Jonathan Harta18e4792013-10-31 10:10:54 -0700368 //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200369 }
370 else {
371 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200372 if (log.isTraceEnabled()) {
373 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
374 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300375 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200376 HexString.toHexString(sw.getId()), pi.getInPort()});
377 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200378
Jonathan Hartabad6a52013-09-30 18:17:21 +1300379 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Harta18e4792013-10-31 10:10:54 -0700380 }*/
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200381 }
382
Jonathan Hart1912afc2013-10-11 12:02:44 +1300383 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200384 if (log.isTraceEnabled()) {
385 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
386 inetAddressToString(arp.getSenderProtocolAddress()),
387 HexString.toHexString(arp.getSenderHardwareAddress()),
388 HexString.toHexString(sw.getId()), pi.getInPort()});
389 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200390
Jonathan Hartabad6a52013-09-30 18:17:21 +1300391 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200392 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300393 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200394 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200395 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200396 return;
397 }
398
Jonathan Hartabad6a52013-09-30 18:17:21 +1300399 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
400
401 arpCache.update(senderIpAddress, senderMacAddress);
402
403 //See if anyone's waiting for this ARP reply
404 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200405
406 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200407 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200408 synchronized (arpRequests) {
409 Iterator<ArpRequest> it = requests.iterator();
410 while (it.hasNext()) {
411 ArpRequest request = it.next();
412 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200413 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200414 }
415 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200416
417 //Don't hold an ARP lock while dispatching requests
418 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300419 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200420 }
421 }
422
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200423 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200424 //TODO what should the sender IP address and MAC address be if no
425 //IP addresses are configured? Will there ever be a need to send
426 //ARP requests from the controller in that case?
427 //All-zero MAC address doesn't seem to work - hosts don't respond to it
428
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200429 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
430 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200431 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200432 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200433 (byte)0xff, (byte)0xff, (byte)0xff};
434
435 ARP arpRequest = new ARP();
436
437 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
438 .setProtocolType(ARP.PROTO_TYPE_IP)
439 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200440 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200441 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200442 .setTargetHardwareAddress(zeroMac)
443 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200444
Jonathan Harta8887642013-10-28 13:46:54 -0700445 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200446 //TODO hack for now as it's unclear what the MAC address should be
447 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200448 if (routerMacAddress != null) {
449 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200450 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200451 arpRequest.setSenderHardwareAddress(senderMacAddress);
452
453 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700454 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200455 if (intf != null) {
456 senderIPAddress = intf.getIpAddress().getAddress();
457 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200458
459 arpRequest.setSenderProtocolAddress(senderIPAddress);
460
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200461 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200462 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200463 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200464 .setEtherType(Ethernet.TYPE_ARP)
465 .setPayload(arpRequest);
466
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700467 if (vlan != NO_VLAN) {
468 eth.setVlanID(vlan)
469 .setPriorityCode((byte)0);
470 }
471
Jonathan Hart2f790d22013-08-15 14:01:24 +1200472 sendArpRequestToSwitches(ipAddress, eth.serialize());
473 }
474
475 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200476 sendArpRequestToSwitches(dstAddress, arpRequest,
477 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200478 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200479
Jonathan Hart2f790d22013-08-15 14:01:24 +1200480 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
481 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200482
Jonathan Harta8887642013-10-28 13:46:54 -0700483 if (configService.hasLayer3Configuration()) {
484 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200485 if (intf != null) {
486 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
487 }
488 else {
489 //TODO here it should be broadcast out all non-interface edge ports.
490 //I think we can assume that if it's not a request for an external
491 //network, it's an ARP for a host in our own network. So we want to
492 //send it out all edge ports that don't have an interface configured
493 //to ensure it reaches all hosts in our network.
494 log.debug("No interface found to send ARP request for {}",
495 dstAddress.getHostAddress());
496 }
497 }
498 else {
499 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
500 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200501 }
502
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800503 private void sendToOtherNodes(Ethernet eth, OFPacketIn pi) {
504 ARP arp = (ARP) eth.getPayload();
Jonathan Hartd3003252013-11-15 09:44:46 -0800505
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800506 if (log.isTraceEnabled()) {
507 log.trace("Sending ARP request for {} to other ONOS instances",
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800508 inetAddressToString(arp.getTargetProtocolAddress()));
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800509 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800510
511 InetAddress targetAddress;
512 try {
513 targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress());
514 } catch (UnknownHostException e) {
515 log.error("Unknown host", e);
516 return;
517 }
518
519 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize()));
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800520 }
521
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200522 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200523 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
524 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
525 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
526
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200527 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700528 //I think this means the switch doesn't have any links.
529 //continue;
530 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200531 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200532
Jonathan Harta18e4792013-10-31 10:10:54 -0700533
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200534 OFPacketOut po = new OFPacketOut();
535 po.setInPort(OFPort.OFPP_NONE)
536 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200537 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200538
539 List<OFAction> actions = new ArrayList<OFAction>();
540
541 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200542 if (linkPorts.contains(portNum) ||
543 (sw.getId() == inSwitch && portNum == inPort)){
544 //If this port isn't an edge port or is the ingress port
545 //for the ARP, don't broadcast out it
546 continue;
547 }
548
549 actions.add(new OFActionOutput(portNum));
550 }
551
552 po.setActions(actions);
553 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
554 po.setActionsLength(actionsLength);
555 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200556 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200557
558 List<OFMessage> msgList = new ArrayList<OFMessage>();
559 msgList.add(po);
560
561 try {
562 sw.write(msgList, null);
563 sw.flush();
564 } catch (IOException e) {
565 log.error("Failure writing packet out to switch", e);
566 }
567 }
568 }
569
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800570 private void broadcastArpRequestOutMyEdge(byte[] arpRequest) {
571 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
572
573 OFPacketOut po = new OFPacketOut();
574 po.setInPort(OFPort.OFPP_NONE)
575 .setBufferId(-1)
576 .setPacketData(arpRequest);
577
578 List<OFAction> actions = new ArrayList<OFAction>();
579
580 Iterable<IPortObject> ports
581 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
582 if (ports == null) {
583 continue;
584 }
585
586 for (IPortObject portObject : ports) {
587 if (!portObject.getLinkedPorts().iterator().hasNext()) {
588 actions.add(new OFActionOutput(portObject.getNumber()));
589 }
590 }
591
592 po.setActions(actions);
593 short actionsLength = (short)
594 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
595 po.setActionsLength(actionsLength);
596 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
597 + arpRequest.length);
598
599 try {
600 sw.write(po, null);
601 sw.flush();
602 } catch (IOException e) {
603 log.error("Failure writing packet out to switch", e);
604 }
605 }
606 }
607
Jonathan Hart2f790d22013-08-15 14:01:24 +1200608 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200609 if (log.isTraceEnabled()) {
610 log.trace("Sending ARP request out {}/{}",
611 HexString.toHexString(dpid), port);
612 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200613
614 OFPacketOut po = new OFPacketOut();
615 po.setInPort(OFPort.OFPP_NONE)
616 .setBufferId(-1)
617 .setPacketData(arpRequest);
618
619 List<OFAction> actions = new ArrayList<OFAction>();
620 actions.add(new OFActionOutput(port));
621 po.setActions(actions);
622 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
623 po.setActionsLength(actionsLength);
624 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
625 + arpRequest.length);
626
627 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
628
629 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200630 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200631 return;
632 }
633
634 try {
635 sw.write(po, null);
636 sw.flush();
637 } catch (IOException e) {
638 log.error("Failure writing packet out to switch", e);
639 }
640 }
641
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300642 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200643 if (log.isTraceEnabled()) {
644 log.trace("Sending reply {} => {} to {}", new Object[] {
645 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300646 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200647 inetAddressToString(arpRequest.getSenderProtocolAddress())});
648 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200649
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200650 ARP arpReply = new ARP();
651 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
652 .setProtocolType(ARP.PROTO_TYPE_IP)
653 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200654 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200655 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300656 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200657 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
658 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
659 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
660
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700661
662
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200663 Ethernet eth = new Ethernet();
664 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300665 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200666 .setEtherType(Ethernet.TYPE_ARP)
667 .setPayload(arpReply);
668
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700669 if (vlan != NO_VLAN) {
670 eth.setVlanID(vlan)
671 .setPriorityCode((byte)0);
672 }
673
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200674 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200675 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200676
677 OFPacketOut po = new OFPacketOut();
678 po.setInPort(OFPort.OFPP_NONE)
679 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200680 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200681 .setActions(actions)
682 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
683 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
684 + po.getPacketData().length);
685
686 List<OFMessage> msgList = new ArrayList<OFMessage>();
687 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200688
689 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
690
691 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200692 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200693 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200694 return;
695 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200696
697 try {
698 sw.write(msgList, null);
699 sw.flush();
700 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200701 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200702 }
703 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300704
705 private String inetAddressToString(byte[] bytes) {
706 try {
707 return InetAddress.getByAddress(bytes).getHostAddress();
708 } catch (UnknownHostException e) {
709 log.debug("Invalid IP address", e);
710 return "";
711 }
712 }
713
714 /*
715 * IProxyArpService methods
716 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200717
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200718 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300719 public MACAddress getMacAddress(InetAddress ipAddress) {
720 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200721 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200722
723 @Override
724 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
725 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200726 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200727
Jonathan Hart6e618212013-08-21 22:28:43 +1200728 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700729 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200730 sendArpRequestForAddress(ipAddress);
731 }
732 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300733
734 @Override
735 public List<String> getMappings() {
736 return arpCache.getMappings();
737 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800738
739 /*
740 * IArpEventHandler methods
741 */
742
743 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800744 public void arpRequestNotification(ArpMessage arpMessage) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800745 log.debug("Received ARP notification from other instances");
Jonathan Hartd3003252013-11-15 09:44:46 -0800746
747 switch (arpMessage.getType()){
748 case REQUEST:
749 broadcastArpRequestOutMyEdge(arpMessage.getPacket());
750 break;
751 case REPLY:
752 sendArpReplyToWaitingRequesters(arpMessage.getAddress());
753 break;
754 }
755 }
756
757 private void sendArpReplyToWaitingRequesters(InetAddress address) {
758 log.debug("Sending ARP reply for {} to requesters",
759 address.getHostAddress());
760
761 //See if anyone's waiting for this ARP reply
762 Set<ArpRequest> requests = arpRequests.get(address);
763
764 //Synchronize on the Multimap while using an iterator for one of the sets
765 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
766 synchronized (arpRequests) {
767 Iterator<ArpRequest> it = requests.iterator();
768 while (it.hasNext()) {
769 ArpRequest request = it.next();
770 it.remove();
771 requestsToSend.add(request);
772 }
773 }
774
775 IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
776 InetAddresses.coerceToInteger(address));
777
778 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
779
780 log.debug("Found {} at {} in network map",
781 address.getHostAddress(), mac);
782
783 //Don't hold an ARP lock while dispatching requests
784 for (ArpRequest request : requestsToSend) {
785 request.dispatchReply(address, mac);
786 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800787 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200788}