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