SwitchAdapter.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <http://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "net/SwitchAdapter.h"
  17. #include "util/Identity.h"
  18. #include "wire/SwitchHeader.h"
  19. #include "util/Linker.h"
  20. Linker_require("net/SwitchAdapter.c")
  21. struct SwitchAdapter_pvt
  22. {
  23. struct SwitchAdapter pub;
  24. struct Log* log;
  25. Identity
  26. };
  27. struct Header {
  28. struct SwitchHeader sh;
  29. uint32_t handle_be;
  30. };
  31. #define Header_SIZE 16
  32. Assert_compileTime(sizeof(struct Header) == Header_SIZE);
  33. static Iface_DEFUN incomingFromControlIf(struct Message* msg, struct Iface* controlIf)
  34. {
  35. struct SwitchAdapter_pvt* sa =
  36. Identity_containerOf(controlIf, struct SwitchAdapter_pvt, pub.controlIf);
  37. return Iface_next(&sa->pub.switchIf, msg);
  38. }
  39. static Iface_DEFUN incomingFromSessionManagerIf(struct Message* msg, struct Iface* sessionManagerIf)
  40. {
  41. struct SwitchAdapter_pvt* sa =
  42. Identity_containerOf(sessionManagerIf, struct SwitchAdapter_pvt, pub.sessionManagerIf);
  43. return Iface_next(&sa->pub.switchIf, msg);
  44. }
  45. static Iface_DEFUN incomingFromSwitchIf(struct Message* msg, struct Iface* switchIf)
  46. {
  47. struct SwitchAdapter_pvt* sa =
  48. Identity_containerOf(switchIf, struct SwitchAdapter_pvt, pub.switchIf);
  49. if (msg->length < Header_SIZE) {
  50. Log_debug(sa->log, "DROP runt");
  51. return 0;
  52. }
  53. Assert_true(!(((uintptr_t)msg->bytes) % 4) && "alignment");
  54. struct Header* hdr = (struct Header*) msg->bytes;
  55. // The label comes in reversed from the switch because the switch doesn't know that we aren't
  56. // another switch ready to parse more bits, bit reversing the label yields the source address.
  57. // (the field is still big endian!)
  58. hdr->sh.label_be = Bits_bitReverse64(hdr->sh.label_be);
  59. if (hdr->handle_be == 0xffffffff) {
  60. return Iface_next(&sa->pub.controlIf, msg);
  61. }
  62. return Iface_next(&sa->pub.sessionManagerIf, msg);
  63. }
  64. struct SwitchAdapter* SwitchAdapter_new(struct Allocator* allocator, struct Log* log)
  65. {
  66. struct Allocator* alloc = Allocator_child(allocator);
  67. struct SwitchAdapter_pvt* out = Allocator_calloc(alloc, sizeof(struct SwitchAdapter_pvt), 1);
  68. out->pub.controlIf.send = incomingFromControlIf;
  69. out->pub.sessionManagerIf.send = incomingFromSessionManagerIf;
  70. out->pub.switchIf.send = incomingFromSwitchIf;
  71. out->log = log;
  72. Identity_set(out);
  73. return &out->pub;
  74. }