RouteBuffer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @file RouteBuffer.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Packet buffer for zero-copy packet routing.
  32. */
  33. #ifndef BADVPN_FLOW_ROUTEBUFFER_H
  34. #define BADVPN_FLOW_ROUTEBUFFER_H
  35. #include <misc/debug.h>
  36. #include <structure/LinkedList1.h>
  37. #include <base/DebugObject.h>
  38. #include <flow/PacketPassInterface.h>
  39. struct RouteBuffer_packet {
  40. LinkedList1Node node;
  41. int len;
  42. };
  43. /**
  44. * Packet buffer for zero-copy packet routing.
  45. *
  46. * Packets are buffered using {@link RouteBufferSource} objects.
  47. */
  48. typedef struct {
  49. int mtu;
  50. PacketPassInterface *output;
  51. LinkedList1 packets_free;
  52. LinkedList1 packets_used;
  53. DebugObject d_obj;
  54. } RouteBuffer;
  55. /**
  56. * Object through which packets are buffered into {@link RouteBuffer} objects.
  57. *
  58. * A packet is routed by calling {@link RouteBufferSource_Pointer}, writing it to
  59. * the returned address, then calling {@link RouteBufferSource_Route}.
  60. */
  61. typedef struct {
  62. int mtu;
  63. struct RouteBuffer_packet *current_packet;
  64. DebugObject d_obj;
  65. } RouteBufferSource;
  66. /**
  67. * Initializes the object.
  68. *
  69. * @param o the object
  70. * @param mtu maximum packet size. Must be >=0. It will only be possible to route packets to this buffer
  71. * from {@link RouteBufferSource}.s with the same MTU.
  72. * @param output output interface. Its MTU must be >=mtu.
  73. * @param buf_size size of the buffer in number of packet. Must be >0.
  74. * @return 1 on success, 0 on failure
  75. */
  76. int RouteBuffer_Init (RouteBuffer *o, int mtu, PacketPassInterface *output, int buf_size) WARN_UNUSED;
  77. /**
  78. * Frees the object.
  79. */
  80. void RouteBuffer_Free (RouteBuffer *o);
  81. /**
  82. * Retuns the buffer's MTU (mtu argument to {@link RouteBuffer_Init}).
  83. *
  84. * @return MTU
  85. */
  86. int RouteBuffer_GetMTU (RouteBuffer *o);
  87. /**
  88. * Initializes the object.
  89. *
  90. * @param o the object
  91. * @param mtu maximum packet size. Must be >=0. The object will only be able to route packets
  92. * to {@link RouteBuffer}'s with the same MTU.
  93. * @return 1 on success, 0 on failure
  94. */
  95. int RouteBufferSource_Init (RouteBufferSource *o, int mtu) WARN_UNUSED;
  96. /**
  97. * Frees the object.
  98. *
  99. * @param o the object
  100. */
  101. void RouteBufferSource_Free (RouteBufferSource *o);
  102. /**
  103. * Returns a pointer to the current packet.
  104. * The pointed to memory area will have space for MTU bytes.
  105. * The pointer is only valid until {@link RouteBufferSource_Route} succeeds.
  106. *
  107. * @param o the object
  108. * @return pointer to the current packet
  109. */
  110. uint8_t * RouteBufferSource_Pointer (RouteBufferSource *o);
  111. /**
  112. * Routes the current packet to a given buffer.
  113. * On success, this invalidates the pointer previously returned from
  114. * {@link RouteBufferSource_Pointer}.
  115. *
  116. * @param o the object
  117. * @param len length of the packet. Must be >=0 and <=MTU.
  118. * @param b buffer to route to. Its MTU must equal this object's MTU.
  119. * @param copy_offset Offset from the beginning for copying. Must be >=0 and
  120. * <=mtu.
  121. * @param copy_len Number of bytes to copy from the old current packet to the new one.
  122. * Must be >=0 and <= mtu - copy_offset.
  123. * @return 1 on success, 0 on failure
  124. */
  125. int RouteBufferSource_Route (RouteBufferSource *o, int len, RouteBuffer *b, int copy_offset, int copy_len);
  126. #endif