OpenSDN source code
bfd_proto.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include <sandesh/sandesh_types.h>
7 #include <sandesh/sandesh.h>
8 #include <cmn/agent_cmn.h>
9 #include <init/agent_init.h>
10 #include <oper/interface.h>
11 #include <oper/vm_interface.h>
12 #include <oper/metadata_ip.h>
13 #include <pkt/proto_handler.h>
14 #include "pkt/pkt_init.h"
15 #include <services/bfd_proto.h>
16 #include <services/bfd_handler.h>
17 #include <services/services_types.h>
18 #include <services/services_init.h>
19 #include "base/logging.h"
20 
21 BfdProto::BfdProto(Agent *agent, boost::asio::io_context &io) :
22  Proto(agent, "Agent::Services", PktHandler::BFD, io),
23  msg_(new PktInfo(agent, BFD_TX_BUFF_LEN, PktHandler::BFD, 0)),
24  communicator_(this),
25  server_(new BFD::Server(agent->event_manager(), &communicator_)),
26  client_(new BFD::Client(&communicator_)), handler_(agent, msg_, io) {
27 
28  // limit the number of entries in the workqueue
30  work_queue_.SetBounded(true);
31 
33  boost::bind(&BfdProto::BfdHealthCheckSessionControl, this, _1, _2),
35 }
36 
38 }
39 
40 ProtoHandler *BfdProto::AllocProtoHandler(boost::shared_ptr<PktInfo> info,
41  boost::asio::io_context &io) {
42  return new BfdHandler(agent(), info, io);
43 }
44 
45 bool BfdSessionsKey::IsLess(const BfdSessionsKey &rhs) const {
46  if (id_ != rhs.id_) {
47  return id_ < rhs.id_;
48  }
49  return ip_ < rhs.ip_;
50 }
51 
52 bool BfdSessionsKey::IsEqual(const BfdSessionsKey &rhs) const {
53  if (id_ != rhs.id_) {
54  return false;
55  }
56  if (ip_ != rhs.ip_) {
57  return false;
58  }
59  return true;
60 }
61 
64  HealthCheckInstanceService *service) {
65 
66  uint16_t remote_port = service->is_multi_hop() ?
68  IpAddress source_ip = service->update_source_ip();
69  IpAddress destination_ip = service->destination_ip();
70  if (source_ip.is_unspecified()) {
71  BFD_TRACE(Trace, "Ignore",
72  destination_ip.to_string(),
73  source_ip.to_string(), service->interface()->id(),
74  0, 0, 0);
75  return false;
76  }
77  BFD::SessionKey key(destination_ip,
78  BFD::SessionIndex(service->interface()->id()),
79  remote_port,
80  source_ip);
81 
82  std::scoped_lock lock(mutex_);
83  switch (action) {
86  {
87  if (source_ip.is_v4() &&
88  source_ip.to_v4() == Ip4Address(METADATA_IP_ADDR)) {
89  return false;
90  }
91 
92  uint32_t tx_interval = service->service()->delay() * 1000000 +
93  service->service()->delay_usecs();
94  if (!tx_interval) {
95  tx_interval = kMinTxInterval;
96  }
97  uint32_t rx_interval = service->service()->timeout() * 1000000 +
98  service->service()->timeout_usecs();
99  if (!rx_interval) {
100  rx_interval = kMinRxInterval;
101  }
102  uint32_t multiplier = service->service()->max_retries();
103  if (!multiplier) {
104  multiplier = kMultiplier;
105  }
106 
107  BFD::SessionConfig session_config;
108  session_config.desiredMinTxInterval =
109  boost::posix_time::microseconds(tx_interval);
110  session_config.requiredMinRxInterval =
111  boost::posix_time::microseconds(rx_interval);
112  session_config.detectionTimeMultiplier = multiplier;
113 
114  client_->AddSession(key, session_config);
115  BfdSessionsKey skey(service->interface()->id(),
116  service->destination_ip());
117  sessions_[skey] = service;
118  BFD_TRACE(Trace, "Add / Update",
119  destination_ip.to_string(),
120  source_ip.to_string(), service->interface()->id(),
121  tx_interval, rx_interval, multiplier);
122  break;
123  }
124 
126  {
127  client_->DeleteSession(key);
128  BfdSessionsKey skey(service->interface()->id(),
129  service->destination_ip());
130  Sessions::iterator it = sessions_.find(skey);
131  if (it != sessions_.end()) {
132  sessions_.erase(it);
133  BFD_TRACE(Trace, "Delete",
134  destination_ip.to_string(),
135  source_ip.to_string(), service->interface()->id(),
136  0, 0, 0);
137  } else {
138  // TODO: replace log with trace
139  std::string str("BFD delete session ");
140  str += " interface-id: " + service->interface()->id();
141  str += " source-ip: " + source_ip.to_string();
142  str += " destination-ip: " + destination_ip.to_string();
143 
144  LOG(ERROR, "Duplicate request: " << str);
145  }
146  break;
147  }
148 
150  break;
151 
153  break;
154 
155  default:
156  assert(0);
157  }
158 
159  return true;
160 }
161 
163  IpAddress address,
164  std::string &data) {
165  std::scoped_lock lock(mutex_);
166  BfdSessionsKey key(interface, address);
167  Sessions::iterator it = sessions_.find(key);
168  if (it == sessions_.end()) {
169  return;
170  }
171 
172  const bool up = data.find("success") != std::string::npos;
173  const bool down = data.find("failure") != std::string::npos;
174  if (up || down) {
175  it->second->BfdStateChange(up);
176  }
177 
178  it->second->OnRead(data);
179 
180  // log the BFD up/down event
181  std::string str("BFD session ");
182  if (data.find("success") != std::string::npos) {
183  str += "Up,";
184  }
185  if (data.find("failure") != std::string::npos) {
186  str += "Down,";
187  }
188  if (it->second->service()) {
189  str += " service-health-check: " + it->second->service()->name();
190  } else {
191  str += " service: null";
192  }
193  if (it->second->interface()) {
194  str += " interface: " + it->second->interface()->name();
195  if (it->second->interface()->vrf()) {
196  str += " vrf: " + it->second->interface()->vrf()->GetName();
197  } else {
198  str += " vrf: null";
199  }
200  } else {
201  str += " interface: null";
202  }
203  if (!it->second->destination_ip().is_unspecified()) {
204  str += " destination-ip: " + it->second->destination_ip().to_string();
205  } else {
206  str += " destination-ip: null";
207  }
208  LOG(WARN, "SYS_NOTICE " << str);
209 
210 }
211 
213  boost::asio::const_buffer packet,
214  const boost::asio::ip::udp::endpoint &local_endpoint,
215  const boost::asio::ip::udp::endpoint &remote_endpoint,
216  const BFD::SessionIndex &session_index,
217  uint8_t len,
218  boost::system::error_code ec) {
219 
220  std::scoped_lock lock(rx_mutex_);
222  packet, local_endpoint, remote_endpoint,
223  session_index,
224  len, ec);
225 
227 }
228 
230  const boost::asio::ip::udp::endpoint &local_endpoint,
231  const boost::asio::ip::udp::endpoint &remote_endpoint,
232  const BFD::SessionIndex &session_index,
233  const boost::asio::mutable_buffer &packet, int pktSize) {
234  bfd_proto_->handler_.SendPacket(local_endpoint, remote_endpoint,
235  session_index.if_index, packet, pktSize);
237 }
238 
240  const bool &up) {
241  std::string data = up ? "success" : "failure";
242  bfd_proto_->NotifyHealthCheckInstanceService(key.index.if_index,
243  key.remote_address ,data);
244 }
245 
246 bool BfdProto::Enqueue(boost::shared_ptr<PktInfo> msg) {
247 
248  if (msg->is_bfd_keepalive) {
249  // Handle BFD keepalive packets
250  return ProcessBfdKeepAlive(msg);
251  }
252  // Handle BFD control packets
253  return Proto::Enqueue(msg);
254 }
255 
256 // Handle BFD packet stats
258  switch(type) {
261  break;
264  break;
265  default:
266  return;
267  }
268 }
269 
270 bool BfdProto::ProcessBfdKeepAlive(boost::shared_ptr<PktInfo> msg_info) {
271  PktHandler *pkt_handler = Agent::GetInstance()->pkt()->pkt_handler();
272 
273  BfdHandler *handler = new BfdHandler(agent(), msg_info, get_io_service());
274  if (handler->HandleReceive())
275  delete handler;
276 
277  return true;;
278 }
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
#define METADATA_IP_ADDR
Definition: agent.h:295
#define BFD_TRACE(obj,...)
Definition: bfd_proto.h:25
#define BFD_TX_BUFF_LEN
Definition: bfd_proto.h:23
uint32_t services_queue_limit()
Definition: agent_param.h:416
Definition: agent.h:361
AgentParam * params() const
Definition: agent.h:1227
static Agent * GetInstance()
Definition: agent.h:439
HealthCheckTable * health_check_table() const
Definition: agent.cc:937
PktModule * pkt() const
Definition: agent.cc:969
void AddSession(const SessionKey &key, const SessionConfig &config)
Definition: bfd_client.cc:45
void DeleteSession(const SessionKey &key)
Definition: bfd_client.cc:50
virtual void HandleReceive(const boost::asio::const_buffer &recv_buffer, const boost::asio::ip::udp::endpoint &local_endpoint, const boost::asio::ip::udp::endpoint &remote_endpoint, const SessionIndex &session_index, std::size_t bytes_transferred, const boost::system::error_code &error)
bool HandleReceive()
Definition: bfd_handler.cc:21
void SendPacket(const boost::asio::ip::udp::endpoint &local_endpoint, const boost::asio::ip::udp::endpoint &remote_endpoint, uint32_t interface_id, const boost::asio::mutable_buffer &packet, int packet_length)
Definition: bfd_handler.cc:51
virtual void NotifyStateChange(const BFD::SessionKey &key, const bool &up)
Definition: bfd_proto.cc:239
virtual void SendPacket(const boost::asio::ip::udp::endpoint &local_endpoint, const boost::asio::ip::udp::endpoint &remote_endpoint, const BFD::SessionIndex &session_index, const boost::asio::mutable_buffer &packet, int pktSize)
Definition: bfd_proto.cc:229
static const uint32_t kMinTxInterval
Definition: bfd_proto.h:63
void NotifyHealthCheckInstanceService(uint32_t interface, IpAddress address, std::string &data)
Definition: bfd_proto.cc:162
void IncrementSent()
Definition: bfd_proto.h:129
bool ProcessBfdKeepAlive(boost::shared_ptr< PktInfo > msg)
Definition: bfd_proto.cc:270
BFD::Client * client_
Definition: bfd_proto.h:147
virtual ~BfdProto()
Definition: bfd_proto.cc:37
bool Enqueue(boost::shared_ptr< PktInfo > msg)
Definition: bfd_proto.cc:246
Sessions sessions_
Definition: bfd_proto.h:149
void IncrementReceiveDropCount()
Definition: bfd_proto.h:131
BfdProto(Agent *agent, boost::asio::io_context &io)
Definition: bfd_proto.cc:21
std::mutex rx_mutex_
Definition: bfd_proto.h:143
void IncrementReceived()
Definition: bfd_proto.h:130
std::mutex mutex_
Definition: bfd_proto.h:142
BfdCommunicator & bfd_communicator()
Definition: bfd_proto.h:127
void ProcessStats(PktStatsType::Type type)
Definition: bfd_proto.cc:257
ProtoHandler * AllocProtoHandler(boost::shared_ptr< PktInfo > info, boost::asio::io_context &io)
Definition: bfd_proto.cc:40
static const uint32_t kMultiplier
Definition: bfd_proto.h:61
void IncrementKaEnqueueCount()
Definition: bfd_proto.h:132
void HandleReceiveSafe(boost::asio::const_buffer pkt, const boost::asio::ip::udp::endpoint &local_endpoint, const boost::asio::ip::udp::endpoint &remote_endpoint, const BFD::SessionIndex &session_index, uint8_t pkt_len, boost::system::error_code ec)
Definition: bfd_proto.cc:212
bool BfdHealthCheckSessionControl(HealthCheckTable::HealthCheckServiceAction action, HealthCheckInstanceService *service)
Definition: bfd_proto.cc:62
BfdHandler handler_
Definition: bfd_proto.h:148
static const uint32_t kMinRxInterval
Definition: bfd_proto.h:62
bool IsEqual(const BfdSessionsKey &rhs) const
Definition: bfd_proto.cc:52
uint32_t id_
Definition: bfd_proto.h:49
IpAddress ip_
Definition: bfd_proto.h:50
bool IsLess(const BfdSessionsKey &rhs) const
Definition: bfd_proto.cc:45
IpAddress destination_ip() const
IpAddress update_source_ip()
InterfaceRef interface() const
Definition: health_check.h:148
HealthCheckService * service() const
Definition: health_check.h:147
uint32_t delay() const
Definition: health_check.h:342
uint32_t max_retries() const
Definition: health_check.h:346
uint32_t timeout() const
Definition: health_check.h:344
uint64_t timeout_usecs() const
Definition: health_check.h:345
uint64_t delay_usecs() const
Definition: health_check.h:343
void RegisterHealthCheckCallback(HealthCheckServiceCallback fn, HealthCheckService::HealthCheckType type)
Definition: health_check.h:453
PktHandler * pkt_handler() const
Definition: pkt_init.h:32
boost::asio::io_context & get_io_service() const
virtual bool Enqueue(boost::shared_ptr< PktInfo > msg)
Definition: trace.h:289
void SetBounded(bool bounded)
Definition: queue_task.h:201
void SetSize(size_t size)
Definition: queue_task.h:197
uint8_t type
Definition: load_balance.h:2
#define LOG(_Level, _Msg)
Definition: logging.h:35
Definition: bfd_client.h:10
@ kSingleHop
Definition: bfd_common.h:26
@ kMultiHop
Definition: bfd_common.h:27
Definition: io_utils.cc:12
TimeInterval desiredMinTxInterval
Definition: bfd_common.h:125
TimeInterval requiredMinRxInterval
Definition: bfd_common.h:126
uint32_t if_index
Definition: bfd_common.h:81
SessionIndex index
Definition: bfd_common.h:116
boost::asio::ip::address remote_address
Definition: bfd_common.h:115