From 4dfbaf1ba9ec6447c94ff62ecbd437993616f1af Mon Sep 17 00:00:00 2001 From: consti10 Date: Tue, 8 Aug 2023 13:53:45 +0200 Subject: [PATCH] packet loss - add new class for uint64_t (nonce) --- src/HelperSources/NonceSeqNrHelper.h | 9 +++++--- src/WBTxRx.cpp | 32 ++++++++++++++++++---------- src/WBTxRx.h | 5 ++--- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/HelperSources/NonceSeqNrHelper.h b/src/HelperSources/NonceSeqNrHelper.h index 0d2f17e5..3a0d8464 100644 --- a/src/HelperSources/NonceSeqNrHelper.h +++ b/src/HelperSources/NonceSeqNrHelper.h @@ -12,7 +12,7 @@ #include "StringHelper.hpp" // Helper for dealing with sequence number -// (calculate packet loss an more) +// (calculate packet loss and more) // Using a unique uint64_t nonce class NonceSeqNrHelper{ public: @@ -22,6 +22,7 @@ class NonceSeqNrHelper{ int16_t get_current_gaps_counter(){ return m_curr_gaps_counter; } + // NOTE: Does no packet re-ordering, therefore can only be used per card ! void on_new_sequence_number(uint64_t seq_nr){ if(m_last_seq_nr==UINT64_MAX){ m_last_seq_nr=seq_nr; @@ -61,8 +62,9 @@ class NonceSeqNrHelper{ m_curr_loss_perc=-1; m_curr_gaps_counter=-1; } - void set_store_and_debug_gaps(bool enable){ + void set_store_and_debug_gaps(int card_idx,bool enable){ m_store_and_debug_gaps=enable; + m_card_index=card_idx; } private: // recalculate the loss in percentage in fixed intervals @@ -90,7 +92,7 @@ class NonceSeqNrHelper{ m_gaps.push_back(gap_size); const auto elasped=std::chrono::steady_clock::now()-m_last_gap_log; if(elasped>std::chrono::seconds(1) || m_gaps.size()>=MAX_N_STORED_GAPS){ - wifibroadcast::log::get_default()->debug("Gaps: {}",StringHelper::vectorAsString(m_gaps)); + wifibroadcast::log::get_default()->debug("Card{} Gaps: {}",m_card_index,StringHelper::vectorAsString(m_gaps)); m_gaps.resize(0); m_last_gap_log=std::chrono::steady_clock::now(); } @@ -120,6 +122,7 @@ class NonceSeqNrHelper{ std::vector m_gaps; static constexpr int GAP_SIZE_COUNTS_AS_BIG_GAP=10; bool m_store_and_debug_gaps= false; + int m_card_index=0; }; #endif // WIFIBROADCAST_NONCESEQNRHELPER_H diff --git a/src/WBTxRx.cpp b/src/WBTxRx.cpp index 396a60a0..4ee96aa4 100644 --- a/src/WBTxRx.cpp +++ b/src/WBTxRx.cpp @@ -56,8 +56,11 @@ WBTxRx::WBTxRx(std::vector wifi_cards,Options options1) // next session key in delta ms if packets are being fed m_session_key_next_announce_ts = std::chrono::steady_clock::now(); // Per libsodium documentation, the first nonce should be chosen randomly + // This selects a random nonce in 32-bit range - we therefore have still 32-bit increasing indexes left, which means tx can run indefinitely m_nonce=randombytes_random(); - m_seq_nr_helper.set_store_and_debug_gaps(m_options.debug_packet_gaps); + for(int i=0;iset_store_and_debug_gaps(i,m_options.debug_packet_gaps); + } } WBTxRx::~WBTxRx() { @@ -468,16 +471,25 @@ bool WBTxRx::process_received_data_packet(int wlan_idx,uint8_t stream_index,bool } } on_valid_packet(nonce,wlan_idx,stream_index,decrypted->data(),decrypted->size()); - if(wlan_idx==0){ - uint16_t tmp=nonce; - m_seq_nr_helper.on_new_sequence_number(tmp); - m_rx_stats.curr_packet_loss=m_seq_nr_helper.get_current_loss_percent(); - //m_console->debug("packet loss:{}",m_seq_nr_helper.get_current_loss_percent()); - } // Calculate sequence number stats per card auto& seq_nr_for_card=m_seq_nr_per_card.at(wlan_idx); seq_nr_for_card->on_new_sequence_number((uint16_t)nonce); m_rx_stats_per_card.at(wlan_idx).curr_packet_loss=seq_nr_for_card->get_current_loss_percent(); + // Update the main loss to whichever card reports the lowest loss + int lowest_loss=INT32_MAX; + for(auto& card_loss: m_seq_nr_per_card){ + const auto loss=card_loss->get_current_loss_percent(); + if(loss<0){ + continue ; + } + if(lossdebug("Got non-wb packet {}",radio_port); @@ -597,8 +609,7 @@ WBTxRx::TxStats WBTxRx::get_tx_stats() { WBTxRx::RxStats WBTxRx::get_rx_stats() { WBTxRx::RxStats ret=m_rx_stats; - ret.curr_packet_loss=m_seq_nr_helper.get_current_loss_percent(); - ret.curr_big_gaps_counter=m_seq_nr_helper.get_current_gaps_counter(); + ret.curr_big_gaps_counter=0; ret.curr_bits_per_second=m_rx_bitrate_calculator.get_last_or_recalculate(ret.count_bytes_valid); ret.curr_packets_per_second=m_rx_packets_per_second_calculator.get_last_or_recalculate(ret.count_p_valid); return ret; @@ -612,7 +623,6 @@ void WBTxRx::rx_reset_stats() { m_rx_stats=RxStats{}; m_rx_bitrate_calculator.reset(); m_rx_packets_per_second_calculator.reset(); - m_seq_nr_helper.reset(); for(int i=0;i m_receive_thread; std::vector m_receive_pollfds; std::chrono::steady_clock::time_point m_last_receiver_error_log=std::chrono::steady_clock::now(); - // for calculating the packet loss on the rx side - NonceSeqNrHelper m_seq_nr_helper; seq_nr::Helper m_seq_nr_helper_iee80211; // for calculating the loss per rx card (when multiple rx cards are used) std::vector> m_seq_nr_per_card;