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