1
#pragma once
2

            
3
#include <cstddef>
4
#include <cstdint>
5
#include <functional>
6
#include <memory>
7
#include <string>
8

            
9
#include "envoy/network/address.h"
10
#include "envoy/singleton/instance.h"
11

            
12
#include "source/common/common/logger.h"
13
#include "source/common/common/thread.h"
14

            
15
#include "absl/container/flat_hash_map.h"
16
#include "absl/container/flat_hash_set.h"
17
#include "cilium/bpf.h"
18

            
19
namespace std {
20
template <> class hash<const string> {
21
public:
22
  size_t operator()(const string& key) const { return hash<string>()(key); }
23
};
24
}; // namespace std
25

            
26
namespace Envoy {
27
namespace Cilium {
28

            
29
class CtMap : public Singleton::Instance, Logger::Loggable<Logger::Id::filter> {
30
public:
31
  CtMap(const std::string& bpf_root);
32

            
33
  const std::string& bpfRoot() { return bpf_root_; }
34

            
35
  uint32_t lookupSrcIdentity(const std::string& map_name, const Network::Address::Ip* sip,
36
                             const Network::Address::Ip* dip, bool ingress);
37

            
38
private:
39
  class CtMap4 : public Bpf {
40
  public:
41
    CtMap4();
42
  };
43

            
44
  class CtMap6 : public Bpf {
45
  public:
46
    CtMap6();
47
  };
48

            
49
public:
50
  class CtMaps4 {
51
  public:
52
    CtMaps4(const std::string& bpf_root, const std::string& map_name);
53

            
54
    bool ok_;
55
    CtMap4 ctmap4_tcp_;
56
    CtMap4 ctmap4_any_;
57
  };
58
  class CtMaps6 {
59
  public:
60
    CtMaps6(const std::string& bpf_root, const std::string& map_name);
61

            
62
    bool ok_;
63
    CtMap6 ctmap6_tcp_;
64
    CtMap6 ctmap6_any_;
65
  };
66
  void closeMaps(const absl::flat_hash_set<std::string>& to_be_closed);
67

            
68
private:
69
  absl::flat_hash_map<const std::string, std::unique_ptr<CtMaps4>>::iterator
70
  openMap4(const std::string& map_name);
71
  absl::flat_hash_map<const std::string, std::unique_ptr<CtMaps6>>::iterator
72
  openMap6(const std::string& map_name);
73

            
74
  // All known conntrack maps. Populated with the "global" maps at startup,
75
  // further maps are opened and inserted on demand.
76
  Thread::MutexBasicLockable maps_mutex_;
77
  absl::flat_hash_map<const std::string, std::unique_ptr<CtMaps4>> ct_maps4_;
78
  absl::flat_hash_map<const std::string, std::unique_ptr<CtMaps6>> ct_maps6_;
79
  std::string bpf_root_;
80
};
81

            
82
using CtMapSharedPtr = std::shared_ptr<CtMap>;
83

            
84
} // namespace Cilium
85
} // namespace Envoy