From 8672ca9cf8b7f065c8e9faa134e5af0683e9f416 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 24 Oct 2014 17:29:09 -0700 Subject: [PATCH] Prep for real tests like alltoall. --- testnet/TestEthernetTap.cpp | 14 +++----------- testnet/TestEthernetTap.hpp | 18 ++++++++++++++++-- testnet/TestEthernetTapFactory.cpp | 8 ++++++++ testnet/TestEthernetTapFactory.hpp | 14 +++++++++++++- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/testnet/TestEthernetTap.cpp b/testnet/TestEthernetTap.cpp index 35c7511b4..fd594a21f 100644 --- a/testnet/TestEthernetTap.cpp +++ b/testnet/TestEthernetTap.cpp @@ -42,8 +42,6 @@ namespace ZeroTier { -static Mutex printLock; - TestEthernetTap::TestEthernetTap( TestEthernetTapFactory *parent, const MAC &mac, @@ -55,6 +53,7 @@ TestEthernetTap::TestEthernetTap( void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &), void *arg) : EthernetTap("TestEthernetTap",mac,mtu,metric), + _nwid(nwid), _parent(parent), _handler(handler), _arg(arg), @@ -114,9 +113,8 @@ std::set TestEthernetTap::ips() const void TestEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) { - Mutex::Lock _l(printLock); - fprintf(stdout,"[%s] %s << %s %.4x %s"ZT_EOL_S,_dev.c_str(),to.toString().c_str(),from.toString().c_str(),etherType,std::string((const char *)data,len).c_str()); - fflush(stdout); + Mutex::Lock _l(_gq_m); + _gq.push_back(TestFrame(from,to,data,etherType,len)); } std::string TestEthernetTap::deviceName() const @@ -144,12 +142,6 @@ bool TestEthernetTap::injectPacketFromHost(const MAC &from,const MAC &to,unsigne } _pq_c.signal(); - { - Mutex::Lock _l(printLock); - fprintf(stdout,"[%s] %s >> %s %.4x %s"ZT_EOL_S,_dev.c_str(),from.toString().c_str(),to.toString().c_str(),etherType,std::string((const char *)data,len).c_str()); - fflush(stdout); - } - return true; } diff --git a/testnet/TestEthernetTap.hpp b/testnet/TestEthernetTap.hpp index 66e219227..e7eae7024 100644 --- a/testnet/TestEthernetTap.hpp +++ b/testnet/TestEthernetTap.hpp @@ -60,7 +60,7 @@ class TestEthernetTap : public EthernetTap { friend class SharedPtr; -private: +public: struct TestFrame { TestFrame() : from(),to(),etherType(0),len(0) {} @@ -79,7 +79,6 @@ private: char data[4096]; }; -public: TestEthernetTap( TestEthernetTapFactory *parent, const MAC &mac, @@ -104,10 +103,22 @@ public: virtual bool updateMulticastGroups(std::set &groups); virtual bool injectPacketFromHost(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len); + inline uint64_t nwid() const { return _nwid; } + + // Get things that have been put() and empty queue + inline void get(std::vector &v,bool clearQueue = true) + { + Mutex::Lock _l(_gq_m); + v = _gq; + if (clearQueue) + _gq.clear(); + } + void threadMain() throw(); private: + uint64_t _nwid; TestEthernetTapFactory *_parent; void (*_handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &); @@ -120,6 +131,9 @@ private: Mutex _pq_m; Condition _pq_c; + std::vector< TestFrame > _gq; + Mutex _gq_m; + AtomicCounter __refCount; }; diff --git a/testnet/TestEthernetTapFactory.cpp b/testnet/TestEthernetTapFactory.cpp index 836f586e3..1e72bc4ef 100644 --- a/testnet/TestEthernetTapFactory.cpp +++ b/testnet/TestEthernetTapFactory.cpp @@ -57,6 +57,10 @@ EthernetTap *TestEthernetTapFactory::open( Mutex::Lock _l(_tapsByMac_m); _tapsByMac[mac] = tap; } + { + Mutex::Lock _l(_tapsByNwid_m); + _tapsByNwid[nwid] = tap; + } return tap.ptr(); } @@ -73,6 +77,10 @@ void TestEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevice Mutex::Lock _l(_tapsByMac_m); _tapsByMac.erase(tapp->mac()); } + { + Mutex::Lock _l(_tapsByNwid_m); + _tapsByNwid.erase(tapp->nwid()); + } } } // namespace ZeroTier diff --git a/testnet/TestEthernetTapFactory.hpp b/testnet/TestEthernetTapFactory.hpp index 57d195d27..bc5742af4 100644 --- a/testnet/TestEthernetTapFactory.hpp +++ b/testnet/TestEthernetTapFactory.hpp @@ -68,13 +68,25 @@ public: return t->second; } + inline SharedPtr getByNwid(uint64_t nwid) const + { + Mutex::Lock _l(_tapsByNwid_m); + std::map< uint64_t,SharedPtr >::const_iterator t(_tapsByNwid.find(nwid)); + if (t == _tapsByNwid.end()) + return SharedPtr(); + return t->second; + } + private: std::set< SharedPtr > _taps; Mutex _taps_m; - std::map > _tapsByMac; + std::map< MAC,SharedPtr > _tapsByMac; Mutex _tapsByMac_m; + std::map< uint64_t,SharedPtr > _tapsByNwid; + Mutex _tapsByNwid_m; + CMWC4096 _prng; Mutex _prng_m; };