PacketMultiplexer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. 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)
  20. {
  21. PacketRecord* packet;
  22. _rxPacketVector_m.lock();
  23. if (_rxPacketVector.empty()) {
  24. packet = new PacketRecord;
  25. }
  26. else {
  27. packet = _rxPacketVector.back();
  28. _rxPacketVector.pop_back();
  29. }
  30. _rxPacketVector_m.unlock();
  31. packet->tPtr = tPtr;
  32. packet->nwid = nwid;
  33. packet->nuptr = nuptr;
  34. packet->source = source.toInt();
  35. packet->dest = dest.toInt();
  36. packet->etherType = etherType;
  37. packet->vlanId = vlanId;
  38. packet->len = len;
  39. packet->flowId = flowId;
  40. memcpy(packet->data, data, len);
  41. int bucket = flowId % _concurrency;
  42. //fprintf(stderr, "bucket=%d\n", bucket);
  43. _rxPacketQueues[bucket]->postLimit(packet, 2048);
  44. }
  45. PacketMultiplexer::PacketMultiplexer(const RuntimeEnvironment* renv)
  46. {
  47. RR = renv;
  48. bool _enablePinning = false;
  49. char* pinningVar = std::getenv("ZT_CPU_PINNING");
  50. if (pinningVar) {
  51. int tmp = atoi(pinningVar);
  52. if (tmp > 0) {
  53. _enablePinning = true;
  54. }
  55. }
  56. _concurrency = 1;
  57. char* concurrencyVar = std::getenv("ZT_PACKET_PROCESSING_CONCURRENCY");
  58. if (concurrencyVar) {
  59. int tmp = atoi(concurrencyVar);
  60. if (tmp > 0) {
  61. _concurrency = tmp;
  62. }
  63. else {
  64. _concurrency = std::max((unsigned int)1, std::thread::hardware_concurrency() / 2);
  65. }
  66. }
  67. else {
  68. _concurrency = std::max((unsigned int)1, std::thread::hardware_concurrency() / 2);
  69. }
  70. for (unsigned int i = 0; i < _concurrency; ++i) {
  71. fprintf(stderr, "reserved queue for thread %d\n", i);
  72. _rxPacketQueues.push_back(new BlockingQueue<PacketRecord*>());
  73. }
  74. // Each thread picks from its own queue to feed into the core
  75. for (unsigned int i = 0; i < _concurrency; ++i) {
  76. _rxThreads.push_back(std::thread([this, i, _enablePinning]() {
  77. fprintf(stderr, "created post-decode packet ingestion thread %d\n", i);
  78. PacketRecord* packet = nullptr;
  79. for (;;) {
  80. if (! _rxPacketQueues[i]->get(packet)) {
  81. break;
  82. }
  83. if (! packet) {
  84. break;
  85. }
  86. //fprintf(stderr, "popped packet from queue %d\n", i);
  87. MAC sourceMac = MAC(packet->source);
  88. MAC destMac = MAC(packet->dest);
  89. RR->node->putFrame(packet->tPtr, packet->nwid, packet->nuptr, sourceMac, destMac, packet->etherType, 0, (const void*)packet->data, packet->len);
  90. {
  91. Mutex::Lock l(_rxPacketVector_m);
  92. _rxPacketVector.push_back(packet);
  93. }
  94. /*
  95. if (ZT_ResultCode_isFatal(err)) {
  96. char tmp[256];
  97. OSUtils::ztsnprintf(tmp, sizeof(tmp), "error processing packet: %d", (int)err);
  98. Mutex::Lock _l(_termReason_m);
  99. _termReason = ONE_UNRECOVERABLE_ERROR;
  100. _fatalErrorMessage = tmp;
  101. this->terminate();
  102. break;
  103. }
  104. */
  105. }
  106. }));
  107. }
  108. };
  109. } // namespace ZeroTier