blob: c0ea064a0b5f7e2e5b40c01a2b60b1721af739ef [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 Hart6261dcd2013-07-22 17:58:35 +12008import java.util.Iterator;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +12009import java.util.List;
10import java.util.Map;
11import java.util.Set;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120012import java.util.Timer;
13import java.util.TimerTask;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120014
15import net.floodlightcontroller.core.FloodlightContext;
16import net.floodlightcontroller.core.IFloodlightProviderService;
17import net.floodlightcontroller.core.IOFMessageListener;
18import net.floodlightcontroller.core.IOFSwitch;
19import net.floodlightcontroller.packet.ARP;
20import net.floodlightcontroller.packet.Ethernet;
Jonathan Hart08ee8522013-09-22 17:34:43 +120021import net.floodlightcontroller.packet.IPv4;
Jonathan Hart5afde492013-10-01 12:30:53 +130022import net.floodlightcontroller.restserver.IRestApiService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120023import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120024import net.floodlightcontroller.util.MACAddress;
Jonathan Hart2f790d22013-08-15 14:01:24 +120025import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartebba1e12013-10-29 11:37:02 -070026import net.onrc.onos.ofcontroller.core.config.IConfigInfoService;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120027
28import org.openflow.protocol.OFMessage;
29import org.openflow.protocol.OFPacketIn;
30import org.openflow.protocol.OFPacketOut;
31import org.openflow.protocol.OFPort;
32import org.openflow.protocol.OFType;
33import org.openflow.protocol.action.OFAction;
34import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120035import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
Jonathan Hart4dfc3652013-08-02 20:22:36 +120039import com.google.common.collect.HashMultimap;
40import com.google.common.collect.Multimaps;
41import com.google.common.collect.SetMultimap;
42
Jonathan Hart6261dcd2013-07-22 17:58:35 +120043public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120044 private final static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120045
Jonathan Hartdf6ec332013-08-04 01:37:14 +120046 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hartda4d0e12013-09-30 21:00:20 +130047
48 private static final int ARP_REQUEST_TIMEOUT = 2000; //ms
Jonathan Hart6261dcd2013-07-22 17:58:35 +120049
Jonathan Harta8887642013-10-28 13:46:54 -070050 private IFloodlightProviderService floodlightProvider;
51 private ITopologyService topology;
52 private IConfigInfoService configService;
53 private IRestApiService restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120054
Jonathan Hart1cf9de02013-10-21 17:42:29 -070055 private short vlan;
56 private static final short NO_VLAN = 0;
57
Jonathan Harta8887642013-10-28 13:46:54 -070058 private ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120059
Jonathan Harta8887642013-10-28 13:46:54 -070060 private SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120061
Jonathan Hartabad6a52013-09-30 18:17:21 +130062 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120063 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130064 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120065 private long requestTime;
66
Jonathan Hart4dfc3652013-08-02 20:22:36 +120067 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120068 this.requester = requester;
69 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120070 this.requestTime = System.currentTimeMillis();
71 }
72
Jonathan Hart4dfc3652013-08-02 20:22:36 +120073 public ArpRequest(ArpRequest old) {
74 this.requester = old.requester;
75 this.retry = old.retry;
76 this.requestTime = System.currentTimeMillis();
77 }
78
Jonathan Hart4dfc3652013-08-02 20:22:36 +120079 public boolean isExpired() {
Jonathan Hartda4d0e12013-09-30 21:00:20 +130080 return (System.currentTimeMillis() - requestTime) > ARP_REQUEST_TIMEOUT;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120081 }
82
Jonathan Hart4dfc3652013-08-02 20:22:36 +120083 public boolean shouldRetry() {
84 return retry;
85 }
86
Jonathan Hartabad6a52013-09-30 18:17:21 +130087 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120088 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120089 }
90 }
91
Jonathan Hartda4d0e12013-09-30 21:00:20 +130092 private class HostArpRequester implements IArpRequester {
93 private final ARP arpRequest;
94 private final long dpid;
95 private final short port;
96
97 public HostArpRequester(ARP arpRequest, long dpid, short port) {
98 this.arpRequest = arpRequest;
99 this.dpid = dpid;
100 this.port = port;
101 }
102
103 @Override
104 public void arpResponse(InetAddress ipAddress, MACAddress macAddress) {
105 ProxyArpManager.this.sendArpReply(arpRequest, dpid, port, macAddress);
106 }
107 }
108
Jonathan Harta8887642013-10-28 13:46:54 -0700109 /*
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200110 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Harta8887642013-10-28 13:46:54 -0700111 ITopologyService topology, IConfigInfoService configService,
Jonathan Hart5afde492013-10-01 12:30:53 +1300112 IRestApiService restApi){
Jonathan Harta8887642013-10-28 13:46:54 -0700113
114 }
115 */
116
117 public void init(IFloodlightProviderService floodlightProvider,
118 ITopologyService topology, IConfigInfoService config,
119 IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200120 this.floodlightProvider = floodlightProvider;
121 this.topology = topology;
Jonathan Harta8887642013-10-28 13:46:54 -0700122 this.configService = config;
Jonathan Hart5afde492013-10-01 12:30:53 +1300123 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200124
Jonathan Hartabad6a52013-09-30 18:17:21 +1300125 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200126
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200127 arpRequests = Multimaps.synchronizedSetMultimap(
128 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200129 }
130
Jonathan Harta8887642013-10-28 13:46:54 -0700131 public void startUp() {
132 this.vlan = configService.getVlan();
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700133 log.info("vlan set to {}", this.vlan);
134
Jonathan Hart5afde492013-10-01 12:30:53 +1300135 restApi.addRestletRoutable(new ArpWebRoutable());
Jonathan Harta8887642013-10-28 13:46:54 -0700136 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
Jonathan Hart5afde492013-10-01 12:30:53 +1300137
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200138 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200139 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200140 @Override
141 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200142 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200143 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200144 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200145 }
146
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200147 /*
148 * Function that runs periodically to manage the asynchronous request mechanism.
149 * It basically cleans up old ARP requests if we don't get a response for them.
150 * The caller can designate that a request should be retried indefinitely, and
151 * this task will handle that as well.
152 */
153 private void doPeriodicArpProcessing() {
154 SetMultimap<InetAddress, ArpRequest> retryList
155 = HashMultimap.<InetAddress, ArpRequest>create();
156
157 //Have to synchronize externally on the Multimap while using an iterator,
158 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200159 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200160 log.debug("Current have {} outstanding requests",
161 arpRequests.size());
162
163 Iterator<Map.Entry<InetAddress, ArpRequest>> it
164 = arpRequests.entries().iterator();
165
166 while (it.hasNext()) {
167 Map.Entry<InetAddress, ArpRequest> entry
168 = it.next();
169 ArpRequest request = entry.getValue();
170 if (request.isExpired()) {
171 log.debug("Cleaning expired ARP request for {}",
172 entry.getKey().getHostAddress());
173
174 it.remove();
175
176 if (request.shouldRetry()) {
177 retryList.put(entry.getKey(), request);
178 }
179 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200180 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200181 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200182
183 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
184 : retryList.asMap().entrySet()) {
185
186 InetAddress address = entry.getKey();
187
188 log.debug("Resending ARP request for {}", address.getHostAddress());
189
190 sendArpRequestForAddress(address);
191
192 for (ArpRequest request : entry.getValue()) {
193 arpRequests.put(address, new ArpRequest(request));
194 }
195 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200196 }
197
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200198 @Override
199 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200200 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200201 }
202
203 @Override
204 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200205 return false;
206 }
207
208 @Override
209 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200210 return false;
211 }
212
213 @Override
214 public Command receive(
215 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
216
217 if (msg.getType() != OFType.PACKET_IN){
218 return Command.CONTINUE;
219 }
220
221 OFPacketIn pi = (OFPacketIn) msg;
222
223 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
224 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
225
226 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200227 ARP arp = (ARP) eth.getPayload();
228
229 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200230 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200231 }
232 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200233 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200234 }
235 }
236
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200237 //TODO should we propagate ARP or swallow it?
238 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200239 return Command.CONTINUE;
240 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200241
Jonathan Hart1912afc2013-10-11 12:02:44 +1300242 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200243 if (log.isTraceEnabled()) {
244 log.trace("ARP request received for {}",
245 inetAddressToString(arp.getTargetProtocolAddress()));
246 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200247
248 InetAddress target;
249 try {
250 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
251 } catch (UnknownHostException e) {
252 log.debug("Invalid address in ARP request", e);
253 return;
254 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200255
Jonathan Harta8887642013-10-28 13:46:54 -0700256 if (configService.fromExternalNetwork(sw.getId(), pi.getInPort())) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200257 //If the request came from outside our network, we only care if
258 //it was a request for one of our interfaces.
Jonathan Harta8887642013-10-28 13:46:54 -0700259 if (configService.isInterfaceAddress(target)) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200260 log.trace("ARP request for our interface. Sending reply {} => {}",
Jonathan Harta8887642013-10-28 13:46:54 -0700261 target.getHostAddress(), configService.getRouterMacAddress());
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200262
263 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Harta8887642013-10-28 13:46:54 -0700264 configService.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200265 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200266
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200267 return;
268 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200269
Jonathan Hartabad6a52013-09-30 18:17:21 +1300270 MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200271
Jonathan Hartabad6a52013-09-30 18:17:21 +1300272 if (macAddress == null){
273 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200274
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200275 //Record where the request came from so we know where to send the reply
Jonathan Hart9ea31212013-08-12 21:40:34 +1200276 arpRequests.put(target, new ArpRequest(
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300277 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200278
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200279 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200280 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200281 }
282 else {
283 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200284 if (log.isTraceEnabled()) {
285 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
286 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300287 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200288 HexString.toHexString(sw.getId()), pi.getInPort()});
289 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200290
Jonathan Hartabad6a52013-09-30 18:17:21 +1300291 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200292 }
293 }
294
Jonathan Hart1912afc2013-10-11 12:02:44 +1300295 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200296 if (log.isTraceEnabled()) {
297 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
298 inetAddressToString(arp.getSenderProtocolAddress()),
299 HexString.toHexString(arp.getSenderHardwareAddress()),
300 HexString.toHexString(sw.getId()), pi.getInPort()});
301 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200302
Jonathan Hartabad6a52013-09-30 18:17:21 +1300303 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200304 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300305 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200306 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200307 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200308 return;
309 }
310
Jonathan Hartabad6a52013-09-30 18:17:21 +1300311 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
312
313 arpCache.update(senderIpAddress, senderMacAddress);
314
315 //See if anyone's waiting for this ARP reply
316 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200317
318 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200319 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200320 synchronized (arpRequests) {
321 Iterator<ArpRequest> it = requests.iterator();
322 while (it.hasNext()) {
323 ArpRequest request = it.next();
324 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200325 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200326 }
327 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200328
329 //Don't hold an ARP lock while dispatching requests
330 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300331 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200332 }
333 }
334
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200335 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200336 //TODO what should the sender IP address and MAC address be if no
337 //IP addresses are configured? Will there ever be a need to send
338 //ARP requests from the controller in that case?
339 //All-zero MAC address doesn't seem to work - hosts don't respond to it
340
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200341 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
342 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200343 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200344 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200345 (byte)0xff, (byte)0xff, (byte)0xff};
346
347 ARP arpRequest = new ARP();
348
349 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
350 .setProtocolType(ARP.PROTO_TYPE_IP)
351 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200352 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200353 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200354 .setTargetHardwareAddress(zeroMac)
355 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200356
Jonathan Harta8887642013-10-28 13:46:54 -0700357 MACAddress routerMacAddress = configService.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200358 //TODO hack for now as it's unclear what the MAC address should be
359 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200360 if (routerMacAddress != null) {
361 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200362 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200363 arpRequest.setSenderHardwareAddress(senderMacAddress);
364
365 byte[] senderIPAddress = zeroIpv4;
Jonathan Harta8887642013-10-28 13:46:54 -0700366 Interface intf = configService.getOutgoingInterface(ipAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200367 if (intf != null) {
368 senderIPAddress = intf.getIpAddress().getAddress();
369 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200370
371 arpRequest.setSenderProtocolAddress(senderIPAddress);
372
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200373 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200374 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200375 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200376 .setEtherType(Ethernet.TYPE_ARP)
377 .setPayload(arpRequest);
378
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700379 if (vlan != NO_VLAN) {
380 eth.setVlanID(vlan)
381 .setPriorityCode((byte)0);
382 }
383
Jonathan Hart2f790d22013-08-15 14:01:24 +1200384 sendArpRequestToSwitches(ipAddress, eth.serialize());
385 }
386
387 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200388 sendArpRequestToSwitches(dstAddress, arpRequest,
389 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200390 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200391
Jonathan Hart2f790d22013-08-15 14:01:24 +1200392 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
393 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200394
Jonathan Harta8887642013-10-28 13:46:54 -0700395 if (configService.hasLayer3Configuration()) {
396 Interface intf = configService.getOutgoingInterface(dstAddress);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200397 if (intf != null) {
398 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
399 }
400 else {
401 //TODO here it should be broadcast out all non-interface edge ports.
402 //I think we can assume that if it's not a request for an external
403 //network, it's an ARP for a host in our own network. So we want to
404 //send it out all edge ports that don't have an interface configured
405 //to ensure it reaches all hosts in our network.
406 log.debug("No interface found to send ARP request for {}",
407 dstAddress.getHostAddress());
408 }
409 }
410 else {
411 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
412 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200413 }
414
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200415 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200416 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
417 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
418 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
419
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200420 if (linkPorts == null){
421 //I think this means the switch isn't known to topology yet.
422 //Maybe it only just joined.
423 continue;
424 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200425
426 OFPacketOut po = new OFPacketOut();
427 po.setInPort(OFPort.OFPP_NONE)
428 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200429 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200430
431 List<OFAction> actions = new ArrayList<OFAction>();
432
433 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200434 if (linkPorts.contains(portNum) ||
435 (sw.getId() == inSwitch && portNum == inPort)){
436 //If this port isn't an edge port or is the ingress port
437 //for the ARP, don't broadcast out it
438 continue;
439 }
440
441 actions.add(new OFActionOutput(portNum));
442 }
443
444 po.setActions(actions);
445 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
446 po.setActionsLength(actionsLength);
447 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200448 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200449
450 List<OFMessage> msgList = new ArrayList<OFMessage>();
451 msgList.add(po);
452
453 try {
454 sw.write(msgList, null);
455 sw.flush();
456 } catch (IOException e) {
457 log.error("Failure writing packet out to switch", e);
458 }
459 }
460 }
461
Jonathan Hart2f790d22013-08-15 14:01:24 +1200462 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200463 if (log.isTraceEnabled()) {
464 log.trace("Sending ARP request out {}/{}",
465 HexString.toHexString(dpid), port);
466 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200467
468 OFPacketOut po = new OFPacketOut();
469 po.setInPort(OFPort.OFPP_NONE)
470 .setBufferId(-1)
471 .setPacketData(arpRequest);
472
473 List<OFAction> actions = new ArrayList<OFAction>();
474 actions.add(new OFActionOutput(port));
475 po.setActions(actions);
476 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
477 po.setActionsLength(actionsLength);
478 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
479 + arpRequest.length);
480
481 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
482
483 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200484 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200485 return;
486 }
487
488 try {
489 sw.write(po, null);
490 sw.flush();
491 } catch (IOException e) {
492 log.error("Failure writing packet out to switch", e);
493 }
494 }
495
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300496 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200497 if (log.isTraceEnabled()) {
498 log.trace("Sending reply {} => {} to {}", new Object[] {
499 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300500 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200501 inetAddressToString(arpRequest.getSenderProtocolAddress())});
502 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200503
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200504 ARP arpReply = new ARP();
505 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
506 .setProtocolType(ARP.PROTO_TYPE_IP)
507 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200508 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200509 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300510 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200511 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
512 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
513 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
514
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700515
516
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200517 Ethernet eth = new Ethernet();
518 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300519 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200520 .setEtherType(Ethernet.TYPE_ARP)
521 .setPayload(arpReply);
522
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700523 if (vlan != NO_VLAN) {
524 eth.setVlanID(vlan)
525 .setPriorityCode((byte)0);
526 }
527
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200528 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200529 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200530
531 OFPacketOut po = new OFPacketOut();
532 po.setInPort(OFPort.OFPP_NONE)
533 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200534 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200535 .setActions(actions)
536 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
537 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
538 + po.getPacketData().length);
539
540 List<OFMessage> msgList = new ArrayList<OFMessage>();
541 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200542
543 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
544
545 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200546 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200547 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200548 return;
549 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200550
551 try {
552 sw.write(msgList, null);
553 sw.flush();
554 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200555 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200556 }
557 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300558
559 private String inetAddressToString(byte[] bytes) {
560 try {
561 return InetAddress.getByAddress(bytes).getHostAddress();
562 } catch (UnknownHostException e) {
563 log.debug("Invalid IP address", e);
564 return "";
565 }
566 }
567
568 /*
569 * IProxyArpService methods
570 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200571
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200572 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300573 public MACAddress getMacAddress(InetAddress ipAddress) {
574 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200575 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200576
577 @Override
578 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
579 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200580 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200581
Jonathan Hart6e618212013-08-21 22:28:43 +1200582 //Sanity check to make sure we don't send a request for our own address
Jonathan Harta8887642013-10-28 13:46:54 -0700583 if (!configService.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200584 sendArpRequestForAddress(ipAddress);
585 }
586 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300587
588 @Override
589 public List<String> getMappings() {
590 return arpCache.getMappings();
591 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200592}