blob: 85fd618d252d3f6055252c18c08c2ad2fd041b15 [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;
Jonathan Hartd3003252013-11-15 09:44:46 -080020import net.floodlightcontroller.devicemanager.IDevice;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120021import net.floodlightcontroller.packet.ARP;
22import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart08ee8522013-09-22 17:34:43 +120023import net.floodlightcontroller.packet.IPv4;
Jonathan Hart5afde492013-10-01 12:30:53 +130024import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120025import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120026import net.floodlightcontroller.util.MACAddress;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080027import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120028import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070029import net.onrc.onos.ofcontroller.core.IDeviceStorage;
30import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080031import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080032import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoLinkService;
33import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoSwitchService;
Jonathan Hartebba1e12013-10-29 11:37:02 -070034import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070035import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080036import net.onrc.onos.ofcontroller.core.internal.TopoLinkServiceImpl;
37import net.onrc.onos.ofcontroller.core.internal.TopoSwitchServiceImpl;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120038
39import org.openflow.protocol.OFMessage;
40import org.openflow.protocol.OFPacketIn;
41import org.openflow.protocol.OFPacketOut;
42import org.openflow.protocol.OFPort;
43import org.openflow.protocol.OFType;
44import org.openflow.protocol.action.OFAction;
45import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120046import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
Jonathan Hart4dfc3652013-08-02 20:22:36 +120050import com.google.common.collect.HashMultimap;
51import com.google.common.collect.Multimaps;
52import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070053import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120054
Jonathan Hart18ad55c2013-11-11 22:49:55 -080055public class ProxyArpManager implements IProxyArpService, IOFMessageListener,
56 IArpEventHandler {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120057 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120058
Jonathan Hartdf6ec332013-08-04 01:37:14 +120059 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130060
61 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120062
Jonathan Harta8887642013-10-28 13:46:54 -070063 private IFloodlightProviderService floodlightProvider;
64 private ITopologyService topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080065 private IDatagridService datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -070066 private IConfigInfoService configService;
67 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120068
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070069 private IDeviceStorage deviceStorage;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080070 private volatile ITopoSwitchService topoSwitchService;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080071 private ITopoLinkService topoLinkService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070072
Jonathan Hart1cf9de02013-10-21 17:42:29 -070073 private short vlan;
74 private static final short NO_VLAN = 0;
75
Jonathan Harta8887642013-10-28 13:46:54 -070076 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120077
Jonathan Harta8887642013-10-28 13:46:54 -070078 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120079
Jonathan Hartabad6a52013-09-30 18:17:21 +130080 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120081 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130082 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120083 private long requestTime;
84
Jonathan Hart4dfc3652013-08-02 20:22:36 +120085 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120086 this.requester = requester;
87 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120088 this.requestTime = System.currentTimeMillis();
89 }
90
Jonathan Hart4dfc3652013-08-02 20:22:36 +120091 public ArpRequest(ArpRequest old) {
92 this.requester = old.requester;
93 this.retry = old.retry;
94 this.requestTime = System.currentTimeMillis();
95 }
96
Jonathan Hart4dfc3652013-08-02 20:22:36 +120097 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +130098 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120099 }
100
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200101 public boolean shouldRetry() {
102 return retry;
103 }
104
Jonathan Hartabad6a52013-09-30 18:17:21 +1300105 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200106 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200107 }
108 }
109
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300110 private class HostArpRequester implements IArpRequester {
111 private final ARP arpRequest;
112 private final long dpid;
113 private final short port;
114
115 public HostArpRequester(ARP arpRequest, long dpid, short port) {
116 this.arpRequest = arpRequest;
117 this.dpid = dpid;
118 this.port = port;
119 }
120
121 @Override
122 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
123 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
124 }
125 }
126
Jonathan Harta8887642013-10-28 13:46:54 -0700127 /*
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200128 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Harta8887642013-10-28 13:46:54 -0700129 ITopologyService topology, IConfigInfoService configService,
Jonathan Hart5afde492013-10-01 12:30:53 +1300130 IRestApiService restApi){
Jonathan Harta8887642013-10-28 13:46:54 -0700131
132 }
133 */
134
135 public void init(IFloodlightProviderService floodlightProvider,
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800136 ITopologyService topology, IDatagridService datagrid,
Jonathan Harta18e4792013-10-31 10:10:54 -0700137 IConfigInfoService config, IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200138 this.floodlightProvider = floodlightProvider;
139 this.topology = topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800140 this.datagrid = datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -0700141 this.configService = config;
Jonathan Hart5afde492013-10-01 12:30:53 +1300142 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200143
Jonathan Hartabad6a52013-09-30 18:17:21 +1300144 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200145
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200146 arpRequests = Multimaps.synchronizedSetMultimap(
147 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800148
149 topoSwitchService = new TopoSwitchServiceImpl();
150 topoLinkService = new TopoLinkServiceImpl();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200151 }
152
Jonathan Harta8887642013-10-28 13:46:54 -0700153 public void startUp() {
154 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700155 log.info("vlan set to {}", this.vlan);
156
Jonathan Hart5afde492013-10-01 12:30:53 +1300157 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700158 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300159
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800160 datagrid.registerArpEventHandler(this);
161
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700162 deviceStorage = new DeviceStorageImpl();
163 deviceStorage.init("");
164
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200165 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200166 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200167 @Override
168 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200169 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200170 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200171 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200172 }
173
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200174 /*
175 * Function that runs periodically to manage the asynchronous request mechanism.
176 * It basically cleans up old ARP requests if we don't get a response for them.
177 * The caller can designate that a request should be retried indefinitely, and
178 * this task will handle that as well.
179 */
180 private void doPeriodicArpProcessing() {
181 SetMultimap<InetAddress, ArpRequest> retryList
182 = HashMultimap.<InetAddress, ArpRequest>create();
183
184 //Have to synchronize externally on the Multimap while using an iterator,
185 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200186 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200187 log.debug("Current have {} outstanding requests",
188 arpRequests.size());
189
190 Iterator<Map.Entry<InetAddress, ArpRequest>> it
191 = arpRequests.entries().iterator();
192
193 while (it.hasNext()) {
194 Map.Entry<InetAddress, ArpRequest> entry
195 = it.next();
196 ArpRequest request = entry.getValue();
197 if (request.isExpired()) {
198 log.debug("Cleaning expired ARP request for {}",
199 entry.getKey().getHostAddress());
200
201 it.remove();
202
203 if (request.shouldRetry()) {
204 retryList.put(entry.getKey(), request);
205 }
206 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200207 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200208 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200209
210 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
211 : retryList.asMap().entrySet()) {
212
213 InetAddress address = entry.getKey();
214
215 log.debug("Resending ARP request for {}", address.getHostAddress());
216
217 sendArpRequestForAddress(address);
218
219 for (ArpRequest request : entry.getValue()) {
220 arpRequests.put(address, new ArpRequest(request));
221 }
222 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200223 }
224
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200225 @Override
226 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700227 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200228 }
229
230 @Override
231 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700232 if (type == OFType.PACKET_IN) {
233 return "devicemanager".equals(name);
234 }
235 else {
236 return false;
237 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200238 }
239
240 @Override
241 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200242 return false;
243 }
244
245 @Override
246 public Command receive(
247 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
248
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800249 //if (msg.getType() != OFType.PACKET_IN){
250 //return Command.CONTINUE;
251 //}
252 log.debug("received packet");
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200253
254 OFPacketIn pi = (OFPacketIn) msg;
255
256 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
257 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
258
259 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800260 log.debug("is arp");
261 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200262 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800263 log.debug("is request");
Jonathan Harta18e4792013-10-31 10:10:54 -0700264 //TODO check what the DeviceManager does about propagating
265 //or swallowing ARPs. We want to go after DeviceManager in the
266 //chain but we really need it to CONTINUE ARP packets so we can
267 //get them.
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800268 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200269 }
270 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800271 log.debug("is reply");
Jonathan Harta18e4792013-10-31 10:10:54 -0700272 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200273 }
274 }
275
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200276 //TODO should we propagate ARP or swallow it?
277 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200278 return Command.CONTINUE;
279 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200280
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800281 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200282 if (log.isTraceEnabled()) {
283 log.trace("ARP request received for {}",
284 inetAddressToString(arp.getTargetProtocolAddress()));
285 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200286
287 InetAddress target;
288 try {
289 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
290 } catch (UnknownHostException e) {
291 log.debug("Invalid address in ARP request", e);
292 return;
293 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200294
Jonathan Harta8887642013-10-28 13:46:54 -0700295 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200296 //If the request came from outside our network, we only care if
297 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700298 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200299 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700300 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200301
302 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700303 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200304 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200305
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200306 return;
307 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200308
Jonathan Harta18e4792013-10-31 10:10:54 -0700309 //MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200310
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700311 IDeviceObject targetDevice =
312 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
Jonathan Harta18e4792013-10-31 10:10:54 -0700313
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800314 log.debug("targetDevice: {}", targetDevice);
315
Jonathan Harta18e4792013-10-31 10:10:54 -0700316 if (targetDevice != null) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800317 // We have the device in our database, so send a reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700318 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
319
320 if (log.isTraceEnabled()) {
321 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
322 inetAddressToString(arp.getTargetProtocolAddress()),
323 macAddress.toString(),
324 HexString.toHexString(sw.getId()), pi.getInPort()});
325 }
326
327 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
328 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800329 else {
330 // We don't know the device so broadcast the request out
331 // the edge of the network
332
333 //Record where the request came from so we know where to send the reply
334 arpRequests.put(target, new ArpRequest(
335 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
336
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800337 sendToOtherNodes(eth, pi);
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800338 }
Jonathan Harta18e4792013-10-31 10:10:54 -0700339
340 /*if (macAddress == null){
Jonathan Hartabad6a52013-09-30 18:17:21 +1300341 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200342
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200343 //Record where the request came from so we know where to send the reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700344 //arpRequests.put(target, new ArpRequest(
345 //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200346
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200347 //Flood the request out edge ports
Jonathan Harta18e4792013-10-31 10:10:54 -0700348 //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200349 }
350 else {
351 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200352 if (log.isTraceEnabled()) {
353 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
354 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300355 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200356 HexString.toHexString(sw.getId()), pi.getInPort()});
357 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200358
Jonathan Hartabad6a52013-09-30 18:17:21 +1300359 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Harta18e4792013-10-31 10:10:54 -0700360 }*/
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200361 }
362
Jonathan Hart1912afc2013-10-11 12:02:44 +1300363 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200364 if (log.isTraceEnabled()) {
365 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
366 inetAddressToString(arp.getSenderProtocolAddress()),
367 HexString.toHexString(arp.getSenderHardwareAddress()),
368 HexString.toHexString(sw.getId()), pi.getInPort()});
369 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200370
Jonathan Hartabad6a52013-09-30 18:17:21 +1300371 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200372 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300373 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200374 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200375 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200376 return;
377 }
378
Jonathan Hartabad6a52013-09-30 18:17:21 +1300379 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
380
381 arpCache.update(senderIpAddress, senderMacAddress);
382
383 //See if anyone's waiting for this ARP reply
384 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200385
386 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200387 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200388 synchronized (arpRequests) {
389 Iterator<ArpRequest> it = requests.iterator();
390 while (it.hasNext()) {
391 ArpRequest request = it.next();
392 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200393 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200394 }
395 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200396
397 //Don't hold an ARP lock while dispatching requests
398 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300399 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200400 }
401 }
402
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200403 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200404 //TODO what should the sender IP address and MAC address be if no
405 //IP addresses are configured? Will there ever be a need to send
406 //ARP requests from the controller in that case?
407 //All-zero MAC address doesn't seem to work - hosts don't respond to it
408
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200409 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
410 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200411 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200412 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200413 (byte)0xff, (byte)0xff, (byte)0xff};
414
415 ARP arpRequest = new ARP();
416
417 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
418 .setProtocolType(ARP.PROTO_TYPE_IP)
419 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200420 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200421 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200422 .setTargetHardwareAddress(zeroMac)
423 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200424
Jonathan Harta8887642013-10-28 13:46:54 -0700425 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200426 //TODO hack for now as it's unclear what the MAC address should be
427 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200428 if (routerMacAddress != null) {
429 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200430 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200431 arpRequest.setSenderHardwareAddress(senderMacAddress);
432
433 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700434 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200435 if (intf != null) {
436 senderIPAddress = intf.getIpAddress().getAddress();
437 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200438
439 arpRequest.setSenderProtocolAddress(senderIPAddress);
440
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200441 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200442 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200443 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200444 .setEtherType(Ethernet.TYPE_ARP)
445 .setPayload(arpRequest);
446
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700447 if (vlan != NO_VLAN) {
448 eth.setVlanID(vlan)
449 .setPriorityCode((byte)0);
450 }
451
Jonathan Hart2f790d22013-08-15 14:01:24 +1200452 sendArpRequestToSwitches(ipAddress, eth.serialize());
453 }
454
455 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200456 sendArpRequestToSwitches(dstAddress, arpRequest,
457 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200458 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200459
Jonathan Hart2f790d22013-08-15 14:01:24 +1200460 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
461 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200462
Jonathan Harta8887642013-10-28 13:46:54 -0700463 if (configService.hasLayer3Configuration()) {
464 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200465 if (intf != null) {
466 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
467 }
468 else {
469 //TODO here it should be broadcast out all non-interface edge ports.
470 //I think we can assume that if it's not a request for an external
471 //network, it's an ARP for a host in our own network. So we want to
472 //send it out all edge ports that don't have an interface configured
473 //to ensure it reaches all hosts in our network.
474 log.debug("No interface found to send ARP request for {}",
475 dstAddress.getHostAddress());
476 }
477 }
478 else {
479 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
480 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200481 }
482
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800483 private void sendToOtherNodes(Ethernet eth, OFPacketIn pi) {
484 ARP arp = (ARP) eth.getPayload();
Jonathan Hartd3003252013-11-15 09:44:46 -0800485
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800486 if (log.isTraceEnabled()) {
487 log.trace("Sending ARP request for {} to other ONOS instances",
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800488 inetAddressToString(arp.getTargetProtocolAddress()));
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800489 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800490
491 InetAddress targetAddress;
492 try {
493 targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress());
494 } catch (UnknownHostException e) {
495 log.error("Unknown host", e);
496 return;
497 }
498
499 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize()));
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800500 }
501
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200502 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200503 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
504 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
505 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
506
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200507 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700508 //I think this means the switch doesn't have any links.
509 //continue;
510 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200511 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200512
Jonathan Harta18e4792013-10-31 10:10:54 -0700513
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200514 OFPacketOut po = new OFPacketOut();
515 po.setInPort(OFPort.OFPP_NONE)
516 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200517 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200518
519 List<OFAction> actions = new ArrayList<OFAction>();
520
521 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200522 if (linkPorts.contains(portNum) ||
523 (sw.getId() == inSwitch && portNum == inPort)){
524 //If this port isn't an edge port or is the ingress port
525 //for the ARP, don't broadcast out it
526 continue;
527 }
528
529 actions.add(new OFActionOutput(portNum));
530 }
531
532 po.setActions(actions);
533 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
534 po.setActionsLength(actionsLength);
535 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200536 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200537
538 List<OFMessage> msgList = new ArrayList<OFMessage>();
539 msgList.add(po);
540
541 try {
542 sw.write(msgList, null);
543 sw.flush();
544 } catch (IOException e) {
545 log.error("Failure writing packet out to switch", e);
546 }
547 }
548 }
549
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800550 private void broadcastArpRequestOutMyEdge(byte[] arpRequest) {
551 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
552
553 OFPacketOut po = new OFPacketOut();
554 po.setInPort(OFPort.OFPP_NONE)
555 .setBufferId(-1)
556 .setPacketData(arpRequest);
557
558 List<OFAction> actions = new ArrayList<OFAction>();
559
560 Iterable<IPortObject> ports
561 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
562 if (ports == null) {
563 continue;
564 }
565
566 for (IPortObject portObject : ports) {
567 if (!portObject.getLinkedPorts().iterator().hasNext()) {
568 actions.add(new OFActionOutput(portObject.getNumber()));
569 }
570 }
571
572 po.setActions(actions);
573 short actionsLength = (short)
574 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
575 po.setActionsLength(actionsLength);
576 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
577 + arpRequest.length);
578
579 try {
580 sw.write(po, null);
581 sw.flush();
582 } catch (IOException e) {
583 log.error("Failure writing packet out to switch", e);
584 }
585 }
586 }
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 Hart18ad55c2013-11-11 22:49:55 -0800725 log.debug("Received ARP notification from other instances");
Jonathan Hartd3003252013-11-15 09:44:46 -0800726
727 switch (arpMessage.getType()){
728 case REQUEST:
729 broadcastArpRequestOutMyEdge(arpMessage.getPacket());
730 break;
731 case REPLY:
732 sendArpReplyToWaitingRequesters(arpMessage.getAddress());
733 break;
734 }
735 }
736
737 private void sendArpReplyToWaitingRequesters(InetAddress address) {
738 log.debug("Sending ARP reply for {} to requesters",
739 address.getHostAddress());
740
741 //See if anyone's waiting for this ARP reply
742 Set<ArpRequest> requests = arpRequests.get(address);
743
744 //Synchronize on the Multimap while using an iterator for one of the sets
745 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
746 synchronized (arpRequests) {
747 Iterator<ArpRequest> it = requests.iterator();
748 while (it.hasNext()) {
749 ArpRequest request = it.next();
750 it.remove();
751 requestsToSend.add(request);
752 }
753 }
754
755 IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
756 InetAddresses.coerceToInteger(address));
757
758 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
759
760 log.debug("Found {} at {} in network map",
761 address.getHostAddress(), mac);
762
763 //Don't hold an ARP lock while dispatching requests
764 for (ArpRequest request : requestsToSend) {
765 request.dispatchReply(address, mac);
766 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800767 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200768}