-
Notifications
You must be signed in to change notification settings - Fork 390
/
agent_param.cc
1327 lines (1193 loc) · 49.5 KB
/
agent_param.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
/*
* Agent parameters are derived from 3 entities in increasing priority,
* - System information
* - Configuration file
* - Parameters
*/
#include "base/os.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <net/if_arp.h>
#include <unistd.h>
#include <iostream>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <boost/program_options.hpp>
#include <base/logging.h>
#include <base/misc_utils.h>
#include <sandesh/sandesh_types.h>
#include <sandesh/sandesh.h>
#include <sandesh/sandesh_trace.h>
#include <cmn/agent_cmn.h>
#include <init/agent_param.h>
#include <vgw/cfg_vgw.h>
using namespace std;
using namespace boost::property_tree;
using boost::optional;
namespace opt = boost::program_options;
template <typename ElementType>
bool AgentParam::GetOptValueImpl(
const boost::program_options::variables_map &var_map,
std::vector<ElementType> &var, const std::string &val,
std::vector<ElementType>*) {
// Check if the value is present.
if (var_map.count(val) && !var_map[val].defaulted()) {
std::vector<ElementType> tmp(
var_map[val].as<std::vector<ElementType> >());
// Now split the individual elements
for (typename std::vector<ElementType>::const_iterator it =
tmp.begin();
it != tmp.end(); it++) {
std::stringstream ss(*it);
std::copy(istream_iterator<ElementType>(ss),
istream_iterator<ElementType>(),
std::back_inserter(var));
}
return true;
}
return false;
}
bool AgentParam::GetIpAddress(const string &str, Ip4Address *addr) {
boost::system::error_code ec;
Ip4Address tmp = Ip4Address::from_string(str, ec);
if (ec.value() != 0) {
return false;
}
*addr = tmp;
return true;
}
bool AgentParam::ParseIp(const string &key, Ip4Address *server) {
optional<string> opt_str;
if (opt_str = tree_.get_optional<string>(key)) {
Ip4Address addr;
if (GetIpAddress(opt_str.get(), &addr) == false) {
LOG(ERROR, "Error in config file <" << config_file_
<< ">. Error parsing IP address from <"
<< opt_str.get() << ">");
return false;
} else {
*server = addr;
}
}
return true;
}
bool AgentParam::ParseServerList(const string &key, Ip4Address *server1,
Ip4Address *server2) {
optional<string> opt_str;
Ip4Address addr;
vector<string> tokens;
if (opt_str = tree_.get_optional<string>(key)) {
boost::split(tokens, opt_str.get(), boost::is_any_of(" \t"));
if (tokens.size() > 2) {
LOG(ERROR, "Error in config file <" << config_file_
<< ">. Cannot have more than 2 servers <"
<< opt_str.get() << ">");
return false;
}
vector<string>::iterator it = tokens.begin();
if (it != tokens.end()) {
if (!GetIpAddress(*it, server1)) {
return false;
}
++it;
if (it != tokens.end()) {
if (!GetIpAddress(*it, server2)) {
return false;
}
}
}
}
return true;
}
// Parse address string in the form <ip>:<port>
bool AgentParam::ParseAddress(const std::string &addr_string,
Ip4Address *server, uint16_t *port) {
vector<string> tokens;
boost::split(tokens, addr_string, boost::is_any_of(":"));
if (tokens.size() > 2) {
cout << "Error in config file <" << config_file_
<< ">. Improper server address <" << addr_string << ">\n";
return false;
}
vector<string>::iterator it = tokens.begin();
if (!GetIpAddress(*it, server)) {
cout << "Error in config file <" << config_file_
<< ">. Improper server address <" << addr_string << ">\n";
return false;
}
++it;
if (it != tokens.end()) {
stringToInteger(*it, *port);
}
return true;
}
// Parse list of servers in the <ip1>:<port1> <ip2>:<port2> format
bool AgentParam::ParseServerList(const std::string &key,
Ip4Address *server1, uint16_t *port1,
Ip4Address *server2, uint16_t *port2) {
optional<string> opt_str;
if (opt_str = tree_.get_optional<string>(key)) {
vector<string> tokens;
boost::split(tokens, opt_str.get(), boost::is_any_of(" \t"));
if (tokens.size() > 2) {
cout << "Error in config file <" << config_file_
<< ">. Cannot have more than 2 DNS servers <"
<< opt_str.get() << ">\n";
return false;
}
vector<string>::iterator it = tokens.begin();
if (it != tokens.end()) {
if (!ParseAddress(*it, server1, port1))
return false;
++it;
if (it != tokens.end()) {
return ParseAddress(*it, server2, port2);
}
}
}
return true;
}
void AgentParam::ParseIpArgument
(const boost::program_options::variables_map &var_map, Ip4Address &server,
const string &key) {
if (var_map.count(key)) {
Ip4Address addr;
if (GetIpAddress(var_map[key].as<string>(), &addr)) {
server = addr;
}
}
}
bool AgentParam::ParseServerListArguments
(const boost::program_options::variables_map &var_map, Ip4Address &server1,
Ip4Address &server2, const string &key) {
if (var_map.count(key)) {
vector<string> value = var_map[key].as<vector<string> >();
if (value.size() > 2) {
cout << "Error in Arguments. Cannot have more than 2 servers for "
<< key << "\n";
return false;
}
vector<string>::iterator it = value.begin();
Ip4Address addr;
if (it != value.end()) {
if (GetIpAddress(*it, &addr)) {
server1 = addr;
}
++it;
if (it != value.end()) {
if (GetIpAddress(*it, &addr)) {
server2 = addr;
}
}
}
}
return true;
}
bool AgentParam::ParseServerListArguments
(const boost::program_options::variables_map &var_map, Ip4Address *server1,
uint16_t *port1, Ip4Address *server2, uint16_t *port2,
const std::string &key) {
if (var_map.count(key)) {
vector<string> value = var_map[key].as<vector<string> >();
if (value.size() > 2) {
LOG(ERROR, "Error in Arguments. Cannot have more than 2 servers "
"for " << key );
return false;
}
vector<string>::iterator it = value.begin();
if (it != value.end()) {
if (!ParseAddress(*it, server1, port1))
return false;
++it;
if (it != value.end()) {
return ParseAddress(*it, server2, port2);
}
}
}
return true;
}
void AgentParam::ParseCollector() {
optional<string> opt_str;
if (opt_str = tree_.get_optional<string>("DEFAULT.collectors")) {
boost::split(collector_server_list_, opt_str.get(),
boost::is_any_of(" "));
}
}
void AgentParam::BuildAddressList(const string &val) {
compute_node_address_list_.clear();
if (val.empty()) {
return;
}
vector<string> tokens;
boost::split(tokens, val, boost::is_any_of(" "));
vector<string>::iterator it = tokens.begin();
while (it != tokens.end()) {
std::string str = *it;
++it;
boost::algorithm::trim(str);
Ip4Address addr;
if (GetIpAddress(str, &addr)) {
compute_node_address_list_.push_back(addr);
} else {
LOG(ERROR, "Error in parsing address " << *it);
}
}
}
void AgentParam::ParseVirtualHost() {
boost::system::error_code ec;
optional<string> opt_str;
GetValueFromTree<string>(vhost_.name_, "VIRTUAL-HOST-INTERFACE.name");
if (opt_str = tree_.get_optional<string>("VIRTUAL-HOST-INTERFACE.ip")) {
ec = Ip4PrefixParse(opt_str.get(), &vhost_.addr_, &vhost_.plen_);
if (ec != 0 || vhost_.plen_ > 32) {
cout << "Error in config file <" << config_file_
<< ">. Error parsing vhost ip-address from <"
<< opt_str.get() << ">\n";
}
}
if (opt_str = tree_.get_optional<string>("VIRTUAL-HOST-INTERFACE.gateway")) {
if (GetIpAddress(opt_str.get(), &vhost_.gw_) == false) {
cout << "Error in config file <" << config_file_
<< ">. Error parsing vhost gateway address from <"
<< opt_str.get() << ">\n";
}
}
GetValueFromTree<string>(eth_port_,
"VIRTUAL-HOST-INTERFACE.physical_interface");
if (opt_str = tree_.get_optional<string>
("VIRTUAL-HOST-INTERFACE.compute_node_address")) {
BuildAddressList(opt_str.get());
}
}
void AgentParam::ParseDiscovery() {
GetValueFromTree<string>(dss_server_, "DISCOVERY.server");
GetValueFromTree<uint16_t>(dss_port_, "DISCOVERY.port");
GetValueFromTree<uint16_t>(xmpp_instance_count_,
"DISCOVERY.max_control_nodes");
}
void AgentParam::ParseNetworks() {
ParseIp("NETWORKS.control_network_ip", &mgmt_ip_);
}
void AgentParam::ParseHypervisor() {
optional<string> opt_str;
if (opt_str = tree_.get_optional<string>("HYPERVISOR.type")) {
// Initialize mode to KVM. Will be overwritten for XEN later
hypervisor_mode_ = AgentParam::MODE_KVM;
if (opt_str.get() == "xen") {
hypervisor_mode_ = AgentParam::MODE_XEN;
GetValueFromTree<string>(xen_ll_.name_,
"HYPERVISOR.xen_ll_interface");
boost::system::error_code ec;
if (opt_str = tree_.get_optional<string>
("HYPERVISOR.xen_ll_ip")) {
ec = Ip4PrefixParse(opt_str.get(), &xen_ll_.addr_,
&xen_ll_.plen_);
if (ec != 0 || xen_ll_.plen_ >= 32) {
cout << "Error in config file <" << config_file_
<< ">. Error parsing Xen Link-local ip-address from <"
<< opt_str.get() << ">\n";
return;
}
}
} else if (opt_str.get() == "vmware") {
hypervisor_mode_ = AgentParam::MODE_VMWARE;
GetValueFromTree<string>(vmware_physical_port_,
"HYPERVISOR.vmware_physical_interface");
} else if (opt_str.get() == "docker") {
hypervisor_mode_ = AgentParam::MODE_DOCKER;
} else {
hypervisor_mode_ = AgentParam::MODE_KVM;
}
}
if (opt_str = tree_.get_optional<string>("HYPERVISOR.vmware_mode")) {
if (opt_str.get() == "vcenter") {
vmware_mode_ = VCENTER;
} else if (opt_str.get() == "esxi_neutron") {
vmware_mode_ = ESXI_NEUTRON;
} else {
cout << "Error in config file <" << config_file_ <<
">. Error parsing vmware_mode from <"
<< opt_str.get() << ">\n";
return;
}
}
}
void AgentParam::ParsePlatform() {
std::string vrouter_platform;
GetValueFromTree<string>(vrouter_platform, "DEFAULT.platform");
if (vrouter_platform=="nic") {
platform_ = AgentParam::VROUTER_ON_NIC;
} else if (vrouter_platform=="dpdk") {
platform_ = AgentParam::VROUTER_ON_HOST_DPDK;
GetValueFromTree<string>(physical_interface_pci_addr_,
"DEFAULT.physical_interface_address");
GetValueFromTree<string>(physical_interface_mac_addr_,
"DEFAULT.physical_interface_mac");
} else {
platform_ = AgentParam::VROUTER_ON_HOST;
}
}
void AgentParam::ParseDefaultSection() {
optional<string> opt_str;
optional<unsigned int> opt_uint;
GetValueFromTree<string>(host_name_, "DEFAULT.hostname");
GetValueFromTree<string>(agent_name_, "DEFAULT.agent_name");
if (!GetValueFromTree<uint16_t>(http_server_port_,
"DEFAULT.http_server_port")) {
http_server_port_ = ContrailPorts::HttpPortAgent();
}
GetValueFromTree<string>(tunnel_type_, "DEFAULT.tunnel_type");
if ((tunnel_type_ != "MPLSoUDP") && (tunnel_type_ != "VXLAN"))
tunnel_type_ = "MPLSoGRE";
if (!GetValueFromTree<uint16_t>(flow_cache_timeout_,
"DEFAULT.flow_cache_timeout")) {
flow_cache_timeout_ = Agent::kDefaultFlowCacheTimeout;
}
if (!GetValueFromTree<string>(log_level_, "DEFAULT.log_level")) {
log_level_ = "SYS_DEBUG";
}
if (!GetValueFromTree<string>(log_file_, "DEFAULT.log_file")) {
log_file_ = Agent::GetInstance()->log_file();
}
if (!GetValueFromTree<int>(log_files_count_, "DEFAULT.log_files_count")) {
log_files_count_ = 10;
}
if (!GetValueFromTree<long>(log_file_size_, "DEFAULT.log_file_size")) {
log_file_size_ = 1024*1024;
}
if (!GetValueFromTree<string>(log_category_, "DEFAULT.log_category")) {
log_category_ = "";
}
if (optional<bool> log_local_opt =
tree_.get_optional<bool>("DEFAULT.log_local")) {
log_local_ = true;
} else {
log_local_ = false;
}
if (optional<bool> debug_opt =
tree_.get_optional<bool>("DEFAULT.debug")) {
debug_ = true;
} else {
debug_ = false;
}
GetValueFromTree<bool>(use_syslog_, "DEFAULT.use_syslog");
if (!GetValueFromTree<string>(syslog_facility_, "DEFAULT.syslog_facility")) {
syslog_facility_ = "LOG_LOCAL0";
}
if (optional<bool> log_flow_opt =
tree_.get_optional<bool>("DEFAULT.log_flow")) {
log_flow_ = true;
} else {
log_flow_ = false;
}
GetValueFromTree<bool>(disable_flow_collection_, "DEFAULT.disable_flow_collection");
if (!GetValueFromTree<bool>(xmpp_auth_enable_, "DEFAULT.xmpp_auth_enable")) {
xmpp_auth_enable_ = false;
}
if (!GetValueFromTree<string>(xmpp_server_cert_, "DEFAULT.xmpp_server_cert")) {
xmpp_server_cert_ = "/etc/contrail/ssl/certs/server.pem";
}
if (!GetValueFromTree<string>(log_property_file_, "DEFAULT.log_property_file")) {
log_property_file_ = "";
}
if (optional<bool> subnet_hosts_resolvable_opt =
tree_.get_optional<bool>("DEFAULT.subnet_hosts_resolvable")) {
subnet_hosts_resolvable_ = *subnet_hosts_resolvable_opt;
} else {
subnet_hosts_resolvable_ = true;
}
}
void AgentParam::ParseTaskSection() {
if (!GetValueFromTree<uint32_t>(tbb_exec_delay_,
"TASK.log_exec_threshold")) {
tbb_exec_delay_ = 0;
}
if (!GetValueFromTree<uint32_t>(tbb_schedule_delay_,
"TASK.log_schedule_threshold")) {
tbb_schedule_delay_ = 0;
}
if (!GetValueFromTree<uint32_t>(tbb_keepawake_timeout_,
"TASK.tbb_keepawake_timeout")) {
tbb_keepawake_timeout_ = Agent::kDefaultTbbKeepawakeTimeout;
}
}
void AgentParam::ParseMetadataProxy() {
GetValueFromTree<string>(metadata_shared_secret_,
"METADATA.metadata_proxy_secret");
}
void AgentParam::ParseFlows() {
if (!GetValueFromTree<float>(max_vm_flows_, "FLOWS.max_vm_flows")) {
max_vm_flows_ = (float) 100;
}
if (!GetValueFromTree<uint16_t>(linklocal_system_flows_,
"FLOWS.max_system_linklocal_flows")) {
linklocal_system_flows_ = Agent::kDefaultMaxLinkLocalOpenFds;
}
if (!GetValueFromTree<uint16_t>(linklocal_vm_flows_,
"FLOWS.max_vm_linklocal_flows")) {
linklocal_vm_flows_ = Agent::kDefaultMaxLinkLocalOpenFds;
}
}
void AgentParam::ParseHeadlessMode() {
if (!GetValueFromTree<bool>(headless_mode_, "DEFAULT.headless_mode")) {
headless_mode_ = false;
}
}
void AgentParam::ParseDhcpRelayMode() {
if (!GetValueFromTree<bool>(dhcp_relay_mode_, "DEFAULT.dhcp_relay_mode")) {
dhcp_relay_mode_ = false;
}
}
void AgentParam::ParseAgentInfo() {
std::string mode;
GetValueFromTree<string>(mode, "DEFAULT.agent_mode");
set_agent_mode(mode);
if (!GetValueFromTree<string>(agent_base_dir_,
"DEFAULT.agent_base_directory")) {
agent_base_dir_ = "/var/lib/contrail";
}
}
void AgentParam::set_agent_mode(const std::string &mode) {
std::string agent_mode = boost::to_lower_copy(mode);
if (agent_mode == "tsn")
agent_mode_ = TSN_AGENT;
else if (agent_mode == "tor")
agent_mode_ = TOR_AGENT;
else
agent_mode_ = VROUTER_AGENT;
}
void AgentParam::ParseSimulateEvpnTor() {
if (!GetValueFromTree<bool>(simulate_evpn_tor_,
"DEFAULT.simulate_evpn_tor")) {
simulate_evpn_tor_ = false;
}
}
void AgentParam::ParseServiceInstance() {
GetValueFromTree<string>(si_netns_command_,
"SERVICE-INSTANCE.netns_command");
GetValueFromTree<string>(si_docker_command_,
"SERVICE-INSTANCE.docker_command");
GetValueFromTree<int>(si_netns_workers_,
"SERVICE-INSTANCE.netns_workers");
GetValueFromTree<int>(si_netns_timeout_,
"SERVICE-INSTANCE.netns_timeout");
GetValueFromTree<string>(si_lb_ssl_cert_path_,
"SERVICE-INSTANCE.lb_ssl_cert_path");
GetValueFromTree<string>(si_lb_custom_attr_conf_path_,
"SERVICE-INSTANCE.lb_custom_attr_conf_path");
}
void AgentParam::ParseNexthopServer() {
GetValueFromTree<string>(nexthop_server_endpoint_,
"NEXTHOP-SERVER.endpoint");
GetValueFromTree<bool>(nexthop_server_add_pid_,
"NEXTHOP-SERVER.add_pid");
if (nexthop_server_add_pid_) {
std::stringstream ss;
ss << nexthop_server_endpoint_ << "." << getpid();
nexthop_server_endpoint_ = ss.str();
}
}
void AgentParam::ParseCollectorArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue< vector<string> >(var_map, collector_server_list_,
"DEFAULT.collectors");
}
void AgentParam::ParseVirtualHostArguments
(const boost::program_options::variables_map &var_map) {
boost::system::error_code ec;
GetOptValue<string>(var_map, vhost_.name_, "VIRTUAL-HOST-INTERFACE.name");
string ip;
if (GetOptValue<string>(var_map, ip, "VIRTUAL-HOST-INTERFACE.ip")) {
ec = Ip4PrefixParse(ip, &vhost_.addr_, &vhost_.plen_);
if (ec != 0 || vhost_.plen_ >= 32) {
cout << "Error parsing vhost ip argument from <" << ip << ">\n";
}
}
ParseIpArgument(var_map, vhost_.gw_, "VIRTUAL-HOST-INTERFACE.gateway");
GetOptValue<string>(var_map, eth_port_,
"VIRTUAL-HOST-INTERFACE.physical_interface");
}
void AgentParam::ParseDiscoveryArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue<string>(var_map, dss_server_, "DISCOVERY.server");
GetOptValue<uint16_t>(var_map, dss_port_, "DISCOVERY.port");
if (!GetOptValue<uint16_t>(var_map, xmpp_instance_count_,
"DISCOVERY.max_control_nodes")) {
xmpp_instance_count_ = MAX_XMPP_SERVERS;
}
}
void AgentParam::ParseNetworksArguments
(const boost::program_options::variables_map &var_map) {
ParseIpArgument(var_map, mgmt_ip_, "NETWORKS.control_network_ip");
}
void AgentParam::ParseHypervisorArguments
(const boost::program_options::variables_map &var_map) {
boost::system::error_code ec;
if (var_map.count("HYPERVISOR.type") &&
!var_map["HYPERVISOR.type"].defaulted()) {
if (var_map["HYPERVISOR.type"].as<string>() == "xen") {
hypervisor_mode_ = AgentParam::MODE_XEN;
GetOptValue<string>(var_map, xen_ll_.name_,
"HYPERVISOR.xen_ll_interface");
if (var_map.count("HYPERVISOR.xen_ll_ip")) {
string ip = var_map["HYPERVISOR.xen_ll_ip"].as<string>();
ec = Ip4PrefixParse(ip, &xen_ll_.addr_, &xen_ll_.plen_);
if (ec != 0 || xen_ll_.plen_ >= 32) {
cout << "Error in argument <" << config_file_
<< ">. Error parsing Xen Link-local ip-address from <"
<< ip << ">\n";
exit(EINVAL);
}
}
} else if (var_map["HYPERVISOR.type"].as<string>() == "vmware") {
hypervisor_mode_ = AgentParam::MODE_VMWARE;
GetOptValue<string>(var_map, vmware_physical_port_,
"HYPERVISOR.vmware_physical_interface");
} else {
hypervisor_mode_ = AgentParam::MODE_KVM;
}
}
if (var_map.count("HYPERVISOR.vmware_mode") &&
!var_map["HYPERVISOR.vmware_mode"].defaulted()) {
cout << " vmware_mode is " << var_map["HYPERVISOR.vmware_mode"].as<string>() << endl;
if (var_map["HYPERVISOR.vmware_mode"].as<string>() == "vcenter") {
vmware_mode_ = VCENTER;
} else if (var_map["HYPERVISOR.vmware_mode"].as<string>() ==
"esxi_neutron") {
vmware_mode_ = ESXI_NEUTRON;
} else {
cout << "Error in parsing arguement for HYPERVISOR.vmware_mode <"
<< var_map["HYPERVISOR.vmware_mode"].as<string>() << endl;
return;
}
}
}
void AgentParam::ParseDefaultSectionArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue<uint16_t>(var_map, flow_cache_timeout_,
"DEFAULT.flow_cache_timeout");
GetOptValue<string>(var_map, host_name_, "DEFAULT.hostname");
GetOptValue<string>(var_map, agent_name_, "DEFAULT.agent_name");
GetOptValue<uint16_t>(var_map, http_server_port_,
"DEFAULT.http_server_port");
GetOptValue<string>(var_map, log_category_, "DEFAULT.log_category");
GetOptValue<string>(var_map, log_file_, "DEFAULT.log_file");
GetValueFromTree<int>(log_files_count_, "DEFAULT.log_files_count");
GetValueFromTree<long>(log_file_size_, "DEFAULT.log_file_size");
GetOptValue<string>(var_map, log_level_, "DEFAULT.log_level");
GetOptValue<string>(var_map, syslog_facility_, "DEFAULT.syslog_facility");
if (var_map.count("DEFAULT.use_syslog")) {
use_syslog_ = true;
}
if (var_map.count("DEFAULT.log_local")) {
log_local_ = true;
}
if (var_map.count("DEFAULT.debug")) {
debug_ = true;
}
if (var_map.count("DEFAULT.log_flow")) {
log_flow_ = true;
}
GetOptValue<bool>(var_map, disable_flow_collection_, "DEFAULT.disable_flow_collection");
GetOptValue<bool>(var_map, xmpp_auth_enable_, "DEFAULT.xmpp_auth_enable");
GetOptValue<string>(var_map, xmpp_server_cert_, "DEFAULT.xmpp_server_cert");
GetOptValue<bool>(var_map, subnet_hosts_resolvable_,
"DEFAULT.subnet_hosts_resolvable");
}
void AgentParam::ParseTaskSectionArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue<uint32_t>(var_map, tbb_exec_delay_,
"TASK.log_exec_threshold");
GetOptValue<uint32_t>(var_map, tbb_schedule_delay_,
"TASK.log_schedule_threshold");
GetOptValue<uint32_t>(var_map, tbb_keepawake_timeout_,
"TASK.tbb_keepawake_timeout");
}
void AgentParam::ParseMetadataProxyArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue<string>(var_map, metadata_shared_secret_,
"METADATA.metadata_proxy_secret");
}
void AgentParam::ParseFlowArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue<float>(var_map, max_vm_flows_, "FLOWS.max_vm_flows");
GetOptValue<uint16_t>(var_map, linklocal_system_flows_,
"FLOWS.max_system_linklocal_flows");
GetOptValue<uint16_t>(var_map, linklocal_vm_flows_,
"FLOWS.max_vm_linklocal_flows");
}
void AgentParam::ParseHeadlessModeArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue<bool>(var_map, headless_mode_, "DEFAULT.headless_mode");
}
void AgentParam::ParseDhcpRelayModeArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue<bool>(var_map, dhcp_relay_mode_, "DEFAULT.dhcp_relay_mode");
}
void AgentParam::ParseAgentInfoArguments
(const boost::program_options::variables_map &var_map) {
std::string mode;
if (GetOptValue<string>(var_map, mode, "DEFAULT.agent_mode")) {
set_agent_mode(mode);
}
GetOptValue<string>(var_map, agent_base_dir_,
"DEFAULT.agent_base_directory");
}
void AgentParam::ParseServiceInstanceArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue<string>(var_map, si_netns_command_, "SERVICE-INSTANCE.netns_command");
GetOptValue<string>(var_map, si_docker_command_, "SERVICE-INSTANCE.docker_command");
GetOptValue<int>(var_map, si_netns_workers_, "SERVICE-INSTANCE.netns_workers");
GetOptValue<int>(var_map, si_netns_timeout_, "SERVICE-INSTANCE.netns_timeout");
GetOptValue<string>(var_map, si_lb_ssl_cert_path_,
"SERVICE-INSTANCE.lb_ssl_cert_path");
}
void AgentParam::ParseNexthopServerArguments
(const boost::program_options::variables_map &var_map) {
GetOptValue<string>(var_map, nexthop_server_endpoint_,
"NEXTHOP-SERVER.endpoint");
GetOptValue<bool>(var_map, nexthop_server_add_pid_,
"NEXTHOP-SERVER.add_pid");
if (nexthop_server_add_pid_) {
std::stringstream ss;
ss << nexthop_server_endpoint_ << "." << getpid();
nexthop_server_endpoint_ = ss.str();
}
}
void AgentParam::ParsePlatformArguments
(const boost::program_options::variables_map &var_map) {
boost::system::error_code ec;
if (var_map.count("DEFAULT.platform") &&
!var_map["DEFAULT.platform"].defaulted()) {
if (var_map["DEFAULT.platform"].as<string>() == "nic") {
platform_ = AgentParam::VROUTER_ON_NIC;
} else if (var_map["DEFAULT.platform"].as<string>() == "dpdk") {
platform_ = AgentParam::VROUTER_ON_HOST_DPDK;
if (var_map.count("DEFAULT.physical_interface_address")) {
physical_interface_pci_addr_ =
var_map["DEFAULT.physical_interface_address"].as<string>();
physical_interface_mac_addr_ =
var_map["DEFAULT.physical_interface_mac"].as<string>();
}
} else {
platform_ = AgentParam::VROUTER_ON_HOST;
}
}
}
// Initialize hypervisor mode based on system information
// If "/proc/xen" exists it means we are running in Xen dom0
void AgentParam::InitFromSystem() {
boost::system::error_code error;
host_name_ = boost::asio::ip::host_name(error);
agent_name_ = host_name_;
struct stat fstat;
if (stat("/proc/xen", &fstat) == 0) {
hypervisor_mode_ = MODE_XEN;
cout << "Found file /proc/xen. Initializing mode to XEN\n";
}
xen_ll_.addr_ = Ip4Address::from_string("169.254.0.1");
xen_ll_.plen_ = 16;
return;
}
// Update agent parameters from config file
void AgentParam::InitFromConfig() {
// Read and parse INI
try {
read_ini(config_file_, tree_);
} catch (exception &e) {
cout << "Error reading config file <" << config_file_
<< ">. INI format error??? <" << e.what() << ">\n";
return;
}
ParseCollector();
ParseVirtualHost();
ParseServerList("CONTROL-NODE.server", &xmpp_server_1_, &xmpp_server_2_);
ParseServerList("DNS.server", &dns_server_1_, &dns_port_1_,
&dns_server_2_, &dns_port_2_);
ParseDiscovery();
ParseNetworks();
ParseHypervisor();
ParseDefaultSection();
ParseTaskSection();
ParseMetadataProxy();
ParseFlows();
ParseHeadlessMode();
ParseDhcpRelayMode();
ParseSimulateEvpnTor();
ParseServiceInstance();
ParseAgentInfo();
ParseNexthopServer();
ParsePlatform();
cout << "Config file <" << config_file_ << "> parsing completed.\n";
return;
}
void AgentParam::InitFromArguments() {
ParseCollectorArguments(var_map_);
ParseVirtualHostArguments(var_map_);
ParseServerListArguments(var_map_, xmpp_server_1_, xmpp_server_2_,
"CONTROL-NODE.server");
ParseServerListArguments(var_map_, &dns_server_1_, &dns_port_1_,
&dns_server_2_, &dns_port_2_, "DNS.server");
ParseDiscoveryArguments(var_map_);
ParseNetworksArguments(var_map_);
ParseHypervisorArguments(var_map_);
ParseDefaultSectionArguments(var_map_);
ParseTaskSectionArguments(var_map_);
ParseMetadataProxyArguments(var_map_);
ParseHeadlessModeArguments(var_map_);
ParseDhcpRelayModeArguments(var_map_);
ParseServiceInstanceArguments(var_map_);
ParseAgentInfoArguments(var_map_);
ParseNexthopServerArguments(var_map_);
ParsePlatformArguments(var_map_);
return;
}
// Update max_vm_flows_ if it is greater than 100.
// Update linklocal max flows if they are greater than the max allowed for the
// process. Also, ensure that the process is allowed to open upto
// linklocal_system_flows + kMaxOtherOpenFds files
void AgentParam::ComputeFlowLimits() {
if (max_vm_flows_ > 100) {
cout << "Updating flows configuration max-vm-flows to : 100%\n";
max_vm_flows_ = 100;
}
if (max_vm_flows_ < 0) {
cout << "Updating flows configuration max-vm-flows to : 0%\n";
max_vm_flows_ = 0;
}
struct rlimit rl;
int result = getrlimit(RLIMIT_NOFILE, &rl);
if (result == 0) {
if (rl.rlim_max <= Agent::kMaxOtherOpenFds + 1) {
cout << "Updating linklocal flows configuration to 0\n";
linklocal_system_flows_ = linklocal_vm_flows_ = 0;
return;
}
if (linklocal_system_flows_ > rl.rlim_max - Agent::kMaxOtherOpenFds - 1) {
linklocal_system_flows_ = rl.rlim_max - Agent::kMaxOtherOpenFds - 1;
cout << "Updating linklocal-system-flows configuration to : " <<
linklocal_system_flows_ << "\n";
}
if (rl.rlim_cur < linklocal_system_flows_ + Agent::kMaxOtherOpenFds + 1) {
struct rlimit new_rl;
new_rl.rlim_max = rl.rlim_max;
new_rl.rlim_cur = linklocal_system_flows_ + Agent::kMaxOtherOpenFds + 1;
result = setrlimit(RLIMIT_NOFILE, &new_rl);
if (result != 0) {
if (rl.rlim_cur <= Agent::kMaxOtherOpenFds + 1) {
linklocal_system_flows_ = 0;
} else {
linklocal_system_flows_ = rl.rlim_cur - Agent::kMaxOtherOpenFds - 1;
}
cout << "Unable to set Max open files limit to : " <<
new_rl.rlim_cur <<
" Updating linklocal-system-flows configuration to : " <<
linklocal_system_flows_ << "\n";
}
}
if (linklocal_vm_flows_ > linklocal_system_flows_) {
linklocal_vm_flows_ = linklocal_system_flows_;
cout << "Updating linklocal-vm-flows configuration to : " <<
linklocal_vm_flows_ << "\n";
}
} else {
cout << "Unable to validate linklocal flow configuration\n";
}
}
static bool ValidateInterface(bool test_mode, const std::string &ifname,
bool *no_arp, string *eth_encap) {
*no_arp = false;
*eth_encap = "";
if (test_mode) {
return true;
}
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
assert(fd >= 0);
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifname.c_str(), IF_NAMESIZE);
int err = ioctl(fd, SIOCGIFFLAGS, (void *)&ifr);
close (fd);
if (err < 0) {
LOG(ERROR, "Error reading interface <" << ifname << ">. Error number "
<< errno << " : " << strerror(errno));
return false;
}
if ((ifr.ifr_flags & IFF_NOARP)) {
*no_arp = true;
}
char fname[128];
snprintf(fname, 128, "/sys/class/net/%s/type", ifname.c_str());
FILE *f = fopen(fname, "r");
if (f) {
int type;
if (fscanf(f, "%d", &type) >= 0) {
if (type == ARPHRD_NONE) {
*eth_encap = "none";
}
}
}
return true;
}
int AgentParam::Validate() {
// TODO: fix the validation for the DPDK platform
if (platform_ == AgentParam::VROUTER_ON_HOST_DPDK)
return 0;
// Validate vhost interface name
if (vhost_.name_ == "") {
LOG(ERROR, "Configuration error. vhost interface name not specified");
return (EINVAL);
}
bool no_arp;
string encap;
// Check if interface is already present
if (ValidateInterface(test_mode_, vhost_.name_, &no_arp, &encap) == false) {
return (ENODEV);
}
// Validate ethernet port
if (eth_port_ == "") {
LOG(ERROR, "Configuration error. eth_port not specified");
return (EINVAL);
}
// Check if interface is already present
if (ValidateInterface(test_mode_, eth_port_, ð_port_no_arp_,
ð_port_encap_type_) == false) {
return (ENODEV);
}
// Validate physical port used in vmware
if (hypervisor_mode_ == MODE_VMWARE) {
if (vmware_physical_port_ == "") {
LOG(ERROR, "Configuration error. Physical port connecting to "
"virtual-machines not specified");
return (EINVAL);
}
if (ValidateInterface(test_mode_, vmware_physical_port_, &no_arp,
&encap) == false) {
return (ENODEV);
}
}
return 0;
}
void AgentParam::InitVhostAndXenLLPrefix() {
// Set the prefix address for VHOST and XENLL interfaces
uint32_t mask = vhost_.plen_ ? (0xFFFFFFFF << (32 - vhost_.plen_)) : 0;
vhost_.prefix_ = Ip4Address(vhost_.addr_.to_ulong() & mask);
mask = xen_ll_.plen_ ? (0xFFFFFFFF << (32 - xen_ll_.plen_)) : 0;
xen_ll_.prefix_ = Ip4Address(xen_ll_.addr_.to_ulong() & mask);
}
void AgentParam::InitPlatform() {
Ip4Address ip = Ip4Address::from_string("127.0.0.1");
if (platform_ == VROUTER_ON_NIC) {
agent_->set_vrouter_server_ip(ip);
agent_->set_vrouter_server_port(VROUTER_SERVER_PORT);
agent_->set_pkt_interface_name("pkt0");
} else if (platform_ == VROUTER_ON_HOST_DPDK) {