-
Notifications
You must be signed in to change notification settings - Fork 390
/
agent_init.cc
510 lines (413 loc) · 13.5 KB
/
agent_init.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
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
#include <cmn/agent_cmn.h>
#include <cmn/agent_factory.h>
#include <init/agent_param.h>
#include <cfg/cfg_init.h>
#include <cfg/cfg_interface.h>
#include <oper/operdb_init.h>
#include <oper/sg.h>
#include <oper/vrf.h>
#include <oper/nexthop.h>
#include <oper/vn.h>
#include <oper/vm.h>
#include <oper/interface.h>
#include <oper/route_common.h>
#include <filter/acl.h>
#include <controller/controller_init.h>
#include "agent_init.h"
AgentInit::AgentInit() :
agent_(new Agent()), agent_param_(NULL), trigger_(),
enable_controller_(true) {
}
AgentInit::~AgentInit() {
TaskScheduler *scheduler = agent_->task_scheduler();
trigger_.reset();
controller_.reset();
cfg_.reset();
oper_.reset();
agent_->db()->ClearFactoryRegistry();
agent_.reset();
scheduler->Terminate();
}
/****************************************************************************
* Initialization routines
****************************************************************************/
void AgentInit::ProcessOptions
(const std::string &config_file, const std::string &program_name) {
agent_param_->Init(config_file, program_name);
}
int AgentInit::ModuleType() {
return Module::VROUTER_AGENT;
}
string AgentInit::ModuleName() {
Module::type module = static_cast<Module::type>(ModuleType());
return g_vns_constants.ModuleNames.find(module)->second;
}
string AgentInit::AgentName() {
return agent_param_->agent_name();
}
string AgentInit::InstanceId() {
return g_vns_constants.INSTANCE_ID_DEFAULT;
}
// Start of Agent init.
// Trigger init in DBTable task context
int AgentInit::Start() {
// Call to GetScheduler::GetInstance() will also create Task Scheduler
if (TaskScheduler::GetInstance() == NULL) {
TaskScheduler::Initialize();
}
agent_->set_task_scheduler(TaskScheduler::GetInstance());
// Copy tunable parameters into agent_
agent_->CopyConfig(agent_param_);
string module_name = ModuleName();
/* Discovery client name should have InstanceId as well because multiple
* instances of tor-agent can run on a single node.
*/
string ds_client_name =
agent_->BuildDiscoveryClientName(module_name, InstanceId());
agent_->set_discovery_client_name(ds_client_name);
agent_->set_agent_name(AgentName());
agent_->set_instance_id(InstanceId());
agent_->set_module_type(ModuleType());
agent_->set_module_name(module_name);
std::string log_property_file = agent_param_->log_property_file();
if (log_property_file.size()) {
LoggingInit(log_property_file);
}
else {
LoggingInit(agent_param_->log_file(), agent_param_->log_file_size(),
agent_param_->log_files_count(), agent_param_->use_syslog(),
agent_param_->syslog_facility(), module_name);
}
agent_param_->LogConfig();
int ret = agent_param_->Validate();
if (ret != 0) {
return ret;
}
agent_param_->PostValidateLogConfig();
int task_id = agent_->task_scheduler()->GetTaskId(AGENT_INIT_TASKNAME);
trigger_.reset(new TaskTrigger(boost::bind(&AgentInit::InitBase, this),
task_id, 0));
trigger_->Set();
return 0;
}
// Start init sequence
bool AgentInit::InitBase() {
FactoryInit();
InitLoggingBase();
CreatePeersBase();
CreateModulesBase();
CreateDBTablesBase();
RegisterDBClientsBase();
InitModulesBase();
InitCollectorBase();
CreateVrfBase();
CreateNextHopsBase();
InitDiscoveryBase();
CreateInterfacesBase();
InitDoneBase();
bool ret = Init();
agent_->set_init_done(true);
ConnectToControllerBase();
return ret;
}
void AgentInit::InitLoggingBase() {
Sandesh::SetLoggingParams(agent_param_->log_local(),
agent_param_->log_category(),
agent_param_->log_level(),
false,
agent_param_->log_flow());
InitLogging();
}
// Connect to collector specified in config, if discovery server is not set
void AgentInit::InitCollectorBase() {
agent_->InitCollector();
InitCollector();
}
// Create peers
void AgentInit::CreatePeersBase() {
agent_->InitPeers();
CreatePeers();
}
// Create the basic modules for agent operation.
// Optional modules or modules that have different implementation are created
// by init module
void AgentInit::CreateModulesBase() {
cfg_.reset(new AgentConfig(agent()));
agent_->set_cfg(cfg_.get());
oper_.reset(new OperDB(agent()));
agent_->set_oper_db(oper_.get());
if (enable_controller_) {
controller_.reset(new VNController(agent()));
agent_->set_controller(controller_.get());
}
CreateModules();
}
void AgentInit::CreateDBTablesBase() {
if (cfg_.get()) {
cfg_->CreateDBTables(agent_->db());
}
if (oper_.get()) {
oper_->CreateDBTables(agent_->db());
}
CreateDBTables();
}
void AgentInit::RegisterDBClientsBase() {
if (cfg_.get()) {
cfg_->RegisterDBClients(agent_->db());
}
if (oper_.get()) {
oper_->RegisterDBClients();
}
RegisterDBClients();
}
void AgentInit::InitModulesBase() {
if (cfg_.get()) {
cfg_->Init();
}
if (oper_.get()) {
oper_->Init();
}
InitModules();
}
static void CreateVrfIndependentNextHop(Agent *agent) {
DiscardNH::Create();
DiscardNHKey key1;
NextHop *nh = static_cast<NextHop *>
(agent->nexthop_table()->FindActiveEntry(&key1));
agent->nexthop_table()->set_discard_nh(nh);
//Reserve index 2, this would be used to
//set as RPF NH when packet has to be discarded
//due to source route mismatch
assert(agent->nexthop_table()->ReserveIndex() ==
NextHopTable::kRpfDiscardIndex);
L2ReceiveNH::Create();
L2ReceiveNHKey key2;
nh = static_cast<NextHop *>
(agent->nexthop_table()->FindActiveEntry(&key2));
agent->nexthop_table()->set_l2_receive_nh(nh);
}
void AgentInit::CreateVrfBase() {
// Bridge Receive routes are added on VRF creation. Ensure that Bridge
// Receive-NH which is independent of VRF is created first
CreateVrfIndependentNextHop(agent_.get());
// Create the default VRF
VrfTable *vrf_table = agent_->vrf_table();
vrf_table->CreateStaticVrf(agent_->fabric_vrf_name());
VrfEntry *vrf = vrf_table->FindVrfFromName(agent_->fabric_vrf_name());
assert(vrf);
agent_->set_fabric_inet4_unicast_table(vrf->GetInet4UnicastRouteTable());
agent_->set_fabric_inet4_multicast_table
(vrf->GetInet4MulticastRouteTable());
agent_->set_fabric_l2_unicast_table(vrf->GetBridgeRouteTable());
agent_->set_fabric_evpn_table(vrf->GetEvpnRouteTable());
CreateVrf();
}
void AgentInit::CreateNextHopsBase() {
CreateNextHops();
}
void AgentInit::CreateInterfacesBase() {
CreateInterfaces();
}
void AgentInit::InitDiscoveryBase() {
if (cfg_.get()) {
cfg_->InitDiscovery();
}
InitDiscovery();
}
void AgentInit::ConnectToControllerBase() {
if (agent_->router_id_configured() == false) {
LOG(DEBUG,
"Router ID not configured. Connection to controller postponed");
} else {
LOG(DEBUG, "Router ID configured. Connection to controller initiated");
// Connect to controller and DNS servers
agent_->controller()->Connect();
}
ConnectToController();
}
void AgentInit::InitDoneBase() {
if (cfg_.get()) {
cfg_->InitDone();
}
InitDone();
}
/****************************************************************************
* Shutdown routines
***************************************************************************/
typedef boost::function<bool(void)> TaskFnPtr;
static void RunInTaskContext(AgentInit *init, uint32_t task_id, TaskFnPtr fn) {
TaskTrigger trigger(fn, task_id, 0);
trigger.Set();
init->WaitForIdle();
return;
}
// Shutdown IO channel to controller+DNS
void AgentInit::IoShutdownBase() {
agent_->controller()->Cleanup();
agent_->controller()->DisConnect();
IoShutdown();
}
static bool IoShutdownInternal(AgentInit *init) {
init->IoShutdownBase();
return true;
}
void AgentInit::FlushFlowsBase() {
FlushFlows();
return;
}
static bool FlushFlowsInternal(AgentInit *init) {
init->FlushFlowsBase();
return true;
}
void AgentInit::VgwShutdownBase() {
VgwShutdown();
return;
}
static bool VgwShutdownInternal(AgentInit *init) {
init->VgwShutdownBase();
return true;
}
void AgentInit::DeleteRoutesBase() {
DeleteRoutes();
if (agent_->oper_db())
agent_->oper_db()->DeleteRoutes();
}
static bool DeleteRoutesInternal(AgentInit *init) {
init->DeleteRoutesBase();
return true;
}
static bool FlushTable(AgentDBTable *table, DBTableWalker *walker) {
table->Flush(walker);
return true;
}
void AgentInit::DeleteDBEntriesBase() {
DBTableWalker walker;
int task_id = agent_->task_scheduler()->GetTaskId(AGENT_INIT_TASKNAME);
RunInTaskContext(this, task_id, boost::bind(&DeleteRoutesInternal, this));
RunInTaskContext(this, task_id,
boost::bind(&FlushTable, agent_->interface_table(),
&walker));
agent_->set_vhost_interface(NULL);
RunInTaskContext(this, task_id,
boost::bind(&FlushTable, agent_->vm_table(), &walker));
RunInTaskContext(this, task_id,
boost::bind(&FlushTable, agent_->vn_table(), &walker));
agent_->vrf_table()->DeleteStaticVrf(agent_->fabric_vrf_name());
RunInTaskContext(this, task_id,
boost::bind(&FlushTable, agent_->vrf_table(), &walker));
RunInTaskContext(this, task_id,
boost::bind(&FlushTable, agent_->nexthop_table(),
&walker));
agent_->nexthop_table()->set_discard_nh(NULL);
RunInTaskContext(this, task_id,
boost::bind(&FlushTable, agent_->sg_table(), &walker));
RunInTaskContext(this, task_id,
boost::bind(&FlushTable, agent_->acl_table(), &walker));
}
static bool WaitForDbCount(DBTableBase *table, AgentInit *init,
uint32_t count, int msec) {
while ((table->Size() > count) && (msec > 0)) {
init->WaitForIdle();
usleep(1000);
msec -= 1;
}
return (table->Size() == count);
}
void AgentInit::WaitForDBEmpty() {
WaitForDbCount(agent_->interface_table(), this, 0, 10000);
WaitForDbCount(agent_->vrf_table(), this, 0, 10000);
WaitForDbCount(agent_->nexthop_table(), this, 0, 10000);
WaitForDbCount(agent_->vm_table(), this, 0, 10000);
WaitForDbCount(agent_->vn_table(), this, 0, 10000);
WaitForDbCount(agent_->mpls_table(), this, 0, 10000);
WaitForDbCount(agent_->interface_config_table(), this, 0, 10000);
WaitForDbCount(agent_->acl_table(), this, 0, 10000);
}
void AgentInit::ServicesShutdownBase() {
ServicesShutdown();
return;
}
static bool ServicesShutdownInternal(AgentInit *init) {
init->ServicesShutdownBase();
return true;
}
void AgentInit::PktShutdownBase() {
PktShutdown();
return;
}
static bool PktShutdownInternal(AgentInit *init) {
init->PktShutdownBase();
return true;
}
void AgentInit::ModulesShutdownBase() {
ModulesShutdown();
if (agent_->oper_db()) {
agent_->oper_db()->Shutdown();
}
if (agent_->cfg()) {
agent_->cfg()->Shutdown();
}
return;
}
static bool ModulesShutdownInternal(AgentInit *init) {
init->ModulesShutdownBase();
return true;
}
void AgentInit::UveShutdownBase() {
UveShutdown();
return;
}
void AgentInit::StatsCollectorShutdownBase() {
StatsCollectorShutdown();
return;
}
void AgentInit::FlowStatsCollectorShutdownBase() {
FlowStatsCollectorShutdown();
return;
}
static bool UveShutdownInternal(AgentInit *init) {
init->UveShutdownBase();
return true;
}
static bool StatsCollectorShutdownInternal(AgentInit *init) {
init->StatsCollectorShutdownBase();
return true;
}
static bool FlowStatsCollectorShutdownInternal(AgentInit *init) {
init->FlowStatsCollectorShutdownBase();
return true;
}
void AgentInit::KSyncShutdownBase() {
KSyncShutdown();
return;
}
static bool KSyncShutdownInternal(AgentInit *init) {
init->KSyncShutdownBase();
return true;
}
void AgentInit::Shutdown() {
int task_id = agent_->task_scheduler()->GetTaskId(AGENT_INIT_TASKNAME);
RunInTaskContext(this, task_id, boost::bind(&IoShutdownInternal, this));
RunInTaskContext(this, task_id, boost::bind(&FlushFlowsInternal, this));
RunInTaskContext(this, task_id, boost::bind(&VgwShutdownInternal, this));
DeleteDBEntriesBase();
WaitForDBEmpty();
RunInTaskContext(this, task_id, boost::bind(&ServicesShutdownInternal,
this));
RunInTaskContext(this, task_id, boost::bind
(&FlowStatsCollectorShutdownInternal, this));
RunInTaskContext(this, task_id, boost::bind(&StatsCollectorShutdownInternal,
this));
RunInTaskContext(this, task_id, boost::bind(&UveShutdownInternal, this));
RunInTaskContext(this, task_id, boost::bind(&PktShutdownInternal, this));
RunInTaskContext(this, task_id, boost::bind(&ModulesShutdownInternal,
this));
RunInTaskContext(this, task_id, boost::bind(&KSyncShutdownInternal, this));
Sandesh::Uninit();
WaitForIdle();
agent_->event_manager()->Shutdown();
WaitForIdle();
}