diff --git a/src/DbInterface.cpp b/src/DbInterface.cpp index a67502fd..dbabc895 100644 --- a/src/DbInterface.cpp +++ b/src/DbInterface.cpp @@ -46,6 +46,7 @@ constexpr auto DEFAULT_TIMEOUT_MSEC = 1000; std::vector DbInterface::mMuxState = {"active", "standby", "unknown", "Error"}; std::vector DbInterface::mMuxLinkmgrState = {"uninitialized", "unhealthy", "healthy"}; std::vector DbInterface::mMuxMetrics = {"start", "end"}; +std::vector DbInterface::mLinkProbeMetrics = {"link_prober_unknown_start", "link_prober_unknown_end"}; // // ---> DbInterface(mux::MuxManager *muxManager); @@ -161,6 +162,62 @@ void DbInterface::postMetricsEvent( ))); } +// +// ---> postLinkProberMetricsEvent( +// const std::string &portName, +// link_manager::LinkManagerStateMachine::LinkProberMetrics metrics +// ); +// +// post link probe pck loss event to state db +void DbInterface::postLinkProberMetricsEvent( + const std::string &portName, + link_manager::LinkManagerStateMachine::LinkProberMetrics metrics +) +{ + MUXLOGWARNING(boost::format("%s: posting link prober pck loss event %s") % + portName % + mLinkProbeMetrics[static_cast (metrics)] + ); + + boost::asio::io_service &ioService = mStrand.context(); + ioService.post(mStrand.wrap(boost::bind( + &DbInterface::handlePostLinkProberMetrics, + this, + portName, + metrics, + boost::posix_time::microsec_clock::universal_time() + ))); +} + +// +// ---> postPckLossRatio( +// const std::string &portName, +// const uint64_t unknownEventCount, +// const uint64_t expectedPacketCount +// ); +// post pck loss ratio update to state db +void DbInterface::postPckLossRatio( + const std::string &portName, + const uint64_t unknownEventCount, + const uint64_t expectedPacketCount +) +{ + MUXLOGDEBUG(boost::format("%s: posting pck loss ratio, pck_loss_count / pck_expected_count : %d / %d") % + portName % + unknownEventCount % + expectedPacketCount + ); + + boost::asio::io_service &ioService = mStrand.context(); + ioService.post(mStrand.wrap(boost::bind( + &DbInterface::handlePostPckLossRatio, + this, + portName, + unknownEventCount, + expectedPacketCount + ))); +} + // // ---> initialize(); // @@ -184,6 +241,9 @@ void DbInterface::initialize() mStateDbMuxMetricsTablePtr = std::make_shared ( mStateDbPtr.get(), STATE_MUX_METRICS_TABLE_NAME ); + mStateDbLinkProbeStatsTablePtr = std::make_shared ( + mStateDbPtr.get(), LINK_PROBE_STATS_TABLE_NAME + ); mMuxStateTablePtr = std::make_shared (mStateDbPtr.get(), STATE_MUX_CABLE_TABLE_NAME); mSwssThreadPtr = std::make_shared (&DbInterface::handleSwssNotification, this); @@ -319,6 +379,57 @@ void DbInterface::handlePostMuxMetrics( ); } +// +// ---> handlePostLinkProberMetrics( +// const std::string portName, +// link_manager::LinkManagerStateMachine::LinkProberMetrics, +// boost::posix_time::ptime time +// ); +// +// post link prober pck loss event to state db +void DbInterface::handlePostLinkProberMetrics( + const std::string portName, + link_manager::LinkManagerStateMachine::LinkProberMetrics metrics, + boost::posix_time::ptime time +) +{ + MUXLOGWARNING(boost::format("%s: posting link prober pck loss event %s") % + portName % + mLinkProbeMetrics[static_cast (metrics)] + ); + + if (metrics == link_manager::LinkManagerStateMachine::LinkProberMetrics::LinkProberUnknownStart) { + mStateDbLinkProbeStatsTablePtr->hdel(portName, mLinkProbeMetrics[0]); + mStateDbLinkProbeStatsTablePtr->hdel(portName, mLinkProbeMetrics[1]); + } + + mStateDbLinkProbeStatsTablePtr->hset(portName, mLinkProbeMetrics[static_cast (metrics)], boost::posix_time::to_simple_string(time)); +} + +// +// ---> handlePostPckLossRatio( +// const std::string portName, +// const uint64_t unknownEventCount, +// const uint64_t expectedPacketCount +// ); +// +// handle post pck loss ratio +void DbInterface::handlePostPckLossRatio( + const std::string portName, + const uint64_t unknownEventCount, + const uint64_t expectedPacketCount +) +{ + MUXLOGDEBUG(boost::format("%s: posting pck loss ratio, pck_loss_count / pck_expected_count : %d / %d") % + portName % + unknownEventCount % + expectedPacketCount + ); + + mStateDbLinkProbeStatsTablePtr->hset(portName, "pck_loss_count", std::to_string(unknownEventCount)); + mStateDbLinkProbeStatsTablePtr->hset(portName, "pck_expected_count", std::to_string(expectedPacketCount)); +} + // // ---> processTorMacAddress(std::string& mac); // @@ -500,8 +611,28 @@ void DbInterface::processMuxPortConfigNotifiction(std::dequeupdateMuxPortConfig(port, v); } + + std::vector::const_iterator c_it = std::find_if( + fieldValues.cbegin(), + fieldValues.cend(), + [] (const swss::FieldValueTuple &fv) {return fvField(fv) == "pck_loss_data_reset";} + ); + if (c_it != fieldValues.cend()) { + const std::string f = c_it->first; + const std::string v = c_it->second; + + MUXLOGDEBUG(boost::format("key: %s, Operation: %s, f: %s, v: %s") % + port % + operation % + f % + v + ); + + mMuxManagerPtr->resetPckLossCount(port); + } } } diff --git a/src/DbInterface.h b/src/DbInterface.h index 1497bf6e..5afcc60b 100644 --- a/src/DbInterface.h +++ b/src/DbInterface.h @@ -44,6 +44,8 @@ class MuxManagerTest; namespace mux { +#define LINK_PROBE_STATS_TABLE_NAME "LINK_PROBE_STATS" + class MuxManager; using ServerIpPortMap = std::map; @@ -170,6 +172,39 @@ class DbInterface mux_state::MuxState::Label label ); + /** + * @method postLinkProberMetricsEvent + * + * @brief post link prober pck loss event + * + * @param portName (in) port name + * @param metrics (in) pck loss event name + * + * @return none + * + */ + virtual void postLinkProberMetricsEvent( + const std::string &portName, + link_manager::LinkManagerStateMachine::LinkProberMetrics metrics + ); + + /** + * @method postPckLossRatio + * + * @brief post pck loss ratio update to state db + * + * @param portName (in) port name + * @param unknownEventCount (in) count of missing icmp packets + * @param expectedPacketCount (in) count of expected icmp packets + * + * @return none + */ + virtual void postPckLossRatio( + const std::string &portName, + const uint64_t unknownEventCount, + const uint64_t expectedPacketCount + ); + /** *@method initialize * @@ -277,6 +312,40 @@ class DbInterface boost::posix_time::ptime time ); + /** + * @method handlePostLinkProberMetrics + * + * @brief post link prober pck loss event to state db + * + * @param portName (in) port name + * @param metrics (in) metrics data + * @param time (in) event time stamp + * + * @return none + */ + void handlePostLinkProberMetrics( + const std::string portName, + link_manager::LinkManagerStateMachine::LinkProberMetrics metrics, + boost::posix_time::ptime time + ); + + /** + * @method handlePostPckLossRatio + * + * @brief handle post pck loss ratio update + * + * @param portName (in) port name + * @param unknownEventCount (in) count of missing icmp packets + * @param expectedPacketCount (in) count of expected icmp packets + * + * @return none + */ + void handlePostPckLossRatio( + const std::string portName, + const uint64_t unknownEventCount, + const uint64_t expectedPacketCount + ); + /** *@method processTorMacAddress * @@ -488,6 +557,7 @@ class DbInterface static std::vector mMuxState; static std::vector mMuxLinkmgrState; static std::vector mMuxMetrics; + static std::vector mLinkProbeMetrics; private: mux::MuxManager *mMuxManagerPtr; @@ -505,6 +575,8 @@ class DbInterface std::shared_ptr mStateDbMuxLinkmgrTablePtr; // for writing mux metrics std::shared_ptr mStateDbMuxMetricsTablePtr; + // for writing link probe statistics data + std::shared_ptr mStateDbLinkProbeStatsTablePtr; std::shared_ptr mSwssThreadPtr; diff --git a/src/MuxManager.cpp b/src/MuxManager.cpp index a1bab80c..a01f4dd5 100644 --- a/src/MuxManager.cpp +++ b/src/MuxManager.cpp @@ -160,6 +160,21 @@ void MuxManager::updateMuxPortConfig(const std::string &portName, const std::str } } +// +// ---> resetPckLossCount(const std::string &portName); +// +// reset ICMP packet loss count +// +void MuxManager::resetPckLossCount(const std::string &portName) +{ + MUXLOGWARNING(boost::format("%s: reset ICMP packet loss count ") % portName); + + PortMapIterator portMapIterator = mPortMap.find(portName); + if(portMapIterator != mPortMap.end()) { + portMapIterator->second->resetPckLossCount(); + } +} + // // ---> addOrUpdateMuxPortLinkState(const std::string &portName, const std::string &linkState); // diff --git a/src/MuxManager.h b/src/MuxManager.h index eb54b3f1..d7c0c97c 100644 --- a/src/MuxManager.h +++ b/src/MuxManager.h @@ -245,6 +245,17 @@ class MuxManager */ void updateMuxPortConfig(const std::string &portName, const std::string &linkState); + /** + * @method resetPckLossCount + * + * @brief reset ICMP packet loss count. + * + * @param portName (in) Mux port name + * + * @return none + */ + void resetPckLossCount(const std::string &portName); + /** *@method addOrUpdateMuxPortLinkState * diff --git a/src/MuxPort.cpp b/src/MuxPort.cpp index 496d6235..912901ed 100644 --- a/src/MuxPort.cpp +++ b/src/MuxPort.cpp @@ -244,4 +244,21 @@ void MuxPort::handleDefaultRouteState(const std::string &routeState) routeState ))); } + +// +// ---> resetPckLossCount(); +// +// reset ICMP packet loss count +// +void MuxPort::resetPckLossCount() +{ + MUXLOGDEBUG(boost::format("port: %s, reset ICMP packet loss counts ") % mMuxPortConfig.getPortName()); + + boost::asio::io_service &ioService = mStrand.context(); + ioService.post(mStrand.wrap(boost::bind( + &link_manager::LinkManagerStateMachine::handleResetLinkProberPckLossCount, + &mLinkManagerStateMachine + ))); +} + } /* namespace mux */ diff --git a/src/MuxPort.h b/src/MuxPort.h index 3d2981a2..34e936df 100644 --- a/src/MuxPort.h +++ b/src/MuxPort.h @@ -164,6 +164,33 @@ class MuxPort: public std::enable_shared_from_this mDbInterfacePtr->postMetricsEvent(mMuxPortConfig.getPortName(), metrics, label); }; + /** + * @method postLinkProberMetricsEvent + * + * @brief post link prober pck loss event + * + * @param metrics (in) metrics to post + * + * @return none + */ + inline void postLinkProberMetricsEvent(link_manager::LinkManagerStateMachine::LinkProberMetrics metrics) { + mDbInterfacePtr->postLinkProberMetricsEvent(mMuxPortConfig.getPortName(), metrics); + }; + + /** + * @method postPckLossRatio + * + * @brief post pck loss ratio update to state db + * + * @param unknownEventCount (in) count of missing icmp packets + * @param expectedPacketCount (in) count of expected icmp packets + * + * @return none + */ + inline void postPckLossRatio(const uint64_t unknownEventCount, const uint64_t expectedPacketCount) { + mDbInterfacePtr->postPckLossRatio(mMuxPortConfig.getPortName(), unknownEventCount, expectedPacketCount); + }; + /** *@method setServerIpv4Address * @@ -263,6 +290,15 @@ class MuxPort: public std::enable_shared_from_this */ void handleDefaultRouteState(const std::string &routeState); + /** + * @method resetPckLossCount + * + * @brief reset ICMP packet loss count + * + * @return none + */ + void resetPckLossCount(); + protected: friend class test::MuxManagerTest; friend class test::FakeMuxPort; diff --git a/src/link_manager/LinkManagerStateMachine.cpp b/src/link_manager/LinkManagerStateMachine.cpp index dd547c99..9186a115 100644 --- a/src/link_manager/LinkManagerStateMachine.cpp +++ b/src/link_manager/LinkManagerStateMachine.cpp @@ -403,6 +403,9 @@ void LinkManagerStateMachine::handleSwssBladeIpv4AddressUpdate(boost::asio::ip:: mSendPeerSwitchCommandFnPtr = boost::bind( &link_prober::LinkProber::sendPeerSwitchCommand, mLinkProberPtr.get() ); + mResetIcmpPacketCountsFnPtr = boost::bind( + &link_prober::LinkProber::resetIcmpPacketCounts, mLinkProberPtr.get() + ); mComponentInitState.set(LinkProberComponent); activateStateMachine(); @@ -458,7 +461,7 @@ void LinkManagerStateMachine::activateStateMachine() // // ---> handleStateChange(LinkProberEvent &event, link_prober::LinkProberState::Label state); // -// handles LinkProverEvent +// handles LinkProberEvent // void LinkManagerStateMachine::handleStateChange(LinkProberEvent &event, link_prober::LinkProberState::Label state) { @@ -468,6 +471,15 @@ void LinkManagerStateMachine::handleStateChange(LinkProberEvent &event, link_pro mLinkProberStateName[state] ); + // update state db link prober metrics to collect pck loss data + if (mContinuousLinkProberUnknownEvent == true && state != link_prober::LinkProberState::Unknown) { + mContinuousLinkProberUnknownEvent = false; + mMuxPortPtr->postLinkProberMetricsEvent(link_manager::LinkManagerStateMachine::LinkProberMetrics::LinkProberUnknownEnd); + } else if (state == link_prober::LinkProberState::Label::Unknown) { + mContinuousLinkProberUnknownEvent = true; + mMuxPortPtr->postLinkProberMetricsEvent(link_manager::LinkManagerStateMachine::LinkProberMetrics::LinkProberUnknownStart); + } + CompositeState nextState = mCompositeState; ps(nextState) = state; mStateTransitionHandler[ps(nextState)][ms(nextState)][ls(nextState)](this, nextState); @@ -837,6 +849,33 @@ void LinkManagerStateMachine::handleDefaultRouteStateNotification(const std::str } } +// +// ---> handlePostPckLossRatioNotification(const uint64_t unknownEventCount, const uint64_t expectedPacketCount); +// +// handle post pck loss ratio +// +void LinkManagerStateMachine::handlePostPckLossRatioNotification(const uint64_t unknownEventCount, const uint64_t expectedPacketCount) +{ + MUXLOGDEBUG(boost::format("%s: posting pck loss ratio, pck_loss_count / pck_expected_count : %d / %d") % + mMuxPortConfig.getPortName() % + unknownEventCount % + expectedPacketCount + ); + + mMuxPortPtr->postPckLossRatio(unknownEventCount, expectedPacketCount); +} + +// ---> handleResetLinkProberPckLossCount(); +// +// reset link prober heartbeat packet loss count +// +void LinkManagerStateMachine::handleResetLinkProberPckLossCount() +{ + MUXLOGDEBUG(boost::format("%s: reset link prober packet loss counts ") % mMuxPortConfig.getPortName()); + + mResetIcmpPacketCountsFnPtr(); +} + // // ---> updateMuxLinkmgrState(); // diff --git a/src/link_manager/LinkManagerStateMachine.h b/src/link_manager/LinkManagerStateMachine.h index 6a985f7c..bc9700d2 100644 --- a/src/link_manager/LinkManagerStateMachine.h +++ b/src/link_manager/LinkManagerStateMachine.h @@ -121,6 +121,13 @@ class LinkManagerStateMachine: public common::StateMachine, Count }; + enum class LinkProberMetrics { + LinkProberUnknownStart, + LinkProberUnknownEnd, + + Count + }; + private: /** *@enum anonymous @@ -487,6 +494,27 @@ class LinkManagerStateMachine: public common::StateMachine, */ void handleDefaultRouteStateNotification(const std::string &routeState); + /** + * @method handlePostPckLossRatioNotification + * + * @brief handle get post pck loss ratio + * + * @param unknownEventCount (in) count of missing icmp packets + * @param expectedPacketCount (in) count of expected icmp packets + * + * @return none + */ + void handlePostPckLossRatioNotification(const uint64_t unknownEventCount, const uint64_t expectedPacketCount); + + /** + * @method handleResetLinkProberPckLossCount + * + * @brief reset link prober heartbeat packet loss count + * + * @return none + */ + void handleResetLinkProberPckLossCount(); + private: /** *@method updateMuxLinkmgrState @@ -926,6 +954,7 @@ class LinkManagerStateMachine: public common::StateMachine, boost::function mSuspendTxFnPtr; boost::function mResumeTxFnPtr; boost::function mSendPeerSwitchCommandFnPtr; + boost::function mResetIcmpPacketCountsFnPtr; uint32_t mWaitActiveUpCount = 0; uint32_t mMuxUnknownBackoffFactor = 1; @@ -933,6 +962,8 @@ class LinkManagerStateMachine: public common::StateMachine, bool mPendingMuxModeChange = false; common::MuxPortConfig::Mode mTargetMuxMode = common::MuxPortConfig::Mode::Auto; + bool mContinuousLinkProberUnknownEvent = false; + std::bitset mComponentInitState = {0}; Label mLabel = Label::Uninitialized; }; diff --git a/src/link_prober/LinkProber.cpp b/src/link_prober/LinkProber.cpp index 3af4fda9..21080833 100644 --- a/src/link_prober/LinkProber.cpp +++ b/src/link_prober/LinkProber.cpp @@ -439,6 +439,19 @@ void LinkProber::handleTimeout(boost::system::error_code errorCode) if (mTxSeqNo != mRxSelfSeqNo && mTxSeqNo != mRxPeerSeqNo) { // post unknown event mLinkProberStateMachine.postLinkProberStateEvent(LinkProberStateMachine::getIcmpUnknownEvent()); + mIcmpUnknownEventCount++; + } + + mIcmpPacketCount++; + if (mIcmpPacketCount % mMuxPortConfig.getNegativeStateChangeRetryCount() == 0) { + boost::asio::io_service::strand &strand = mLinkProberStateMachine.getStrand(); + boost::asio::io_service &ioService = strand.context(); + ioService.post(strand.wrap(boost::bind( + &LinkProberStateMachine::handlePckLossRatioUpdate, + &mLinkProberStateMachine, + mIcmpUnknownEventCount, + mIcmpPacketCount + ))); } // start another cycle of send/recv @@ -478,6 +491,7 @@ void LinkProber::startRecv() { MUXLOGTRACE(mMuxPortConfig.getPortName()); + mStream.async_read_some( boost::asio::buffer(mRxBuffer, MUX_MAX_ICMP_BUFFER_SIZE), mStrand.wrap(boost::bind( @@ -714,4 +728,24 @@ size_t LinkProber::appendTlvDummy(size_t paddingSize, int seqNo) return tlvSize; } +// +// ---> resetIcmpPacketCounts +// +// reset Icmp packet counts, post a pck loss ratio update immediately +// +void LinkProber::resetIcmpPacketCounts() +{ + mIcmpUnknownEventCount = 0; + mIcmpPacketCount = 0; + + boost::asio::io_service::strand &strand = mLinkProberStateMachine.getStrand(); + boost::asio::io_service &ioService = strand.context(); + ioService.post(strand.wrap(boost::bind( + &LinkProberStateMachine::handlePckLossRatioUpdate, + &mLinkProberStateMachine, + mIcmpUnknownEventCount, + mIcmpPacketCount + ))); +} + } /* namespace link_prober */ diff --git a/src/link_prober/LinkProber.h b/src/link_prober/LinkProber.h index b55cdcab..a9961575 100644 --- a/src/link_prober/LinkProber.h +++ b/src/link_prober/LinkProber.h @@ -160,6 +160,15 @@ class LinkProber */ void sendPeerSwitchCommand(); + /** + * @method resetIcmpPacketCounts() + * + * @brief reset Icmp packet counts, post a pck loss ratio update immediately + * + * @return none + */ + void resetIcmpPacketCounts(); + private: /** *@method handleUpdateEthernetFrame @@ -435,6 +444,9 @@ class LinkProber std::array mRxBuffer; bool mSuspendTx = false; + + uint64_t mIcmpUnknownEventCount = 0; + uint64_t mIcmpPacketCount = 0; }; } /* namespace link_prober */ diff --git a/src/link_prober/LinkProberStateMachine.cpp b/src/link_prober/LinkProberStateMachine.cpp index fb678268..15180461 100644 --- a/src/link_prober/LinkProberStateMachine.cpp +++ b/src/link_prober/LinkProberStateMachine.cpp @@ -255,4 +255,21 @@ void LinkProberStateMachine::handleMackAddressUpdate(const std::array handlePckLossRatioUpdate(const uint64_t unknownEventCount, const uint64_t expectedPacketCount); +// +// post pck loss ratio update to link manager +// +void LinkProberStateMachine::handlePckLossRatioUpdate(const uint64_t unknownEventCount, const uint64_t expectedPacketCount) +{ + boost::asio::io_service::strand &strand = mLinkManagerStateMachine.getStrand(); + boost::asio::io_service &ioService = strand.context(); + ioService.post(strand.wrap(boost::bind( + &link_manager::LinkManagerStateMachine::handlePostPckLossRatioNotification, + &mLinkManagerStateMachine, + unknownEventCount, + expectedPacketCount + ))); +} + } /* namespace link_prober */ diff --git a/src/link_prober/LinkProberStateMachine.h b/src/link_prober/LinkProberStateMachine.h index 18141d94..4ffa96b3 100644 --- a/src/link_prober/LinkProberStateMachine.h +++ b/src/link_prober/LinkProberStateMachine.h @@ -319,6 +319,18 @@ class LinkProberStateMachine: public common::StateMachine */ static SwitchActiveRequestEvent& getSwitchActiveRequestEvent() {return mSwitchActiveRequestEvent;}; + /** + * @method handlePckLossRatioUpdate + * + * @brief post pck loss ratio update to link manager + * + * @param unknownEventCount (in) count of missing icmp packets + * @param expectedPacketCount (in) count of expected icmp packets + * + * @return none + */ + void handlePckLossRatioUpdate(const uint64_t unknownEventCount, const uint64_t expectedPacketCount); + private: /** *@method postLinkManagerEvent diff --git a/test/FakeDbInterface.cpp b/test/FakeDbInterface.cpp index 7becc8d5..5e0dfba2 100644 --- a/test/FakeDbInterface.cpp +++ b/test/FakeDbInterface.cpp @@ -67,4 +67,22 @@ void FakeDbInterface::postMetricsEvent( mPostMetricsInvokeCount++; } +void FakeDbInterface::postLinkProberMetricsEvent( + const std::string &portName, + link_manager::LinkManagerStateMachine::LinkProberMetrics metrics +) +{ + mPostLinkProberMetricsInvokeCount++; +} + +void FakeDbInterface::postPckLossRatio( + const std::string &portName, + const uint64_t unknownEventCount, + const uint64_t expectedPacketCount +) +{ + mUnknownEventCount = unknownEventCount; + mExpectedPacketCount = expectedPacketCount; +} + } /* namespace test */ diff --git a/test/FakeDbInterface.h b/test/FakeDbInterface.h index 6a9e0e6d..c927012a 100644 --- a/test/FakeDbInterface.h +++ b/test/FakeDbInterface.h @@ -48,9 +48,20 @@ class FakeDbInterface: public mux::DbInterface link_manager::LinkManagerStateMachine::Metrics metrics, mux_state::MuxState::Label label ) override; + virtual void postLinkProberMetricsEvent( + const std::string &portName, + link_manager::LinkManagerStateMachine::LinkProberMetrics metrics + ) override; + virtual void postPckLossRatio( + const std::string &portName, + const uint64_t unknownEventCount, + const uint64_t expectedPacketCount + ) override; + void setNextMuxState(mux_state::MuxState::Label label) {mNextMuxState = label;}; + public: mux_state::MuxState::Label mNextMuxState; @@ -61,6 +72,9 @@ class FakeDbInterface: public mux::DbInterface uint32_t mProbeMuxStateInvokeCount = 0; uint32_t mSetMuxLinkmgrStateInvokeCount = 0; uint32_t mPostMetricsInvokeCount = 0; + uint32_t mPostLinkProberMetricsInvokeCount = 0; + uint64_t mUnknownEventCount = 0; + uint64_t mExpectedPacketCount = 0; }; } /* namespace test */ diff --git a/test/FakeLinkProber.cpp b/test/FakeLinkProber.cpp index 9b7ae3a6..256d3f1e 100644 --- a/test/FakeLinkProber.cpp +++ b/test/FakeLinkProber.cpp @@ -121,4 +121,20 @@ void FakeLinkProber::sendPeerSwitchCommand() mSendPeerSwitchCommand++; } +void FakeLinkProber::resetIcmpPacketCounts() +{ + MUXLOGINFO(""); + + mIcmpUnknownEventCount = 0; + mIcmpPacketCount = 0; + + boost::asio::io_service::strand &strand = mLinkProberStateMachine->getStrand(); + boost::asio::io_service &ioService = strand.context(); + ioService.post(strand.wrap(boost::bind( + &link_prober::LinkProberStateMachine::handlePckLossRatioUpdate, + mLinkProberStateMachine, + mIcmpUnknownEventCount, + mIcmpPacketCount + ))); +} } /* namespace test */ diff --git a/test/FakeLinkProber.h b/test/FakeLinkProber.h index a920dbe2..fa35ae3a 100644 --- a/test/FakeLinkProber.h +++ b/test/FakeLinkProber.h @@ -46,6 +46,7 @@ class FakeLinkProber void suspendTxProbes(uint32_t suspendTime_msec); void resumeTxProbes(); void sendPeerSwitchCommand(); + void resetIcmpPacketCounts(); public: uint32_t mInitializeCallCount = 0; @@ -56,6 +57,9 @@ class FakeLinkProber uint32_t mResumeTxProbeCallCount = 0; uint32_t mSendPeerSwitchCommand = 0; + uint64_t mIcmpUnknownEventCount = 0; + uint64_t mIcmpPacketCount = 0; + private: link_prober::LinkProberStateMachine *mLinkProberStateMachine; }; diff --git a/test/LinkManagerStateMachineTest.cpp b/test/LinkManagerStateMachineTest.cpp index dcd416ae..339fa9e3 100644 --- a/test/LinkManagerStateMachineTest.cpp +++ b/test/LinkManagerStateMachineTest.cpp @@ -236,6 +236,22 @@ void LinkManagerStateMachineTest::postDefaultRouteEvent(std::string routeState, runIoService(count); } +void LinkManagerStateMachineTest::postPckLossRatioUpdateEvent(uint64_t unknownCount, uint64_t totalCount) +{ + mFakeMuxPort.postPckLossRatio(unknownCount, totalCount); + mFakeMuxPort.mFakeLinkProber->mIcmpUnknownEventCount = unknownCount; + mFakeMuxPort.mFakeLinkProber->mIcmpPacketCount = totalCount; + + runIoService(); +} + +void LinkManagerStateMachineTest::postPckLossCountsResetEvent() +{ + mFakeMuxPort.mFakeLinkProber->resetIcmpPacketCounts(); + + runIoService(2); +} + TEST_F(LinkManagerStateMachineTest, MuxActiveSwitchOver) { setMuxActive(); @@ -1053,4 +1069,35 @@ TEST_F(LinkManagerStateMachineTest, MuxStandbyDefaultRouteStateOK) VALIDATE_STATE(Standby, Standby, Up); } +TEST_F(LinkManagerStateMachineTest, PostPckLossMetricsEvent) +{ + setMuxStandby(); + + EXPECT_EQ(mDbInterfacePtr->mPostLinkProberMetricsInvokeCount, 0); + postLinkProberEvent(link_prober::LinkProberState::Unknown, 3); + + EXPECT_EQ(mDbInterfacePtr->mPostLinkProberMetricsInvokeCount, 1); + postLinkProberEvent(link_prober::LinkProberState::Active, 3); + + EXPECT_EQ(mDbInterfacePtr->mPostLinkProberMetricsInvokeCount, 2); +} + +TEST_F(LinkManagerStateMachineTest, PostPckLossUpdateAndResetEvent) +{ + uint64_t unknownCount = 999; + uint64_t totalCount = 10000; + + postPckLossRatioUpdateEvent(unknownCount,totalCount); + EXPECT_EQ(mFakeMuxPort.mFakeLinkProber->mIcmpUnknownEventCount, unknownCount); + EXPECT_EQ(mFakeMuxPort.mFakeLinkProber->mIcmpPacketCount, totalCount); + EXPECT_EQ(mDbInterfacePtr->mUnknownEventCount, unknownCount); + EXPECT_EQ(mDbInterfacePtr->mExpectedPacketCount, totalCount); + + postPckLossCountsResetEvent(); + EXPECT_EQ(mFakeMuxPort.mFakeLinkProber->mIcmpUnknownEventCount, 0); + EXPECT_EQ(mFakeMuxPort.mFakeLinkProber->mIcmpPacketCount, 0); + EXPECT_EQ(mDbInterfacePtr->mUnknownEventCount, 0); + EXPECT_EQ(mDbInterfacePtr->mExpectedPacketCount, 0); +} + } /* namespace test */ diff --git a/test/LinkManagerStateMachineTest.h b/test/LinkManagerStateMachineTest.h index c752a0e1..f687ebcc 100644 --- a/test/LinkManagerStateMachineTest.h +++ b/test/LinkManagerStateMachineTest.h @@ -52,6 +52,8 @@ class LinkManagerStateMachineTest: public ::testing::Test void setMuxActive(); void setMuxStandby(); void postDefaultRouteEvent(std::string routeState, uint32_t count = 0); + void postPckLossRatioUpdateEvent(uint64_t unknownCount, uint64_t totalCount); + void postPckLossCountsResetEvent(); public: boost::asio::io_service mIoService;