Line data Source code
1 : /*
2 : * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3 : * Copyright Contributors to the OpenSDN project
4 : */
5 : #include "bgp/bgp_config.h"
6 :
7 : #include <boost/foreach.hpp>
8 :
9 : #include <sstream>
10 :
11 : #include "base/string_util.h"
12 : #include "base/time_util.h"
13 :
14 : using std::copy;
15 : using std::endl;
16 : using std::ostringstream;
17 : using std::ostream_iterator;
18 : using std::sort;
19 : using std::string;
20 : using std::swap;
21 : using std::vector;
22 :
23 : const char *BgpConfigManager::kMasterNetwork =
24 : "default-domain:default-project:ip-fabric";
25 : const char *BgpConfigManager::kMasterInstance =
26 : "default-domain:default-project:ip-fabric:__default__";
27 : const char *BgpConfigManager::kFabricInstance =
28 : "default-domain:default-project:ip-fabric:ip-fabric";
29 : const int BgpConfigManager::kDefaultPort = 179;
30 : const uint32_t BgpConfigManager::kDefaultAutonomousSystem = 64512;
31 :
32 1396 : static int CompareTo(const AuthenticationKey &lhs,
33 : const AuthenticationKey &rhs) {
34 1396 : KEY_COMPARE(lhs.id, rhs.id);
35 1396 : KEY_COMPARE(lhs.value, rhs.value);
36 1396 : KEY_COMPARE(lhs.start_time, rhs.start_time);
37 1396 : return 0;
38 : }
39 :
40 1328 : bool AuthenticationKey::operator<(const AuthenticationKey &rhs) const {
41 1328 : int cmp = CompareTo(*this, rhs);
42 1328 : return cmp < 0;
43 : }
44 :
45 68 : bool AuthenticationKey::operator==(const AuthenticationKey &rhs) const {
46 68 : int cmp = CompareTo(*this, rhs);
47 68 : return cmp == 0;
48 : }
49 :
50 1246 : AuthenticationData::AuthenticationData() : key_type_(NIL) {
51 1246 : }
52 :
53 0 : AuthenticationData::AuthenticationData(KeyType type,
54 0 : const AuthenticationKeyChain &chain) :
55 0 : key_type_(type), key_chain_(chain) {
56 0 : }
57 :
58 652 : bool AuthenticationData::operator<(const AuthenticationData &rhs) const {
59 652 : BOOL_KEY_COMPARE(key_type_, rhs.key_type());
60 652 : BOOL_KEY_COMPARE(key_chain_, rhs.key_chain());
61 652 : return false;
62 : }
63 :
64 234 : bool AuthenticationData::operator==(const AuthenticationData &rhs) const {
65 414 : if ((key_chain_.size() != rhs.key_chain().size()) ||
66 180 : (key_type_ != rhs.key_type())) {
67 54 : return false;
68 : }
69 :
70 248 : for (size_t ocnt = 0; ocnt < rhs.key_chain().size(); ++ocnt) {
71 68 : bool found = false;
72 68 : for (size_t icnt = 0; icnt < key_chain_.size(); ++icnt) {
73 68 : if (key_chain_[icnt] == rhs.key_chain()[ocnt]) {
74 68 : found = true;
75 68 : break;
76 : }
77 : }
78 68 : if (!found) {
79 0 : return false;
80 : }
81 : }
82 180 : return true;
83 : }
84 :
85 706 : AuthenticationData &AuthenticationData::operator=(
86 : const AuthenticationData &rhs) {
87 706 : set_key_type(rhs.key_type());
88 706 : set_key_chain(rhs.key_chain());
89 706 : return *this;
90 : }
91 :
92 252 : void AuthenticationData::AddKeyToKeyChain(const AuthenticationKey &key) {
93 252 : key_chain_.push_back(key);
94 252 : }
95 :
96 0 : const AuthenticationKey *AuthenticationData::Find(int key_id) const {
97 : const AuthenticationKey *item;
98 0 : for (size_t i = 0; i < key_chain_.size(); ++i) {
99 0 : item = &key_chain_[i];
100 0 : if (item->id == key_id) {
101 0 : return item;
102 : }
103 : }
104 0 : return NULL;
105 : }
106 :
107 382 : bool AuthenticationData::Empty() const {
108 382 : return key_chain_.empty();
109 : }
110 :
111 0 : void AuthenticationData::Clear() {
112 0 : key_chain_.clear();
113 0 : }
114 :
115 332 : string AuthenticationData::KeyTypeToString() const {
116 332 : switch (key_type_) {
117 172 : case MD5:
118 172 : return "MD5";
119 160 : default:
120 160 : return "NIL";
121 : }
122 : }
123 :
124 847 : string AuthenticationData::KeyTypeToString(KeyType key_type) {
125 847 : switch (key_type) {
126 847 : case MD5:
127 847 : return "MD5";
128 0 : default:
129 0 : return "NIL";
130 : }
131 : }
132 :
133 332 : vector<string> AuthenticationData::KeysToString() const {
134 332 : AuthenticationKeyChain::const_iterator iter;
135 332 : vector<string> auth_keys;
136 504 : for (iter = key_chain_.begin(); iter != key_chain_.end(); ++iter) {
137 172 : AuthenticationKey key = *iter;
138 172 : auth_keys.push_back(integerToString(key.id));
139 172 : }
140 664 : return auth_keys;
141 0 : }
142 :
143 : // NOTE: This prints the actual key too. Use with care.
144 0 : vector<string> AuthenticationData::KeysToStringDetail() const {
145 0 : AuthenticationKeyChain::const_iterator iter;
146 0 : vector<string> auth_keys;
147 0 : for (iter = key_chain_.begin(); iter != key_chain_.end(); ++iter) {
148 0 : AuthenticationKey key = *iter;
149 0 : auth_keys.push_back(integerToString(key.id) + ":" + key.value);
150 0 : }
151 0 : return auth_keys;
152 0 : }
153 :
154 492 : BgpNeighborConfig::OriginOverrideConfig::OriginOverrideConfig()
155 492 : : origin_override(false), origin("IGP") {
156 492 : }
157 :
158 780 : bool BgpNeighborConfig::OriginOverrideConfig::operator<(
159 : const OriginOverrideConfig &rhs) const {
160 780 : if (origin_override == rhs.origin_override) {
161 780 : if (origin_override) {
162 : // chek origin value only if override is set
163 0 : return origin < rhs.origin;
164 : }
165 780 : return false;
166 : }
167 :
168 0 : return origin_override < rhs.origin_override;
169 : }
170 :
171 0 : bool BgpFamilyAttributesConfig::operator==(
172 : const BgpFamilyAttributesConfig &rhs) const {
173 0 : if (family != rhs.family)
174 0 : return false;
175 0 : if (loop_count != rhs.loop_count)
176 0 : return false;
177 0 : if (prefix_limit != rhs.prefix_limit)
178 0 : return false;
179 0 : if (idle_timeout != rhs.idle_timeout)
180 0 : return false;
181 0 : if (default_tunnel_encap_list != rhs.default_tunnel_encap_list)
182 0 : return false;
183 0 : return true;
184 : }
185 :
186 492 : BgpNeighborConfig::BgpNeighborConfig()
187 492 : : type_(UNSPECIFIED),
188 492 : admin_down_(false),
189 492 : passive_(false),
190 492 : as_override_(false),
191 492 : cluster_id_(0),
192 492 : peer_as_(0),
193 492 : identifier_(0),
194 492 : port_(BgpConfigManager::kDefaultPort),
195 492 : source_port_(0),
196 492 : hold_time_(0),
197 492 : loop_count_(0),
198 492 : local_as_(0),
199 492 : local_identifier_(0),
200 492 : last_change_at_(UTCTimestampUsec()) {
201 492 : }
202 :
203 0 : void BgpNeighborConfig::CopyValues(const BgpNeighborConfig &rhs) {
204 0 : instance_name_ = rhs.instance_name_;
205 0 : group_name_ = rhs.group_name_;
206 0 : type_ = rhs.type_;
207 0 : admin_down_ = rhs.admin_down_;
208 0 : passive_ = rhs.passive_;
209 0 : as_override_ = rhs.as_override_;
210 0 : private_as_action_ = rhs.private_as_action_;
211 0 : cluster_id_ = rhs.cluster_id_;
212 0 : peer_as_ = rhs.peer_as_;
213 0 : identifier_ = rhs.identifier_;
214 0 : address_ = rhs.address_;
215 0 : inet_gateway_address_ = rhs.inet_gateway_address_;
216 0 : inet6_gateway_address_ = rhs.inet6_gateway_address_;
217 0 : port_ = rhs.port_;
218 0 : source_port_ = rhs.source_port_;
219 0 : hold_time_ = rhs.hold_time_;
220 0 : loop_count_ = rhs.loop_count_;
221 0 : local_as_ = rhs.local_as_;
222 0 : local_identifier_ = rhs.local_identifier_;
223 0 : auth_data_ = rhs.auth_data_;
224 0 : family_attributes_list_ = rhs.family_attributes_list_;
225 0 : origin_override_ = rhs.origin_override_;
226 0 : }
227 :
228 390 : int BgpNeighborConfig::CompareTo(const BgpNeighborConfig &rhs) const {
229 390 : KEY_COMPARE(name_, rhs.name_);
230 390 : KEY_COMPARE(uuid_, rhs.uuid_);
231 390 : KEY_COMPARE(instance_name_, rhs.instance_name_);
232 390 : KEY_COMPARE(type_, rhs.type_);
233 390 : KEY_COMPARE(admin_down_, rhs.admin_down_);
234 390 : KEY_COMPARE(passive_, rhs.passive_);
235 390 : KEY_COMPARE(as_override_, rhs.as_override_);
236 390 : KEY_COMPARE(origin_override_, rhs.origin_override_);
237 390 : KEY_COMPARE(private_as_action_, rhs.private_as_action_);
238 390 : KEY_COMPARE(cluster_id_, rhs.cluster_id_);
239 390 : KEY_COMPARE(peer_as_, rhs.peer_as_);
240 342 : KEY_COMPARE(identifier_, rhs.identifier_);
241 342 : KEY_COMPARE(address_, rhs.address_);
242 342 : KEY_COMPARE(inet_gateway_address_, rhs.inet_gateway_address_);
243 342 : KEY_COMPARE(inet6_gateway_address_, rhs.inet6_gateway_address_);
244 342 : KEY_COMPARE(port_, rhs.port_);
245 342 : KEY_COMPARE(source_port_, rhs.source_port_);
246 342 : KEY_COMPARE(hold_time_, rhs.hold_time_);
247 342 : KEY_COMPARE(loop_count_, rhs.loop_count_);
248 342 : KEY_COMPARE(local_as_, rhs.local_as_);
249 326 : KEY_COMPARE(local_identifier_, rhs.local_identifier_);
250 326 : KEY_COMPARE(auth_data_, rhs.auth_data_);
251 326 : return STLSortedCompare(
252 : family_attributes_list_.begin(), family_attributes_list_.end(),
253 : rhs.family_attributes_list_.begin(), rhs.family_attributes_list_.end(),
254 326 : BgpFamilyAttributesConfigCompare());
255 : }
256 :
257 492 : void BgpNeighborConfig::SetOriginOverride(bool origin_override,
258 : std::string origin) {
259 492 : origin_override_.origin_override = origin_override;
260 492 : origin_override_.origin = origin;
261 492 : }
262 :
263 132 : const IpAddress &BgpNeighborConfig::gateway_address(
264 : Address::Family family) const {
265 132 : assert(family == Address::INET || family == Address::INET6);
266 132 : if (family == Address::INET) {
267 66 : return inet_gateway_address_;
268 : } else {
269 66 : return inet6_gateway_address_;
270 : }
271 : }
272 :
273 328 : void BgpNeighborConfig::set_gateway_address(Address::Family family,
274 : const IpAddress &address) {
275 328 : assert(family == Address::INET || family == Address::INET6);
276 328 : if (family == Address::INET) {
277 164 : inet_gateway_address_ = address;
278 : } else {
279 164 : inet6_gateway_address_ = address;
280 : }
281 328 : }
282 :
283 : BgpNeighborConfig::AddressFamilyList
284 566 : BgpNeighborConfig::GetAddressFamilies() const {
285 566 : BgpNeighborConfig::AddressFamilyList family_list;
286 1562 : for (const auto& family_config : family_attributes_list_) {
287 996 : family_list.push_back(family_config.family);
288 : }
289 566 : sort(family_list.begin(), family_list.end());
290 566 : return family_list;
291 0 : }
292 :
293 332 : string BgpNeighborConfig::AuthKeyTypeToString() const {
294 332 : return auth_data_.KeyTypeToString();
295 : }
296 :
297 : // Return the key's id concatenated with its type.
298 332 : vector<string> BgpNeighborConfig::AuthKeysToString() const {
299 332 : return auth_data_.KeysToString();
300 : }
301 :
302 :
303 0 : bool StaticRouteConfig::operator<(const StaticRouteConfig &rhs) const {
304 0 : BOOL_KEY_COMPARE(address, rhs.address);
305 0 : BOOL_KEY_COMPARE(prefix_length, rhs.prefix_length);
306 0 : BOOL_KEY_COMPARE(nexthop, rhs.nexthop);
307 0 : return false;
308 : }
309 :
310 68 : BgpProtocolConfig::BgpProtocolConfig(const string &instance_name)
311 68 : : instance_name_(instance_name),
312 68 : admin_down_(false),
313 68 : cluster_id_(0),
314 68 : autonomous_system_(0),
315 68 : local_autonomous_system_(0),
316 68 : identifier_(0),
317 68 : port_(0),
318 68 : hold_time_(0),
319 68 : last_change_at_(UTCTimestampUsec()) {
320 68 : }
321 :
322 0 : int BgpProtocolConfig::CompareTo(const BgpProtocolConfig &rhs) const {
323 0 : KEY_COMPARE(instance_name_, rhs.instance_name_);
324 0 : KEY_COMPARE(admin_down_, rhs.admin_down_);
325 0 : KEY_COMPARE(autonomous_system_, rhs.autonomous_system_);
326 0 : KEY_COMPARE(identifier_, rhs.identifier_);
327 0 : KEY_COMPARE(port_, rhs.port_);
328 0 : KEY_COMPARE(hold_time_, rhs.hold_time_);
329 0 : return 0;
330 : }
331 :
332 102 : BgpInstanceConfig::BgpInstanceConfig(const string &name)
333 102 : : name_(name),
334 102 : has_pnf_(false),
335 102 : virtual_network_index_(0),
336 102 : virtual_network_allow_transit_(false),
337 102 : virtual_network_pbb_evpn_enable_(false),
338 102 : index_(-1),
339 102 : vxlan_id_(0),
340 102 : last_change_at_(UTCTimestampUsec()) {
341 102 : }
342 :
343 102 : BgpInstanceConfig::~BgpInstanceConfig() {
344 102 : }
345 :
346 102 : void BgpInstanceConfig::Clear() {
347 102 : import_list_.clear();
348 102 : export_list_.clear();
349 102 : has_pnf_ = false;
350 102 : virtual_network_.clear();
351 102 : virtual_network_index_ = 0;
352 102 : virtual_network_allow_transit_ = false;
353 102 : virtual_network_pbb_evpn_enable_ = false;
354 102 : vxlan_id_ = 0;
355 102 : inet_static_routes_.clear();
356 102 : inet6_static_routes_.clear();
357 102 : service_chain_list_.clear();
358 102 : }
359 :
360 68 : const BgpInstanceConfig::StaticRouteList &BgpInstanceConfig::static_routes(
361 : Address::Family family) const {
362 68 : assert(family == Address::INET || family == Address::INET6);
363 68 : if (family == Address::INET) {
364 34 : return inet_static_routes_;
365 : } else {
366 34 : return inet6_static_routes_;
367 : }
368 : }
369 :
370 0 : void BgpInstanceConfig::swap_static_routes(Address::Family family,
371 : StaticRouteList *list) {
372 0 : assert(family == Address::INET || family == Address::INET6);
373 0 : if (family == Address::INET) {
374 0 : swap(inet_static_routes_, *list);
375 : } else {
376 0 : swap(inet6_static_routes_, *list);
377 : }
378 0 : }
379 :
380 136 : const ServiceChainConfig *BgpInstanceConfig::service_chain_info(
381 : SCAddress::Family family) const {
382 136 : for (ServiceChainList::const_iterator it = service_chain_list_.begin();
383 136 : it != service_chain_list_.end(); ++it) {
384 0 : if (it->family == family)
385 0 : return it.operator->();
386 : }
387 136 : return NULL;
388 : }
389 :
390 : const BgpInstanceConfig::AggregateRouteList &
391 68 : BgpInstanceConfig::aggregate_routes(Address::Family family) const {
392 68 : assert(family == Address::INET || family == Address::INET6);
393 68 : if (family == Address::INET) {
394 34 : return inet_aggregate_routes_;
395 : } else {
396 34 : return inet6_aggregate_routes_;
397 : }
398 : }
399 :
400 204 : void BgpInstanceConfig::swap_aggregate_routes(Address::Family family,
401 : AggregateRouteList *list) {
402 204 : assert(family == Address::INET || family == Address::INET6);
403 204 : if (family == Address::INET) {
404 102 : swap(inet_aggregate_routes_, *list);
405 : } else {
406 102 : swap(inet6_aggregate_routes_, *list);
407 : }
408 204 : }
409 :
410 0 : BgpRoutingPolicyConfig::BgpRoutingPolicyConfig(const string &name)
411 0 : : name_(name),
412 0 : last_change_at_(UTCTimestampUsec()) {
413 0 : }
414 :
415 0 : BgpRoutingPolicyConfig::~BgpRoutingPolicyConfig() {
416 0 : }
417 :
418 0 : void BgpRoutingPolicyConfig::Clear() {
419 0 : terms_.clear();
420 0 : }
421 :
422 0 : string RoutingPolicyMatchConfig::ToString() const {
423 0 : ostringstream oss;
424 0 : oss << "from {" << endl;
425 0 : if (!protocols_match.empty()) {
426 0 : oss << " protocol [ ";
427 0 : for (const auto &protocol : protocols_match) {
428 0 : oss << protocol << ",";
429 : }
430 0 : oss.seekp(-1, oss.cur);
431 0 : oss << " ]";
432 : }
433 0 : if (!community_match.empty()) {
434 0 : if (community_match_all) {
435 0 : oss << " community (all) [ ";
436 : } else {
437 0 : oss << " community (any) [ ";
438 : }
439 0 : for (const auto &community : community_match) {
440 0 : oss << community << ",";
441 : }
442 0 : oss.seekp(-1, oss.cur);
443 0 : oss << " ]";
444 : }
445 0 : if (!prefixes_to_match.empty()) {
446 0 : for (const auto &match : prefixes_to_match) {
447 0 : oss << " prefix " << match.prefix_to_match << " "
448 0 : << match.prefix_match_type << endl;
449 : }
450 : }
451 0 : oss << "}" << endl;
452 0 : return oss.str();
453 0 : }
454 :
455 0 : static void PutAsnList(ostringstream *oss, const AsnList &list) {
456 0 : copy(list.begin(), list.end(), ostream_iterator<uint16_t>(*oss, ","));
457 0 : oss->seekp(-1, oss->cur);
458 0 : }
459 :
460 0 : static void PutCommunityList(ostringstream *oss, const CommunityList &list) {
461 0 : copy(list.begin(), list.end(), ostream_iterator<string>(*oss, ","));
462 0 : oss->seekp(-1, oss->cur);
463 0 : }
464 :
465 0 : string RoutingPolicyActionConfig::ToString() const {
466 0 : ostringstream oss;
467 0 : oss << "then {" << endl;
468 0 : if (!update.aspath_expand.empty()) {
469 0 : oss << " as-path expand [ ";
470 0 : PutAsnList(&oss, update.aspath_expand);
471 0 : oss << " ]" << endl;
472 : }
473 0 : if (!update.community_set.empty()) {
474 0 : oss << " community set [ ";
475 0 : PutCommunityList(&oss, update.community_set);
476 0 : oss << " ]" << endl;
477 : }
478 0 : if (!update.community_add.empty()) {
479 0 : oss << " community add [ ";
480 0 : PutCommunityList(&oss, update.community_add);
481 0 : oss << " ]" << endl;
482 : }
483 0 : if (!update.community_remove.empty()) {
484 0 : oss << " community remove [ ";
485 0 : PutCommunityList(&oss, update.community_remove);
486 0 : oss << " ]" << endl;
487 : }
488 0 : if (update.local_pref) {
489 0 : oss << " local-preference " << update.local_pref << endl;
490 : }
491 0 : if (update.med) {
492 0 : oss << " med " << update.med << endl;
493 : }
494 :
495 0 : if (action == ACCEPT) {
496 0 : oss << " accept" << endl;
497 0 : } else if (action == REJECT) {
498 0 : oss << " reject" << endl;
499 0 : } else if (action == NEXT_TERM) {
500 0 : oss << " next-term" << endl;
501 : }
502 0 : oss << "}" << endl;
503 0 : return oss.str();
504 0 : }
505 :
506 68 : BgpConfigManager::BgpConfigManager(BgpServer *server)
507 68 : : server_(server) {
508 68 : }
509 :
510 68 : BgpConfigManager::~BgpConfigManager() {
511 68 : }
512 :
513 : template<>
514 271 : void BgpConfigManager::Notify<BgpInstanceConfig>(
515 : const BgpInstanceConfig *config, EventType event) {
516 271 : config->set_last_change_at(UTCTimestampUsec());
517 627 : for (auto obs : obs_) {
518 356 : if (obs.instance) {
519 271 : (obs.instance)(config, event);
520 : }
521 356 : }
522 271 : }
523 :
524 : template<>
525 0 : void BgpConfigManager::Notify<BgpRoutingPolicyConfig>(
526 : const BgpRoutingPolicyConfig *config, EventType event) {
527 0 : config->set_last_change_at(UTCTimestampUsec());
528 0 : for (auto obs : obs_) {
529 0 : if (obs.policy) {
530 0 : (obs.policy)(config, event);
531 : }
532 0 : }
533 0 : }
534 :
535 : template<>
536 132 : void BgpConfigManager::Notify<BgpProtocolConfig>(
537 : const BgpProtocolConfig *config, EventType event) {
538 132 : config->set_last_change_at(UTCTimestampUsec());
539 297 : for (auto obs : obs_) {
540 165 : if (obs.protocol) {
541 165 : (obs.protocol)(config, event);
542 : }
543 165 : }
544 132 : }
545 :
546 : template<>
547 166 : void BgpConfigManager::Notify<BgpNeighborConfig>(
548 : const BgpNeighborConfig *config, EventType event) {
549 166 : config->set_last_change_at(UTCTimestampUsec());
550 415 : for (auto obs : obs_) {
551 249 : if (obs.neighbor) {
552 166 : (obs.neighbor)(config, event);
553 : }
554 249 : }
555 166 : }
556 :
557 : template<>
558 68 : void BgpConfigManager::Notify<BgpGlobalSystemConfig>(
559 : const BgpGlobalSystemConfig *config, EventType event) {
560 68 : config->set_last_change_at(UTCTimestampUsec());
561 153 : for (auto obs : obs_) {
562 85 : if (obs.system) {
563 85 : (obs.system)(config, event);
564 : }
565 85 : }
566 68 : }
567 :
568 : template<>
569 0 : void BgpConfigManager::Notify<BgpGlobalQosConfig>(
570 : const BgpGlobalQosConfig *config, EventType event) {
571 0 : config->set_last_change_at(UTCTimestampUsec());
572 0 : for (auto obs : obs_) {
573 0 : if (obs.qos) {
574 0 : (obs.qos)(config, event);
575 : }
576 0 : }
577 0 : }
|