blob: 415d697d59b9262e1d6497c0b4aa4ca352c8c60d [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 Hartba9ced92013-11-24 16:52:13 -080039import net.onrc.onos.ofcontroller.util.Dpid;
40import net.onrc.onos.ofcontroller.util.Port;
41import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120042
43import org.openflow.protocol.OFMessage;
44import org.openflow.protocol.OFPacketIn;
45import org.openflow.protocol.OFPacketOut;
46import org.openflow.protocol.OFPort;
47import org.openflow.protocol.OFType;
48import org.openflow.protocol.action.OFAction;
49import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120050import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120051import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
Jonathan Hart4dfc3652013-08-02 20:22:36 +120054import com.google.common.collect.HashMultimap;
55import com.google.common.collect.Multimaps;
56import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070057import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120058
Jonathan Hart18ad55c2013-11-11 22:49:55 -080059public class ProxyArpManager implements IProxyArpService, IOFMessageListener,
Jonathan Harte93aed42013-12-05 18:39:50 -080060 IArpEventHandler, IFloodlightModule {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120061 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120062
Jonathan Hartdf6ec332013-08-04 01:37:14 +120063 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130064
65 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120066
Jonathan Harta8887642013-10-28 13:46:54 -070067 private IFloodlightProviderService floodlightProvider;
68 private ITopologyService topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080069 private IDatagridService datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -070070 private IConfigInfoService configService;
71 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120072
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070073 private IDeviceStorage deviceStorage;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080074 private volatile ITopoSwitchService topoSwitchService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070075
Jonathan Hart1cf9de02013-10-21 17:42:29 -070076 private short vlan;
77 private static final short NO_VLAN = 0;
78
Jonathan Harta8887642013-10-28 13:46:54 -070079 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120080
Jonathan Harta8887642013-10-28 13:46:54 -070081 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120082
Jonathan Hartabad6a52013-09-30 18:17:21 +130083 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120084 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130085 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120086 private long requestTime;
87
Jonathan Hart4dfc3652013-08-02 20:22:36 +120088 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120089 this.requester = requester;
90 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120091 this.requestTime = System.currentTimeMillis();
92 }
93
Jonathan Hart4dfc3652013-08-02 20:22:36 +120094 public ArpRequest(ArpRequest old) {
95 this.requester = old.requester;
96 this.retry = old.retry;
97 this.requestTime = System.currentTimeMillis();
98 }
99
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200100 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300101 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200102 }
103
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200104 public boolean shouldRetry() {
105 return retry;
106 }
107
Jonathan Hartabad6a52013-09-30 18:17:21 +1300108 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200109 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200110 }
111 }
112
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300113 private class HostArpRequester implements IArpRequester {
114 private final ARP arpRequest;
115 private final long dpid;
116 private final short port;
117
118 public HostArpRequester(ARP arpRequest, long dpid, short port) {
119 this.arpRequest = arpRequest;
120 this.dpid = dpid;
121 this.port = port;
122 }
123
124 @Override
125 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
126 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
127 }
128 }
129
Jonathan Harte93aed42013-12-05 18:39:50 -0800130 @Override
131 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
132 Collection<Class<? extends IFloodlightService>> l
133 = new ArrayList<Class<? extends IFloodlightService>>();
134 l.add(IProxyArpService.class);
135 return l;
Jonathan Harta8887642013-10-28 13:46:54 -0700136 }
Jonathan Harte93aed42013-12-05 18:39:50 -0800137
138 @Override
139 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
140 Map<Class<? extends IFloodlightService>, IFloodlightService> m
141 = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
142 m.put(IProxyArpService.class, this);
143 return m;
144 }
145
146 @Override
147 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
148 Collection<Class<? extends IFloodlightService>> dependencies
149 = new ArrayList<Class<? extends IFloodlightService>>();
150 dependencies.add(IFloodlightProviderService.class);
151 dependencies.add(ITopologyService.class);
152 dependencies.add(IRestApiService.class);
153 dependencies.add(IDatagridService.class);
154 dependencies.add(IConfigInfoService.class);
155 return dependencies;
156 }
Jonathan Harta8887642013-10-28 13:46:54 -0700157
Jonathan Harte93aed42013-12-05 18:39:50 -0800158 @Override
159 public void init(FloodlightModuleContext context){
160 this.floodlightProvider =
161 context.getServiceImpl(IFloodlightProviderService.class);
162 this.topology = context.getServiceImpl(ITopologyService.class);
163 this.datagrid = context.getServiceImpl(IDatagridService.class);
164 this.configService = context.getServiceImpl(IConfigInfoService.class);
165 this.restApi = context.getServiceImpl(IRestApiService.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200166
Jonathan Hartabad6a52013-09-30 18:17:21 +1300167 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200168
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200169 arpRequests = Multimaps.synchronizedSetMultimap(
170 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800171
172 topoSwitchService = new TopoSwitchServiceImpl();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200173 }
174
Jonathan Harte93aed42013-12-05 18:39:50 -0800175 @Override
176 public void startUp(FloodlightModuleContext context) {
Jonathan Harta8887642013-10-28 13:46:54 -0700177 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700178 log.info("vlan set to {}", this.vlan);
179
Jonathan Hart5afde492013-10-01 12:30:53 +1300180 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700181 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300182
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800183 datagrid.registerArpEventHandler(this);
184
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700185 deviceStorage = new DeviceStorageImpl();
186 deviceStorage.init("");
187
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200188 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200189 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200190 @Override
191 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200192 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200193 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200194 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200195 }
196
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200197 /*
198 * Function that runs periodically to manage the asynchronous request mechanism.
199 * It basically cleans up old ARP requests if we don't get a response for them.
200 * The caller can designate that a request should be retried indefinitely, and
201 * this task will handle that as well.
202 */
203 private void doPeriodicArpProcessing() {
204 SetMultimap<InetAddress, ArpRequest> retryList
205 = HashMultimap.<InetAddress, ArpRequest>create();
206
207 //Have to synchronize externally on the Multimap while using an iterator,
208 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200209 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200210 log.debug("Current have {} outstanding requests",
211 arpRequests.size());
212
213 Iterator<Map.Entry<InetAddress, ArpRequest>> it
214 = arpRequests.entries().iterator();
215
216 while (it.hasNext()) {
217 Map.Entry<InetAddress, ArpRequest> entry
218 = it.next();
219 ArpRequest request = entry.getValue();
220 if (request.isExpired()) {
221 log.debug("Cleaning expired ARP request for {}",
222 entry.getKey().getHostAddress());
223
224 it.remove();
225
226 if (request.shouldRetry()) {
227 retryList.put(entry.getKey(), request);
228 }
229 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200230 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200231 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200232
233 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
234 : retryList.asMap().entrySet()) {
235
236 InetAddress address = entry.getKey();
237
238 log.debug("Resending ARP request for {}", address.getHostAddress());
239
240 sendArpRequestForAddress(address);
241
242 for (ArpRequest request : entry.getValue()) {
243 arpRequests.put(address, new ArpRequest(request));
244 }
245 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200246 }
247
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200248 @Override
249 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700250 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200251 }
252
253 @Override
254 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700255 if (type == OFType.PACKET_IN) {
256 return "devicemanager".equals(name);
257 }
258 else {
259 return false;
260 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200261 }
262
263 @Override
264 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200265 return false;
266 }
267
268 @Override
269 public Command receive(
270 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
271
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200272 OFPacketIn pi = (OFPacketIn) msg;
273
274 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
275 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
276
277 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800278 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200279 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700280 //TODO check what the DeviceManager does about propagating
281 //or swallowing ARPs. We want to go after DeviceManager in the
282 //chain but we really need it to CONTINUE ARP packets so we can
283 //get them.
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800284 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200285 }
286 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700287 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200288 }
289 }
290
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200291 //TODO should we propagate ARP or swallow it?
292 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200293 return Command.CONTINUE;
294 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200295
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800296 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200297 if (log.isTraceEnabled()) {
298 log.trace("ARP request received for {}",
299 inetAddressToString(arp.getTargetProtocolAddress()));
300 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200301
302 InetAddress target;
303 try {
304 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
305 } catch (UnknownHostException e) {
306 log.debug("Invalid address in ARP request", e);
307 return;
308 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200309
Jonathan Harta8887642013-10-28 13:46:54 -0700310 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200311 //If the request came from outside our network, we only care if
312 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700313 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200314 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700315 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200316
317 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700318 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200319 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200320
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200321 return;
322 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200323
Jonathan Harta18e4792013-10-31 10:10:54 -0700324 //MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200325
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700326 IDeviceObject targetDevice =
327 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
Jonathan Harta18e4792013-10-31 10:10:54 -0700328
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800329 log.debug("targetDevice: {}", targetDevice);
330
Jonathan Harta18e4792013-10-31 10:10:54 -0700331 if (targetDevice != null) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800332 // We have the device in our database, so send a reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700333 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
334
335 if (log.isTraceEnabled()) {
336 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
337 inetAddressToString(arp.getTargetProtocolAddress()),
338 macAddress.toString(),
339 HexString.toHexString(sw.getId()), pi.getInPort()});
340 }
341
342 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
343 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800344 else {
345 // We don't know the device so broadcast the request out
346 // the edge of the network
347
348 //Record where the request came from so we know where to send the reply
349 arpRequests.put(target, new ArpRequest(
350 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
351
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800352 sendToOtherNodes(eth, pi);
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800353 }
Jonathan Harta18e4792013-10-31 10:10:54 -0700354
355 /*if (macAddress == null){
Jonathan Hartabad6a52013-09-30 18:17:21 +1300356 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200357
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200358 //Record where the request came from so we know where to send the reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700359 //arpRequests.put(target, new ArpRequest(
360 //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200361
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200362 //Flood the request out edge ports
Jonathan Harta18e4792013-10-31 10:10:54 -0700363 //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200364 }
365 else {
366 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200367 if (log.isTraceEnabled()) {
368 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
369 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300370 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200371 HexString.toHexString(sw.getId()), pi.getInPort()});
372 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200373
Jonathan Hartabad6a52013-09-30 18:17:21 +1300374 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Harta18e4792013-10-31 10:10:54 -0700375 }*/
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200376 }
377
Naoki Shiotae2f4da72013-12-09 16:34:17 -0800378 @SuppressWarnings("unused")
Jonathan Hart1912afc2013-10-11 12:02:44 +1300379 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200380 if (log.isTraceEnabled()) {
381 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
382 inetAddressToString(arp.getSenderProtocolAddress()),
383 HexString.toHexString(arp.getSenderHardwareAddress()),
384 HexString.toHexString(sw.getId()), pi.getInPort()});
385 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200386
Jonathan Hartabad6a52013-09-30 18:17:21 +1300387 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200388 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300389 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200390 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200391 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200392 return;
393 }
394
Jonathan Hartabad6a52013-09-30 18:17:21 +1300395 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
396
397 arpCache.update(senderIpAddress, senderMacAddress);
398
399 //See if anyone's waiting for this ARP reply
400 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200401
402 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200403 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200404 synchronized (arpRequests) {
405 Iterator<ArpRequest> it = requests.iterator();
406 while (it.hasNext()) {
407 ArpRequest request = it.next();
408 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200409 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200410 }
411 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200412
413 //Don't hold an ARP lock while dispatching requests
414 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300415 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200416 }
417 }
418
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200419 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200420 //TODO what should the sender IP address and MAC address be if no
421 //IP addresses are configured? Will there ever be a need to send
422 //ARP requests from the controller in that case?
423 //All-zero MAC address doesn't seem to work - hosts don't respond to it
424
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200425 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
426 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200427 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200428 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200429 (byte)0xff, (byte)0xff, (byte)0xff};
430
431 ARP arpRequest = new ARP();
432
433 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
434 .setProtocolType(ARP.PROTO_TYPE_IP)
435 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200436 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200437 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200438 .setTargetHardwareAddress(zeroMac)
439 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200440
Jonathan Harta8887642013-10-28 13:46:54 -0700441 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200442 //TODO hack for now as it's unclear what the MAC address should be
443 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200444 if (routerMacAddress != null) {
445 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200446 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200447 arpRequest.setSenderHardwareAddress(senderMacAddress);
448
449 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700450 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200451 if (intf != null) {
452 senderIPAddress = intf.getIpAddress().getAddress();
453 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200454
455 arpRequest.setSenderProtocolAddress(senderIPAddress);
456
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200457 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200458 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200459 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200460 .setEtherType(Ethernet.TYPE_ARP)
461 .setPayload(arpRequest);
462
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700463 if (vlan != NO_VLAN) {
464 eth.setVlanID(vlan)
465 .setPriorityCode((byte)0);
466 }
467
Jonathan Hart2f790d22013-08-15 14:01:24 +1200468 sendArpRequestToSwitches(ipAddress, eth.serialize());
469 }
470
471 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200472 sendArpRequestToSwitches(dstAddress, arpRequest,
473 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200474 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200475
Jonathan Hart2f790d22013-08-15 14:01:24 +1200476 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
477 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200478
Jonathan Harta8887642013-10-28 13:46:54 -0700479 if (configService.hasLayer3Configuration()) {
480 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200481 if (intf != null) {
482 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
483 }
484 else {
485 //TODO here it should be broadcast out all non-interface edge ports.
486 //I think we can assume that if it's not a request for an external
487 //network, it's an ARP for a host in our own network. So we want to
488 //send it out all edge ports that don't have an interface configured
489 //to ensure it reaches all hosts in our network.
490 log.debug("No interface found to send ARP request for {}",
491 dstAddress.getHostAddress());
492 }
493 }
494 else {
495 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
496 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200497 }
498
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800499 private void sendToOtherNodes(Ethernet eth, OFPacketIn pi) {
500 ARP arp = (ARP) eth.getPayload();
Jonathan Hartd3003252013-11-15 09:44:46 -0800501
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800502 if (log.isTraceEnabled()) {
503 log.trace("Sending ARP request for {} to other ONOS instances",
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800504 inetAddressToString(arp.getTargetProtocolAddress()));
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800505 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800506
507 InetAddress targetAddress;
508 try {
509 targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress());
510 } catch (UnknownHostException e) {
511 log.error("Unknown host", e);
512 return;
513 }
514
515 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize()));
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800516 }
517
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200518 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200519 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
520 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
521 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
522
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200523 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700524 //I think this means the switch doesn't have any links.
525 //continue;
526 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200527 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200528
Jonathan Harta18e4792013-10-31 10:10:54 -0700529
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200530 OFPacketOut po = new OFPacketOut();
531 po.setInPort(OFPort.OFPP_NONE)
532 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200533 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200534
535 List<OFAction> actions = new ArrayList<OFAction>();
536
537 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200538 if (linkPorts.contains(portNum) ||
539 (sw.getId() == inSwitch && portNum == inPort)){
540 //If this port isn't an edge port or is the ingress port
541 //for the ARP, don't broadcast out it
542 continue;
543 }
544
545 actions.add(new OFActionOutput(portNum));
546 }
547
548 po.setActions(actions);
549 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
550 po.setActionsLength(actionsLength);
551 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200552 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200553
554 List<OFMessage> msgList = new ArrayList<OFMessage>();
555 msgList.add(po);
556
557 try {
558 sw.write(msgList, null);
559 sw.flush();
560 } catch (IOException e) {
561 log.error("Failure writing packet out to switch", e);
562 }
563 }
564 }
565
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800566 private void broadcastArpRequestOutMyEdge(byte[] arpRequest) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800567 List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
568
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800569 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
570
571 OFPacketOut po = new OFPacketOut();
572 po.setInPort(OFPort.OFPP_NONE)
573 .setBufferId(-1)
574 .setPacketData(arpRequest);
575
576 List<OFAction> actions = new ArrayList<OFAction>();
577
578 Iterable<IPortObject> ports
579 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
580 if (ports == null) {
581 continue;
582 }
583
584 for (IPortObject portObject : ports) {
585 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800586 switchPorts.add(new SwitchPort(new Dpid(sw.getId()),
587 new Port(portObject.getNumber())));
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800588 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 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800606
607 log.debug("Broadcast ARP request for to: {}", switchPorts);
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800608 }
609
Jonathan Hart2f790d22013-08-15 14:01:24 +1200610 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200611 if (log.isTraceEnabled()) {
612 log.trace("Sending ARP request out {}/{}",
613 HexString.toHexString(dpid), port);
614 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200615
616 OFPacketOut po = new OFPacketOut();
617 po.setInPort(OFPort.OFPP_NONE)
618 .setBufferId(-1)
619 .setPacketData(arpRequest);
620
621 List<OFAction> actions = new ArrayList<OFAction>();
622 actions.add(new OFActionOutput(port));
623 po.setActions(actions);
624 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
625 po.setActionsLength(actionsLength);
626 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
627 + arpRequest.length);
628
629 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
630
631 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200632 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200633 return;
634 }
635
636 try {
637 sw.write(po, null);
638 sw.flush();
639 } catch (IOException e) {
640 log.error("Failure writing packet out to switch", e);
641 }
642 }
643
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300644 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200645 if (log.isTraceEnabled()) {
646 log.trace("Sending reply {} => {} to {}", new Object[] {
647 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300648 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200649 inetAddressToString(arpRequest.getSenderProtocolAddress())});
650 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200651
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200652 ARP arpReply = new ARP();
653 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
654 .setProtocolType(ARP.PROTO_TYPE_IP)
655 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200656 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200657 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300658 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200659 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
660 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
661 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
662
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700663
664
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200665 Ethernet eth = new Ethernet();
666 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300667 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200668 .setEtherType(Ethernet.TYPE_ARP)
669 .setPayload(arpReply);
670
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700671 if (vlan != NO_VLAN) {
672 eth.setVlanID(vlan)
673 .setPriorityCode((byte)0);
674 }
675
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200676 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200677 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200678
679 OFPacketOut po = new OFPacketOut();
680 po.setInPort(OFPort.OFPP_NONE)
681 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200682 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200683 .setActions(actions)
684 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
685 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
686 + po.getPacketData().length);
687
688 List<OFMessage> msgList = new ArrayList<OFMessage>();
689 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200690
691 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
692
693 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200694 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200695 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200696 return;
697 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200698
699 try {
700 sw.write(msgList, null);
701 sw.flush();
702 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200703 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200704 }
705 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300706
707 private String inetAddressToString(byte[] bytes) {
708 try {
709 return InetAddress.getByAddress(bytes).getHostAddress();
710 } catch (UnknownHostException e) {
711 log.debug("Invalid IP address", e);
712 return "";
713 }
714 }
715
716 /*
717 * IProxyArpService methods
718 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200719
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200720 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300721 public MACAddress getMacAddress(InetAddress ipAddress) {
722 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200723 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200724
725 @Override
726 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
727 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200728 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200729
Jonathan Hart6e618212013-08-21 22:28:43 +1200730 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700731 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200732 sendArpRequestForAddress(ipAddress);
733 }
734 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300735
736 @Override
737 public List<String> getMappings() {
738 return arpCache.getMappings();
739 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800740
741 /*
742 * IArpEventHandler methods
743 */
744
745 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800746 public void arpRequestNotification(ArpMessage arpMessage) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800747 //log.debug("Received ARP notification from other instances");
Jonathan Hartd3003252013-11-15 09:44:46 -0800748
749 switch (arpMessage.getType()){
750 case REQUEST:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800751 log.debug("Received ARP request notification for {}",
752 arpMessage.getAddress());
Jonathan Hartd3003252013-11-15 09:44:46 -0800753 broadcastArpRequestOutMyEdge(arpMessage.getPacket());
754 break;
755 case REPLY:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800756 log.debug("Received ARP reply notification for {}",
757 arpMessage.getAddress());
Jonathan Hartd3003252013-11-15 09:44:46 -0800758 sendArpReplyToWaitingRequesters(arpMessage.getAddress());
759 break;
760 }
761 }
762
763 private void sendArpReplyToWaitingRequesters(InetAddress address) {
764 log.debug("Sending ARP reply for {} to requesters",
765 address.getHostAddress());
766
767 //See if anyone's waiting for this ARP reply
768 Set<ArpRequest> requests = arpRequests.get(address);
769
770 //Synchronize on the Multimap while using an iterator for one of the sets
771 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
772 synchronized (arpRequests) {
773 Iterator<ArpRequest> it = requests.iterator();
774 while (it.hasNext()) {
775 ArpRequest request = it.next();
776 it.remove();
777 requestsToSend.add(request);
778 }
779 }
780
781 IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
782 InetAddresses.coerceToInteger(address));
783
784 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
785
786 log.debug("Found {} at {} in network map",
787 address.getHostAddress(), mac);
788
789 //Don't hold an ARP lock while dispatching requests
790 for (ArpRequest request : requestsToSend) {
791 request.dispatchReply(address, mac);
792 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800793 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200794}