LCOV - code coverage report
Current view: top level - bgp - bgp_table.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 44 47 93.6 %
Date: 2026-06-04 02:06:09 Functions: 24 25 96.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #ifndef SRC_BGP_BGP_TABLE_H_
       6             : #define SRC_BGP_BGP_TABLE_H_
       7             : 
       8             : #include <map>
       9             : #include <string>
      10             : #include <vector>
      11             : #include <atomic>
      12             : 
      13             : #include "base/lifetime.h"
      14             : #include "bgp/bgp_rib_policy.h"
      15             : #include "db/db_table_walker.h"
      16             : #include "route/table.h"
      17             : #include "bgp/bgp_ribout.h"
      18             : 
      19             : class BgpServer;
      20             : class BgpRoute;
      21             : class BgpPath;
      22             : class BgpUpdateSender;
      23             : class IPeer;
      24             : class Path;
      25             : class PathResolver;
      26             : class RibOut;
      27             : class RibPeerSet;
      28             : class Route;
      29             : class RoutingInstance;
      30             : class ShowRibOutStatistics;
      31             : class UpdateInfoSList;
      32             : struct UpdateInfo;
      33             : 
      34             : class BgpTable : public RouteTable {
      35             : public:
      36             :     typedef std::map<RibExportPolicy, RibOut *> RibOutMap;
      37             :     typedef std::set<BgpTable *> TableSet;
      38             : 
      39             :     struct RequestKey : DBRequestKey {
      40             :         virtual const IPeer *GetPeer() const = 0;
      41             :     };
      42             : 
      43             :     struct RequestData : DBRequestData {
      44             :         struct NextHop {
      45             :             NextHop()
      46             :                 : flags_(0),
      47             :                   address_(Ip4Address(0)),
      48             :                   label_(0),
      49             :                   l3_label_(0) {
      50             :             }
      51      296273 :             NextHop(uint32_t flags, IpAddress address, uint32_t label,
      52             :                     uint32_t l3_label = 0)
      53      296273 :                 : flags_(flags),
      54      296273 :                   address_(address),
      55      296271 :                   label_(label),
      56      296271 :                   l3_label_(l3_label) {
      57      296271 :             }
      58             : 
      59             :             uint32_t flags_;
      60             :             IpAddress address_;
      61             :             uint32_t label_;
      62             :             uint32_t l3_label_;
      63             :         };
      64             : 
      65      210538 :         RequestData(const BgpAttrPtr &attrs, uint32_t flags, uint32_t label,
      66             :             uint32_t l3_label, uint64_t subscription_gen_id)
      67      421075 :             : attrs_(attrs),
      68      210553 :               nexthop_(flags, attrs ? attrs->nexthop() : Ip4Address(0),
      69             :                        label, l3_label),
      70      210538 :               subscription_gen_id_(subscription_gen_id) {
      71      210545 :         }
      72             : 
      73       85726 :         RequestData(const BgpAttrPtr &attrs, uint32_t flags, uint32_t label,
      74             :             uint32_t l3_label = 0)
      75      171452 :             : attrs_(attrs),
      76       85726 :               nexthop_(flags, attrs ? attrs->nexthop() : Ip4Address(0),
      77             :                        label, l3_label),
      78       85726 :               subscription_gen_id_(0) {
      79       85726 :         }
      80             : 
      81      273600 :         const NextHop &nexthop() { return nexthop_; }
      82      280833 :         BgpAttrPtr &attrs() { return attrs_; }
      83             :         void set_attrs(BgpAttrPtr attrs) { attrs_ = attrs; }
      84         940 :         void set_subscription_gen_id(uint64_t subscription_gen_id) {
      85         940 :             subscription_gen_id_ = subscription_gen_id;
      86         940 :         }
      87       27024 :         uint64_t subscription_gen_id() const { return subscription_gen_id_; }
      88             : 
      89             :     private:
      90             :         BgpAttrPtr attrs_;
      91             :         NextHop nexthop_;
      92             :         uint64_t subscription_gen_id_;
      93             :     };
      94             : 
      95             :     BgpTable(DB *db, const std::string &name);
      96             :     ~BgpTable();
      97             : 
      98          24 :     const RibOutMap &ribout_map() { return ribout_map_; }
      99             :     RibOut *RibOutFind(const RibExportPolicy &policy);
     100             :     RibOut *RibOutLocate(BgpUpdateSender *sender,
     101             :                          const RibExportPolicy &policy);
     102             :     void RibOutDelete(const RibExportPolicy &policy);
     103             : 
     104             :     virtual bool Export(RibOut *ribout, Route *route,
     105             :                         const RibPeerSet &peerset,
     106             :                         UpdateInfoSList &uinfo_slist) = 0;
     107             : 
     108             :     virtual Address::Family family() const = 0;
     109      599492 :     virtual bool IsVpnTable() const { return false; }
     110      515007 :     virtual bool IsRoutingPolicySupported() const { return false; }
     111     1230869 :     virtual bool IsRouteAggregationSupported() const { return false; }
     112             :     virtual std::unique_ptr<DBEntry> AllocEntryStr(
     113             :         const std::string &key) const = 0;
     114             : 
     115             :     virtual BgpRoute *RouteReplicate(BgpServer *server, BgpTable *table,
     116             :                                      BgpRoute *src, const BgpPath *path,
     117             :                                      ExtCommunityPtr community) = 0;
     118             : 
     119             :     static bool PathSelection(const Path &path1, const Path &path2);
     120             :     void CheckAggregatorAttr(BgpAttr *attr) const;
     121             :     void CreateAsPath4Byte(BgpAttr *attr, as_t local_as) const;
     122             :     void CreateAsPath2Byte(BgpAttr *attr) const;
     123             :     void CreateAs4Path(BgpAttr *attr) const;
     124             :     bool Has4ByteAsn(BgpAttr *attr) const;
     125             :     void PrependAsToAsPath4Byte(BgpAttr *attr, as_t asn) const;
     126             :     void PrependAsToAsPath2Byte(BgpAttr *attr, as_t asn) const;
     127             :     void PrependAsToAsPath2Byte(BgpAttr *attr, as2_t asn) const;
     128             :     void PrependAsToAs4Path(BgpAttr* attr, as_t asn) const;
     129             :     UpdateInfo *GetUpdateInfo(RibOut *ribout, BgpRoute *route,
     130             :                               const RibPeerSet &peerset);
     131             :     void ProcessDefaultTunnelEncapsulation(const RibOut *ribout,
     132             :         ExtCommunityDB *extcomm_db, BgpAttr *attr) const;
     133             : 
     134      318994 :     virtual void UpdateSecondaryTablesForReplication(BgpRoute *rt,
     135             :                      TableSet *secondary_tables) {
     136      318994 :     }
     137             : 
     138             :     void ManagedDelete();
     139             :     virtual void RetryDelete();
     140             :     void Shutdown();
     141             :     virtual bool MayDelete() const;
     142      282523 :     bool IsDeleted() const { return deleter()->IsDeleted(); }
     143             :     virtual PathResolver *CreatePathResolver();
     144             :     void LocatePathResolver();
     145             :     void DestroyPathResolver();
     146             : 
     147     6475572 :     RoutingInstance *routing_instance() { return rtinstance_; }
     148     7328372 :     const RoutingInstance *routing_instance() const { return rtinstance_; }
     149             :     virtual void set_routing_instance(RoutingInstance *rtinstance);
     150             :     BgpServer *server();
     151             :     const BgpServer *server() const;
     152        9185 :     PathResolver *path_resolver() { return path_resolver_; }
     153          80 :     const PathResolver *path_resolver() const { return path_resolver_; }
     154             : 
     155             :     LifetimeActor *deleter();
     156             :     const LifetimeActor *deleter() const;
     157             :     size_t GetPendingRiboutsCount(size_t *markers) const;
     158             : 
     159             :     void UpdatePathCount(const BgpPath *path, int count);
     160       22565 :     const uint64_t GetPrimaryPathCount() const { return primary_path_count_; }
     161       22569 :     const uint64_t GetSecondaryPathCount() const {
     162       22569 :         return secondary_path_count_;
     163             :     }
     164       22562 :     const uint64_t GetInfeasiblePathCount() const {
     165       22562 :         return infeasible_path_count_;
     166             :     }
     167       16974 :     const uint64_t GetStalePathCount() const { return stale_path_count_; }
     168       16971 :     const uint64_t GetLlgrStalePathCount() const {
     169       16971 :         return llgr_stale_path_count_;
     170             :     }
     171        3156 :     void UpdateStalePathCount(int count) { stale_path_count_ += count; }
     172           0 :     void UpdateLlgrStalePathCount(int count) {
     173           0 :         llgr_stale_path_count_ += count;
     174           0 :     }
     175             : 
     176             :     // Check whether the route is aggregate route
     177             :     bool IsAggregateRoute(const BgpRoute *route) const;
     178             : 
     179             :     // Check whether the route is contributing route to aggregate route
     180             :     bool IsContributingRoute(const BgpRoute *route) const;
     181             : 
     182             :     bool DeletePath(DBTablePartBase *root, BgpRoute *rt, BgpPath *path);
     183             :     virtual void Input(DBTablePartition *root, DBClient *client,
     184             :                        DBRequest *req);
     185             :     bool InputCommon(DBTablePartBase *root, BgpRoute *rt, BgpPath *path,
     186             :                      const IPeer *peer, DBRequest *req,
     187             :                      DBRequest::DBOperation oper, BgpAttrPtr attrs,
     188             :                      uint32_t path_id, uint32_t flags, uint32_t label,
     189             :                      uint32_t l3_label);
     190             :     void InputCommonPostProcess(DBTablePartBase *root, BgpRoute *rt,
     191             :                                 bool notify_rt);
     192             : 
     193             :     void FillRibOutStatisticsInfo(
     194             :         std::vector<ShowRibOutStatistics> *sros_list) const;
     195             : 
     196      243074 :     virtual BgpAttrPtr GetAttributes(BgpRoute *rt, BgpAttrPtr attrp,
     197             :                                      const IPeer *peer) {
     198      243074 :         return attrp;
     199             :     };
     200             : private:
     201             :     friend class BgpTableTest;
     202             : 
     203             :     class DeleteActor;
     204             : 
     205             :     void PrependLocalAs(const RibOut *ribout, BgpAttr *attr, const IPeer*) const;
     206             :     void ProcessAsOverride(const RibOut *ribout, BgpAttr *attr) const;
     207             :     void ProcessRemovePrivate(const RibOut *ribout, BgpAttr *attr) const;
     208             :     void RemovePrivateAs(const RibOut *ribout, BgpAttr *attr) const;
     209             :     void RemovePrivate4ByteAs(const RibOut *ribout, BgpAttr *attr) const;
     210             :     void RemovePrivateAs4(const RibOut *ribout, BgpAttr *attr) const;
     211             :     void ProcessLlgrState(const RibOut *ribout, const BgpPath *path,
     212             :                           BgpAttr *attr, bool llgr_stale_comm);
     213             :     virtual BgpRoute *TableFind(DBTablePartition *rtp,
     214             :             const DBRequestKey *prefix) = 0;
     215             : 
     216             :     RoutingInstance *rtinstance_;
     217             :     PathResolver *path_resolver_;
     218             :     RibOutMap ribout_map_;
     219             : 
     220             :     boost::scoped_ptr<DeleteActor> deleter_;
     221             :     LifetimeRef<BgpTable> instance_delete_ref_;
     222             :     std::atomic<uint64_t> primary_path_count_;
     223             :     std::atomic<uint64_t> secondary_path_count_;
     224             :     std::atomic<uint64_t> infeasible_path_count_;
     225             :     std::atomic<uint64_t> stale_path_count_;
     226             :     std::atomic<uint64_t> llgr_stale_path_count_;
     227             : 
     228             :     DISALLOW_COPY_AND_ASSIGN(BgpTable);
     229             : };
     230             : 
     231             : #endif  // SRC_BGP_BGP_TABLE_H_

Generated by: LCOV version 1.14