]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #2592 in SNORT/snort3 from ~SMINUT/snort3:host_cache_ipv6 to master
authorMasud Hasan (mashasan) <mashasan@cisco.com>
Mon, 2 Nov 2020 22:12:26 +0000 (22:12 +0000)
committerMasud Hasan (mashasan) <mashasan@cisco.com>
Mon, 2 Nov 2020 22:12:26 +0000 (22:12 +0000)
Squashed commit of the following:

commit c540602d306a1700efb69a7389cefcd25ee7e8e3
Author: Silviu Minut <sminut@cisco.com>
Date:   Fri Oct 30 14:06:18 2020 -0400

    host_tracker: ignore IP family when comparing SfIp keys in the host cache

src/hash/lru_cache_shared.h
src/host_tracker/host_cache.h

index 4bdc10742f827c73f592101610bd944b9a9c94d0..a3dd6dc534fcd34df84f88f41e8e5d3433e917fa 100644 (file)
@@ -48,7 +48,7 @@ struct LruCacheSharedStats
     PegCount replaced = 0;      // found entry and replaced it
 };
 
-template<typename Key, typename Value, typename Hash>
+template<typename Key, typename Value, typename Hash, typename Eq = std::equal_to<Key>>
 class LruCacheShared
 {
 public:
@@ -130,7 +130,7 @@ public:
 protected:
     using LruList = std::list<std::pair<Key, Data>>;
     using LruListIter = typename LruList::iterator;
-    using LruMap = std::unordered_map<Key, LruListIter, Hash>;
+    using LruMap = std::unordered_map<Key, LruListIter, Hash, Eq>;
     using LruMapIter = typename LruMap::iterator;
 
     static constexpr size_t mem_chunk = sizeof(Data) + sizeof(Value);
@@ -180,8 +180,8 @@ protected:
     }
 };
 
-template<typename Key, typename Value, typename Hash>
-bool LruCacheShared<Key, Value, Hash>::set_max_size(size_t newsize)
+template<typename Key, typename Value, typename Hash, typename Eq>
+bool LruCacheShared<Key, Value, Hash, Eq>::set_max_size(size_t newsize)
 {
     if (newsize == 0)
         return false;   //  Not allowed to set size to zero.
@@ -201,8 +201,8 @@ bool LruCacheShared<Key, Value, Hash>::set_max_size(size_t newsize)
     return true;
 }
 
-template<typename Key, typename Value, typename Hash>
-std::shared_ptr<Value> LruCacheShared<Key, Value, Hash>::find(const Key& key)
+template<typename Key, typename Value, typename Hash, typename Eq>
+std::shared_ptr<Value> LruCacheShared<Key, Value, Hash, Eq>::find(const Key& key)
 {
     LruMapIter map_iter;
     std::lock_guard<std::mutex> cache_lock(cache_mutex);
@@ -220,14 +220,14 @@ std::shared_ptr<Value> LruCacheShared<Key, Value, Hash>::find(const Key& key)
     return map_iter->second->second;
 }
 
-template<typename Key, typename Value, typename Hash>
-std::shared_ptr<Value> LruCacheShared<Key, Value, Hash>::operator[](const Key& key)
+template<typename Key, typename Value, typename Hash, typename Eq>
+std::shared_ptr<Value> LruCacheShared<Key, Value, Hash, Eq>::operator[](const Key& key)
 {
     return find_else_create(key, nullptr);
 }
 
-template<typename Key, typename Value, typename Hash>
-std::shared_ptr<Value> LruCacheShared<Key, Value, Hash>::
+template<typename Key, typename Value, typename Hash, typename Eq>
+std::shared_ptr<Value> LruCacheShared<Key, Value, Hash, Eq>::
 find_else_create(const Key& key, bool* new_data)
 {
     LruMapIter map_iter;
@@ -268,8 +268,8 @@ find_else_create(const Key& key, bool* new_data)
     return data;
 }
 
-template<typename Key, typename Value, typename Hash>
-bool LruCacheShared<Key, Value, Hash>::
+template<typename Key, typename Value, typename Hash, typename Eq>
+bool LruCacheShared<Key, Value, Hash, Eq>::
 find_else_insert(const Key& key, std::shared_ptr<Value>& data, bool replace)
 {
     LruMapIter map_iter;
@@ -307,9 +307,9 @@ find_else_insert(const Key& key, std::shared_ptr<Value>& data, bool replace)
     return false;
 }
 
