/
community.h
313 lines (264 loc) · 9.91 KB
/
community.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#ifndef SRC_BGP_COMMUNITY_H_
#define SRC_BGP_COMMUNITY_H_
#include <boost/array.hpp>
#include <boost/intrusive_ptr.hpp>
#include <tbb/atomic.h>
#include <set>
#include <string>
#include <vector>
#include "bgp/bgp_attr_base.h"
#include "base/parse_object.h"
#include "base/util.h"
class BgpAttr;
class CommunityDB;
class ExtCommunityDB;
class BgpServer;
struct CommunitySpec : public BgpAttribute {
static const int kSize = -1;
static const uint8_t kFlags = Optional | Transitive;
CommunitySpec() : BgpAttribute(Communities, kFlags) { }
explicit CommunitySpec(const BgpAttribute &rhs) : BgpAttribute(rhs) { }
std::vector<uint32_t> communities;
virtual int CompareTo(const BgpAttribute &rhs_attr) const {
int ret = BgpAttribute::CompareTo(rhs_attr);
if (ret != 0) return ret;
KEY_COMPARE(communities,
static_cast<const CommunitySpec &>(rhs_attr).communities);
return 0;
}
virtual void ToCanonical(BgpAttr *attr);
virtual std::string ToString() const;
virtual size_t EncodeLength() const;
};
class Community {
public:
enum WellKnownCommunity {
NoExport = 0xFFFFFF01,
NoAdvertise = 0xFFFFFF02,
NoExportSubconfed = 0xFFFFFF03,
};
explicit Community(CommunityDB *comm_db)
: comm_db_(comm_db) {
refcount_ = 0;
}
explicit Community(CommunityDB *comm_db, const CommunitySpec spec);
virtual ~Community() { }
virtual void Remove();
int CompareTo(const Community &rhs) const;
bool ContainsValue(uint32_t value) const;
const std::vector<uint32_t> &communities() const { return communities_; }
friend std::size_t hash_value(Community const &comm) {
size_t hash = 0;
boost::hash_range(hash, comm.communities_.begin(),
comm.communities_.end());
return hash;
}
private:
friend int intrusive_ptr_add_ref(const Community *ccomm);
friend int intrusive_ptr_del_ref(const Community *ccomm);
friend void intrusive_ptr_release(const Community *ccomm);
mutable tbb::atomic<int> refcount_;
CommunityDB *comm_db_;
std::vector<uint32_t> communities_;
};
inline int intrusive_ptr_add_ref(const Community *ccomm) {
return ccomm->refcount_.fetch_and_increment();
}
inline int intrusive_ptr_del_ref(const Community *ccomm) {
return ccomm->refcount_.fetch_and_decrement();
}
inline void intrusive_ptr_release(const Community *ccomm) {
int prev = ccomm->refcount_.fetch_and_decrement();
if (prev == 1) {
Community *comm = const_cast<Community *>(ccomm);
comm->Remove();
assert(comm->refcount_ == 0);
delete comm;
}
}
typedef boost::intrusive_ptr<const Community> CommunityPtr;
struct CommunityCompare {
bool operator()(const Community *lhs, const Community *rhs) {
return lhs->CompareTo(*rhs) < 0;
}
};
class CommunityDB : public BgpPathAttributeDB<Community, CommunityPtr,
CommunitySpec, CommunityCompare,
CommunityDB> {
public:
explicit CommunityDB(BgpServer *server);
virtual ~CommunityDB() { }
private:
};
class ExtCommunitySpec : public BgpAttribute {
public:
static const int kSize = -1;
static const uint8_t kFlags = Optional | Transitive;
ExtCommunitySpec() : BgpAttribute(ExtendedCommunities, kFlags) { }
explicit ExtCommunitySpec(const BgpAttribute &rhs) : BgpAttribute(rhs) { }
virtual size_t EncodeLength() const;
std::vector<uint64_t> communities;
virtual int CompareTo(const BgpAttribute &rhs_attr) const;
virtual void ToCanonical(BgpAttr *attr);
virtual std::string ToString() const;
};
class ExtCommunity {
public:
typedef boost::array<uint8_t, 8> ExtCommunityValue;
typedef std::vector<ExtCommunityValue> ExtCommunityList;
explicit ExtCommunity(ExtCommunityDB *extcomm_db)
: extcomm_db_(extcomm_db) {
refcount_ = 0;
}
explicit ExtCommunity(const ExtCommunity &rhs)
: extcomm_db_(rhs.extcomm_db_),
communities_(rhs.communities_) {
refcount_ = 0;
}
explicit ExtCommunity(ExtCommunityDB *extcomm_db,
const ExtCommunitySpec spec);
virtual ~ExtCommunity() { }
virtual void Remove();
int CompareTo(const ExtCommunity &rhs) const;
void Append(const ExtCommunityValue &value);
void Append(const ExtCommunityList &list);
bool ContainsOriginVn(const ExtCommunityValue &val) const;
void RemoveRTarget();
void RemoveSGID();
void RemoveSiteOfOrigin();
void RemoveOriginVn();
void RemoveTunnelEncapsulation();
// Return vector of communities
const ExtCommunityList &communities() const {
return communities_;
}
std::vector<std::string> GetTunnelEncap() const;
bool ContainsTunnelEncapVxlan() const;
static bool is_origin_vn(const ExtCommunityValue &val) {
//
// Origin VN extended community
// 2 Octet AS specific extended community
//
return (val[0] == 0x80) && (val[1] == 0x71);
}
static bool is_default_gateway(const ExtCommunityValue &val) {
//
// Default Gateway extended community
//
return (val[0] == 0x06) && (val[1] == 0x0d);
}
static bool is_es_import(const ExtCommunityValue &val) {
//
// ES Import extended community
//
return (val[0] == 0x06) && (val[1] == 0x02);
}
static bool is_esi_label(const ExtCommunityValue &val) {
//
// ESI Label extended community
//
return (val[0] == 0x06) && (val[1] == 0x01);
}
static bool is_mac_mobility(const ExtCommunityValue &val) {
//
// MAC Mobility extended community
//
return (val[0] == 0x06) && (val[1] == 0x00);
}
static bool is_route_target(const ExtCommunityValue &val) {
//
// Route target extended community
// 1. 2 Octet AS specific extended community Route Target
// 2. IPv4 Address specific extended community Route Target
// 3. 4 Octet AS specific extended community Route Target
//
return ((val[0] == 0x0 || (val[0] == 0x2) || (val[0] == 0x1)) &&
(val[1] == 0x2));
}
static bool is_security_group(const ExtCommunityValue &val) {
//
// SG ID extended community
// 2 Octet AS specific extended community
//
return (val[0] == 0x80) && (val[1] == 0x4);
}
static bool is_site_of_origin(const ExtCommunityValue &val) {
//
// Site of Origin / Route Origin extended community
// 1. 2 Octet AS specific extended community
// 2. IPv4 Address specific extended community
// 3. 4 Octet AS specific extended community Route Target
//
return ((val[0] == 0x00 || val[0] == 0x02 || val[0] == 0x01) &&
(val[1] == 0x3));
}
static bool is_tunnel_encap(const ExtCommunityValue &val) {
// Tunnel encap extended community
return (val[0] == 0x03) && (val[1] == 0x0C);
}
friend std::size_t hash_value(ExtCommunity const &comm) {
size_t hash = 0;
for (ExtCommunityList::const_iterator iter = comm.communities_.begin();
iter != comm.communities_.end(); iter++) {
boost::hash_range(hash, iter->begin(), iter->end());
}
return hash;
}
private:
friend int intrusive_ptr_add_ref(const ExtCommunity *cextcomm);
friend int intrusive_ptr_del_ref(const ExtCommunity *cextcomm);
friend void intrusive_ptr_release(const ExtCommunity *cextcomm);
mutable tbb::atomic<int> refcount_;
ExtCommunityDB *extcomm_db_;
ExtCommunityList communities_;
};
inline int intrusive_ptr_add_ref(const ExtCommunity *cextcomm) {
return cextcomm->refcount_.fetch_and_increment();
}
inline int intrusive_ptr_del_ref(const ExtCommunity *cextcomm) {
return cextcomm->refcount_.fetch_and_decrement();
}
inline void intrusive_ptr_release(const ExtCommunity *cextcomm) {
int prev = cextcomm->refcount_.fetch_and_decrement();
if (prev == 1) {
ExtCommunity *extcomm = const_cast<ExtCommunity *>(cextcomm);
extcomm->Remove();
assert(extcomm->refcount_ == 0);
delete extcomm;
}
}
typedef boost::intrusive_ptr<const ExtCommunity> ExtCommunityPtr;
struct ExtCommunityCompare {
bool operator()(const ExtCommunity *lhs, const ExtCommunity *rhs) {
return lhs->CompareTo(*rhs) < 0;
}
};
class ExtCommunityDB : public BgpPathAttributeDB<ExtCommunity, ExtCommunityPtr,
ExtCommunitySpec,
ExtCommunityCompare,
ExtCommunityDB> {
public:
explicit ExtCommunityDB(BgpServer *server);
ExtCommunityPtr AppendAndLocate(const ExtCommunity *src,
const ExtCommunity::ExtCommunityList &list);
ExtCommunityPtr AppendAndLocate(const ExtCommunity *src,
const ExtCommunity::ExtCommunityValue &value);
ExtCommunityPtr ReplaceRTargetAndLocate(const ExtCommunity *src,
const ExtCommunity::ExtCommunityList &export_list);
ExtCommunityPtr ReplaceSGIDListAndLocate(const ExtCommunity *src,
const ExtCommunity::ExtCommunityList &sgid_list);
ExtCommunityPtr RemoveSiteOfOriginAndLocate(const ExtCommunity *src);
ExtCommunityPtr ReplaceSiteOfOriginAndLocate(const ExtCommunity *src,
const ExtCommunity::ExtCommunityValue &soo);
ExtCommunityPtr RemoveOriginVnAndLocate(const ExtCommunity *src);
ExtCommunityPtr ReplaceOriginVnAndLocate(const ExtCommunity *src,
const ExtCommunity::ExtCommunityValue &origin_vn);
ExtCommunityPtr ReplaceTunnelEncapsulationAndLocate(
const ExtCommunity *src,
const ExtCommunity::ExtCommunityList &tunnel_encaps);
private:
};
#endif // SRC_BGP_COMMUNITY_H_