blob: ac99678e2c63786972324d956c6888dde095410b [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 Hart18ad55c2013-11-11 22:49:55 -0800190
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700191 deviceStorage = new DeviceStorageImpl();
192 deviceStorage.init("");
193
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200194 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200195 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200196 @Override
197 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200198 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200199 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200200 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200201 }
202
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200203 /*
204 * Function that runs periodically to manage the asynchronous request mechanism.
205 * It basically cleans up old ARP requests if we don't get a response for them.
206 * The caller can designate that a request should be retried indefinitely, and
207 * this task will handle that as well.
208 */
209 private void doPeriodicArpProcessing() {
210 SetMultimap<InetAddress, ArpRequest> retryList
211 = HashMultimap.<InetAddress, ArpRequest>create();
212
213 //Have to synchronize externally on the Multimap while using an iterator,
214 //even though it's a synchronizedMultimap
Jonathan Hart18ad9502013-12-15 18:28:00 -0800215 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200216 Iterator<Map.Entry<InetAddress, ArpRequest>> it
217 = arpRequests.entries().iterator();
218
219 while (it.hasNext()) {
220 Map.Entry<InetAddress, ArpRequest> entry
221 = it.next();
222 ArpRequest request = entry.getValue();
223 if (request.isExpired()) {
224 log.debug("Cleaning expired ARP request for {}",
225 entry.getKey().getHostAddress());
226
Jonathan Hart18ad9502013-12-15 18:28:00 -0800227 // If the ARP request is expired and then delete the device
228 // TODO check whether this is OK from this thread
pingping-lin017a8922013-12-11 11:15:33 +0800229 IDeviceObject targetDevice =
230 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(entry.getKey()));
231
Jonathan Hart18ad9502013-12-15 18:28:00 -0800232 if (targetDevice != null) {
233 deviceStorage.removeDevice(targetDevice);
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800234 if (log.isDebugEnabled()) {
235 log.debug("RemoveDevice: {} due to no have not recieve the ARP reply", targetDevice);
236 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800237 }
pingping-lin017a8922013-12-11 11:15:33 +0800238
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200239 it.remove();
240
241 if (request.shouldRetry()) {
242 retryList.put(entry.getKey(), request);
243 }
244 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200245 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200246 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200247
248 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
249 : retryList.asMap().entrySet()) {
250
251 InetAddress address = entry.getKey();
252
253 log.debug("Resending ARP request for {}", address.getHostAddress());
254
255 sendArpRequestForAddress(address);
256
257 for (ArpRequest request : entry.getValue()) {
258 arpRequests.put(address, new ArpRequest(request));
259 }
260 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200261 }
262
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200263 @Override
264 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700265 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200266 }
267
268 @Override
269 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700270 if (type == OFType.PACKET_IN) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800271 return "devicemanager".equals(name) || "onosdevicemanager".equals(name);
Jonathan Harta18e4792013-10-31 10:10:54 -0700272 }
273 else {
274 return false;
275 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200276 }
277
278 @Override
279 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800280 return type == OFType.PACKET_IN && "onosforwarding".equals(name);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200281 }
282
283 @Override
284 public Command receive(
285 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
286
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200287 OFPacketIn pi = (OFPacketIn) msg;
288
289 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
290 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
291
292 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800293 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200294 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800295 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200296 }
297 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800298 handleArpReply(sw, pi, arp);
Jonathan Hartc6325622014-01-14 16:37:50 -0800299 sendArpReplyNotification(eth, pi);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200300 }
Jonathan Hart17672992013-12-12 16:15:16 -0800301
302 // Stop ARP packets here
303 return Command.STOP;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200304 }
305
Jonathan Hart17672992013-12-12 16:15:16 -0800306 // Propagate everything else
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200307 return Command.CONTINUE;
308 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200309
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800310 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200311 if (log.isTraceEnabled()) {
312 log.trace("ARP request received for {}",
313 inetAddressToString(arp.getTargetProtocolAddress()));
314 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200315
316 InetAddress target;
317 try {
pingping-linb8757bf2013-12-13 01:48:58 +0800318 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200319 } catch (UnknownHostException e) {
320 log.debug("Invalid address in ARP request", e);
321 return;
322 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200323
Jonathan Harta8887642013-10-28 13:46:54 -0700324 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200325 //If the request came from outside our network, we only care if
326 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700327 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200328 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700329 target.getHostAddress(), configService.getRouterMacAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800330
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200331 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700332 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200333 }
pingping-linb8757bf2013-12-13 01:48:58 +0800334
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200335 return;
336 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200337
Jonathan Harta18e4792013-10-31 10:10:54 -0700338 //MACAddress macAddress = arpCache.lookup(target);
pingping-linb8757bf2013-12-13 01:48:58 +0800339
340 arpRequests.put(target, new ArpRequest(
341 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
342
Jonathan Hart18ad9502013-12-15 18:28:00 -0800343 IDeviceObject targetDevice =
344 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
345
346 if (targetDevice == null) {
347 if (log.isTraceEnabled()) {
348 log.trace("No device info found for {} - broadcasting",
349 target.getHostAddress());
350 }
351
352 // We don't know the device so broadcast the request out
Jonathan Hart7804bea2014-01-07 10:50:52 -0800353 datagrid.sendPacketOutNotification(
354 new BroadcastPacketOutNotification(eth.serialize(),
355 sw.getId(), pi.getInPort()));
Jonathan Hart18ad9502013-12-15 18:28:00 -0800356 }
357 else {
358 // Even if the device exists in our database, we do not reply to
359 // the request directly, but check whether the device is still valid
Jonathan Harta18e4792013-10-31 10:10:54 -0700360 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800361
Jonathan Harta18e4792013-10-31 10:10:54 -0700362 if (log.isTraceEnabled()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800363 log.trace("The target Device Record in DB is: {} => {} from ARP request host at {}/{}",
Jonathan Hart18ad9502013-12-15 18:28:00 -0800364 new Object [] {
pingping-linb8757bf2013-12-13 01:48:58 +0800365 inetAddressToString(arp.getTargetProtocolAddress()),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800366 macAddress,
pingping-linb8757bf2013-12-13 01:48:58 +0800367 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Harta18e4792013-10-31 10:10:54 -0700368 }
pingping-linb8757bf2013-12-13 01:48:58 +0800369
370 // sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
371
Jonathan Hart18ad9502013-12-15 18:28:00 -0800372 Iterable<IPortObject> outPorts = targetDevice.getAttachedPorts();
pingping-linb8757bf2013-12-13 01:48:58 +0800373
Jonathan Hart18ad9502013-12-15 18:28:00 -0800374 if (!outPorts.iterator().hasNext()){
375 if (log.isTraceEnabled()) {
376 log.trace("Device {} exists but is not connected to any ports" +
377 " - broadcasting", macAddress);
378 }
379
Jonathan Hart7804bea2014-01-07 10:50:52 -0800380 datagrid.sendPacketOutNotification(
381 new BroadcastPacketOutNotification(eth.serialize(),
382 sw.getId(), pi.getInPort()));
Jonathan Hart18ad9502013-12-15 18:28:00 -0800383 }
384 else {
pingping-linb8757bf2013-12-13 01:48:58 +0800385 for (IPortObject portObject : outPorts) {
Jonathan Hart7804bea2014-01-07 10:50:52 -0800386 //long outSwitch = 0;
387 //short outPort = 0;
pingping-linb8757bf2013-12-13 01:48:58 +0800388
Jonathan Hart7804bea2014-01-07 10:50:52 -0800389 /*
pingping-linb8757bf2013-12-13 01:48:58 +0800390 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800391 outPort = portObject.getNumber();
Jonathan Hart7804bea2014-01-07 10:50:52 -0800392 }*/
393 if (portObject.getLinkedPorts().iterator().hasNext()) {
394 continue;
Jonathan Hart18ad9502013-12-15 18:28:00 -0800395 }
pingping-linb8757bf2013-12-13 01:48:58 +0800396
Jonathan Hart7804bea2014-01-07 10:50:52 -0800397 short outPort = portObject.getNumber();
pingping-lin568c6012013-12-14 05:57:11 +0800398 ISwitchObject outSwitchObject = portObject.getSwitch();
Jonathan Hart7804bea2014-01-07 10:50:52 -0800399 long outSwitch = HexString.toLong(outSwitchObject.getDPID());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800400
401 if (log.isTraceEnabled()) {
402 log.trace("Probing device {} on port {}/{}",
403 new Object[] {macAddress,
404 HexString.toHexString(outSwitch), outPort});
405 }
pingping-lin568c6012013-12-14 05:57:11 +0800406
Jonathan Hart7804bea2014-01-07 10:50:52 -0800407 datagrid.sendPacketOutNotification(
408 new SinglePacketOutNotification(eth.serialize(),
409 outSwitch, outPort));
pingping-linb8757bf2013-12-13 01:48:58 +0800410 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200411 }
pingping-lin017a8922013-12-11 11:15:33 +0800412 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200413 }
414
Jonathan Hart1912afc2013-10-11 12:02:44 +1300415 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200416 if (log.isTraceEnabled()) {
417 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
418 inetAddressToString(arp.getSenderProtocolAddress()),
419 HexString.toHexString(arp.getSenderHardwareAddress()),
420 HexString.toHexString(sw.getId()), pi.getInPort()});
421 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200422
Jonathan Hartabad6a52013-09-30 18:17:21 +1300423 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200424 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300425 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200426 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200427 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200428 return;
429 }
430
Jonathan Hartabad6a52013-09-30 18:17:21 +1300431 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
432
pingping-lin017a8922013-12-11 11:15:33 +0800433 //arpCache.update(senderIpAddress, senderMacAddress);
Jonathan Hartabad6a52013-09-30 18:17:21 +1300434
435 //See if anyone's waiting for this ARP reply
436 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200437
438 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200439 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200440 synchronized (arpRequests) {
441 Iterator<ArpRequest> it = requests.iterator();
442 while (it.hasNext()) {
443 ArpRequest request = it.next();
444 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200445 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200446 }
447 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200448
449 //Don't hold an ARP lock while dispatching requests
450 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300451 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200452 }
453 }
454
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200455 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200456 //TODO what should the sender IP address and MAC address be if no
457 //IP addresses are configured? Will there ever be a need to send
458 //ARP requests from the controller in that case?
459 //All-zero MAC address doesn't seem to work - hosts don't respond to it
460
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200461 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
462 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200463 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200464 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200465 (byte)0xff, (byte)0xff, (byte)0xff};
466
467 ARP arpRequest = new ARP();
468
469 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
470 .setProtocolType(ARP.PROTO_TYPE_IP)
471 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200472 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200473 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200474 .setTargetHardwareAddress(zeroMac)
475 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200476
Jonathan Harta8887642013-10-28 13:46:54 -0700477 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200478 //TODO hack for now as it's unclear what the MAC address should be
479 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200480 if (routerMacAddress != null) {
481 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200482 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200483 arpRequest.setSenderHardwareAddress(senderMacAddress);
484
485 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700486 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200487 if (intf != null) {
488 senderIPAddress = intf.getIpAddress().getAddress();
489 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200490
491 arpRequest.setSenderProtocolAddress(senderIPAddress);
492
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200493 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200494 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200495 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200496 .setEtherType(Ethernet.TYPE_ARP)
497 .setPayload(arpRequest);
498
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700499 if (vlan != NO_VLAN) {
500 eth.setVlanID(vlan)
501 .setPriorityCode((byte)0);
502 }
503
Jonathan Hart2f790d22013-08-15 14:01:24 +1200504 sendArpRequestToSwitches(ipAddress, eth.serialize());
505 }
506
507 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200508 sendArpRequestToSwitches(dstAddress, arpRequest,
509 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200510 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200511
Jonathan Hart2f790d22013-08-15 14:01:24 +1200512 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
513 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200514
Jonathan Harta8887642013-10-28 13:46:54 -0700515 if (configService.hasLayer3Configuration()) {
516 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200517 if (intf != null) {
518 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
519 }
520 else {
521 //TODO here it should be broadcast out all non-interface edge ports.
522 //I think we can assume that if it's not a request for an external
523 //network, it's an ARP for a host in our own network. So we want to
524 //send it out all edge ports that don't have an interface configured
525 //to ensure it reaches all hosts in our network.
526 log.debug("No interface found to send ARP request for {}",
527 dstAddress.getHostAddress());
528 }
529 }
530 else {
531 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
532 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200533 }
534
Jonathan Hartc6325622014-01-14 16:37:50 -0800535 private void sendArpReplyNotification(Ethernet eth, OFPacketIn pi) {
pingping-lin017a8922013-12-11 11:15:33 +0800536 ARP arp = (ARP) eth.getPayload();
537
538 if (log.isTraceEnabled()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800539 log.trace("Sending ARP reply for {} to other ONOS instances",
540 inetAddressToString(arp.getSenderProtocolAddress()));
pingping-lin017a8922013-12-11 11:15:33 +0800541 }
542
543 InetAddress targetAddress;
544 MACAddress mac = new MACAddress(arp.getSenderHardwareAddress());
545
546 try {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800547 targetAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800548 } catch (UnknownHostException e) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800549 log.error("Unknown host", e);
550 return;
pingping-lin017a8922013-12-11 11:15:33 +0800551 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800552
553 datagrid.sendArpReplyNotification(new ArpReplyNotification(targetAddress, mac));
pingping-lin017a8922013-12-11 11:15:33 +0800554 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800555
Jonathan Hart7804bea2014-01-07 10:50:52 -0800556 // This remains from the older single-instance ARP code. It used Floodlight
557 // APIs to find the edge of the network, but only worked on a single instance.
558 // We now do this using ONOS network graph APIs.
559 @Deprecated
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200560 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200561 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
562 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
563 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
564
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200565 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700566 //I think this means the switch doesn't have any links.
567 //continue;
568 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200569 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200570
Jonathan Harta18e4792013-10-31 10:10:54 -0700571
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200572 OFPacketOut po = new OFPacketOut();
573 po.setInPort(OFPort.OFPP_NONE)
574 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200575 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200576
577 List<OFAction> actions = new ArrayList<OFAction>();
578
579 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200580 if (linkPorts.contains(portNum) ||
581 (sw.getId() == inSwitch && portNum == inPort)){
582 //If this port isn't an edge port or is the ingress port
583 //for the ARP, don't broadcast out it
584 continue;
585 }
586
587 actions.add(new OFActionOutput(portNum));
588 }
589
590 po.setActions(actions);
591 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
592 po.setActionsLength(actionsLength);
593 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200594 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200595
596 List<OFMessage> msgList = new ArrayList<OFMessage>();
597 msgList.add(po);
598
599 try {
600 sw.write(msgList, null);
601 sw.flush();
602 } catch (IOException e) {
603 log.error("Failure writing packet out to switch", e);
604 }
605 }
606 }
607
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800608 private void broadcastArpRequestOutMyEdge(byte[] arpRequest,
609 long inSwitch, short inPort) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800610 List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
611
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800612 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
613
614 OFPacketOut po = new OFPacketOut();
615 po.setInPort(OFPort.OFPP_NONE)
616 .setBufferId(-1)
617 .setPacketData(arpRequest);
618
619 List<OFAction> actions = new ArrayList<OFAction>();
620
621 Iterable<IPortObject> ports
622 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
623 if (ports == null) {
624 continue;
625 }
626
627 for (IPortObject portObject : ports) {
628 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800629 short portNumber = portObject.getNumber();
630
631 if (sw.getId() == inSwitch && portNumber == inPort) {
632 // This is the port that the ARP message came in,
633 // so don't broadcast out this port
634 continue;
635 }
636
Jonathan Hartba9ced92013-11-24 16:52:13 -0800637 switchPorts.add(new SwitchPort(new Dpid(sw.getId()),
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800638 new Port(portNumber)));
639 actions.add(new OFActionOutput(portNumber));
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800640 }
641 }
642
643 po.setActions(actions);
644 short actionsLength = (short)
645 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
646 po.setActionsLength(actionsLength);
647 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
648 + arpRequest.length);
649
Jonathan Hart7804bea2014-01-07 10:50:52 -0800650 flowPusher.add(sw, po);
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800651 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800652
Jonathan Hart18ad9502013-12-15 18:28:00 -0800653 if (log.isTraceEnabled()) {
654 log.trace("Broadcast ARP request to: {}", switchPorts);
655 }
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800656 }
657
Jonathan Hart2f790d22013-08-15 14:01:24 +1200658 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200659 if (log.isTraceEnabled()) {
660 log.trace("Sending ARP request out {}/{}",
661 HexString.toHexString(dpid), port);
662 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200663
664 OFPacketOut po = new OFPacketOut();
665 po.setInPort(OFPort.OFPP_NONE)
666 .setBufferId(-1)
667 .setPacketData(arpRequest);
668
669 List<OFAction> actions = new ArrayList<OFAction>();
670 actions.add(new OFActionOutput(port));
671 po.setActions(actions);
672 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
673 po.setActionsLength(actionsLength);
674 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
675 + arpRequest.length);
676
677 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
678
679 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200680 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200681 return;
682 }
683
Jonathan Hart7804bea2014-01-07 10:50:52 -0800684 flowPusher.add(sw, po);
Jonathan Hart2f790d22013-08-15 14:01:24 +1200685 }
686
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300687 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200688 if (log.isTraceEnabled()) {
689 log.trace("Sending reply {} => {} to {}", new Object[] {
690 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300691 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200692 inetAddressToString(arpRequest.getSenderProtocolAddress())});
693 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200694
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200695 ARP arpReply = new ARP();
696 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
697 .setProtocolType(ARP.PROTO_TYPE_IP)
698 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200699 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200700 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300701 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200702 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
703 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
704 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
705
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700706
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200707 Ethernet eth = new Ethernet();
708 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300709 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200710 .setEtherType(Ethernet.TYPE_ARP)
711 .setPayload(arpReply);
712
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700713 if (vlan != NO_VLAN) {
714 eth.setVlanID(vlan)
715 .setPriorityCode((byte)0);
716 }
717
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200718 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200719 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200720
721 OFPacketOut po = new OFPacketOut();
722 po.setInPort(OFPort.OFPP_NONE)
723 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200724 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200725 .setActions(actions)
726 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
727 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
728 + po.getPacketData().length);
729
730 List<OFMessage> msgList = new ArrayList<OFMessage>();
731 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200732
733 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
734
735 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200736 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200737 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200738 return;
739 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200740
Jonathan Hart7804bea2014-01-07 10:50:52 -0800741 flowPusher.add(sw, po);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200742 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300743
744 private String inetAddressToString(byte[] bytes) {
745 try {
746 return InetAddress.getByAddress(bytes).getHostAddress();
747 } catch (UnknownHostException e) {
748 log.debug("Invalid IP address", e);
749 return "";
750 }
751 }
752
753 /*
754 * IProxyArpService methods
755 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200756
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200757 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300758 public MACAddress getMacAddress(InetAddress ipAddress) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800759 //return arpCache.lookup(ipAddress);
760 return null;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200761 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200762
763 @Override
764 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
765 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200766 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200767
Jonathan Hart6e618212013-08-21 22:28:43 +1200768 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700769 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200770 sendArpRequestForAddress(ipAddress);
771 }
772 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300773
774 @Override
775 public List<String> getMappings() {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800776 //return arpCache.getMappings();
777 return new ArrayList<String>();
Jonathan Hart5afde492013-10-01 12:30:53 +1300778 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800779
780 /*
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800781 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800782 public void arpRequestNotification(ArpMessage arpMessage) {
pingping-lin017a8922013-12-11 11:15:33 +0800783 log.debug("Received ARP notification from other instances");
pingping-linb8757bf2013-12-13 01:48:58 +0800784
Jonathan Hartd3003252013-11-15 09:44:46 -0800785 switch (arpMessage.getType()){
786 case REQUEST:
pingping-linb8757bf2013-12-13 01:48:58 +0800787 if(arpMessage.getOutSwitch() == -1 || arpMessage.getOutPort() == -1){
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800788 broadcastArpRequestOutMyEdge(arpMessage.getPacket(),
789 arpMessage.getInSwitch(), arpMessage.getInPort());
pingping-linb8757bf2013-12-13 01:48:58 +0800790 }else{
791 sendArpRequestOutPort(arpMessage.getPacket(),arpMessage.getOutSwitch(),arpMessage.getOutPort());
792 log.debug("OutSwitch in ARP request message is: {}; OutPort in ARP request message is: {}",arpMessage.getOutSwitch(),arpMessage.getOutPort());
793 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800794 break;
795 case REPLY:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800796 log.debug("Received ARP reply notification for {}",
797 arpMessage.getAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800798 sendArpReplyToWaitingRequesters(arpMessage.getAddress(),arpMessage.getMAC());
Jonathan Hartd3003252013-11-15 09:44:46 -0800799 break;
800 }
801 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800802 */
Jonathan Hartd3003252013-11-15 09:44:46 -0800803
pingping-lin017a8922013-12-11 11:15:33 +0800804 private void sendArpReplyToWaitingRequesters(InetAddress address, MACAddress mac) {
Jonathan Hartd3003252013-11-15 09:44:46 -0800805 log.debug("Sending ARP reply for {} to requesters",
806 address.getHostAddress());
807
808 //See if anyone's waiting for this ARP reply
809 Set<ArpRequest> requests = arpRequests.get(address);
810
811 //Synchronize on the Multimap while using an iterator for one of the sets
812 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
813 synchronized (arpRequests) {
814 Iterator<ArpRequest> it = requests.iterator();
815 while (it.hasNext()) {
816 ArpRequest request = it.next();
817 it.remove();
818 requestsToSend.add(request);
819 }
820 }
821
pingping-lin017a8922013-12-11 11:15:33 +0800822 /*IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
Jonathan Hartd3003252013-11-15 09:44:46 -0800823 InetAddresses.coerceToInteger(address));
824
825 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
826
827 log.debug("Found {} at {} in network map",
pingping-lin017a8922013-12-11 11:15:33 +0800828 address.getHostAddress(), mac);*/
Jonathan Hartd3003252013-11-15 09:44:46 -0800829
830 //Don't hold an ARP lock while dispatching requests
831 for (ArpRequest request : requestsToSend) {
832 request.dispatchReply(address, mac);
833 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800834 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800835
836 @Override
837 public void arpReplyEvent(ArpReplyNotification arpReply) {
838 log.debug("Received ARP reply notification for {}",
839 arpReply.getTargetAddress());
840 sendArpReplyToWaitingRequesters(arpReply.getTargetAddress(),
841 arpReply.getTargetMacAddress());
842 }
843
844 @Override
845 public void packetOutNotification(
846 PacketOutNotification packetOutNotification) {
847
848 if (packetOutNotification instanceof SinglePacketOutNotification) {
849 SinglePacketOutNotification notification =
850 (SinglePacketOutNotification) packetOutNotification;
851 sendArpRequestOutPort(notification.packet, notification.getOutSwitch(),
852 notification.getOutPort());
853 }
854 else if (packetOutNotification instanceof BroadcastPacketOutNotification) {
855 BroadcastPacketOutNotification notification =
856 (BroadcastPacketOutNotification) packetOutNotification;
857 broadcastArpRequestOutMyEdge(notification.packet,
858 notification.getInSwitch(), notification.getInPort());
859 }
860 else {
861 log.warn("Unknown packet out notification received");
862 }
863 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200864}