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