blob: a5dabc920100f96ecb0e0ed5f81e816ff93ab261 [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 Harta18e4792013-10-31 10:10:54 -07008import java.util.HashSet;
Jonathan Hart6261dcd2013-07-22 17:58:35 +12009import java.util.Iterator;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120010import java.util.List;
11import java.util.Map;
12import java.util.Set;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120013import java.util.Timer;
14import java.util.TimerTask;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120015
16import net.floodlightcontroller.core.FloodlightContext;
17import net.floodlightcontroller.core.IFloodlightProviderService;
18import net.floodlightcontroller.core.IOFMessageListener;
19import net.floodlightcontroller.core.IOFSwitch;
Jonathan Harta18e4792013-10-31 10:10:54 -070020import net.floodlightcontroller.devicemanager.IDeviceService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120021import net.floodlightcontroller.packet.ARP;
22import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart08ee8522013-09-22 17:34:43 +120023import net.floodlightcontroller.packet.IPv4;
Jonathan Hart5afde492013-10-01 12:30:53 +130024import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120025import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120026import net.floodlightcontroller.util.MACAddress;
Jonathan Hart2f790d22013-08-15 14:01:24 +120027import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070028import net.onrc.onos.ofcontroller.core.IDeviceStorage;
29import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
Jonathan Hartebba1e12013-10-29 11:37:02 -070030import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070031import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120032
33import org.openflow.protocol.OFMessage;
34import org.openflow.protocol.OFPacketIn;
35import org.openflow.protocol.OFPacketOut;
36import org.openflow.protocol.OFPort;
37import org.openflow.protocol.OFType;
38import org.openflow.protocol.action.OFAction;
39import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120040import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
Jonathan Hart4dfc3652013-08-02 20:22:36 +120044import com.google.common.collect.HashMultimap;
45import com.google.common.collect.Multimaps;
46import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070047import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120048
Jonathan Hart6261dcd2013-07-22 17:58:35 +120049public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120050 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120051
Jonathan Hartdf6ec332013-08-04 01:37:14 +120052 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130053
54 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120055
Jonathan Harta8887642013-10-28 13:46:54 -070056 private IFloodlightProviderService floodlightProvider;
57 private ITopologyService topology;
Jonathan Harta18e4792013-10-31 10:10:54 -070058 private IDeviceService deviceService;
Jonathan Harta8887642013-10-28 13:46:54 -070059 private IConfigInfoService configService;
60 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120061
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070062 private IDeviceStorage deviceStorage;
63
Jonathan Hart1cf9de02013-10-21 17:42:29 -070064 private short vlan;
65 private static final short NO_VLAN = 0;
66
Jonathan Harta8887642013-10-28 13:46:54 -070067 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120068
Jonathan Harta8887642013-10-28 13:46:54 -070069 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120070
Jonathan Hartabad6a52013-09-30 18:17:21 +130071 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120072 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130073 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120074 private long requestTime;
75
Jonathan Hart4dfc3652013-08-02 20:22:36 +120076 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120077 this.requester = requester;
78 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120079 this.requestTime = System.currentTimeMillis();
80 }
81
Jonathan Hart4dfc3652013-08-02 20:22:36 +120082 public ArpRequest(ArpRequest old) {
83 this.requester = old.requester;
84 this.retry = old.retry;
85 this.requestTime = System.currentTimeMillis();
86 }
87
Jonathan Hart4dfc3652013-08-02 20:22:36 +120088 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +130089 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120090 }
91
Jonathan Hart4dfc3652013-08-02 20:22:36 +120092 public boolean shouldRetry() {
93 return retry;
94 }
95
Jonathan Hartabad6a52013-09-30 18:17:21 +130096 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120097 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120098 }
99 }
100
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300101 private class HostArpRequester implements IArpRequester {
102 private final ARP arpRequest;
103 private final long dpid;
104 private final short port;
105
106 public HostArpRequester(ARP arpRequest, long dpid, short port) {
107 this.arpRequest = arpRequest;
108 this.dpid = dpid;
109 this.port = port;
110 }
111
112 @Override
113 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
114 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
115 }
116 }
117
Jonathan Harta8887642013-10-28 13:46:54 -0700118 /*
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200119 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Harta8887642013-10-28 13:46:54 -0700120 ITopologyService topology, IConfigInfoService configService,
Jonathan Hart5afde492013-10-01 12:30:53 +1300121 IRestApiService restApi){
Jonathan Harta8887642013-10-28 13:46:54 -0700122
123 }
124 */
125
126 public void init(IFloodlightProviderService floodlightProvider,
Jonathan Harta18e4792013-10-31 10:10:54 -0700127 ITopologyService topology, IDeviceService deviceService,
128 IConfigInfoService config, IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200129 this.floodlightProvider = floodlightProvider;
130 this.topology = topology;
Jonathan Harta18e4792013-10-31 10:10:54 -0700131 this.deviceService = deviceService;
Jonathan Harta8887642013-10-28 13:46:54 -0700132 this.configService = config;
Jonathan Hart5afde492013-10-01 12:30:53 +1300133 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200134
Jonathan Hartabad6a52013-09-30 18:17:21 +1300135 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200136
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200137 arpRequests = Multimaps.synchronizedSetMultimap(
138 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200139 }
140
Jonathan Harta8887642013-10-28 13:46:54 -0700141 public void startUp() {
142 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700143 log.info("vlan set to {}", this.vlan);
144
Jonathan Hart5afde492013-10-01 12:30:53 +1300145 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700146 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300147
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700148 deviceStorage = new DeviceStorageImpl();
149 deviceStorage.init("");
150
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200151 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200152 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200153 @Override
154 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200155 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200156 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200157 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200158 }
159
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200160 /*
161 * Function that runs periodically to manage the asynchronous request mechanism.
162 * It basically cleans up old ARP requests if we don't get a response for them.
163 * The caller can designate that a request should be retried indefinitely, and
164 * this task will handle that as well.
165 */
166 private void doPeriodicArpProcessing() {
167 SetMultimap<InetAddress, ArpRequest> retryList
168 = HashMultimap.<InetAddress, ArpRequest>create();
169
170 //Have to synchronize externally on the Multimap while using an iterator,
171 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200172 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200173 log.debug("Current have {} outstanding requests",
174 arpRequests.size());
175
176 Iterator<Map.Entry<InetAddress, ArpRequest>> it
177 = arpRequests.entries().iterator();
178
179 while (it.hasNext()) {
180 Map.Entry<InetAddress, ArpRequest> entry
181 = it.next();
182 ArpRequest request = entry.getValue();
183 if (request.isExpired()) {
184 log.debug("Cleaning expired ARP request for {}",
185 entry.getKey().getHostAddress());
186
187 it.remove();
188
189 if (request.shouldRetry()) {
190 retryList.put(entry.getKey(), request);
191 }
192 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200193 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200194 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200195
196 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
197 : retryList.asMap().entrySet()) {
198
199 InetAddress address = entry.getKey();
200
201 log.debug("Resending ARP request for {}", address.getHostAddress());
202
203 sendArpRequestForAddress(address);
204
205 for (ArpRequest request : entry.getValue()) {
206 arpRequests.put(address, new ArpRequest(request));
207 }
208 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200209 }
210
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200211 @Override
212 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700213 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200214 }
215
216 @Override
217 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700218 if (type == OFType.PACKET_IN) {
219 return "devicemanager".equals(name);
220 }
221 else {
222 return false;
223 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200224 }
225
226 @Override
227 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200228 return false;
229 }
230
231 @Override
232 public Command receive(
233 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
234
235 if (msg.getType() != OFType.PACKET_IN){
236 return Command.CONTINUE;
237 }
238
239 OFPacketIn pi = (OFPacketIn) msg;
240
241 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
242 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
243
244 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200245 ARP arp = (ARP) eth.getPayload();
246
247 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700248 //TODO check what the DeviceManager does about propagating
249 //or swallowing ARPs. We want to go after DeviceManager in the
250 //chain but we really need it to CONTINUE ARP packets so we can
251 //get them.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200252 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200253 }
254 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700255 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200256 }
257 }
258
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200259 //TODO should we propagate ARP or swallow it?
260 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200261 return Command.CONTINUE;
262 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200263
Jonathan Hart1912afc2013-10-11 12:02:44 +1300264 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200265 if (log.isTraceEnabled()) {
266 log.trace("ARP request received for {}",
267 inetAddressToString(arp.getTargetProtocolAddress()));
268 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200269
270 InetAddress target;
271 try {
272 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
273 } catch (UnknownHostException e) {
274 log.debug("Invalid address in ARP request", e);
275 return;
276 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200277
Jonathan Harta8887642013-10-28 13:46:54 -0700278 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200279 //If the request came from outside our network, we only care if
280 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700281 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200282 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700283 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200284
285 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700286 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200287 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200288
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200289 return;
290 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200291
Jonathan Harta18e4792013-10-31 10:10:54 -0700292 //MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200293
Jonathan Harta18e4792013-10-31 10:10:54 -0700294 //IDevice dstDevice = deviceService.fcStore.get(cntx, IDeviceService.CONTEXT_DST_DEVICE);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700295 //Iterator<? extends IDevice> it = deviceService.queryDevices(
296 //null, null, InetAddresses.coerceToInteger(target), null, null);
Jonathan Harta18e4792013-10-31 10:10:54 -0700297
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700298 //IDevice targetDevice = null;
299 //if (it.hasNext()) {
300 //targetDevice = it.next();
301 //}
302 IDeviceObject targetDevice =
303 deviceStorage.getDeviceByIP(InetAddresses.coerceToInteger(target));
Jonathan Harta18e4792013-10-31 10:10:54 -0700304
305 if (targetDevice != null) {
306 //We have the device in our database, so send a reply
307 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
308
309 if (log.isTraceEnabled()) {
310 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
311 inetAddressToString(arp.getTargetProtocolAddress()),
312 macAddress.toString(),
313 HexString.toHexString(sw.getId()), pi.getInPort()});
314 }
315
316 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
317 }
318
319 /*if (macAddress == null){
Jonathan Hartabad6a52013-09-30 18:17:21 +1300320 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200321
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200322 //Record where the request came from so we know where to send the reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700323 //arpRequests.put(target, new ArpRequest(
324 //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200325
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200326 //Flood the request out edge ports
Jonathan Harta18e4792013-10-31 10:10:54 -0700327 //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200328 }
329 else {
330 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200331 if (log.isTraceEnabled()) {
332 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
333 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300334 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200335 HexString.toHexString(sw.getId()), pi.getInPort()});
336 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200337
Jonathan Hartabad6a52013-09-30 18:17:21 +1300338 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Harta18e4792013-10-31 10:10:54 -0700339 }*/
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200340 }
341
Jonathan Hart1912afc2013-10-11 12:02:44 +1300342 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200343 if (log.isTraceEnabled()) {
344 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
345 inetAddressToString(arp.getSenderProtocolAddress()),
346 HexString.toHexString(arp.getSenderHardwareAddress()),
347 HexString.toHexString(sw.getId()), pi.getInPort()});
348 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200349
Jonathan Hartabad6a52013-09-30 18:17:21 +1300350 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200351 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300352 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200353 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200354 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200355 return;
356 }
357
Jonathan Hartabad6a52013-09-30 18:17:21 +1300358 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
359
360 arpCache.update(senderIpAddress, senderMacAddress);
361
362 //See if anyone's waiting for this ARP reply
363 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200364
365 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200366 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200367 synchronized (arpRequests) {
368 Iterator<ArpRequest> it = requests.iterator();
369 while (it.hasNext()) {
370 ArpRequest request = it.next();
371 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200372 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200373 }
374 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200375
376 //Don't hold an ARP lock while dispatching requests
377 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300378 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200379 }
380 }
381
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200382 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200383 //TODO what should the sender IP address and MAC address be if no
384 //IP addresses are configured? Will there ever be a need to send
385 //ARP requests from the controller in that case?
386 //All-zero MAC address doesn't seem to work - hosts don't respond to it
387
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200388 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
389 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200390 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200391 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200392 (byte)0xff, (byte)0xff, (byte)0xff};
393
394 ARP arpRequest = new ARP();
395
396 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
397 .setProtocolType(ARP.PROTO_TYPE_IP)
398 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200399 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200400 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200401 .setTargetHardwareAddress(zeroMac)
402 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200403
Jonathan Harta8887642013-10-28 13:46:54 -0700404 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200405 //TODO hack for now as it's unclear what the MAC address should be
406 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200407 if (routerMacAddress != null) {
408 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200409 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200410 arpRequest.setSenderHardwareAddress(senderMacAddress);
411
412 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700413 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200414 if (intf != null) {
415 senderIPAddress = intf.getIpAddress().getAddress();
416 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200417
418 arpRequest.setSenderProtocolAddress(senderIPAddress);
419
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200420 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200421 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200422 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200423 .setEtherType(Ethernet.TYPE_ARP)
424 .setPayload(arpRequest);
425
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700426 if (vlan != NO_VLAN) {
427 eth.setVlanID(vlan)
428 .setPriorityCode((byte)0);
429 }
430
Jonathan Hart2f790d22013-08-15 14:01:24 +1200431 sendArpRequestToSwitches(ipAddress, eth.serialize());
432 }
433
434 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200435 sendArpRequestToSwitches(dstAddress, arpRequest,
436 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200437 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200438
Jonathan Hart2f790d22013-08-15 14:01:24 +1200439 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
440 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200441
Jonathan Harta8887642013-10-28 13:46:54 -0700442 if (configService.hasLayer3Configuration()) {
443 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200444 if (intf != null) {
445 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
446 }
447 else {
448 //TODO here it should be broadcast out all non-interface edge ports.
449 //I think we can assume that if it's not a request for an external
450 //network, it's an ARP for a host in our own network. So we want to
451 //send it out all edge ports that don't have an interface configured
452 //to ensure it reaches all hosts in our network.
453 log.debug("No interface found to send ARP request for {}",
454 dstAddress.getHostAddress());
455 }
456 }
457 else {
458 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
459 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200460 }
461
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200462 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200463 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
464 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
465 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
466
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200467 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700468 //I think this means the switch doesn't have any links.
469 //continue;
470 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200471 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200472
Jonathan Harta18e4792013-10-31 10:10:54 -0700473
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200474 OFPacketOut po = new OFPacketOut();
475 po.setInPort(OFPort.OFPP_NONE)
476 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200477 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200478
479 List<OFAction> actions = new ArrayList<OFAction>();
480
481 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200482 if (linkPorts.contains(portNum) ||
483 (sw.getId() == inSwitch && portNum == inPort)){
484 //If this port isn't an edge port or is the ingress port
485 //for the ARP, don't broadcast out it
486 continue;
487 }
488
489 actions.add(new OFActionOutput(portNum));
490 }
491
492 po.setActions(actions);
493 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
494 po.setActionsLength(actionsLength);
495 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200496 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200497
498 List<OFMessage> msgList = new ArrayList<OFMessage>();
499 msgList.add(po);
500
501 try {
502 sw.write(msgList, null);
503 sw.flush();
504 } catch (IOException e) {
505 log.error("Failure writing packet out to switch", e);
506 }
507 }
508 }
509
Jonathan Hart2f790d22013-08-15 14:01:24 +1200510 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200511 if (log.isTraceEnabled()) {
512 log.trace("Sending ARP request out {}/{}",
513 HexString.toHexString(dpid), port);
514 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200515
516 OFPacketOut po = new OFPacketOut();
517 po.setInPort(OFPort.OFPP_NONE)
518 .setBufferId(-1)
519 .setPacketData(arpRequest);
520
521 List<OFAction> actions = new ArrayList<OFAction>();
522 actions.add(new OFActionOutput(port));
523 po.setActions(actions);
524 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
525 po.setActionsLength(actionsLength);
526 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
527 + arpRequest.length);
528
529 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
530
531 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200532 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200533 return;
534 }
535
536 try {
537 sw.write(po, null);
538 sw.flush();
539 } catch (IOException e) {
540 log.error("Failure writing packet out to switch", e);
541 }
542 }
543
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300544 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200545 if (log.isTraceEnabled()) {
546 log.trace("Sending reply {} => {} to {}", new Object[] {
547 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300548 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200549 inetAddressToString(arpRequest.getSenderProtocolAddress())});
550 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200551
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200552 ARP arpReply = new ARP();
553 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
554 .setProtocolType(ARP.PROTO_TYPE_IP)
555 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200556 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200557 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300558 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200559 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
560 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
561 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
562
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700563
564
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200565 Ethernet eth = new Ethernet();
566 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300567 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200568 .setEtherType(Ethernet.TYPE_ARP)
569 .setPayload(arpReply);
570
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700571 if (vlan != NO_VLAN) {
572 eth.setVlanID(vlan)
573 .setPriorityCode((byte)0);
574 }
575
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200576 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200577 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200578
579 OFPacketOut po = new OFPacketOut();
580 po.setInPort(OFPort.OFPP_NONE)
581 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200582 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200583 .setActions(actions)
584 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
585 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
586 + po.getPacketData().length);
587
588 List<OFMessage> msgList = new ArrayList<OFMessage>();
589 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200590
591 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
592
593 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200594 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200595 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200596 return;
597 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200598
599 try {
600 sw.write(msgList, null);
601 sw.flush();
602 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200603 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200604 }
605 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300606
607 private String inetAddressToString(byte[] bytes) {
608 try {
609 return InetAddress.getByAddress(bytes).getHostAddress();
610 } catch (UnknownHostException e) {
611 log.debug("Invalid IP address", e);
612 return "";
613 }
614 }
615
616 /*
617 * IProxyArpService methods
618 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200619
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200620 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300621 public MACAddress getMacAddress(InetAddress ipAddress) {
622 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200623 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200624
625 @Override
626 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
627 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200628 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200629
Jonathan Hart6e618212013-08-21 22:28:43 +1200630 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700631 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200632 sendArpRequestForAddress(ipAddress);
633 }
634 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300635
636 @Override
637 public List<String> getMappings() {
638 return arpCache.getMappings();
639 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200640}