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.
7 * This file is part of the SCTP kernel implementation
9 * These functions manipulate sctp tsn mapping array.
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)
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.
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.
28 * Please send any bug reports or fixes you make to the
30 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 * Or submit a bug report through the following website:
33 * http://www.sf.net/projects/lksctp
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>
41 * Any bugs reported given to us we will try to fix... any fixes shared will
42 * be incorporated into the next SCTP release.
45 #include <linux/slab.h>
46 #include <linux/types.h>
47 #include <linux/bitmap.h>
48 #include <net/sctp/sctp.h>
49 #include <net/sctp/sm.h>
51 static void sctp_tsnmap_update(struct sctp_tsnmap *map);
52 static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
53 __u16 len, __u16 *start, __u16 *end);
54 static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size);
56 /* Initialize a block of memory as a tsnmap. */
57 struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
58 __u32 initial_tsn, gfp_t gfp)
61 map->tsn_map = kzalloc(len>>3, gfp);
62 if (map->tsn_map == NULL)
67 bitmap_zero(map->tsn_map, map->len);
70 /* Keep track of TSNs represented by tsn_map. */
71 map->base_tsn = initial_tsn;
72 map->cumulative_tsn_ack_point = initial_tsn - 1;
73 map->max_tsn_seen = map->cumulative_tsn_ack_point;
74 map->num_dup_tsns = 0;
79 void sctp_tsnmap_free(struct sctp_tsnmap *map)
85 /* Test the tracking state of this TSN.
87 * 0 if the TSN has not yet been seen
88 * >0 if the TSN has been seen (duplicate)
89 * <0 if the TSN is invalid (too large to track)
91 int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
95 /* Check to see if this is an old TSN */
96 if (TSN_lte(tsn, map->cumulative_tsn_ack_point))
99 /* Verify that we can hold this TSN and that it will not
102 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
105 /* Calculate the index into the mapping arrays. */
106 gap = tsn - map->base_tsn;
108 /* Check to see if TSN has already been recorded. */
109 if (gap < map->len && test_bit(gap, map->tsn_map))
116 /* Mark this TSN as seen. */
117 int sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn,
118 struct sctp_transport *trans)
122 if (TSN_lt(tsn, map->base_tsn))
125 gap = tsn - map->base_tsn;
127 if (gap >= map->len && !sctp_tsnmap_grow(map, gap + 1))
130 if (!sctp_tsnmap_has_gap(map) && gap == 0) {
131 /* In this case the map has no gaps and the tsn we are
132 * recording is the next expected tsn. We don't touch
133 * the map but simply bump the values.
136 map->cumulative_tsn_ack_point++;
138 trans->sack_generation =
139 trans->asoc->peer.sack_generation;
142 /* Either we already have a gap, or about to record a gap, so
147 if (TSN_lt(map->max_tsn_seen, tsn))
148 map->max_tsn_seen = tsn;
150 /* Mark the TSN as received. */
151 set_bit(gap, map->tsn_map);
153 /* Go fixup any internal TSN mapping variables including
154 * cumulative_tsn_ack_point.
156 sctp_tsnmap_update(map);
163 /* Initialize a Gap Ack Block iterator from memory being provided. */
164 SCTP_STATIC void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
165 struct sctp_tsnmap_iter *iter)
167 /* Only start looking one past the Cumulative TSN Ack Point. */
168 iter->start = map->cumulative_tsn_ack_point + 1;
171 /* Get the next Gap Ack Blocks. Returns 0 if there was not another block
174 SCTP_STATIC int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
175 struct sctp_tsnmap_iter *iter,
176 __u16 *start, __u16 *end)
179 __u16 start_ = 0, end_ = 0, offset;
181 /* If there are no more gap acks possible, get out fast. */
182 if (TSN_lte(map->max_tsn_seen, iter->start))
185 offset = iter->start - map->base_tsn;
186 sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len,
189 /* The Gap Ack Block happens to end at the end of the map. */
193 /* If we found a Gap Ack Block, return the start and end and
194 * bump the iterator forward.
197 /* Fix up the start and end based on the
198 * Cumulative TSN Ack which is always 1 behind base.
203 /* Move the iterator forward. */
204 iter->start = map->cumulative_tsn_ack_point + *end + 1;
211 /* Mark this and any lower TSN as seen. */
212 void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
216 if (TSN_lt(tsn, map->base_tsn))
218 if (!TSN_lt(tsn, map->base_tsn + SCTP_TSN_MAP_SIZE))
222 if (TSN_lt(map->max_tsn_seen, tsn))
223 map->max_tsn_seen = tsn;
225 gap = tsn - map->base_tsn + 1;
227 map->base_tsn += gap;
228 map->cumulative_tsn_ack_point += gap;
229 if (gap >= map->len) {
230 /* If our gap is larger then the map size, just
233 bitmap_zero(map->tsn_map, map->len);
235 /* If the gap is smaller than the map size,
236 * shift the map by 'gap' bits and update further.
238 bitmap_shift_right(map->tsn_map, map->tsn_map, gap, map->len);
239 sctp_tsnmap_update(map);
243 /********************************************************************
244 * 2nd Level Abstractions
245 ********************************************************************/
247 /* This private helper function updates the tsnmap buffers and
248 * the Cumulative TSN Ack Point.
250 static void sctp_tsnmap_update(struct sctp_tsnmap *map)
253 unsigned long zero_bit;
256 len = map->max_tsn_seen - map->cumulative_tsn_ack_point;
257 zero_bit = find_first_zero_bit(map->tsn_map, len);
259 return; /* The first 0-bit is bit 0. nothing to do */
261 map->base_tsn += zero_bit;
262 map->cumulative_tsn_ack_point += zero_bit;
264 bitmap_shift_right(map->tsn_map, map->tsn_map, zero_bit, map->len);
267 /* How many data chunks are we missing from our peer?
269 __u16 sctp_tsnmap_pending(struct sctp_tsnmap *map)
271 __u32 cum_tsn = map->cumulative_tsn_ack_point;
272 __u32 max_tsn = map->max_tsn_seen;
273 __u32 base_tsn = map->base_tsn;
277 pending_data = max_tsn - cum_tsn;
278 gap = max_tsn - base_tsn;
280 if (gap == 0 || gap >= map->len)
283 pending_data -= bitmap_weight(map->tsn_map, gap + 1);
288 /* This is a private helper for finding Gap Ack Blocks. It searches a
289 * single array for the start and end of a Gap Ack Block.
291 * The flags "started" and "ended" tell is if we found the beginning
292 * or (respectively) the end of a Gap Ack Block.
294 static void sctp_tsnmap_find_gap_ack(unsigned long *map, __u16 off,
295 __u16 len, __u16 *start, __u16 *end)
299 /* Look through the entire array, but break out
300 * early if we have found the end of the Gap Ack Block.
303 /* Also, stop looking past the maximum TSN seen. */
305 /* Look for the start. */
306 i = find_next_bit(map, len, off);
310 /* Look for the end. */
312 /* We have found the start, let's find the
313 * end. If we find the end, break out.
315 i = find_next_zero_bit(map, len, i);
321 /* Renege that we have seen a TSN. */
322 void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
326 if (TSN_lt(tsn, map->base_tsn))
328 /* Assert: TSN is in range. */
329 if (!TSN_lt(tsn, map->base_tsn + map->len))
332 gap = tsn - map->base_tsn;
334 /* Pretend we never saw the TSN. */
335 clear_bit(gap, map->tsn_map);
338 /* How many gap ack blocks do we have recorded? */
339 __u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map,
340 struct sctp_gap_ack_block *gabs)
342 struct sctp_tsnmap_iter iter;
345 /* Refresh the gap ack information. */
346 if (sctp_tsnmap_has_gap(map)) {
347 __u16 start = 0, end = 0;
348 sctp_tsnmap_iter_init(map, &iter);
349 while (sctp_tsnmap_next_gap_ack(map, &iter,
353 gabs[ngaps].start = htons(start);
354 gabs[ngaps].end = htons(end);
356 if (ngaps >= SCTP_MAX_GABS)
363 static int sctp_tsnmap_grow(struct sctp_tsnmap *map, u16 size)
369 if (size > SCTP_TSN_MAP_SIZE)
372 inc = ALIGN((size - map->len), BITS_PER_LONG) + SCTP_TSN_MAP_INCREMENT;
373 len = min_t(u16, map->len + inc, SCTP_TSN_MAP_SIZE);
375 new = kzalloc(len>>3, GFP_ATOMIC);
379 bitmap_copy(new, map->tsn_map,
380 map->max_tsn_seen - map->cumulative_tsn_ack_point);