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 : #ifndef SRC_BGP_BGP_PATH_H_
7 : #define SRC_BGP_BGP_PATH_H_
8 :
9 : #include <string>
10 : #include <vector>
11 :
12 : #include "base/util.h"
13 : #include "route/path.h"
14 : #include "bgp/bgp_attr.h"
15 :
16 : class BgpTable;
17 : class BgpRoute;
18 : class IPeer;
19 :
20 : class BgpPath : public Path {
21 : public:
22 : enum PathFlag {
23 : AsPathLooped = 1 << 0,
24 : NoNeighborAs = 1 << 1,
25 : Stale = 1 << 2,
26 : NoTunnelEncap = 1 << 3,
27 : OriginatorIdLooped = 1 << 4,
28 : ResolveNexthop = 1 << 5,
29 : ResolvedPath = 1 << 6,
30 : RoutingPolicyReject = 1 << 7,
31 : LlgrStale = 1 << 8,
32 : ClusterListLooped = 1 << 9,
33 : AliasedPath = 1 << 10,
34 : CheckGlobalErmVpnRoute = 1 << 11,
35 : };
36 :
37 : // Ordered in the ascending order of path preference
38 : enum PathSource {
39 : None = 0,
40 : BGP_XMPP = 1,
41 : ServiceChain = 2,
42 : StaticRoute = 3,
43 : Aggregate = 4,
44 : Local = 5
45 : };
46 :
47 : static const uint32_t INFEASIBLE_MASK = (AsPathLooped |
48 : NoNeighborAs | NoTunnelEncap | OriginatorIdLooped | ResolveNexthop |
49 : RoutingPolicyReject | ClusterListLooped | CheckGlobalErmVpnRoute);
50 :
51 : static std::string PathIdString(uint32_t path_id);
52 :
53 : BgpPath(const IPeer *peer, uint32_t path_id, PathSource src,
54 : const BgpAttrPtr ptr, uint32_t flags, uint32_t label,
55 : uint32_t l3_label = 0);
56 : BgpPath(const IPeer *peer, PathSource src, const BgpAttrPtr attr,
57 : uint32_t flags, uint32_t label, uint32_t l3_label = 0);
58 : BgpPath(uint32_t path_id, PathSource src, const BgpAttrPtr attr,
59 : uint32_t flags = 0, uint32_t label = 0, uint32_t l3_label = 0);
60 : BgpPath(PathSource src, const BgpAttrPtr attr,
61 : uint32_t flags = 0, uint32_t label = 0, uint32_t l3_label = 0);
62 1353305 : virtual ~BgpPath() {
63 1353305 : }
64 :
65 : void AddExtCommunitySubCluster(uint32_t subcluster_id);
66 :
67 : RouteDistinguisher GetSourceRouteDistinguisher() const;
68 :
69 722345 : bool IsVrfOriginated() const {
70 722345 : if (IsReplicated())
71 409217 : return false;
72 313131 : if (source_ != Aggregate && source_ != BGP_XMPP && source_ != Local)
73 2016 : return false;
74 311115 : return true;
75 : }
76 :
77 7362569 : IPeer *GetPeer() { return const_cast<IPeer *>(peer_); }
78 2209324 : const IPeer *GetPeer() const { return peer_; }
79 5308784 : const uint32_t GetPathId() const { return path_id_; }
80 :
81 : void UpdatePeerRefCount(int count, Address::Family family) const;
82 :
83 711826 : void SetAttr(const BgpAttrPtr attr, const BgpAttrPtr original_attr) {
84 711826 : attr_ = attr;
85 711826 : original_attr_ = original_attr;
86 711843 : }
87 :
88 5812444 : const BgpAttr *GetAttr() const { return attr_.get(); }
89 2333283 : const BgpAttr *GetOriginalAttr() const { return original_attr_.get(); }
90 1984549 : uint32_t GetLabel() const { return label_; }
91 893390 : uint32_t GetL3Label() const { return l3_label_; }
92 1658121 : virtual bool IsReplicated() const { return false; }
93 14686443 : bool IsFeasible() const { return ((flags_ & INFEASIBLE_MASK) == 0); }
94 :
95 3026 : bool IsResolutionFeasible() const {
96 3026 : return ((flags_ & (INFEASIBLE_MASK & ~ResolveNexthop)) == 0);
97 : }
98 :
99 5058707 : bool IsAliased() const { return ((flags_ & AliasedPath) != 0); }
100 948384 : bool IsResolved() const { return ((flags_ & ResolvedPath) != 0); }
101 10810000 : uint32_t GetFlags() const { return flags_; }
102 : std::vector<std::string> GetFlagsStringList() const;
103 :
104 11642990 : PathSource GetSource() const { return source_; }
105 : std::string GetSourceString(bool combine_bgp_and_xmpp = false) const;
106 :
107 : // Check if the path is stale
108 3761714 : bool IsStale() const { return ((flags_ & Stale) != 0); }
109 :
110 : // Check if the path is stale
111 8582218 : bool IsLlgrStale() const { return ((flags_ & LlgrStale) != 0); }
112 :
113 : // Mark a path as rejected by Routing policy
114 19 : void SetPolicyReject() { flags_ |= RoutingPolicyReject; }
115 :
116 : // Reset a path as active from Routing Policy
117 3 : void ResetPolicyReject() { flags_ &= ~RoutingPolicyReject; }
118 :
119 448493 : bool IsPolicyReject() const {
120 448493 : return ((flags_ & RoutingPolicyReject) != 0);
121 : }
122 :
123 : // Mark a path as stale
124 5 : void SetStale() { flags_ |= Stale; }
125 :
126 : // Reset a path as active (not stale)
127 3110 : void ResetStale() { flags_ &= ~Stale; }
128 :
129 : // Mark/Reset a path as needing resolution
130 106 : void SetResolveNextHop() { flags_ |= ResolveNexthop; }
131 : void ResetResolveNextHop() { flags_ &= ~ResolveNexthop; }
132 :
133 26 : void SetLlgrStale() { flags_ |= LlgrStale; }
134 16 : void ResetLlgrStale() { flags_ &= ~LlgrStale; }
135 :
136 1140215 : bool NeedsResolution() const { return ((flags_ & ResolveNexthop) != 0); }
137 519635 : bool CheckErmVpn() const {
138 519635 : return ((flags_ & CheckGlobalErmVpnRoute) != 0);
139 : }
140 626 : void ResetCheckErmVpn() { flags_ &= ~CheckGlobalErmVpnRoute; }
141 174 : void SetCheckErmVpn() { flags_ |= CheckGlobalErmVpnRoute; }
142 :
143 : virtual std::string ToString() const;
144 :
145 : // Select one path over other
146 : int PathCompare(const BgpPath &rhs, bool allow_ecmp) const;
147 : bool PathSameNeighborAs(const BgpPath &rhs) const;
148 :
149 : private:
150 : const IPeer *peer_;
151 : const uint32_t path_id_;
152 : const PathSource source_;
153 : // Attribute for the BgpPath. If routing policy updates the path attribute,
154 : // this member contains the attribute after policy update
155 : BgpAttrPtr attr_;
156 : // Original path attribute before applying routing policy
157 : BgpAttrPtr original_attr_;
158 : uint32_t flags_;
159 : uint32_t label_;
160 : uint32_t l3_label_;
161 : };
162 :
163 : class BgpSecondaryPath : public BgpPath {
164 : public:
165 : BgpSecondaryPath(const IPeer *peer, uint32_t path_id, PathSource src,
166 : const BgpAttrPtr attr, uint32_t flags, uint32_t label,
167 : uint32_t l3_label = 0);
168 :
169 3610727 : virtual bool IsReplicated() const {
170 3610727 : return true;
171 : }
172 :
173 579469 : void SetReplicateInfo(const BgpTable *table, const BgpRoute *rt) {
174 579469 : src_table_ = table;
175 579469 : src_entry_ = rt;
176 579469 : }
177 :
178 1158557 : virtual ~BgpSecondaryPath() {
179 1158557 : }
180 :
181 : RouteDistinguisher GetPrimaryRouteDistinguisher() const;
182 :
183 17436 : const BgpTable *src_table() const {
184 17436 : return src_table_;
185 : }
186 :
187 1833756 : const BgpRoute *src_rt() const {
188 1833756 : return src_entry_;
189 : }
190 :
191 : private:
192 : const BgpTable *src_table_;
193 : const BgpRoute *src_entry_;
194 :
195 : DISALLOW_COPY_AND_ASSIGN(BgpSecondaryPath);
196 : };
197 :
198 : #endif // SRC_BGP_BGP_PATH_H_
|