clean up ip packet code

pull/1917/head
Jeff 2 years ago
parent d30fe95f2e
commit 70b07bab44
No known key found for this signature in database
GPG Key ID: 025C02EE3A092F2D

@ -23,8 +23,6 @@ namespace llarp
, m_Name(std::move(name))
, m_LocalResolverAddr{"127.0.0.1:53"}
, m_QUIC{std::make_shared<quic::TunnelManager>(*this)}
, m_InetToNetwork(name + "_exit_rx", r->loop(), r->loop())
{
m_ShouldInitTun = true;
m_QUIC = std::make_shared<quic::TunnelManager>(*this);
@ -363,7 +361,11 @@ namespace llarp
void
ExitEndpoint::Flush()
{
m_InetToNetwork.Process([&](Pkt_t& pkt) {
while (not m_InetToNetwork.empty())
{
net::IPPacket pkt{m_InetToNetwork.top()};
m_InetToNetwork.pop();
PubKey pk;
{
auto itr = m_IPToKey.find(pkt.dstv6());
@ -371,7 +373,7 @@ namespace llarp
{
// drop
LogWarn(Name(), " dropping packet, has no session at ", pkt.dstv6());
return;
continue;
}
pk = itr->second;
}
@ -385,7 +387,7 @@ namespace llarp
if (itr != m_SNodeSessions.end())
{
itr->second->SendPacketToRemote(pkt.ConstBuffer(), service::ProtocolType::TrafficV4);
return;
continue;
}
}
auto tryFlushingTraffic = [&](exit::Endpoint* const ep) -> bool {
@ -412,7 +414,8 @@ namespace llarp
pk,
" as we have no working endpoints");
}
});
}
for (auto& [pubkey, endpoint] : m_ActiveExits)
{
if (!endpoint->Flush())
@ -633,7 +636,7 @@ namespace llarp
void
ExitEndpoint::OnInetPacket(net::IPPacket pkt)
{
m_InetToNetwork.Emplace(std::move(pkt));
m_InetToNetwork.emplace(std::move(pkt));
}
bool

@ -214,15 +214,8 @@ namespace llarp
std::shared_ptr<quic::TunnelManager> m_QUIC;
using Pkt_t = net::IPPacket;
using PacketQueue_t = util::CoDelQueue<
Pkt_t,
Pkt_t::GetTime,
Pkt_t::PutTime,
Pkt_t::CompareOrder,
Pkt_t::GetNow,
util::NullMutex,
util::NullLock>;
using PacketQueue_t = std::
priority_queue<net::IPPacket, std::vector<net::IPPacket>, net::IPPacket::CompareOrder>;
/// internet to llarp packet queue
PacketQueue_t m_InetToNetwork;

@ -7,7 +7,6 @@
#include <llarp/net/ip_packet.hpp>
#include <llarp/net/net.hpp>
#include <llarp/service/endpoint.hpp>
#include <llarp/util/codel.hpp>
#include <llarp/util/thread/threading.hpp>
#include <llarp/vpn/packet_router.hpp>
@ -174,13 +173,6 @@ namespace llarp
ResetInternalState() override;
protected:
using PacketQueue_t = llarp::util::CoDelQueue<
net::IPPacket,
net::IPPacket::GetTime,
net::IPPacket::PutTime,
net::IPPacket::CompareOrder,
net::IPPacket::GetNow>;
struct WritePacket
{
uint64_t seqno;

@ -13,26 +13,42 @@
#include <algorithm>
#include <map>
namespace llarp::net
{
constexpr uint32_t ipv6_flowlabel_mask = 0b0000'0000'0000'1111'1111'1111'1111'1111;
void
ipv6_header::FlowLabel(llarp::nuint32_t label)
template <bool little>
struct ipv6_header_preamble
{
// the ipv6 flow label is the last 20 bits in the first 32 bits of the header
preamble.flowlabel =
(htonl(ipv6_flowlabel_mask) & label.n) | (preamble.flowlabel & htonl(~ipv6_flowlabel_mask));
}
unsigned char pad_small : 4;
unsigned char version : 4;
uint8_t pad[3];
};
template <>
struct ipv6_header_preamble<false>
{
unsigned char version : 4;
unsigned char pad_small : 4;
uint8_t pad[3];
};
/// get 20 bit truncated flow label in network order
llarp::nuint32_t
ipv6_header::FlowLabel() const
{
return llarp::nuint32_t{preamble.flowlabel & htonl(ipv6_flowlabel_mask)};
}
namespace llarp
{
namespace net
/// put 20 bit truncated flow label network order
void
ipv6_header::FlowLabel(llarp::nuint32_t label)
{
// the ipv6 flow label is the last 20 bits in the first 32 bits of the header
preamble.flowlabel =
(htonl(ipv6_flowlabel_mask) & label.n) | (preamble.flowlabel & htonl(~ipv6_flowlabel_mask));
};
std::string
IPProtocolName(IPProtocol proto)
{
@ -650,5 +666,4 @@ namespace llarp
return pkt;
}
} // namespace net
} // namespace llarp
} // namespace llarp::net

