blob: ca84bc0b48915f797052d134a0aeec663cee4be9 [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;
8import java.util.HashMap;
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;
20import net.floodlightcontroller.packet.ARP;
21import net.floodlightcontroller.packet.Ethernet;
22import net.floodlightcontroller.topology.ITopologyService;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120023import net.floodlightcontroller.util.MACAddress;
Jonathan Hart2f790d22013-08-15 14:01:24 +120024import net.onrc.onos.ofcontroller.bgproute.IPatriciaTrie;
25import net.onrc.onos.ofcontroller.bgproute.Interface;
26import net.onrc.onos.ofcontroller.bgproute.Prefix;
27import net.onrc.onos.ofcontroller.util.SwitchPort;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120028
29import org.openflow.protocol.OFMessage;
30import org.openflow.protocol.OFPacketIn;
31import org.openflow.protocol.OFPacketOut;
32import org.openflow.protocol.OFPort;
33import org.openflow.protocol.OFType;
34import org.openflow.protocol.action.OFAction;
35import org.openflow.protocol.action.OFActionOutput;
Jonathan Hart8ec133c2013-06-26 15:25:18 +120036import org.openflow.util.HexString;
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
Jonathan Hart4dfc3652013-08-02 20:22:36 +120040import com.google.common.collect.HashMultimap;
41import com.google.common.collect.Multimaps;
42import com.google.common.collect.SetMultimap;
43
Jonathan Hart32e18222013-08-07 22:05:42 +120044//TODO have L2 and also L3 mode, where it takes into account interface addresses
Jonathan Hart6261dcd2013-07-22 17:58:35 +120045public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120046 private static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
47
Jonathan Hart6261dcd2013-07-22 17:58:35 +120048 private final long ARP_ENTRY_TIMEOUT = 600000; //ms (== 10 mins)
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 Hart6261dcd2013-07-22 17:58:35 +120051
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120052 protected IFloodlightProviderService floodlightProvider;
53 protected ITopologyService topology;
54
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120055 protected Map<InetAddress, ArpTableEntry> arpTable;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120056
Jonathan Hart4dfc3652013-08-02 20:22:36 +120057 protected SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120058
Jonathan Hart2f790d22013-08-15 14:01:24 +120059 public enum Mode {L2_MODE, L3_MODE}
60
61 private Mode mode;
62 private IPatriciaTrie<Interface> interfacePtrie = null;
63 private MACAddress routerMacAddress = null;
64 //private SwitchPort bgpdAttachmentPoint = null;
65
Jonathan Hart6261dcd2013-07-22 17:58:35 +120066 private class ArpRequest {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120067 private IArpRequester requester;
68 private boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120069 private long requestTime;
70
Jonathan Hart4dfc3652013-08-02 20:22:36 +120071 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120072 this.requester = requester;
73 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120074 this.requestTime = System.currentTimeMillis();
75 }
76
Jonathan Hart4dfc3652013-08-02 20:22:36 +120077 public ArpRequest(ArpRequest old) {
78 this.requester = old.requester;
79 this.retry = old.retry;
80 this.requestTime = System.currentTimeMillis();
81 }
82
Jonathan Hart4dfc3652013-08-02 20:22:36 +120083 public boolean isExpired() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +120084 return (System.currentTimeMillis() - requestTime)
85 > IProxyArpService.ARP_REQUEST_TIMEOUT;
86 }
87
Jonathan Hart4dfc3652013-08-02 20:22:36 +120088 public boolean shouldRetry() {
89 return retry;
90 }
91
Jonathan Hart32e18222013-08-07 22:05:42 +120092 public void dispatchReply(InetAddress ipAddress, byte[] replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120093 log.debug("Dispatching reply for {} to {}", ipAddress.getHostAddress(),
94 requester);
95 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120096 }
97 }
98
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120099 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart2f790d22013-08-15 14:01:24 +1200100 ITopologyService topology){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200101 this.floodlightProvider = floodlightProvider;
102 this.topology = topology;
103
104 arpTable = new HashMap<InetAddress, ArpTableEntry>();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200105
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200106 arpRequests = Multimaps.synchronizedSetMultimap(
107 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200108
Jonathan Hart2f790d22013-08-15 14:01:24 +1200109 mode = Mode.L2_MODE;
110 }
111
112 public void setL3Mode(IPatriciaTrie<Interface> interfacePtrie, MACAddress routerMacAddress) {
113 this.interfacePtrie = interfacePtrie;
114 this.routerMacAddress = routerMacAddress;
115 //this.bgpdAttachmentPoint = bgpdAttachmentPoint;
116 mode = Mode.L3_MODE;
117 }
118
119 public void startUp() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200120 Timer arpTimer = new Timer();
121 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200122 @Override
123 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200124 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200125 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200126 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200127 }
128
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200129 /*
130 * Function that runs periodically to manage the asynchronous request mechanism.
131 * It basically cleans up old ARP requests if we don't get a response for them.
132 * The caller can designate that a request should be retried indefinitely, and
133 * this task will handle that as well.
134 */
135 private void doPeriodicArpProcessing() {
136 SetMultimap<InetAddress, ArpRequest> retryList
137 = HashMultimap.<InetAddress, ArpRequest>create();
138
139 //Have to synchronize externally on the Multimap while using an iterator,
140 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200141 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200142 log.debug("Current have {} outstanding requests",
143 arpRequests.size());
144
145 Iterator<Map.Entry<InetAddress, ArpRequest>> it
146 = arpRequests.entries().iterator();
147
148 while (it.hasNext()) {
149 Map.Entry<InetAddress, ArpRequest> entry
150 = it.next();
151 ArpRequest request = entry.getValue();
152 if (request.isExpired()) {
153 log.debug("Cleaning expired ARP request for {}",
154 entry.getKey().getHostAddress());
155
156 it.remove();
157
158 if (request.shouldRetry()) {
159 retryList.put(entry.getKey(), request);
160 }
161 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200162 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200163 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200164
165 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
166 : retryList.asMap().entrySet()) {
167
168 InetAddress address = entry.getKey();
169
170 log.debug("Resending ARP request for {}", address.getHostAddress());
171
172 sendArpRequestForAddress(address);
173
174 for (ArpRequest request : entry.getValue()) {
175 arpRequests.put(address, new ArpRequest(request));
176 }
177 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200178 }
179
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200180 @Override
181 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200182 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200183 }
184
185 @Override
186 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200187 return false;
188 }
189
190 @Override
191 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200192 return false;
193 }
194
195 @Override
196 public Command receive(
197 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
198
199 if (msg.getType() != OFType.PACKET_IN){
200 return Command.CONTINUE;
201 }
202
203 OFPacketIn pi = (OFPacketIn) msg;
204
205 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
206 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
207
208 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200209 ARP arp = (ARP) eth.getPayload();
210
211 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200212 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200213 }
214 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200215 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200216 }
217 }
218
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200219 //TODO should we propagate ARP or swallow it?
220 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200221 return Command.CONTINUE;
222 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200223
224 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
225 log.debug("ARP request received for {}",
226 bytesToStringAddr(arp.getTargetProtocolAddress()));
Jonathan Hart2f790d22013-08-15 14:01:24 +1200227
228 InetAddress target;
229 try {
230 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
231 } catch (UnknownHostException e) {
232 log.debug("Invalid address in ARP request", e);
233 return;
234 }
235
236 if (mode == Mode.L3_MODE) {
237 Interface intf = interfacePtrie.match(new Prefix(target.getAddress(), 32));
238 if (intf != null && target.equals(intf.getIpAddress())) {
239 //ARP request for one of our interfaces, we can reply straight away
240 sendArpReply(arp, sw.getId(), pi.getInPort(), routerMacAddress.toBytes());
241 return;
242 }
243 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200244
245 byte[] mac = lookupArpTable(arp.getTargetProtocolAddress());
246
247 if (mac == null){
248 //Mac address is not in our arp table.
249
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200250 //Record where the request came from so we know where to send the reply
Jonathan Hart2f790d22013-08-15 14:01:24 +1200251
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200252
Jonathan Hart9ea31212013-08-12 21:40:34 +1200253 //Should we just broadcast all received requests here? Or rate limit
254 //if we know we just sent an request?
255 arpRequests.put(target, new ArpRequest(
256 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200257
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200258 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200259 //broadcastArpRequestOutEdge(pi.getPacketData(), sw.getId(), pi.getInPort());
260 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200261 }
262 else {
263 //We know the address, so send a reply
264 log.debug("Sending reply of {}", MACAddress.valueOf(mac).toString());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200265 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
266 }
267 }
268
269 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200270 log.debug("ARP reply recieved for {}, is {}, on {}/{}", new Object[] {
271 bytesToStringAddr(arp.getSenderProtocolAddress()),
272 HexString.toHexString(arp.getSenderHardwareAddress()),
273 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200274
275 updateArpTable(arp);
276
277 //See if anyone's waiting for this ARP reply
278 InetAddress addr;
279 try {
280 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
281 } catch (UnknownHostException e) {
282 return;
283 }
284
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200285 Set<ArpRequest> requests = arpRequests.get(addr);
286
287 //Synchronize on the Multimap while using an iterator for one of the sets
288 synchronized (arpRequests) {
289 Iterator<ArpRequest> it = requests.iterator();
290 while (it.hasNext()) {
291 ArpRequest request = it.next();
292 it.remove();
293 request.dispatchReply(addr, arp.getSenderHardwareAddress());
294 }
295 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200296 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200297
298 private synchronized byte[] lookupArpTable(byte[] ipAddress){
299 InetAddress addr;
300 try {
301 addr = InetAddress.getByAddress(ipAddress);
302 } catch (UnknownHostException e) {
303 log.warn("Unable to create InetAddress", e);
304 return null;
305 }
306
307 ArpTableEntry arpEntry = arpTable.get(addr);
308
309 if (arpEntry == null){
Jonathan Hart2f790d22013-08-15 14:01:24 +1200310 //log.debug("MAC for {} unknown", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200311 return null;
312 }
313
314 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
315 > ARP_ENTRY_TIMEOUT){
316 //Entry has timed out so we'll remove it and return null
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200317 log.debug("Timing out old ARP entry for {}", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200318 arpTable.remove(addr);
319 return null;
320 }
321
322 return arpEntry.getMacAddress();
323 }
324
325 private synchronized void updateArpTable(ARP arp){
326 InetAddress addr;
327 try {
328 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
329 } catch (UnknownHostException e) {
330 log.warn("Unable to create InetAddress", e);
331 return;
332 }
333
334 ArpTableEntry arpEntry = arpTable.get(addr);
335
336 if (arpEntry != null
337 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
338 arpEntry.setTimeLastSeen(System.currentTimeMillis());
339 }
340 else {
341 arpTable.put(addr,
342 new ArpTableEntry(arp.getSenderHardwareAddress(),
343 System.currentTimeMillis()));
344 }
345 }
346
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200347 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200348 //TODO what should the sender IP address be? Probably not 0.0.0.0
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200349 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
350 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200351 byte[] bgpdMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
352 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200353 (byte)0xff, (byte)0xff, (byte)0xff};
354
355 ARP arpRequest = new ARP();
356
357 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
358 .setProtocolType(ARP.PROTO_TYPE_IP)
359 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
360 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
361 .setOpCode(ARP.OP_REQUEST)
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200362 .setSenderHardwareAddress(bgpdMac)
Jonathan Hart2f790d22013-08-15 14:01:24 +1200363 //.setSenderProtocolAddress(zeroIpv4)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200364 .setTargetHardwareAddress(zeroMac)
365 .setTargetProtocolAddress(ipAddress.getAddress());
Jonathan Hart2f790d22013-08-15 14:01:24 +1200366
367 byte[] senderIPAddress = zeroIpv4;
368 if (mode == Mode.L3_MODE) {
369 Interface intf = interfacePtrie.match(new Prefix(ipAddress.getAddress(), 32));
370 if (intf != null) {
371 senderIPAddress = intf.getIpAddress().getAddress();
372 }
373 }
374
375 arpRequest.setSenderProtocolAddress(senderIPAddress);
376
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200377 Ethernet eth = new Ethernet();
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200378 eth.setSourceMACAddress(bgpdMac)
379 .setDestinationMACAddress(broadcastMac)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200380 .setEtherType(Ethernet.TYPE_ARP)
381 .setPayload(arpRequest);
382
Jonathan Hart2f790d22013-08-15 14:01:24 +1200383 //broadcastArpRequestOutEdge(eth.serialize(), 0, OFPort.OFPP_NONE.getValue());
384 sendArpRequestToSwitches(ipAddress, eth.serialize());
385 }
386
387 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest) {
388 sendArpRequestToSwitches(dstAddress, arpRequest, 0, OFPort.OFPP_NONE.getValue());
389 }
390 private void sendArpRequestToSwitches(InetAddress dstAddress, byte[] arpRequest,
391 long inSwitch, short inPort) {
392 if (mode == Mode.L2_MODE) {
393 log.debug("mode is l2");
394 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
395 }
396 else if (mode == Mode.L3_MODE) {
397 log.debug("mode is l3");
398 Interface intf = interfacePtrie.match(new Prefix(dstAddress.getAddress(), 32));
399 if (intf != null) {
400 sendArpRequestOutPort(arpRequest, intf.getDpid(), intf.getPort());
401 }
402 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200403 }
404
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200405 private void broadcastArpRequestOutEdge(byte[] arpRequest, long inSwitch, short inPort) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200406 for (IOFSwitch sw : floodlightProvider.getSwitches().values()){
407 Collection<Short> enabledPorts = sw.getEnabledPortNumbers();
408 Set<Short> linkPorts = topology.getPortsWithLinks(sw.getId());
409
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200410 if (linkPorts == null){
411 //I think this means the switch isn't known to topology yet.
412 //Maybe it only just joined.
413 continue;
414 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200415
416 OFPacketOut po = new OFPacketOut();
417 po.setInPort(OFPort.OFPP_NONE)
418 .setBufferId(-1)
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200419 .setPacketData(arpRequest);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200420
421 List<OFAction> actions = new ArrayList<OFAction>();
422
423 for (short portNum : enabledPorts){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200424 if (linkPorts.contains(portNum) ||
425 (sw.getId() == inSwitch && portNum == inPort)){
426 //If this port isn't an edge port or is the ingress port
427 //for the ARP, don't broadcast out it
428 continue;
429 }
430
431 actions.add(new OFActionOutput(portNum));
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200432 //log.debug("Broadcasting out {}/{}", HexString.toHexString(sw.getId()), portNum);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200433 }
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) {
454 log.debug("Sending ARP request out {}/{}", HexString.toHexString(dpid), port);
455
456 OFPacketOut po = new OFPacketOut();
457 po.setInPort(OFPort.OFPP_NONE)
458 .setBufferId(-1)
459 .setPacketData(arpRequest);
460
461 List<OFAction> actions = new ArrayList<OFAction>();
462 actions.add(new OFActionOutput(port));
463 po.setActions(actions);
464 short actionsLength = (short) (actions.size() * OFActionOutput.MINIMUM_LENGTH);
465 po.setActionsLength(actionsLength);
466 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + actionsLength
467 + arpRequest.length);
468
469 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
470
471 if (sw == null) {
472 log.debug("Switch not found when sending ARP request");
473 return;
474 }
475
476 try {
477 sw.write(po, null);
478 sw.flush();
479 } catch (IOException e) {
480 log.error("Failure writing packet out to switch", e);
481 }
482 }
483
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200484 public void sendArpReply(ARP arpRequest, long dpid, short port, byte[] targetMac) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200485 ARP arpReply = new ARP();
486 arpReply.setHardwareType(ARP.HW_TYPE_ETHERNET)
487 .setProtocolType(ARP.PROTO_TYPE_IP)
488 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
489 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
490 .setOpCode(ARP.OP_REPLY)
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200491 .setSenderHardwareAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200492 .setSenderProtocolAddress(arpRequest.getTargetProtocolAddress())
493 .setTargetHardwareAddress(arpRequest.getSenderHardwareAddress())
494 .setTargetProtocolAddress(arpRequest.getSenderProtocolAddress());
495
496 Ethernet eth = new Ethernet();
497 eth.setDestinationMACAddress(arpRequest.getSenderHardwareAddress())
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200498 .setSourceMACAddress(targetMac)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200499 .setEtherType(Ethernet.TYPE_ARP)
500 .setPayload(arpReply);
501
502 List<OFAction> actions = new ArrayList<OFAction>();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200503 actions.add(new OFActionOutput(port));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200504
505 OFPacketOut po = new OFPacketOut();
506 po.setInPort(OFPort.OFPP_NONE)
507 .setBufferId(-1)
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200508 .setPacketData(eth.serialize())
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200509 .setActions(actions)
510 .setActionsLength((short)OFActionOutput.MINIMUM_LENGTH)
511 .setLengthU(OFPacketOut.MINIMUM_LENGTH + OFActionOutput.MINIMUM_LENGTH
512 + po.getPacketData().length);
513
514 List<OFMessage> msgList = new ArrayList<OFMessage>();
515 msgList.add(po);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200516
517 IOFSwitch sw = floodlightProvider.getSwitches().get(dpid);
518
519 if (sw == null) {
520 return;
521 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200522
523 try {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200524 log.debug("Sending ARP reply to {}/{}", HexString.toHexString(sw.getId()), port);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200525 sw.write(msgList, null);
526 sw.flush();
527 } catch (IOException e) {
528 log.warn("Failure writing packet out to switch", e);
529 }
530 }
Jonathan Hartc824ad02013-07-03 15:58:45 +1200531
532 //TODO this should be put somewhere more central. I use it in BgpRoute as well.
533 //We need a HexString.toHexString() equivalent.
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200534 private String bytesToStringAddr(byte[] bytes) {
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200535 InetAddress addr;
536 try {
537 addr = InetAddress.getByAddress(bytes);
538 } catch (UnknownHostException e) {
Jonathan Hartc824ad02013-07-03 15:58:45 +1200539 log.warn(" ", e);
Jonathan Hart8ec133c2013-06-26 15:25:18 +1200540 return "";
541 }
542 if (addr == null) return "";
543 else return addr.getHostAddress();
544 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200545
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200546 @Override
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200547 public byte[] getMacAddress(InetAddress ipAddress) {
548 return lookupArpTable(ipAddress.getAddress());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200549 }
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200550
551 @Override
552 public void sendArpRequest(InetAddress ipAddress, IArpRequester requester,
553 boolean retry) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200554 arpRequests.put(ipAddress, new ArpRequest(requester, retry));
555 //storeRequester(ipAddress, requester, retry);
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200556
557 sendArpRequestForAddress(ipAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200558 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200559}