OpenSDN source code
bfd_proto.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef vnsw_agent_bfd_proto_h_
6 #define vnsw_agent_bfd_proto_h_
7 
8 #include <mutex>
9 
10 #include "pkt/proto.h"
11 #include "pkt/pkt_handler.h"
12 #include "services/bfd_handler.h"
13 #include "oper/health_check.h"
14 
15 #include "bfd/bfd_client.h"
16 #include "bfd/bfd_server.h"
17 #include "bfd/bfd_connection.h"
18 #include "bfd/bfd_session.h"
19 
20 #include "base/test/task_test_util.h"
21 
22 #define BFD_TX_BUFF_LEN 128
23 
24 #define BFD_TRACE(obj, ...) \
25 do { \
26  Bfd##obj::TraceMsg(BfdTraceBuf, __FILE__, __LINE__, ##__VA_ARGS__); \
27 } while (false)
28 
30 public:
31  BfdSessionsKey(uint32_t id, IpAddress ip) :
32  id_(id), ip_(ip) {
33  }
34 
36 
37  bool operator<(const BfdSessionsKey &right) const {
38  if ( id_ == right.id_ ) {
39  return ip_ < right.ip_;
40  } else {
41  return id_ < right.id_;
42  }
43  }
44 
45  bool IsLess(const BfdSessionsKey &rhs) const;
46  bool IsEqual(const BfdSessionsKey &rhs) const;
47 
48  uint32_t id_;
50 };
51 
53  bool operator()(const BfdSessionsKey &lhs, const BfdSessionsKey &rhs) const {
54  return lhs.IsLess(rhs);
55  }
56 };
57 
58 class BfdProto : public Proto {
59 public:
60  static const uint32_t kMultiplier = 2;
61  static const uint32_t kMinRxInterval = 500000; // microseconds
62  static const uint32_t kMinTxInterval = 500000; // microseconds
63 
64  struct BfdStats {
65  BfdStats() { Reset(); }
68 
69  uint64_t bfd_sent;
70  uint64_t bfd_received;
73  };
74 
76  public:
77  BfdCommunicator(BfdProto *bfd_proto) :
78  bfd_proto_(bfd_proto), server_(NULL) {}
79  virtual ~BfdCommunicator() {}
80  virtual void SendPacket(
81  const boost::asio::ip::udp::endpoint &local_endpoint,
82  const boost::asio::ip::udp::endpoint &remote_endpoint,
83  const BFD::SessionIndex &session_index,
84  const boost::asio::mutable_buffer &packet, int pktSize);
85  virtual void NotifyStateChange(const BFD::SessionKey &key, const bool &up);
86  virtual BFD::Server *GetServer() const { return server_; }
87  virtual void SetServer(BFD::Server *server) { server_ = server; }
88 
89  private:
92  };
93 
94  BfdProto(Agent *agent, boost::asio::io_context &io);
95  virtual ~BfdProto();
96  ProtoHandler *AllocProtoHandler(boost::shared_ptr<PktInfo> info,
97  boost::asio::io_context &io);
98  void Shutdown() {
99  delete client_;
100  client_ = NULL;
101 
102  // server_->DeleteClientSessions();
103  // TASK_UTIL_EXPECT_TRUE(server_->event_queue()->IsQueueEmpty());
104  server_->event_queue()->Shutdown();
105  delete server_;
106  server_ = NULL;
107 
108  sessions_.clear();
109  }
110 
111  bool Enqueue(boost::shared_ptr<PktInfo> msg);
113  bool ProcessBfdKeepAlive(boost::shared_ptr<PktInfo> msg);
114  void HandleReceiveSafe(boost::asio::const_buffer pkt,
115  const boost::asio::ip::udp::endpoint &local_endpoint,
116  const boost::asio::ip::udp::endpoint &remote_endpoint,
117  const BFD::SessionIndex &session_index,
118  uint8_t pkt_len,
119  boost::system::error_code ec);
120 
123  HealthCheckInstanceService *service);
124  void NotifyHealthCheckInstanceService(uint32_t interface,
125  IpAddress address, std::string &data);
127 
132  const BfdStats &GetStats() const { return stats_; }
133  uint32_t ActiveSessions() const { return sessions_.size(); }
134 
135 private:
137  // map from interface id,dest-ip to health check instance service
138  typedef std::map<BfdSessionsKey, HealthCheckInstanceService *, BfdSessionsKeyCmp> Sessions;
139  typedef std::pair<BfdSessionsKey, HealthCheckInstanceService *> SessionsPair;
140 
141  std::mutex mutex_; // lock for sessions_ access between health check & BFD
142  std::mutex rx_mutex_; // lock for BFD control & keepalive Rx data
143  boost::shared_ptr<PktInfo> msg_;
150 
152 };
153 
154 #endif // vnsw_agent_bfd_proto_h_
boost::asio::ip::address IpAddress
Definition: address.h:13
Definition: agent.h:360
WorkQueue< Event * > * event_queue()
Definition: bfd_server.h:61
virtual BFD::Server * GetServer() const
Definition: bfd_proto.h:86
virtual void NotifyStateChange(const BFD::SessionKey &key, const bool &up)
Definition: bfd_proto.cc:231
virtual ~BfdCommunicator()
Definition: bfd_proto.h:79
virtual void SetServer(BFD::Server *server)
Definition: bfd_proto.h:87
BfdCommunicator(BfdProto *bfd_proto)
Definition: bfd_proto.h:77
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:221
BFD::Server * server_
Definition: bfd_proto.h:91
std::map< BfdSessionsKey, HealthCheckInstanceService *, BfdSessionsKeyCmp > Sessions
Definition: bfd_proto.h:138
DISALLOW_COPY_AND_ASSIGN(BfdProto)
static const uint32_t kMinTxInterval
Definition: bfd_proto.h:62
std::pair< BfdSessionsKey, HealthCheckInstanceService * > SessionsPair
Definition: bfd_proto.h:139
BFD::Server * server_
Definition: bfd_proto.h:145
void NotifyHealthCheckInstanceService(uint32_t interface, IpAddress address, std::string &data)
Definition: bfd_proto.cc:161
void IncrementSent()
Definition: bfd_proto.h:128
bool ProcessBfdKeepAlive(boost::shared_ptr< PktInfo > msg)
Definition: bfd_proto.cc:262
BFD::Client * client_
Definition: bfd_proto.h:146
virtual ~BfdProto()
Definition: bfd_proto.cc:36
bool Enqueue(boost::shared_ptr< PktInfo > msg)
Definition: bfd_proto.cc:238
const BfdStats & GetStats() const
Definition: bfd_proto.h:132
BfdStats stats_
Definition: bfd_proto.h:149
Sessions sessions_
Definition: bfd_proto.h:148
void IncrementReceiveDropCount()
Definition: bfd_proto.h:130
BfdProto(Agent *agent, boost::asio::io_context &io)
Definition: bfd_proto.cc:20
std::mutex rx_mutex_
Definition: bfd_proto.h:142
void IncrementReceived()
Definition: bfd_proto.h:129
friend BfdCommunicator
Definition: bfd_proto.h:136
uint32_t ActiveSessions() const
Definition: bfd_proto.h:133
std::mutex mutex_
Definition: bfd_proto.h:141
BfdCommunicator & bfd_communicator()
Definition: bfd_proto.h:126
void ProcessStats(PktStatsType::Type type)
Definition: bfd_proto.cc:249
BfdCommunicator communicator_
Definition: bfd_proto.h:144
ProtoHandler * AllocProtoHandler(boost::shared_ptr< PktInfo > info, boost::asio::io_context &io)
Definition: bfd_proto.cc:39
static const uint32_t kMultiplier
Definition: bfd_proto.h:60
void IncrementKaEnqueueCount()
Definition: bfd_proto.h:131
void Shutdown()
Definition: bfd_proto.h:98
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:204
bool BfdHealthCheckSessionControl(HealthCheckTable::HealthCheckServiceAction action, HealthCheckInstanceService *service)
Definition: bfd_proto.cc:61
BfdHandler handler_
Definition: bfd_proto.h:147
boost::shared_ptr< PktInfo > msg_
Definition: bfd_proto.h:143
static const uint32_t kMinRxInterval
Definition: bfd_proto.h:61
bool IsEqual(const BfdSessionsKey &rhs) const
Definition: bfd_proto.cc:51
bool operator<(const BfdSessionsKey &right) const
Definition: bfd_proto.h:37
uint32_t id_
Definition: bfd_proto.h:48
BfdSessionsKey(uint32_t id, IpAddress ip)
Definition: bfd_proto.h:31
IpAddress ip_
Definition: bfd_proto.h:49
bool IsLess(const BfdSessionsKey &rhs) const
Definition: bfd_proto.cc:44
uint8_t type
Definition: load_balance.h:2
Definition: io_utils.cc:11
uint32_t bfd_rx_drop_count
Definition: bfd_proto.h:71
uint64_t bfd_rx_ka_enqueue_count
Definition: bfd_proto.h:72
uint64_t bfd_sent
Definition: bfd_proto.h:69
uint64_t bfd_received
Definition: bfd_proto.h:70
bool operator()(const BfdSessionsKey &lhs, const BfdSessionsKey &rhs) const
Definition: bfd_proto.h:53