blob: 5a8ed080a9d83185928378e807924fa0e22abf61 [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 Hartc7ca35d2013-06-25 20:54:25 +120037
38import org.openflow.protocol.OFMessage;
39import org.openflow.protocol.OFPacketIn;
40import org.openflow.protocol.OFPacketOut;
41import org.openflow.protocol.OFPort;
42import org.openflow.protocol.OFType;
43import org.openflow.protocol.action.OFAction;
44import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120045import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
Jonathan Hart4dfc3652013-08-02 20:22:36 +120049import com.google.common.collect.HashMultimap;
50import com.google.common.collect.Multimaps;
51import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070052import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120053
Jonathan Hart18ad55c2013-11-11 22:49:55 -080054public class ProxyArpManager implements IProxyArpService, IOFMessageListener,
55 IArpEventHandler {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120056 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120057
Jonathan Hartdf6ec332013-08-04 01:37:14 +120058 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130059
60 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120061
Jonathan Harta8887642013-10-28 13:46:54 -070062 private IFloodlightProviderService floodlightProvider;
63 private ITopologyService topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080064 private IDatagridService datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -070065 private IConfigInfoService configService;
66 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120067
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070068 private IDeviceStorage deviceStorage;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080069 private volatile ITopoSwitchService topoSwitchService;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080070 private ITopoLinkService topoLinkService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070071
Jonathan Hart1cf9de02013-10-21 17:42:29 -070072 private short vlan;
73 private static final short NO_VLAN = 0;
74
Jonathan Harta8887642013-10-28 13:46:54 -070075 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120076
Jonathan Harta8887642013-10-28 13:46:54 -070077 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120078
Jonathan Hartabad6a52013-09-30 18:17:21 +130079 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120080 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130081 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120082 private long requestTime;
83
Jonathan Hart4dfc3652013-08-02 20:22:36 +120084 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120085 this.requester = requester;
86 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120087 this.requestTime = System.currentTimeMillis();
88 }
89
Jonathan Hart4dfc3652013-08-02 20:22:36 +120090 public ArpRequest(ArpRequest old) {
91 this.requester = old.requester;
92 this.retry = old.retry;
93 this.requestTime = System.currentTimeMillis();
94 }
95
Jonathan Hart4dfc3652013-08-02 20:22:36 +120096 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +130097 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120098 }
99
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200100 public boolean shouldRetry() {
101 return retry;
102 }
103
Jonathan Hartabad6a52013-09-30 18:17:21 +1300104 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200105 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200106 }
107 }
108
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300109 private class HostArpRequester implements IArpRequester {
110 private final ARP arpRequest;
111 private final long dpid;
112 private final short port;
113
114 public HostArpRequester(ARP arpRequest, long dpid, short port) {
115 this.arpRequest = arpRequest;
116 this.dpid = dpid;
117 this.port = port;
118 }
119
120 @Override
121 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
122 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
123 }
124 }
125
Jonathan Harta8887642013-10-28 13:46:54 -0700126 /*
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200127 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Harta8887642013-10-28 13:46:54 -0700128 ITopologyService topology, IConfigInfoService configService,
Jonathan Hart5afde492013-10-01 12:30:53 +1300129 IRestApiService restApi){
Jonathan Harta8887642013-10-28 13:46:54 -0700130
131 }
132 */
133
134 public void init(IFloodlightProviderService floodlightProvider,
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800135 ITopologyService topology, IDatagridService datagrid,
Jonathan Harta18e4792013-10-31 10:10:54 -0700136 IConfigInfoService config, IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200137 this.floodlightProvider = floodlightProvider;
138 this.topology = topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800139 this.datagrid = datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -0700140 this.configService = config;
Jonathan Hart5afde492013-10-01 12:30:53 +1300141 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200142
Jonathan Hartabad6a52013-09-30 18:17:21 +1300143 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200144
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200145 arpRequests = Multimaps.synchronizedSetMultimap(
146 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800147
148 topoSwitchService = new TopoSwitchServiceImpl();
149 topoLinkService = new TopoLinkServiceImpl();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200150 }
151
Jonathan Harta8887642013-10-28 13:46:54 -0700152 public void startUp() {
153 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700154 log.info("vlan set to {}", this.vlan);
155
Jonathan Hart5afde492013-10-01 12:30:53 +1300156 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700157 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300158
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800159 datagrid.registerArpEventHandler(this);
160
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700161 deviceStorage = new DeviceStorageImpl();
162 deviceStorage.init("");
163
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200164 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200165 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200166 @Override
167 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200168 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200169 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200170 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200171 }
172
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200173 /*
174 * Function that runs periodically to manage the asynchronous request mechanism.
175 * It basically cleans up old ARP requests if we don't get a response for them.
176 * The caller can designate that a request should be retried indefinitely, and
177 * this task will handle that as well.
178 */
179 private void doPeriodicArpProcessing() {
180 SetMultimap<InetAddress, ArpRequest> retryList
181 = HashMultimap.<InetAddress, ArpRequest>create();
182
183 //Have to synchronize externally on the Multimap while using an iterator,
184 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200185 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200186 log.debug("Current have {} outstanding requests",
187 arpRequests.size());
188
189 Iterator<Map.Entry<InetAddress, ArpRequest>> it
190 = arpRequests.entries().iterator();
191
192 while (it.hasNext()) {
193 Map.Entry<InetAddress, ArpRequest> entry
194 = it.next();
195 ArpRequest request = entry.getValue();
196 if (request.isExpired()) {
197 log.debug("Cleaning expired ARP request for {}",
198 entry.getKey().getHostAddress());
199
200 it.remove();
201
202 if (request.shouldRetry()) {
203 retryList.put(entry.getKey(), request);
204 }
205 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200206 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200207 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200208
209 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
210 : retryList.asMap().entrySet()) {
211
212 InetAddress address = entry.getKey();
213
214 log.debug("Resending ARP request for {}", address.getHostAddress());
215
216 sendArpRequestForAddress(address);
217
218 for (ArpRequest request : entry.getValue()) {
219 arpRequests.put(address, new ArpRequest(request));
220 }
221 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200222 }
223
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200224 @Override
225 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700226 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200227 }
228
229 @Override
230 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700231 if (type == OFType.PACKET_IN) {
232 return "devicemanager".equals(name);
233 }
234 else {
235 return false;
236 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200237 }
238
239 @Override
240 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200241 return false;
242 }
243
244 @Override
245 public Command receive(
246 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
247
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800248 //if (msg.getType() != OFType.PACKET_IN){
249 //return Command.CONTINUE;
250 //}
251 log.debug("received packet");
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200252
253 OFPacketIn pi = (OFPacketIn) msg;
254
255 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
256 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
257
258 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800259 log.debug("is arp");
260 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200261 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800262 log.debug("is request");
Jonathan Harta18e4792013-10-31 10:10:54 -0700263 //TODO check what the DeviceManager does about propagating
264 //or swallowing ARPs. We want to go after DeviceManager in the
265 //chain but we really need it to CONTINUE ARP packets so we can
266 //get them.
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800267 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200268 }
269 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800270 log.debug("is reply");
Jonathan Harta18e4792013-10-31 10:10:54 -0700271 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200272 }
273 }
274
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200275 //TODO should we propagate ARP or swallow it?
276 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200277 return Command.CONTINUE;
278 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200279
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800280 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200281 if (log.isTraceEnabled()) {
282 log.trace("ARP request received for {}",
283 inetAddressToString(arp.getTargetProtocolAddress()));
284 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200285
286 InetAddress target;
287 try {
288 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
289 } catch (UnknownHostException e) {
290 log.debug("Invalid address in ARP request", e);
291 return;
292 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200293
Jonathan Harta8887642013-10-28 13:46:54 -0700294 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200295 //If the request came from outside our network, we only care if
296 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700297 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200298 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700299 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200300
301 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700302 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200303 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200304
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200305 return;
306 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200307
Jonathan Harta18e4792013-10-31 10:10:54 -0700308 //MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200309
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700310 IDeviceObject targetDevice =
311 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
Jonathan Harta18e4792013-10-31 10:10:54 -0700312
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800313 log.debug("targetDevice: {}", targetDevice);
314
Jonathan Harta18e4792013-10-31 10:10:54 -0700315 if (targetDevice != null) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800316 // We have the device in our database, so send a reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700317 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
318
319 if (log.isTraceEnabled()) {
320 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
321 inetAddressToString(arp.getTargetProtocolAddress()),
322 macAddress.toString(),
323 HexString.toHexString(sw.getId()), pi.getInPort()});
324 }
325
326 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
327 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800328 else {
329 // We don't know the device so broadcast the request out
330 // the edge of the network
331
332 //Record where the request came from so we know where to send the reply
333 arpRequests.put(target, new ArpRequest(
334 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
335
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800336 sendToOtherNodes(eth, pi);
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800337 }
Jonathan Harta18e4792013-10-31 10:10:54 -0700338
339 /*if (macAddress == null){
Jonathan Hartabad6a52013-09-30 18:17:21 +1300340 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200341
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200342 //Record where the request came from so we know where to send the reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700343 //arpRequests.put(target, new ArpRequest(
344 //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200345
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200346 //Flood the request out edge ports
Jonathan Harta18e4792013-10-31 10:10:54 -0700347 //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200348 }
349 else {
350 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200351 if (log.isTraceEnabled()) {
352 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
353 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300354 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200355 HexString.toHexString(sw.getId()), pi.getInPort()});
356 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200357
Jonathan Hartabad6a52013-09-30 18:17:21 +1300358 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Harta18e4792013-10-31 10:10:54 -0700359 }*/
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200360 }
361
Jonathan Hart1912afc2013-10-11 12:02:44 +1300362 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200363 if (log.isTraceEnabled()) {
364 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
365 inetAddressToString(arp.getSenderProtocolAddress()),
366 HexString.toHexString(arp.getSenderHardwareAddress()),
367 HexString.toHexString(sw.getId()), pi.getInPort()});
368 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200369
Jonathan Hartabad6a52013-09-30 18:17:21 +1300370 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200371 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300372 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200373 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200374 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200375 return;
376 }
377
Jonathan Hartabad6a52013-09-30 18:17:21 +1300378 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
379
380 arpCache.update(senderIpAddress, senderMacAddress);
381
382 //See if anyone's waiting for this ARP reply
383 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200384
385 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200386 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200387 synchronized (arpRequests) {
388 Iterator<ArpRequest> it = requests.iterator();
389 while (it.hasNext()) {
390 ArpRequest request = it.next();
391 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200392 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200393 }
394 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200395
396 //Don't hold an ARP lock while dispatching requests
397 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300398 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200399 }
400 }
401
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200402 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200403 //TODO what should the sender IP address and MAC address be if no
404 //IP addresses are configured? Will there ever be a need to send
405 //ARP requests from the controller in that case?
406 //All-zero MAC address doesn't seem to work - hosts don't respond to it
407
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200408 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
409 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200410 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200411 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200412 (byte)0xff, (byte)0xff, (byte)0xff};
413
414 ARP arpRequest = new ARP();
415
416 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
417 .setProtocolType(ARP.PROTO_TYPE_IP)
418 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200419 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200420 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200421 .setTargetHardwareAddress(zeroMac)
422 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200423
Jonathan Harta8887642013-10-28 13:46:54 -0700424 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200425 //TODO hack for now as it's unclear what the MAC address should be
426 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200427 if (routerMacAddress != null) {
428 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200429 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200430 arpRequest.setSenderHardwareAddress(senderMacAddress);
431
432 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700433 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200434 if (intf != null) {
435 senderIPAddress = intf.getIpAddress().getAddress();
436 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200437
438 arpRequest.setSenderProtocolAddress(senderIPAddress);
439
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200440 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200441 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200442 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200443 .setEtherType(Ethernet.TYPE_ARP)
444 .setPayload(arpRequest);
445
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700446 if (vlan != NO_VLAN) {
447 eth.setVlanID(vlan)
448 .setPriorityCode((byte)0);
449 }
450
Jonathan Hart2f790d22013-08-15 14:01:24 +1200451 sendArpRequestToSwitches(ipAddress, eth.serialize());
452 }
453
454 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200455 sendArpRequestToSwitches(dstAddress, arpRequest,
456 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200457 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200458
Jonathan Hart2f790d22013-08-15 14:01:24 +1200459 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
460 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200461
Jonathan Harta8887642013-10-28 13:46:54 -0700462 if (configService.hasLayer3Configuration()) {
463 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200464 if (intf != null) {
465 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
466 }
467 else {
468 //TODO here it should be broadcast out all non-interface edge ports.
469 //I think we can assume that if it's not a request for an external
470 //network, it's an ARP for a host in our own network. So we want to
471 //send it out all edge ports that don't have an interface configured
472 //to ensure it reaches all hosts in our network.
473 log.debug("No interface found to send ARP request for {}",
474 dstAddress.getHostAddress());
475 }
476 }
477 else {
478 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
479 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200480 }
481
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800482 private void sendToOtherNodes(Ethernet eth, OFPacketIn pi) {
483 ARP arp = (ARP) eth.getPayload();
484 if (log.isTraceEnabled()) {
485 log.trace("Sending ARP request for {} to other ONOS instances",
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800486 inetAddressToString(arp.getTargetProtocolAddress()));
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800487 }
488 datagrid.sendArpRequest(eth.serialize());
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800489 }
490
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200491 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200492 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
493 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
494 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
495
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200496 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700497 //I think this means the switch doesn't have any links.
498 //continue;
499 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200500 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200501
Jonathan Harta18e4792013-10-31 10:10:54 -0700502
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200503 OFPacketOut po = new OFPacketOut();
504 po.setInPort(OFPort.OFPP_NONE)
505 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200506 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200507
508 List<OFAction> actions = new ArrayList<OFAction>();
509
510 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200511 if (linkPorts.contains(portNum) ||
512 (sw.getId() == inSwitch && portNum == inPort)){
513 //If this port isn't an edge port or is the ingress port
514 //for the ARP, don't broadcast out it
515 continue;
516 }
517
518 actions.add(new OFActionOutput(portNum));
519 }
520
521 po.setActions(actions);
522 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
523 po.setActionsLength(actionsLength);
524 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200525 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200526
527 List<OFMessage> msgList = new ArrayList<OFMessage>();
528 msgList.add(po);
529
530 try {
531 sw.write(msgList, null);
532 sw.flush();
533 } catch (IOException e) {
534 log.error("Failure writing packet out to switch", e);
535 }
536 }
537 }
538
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800539 private void broadcastArpRequestOutMyEdge(byte[] arpRequest) {
540 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
541
542 OFPacketOut po = new OFPacketOut();
543 po.setInPort(OFPort.OFPP_NONE)
544 .setBufferId(-1)
545 .setPacketData(arpRequest);
546
547 List<OFAction> actions = new ArrayList<OFAction>();
548
549 Iterable<IPortObject> ports
550 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
551 if (ports == null) {
552 continue;
553 }
554
555 for (IPortObject portObject : ports) {
556 if (!portObject.getLinkedPorts().iterator().hasNext()) {
557 actions.add(new OFActionOutput(portObject.getNumber()));
558 }
559 }
560
561 po.setActions(actions);
562 short actionsLength = (short)
563 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
564 po.setActionsLength(actionsLength);
565 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
566 + arpRequest.length);
567
568 try {
569 sw.write(po, null);
570 sw.flush();
571 } catch (IOException e) {
572 log.error("Failure writing packet out to switch", e);
573 }
574 }
575 }
576
Jonathan Hart2f790d22013-08-15 14:01:24 +1200577 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200578 if (log.isTraceEnabled()) {
579 log.trace("Sending ARP request out {}/{}",
580 HexString.toHexString(dpid), port);
581 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200582
583 OFPacketOut po = new OFPacketOut();
584 po.setInPort(OFPort.OFPP_NONE)
585 .setBufferId(-1)
586 .setPacketData(arpRequest);
587
588 List<OFAction> actions = new ArrayList<OFAction>();
589 actions.add(new OFActionOutput(port));
590 po.setActions(actions);
591 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
592 po.setActionsLength(actionsLength);
593 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
594 + arpRequest.length);
595
596 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
597
598 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200599 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200600 return;
601 }
602
603 try {
604 sw.write(po, null);
605 sw.flush();
606 } catch (IOException e) {
607 log.error("Failure writing packet out to switch", e);
608 }
609 }
610
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300611 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200612 if (log.isTraceEnabled()) {
613 log.trace("Sending reply {} => {} to {}", new Object[] {
614 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300615 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200616 inetAddressToString(arpRequest.getSenderProtocolAddress())});
617 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200618
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200619 ARP arpReply = new ARP();
620 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
621 .setProtocolType(ARP.PROTO_TYPE_IP)
622 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200623 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200624 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300625 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200626 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
627 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
628 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
629
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700630
631
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200632 Ethernet eth = new Ethernet();
633 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300634 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200635 .setEtherType(Ethernet.TYPE_ARP)
636 .setPayload(arpReply);
637
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700638 if (vlan != NO_VLAN) {
639 eth.setVlanID(vlan)
640 .setPriorityCode((byte)0);
641 }
642
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200643 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200644 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200645
646 OFPacketOut po = new OFPacketOut();
647 po.setInPort(OFPort.OFPP_NONE)
648 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200649 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200650 .setActions(actions)
651 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
652 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
653 + po.getPacketData().length);
654
655 List<OFMessage> msgList = new ArrayList<OFMessage>();
656 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200657
658 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
659
660 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200661 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200662 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200663 return;
664 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200665
666 try {
667 sw.write(msgList, null);
668 sw.flush();
669 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200670 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200671 }
672 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300673
674 private String inetAddressToString(byte[] bytes) {
675 try {
676 return InetAddress.getByAddress(bytes).getHostAddress();
677 } catch (UnknownHostException e) {
678 log.debug("Invalid IP address", e);
679 return "";
680 }
681 }
682
683 /*
684 * IProxyArpService methods
685 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200686
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200687 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300688 public MACAddress getMacAddress(InetAddress ipAddress) {
689 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200690 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200691
692 @Override
693 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
694 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200695 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200696
Jonathan Hart6e618212013-08-21 22:28:43 +1200697 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700698 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200699 sendArpRequestForAddress(ipAddress);
700 }
701 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300702
703 @Override
704 public List<String> getMappings() {
705 return arpCache.getMappings();
706 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800707
708 /*
709 * IArpEventHandler methods
710 */
711
712 @Override
713 public void arpRequestNotification(byte[] arpRequest) {
714 log.debug("Received ARP notification from other instances");
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800715 //broadcastArpRequestOutEdge(arpRequest, Long.MAX_VALUE, Short.MAX_VALUE);
716 broadcastArpRequestOutMyEdge(arpRequest);
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800717 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200718}