blob: 0b467780b7311e645fecf8a85b2b2f4eb4c82382 [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
Jonathan Hart1912afc2013-10-11 12:02:44 +1300378 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200379 if (log.isTraceEnabled()) {
380 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
381 inetAddressToString(arp.getSenderProtocolAddress()),
382 HexString.toHexString(arp.getSenderHardwareAddress()),
383 HexString.toHexString(sw.getId()), pi.getInPort()});
384 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200385
Jonathan Hartabad6a52013-09-30 18:17:21 +1300386 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200387 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300388 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200389 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200390 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200391 return;
392 }
393
Jonathan Hartabad6a52013-09-30 18:17:21 +1300394 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
395
396 arpCache.update(senderIpAddress, senderMacAddress);
397
398 //See if anyone's waiting for this ARP reply
399 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200400
401 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200402 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200403 synchronized (arpRequests) {
404 Iterator<ArpRequest> it = requests.iterator();
405 while (it.hasNext()) {
406 ArpRequest request = it.next();
407 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200408 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200409 }
410 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200411
412 //Don't hold an ARP lock while dispatching requests
413 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300414 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200415 }
416 }
417
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200418 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200419 //TODO what should the sender IP address and MAC address be if no
420 //IP addresses are configured? Will there ever be a need to send
421 //ARP requests from the controller in that case?
422 //All-zero MAC address doesn't seem to work - hosts don't respond to it
423
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200424 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
425 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200426 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200427 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200428 (byte)0xff, (byte)0xff, (byte)0xff};
429
430 ARP arpRequest = new ARP();
431
432 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
433 .setProtocolType(ARP.PROTO_TYPE_IP)
434 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200435 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200436 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200437 .setTargetHardwareAddress(zeroMac)
438 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200439
Jonathan Harta8887642013-10-28 13:46:54 -0700440 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200441 //TODO hack for now as it's unclear what the MAC address should be
442 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200443 if (routerMacAddress != null) {
444 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200445 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200446 arpRequest.setSenderHardwareAddress(senderMacAddress);
447
448 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700449 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200450 if (intf != null) {
451 senderIPAddress = intf.getIpAddress().getAddress();
452 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200453
454 arpRequest.setSenderProtocolAddress(senderIPAddress);
455
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200456 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200457 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200458 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200459 .setEtherType(Ethernet.TYPE_ARP)
460 .setPayload(arpRequest);
461
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700462 if (vlan != NO_VLAN) {
463 eth.setVlanID(vlan)
464 .setPriorityCode((byte)0);
465 }
466
Jonathan Hart2f790d22013-08-15 14:01:24 +1200467 sendArpRequestToSwitches(ipAddress, eth.serialize());
468 }
469
470 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200471 sendArpRequestToSwitches(dstAddress, arpRequest,
472 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200473 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200474
Jonathan Hart2f790d22013-08-15 14:01:24 +1200475 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
476 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200477
Jonathan Harta8887642013-10-28 13:46:54 -0700478 if (configService.hasLayer3Configuration()) {
479 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200480 if (intf != null) {
481 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
482 }
483 else {
484 //TODO here it should be broadcast out all non-interface edge ports.
485 //I think we can assume that if it's not a request for an external
486 //network, it's an ARP for a host in our own network. So we want to
487 //send it out all edge ports that don't have an interface configured
488 //to ensure it reaches all hosts in our network.
489 log.debug("No interface found to send ARP request for {}",
490 dstAddress.getHostAddress());
491 }
492 }
493 else {
494 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
495 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200496 }
497
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800498 private void sendToOtherNodes(Ethernet eth, OFPacketIn pi) {
499 ARP arp = (ARP) eth.getPayload();
Jonathan Hartd3003252013-11-15 09:44:46 -0800500
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800501 if (log.isTraceEnabled()) {
502 log.trace("Sending ARP request for {} to other ONOS instances",
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800503 inetAddressToString(arp.getTargetProtocolAddress()));
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800504 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800505
506 InetAddress targetAddress;
507 try {
508 targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress());
509 } catch (UnknownHostException e) {
510 log.error("Unknown host", e);
511 return;
512 }
513
514 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize()));
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800515 }
516
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200517 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200518 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
519 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
520 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
521
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200522 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700523 //I think this means the switch doesn't have any links.
524 //continue;
525 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200526 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200527
Jonathan Harta18e4792013-10-31 10:10:54 -0700528
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200529 OFPacketOut po = new OFPacketOut();
530 po.setInPort(OFPort.OFPP_NONE)
531 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200532 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200533
534 List<OFAction> actions = new ArrayList<OFAction>();
535
536 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200537 if (linkPorts.contains(portNum) ||
538 (sw.getId() == inSwitch && portNum == inPort)){
539 //If this port isn't an edge port or is the ingress port
540 //for the ARP, don't broadcast out it
541 continue;
542 }
543
544 actions.add(new OFActionOutput(portNum));
545 }
546
547 po.setActions(actions);
548 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
549 po.setActionsLength(actionsLength);
550 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200551 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200552
553 List<OFMessage> msgList = new ArrayList<OFMessage>();
554 msgList.add(po);
555
556 try {
557 sw.write(msgList, null);
558 sw.flush();
559 } catch (IOException e) {
560 log.error("Failure writing packet out to switch", e);
561 }
562 }
563 }
564
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800565 private void broadcastArpRequestOutMyEdge(byte[] arpRequest) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800566 List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
567
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800568 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
569
570 OFPacketOut po = new OFPacketOut();
571 po.setInPort(OFPort.OFPP_NONE)
572 .setBufferId(-1)
573 .setPacketData(arpRequest);
574
575 List<OFAction> actions = new ArrayList<OFAction>();
576
577 Iterable<IPortObject> ports
578 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
579 if (ports == null) {
580 continue;
581 }
582
583 for (IPortObject portObject : ports) {
584 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800585 switchPorts.add(new SwitchPort(new Dpid(sw.getId()),
586 new Port(portObject.getNumber())));
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800587 actions.add(new OFActionOutput(portObject.getNumber()));
588 }
589 }
590
591 po.setActions(actions);
592 short actionsLength = (short)
593 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
594 po.setActionsLength(actionsLength);
595 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
596 + arpRequest.length);
597
598 try {
599 sw.write(po, null);
600 sw.flush();
601 } catch (IOException e) {
602 log.error("Failure writing packet out to switch", e);
603 }
604 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800605
606 log.debug("Broadcast ARP request for to: {}", switchPorts);
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800607 }
608
Jonathan Hart2f790d22013-08-15 14:01:24 +1200609 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200610 if (log.isTraceEnabled()) {
611 log.trace("Sending ARP request out {}/{}",
612 HexString.toHexString(dpid), port);
613 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200614
615 OFPacketOut po = new OFPacketOut();
616 po.setInPort(OFPort.OFPP_NONE)
617 .setBufferId(-1)
618 .setPacketData(arpRequest);
619
620 List<OFAction> actions = new ArrayList<OFAction>();
621 actions.add(new OFActionOutput(port));
622 po.setActions(actions);
623 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
624 po.setActionsLength(actionsLength);
625 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
626 + arpRequest.length);
627
628 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
629
630 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200631 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200632 return;
633 }
634
635 try {
636 sw.write(po, null);
637 sw.flush();
638 } catch (IOException e) {
639 log.error("Failure writing packet out to switch", e);
640 }
641 }
642
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300643 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200644 if (log.isTraceEnabled()) {
645 log.trace("Sending reply {} => {} to {}", new Object[] {
646 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300647 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200648 inetAddressToString(arpRequest.getSenderProtocolAddress())});
649 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200650
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200651 ARP arpReply = new ARP();
652 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
653 .setProtocolType(ARP.PROTO_TYPE_IP)
654 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200655 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200656 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300657 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200658 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
659 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
660 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
661
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700662
663
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200664 Ethernet eth = new Ethernet();
665 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300666 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200667 .setEtherType(Ethernet.TYPE_ARP)
668 .setPayload(arpReply);
669
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700670 if (vlan != NO_VLAN) {
671 eth.setVlanID(vlan)
672 .setPriorityCode((byte)0);
673 }
674
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200675 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200676 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200677
678 OFPacketOut po = new OFPacketOut();
679 po.setInPort(OFPort.OFPP_NONE)
680 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200681 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200682 .setActions(actions)
683 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
684 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
685 + po.getPacketData().length);
686
687 List<OFMessage> msgList = new ArrayList<OFMessage>();
688 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200689
690 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
691
692 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200693 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200694 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200695 return;
696 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200697
698 try {
699 sw.write(msgList, null);
700 sw.flush();
701 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200702 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200703 }
704 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300705
706 private String inetAddressToString(byte[] bytes) {
707 try {
708 return InetAddress.getByAddress(bytes).getHostAddress();
709 } catch (UnknownHostException e) {
710 log.debug("Invalid IP address", e);
711 return "";
712 }
713 }
714
715 /*
716 * IProxyArpService methods
717 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200718
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200719 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300720 public MACAddress getMacAddress(InetAddress ipAddress) {
721 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200722 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200723
724 @Override
725 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
726 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200727 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200728
Jonathan Hart6e618212013-08-21 22:28:43 +1200729 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700730 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200731 sendArpRequestForAddress(ipAddress);
732 }
733 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300734
735 @Override
736 public List<String> getMappings() {
737 return arpCache.getMappings();
738 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800739
740 /*
741 * IArpEventHandler methods
742 */
743
744 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800745 public void arpRequestNotification(ArpMessage arpMessage) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800746 //log.debug("Received ARP notification from other instances");
Jonathan Hartd3003252013-11-15 09:44:46 -0800747
748 switch (arpMessage.getType()){
749 case REQUEST:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800750 log.debug("Received ARP request notification for {}",
751 arpMessage.getAddress());
Jonathan Hartd3003252013-11-15 09:44:46 -0800752 broadcastArpRequestOutMyEdge(arpMessage.getPacket());
753 break;
754 case REPLY:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800755 log.debug("Received ARP reply notification for {}",
756 arpMessage.getAddress());
Jonathan Hartd3003252013-11-15 09:44:46 -0800757 sendArpReplyToWaitingRequesters(arpMessage.getAddress());
758 break;
759 }
760 }
761
762 private void sendArpReplyToWaitingRequesters(InetAddress address) {
763 log.debug("Sending ARP reply for {} to requesters",
764 address.getHostAddress());
765
766 //See if anyone's waiting for this ARP reply
767 Set<ArpRequest> requests = arpRequests.get(address);
768
769 //Synchronize on the Multimap while using an iterator for one of the sets
770 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
771 synchronized (arpRequests) {
772 Iterator<ArpRequest> it = requests.iterator();
773 while (it.hasNext()) {
774 ArpRequest request = it.next();
775 it.remove();
776 requestsToSend.add(request);
777 }
778 }
779
780 IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
781 InetAddresses.coerceToInteger(address));
782
783 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
784
785 log.debug("Found {} at {} in network map",
786 address.getHostAddress(), mac);
787
788 //Don't hold an ARP lock while dispatching requests
789 for (ArpRequest request : requestsToSend) {
790 request.dispatchReply(address, mac);
791 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800792 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200793}