Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6/openmoko-kernel/knife-kernel.git] / net / sctp / tsnmap.c
blobd3192a1babccb96645c8f3d54db2621a62073fc1
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)
15 * any later version.
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
29 * email address(es):
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 /* Initialize a block of memory as a tsnmap. */
56 struct sctp_tsnmap *sctp_tsnmap_init(struct sctp_tsnmap *map, __u16 len,
57 __u32 initial_tsn)
59 map->tsn_map = map->raw_map;
60 map->overflow_map = map->tsn_map + len;
61 map->len = len;
63 /* Clear out a TSN ack status. */
64 memset(map->tsn_map, 0x00, map->len + map->len);
66 /* Keep track of TSNs represented by tsn_map. */
67 map->base_tsn = initial_tsn;
68 map->overflow_tsn = initial_tsn + map->len;
69 map->cumulative_tsn_ack_point = initial_tsn - 1;
70 map->max_tsn_seen = map->cumulative_tsn_ack_point;
71 map->malloced = 0;
72 map->num_dup_tsns = 0;
74 return map;
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)
83 int sctp_tsnmap_check(const struct sctp_tsnmap *map, __u32 tsn)
85 __s32 gap;
86 int dup;
88 /* Calculate the index into the mapping arrays. */
89 gap = tsn - map->base_tsn;
91 /* Verify that we can hold this TSN. */
92 if (gap >= (/* base */ map->len + /* overflow */ map->len)) {
93 dup = -1;
94 goto out;
97 /* Honk if we've already seen this TSN.
98 * We have three cases:
99 * 1. The TSN is ancient or belongs to a previous tsn_map.
100 * 2. The TSN is already marked in the tsn_map.
101 * 3. The TSN is already marked in the tsn_map_overflow.
103 if (gap < 0 ||
104 (gap < map->len && map->tsn_map[gap]) ||
105 (gap >= map->len && map->overflow_map[gap - map->len]))
106 dup = 1;
107 else
108 dup = 0;
110 out:
111 return dup;
115 /* Mark this TSN as seen. */
116 void sctp_tsnmap_mark(struct sctp_tsnmap *map, __u32 tsn)
118 __s32 gap;
120 /* Vacuously mark any TSN which precedes the map base or
121 * exceeds the end of the map.
123 if (TSN_lt(tsn, map->base_tsn))
124 return;
125 if (!TSN_lt(tsn, map->base_tsn + map->len + map->len))
126 return;
128 /* Bump the max. */
129 if (TSN_lt(map->max_tsn_seen, tsn))
130 map->max_tsn_seen = tsn;
132 /* Assert: TSN is in range. */
133 gap = tsn - map->base_tsn;
135 /* Mark the TSN as received. */
136 if (gap < map->len)
137 map->tsn_map[gap]++;
138 else
139 map->overflow_map[gap - map->len]++;
141 /* Go fixup any internal TSN mapping variables including
142 * cumulative_tsn_ack_point.
144 sctp_tsnmap_update(map);
148 /* Initialize a Gap Ack Block iterator from memory being provided. */
149 SCTP_STATIC void sctp_tsnmap_iter_init(const struct sctp_tsnmap *map,
150 struct sctp_tsnmap_iter *iter)
152 /* Only start looking one past the Cumulative TSN Ack Point. */
153 iter->start = map->cumulative_tsn_ack_point + 1;
156 /* Get the next Gap Ack Blocks. Returns 0 if there was not another block
157 * to get.
159 SCTP_STATIC int sctp_tsnmap_next_gap_ack(const struct sctp_tsnmap *map,
160 struct sctp_tsnmap_iter *iter,
161 __u16 *start, __u16 *end)
163 int started, ended;
164 __u16 _start, _end, offset;
166 /* We haven't found a gap yet. */
167 started = ended = 0;
169 /* If there are no more gap acks possible, get out fast. */
170 if (TSN_lte(map->max_tsn_seen, iter->start))
171 return 0;
173 /* Search the first mapping array. */
174 if (iter->start - map->base_tsn < map->len) {
176 offset = iter->start - map->base_tsn;
177 sctp_tsnmap_find_gap_ack(map->tsn_map, offset, map->len, 0,
178 &started, &_start, &ended, &_end);
181 /* Do we need to check the overflow map? */
182 if (!ended) {
183 /* Fix up where we'd like to start searching in the
184 * overflow map.
186 if (iter->start - map->base_tsn < map->len)
187 offset = 0;
188 else
189 offset = iter->start - map->base_tsn - map->len;
191 /* Search the overflow map. */
192 sctp_tsnmap_find_gap_ack(map->overflow_map,
193 offset,
194 map->len,
195 map->len,
196 &started, &_start,
197 &ended, &_end);
200 /* The Gap Ack Block happens to end at the end of the
201 * overflow map.
203 if (started && !ended) {
204 ended++;
205 _end = map->len + map->len - 1;
208 /* If we found a Gap Ack Block, return the start and end and
209 * bump the iterator forward.
211 if (ended) {
212 /* Fix up the start and end based on the
213 * Cumulative TSN Ack offset into the map.
215 int gap = map->cumulative_tsn_ack_point -
216 map->base_tsn;
218 *start = _start - gap;
219 *end = _end - gap;
221 /* Move the iterator forward. */
222 iter->start = map->cumulative_tsn_ack_point + *end + 1;
225 return ended;
228 /* Mark this and any lower TSN as seen. */
229 void sctp_tsnmap_skip(struct sctp_tsnmap *map, __u32 tsn)
231 __s32 gap;
233 /* Vacuously mark any TSN which precedes the map base or
234 * exceeds the end of the map.
236 if (TSN_lt(tsn, map->base_tsn))
237 return;
238 if (!TSN_lt(tsn, map->base_tsn + map->len + map->len))
239 return;
241 /* Bump the max. */
242 if (TSN_lt(map->max_tsn_seen, tsn))
243 map->max_tsn_seen = tsn;
245 /* Assert: TSN is in range. */
246 gap = tsn - map->base_tsn + 1;
248 /* Mark the TSNs as received. */
249 if (gap <= map->len)
250 memset(map->tsn_map, 0x01, gap);
251 else {
252 memset(map->tsn_map, 0x01, map->len);
253 memset(map->overflow_map, 0x01, (gap - map->len));
256 /* Go fixup any internal TSN mapping variables including
257 * cumulative_tsn_ack_point.
259 sctp_tsnmap_update(map);
262 /********************************************************************
263 * 2nd Level Abstractions
264 ********************************************************************/
266 /* This private helper function updates the tsnmap buffers and
267 * the Cumulative TSN Ack Point.
269 static void sctp_tsnmap_update(struct sctp_tsnmap *map)
271 __u32 ctsn;
273 ctsn = map->cumulative_tsn_ack_point;
274 do {
275 ctsn++;
276 if (ctsn == map->overflow_tsn) {
277 /* Now tsn_map must have been all '1's,
278 * so we swap the map and check the overflow table
280 __u8 *tmp = map->tsn_map;
281 memset(tmp, 0, map->len);
282 map->tsn_map = map->overflow_map;
283 map->overflow_map = tmp;
285 /* Update the tsn_map boundaries. */
286 map->base_tsn += map->len;
287 map->overflow_tsn += map->len;
289 } while (map->tsn_map[ctsn - map->base_tsn]);
291 map->cumulative_tsn_ack_point = ctsn - 1; /* Back up one. */
294 /* How many data chunks are we missing from our peer?
296 __u16 sctp_tsnmap_pending(struct sctp_tsnmap *map)
298 __u32 cum_tsn = map->cumulative_tsn_ack_point;
299 __u32 max_tsn = map->max_tsn_seen;
300 __u32 base_tsn = map->base_tsn;
301 __u16 pending_data;
302 __s32 gap, start, end, i;
304 pending_data = max_tsn - cum_tsn;
305 gap = max_tsn - base_tsn;
307 if (gap <= 0 || gap >= (map->len + map->len))
308 goto out;
310 start = ((cum_tsn >= base_tsn) ? (cum_tsn - base_tsn + 1) : 0);
311 end = ((gap > map->len ) ? map->len : gap + 1);
313 for (i = start; i < end; i++) {
314 if (map->tsn_map[i])
315 pending_data--;
318 if (gap >= map->len) {
319 start = 0;
320 end = gap - map->len + 1;
321 for (i = start; i < end; i++) {
322 if (map->overflow_map[i])
323 pending_data--;
327 out:
328 return pending_data;
331 /* This is a private helper for finding Gap Ack Blocks. It searches a
332 * single array for the start and end of a Gap Ack Block.
334 * The flags "started" and "ended" tell is if we found the beginning
335 * or (respectively) the end of a Gap Ack Block.
337 static void sctp_tsnmap_find_gap_ack(__u8 *map, __u16 off,
338 __u16 len, __u16 base,
339 int *started, __u16 *start,
340 int *ended, __u16 *end)
342 int i = off;
344 /* Look through the entire array, but break out
345 * early if we have found the end of the Gap Ack Block.
348 /* Also, stop looking past the maximum TSN seen. */
350 /* Look for the start. */
351 if (!(*started)) {
352 for (; i < len; i++) {
353 if (map[i]) {
354 (*started)++;
355 *start = base + i;
356 break;
361 /* Look for the end. */
362 if (*started) {
363 /* We have found the start, let's find the
364 * end. If we find the end, break out.
366 for (; i < len; i++) {
367 if (!map[i]) {
368 (*ended)++;
369 *end = base + i - 1;
370 break;
376 /* Renege that we have seen a TSN. */
377 void sctp_tsnmap_renege(struct sctp_tsnmap *map, __u32 tsn)
379 __s32 gap;
381 if (TSN_lt(tsn, map->base_tsn))
382 return;
383 if (!TSN_lt(tsn, map->base_tsn + map->len + map->len))
384 return;
386 /* Assert: TSN is in range. */
387 gap = tsn - map->base_tsn;
389 /* Pretend we never saw the TSN. */
390 if (gap < map->len)
391 map->tsn_map[gap] = 0;
392 else
393 map->overflow_map[gap - map->len] = 0;
396 /* How many gap ack blocks do we have recorded? */
397 __u16 sctp_tsnmap_num_gabs(struct sctp_tsnmap *map)
399 struct sctp_tsnmap_iter iter;
400 int gabs = 0;
402 /* Refresh the gap ack information. */
403 if (sctp_tsnmap_has_gap(map)) {
404 __u16 start, end;
405 sctp_tsnmap_iter_init(map, &iter);
406 while (sctp_tsnmap_next_gap_ack(map, &iter,
407 &start,
408 &end)) {
410 map->gabs[gabs].start = htons(start);
411 map->gabs[gabs].end = htons(end);
412 gabs++;
413 if (gabs >= SCTP_MAX_GABS)
414 break;
417 return gabs;