Line data Source code
1 : /*
2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include <oper/vn.h>
7 : #include <oper/vrf.h>
8 : #include <oper/nexthop.h>
9 : #include <oper/interface_common.h>
10 : #include <oper/route_common.h>
11 : #include "mac_learning_proto.h"
12 : #include "mac_learning_proto_handler.h"
13 : #include "mac_learning.h"
14 : #include "mac_learning_mgmt.h"
15 : #include "mac_ip_learning.h"
16 :
17 0 : MacLearningMgmtNode::MacLearningMgmtNode(MacLearningEntryPtr ptr):
18 0 : mac_entry_(ptr), intf_(this), vrf_(this), rt_(this), vn_(this),
19 0 : hc_service_(this) {
20 0 : }
21 :
22 0 : MacLearningMgmtNode::~MacLearningMgmtNode() {
23 0 : MacLearningMgmtDBEntry *entry = vrf_.get();
24 0 : vrf_.reset(NULL);
25 0 : if (entry) {
26 0 : entry->tree()->TryDelete(entry);
27 : }
28 :
29 0 : entry = intf_.get();
30 0 : intf_.reset(NULL);
31 0 : if (entry) {
32 0 : entry->tree()->TryDelete(entry);
33 : }
34 :
35 0 : entry = rt_.get();
36 0 : rt_.reset(NULL);
37 0 : if (entry) {
38 0 : entry->tree()->TryDelete(entry);
39 : }
40 :
41 0 : entry = vn_.get();
42 0 : vn_.reset(NULL);
43 0 : if (entry) {
44 0 : entry->tree()->TryDelete(entry);
45 : }
46 :
47 0 : entry = hc_service_.get();
48 0 : hc_service_.reset(NULL);
49 0 : if (entry) {
50 0 : entry->tree()->TryDelete(entry);
51 : }
52 0 : }
53 :
54 0 : void MacLearningMgmtNode::UpdateRef(MacLearningMgmtManager *mgr) {
55 0 : vrf_.reset(mgr->Locate(mac_entry_->vrf()));
56 :
57 : MacLearningEntryLocal *le =
58 0 : dynamic_cast<MacLearningEntryLocal *>(mac_entry_.get());
59 0 : if (le != NULL) {
60 0 : intf_.reset(mgr->Locate(le->intf()));
61 : }
62 :
63 : MacLearningEntryPBB *pbb_mac =
64 0 : dynamic_cast<MacLearningEntryPBB *>(mac_entry_.get());
65 0 : if (pbb_mac) {
66 0 : rt_.reset(mgr->Locate(pbb_mac->vrf()->bmac_vrf_name(),
67 : pbb_mac->bmac()));
68 : }
69 : MacIpLearningEntry *mac_ip_entry =
70 0 : dynamic_cast<MacIpLearningEntry *>(mac_entry_.get());
71 0 : if (mac_ip_entry != NULL) {
72 0 : intf_.reset(mgr->Locate(mac_ip_entry->intf()));
73 0 : vn_.reset(mgr->Locate(mac_ip_entry->Vn()));
74 0 : hc_service_.reset(mgr->Locate(mac_ip_entry->HcService()));
75 : }
76 :
77 0 : }
78 :
79 450 : MacLearningMgmtDBEntry::MacLearningMgmtDBEntry(Type type, const DBEntry *entry):
80 450 : type_(type), db_entry_(entry), deleted_(false) {
81 450 : }
82 :
83 176 : void MacLearningMgmtDBEntry::Change() {
84 176 : deleted_ = false;
85 176 : for (MacLearningEntryList::iterator iter = mac_entry_list_.begin();
86 176 : iter != mac_entry_list_.end(); iter++) {
87 0 : MacLearningMgmtNode *ptr = iter.operator->();
88 : MacLearningEntryRequestPtr req_ptr(new MacLearningEntryRequest(
89 : MacLearningEntryRequest::RESYNC_MAC,
90 0 : ptr->mac_learning_entry()));
91 :
92 0 : ptr->mac_learning_entry()->EnqueueToTable(req_ptr);
93 0 : }
94 176 : }
95 :
96 83 : void MacLearningMgmtDBEntry::Delete(bool set_deleted, bool notify_event) {
97 83 : if (set_deleted) {
98 82 : deleted_ = true;
99 : }
100 83 : if (notify_event) {
101 83 : for (MacLearningEntryList::iterator iter = mac_entry_list_.begin();
102 83 : iter != mac_entry_list_.end(); iter++) {
103 0 : MacLearningMgmtNode *ptr = iter.operator->();
104 : MacLearningEntryRequestPtr req_ptr(new MacLearningEntryRequest(
105 : MacLearningEntryRequest::DELETE_MAC,
106 0 : ptr->mac_learning_entry()));
107 0 : ptr->mac_learning_entry()->EnqueueToTable(req_ptr);
108 0 : }
109 : }
110 83 : }
111 :
112 91 : bool MacLearningMgmtDBEntry::TryDelete() {
113 91 : if (mac_entry_list_.empty() == false || tree_->Find(this) == NULL) {
114 0 : return false;
115 : }
116 :
117 91 : if (db_entry_ == NULL || deleted_ == true) {
118 : MacLearningEntryRequestPtr req_ptr(new MacLearningEntryRequest(
119 : MacLearningEntryRequest::FREE_DB_ENTRY, db_entry_,
120 82 : gen_id_));
121 : Agent::GetInstance()->mac_learning_proto()->
122 82 : Find(0)->Enqueue(req_ptr);
123 82 : tree_->Erase(this);
124 82 : return true;
125 82 : }
126 :
127 9 : return false;
128 : }
129 :
130 73 : MacLearningMgmtIntfEntry::MacLearningMgmtIntfEntry(const Interface *intf) :
131 73 : MacLearningMgmtDBEntry(INTERFACE, intf) {
132 73 : }
133 :
134 78 : MacLearningMgmtVrfEntry::MacLearningMgmtVrfEntry(const VrfEntry *vrf) :
135 78 : MacLearningMgmtDBEntry(VRF, vrf) {
136 78 : }
137 :
138 21 : MacLearningMgmtVnEntry::MacLearningMgmtVnEntry(const VnEntry *vn) :
139 21 : MacLearningMgmtDBEntry(VN, vn) {
140 21 : }
141 :
142 0 : MacLearningMgmtHcServiceEntry::MacLearningMgmtHcServiceEntry(
143 0 : const HealthCheckService *hc) :
144 0 : MacLearningMgmtDBEntry(HC_SERVICE, hc) {
145 0 : }
146 :
147 5 : MacLearningMgmtDBTree::MacLearningMgmtDBTree(MacLearningMgmtManager *mgr) :
148 5 : mac_learning_mac_manager_(mgr) {
149 5 : }
150 :
151 219 : MacLearningMgmtRouteEntry::MacLearningMgmtRouteEntry(const AgentRoute *rt):
152 219 : MacLearningMgmtDBEntry(BRIDGE, rt) {
153 219 : const BridgeRouteEntry *br_rt = dynamic_cast<const BridgeRouteEntry *>(rt);
154 219 : mac_ = br_rt->prefix_address();
155 219 : vrf_ = br_rt->vrf()->GetName();
156 219 : }
157 :
158 50 : bool MacLearningMgmtRouteEntry::TryDelete() {
159 50 : bool ret = MacLearningMgmtDBEntry::TryDelete();
160 50 : if (ret == false) {
161 0 : return ret;
162 : }
163 :
164 : //If route entry can be deleted, check if VRF entry can be
165 : //deleted
166 50 : VrfKey vrf_key(vrf_);
167 50 : Agent *agent = tree_->mac_learning_mac_manager()->agent();
168 50 : const VrfEntry *vrf = static_cast<const VrfEntry *>(
169 50 : agent->vrf_table()->Find(&vrf_key, true));
170 50 : if (vrf) {
171 50 : MacLearningMgmtVrfEntry mgmt_vrf(vrf);
172 : MacLearningMgmtVrfEntry *mgmt_entry =
173 : static_cast<MacLearningMgmtVrfEntry *>(
174 50 : tree_->mac_learning_mac_manager()->vrf_tree()->Find(&mgmt_vrf));
175 50 : if (mgmt_entry) {
176 50 : mgmt_entry->tree()->TryDelete(mgmt_entry);
177 : }
178 50 : }
179 50 : return ret;
180 50 : }
181 :
182 59 : bool MacLearningMgmtVrfEntry::TryDelete() {
183 59 : const VrfEntry *vrf = static_cast<const VrfEntry *>(db_entry_);
184 59 : if (tree_->mac_learning_mac_manager()->IsVrfRouteEmpty(vrf->GetName())) {
185 18 : return MacLearningMgmtDBEntry::TryDelete();
186 : }
187 41 : return false;
188 : }
189 :
190 59 : MacLearningMgmtRouteEntry::MacLearningMgmtRouteEntry(const std::string &vrf,
191 59 : const MacAddress &mac):
192 59 : MacLearningMgmtDBEntry(BRIDGE, NULL), vrf_(vrf), mac_(mac) {
193 59 : }
194 :
195 82 : void MacLearningMgmtDBTree::Add(MacLearningMgmtDBEntry *entry) {
196 82 : tree_.insert(MacLearningMgmtDBPair(entry, entry));
197 82 : entry->set_tree(this);
198 82 : }
199 :
200 0 : void MacLearningMgmtDBTree::Change(MacLearningMgmtDBEntry *e) {
201 0 : MacLearningMgmtDBEntry *entry = Find(e);
202 0 : if (entry) {
203 0 : entry->Change();
204 : }
205 0 : }
206 :
207 0 : void MacLearningMgmtDBTree::Delete(MacLearningMgmtDBEntry *e) {
208 0 : MacLearningMgmtDBEntry *entry = Find(e);
209 0 : if (entry) {
210 0 : entry->Delete(true);
211 0 : TryDelete(entry);
212 : }
213 0 : }
214 :
215 : MacLearningMgmtDBEntry*
216 400 : MacLearningMgmtDBTree::Find(MacLearningMgmtDBEntry *e) {
217 400 : Tree::iterator it = tree_.find(e);
218 400 : if (it == tree_.end()) {
219 82 : return NULL;
220 : }
221 :
222 318 : return it->second;
223 : }
224 :
225 82 : void MacLearningMgmtDBTree::Erase(MacLearningMgmtDBEntry *e) {
226 82 : tree_.erase(e);
227 82 : }
228 :
229 132 : void MacLearningMgmtDBTree::TryDelete(MacLearningMgmtDBEntry *e) {
230 132 : if (e->TryDelete()) {
231 82 : delete e;
232 : }
233 132 : }
234 :
235 : void
236 0 : MacLearningMgmtManager::AddMacLearningEntry(MacLearningMgmtRequestPtr ptr) {
237 : MacPbbLearningEntry *entry =
238 0 : dynamic_cast<MacPbbLearningEntry *>(ptr->mac_learning_entry().get());
239 : MacLearningNodeTree::iterator it =
240 0 : mac_learning_node_tree_.find(entry->key());
241 0 : if (it == mac_learning_node_tree_.end()) {
242 : MacLearningMgmtNodePtr node(new MacLearningMgmtNode(
243 0 : ptr->mac_learning_entry()));
244 0 : node->UpdateRef(this);
245 0 : MacLearningNodePair pair(entry->key(), node);
246 0 : mac_learning_node_tree_.insert(pair);
247 0 : } else {
248 0 : it->second->set_mac_learning_entry(ptr->mac_learning_entry());
249 0 : it->second->UpdateRef(this);
250 : }
251 0 : }
252 :
253 : void
254 0 : MacLearningMgmtManager::AddMacIpLearningEntry(MacLearningMgmtRequestPtr ptr) {
255 : MacIpLearningEntry *entry =
256 0 : dynamic_cast<MacIpLearningEntry *>(ptr->mac_learning_entry().get());
257 :
258 : MacIpLearningNodeTree::iterator it =
259 0 : mac_ip_learning_node_tree_.find(entry->key());
260 0 : if (it == mac_ip_learning_node_tree_.end()) {
261 : MacLearningMgmtNodePtr node(new MacLearningMgmtNode(
262 0 : ptr->mac_learning_entry()));
263 0 : node->UpdateRef(this);
264 0 : MacIpLearningNodePair pair(entry->key(), node);
265 0 : mac_ip_learning_node_tree_.insert(pair);
266 0 : } else {
267 0 : it->second->set_mac_learning_entry(ptr->mac_learning_entry());
268 0 : it->second->UpdateRef(this);
269 : }
270 0 : }
271 : void
272 0 : MacLearningMgmtManager::DeleteMacLearningEntry(MacLearningMgmtRequestPtr ptr) {
273 : MacPbbLearningEntry *entry =
274 0 : dynamic_cast<MacPbbLearningEntry *>(ptr->mac_learning_entry().get());
275 0 : mac_learning_node_tree_.erase(entry->key());
276 0 : }
277 :
278 0 : void MacLearningMgmtManager::DeleteMacIpLearningEntry(MacLearningMgmtRequestPtr ptr) {
279 : MacIpLearningEntry *entry =
280 0 : dynamic_cast<MacIpLearningEntry *>(ptr->mac_learning_entry().get());
281 0 : mac_ip_learning_node_tree_.erase(entry->key());
282 0 : }
283 :
284 176 : void MacLearningMgmtManager::AddDBEntry(MacLearningMgmtRequestPtr ptr) {
285 176 : MacLearningMgmtDBEntry *entry = Locate(ptr->db_entry());
286 176 : entry->set_db_entry(ptr->db_entry());
287 176 : entry->Change();
288 176 : }
289 :
290 82 : void MacLearningMgmtManager::DeleteDBEntry(MacLearningMgmtRequestPtr ptr,
291 : bool notify_event) {
292 82 : MacLearningMgmtDBEntry *entry = Find(ptr->db_entry());
293 82 : if (entry) {
294 82 : entry->Delete(true, notify_event);
295 82 : entry->set_gen_id(ptr->gen_id());
296 82 : entry->tree()->TryDelete(entry);
297 : }
298 82 : }
299 :
300 1 : void MacLearningMgmtManager::DeleteAllEntry(MacLearningMgmtRequestPtr ptr) {
301 1 : MacLearningMgmtDBEntry *entry = Find(ptr->db_entry());
302 1 : if (entry) {
303 1 : entry->Delete(false);
304 : }
305 1 : }
306 :
307 : bool
308 259 : MacLearningMgmtManager::RequestHandler(MacLearningMgmtRequestPtr ptr) {
309 259 : switch(ptr->event()) {
310 0 : case MacLearningMgmtRequest::ADD_MAC:
311 : case MacLearningMgmtRequest::CHANGE_MAC:
312 0 : AddMacLearningEntry(ptr);
313 0 : break;
314 :
315 0 : case MacLearningMgmtRequest::DELETE_MAC:
316 0 : DeleteMacLearningEntry(ptr);
317 0 : break;
318 :
319 176 : case MacLearningMgmtRequest::ADD_DBENTRY:
320 : case MacLearningMgmtRequest::CHANGE_DBENTRY:
321 176 : AddDBEntry(ptr);
322 176 : break;
323 :
324 82 : case MacLearningMgmtRequest::DELETE_DBENTRY:
325 82 : DeleteDBEntry(ptr);
326 82 : break;
327 :
328 0 : case MacLearningMgmtRequest::DELETE_DBENTRY_NO_OP:
329 0 : DeleteDBEntry(ptr, false);
330 0 : break;
331 1 : case MacLearningMgmtRequest::DELETE_ALL_MAC:
332 1 : DeleteAllEntry(ptr);
333 1 : break;
334 :
335 0 : case MacLearningMgmtRequest::ADD_MAC_IP:
336 : case MacLearningMgmtRequest::CHANGE_MAC_IP:
337 0 : AddMacIpLearningEntry(ptr);
338 0 : break;
339 :
340 0 : case MacLearningMgmtRequest::DELETE_MAC_IP:
341 0 : DeleteMacIpLearningEntry(ptr);
342 0 : break;
343 0 : default:
344 0 : assert(0);
345 : }
346 :
347 259 : return true;
348 : }
349 :
350 259 : void MacLearningMgmtManager::Enqueue(MacLearningMgmtRequestPtr &ptr) {
351 259 : request_queue_.Enqueue(ptr);
352 259 : }
353 :
354 : MacLearningMgmtDBEntry*
355 0 : MacLearningMgmtManager::Locate(const std::string &vrf, const MacAddress &mac) {
356 0 : MacLearningMgmtRouteEntry mgmt_rt(vrf, mac);
357 : MacLearningMgmtRouteEntry *mgmt_entry =
358 0 : static_cast<MacLearningMgmtRouteEntry *>(rt_tree_.Find(&mgmt_rt));
359 0 : if (mgmt_entry != NULL) {
360 0 : return mgmt_entry;
361 : }
362 :
363 0 : mgmt_entry = new MacLearningMgmtRouteEntry(vrf, mac);
364 0 : rt_tree_.Add(mgmt_entry);
365 0 : return mgmt_entry;
366 0 : }
367 :
368 59 : bool MacLearningMgmtManager::IsVrfRouteEmpty(const std::string &vrf_name) {
369 :
370 59 : MacLearningMgmtRouteEntry rt_entry(vrf_name, MacAddress::ZeroMac());
371 : MacLearningMgmtRouteEntry *rt_entry_ptr =
372 59 : static_cast<MacLearningMgmtRouteEntry *>(rt_tree_.LowerBound(&rt_entry));
373 59 : if (rt_entry_ptr && rt_entry_ptr->vrf() == vrf_name) {
374 41 : return false;
375 : }
376 :
377 18 : return true;
378 59 : }
379 :
380 : MacLearningMgmtDBEntry*
381 83 : MacLearningMgmtManager::Find(const DBEntry *e) {
382 83 : if (e == NULL) {
383 0 : return NULL;
384 : }
385 :
386 83 : MacLearningMgmtDBEntry *mgmt_entry = NULL;
387 83 : const Interface *intf = dynamic_cast<const Interface *>(e);
388 83 : if (intf != NULL) {
389 16 : MacLearningMgmtIntfEntry mgmt_intf(intf);
390 16 : mgmt_entry = intf_tree_.Find(&mgmt_intf);
391 16 : return mgmt_entry;
392 16 : }
393 :
394 67 : const VrfEntry *vrf = dynamic_cast<const VrfEntry *>(e);
395 67 : if (vrf != NULL) {
396 10 : MacLearningMgmtVrfEntry mgmt_vrf(vrf);
397 10 : mgmt_entry = vrf_tree_.Find(&mgmt_vrf);
398 10 : return mgmt_entry;
399 10 : }
400 :
401 57 : const AgentRoute *rt = dynamic_cast<const AgentRoute *>(e);
402 57 : if (rt != NULL) {
403 50 : MacLearningMgmtRouteEntry mgmt_rt(rt);
404 50 : mgmt_entry = rt_tree_.Find(&mgmt_rt);
405 50 : return mgmt_entry;
406 50 : }
407 :
408 7 : const VnEntry *vn = dynamic_cast<const VnEntry *>(e);
409 7 : if (vn != NULL) {
410 7 : MacLearningMgmtVnEntry mgmt_vn(vn);
411 7 : mgmt_entry = vn_tree_.Find(&mgmt_vn);
412 7 : return mgmt_entry;
413 7 : }
414 :
415 0 : const HealthCheckService *hc = dynamic_cast<const HealthCheckService *>(e);
416 0 : if (hc != NULL) {
417 0 : MacLearningMgmtHcServiceEntry mgmt_hc(hc);
418 0 : mgmt_entry = hc_tree_.Find(&mgmt_hc);
419 0 : return mgmt_entry;
420 0 : }
421 0 : return NULL;
422 : }
423 :
424 :
425 : MacLearningMgmtDBEntry*
426 176 : MacLearningMgmtManager::Locate(const DBEntry *e) {
427 176 : if (e == NULL) {
428 0 : return NULL;
429 : }
430 :
431 176 : MacLearningMgmtDBEntry *mgmt_entry = NULL;
432 176 : const Interface *intf = dynamic_cast<const Interface *>(e);
433 176 : if (intf != NULL) {
434 41 : MacLearningMgmtIntfEntry mgmt_intf(intf);
435 41 : mgmt_entry = intf_tree_.Find(&mgmt_intf);
436 41 : if (mgmt_entry != NULL) {
437 25 : return mgmt_entry;
438 : }
439 :
440 16 : mgmt_entry = new MacLearningMgmtIntfEntry(intf);
441 16 : intf_tree_.Add(mgmt_entry);
442 16 : return mgmt_entry;
443 41 : }
444 :
445 135 : const VrfEntry *vrf = dynamic_cast<const VrfEntry *>(e);
446 135 : if (vrf != NULL) {
447 9 : MacLearningMgmtVrfEntry mgmt_vrf(vrf);
448 9 : mgmt_entry = vrf_tree_.Find(&mgmt_vrf);
449 9 : if (mgmt_entry != NULL) {
450 0 : return mgmt_entry;
451 : }
452 :
453 9 : mgmt_entry = new MacLearningMgmtVrfEntry(vrf);
454 9 : vrf_tree_.Add(mgmt_entry);
455 9 : return mgmt_entry;
456 9 : }
457 :
458 126 : const AgentRoute *rt = dynamic_cast<const AgentRoute *>(e);
459 126 : if (rt != NULL) {
460 119 : MacLearningMgmtRouteEntry mgmt_rt(rt);
461 119 : mgmt_entry = rt_tree_.Find(&mgmt_rt);
462 119 : if (mgmt_entry != NULL) {
463 69 : return mgmt_entry;
464 : }
465 :
466 50 : mgmt_entry = new MacLearningMgmtRouteEntry(rt);
467 50 : rt_tree_.Add(mgmt_entry);
468 50 : return mgmt_entry;
469 119 : }
470 7 : const VnEntry *vn = dynamic_cast<const VnEntry *>(e);
471 7 : if (vn != NULL) {
472 7 : MacLearningMgmtVnEntry mgmt_vn(vn);
473 7 : mgmt_entry = vn_tree_.Find(&mgmt_vn);
474 7 : if (mgmt_entry != NULL) {
475 0 : return mgmt_entry;
476 : }
477 7 : mgmt_entry = new MacLearningMgmtVnEntry(vn);
478 7 : vn_tree_.Add(mgmt_entry);
479 7 : return mgmt_entry;
480 7 : }
481 0 : const HealthCheckService *hc = dynamic_cast<const HealthCheckService *>(e);
482 0 : if (hc != NULL) {
483 0 : MacLearningMgmtHcServiceEntry mgmt_hc(hc);
484 0 : mgmt_entry = hc_tree_.Find(&mgmt_hc);
485 0 : if (mgmt_entry != NULL) {
486 0 : return mgmt_entry;
487 : }
488 0 : mgmt_entry = new MacLearningMgmtHcServiceEntry(hc);
489 0 : hc_tree_.Add(mgmt_entry);
490 0 : return mgmt_entry;
491 :
492 0 : }
493 0 : return NULL;
494 : }
495 :
496 1 : MacLearningMgmtManager::MacLearningMgmtManager(Agent *agent) :
497 1 : agent_(agent), intf_tree_(this), vrf_tree_(this), rt_tree_(this),
498 1 : vn_tree_(NULL), hc_tree_(NULL),
499 1 : request_queue_(agent_->task_scheduler()->GetTaskId(kTaskMacLearningMgmt), 0,
500 : boost::bind(&MacLearningMgmtManager::RequestHandler,
501 1 : this, _1)) {
502 1 : }
|