SwitchCore.c 13 KB

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