Skip to content

Commit

Permalink
packet loss - add new class for uint64_t (nonce)
Browse files Browse the repository at this point in the history
  • Loading branch information
Consti10 committed Aug 8, 2023
1 parent 8155f10 commit 4dfbaf1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/HelperSources/NonceSeqNrHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -120,6 +122,7 @@ class NonceSeqNrHelper{
std::vector<int> 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
32 changes: 21 additions & 11 deletions src/WBTxRx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ WBTxRx::WBTxRx(std::vector<std::string> 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;i<m_wifi_cards.size();i++){
m_seq_nr_per_card[i]->set_store_and_debug_gaps(i,m_options.debug_packet_gaps);
}
}

WBTxRx::~WBTxRx() {
Expand Down Expand Up @@ -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(loss<lowest_loss){
lowest_loss=loss;
}
}
if(lowest_loss==INT32_MAX){
lowest_loss=-1;
}
m_rx_stats.curr_lowest_packet_loss=lowest_loss;
return true;
}
//m_console->debug("Got non-wb packet {}",radio_port);
Expand Down Expand Up @@ -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;
Expand All @@ -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_wifi_cards.size();i++){
RxStatsPerCard card_stats{};
card_stats.card_index=i;
Expand Down Expand Up @@ -646,7 +656,7 @@ std::string WBTxRx::tx_stats_to_string(const WBTxRx::TxStats& data) {
std::string WBTxRx::rx_stats_to_string(const WBTxRx::RxStats& data) {
return fmt::format("RxStats[packets any:{} session:{} valid:{} Loss:{}% pps:{} bps:{} foreign:{}%]",
data.count_p_any,data.n_received_valid_session_key_packets,data.count_p_valid,
data.curr_packet_loss,data.curr_packets_per_second,data.curr_bits_per_second,
data.curr_lowest_packet_loss,data.curr_packets_per_second,data.curr_bits_per_second,
data.curr_link_pollution_perc);
}
std::string WBTxRx::rx_stats_per_card_to_string(
Expand Down
5 changes: 2 additions & 3 deletions src/WBTxRx.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ class WBTxRx {
int64_t count_bytes_valid=0;
// Those values are recalculated in X second intervals.
// If no data arrives for a long time, they report -1 instead of 0
int32_t curr_packet_loss=-1;
// Current packet loss on whatever card reports the lowest packet loss (or card0 if there are not multiple RX cards)
int32_t curr_lowest_packet_loss=-1;
int32_t curr_packets_per_second=-1;
int32_t curr_bits_per_second=-1;
// n received valid session key packets
Expand Down Expand Up @@ -274,8 +275,6 @@ class WBTxRx {
std::unique_ptr<std::thread> m_receive_thread;
std::vector<pollfd> 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<std::shared_ptr<NonceSeqNrHelper>> m_seq_nr_per_card;
Expand Down

0 comments on commit 4dfbaf1

Please sign in to comment.