OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
diag_proto.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <cmn/agent_cmn.h>
6 #include <init/agent_init.h>
7 #include <oper/interface.h>
8 #include <oper/vm_interface.h>
9 #include <oper/metadata_ip.h>
10 #include <pkt/proto.h>
11 #include <pkt/proto_handler.h>
12 #include <diag/diag_proto.h>
13 #include <diag/diag_pkt_handler.h>
15 
16 DiagProto::DiagProto(Agent *agent, boost::asio::io_context &io)
17  : Proto(agent, "Agent::Diag", PktHandler::DIAG, io),
18  session_map_(), stats_mutex_(), stats_() {
20  boost::bind(&DiagProto::SegmentHealthCheckProcess, this, _1, _2),
22 }
23 
24 ProtoHandler *DiagProto::AllocProtoHandler(boost::shared_ptr<PktInfo> info,
25  boost::asio::io_context &io) {
26  return new DiagPktHandler(agent(), info, io);
27 }
28 
31  HealthCheckInstanceService *service) {
32 
33  uint32_t intf_id = service->interface()->id();
34  SessionMap::iterator it = session_map_.find(intf_id);
35 
36  switch (action) {
39  {
40  /* When we get create/update request for already created service,
41  * we update the existing object to reflect change in properties
42  * of service.
43  */
44  if (it != session_map_.end()) {
45  SegmentHealthCheckPkt *old_ptr = it->second;
46  old_ptr->set_service(NULL);
47  old_ptr->UpdateService(service);
48  return true;
49  }
51  new SegmentHealthCheckPkt(service, agent_->diag_table());
52 
53  session_map_.insert(SessionPair(intf_id, ptr));
54  /* Init will add ptr to DiagTable and sends
55  * health-check-packets
56  */
57  ptr->Init();
58  break;
59  }
60 
62  {
63  /* Ignore delete request if we are not sending any
64  * health-check-packets on this interface.
65  */
66  if (it == session_map_.end()) {
67  return true;
68  }
69  SegmentHealthCheckPkt *old_ptr = it->second;
70  old_ptr->set_service(NULL);
71  old_ptr->StopDelayTimer();
72  session_map_.erase(it);
73 
74  /* DeleteEnqueue will remove old_ptr from DiagTable and
75  * frees it */
76  old_ptr->EnqueueForceDelete();
77  break;
78  }
79 
81  break;
82 
84  break;
85 
86  default:
87  assert(0);
88  }
89 
90  return true;
91 }
92 
94  tbb::mutex::scoped_lock lock(stats_mutex_);
95  DiagStats new_entry;
96  std::pair<DiagStatsMap::iterator, bool> ret = stats_.insert(DiagStatsPair
97  (itf_id,
98  new_entry));
99  DiagStats &entry = ret.first->second;
100  switch (type) {
101  case REQUESTS_SENT:
102  entry.requests_sent++;
103  break;
104  case REQUESTS_RECEIVED:
105  entry.requests_received++;
106  break;
107  case REPLIES_SENT:
108  entry.replies_sent++;
109  break;
110  case REPLIES_RECEIVED:
111  entry.replies_received++;
112  break;
113  default:
114  assert(0);
115  }
116 }
117 
118 void DiagProto::FillSandeshHealthCheckResponse(SegmentHealthCheckPktStatsResp
119  *resp) {
120  vector<SegmentHealthCheckStats> &list =
121  const_cast<std::vector<SegmentHealthCheckStats>&>(resp->get_stats());
122  tbb::mutex::scoped_lock lock(stats_mutex_);
123  DiagProto::DiagStatsMap::const_iterator it = stats_.begin();
124  while (it != stats_.end()) {
125  SegmentHealthCheckStats item;
126  DiagProto::DiagStats source = it->second;
127  item.set_interface_index(it->first);
128  item.set_requests_sent(source.requests_sent);
129  item.set_requests_received(source.requests_received);
130  item.set_replies_sent(source.replies_sent);
131  item.set_replies_received(source.replies_received);
132  list.push_back(item);
133  ++it;
134  }
135 }
136 
137 void SegmentHealthCheckPktStats::HandleRequest() const {
139  SegmentHealthCheckPktStatsResp *resp = new SegmentHealthCheckPktStatsResp();
140  proto->FillSandeshHealthCheckResponse(resp);
141  resp->set_context(context());
142  resp->Response();
143  return;
144 }
uint64_t requests_received
Definition: diag_proto.h:31
InterfaceRef interface() const
Definition: health_check.h:147
void UpdateService(HealthCheckInstanceService *service)
static Agent * GetInstance()
Definition: agent.h:436
SessionMap session_map_
Definition: diag_proto.h:56
DiagProto * diag_proto() const
Definition: diag.h:118
void IncrementDiagStats(uint32_t itf_id, DiagStatsType type)
Definition: diag_proto.cc:93
void set_service(HealthCheckInstanceService *svc)
uint64_t replies_sent
Definition: diag_proto.h:32
DiagTable * diag_table() const
Definition: agent.cc:873
uint64_t requests_sent
Definition: diag_proto.h:30
std::pair< uint32_t, SegmentHealthCheckPkt * > SessionPair
Definition: diag_proto.h:20
void Init()
Definition: diag.cc:47
uint8_t type
Definition: load_balance.h:109
std::pair< uint32_t, DiagStats > DiagStatsPair
Definition: diag_proto.h:41
Definition: agent.h:358
void FillSandeshHealthCheckResponse(SegmentHealthCheckPktStatsResp *resp)
Definition: diag_proto.cc:118
HealthCheckTable * health_check_table() const
Definition: agent.cc:933
tbb::mutex stats_mutex_
Definition: diag_proto.h:64
void RegisterHealthCheckCallback(HealthCheckServiceCallback fn, HealthCheckService::HealthCheckType type)
Definition: health_check.h:441
bool SegmentHealthCheckProcess(HealthCheckTable::HealthCheckServiceAction action, HealthCheckInstanceService *service)
Definition: diag_proto.cc:29
uint64_t replies_received
Definition: diag_proto.h:33
void EnqueueForceDelete()
Definition: diag.cc:52
DiagProto(Agent *agent, boost::asio::io_context &io)
Definition: diag_proto.cc:16
DiagStatsMap stats_
Definition: diag_proto.h:65
ProtoHandler * AllocProtoHandler(boost::shared_ptr< PktInfo > info, boost::asio::io_context &io)
Definition: diag_proto.cc:24