DPRelay.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @file DPRelay.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 <string.h>
  31. #include <misc/offset.h>
  32. #include <base/BLog.h>
  33. #include <client/DPRelay.h>
  34. #include <generated/blog_channel_DPRelay.h>
  35. static void flow_inactivity_handler (struct DPRelay_flow *flow);
  36. static struct DPRelay_flow * create_flow (DPRelaySource *src, DPRelaySink *sink, int num_packets, int inactivity_time)
  37. {
  38. ASSERT(num_packets > 0)
  39. // allocate structure
  40. struct DPRelay_flow *flow = (struct DPRelay_flow *)malloc(sizeof(*flow));
  41. if (!flow) {
  42. BLog(BLOG_ERROR, "relay flow %d->%d: malloc failed", (int)src->source_id, (int)sink->dest_id);
  43. goto fail0;
  44. }
  45. // set src and sink
  46. flow->src = src;
  47. flow->sink = sink;
  48. // init DataProtoFlow
  49. if (!DataProtoFlow_Init(&flow->dp_flow, &src->router->dp_source, src->source_id, sink->dest_id, num_packets, inactivity_time, flow, (DataProtoFlow_handler_inactivity)flow_inactivity_handler)) {
  50. BLog(BLOG_ERROR, "relay flow %d->%d: DataProtoFlow_Init failed", (int)src->source_id, (int)sink->dest_id);
  51. goto fail1;
  52. }
  53. // insert to source list
  54. LinkedList1_Append(&src->flows_list, &flow->src_list_node);
  55. // insert to sink list
  56. LinkedList1_Append(&sink->flows_list, &flow->sink_list_node);
  57. // attach flow if needed
  58. if (sink->dp_sink) {
  59. DataProtoFlow_Attach(&flow->dp_flow, sink->dp_sink);
  60. }
  61. BLog(BLOG_INFO, "relay flow %d->%d: created", (int)src->source_id, (int)sink->dest_id);
  62. return flow;
  63. fail1:
  64. free(flow);
  65. fail0:
  66. return NULL;
  67. }
  68. static void free_flow (struct DPRelay_flow *flow)
  69. {
  70. // detach flow if needed
  71. if (flow->sink->dp_sink) {
  72. DataProtoFlow_Detach(&flow->dp_flow);
  73. }
  74. // remove posible router reference
  75. if (flow->src->router->current_flow == flow) {
  76. flow->src->router->current_flow = NULL;
  77. }
  78. // remove from sink list
  79. LinkedList1_Remove(&flow->sink->flows_list, &flow->sink_list_node);
  80. // remove from source list
  81. LinkedList1_Remove(&flow->src->flows_list, &flow->src_list_node);
  82. // free DataProtoFlow
  83. DataProtoFlow_Free(&flow->dp_flow);
  84. // free structore
  85. free(flow);
  86. }
  87. static void flow_inactivity_handler (struct DPRelay_flow *flow)
  88. {
  89. BLog(BLOG_INFO, "relay flow %d->%d: timed out", (int)flow->src->source_id, (int)flow->sink->dest_id);
  90. free_flow(flow);
  91. }
  92. static struct DPRelay_flow * source_find_flow (DPRelaySource *o, DPRelaySink *sink)
  93. {
  94. for (LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list); node; node = LinkedList1Node_Next(node)) {
  95. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  96. ASSERT(flow->src == o)
  97. if (flow->sink == sink) {
  98. return flow;
  99. }
  100. }
  101. return NULL;
  102. }
  103. static void router_dp_source_handler (DPRelayRouter *o, const uint8_t *frame, int frame_len)
  104. {
  105. DebugObject_Access(&o->d_obj);
  106. if (!o->current_flow) {
  107. return;
  108. }
  109. // route frame to current flow
  110. DataProtoFlow_Route(&o->current_flow->dp_flow, 0);
  111. // set no current flow
  112. o->current_flow = NULL;
  113. }
  114. int DPRelayRouter_Init (DPRelayRouter *o, int frame_mtu, BReactor *reactor)
  115. {
  116. ASSERT(frame_mtu >= 0)
  117. ASSERT(frame_mtu <= INT_MAX - DATAPROTO_MAX_OVERHEAD)
  118. // init arguments
  119. o->frame_mtu = frame_mtu;
  120. // init BufferWriter
  121. BufferWriter_Init(&o->writer, frame_mtu, BReactor_PendingGroup(reactor));
  122. // init DataProtoSource
  123. if (!DataProtoSource_Init(&o->dp_source, BufferWriter_GetOutput(&o->writer), (DataProtoSource_handler)router_dp_source_handler, o, reactor)) {
  124. BLog(BLOG_ERROR, "DataProtoSource_Init failed");
  125. goto fail1;
  126. }
  127. // have no current flow
  128. o->current_flow = NULL;
  129. DebugCounter_Init(&o->d_ctr);
  130. DebugObject_Init(&o->d_obj);
  131. return 1;
  132. fail1:
  133. BufferWriter_Free(&o->writer);
  134. return 0;
  135. }
  136. void DPRelayRouter_Free (DPRelayRouter *o)
  137. {
  138. DebugObject_Free(&o->d_obj);
  139. DebugCounter_Free(&o->d_ctr);
  140. ASSERT(!o->current_flow) // have no sources
  141. // free DataProtoSource
  142. DataProtoSource_Free(&o->dp_source);
  143. // free BufferWriter
  144. BufferWriter_Free(&o->writer);
  145. }
  146. void DPRelayRouter_SubmitFrame (DPRelayRouter *o, DPRelaySource *src, DPRelaySink *sink, uint8_t *data, int data_len, int num_packets, int inactivity_time)
  147. {
  148. DebugObject_Access(&o->d_obj);
  149. DebugObject_Access(&src->d_obj);
  150. DebugObject_Access(&sink->d_obj);
  151. ASSERT(!o->current_flow)
  152. ASSERT(src->router == o)
  153. ASSERT(data_len >= 0)
  154. ASSERT(data_len <= o->frame_mtu)
  155. ASSERT(num_packets > 0)
  156. // get memory location
  157. uint8_t *out;
  158. if (!BufferWriter_StartPacket(&o->writer, &out)) {
  159. BLog(BLOG_ERROR, "BufferWriter_StartPacket failed for frame %d->%d !?", (int)src->source_id, (int)sink->dest_id);
  160. return;
  161. }
  162. // write frame
  163. memcpy(out, data, data_len);
  164. // submit frame
  165. BufferWriter_EndPacket(&o->writer, data_len);
  166. // get a flow
  167. // this comes _after_ writing the packet, in case flow initialization schedules jobs
  168. struct DPRelay_flow *flow = source_find_flow(src, sink);
  169. if (!flow) {
  170. if (!(flow = create_flow(src, sink, num_packets, inactivity_time))) {
  171. return;
  172. }
  173. }
  174. // remember flow so we know where to route the frame in router_dp_source_handler
  175. o->current_flow = flow;
  176. }
  177. void DPRelaySource_Init (DPRelaySource *o, DPRelayRouter *router, peerid_t source_id, BReactor *reactor)
  178. {
  179. DebugObject_Access(&router->d_obj);
  180. // init arguments
  181. o->router = router;
  182. o->source_id = source_id;
  183. // init flows list
  184. LinkedList1_Init(&o->flows_list);
  185. DebugCounter_Increment(&o->router->d_ctr);
  186. DebugObject_Init(&o->d_obj);
  187. }
  188. void DPRelaySource_Free (DPRelaySource *o)
  189. {
  190. DebugObject_Free(&o->d_obj);
  191. DebugCounter_Decrement(&o->router->d_ctr);
  192. // free flows, detaching them if needed
  193. LinkedList1Node *node;
  194. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  195. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, src_list_node);
  196. free_flow(flow);
  197. }
  198. }
  199. void DPRelaySink_Init (DPRelaySink *o, peerid_t dest_id)
  200. {
  201. // init arguments
  202. o->dest_id = dest_id;
  203. // init flows list
  204. LinkedList1_Init(&o->flows_list);
  205. // have no sink
  206. o->dp_sink = NULL;
  207. DebugObject_Init(&o->d_obj);
  208. }
  209. void DPRelaySink_Free (DPRelaySink *o)
  210. {
  211. DebugObject_Free(&o->d_obj);
  212. ASSERT(!o->dp_sink)
  213. // free flows
  214. LinkedList1Node *node;
  215. while (node = LinkedList1_GetFirst(&o->flows_list)) {
  216. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  217. free_flow(flow);
  218. }
  219. }
  220. void DPRelaySink_Attach (DPRelaySink *o, DataProtoSink *dp_sink)
  221. {
  222. DebugObject_Access(&o->d_obj);
  223. ASSERT(!o->dp_sink)
  224. ASSERT(dp_sink)
  225. // attach flows
  226. for (LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list); node; node = LinkedList1Node_Next(node)) {
  227. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  228. DataProtoFlow_Attach(&flow->dp_flow, dp_sink);
  229. }
  230. // set sink
  231. o->dp_sink = dp_sink;
  232. }
  233. void DPRelaySink_Detach (DPRelaySink *o)
  234. {
  235. DebugObject_Access(&o->d_obj);
  236. ASSERT(o->dp_sink)
  237. // detach flows
  238. for (LinkedList1Node *node = LinkedList1_GetFirst(&o->flows_list); node; node = LinkedList1Node_Next(node)) {
  239. struct DPRelay_flow *flow = UPPER_OBJECT(node, struct DPRelay_flow, sink_list_node);
  240. DataProtoFlow_Detach(&flow->dp_flow);
  241. }
  242. // set no sink
  243. o->dp_sink = NULL;
  244. }