Merge "Do not catch Throwables, but rather Exceptions"
[controller.git] / opendaylight / arphandler / src / main / java / org / opendaylight / controller / arphandler / internal / ArpHandler.java
index 8c91c71533a19282c9f1c48e8697dd3f81d181ee..083e2ed0e9784d31355ceaac1a995bde2c386278 100644 (file)
@@ -113,16 +113,6 @@ public class ArpHandler implements IHostFinder, IListenDataPacket, ICacheUpdateA
         }
     }
 
-    void setRouting(IRouting r) {
-        this.routing = r;
-    }
-
-    void unsetRouting(IRouting r) {
-        if (this.routing == r) {
-            this.routing = null;
-        }
-    }
-
     void setHostListener(IfHostListener s) {
         if (this.hostListeners != null) {
             this.hostListeners.add(s);
@@ -172,6 +162,12 @@ public class ArpHandler implements IHostFinder, IListenDataPacket, ICacheUpdateA
         byte[] targetIP = tIP.getAddress();
         ARP arp = createARP(ARP.REPLY, sMAC, senderIP, tMAC, targetIP);
 
+        if(log.isTraceEnabled()) {
+            log.trace("Sending Arp Reply with srcMac {} - srcIp {} - dstMac {} - dstIp {} - outport {}",
+                    HexEncode.bytesToHexString(sMAC),
+                    sIP, HexEncode.bytesToHexString(tMAC), tIP, p);
+        }
+
         Ethernet ethernet = createEthernet(sMAC, tMAC, arp);
 
         RawPacket destPkt = this.dataPacketService.encodeDataPacket(ethernet);
@@ -180,8 +176,28 @@ public class ArpHandler implements IHostFinder, IListenDataPacket, ICacheUpdateA
         this.dataPacketService.transmitDataPacket(destPkt);
     }
 
+    private void logArpPacket(ARP pkt, NodeConnector p) {
+        try {
+            if (pkt.getOpCode() == ARP.REQUEST) {
+                log.trace("Received Arp Request with srcMac {} - srcIp {} - dstMac {} - dstIp {} - inport {}", HexEncode.bytesToHexString(pkt.getSenderHardwareAddress()),
+                        InetAddress.getByAddress(pkt.getSenderProtocolAddress()), HexEncode.bytesToHexString(pkt.getTargetHardwareAddress()),
+                        InetAddress.getByAddress(pkt.getTargetProtocolAddress()), p);
+            } else if(pkt.getOpCode() == ARP.REPLY) {
+                log.trace("Received Arp Reply with srcMac {} - srcIp {} - dstMac {} - dstIp {} - inport {}", HexEncode.bytesToHexString(pkt.getSenderHardwareAddress()),
+                        InetAddress.getByAddress(pkt.getSenderProtocolAddress()), HexEncode.bytesToHexString(pkt.getTargetHardwareAddress()),
+                        InetAddress.getByAddress(pkt.getTargetProtocolAddress()), p);
+            }
+        } catch(UnknownHostException e) {
+            log.warn("Illegal Ip Address in the ARP packet", e);
+        }
+    }
+
     protected void handleARPPacket(Ethernet eHeader, ARP pkt, NodeConnector p) {
 
+        if(log.isTraceEnabled()) {
+            logArpPacket(pkt, p);
+        }
+
         byte[] sourceMAC = eHeader.getSourceMACAddress();
         byte[] targetMAC = eHeader.getDestinationMACAddress();
         /*
@@ -357,6 +373,11 @@ public class ArpHandler implements IHostFinder, IListenDataPacket, ICacheUpdateA
             byte[] targetIPByte = targetIP.getAddress();
             ARP arp = createARP(ARP.REQUEST, getControllerMAC(), senderIP, targetHardwareAddress, targetIPByte);
 
+            if(log.isTraceEnabled()) {
+                log.trace("Sending Broadcast Arp Request with srcMac {} - srcIp {} - dstMac {} - dstIp {} - outport {}", HexEncode.bytesToHexString(getControllerMAC()),
+                        subnet.getNetworkAddress(), HexEncode.bytesToHexString(targetHardwareAddress), targetIP, p);
+            }
+
             byte[] destMACAddress = NetUtils.getBroadcastMACAddr();
             Ethernet ethernet = createEthernet(getControllerMAC(), destMACAddress, arp);
 
@@ -387,6 +408,13 @@ public class ArpHandler implements IHostFinder, IListenDataPacket, ICacheUpdateA
         byte[] targetMAC = host.getDataLayerAddressBytes();
         ARP arp = createARP(ARP.REQUEST, getControllerMAC(), senderIP, targetMAC, targetIP);
 
+        if(log.isTraceEnabled()) {
+            log.trace("Sending Unicast Arp Request with srcMac {} - srcIp {} - dstMac {} - dstIp {} - outport {}",
+                    HexEncode.bytesToHexString(getControllerMAC()),
+                    subnet.getNetworkAddress(), HexEncode.bytesToHexString(targetMAC), host.getNetworkAddress(),
+                    outPort);
+        }
+
         Ethernet ethernet = createEthernet(getControllerMAC(), targetMAC, arp);
 
         RawPacket destPkt = this.dataPacketService.encodeDataPacket(ethernet);
@@ -461,44 +489,26 @@ public class ArpHandler implements IHostFinder, IListenDataPacket, ICacheUpdateA
             log.debug("Can't find subnet matching {}, drop packet", dIP);
             return;
         }
+        // If packet is sent to the default gw (us), ignore it for now
+        if (subnet.getNetworkAddress().equals(dIP)) {
+            log.trace("Ignore IP packet destined to default gw");
+            return;
+        }
 
         // see if we know about the host
         // Hosttracker hosts db key implementation
-        IHostId id = HostIdFactory.create(dIP, null);
-        HostNodeConnector host = hostTracker.hostFind(id);
+        HostNodeConnector host = hostTracker.hostFind(dIP);
 
         if (host == null) {
-            // if we don't, know about the host, try to find it
+            // if we don't know about the host, try to find it
             log.trace("Punted IP pkt to {}, sending bcast ARP event...", dIP);
             /*
              * unknown destination host, initiate bcast ARP request
              */
             arpRequestReplyEvent.put(new ARPRequest(dIP, subnet), false);
 
-        } else if (routing == null || routing.getRoute(p.getNode(), host.getnodeconnectorNode()) != null) {
-            /*
-             * if IRouting is available, make sure that this packet can get it's
-             * destination normally before teleporting it there. If it's not
-             * available, then assume it's reachable.
-             *
-             * TODO: come up with a way to do this in the absence of IRouting
-             */
-
-            log.trace("forwarding punted IP pkt to {} received at {}", dIP, p);
-
-            /*
-             * if we know where the host is and there's a path from where this
-             * packet was punted to where the host is, then deliver it to the
-             * host for now
-             */
-            NodeConnector nc = host.getnodeConnector();
-
-            // re-encode the Ethernet packet (the parent of the IPv4 packet)
-            RawPacket rp = this.dataPacketService.encodeDataPacket(pkt.getParent());
-            rp.setOutgoingNodeConnector(nc);
-            this.dataPacketService.transmitDataPacket(rp);
         } else {
-            log.trace("ignoring punted IP pkt to {} because there is no route from {}", dIP, p);
+            log.trace("Ignoring punted IP pkt to known host: {} (received on: {})", dIP, p);
         }
     }