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