Line data Source code
1 : /* 2 : * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef _ROOT_STATS_MANAGER_H_ 7 : #define _ROOT_STATS_MANAGER_H_ 8 : 9 : #include <cmn/agent_cmn.h> 10 : #include <cmn/agent_stats.h> 11 : #include <oper/vrf.h> 12 : #include <oper/interface.h> 13 : #include <vrouter_types.h> 14 : #include <string> 15 : #include <map> 16 : #include <utility> 17 : #include <uve/flow_uve_stats_request.h> 18 : #include <vr_types.h> 19 : #include <uve/agent_uve.h> 20 : 21 : struct FlowRateComputeInfo { 22 : uint64_t prev_time_; 23 : uint64_t prev_flow_created_; 24 : uint64_t prev_flow_aged_; 25 : 26 330 : FlowRateComputeInfo() : prev_time_(UTCTimestampUsec()), 27 330 : prev_flow_created_(0), prev_flow_aged_(0) { 28 330 : } 29 : }; 30 : 31 : #define DEFAULT_FUVE_REQUEST_QUEUE_SIZE (4*1024*1024) 32 : 33 : // The container class for storing stats queried from vrouter 34 : // Defines routines for storing and managing (add, delete and query) 35 : // interface, vrf and drop statistics 36 : class StatsManager { 37 : public: 38 : struct InterfaceStats { 39 : InterfaceStats(); 40 : void UpdateStats(uint64_t in_b, uint64_t in_p, uint64_t out_b, 41 : uint64_t out_p); 42 : void UpdatePrevStats(); 43 : void GetDiffStats(uint64_t *in_b, uint64_t *out_b) const; 44 : 45 : std::string name; 46 : int32_t speed; 47 : int32_t duplexity; 48 : uint64_t in_pkts; 49 : uint64_t in_bytes; 50 : uint64_t out_pkts; 51 : uint64_t out_bytes; 52 : uint64_t prev_in_bytes; 53 : uint64_t prev_out_bytes; 54 : uint64_t prev_5min_in_bytes; 55 : uint64_t prev_5min_out_bytes; 56 : uint64_t stats_time; 57 : FlowRateComputeInfo flow_info; 58 : AgentStats::FlowCounters added; 59 : AgentStats::FlowCounters deleted; 60 : bool drop_stats_received; 61 : vr_drop_stats_req drop_stats; 62 : }; 63 : struct VrfStats { 64 : VrfStats(); 65 : std::string name; 66 : uint64_t discards; 67 : uint64_t resolves; 68 : uint64_t receives; 69 : uint64_t udp_tunnels; 70 : uint64_t udp_mpls_tunnels; 71 : uint64_t gre_mpls_tunnels; 72 : uint64_t ecmp_composites; 73 : uint64_t l2_mcast_composites; 74 : uint64_t fabric_composites; 75 : uint64_t encaps; 76 : uint64_t l2_encaps; 77 : uint64_t gros; 78 : uint64_t diags; 79 : uint64_t encap_composites; 80 : uint64_t evpn_composites; 81 : uint64_t vrf_translates; 82 : uint64_t vxlan_tunnels; 83 : uint64_t arp_virtual_proxy; 84 : uint64_t arp_virtual_stitch; 85 : uint64_t arp_virtual_flood; 86 : uint64_t arp_physical_stitch; 87 : uint64_t arp_tor_proxy; 88 : uint64_t arp_physical_flood; 89 : uint64_t l2_receives; 90 : uint64_t uuc_floods; 91 : 92 : uint64_t prev_discards; 93 : uint64_t prev_resolves; 94 : uint64_t prev_receives; 95 : uint64_t prev_udp_tunnels; 96 : uint64_t prev_udp_mpls_tunnels; 97 : uint64_t prev_gre_mpls_tunnels; 98 : uint64_t prev_ecmp_composites; 99 : uint64_t prev_l2_mcast_composites; 100 : uint64_t prev_fabric_composites; 101 : uint64_t prev_encaps; 102 : uint64_t prev_l2_encaps; 103 : uint64_t prev_gros; 104 : uint64_t prev_diags; 105 : uint64_t prev_encap_composites; 106 : uint64_t prev_evpn_composites; 107 : uint64_t prev_vrf_translates; 108 : uint64_t prev_vxlan_tunnels; 109 : uint64_t prev_arp_virtual_proxy; 110 : uint64_t prev_arp_virtual_stitch; 111 : uint64_t prev_arp_virtual_flood; 112 : uint64_t prev_arp_physical_stitch; 113 : uint64_t prev_arp_tor_proxy; 114 : uint64_t prev_arp_physical_flood; 115 : uint64_t prev_l2_receives; 116 : uint64_t prev_uuc_floods; 117 : 118 : uint64_t k_discards; 119 : uint64_t k_resolves; 120 : uint64_t k_receives; 121 : uint64_t k_udp_tunnels; 122 : uint64_t k_udp_mpls_tunnels; 123 : uint64_t k_gre_mpls_tunnels; 124 : uint64_t k_ecmp_composites; 125 : uint64_t k_l2_mcast_composites; 126 : uint64_t k_fabric_composites; 127 : uint64_t k_encaps; 128 : uint64_t k_l2_encaps; 129 : uint64_t k_gros; 130 : uint64_t k_diags; 131 : uint64_t k_encap_composites; 132 : uint64_t k_evpn_composites; 133 : uint64_t k_vrf_translates; 134 : uint64_t k_vxlan_tunnels; 135 : uint64_t k_arp_virtual_proxy; 136 : uint64_t k_arp_virtual_stitch; 137 : uint64_t k_arp_virtual_flood; 138 : uint64_t k_arp_physical_stitch; 139 : uint64_t k_arp_tor_proxy; 140 : uint64_t k_arp_physical_flood; 141 : uint64_t k_l2_receives; 142 : uint64_t k_uuc_floods; 143 : }; 144 : 145 : typedef std::map<const Interface *, InterfaceStats> InterfaceStatsTree; 146 : typedef std::pair<const Interface *, InterfaceStats> InterfaceStatsPair; 147 : typedef std::map<int, VrfStats> VrfIdToVrfStatsTree; 148 : typedef std::pair<int, VrfStats> VrfStatsPair; 149 : 150 : struct FlowRuleMatchInfo { 151 : std::string interface; 152 : std::string sg_rule_uuid; 153 : FlowUveFwPolicyInfo fw_policy_info; 154 : FlowUveVnAcePolicyInfo vn_ace_info; 155 150 : FlowRuleMatchInfo(const std::string &itf, const std::string &sg_rule, 156 : const FlowUveFwPolicyInfo &fw_info, 157 150 : const FlowUveVnAcePolicyInfo &nw_ace_info) : 158 150 : interface(itf), sg_rule_uuid(sg_rule), fw_policy_info(fw_info), 159 150 : vn_ace_info(nw_ace_info) { 160 150 : } 161 204 : bool IsFwPolicyInfoEqual(const FlowUveFwPolicyInfo &info) const { 162 204 : if (fw_policy_info.is_valid_ != info.is_valid_) { 163 108 : return false; 164 : } 165 96 : if (fw_policy_info.local_tagset_ != info.local_tagset_) { 166 0 : return false; 167 : } 168 96 : if (fw_policy_info.fw_policy_ != info.fw_policy_) { 169 24 : return false; 170 : } 171 72 : if (fw_policy_info.remote_tagset_ != info.remote_tagset_) { 172 0 : return false; 173 : } 174 72 : if (fw_policy_info.remote_prefix_ != info.remote_prefix_) { 175 0 : return false; 176 : } 177 72 : if (fw_policy_info.local_vn_ != info.local_vn_) { 178 0 : return false; 179 : } 180 72 : if (fw_policy_info.remote_vn_ != info.remote_vn_) { 181 0 : return false; 182 : } 183 72 : if (fw_policy_info.initiator_ != info.initiator_) { 184 66 : return false; 185 : } 186 6 : return true; 187 : } 188 76 : bool IsVnAceInfoEqual(const FlowUveVnAcePolicyInfo &info) const { 189 76 : if (vn_ace_info.vn_ != info.vn_) { 190 0 : return false; 191 : } 192 76 : if (vn_ace_info.nw_ace_uuid_ != info.nw_ace_uuid_) { 193 17 : return false; 194 : } 195 59 : return true; 196 : } 197 : }; 198 : 199 : typedef std::map<const boost::uuids::uuid, FlowRuleMatchInfo> FlowAceTree; 200 : typedef std::pair<const boost::uuids::uuid, FlowRuleMatchInfo> FlowAcePair; 201 : 202 : explicit StatsManager(Agent *agent); 203 : virtual ~StatsManager(); 204 : 205 40 : vr_drop_stats_req drop_stats() const { return drop_stats_; } 206 0 : void set_drop_stats(const vr_drop_stats_req &req) { drop_stats_ = req; } 207 : 208 : InterfaceStats* GetInterfaceStats(const Interface *intf); 209 : 210 : VrfStats* GetVrfStats(int vrf_id); 211 2 : std::string GetNamelessVrf() { return "__untitled__"; } 212 2 : int GetNamelessVrfId() { return -1; } 213 : void Shutdown(void); 214 : void RegisterDBClients(); 215 : void InitDone(); 216 : bool RequestHandler(boost::shared_ptr<FlowUveStatsRequest> req); 217 : void EnqueueEvent(const boost::shared_ptr<FlowUveStatsRequest> &req); 218 : bool BuildFlowRate(AgentStats::FlowCounters &created, 219 : AgentStats::FlowCounters &aged, 220 : FlowRateComputeInfo &flow_info, 221 : VrouterFlowRate &flow_rate) const; 222 : void BuildDropStats(const vr_drop_stats_req &r, 223 : AgentDropStats &ds) const; 224 : friend class AgentStatsCollectorTest; 225 : 226 : private: 227 : void VrfNotify(DBTablePartBase *partition, DBEntryBase *e); 228 : void InterfaceNotify(DBTablePartBase *part, DBEntryBase *e); 229 : void AddNamelessVrfStatsEntry(); 230 : void AddInterfaceStatsEntry(const Interface *intf); 231 : void DelInterfaceStatsEntry(const Interface *intf); 232 : void AddUpdateVrfStatsEntry(const VrfEntry *intf); 233 : void DelVrfStatsEntry(const VrfEntry *intf); 234 : void AddFlow(const FlowUveStatsRequest *req); 235 : void DeleteFlow(const FlowUveStatsRequest *req); 236 : bool FlowStatsUpdate(); 237 : 238 : VrfIdToVrfStatsTree vrf_stats_tree_; 239 : InterfaceStatsTree if_stats_tree_; 240 : FlowAceTree flow_ace_tree_; 241 : vr_drop_stats_req drop_stats_; 242 : DBTableBase::ListenerId vrf_listener_id_; 243 : DBTableBase::ListenerId intf_listener_id_; 244 : Agent *agent_; 245 : WorkQueue<boost::shared_ptr<FlowUveStatsRequest> > request_queue_; 246 : Timer *timer_; 247 : DISALLOW_COPY_AND_ASSIGN(StatsManager); 248 : }; 249 : #endif // _ROOT_STATS_MANAGER_H_