@ -4,27 +4,33 @@
#include "net.hpp"
#include <llarp/util/buffer.hpp>
#include <llarp/util/time.hpp>
#include <oxenc/endian.h> // Guarantees __{LITTLE,BIG}_ENDIAN__ defines
#ifndef _WIN32
// unix, linux
#include <sys/types.h> // FreeBSD needs this for uchar for ip.h
#include <netinet/in.h>
#include <netinet/ip.h>
// anything not win32
struct ip_header
#include <memory>
#include <llarp/service/protocol_type.hpp>
#include <utility>
namespace llarp::net
{
template <bool is_little_endian>
struct ip_header_le
{
#ifdef __LITTLE_ENDIAN__
unsigned int ihl : 4;
unsigned int version : 4;
#else
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
};
template <>
struct ip_header_le<false>
{
unsigned int version : 4;
unsigned int ihl : 4;
#endif
#if defined(__linux__)
#define ip_version version
#endif
uint8_t tos;
uint16_t tot_len;
uint16_t id;
@ -35,45 +41,19 @@ struct ip_header
uint32_t saddr;
uint32_t daddr;
};
#else
// windows nt
#include <winsock2.h>
typedef struct ip_hdr
using ip_header = ip_header_le<oxenc::little_endian>;
template <bool little>
struct ipv6_header_preamble_le
{
unsigned char ip_header_len : 4; // 4-bit header length (in 32-bit words) normally=5
// (Means 20 Bytes may be 24 also)
unsigned char version : 4; // 4-bit IPv4 version
unsigned char ip_tos; // IP type of service
unsigned short ip_total_length; // Total length
unsigned short ip_id; // Unique identifier
unsigned char ip_frag_offset : 5; // Fragment offset field
unsigned char ip_more_fragment : 1;
unsigned char ip_dont_fragment : 1;
unsigned char ip_reserved_zero : 1;
unsigned char ip_frag_offset1; // fragment offset
unsigned char ip_ttl; // Time to live
unsigned char ip_protocol; // Protocol(TCP,UDP etc)
unsigned short ip_checksum; // IP checksum
unsigned int ip_srcaddr; // Source address
unsigned int ip_destaddr; // Source address
} IPV4_HDR;
#define ip_header IPV4_HDR
#define saddr ip_srcaddr
#define daddr ip_destaddr
#define check ip_checksum
#define ihl ip_header_len
#define protocol ip_protocol
#define frag_off ip_frag_offset
#define tos ip_tos
#define ttl ip_ttl
#define tot_len ip_total_length
#endif
struct ipv6_header_preamble
unsigned char pad_small : 4;
unsigned char version : 4;
uint8_t pad[3];
};
template <>
struct ipv6_header_preamble_le<false>
{
unsigned char version : 4;
unsigned char pad_small : 4;
@ -84,7 +64,7 @@ struct ipv6_header
{
union
{
ipv6_header_preamble preamble;
ipv6_header_preamble_le<oxenc::little_endian> preamble;
uint32_t flowlabel;
} preamble;
@ -93,24 +73,14 @@ struct ipv6_header
uint8_t hoplimit;
in6_addr srcaddr;
in6_addr dstaddr;
/// get 20 bit truncated flow label in network order
llarp::nuint32_t
FlowLabel() const;
/// put 20 bit truncated flow label network order
void
FlowLabel(llarp::nuint32_t flowlabel);
FlowLabel(llarp::nuint32_t label);
};
#include <memory>
#include <llarp/service/protocol_type.hpp>
#include <utility>
namespace llarp
{
namespace net
{
/// "well known" ip protocols
/// TODO: extend this to non "well known values"
enum class IPProtocol : uint8_t
@ -160,39 +130,6 @@ namespace llarp
bool
Load(const llarp_buffer_t& buf);
struct GetTime
{
llarp_time_t
operator()(const IPPacket& pkt) const
{
return pkt.timestamp;
}
};
struct PutTime
{
EventLoop_ptr loop;
PutTime(EventLoop_ptr evloop) : loop(std::move(evloop))
{}
void
operator()(IPPacket& pkt) const
{
pkt.timestamp = loop->time_now();
}
};
struct GetNow
{
EventLoop_ptr loop;
GetNow(EventLoop_ptr evloop) : loop(std::move(evloop))
{}
llarp_time_t
operator()() const
{
return loop->time_now();
}
};
struct CompareSize
{
bool
@ -214,25 +151,25 @@ namespace llarp
inline ip_header*
Header()
{
return (ip_header*)&buf[0];
return reinterpret_cast<ip_header*>(&buf[0]);
}
inline const ip_header*
Header() const
{
return (ip_header*)&buf[0];
return reinterpret_cast<const ip_header*>(&buf[0]);
}
inline ipv6_header*
HeaderV6()
{
return (ipv6_header*)&buf[0];
return reinterpret_cast<ipv6_header*>(&buf[0]);
}
inline const ipv6_header*
HeaderV6() const
{
return (ipv6_header*)&buf[0];
return reinterpret_cast<const ipv6_header*>(&buf[0]);
}
inline int
@ -323,5 +260,5 @@ namespace llarp
/// generate ip checksum
uint16_t
ipchksum(const byte_t* buf, size_t sz, uint32_t sum = 0);
} // namespace net
} // namespace llarp
} // namespace llarp::net

@ -1,167 +0,0 @@
#pragma once
#include <llarp/util/logging/logger.hpp>
#include "mem.hpp"
#include <llarp/util/thread/threading.hpp>
#include "time.hpp"
#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <string>
#include <utility>
namespace llarp
{
namespace util
{
struct GetNowSyscall
{
llarp_time_t
operator()() const
{
return llarp::time_now_ms();
}
};
template <
typename T,
typename GetTime,
typename PutTime,
typename Compare,
typename GetNow = GetNowSyscall,
typename Mutex_t = util::Mutex,
typename Lock_t = std::lock_guard<Mutex_t>,
size_t MaxSize = 1024>
struct CoDelQueue
{
CoDelQueue(std::string name, PutTime put, GetNow now)
: m_QueueIdx(0)
, m_name(std::move(name))
, _putTime(std::move(put))
, _getNow(std::move(now))
{}
size_t
Size() EXCLUDES(m_QueueMutex)
{
Lock_t lock(m_QueueMutex);
return m_QueueIdx;
}
template <typename... Args>
bool
EmplaceIf(std::function<bool(T&)> pred, Args&&... args) EXCLUDES(m_QueueMutex)
{
Lock_t lock(m_QueueMutex);
if (m_QueueIdx == MaxSize)
return false;
T* t = &m_Queue[m_QueueIdx];
new (t) T(std::forward<Args>(args)...);
if (!pred(*t))
{
t->~T();
return false;
}
_putTime(m_Queue[m_QueueIdx]);
if (firstPut == 0s)
firstPut = _getTime(m_Queue[m_QueueIdx]);
++m_QueueIdx;
return true;
}
template <typename... Args>
void
Emplace(Args&&... args) EXCLUDES(m_QueueMutex)
{
Lock_t lock(m_QueueMutex);
if (m_QueueIdx == MaxSize)
return;
T* t = &m_Queue[m_QueueIdx];
new (t) T(std::forward<Args>(args)...);
_putTime(m_Queue[m_QueueIdx]);
if (firstPut == 0s)
firstPut = _getTime(m_Queue[m_QueueIdx]);
++m_QueueIdx;
}
template <typename Visit>
void
Process(Visit v)
{
return Process(v, [](T&) -> bool { return false; });
}
template <typename Visit, typename Filter>
void
Process(Visit visitor, Filter f) EXCLUDES(m_QueueMutex)
{
llarp_time_t lowest = std::numeric_limits<llarp_time_t>::max();
if (_getNow() < nextTickAt)
return;
// llarp::LogInfo("CoDelQueue::Process - start at ", start);
Lock_t lock(m_QueueMutex);
auto start = firstPut;
if (m_QueueIdx == 1)
{
visitor(m_Queue[0]);
T* t = &m_Queue[0];
t->~T();
m_QueueIdx = 0;
firstPut = 0s;
return;
}
size_t idx = 0;
while (m_QueueIdx)
{
llarp::LogDebug(m_name, " - queue has ", m_QueueIdx);
T* item = &m_Queue[idx++];
if (f(*item))
break;
--m_QueueIdx;
const llarp_time_t dlt = start - _getTime(*item);
// llarp::LogInfo("CoDelQueue::Process - dlt ", dlt);
lowest = std::min(dlt, lowest);
if (m_QueueIdx == 0)
{
// llarp::LogInfo("CoDelQueue::Process - single item: lowest ",
// lowest, " dropMs: ", dropMs);
if (lowest > dropMs)
{
item->~T();
nextTickInterval += initialIntervalMs / uint64_t(std::sqrt(++dropNum));
firstPut = 0s;
nextTickAt = start + nextTickInterval;
return;
}
nextTickInterval = initialIntervalMs;
dropNum = 0;
}
visitor(*item);
item->~T();
}
firstPut = 0s;
nextTickAt = start + nextTickInterval;
}
const llarp_time_t initialIntervalMs = 5ms;
const llarp_time_t dropMs = 100ms;
llarp_time_t firstPut = 0s;
size_t dropNum = 0;
llarp_time_t nextTickInterval = initialIntervalMs;
llarp_time_t nextTickAt = 0s;
Mutex_t m_QueueMutex;
size_t m_QueueIdx GUARDED_BY(m_QueueMutex);
std::array<T, MaxSize> m_Queue GUARDED_BY(m_QueueMutex);
std::string m_name;
GetTime _getTime;
PutTime _putTime;
GetNow _getNow;
}; // namespace util
} // namespace util
} // namespace llarp
Loading…
Cancel
Save