PacketMultiplexer.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c)2013-2021 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "PacketMultiplexer.hpp"
  14. #include "Node.hpp"
  15. #include "RuntimeEnvironment.hpp"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. namespace ZeroTier {
  19. PacketMultiplexer::PacketMultiplexer(const RuntimeEnvironment* renv)
  20. {
  21. RR = renv;
  22. };
  23. void PacketMultiplexer::putFrame(void* tPtr, uint64_t nwid, void** nuptr, const MAC& source, const MAC& dest, unsigned int etherType, unsigned int vlanId, const void* data, unsigned int len, unsigned int flowId)
  24. {
  25. PacketRecord* packet;
  26. _rxPacketVector_m.lock();
  27. if (_rxPacketVector.empty()) {
  28. packet = new PacketRecord;
  29. }
  30. else {
  31. packet = _rxPacketVector.back();
  32. _rxPacketVector.pop_back();
  33. }
  34. _rxPacketVector_m.unlock();
  35. packet->tPtr = tPtr;
  36. packet->nwid = nwid;
  37. packet->nuptr = nuptr;
  38. packet->source = source.toInt();
  39. packet->dest = dest.toInt();
  40. packet->etherType = etherType;
  41. packet->vlanId = vlanId;
  42. packet->len = len;
  43. packet->flowId = flowId;
  44. memcpy(packet->data, data, len);
  45. int bucket = flowId % _concurrency;
  46. _rxPacketQueues[bucket]->postLimit(packet, 256);
  47. }
  48. void PacketMultiplexer::setUpPostDecodeReceiveThreads(unsigned int concurrency, bool cpuPinningEnabled)
  49. {
  50. if (! RR->node->getMultithreadingEnabled()) {
  51. return;
  52. }
  53. _concurrency = concurrency;
  54. bool _enablePinning = cpuPinningEnabled;
  55. for (unsigned int i = 0; i < _concurrency; ++i) {
  56. fprintf(stderr, "Reserved queue for thread %d\n", i);
  57. _rxPacketQueues.push_back(new BlockingQueue<PacketRecord*>());
  58. }
  59. // Each thread picks from its own queue to feed into the core
  60. for (unsigned int i = 0; i < _concurrency; ++i) {
  61. _rxThreads.push_back(std::thread([this, i, _enablePinning]() {
  62. fprintf(stderr, "Created post-decode packet ingestion thread %d\n", i);
  63. PacketRecord* packet = nullptr;
  64. for (;;) {
  65. if (! _rxPacketQueues[i]->get(packet)) {
  66. break;
  67. }
  68. if (! packet) {
  69. break;
  70. }
  71. // fprintf(stderr, "popped packet from queue %d\n", i);
  72. MAC sourceMac = MAC(packet->source);
  73. MAC destMac = MAC(packet->dest);
  74. RR->node->putFrame(packet->tPtr, packet->nwid, packet->nuptr, sourceMac, destMac, packet->etherType, 0, (const void*)packet->data, packet->len);
  75. {
  76. Mutex::Lock l(_rxPacketVector_m);
  77. _rxPacketVector.push_back(packet);
  78. }
  79. /*
  80. if (ZT_ResultCode_isFatal(err)) {
  81. char tmp[256];
  82. OSUtils::ztsnprintf(tmp, sizeof(tmp), "error processing packet: %d", (int)err);
  83. Mutex::Lock _l(_termReason_m);
  84. _termReason = ONE_UNRECOVERABLE_ERROR;
  85. _fatalErrorMessage = tmp;
  86. this->terminate();
  87. break;
  88. }
  89. */
  90. }
  91. }));
  92. }
  93. }
  94. } // namespace ZeroTier