ArpHandler to ignore ip packet sent to default GW
[controller.git] / opendaylight / arphandler / src / main / java / org / opendaylight / controller / arphandler / internal / ArpHandler.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 /*
10  *
11  */
12 package org.opendaylight.controller.arphandler.internal;
13
14 import java.net.InetAddress;
15 import java.net.UnknownHostException;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.EnumSet;
19 import java.util.HashSet;
20 import java.util.Set;
21 import java.util.Timer;
22 import java.util.TimerTask;
23 import java.util.concurrent.BlockingQueue;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ConcurrentMap;
26 import java.util.concurrent.CopyOnWriteArraySet;
27 import java.util.concurrent.LinkedBlockingQueue;
28
29 import org.opendaylight.controller.arphandler.ARPCacheEvent;
30 import org.opendaylight.controller.arphandler.ARPEvent;
31 import org.opendaylight.controller.arphandler.ARPReply;
32 import org.opendaylight.controller.arphandler.ARPRequest;
33 import org.opendaylight.controller.clustering.services.CacheConfigException;
34 import org.opendaylight.controller.clustering.services.CacheExistException;
35 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
36 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
37 import org.opendaylight.controller.clustering.services.IClusterServices;
38 import org.opendaylight.controller.connectionmanager.IConnectionManager;
39 import org.opendaylight.controller.hosttracker.HostIdFactory;
40 import org.opendaylight.controller.hosttracker.IHostId;
41 import org.opendaylight.controller.hosttracker.IfHostListener;
42 import org.opendaylight.controller.hosttracker.IfIptoHost;
43 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
44 import org.opendaylight.controller.hosttracker.hostAware.IHostFinder;
45 import org.opendaylight.controller.sal.connection.ConnectionLocality;
46 import org.opendaylight.controller.sal.core.ConstructionException;
47 import org.opendaylight.controller.sal.core.Node;
48 import org.opendaylight.controller.sal.core.NodeConnector;
49 import org.opendaylight.controller.sal.packet.ARP;
50 import org.opendaylight.controller.sal.packet.Ethernet;
51 import org.opendaylight.controller.sal.packet.IDataPacketService;
52 import org.opendaylight.controller.sal.packet.IListenDataPacket;
53 import org.opendaylight.controller.sal.packet.IPv4;
54 import org.opendaylight.controller.sal.packet.Packet;
55 import org.opendaylight.controller.sal.packet.PacketResult;
56 import org.opendaylight.controller.sal.packet.RawPacket;
57 import org.opendaylight.controller.sal.routing.IRouting;
58 import org.opendaylight.controller.sal.utils.EtherTypes;
59 import org.opendaylight.controller.sal.utils.HexEncode;
60 import org.opendaylight.controller.sal.utils.NetUtils;
61 import org.opendaylight.controller.switchmanager.ISwitchManager;
62 import org.opendaylight.controller.switchmanager.Subnet;
63 import org.opendaylight.controller.topologymanager.ITopologyManager;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 public class ArpHandler implements IHostFinder, IListenDataPacket, ICacheUpdateAware<ARPEvent, Boolean> {
68     private static final Logger log = LoggerFactory.getLogger(ArpHandler.class);
69     static final String ARP_EVENT_CACHE_NAME = "arphandler.arpRequestReplyEvent";
70     private IfIptoHost hostTracker;
71     private ISwitchManager switchManager;
72     private ITopologyManager topologyManager;
73     private IDataPacketService dataPacketService;
74     private IRouting routing;
75     private IClusterContainerServices clusterContainerService;
76     private IConnectionManager connectionManager;
77     private Set<IfHostListener> hostListeners = new CopyOnWriteArraySet<IfHostListener>();
78     private ConcurrentMap<InetAddress, Set<HostNodeConnector>> arpRequestors;
79     private ConcurrentMap<InetAddress, Short> countDownTimers;
80     private Timer periodicTimer;
81     private BlockingQueue<ARPCacheEvent> ARPCacheEvents = new LinkedBlockingQueue<ARPCacheEvent>();
82     private Thread cacheEventHandler;
83     private boolean stopping = false;
84
85     /*
86      * A cluster allocated cache. Used for synchronizing ARP request/reply
87      * events across all cluster controllers. To raise an event, we put() a
88      * specific event object (as key) and all nodes handle it in the
89      * entryUpdated callback.
90      *
91      * In case of ARPReply, we put true value to send replies to any requestors
92      * by calling generateAndSendReply
93      */
94     private ConcurrentMap<ARPEvent, Boolean> arpRequestReplyEvent;
95
96     void setConnectionManager(IConnectionManager cm) {
97         this.connectionManager = cm;
98     }
99
100     void unsetConnectionManager(IConnectionManager cm) {
101         if (this.connectionManager == cm) {
102             connectionManager = null;
103         }
104     }
105
106     void setClusterContainerService(IClusterContainerServices s) {
107         this.clusterContainerService = s;
108     }
109
110     void unsetClusterContainerService(IClusterContainerServices s) {
111         if (this.clusterContainerService == s) {
112             this.clusterContainerService = null;
113         }
114     }
115
116     void setRouting(IRouting r) {
117         this.routing = r;
118     }
119
120     void unsetRouting(IRouting r) {
121         if (this.routing == r) {
122             this.routing = null;
123         }
124     }
125
126     void setHostListener(IfHostListener s) {
127         if (this.hostListeners != null) {
128             this.hostListeners.add(s);
129         }
130     }
131
132     void unsetHostListener(IfHostListener s) {
133         if (this.hostListeners != null) {
134             this.hostListeners.remove(s);
135         }
136     }
137
138     void setDataPacketService(IDataPacketService s) {
139         this.dataPacketService = s;
140     }
141
142     void unsetDataPacketService(IDataPacketService s) {
143         if (this.dataPacketService == s) {
144             this.dataPacketService = null;
145         }
146     }
147
148     public void setHostTracker(IfIptoHost hostTracker) {
149         log.debug("Setting HostTracker");
150         this.hostTracker = hostTracker;
151     }
152
153     public void unsetHostTracker(IfIptoHost s) {
154         log.debug("UNSetting HostTracker");
155         if (this.hostTracker == s) {
156             this.hostTracker = null;
157         }
158     }
159
160     public void setTopologyManager(ITopologyManager tm) {
161         this.topologyManager = tm;
162     }
163
164     public void unsetTopologyManager(ITopologyManager tm) {
165         if (this.topologyManager == tm) {
166             this.topologyManager = null;
167         }
168     }
169
170     protected void sendARPReply(NodeConnector p, byte[] sMAC, InetAddress sIP, byte[] tMAC, InetAddress tIP) {
171         byte[] senderIP = sIP.getAddress();
172         byte[] targetIP = tIP.getAddress();
173         ARP arp = createARP(ARP.REPLY, sMAC, senderIP, tMAC, targetIP);
174
175         Ethernet ethernet = createEthernet(sMAC, tMAC, arp);
176
177         RawPacket destPkt = this.dataPacketService.encodeDataPacket(ethernet);
178         destPkt.setOutgoingNodeConnector(p);
179
180         this.dataPacketService.transmitDataPacket(destPkt);
181     }
182
183     protected void handleARPPacket(Ethernet eHeader, ARP pkt, NodeConnector p) {
184
185         byte[] sourceMAC = eHeader.getSourceMACAddress();
186         byte[] targetMAC = eHeader.getDestinationMACAddress();
187         /*
188          * Sanity Check; drop ARP packets originated by the controller itself.
189          * This is to avoid continuous flooding
190          */
191         if (Arrays.equals(sourceMAC, getControllerMAC())) {
192             if (log.isDebugEnabled()) {
193                 log.debug("Receive a self originated ARP pkt (srcMAC {}) --> DROP",
194                         HexEncode.bytesToHexString(sourceMAC));
195             }
196             return;
197         }
198
199         InetAddress targetIP, sourceIP;
200         try {
201             targetIP = InetAddress.getByAddress(pkt.getTargetProtocolAddress());
202             sourceIP = InetAddress.getByAddress(pkt.getSenderProtocolAddress());
203         } catch (UnknownHostException e1) {
204             log.debug("Invalid host in ARP packet: {}", e1.getMessage());
205             return;
206         }
207
208         Subnet subnet = null;
209         if (switchManager != null) {
210             subnet = switchManager.getSubnetByNetworkAddress(sourceIP);
211         }
212         if (subnet == null) {
213             log.debug("ARPHandler: can't find subnet matching {}, drop packet", sourceIP);
214             return;
215         }
216
217         // Make sure that the host is a legitimate member of this subnet
218         if (!subnet.hasNodeConnector(p)) {
219             log.debug("{} showing up on {} does not belong to {}", new Object[] { sourceIP, p, subnet });
220             return;
221         }
222
223         HostNodeConnector requestor = null;
224         if (NetUtils.isUnicastMACAddr(sourceMAC) && p.getNode() != null) {
225             try {
226                 requestor = new HostNodeConnector(sourceMAC, sourceIP, p, subnet.getVlan());
227             } catch (ConstructionException e) {
228                 log.debug("Received ARP packet with invalid MAC: {}", HexEncode.bytesToHexString(sourceMAC));
229                 return;
230             }
231             /*
232              * Learn host from the received ARP REQ/REPLY, inform Host Tracker
233              */
234             log.trace("Inform Host tracker of new host {}", requestor.getNetworkAddress());
235             for (IfHostListener listener : this.hostListeners) {
236                 listener.hostListener(requestor);
237             }
238         }
239
240         /*
241          * OpCode != request -> ARP Reply. If there are hosts (in arpRequestors)
242          * waiting for the ARP reply for this sourceIP, it's time to generate
243          * the reply and send it to these hosts.
244          *
245          * If sourceIP==targetIP, it is a Gratuitous ARP. If there are hosts (in
246          * arpRequestors) waiting for the ARP reply for this sourceIP, it's time
247          * to generate the reply and send it to these hosts
248          */
249
250         if (pkt.getOpCode() != ARP.REQUEST || sourceIP.equals(targetIP)) {
251             // Raise a reply event so that any waiting requestors will be sent a
252             // reply
253             // the true value indicates we should generate replies to requestors
254             // across the cluster
255             log.trace("Received ARP reply packet from {}, reply to all requestors.", sourceIP);
256             arpRequestReplyEvent.put(new ARPReply(sourceIP, sourceMAC), true);
257             return;
258         }
259
260         /*
261          * ARP Request Handling: If targetIP is the IP of the subnet, reply with
262          * ARP REPLY If targetIP is a known host, PROXY ARP (by sending ARP
263          * REPLY) on behalf of known target hosts. For unknown target hosts,
264          * generate and send an ARP request to ALL switches/ports using the IP
265          * address defined in the subnet as source address
266          */
267         /*
268          * If target IP is gateway IP, Send ARP reply
269          */
270         if ((targetIP.equals(subnet.getNetworkAddress()))
271                 && (NetUtils.isBroadcastMACAddr(targetMAC) || Arrays.equals(targetMAC, getControllerMAC()))) {
272             if (connectionManager.getLocalityStatus(p.getNode()) == ConnectionLocality.LOCAL) {
273                 if (log.isTraceEnabled()) {
274                     log.trace("Received local ARP req. for default gateway. Replying with controller MAC: {}",
275                             HexEncode.bytesToHexString(getControllerMAC()));
276                 }
277                 sendARPReply(p, getControllerMAC(), targetIP, pkt.getSenderHardwareAddress(), sourceIP);
278             } else {
279                 log.trace("Received non-local ARP req. for default gateway. Raising reply event");
280                 arpRequestReplyEvent.put(
281                         new ARPReply(p, targetIP, getControllerMAC(), sourceIP, pkt.getSenderHardwareAddress()), false);
282             }
283             return;
284         }
285
286         // Hosttracker hosts db key implementation
287         IHostId id = HostIdFactory.create(targetIP, null);
288         HostNodeConnector host = hostTracker.hostQuery(id);
289         // unknown host, initiate ARP request
290         if (host == null) {
291             // add the requestor to the list so that we can replay the reply
292             // when the host responds
293             if (requestor != null) {
294                 Set<HostNodeConnector> requestorSet = arpRequestors.get(targetIP);
295                 if (requestorSet == null) {
296                     requestorSet = Collections.newSetFromMap(new ConcurrentHashMap<HostNodeConnector, Boolean>());
297                     arpRequestors.put(targetIP, requestorSet);
298                 }
299                 requestorSet.add(requestor);
300                 countDownTimers.put(targetIP, (short) 2); // reset timeout to
301                                                           // 2sec
302             }
303             // Raise a bcast request event, all controllers need to send one
304             log.trace("Sending a bcast ARP request for {}", targetIP);
305             arpRequestReplyEvent.put(new ARPRequest(targetIP, subnet), false);
306         } else {
307             /*
308              * Target host known (across the cluster), send ARP REPLY make sure
309              * that targetMAC matches the host's MAC if it is not broadcastMAC
310              */
311             if (NetUtils.isBroadcastMACAddr(targetMAC) || Arrays.equals(host.getDataLayerAddressBytes(), targetMAC)) {
312                 log.trace("Received ARP req. for known host {}, sending reply...", targetIP);
313                 if (connectionManager.getLocalityStatus(p.getNode()) == ConnectionLocality.LOCAL) {
314                     sendARPReply(p, host.getDataLayerAddressBytes(), host.getNetworkAddress(),
315                             pkt.getSenderHardwareAddress(), sourceIP);
316                 } else {
317                     arpRequestReplyEvent.put(new ARPReply(p, host.getNetworkAddress(), host.getDataLayerAddressBytes(),
318                             sourceIP, pkt.getSenderHardwareAddress()), false);
319                 }
320             } else {
321                 /*
322                  * Target MAC has been changed. For now, discard it. TODO: We
323                  * may need to send unicast ARP REQUEST on behalf of the target
324                  * back to the sender to trigger the sender to update its table
325                  */
326             }
327         }
328     }
329
330     /**
331      * Send a broadcast ARP Request to the switch/ ports using the
332      * networkAddress of the subnet as sender IP the controller's MAC as sender
333      * MAC the targetIP as the target Network Address
334      */
335     protected void sendBcastARPRequest(InetAddress targetIP, Subnet subnet) {
336         log.trace("sendBcatARPRequest targetIP:{} subnet:{}", targetIP, subnet);
337         Set<NodeConnector> nodeConnectors;
338         if (subnet.isFlatLayer2()) {
339             nodeConnectors = new HashSet<NodeConnector>();
340             for (Node n : this.switchManager.getNodes()) {
341                 nodeConnectors.addAll(this.switchManager.getUpNodeConnectors(n));
342             }
343         } else {
344             nodeConnectors = subnet.getNodeConnectors();
345         }
346         byte[] targetHardwareAddress = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
347
348         // TODO: should use IBroadcastHandler instead
349         for (NodeConnector p : nodeConnectors) {
350             // filter out any non-local or internal ports
351             if (!(connectionManager.getLocalityStatus(p.getNode()) == ConnectionLocality.LOCAL)
352                     || topologyManager.isInternal(p)) {
353                 continue;
354             }
355             log.trace("Sending toward nodeConnector:{}", p);
356             byte[] senderIP = subnet.getNetworkAddress().getAddress();
357             byte[] targetIPByte = targetIP.getAddress();
358             ARP arp = createARP(ARP.REQUEST, getControllerMAC(), senderIP, targetHardwareAddress, targetIPByte);
359
360             byte[] destMACAddress = NetUtils.getBroadcastMACAddr();
361             Ethernet ethernet = createEthernet(getControllerMAC(), destMACAddress, arp);
362
363             // TODO For now send port-by-port, see how to optimize to
364             // send to multiple ports at once
365             RawPacket destPkt = this.dataPacketService.encodeDataPacket(ethernet);
366             destPkt.setOutgoingNodeConnector(p);
367
368             this.dataPacketService.transmitDataPacket(destPkt);
369         }
370     }
371
372     /**
373      * Send a unicast ARP Request to the known host on a specific switch/port as
374      * defined in the host. The sender IP is the networkAddress of the subnet
375      * The sender MAC is the controller's MAC
376      */
377     protected void sendUcastARPRequest(HostNodeConnector host, Subnet subnet) {
378         log.trace("sendUcastARPRequest host:{} subnet:{}", host, subnet);
379         NodeConnector outPort = host.getnodeConnector();
380         if (outPort == null) {
381             log.error("Failed sending UcastARP because cannot extract output port from Host: {}", host);
382             return;
383         }
384
385         byte[] senderIP = subnet.getNetworkAddress().getAddress();
386         byte[] targetIP = host.getNetworkAddress().getAddress();
387         byte[] targetMAC = host.getDataLayerAddressBytes();
388         ARP arp = createARP(ARP.REQUEST, getControllerMAC(), senderIP, targetMAC, targetIP);
389
390         Ethernet ethernet = createEthernet(getControllerMAC(), targetMAC, arp);
391
392         RawPacket destPkt = this.dataPacketService.encodeDataPacket(ethernet);
393         destPkt.setOutgoingNodeConnector(outPort);
394
395         this.dataPacketService.transmitDataPacket(destPkt);
396     }
397
398     @Override
399     public void find(InetAddress networkAddress) {
400         log.trace("Received find IP {}", networkAddress);
401
402         Subnet subnet = null;
403         if (switchManager != null) {
404             subnet = switchManager.getSubnetByNetworkAddress(networkAddress);
405         }
406         if (subnet == null) {
407             log.debug("Can't find subnet matching IP {}", networkAddress);
408             return;
409         }
410
411         // send a broadcast ARP Request to this IP
412         arpRequestReplyEvent.put(new ARPRequest(networkAddress, subnet), false);
413     }
414
415     /*
416      * Probe the host by sending a unicast ARP Request to the host
417      */
418     @Override
419     public void probe(HostNodeConnector host) {
420         log.trace("Received probe host {}", host);
421
422         Subnet subnet = null;
423         if (switchManager != null) {
424             subnet = switchManager.getSubnetByNetworkAddress(host.getNetworkAddress());
425         }
426         if (subnet == null) {
427             log.debug("can't find subnet matching {}", host.getNetworkAddress());
428             return;
429         }
430
431         if (connectionManager.getLocalityStatus(host.getnodeconnectorNode()) == ConnectionLocality.LOCAL) {
432             log.trace("Send a ucast ARP req. to: {}", host);
433             sendUcastARPRequest(host, subnet);
434         } else {
435             log.trace("Raise a ucast ARP req. event to: {}", host);
436             arpRequestReplyEvent.put(new ARPRequest(host, subnet), false);
437         }
438     }
439
440     /**
441      * An IP packet is punted to the controller, this means that the destination
442      * host is not known to the controller. Need to discover it by sending a
443      * Broadcast ARP Request
444      *
445      * @param pkt
446      * @param p
447      */
448     protected void handlePuntedIPPacket(IPv4 pkt, NodeConnector p) {
449
450         InetAddress dIP = NetUtils.getInetAddress(pkt.getDestinationAddress());
451         if (dIP == null) {
452             return;
453         }
454
455         // try to find a matching subnet
456         Subnet subnet = null;
457         if (switchManager != null) {
458             subnet = switchManager.getSubnetByNetworkAddress(dIP);
459         }
460         if (subnet == null) {
461             log.debug("Can't find subnet matching {}, drop packet", dIP);
462             return;
463         }
464         // If packet is sent to the default gw (us), ignore it for now
465         if (subnet.getNetworkAddress().equals(dIP)) {
466             log.trace("Ignore IP packet destined to default gw");
467             return;
468         }
469
470         // see if we know about the host
471         // Hosttracker hosts db key implementation
472         IHostId id = HostIdFactory.create(dIP, null);
473         HostNodeConnector host = hostTracker.hostFind(id);
474
475         if (host == null) {
476             // if we don't, know about the host, try to find it
477             log.trace("Punted IP pkt to {}, sending bcast ARP event...", dIP);
478             /*
479              * unknown destination host, initiate bcast ARP request
480              */
481             arpRequestReplyEvent.put(new ARPRequest(dIP, subnet), false);
482
483         } else if (routing == null || routing.getRoute(p.getNode(), host.getnodeconnectorNode()) != null) {
484             /*
485              * if IRouting is available, make sure that this packet can get it's
486              * destination normally before teleporting it there. If it's not
487              * available, then assume it's reachable.
488              *
489              * TODO: come up with a way to do this in the absence of IRouting
490              */
491
492             log.trace("forwarding punted IP pkt to {} received at {}", dIP, p);
493
494             /*
495              * if we know where the host is and there's a path from where this
496              * packet was punted to where the host is, then deliver it to the
497              * host for now
498              */
499             NodeConnector nc = host.getnodeConnector();
500
501             // re-encode the Ethernet packet (the parent of the IPv4 packet)
502             RawPacket rp = this.dataPacketService.encodeDataPacket(pkt.getParent());
503             rp.setOutgoingNodeConnector(nc);
504             this.dataPacketService.transmitDataPacket(rp);
505         } else {
506             log.trace("ignoring punted IP pkt to {} because there is no route from {}", dIP, p);
507         }
508     }
509
510     public byte[] getControllerMAC() {
511         if (switchManager == null) {
512             return null;
513         }
514         return switchManager.getControllerMAC();
515     }
516
517     /**
518      * Function called by the dependency manager when all the required
519      * dependencies are satisfied
520      *
521      */
522     void init() {
523         arpRequestors = new ConcurrentHashMap<InetAddress, Set<HostNodeConnector>>();
524         countDownTimers = new ConcurrentHashMap<InetAddress, Short>();
525         cacheEventHandler = new Thread(new ARPCacheEventHandler(), "ARPCacheEventHandler Thread");
526
527         allocateCaches();
528         retrieveCaches();
529
530     }
531
532     @SuppressWarnings({ "unchecked" })
533     private void retrieveCaches() {
534         ConcurrentMap<?, ?> map;
535
536         if (this.clusterContainerService == null) {
537             log.error("Cluster service unavailable, can't retieve ARPHandler caches!");
538             return;
539         }
540
541         map = clusterContainerService.getCache(ARP_EVENT_CACHE_NAME);
542         if (map != null) {
543             this.arpRequestReplyEvent = (ConcurrentMap<ARPEvent, Boolean>) map;
544         } else {
545             log.error("Cache allocation failed for {}", ARP_EVENT_CACHE_NAME);
546         }
547     }
548
549     private void allocateCaches() {
550         if (clusterContainerService == null) {
551             nonClusterObjectCreate();
552             log.error("Clustering service unavailable. Allocated non-cluster caches for ARPHandler.");
553             return;
554         }
555
556         try {
557             clusterContainerService.createCache(ARP_EVENT_CACHE_NAME,
558                     EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
559         } catch (CacheConfigException e) {
560             log.error("ARPHandler cache configuration invalid!");
561         } catch (CacheExistException e) {
562             log.debug("ARPHandler cache exists, skipped allocation.");
563         }
564
565     }
566
567     private void nonClusterObjectCreate() {
568         arpRequestReplyEvent = new ConcurrentHashMap<ARPEvent, Boolean>();
569     }
570
571     /**
572      * Function called by the dependency manager when at least one dependency
573      * become unsatisfied or when the component is shutting down because for
574      * example bundle is being stopped.
575      *
576      */
577     void destroy() {
578         cacheEventHandler.interrupt();
579     }
580
581     /**
582      * Function called by dependency manager after "init ()" is called and after
583      * the services provided by the class are registered in the service registry
584      *
585      */
586     void start() {
587         stopping = false;
588         startPeriodicTimer();
589         cacheEventHandler.start();
590     }
591
592     /**
593      * Function called by the dependency manager before the services exported by
594      * the component are unregistered, this will be followed by a "destroy ()"
595      * calls
596      *
597      */
598     void stop() {
599     }
600
601     void stopping() {
602         stopping = true;
603         cancelPeriodicTimer();
604     }
605
606     void setSwitchManager(ISwitchManager s) {
607         log.debug("SwitchManager service set.");
608         this.switchManager = s;
609     }
610
611     void unsetSwitchManager(ISwitchManager s) {
612         if (this.switchManager == s) {
613             log.debug("SwitchManager service UNset.");
614             this.switchManager = null;
615         }
616     }
617
618     @Override
619     public PacketResult receiveDataPacket(RawPacket inPkt) {
620         if (inPkt == null) {
621             return PacketResult.IGNORED;
622         }
623         log.trace("Received a frame of size: {}", inPkt.getPacketData().length);
624         Packet formattedPak = this.dataPacketService.decodeDataPacket(inPkt);
625         if (formattedPak instanceof Ethernet) {
626             Object nextPak = formattedPak.getPayload();
627             if (nextPak instanceof IPv4) {
628                 log.trace("Handle IP packet: {}", formattedPak);
629                 handlePuntedIPPacket((IPv4) nextPak, inPkt.getIncomingNodeConnector());
630             } else if (nextPak instanceof ARP) {
631                 log.trace("Handle ARP packet: {}", formattedPak);
632                 handleARPPacket((Ethernet) formattedPak, (ARP) nextPak, inPkt.getIncomingNodeConnector());
633             }
634         }
635         return PacketResult.IGNORED;
636     }
637
638     private ARP createARP(short opCode, byte[] senderMacAddress, byte[] senderIP, byte[] targetMacAddress,
639             byte[] targetIP) {
640         ARP arp = new ARP();
641         arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
642         arp.setProtocolType(EtherTypes.IPv4.shortValue());
643         arp.setHardwareAddressLength((byte) 6);
644         arp.setProtocolAddressLength((byte) 4);
645         arp.setOpCode(opCode);
646         arp.setSenderHardwareAddress(senderMacAddress);
647         arp.setSenderProtocolAddress(senderIP);
648         arp.setTargetHardwareAddress(targetMacAddress);
649         arp.setTargetProtocolAddress(targetIP);
650         return arp;
651     }
652
653     private Ethernet createEthernet(byte[] sourceMAC, byte[] targetMAC, ARP arp) {
654         Ethernet ethernet = new Ethernet();
655         ethernet.setSourceMACAddress(sourceMAC);
656         ethernet.setDestinationMACAddress(targetMAC);
657         ethernet.setEtherType(EtherTypes.ARP.shortValue());
658         ethernet.setPayload(arp);
659         return ethernet;
660     }
661
662     private void startPeriodicTimer() {
663         this.periodicTimer = new Timer("ArpHandler Periodic Timer");
664         this.periodicTimer.scheduleAtFixedRate(new TimerTask() {
665             @Override
666             public void run() {
667                 Set<InetAddress> targetIPs = countDownTimers.keySet();
668                 Set<InetAddress> expiredTargets = new HashSet<InetAddress>();
669                 for (InetAddress t : targetIPs) {
670                     short tick = countDownTimers.get(t);
671                     tick--;
672                     if (tick <= 0) {
673                         expiredTargets.add(t);
674                     } else {
675                         countDownTimers.replace(t, tick);
676                     }
677                 }
678                 for (InetAddress tIP : expiredTargets) {
679                     countDownTimers.remove(tIP);
680                     // Remove the requestor(s) who have been waiting for the ARP
681                     // reply from this target for more than 1sec
682                     arpRequestors.remove(tIP);
683                     log.debug("ARP reply was not received from {}", tIP);
684                 }
685
686                 // Clean up ARP event cache
687                 try {
688                     if (clusterContainerService.amICoordinator() && !arpRequestReplyEvent.isEmpty()) {
689                         arpRequestReplyEvent.clear();
690                     }
691                 } catch (Exception e) {
692                     log.warn("ARPHandler: A cluster member failed to clear event cache.");
693                 }
694             }
695         }, 0, 1000);
696     }
697
698     private void cancelPeriodicTimer() {
699         if (this.periodicTimer != null) {
700             this.periodicTimer.cancel();
701         }
702     }
703
704     private void generateAndSendReply(InetAddress sourceIP, byte[] sourceMAC) {
705         if (log.isTraceEnabled()) {
706             log.trace("generateAndSendReply called with params sourceIP:{} sourceMAC:{}", sourceIP,
707                     HexEncode.bytesToHexString(sourceMAC));
708         }
709         Set<HostNodeConnector> hosts = arpRequestors.remove(sourceIP);
710         if ((hosts == null) || hosts.isEmpty()) {
711             log.trace("Bailing out no requestors Hosts");
712             return;
713         }
714         countDownTimers.remove(sourceIP);
715         for (HostNodeConnector host : hosts) {
716             if (log.isTraceEnabled()) {
717                 log.trace(
718                         "Sending ARP Reply with src {}/{}, target {}/{}",
719                         new Object[] { HexEncode.bytesToHexString(sourceMAC), sourceIP,
720                                 HexEncode.bytesToHexString(host.getDataLayerAddressBytes()), host.getNetworkAddress() });
721             }
722             if (connectionManager.getLocalityStatus(host.getnodeconnectorNode()) == ConnectionLocality.LOCAL) {
723                 sendARPReply(host.getnodeConnector(), sourceMAC, sourceIP, host.getDataLayerAddressBytes(),
724                         host.getNetworkAddress());
725             } else {
726                 /*
727                  * In the remote event a requestor moved to another controller
728                  * it may turn out it now we need to send the ARP reply from a
729                  * different controller, this cover the case
730                  */
731                 arpRequestReplyEvent.put(
732                         new ARPReply(host.getnodeConnector(), sourceIP, sourceMAC, host.getNetworkAddress(), host
733                                 .getDataLayerAddressBytes()), false);
734             }
735         }
736     }
737
738     @Override
739     public void entryUpdated(ARPEvent key, Boolean new_value, String cacheName, boolean originLocal) {
740         log.trace("Got and entryUpdated for cacheName {} key {} isNew {}", cacheName, key, new_value);
741         enqueueARPCacheEvent(key, new_value);
742     }
743
744     @Override
745     public void entryCreated(ARPEvent key, String cacheName, boolean originLocal) {
746         // nothing to do
747     }
748
749     @Override
750     public void entryDeleted(ARPEvent key, String cacheName, boolean originLocal) {
751         // nothing to do
752     }
753
754     private void enqueueARPCacheEvent(ARPEvent event, boolean new_value) {
755         try {
756             ARPCacheEvent cacheEvent = new ARPCacheEvent(event, new_value);
757             if (!ARPCacheEvents.contains(cacheEvent)) {
758                 this.ARPCacheEvents.add(cacheEvent);
759                 log.trace("Enqueued {}", event);
760             }
761         } catch (Exception e) {
762             log.debug("enqueueARPCacheEvent caught Interrupt Exception for event {}", event);
763         }
764     }
765
766     /*
767      * this thread monitors the connectionEvent queue for new incoming events
768      * from
769      */
770     private class ARPCacheEventHandler implements Runnable {
771         @Override
772         public void run() {
773             while (!stopping) {
774                 try {
775                     ARPCacheEvent ev = ARPCacheEvents.take();
776                     ARPEvent event = ev.getEvent();
777                     if (event instanceof ARPRequest) {
778                         ARPRequest req = (ARPRequest) event;
779                         // If broadcast request
780                         if (req.getHost() == null) {
781                             log.trace("Trigger and ARP Broadcast Request upon receipt of {}", req);
782                             sendBcastARPRequest(req.getTargetIP(), req.getSubnet());
783
784                             // If unicast and local, send reply
785                         } else if (connectionManager.getLocalityStatus(req.getHost().getnodeconnectorNode()) == ConnectionLocality.LOCAL) {
786                             log.trace("ARPCacheEventHandler - sendUcatARPRequest upon receipt of {}", req);
787                             sendUcastARPRequest(req.getHost(), req.getSubnet());
788                         }
789                     } else if (event instanceof ARPReply) {
790                         ARPReply rep = (ARPReply) event;
791                         // New reply received by controller, notify all awaiting
792                         // requestors across the cluster
793                         if (ev.isNewReply()) {
794                             log.trace("Trigger a generateAndSendReply in response to {}", rep);
795                             generateAndSendReply(rep.getTargetIP(), rep.getTargetMac());
796                             // Otherwise, a specific reply. If local, send out.
797                         } else if (connectionManager.getLocalityStatus(rep.getPort().getNode()) == ConnectionLocality.LOCAL) {
798                             log.trace("ARPCacheEventHandler - sendUcatARPReply locally in response to {}", rep);
799                             sendARPReply(rep.getPort(), rep.getSourceMac(), rep.getSourceIP(), rep.getTargetMac(),
800                                     rep.getTargetIP());
801                         }
802                     }
803                 } catch (InterruptedException e) {
804                     ARPCacheEvents.clear();
805                     return;
806                 }
807             }
808         }
809     }
810 }