blob: 32b2e9cd7de449e06bb23afaccbb1ebc3f52d61c [file] [log] [blame]
Jonathan Hartc7ca35d2013-06-25 20:54:25 +12001package net.onrc.onos.ofcontroller.proxyarp;
2
3import java.io.IOException;
4import java.net.InetAddress;
5import java.net.UnknownHostException;
6import java.util.ArrayList;
7import java.util.Collection;
Jonathan Harte93aed42013-12-05 18:39:50 -08008import java.util.HashMap;
Jonathan Harta18e4792013-10-31 10:10:54 -07009import java.util.HashSet;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120010import java.util.Iterator;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120011import java.util.List;
12import java.util.Map;
13import java.util.Set;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120014import java.util.Timer;
15import java.util.TimerTask;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120016
17import net.floodlightcontroller.core.FloodlightContext;
18import net.floodlightcontroller.core.IFloodlightProviderService;
19import net.floodlightcontroller.core.IOFMessageListener;
20import net.floodlightcontroller.core.IOFSwitch;
Jonathan Harte93aed42013-12-05 18:39:50 -080021import net.floodlightcontroller.core.module.FloodlightModuleContext;
22import net.floodlightcontroller.core.module.IFloodlightModule;
23import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120024import net.floodlightcontroller.packet.ARP;
25import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart08ee8522013-09-22 17:34:43 +120026import net.floodlightcontroller.packet.IPv4;
Jonathan Hart5afde492013-10-01 12:30:53 +130027import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120028import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120029import net.floodlightcontroller.util.MACAddress;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080030import net.onrc.onos.datagrid.IDatagridService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120031import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070032import net.onrc.onos.ofcontroller.core.IDeviceStorage;
33import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080034import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
pingping-lin017a8922013-12-11 11:15:33 +080035import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080036import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoSwitchService;
Jonathan Hartebba1e12013-10-29 11:37:02 -070037import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070038import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080039import net.onrc.onos.ofcontroller.core.internal.TopoSwitchServiceImpl;
Jonathan Hartba9ced92013-11-24 16:52:13 -080040import net.onrc.onos.ofcontroller.util.Dpid;
41import net.onrc.onos.ofcontroller.util.Port;
42import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120043
44import org.openflow.protocol.OFMessage;
45import org.openflow.protocol.OFPacketIn;
46import org.openflow.protocol.OFPacketOut;
47import org.openflow.protocol.OFPort;
48import org.openflow.protocol.OFType;
49import org.openflow.protocol.action.OFAction;
50import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120051import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120052import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
54
Jonathan Hart4dfc3652013-08-02 20:22:36 +120055import com.google.common.collect.HashMultimap;
56import com.google.common.collect.Multimaps;
57import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070058import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120059
Jonathan Hart18ad55c2013-11-11 22:49:55 -080060public class ProxyArpManager implements IProxyArpService, IOFMessageListener,
Jonathan Harte93aed42013-12-05 18:39:50 -080061 IArpEventHandler, IFloodlightModule {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120062 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120063
pingping-lin017a8922013-12-11 11:15:33 +080064 private final long ARP_TIMER_PERIOD = 100; //ms
65
Jonathan Hart18ad9502013-12-15 18:28:00 -080066 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120067
Jonathan Harta8887642013-10-28 13:46:54 -070068 private IFloodlightProviderService floodlightProvider;
69 private ITopologyService topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080070 private IDatagridService datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -070071 private IConfigInfoService configService;
72 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120073
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070074 private IDeviceStorage deviceStorage;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080075 private volatile ITopoSwitchService topoSwitchService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070076
Jonathan Hart1cf9de02013-10-21 17:42:29 -070077 private short vlan;
78 private static final short NO_VLAN = 0;
79
Jonathan Hart18ad9502013-12-15 18:28:00 -080080 //private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120081
Jonathan Harta8887642013-10-28 13:46:54 -070082 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120083
Jonathan Hartabad6a52013-09-30 18:17:21 +130084 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120085 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130086 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120087 private long requestTime;
88
Jonathan Hart4dfc3652013-08-02 20:22:36 +120089 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120090 this.requester = requester;
91 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120092 this.requestTime = System.currentTimeMillis();
93 }
94
Jonathan Hart4dfc3652013-08-02 20:22:36 +120095 public ArpRequest(ArpRequest old) {
96 this.requester = old.requester;
97 this.retry = old.retry;
98 this.requestTime = System.currentTimeMillis();
99 }
100
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200101 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300102 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200103 }
104
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200105 public boolean shouldRetry() {
106 return retry;
107 }
108
Jonathan Hartabad6a52013-09-30 18:17:21 +1300109 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200110 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200111 }
112 }
113
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300114 private class HostArpRequester implements IArpRequester {
115 private final ARP arpRequest;
116 private final long dpid;
117 private final short port;
118
119 public HostArpRequester(ARP arpRequest, long dpid, short port) {
120 this.arpRequest = arpRequest;
121 this.dpid = dpid;
122 this.port = port;
123 }
124
125 @Override
126 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
127 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
128 }
129 }
130
Jonathan Harte93aed42013-12-05 18:39:50 -0800131 @Override
132 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
133 Collection<Class<? extends IFloodlightService>> l
134 = new ArrayList<Class<? extends IFloodlightService>>();
135 l.add(IProxyArpService.class);
136 return l;
Jonathan Harta8887642013-10-28 13:46:54 -0700137 }
Jonathan Harte93aed42013-12-05 18:39:50 -0800138
139 @Override
140 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
141 Map<Class<? extends IFloodlightService>, IFloodlightService> m
142 = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
143 m.put(IProxyArpService.class, this);
144 return m;
145 }
146
147 @Override
148 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
149 Collection<Class<? extends IFloodlightService>> dependencies
150 = new ArrayList<Class<? extends IFloodlightService>>();
151 dependencies.add(IFloodlightProviderService.class);
152 dependencies.add(ITopologyService.class);
153 dependencies.add(IRestApiService.class);
154 dependencies.add(IDatagridService.class);
155 dependencies.add(IConfigInfoService.class);
156 return dependencies;
157 }
Jonathan Harta8887642013-10-28 13:46:54 -0700158
Jonathan Harte93aed42013-12-05 18:39:50 -0800159 @Override
160 public void init(FloodlightModuleContext context){
161 this.floodlightProvider =
162 context.getServiceImpl(IFloodlightProviderService.class);
163 this.topology = context.getServiceImpl(ITopologyService.class);
164 this.datagrid = context.getServiceImpl(IDatagridService.class);
165 this.configService = context.getServiceImpl(IConfigInfoService.class);
166 this.restApi = context.getServiceImpl(IRestApiService.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200167
Jonathan Hart18ad9502013-12-15 18:28:00 -0800168 //arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200169
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200170 arpRequests = Multimaps.synchronizedSetMultimap(
171 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800172
173 topoSwitchService = new TopoSwitchServiceImpl();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200174 }
175
Jonathan Harte93aed42013-12-05 18:39:50 -0800176 @Override
177 public void startUp(FloodlightModuleContext context) {
Jonathan Harta8887642013-10-28 13:46:54 -0700178 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700179 log.info("vlan set to {}", this.vlan);
180
Jonathan Hart5afde492013-10-01 12:30:53 +1300181 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700182 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300183
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800184 datagrid.registerArpEventHandler(this);
185
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700186 deviceStorage = new DeviceStorageImpl();
187 deviceStorage.init("");
188
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200189 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200190 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200191 @Override
192 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200193 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200194 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200195 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200196 }
197
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200198 /*
199 * Function that runs periodically to manage the asynchronous request mechanism.
200 * It basically cleans up old ARP requests if we don't get a response for them.
201 * The caller can designate that a request should be retried indefinitely, and
202 * this task will handle that as well.
203 */
204 private void doPeriodicArpProcessing() {
205 SetMultimap<InetAddress, ArpRequest> retryList
206 = HashMultimap.<InetAddress, ArpRequest>create();
207
208 //Have to synchronize externally on the Multimap while using an iterator,
209 //even though it's a synchronizedMultimap
Jonathan Hart18ad9502013-12-15 18:28:00 -0800210 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200211 Iterator<Map.Entry<InetAddress, ArpRequest>> it
212 = arpRequests.entries().iterator();
213
214 while (it.hasNext()) {
215 Map.Entry<InetAddress, ArpRequest> entry
216 = it.next();
217 ArpRequest request = entry.getValue();
218 if (request.isExpired()) {
219 log.debug("Cleaning expired ARP request for {}",
220 entry.getKey().getHostAddress());
221
Jonathan Hart18ad9502013-12-15 18:28:00 -0800222 // If the ARP request is expired and then delete the device
223 // TODO check whether this is OK from this thread
pingping-lin017a8922013-12-11 11:15:33 +0800224 IDeviceObject targetDevice =
225 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(entry.getKey()));
226
Jonathan Hart18ad9502013-12-15 18:28:00 -0800227 if (targetDevice != null) {
228 deviceStorage.removeDevice(targetDevice);
229 log.debug("RemoveDevice: {} due to no have not recieve the ARP reply", targetDevice.toString());
Jonathan Hartd857ad62013-12-14 18:08:17 -0800230 }
pingping-lin017a8922013-12-11 11:15:33 +0800231
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200232 it.remove();
233
234 if (request.shouldRetry()) {
235 retryList.put(entry.getKey(), request);
236 }
237 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200238 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200239 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200240
241 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
242 : retryList.asMap().entrySet()) {
243
244 InetAddress address = entry.getKey();
245
246 log.debug("Resending ARP request for {}", address.getHostAddress());
247
248 sendArpRequestForAddress(address);
249
250 for (ArpRequest request : entry.getValue()) {
251 arpRequests.put(address, new ArpRequest(request));
252 }
253 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200254 }
255
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200256 @Override
257 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700258 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200259 }
260
261 @Override
262 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700263 if (type == OFType.PACKET_IN) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800264 return "devicemanager".equals(name) || "onosdevicemanager".equals(name);
Jonathan Harta18e4792013-10-31 10:10:54 -0700265 }
266 else {
267 return false;
268 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200269 }
270
271 @Override
272 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800273 return type == OFType.PACKET_IN && "onosforwarding".equals(name);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200274 }
275
276 @Override
277 public Command receive(
278 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
279
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200280 OFPacketIn pi = (OFPacketIn) msg;
281
282 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
283 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
284
285 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800286 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200287 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800288 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200289 }
290 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800291 handleArpReply(sw, pi, arp);
292 sendToOtherNodesReply(eth, pi);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200293 }
Jonathan Hart17672992013-12-12 16:15:16 -0800294
295 // Stop ARP packets here
296 return Command.STOP;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200297 }
298
Jonathan Hart17672992013-12-12 16:15:16 -0800299 // Propagate everything else
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200300 return Command.CONTINUE;
301 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200302
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800303 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200304 if (log.isTraceEnabled()) {
305 log.trace("ARP request received for {}",
306 inetAddressToString(arp.getTargetProtocolAddress()));
307 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200308
309 InetAddress target;
310 try {
pingping-linb8757bf2013-12-13 01:48:58 +0800311 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200312 } catch (UnknownHostException e) {
313 log.debug("Invalid address in ARP request", e);
314 return;
315 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200316
Jonathan Harta8887642013-10-28 13:46:54 -0700317 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200318 //If the request came from outside our network, we only care if
319 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700320 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200321 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700322 target.getHostAddress(), configService.getRouterMacAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800323
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200324 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700325 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200326 }
pingping-linb8757bf2013-12-13 01:48:58 +0800327
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200328 return;
329 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200330
Jonathan Harta18e4792013-10-31 10:10:54 -0700331 //MACAddress macAddress = arpCache.lookup(target);
pingping-linb8757bf2013-12-13 01:48:58 +0800332
333 arpRequests.put(target, new ArpRequest(
334 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
335
Jonathan Hart18ad9502013-12-15 18:28:00 -0800336 IDeviceObject targetDevice =
337 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
338
339 if (targetDevice == null) {
340 if (log.isTraceEnabled()) {
341 log.trace("No device info found for {} - broadcasting",
342 target.getHostAddress());
343 }
344
345 // We don't know the device so broadcast the request out
346 sendToOtherNodes(eth, sw.getId(), pi);
347 }
348 else {
349 // Even if the device exists in our database, we do not reply to
350 // the request directly, but check whether the device is still valid
Jonathan Harta18e4792013-10-31 10:10:54 -0700351 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800352
Jonathan Harta18e4792013-10-31 10:10:54 -0700353 if (log.isTraceEnabled()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800354 log.trace("The target Device Record in DB is: {} => {} from ARP request host at {}/{}",
355 new Object [] {
pingping-linb8757bf2013-12-13 01:48:58 +0800356 inetAddressToString(arp.getTargetProtocolAddress()),
357 macAddress.toString(),
358 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Harta18e4792013-10-31 10:10:54 -0700359 }
pingping-linb8757bf2013-12-13 01:48:58 +0800360
361 // sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
362
363 log.trace("Checking the device info from DB is still valid or not");
Jonathan Hart18ad9502013-12-15 18:28:00 -0800364 Iterable<IPortObject> outPorts = targetDevice.getAttachedPorts();
pingping-linb8757bf2013-12-13 01:48:58 +0800365
Jonathan Hart18ad9502013-12-15 18:28:00 -0800366 if (!outPorts.iterator().hasNext()){
367 if (log.isTraceEnabled()) {
368 log.trace("Device {} exists but is not connected to any ports" +
369 " - broadcasting", macAddress);
370 }
371
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800372 sendToOtherNodes(eth, sw.getId(), pi);
Jonathan Hart18ad9502013-12-15 18:28:00 -0800373 }
374 else {
pingping-linb8757bf2013-12-13 01:48:58 +0800375 for (IPortObject portObject : outPorts) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800376 long outSwitch = 0;
377 short outPort = 0;
pingping-linb8757bf2013-12-13 01:48:58 +0800378
379 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800380 outPort = portObject.getNumber();
381 }
pingping-linb8757bf2013-12-13 01:48:58 +0800382
pingping-lin568c6012013-12-14 05:57:11 +0800383 ISwitchObject outSwitchObject = portObject.getSwitch();
Jonathan Hart18ad9502013-12-15 18:28:00 -0800384 outSwitch = HexString.toLong(outSwitchObject.getDPID());
385
386 if (log.isTraceEnabled()) {
387 log.trace("Probing device {} on port {}/{}",
388 new Object[] {macAddress,
389 HexString.toHexString(outSwitch), outPort});
390 }
pingping-lin568c6012013-12-14 05:57:11 +0800391
Jonathan Hart18ad9502013-12-15 18:28:00 -0800392 sendToOtherNodes(eth, pi, outSwitch, outPort);
pingping-linb8757bf2013-12-13 01:48:58 +0800393 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200394 }
pingping-lin017a8922013-12-11 11:15:33 +0800395 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200396 }
397
Jonathan Hart1912afc2013-10-11 12:02:44 +1300398 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200399 if (log.isTraceEnabled()) {
400 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
401 inetAddressToString(arp.getSenderProtocolAddress()),
402 HexString.toHexString(arp.getSenderHardwareAddress()),
403 HexString.toHexString(sw.getId()), pi.getInPort()});
404 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200405
Jonathan Hartabad6a52013-09-30 18:17:21 +1300406 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200407 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300408 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200409 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200410 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200411 return;
412 }
413
Jonathan Hartabad6a52013-09-30 18:17:21 +1300414 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
415
pingping-lin017a8922013-12-11 11:15:33 +0800416 //arpCache.update(senderIpAddress, senderMacAddress);
Jonathan Hartabad6a52013-09-30 18:17:21 +1300417
418 //See if anyone's waiting for this ARP reply
419 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200420
421 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200422 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200423 synchronized (arpRequests) {
424 Iterator<ArpRequest> it = requests.iterator();
425 while (it.hasNext()) {
426 ArpRequest request = it.next();
427 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200428 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200429 }
430 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200431
432 //Don't hold an ARP lock while dispatching requests
433 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300434 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200435 }
436 }
437
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200438 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200439 //TODO what should the sender IP address and MAC address be if no
440 //IP addresses are configured? Will there ever be a need to send
441 //ARP requests from the controller in that case?
442 //All-zero MAC address doesn't seem to work - hosts don't respond to it
443
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200444 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
445 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200446 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200447 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200448 (byte)0xff, (byte)0xff, (byte)0xff};
449
450 ARP arpRequest = new ARP();
451
452 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
453 .setProtocolType(ARP.PROTO_TYPE_IP)
454 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200455 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200456 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200457 .setTargetHardwareAddress(zeroMac)
458 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200459
Jonathan Harta8887642013-10-28 13:46:54 -0700460 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200461 //TODO hack for now as it's unclear what the MAC address should be
462 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200463 if (routerMacAddress != null) {
464 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200465 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200466 arpRequest.setSenderHardwareAddress(senderMacAddress);
467
468 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700469 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200470 if (intf != null) {
471 senderIPAddress = intf.getIpAddress().getAddress();
472 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200473
474 arpRequest.setSenderProtocolAddress(senderIPAddress);
475
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200476 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200477 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200478 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200479 .setEtherType(Ethernet.TYPE_ARP)
480 .setPayload(arpRequest);
481
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700482 if (vlan != NO_VLAN) {
483 eth.setVlanID(vlan)
484 .setPriorityCode((byte)0);
485 }
486
Jonathan Hart2f790d22013-08-15 14:01:24 +1200487 sendArpRequestToSwitches(ipAddress, eth.serialize());
488 }
489
490 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200491 sendArpRequestToSwitches(dstAddress, arpRequest,
492 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200493 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200494
Jonathan Hart2f790d22013-08-15 14:01:24 +1200495 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
496 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200497
Jonathan Harta8887642013-10-28 13:46:54 -0700498 if (configService.hasLayer3Configuration()) {
499 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200500 if (intf != null) {
501 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
502 }
503 else {
504 //TODO here it should be broadcast out all non-interface edge ports.
505 //I think we can assume that if it's not a request for an external
506 //network, it's an ARP for a host in our own network. So we want to
507 //send it out all edge ports that don't have an interface configured
508 //to ensure it reaches all hosts in our network.
509 log.debug("No interface found to send ARP request for {}",
510 dstAddress.getHostAddress());
511 }
512 }
513 else {
514 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
515 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200516 }
517
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800518 private void sendToOtherNodes(Ethernet eth, long inSwitchId, OFPacketIn pi) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800519 ARP arp = (ARP) eth.getPayload();
Jonathan Hartd3003252013-11-15 09:44:46 -0800520
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800521 if (log.isTraceEnabled()) {
522 log.trace("Sending ARP request for {} to other ONOS instances",
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800523 inetAddressToString(arp.getTargetProtocolAddress()));
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800524 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800525
526 InetAddress targetAddress;
527 try {
528 targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress());
529 } catch (UnknownHostException e) {
530 log.error("Unknown host", e);
531 return;
532 }
533
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800534 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize(),
535 -1L, (short)-1, inSwitchId, pi.getInPort()));
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800536 }
Jonathan Hart18ad9502013-12-15 18:28:00 -0800537
pingping-lin017a8922013-12-11 11:15:33 +0800538 //hazelcast to other ONOS instances to send the ARP packet out on outPort of outSwitch
539 private void sendToOtherNodes(Ethernet eth, OFPacketIn pi, long outSwitch, short outPort) {
540 ARP arp = (ARP) eth.getPayload();
541
542 if (log.isTraceEnabled()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800543 log.trace("Sending ARP request for {} to other ONOS instances with outSwitch {} ",
544 inetAddressToString(arp.getTargetProtocolAddress()), String.valueOf(outSwitch));
pingping-lin017a8922013-12-11 11:15:33 +0800545 }
546
547 InetAddress targetAddress;
548 try {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800549 targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800550 } catch (UnknownHostException e) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800551 log.error("Unknown host", e);
552 return;
pingping-lin017a8922013-12-11 11:15:33 +0800553 }
554
555 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize(), outSwitch, outPort));
556 //datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize()));
557
558
559 }
Jonathan Hart18ad9502013-12-15 18:28:00 -0800560
pingping-lin017a8922013-12-11 11:15:33 +0800561 private void sendToOtherNodesReply(Ethernet eth, OFPacketIn pi) {
562 ARP arp = (ARP) eth.getPayload();
563
564 if (log.isTraceEnabled()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800565 log.trace("Sending ARP reply for {} to other ONOS instances",
566 inetAddressToString(arp.getSenderProtocolAddress()));
pingping-lin017a8922013-12-11 11:15:33 +0800567 }
568
569 InetAddress targetAddress;
570 MACAddress mac = new MACAddress(arp.getSenderHardwareAddress());
571
572 try {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800573 targetAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800574 } catch (UnknownHostException e) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800575 log.error("Unknown host", e);
576 return;
pingping-lin017a8922013-12-11 11:15:33 +0800577 }
578
Jonathan Hart18ad9502013-12-15 18:28:00 -0800579 datagrid.sendArpRequest(ArpMessage.newReply(targetAddress, mac));
pingping-lin017a8922013-12-11 11:15:33 +0800580 //datagrid.sendArpReply(ArpMessage.newRequest(targetAddress, eth.serialize()));
581
582 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800583
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200584 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200585 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
586 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
587 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
588
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200589 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700590 //I think this means the switch doesn't have any links.
591 //continue;
592 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200593 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200594
Jonathan Harta18e4792013-10-31 10:10:54 -0700595
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200596 OFPacketOut po = new OFPacketOut();
597 po.setInPort(OFPort.OFPP_NONE)
598 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200599 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200600
601 List<OFAction> actions = new ArrayList<OFAction>();
602
603 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200604 if (linkPorts.contains(portNum) ||
605 (sw.getId() == inSwitch && portNum == inPort)){
606 //If this port isn't an edge port or is the ingress port
607 //for the ARP, don't broadcast out it
608 continue;
609 }
610
611 actions.add(new OFActionOutput(portNum));
612 }
613
614 po.setActions(actions);
615 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
616 po.setActionsLength(actionsLength);
617 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200618 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200619
620 List<OFMessage> msgList = new ArrayList<OFMessage>();
621 msgList.add(po);
622
623 try {
624 sw.write(msgList, null);
625 sw.flush();
626 } catch (IOException e) {
627 log.error("Failure writing packet out to switch", e);
628 }
629 }
630 }
631
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800632 private void broadcastArpRequestOutMyEdge(byte[] arpRequest,
633 long inSwitch, short inPort) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800634 List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
635
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800636 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
637
638 OFPacketOut po = new OFPacketOut();
639 po.setInPort(OFPort.OFPP_NONE)
640 .setBufferId(-1)
641 .setPacketData(arpRequest);
642
643 List<OFAction> actions = new ArrayList<OFAction>();
644
645 Iterable<IPortObject> ports
646 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
647 if (ports == null) {
648 continue;
649 }
650
651 for (IPortObject portObject : ports) {
652 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800653 short portNumber = portObject.getNumber();
654
655 if (sw.getId() == inSwitch && portNumber == inPort) {
656 // This is the port that the ARP message came in,
657 // so don't broadcast out this port
658 continue;
659 }
660
Jonathan Hartba9ced92013-11-24 16:52:13 -0800661 switchPorts.add(new SwitchPort(new Dpid(sw.getId()),
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800662 new Port(portNumber)));
663 actions.add(new OFActionOutput(portNumber));
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800664 }
665 }
666
667 po.setActions(actions);
668 short actionsLength = (short)
669 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
670 po.setActionsLength(actionsLength);
671 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
672 + arpRequest.length);
673
674 try {
675 sw.write(po, null);
676 sw.flush();
677 } catch (IOException e) {
678 log.error("Failure writing packet out to switch", e);
679 }
680 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800681
Jonathan Hart18ad9502013-12-15 18:28:00 -0800682 if (log.isTraceEnabled()) {
683 log.trace("Broadcast ARP request to: {}", switchPorts);
684 }
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800685 }
686
Jonathan Hart2f790d22013-08-15 14:01:24 +1200687 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200688 if (log.isTraceEnabled()) {
689 log.trace("Sending ARP request out {}/{}",
690 HexString.toHexString(dpid), port);
691 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200692
693 OFPacketOut po = new OFPacketOut();
694 po.setInPort(OFPort.OFPP_NONE)
695 .setBufferId(-1)
696 .setPacketData(arpRequest);
697
698 List<OFAction> actions = new ArrayList<OFAction>();
699 actions.add(new OFActionOutput(port));
700 po.setActions(actions);
701 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
702 po.setActionsLength(actionsLength);
703 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
704 + arpRequest.length);
705
706 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
707
708 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200709 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200710 return;
711 }
712
713 try {
714 sw.write(po, null);
715 sw.flush();
716 } catch (IOException e) {
717 log.error("Failure writing packet out to switch", e);
718 }
719 }
720
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300721 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200722 if (log.isTraceEnabled()) {
723 log.trace("Sending reply {} => {} to {}", new Object[] {
724 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300725 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200726 inetAddressToString(arpRequest.getSenderProtocolAddress())});
727 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200728
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200729 ARP arpReply = new ARP();
730 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
731 .setProtocolType(ARP.PROTO_TYPE_IP)
732 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200733 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200734 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300735 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200736 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
737 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
738 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
739
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700740
741
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200742 Ethernet eth = new Ethernet();
743 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300744 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200745 .setEtherType(Ethernet.TYPE_ARP)
746 .setPayload(arpReply);
747
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700748 if (vlan != NO_VLAN) {
749 eth.setVlanID(vlan)
750 .setPriorityCode((byte)0);
751 }
752
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200753 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200754 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200755
756 OFPacketOut po = new OFPacketOut();
757 po.setInPort(OFPort.OFPP_NONE)
758 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200759 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200760 .setActions(actions)
761 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
762 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
763 + po.getPacketData().length);
764
765 List<OFMessage> msgList = new ArrayList<OFMessage>();
766 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200767
768 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
769
770 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200771 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200772 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200773 return;
774 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200775
776 try {
777 sw.write(msgList, null);
778 sw.flush();
779 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200780 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200781 }
782 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300783
784 private String inetAddressToString(byte[] bytes) {
785 try {
786 return InetAddress.getByAddress(bytes).getHostAddress();
787 } catch (UnknownHostException e) {
788 log.debug("Invalid IP address", e);
789 return "";
790 }
791 }
792
793 /*
794 * IProxyArpService methods
795 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200796
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200797 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300798 public MACAddress getMacAddress(InetAddress ipAddress) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800799 //return arpCache.lookup(ipAddress);
800 return null;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200801 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200802
803 @Override
804 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
805 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200806 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200807
Jonathan Hart6e618212013-08-21 22:28:43 +1200808 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700809 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200810 sendArpRequestForAddress(ipAddress);
811 }
812 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300813
814 @Override
815 public List<String> getMappings() {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800816 //return arpCache.getMappings();
817 return new ArrayList<String>();
Jonathan Hart5afde492013-10-01 12:30:53 +1300818 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800819
820 /*
821 * IArpEventHandler methods
822 */
823
824 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800825 public void arpRequestNotification(ArpMessage arpMessage) {
pingping-lin017a8922013-12-11 11:15:33 +0800826 log.debug("Received ARP notification from other instances");
pingping-linb8757bf2013-12-13 01:48:58 +0800827
Jonathan Hartd3003252013-11-15 09:44:46 -0800828 switch (arpMessage.getType()){
829 case REQUEST:
pingping-linb8757bf2013-12-13 01:48:58 +0800830 if(arpMessage.getOutSwitch() == -1 || arpMessage.getOutPort() == -1){
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800831 broadcastArpRequestOutMyEdge(arpMessage.getPacket(),
832 arpMessage.getInSwitch(), arpMessage.getInPort());
pingping-linb8757bf2013-12-13 01:48:58 +0800833 }else{
834 sendArpRequestOutPort(arpMessage.getPacket(),arpMessage.getOutSwitch(),arpMessage.getOutPort());
835 log.debug("OutSwitch in ARP request message is: {}; OutPort in ARP request message is: {}",arpMessage.getOutSwitch(),arpMessage.getOutPort());
836 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800837 break;
838 case REPLY:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800839 log.debug("Received ARP reply notification for {}",
840 arpMessage.getAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800841 sendArpReplyToWaitingRequesters(arpMessage.getAddress(),arpMessage.getMAC());
Jonathan Hartd3003252013-11-15 09:44:46 -0800842 break;
843 }
844 }
845
pingping-lin017a8922013-12-11 11:15:33 +0800846 private void sendArpReplyToWaitingRequesters(InetAddress address, MACAddress mac) {
Jonathan Hartd3003252013-11-15 09:44:46 -0800847 log.debug("Sending ARP reply for {} to requesters",
848 address.getHostAddress());
849
850 //See if anyone's waiting for this ARP reply
851 Set<ArpRequest> requests = arpRequests.get(address);
852
853 //Synchronize on the Multimap while using an iterator for one of the sets
854 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
855 synchronized (arpRequests) {
856 Iterator<ArpRequest> it = requests.iterator();
857 while (it.hasNext()) {
858 ArpRequest request = it.next();
859 it.remove();
860 requestsToSend.add(request);
861 }
862 }
863
pingping-lin017a8922013-12-11 11:15:33 +0800864 /*IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
Jonathan Hartd3003252013-11-15 09:44:46 -0800865 InetAddresses.coerceToInteger(address));
866
867 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
868
869 log.debug("Found {} at {} in network map",
pingping-lin017a8922013-12-11 11:15:33 +0800870 address.getHostAddress(), mac);*/
Jonathan Hartd3003252013-11-15 09:44:46 -0800871
872 //Don't hold an ARP lock while dispatching requests
873 for (ArpRequest request : requestsToSend) {
874 request.dispatchReply(address, mac);
875 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800876 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200877}