blob: 162032baeac0acb4fddece61def024fe370c684f [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/*
Gerrit Renker0e64e942006-10-24 16:17:51 -070026 * This implementation should follow RFC 4341
Andrea Bittau2a91aa32006-03-20 17:41:47 -080027 *
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
Andrea Bittau593f16a2006-09-19 13:15:33 -0700202static void ccid2_change_srtt(struct ccid2_hc_tx_sock *hctx, long val)
203{
204 ccid2_pr_debug("change SRTT to %ld\n", val);
205 hctx->ccid2hctx_srtt = val;
206}
207
208static void ccid2_change_pipe(struct ccid2_hc_tx_sock *hctx, long val)
209{
210 hctx->ccid2hctx_pipe = val;
211}
212
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800213static void ccid2_start_rto_timer(struct sock *sk);
214
215static void ccid2_hc_tx_rto_expire(unsigned long data)
216{
217 struct sock *sk = (struct sock *)data;
218 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
219 long s;
220
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800221 bh_lock_sock(sk);
222 if (sock_owned_by_user(sk)) {
223 sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
224 jiffies + HZ / 5);
225 goto out;
226 }
227
228 ccid2_pr_debug("RTO_EXPIRE\n");
229
230 ccid2_hc_tx_check_sanity(hctx);
231
232 /* back-off timer */
233 hctx->ccid2hctx_rto <<= 1;
234
235 s = hctx->ccid2hctx_rto / HZ;
236 if (s > 60)
237 hctx->ccid2hctx_rto = 60 * HZ;
238
239 ccid2_start_rto_timer(sk);
240
241 /* adjust pipe, cwnd etc */
Andrea Bittau593f16a2006-09-19 13:15:33 -0700242 ccid2_change_pipe(hctx, 0);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800243 hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd >> 1;
244 if (hctx->ccid2hctx_ssthresh < 2)
245 hctx->ccid2hctx_ssthresh = 2;
Andrea Bittau374bcf32006-09-19 13:14:43 -0700246 ccid2_change_cwnd(hctx, 1);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800247
248 /* clear state about stuff we sent */
249 hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqh;
250 hctx->ccid2hctx_ssacks = 0;
251 hctx->ccid2hctx_acks = 0;
252 hctx->ccid2hctx_sent = 0;
253
254 /* clear ack ratio state. */
255 hctx->ccid2hctx_arsent = 0;
256 hctx->ccid2hctx_ackloss = 0;
257 hctx->ccid2hctx_rpseq = 0;
258 hctx->ccid2hctx_rpdupack = -1;
259 ccid2_change_l_ack_ratio(sk, 1);
260 ccid2_hc_tx_check_sanity(hctx);
261out:
262 bh_unlock_sock(sk);
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800263 sock_put(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800264}
265
266static void ccid2_start_rto_timer(struct sock *sk)
267{
268 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
269
270 ccid2_pr_debug("setting RTO timeout=%ld\n", hctx->ccid2hctx_rto);
271
272 BUG_ON(timer_pending(&hctx->ccid2hctx_rtotimer));
273 sk_reset_timer(sk, &hctx->ccid2hctx_rtotimer,
274 jiffies + hctx->ccid2hctx_rto);
275}
276
277static void ccid2_hc_tx_packet_sent(struct sock *sk, int more, int len)
278{
279 struct dccp_sock *dp = dccp_sk(sk);
280 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
Andrea Bittau07978aa2006-09-19 13:13:37 -0700281 struct ccid2_seq *next;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800282 u64 seq;
283
284 ccid2_hc_tx_check_sanity(hctx);
285
286 BUG_ON(!hctx->ccid2hctx_sendwait);
287 hctx->ccid2hctx_sendwait = 0;
Andrea Bittau593f16a2006-09-19 13:15:33 -0700288 ccid2_change_pipe(hctx, hctx->ccid2hctx_pipe + 1);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800289 BUG_ON(hctx->ccid2hctx_pipe < 0);
290
291 /* There is an issue. What if another packet is sent between
292 * packet_send() and packet_sent(). Then the sequence number would be
293 * wrong.
294 * -sorbo.
295 */
296 seq = dp->dccps_gss;
297
298 hctx->ccid2hctx_seqh->ccid2s_seq = seq;
299 hctx->ccid2hctx_seqh->ccid2s_acked = 0;
300 hctx->ccid2hctx_seqh->ccid2s_sent = jiffies;
Andrea Bittau07978aa2006-09-19 13:13:37 -0700301
302 next = hctx->ccid2hctx_seqh->ccid2s_next;
303 /* check if we need to alloc more space */
304 if (next == hctx->ccid2hctx_seqt) {
305 int rc;
306
307 ccid2_pr_debug("allocating more space in history\n");
308 rc = ccid2_hc_tx_alloc_seq(hctx, CCID2_SEQBUF_LEN, GFP_KERNEL);
309 BUG_ON(rc); /* XXX what do we do? */
310
311 next = hctx->ccid2hctx_seqh->ccid2s_next;
312 BUG_ON(next == hctx->ccid2hctx_seqt);
313 }
314 hctx->ccid2hctx_seqh = next;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800315
316 ccid2_pr_debug("cwnd=%d pipe=%d\n", hctx->ccid2hctx_cwnd,
317 hctx->ccid2hctx_pipe);
318
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800319 hctx->ccid2hctx_sent++;
320
321 /* Ack Ratio. Need to maintain a concept of how many windows we sent */
322 hctx->ccid2hctx_arsent++;
323 /* We had an ack loss in this window... */
324 if (hctx->ccid2hctx_ackloss) {
325 if (hctx->ccid2hctx_arsent >= hctx->ccid2hctx_cwnd) {
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800326 hctx->ccid2hctx_arsent = 0;
327 hctx->ccid2hctx_ackloss = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800328 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800329 } else {
330 /* No acks lost up to now... */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800331 /* decrease ack ratio if enough packets were sent */
332 if (dp->dccps_l_ack_ratio > 1) {
333 /* XXX don't calculate denominator each time */
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800334 int denom = dp->dccps_l_ack_ratio * dp->dccps_l_ack_ratio -
335 dp->dccps_l_ack_ratio;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800336
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800337 denom = hctx->ccid2hctx_cwnd * hctx->ccid2hctx_cwnd / denom;
338
339 if (hctx->ccid2hctx_arsent >= denom) {
340 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio - 1);
341 hctx->ccid2hctx_arsent = 0;
342 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800343 } else {
344 /* we can't increase ack ratio further [1] */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800345 hctx->ccid2hctx_arsent = 0; /* or maybe set it to cwnd*/
346 }
347 }
348
349 /* setup RTO timer */
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800350 if (!timer_pending(&hctx->ccid2hctx_rtotimer))
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800351 ccid2_start_rto_timer(sk);
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800352
Andrea Bittau8d424f62006-09-19 13:12:44 -0700353#ifdef CONFIG_IP_DCCP_CCID2_DEBUG
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800354 ccid2_pr_debug("pipe=%d\n", hctx->ccid2hctx_pipe);
Randy Dunlap234af482006-10-29 16:03:30 -0800355 ccid2_pr_debug("Sent: seq=%llu\n", (unsigned long long)seq);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800356 do {
357 struct ccid2_seq *seqp = hctx->ccid2hctx_seqt;
358
359 while (seqp != hctx->ccid2hctx_seqh) {
360 ccid2_pr_debug("out seq=%llu acked=%d time=%lu\n",
Randy Dunlap234af482006-10-29 16:03:30 -0800361 (unsigned long long)seqp->ccid2s_seq,
362 seqp->ccid2s_acked, seqp->ccid2s_sent);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800363 seqp = seqp->ccid2s_next;
364 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800365 } while (0);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800366 ccid2_pr_debug("=========\n");
367 ccid2_hc_tx_check_sanity(hctx);
368#endif
369}
370
371/* XXX Lame code duplication!
372 * returns -1 if none was found.
373 * else returns the next offset to use in the function call.
374 */
375static int ccid2_ackvector(struct sock *sk, struct sk_buff *skb, int offset,
376 unsigned char **vec, unsigned char *veclen)
377{
378 const struct dccp_hdr *dh = dccp_hdr(skb);
379 unsigned char *options = (unsigned char *)dh + dccp_hdr_len(skb);
380 unsigned char *opt_ptr;
381 const unsigned char *opt_end = (unsigned char *)dh +
382 (dh->dccph_doff * 4);
383 unsigned char opt, len;
384 unsigned char *value;
385
386 BUG_ON(offset < 0);
387 options += offset;
388 opt_ptr = options;
389 if (opt_ptr >= opt_end)
390 return -1;
391
392 while (opt_ptr != opt_end) {
393 opt = *opt_ptr++;
394 len = 0;
395 value = NULL;
396
397 /* Check if this isn't a single byte option */
398 if (opt > DCCPO_MAX_RESERVED) {
399 if (opt_ptr == opt_end)
400 goto out_invalid_option;
401
402 len = *opt_ptr++;
403 if (len < 3)
404 goto out_invalid_option;
405 /*
406 * Remove the type and len fields, leaving
407 * just the value size
408 */
409 len -= 2;
410 value = opt_ptr;
411 opt_ptr += len;
412
413 if (opt_ptr > opt_end)
414 goto out_invalid_option;
415 }
416
417 switch (opt) {
418 case DCCPO_ACK_VECTOR_0:
419 case DCCPO_ACK_VECTOR_1:
420 *vec = value;
421 *veclen = len;
422 return offset + (opt_ptr - options);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800423 }
424 }
425
426 return -1;
427
428out_invalid_option:
429 BUG_ON(1); /* should never happen... options were previously parsed ! */
430 return -1;
431}
432
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800433static void ccid2_hc_tx_kill_rto_timer(struct sock *sk)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800434{
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800435 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
436
437 sk_stop_timer(sk, &hctx->ccid2hctx_rtotimer);
438 ccid2_pr_debug("deleted RTO timer\n");
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800439}
440
441static inline void ccid2_new_ack(struct sock *sk,
442 struct ccid2_seq *seqp,
443 unsigned int *maxincr)
444{
445 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
446
447 /* slow start */
448 if (hctx->ccid2hctx_cwnd < hctx->ccid2hctx_ssthresh) {
449 hctx->ccid2hctx_acks = 0;
450
451 /* We can increase cwnd at most maxincr [ack_ratio/2] */
452 if (*maxincr) {
453 /* increase every 2 acks */
454 hctx->ccid2hctx_ssacks++;
455 if (hctx->ccid2hctx_ssacks == 2) {
Andrea Bittau374bcf32006-09-19 13:14:43 -0700456 ccid2_change_cwnd(hctx, hctx->ccid2hctx_cwnd+1);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800457 hctx->ccid2hctx_ssacks = 0;
458 *maxincr = *maxincr - 1;
459 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800460 } else {
461 /* increased cwnd enough for this single ack */
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800462 hctx->ccid2hctx_ssacks = 0;
463 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800464 } else {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800465 hctx->ccid2hctx_ssacks = 0;
466 hctx->ccid2hctx_acks++;
467
468 if (hctx->ccid2hctx_acks >= hctx->ccid2hctx_cwnd) {
Andrea Bittau374bcf32006-09-19 13:14:43 -0700469 ccid2_change_cwnd(hctx, hctx->ccid2hctx_cwnd + 1);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800470 hctx->ccid2hctx_acks = 0;
471 }
472 }
473
474 /* update RTO */
475 if (hctx->ccid2hctx_srtt == -1 ||
Andrea Bittau29651cd2006-09-19 13:06:46 -0700476 time_after(jiffies, hctx->ccid2hctx_lastrtt + hctx->ccid2hctx_srtt)) {
477 unsigned long r = (long)jiffies - (long)seqp->ccid2s_sent;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800478 int s;
479
480 /* first measurement */
481 if (hctx->ccid2hctx_srtt == -1) {
482 ccid2_pr_debug("R: %lu Time=%lu seq=%llu\n",
Randy Dunlap234af482006-10-29 16:03:30 -0800483 r, jiffies,
484 (unsigned long long)seqp->ccid2s_seq);
Andrea Bittau593f16a2006-09-19 13:15:33 -0700485 ccid2_change_srtt(hctx, r);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800486 hctx->ccid2hctx_rttvar = r >> 1;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800487 } else {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800488 /* RTTVAR */
489 long tmp = hctx->ccid2hctx_srtt - r;
Andrea Bittau593f16a2006-09-19 13:15:33 -0700490 long srtt;
491
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800492 if (tmp < 0)
493 tmp *= -1;
494
495 tmp >>= 2;
496 hctx->ccid2hctx_rttvar *= 3;
497 hctx->ccid2hctx_rttvar >>= 2;
498 hctx->ccid2hctx_rttvar += tmp;
499
500 /* SRTT */
Andrea Bittau593f16a2006-09-19 13:15:33 -0700501 srtt = hctx->ccid2hctx_srtt;
502 srtt *= 7;
503 srtt >>= 3;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800504 tmp = r >> 3;
Andrea Bittau593f16a2006-09-19 13:15:33 -0700505 srtt += tmp;
506 ccid2_change_srtt(hctx, srtt);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800507 }
508 s = hctx->ccid2hctx_rttvar << 2;
509 /* clock granularity is 1 when based on jiffies */
510 if (!s)
511 s = 1;
512 hctx->ccid2hctx_rto = hctx->ccid2hctx_srtt + s;
513
514 /* must be at least a second */
515 s = hctx->ccid2hctx_rto / HZ;
516 /* DCCP doesn't require this [but I like it cuz my code sux] */
517#if 1
518 if (s < 1)
519 hctx->ccid2hctx_rto = HZ;
520#endif
521 /* max 60 seconds */
522 if (s > 60)
523 hctx->ccid2hctx_rto = HZ * 60;
524
525 hctx->ccid2hctx_lastrtt = jiffies;
526
527 ccid2_pr_debug("srtt: %ld rttvar: %ld rto: %ld (HZ=%d) R=%lu\n",
528 hctx->ccid2hctx_srtt, hctx->ccid2hctx_rttvar,
529 hctx->ccid2hctx_rto, HZ, r);
530 hctx->ccid2hctx_sent = 0;
531 }
532
533 /* we got a new ack, so re-start RTO timer */
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800534 ccid2_hc_tx_kill_rto_timer(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800535 ccid2_start_rto_timer(sk);
536}
537
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800538static void ccid2_hc_tx_dec_pipe(struct sock *sk)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800539{
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800540 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
541
Andrea Bittau593f16a2006-09-19 13:15:33 -0700542 ccid2_change_pipe(hctx, hctx->ccid2hctx_pipe-1);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800543 BUG_ON(hctx->ccid2hctx_pipe < 0);
544
545 if (hctx->ccid2hctx_pipe == 0)
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800546 ccid2_hc_tx_kill_rto_timer(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800547}
548
Andrea Bittau374bcf32006-09-19 13:14:43 -0700549static void ccid2_congestion_event(struct ccid2_hc_tx_sock *hctx,
550 struct ccid2_seq *seqp)
551{
552 if (time_before(seqp->ccid2s_sent, hctx->ccid2hctx_last_cong)) {
553 ccid2_pr_debug("Multiple losses in an RTT---treating as one\n");
554 return;
555 }
556
557 hctx->ccid2hctx_last_cong = jiffies;
558
559 ccid2_change_cwnd(hctx, hctx->ccid2hctx_cwnd >> 1);
560 hctx->ccid2hctx_ssthresh = hctx->ccid2hctx_cwnd;
561 if (hctx->ccid2hctx_ssthresh < 2)
562 hctx->ccid2hctx_ssthresh = 2;
563}
564
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800565static void ccid2_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
566{
567 struct dccp_sock *dp = dccp_sk(sk);
568 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
569 u64 ackno, seqno;
570 struct ccid2_seq *seqp;
571 unsigned char *vector;
572 unsigned char veclen;
573 int offset = 0;
574 int done = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800575 unsigned int maxincr = 0;
576
577 ccid2_hc_tx_check_sanity(hctx);
578 /* check reverse path congestion */
579 seqno = DCCP_SKB_CB(skb)->dccpd_seq;
580
581 /* XXX this whole "algorithm" is broken. Need to fix it to keep track
582 * of the seqnos of the dupacks so that rpseq and rpdupack are correct
583 * -sorbo.
584 */
585 /* need to bootstrap */
586 if (hctx->ccid2hctx_rpdupack == -1) {
587 hctx->ccid2hctx_rpdupack = 0;
588 hctx->ccid2hctx_rpseq = seqno;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800589 } else {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800590 /* check if packet is consecutive */
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800591 if ((hctx->ccid2hctx_rpseq + 1) == seqno)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800592 hctx->ccid2hctx_rpseq++;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800593 /* it's a later packet */
594 else if (after48(seqno, hctx->ccid2hctx_rpseq)) {
595 hctx->ccid2hctx_rpdupack++;
596
597 /* check if we got enough dupacks */
598 if (hctx->ccid2hctx_rpdupack >=
599 hctx->ccid2hctx_numdupack) {
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800600 hctx->ccid2hctx_rpdupack = -1; /* XXX lame */
601 hctx->ccid2hctx_rpseq = 0;
602
603 ccid2_change_l_ack_ratio(sk, dp->dccps_l_ack_ratio << 1);
604 }
605 }
606 }
607
608 /* check forward path congestion */
609 /* still didn't send out new data packets */
610 if (hctx->ccid2hctx_seqh == hctx->ccid2hctx_seqt)
611 return;
612
613 switch (DCCP_SKB_CB(skb)->dccpd_type) {
614 case DCCP_PKT_ACK:
615 case DCCP_PKT_DATAACK:
616 break;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800617 default:
618 return;
619 }
620
621 ackno = DCCP_SKB_CB(skb)->dccpd_ack_seq;
622 seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
623
624 /* If in slow-start, cwnd can increase at most Ack Ratio / 2 packets for
625 * this single ack. I round up.
626 * -sorbo.
627 */
628 maxincr = dp->dccps_l_ack_ratio >> 1;
629 maxincr++;
630
631 /* go through all ack vectors */
632 while ((offset = ccid2_ackvector(sk, skb, offset,
633 &vector, &veclen)) != -1) {
634 /* go through this ack vector */
635 while (veclen--) {
636 const u8 rl = *vector & DCCP_ACKVEC_LEN_MASK;
637 u64 ackno_end_rl;
638
639 dccp_set_seqno(&ackno_end_rl, ackno - rl);
Randy Dunlap234af482006-10-29 16:03:30 -0800640 ccid2_pr_debug("ackvec start:%llu end:%llu\n",
641 (unsigned long long)ackno,
642 (unsigned long long)ackno_end_rl);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800643 /* if the seqno we are analyzing is larger than the
644 * current ackno, then move towards the tail of our
645 * seqnos.
646 */
647 while (after48(seqp->ccid2s_seq, ackno)) {
648 if (seqp == hctx->ccid2hctx_seqt) {
649 done = 1;
650 break;
651 }
652 seqp = seqp->ccid2s_prev;
653 }
654 if (done)
655 break;
656
657 /* check all seqnos in the range of the vector
658 * run length
659 */
660 while (between48(seqp->ccid2s_seq,ackno_end_rl,ackno)) {
Andrea Bittau8e27e462006-09-19 13:05:35 -0700661 const u8 state = *vector &
662 DCCP_ACKVEC_STATE_MASK;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800663
664 /* new packet received or marked */
665 if (state != DCCP_ACKVEC_STATE_NOT_RECEIVED &&
666 !seqp->ccid2s_acked) {
667 if (state ==
668 DCCP_ACKVEC_STATE_ECN_MARKED) {
Andrea Bittau374bcf32006-09-19 13:14:43 -0700669 ccid2_congestion_event(hctx,
670 seqp);
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800671 } else
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800672 ccid2_new_ack(sk, seqp,
673 &maxincr);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800674
675 seqp->ccid2s_acked = 1;
676 ccid2_pr_debug("Got ack for %llu\n",
Randy Dunlap234af482006-10-29 16:03:30 -0800677 (unsigned long long)seqp->ccid2s_seq);
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800678 ccid2_hc_tx_dec_pipe(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800679 }
680 if (seqp == hctx->ccid2hctx_seqt) {
681 done = 1;
682 break;
683 }
684 seqp = seqp->ccid2s_next;
685 }
686 if (done)
687 break;
688
689
690 dccp_set_seqno(&ackno, ackno_end_rl - 1);
691 vector++;
692 }
693 if (done)
694 break;
695 }
696
697 /* The state about what is acked should be correct now
698 * Check for NUMDUPACK
699 */
700 seqp = hctx->ccid2hctx_seqh->ccid2s_prev;
701 done = 0;
702 while (1) {
703 if (seqp->ccid2s_acked) {
704 done++;
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800705 if (done == hctx->ccid2hctx_numdupack)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800706 break;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800707 }
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800708 if (seqp == hctx->ccid2hctx_seqt)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800709 break;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800710 seqp = seqp->ccid2s_prev;
711 }
712
713 /* If there are at least 3 acknowledgements, anything unacknowledged
714 * below the last sequence number is considered lost
715 */
716 if (done == hctx->ccid2hctx_numdupack) {
717 struct ccid2_seq *last_acked = seqp;
718
719 /* check for lost packets */
720 while (1) {
721 if (!seqp->ccid2s_acked) {
Andrea Bittau374bcf32006-09-19 13:14:43 -0700722 ccid2_pr_debug("Packet lost: %llu\n",
Randy Dunlap234af482006-10-29 16:03:30 -0800723 (unsigned long long)seqp->ccid2s_seq);
Andrea Bittau374bcf32006-09-19 13:14:43 -0700724 /* XXX need to traverse from tail -> head in
725 * order to detect multiple congestion events in
726 * one ack vector.
727 */
728 ccid2_congestion_event(hctx, seqp);
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800729 ccid2_hc_tx_dec_pipe(sk);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800730 }
731 if (seqp == hctx->ccid2hctx_seqt)
732 break;
733 seqp = seqp->ccid2s_prev;
734 }
735
736 hctx->ccid2hctx_seqt = last_acked;
737 }
738
739 /* trim acked packets in tail */
740 while (hctx->ccid2hctx_seqt != hctx->ccid2hctx_seqh) {
741 if (!hctx->ccid2hctx_seqt->ccid2s_acked)
742 break;
743
744 hctx->ccid2hctx_seqt = hctx->ccid2hctx_seqt->ccid2s_next;
745 }
746
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800747 ccid2_hc_tx_check_sanity(hctx);
748}
749
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800750static int ccid2_hc_tx_init(struct ccid *ccid, struct sock *sk)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800751{
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800752 struct ccid2_hc_tx_sock *hctx = ccid_priv(ccid);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800753
Andrea Bittau374bcf32006-09-19 13:14:43 -0700754 ccid2_change_cwnd(hctx, 1);
Andrea Bittaud458c252006-09-19 13:07:20 -0700755 /* Initialize ssthresh to infinity. This means that we will exit the
756 * initial slow-start after the first packet loss. This is what we
757 * want.
758 */
759 hctx->ccid2hctx_ssthresh = ~0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800760 hctx->ccid2hctx_numdupack = 3;
Andrea Bittau07978aa2006-09-19 13:13:37 -0700761 hctx->ccid2hctx_seqbufc = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800762
763 /* XXX init ~ to window size... */
Andrea Bittau07978aa2006-09-19 13:13:37 -0700764 if (ccid2_hc_tx_alloc_seq(hctx, CCID2_SEQBUF_LEN, GFP_ATOMIC) != 0)
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800765 return -ENOMEM;
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800766
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800767 hctx->ccid2hctx_sent = 0;
768 hctx->ccid2hctx_rto = 3 * HZ;
Andrea Bittau593f16a2006-09-19 13:15:33 -0700769 ccid2_change_srtt(hctx, -1);
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800770 hctx->ccid2hctx_rttvar = -1;
771 hctx->ccid2hctx_lastrtt = 0;
772 hctx->ccid2hctx_rpdupack = -1;
Andrea Bittau374bcf32006-09-19 13:14:43 -0700773 hctx->ccid2hctx_last_cong = jiffies;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800774
775 hctx->ccid2hctx_rtotimer.function = &ccid2_hc_tx_rto_expire;
776 hctx->ccid2hctx_rtotimer.data = (unsigned long)sk;
777 init_timer(&hctx->ccid2hctx_rtotimer);
778
779 ccid2_hc_tx_check_sanity(hctx);
780 return 0;
781}
782
783static void ccid2_hc_tx_exit(struct sock *sk)
784{
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800785 struct ccid2_hc_tx_sock *hctx = ccid2_hc_tx_sk(sk);
Andrea Bittau07978aa2006-09-19 13:13:37 -0700786 int i;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800787
Andrea Bittau77ff72d2006-03-20 17:57:52 -0800788 ccid2_hc_tx_kill_rto_timer(sk);
Andrea Bittau07978aa2006-09-19 13:13:37 -0700789
790 for (i = 0; i < hctx->ccid2hctx_seqbufc; i++)
791 kfree(hctx->ccid2hctx_seqbuf[i]);
792 hctx->ccid2hctx_seqbufc = 0;
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800793}
794
795static void ccid2_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
796{
797 const struct dccp_sock *dp = dccp_sk(sk);
798 struct ccid2_hc_rx_sock *hcrx = ccid2_hc_rx_sk(sk);
799
800 switch (DCCP_SKB_CB(skb)->dccpd_type) {
801 case DCCP_PKT_DATA:
802 case DCCP_PKT_DATAACK:
803 hcrx->ccid2hcrx_data++;
804 if (hcrx->ccid2hcrx_data >= dp->dccps_r_ack_ratio) {
805 dccp_send_ack(sk);
806 hcrx->ccid2hcrx_data = 0;
807 }
808 break;
809 }
810}
811
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800812static struct ccid_operations ccid2 = {
Ian McDonald3dd9a7c2006-09-22 14:26:44 +1200813 .ccid_id = DCCPC_CCID2,
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800814 .ccid_name = "ccid2",
815 .ccid_owner = THIS_MODULE,
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800816 .ccid_hc_tx_obj_size = sizeof(struct ccid2_hc_tx_sock),
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800817 .ccid_hc_tx_init = ccid2_hc_tx_init,
818 .ccid_hc_tx_exit = ccid2_hc_tx_exit,
819 .ccid_hc_tx_send_packet = ccid2_hc_tx_send_packet,
820 .ccid_hc_tx_packet_sent = ccid2_hc_tx_packet_sent,
821 .ccid_hc_tx_packet_recv = ccid2_hc_tx_packet_recv,
Arnaldo Carvalho de Melo91f0ebf2006-03-20 19:21:44 -0800822 .ccid_hc_rx_obj_size = sizeof(struct ccid2_hc_rx_sock),
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800823 .ccid_hc_rx_packet_recv = ccid2_hc_rx_packet_recv,
824};
825
826module_param(ccid2_debug, int, 0444);
827MODULE_PARM_DESC(ccid2_debug, "Enable debug messages");
828
829static __init int ccid2_module_init(void)
830{
831 return ccid_register(&ccid2);
832}
833module_init(ccid2_module_init);
834
835static __exit void ccid2_module_exit(void)
836{
837 ccid_unregister(&ccid2);
838}
839module_exit(ccid2_module_exit);
840
841MODULE_AUTHOR("Andrea Bittau <a.bittau@cs.ucl.ac.uk>");
Arnaldo Carvalho de Meloc0c736d2006-03-20 22:05:37 -0800842MODULE_DESCRIPTION("DCCP TCP-Like (CCID2) CCID");
Andrea Bittau2a91aa32006-03-20 17:41:47 -0800843MODULE_LICENSE("GPL");
844MODULE_ALIAS("net-dccp-ccid-2");