libbgp  0.6
A C++ BGP Library.
bgp-rib6.h
Go to the documentation of this file.
1 
11 #ifndef RIB6_H_
12 #define RIB6_H_
13 #include <stdint.h>
14 #include <string.h>
15 #include <vector>
16 #include <unordered_map>
17 #include <memory>
18 #include <mutex>
19 #include "bgp-rib.h"
20 #include "prefix6.h"
21 #include "bgp-path-attrib.h"
22 #include "route-event-bus.h"
23 
24 namespace libbgp {
25 
31 public:
32  BgpRib6EntryKey() {}
33  BgpRib6EntryKey(const Prefix6 &prefix) {
34  prefix.getPrefix(this->prefix);
35  length = prefix.getLength();
36 
37  uint64_t prefix_pre = 0;
38  memcpy(&prefix_pre, this->prefix, 8);
39  hash = prefix_pre;
40  }
41 
42  bool operator== (const BgpRib6EntryKey &other) const {
43  return memcmp(other.prefix, prefix, 16) == 0 &&
44  length == other.length;
45  }
46 
47  uint8_t prefix[16];
48  uint8_t length;
49  uint64_t hash;
50 };
51 
57  std::size_t operator()(const BgpRib6EntryKey &key) const {
58  return key.hash;
59  }
60 };
61 
66 class BgpRib6Entry : public BgpRibEntry<BgpRib6Entry> {
67 public:
68  BgpRib6Entry (Prefix6 r, uint32_t src, const uint8_t nexthop_global[16],
69  const uint8_t nexthop_linklocal[16],
70  const std::vector<std::shared_ptr<BgpPathAttrib>> attribs);
71 
77 
82  uint8_t nexthop_global[16];
83 
89  uint8_t nexthop_linklocal[16];
90 };
91 
92 typedef std::unordered_multimap<BgpRib6EntryKey, BgpRib6Entry, BgpRib6EntryHash> rib6_t;
93 
98 class BgpRib6 : private BgpRib<BgpRib6Entry> {
99 public:
100  BgpRib6(BgpLogHandler *logger);
101 
102  // insert a route as local routing information
103  const BgpRib6Entry* insert(BgpLogHandler *logger,
104  const Prefix6 &route, const uint8_t nexthop_global[16],
105  const uint8_t nexthop_linklocal[16], int32_t weight = 0);
106 
107  const std::vector<BgpRib6Entry> insert(BgpLogHandler *logger,
108  const std::vector<Prefix6> &routes, const uint8_t nexthop_global[16],
109  const uint8_t nexthop_linklocal[16], int32_t weight = 0);
110 
111  // insert a new route into RIB, return BgpRib6Entry that should be send to other peers.
112  // <NULL, false> if a better route is already exist
113  // <BgpRib6Entry*, false> if inserted route replaced current best route, and another route become the new best
114  // <BgpRib6Entry*, true> if inserted route become the new best route
115  std::pair<const BgpRib6Entry*, bool> insert(uint32_t src_router_id,
116  const Prefix6 &route,
117  const uint8_t nexthop_global[16], const uint8_t nexthop_linklocal[16],
118  const std::vector<std::shared_ptr<BgpPathAttrib>> &attrib, int32_t weight,
119  uint32_t ibgp_asn);
120 
121  // insert new routes w/ common attribs.
122  // returns a pair: <updated_routes, new_best_routes> where updated_routes is a vector
123  // containing routes with different attribute then provided.
124  std::pair<std::vector<BgpRib6Entry>, std::vector<Prefix6>> insert(
125  uint32_t src_router_id, const std::vector<Prefix6> &routes,
126  const uint8_t nexthop_global[16], const uint8_t nexthop_linklocal[16],
127  const std::vector<std::shared_ptr<BgpPathAttrib>> &attrib, int32_t weight,
128  uint32_t ibgp_asn);
129 
130  // remove a route from RIB
131  std::pair<bool, const BgpRib6Entry*> withdraw(uint32_t src_router_id, const Prefix6 &route);
132 
133  // remove all routes from a peer, return <unreachabled routes, updated_routes>.
134  std::pair<std::vector<Prefix6>, std::vector<BgpRib6Entry>> discard(uint32_t src_router_id);
135 
136  // lookup in rib, return null if not found
137  const BgpRib6Entry* lookup(const uint8_t dest[16]) const;
138 
139  // scoped lookup in rib, return null if not found
140  const BgpRib6Entry* lookup(uint32_t src_router_id, const uint8_t dest[16]) const;
141 
142  // get RIB
143  const rib6_t &get() const;
144 private:
145  rib6_t::iterator find_best (const Prefix6 &prefix);
146  rib6_t::iterator find_entry (const Prefix6 &prefix, uint32_t src);
147 
148  std::pair<const BgpRib6Entry*, bool> insertPriv(uint32_t src_router_id,
149  const Prefix6 &route,
150  const uint8_t nexthop_global[16], const uint8_t nexthop_linklocal[16],
151  const std::vector<std::shared_ptr<BgpPathAttrib>> &attrib,
152  int32_t weight, uint32_t ibgp_asn);
153 
154  rib6_t rib;
155  std::recursive_mutex mutex;
156  BgpLogHandler *logger;
157  uint64_t update_id;
158 };
159 
160 }
161 #endif // RIB6_H_
IPv6 Route/Prefix related utilities.
Definition: prefix6.h:27
The BGP path attributes.
Hasher for the Rib6 entry key.
Definition: bgp-rib6.h:56
The route event bus.
Key for the Rib6 entry map.
Definition: bgp-rib6.h:30
The base of BGP RIB entry.
Definition: bgp-rib.h:35
uint8_t getLength() const
Get netmask.
Definition: prefix6.cc:459
The Base of BGP RIB.
Definition: bgp-rib.h:224
The BgpRib6Entry class.
Definition: bgp-rib6.h:66
The BgpRib6 (IPv6 BGP Routing Information Base) class.
Definition: bgp-rib6.h:98
Definition: bgp-afi.h:14
The BgpLogHandler class.
IPv6 Route/Prefix related utilities.
void getPrefix(uint8_t prefix[16]) const
Get prefix.
Definition: prefix6.cc:450
Prefix6 route
The prefix of this entry.
Definition: bgp-rib6.h:76