Merge master.kernel.org:/home/rmk/linux-2.6-mmc
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / dccp / ccids / ccid3.c
CommitLineData
7c657876
ACM
1/*
2 * net/dccp/ccids/ccid3.c
3 *
4 * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
1bc09869 5 * Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz>
7c657876
ACM
6 *
7 * An implementation of the DCCP protocol
8 *
9 * This code has been developed by the University of Waikato WAND
10 * research group. For further information please see http://www.wand.net.nz/
7c657876
ACM
11 *
12 * This code also uses code from Lulea University, rereleased as GPL by its
13 * authors:
14 * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
15 *
16 * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
17 * and to make it work as a loadable module in the DCCP stack written by
18 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
19 *
20 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
21 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 */
36
8c60f3fa 37#include <linux/config.h>
7c657876
ACM
38#include "../ccid.h"
39#include "../dccp.h"
4524b259 40#include "lib/packet_history.h"
ae6706f0 41#include "lib/loss_interval.h"
36729c1a 42#include "lib/tfrc.h"
7c657876
ACM
43#include "ccid3.h"
44
6b5e633a 45/*
0ba7a3ba
ACM
46 * Reason for maths here is to avoid 32 bit overflow when a is big.
47 * With this we get close to the limit.
6b5e633a
ACM
48 */
49static inline u32 usecs_div(const u32 a, const u32 b)
50{
0ba7a3ba
ACM
51 const u32 div = a < (UINT_MAX / (USEC_PER_SEC / 10)) ? 10 :
52 a < (UINT_MAX / (USEC_PER_SEC / 50)) ? 50 :
53 a < (UINT_MAX / (USEC_PER_SEC / 100)) ? 100 :
54 a < (UINT_MAX / (USEC_PER_SEC / 500)) ? 500 :
55 a < (UINT_MAX / (USEC_PER_SEC / 1000)) ? 1000 :
56 a < (UINT_MAX / (USEC_PER_SEC / 5000)) ? 5000 :
57 a < (UINT_MAX / (USEC_PER_SEC / 10000)) ? 10000 :
58 a < (UINT_MAX / (USEC_PER_SEC / 50000)) ? 50000 :
59 100000;
60 const u32 tmp = a * (USEC_PER_SEC / div);
61 return (b >= 2 * div) ? tmp / (b / div) : tmp;
6b5e633a
ACM
62}
63
cfc3c525 64static int ccid3_debug;
7c657876 65
cfc3c525 66#ifdef CCID3_DEBUG
7c657876
ACM
67#define ccid3_pr_debug(format, a...) \
68 do { if (ccid3_debug) \
69 printk(KERN_DEBUG "%s: " format, __FUNCTION__, ##a); \
70 } while (0)
71#else
72#define ccid3_pr_debug(format, a...)
73#endif
74
a1d3a355
ACM
75static struct dccp_tx_hist *ccid3_tx_hist;
76static struct dccp_rx_hist *ccid3_rx_hist;
ae6706f0 77static struct dccp_li_hist *ccid3_li_hist;
7c657876
ACM
78
79static int ccid3_init(struct sock *sk)
80{
7c657876
ACM
81 return 0;
82}
83
84static void ccid3_exit(struct sock *sk)
85{
7c657876
ACM
86}
87
88/* TFRC sender states */
89enum ccid3_hc_tx_states {
90 TFRC_SSTATE_NO_SENT = 1,
91 TFRC_SSTATE_NO_FBACK,
92 TFRC_SSTATE_FBACK,
93 TFRC_SSTATE_TERM,
94};
95
96#ifdef CCID3_DEBUG
97static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
98{
99 static char *ccid3_state_names[] = {
100 [TFRC_SSTATE_NO_SENT] = "NO_SENT",
101 [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
102 [TFRC_SSTATE_FBACK] = "FBACK",
103 [TFRC_SSTATE_TERM] = "TERM",
104 };
105
106 return ccid3_state_names[state];
107}
108#endif
109
1f2333ae
ACM
110static inline void ccid3_hc_tx_set_state(struct sock *sk,
111 enum ccid3_hc_tx_states state)
7c657876 112{
59725dc2 113 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
7c657876
ACM
114 enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
115
116 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
1f2333ae
ACM
117 dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
118 ccid3_tx_state_name(state));
7c657876
ACM
119 WARN_ON(state == oldstate);
120 hctx->ccid3hctx_state = state;
121}
122
7c657876
ACM
123/* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
124static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
125{
6b5e633a
ACM
126 /*
127 * If no feedback spec says t_ipi is 1 second (set elsewhere and then
128 * doubles after every no feedback timer (separate function)
129 */
130 if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
131 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
132 hctx->ccid3hctx_x);
7c657876
ACM
133}
134
135/* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
136static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
137{
1f2333ae
ACM
138 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
139 TFRC_OPSYS_HALF_TIME_GRAN);
7c657876
ACM
140}
141
142/*
143 * Update X by
144 * If (p > 0)
145 * x_calc = calcX(s, R, p);
146 * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
147 * Else
148 * If (now - tld >= R)
149 * X = max(min(2 * X, 2 * X_recv), s / R);
150 * tld = now;
151 */
152static void ccid3_hc_tx_update_x(struct sock *sk)
153{
59725dc2 154 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
7c657876 155
1f2333ae
ACM
156 /* To avoid large error in calcX */
157 if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
36729c1a
ACM
158 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
159 hctx->ccid3hctx_rtt,
160 hctx->ccid3hctx_p);
1f2333ae
ACM
161 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
162 2 * hctx->ccid3hctx_x_recv),
163 (hctx->ccid3hctx_s /
164 TFRC_MAX_BACK_OFF_TIME));
b6ee3d4a
ACM
165 } else {
166 struct timeval now;
167
b0e56780 168 dccp_timestamp(sk, &now);
b6ee3d4a
ACM
169 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
170 hctx->ccid3hctx_rtt) {
6b5e633a
ACM
171 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
172 hctx->ccid3hctx_x) * 2,
173 usecs_div(hctx->ccid3hctx_s,
174 hctx->ccid3hctx_rtt));
b6ee3d4a
ACM
175 hctx->ccid3hctx_t_ld = now;
176 }
7c657876 177 }
7c657876
ACM
178}
179
180static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
181{
182 struct sock *sk = (struct sock *)data;
7c657876 183 unsigned long next_tmout = 0;
59725dc2 184 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
7c657876
ACM
185
186 bh_lock_sock(sk);
187 if (sock_owned_by_user(sk)) {
188 /* Try again later. */
189 /* XXX: set some sensible MIB */
1f2333ae
ACM
190 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
191 jiffies + HZ / 5);
7c657876
ACM
192 goto out;
193 }
194
195 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
196 ccid3_tx_state_name(hctx->ccid3hctx_state));
197
7c657876
ACM
198 switch (hctx->ccid3hctx_state) {
199 case TFRC_SSTATE_TERM:
200 goto out;
201 case TFRC_SSTATE_NO_FBACK:
202 /* Halve send rate */
203 hctx->ccid3hctx_x /= 2;
6b5e633a
ACM
204 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
205 TFRC_MAX_BACK_OFF_TIME))
1f2333ae
ACM
206 hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
207 TFRC_MAX_BACK_OFF_TIME);
7c657876 208
1f2333ae
ACM
209 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
210 "bytes/s\n",
211 dccp_role(sk), sk,
212 ccid3_tx_state_name(hctx->ccid3hctx_state),
7c657876 213 hctx->ccid3hctx_x);
6b5e633a
ACM
214 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
215 hctx->ccid3hctx_x),
1f2333ae 216 TFRC_INITIAL_TIMEOUT);
1f2333ae
ACM
217 /*
218 * FIXME - not sure above calculation is correct. See section
219 * 5 of CCID3 11 should adjust tx_t_ipi and double that to
220 * achieve it really
221 */
7c657876
ACM
222 break;
223 case TFRC_SSTATE_FBACK:
1f2333ae
ACM
224 /*
225 * Check if IDLE since last timeout and recv rate is less than
226 * 4 packets per RTT
227 */
1f2333ae 228 if (!hctx->ccid3hctx_idle ||
6b5e633a
ACM
229 (hctx->ccid3hctx_x_recv >=
230 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
1f2333ae
ACM
231 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
232 dccp_role(sk), sk,
7c657876
ACM
233 ccid3_tx_state_name(hctx->ccid3hctx_state));
234 /* Halve sending rate */
235
236 /* If (X_calc > 2 * X_recv)
237 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
238 * Else
239 * X_recv = X_calc / 4;
240 */
1f2333ae
ACM
241 BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
242 hctx->ccid3hctx_x_calc == 0);
7c657876
ACM
243
244 /* check also if p is zero -> x_calc is infinity? */
245 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
246 hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
247 hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
248 hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
249 else
250 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
251
252 /* Update sending rate */
253 ccid3_hc_tx_update_x(sk);
254 }
6b5e633a
ACM
255 /*
256 * Schedule no feedback timer to expire in
257 * max(4 * R, 2 * s / X)
258 */
c68e64cf 259 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
6b5e633a
ACM
260 2 * usecs_div(hctx->ccid3hctx_s,
261 hctx->ccid3hctx_x));
7c657876
ACM
262 break;
263 default:
264 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
265 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
266 dump_stack();
267 goto out;
268 }
269
270 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
1f2333ae 271 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
7c657876
ACM
272 hctx->ccid3hctx_idle = 1;
273out:
274 bh_unlock_sock(sk);
275 sock_put(sk);
276}
277
27258ee5
ACM
278static int ccid3_hc_tx_send_packet(struct sock *sk,
279 struct sk_buff *skb, int len)
7c657876
ACM
280{
281 struct dccp_sock *dp = dccp_sk(sk);
59725dc2 282 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
8c60f3fa 283 struct dccp_tx_hist_entry *new_packet;
7c657876 284 struct timeval now;
27258ee5 285 long delay;
7c657876
ACM
286 int rc = -ENOTCONN;
287
59d203f9 288 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
1f2333ae 289
59d203f9 290 /* Check if pure ACK or Terminating*/
7c657876 291 /*
1f2333ae
ACM
292 * XXX: We only call this function for DATA and DATAACK, on, these
293 * packets can have zero length, but why the comment about "pure ACK"?
7c657876 294 */
59d203f9 295 if (unlikely(len == 0))
7c657876
ACM
296 goto out;
297
298 /* See if last packet allocated was not sent */
8c60f3fa
ACM
299 new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
300 if (new_packet == NULL || new_packet->dccphtx_sent) {
1f2333ae
ACM
301 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
302 SLAB_ATOMIC);
7c657876
ACM
303
304 rc = -ENOBUFS;
59d203f9
ACM
305 if (unlikely(new_packet == NULL)) {
306 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, not enough "
307 "mem to add to history, send refused\n",
308 __FUNCTION__, dccp_role(sk), sk);
7c657876
ACM
309 goto out;
310 }
311
8c60f3fa 312 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
7c657876
ACM
313 }
314
b0e56780 315 dccp_timestamp(sk, &now);
7c657876
ACM
316
317 switch (hctx->ccid3hctx_state) {
318 case TFRC_SSTATE_NO_SENT:
7c657876
ACM
319 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
320 hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
1f2333ae
ACM
321 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
322 jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
7c657876
ACM
323 hctx->ccid3hctx_last_win_count = 0;
324 hctx->ccid3hctx_t_last_win_count = now;
325 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
d7e0fb98 326 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_IPI;
7c657876
ACM
327
328 /* Set nominal send time for initial packet */
329 hctx->ccid3hctx_t_nom = now;
b6ee3d4a
ACM
330 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
331 hctx->ccid3hctx_t_ipi);
7c657876
ACM
332 ccid3_calc_new_delta(hctx);
333 rc = 0;
334 break;
335 case TFRC_SSTATE_NO_FBACK:
336 case TFRC_SSTATE_FBACK:
b6ee3d4a
ACM
337 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
338 hctx->ccid3hctx_delta);
27258ee5 339 delay /= -1000;
7c657876 340 /* divide by -1000 is to convert to ms and get sign right */
d6809c12 341 rc = delay > 0 ? delay : 0;
7c657876
ACM
342 break;
343 default:
344 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
345 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
346 dump_stack();
347 rc = -EINVAL;
348 break;
349 }
350
351 /* Can we send? if so add options and add to packet history */
507d37cf
ACM
352 if (rc == 0) {
353 dp->dccps_hc_tx_insert_options = 1;
c1734376 354 new_packet->dccphtx_ccval =
8c60f3fa
ACM
355 DCCP_SKB_CB(skb)->dccpd_ccval =
356 hctx->ccid3hctx_last_win_count;
507d37cf 357 }
7c657876
ACM
358out:
359 return rc;
360}
361
362static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
363{
59725dc2
ACM
364 const struct dccp_sock *dp = dccp_sk(sk);
365 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
7c657876
ACM
366 struct timeval now;
367
59d203f9 368 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
7c657876 369
b0e56780 370 dccp_timestamp(sk, &now);
7c657876
ACM
371
372 /* check if we have sent a data packet */
373 if (len > 0) {
374 unsigned long quarter_rtt;
8c60f3fa 375 struct dccp_tx_hist_entry *packet;
7c657876 376
8c60f3fa 377 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
59d203f9
ACM
378 if (unlikely(packet == NULL)) {
379 LIMIT_NETDEBUG(KERN_WARNING "%s: packet doesn't "
380 "exists in history!\n", __FUNCTION__);
7c657876
ACM
381 return;
382 }
59d203f9
ACM
383 if (unlikely(packet->dccphtx_sent)) {
384 LIMIT_NETDEBUG(KERN_WARNING "%s: no unsent packet in "
385 "history!\n", __FUNCTION__);
7c657876
ACM
386 return;
387 }
8c60f3fa
ACM
388 packet->dccphtx_tstamp = now;
389 packet->dccphtx_seqno = dp->dccps_gss;
7c657876 390 /*
1f2333ae
ACM
391 * Check if win_count have changed
392 * Algorithm in "8.1. Window Counter Valuer" in
393 * draft-ietf-dccp-ccid3-11.txt
7c657876 394 */
6b5e633a
ACM
395 quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
396 if (likely(hctx->ccid3hctx_rtt > 8))
397 quarter_rtt /= hctx->ccid3hctx_rtt / 4;
398
7c657876
ACM
399 if (quarter_rtt > 0) {
400 hctx->ccid3hctx_t_last_win_count = now;
401 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count +
402 min_t(unsigned long, quarter_rtt, 5)) % 16;
1f2333ae
ACM
403 ccid3_pr_debug("%s, sk=%p, window changed from "
404 "%u to %u!\n",
7c657876 405 dccp_role(sk), sk,
c1734376 406 packet->dccphtx_ccval,
7c657876
ACM
407 hctx->ccid3hctx_last_win_count);
408 }
1f2333ae 409
7c657876 410 hctx->ccid3hctx_idle = 0;
c1734376 411 packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
8c60f3fa 412 packet->dccphtx_sent = 1;
7c657876
ACM
413 } else
414 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
415 dccp_role(sk), sk, dp->dccps_gss);
416
417 switch (hctx->ccid3hctx_state) {
418 case TFRC_SSTATE_NO_SENT:
419 /* if first wasn't pure ack */
420 if (len != 0)
1f2333ae
ACM
421 printk(KERN_CRIT "%s: %s, First packet sent is noted "
422 "as a data packet\n",
7c657876
ACM
423 __FUNCTION__, dccp_role(sk));
424 return;
425 case TFRC_SSTATE_NO_FBACK:
426 case TFRC_SSTATE_FBACK:
427 if (len > 0) {
428 hctx->ccid3hctx_t_nom = now;
429 ccid3_calc_new_t_ipi(hctx);
430 ccid3_calc_new_delta(hctx);
b6ee3d4a
ACM
431 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
432 hctx->ccid3hctx_t_ipi);
7c657876
ACM
433 }
434 break;
435 default:
436 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
437 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
438 dump_stack();
439 break;
440 }
441}
442
443static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
444{
59725dc2
ACM
445 const struct dccp_sock *dp = dccp_sk(sk);
446 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
7c657876 447 struct ccid3_options_received *opt_recv;
8c60f3fa 448 struct dccp_tx_hist_entry *packet;
b0e56780 449 struct timeval now;
7c657876 450 unsigned long next_tmout;
1bc09869 451 u32 t_elapsed;
7c657876
ACM
452 u32 pinv;
453 u32 x_recv;
454 u32 r_sample;
1f2333ae 455
59d203f9 456 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
7c657876
ACM
457
458 /* we are only interested in ACKs */
459 if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
460 DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
461 return;
462
463 opt_recv = &hctx->ccid3hctx_options_received;
464
1a28599a 465 t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10;
7c657876
ACM
466 x_recv = opt_recv->ccid3or_receive_rate;
467 pinv = opt_recv->ccid3or_loss_event_rate;
468
469 switch (hctx->ccid3hctx_state) {
470 case TFRC_SSTATE_NO_SENT:
471 /* FIXME: what to do here? */
472 return;
473 case TFRC_SSTATE_NO_FBACK:
474 case TFRC_SSTATE_FBACK:
475 /* Calculate new round trip sample by
476 * R_sample = (now - t_recvdata) - t_delay */
477 /* get t_recvdata from history */
8c60f3fa
ACM
478 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
479 DCCP_SKB_CB(skb)->dccpd_ack_seq);
59d203f9
ACM
480 if (unlikely(packet == NULL)) {
481 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, seqno "
482 "%llu(%s) does't exist in history!\n",
483 __FUNCTION__, dccp_role(sk), sk,
484 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
485 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
7c657876
ACM
486 return;
487 }
488
489 /* Update RTT */
b0e56780
ACM
490 dccp_timestamp(sk, &now);
491 r_sample = timeval_delta(&now, &packet->dccphtx_tstamp);
1a28599a 492 if (unlikely(r_sample <= t_elapsed))
59d203f9
ACM
493 LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
494 "t_elapsed=%uus\n",
1a28599a
ACM
495 __FUNCTION__, r_sample, t_elapsed);
496 else
497 r_sample -= t_elapsed;
7c657876
ACM
498
499 /* Update RTT estimate by
500 * If (No feedback recv)
501 * R = R_sample;
502 * Else
503 * R = q * R + (1 - q) * R_sample;
504 *
505 * q is a constant, RFC 3448 recomments 0.9
506 */
507 if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
508 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
509 hctx->ccid3hctx_rtt = r_sample;
510 } else
1f2333ae
ACM
511 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
512 r_sample / 10;
7c657876 513
1f2333ae
ACM
514 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
515 "r_sample=%us\n", dccp_role(sk), sk,
516 hctx->ccid3hctx_rtt, r_sample);
7c657876
ACM
517
518 /* Update timeout interval */
c68e64cf
ACM
519 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
520 USEC_PER_SEC);
7c657876
ACM
521
522 /* Update receive rate */
1f2333ae 523 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
7c657876
ACM
524
525 /* Update loss event rate */
526 if (pinv == ~0 || pinv == 0)
527 hctx->ccid3hctx_p = 0;
528 else {
529 hctx->ccid3hctx_p = 1000000 / pinv;
530
531 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
532 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
1f2333ae
ACM
533 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
534 dccp_role(sk), sk);
7c657876
ACM
535 }
536 }
537
538 /* unschedule no feedback timer */
539 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
540
541 /* Update sending rate */
542 ccid3_hc_tx_update_x(sk);
543
544 /* Update next send time */
b6ee3d4a
ACM
545 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
546 hctx->ccid3hctx_t_ipi);
7c657876 547 ccid3_calc_new_t_ipi(hctx);
b6ee3d4a
ACM
548 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
549 hctx->ccid3hctx_t_ipi);
7c657876
ACM
550 ccid3_calc_new_delta(hctx);
551
552 /* remove all packets older than the one acked from history */
8c60f3fa
ACM
553 dccp_tx_hist_purge_older(ccid3_tx_hist,
554 &hctx->ccid3hctx_hist, packet);
c530cfb1
ACM
555 /*
556 * As we have calculated new ipi, delta, t_nom it is possible that
557 * we now can send a packet, so wake up dccp_wait_for_ccids.
558 */
559 sk->sk_write_space(sk);
8c60f3fa 560
1f2333ae
ACM
561 /*
562 * Schedule no feedback timer to expire in
563 * max(4 * R, 2 * s / X)
564 */
c68e64cf 565 next_tmout = max(hctx->ccid3hctx_t_rto,
6b5e633a
ACM
566 2 * usecs_div(hctx->ccid3hctx_s,
567 hctx->ccid3hctx_x));
568
1f2333ae
ACM
569 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
570 "expire in %lu jiffies (%luus)\n",
571 dccp_role(sk), sk,
572 usecs_to_jiffies(next_tmout), next_tmout);
7c657876
ACM
573
574 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
1f2333ae 575 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
7c657876
ACM
576
577 /* set idle flag */
578 hctx->ccid3hctx_idle = 1;
579 break;
580 default:
581 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
582 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
583 dump_stack();
584 break;
585 }
586}
587
588static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
589{
59d203f9 590 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
7c657876 591
59d203f9
ACM
592 BUG_ON(hctx == NULL);
593
594 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
7c657876
ACM
595 return;
596
597 DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
598}
599
600static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
1f2333ae
ACM
601 unsigned char len, u16 idx,
602 unsigned char *value)
7c657876
ACM
603{
604 int rc = 0;
59725dc2
ACM
605 const struct dccp_sock *dp = dccp_sk(sk);
606 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
7c657876
ACM
607 struct ccid3_options_received *opt_recv;
608
59d203f9 609 BUG_ON(hctx == NULL);
7c657876
ACM
610
611 opt_recv = &hctx->ccid3hctx_options_received;
612
613 if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
614 opt_recv->ccid3or_seqno = dp->dccps_gsr;
615 opt_recv->ccid3or_loss_event_rate = ~0;
616 opt_recv->ccid3or_loss_intervals_idx = 0;
617 opt_recv->ccid3or_loss_intervals_len = 0;
618 opt_recv->ccid3or_receive_rate = 0;
619 }
620
621 switch (option) {
622 case TFRC_OPT_LOSS_EVENT_RATE:
59d203f9
ACM
623 if (unlikely(len != 4)) {
624 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
625 "len for TFRC_OPT_LOSS_EVENT_RATE\n",
626 __FUNCTION__, dccp_role(sk), sk);
7c657876
ACM
627 rc = -EINVAL;
628 } else {
629 opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
630 ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
631 dccp_role(sk), sk,
632 opt_recv->ccid3or_loss_event_rate);
633 }
634 break;
635 case TFRC_OPT_LOSS_INTERVALS:
636 opt_recv->ccid3or_loss_intervals_idx = idx;
637 opt_recv->ccid3or_loss_intervals_len = len;
638 ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
639 dccp_role(sk), sk,
640 opt_recv->ccid3or_loss_intervals_idx,
641 opt_recv->ccid3or_loss_intervals_len);
642 break;
643 case TFRC_OPT_RECEIVE_RATE:
59d203f9
ACM
644 if (unlikely(len != 4)) {
645 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
646 "len for TFRC_OPT_RECEIVE_RATE\n",
647 __FUNCTION__, dccp_role(sk), sk);
7c657876
ACM
648 rc = -EINVAL;
649 } else {
650 opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
651 ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
652 dccp_role(sk), sk,
653 opt_recv->ccid3or_receive_rate);
654 }
655 break;
656 }
657
658 return rc;
659}
660
661static int ccid3_hc_tx_init(struct sock *sk)
662{
663 struct dccp_sock *dp = dccp_sk(sk);
664 struct ccid3_hc_tx_sock *hctx;
665
59725dc2
ACM
666 dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx), gfp_any());
667 if (dp->dccps_hc_tx_ccid_private == NULL)
7c657876
ACM
668 return -ENOMEM;
669
59725dc2 670 hctx = ccid3_hc_tx_sk(sk);
7c657876
ACM
671 memset(hctx, 0, sizeof(*hctx));
672
a84ffe43
ACM
673 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
674 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
675 hctx->ccid3hctx_s = dp->dccps_packet_size;
7c657876
ACM
676 else
677 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
678
1f2333ae
ACM
679 /* Set transmission rate to 1 packet per second */
680 hctx->ccid3hctx_x = hctx->ccid3hctx_s;
c68e64cf 681 hctx->ccid3hctx_t_rto = USEC_PER_SEC;
7c657876
ACM
682 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
683 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
684 init_timer(&hctx->ccid3hctx_no_feedback_timer);
685
686 return 0;
687}
688
689static void ccid3_hc_tx_exit(struct sock *sk)
690{
691 struct dccp_sock *dp = dccp_sk(sk);
59725dc2 692 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
7c657876 693
7c657876
ACM
694 BUG_ON(hctx == NULL);
695
696 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
697 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
698
699 /* Empty packet history */
8c60f3fa 700 dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
7c657876
ACM
701
702 kfree(dp->dccps_hc_tx_ccid_private);
703 dp->dccps_hc_tx_ccid_private = NULL;
704}
705
706/*
707 * RX Half Connection methods
708 */
709
710/* TFRC receiver states */
711enum ccid3_hc_rx_states {
712 TFRC_RSTATE_NO_DATA = 1,
713 TFRC_RSTATE_DATA,
714 TFRC_RSTATE_TERM = 127,
715};
716
717#ifdef CCID3_DEBUG
718static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
719{
720 static char *ccid3_rx_state_names[] = {
721 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
722 [TFRC_RSTATE_DATA] = "DATA",
723 [TFRC_RSTATE_TERM] = "TERM",
724 };
725
726 return ccid3_rx_state_names[state];
727}
728#endif
729
1f2333ae
ACM
730static inline void ccid3_hc_rx_set_state(struct sock *sk,
731 enum ccid3_hc_rx_states state)
7c657876 732{
59725dc2 733 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
7c657876
ACM
734 enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
735
736 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
1f2333ae
ACM
737 dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
738 ccid3_rx_state_name(state));
7c657876
ACM
739 WARN_ON(state == oldstate);
740 hcrx->ccid3hcrx_state = state;
741}
742
7c657876
ACM
743static void ccid3_hc_rx_send_feedback(struct sock *sk)
744{
59725dc2 745 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
7c657876 746 struct dccp_sock *dp = dccp_sk(sk);
8c60f3fa 747 struct dccp_rx_hist_entry *packet;
b6ee3d4a 748 struct timeval now;
7c657876
ACM
749
750 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
751
b0e56780 752 dccp_timestamp(sk, &now);
b6ee3d4a 753
7c657876
ACM
754 switch (hcrx->ccid3hcrx_state) {
755 case TFRC_RSTATE_NO_DATA:
756 hcrx->ccid3hcrx_x_recv = 0;
757 break;
758 case TFRC_RSTATE_DATA: {
b6ee3d4a
ACM
759 const u32 delta = timeval_delta(&now,
760 &hcrx->ccid3hcrx_tstamp_last_feedback);
27ae543e
ACM
761 hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
762 delta);
7c657876
ACM
763 }
764 break;
765 default:
766 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
767 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
768 dump_stack();
769 return;
770 }
771
8c60f3fa 772 packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
59d203f9
ACM
773 if (unlikely(packet == NULL)) {
774 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, no data packet "
775 "in history!\n",
776 __FUNCTION__, dccp_role(sk), sk);
7c657876
ACM
777 return;
778 }
779
b6ee3d4a 780 hcrx->ccid3hcrx_tstamp_last_feedback = now;
c1734376 781 hcrx->ccid3hcrx_last_counter = packet->dccphrx_ccval;
8c60f3fa 782 hcrx->ccid3hcrx_seqno_last_counter = packet->dccphrx_seqno;
7c657876
ACM
783 hcrx->ccid3hcrx_bytes_recv = 0;
784
785 /* Convert to multiples of 10us */
b6ee3d4a
ACM
786 hcrx->ccid3hcrx_elapsed_time =
787 timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
7c657876
ACM
788 if (hcrx->ccid3hcrx_p == 0)
789 hcrx->ccid3hcrx_pinv = ~0;
790 else
791 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
507d37cf 792 dp->dccps_hc_rx_insert_options = 1;
7c657876
ACM
793 dccp_send_ack(sk);
794}
795
796static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
797{
59d203f9 798 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
4fded33b 799 u32 x_recv, pinv;
7c657876 800
59d203f9
ACM
801 BUG_ON(hcrx == NULL);
802
803 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
7c657876
ACM
804 return;
805
7c657876 806 DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
4fded33b
ACM
807
808 if (dccp_packet_without_ack(skb))
809 return;
810
811 if (hcrx->ccid3hcrx_elapsed_time != 0)
812 dccp_insert_option_elapsed_time(sk, skb,
813 hcrx->ccid3hcrx_elapsed_time);
814 dccp_insert_option_timestamp(sk, skb);
815 x_recv = htonl(hcrx->ccid3hcrx_x_recv);
816 pinv = htonl(hcrx->ccid3hcrx_pinv);
817 dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
818 &pinv, sizeof(pinv));
819 dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
820 &x_recv, sizeof(x_recv));
7c657876
ACM
821}
822
7c657876
ACM
823/* calculate first loss interval
824 *
825 * returns estimated loss interval in usecs */
826
827static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
828{
59725dc2 829 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
8c60f3fa 830 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
7c657876 831 u32 rtt, delta, x_recv, fval, p, tmp2;
b6ee3d4a 832 struct timeval tstamp = { 0, };
7c657876
ACM
833 int interval = 0;
834 int win_count = 0;
835 int step = 0;
836 u64 tmp1;
837
8c60f3fa
ACM
838 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
839 dccphrx_node) {
840 if (dccp_rx_hist_entry_data_packet(entry)) {
7c657876
ACM
841 tail = entry;
842
843 switch (step) {
844 case 0:
8c60f3fa 845 tstamp = entry->dccphrx_tstamp;
c1734376 846 win_count = entry->dccphrx_ccval;
7c657876
ACM
847 step = 1;
848 break;
849 case 1:
c1734376 850 interval = win_count - entry->dccphrx_ccval;
7c657876
ACM
851 if (interval < 0)
852 interval += TFRC_WIN_COUNT_LIMIT;
853 if (interval > 4)
854 goto found;
855 break;
856 }
857 }
858 }
859
59d203f9
ACM
860 if (unlikely(step == 0)) {
861 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, packet history "
862 "contains no data packets!\n",
863 __FUNCTION__, dccp_role(sk), sk);
7c657876
ACM
864 return ~0;
865 }
866
59d203f9
ACM
867 if (unlikely(interval == 0)) {
868 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Could not find a "
869 "win_count interval > 0. Defaulting to 1\n",
870 __FUNCTION__, dccp_role(sk), sk);
7c657876
ACM
871 interval = 1;
872 }
873found:
b6ee3d4a 874 rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
7c657876
ACM
875 ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
876 dccp_role(sk), sk, rtt);
877 if (rtt == 0)
878 rtt = 1;
879
b0e56780
ACM
880 dccp_timestamp(sk, &tstamp);
881 delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
882 x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv, delta);
7c657876
ACM
883
884 tmp1 = (u64)x_recv * (u64)rtt;
885 do_div(tmp1,10000000);
886 tmp2 = (u32)tmp1;
887 fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
888 /* do not alter order above or you will get overflow on 32 bit */
36729c1a 889 p = tfrc_calc_x_reverse_lookup(fval);
1f2333ae
ACM
890 ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
891 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
7c657876
ACM
892
893 if (p == 0)
894 return ~0;
895 else
896 return 1000000 / p;
897}
898
899static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
900{
59725dc2 901 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
7c657876 902
ae6706f0
ACM
903 if (seq_loss != DCCP_MAX_SEQNO + 1 &&
904 list_empty(&hcrx->ccid3hcrx_li_hist)) {
905 struct dccp_li_hist_entry *li_tail;
7c657876 906
ae6706f0
ACM
907 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
908 &hcrx->ccid3hcrx_li_hist,
909 seq_loss, win_loss);
910 if (li_tail == NULL)
911 return;
912 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
59d203f9
ACM
913 } else
914 LIMIT_NETDEBUG(KERN_WARNING "%s: FIXME: find end of "
915 "interval\n", __FUNCTION__);
7c657876
ACM
916}
917
918static void ccid3_hc_rx_detect_loss(struct sock *sk)
919{
59725dc2 920 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
29e4f8b3
ACM
921 u8 win_loss;
922 const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
923 &hcrx->ccid3hcrx_li_hist,
924 &win_loss);
7c657876 925
7c657876
ACM
926 ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
927}
928
7c657876
ACM
929static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
930{
59725dc2 931 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
4fded33b 932 const struct dccp_options_received *opt_recv;
8c60f3fa 933 struct dccp_rx_hist_entry *packet;
7c657876
ACM
934 struct timeval now;
935 u8 win_count;
b3a3077d 936 u32 p_prev, r_sample, t_elapsed;
7c657876 937 int ins;
1f2333ae 938
59d203f9
ACM
939 BUG_ON(hcrx == NULL ||
940 !(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
7c657876
ACM
941 hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
942
59725dc2 943 opt_recv = &dccp_sk(sk)->dccps_options_received;
4fded33b 944
7c657876
ACM
945 switch (DCCP_SKB_CB(skb)->dccpd_type) {
946 case DCCP_PKT_ACK:
947 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
948 return;
949 case DCCP_PKT_DATAACK:
4fded33b 950 if (opt_recv->dccpor_timestamp_echo == 0)
7c657876
ACM
951 break;
952 p_prev = hcrx->ccid3hcrx_rtt;
b0e56780 953 dccp_timestamp(sk, &now);
b3a3077d
ACM
954 timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
955 r_sample = timeval_usecs(&now);
956 t_elapsed = opt_recv->dccpor_elapsed_time * 10;
957
958 if (unlikely(r_sample <= t_elapsed))
59d203f9
ACM
959 LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
960 "t_elapsed=%uus\n",
b3a3077d
ACM
961 __FUNCTION__, r_sample, t_elapsed);
962 else
963 r_sample -= t_elapsed;
964
965 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
966 hcrx->ccid3hcrx_rtt = r_sample;
967 else
968 hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
969 r_sample / 10;
970
7c657876 971 if (p_prev != hcrx->ccid3hcrx_rtt)
4fded33b
ACM
972 ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
973 dccp_role(sk), hcrx->ccid3hcrx_rtt,
974 opt_recv->dccpor_elapsed_time);
7c657876
ACM
975 break;
976 case DCCP_PKT_DATA:
977 break;
59d203f9 978 default: /* We're not interested in other packet types, move along */
7c657876
ACM
979 return;
980 }
981
b0e56780 982 packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
8c60f3fa 983 skb, SLAB_ATOMIC);
59d203f9
ACM
984 if (unlikely(packet == NULL)) {
985 LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Not enough mem to "
986 "add rx packet to history, consider it lost!\n",
987 __FUNCTION__, dccp_role(sk), sk);
7c657876
ACM
988 return;
989 }
990
c1734376 991 win_count = packet->dccphrx_ccval;
7c657876 992
072ab6c6
ACM
993 ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
994 &hcrx->ccid3hcrx_li_hist, packet);
7c657876
ACM
995
996 if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
997 return;
998
999 switch (hcrx->ccid3hcrx_state) {
1000 case TFRC_RSTATE_NO_DATA:
1f2333ae
ACM
1001 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
1002 "feedback\n",
1003 dccp_role(sk), sk,
1004 dccp_state_name(sk->sk_state), skb);
7c657876
ACM
1005 ccid3_hc_rx_send_feedback(sk);
1006 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
1007 return;
1008 case TFRC_RSTATE_DATA:
1f2333ae
ACM
1009 hcrx->ccid3hcrx_bytes_recv += skb->len -
1010 dccp_hdr(skb)->dccph_doff * 4;
b6ee3d4a
ACM
1011 if (ins != 0)
1012 break;
1013
b0e56780 1014 dccp_timestamp(sk, &now);
b6ee3d4a
ACM
1015 if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
1016 hcrx->ccid3hcrx_rtt) {
1017 hcrx->ccid3hcrx_tstamp_last_ack = now;
1018 ccid3_hc_rx_send_feedback(sk);
7c657876 1019 }
b6ee3d4a 1020 return;
7c657876
ACM
1021 default:
1022 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1023 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1024 dump_stack();
1025 return;
1026 }
1027
1028 /* Dealing with packet loss */
4fded33b
ACM
1029 ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1030 dccp_role(sk), sk, dccp_state_name(sk->sk_state));
7c657876
ACM
1031
1032 ccid3_hc_rx_detect_loss(sk);
1033 p_prev = hcrx->ccid3hcrx_p;
1034
1035 /* Calculate loss event rate */
ae6706f0 1036 if (!list_empty(&hcrx->ccid3hcrx_li_hist))
7c657876 1037 /* Scaling up by 1000000 as fixed decimal */
ae6706f0 1038 hcrx->ccid3hcrx_p = 1000000 / dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
7c657876
ACM
1039
1040 if (hcrx->ccid3hcrx_p > p_prev) {
1041 ccid3_hc_rx_send_feedback(sk);
1042 return;
1043 }
1044}
1045
1046static int ccid3_hc_rx_init(struct sock *sk)
1047{
1048 struct dccp_sock *dp = dccp_sk(sk);
1049 struct ccid3_hc_rx_sock *hcrx;
1050
1051 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1052
59725dc2
ACM
1053 dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx), gfp_any());
1054 if (dp->dccps_hc_rx_ccid_private == NULL)
7c657876
ACM
1055 return -ENOMEM;
1056
59725dc2 1057 hcrx = ccid3_hc_rx_sk(sk);
7c657876
ACM
1058 memset(hcrx, 0, sizeof(*hcrx));
1059
a84ffe43
ACM
1060 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
1061 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
1062 hcrx->ccid3hcrx_s = dp->dccps_packet_size;
7c657876
ACM
1063 else
1064 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1065
1066 hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1067 INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
ae6706f0 1068 INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
b0e56780 1069 dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
954ee31f
ACM
1070 hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
1071 hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
7c657876
ACM
1072 return 0;
1073}
1074
1075static void ccid3_hc_rx_exit(struct sock *sk)
1076{
59725dc2 1077 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
7c657876 1078 struct dccp_sock *dp = dccp_sk(sk);
7c657876 1079
59d203f9 1080 BUG_ON(hcrx == NULL);
7c657876
ACM
1081
1082 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1083
1084 /* Empty packet history */
8c60f3fa 1085 dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
7c657876
ACM
1086
1087 /* Empty loss interval history */
ae6706f0 1088 dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
7c657876
ACM
1089
1090 kfree(dp->dccps_hc_rx_ccid_private);
1091 dp->dccps_hc_rx_ccid_private = NULL;
1092}
1093
2babe1f6
ACM
1094static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1095{
59725dc2 1096 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
2babe1f6 1097
59d203f9 1098 BUG_ON(hcrx == NULL);
2babe1f6
ACM
1099
1100 info->tcpi_ca_state = hcrx->ccid3hcrx_state;
1101 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1102 info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
1103}
1104
1105static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1106{
59725dc2 1107 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
2babe1f6 1108
59d203f9 1109 BUG_ON(hctx == NULL);
2babe1f6
ACM
1110
1111 info->tcpi_rto = hctx->ccid3hctx_t_rto;
1112 info->tcpi_rtt = hctx->ccid3hctx_rtt;
1113}
1114
7c657876
ACM
1115static struct ccid ccid3 = {
1116 .ccid_id = 3,
1117 .ccid_name = "ccid3",
1118 .ccid_owner = THIS_MODULE,
1119 .ccid_init = ccid3_init,
1120 .ccid_exit = ccid3_exit,
1121 .ccid_hc_tx_init = ccid3_hc_tx_init,
1122 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
1123 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
1124 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
1125 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
1126 .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1127 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
1128 .ccid_hc_rx_init = ccid3_hc_rx_init,
1129 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
1130 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1131 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
2babe1f6
ACM
1132 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
1133 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
7c657876
ACM
1134};
1135
1136module_param(ccid3_debug, int, 0444);
1137MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1138
1139static __init int ccid3_module_init(void)
1140{
8c60f3fa 1141 int rc = -ENOBUFS;
7c657876 1142
8c60f3fa
ACM
1143 ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1144 if (ccid3_rx_hist == NULL)
7c657876
ACM
1145 goto out;
1146
8c60f3fa
ACM
1147 ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1148 if (ccid3_tx_hist == NULL)
1149 goto out_free_rx;
7c657876 1150
ae6706f0
ACM
1151 ccid3_li_hist = dccp_li_hist_new("ccid3");
1152 if (ccid3_li_hist == NULL)
8c60f3fa 1153 goto out_free_tx;
7c657876
ACM
1154
1155 rc = ccid_register(&ccid3);
1156 if (rc != 0)
1157 goto out_free_loss_interval_history;
7c657876
ACM
1158out:
1159 return rc;
8c60f3fa 1160
7c657876 1161out_free_loss_interval_history:
ae6706f0
ACM
1162 dccp_li_hist_delete(ccid3_li_hist);
1163 ccid3_li_hist = NULL;
8c60f3fa
ACM
1164out_free_tx:
1165 dccp_tx_hist_delete(ccid3_tx_hist);
1166 ccid3_tx_hist = NULL;
1167out_free_rx:
1168 dccp_rx_hist_delete(ccid3_rx_hist);
1169 ccid3_rx_hist = NULL;
7c657876
ACM
1170 goto out;
1171}
1172module_init(ccid3_module_init);
1173
1174static __exit void ccid3_module_exit(void)
1175{
725ba8ee
ACM
1176#ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1177 /*
1178 * Hack to use while developing, so that we get rid of the control
1179 * sock, that is what keeps a refcount on dccp.ko -acme
1180 */
1181 extern void dccp_ctl_sock_exit(void);
1182
1183 dccp_ctl_sock_exit();
1184#endif
7c657876
ACM
1185 ccid_unregister(&ccid3);
1186
8c60f3fa
ACM
1187 if (ccid3_tx_hist != NULL) {
1188 dccp_tx_hist_delete(ccid3_tx_hist);
1189 ccid3_tx_hist = NULL;
7c657876 1190 }
8c60f3fa
ACM
1191 if (ccid3_rx_hist != NULL) {
1192 dccp_rx_hist_delete(ccid3_rx_hist);
1193 ccid3_rx_hist = NULL;
7c657876 1194 }
ae6706f0
ACM
1195 if (ccid3_li_hist != NULL) {
1196 dccp_li_hist_delete(ccid3_li_hist);
1197 ccid3_li_hist = NULL;
7c657876
ACM
1198 }
1199}
1200module_exit(ccid3_module_exit);
1201
1f2333ae
ACM
1202MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1203 "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
7c657876
ACM
1204MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1205MODULE_LICENSE("GPL");
1206MODULE_ALIAS("net-dccp-ccid-3");