blob: 3bf6c4e47b9a45eca6f9ff11e66e0fe1800ba1db [file] [log] [blame]
Jonathan Hartc7ca35d2013-06-25 20:54:25 +12001package net.onrc.onos.ofcontroller.proxyarp;
2
3import java.io.IOException;
4import java.net.InetAddress;
5import java.net.UnknownHostException;
6import java.util.ArrayList;
7import java.util.Collection;
Jonathan Harta18e4792013-10-31 10:10:54 -07008import java.util.HashSet;
Jonathan Hart6261dcd2013-07-22 17:58:35 +12009import java.util.Iterator;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120010import java.util.List;
11import java.util.Map;
12import java.util.Set;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120013import java.util.Timer;
14import java.util.TimerTask;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120015
16import net.floodlightcontroller.core.FloodlightContext;
17import net.floodlightcontroller.core.IFloodlightProviderService;
18import net.floodlightcontroller.core.IOFMessageListener;
19import net.floodlightcontroller.core.IOFSwitch;
20import net.floodlightcontroller.packet.ARP;
21import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart08ee8522013-09-22 17:34:43 +120022import net.floodlightcontroller.packet.IPv4;
Jonathan Hart5afde492013-10-01 12:30:53 +130023import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120024import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120025import net.floodlightcontroller.util.MACAddress;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080026import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120027import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070028import net.onrc.onos.ofcontroller.core.IDeviceStorage;
29import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080030import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080031import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService;
32import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoSwitchService;
Jonathan Hartebba1e12013-10-29 11:37:02 -070033import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070034import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080035import net.onrc.onos.ofcontroller.core.internal.TopoLinkServiceImpl;
36import net.onrc.onos.ofcontroller.core.internal.TopoSwitchServiceImpl;
Jonathan Hartba9ced92013-11-24 16:52:13 -080037import net.onrc.onos.ofcontroller.util.Dpid;
38import net.onrc.onos.ofcontroller.util.Port;
39import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120040
41import org.openflow.protocol.OFMessage;
42import org.openflow.protocol.OFPacketIn;
43import org.openflow.protocol.OFPacketOut;
44import org.openflow.protocol.OFPort;
45import org.openflow.protocol.OFType;
46import org.openflow.protocol.action.OFAction;
47import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120048import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120049import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
51
Jonathan Hart4dfc3652013-08-02 20:22:36 +120052import com.google.common.collect.HashMultimap;
53import com.google.common.collect.Multimaps;
54import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070055import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120056
Jonathan Hart18ad55c2013-11-11 22:49:55 -080057public class ProxyArpManager implements IProxyArpService, IOFMessageListener,
58 IArpEventHandler {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120059 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120060
Jonathan Hartdf6ec332013-08-04 01:37:14 +120061 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130062
63 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120064
Jonathan Harta8887642013-10-28 13:46:54 -070065 private IFloodlightProviderService floodlightProvider;
66 private ITopologyService topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080067 private IDatagridService datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -070068 private IConfigInfoService configService;
69 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120070
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070071 private IDeviceStorage deviceStorage;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080072 private volatile ITopoSwitchService topoSwitchService;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080073 private ITopoLinkService topoLinkService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070074
Jonathan Hart1cf9de02013-10-21 17:42:29 -070075 private short vlan;
76 private static final short NO_VLAN = 0;
77
Jonathan Harta8887642013-10-28 13:46:54 -070078 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120079
Jonathan Harta8887642013-10-28 13:46:54 -070080 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120081
Jonathan Hartabad6a52013-09-30 18:17:21 +130082 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120083 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130084 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120085 private long requestTime;
86
Jonathan Hart4dfc3652013-08-02 20:22:36 +120087 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120088 this.requester = requester;
89 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120090 this.requestTime = System.currentTimeMillis();
91 }
92
Jonathan Hart4dfc3652013-08-02 20:22:36 +120093 public ArpRequest(ArpRequest old) {
94 this.requester = old.requester;
95 this.retry = old.retry;
96 this.requestTime = System.currentTimeMillis();
97 }
98
Jonathan Hart4dfc3652013-08-02 20:22:36 +120099 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300100 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200101 }
102
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200103 public boolean shouldRetry() {
104 return retry;
105 }
106
Jonathan Hartabad6a52013-09-30 18:17:21 +1300107 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200108 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200109 }
110 }
111
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300112 private class HostArpRequester implements IArpRequester {
113 private final ARP arpRequest;
114 private final long dpid;
115 private final short port;
116
117 public HostArpRequester(ARP arpRequest, long dpid, short port) {
118 this.arpRequest = arpRequest;
119 this.dpid = dpid;
120 this.port = port;
121 }
122
123 @Override
124 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
125 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
126 }
127 }
128
Jonathan Harta8887642013-10-28 13:46:54 -0700129 /*
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200130 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Harta8887642013-10-28 13:46:54 -0700131 ITopologyService topology, IConfigInfoService configService,
Jonathan Hart5afde492013-10-01 12:30:53 +1300132 IRestApiService restApi){
Jonathan Harta8887642013-10-28 13:46:54 -0700133
134 }
135 */
136
137 public void init(IFloodlightProviderService floodlightProvider,
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800138 ITopologyService topology, IDatagridService datagrid,
Jonathan Harta18e4792013-10-31 10:10:54 -0700139 IConfigInfoService config, IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200140 this.floodlightProvider = floodlightProvider;
141 this.topology = topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800142 this.datagrid = datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -0700143 this.configService = config;
Jonathan Hart5afde492013-10-01 12:30:53 +1300144 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200145
Jonathan Hartabad6a52013-09-30 18:17:21 +1300146 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200147
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200148 arpRequests = Multimaps.synchronizedSetMultimap(
149 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800150
151 topoSwitchService = new TopoSwitchServiceImpl();
152 topoLinkService = new TopoLinkServiceImpl();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200153 }
154
Jonathan Harta8887642013-10-28 13:46:54 -0700155 public void startUp() {
156 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700157 log.info("vlan set to {}", this.vlan);
158
Jonathan Hart5afde492013-10-01 12:30:53 +1300159 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700160 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300161
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800162 datagrid.registerArpEventHandler(this);
163
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700164 deviceStorage = new DeviceStorageImpl();
165 deviceStorage.init("");
166
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200167 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200168 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200169 @Override
170 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200171 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200172 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200173 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200174 }
175
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200176 /*
177 * Function that runs periodically to manage the asynchronous request mechanism.
178 * It basically cleans up old ARP requests if we don't get a response for them.
179 * The caller can designate that a request should be retried indefinitely, and
180 * this task will handle that as well.
181 */
182 private void doPeriodicArpProcessing() {
183 SetMultimap<InetAddress, ArpRequest> retryList
184 = HashMultimap.<InetAddress, ArpRequest>create();
185
186 //Have to synchronize externally on the Multimap while using an iterator,
187 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200188 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200189 log.debug("Current have {} outstanding requests",
190 arpRequests.size());
191
192 Iterator<Map.Entry<InetAddress, ArpRequest>> it
193 = arpRequests.entries().iterator();
194
195 while (it.hasNext()) {
196 Map.Entry<InetAddress, ArpRequest> entry
197 = it.next();
198 ArpRequest request = entry.getValue();
199 if (request.isExpired()) {
200 log.debug("Cleaning expired ARP request for {}",
201 entry.getKey().getHostAddress());
202
203 it.remove();
204
205 if (request.shouldRetry()) {
206 retryList.put(entry.getKey(), request);
207 }
208 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200209 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200210 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200211
212 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
213 : retryList.asMap().entrySet()) {
214
215 InetAddress address = entry.getKey();
216
217 log.debug("Resending ARP request for {}", address.getHostAddress());
218
219 sendArpRequestForAddress(address);
220
221 for (ArpRequest request : entry.getValue()) {
222 arpRequests.put(address, new ArpRequest(request));
223 }
224 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200225 }
226
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200227 @Override
228 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700229 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200230 }
231
232 @Override
233 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700234 if (type == OFType.PACKET_IN) {
235 return "devicemanager".equals(name);
236 }
237 else {
238 return false;
239 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200240 }
241
242 @Override
243 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200244 return false;
245 }
246
247 @Override
248 public Command receive(
249 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
250
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200251 OFPacketIn pi = (OFPacketIn) msg;
252
253 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
254 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
255
256 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800257 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200258 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700259 //TODO check what the DeviceManager does about propagating
260 //or swallowing ARPs. We want to go after DeviceManager in the
261 //chain but we really need it to CONTINUE ARP packets so we can
262 //get them.
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800263 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200264 }
265 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700266 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200267 }
268 }
269
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200270 //TODO should we propagate ARP or swallow it?
271 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200272 return Command.CONTINUE;
273 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200274
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800275 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200276 if (log.isTraceEnabled()) {
277 log.trace("ARP request received for {}",
278 inetAddressToString(arp.getTargetProtocolAddress()));
279 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200280
281 InetAddress target;
282 try {
283 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
284 } catch (UnknownHostException e) {
285 log.debug("Invalid address in ARP request", e);
286 return;
287 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200288
Jonathan Harta8887642013-10-28 13:46:54 -0700289 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200290 //If the request came from outside our network, we only care if
291 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700292 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200293 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700294 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200295
296 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700297 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200298 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200299
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200300 return;
301 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200302
Jonathan Harta18e4792013-10-31 10:10:54 -0700303 //MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200304
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700305 IDeviceObject targetDevice =
306 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
Jonathan Harta18e4792013-10-31 10:10:54 -0700307
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800308 log.debug("targetDevice: {}", targetDevice);
309
Jonathan Harta18e4792013-10-31 10:10:54 -0700310 if (targetDevice != null) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800311 // We have the device in our database, so send a reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700312 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
313
314 if (log.isTraceEnabled()) {
315 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
316 inetAddressToString(arp.getTargetProtocolAddress()),
317 macAddress.toString(),
318 HexString.toHexString(sw.getId()), pi.getInPort()});
319 }
320
321 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
322 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800323 else {
324 // We don't know the device so broadcast the request out
325 // the edge of the network
326
327 //Record where the request came from so we know where to send the reply
328 arpRequests.put(target, new ArpRequest(
329 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
330
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800331 sendToOtherNodes(eth, pi);
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800332 }
Jonathan Harta18e4792013-10-31 10:10:54 -0700333
334 /*if (macAddress == null){
Jonathan Hartabad6a52013-09-30 18:17:21 +1300335 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200336
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200337 //Record where the request came from so we know where to send the reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700338 //arpRequests.put(target, new ArpRequest(
339 //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200340
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200341 //Flood the request out edge ports
Jonathan Harta18e4792013-10-31 10:10:54 -0700342 //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200343 }
344 else {
345 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200346 if (log.isTraceEnabled()) {
347 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
348 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300349 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200350 HexString.toHexString(sw.getId()), pi.getInPort()});
351 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200352
Jonathan Hartabad6a52013-09-30 18:17:21 +1300353 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Harta18e4792013-10-31 10:10:54 -0700354 }*/
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200355 }
356
Jonathan Hart1912afc2013-10-11 12:02:44 +1300357 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200358 if (log.isTraceEnabled()) {
359 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
360 inetAddressToString(arp.getSenderProtocolAddress()),
361 HexString.toHexString(arp.getSenderHardwareAddress()),
362 HexString.toHexString(sw.getId()), pi.getInPort()});
363 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200364
Jonathan Hartabad6a52013-09-30 18:17:21 +1300365 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200366 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300367 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200368 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200369 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200370 return;
371 }
372
Jonathan Hartabad6a52013-09-30 18:17:21 +1300373 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
374
375 arpCache.update(senderIpAddress, senderMacAddress);
376
377 //See if anyone's waiting for this ARP reply
378 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200379
380 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200381 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200382 synchronized (arpRequests) {
383 Iterator<ArpRequest> it = requests.iterator();
384 while (it.hasNext()) {
385 ArpRequest request = it.next();
386 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200387 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200388 }
389 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200390
391 //Don't hold an ARP lock while dispatching requests
392 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300393 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200394 }
395 }
396
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200397 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200398 //TODO what should the sender IP address and MAC address be if no
399 //IP addresses are configured? Will there ever be a need to send
400 //ARP requests from the controller in that case?
401 //All-zero MAC address doesn't seem to work - hosts don't respond to it
402
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200403 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
404 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200405 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200406 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200407 (byte)0xff, (byte)0xff, (byte)0xff};
408
409 ARP arpRequest = new ARP();
410
411 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
412 .setProtocolType(ARP.PROTO_TYPE_IP)
413 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200414 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200415 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200416 .setTargetHardwareAddress(zeroMac)
417 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200418
Jonathan Harta8887642013-10-28 13:46:54 -0700419 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200420 //TODO hack for now as it's unclear what the MAC address should be
421 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200422 if (routerMacAddress != null) {
423 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200424 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200425 arpRequest.setSenderHardwareAddress(senderMacAddress);
426
427 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700428 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200429 if (intf != null) {
430 senderIPAddress = intf.getIpAddress().getAddress();
431 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200432
433 arpRequest.setSenderProtocolAddress(senderIPAddress);
434
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200435 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200436 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200437 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200438 .setEtherType(Ethernet.TYPE_ARP)
439 .setPayload(arpRequest);
440
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700441 if (vlan != NO_VLAN) {
442 eth.setVlanID(vlan)
443 .setPriorityCode((byte)0);
444 }
445
Jonathan Hart2f790d22013-08-15 14:01:24 +1200446 sendArpRequestToSwitches(ipAddress, eth.serialize());
447 }
448
449 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200450 sendArpRequestToSwitches(dstAddress, arpRequest,
451 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200452 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200453
Jonathan Hart2f790d22013-08-15 14:01:24 +1200454 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
455 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200456
Jonathan Harta8887642013-10-28 13:46:54 -0700457 if (configService.hasLayer3Configuration()) {
458 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200459 if (intf != null) {
460 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
461 }
462 else {
463 //TODO here it should be broadcast out all non-interface edge ports.
464 //I think we can assume that if it's not a request for an external
465 //network, it's an ARP for a host in our own network. So we want to
466 //send it out all edge ports that don't have an interface configured
467 //to ensure it reaches all hosts in our network.
468 log.debug("No interface found to send ARP request for {}",
469 dstAddress.getHostAddress());
470 }
471 }
472 else {
473 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
474 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200475 }
476
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800477 private void sendToOtherNodes(Ethernet eth, OFPacketIn pi) {
478 ARP arp = (ARP) eth.getPayload();
Jonathan Hartd3003252013-11-15 09:44:46 -0800479
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800480 if (log.isTraceEnabled()) {
481 log.trace("Sending ARP request for {} to other ONOS instances",
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800482 inetAddressToString(arp.getTargetProtocolAddress()));
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800483 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800484
485 InetAddress targetAddress;
486 try {
487 targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress());
488 } catch (UnknownHostException e) {
489 log.error("Unknown host", e);
490 return;
491 }
492
493 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize()));
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800494 }
495
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200496 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200497 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
498 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
499 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
500
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200501 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700502 //I think this means the switch doesn't have any links.
503 //continue;
504 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200505 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200506
Jonathan Harta18e4792013-10-31 10:10:54 -0700507
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200508 OFPacketOut po = new OFPacketOut();
509 po.setInPort(OFPort.OFPP_NONE)
510 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200511 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200512
513 List<OFAction> actions = new ArrayList<OFAction>();
514
515 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200516 if (linkPorts.contains(portNum) ||
517 (sw.getId() == inSwitch && portNum == inPort)){
518 //If this port isn't an edge port or is the ingress port
519 //for the ARP, don't broadcast out it
520 continue;
521 }
522
523 actions.add(new OFActionOutput(portNum));
524 }
525
526 po.setActions(actions);
527 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
528 po.setActionsLength(actionsLength);
529 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200530 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200531
532 List<OFMessage> msgList = new ArrayList<OFMessage>();
533 msgList.add(po);
534
535 try {
536 sw.write(msgList, null);
537 sw.flush();
538 } catch (IOException e) {
539 log.error("Failure writing packet out to switch", e);
540 }
541 }
542 }
543
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800544 private void broadcastArpRequestOutMyEdge(byte[] arpRequest) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800545 List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
546
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800547 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
548
549 OFPacketOut po = new OFPacketOut();
550 po.setInPort(OFPort.OFPP_NONE)
551 .setBufferId(-1)
552 .setPacketData(arpRequest);
553
554 List<OFAction> actions = new ArrayList<OFAction>();
555
556 Iterable<IPortObject> ports
557 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
558 if (ports == null) {
559 continue;
560 }
561
562 for (IPortObject portObject : ports) {
563 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800564 switchPorts.add(new SwitchPort(new Dpid(sw.getId()),
565 new Port(portObject.getNumber())));
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800566 actions.add(new OFActionOutput(portObject.getNumber()));
567 }
568 }
569
570 po.setActions(actions);
571 short actionsLength = (short)
572 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
573 po.setActionsLength(actionsLength);
574 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
575 + arpRequest.length);
576
577 try {
578 sw.write(po, null);
579 sw.flush();
580 } catch (IOException e) {
581 log.error("Failure writing packet out to switch", e);
582 }
583 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800584
585 log.debug("Broadcast ARP request for to: {}", switchPorts);
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800586 }
587
Jonathan Hart2f790d22013-08-15 14:01:24 +1200588 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200589 if (log.isTraceEnabled()) {
590 log.trace("Sending ARP request out {}/{}",
591 HexString.toHexString(dpid), port);
592 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200593
594 OFPacketOut po = new OFPacketOut();
595 po.setInPort(OFPort.OFPP_NONE)
596 .setBufferId(-1)
597 .setPacketData(arpRequest);
598
599 List<OFAction> actions = new ArrayList<OFAction>();
600 actions.add(new OFActionOutput(port));
601 po.setActions(actions);
602 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
603 po.setActionsLength(actionsLength);
604 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
605 + arpRequest.length);
606
607 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
608
609 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200610 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200611 return;
612 }
613
614 try {
615 sw.write(po, null);
616 sw.flush();
617 } catch (IOException e) {
618 log.error("Failure writing packet out to switch", e);
619 }
620 }
621
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300622 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200623 if (log.isTraceEnabled()) {
624 log.trace("Sending reply {} => {} to {}", new Object[] {
625 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300626 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200627 inetAddressToString(arpRequest.getSenderProtocolAddress())});
628 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200629
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200630 ARP arpReply = new ARP();
631 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
632 .setProtocolType(ARP.PROTO_TYPE_IP)
633 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200634 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200635 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300636 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200637 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
638 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
639 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
640
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700641
642
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200643 Ethernet eth = new Ethernet();
644 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300645 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200646 .setEtherType(Ethernet.TYPE_ARP)
647 .setPayload(arpReply);
648
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700649 if (vlan != NO_VLAN) {
650 eth.setVlanID(vlan)
651 .setPriorityCode((byte)0);
652 }
653
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200654 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200655 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200656
657 OFPacketOut po = new OFPacketOut();
658 po.setInPort(OFPort.OFPP_NONE)
659 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200660 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200661 .setActions(actions)
662 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
663 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
664 + po.getPacketData().length);
665
666 List<OFMessage> msgList = new ArrayList<OFMessage>();
667 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200668
669 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
670
671 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200672 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200673 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200674 return;
675 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200676
677 try {
678 sw.write(msgList, null);
679 sw.flush();
680 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200681 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200682 }
683 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300684
685 private String inetAddressToString(byte[] bytes) {
686 try {
687 return InetAddress.getByAddress(bytes).getHostAddress();
688 } catch (UnknownHostException e) {
689 log.debug("Invalid IP address", e);
690 return "";
691 }
692 }
693
694 /*
695 * IProxyArpService methods
696 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200697
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200698 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300699 public MACAddress getMacAddress(InetAddress ipAddress) {
700 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200701 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200702
703 @Override
704 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
705 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200706 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200707
Jonathan Hart6e618212013-08-21 22:28:43 +1200708 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700709 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200710 sendArpRequestForAddress(ipAddress);
711 }
712 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300713
714 @Override
715 public List<String> getMappings() {
716 return arpCache.getMappings();
717 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800718
719 /*
720 * IArpEventHandler methods
721 */
722
723 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800724 public void arpRequestNotification(ArpMessage arpMessage) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800725 //log.debug("Received ARP notification from other instances");
Jonathan Hartd3003252013-11-15 09:44:46 -0800726
727 switch (arpMessage.getType()){
728 case REQUEST:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800729 log.debug("Received ARP request notification for {}",
730 arpMessage.getAddress());
Jonathan Hartd3003252013-11-15 09:44:46 -0800731 broadcastArpRequestOutMyEdge(arpMessage.getPacket());
732 break;
733 case REPLY:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800734 log.debug("Received ARP reply notification for {}",
735 arpMessage.getAddress());
Jonathan Hartd3003252013-11-15 09:44:46 -0800736 sendArpReplyToWaitingRequesters(arpMessage.getAddress());
737 break;
738 }
739 }
740
741 private void sendArpReplyToWaitingRequesters(InetAddress address) {
742 log.debug("Sending ARP reply for {} to requesters",
743 address.getHostAddress());
744
745 //See if anyone's waiting for this ARP reply
746 Set<ArpRequest> requests = arpRequests.get(address);
747
748 //Synchronize on the Multimap while using an iterator for one of the sets
749 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
750 synchronized (arpRequests) {
751 Iterator<ArpRequest> it = requests.iterator();
752 while (it.hasNext()) {
753 ArpRequest request = it.next();
754 it.remove();
755 requestsToSend.add(request);
756 }
757 }
758
759 IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
760 InetAddresses.coerceToInteger(address));
761
762 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
763
764 log.debug("Found {} at {} in network map",
765 address.getHostAddress(), mac);
766
767 //Don't hold an ARP lock while dispatching requests
768 for (ArpRequest request : requestsToSend) {
769 request.dispatchReply(address, mac);
770 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800771 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200772}