OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
flow_mgmt_tree.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef __AGENT_PKT_FLOW_MGMT_TREE_H__
6 #define __AGENT_PKT_FLOW_MGMT_TREE_H__
7 
8 #include <cstdlib>
9 #include <map>
11 
12 class FlowMgmtKeyNode;
13 class FlowMgmtEntry;
14 
16 
17 typedef std::map<FlowMgmtKey *, FlowMgmtKeyNode *, FlowMgmtKeyCmp> FlowMgmtKeyTree;
18 
19 class FlowMgmtTree {
20 public:
21  typedef std::map<FlowMgmtKey *, FlowMgmtEntry *, FlowMgmtKeyCmp> Tree;
23  virtual ~FlowMgmtTree() {
24  assert(tree_.size() == 0);
25  }
26 
27  // Add a flow into dependency tree for an object
28  // Creates an entry if not already present in the tree
29  virtual bool Add(FlowMgmtKey *key, FlowEntry *flow,
30  FlowMgmtKeyNode *node);
31  // Delete a flow from dependency tree for an object
32  // Entry is deleted after all flows dependent on the entry are deleted
33  // and DBEntry delete message is got from FlowTable
34  virtual bool Delete(FlowMgmtKey *key, FlowEntry *flow,
35  FlowMgmtKeyNode *node);
36  virtual void InsertEntry(FlowMgmtKey *key, FlowMgmtEntry *entry);
37  virtual void RemoveEntry(Tree::iterator it);
38 
39  // Handle DBEntry add
40  virtual bool OperEntryAdd(const FlowMgmtRequest *req, FlowMgmtKey *key);
41  // Handle DBEntry change
42  virtual bool OperEntryChange(const FlowMgmtRequest *req, FlowMgmtKey *key);
43  // Handle DBEntry delete
44  virtual bool OperEntryDelete(const FlowMgmtRequest *req, FlowMgmtKey *key);
45 
46  // Try delete a DBEntry
47  virtual bool RetryDelete(FlowMgmtKey *key);
48 
49  // Get all Keys relavent for the tree and store them into FlowMgmtKeyTree
50  virtual void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree) = 0;
51  // Allocate a FlowMgmtEntry for the tree
52  virtual FlowMgmtEntry *Allocate(const FlowMgmtKey *key) = 0;
53 
54  // Called just before entry is deleted. Used to implement cleanup operations
55  virtual void FreeNotify(FlowMgmtKey *key, uint32_t gen_id);
56 
60  Tree &tree() { return tree_; }
61  FlowMgmtManager *mgr() const { return mgr_; }
62  static bool AddFlowMgmtKey(FlowMgmtKeyTree *tree, FlowMgmtKey *key);
63 
64 protected:
65  bool TryDelete(FlowMgmtKey *key, FlowMgmtEntry *entry);
68 
69 private:
71 };
72 
74 // Object specific information below
76 class AclFlowMgmtTree : public FlowMgmtTree {
77 public:
79  virtual ~AclFlowMgmtTree() { }
80 
81  bool Add(FlowMgmtKey *key, FlowEntry *flow, FlowMgmtKey *old_key,
82  FlowMgmtKeyNode *node);
83  bool Delete(FlowMgmtKey *key, FlowEntry *flow,
84  FlowMgmtKeyNode *node);
86  const MatchAclParamsList *acl_list);
88  FlowMgmtEntry *Allocate(const FlowMgmtKey *key);
89 
90 private:
92 };
93 
94 class VnFlowMgmtTree : public FlowMgmtTree {
95 public:
97  virtual ~VnFlowMgmtTree() {}
98 
100  FlowMgmtEntry *Allocate(const FlowMgmtKey *key);
101 
102  void VnFlowCounters(const VnEntry *vn,
103  uint32_t *ingress_flow_count,
104  uint32_t *egress_flow_count);
105  void RemoveEntry(Tree::iterator it);
106  void InsertEntry(FlowMgmtKey *key, FlowMgmtEntry *entry);
107 
108 private:
109  // We need to support query of counters in VN from other threads.
110  // So, implement synchronization on access to VN Flow Tree
111  tbb::mutex mutex_;
113 };
114 
116 public:
119 
121  FlowMgmtEntry *Allocate(const FlowMgmtKey *key);
122  void InterfaceFlowCount(const Interface *itf, uint64_t *created,
123  uint64_t *aged, uint32_t *active_flows);
124  void InsertEntry(FlowMgmtKey *key, FlowMgmtEntry *entry);
125  void RemoveEntry(Tree::iterator it);
126 
127 private:
128  // We need to support query of counters in Interface from other threads.
129  // So, implement synchronization on access to Interface Flow Tree
130  tbb::mutex mutex_;
132 };
133 
134 class NhFlowMgmtTree : public FlowMgmtTree {
135 public:
137  virtual ~NhFlowMgmtTree() {}
138 
140  FlowMgmtEntry *Allocate(const FlowMgmtKey *key);
141 
142 private:
144 };
145 
147 public:
149  virtual ~RouteFlowMgmtTree() { }
150  virtual bool HasVrfFlows(uint32_t vrf_id, Agent::RouteTableType type) = 0;
151 
152  virtual bool Delete(FlowMgmtKey *key, FlowEntry *flow, FlowMgmtKeyNode *node);
153  virtual bool OperEntryDelete(const FlowMgmtRequest *req, FlowMgmtKey *key);
154  virtual bool OperEntryAdd(const FlowMgmtRequest *req, FlowMgmtKey *key);
155 
156 private:
157  void SetDBEntry(const FlowMgmtRequest *req, FlowMgmtKey *key);
159 };
160 
162 public:
165 
168 
169  void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree, uint32_t vrf,
170  const IpAddress &ip, uint8_t plen);
172  const IpAddress &ip, const FlowRouteRefMap *rt_list);
174 
175  virtual bool OperEntryAdd(const FlowMgmtRequest *req, FlowMgmtKey *key);
176  virtual bool OperEntryDelete(const FlowMgmtRequest *req, FlowMgmtKey *key);
177  FlowMgmtEntry *Allocate(const FlowMgmtKey *key);
178  bool HasVrfFlows(uint32_t vrf_id, Agent::RouteTableType type);
179 
181  if (key->plen_ == 0)
182  return NULL;
183  return lpm_tree_.LPMFind(key);
184  }
185 
187  InetRouteFlowMgmtKey *rt_key =
188  static_cast<InetRouteFlowMgmtKey *>(key->Clone());
189  if (lpm_tree_.Insert(rt_key) == false)
190  delete rt_key;
191  }
192 
194  InetRouteFlowMgmtKey *rt_key = lpm_tree_.Find(key);
195  if (rt_key != NULL) {
196  lpm_tree_.Remove(rt_key);
197  delete rt_key;
198  }
199  }
200  bool RecomputeCoveringRoute(InetRouteFlowMgmtKey *covering_route,
201  InetRouteFlowMgmtKey *key);
202  bool RouteNHChangeEvent(const FlowMgmtRequest *req, FlowMgmtKey *key);
203 
204 private:
207 };
208 
210 public:
214  FlowMgmtEntry *Allocate(const FlowMgmtKey *key);
215  bool HasVrfFlows(uint32_t vrf_id, Agent::RouteTableType type);
216 
217 private:
219 };
220 
222 // Flow Management tree for VRF. VRF tree does not follow the normal pattern
223 // for other DBEntries.
224 //
225 // VRF flow management implements following functions,
226 // 1. Generate event to delete VRF when all flows for a VRF are deleted
227 // 2. Implement lifetime reference to Route Table for INET/Bridge DBTables
228 // The route-table must be present till all flows relavent for the flow are
229 // present.
230 // FlowLifetimeRef implements Lifetime actor on the route-table till all
231 // flows for the route-table are deleted
232 //
233 // When a flow-add is got, we dont really check for presence of VRF or not.
234 // When adding a flow, FlowTable must ensure that VRF is valid at that time
235 //
236 // FlowTable module on the other hand, ensures that flow is not added on a
237 // non-existing or a deleted VRF
238 //
239 // The routes used in flow-entry refer to VRF by vrf-id (ex. RouteFlowRefMap).
240 // Hence, we dont have VRF pointer in all cases. Instead, we store vrf-id as
241 // key
244 public:
245  // Build local mapping of vrf-id to VrfEntry mapping.
246  // The mapping is already maintained in VrfTable. But, we cannot query it
247  // since we run in parallel to DB Task context
248  typedef std::map<uint32_t, const VrfEntry *> VrfIdMap;
250  virtual ~VrfFlowMgmtTree() { }
251 
252  virtual FlowMgmtEntry *Allocate(const FlowMgmtKey *key);
253  virtual bool OperEntryAdd(const FlowMgmtRequest *req, FlowMgmtKey *key);
254  virtual bool OperEntryDelete(const FlowMgmtRequest *req, FlowMgmtKey *key);
255  void DeleteDefaultRoute(const VrfEntry *vrf);
256  virtual void FreeNotify(FlowMgmtKey *key, uint32_t gen_id);
257  void RetryDelete(uint32_t vrf_id);
259 
260 private:
263 };
264 
266 public:
267  static const int kInvalidCnIndex = -1;
269  FlowMgmtTree(mgr), index_(index) {}
271 
273  FlowMgmtEntry *Allocate(const FlowMgmtKey *key);
278  const FlowMgmtRequest *req);
279  void DeleteAll();
280  //Gets CN index from flow.
281  static int GetCNIndex(const FlowEntry *flow);
282  // Called just before entry is deleted. Used to implement cleanup operations
283  virtual void FreeNotify(FlowMgmtKey *key, uint32_t gen_id);
284 
285 private:
286  int index_;
288 };
289 
290 #endif // __AGENT_PKT_FLOW_MGMT_TREE_H__
virtual bool OperEntryDelete(const FlowMgmtRequest *req, FlowMgmtKey *key)
tbb::mutex mutex_
virtual ~AclFlowMgmtTree()
NhFlowMgmtTree(FlowMgmtManager *mgr)
virtual ~InetRouteFlowMgmtTree()
bool BgpAsAServiceHealthCheckUpdate(Agent *agent, BgpAsAServiceFlowMgmtKey &key, BgpAsAServiceFlowMgmtRequest *req)
Definition: vrf.h:86
DISALLOW_COPY_AND_ASSIGN(BridgeRouteFlowMgmtTree)
DISALLOW_COPY_AND_ASSIGN(VrfFlowMgmtTree)
virtual FlowMgmtEntry * Allocate(const FlowMgmtKey *key)=0
FlowMgmtEntry * Allocate(const FlowMgmtKey *key)
void VnFlowCounters(const VnEntry *vn, uint32_t *ingress_flow_count, uint32_t *egress_flow_count)
void SetDBEntry(const FlowMgmtRequest *req, FlowMgmtKey *key)
FlowMgmtEntry * Locate(FlowMgmtKey *key)
DISALLOW_COPY_AND_ASSIGN(FlowMgmtTree)
FlowMgmtEntry * Find(FlowMgmtKey *key)
virtual bool HasVrfFlows(uint32_t vrf_id, Agent::RouteTableType type)=0
void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree, uint32_t vrf, const IpAddress &ip, uint8_t plen)
DISALLOW_COPY_AND_ASSIGN(NhFlowMgmtTree)
virtual ~FlowMgmtTree()
virtual ~InterfaceFlowMgmtTree()
boost::asio::ip::address IpAddress
Definition: address.h:13
virtual bool Delete(FlowMgmtKey *key, FlowEntry *flow, FlowMgmtKeyNode *node)
void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree)
DISALLOW_COPY_AND_ASSIGN(InetRouteFlowMgmtTree)
static int GetCNIndex(const FlowEntry *flow)
DISALLOW_COPY_AND_ASSIGN(AclFlowMgmtTree)
bool Add(FlowMgmtKey *key, FlowEntry *flow, FlowMgmtKey *old_key, FlowMgmtKeyNode *node)
virtual void FreeNotify(FlowMgmtKey *key, uint32_t gen_id)
AclFlowMgmtTree(FlowMgmtManager *mgr)
virtual FlowMgmtEntry * Allocate(const FlowMgmtKey *key)
void InsertEntry(FlowMgmtKey *key, FlowMgmtEntry *entry)
virtual bool Delete(FlowMgmtKey *key, FlowEntry *flow, FlowMgmtKeyNode *node)
bool HasVrfFlows(uint32_t vrf_id, Agent::RouteTableType type)
void RetryDelete(uint32_t vrf_id)
bool TryDelete(FlowMgmtKey *key, FlowMgmtEntry *entry)
bool RouteNHChangeEvent(const FlowMgmtRequest *req, FlowMgmtKey *key)
virtual void FreeNotify(FlowMgmtKey *key, uint32_t gen_id)
virtual void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree)=0
virtual bool OperEntryChange(const FlowMgmtRequest *req, FlowMgmtKey *key)
FlowMgmtEntry * Allocate(const FlowMgmtKey *key)
void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree)
bool HasVrfFlows(uint32_t vrf_id, Agent::RouteTableType type)
FlowMgmtEntry * Allocate(const FlowMgmtKey *key)
virtual ~BridgeRouteFlowMgmtTree()
FlowMgmtEntry * Allocate(const FlowMgmtKey *key)
Tree & tree()
InetRouteFlowMgmtTree(FlowMgmtManager *mgr)
uint8_t type
Definition: load_balance.h:109
static bool AddFlowMgmtKey(FlowMgmtKeyTree *tree, FlowMgmtKey *key)
Definition: agent.h:358
virtual void RemoveEntry(Tree::iterator it)
FlowMgmtEntry * Allocate(const FlowMgmtKey *key)
FlowMgmtKey * Clone()
virtual bool OperEntryDelete(const FlowMgmtRequest *req, FlowMgmtKey *key)
DISALLOW_COPY_AND_ASSIGN(BgpAsAServiceFlowMgmtTree)
void AddToLPMTree(InetRouteFlowMgmtKey *key)
void DeleteDefaultRoute(const VrfEntry *vrf)
map< int, int > FlowRouteRefMap
Definition: pkt_flow_info.h:16
virtual bool OperEntryDelete(const FlowMgmtRequest *req, FlowMgmtKey *key)
static const int kInvalidCnIndex
std::map< FlowMgmtKey *, FlowMgmtKeyNode *, FlowMgmtKeyCmp > FlowMgmtKeyTree
void RemoveEntry(Tree::iterator it)
bool BgpAsAServiceDelete(BgpAsAServiceFlowMgmtKey &key, const FlowMgmtRequest *req)
void InterfaceFlowCount(const Interface *itf, uint64_t *created, uint64_t *aged, uint32_t *active_flows)
virtual void InsertEntry(FlowMgmtKey *key, FlowMgmtEntry *entry)
D * LPMFind(const D *data)
Definition: patricia.h:107
FlowMgmtEntry * Allocate(const FlowMgmtKey *key)
VrfFlowMgmtTree(FlowMgmtManager *mgr)
virtual bool OperEntryAdd(const FlowMgmtRequest *req, FlowMgmtKey *key)
BgpAsAServiceFlowMgmtTree(FlowMgmtManager *mgr, int index)
Definition: vn.h:151
virtual bool OperEntryAdd(const FlowMgmtRequest *req, FlowMgmtKey *key)
FlowMgmtKey * LowerBound(FlowMgmtKey *key)
void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree)
RouteFlowMgmtTree(FlowMgmtManager *mgr)
Patricia::Tree< InetRouteFlowMgmtKey,&InetRouteFlowMgmtKey::node_, InetRouteFlowMgmtKey::KeyCmp > LpmTree
virtual ~NhFlowMgmtTree()
DISALLOW_COPY_AND_ASSIGN(VnFlowMgmtTree)
void RemoveEntry(Tree::iterator it)
virtual ~VnFlowMgmtTree()
InetRouteFlowMgmtKey * LPM(const InetRouteFlowMgmtKey *key)
void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree, const MatchAclParamsList *acl_list)
DISALLOW_COPY_AND_ASSIGN(RouteFlowMgmtTree)
void DelFromLPMTree(InetRouteFlowMgmtKey *key)
virtual bool Add(FlowMgmtKey *key, FlowEntry *flow, FlowMgmtKeyNode *node)
virtual ~VrfFlowMgmtTree()
void InsertEntry(FlowMgmtKey *key, FlowMgmtEntry *entry)
InterfaceFlowMgmtTree(FlowMgmtManager *mgr)
VnFlowMgmtTree(FlowMgmtManager *mgr)
void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree)
virtual void FreeNotify(FlowMgmtKey *key, uint32_t gen_id)
virtual bool RetryDelete(FlowMgmtKey *key)
std::list< MatchAclParams > MatchAclParamsList
Definition: flow_entry.h:220
FlowMgmtEntry * Allocate(const FlowMgmtKey *key)
virtual bool OperEntryAdd(const FlowMgmtRequest *req, FlowMgmtKey *key)
virtual ~RouteFlowMgmtTree()
std::map< uint32_t, const VrfEntry * > VrfIdMap
D * Find(const D *data)
Definition: patricia.h:99
void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree)
RouteTableType
Definition: agent.h:415
BridgeRouteFlowMgmtTree(FlowMgmtManager *mgr)
Patricia::Node node_
std::map< FlowMgmtKey *, FlowMgmtEntry *, FlowMgmtKeyCmp > Tree
DISALLOW_COPY_AND_ASSIGN(InterfaceFlowMgmtTree)
virtual bool OperEntryDelete(const FlowMgmtRequest *req, FlowMgmtKey *key)
FlowMgmtManager * mgr() const
FlowMgmtManager * mgr_
bool Delete(FlowMgmtKey *key, FlowEntry *flow, FlowMgmtKeyNode *node)
bool Remove(D *data)
Definition: patricia.h:95
virtual bool OperEntryAdd(const FlowMgmtRequest *req, FlowMgmtKey *key)
void ExtractKeys(FlowEntry *flow, FlowMgmtKeyTree *tree)
bool RecomputeCoveringRoute(InetRouteFlowMgmtKey *covering_route, InetRouteFlowMgmtKey *key)
bool Insert(D *data)
Definition: patricia.h:91
FlowMgmtTree(FlowMgmtManager *mgr)