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;

File diff suppressed because it is too large Load Diff

@ -4,324 +4,261 @@
#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
{
#ifdef __LITTLE_ENDIAN__
unsigned int ihl : 4;
unsigned int version : 4;
#else
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;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
};
#else
// windows nt
#include <winsock2.h>
typedef struct ip_hdr
{
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 version : 4;
unsigned char pad_small : 4;
uint8_t pad[3];
};
struct ipv6_header
{
union
{
ipv6_header_preamble preamble;
uint32_t flowlabel;
} preamble;
uint16_t payload_len;
uint8_t proto;
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);
};
#include <memory>
#include <llarp/service/protocol_type.hpp>
#include <utility>
namespace llarp
namespace llarp::net
{
namespace net
template <bool is_little_endian>
struct ip_header_le
{
/// "well known" ip protocols
/// TODO: extend this to non "well known values"
enum class IPProtocol : uint8_t
{
ICMP = 0x01,
IGMP = 0x02,
IPIP = 0x04,
TCP = 0x06,
UDP = 0x11,
GRE = 0x2F,
ICMP6 = 0x3A,
OSFP = 0x59,
PGM = 0x71,
};
/// get string representation of this protocol
/// throws std::invalid_argument if we don't know the name of this ip protocol
std::string
IPProtocolName(IPProtocol proto);
unsigned int ihl : 4;
unsigned int version : 4;
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;
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;
};
using ip_header = ip_header_le<oxenc::little_endian>;
template <bool little>
struct ipv6_header_preamble_le
{
unsigned char pad_small : 4;
unsigned char version : 4;
uint8_t pad[3];
};
/// parse a string to an ip protocol
/// throws std::invalid_argument if cannot be parsed
IPProtocol
ParseIPProtocol(std::string data);
template <>
struct ipv6_header_preamble_le<false>
{
unsigned char version : 4;
unsigned char pad_small : 4;
uint8_t pad[3];
};
/// an Packet
struct IPPacket
struct ipv6_header
{
union
{
static constexpr size_t MaxSize = 1500;
llarp_time_t timestamp;
size_t sz;
byte_t buf[MaxSize];
ipv6_header_preamble_le<oxenc::little_endian> preamble;
uint32_t flowlabel;
} preamble;
uint16_t payload_len;
uint8_t proto;
uint8_t hoplimit;
in6_addr srcaddr;
in6_addr dstaddr;
llarp::nuint32_t
FlowLabel() const;
/// put 20 bit truncated flow label network order
void
FlowLabel(llarp::nuint32_t label);
};
/// "well known" ip protocols
/// TODO: extend this to non "well known values"
enum class IPProtocol : uint8_t
{
ICMP = 0x01,
IGMP = 0x02,
IPIP = 0x04,
TCP = 0x06,
UDP = 0x11,
GRE = 0x2F,
ICMP6 = 0x3A,
OSFP = 0x59,
PGM = 0x71,
};
/// get string representation of this protocol
/// throws std::invalid_argument if we don't know the name of this ip protocol
std::string
IPProtocolName(IPProtocol proto);
/// parse a string to an ip protocol
/// throws std::invalid_argument if cannot be parsed
IPProtocol
ParseIPProtocol(std::string data);
/// an Packet
struct IPPacket
{
static constexpr size_t MaxSize = 1500;
llarp_time_t timestamp;
size_t sz;
byte_t buf[MaxSize];
static IPPacket
UDP(nuint32_t srcaddr,
nuint16_t srcport,
nuint32_t dstaddr,
nuint16_t dstport,
const llarp_buffer_t& data);
static IPPacket
UDP(nuint32_t srcaddr,
nuint16_t srcport,
nuint32_t dstaddr,
nuint16_t dstport,
const llarp_buffer_t& data);
ManagedBuffer
Buffer();
ManagedBuffer
Buffer();
ManagedBuffer
ConstBuffer() const;
ManagedBuffer
ConstBuffer() const;
bool
Load(const llarp_buffer_t& buf);
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
operator()(const IPPacket& left, const IPPacket& right)
{
return left.sz < right.sz;
}
};
struct CompareOrder
{
bool
operator()(const IPPacket& left, const IPPacket& right)
{
return left.timestamp < right.timestamp;
}
};
inline ip_header*
Header()
struct CompareSize
{
bool
operator()(const IPPacket& left, const IPPacket& right)
{
return (ip_header*)&buf[0];
return left.sz < right.sz;
}
};
inline const ip_header*
Header() const
struct CompareOrder
{
bool
operator()(const IPPacket& left, const IPPacket& right)
{
return (ip_header*)&buf[0];
return left.timestamp < right.timestamp;
}
};
inline ipv6_header*
HeaderV6()
{
return (ipv6_header*)&buf[0];
}
inline ip_header*
Header()
{
return reinterpret_cast<ip_header*>(&buf[0]);
}
inline const ipv6_header*
HeaderV6() const
{
return (ipv6_header*)&buf[0];
}
inline const ip_header*
Header() const
{
return reinterpret_cast<const ip_header*>(&buf[0]);
}
inline int
Version() const
{
return Header()->version;
}
inline ipv6_header*
HeaderV6()
{
return reinterpret_cast<ipv6_header*>(&buf[0]);
}
inline bool
IsV4() const
{
return Version() == 4;
}
inline const ipv6_header*
HeaderV6() const
{
return reinterpret_cast<const ipv6_header*>(&buf[0]);
}
inline bool
IsV6() const
{
return Version() == 6;
}
inline int
Version() const
{
return Header()->version;
}
inline service::ProtocolType
ServiceProtocol() const
{
if (IsV4())
return service::ProtocolType::TrafficV4;
if (IsV6())
return service::ProtocolType::TrafficV6;
inline bool
IsV4() const
{
return Version() == 4;
}
return service::ProtocolType::Control;
}
inline bool
IsV6() const
{
return Version() == 6;
}
huint128_t
srcv6() const;
inline service::ProtocolType
ServiceProtocol() const
{
if (IsV4())
return service::ProtocolType::TrafficV4;
if (IsV6())
return service::ProtocolType::TrafficV6;
huint128_t
dstv6() const;
return service::ProtocolType::Control;
}
huint32_t
srcv4() const;
huint128_t
srcv6() const;
huint32_t
dstv4() const;
huint128_t
dstv6() const;
huint128_t
src4to6() const;
huint32_t
srcv4() const;
huint128_t
dst4to6() const;
huint32_t
dstv4() const;
huint128_t
src4to6Lan() const;
huint128_t
src4to6() const;
huint128_t
dst4to6Lan() const;
huint128_t
dst4to6() const;
/// get destination port if applicable
std::optional<nuint16_t>
DstPort() const;
huint128_t
src4to6Lan() const;
/// get source port if applicable
std::optional<nuint16_t>
SrcPort() const;
huint128_t
dst4to6Lan() const;
/// get pointer and size of layer 4 data
std::optional<std::pair<const char*, size_t>>
L4Data() const;
/// get destination port if applicable
std::optional<nuint16_t>
DstPort() const;
void
UpdateIPv4Address(nuint32_t src, nuint32_t dst);
/// get source port if applicable
std::optional<nuint16_t>
SrcPort() const;
void
UpdateIPv6Address(
huint128_t src, huint128_t dst, std::optional<nuint32_t> flowlabel = std::nullopt);
/// get pointer and size of layer 4 data
std::optional<std::pair<const char*, size_t>>
L4Data() const;
/// set addresses to zero and recacluate checksums
void
ZeroAddresses(std::optional<nuint32_t> flowlabel = std::nullopt);
void
UpdateIPv4Address(nuint32_t src, nuint32_t dst);
/// zero out source address
void
ZeroSourceAddress(std::optional<nuint32_t> flowlabel = std::nullopt);
void
UpdateIPv6Address(
huint128_t src, huint128_t dst, std::optional<nuint32_t> flowlabel = std::nullopt);
/// make an icmp unreachable reply packet based of this ip packet
std::optional<IPPacket>
MakeICMPUnreachable() const;
};
/// set addresses to zero and recacluate checksums
void
ZeroAddresses(std::optional<nuint32_t> flowlabel = std::nullopt);
/// zero out source address
void
ZeroSourceAddress(std::optional<nuint32_t> flowlabel = std::nullopt);
/// make an icmp unreachable reply packet based of this ip packet
std::optional<IPPacket>
MakeICMPUnreachable() const;
};
/// generate ip checksum
uint16_t
ipchksum(const byte_t* buf, size_t sz, uint32_t sum = 0);
/// 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