RouteBuffer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @file RouteBuffer.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Packet buffer for zero-copy packet routing.
  25. */
  26. #ifndef BADVPN_FLOW_ROUTEBUFFER_H
  27. #define BADVPN_FLOW_ROUTEBUFFER_H
  28. #include <misc/debug.h>
  29. #include <structure/LinkedList1.h>
  30. #include <system/DebugObject.h>
  31. #include <flow/PacketPassInterface.h>
  32. struct RouteBuffer_packet {
  33. LinkedList1Node node;
  34. int len;
  35. };
  36. /**
  37. * Packet buffer for zero-copy packet routing.
  38. *
  39. * Packets are buffered using {@link RouteBufferSource} objects.
  40. */
  41. typedef struct {
  42. int mtu;
  43. PacketPassInterface *output;
  44. LinkedList1 packets_free;
  45. LinkedList1 packets_used;
  46. DebugObject d_obj;
  47. } RouteBuffer;
  48. /**
  49. * Object through which packets are buffered into {@link RouteBuffer} objects.
  50. *
  51. * A packet is routed by calling {@link RouteBufferSource_Pointer}, writing it to
  52. * the returned address, then calling {@link RouteBufferSource_Route}.
  53. */
  54. typedef struct {
  55. int mtu;
  56. struct RouteBuffer_packet *current_packet;
  57. DebugObject d_obj;
  58. } RouteBufferSource;
  59. /**
  60. * Initializes the object.
  61. *
  62. * @param o the object
  63. * @param mtu maximum packet size. Must be >=0. It will only be possible to route packets to this buffer
  64. * from {@link RouteBufferSource}.s with the same MTU.
  65. * @param output output interface. Its MTU must be >=mtu.
  66. * @param buf_size size of the buffer in number of packet. Must be >0.
  67. * @return 1 on success, 0 on failure
  68. */
  69. int RouteBuffer_Init (RouteBuffer *o, int mtu, PacketPassInterface *output, int buf_size) WARN_UNUSED;
  70. /**
  71. * Frees the object.
  72. */
  73. void RouteBuffer_Free (RouteBuffer *o);
  74. /**
  75. * Retuns the buffer's MTU (mtu argument to {@link RouteBuffer_Init}).
  76. *
  77. * @return MTU
  78. */
  79. int RouteBuffer_GetMTU (RouteBuffer *o);
  80. /**
  81. * Initializes the object.
  82. *
  83. * @param o the object
  84. * @param mtu maximum packet size. Must be >=0. The object will only be able to route packets
  85. * to {@link RouteBuffer}'s with the same MTU.
  86. * @return 1 on success, 0 on failure
  87. */
  88. int RouteBufferSource_Init (RouteBufferSource *o, int mtu) WARN_UNUSED;
  89. /**
  90. * Frees the object.
  91. *
  92. * @param o the object
  93. */
  94. void RouteBufferSource_Free (RouteBufferSource *o);
  95. /**
  96. * Returns a pointer to the current packet.
  97. * The pointed to memory area will have space for MTU bytes.
  98. * The pointer is only valid until {@link RouteBufferSource_Route} succeeds.
  99. *
  100. * @param o the object
  101. * @return pointer to the current packet
  102. */
  103. uint8_t * RouteBufferSource_Pointer (RouteBufferSource *o);
  104. /**
  105. * Routes the current packet to a given buffer.
  106. * On success, this invalidates the pointer previously returned from
  107. * {@link RouteBufferSource_Pointer}.
  108. *
  109. * @param o the object
  110. * @param len length of the packet. Must be >=0 and <=MTU.
  111. * @param b buffer to route to. Its MTU must equal this object's MTU.
  112. * @param copy_offset Offset from the beginning for copying. Must be >=0 and
  113. * <=mtu.
  114. * @param copy_len Number of bytes to copy from the old current packet to the new one.
  115. * Must be >=0 and <= mtu - copy_offset.
  116. * @return 1 on success, 0 on failure
  117. */
  118. int RouteBufferSource_Route (RouteBufferSource *o, int len, RouteBuffer *b, int copy_offset, int copy_len);
  119. #endif