blob: fd21265d15b5bdcf2c2a6c3112f3a68cbddc89c9 [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;
Naoki Shiota78e403c2014-02-20 17:13:36 -080090 private boolean sent = false;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120091 private long requestTime;
92
Jonathan Hart4dfc3652013-08-02 20:22:36 +120093 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120094 this.requester = requester;
95 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120096 }
97
Jonathan Hart4dfc3652013-08-02 20:22:36 +120098 public ArpRequest(ArpRequest old) {
99 this.requester = old.requester;
100 this.retry = old.retry;
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200101 }
102
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200103 public boolean isExpired() {
Naoki Shiota78e403c2014-02-20 17:13:36 -0800104 return sent && ((System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200105 }
106
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200107 public boolean shouldRetry() {
108 return retry;
109 }
110
Jonathan Hartabad6a52013-09-30 18:17:21 +1300111 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200112 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200113 }
Naoki Shiota78e403c2014-02-20 17:13:36 -0800114
115 public void setRequestTime() {
116 this.requestTime = System.currentTimeMillis();
117 this.sent = true;
118 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200119 }
120
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300121 private class HostArpRequester implements IArpRequester {
122 private final ARP arpRequest;
123 private final long dpid;
124 private final short port;
125
126 public HostArpRequester(ARP arpRequest, long dpid, short port) {
127 this.arpRequest = arpRequest;
128 this.dpid = dpid;
129 this.port = port;
130 }
131
132 @Override
133 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
134 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
135 }
136 }
137
Jonathan Harte93aed42013-12-05 18:39:50 -0800138 @Override
139 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
140 Collection<Class<? extends IFloodlightService>> l
141 = new ArrayList<Class<? extends IFloodlightService>>();
142 l.add(IProxyArpService.class);
143 return l;
Jonathan Harta8887642013-10-28 13:46:54 -0700144 }
Jonathan Harte93aed42013-12-05 18:39:50 -0800145
146 @Override
147 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
148 Map<Class<? extends IFloodlightService>, IFloodlightService> m
149 = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
150 m.put(IProxyArpService.class, this);
151 return m;
152 }
153
154 @Override
155 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
156 Collection<Class<? extends IFloodlightService>> dependencies
157 = new ArrayList<Class<? extends IFloodlightService>>();
158 dependencies.add(IFloodlightProviderService.class);
159 dependencies.add(ITopologyService.class);
160 dependencies.add(IRestApiService.class);
161 dependencies.add(IDatagridService.class);
162 dependencies.add(IConfigInfoService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800163 dependencies.add(IFlowPusherService.class);
Jonathan Harte93aed42013-12-05 18:39:50 -0800164 return dependencies;
165 }
Jonathan Harta8887642013-10-28 13:46:54 -0700166
Jonathan Harte93aed42013-12-05 18:39:50 -0800167 @Override
168 public void init(FloodlightModuleContext context){
169 this.floodlightProvider =
170 context.getServiceImpl(IFloodlightProviderService.class);
171 this.topology = context.getServiceImpl(ITopologyService.class);
172 this.datagrid = context.getServiceImpl(IDatagridService.class);
173 this.configService = context.getServiceImpl(IConfigInfoService.class);
174 this.restApi = context.getServiceImpl(IRestApiService.class);
Jonathan Hart7804bea2014-01-07 10:50:52 -0800175 this.flowPusher = context.getServiceImpl(IFlowPusherService.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200176
Jonathan Hart18ad9502013-12-15 18:28:00 -0800177 //arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200178
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200179 arpRequests = Multimaps.synchronizedSetMultimap(
180 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800181
182 topoSwitchService = new TopoSwitchServiceImpl();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200183 }
184
Jonathan Harte93aed42013-12-05 18:39:50 -0800185 @Override
186 public void startUp(FloodlightModuleContext context) {
Jonathan Harta8887642013-10-28 13:46:54 -0700187 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700188 log.info("vlan set to {}", this.vlan);
189
Jonathan Hart5afde492013-10-01 12:30:53 +1300190 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700191 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300192
Jonathan Hart7804bea2014-01-07 10:50:52 -0800193 datagrid.registerPacketOutEventHandler(this);
Jonathan Hart016f03b2014-01-23 13:56:43 -0800194 datagrid.registerArpReplyEventHandler(this);
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800195
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700196 deviceStorage = new DeviceStorageImpl();
yoshitomob292c622013-11-23 14:35:58 -0800197 deviceStorage.init("","");
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700198
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200199 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200200 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200201 @Override
202 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200203 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200204 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200205 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200206 }
207
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200208 /*
209 * Function that runs periodically to manage the asynchronous request mechanism.
210 * It basically cleans up old ARP requests if we don't get a response for them.
211 * The caller can designate that a request should be retried indefinitely, and
212 * this task will handle that as well.
213 */
214 private void doPeriodicArpProcessing() {
215 SetMultimap<InetAddress, ArpRequest> retryList
216 = HashMultimap.<InetAddress, ArpRequest>create();
217
218 //Have to synchronize externally on the Multimap while using an iterator,
219 //even though it's a synchronizedMultimap
Jonathan Hart18ad9502013-12-15 18:28:00 -0800220 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200221 Iterator<Map.Entry<InetAddress, ArpRequest>> it
222 = arpRequests.entries().iterator();
223
224 while (it.hasNext()) {
225 Map.Entry<InetAddress, ArpRequest> entry
226 = it.next();
227 ArpRequest request = entry.getValue();
228 if (request.isExpired()) {
229 log.debug("Cleaning expired ARP request for {}",
230 entry.getKey().getHostAddress());
231
Jonathan Hart18ad9502013-12-15 18:28:00 -0800232 // If the ARP request is expired and then delete the device
233 // TODO check whether this is OK from this thread
pingping-lin017a8922013-12-11 11:15:33 +0800234 IDeviceObject targetDevice =
235 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(entry.getKey()));
Jonathan Hart18ad9502013-12-15 18:28:00 -0800236 if (targetDevice != null) {
237 deviceStorage.removeDevice(targetDevice);
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800238 if (log.isDebugEnabled()) {
239 log.debug("RemoveDevice: {} due to no have not recieve the ARP reply", targetDevice);
240 }
Jonathan Hartd857ad62013-12-14 18:08:17 -0800241 }
pingping-lin017a8922013-12-11 11:15:33 +0800242
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200243 it.remove();
244
245 if (request.shouldRetry()) {
246 retryList.put(entry.getKey(), request);
247 }
248 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200249 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200250 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200251
252 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
253 : retryList.asMap().entrySet()) {
254
255 InetAddress address = entry.getKey();
256
257 log.debug("Resending ARP request for {}", address.getHostAddress());
258
pingping-linba5c52f2014-02-11 16:52:01 -0800259 // Only ARP requests sent by the controller will have the retry flag
260 // set, so for now we can just send a new ARP request for that address.
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200261 sendArpRequestForAddress(address);
262
263 for (ArpRequest request : entry.getValue()) {
264 arpRequests.put(address, new ArpRequest(request));
265 }
266 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200267 }
268
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200269 @Override
270 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700271 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200272 }
273
274 @Override
275 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700276 if (type == OFType.PACKET_IN) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800277 return "devicemanager".equals(name) || "onosdevicemanager".equals(name);
Jonathan Harta18e4792013-10-31 10:10:54 -0700278 }
279 else {
280 return false;
281 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200282 }
283
284 @Override
285 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartd857ad62013-12-14 18:08:17 -0800286 return type == OFType.PACKET_IN && "onosforwarding".equals(name);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200287 }
288
289 @Override
290 public Command receive(
291 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
292
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200293 OFPacketIn pi = (OFPacketIn) msg;
294
295 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
296 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
297
298 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hart5ea7cc02013-11-12 11:56:23 -0800299 ARP arp = (ARP) eth.getPayload();
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200300 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800301 handleArpRequest(sw, pi, arp, eth);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200302 }
303 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart016f03b2014-01-23 13:56:43 -0800304 // For replies we simply send a notification via Hazelcast
Jonathan Hartc6325622014-01-14 16:37:50 -0800305 sendArpReplyNotification(eth, pi);
Jonathan Hart016f03b2014-01-23 13:56:43 -0800306
307 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200308 }
Jonathan Hart17672992013-12-12 16:15:16 -0800309
310 // Stop ARP packets here
311 return Command.STOP;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200312 }
313
Jonathan Hart17672992013-12-12 16:15:16 -0800314 // Propagate everything else
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200315 return Command.CONTINUE;
316 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200317
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800318 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp, Ethernet eth) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200319 if (log.isTraceEnabled()) {
320 log.trace("ARP request received for {}",
321 inetAddressToString(arp.getTargetProtocolAddress()));
322 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200323
324 InetAddress target;
325 try {
pingping-linb8757bf2013-12-13 01:48:58 +0800326 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200327 } catch (UnknownHostException e) {
328 log.debug("Invalid address in ARP request", e);
329 return;
330 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200331
Jonathan Harta8887642013-10-28 13:46:54 -0700332 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200333 //If the request came from outside our network, we only care if
334 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700335 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200336 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700337 target.getHostAddress(), configService.getRouterMacAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800338
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200339 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700340 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200341 }
pingping-linb8757bf2013-12-13 01:48:58 +0800342
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200343 return;
344 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200345
Jonathan Harta18e4792013-10-31 10:10:54 -0700346 //MACAddress macAddress = arpCache.lookup(target);
pingping-linb8757bf2013-12-13 01:48:58 +0800347
348 arpRequests.put(target, new ArpRequest(
349 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
350
Jonathan Hart18ad9502013-12-15 18:28:00 -0800351 IDeviceObject targetDevice =
352 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
353
354 if (targetDevice == null) {
355 if (log.isTraceEnabled()) {
356 log.trace("No device info found for {} - broadcasting",
357 target.getHostAddress());
358 }
359
360 // We don't know the device so broadcast the request out
Jonathan Hart7804bea2014-01-07 10:50:52 -0800361 datagrid.sendPacketOutNotification(
Naoki Shiota78e403c2014-02-20 17:13:36 -0800362 new BroadcastPacketOutNotification(eth.serialize(),
363 target, sw.getId(), pi.getInPort()));
Jonathan Hart18ad9502013-12-15 18:28:00 -0800364 }
365 else {
366 // Even if the device exists in our database, we do not reply to
367 // the request directly, but check whether the device is still valid
Jonathan Harta18e4792013-10-31 10:10:54 -0700368 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
pingping-linb8757bf2013-12-13 01:48:58 +0800369
Jonathan Harta18e4792013-10-31 10:10:54 -0700370 if (log.isTraceEnabled()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800371 log.trace("The target Device Record in DB is: {} => {} from ARP request host at {}/{}",
Jonathan Hart18ad9502013-12-15 18:28:00 -0800372 new Object [] {
Jonathan Harta18e4792013-10-31 10:10:54 -0700373 inetAddressToString(arp.getTargetProtocolAddress()),
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800374 macAddress,
Jonathan Harta18e4792013-10-31 10:10:54 -0700375 HexString.toHexString(sw.getId()), pi.getInPort()});
376 }
pingping-linb8757bf2013-12-13 01:48:58 +0800377
378 // sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
379
Jonathan Hart18ad9502013-12-15 18:28:00 -0800380 Iterable<IPortObject> outPorts = targetDevice.getAttachedPorts();
pingping-linb8757bf2013-12-13 01:48:58 +0800381
Jonathan Hart18ad9502013-12-15 18:28:00 -0800382 if (!outPorts.iterator().hasNext()){
383 if (log.isTraceEnabled()) {
384 log.trace("Device {} exists but is not connected to any ports" +
385 " - broadcasting", macAddress);
386 }
387
Jonathan Hart7804bea2014-01-07 10:50:52 -0800388 datagrid.sendPacketOutNotification(
389 new BroadcastPacketOutNotification(eth.serialize(),
Naoki Shiota78e403c2014-02-20 17:13:36 -0800390 target, sw.getId(), pi.getInPort()));
Jonathan Hart18ad9502013-12-15 18:28:00 -0800391 }
392 else {
pingping-linb8757bf2013-12-13 01:48:58 +0800393 for (IPortObject portObject : outPorts) {
Jonathan Hart7804bea2014-01-07 10:50:52 -0800394 //long outSwitch = 0;
395 //short outPort = 0;
pingping-linb8757bf2013-12-13 01:48:58 +0800396
Jonathan Hart7804bea2014-01-07 10:50:52 -0800397 /*
pingping-linb8757bf2013-12-13 01:48:58 +0800398 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800399 outPort = portObject.getNumber();
Jonathan Hart7804bea2014-01-07 10:50:52 -0800400 }*/
401 if (portObject.getLinkedPorts().iterator().hasNext()) {
402 continue;
pingping-linb8757bf2013-12-13 01:48:58 +0800403 }
pingping-linb8757bf2013-12-13 01:48:58 +0800404
Jonathan Hart7804bea2014-01-07 10:50:52 -0800405 short outPort = portObject.getNumber();
pingping-lin568c6012013-12-14 05:57:11 +0800406 ISwitchObject outSwitchObject = portObject.getSwitch();
Jonathan Hart7804bea2014-01-07 10:50:52 -0800407 long outSwitch = HexString.toLong(outSwitchObject.getDPID());
Jonathan Hart18ad9502013-12-15 18:28:00 -0800408
409 if (log.isTraceEnabled()) {
410 log.trace("Probing device {} on port {}/{}",
411 new Object[] {macAddress,
412 HexString.toHexString(outSwitch), outPort});
413 }
pingping-lin568c6012013-12-14 05:57:11 +0800414
Jonathan Hart7804bea2014-01-07 10:50:52 -0800415 datagrid.sendPacketOutNotification(
416 new SinglePacketOutNotification(eth.serialize(),
Naoki Shiota78e403c2014-02-20 17:13:36 -0800417 target, outSwitch, outPort));
pingping-linb8757bf2013-12-13 01:48:58 +0800418 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200419 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200420 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200421 }
422
Jonathan Hart1912afc2013-10-11 12:02:44 +1300423 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200424 if (log.isTraceEnabled()) {
425 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
426 inetAddressToString(arp.getSenderProtocolAddress()),
427 HexString.toHexString(arp.getSenderHardwareAddress()),
428 HexString.toHexString(sw.getId()), pi.getInPort()});
429 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200430
Jonathan Hartabad6a52013-09-30 18:17:21 +1300431 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200432 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300433 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200434 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200435 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200436 return;
437 }
438
Jonathan Hartabad6a52013-09-30 18:17:21 +1300439 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
440
Jonathan Hartabad6a52013-09-30 18:17:21 +1300441 //See if anyone's waiting for this ARP reply
442 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200443
444 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200445 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200446 synchronized (arpRequests) {
447 Iterator<ArpRequest> it = requests.iterator();
448 while (it.hasNext()) {
449 ArpRequest request = it.next();
450 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200451 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200452 }
453 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200454
455 //Don't hold an ARP lock while dispatching requests
456 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300457 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200458 }
459 }
460
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200461 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200462 //TODO what should the sender IP address and MAC address be if no
463 //IP addresses are configured? Will there ever be a need to send
464 //ARP requests from the controller in that case?
465 //All-zero MAC address doesn't seem to work - hosts don't respond to it
466
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200467 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
468 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200469 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200470 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200471 (byte)0xff, (byte)0xff, (byte)0xff};
472
473 ARP arpRequest = new ARP();
474
475 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
476 .setProtocolType(ARP.PROTO_TYPE_IP)
477 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200478 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200479 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200480 .setTargetHardwareAddress(zeroMac)
481 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200482
Jonathan Harta8887642013-10-28 13:46:54 -0700483 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200484 //TODO hack for now as it's unclear what the MAC address should be
485 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200486 if (routerMacAddress != null) {
487 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200488 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200489 arpRequest.setSenderHardwareAddress(senderMacAddress);
490
491 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700492 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200493 if (intf != null) {
494 senderIPAddress = intf.getIpAddress().getAddress();
495 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200496
497 arpRequest.setSenderProtocolAddress(senderIPAddress);
498
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200499 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200500 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200501 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200502 .setEtherType(Ethernet.TYPE_ARP)
503 .setPayload(arpRequest);
504
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700505 if (vlan != NO_VLAN) {
506 eth.setVlanID(vlan)
507 .setPriorityCode((byte)0);
508 }
509
pingping-linba5c52f2014-02-11 16:52:01 -0800510 //sendArpRequestToSwitches(ipAddress, eth.serialize());
Naoki Shiota78e403c2014-02-20 17:13:36 -0800511 datagrid.sendPacketOutNotification(new SinglePacketOutNotification(eth.serialize(),
512 ipAddress, intf.getDpid(),intf.getPort()));
Jonathan Hart2f790d22013-08-15 14:01:24 +1200513 }
514
515 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200516 sendArpRequestToSwitches(dstAddress, arpRequest,
517 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200518 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200519
Jonathan Hart2f790d22013-08-15 14:01:24 +1200520 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
521 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200522
Jonathan Harta8887642013-10-28 13:46:54 -0700523 if (configService.hasLayer3Configuration()) {
524 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200525 if (intf != null) {
526 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
527 }
528 else {
529 //TODO here it should be broadcast out all non-interface edge ports.
530 //I think we can assume that if it's not a request for an external
531 //network, it's an ARP for a host in our own network. So we want to
532 //send it out all edge ports that don't have an interface configured
533 //to ensure it reaches all hosts in our network.
534 log.debug("No interface found to send ARP request for {}",
535 dstAddress.getHostAddress());
536 }
537 }
538 else {
pingping-linba5c52f2014-02-11 16:52:01 -0800539 //broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
540 broadcastArpRequestOutMyEdge(arpRequest, inSwitch, inPort);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200541 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200542 }
543
Jonathan Hartc6325622014-01-14 16:37:50 -0800544 private void sendArpReplyNotification(Ethernet eth, OFPacketIn pi) {
pingping-lin017a8922013-12-11 11:15:33 +0800545 ARP arp = (ARP) eth.getPayload();
546
547 if (log.isTraceEnabled()) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800548 log.trace("Sending ARP reply for {} to other ONOS instances",
549 inetAddressToString(arp.getSenderProtocolAddress()));
pingping-lin017a8922013-12-11 11:15:33 +0800550 }
551
552 InetAddress targetAddress;
553 MACAddress mac = new MACAddress(arp.getSenderHardwareAddress());
554
555 try {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800556 targetAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800557 } catch (UnknownHostException e) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800558 log.error("Unknown host", e);
559 return;
pingping-lin017a8922013-12-11 11:15:33 +0800560 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800561
562 datagrid.sendArpReplyNotification(new ArpReplyNotification(targetAddress, mac));
pingping-lin017a8922013-12-11 11:15:33 +0800563 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800564
Jonathan Hart7804bea2014-01-07 10:50:52 -0800565 // This remains from the older single-instance ARP code. It used Floodlight
566 // APIs to find the edge of the network, but only worked on a single instance.
567 // We now do this using ONOS network graph APIs.
568 @Deprecated
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200569 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200570 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
571 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
572 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
573
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200574 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700575 //I think this means the switch doesn't have any links.
576 //continue;
577 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200578 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200579
Jonathan Harta18e4792013-10-31 10:10:54 -0700580
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200581 OFPacketOut po = new OFPacketOut();
582 po.setInPort(OFPort.OFPP_NONE)
583 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200584 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200585
586 List<OFAction> actions = new ArrayList<OFAction>();
587
588 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200589 if (linkPorts.contains(portNum) ||
590 (sw.getId() == inSwitch && portNum == inPort)){
591 //If this port isn't an edge port or is the ingress port
592 //for the ARP, don't broadcast out it
593 continue;
594 }
595
596 actions.add(new OFActionOutput(portNum));
597 }
598
599 po.setActions(actions);
600 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
601 po.setActionsLength(actionsLength);
602 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200603 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200604
605 List<OFMessage> msgList = new ArrayList<OFMessage>();
606 msgList.add(po);
607
608 try {
609 sw.write(msgList, null);
610 sw.flush();
611 } catch (IOException e) {
612 log.error("Failure writing packet out to switch", e);
613 }
614 }
615 }
616
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800617 private void broadcastArpRequestOutMyEdge(byte[] arpRequest,
618 long inSwitch, short inPort) {
Jonathan Hartba9ced92013-11-24 16:52:13 -0800619 List<SwitchPort> switchPorts = new ArrayList<SwitchPort>();
620
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800621 for (IOFSwitch sw : floodlightProvider.getSwitches().values()) {
622
623 OFPacketOut po = new OFPacketOut();
624 po.setInPort(OFPort.OFPP_NONE)
625 .setBufferId(-1)
626 .setPacketData(arpRequest);
627
628 List<OFAction> actions = new ArrayList<OFAction>();
629
630 Iterable<IPortObject> ports
631 = topoSwitchService.getPortsOnSwitch(sw.getStringId());
632 if (ports == null) {
633 continue;
634 }
635
636 for (IPortObject portObject : ports) {
637 if (!portObject.getLinkedPorts().iterator().hasNext()) {
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800638 short portNumber = portObject.getNumber();
639
640 if (sw.getId() == inSwitch && portNumber == inPort) {
641 // This is the port that the ARP message came in,
642 // so don't broadcast out this port
643 continue;
644 }
645
Jonathan Hartba9ced92013-11-24 16:52:13 -0800646 switchPorts.add(new SwitchPort(new Dpid(sw.getId()),
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800647 new Port(portNumber)));
648 actions.add(new OFActionOutput(portNumber));
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800649 }
650 }
651
652 po.setActions(actions);
653 short actionsLength = (short)
654 (actions.size() * OFActionOutput.MINIMUM_LENGTH);
655 po.setActionsLength(actionsLength);
656 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
657 + arpRequest.length);
658
Jonathan Hart7804bea2014-01-07 10:50:52 -0800659 flowPusher.add(sw, po);
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800660 }
Jonathan Hartba9ced92013-11-24 16:52:13 -0800661
Jonathan Hart18ad9502013-12-15 18:28:00 -0800662 if (log.isTraceEnabled()) {
663 log.trace("Broadcast ARP request to: {}", switchPorts);
664 }
Jonathan Hart9a21e0d2013-11-14 15:09:09 -0800665 }
666
Jonathan Hart2f790d22013-08-15 14:01:24 +1200667 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200668 if (log.isTraceEnabled()) {
669 log.trace("Sending ARP request out {}/{}",
670 HexString.toHexString(dpid), port);
671 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200672
673 OFPacketOut po = new OFPacketOut();
674 po.setInPort(OFPort.OFPP_NONE)
675 .setBufferId(-1)
676 .setPacketData(arpRequest);
677
678 List<OFAction> actions = new ArrayList<OFAction>();
679 actions.add(new OFActionOutput(port));
680 po.setActions(actions);
681 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
682 po.setActionsLength(actionsLength);
683 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
684 + arpRequest.length);
685
686 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
687
688 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200689 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200690 return;
691 }
692
Jonathan Hart7804bea2014-01-07 10:50:52 -0800693 flowPusher.add(sw, po);
Jonathan Hart2f790d22013-08-15 14:01:24 +1200694 }
695
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300696 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200697 if (log.isTraceEnabled()) {
698 log.trace("Sending reply {} => {} to {}", new Object[] {
699 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300700 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200701 inetAddressToString(arpRequest.getSenderProtocolAddress())});
702 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200703
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200704 ARP arpReply = new ARP();
705 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
706 .setProtocolType(ARP.PROTO_TYPE_IP)
707 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200708 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200709 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300710 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200711 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
712 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
713 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
714
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700715
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200716 Ethernet eth = new Ethernet();
717 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300718 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200719 .setEtherType(Ethernet.TYPE_ARP)
720 .setPayload(arpReply);
721
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700722 if (vlan != NO_VLAN) {
723 eth.setVlanID(vlan)
724 .setPriorityCode((byte)0);
725 }
726
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200727 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200728 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200729
730 OFPacketOut po = new OFPacketOut();
731 po.setInPort(OFPort.OFPP_NONE)
732 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200733 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200734 .setActions(actions)
735 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
736 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
737 + po.getPacketData().length);
738
739 List<OFMessage> msgList = new ArrayList<OFMessage>();
740 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200741
742 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
743
744 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200745 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200746 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200747 return;
748 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200749
Jonathan Hart7804bea2014-01-07 10:50:52 -0800750 flowPusher.add(sw, po);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200751 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300752
753 private String inetAddressToString(byte[] bytes) {
754 try {
755 return InetAddress.getByAddress(bytes).getHostAddress();
756 } catch (UnknownHostException e) {
757 log.debug("Invalid IP address", e);
758 return "";
759 }
760 }
761
762 /*
763 * IProxyArpService methods
764 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200765
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200766 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300767 public MACAddress getMacAddress(InetAddress ipAddress) {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800768 //return arpCache.lookup(ipAddress);
769 return null;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200770 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200771
772 @Override
773 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
774 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200775 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200776
Jonathan Hart6e618212013-08-21 22:28:43 +1200777 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700778 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200779 sendArpRequestForAddress(ipAddress);
780 }
781 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300782
783 @Override
784 public List<String> getMappings() {
Jonathan Hart18ad9502013-12-15 18:28:00 -0800785 //return arpCache.getMappings();
786 return new ArrayList<String>();
Jonathan Hart5afde492013-10-01 12:30:53 +1300787 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800788
789 /*
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800790 @Override
Jonathan Hartd3003252013-11-15 09:44:46 -0800791 public void arpRequestNotification(ArpMessage arpMessage) {
pingping-lin017a8922013-12-11 11:15:33 +0800792 log.debug("Received ARP notification from other instances");
pingping-linb8757bf2013-12-13 01:48:58 +0800793
Jonathan Hartd3003252013-11-15 09:44:46 -0800794 switch (arpMessage.getType()){
795 case REQUEST:
pingping-linb8757bf2013-12-13 01:48:58 +0800796 if(arpMessage.getOutSwitch() == -1 || arpMessage.getOutPort() == -1){
Jonathan Hartf9bd98d2013-12-13 11:40:55 -0800797 broadcastArpRequestOutMyEdge(arpMessage.getPacket(),
798 arpMessage.getInSwitch(), arpMessage.getInPort());
pingping-linb8757bf2013-12-13 01:48:58 +0800799 }else{
800 sendArpRequestOutPort(arpMessage.getPacket(),arpMessage.getOutSwitch(),arpMessage.getOutPort());
801 log.debug("OutSwitch in ARP request message is: {}; OutPort in ARP request message is: {}",arpMessage.getOutSwitch(),arpMessage.getOutPort());
802 }
Jonathan Hartd3003252013-11-15 09:44:46 -0800803 break;
804 case REPLY:
Jonathan Hartba9ced92013-11-24 16:52:13 -0800805 log.debug("Received ARP reply notification for {}",
806 arpMessage.getAddress());
pingping-lin017a8922013-12-11 11:15:33 +0800807 sendArpReplyToWaitingRequesters(arpMessage.getAddress(),arpMessage.getMAC());
Jonathan Hartd3003252013-11-15 09:44:46 -0800808 break;
809 }
810 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800811 */
Jonathan Hartd3003252013-11-15 09:44:46 -0800812
pingping-lin017a8922013-12-11 11:15:33 +0800813 private void sendArpReplyToWaitingRequesters(InetAddress address, MACAddress mac) {
Jonathan Hartd3003252013-11-15 09:44:46 -0800814 log.debug("Sending ARP reply for {} to requesters",
815 address.getHostAddress());
816
817 //See if anyone's waiting for this ARP reply
818 Set<ArpRequest> requests = arpRequests.get(address);
819
820 //Synchronize on the Multimap while using an iterator for one of the sets
821 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
822 synchronized (arpRequests) {
823 Iterator<ArpRequest> it = requests.iterator();
824 while (it.hasNext()) {
825 ArpRequest request = it.next();
826 it.remove();
827 requestsToSend.add(request);
828 }
829 }
830
pingping-lin017a8922013-12-11 11:15:33 +0800831 /*IDeviceObject deviceObject = deviceStorage.getDeviceByIP(
Jonathan Hartd3003252013-11-15 09:44:46 -0800832 InetAddresses.coerceToInteger(address));
833
834 MACAddress mac = MACAddress.valueOf(deviceObject.getMACAddress());
835
836 log.debug("Found {} at {} in network map",
pingping-lin017a8922013-12-11 11:15:33 +0800837 address.getHostAddress(), mac);*/
Jonathan Hartd3003252013-11-15 09:44:46 -0800838
839 //Don't hold an ARP lock while dispatching requests
840 for (ArpRequest request : requestsToSend) {
841 request.dispatchReply(address, mac);
842 }
Jonathan Hart18ad55c2013-11-11 22:49:55 -0800843 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800844
845 @Override
846 public void arpReplyEvent(ArpReplyNotification arpReply) {
847 log.debug("Received ARP reply notification for {}",
848 arpReply.getTargetAddress());
849 sendArpReplyToWaitingRequesters(arpReply.getTargetAddress(),
850 arpReply.getTargetMacAddress());
851 }
852
853 @Override
854 public void packetOutNotification(
855 PacketOutNotification packetOutNotification) {
856
857 if (packetOutNotification instanceof SinglePacketOutNotification) {
858 SinglePacketOutNotification notification =
859 (SinglePacketOutNotification) packetOutNotification;
860 sendArpRequestOutPort(notification.packet, notification.getOutSwitch(),
861 notification.getOutPort());
Naoki Shiota78e403c2014-02-20 17:13:36 -0800862
863 // set timestamp
864 InetAddress addr = notification.getTargetAddress();
865 if (addr != null) {
866 for (ArpRequest request : arpRequests.get(addr)) {
867 request.setRequestTime();
868 }
869 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800870 }
871 else if (packetOutNotification instanceof BroadcastPacketOutNotification) {
872 BroadcastPacketOutNotification notification =
873 (BroadcastPacketOutNotification) packetOutNotification;
874 broadcastArpRequestOutMyEdge(notification.packet,
875 notification.getInSwitch(), notification.getInPort());
Naoki Shiota78e403c2014-02-20 17:13:36 -0800876
877 // set timestamp
878 InetAddress addr = notification.getTargetAddress();
879 if (addr != null) {
880 for (ArpRequest request : arpRequests.get(addr)) {
881 request.setRequestTime();
882 }
883 }
Jonathan Hart7804bea2014-01-07 10:50:52 -0800884 }
885 else {
886 log.warn("Unknown packet out notification received");
887 }
888 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200889}