OpenSDN source code
vm_uve_table.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 
7 #include <uve/vm_uve_table.h>
8 #include <uve/vm_uve_entry.h>
9 #include <uve/agent_uve.h>
10 #include <uve/vm_stat_kvm.h>
11 #include <uve/vm_stat_docker.h>
12 
13 VmUveTable::VmUveTable(Agent *agent, uint32_t default_intvl)
14  : VmUveTableBase(agent, default_intvl) {
16  (TaskScheduler::GetInstance()->GetTaskId("Agent::VmUve"), 0,
17  boost::bind(&VmUveTable::Process, this, _1)));
18  event_queue_->set_name("Virtual-Machine UVE");
19 }
20 
22 }
23 
24 void VmUveTable::UpdateBitmap(const VmEntry* vm, uint8_t proto,
25  uint16_t sport, uint16_t dport) {
26  std::scoped_lock lock(uve_vm_map_mutex_);
27  UveVmMap::iterator it = uve_vm_map_.find(vm->GetUuid());
28  if (it != uve_vm_map_.end()) {
29  VmUveEntry *entry = static_cast<VmUveEntry *>(it->second.get());
30  entry->UpdatePortBitmap(proto, sport, dport);
31  }
32 }
33 
35  VmUveEntryPtr uve(new VmUveEntry(agent_, vm->GetCfgName()));
36  return uve;
37 }
38 
40  VmUveEntry* entry = static_cast<VmUveEntry*>(UveEntryFromVm(u));
41  if (entry == NULL) {
42  return;
43  }
44  if (entry->deleted()) {
45  /* Skip entry marked for delete because the 'vm' pointer could be
46  * invalid */
47  return;
48  }
49  UveVirtualMachineAgent uve;
50 
51  bool send = entry->FrameVmStatsMsg(&uve);
52  if (send) {
53  DispatchVmMsg(uve);
54  }
55 }
56 
58  //Create object to poll for VM stats
59  VmStat *stat = NULL;
60  if (agent_->isKvmMode()) {
61  stat = new VmStatKvm(agent_, vm->GetUuid());
62  } else if (agent_->isDockerMode()) {
63  stat = new VmStatDocker(agent_, vm->GetUuid());
64  }
65 
66  if (stat) {
67  stat->Start();
68  state->stat_ = stat;
69  }
70 }
71 
73  if (state->stat_) {
74  state->stat_->Stop();
75  state->stat_ = NULL;
76  }
77 }
78 
80  event_queue_->Enqueue(data);
81 }
82 
83 bool VmUveTable::Process(VmStatData* vm_stat_data) {
84  if (vm_stat_data->vm_stat()->marked_delete()) {
85  delete vm_stat_data->vm_stat();
86  } else {
87  vm_stat_data->vm_stat()->ProcessData();
88  }
89  delete vm_stat_data;
90  return true;
91 }
92 
93 void VmUveTable::DispatchVmStatsMsg(const VirtualMachineStats &uve) {
94  VirtualMachineStatsTrace::Send(uve);
95 }
96 
98  UveVmMap::iterator it = uve_vm_map_.begin();
99  while (it != uve_vm_map_.end()) {
100  SendVmStatsMsg(it->first);
101  it++;
102  }
103 }
104 
105 void VmUveTable::SendVmDeleteMsg(const string &vm_config_name) {
106  VmUveTableBase::SendVmDeleteMsg(vm_config_name);
107 
108  VirtualMachineStats stats_uve;
109  stats_uve.set_name(vm_config_name);
110  stats_uve.set_deleted(true);
111  DispatchVmStatsMsg(stats_uve);
112 }
Definition: agent.h:361
bool isDockerMode()
Definition: agent.cc:150
bool isKvmMode()
Definition: agent.cc:146
static TaskScheduler * GetInstance()
Definition: task.cc:574
Definition: vm.h:35
const boost::uuids::uuid & GetUuid() const
Definition: vm.h:49
const string & GetCfgName() const
Definition: vm.h:46
VmStat * vm_stat() const
Definition: vm_stat_data.h:17
Definition: vm_stat.h:18
virtual void Start()
Definition: vm_stat.cc:212
void ProcessData()
Definition: vm_stat.cc:70
void Stop()
Definition: vm_stat.cc:215
bool marked_delete() const
Definition: vm_stat.h:27
bool deleted() const
bool FrameVmStatsMsg(UveVirtualMachineAgent *uve)
Definition: vm_uve_entry.cc:69
void UpdatePortBitmap(uint8_t proto, uint16_t sport, uint16_t dport)
Definition: vm_uve_entry.cc:19
boost::shared_ptr< VmUveEntryBase > VmUveEntryPtr
virtual void DispatchVmMsg(const UveVirtualMachineAgent &uve)
std::mutex uve_vm_map_mutex_
VmUveEntryBase * UveEntryFromVm(const boost::uuids::uuid &u)
virtual void SendVmDeleteMsg(const std::string &vm_config_name)
void SendVmStats(void)
Definition: vm_uve_table.cc:97
virtual void VmStatCollectionStop(VmUveVmState *state)
Definition: vm_uve_table.cc:72
VmUveTable(Agent *agent, uint32_t default_intvl)
Definition: vm_uve_table.cc:13
bool Process(VmStatData *vm_stat_data)
Definition: vm_uve_table.cc:83
void SendVmStatsMsg(const boost::uuids::uuid &u)
Definition: vm_uve_table.cc:39
void UpdateBitmap(const VmEntry *vm, uint8_t proto, uint16_t sport, uint16_t dport)
Definition: vm_uve_table.cc:24
virtual void DispatchVmStatsMsg(const VirtualMachineStats &uve)
Definition: vm_uve_table.cc:93
virtual VmUveEntryPtr Allocate(const VmEntry *vm)
Definition: vm_uve_table.cc:34
virtual ~VmUveTable()
Definition: vm_uve_table.cc:21
virtual void SendVmDeleteMsg(const std::string &vm_config_name)
virtual void VmStatCollectionStart(VmUveVmState *state, const VmEntry *vm)
Definition: vm_uve_table.cc:57
void EnqueueVmStatData(VmStatData *data)
Definition: vm_uve_table.cc:79
boost::scoped_ptr< WorkQueue< VmStatData * > > event_queue_
Definition: vm_uve_table.h:33
boost::uuids::uuid uuid