2017-02-10 14:25:03 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2005-2017, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_PACKET_POOL_HPP
|
|
|
|
#define TORRENT_PACKET_POOL_HPP
|
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2017-02-11 02:54:26 +01:00
|
|
|
#include "libtorrent/aux_/throw.hpp"
|
2017-02-10 14:25:03 +01:00
|
|
|
#include "libtorrent/aux_/vector.hpp"
|
2017-02-11 02:54:26 +01:00
|
|
|
#include "libtorrent/aux_/numeric_cast.hpp"
|
2017-02-11 20:21:48 +01:00
|
|
|
#include "libtorrent/time.hpp"
|
2017-02-11 02:54:26 +01:00
|
|
|
#include "libtorrent/assert.hpp"
|
2017-02-11 20:21:48 +01:00
|
|
|
#include "libtorrent/debug.hpp" // for single_threaded
|
2017-02-10 14:25:03 +01:00
|
|
|
|
2017-02-27 17:50:53 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2017-02-11 20:21:48 +01:00
|
|
|
// internal: some MTU and protocol header sizes constants
|
|
|
|
constexpr int TORRENT_IPV4_HEADER = 20;
|
|
|
|
constexpr int TORRENT_IPV6_HEADER = 40;
|
|
|
|
constexpr int TORRENT_UDP_HEADER = 8;
|
|
|
|
constexpr int TORRENT_UTP_HEADER = 20;
|
|
|
|
constexpr int TORRENT_SOCKS5_HEADER = 6; // plus the size of the destination address
|
|
|
|
|
|
|
|
constexpr int TORRENT_ETHERNET_MTU = 1500;
|
|
|
|
constexpr int TORRENT_TEREDO_MTU = 1280;
|
|
|
|
constexpr int TORRENT_INET_MIN_MTU = 576;
|
|
|
|
constexpr int TORRENT_INET_MAX_MTU = 0xffff;
|
2017-02-10 14:25:03 +01:00
|
|
|
|
|
|
|
// used for out-of-order incoming packets
|
|
|
|
// as well as sent packets that are waiting to be ACKed
|
|
|
|
struct packet
|
|
|
|
{
|
|
|
|
// the last time this packet was sent
|
|
|
|
time_point send_time;
|
|
|
|
|
|
|
|
// the number of bytes actually allocated in 'buf'
|
|
|
|
std::uint16_t allocated;
|
|
|
|
|
|
|
|
// the size of the buffer 'buf' points to
|
|
|
|
std::uint16_t size;
|
|
|
|
|
|
|
|
// this is the offset to the payload inside the buffer
|
|
|
|
// this is also used as a cursor to describe where the
|
|
|
|
// next payload that hasn't been consumed yet starts
|
|
|
|
std::uint16_t header_size;
|
|
|
|
|
|
|
|
// the number of times this packet has been sent
|
|
|
|
std::uint8_t num_transmissions:6;
|
|
|
|
|
|
|
|
// true if we need to send this packet again. All
|
|
|
|
// outstanding packets are marked as needing to be
|
|
|
|
// resent on timeouts
|
|
|
|
bool need_resend:1;
|
|
|
|
|
|
|
|
// this is set to true for packets that were
|
|
|
|
// sent with the DF bit set (Don't Fragment)
|
|
|
|
bool mtu_probe:1;
|
|
|
|
|
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
int num_fast_resend;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// the actual packet buffer
|
|
|
|
std::uint8_t buf[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct packet_deleter
|
|
|
|
{
|
|
|
|
// deleter for std::unique_ptr
|
|
|
|
void operator()(packet* p) const
|
|
|
|
{
|
2017-02-11 02:54:26 +01:00
|
|
|
TORRENT_ASSERT(p != nullptr);
|
2017-02-10 14:25:03 +01:00
|
|
|
p->~packet();
|
|
|
|
std::free(p);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-11 20:21:48 +01:00
|
|
|
using packet_ptr = std::unique_ptr<packet, packet_deleter>;
|
|
|
|
|
|
|
|
inline packet_ptr create_packet(int const size)
|
2017-02-10 14:25:03 +01:00
|
|
|
{
|
2017-02-23 05:28:25 +01:00
|
|
|
packet* p = static_cast<packet*>(std::malloc(sizeof(packet) + aux::numeric_cast<std::uint16_t>(size)));
|
2017-02-11 02:54:26 +01:00
|
|
|
if (p == nullptr) aux::throw_ex<std::bad_alloc>();
|
2017-02-10 14:25:03 +01:00
|
|
|
new (p) packet();
|
2017-02-11 02:54:26 +01:00
|
|
|
p->allocated = aux::numeric_cast<std::uint16_t>(size);
|
2017-02-11 20:21:48 +01:00
|
|
|
return packet_ptr(p);
|
2017-02-10 14:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TORRENT_EXTRA_EXPORT packet_slab
|
|
|
|
{
|
2017-02-11 02:54:26 +01:00
|
|
|
int const allocate_size;
|
2017-02-10 14:25:03 +01:00
|
|
|
|
2017-02-11 20:21:48 +01:00
|
|
|
explicit packet_slab(int const alloc_size, std::size_t const limit = 10)
|
2017-02-11 02:54:26 +01:00
|
|
|
: allocate_size(alloc_size)
|
|
|
|
, m_limit(limit)
|
|
|
|
{
|
|
|
|
m_storage.reserve(m_limit);
|
|
|
|
}
|
2017-02-10 14:25:03 +01:00
|
|
|
|
|
|
|
packet_slab(const packet_slab&) = delete;
|
|
|
|
|
|
|
|
void try_push_back(packet_ptr &p)
|
|
|
|
{
|
|
|
|
if (m_storage.size() < m_limit)
|
|
|
|
m_storage.push_back(std::move(p));
|
|
|
|
}
|
|
|
|
|
|
|
|
packet_ptr alloc()
|
|
|
|
{
|
2017-02-11 20:21:48 +01:00
|
|
|
if (m_storage.empty()) return create_packet(allocate_size);
|
2017-02-11 02:54:26 +01:00
|
|
|
auto ret = std::move(m_storage.back());
|
|
|
|
m_storage.pop_back();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void decay()
|
|
|
|
{
|
|
|
|
if (m_storage.empty()) return;
|
2017-02-27 17:50:53 +01:00
|
|
|
m_storage.erase(m_storage.end() - 1);
|
2017-02-10 14:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::size_t m_limit;
|
2017-02-11 20:21:48 +01:00
|
|
|
std::vector<packet_ptr> m_storage;
|
2017-02-10 14:25:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// single thread packet allocation packet pool
|
|
|
|
// can handle common cases of packet size by 3 pools
|
|
|
|
struct TORRENT_EXTRA_EXPORT packet_pool : private single_threaded
|
|
|
|
{
|
2017-02-11 20:21:48 +01:00
|
|
|
packet_ptr acquire(int const allocate)
|
2017-02-10 14:25:03 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_single_thread());
|
|
|
|
TORRENT_ASSERT(allocate >= 0);
|
|
|
|
TORRENT_ASSERT(allocate <= std::numeric_limits<std::uint16_t>::max());
|
|
|
|
|
2017-02-11 20:21:48 +01:00
|
|
|
return alloc(allocate);
|
2017-02-10 14:25:03 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 20:21:48 +01:00
|
|
|
void release(packet_ptr p)
|
2017-02-10 14:25:03 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_single_thread());
|
|
|
|
|
2017-02-11 20:21:48 +01:00
|
|
|
if (p.get() == nullptr) return;
|
2017-02-10 14:25:03 +01:00
|
|
|
|
2017-02-11 20:21:48 +01:00
|
|
|
int const allocated = p->allocated;
|
2017-02-10 14:25:03 +01:00
|
|
|
|
2017-02-11 20:21:48 +01:00
|
|
|
if (allocated == m_syn_slab.allocate_size) { m_syn_slab.try_push_back(p); }
|
|
|
|
else if (allocated == m_mtu_floor_slab.allocate_size) { m_mtu_floor_slab.try_push_back(p); }
|
|
|
|
else if (allocated == m_mtu_ceiling_slab.allocate_size) { m_mtu_ceiling_slab.try_push_back(p); }
|
2017-02-10 14:25:03 +01:00
|
|
|
}
|
|
|
|
|
2017-02-11 02:54:26 +01:00
|
|
|
// periodically free up some of the cached packets
|
|
|
|
void decay()
|
2017-02-10 14:25:03 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(is_single_thread());
|
|
|
|
|
2017-02-11 02:54:26 +01:00
|
|
|
m_syn_slab.decay();
|
|
|
|
m_mtu_floor_slab.decay();
|
|
|
|
m_mtu_ceiling_slab.decay();
|
2017-02-10 14:25:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-02-11 02:54:26 +01:00
|
|
|
packet_ptr alloc(int const allocate)
|
2017-02-10 14:25:03 +01:00
|
|
|
{
|
|
|
|
if (allocate <= m_syn_slab.allocate_size) { return m_syn_slab.alloc(); }
|
|
|
|
else if (allocate <= m_mtu_floor_slab.allocate_size) { return m_mtu_floor_slab.alloc(); }
|
|
|
|
else if (allocate <= m_mtu_ceiling_slab.allocate_size) { return m_mtu_ceiling_slab.alloc(); }
|
2017-02-11 20:21:48 +01:00
|
|
|
return create_packet(allocate);
|
2017-02-10 14:25:03 +01:00
|
|
|
}
|
2017-02-11 02:54:26 +01:00
|
|
|
static int const mtu_floor_size = TORRENT_INET_MIN_MTU - TORRENT_IPV4_HEADER - TORRENT_UDP_HEADER;
|
|
|
|
static int const mtu_ceiling_size = TORRENT_ETHERNET_MTU - TORRENT_IPV4_HEADER - TORRENT_UDP_HEADER;
|
2017-02-11 20:21:48 +01:00
|
|
|
packet_slab m_syn_slab{ TORRENT_UTP_HEADER };
|
|
|
|
packet_slab m_mtu_floor_slab{ mtu_floor_size };
|
|
|
|
packet_slab m_mtu_ceiling_slab{ mtu_ceiling_size };
|
2017-02-10 14:25:03 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_PACKET_POOL_HPP
|