Line data Source code
1 : /*
2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 :
6 : #include "bgp/bgp_update_monitor.h"
7 :
8 : #include "base/task_annotations.h"
9 : #include "bgp/bgp_ribout_updates.h"
10 : #include "bgp/bgp_table.h"
11 : #include "bgp/bgp_update_queue.h"
12 :
13 146263 : RouteUpdatePtr::RouteUpdatePtr(RouteUpdate *rt_update) : rt_update_(rt_update) {
14 146263 : }
15 :
16 353168 : RouteUpdatePtr::~RouteUpdatePtr() {
17 353168 : }
18 :
19 3216 : RibUpdateMonitor::RibUpdateMonitor(RibOut *ribout, QueueVec *queue_vec)
20 3216 : : ribout_(ribout), queue_vec_(queue_vec) {
21 3216 : }
22 :
23 : //
24 : // Helper routine for GetRouteStateAndDequeue to handle a RouteUpdate. Do
25 : // nothing if it's a duplicate.
26 : //
27 : // Return a pointer to the DBState and update duplicate as appropriate.
28 : //
29 0 : DBState *RibUpdateMonitor::GetRouteUpdateAndDequeue(DBEntryBase *db_entry,
30 : RouteUpdate *rt_update, UpdateCmp cmp, bool *duplicate) {
31 0 : CHECK_CONCURRENCY("db::DBTable");
32 :
33 : // Compare desired state with current update. If the desired state
34 : // is a NOP just return without modifying the queue.
35 0 : if (rt_update->queue_id() == RibOutUpdates::QUPDATE) {
36 0 : *duplicate = cmp(rt_update);
37 0 : if (*duplicate) {
38 0 : return NULL;
39 : }
40 : }
41 :
42 : // Set the state on the DBEntryBase and remove the RouteUpdate from
43 : // the queue. Change the queue to QUPDATE since that's where we will
44 : // re-enqueue the RouteUpdate, if we do it.
45 0 : db_entry->SetState(ribout_->table(), ribout_->listener_id(), rt_update);
46 0 : UpdateQueue *queue = queue_vec_->at(rt_update->queue_id());
47 0 : queue->Dequeue(rt_update);
48 0 : rt_update->set_queue_id(RibOutUpdates::QUPDATE);
49 0 : return rt_update;
50 : }
51 :
52 : //
53 : // Helper routine for GetRouteStateAndDequeue to handle a UpdateList.
54 : //
55 : // Since an update operation overrides any join/refresh state, get rid of
56 : // RouteUpdates that are not for QUPDATE.
57 : //
58 : // Note that we should NOT check for duplicate state in QUPDATE and bail.
59 : // Doing so could result in a failure to remove pending RouteUpdates from
60 : // QBULK which are no longer required.
61 : //
62 : // Return a pointer to the RouteUpdate for QUPDATE if it exists, else
63 : // Return a pointer to new RouteState if the UpdateList has history, else
64 : // Return NULL.
65 : //
66 0 : DBState *RibUpdateMonitor::GetUpdateListAndDequeue(DBEntryBase *db_entry,
67 : UpdateList *uplist) {
68 0 : CHECK_CONCURRENCY("db::DBTable");
69 :
70 : DBState *dbstate;
71 0 : RouteUpdate *rt_update = uplist->FindUpdate(RibOutUpdates::QUPDATE);
72 0 : if (rt_update) {
73 : // There's a RouteUpdate for QUPDATE. Remove it from the UpdateList
74 : // and move the history to it. The entry will be reused as if that
75 : // was the DBState instead of the UpdateList.
76 0 : uplist->RemoveUpdate(rt_update);
77 0 : queue_vec_->at(rt_update->queue_id())->Dequeue(rt_update);
78 0 : uplist->MoveHistory(rt_update);
79 0 : dbstate = rt_update;
80 0 : } else if (!uplist->History()->empty()) {
81 : // There's no RouteUpdate for QUPDATE, but the history is not empty.
82 : // Move the history to a new RouteState.
83 0 : RouteState *rstate = new RouteState();
84 0 : uplist->MoveHistory(rstate);
85 0 : dbstate = rstate;
86 : } else {
87 : // There's no RouteUpdate for QUPDATE and the history is empty. We
88 : // can simply pretend that there was no DBState after we get rid
89 : // of the UpdateList.
90 0 : dbstate = NULL;
91 : }
92 :
93 : // Get rid of all other RouteUpdates and the UpdateList itself. Note
94 : // that the RouteUpdate for QUPDATE, if any, has already been removed
95 : // from the UpdateList at this point.
96 0 : UpdateList::List *list = uplist->GetList();
97 0 : for (UpdateList::List::iterator iter = list->begin();
98 0 : iter != list->end(); iter++) {
99 0 : RouteUpdate *temp_rt_update = *iter;
100 0 : queue_vec_->at(temp_rt_update->queue_id())->Dequeue(temp_rt_update);
101 0 : delete temp_rt_update;
102 : }
103 :
104 : // Update the DBState and return it.
105 0 : if (dbstate) {
106 0 : db_entry->SetState(ribout_->table(), ribout_->listener_id(), dbstate);
107 : } else {
108 0 : db_entry->ClearState(ribout_->table(), ribout_->listener_id());
109 : }
110 0 : return dbstate;
111 : }
112 :
113 : //
114 : // Get the DBState for the DBEntryBase and dequeue it from an UpdateQueue
115 : // if required. The DBState could be a RouteState, a RouteUpdate or an
116 : // UpdateList. If it's a RouteState, it won't be on an UpdateQueue. If
117 : // it's a RouteUpdate or an UpdateList, see if it's a duplicate i.e. the
118 : // desired state is the same as the currently advertised state.
119 : //
120 : // This routine is used by the export module to obtain exclusive access to
121 : // the RouteUpdate or UpdateList. The access is exclusive by definition if
122 : // there's no DBState or it's a RouteState.
123 : //
124 : // Return a pointer to the DBState and update duplicate as appropriate.
125 : //
126 99709 : DBState *RibUpdateMonitor::GetDBStateAndDequeue(DBEntryBase *db_entry,
127 : UpdateCmp cmp, bool *duplicate) {
128 99709 : CHECK_CONCURRENCY("db::DBTable");
129 :
130 99478 : *duplicate = false;
131 :
132 : // Don't need to bother going through the monitor if there's no DBState.
133 : DBState *dbstate =
134 99478 : db_entry->GetState(ribout_->table(), ribout_->listener_id());
135 100381 : if (dbstate == NULL) {
136 68065 : return dbstate;
137 : }
138 :
139 : // Go through the monitor and handle the DBState as appropriate. Note
140 : // that we still need to check for the DBState being NULL as things may
141 : // have changed after the check made above.
142 : while (true) {
143 : // Get the DBState; bail if there's no existing state.
144 : DBState *dbstate =
145 32316 : db_entry->GetState(ribout_->table(), ribout_->listener_id());
146 32327 : if (dbstate == NULL) {
147 0 : return dbstate;
148 : }
149 :
150 : // Return if it's a RouteState.
151 32327 : RouteState *rstate = dynamic_cast<RouteState *>(dbstate);
152 32327 : if (rstate != NULL) {
153 32333 : return rstate;
154 : }
155 :
156 : // Handle the case where it's a RouteUpdate.
157 0 : RouteUpdate *rt_update = dynamic_cast<RouteUpdate *>(dbstate);
158 0 : if (rt_update != NULL) {
159 0 : return GetRouteUpdateAndDequeue(db_entry, rt_update, cmp,
160 0 : duplicate);
161 : }
162 :
163 : // Handle the case where it's a UpdateList.
164 0 : UpdateList *uplist = dynamic_cast<UpdateList *>(dbstate);
165 : DBState *db_state;
166 0 : assert(uplist);
167 0 : db_state = GetUpdateListAndDequeue(db_entry, uplist);
168 0 : delete uplist;
169 0 : return db_state;
170 : }
171 :
172 : assert(false);
173 : return NULL;
174 : }
175 :
176 : //
177 : // Helper routine for GetPeerSetCurrentAndScheduled to handle a RouteState.
178 : // Go through all the AdvertiseInfo to build the bitset.
179 : //
180 35277 : static void RouteStateCurrent(const RouteState *rstate,
181 : RibPeerSet *mcurrent) {
182 35277 : CHECK_CONCURRENCY("db::DBTable");
183 :
184 34790 : const AdvertiseSList &adv_slist = rstate->Advertised();
185 34784 : for (AdvertiseSList::List::const_iterator iter = adv_slist->begin();
186 138881 : iter != adv_slist->end(); ++iter) {
187 69624 : mcurrent->Set(iter->bitset);
188 : }
189 34536 : }
190 :
191 : //
192 : // Helper routine for GetPeerSetCurrentAndScheduled to handle a RouteUpdate.
193 : // Go through all the AdvertiseInfo and UpdateInfo to build the bitset.
194 : //
195 : // Do not consider the UpdateInfos if the given queue_id does not match the
196 : // one in the RouteUpdate. Note that QCOUNT is considered to be a wildcard.
197 : //
198 6297 : static void RouteUpdateCurrentAndScheduled(const RouteUpdate *rt_update,
199 : int queue_id, RibPeerSet *mcurrent, RibPeerSet *mscheduled) {
200 6297 : CHECK_CONCURRENCY("db::DBTable");
201 :
202 6254 : const AdvertiseSList &adv_slist = rt_update->History();
203 6254 : for (AdvertiseSList::List::const_iterator iter = adv_slist->begin();
204 20486 : iter != adv_slist->end(); ++iter) {
205 7982 : mcurrent->Set(iter->bitset);
206 : }
207 :
208 6251 : if (queue_id != RibOutUpdates::QCOUNT && queue_id != rt_update->queue_id())
209 0 : return;
210 :
211 6251 : const UpdateInfoSList &uinfo_slist = rt_update->Updates();
212 6262 : for (UpdateInfoSList::List::const_iterator iter = uinfo_slist->begin();
213 25049 : iter != uinfo_slist->end(); ++iter) {
214 12526 : mscheduled->Set(iter->target);
215 : }
216 : }
217 :
218 : //
219 : // Helper routine for GetPeerSetCurrentAndScheduled to handle a UpdateList.
220 : // Go through all AdvertiseInfo in the UpdateList and then each RouteUpdate
221 : // in the UpdateList to build the bitset.
222 : //
223 0 : static void UpdateListCurrentAndScheduled(const UpdateList *uplist,
224 : int queue_id, RibPeerSet *mcurrent, RibPeerSet *mscheduled) {
225 0 : CHECK_CONCURRENCY("db::DBTable");
226 :
227 0 : const AdvertiseSList &adv_slist = uplist->History();
228 0 : for (AdvertiseSList::List::const_iterator iter = adv_slist->begin();
229 0 : iter != adv_slist->end(); ++iter) {
230 0 : mcurrent->Set(iter->bitset);
231 : }
232 :
233 0 : const UpdateList::List *list = uplist->GetList();
234 0 : for (UpdateList::List::const_iterator iter = list->begin();
235 0 : iter != list->end(); ++iter) {
236 0 : RouteUpdateCurrentAndScheduled(*iter, queue_id, mcurrent, mscheduled);
237 : }
238 0 : }
239 :
240 : //
241 : // Build the RibPeerSet of peers that are currently advertising the given
242 : // DBEntryBase or are scheduled to advertise it. The mcurrent parameter is
243 : // filled with the contents of the advertised bitmask (history) while the
244 : // mscheduled parameter is filled with the bitmask of updates in the queue.
245 : //
246 : // Return false if there's no associated DBState or if it's a RouteState
247 : // i.e. there's no pending state to be advertised.
248 : //
249 60861 : bool RibUpdateMonitor::GetPeerSetCurrentAndScheduled(DBEntryBase *db_entry,
250 : int queue_id, RibPeerSet *mcurrent, RibPeerSet *mscheduled) {
251 60861 : CHECK_CONCURRENCY("db::DBTable");
252 :
253 : // Don't need to bother going through the monitor if there's no DBState.
254 : DBState *dbstate =
255 60622 : db_entry->GetState(ribout_->table(), ribout_->listener_id());
256 61224 : if (dbstate == NULL) {
257 19690 : return false;
258 : }
259 :
260 : // Go through the monitor and handle the DBState as appropriate. Note
261 : // that we still need to check for the DBState being NULL as things may
262 : // have changed after the check made above.
263 : while (true) {
264 : // Get the DBState; bail if there's no existing state.
265 41509 : DBState *dbstate = db_entry->GetState(ribout_->table(),
266 41534 : ribout_->listener_id());
267 41575 : if (dbstate == NULL) {
268 0 : return false;
269 : }
270 :
271 : // Handle the case where it's a RouteState.
272 41575 : RouteState *rstate = dynamic_cast<RouteState *>(dbstate);
273 41575 : if (rstate != NULL) {
274 35284 : RouteStateCurrent(rstate, mcurrent);
275 34695 : return false;
276 : }
277 :
278 : // Handle the case where it's a RouteUpdate.
279 6291 : RouteUpdate *rt_update = dynamic_cast<RouteUpdate *>(dbstate);
280 6291 : if (rt_update != NULL) {
281 6297 : RouteUpdateCurrentAndScheduled(rt_update, queue_id,
282 : mcurrent, mscheduled);
283 6265 : break;
284 : }
285 :
286 : // Handle the case where it's a UpdateList.
287 0 : UpdateList *uplist = dynamic_cast<UpdateList *>(dbstate);
288 0 : if (uplist != NULL) {
289 0 : UpdateListCurrentAndScheduled(uplist, queue_id,
290 : mcurrent, mscheduled);
291 0 : break;
292 : }
293 :
294 : // Unknown DBState.
295 0 : assert(false);
296 : }
297 :
298 6265 : return true;
299 : }
300 :
301 : //
302 : // Helper routine for MergeUpdate to handle case where the current DBState
303 : // is a RouteState.
304 : //
305 : // Move the previous history from the RouteState to the RouteUpdate and
306 : // enqueue it.
307 : //
308 : // Return true if the BgpUpdateSender needs to trigger a tail dequeue for
309 : // the (RibOut, QueueId).
310 : //
311 0 : bool RibUpdateMonitor::RouteStateMergeUpdate(DBEntryBase *db_entry,
312 : RouteUpdate *rt_update, RouteState *rstate) {
313 0 : CHECK_CONCURRENCY("db::DBTable");
314 :
315 0 : rstate->MoveHistory(rt_update);
316 0 : delete rstate;
317 0 : return EnqueueUpdate(db_entry, rt_update);
318 : }
319 :
320 : //
321 : // Helper routine for MergeUpdate to handle case where the current DBState
322 : // is a RouteUpdate.
323 : //
324 : // If the new RouteUpdate is for the same queue as the current RouteUpdate,
325 : // dequeue the current, merge in the UpdateInfo from the new and enqueue it
326 : // again. Otherwise, build an UpdateList containing both RouteUpdates and
327 : // enqueue the new RouteUpdate.
328 : //
329 : // Return true if the BgpUpdateSender needs to trigger a tail dequeue for
330 : // the (RibOut, QueueId).
331 : //
332 0 : bool RibUpdateMonitor::RouteUpdateMergeUpdate(DBEntryBase *db_entry,
333 : RouteUpdate *rt_update, RouteUpdate *current_rt_update) {
334 0 : CHECK_CONCURRENCY("db::DBTable");
335 :
336 0 : if (current_rt_update->queue_id() == rt_update->queue_id()) {
337 0 : UpdateQueue *queue = queue_vec_->at(current_rt_update->queue_id());
338 0 : queue->Dequeue(current_rt_update);
339 0 : current_rt_update->MergeUpdateInfo(rt_update->Updates());
340 0 : assert(rt_update->Updates()->empty());
341 0 : delete rt_update;
342 0 : return EnqueueUpdate(db_entry, current_rt_update);
343 : } else {
344 0 : UpdateList *uplist = current_rt_update->MakeUpdateList();
345 0 : uplist->AddUpdate(rt_update);
346 0 : return EnqueueUpdate(db_entry, rt_update, uplist);
347 : }
348 : }
349 :
350 : //
351 : // Helper routine for MergeUpdate to handle case where the current DBState
352 : // is a UpdateList.
353 : //
354 : // Figure out if the UpdateList already has a RouteUpdate for this queue.
355 : // If so, dequeue the existing RouteUpdate, merge the UpdateInfo from the
356 : // new one and enqueue the existing RouteUpdate again. Otherwise, add the
357 : // new RouteUpdate to the UpdateList and enqueue the RouteUpdate.
358 : //
359 : // Return true if the BgpUpdateSender needs to trigger a tail dequeue for
360 : // the (RibOut, QueueId).
361 : //
362 0 : bool RibUpdateMonitor::UpdateListMergeUpdate(DBEntryBase *db_entry,
363 : RouteUpdate *rt_update, UpdateList *uplist) {
364 0 : CHECK_CONCURRENCY("db::DBTable");
365 :
366 0 : RouteUpdate *current_rt_update = uplist->FindUpdate(rt_update->queue_id());
367 0 : if (current_rt_update) {
368 0 : UpdateQueue *queue = queue_vec_->at(current_rt_update->queue_id());
369 0 : queue->Dequeue(current_rt_update);
370 0 : current_rt_update->MergeUpdateInfo(rt_update->Updates());
371 0 : assert(rt_update->Updates()->empty());
372 0 : delete rt_update;
373 0 : return EnqueueUpdate(db_entry, current_rt_update, uplist);
374 : } else {
375 0 : uplist->AddUpdate(rt_update);
376 0 : return EnqueueUpdate(db_entry, rt_update, uplist);
377 : }
378 : }
379 :
380 : //
381 : // Take the desired state as represented by the RouteUpdate and merge it with
382 : // any previously advertised state for the DBEntryBase. Note that the previous
383 : // state could be for a different queue and/or other peers in the RibOut.
384 : //
385 : // Return true if the BgpUpdateSender needs to trigger a tail dequeue for
386 : // the (RibOut, QueueId).
387 : //
388 2965 : bool RibUpdateMonitor::MergeUpdate(DBEntryBase *db_entry,
389 : RouteUpdate *rt_update) {
390 2965 : CHECK_CONCURRENCY("db::DBTable");
391 :
392 : // Go through the monitor and handle the DBState as necessary. Need
393 : // to use the monitor even if there's no DBState in order to protect
394 : // against race conditions which could result in an incorrect return
395 : // value.
396 : while (true) {
397 : DBState *dbstate =
398 2965 : db_entry->GetState(ribout_->table(), ribout_->listener_id());
399 :
400 : // Handle the case where there's no DBState. Simply add the new
401 : // RouteUpdate to the queue.
402 2965 : if (dbstate == NULL) {
403 2965 : return EnqueueUpdate(db_entry, rt_update);
404 : }
405 :
406 : // Handle the case where it's a RouteState.
407 0 : RouteState *rstate = dynamic_cast<RouteState *>(dbstate);
408 0 : if (rstate != NULL) {
409 0 : return RouteStateMergeUpdate(db_entry, rt_update, rstate);
410 : }
411 :
412 : // Handle the case where it's a RouteUpdate.
413 0 : RouteUpdate *current_rt_update = dynamic_cast<RouteUpdate *>(dbstate);
414 0 : if (current_rt_update != NULL) {
415 0 : return RouteUpdateMergeUpdate(db_entry, rt_update,
416 0 : current_rt_update);
417 : }
418 :
419 : // Handle the case where it's a UpdateList.
420 0 : UpdateList *uplist = dynamic_cast<UpdateList *>(dbstate);
421 0 : if (uplist != NULL) {
422 0 : return UpdateListMergeUpdate(db_entry, rt_update, uplist);
423 : }
424 :
425 : // Unknown DBState.
426 0 : assert(false);
427 : }
428 :
429 : assert(false);
430 : return false;
431 : }
432 :
433 : //
434 : // Traipse through all the AdvertiseInfo elements in the list and clear the
435 : // RibPeerSet. If the RibPeerSet in an element becomes empty, remove it from
436 : // the list container and get rid of the element.
437 : //
438 20416 : void RibUpdateMonitor::AdvertiseSListClearBits(AdvertiseSList &adv_slist,
439 : const RibPeerSet &clear) {
440 20416 : CHECK_CONCURRENCY("db::DBTable");
441 :
442 20441 : for (AdvertiseSList::List::iterator iter = adv_slist->begin();
443 82537 : iter != adv_slist->end(); ) {
444 20804 : iter->bitset.Reset(clear);
445 20782 : if (iter->bitset.empty()) {
446 35129 : iter = adv_slist->erase_and_dispose(iter, AdvertiseInfoDisposer());
447 : } else {
448 9098 : iter++;
449 : }
450 : }
451 20419 : }
452 :
453 : //
454 : // Traipse through all the UpdateInfo elements in the list and clear the
455 : // RibPeerSet. If the RibPeerSet in an element becomes empty, remove it
456 : // from the set and list containers and get rid of the element.
457 : //
458 2963 : void RibUpdateMonitor::UpdateInfoSListClearBits(UpdateInfoSList &uinfo_slist,
459 : const RibPeerSet &clear) {
460 2963 : CHECK_CONCURRENCY("db::DBTable");
461 :
462 2945 : for (UpdateInfoSList::List::iterator iter = uinfo_slist->begin();
463 11822 : iter != uinfo_slist->end(); ) {
464 2943 : iter->target.Reset(clear);
465 2947 : if (iter->target.empty()) {
466 2949 : RouteUpdate *rt_update = iter->update;
467 2949 : UpdateQueue *queue = queue_vec_->at(rt_update->queue_id());
468 5900 : queue->AttrDequeue(iter.operator->());
469 8895 : iter = uinfo_slist->erase_and_dispose(iter, UpdateInfoDisposer());
470 : } else {
471 0 : iter++;
472 : }
473 : }
474 2963 : }
475 :
476 : //
477 : // Helper routine for ClearPeerSetCurrentAndScheduled to handle case where
478 : // the current DBState is a RouteState.
479 : //
480 : // Clean up the AdvertiseInfos corresponding to the peers in RibPeerSet.
481 : //
482 17600 : void RibUpdateMonitor::RouteStateClearPeerSet(DBEntryBase *db_entry,
483 : RouteState *rstate, const RibPeerSet &mleave) {
484 17600 : CHECK_CONCURRENCY("db::DBTable");
485 :
486 : // Clear the bits for each element in the AdvertiseSList.
487 17469 : AdvertiseSListClearBits(rstate->Advertised(), mleave);
488 :
489 : // Get rid of the RouteState itself if it's empty.
490 17531 : if (rstate->Advertised()->empty()) {
491 8769 : db_entry->ClearState(ribout_->table(), ribout_->listener_id());
492 8803 : delete rstate;
493 : }
494 17511 : }
495 :
496 : //
497 : // Helper routine for ClearPeerSetCurrentAndScheduled to handle case where
498 : // the current DBState is a RouteUpdate.
499 : //
500 : // Clean up the UpdateInfos corresponding to the peers in RibPeerSet.
501 : //
502 : // Return true if rt_update has to be deleted, false otherwise
503 : //
504 2978 : bool RibUpdateMonitor::RouteUpdateClearPeerSet(DBEntryBase *db_entry,
505 : RouteUpdate *rt_update, const RibPeerSet &mleave) {
506 2978 : CHECK_CONCURRENCY("db::DBTable");
507 :
508 : // Clear the bits for each element in the AdvertiseSList.
509 2952 : AdvertiseSListClearBits(rt_update->History(), mleave);
510 :
511 : // Clear the bits for each element in the UpdateInfoSList.
512 2965 : UpdateInfoSListClearBits(rt_update->Updates(), mleave);
513 :
514 : // Update the DBstate for the DBentry as appropriate.
515 2966 : if (!rt_update->Updates()->empty()) {
516 : // There are more scheduled updates, do nothing.
517 0 : return false;
518 : }
519 :
520 2964 : if (!rt_update->History()->empty()) {
521 : // No more scheduled updates but there are current updates. Dequeue
522 : // the RouteUpdate, move the history to a new RouteState and get rid
523 : // of the RouteUpdate.
524 350 : DequeueUpdate(rt_update);
525 351 : RouteState *rstate = new RouteState;
526 350 : rt_update->MoveHistory(rstate);
527 350 : db_entry->SetState(ribout_->table(), ribout_->listener_id(), rstate);
528 : } else {
529 : // No more scheduled or current updates. Dequeue the RouteUpdate,
530 : // clear the state on the DBEntry and get rid of the RouteUpdate.
531 2611 : DequeueUpdate(rt_update);
532 2603 : db_entry->ClearState(ribout_->table(), ribout_->listener_id());
533 : }
534 :
535 2979 : return true;
536 : }
537 :
538 : //
539 : // Helper routine for ClearPeerSetCurrentAndScheduled to handle case where
540 : // the current DBState is a UpdateList.
541 : //
542 : // Return true if uplist has to be deleted, false otherwise
543 : //
544 0 : bool RibUpdateMonitor::UpdateListClearPeerSet(DBEntryBase *db_entry,
545 : UpdateList *uplist, const RibPeerSet &mleave) {
546 0 : CHECK_CONCURRENCY("db::DBTable");
547 :
548 0 : UpdateList::List *list = uplist->GetList();
549 :
550 : // Clear the bits for each element in the AdvertiseSList.
551 0 : AdvertiseSListClearBits(uplist->History(), mleave);
552 :
553 : // Clear the bits for each element in the UpdateInfoSList for each
554 : // RouteUpdate. If a RouteUpdate becomes empty as a result, get rid
555 : // of it.
556 0 : for (UpdateList::List::iterator iter = list->begin();
557 0 : iter != list->end(); ) {
558 0 : RouteUpdate *rt_update = *iter++;
559 0 : UpdateInfoSListClearBits(rt_update->Updates(), mleave);
560 0 : if (rt_update->Updates()->empty()) {
561 0 : uplist->RemoveUpdate(rt_update);
562 0 : DequeueUpdate(rt_update);
563 0 : delete rt_update;
564 : }
565 : }
566 :
567 : // Update the DBstate for the DBentry as appropriate.
568 0 : if (list->size() > 1) {
569 : // There's multiple RouteUpdates on the UpdateList, do nothing.
570 0 : return false;
571 0 : } else if (list->size() == 1) {
572 : // There's exactly 1 RouteUpdate on the UpdateList. Downgrade
573 : // the UpdateList to a RouteUpdate and get rid of UpdateList.
574 : // Note that the history will be moved to the RouteUpdate as
575 : // part of the downgrade.
576 0 : RouteUpdate *rt_update = uplist->MakeRouteUpdate();
577 0 : assert(rt_update);
578 0 : db_entry->SetState(ribout_->table(), ribout_->listener_id(), rt_update);
579 0 : } else if (!uplist->History()->empty()) {
580 : // There's no RouteUpdates on the UpdateList, but the history
581 : // is not empty. Move the history to a new RouteState and get
582 : // rid of the UpdateList.
583 0 : RouteState *rstate = new RouteState;
584 0 : uplist->MoveHistory(rstate);
585 0 : db_entry->SetState(ribout_->table(), ribout_->listener_id(), rstate);
586 : } else {
587 : // There's no RouteUpdates on the UpdateList and the history is
588 : // empty. Clear state on the DBEntry and get rid of UpdateList.
589 0 : db_entry->ClearState(ribout_->table(), ribout_->listener_id());
590 : }
591 :
592 0 : return true;
593 : }
594 :
595 : //
596 : // Cancel all scheduled updates and clean up AdvertiseInfo corresponding
597 : // to any current updates for the peers in the RibPeerSet.
598 : //
599 20392 : void RibUpdateMonitor::ClearPeerSetCurrentAndScheduled(DBEntryBase *db_entry,
600 : const RibPeerSet &mleave) {
601 20392 : CHECK_CONCURRENCY("db::DBTable");
602 :
603 : // Don't need to bother going through the monitor if there's no DBState.
604 : DBState *dbstate =
605 20391 : db_entry->GetState(ribout_->table(), ribout_->listener_id());
606 20583 : if (dbstate == NULL) {
607 0 : return;
608 : }
609 :
610 : // Go through the monitor and handle the DBState as appropriate. Note
611 : // that we still need to check for the DBState being NULL as things may
612 : // have changed after the check made above.
613 : while (true) {
614 : DBState *dbstate =
615 20583 : db_entry->GetState(ribout_->table(), ribout_->listener_id());
616 :
617 : // Handle the case where there's no DBState.
618 20584 : if (dbstate == NULL) {
619 0 : return;
620 : }
621 :
622 : // Handle the case where it's a RouteState.
623 20584 : RouteState *rstate = dynamic_cast<RouteState *>(dbstate);
624 20584 : if (rstate != NULL) {
625 17604 : RouteStateClearPeerSet(db_entry, rstate, mleave);
626 17509 : return;
627 : }
628 :
629 : // Handle the case where it's a RouteUpdate.
630 2980 : RouteUpdate *rt_update = dynamic_cast<RouteUpdate *>(dbstate);
631 2980 : if (rt_update != NULL) {
632 : bool delete_rt_update =
633 2978 : RouteUpdateClearPeerSet(db_entry, rt_update, mleave);
634 2979 : if (delete_rt_update) {
635 2979 : delete rt_update;
636 : }
637 2977 : return;
638 : }
639 :
640 : // Handle the case where it's a UpdateList.
641 2 : UpdateList *uplist = dynamic_cast<UpdateList *>(dbstate);
642 : bool delete_uplist;
643 :
644 2 : assert(uplist);
645 2 : delete_uplist = UpdateListClearPeerSet(db_entry, uplist, mleave);
646 0 : if (delete_uplist) {
647 0 : delete uplist;
648 : }
649 0 : return;
650 : }
651 :
652 : assert(false);
653 : }
654 :
655 : //
656 : // Enqueue the specified RouteUpdate to the UpdateQueue and set the listener
657 : // state for the for the DBEntry to point to the RouteUpdate.
658 : // Set the listener state to the UpdateList if it is non-NULL, else set it to
659 : // the RouteUpdate.
660 : //
661 : // Return true if the BgpUpdateSender needs to trigger a tail dequeue for
662 : // the (RibOut, QueueId).
663 : //
664 63733 : bool RibUpdateMonitor::EnqueueUpdate(DBEntryBase *db_entry,
665 : RouteUpdate *rt_update, UpdateList *uplist) {
666 63733 : CHECK_CONCURRENCY("db::DBTable");
667 :
668 63744 : UpdateQueue *queue = queue_vec_->at(rt_update->queue_id());
669 63762 : if (uplist) {
670 0 : db_entry->SetState(ribout_->table(), ribout_->listener_id(), uplist);
671 : } else {
672 63762 : db_entry->SetState(ribout_->table(), ribout_->listener_id(), rt_update);
673 : }
674 64029 : return queue->Enqueue(rt_update);
675 : }
676 :
677 : //
678 : // Dequeue the specified RouteUpdate from it's UpdateQueue.
679 : //
680 63978 : void RibUpdateMonitor::DequeueUpdate(RouteUpdate *rt_update) {
681 63978 : CHECK_CONCURRENCY("db::DBTable", "bgp::SendUpdate");
682 :
683 63948 : UpdateQueue *queue = queue_vec_->at(rt_update->queue_id());
684 63938 : queue->Dequeue(rt_update);
685 63966 : }
686 :
687 : //
688 : // Get the next RouteUpdate after the provided UpdateEntry and return
689 : // the RouteUpdatePtr encapsulator for it.
690 : //
691 : // If the next RouteUpdate is NULL, move the tail marker to be after
692 : // the UpdateEntry. The tail marker move must be done must be done
693 : // atomically with returning a NULL RouteUpdatePtr. This ensures that
694 : // Enqueue can correctly detect the need to trigger a tail dequeue.
695 : //
696 84855 : RouteUpdatePtr RibUpdateMonitor::GetNextUpdate(int queue_id,
697 : UpdateEntry *upentry) {
698 84855 : CHECK_CONCURRENCY("bgp::SendUpdate");
699 :
700 84782 : UpdateQueue *queue = queue_vec_->at(queue_id);
701 84792 : RouteUpdate *next_rt_update = queue->NextUpdate(upentry);
702 84840 : RouteUpdatePtr update(next_rt_update);
703 84900 : if (!next_rt_update && upentry->IsUpdate()) {
704 41990 : RouteUpdate *rt_update = static_cast<RouteUpdate *>(upentry);
705 41990 : queue->MoveMarker(queue->tail_marker(), rt_update);
706 : }
707 84891 : return update;
708 0 : }
709 :
710 : //
711 : // Get the next UpdateEntry after the one provided and return it via
712 : // the output parameter.
713 : //
714 : // Return the RouteUpdatePtr encapsulator for the next UpdateEntry
715 : // if it's a RouteUpdate. If it's not, return an encapsulator for
716 : // a NULL RouteUpdate.
717 : //
718 0 : RouteUpdatePtr RibUpdateMonitor::GetNextEntry(int queue_id,
719 : UpdateEntry *upentry, UpdateEntry **next_upentry_p) {
720 0 : CHECK_CONCURRENCY("bgp::SendUpdate");
721 :
722 0 : UpdateQueue *queue = queue_vec_->at(queue_id);
723 0 : UpdateEntry *next_upentry = *next_upentry_p = queue->NextEntry(upentry);
724 0 : if (next_upentry != NULL && next_upentry->IsUpdate()) {
725 0 : RouteUpdate *rt_update = static_cast<RouteUpdate *>(next_upentry);
726 0 : RouteUpdatePtr update(rt_update);
727 0 : return update;
728 0 : }
729 0 : return RouteUpdatePtr();
730 : }
731 :
732 : //
733 : // Get the next UpdateInfo after the one provided and return it via
734 : // the output parameter.
735 : //
736 : // Return the RouteUpdatePtr encapsulator for the RouteUpdate that
737 : // corresponds to the next UpdateInfo. If there's no next UpdateInfo
738 : // return an encapsulator for a NULL RouteUpdate.
739 : //
740 61373 : RouteUpdatePtr RibUpdateMonitor::GetAttrNext(int queue_id,
741 : UpdateInfo *current_uinfo, UpdateInfo **next_uinfo_p) {
742 61373 : CHECK_CONCURRENCY("bgp::SendUpdate");
743 :
744 61366 : UpdateQueue *queue = queue_vec_->at(queue_id);
745 61376 : UpdateInfo *next_uinfo = queue->AttrNext(current_uinfo);
746 61443 : RouteUpdate *rt_update = NULL;
747 61443 : if (next_uinfo) {
748 18830 : rt_update = next_uinfo->update;
749 : }
750 61443 : RouteUpdatePtr update(rt_update);
751 61442 : *next_uinfo_p = next_uinfo;
752 61442 : return update;
753 : }
754 :
755 : //
756 : // Set the listener state for the DBEntryBase to be the DBState.
757 : //
758 37152 : void RibUpdateMonitor::SetEntryState(DBEntryBase *db_entry, DBState *dbstate) {
759 37152 : CHECK_CONCURRENCY("bgp::SendUpdate");
760 :
761 37134 : db_entry->SetState(ribout_->table(), ribout_->listener_id(), dbstate);
762 37173 : }
763 :
764 : //
765 : // Clear the listener state for the DBEntryBase.
766 : //
767 23866 : void RibUpdateMonitor::ClearEntryState(DBEntryBase *db_entry) {
768 23866 : CHECK_CONCURRENCY("bgp::SendUpdate");
769 :
770 23855 : db_entry->ClearState(ribout_->table(), ribout_->listener_id());
771 23910 : }
|