Line data Source code
1 : /*
2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3 : */
4 :
5 : #ifndef SRC_VNSW_AGENT_MAC_LEARNING_MAC_LEARNING_MGMT_H_
6 : #define SRC_VNSW_AGENT_MAC_LEARNING_MAC_LEARNING_MGMT_H_
7 : #include "mac_learning_key.h"
8 : #include "mac_ip_learning_key.h"
9 :
10 : class MacLearningMgmtDBEntry;
11 : class MacLearningMgmtManager;
12 : class MacLearningMgmtDBTree;
13 : class MacLearningMgmtNode {
14 : public:
15 : MacLearningMgmtNode(MacLearningEntryPtr ptr);
16 : virtual ~MacLearningMgmtNode();
17 :
18 : void set_intf(MacLearningMgmtDBEntry *intf) {
19 : intf_.reset(intf);
20 : }
21 :
22 : void set_vrf(MacLearningMgmtDBEntry *vrf) {
23 : vrf_.reset(vrf);
24 : }
25 :
26 : void set_rt(MacLearningMgmtDBEntry *rt) {
27 : rt_.reset(rt);
28 : }
29 :
30 : void set_vn(MacLearningMgmtDBEntry *vn) {
31 : vn_.reset(vn);
32 : }
33 :
34 : void set_hc(MacLearningMgmtDBEntry *hc) {
35 : hc_service_.reset(hc);
36 : }
37 :
38 : void UpdateRef(MacLearningMgmtManager *mgr);
39 :
40 0 : MacLearningEntryPtr mac_learning_entry() {
41 0 : return mac_entry_;
42 : }
43 :
44 0 : void set_mac_learning_entry(MacLearningEntryPtr mac_entry) {
45 0 : mac_entry_ = mac_entry;
46 0 : }
47 : private:
48 : MacLearningEntryPtr mac_entry_;
49 : DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> intf_;
50 : DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> vrf_;
51 : DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> rt_;
52 : DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> vn_;
53 : DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> hc_service_;
54 : DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtNode);
55 : };
56 : typedef boost::shared_ptr<MacLearningMgmtNode> MacLearningMgmtNodePtr;
57 :
58 : class MacLearningMgmtDBEntry {
59 : public:
60 : typedef DependencyList<MacLearningMgmtNode,
61 : MacLearningMgmtDBEntry> MacLearningEntryList;
62 : enum Type {
63 : INVALID,
64 : INTERFACE,
65 : VRF,
66 : BRIDGE,
67 : EVPN,
68 : VN,
69 : HC_SERVICE,
70 : END
71 : };
72 :
73 : MacLearningMgmtDBEntry(Type type_, const DBEntry *entry);
74 492 : virtual ~MacLearningMgmtDBEntry() {}
75 :
76 : const DBEntry *db_entry() const {
77 : return db_entry_;
78 : }
79 :
80 690 : virtual bool UseDBEntry() const {
81 690 : return true;
82 : }
83 : void Change();
84 : void Delete(bool set_delete, bool notify_event = true);
85 : virtual bool TryDelete();
86 :
87 87 : void set_tree(MacLearningMgmtDBTree *tree) {
88 87 : tree_ = tree;
89 87 : }
90 :
91 197 : void set_db_entry(const DBEntry *entry) {
92 197 : db_entry_ = entry;
93 197 : }
94 :
95 396 : virtual bool Compare(const MacLearningMgmtDBEntry *rhs) const {
96 396 : return false;
97 : }
98 :
99 2259 : virtual bool IsLess(const MacLearningMgmtDBEntry *rhs) const {
100 2259 : if (type_ != rhs->type_) {
101 0 : return type_ < rhs->type_;
102 : }
103 :
104 2259 : if (UseDBEntry()) {
105 690 : if (db_entry_ != rhs->db_entry_) {
106 294 : return db_entry_ < rhs->db_entry_;
107 : }
108 : }
109 :
110 1965 : return Compare(rhs);
111 : }
112 :
113 139 : MacLearningMgmtDBTree* tree() const {
114 139 : return tree_;
115 : }
116 :
117 87 : void set_gen_id(uint32_t gen_id) {
118 87 : gen_id_ = gen_id;
119 87 : }
120 :
121 : uint32_t gen_id() const {
122 : return gen_id_;
123 : }
124 :
125 : protected:
126 : Type type_;
127 : const DBEntry* db_entry_;
128 : bool deleted_;
129 0 : DEPENDENCY_LIST(MacLearningMgmtNode, MacLearningMgmtDBEntry, mac_entry_list_);
130 : MacLearningMgmtDBTree *tree_;
131 : uint32_t gen_id_;
132 : DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtDBEntry);
133 : };
134 :
135 : class MacLearningMgmtIntfEntry : public MacLearningMgmtDBEntry {
136 : public:
137 : MacLearningMgmtIntfEntry(const Interface *intf);
138 92 : virtual ~MacLearningMgmtIntfEntry() {}
139 : private:
140 : DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtIntfEntry);
141 : };
142 :
143 : class MacLearningMgmtVrfEntry : public MacLearningMgmtDBEntry {
144 : public:
145 : MacLearningMgmtVrfEntry(const VrfEntry *vrf);
146 105 : virtual ~MacLearningMgmtVrfEntry() {}
147 : virtual bool TryDelete();
148 :
149 : private:
150 : DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtVrfEntry);
151 : };
152 :
153 : class MacLearningMgmtRouteEntry : public MacLearningMgmtDBEntry {
154 : public:
155 : MacLearningMgmtRouteEntry(const AgentRoute *rt);
156 : MacLearningMgmtRouteEntry(const std::string &vrf,
157 : const MacAddress &mac);
158 358 : virtual ~MacLearningMgmtRouteEntry() {}
159 1569 : virtual bool UseDBEntry() const {
160 1569 : return false;
161 : }
162 :
163 : virtual bool TryDelete();
164 1569 : virtual bool Compare(const MacLearningMgmtDBEntry *rhs) const {
165 1569 : const MacLearningMgmtRouteEntry *rhs_rt =
166 : static_cast<const MacLearningMgmtRouteEntry *>(rhs);
167 1569 : if (vrf_ != rhs_rt->vrf_) {
168 435 : return vrf_ < rhs_rt->vrf_;
169 : }
170 1134 : return mac_ < rhs_rt->mac_;
171 : }
172 :
173 43 : const std::string& vrf() {
174 43 : return vrf_;
175 : }
176 : private:
177 : std::string vrf_;
178 : MacAddress mac_;
179 : DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtRouteEntry);
180 : };
181 :
182 : class MacLearningMgmtVnEntry : public MacLearningMgmtDBEntry {
183 : public:
184 : MacLearningMgmtVnEntry(const VnEntry *vn);
185 24 : virtual ~MacLearningMgmtVnEntry() {}
186 : private:
187 : DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtVnEntry);
188 : };
189 :
190 : class MacLearningMgmtHcServiceEntry : public MacLearningMgmtDBEntry {
191 : public:
192 : MacLearningMgmtHcServiceEntry(const HealthCheckService *hc);
193 0 : virtual ~MacLearningMgmtHcServiceEntry() {}
194 : private:
195 : DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtHcServiceEntry);
196 : };
197 : class MacLearningMgmtKeyCmp {
198 : public:
199 2259 : bool operator()(const MacLearningMgmtDBEntry *lhs, const MacLearningMgmtDBEntry *rhs) const {
200 2259 : return lhs->IsLess(rhs);
201 : }
202 : };
203 :
204 : class MacLearningMgmtDBTree {
205 : public:
206 : typedef std::map<MacLearningMgmtDBEntry *, MacLearningMgmtDBEntry *,
207 : MacLearningMgmtKeyCmp> Tree;
208 : typedef std::pair<MacLearningMgmtDBEntry *, MacLearningMgmtDBEntry *>
209 : MacLearningMgmtDBPair;
210 :
211 : MacLearningMgmtDBTree(MacLearningMgmtManager *mgr);
212 15 : virtual ~MacLearningMgmtDBTree() {}
213 :
214 : void Add(MacLearningMgmtDBEntry *entry);
215 : void Change(MacLearningMgmtDBEntry *entry);
216 : void Delete(MacLearningMgmtDBEntry *entry);
217 : void Erase(MacLearningMgmtDBEntry *entry);
218 : MacLearningMgmtDBEntry* Find(MacLearningMgmtDBEntry *entry);
219 64 : MacLearningMgmtDBEntry* LowerBound(MacLearningMgmtDBEntry* entry) {
220 64 : Tree::iterator it = tree_.lower_bound(entry);
221 64 : if (it != tree_.end()) {
222 43 : return it->second;
223 : }
224 21 : return NULL;
225 : }
226 :
227 168 : MacLearningMgmtManager* mac_learning_mac_manager() {
228 168 : return mac_learning_mac_manager_;
229 : }
230 : void TryDelete(MacLearningMgmtDBEntry *db_entry);
231 : protected:
232 : Tree tree_;
233 : MacLearningMgmtManager *mac_learning_mac_manager_;
234 : DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtDBTree);
235 : };
236 :
237 : class MacLearningMgmtRequest {
238 : public:
239 : enum Event {
240 : ADD_MAC,
241 : CHANGE_MAC,
242 : DELETE_MAC,
243 : ADD_DBENTRY,
244 : CHANGE_DBENTRY,
245 : DELETE_DBENTRY,
246 : DELETE_DBENTRY_NO_OP,
247 : DELETE_ALL_MAC,
248 : RELEASE_TOKEN,
249 : ADD_MAC_IP,
250 : CHANGE_MAC_IP,
251 : DELETE_MAC_IP,
252 : };
253 :
254 0 : MacLearningMgmtRequest(Event event, MacLearningEntryPtr ptr):
255 0 : event_(event), mac_learning_entry_(ptr) {
256 0 : }
257 :
258 289 : MacLearningMgmtRequest(Event event, const DBEntry *db_entry,
259 289 : uint32_t gen_id):
260 289 : event_(event), db_entry_(db_entry), gen_id_(gen_id) {
261 289 : }
262 :
263 0 : MacLearningEntryPtr mac_learning_entry() {
264 0 : return mac_learning_entry_;
265 : }
266 :
267 289 : Event event() {
268 289 : return event_;
269 : }
270 :
271 486 : const DBEntry *db_entry() {
272 486 : return db_entry_;
273 : }
274 :
275 87 : uint32_t gen_id() const {
276 87 : return gen_id_;
277 : }
278 :
279 : void set_gen_id(uint32_t gen_id) {
280 : gen_id_ = gen_id;
281 : }
282 :
283 : private:
284 : Event event_;
285 : MacLearningEntryPtr mac_learning_entry_;
286 : const DBEntry *db_entry_;
287 : uint32_t gen_id_;
288 : };
289 :
290 : typedef boost::shared_ptr<MacLearningMgmtRequest> MacLearningMgmtRequestPtr;
291 :
292 : class MacLearningMgmtManager {
293 : public:
294 : typedef WorkQueue<MacLearningMgmtRequestPtr> MacLearningMgmtQueue;
295 : typedef std::map<MacLearningKey, MacLearningMgmtNodePtr,
296 : MacLearningKeyCmp> MacLearningNodeTree;
297 : typedef std::pair<MacLearningKey, MacLearningMgmtNodePtr>
298 : MacLearningNodePair;
299 :
300 : typedef std::map<MacIpLearningKey, MacLearningMgmtNodePtr,
301 : MacIpLearningKeyCmp> MacIpLearningNodeTree;
302 : typedef std::pair<MacIpLearningKey, MacLearningMgmtNodePtr>
303 : MacIpLearningNodePair;
304 : MacLearningMgmtManager(Agent *agent);
305 6 : virtual ~MacLearningMgmtManager() {}
306 : bool RequestHandler(MacLearningMgmtRequestPtr ptr);
307 :
308 : void AddMacLearningEntry(MacLearningMgmtRequestPtr ptr);
309 : void AddMacIpLearningEntry(MacLearningMgmtRequestPtr ptr);
310 : void ReleaseToken(MacLearningMgmtRequestPtr ptr);
311 : void DeleteMacLearningEntry(MacLearningMgmtRequestPtr ptr);
312 : void DeleteMacIpLearningEntry(MacLearningMgmtRequestPtr ptr);
313 : void AddDBEntry(MacLearningMgmtRequestPtr ptr);
314 : void DeleteDBEntry(MacLearningMgmtRequestPtr ptr, bool notify_event = true);
315 : void Enqueue(MacLearningMgmtRequestPtr &ptr);
316 : void DeleteAllEntry(MacLearningMgmtRequestPtr ptr);
317 :
318 : MacLearningMgmtDBEntry* Locate(const DBEntry *e);
319 : MacLearningMgmtDBEntry* Find(const DBEntry *e);
320 : MacLearningMgmtDBEntry* Locate(const std::string &vrf,
321 : const MacAddress &mac);
322 52 : MacLearningMgmtDBTree* vrf_tree() {
323 52 : return &vrf_tree_;
324 : }
325 :
326 : MacLearningMgmtDBTree* rt_tree() {
327 : return &rt_tree_;
328 : }
329 :
330 : bool IsVrfRouteEmpty(const std::string &vrf_name);
331 52 : Agent *agent() const {
332 52 : return agent_;
333 : }
334 : private:
335 : Agent *agent_;
336 : MacLearningNodeTree mac_learning_node_tree_;
337 : MacIpLearningNodeTree mac_ip_learning_node_tree_;
338 : MacLearningMgmtDBTree intf_tree_;
339 : MacLearningMgmtDBTree vrf_tree_;
340 : MacLearningMgmtDBTree rt_tree_;
341 : MacLearningMgmtDBTree vn_tree_;
342 : MacLearningMgmtDBTree hc_tree_;
343 : MacLearningMgmtQueue request_queue_;
344 : DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtManager);
345 : };
346 : #endif
|