Jonathan Hart | 0961fe8 | 2014-04-03 09:56:25 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.proxyarp; |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 2 | |
| 3 | import java.net.InetAddress; |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 4 | import java.util.ArrayList; |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 5 | import java.util.List; |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 6 | import java.util.Map; |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 7 | import java.util.Map.Entry; |
| 8 | import java.util.concurrent.ConcurrentHashMap; |
| 9 | import java.util.concurrent.ConcurrentMap; |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 10 | |
| 11 | import net.floodlightcontroller.util.MACAddress; |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.datastore.KVArpCache; |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 13 | |
| 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| 16 | |
| 17 | /** |
| 18 | * Implements a basic ARP cache which maps IPv4 addresses to MAC addresses. |
| 19 | * Mappings time out after a short period of time (currently 1 min). We don't |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 20 | * try and refresh the mapping before the entry times out because as a |
| 21 | * controller we don't know if the mapping is still needed. |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 22 | */ |
Jonathan Hart | 5afde49 | 2013-10-01 12:30:53 +1300 | [diff] [blame] | 23 | class ArpCache { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 24 | private static final Logger log = LoggerFactory.getLogger(ArpCache.class); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 25 | private static long arpEntryTimeoutConfig = 60000; // ms (1 min) |
| 26 | private final KVArpCache kvArpCache; |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 27 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 28 | // Protected by locking on the ArpCache object (this) |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 29 | private final ConcurrentMap<InetAddress, ArpCacheEntry> arpCache; |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 30 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Represents a MAC address entry with a timestamp in the ARP cache. |
| 33 | * ARP cache entries are considered invalid if their timestamp is older |
| 34 | * than a timeout value. |
| 35 | */ |
| 36 | private static class ArpCacheEntry { |
| 37 | private final MACAddress macAddress; |
| 38 | private long timeLastSeen; |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 39 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Class constructor, specifying the MAC address for the entry. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | * |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 43 | * @param macAddress MAC address for the entry |
| 44 | */ |
| 45 | public ArpCacheEntry(MACAddress macAddress) { |
| 46 | this.macAddress = macAddress; |
| 47 | this.timeLastSeen = System.currentTimeMillis(); |
| 48 | } |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 49 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Returns the MAC address this entry represents. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | * |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 53 | * @return this entry's MAC address |
| 54 | */ |
| 55 | public MACAddress getMacAddress() { |
| 56 | return macAddress; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Update the timestamp for this entry. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | * |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 62 | * @param time the new timestamp to update the entry with |
| 63 | */ |
| 64 | public void setTimeLastSeen(long time) { |
| 65 | timeLastSeen = time; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns whether the entry has timed out or not. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | * |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 71 | * @return true if the entry has timed out. |
| 72 | */ |
| 73 | public boolean isExpired() { |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 74 | return System.currentTimeMillis() - timeLastSeen > arpEntryTimeoutConfig; |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 79 | * Class constructors. |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 80 | */ |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 81 | public ArpCache() { |
| 82 | arpCache = new ConcurrentHashMap<InetAddress, ArpCacheEntry>(); |
| 83 | kvArpCache = new KVArpCache(); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 84 | } |
| 85 | |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 86 | public void setArpEntryTimeoutConfig(long arpEntryTimeout) { |
| 87 | if (arpEntryTimeout <= 0) { |
| 88 | throw new IllegalArgumentException(Long.toString(arpEntryTimeout)); |
| 89 | } |
| 90 | arpEntryTimeoutConfig(arpEntryTimeout); |
| 91 | } |
| 92 | |
| 93 | private static void arpEntryTimeoutConfig(long arpEntryTimeout) { |
| 94 | ArpCache.arpEntryTimeoutConfig = arpEntryTimeout; |
| 95 | log.debug("Set arpEntryTimeoutConfig {}", ArpCache.arpEntryTimeoutConfig); |
| 96 | } |
| 97 | |
TeruU | 8b2d167 | 2014-04-25 17:02:56 -0700 | [diff] [blame] | 98 | public long getArpEntryTimeout() { |
| 99 | return ArpCache.arpEntryTimeoutConfig; |
| 100 | } |
| 101 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Get the MAC address that is mapped to an IP address in the ARP cache. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | * |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 105 | * @param ipAddress the IP address to look up |
| 106 | * @return the MAC address if found in the cache, null if not |
| 107 | */ |
| 108 | synchronized MACAddress lookup(InetAddress ipAddress) { |
| 109 | ArpCacheEntry arpEntry = arpCache.get(ipAddress); |
| 110 | |
| 111 | if (arpEntry == null) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | if (arpEntry.isExpired()) { |
| 116 | // Entry has timed out so we'll remove it and return null |
| 117 | log.trace("Removing expired ARP entry for {}", |
| 118 | ipAddress.getHostAddress()); |
| 119 | |
| 120 | arpCache.remove(ipAddress); |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | return arpEntry.getMacAddress(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Update an entry in the ARP cache. If the IP to MAC mapping is already |
| 129 | * in the cache, its timestamp will be updated. If not, the entry will |
| 130 | * be added with a new timestamp of the current time. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 131 | * |
| 132 | * @param ipAddress the IP address that will be mapped in the cache |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 133 | * @param macAddress the MAC address that maps to {@code ipAddress} |
| 134 | */ |
| 135 | synchronized void update(InetAddress ipAddress, MACAddress macAddress) { |
| 136 | ArpCacheEntry arpEntry = arpCache.get(ipAddress); |
| 137 | |
| 138 | if (arpEntry != null && arpEntry.getMacAddress().equals(macAddress)) { |
| 139 | arpEntry.setTimeLastSeen(System.currentTimeMillis()); |
Jonathan Hart | c00f5c2 | 2014-06-10 15:14:40 -0700 | [diff] [blame] | 140 | log.debug("The same ArpCache, ip {}, mac {}. " + |
| 141 | "Update local cache last seen time only.", ipAddress, macAddress); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 142 | } else { |
| 143 | arpCache.put(ipAddress, new ArpCacheEntry(macAddress)); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 144 | kvArpCache.forceCreate(ipAddress, macAddress.toBytes()); |
| 145 | log.debug("Create/Update ip {}, mac {} in ArpCache.", ipAddress, macAddress); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Remove an entry in the ARP cache. |
| 151 | * |
| 152 | * @param ipAddress the IP address that will be mapped in the cache |
| 153 | */ |
| 154 | synchronized void remove(InetAddress ipAddress) { |
| 155 | ArpCacheEntry entry = arpCache.remove(ipAddress); |
| 156 | |
| 157 | if (entry == null) { |
| 158 | log.debug("ArpCache doesn't have the ip key {}.", ipAddress); |
| 159 | } else { |
| 160 | kvArpCache.forceDelete(ipAddress); |
| 161 | log.debug("Remove it in ArpCache and DB, ip {}", ipAddress); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Retrieve a list of all mappings in the ARP cache. |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 167 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 168 | * @return list of all ARP mappings, formatted as a human-readable string |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 169 | */ |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 170 | List<String> getMappings() { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 171 | List<String> result = new ArrayList<String>(arpCache.size()); |
| 172 | |
| 173 | for (Map.Entry<InetAddress, ArpCacheEntry> entry : arpCache.entrySet()) { |
| 174 | result.add(entry.getKey().getHostAddress() |
| 175 | + " => " |
| 176 | + entry.getValue().getMacAddress().toString() |
| 177 | + (entry.getValue().isExpired() ? " : EXPIRED" : " : VALID")); |
| 178 | } |
| 179 | |
| 180 | return result; |
| 181 | } |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Retrieve a list of all expired IPs in the ARP cache. |
| 185 | * |
| 186 | * @return list of all expired IPs |
| 187 | */ |
| 188 | List<InetAddress> getExpiredArpCacheIps() { |
| 189 | List<InetAddress> result = new ArrayList<InetAddress>(); |
| 190 | |
| 191 | for (Entry<InetAddress, ArpCacheEntry> entry : arpCache.entrySet()) { |
| 192 | if (entry.getValue().isExpired()) { |
| 193 | log.debug("add to the expired ip list, ip {}", entry.getKey()); |
| 194 | result.add(entry.getKey()); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return result; |
| 199 | } |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 200 | } |