Switch.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_N_SWITCH_HPP
  27. #define ZT_N_SWITCH_HPP
  28. #include <map>
  29. #include <set>
  30. #include <vector>
  31. #include <list>
  32. #include "Constants.hpp"
  33. #include "Mutex.hpp"
  34. #include "MAC.hpp"
  35. #include "Packet.hpp"
  36. #include "Utils.hpp"
  37. #include "InetAddress.hpp"
  38. #include "Topology.hpp"
  39. #include "Network.hpp"
  40. #include "SharedPtr.hpp"
  41. #include "IncomingPacket.hpp"
  42. #include "Hashtable.hpp"
  43. /* Ethernet frame types that might be relevant to us */
  44. #define ZT_ETHERTYPE_IPV4 0x0800
  45. #define ZT_ETHERTYPE_ARP 0x0806
  46. #define ZT_ETHERTYPE_RARP 0x8035
  47. #define ZT_ETHERTYPE_ATALK 0x809b
  48. #define ZT_ETHERTYPE_AARP 0x80f3
  49. #define ZT_ETHERTYPE_IPX_A 0x8137
  50. #define ZT_ETHERTYPE_IPX_B 0x8138
  51. #define ZT_ETHERTYPE_IPV6 0x86dd
  52. namespace ZeroTier {
  53. class RuntimeEnvironment;
  54. class Peer;
  55. /**
  56. * Core of the distributed Ethernet switch and protocol implementation
  57. *
  58. * This class is perhaps a bit misnamed, but it's basically where everything
  59. * meets. Transport-layer ZT packets come in here, as do virtual network
  60. * packets from tap devices, and this sends them where they need to go and
  61. * wraps/unwraps accordingly. It also handles queues and timeouts and such.
  62. */
  63. class Switch
  64. {
  65. struct ManagedQueue;
  66. struct TXQueueEntry;
  67. typedef struct {
  68. TXQueueEntry *p;
  69. bool ok_to_drop;
  70. } dqr;
  71. public:
  72. Switch(const RuntimeEnvironment *renv);
  73. /**
  74. * Called when a packet is received from the real network
  75. *
  76. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  77. * @param localSocket Local I/O socket as supplied by external code
  78. * @param fromAddr Internet IP address of origin
  79. * @param data Packet data
  80. * @param len Packet length
  81. */
  82. void onRemotePacket(void *tPtr,const int64_t localSocket,const InetAddress &fromAddr,const void *data,unsigned int len);
  83. /**
  84. * Returns whether our bonding or balancing policy is aware of flows.
  85. */
  86. bool isFlowAware();
  87. /**
  88. * Called when a packet comes from a local Ethernet tap
  89. *
  90. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  91. * @param network Which network's TAP did this packet come from?
  92. * @param from Originating MAC address
  93. * @param to Destination MAC address
  94. * @param etherType Ethernet packet type
  95. * @param vlanId VLAN ID or 0 if none
  96. * @param data Ethernet payload
  97. * @param len Frame length
  98. */
  99. void onLocalEthernet(void *tPtr,const SharedPtr<Network> &network,const MAC &from,const MAC &to,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len);
  100. /**
  101. * Determines the next drop schedule for packets in the TX queue
  102. *
  103. * @param t Current time
  104. * @param count Number of packets dropped this round
  105. */
  106. uint64_t control_law(uint64_t t, int count);
  107. /**
  108. * Selects a packet eligible for transmission from a TX queue. According to the control law, multiple packets
  109. * may be intentionally dropped before a packet is returned to the AQM scheduler.
  110. *
  111. * @param q The TX queue that is being dequeued from
  112. * @param now Current time
  113. */
  114. dqr dodequeue(ManagedQueue *q, uint64_t now);
  115. /**
  116. * Presents a packet to the AQM scheduler.
  117. *
  118. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  119. * @param network Network that the packet shall be sent over
  120. * @param packet Packet to be sent
  121. * @param encrypt Encrypt packet payload? (always true except for HELLO)
  122. * @param qosBucket Which bucket the rule-system determined this packet should fall into
  123. */
  124. void aqm_enqueue(void *tPtr, const SharedPtr<Network> &network, Packet &packet,bool encrypt,int qosBucket,int64_t flowId = -1);
  125. /**
  126. * Performs a single AQM cycle and dequeues and transmits all eligible packets on all networks
  127. *
  128. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  129. */
  130. void aqm_dequeue(void *tPtr);
  131. /**
  132. * Calls the dequeue mechanism and adjust queue state variables
  133. *
  134. * @param q The TX queue that is being dequeued from
  135. * @param isNew Whether or not this queue is in the NEW list
  136. * @param now Current time
  137. */
  138. Switch::TXQueueEntry * CoDelDequeue(ManagedQueue *q, bool isNew, uint64_t now);
  139. /**
  140. * Removes QoS Queues and flow state variables for a specific network. These queues are created
  141. * automatically upon the transmission of the first packet from this peer to another peer on the
  142. * given network.
  143. *
  144. * The reason for existence of queues and flow state variables specific to each network is so that
  145. * each network's QoS rules function independently.
  146. *
  147. * @param nwid Network ID
  148. */
  149. void removeNetworkQoSControlBlock(uint64_t nwid);
  150. /**
  151. * Send a packet to a ZeroTier address (destination in packet)
  152. *
  153. * The packet must be fully composed with source and destination but not
  154. * yet encrypted. If the destination peer is known the packet
  155. * is sent immediately. Otherwise it is queued and a WHOIS is dispatched.
  156. *
  157. * The packet may be compressed. Compression isn't done here.
  158. *
  159. * Needless to say, the packet's source must be this node. Otherwise it
  160. * won't be encrypted right. (This is not used for relaying.)
  161. *
  162. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  163. * @param packet Packet to send (buffer may be modified)
  164. * @param encrypt Encrypt packet payload? (always true except for HELLO)
  165. */
  166. void send(void *tPtr,Packet &packet,bool encrypt,int64_t flowId = -1);
  167. /**
  168. * Request WHOIS on a given address
  169. *
  170. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  171. * @param now Current time
  172. * @param addr Address to look up
  173. */
  174. void requestWhois(void *tPtr,const int64_t now,const Address &addr);
  175. /**
  176. * Run any processes that are waiting for this peer's identity
  177. *
  178. * Called when we learn of a peer's identity from HELLO, OK(WHOIS), etc.
  179. *
  180. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  181. * @param peer New peer
  182. */
  183. void doAnythingWaitingForPeer(void *tPtr,const SharedPtr<Peer> &peer);
  184. /**
  185. * Perform retries and other periodic timer tasks
  186. *
  187. * This can return a very long delay if there are no pending timer
  188. * tasks. The caller should cap this comparatively vs. other values.
  189. *
  190. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  191. * @param now Current time
  192. * @return Number of milliseconds until doTimerTasks() should be run again
  193. */
  194. unsigned long doTimerTasks(void *tPtr,int64_t now);
  195. private:
  196. bool _shouldUnite(const int64_t now,const Address &source,const Address &destination);
  197. bool _trySend(void *tPtr,Packet &packet,bool encrypt,int64_t flowId = -1); // packet is modified if return is true
  198. const RuntimeEnvironment *const RR;
  199. int64_t _lastBeaconResponse;
  200. volatile int64_t _lastCheckedQueues;
  201. // Time we last sent a WHOIS request for each address
  202. Hashtable< Address,int64_t > _lastSentWhoisRequest;
  203. Mutex _lastSentWhoisRequest_m;
  204. // Packets waiting for WHOIS replies or other decode info or missing fragments
  205. struct RXQueueEntry
  206. {
  207. RXQueueEntry() : timestamp(0) {}
  208. volatile int64_t timestamp; // 0 if entry is not in use
  209. volatile uint64_t packetId;
  210. IncomingPacket frag0; // head of packet
  211. Packet::Fragment frags[ZT_MAX_PACKET_FRAGMENTS - 1]; // later fragments (if any)
  212. unsigned int totalFragments; // 0 if only frag0 received, waiting for frags
  213. uint32_t haveFragments; // bit mask, LSB to MSB
  214. volatile bool complete; // if true, packet is complete
  215. Mutex lock;
  216. };
  217. RXQueueEntry _rxQueue[ZT_RX_QUEUE_SIZE];
  218. AtomicCounter _rxQueuePtr;
  219. // Returns matching or next available RX queue entry
  220. inline RXQueueEntry *_findRXQueueEntry(uint64_t packetId)
  221. {
  222. const unsigned int current = static_cast<unsigned int>(_rxQueuePtr.load());
  223. for(unsigned int k=1;k<=ZT_RX_QUEUE_SIZE;++k) {
  224. RXQueueEntry *rq = &(_rxQueue[(current - k) % ZT_RX_QUEUE_SIZE]);
  225. if ((rq->packetId == packetId)&&(rq->timestamp))
  226. return rq;
  227. }
  228. ++_rxQueuePtr;
  229. return &(_rxQueue[static_cast<unsigned int>(current) % ZT_RX_QUEUE_SIZE]);
  230. }
  231. // Returns current entry in rx queue ring buffer and increments ring pointer
  232. inline RXQueueEntry *_nextRXQueueEntry()
  233. {
  234. return &(_rxQueue[static_cast<unsigned int>((++_rxQueuePtr) - 1) % ZT_RX_QUEUE_SIZE]);
  235. }
  236. // ZeroTier-layer TX queue entry
  237. struct TXQueueEntry
  238. {
  239. TXQueueEntry() {}
  240. TXQueueEntry(Address d,uint64_t ct,const Packet &p,bool enc,int64_t fid) :
  241. dest(d),
  242. creationTime(ct),
  243. packet(p),
  244. encrypt(enc),
  245. flowId(fid) {}
  246. Address dest;
  247. uint64_t creationTime;
  248. Packet packet; // unencrypted/unMAC'd packet -- this is done at send time
  249. bool encrypt;
  250. int64_t flowId;
  251. };
  252. std::list< TXQueueEntry > _txQueue;
  253. Mutex _txQueue_m;
  254. Mutex _aqm_m;
  255. // Tracks sending of VERB_RENDEZVOUS to relaying peers
  256. struct _LastUniteKey
  257. {
  258. _LastUniteKey() : x(0),y(0) {}
  259. _LastUniteKey(const Address &a1,const Address &a2)
  260. {
  261. if (a1 > a2) {
  262. x = a2.toInt();
  263. y = a1.toInt();
  264. } else {
  265. x = a1.toInt();
  266. y = a2.toInt();
  267. }
  268. }
  269. inline unsigned long hashCode() const { return ((unsigned long)x ^ (unsigned long)y); }
  270. inline bool operator==(const _LastUniteKey &k) const { return ((x == k.x)&&(y == k.y)); }
  271. uint64_t x,y;
  272. };
  273. Hashtable< _LastUniteKey,uint64_t > _lastUniteAttempt; // key is always sorted in ascending order, for set-like behavior
  274. Mutex _lastUniteAttempt_m;
  275. // Queue with additional flow state variables
  276. struct ManagedQueue
  277. {
  278. ManagedQueue(int id) :
  279. id(id),
  280. byteCredit(ZT_QOS_QUANTUM),
  281. byteLength(0),
  282. dropping(false)
  283. {}
  284. int id;
  285. int byteCredit;
  286. int byteLength;
  287. uint64_t first_above_time;
  288. uint32_t count;
  289. uint64_t drop_next;
  290. bool dropping;
  291. uint64_t drop_next_time;
  292. std::list< TXQueueEntry *> q;
  293. };
  294. // To implement fq_codel we need to maintain a queue of queues
  295. struct NetworkQoSControlBlock
  296. {
  297. int _currEnqueuedPackets;
  298. std::vector<ManagedQueue *> newQueues;
  299. std::vector<ManagedQueue *> oldQueues;
  300. std::vector<ManagedQueue *> inactiveQueues;
  301. };
  302. std::map<uint64_t,NetworkQoSControlBlock*> _netQueueControlBlock;
  303. };
  304. } // namespace ZeroTier
  305. #endif