OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
agent_db.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef vnsw_agent_db_hpp
6 #define vnsw_agent_db_hpp
7 
8 #include <cmn/agent_cmn.h>
9 
10 class Agent;
11 class AgentDBEntry;
12 class AgentDBTable;
13 struct AgentDBState;
14 class AgentSandesh;
16 class AgentSandesh;
17 class ResourceManager;
18 typedef class boost::shared_ptr<AgentSandesh> AgentSandeshPtr;
19 
21 // Refcount class for AgentDBEntry
23 template <class Derived>
25 public:
26  friend void intrusive_ptr_add_ref(const Derived* p) {
27  const AgentRefCount *entry = (const AgentRefCount *) (p);
28  if (entry->refcount_.fetch_and_increment() == 0) {
29  p->SetRefState();
30  }
31  }
32 
33  friend void intrusive_ptr_release(const Derived* p) {
34  const AgentRefCount *entry = (const AgentRefCount *) (p);
35  if (entry->refcount_.fetch_and_decrement() == 1) {
36  p->ClearRefState();
37  }
38  }
39 
41  const Derived* p) {
42  const AgentRefCount *entry = (const AgentRefCount *) (p);
43  tbb::mutex::scoped_lock lock(entry->back_ref_mutex_);
44  entry->back_ref_set_.insert(ref);
45  }
46 
48  const Derived* p) {
49  const AgentRefCount *entry = (const AgentRefCount *) (p);
50  tbb::mutex::scoped_lock lock(entry->back_ref_mutex_);
51  entry->back_ref_set_.erase(ref);
52  }
53 
54  uint32_t GetRefCount() const {return refcount_;};
55 protected:
58  AgentRefCount& operator=(const AgentRefCount&) { return *this; }
59  virtual ~AgentRefCount() {assert(refcount_ == 0);};
60  void swap(AgentRefCount&) {};
61 
62  mutable tbb::mutex back_ref_mutex_;
63  mutable std::set<IntrusiveReferrer> back_ref_set_;
64 
65 private:
66  mutable tbb::atomic<uint32_t> refcount_;
67 };
68 
70 // DBState for AgentDBEntry
73  AgentDBState(const AgentDBEntry *entry) : DBState(), entry_(entry) { };
74  const AgentDBEntry *entry_;
75 };
76 
78 // VNSwitch DB Table Partition class
81 public:
83  DBTablePartition(parent, index) { };
84  virtual ~AgentDBTablePartition() {};
85  virtual void Add(DBEntry *entry);
86  virtual void Remove(DBEntryBase *entry);
87 
88 private:
90 };
91 
93 // VNSwitch DB Entry Key base class. Defines additional operations on DBEntry
95 struct AgentKey : public DBRequestKey {
96  typedef enum {
97  // Add/Delete/Change a entry
99  // Change an entry if its already present and not in deleted state
100  // Its a no-op if entry is not present or is in deleted state
102  } DBSubOperation;
103 
105  AgentKey(DBSubOperation sub_op) : DBRequestKey(), sub_op_(sub_op) { };
106  virtual ~AgentKey() { };
107 
108  uint8_t sub_op_;
109 };
110 
112 // VNSwitch DB Entry Data base class
114 struct AgentData : public DBRequestData {
116  virtual ~AgentData() { };
117 };
118 
120 // VNSwitch DB Entry base class. Supports
121 // 1. Reference counting with boost Intrusive pointer
123 class AgentDBEntry : public DBEntry {
124 public:
126  virtual ~AgentDBEntry() {};
127  virtual uint32_t GetRefCount() const = 0;
128 
129  typedef boost::intrusive_ptr<AgentDBEntry> AgentDBEntyRef;
130  void SetRefState() const;
131  void ClearRefState() const;
132  bool IsActive() const;
133  DBState *GetAgentDBEntryState(int listener_id);
134  const DBState *GetAgentDBEntryState(int listener_id) const;
135 
136  virtual void AllocateResources(ResourceManager *resource_manager);
137  virtual void FreeResources(ResourceManager *resource_manager);
138  virtual void PostAdd();
139  virtual bool DBEntrySandesh(Sandesh *resp, std::string &name) const = 0;
140 private:
141  friend class AgentDBTable;
143 };
144 
146 // VNSwitch DB Table base class
148 class AgentDBTable : public DBTable {
149 public:
150  static const int kPartitionCount = 1;
151  AgentDBTable(DB *db, const std::string &name);
152  AgentDBTable(DB *db, const std::string &name, bool del_on_zero_refcount);
153  virtual ~AgentDBTable();
154 
155  virtual int PartitionCount() const { return kPartitionCount; }
156  virtual void Input(DBTablePartition *root, DBClient *client,
157  DBRequest *req);
158  virtual DBEntry *CfgAdd(DBRequest *req) {return NULL;};
159  virtual bool Resync(DBEntry *entry, const DBRequest *req) {return false;};
160 
161  /*
162  * Clear all entries on a table. Requires the table to have no listeners.
163  * Used in process shutdown.
164  */
165  virtual void Clear();
166 
167  virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req,
168  const boost::uuids::uuid &uuid) {
169  assert(0);
170  return false;
171  }
172  virtual bool IFLinkToReq(IFMapLink *link, IFMapNode *node,
173  const std::string &peer_name, IFMapNode *peer,
174  DBRequest &req) {
175  assert(0);
176  return false;
177  }
178  virtual bool IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &id);
179 
180  virtual DBTablePartition *AllocPartition(int index) {
181  return new AgentDBTablePartition(this, index);
182  };
183  virtual void OnZeroRefcount(AgentDBEntry *e) {};
184  virtual void NotifyEntry(DBEntryBase *entry);
185 
187  const std::string &context) {
188  return AgentSandeshPtr();
189  }
190 
191  // Dummy notification
192  void Notify(DBTablePartBase *partition, DBEntryBase *entry) {
193  };
194 
196  AgentDBEntry *FindActiveEntry(const DBEntry *key);
200  AgentDBEntry *Find(const DBEntry *key, bool ret_del);
201  AgentDBEntry *Find(const DBRequestKey *key, bool ret_del);
202  virtual bool CanNotify(IFMapNode *dbe) {
203  return true;
204  }
205  virtual void Process(DBRequest &req);
206  virtual bool ProcessConfig(IFMapNode *node, DBRequest &req,
207  const boost::uuids::uuid &u) {
208  assert(0);
209  return false;
210  }
211 
213  Agent *agent() const { return agent_; }
214 
215  void Flush();
216  void reset_flush_walk_ref();
218 private:
219  AgentDBEntry *Find(const DBEntry *key);
220  AgentDBEntry *Find(const DBRequestKey *key);
221 
227 };
228 
229 #define OPER_TRACE(obj, ...)\
230 do {\
231  Oper##obj::TraceMsg(GetOperDBTraceBuf(), __FILE__, __LINE__, __VA_ARGS__);\
232 } while (false)
233 
234 #define OPER_TRACE_ENTRY(obj, table, ...)\
235 do {\
236  Oper##obj::TraceMsg(table->GetOperDBTraceBuf(),\
237  __FILE__, __LINE__, __VA_ARGS__);\
238 } while (false)
239 
240 #endif // vnsw_agent_db_hpp
void Notify(DBTablePartBase *partition, DBEntryBase *entry)
Definition: agent_db.h:192
DISALLOW_COPY_AND_ASSIGN(AgentDBEntry)
const AgentDBEntry * entry_
Definition: agent_db.h:73
boost::intrusive_ptr< AgentDBEntry > AgentDBEntyRef
Definition: agent_db.h:129
std::set< IntrusiveReferrer > back_ref_set_
Definition: agent_db.h:63
DBTableBase::ListenerId GetRefListenerId() const
Definition: agent_db.h:195
uint32_t GetRefCount() const
Definition: agent_db.h:54
tbb::atomic< uint32_t > refcount_
Definition: agent_db.h:66
virtual void Add(DBEntry *entry)
Definition: agent_db.cc:158
void Flush()
Definition: agent_db.cc:238
uint8_t sub_op_
Definition: agent_db.h:106
Agent * agent() const
Definition: agent_db.h:213
AgentRefCount & operator=(const AgentRefCount &)
Definition: agent_db.h:58
int ListenerId
Definition: db_table.h:62
virtual ~AgentDBTablePartition()
Definition: agent_db.h:84
DBTableBase * parent()
AgentDBEntry * FindActiveEntry(const DBEntry *key)
Definition: agent_db.cc:110
boost::uuids::uuid uuid
std::pair< void *, void * > IntrusiveReferrer
friend void intrusive_ptr_add_ref(const Derived *p)
Definition: agent_db.h:26
SandeshTraceBufferPtr OperDBTraceBuf
Definition: agent_db.h:224
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:18
AgentDBEntry * FindActiveEntryNoLock(const DBEntry *key)
Definition: agent_db.cc:102
virtual ~AgentData()
Definition: agent_db.h:116
friend void intrusive_ptr_del_back_ref(const IntrusiveReferrer ref, const Derived *p)
Definition: agent_db.h:47
Agent * agent_
Definition: agent_db.h:223
virtual uint32_t GetRefCount() const =0
AgentDBState(const AgentDBEntry *entry)
Definition: agent_db.h:73
DBState * GetAgentDBEntryState(int listener_id)
Definition: agent_db.cc:31
Definition: db.h:24
virtual void AllocateResources(ResourceManager *resource_manager)
Definition: agent_db.cc:43
virtual void FreeResources(ResourceManager *resource_manager)
Definition: agent_db.cc:46
virtual bool CanNotify(IFMapNode *dbe)
Definition: agent_db.h:202
AgentKey(DBSubOperation sub_op)
Definition: agent_db.h:105
Definition: agent.h:358
AgentData()
Definition: agent_db.h:115
virtual ~AgentDBEntry()
Definition: agent_db.h:126
DBTableBase::ListenerId ref_listener_id_
Definition: agent_db.h:222
AgentDBTable(DB *db, const std::string &name)
Definition: agent_db.cc:70
tbb::mutex back_ref_mutex_
Definition: agent_db.h:60
void ClearRefState() const
Definition: agent_db.cc:17
virtual void Clear()
Definition: agent_db.cc:215
virtual void PostAdd()
Definition: agent_db.cc:49
AgentRefCount(const AgentRefCount &)
Definition: agent_db.h:57
class boost::shared_ptr< AgentSandesh > AgentSandeshPtr
Definition: agent_db.h:18
const std::string & name() const
Definition: db_table.h:110
AgentDBEntry * Find(const DBEntry *key, bool ret_del)
Definition: agent_db.cc:134
virtual void Process(DBRequest &req)
Definition: agent_db.cc:231
virtual ~AgentDBTable()
Definition: agent_db.cc:87
virtual int PartitionCount() const
Definition: agent_db.h:155
virtual bool IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &id)
Definition: agent_db.cc:183
SandeshTraceBufferPtr GetOperDBTraceBuf() const
Definition: agent_db.h:217
virtual bool IFNodeToReq(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &uuid)
Definition: agent_db.h:167
void reset_flush_walk_ref()
Definition: agent_db.cc:98
virtual DBTablePartition * AllocPartition(int index)
Definition: agent_db.h:180
void SetRefState() const
Definition: agent_db.cc:9
void swap(AgentRefCount &)
Definition: agent_db.h:60
virtual DBEntry * CfgAdd(DBRequest *req)
Definition: agent_db.h:158
virtual ~AgentKey()
Definition: agent_db.h:106
virtual AgentSandeshPtr GetAgentSandesh(const AgentSandeshArguments *args, const std::string &context)
Definition: agent_db.h:186
DISALLOW_COPY_AND_ASSIGN(AgentDBTable)
AgentKey()
Definition: agent_db.h:104
virtual bool Resync(DBEntry *entry, const DBRequest *req)
Definition: agent_db.h:159
virtual void Remove(DBEntryBase *entry)
Definition: agent_db.cc:169
virtual bool DBEntrySandesh(Sandesh *resp, std::string &name) const =0
DBSubOperation
Definition: agent_db.h:96
friend void intrusive_ptr_release(const Derived *p)
Definition: agent_db.h:33
bool IsActive() const
Definition: agent_db.cc:27
virtual void NotifyEntry(DBEntryBase *entry)
Definition: agent_db.cc:91
static const int kPartitionCount
Definition: agent_db.h:150
friend void intrusive_ptr_add_back_ref(const IntrusiveReferrer ref, const Derived *p)
Definition: agent_db.h:40
virtual bool ProcessConfig(IFMapNode *node, DBRequest &req, const boost::uuids::uuid &u)
Definition: agent_db.h:206
virtual void OnZeroRefcount(AgentDBEntry *e)
Definition: agent_db.h:183
boost::intrusive_ptr< DBTableWalk > DBTableWalkRef
Definition: db_table.h:169
virtual bool IFLinkToReq(IFMapLink *link, IFMapNode *node, const std::string &peer_name, IFMapNode *peer, DBRequest &req)
Definition: agent_db.h:172
DBTable::DBTableWalkRef flush_walk_ref_
Definition: agent_db.h:225
AgentDBTablePartition(DBTable *parent, int index)
Definition: agent_db.h:82
virtual void Input(DBTablePartition *root, DBClient *client, DBRequest *req)
Definition: agent_db.cc:188
virtual ~AgentRefCount()
Definition: agent_db.h:59
void set_agent(Agent *agent)
Definition: agent_db.h:212
int index() const
DISALLOW_COPY_AND_ASSIGN(AgentDBTablePartition)