-
Notifications
You must be signed in to change notification settings - Fork 390
/
bgp_message_builder.cc
240 lines (195 loc) · 7.03 KB
/
bgp_message_builder.cc
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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "bgp/bgp_message_builder.h"
#include "base/parse_object.h"
#include "bgp/bgp_log.h"
#include "bgp/bgp_route.h"
#include "net/bgp_af.h"
using std::auto_ptr;
BgpMessage::BgpMessage(const BgpTable *table) : table_(table), datalen_(0) {
}
BgpMessage::~BgpMessage() {
}
bool BgpMessage::StartReach(const RibOutAttr *roattr, const BgpRoute *route) {
BgpProto::Update update;
const BgpAttr *attr = roattr->attr();
BgpAttrOrigin *origin = new BgpAttrOrigin(attr->origin());
update.path_attributes.push_back(origin);
if ((route->Afi() == BgpAf::IPv4) && (route->Safi() == BgpAf::Unicast)) {
BgpAttrNextHop *nh = new BgpAttrNextHop(attr->nexthop().to_v4().to_ulong());
update.path_attributes.push_back(nh);
}
if (attr->med()) {
BgpAttrMultiExitDisc *med = new BgpAttrMultiExitDisc(attr->med());
update.path_attributes.push_back(med);
}
if (attr->local_pref()) {
BgpAttrLocalPref *lp = new BgpAttrLocalPref(attr->local_pref());
update.path_attributes.push_back(lp);
}
if (attr->atomic_aggregate()) {
BgpAttrAtomicAggregate *aa = new BgpAttrAtomicAggregate;
update.path_attributes.push_back(aa);
}
if (attr->aggregator_as_num()) {
BgpAttrAggregator *agg = new BgpAttrAggregator(
attr->aggregator_as_num(),
attr->aggregator_adderess().to_v4().to_ulong());
update.path_attributes.push_back(agg);
}
if (!attr->originator_id().is_unspecified()) {
BgpAttrOriginatorId *originator_id =
new BgpAttrOriginatorId(attr->originator_id().to_ulong());
update.path_attributes.push_back(originator_id);
}
if (attr->as_path()) {
AsPathSpec *path = new AsPathSpec(attr->as_path()->path());
update.path_attributes.push_back(path);
}
if (attr->edge_discovery()) {
EdgeDiscoverySpec *edspec =
new EdgeDiscoverySpec(attr->edge_discovery()->edge_discovery());
update.path_attributes.push_back(edspec);
}
if (attr->edge_forwarding()) {
EdgeForwardingSpec *efspec =
new EdgeForwardingSpec(attr->edge_forwarding()->edge_forwarding());
update.path_attributes.push_back(efspec);
}
if (attr->community() && attr->community()->communities().size()) {
CommunitySpec *comm = new CommunitySpec;
comm->communities = attr->community()->communities();
update.path_attributes.push_back(comm);
}
if (attr->ext_community() && attr->ext_community()->communities().size()) {
ExtCommunitySpec *ext_comm = new ExtCommunitySpec;
const ExtCommunity::ExtCommunityList &v =
attr->ext_community()->communities();
for (ExtCommunity::ExtCommunityList::const_iterator it = v.begin();
it != v.end(); ++it) {
uint64_t value = get_value(it->data(), it->size());
ext_comm->communities.push_back(value);
}
update.path_attributes.push_back(ext_comm);
}
if (attr->pmsi_tunnel()) {
PmsiTunnelSpec *pmsi_spec =
new PmsiTunnelSpec(attr->pmsi_tunnel()->pmsi_tunnel());
update.path_attributes.push_back(pmsi_spec);
}
std::vector<uint8_t> nh;
route->BuildBgpProtoNextHop(nh, attr->nexthop());
BgpMpNlri *nlri =
new BgpMpNlri(BgpAttribute::MPReachNlri, route->Afi(), route->Safi(), nh);
update.path_attributes.push_back(nlri);
BgpProtoPrefix *prefix = new BgpProtoPrefix;
route->BuildProtoPrefix(prefix, attr, roattr->label());
nlri->nlri.push_back(prefix);
int result =
BgpProto::Encode(&update, data_, sizeof(data_), &encode_offsets_);
if (result <= 0) {
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_WARN, BGP_LOG_FLAG_ALL,
"Error encoding reach message for route " << route->ToString() <<
" in table " << (table_ ? table_->name() : "unknown"));
table_->server()->increment_message_build_error();
return false;
}
num_reach_route_++;
datalen_ = result;
return true;
}
bool BgpMessage::StartUnreach(const BgpRoute *route) {
BgpProto::Update update;
BgpMpNlri *nlri =
new BgpMpNlri(BgpAttribute::MPUnreachNlri, route->Afi(), route->Safi());
update.path_attributes.push_back(nlri);
BgpProtoPrefix *prefix = new BgpProtoPrefix;
route->BuildProtoPrefix(prefix);
nlri->nlri.push_back(prefix);
int result =
BgpProto::Encode(&update, data_, sizeof(data_), &encode_offsets_);
if (result <= 0) {
BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_WARN, BGP_LOG_FLAG_ALL,
"Error encoding unreach message for route " << route->ToString() <<
" in table " << (table_ ? table_->name() : "unknown"));
table_->server()->increment_message_build_error();
return false;
}
num_unreach_route_++;
datalen_ = result;
return true;
}
bool BgpMessage::Start(const RibOutAttr *roattr, const BgpRoute *route) {
if (roattr->IsReachable()) {
return StartReach(roattr, route);
} else {
return StartUnreach(route);
}
}
bool BgpMessage::UpdateLength(const char *tag, int size, int delta) {
int offset = encode_offsets_.FindOffset(tag);
if (offset < 0) {
return false;
}
int value = get_value(&data_[offset], size);
value += delta;
put_value(&data_[offset], size, value);
return true;
}
bool BgpMessage::AddRoute(const BgpRoute *route, const RibOutAttr *roattr) {
uint8_t *data = data_ + datalen_;
size_t size = sizeof(data_) - datalen_;
BgpMpNlri nlri;
nlri.afi = route->Afi();
nlri.safi = route->Safi();
BgpProtoPrefix *prefix = new BgpProtoPrefix;
if (roattr) {
route->BuildProtoPrefix(prefix, roattr->attr(), roattr->label());
} else {
route->BuildProtoPrefix(prefix);
}
nlri.nlri.push_back(prefix);
if (roattr->IsReachable()) {
num_reach_route_++;
} else {
num_unreach_route_++;
}
int result = BgpProto::Encode(&nlri, data, size);
if (result <= 0) return false;
datalen_ += result;
if (!UpdateLength("BgpMsgLength", 2, result)) {
assert(false);
return false;
}
if (!UpdateLength("BgpPathAttribute", 2, result)) {
assert(false);
return false;
}
if (!UpdateLength("MpReachUnreachNlri", 2, result)) {
assert(false);
return false;
}
return true;
}
void BgpMessage::Finish() {
}
const uint8_t *BgpMessage::GetData(IPeerUpdate *ipeer_update, size_t *lenp) {
*lenp = datalen_;
return data_;
}
Message *BgpMessageBuilder::Create(const BgpTable *table,
const RibOutAttr *roattr, const BgpRoute *route) const {
auto_ptr<BgpMessage> msg(new BgpMessage(table));
if (msg->Start(roattr, route)) {
return msg.release();
} else {
return NULL;
}
}
BgpMessageBuilder BgpMessageBuilder::instance_;
BgpMessageBuilder::BgpMessageBuilder() {
}
BgpMessageBuilder *BgpMessageBuilder::GetInstance() {
return &instance_;
}