LCOV - code coverage report
Current view: top level - bgp/routing-policy - routing_policy_match.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 31 32 96.9 %
Date: 2026-06-18 01:51:13 Functions: 19 20 95.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14