OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vn_uve_table_base.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 #include <uve/agent_uve_base.h>
7 
8 VnUveTableBase::VnUveTableBase(Agent *agent, uint32_t default_intvl)
9  : uve_vn_map_(), agent_(agent), uve_vn_map_mutex_(),
10  vn_listener_id_(DBTableBase::kInvalidId),
11  intf_listener_id_(DBTableBase::kInvalidId),
12  timer_last_visited_(""),
13  timer_(TimerManager::CreateTimer
14  (*(agent->event_manager())->io_service(),
15  "VnUveTimer",
16  TaskScheduler::GetInstance()->GetTaskId(kTaskDBExclude), 0)) {
17  expiry_time_ = default_intvl;
19  boost::bind(&VnUveTableBase::TimerExpiry, this));
20 }
21 
23 }
24 
26  VnTable *vn_table = agent_->vn_table();
27  vn_listener_id_ = vn_table->Register
28  (boost::bind(&VnUveTableBase::VnNotify, this, _1, _2));
29 
30  InterfaceTable *intf_table = agent_->interface_table();
31  intf_listener_id_ = intf_table->Register
32  (boost::bind(&VnUveTableBase::InterfaceNotify, this, _1, _2));
35 }
36 
42  if (timer_) {
43  timer_->Cancel();
45  timer_ = NULL;
46  }
47 }
48 
50  UveVnMap::iterator it = uve_vn_map_.lower_bound(timer_last_visited_);
51  if (it == uve_vn_map_.end()) {
53  return true;
54  }
55 
56  uint32_t count = 0;
57  while (it != uve_vn_map_.end() && count < AgentUveBase::kUveCountPerTimer) {
58  VnUveEntryBase *entry = it->second.get();
59  UveVnMap::iterator prev = it;
60  it++;
61  count++;
62 
63  if (entry->deleted()) {
64  SendDeleteVnMsg(prev->first);
65  if (!entry->renewed()) {
66  Delete(prev->first);
67  } else {
68  entry->set_deleted(false);
69  entry->set_renewed(false);
70  entry->set_changed(false);
71  SendVnMsg(entry, entry->vn());
72  // Send VN ACE stats
73  SendVnAceStats(entry, entry->vn());
74  }
75  } else {
76  if (entry->changed()) {
77  SendVnMsg(entry, entry->vn());
78  entry->set_changed(false);
79  /* Clear renew flag to be on safer side. Not really required */
80  entry->set_renewed(false);
81  }
82 
83  // Send VN ACE stats
84  SendVnAceStats(entry, entry->vn());
85  }
86  }
87 
88  if (it == uve_vn_map_.end()) {
91  } else {
92  timer_last_visited_ = it->first;
94  }
95  return true;
96 }
97 
99  if (time != expiry_time_) {
100  expiry_time_ = time;
102  }
103 }
104 
106  UveVirtualNetworkAgent uve;
107  if (vn == NULL) {
108  return;
109  }
110  if (entry->FrameVnMsg(vn, uve)) {
111  DispatchVnMsg(uve);
112  }
113 }
114 
115 void VnUveTableBase::DispatchVnMsg(const UveVirtualNetworkAgent &uve) {
116  UveVirtualNetworkAgentTrace::Send(uve);
117 }
118 
120  if (vn->GetName() == agent_->NullString()) {
121  return NULL;
122  }
123 
124  UveVnMap::iterator it = uve_vn_map_.find(vn->GetName());
125  if (it == uve_vn_map_.end()) {
126  return NULL;
127  }
128  return it->second.get();
129 }
130 
132  VnUveEntryBase* entry = UveEntryFromVn(vn);
133  if (entry == NULL) {
134  return;
135  }
136 
137  entry->set_changed(true);
138  return;
139 }
140 
141 void VnUveTableBase::SendDeleteVnMsg(const string &vn) {
142  UveVirtualNetworkAgent s_vn;
143  s_vn.set_name(vn);
144  s_vn.set_deleted(true);
145  DispatchVnMsg(s_vn);
146 }
147 
148 void VnUveTableBase::Delete(const std::string &name) {
149  UveVnMap::iterator it = uve_vn_map_.find(name);
150  if (it != uve_vn_map_.end()) {
151  tbb::mutex::scoped_lock lock(uve_vn_map_mutex_);
152  uve_vn_map_.erase(it);
153  }
154 }
155 
157  VnUveEntryPtr uve = Allocate(vn);
158  pair<UveVnMap::iterator, bool> ret;
159  ret = uve_vn_map_.insert(UveVnPair(vn->GetName(), uve));
160  UveVnMap::iterator it = ret.first;
161  VnUveEntryBase* entry = it->second.get();
162  entry->set_vn(vn);
163  if (entry->deleted()) {
164  entry->set_renewed(true);
165  }
166 
167  return entry;
168 }
169 
170 void VnUveTableBase::Add(const string &vn) {
171  VnUveEntryPtr uve = Allocate();
172  uve_vn_map_.insert(UveVnPair(vn, uve));
173 }
174 
176  VnUveEntryPtr uve(new VnUveEntryBase(agent_, vn));
177  return uve;
178 }
179 
182  return uve;
183 }
184 
186  const VnEntry *vn = static_cast<const VnEntry *>(e);
187 
188  DBState *state = static_cast<DBState *>
189  (e->GetState(partition->parent(), vn_listener_id_));
190 
191  if (e->IsDeleted()) {
192  if (state) {
193  VnUveEntryBase *uve = UveEntryFromVn(vn);
194  if (uve) {
195  /* The Reset API sets 'deleted' flag and resets 'renewed' and
196  * 'add_by_vn_notify' flags */
197  uve->Reset();
198  }
199 
200  e->ClearState(partition->parent(), vn_listener_id_);
201  delete state;
202  }
203  return;
204  }
205 
206  if (!state) {
207  state = new DBState();
208  e->SetState(partition->parent(), vn_listener_id_, state);
209 
210  VnUveEntryBase* entry = Add(vn);
211  entry->set_add_by_vn_notify(true);
212  }
213  MarkChanged(vn);
214 }
215 
216 void VnUveTableBase::InterfaceDeleteHandler(const string &vm, const string &vn,
217  const Interface* intf) {
218  if (vn == agent_->NullString()) {
219  return;
220  }
221 
222  UveVnMap::iterator it = uve_vn_map_.find(vn);
223  if (it == uve_vn_map_.end()) {
224  return;
225  }
226 
227  VnUveEntryPtr vn_uve_entry_ptr(it->second);
228  VnUveEntryBase *vn_uve_entry = vn_uve_entry_ptr.get();
229  UveVirtualNetworkAgent uve;
230 
231  vn_uve_entry->VmDelete(vm);
232  vn_uve_entry->InterfaceDelete(intf);
233  vn_uve_entry->set_changed(true);
234  return;
235 }
236 
238  const Interface* intf,
239  const string &vm_name,
240  VnUveInterfaceState *state) {
241  VnUveEntryBase *vn_uve_entry;
242  vn_uve_entry = Add(vn);
243 
244  UveVirtualNetworkAgent uve;
245 
246  if (vm_name != state->vm_name_) {
247  if (state->vm_name_.length()) {
248  vn_uve_entry->VmDelete(state->vm_name_);
249  }
250  }
251  if (vm_name.length()) {
252  vn_uve_entry->VmAdd(vm_name);
253  }
254  vn_uve_entry->InterfaceAdd(intf);
255  vn_uve_entry->set_changed(true);
256  return;
257 }
258 
260  const VmInterface *vm_port = dynamic_cast<const VmInterface*>(e);
261  if (vm_port == NULL) {
262  return;
263  }
264 
265  VnUveInterfaceState *state = static_cast<VnUveInterfaceState *>
266  (e->GetState(partition->parent(), intf_listener_id_));
267  if (e->IsDeleted() || (vm_port->vn() == NULL)) {
268  if (state) {
270  vm_port);
271  e->ClearState(partition->parent(), intf_listener_id_);
272  delete state;
273  }
274  } else {
275  const VnEntry *vn = vm_port->vn();
276  const VmEntry *vm = vm_port->vm();
277  std::string vm_name = vm? vm->GetCfgName() : agent_->NullString();
278 
279  if (!state) {
280  state = new VnUveInterfaceState(vm_name, vn->GetName());
281  e->SetState(partition->parent(), intf_listener_id_, state);
282  InterfaceAddHandler(vn, vm_port, vm_name, state);
283  } else {
284  if (state->vn_name_.compare(vn->GetName()) != 0) {
286  vm_port);
287  state->vm_name_ = vm_name;
288  state->vn_name_ = vn->GetName();
289  InterfaceAddHandler(vn, vm_port, vm_name, state);
290  return;
291  }
292  if (state->vm_name_.compare(vm_name) != 0) {
293  InterfaceAddHandler(vn, vm_port, vm_name, state);
294  state->vm_name_ = vm_name;
295  }
296  }
297  }
298  return;
299 }
300 
302  UveVnMap::const_iterator it = uve_vn_map_.begin();
303  while (it != uve_vn_map_.end()) {
304  VnUveEntryBase *entry = it->second.get();
305  ++it;
306  if (entry->deleted()) {
307  continue;
308  }
309  if (entry->vn()) {
310  UveVirtualNetworkAgent uve;
311  bool send = entry->FrameVnAclRuleCountMsg(entry->vn(), &uve);
312  if (send) {
313  DispatchVnMsg(uve);
314  }
315  }
316  }
317 }
static const std::string LinkLocalVn()
Definition: flow_handler.h:32
bool deleted() const
DBTableBase::ListenerId vn_listener_id_
void set_vn(const VnEntry *vn)
void set_add_by_vn_notify(bool val)
uint32_t incremental_interval() const
void set_renewed(bool value)
const string & GetCfgName() const
Definition: vm.h:43
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:178
#define kTaskDBExclude
Definition: agent.h:336
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:37
uint32_t default_interval() const
void InterfaceDelete(const Interface *intf)
void VmAdd(const std::string &vm)
void SendDeleteVnMsg(const std::string &vn)
AgentUveBase * uve() const
Definition: agent.cc:909
void InterfaceAdd(const Interface *intf)
bool renewed() const
std::string timer_last_visited_
bool IsDeleted() const
Definition: db_entry.h:49
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
Definition: db_entry.cc:22
void set_changed(bool val)
Definition: vm.h:32
void VmDelete(const std::string &vm)
DBTableBase::ListenerId intf_listener_id_
DBTableBase * parent()
static const uint32_t kUveCountPerTimer
virtual void SendVnAceStats(VnUveEntryBase *entry, const VnEntry *vn)
InterfaceTable * interface_table() const
Definition: agent.h:465
VnTable * vn_table() const
Definition: agent.h:495
void set_deleted(bool value)
void MarkChanged(const VnEntry *vn)
void Delete(const std::string &name)
void Unregister(ListenerId listener)
Definition: db_table.cc:186
bool FrameVnAclRuleCountMsg(const VnEntry *vn, UveVirtualNetworkAgent *uve)
ListenerId Register(ChangeCallback callback, const std::string &name="unspecified")
Definition: db_table.cc:181
static const std::string UnknownVn()
Definition: flow_handler.h:26
VnUveEntryBase * Add(const VnEntry *vn)
void InterfaceDeleteHandler(const std::string &vm, const std::string &vn, const Interface *intf)
Definition: agent.h:358
const VnEntry * vn() const
virtual VnUveEntryPtr Allocate()
tbb::mutex uve_vn_map_mutex_
static const std::string & NullString()
Definition: agent.h:437
bool changed() const
const VnEntry * vn() const
void ClearState(DBTableBase *tbl_base, ListenerId listener)
Definition: db_entry.cc:73
virtual ~VnUveTableBase()
bool Cancel()
Definition: timer.cc:150
Definition: vn.h:151
Definition: vn.h:331
VnUveTableBase(Agent *agent, uint32_t default_intvl)
void VnNotify(DBTablePartBase *partition, DBEntryBase *e)
std::pair< std::string, VnUveEntryPtr > UveVnPair
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
Definition: timer.cc:108
static const int kInvalidId
Definition: db_table.h:64
void SendVnMsg(VnUveEntryBase *entry, const VnEntry *vn)
virtual void Reset()
const string & GetName() const
Definition: vn.h:162
VnUveEntryBase * UveEntryFromVn(const VnEntry *vn)
boost::shared_ptr< VnUveEntryBase > VnUveEntryPtr
void set_expiry_time(int time)
bool Reschedule(int time)
Definition: timer.cc:137
bool FrameVnMsg(const VnEntry *vn, UveVirtualNetworkAgent &uve)
virtual void DispatchVnMsg(const UveVirtualNetworkAgent &uve)
void InterfaceAddHandler(const VnEntry *vn, const Interface *intf, const std::string &vm_name, VnUveInterfaceState *state)
const VmEntry * vm() const
void InterfaceNotify(DBTablePartBase *partition, DBEntryBase *e)
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222