diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 5fda11429..23aaca60c 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -257,8 +257,6 @@ namespace libtorrent const stat& statistics() const { return m_statistics; } void add_stat(size_type downloaded, size_type uploaded); - void calc_ip_overhead(); - // is called once every second by the main loop void second_tick(float tick_interval); diff --git a/include/libtorrent/stat.hpp b/include/libtorrent/stat.hpp index 671b868de..dfcf82c01 100644 --- a/include/libtorrent/stat.hpp +++ b/include/libtorrent/stat.hpp @@ -130,16 +130,16 @@ namespace libtorrent m_stat[i] += s.m_stat[i]; } - void sent_syn() + void sent_syn(bool ipv6) { - m_stat[upload_ip_protocol].add(40); + m_stat[upload_ip_protocol].add(ipv6 ? 60 : 40); } - void received_synack() + void received_synack(bool ipv6) { // we received SYN-ACK and also sent ACK back - m_stat[download_ip_protocol].add(40); - m_stat[upload_ip_protocol].add(40); + m_stat[download_ip_protocol].add(ipv6 ? 60 : 40); + m_stat[upload_ip_protocol].add(ipv6 ? 60 : 40); } void received_dht_bytes(int bytes) @@ -184,24 +184,19 @@ namespace libtorrent m_stat[upload_protocol].add(bytes_protocol); } - // calculate ip protocol overhead - void calc_ip_overhead() + // and IP packet was received or sent + // account for the overhead caused by it + void trancieve_ip_packet(int bytes_transferred, bool ipv6) { - int uploaded = m_stat[upload_protocol].counter() - + m_stat[upload_payload].counter(); - int downloaded = m_stat[download_protocol].counter() - + m_stat[download_payload].counter(); - - // IP + TCP headers are 40 bytes per MTU (1460) - // bytes of payload, but at least 40 bytes - m_stat[upload_ip_protocol].add((std::max)(uploaded / 1460, uploaded>0?40:0)); - m_stat[download_ip_protocol].add((std::max)(downloaded / 1460, downloaded>0?40:0)); - - // also account for ACK traffic. That adds to the transfers - // in the opposite direction. Even on connections with symmetric - // transfer rates, it seems to add a penalty. - m_stat[upload_ip_protocol].add((std::max)(downloaded * 40 / 1460, downloaded>0?40:0)); - m_stat[download_ip_protocol].add((std::max)(uploaded * 40 / 1460, uploaded>0?40:0)); + // one TCP/IP packet header for the packet + // sent or received, and one for the ACK + // The IPv4 header is 20 bytes + // and IPv6 header is 40 bytes + const int header = (ipv6 ? 40 : 20) + 20; + const int mtu = 1500; + const int overhead = (std::max)(1, bytes_transferred / (mtu - header)) * header; + m_stat[download_ip_protocol].add(overhead); + m_stat[upload_ip_protocol].add(overhead); } int upload_ip_overhead() const { return m_stat[upload_ip_protocol].counter(); } diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 8c9472f05..00830692f 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -430,7 +430,7 @@ namespace libtorrent { namespace dht { mutex_t::scoped_lock l(m_mutex); // account for IP and UDP overhead - m_received_bytes += bytes_transferred + 28; + m_received_bytes += bytes_transferred + (ep.address().is_v6() ? 48 : 28); node_ban_entry* match = 0; node_ban_entry* min = m_ban_nodes; @@ -1127,7 +1127,7 @@ namespace libtorrent { namespace dht if (m_sock.send(m.addr, &m_send_buf[0], (int)m_send_buf.size(), ec, send_flags)) { // account for IP and UDP overhead - m_sent_bytes += m_send_buf.size() + 28; + m_sent_bytes += m_send_buf.size() + (m.addr.address().is_v6() ? 48 : 28); #ifdef TORRENT_DHT_VERBOSE_LOGGING m_total_out_bytes += m_send_buf.size(); diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 44486f7ea..6915e4150 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -2499,7 +2499,6 @@ namespace libtorrent if (t) { // make sure we keep all the stats! - calc_ip_overhead(); t->add_stats(statistics()); if (t->has_picker()) @@ -2754,11 +2753,6 @@ namespace libtorrent m_packet_size = packet_size; } - void peer_connection::calc_ip_overhead() - { - m_statistics.calc_ip_overhead(); - } - void peer_connection::second_tick(float tick_interval) { ptime now(time_now()); @@ -3508,6 +3502,8 @@ namespace libtorrent TORRENT_ASSERT(m_channel_state[download_channel] == peer_info::bw_network); m_channel_state[download_channel] = peer_info::bw_idle; + m_statistics.trancieve_ip_packet(bytes_transferred, m_remote.address().is_v6()); + if (error) { #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING @@ -3707,7 +3703,7 @@ namespace libtorrent m_socket->async_connect(m_remote , bind(&peer_connection::on_connection_complete, self(), _1)); m_connect = time_now(); - m_statistics.sent_syn(); + m_statistics.sent_syn(m_remote.address().is_v6()); if (t->alerts().should_post()) { @@ -3747,7 +3743,7 @@ namespace libtorrent // this means the connection just succeeded - m_statistics.received_synack(); + m_statistics.received_synack(m_remote.address().is_v6()); TORRENT_ASSERT(m_socket); #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING @@ -3803,6 +3799,8 @@ namespace libtorrent if (!m_ignore_bandwidth_limits) m_bandwidth_limit[upload_channel].use_quota(bytes_transferred); + m_statistics.trancieve_ip_packet(bytes_transferred, m_remote.address().is_v6()); + #ifdef TORRENT_VERBOSE_LOGGING (*m_logger) << "wrote " << bytes_transferred << " bytes\n"; #endif diff --git a/src/torrent.cpp b/src/torrent.cpp index 270a56785..7c5d73c54 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -4241,7 +4241,6 @@ namespace libtorrent { peer_connection* p = *i; ++i; - p->calc_ip_overhead(); m_stat += p->statistics(); // updates the peer connection's ul/dl bandwidth // resource requests