libbgp  0.6
A C++ BGP Library.
bgp-rib4.h
Go to the documentation of this file.
1 
11 #ifndef RIB_H_
12 #define RIB_H_
13 #include <stdint.h>
14 #include <vector>
15 #include <unordered_map>
16 #include <tuple>
17 #include <memory>
18 #include <mutex>
19 #include "bgp-rib.h"
20 #include "prefix4.h"
21 #include "bgp-path-attrib.h"
22 
23 namespace libbgp {
24 
30 public:
31  BgpRib4EntryKey() {}
32  BgpRib4EntryKey(const Prefix4 &prefix) {
33  this->prefix = prefix.getPrefix();
34  this->length = prefix.getLength();
35  hash = this->prefix | (this->length << 4);
36  }
37  BgpRib4EntryKey(uint32_t prefix, uint32_t length) {
38  this->prefix = prefix;
39  this->length = length;
40  hash = this->prefix | (this->length << 4);
41  }
42 
43  bool operator== (const BgpRib4EntryKey &other) const {
44  return prefix == other.prefix && length == other.length;
45  }
46 
47  uint64_t hash;
48  uint32_t prefix;
49  uint8_t length;
50 };
51 
57  std::size_t operator()(const BgpRib4EntryKey &key) const {
58  return key.hash;
59  }
60 };
61 
66 class BgpRib4Entry : public BgpRibEntry<BgpRib4Entry> {
67 public:
68  BgpRib4Entry ();
69  BgpRib4Entry (Prefix4 r, uint32_t src, const std::vector<std::shared_ptr<BgpPathAttrib>> attribs);
70 
76 
77  // get nexthop of this entry.
78  uint32_t getNexthop() const;
79 };
80 
81 typedef std::unordered_multimap<BgpRib4EntryKey, BgpRib4Entry, BgpRib4EntryHash> rib4_t;
82 
87 class BgpRib4 : private BgpRib<BgpRib4Entry> {
88 public:
89  BgpRib4(BgpLogHandler *logger);
90 
91  // insert a route as local routing information base. This MUST NOT be called when FSM is running.
92  const BgpRib4Entry* insert(BgpLogHandler *logger, const Prefix4 &route, uint32_t nexthop, int32_t weight = 0);
93  const std::vector<BgpRib4Entry> insert(BgpLogHandler *logger, const std::vector<Prefix4> &routes, uint32_t nexthop, int32_t weight = 0);
94 
95  // insert a new route into RIB, return BgpRib4Entry that should be send to other peers.
96  // <NULL, false> if a better route is already exist
97  // <BgpRib4Entry*, false> if inserted route replaced current best route, and another route become the new best
98  // <BgpRib4Entry*, true> if inserted route become the new best route
99  std::pair<const BgpRib4Entry*, bool> insert(uint32_t src_router_id, const Prefix4 &route, const std::vector<std::shared_ptr<BgpPathAttrib>> &attrib, int32_t weight, uint32_t ibgp_asn);
100 
101  // insert new routes w/ common attribs.
102  // returns a pair: <updated_routes, new_best_routes> where updated_routes is a vector
103  // containing routes with different attribute then provided.
104  std::pair<std::vector<BgpRib4Entry>, std::vector<Prefix4>> insert(uint32_t src_router_id, const std::vector<Prefix4> &routes, const std::vector<std::shared_ptr<BgpPathAttrib>> &attrib, int32_t weight, uint32_t ibgp_asn);
105 
106  // remove a route from RIB
107  std::pair<bool, const BgpRib4Entry*> withdraw(uint32_t src_router_id, const Prefix4 &route);
108 
109  // remove all routes from a peer, return <unreachabled routes, updated_routes>.
110  std::pair<std::vector<Prefix4>, std::vector<BgpRib4Entry>> discard(uint32_t src_router_id);
111 
112  // lookup in rib, return null if not found
113  const BgpRib4Entry* lookup(uint32_t dest) const;
114 
115  // scoped lookup in rib, return null if not found
116  const BgpRib4Entry* lookup(uint32_t src_router_id, uint32_t dest) const;
117 
118  // get RIB
119  const rib4_t &get() const;
120 private:
121  rib4_t::iterator find_best (const Prefix4 &prefix);
122  rib4_t::iterator find_entry (const Prefix4 &prefix, uint32_t src);
123  std::pair<const BgpRib4Entry*, bool> insertPriv(uint32_t src_router_id, const Prefix4 &route, const std::vector<std::shared_ptr<BgpPathAttrib>> &attrib, int32_t weight, uint32_t ibgp_asn);
124  rib4_t rib;
125  std::recursive_mutex mutex;
126  BgpLogHandler *logger;
127  uint64_t update_id;
128 };
129 
147 }
148 #endif // RIB_H_
uint32_t getPrefix() const
Get prefix.
Definition: prefix4.cc:288
The BgpRib4 (IPv4 BGP Routing Information Base) class.
Definition: bgp-rib4.h:87
Hasher for the Rib4 entry key.
Definition: bgp-rib4.h:56
The BGP path attributes.
Prefix4 route
The prefix of this entry.
Definition: bgp-rib4.h:75
Key for the Rib4 entry map.
Definition: bgp-rib4.h:29
The base of BGP RIB entry.
Definition: bgp-rib.h:35
The Base of BGP RIB.
Definition: bgp-rib.h:224
The BgpRib4Entry class.
Definition: bgp-rib4.h:66
uint8_t getLength() const
Get netmask.
Definition: prefix4.cc:297
IPv4 Route/Prefix related utilities.
Definition: prefix4.h:25
Definition: bgp-afi.h:14
IPv4 Route/Prefix related utilities.
The BgpLogHandler class.