LCOV - code coverage report
Current view: top level - bgp - bgp_update.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 69 213 32.4 %
Date: 2026-09-28 02:13:17 Functions: 12 30 40.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #include "bgp/bgp_update.h"
       6             : 
       7             : #include "bgp/bgp_route.h"
       8             : #include "bgp/bgp_table.h"
       9             : 
      10             : //
      11             : // Find the UpdateInfo element with matching RibOutAttr.
      12             : //
      13           0 : const UpdateInfo *UpdateInfoSList::FindUpdateInfo(
      14             :     const RibOutAttr &roattr) const {
      15           0 :     for (List::const_iterator iter = list_.begin();
      16           0 :          iter != list_.end(); ++iter) {
      17           0 :         if (iter->roattr == roattr) return iter.operator->();
      18             :     }
      19           0 :     return NULL;
      20             : }
      21             : 
      22          51 : RouteUpdate::RouteUpdate(BgpRoute *route, int queue_id)
      23             :     : UpdateEntry(UpdateEntry::UPDATE),
      24          51 :     route_(route),
      25          51 :     queue_id_(queue_id),
      26          51 :     flags_(0) {
      27          51 : }
      28             : 
      29         102 : RouteUpdate::~RouteUpdate() {
      30          51 :     ClearUpdateInfo();
      31          51 :     ClearHistory();
      32         102 : }
      33             : 
      34             : //
      35             : // Set the given UpdateInfoSList in the RouteUpdate to the given value.
      36             : //
      37             : // Note that the back pointers from the individual UpdateInfo elements
      38             : // to the RouteUpdate will be set up when the RouteUpdate gets enqueued
      39             : // to an UpdateQueue.
      40             : //
      41          51 : void RouteUpdate::SetUpdateInfo(UpdateInfoSList &uinfo_slist) {
      42          51 :     assert(updates_->empty());
      43          51 :     updates_.swap(uinfo_slist);
      44          51 : }
      45             : 
      46             : //
      47             : // Get rid of all the UpdateInfo elements in the RouteUpdate. Each element
      48             : // is also deleted.
      49             : //
      50          51 : void RouteUpdate::ClearUpdateInfo() {
      51          51 :     updates_->clear_and_dispose(UpdateInfoDisposer());
      52          51 : }
      53             : 
      54             : //
      55             : // Remove the UpdateInfo element from the UpdateInfoSList and delete the
      56             : // element.
      57             : //
      58             : // Return true if the UpdateInfoSList is now empty.
      59             : //
      60          51 : bool RouteUpdate::RemoveUpdateInfo(UpdateInfo *uinfo) {
      61          51 :     updates_->erase_and_dispose(updates_->iterator_to(*uinfo),
      62             :             UpdateInfoDisposer());
      63          51 :     return updates_->empty();
      64             : }
      65             : 
      66             : //
      67             : // Find the UpdateInfo element with matching RibOutAttr.
      68             : //
      69           0 : UpdateInfo *RouteUpdate::FindUpdateInfo(const RibOutAttr &roattr) {
      70           0 :     for (UpdateInfoSList::List::iterator iter = updates_->begin();
      71           0 :          iter != updates_->end(); ++iter) {
      72           0 :         if (iter->roattr == roattr) return iter.operator->();
      73             :     }
      74           0 :     return NULL;
      75             : }
      76             : 
      77             : //
      78             : // Find the UpdateInfo element with matching RibOutAttr, full of const
      79             : // goodness so it can be used by CompareUpdateInfo.
      80             : //
      81           0 : const UpdateInfo *RouteUpdate::FindUpdateInfo(const RibOutAttr &roattr) const {
      82           0 :     for (UpdateInfoSList::List::const_iterator iter = updates_->begin();
      83           0 :          iter != updates_->end(); ++iter) {
      84           0 :         if (iter->roattr == roattr) return iter.operator->();
      85             :     }
      86           0 :     return NULL;
      87             : }
      88             : 
      89             : //
      90             : // Reset the given RibPeerSet from the target of all UpdateInfo elements. Get
      91             : // rid of any elements whose target becomes empty.
      92             : //
      93           0 : void RouteUpdate::ResetUpdateInfo(const RibPeerSet &peerset) {
      94           0 :     for (UpdateInfoSList::List::iterator iter = updates_->begin();
      95           0 :          iter != updates_->end(); ) {
      96           0 :         iter->target.Reset(peerset);
      97           0 :         if (iter->target.empty()) {
      98           0 :             iter = updates_->erase_and_dispose(iter, UpdateInfoDisposer());
      99             :         } else {
     100             :             ++iter;
     101             :         }
     102             :     }
     103           0 : }
     104             : 
     105             : //
     106             : // Compare this RouteUpdate with the UpdateInfo elements in the given list.
     107             : // The UpdateInfos and AdvertiseInfos in the RouteUpdate represent the old
     108             : // state and the UpdateInfoSList represents the new state.
     109             : //
     110             : // Uses brute force since the UpdateInfo and AdvertiseInfo lists typically
     111             : // contain just a single element.
     112             : //
     113             : // Return true if the information in the RouteUpdate is logically the same
     114             : // as that in the UpdateInfoSList.
     115             : //
     116           0 : bool RouteUpdate::CompareUpdateInfo(const UpdateInfoSList &uinfo_slist) const {
     117             :     // Handle the special case where the given UpdateInfoSList is empty.
     118             :     //
     119             :     // If we already have withdraws scheduled to all peers to which the
     120             :     // route was sent earlier, and there are no other scheduled updates,
     121             :     // we can safely consider the RouteUpdate to have the same state as
     122             :     // an empty UpdateInfoSList.
     123           0 :     RibOutAttr roattr_null;
     124           0 :     if (uinfo_slist->empty() &&
     125           0 :         updates_->size() == 1 && updates_->begin()->roattr == roattr_null) {
     126           0 :         RibPeerSet withdraw_peerset = updates_->begin()->target;
     127           0 :         for (AdvertiseSList::List::const_iterator iter = history_->begin();
     128           0 :              iter != history_->end(); ++iter) {
     129           0 :             if (!withdraw_peerset.Contains(iter->bitset))
     130           0 :                 return false;
     131             :         }
     132           0 :         return true;
     133           0 :     }
     134             : 
     135             :     // All the peers that we sent the route to previously must be covered
     136             :     // by the UpdateInfos in the given UpdateInfoSList. If this is not the
     137             :     // case, we would need to schedule withdraws to the peers that are not
     138             :     // covered.
     139           0 :     RibPeerSet old_peerset, new_peerset;
     140           0 :     for (AdvertiseSList::List::const_iterator iter = history_->begin();
     141           0 :          iter != history_->end(); ++iter) {
     142           0 :         old_peerset.Set(iter->bitset);
     143             :     }
     144           0 :     for (UpdateInfoSList::List::const_iterator iter = uinfo_slist->begin();
     145           0 :          iter != uinfo_slist->end(); ++iter) {
     146           0 :         new_peerset.Set(iter->target);
     147             :     }
     148           0 :     if (!new_peerset.Contains(old_peerset))
     149           0 :         return false;
     150             : 
     151             :     // Compare the peerset for each UpdateInfo in the UpdateInfoSList to
     152             :     // the peerset for the corresponding UpdateInfo and AdvertiseInfo in
     153             :     // in the RouteUpdate i.e. the ones for the same RibOutAttr.  Each
     154             :     // peer in the new UpdateInfo must either be in the old UpdateInfo or
     155             :     // AdvertiseInfo.
     156           0 :     for (UpdateInfoSList::List::const_iterator iter = uinfo_slist->begin();
     157           0 :          iter != uinfo_slist->end(); ++iter) {
     158           0 :         RibPeerSet attr_peerset;
     159           0 :         const UpdateInfo *uinfo = FindUpdateInfo(iter->roattr);
     160           0 :         if (uinfo)
     161           0 :             attr_peerset.Set(uinfo->target);
     162           0 :         const AdvertiseInfo *ainfo = FindHistory(iter->roattr);
     163           0 :         if (ainfo)
     164           0 :             attr_peerset.Set(ainfo->bitset);
     165           0 :         if (iter->target != attr_peerset)
     166           0 :             return false;
     167           0 :     }
     168             : 
     169             :     // Compare the peerset for each UpdateInfo in the RouteUpdate to the
     170             :     // corresponding UpdateInfo in the given UpdateInfoSList. The peerset
     171             :     // from the old UpdateInfo needs to be a subset of the peerset from
     172             :     // the new UpdateInfo. It need not be equal since we may already have
     173             :     // sent the old state to some peers and moved them to the history.
     174           0 :     for (UpdateInfoSList::List::const_iterator iter = updates_->begin();
     175           0 :          iter != updates_->end(); ++iter) {
     176           0 :         const UpdateInfo *uinfo = uinfo_slist.FindUpdateInfo(iter->roattr);
     177           0 :         if (!uinfo || !uinfo->target.Contains(iter->target))
     178           0 :             return false;
     179             :     }
     180             : 
     181           0 :     return true;
     182           0 : }
     183             : 
     184             : //
     185             : // Merge the list of UpdateInfo elements into the RouteUpdate. If we find
     186             : // an UpdateInfo with the same RibOutAttr, we modify it's target in place.
     187             : // Otherwise, we reset the target bits in any existing UpdateInfos in the
     188             : // RouteUpdate and move the UpdateInfo from the list into the RouteUpdate.
     189             : //
     190           0 : void RouteUpdate::MergeUpdateInfo(UpdateInfoSList &uinfo_slist) {
     191           0 :     for (UpdateInfoSList::List::iterator iter = uinfo_slist->begin();
     192           0 :          iter != uinfo_slist->end(); ) {
     193           0 :         UpdateInfo *uinfo = FindUpdateInfo(iter->roattr);
     194           0 :         if (uinfo) {
     195           0 :             uinfo->target |= iter->target;
     196           0 :             iter = uinfo_slist->erase_and_dispose(iter, UpdateInfoDisposer());
     197             :         } else {
     198           0 :             UpdateInfo &temp_uinfo = *iter;
     199           0 :             ResetUpdateInfo(temp_uinfo.target);
     200           0 :             iter = uinfo_slist->erase(iter);
     201           0 :             updates_->push_front(temp_uinfo);
     202             :         }
     203             :     }
     204           0 : }
     205             : 
     206             : //
     207             : // The AdvertiseSList in the history contains state that has been sent to
     208             : // a set of peers, and the given UpdateInfoSList represents state that we
     209             : // intend to send to a possibly different set of peers.
     210             : //
     211             : // If there are peers in any AdvertiseInfo in the history for which there
     212             : // is no state in the UpdateInfoSList, create an UpdateInfo with negative
     213             : // state to withdraw what we sent out previously. This UpdateInfo has null
     214             : // RibOutAttr and a RibPeerSet of all peers from which we need to withdraw
     215             : // the route.
     216             : //
     217          17 : void RouteUpdate::BuildNegativeUpdateInfo(UpdateInfoSList &uinfo_slist) const {
     218          17 :     RibPeerSet peerset;
     219             : 
     220             :     // Build the bitset of peers to which we previously advertised something.
     221          17 :     for (AdvertiseSList::List::const_iterator iter = history_->begin();
     222          68 :          iter != history_->end(); ++iter) {
     223          17 :         peerset.Set(iter->bitset);
     224             :     }
     225             : 
     226             :     // Remove the peers to which we are going to send updated state.
     227          34 :     for (UpdateInfoSList::List::const_iterator iter = uinfo_slist->begin();
     228          34 :          iter != uinfo_slist->end(); ++iter) {
     229           0 :         peerset.Reset(iter->target);
     230             :     }
     231             : 
     232             :     // Push an UpdateInfo for a withdraw if the bitset is non-empty.
     233          17 :     if (!peerset.empty()) {
     234          17 :         UpdateInfo *uinfo = new UpdateInfo(peerset);
     235          17 :         uinfo_slist->push_front(*uinfo);
     236             :     }
     237          17 : }
     238             : 
     239             : //
     240             : // If any UpdateInfo element in the UpdateInfoSList describes state that's
     241             : // already in one of the AdvertiseInfos in the history, trim that state to
     242             : // avoid sending duplicate information.
     243             : //
     244             : // The RibPeerSet in the UpdateInfo need not be exactly the same as that in
     245             : // the AdevrtiseInfo.  If the RibOutAttrs match, we can trim the RibPeerSet
     246             : // in the UpdateInfo.
     247             : //
     248          17 : void RouteUpdate::TrimRedundantUpdateInfo(UpdateInfoSList &uinfo_slist) const {
     249          17 :     for (AdvertiseSList::List::const_iterator iter = history_->begin();
     250          68 :          iter != history_->end(); ++iter) {
     251          17 :         const AdvertiseInfo *ainfo = iter.operator->();
     252             : 
     253          17 :         for (UpdateInfoSList::List::iterator iter = uinfo_slist->begin();
     254          68 :              iter != uinfo_slist->end(); ++iter) {
     255             :             // Keep going if the attributes are different.
     256          34 :             if (iter->roattr != ainfo->roattr)
     257          17 :                 continue;
     258             : 
     259             :             // Found one with matching attributes.  Reset the target bits in
     260             :             // the UpdateInfo. Get rid of the UpdateInfo if the target is now
     261             :             // empty.
     262           0 :             iter->target.Reset(ainfo->bitset);
     263           0 :             if (iter->target.empty()) {
     264           0 :                 uinfo_slist->erase_and_dispose(iter, UpdateInfoDisposer());
     265             :             }
     266             : 
     267             :             // Since a given UpdateInfoSList can have at most one UpdateInfo
     268             :             // with any given attributes, there's no point in looking at any
     269             :             // more UpdateInfos.
     270           0 :             break;
     271             :         }
     272             :     }
     273          17 : }
     274             : 
     275             : //
     276             : // Set the given AdvertiseSList in the RouteUpdate to the given value.
     277             : //
     278             : // Should be used only for testing.
     279             : //
     280           0 : void RouteUpdate::SetHistory(AdvertiseSList &history) {
     281           0 :     assert(history_->empty());
     282           0 :     history_.swap(history);
     283           0 : }
     284             : 
     285             : //
     286             : // Get rid of all the AdvertiseInfo elements in the RouteUpdate. Each element
     287             : // is also deleted.
     288             : //
     289          51 : void RouteUpdate::ClearHistory() {
     290          51 :     history_->clear_and_dispose(AdvertiseInfoDisposer());
     291          51 : }
     292             : 
     293             : //
     294             : // Move history from RouteUpdate to RouteState.
     295             : //
     296          34 : void RouteUpdate::MoveHistory(RouteState *rstate) {
     297          34 :     AdvertiseSList adv_slist;
     298          34 :     SwapHistory(adv_slist);
     299          34 :     rstate->SwapHistory(adv_slist);
     300          34 : }
     301             : 
     302             : //
     303             : // Find the history element with matching RibOutAttr.
     304             : //
     305           0 : const AdvertiseInfo *RouteUpdate::FindHistory(const RibOutAttr &roattr) const {
     306           0 :     for (AdvertiseSList::List::const_iterator iter = history_->begin();
     307           0 :          iter != history_->end(); ++iter) {
     308           0 :         if (iter->roattr == roattr) return iter.operator->();
     309             :     }
     310           0 :     return NULL;
     311             : }
     312             : 
     313             : //
     314             : // Update the history information for the RouteUpdate to include the given
     315             : // RibOutAttr and the RibPeerSet. This may entail an update to an existing
     316             : // AdvertiseInfo or the creation of a new one, as well as possible deletion
     317             : // of an existing one.
     318             : //
     319          51 : void RouteUpdate::UpdateHistory(RibOut *ribout, const RibOutAttr *roattr,
     320             :         const RibPeerSet &bits) {
     321             :     // The history information may reside in the RouteUpdate itself or in
     322             :     // the associated UpdateList if the RouteUpdate in on an UpdateList.
     323             :     AdvertiseSList &adv_slist =
     324          51 :         (OnUpdateList() ? GetUpdateList(ribout)->History() : history_);
     325             : 
     326             :     // Traipse through all the AdvertiseInfo elements in the history.  We
     327             :     // obviously need to find one with a matching RibOutAttr if it exists
     328             :     // and update it's RibPeerSet.   Additionally, we also want to reset
     329             :     // the RibPeerSet in all other elements since we may have previously
     330             :     // advertised different attributes to some of the peers in the set.
     331          51 :     AdvertiseInfo *ainfo = NULL;
     332          51 :     for (AdvertiseSList::List::iterator iter = adv_slist->begin();
     333         136 :          iter != adv_slist->end(); ) {
     334             :         // Remember if we find the matching element, and move on to the
     335             :         // next one.
     336          17 :         if (iter->roattr == *roattr) {
     337           0 :             assert(ainfo == NULL);
     338           0 :             ainfo = iter.operator->();
     339             :             ++iter;
     340           0 :             continue;
     341             :         }
     342             : 
     343             :         // TBD: optimize to reuse an element with the same RibPeerSet but
     344             :         // different attributes.
     345             : 
     346             :         // Reset the bits in the current element.  If the RibPeerSet in the
     347             :         // element becomes empty, get rid of it.
     348          17 :         iter->bitset.Reset(bits);
     349          17 :         if (iter->bitset.empty()) {
     350             :             AdvertiseSList::List::iterator prev_iter = iter++;
     351          34 :             adv_slist->erase_and_dispose(prev_iter, AdvertiseInfoDisposer());
     352             :         } else {
     353             :             ++iter;
     354             :         }
     355             :     }
     356             : 
     357             :     // Update an existing element if we found one or create a new one with
     358             :     // the appropriate state.
     359          51 :     if (roattr->IsReachable()) {
     360          34 :         if (ainfo == NULL) {
     361          34 :             ainfo = new AdvertiseInfo(roattr);
     362          34 :             adv_slist->push_front(*ainfo);
     363             :         }
     364          34 :         ainfo->bitset |= bits;
     365             :     } else {
     366          17 :         assert(ainfo == NULL);
     367             :     }
     368          51 : }
     369             : 
     370             : //
     371             : // Return true if this RouteUpdate has been advertised to anyone. We simply
     372             : // go through all the AdvertiseInfo elements in the history and check if we
     373             : // have at least one that's reachable.
     374             : //
     375          51 : bool RouteUpdate::IsAdvertised() const {
     376          51 :     for (AdvertiseSList::List::const_iterator iter = history_->begin();
     377         102 :          iter != history_->end(); ++iter) {
     378          34 :         if (iter->roattr.IsReachable()) {
     379          34 :             return true;
     380             :         }
     381             :     }
     382          17 :     return false;
     383             : }
     384             : 
     385             : //
     386             : // Upgrade this RouteUpdate to an UpdateList.  Creates a new UpdateList and
     387             : // adds the RouteUpdate to it.  Also moves the history from the RouteUpdate
     388             : // to the UpdateList.
     389             : //
     390             : // Returns the new UpdateList.
     391             : //
     392           0 : UpdateList *RouteUpdate::MakeUpdateList() {
     393           0 :     UpdateList *uplist = new UpdateList;
     394           0 :     AdvertiseSList history;
     395           0 :     SwapHistory(history);
     396           0 :     uplist->SwapHistory(history);
     397           0 :     uplist->AddUpdate(this);
     398           0 :     return uplist;
     399           0 : }
     400             : 
     401             : //
     402             : // Return the UpdateList that contains this RouteUpdate.  Assumes that the
     403             : // RouteUpdate is on an UpdateList.
     404             : //
     405           0 : UpdateList *RouteUpdate::GetUpdateList(RibOut *ribout) {
     406           0 :     assert(FlagIsSet(RouteUpdate::F_LIST));
     407           0 :     DBState *dbstate = route_->GetState(ribout->table(), ribout->listener_id());
     408           0 :     UpdateList *uplist = dynamic_cast<UpdateList *>(dbstate);
     409           0 :     assert(uplist);
     410           0 :     return uplist;
     411             : }
     412             : 
     413             : //
     414             : // Set the given AdvertiseSList in the UpdateList to the given value.
     415             : //
     416             : // Should be used only for testing.
     417             : //
     418           0 : void UpdateList::SetHistory(AdvertiseSList &history) {
     419           0 :     assert(history_->empty());
     420           0 :     history_.swap(history);
     421           0 : }
     422             : 
     423             : //
     424             : // Move history from UpdateList to RouteState.
     425             : //
     426           0 : void UpdateList::MoveHistory(RouteState *rstate) {
     427           0 :     AdvertiseSList adv_slist;
     428           0 :     SwapHistory(adv_slist);
     429           0 :     rstate->SwapHistory(adv_slist);
     430           0 : }
     431             : 
     432             : //
     433             : // Move history from UpdateList to RouteUpdate.
     434             : //
     435           0 : void UpdateList::MoveHistory(RouteUpdate *rt_update) {
     436           0 :     AdvertiseSList adv_slist;
     437           0 :     SwapHistory(adv_slist);
     438           0 :     rt_update->SwapHistory(adv_slist);
     439           0 : }
     440             : 
     441             : //
     442             : // Find the history element with matching RibOutAttr.
     443             : //
     444           0 : const AdvertiseInfo *UpdateList::FindHistory(const RibOutAttr &roattr) const {
     445           0 :     for (AdvertiseSList::List::const_iterator iter = history_->begin();
     446           0 :          iter != history_->end(); ++iter) {
     447           0 :         if (iter->roattr == roattr) return iter.operator->();
     448             :     }
     449           0 :     return NULL;
     450             : }
     451             : 
     452             : //
     453             : // Add a RouteUpdate to this UpdateList.  Sets up the association between
     454             : // the RouteUpdate and the UpdateList.
     455             : //
     456             : // Assumes that RouteUpdate has no history i.e. AdvertiseSList is empty.
     457             : //
     458           0 : void UpdateList::AddUpdate(RouteUpdate *rt_update) {
     459           0 :     list_.push_back(rt_update);
     460           0 :     rt_update->set_update_list();
     461           0 : }
     462             : 
     463             : //
     464             : // Remove a RouteUpdate from this UpdateList.  Takes care of removing the
     465             : // linkage between the UpdateList and the RouteUpdate.
     466             : //
     467           0 : void UpdateList::RemoveUpdate(RouteUpdate *rt_update) {
     468           0 :     rt_update->clear_update_list();
     469           0 :     list_.remove(rt_update);
     470           0 : }
     471             : 
     472             : //
     473             : // Find the RouteUpdate for the given queue.
     474             : //
     475           0 : RouteUpdate *UpdateList::FindUpdate(int queue_id) {
     476           0 :     for (List::iterator iter = list_.begin(); iter != list_.end(); ++iter) {
     477           0 :         if ((*iter)->queue_id() == queue_id)
     478           0 :             return *iter;
     479             :     }
     480           0 :     return NULL;
     481             : }
     482             : 
     483             : //
     484             : // Downgrade this UpdateList to a RouteUpdate if there's only 1 RouteUpdate
     485             : // on it. Takes care of removing the linkage between the UpdateList and the
     486             : // last RouteUpdate and moves history from the UpdateList to the RouteUpdate.
     487             : //
     488             : // Note that the caller is responsible for deleting the UpdateList.
     489             : //
     490             : // Returns the last/only RouteUpdate if successful, NULL otherwise.
     491             : //
     492           0 : RouteUpdate *UpdateList::MakeRouteUpdate() {
     493           0 :     assert(list_.size() != 0);
     494             : 
     495           0 :     if (list_.size() > 1)
     496           0 :         return NULL;
     497             : 
     498           0 :     RouteUpdate *rt_update = list_.front();
     499           0 :     rt_update->clear_update_list();
     500           0 :     MoveHistory(rt_update);
     501           0 :     return rt_update;
     502             : }

Generated by: LCOV version 1.14