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 | |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 98 | /** |
| 99 | * 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] | 100 | * |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 101 | * @param ipAddress the IP address to look up |
| 102 | * @return the MAC address if found in the cache, null if not |
| 103 | */ |
| 104 | synchronized MACAddress lookup(InetAddress ipAddress) { |
| 105 | ArpCacheEntry arpEntry = arpCache.get(ipAddress); |
| 106 | |
| 107 | if (arpEntry == null) { |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | if (arpEntry.isExpired()) { |
| 112 | // Entry has timed out so we'll remove it and return null |
| 113 | log.trace("Removing expired ARP entry for {}", |
| 114 | ipAddress.getHostAddress()); |
| 115 | |
| 116 | arpCache.remove(ipAddress); |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | return arpEntry.getMacAddress(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Update an entry in the ARP cache. If the IP to MAC mapping is already |
| 125 | * in the cache, its timestamp will be updated. If not, the entry will |
| 126 | * be added with a new timestamp of the current time. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | * |
| 128 | * @param ipAddress the IP address that will be mapped in the cache |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 129 | * @param macAddress the MAC address that maps to {@code ipAddress} |
| 130 | */ |
| 131 | synchronized void update(InetAddress ipAddress, MACAddress macAddress) { |
| 132 | ArpCacheEntry arpEntry = arpCache.get(ipAddress); |
| 133 | |
| 134 | if (arpEntry != null && arpEntry.getMacAddress().equals(macAddress)) { |
| 135 | arpEntry.setTimeLastSeen(System.currentTimeMillis()); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 136 | log.debug("The same ArpCache, ip {}, mac {}. Update local cache last seen time only.", ipAddress, macAddress); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 137 | } else { |
| 138 | arpCache.put(ipAddress, new ArpCacheEntry(macAddress)); |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 139 | kvArpCache.forceCreate(ipAddress, macAddress.toBytes()); |
| 140 | log.debug("Create/Update ip {}, mac {} in ArpCache.", ipAddress, macAddress); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Remove an entry in the ARP cache. |
| 146 | * |
| 147 | * @param ipAddress the IP address that will be mapped in the cache |
| 148 | */ |
| 149 | synchronized void remove(InetAddress ipAddress) { |
| 150 | ArpCacheEntry entry = arpCache.remove(ipAddress); |
| 151 | |
| 152 | if (entry == null) { |
| 153 | log.debug("ArpCache doesn't have the ip key {}.", ipAddress); |
| 154 | } else { |
| 155 | kvArpCache.forceDelete(ipAddress); |
| 156 | log.debug("Remove it in ArpCache and DB, ip {}", ipAddress); |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Retrieve a list of all mappings in the ARP cache. |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 162 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 163 | * @return list of all ARP mappings, formatted as a human-readable string |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 164 | */ |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 165 | List<String> getMappings() { |
Jonathan Hart | 7c9a2fb | 2014-03-27 09:51:41 -0700 | [diff] [blame] | 166 | List<String> result = new ArrayList<String>(arpCache.size()); |
| 167 | |
| 168 | for (Map.Entry<InetAddress, ArpCacheEntry> entry : arpCache.entrySet()) { |
| 169 | result.add(entry.getKey().getHostAddress() |
| 170 | + " => " |
| 171 | + entry.getValue().getMacAddress().toString() |
| 172 | + (entry.getValue().isExpired() ? " : EXPIRED" : " : VALID")); |
| 173 | } |
| 174 | |
| 175 | return result; |
| 176 | } |
TeruU | 3c049c4 | 2014-04-15 10:13:25 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Retrieve a list of all expired IPs in the ARP cache. |
| 180 | * |
| 181 | * @return list of all expired IPs |
| 182 | */ |
| 183 | List<InetAddress> getExpiredArpCacheIps() { |
| 184 | List<InetAddress> result = new ArrayList<InetAddress>(); |
| 185 | |
| 186 | for (Entry<InetAddress, ArpCacheEntry> entry : arpCache.entrySet()) { |
| 187 | if (entry.getValue().isExpired()) { |
| 188 | log.debug("add to the expired ip list, ip {}", entry.getKey()); |
| 189 | result.add(entry.getKey()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return result; |
| 194 | } |
Jonathan Hart | abad6a5 | 2013-09-30 18:17:21 +1300 | [diff] [blame] | 195 | } |