Merge branch 'vanity' into exit-traffic-tuning

This commit is contained in:
Jeff Becker 2018-11-28 08:04:25 -05:00
commit 226f5520b3
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05
7 changed files with 50 additions and 14 deletions

View File

@ -19,7 +19,7 @@ extern "C"
}
namespace llarp
{
/// aligned buffer, aligns to the nears Long_t
/// aligned buffer that is sz bytes long and aligns to the nears Long_t
template < size_t sz, bool randomize = false, typename Long_t = uint64_t >
struct AlignedBuffer
{

View File

@ -35,6 +35,10 @@ namespace llarp
bool
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 5000) const;
/// return true if this endpoint looks dead right now
bool
LooksDead(llarp_time_t now, llarp_time_t timeout = 10000) const;
/// tick ourself, reset tx/rx rates
void
Tick(llarp_time_t now);
@ -92,6 +96,7 @@ namespace llarp
llarp::PathID_t m_CurrentPath;
llarp::huint32_t m_IP;
uint64_t m_TxRate, m_RxRate;
llarp_time_t m_LastActive;
bool m_RewriteSource;
};
} // namespace exit

View File

@ -44,6 +44,9 @@ namespace llarp
llarp_router*
Router();
llarp_time_t
Now() const;
llarp_crypto*
Crypto();

View File

@ -13,9 +13,10 @@ namespace llarp
private:
llarp::PubKey enckey;
llarp::PubKey signkey;
VanityNonce vanity;
public:
VanityNonce vanity;
ServiceInfo() = default;
ServiceInfo(ServiceInfo&& other)
@ -57,10 +58,12 @@ namespace llarp
}
bool
Update(const byte_t* enc, const byte_t* sign)
Update(const byte_t* enc, const byte_t* sign, const byte_t * nonce=nullptr)
{
enckey = enc;
signkey = sign;
if(nonce)
vanity = nonce;
return UpdateAddr();
}

View File

@ -14,6 +14,7 @@ namespace llarp
, m_IP(ip)
, m_RewriteSource(rewriteIP)
{
m_LastActive = parent->Now();
}
Endpoint::~Endpoint()
@ -65,6 +66,15 @@ namespace llarp
return true;
}
bool Endpoint::LooksDead(llarp_time_t now, llarp_time_t timeout) const
{
if(ExpiresSoon(now, timeout))
return true;
if (now > m_LastActive)
return now - m_LastActive > timeout;
return true;
}
bool
Endpoint::SendOutboundTraffic(llarp_buffer_t buf)
{
@ -83,6 +93,7 @@ namespace llarp
return false;
}
m_TxRate += buf.sz;
m_LastActive = m_Parent->Now();
return true;
}

View File

@ -37,10 +37,16 @@ namespace llarp
{
}
llarp_time_t
ExitEndpoint::Now() const
{
return m_Router->Now();
}
void
ExitEndpoint::FlushInbound()
{
auto now = Router()->Now();
auto now = Now();
m_InetToNetwork.Process([&](Pkt_t &pkt) {
llarp::PubKey pk;
{
@ -58,23 +64,27 @@ namespace llarp
auto range = m_ActiveExits.equal_range(pk);
auto itr = range.first;
uint64_t min = std::numeric_limits< uint64_t >::max();
/// pick path with lowest rx rate
/// pick non dead looking path with lowest tx rate
while(itr != range.second)
{
if(ep == nullptr)
ep = itr->second.get();
else if(itr->second->RxRate() < min && !itr->second->ExpiresSoon(now))
if(itr->second->TxRate() < min && !itr->second->LooksDead(now))
{
min = ep->RxRate();
ep = itr->second.get();
min = ep->TxRate();
}
++itr;
}
if(ep)
if(ep == nullptr)
{
// we may have all dead sessions, wtf now?
llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk, " as we have no working endpoints");
}
else
{
if(!ep->SendInboundTraffic(pkt.Buffer()))
{
llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk);
llarp::LogWarn(Name(), " dropped inbound traffic for session ", pk, " as we are overloaded (probably)");
}
}
});

View File

@ -262,9 +262,13 @@ namespace llarp
inf.read((char*)buf.base, sz);
if(!BDecode(&buf))
return false;
// update pubkey
const byte_t * ptr = nullptr;
if(!vanity.IsZero())
ptr = vanity.data();
// update pubkeys
pub.Update(llarp::seckey_topublic(enckey),
llarp::seckey_topublic(signkey));
llarp::seckey_topublic(signkey), ptr);
return true;
}