blob: b88da035865f3e9e7aca9e4adef866d5d6cc32af [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
Andrea Bittau374bcf32006-09-19 13:14:43 -0700190static void ccid2_change_cwnd(struct ccid2_hc_tx_sock *hctx, int val)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800191{
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800192 if (val == 0)
193 val = 1;
194
195 /* XXX do we need to change ack ratio? */
196 ccid2_pr_debug("change cwnd to %d\n", val);
197
198 BUG_ON(val < 1);
199 hctx->ccid2hctx_cwnd = val;
200}
201
202static void ccid2_start_rto_timer(struct sock *sk);
203
204static void ccid2_hc_tx_rto_expire(unsigned long data)
205{
206 struct sock *sk = (struct sock *)data;
207 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
208 long s;
209
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800210 bh_lock_sock(sk);
211 if (sock_owned_by_user(sk)) {
212 sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
213 jiffies + HZ / 5);
214 goto out;
215 }
216
217 ccid2_pr_debug("RTO_EXPIRE\n");
218
219 ccid2_hc_tx_check_sanity(hctx);
220
221 /* back-off timer */
222 hctx->ccid2hctx_rto <<= 1;
223
224 s = hctx->ccid2hctx_rto / HZ;
225 if (s > 60)
226 hctx->ccid2hctx_rto = 60 * HZ;
227
228 ccid2_start_rto_timer(sk);
229
230 /* adjust pipe, cwnd etc */
231 hctx->ccid2hctx_pipe = 0;
232 hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd >> 1;
233 if (hctx->ccid2hctx_ssthresh < 2)
234 hctx->ccid2hctx_ssthresh = 2;
Andrea Bittau374bcf32006-09-19 13:14:43 -0700235 ccid2_change_cwnd(hctx, 1);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800236
237 /* clear state about stuff we sent */
238 hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqh;
239 hctx->ccid2hctx_ssacks = 0;
240 hctx->ccid2hctx_acks = 0;
241 hctx->ccid2hctx_sent = 0;
242
243 /* clear ack ratio state. */
244 hctx->ccid2hctx_arsent = 0;
245 hctx->ccid2hctx_ackloss = 0;
246 hctx->ccid2hctx_rpseq = 0;
247 hctx->ccid2hctx_rpdupack = -1;
248 ccid2_change_l_ack_ratio(sk, 1);
249 ccid2_hc_tx_check_sanity(hctx);
250out:
251 bh_unlock_sock(sk);
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800252 sock_put(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800253}
254
255static void ccid2_start_rto_timer(struct sock *sk)
256{
257 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
258
259 ccid2_pr_debug("setting RTO timeout=%ld\n", hctx->ccid2hctx_rto);
260
261 BUG_ON(timer_pending(&hctx->ccid2hctx_rtotimer));
262 sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
263 jiffies + hctx->ccid2hctx_rto);
264}
265
266static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, int len)
267{
268 struct dccp_sock *dp = dccp_sk(sk);
269 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
Andrea Bittau07978aa2006-09-19 13:13:37 -0700270 struct ccid2_seq *next;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800271 u64 seq;
272
273 ccid2_hc_tx_check_sanity(hctx);
274
275 BUG_ON(!hctx->ccid2hctx_sendwait);
276 hctx->ccid2hctx_sendwait = 0;
277 hctx->ccid2hctx_pipe++;
278 BUG_ON(hctx->ccid2hctx_pipe < 0);
279
280 /* There is an issue. What if another packet is sent between
281 * packet_send() and packet_sent(). Then the sequence number would be
282 * wrong.
283 * -sorbo.
284 */
285 seq = dp->dccps_gss;
286
287 hctx->ccid2hctx_seqh->ccid2s_seq = seq;
288 hctx->ccid2hctx_seqh->ccid2s_acked = 0;
289 hctx->ccid2hctx_seqh->ccid2s_sent = jiffies;
Andrea Bittau07978aa2006-09-19 13:13:37 -0700290
291 next = hctx->ccid2hctx_seqh->ccid2s_next;
292 /* check if we need to alloc more space */
293 if (next == hctx->ccid2hctx_seqt) {
294 int rc;
295
296 ccid2_pr_debug("allocating more space in history\n");
297 rc = ccid2_hc_tx_alloc_seq(hctx, CCID2_SEQBUF_LEN, GFP_KERNEL);
298 BUG_ON(rc); /* XXX what do we do? */
299
300 next = hctx->ccid2hctx_seqh->ccid2s_next;
301 BUG_ON(next == hctx->ccid2hctx_seqt);
302 }
303 hctx->ccid2hctx_seqh = next;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800304
305 ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->ccid2hctx_cwnd,
306 hctx->ccid2hctx_pipe);
307
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800308 hctx->ccid2hctx_sent++;
309
310 /* Ack Ratio. Need to maintain a concept of how many windows we sent */
311 hctx->ccid2hctx_arsent++;
312 /* We had an ack loss in this window... */
313 if (hctx->ccid2hctx_ackloss) {
314 if (hctx->ccid2hctx_arsent >= hctx->ccid2hctx_cwnd) {
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800315 hctx->ccid2hctx_arsent = 0;
316 hctx->ccid2hctx_ackloss = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800317 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800318 } else {
319 /* No acks lost up to now... */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800320 /* decrease ack ratio if enough packets were sent */
321 if (dp->dccps_l_ack_ratio > 1) {
322 /* XXX don't calculate denominator each time */
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800323 int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
324 dp->dccps_l_ack_ratio;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800325
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800326 denom = hctx->ccid2hctx_cwnd * hctx->ccid2hctx_cwnd / denom;
327
328 if (hctx->ccid2hctx_arsent >= denom) {
329 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
330 hctx->ccid2hctx_arsent = 0;
331 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800332 } else {
333 /* we can't increase ack ratio further [1] */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800334 hctx->ccid2hctx_arsent = 0; /* or maybe set it to cwnd*/
335 }
336 }
337
338 /* setup RTO timer */
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800339 if (!timer_pending(&hctx->ccid2hctx_rtotimer))
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800340 ccid2_start_rto_timer(sk);
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800341
Andrea Bittau8d424f62006-09-19 13:12:44 -0700342#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800343 ccid2_pr_debug("pipe=%d\n", hctx->ccid2hctx_pipe);
344 ccid2_pr_debug("Sent: seq=%llu\n", seq);
345 do {
346 struct ccid2_seq *seqp = hctx->ccid2hctx_seqt;
347
348 while (seqp != hctx->ccid2hctx_seqh) {
349 ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
350 seqp->ccid2s_seq, seqp->ccid2s_acked,
351 seqp->ccid2s_sent);
352 seqp = seqp->ccid2s_next;
353 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800354 } while (0);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800355 ccid2_pr_debug("=========\n");
356 ccid2_hc_tx_check_sanity(hctx);
357#endif
358}
359
360/* XXX Lame code duplication!
361 * returns -1 if none was found.
362 * else returns the next offset to use in the function call.
363 */
364static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
365 unsigned char **vec, unsigned char *veclen)
366{
367 const struct dccp_hdr *dh = dccp_hdr(skb);
368 unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
369 unsigned char *opt_ptr;
370 const unsigned char *opt_end = (unsigned char *)dh +
371 (dh->dccph_doff * 4);
372 unsigned char opt, len;
373 unsigned char *value;
374
375 BUG_ON(offset < 0);
376 options += offset;
377 opt_ptr = options;
378 if (opt_ptr >= opt_end)
379 return -1;
380
381 while (opt_ptr != opt_end) {
382 opt = *opt_ptr++;
383 len = 0;
384 value = NULL;
385
386 /* Check if this isn't a single byte option */
387 if (opt > DCCPO_MAX_RESERVED) {
388 if (opt_ptr == opt_end)
389 goto out_invalid_option;
390
391 len = *opt_ptr++;
392 if (len < 3)
393 goto out_invalid_option;
394 /*
395 * Remove the type and len fields, leaving
396 * just the value size
397 */
398 len -= 2;
399 value = opt_ptr;
400 opt_ptr += len;
401
402 if (opt_ptr > opt_end)
403 goto out_invalid_option;
404 }
405
406 switch (opt) {
407 case DCCPO_ACK_VECTOR_0:
408 case DCCPO_ACK_VECTOR_1:
409 *vec = value;
410 *veclen = len;
411 return offset + (opt_ptr - options);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800412 }
413 }
414
415 return -1;
416
417out_invalid_option:
418 BUG_ON(1); /* should never happen... options were previously parsed ! */
419 return -1;
420}
421
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800422static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800423{
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800424 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
425
426 sk_stop_timer(sk, &hctx->ccid2hctx_rtotimer);
427 ccid2_pr_debug("deleted RTO timer\n");
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800428}
429
430static inline void ccid2_new_ack(struct sock *sk,
431 struct ccid2_seq *seqp,
432 unsigned int *maxincr)
433{
434 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
435
436 /* slow start */
437 if (hctx->ccid2hctx_cwnd < hctx->ccid2hctx_ssthresh) {
438 hctx->ccid2hctx_acks = 0;
439
440 /* We can increase cwnd at most maxincr [ack_ratio/2] */
441 if (*maxincr) {
442 /* increase every 2 acks */
443 hctx->ccid2hctx_ssacks++;
444 if (hctx->ccid2hctx_ssacks == 2) {
Andrea Bittau374bcf32006-09-19 13:14:43 -0700445 ccid2_change_cwnd(hctx, hctx->ccid2hctx_cwnd+1);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800446 hctx->ccid2hctx_ssacks = 0;
447 *maxincr = *maxincr - 1;
448 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800449 } else {
450 /* increased cwnd enough for this single ack */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800451 hctx->ccid2hctx_ssacks = 0;
452 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800453 } else {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800454 hctx->ccid2hctx_ssacks = 0;
455 hctx->ccid2hctx_acks++;
456
457 if (hctx->ccid2hctx_acks >= hctx->ccid2hctx_cwnd) {
Andrea Bittau374bcf32006-09-19 13:14:43 -0700458 ccid2_change_cwnd(hctx, hctx->ccid2hctx_cwnd + 1);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800459 hctx->ccid2hctx_acks = 0;
460 }
461 }
462
463 /* update RTO */
464 if (hctx->ccid2hctx_srtt == -1 ||
Andrea Bittau29651cd2006-09-19 13:06:46 -0700465 time_after(jiffies, hctx->ccid2hctx_lastrtt + hctx->ccid2hctx_srtt)) {
466 unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800467 int s;
468
469 /* first measurement */
470 if (hctx->ccid2hctx_srtt == -1) {
471 ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
472 r, jiffies, seqp->ccid2s_seq);
473 hctx->ccid2hctx_srtt = r;
474 hctx->ccid2hctx_rttvar = r >> 1;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800475 } else {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800476 /* RTTVAR */
477 long tmp = hctx->ccid2hctx_srtt - r;
478 if (tmp < 0)
479 tmp *= -1;
480
481 tmp >>= 2;
482 hctx->ccid2hctx_rttvar *= 3;
483 hctx->ccid2hctx_rttvar >>= 2;
484 hctx->ccid2hctx_rttvar += tmp;
485
486 /* SRTT */
487 hctx->ccid2hctx_srtt *= 7;
488 hctx->ccid2hctx_srtt >>= 3;
489 tmp = r >> 3;
490 hctx->ccid2hctx_srtt += tmp;
491 }
492 s = hctx->ccid2hctx_rttvar << 2;
493 /* clock granularity is 1 when based on jiffies */
494 if (!s)
495 s = 1;
496 hctx->ccid2hctx_rto = hctx->ccid2hctx_srtt + s;
497
498 /* must be at least a second */
499 s = hctx->ccid2hctx_rto / HZ;
500 /* DCCP doesn't require this [but I like it cuz my code sux] */
501#if 1
502 if (s < 1)
503 hctx->ccid2hctx_rto = HZ;
504#endif
505 /* max 60 seconds */
506 if (s > 60)
507 hctx->ccid2hctx_rto = HZ * 60;
508
509 hctx->ccid2hctx_lastrtt = jiffies;
510
511 ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
512 hctx->ccid2hctx_srtt, hctx->ccid2hctx_rttvar,
513 hctx->ccid2hctx_rto, HZ, r);
514 hctx->ccid2hctx_sent = 0;
515 }
516
517 /* we got a new ack, so re-start RTO timer */
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800518 ccid2_hc_tx_kill_rto_timer(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800519 ccid2_start_rto_timer(sk);
520}
521
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800522static void ccid2_hc_tx_dec_pipe(struct sock *sk)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800523{
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800524 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
525
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800526 hctx->ccid2hctx_pipe--;
527 BUG_ON(hctx->ccid2hctx_pipe < 0);
528
529 if (hctx->ccid2hctx_pipe == 0)
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800530 ccid2_hc_tx_kill_rto_timer(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800531}
532
Andrea Bittau374bcf32006-09-19 13:14:43 -0700533static void ccid2_congestion_event(struct ccid2_hc_tx_sock *hctx,
534 struct ccid2_seq *seqp)
535{
536 if (time_before(seqp->ccid2s_sent, hctx->ccid2hctx_last_cong)) {
537 ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
538 return;
539 }
540
541 hctx->ccid2hctx_last_cong = jiffies;
542
543 ccid2_change_cwnd(hctx, hctx->ccid2hctx_cwnd >> 1);
544 hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd;
545 if (hctx->ccid2hctx_ssthresh < 2)
546 hctx->ccid2hctx_ssthresh = 2;
547}
548
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800549static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
550{
551 struct dccp_sock *dp = dccp_sk(sk);
552 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
553 u64 ackno, seqno;
554 struct ccid2_seq *seqp;
555 unsigned char *vector;
556 unsigned char veclen;
557 int offset = 0;
558 int done = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800559 unsigned int maxincr = 0;
560
561 ccid2_hc_tx_check_sanity(hctx);
562 /* check reverse path congestion */
563 seqno = DCCP_SKB_CB(skb)->dccpd_seq;
564
565 /* XXX this whole "algorithm" is broken. Need to fix it to keep track
566 * of the seqnos of the dupacks so that rpseq and rpdupack are correct
567 * -sorbo.
568 */
569 /* need to bootstrap */
570 if (hctx->ccid2hctx_rpdupack == -1) {
571 hctx->ccid2hctx_rpdupack = 0;
572 hctx->ccid2hctx_rpseq = seqno;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800573 } else {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800574 /* check if packet is consecutive */
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800575 if ((hctx->ccid2hctx_rpseq + 1) == seqno)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800576 hctx->ccid2hctx_rpseq++;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800577 /* it's a later packet */
578 else if (after48(seqno, hctx->ccid2hctx_rpseq)) {
579 hctx->ccid2hctx_rpdupack++;
580
581 /* check if we got enough dupacks */
582 if (hctx->ccid2hctx_rpdupack >=
583 hctx->ccid2hctx_numdupack) {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800584 hctx->ccid2hctx_rpdupack = -1; /* XXX lame */
585 hctx->ccid2hctx_rpseq = 0;
586
587 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio << 1);
588 }
589 }
590 }
591
592 /* check forward path congestion */
593 /* still didn't send out new data packets */
594 if (hctx->ccid2hctx_seqh == hctx->ccid2hctx_seqt)
595 return;
596
597 switch (DCCP_SKB_CB(skb)->dccpd_type) {
598 case DCCP_PKT_ACK:
599 case DCCP_PKT_DATAACK:
600 break;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800601 default:
602 return;
603 }
604
605 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
606 seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
607
608 /* If in slow-start, cwnd can increase at most Ack Ratio / 2 packets for
609 * this single ack. I round up.
610 * -sorbo.
611 */
612 maxincr = dp->dccps_l_ack_ratio >> 1;
613 maxincr++;
614
615 /* go through all ack vectors */
616 while ((offset = ccid2_ackvector(sk, skb, offset,
617 &vector, &veclen)) != -1) {
618 /* go through this ack vector */
619 while (veclen--) {
620 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
621 u64 ackno_end_rl;
622
623 dccp_set_seqno(&ackno_end_rl, ackno - rl);
624 ccid2_pr_debug("ackvec start:%llu end:%llu\n", ackno,
625 ackno_end_rl);
626 /* if the seqno we are analyzing is larger than the
627 * current ackno, then move towards the tail of our
628 * seqnos.
629 */
630 while (after48(seqp->ccid2s_seq, ackno)) {
631 if (seqp == hctx->ccid2hctx_seqt) {
632 done = 1;
633 break;
634 }
635 seqp = seqp->ccid2s_prev;
636 }
637 if (done)
638 break;
639
640 /* check all seqnos in the range of the vector
641 * run length
642 */
643 while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
Andrea Bittau8e27e462006-09-19 13:05:35 -0700644 const u8 state = *vector &
645 DCCP_ACKVEC_STATE_MASK;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800646
647 /* new packet received or marked */
648 if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
649 !seqp->ccid2s_acked) {
650 if (state ==
651 DCCP_ACKVEC_STATE_ECN_MARKED) {
Andrea Bittau374bcf32006-09-19 13:14:43 -0700652 ccid2_congestion_event(hctx,
653 seqp);
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800654 } else
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800655 ccid2_new_ack(sk, seqp,
656 &maxincr);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800657
658 seqp->ccid2s_acked = 1;
659 ccid2_pr_debug("Got ack for %llu\n",
660 seqp->ccid2s_seq);
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800661 ccid2_hc_tx_dec_pipe(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800662 }
663 if (seqp == hctx->ccid2hctx_seqt) {
664 done = 1;
665 break;
666 }
667 seqp = seqp->ccid2s_next;
668 }
669 if (done)
670 break;
671
672
673 dccp_set_seqno(&ackno, ackno_end_rl - 1);
674 vector++;
675 }
676 if (done)
677 break;
678 }
679
680 /* The state about what is acked should be correct now
681 * Check for NUMDUPACK
682 */
683 seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
684 done = 0;
685 while (1) {
686 if (seqp->ccid2s_acked) {
687 done++;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800688 if (done == hctx->ccid2hctx_numdupack)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800689 break;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800690 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800691 if (seqp == hctx->ccid2hctx_seqt)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800692 break;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800693 seqp = seqp->ccid2s_prev;
694 }
695
696 /* If there are at least 3 acknowledgements, anything unacknowledged
697 * below the last sequence number is considered lost
698 */
699 if (done == hctx->ccid2hctx_numdupack) {
700 struct ccid2_seq *last_acked = seqp;
701
702 /* check for lost packets */
703 while (1) {
704 if (!seqp->ccid2s_acked) {
Andrea Bittau374bcf32006-09-19 13:14:43 -0700705 ccid2_pr_debug("Packet lost: %llu\n",
706 seqp->ccid2s_seq);
707 /* XXX need to traverse from tail -> head in
708 * order to detect multiple congestion events in
709 * one ack vector.
710 */
711 ccid2_congestion_event(hctx, seqp);
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800712 ccid2_hc_tx_dec_pipe(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800713 }
714 if (seqp == hctx->ccid2hctx_seqt)
715 break;
716 seqp = seqp->ccid2s_prev;
717 }
718
719 hctx->ccid2hctx_seqt = last_acked;
720 }
721
722 /* trim acked packets in tail */
723 while (hctx->ccid2hctx_seqt != hctx->ccid2hctx_seqh) {
724 if (!hctx->ccid2hctx_seqt->ccid2s_acked)
725 break;
726
727 hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqt->ccid2s_next;
728 }
729
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800730 ccid2_hc_tx_check_sanity(hctx);
731}
732
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800733static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800734{
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800735 struct ccid2_hc_tx_sock *hctx = ccid_priv(ccid);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800736
Andrea Bittau374bcf32006-09-19 13:14:43 -0700737 ccid2_change_cwnd(hctx, 1);
Andrea Bittaud458c252006-09-19 13:07:20 -0700738 /* Initialize ssthresh to infinity. This means that we will exit the
739 * initial slow-start after the first packet loss. This is what we
740 * want.
741 */
742 hctx->ccid2hctx_ssthresh = ~0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800743 hctx->ccid2hctx_numdupack = 3;
Andrea Bittau07978aa2006-09-19 13:13:37 -0700744 hctx->ccid2hctx_seqbufc = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800745
746 /* XXX init ~ to window size... */
Andrea Bittau07978aa2006-09-19 13:13:37 -0700747 if (ccid2_hc_tx_alloc_seq(hctx, CCID2_SEQBUF_LEN, GFP_ATOMIC) != 0)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800748 return -ENOMEM;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800749
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800750 hctx->ccid2hctx_sent = 0;
751 hctx->ccid2hctx_rto = 3 * HZ;
752 hctx->ccid2hctx_srtt = -1;
753 hctx->ccid2hctx_rttvar = -1;
754 hctx->ccid2hctx_lastrtt = 0;
755 hctx->ccid2hctx_rpdupack = -1;
Andrea Bittau374bcf32006-09-19 13:14:43 -0700756 hctx->ccid2hctx_last_cong = jiffies;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800757
758 hctx->ccid2hctx_rtotimer.function = &ccid2_hc_tx_rto_expire;
759 hctx->ccid2hctx_rtotimer.data = (unsigned long)sk;
760 init_timer(&hctx->ccid2hctx_rtotimer);
761
762 ccid2_hc_tx_check_sanity(hctx);
763 return 0;
764}
765
766static void ccid2_hc_tx_exit(struct sock *sk)
767{
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800768 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
Andrea Bittau07978aa2006-09-19 13:13:37 -0700769 int i;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800770
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800771 ccid2_hc_tx_kill_rto_timer(sk);
Andrea Bittau07978aa2006-09-19 13:13:37 -0700772
773 for (i = 0; i < hctx->ccid2hctx_seqbufc; i++)
774 kfree(hctx->ccid2hctx_seqbuf[i]);
775 hctx->ccid2hctx_seqbufc = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800776}
777
778static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
779{
780 const struct dccp_sock *dp = dccp_sk(sk);
781 struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
782
783 switch (DCCP_SKB_CB(skb)->dccpd_type) {
784 case DCCP_PKT_DATA:
785 case DCCP_PKT_DATAACK:
786 hcrx->ccid2hcrx_data++;
787 if (hcrx->ccid2hcrx_data >= dp->dccps_r_ack_ratio) {
788 dccp_send_ack(sk);
789 hcrx->ccid2hcrx_data = 0;
790 }
791 break;
792 }
793}
794
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800795static struct ccid_operations ccid2 = {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800796 .ccid_id = 2,
797 .ccid_name = "ccid2",
798 .ccid_owner = THIS_MODULE,
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800799 .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800800 .ccid_hc_tx_init = ccid2_hc_tx_init,
801 .ccid_hc_tx_exit = ccid2_hc_tx_exit,
802 .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
803 .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
804 .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800805 .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800806 .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
807};
808
809module_param(ccid2_debug, int, 0444);
810MODULE_PARM_DESC(ccid2_debug, "Enable debug messages");
811
812static __init int ccid2_module_init(void)
813{
814 return ccid_register(&ccid2);
815}
816module_init(ccid2_module_init);
817
818static __exit void ccid2_module_exit(void)
819{
820 ccid_unregister(&ccid2);
821}
822module_exit(ccid2_module_exit);
823
824MODULE_AUTHOR("Andrea Bittau <a.bittau@cs.ucl.ac.uk>");
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800825MODULE_DESCRIPTION("DCCP TCP-Like (CCID2) CCID");
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800826MODULE_LICENSE("GPL");
827MODULE_ALIAS("net-dccp-ccid-2");