tsnmap.c 9.8 KB

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