blob: 289e0e2a9e55746918c4afe870a1bf407a5ff442 [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);
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800229 if (log.isDebugEnabled()) {
230 log.debug("RemoveDevice: {} due to no have not recieve the ARP reply", targetDevice);
231 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800232 }
pingping-lin017a8922013-12-11 11:15:33 +0800233
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200234 it.remove();
235
236 if (request.shouldRetry()) {
237 retryList.put(entry.getKey(), request);
238 }
239 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200240 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200241 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200242
243 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
244 : retryList.asMap().entrySet()) {
245
246 InetAddress address = entry.getKey();
247
248 log.debug("Resending ARP request for {}", address.getHostAddress());
249
250 sendArpRequestForAddress(address);
251
252 for (ArpRequest request : entry.getValue()) {
253 arpRequests.put(address, new ArpRequest(request));
254 }
255 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200256 }
257
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200258 @Override
259 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700260 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200261 }
262
263 @Override
264 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700265 if (type == OFType.PACKET_IN) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800266 return "devicemanager".equals(name) || "onosdevicemanager".equals(name);
Jonathan Harta18e4792013-10-31 10:10:54 -0700267 }
268 else {
269 return false;
270 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200271 }
272
273 @Override
274 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800275 return type == OFType.PACKET_IN && "onosforwarding".equals(name);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200276 }
277
278 @Override
279 public Command receive(
280 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
281
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200282 OFPacketIn pi = (OFPacketIn) msg;
283
284 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
285 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
286
287 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800288 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200289 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800290 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200291 }
292 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800293 handleArpReply(sw, pi, arp);
294 sendToOtherNodesReply(eth, pi);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200295 }
Jonathan Hart17672992013-12-12 16:15:16 -0800296
297 // Stop ARP packets here
298 return Command.STOP;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200299 }
300
Jonathan Hart17672992013-12-12 16:15:16 -0800301 // Propagate everything else
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200302 return Command.CONTINUE;
303 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200304
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800305 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200306 if (log.isTraceEnabled()) {
307 log.trace("ARP request received for {}",
308 inetAddressToString(arp.getTargetProtocolAddress()));
309 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200310
311 InetAddress target;
312 try {
pingping-linb8757bf2013-12-13 01:48:58 +0800313 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200314 } catch (UnknownHostException e) {
315 log.debug("Invalid address in ARP request", e);
316 return;
317 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200318
Jonathan Harta8887642013-10-28 13:46:54 -0700319 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200320 //If the request came from outside our network, we only care if
321 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700322 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200323 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700324 target.getHostAddress(), configService.getRouterMacAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800325
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200326 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700327 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200328 }
pingping-linb8757bf2013-12-13 01:48:58 +0800329
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200330 return;
331 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200332
Jonathan Harta18e4792013-10-31 10:10:54 -0700333 //MACAddress macAddress = arpCache.lookup(target);
pingping-linb8757bf2013-12-13 01:48:58 +0800334
335 arpRequests.put(target, new ArpRequest(
336 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
337
Jonathan Hart18ad9502013-12-15 18:28:00 -0800338 IDeviceObject targetDevice =
339 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
340
341 if (targetDevice == null) {
342 if (log.isTraceEnabled()) {
343 log.trace("No device info found for {} - broadcasting",
344 target.getHostAddress());
345 }
346
347 // We don't know the device so broadcast the request out
348 sendToOtherNodes(eth, sw.getId(), pi);
349 }
350 else {
351 // Even if the device exists in our database, we do not reply to
352 // the request directly, but check whether the device is still valid
Jonathan Harta18e4792013-10-31 10:10:54 -0700353 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800354
Jonathan Harta18e4792013-10-31 10:10:54 -0700355 if (log.isTraceEnabled()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800356 log.trace("The target Device Record in DB is: {} => {} from ARP request host at {}/{}",
Jonathan Hart18ad9502013-12-15 18:28:00 -0800357 new Object [] {
pingping-linb8757bf2013-12-13 01:48:58 +0800358 inetAddressToString(arp.getTargetProtocolAddress()),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800359 macAddress,
pingping-linb8757bf2013-12-13 01:48:58 +0800360 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Harta18e4792013-10-31 10:10:54 -0700361 }
pingping-linb8757bf2013-12-13 01:48:58 +0800362
363 // sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
364
365 log.trace("Checking the device info from DB is still valid or not");
Jonathan Hart18ad9502013-12-15 18:28:00 -0800366 Iterable<IPortObject> outPorts = targetDevice.getAttachedPorts();
pingping-linb8757bf2013-12-13 01:48:58 +0800367
Jonathan Hart18ad9502013-12-15 18:28:00 -0800368 if (!outPorts.iterator().hasNext()){
369 if (log.isTraceEnabled()) {
370 log.trace("Device {} exists but is not connected to any ports" +
371 " - broadcasting", macAddress);
372 }
373
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800374 sendToOtherNodes(eth, sw.getId(), pi);
Jonathan Hart18ad9502013-12-15 18:28:00 -0800375 }
376 else {
pingping-linb8757bf2013-12-13 01:48:58 +0800377 for (IPortObject portObject : outPorts) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800378 long outSwitch = 0;
379 short outPort = 0;
pingping-linb8757bf2013-12-13 01:48:58 +0800380
381 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800382 outPort = portObject.getNumber();
383 }
pingping-linb8757bf2013-12-13 01:48:58 +0800384
pingping-lin568c6012013-12-14 05:57:11 +0800385 ISwitchObject outSwitchObject = portObject.getSwitch();
Jonathan Hart18ad9502013-12-15 18:28:00 -0800386 outSwitch = HexString.toLong(outSwitchObject.getDPID());
387
388 if (log.isTraceEnabled()) {
389 log.trace("Probing device {} on port {}/{}",
390 new Object[] {macAddress,
391 HexString.toHexString(outSwitch), outPort});
392 }
pingping-lin568c6012013-12-14 05:57:11 +0800393
Jonathan Hart18ad9502013-12-15 18:28:00 -0800394 sendToOtherNodes(eth, pi, outSwitch, outPort);
pingping-linb8757bf2013-12-13 01:48:58 +0800395 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200396 }
pingping-lin017a8922013-12-11 11:15:33 +0800397 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200398 }
399
Jonathan Hart1912afc2013-10-11 12:02:44 +1300400 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200401 if (log.isTraceEnabled()) {
402 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
403 inetAddressToString(arp.getSenderProtocolAddress()),
404 HexString.toHexString(arp.getSenderHardwareAddress()),
405 HexString.toHexString(sw.getId()), pi.getInPort()});
406 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200407
Jonathan Hartabad6a52013-09-30 18:17:21 +1300408 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200409 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300410 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200411 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200412 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200413 return;
414 }
415
Jonathan Hartabad6a52013-09-30 18:17:21 +1300416 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
417
pingping-lin017a8922013-12-11 11:15:33 +0800418 //arpCache.update(senderIpAddress, senderMacAddress);
Jonathan Hartabad6a52013-09-30 18:17:21 +1300419
420 //See if anyone's waiting for this ARP reply
421 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200422
423 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200424 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200425 synchronized (arpRequests) {
426 Iterator<ArpRequest> it = requests.iterator();
427 while (it.hasNext()) {
428 ArpRequest request = it.next();
429 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200430 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200431 }
432 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200433
434 //Don't hold an ARP lock while dispatching requests
435 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300436 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200437 }
438 }
439
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200440 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200441 //TODO what should the sender IP address and MAC address be if no
442 //IP addresses are configured? Will there ever be a need to send
443 //ARP requests from the controller in that case?
444 //All-zero MAC address doesn't seem to work - hosts don't respond to it
445
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200446 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
447 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200448 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200449 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200450 (byte)0xff, (byte)0xff, (byte)0xff};
451
452 ARP arpRequest = new ARP();
453
454 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
455 .setProtocolType(ARP.PROTO_TYPE_IP)
456 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200457 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200458 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200459 .setTargetHardwareAddress(zeroMac)
460 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200461
Jonathan Harta8887642013-10-28 13:46:54 -0700462 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200463 //TODO hack for now as it's unclear what the MAC address should be
464 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200465 if (routerMacAddress != null) {
466 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200467 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200468 arpRequest.setSenderHardwareAddress(senderMacAddress);
469
470 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700471 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200472 if (intf != null) {
473 senderIPAddress = intf.getIpAddress().getAddress();
474 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200475
476 arpRequest.setSenderProtocolAddress(senderIPAddress);
477
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200478 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200479 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200480 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200481 .setEtherType(Ethernet.TYPE_ARP)
482 .setPayload(arpRequest);
483
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700484 if (vlan != NO_VLAN) {
485 eth.setVlanID(vlan)
486 .setPriorityCode((byte)0);
487 }
488
Jonathan Hart2f790d22013-08-15 14:01:24 +1200489 sendArpRequestToSwitches(ipAddress, eth.serialize());
490 }
491
492 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200493 sendArpRequestToSwitches(dstAddress, arpRequest,
494 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200495 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200496
Jonathan Hart2f790d22013-08-15 14:01:24 +1200497 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
498 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200499
Jonathan Harta8887642013-10-28 13:46:54 -0700500 if (configService.hasLayer3Configuration()) {
501 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200502 if (intf != null) {
503 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
504 }
505 else {
506 //TODO here it should be broadcast out all non-interface edge ports.
507 //I think we can assume that if it's not a request for an external
508 //network, it's an ARP for a host in our own network. So we want to
509 //send it out all edge ports that don't have an interface configured
510 //to ensure it reaches all hosts in our network.
511 log.debug("No interface found to send ARP request for {}",
512 dstAddress.getHostAddress());
513 }
514 }
515 else {
516 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
517 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200518 }
519
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800520 private void sendToOtherNodes(Ethernet eth, long inSwitchId, OFPacketIn pi) {
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800521 ARP arp = (ARP) eth.getPayload();
Jonathan Hartd3003252013-11-15 09:44:46 -0800522
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800523 if (log.isTraceEnabled()) {
524 log.trace("Sending ARP request for {} to other ONOS instances",
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800525 inetAddressToString(arp.getTargetProtocolAddress()));
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800526 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800527
528 InetAddress targetAddress;
529 try {
530 targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress());
531 } catch (UnknownHostException e) {
532 log.error("Unknown host", e);
533 return;
534 }
535
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800536 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize(),
537 -1L, (short)-1, inSwitchId, pi.getInPort()));
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800538 }
Jonathan Hart18ad9502013-12-15 18:28:00 -0800539
pingping-lin017a8922013-12-11 11:15:33 +0800540 //hazelcast to other ONOS instances to send the ARP packet out on outPort of outSwitch
541 private void sendToOtherNodes(Ethernet eth, OFPacketIn pi, long outSwitch, short outPort) {
542 ARP arp = (ARP) eth.getPayload();
543
544 if (log.isTraceEnabled()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800545 log.trace("Sending ARP request for {} to other ONOS instances with outSwitch {} ",
546 inetAddressToString(arp.getTargetProtocolAddress()), String.valueOf(outSwitch));
pingping-lin017a8922013-12-11 11:15:33 +0800547 }
548
549 InetAddress targetAddress;
550 try {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800551 targetAddress = InetAddress.getByAddress(arp.getTargetProtocolAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800552 } catch (UnknownHostException e) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800553 log.error("Unknown host", e);
554 return;
pingping-lin017a8922013-12-11 11:15:33 +0800555 }
556
557 datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize(), outSwitch, outPort));
558 //datagrid.sendArpRequest(ArpMessage.newRequest(targetAddress, eth.serialize()));
559
560
561 }
Jonathan Hart18ad9502013-12-15 18:28:00 -0800562
pingping-lin017a8922013-12-11 11:15:33 +0800563 private void sendToOtherNodesReply(Ethernet eth, OFPacketIn pi) {
564 ARP arp = (ARP) eth.getPayload();
565
566 if (log.isTraceEnabled()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800567 log.trace("Sending ARP reply for {} to other ONOS instances",
568 inetAddressToString(arp.getSenderProtocolAddress()));
pingping-lin017a8922013-12-11 11:15:33 +0800569 }
570
571 InetAddress targetAddress;
572 MACAddress mac = new MACAddress(arp.getSenderHardwareAddress());
573
574 try {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800575 targetAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800576 } catch (UnknownHostException e) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800577 log.error("Unknown host", e);
578 return;
pingping-lin017a8922013-12-11 11:15:33 +0800579 }
580
Jonathan Hart18ad9502013-12-15 18:28:00 -0800581 datagrid.sendArpRequest(ArpMessage.newReply(targetAddress, mac));
pingping-lin017a8922013-12-11 11:15:33 +0800582 //datagrid.sendArpReply(ArpMessage.newRequest(targetAddress, eth.serialize()));
583
584 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800585
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200586 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200587 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
588 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
589 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
590
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200591 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700592 //I think this means the switch doesn't have any links.
593 //continue;
594 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200595 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200596
Jonathan Harta18e4792013-10-31 10:10:54 -0700597
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200598 OFPacketOut po = new OFPacketOut();
599 po.setInPort(OFPort.OFPP_NONE)
600 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200601 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200602
603 List<OFAction> actions = new ArrayList<OFAction>();
604
605 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200606 if (linkPorts.contains(portNum) ||
607 (sw.getId() == inSwitch && portNum == inPort)){
608 //If this port isn't an edge port or is the ingress port
609 //for the ARP, don't broadcast out it
610 continue;
611 }
612
613 actions.add(new OFActionOutput(portNum));
614 }
615
616 po.setActions(actions);
617 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
618 po.setActionsLength(actionsLength);
619 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200620 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200621
622 List<OFMessage> msgList = new ArrayList<OFMessage>();
623 msgList.add(po);
624
625 try {
626 sw.write(msgList, null);
627 sw.flush();
628 } catch (IOException e) {
629 log.error("Failure writing packet out to switch", e);
630 }
631 }
632 }
633
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800634 private void broadcastArpRequestOutMyEdge(byte[] arpRequest,
635 long inSwitch, short inPort) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800636 List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
637
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800638 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
639
640 OFPacketOut po = new OFPacketOut();
641 po.setInPort(OFPort.OFPP_NONE)
642 .setBufferId(-1)
643 .setPacketData(arpRequest);
644
645 List<OFAction> actions = new ArrayList<OFAction>();
646
647 Iterable<IPortObject> ports
648 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
649 if (ports == null) {
650 continue;
651 }
652
653 for (IPortObject portObject : ports) {
654 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800655 short portNumber = portObject.getNumber();
656
657 if (sw.getId() == inSwitch && portNumber == inPort) {
658 // This is the port that the ARP message came in,
659 // so don't broadcast out this port
660 continue;
661 }
662
Jonathan Hartba9ced92013-11-24 16:52:13 -0800663 switchPorts.add(new SwitchPort(new Dpid(sw.getId()),
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800664 new Port(portNumber)));
665 actions.add(new OFActionOutput(portNumber));
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800666 }
667 }
668
669 po.setActions(actions);
670 short actionsLength = (short)
671 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
672 po.setActionsLength(actionsLength);
673 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
674 + arpRequest.length);
675
676 try {
677 sw.write(po, null);
678 sw.flush();
679 } catch (IOException e) {
680 log.error("Failure writing packet out to switch", e);
681 }
682 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800683
Jonathan Hart18ad9502013-12-15 18:28:00 -0800684 if (log.isTraceEnabled()) {
685 log.trace("Broadcast ARP request to: {}", switchPorts);
686 }
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800687 }
688
Jonathan Hart2f790d22013-08-15 14:01:24 +1200689 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200690 if (log.isTraceEnabled()) {
691 log.trace("Sending ARP request out {}/{}",
692 HexString.toHexString(dpid), port);
693 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200694
695 OFPacketOut po = new OFPacketOut();
696 po.setInPort(OFPort.OFPP_NONE)
697 .setBufferId(-1)
698 .setPacketData(arpRequest);
699
700 List<OFAction> actions = new ArrayList<OFAction>();
701 actions.add(new OFActionOutput(port));
702 po.setActions(actions);
703 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
704 po.setActionsLength(actionsLength);
705 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
706 + arpRequest.length);
707
708 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
709
710 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200711 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200712 return;
713 }
714
715 try {
716 sw.write(po, null);
717 sw.flush();
718 } catch (IOException e) {
719 log.error("Failure writing packet out to switch", e);
720 }
721 }
722
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300723 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200724 if (log.isTraceEnabled()) {
725 log.trace("Sending reply {} => {} to {}", new Object[] {
726 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300727 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200728 inetAddressToString(arpRequest.getSenderProtocolAddress())});
729 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200730
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200731 ARP arpReply = new ARP();
732 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
733 .setProtocolType(ARP.PROTO_TYPE_IP)
734 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200735 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200736 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300737 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200738 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
739 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
740 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
741
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700742
743
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200744 Ethernet eth = new Ethernet();
745 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300746 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200747 .setEtherType(Ethernet.TYPE_ARP)
748 .setPayload(arpReply);
749
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700750 if (vlan != NO_VLAN) {
751 eth.setVlanID(vlan)
752 .setPriorityCode((byte)0);
753 }
754
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200755 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200756 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200757
758 OFPacketOut po = new OFPacketOut();
759 po.setInPort(OFPort.OFPP_NONE)
760 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200761 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200762 .setActions(actions)
763 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
764 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
765 + po.getPacketData().length);
766
767 List<OFMessage> msgList = new ArrayList<OFMessage>();
768 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200769
770 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
771
772 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200773 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200774 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200775 return;
776 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200777
778 try {
779 sw.write(msgList, null);
780 sw.flush();
781 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200782 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200783 }
784 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300785
786 private String inetAddressToString(byte[] bytes) {
787 try {
788 return InetAddress.getByAddress(bytes).getHostAddress();
789 } catch (UnknownHostException e) {
790 log.debug("Invalid IP address", e);
791 return "";
792 }
793 }
794
795 /*
796 * IProxyArpService methods
797 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200798
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200799 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300800 public MACAddress getMacAddress(InetAddress ipAddress) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800801 //return arpCache.lookup(ipAddress);
802 return null;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200803 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200804
805 @Override
806 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
807 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200808 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200809
Jonathan Hart6e618212013-08-21 22:28:43 +1200810 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700811 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200812 sendArpRequestForAddress(ipAddress);
813 }
814 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300815
816 @Override
817 public List<String> getMappings() {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800818 //return arpCache.getMappings();
819 return new ArrayList<String>();
Jonathan Hart5afde492013-10-01 12:30:53 +1300820 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800821
822 /*
823 * IArpEventHandler methods
824 */
825
826 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800827 public void arpRequestNotification(ArpMessage arpMessage) {
pingping-lin017a8922013-12-11 11:15:33 +0800828 log.debug("Received ARP notification from other instances");
pingping-linb8757bf2013-12-13 01:48:58 +0800829
Jonathan Hartd3003252013-11-15 09:44:46 -0800830 switch (arpMessage.getType()){
831 case REQUEST:
pingping-linb8757bf2013-12-13 01:48:58 +0800832 if(arpMessage.getOutSwitch() == -1 || arpMessage.getOutPort() == -1){
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800833 broadcastArpRequestOutMyEdge(arpMessage.getPacket(),
834 arpMessage.getInSwitch(), arpMessage.getInPort());
pingping-linb8757bf2013-12-13 01:48:58 +0800835 }else{
836 sendArpRequestOutPort(arpMessage.getPacket(),arpMessage.getOutSwitch(),arpMessage.getOutPort());
837 log.debug("OutSwitch in ARP request message is: {}; OutPort in ARP request message is: {}",arpMessage.getOutSwitch(),arpMessage.getOutPort());
838 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800839 break;
840 case REPLY:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800841 log.debug("Received ARP reply notification for {}",
842 arpMessage.getAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800843 sendArpReplyToWaitingRequesters(arpMessage.getAddress(),arpMessage.getMAC());
Jonathan Hartd3003252013-11-15 09:44:46 -0800844 break;
845 }
846 }
847
pingping-lin017a8922013-12-11 11:15:33 +0800848 private void sendArpReplyToWaitingRequesters(InetAddress address, MACAddress mac) {
Jonathan Hartd3003252013-11-15 09:44:46 -0800849 log.debug("Sending ARP reply for {} to requesters",
850 address.getHostAddress());
851
852 //See if anyone's waiting for this ARP reply
853 Set<ArpRequest> requests = arpRequests.get(address);
854
855 //Synchronize on the Multimap while using an iterator for one of the sets
856 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
857 synchronized (arpRequests) {
858 Iterator<ArpRequest> it = requests.iterator();
859 while (it.hasNext()) {
860 ArpRequest request = it.next();
861 it.remove();
862 requestsToSend.add(request);
863 }
864 }
865
pingping-lin017a8922013-12-11 11:15:33 +0800866 /*IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
Jonathan Hartd3003252013-11-15 09:44:46 -0800867 InetAddresses.coerceToInteger(address));
868
869 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
870
871 log.debug("Found {} at {} in network map",
pingping-lin017a8922013-12-11 11:15:33 +0800872 address.getHostAddress(), mac);*/
Jonathan Hartd3003252013-11-15 09:44:46 -0800873
874 //Don't hold an ARP lock while dispatching requests
875 for (ArpRequest request : requestsToSend) {
876 request.dispatchReply(address, mac);
877 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800878 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200879}