LCOV - code coverage report
Current view: top level - vnsw/agent/pkt/flow_mgmt - flow_mgmt_key.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 119 153 77.8 %
Date: 2026-11-30 02:00:48 Functions: 52 66 78.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #ifndef __AGENT_PKT_FLOW_MGMT_KEY_H__
       7             : #define __AGENT_PKT_FLOW_MGMT_KEY_H__
       8             : 
       9             : #include <db/db_entry.h>
      10             : #include <pkt/flow_event.h>
      11             : 
      12             : class FlowMgmtKey {
      13             : public:
      14             :     enum Type {
      15             :         INVALID,
      16             :         INTERFACE,
      17             :         ACL,
      18             :         ACE_ID,
      19             :         VN,
      20             :         VM,
      21             :         INET4,
      22             :         INET6,
      23             :         BRIDGE,
      24             :         NH,
      25             :         VRF,
      26             :         BGPASASERVICE,
      27             :         END
      28             :     };
      29             : 
      30        2823 :     FlowMgmtKey(Type type, const DBEntry *db_entry) :
      31        2823 :         type_(type), db_entry_(db_entry) {
      32        2823 :     }
      33             : 
      34        2823 :     virtual ~FlowMgmtKey() { }
      35             : 
      36             :     // Clone the key
      37             :     virtual FlowMgmtKey *Clone() = 0;
      38             : 
      39             :     // Convert from FlowMgmtKey to FlowEvent
      40         153 :     virtual void KeyToFlowRequest(FlowEvent *req) {
      41         153 :         req->set_db_entry(db_entry_);
      42         153 :     }
      43             : 
      44             :     // Is DBEntry used as key. Routes dont use DBEntry as key
      45        7385 :     virtual bool UseDBEntry() const { return true; }
      46             : 
      47             :     // Comparator
      48        3154 :     virtual bool Compare(const FlowMgmtKey *rhs) const {
      49        3154 :         return false;
      50             :     }
      51             : 
      52       17524 :     bool IsLess(const FlowMgmtKey *rhs) const {
      53       17524 :         if (type_ != rhs->type_)
      54        3108 :             return type_ < rhs->type_;
      55             : 
      56       14416 :         if (UseDBEntry()) {
      57        7385 :             if (db_entry_ != rhs->db_entry_)
      58        4231 :                 return db_entry_ < rhs->db_entry_;
      59             :         }
      60             : 
      61       10185 :         return Compare(rhs);
      62             :     }
      63             : 
      64             :     FlowEvent::Event FreeDBEntryEvent() const;
      65        1018 :     Type type() const { return type_; }
      66        2166 :     const DBEntry *db_entry() const { return db_entry_; }
      67         119 :     void set_db_entry(const DBEntry *db_entry) { db_entry_ = db_entry; }
      68             : 
      69             : protected:
      70             :     Type type_;
      71             :     mutable const DBEntry *db_entry_;
      72             : 
      73             : private:
      74             :     DISALLOW_COPY_AND_ASSIGN(FlowMgmtKey);
      75             : };
      76             : 
      77             : struct FlowMgmtKeyCmp {
      78       16923 :     bool operator()(const FlowMgmtKey *l, const FlowMgmtKey *r) const {
      79       16923 :         return l->IsLess(r);
      80             :     }
      81             : };
      82             : 
      83             : class AclFlowMgmtKey : public FlowMgmtKey {
      84             : public:
      85          78 :     AclFlowMgmtKey(const AclDBEntry *acl, const AclEntryIDList *ace_id_list) :
      86          78 :         FlowMgmtKey(FlowMgmtKey::ACL, acl) {
      87          78 :         if (ace_id_list) {
      88          68 :             ace_id_list_ = *ace_id_list;
      89             :         }
      90          78 :     }
      91             : 
      92         146 :     virtual ~AclFlowMgmtKey() { }
      93             : 
      94          36 :     virtual FlowMgmtKey *Clone() {
      95          36 :         return new AclFlowMgmtKey(static_cast<const AclDBEntry *>(db_entry()),
      96          36 :                                   &ace_id_list_);
      97             :     }
      98             : 
      99          89 :     const AclEntryIDList *ace_id_list() const { return &ace_id_list_; }
     100             : 
     101          25 :     void set_ace_id_list(const AclEntryIDList *list) {
     102          25 :         ace_id_list_ = *list;
     103          25 :     }
     104             : 
     105             : private:
     106             :     AclEntryIDList ace_id_list_;
     107             :     DISALLOW_COPY_AND_ASSIGN(AclFlowMgmtKey);
     108             : };
     109             : 
     110             : class VnFlowMgmtKey : public FlowMgmtKey {
     111             : public:
     112         233 :     VnFlowMgmtKey(const VnEntry *vn) : FlowMgmtKey(FlowMgmtKey::VN, vn) { }
     113         452 :     virtual ~VnFlowMgmtKey() { }
     114             : 
     115         113 :     virtual FlowMgmtKey *Clone() {
     116         113 :         return new VnFlowMgmtKey(static_cast<const VnEntry *>(db_entry()));
     117             :     }
     118             : 
     119             : private:
     120             :     DISALLOW_COPY_AND_ASSIGN(VnFlowMgmtKey);
     121             : };
     122             : 
     123             : class InterfaceFlowMgmtKey : public FlowMgmtKey {
     124             : public:
     125         315 :     InterfaceFlowMgmtKey(const Interface *intf) :
     126         315 :         FlowMgmtKey(FlowMgmtKey::INTERFACE, intf) {
     127         315 :     }
     128             : 
     129         558 :     virtual ~InterfaceFlowMgmtKey() { }
     130             : 
     131         137 :     virtual FlowMgmtKey *Clone() {
     132         137 :         return new InterfaceFlowMgmtKey(static_cast<const Interface *>(db_entry()));
     133             :     }
     134             : 
     135             : private:
     136             :     DISALLOW_COPY_AND_ASSIGN(InterfaceFlowMgmtKey);
     137             : };
     138             : 
     139             : class NhFlowMgmtKey : public FlowMgmtKey {
     140             : public:
     141         610 :     NhFlowMgmtKey(const NextHop *nh) : FlowMgmtKey(FlowMgmtKey::NH, nh) { }
     142         940 :     virtual ~NhFlowMgmtKey() { }
     143             : 
     144         235 :     virtual FlowMgmtKey *Clone() {
     145         235 :         return new NhFlowMgmtKey(static_cast<const NextHop *>(db_entry()));
     146             :     }
     147             : 
     148             : private:
     149             :     DISALLOW_COPY_AND_ASSIGN(NhFlowMgmtKey);
     150             : };
     151             : 
     152             : class RouteFlowMgmtKey : public FlowMgmtKey {
     153             : public:
     154         912 :     static Type AddrToType(const IpAddress &addr) {
     155         912 :         if (addr.is_v4())
     156         695 :             return INET4;
     157             : 
     158         217 :         if (addr.is_v6())
     159         217 :             return INET6;
     160             : 
     161           0 :         assert(0);
     162             :         return INVALID;
     163             :     }
     164             : 
     165             :     static Type RouteToType(const AgentRoute *rt) {
     166             :         const InetUnicastRouteEntry *inet_rt =
     167             :             dynamic_cast<const InetUnicastRouteEntry *>(rt);
     168             :         if (inet_rt) {
     169             :             return AddrToType(inet_rt->prefix_address());
     170             :         }
     171             : 
     172             :         if (dynamic_cast<const BridgeRouteEntry *>(rt))
     173             :             return BRIDGE;
     174             : 
     175             :         assert(0);
     176             :     }
     177             : 
     178         952 :     RouteFlowMgmtKey(Type type, uint32_t vrf_id) :
     179         952 :         FlowMgmtKey(type, NULL), vrf_id_(vrf_id) {
     180         952 :     }
     181             : 
     182         378 :     RouteFlowMgmtKey(Type type, const AgentRoute *rt, uint32_t vrf_id) :
     183         378 :         FlowMgmtKey(type, rt), vrf_id_(vrf_id) {
     184         378 :     }
     185             : 
     186        1330 :     virtual ~RouteFlowMgmtKey() { }
     187             : 
     188        7031 :     virtual bool UseDBEntry() const { return false; }
     189         313 :     uint32_t vrf_id() const { return vrf_id_; }
     190             : 
     191             : protected:
     192             :     uint32_t vrf_id_;
     193             : 
     194             : private:
     195             :     DISALLOW_COPY_AND_ASSIGN(RouteFlowMgmtKey);
     196             : };
     197             : 
     198             : class InetRouteFlowMgmtKey : public RouteFlowMgmtKey {
     199             : public:
     200         217 :     InetRouteFlowMgmtKey(const InetUnicastRouteEntry *route) :
     201         434 :         RouteFlowMgmtKey(AddrToType(route->prefix_address()), route, route->vrf_id()),
     202         217 :         ip_(route->prefix_address()), plen_(route->prefix_length()) {
     203         217 :     }
     204             : 
     205         695 :     InetRouteFlowMgmtKey(uint32_t vrf_id, const IpAddress &ip, uint8_t plen) :
     206         695 :         RouteFlowMgmtKey(AddrToType(ip), vrf_id), ip_(ip), plen_(plen) {
     207         695 :     }
     208             : 
     209        1437 :     virtual ~InetRouteFlowMgmtKey() { }
     210             : 
     211        4294 :     virtual bool Compare(const FlowMgmtKey *rhs) const {
     212        4294 :         const InetRouteFlowMgmtKey *rhs_key =
     213             :             static_cast<const InetRouteFlowMgmtKey *>(rhs);
     214        4294 :         if (vrf_id_ != rhs_key->vrf_id_)
     215        1250 :             return vrf_id_ < rhs_key->vrf_id_;
     216             : 
     217        3044 :         if (ip_ != rhs_key->ip_)
     218        1246 :             return ip_ < rhs_key->ip_;
     219             : 
     220        1798 :         return plen_ < rhs_key->plen_;
     221             :     }
     222             : 
     223             :     class KeyCmp {
     224             :     public:
     225        8172 :         static std::size_t BitLength(const InetRouteFlowMgmtKey *rt) {
     226             :             return (((sizeof(rt->vrf_id_) + sizeof(rt->type_)) << 3) +
     227        8172 :                     rt->plen_);
     228             :         }
     229             : 
     230       16399 :         static char ByteValue(const InetRouteFlowMgmtKey *rt, std::size_t idx) {
     231             :             const char *ch;
     232       16399 :             std::size_t i = idx;
     233       16399 :             if (i < sizeof(rt->vrf_id_)) {
     234        4027 :                 ch = (const char *)&rt->vrf_id_;
     235        4027 :                 return ch[sizeof(rt->vrf_id_) - i - 1];
     236             :             }
     237       12372 :             i -= sizeof(rt->vrf_id_);
     238       12372 :             if (i < sizeof(rt->type_)) {
     239        3208 :                 ch = (const char *)&rt->type_;
     240        3208 :                 return ch[sizeof(rt->type_) - i - 1];
     241             :             }
     242        9164 :             i -= sizeof(rt->type_);
     243        9164 :             if (rt->type_ == INET4) {
     244        3587 :                 return rt->ip_.to_v4().to_bytes()[i];
     245        5577 :             } else if (rt->type_ == INET6) {
     246        5577 :                 return rt->ip_.to_v6().to_bytes()[i];
     247             :             } else {
     248           0 :                 assert(0);
     249             :                 return 0;
     250             :             }
     251             :         }
     252             :     };
     253             : 
     254         317 :     FlowMgmtKey *Clone() {
     255         317 :         if (ip_.is_v4()) {
     256         239 :             return new InetRouteFlowMgmtKey(vrf_id_, ip_.to_v4(), plen_);
     257             :         }
     258             : 
     259          78 :         if (ip_.is_v6()) {
     260          78 :             return new InetRouteFlowMgmtKey(vrf_id_, ip_.to_v6(), plen_);
     261             :         }
     262             : 
     263           0 :         return NULL;
     264             :     }
     265             : 
     266           0 :     bool Match(const IpAddress &match_ip) const {
     267           0 :         if (ip_.is_v4()) {
     268           0 :             return (Address::GetIp4SubnetAddress(ip_.to_v4(), plen_) ==
     269           0 :                     Address::GetIp4SubnetAddress(match_ip.to_v4(), plen_));
     270           0 :         } else if (ip_.is_v6()) {
     271           0 :             return (Address::GetIp6SubnetAddress(ip_.to_v6(), plen_) ==
     272           0 :                     Address::GetIp6SubnetAddress(match_ip.to_v6(), plen_));
     273             :         }
     274             : 
     275           0 :         assert(0);
     276             :         return false;
     277             :     }
     278             : 
     279             :     bool NeedsReCompute(const FlowEntry *flow);
     280           0 :     IpAddress ip() const { return ip_; }
     281           0 :     uint8_t plen() const { return plen_; }
     282             : 
     283             : private:
     284             :     friend class InetRouteFlowMgmtTree;
     285             : 
     286             :     IpAddress ip_;
     287             :     uint8_t plen_;
     288             :     Patricia::Node node_;
     289             :     DISALLOW_COPY_AND_ASSIGN(InetRouteFlowMgmtKey);
     290             : };
     291             : 
     292             : class BridgeRouteFlowMgmtKey : public RouteFlowMgmtKey {
     293             : public:
     294         161 :     BridgeRouteFlowMgmtKey(const BridgeRouteEntry *rt) :
     295         161 :         RouteFlowMgmtKey(BRIDGE, rt, rt->vrf_id()), mac_(rt->prefix_address()) {
     296         161 :     }
     297             : 
     298         257 :     BridgeRouteFlowMgmtKey(uint32_t vrf_id, const MacAddress &mac) :
     299         257 :         RouteFlowMgmtKey(BRIDGE, vrf_id), mac_(mac) {
     300         257 :     }
     301             : 
     302         666 :     virtual ~BridgeRouteFlowMgmtKey() { }
     303             : 
     304        2737 :     virtual bool Compare(const FlowMgmtKey *rhs) const {
     305        2737 :         const BridgeRouteFlowMgmtKey *rhs_key =
     306             :             static_cast<const BridgeRouteFlowMgmtKey *>(rhs);
     307        2737 :         if (vrf_id_ != rhs_key->vrf_id_)
     308         651 :             return vrf_id_ < rhs_key->vrf_id_;
     309             : 
     310        2086 :         return mac_ < rhs_key->mac_;
     311             :     }
     312             : 
     313         116 :     FlowMgmtKey *Clone() {
     314         116 :         return new BridgeRouteFlowMgmtKey(vrf_id(), mac_);
     315             :     }
     316             : 
     317             : private:
     318             :     friend class BridgeRouteFlowMgmtTree;
     319             : 
     320             :     MacAddress mac_;
     321             :     DISALLOW_COPY_AND_ASSIGN(BridgeRouteFlowMgmtKey);
     322             : };
     323             : 
     324             : class VrfFlowMgmtKey : public FlowMgmtKey {
     325             : public:
     326         257 :     VrfFlowMgmtKey(const VrfEntry *vrf) :
     327         257 :         FlowMgmtKey(FlowMgmtKey::VRF, vrf) {
     328         257 :     }
     329             : 
     330         266 :     virtual ~VrfFlowMgmtKey() { }
     331             : 
     332           9 :     virtual FlowMgmtKey *Clone() {
     333           9 :         return new VrfFlowMgmtKey(static_cast<const VrfEntry *>(db_entry()));
     334             :     }
     335             : 
     336             : private:
     337             :     DISALLOW_COPY_AND_ASSIGN(VrfFlowMgmtKey);
     338             : };
     339             : 
     340             : class BgpAsAServiceFlowMgmtKey : public FlowMgmtKey {
     341             : public:
     342           0 :     BgpAsAServiceFlowMgmtKey(const boost::uuids::uuid &uuid,
     343             :                              uint32_t source_port,
     344             :                              uint8_t cn_index,
     345             :                              HealthCheckInstanceBase *hc_instance,
     346           0 :                              HealthCheckService *hc_service) :
     347           0 :         FlowMgmtKey(FlowMgmtKey::BGPASASERVICE, NULL), uuid_(uuid),
     348           0 :         source_port_(source_port), cn_index_(cn_index),
     349           0 :         bgp_health_check_instance_(hc_instance),
     350           0 :         bgp_health_check_service_(hc_service) { }
     351             : 
     352           0 :     virtual ~BgpAsAServiceFlowMgmtKey() { }
     353             : 
     354           0 :     virtual FlowMgmtKey *Clone() {
     355           0 :         return new BgpAsAServiceFlowMgmtKey(uuid_, source_port_, cn_index_,
     356             :                                             bgp_health_check_instance_,
     357           0 :                                             bgp_health_check_service_);
     358             :     }
     359             : 
     360           0 :     virtual bool UseDBEntry() const { return false; }
     361             : 
     362           0 :     virtual bool Compare(const FlowMgmtKey *rhs) const {
     363           0 :         const BgpAsAServiceFlowMgmtKey *rhs_key =
     364             :             static_cast<const BgpAsAServiceFlowMgmtKey *>(rhs);
     365           0 :         if (uuid_ != rhs_key->uuid_)
     366           0 :             return uuid_ < rhs_key->uuid_;
     367           0 :         if (cn_index_ != rhs_key->cn_index_)
     368           0 :             return cn_index_< rhs_key->cn_index_;
     369           0 :         return source_port_ < rhs_key->source_port_;
     370             :     }
     371             : 
     372           0 :     const boost::uuids::uuid &uuid() const { return uuid_; }
     373           0 :     uint32_t source_port() const { return source_port_; }
     374           0 :     uint8_t cn_index() const { return cn_index_; }
     375             : 
     376             :     HealthCheckInstanceBase *bgp_health_check_instance() const {
     377             :         return bgp_health_check_instance_;
     378             :     }
     379             : 
     380             :     void StartHealthCheck(Agent *agent, FlowEntry *flow,
     381             :                           const boost::uuids::uuid &hc_uuid);
     382             :     void StopHealthCheck(FlowEntry *flow);
     383             : 
     384             : private:
     385             :     boost::uuids::uuid uuid_;
     386             :     uint32_t source_port_;
     387             :     uint8_t cn_index_; //Control node index
     388             :     // By adding the health check instance here, we ensure one BFD session
     389             :     // per <VMI-SRC-IP, BGP-DEST-IP, VMI>
     390             :     mutable HealthCheckInstanceBase *bgp_health_check_instance_;
     391             :     mutable HealthCheckService *bgp_health_check_service_;
     392             :     DISALLOW_COPY_AND_ASSIGN(BgpAsAServiceFlowMgmtKey);
     393             : };
     394             : 
     395             : #endif // __AGENT_PKT_FLOW_MGMT_KEY_H__

Generated by: LCOV version 1.14