blob: fe16144197df8ac57c73ad67c3e847b754ee60cb [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 Hartc7ca35d2013-06-25 20:54:25 +120022import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120023import net.floodlightcontroller.util.MACAddress;
Jonathan Hart08ee8522013-09-22 17:34:43 +120024import net.onrc.onos.ofcontroller.bgproute.ILayer3InfoService;
Jonathan Hart2f790d22013-08-15 14:01:24 +120025import net.onrc.onos.ofcontroller.bgproute.Interface;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120026
27import org.openflow.protocol.OFMessage;
28import org.openflow.protocol.OFPacketIn;
29import org.openflow.protocol.OFPacketOut;
30import org.openflow.protocol.OFPort;
31import org.openflow.protocol.OFType;
32import org.openflow.protocol.action.OFAction;
33import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120034import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Jonathan Hart4dfc3652013-08-02 20:22:36 +120038import com.google.common.collect.HashMultimap;
39import com.google.common.collect.Multimaps;
40import com.google.common.collect.SetMultimap;
41
Jonathan Harte751e1c2013-08-23 00:48:47 +120042//TODO REST API to inspect ARP table
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 Hart6261dcd2013-07-22 17:58:35 +120047
Jonathan Hartabad6a52013-09-30 18:17:21 +130048 private final IFloodlightProviderService floodlightProvider;
49 private final ITopologyService topology;
50 private final ILayer3InfoService layer3;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120051
Jonathan Hartabad6a52013-09-30 18:17:21 +130052 private final ArpCache arpCache;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120053
Jonathan Hartabad6a52013-09-30 18:17:21 +130054 private final SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120055
Jonathan Hartabad6a52013-09-30 18:17:21 +130056 private static class ArpRequest {
Jonathan Hart5b803bc2013-09-23 14:46:11 +120057 private final IArpRequester requester;
Jonathan Hartabad6a52013-09-30 18:17:21 +130058 private final boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120059 private long requestTime;
60
Jonathan Hart4dfc3652013-08-02 20:22:36 +120061 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120062 this.requester = requester;
63 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120064 this.requestTime = System.currentTimeMillis();
65 }
66
Jonathan Hart4dfc3652013-08-02 20:22:36 +120067 public ArpRequest(ArpRequest old) {
68 this.requester = old.requester;
69 this.retry = old.retry;
70 this.requestTime = System.currentTimeMillis();
71 }
72
Jonathan Hart4dfc3652013-08-02 20:22:36 +120073 public boolean isExpired() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +120074 return (System.currentTimeMillis() - requestTime)
75 > IProxyArpService.ARP_REQUEST_TIMEOUT;
76 }
77
Jonathan Hart4dfc3652013-08-02 20:22:36 +120078 public boolean shouldRetry() {
79 return retry;
80 }
81
Jonathan Hartabad6a52013-09-30 18:17:21 +130082 public void dispatchReply(InetAddress ipAddress, MACAddress replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120083 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120084 }
85 }
86
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120087 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart08ee8522013-09-22 17:34:43 +120088 ITopologyService topology, ILayer3InfoService layer3){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120089 this.floodlightProvider = floodlightProvider;
90 this.topology = topology;
Jonathan Hart08ee8522013-09-22 17:34:43 +120091 this.layer3 = layer3;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120092
Jonathan Hartabad6a52013-09-30 18:17:21 +130093 arpCache = new ArpCache();
Jonathan Hartdf6ec332013-08-04 01:37:14 +120094
Jonathan Hart4dfc3652013-08-02 20:22:36 +120095 arpRequests = Multimaps.synchronizedSetMultimap(
96 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart2f790d22013-08-15 14:01:24 +120097 }
98
Jonathan Hart2f790d22013-08-15 14:01:24 +120099 public void startUp() {
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +1200100 Timer arpTimer = new Timer("arp-processing");
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200101 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200102 @Override
103 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200104 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200105 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200106 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200107 }
108
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200109 /*
110 * Function that runs periodically to manage the asynchronous request mechanism.
111 * It basically cleans up old ARP requests if we don't get a response for them.
112 * The caller can designate that a request should be retried indefinitely, and
113 * this task will handle that as well.
114 */
115 private void doPeriodicArpProcessing() {
116 SetMultimap<InetAddress, ArpRequest> retryList
117 = HashMultimap.<InetAddress, ArpRequest>create();
118
119 //Have to synchronize externally on the Multimap while using an iterator,
120 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200121 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200122 log.debug("Current have {} outstanding requests",
123 arpRequests.size());
124
125 Iterator<Map.Entry<InetAddress, ArpRequest>> it
126 = arpRequests.entries().iterator();
127
128 while (it.hasNext()) {
129 Map.Entry<InetAddress, ArpRequest> entry
130 = it.next();
131 ArpRequest request = entry.getValue();
132 if (request.isExpired()) {
133 log.debug("Cleaning expired ARP request for {}",
134 entry.getKey().getHostAddress());
135
136 it.remove();
137
138 if (request.shouldRetry()) {
139 retryList.put(entry.getKey(), request);
140 }
141 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200142 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200143 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200144
145 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
146 : retryList.asMap().entrySet()) {
147
148 InetAddress address = entry.getKey();
149
150 log.debug("Resending ARP request for {}", address.getHostAddress());
151
152 sendArpRequestForAddress(address);
153
154 for (ArpRequest request : entry.getValue()) {
155 arpRequests.put(address, new ArpRequest(request));
156 }
157 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200158 }
159
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200160 @Override
161 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200162 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200163 }
164
165 @Override
166 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200167 return false;
168 }
169
170 @Override
171 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200172 return false;
173 }
174
175 @Override
176 public Command receive(
177 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
178
179 if (msg.getType() != OFType.PACKET_IN){
180 return Command.CONTINUE;
181 }
182
183 OFPacketIn pi = (OFPacketIn) msg;
184
185 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
186 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
187
188 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200189 ARP arp = (ARP) eth.getPayload();
190
191 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200192 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200193 }
194 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200195 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200196 }
197 }
198
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200199 //TODO should we propagate ARP or swallow it?
200 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200201 return Command.CONTINUE;
202 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200203
204 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200205 if (log.isTraceEnabled()) {
206 log.trace("ARP request received for {}",
207 inetAddressToString(arp.getTargetProtocolAddress()));
208 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200209
210 InetAddress target;
211 try {
212 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
213 } catch (UnknownHostException e) {
214 log.debug("Invalid address in ARP request", e);
215 return;
216 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200217
218 if (layer3.fromExternalNetwork(sw.getId(), pi.getInPort())) {
219 //If the request came from outside our network, we only care if
220 //it was a request for one of our interfaces.
221 if (layer3.isInterfaceAddress(target)) {
222 log.trace("ARP request for our interface. Sending reply {} => {}",
223 target.getHostAddress(), layer3.getRouterMacAddress());
224
225 sendArpReply(arp, sw.getId(), pi.getInPort(),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300226 layer3.getRouterMacAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200227 }
Jonathan Hart6e618212013-08-21 22:28:43 +1200228
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200229 return;
230 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200231
Jonathan Hartabad6a52013-09-30 18:17:21 +1300232 MACAddress macAddress = arpCache.lookup(target);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200233
Jonathan Hartabad6a52013-09-30 18:17:21 +1300234 if (macAddress == null){
235 //MAC address is not in our ARP cache.
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200236
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200237 //Record where the request came from so we know where to send the reply
Jonathan Hart9ea31212013-08-12 21:40:34 +1200238 arpRequests.put(target, new ArpRequest(
239 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200240
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200241 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200242 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200243 }
244 else {
245 //We know the address, so send a reply
Jonathan Hart08ee8522013-09-22 17:34:43 +1200246 if (log.isTraceEnabled()) {
247 log.trace("Sending reply: {} => {} to host at {}/{}", new Object [] {
248 inetAddressToString(arp.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300249 macAddress.toString(),
Jonathan Hart08ee8522013-09-22 17:34:43 +1200250 HexString.toHexString(sw.getId()), pi.getInPort()});
251 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200252
Jonathan Hartabad6a52013-09-30 18:17:21 +1300253 sendArpReply(arp, sw.getId(), pi.getInPort(), macAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200254 }
255 }
256
257 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart08ee8522013-09-22 17:34:43 +1200258 if (log.isTraceEnabled()) {
259 log.trace("ARP reply recieved: {} => {}, on {}/{}", new Object[] {
260 inetAddressToString(arp.getSenderProtocolAddress()),
261 HexString.toHexString(arp.getSenderHardwareAddress()),
262 HexString.toHexString(sw.getId()), pi.getInPort()});
263 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200264
Jonathan Hartabad6a52013-09-30 18:17:21 +1300265 InetAddress senderIpAddress;
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200266 try {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300267 senderIpAddress = InetAddress.getByAddress(arp.getSenderProtocolAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200268 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200269 log.debug("Invalid address in ARP reply", e);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200270 return;
271 }
272
Jonathan Hartabad6a52013-09-30 18:17:21 +1300273 MACAddress senderMacAddress = MACAddress.valueOf(arp.getSenderHardwareAddress());
274
275 arpCache.update(senderIpAddress, senderMacAddress);
276
277 //See if anyone's waiting for this ARP reply
278 Set<ArpRequest> requests = arpRequests.get(senderIpAddress);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200279
280 //Synchronize on the Multimap while using an iterator for one of the sets
Jonathan Harte751e1c2013-08-23 00:48:47 +1200281 List<ArpRequest> requestsToSend = new ArrayList<ArpRequest>(requests.size());
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200282 synchronized (arpRequests) {
283 Iterator<ArpRequest> it = requests.iterator();
284 while (it.hasNext()) {
285 ArpRequest request = it.next();
286 it.remove();
Jonathan Harte751e1c2013-08-23 00:48:47 +1200287 requestsToSend.add(request);
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200288 }
289 }
Jonathan Harte751e1c2013-08-23 00:48:47 +1200290
291 //Don't hold an ARP lock while dispatching requests
292 for (ArpRequest request : requestsToSend) {
Jonathan Hartabad6a52013-09-30 18:17:21 +1300293 request.dispatchReply(senderIpAddress, senderMacAddress);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200294 }
295 }
296
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200297 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart08ee8522013-09-22 17:34:43 +1200298 //TODO what should the sender IP address and MAC address be if no
299 //IP addresses are configured? Will there ever be a need to send
300 //ARP requests from the controller in that case?
301 //All-zero MAC address doesn't seem to work - hosts don't respond to it
302
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200303 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
304 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart08ee8522013-09-22 17:34:43 +1200305 byte[] genericNonZeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200306 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200307 (byte)0xff, (byte)0xff, (byte)0xff};
308
309 ARP arpRequest = new ARP();
310
311 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
312 .setProtocolType(ARP.PROTO_TYPE_IP)
313 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200314 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200315 .setOpCode(ARP.OP_REQUEST)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200316 .setTargetHardwareAddress(zeroMac)
317 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200318
Jonathan Hart08ee8522013-09-22 17:34:43 +1200319 MACAddress routerMacAddress = layer3.getRouterMacAddress();
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200320 //TODO hack for now as it's unclear what the MAC address should be
321 byte[] senderMacAddress = genericNonZeroMac;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200322 if (routerMacAddress != null) {
323 senderMacAddress = routerMacAddress.toBytes();
Jonathan Hart2f790d22013-08-15 14:01:24 +1200324 }
Jonathan Hart08ee8522013-09-22 17:34:43 +1200325 arpRequest.setSenderHardwareAddress(senderMacAddress);
326
327 byte[] senderIPAddress = zeroIpv4;
Jonathan Hart08ee8522013-09-22 17:34:43 +1200328 Interface intf = layer3.getOutgoingInterface(ipAddress);
329 if (intf != null) {
330 senderIPAddress = intf.getIpAddress().getAddress();
331 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200332
333 arpRequest.setSenderProtocolAddress(senderIPAddress);
334
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200335 Ethernet eth = new Ethernet();
Jonathan Hart08ee8522013-09-22 17:34:43 +1200336 eth.setSourceMACAddress(senderMacAddress)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200337 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200338 .setEtherType(Ethernet.TYPE_ARP)
339 .setPayload(arpRequest);
340
Jonathan Hart2f790d22013-08-15 14:01:24 +1200341 sendArpRequestToSwitches(ipAddress, eth.serialize());
342 }
343
344 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200345 sendArpRequestToSwitches(dstAddress, arpRequest,
346 0, OFPort.OFPP_NONE.getValue());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200347 }
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200348
Jonathan Hart2f790d22013-08-15 14:01:24 +1200349 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
350 long inSwitch, short inPort) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200351
Jonathan Hart08ee8522013-09-22 17:34:43 +1200352 if (layer3.hasLayer3Configuration()) {
353 Interface intf = layer3.getOutgoingInterface(dstAddress);
354 if (intf != null) {
355 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
356 }
357 else {
358 //TODO here it should be broadcast out all non-interface edge ports.
359 //I think we can assume that if it's not a request for an external
360 //network, it's an ARP for a host in our own network. So we want to
361 //send it out all edge ports that don't have an interface configured
362 //to ensure it reaches all hosts in our network.
363 log.debug("No interface found to send ARP request for {}",
364 dstAddress.getHostAddress());
365 }
366 }
367 else {
368 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
369 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200370 }
371
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200372 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200373 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
374 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
375 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
376
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200377 if (linkPorts == null){
378 //I think this means the switch isn't known to topology yet.
379 //Maybe it only just joined.
380 continue;
381 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200382
383 OFPacketOut po = new OFPacketOut();
384 po.setInPort(OFPort.OFPP_NONE)
385 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200386 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200387
388 List<OFAction> actions = new ArrayList<OFAction>();
389
390 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200391 if (linkPorts.contains(portNum) ||
392 (sw.getId() == inSwitch && portNum == inPort)){
393 //If this port isn't an edge port or is the ingress port
394 //for the ARP, don't broadcast out it
395 continue;
396 }
397
398 actions.add(new OFActionOutput(portNum));
399 }
400
401 po.setActions(actions);
402 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
403 po.setActionsLength(actionsLength);
404 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200405 + arpRequest.length);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200406
407 List<OFMessage> msgList = new ArrayList<OFMessage>();
408 msgList.add(po);
409
410 try {
411 sw.write(msgList, null);
412 sw.flush();
413 } catch (IOException e) {
414 log.error("Failure writing packet out to switch", e);
415 }
416 }
417 }
418
Jonathan Hart2f790d22013-08-15 14:01:24 +1200419 private void sendArpRequestOutPort(byte[] arpRequest, long dpid, short port) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200420 if (log.isTraceEnabled()) {
421 log.trace("Sending ARP request out {}/{}",
422 HexString.toHexString(dpid), port);
423 }
Jonathan Hart2f790d22013-08-15 14:01:24 +1200424
425 OFPacketOut po = new OFPacketOut();
426 po.setInPort(OFPort.OFPP_NONE)
427 .setBufferId(-1)
428 .setPacketData(arpRequest);
429
430 List<OFAction> actions = new ArrayList<OFAction>();
431 actions.add(new OFActionOutput(port));
432 po.setActions(actions);
433 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
434 po.setActionsLength(actionsLength);
435 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
436 + arpRequest.length);
437
438 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
439
440 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200441 log.warn("Switch not found when sending ARP request");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200442 return;
443 }
444
445 try {
446 sw.write(po, null);
447 sw.flush();
448 } catch (IOException e) {
449 log.error("Failure writing packet out to switch", e);
450 }
451 }
452
Jonathan Hart08ee8522013-09-22 17:34:43 +1200453 private String inetAddressToString(byte[] bytes) {
454 try {
455 return InetAddress.getByAddress(bytes).getHostAddress();
456 } catch (UnknownHostException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200457 log.debug("Invalid IP address", e);
Jonathan Hart08ee8522013-09-22 17:34:43 +1200458 return "";
459 }
460 }
461
462 /*
463 * IProxyArpService methods
464 */
465
Jonathan Hartabad6a52013-09-30 18:17:21 +1300466 @Override
467 public void sendArpReply(ARP arpRequest, long dpid, short port, MACAddress targetMac) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200468 if (log.isTraceEnabled()) {
469 log.trace("Sending reply {} => {} to {}", new Object[] {
470 inetAddressToString(arpRequest.getTargetProtocolAddress()),
Jonathan Hartabad6a52013-09-30 18:17:21 +1300471 targetMac,
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200472 inetAddressToString(arpRequest.getSenderProtocolAddress())});
473 }
Jonathan Hart1633a402013-08-24 11:38:56 +1200474
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200475 ARP arpReply = new ARP();
476 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
477 .setProtocolType(ARP.PROTO_TYPE_IP)
478 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
Jonathan Hart08ee8522013-09-22 17:34:43 +1200479 .setProtocolAddressLength((byte)IPv4.ADDRESS_LENGTH)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200480 .setOpCode(ARP.OP_REPLY)
Jonathan Hartabad6a52013-09-30 18:17:21 +1300481 .setSenderHardwareAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200482 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
483 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
484 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
485
486 Ethernet eth = new Ethernet();
487 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hartabad6a52013-09-30 18:17:21 +1300488 .setSourceMACAddress(targetMac.toBytes())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200489 .setEtherType(Ethernet.TYPE_ARP)
490 .setPayload(arpReply);
491
492 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200493 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200494
495 OFPacketOut po = new OFPacketOut();
496 po.setInPort(OFPort.OFPP_NONE)
497 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200498 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200499 .setActions(actions)
500 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
501 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
502 + po.getPacketData().length);
503
504 List<OFMessage> msgList = new ArrayList<OFMessage>();
505 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200506
507 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
508
509 if (sw == null) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200510 log.warn("Switch {} not found when sending ARP reply",
Jonathan Hart1633a402013-08-24 11:38:56 +1200511 HexString.toHexString(dpid));
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200512 return;
513 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200514
515 try {
516 sw.write(msgList, null);
517 sw.flush();
518 } catch (IOException e) {
Jonathan Hart5b803bc2013-09-23 14:46:11 +1200519 log.error("Failure writing packet out to switch", e);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200520 }
521 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200522
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200523 @Override
Jonathan Hartabad6a52013-09-30 18:17:21 +1300524 public MACAddress getMacAddress(InetAddress ipAddress) {
525 return arpCache.lookup(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200526 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200527
528 @Override
529 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
530 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200531 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200532
Jonathan Hart6e618212013-08-21 22:28:43 +1200533 //Sanity check to make sure we don't send a request for our own address
Jonathan Hart08ee8522013-09-22 17:34:43 +1200534 if (!layer3.isInterfaceAddress(ipAddress)) {
Jonathan Hart6e618212013-08-21 22:28:43 +1200535 sendArpRequestForAddress(ipAddress);
536 }
537 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200538}