-
Notifications
You must be signed in to change notification settings - Fork 390
/
bgp_ribout.h
362 lines (310 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
356
357
358
359
360
361
362
/*
* 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 "db/db_entry.h"
#include "net/tunnel_encap_type.h"
class IPeer;
class IPeerUpdate;
class RibOutUpdates;
class SchedulingGroup;
class SchedulingGroupManager;
class BgpTable;
class BgpExport;
class BgpRoute;
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,
const ExtCommunity *ext_community, bool vrf_originated);
const IpAddress address() const { return address_; }
uint32_t label() const { return 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_;
int origin_vn_index_;
std::vector<std::string> encap_;
};
typedef std::vector<NextHop> NextHopList;
RibOutAttr() : attr_out_(NULL), vrf_originated_(false) { }
RibOutAttr(const BgpTable *table, const BgpAttr *attr, uint32_t label);
RibOutAttr(const BgpTable *table, const BgpRoute *route,
const BgpAttr *attr, uint32_t label, bool include_nh = true);
RibOutAttr(const BgpRoute *route, const BgpAttr *attr, bool is_xmpp);
bool IsReachable() const { return attr_out_.get() != NULL; }
bool operator==(const RibOutAttr &rhs) const { return CompareTo(rhs) == 0; }
bool operator!=(const RibOutAttr &rhs) const { return CompareTo(rhs) != 0; }
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,
uint32_t label = 0, bool vrf_originated = false);
void clear() {
attr_out_.reset();
nexthop_list_.clear();
}
uint32_t label() const {
return nexthop_list_.empty() ? 0 : nexthop_list_.at(0).label();
}
bool vrf_originated() const { return vrf_originated_; }
private:
int CompareTo(const RibOutAttr &rhs) const;
BgpAttrPtr attr_out_;
NextHopList nexthop_list_;
bool vrf_originated_;
};
//
// 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 the export policy for a rib. Given that we do not
// currently support any real policy configuration, this is pretty trivial
// for now.
//
// Including the AS number as part of the policy results in creation of a
// different RibOut for every neighbor AS that we peer with. This allows a
// simplified implementation of the sender side AS path loop check. In most
// practical deployment scenarios all eBGP peers will belong to the same
// neighbor AS anyway.
//
// Including the CPU affinity as part of the RibExportPolicy allows us to
// artificially create more RibOuts than otherwise necessary. This is used
// to achieve higher concurrency at the expense of creating more state.
//
struct RibExportPolicy {
enum Encoding {
BGP,
XMPP,
};
RibExportPolicy()
: type(BgpProto::IBGP), encoding(BGP),
as_number(0), affinity(-1), cluster_id(0) {
}
RibExportPolicy(BgpProto::BgpPeerType type, Encoding encoding,
int affinity, u_int32_t cluster_id)
: type(type), encoding(encoding), as_number(0),
affinity(affinity), cluster_id(cluster_id) {
if (encoding == XMPP)
assert(type == BgpProto::XMPP);
if (encoding == BGP)
assert(type == BgpProto::IBGP || type == BgpProto::EBGP);
}
RibExportPolicy(BgpProto::BgpPeerType type, Encoding encoding,
as_t as_number, int affinity, u_int32_t cluster_id)
: type(type), encoding(encoding), as_number(as_number),
affinity(affinity), cluster_id(cluster_id) {
if (encoding == XMPP)
assert(type == BgpProto::XMPP);
if (encoding == BGP)
assert(type == BgpProto::IBGP || type == BgpProto::EBGP);
}
bool operator<(const RibExportPolicy &rhs) const;
BgpProto::BgpPeerType type;
Encoding encoding;
as_t as_number;
int affinity;
uint32_t cluster_id;
};
//
// 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:1 association with RibOutUpdates to which
// it maintains a smart pointer.
//
// 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, SchedulingGroupManager *mgr,
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;
SchedulingGroup *GetSchedulingGroup();
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;
BgpTable* table() const { return table_; }
const RibExportPolicy &ExportPolicy() const { return policy_; }
int RouteAdvertiseCount(const BgpRoute *rt) const;
DBTableBase::ListenerId listener_id() const { return listener_id_; }
const std::string &ToString() const { return name_; }
RibOutUpdates *updates() { return updates_.get(); }
const RibOutUpdates *updates() const { return updates_.get(); }
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 IsEncodingXmpp() const {
return (policy_.encoding == RibExportPolicy::XMPP);
}
bool IsEncodingBgp() const {
return (policy_.encoding == RibExportPolicy::BGP);
}
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_;
SchedulingGroupManager *mgr_;
RibExportPolicy policy_;
std::string name_;
PeerStateMap state_map_;
RibPeerSet active_peerset_;
int listener_id_;
boost::scoped_ptr<RibOutUpdates> updates_;
boost::scoped_ptr<BgpExport> bgp_export_;
DISALLOW_COPY_AND_ASSIGN(RibOut);
};
#endif // SRC_BGP_BGP_RIBOUT_H_