DataProto.c 17 KB

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