blob: c7d495ca56345ad72cd411b12bfe22aba8c3f6c6 [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()));
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
pingping-linba5c52f2014-02-11 16:52:01 -0800255 // Only ARP requests sent by the controller will have the retry flag
256 // set, so for now we can just send a new ARP request for that address.
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200257 sendArpRequestForAddress(address);
258
259 for (ArpRequest request : entry.getValue()) {
260 arpRequests.put(address, new ArpRequest(request));
261 }
262 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200263 }
264
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200265 @Override
266 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700267 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200268 }
269
270 @Override
271 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700272 if (type == OFType.PACKET_IN) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800273 return "devicemanager".equals(name) || "onosdevicemanager".equals(name);
Jonathan Harta18e4792013-10-31 10:10:54 -0700274 }
275 else {
276 return false;
277 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200278 }
279
280 @Override
281 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800282 return type == OFType.PACKET_IN && "onosforwarding".equals(name);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200283 }
284
285 @Override
286 public Command receive(
287 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
288
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200289 OFPacketIn pi = (OFPacketIn) msg;
290
291 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
292 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
293
294 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800295 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200296 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800297 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200298 }
299 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart016f03b2014-01-23 13:56:43 -0800300 // For replies we simply send a notification via Hazelcast
Jonathan Hartc6325622014-01-14 16:37:50 -0800301 sendArpReplyNotification(eth, pi);
Jonathan Hart016f03b2014-01-23 13:56:43 -0800302
303 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200304 }
Jonathan Hart17672992013-12-12 16:15:16 -0800305
306 // Stop ARP packets here
307 return Command.STOP;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200308 }
309
Jonathan Hart17672992013-12-12 16:15:16 -0800310 // Propagate everything else
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200311 return Command.CONTINUE;
312 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200313
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800314 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200315 if (log.isTraceEnabled()) {
316 log.trace("ARP request received for {}",
317 inetAddressToString(arp.getTargetProtocolAddress()));
318 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200319
320 InetAddress target;
321 try {
pingping-linb8757bf2013-12-13 01:48:58 +0800322 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200323 } catch (UnknownHostException e) {
324 log.debug("Invalid address in ARP request", e);
325 return;
326 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200327
Jonathan Harta8887642013-10-28 13:46:54 -0700328 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200329 //If the request came from outside our network, we only care if
330 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700331 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200332 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700333 target.getHostAddress(), configService.getRouterMacAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800334
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200335 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700336 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200337 }
pingping-linb8757bf2013-12-13 01:48:58 +0800338
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200339 return;
340 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200341
Jonathan Harta18e4792013-10-31 10:10:54 -0700342 //MACAddress macAddress = arpCache.lookup(target);
pingping-linb8757bf2013-12-13 01:48:58 +0800343
344 arpRequests.put(target, new ArpRequest(
345 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
346
Jonathan Hart18ad9502013-12-15 18:28:00 -0800347 IDeviceObject targetDevice =
348 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
349
350 if (targetDevice == null) {
351 if (log.isTraceEnabled()) {
352 log.trace("No device info found for {} - broadcasting",
353 target.getHostAddress());
354 }
355
356 // We don't know the device so broadcast the request out
Jonathan Hart7804bea2014-01-07 10:50:52 -0800357 datagrid.sendPacketOutNotification(
358 new BroadcastPacketOutNotification(eth.serialize(),
359 sw.getId(), pi.getInPort()));
Jonathan Hart18ad9502013-12-15 18:28:00 -0800360 }
361 else {
362 // Even if the device exists in our database, we do not reply to
363 // the request directly, but check whether the device is still valid
Jonathan Harta18e4792013-10-31 10:10:54 -0700364 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800365
Jonathan Harta18e4792013-10-31 10:10:54 -0700366 if (log.isTraceEnabled()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800367 log.trace("The target Device Record in DB is: {} => {} from ARP request host at {}/{}",
Jonathan Hart18ad9502013-12-15 18:28:00 -0800368 new Object [] {
Jonathan Harta18e4792013-10-31 10:10:54 -0700369 inetAddressToString(arp.getTargetProtocolAddress()),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800370 macAddress,
Jonathan Harta18e4792013-10-31 10:10:54 -0700371 HexString.toHexString(sw.getId()), pi.getInPort()});
372 }
pingping-linb8757bf2013-12-13 01:48:58 +0800373
374 // sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
375
Jonathan Hart18ad9502013-12-15 18:28:00 -0800376 Iterable<IPortObject> outPorts = targetDevice.getAttachedPorts();
pingping-linb8757bf2013-12-13 01:48:58 +0800377
Jonathan Hart18ad9502013-12-15 18:28:00 -0800378 if (!outPorts.iterator().hasNext()){
379 if (log.isTraceEnabled()) {
380 log.trace("Device {} exists but is not connected to any ports" +
381 " - broadcasting", macAddress);
382 }
383
Jonathan Hart7804bea2014-01-07 10:50:52 -0800384 datagrid.sendPacketOutNotification(
385 new BroadcastPacketOutNotification(eth.serialize(),
386 sw.getId(), pi.getInPort()));
Jonathan Hart18ad9502013-12-15 18:28:00 -0800387 }
388 else {
pingping-linb8757bf2013-12-13 01:48:58 +0800389 for (IPortObject portObject : outPorts) {
Jonathan Hart7804bea2014-01-07 10:50:52 -0800390 //long outSwitch = 0;
391 //short outPort = 0;
pingping-linb8757bf2013-12-13 01:48:58 +0800392
Jonathan Hart7804bea2014-01-07 10:50:52 -0800393 /*
pingping-linb8757bf2013-12-13 01:48:58 +0800394 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800395 outPort = portObject.getNumber();
Jonathan Hart7804bea2014-01-07 10:50:52 -0800396 }*/
397 if (portObject.getLinkedPorts().iterator().hasNext()) {
398 continue;
pingping-linb8757bf2013-12-13 01:48:58 +0800399 }
pingping-linb8757bf2013-12-13 01:48:58 +0800400
Jonathan Hart7804bea2014-01-07 10:50:52 -0800401 short outPort = portObject.getNumber();
pingping-lin568c6012013-12-14 05:57:11 +0800402 ISwitchObject outSwitchObject = portObject.getSwitch();
Jonathan Hart7804bea2014-01-07 10:50:52 -0800403 long outSwitch = HexString.toLong(outSwitchObject.getDPID());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800404
405 if (log.isTraceEnabled()) {
406 log.trace("Probing device {} on port {}/{}",
407 new Object[] {macAddress,
408 HexString.toHexString(outSwitch), outPort});
409 }
pingping-lin568c6012013-12-14 05:57:11 +0800410
Jonathan Hart7804bea2014-01-07 10:50:52 -0800411 datagrid.sendPacketOutNotification(
412 new SinglePacketOutNotification(eth.serialize(),
413 outSwitch, outPort));
pingping-linb8757bf2013-12-13 01:48:58 +0800414 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200415 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200416 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200417 }
418
Jonathan Hart1912afc2013-10-11 12:02:44 +1300419 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200420 if (log.isTraceEnabled()) {
421 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
422 inetAddressToString(arp.getSenderProtocolAddress()),
423 HexString.toHexString(arp.getSenderHardwareAddress()),
424 HexString.toHexString(sw.getId()), pi.getInPort()});
425 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200426
Jonathan Hartabad6a52013-09-30 18:17:21 +1300427 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200428 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300429 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200430 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200431 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200432 return;
433 }
434
Jonathan Hartabad6a52013-09-30 18:17:21 +1300435 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
436
Jonathan Hartabad6a52013-09-30 18:17:21 +1300437 //See if anyone's waiting for this ARP reply
438 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200439
440 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200441 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200442 synchronized (arpRequests) {
443 Iterator<ArpRequest> it = requests.iterator();
444 while (it.hasNext()) {
445 ArpRequest request = it.next();
446 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200447 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200448 }
449 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200450
451 //Don't hold an ARP lock while dispatching requests
452 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300453 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200454 }
455 }
456
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200457 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200458 //TODO what should the sender IP address and MAC address be if no
459 //IP addresses are configured? Will there ever be a need to send
460 //ARP requests from the controller in that case?
461 //All-zero MAC address doesn't seem to work - hosts don't respond to it
462
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200463 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
464 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200465 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200466 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200467 (byte)0xff, (byte)0xff, (byte)0xff};
468
469 ARP arpRequest = new ARP();
470
471 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
472 .setProtocolType(ARP.PROTO_TYPE_IP)
473 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200474 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200475 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200476 .setTargetHardwareAddress(zeroMac)
477 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200478
Jonathan Harta8887642013-10-28 13:46:54 -0700479 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200480 //TODO hack for now as it's unclear what the MAC address should be
481 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200482 if (routerMacAddress != null) {
483 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200484 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200485 arpRequest.setSenderHardwareAddress(senderMacAddress);
486
487 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700488 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200489 if (intf != null) {
490 senderIPAddress = intf.getIpAddress().getAddress();
491 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200492
493 arpRequest.setSenderProtocolAddress(senderIPAddress);
494
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200495 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200496 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200497 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200498 .setEtherType(Ethernet.TYPE_ARP)
499 .setPayload(arpRequest);
500
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700501 if (vlan != NO_VLAN) {
502 eth.setVlanID(vlan)
503 .setPriorityCode((byte)0);
504 }
505
pingping-linba5c52f2014-02-11 16:52:01 -0800506 //sendArpRequestToSwitches(ipAddress, eth.serialize());
507 datagrid.sendPacketOutNotification(new SinglePacketOutNotification(eth.serialize(),intf.getDpid(),intf.getPort()));
Jonathan Hart2f790d22013-08-15 14:01:24 +1200508 }
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 {
pingping-linba5c52f2014-02-11 16:52:01 -0800534 //broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
535 broadcastArpRequestOutMyEdge(arpRequest, inSwitch, inPort);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200536 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200537 }
538
Jonathan Hartc6325622014-01-14 16:37:50 -0800539 private void sendArpReplyNotification(Ethernet eth, OFPacketIn pi) {
pingping-lin017a8922013-12-11 11:15:33 +0800540 ARP arp = (ARP) eth.getPayload();
541
542 if (log.isTraceEnabled()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800543 log.trace("Sending ARP reply for {} to other ONOS instances",
544 inetAddressToString(arp.getSenderProtocolAddress()));
pingping-lin017a8922013-12-11 11:15:33 +0800545 }
546
547 InetAddress targetAddress;
548 MACAddress mac = new MACAddress(arp.getSenderHardwareAddress());
549
550 try {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800551 targetAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
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 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800556
557 datagrid.sendArpReplyNotification(new ArpReplyNotification(targetAddress, mac));
pingping-lin017a8922013-12-11 11:15:33 +0800558 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800559
Jonathan Hart7804bea2014-01-07 10:50:52 -0800560 // This remains from the older single-instance ARP code. It used Floodlight
561 // APIs to find the edge of the network, but only worked on a single instance.
562 // We now do this using ONOS network graph APIs.
563 @Deprecated
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200564 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200565 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
566 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
567 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
568
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200569 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700570 //I think this means the switch doesn't have any links.
571 //continue;
572 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200573 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200574
Jonathan Harta18e4792013-10-31 10:10:54 -0700575
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200576 OFPacketOut po = new OFPacketOut();
577 po.setInPort(OFPort.OFPP_NONE)
578 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200579 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200580
581 List<OFAction> actions = new ArrayList<OFAction>();
582
583 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200584 if (linkPorts.contains(portNum) ||
585 (sw.getId() == inSwitch && portNum == inPort)){
586 //If this port isn't an edge port or is the ingress port
587 //for the ARP, don't broadcast out it
588 continue;
589 }
590
591 actions.add(new OFActionOutput(portNum));
592 }
593
594 po.setActions(actions);
595 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
596 po.setActionsLength(actionsLength);
597 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200598 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200599
600 List<OFMessage> msgList = new ArrayList<OFMessage>();
601 msgList.add(po);
602
603 try {
604 sw.write(msgList, null);
605 sw.flush();
606 } catch (IOException e) {
607 log.error("Failure writing packet out to switch", e);
608 }
609 }
610 }
611
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800612 private void broadcastArpRequestOutMyEdge(byte[] arpRequest,
613 long inSwitch, short inPort) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800614 List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
615
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800616 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
617
618 OFPacketOut po = new OFPacketOut();
619 po.setInPort(OFPort.OFPP_NONE)
620 .setBufferId(-1)
621 .setPacketData(arpRequest);
622
623 List<OFAction> actions = new ArrayList<OFAction>();
624
625 Iterable<IPortObject> ports
626 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
627 if (ports == null) {
628 continue;
629 }
630
631 for (IPortObject portObject : ports) {
632 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800633 short portNumber = portObject.getNumber();
634
635 if (sw.getId() == inSwitch && portNumber == inPort) {
636 // This is the port that the ARP message came in,
637 // so don't broadcast out this port
638 continue;
639 }
640
Jonathan Hartba9ced92013-11-24 16:52:13 -0800641 switchPorts.add(new SwitchPort(new Dpid(sw.getId()),
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800642 new Port(portNumber)));
643 actions.add(new OFActionOutput(portNumber));
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800644 }
645 }
646
647 po.setActions(actions);
648 short actionsLength = (short)
649 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
650 po.setActionsLength(actionsLength);
651 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
652 + arpRequest.length);
653
Jonathan Hart7804bea2014-01-07 10:50:52 -0800654 flowPusher.add(sw, po);
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800655 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800656
Jonathan Hart18ad9502013-12-15 18:28:00 -0800657 if (log.isTraceEnabled()) {
658 log.trace("Broadcast ARP request to: {}", switchPorts);
659 }
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800660 }
661
Jonathan Hart2f790d22013-08-15 14:01:24 +1200662 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200663 if (log.isTraceEnabled()) {
664 log.trace("Sending ARP request out {}/{}",
665 HexString.toHexString(dpid), port);
666 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200667
668 OFPacketOut po = new OFPacketOut();
669 po.setInPort(OFPort.OFPP_NONE)
670 .setBufferId(-1)
671 .setPacketData(arpRequest);
672
673 List<OFAction> actions = new ArrayList<OFAction>();
674 actions.add(new OFActionOutput(port));
675 po.setActions(actions);
676 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
677 po.setActionsLength(actionsLength);
678 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
679 + arpRequest.length);
680
681 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
682
683 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200684 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200685 return;
686 }
687
Jonathan Hart7804bea2014-01-07 10:50:52 -0800688 flowPusher.add(sw, po);
Jonathan Hart2f790d22013-08-15 14:01:24 +1200689 }
690
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300691 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200692 if (log.isTraceEnabled()) {
693 log.trace("Sending reply {} => {} to {}", new Object[] {
694 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300695 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200696 inetAddressToString(arpRequest.getSenderProtocolAddress())});
697 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200698
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200699 ARP arpReply = new ARP();
700 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
701 .setProtocolType(ARP.PROTO_TYPE_IP)
702 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200703 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200704 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300705 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200706 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
707 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
708 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
709
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700710
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200711 Ethernet eth = new Ethernet();
712 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300713 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200714 .setEtherType(Ethernet.TYPE_ARP)
715 .setPayload(arpReply);
716
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700717 if (vlan != NO_VLAN) {
718 eth.setVlanID(vlan)
719 .setPriorityCode((byte)0);
720 }
721
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200722 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200723 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200724
725 OFPacketOut po = new OFPacketOut();
726 po.setInPort(OFPort.OFPP_NONE)
727 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200728 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200729 .setActions(actions)
730 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
731 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
732 + po.getPacketData().length);
733
734 List<OFMessage> msgList = new ArrayList<OFMessage>();
735 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200736
737 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
738
739 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200740 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200741 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200742 return;
743 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200744
Jonathan Hart7804bea2014-01-07 10:50:52 -0800745 flowPusher.add(sw, po);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200746 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300747
748 private String inetAddressToString(byte[] bytes) {
749 try {
750 return InetAddress.getByAddress(bytes).getHostAddress();
751 } catch (UnknownHostException e) {
752 log.debug("Invalid IP address", e);
753 return "";
754 }
755 }
756
757 /*
758 * IProxyArpService methods
759 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200760
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200761 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300762 public MACAddress getMacAddress(InetAddress ipAddress) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800763 //return arpCache.lookup(ipAddress);
764 return null;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200765 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200766
767 @Override
768 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
769 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200770 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200771
Jonathan Hart6e618212013-08-21 22:28:43 +1200772 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700773 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200774 sendArpRequestForAddress(ipAddress);
775 }
776 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300777
778 @Override
779 public List<String> getMappings() {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800780 //return arpCache.getMappings();
781 return new ArrayList<String>();
Jonathan Hart5afde492013-10-01 12:30:53 +1300782 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800783
784 /*
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800785 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800786 public void arpRequestNotification(ArpMessage arpMessage) {
pingping-lin017a8922013-12-11 11:15:33 +0800787 log.debug("Received ARP notification from other instances");
pingping-linb8757bf2013-12-13 01:48:58 +0800788
Jonathan Hartd3003252013-11-15 09:44:46 -0800789 switch (arpMessage.getType()){
790 case REQUEST:
pingping-linb8757bf2013-12-13 01:48:58 +0800791 if(arpMessage.getOutSwitch() == -1 || arpMessage.getOutPort() == -1){
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800792 broadcastArpRequestOutMyEdge(arpMessage.getPacket(),
793 arpMessage.getInSwitch(), arpMessage.getInPort());
pingping-linb8757bf2013-12-13 01:48:58 +0800794 }else{
795 sendArpRequestOutPort(arpMessage.getPacket(),arpMessage.getOutSwitch(),arpMessage.getOutPort());
796 log.debug("OutSwitch in ARP request message is: {}; OutPort in ARP request message is: {}",arpMessage.getOutSwitch(),arpMessage.getOutPort());
797 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800798 break;
799 case REPLY:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800800 log.debug("Received ARP reply notification for {}",
801 arpMessage.getAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800802 sendArpReplyToWaitingRequesters(arpMessage.getAddress(),arpMessage.getMAC());
Jonathan Hartd3003252013-11-15 09:44:46 -0800803 break;
804 }
805 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800806 */
Jonathan Hartd3003252013-11-15 09:44:46 -0800807
pingping-lin017a8922013-12-11 11:15:33 +0800808 private void sendArpReplyToWaitingRequesters(InetAddress address, MACAddress mac) {
Jonathan Hartd3003252013-11-15 09:44:46 -0800809 log.debug("Sending ARP reply for {} to requesters",
810 address.getHostAddress());
811
812 //See if anyone's waiting for this ARP reply
813 Set<ArpRequest> requests = arpRequests.get(address);
814
815 //Synchronize on the Multimap while using an iterator for one of the sets
816 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
817 synchronized (arpRequests) {
818 Iterator<ArpRequest> it = requests.iterator();
819 while (it.hasNext()) {
820 ArpRequest request = it.next();
821 it.remove();
822 requestsToSend.add(request);
823 }
824 }
825
pingping-lin017a8922013-12-11 11:15:33 +0800826 /*IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
Jonathan Hartd3003252013-11-15 09:44:46 -0800827 InetAddresses.coerceToInteger(address));
828
829 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
830
831 log.debug("Found {} at {} in network map",
pingping-lin017a8922013-12-11 11:15:33 +0800832 address.getHostAddress(), mac);*/
Jonathan Hartd3003252013-11-15 09:44:46 -0800833
834 //Don't hold an ARP lock while dispatching requests
835 for (ArpRequest request : requestsToSend) {
836 request.dispatchReply(address, mac);
837 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800838 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800839
840 @Override
841 public void arpReplyEvent(ArpReplyNotification arpReply) {
842 log.debug("Received ARP reply notification for {}",
843 arpReply.getTargetAddress());
844 sendArpReplyToWaitingRequesters(arpReply.getTargetAddress(),
845 arpReply.getTargetMacAddress());
846 }
847
848 @Override
849 public void packetOutNotification(
850 PacketOutNotification packetOutNotification) {
851
852 if (packetOutNotification instanceof SinglePacketOutNotification) {
853 SinglePacketOutNotification notification =
854 (SinglePacketOutNotification) packetOutNotification;
855 sendArpRequestOutPort(notification.packet, notification.getOutSwitch(),
856 notification.getOutPort());
857 }
858 else if (packetOutNotification instanceof BroadcastPacketOutNotification) {
859 BroadcastPacketOutNotification notification =
860 (BroadcastPacketOutNotification) packetOutNotification;
861 broadcastArpRequestOutMyEdge(notification.packet,
862 notification.getInSwitch(), notification.getInPort());
863 }
864 else {
865 log.warn("Unknown packet out notification received");
866 }
867 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200868}