Line data Source code
1 : /* 2 : * Copyright (c) 2014 CodiLime, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef SRC_BFD_BFD_SESSION_H_ 6 : #define SRC_BFD_BFD_SESSION_H_ 7 : 8 : #include "bfd/bfd_common.h" 9 : #include "bfd/bfd_state_machine.h" 10 : 11 : #include <string> 12 : #include <map> 13 : #include <boost/scoped_ptr.hpp> 14 : #include <boost/asio/ip/address.hpp> 15 : 16 : #include "base/timer.h" 17 : #include "io/event_manager.h" 18 : 19 : namespace BFD { 20 : class Connection; 21 : struct SessionConfig; 22 : struct ControlPacket; 23 : 24 : struct BFDRemoteSessionState { 25 32 : BFDRemoteSessionState() : discriminator(0), 26 32 : minRxInterval(boost::posix_time::seconds(1)), 27 32 : minTxInterval(boost::posix_time::seconds(0)), 28 32 : detectionTimeMultiplier(1), 29 32 : state(kDown) {} 30 : 31 : Discriminator discriminator; 32 : TimeInterval minRxInterval; 33 : TimeInterval minTxInterval; 34 : int detectionTimeMultiplier; 35 : BFDState state; 36 : }; 37 : 38 : struct BFDStats { 39 32 : BFDStats(): rx_count(0), tx_count(0), 40 32 : rx_error_count(0), tx_error_count(0), 41 32 : receive_timer_expired_count(0), 42 32 : send_timer_expired_count(0) {} 43 : 44 : int rx_count; 45 : int tx_count; 46 : int rx_error_count; 47 : int tx_error_count; 48 : uint32_t receive_timer_expired_count; 49 : uint32_t send_timer_expired_count; 50 : }; 51 : 52 : class Session { 53 : public: 54 : Session(Discriminator localDiscriminator, const SessionKey &key, 55 : EventManager *evm, const SessionConfig &config, 56 : Connection *communicator); 57 : virtual ~Session(); 58 : 59 : void Stop(); 60 : ResultCode ProcessControlPacket(const ControlPacket *packet); 61 : void InitPollSequence(); 62 : void RegisterChangeCallback(ClientId client_id, ChangeCb cb); 63 : void UnregisterChangeCallback(ClientId client_id); 64 : void UpdateConfig(const SessionConfig& config); 65 : 66 : std::string toString() const; 67 : const SessionKey & key() const; 68 : BFDState local_state() const; 69 : SessionConfig config() const; 70 : BFDRemoteSessionState remote_state() const; 71 : Discriminator local_discriminator() const; 72 : bool Up() const; 73 5489 : BFDStats & Stats() { return stats_; } 74 : 75 : TimeInterval detection_time(); 76 : TimeInterval tx_interval(); 77 : 78 : // Yields number of registered callbacks. 79 : // Server::SessionManager will delete a Session instance if its 80 : // reference count drops to zero. 81 : int reference_count(); 82 : 83 : // Used only in UTs 84 3 : Session() 85 3 : { 86 : // Setting stopped_ as true so that Session::Stop can be avoided 87 : // in destructor. It was adding unnecessary complications in UT 88 3 : stopped_ = true; 89 : // creating a default session key for UT 90 3 : key_ = SessionKey(); 91 3 : } 92 : 93 : protected: 94 : bool RecvTimerExpired(); 95 : 96 : private: 97 : typedef std::map<ClientId, ChangeCb> Callbacks; 98 : 99 : bool SendTimerExpired(); 100 : void ScheduleSendTimer(); 101 : void ScheduleRecvDeadlineTimer(); 102 : void PreparePacket(const SessionConfig &config, ControlPacket *packet); 103 : void SendPacket(const ControlPacket *packet); 104 : void CallStateChangeCallbacks(const SessionKey &key, 105 : const BFD::BFDState &new_state); 106 : boost::asio::ip::udp::endpoint GetRandomLocalEndPoint() const; 107 : uint16_t GetRandomLocalPort() const; 108 : BFDState local_state_non_locking() const; 109 : 110 : Discriminator localDiscriminator_; 111 : SessionKey key_; 112 : Timer *sendTimer_; 113 : Timer *recvTimer_; 114 : SessionConfig currentConfig_; 115 : SessionConfig nextConfig_; 116 : BFDRemoteSessionState remoteSession_; 117 : boost::scoped_ptr<StateMachine> sm_; 118 : bool pollSequence_; 119 : Connection *communicator_; 120 : boost::asio::ip::udp::endpoint local_endpoint_; 121 : boost::asio::ip::udp::endpoint remote_endpoint_; 122 : bool started_; 123 : bool stopped_; 124 : Callbacks callbacks_; 125 : BFDStats stats_; 126 : }; 127 : 128 : } // namespace BFD 129 : 130 : #endif // SRC_BFD_BFD_SESSION_H_