DataProto.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /**
  2. * @file DataProto.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 <limits.h>
  32. #include <protocol/dataproto.h>
  33. #include <misc/byteorder.h>
  34. #include <base/BLog.h>
  35. #include <client/DataProto.h>
  36. #include <generated/blog_channel_DataProto.h>
  37. static void monitor_handler (DataProtoSink *o);
  38. static void refresh_up_job (DataProtoSink *o);
  39. static void receive_timer_handler (DataProtoSink *o);
  40. static void notifier_handler (DataProtoSink *o, uint8_t *data, int data_len);
  41. static void up_job_handler (DataProtoSink *o);
  42. static void flow_buffer_free (struct DataProtoFlow_buffer *b);
  43. static void flow_buffer_attach (struct DataProtoFlow_buffer *b, DataProtoSink *sink);
  44. static void flow_buffer_detach (struct DataProtoFlow_buffer *b);
  45. static void flow_buffer_schedule_detach (struct DataProtoFlow_buffer *b);
  46. static void flow_buffer_finish_detach (struct DataProtoFlow_buffer *b);
  47. static void flow_buffer_qflow_handler_busy (struct DataProtoFlow_buffer *b);
  48. void monitor_handler (DataProtoSink *o)
  49. {
  50. DebugObject_Access(&o->d_obj);
  51. // send keep-alive
  52. PacketRecvBlocker_AllowBlockedPacket(&o->ka_blocker);
  53. }
  54. void refresh_up_job (DataProtoSink *o)
  55. {
  56. if (o->up != o->up_report) {
  57. BPending_Set(&o->up_job);
  58. } else {
  59. BPending_Unset(&o->up_job);
  60. }
  61. }
  62. void receive_timer_handler (DataProtoSink *o)
  63. {
  64. DebugObject_Access(&o->d_obj);
  65. // consider down
  66. o->up = 0;
  67. refresh_up_job(o);
  68. }
  69. void notifier_handler (DataProtoSink *o, uint8_t *data, int data_len)
  70. {
  71. DebugObject_Access(&o->d_obj);
  72. ASSERT(data_len >= sizeof(struct dataproto_header))
  73. int flags = 0;
  74. // if we are receiving keepalives, set the flag
  75. if (BTimer_IsRunning(&o->receive_timer)) {
  76. flags |= DATAPROTO_FLAGS_RECEIVING_KEEPALIVES;
  77. }
  78. // modify existing packet here
  79. struct dataproto_header *header = (struct dataproto_header *)data;
  80. header->flags = htol8(flags);
  81. }
  82. void up_job_handler (DataProtoSink *o)
  83. {
  84. DebugObject_Access(&o->d_obj);
  85. ASSERT(o->up != o->up_report)
  86. o->up_report = o->up;
  87. o->handler(o->user, o->up);
  88. return;
  89. }
  90. void source_router_handler (DataProtoSource *o, uint8_t *buf, int recv_len)
  91. {
  92. DebugObject_Access(&o->d_obj);
  93. ASSERT(buf)
  94. ASSERT(recv_len >= 0)
  95. ASSERT(recv_len <= o->frame_mtu)
  96. // remember packet
  97. o->current_buf = buf;
  98. o->current_recv_len = recv_len;
  99. // call handler
  100. o->handler(o->user, buf + DATAPROTO_MAX_OVERHEAD, recv_len);
  101. return;
  102. }
  103. void flow_buffer_free (struct DataProtoFlow_buffer *b)
  104. {
  105. ASSERT(!b->sink)
  106. // free route buffer
  107. RouteBuffer_Free(&b->rbuf);
  108. // free inactivity monitor
  109. if (b->inactivity_time >= 0) {
  110. PacketPassInactivityMonitor_Free(&b->monitor);
  111. }
  112. // free connector
  113. PacketPassConnector_Free(&b->connector);
  114. // free buffer structure
  115. free(b);
  116. }
  117. void flow_buffer_attach (struct DataProtoFlow_buffer *b, DataProtoSink *sink)
  118. {
  119. ASSERT(!b->sink)
  120. // init queue flow
  121. PacketPassFairQueueFlow_Init(&b->sink_qflow, &sink->queue);
  122. // connect to queue flow
  123. PacketPassConnector_ConnectOutput(&b->connector, PacketPassFairQueueFlow_GetInput(&b->sink_qflow));
  124. // set DataProto
  125. b->sink = sink;
  126. }
  127. void flow_buffer_detach (struct DataProtoFlow_buffer *b)
  128. {
  129. ASSERT(b->sink)
  130. PacketPassFairQueueFlow_AssertFree(&b->sink_qflow);
  131. // disconnect from queue flow
  132. PacketPassConnector_DisconnectOutput(&b->connector);
  133. // free queue flow
  134. PacketPassFairQueueFlow_Free(&b->sink_qflow);
  135. // clear reference to this buffer in the sink
  136. if (b->sink->detaching_buffer == b) {
  137. b->sink->detaching_buffer = NULL;
  138. }
  139. // set no DataProto
  140. b->sink = NULL;
  141. }
  142. void flow_buffer_schedule_detach (struct DataProtoFlow_buffer *b)
  143. {
  144. ASSERT(b->sink)
  145. ASSERT(PacketPassFairQueueFlow_IsBusy(&b->sink_qflow))
  146. ASSERT(!b->sink->detaching_buffer || b->sink->detaching_buffer == b)
  147. if (b->sink->detaching_buffer == b) {
  148. return;
  149. }
  150. // request cancel
  151. PacketPassFairQueueFlow_RequestCancel(&b->sink_qflow);
  152. // set busy handler
  153. PacketPassFairQueueFlow_SetBusyHandler(&b->sink_qflow, (PacketPassFairQueue_handler_busy)flow_buffer_qflow_handler_busy, b);
  154. // remember this buffer in the sink so it can handle us if it goes away
  155. b->sink->detaching_buffer = b;
  156. }
  157. void flow_buffer_finish_detach (struct DataProtoFlow_buffer *b)
  158. {
  159. ASSERT(b->sink)
  160. ASSERT(b->sink->detaching_buffer == b)
  161. PacketPassFairQueueFlow_AssertFree(&b->sink_qflow);
  162. // detach
  163. flow_buffer_detach(b);
  164. if (!b->flow) {
  165. // free
  166. flow_buffer_free(b);
  167. } else if (b->flow->sink_desired) {
  168. // attach
  169. flow_buffer_attach(b, b->flow->sink_desired);
  170. }
  171. }
  172. void flow_buffer_qflow_handler_busy (struct DataProtoFlow_buffer *b)
  173. {
  174. ASSERT(b->sink)
  175. ASSERT(b->sink->detaching_buffer == b)
  176. PacketPassFairQueueFlow_AssertFree(&b->sink_qflow);
  177. flow_buffer_finish_detach(b);
  178. }
  179. int DataProtoSink_Init (DataProtoSink *o, BReactor *reactor, PacketPassInterface *output, btime_t keepalive_time, btime_t tolerance_time, DataProtoSink_handler handler, void *user)
  180. {
  181. ASSERT(PacketPassInterface_HasCancel(output))
  182. ASSERT(PacketPassInterface_GetMTU(output) >= DATAPROTO_MAX_OVERHEAD)
  183. // init arguments
  184. o->reactor = reactor;
  185. o->handler = handler;
  186. o->user = user;
  187. // set frame MTU
  188. o->frame_mtu = PacketPassInterface_GetMTU(output) - DATAPROTO_MAX_OVERHEAD;
  189. // init notifier
  190. PacketPassNotifier_Init(&o->notifier, output, BReactor_PendingGroup(o->reactor));
  191. PacketPassNotifier_SetHandler(&o->notifier, (PacketPassNotifier_handler_notify)notifier_handler, o);
  192. // init monitor
  193. PacketPassInactivityMonitor_Init(&o->monitor, PacketPassNotifier_GetInput(&o->notifier), o->reactor, keepalive_time, (PacketPassInactivityMonitor_handler)monitor_handler, o);
  194. PacketPassInactivityMonitor_Force(&o->monitor);
  195. // init queue
  196. if (!PacketPassFairQueue_Init(&o->queue, PacketPassInactivityMonitor_GetInput(&o->monitor), BReactor_PendingGroup(o->reactor), 1, 1)) {
  197. BLog(BLOG_ERROR, "PacketPassFairQueue_Init failed");
  198. goto fail1;
  199. }
  200. // init keepalive queue flow
  201. PacketPassFairQueueFlow_Init(&o->ka_qflow, &o->queue);
  202. // init keepalive source
  203. DataProtoKeepaliveSource_Init(&o->ka_source, BReactor_PendingGroup(o->reactor));
  204. // init keepalive blocker
  205. PacketRecvBlocker_Init(&o->ka_blocker, DataProtoKeepaliveSource_GetOutput(&o->ka_source), BReactor_PendingGroup(o->reactor));
  206. // init keepalive buffer
  207. if (!SinglePacketBuffer_Init(&o->ka_buffer, PacketRecvBlocker_GetOutput(&o->ka_blocker), PacketPassFairQueueFlow_GetInput(&o->ka_qflow), BReactor_PendingGroup(o->reactor))) {
  208. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
  209. goto fail2;
  210. }
  211. // init receive timer
  212. BTimer_Init(&o->receive_timer, tolerance_time, (BTimer_handler)receive_timer_handler, o);
  213. // init handler job
  214. BPending_Init(&o->up_job, BReactor_PendingGroup(o->reactor), (BPending_handler)up_job_handler, o);
  215. // set not up
  216. o->up = 0;
  217. o->up_report = 0;
  218. // set no detaching buffer
  219. o->detaching_buffer = NULL;
  220. DebugCounter_Init(&o->d_ctr);
  221. DebugObject_Init(&o->d_obj);
  222. return 1;
  223. fail2:
  224. PacketRecvBlocker_Free(&o->ka_blocker);
  225. DataProtoKeepaliveSource_Free(&o->ka_source);
  226. PacketPassFairQueueFlow_Free(&o->ka_qflow);
  227. PacketPassFairQueue_Free(&o->queue);
  228. fail1:
  229. PacketPassInactivityMonitor_Free(&o->monitor);
  230. PacketPassNotifier_Free(&o->notifier);
  231. return 0;
  232. }
  233. void DataProtoSink_Free (DataProtoSink *o)
  234. {
  235. DebugObject_Free(&o->d_obj);
  236. DebugCounter_Free(&o->d_ctr);
  237. // allow freeing queue flows
  238. PacketPassFairQueue_PrepareFree(&o->queue);
  239. // release detaching buffer
  240. if (o->detaching_buffer) {
  241. ASSERT(!o->detaching_buffer->flow || o->detaching_buffer->flow->sink_desired != o)
  242. flow_buffer_finish_detach(o->detaching_buffer);
  243. }
  244. // free handler job
  245. BPending_Free(&o->up_job);
  246. // free receive timer
  247. BReactor_RemoveTimer(o->reactor, &o->receive_timer);
  248. // free keepalive buffer
  249. SinglePacketBuffer_Free(&o->ka_buffer);
  250. // free keepalive blocker
  251. PacketRecvBlocker_Free(&o->ka_blocker);
  252. // free keepalive source
  253. DataProtoKeepaliveSource_Free(&o->ka_source);
  254. // free keepalive queue flow
  255. PacketPassFairQueueFlow_Free(&o->ka_qflow);
  256. // free queue
  257. PacketPassFairQueue_Free(&o->queue);
  258. // free monitor
  259. PacketPassInactivityMonitor_Free(&o->monitor);
  260. // free notifier
  261. PacketPassNotifier_Free(&o->notifier);
  262. }
  263. void DataProtoSink_Received (DataProtoSink *o, int peer_receiving)
  264. {
  265. ASSERT(peer_receiving == 0 || peer_receiving == 1)
  266. DebugObject_Access(&o->d_obj);
  267. // reset receive timer
  268. BReactor_SetTimer(o->reactor, &o->receive_timer);
  269. if (!peer_receiving) {
  270. // peer reports not receiving, consider down
  271. o->up = 0;
  272. // send keep-alive to converge faster
  273. PacketRecvBlocker_AllowBlockedPacket(&o->ka_blocker);
  274. } else {
  275. // consider up
  276. o->up = 1;
  277. }
  278. refresh_up_job(o);
  279. }
  280. int DataProtoSource_Init (DataProtoSource *o, PacketRecvInterface *input, DataProtoSource_handler handler, void *user, BReactor *reactor)
  281. {
  282. ASSERT(PacketRecvInterface_GetMTU(input) <= INT_MAX - DATAPROTO_MAX_OVERHEAD)
  283. ASSERT(handler)
  284. // init arguments
  285. o->handler = handler;
  286. o->user = user;
  287. o->reactor = reactor;
  288. // remember frame MTU
  289. o->frame_mtu = PacketRecvInterface_GetMTU(input);
  290. // init router
  291. if (!PacketRouter_Init(&o->router, DATAPROTO_MAX_OVERHEAD + o->frame_mtu, DATAPROTO_MAX_OVERHEAD, input, (PacketRouter_handler)source_router_handler, o, BReactor_PendingGroup(reactor))) {
  292. BLog(BLOG_ERROR, "PacketRouter_Init failed");
  293. goto fail0;
  294. }
  295. DebugCounter_Init(&o->d_ctr);
  296. DebugObject_Init(&o->d_obj);
  297. return 1;
  298. fail0:
  299. return 0;
  300. }
  301. void DataProtoSource_Free (DataProtoSource *o)
  302. {
  303. DebugObject_Free(&o->d_obj);
  304. DebugCounter_Free(&o->d_ctr);
  305. // free router
  306. PacketRouter_Free(&o->router);
  307. }
  308. int DataProtoFlow_Init (DataProtoFlow *o, DataProtoSource *source, peerid_t source_id, peerid_t dest_id, int num_packets, int inactivity_time, void *user,
  309. DataProtoFlow_handler_inactivity handler_inactivity)
  310. {
  311. DebugObject_Access(&source->d_obj);
  312. ASSERT(num_packets > 0)
  313. ASSERT(!(inactivity_time >= 0) || handler_inactivity)
  314. // init arguments
  315. o->source = source;
  316. o->source_id = source_id;
  317. o->dest_id = dest_id;
  318. // set no desired sink
  319. o->sink_desired = NULL;
  320. // allocate buffer structure
  321. struct DataProtoFlow_buffer *b = malloc(sizeof(*b));
  322. if (!b) {
  323. BLog(BLOG_ERROR, "malloc failed");
  324. goto fail0;
  325. }
  326. o->b = b;
  327. // set parent
  328. b->flow = o;
  329. // remember inactivity time
  330. b->inactivity_time = inactivity_time;
  331. // init connector
  332. PacketPassConnector_Init(&b->connector, DATAPROTO_MAX_OVERHEAD + source->frame_mtu, BReactor_PendingGroup(source->reactor));
  333. // init inactivity monitor
  334. PacketPassInterface *buf_out = PacketPassConnector_GetInput(&b->connector);
  335. if (b->inactivity_time >= 0) {
  336. PacketPassInactivityMonitor_Init(&b->monitor, buf_out, source->reactor, b->inactivity_time, handler_inactivity, user);
  337. buf_out = PacketPassInactivityMonitor_GetInput(&b->monitor);
  338. }
  339. // init route buffer
  340. if (!RouteBuffer_Init(&b->rbuf, DATAPROTO_MAX_OVERHEAD + source->frame_mtu, buf_out, num_packets)) {
  341. BLog(BLOG_ERROR, "RouteBuffer_Init failed");
  342. goto fail1;
  343. }
  344. // set no sink
  345. b->sink = NULL;
  346. DebugCounter_Increment(&source->d_ctr);
  347. DebugObject_Init(&o->d_obj);
  348. return 1;
  349. fail1:
  350. if (b->inactivity_time >= 0) {
  351. PacketPassInactivityMonitor_Free(&b->monitor);
  352. }
  353. PacketPassConnector_Free(&b->connector);
  354. free(b);
  355. fail0:
  356. return 0;
  357. }
  358. void DataProtoFlow_Free (DataProtoFlow *o)
  359. {
  360. DebugObject_Free(&o->d_obj);
  361. DebugCounter_Decrement(&o->source->d_ctr);
  362. ASSERT(!o->sink_desired)
  363. struct DataProtoFlow_buffer *b = o->b;
  364. if (b->sink) {
  365. if (PacketPassFairQueueFlow_IsBusy(&b->sink_qflow)) {
  366. // schedule detach, free buffer after detach
  367. flow_buffer_schedule_detach(b);
  368. b->flow = NULL;
  369. // remove inactivity handler
  370. if (b->inactivity_time >= 0) {
  371. PacketPassInactivityMonitor_SetHandler(&b->monitor, NULL, NULL);
  372. }
  373. } else {
  374. // detach and free buffer now
  375. flow_buffer_detach(b);
  376. flow_buffer_free(b);
  377. }
  378. } else {
  379. // free buffer
  380. flow_buffer_free(b);
  381. }
  382. }
  383. void DataProtoFlow_Route (DataProtoFlow *o, int more)
  384. {
  385. DebugObject_Access(&o->d_obj);
  386. PacketRouter_AssertRoute(&o->source->router);
  387. ASSERT(o->source->current_buf)
  388. ASSERT(more == 0 || more == 1)
  389. struct DataProtoFlow_buffer *b = o->b;
  390. // write header
  391. struct dataproto_header *header = (struct dataproto_header *)o->source->current_buf;
  392. // don't set flags, it will be set in notifier_handler
  393. header->from_id = htol16(o->source_id);
  394. header->num_peer_ids = htol16(1);
  395. struct dataproto_peer_id *id = (struct dataproto_peer_id *)(header + 1);
  396. id->id = htol16(o->dest_id);
  397. // route
  398. uint8_t *next_buf;
  399. if (!PacketRouter_Route(&o->source->router, DATAPROTO_MAX_OVERHEAD + o->source->current_recv_len, &b->rbuf,
  400. &next_buf, DATAPROTO_MAX_OVERHEAD, (more ? o->source->current_recv_len : 0)
  401. )) {
  402. BLog(BLOG_NOTICE, "buffer full: %d->%d", (int)o->source_id, (int)o->dest_id);
  403. return;
  404. }
  405. // remember next buffer, or don't allow further routing if more==0
  406. o->source->current_buf = (more ? next_buf : NULL);
  407. }
  408. void DataProtoFlow_Attach (DataProtoFlow *o, DataProtoSink *sink)
  409. {
  410. DebugObject_Access(&o->d_obj);
  411. DebugObject_Access(&sink->d_obj);
  412. ASSERT(!o->sink_desired)
  413. ASSERT(sink)
  414. ASSERT(o->source->frame_mtu <= sink->frame_mtu)
  415. struct DataProtoFlow_buffer *b = o->b;
  416. if (b->sink) {
  417. if (PacketPassFairQueueFlow_IsBusy(&b->sink_qflow)) {
  418. // schedule detach and reattach
  419. flow_buffer_schedule_detach(b);
  420. } else {
  421. // detach and reattach now
  422. flow_buffer_detach(b);
  423. flow_buffer_attach(b, sink);
  424. }
  425. } else {
  426. // attach
  427. flow_buffer_attach(b, sink);
  428. }
  429. // set desired sink
  430. o->sink_desired = sink;
  431. DebugCounter_Increment(&sink->d_ctr);
  432. }
  433. void DataProtoFlow_Detach (DataProtoFlow *o)
  434. {
  435. DebugObject_Access(&o->d_obj);
  436. ASSERT(o->sink_desired)
  437. struct DataProtoFlow_buffer *b = o->b;
  438. ASSERT(b->sink)
  439. DataProtoSink *sink = o->sink_desired;
  440. if (PacketPassFairQueueFlow_IsBusy(&b->sink_qflow)) {
  441. // schedule detach
  442. flow_buffer_schedule_detach(b);
  443. } else {
  444. // detach now
  445. flow_buffer_detach(b);
  446. }
  447. // set no desired sink
  448. o->sink_desired = NULL;
  449. DebugCounter_Decrement(&sink->d_ctr);
  450. }