-
Notifications
You must be signed in to change notification settings - Fork 390
/
bgp_condition_listener.cc
449 lines (386 loc) · 14 KB
/
bgp_condition_listener.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_condition_listener.h"
#include <utility>
#include "base/task_annotations.h"
using std::make_pair;
using std::map;
using std::pair;
using std::set;
//
// Helper class to maintain the WalkRequests
// Contains a map of ConditionMatch object and RequestComplete callback
// pending_walk_list_: List of ConditionObjects requesting walk(not completed)
// current_walk_list_: List of ConditionObjects for which current walk is done
//
class WalkRequest {
public:
typedef map<ConditionMatchPtr,
BgpConditionListener::RequestDoneCb> WalkList;
WalkRequest();
void AddMatchObject(ConditionMatch *obj,
BgpConditionListener::RequestDoneCb cb) {
pair<WalkList::iterator, bool> ret =
pending_walk_list_.insert(make_pair(obj, cb));
if (!ret.second) {
if (ret.first->second.empty()) {
ret.first->second = cb;
}
}
}
//
// When the walk actually starts, pending_walk_list_ entries are moved to
// current_walk_list_.
//
void WalkStarted(DBTableWalker::WalkId id) {
id_ = id;
pending_walk_list_.swap(current_walk_list_);
pending_walk_list_.clear();
}
DBTableWalker::WalkId GetWalkId() const {
return id_;
}
void ResetWalkId() {
id_ = DBTableWalker::kInvalidWalkerId;
}
bool walk_in_progress() {
return (id_ != DBTableWalker::kInvalidWalkerId);
}
//
// Table requires further walk as requests are cached in pending_walk_list_
// during the current table walk
//
bool walk_again() {
return !pending_walk_list_.empty();
}
WalkList *walk_list() {
return ¤t_walk_list_;
}
bool is_walk_pending(ConditionMatch *obj) {
if (pending_walk_list_.empty()) {
return false;
} else {
return (pending_walk_list_.find(ConditionMatchPtr(obj)) !=
pending_walk_list_.end());
}
}
private:
WalkList pending_walk_list_;
WalkList current_walk_list_;
DBTableWalker::WalkId id_;
};
//
// ConditionMatchTableState
// State managed by the BgpConditionListener for each of the table it is
// listening to.
// BgpConditionListener registers for a DBTable when application request
// for ConditionMatch
// BgpConditionListener unregisters from the DBTable when application removes
// the ConditionMatch and all table walks have finished
// Holds a table reference to ensure that table with active walk or listener
// is not deleted
//
class ConditionMatchTableState {
public:
typedef set<ConditionMatchPtr> MatchList;
ConditionMatchTableState(BgpTable *table, DBTableBase::ListenerId id);
~ConditionMatchTableState();
void ManagedDelete() {
}
DBTableBase::ListenerId GetListenerId() const {
return id_;
}
MatchList *match_objects() {
return &match_object_list_;
}
void AddMatchObject(ConditionMatch *obj) {
match_object_list_.insert(ConditionMatchPtr(obj));
}
//
// Mutex required to manager MatchState list for concurrency
//
tbb::mutex &table_state_mutex() {
return table_state_mutex_;
}
private:
tbb::mutex table_state_mutex_;
DBTableBase::ListenerId id_;
MatchList match_object_list_;
LifetimeRef<ConditionMatchTableState> table_delete_ref_;
DISALLOW_COPY_AND_ASSIGN(ConditionMatchTableState);
};
BgpConditionListener::BgpConditionListener(BgpServer *server) :
server_(server),
walk_trigger_(new TaskTrigger(boost::bind(&BgpConditionListener::StartWalk,
this),
TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0)) {
}
//
// AddMatchCondition:
// API to add ConditionMatch object against a table
// All entries present in this table will be matched for this ConditionMatch
// object.[All table partition]
// Match is done either in TableWalk or During Table entry notification
//
void BgpConditionListener::AddMatchCondition(BgpTable *table,
ConditionMatch *obj,
RequestDoneCb cb) {
CHECK_CONCURRENCY("bgp::Config");
ConditionMatchTableState *ts = NULL;
TableMap::iterator loc = map_.find(table);
if (loc == map_.end()) {
DBTableBase::ListenerId id =
table->Register(
boost::bind(&BgpConditionListener::BgpRouteNotify,
this, server(), _1, _2),
"BgpConditionListener");
ts = new ConditionMatchTableState(table, id);
map_.insert(make_pair(table, ts));
} else {
ts = loc->second;
}
ts->AddMatchObject(obj);
TableWalk(table, obj, cb);
}
//
// RemoveMatchCondition:
// API to Remove ConditionMatch object from a table
// All entries present in this table will be matched for this ConditionMatch
// object[All table partition] and notified to application with "DELETE" flag
// All Match notifications after invoking this API is called with "DELETE" flag
//
void BgpConditionListener::RemoveMatchCondition(BgpTable *table,
ConditionMatch *obj,
RequestDoneCb cb) {
CHECK_CONCURRENCY("bgp::Config");
obj->SetDeleted();
TableWalk(table, obj, cb);
}
//
// MatchState
// BgpConditionListener will hold this as DBState for each BgpRoute
//
class MatchState : public DBState {
public:
typedef map<ConditionMatchPtr, ConditionMatchState *> MatchStateList;
private:
friend class BgpConditionListener;
MatchStateList list_;
};
//
// CheckMatchState
// API to check if MatchState is added by module registering ConditionMatch
// object.
//
bool BgpConditionListener::CheckMatchState(BgpTable *table, BgpRoute *route,
ConditionMatch *obj) {
TableMap::iterator loc = map_.find(table);
ConditionMatchTableState *ts = loc->second;
tbb::mutex::scoped_lock lock(ts->table_state_mutex());
// Get the DBState.
MatchState *dbstate =
static_cast<MatchState *>(route->GetState(table, ts->GetListenerId()));
if (dbstate == NULL)
return false;
// Index with ConditionMatch object to check.
MatchState::MatchStateList::iterator it =
dbstate->list_.find(ConditionMatchPtr(obj));
return (it != dbstate->list_.end()) ? true : false;
}
//
// GetMatchState
// API to fetch MatchState added by module registering ConditionMatch object
// MatchState is maintained as Map of ConditionMatch object and State in
// DBState added BgpConditionListener module
//
ConditionMatchState * BgpConditionListener::GetMatchState(BgpTable *table,
BgpRoute *route,
ConditionMatch *obj) {
TableMap::iterator loc = map_.find(table);
ConditionMatchTableState *ts = loc->second;
tbb::mutex::scoped_lock lock(ts->table_state_mutex());
// Get the DBState
MatchState *dbstate =
static_cast<MatchState *>(route->GetState(table, ts->GetListenerId()));
if (dbstate == NULL) return NULL;
// Index with ConditionMatch object to retrieve the MatchState
MatchState::MatchStateList::iterator it =
dbstate->list_.find(ConditionMatchPtr(obj));
return (it != dbstate->list_.end()) ? it->second : NULL;
}
//
// SetMatchState
// API for module registering ConditionMatch object to add MatchState
//
void BgpConditionListener::SetMatchState(BgpTable *table, BgpRoute *route,
ConditionMatch *obj,
ConditionMatchState *state) {
TableMap::iterator loc = map_.find(table);
ConditionMatchTableState *ts = loc->second;
tbb::mutex::scoped_lock lock(ts->table_state_mutex());
// Get the DBState
MatchState *dbstate =
static_cast<MatchState *>(route->GetState(table, ts->GetListenerId()));
if (!dbstate) {
// Add new DBState when first application requests for MatchState
dbstate = new MatchState();
route->SetState(table, ts->GetListenerId(), dbstate);
} else {
// Add Match to the existing list
MatchState::MatchStateList::iterator it =
dbstate->list_.find(ConditionMatchPtr(obj));
assert(it == dbstate->list_.end());
}
dbstate->list_.insert(make_pair(obj, state));
obj->IncrementNumMatchstate();
}
//
// RemoveMatchState
// Clear the module specific MatchState
//
void BgpConditionListener::RemoveMatchState(BgpTable *table, BgpRoute *route,
ConditionMatch *obj) {
TableMap::iterator loc = map_.find(table);
ConditionMatchTableState *ts = loc->second;
tbb::mutex::scoped_lock lock(ts->table_state_mutex());
// Get the DBState
MatchState *dbstate =
static_cast<MatchState *>(route->GetState(table, ts->GetListenerId()));
MatchState::MatchStateList::iterator it =
dbstate->list_.find(ConditionMatchPtr(obj));
assert(it != dbstate->list_.end());
dbstate->list_.erase(it);
obj->DecrementNumMatchstate();
if (dbstate->list_.empty()) {
// Remove the DBState when last module removes the MatchState
route->ClearState(table, ts->GetListenerId());
delete dbstate;
}
}
//
// Add ConditionMatch object to pending_walk_list_
// and trigger the task to actually start the walk
//
void BgpConditionListener::TableWalk(BgpTable *table, ConditionMatch *obj,
RequestDoneCb cb) {
CHECK_CONCURRENCY("bgp::Config");
WalkRequestMap::iterator loc = walk_map_.find(table);
WalkRequest *walk_req = NULL;
if (loc != walk_map_.end()) {
walk_req = loc->second;
walk_req->AddMatchObject(obj, cb);
} else {
walk_req = new WalkRequest();
walk_map_.insert(make_pair(table, walk_req));
walk_req->AddMatchObject(obj, cb);
}
walk_trigger_->Set();
}
bool BgpConditionListener::StartWalk() {
CHECK_CONCURRENCY("bgp::Config");
DBTableWalker::WalkCompleteFn walk_complete
= boost::bind(&BgpConditionListener::WalkDone, this, _1);
DBTableWalker::WalkFn walker
= boost::bind(&BgpConditionListener::BgpRouteNotify, this, server(),
_1, _2);
for (WalkRequestMap::iterator it = walk_map_.begin();
it != walk_map_.end(); ++it) {
if (it->second->walk_in_progress()) {
continue;
}
DB *db = server()->database();
DBTableWalker::WalkId id =
db->GetWalker()->WalkTable(it->first, NULL, walker, walk_complete);
it->second->WalkStarted(id);
}
return true;
}
// Table listener
bool BgpConditionListener::BgpRouteNotify(BgpServer *server,
DBTablePartBase *root,
DBEntryBase *entry) {
BgpTable *bgptable = static_cast<BgpTable *>(root->parent());
BgpRoute *rt = static_cast<BgpRoute *> (entry);
// Either the route is deleted or no valid path exists
bool del_rt = !rt->IsUsable();
TableMap::iterator loc = map_.find(bgptable);
assert(loc != map_.end());
ConditionMatchTableState *ts = loc->second;
DBTableBase::ListenerId id = ts->GetListenerId();
assert(id != DBTableBase::kInvalidId);
for (ConditionMatchTableState::MatchList::iterator match_obj_it =
ts->match_objects()->begin();
match_obj_it != ts->match_objects()->end(); match_obj_it++) {
bool deleted = false;
if ((*match_obj_it)->deleted() || del_rt) {
deleted = true;
}
(*match_obj_it)->Match(server, bgptable, rt, deleted);
}
return true;
}
//
// WalkComplete function
// At the end of the walk reset the WalkId.
// Invoke the RequestDoneCb for all objects for which walk was started
// Clear the current_walk_list_ and check whether the table needs to be
// walked again.
//
void BgpConditionListener::WalkDone(DBTableBase *table) {
BgpTable *bgptable = static_cast<BgpTable *>(table);
WalkRequestMap::iterator it = walk_map_.find(bgptable);
assert(it != walk_map_.end());
WalkRequest *walk_state = it->second;
walk_state->ResetWalkId();
//
// Invoke the RequestDoneCb after the TableWalk
//
for (WalkRequest::WalkList::iterator walk_it =
walk_state->walk_list()->begin();
walk_it != walk_state->walk_list()->end(); ++walk_it) {
// If application has registered a WalkDone callback, invoke it
if (!walk_it->second.empty())
walk_it->second(bgptable, walk_it->first.get());
}
walk_state->walk_list()->clear();
if (walk_state->walk_again()) {
// More walk requests are pending
walk_trigger_->Set();
} else {
delete walk_state;
walk_map_.erase(it);
}
}
void BgpConditionListener::UnregisterMatchCondition(BgpTable *bgptable,
ConditionMatch *obj) {
TableMap::iterator loc = map_.find(bgptable);
assert(loc != map_.end());
ConditionMatchTableState *ts = loc->second;
WalkRequestMap::iterator it = walk_map_.find(bgptable);
WalkRequest *walk_state = NULL;
if (it != walk_map_.end()) {
walk_state = it->second;
}
//
// Wait for Walk completion of deleted ConditionMatch object
//
if ((!walk_state || !walk_state->is_walk_pending(obj)) &&
obj->deleted()) {
ts->match_objects()->erase(obj);
}
if (ts->match_objects()->empty()) {
bgptable->Unregister(ts->GetListenerId());
map_.erase(bgptable);
delete ts;
}
}
ConditionMatchTableState::ConditionMatchTableState(BgpTable *table,
DBTableBase::ListenerId id)
: id_(id), table_delete_ref_(this, table->deleter()) {
assert(table->deleter() != NULL);
}
ConditionMatchTableState::~ConditionMatchTableState() {
}
WalkRequest::WalkRequest() : id_(DBTableWalker::kInvalidWalkerId) {
}