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 0 : static int CompareTo(const AuthenticationKey &lhs,
33 : const AuthenticationKey &rhs) {
34 0 : KEY_COMPARE(lhs.id, rhs.id);
35 0 : KEY_COMPARE(lhs.value, rhs.value);
36 0 : KEY_COMPARE(lhs.start_time, rhs.start_time);
37 0 : return 0;
38 : }
39 :
40 0 : bool AuthenticationKey::operator<(const AuthenticationKey &rhs) const {
41 0 : int cmp = CompareTo(*this, rhs);
42 0 : return cmp < 0;
43 : }
44 :
45 0 : bool AuthenticationKey::operator==(const AuthenticationKey &rhs) const {
46 0 : int cmp = CompareTo(*this, rhs);
47 0 : return cmp == 0;
48 : }
49 :
50 224 : AuthenticationData::AuthenticationData() : key_type_(NIL) {
51 224 : }
52 :
53 0 : AuthenticationData::AuthenticationData(KeyType type,
54 0 : const AuthenticationKeyChain &chain) :
55 0 : key_type_(type), key_chain_(chain) {
56 0 : }
57 :
58 64 : bool AuthenticationData::operator<(const AuthenticationData &rhs) const {
59 64 : BOOL_KEY_COMPARE(key_type_, rhs.key_type());
60 64 : BOOL_KEY_COMPARE(key_chain_, rhs.key_chain());
61 64 : return false;
62 : }
63 :
64 64 : bool AuthenticationData::operator==(const AuthenticationData &rhs) const {
65 128 : if ((key_chain_.size() != rhs.key_chain().size()) ||
66 64 : (key_type_ != rhs.key_type())) {
67 0 : return false;
68 : }
69 :
70 64 : for (size_t ocnt = 0; ocnt < rhs.key_chain().size(); ++ocnt) {
71 0 : bool found = false;
72 0 : for (size_t icnt = 0; icnt < key_chain_.size(); ++icnt) {
73 0 : if (key_chain_[icnt] == rhs.key_chain()[ocnt]) {
74 0 : found = true;
75 0 : break;
76 : }
77 : }
78 0 : if (!found) {
79 0 : return false;
80 : }
81 : }
82 64 : return true;
83 : }
84 :
85 128 : AuthenticationData &AuthenticationData::operator=(
86 : const AuthenticationData &rhs) {
87 128 : set_key_type(rhs.key_type());
88 128 : set_key_chain(rhs.key_chain());
89 128 : return *this;
90 : }
91 :
92 0 : void AuthenticationData::AddKeyToKeyChain(const AuthenticationKey &key) {
93 0 : key_chain_.push_back(key);
94 0 : }
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 64 : bool AuthenticationData::Empty() const {
108 64 : return key_chain_.empty();
109 : }
110 :
111 0 : void AuthenticationData::Clear() {
112 0 : key_chain_.clear();
113 0 : }
114 :
115 64 : string AuthenticationData::KeyTypeToString() const {
116 64 : switch (key_type_) {
117 0 : case MD5:
118 0 : return "MD5";
119 64 : default:
120 64 : return "NIL";
121 : }
122 : }
123 :
124 0 : string AuthenticationData::KeyTypeToString(KeyType key_type) {
125 0 : switch (key_type) {
126 0 : case MD5:
127 0 : return "MD5";
128 0 : default:
129 0 : return "NIL";
130 : }
131 : }
132 :
133 64 : vector<string> AuthenticationData::KeysToString() const {
134 64 : AuthenticationKeyChain::const_iterator iter;
135 64 : vector<string> auth_keys;
136 64 : for (iter = key_chain_.begin(); iter != key_chain_.end(); ++iter) {
137 0 : AuthenticationKey key = *iter;
138 0 : auth_keys.push_back(integerToString(key.id));
139 0 : }
140 128 : 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 64 : BgpNeighborConfig::OriginOverrideConfig::OriginOverrideConfig()
155 64 : : origin_override(false), origin("IGP") {
156 64 : }
157 :
158 64 : bool BgpNeighborConfig::OriginOverrideConfig::operator<(
159 : const OriginOverrideConfig &rhs) const {
160 64 : if (origin_override == rhs.origin_override) {
161 64 : if (origin_override) {
162 : // chek origin value only if override is set
163 0 : return origin < rhs.origin;
164 : }
165 64 : 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 64 : BgpNeighborConfig::BgpNeighborConfig()
187 64 : : type_(UNSPECIFIED),
188 64 : admin_down_(false),
189 64 : passive_(false),
190 64 : as_override_(false),
191 64 : cluster_id_(0),
192 64 : peer_as_(0),
193 64 : identifier_(0),
194 64 : port_(BgpConfigManager::kDefaultPort),
195 64 : source_port_(0),
196 64 : hold_time_(0),
197 64 : loop_count_(0),
198 64 : local_as_(0),
199 64 : local_identifier_(0),
200 64 : last_change_at_(UTCTimestampUsec()) {
201 64 : }
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 32 : int BgpNeighborConfig::CompareTo(const BgpNeighborConfig &rhs) const {
229 32 : KEY_COMPARE(name_, rhs.name_);
230 32 : KEY_COMPARE(uuid_, rhs.uuid_);
231 32 : KEY_COMPARE(instance_name_, rhs.instance_name_);
232 32 : KEY_COMPARE(type_, rhs.type_);
233 32 : KEY_COMPARE(admin_down_, rhs.admin_down_);
234 32 : KEY_COMPARE(passive_, rhs.passive_);
235 32 : KEY_COMPARE(as_override_, rhs.as_override_);
236 32 : KEY_COMPARE(origin_override_, rhs.origin_override_);
237 32 : KEY_COMPARE(private_as_action_, rhs.private_as_action_);
238 32 : KEY_COMPARE(cluster_id_, rhs.cluster_id_);
239 32 : KEY_COMPARE(peer_as_, rhs.peer_as_);
240 32 : KEY_COMPARE(identifier_, rhs.identifier_);
241 32 : KEY_COMPARE(address_, rhs.address_);
242 32 : KEY_COMPARE(inet_gateway_address_, rhs.inet_gateway_address_);
243 32 : KEY_COMPARE(inet6_gateway_address_, rhs.inet6_gateway_address_);
244 32 : KEY_COMPARE(port_, rhs.port_);
245 32 : KEY_COMPARE(source_port_, rhs.source_port_);
246 32 : KEY_COMPARE(hold_time_, rhs.hold_time_);
247 32 : KEY_COMPARE(loop_count_, rhs.loop_count_);
248 32 : KEY_COMPARE(local_as_, rhs.local_as_);
249 32 : KEY_COMPARE(local_identifier_, rhs.local_identifier_);
250 32 : KEY_COMPARE(auth_data_, rhs.auth_data_);
251 32 : return STLSortedCompare(
252 : family_attributes_list_.begin(), family_attributes_list_.end(),
253 : rhs.family_attributes_list_.begin(), rhs.family_attributes_list_.end(),
254 32 : BgpFamilyAttributesConfigCompare());
255 : }
256 :
257 64 : void BgpNeighborConfig::SetOriginOverride(bool origin_override,
258 : std::string origin) {
259 64 : origin_override_.origin_override = origin_override;
260 64 : origin_override_.origin = origin;
261 64 : }
262 :
263 0 : const IpAddress &BgpNeighborConfig::gateway_address(
264 : Address::Family family) const {
265 0 : assert(family == Address::INET || family == Address::INET6);
266 0 : if (family == Address::INET) {
267 0 : return inet_gateway_address_;
268 : } else {
269 0 : return inet6_gateway_address_;
270 : }
271 : }
272 :
273 0 : void BgpNeighborConfig::set_gateway_address(Address::Family family,
274 : const IpAddress &address) {
275 0 : assert(family == Address::INET || family == Address::INET6);
276 0 : if (family == Address::INET) {
277 0 : inet_gateway_address_ = address;
278 : } else {
279 0 : inet6_gateway_address_ = address;
280 : }
281 0 : }
282 :
283 : BgpNeighborConfig::AddressFamilyList
284 128 : BgpNeighborConfig::GetAddressFamilies() const {
285 128 : BgpNeighborConfig::AddressFamilyList family_list;
286 768 : for (const auto& family_config : family_attributes_list_) {
287 640 : family_list.push_back(family_config.family);
288 : }
289 128 : sort(family_list.begin(), family_list.end());
290 128 : return family_list;
291 0 : }
292 :
293 64 : string BgpNeighborConfig::AuthKeyTypeToString() const {
294 64 : return auth_data_.KeyTypeToString();
295 : }
296 :
297 : // Return the key's id concatenated with its type.
298 64 : vector<string> BgpNeighborConfig::AuthKeysToString() const {
299 64 : 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 20 : BgpProtocolConfig::BgpProtocolConfig(const string &instance_name)
311 20 : : instance_name_(instance_name),
312 20 : admin_down_(false),
313 20 : cluster_id_(0),
314 20 : autonomous_system_(0),
315 20 : local_autonomous_system_(0),
316 20 : identifier_(0),
317 20 : port_(0),
318 20 : hold_time_(0),
319 20 : last_change_at_(UTCTimestampUsec()) {
320 20 : }
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 104 : BgpInstanceConfig::BgpInstanceConfig(const string &name)
333 104 : : name_(name),
334 104 : has_pnf_(false),
335 104 : virtual_network_index_(0),
336 104 : virtual_network_allow_transit_(false),
337 104 : virtual_network_pbb_evpn_enable_(false),
338 104 : index_(-1),
339 104 : vxlan_id_(0),
340 104 : last_change_at_(UTCTimestampUsec()) {
341 104 : }
342 :
343 104 : BgpInstanceConfig::~BgpInstanceConfig() {
344 104 : }
345 :
346 181 : void BgpInstanceConfig::Clear() {
347 181 : import_list_.clear();
348 181 : export_list_.clear();
349 181 : has_pnf_ = false;
350 181 : virtual_network_.clear();
351 181 : virtual_network_index_ = 0;
352 181 : virtual_network_allow_transit_ = false;
353 181 : virtual_network_pbb_evpn_enable_ = false;
354 181 : vxlan_id_ = 0;
355 181 : inet_static_routes_.clear();
356 181 : inet6_static_routes_.clear();
357 181 : service_chain_list_.clear();
358 181 : }
359 :
360 162 : const BgpInstanceConfig::StaticRouteList &BgpInstanceConfig::static_routes(
361 : Address::Family family) const {
362 162 : assert(family == Address::INET || family == Address::INET6);
363 162 : if (family == Address::INET) {
364 81 : return inet_static_routes_;
365 : } else {
366 81 : 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 27706 : const ServiceChainConfig *BgpInstanceConfig::service_chain_info(
381 : SCAddress::Family family) const {
382 27706 : for (ServiceChainList::const_iterator it = service_chain_list_.begin();
383 27706 : it != service_chain_list_.end(); ++it) {
384 0 : if (it->family == family)
385 0 : return it.operator->();
386 : }
387 27706 : return NULL;
388 : }
389 :
390 : const BgpInstanceConfig::AggregateRouteList &
391 162 : BgpInstanceConfig::aggregate_routes(Address::Family family) const {
392 162 : assert(family == Address::INET || family == Address::INET6);
393 162 : if (family == Address::INET) {
394 81 : return inet_aggregate_routes_;
395 : } else {
396 81 : return inet6_aggregate_routes_;
397 : }
398 : }
399 :
400 362 : void BgpInstanceConfig::swap_aggregate_routes(Address::Family family,
401 : AggregateRouteList *list) {
402 362 : assert(family == Address::INET || family == Address::INET6);
403 362 : if (family == Address::INET) {
404 181 : swap(inet_aggregate_routes_, *list);
405 : } else {
406 181 : swap(inet6_aggregate_routes_, *list);
407 : }
408 362 : }
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 20 : BgpConfigManager::BgpConfigManager(BgpServer *server)
507 20 : : server_(server) {
508 20 : }
509 :
510 20 : BgpConfigManager::~BgpConfigManager() {
511 20 : }
512 :
513 : template<>
514 300 : void BgpConfigManager::Notify<BgpInstanceConfig>(
515 : const BgpInstanceConfig *config, EventType event) {
516 300 : config->set_last_change_at(UTCTimestampUsec());
517 880 : for (auto obs : obs_) {
518 580 : if (obs.instance) {
519 300 : (obs.instance)(config, event);
520 : }
521 580 : }
522 300 : }
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 20 : void BgpConfigManager::Notify<BgpProtocolConfig>(
537 : const BgpProtocolConfig *config, EventType event) {
538 20 : config->set_last_change_at(UTCTimestampUsec());
539 60 : for (auto obs : obs_) {
540 40 : if (obs.protocol) {
541 40 : (obs.protocol)(config, event);
542 : }
543 40 : }
544 20 : }
545 :
546 : template<>
547 32 : void BgpConfigManager::Notify<BgpNeighborConfig>(
548 : const BgpNeighborConfig *config, EventType event) {
549 32 : config->set_last_change_at(UTCTimestampUsec());
550 96 : for (auto obs : obs_) {
551 64 : if (obs.neighbor) {
552 32 : (obs.neighbor)(config, event);
553 : }
554 64 : }
555 32 : }
556 :
557 : template<>
558 30 : void BgpConfigManager::Notify<BgpGlobalSystemConfig>(
559 : const BgpGlobalSystemConfig *config, EventType event) {
560 30 : config->set_last_change_at(UTCTimestampUsec());
561 90 : for (auto obs : obs_) {
562 60 : if (obs.system) {
563 60 : (obs.system)(config, event);
564 : }
565 60 : }
566 30 : }
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 : }
|