blob: ab654c1cd278764f08f4f7018437e69f13cd98cd [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;
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 Hart32e18222013-08-07 22:05:42 +120043//TODO have L2 and also L3 mode, where it takes into account interface addresses
Jonathan Hart6261dcd2013-07-22 17:58:35 +120044public class ProxyArpManager implements IProxyArpService, IOFMessageListener {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120045 private static Logger log = LoggerFactory.getLogger(ProxyArpManager.class);
46
Jonathan Hart6261dcd2013-07-22 17:58:35 +120047 private final long ARP_ENTRY_TIMEOUT = 600000; //ms (== 10 mins)
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120048
Jonathan Hartdf6ec332013-08-04 01:37:14 +120049 private final long ARP_TIMER_PERIOD = 60000; //ms (== 1 min)
Jonathan Hart6261dcd2013-07-22 17:58:35 +120050
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120051 protected IFloodlightProviderService floodlightProvider;
52 protected ITopologyService topology;
53
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120054 protected Map<InetAddress, ArpTableEntry> arpTable;
Jonathan Hartdf6ec332013-08-04 01:37:14 +120055
Jonathan Hart4dfc3652013-08-02 20:22:36 +120056 protected SetMultimap<InetAddress, ArpRequest> arpRequests;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120057
Jonathan Hart2f790d22013-08-15 14:01:24 +120058 public enum Mode {L2_MODE, L3_MODE}
59
60 private Mode mode;
61 private IPatriciaTrie<Interface> interfacePtrie = null;
62 private MACAddress routerMacAddress = null;
63 //private SwitchPort bgpdAttachmentPoint = null;
64
Jonathan Hart6261dcd2013-07-22 17:58:35 +120065 private class ArpRequest {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120066 private IArpRequester requester;
67 private boolean retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120068 private long requestTime;
69
Jonathan Hart4dfc3652013-08-02 20:22:36 +120070 public ArpRequest(IArpRequester requester, boolean retry){
Jonathan Hart4dfc3652013-08-02 20:22:36 +120071 this.requester = requester;
72 this.retry = retry;
Jonathan Hart6261dcd2013-07-22 17:58:35 +120073 this.requestTime = System.currentTimeMillis();
74 }
75
Jonathan Hart4dfc3652013-08-02 20:22:36 +120076 public ArpRequest(ArpRequest old) {
77 this.requester = old.requester;
78 this.retry = old.retry;
79 this.requestTime = System.currentTimeMillis();
80 }
81
Jonathan Hart4dfc3652013-08-02 20:22:36 +120082 public boolean isExpired() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +120083 return (System.currentTimeMillis() - requestTime)
84 > IProxyArpService.ARP_REQUEST_TIMEOUT;
85 }
86
Jonathan Hart4dfc3652013-08-02 20:22:36 +120087 public boolean shouldRetry() {
88 return retry;
89 }
90
Jonathan Hart32e18222013-08-07 22:05:42 +120091 public void dispatchReply(InetAddress ipAddress, byte[] replyMacAddress) {
Jonathan Hart4dfc3652013-08-02 20:22:36 +120092 log.debug("Dispatching reply for {} to {}", ipAddress.getHostAddress(),
93 requester);
94 requester.arpResponse(ipAddress, replyMacAddress);
Jonathan Hart6261dcd2013-07-22 17:58:35 +120095 }
96 }
97
Jonathan Hartc7ca35d2013-06-25 20:54:25 +120098 public ProxyArpManager(IFloodlightProviderService floodlightProvider,
Jonathan Hart2f790d22013-08-15 14:01:24 +120099 ITopologyService topology){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200100 this.floodlightProvider = floodlightProvider;
101 this.topology = topology;
102
103 arpTable = new HashMap<InetAddress, ArpTableEntry>();
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200104
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200105 arpRequests = Multimaps.synchronizedSetMultimap(
106 HashMultimap.<InetAddress, ArpRequest>create());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200107
Jonathan Hart2f790d22013-08-15 14:01:24 +1200108 mode = Mode.L2_MODE;
109 }
110
111 public void setL3Mode(IPatriciaTrie<Interface> interfacePtrie, MACAddress routerMacAddress) {
112 this.interfacePtrie = interfacePtrie;
113 this.routerMacAddress = routerMacAddress;
114 //this.bgpdAttachmentPoint = bgpdAttachmentPoint;
115 mode = Mode.L3_MODE;
116 }
117
118 public void startUp() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200119 Timer arpTimer = new Timer();
120 arpTimer.scheduleAtFixedRate(new TimerTask() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200121 @Override
122 public void run() {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200123 doPeriodicArpProcessing();
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200124 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200125 }, 0, ARP_TIMER_PERIOD);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200126 }
127
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200128 /*
129 * Function that runs periodically to manage the asynchronous request mechanism.
130 * It basically cleans up old ARP requests if we don't get a response for them.
131 * The caller can designate that a request should be retried indefinitely, and
132 * this task will handle that as well.
133 */
134 private void doPeriodicArpProcessing() {
135 SetMultimap<InetAddress, ArpRequest> retryList
136 = HashMultimap.<InetAddress, ArpRequest>create();
137
138 //Have to synchronize externally on the Multimap while using an iterator,
139 //even though it's a synchronizedMultimap
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200140 synchronized (arpRequests) {
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200141 log.debug("Current have {} outstanding requests",
142 arpRequests.size());
143
144 Iterator<Map.Entry<InetAddress, ArpRequest>> it
145 = arpRequests.entries().iterator();
146
147 while (it.hasNext()) {
148 Map.Entry<InetAddress, ArpRequest> entry
149 = it.next();
150 ArpRequest request = entry.getValue();
151 if (request.isExpired()) {
152 log.debug("Cleaning expired ARP request for {}",
153 entry.getKey().getHostAddress());
154
155 it.remove();
156
157 if (request.shouldRetry()) {
158 retryList.put(entry.getKey(), request);
159 }
160 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200161 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200162 }
Jonathan Hartdf6ec332013-08-04 01:37:14 +1200163
164 for (Map.Entry<InetAddress, Collection<ArpRequest>> entry
165 : retryList.asMap().entrySet()) {
166
167 InetAddress address = entry.getKey();
168
169 log.debug("Resending ARP request for {}", address.getHostAddress());
170
171 sendArpRequestForAddress(address);
172
173 for (ArpRequest request : entry.getValue()) {
174 arpRequests.put(address, new ArpRequest(request));
175 }
176 }
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200177 }
178
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200179 @Override
180 public String getName() {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200181 return "ProxyArpManager";
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200182 }
183
184 @Override
185 public boolean isCallbackOrderingPrereq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200186 return false;
187 }
188
189 @Override
190 public boolean isCallbackOrderingPostreq(OFType type, String name) {
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200191 return false;
192 }
193
194 @Override
195 public Command receive(
196 IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
197
198 if (msg.getType() != OFType.PACKET_IN){
199 return Command.CONTINUE;
200 }
201
202 OFPacketIn pi = (OFPacketIn) msg;
203
204 Ethernet eth = IFloodlightProviderService.bcStore.get(cntx,
205 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
206
207 if (eth.getEtherType() == Ethernet.TYPE_ARP){
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200208 ARP arp = (ARP) eth.getPayload();
209
210 if (arp.getOpCode() == ARP.OP_REQUEST) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200211 handleArpRequest(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200212 }
213 else if (arp.getOpCode() == ARP.OP_REPLY) {
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200214 handleArpReply(sw, pi, arp);
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200215 }
216 }
217
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200218 //TODO should we propagate ARP or swallow it?
219 //Always propagate for now so DeviceManager can learn the host location
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200220 return Command.CONTINUE;
221 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200222
223 protected void handleArpRequest(IOFSwitch sw, OFPacketIn pi, ARP arp) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200224 log.trace("ARP request received for {}",
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200225 bytesToStringAddr(arp.getTargetProtocolAddress()));
Jonathan Hart2f790d22013-08-15 14:01:24 +1200226
227 InetAddress target;
228 try {
229 target = InetAddress.getByAddress(arp.getTargetProtocolAddress());
230 } catch (UnknownHostException e) {
231 log.debug("Invalid address in ARP request", e);
232 return;
233 }
234
235 if (mode == Mode.L3_MODE) {
236 Interface intf = interfacePtrie.match(new Prefix(target.getAddress(), 32));
237 if (intf != null && target.equals(intf.getIpAddress())) {
238 //ARP request for one of our interfaces, we can reply straight away
239 sendArpReply(arp, sw.getId(), pi.getInPort(), routerMacAddress.toBytes());
240 return;
241 }
242 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200243
244 byte[] mac = lookupArpTable(arp.getTargetProtocolAddress());
245
246 if (mac == null){
247 //Mac address is not in our arp table.
248
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200249 //Record where the request came from so we know where to send the reply
Jonathan Hart2f790d22013-08-15 14:01:24 +1200250
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200251
Jonathan Hart9ea31212013-08-12 21:40:34 +1200252 //Should we just broadcast all received requests here? Or rate limit
253 //if we know we just sent an request?
254 arpRequests.put(target, new ArpRequest(
255 new HostArpRequester(this, arp, sw.getId(), pi.getInPort()), false));
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200256
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200257 //Flood the request out edge ports
Jonathan Hart2f790d22013-08-15 14:01:24 +1200258 //broadcastArpRequestOutEdge(pi.getPacketData(), sw.getId(), pi.getInPort());
259 sendArpRequestToSwitches(target, pi.getPacketData(), sw.getId(), pi.getInPort());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200260 }
261 else {
262 //We know the address, so send a reply
Jonathan Hart64c0b202013-08-20 15:45:07 +1200263 log.trace("Sending reply of {}", MACAddress.valueOf(mac).toString());
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200264 sendArpReply(arp, sw.getId(), pi.getInPort(), mac);
265 }
266 }
267
268 protected void handleArpReply(IOFSwitch sw, OFPacketIn pi, ARP arp){
Jonathan Hart64c0b202013-08-20 15:45:07 +1200269 log.trace("ARP reply recieved for {}, is {}, on {}/{}", new Object[] {
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200270 bytesToStringAddr(arp.getSenderProtocolAddress()),
271 HexString.toHexString(arp.getSenderHardwareAddress()),
272 HexString.toHexString(sw.getId()), pi.getInPort()});
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200273
274 updateArpTable(arp);
275
276 //See if anyone's waiting for this ARP reply
277 InetAddress addr;
278 try {
279 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
280 } catch (UnknownHostException e) {
281 return;
282 }
283
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200284 Set<ArpRequest> requests = arpRequests.get(addr);
285
286 //Synchronize on the Multimap while using an iterator for one of the sets
287 synchronized (arpRequests) {
288 Iterator<ArpRequest> it = requests.iterator();
289 while (it.hasNext()) {
290 ArpRequest request = it.next();
291 it.remove();
292 request.dispatchReply(addr, arp.getSenderHardwareAddress());
293 }
294 }
Jonathan Hart6261dcd2013-07-22 17:58:35 +1200295 }
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200296
297 private synchronized byte[] lookupArpTable(byte[] ipAddress){
298 InetAddress addr;
299 try {
300 addr = InetAddress.getByAddress(ipAddress);
301 } catch (UnknownHostException e) {
302 log.warn("Unable to create InetAddress", e);
303 return null;
304 }
305
306 ArpTableEntry arpEntry = arpTable.get(addr);
307
308 if (arpEntry == null){
Jonathan Hart2f790d22013-08-15 14:01:24 +1200309 //log.debug("MAC for {} unknown", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200310 return null;
311 }
312
313 if (System.currentTimeMillis() - arpEntry.getTimeLastSeen()
314 > ARP_ENTRY_TIMEOUT){
315 //Entry has timed out so we'll remove it and return null
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200316 log.debug("Timing out old ARP entry for {}", bytesToStringAddr(ipAddress));
Jonathan Hartc7ca35d2013-06-25 20:54:25 +1200317 arpTable.remove(addr);
318 return null;
319 }
320
321 return arpEntry.getMacAddress();
322 }
323
324 private synchronized void updateArpTable(ARP arp){
325 InetAddress addr;
326 try {
327 addr = InetAddress.getByAddress(arp.getSenderProtocolAddress());
328 } catch (UnknownHostException e) {
329 log.warn("Unable to create InetAddress", e);
330 return;
331 }
332
333 ArpTableEntry arpEntry = arpTable.get(addr);
334
335 if (arpEntry != null
336 && arpEntry.getMacAddress() == arp.getSenderHardwareAddress()){
337 arpEntry.setTimeLastSeen(System.currentTimeMillis());
338 }
339 else {
340 arpTable.put(addr,
341 new ArpTableEntry(arp.getSenderHardwareAddress(),
342 System.currentTimeMillis()));
343 }
344 }
345
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200346 private void sendArpRequestForAddress(InetAddress ipAddress) {
Jonathan Hart0ee0f022013-08-03 22:21:54 +1200347 //TODO what should the sender IP address be? Probably not 0.0.0.0
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200348 byte[] zeroIpv4 = {0x0, 0x0, 0x0, 0x0};
349 byte[] zeroMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jonathan Hart4dfc3652013-08-02 20:22:36 +1200350 byte[] bgpdMac = {0x0, 0x0, 0x0, 0x0, 0x0, 0x01};
351 byte[] broadcastMac = {(byte)0xff, (byte)0xff, (byte)0xff,
Jonathan Hartf0c0dcb2013-07-24 15:28:42 +1200352 (byte)0xff, (byte)0xff, (byte)0xff};
353
354 ARP arpRequest = new ARP();
355
356 arpRequest.setHardwareType(ARP.HW_TYPE_ETHERNET)
357 .setProtocolType(ARP.PROTO_TYPE_IP)
358 .setHardwareAddressLength((byte)Ethernet.DATALAYER_ADDRESS_LENGTH)
359 .setProtocolAddressLength((byte)4) //can't find the constant anywhere
360 .setOpCode(ARP.OP_REQUEST)
Jonathan Harte97fa642013-08-21 13:37:38 +1200361 //.setSenderHardwareAddress(bgpdMac)
362 .setSenderHardwareAddress(routerMacAddress.toBytes())
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) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200393 //log.debug("mode is l2");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200394 broadcastArpRequestOutEdge(arpRequest, inSwitch, inPort);
395 }
396 else if (mode == Mode.L3_MODE) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200397 //log.debug("mode is l3");
Jonathan Hart2f790d22013-08-15 14:01:24 +1200398 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) {
Jonathan Hart64c0b202013-08-20 15:45:07 +1200454 //log.debug("Sending ARP request out {}/{}", HexString.toHexString(dpid), port);
Jonathan Hart2f790d22013-08-15 14:01:24 +1200455
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 Hart64c0b202013-08-20 15:45:07 +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}