Line data Source code
1 : /* 2 : * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef SRC_BGP_ROUTING_POLICY_ROUTING_POLICY_MATCH_H_ 7 : #define SRC_BGP_ROUTING_POLICY_ROUTING_POLICY_MATCH_H_ 8 : 9 : #include <stdint.h> 10 : 11 : #include <set> 12 : #include <string> 13 : #include <typeinfo> 14 : #include <utility> 15 : #include <vector> 16 : 17 : #include "base/regex.h" 18 : #include "bgp/bgp_config.h" 19 : #include "bgp/inet/inet_route.h" 20 : #include "bgp/inet6/inet6_route.h" 21 : 22 : class BgpAttr; 23 : class BgpPath; 24 : class BgpRoute; 25 : class Community; 26 : 27 : class RoutingPolicyMatch { 28 : public: 29 : virtual std::string ToString() const = 0; 30 1048 : virtual ~RoutingPolicyMatch() { 31 1048 : } 32 1041 : bool operator()(const BgpRoute *route, 33 : const BgpPath *path, const BgpAttr *attr) const { 34 1041 : return Match(route, path, attr); 35 : } 36 : virtual bool Match(const BgpRoute *route, 37 : const BgpPath *path, const BgpAttr *attr) const = 0; 38 269 : virtual bool operator==(const RoutingPolicyMatch &match) const { 39 269 : if (typeid(match) == typeid(*this)) 40 269 : return(IsEqual(match)); 41 0 : return false; 42 : } 43 269 : virtual bool operator!=(const RoutingPolicyMatch &match) const { 44 269 : return !operator==(match); 45 : } 46 : virtual bool IsEqual(const RoutingPolicyMatch &match) const = 0; 47 : }; 48 : 49 : class MatchCommunity: public RoutingPolicyMatch { 50 : public: 51 : typedef std::set<uint32_t> CommunityList; 52 : typedef std::vector<std::string> CommunityRegexStringList; 53 : typedef std::vector<contrail::regex> CommunityRegexList; 54 : 55 : MatchCommunity(const std::vector<std::string> &communities, bool match_all); 56 : virtual ~MatchCommunity(); 57 : virtual bool Match(const BgpRoute *route, 58 : const BgpPath *path, const BgpAttr *attr) const; 59 : virtual std::string ToString() const; 60 : virtual bool IsEqual(const RoutingPolicyMatch &community) const; 61 420 : bool match_all() const { return match_all_; } 62 951 : const CommunityList &communities() const { return to_match_; } 63 800 : const CommunityRegexStringList ®ex_strings() const { 64 800 : return to_match_regex_strings_; 65 : } 66 134 : const CommunityRegexList ®exs() const { return to_match_regexs_; } 67 : 68 : private: 69 : bool MatchAll(const BgpAttr *attr) const; 70 : bool MatchAny(const BgpAttr *attr) const; 71 : 72 : bool match_all_; 73 : CommunityList to_match_; 74 : CommunityRegexStringList to_match_regex_strings_; 75 : CommunityRegexList to_match_regexs_; 76 : }; 77 : 78 : class MatchExtCommunity: public RoutingPolicyMatch { 79 : public: 80 : typedef std::vector<std::string> CommunityRegexStringList; 81 : typedef std::vector<contrail::regex> CommunityRegexList; 82 : 83 : MatchExtCommunity(const std::vector<std::string> &communities, 84 : bool match_all); 85 : virtual ~MatchExtCommunity(); 86 : virtual bool Match(const BgpRoute *route, 87 : const BgpPath *path, const BgpAttr *attr) const; 88 : virtual std::string ToString() const; 89 : virtual bool IsEqual(const RoutingPolicyMatch &community) const; 90 104 : bool match_all() const { return match_all_; } 91 655 : const ExtCommunity::ExtCommunityList &communities() const { 92 655 : return to_match_; 93 : } 94 209 : const CommunityRegexStringList ®ex_strings() const { 95 209 : return to_match_regex_strings_; 96 : } 97 102 : const CommunityRegexList ®exs() const { return to_match_regexs_; } 98 : bool Find(const ExtCommunity::ExtCommunityValue &community) const; 99 : 100 : private: 101 : bool MatchAll(const BgpAttr *attr) const; 102 : bool MatchAny(const BgpAttr *attr) const; 103 : const ExtCommunity::ExtCommunityList ExtCommunityFromString( 104 : const std::string &comm); 105 : 106 : bool match_all_; 107 : ExtCommunity::ExtCommunityList to_match_; 108 : CommunityRegexStringList to_match_regex_strings_; 109 : CommunityRegexList to_match_regexs_; 110 : }; 111 : 112 : class MatchProtocol: public RoutingPolicyMatch { 113 : public: 114 : enum MatchProtocolType { 115 : Unspecified = 0, 116 : BGP, 117 : XMPP, 118 : StaticRoute, 119 : ServiceChainRoute, 120 : AggregateRoute, 121 : Interface, 122 : InterfaceStatic, 123 : ServiceInterface, 124 : BGPaaS 125 : }; 126 : typedef std::vector<MatchProtocolType> PathSourceList; 127 : explicit MatchProtocol(const std::vector<std::string> &protocols); 128 : virtual ~MatchProtocol(); 129 : virtual bool Match(const BgpRoute *route, 130 : const BgpPath *path, const BgpAttr *attr) const; 131 : virtual std::string ToString() const; 132 : virtual bool IsEqual(const RoutingPolicyMatch &community) const; 133 154 : const PathSourceList &protocols() const { 134 154 : return to_match_; 135 : } 136 : 137 : private: 138 : PathSourceList to_match_; 139 : }; 140 : const std::string MatchProtocolToString(MatchProtocol::MatchProtocolType protocol); 141 : 142 : template <typename T1, typename T2> 143 : struct PrefixMatchBase { 144 : typedef T1 RouteT; 145 : typedef T2 PrefixT; 146 : }; 147 : 148 : typedef PrefixMatchBase<InetRoute, Ip4Prefix> PrefixMatchInet; 149 : typedef PrefixMatchBase<Inet6Route, Inet6Prefix> PrefixMatchInet6; 150 : 151 : template <typename T> 152 : class MatchPrefix : public RoutingPolicyMatch { 153 : public: 154 : typedef typename T::RouteT RouteT; 155 : typedef typename T::PrefixT PrefixT; 156 : enum MatchType { 157 : EXACT, 158 : LONGER, 159 : ORLONGER, 160 : }; 161 : 162 : class PrefixMatch { 163 : public: 164 298 : PrefixMatch(PrefixT prefix, MatchType match_type) 165 298 : : prefix(prefix), match_type(match_type) { 166 298 : } 167 248 : bool operator==(const PrefixMatch &rhs) const { 168 248 : return (prefix == rhs.prefix && match_type == rhs.match_type); 169 : } 170 160 : bool operator<(const PrefixMatch &rhs) const { 171 160 : BOOL_KEY_COMPARE(prefix, rhs.prefix); 172 14 : BOOL_KEY_COMPARE(match_type, rhs.match_type); 173 6 : return false; 174 : } 175 : 176 : PrefixT prefix; 177 : MatchType match_type; 178 : }; 179 : 180 : explicit MatchPrefix(const PrefixMatchConfigList &match_config_list); 181 : virtual ~MatchPrefix(); 182 : virtual bool Match(const BgpRoute *route, 183 : const BgpPath *path, const BgpAttr *attr) const; 184 : virtual std::string ToString() const; 185 : virtual bool IsEqual(const RoutingPolicyMatch &prefix) const; 186 : 187 : static MatchType GetMatchType(const std::string &match_type_str); 188 : 189 : private: 190 : template <typename U> friend class MatchPrefixTest; 191 : typedef std::vector<PrefixMatch> PrefixMatchList; 192 : 193 : PrefixMatchList match_list_; 194 : }; 195 : 196 : typedef MatchPrefix<PrefixMatchInet> MatchPrefixInet; 197 : typedef MatchPrefix<PrefixMatchInet6> MatchPrefixInet6; 198 : 199 : #endif // SRC_BGP_ROUTING_POLICY_ROUTING_POLICY_MATCH_H_