libbgp  0.6
A C++ BGP Library.
bgp-rib.h
1 #ifndef BGP_RIB_H_
2 #define BGP_RIB_H_
3 #include <stdint.h>
4 #include <vector>
5 #include <memory>
6 #include <arpa/inet.h>
7 #include "bgp-path-attrib.h"
8 #include "bgp-log-handler.h"
9 
10 namespace libbgp {
11 
16 enum BgpRouteSource {
17  SRC_EBGP = 0,
18  SRC_IBGP = 1
19 };
20 
25 enum BgpRouteStatus {
26  RS_STANDBY = 0,
27  RS_ACTIVE = 1
28 };
29 
35 template<typename T> class BgpRibEntry {
36 public:
43  BgpRibEntry () { src = SRC_EBGP; status = RS_ACTIVE; }
44 
49  uint32_t src_router_id;
50 
59  uint64_t update_id;
60 
68  int32_t weight;
69 
74  std::vector<std::shared_ptr<BgpPathAttrib>> attribs;
75 
83  BgpRouteSource src;
84 
91  BgpRouteStatus status;
92 
100  uint32_t ibgp_peer_asn;
101 
111  bool operator> (const T &other) const {
112  // perfer ebgp
113  if (this->src > other.src) return false;
114 
115  // prefer higher weight
116  if (this->weight > other.weight) return true;
117  if (this->weight < other.weight) return false;
118 
119  uint32_t other_med = 0;
120  uint32_t this_med = 0;
121 
122  uint32_t other_orig_as = 0;
123  uint32_t this_orig_as = 0;
124 
125  uint8_t other_origin = 0;
126  uint8_t this_origin = 0;
127 
128  uint8_t other_as_path_len = 0;
129  uint8_t this_as_path_len = 0;
130 
131  uint32_t other_local_pref = 100;
132  uint32_t this_local_pref = 100;
133 
134  // grab attributes
135  for (const std::shared_ptr<BgpPathAttrib> &attr : other.attribs) {
136  if (attr->type_code == MULTI_EXIT_DISC) {
137  const BgpPathAttribMed &med = dynamic_cast<const BgpPathAttribMed &>(*attr);
138  other_med = med.med;
139  }
140 
141  if (attr->type_code == ORIGIN) {
142  const BgpPathAttribOrigin &orig = dynamic_cast<const BgpPathAttribOrigin &>(*attr);
143  other_origin = orig.origin;
144  continue;
145  }
146 
147  if (attr->type_code == AS_PATH) {
148  const BgpPathAttribAsPath &as_path = dynamic_cast<const BgpPathAttribAsPath &>(*attr);
149  for (const BgpAsPathSegment &seg : as_path.as_paths) {
150  if (seg.type == AS_SEQUENCE) {
151  other_as_path_len = seg.value.size();
152  other_orig_as = seg.value.back();
153  }
154  }
155  continue;
156  }
157 
158  if (attr->type_code == LOCAL_PREF) {
159  const BgpPathAttribLocalPref &pref = dynamic_cast<const BgpPathAttribLocalPref &>(*attr);
160  other_local_pref = pref.local_pref;
161  continue;
162  }
163  }
164 
165  for (const std::shared_ptr<BgpPathAttrib> &attr : attribs) {
166  if (attr->type_code == MULTI_EXIT_DISC) {
167  const BgpPathAttribMed &med = dynamic_cast<const BgpPathAttribMed &>(*attr);
168  this_med = med.med;
169  }
170 
171  if (attr->type_code == ORIGIN) {
172  const BgpPathAttribOrigin &orig = dynamic_cast<const BgpPathAttribOrigin &>(*attr);
173  this_origin = orig.origin;
174  continue;
175  }
176 
177  if (attr->type_code == AS_PATH) {
178  const BgpPathAttribAsPath &as_path = dynamic_cast<const BgpPathAttribAsPath &>(*attr);
179  for (const BgpAsPathSegment &seg : as_path.as_paths) {
180  if (seg.type == AS_SEQUENCE) {
181  this_as_path_len = seg.value.size();
182  this_orig_as = seg.value.back();
183  }
184  }
185  continue;
186  }
187 
188  if (attr->type_code == LOCAL_PREF) {
189  const BgpPathAttribLocalPref &pref = dynamic_cast<const BgpPathAttribLocalPref &>(*attr);
190  this_local_pref = pref.local_pref;
191  continue;
192  }
193  }
194 
195  if (this_local_pref > other_local_pref) return true;
196  else if (this_local_pref < other_local_pref) return false;
197  else if (other_as_path_len > this_as_path_len) return true;
198  else if (other_as_path_len < this_as_path_len) return false;
199  else if (other_origin > this_origin) return true;
200  else if (other_origin < this_origin) return false;
201  else if (other_orig_as == this_orig_as && other_med > this_med) return true;
202  else if (other_orig_as == this_orig_as && other_med < this_med) return false;
203  else if (other.update_id > update_id) return true;
204  else if (other.update_id < update_id) return false;
205  else if (htonl(other.src_router_id) > htonl(src_router_id)) return true;
206 
207  return false;
208  }
209 
210 };
211 
212 #ifdef SWIG
213 class BgpRib6Entry;
214 class BgpRib4Entry;
215 %template(Rib6Entry) BgpRibEntry<BgpRib6Entry>;
216 %template(Rib4Entry) BgpRibEntry<BgpRib4Entry>;
217 #endif
218 
224 template<typename T> class BgpRib {
225 protected:
233  static const T* selectEntry (const T *a, const T *b) {
234  if (a == NULL) return b;
235  if (b == NULL) return a;
236 
237  auto &ra = a->route;
238  auto &rb = b->route;
239 
240  // a is more specific, use a
241  if (ra.getLength() > rb.getLength()) return a;
242 
243  // a, b are same level of specific, check metric
244  if (ra.getLength() == rb.getLength()) {
245  // return the one with higher weight
246  return (*b > *a) ? b : a;
247  }
248 
249  // b is more specific, use b
250  return b;
251  }
252 
260  static T* selectEntry (T *a, T *b) {
261  if (a == NULL) return b;
262  if (b == NULL) return a;
263 
264  auto &ra = a->route;
265  auto &rb = b->route;
266 
267  // a is more specific, use a
268  if (ra.getLength() > rb.getLength()) return a;
269 
270  // a, b are same level of specific, check metric
271  if (ra.getLength() == rb.getLength()) {
272  // return the one with higher weight
273  return (*b > *a) ? b : a;
274  }
275 
276  // b is more specific, use b
277  return b;
278  }
279 };
280 
281 }
282 
283 #endif // BGP_RIB_H_
static T * selectEntry(T *a, T *b)
Select an entry from two to use.
Definition: bgp-rib.h:260
The BGP path attributes.
std::vector< std::shared_ptr< BgpPathAttrib > > attribs
Path attributes for this entry.
Definition: bgp-rib.h:74
std::vector< uint32_t > value
The segment value.
BgpRibEntry()
Construct a new BgpRibEntry.
Definition: bgp-rib.h:43
The base of BGP RIB entry.
Definition: bgp-rib.h:35
The Base of BGP RIB.
Definition: bgp-rib.h:224
uint32_t local_pref
Local Pref.
std::vector< BgpAsPathSegment > as_paths
The AS Path segments.
The BgpRib4Entry class.
Definition: bgp-rib4.h:66
BgpRouteSource src
Source of this entry.
Definition: bgp-rib.h:83
uint8_t type
Segment type.
The BgpRib6Entry class.
Definition: bgp-rib6.h:66
Local Pref attribute.
uint32_t ibgp_peer_asn
ASN of the IBGP peer. (Valid iff src == SRC_IBGP)
Definition: bgp-rib.h:100
int32_t weight
Weight of this entry.
Definition: bgp-rib.h:68
uint32_t src_router_id
The originating BGP speaker&#39;s ID of this entry. (network bytes order)
Definition: bgp-rib.h:49
Definition: bgp-afi.h:14
An AS_PATH or AS4_PATH segment.
BGP log handler.
Prefix6 route
The prefix of this entry.
Definition: bgp-rib6.h:76
static const T * selectEntry(const T *a, const T *b)
Select an entry from two to use.
Definition: bgp-rib.h:233
Multi Exit Discriminator attribute.
BgpRouteStatus status
Status of this entry.
Definition: bgp-rib.h:91
uint64_t update_id
The update ID.
Definition: bgp-rib.h:59
bool operator>(const T &other) const
Test if this entry has greater weight then anoter entry. Please note that weight are only calculated ...
Definition: bgp-rib.h:111