LCOV - code coverage report
Current view: top level - vnsw/agent/mac_learning - mac_learning_mgmt.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 60 73 82.2 %
Date: 2026-11-09 02:10:49 Functions: 31 41 75.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #ifndef SRC_VNSW_AGENT_MAC_LEARNING_MAC_LEARNING_MGMT_H_
       7             : #define SRC_VNSW_AGENT_MAC_LEARNING_MAC_LEARNING_MGMT_H_
       8             : #include "mac_learning_key.h"
       9             : #include "mac_ip_learning_key.h"
      10             : 
      11             : class MacLearningMgmtDBEntry;
      12             : class MacLearningMgmtManager;
      13             : class MacLearningMgmtDBTree;
      14             : class MacLearningMgmtNode {
      15             : public:
      16             :     MacLearningMgmtNode(MacLearningEntryPtr ptr);
      17             :     virtual ~MacLearningMgmtNode();
      18             : 
      19             :     void set_intf(MacLearningMgmtDBEntry *intf) {
      20             :         intf_.reset(intf);
      21             :     }
      22             : 
      23             :     void set_vrf(MacLearningMgmtDBEntry *vrf) {
      24             :         vrf_.reset(vrf);
      25             :     }
      26             : 
      27             :     void set_rt(MacLearningMgmtDBEntry *rt) {
      28             :         rt_.reset(rt);
      29             :     }
      30             : 
      31             :     void set_vn(MacLearningMgmtDBEntry *vn) {
      32             :         vn_.reset(vn);
      33             :     }
      34             : 
      35             :     void set_hc(MacLearningMgmtDBEntry *hc) {
      36             :         hc_service_.reset(hc);
      37             :     }
      38             : 
      39             :     void UpdateRef(MacLearningMgmtManager *mgr);
      40             : 
      41           0 :     MacLearningEntryPtr mac_learning_entry() {
      42           0 :         return mac_entry_;
      43             :     }
      44             : 
      45           0 :     void set_mac_learning_entry(MacLearningEntryPtr mac_entry) {
      46           0 :         mac_entry_ = mac_entry;
      47           0 :     }
      48             : private:
      49             :     MacLearningEntryPtr mac_entry_;
      50             :     DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> intf_;
      51             :     DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> vrf_;
      52             :     DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> rt_;
      53             :     DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> vn_;
      54             :     DependencyRef<MacLearningMgmtNode, MacLearningMgmtDBEntry> hc_service_;
      55             :     DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtNode);
      56             : };
      57             : typedef boost::shared_ptr<MacLearningMgmtNode> MacLearningMgmtNodePtr;
      58             : 
      59             : class MacLearningMgmtDBEntry {
      60             : public:
      61             :     typedef DependencyList<MacLearningMgmtNode,
      62             :                            MacLearningMgmtDBEntry> MacLearningEntryList;
      63             :     enum Type {
      64             :         INVALID,
      65             :         INTERFACE,
      66             :         VRF,
      67             :         BRIDGE,
      68             :         EVPN,
      69             :         VN,
      70             :         HC_SERVICE,
      71             :         END
      72             :     };
      73             : 
      74             :      MacLearningMgmtDBEntry(Type type_, const DBEntry *entry);
      75        4398 :      virtual ~MacLearningMgmtDBEntry() {}
      76             : 
      77             :      const DBEntry *db_entry() const {
      78             :          return db_entry_;
      79             :      }
      80             : 
      81        8230 :      virtual bool UseDBEntry() const {
      82        8230 :          return true;
      83             :      }
      84             :      void Change();
      85             :      void Delete(bool set_delete, bool notify_event = true);
      86             :      virtual bool TryDelete();
      87             : 
      88         736 :      void set_tree(MacLearningMgmtDBTree *tree) {
      89         736 :          tree_ = tree;
      90         736 :      }
      91             : 
      92        2002 :      void set_db_entry(const DBEntry *entry) {
      93        2002 :          db_entry_ = entry;
      94        2002 :      }
      95             : 
      96        3380 :      virtual bool Compare(const MacLearningMgmtDBEntry *rhs) const {
      97        3380 :          return false;
      98             :      }
      99             : 
     100       28420 :      virtual bool IsLess(const MacLearningMgmtDBEntry *rhs) const {
     101       28420 :          if (type_ != rhs->type_) {
     102           0 :              return type_ < rhs->type_;
     103             :          }
     104             : 
     105       28420 :          if (UseDBEntry()) {
     106        8230 :              if (db_entry_ != rhs->db_entry_) {
     107        4850 :                  return db_entry_ < rhs->db_entry_;
     108             :              }
     109             :          }
     110             : 
     111       23570 :          return Compare(rhs);
     112             :      }
     113             : 
     114        1160 :      MacLearningMgmtDBTree* tree() const {
     115        1160 :          return tree_;
     116             :      }
     117             : 
     118         736 :      void set_gen_id(uint32_t gen_id) {
     119         736 :          gen_id_ = gen_id;
     120         736 :      }
     121             : 
     122             :      uint32_t gen_id() const {
     123             :          return gen_id_;
     124             :      }
     125             : 
     126             : protected:
     127             :      Type type_;
     128             :      const DBEntry* db_entry_;
     129             :      bool deleted_;
     130           0 :      DEPENDENCY_LIST(MacLearningMgmtNode, MacLearningMgmtDBEntry, mac_entry_list_);
     131             :      MacLearningMgmtDBTree *tree_;
     132             :      uint32_t gen_id_;
     133             :      DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtDBEntry);
     134             : };
     135             : 
     136             : class MacLearningMgmtIntfEntry : public MacLearningMgmtDBEntry {
     137             : public:
     138             :      MacLearningMgmtIntfEntry(const Interface *intf);
     139         930 :      virtual ~MacLearningMgmtIntfEntry() {}
     140             : private:
     141             :     DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtIntfEntry);
     142             : };
     143             : 
     144             : class MacLearningMgmtVrfEntry : public MacLearningMgmtDBEntry {
     145             : public:
     146             :      MacLearningMgmtVrfEntry(const VrfEntry *vrf);
     147         722 :      virtual ~MacLearningMgmtVrfEntry() {}
     148             :      virtual bool TryDelete();
     149             : 
     150             : private:
     151             :      DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtVrfEntry);
     152             : };
     153             : 
     154             : class MacLearningMgmtRouteEntry : public MacLearningMgmtDBEntry {
     155             : public:
     156             :     MacLearningMgmtRouteEntry(const AgentRoute *rt);
     157             :     MacLearningMgmtRouteEntry(const std::string &vrf,
     158             :                               const MacAddress &mac);
     159        3206 :    virtual ~MacLearningMgmtRouteEntry() {}
     160       20190 :    virtual bool UseDBEntry() const {
     161       20190 :        return false;
     162             :    }
     163             : 
     164             :    virtual bool TryDelete();
     165       20190 :    virtual bool Compare(const MacLearningMgmtDBEntry *rhs) const {
     166       20190 :        const MacLearningMgmtRouteEntry *rhs_rt =
     167             :            static_cast<const MacLearningMgmtRouteEntry *>(rhs);
     168       20190 :        if (vrf_ != rhs_rt->vrf_) {
     169        8855 :            return vrf_ < rhs_rt->vrf_;
     170             :        }
     171       11335 :        return mac_ < rhs_rt->mac_;
     172             :    }
     173             : 
     174         396 :    const std::string& vrf() {
     175         396 :        return vrf_;
     176             :    }
     177             : private:
     178             :     std::string vrf_;
     179             :     MacAddress mac_;
     180             :     DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtRouteEntry);
     181             : };
     182             : 
     183             : class MacLearningMgmtVnEntry : public MacLearningMgmtDBEntry {
     184             : public:
     185             :      MacLearningMgmtVnEntry(const VnEntry *vn);
     186         276 :      virtual ~MacLearningMgmtVnEntry() {}
     187             : private:
     188             :     DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtVnEntry);
     189             : };
     190             : 
     191             : class MacLearningMgmtHcServiceEntry : public MacLearningMgmtDBEntry {
     192             : public:
     193             :      MacLearningMgmtHcServiceEntry(const HealthCheckService *hc);
     194           0 :      virtual ~MacLearningMgmtHcServiceEntry() {}
     195             : private:
     196             :     DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtHcServiceEntry);
     197             : };
     198             : class MacLearningMgmtKeyCmp {
     199             : public:
     200       28420 :     bool operator()(const MacLearningMgmtDBEntry *lhs, const MacLearningMgmtDBEntry *rhs) const {
     201       28420 :         return lhs->IsLess(rhs);
     202             :     }
     203             : };
     204             : 
     205             : class MacLearningMgmtDBTree {
     206             : public:
     207             :     typedef std::map<MacLearningMgmtDBEntry *, MacLearningMgmtDBEntry *,
     208             :                      MacLearningMgmtKeyCmp> Tree;
     209             :     typedef std::pair<MacLearningMgmtDBEntry *, MacLearningMgmtDBEntry *>
     210             :         MacLearningMgmtDBPair;
     211             : 
     212             :     MacLearningMgmtDBTree(MacLearningMgmtManager *mgr);
     213          10 :     virtual ~MacLearningMgmtDBTree() {}
     214             : 
     215             :     void Add(MacLearningMgmtDBEntry *entry);
     216             :     void Change(MacLearningMgmtDBEntry *entry);
     217             :     void Delete(MacLearningMgmtDBEntry *entry);
     218             :     void Erase(MacLearningMgmtDBEntry *entry);
     219             :     MacLearningMgmtDBEntry* Find(MacLearningMgmtDBEntry *entry);
     220         498 :     MacLearningMgmtDBEntry* LowerBound(MacLearningMgmtDBEntry* entry) {
     221         498 :         Tree::iterator it = tree_.lower_bound(entry);
     222         498 :         if (it != tree_.end()) {
     223         396 :             return it->second;
     224             :         }
     225         102 :         return NULL;
     226             :     }
     227             : 
     228        1346 :     MacLearningMgmtManager* mac_learning_mac_manager() {
     229        1346 :         return mac_learning_mac_manager_;
     230             :     }
     231             :     void TryDelete(MacLearningMgmtDBEntry *db_entry);
     232             : protected:
     233             :     Tree tree_;
     234             :     MacLearningMgmtManager *mac_learning_mac_manager_;
     235             :     DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtDBTree);
     236             : };
     237             : 
     238             : class MacLearningMgmtRequest {
     239             : public:
     240             :     enum Event {
     241             :         ADD_MAC,
     242             :         CHANGE_MAC,
     243             :         DELETE_MAC,
     244             :         ADD_DBENTRY,
     245             :         CHANGE_DBENTRY,
     246             :         DELETE_DBENTRY,
     247             :         DELETE_DBENTRY_NO_OP,
     248             :         DELETE_ALL_MAC,
     249             :         RELEASE_TOKEN,
     250             :         ADD_MAC_IP,
     251             :         CHANGE_MAC_IP,
     252             :         DELETE_MAC_IP,
     253             :     };
     254             : 
     255           0 :     MacLearningMgmtRequest(Event event, MacLearningEntryPtr ptr):
     256           0 :         event_(event), mac_learning_entry_(ptr) {
     257           0 :     }
     258             : 
     259        2740 :     MacLearningMgmtRequest(Event event, const DBEntry *db_entry,
     260        2740 :                            uint32_t gen_id):
     261        2740 :         event_(event), db_entry_(db_entry), gen_id_(gen_id) {
     262        2740 :     }
     263             : 
     264           0 :     MacLearningEntryPtr mac_learning_entry() {
     265           0 :         return mac_learning_entry_;
     266             :     }
     267             : 
     268        2740 :     Event event() {
     269        2740 :         return event_;
     270             :     }
     271             : 
     272        4742 :     const DBEntry *db_entry() {
     273        4742 :         return db_entry_;
     274             :     }
     275             : 
     276         736 :     uint32_t gen_id() const {
     277         736 :         return gen_id_;
     278             :     }
     279             : 
     280             :     void set_gen_id(uint32_t gen_id) {
     281             :         gen_id_ = gen_id;
     282             :     }
     283             : 
     284             : private:
     285             :     Event event_;
     286             :     MacLearningEntryPtr mac_learning_entry_;
     287             :     const DBEntry *db_entry_;
     288             :     uint32_t gen_id_;
     289             : };
     290             : 
     291             : typedef boost::shared_ptr<MacLearningMgmtRequest> MacLearningMgmtRequestPtr;
     292             : 
     293             : class MacLearningMgmtManager {
     294             : public:
     295             :     typedef WorkQueue<MacLearningMgmtRequestPtr> MacLearningMgmtQueue;
     296             :     typedef std::map<MacLearningKey, MacLearningMgmtNodePtr,
     297             :                      MacLearningKeyCmp> MacLearningNodeTree;
     298             :     typedef std::pair<MacLearningKey, MacLearningMgmtNodePtr>
     299             :         MacLearningNodePair;
     300             : 
     301             :     typedef std::map<MacIpLearningKey, MacLearningMgmtNodePtr,
     302             :                      MacIpLearningKeyCmp> MacIpLearningNodeTree;
     303             :     typedef std::pair<MacIpLearningKey, MacLearningMgmtNodePtr>
     304             :         MacIpLearningNodePair;
     305             :     MacLearningMgmtManager(Agent *agent);
     306           4 :     virtual ~MacLearningMgmtManager() {}
     307             :     bool RequestHandler(MacLearningMgmtRequestPtr ptr);
     308             : 
     309             :     void AddMacLearningEntry(MacLearningMgmtRequestPtr ptr);
     310             :     void AddMacIpLearningEntry(MacLearningMgmtRequestPtr ptr);
     311             :     void ReleaseToken(MacLearningMgmtRequestPtr ptr);
     312             :     void DeleteMacLearningEntry(MacLearningMgmtRequestPtr ptr);
     313             :     void DeleteMacIpLearningEntry(MacLearningMgmtRequestPtr ptr);
     314             :     void AddDBEntry(MacLearningMgmtRequestPtr ptr);
     315             :     void DeleteDBEntry(MacLearningMgmtRequestPtr ptr, bool notify_event = true);
     316             :     void Enqueue(MacLearningMgmtRequestPtr &ptr);
     317             :     void DeleteAllEntry(MacLearningMgmtRequestPtr ptr);
     318             : 
     319             :     MacLearningMgmtDBEntry* Locate(const DBEntry *e);
     320             :     MacLearningMgmtDBEntry* Find(const DBEntry *e);
     321             :     MacLearningMgmtDBEntry* Locate(const std::string &vrf,
     322             :                                    const MacAddress &mac);
     323         424 :     MacLearningMgmtDBTree* vrf_tree() {
     324         424 :         return &vrf_tree_;
     325             :     }
     326             : 
     327             :     MacLearningMgmtDBTree* rt_tree() {
     328             :         return &rt_tree_;
     329             :     }
     330             : 
     331             :     bool IsVrfRouteEmpty(const std::string &vrf_name);
     332         424 :     Agent *agent() const {
     333         424 :         return agent_;
     334             :     }
     335             : private:
     336             :     Agent *agent_;
     337             :     MacLearningNodeTree mac_learning_node_tree_;
     338             :     MacIpLearningNodeTree mac_ip_learning_node_tree_;
     339             :     MacLearningMgmtDBTree intf_tree_;
     340             :     MacLearningMgmtDBTree vrf_tree_;
     341             :     MacLearningMgmtDBTree rt_tree_;
     342             :     MacLearningMgmtDBTree vn_tree_;
     343             :     MacLearningMgmtDBTree hc_tree_;
     344             :     MacLearningMgmtQueue request_queue_;
     345             :     DISALLOW_COPY_AND_ASSIGN(MacLearningMgmtManager);
     346             : };
     347             : #endif

Generated by: LCOV version 1.14