/
interface_uve_table.cc
634 lines (579 loc) · 21.5 KB
/
interface_uve_table.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
* Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
*/
#include <oper/interface_common.h>
#include <oper/health_check.h>
#include <uve/interface_uve_table.h>
#include <uve/agent_uve_base.h>
InterfaceUveTable::InterfaceUveTable(Agent *agent, uint32_t default_intvl)
: agent_(agent), interface_tree_(), interface_tree_mutex_(),
intf_listener_id_(DBTableBase::kInvalidId),
timer_last_visited_(""),
timer_(TimerManager::CreateTimer
(*(agent->event_manager())->io_service(),
"InterfaceUveTimer",
TaskScheduler::GetInstance()->GetTaskId(kTaskDBExclude), 0)) {
expiry_time_ = default_intvl;
timer_->Start(expiry_time_,
boost::bind(&InterfaceUveTable::TimerExpiry, this));
}
InterfaceUveTable::~InterfaceUveTable() {
}
bool InterfaceUveTable::TimerExpiry() {
InterfaceMap::iterator it = interface_tree_.lower_bound(timer_last_visited_);
if (it == interface_tree_.end()) {
timer_last_visited_ = "";
return true;
}
uint32_t count = 0;
while (it != interface_tree_.end() &&
count < AgentUveBase::kUveCountPerTimer) {
string cfg_name = it->first;
UveInterfaceEntry* entry = it->second.get();
InterfaceMap::iterator prev = it;
it++;
count++;
if (entry->deleted_) {
SendInterfaceDeleteMsg(cfg_name);
if (!entry->renewed_) {
tbb::mutex::scoped_lock lock(interface_tree_mutex_);
interface_tree_.erase(prev);
} else {
entry->deleted_ = false;
entry->renewed_ = false;
entry->changed_ = false;
SendInterfaceMsg(cfg_name, entry);
// Send Interface ACE stats
SendInterfaceAceStats(cfg_name, entry);
}
} else {
if (entry->changed_) {
SendInterfaceMsg(cfg_name, entry);
entry->changed_ = false;
/* Clear renew flag to be on safer side. Not really required */
entry->renewed_ = false;
}
// Send Interface ACE stats
SendInterfaceAceStats(cfg_name, entry);
}
}
if (it == interface_tree_.end()) {
timer_last_visited_ = "";
set_expiry_time(agent_->uve()->default_interval());
} else {
timer_last_visited_ = it->first;
set_expiry_time(agent_->uve()->incremental_interval());
}
/* Return true to trigger auto-restart of timer */
return true;
}
void InterfaceUveTable::set_expiry_time(int time) {
if (time != expiry_time_) {
expiry_time_ = time;
timer_->Reschedule(expiry_time_);
}
}
void InterfaceUveTable::UveInterfaceEntry::SetVnVmInfo(UveVMInterfaceAgent *uve)
const {
/* VM interfaces which are not created by Nova will not have VM name set.
* In that case pick VM name from VM object instead of VMI object */
const VmEntry *vm = intf_->vm();
if (!intf_->vm_name().empty()) {
uve->set_vm_name(intf_->vm_name());
} else {
if (vm) {
uve->set_vm_name(vm->GetCfgName());
}
}
if (intf_->vn() != NULL) {
uve->set_virtual_network(intf_->vn()->GetName());
} else {
uve->set_virtual_network("");
}
if (vm) {
uve->set_vm_uuid(to_string(vm->GetUuid()));
} else {
uve->set_vm_uuid("");
}
}
bool InterfaceUveTable::UveInterfaceEntry::FrameInterfaceMsg(const string &name,
UveVMInterfaceAgent *s_intf) const {
s_intf->set_name(name);
SetVnVmInfo(s_intf);
s_intf->set_ip_address(intf_->primary_ip_addr().to_string());
s_intf->set_mac_address(intf_->vm_mac());
s_intf->set_ip6_address(intf_->primary_ip6_addr().to_string());
s_intf->set_ip6_active(intf_->ipv6_active());
s_intf->set_is_health_check_active(intf_->is_hc_active());
vector<VmFloatingIPAgent> uve_fip_list;
if (intf_->HasFloatingIp(Address::INET)) {
const VmInterface::FloatingIpList fip_list =
intf_->floating_ip_list();
VmInterface::FloatingIpSet::const_iterator it =
fip_list.list_.begin();
while(it != fip_list.list_.end()) {
const VmInterface::FloatingIp &ip = *it;
/* Don't export FIP entry if it is not installed. When FIP entry
* is not installed it will have NULL VN pointer. We can receive
* notifications for VM interface with un-installed FIP entries
* when the VM interface is not "L3 Active".
*/
if (ip.installed_) {
VmFloatingIPAgent uve_fip;
uve_fip.set_ip_address(ip.floating_ip_.to_string());
uve_fip.set_virtual_network(ip.vn_.get()->GetName());
uve_fip_list.push_back(uve_fip);
}
it++;
}
}
s_intf->set_floating_ips(uve_fip_list);
vector<VmHealthCheckInstance> uve_hc_list;
const VmInterface::HealthCheckInstanceSet hc_list =
intf_->hc_instance_set();
VmInterface::HealthCheckInstanceSet::const_iterator hc_it =
hc_list.begin();
while (hc_it != hc_list.end()) {
HealthCheckInstance *inst = (*hc_it);
VmHealthCheckInstance uve_inst;
uve_inst.set_name(inst->service_->name());
uve_inst.set_uuid(to_string(inst->service_->uuid()));
uve_inst.set_status(inst->active() ? "Active" : "InActive");
uve_inst.set_is_running(inst->IsRunning());
hc_it++;
uve_hc_list.push_back(uve_inst);
}
s_intf->set_health_check_instance_list(uve_hc_list);
s_intf->set_label(intf_->label());
s_intf->set_ip4_active(intf_->ipv4_active());
s_intf->set_l2_active(intf_->l2_active());
s_intf->set_active(intf_->IsUveActive());
s_intf->set_uuid(to_string(intf_->GetUuid()));
string gw;
if (GetVmInterfaceGateway(intf_, gw)) {
s_intf->set_gateway(gw);
}
return true;
}
void InterfaceUveTable::UveInterfaceEntry::Reset() {
intf_ = NULL;
port_bitmap_.Reset();
prev_fip_tree_.clear();
fip_tree_.clear();
ace_set_.clear();
ace_stats_changed_ = false;
deleted_ = true;
renewed_ = false;
}
void InterfaceUveTable::UveInterfaceEntry::UpdatePortBitmap
(uint8_t proto, uint16_t sport, uint16_t dport) {
tbb::mutex::scoped_lock lock(mutex_);
/* No need to update stats if the entry is marked for delete and not
* renewed */
if (deleted_ && !renewed_) {
return;
}
port_bitmap_.AddPort(proto, sport, dport);
}
bool InterfaceUveTable::UveInterfaceEntry::FipAggStatsChanged
(const vector<VmFloatingIPStats> &list) const {
if (list != uve_info_.get_fip_agg_stats()) {
return true;
}
return false;
}
bool InterfaceUveTable::UveInterfaceEntry::GetVmInterfaceGateway(
const VmInterface *vm_intf, string &gw) const {
const VnEntry *vn = vm_intf->vn();
if (vn == NULL) {
return false;
}
const vector<VnIpam> &list = vn->GetVnIpam();
Ip4Address vm_addr = vm_intf->primary_ip_addr();
unsigned int i;
for (i = 0; i < list.size(); i++) {
if (list[i].IsSubnetMember(vm_addr))
break;
}
if (i == list.size()) {
return false;
}
gw = list[i].default_gw.to_string();
return true;
}
void InterfaceUveTable::SendInterfaceDeleteMsg(const string &config_name) {
UveVMInterfaceAgent uve;
uve.set_name(config_name);
uve.set_deleted(true);
DispatchInterfaceMsg(uve);
}
InterfaceUveTable::UveInterfaceEntryPtr
InterfaceUveTable::Allocate(const VmInterface *itf) {
UveInterfaceEntryPtr uve(new UveInterfaceEntry(itf));
return uve;
}
void InterfaceUveTable::DispatchInterfaceMsg(const UveVMInterfaceAgent &uve) {
UveVMInterfaceAgentTrace::Send(uve);
}
void InterfaceUveTable::SendInterfaceMsg(const string &name,
UveInterfaceEntry *entry) {
UveVMInterfaceAgent uve;
if (entry->FrameInterfaceMsg(name, &uve)) {
DispatchInterfaceMsg(uve);
}
}
void InterfaceUveTable::InterfaceAddHandler(const VmInterface* itf,
const VmInterface::FloatingIpSet &old_list) {
UveInterfaceEntryPtr uve = Allocate(itf);
pair<InterfaceMap::iterator, bool> ret;
ret = interface_tree_.insert(InterfacePair(itf->cfg_name(), uve));
InterfaceMap::iterator it = ret.first;
UveInterfaceEntry* entry = it->second.get();
if (entry->deleted_) {
entry->renewed_ = true;
entry->intf_ = itf;
}
/* Mark the entry as changed to account for change in any fields of VMI */
entry->changed_ = true;
const VmInterface::FloatingIpSet &new_list = itf->floating_ip_list().list_;
/* Remove old entries, by checking entries which are present in old list,
* but not in new list */
VmInterface::FloatingIpSet::const_iterator old_it = old_list.begin();
while (old_it != old_list.end()) {
VmInterface::FloatingIp fip = *old_it;
++old_it;
/* Skip entries which are not installed as they wouldn't have been
* added
*/
if (!fip.installed_) {
continue;
}
VmInterface::FloatingIpSet::const_iterator new_it = new_list.find(fip);
if (new_it == new_list.end()) {
entry->RemoveFloatingIp(fip);
}
}
/* Add entries in new list. Ignore if entries already present */
VmInterface::FloatingIpSet::const_iterator fip_it = new_list.begin();
while (fip_it != new_list.end()) {
VmInterface::FloatingIp fip = *fip_it;
++fip_it;
/* Skip entries which are not installed as they wouldn't have been
* added
*/
if (!fip.installed_) {
continue;
}
entry->AddFloatingIp(fip);
}
}
void InterfaceUveTable::InterfaceDeleteHandler(const string &name) {
InterfaceMap::iterator it = interface_tree_.find(name);
if (it == interface_tree_.end()) {
return;
}
UveInterfaceEntry* entry = it->second.get();
/* We need to reset all non-key fields to ensure that they have right
* values since the entry is getting re-used. Also update the 'deleted_'
* and 'renewed_' flags */
entry->Reset();
return;
}
void InterfaceUveTable::InterfaceNotify(DBTablePartBase *partition,
DBEntryBase *e) {
const VmInterface *vm_port = dynamic_cast<const VmInterface*>(e);
if (vm_port == NULL) {
return;
}
UveInterfaceState *state = static_cast<UveInterfaceState *>
(e->GetState(partition->parent(), intf_listener_id_));
if (e->IsDeleted()) {
if (state) {
InterfaceDeleteHandler(state->cfg_name_);
state->fip_list_.clear();
e->ClearState(partition->parent(), intf_listener_id_);
delete state;
}
} else {
VmInterface::FloatingIpSet old_list;
if (!state && vm_port->cfg_name().empty()) {
/* Skip Add/change notifications if the config_name is empty */
return;
}
if (!state) {
state = new UveInterfaceState(vm_port);
e->SetState(partition->parent(), intf_listener_id_, state);
} else {
old_list = state->fip_list_;
state->fip_list_ = vm_port->floating_ip_list().list_;
}
if (state->cfg_name_ != vm_port->cfg_name()) {
InterfaceDeleteHandler(state->cfg_name_);
state->cfg_name_ = vm_port->cfg_name();
old_list.clear();
}
if (!vm_port->cfg_name().empty()) {
InterfaceAddHandler(vm_port, old_list);
}
}
}
void InterfaceUveTable::RegisterDBClients() {
InterfaceTable *intf_table = agent_->interface_table();
intf_listener_id_ = intf_table->Register
(boost::bind(&InterfaceUveTable::InterfaceNotify, this, _1, _2));
}
void InterfaceUveTable::Shutdown(void) {
if (intf_listener_id_ != DBTableBase::kInvalidId)
agent_->interface_table()->Unregister(intf_listener_id_);
if (timer_) {
timer_->Cancel();
TimerManager::DeleteTimer(timer_);
timer_ = NULL;
}
}
bool InterfaceUveTable::UveInterfaceEntry::PortBitmapChanged
(const PortBucketBitmap &bmap) const {
if (!uve_info_.__isset.port_bucket_bmap) {
return true;
}
if (bmap != uve_info_.get_port_bucket_bmap()) {
return true;
}
return false;
}
bool InterfaceUveTable::UveInterfaceEntry::InBandChanged(uint64_t in_band)
const {
if (!uve_info_.__isset.in_bw_usage) {
return true;
}
if (in_band != uve_info_.get_in_bw_usage()) {
return true;
}
return false;
}
bool InterfaceUveTable::UveInterfaceEntry::OutBandChanged(uint64_t out_band)
const {
if (!uve_info_.__isset.out_bw_usage) {
return true;
}
if (out_band != uve_info_.get_out_bw_usage()) {
return true;
}
return false;
}
void InterfaceUveTable::UveInterfaceEntry::UpdateFloatingIpStats
(const FipInfo &fip_info) {
tbb::mutex::scoped_lock lock(mutex_);
/* No need to update stats if the entry is marked for delete and not
* renewed */
if (deleted_ && !renewed_) {
return;
}
FloatingIp *entry = FipEntry(fip_info.fip_, fip_info.vn_);
/* Ignore stats update request if it comes after entry is removed */
if (entry == NULL) {
return;
}
entry->UpdateFloatingIpStats(fip_info);
}
InterfaceUveTable::FloatingIp *InterfaceUveTable::UveInterfaceEntry::FipEntry
(uint32_t ip, const string &vn) {
Ip4Address addr(ip);
FloatingIpPtr key(new FloatingIp(addr, vn));
FloatingIpSet::iterator fip_it = fip_tree_.find(key);
if (fip_it == fip_tree_.end()) {
return NULL;
} else {
return (*fip_it).get();
}
}
void InterfaceUveTable::FloatingIp::UpdateFloatingIpStats(const FipInfo &fip_info) {
if (fip_info.is_local_flow_) {
if (fip_info.is_reverse_flow_) {
out_bytes_ += fip_info.bytes_;
out_packets_ += fip_info.packets_;
if (fip_info.rev_fip_) {
/* This is the case where Source and Destination VMs (part of
* same compute node) ping to each other to their respective
* Floating IPs. In this case for each flow we need to increment
* stats for both the VMs */
fip_info.rev_fip_->out_bytes_ += fip_info.bytes_;
fip_info.rev_fip_->out_packets_ += fip_info.packets_;
}
} else {
in_bytes_ += fip_info.bytes_;
in_packets_ += fip_info.packets_;
if (fip_info.rev_fip_) {
/* This is the case where Source and Destination VMs (part of
* same compute node) ping to each other to their respective
* Floating IPs. In this case for each flow we need to increment
* stats for both the VMs */
fip_info.rev_fip_->in_bytes_ += fip_info.bytes_;
fip_info.rev_fip_->in_packets_ += fip_info.packets_;
}
}
} else {
if (fip_info.is_ingress_flow_) {
in_bytes_ += fip_info.bytes_;
in_packets_ += fip_info.packets_;
} else {
out_bytes_ += fip_info.bytes_;
out_packets_ += fip_info.packets_;
}
}
}
bool InterfaceUveTable::UveInterfaceEntry::FillFloatingIpStats
(vector<VmFloatingIPStats> &result,
vector<VmFloatingIPStats> &diff_list,
bool &diff_list_send) {
const VmInterface *vm_intf = static_cast<const VmInterface *>(intf_);
tbb::mutex::scoped_lock lock(mutex_);
if (vm_intf->HasFloatingIp()) {
const VmInterface::FloatingIpList fip_list =
vm_intf->floating_ip_list();
VmInterface::FloatingIpSet::const_iterator it =
fip_list.list_.begin();
while(it != fip_list.list_.end()) {
const VmInterface::FloatingIp &ip = *it;
/* Skip FIP entries which are not yet activated */
if (!ip.installed_) {
it++;
continue;
}
VmFloatingIPStats uve_fip;
VmFloatingIPStats diff_uve;
uve_fip.set_ip_address(ip.floating_ip_.to_string());
uve_fip.set_virtual_network(ip.vn_.get()->GetName());
diff_uve.set_ip_address(ip.floating_ip_.to_string());
diff_uve.set_virtual_network(ip.vn_.get()->GetName());
FloatingIpPtr key(new FloatingIp(ip.floating_ip_,
ip.vn_.get()->GetName()));
FloatingIpSet::iterator fip_it = fip_tree_.find(key);
if (fip_it == fip_tree_.end()) {
SetStats(uve_fip, 0, 0, 0, 0);
SetDiffStats(diff_uve, 0, 0, 0, 0, diff_list_send);
} else {
FloatingIp *fip = (*fip_it).get();
SetStats(uve_fip, fip->in_bytes_, fip->in_packets_,
fip->out_bytes_, fip->out_packets_);
FloatingIpSet::iterator prev_it = prev_fip_tree_.find(key);
if (prev_it == prev_fip_tree_.end()) {
SetDiffStats(diff_uve, fip->in_bytes_, fip->in_packets_,
fip->out_bytes_, fip->out_packets_,
diff_list_send);
FloatingIpPtr prev_fip_ptr(new FloatingIp(ip.floating_ip_,
ip.vn_.get()->GetName(),
fip->in_bytes_,
fip->in_packets_,
fip->out_bytes_,
fip->out_packets_));
prev_fip_tree_.insert(prev_fip_ptr);
} else {
FloatingIp *pfip = (*prev_it).get();
SetDiffStats(diff_uve, (fip->in_bytes_ - pfip->in_bytes_),
(fip->in_packets_ - pfip->in_packets_),
(fip->out_bytes_ - pfip->out_bytes_),
(fip->out_packets_ - pfip->out_packets_),
diff_list_send);
pfip->in_bytes_ = fip->in_bytes_;
pfip->in_packets_ = fip->in_packets_;
pfip->out_bytes_ = fip->out_bytes_;
pfip->out_packets_ = fip->out_packets_;
}
}
result.push_back(uve_fip);
diff_list.push_back(diff_uve);
it++;
}
return true;
}
return false;
}
void InterfaceUveTable::UveInterfaceEntry::SetDiffStats
(VmFloatingIPStats &fip, uint64_t in_bytes, uint64_t in_pkts,
uint64_t out_bytes, uint64_t out_pkts, bool &diff_list_send) const {
fip.set_in_bytes(in_bytes);
fip.set_in_pkts(in_pkts);
fip.set_out_bytes(out_bytes);
fip.set_out_pkts(out_pkts);
if ((in_bytes != 0) || (in_pkts != 0) || (out_bytes != 0) ||
(out_pkts != 0)) {
diff_list_send = true;
}
}
void InterfaceUveTable::UveInterfaceEntry::SetStats
(VmFloatingIPStats &fip, uint64_t in_bytes, uint64_t in_pkts,
uint64_t out_bytes, uint64_t out_pkts) const {
fip.set_in_bytes(in_bytes);
fip.set_in_pkts(in_pkts);
fip.set_out_bytes(out_bytes);
fip.set_out_pkts(out_pkts);
}
void InterfaceUveTable::UveInterfaceEntry::AddFloatingIp
(const VmInterface::FloatingIp &fip) {
FloatingIpPtr key(new FloatingIp(fip.floating_ip_,
fip.vn_.get()->GetName()));
FloatingIpSet::iterator it = fip_tree_.find(key);
if (it != fip_tree_.end()) {
return;
}
fip_tree_.insert(key);
}
void InterfaceUveTable::UveInterfaceEntry::RemoveFloatingIp
(const VmInterface::FloatingIp &fip) {
FloatingIpPtr key(new FloatingIp(fip.floating_ip_, fip.vn_.get()->GetName()));
FloatingIpSet::iterator it = fip_tree_.find(key);
if (it != fip_tree_.end()) {
fip_tree_.erase(it);
}
FloatingIpSet::iterator prev_it = prev_fip_tree_.find(key);
if (prev_it != prev_fip_tree_.end()) {
prev_fip_tree_.erase(prev_it);
}
}
void InterfaceUveTable::UveInterfaceEntry::UpdateInterfaceAceStats
(const std::string &ace_uuid) {
AceStats key(ace_uuid);
ace_stats_changed_ = true;
AceStatsSet::const_iterator it = ace_set_.find(key);
if (it != ace_set_.end()) {
it->count++;
return;
}
key.count = 1;
ace_set_.insert(key);
}
bool InterfaceUveTable::UveInterfaceEntry::FrameInterfaceAceStatsMsg
(const std::string &name, UveVMInterfaceAgent *s_intf) {
if (!ace_stats_changed_) {
return false;
}
std::vector<SgAclRuleStats> list;
AceStatsSet::iterator it = ace_set_.begin();
bool changed = false;
while (it != ace_set_.end()) {
SgAclRuleStats item;
item.set_rule(it->ace_uuid);
uint64_t diff_count = it->count - it->prev_count;
item.set_count(diff_count);
//Update prev_count
it->prev_count = it->count;
list.push_back(item);
++it;
/* If diff_count is non-zero for any rule entry, we send the entire
* list */
if (diff_count) {
changed = true;
}
}
/* If all the entries in the list has 0 diff_stats, then UVE won't be
* sent */
if (changed) {
s_intf->set_name(name);
SetVnVmInfo(s_intf);
s_intf->set_sg_rule_stats(list);
ace_stats_changed_ = false;
return true;
}
return false;
}