mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-20 05:13:58 -07:00
Cleanup, multicast fingerprint, benchmark asymmetric crypto
This commit is contained in:
parent
199b3345a0
commit
6e730cfad1
3 changed files with 111 additions and 264 deletions
|
@ -43,15 +43,11 @@ class MulticastGroup
|
|||
public:
|
||||
ZT_ALWAYS_INLINE MulticastGroup() :
|
||||
_mac(),
|
||||
_adi(0)
|
||||
{
|
||||
}
|
||||
_adi(0) {}
|
||||
|
||||
ZT_ALWAYS_INLINE MulticastGroup(const MAC &m,uint32_t a) :
|
||||
_mac(m),
|
||||
_adi(a)
|
||||
{
|
||||
}
|
||||
_adi(a) {}
|
||||
|
||||
/**
|
||||
* Derive the multicast group used for address resolution (ARP/NDP) for an IP
|
||||
|
@ -97,6 +93,42 @@ public:
|
|||
ZT_ALWAYS_INLINE bool operator<=(const MulticastGroup &g) const { return !(g < *this); }
|
||||
ZT_ALWAYS_INLINE bool operator>=(const MulticastGroup &g) const { return !(*this < g); }
|
||||
|
||||
/**
|
||||
* Compute a 32-bit fnv1a hash of a multicast group and a network ID
|
||||
*
|
||||
* @param mg Multicast group
|
||||
* @param nwid Network ID
|
||||
* @return 32-bit relatively-unique ID
|
||||
*/
|
||||
static ZT_ALWAYS_INLINE uint32_t id(const MulticastGroup &mg,const uint64_t nwid)
|
||||
{
|
||||
const uint32_t fnv1aPrime = 0x01000193;
|
||||
uint32_t i = 0x811c9dc5;
|
||||
i = (((uint32_t)(nwid >> 56) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(nwid >> 48) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(nwid >> 40) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(nwid >> 32) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(nwid >> 24) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(nwid >> 16) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(nwid >> 8) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)nwid & 0xff) ^ i) * fnv1aPrime;
|
||||
const uint64_t mac = mg._mac.toInt();
|
||||
i = (((uint32_t)(mac >> 56) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(mac >> 48) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(mac >> 40) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(mac >> 32) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(mac >> 24) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(mac >> 16) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)(mac >> 8) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((uint32_t)mac & 0xff) ^ i) * fnv1aPrime;
|
||||
const uint32_t adi = mg._adi;
|
||||
i = (((adi >> 24) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((adi >> 16) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = (((adi >> 8) & 0xff) ^ i) * fnv1aPrime;
|
||||
i = ((adi & 0xff) ^ i) * fnv1aPrime;
|
||||
return i;
|
||||
}
|
||||
|
||||
private:
|
||||
MAC _mac;
|
||||
uint32_t _adi;
|
||||
|
|
109
node/Mutex.hpp
109
node/Mutex.hpp
|
@ -26,15 +26,11 @@ namespace ZeroTier {
|
|||
|
||||
#if defined(__GNUC__) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
|
||||
|
||||
// Inline ticket lock on x64 systems with GCC and CLANG (Mac, Linux) -- this is really fast as long as locking durations are very short
|
||||
// Inline ticket lock on x64 systems with GCC and CLANG (Mac, Linux) -- this is really fast as long as contention is LOW
|
||||
class Mutex
|
||||
{
|
||||
public:
|
||||
ZT_ALWAYS_INLINE Mutex() :
|
||||
nextTicket(0),
|
||||
nowServing(0)
|
||||
{
|
||||
}
|
||||
ZT_ALWAYS_INLINE Mutex() : nextTicket(0),nowServing(0) {}
|
||||
|
||||
ZT_ALWAYS_INLINE void lock() const
|
||||
{
|
||||
|
@ -47,9 +43,6 @@ public:
|
|||
|
||||
ZT_ALWAYS_INLINE void unlock() const { ++(const_cast<Mutex *>(this)->nowServing); }
|
||||
|
||||
/**
|
||||
* Uses C++ contexts and constructor/destructor to lock/unlock automatically
|
||||
*/
|
||||
class Lock
|
||||
{
|
||||
public:
|
||||
|
@ -74,46 +67,17 @@ private:
|
|||
class Mutex
|
||||
{
|
||||
public:
|
||||
ZT_ALWAYS_INLINE Mutex()
|
||||
{
|
||||
pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
|
||||
}
|
||||
|
||||
ZT_ALWAYS_INLINE ~Mutex()
|
||||
{
|
||||
pthread_mutex_destroy(&_mh);
|
||||
}
|
||||
|
||||
ZT_ALWAYS_INLINE void lock() const
|
||||
{
|
||||
pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh));
|
||||
}
|
||||
|
||||
ZT_ALWAYS_INLINE void unlock() const
|
||||
{
|
||||
pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh));
|
||||
}
|
||||
ZT_ALWAYS_INLINE Mutex() { pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0); }
|
||||
ZT_ALWAYS_INLINE ~Mutex() { pthread_mutex_destroy(&_mh); }
|
||||
ZT_ALWAYS_INLINE void lock() const { pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh)); }
|
||||
ZT_ALWAYS_INLINE void unlock() const { pthread_mutex_unlock(&((const_cast <Mutex *> (this))->_mh)); }
|
||||
|
||||
class Lock
|
||||
{
|
||||
public:
|
||||
ZT_ALWAYS_INLINE Lock(Mutex &m) :
|
||||
_m(&m)
|
||||
{
|
||||
m.lock();
|
||||
}
|
||||
|
||||
ZT_ALWAYS_INLINE Lock(const Mutex &m) :
|
||||
_m(const_cast<Mutex *>(&m))
|
||||
{
|
||||
_m->lock();
|
||||
}
|
||||
|
||||
ZT_ALWAYS_INLINE ~Lock()
|
||||
{
|
||||
_m->unlock();
|
||||
}
|
||||
|
||||
ZT_ALWAYS_INLINE Lock(Mutex &m) : _m(&m) { m.lock(); }
|
||||
ZT_ALWAYS_INLINE Lock(const Mutex &m) : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
|
||||
ZT_ALWAYS_INLINE ~Lock() { _m->unlock(); }
|
||||
private:
|
||||
Mutex *const _m;
|
||||
};
|
||||
|
@ -142,56 +106,19 @@ namespace ZeroTier {
|
|||
class Mutex
|
||||
{
|
||||
public:
|
||||
inline Mutex()
|
||||
{
|
||||
InitializeCriticalSection(&_cs);
|
||||
}
|
||||
|
||||
inline ~Mutex()
|
||||
{
|
||||
DeleteCriticalSection(&_cs);
|
||||
}
|
||||
|
||||
inline void lock()
|
||||
{
|
||||
EnterCriticalSection(&_cs);
|
||||
}
|
||||
|
||||
inline void unlock()
|
||||
{
|
||||
LeaveCriticalSection(&_cs);
|
||||
}
|
||||
|
||||
inline void lock() const
|
||||
{
|
||||
(const_cast <Mutex *> (this))->lock();
|
||||
}
|
||||
|
||||
inline void unlock() const
|
||||
{
|
||||
(const_cast <Mutex *> (this))->unlock();
|
||||
}
|
||||
ZT_ALWAYS_INLINE Mutex() { InitializeCriticalSection(&_cs); }
|
||||
ZT_ALWAYS_INLINE ~Mutex() { DeleteCriticalSection(&_cs); }
|
||||
ZT_ALWAYS_INLINE void lock() { EnterCriticalSection(&_cs); }
|
||||
ZT_ALWAYS_INLINE void unlock() { LeaveCriticalSection(&_cs); }
|
||||
ZT_ALWAYS_INLINE void lock() const { (const_cast <Mutex *> (this))->lock(); }
|
||||
ZT_ALWAYS_INLINE void unlock() const { (const_cast <Mutex *> (this))->unlock(); }
|
||||
|
||||
class Lock
|
||||
{
|
||||
public:
|
||||
inline Lock(Mutex &m) :
|
||||
_m(&m)
|
||||
{
|
||||
m.lock();
|
||||
}
|
||||
|
||||
inline Lock(const Mutex &m) :
|
||||
_m(const_cast<Mutex *>(&m))
|
||||
{
|
||||
_m->lock();
|
||||
}
|
||||
|
||||
inline ~Lock()
|
||||
{
|
||||
_m->unlock();
|
||||
}
|
||||
|
||||
ZT_ALWAYS_INLINE Lock(Mutex &m) : _m(&m) { m.lock(); }
|
||||
ZT_ALWAYS_INLINE Lock(const Mutex &m) : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
|
||||
ZT_ALWAYS_INLINE ~Lock() { _m->unlock(); }
|
||||
private:
|
||||
Mutex *const _m;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue