OpenSDN source code
control_node_uve.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "control_node.h"
7 
8 #include "base/misc_utils.h"
9 #include "base/task.h"
10 #include "base/task_annotations.h"
11 #include "base/task_trigger.h"
12 #include "base/timer.h"
13 #include "bgp/bgp_server.h"
14 #include "bgp/bgp_xmpp_channel.h"
15 #include "control-node/sandesh/control_node_types.h"
16 #include "ifmap/ifmap_server.h"
17 
18 std::string ControlNode::hostname_;
19 std::string ControlNode::prog_name_;
20 std::string ControlNode::self_ip_;
22 
23 static std::unique_ptr<Timer> node_info_log_timer;
24 static std::unique_ptr<TaskTrigger> node_info_trigger;
25 
27  const BgpXmppChannelManager *xmpp_channel_mgr,
28  const IFMapServer *ifmap_server, const string &build_info) {
29  CHECK_CONCURRENCY("bgp::ShowCommand");
30  static bool first = true;
31  static BgpRouterState state;
32  bool change = false;
33 
34  state.set_name(server->localname());
35 
36  // Send self information.
37  if (first) {
38  state.set_uptime(UTCTimestampUsec());
39  change = true;
40  }
41 
42  vector<string> ip_list;
43  ip_list.push_back(ControlNode::GetSelfIp());
44  if (first || state.get_bgp_router_ip_list() != ip_list) {
45  state.set_bgp_router_ip_list(ip_list);
46  change = true;
47  }
48 
49  // Send Build information.
50  if (first || build_info != state.get_build_info()) {
51  state.set_build_info(build_info);
52  change = true;
53  }
54 
55  change |= server->CollectStats(&state, first);
56  change |= xmpp_channel_mgr->CollectStats(&state, first);
57  change |= ifmap_server->CollectStats(&state, first);
58 
59  if (change) {
60  assert(!state.get_name().empty());
61  BGPRouterInfo::Send(state);
62 
63  // Reset changed flags in the uve structure.
64  memset(static_cast<void*>(&state.__isset), 0, sizeof(state.__isset));
65  }
66 
67  /* Release process free memory to back to system */
68  int rc = malloc_trim(0);
69  if (!rc) {
70  LOG(ERROR, "Control-node free memory is not released to system,"
71  " rc: " << rc);
72  }
73 
74  first = false;
75  return true;
76 }
77 
79  node_info_trigger->Set();
80  // Periodic timer. Restart
81  return true;
82 }
83 
85  EventManager &evm, uint64_t period_msecs,
86  const BgpServer *bgp_server,
87  const BgpXmppChannelManager *xmpp_channel_mgr,
88  const IFMapServer *ifmap_server, const string &build_info) {
89  node_info_trigger.reset(
90  new TaskTrigger(
91  boost::bind(&ControlNode::ControlNodeInfoLogger, bgp_server,
92  xmpp_channel_mgr, ifmap_server, build_info),
93  TaskScheduler::GetInstance()->GetTaskId("bgp::ShowCommand"), 0));
94 
96  "ControlNode Info log timer"));
97 
98  // Start periodic timer to send BGPRouterInfo UVE.
99  node_info_log_timer->Start(period_msecs,
101  node_info_trigger.get()), NULL);
102 }
103 
105  node_info_trigger->set_disable();
106  while (node_info_trigger->IsSet())
107  usleep(100);
108  node_info_trigger.reset();
109  if (node_info_log_timer.get()) {
110  node_info_log_timer->Cancel();
111  while (node_info_log_timer->running() || node_info_log_timer->fired())
112  usleep(100);
114  node_info_log_timer.release();
115  }
116 }
bool CollectStats(BgpRouterState *state, bool first) const
Definition: bgp_server.cc:1074
const std::string & localname() const
Definition: bgp_server.cc:687
bool CollectStats(BgpRouterState *state, bool first) const
static std::string self_ip_
Definition: control_node.h:56
static bool ControlNodeInfoLogger(const BgpServer *server, const BgpXmppChannelManager *xmpp_channel_mgr, const IFMapServer *ifmap_server, const std::string &build_info)
static std::string GetSelfIp()
Definition: control_node.h:34
static void StartControlNodeInfoLogger(EventManager &evm, uint64_t period_msecs, const BgpServer *server, const BgpXmppChannelManager *xmpp_channel_mgr, const IFMapServer *ifmap_server, const string &build_info)
static bool test_mode_
Definition: control_node.h:57
static std::string hostname_
Definition: control_node.h:54
static bool ControlNodeInfoLogTimer(TaskTrigger *node_info_trigger)
static void Shutdown()
static std::string prog_name_
Definition: control_node.h:55
boost::asio::io_context * io_service()
Definition: event_manager.h:43
bool CollectStats(BgpRouterState *state, bool first) const
static TaskScheduler * GetInstance()
Definition: task.cc:574
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222
static Timer * CreateTimer(boost::asio::io_context &service, const std::string &name, int task_id=Timer::GetTimerTaskId(), int task_instance=Timer::GetTimerInstanceId(), bool delete_on_completion=false)
Definition: timer.cc:201
static std::unique_ptr< TaskTrigger > node_info_trigger
static std::unique_ptr< Timer > node_info_log_timer
static EventManager evm
#define LOG(_Level, _Msg)
Definition: logging.h:35
#define CHECK_CONCURRENCY(...)
static uint64_t UTCTimestampUsec()
Definition: time_util.h:14