blob: f5fee4539438fd9d09f4a17ff912a24f90794486 [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 Hart7804bea2014-01-07 10:50:52 -080040import net.onrc.onos.ofcontroller.flowprogrammer.IFlowPusherService;
Jonathan Hartba9ced92013-11-24 16:52:13 -080041import net.onrc.onos.ofcontroller.util.Dpid;
42import net.onrc.onos.ofcontroller.util.Port;
43import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120044
45import org.openflow.protocol.OFMessage;
46import org.openflow.protocol.OFPacketIn;
47import org.openflow.protocol.OFPacketOut;
48import org.openflow.protocol.OFPort;
49import org.openflow.protocol.OFType;
50import org.openflow.protocol.action.OFAction;
51import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120052import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
Jonathan Hart4dfc3652013-08-02 20:22:36 +120056import com.google.common.collect.HashMultimap;
57import com.google.common.collect.Multimaps;
58import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070059import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120060
Jonathan Hart18ad55c2013-11-11 22:49:55 -080061public class ProxyArpManager implements IProxyArpService, IOFMessageListener,
Jonathan Hart7804bea2014-01-07 10:50:52 -080062 IPacketOutEventHandler, IArpReplyEventHandler,
63 IFloodlightModule {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120064 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120065
pingping-lin017a8922013-12-11 11:15:33 +080066 private final long ARP_TIMER_PERIOD = 100; //ms
67
Jonathan Hart18ad9502013-12-15 18:28:00 -080068 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120069
Jonathan Harta8887642013-10-28 13:46:54 -070070 private IFloodlightProviderService floodlightProvider;
71 private ITopologyService topology;
Jonathan Hart18ad55c2013-11-11 22:49:55 -080072 private IDatagridService datagrid;
Jonathan Harta8887642013-10-28 13:46:54 -070073 private IConfigInfoService configService;
74 private IRestApiService restApi;
Jonathan Hart7804bea2014-01-07 10:50:52 -080075 private IFlowPusherService flowPusher;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120076
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070077 private IDeviceStorage deviceStorage;
Jonathan Hart9a21e0d2013-11-14 15:09:09 -080078 private volatile ITopoSwitchService topoSwitchService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070079
Jonathan Hart1cf9de02013-10-21 17:42:29 -070080 private short vlan;
81 private static final short NO_VLAN = 0;
82
Jonathan Hart18ad9502013-12-15 18:28:00 -080083 //private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120084
Jonathan Harta8887642013-10-28 13:46:54 -070085 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120086
Jonathan Hartabad6a52013-09-30 18:17:21 +130087 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120088 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130089 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120090 private long requestTime;
91
Jonathan Hart4dfc3652013-08-02 20:22:36 +120092 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120093 this.requester = requester;
94 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120095 this.requestTime = System.currentTimeMillis();
96 }
97
Jonathan Hart4dfc3652013-08-02 20:22:36 +120098 public ArpRequest(ArpRequest old) {
99 this.requester = old.requester;
100 this.retry = old.retry;
101 this.requestTime = System.currentTimeMillis();
102 }
103
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200104 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300105 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200106 }
107
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200108 public boolean shouldRetry() {
109 return retry;
110 }
111
Jonathan Hartabad6a52013-09-30 18:17:21 +1300112 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200113 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200114 }
115 }
116
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300117 private class HostArpRequester implements IArpRequester {
118 private final ARP arpRequest;
119 private final long dpid;
120 private final short port;
121
122 public HostArpRequester(ARP arpRequest, long dpid, short port) {
123 this.arpRequest = arpRequest;
124 this.dpid = dpid;
125 this.port = port;
126 }
127
128 @Override
129 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
130 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
131 }
132 }
133
Jonathan Harte93aed42013-12-05 18:39:50 -0800134 @Override
135 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
136 Collection<Class<? extends IFloodlightService>> l
137 = new ArrayList<Class<? extends IFloodlightService>>();
138 l.add(IProxyArpService.class);
139 return l;
Jonathan Harta8887642013-10-28 13:46:54 -0700140 }
Jonathan Harte93aed42013-12-05 18:39:50 -0800141
142 @Override
143 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
144 Map<Class<? extends IFloodlightService>, IFloodlightService> m
145 = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
146 m.put(IProxyArpService.class, this);
147 return m;
148 }
149
150 @Override
151 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
152 Collection<Class<? extends IFloodlightService>> dependencies
153 = new ArrayList<Class<? extends IFloodlightService>>();
154 dependencies.add(IFloodlightProviderService.class);
155 dependencies.add(ITopologyService.class);
156 dependencies.add(IRestApiService.class);
157 dependencies.add(IDatagridService.class);
158 dependencies.add(IConfigInfoService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800159 dependencies.add(IFlowPusherService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800160 return dependencies;
161 }
Jonathan Harta8887642013-10-28 13:46:54 -0700162
Jonathan Harte93aed42013-12-05 18:39:50 -0800163 @Override
164 public void init(FloodlightModuleContext context){
165 this.floodlightProvider =
166 context.getServiceImpl(IFloodlightProviderService.class);
167 this.topology = context.getServiceImpl(ITopologyService.class);
168 this.datagrid = context.getServiceImpl(IDatagridService.class);
169 this.configService = context.getServiceImpl(IConfigInfoService.class);
170 this.restApi = context.getServiceImpl(IRestApiService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800171 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200172
Jonathan Hart18ad9502013-12-15 18:28:00 -0800173 //arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200174
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200175 arpRequests = Multimaps.synchronizedSetMultimap(
176 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800177
178 topoSwitchService = new TopoSwitchServiceImpl();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200179 }
180
Jonathan Harte93aed42013-12-05 18:39:50 -0800181 @Override
182 public void startUp(FloodlightModuleContext context) {
Jonathan Harta8887642013-10-28 13:46:54 -0700183 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700184 log.info("vlan set to {}", this.vlan);
185
Jonathan Hart5afde492013-10-01 12:30:53 +1300186 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700187 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300188
Jonathan Hart7804bea2014-01-07 10:50:52 -0800189 datagrid.registerPacketOutEventHandler(this);
Jonathan Hart016f03b2014-01-23 13:56:43 -0800190 datagrid.registerArpReplyEventHandler(this);
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800191
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700192 deviceStorage = new DeviceStorageImpl();
yoshitomob292c622013-11-23 14:35:58 -0800193 deviceStorage.init("","");
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700194
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200195 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200196 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200197 @Override
198 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200199 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200200 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200201 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200202 }
203
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200204 /*
205 * Function that runs periodically to manage the asynchronous request mechanism.
206 * It basically cleans up old ARP requests if we don't get a response for them.
207 * The caller can designate that a request should be retried indefinitely, and
208 * this task will handle that as well.
209 */
210 private void doPeriodicArpProcessing() {
211 SetMultimap<InetAddress, ArpRequest> retryList
212 = HashMultimap.<InetAddress, ArpRequest>create();
213
214 //Have to synchronize externally on the Multimap while using an iterator,
215 //even though it's a synchronizedMultimap
Jonathan Hart18ad9502013-12-15 18:28:00 -0800216 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200217 Iterator<Map.Entry<InetAddress, ArpRequest>> it
218 = arpRequests.entries().iterator();
219
220 while (it.hasNext()) {
221 Map.Entry<InetAddress, ArpRequest> entry
222 = it.next();
223 ArpRequest request = entry.getValue();
224 if (request.isExpired()) {
225 log.debug("Cleaning expired ARP request for {}",
226 entry.getKey().getHostAddress());
227
Jonathan Hart18ad9502013-12-15 18:28:00 -0800228 // If the ARP request is expired and then delete the device
229 // TODO check whether this is OK from this thread
pingping-lin017a8922013-12-11 11:15:33 +0800230 IDeviceObject targetDevice =
231 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(entry.getKey()));
232
Jonathan Hart18ad9502013-12-15 18:28:00 -0800233 if (targetDevice != null) {
234 deviceStorage.removeDevice(targetDevice);
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800235 if (log.isDebugEnabled()) {
236 log.debug("RemoveDevice: {} due to no have not recieve the ARP reply", targetDevice);
237 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800238 }
pingping-lin017a8922013-12-11 11:15:33 +0800239
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200240 it.remove();
241
242 if (request.shouldRetry()) {
243 retryList.put(entry.getKey(), request);
244 }
245 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200246 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200247 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200248
249 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
250 : retryList.asMap().entrySet()) {
251
252 InetAddress address = entry.getKey();
253
254 log.debug("Resending ARP request for {}", address.getHostAddress());
255
256 sendArpRequestForAddress(address);
257
258 for (ArpRequest request : entry.getValue()) {
259 arpRequests.put(address, new ArpRequest(request));
260 }
261 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200262 }
263
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200264 @Override
265 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700266 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200267 }
268
269 @Override
270 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700271 if (type == OFType.PACKET_IN) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800272 return "devicemanager".equals(name) || "onosdevicemanager".equals(name);
Jonathan Harta18e4792013-10-31 10:10:54 -0700273 }
274 else {
275 return false;
276 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200277 }
278
279 @Override
280 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800281 return type == OFType.PACKET_IN && "onosforwarding".equals(name);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200282 }
283
284 @Override
285 public Command receive(
286 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
287
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200288 OFPacketIn pi = (OFPacketIn) msg;
289
290 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
291 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
292
293 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800294 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200295 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800296 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200297 }
298 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart016f03b2014-01-23 13:56:43 -0800299 // For replies we simply send a notification via Hazelcast
Jonathan Hartc6325622014-01-14 16:37:50 -0800300 sendArpReplyNotification(eth, pi);
Jonathan Hart016f03b2014-01-23 13:56:43 -0800301
302 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200303 }
Jonathan Hart17672992013-12-12 16:15:16 -0800304
305 // Stop ARP packets here
306 return Command.STOP;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200307 }
308
Jonathan Hart17672992013-12-12 16:15:16 -0800309 // Propagate everything else
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200310 return Command.CONTINUE;
311 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200312
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800313 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200314 if (log.isTraceEnabled()) {
315 log.trace("ARP request received for {}",
316 inetAddressToString(arp.getTargetProtocolAddress()));
317 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200318
319 InetAddress target;
320 try {
pingping-linb8757bf2013-12-13 01:48:58 +0800321 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200322 } catch (UnknownHostException e) {
323 log.debug("Invalid address in ARP request", e);
324 return;
325 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200326
Jonathan Harta8887642013-10-28 13:46:54 -0700327 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200328 //If the request came from outside our network, we only care if
329 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700330 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200331 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700332 target.getHostAddress(), configService.getRouterMacAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800333
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200334 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700335 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200336 }
pingping-linb8757bf2013-12-13 01:48:58 +0800337
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200338 return;
339 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200340
Jonathan Harta18e4792013-10-31 10:10:54 -0700341 //MACAddress macAddress = arpCache.lookup(target);
pingping-linb8757bf2013-12-13 01:48:58 +0800342
343 arpRequests.put(target, new ArpRequest(
344 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
345
Jonathan Hart18ad9502013-12-15 18:28:00 -0800346 IDeviceObject targetDevice =
347 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
348
349 if (targetDevice == null) {
350 if (log.isTraceEnabled()) {
351 log.trace("No device info found for {} - broadcasting",
352 target.getHostAddress());
353 }
354
355 // We don't know the device so broadcast the request out
Jonathan Hart7804bea2014-01-07 10:50:52 -0800356 datagrid.sendPacketOutNotification(
357 new BroadcastPacketOutNotification(eth.serialize(),
358 sw.getId(), pi.getInPort()));
Jonathan Hart18ad9502013-12-15 18:28:00 -0800359 }
360 else {
361 // Even if the device exists in our database, we do not reply to
362 // the request directly, but check whether the device is still valid
Jonathan Harta18e4792013-10-31 10:10:54 -0700363 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800364
Jonathan Harta18e4792013-10-31 10:10:54 -0700365 if (log.isTraceEnabled()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800366 log.trace("The target Device Record in DB is: {} => {} from ARP request host at {}/{}",
Jonathan Hart18ad9502013-12-15 18:28:00 -0800367 new Object [] {
Jonathan Harta18e4792013-10-31 10:10:54 -0700368 inetAddressToString(arp.getTargetProtocolAddress()),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800369 macAddress,
Jonathan Harta18e4792013-10-31 10:10:54 -0700370 HexString.toHexString(sw.getId()), pi.getInPort()});
371 }
pingping-linb8757bf2013-12-13 01:48:58 +0800372
373 // sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
374
Jonathan Hart18ad9502013-12-15 18:28:00 -0800375 Iterable<IPortObject> outPorts = targetDevice.getAttachedPorts();
pingping-linb8757bf2013-12-13 01:48:58 +0800376
Jonathan Hart18ad9502013-12-15 18:28:00 -0800377 if (!outPorts.iterator().hasNext()){
378 if (log.isTraceEnabled()) {
379 log.trace("Device {} exists but is not connected to any ports" +
380 " - broadcasting", macAddress);
381 }
382
Jonathan Hart7804bea2014-01-07 10:50:52 -0800383 datagrid.sendPacketOutNotification(
384 new BroadcastPacketOutNotification(eth.serialize(),
385 sw.getId(), pi.getInPort()));
Jonathan Hart18ad9502013-12-15 18:28:00 -0800386 }
387 else {
pingping-linb8757bf2013-12-13 01:48:58 +0800388 for (IPortObject portObject : outPorts) {
Jonathan Hart7804bea2014-01-07 10:50:52 -0800389 //long outSwitch = 0;
390 //short outPort = 0;
pingping-linb8757bf2013-12-13 01:48:58 +0800391
Jonathan Hart7804bea2014-01-07 10:50:52 -0800392 /*
pingping-linb8757bf2013-12-13 01:48:58 +0800393 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800394 outPort = portObject.getNumber();
Jonathan Hart7804bea2014-01-07 10:50:52 -0800395 }*/
396 if (portObject.getLinkedPorts().iterator().hasNext()) {
397 continue;
pingping-linb8757bf2013-12-13 01:48:58 +0800398 }
pingping-linb8757bf2013-12-13 01:48:58 +0800399
Jonathan Hart7804bea2014-01-07 10:50:52 -0800400 short outPort = portObject.getNumber();
pingping-lin568c6012013-12-14 05:57:11 +0800401 ISwitchObject outSwitchObject = portObject.getSwitch();
Jonathan Hart7804bea2014-01-07 10:50:52 -0800402 long outSwitch = HexString.toLong(outSwitchObject.getDPID());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800403
404 if (log.isTraceEnabled()) {
405 log.trace("Probing device {} on port {}/{}",
406 new Object[] {macAddress,
407 HexString.toHexString(outSwitch), outPort});
408 }
pingping-lin568c6012013-12-14 05:57:11 +0800409
Jonathan Hart7804bea2014-01-07 10:50:52 -0800410 datagrid.sendPacketOutNotification(
411 new SinglePacketOutNotification(eth.serialize(),
412 outSwitch, outPort));
pingping-linb8757bf2013-12-13 01:48:58 +0800413 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200414 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200415 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200416 }
417
Jonathan Hart1912afc2013-10-11 12:02:44 +1300418 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200419 if (log.isTraceEnabled()) {
420 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
421 inetAddressToString(arp.getSenderProtocolAddress()),
422 HexString.toHexString(arp.getSenderHardwareAddress()),
423 HexString.toHexString(sw.getId()), pi.getInPort()});
424 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200425
Jonathan Hartabad6a52013-09-30 18:17:21 +1300426 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200427 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300428 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200429 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200430 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200431 return;
432 }
433
Jonathan Hartabad6a52013-09-30 18:17:21 +1300434 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
435
pingping-lin017a8922013-12-11 11:15:33 +0800436 //arpCache.update(senderIpAddress, senderMacAddress);
Jonathan Hartabad6a52013-09-30 18:17:21 +1300437
438 //See if anyone's waiting for this ARP reply
439 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200440
441 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200442 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200443 synchronized (arpRequests) {
444 Iterator<ArpRequest> it = requests.iterator();
445 while (it.hasNext()) {
446 ArpRequest request = it.next();
447 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200448 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200449 }
450 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200451
452 //Don't hold an ARP lock while dispatching requests
453 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300454 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200455 }
456 }
457
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200458 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200459 //TODO what should the sender IP address and MAC address be if no
460 //IP addresses are configured? Will there ever be a need to send
461 //ARP requests from the controller in that case?
462 //All-zero MAC address doesn't seem to work - hosts don't respond to it
463
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200464 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
465 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200466 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200467 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200468 (byte)0xff, (byte)0xff, (byte)0xff};
469
470 ARP arpRequest = new ARP();
471
472 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
473 .setProtocolType(ARP.PROTO_TYPE_IP)
474 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200475 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200476 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200477 .setTargetHardwareAddress(zeroMac)
478 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200479
Jonathan Harta8887642013-10-28 13:46:54 -0700480 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200481 //TODO hack for now as it's unclear what the MAC address should be
482 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200483 if (routerMacAddress != null) {
484 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200485 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200486 arpRequest.setSenderHardwareAddress(senderMacAddress);
487
488 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700489 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200490 if (intf != null) {
491 senderIPAddress = intf.getIpAddress().getAddress();
492 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200493
494 arpRequest.setSenderProtocolAddress(senderIPAddress);
495
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200496 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200497 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200498 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200499 .setEtherType(Ethernet.TYPE_ARP)
500 .setPayload(arpRequest);
501
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700502 if (vlan != NO_VLAN) {
503 eth.setVlanID(vlan)
504 .setPriorityCode((byte)0);
505 }
506
Jonathan Hart2f790d22013-08-15 14:01:24 +1200507 sendArpRequestToSwitches(ipAddress, eth.serialize());
508 }
509
510 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200511 sendArpRequestToSwitches(dstAddress, arpRequest,
512 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200513 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200514
Jonathan Hart2f790d22013-08-15 14:01:24 +1200515 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
516 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200517
Jonathan Harta8887642013-10-28 13:46:54 -0700518 if (configService.hasLayer3Configuration()) {
519 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200520 if (intf != null) {
521 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
522 }
523 else {
524 //TODO here it should be broadcast out all non-interface edge ports.
525 //I think we can assume that if it's not a request for an external
526 //network, it's an ARP for a host in our own network. So we want to
527 //send it out all edge ports that don't have an interface configured
528 //to ensure it reaches all hosts in our network.
529 log.debug("No interface found to send ARP request for {}",
530 dstAddress.getHostAddress());
531 }
532 }
533 else {
534 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
535 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200536 }
537
Jonathan Hartc6325622014-01-14 16:37:50 -0800538 private void sendArpReplyNotification(Ethernet eth, OFPacketIn pi) {
pingping-lin017a8922013-12-11 11:15:33 +0800539 ARP arp = (ARP) eth.getPayload();
540
541 if (log.isTraceEnabled()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800542 log.trace("Sending ARP reply for {} to other ONOS instances",
543 inetAddressToString(arp.getSenderProtocolAddress()));
pingping-lin017a8922013-12-11 11:15:33 +0800544 }
545
546 InetAddress targetAddress;
547 MACAddress mac = new MACAddress(arp.getSenderHardwareAddress());
548
549 try {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800550 targetAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800551 } catch (UnknownHostException e) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800552 log.error("Unknown host", e);
553 return;
pingping-lin017a8922013-12-11 11:15:33 +0800554 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800555
556 datagrid.sendArpReplyNotification(new ArpReplyNotification(targetAddress, mac));
pingping-lin017a8922013-12-11 11:15:33 +0800557 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800558
Jonathan Hart7804bea2014-01-07 10:50:52 -0800559 // This remains from the older single-instance ARP code. It used Floodlight
560 // APIs to find the edge of the network, but only worked on a single instance.
561 // We now do this using ONOS network graph APIs.
562 @Deprecated
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200563 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200564 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
565 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
566 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
567
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200568 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700569 //I think this means the switch doesn't have any links.
570 //continue;
571 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200572 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200573
Jonathan Harta18e4792013-10-31 10:10:54 -0700574
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200575 OFPacketOut po = new OFPacketOut();
576 po.setInPort(OFPort.OFPP_NONE)
577 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200578 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200579
580 List<OFAction> actions = new ArrayList<OFAction>();
581
582 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200583 if (linkPorts.contains(portNum) ||
584 (sw.getId() == inSwitch && portNum == inPort)){
585 //If this port isn't an edge port or is the ingress port
586 //for the ARP, don't broadcast out it
587 continue;
588 }
589
590 actions.add(new OFActionOutput(portNum));
591 }
592
593 po.setActions(actions);
594 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
595 po.setActionsLength(actionsLength);
596 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200597 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200598
599 List<OFMessage> msgList = new ArrayList<OFMessage>();
600 msgList.add(po);
601
602 try {
603 sw.write(msgList, null);
604 sw.flush();
605 } catch (IOException e) {
606 log.error("Failure writing packet out to switch", e);
607 }
608 }
609 }
610
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800611 private void broadcastArpRequestOutMyEdge(byte[] arpRequest,
612 long inSwitch, short inPort) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800613 List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
614
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800615 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
616
617 OFPacketOut po = new OFPacketOut();
618 po.setInPort(OFPort.OFPP_NONE)
619 .setBufferId(-1)
620 .setPacketData(arpRequest);
621
622 List<OFAction> actions = new ArrayList<OFAction>();
623
624 Iterable<IPortObject> ports
625 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
626 if (ports == null) {
627 continue;
628 }
629
630 for (IPortObject portObject : ports) {
631 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800632 short portNumber = portObject.getNumber();
633
634 if (sw.getId() == inSwitch && portNumber == inPort) {
635 // This is the port that the ARP message came in,
636 // so don't broadcast out this port
637 continue;
638 }
639
Jonathan Hartba9ced92013-11-24 16:52:13 -0800640 switchPorts.add(new SwitchPort(new Dpid(sw.getId()),
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800641 new Port(portNumber)));
642 actions.add(new OFActionOutput(portNumber));
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800643 }
644 }
645
646 po.setActions(actions);
647 short actionsLength = (short)
648 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
649 po.setActionsLength(actionsLength);
650 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
651 + arpRequest.length);
652
Jonathan Hart7804bea2014-01-07 10:50:52 -0800653 flowPusher.add(sw, po);
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800654 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800655
Jonathan Hart18ad9502013-12-15 18:28:00 -0800656 if (log.isTraceEnabled()) {
657 log.trace("Broadcast ARP request to: {}", switchPorts);
658 }
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800659 }
660
Jonathan Hart2f790d22013-08-15 14:01:24 +1200661 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200662 if (log.isTraceEnabled()) {
663 log.trace("Sending ARP request out {}/{}",
664 HexString.toHexString(dpid), port);
665 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200666
667 OFPacketOut po = new OFPacketOut();
668 po.setInPort(OFPort.OFPP_NONE)
669 .setBufferId(-1)
670 .setPacketData(arpRequest);
671
672 List<OFAction> actions = new ArrayList<OFAction>();
673 actions.add(new OFActionOutput(port));
674 po.setActions(actions);
675 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
676 po.setActionsLength(actionsLength);
677 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
678 + arpRequest.length);
679
680 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
681
682 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200683 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200684 return;
685 }
686
Jonathan Hart7804bea2014-01-07 10:50:52 -0800687 flowPusher.add(sw, po);
Jonathan Hart2f790d22013-08-15 14:01:24 +1200688 }
689
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300690 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200691 if (log.isTraceEnabled()) {
692 log.trace("Sending reply {} => {} to {}", new Object[] {
693 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300694 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200695 inetAddressToString(arpRequest.getSenderProtocolAddress())});
696 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200697
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200698 ARP arpReply = new ARP();
699 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
700 .setProtocolType(ARP.PROTO_TYPE_IP)
701 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200702 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200703 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300704 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200705 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
706 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
707 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
708
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700709
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200710 Ethernet eth = new Ethernet();
711 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300712 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200713 .setEtherType(Ethernet.TYPE_ARP)
714 .setPayload(arpReply);
715
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700716 if (vlan != NO_VLAN) {
717 eth.setVlanID(vlan)
718 .setPriorityCode((byte)0);
719 }
720
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200721 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200722 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200723
724 OFPacketOut po = new OFPacketOut();
725 po.setInPort(OFPort.OFPP_NONE)
726 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200727 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200728 .setActions(actions)
729 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
730 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
731 + po.getPacketData().length);
732
733 List<OFMessage> msgList = new ArrayList<OFMessage>();
734 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200735
736 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
737
738 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200739 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200740 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200741 return;
742 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200743
Jonathan Hart7804bea2014-01-07 10:50:52 -0800744 flowPusher.add(sw, po);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200745 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300746
747 private String inetAddressToString(byte[] bytes) {
748 try {
749 return InetAddress.getByAddress(bytes).getHostAddress();
750 } catch (UnknownHostException e) {
751 log.debug("Invalid IP address", e);
752 return "";
753 }
754 }
755
756 /*
757 * IProxyArpService methods
758 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200759
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200760 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300761 public MACAddress getMacAddress(InetAddress ipAddress) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800762 //return arpCache.lookup(ipAddress);
763 return null;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200764 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200765
766 @Override
767 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
768 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200769 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200770
Jonathan Hart6e618212013-08-21 22:28:43 +1200771 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700772 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200773 sendArpRequestForAddress(ipAddress);
774 }
775 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300776
777 @Override
778 public List<String> getMappings() {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800779 //return arpCache.getMappings();
780 return new ArrayList<String>();
Jonathan Hart5afde492013-10-01 12:30:53 +1300781 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800782
783 /*
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800784 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800785 public void arpRequestNotification(ArpMessage arpMessage) {
pingping-lin017a8922013-12-11 11:15:33 +0800786 log.debug("Received ARP notification from other instances");
pingping-linb8757bf2013-12-13 01:48:58 +0800787
Jonathan Hartd3003252013-11-15 09:44:46 -0800788 switch (arpMessage.getType()){
789 case REQUEST:
pingping-linb8757bf2013-12-13 01:48:58 +0800790 if(arpMessage.getOutSwitch() == -1 || arpMessage.getOutPort() == -1){
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800791 broadcastArpRequestOutMyEdge(arpMessage.getPacket(),
792 arpMessage.getInSwitch(), arpMessage.getInPort());
pingping-linb8757bf2013-12-13 01:48:58 +0800793 }else{
794 sendArpRequestOutPort(arpMessage.getPacket(),arpMessage.getOutSwitch(),arpMessage.getOutPort());
795 log.debug("OutSwitch in ARP request message is: {}; OutPort in ARP request message is: {}",arpMessage.getOutSwitch(),arpMessage.getOutPort());
796 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800797 break;
798 case REPLY:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800799 log.debug("Received ARP reply notification for {}",
800 arpMessage.getAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800801 sendArpReplyToWaitingRequesters(arpMessage.getAddress(),arpMessage.getMAC());
Jonathan Hartd3003252013-11-15 09:44:46 -0800802 break;
803 }
804 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800805 */
Jonathan Hartd3003252013-11-15 09:44:46 -0800806
pingping-lin017a8922013-12-11 11:15:33 +0800807 private void sendArpReplyToWaitingRequesters(InetAddress address, MACAddress mac) {
Jonathan Hartd3003252013-11-15 09:44:46 -0800808 log.debug("Sending ARP reply for {} to requesters",
809 address.getHostAddress());
810
811 //See if anyone's waiting for this ARP reply
812 Set<ArpRequest> requests = arpRequests.get(address);
813
814 //Synchronize on the Multimap while using an iterator for one of the sets
815 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
816 synchronized (arpRequests) {
817 Iterator<ArpRequest> it = requests.iterator();
818 while (it.hasNext()) {
819 ArpRequest request = it.next();
820 it.remove();
821 requestsToSend.add(request);
822 }
823 }
824
pingping-lin017a8922013-12-11 11:15:33 +0800825 /*IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
Jonathan Hartd3003252013-11-15 09:44:46 -0800826 InetAddresses.coerceToInteger(address));
827
828 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
829
830 log.debug("Found {} at {} in network map",
pingping-lin017a8922013-12-11 11:15:33 +0800831 address.getHostAddress(), mac);*/
Jonathan Hartd3003252013-11-15 09:44:46 -0800832
833 //Don't hold an ARP lock while dispatching requests
834 for (ArpRequest request : requestsToSend) {
835 request.dispatchReply(address, mac);
836 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800837 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800838
839 @Override
840 public void arpReplyEvent(ArpReplyNotification arpReply) {
841 log.debug("Received ARP reply notification for {}",
842 arpReply.getTargetAddress());
843 sendArpReplyToWaitingRequesters(arpReply.getTargetAddress(),
844 arpReply.getTargetMacAddress());
845 }
846
847 @Override
848 public void packetOutNotification(
849 PacketOutNotification packetOutNotification) {
850
851 if (packetOutNotification instanceof SinglePacketOutNotification) {
852 SinglePacketOutNotification notification =
853 (SinglePacketOutNotification) packetOutNotification;
854 sendArpRequestOutPort(notification.packet, notification.getOutSwitch(),
855 notification.getOutPort());
856 }
857 else if (packetOutNotification instanceof BroadcastPacketOutNotification) {
858 BroadcastPacketOutNotification notification =
859 (BroadcastPacketOutNotification) packetOutNotification;
860 broadcastArpRequestOutMyEdge(notification.packet,
861 notification.getInSwitch(), notification.getInPort());
862 }
863 else {
864 log.warn("Unknown packet out notification received");
865 }
866 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200867}