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