blob: 0151212ef3542d5e0ccb82c8d7c3ceaee289402b [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 Hart08ee8522013-09-22 17:34:43 +120025import net.onrc.onos.ofcontroller.bgproute.ILayer3InfoService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120026import net.onrc.onos.ofcontroller.bgproute.Interface;
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 Hartabad6a52013-09-30 18:17:21 +130050 private final IFloodlightProviderService floodlightProvider;
51 private final ITopologyService topology;
52 private final ILayer3InfoService layer3;
Jonathan Hart5afde492013-10-01 12:30:53 +130053 private final 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 Hartabad6a52013-09-30 18:17:21 +130058 private final ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120059
Jonathan Hartabad6a52013-09-30 18:17:21 +130060 private final 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 Hartc7ca35d2013-06-25 20:54:25 +1200109 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart5afde492013-10-01 12:30:53 +1300110 ITopologyService topology, ILayer3InfoService layer3,
111 IRestApiService restApi){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200112 this.floodlightProvider = floodlightProvider;
113 this.topology = topology;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200114 this.layer3 = layer3;
Jonathan Hart5afde492013-10-01 12:30:53 +1300115 this.restApi = restApi;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200116
Jonathan Hartabad6a52013-09-30 18:17:21 +1300117 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200118
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200119 arpRequests = Multimaps.synchronizedSetMultimap(
120 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200121 }
122
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700123 public void startUp(short vlan) {
124 this.vlan = vlan;
125 log.info("vlan set to {}", this.vlan);
126
Jonathan Hart5afde492013-10-01 12:30:53 +1300127 restApi.addRestletRoutable(new ArpWebRoutable());
128
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200129 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200130 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200131 @Override
132 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200133 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200134 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200135 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200136 }
137
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200138 /*
139 * Function that runs periodically to manage the asynchronous request mechanism.
140 * It basically cleans up old ARP requests if we don't get a response for them.
141 * The caller can designate that a request should be retried indefinitely, and
142 * this task will handle that as well.
143 */
144 private void doPeriodicArpProcessing() {
145 SetMultimap<InetAddress, ArpRequest> retryList
146 = HashMultimap.<InetAddress, ArpRequest>create();
147
148 //Have to synchronize externally on the Multimap while using an iterator,
149 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200150 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200151 log.debug("Current have {} outstanding requests",
152 arpRequests.size());
153
154 Iterator<Map.Entry<InetAddress, ArpRequest>> it
155 = arpRequests.entries().iterator();
156
157 while (it.hasNext()) {
158 Map.Entry<InetAddress, ArpRequest> entry
159 = it.next();
160 ArpRequest request = entry.getValue();
161 if (request.isExpired()) {
162 log.debug("Cleaning expired ARP request for {}",
163 entry.getKey().getHostAddress());
164
165 it.remove();
166
167 if (request.shouldRetry()) {
168 retryList.put(entry.getKey(), request);
169 }
170 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200171 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200172 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200173
174 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
175 : retryList.asMap().entrySet()) {
176
177 InetAddress address = entry.getKey();
178
179 log.debug("Resending ARP request for {}", address.getHostAddress());
180
181 sendArpRequestForAddress(address);
182
183 for (ArpRequest request : entry.getValue()) {
184 arpRequests.put(address, new ArpRequest(request));
185 }
186 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200187 }
188
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200189 @Override
190 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200191 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200192 }
193
194 @Override
195 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200196 return false;
197 }
198
199 @Override
200 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200201 return false;
202 }
203
204 @Override
205 public Command receive(
206 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
207
208 if (msg.getType() != OFType.PACKET_IN){
209 return Command.CONTINUE;
210 }
211
212 OFPacketIn pi = (OFPacketIn) msg;
213
214 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
215 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
216
217 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200218 ARP arp = (ARP) eth.getPayload();
219
220 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200221 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200222 }
223 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200224 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200225 }
226 }
227
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200228 //TODO should we propagate ARP or swallow it?
229 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200230 return Command.CONTINUE;
231 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200232
Jonathan Hart1912afc2013-10-11 12:02:44 +1300233 private void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200234 if (log.isTraceEnabled()) {
235 log.trace("ARP request received for {}",
236 inetAddressToString(arp.getTargetProtocolAddress()));
237 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200238
239 InetAddress target;
240 try {
241 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
242 } catch (UnknownHostException e) {
243 log.debug("Invalid address in ARP request", e);
244 return;
245 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200246
247 if (layer3.fromExternalNetwork(sw.getId(), pi.getInPort())) {
248 //If the request came from outside our network, we only care if
249 //it was a request for one of our interfaces.
250 if (layer3.isInterfaceAddress(target)) {
251 log.trace("ARP request for our interface. Sending reply {} => {}",
252 target.getHostAddress(), layer3.getRouterMacAddress());
253
254 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300255 layer3.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200256 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200257
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200258 return;
259 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200260
Jonathan Hartabad6a52013-09-30 18:17:21 +1300261 MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200262
Jonathan Hartabad6a52013-09-30 18:17:21 +1300263 if (macAddress == null){
264 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200265
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200266 //Record where the request came from so we know where to send the reply
Jonathan Hart9ea31212013-08-12 21:40:34 +1200267 arpRequests.put(target, new ArpRequest(
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300268 new HostArpRequester(arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200269
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200270 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200271 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200272 }
273 else {
274 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200275 if (log.isTraceEnabled()) {
276 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
277 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300278 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200279 HexString.toHexString(sw.getId()), pi.getInPort()});
280 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200281
Jonathan Hartabad6a52013-09-30 18:17:21 +1300282 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200283 }
284 }
285
Jonathan Hart1912afc2013-10-11 12:02:44 +1300286 private void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200287 if (log.isTraceEnabled()) {
288 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
289 inetAddressToString(arp.getSenderProtocolAddress()),
290 HexString.toHexString(arp.getSenderHardwareAddress()),
291 HexString.toHexString(sw.getId()), pi.getInPort()});
292 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200293
Jonathan Hartabad6a52013-09-30 18:17:21 +1300294 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200295 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300296 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200297 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200298 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200299 return;
300 }
301
Jonathan Hartabad6a52013-09-30 18:17:21 +1300302 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
303
304 arpCache.update(senderIpAddress, senderMacAddress);
305
306 //See if anyone's waiting for this ARP reply
307 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200308
309 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200310 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200311 synchronized (arpRequests) {
312 Iterator<ArpRequest> it = requests.iterator();
313 while (it.hasNext()) {
314 ArpRequest request = it.next();
315 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200316 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200317 }
318 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200319
320 //Don't hold an ARP lock while dispatching requests
321 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300322 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200323 }
324 }
325
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200326 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200327 //TODO what should the sender IP address and MAC address be if no
328 //IP addresses are configured? Will there ever be a need to send
329 //ARP requests from the controller in that case?
330 //All-zero MAC address doesn't seem to work - hosts don't respond to it
331
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200332 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
333 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200334 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200335 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200336 (byte)0xff, (byte)0xff, (byte)0xff};
337
338 ARP arpRequest = new ARP();
339
340 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
341 .setProtocolType(ARP.PROTO_TYPE_IP)
342 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200343 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200344 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200345 .setTargetHardwareAddress(zeroMac)
346 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200347
Jonathan Hart08ee8522013-09-22 17:34:43 +1200348 MACAddress routerMacAddress = layer3.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200349 //TODO hack for now as it's unclear what the MAC address should be
350 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200351 if (routerMacAddress != null) {
352 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200353 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200354 arpRequest.setSenderHardwareAddress(senderMacAddress);
355
356 byte[] senderIPAddress = zeroIpv4;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200357 Interface intf = layer3.getOutgoingInterface(ipAddress);
358 if (intf != null) {
359 senderIPAddress = intf.getIpAddress().getAddress();
360 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200361
362 arpRequest.setSenderProtocolAddress(senderIPAddress);
363
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200364 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200365 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200366 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200367 .setEtherType(Ethernet.TYPE_ARP)
368 .setPayload(arpRequest);
369
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700370 if (vlan != NO_VLAN) {
371 eth.setVlanID(vlan)
372 .setPriorityCode((byte)0);
373 }
374
Jonathan Hart2f790d22013-08-15 14:01:24 +1200375 sendArpRequestToSwitches(ipAddress, eth.serialize());
376 }
377
378 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200379 sendArpRequestToSwitches(dstAddress, arpRequest,
380 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200381 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200382
Jonathan Hart2f790d22013-08-15 14:01:24 +1200383 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
384 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200385
Jonathan Hart08ee8522013-09-22 17:34:43 +1200386 if (layer3.hasLayer3Configuration()) {
387 Interface intf = layer3.getOutgoingInterface(dstAddress);
388 if (intf != null) {
389 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
390 }
391 else {
392 //TODO here it should be broadcast out all non-interface edge ports.
393 //I think we can assume that if it's not a request for an external
394 //network, it's an ARP for a host in our own network. So we want to
395 //send it out all edge ports that don't have an interface configured
396 //to ensure it reaches all hosts in our network.
397 log.debug("No interface found to send ARP request for {}",
398 dstAddress.getHostAddress());
399 }
400 }
401 else {
402 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
403 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200404 }
405
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200406 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200407 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
408 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
409 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
410
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200411 if (linkPorts == null){
412 //I think this means the switch isn't known to topology yet.
413 //Maybe it only just joined.
414 continue;
415 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200416
417 OFPacketOut po = new OFPacketOut();
418 po.setInPort(OFPort.OFPP_NONE)
419 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200420 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200421
422 List<OFAction> actions = new ArrayList<OFAction>();
423
424 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200425 if (linkPorts.contains(portNum) ||
426 (sw.getId() == inSwitch && portNum == inPort)){
427 //If this port isn't an edge port or is the ingress port
428 //for the ARP, don't broadcast out it
429 continue;
430 }
431
432 actions.add(new OFActionOutput(portNum));
433 }
434
435 po.setActions(actions);
436 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
437 po.setActionsLength(actionsLength);
438 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200439 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200440
441 List<OFMessage> msgList = new ArrayList<OFMessage>();
442 msgList.add(po);
443
444 try {
445 sw.write(msgList, null);
446 sw.flush();
447 } catch (IOException e) {
448 log.error("Failure writing packet out to switch", e);
449 }
450 }
451 }
452
Jonathan Hart2f790d22013-08-15 14:01:24 +1200453 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200454 if (log.isTraceEnabled()) {
455 log.trace("Sending ARP request out {}/{}",
456 HexString.toHexString(dpid), port);
457 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200458
459 OFPacketOut po = new OFPacketOut();
460 po.setInPort(OFPort.OFPP_NONE)
461 .setBufferId(-1)
462 .setPacketData(arpRequest);
463
464 List<OFAction> actions = new ArrayList<OFAction>();
465 actions.add(new OFActionOutput(port));
466 po.setActions(actions);
467 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
468 po.setActionsLength(actionsLength);
469 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
470 + arpRequest.length);
471
472 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
473
474 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200475 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200476 return;
477 }
478
479 try {
480 sw.write(po, null);
481 sw.flush();
482 } catch (IOException e) {
483 log.error("Failure writing packet out to switch", e);
484 }
485 }
486
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300487 private void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200488 if (log.isTraceEnabled()) {
489 log.trace("Sending reply {} => {} to {}", new Object[] {
490 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300491 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200492 inetAddressToString(arpRequest.getSenderProtocolAddress())});
493 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200494
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200495 ARP arpReply = new ARP();
496 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
497 .setProtocolType(ARP.PROTO_TYPE_IP)
498 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200499 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200500 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300501 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200502 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
503 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
504 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
505
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700506
507
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200508 Ethernet eth = new Ethernet();
509 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300510 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200511 .setEtherType(Ethernet.TYPE_ARP)
512 .setPayload(arpReply);
513
Jonathan Hart1cf9de02013-10-21 17:42:29 -0700514 if (vlan != NO_VLAN) {
515 eth.setVlanID(vlan)
516 .setPriorityCode((byte)0);
517 }
518
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200519 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200520 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200521
522 OFPacketOut po = new OFPacketOut();
523 po.setInPort(OFPort.OFPP_NONE)
524 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200525 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200526 .setActions(actions)
527 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
528 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
529 + po.getPacketData().length);
530
531 List<OFMessage> msgList = new ArrayList<OFMessage>();
532 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200533
534 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
535
536 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200537 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200538 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200539 return;
540 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200541
542 try {
543 sw.write(msgList, null);
544 sw.flush();
545 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200546 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200547 }
548 }
Jonathan Hartda4d0e12013-09-30 21:00:20 +1300549
550 private String inetAddressToString(byte[] bytes) {
551 try {
552 return InetAddress.getByAddress(bytes).getHostAddress();
553 } catch (UnknownHostException e) {
554 log.debug("Invalid IP address", e);
555 return "";
556 }
557 }
558
559 /*
560 * IProxyArpService methods
561 */
Jonathan Hartc824ad02013-07-03 15:58:45 +1200562
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200563 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300564 public MACAddress getMacAddress(InetAddress ipAddress) {
565 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200566 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200567
568 @Override
569 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
570 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200571 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200572
Jonathan Hart6e618212013-08-21 22:28:43 +1200573 //Sanity check to make sure we don't send a request for our own address
Jonathan Hart08ee8522013-09-22 17:34:43 +1200574 if (!layer3.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200575 sendArpRequestForAddress(ipAddress);
576 }
577 }
Jonathan Hart5afde492013-10-01 12:30:53 +1300578
579 @Override
580 public List<String> getMappings() {
581 return arpCache.getMappings();
582 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200583}