tsnmap.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* SCTP kernel implementation
  2. * (C) Copyright IBM Corp. 2001, 2004
  3. * Copyright (c) 1999-2000 Cisco, Inc.
  4. * Copyright (c) 1999-2001 Motorola, Inc.
  5. * Copyright (c) 2001 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel implementation
  8. *
  9. * These functions manipulate sctp tsn mapping array.
  10. *
  11. * This SCTP implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This SCTP implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, see
  25. * <http://www.gnu.org/licenses/>.
  26. *
  27. * Please send any bug reports or fixes you make to the
  28. * email address(es):
  29. * lksctp developers <linux-sctp@vger.kernel.org>
  30. *
  31. * Written or modified by:
  32. * La Monte H.P. Yarroll <piggy@acm.org>
  33. * Jon Grimm <jgrimm@us.ibm.com>
  34. * Karl Knutson <karl@athena.chicago.il.us>
  35. * Sridhar Samudrala <sri@us.ibm.com>
  36. */
  37. #include <linux/slab.h>
  38. #include <linux/types.h>
  39. #include <linux/bitmap.h>
  40. #include <net/sctp/sctp.h>
  41. #include <net/sctp/sm.h>
  42. static void sctp_tsnmap_update(struct sctp_tsnmap *map);
  43. static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
  44. __u16 len, __u16 *start, __u16 *end);
  45. static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size);
  46. /* Initialize a block of memory as a tsnmap. */
  47. struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
  48. __u32 initial_tsn, gfp_t gfp)
  49. {
  50. if (!map->tsn_map) {
  51. map->tsn_map = kzalloc(len>>3, gfp);
  52. if (map->tsn_map == NULL)
  53. return NULL;
  54. map->len = len;
  55. } else {
  56. bitmap_zero(map->tsn_map, map->len);
  57. }
  58. /* Keep track of TSNs represented by tsn_map. */
  59. map->base_tsn = initial_tsn;
  60. map->cumulative_tsn_ack_point = initial_tsn - 1;
  61. map->max_tsn_seen = map->cumulative_tsn_ack_point;
  62. map->num_dup_tsns = 0;
  63. return map;
  64. }
  65. void sctp_tsnmap_free(struct sctp_tsnmap *map)
  66. {
  67. map->len = 0;
  68. kfree(map->tsn_map);
  69. }
  70. /* Test the tracking state of this TSN.
  71. * Returns:
  72. * 0 if the TSN has not yet been seen
  73. * >0 if the TSN has been seen (duplicate)
  74. * <0 if the TSN is invalid (too large to track)
  75. */
  76. int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
  77. {
  78. u32 gap;
  79. /* Check to see if this is an old TSN */
  80. if (TSN_lte(tsn, map->cumulative_tsn_ack_point))
  81. return 1;
  82. /* Verify that we can hold this TSN and that it will not
  83. * overlfow our map
  84. */
  85. if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
  86. return -1;
  87. /* Calculate the index into the mapping arrays. */
  88. gap = tsn - map->base_tsn;
  89. /* Check to see if TSN has already been recorded. */
  90. if (gap < map->len && test_bit(gap, map->tsn_map))
  91. return 1;
  92. else
  93. return 0;
  94. }
  95. /* Mark this TSN as seen. */
  96. int sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn,
  97. struct sctp_transport *trans)
  98. {
  99. u16 gap;
  100. if (TSN_lt(tsn, map->base_tsn))
  101. return 0;
  102. gap = tsn - map->base_tsn;
  103. if (gap >= map->len && !sctp_tsnmap_grow(map, gap + 1))
  104. return -ENOMEM;
  105. if (!sctp_tsnmap_has_gap(map) && gap == 0) {
  106. /* In this case the map has no gaps and the tsn we are
  107. * recording is the next expected tsn. We don't touch
  108. * the map but simply bump the values.
  109. */
  110. map->max_tsn_seen++;
  111. map->cumulative_tsn_ack_point++;
  112. if (trans)
  113. trans->sack_generation =
  114. trans->asoc->peer.sack_generation;
  115. map->base_tsn++;
  116. } else {
  117. /* Either we already have a gap, or about to record a gap, so
  118. * have work to do.
  119. *
  120. * Bump the max.
  121. */
  122. if (TSN_lt(map->max_tsn_seen, tsn))
  123. map->max_tsn_seen = tsn;
  124. /* Mark the TSN as received. */
  125. set_bit(gap, map->tsn_map);
  126. /* Go fixup any internal TSN mapping variables including
  127. * cumulative_tsn_ack_point.
  128. */
  129. sctp_tsnmap_update(map);
  130. }
  131. return 0;
  132. }
  133. /* Initialize a Gap Ack Block iterator from memory being provided. */
  134. static void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
  135. struct sctp_tsnmap_iter *iter)
  136. {
  137. /* Only start looking one past the Cumulative TSN Ack Point. */
  138. iter->start = map->cumulative_tsn_ack_point + 1;
  139. }
  140. /* Get the next Gap Ack Blocks. Returns 0 if there was not another block
  141. * to get.
  142. */
  143. static int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
  144. struct sctp_tsnmap_iter *iter,
  145. __u16 *start, __u16 *end)
  146. {
  147. int ended = 0;
  148. __u16 start_ = 0, end_ = 0, offset;
  149. /* If there are no more gap acks possible, get out fast. */
  150. if (TSN_lte(map->max_tsn_seen, iter->start))
  151. return 0;
  152. offset = iter->start - map->base_tsn;
  153. sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len,
  154. &start_, &end_);
  155. /* The Gap Ack Block happens to end at the end of the map. */
  156. if (start_ && !end_)
  157. end_ = map->len - 1;
  158. /* If we found a Gap Ack Block, return the start and end and
  159. * bump the iterator forward.
  160. */
  161. if (end_) {
  162. /* Fix up the start and end based on the
  163. * Cumulative TSN Ack which is always 1 behind base.
  164. */
  165. *start = start_ + 1;
  166. *end = end_ + 1;
  167. /* Move the iterator forward. */
  168. iter->start = map->cumulative_tsn_ack_point + *end + 1;
  169. ended = 1;
  170. }
  171. return ended;
  172. }
  173. /* Mark this and any lower TSN as seen. */
  174. void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
  175. {
  176. u32 gap;
  177. if (TSN_lt(tsn, map->base_tsn))
  178. return;
  179. if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
  180. return;
  181. /* Bump the max. */
  182. if (TSN_lt(map->max_tsn_seen, tsn))
  183. map->max_tsn_seen = tsn;
  184. gap = tsn - map->base_tsn + 1;
  185. map->base_tsn += gap;
  186. map->cumulative_tsn_ack_point += gap;
  187. if (gap >= map->len) {
  188. /* If our gap is larger then the map size, just
  189. * zero out the map.
  190. */
  191. bitmap_zero(map->tsn_map, map->len);
  192. } else {
  193. /* If the gap is smaller than the map size,
  194. * shift the map by 'gap' bits and update further.
  195. */
  196. bitmap_shift_right(map->tsn_map, map->tsn_map, gap, map->len);
  197. sctp_tsnmap_update(map);
  198. }
  199. }
  200. /********************************************************************
  201. * 2nd Level Abstractions
  202. ********************************************************************/
  203. /* This private helper function updates the tsnmap buffers and
  204. * the Cumulative TSN Ack Point.
  205. */
  206. static void sctp_tsnmap_update(struct sctp_tsnmap *map)
  207. {
  208. u16 len;
  209. unsigned long zero_bit;
  210. len = map->max_tsn_seen - map->cumulative_tsn_ack_point;
  211. zero_bit = find_first_zero_bit(map->tsn_map, len);
  212. if (!zero_bit)
  213. return; /* The first 0-bit is bit 0. nothing to do */
  214. map->base_tsn += zero_bit;
  215. map->cumulative_tsn_ack_point += zero_bit;
  216. bitmap_shift_right(map->tsn_map, map->tsn_map, zero_bit, map->len);
  217. }
  218. /* How many data chunks are we missing from our peer?
  219. */
  220. __u16 sctp_tsnmap_pending(struct sctp_tsnmap *map)
  221. {
  222. __u32 cum_tsn = map->cumulative_tsn_ack_point;
  223. __u32 max_tsn = map->max_tsn_seen;
  224. __u32 base_tsn = map->base_tsn;
  225. __u16 pending_data;
  226. u32 gap;
  227. pending_data = max_tsn - cum_tsn;
  228. gap = max_tsn - base_tsn;
  229. if (gap == 0 || gap >= map->len)
  230. goto out;
  231. pending_data -= bitmap_weight(map->tsn_map, gap + 1);
  232. out:
  233. return pending_data;
  234. }
  235. /* This is a private helper for finding Gap Ack Blocks. It searches a
  236. * single array for the start and end of a Gap Ack Block.
  237. *
  238. * The flags "started" and "ended" tell is if we found the beginning
  239. * or (respectively) the end of a Gap Ack Block.
  240. */
  241. static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
  242. __u16 len, __u16 *start, __u16 *end)
  243. {
  244. int i = off;
  245. /* Look through the entire array, but break out
  246. * early if we have found the end of the Gap Ack Block.
  247. */
  248. /* Also, stop looking past the maximum TSN seen. */
  249. /* Look for the start. */
  250. i = find_next_bit(map, len, off);
  251. if (i < len)
  252. *start = i;
  253. /* Look for the end. */
  254. if (*start) {
  255. /* We have found the start, let's find the
  256. * end. If we find the end, break out.
  257. */
  258. i = find_next_zero_bit(map, len, i);
  259. if (i < len)
  260. *end = i - 1;
  261. }
  262. }
  263. /* Renege that we have seen a TSN. */
  264. void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
  265. {
  266. u32 gap;
  267. if (TSN_lt(tsn, map->base_tsn))
  268. return;
  269. /* Assert: TSN is in range. */
  270. if (!TSN_lt(tsn, map->base_tsn + map->len))
  271. return;
  272. gap = tsn - map->base_tsn;
  273. /* Pretend we never saw the TSN. */
  274. clear_bit(gap, map->tsn_map);
  275. }
  276. /* How many gap ack blocks do we have recorded? */
  277. __u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map,
  278. struct sctp_gap_ack_block *gabs)
  279. {
  280. struct sctp_tsnmap_iter iter;
  281. int ngaps = 0;
  282. /* Refresh the gap ack information. */
  283. if (sctp_tsnmap_has_gap(map)) {
  284. __u16 start = 0, end = 0;
  285. sctp_tsnmap_iter_init(map, &iter);
  286. while (sctp_tsnmap_next_gap_ack(map, &iter,
  287. &start,
  288. &end)) {
  289. gabs[ngaps].start = htons(start);
  290. gabs[ngaps].end = htons(end);
  291. ngaps++;
  292. if (ngaps >= SCTP_MAX_GABS)
  293. break;
  294. }
  295. }
  296. return ngaps;
  297. }
  298. static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size)
  299. {
  300. unsigned long *new;
  301. unsigned long inc;
  302. u16 len;
  303. if (size > SCTP_TSN_MAP_SIZE)
  304. return 0;
  305. inc = ALIGN((size - map->len), BITS_PER_LONG) + SCTP_TSN_MAP_INCREMENT;
  306. len = min_t(u16, map->len + inc, SCTP_TSN_MAP_SIZE);
  307. new = kzalloc(len>>3, GFP_ATOMIC);
  308. if (!new)
  309. return 0;
  310. bitmap_copy(new, map->tsn_map,
  311. map->max_tsn_seen - map->cumulative_tsn_ack_point);
  312. kfree(map->tsn_map);
  313. map->tsn_map = new;
  314. map->len = len;
  315. return 1;
  316. }