Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : */
4 :
5 : #ifndef vnsw_agent_mirror_table_hpp
6 : #define vnsw_agent_mirror_table_hpp
7 :
8 : #include <cmn/agent_cmn.h>
9 : #include <cmn/agent.h>
10 : #include <agent_types.h>
11 :
12 : struct MirrorEntryKey : public AgentKey {
13 60 : MirrorEntryKey(const std::string analyzer_name)
14 60 : : analyzer_name_(analyzer_name) {
15 60 : }
16 : std::string analyzer_name_;
17 : };
18 :
19 : struct MirrorEntryData : public AgentData {
20 : enum MirrorEntryFlags {
21 : DynamicNH_With_JuniperHdr = 1,
22 : DynamicNH_Without_JuniperHdr = 2,
23 : StaticNH_With_JuniperHdr = 3,
24 : StaticNH_Without_JuniperHdr = 4
25 :
26 : };
27 :
28 0 : MirrorEntryData(const std::string vrf_name, const IpAddress &sip,
29 : const uint16_t sport, const IpAddress &dip,
30 : const uint16_t dport, uint8_t mirror_flags,
31 0 : uint32_t vni, const MacAddress &mac, bool createdvrf):
32 0 : vrf_name_(vrf_name), sip_(sip), sport_(sport), dip_(dip),
33 0 : dport_(dport), mirror_flags_(mirror_flags), vni_(vni),
34 0 : mac_(mac), createdvrf_(createdvrf),
35 0 : nic_assisted_mirroring_(false) { }
36 :
37 0 : MirrorEntryData(bool nic_assisted_mirroring,
38 0 : uint16_t nic_assisted_mirroring_vlan):
39 0 : nic_assisted_mirroring_(nic_assisted_mirroring),
40 0 : nic_assisted_mirroring_vlan_(nic_assisted_mirroring_vlan) { }
41 :
42 :
43 : std::string vrf_name_;
44 : IpAddress sip_;
45 : uint16_t sport_;
46 : IpAddress dip_;
47 : uint16_t dport_;
48 : uint8_t mirror_flags_;
49 : uint32_t vni_;
50 : MacAddress mac_; // can be type of vtep mac or analyzer-mac based on type NH
51 : bool createdvrf_;
52 : bool nic_assisted_mirroring_;
53 : uint16_t nic_assisted_mirroring_vlan_;
54 : };
55 :
56 : class MirrorEntry : AgentRefCount<MirrorEntry>, public AgentDBEntry {
57 : public:
58 60 : MirrorEntry(std::string analyzer_name) :
59 120 : analyzer_name_(analyzer_name), vrf_(NULL, this), nh_(NULL),
60 180 : vrf_name_(""), createdvrf_(false) { };
61 120 : virtual ~MirrorEntry() { };
62 :
63 : virtual bool IsLess(const DBEntry &rhs) const;
64 0 : virtual bool Change(const DBRequest *req) {return false;};
65 :
66 : virtual void SetKey(const DBRequestKey *key);
67 : virtual KeyPtr GetDBRequestKey() const;
68 0 : virtual std::string ToString() const { return "MirrorEntry";};
69 :
70 0 : uint32_t GetRefCount() const {
71 0 : return AgentRefCount<MirrorEntry>::GetRefCount();
72 : }
73 : bool DBEntrySandesh(Sandesh *sresp, std::string &name) const;
74 : void set_mirror_entrySandeshData(MirrorEntrySandeshData &data) const;
75 : uint32_t vrf_id() const;
76 : const VrfEntry *GetVrf() const;
77 0 : const std::string GetAnalyzerName() const {return analyzer_name_;}
78 0 : const IpAddress *GetSip() const {return &sip_;}
79 0 : uint16_t GetSPort() const {return sport_;}
80 0 : const IpAddress *GetDip() const {return &dip_;}
81 0 : uint16_t GetDPort() const {return dport_;}
82 178 : const NextHop *GetNH() const {return nh_.get();}
83 0 : const std::string vrf_name() const {return vrf_name_;}
84 0 : uint32_t GetVni() const {return vni_;}
85 0 : uint8_t GetMirrorFlag() const {return mirror_flags_;}
86 0 : const MacAddress *GetMac() const { return &mac_;}
87 0 : bool GetCreatedVrf() const {return createdvrf_;}
88 94 : uint16_t nic_assisted_mirroring_vlan() const {
89 94 : return nic_assisted_mirroring_vlan_;}
90 150 : bool nic_assisted_mirroring() const {
91 150 : return nic_assisted_mirroring_;
92 : }
93 0 : void set_mirror_index(uint32_t index) {mirror_index_ = index;}
94 0 : uint32_t mirror_index() const { return mirror_index_; }
95 : private:
96 : std::string analyzer_name_;
97 : VrfEntryRef vrf_;
98 : IpAddress sip_;
99 : uint16_t sport_;
100 : IpAddress dip_;
101 : uint16_t dport_;
102 : NextHopRef nh_;
103 : std::string vrf_name_;
104 : uint8_t mirror_flags_;
105 : uint32_t vni_;
106 : MacAddress mac_; // can be type of vtep mac or analyzer-mac based on type NH
107 : // this vrf will be created if this mirror vrf is not known to compute node
108 : // add it subscribes for the route information to get down loaded
109 : bool createdvrf_;
110 : bool nic_assisted_mirroring_;
111 : uint16_t nic_assisted_mirroring_vlan_;
112 : uint32_t mirror_index_;
113 : friend class MirrorTable;
114 : };
115 :
116 : class MirrorTable : public AgentDBTable {
117 : public:
118 : const static unsigned bufLen = 512;
119 : const static uint8_t kInvalidIndex = 0xFF;
120 : typedef std::vector<MirrorEntry *> MirrorEntryList;
121 : typedef std::map<std::string , MirrorEntryList> VrfMirrorEntryList;
122 : typedef std::pair<std::string , MirrorEntryList> VrfMirrorEntry;
123 :
124 3 : MirrorTable(DB *db, const std::string &name) : AgentDBTable(db, name) {
125 3 : }
126 : virtual ~MirrorTable();
127 : virtual std::unique_ptr<DBEntry> AllocEntry(const DBRequestKey *k) const;
128 0 : virtual size_t Hash(const DBEntry *entry) const {return 0;}
129 60 : virtual size_t Hash(const DBRequestKey *key) const {return 0;}
130 :
131 : virtual DBEntry *Add(const DBRequest *req);
132 : virtual bool OnChange(DBEntry *entry, const DBRequest *req);
133 : virtual bool Delete(DBEntry *entry, const DBRequest *request);
134 0 : virtual bool Resync(DBEntry *entry, const DBRequest *req) {
135 0 : bool ret = OnChange(entry, req);
136 0 : return ret;
137 : }
138 : virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args,
139 : const std::string &context);
140 : VrfEntry *FindVrfEntry(const std::string &vrf_name) const;
141 : static void AddMirrorEntry(const std::string &analyzer_name,
142 : const std::string &vrf_name,
143 : const IpAddress &sip, uint16_t sport,
144 : const IpAddress &dip, uint16_t dport);
145 : static void AddMirrorEntry(const std::string &analyzer_name,
146 : const std::string &vrf_name,
147 : const IpAddress &sip, uint16_t sport,
148 : const IpAddress &dip, uint16_t dport,
149 : uint32_t vni, uint8_t mirror_flag,
150 : const MacAddress &mac);
151 : static void AddMirrorEntry(const std::string &analyzer_name,
152 : uint32_t nic_assisted_mirroring_vlan);
153 : static void DelMirrorEntry(const std::string &analyzer_name);
154 : virtual void OnZeroRefcount(AgentDBEntry *e);
155 : static DBTableBase *CreateTable(DB *db, const std::string &name);
156 2 : static MirrorTable *GetInstance() {return mirror_table_;}
157 : void MirrorSockInit(void);
158 : void ReadHandler(const boost::system::error_code& error, size_t bytes);
159 : void AddUnresolved(MirrorEntry *entry);
160 : void RemoveUnresolved(MirrorEntry *entry);
161 : void AddResolvedVrfMirrorEntry(MirrorEntry *entry);
162 : void DeleteResolvedVrfMirrorEntry(MirrorEntry *entry);
163 : void ResyncMirrorEntry(VrfMirrorEntryList &list, const VrfEntry *vrf);
164 : void ResyncResolvedMirrorEntry(const VrfEntry *vrf);
165 : void ResyncUnresolvedMirrorEntry(const VrfEntry *vrf);
166 : void Add(VrfMirrorEntryList &vrf_entry_map, MirrorEntry *entry);
167 : void Delete(VrfMirrorEntryList &vrf_entry_map, MirrorEntry *entry);
168 : void VrfListenerInit();
169 : void VrfNotify(DBTablePartBase *root, DBEntryBase *entry);
170 : void Shutdown();
171 : void Initialize();
172 : bool OnChange(MirrorEntry *mirror_entry);
173 : struct MirrorVrfState : DBState {
174 0 : MirrorVrfState() : DBState(), seen_(false),
175 0 : bridge_rt_table_listener_id_(DBTableBase::kInvalidId) {}
176 : bool seen_;
177 : DBTableBase::ListenerId bridge_rt_table_listener_id_;
178 : };
179 : void BridgeRouteTableNotify(DBTablePartBase *partition, DBEntryBase *e);
180 : void UnRegisterBridgeRouteTableListener(const VrfEntry *entry,
181 : MirrorVrfState *state);
182 : bool UnresolvedMirrorVrf(const VrfEntry *vrf, VrfMirrorEntryList &list);
183 : MirrorEntry* GetMirrorEntry(VrfEntry *vrf, const MacAddress & mac,
184 : VrfMirrorEntryList &list);
185 : static MirrorEntryData::MirrorEntryFlags
186 : DecodeMirrorFlag (const std::string &nh_mode, bool juniper_header);
187 : void DeleteMirrorVrf(MirrorEntry *entry, bool del_from_vrf_list);
188 : bool IsConfigured();
189 : private:
190 : std::unique_ptr<boost::asio::ip::udp::socket> udp_sock_;
191 : static MirrorTable *mirror_table_;
192 : char rx_buff_[bufLen];
193 : VrfMirrorEntryList unresolved_entry_list_;
194 : VrfMirrorEntryList resolved_entry_list_;
195 : DBTableBase::ListenerId vrf_listener_id_;
196 : };
197 : #endif
|