blob: 93a30ae8d07a67747b2e552a3b5385e02b159ca1 [file] [log] [blame]
Andrea Bittau2a91aa32006-03-20 17:41:47 -08001/*
2 * net/dccp/ccids/ccid2.c
3 *
4 * Copyright (c) 2005, 2006 Andrea Bittau <a.bittau@cs.ucl.ac.uk>
5 *
6 * Changes to meet Linux coding standards, and DCCP infrastructure fixes.
7 *
8 * Copyright (c) 2006 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25/*
26 * This implementation should follow: draft-ietf-dccp-ccid2-10.txt
27 *
28 * BUGS:
29 * - sequence number wrapping
Andrea Bittau2a91aa32006-03-20 17:41:47 -080030 */
31
Andrea Bittau2a91aa32006-03-20 17:41:47 -080032#include "../ccid.h"
33#include "../dccp.h"
34#include "ccid2.h"
35
36static int ccid2_debug;
37
Andrea Bittau8d424f62006-09-19 13:12:44 -070038#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
Andrea Bittau2a91aa32006-03-20 17:41:47 -080039#define ccid2_pr_debug(format, a...) \
40 do { if (ccid2_debug) \
41 printk(KERN_DEBUG "%s: " format, __FUNCTION__, ##a); \
42 } while (0)
43#else
44#define ccid2_pr_debug(format, a...)
45#endif
46
Andrea Bittau8d424f62006-09-19 13:12:44 -070047#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
Andrea Bittau2a91aa32006-03-20 17:41:47 -080048static void ccid2_hc_tx_check_sanity(const struct ccid2_hc_tx_sock *hctx)
49{
50 int len = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -080051 int pipe = 0;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -080052 struct ccid2_seq *seqp = hctx->ccid2hctx_seqh;
Andrea Bittau2a91aa32006-03-20 17:41:47 -080053
54 /* there is data in the chain */
55 if (seqp != hctx->ccid2hctx_seqt) {
56 seqp = seqp->ccid2s_prev;
57 len++;
58 if (!seqp->ccid2s_acked)
59 pipe++;
60
61 while (seqp != hctx->ccid2hctx_seqt) {
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -080062 struct ccid2_seq *prev = seqp->ccid2s_prev;
Andrea Bittau2a91aa32006-03-20 17:41:47 -080063
Andrea Bittau2a91aa32006-03-20 17:41:47 -080064 len++;
65 if (!prev->ccid2s_acked)
66 pipe++;
67
68 /* packets are sent sequentially */
69 BUG_ON(seqp->ccid2s_seq <= prev->ccid2s_seq);
Andrea Bittau29651cd2006-09-19 13:06:46 -070070 BUG_ON(time_before(seqp->ccid2s_sent,
71 prev->ccid2s_sent));
Andrea Bittau2a91aa32006-03-20 17:41:47 -080072
73 seqp = prev;
74 }
75 }
76
77 BUG_ON(pipe != hctx->ccid2hctx_pipe);
78 ccid2_pr_debug("len of chain=%d\n", len);
79
80 do {
81 seqp = seqp->ccid2s_prev;
82 len++;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -080083 } while (seqp != hctx->ccid2hctx_seqh);
Andrea Bittau2a91aa32006-03-20 17:41:47 -080084
Andrea Bittau2a91aa32006-03-20 17:41:47 -080085 ccid2_pr_debug("total len=%d\n", len);
Andrea Bittau07978aa2006-09-19 13:13:37 -070086 BUG_ON(len != hctx->ccid2hctx_seqbufc * CCID2_SEQBUF_LEN);
Andrea Bittau2a91aa32006-03-20 17:41:47 -080087}
88#else
89#define ccid2_hc_tx_check_sanity(hctx) do {} while (0)
90#endif
91
Andrea Bittau07978aa2006-09-19 13:13:37 -070092static int ccid2_hc_tx_alloc_seq(struct ccid2_hc_tx_sock *hctx, int num,
93 gfp_t gfp)
94{
95 struct ccid2_seq *seqp;
96 int i;
97
98 /* check if we have space to preserve the pointer to the buffer */
99 if (hctx->ccid2hctx_seqbufc >= (sizeof(hctx->ccid2hctx_seqbuf) /
100 sizeof(struct ccid2_seq*)))
101 return -ENOMEM;
102
103 /* allocate buffer and initialize linked list */
104 seqp = kmalloc(sizeof(*seqp) * num, gfp);
105 if (seqp == NULL)
106 return -ENOMEM;
107
108 for (i = 0; i < (num - 1); i++) {
109 seqp[i].ccid2s_next = &seqp[i + 1];
110 seqp[i + 1].ccid2s_prev = &seqp[i];
111 }
112 seqp[num - 1].ccid2s_next = seqp;
113 seqp->ccid2s_prev = &seqp[num - 1];
114
115 /* This is the first allocation. Initiate the head and tail. */
116 if (hctx->ccid2hctx_seqbufc == 0)
117 hctx->ccid2hctx_seqh = hctx->ccid2hctx_seqt = seqp;
118 else {
119 /* link the existing list with the one we just created */
120 hctx->ccid2hctx_seqh->ccid2s_next = seqp;
121 seqp->ccid2s_prev = hctx->ccid2hctx_seqh;
122
123 hctx->ccid2hctx_seqt->ccid2s_prev = &seqp[num - 1];
124 seqp[num - 1].ccid2s_next = hctx->ccid2hctx_seqt;
125 }
126
127 /* store the original pointer to the buffer so we can free it */
128 hctx->ccid2hctx_seqbuf[hctx->ccid2hctx_seqbufc] = seqp;
129 hctx->ccid2hctx_seqbufc++;
130
131 return 0;
132}
133
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800134static int ccid2_hc_tx_send_packet(struct sock *sk,
135 struct sk_buff *skb, int len)
136{
137 struct ccid2_hc_tx_sock *hctx;
138
139 switch (DCCP_SKB_CB(skb)->dccpd_type) {
140 case 0: /* XXX data packets from userland come through like this */
141 case DCCP_PKT_DATA:
142 case DCCP_PKT_DATAACK:
143 break;
144 /* No congestion control on other packets */
145 default:
146 return 0;
147 }
148
149 hctx = ccid2_hc_tx_sk(sk);
150
151 ccid2_pr_debug("pipe=%d cwnd=%d\n", hctx->ccid2hctx_pipe,
152 hctx->ccid2hctx_cwnd);
153
154 if (hctx->ccid2hctx_pipe < hctx->ccid2hctx_cwnd) {
155 /* OK we can send... make sure previous packet was sent off */
156 if (!hctx->ccid2hctx_sendwait) {
157 hctx->ccid2hctx_sendwait = 1;
158 return 0;
159 }
160 }
161
Andrea Bittau446dec32006-09-19 13:10:11 -0700162 return 1; /* XXX CCID should dequeue when ready instead of polling */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800163}
164
165static void ccid2_change_l_ack_ratio(struct sock *sk, int val)
166{
167 struct dccp_sock *dp = dccp_sk(sk);
168 /*
169 * XXX I don't really agree with val != 2. If cwnd is 1, ack ratio
170 * should be 1... it shouldn't be allowed to become 2.
171 * -sorbo.
172 */
173 if (val != 2) {
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800174 const struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800175 int max = hctx->ccid2hctx_cwnd / 2;
176
177 /* round up */
178 if (hctx->ccid2hctx_cwnd & 1)
179 max++;
180
181 if (val > max)
182 val = max;
183 }
184
185 ccid2_pr_debug("changing local ack ratio to %d\n", val);
186 WARN_ON(val <= 0);
187 dp->dccps_l_ack_ratio = val;
188}
189
190static void ccid2_change_cwnd(struct sock *sk, int val)
191{
192 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
193
194 if (val == 0)
195 val = 1;
196
197 /* XXX do we need to change ack ratio? */
198 ccid2_pr_debug("change cwnd to %d\n", val);
199
200 BUG_ON(val < 1);
201 hctx->ccid2hctx_cwnd = val;
202}
203
204static void ccid2_start_rto_timer(struct sock *sk);
205
206static void ccid2_hc_tx_rto_expire(unsigned long data)
207{
208 struct sock *sk = (struct sock *)data;
209 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
210 long s;
211
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800212 bh_lock_sock(sk);
213 if (sock_owned_by_user(sk)) {
214 sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
215 jiffies + HZ / 5);
216 goto out;
217 }
218
219 ccid2_pr_debug("RTO_EXPIRE\n");
220
221 ccid2_hc_tx_check_sanity(hctx);
222
223 /* back-off timer */
224 hctx->ccid2hctx_rto <<= 1;
225
226 s = hctx->ccid2hctx_rto / HZ;
227 if (s > 60)
228 hctx->ccid2hctx_rto = 60 * HZ;
229
230 ccid2_start_rto_timer(sk);
231
232 /* adjust pipe, cwnd etc */
233 hctx->ccid2hctx_pipe = 0;
234 hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd >> 1;
235 if (hctx->ccid2hctx_ssthresh < 2)
236 hctx->ccid2hctx_ssthresh = 2;
237 ccid2_change_cwnd(sk, 1);
238
239 /* clear state about stuff we sent */
240 hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqh;
241 hctx->ccid2hctx_ssacks = 0;
242 hctx->ccid2hctx_acks = 0;
243 hctx->ccid2hctx_sent = 0;
244
245 /* clear ack ratio state. */
246 hctx->ccid2hctx_arsent = 0;
247 hctx->ccid2hctx_ackloss = 0;
248 hctx->ccid2hctx_rpseq = 0;
249 hctx->ccid2hctx_rpdupack = -1;
250 ccid2_change_l_ack_ratio(sk, 1);
251 ccid2_hc_tx_check_sanity(hctx);
252out:
253 bh_unlock_sock(sk);
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800254 sock_put(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800255}
256
257static void ccid2_start_rto_timer(struct sock *sk)
258{
259 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
260
261 ccid2_pr_debug("setting RTO timeout=%ld\n", hctx->ccid2hctx_rto);
262
263 BUG_ON(timer_pending(&hctx->ccid2hctx_rtotimer));
264 sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
265 jiffies + hctx->ccid2hctx_rto);
266}
267
268static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, int len)
269{
270 struct dccp_sock *dp = dccp_sk(sk);
271 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
Andrea Bittau07978aa2006-09-19 13:13:37 -0700272 struct ccid2_seq *next;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800273 u64 seq;
274
275 ccid2_hc_tx_check_sanity(hctx);
276
277 BUG_ON(!hctx->ccid2hctx_sendwait);
278 hctx->ccid2hctx_sendwait = 0;
279 hctx->ccid2hctx_pipe++;
280 BUG_ON(hctx->ccid2hctx_pipe < 0);
281
282 /* There is an issue. What if another packet is sent between
283 * packet_send() and packet_sent(). Then the sequence number would be
284 * wrong.
285 * -sorbo.
286 */
287 seq = dp->dccps_gss;
288
289 hctx->ccid2hctx_seqh->ccid2s_seq = seq;
290 hctx->ccid2hctx_seqh->ccid2s_acked = 0;
291 hctx->ccid2hctx_seqh->ccid2s_sent = jiffies;
Andrea Bittau07978aa2006-09-19 13:13:37 -0700292
293 next = hctx->ccid2hctx_seqh->ccid2s_next;
294 /* check if we need to alloc more space */
295 if (next == hctx->ccid2hctx_seqt) {
296 int rc;
297
298 ccid2_pr_debug("allocating more space in history\n");
299 rc = ccid2_hc_tx_alloc_seq(hctx, CCID2_SEQBUF_LEN, GFP_KERNEL);
300 BUG_ON(rc); /* XXX what do we do? */
301
302 next = hctx->ccid2hctx_seqh->ccid2s_next;
303 BUG_ON(next == hctx->ccid2hctx_seqt);
304 }
305 hctx->ccid2hctx_seqh = next;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800306
307 ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->ccid2hctx_cwnd,
308 hctx->ccid2hctx_pipe);
309
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800310 hctx->ccid2hctx_sent++;
311
312 /* Ack Ratio. Need to maintain a concept of how many windows we sent */
313 hctx->ccid2hctx_arsent++;
314 /* We had an ack loss in this window... */
315 if (hctx->ccid2hctx_ackloss) {
316 if (hctx->ccid2hctx_arsent >= hctx->ccid2hctx_cwnd) {
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800317 hctx->ccid2hctx_arsent = 0;
318 hctx->ccid2hctx_ackloss = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800319 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800320 } else {
321 /* No acks lost up to now... */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800322 /* decrease ack ratio if enough packets were sent */
323 if (dp->dccps_l_ack_ratio > 1) {
324 /* XXX don't calculate denominator each time */
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800325 int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
326 dp->dccps_l_ack_ratio;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800327
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800328 denom = hctx->ccid2hctx_cwnd * hctx->ccid2hctx_cwnd / denom;
329
330 if (hctx->ccid2hctx_arsent >= denom) {
331 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
332 hctx->ccid2hctx_arsent = 0;
333 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800334 } else {
335 /* we can't increase ack ratio further [1] */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800336 hctx->ccid2hctx_arsent = 0; /* or maybe set it to cwnd*/
337 }
338 }
339
340 /* setup RTO timer */
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800341 if (!timer_pending(&hctx->ccid2hctx_rtotimer))
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800342 ccid2_start_rto_timer(sk);
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800343
Andrea Bittau8d424f62006-09-19 13:12:44 -0700344#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800345 ccid2_pr_debug("pipe=%d\n", hctx->ccid2hctx_pipe);
346 ccid2_pr_debug("Sent: seq=%llu\n", seq);
347 do {
348 struct ccid2_seq *seqp = hctx->ccid2hctx_seqt;
349
350 while (seqp != hctx->ccid2hctx_seqh) {
351 ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
352 seqp->ccid2s_seq, seqp->ccid2s_acked,
353 seqp->ccid2s_sent);
354 seqp = seqp->ccid2s_next;
355 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800356 } while (0);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800357 ccid2_pr_debug("=========\n");
358 ccid2_hc_tx_check_sanity(hctx);
359#endif
360}
361
362/* XXX Lame code duplication!
363 * returns -1 if none was found.
364 * else returns the next offset to use in the function call.
365 */
366static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
367 unsigned char **vec, unsigned char *veclen)
368{
369 const struct dccp_hdr *dh = dccp_hdr(skb);
370 unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
371 unsigned char *opt_ptr;
372 const unsigned char *opt_end = (unsigned char *)dh +
373 (dh->dccph_doff * 4);
374 unsigned char opt, len;
375 unsigned char *value;
376
377 BUG_ON(offset < 0);
378 options += offset;
379 opt_ptr = options;
380 if (opt_ptr >= opt_end)
381 return -1;
382
383 while (opt_ptr != opt_end) {
384 opt = *opt_ptr++;
385 len = 0;
386 value = NULL;
387
388 /* Check if this isn't a single byte option */
389 if (opt > DCCPO_MAX_RESERVED) {
390 if (opt_ptr == opt_end)
391 goto out_invalid_option;
392
393 len = *opt_ptr++;
394 if (len < 3)
395 goto out_invalid_option;
396 /*
397 * Remove the type and len fields, leaving
398 * just the value size
399 */
400 len -= 2;
401 value = opt_ptr;
402 opt_ptr += len;
403
404 if (opt_ptr > opt_end)
405 goto out_invalid_option;
406 }
407
408 switch (opt) {
409 case DCCPO_ACK_VECTOR_0:
410 case DCCPO_ACK_VECTOR_1:
411 *vec = value;
412 *veclen = len;
413 return offset + (opt_ptr - options);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800414 }
415 }
416
417 return -1;
418
419out_invalid_option:
420 BUG_ON(1); /* should never happen... options were previously parsed ! */
421 return -1;
422}
423
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800424static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800425{
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800426 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
427
428 sk_stop_timer(sk, &hctx->ccid2hctx_rtotimer);
429 ccid2_pr_debug("deleted RTO timer\n");
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800430}
431
432static inline void ccid2_new_ack(struct sock *sk,
433 struct ccid2_seq *seqp,
434 unsigned int *maxincr)
435{
436 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
437
438 /* slow start */
439 if (hctx->ccid2hctx_cwnd < hctx->ccid2hctx_ssthresh) {
440 hctx->ccid2hctx_acks = 0;
441
442 /* We can increase cwnd at most maxincr [ack_ratio/2] */
443 if (*maxincr) {
444 /* increase every 2 acks */
445 hctx->ccid2hctx_ssacks++;
446 if (hctx->ccid2hctx_ssacks == 2) {
447 ccid2_change_cwnd(sk, hctx->ccid2hctx_cwnd + 1);
448 hctx->ccid2hctx_ssacks = 0;
449 *maxincr = *maxincr - 1;
450 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800451 } else {
452 /* increased cwnd enough for this single ack */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800453 hctx->ccid2hctx_ssacks = 0;
454 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800455 } else {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800456 hctx->ccid2hctx_ssacks = 0;
457 hctx->ccid2hctx_acks++;
458
459 if (hctx->ccid2hctx_acks >= hctx->ccid2hctx_cwnd) {
460 ccid2_change_cwnd(sk, hctx->ccid2hctx_cwnd + 1);
461 hctx->ccid2hctx_acks = 0;
462 }
463 }
464
465 /* update RTO */
466 if (hctx->ccid2hctx_srtt == -1 ||
Andrea Bittau29651cd2006-09-19 13:06:46 -0700467 time_after(jiffies, hctx->ccid2hctx_lastrtt + hctx->ccid2hctx_srtt)) {
468 unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800469 int s;
470
471 /* first measurement */
472 if (hctx->ccid2hctx_srtt == -1) {
473 ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
474 r, jiffies, seqp->ccid2s_seq);
475 hctx->ccid2hctx_srtt = r;
476 hctx->ccid2hctx_rttvar = r >> 1;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800477 } else {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800478 /* RTTVAR */
479 long tmp = hctx->ccid2hctx_srtt - r;
480 if (tmp < 0)
481 tmp *= -1;
482
483 tmp >>= 2;
484 hctx->ccid2hctx_rttvar *= 3;
485 hctx->ccid2hctx_rttvar >>= 2;
486 hctx->ccid2hctx_rttvar += tmp;
487
488 /* SRTT */
489 hctx->ccid2hctx_srtt *= 7;
490 hctx->ccid2hctx_srtt >>= 3;
491 tmp = r >> 3;
492 hctx->ccid2hctx_srtt += tmp;
493 }
494 s = hctx->ccid2hctx_rttvar << 2;
495 /* clock granularity is 1 when based on jiffies */
496 if (!s)
497 s = 1;
498 hctx->ccid2hctx_rto = hctx->ccid2hctx_srtt + s;
499
500 /* must be at least a second */
501 s = hctx->ccid2hctx_rto / HZ;
502 /* DCCP doesn't require this [but I like it cuz my code sux] */
503#if 1
504 if (s < 1)
505 hctx->ccid2hctx_rto = HZ;
506#endif
507 /* max 60 seconds */
508 if (s > 60)
509 hctx->ccid2hctx_rto = HZ * 60;
510
511 hctx->ccid2hctx_lastrtt = jiffies;
512
513 ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
514 hctx->ccid2hctx_srtt, hctx->ccid2hctx_rttvar,
515 hctx->ccid2hctx_rto, HZ, r);
516 hctx->ccid2hctx_sent = 0;
517 }
518
519 /* we got a new ack, so re-start RTO timer */
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800520 ccid2_hc_tx_kill_rto_timer(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800521 ccid2_start_rto_timer(sk);
522}
523
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800524static void ccid2_hc_tx_dec_pipe(struct sock *sk)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800525{
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800526 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
527
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800528 hctx->ccid2hctx_pipe--;
529 BUG_ON(hctx->ccid2hctx_pipe < 0);
530
531 if (hctx->ccid2hctx_pipe == 0)
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800532 ccid2_hc_tx_kill_rto_timer(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800533}
534
535static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
536{
537 struct dccp_sock *dp = dccp_sk(sk);
538 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
539 u64 ackno, seqno;
540 struct ccid2_seq *seqp;
541 unsigned char *vector;
542 unsigned char veclen;
543 int offset = 0;
544 int done = 0;
545 int loss = 0;
546 unsigned int maxincr = 0;
547
548 ccid2_hc_tx_check_sanity(hctx);
549 /* check reverse path congestion */
550 seqno = DCCP_SKB_CB(skb)->dccpd_seq;
551
552 /* XXX this whole "algorithm" is broken. Need to fix it to keep track
553 * of the seqnos of the dupacks so that rpseq and rpdupack are correct
554 * -sorbo.
555 */
556 /* need to bootstrap */
557 if (hctx->ccid2hctx_rpdupack == -1) {
558 hctx->ccid2hctx_rpdupack = 0;
559 hctx->ccid2hctx_rpseq = seqno;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800560 } else {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800561 /* check if packet is consecutive */
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800562 if ((hctx->ccid2hctx_rpseq + 1) == seqno)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800563 hctx->ccid2hctx_rpseq++;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800564 /* it's a later packet */
565 else if (after48(seqno, hctx->ccid2hctx_rpseq)) {
566 hctx->ccid2hctx_rpdupack++;
567
568 /* check if we got enough dupacks */
569 if (hctx->ccid2hctx_rpdupack >=
570 hctx->ccid2hctx_numdupack) {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800571 hctx->ccid2hctx_rpdupack = -1; /* XXX lame */
572 hctx->ccid2hctx_rpseq = 0;
573
574 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio << 1);
575 }
576 }
577 }
578
579 /* check forward path congestion */
580 /* still didn't send out new data packets */
581 if (hctx->ccid2hctx_seqh == hctx->ccid2hctx_seqt)
582 return;
583
584 switch (DCCP_SKB_CB(skb)->dccpd_type) {
585 case DCCP_PKT_ACK:
586 case DCCP_PKT_DATAACK:
587 break;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800588 default:
589 return;
590 }
591
592 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
593 seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
594
595 /* If in slow-start, cwnd can increase at most Ack Ratio / 2 packets for
596 * this single ack. I round up.
597 * -sorbo.
598 */
599 maxincr = dp->dccps_l_ack_ratio >> 1;
600 maxincr++;
601
602 /* go through all ack vectors */
603 while ((offset = ccid2_ackvector(sk, skb, offset,
604 &vector, &veclen)) != -1) {
605 /* go through this ack vector */
606 while (veclen--) {
607 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
608 u64 ackno_end_rl;
609
610 dccp_set_seqno(&ackno_end_rl, ackno - rl);
611 ccid2_pr_debug("ackvec start:%llu end:%llu\n", ackno,
612 ackno_end_rl);
613 /* if the seqno we are analyzing is larger than the
614 * current ackno, then move towards the tail of our
615 * seqnos.
616 */
617 while (after48(seqp->ccid2s_seq, ackno)) {
618 if (seqp == hctx->ccid2hctx_seqt) {
619 done = 1;
620 break;
621 }
622 seqp = seqp->ccid2s_prev;
623 }
624 if (done)
625 break;
626
627 /* check all seqnos in the range of the vector
628 * run length
629 */
630 while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
Andrea Bittau8e27e462006-09-19 13:05:35 -0700631 const u8 state = *vector &
632 DCCP_ACKVEC_STATE_MASK;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800633
634 /* new packet received or marked */
635 if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
636 !seqp->ccid2s_acked) {
637 if (state ==
638 DCCP_ACKVEC_STATE_ECN_MARKED) {
639 loss = 1;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800640 } else
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800641 ccid2_new_ack(sk, seqp,
642 &maxincr);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800643
644 seqp->ccid2s_acked = 1;
645 ccid2_pr_debug("Got ack for %llu\n",
646 seqp->ccid2s_seq);
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800647 ccid2_hc_tx_dec_pipe(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800648 }
649 if (seqp == hctx->ccid2hctx_seqt) {
650 done = 1;
651 break;
652 }
653 seqp = seqp->ccid2s_next;
654 }
655 if (done)
656 break;
657
658
659 dccp_set_seqno(&ackno, ackno_end_rl - 1);
660 vector++;
661 }
662 if (done)
663 break;
664 }
665
666 /* The state about what is acked should be correct now
667 * Check for NUMDUPACK
668 */
669 seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
670 done = 0;
671 while (1) {
672 if (seqp->ccid2s_acked) {
673 done++;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800674 if (done == hctx->ccid2hctx_numdupack)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800675 break;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800676 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800677 if (seqp == hctx->ccid2hctx_seqt)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800678 break;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800679 seqp = seqp->ccid2s_prev;
680 }
681
682 /* If there are at least 3 acknowledgements, anything unacknowledged
683 * below the last sequence number is considered lost
684 */
685 if (done == hctx->ccid2hctx_numdupack) {
686 struct ccid2_seq *last_acked = seqp;
687
688 /* check for lost packets */
689 while (1) {
690 if (!seqp->ccid2s_acked) {
691 loss = 1;
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800692 ccid2_hc_tx_dec_pipe(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800693 }
694 if (seqp == hctx->ccid2hctx_seqt)
695 break;
696 seqp = seqp->ccid2s_prev;
697 }
698
699 hctx->ccid2hctx_seqt = last_acked;
700 }
701
702 /* trim acked packets in tail */
703 while (hctx->ccid2hctx_seqt != hctx->ccid2hctx_seqh) {
704 if (!hctx->ccid2hctx_seqt->ccid2s_acked)
705 break;
706
707 hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqt->ccid2s_next;
708 }
709
710 if (loss) {
711 /* XXX do bit shifts guarantee a 0 as the new bit? */
712 ccid2_change_cwnd(sk, hctx->ccid2hctx_cwnd >> 1);
713 hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd;
714 if (hctx->ccid2hctx_ssthresh < 2)
715 hctx->ccid2hctx_ssthresh = 2;
716 }
717
718 ccid2_hc_tx_check_sanity(hctx);
719}
720
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800721static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800722{
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800723 struct ccid2_hc_tx_sock *hctx = ccid_priv(ccid);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800724
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800725 hctx->ccid2hctx_cwnd = 1;
Andrea Bittaud458c252006-09-19 13:07:20 -0700726 /* Initialize ssthresh to infinity. This means that we will exit the
727 * initial slow-start after the first packet loss. This is what we
728 * want.
729 */
730 hctx->ccid2hctx_ssthresh = ~0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800731 hctx->ccid2hctx_numdupack = 3;
Andrea Bittau07978aa2006-09-19 13:13:37 -0700732 hctx->ccid2hctx_seqbufc = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800733
734 /* XXX init ~ to window size... */
Andrea Bittau07978aa2006-09-19 13:13:37 -0700735 if (ccid2_hc_tx_alloc_seq(hctx, CCID2_SEQBUF_LEN, GFP_ATOMIC) != 0)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800736 return -ENOMEM;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800737
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800738 hctx->ccid2hctx_sent = 0;
739 hctx->ccid2hctx_rto = 3 * HZ;
740 hctx->ccid2hctx_srtt = -1;
741 hctx->ccid2hctx_rttvar = -1;
742 hctx->ccid2hctx_lastrtt = 0;
743 hctx->ccid2hctx_rpdupack = -1;
744
745 hctx->ccid2hctx_rtotimer.function = &ccid2_hc_tx_rto_expire;
746 hctx->ccid2hctx_rtotimer.data = (unsigned long)sk;
747 init_timer(&hctx->ccid2hctx_rtotimer);
748
749 ccid2_hc_tx_check_sanity(hctx);
750 return 0;
751}
752
753static void ccid2_hc_tx_exit(struct sock *sk)
754{
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800755 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
Andrea Bittau07978aa2006-09-19 13:13:37 -0700756 int i;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800757
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800758 ccid2_hc_tx_kill_rto_timer(sk);
Andrea Bittau07978aa2006-09-19 13:13:37 -0700759
760 for (i = 0; i < hctx->ccid2hctx_seqbufc; i++)
761 kfree(hctx->ccid2hctx_seqbuf[i]);
762 hctx->ccid2hctx_seqbufc = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800763}
764
765static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
766{
767 const struct dccp_sock *dp = dccp_sk(sk);
768 struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
769
770 switch (DCCP_SKB_CB(skb)->dccpd_type) {
771 case DCCP_PKT_DATA:
772 case DCCP_PKT_DATAACK:
773 hcrx->ccid2hcrx_data++;
774 if (hcrx->ccid2hcrx_data >= dp->dccps_r_ack_ratio) {
775 dccp_send_ack(sk);
776 hcrx->ccid2hcrx_data = 0;
777 }
778 break;
779 }
780}
781
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800782static struct ccid_operations ccid2 = {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800783 .ccid_id = 2,
784 .ccid_name = "ccid2",
785 .ccid_owner = THIS_MODULE,
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800786 .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800787 .ccid_hc_tx_init = ccid2_hc_tx_init,
788 .ccid_hc_tx_exit = ccid2_hc_tx_exit,
789 .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
790 .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
791 .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800792 .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800793 .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
794};
795
796module_param(ccid2_debug, int, 0444);
797MODULE_PARM_DESC(ccid2_debug, "Enable debug messages");
798
799static __init int ccid2_module_init(void)
800{
801 return ccid_register(&ccid2);
802}
803module_init(ccid2_module_init);
804
805static __exit void ccid2_module_exit(void)
806{
807 ccid_unregister(&ccid2);
808}
809module_exit(ccid2_module_exit);
810
811MODULE_AUTHOR("Andrea Bittau <a.bittau@cs.ucl.ac.uk>");
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800812MODULE_DESCRIPTION("DCCP TCP-Like (CCID2) CCID");
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800813MODULE_LICENSE("GPL");
814MODULE_ALIAS("net-dccp-ccid-2");