fragmentation.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2009-2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file src/fragmentation/fragmentation.c
  18. * @brief library to help fragment messages
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_fragmentation_lib.h"
  23. #include "gnunet_protocols.h"
  24. #include "fragmentation.h"
  25. /**
  26. * Absolute minimum delay we impose between sending and expecting ACK to arrive.
  27. */
  28. #define MIN_ACK_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1)
  29. /**
  30. * Fragmentation context.
  31. */
  32. struct GNUNET_FRAGMENT_Context
  33. {
  34. /**
  35. * Statistics to use.
  36. */
  37. struct GNUNET_STATISTICS_Handle *stats;
  38. /**
  39. * Tracker for flow control.
  40. */
  41. struct GNUNET_BANDWIDTH_Tracker *tracker;
  42. /**
  43. * Current expected delay for ACKs.
  44. */
  45. struct GNUNET_TIME_Relative ack_delay;
  46. /**
  47. * Current expected delay between messages.
  48. */
  49. struct GNUNET_TIME_Relative msg_delay;
  50. /**
  51. * Next allowed transmission time.
  52. */
  53. struct GNUNET_TIME_Absolute delay_until;
  54. /**
  55. * Time we transmitted the last message of the last round.
  56. */
  57. struct GNUNET_TIME_Absolute last_round;
  58. /**
  59. * Message to fragment (allocated at the end of this struct).
  60. */
  61. const struct GNUNET_MessageHeader *msg;
  62. /**
  63. * Function to call for transmissions.
  64. */
  65. GNUNET_FRAGMENT_MessageProcessor proc;
  66. /**
  67. * Closure for @e proc.
  68. */
  69. void *proc_cls;
  70. /**
  71. * Bitfield, set to 1 for each unacknowledged fragment.
  72. */
  73. uint64_t acks;
  74. /**
  75. * Bitfield with all possible bits for @e acks (used to mask the
  76. * ack we get back).
  77. */
  78. uint64_t acks_mask;
  79. /**
  80. * Task performing work for the fragmenter.
  81. */
  82. struct GNUNET_SCHEDULER_Task *task;
  83. /**
  84. * Our fragmentation ID. (chosen at random)
  85. */
  86. uint32_t fragment_id;
  87. /**
  88. * Round-robin selector for the next transmission.
  89. */
  90. unsigned int next_transmission;
  91. /**
  92. * How many rounds of transmission have we completed so far?
  93. */
  94. unsigned int num_rounds;
  95. /**
  96. * How many transmission have we completed in this round?
  97. */
  98. unsigned int num_transmissions;
  99. /**
  100. * #GNUNET_YES if we called @e proc and are now waiting for #GNUNET_FRAGMENT_context_transmission_done()
  101. */
  102. int8_t proc_busy;
  103. /**
  104. * #GNUNET_YES if we are waiting for an ACK.
  105. */
  106. int8_t wack;
  107. /**
  108. * Target fragment size.
  109. */
  110. uint16_t mtu;
  111. };
  112. /**
  113. * Convert an ACK message to a printable format suitable for logging.
  114. *
  115. * @param ack message to print
  116. * @return ack in human-readable format
  117. */
  118. const char *
  119. GNUNET_FRAGMENT_print_ack (const struct GNUNET_MessageHeader *ack)
  120. {
  121. static char buf[128];
  122. const struct FragmentAcknowledgement *fa;
  123. if (sizeof (struct FragmentAcknowledgement) !=
  124. htons (ack->size))
  125. return "<malformed ack>";
  126. fa = (const struct FragmentAcknowledgement *) ack;
  127. GNUNET_snprintf (buf,
  128. sizeof (buf),
  129. "%u-%llX",
  130. ntohl (fa->fragment_id),
  131. GNUNET_ntohll (fa->bits));
  132. return buf;
  133. }
  134. /**
  135. * Transmit the next fragment to the other peer.
  136. *
  137. * @param cls the `struct GNUNET_FRAGMENT_Context`
  138. */
  139. static void
  140. transmit_next (void *cls)
  141. {
  142. struct GNUNET_FRAGMENT_Context *fc = cls;
  143. char msg[fc->mtu];
  144. const char *mbuf;
  145. struct FragmentHeader *fh;
  146. struct GNUNET_TIME_Relative delay;
  147. unsigned int bit;
  148. size_t size;
  149. size_t fsize;
  150. int wrap;
  151. fc->task = NULL;
  152. GNUNET_assert (GNUNET_NO == fc->proc_busy);
  153. if (0 == fc->acks)
  154. return; /* all done */
  155. /* calculate delay */
  156. wrap = 0;
  157. while (0 == (fc->acks & (1LLU << fc->next_transmission)))
  158. {
  159. fc->next_transmission = (fc->next_transmission + 1) % 64;
  160. wrap |= (0 == fc->next_transmission);
  161. }
  162. bit = fc->next_transmission;
  163. size = ntohs (fc->msg->size);
  164. if (bit == size / (fc->mtu - sizeof (struct FragmentHeader)))
  165. fsize =
  166. (size % (fc->mtu - sizeof (struct FragmentHeader))) +
  167. sizeof (struct FragmentHeader);
  168. else
  169. fsize = fc->mtu;
  170. if (NULL != fc->tracker)
  171. delay = GNUNET_BANDWIDTH_tracker_get_delay (fc->tracker,
  172. fsize);
  173. else
  174. delay = GNUNET_TIME_UNIT_ZERO;
  175. if (delay.rel_value_us > 0)
  176. {
  177. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  178. "Fragmentation logic delays transmission of next fragment by %s\n",
  179. GNUNET_STRINGS_relative_time_to_string (delay,
  180. GNUNET_YES));
  181. fc->task = GNUNET_SCHEDULER_add_delayed (delay,
  182. &transmit_next,
  183. fc);
  184. return;
  185. }
  186. fc->next_transmission = (fc->next_transmission + 1) % 64;
  187. wrap |= (0 == fc->next_transmission);
  188. while (0 == (fc->acks & (1LLU << fc->next_transmission)))
  189. {
  190. fc->next_transmission = (fc->next_transmission + 1) % 64;
  191. wrap |= (0 == fc->next_transmission);
  192. }
  193. /* assemble fragmentation message */
  194. mbuf = (const char *) &fc[1];
  195. fh = (struct FragmentHeader *) msg;
  196. fh->header.size = htons (fsize);
  197. fh->header.type = htons (GNUNET_MESSAGE_TYPE_FRAGMENT);
  198. fh->fragment_id = htonl (fc->fragment_id);
  199. fh->total_size = fc->msg->size; /* already in big-endian */
  200. fh->offset = htons ((fc->mtu - sizeof (struct FragmentHeader)) * bit);
  201. GNUNET_memcpy (&fh[1], &mbuf[bit * (fc->mtu - sizeof (struct FragmentHeader))],
  202. fsize - sizeof (struct FragmentHeader));
  203. if (NULL != fc->tracker)
  204. GNUNET_BANDWIDTH_tracker_consume (fc->tracker, fsize);
  205. GNUNET_STATISTICS_update (fc->stats,
  206. _("# fragments transmitted"),
  207. 1,
  208. GNUNET_NO);
  209. if (0 != fc->last_round.abs_value_us)
  210. GNUNET_STATISTICS_update (fc->stats,
  211. _("# fragments retransmitted"),
  212. 1,
  213. GNUNET_NO);
  214. /* select next message to calculate delay */
  215. bit = fc->next_transmission;
  216. size = ntohs (fc->msg->size);
  217. if (bit == size / (fc->mtu - sizeof (struct FragmentHeader)))
  218. fsize = size % (fc->mtu - sizeof (struct FragmentHeader));
  219. else
  220. fsize = fc->mtu;
  221. if (NULL != fc->tracker)
  222. delay = GNUNET_BANDWIDTH_tracker_get_delay (fc->tracker,
  223. fsize);
  224. else
  225. delay = GNUNET_TIME_UNIT_ZERO;
  226. if (fc->num_rounds < 64)
  227. delay = GNUNET_TIME_relative_max (delay,
  228. GNUNET_TIME_relative_saturating_multiply
  229. (fc->msg_delay,
  230. (1ULL << fc->num_rounds)));
  231. else
  232. delay = GNUNET_TIME_UNIT_FOREVER_REL;
  233. if (wrap)
  234. {
  235. /* full round transmitted wait 2x delay for ACK before going again */
  236. fc->num_rounds++;
  237. delay = GNUNET_TIME_relative_saturating_multiply (fc->ack_delay, 2);
  238. /* never use zero, need some time for ACK always */
  239. delay = GNUNET_TIME_relative_max (MIN_ACK_DELAY, delay);
  240. fc->wack = GNUNET_YES;
  241. fc->last_round = GNUNET_TIME_absolute_get ();
  242. GNUNET_STATISTICS_update (fc->stats,
  243. _("# fragments wrap arounds"),
  244. 1,
  245. GNUNET_NO);
  246. }
  247. fc->proc_busy = GNUNET_YES;
  248. fc->delay_until = GNUNET_TIME_relative_to_absolute (delay);
  249. fc->num_transmissions++;
  250. fc->proc (fc->proc_cls,
  251. &fh->header);
  252. }
  253. /**
  254. * Create a fragmentation context for the given message.
  255. * Fragments the message into fragments of size @a mtu or
  256. * less. Calls @a proc on each un-acknowledged fragment,
  257. * using both the expected @a msg_delay between messages and
  258. * acknowledgements and the given @a tracker to guide the
  259. * frequency of calls to @a proc.
  260. *
  261. * @param stats statistics context
  262. * @param mtu the maximum message size for each fragment
  263. * @param tracker bandwidth tracker to use for flow control (can be NULL)
  264. * @param msg_delay initial delay to insert between fragment transmissions
  265. * based on previous messages
  266. * @param ack_delay expected delay between fragment transmission
  267. * and ACK based on previous messages
  268. * @param msg the message to fragment
  269. * @param proc function to call for each fragment to transmit
  270. * @param proc_cls closure for @a proc
  271. * @return the fragmentation context
  272. */
  273. struct GNUNET_FRAGMENT_Context *
  274. GNUNET_FRAGMENT_context_create (struct GNUNET_STATISTICS_Handle *stats,
  275. uint16_t mtu,
  276. struct GNUNET_BANDWIDTH_Tracker *tracker,
  277. struct GNUNET_TIME_Relative msg_delay,
  278. struct GNUNET_TIME_Relative ack_delay,
  279. const struct GNUNET_MessageHeader *msg,
  280. GNUNET_FRAGMENT_MessageProcessor proc,
  281. void *proc_cls)
  282. {
  283. struct GNUNET_FRAGMENT_Context *fc;
  284. size_t size;
  285. uint64_t bits;
  286. GNUNET_STATISTICS_update (stats,
  287. _("# messages fragmented"),
  288. 1,
  289. GNUNET_NO);
  290. GNUNET_assert (mtu >= 1024 + sizeof (struct FragmentHeader));
  291. size = ntohs (msg->size);
  292. GNUNET_STATISTICS_update (stats,
  293. _("# total size of fragmented messages"),
  294. size, GNUNET_NO);
  295. GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
  296. fc = GNUNET_malloc (sizeof (struct GNUNET_FRAGMENT_Context) + size);
  297. fc->stats = stats;
  298. fc->mtu = mtu;
  299. fc->tracker = tracker;
  300. fc->ack_delay = ack_delay;
  301. fc->msg_delay = msg_delay;
  302. fc->msg = (const struct GNUNET_MessageHeader *) &fc[1];
  303. fc->proc = proc;
  304. fc->proc_cls = proc_cls;
  305. fc->fragment_id =
  306. GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  307. UINT32_MAX);
  308. GNUNET_memcpy (&fc[1], msg, size);
  309. bits =
  310. (size + mtu - sizeof (struct FragmentHeader) - 1) / (mtu -
  311. sizeof (struct
  312. FragmentHeader));
  313. GNUNET_assert (bits <= 64);
  314. if (bits == 64)
  315. fc->acks_mask = UINT64_MAX; /* set all 64 bit */
  316. else
  317. fc->acks_mask = (1LLU << bits) - 1; /* set lowest 'bits' bit */
  318. fc->acks = fc->acks_mask;
  319. fc->task = GNUNET_SCHEDULER_add_now (&transmit_next, fc);
  320. return fc;
  321. }
  322. /**
  323. * Continuation to call from the 'proc' function after the fragment
  324. * has been transmitted (and hence the next fragment can now be
  325. * given to proc).
  326. *
  327. * @param fc fragmentation context
  328. */
  329. void
  330. GNUNET_FRAGMENT_context_transmission_done (struct GNUNET_FRAGMENT_Context *fc)
  331. {
  332. GNUNET_assert (fc->proc_busy == GNUNET_YES);
  333. fc->proc_busy = GNUNET_NO;
  334. GNUNET_assert (fc->task == NULL);
  335. fc->task =
  336. GNUNET_SCHEDULER_add_at (fc->delay_until,
  337. &transmit_next,
  338. fc);
  339. }
  340. /**
  341. * Process an acknowledgement message we got from the other
  342. * side (to control re-transmits).
  343. *
  344. * @param fc fragmentation context
  345. * @param msg acknowledgement message we received
  346. * @return #GNUNET_OK if this ack completes the work of the 'fc'
  347. * (all fragments have been received);
  348. * #GNUNET_NO if more messages are pending
  349. * #GNUNET_SYSERR if this ack is not valid for this fc
  350. */
  351. int
  352. GNUNET_FRAGMENT_process_ack (struct GNUNET_FRAGMENT_Context *fc,
  353. const struct GNUNET_MessageHeader *msg)
  354. {
  355. const struct FragmentAcknowledgement *fa;
  356. uint64_t abits;
  357. struct GNUNET_TIME_Relative ndelay;
  358. unsigned int ack_cnt;
  359. unsigned int snd_cnt;
  360. unsigned int i;
  361. if (sizeof (struct FragmentAcknowledgement) != ntohs (msg->size))
  362. {
  363. GNUNET_break_op (0);
  364. return GNUNET_SYSERR;
  365. }
  366. fa = (const struct FragmentAcknowledgement *) msg;
  367. if (ntohl (fa->fragment_id) != fc->fragment_id)
  368. return GNUNET_SYSERR; /* not our ACK */
  369. abits = GNUNET_ntohll (fa->bits);
  370. if ( (GNUNET_YES == fc->wack) &&
  371. (0 != fc->num_transmissions) )
  372. {
  373. /* normal ACK, can update running average of delay... */
  374. fc->wack = GNUNET_NO;
  375. ndelay = GNUNET_TIME_absolute_get_duration (fc->last_round);
  376. fc->ack_delay.rel_value_us =
  377. (ndelay.rel_value_us / fc->num_transmissions + 3 * fc->ack_delay.rel_value_us) / 4;
  378. /* calculate ratio msg sent vs. msg acked */
  379. ack_cnt = 0;
  380. snd_cnt = 0;
  381. for (i=0;i<64;i++)
  382. {
  383. if (1 == (fc->acks_mask & (1ULL << i)))
  384. {
  385. snd_cnt++;
  386. if (0 == (abits & (1ULL << i)))
  387. ack_cnt++;
  388. }
  389. }
  390. if (0 == ack_cnt)
  391. {
  392. /* complete loss */
  393. fc->msg_delay = GNUNET_TIME_relative_saturating_multiply (fc->msg_delay,
  394. snd_cnt);
  395. }
  396. else if (snd_cnt > ack_cnt)
  397. {
  398. /* some loss, slow down proportionally */
  399. fc->msg_delay.rel_value_us = ((fc->msg_delay.rel_value_us * ack_cnt) / snd_cnt);
  400. }
  401. else if (snd_cnt == ack_cnt)
  402. {
  403. fc->msg_delay.rel_value_us =
  404. (ndelay.rel_value_us / fc->num_transmissions + 3 * fc->msg_delay.rel_value_us) / 5;
  405. }
  406. fc->num_transmissions = 0;
  407. fc->msg_delay = GNUNET_TIME_relative_min (fc->msg_delay,
  408. GNUNET_TIME_UNIT_SECONDS);
  409. fc->ack_delay = GNUNET_TIME_relative_min (fc->ack_delay,
  410. GNUNET_TIME_UNIT_SECONDS);
  411. }
  412. GNUNET_STATISTICS_update (fc->stats,
  413. _("# fragment acknowledgements received"),
  414. 1,
  415. GNUNET_NO);
  416. if (abits != (fc->acks & abits))
  417. {
  418. /* ID collission or message reordering, count! This should be rare! */
  419. GNUNET_STATISTICS_update (fc->stats,
  420. _("# bits removed from fragmentation ACKs"), 1,
  421. GNUNET_NO);
  422. }
  423. fc->acks = abits & fc->acks_mask;
  424. if (0 != fc->acks)
  425. {
  426. /* more to transmit, do so right now (if tracker permits...) */
  427. if (fc->task != NULL)
  428. {
  429. /* schedule next transmission now, no point in waiting... */
  430. GNUNET_SCHEDULER_cancel (fc->task);
  431. fc->task = GNUNET_SCHEDULER_add_now (&transmit_next, fc);
  432. }
  433. else
  434. {
  435. /* only case where there is no task should be if we're waiting
  436. * for the right to transmit again (proc_busy set to YES) */
  437. GNUNET_assert (GNUNET_YES == fc->proc_busy);
  438. }
  439. return GNUNET_NO;
  440. }
  441. /* all done */
  442. GNUNET_STATISTICS_update (fc->stats,
  443. _("# fragmentation transmissions completed"),
  444. 1,
  445. GNUNET_NO);
  446. if (NULL != fc->task)
  447. {
  448. GNUNET_SCHEDULER_cancel (fc->task);
  449. fc->task = NULL;
  450. }
  451. return GNUNET_OK;
  452. }
  453. /**
  454. * Destroy the given fragmentation context (stop calling 'proc', free
  455. * resources).
  456. *
  457. * @param fc fragmentation context
  458. * @param msg_delay where to store average delay between individual message transmissions the
  459. * last message (OUT only)
  460. * @param ack_delay where to store average delay between transmission and ACK for the
  461. * last message, set to FOREVER if the message was not fully transmitted (OUT only)
  462. */
  463. void
  464. GNUNET_FRAGMENT_context_destroy (struct GNUNET_FRAGMENT_Context *fc,
  465. struct GNUNET_TIME_Relative *msg_delay,
  466. struct GNUNET_TIME_Relative *ack_delay)
  467. {
  468. if (fc->task != NULL)
  469. GNUNET_SCHEDULER_cancel (fc->task);
  470. if (NULL != ack_delay)
  471. *ack_delay = fc->ack_delay;
  472. if (NULL != msg_delay)
  473. *msg_delay = GNUNET_TIME_relative_saturating_multiply (fc->msg_delay,
  474. fc->num_rounds);
  475. GNUNET_free (fc);
  476. }
  477. /* end of fragmentation.c */