blob: a745aafc2676b0be5fe7332464c0981c2dc1be23 [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.IDevice;
21import net.floodlightcontroller.devicemanager.IDeviceService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120022import net.floodlightcontroller.packet.ARP;
23import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart08ee8522013-09-22 17:34:43 +120024import net.floodlightcontroller.packet.IPv4;
Jonathan Hart5afde492013-10-01 12:30:53 +130025import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120026import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120027import net.floodlightcontroller.util.MACAddress;
Jonathan Hart2f790d22013-08-15 14:01:24 +120028import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartebba1e12013-10-29 11:37:02 -070029import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120030
31import org.openflow.protocol.OFMessage;
32import org.openflow.protocol.OFPacketIn;
33import org.openflow.protocol.OFPacketOut;
34import org.openflow.protocol.OFPort;
35import org.openflow.protocol.OFType;
36import org.openflow.protocol.action.OFAction;
37import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120038import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
Jonathan Hart4dfc3652013-08-02 20:22:36 +120042import com.google.common.collect.HashMultimap;
43import com.google.common.collect.Multimaps;
44import com.google.common.collect.SetMultimap;
Jonathan Harta18e4792013-10-31 10:10:54 -070045import com.google.common.net.InetAddresses;
Jonathan Hart4dfc3652013-08-02 20:22:36 +120046
Jonathan Hart6261dcd2013-07-22 17:58:35 +120047public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120048 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120049
Jonathan Hartdf6ec332013-08-04 01:37:14 +120050 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130051
52 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120053
Jonathan Harta8887642013-10-28 13:46:54 -070054 private IFloodlightProviderService floodlightProvider;
55 private ITopologyService topology;
Jonathan Harta18e4792013-10-31 10:10:54 -070056 private IDeviceService deviceService;
Jonathan Harta8887642013-10-28 13:46:54 -070057 private IConfigInfoService configService;
58 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120059
Jonathan Hart1cf9de02013-10-21 17:42:29 -070060 private short vlan;
61 private static final short NO_VLAN = 0;
62
Jonathan Harta8887642013-10-28 13:46:54 -070063 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120064
Jonathan Harta8887642013-10-28 13:46:54 -070065 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120066
Jonathan Hartabad6a52013-09-30 18:17:21 +130067 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120068 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130069 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120070 private long requestTime;
71
Jonathan Hart4dfc3652013-08-02 20:22:36 +120072 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120073 this.requester = requester;
74 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120075 this.requestTime = System.currentTimeMillis();
76 }
77
Jonathan Hart4dfc3652013-08-02 20:22:36 +120078 public ArpRequest(ArpRequest old) {
79 this.requester = old.requester;
80 this.retry = old.retry;
81 this.requestTime = System.currentTimeMillis();
82 }
83
Jonathan Hart4dfc3652013-08-02 20:22:36 +120084 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +130085 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120086 }
87
Jonathan Hart4dfc3652013-08-02 20:22:36 +120088 public boolean shouldRetry() {
89 return retry;
90 }
91
Jonathan Hartabad6a52013-09-30 18:17:21 +130092 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120093 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120094 }
95 }
96
Jonathan Hartda4d0e12013-09-30 21:00:20 +130097 private class HostArpRequester implements IArpRequester {
98 private final ARP arpRequest;
99 private final long dpid;
100 private final short port;
101
102 public HostArpRequester(ARP arpRequest, long dpid, short port) {
103 this.arpRequest = arpRequest;
104 this.dpid = dpid;
105 this.port = port;
106 }
107
108 @Override
109 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
110 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
111 }
112 }
113
Jonathan Harta8887642013-10-28 13:46:54 -0700114 /*
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200115 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Harta8887642013-10-28 13:46:54 -0700116 ITopologyService topology, IConfigInfoService configService,
Jonathan Hart5afde492013-10-01 12:30:53 +1300117 IRestApiService restApi){
Jonathan Harta8887642013-10-28 13:46:54 -0700118
119 }
120 */
121
122 public void init(IFloodlightProviderService floodlightProvider,
Jonathan Harta18e4792013-10-31 10:10:54 -0700123 ITopologyService topology, IDeviceService deviceService,
124 IConfigInfoService config, IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200125 this.floodlightProvider = floodlightProvider;
126 this.topology = topology;
Jonathan Harta18e4792013-10-31 10:10:54 -0700127 this.deviceService = deviceService;
Jonathan Harta8887642013-10-28 13:46:54 -0700128 this.configService = config;
Jonathan Hart5afde492013-10-01 12:30:53 +1300129 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200130
Jonathan Hartabad6a52013-09-30 18:17:21 +1300131 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200132
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200133 arpRequests = Multimaps.synchronizedSetMultimap(
134 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200135 }
136
Jonathan Harta8887642013-10-28 13:46:54 -0700137 public void startUp() {
138 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700139 log.info("vlan set to {}", this.vlan);
140
Jonathan Hart5afde492013-10-01 12:30:53 +1300141 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700142 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300143
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200144 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200145 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200146 @Override
147 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200148 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200149 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200150 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200151 }
152
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200153 /*
154 * Function that runs periodically to manage the asynchronous request mechanism.
155 * It basically cleans up old ARP requests if we don't get a response for them.
156 * The caller can designate that a request should be retried indefinitely, and
157 * this task will handle that as well.
158 */
159 private void doPeriodicArpProcessing() {
160 SetMultimap<InetAddress, ArpRequest> retryList
161 = HashMultimap.<InetAddress, ArpRequest>create();
162
163 //Have to synchronize externally on the Multimap while using an iterator,
164 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200165 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200166 log.debug("Current have {} outstanding requests",
167 arpRequests.size());
168
169 Iterator<Map.Entry<InetAddress, ArpRequest>> it
170 = arpRequests.entries().iterator();
171
172 while (it.hasNext()) {
173 Map.Entry<InetAddress, ArpRequest> entry
174 = it.next();
175 ArpRequest request = entry.getValue();
176 if (request.isExpired()) {
177 log.debug("Cleaning expired ARP request for {}",
178 entry.getKey().getHostAddress());
179
180 it.remove();
181
182 if (request.shouldRetry()) {
183 retryList.put(entry.getKey(), request);
184 }
185 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200186 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200187 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200188
189 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
190 : retryList.asMap().entrySet()) {
191
192 InetAddress address = entry.getKey();
193
194 log.debug("Resending ARP request for {}", address.getHostAddress());
195
196 sendArpRequestForAddress(address);
197
198 for (ArpRequest request : entry.getValue()) {
199 arpRequests.put(address, new ArpRequest(request));
200 }
201 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200202 }
203
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200204 @Override
205 public String getName() {
Jonathan Harta18e4792013-10-31 10:10:54 -0700206 return "proxyarpmanager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200207 }
208
209 @Override
210 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700211 if (type == OFType.PACKET_IN) {
212 return "devicemanager".equals(name);
213 }
214 else {
215 return false;
216 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200217 }
218
219 @Override
220 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200221 return false;
222 }
223
224 @Override
225 public Command receive(
226 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
227
228 if (msg.getType() != OFType.PACKET_IN){
229 return Command.CONTINUE;
230 }
231
232 OFPacketIn pi = (OFPacketIn) msg;
233
234 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
235 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
236
237 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200238 ARP arp = (ARP) eth.getPayload();
239
240 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700241 //TODO check what the DeviceManager does about propagating
242 //or swallowing ARPs. We want to go after DeviceManager in the
243 //chain but we really need it to CONTINUE ARP packets so we can
244 //get them.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200245 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200246 }
247 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Harta18e4792013-10-31 10:10:54 -0700248 //handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200249 }
250 }
251
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200252 //TODO should we propagate ARP or swallow it?
253 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200254 return Command.CONTINUE;
255 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200256
Jonathan Hart1912afc2013-10-11 12:02:44 +1300257 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200258 if (log.isTraceEnabled()) {
259 log.trace("ARP request received for {}",
260 inetAddressToString(arp.getTargetProtocolAddress()));
261 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200262
263 InetAddress target;
264 try {
265 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
266 } catch (UnknownHostException e) {
267 log.debug("Invalid address in ARP request", e);
268 return;
269 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200270
Jonathan Harta8887642013-10-28 13:46:54 -0700271 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200272 //If the request came from outside our network, we only care if
273 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700274 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200275 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700276 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200277
278 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700279 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200280 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200281
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200282 return;
283 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200284
Jonathan Harta18e4792013-10-31 10:10:54 -0700285 //MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200286
Jonathan Harta18e4792013-10-31 10:10:54 -0700287 //IDevice dstDevice = deviceService.fcStore.get(cntx, IDeviceService.CONTEXT_DST_DEVICE);
288 Iterator<? extends IDevice> it = deviceService.queryDevices(
289 null, null, InetAddresses.coerceToInteger(target), null, null);
290
291 IDevice targetDevice = null;
292 if (it.hasNext()) {
293 targetDevice = it.next();
294 }
295
296 if (targetDevice != null) {
297 //We have the device in our database, so send a reply
298 MACAddress macAddress = MACAddress.valueOf(targetDevice.getMACAddress());
299
300 if (log.isTraceEnabled()) {
301 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
302 inetAddressToString(arp.getTargetProtocolAddress()),
303 macAddress.toString(),
304 HexString.toHexString(sw.getId()), pi.getInPort()});
305 }
306
307 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
308 }
309
310 /*if (macAddress == null){
Jonathan Hartabad6a52013-09-30 18:17:21 +1300311 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200312
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200313 //Record where the request came from so we know where to send the reply
Jonathan Harta18e4792013-10-31 10:10:54 -0700314 //arpRequests.put(target, new ArpRequest(
315 //new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200316
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200317 //Flood the request out edge ports
Jonathan Harta18e4792013-10-31 10:10:54 -0700318 //sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200319 }
320 else {
321 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200322 if (log.isTraceEnabled()) {
323 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
324 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300325 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200326 HexString.toHexString(sw.getId()), pi.getInPort()});
327 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200328
Jonathan Hartabad6a52013-09-30 18:17:21 +1300329 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Harta18e4792013-10-31 10:10:54 -0700330 }*/
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200331 }
332
Jonathan Hart1912afc2013-10-11 12:02:44 +1300333 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200334 if (log.isTraceEnabled()) {
335 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
336 inetAddressToString(arp.getSenderProtocolAddress()),
337 HexString.toHexString(arp.getSenderHardwareAddress()),
338 HexString.toHexString(sw.getId()), pi.getInPort()});
339 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200340
Jonathan Hartabad6a52013-09-30 18:17:21 +1300341 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200342 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300343 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200344 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200345 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200346 return;
347 }
348
Jonathan Hartabad6a52013-09-30 18:17:21 +1300349 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
350
351 arpCache.update(senderIpAddress, senderMacAddress);
352
353 //See if anyone's waiting for this ARP reply
354 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200355
356 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200357 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200358 synchronized (arpRequests) {
359 Iterator<ArpRequest> it = requests.iterator();
360 while (it.hasNext()) {
361 ArpRequest request = it.next();
362 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200363 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200364 }
365 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200366
367 //Don't hold an ARP lock while dispatching requests
368 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300369 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200370 }
371 }
372
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200373 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200374 //TODO what should the sender IP address and MAC address be if no
375 //IP addresses are configured? Will there ever be a need to send
376 //ARP requests from the controller in that case?
377 //All-zero MAC address doesn't seem to work - hosts don't respond to it
378
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200379 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
380 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200381 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200382 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200383 (byte)0xff, (byte)0xff, (byte)0xff};
384
385 ARP arpRequest = new ARP();
386
387 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
388 .setProtocolType(ARP.PROTO_TYPE_IP)
389 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200390 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200391 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200392 .setTargetHardwareAddress(zeroMac)
393 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200394
Jonathan Harta8887642013-10-28 13:46:54 -0700395 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200396 //TODO hack for now as it's unclear what the MAC address should be
397 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200398 if (routerMacAddress != null) {
399 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200400 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200401 arpRequest.setSenderHardwareAddress(senderMacAddress);
402
403 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700404 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200405 if (intf != null) {
406 senderIPAddress = intf.getIpAddress().getAddress();
407 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200408
409 arpRequest.setSenderProtocolAddress(senderIPAddress);
410
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200411 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200412 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200413 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200414 .setEtherType(Ethernet.TYPE_ARP)
415 .setPayload(arpRequest);
416
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700417 if (vlan != NO_VLAN) {
418 eth.setVlanID(vlan)
419 .setPriorityCode((byte)0);
420 }
421
Jonathan Hart2f790d22013-08-15 14:01:24 +1200422 sendArpRequestToSwitches(ipAddress, eth.serialize());
423 }
424
425 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200426 sendArpRequestToSwitches(dstAddress, arpRequest,
427 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200428 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200429
Jonathan Hart2f790d22013-08-15 14:01:24 +1200430 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
431 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200432
Jonathan Harta8887642013-10-28 13:46:54 -0700433 if (configService.hasLayer3Configuration()) {
434 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200435 if (intf != null) {
436 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
437 }
438 else {
439 //TODO here it should be broadcast out all non-interface edge ports.
440 //I think we can assume that if it's not a request for an external
441 //network, it's an ARP for a host in our own network. So we want to
442 //send it out all edge ports that don't have an interface configured
443 //to ensure it reaches all hosts in our network.
444 log.debug("No interface found to send ARP request for {}",
445 dstAddress.getHostAddress());
446 }
447 }
448 else {
449 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
450 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200451 }
452
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200453 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200454 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
455 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
456 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
457
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200458 if (linkPorts == null){
Jonathan Harta18e4792013-10-31 10:10:54 -0700459 //I think this means the switch doesn't have any links.
460 //continue;
461 linkPorts = new HashSet<Short>();
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200462 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200463
Jonathan Harta18e4792013-10-31 10:10:54 -0700464
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200465 OFPacketOut po = new OFPacketOut();
466 po.setInPort(OFPort.OFPP_NONE)
467 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200468 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200469
470 List<OFAction> actions = new ArrayList<OFAction>();
471
472 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200473 if (linkPorts.contains(portNum) ||
474 (sw.getId() == inSwitch && portNum == inPort)){
475 //If this port isn't an edge port or is the ingress port
476 //for the ARP, don't broadcast out it
477 continue;
478 }
479
480 actions.add(new OFActionOutput(portNum));
481 }
482
483 po.setActions(actions);
484 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
485 po.setActionsLength(actionsLength);
486 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200487 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200488
489 List<OFMessage> msgList = new ArrayList<OFMessage>();
490 msgList.add(po);
491
492 try {
493 sw.write(msgList, null);
494 sw.flush();
495 } catch (IOException e) {
496 log.error("Failure writing packet out to switch", e);
497 }
498 }
499 }
500
Jonathan Hart2f790d22013-08-15 14:01:24 +1200501 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200502 if (log.isTraceEnabled()) {
503 log.trace("Sending ARP request out {}/{}",
504 HexString.toHexString(dpid), port);
505 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200506
507 OFPacketOut po = new OFPacketOut();
508 po.setInPort(OFPort.OFPP_NONE)
509 .setBufferId(-1)
510 .setPacketData(arpRequest);
511
512 List<OFAction> actions = new ArrayList<OFAction>();
513 actions.add(new OFActionOutput(port));
514 po.setActions(actions);
515 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
516 po.setActionsLength(actionsLength);
517 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
518 + arpRequest.length);
519
520 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
521
522 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200523 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200524 return;
525 }
526
527 try {
528 sw.write(po, null);
529 sw.flush();
530 } catch (IOException e) {
531 log.error("Failure writing packet out to switch", e);
532 }
533 }
534
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300535 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200536 if (log.isTraceEnabled()) {
537 log.trace("Sending reply {} => {} to {}", new Object[] {
538 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300539 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200540 inetAddressToString(arpRequest.getSenderProtocolAddress())});
541 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200542
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200543 ARP arpReply = new ARP();
544 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
545 .setProtocolType(ARP.PROTO_TYPE_IP)
546 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200547 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200548 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300549 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200550 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
551 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
552 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
553
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700554
555
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200556 Ethernet eth = new Ethernet();
557 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300558 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200559 .setEtherType(Ethernet.TYPE_ARP)
560 .setPayload(arpReply);
561
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700562 if (vlan != NO_VLAN) {
563 eth.setVlanID(vlan)
564 .setPriorityCode((byte)0);
565 }
566
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200567 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200568 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200569
570 OFPacketOut po = new OFPacketOut();
571 po.setInPort(OFPort.OFPP_NONE)
572 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200573 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200574 .setActions(actions)
575 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
576 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
577 + po.getPacketData().length);
578
579 List<OFMessage> msgList = new ArrayList<OFMessage>();
580 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200581
582 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
583
584 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200585 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200586 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200587 return;
588 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200589
590 try {
591 sw.write(msgList, null);
592 sw.flush();
593 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200594 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200595 }
596 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300597
598 private String inetAddressToString(byte[] bytes) {
599 try {
600 return InetAddress.getByAddress(bytes).getHostAddress();
601 } catch (UnknownHostException e) {
602 log.debug("Invalid IP address", e);
603 return "";
604 }
605 }
606
607 /*
608 * IProxyArpService methods
609 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200610
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200611 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300612 public MACAddress getMacAddress(InetAddress ipAddress) {
613 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200614 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200615
616 @Override
617 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
618 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200619 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200620
Jonathan Hart6e618212013-08-21 22:28:43 +1200621 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700622 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200623 sendArpRequestForAddress(ipAddress);
624 }
625 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300626
627 @Override
628 public List<String> getMappings() {
629 return arpCache.getMappings();
630 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200631}