SwitchCore.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "util/log/Log.h"
  17. #include "switch/SwitchCore.h"
  18. // TODO(cjd): Get rid of NumberCompress so we can set encodingScheme at runtime.
  19. #define NumberCompress_OLD_CODE
  20. #include "switch/NumberCompress.h"
  21. #include "util/Bits.h"
  22. #include "util/Checksum.h"
  23. #include "util/Endian.h"
  24. #include "wire/Control.h"
  25. #include "wire/Error.h"
  26. #include "wire/SwitchHeader.h"
  27. #include "wire/Message.h"
  28. #include <inttypes.h>
  29. #include <stdbool.h>
  30. struct SwitchInterface
  31. {
  32. struct Iface iface;
  33. struct Allocator* alloc;
  34. struct SwitchCore_pvt* core;
  35. struct Allocator_OnFreeJob* onFree;
  36. Identity
  37. };
  38. struct SwitchCore_pvt
  39. {
  40. struct SwitchCore pub;
  41. struct SwitchInterface interfaces[NumberCompress_INTERFACES];
  42. bool routerAdded;
  43. struct Log* logger;
  44. EventBase_t* eventBase;
  45. struct Allocator* allocator;
  46. Identity
  47. };
  48. struct ErrorPacket8 {
  49. struct SwitchHeader switchHeader;
  50. uint32_t handle;
  51. struct Control ctrl;
  52. };
  53. Assert_compileTime(sizeof(struct ErrorPacket8) == SwitchHeader_SIZE + 4 + sizeof(struct Control));
  54. static inline Iface_DEFUN sendError(struct SwitchInterface* iface,
  55. Message_t* cause,
  56. uint32_t code,
  57. struct Log* logger)
  58. {
  59. if (Message_getLength(cause) < SwitchHeader_SIZE + 4) {
  60. Log_debug(logger, "runt");
  61. return Error(cause, "RUNT");
  62. }
  63. struct SwitchHeader* causeHeader = (struct SwitchHeader*) Message_bytes(cause);
  64. if (SwitchHeader_getSuppressErrors(causeHeader)) {
  65. // don't send errors if they're asking us to suppress them!
  66. return NULL;
  67. }
  68. // limit of 256 bytes
  69. if (Message_getLength(cause) > Control_Error_MAX_SIZE) {
  70. Err(Message_truncate(cause, Control_Error_MAX_SIZE));
  71. }
  72. // Shift back so we can add another header.
  73. Err(Message_epush(cause,
  74. NULL,
  75. SwitchHeader_SIZE + 4 + Control_Header_SIZE + Control_Error_HEADER_SIZE));
  76. struct ErrorPacket8* err = (struct ErrorPacket8*) Message_bytes(cause);
  77. err->switchHeader.label_be = Bits_bitReverse64(causeHeader->label_be);
  78. SwitchHeader_setSuppressErrors(&err->switchHeader, true);
  79. SwitchHeader_setVersion(&err->switchHeader, SwitchHeader_CURRENT_VERSION);
  80. SwitchHeader_setTrafficClass(&err->switchHeader, 0xffff);
  81. SwitchHeader_setCongestion(&err->switchHeader, 0);
  82. err->handle = 0xffffffff;
  83. err->ctrl.header.type_be = Control_ERROR_be;
  84. err->ctrl.content.error.errorType_be = Endian_hostToBigEndian32(code);
  85. err->ctrl.header.checksum_be = 0;
  86. err->ctrl.header.checksum_be =
  87. Checksum_engine_be((uint8_t*) &err->ctrl, Message_getLength(cause) - SwitchHeader_SIZE - 4);
  88. return Iface_next(&iface->iface, cause);
  89. }
  90. #define DEBUG_SRC_DST(logger, message) \
  91. Log_debug(logger, message " ([%u] to [%u])", sourceIndex, destIndex)
  92. /** This never returns an error, it sends an error packet instead. */
  93. static Iface_DEFUN receiveMessage(Message_t* message, struct Iface* iface)
  94. {
  95. struct SwitchInterface* sourceIf = Identity_check((struct SwitchInterface*) iface);
  96. struct SwitchCore_pvt* core = Identity_check(sourceIf->core);
  97. if (Message_getLength(message) < SwitchHeader_SIZE) {
  98. Log_debug(core->logger, "DROP runt");
  99. return Error(message, "RUNT");
  100. }
  101. struct SwitchHeader* header = (struct SwitchHeader*) Message_bytes(message);
  102. const uint64_t label = Endian_bigEndianToHost64(header->label_be);
  103. uint32_t bits = NumberCompress_bitsUsedForLabel(label);
  104. const uint32_t sourceIndex = sourceIf - core->interfaces;
  105. const uint32_t destIndex = NumberCompress_getDecompressed(label, bits);
  106. const uint32_t sourceBits = NumberCompress_bitsUsedForNumber(sourceIndex);
  107. Assert_true(destIndex < NumberCompress_INTERFACES);
  108. Assert_true(sourceIndex < NumberCompress_INTERFACES);
  109. if (1 == destIndex && 1 != (label & 0xf)) {
  110. // routing interface: must always be compressed as 0001
  111. DEBUG_SRC_DST(core->logger,
  112. "DROP packet for this router because the destination "
  113. "discriminator was wrong");
  114. return sendError(sourceIf, message, Error_MALFORMED_ADDRESS, core->logger);
  115. }
  116. if (sourceBits > bits) {
  117. if (destIndex == 1) {
  118. // If the destination index is this router, don't drop the packet since there no
  119. // way for a node to know the size of the representation of its source label.
  120. // - label ends in 0001; if there are enough zeroes at the end after removing the 1,
  121. // we can still fit in the source discriminator
  122. // - the return path probably doesn't start with 3 zeroes, but it will still be working,
  123. // as the source discriminator is large enough to make space for 3 zeroes between
  124. // reverse return path and forward path (see below)
  125. if (0 != ((label ^ 1) & (UINT64_MAX >> (64 - sourceBits - 4)))) {
  126. // This is a bug.
  127. // https://github.com/cjdelisle/cjdns/issues/93
  128. // The problem is that there is no way to splice a route and know for certain
  129. // that you've not spliced one which will end up in this if statement.
  130. // Unfortunately there seems no clean way around this issue at the moment.
  131. // If this router and switch communicated using labels with "64 + four less
  132. // than the number of bits in largest discriminator" bits wide, it could handle
  133. // this situation, this solution is obviously non-trivial.
  134. DEBUG_SRC_DST(core->logger,
  135. "DROP packet for this router because there is no way to "
  136. "represent the return path.");
  137. return sendError(sourceIf, message, Error_RETURN_PATH_INVALID, core->logger);
  138. }
  139. bits = sourceBits;
  140. } else if (1 == sourceIndex) {
  141. // - we need at least 3 zeroes between reverse return path and forward path:
  142. // right now the label only contains the forward path
  143. // - sourceBits == 4, bits < 4 -> bits + 64 - sourceBits < 64
  144. // - the reverse source discriminator "1000" and the target discriminator "0001"
  145. // can overlap as "10001" (or "100001" or ...)
  146. if (0 != label >> (bits + 64 - sourceBits)) {
  147. // not enough zeroes
  148. DEBUG_SRC_DST(core->logger, "DROP packet because source address is "
  149. "larger than destination address.");
  150. return sendError(sourceIf, message, Error_MALFORMED_ADDRESS, core->logger);
  151. }
  152. } else {
  153. //Log_info(core->logger, "source exceeds dest");
  154. DEBUG_SRC_DST(core->logger, "DROP packet because source address is "
  155. "larger than destination address.");
  156. return sendError(sourceIf, message, Error_MALFORMED_ADDRESS, core->logger);
  157. }
  158. }
  159. if (core->interfaces[destIndex].alloc == NULL) {
  160. if (sourceIndex == 1) {
  161. DEBUG_SRC_DST(core->logger, "DROP packet we tried to send, no such peer");
  162. }
  163. // This is important, but it's someone else's important problem
  164. // DEBUG_SRC_DST(core->logger, "DROP packet because there is no interface "
  165. // "where the bits specify.");
  166. return sendError(sourceIf, message, Error_MALFORMED_ADDRESS, core->logger);
  167. }
  168. /*if (sourceIndex == destIndex && sourceIndex != 1) {
  169. DEBUG_SRC_DST(core->logger, "DROP Packet with redundant route.");
  170. return sendError(sourceIf, message, Error_LOOP_ROUTE, core->logger);
  171. }*/
  172. uint64_t sourceLabel = Bits_bitReverse64(NumberCompress_getCompressed(sourceIndex, bits));
  173. uint64_t targetLabel = (label >> bits) | sourceLabel;
  174. int cloneLength = (Message_getLength(message) < Control_Error_MAX_SIZE) ?
  175. Message_getLength(message) : Control_Error_MAX_SIZE;
  176. uint8_t messageClone[Control_Error_MAX_SIZE];
  177. Bits_memcpy(messageClone, Message_bytes(message), cloneLength);
  178. // Update the header
  179. header->label_be = Endian_hostToBigEndian64(targetLabel);
  180. uint32_t labelShift = SwitchHeader_getLabelShift(header) + bits;
  181. if (labelShift > 63) {
  182. // TODO(cjd): hmm should we return an error packet?
  183. Log_debug(core->logger, "Label rolled over");
  184. return Error(message, "UNDELIVERABLE");
  185. }
  186. SwitchHeader_setLabelShift(header, labelShift);
  187. SwitchHeader_setTrafficClass(header, 0xffff);
  188. return Iface_next(&core->interfaces[destIndex].iface, message);
  189. }
  190. static void removeInterface(struct Allocator_OnFreeJob* job)
  191. {
  192. struct SwitchInterface* si = Identity_check((struct SwitchInterface*) job->userData);
  193. Bits_memset(si, 0, sizeof(struct SwitchInterface));
  194. }
  195. void SwitchCore_swapInterfaces(struct Iface* userIf1, struct Iface* userIf2)
  196. {
  197. struct SwitchInterface* si1 = Identity_check((struct SwitchInterface*) userIf1->connectedIf);
  198. struct SwitchInterface* si2 = Identity_check((struct SwitchInterface*) userIf2->connectedIf);
  199. Iface_unplumb(userIf1, &si1->iface);
  200. Iface_unplumb(userIf2, &si2->iface);
  201. Assert_true(Allocator_cancelOnFree(si1->onFree) > -1);
  202. Assert_true(Allocator_cancelOnFree(si2->onFree) > -1);
  203. struct SwitchInterface si3;
  204. Bits_memcpy(&si3, si1, sizeof(struct SwitchInterface));
  205. Bits_memcpy(si1, si2, sizeof(struct SwitchInterface));
  206. Bits_memcpy(si2, &si3, sizeof(struct SwitchInterface));
  207. si1->onFree = Allocator_onFree(si1->alloc, removeInterface, si1);
  208. si2->onFree = Allocator_onFree(si2->alloc, removeInterface, si2);
  209. Iface_plumb(userIf2, &si1->iface);
  210. Iface_plumb(userIf1, &si2->iface);
  211. }
  212. int SwitchCore_addInterface(struct SwitchCore* switchCore,
  213. struct Iface* iface,
  214. struct Allocator* alloc,
  215. uint64_t* labelOut)
  216. {
  217. struct SwitchCore_pvt* core = Identity_check((struct SwitchCore_pvt*)switchCore);
  218. int ifIndex = 0;
  219. // If there's a vacent spot where another iface was before it was removed, use that.
  220. for (;;ifIndex++) {
  221. if (ifIndex == NumberCompress_INTERFACES) { return SwitchCore_addInterface_OUT_OF_SPACE; }
  222. if (!core->interfaces[ifIndex].iface.send) { break; }
  223. }
  224. struct SwitchInterface* newIf = &core->interfaces[ifIndex];
  225. Identity_set(newIf);
  226. newIf->iface.send = receiveMessage;
  227. newIf->core = core;
  228. newIf->alloc = alloc;
  229. newIf->onFree = Allocator_onFree(alloc, removeInterface, newIf);
  230. Iface_plumb(iface, &newIf->iface);
  231. uint32_t bits = NumberCompress_bitsUsedForNumber(ifIndex);
  232. *labelOut = NumberCompress_getCompressed(ifIndex, bits) | (1 << bits);
  233. return 0;
  234. }
  235. struct SwitchCore* SwitchCore_new(struct Log* logger,
  236. struct Allocator* allocator,
  237. EventBase_t* base)
  238. {
  239. struct SwitchCore_pvt* core = Allocator_calloc(allocator, sizeof(struct SwitchCore_pvt), 1);
  240. Identity_set(core);
  241. core->allocator = allocator;
  242. core->logger = logger;
  243. core->eventBase = base;
  244. struct SwitchInterface* routerIf = &core->interfaces[1];
  245. Identity_set(routerIf);
  246. routerIf->iface.send = receiveMessage;
  247. routerIf->core = core;
  248. routerIf->alloc = allocator;
  249. core->pub.routerIf = &routerIf->iface;
  250. return &core->pub;
  251. }