-template<typename Key, typename Value, typename Hash>
+template<typename Key, typename Value, typename Hash, typename Eq>
 std::vector< std::pair<Key, std::shared_ptr<Value>> >
-LruCacheShared<Key, Value, Hash>::get_all_data()
+LruCacheShared<Key, Value, Hash, Eq>::get_all_data()
 {
     std::vector<std::pair<Key, Data> > vec;
     std::lock_guard<std::mutex> cache_lock(cache_mutex);
@@ -322,8 +322,8 @@ LruCacheShared<Key, Value, Hash>::get_all_data()
     return vec;
 }
 
-template<typename Key, typename Value, typename Hash>
-bool LruCacheShared<Key, Value, Hash>::remove(const Key& key)
+template<typename Key, typename Value, typename Hash, typename Eq>
+bool LruCacheShared<Key, Value, Hash, Eq>::remove(const Key& key)
 {
     LruMapIter map_iter;
 
@@ -363,8 +363,8 @@ bool LruCacheShared<Key, Value, Hash>::remove(const Key& key)
     return true;
 }
 
-template<typename Key, typename Value, typename Hash>
-bool LruCacheShared<Key, Value, Hash>::remove(const Key& key, std::shared_ptr<Value>& data)
+template<typename Key, typename Value, typename Hash, typename Eq>
+bool LruCacheShared<Key, Value, Hash, Eq>::remove(const Key& key, std::shared_ptr<Value>& data)
 {
     LruMapIter map_iter;
 
index 0f7a86a9cf8d5d3d9f13ecbf4cbc2233e7ab672f..a18403874ec3fec1b5b0599ffbc2ad1c51f75c32 100644 (file)
 #include "sfip/sf_ip.h"
 #include "utils/stats.h"
 
-//  Used to create hash of key for indexing into cache.
+// Used to create hash of key for indexing into cache.
+//
+// Note that both HashIp and IpEqualTo below ignore the IP family.
+// This means that 1.2.3.4 and ::ffff:0102:0304 will be treated
+// as equal (same host).
 struct HashIp
 {
     size_t operator()(const snort::SfIp& ip) const
@@ -46,11 +50,19 @@ struct HashIp
     }
 };
 
-template<typename Key, typename Value, typename Hash>
-class LruCacheSharedMemcap : public LruCacheShared<Key, Value, Hash>, public HostCacheInterface
+struct IpEqualTo
+{
+    bool operator()(const snort::SfIp &lhs, const snort::SfIp &rhs) const
+    {
+        return lhs.fast_eq6(rhs);
+    }
+};
+
+template<typename Key, typename Value, typename Hash, typename Eq = std::equal_to<Key>>
+class LruCacheSharedMemcap : public LruCacheShared<Key, Value, Hash, Eq>, public HostCacheInterface
 {
 public:
-    using LruBase = LruCacheShared<Key, Value, Hash>;
+    using LruBase = LruCacheShared<Key, Value, Hash, Eq>;
     using LruBase::cache_mutex;
     using LruBase::current_size;
     using LruBase::list;
@@ -66,7 +78,7 @@ public:
     LruCacheSharedMemcap(const LruCacheSharedMemcap& arg) = delete;
     LruCacheSharedMemcap& operator=(const LruCacheSharedMemcap& arg) = delete;
 
-    LruCacheSharedMemcap(const size_t initial_size) : LruCacheShared<Key, Value, Hash>(initial_size) {}
+    LruCacheSharedMemcap(const size_t initial_size) : LruCacheShared<Key, Value, Hash, Eq>(initial_size) {}
 
     size_t mem_size() override
     {
@@ -197,7 +209,7 @@ private:
     friend class TEST_host_cache_module_misc_Test; // for unit test
 };
 
-typedef LruCacheSharedMemcap<snort::SfIp, snort::HostTracker, HashIp> HostCacheIp;
+typedef LruCacheSharedMemcap<snort::SfIp, snort::HostTracker, HashIp, IpEqualTo> HostCacheIp;
 
 extern SO_PUBLIC HostCacheIp host_cache;