blob: 426a45583d814245d5ea5ac6e11c1e2b70902abb [file] [log] [blame]
Jonathan Hartc7ca35d2013-06-25 20:54:25 +12001package net.onrc.onos.ofcontroller.proxyarp;
2
3import java.io.IOException;
4import java.net.InetAddress;
5import java.net.UnknownHostException;
6import java.util.ArrayList;
7import java.util.Collection;
Jonathan Harta18e4792013-10-31 10:10:54 -07008import java.util.HashSet;
Jonathan Hart6261dcd2013-07-22 17:58:35 +12009import java.util.Iterator;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120010import java.util.List;
11import java.util.Map;
12import java.util.Set;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120013import java.util.Timer;
14import java.util.TimerTask;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120015
16import net.floodlightcontroller.core.FloodlightContext;
17import net.floodlightcontroller.core.IFloodlightProviderService;
18import net.floodlightcontroller.core.IOFMessageListener;
19import net.floodlightcontroller.core.IOFSwitch;
20import net.floodlightcontroller.packet.ARP;
21import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart08ee8522013-09-22 17:34:43 +120022import net.floodlightcontroller.packet.IPv4;
Jonathan Hart5afde492013-10-01 12:30:53 +130023import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120024import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120025import net.floodlightcontroller.util.MACAddress;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080026import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120027import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070028import net.onrc.onos.ofcontroller.core.IDeviceStorage;
29import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080030import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService;
31import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoSwitchService;
Jonathan Hartebba1e12013-10-29 11:37:02 -070032import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070033import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120034
35import org.openflow.protocol.OFMessage;
36import org.openflow.protocol.OFPacketIn;
37import org.openflow.protocol.OFPacketOut;
38import org.openflow.protocol.OFPort;
39import org.openflow.protocol.OFType;
40import org.openflow.protocol.action.OFAction;
41import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120042import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
Jonathan Hart4dfc3652013-08-02 20:22:36 +120046import com.google.common.collect.HashMultimap;
47import com.google.common.collect.Multimaps;
48import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070049import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120050
Jonathan Hart18ad55c2013-11-11 22:49:55 -080051public class ProxyArpManager implements IProxyArpService, IOFMessageListener,
52 IArpEventHandler {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120053 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120054
Jonathan Hartdf6ec332013-08-04 01:37:14 +120055 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130056
57 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120058
Jonathan Harta8887642013-10-28 13:46:54 -070059 private IFloodlightProviderService floodlightProvider;
60 private ITopologyService topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080061 private IDatagridService datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -070062 private IConfigInfoService configService;
63 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120064
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070065 private IDeviceStorage deviceStorage;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080066 private ITopoSwitchService topoSwitchService;
67 private ITopoLinkService topoLinkService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070068
Jonathan Hart1cf9de02013-10-21 17:42:29 -070069 private short vlan;
70 private static final short NO_VLAN = 0;
71
Jonathan Harta8887642013-10-28 13:46:54 -070072 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120073
Jonathan Harta8887642013-10-28 13:46:54 -070074 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120075
Jonathan Hartabad6a52013-09-30 18:17:21 +130076 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120077 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130078 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120079 private long requestTime;
80
Jonathan Hart4dfc3652013-08-02 20:22:36 +120081 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120082 this.requester = requester;
83 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120084 this.requestTime = System.currentTimeMillis();
85 }
86
Jonathan Hart4dfc3652013-08-02 20:22:36 +120087 public ArpRequest(ArpRequest old) {
88 this.requester = old.requester;
89 this.retry = old.retry;
90 this.requestTime = System.currentTimeMillis();
91 }
92
Jonathan Hart4dfc3652013-08-02 20:22:36 +120093 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +130094 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120095 }
96
Jonathan Hart4dfc3652013-08-02 20:22:36 +120097 public boolean shouldRetry() {
98 return retry;
99 }
100
Jonathan Hartabad6a52013-09-30 18:17:21 +1300101 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200102 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200103 }
104 }
105
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300106 private class HostArpRequester implements IArpRequester {
107 private final ARP arpRequest;
108 private final long dpid;
109 private final short port;
110
111 public HostArpRequester(ARP arpRequest, long dpid, short port) {
112 this.arpRequest = arpRequest;
113 this.dpid = dpid;
114 this.port = port;
115 }
116
117 @Override
118 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
119 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
120 }
121 }
122
Jonathan Harta8887642013-10-28 13:46:54 -0700123 /*
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200124 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Harta8887642013-10-28 13:46:54 -0700125 ITopologyService topology, IConfigInfoService configService,
Jonathan Hart5afde492013-10-01 12:30:53 +1300126 IRestApiService restApi){
Jonathan Harta8887642013-10-28 13:46:54 -0700127
128 }
129 */
130
131 public void init(IFloodlightProviderService floodlightProvider,
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800132 ITopologyService topology, IDatagridService datagrid,
Jonathan Harta18e4792013-10-31 10:10:54 -0700133 IConfigInfoService config, IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200134 this.floodlightProvider = floodlightProvider;
135 this.topology = topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800136 this.datagrid = datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -0700137 this.configService = config;
Jonathan Hart5afde492013-10-01 12:30:53 +1300138 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200139
Jonathan Hartabad6a52013-09-30 18:17:21 +1300140 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200141
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200142 arpRequests = Multimaps.synchronizedSetMultimap(
143 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200144 }
145
Jonathan Harta8887642013-10-28 13:46:54 -0700146 public void startUp() {
147 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700148 log.info("vlan set to {}", this.vlan);
149
Jonathan Hart5afde492013-10-01 12:30:53 +1300150 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700151 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300152
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800153 datagrid.registerArpEventHandler(this);
154
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700155 deviceStorage = new DeviceStorageImpl();
156 deviceStorage.init("");
157
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200158 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200159 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200160 @Override
161 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200162 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200163 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200164 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200165 }
166
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200167 /*
168 * Function that runs periodically to manage the asynchronous request mechanism.
169 * It basically cleans up old ARP requests if we don't get a response for them.
170 * The caller can designate that a request should be retried indefinitely, and
171 * this task will handle that as well.
172 */
173 private void doPeriodicArpProcessing() {
174 SetMultimap<InetAddress, ArpRequest> retryList
175 = HashMultimap.<InetAddress, ArpRequest>create();
176
177 //Have to synchronize externally on the Multimap while using an iterator,
178 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200179 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200180 log.debug("Current have {} outstanding requests",
181 arpRequests.size());
182
183 Iterator<Map.Entry<InetAddress, ArpRequest>> it
184 = arpRequests.entries().iterator();
185
186 while (it.hasNext()) {
187 Map.Entry<InetAddress, ArpRequest> entry
188 = it.next();
189 ArpRequest request = entry.getValue();
190 if (request.isExpired()) {
191 log.debug("Cleaning expired ARP request for {}",
192 entry.getKey().getHostAddress());
193
194 it.remove();
195
196 if (request.shouldRetry()) {
197 retryList.put(entry.getKey(), request);
198 }
199 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200200 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200201 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200202
203 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
204 : retryList.asMap().entrySet()) {
205
206 InetAddress address = entry.getKey();
207
208 log.debug("Resending ARP request for {}", address.getHostAddress());
209
210 sendArpRequestForAddress(address);
211
212 for (ArpRequest request : entry.getValue()) {
213 arpRequests.put(address, new ArpRequest(request));
214 }
215 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200216 }
217
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200218 @Override
219 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700220 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200221 }
222
223 @Override
224 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700225 if (type == OFType.PACKET_IN) {
226 return "devicemanager".equals(name);
227 }
228 else {
229 return false;
230 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200231 }
232
233 @Override
234 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200235 return false;
236 }
237
238 @Override
239 public Command receive(
240 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
241
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800242 //if (msg.getType() != OFType.PACKET_IN){
243 //return Command.CONTINUE;
244 //}
245 log.debug("received packet");
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200246
247 OFPacketIn pi = (OFPacketIn) msg;
248
249 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
250 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
251
252 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800253 log.debug("is arp");
254 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200255 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800256 log.debug("is request");
Jonathan Harta18e4792013-10-31 10:10:54 -0700257 //TODO check what the DeviceManager does about propagating
258 //or swallowing ARPs. We want to go after DeviceManager in the
259 //chain but we really need it to CONTINUE ARP packets so we can
260 //get them.
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800261 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200262 }
263 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800264 log.debug("is reply");
Jonathan Harta18e4792013-10-31 10:10:54 -0700265 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200266 }
267 }
268
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200269 //TODO should we propagate ARP or swallow it?
270 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200271 return Command.CONTINUE;
272 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200273
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800274 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200275 if (log.isTraceEnabled()) {
276 log.trace("ARP request received for {}",
277 inetAddressToString(arp.getTargetProtocolAddress()));
278 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200279
280 InetAddress target;
281 try {
282 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
283 } catch (UnknownHostException e) {
284 log.debug("Invalid address in ARP request", e);
285 return;
286 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200287
Jonathan Harta8887642013-10-28 13:46:54 -0700288 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200289 //If the request came from outside our network, we only care if
290 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700291 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200292 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700293 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200294
295 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700296 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200297 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200298
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200299 return;
300 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200301
Jonathan Harta18e4792013-10-31 10:10:54 -0700302 //MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200303
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700304 IDeviceObject targetDevice =
305 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
Jonathan Harta18e4792013-10-31 10:10:54 -0700306
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800307 log.debug("targetDevice: {}", targetDevice);
308
Jonathan Harta18e4792013-10-31 10:10:54 -0700309 if (targetDevice != null) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800310 // We have the device in our database, so send a reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700311 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
312
313 if (log.isTraceEnabled()) {
314 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
315 inetAddressToString(arp.getTargetProtocolAddress()),
316 macAddress.toString(),
317 HexString.toHexString(sw.getId()), pi.getInPort()});
318 }
319
320 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
321 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800322 else {
323 // We don't know the device so broadcast the request out
324 // the edge of the network
325
326 //Record where the request came from so we know where to send the reply
327 arpRequests.put(target, new ArpRequest(
328 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
329
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800330 sendToOtherNodes(eth, pi);
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800331 }
Jonathan Harta18e4792013-10-31 10:10:54 -0700332
333 /*if (macAddress == null){
Jonathan Hartabad6a52013-09-30 18:17:21 +1300334 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200335
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200336 //Record where the request came from so we know where to send the reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700337 //arpRequests.put(target, new ArpRequest(
338 //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200339
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200340 //Flood the request out edge ports
Jonathan Harta18e4792013-10-31 10:10:54 -0700341 //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200342 }
343 else {
344 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200345 if (log.isTraceEnabled()) {
346 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
347 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300348 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200349 HexString.toHexString(sw.getId()), pi.getInPort()});
350 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200351
Jonathan Hartabad6a52013-09-30 18:17:21 +1300352 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Harta18e4792013-10-31 10:10:54 -0700353 }*/
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200354 }
355
Jonathan Hart1912afc2013-10-11 12:02:44 +1300356 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200357 if (log.isTraceEnabled()) {
358 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
359 inetAddressToString(arp.getSenderProtocolAddress()),
360 HexString.toHexString(arp.getSenderHardwareAddress()),
361 HexString.toHexString(sw.getId()), pi.getInPort()});
362 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200363
Jonathan Hartabad6a52013-09-30 18:17:21 +1300364 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200365 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300366 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200367 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200368 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200369 return;
370 }
371
Jonathan Hartabad6a52013-09-30 18:17:21 +1300372 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
373
374 arpCache.update(senderIpAddress, senderMacAddress);
375
376 //See if anyone's waiting for this ARP reply
377 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200378
379 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200380 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200381 synchronized (arpRequests) {
382 Iterator<ArpRequest> it = requests.iterator();
383 while (it.hasNext()) {
384 ArpRequest request = it.next();
385 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200386 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200387 }
388 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200389
390 //Don't hold an ARP lock while dispatching requests
391 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300392 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200393 }
394 }
395
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200396 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200397 //TODO what should the sender IP address and MAC address be if no
398 //IP addresses are configured? Will there ever be a need to send
399 //ARP requests from the controller in that case?
400 //All-zero MAC address doesn't seem to work - hosts don't respond to it
401
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200402 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
403 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200404 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200405 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200406 (byte)0xff, (byte)0xff, (byte)0xff};
407
408 ARP arpRequest = new ARP();
409
410 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
411 .setProtocolType(ARP.PROTO_TYPE_IP)
412 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200413 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200414 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200415 .setTargetHardwareAddress(zeroMac)
416 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200417
Jonathan Harta8887642013-10-28 13:46:54 -0700418 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200419 //TODO hack for now as it's unclear what the MAC address should be
420 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200421 if (routerMacAddress != null) {
422 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200423 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200424 arpRequest.setSenderHardwareAddress(senderMacAddress);
425
426 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700427 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200428 if (intf != null) {
429 senderIPAddress = intf.getIpAddress().getAddress();
430 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200431
432 arpRequest.setSenderProtocolAddress(senderIPAddress);
433
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200434 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200435 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200436 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200437 .setEtherType(Ethernet.TYPE_ARP)
438 .setPayload(arpRequest);
439
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700440 if (vlan != NO_VLAN) {
441 eth.setVlanID(vlan)
442 .setPriorityCode((byte)0);
443 }
444
Jonathan Hart2f790d22013-08-15 14:01:24 +1200445 sendArpRequestToSwitches(ipAddress, eth.serialize());
446 }
447
448 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200449 sendArpRequestToSwitches(dstAddress, arpRequest,
450 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200451 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200452
Jonathan Hart2f790d22013-08-15 14:01:24 +1200453 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
454 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200455
Jonathan Harta8887642013-10-28 13:46:54 -0700456 if (configService.hasLayer3Configuration()) {
457 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200458 if (intf != null) {
459 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
460 }
461 else {
462 //TODO here it should be broadcast out all non-interface edge ports.
463 //I think we can assume that if it's not a request for an external
464 //network, it's an ARP for a host in our own network. So we want to
465 //send it out all edge ports that don't have an interface configured
466 //to ensure it reaches all hosts in our network.
467 log.debug("No interface found to send ARP request for {}",
468 dstAddress.getHostAddress());
469 }
470 }
471 else {
472 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
473 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200474 }
475
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800476 private void sendToOtherNodes(Ethernet eth, OFPacketIn pi) {
477 ARP arp = (ARP) eth.getPayload();
478 if (log.isTraceEnabled()) {
479 log.trace("Sending ARP request for {} to other ONOS instances",
480 HexString.toHexString(arp.getTargetProtocolAddress()));
481 }
482 datagrid.sendArpRequest(eth.serialize());
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800483 }
484
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200485 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200486 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
487 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
488 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
489
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200490 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700491 //I think this means the switch doesn't have any links.
492 //continue;
493 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200494 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200495
Jonathan Harta18e4792013-10-31 10:10:54 -0700496
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200497 OFPacketOut po = new OFPacketOut();
498 po.setInPort(OFPort.OFPP_NONE)
499 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200500 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200501
502 List<OFAction> actions = new ArrayList<OFAction>();
503
504 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200505 if (linkPorts.contains(portNum) ||
506 (sw.getId() == inSwitch && portNum == inPort)){
507 //If this port isn't an edge port or is the ingress port
508 //for the ARP, don't broadcast out it
509 continue;
510 }
511
512 actions.add(new OFActionOutput(portNum));
513 }
514
515 po.setActions(actions);
516 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
517 po.setActionsLength(actionsLength);
518 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200519 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200520
521 List<OFMessage> msgList = new ArrayList<OFMessage>();
522 msgList.add(po);
523
524 try {
525 sw.write(msgList, null);
526 sw.flush();
527 } catch (IOException e) {
528 log.error("Failure writing packet out to switch", e);
529 }
530 }
531 }
532
Jonathan Hart2f790d22013-08-15 14:01:24 +1200533 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200534 if (log.isTraceEnabled()) {
535 log.trace("Sending ARP request out {}/{}",
536 HexString.toHexString(dpid), port);
537 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200538
539 OFPacketOut po = new OFPacketOut();
540 po.setInPort(OFPort.OFPP_NONE)
541 .setBufferId(-1)
542 .setPacketData(arpRequest);
543
544 List<OFAction> actions = new ArrayList<OFAction>();
545 actions.add(new OFActionOutput(port));
546 po.setActions(actions);
547 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
548 po.setActionsLength(actionsLength);
549 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
550 + arpRequest.length);
551
552 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
553
554 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200555 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200556 return;
557 }
558
559 try {
560 sw.write(po, null);
561 sw.flush();
562 } catch (IOException e) {
563 log.error("Failure writing packet out to switch", e);
564 }
565 }
566
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300567 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200568 if (log.isTraceEnabled()) {
569 log.trace("Sending reply {} => {} to {}", new Object[] {
570 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300571 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200572 inetAddressToString(arpRequest.getSenderProtocolAddress())});
573 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200574
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200575 ARP arpReply = new ARP();
576 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
577 .setProtocolType(ARP.PROTO_TYPE_IP)
578 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200579 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200580 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300581 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200582 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
583 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
584 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
585
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700586
587
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200588 Ethernet eth = new Ethernet();
589 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300590 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200591 .setEtherType(Ethernet.TYPE_ARP)
592 .setPayload(arpReply);
593
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700594 if (vlan != NO_VLAN) {
595 eth.setVlanID(vlan)
596 .setPriorityCode((byte)0);
597 }
598
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200599 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200600 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200601
602 OFPacketOut po = new OFPacketOut();
603 po.setInPort(OFPort.OFPP_NONE)
604 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200605 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200606 .setActions(actions)
607 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
608 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
609 + po.getPacketData().length);
610
611 List<OFMessage> msgList = new ArrayList<OFMessage>();
612 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200613
614 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
615
616 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200617 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200618 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200619 return;
620 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200621
622 try {
623 sw.write(msgList, null);
624 sw.flush();
625 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200626 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200627 }
628 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300629
630 private String inetAddressToString(byte[] bytes) {
631 try {
632 return InetAddress.getByAddress(bytes).getHostAddress();
633 } catch (UnknownHostException e) {
634 log.debug("Invalid IP address", e);
635 return "";
636 }
637 }
638
639 /*
640 * IProxyArpService methods
641 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200642
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200643 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300644 public MACAddress getMacAddress(InetAddress ipAddress) {
645 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200646 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200647
648 @Override
649 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
650 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200651 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200652
Jonathan Hart6e618212013-08-21 22:28:43 +1200653 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700654 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200655 sendArpRequestForAddress(ipAddress);
656 }
657 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300658
659 @Override
660 public List<String> getMappings() {
661 return arpCache.getMappings();
662 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800663
664 /*
665 * IArpEventHandler methods
666 */
667
668 @Override
669 public void arpRequestNotification(byte[] arpRequest) {
670 log.debug("Received ARP notification from other instances");
671 broadcastArpRequestOutEdge(arpRequest, Long.MAX_VALUE, Short.MAX_VALUE);
672 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200673}