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