OpenSDN source code
routing_policy_match.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
8 #include <boost/foreach.hpp>
9 #include <boost/assign/list_of.hpp>
10 
11 #include <algorithm>
12 #include <map>
13 #include <sstream>
14 #include <vector>
15 
16 #include "bgp/ipeer.h"
17 #include "bgp/bgp_attr.h"
18 #include "bgp/bgp_path.h"
36 #include "net/community_type.h"
37 
38 
39 using contrail::regex;
42 using std::includes;
43 using std::map;
44 using std::ostringstream;
45 using std::sort;
46 using std::string;
47 using std::unique;
48 using std::vector;
49 using std::find;
50 
51 MatchCommunity::MatchCommunity(const vector<string> &communities,
52  bool match_all) : match_all_(match_all) {
53  // Assume that the each community string that doesn't correspond to a
54  // community name or value is a regex string.
55  BOOST_FOREACH(const string &community, communities) {
56  uint32_t value = CommunityType::CommunityFromString(community);
57  if (value) {
58  to_match_.insert(value);
59  } else {
60  to_match_regex_strings_.push_back(community);
61  }
62  }
63 
64  // Sort and uniquify the vector of regex strings.
66  vector<string>::iterator it =
67  unique(to_match_regex_strings_.begin(), to_match_regex_strings_.end());
69 
70  // Build a vector of regexs corresponding to the regex strings.
71  BOOST_FOREACH(string regex_str, regex_strings()) {
72  to_match_regexs_.push_back(regex(regex_str));
73  }
74 }
75 
77 }
78 
79 //
80 // Return true if all community strings (normal or regex) are matched by the
81 // community values in the BgpAttr.
82 //
83 bool MatchCommunity::MatchAll(const BgpAttr *attr) const {
84  // Bail if there's no community values in the BgpAttr.
85  const Community *comm = attr->community();
86  if (!comm)
87  return false;
88 
89  // Make sure that all non-regex communities in this MatchCommunity are
90  // present in the BgpAttr.
91  vector<uint32_t> list = comm->communities();
92  sort(list.begin(), list.end());
93  if (!includes(list.begin(), list.end(),
94  communities().begin(), communities().end())) {
95  return false;
96  }
97 
98  // Make sure that each regex in this MatchCommunity is matched by one
99  // of the communities in the BgpAttr.
100  BOOST_FOREACH(const regex &match_expr, regexs()) {
101  bool matched = false;
102  BOOST_FOREACH(uint32_t community, comm->communities()) {
103  string community_str = CommunityType::CommunityToString(community);
104  if (regex_match(community_str, match_expr)) {
105  matched = true;
106  break;
107  }
108  }
109  if (!matched)
110  return false;
111  }
112 
113  return true;
114 }
115 
116 //
117 // Return true if any community strings (normal or regex) is matched by the
118 // community values in the BgpAttr.
119 //
120 bool MatchCommunity::MatchAny(const BgpAttr *attr) const {
121  // Bail if there's no community values in the BgpAttr.
122  const Community *comm = attr->community();
123  if (!comm)
124  return false;
125 
126  // Check if any of the community values in the BgpAttr matches one of
127  // the normal community strings.
128  BOOST_FOREACH(uint32_t community, comm->communities()) {
129  if (communities().find(community) != communities().end())
130  return true;
131  }
132 
133  // Check if any of the community values in the BgpAttr matches one of
134  // the community regexs.
135  BOOST_FOREACH(uint32_t community, comm->communities()) {
136  string community_str = CommunityType::CommunityToString(community);
137  BOOST_FOREACH(const regex &match_expr, regexs()) {
138  if (regex_match(community_str, match_expr))
139  return true;
140  }
141  }
142 
143  return false;
144 }
145 
146 //
147 // Return true if the BgpPath matches this MatchCommunity.
148 //
149 bool MatchCommunity::Match(const BgpRoute *route, const BgpPath *path,
150  const BgpAttr *attr) const {
151  return (match_all_ ? MatchAll(attr) : MatchAny(attr));
152 }
153 
154 //
155 // Return string representation of this MatchCommunity.
156 //
157 string MatchCommunity::ToString() const {
158  ostringstream oss;
159  if (match_all_) {
160  oss << "community (all) [ ";
161  } else {
162  oss << "community (any) [ ";
163  }
164  BOOST_FOREACH(uint32_t community, communities()) {
165  string name = CommunityType::CommunityToString(community);
166  oss << name << ",";
167  }
168  BOOST_FOREACH(string regex_str, regex_strings()) {
169  oss << regex_str << ",";
170  }
171  oss.seekp(-1, oss.cur);
172  oss << " ]";
173  return oss.str();
174 }
175 
176 //
177 // Return true if this MatchCommunity is equal to the one supplied.
178 //
179 bool MatchCommunity::IsEqual(const RoutingPolicyMatch &community) const {
180  const MatchCommunity in_community =
181  static_cast<const MatchCommunity &>(community);
182  if (match_all() != in_community.match_all())
183  return false;
184  if (communities() != in_community.communities())
185  return false;
186  if (regex_strings() != in_community.regex_strings())
187  return false;
188  return true;
189 }
190 
191 MatchExtCommunity::MatchExtCommunity(const vector<string> &communities,
192  bool match_all) : match_all_(match_all) {
193  // Assume that the each community string that doesn't correspond to a
194  // community name or value is a regex string.
195  BOOST_FOREACH(const string &community, communities) {
196  const ExtCommunity::ExtCommunityList list =
198  if (list.size()) {
199  if(!Find(list[0]))
200  to_match_.push_back(list[0]);
201  } else {
202  to_match_regex_strings_.push_back(community);
203  }
204  }
205 
206  // Sort and uniquify the vector of regex strings.
207  sort(to_match_.begin(), to_match_.end());
209  vector<string>::iterator it =
210  unique(to_match_regex_strings_.begin(), to_match_regex_strings_.end());
212 
213  // Build a vector of regexs corresponding to the regex strings.
214  BOOST_FOREACH(string regex_str, regex_strings()) {
215  to_match_regexs_.push_back(regex(regex_str));
216  }
217 }
218 
220 }
221 
222 //
223 // Return true if all community strings (normal or regex) are matched by the
224 // community values in the BgpAttr.
225 //
226 bool MatchExtCommunity::MatchAll(const BgpAttr *attr) const {
227  // Bail if there's no community values in the BgpAttr.
228  const ExtCommunity *comm = attr->ext_community();
229  if (!comm)
230  return false;
231 
232  // Make sure that all non-regex communities in this MatchExtCommunity are
233  // present in the BgpAttr.
235  sort(list.begin(), list.end());
236  if (!includes(list.begin(), list.end(),
237  communities().begin(), communities().end())) {
238  return false;
239  }
240 
241  // Make sure that each regex in this MatchExtCommunity is matched by one
242  // of the communities in the BgpAttr.
243  BOOST_FOREACH(const regex &match_expr, regexs()) {
244  bool matched = false;
245  BOOST_FOREACH(ExtCommunity::ExtCommunityValue community,
246  comm->communities()) {
247  string community_str = ExtCommunity::ToString(community);
248  if (regex_match(community_str, match_expr)) {
249  matched = true;
250  break;
251  }
252  community_str = ExtCommunity::ToHexString(community);
253  if (regex_match(community_str, match_expr)) {
254  matched = true;
255  break;
256  }
257  }
258  if (!matched)
259  return false;
260  }
261 
262  return true;
263 }
264 
266  const {
267  return (std::find(communities().begin(), communities().end(), community) !=
268  communities().end());
269 }
270 
271 //
272 // Return true if any community strings (normal or regex) is matched by the
273 // community values in the BgpAttr.
274 //
275 bool MatchExtCommunity::MatchAny(const BgpAttr *attr) const {
276  // Bail if there's no community values in the BgpAttr.
277  const ExtCommunity *comm = attr->ext_community();
278  if (!comm)
279  return false;
280 
281  // Check if any of the community values in the BgpAttr matches one of
282  // the normal community strings.
283  BOOST_FOREACH(ExtCommunity::ExtCommunityValue community,
284  comm->communities()) {
285  if (Find(community))
286  return true;
287  }
288 
289  // Check if any of the community values in the BgpAttr matches one of
290  // the community regexs.
291  BOOST_FOREACH(ExtCommunity::ExtCommunityValue community,
292  comm->communities()) {
293  string community_str = ExtCommunity::ToString(community);
294  BOOST_FOREACH(const regex &match_expr, regexs()) {
295  if (regex_match(community_str, match_expr))
296  return true;
297  }
298  community_str = ExtCommunity::ToHexString(community);
299  BOOST_FOREACH(const regex &match_expr, regexs()) {
300  if (regex_match(community_str, match_expr))
301  return true;
302  }
303  }
304 
305  return false;
306 }
307 
308 //
309 // Return true if the BgpPath matches this MatchExtCommunity.
310 //
311 bool MatchExtCommunity::Match(const BgpRoute *route, const BgpPath *path,
312  const BgpAttr *attr) const {
313  return (match_all_ ? MatchAll(attr) : MatchAny(attr));
314 }
315 
316 //
317 // Return string representation of this MatchExtCommunity.
318 //
320  ostringstream oss;
321  if (match_all_) {
322  oss << "Extcommunity (all) [ ";
323  } else {
324  oss << "Extcommunity (any) [ ";
325  }
326  BOOST_FOREACH(ExtCommunity::ExtCommunityValue community, communities()) {
327  string name = ExtCommunity::ToString(community);
328  oss << name << ",";
329  }
330  BOOST_FOREACH(string regex_str, regex_strings()) {
331  oss << regex_str << ",";
332  }
333  oss.seekp(-1, oss.cur);
334  oss << " ]";
335  return oss.str();
336 }
337 
338 //
339 // Return true if this MatchExtCommunity is equal to the one supplied.
340 //
341 bool MatchExtCommunity::IsEqual(const RoutingPolicyMatch &community) const {
342  const MatchExtCommunity in_community =
343  static_cast<const MatchExtCommunity &>(community);
344  if (match_all() != in_community.match_all())
345  return false;
346  if (communities() != in_community.communities())
347  return false;
348  if (regex_strings() != in_community.regex_strings())
349  return false;
350  return true;
351 }
352 
353 template <typename T>
355  const string &match_type_str) {
356  MatchType match_type = EXACT;
357  if (match_type_str == "exact") {
358  match_type = EXACT;
359  } else if (match_type_str == "longer") {
360  match_type = LONGER;
361  } else if (match_type_str == "orlonger") {
362  match_type = ORLONGER;
363  }
364  return match_type;
365 }
366 
367 template <typename T>
369  BOOST_FOREACH(const PrefixMatchConfig &match_config, match_config_list) {
370  boost::system::error_code ec;
371  PrefixT prefix = PrefixT::FromString(match_config.prefix_to_match, &ec);
372  MatchType match_type =
374  match_list_.push_back(PrefixMatch(prefix, match_type));
375  }
376 
377  // Sort and uniquify the vector of PrefixMatch elements.
378  sort(match_list_.begin(), match_list_.end());
379  typename PrefixMatchList::iterator it =
380  unique(match_list_.begin(), match_list_.end());
381  match_list_.erase(it, match_list_.end());
382 }
383 
384 template <typename T>
386 }
387 
388 template <typename T>
389 bool MatchPrefix<T>::Match(const BgpRoute *route, const BgpPath *path,
390  const BgpAttr *attr) const {
391  const RouteT *in_route = dynamic_cast<const RouteT *>(route);
392  if (in_route == NULL)
393  return false;
394  const PrefixT &prefix = in_route->GetPrefix();
395  BOOST_FOREACH(const PrefixMatch &prefix_match, match_list_) {
396  if (prefix_match.match_type == EXACT) {
397  if (prefix == prefix_match.prefix)
398  return true;
399  } else if (prefix_match.match_type == LONGER) {
400  if (prefix == prefix_match.prefix)
401  continue;
402  if (prefix.IsMoreSpecific(prefix_match.prefix))
403  return true;
404  } else if (prefix_match.match_type == ORLONGER) {
405  if (prefix.IsMoreSpecific(prefix_match.prefix))
406  return true;
407  }
408  }
409  return false;
410 }
411 
412 template <typename T>
413 bool MatchPrefix<T>::IsEqual(const RoutingPolicyMatch &prefix) const {
414  const MatchPrefix in_prefix = static_cast<const MatchPrefix&>(prefix);
415  return (in_prefix.match_list_ == match_list_);
416 }
417 
418 template <typename T>
419 string MatchPrefix<T>::ToString() const {
420  ostringstream oss;
421  oss << "prefix [";
422  BOOST_FOREACH(const PrefixMatch &prefix_match, match_list_) {
423  oss << " " << prefix_match.prefix.ToString();
424  if (prefix_match.match_type == LONGER) {
425  oss << " longer";
426  } else if (prefix_match.match_type == ORLONGER) {
427  oss << " orlonger";
428  }
429  oss << ",";
430  }
431  oss.seekp(-1, oss.cur);
432  oss << " ]";
433  return oss.str();
434 }
435 
436 template class MatchPrefix<PrefixMatchInet>;
437 template class MatchPrefix<PrefixMatchInet6>;
438 
439 static const map<string, MatchProtocol::MatchProtocolType> fromString
440  = boost::assign::map_list_of
441  ("bgp", MatchProtocol::BGP)
442  ("xmpp", MatchProtocol::XMPP)
443  ("static", MatchProtocol::StaticRoute)
444  ("service-chain", MatchProtocol::ServiceChainRoute)
445  ("aggregate", MatchProtocol::AggregateRoute)
446  ("interface", MatchProtocol::Interface)
447  ("interface-static", MatchProtocol::InterfaceStatic)
448  ("service-interface", MatchProtocol::ServiceInterface)
449  ("bgpaas", MatchProtocol::BGPaaS);
450 
451 static const map<MatchProtocol::MatchProtocolType, string> toString
452  = boost::assign::map_list_of
453  (MatchProtocol::BGP, "bgp")
454  (MatchProtocol::XMPP, "xmpp")
455  (MatchProtocol::StaticRoute, "static")
456  (MatchProtocol::ServiceChainRoute, "service-chain")
457  (MatchProtocol::AggregateRoute, "aggregate")
458  (MatchProtocol::Interface, "interface")
459  (MatchProtocol::InterfaceStatic, "interface-static")
460  (MatchProtocol::ServiceInterface, "service-interface")
461  (MatchProtocol::BGPaaS, "bgpaas");
462 
463 static const map<MatchProtocol::MatchProtocolType, BgpPath::PathSource>
464  pathSourceMap = boost::assign::map_list_of
474 
475 static const vector<MatchProtocol::MatchProtocolType>
476  isSubprotocol = boost::assign::list_of(MatchProtocol::Interface)
480 
482  return (find(isSubprotocol.begin(), isSubprotocol.end(), protocol) !=
483  isSubprotocol.end());
484 }
485 
487  map<MatchProtocol::MatchProtocolType, string>::const_iterator it =
488  toString.find(protocol);
489  if (it != toString.end()) {
490  return it->second;
491  }
492  return "unspecified";
493 }
494 
496  const string &protocol) {
497  map<string, MatchProtocol::MatchProtocolType>::const_iterator it =
498  fromString.find(protocol);
499  if (it != fromString.end()) {
500  return it->second;
501  }
503 }
504 
507  map<MatchProtocol::MatchProtocolType, BgpPath::PathSource>::const_iterator
508  it = pathSourceMap.find(src);
509  if (it != pathSourceMap.end()) {
510  return it->second;
511  }
512  return BgpPath::None;
513 }
514 
515 MatchProtocol::MatchProtocol(const vector<string> &protocols) {
516  BOOST_FOREACH(const string &protocol, protocols) {
517  MatchProtocolType value = MatchProtocolFromString(protocol);
518  // Ignore invalid protocol values.
519  if (value == Unspecified)
520  continue;
521  to_match_.push_back(value);
522  }
523 
524  // Sort and uniquify the vector match protocols.
525  sort(to_match_.begin(), to_match_.end());
526  vector<MatchProtocolType>::iterator it =
527  unique(to_match_.begin(), to_match_.end());
528  to_match_.erase(it, to_match_.end());
529 }
530 
532 }
533 
534 bool MatchProtocol::Match(const BgpRoute *route, const BgpPath *path,
535  const BgpAttr *attr) const {
536  BgpPath::PathSource path_src = path->GetSource();
537  bool is_xmpp = path->GetPeer() ? path->GetPeer()->IsXmppPeer() : false;
538  bool bgpaas = path->GetPeer() ? path->GetPeer()->IsRouterTypeBGPaaS() : false;
539  BOOST_FOREACH(MatchProtocolType protocol, protocols()) {
540  if (IsSubprotocol(protocol)) {
541  // Check only if matching protocol is subprotocol
542  if (attr && !attr->sub_protocol().empty()) {
543  std::string matchps = MatchProtocolToString(protocol);
544  if (matchps.compare(attr->sub_protocol()) == 0)
545  return true;
546  }
547  } else {
548  BgpPath::PathSource mapped_src =
549  PathSourceFromMatchProtocol(protocol);
550  if (mapped_src != BgpPath::None) {
551  if (mapped_src == path_src) {
552  if (protocol == XMPP && !is_xmpp)
553  continue;
554  if (protocol == BGP && (bgpaas || is_xmpp))
555  continue;
556  if (protocol == BGPaaS && (!bgpaas || is_xmpp))
557  continue;
558  return true;
559  }
560  }
561  }
562  }
563  return false;
564 }
565 
566 string MatchProtocol::ToString() const {
567  ostringstream oss;
568  oss << "protocol [ ";
569  BOOST_FOREACH(MatchProtocolType protocol, protocols()) {
570  string name = MatchProtocolToString(protocol);
571  oss << name << ",";
572  }
573  oss.seekp(-1, oss.cur);
574  oss << " ]";
575  return oss.str();
576 }
577 
578 bool MatchProtocol::IsEqual(const RoutingPolicyMatch &protocol) const {
579  const MatchProtocol in_protocol =
580  static_cast<const MatchProtocol&>(protocol);
581  return (protocols() == in_protocol.protocols());
582 }
std::vector< PrefixMatchConfig > PrefixMatchConfigList
Definition: bgp_config.h:359
const std::string & sub_protocol() const
Definition: bgp_attr.h:933
const Community * community() const
Definition: bgp_attr.h:917
const ExtCommunity * ext_community() const
Definition: bgp_attr.h:918
IPeer * GetPeer()
Definition: bgp_path.h:77
PathSource
Definition: bgp_path.h:38
@ ServiceChain
Definition: bgp_path.h:41
@ Aggregate
Definition: bgp_path.h:43
@ None
Definition: bgp_path.h:39
@ BGP_XMPP
Definition: bgp_path.h:40
@ StaticRoute
Definition: bgp_path.h:42
PathSource GetSource() const
Definition: bgp_path.h:104
static uint32_t CommunityFromString(const std::string &comm, boost::system::error_code *perr=NULL)
static const std::string CommunityToString(uint32_t comm)
const std::vector< uint32_t > & communities() const
Definition: community.h:68
static ExtCommunityList ExtCommunityFromString(const std::string &comm)
Definition: community.cc:375
std::vector< ExtCommunityValue > ExtCommunityList
Definition: community.h:156
boost::array< uint8_t, 8 > ExtCommunityValue
Definition: community.h:155
static std::string ToHexString(const ExtCommunityValue &val)
Definition: community.cc:422
static std::string ToString(const ExtCommunityValue &val)
Definition: community.cc:431
const ExtCommunityList & communities() const
Definition: community.h:183
virtual bool IsXmppPeer() const =0
virtual bool IsRouterTypeBGPaaS() const =0
virtual bool Match(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const
const CommunityRegexStringList & regex_strings() const
bool MatchAny(const BgpAttr *attr) const
const CommunityRegexList & regexs() const
CommunityList to_match_
bool match_all() const
virtual bool IsEqual(const RoutingPolicyMatch &community) const
MatchCommunity(const std::vector< std::string > &communities, bool match_all)
const CommunityList & communities() const
CommunityRegexStringList to_match_regex_strings_
CommunityRegexList to_match_regexs_
bool MatchAll(const BgpAttr *attr) const
virtual std::string ToString() const
CommunityRegexList to_match_regexs_
virtual bool Match(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const
const CommunityRegexStringList & regex_strings() const
const CommunityRegexList & regexs() const
bool Find(const ExtCommunity::ExtCommunityValue &community) const
CommunityRegexStringList to_match_regex_strings_
ExtCommunity::ExtCommunityList to_match_
bool MatchAny(const BgpAttr *attr) const
virtual std::string ToString() const
MatchExtCommunity(const std::vector< std::string > &communities, bool match_all)
virtual bool IsEqual(const RoutingPolicyMatch &community) const
bool MatchAll(const BgpAttr *attr) const
const ExtCommunity::ExtCommunityList & communities() const
virtual bool IsEqual(const RoutingPolicyMatch &prefix) const
virtual std::string ToString() const
PrefixMatchList match_list_
MatchPrefix(const PrefixMatchConfigList &match_config_list)
virtual bool Match(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const
static MatchType GetMatchType(const std::string &match_type_str)
virtual std::string ToString() const
const PathSourceList & protocols() const
virtual bool Match(const BgpRoute *route, const BgpPath *path, const BgpAttr *attr) const
PathSourceList to_match_
virtual bool IsEqual(const RoutingPolicyMatch &community) const
MatchProtocol(const std::vector< std::string > &protocols)
static bool regex_search(const std::string &input, const regex &regex)
Definition: regex.h:26
static bool regex_match(const std::string &input, const regex &regex)
Definition: regex.h:35
static PhysicalDevice::ManagementProtocol FromString(const string &proto)
static const map< MatchProtocol::MatchProtocolType, string > toString
static bool IsSubprotocol(MatchProtocol::MatchProtocolType protocol)
static const vector< MatchProtocol::MatchProtocolType > isSubprotocol
static BgpPath::PathSource PathSourceFromMatchProtocol(MatchProtocol::MatchProtocolType src)
static const map< string, MatchProtocol::MatchProtocolType > fromString
static const map< MatchProtocol::MatchProtocolType, BgpPath::PathSource > pathSourceMap
static MatchProtocol::MatchProtocolType MatchProtocolFromString(const string &protocol)
const string MatchProtocolToString(MatchProtocol::MatchProtocolType protocol)
std::string prefix_to_match
Definition: bgp_config.h:355
std::string prefix_match_type
Definition: bgp_config.h:356