Line data Source code
1 : /* 2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef SRC_VNSW_AGENT_MAC_LEARNING_MAC_AGING_H_ 6 : #define SRC_VNSW_AGENT_MAC_LEARNING_MAC_AGING_H_ 7 : 8 : #include <mutex> 9 : 10 : #include "cmn/agent.h" 11 : 12 : class MacEntryResp; 13 : class SandeshMacEntry; 14 : 15 : class MacAgingEntry { 16 : public: 17 : MacAgingEntry(MacLearningEntryPtr ptr); 18 0 : virtual ~MacAgingEntry() {} 19 : 20 : void set_mac_learning_entry(MacLearningEntryPtr ptr) { 21 : mac_learning_entry_ = ptr; 22 : } 23 : 24 0 : MacLearningEntryPtr mac_learning_entry() const { 25 0 : return mac_learning_entry_; 26 : } 27 : 28 0 : void set_last_modified_time(uint64_t curr_time) { 29 0 : last_modified_time_ = curr_time; 30 0 : } 31 : 32 0 : uint64_t last_modified_time() const { 33 0 : return last_modified_time_; 34 : } 35 : 36 0 : uint64_t packets() const { 37 0 : return packets_; 38 : } 39 : 40 0 : void set_packets(uint64_t packets) { 41 0 : packets_ = packets; 42 0 : } 43 : 44 0 : void set_deleted(bool deleted) { 45 0 : deleted_ = deleted; 46 0 : } 47 : 48 0 : bool deleted() const { 49 0 : return deleted_; 50 : } 51 : 52 : void FillSandesh(SandeshMacEntry *sme) const; 53 : private: 54 : MacLearningEntryPtr mac_learning_entry_; 55 : uint64_t packets_; 56 : uint64_t last_modified_time_; 57 : bool deleted_; 58 : uint64_t addition_time_; 59 : }; 60 : typedef boost::shared_ptr<MacAgingEntry> MacAgingEntryPtr; 61 : 62 : //Per VRF mac aging table 63 : class MacAgingTable { 64 : public: 65 : static const uint32_t kDefaultAgingTimeout = 30 * 1000; 66 : static const uint32_t kMinEntriesPerScan = 100; 67 : typedef std::pair<MacLearningEntry*, MacAgingEntryPtr> MacAgingPair; 68 : typedef std::map<MacLearningEntry*, MacAgingEntryPtr> MacAgingEntryTable; 69 : 70 : MacAgingTable(Agent *agent, const VrfEntry *); 71 : virtual ~MacAgingTable(); 72 : uint32_t CalculateEntriesPerIteration(uint32_t table_size); 73 0 : uint64_t timeout_in_usecs() const { 74 0 : return timeout_msec_ * 1000; 75 : } 76 : 77 1 : void set_timeout(uint32_t msec) { 78 1 : timeout_msec_ = msec; 79 1 : } 80 : bool Run(); 81 : void Add(MacLearningEntryPtr ptr); 82 : void Delete(MacLearningEntryPtr ptr); 83 : 84 0 : const MacAgingEntry *Find(MacLearningEntry *me) const { 85 : MacAgingEntryTable::const_iterator it = 86 0 : aging_table_.find(me); 87 0 : if (it != aging_table_.end()) { 88 0 : return it->second.get(); 89 : } 90 0 : return NULL; 91 : } 92 : 93 : private: 94 : bool ShouldBeAged(MacAgingEntry *ptr, uint64_t curr_time); 95 : void SendDeleteMsg(MacAgingEntry *ptr); 96 : void ReadStats(MacAgingEntry *ptr); 97 : void Trace(const std::string &str, MacAgingEntry *ptr); 98 : friend class MacAgingSandeshResp; 99 : Agent *agent_; 100 : MacAgingEntryTable aging_table_; 101 : MacLearningEntry* last_key_; 102 : uint32_t timeout_msec_; 103 : VrfEntryConstRef vrf_; 104 : DISALLOW_COPY_AND_ASSIGN(MacAgingTable); 105 : }; 106 : 107 : //MacAgingPartition maintains Per VRF mac entries 108 : //for aging purpose. Timer for each partition gets 109 : //fired every 100ms and goes thru all the VRF entries. 110 : //No. of entries to be visited would be based on aging timeout 111 : //and no. of entries in tree. 112 : class MacAgingPartition { 113 : public: 114 : static const uint32_t kMinIterationTimeout = 1 * 100; 115 : typedef WorkQueue<MacLearningEntryRequestPtr> MacAgingQueue; 116 : typedef boost::shared_ptr<MacAgingTable> MacAgingTablePtr; 117 : typedef std::pair<uint32_t, MacAgingTablePtr> MacAgingTablePair; 118 : typedef std::map<uint32_t, MacAgingTablePtr> MacAgingTableMap; 119 : MacAgingPartition(Agent *agent, uint32_t id); 120 : virtual ~MacAgingPartition(); 121 : void Enqueue(MacLearningEntryRequestPtr req); 122 : bool Run(); 123 : bool RequestHandler(MacLearningEntryRequestPtr ptr); 124 : void Add(MacLearningEntryPtr ptr); 125 : void Delete(MacLearningEntryPtr ptr); 126 : 127 2 : MacAgingTable *Find(uint32_t id) { 128 2 : return aging_table_map_[id].get(); 129 : } 130 : 131 : private: 132 : void DeleteVrf(uint32_t id); 133 : friend class MacAgingSandeshResp; 134 : Agent *agent_; 135 : uint32_t partition_id_; 136 : MacAgingQueue request_queue_; 137 : Timer *timer_; 138 : std::mutex mutex_; 139 : MacAgingTableMap aging_table_map_; 140 : DISALLOW_COPY_AND_ASSIGN(MacAgingPartition); 141 : }; 142 : #endif