From e236d6f743f1015a6ad224e9976ea8b338cc41eb Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 14 Feb 2020 10:19:45 -0800 Subject: [PATCH] More minor refactoring --- node/AES.cpp | 8 ------- node/AES.hpp | 5 +++- node/Buf.cpp | 6 ++--- node/Buf.hpp | 4 ++-- node/Capability.cpp | 10 ++++---- node/Capability.hpp | 57 ++++++++++++++++++++++++++++++--------------- node/VL1.cpp | 13 ++--------- node/VL2.cpp | 20 ++++++++-------- node/VL2.hpp | 2 +- 9 files changed, 65 insertions(+), 60 deletions(-) diff --git a/node/AES.cpp b/node/AES.cpp index 83c1d1676..511cd246a 100644 --- a/node/AES.cpp +++ b/node/AES.cpp @@ -447,10 +447,6 @@ void AES::CTR::crypt(const void *const input,unsigned int len) noexcept #ifdef ZT_AES_AESNI if (likely(Utils::CPUID.aes)) { - _mm_prefetch(in,_MM_HINT_T0); - _mm_prefetch(in + 64,_MM_HINT_T0); - _mm_prefetch(in + 128,_MM_HINT_T0); - uint64_t c0 = _ctr[0]; uint64_t c1 = Utils::ntoh(_ctr[1]); @@ -496,10 +492,6 @@ void AES::CTR::crypt(const void *const input,unsigned int len) noexcept // This is the largest chunk size that will fit in SSE registers with four // registers left over for round key data and temporaries. while (len >= 192) { - _mm_prefetch(in + 192,_MM_HINT_T0); - _mm_prefetch(in + 256,_MM_HINT_T0); - _mm_prefetch(in + 320,_MM_HINT_T0); - __m128i d0,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11; if (likely(c1 < 0xfffffffffffffff4ULL)) { d0 = _mm_set_epi64x((long long)Utils::hton(c1),(long long)c0); diff --git a/node/AES.hpp b/node/AES.hpp index c87d69b27..6becbbcb4 100644 --- a/node/AES.hpp +++ b/node/AES.hpp @@ -21,6 +21,7 @@ #include #include +#ifndef ZT_AES_NO_ACCEL #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64)) #include #include @@ -28,6 +29,7 @@ #include #define ZT_AES_AESNI 1 #endif +#endif namespace ZeroTier { @@ -197,7 +199,8 @@ public: */ ZT_ALWAYS_INLINE void init(const uint8_t iv[16],void *const output) noexcept { - memcpy(_ctr,iv,16); + _ctr[0] = Utils::loadAsIsEndian(iv); + _ctr[1] = Utils::loadAsIsEndian(iv + 8); _out = reinterpret_cast(output); _len = 0; } diff --git a/node/Buf.cpp b/node/Buf.cpp index 05e46c343..bff6d1f14 100644 --- a/node/Buf.cpp +++ b/node/Buf.cpp @@ -17,7 +17,7 @@ namespace ZeroTier { static std::atomic s_pool(0); -void *Buf::operator new(std::size_t sz) noexcept +void *Buf::operator new(std::size_t sz) { uintptr_t bb; for (;;) { @@ -34,14 +34,14 @@ void *Buf::operator new(std::size_t sz) noexcept s_pool.store(0); b = (Buf *)malloc(sz); if (!b) - return nullptr; + throw std::bad_alloc(); } b->__refCount.store(0); return (void *)b; } -void Buf::operator delete(void *ptr) noexcept +void Buf::operator delete(void *ptr) { if (ptr) { uintptr_t bb; diff --git a/node/Buf.hpp b/node/Buf.hpp index e56a29492..b14ebaba2 100644 --- a/node/Buf.hpp +++ b/node/Buf.hpp @@ -83,8 +83,8 @@ class Buf public: // New and delete operators that allocate Buf instances from a shared lock-free memory pool. - static void *operator new(std::size_t sz) noexcept; - static void operator delete(void *ptr) noexcept; + static void *operator new(std::size_t sz); + static void operator delete(void *ptr); /** * Free all instances of Buf in shared pool. diff --git a/node/Capability.cpp b/node/Capability.cpp index e31fed949..f12abcee7 100644 --- a/node/Capability.cpp +++ b/node/Capability.cpp @@ -18,7 +18,7 @@ namespace ZeroTier { -bool Capability::sign(const Identity &from,const Address &to) +bool Capability::sign(const Identity &from,const Address &to) noexcept { uint8_t buf[ZT_CAPABILITY_MARSHAL_SIZE_MAX + 16]; try { @@ -34,7 +34,7 @@ bool Capability::sign(const Identity &from,const Address &to) return false; } -int Capability::marshal(uint8_t data[ZT_CAPABILITY_MARSHAL_SIZE_MAX],const bool forSign) const +int Capability::marshal(uint8_t data[ZT_CAPABILITY_MARSHAL_SIZE_MAX],const bool forSign) const noexcept { int p = 0; if (forSign) { @@ -72,7 +72,7 @@ int Capability::marshal(uint8_t data[ZT_CAPABILITY_MARSHAL_SIZE_MAX],const bool return p; } -int Capability::unmarshal(const uint8_t *data,int len) +int Capability::unmarshal(const uint8_t *data,int len) noexcept { if (len < 22) return -1; @@ -122,7 +122,7 @@ int Capability::unmarshal(const uint8_t *data,int len) return p; } -int Capability::marshalVirtualNetworkRules(uint8_t *data,const ZT_VirtualNetworkRule *const rules,const unsigned int ruleCount) +int Capability::marshalVirtualNetworkRules(uint8_t *data,const ZT_VirtualNetworkRule *const rules,const unsigned int ruleCount) noexcept { int p = 0; for(unsigned int i=0;it->unexpectedError(tPtr,0x1de16991,"Buf::get() failed (out of memory?)"); - return; - } + pkt.b.set(new Buf()); // Salsa20 is a stream cipher but it's only seekable to multiples of 64 bytes. // This moves data in slices around so that all slices have sizes that are @@ -351,12 +347,7 @@ void VL1::onRemotePacket(void *const tPtr,const int64_t localSocket,const InetAd return; } - SharedPtr nb(Buf::get()); - if (!nb) { - RR->t->unexpectedError(tPtr,0xffe169fa,"Buf::get() failed (out of memory?)"); - return; - } - + SharedPtr nb(new Buf()); const int uncompressedLen = LZ4_decompress_safe( reinterpret_cast(pkt.b->b + ZT_PROTO_PACKET_PAYLOAD_START), reinterpret_cast(nb->b), diff --git a/node/VL2.cpp b/node/VL2.cpp index 48e55abef..63e8a4c0c 100644 --- a/node/VL2.cpp +++ b/node/VL2.cpp @@ -31,43 +31,43 @@ VL2::~VL2() { } -bool VL2::onLocalEthernet(void *tPtr,const SharedPtr &network,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len) +void VL2::onLocalEthernet(void *const tPtr,const SharedPtr &network,const MAC &from,const MAC &to,const unsigned int etherType,unsigned int vlanId,SharedPtr &data,unsigned int len) { } -bool VL2::_FRAME(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) +bool VL2::_FRAME(void *const tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) { } -bool VL2::_EXT_FRAME(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) +bool VL2::_EXT_FRAME(void *const tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) { } -bool VL2::_MULTICAST_LIKE(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) +bool VL2::_MULTICAST_LIKE(void *const tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) { } -bool VL2::_NETWORK_CREDENTIALS(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) +bool VL2::_NETWORK_CREDENTIALS(void *const tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) { } -bool VL2::_NETWORK_CONFIG_REQUEST(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) +bool VL2::_NETWORK_CONFIG_REQUEST(void *const tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) { } -bool VL2::_NETWORK_CONFIG(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) +bool VL2::_NETWORK_CONFIG(void *const tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) { } -bool VL2::_MULTICAST_GATHER(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) +bool VL2::_MULTICAST_GATHER(void *const tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) { } -bool VL2::_MULTICAST_FRAME_deprecated(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) +bool VL2::_MULTICAST_FRAME_deprecated(void *const tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) { } -bool VL2::_MULTICAST(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) +bool VL2::_MULTICAST(void *const tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize) { } diff --git a/node/VL2.hpp b/node/VL2.hpp index 8b8b6e073..f165601e3 100644 --- a/node/VL2.hpp +++ b/node/VL2.hpp @@ -51,7 +51,7 @@ public: * @param data Ethernet payload * @param len Frame length */ - void onLocalEthernet(void *tPtr,const SharedPtr &network,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len); + void onLocalEthernet(void *tPtr,const SharedPtr &network,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,SharedPtr &data,unsigned int len); protected: bool _FRAME(void *tPtr,const SharedPtr &path,SharedPtr &peer,Buf &pkt,int packetSize);