fragmentation.c 16 KB

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