OpenSDN source code
vm_uve_table_base.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
8 #include <uve/agent_uve_base.h>
9 
10 using boost::uuids::nil_uuid;
11 
12 VmUveTableBase::VmUveTableBase(Agent *agent, uint32_t default_intvl)
13  : uve_vm_map_(), agent_(agent), uve_vm_map_mutex_(),
14  intf_listener_id_(DBTableBase::kInvalidId),
15  vm_listener_id_(DBTableBase::kInvalidId), timer_last_visited_(nil_uuid()),
16  timer_(TimerManager::CreateTimer
17  (*(agent->event_manager())->io_service(),
18  "VmUveTimer",
19  TaskScheduler::GetInstance()->GetTaskId(kTaskDBExclude), 0)) {
20  expiry_time_ = default_intvl;
22  boost::bind(&VmUveTableBase::TimerExpiry, this));
23 }
24 
26 }
27 
29  UveVmMap::iterator it = uve_vm_map_.lower_bound(timer_last_visited_);
30  if (it == uve_vm_map_.end()) {
31  timer_last_visited_ = nil_uuid();
32  return true;
33  }
34 
35  uint32_t count = 0;
36  while (it != uve_vm_map_.end() && count < AgentUveBase::kUveCountPerTimer) {
37  VmUveEntryBase* entry = it->second.get();
38  const boost::uuids::uuid u= it->first;
39  UveVmMap::iterator prev = it;
40  it++;
41  count++;
42 
43  if (entry->deleted()) {
45  if (!entry->renewed()) {
46  std::scoped_lock lock(uve_vm_map_mutex_);
47  uve_vm_map_.erase(prev);
48  } else {
49  entry->set_deleted(false);
50  entry->set_renewed(false);
51  entry->set_changed(false);
52  SendVmMsg(entry, u);
53  }
54  } else if (entry->changed()) {
55  SendVmMsg(entry, u);
56  entry->set_changed(false);
57  /* Clear renew flag to be on safer side. Not really required */
58  entry->set_renewed(false);
59  }
60  }
61 
62  if (it == uve_vm_map_.end()) {
63  timer_last_visited_ = nil_uuid();
65  } else {
66  timer_last_visited_ = it->first;
68  }
69  /* Return true to trigger auto-restart of timer */
70  return true;
71 }
72 
74  if (time != expiry_time_) {
75  expiry_time_ = time;
77  }
78 }
79 
80 void VmUveTableBase::SendVmDeleteMsg(const string &vm_config_name) {
81  UveVirtualMachineAgent uve;
82  uve.set_name(vm_config_name);
83  uve.set_deleted(true);
84  DispatchVmMsg(uve);
85 }
86 
87 VmUveEntryBase* VmUveTableBase::Add(const VmEntry *vm, bool vm_notify) {
88  VmUveEntryPtr uve = Allocate(vm);
89  pair<UveVmMap::iterator, bool> ret;
90  ret = uve_vm_map_.insert(UveVmPair(vm->GetUuid(), uve));
91  UveVmMap::iterator it = ret.first;
92  VmUveEntryBase* entry = it->second.get();
93  if (!entry->add_by_vm_notify()) {
94  entry->set_add_by_vm_notify(vm_notify);
95  }
96  if (entry->deleted()) {
97  entry->set_renewed(true);
98  }
99  return entry;
100 }
101 
103  UveVmMap::iterator it = uve_vm_map_.find(u);
104  if (it == uve_vm_map_.end()) {
105  return;
106  }
107  VmUveEntryBase* entry = it->second.get();
108  /* We need to reset all non-key fields to ensure that they have right
109  * values since the entry is getting re-used. Also update the 'deleted_'
110  * and 'renewed_' flags */
111  entry->Reset();
112  return;
113 }
114 
116  VmUveEntryBase* entry = UveEntryFromVm(vm->GetUuid());
117  if (entry == NULL) {
118  return;
119  }
120 
121  bool send = entry->Update(vm);
122  if (send) {
123  entry->set_changed(true);
124  }
125 }
126 
129  return uve;
130 }
131 
133  UveVmMap::iterator it = uve_vm_map_.find(u);
134  if (it == uve_vm_map_.end()) {
135  return NULL;
136  }
137  return it->second.get();
138 }
139 
140 void VmUveTableBase::DispatchVmMsg(const UveVirtualMachineAgent &uve) {
141  UveVirtualMachineAgentTrace::Send(uve);
142 }
143 
145  const boost::uuids::uuid &u) {
146  UveVirtualMachineAgent uve;
147  if (entry->FrameVmMsg(u, &uve)) {
148  DispatchVmMsg(uve);
149  }
150 }
151 
153  VmUveEntryBase* entry = UveEntryFromVm(u);
154  if (entry == NULL) {
155  return;
156  }
157  entry->set_changed(true);
158  return;
159 }
160 
162  const VmInterface *vmi) {
163  VmUveEntryBase *vm_uve_entry = Add(vm, false);
164 
165  vm_uve_entry->InterfaceAdd(vmi->cfg_name());
166  vm_uve_entry->set_vm_name(vmi->vm_name());
167  vm_uve_entry->set_changed(true);
168 }
169 
171  const string &intf_cfg_name) {
172  VmUveEntryBase* entry = UveEntryFromVm(u);
173  if (entry == NULL) {
174  return;
175  }
176 
177  entry->InterfaceDelete(intf_cfg_name);
178  entry->set_changed(true);
179 }
180 
182  const string &vm_name) {
183  VmUveEntryBase* entry = UveEntryFromVm(u);
184  if (entry == NULL) {
185  return;
186  }
187 
188  entry->set_vm_name(vm_name);
189  entry->set_changed(true);
190 }
191 
193  DBEntryBase *e) {
194  const VmInterface *vm_port = dynamic_cast<const VmInterface*>(e);
195  if (vm_port == NULL) {
196  return;
197  }
198 
199  VmUveInterfaceState *state = static_cast<VmUveInterfaceState *>
200  (e->GetState(partition->parent(), intf_listener_id_));
201  if (e->IsDeleted() || ((vm_port->vm() == NULL))) {
202  if (state) {
204  e->ClearState(partition->parent(), intf_listener_id_);
205  delete state;
206  }
207  } else {
208  const VmEntry *vm = vm_port->vm();
210 
211  if (!state) {
212  /* Skip Add notification if it does not have config name */
213  if (vm_port->cfg_name().empty()) {
214  return;
215  }
216  state = new VmUveInterfaceState(nil_uuid(), "");
217  e->SetState(partition->parent(), intf_listener_id_, state);
218  }
219  /* Handle Change of VM in a given VM interface */
220  if ((vm->GetUuid() != state->vm_uuid_) ||
221  (vm_port->cfg_name() != state->interface_cfg_name_)) {
222  //Handle disassociation of old VM from the VMI
223  if (state->vm_uuid_ != nil_uuid() &&
224  !state->interface_cfg_name_.empty()) {
226  state->interface_cfg_name_);
227  }
228  if (vm->GetUuid() != nil_uuid() &&
229  !vm_port->cfg_name().empty()) {
230  InterfaceAddHandler(vm, vm_port);
231  }
232  state->vm_uuid_ = vm->GetUuid();
233  state->interface_cfg_name_ = vm_port->cfg_name();
234  state->vm_name_ = vm_port->vm_name();
235  } else if (vm_port->vm_name() != state->vm_name_) {
236  UpdateVmName(state->vm_uuid_, vm_port->vm_name());
237  state->vm_name_ = vm_port->vm_name();
238  }
239  }
240 }
241 
243  const VmEntry *vm = static_cast<const VmEntry *>(e);
244 
245  VmUveVmState *state = static_cast<VmUveVmState *>
246  (e->GetState(partition->parent(), vm_listener_id_));
247 
248  if (e->IsDeleted()) {
249  if (state) {
250  Delete(vm->GetUuid());
251 
252  VmStatCollectionStop(state);
253 
254  e->ClearState(partition->parent(), vm_listener_id_);
255  delete state;
256  }
257  return;
258  }
259 
260  if (!state) {
261  state = new VmUveVmState();
262  e->SetState(partition->parent(), vm_listener_id_, state);
263 
264  Add(vm, true);
265 
266  VmStatCollectionStart(state, vm);
267  MarkChanged(vm->GetUuid());
268  } else {
269  Change(vm);
270  }
271 }
272 
274  const VmEntry *vm) {
275 }
276 
278 }
279 
281  InterfaceTable *intf_table = agent_->interface_table();
282  intf_listener_id_ = intf_table->Register
283  (boost::bind(&VmUveTableBase::InterfaceNotify, this, _1, _2));
284 
285  VmTable *vm_table = agent_->vm_table();
286  vm_listener_id_ = vm_table->Register
287  (boost::bind(&VmUveTableBase::VmNotify, this, _1, _2));
288 }
289 
295 
296  if (timer_) {
297  timer_->Cancel();
299  timer_ = NULL;
300  }
301 }
#define kTaskDBExclude
Definition: agent.h:339
static const uint32_t kUveCountPerTimer
uint32_t incremental_interval() const
uint32_t default_interval() const
Definition: agent.h:361
InterfaceTable * interface_table() const
Definition: agent.h:468
VmTable * vm_table() const
Definition: agent.h:493
AgentUveBase * uve() const
Definition: agent.cc:913
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:38
void ClearState(DBTableBase *tbl_base, ListenerId listener)
Definition: db_entry.cc:74
bool IsDeleted() const
Definition: db_entry.h:49
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
Definition: db_entry.cc:23
ListenerId Register(ChangeCallback callback, const std::string &name="unspecified")
Definition: db_table.cc:208
static const int kInvalidId
Definition: db_table.h:65
void Unregister(ListenerId listener)
Definition: db_table.cc:213
DBTableBase * parent()
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222
bool Cancel()
Definition: timer.cc:150
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
Definition: timer.cc:108
bool Reschedule(int time)
Definition: timer.cc:137
Definition: vm.h:35
const boost::uuids::uuid & GetUuid() const
Definition: vm.h:49
const string & GetCfgName() const
Definition: vm.h:46
std::set< FloatingIp, FloatingIp > FloatingIpSet
Definition: vm_interface.h:570
const std::string & cfg_name() const
const VmEntry * vm() const
const std::string & vm_name() const
Definition: vm.h:81
void set_changed(bool val)
bool renewed() const
void set_vm_name(const std::string name)
bool add_by_vm_notify() const
bool changed() const
void set_add_by_vm_notify(bool value)
void set_deleted(bool value)
void InterfaceAdd(const std::string &intf_cfg_name)
bool Update(const VmEntry *vm)
bool deleted() const
const std::string & vm_config_name() const
virtual void Reset()
void set_renewed(bool value)
bool FrameVmMsg(const boost::uuids::uuid &u, UveVirtualMachineAgent *uve)
void InterfaceDelete(const std::string &intf_cfg_name)
void InterfaceNotify(DBTablePartBase *partition, DBEntryBase *e)
boost::shared_ptr< VmUveEntryBase > VmUveEntryPtr
std::pair< const boost::uuids::uuid, VmUveEntryPtr > UveVmPair
void set_expiry_time(int time)
DBTableBase::ListenerId intf_listener_id_
virtual void VmStatCollectionStart(VmUveVmState *state, const VmEntry *vm)
virtual void VmStatCollectionStop(VmUveVmState *state)
virtual void DispatchVmMsg(const UveVirtualMachineAgent &uve)
void MarkChanged(const boost::uuids::uuid &u)
virtual VmUveEntryPtr Allocate(const VmEntry *vm)
VmUveTableBase(Agent *agent, uint32_t default_intvl)
void InterfaceDeleteHandler(const boost::uuids::uuid &u, const std::string &intf_cfg_name)
void Delete(const boost::uuids::uuid &u)
void SendVmMsg(VmUveEntryBase *entry, const boost::uuids::uuid &u)
DBTableBase::ListenerId vm_listener_id_
virtual ~VmUveTableBase()
void VmNotify(DBTablePartBase *partition, DBEntryBase *e)
void InterfaceAddHandler(const VmEntry *vm, const VmInterface *vmi)
VmUveEntryBase * Add(const VmEntry *vm, bool vm_notify)
std::mutex uve_vm_map_mutex_
VmUveEntryBase * UveEntryFromVm(const boost::uuids::uuid &u)
void Change(const VmEntry *vm)
virtual void SendVmDeleteMsg(const std::string &vm_config_name)
boost::uuids::uuid timer_last_visited_
void UpdateVmName(const boost::uuids::uuid &u, const std::string &vm_name)
boost::uuids::uuid uuid