PacketBuffer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file PacketBuffer.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 <stdlib.h>
  30. #include <misc/debug.h>
  31. #include <misc/balloc.h>
  32. #include <flow/PacketBuffer.h>
  33. static void input_handler_done (PacketBuffer *buf, int in_len);
  34. static void output_handler_done (PacketBuffer *buf);
  35. void input_handler_done (PacketBuffer *buf, int in_len)
  36. {
  37. ASSERT(in_len >= 0)
  38. ASSERT(in_len <= buf->input_mtu)
  39. DebugObject_Access(&buf->d_obj);
  40. // remember if buffer is empty
  41. int was_empty = (buf->buf.output_avail < 0);
  42. // submit packet to buffer
  43. ChunkBuffer2_SubmitPacket(&buf->buf, in_len);
  44. // if there is space, schedule receive
  45. if (buf->buf.input_avail >= buf->input_mtu) {
  46. PacketRecvInterface_Receiver_Recv(buf->input, buf->buf.input_dest);
  47. }
  48. // if buffer was empty, schedule send
  49. if (was_empty) {
  50. PacketPassInterface_Sender_Send(buf->output, buf->buf.output_dest, buf->buf.output_avail);
  51. }
  52. }
  53. void output_handler_done (PacketBuffer *buf)
  54. {
  55. DebugObject_Access(&buf->d_obj);
  56. // remember if buffer is full
  57. int was_full = (buf->buf.input_avail < buf->input_mtu);
  58. // remove packet from buffer
  59. ChunkBuffer2_ConsumePacket(&buf->buf);
  60. // if buffer was full and there is space, schedule receive
  61. if (was_full && buf->buf.input_avail >= buf->input_mtu) {
  62. PacketRecvInterface_Receiver_Recv(buf->input, buf->buf.input_dest);
  63. }
  64. // if there is more data, schedule send
  65. if (buf->buf.output_avail >= 0) {
  66. PacketPassInterface_Sender_Send(buf->output, buf->buf.output_dest, buf->buf.output_avail);
  67. }
  68. }
  69. int PacketBuffer_Init (PacketBuffer *buf, PacketRecvInterface *input, PacketPassInterface *output, int num_packets, BPendingGroup *pg)
  70. {
  71. ASSERT(PacketPassInterface_GetMTU(output) >= PacketRecvInterface_GetMTU(input))
  72. ASSERT(num_packets > 0)
  73. // init arguments
  74. buf->input = input;
  75. buf->output = output;
  76. // init input
  77. PacketRecvInterface_Receiver_Init(buf->input, (PacketRecvInterface_handler_done)input_handler_done, buf);
  78. // set input MTU
  79. buf->input_mtu = PacketRecvInterface_GetMTU(buf->input);
  80. // init output
  81. PacketPassInterface_Sender_Init(buf->output, (PacketPassInterface_handler_done)output_handler_done, buf);
  82. // allocate buffer
  83. int num_blocks = ChunkBuffer2_calc_blocks(buf->input_mtu, num_packets);
  84. if (num_blocks < 0) {
  85. goto fail0;
  86. }
  87. if (!(buf->buf_data = (struct ChunkBuffer2_block *)BAllocArray(num_blocks, sizeof(buf->buf_data[0])))) {
  88. goto fail0;
  89. }
  90. // init buffer
  91. ChunkBuffer2_Init(&buf->buf, buf->buf_data, num_blocks, buf->input_mtu);
  92. // schedule receive
  93. PacketRecvInterface_Receiver_Recv(buf->input, buf->buf.input_dest);
  94. DebugObject_Init(&buf->d_obj);
  95. return 1;
  96. fail0:
  97. return 0;
  98. }
  99. void PacketBuffer_Free (PacketBuffer *buf)
  100. {
  101. DebugObject_Free(&buf->d_obj);
  102. // free buffer
  103. BFree(buf->buf_data);
  104. }