OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
interface_uve_table.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 #include <oper/health_check.h>
7 #include <oper/tag.h>
9 #include <uve/agent_uve_base.h>
10 
11 InterfaceUveTable::InterfaceUveTable(Agent *agent, uint32_t default_intvl)
12  : agent_(agent), interface_tree_(), interface_tree_mutex_(),
13  intf_listener_id_(DBTableBase::kInvalidId),
14  timer_last_visited_(""),
15  timer_(TimerManager::CreateTimer
16  (*(agent->event_manager())->io_service(),
17  "InterfaceUveTimer",
18  TaskScheduler::GetInstance()->GetTaskId(kTaskDBExclude), 0)) {
19  expiry_time_ = default_intvl;
21  boost::bind(&InterfaceUveTable::TimerExpiry, this));
22 }
23 
25 }
26 
28  InterfaceMap::iterator it = interface_tree_.lower_bound(timer_last_visited_);
29  if (it == interface_tree_.end()) {
31  return true;
32  }
33 
34  uint32_t count = 0;
35  while (it != interface_tree_.end() &&
37  string cfg_name = it->first;
38  UveInterfaceEntry* entry = it->second.get();
39  InterfaceMap::iterator prev = it;
40  it++;
41  count++;
42 
43  if (entry->deleted_) {
44  SendInterfaceDeleteMsg(cfg_name);
45  if (!entry->renewed_) {
46  tbb::mutex::scoped_lock lock(interface_tree_mutex_);
47  interface_tree_.erase(prev);
48  } else {
49  entry->deleted_ = false;
50  entry->renewed_ = false;
51  entry->changed_ = false;
52  SendInterfaceMsg(cfg_name, entry);
53  // Send Interface ACE stats
54  SendInterfaceAceStats(cfg_name, entry);
55  }
56  } else {
57  if (entry->changed_) {
58  SendInterfaceMsg(cfg_name, entry);
59  entry->changed_ = false;
60  /* Clear renew flag to be on safer side. Not really required */
61  entry->renewed_ = false;
62  }
63  // Send Interface ACE stats
64  SendInterfaceAceStats(cfg_name, entry);
65  }
66  }
67 
68 
69  if (it == interface_tree_.end()) {
72  } else {
73  timer_last_visited_ = it->first;
75  }
76  /* Return true to trigger auto-restart of timer */
77  return true;
78 }
79 
81  if (time != expiry_time_) {
82  expiry_time_ = time;
84  }
85 }
86 
87 /* VM interfaces which are not created by Nova will not have VM name set.
88  * In that case pick VM name from VM object instead of VMI object */
90  const VmEntry *vm = intf_->vm();
91  if (!intf_->vm_name().empty()) {
92  return intf_->vm_name();
93  } else {
94  if (vm) {
95  return vm->GetCfgName();
96  }
97  }
98  return "";
99 }
100 
102  const {
103  const VmEntry *vm = intf_->vm();
104  const string &vm_name = GetVmName();
105  if (!vm_name.empty()) {
106  uve->set_vm_name(vm_name);
107  }
108  if (intf_->vn() != NULL) {
109  uve->set_virtual_network(intf_->vn()->GetName());
110  uve->set_vn_uuid(to_string(intf_->vn()->GetUuid()));
111  } else {
112  uve->set_virtual_network("");
113  }
114  if (vm) {
115  uve->set_vm_uuid(to_string(vm->GetUuid()));
116  } else {
117  uve->set_vm_uuid("");
118  }
119 }
120 
122  const {
123  const VmEntry *vm = intf_->vm();
124  const string &vm_name = GetVmName();
125  if (!vm_name.empty()) {
126  uve->set_vm_name(vm_name);
127  }
128  if (intf_->vn() != NULL) {
129  uve->set_virtual_network(intf_->vn()->GetName());
130  } else {
131  uve->set_virtual_network("");
132  }
133  if (vm) {
134  uve->set_vm_uuid(to_string(vm->GetUuid()));
135  } else {
136  uve->set_vm_uuid("");
137  }
138 }
139 
141  UveVMInterfaceAgent *s_intf) const {
142  s_intf->set_name(name);
143  SetVnVmInfo(s_intf);
144  s_intf->set_ip_address(intf_->primary_ip_addr().to_string());
145  s_intf->set_mac_address(intf_->vm_mac().ToString());
146  s_intf->set_ip6_address(intf_->primary_ip6_addr().to_string());
147  s_intf->set_ip6_active(intf_->ipv6_active());
148  s_intf->set_is_health_check_active(intf_->is_hc_active());
149  s_intf->set_tx_vlan(intf_->tx_vlan_id());
150  s_intf->set_rx_vlan(intf_->rx_vlan_id());
151  s_intf->set_vhostuser_mode(intf_->vhostuser_mode());
152  if (intf_->GetAnalyzer().empty()) {
153  s_intf->set_port_mirror_enabled(false);
154  } else {
155  s_intf->set_port_mirror_enabled(true);
156  }
157  const Interface *parent = intf_->parent();
158  if (parent) {
159  const VmInterface *p_vmi = dynamic_cast<const VmInterface*>(parent);
160  if (p_vmi) {
161  s_intf->set_parent_interface(p_vmi->cfg_name());
162  } else {
163  s_intf->set_parent_interface(parent->name());
164  }
165  }
166 
167  vector<VmFloatingIPAgent> uve_fip_list;
168  if (intf_->HasFloatingIp(Address::INET)) {
169  const VmInterface::FloatingIpList fip_list =
170  intf_->floating_ip_list();
171  VmInterface::FloatingIpSet::const_iterator it =
172  fip_list.list_.begin();
173  while(it != fip_list.list_.end()) {
174  const VmInterface::FloatingIp &ip = *it;
175  /* Don't export FIP entry if it is not installed. When FIP entry
176  * is not installed it will have NULL VN pointer. We can receive
177  * notifications for VM interface with un-installed FIP entries
178  * when the VM interface is not "L3 Active".
179  */
180  if (ip.Installed()) {
181  VmFloatingIPAgent uve_fip;
182  uve_fip.set_ip_address(ip.floating_ip_.to_string());
183  uve_fip.set_virtual_network(ip.vn_.get()->GetName());
184  uve_fip_list.push_back(uve_fip);
185  }
186  it++;
187  }
188  }
189  s_intf->set_floating_ips(uve_fip_list);
190 
191  vector<VmHealthCheckInstance> uve_hc_list;
193  intf_->hc_instance_set();
194  VmInterface::HealthCheckInstanceSet::const_iterator hc_it =
195  hc_list.begin();
196  while (hc_it != hc_list.end()) {
197  HealthCheckInstanceBase *inst = (*hc_it);
198  hc_it++;
199  HealthCheckService *svc = inst->service();
200  if (!svc) {
201  continue;
202  }
203  VmHealthCheckInstance uve_inst;
204  uve_inst.set_name(svc->name());
205  uve_inst.set_uuid(to_string(svc->uuid()));
206  uve_inst.set_status(inst->active() ? "Active" : "InActive");
207  uve_inst.set_is_running(inst->IsRunning());
208  uve_hc_list.push_back(uve_inst);
209  }
210  s_intf->set_health_check_instance_list(uve_hc_list);
211 
212  s_intf->set_label(intf_->label());
213  s_intf->set_ip4_active(intf_->ipv4_active());
214  s_intf->set_l2_active(intf_->l2_active());
215  s_intf->set_active(intf_->IsUveActive());
216  s_intf->set_admin_state(intf_->admin_state());
217 
218  s_intf->set_uuid(to_string(intf_->vmi_cfg_uuid()));
219  string gw;
220  if (GetVmInterfaceGateway(intf_, gw)) {
221  s_intf->set_gateway(gw);
222  }
223 
224  std::vector<std::string> fixed_ip4_list;
225  intf_->BuildIpStringList(Address::INET, &fixed_ip4_list);
226  s_intf->set_fixed_ip4_list(fixed_ip4_list);
227 
228  std::vector<std::string> fixed_ip6_list;
229  intf_->BuildIpStringList(Address::INET6, &fixed_ip6_list);
230  s_intf->set_fixed_ip6_list(fixed_ip6_list);
231  if (intf_->hbs_intf_type() == VmInterface::HBS_INTF_LEFT) {
232  s_intf->set_hbf_intf_type("left");
233  } else if (intf_->hbs_intf_type() == VmInterface::HBS_INTF_RIGHT) {
234  s_intf->set_hbf_intf_type("right");
235  } else if (intf_->hbs_intf_type() == VmInterface::HBS_INTF_MGMT) {
236  s_intf->set_hbf_intf_type("mgmt");
237  }
238  return true;
239 }
240 
242  intf_ = NULL;
243  port_bitmap_.Reset();
244  prev_fip_tree_.clear();
245  fip_tree_.clear();
246  ace_set_.clear();
247  security_policy_stats_map_.clear();
248 
249  ace_stats_changed_ = false;
250  deleted_ = true;
251  renewed_ = false;
252 }
253 
255  assert(intf_);
256  /* No action is required if tags are not added yet. Addition of tags is
257  * handled via FlowStats module notification */
258  if (local_tagset_.size() != 0) {
259  TagList new_tag_list;
260  intf_->CopyTagIdList(&new_tag_list);
261  if (local_tagset_ != new_tag_list) {
262  /* Do not update local_tagset_ with new values. This is handled
263  * as part of FlowStats module notification */
264  local_tagset_.clear();
265  }
266  }
267 }
268 
270  (uint8_t proto, uint16_t sport, uint16_t dport) {
271  tbb::mutex::scoped_lock lock(mutex_);
272  /* No need to update stats if the entry is marked for delete and not
273  * renewed */
274  if (deleted_ && !renewed_) {
275  return;
276  }
277  port_bitmap_.AddPort(proto, sport, dport);
278 }
279 
281  (const vector<VmFloatingIPStats> &list) const {
282  if (list != uve_stats_.get_fip_agg_stats()) {
283  return true;
284  }
285  return false;
286 }
287 
289  const VmInterface *vm_intf, string &gw) const {
290  const VnEntry *vn = vm_intf->vn();
291  if (vn == NULL) {
292  return false;
293  }
294  const vector<VnIpam> &list = vn->GetVnIpam();
295  Ip4Address vm_addr = vm_intf->primary_ip_addr();
296  unsigned int i;
297  for (i = 0; i < list.size(); i++) {
298  if (list[i].IsSubnetMember(vm_addr))
299  break;
300  }
301  if (i == list.size()) {
302  return false;
303  }
304  gw = list[i].default_gw.to_string();
305  return true;
306 }
307 
308 void InterfaceUveTable::SendInterfaceDeleteMsg(const string &config_name) {
309  UveVMInterfaceAgent uve;
310  uve.set_name(config_name);
311  uve.set_deleted(true);
313 
314  VMITags tags_uve;
315  tags_uve.set_name(config_name);
316  tags_uve.set_deleted(true);
317  DispatchVMITagsMsg(tags_uve);
318 
319  VMIStats s_uve;
320  s_uve.set_name(config_name);
321  s_uve.set_deleted(true);
322  DispatchVMIStatsMsg(s_uve);
323 }
324 
326 (Agent *agent, const string &name, VMITags *obj) {
327  bool changed = false;
328  AgentUveBase *uve = agent->uve();
329  obj->set_name(name);
330  TagList new_tag_list;
331  intf_->CopyTagIdList(&new_tag_list);
333  uve->BuildTagNamesFromList(new_tag_list, &tinfo);
334  if ((!prev_tags_uve_.__isset.app) ||
335  (prev_tags_uve_.__isset.app && (prev_tags_uve_.get_app() !=
336  tinfo.application))) {
337  obj->set_app(tinfo.application);
338  prev_tags_uve_.set_app(tinfo.application);
339  changed = true;
340  }
341  if ((!prev_tags_uve_.__isset.tier) ||
342  (prev_tags_uve_.__isset.tier && (prev_tags_uve_.get_tier() !=
343  tinfo.tier))) {
344  obj->set_tier(tinfo.tier);
345  prev_tags_uve_.set_tier(tinfo.tier);
346  changed = true;
347  }
348  if ((!prev_tags_uve_.__isset.site) ||
349  (prev_tags_uve_.__isset.site && (prev_tags_uve_.get_site() !=
350  tinfo.site))) {
351  obj->set_site(tinfo.site);
352  prev_tags_uve_.set_site(tinfo.site);
353  changed = true;
354  }
355  if ((!prev_tags_uve_.__isset.deployment) ||
356  (prev_tags_uve_.__isset.deployment &&
357  (prev_tags_uve_.get_deployment() != tinfo.deployment))) {
358  obj->set_deployment(tinfo.deployment);
359  prev_tags_uve_.set_deployment(tinfo.deployment);
360  changed = true;
361  }
362  if ((!prev_tags_uve_.__isset.labels) ||
363  (prev_tags_uve_.__isset.labels && (prev_tags_uve_.get_labels() !=
364  tinfo.label_vector))) {
365  obj->set_labels(tinfo.label_vector);
366  prev_tags_uve_.set_labels(tinfo.label_vector);
367  changed = true;
368  }
369  if ((!prev_tags_uve_.__isset.custom_tags) ||
370  (prev_tags_uve_.__isset.custom_tags &&
371  (prev_tags_uve_.get_custom_tags() != tinfo.custom_tag_vector))) {
372  obj->set_custom_tags(tinfo.custom_tag_vector);
373  prev_tags_uve_.set_custom_tags(tinfo.custom_tag_vector);
374  changed = true;
375  }
376  return changed;
377 }
378 
382  return uve;
383 }
384 
385 void InterfaceUveTable::DispatchInterfaceMsg(const UveVMInterfaceAgent &uve) {
386  UveVMInterfaceAgentTrace::Send(uve);
387 }
388 
389 void InterfaceUveTable::DispatchVMIStatsMsg(const VMIStats &uve) {
390  UVEVMIStats::Send(uve);
391 }
392 
393 void InterfaceUveTable::DispatchVMITagsMsg(const VMITags &uve) const {
394  UVEVMITags::Send(uve);
395 }
396 
397 void InterfaceUveTable::DispatchInterfaceObjectLog(EndpointSecurityStats *obj) {
398  ENDPOINT_SECURITY_STATS_SEND_SANDESH(obj);
399 }
400 
401 void InterfaceUveTable::SendInterfaceMsg(const string &name,
402  UveInterfaceEntry *entry) {
403  UveVMInterfaceAgent uve;
404  if (entry->FrameInterfaceMsg(name, &uve)) {
406  }
407  VMITags tags_uve;
408  if (entry->FrameTagsUveMsg(agent_, name, &tags_uve)) {
409  DispatchVMITagsMsg(tags_uve);
410  }
411 }
412 
414  const VmInterface::FloatingIpSet &old_list) {
415  UveInterfaceEntryPtr uve = Allocate(itf);
416  pair<InterfaceMap::iterator, bool> ret;
417  ret = interface_tree_.insert(InterfacePair(itf->cfg_name(), uve));
418  InterfaceMap::iterator it = ret.first;
419  UveInterfaceEntry* entry = it->second.get();
420  if (entry->deleted_) {
421  entry->renewed_ = true;
422  entry->intf_ = itf;
423  }
424 
425  /* Mark the entry as changed to account for change in any fields of VMI */
426  entry->changed_ = true;
427 
428  const VmInterface::FloatingIpSet &new_list = itf->floating_ip_list().list_;
429  /* Remove old entries, by checking entries which are present in old list,
430  * but not in new list */
431  VmInterface::FloatingIpSet::const_iterator old_it = old_list.begin();
432  while (old_it != old_list.end()) {
433  VmInterface::FloatingIp fip = *old_it;
434  ++old_it;
435  /* Skip entries which are not installed as they wouldn't have been
436  * added
437  */
438  if (!fip.Installed()) {
439  continue;
440  }
441  VmInterface::FloatingIpSet::const_iterator new_it = new_list.find(fip);
442  if (new_it == new_list.end()) {
443  entry->RemoveFloatingIp(fip);
444  }
445  }
446  /* Add entries in new list. Ignore if entries already present */
447  VmInterface::FloatingIpSet::const_iterator fip_it = new_list.begin();
448  while (fip_it != new_list.end()) {
449  VmInterface::FloatingIp fip = *fip_it;
450  ++fip_it;
451  /* Skip entries which are not installed as they wouldn't have been
452  * added
453  */
454  if (!fip.Installed()) {
455  continue;
456  }
457  entry->AddFloatingIp(fip);
458  }
459 }
460 
462  InterfaceMap::iterator it = interface_tree_.find(name);
463  if (it == interface_tree_.end()) {
464  return;
465  }
466  UveInterfaceEntry* entry = it->second.get();
467  tbb::mutex::scoped_lock lock(entry->mutex_);
468  /* We need to reset all non-key fields to ensure that they have right
469  * values since the entry is getting re-used. Also update the 'deleted_'
470  * and 'renewed_' flags */
471  entry->Reset();
472  return;
473 }
474 
476  InterfaceMap::iterator it = interface_tree_.find(name);
477  if (it == interface_tree_.end()) {
478  return;
479  }
480  UveInterfaceEntry* entry = it->second.get();
481  entry->HandleTagListChange();
482  return;
483 }
484 
486  DBEntryBase *e) {
487  const VmInterface *vm_port = dynamic_cast<const VmInterface*>(e);
488  if (vm_port == NULL) {
489  return;
490  }
491 
492  UveInterfaceState *state = static_cast<UveInterfaceState *>
493  (e->GetState(partition->parent(), intf_listener_id_));
494  if (e->IsDeleted()) {
495  if (state) {
497  state->fip_list_.clear();
498  e->ClearState(partition->parent(), intf_listener_id_);
499  delete state;
500  }
501  } else {
503  if (!state && vm_port->cfg_name().empty()) {
504  /* Skip Add/change notifications if the config_name is empty */
505  return;
506  }
507 
508  if (!state) {
509  state = new UveInterfaceState(vm_port);
510  e->SetState(partition->parent(), intf_listener_id_, state);
511  } else {
512  old_list = state->fip_list_;
513  state->fip_list_ = vm_port->floating_ip_list().list_;
514  }
515  if (state->cfg_name_ != vm_port->cfg_name()) {
517  state->cfg_name_ = vm_port->cfg_name();
518  old_list.clear();
519  }
520  if (!vm_port->cfg_name().empty()) {
521  InterfaceAddHandler(vm_port, old_list);
522  HandleVmiTagListChange(vm_port->cfg_name());
523  }
524  }
525 }
526 
528  InterfaceTable *intf_table = agent_->interface_table();
529  intf_listener_id_ = intf_table->Register
530  (boost::bind(&InterfaceUveTable::InterfaceNotify, this, _1, _2));
531 
532 }
533 
537 
538  if (timer_) {
539  timer_->Cancel();
541  timer_ = NULL;
542  }
543 }
544 
546  (const PortBucketBitmap &bmap) const {
547  if (!uve_stats_.__isset.port_bucket_bmap) {
548  return true;
549  }
550  if (bmap != uve_stats_.get_port_bucket_bmap()) {
551  return true;
552  }
553  return false;
554 }
555 
557  const {
558  if (!uve_stats_.__isset.in_bw_usage) {
559  return true;
560  }
561  if (in_band != uve_stats_.get_in_bw_usage()) {
562  return true;
563  }
564  return false;
565 }
566 
568  const {
569  if (!uve_stats_.__isset.out_bw_usage) {
570  return true;
571  }
572  if (out_band != uve_stats_.get_out_bw_usage()) {
573  return true;
574  }
575  return false;
576 }
577 
579  (const FipInfo &fip_info) {
580  tbb::mutex::scoped_lock lock(mutex_);
581  /* No need to update stats if the entry is marked for delete and not
582  * renewed */
583  if (deleted_ && !renewed_) {
584  return;
585  }
586  FloatingIp *entry = FipEntry(fip_info.fip_, fip_info.vn_);
587  /* Ignore stats update request if it comes after entry is removed */
588  if (entry == NULL) {
589  return;
590  }
591  entry->UpdateFloatingIpStats(fip_info);
592 }
593 
595  (uint32_t ip, const string &vn) {
596  Ip4Address addr(ip);
597  FloatingIpPtr key(new FloatingIp(addr, vn));
598  FloatingIpSet::iterator fip_it = fip_tree_.find(key);
599  if (fip_it == fip_tree_.end()) {
600  return NULL;
601  } else {
602  return (*fip_it).get();
603  }
604 }
605 
607  if (fip_info.is_local_flow_) {
608  if (fip_info.is_reverse_flow_) {
609  out_bytes_ += fip_info.bytes_;
610  out_packets_ += fip_info.packets_;
611 
612  if (fip_info.rev_fip_) {
613  /* This is the case where Source and Destination VMs (part of
614  * same compute node) ping to each other to their respective
615  * Floating IPs. In this case for each flow we need to increment
616  * stats for both the VMs */
617  fip_info.rev_fip_->out_bytes_ += fip_info.bytes_;
618  fip_info.rev_fip_->out_packets_ += fip_info.packets_;
619  }
620  } else {
621  in_bytes_ += fip_info.bytes_;
622  in_packets_ += fip_info.packets_;
623  if (fip_info.rev_fip_) {
624  /* This is the case where Source and Destination VMs (part of
625  * same compute node) ping to each other to their respective
626  * Floating IPs. In this case for each flow we need to increment
627  * stats for both the VMs */
628  fip_info.rev_fip_->in_bytes_ += fip_info.bytes_;
629  fip_info.rev_fip_->in_packets_ += fip_info.packets_;
630  }
631  }
632  } else {
633  if (fip_info.is_ingress_flow_) {
634  in_bytes_ += fip_info.bytes_;
635  in_packets_ += fip_info.packets_;
636  } else {
637  out_bytes_ += fip_info.bytes_;
638  out_packets_ += fip_info.packets_;
639  }
640  }
641 }
642 
643 
645  (vector<VmFloatingIPStats> &result,
646  vector<VmFloatingIPStats> &diff_list,
647  bool &diff_list_send) {
648  const VmInterface *vm_intf = static_cast<const VmInterface *>(intf_);
649  if (vm_intf->HasFloatingIp()) {
650  const VmInterface::FloatingIpList fip_list =
651  vm_intf->floating_ip_list();
652  VmInterface::FloatingIpSet::const_iterator it =
653  fip_list.list_.begin();
654  while(it != fip_list.list_.end()) {
655  const VmInterface::FloatingIp &ip = *it;
656  /* Skip FIP entries which are not yet activated */
657  if (!ip.Installed()) {
658  it++;
659  continue;
660  }
661  VmFloatingIPStats uve_fip;
662  VmFloatingIPStats diff_uve;
663  uve_fip.set_ip_address(ip.floating_ip_.to_string());
664  uve_fip.set_virtual_network(ip.vn_.get()->GetName());
665 
666  diff_uve.set_ip_address(ip.floating_ip_.to_string());
667  diff_uve.set_virtual_network(ip.vn_.get()->GetName());
668 
670  ip.vn_.get()->GetName()));
671  FloatingIpSet::iterator fip_it = fip_tree_.find(key);
672  if (fip_it == fip_tree_.end()) {
673  SetStats(uve_fip, 0, 0, 0, 0);
674  SetDiffStats(diff_uve, 0, 0, 0, 0, diff_list_send);
675  } else {
676  FloatingIp *fip = (*fip_it).get();
677  SetStats(uve_fip, fip->in_bytes_, fip->in_packets_,
678  fip->out_bytes_, fip->out_packets_);
679  FloatingIpSet::iterator prev_it = prev_fip_tree_.find(key);
680  if (prev_it == prev_fip_tree_.end()) {
681  SetDiffStats(diff_uve, fip->in_bytes_, fip->in_packets_,
682  fip->out_bytes_, fip->out_packets_,
683  diff_list_send);
684  FloatingIpPtr prev_fip_ptr(new FloatingIp(ip.floating_ip_,
685  ip.vn_.get()->GetName(),
686  fip->in_bytes_,
687  fip->in_packets_,
688  fip->out_bytes_,
689  fip->out_packets_));
690  prev_fip_tree_.insert(prev_fip_ptr);
691  } else {
692  FloatingIp *pfip = (*prev_it).get();
693  SetDiffStats(diff_uve, (fip->in_bytes_ - pfip->in_bytes_),
694  (fip->in_packets_ - pfip->in_packets_),
695  (fip->out_bytes_ - pfip->out_bytes_),
696  (fip->out_packets_ - pfip->out_packets_),
697  diff_list_send);
698  pfip->in_bytes_ = fip->in_bytes_;
699  pfip->in_packets_ = fip->in_packets_;
700  pfip->out_bytes_ = fip->out_bytes_;
701  pfip->out_packets_ = fip->out_packets_;
702  }
703 
704  }
705  result.push_back(uve_fip);
706  diff_list.push_back(diff_uve);
707  it++;
708  }
709  return true;
710  }
711  return false;
712 }
713 
715  (VmFloatingIPStats &fip, uint64_t in_bytes, uint64_t in_pkts,
716  uint64_t out_bytes, uint64_t out_pkts, bool &diff_list_send) const {
717  fip.set_in_bytes(in_bytes);
718  fip.set_in_pkts(in_pkts);
719  fip.set_out_bytes(out_bytes);
720  fip.set_out_pkts(out_pkts);
721  if ((in_bytes != 0) || (in_pkts != 0) || (out_bytes != 0) ||
722  (out_pkts != 0)) {
723  diff_list_send = true;
724  }
725 }
726 
728  (VmFloatingIPStats &fip, uint64_t in_bytes, uint64_t in_pkts,
729  uint64_t out_bytes, uint64_t out_pkts) const {
730  fip.set_in_bytes(in_bytes);
731  fip.set_in_pkts(in_pkts);
732  fip.set_out_bytes(out_bytes);
733  fip.set_out_pkts(out_pkts);
734 }
735 
737  (const VmInterface::FloatingIp &fip) {
739  fip.vn_.get()->GetName()));
740  FloatingIpSet::iterator it = fip_tree_.find(key);
741  if (it != fip_tree_.end()) {
742  return;
743  }
744  fip_tree_.insert(key);
745 }
746 
748  (const VmInterface::FloatingIp &fip) {
749  FloatingIpPtr key(new FloatingIp(fip.floating_ip_, fip.vn_.get()->GetName()));
750  FloatingIpSet::iterator it = fip_tree_.find(key);
751  if (it != fip_tree_.end()) {
752  fip_tree_.erase(it);
753  }
754  FloatingIpSet::iterator prev_it = prev_fip_tree_.find(key);
755  if (prev_it != prev_fip_tree_.end()) {
756  prev_fip_tree_.erase(prev_it);
757  }
758 }
759 
761  (const std::string &ace_uuid) {
762  AceStats key(ace_uuid);
763  ace_stats_changed_ = true;
764  AceStatsSet::const_iterator it = ace_set_.find(key);
765  if (it != ace_set_.end()) {
766  it->count++;
767  return;
768  }
769  key.count = 1;
770  ace_set_.insert(key);
771 }
772 
775  if (info.added_) {
776  ++obj->added;
777  } else {
778  ++obj->deleted;
779  }
780  if (obj->added >= obj->deleted) {
781  obj->active = obj->added - obj->deleted;
782  }
783 }
784 
786  (const FlowUveFwPolicyInfo &info) {
787  ace_stats_changed_ = true;
788  tbb::mutex::scoped_lock lock(mutex_);
789  /* If TagList of VMI has changed clear the earlier statistics as they are
790  * valid only for previous TagList.
791  */
792  if (info.added_) {
793  local_tagset_ = info.local_tagset_;
794  }
795  SecurityPolicyStatsMap::iterator it = security_policy_stats_map_.
796  find(info.fw_policy_);
798  (info.local_tagset_,
799  info.remote_tagset_,
800  info.remote_prefix_, info.remote_vn_,
801  info.local_vn_, info.action_));
802  if (it != security_policy_stats_map_.end()) {
803  EndpointStatsContainer &cont = it->second;
805  cont.ToList(info.initiator_);
806  SecurityPolicyStatsSet::iterator ep_it = remote_ep_list.find(ep_key);
807  if (ep_it != remote_ep_list.end()) {
808  UveSecurityPolicyStatsPtr entry = *ep_it;
809  UpdateCounters(info, entry.get());
810  return;
811  } else {
812  /* Ignore delete counter increment request when the entry itself is
813  * absent
814  */
815  if (!info.added_) {
816  return;
817  }
818  UpdateCounters(info, ep_key.get());
819  remote_ep_list.insert(ep_key);
820  }
821  } else {
822  UpdateCounters(info, ep_key.get());
824  SecurityPolicyStatsSet &remote_ep_list = cont.ToList(info.initiator_);
825  remote_ep_list.insert(ep_key);
826  security_policy_stats_map_.insert(SecurityPolicyStatsPair
827  (info.fw_policy_, cont));
828  }
829 }
830 
832  (const std::string &name, VMIStats *s_intf) {
833  if (!ace_stats_changed_) {
834  return false;
835  }
836  std::vector<SgAclRuleStats> list;
837  AceStatsSet::iterator it = ace_set_.begin();
838  bool changed = false;
839  /* Build the updated list */
840  while (it != ace_set_.end()) {
841  uint64_t diff_count = it->count - it->prev_count;
842  if (diff_count) {
843  //Update prev_count
844  it->prev_count = it->count;
845  SgAclRuleStats item;
846  item.set_rule(it->ace_uuid);
847  item.set_count(diff_count);
848  list.push_back(item);
849  changed = true;
850  }
851  ++it;
852  }
853  /* If all the entries in the list has 0 diff_stats, then UVE won't be
854  * sent */
855  if (changed) {
856  s_intf->set_name(name);
857  SetVMIStatsVnVm(s_intf);
858  s_intf->set_sg_rule_stats(list);
859  ace_stats_changed_ = false;
860  return true;
861  }
862  return false;
863 }
864 
866  (const EndpointStatsInfo &info) {
867  tbb::mutex::scoped_lock lock(mutex_);
868  local_tagset_ = info.local_tagset;
870  (info.local_tagset, info.remote_tagset,
871  info.remote_prefix, info.remote_vn,
872  info.local_vn, info.action));
873  SecurityPolicyStatsMap::iterator it =
874  security_policy_stats_map_.find(info.policy);
875  if (it == security_policy_stats_map_.end()) {
876  UpdateSecurityPolicyStatsInternal(info, stats.get());
878  SecurityPolicyStatsSet &stats_set = cont.ToList(info.client);
879  stats_set.insert(stats);
880  security_policy_stats_map_.insert(SecurityPolicyStatsPair(info.policy,
881  cont));
882  } else {
883  EndpointStatsContainer &cont = it->second;
884  SecurityPolicyStatsSet &stats_set = cont.ToList(info.client);
885  std::pair<SecurityPolicyStatsSet::iterator,bool> ret =
886  stats_set.insert(stats);
887  UveSecurityPolicyStatsPtr entry(*ret.first);
888  /* Update the statistics and action for the entry */
889  UpdateSecurityPolicyStatsInternal(info, entry.get());
890  }
891 }
892 
893 
896  stats->action = info.action;
897  if (info.in_stats) {
898  stats->in_bytes += info.diff_bytes;
899  stats->in_pkts += info.diff_pkts;
900  } else {
901  stats->out_bytes += info.diff_bytes;
902  stats->out_pkts += info.diff_pkts;
903  }
904 }
905 
907  (Agent *agent, EndpointSecurityStats *obj) {
908  std::map<std::string, EndpointStats> eps;
909 
910  obj->set_name(intf_->cfg_name());
911  SecurityPolicyStatsMap::const_iterator it =
912  security_policy_stats_map_.begin();
913  while (it != security_policy_stats_map_.end()) {
914  std::vector<SecurityPolicyFlowStats> traffic_list;
915  const EndpointStatsContainer &cont = it->second;
916  EndpointStats value;
917  value.set_workload(GetVmName());
918  FillSecurityPolicyList(agent, cont.client_list, &value.client);
919  FillSecurityPolicyList(agent, cont.server_list, &value.server);
920  eps.insert(make_pair(it->first, value));
921  ++it;
922  }
923  obj->set_eps(eps);
924 }
925 
927  (Agent *agent, const SecurityPolicyStatsSet &ilist,
928  std::vector<SecurityPolicyFlowStats> *olist) {
929  const AgentUveBase *uve = agent->uve();
930  SecurityPolicyStatsSet::const_iterator sit = ilist.begin();
931  while (sit != ilist.end()) {
932  SecurityPolicyFlowStats item;
933  UveSecurityPolicyStatsPtr entry(*sit);
934  UveTagData tinfo;
935  uve->BuildTagNamesFromList(entry->local_tagset, &tinfo);
936  item.set_app(tinfo.application);
937  item.set_tier(tinfo.tier);
938  item.set_site(tinfo.site);
939  item.set_deployment(tinfo.deployment);
940  item.set_labels(tinfo.labels);
941  item.set_custom_tags(tinfo.custom_tags);
942  tinfo.Reset();
943  uve->BuildTagIdsFromList(entry->remote_tagset, &tinfo);
944  item.set_remote_app_id(tinfo.application);
945  item.set_remote_tier_id(tinfo.tier);
946  item.set_remote_site_id(tinfo.site);
947  item.set_remote_deployment_id(tinfo.deployment);
948  item.set_remote_label_ids(tinfo.labels);
949  item.set_remote_custom_tag_ids(tinfo.custom_tags);
950 
951  item.set_remote_vn(entry->remote_vn);
952  item.set_local_vn(entry->local_vn);
953  item.set_action(entry->action);
954  item.set_added(entry->added - entry->prev_added);
955  item.set_deleted(entry->deleted - entry->prev_deleted);
956  item.set_active(entry->active);
957  item.set_in_bytes(entry->in_bytes - entry->prev_in_bytes);
958  item.set_in_pkts(entry->in_pkts - entry->prev_in_pkts);
959  item.set_out_bytes(entry->out_bytes - entry->prev_out_bytes);
960  item.set_out_pkts(entry->out_pkts - entry->prev_out_pkts);
961  olist->push_back(item);
962 
963  entry->prev_in_bytes = entry->in_bytes;
964  entry->prev_in_pkts = entry->in_pkts;
965  entry->prev_out_bytes = entry->out_bytes;
966  entry->prev_out_pkts = entry->out_pkts;
967  entry->prev_added = entry->added;
968  entry->prev_deleted = entry->deleted;
969 
970  ++sit;
971  }
972 }
973 
975  (VMIStats *obj) {
976 
977  vector<string> rule_list;
978  SecurityPolicyStatsMap::const_iterator it =
979  security_policy_stats_map_.begin();
980  while (it != security_policy_stats_map_.end()) {
981  rule_list.push_back(it->first);
982  ++it;
983  }
984  obj->set_policy_rules(rule_list);
985 }
986 
988  (const TagList &list, vector<SandeshUveTagInfo> *rts) const {
989  TagList::const_iterator it = list.begin();
990  while (it != list.end()) {
991  SandeshUveTagInfo tag_entry;
992  uint32_t ttype = (uint32_t)*it >> TagEntry::kTagTypeBitShift;
993  tag_entry.set_type(TagEntry::GetTypeStr(ttype));
994  tag_entry.set_id(*it);
995  AgentDBTable *table = static_cast<AgentDBTable *>(intf_->get_table());
996  tag_entry.set_name(table->agent()->tag_table()->TagName(*it));
997  rts->push_back(tag_entry);
998  ++it;
999  }
1000 }
1001 
1003  (const SecurityPolicyStatsSet &ilist,
1004  vector<SandeshUveRemoteEndpoint> *olist) const {
1005  SecurityPolicyStatsSet::const_iterator sit = ilist.begin();
1006  while (sit != ilist.end()) {
1007  SandeshUveRemoteEndpoint rep;
1008  UveSecurityPolicyStatsPtr entry(*sit);
1009  ++sit;
1010 
1011  rep.set_remote_vn(entry->remote_vn);
1012  rep.set_local_vn(entry->local_vn);
1013  rep.set_remote_prefix(entry->remote_prefix);
1014 
1015  vector<SandeshUveTagInfo> lts;
1016  BuildSandeshUveTagList(entry->local_tagset, &lts);
1017  rep.set_local_tagset(lts);
1018  vector<SandeshUveTagInfo> rts;
1019  BuildSandeshUveTagList(entry->remote_tagset, &rts);
1020  rep.set_remote_tagset(rts);
1021  rep.set_added(entry->added);
1022  rep.set_deleted(entry->deleted);
1023  rep.set_active(entry->active);
1024  rep.set_dropped_short(entry->dropped_short);
1025  rep.set_in_bytes(entry->in_bytes);
1026  rep.set_in_pkts(entry->in_pkts);
1027  rep.set_out_bytes(entry->out_bytes);
1028  rep.set_out_pkts(entry->out_pkts);
1029  rep.set_prev_in_bytes(entry->prev_in_bytes);
1030  rep.set_prev_in_pkts(entry->prev_in_pkts);
1031  rep.set_prev_out_bytes(entry->prev_out_bytes);
1032  rep.set_prev_out_pkts(entry->prev_out_pkts);
1033  rep.set_action(entry->action);
1034 
1035  olist->push_back(rep);
1036  }
1037 }
1038 
1040  (InterfaceUveInfo *r) const {
1041  r->set_name(intf_->cfg_name());
1042 
1043  vector<SandeshUveTagInfo> lts;
1044  BuildSandeshUveTagList(local_tagset_, &lts);
1045  r->set_local_tagset(lts);
1046 
1047  vector<SandeshUvePolicyInfo> policy_list;
1048  SecurityPolicyStatsMap::const_iterator it =
1049  security_policy_stats_map_.begin();
1050  while (it != security_policy_stats_map_.end()) {
1051  SandeshUvePolicyInfo item;
1052  item.set_name(it->first);
1053  const EndpointStatsContainer &cont = it->second;
1054  BuildInterfaceUveSecurityPolicyList(cont.client_list,
1055  &item.client_list);
1056  BuildInterfaceUveSecurityPolicyList(cont.server_list,
1057  &item.server_list);
1058  policy_list.push_back(item);
1059  ++it;
1060  }
1061  r->set_policy_list(policy_list);
1062 }
void SetStats(VmFloatingIPStats &fip, uint64_t in_bytes, uint64_t in_pkts, uint64_t out_bytes, uint64_t out_pkts) const
bool FrameTagsUveMsg(Agent *agent, const std::string &name, VMITags *uve)
std::string application
std::vector< string > label_vector
void UpdatePortBitmap(uint8_t proto, uint16_t sport, uint16_t dport)
void UpdateInterfaceAceStats(const std::string &ace_uuid)
std::pair< std::string, EndpointStatsContainer > SecurityPolicyStatsPair
uint32_t incremental_interval() const
void SetVMIStatsVnVm(VMIStats *uve) const
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
void set_expiry_time(int time)
DBState * GetState(DBTableBase *tbl_base, ListenerId listener) const
Definition: db_entry.cc:37
uint32_t default_interval() const
InterfaceMap interface_tree_
bool FipAggStatsChanged(const vector< VmFloatingIPStats > &list) const
HealthCheckService * service() const
Definition: health_check.h:146
static const std::string & GetTypeStr(uint32_t tag_type)
const std::string & TagName(uint32_t id)
std::string tier
void UpdateSecurityPolicyStats(const EndpointStatsInfo &info)
std::string custom_tags
AgentUveBase * uve() const
Definition: agent.cc:909
bool IsDeleted() const
Definition: db_entry.h:49
void SetState(DBTableBase *tbl_base, ListenerId listener, DBState *state)
Definition: db_entry.cc:22
DBTableBase::ListenerId intf_listener_id_
Definition: vm.h:32
Agent * agent() const
Definition: agent_db.h:213
virtual bool IsRunning() const
Definition: health_check.h:145
DBTableBase * parent()
static const uint32_t kUveCountPerTimer
InterfaceTable * interface_table() const
Definition: agent.h:465
std::string site
void BuildTagIdsFromList(const TagList &tl, UveTagData *info) const
void DispatchVMITagsMsg(const VMITags &uve) const
tbb::mutex interface_tree_mutex_
void InterfaceAddHandler(const VmInterface *intf, const VmInterface::FloatingIpSet &old_list)
virtual void DispatchInterfaceObjectLog(EndpointSecurityStats *obj)
const boost::uuids::uuid & GetUuid() const
Definition: vm.h:46
VmInterface::FloatingIpSet fip_list_
void BuildTagNamesFromList(const TagList &tl, UveTagData *info) const
TagTable * tag_table() const
Definition: agent.h:505
void Unregister(ListenerId listener)
Definition: db_table.cc:186
std::set< UveSecurityPolicyStatsPtr, PolicyCmp > SecurityPolicyStatsSet
InterfaceUveTable(Agent *agent, uint32_t default_intvl)
ListenerId Register(ChangeCallback callback, const std::string &name="unspecified")
Definition: db_table.cc:181
void UpdateInterfaceFwPolicyStats(const FlowUveFwPolicyInfo &info)
void RemoveFloatingIp(const VmInterface::FloatingIp &fip)
boost::shared_ptr< FloatingIp > FloatingIpPtr
void UpdateFloatingIpStats(const FipInfo &fip_info)
virtual void DispatchInterfaceMsg(const UveVMInterfaceAgent &uve)
virtual UveInterfaceEntryPtr Allocate(const VmInterface *vm)
void HandleVmiTagListChange(const std::string &name)
Definition: agent.h:358
bool OutBandChanged(uint64_t out_band) const
bool InBandChanged(uint64_t in_band) const
void UpdateSecurityPolicyStatsInternal(const EndpointStatsInfo &info, UveSecurityPolicyStats *stats)
void SetDiffStats(VmFloatingIPStats &fip, uint64_t in_bytes, uint64_t in_pkts, uint64_t out_bytes, uint64_t out_pkts, bool &diff_list_send) const
std::pair< std::string, UveInterfaceEntryPtr > InterfacePair
void InterfaceNotify(DBTablePartBase *partition, DBEntryBase *e)
bool Installed() const
Definition: vm_interface.h:171
bool GetVmInterfaceGateway(const VmInterface *vm_intf, std::string &gw) const
std::set< HealthCheckInstanceBase * > HealthCheckInstanceSet
Definition: vm_interface.h:466
std::string deployment
const std::vector< VnIpam > & GetVnIpam() const
Definition: vn.h:171
const VnEntry * vn() const
SecurityPolicyStatsSet & ToList(bool client)
void UpdateCounters(const FlowUveFwPolicyInfo &info, UveSecurityPolicyStats *obj)
void AddFloatingIp(const VmInterface::FloatingIp &fip)
bool PortBitmapChanged(const PortBucketBitmap &bmap) const
bool FillFloatingIpStats(vector< VmFloatingIPStats > &result, vector< VmFloatingIPStats > &diff_list, bool &diff_list_send)
void FillEndpointStats(Agent *agent, EndpointSecurityStats *obj)
void SendInterfaceDeleteMsg(const std::string &config_name)
void FillSecurityPolicyList(Agent *agent, const SecurityPolicyStatsSet &ilist, std::vector< SecurityPolicyFlowStats > *ol)
std::vector< string > custom_tag_vector
void ClearState(DBTableBase *tbl_base, ListenerId listener)
Definition: db_entry.cc:73
virtual void DispatchVMIStatsMsg(const VMIStats &uve)
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
bool Cancel()
Definition: timer.cc:150
Definition: vn.h:151
void SetVnVmInfo(UveVMInterfaceAgent *uve) const
const Ip4Address & primary_ip_addr() const
const boost::uuids::uuid & uuid() const
Definition: health_check.h:325
const FloatingIpList & floating_ip_list() const
InterfaceUveTable::FloatingIp * FipEntry(uint32_t ip, const std::string &vn)
std::string timer_last_visited_
bool FrameInterfaceAceStatsMsg(const std::string &name, VMIStats *s_intf)
const std::string & vm_name() const
void UpdateFloatingIpStats(const FipInfo &fip_info)
const std::string & name() const
Definition: health_check.h:326
void BuildSandeshUveTagList(const TagList &list, std::vector< SandeshUveTagInfo > *rts) const
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
Definition: timer.cc:108
const std::string & cfg_name() const
static const int kInvalidId
Definition: db_table.h:64
std::set< FloatingIp, FloatingIp > FloatingIpSet
Definition: vm_interface.h:567
void SendInterfaceMsg(const std::string &name, UveInterfaceEntry *entry)
boost::shared_ptr< UveInterfaceEntry > UveInterfaceEntryPtr
static const uint32_t kTagTypeBitShift
const std::string & name() const
Definition: interface.h:114
void BuildInterfaceUveInfo(InterfaceUveInfo *r) const
void InterfaceDeleteHandler(const std::string &name)
boost::shared_ptr< UveSecurityPolicyStats > UveSecurityPolicyStatsPtr
bool Reschedule(int time)
Definition: timer.cc:137
virtual void SendInterfaceAceStats(const string &name, UveInterfaceEntry *entry)
void BuildInterfaceUveSecurityPolicyList(const SecurityPolicyStatsSet &ilist, std::vector< SandeshUveRemoteEndpoint > *olist) const
bool HasFloatingIp(Address::Family family) const
std::string labels
void Reset()
bool FrameInterfaceMsg(const std::string &name, UveVMInterfaceAgent *s_intf) const
const VmEntry * vm() const
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222
std::vector< int > TagList
Definition: agent.h:202