OpenSDN source code
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bgp_ribout.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef SRC_BGP_BGP_RIBOUT_H_
6 #define SRC_BGP_BGP_RIBOUT_H_
7 
8 #include <boost/scoped_ptr.hpp>
9 #include <boost/intrusive/slist.hpp>
10 
11 #include <algorithm>
12 #include <string>
13 #include <vector>
14 
15 #include "base/bitset.h"
16 #include "base/index_map.h"
17 #include "bgp/bgp_attr.h"
18 #include "bgp/bgp_proto.h"
19 #include "bgp/bgp_rib_policy.h"
20 #include "db/db_entry.h"
21 #include "net/tunnel_encap_type.h"
22 
23 class IPeer;
24 class IPeerUpdate;
25 class RibOutUpdates;
26 class ShowRibOutStatistics;
27 class BgpTable;
28 class BgpExport;
29 class BgpRoute;
30 class BgpUpdateSender;
31 class RouteUpdate;
32 class UpdateInfoSList;
33 
34 //
35 // This class represents the attributes for a ribout entry, including the
36 // label. It is essentially a combination of a smart pointer to BgpAttr
37 // and a label. The label is not included in BgpAttr in order to maximize
38 // sharing of the BgpAttr.
39 //
40 class RibOutAttr {
41 public:
42  // This nested class represents an ecmp element for a ribout entry. A
43  // ribout entry keeps a vector of these elements. Each element stores
44  // all per nexthop properties in addition to the nexthop address and
45  // label.
46  //
47  // The origin_vn_index keeps track of the index of the VN from which
48  // the BgpPath originated. A value of -1 means that the VN is unknown.
49  class NextHop {
50  public:
51  NextHop(const BgpTable *table, IpAddress address,
52  const MacAddress &mac, uint32_t label, uint32_t l3_label,
53  const ExtCommunity *ext_community, bool vrf_originated);
54 
55  const IpAddress address() const { return address_; }
56  const MacAddress &mac() const { return mac_; }
57  uint32_t label() const { return label_; }
58  uint32_t l3_label() const { return l3_label_; }
59  const Ip4Address &source_address() const { return source_address_; }
60  int origin_vn_index() const { return origin_vn_index_; }
61  std::vector<std::string> encap() const { return encap_; }
62  std::vector<int> tag_list() const { return tag_list_; }
63 
64  int CompareTo(const NextHop &rhs) const;
65  bool operator==(const NextHop &rhs) const;
66  bool operator!=(const NextHop &rhs) const;
67  bool operator<(const NextHop &rhs) const;
68 
69  private:
72  uint32_t label_;
73  uint32_t l3_label_;
76  std::vector<std::string> encap_;
77  std::vector<int> tag_list_;
78  };
79 
80  typedef std::vector<NextHop> NextHopList;
81 
82  RibOutAttr();
83  RibOutAttr(const RibOutAttr &rhs);
84  RibOutAttr(const BgpRoute *route, const BgpAttr *attr, bool is_xmpp);
85  RibOutAttr(const BgpTable *table, const BgpAttr *attr, uint32_t label,
86  uint32_t l3_label = 0, bool is_xmpp = false);
87  RibOutAttr(const BgpTable *table, const BgpRoute *route,
88  const BgpAttr *attr, uint32_t label, bool include_nh = true,
89  bool is_xmpp = false);
90 
91  RibOutAttr &operator=(const RibOutAttr &rhs);
92  bool operator==(const RibOutAttr &rhs) const { return CompareTo(rhs) == 0; }
93  bool operator!=(const RibOutAttr &rhs) const { return CompareTo(rhs) != 0; }
94  bool IsReachable() const { return attr_out_.get() != NULL; }
95 
96  const NextHopList &nexthop_list() const { return nexthop_list_; }
97  const BgpAttr *attr() const { return attr_out_.get(); }
98  void set_attr(const BgpTable *table, const BgpAttrPtr &attrp) {
99  set_attr(table, attrp, 0, 0, false, false);
100  }
101  void set_attr(const BgpTable *table, const BgpAttrPtr &attrp,
102  uint32_t label) {
103  set_attr(table, attrp, label, 0, false, false);
104  }
105  void set_attr(const BgpTable *table, const BgpAttrPtr &attrp,
106  uint32_t label, uint32_t l3_label, bool vrf_originated, bool is_xmpp);
109  }
110 
111  void clear() {
112  attr_out_.reset();
113  nexthop_list_.clear();
114  }
115  uint32_t label() const {
116  return nexthop_list_.empty() ? label_ : nexthop_list_[0].label();
117  }
118  uint32_t l3_label() const {
119  return nexthop_list_.empty() ? l3_label_ : nexthop_list_[0].l3_label();
120  }
121  const Ip4Address &source_address() const { return source_address_; }
123  bool is_xmpp() const { return is_xmpp_; }
124  bool vrf_originated() const { return vrf_originated_; }
125  const std::string &repr() const { return repr_; }
126  void set_repr(const std::string &repr, size_t pos = 0) const {
127  repr_.clear();
128  repr_.append(repr, pos, std::string::npos);
129  }
130 
131 private:
132  int CompareTo(const RibOutAttr &rhs) const;
133 
136  uint32_t label_;
137  uint32_t l3_label_;
139  bool is_xmpp_;
141  mutable std::string repr_;
142 };
143 
144 //
145 // This class represents a bitset of peers within a RibOut. This is distinct
146 // from the GroupPeerSet in order to allow it to be denser. This is possible
147 // because not every peer in the group may be interested in every RibOut.
148 //
149 class RibPeerSet : public BitSet {
150 };
151 
152 //
153 // This class represents information for a particular prefix that has been
154 // advertised to a set of peers.
155 //
156 // An AdvertiseInfo is part of a intrusive singly linked list container in
157 // the RouteState. The RibPeerSet represents the set of peers to which the
158 // prefix has been advertised and the RibOutAttr represents the associated
159 // attributes. This representation allows us to keep track of a different
160 // set of attributes for each set of peers.
161 //
164  explicit AdvertiseInfo(const RibOutAttr *roattr) : roattr(*roattr) { }
166  : bitset(rhs.bitset), roattr(rhs.roattr) {
167  }
168 
169  // Intrusive slist node for RouteState.
170  boost::intrusive::slist_member_hook<> slist_node;
171 
174 };
175 
176 //
177 // Disposer for AdvertiseInfo.
178 //
180  void operator()(AdvertiseInfo *ainfo) { delete ainfo; }
181 };
182 
183 //
184 // Wrapper for intrusive slist of AdvertiseInfos. Destructor automatically
185 // deletes any elements still on the slist.
186 //
187 // TBD: create a class template.
188 //
190 public:
191  typedef boost::intrusive::member_hook<
193  boost::intrusive::slist_member_hook<>,
195  > Node;
196  typedef boost::intrusive::slist<
198  Node,
199  boost::intrusive::linear<true>
200  > List;
201 
203  ~AdvertiseSList() { list_.clear_and_dispose(AdvertiseInfoDisposer()); }
204 
205  List *operator->() { return &list_; }
206  const List *operator->() const { return &list_; }
207  const List &list() const { return list_; }
208  void swap(AdvertiseSList &adv_slist) { list_.swap(adv_slist.list_); }
209 
210 private:
212 };
213 
214 //
215 // This class represents per prefix information that been advertised to
216 // the peers in a RibOut.
217 //
218 // A RouteState is derived from a DBState which means that it is part of
219 // the state map within a DBEntry. This allows the RouteState to be
220 // associated with a DBEntry using the listener id of the RibOut as the
221 // index. In the steady state i.e. when there are no pending updates the
222 // DBEntry maps the listener id for the RibOut to a RouteState.
223 //
224 // A RouteState maintains a singly linked list of AdvertiseInfo entries
225 // to keep track of the attributes that have been advertised to each set
226 // of peers.
227 //
228 class RouteState : public DBState {
229 public:
230  RouteState();
231 
232  void SetHistory(AdvertiseSList &history) {
233  assert(advertised_->empty());
234  advertised_.swap(history);
235  }
236  void SwapHistory(AdvertiseSList &history) {
237  advertised_.swap(history);
238  }
239  void MoveHistory(RouteUpdate *rt_update);
240  const AdvertiseInfo *FindHistory(const RibOutAttr &roattr) const;
241  bool CompareUpdateInfo(const UpdateInfoSList &uinfo_slist) const;
242 
243  const AdvertiseSList &Advertised() const { return advertised_; }
245 
246 private:
249 };
250 
251 //
252 // This class represents per-table state for a collection of peers with the
253 // same export policy. It is effectively a combination of RibExportPolicy
254 // and BgpTable. A RibOut has a 1:N association with RibOutUpdates wherein
255 // one entry is created per DB partition.
256 //
257 // A RibOut maintains a PeerStateMap to facilitate allocation and lookup of
258 // a bit index per peer in the RibOut.
259 //
260 class RibOut {
261 public:
262  class PeerIterator {
263  public:
264  PeerIterator(const RibOut *ribout, const RibPeerSet &peer_set)
265  : ribout_(ribout), peer_set_(peer_set) {
267  }
268  bool HasNext() const {
269  return index_ != RibPeerSet::npos;
270  }
274  return ptr;
275  }
276  int index() const { return index_; }
277 
278  private:
279  const RibOut *ribout_;
281  size_t index_;
282  };
283 
285  const RibExportPolicy &policy);
286  ~RibOut();
287 
288  void RegisterListener();
289  void Register(IPeerUpdate *peer);
290  void Unregister(IPeerUpdate *peer);
291  bool IsRegistered(IPeerUpdate *peer);
292  void Deactivate(IPeerUpdate *peer);
293  bool IsActive(IPeerUpdate *peer) const;
294  void BuildSendReadyBitSet(const RibPeerSet &peerset,
295  RibPeerSet *mready) const;
296 
297  IPeerUpdate *GetPeer(int index) const;
298  int GetPeerIndex(IPeerUpdate *peer) const;
299 
300  // Returns a bitmask with all the peers that are advertising this RibOut.
301  const RibPeerSet &PeerSet() const;
302  void GetSubsetPeerSet(RibPeerSet *peerset, const IPeerUpdate *cpeer) const;
303 
304  BgpTable *table() { return table_; }
305  const BgpTable *table() const { return table_; }
307 
308  const RibExportPolicy &ExportPolicy() const { return policy_; }
309 
310  int RouteAdvertiseCount(const BgpRoute *rt) const;
311  uint32_t GetQueueSize() const;
312 
314  const std::string &ToString() const { return name_; }
315 
316  RibOutUpdates *updates(int idx) { return updates_[idx]; }
317  const RibOutUpdates *updates(int idx) const { return updates_[idx]; }
318  BgpExport *bgp_export() { return bgp_export_.get(); }
319 
321  as_t peer_as() const { return policy_.as_number; }
322  as_t local_as() const { return policy_.local_as_number; }
323  bool as_override() const { return policy_.as_override; }
324  bool llgr() const { return policy_.llgr; }
325  bool as4_supported() const { return policy_.as4_supported; }
326  void set_as4_supported(bool as4) { policy_.as4_supported = as4; }
327  const IpAddress &nexthop() const { return policy_.nexthop; }
328  bool IsEncodingXmpp() const {
330  }
331  bool IsEncodingBgp() const {
333  }
334  std::string EncodingString() const {
335  return IsEncodingXmpp() ? "XMPP" : "BGP";
336  }
337  bool remove_private_enabled() const {
339  }
340  bool remove_private_all() const { return policy_.remove_private.all; }
341  bool remove_private_replace() const {
343  }
346  }
347  uint32_t cluster_id() const { return policy_.cluster_id; }
348 
349  void FillStatisticsInfo(std::vector<ShowRibOutStatistics> *sros_list) const;
350 
351 private:
352  struct PeerState {
353  explicit PeerState(IPeerUpdate *key) : peer(key), index(-1) {
354  }
355  void set_index(int idx) { index = idx; }
357  int index;
358  };
360 
364  std::string name_;
368  std::vector<RibOutUpdates *> updates_;
369  boost::scoped_ptr<BgpExport> bgp_export_;
370 
372 };
373 
374 #endif // SRC_BGP_BGP_RIBOUT_H_
int GetPeerIndex(IPeerUpdate *peer) const
Definition: bgp_ribout.cc:536
int index() const
Definition: bgp_ribout.h:276
AdvertiseInfo(const RibOutAttr *roattr)
Definition: bgp_ribout.h:164
bool remove_private_enabled() const
Definition: bgp_ribout.h:337
std::vector< NextHop > NextHopList
Definition: bgp_ribout.h:80
RibOutAttr & operator=(const RibOutAttr &rhs)
Definition: bgp_ribout.cc:188
BgpTable * table()
Definition: bgp_ribout.h:304
int listener_id_
Definition: bgp_ribout.h:367
uint32_t label_
Definition: bgp_ribout.h:136
void set_attr(const BgpTable *table, const BgpAttrPtr &attrp, uint32_t label)
Definition: bgp_ribout.h:101
std::vector< RibOutUpdates * > updates_
Definition: bgp_ribout.h:368
const BgpAttr * attr() const
Definition: bgp_ribout.h:97
const MacAddress & mac() const
Definition: bgp_ribout.h:56
IpAddress nexthop
bool operator!=(const NextHop &rhs) const
Definition: bgp_ribout.cc:70
const RibPeerSet & PeerSet() const
Definition: bgp_ribout.cc:504
Ip4Address source_address_
Definition: bgp_ribout.h:74
void set_as4_supported(bool as4)
Definition: bgp_ribout.h:326
const IpAddress & nexthop() const
Definition: bgp_ribout.h:327
bool is_xmpp_
Definition: bgp_ribout.h:139
RibExportPolicy policy_
Definition: bgp_ribout.h:363
Ip4Address source_address_
Definition: bgp_ribout.h:138
uint32_t cluster_id() const
Definition: bgp_ribout.h:347
bool llgr() const
Definition: bgp_ribout.h:324
const std::string & ToString() const
Definition: bgp_ribout.h:314
void MoveHistory(RouteUpdate *rt_update)
Definition: bgp_ribout.cc:248
const NextHopList & nexthop_list() const
Definition: bgp_ribout.h:96
bool as4_supported() const
Definition: bgp_ribout.h:325
boost::asio::ip::address IpAddress
Definition: address.h:13
const RibExportPolicy & ExportPolicy() const
Definition: bgp_ribout.h:308
BgpProto::BgpPeerType peer_type() const
Definition: bgp_ribout.h:320
void Unregister(IPeerUpdate *peer)
Definition: bgp_ribout.cc:378
BgpAttrPtr attr_out_
Definition: bgp_ribout.h:134
bool as_override() const
Definition: bgp_ribout.h:323
DISALLOW_COPY_AND_ASSIGN(RouteState)
int ListenerId
Definition: db_table.h:62
boost::intrusive::slist_member_hook slist_node
Definition: bgp_ribout.h:170
IpAddress address_
Definition: bgp_ribout.h:70
PeerIterator(const RibOut *ribout, const RibPeerSet &peer_set)
Definition: bgp_ribout.h:264
void SetHistory(AdvertiseSList &history)
Definition: bgp_ribout.h:232
std::vector< std::string > encap() const
Definition: bgp_ribout.h:61
const std::string & repr() const
Definition: bgp_ribout.h:125
std::vector< int > tag_list_
Definition: bgp_ribout.h:77
void Register(IPeerUpdate *peer)
Definition: bgp_ribout.cc:357
bool HasNext() const
Definition: bgp_ribout.h:268
const List * operator->() const
Definition: bgp_ribout.h:206
uint32_t as_t
Definition: bgp_common.h:21
Definition: ipeer.h:186
const Ip4Address & source_address() const
Definition: bgp_ribout.h:121
uint32_t GetQueueSize() const
Definition: bgp_ribout.cc:485
int CompareTo(const NextHop &rhs) const
Definition: bgp_ribout.cc:48
const List & list() const
Definition: bgp_ribout.h:207
boost::intrusive::slist< AdvertiseInfo, Node, boost::intrusive::linear< true > > List
Definition: bgp_ribout.h:200
uint32_t l3_label_
Definition: bgp_ribout.h:73
boost::intrusive_ptr< const BgpAttr > BgpAttrPtr
Definition: bgp_attr.h:991
Ip4Address * source_address()
Definition: bgp_ribout.h:122
bool vrf_originated() const
Definition: bgp_ribout.h:124
std::string name_
Definition: bgp_ribout.h:364
IPeerUpdate * peer
Definition: bgp_ribout.h:356
AdvertiseInfo(const AdvertiseInfo &rhs)
Definition: bgp_ribout.h:165
bool operator==(const RibOutAttr &rhs) const
Definition: bgp_ribout.h:92
NextHopList nexthop_list_
Definition: bgp_ribout.h:135
static const size_t npos
Definition: bitset.h:19
const BgpTable * table() const
Definition: bgp_ribout.h:305
BgpUpdateSender * sender()
Definition: bgp_ribout.h:306
void GetSubsetPeerSet(RibPeerSet *peerset, const IPeerUpdate *cpeer) const
Definition: bgp_ribout.cc:512
uint32_t l3_label() const
Definition: bgp_ribout.h:58
BgpProto::BgpPeerType type
RibPeerSet bitset
Definition: bgp_ribout.h:172
void set_attr(const BgpTable *table, const BgpAttrPtr &attrp)
Definition: bgp_ribout.h:98
uint32_t cluster_id
List * operator->()
Definition: bgp_ribout.h:205
bool IsRegistered(IPeerUpdate *peer)
Definition: bgp_ribout.cc:398
DISALLOW_COPY_AND_ASSIGN(RibOut)
std::vector< std::string > encap_
Definition: bgp_ribout.h:76
AdvertiseSList advertised_
Definition: bgp_ribout.h:247
bool remove_private_all() const
Definition: bgp_ribout.h:340
void set_source_address(Ip4Address source_address)
Definition: bgp_ribout.h:107
bool IsEncodingBgp() const
Definition: bgp_ribout.h:331
IPeerUpdate * GetPeer(int index) const
Definition: bgp_ribout.cc:525
RemovePrivatePolicy remove_private
bool IsActive(IPeerUpdate *peer) const
Definition: bgp_ribout.cc:418
bool operator==(const NextHop &rhs) const
Definition: bgp_ribout.cc:66
RibOut(BgpTable *table, BgpUpdateSender *sender, const RibExportPolicy &policy)
Definition: bgp_ribout.cc:297
uint32_t l3_label() const
Definition: bgp_ribout.h:118
MacAddress mac_
Definition: bgp_ribout.h:71
bool IsReachable() const
Definition: bgp_ribout.h:94
as_t peer_as() const
Definition: bgp_ribout.h:321
void set_repr(const std::string &repr, size_t pos=0) const
Definition: bgp_ribout.h:126
void FillStatisticsInfo(std::vector< ShowRibOutStatistics > *sros_list) const
Definition: bgp_ribout.cc:545
BgpExport * bgp_export()
Definition: bgp_ribout.h:318
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
Definition: bitset.h:17
std::vector< int > tag_list() const
Definition: bgp_ribout.h:62
std::string repr_
Definition: bgp_ribout.h:141
const RibOut * ribout_
Definition: bgp_ribout.h:279
const AdvertiseSList & Advertised() const
Definition: bgp_ribout.h:243
AdvertiseSList & Advertised()
Definition: bgp_ribout.h:244
bool operator!=(const RibOutAttr &rhs) const
Definition: bgp_ribout.h:93
uint32_t label() const
Definition: bgp_ribout.h:115
uint32_t l3_label_
Definition: bgp_ribout.h:137
std::string EncodingString() const
Definition: bgp_ribout.h:334
BgpTable * table_
Definition: bgp_ribout.h:361
const Ip4Address & source_address() const
Definition: bgp_ribout.h:59
BgpUpdateSender * sender_
Definition: bgp_ribout.h:362
bool operator<(const NextHop &rhs) const
Definition: bgp_ribout.cc:74
const RibPeerSet & peer_set_
Definition: bgp_ribout.h:280
size_t find_first() const
Definition: bitset.cc:242
const IpAddress address() const
Definition: bgp_ribout.h:55
bool remove_private_replace() const
Definition: bgp_ribout.h:341
int RouteAdvertiseCount(const BgpRoute *rt) const
Definition: bgp_ribout.cc:440
bool IsEncodingXmpp() const
Definition: bgp_ribout.h:328
void clear()
Definition: bgp_ribout.h:111
bool is_xmpp() const
Definition: bgp_ribout.h:123
PeerState(IPeerUpdate *key)
Definition: bgp_ribout.h:353
boost::intrusive::member_hook< AdvertiseInfo, boost::intrusive::slist_member_hook<>,&AdvertiseInfo::slist_node > Node
Definition: bgp_ribout.h:195
as_t local_as() const
Definition: bgp_ribout.h:322
void Deactivate(IPeerUpdate *peer)
Definition: bgp_ribout.cc:411
size_t find_next(size_t pos) const
Definition: bitset.cc:255
const RibOutUpdates * updates(int idx) const
Definition: bgp_ribout.h:317
int origin_vn_index() const
Definition: bgp_ribout.h:60
void BuildSendReadyBitSet(const RibPeerSet &peerset, RibPeerSet *mready) const
Definition: bgp_ribout.cc:426
RibOutUpdates * updates(int idx)
Definition: bgp_ribout.h:316
RibPeerSet active_peerset_
Definition: bgp_ribout.h:366
bool vrf_originated_
Definition: bgp_ribout.h:140
bool CompareUpdateInfo(const UpdateInfoSList &uinfo_slist) const
Definition: bgp_ribout.cc:276
const AdvertiseInfo * FindHistory(const RibOutAttr &roattr) const
Definition: bgp_ribout.cc:257
boost::scoped_ptr< BgpExport > bgp_export_
Definition: bgp_ribout.h:369
void RegisterListener()
Definition: bgp_ribout.cc:343
uint32_t label() const
Definition: bgp_ribout.h:57
IndexMap< IPeerUpdate *, PeerState, RibPeerSet > PeerStateMap
Definition: bgp_ribout.h:359
void SwapHistory(AdvertiseSList &history)
Definition: bgp_ribout.h:236
bool remove_private_peer_loop_check() const
Definition: bgp_ribout.h:344
DBTableBase::ListenerId listener_id() const
Definition: bgp_ribout.h:313
NextHop(const BgpTable *table, IpAddress address, const MacAddress &mac, uint32_t label, uint32_t l3_label, const ExtCommunity *ext_community, bool vrf_originated)
Definition: bgp_ribout.cc:28
void set_index(int idx)
Definition: bgp_ribout.h:355
IPeerUpdate * Next()
Definition: bgp_ribout.h:271
void operator()(AdvertiseInfo *ainfo)
Definition: bgp_ribout.h:180
void swap(AdvertiseSList &adv_slist)
Definition: bgp_ribout.h:208
PeerStateMap state_map_
Definition: bgp_ribout.h:365
RibOutAttr roattr
Definition: bgp_ribout.h:173
int CompareTo(const RibOutAttr &rhs) const
Definition: bgp_ribout.cc:203