/
vnswif_listener_base.cc
521 lines (440 loc) · 16.6 KB
/
vnswif_listener_base.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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <assert.h>
#include <base/logging.h>
#include <base/util.h>
#include <cmn/agent_cmn.h>
#include <init/agent_param.h>
#include <init/agent_init.h>
#include <cfg/cfg_init.h>
#include <oper/route_common.h>
#include <oper/mirror_table.h>
#include <ksync/ksync_index.h>
#include <vrouter/ksync/interface_ksync.h>
#include "vrouter/ksync/vnswif_listener_base.h"
extern void RouterIdDepInit(Agent *agent);
SandeshTraceBufferPtr VnswIfTraceBuf(SandeshTraceBufferCreate(
VNSWIF_TRACE_BUF, 2000));
VnswInterfaceListenerBase::VnswInterfaceListenerBase(Agent *agent) :
agent_(agent), read_buf_(NULL), sock_fd_(-1),
sock_(*(agent->event_manager())->io_service()),
intf_listener_id_(DBTableBase::kInvalidId),
fabric_listener_id_(DBTableBase::kInvalidId), seqno_(0),
vhost_intf_up_(false), ll_addr_table_(), revent_queue_(NULL),
vhost_update_count_(0), ll_add_count_(0), ll_del_count_(0) {
}
VnswInterfaceListenerBase::~VnswInterfaceListenerBase() {
if (read_buf_){
delete [] read_buf_;
read_buf_ = NULL;
}
delete revent_queue_;
}
/****************************************************************************
* Initialization and shutdown code
* 1. Opens netlink socket
* 2. Starts scan of interface from host-os
* 3. Starts scan of routes from host-os
* 4. Registers for netlink messages with ASIO
****************************************************************************/
void VnswInterfaceListenerBase::Init() {
intf_listener_id_ = agent_->interface_table()->Register
(boost::bind(&VnswInterfaceListenerBase::InterfaceNotify, this, _1, _2));
fabric_listener_id_ = agent_->fabric_inet4_unicast_table()->Register
(boost::bind(&VnswInterfaceListenerBase::FabricRouteNotify,
this, _1, _2));
/* Allocate Route Event Workqueue */
revent_queue_ = new WorkQueue<Event *>
(TaskScheduler::GetInstance()->GetTaskId(kTaskDBExclude), 0,
boost::bind(&VnswInterfaceListenerBase::ProcessEvent,
this, _1));
revent_queue_->set_name("Netlink interface listener");
if (agent_->test_mode())
return;
/* Create socket and listen and handle ip address updates */
sock_fd_ = CreateSocket();
/* Assign native socket to boost asio */
boost::asio::local::datagram_protocol protocol;
sock_.assign(protocol, sock_fd_);
SyncCurrentState();
RegisterAsyncReadHandler();
}
void VnswInterfaceListenerBase::Shutdown() {
agent_->interface_table()->Unregister(intf_listener_id_);
agent_->fabric_inet4_unicast_table()->Unregister(fabric_listener_id_);
// Expect only one entry for vhost0 during shutdown
assert(host_interface_table_.size() == 0);
if (agent_->test_mode()) {
return;
}
boost::system::error_code ec;
sock_.close(ec);
}
void VnswInterfaceListenerBase::Enqueue(Event *event) {
revent_queue_->Enqueue(event);
}
/****************************************************************************
* Interface DB notification handler. Triggers add/delete of link-local route
****************************************************************************/
void VnswInterfaceListenerBase::InterfaceNotify(DBTablePartBase *part,
DBEntryBase *e) {
const VmInterface *vmport = dynamic_cast<VmInterface *>(e);
if (vmport == NULL) {
return;
}
DBState *state = e->GetState(part->parent(), intf_listener_id_);
if (vmport->IsDeleted()) {
if (state) {
HostInterfaceEntry *entry = GetHostInterfaceEntry(vmport->name());
uint32_t id = Interface::kInvalidIndex;
if (entry) {
id = entry->oper_id_;
}
ostringstream oss;
oss << "Intf Del " << vmport->name() << " id " << id;
string msg = oss.str();
VNSWIF_TRACE(msg.c_str());
ResetSeen(vmport->name(), true);
e->ClearState(part->parent(), intf_listener_id_);
delete state;
}
} else {
if (state == NULL) {
ostringstream oss;
oss << "Intf Add " << vmport->name() << " id " << vmport->id();
string msg = oss.str();
VNSWIF_TRACE(msg.c_str());
state = new DBState();
e->SetState(part->parent(), intf_listener_id_, state);
SetSeen(vmport->name(), true, vmport->id());
}
}
return;
}
/****************************************************************************
* Fabric unicast route notification handler. Triggers add/delete of
* link-local route
****************************************************************************/
void VnswInterfaceListenerBase::FabricRouteNotify(DBTablePartBase *part,
DBEntryBase *e) {
const InetUnicastRouteEntry *rt = dynamic_cast<InetUnicastRouteEntry *>(e);
if (rt == NULL || rt->GetTableType() != Agent::INET4_UNICAST) {
return;
}
DBState *state = e->GetState(part->parent(), fabric_listener_id_);
if (rt->IsDeleted()) {
if (state) {
e->ClearState(part->parent(), fabric_listener_id_);
delete state;
revent_queue_->Enqueue(new Event(Event::DEL_LL_ROUTE,
"", rt->addr().to_v4()));
}
} else {
// listen to only metadata ip routes
const AgentPath *path = rt->GetActivePath();
if (path == NULL || path->peer() != agent_->link_local_peer()) {
return;
}
if (state == NULL) {
state = new DBState();
e->SetState(part->parent(), fabric_listener_id_, state);
revent_queue_->Enqueue(new Event(Event::ADD_LL_ROUTE,
"", rt->addr().to_v4()));
}
}
return;
}
/****************************************************************************
* Interface Event handler
****************************************************************************/
bool VnswInterfaceListenerBase::IsInterfaceActive(const HostInterfaceEntry *entry) {
return entry->oper_seen_ && entry->host_seen_ && entry->link_up_;
}
static void InterfaceResync(Agent *agent, uint32_t id, bool active,
bool link_status) {
if (id == Interface::kInvalidIndex) {
return;
}
InterfaceTable *table = agent->interface_table();
Interface *interface = table->FindInterface(id);
if (interface == NULL) {
ostringstream oss;
oss << "InterfaceResync failed. Interface index " << id <<
" not found. Active " << active;
string msg = oss.str();
VNSWIF_TRACE(msg.c_str());
return;
}
if (agent->test_mode())
interface->set_test_oper_state(active);
DBRequest req(DBRequest::DB_ENTRY_ADD_CHANGE);
req.key.reset(new VmInterfaceKey(AgentKey::RESYNC, interface->GetUuid(),
interface->name()));
string link_state_str = link_status? "up" : "down";
ostringstream oss;
oss << "InterfaceResync for id " << id << " link_state " << link_state_str;
string msg = oss.str();
VNSWIF_TRACE(msg.c_str());
req.data.reset(new VmInterfaceOsOperStateData(link_status));
table->Enqueue(&req);
}
VnswInterfaceListenerBase::HostInterfaceEntry *
VnswInterfaceListenerBase::GetHostInterfaceEntry(const std::string &name) {
HostInterfaceTable::iterator it = host_interface_table_.find(name);
if (it == host_interface_table_.end())
return NULL;
return it->second;
}
// Handle transition from In-Active to Active interface
void VnswInterfaceListenerBase::Activate(const std::string &name,
const HostInterfaceEntry *entry) {
if (name == agent_->vhost_interface_name()) {
// link-local routes would have been deleted when vhost link was down.
// Add all routes again on activation
AddLinkLocalRoutes();
}
if (entry == NULL) {
return;
}
InterfaceResync(agent_, entry->oper_id_, true, entry->link_up_);
}
void VnswInterfaceListenerBase::DeActivate(const std::string &name,
const HostInterfaceEntry *entry) {
if (entry == NULL) {
return;
}
InterfaceResync(agent_, entry->oper_id_, false, entry->link_up_);
}
void VnswInterfaceListenerBase::SetSeen(const std::string &name, bool oper,
uint32_t id) {
HostInterfaceEntry *entry = GetHostInterfaceEntry(name);
if (entry == NULL) {
entry = new HostInterfaceEntry();
host_interface_table_.insert(make_pair(name, entry));
}
if (oper) {
entry->oper_seen_ = true;
entry->oper_id_ = id;
} else {
entry->host_seen_ = true;
}
Activate(name, entry);
}
void VnswInterfaceListenerBase::ResetSeen(const std::string &name, bool oper) {
HostInterfaceEntry *entry = GetHostInterfaceEntry(name);
if (entry == NULL)
return;
if (oper) {
entry->oper_seen_ = false;
entry->oper_id_ = Interface::kInvalidIndex;
} else {
entry->host_seen_ = false;
}
if (entry->oper_seen_ == false && entry->host_seen_ == false) {
HostInterfaceTable::iterator it = host_interface_table_.find(name);
host_interface_table_.erase(it);
delete entry;
return;
}
if (!oper) {
/* Send notification to interface only when it is not marked for
* delete */
DeActivate(name, entry);
}
}
void VnswInterfaceListenerBase::SetLinkState(const std::string &name, bool link_up){
HostInterfaceEntry *entry = GetHostInterfaceEntry(name);
if (entry == NULL)
return;
bool old_active = IsInterfaceActive(entry);
entry->link_up_ = link_up;
if (old_active)
DeActivate(name, entry);
else
Activate(name, entry);
}
void VnswInterfaceListenerBase::HandleInterfaceEvent(const Event *event) {
if (event->event_ == Event::DEL_INTERFACE) {
ResetSeen(event->interface_, false);
} else {
SetSeen(event->interface_, false, Interface::kInvalidIndex);
bool up =
(event->flags_ & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING);
SetLinkState(event->interface_, up);
// In XEN mode, notify add of XAPI interface
if (agent_->isXenMode()) {
if (string::npos != event->interface_.find(XAPI_INTF_PREFIX)) {
agent_->params()->set_xen_ll_name(event->interface_);
agent_->InitXenLinkLocalIntf();
}
}
}
}
bool VnswInterfaceListenerBase::IsValidLinkLocalAddress(const Ip4Address &addr)
const {
return (ll_addr_table_.find(addr) != ll_addr_table_.end());
}
/****************************************************************************
* Interface Address event handler
****************************************************************************/
void VnswInterfaceListenerBase::SetAddress(const Event *event) {
HostInterfaceEntry *entry = GetHostInterfaceEntry(event->interface_);
if (entry == NULL) {
entry = new HostInterfaceEntry();
host_interface_table_.insert(make_pair(event->interface_, entry));
}
entry->addr_ = event->addr_;
entry->plen_ = event->plen_;
}
void VnswInterfaceListenerBase::ResetAddress(const Event *event) {
HostInterfaceEntry *entry = GetHostInterfaceEntry(event->interface_);
if (entry == NULL) {
return;
}
entry->addr_ = Ip4Address(0);
entry->plen_ = 0;
}
void VnswInterfaceListenerBase::HandleAddressEvent(const Event *event) {
// Update address in interface table
if (event->event_ == Event::ADD_ADDR) {
SetAddress(event);
} else {
ResetAddress(event);
}
// We only handle IP Address add for VHOST interface
// We dont yet handle delete of IP address or change of IP address
if (event->event_ != Event::ADD_ADDR ||
event->interface_ != agent_->vhost_interface_name() ||
event->addr_.to_ulong() == 0) {
return;
}
// Check if vhost already has address. We cant handle IP address change yet
const VmInterface *vhost =
static_cast<const VmInterface *>(agent_->vhost_interface());
if (vhost->primary_ip_addr() != Ip4Address(0)) {
return;
}
ostringstream oss;
oss << "Setting IP address for " << event->interface_ << " to "
<< event->addr_.to_string() << "/" << (unsigned short)event->plen_;
string msg = oss.str();
VNSWIF_TRACE(msg.c_str());
vhost_update_count_++;
bool dep_init_reqd = false;
if (agent_->router_id_configured() == false)
dep_init_reqd = true;
// Update vhost ip-address and enqueue db request
agent_->set_router_id(event->addr_);
agent_->set_vhost_prefix_len(event->plen_);
agent_->interface_table()->CreateVhostReq();
if (dep_init_reqd)
agent_->agent_init()->ConnectToControllerBase();
}
void
VnswInterfaceListenerBase::UpdateLinkLocalRouteAndCount(
const Ip4Address &addr, bool del_rt)
{
if (del_rt)
ll_del_count_++;
else
ll_add_count_++;
if (agent_->test_mode())
return;
UpdateLinkLocalRoute(addr, del_rt);
}
// Handle link-local route changes resulting from ADD_LL_ROUTE or DEL_LL_ROUTE
void VnswInterfaceListenerBase::LinkLocalRouteFromLinkLocalEvent(Event *event) {
if (event->event_ == Event::DEL_LL_ROUTE) {
ll_addr_table_.erase(event->addr_);
UpdateLinkLocalRouteAndCount(event->addr_, true); } else {
ll_addr_table_.insert(event->addr_);
UpdateLinkLocalRouteAndCount(event->addr_, false);
}
}
// For link-local routes added by agent, we treat agent as master. So, force
// add the routes again if they are deleted from kernel
void VnswInterfaceListenerBase::LinkLocalRouteFromRouteEvent(Event *event) {
if (event->protocol_ != kVnswRtmProto)
return;
if (ll_addr_table_.find(event->addr_) == ll_addr_table_.end()) {
// link-local route is in kernel, but not in agent. This can happen
// when agent has restarted and the link-local route is not valid
// after agent restart.
// Delete the route
if (event->event_ == Event::ADD_ROUTE) {
UpdateLinkLocalRouteAndCount(event->addr_, true);
}
return;
}
if ((event->event_ == Event::DEL_ROUTE) ||
(event->event_ == Event::ADD_ROUTE
&& event->interface_ != agent_->vhost_interface_name())) {
UpdateLinkLocalRouteAndCount(event->addr_, false);
}
}
void VnswInterfaceListenerBase::AddLinkLocalRoutes() {
for (LinkLocalAddressTable::iterator it = ll_addr_table_.begin();
it != ll_addr_table_.end(); ++it) {
UpdateLinkLocalRouteAndCount(*it, false);
}
}
void VnswInterfaceListenerBase::DelLinkLocalRoutes() {
}
/****************************************************************************
* Event handler
****************************************************************************/
static string EventTypeToString(uint32_t type) {
const char *name[] = {
"INVALID",
"ADD_ADDR",
"DEL_ADDR",
"ADD_INTERFACE",
"DEL_INTERFACE",
"ADD_ROUTE",
"DEL_ROUTE",
"ADD_LL_ROUTE",
"DEL_LL_ROUTE"
};
if (type < sizeof(name)/sizeof(void *)) {
return name[type];
}
return "UNKNOWN";
}
bool VnswInterfaceListenerBase::ProcessEvent(Event *event) {
HostInterfaceEntry *entry = GetHostInterfaceEntry(event->interface_);
uint32_t id = Interface::kInvalidIndex;
if (entry) {
id = entry->oper_id_;
}
ostringstream oss;
oss << " Event " << EventTypeToString(event->event_)
<< " Interface " << event->interface_ << " Addr "
<< event->addr_.to_string() << " prefixlen " << (uint32_t)event->plen_
<< " Gateway " << event->gw_.to_string() << " Flags " << event->flags_
<< " Protocol " << (uint32_t)event->protocol_ << " Index " << id;
string msg = oss.str();
VNSWIF_TRACE(msg.c_str());
switch (event->event_) {
case Event::ADD_ADDR:
case Event::DEL_ADDR:
HandleAddressEvent(event);
break;
case Event::ADD_INTERFACE:
case Event::DEL_INTERFACE:
HandleInterfaceEvent(event);
break;
case Event::ADD_ROUTE:
case Event::DEL_ROUTE:
LinkLocalRouteFromRouteEvent(event);
break;
case Event::ADD_LL_ROUTE:
case Event::DEL_LL_ROUTE:
LinkLocalRouteFromLinkLocalEvent(event);
break;
default:
break;
}
delete event;
return true;
}