RouteBuffer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * @file RouteBuffer.c
  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. #include <stddef.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <misc/offset.h>
  33. #include <flow/RouteBuffer.h>
  34. static struct RouteBuffer_packet * alloc_packet (int mtu)
  35. {
  36. if (mtu > SIZE_MAX - sizeof(struct RouteBuffer_packet)) {
  37. return NULL;
  38. }
  39. // allocate memory
  40. struct RouteBuffer_packet *p = (struct RouteBuffer_packet *)malloc(sizeof(*p) + mtu);
  41. if (!p) {
  42. return NULL;
  43. }
  44. return p;
  45. }
  46. static int alloc_free_packet (RouteBuffer *o)
  47. {
  48. struct RouteBuffer_packet *p = alloc_packet(o->mtu);
  49. if (!p) {
  50. return 0;
  51. }
  52. // add to free packets list
  53. LinkedList1_Append(&o->packets_free, &p->node);
  54. return 1;
  55. }
  56. static void free_free_packets (RouteBuffer *o)
  57. {
  58. while (!LinkedList1_IsEmpty(&o->packets_free)) {
  59. // get packet
  60. struct RouteBuffer_packet *p = UPPER_OBJECT(LinkedList1_GetLast(&o->packets_free), struct RouteBuffer_packet, node);
  61. // remove from free packets list
  62. LinkedList1_Remove(&o->packets_free, &p->node);
  63. // free memory
  64. free(p);
  65. }
  66. }
  67. static void release_used_packet (RouteBuffer *o)
  68. {
  69. ASSERT(!LinkedList1_IsEmpty(&o->packets_used))
  70. // get packet
  71. struct RouteBuffer_packet *p = UPPER_OBJECT(LinkedList1_GetFirst(&o->packets_used), struct RouteBuffer_packet, node);
  72. // remove from used packets list
  73. LinkedList1_Remove(&o->packets_used, &p->node);
  74. // add to free packets list
  75. LinkedList1_Append(&o->packets_free, &p->node);
  76. }
  77. static void send_used_packet (RouteBuffer *o)
  78. {
  79. ASSERT(!LinkedList1_IsEmpty(&o->packets_used))
  80. // get packet
  81. struct RouteBuffer_packet *p = UPPER_OBJECT(LinkedList1_GetFirst(&o->packets_used), struct RouteBuffer_packet, node);
  82. // send
  83. PacketPassInterface_Sender_Send(o->output, (uint8_t *)(p + 1), p->len);
  84. }
  85. static void output_handler_done (RouteBuffer *o)
  86. {
  87. ASSERT(!LinkedList1_IsEmpty(&o->packets_used))
  88. DebugObject_Access(&o->d_obj);
  89. // release packet
  90. release_used_packet(o);
  91. // send next packet if there is one
  92. if (!LinkedList1_IsEmpty(&o->packets_used)) {
  93. send_used_packet(o);
  94. }
  95. }
  96. int RouteBuffer_Init (RouteBuffer *o, int mtu, PacketPassInterface *output, int buf_size)
  97. {
  98. ASSERT(mtu >= 0)
  99. ASSERT(PacketPassInterface_GetMTU(output) >= mtu)
  100. ASSERT(buf_size > 0)
  101. // init arguments
  102. o->mtu = mtu;
  103. o->output = output;
  104. // init output
  105. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  106. // init free packets list
  107. LinkedList1_Init(&o->packets_free);
  108. // init used packets list
  109. LinkedList1_Init(&o->packets_used);
  110. // allocate packets
  111. for (int i = 0; i < buf_size; i++) {
  112. if (!alloc_free_packet(o)) {
  113. goto fail1;
  114. }
  115. }
  116. DebugObject_Init(&o->d_obj);
  117. return 1;
  118. fail1:
  119. free_free_packets(o);
  120. return 0;
  121. }
  122. void RouteBuffer_Free (RouteBuffer *o)
  123. {
  124. DebugObject_Free(&o->d_obj);
  125. // release packets so they can be freed
  126. while (!LinkedList1_IsEmpty(&o->packets_used)) {
  127. release_used_packet(o);
  128. }
  129. // free packets
  130. free_free_packets(o);
  131. }
  132. int RouteBuffer_GetMTU (RouteBuffer *o)
  133. {
  134. DebugObject_Access(&o->d_obj);
  135. return o->mtu;
  136. }
  137. int RouteBufferSource_Init (RouteBufferSource *o, int mtu)
  138. {
  139. ASSERT(mtu >= 0)
  140. // init arguments
  141. o->mtu = mtu;
  142. // allocate current packet
  143. if (!(o->current_packet = alloc_packet(o->mtu))) {
  144. goto fail0;
  145. }
  146. DebugObject_Init(&o->d_obj);
  147. return 1;
  148. fail0:
  149. return 0;
  150. }
  151. void RouteBufferSource_Free (RouteBufferSource *o)
  152. {
  153. DebugObject_Free(&o->d_obj);
  154. // free current packet
  155. free(o->current_packet);
  156. }
  157. uint8_t * RouteBufferSource_Pointer (RouteBufferSource *o)
  158. {
  159. DebugObject_Access(&o->d_obj);
  160. return (uint8_t *)(o->current_packet + 1);
  161. }
  162. int RouteBufferSource_Route (RouteBufferSource *o, int len, RouteBuffer *b, int copy_offset, int copy_len)
  163. {
  164. ASSERT(len >= 0)
  165. ASSERT(len <= o->mtu)
  166. ASSERT(b->mtu == o->mtu)
  167. ASSERT(copy_offset >= 0)
  168. ASSERT(copy_offset <= o->mtu)
  169. ASSERT(copy_len >= 0)
  170. ASSERT(copy_len <= o->mtu - copy_offset)
  171. DebugObject_Access(&b->d_obj);
  172. DebugObject_Access(&o->d_obj);
  173. // check if there's space in the buffer
  174. if (LinkedList1_IsEmpty(&b->packets_free)) {
  175. return 0;
  176. }
  177. int was_empty = LinkedList1_IsEmpty(&b->packets_used);
  178. struct RouteBuffer_packet *p = o->current_packet;
  179. // set packet length
  180. p->len = len;
  181. // append packet to used packets list
  182. LinkedList1_Append(&b->packets_used, &p->node);
  183. // get a free packet
  184. struct RouteBuffer_packet *np = UPPER_OBJECT(LinkedList1_GetLast(&b->packets_free), struct RouteBuffer_packet, node);
  185. // remove it from free packets list
  186. LinkedList1_Remove(&b->packets_free, &np->node);
  187. // make it the current packet
  188. o->current_packet = np;
  189. // copy packet
  190. if (copy_len > 0) {
  191. memcpy((uint8_t *)(np + 1) + copy_offset, (uint8_t *)(p + 1) + copy_offset, copy_len);
  192. }
  193. // start sending if required
  194. if (was_empty) {
  195. send_used_packet(b);
  196. }
  197. return 1;
  198. }