SwitchAdapter.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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* alloc, struct Log* log)
  65. {
  66. struct SwitchAdapter_pvt* out = Allocator_calloc(alloc, sizeof(struct SwitchAdapter_pvt), 1);
  67. out->pub.controlIf.send = incomingFromControlIf;
  68. out->pub.sessionManagerIf.send = incomingFromSessionManagerIf;
  69. out->pub.switchIf.send = incomingFromSwitchIf;
  70. out->log = log;
  71. Identity_set(out);
  72. return &out->pub;
  73. }