OpenSDN source code
controller/src/vnsw/agent/contrail/main.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <boost/program_options.hpp>
6 #include <base/logging.h>
7 #include <base/contrail_ports.h>
8 
9 #include <pugixml/pugixml.hpp>
10 
11 #include <base/task.h>
12 #include <io/event_manager.h>
13 #include <sandesh/common/vns_types.h>
14 #include <sandesh/common/vns_constants.h>
15 #include <base/misc_utils.h>
16 
17 #include <cmn/buildinfo.h>
18 #include <cmn/agent_cmn.h>
19 
20 #include <cfg/cfg_init.h>
21 #include <cfg/cfg_mirror.h>
22 
23 #include <init/agent_param.h>
24 
25 #include <oper/operdb_init.h>
26 #include <oper/vrf.h>
27 #include <oper/multicast.h>
28 #include <oper/mirror_table.h>
31 #include <pkt/pkt_init.h>
32 #include <services/services_init.h>
34 #include <uve/agent_uve.h>
35 #include <kstate/kstate.h>
36 #include <pkt/proto.h>
37 #include <diag/diag.h>
38 #include <boost/functional/factory.hpp>
39 #include <cmn/agent_factory.h>
40 
41 #include "contrail_agent_init.h"
42 #define MAX_RETRY 60
43 namespace opt = boost::program_options;
44 
45 void RouterIdDepInit(Agent *agent) {
46  // Parse config and then connect
48  LOG(DEBUG, "Router ID Dependent modules (Nova and BGP) INITIALIZED");
49 }
50 
52  struct ifreq ifr;
53  int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
54  memset(&ifr, 0, sizeof(ifr));
55  strcpy(ifr.ifr_name, "vhost0");
56  int err = ioctl(sock, SIOCGIFFLAGS, &ifr);
57  if (err < 0 || !(ifr.ifr_flags & IFF_UP)) {
58  close(sock);
59  LOG(DEBUG, "vhost is down");
60  return false;
61  }
62  err = ioctl(sock, SIOCGIFADDR, &ifr);
63  if (err < 0) {
64  close(sock);
65  LOG(DEBUG, "vhost is up, but ip is not set");
66  return false;
67  }
68  close(sock);
69  return true;
70 }
71 
72 int main(int argc, char *argv[]) {
73  AgentParam params;
74  srand(unsigned(time(NULL)));
75  try {
76  params.ParseArguments(argc, argv);
77  } catch (...) {
78  std::cout << "Invalid arguments. ";
79  std::cout << params.options() << std::endl;
80  exit(0);
81  }
82 
83  opt::variables_map var_map = params.var_map();
84  if (var_map.count("help")) {
85  std::cout << params.options() << std::endl;
86  exit(0);
87  }
88 
89  if (var_map.count("version")) {
90  std::cout << BuildInfo << std::endl;
91  exit(0);
92  }
93 
94  string init_file = "";
95  if (var_map.count("config_file")) {
96  init_file = var_map["config_file"].as<string>();
97  struct stat s;
98  if (stat(init_file.c_str(), &s) != 0) {
99  std::cout << "Error opening config file <" << init_file
100  << ">. Error number <" << errno << ">";
101  exit(EINVAL);
102  }
103  }
104 
105  // Read agent parameters from config file and arguments
106  params.Init(init_file, argv[0]);
107 
108  if (!params.cat_is_agent_mocked() &&
109  params.loopback_ip() == Ip4Address(0)) {
110 
111  uint16_t i;
112 
113  for (i = 0; i < MAX_RETRY; i++) {
114  std::cout << "INFO: wait vhost0 to be initilaized... "
115  << i << "/" << MAX_RETRY << std::endl;
116  if (is_vhost_interface_up()) {
117  std::cout << "INFO: vhost0 is ready." << std::endl;
118  break;
119  }
120  usleep(5000000); //sleep for 5 seconds
121  }
122 
123  if (i == MAX_RETRY) {
124  std::cout << "INFO: vhost0 is not ready." << std::endl;
125  exit(0);
126  }
127  }
128 
129  // Initialize TBB
130  // Call to GetScheduler::GetInstance() will also create Task Scheduler
132 
133  // Initialize the agent-init control class
135  if (params.cat_is_agent_mocked()) {
136  init.set_create_vhost(false);
137  }
138 
139  init.set_agent_param(&params);
140  // kick start initialization
141  int ret = 0;
142  if ((ret = init.Start()) != 0) {
143  return ret;
144  }
145 
146  MiscUtils::LogVersionInfo(BuildInfo, Category::VROUTER);
147 
148  Agent *agent = init.agent();
150  agent->event_manager()->Run();
151 
152  return 0;
153 }
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
bool cat_is_agent_mocked() const
Definition: agent_param.h:567
const boost::program_options::variables_map & var_map() const
Definition: agent_param.h:371
boost::program_options::options_description options() const
Definition: agent_param.h:375
void ParseArguments(int argc, char *argv[])
const Ip4Address & loopback_ip() const
Definition: agent_param.h:575
void Init(const std::string &config_file, const std::string &program_name)
uint32_t tbb_thread_count() const
Definition: agent_param.h:451
Definition: agent.h:360
VNController * controller() const
Definition: agent.cc:984
EventManager * event_manager() const
Definition: agent.h:1105
static Agent * GetInstance()
Definition: agent.h:438
static void LogVersionInfo(const std::string str, Category::type categ)
Definition: misc_utils.cc:36
static void Initialize(uint32_t thread_count=0, EventManager *evm=NULL)
Definition: task.cc:478
static TaskScheduler * GetInstance()
Definition: task.cc:547
void set_event_manager(EventManager *evm)
Definition: task.cc:493
void RouterIdDepInit(Agent *agent)
#define LOG(_Level, _Msg)
Definition: logging.h:33
void init()
Definition: bgp_log.cc:37
const std::string BuildInfo