1 /* SCTP kernel reference 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 reference Implementation
9 * These functions manipulate sctp tsn mapping array.
11 * The SCTP reference 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 * The SCTP reference 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/types.h>
46 #include <net/sctp/sctp.h>
47 #include <net/sctp/sm.h>
49 static void sctp_tsnmap_update(struct sctp_tsnmap *map);
50 static void sctp_tsnmap_find_gap_ack(__u8 *map, __u16 off,
51 __u16 len, __u16 base,
52 int *started, __u16 *start,
53 int *ended, __u16 *end);
55 /* Create a new sctp_tsnmap.
56 * Allocate room to store at least 'len' contiguous TSNs.
58 struct sctp_tsnmap *sctp_tsnmap_new(__u16 len, __u32 initial_tsn, int gfp)
60 struct sctp_tsnmap *retval;
62 retval = kmalloc(sizeof(struct sctp_tsnmap) +
63 sctp_tsnmap_storage_size(len), gfp);
67 if (!sctp_tsnmap_init(retval, len, initial_tsn))
78 /* Initialize a block of memory as a tsnmap. */
79 struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
82 map->tsn_map = map->raw_map;
83 map->overflow_map = map->tsn_map + len;
86 /* Clear out a TSN ack status. */
87 memset(map->tsn_map, 0x00, map->len + map->len);
89 /* Keep track of TSNs represented by tsn_map. */
90 map->base_tsn = initial_tsn;
91 map->overflow_tsn = initial_tsn + map->len;
92 map->cumulative_tsn_ack_point = initial_tsn - 1;
93 map->max_tsn_seen = map->cumulative_tsn_ack_point;
95 map->num_dup_tsns = 0;
100 /* Test the tracking state of this TSN.
102 * 0 if the TSN has not yet been seen
103 * >0 if the TSN has been seen (duplicate)
104 * <0 if the TSN is invalid (too large to track)
106 int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
111 /* Calculate the index into the mapping arrays. */
112 gap = tsn - map->base_tsn;
114 /* Verify that we can hold this TSN. */
115 if (gap >= (/* base */ map->len + /* overflow */ map->len)) {
120 /* Honk if we've already seen this TSN.
121 * We have three cases:
122 * 1. The TSN is ancient or belongs to a previous tsn_map.
123 * 2. The TSN is already marked in the tsn_map.
124 * 3. The TSN is already marked in the tsn_map_overflow.
127 (gap < map->len && map->tsn_map[gap]) ||
128 (gap >= map->len && map->overflow_map[gap - map->len]))
138 /* Mark this TSN as seen. */
139 void sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn)
143 /* Vacuously mark any TSN which precedes the map base or
144 * exceeds the end of the map.
146 if (TSN_lt(tsn, map->base_tsn))
148 if (!TSN_lt(tsn, map->base_tsn + map->len + map->len))
152 if (TSN_lt(map->max_tsn_seen, tsn))
153 map->max_tsn_seen = tsn;
155 /* Assert: TSN is in range. */
156 gap = tsn - map->base_tsn;
158 /* Mark the TSN as received. */
162 map->overflow_map[gap - map->len]++;
164 /* Go fixup any internal TSN mapping variables including
165 * cumulative_tsn_ack_point.
167 sctp_tsnmap_update(map);
171 /* Dispose of a tsnmap. */
172 void sctp_tsnmap_free(struct sctp_tsnmap *map)
178 /* Initialize a Gap Ack Block iterator from memory being provided. */
179 void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
180 struct sctp_tsnmap_iter *iter)
182 /* Only start looking one past the Cumulative TSN Ack Point. */
183 iter->start = map->cumulative_tsn_ack_point + 1;
186 /* Get the next Gap Ack Blocks. Returns 0 if there was not another block
189 int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
190 struct sctp_tsnmap_iter *iter, __u16 *start, __u16 *end)
193 __u16 _start, _end, offset;
195 /* We haven't found a gap yet. */
198 /* If there are no more gap acks possible, get out fast. */
199 if (TSN_lte(map->max_tsn_seen, iter->start))
202 /* Search the first mapping array. */
203 if (iter->start - map->base_tsn < map->len) {
205 offset = iter->start - map->base_tsn;
206 sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len, 0,
207 &started, &_start, &ended, &_end);
210 /* Do we need to check the overflow map? */
212 /* Fix up where we'd like to start searching in the
215 if (iter->start - map->base_tsn < map->len)
218 offset = iter->start - map->base_tsn - map->len;
220 /* Search the overflow map. */
221 sctp_tsnmap_find_gap_ack(map->overflow_map,
229 /* The Gap Ack Block happens to end at the end of the
232 if (started && !ended) {
234 _end = map->len + map->len - 1;
237 /* If we found a Gap Ack Block, return the start and end and
238 * bump the iterator forward.
241 /* Fix up the start and end based on the
242 * Cumulative TSN Ack offset into the map.
244 int gap = map->cumulative_tsn_ack_point -
247 *start = _start - gap;
250 /* Move the iterator forward. */
251 iter->start = map->cumulative_tsn_ack_point + *end + 1;
257 /* Mark this and any lower TSN as seen. */
258 void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
262 /* Vacuously mark any TSN which precedes the map base or
263 * exceeds the end of the map.
265 if (TSN_lt(tsn, map->base_tsn))
267 if (!TSN_lt(tsn, map->base_tsn + map->len + map->len))
271 if (TSN_lt(map->max_tsn_seen, tsn))
272 map->max_tsn_seen = tsn;
274 /* Assert: TSN is in range. */
275 gap = tsn - map->base_tsn + 1;
277 /* Mark the TSNs as received. */
279 memset(map->tsn_map, 0x01, gap);
281 memset(map->tsn_map, 0x01, map->len);
282 memset(map->overflow_map, 0x01, (gap - map->len));
285 /* Go fixup any internal TSN mapping variables including
286 * cumulative_tsn_ack_point.
288 sctp_tsnmap_update(map);
291 /********************************************************************
292 * 2nd Level Abstractions
293 ********************************************************************/
295 /* This private helper function updates the tsnmap buffers and
296 * the Cumulative TSN Ack Point.
298 static void sctp_tsnmap_update(struct sctp_tsnmap *map)
302 ctsn = map->cumulative_tsn_ack_point;
305 if (ctsn == map->overflow_tsn) {
306 /* Now tsn_map must have been all '1's,
307 * so we swap the map and check the overflow table
309 __u8 *tmp = map->tsn_map;
310 memset(tmp, 0, map->len);
311 map->tsn_map = map->overflow_map;
312 map->overflow_map = tmp;
314 /* Update the tsn_map boundaries. */
315 map->base_tsn += map->len;
316 map->overflow_tsn += map->len;
318 } while (map->tsn_map[ctsn - map->base_tsn]);
320 map->cumulative_tsn_ack_point = ctsn - 1; /* Back up one. */
323 /* How many data chunks are we missing from our peer?
325 __u16 sctp_tsnmap_pending(struct sctp_tsnmap *map)
327 __u32 cum_tsn = map->cumulative_tsn_ack_point;
328 __u32 max_tsn = map->max_tsn_seen;
329 __u32 base_tsn = map->base_tsn;
331 __s32 gap, start, end, i;
333 pending_data = max_tsn - cum_tsn;
334 gap = max_tsn - base_tsn;
336 if (gap <= 0 || gap >= (map->len + map->len))
339 start = ((cum_tsn >= base_tsn) ? (cum_tsn - base_tsn + 1) : 0);
340 end = ((gap > map->len ) ? map->len : gap + 1);
342 for (i = start; i < end; i++) {
347 if (gap >= map->len) {
349 end = gap - map->len + 1;
350 for (i = start; i < end; i++) {
351 if (map->overflow_map[i])
360 /* This is a private helper for finding Gap Ack Blocks. It searches a
361 * single array for the start and end of a Gap Ack Block.
363 * The flags "started" and "ended" tell is if we found the beginning
364 * or (respectively) the end of a Gap Ack Block.
366 static void sctp_tsnmap_find_gap_ack(__u8 *map, __u16 off,
367 __u16 len, __u16 base,
368 int *started, __u16 *start,
369 int *ended, __u16 *end)
373 /* Look through the entire array, but break out
374 * early if we have found the end of the Gap Ack Block.
377 /* Also, stop looking past the maximum TSN seen. */
379 /* Look for the start. */
381 for (; i < len; i++) {
390 /* Look for the end. */
392 /* We have found the start, let's find the
393 * end. If we find the end, break out.
395 for (; i < len; i++) {
405 /* Renege that we have seen a TSN. */
406 void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
410 if (TSN_lt(tsn, map->base_tsn))
412 if (!TSN_lt(tsn, map->base_tsn + map->len + map->len))
415 /* Assert: TSN is in range. */
416 gap = tsn - map->base_tsn;
418 /* Pretend we never saw the TSN. */
420 map->tsn_map[gap] = 0;
422 map->overflow_map[gap - map->len] = 0;
425 /* How many gap ack blocks do we have recorded? */
426 __u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map)
428 struct sctp_tsnmap_iter iter;
431 /* Refresh the gap ack information. */
432 if (sctp_tsnmap_has_gap(map)) {
433 sctp_tsnmap_iter_init(map, &iter);
434 while (sctp_tsnmap_next_gap_ack(map, &iter,
435 &map->gabs[gabs].start,
436 &map->gabs[gabs].end)) {
438 map->gabs[gabs].start = htons(map->gabs[gabs].start);
439 map->gabs[gabs].end = htons(map->gabs[gabs].end);
441 if (gabs >= SCTP_MAX_GABS)