Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild-bugfix
[linux-2.6/kvm.git] / net / dccp / ccids / ccid3.c
blobaa68e0ab274d4b837a8147b6cb4d677d53740185
1 /*
2 * net/dccp/ccids/ccid3.c
4 * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
5 * Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz>
7 * An implementation of the DCCP protocol
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/
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
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>.
20 * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
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.
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.
37 #include <linux/config.h>
38 #include "../ccid.h"
39 #include "../dccp.h"
40 #include "lib/packet_history.h"
41 #include "lib/loss_interval.h"
42 #include "lib/tfrc.h"
43 #include "ccid3.h"
46 * Reason for maths here is to avoid 32 bit overflow when a is big.
47 * With this we get close to the limit.
49 static inline u32 usecs_div(const u32 a, const u32 b)
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;
64 static int ccid3_debug;
66 #ifdef CCID3_DEBUG
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
75 static struct dccp_tx_hist *ccid3_tx_hist;
76 static struct dccp_rx_hist *ccid3_rx_hist;
77 static struct dccp_li_hist *ccid3_li_hist;
79 static int ccid3_init(struct sock *sk)
81 return 0;
84 static void ccid3_exit(struct sock *sk)
88 /* TFRC sender states */
89 enum ccid3_hc_tx_states {
90 TFRC_SSTATE_NO_SENT = 1,
91 TFRC_SSTATE_NO_FBACK,
92 TFRC_SSTATE_FBACK,
93 TFRC_SSTATE_TERM,
96 #ifdef CCID3_DEBUG
97 static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
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",
106 return ccid3_state_names[state];
108 #endif
110 static inline void ccid3_hc_tx_set_state(struct sock *sk,
111 enum ccid3_hc_tx_states state)
113 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
114 enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
116 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
117 dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
118 ccid3_tx_state_name(state));
119 WARN_ON(state == oldstate);
120 hctx->ccid3hctx_state = state;
123 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
124 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
127 * If no feedback spec says t_ipi is 1 second (set elsewhere and then
128 * doubles after every no feedback timer (separate function)
130 if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
131 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
132 hctx->ccid3hctx_x);
135 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
136 static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
138 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
139 TFRC_OPSYS_HALF_TIME_GRAN);
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;
152 static void ccid3_hc_tx_update_x(struct sock *sk)
154 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
156 /* To avoid large error in calcX */
157 if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
158 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
159 hctx->ccid3hctx_rtt,
160 hctx->ccid3hctx_p);
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));
165 } else {
166 struct timeval now;
168 dccp_timestamp(sk, &now);
169 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
170 hctx->ccid3hctx_rtt) {
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));
175 hctx->ccid3hctx_t_ld = now;
180 static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
182 struct sock *sk = (struct sock *)data;
183 unsigned long next_tmout = 0;
184 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
186 bh_lock_sock(sk);
187 if (sock_owned_by_user(sk)) {
188 /* Try again later. */
189 /* XXX: set some sensible MIB */
190 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
191 jiffies + HZ / 5);
192 goto out;
195 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
196 ccid3_tx_state_name(hctx->ccid3hctx_state));
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;
204 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
205 TFRC_MAX_BACK_OFF_TIME))
206 hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
207 TFRC_MAX_BACK_OFF_TIME);
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),
213 hctx->ccid3hctx_x);
214 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
215 hctx->ccid3hctx_x),
216 TFRC_INITIAL_TIMEOUT);
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
222 break;
223 case TFRC_SSTATE_FBACK:
225 * Check if IDLE since last timeout and recv rate is less than
226 * 4 packets per RTT
228 if (!hctx->ccid3hctx_idle ||
229 (hctx->ccid3hctx_x_recv >=
230 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
231 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
232 dccp_role(sk), sk,
233 ccid3_tx_state_name(hctx->ccid3hctx_state));
234 /* Halve sending rate */
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;
241 BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
242 hctx->ccid3hctx_x_calc == 0);
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;
252 /* Update sending rate */
253 ccid3_hc_tx_update_x(sk);
256 * Schedule no feedback timer to expire in
257 * max(4 * R, 2 * s / X)
259 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
260 2 * usecs_div(hctx->ccid3hctx_s,
261 hctx->ccid3hctx_x));
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;
270 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
271 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
272 hctx->ccid3hctx_idle = 1;
273 out:
274 bh_unlock_sock(sk);
275 sock_put(sk);
278 static int ccid3_hc_tx_send_packet(struct sock *sk,
279 struct sk_buff *skb, int len)
281 struct dccp_sock *dp = dccp_sk(sk);
282 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
283 struct dccp_tx_hist_entry *new_packet;
284 struct timeval now;
285 long delay;
286 int rc = -ENOTCONN;
288 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
290 /* Check if pure ACK or Terminating*/
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"?
295 if (unlikely(len == 0))
296 goto out;
298 /* See if last packet allocated was not sent */
299 new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
300 if (new_packet == NULL || new_packet->dccphtx_sent) {
301 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
302 SLAB_ATOMIC);
304 rc = -ENOBUFS;
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);
309 goto out;
312 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
315 dccp_timestamp(sk, &now);
317 switch (hctx->ccid3hctx_state) {
318 case TFRC_SSTATE_NO_SENT:
319 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
320 hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
321 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
322 jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
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);
326 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_IPI;
328 /* Set nominal send time for initial packet */
329 hctx->ccid3hctx_t_nom = now;
330 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
331 hctx->ccid3hctx_t_ipi);
332 ccid3_calc_new_delta(hctx);
333 rc = 0;
334 break;
335 case TFRC_SSTATE_NO_FBACK:
336 case TFRC_SSTATE_FBACK:
337 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
338 hctx->ccid3hctx_delta);
339 delay /= -1000;
340 /* divide by -1000 is to convert to ms and get sign right */
341 rc = delay > 0 ? delay : 0;
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;
351 /* Can we send? if so add options and add to packet history */
352 if (rc == 0) {
353 dp->dccps_hc_tx_insert_options = 1;
354 new_packet->dccphtx_ccval =
355 DCCP_SKB_CB(skb)->dccpd_ccval =
356 hctx->ccid3hctx_last_win_count;
358 out:
359 return rc;
362 static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
364 const struct dccp_sock *dp = dccp_sk(sk);
365 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
366 struct timeval now;
368 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
370 dccp_timestamp(sk, &now);
372 /* check if we have sent a data packet */
373 if (len > 0) {
374 unsigned long quarter_rtt;
375 struct dccp_tx_hist_entry *packet;
377 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
378 if (unlikely(packet == NULL)) {
379 LIMIT_NETDEBUG(KERN_WARNING "%s: packet doesn't "
380 "exists in history!\n", __FUNCTION__);
381 return;
383 if (unlikely(packet->dccphtx_sent)) {
384 LIMIT_NETDEBUG(KERN_WARNING "%s: no unsent packet in "
385 "history!\n", __FUNCTION__);
386 return;
388 packet->dccphtx_tstamp = now;
389 packet->dccphtx_seqno = dp->dccps_gss;
391 * Check if win_count have changed
392 * Algorithm in "8.1. Window Counter Valuer" in
393 * draft-ietf-dccp-ccid3-11.txt
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;
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;
403 ccid3_pr_debug("%s, sk=%p, window changed from "
404 "%u to %u!\n",
405 dccp_role(sk), sk,
406 packet->dccphtx_ccval,
407 hctx->ccid3hctx_last_win_count);
410 hctx->ccid3hctx_idle = 0;
411 packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
412 packet->dccphtx_sent = 1;
413 } else
414 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
415 dccp_role(sk), sk, dp->dccps_gss);
417 switch (hctx->ccid3hctx_state) {
418 case TFRC_SSTATE_NO_SENT:
419 /* if first wasn't pure ack */
420 if (len != 0)
421 printk(KERN_CRIT "%s: %s, First packet sent is noted "
422 "as a data packet\n",
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);
431 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
432 hctx->ccid3hctx_t_ipi);
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;
443 static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
445 const struct dccp_sock *dp = dccp_sk(sk);
446 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
447 struct ccid3_options_received *opt_recv;
448 struct dccp_tx_hist_entry *packet;
449 struct timeval now;
450 unsigned long next_tmout;
451 u32 t_elapsed;
452 u32 pinv;
453 u32 x_recv;
454 u32 r_sample;
456 BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
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;
463 opt_recv = &hctx->ccid3hctx_options_received;
465 t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10;
466 x_recv = opt_recv->ccid3or_receive_rate;
467 pinv = opt_recv->ccid3or_loss_event_rate;
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 */
478 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
479 DCCP_SKB_CB(skb)->dccpd_ack_seq);
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));
486 return;
489 /* Update RTT */
490 dccp_timestamp(sk, &now);
491 r_sample = timeval_delta(&now, &packet->dccphtx_tstamp);
492 if (unlikely(r_sample <= t_elapsed))
493 LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
494 "t_elapsed=%uus\n",
495 __FUNCTION__, r_sample, t_elapsed);
496 else
497 r_sample -= t_elapsed;
499 /* Update RTT estimate by
500 * If (No feedback recv)
501 * R = R_sample;
502 * Else
503 * R = q * R + (1 - q) * R_sample;
505 * q is a constant, RFC 3448 recomments 0.9
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
511 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
512 r_sample / 10;
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);
518 /* Update timeout interval */
519 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
520 USEC_PER_SEC);
522 /* Update receive rate */
523 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
525 /* Update loss event rate */
526 if (pinv == ~0 || pinv == 0)
527 hctx->ccid3hctx_p = 0;
528 else {
529 hctx->ccid3hctx_p = 1000000 / pinv;
531 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
532 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
533 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
534 dccp_role(sk), sk);
538 /* unschedule no feedback timer */
539 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
541 /* Update sending rate */
542 ccid3_hc_tx_update_x(sk);
544 /* Update next send time */
545 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
546 hctx->ccid3hctx_t_ipi);
547 ccid3_calc_new_t_ipi(hctx);
548 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
549 hctx->ccid3hctx_t_ipi);
550 ccid3_calc_new_delta(hctx);
552 /* remove all packets older than the one acked from history */
553 dccp_tx_hist_purge_older(ccid3_tx_hist,
554 &hctx->ccid3hctx_hist, packet);
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.
559 sk->sk_write_space(sk);
562 * Schedule no feedback timer to expire in
563 * max(4 * R, 2 * s / X)
565 next_tmout = max(hctx->ccid3hctx_t_rto,
566 2 * usecs_div(hctx->ccid3hctx_s,
567 hctx->ccid3hctx_x));
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);
574 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
575 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
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;
588 static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
590 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
592 BUG_ON(hctx == NULL);
594 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
595 return;
597 DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
600 static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
601 unsigned char len, u16 idx,
602 unsigned char *value)
604 int rc = 0;
605 const struct dccp_sock *dp = dccp_sk(sk);
606 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
607 struct ccid3_options_received *opt_recv;
609 BUG_ON(hctx == NULL);
611 opt_recv = &hctx->ccid3hctx_options_received;
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;
621 switch (option) {
622 case TFRC_OPT_LOSS_EVENT_RATE:
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);
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);
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:
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);
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);
655 break;
658 return rc;
661 static int ccid3_hc_tx_init(struct sock *sk)
663 struct dccp_sock *dp = dccp_sk(sk);
664 struct ccid3_hc_tx_sock *hctx;
666 dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx), gfp_any());
667 if (dp->dccps_hc_tx_ccid_private == NULL)
668 return -ENOMEM;
670 hctx = ccid3_hc_tx_sk(sk);
671 memset(hctx, 0, sizeof(*hctx));
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;
676 else
677 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
679 /* Set transmission rate to 1 packet per second */
680 hctx->ccid3hctx_x = hctx->ccid3hctx_s;
681 hctx->ccid3hctx_t_rto = USEC_PER_SEC;
682 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
683 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
684 init_timer(&hctx->ccid3hctx_no_feedback_timer);
686 return 0;
689 static void ccid3_hc_tx_exit(struct sock *sk)
691 struct dccp_sock *dp = dccp_sk(sk);
692 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
694 BUG_ON(hctx == NULL);
696 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
697 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
699 /* Empty packet history */
700 dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
702 kfree(dp->dccps_hc_tx_ccid_private);
703 dp->dccps_hc_tx_ccid_private = NULL;
707 * RX Half Connection methods
710 /* TFRC receiver states */
711 enum ccid3_hc_rx_states {
712 TFRC_RSTATE_NO_DATA = 1,
713 TFRC_RSTATE_DATA,
714 TFRC_RSTATE_TERM = 127,
717 #ifdef CCID3_DEBUG
718 static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
720 static char *ccid3_rx_state_names[] = {
721 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
722 [TFRC_RSTATE_DATA] = "DATA",
723 [TFRC_RSTATE_TERM] = "TERM",
726 return ccid3_rx_state_names[state];
728 #endif
730 static inline void ccid3_hc_rx_set_state(struct sock *sk,
731 enum ccid3_hc_rx_states state)
733 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
734 enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
736 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
737 dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
738 ccid3_rx_state_name(state));
739 WARN_ON(state == oldstate);
740 hcrx->ccid3hcrx_state = state;
743 static void ccid3_hc_rx_send_feedback(struct sock *sk)
745 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
746 struct dccp_sock *dp = dccp_sk(sk);
747 struct dccp_rx_hist_entry *packet;
748 struct timeval now;
750 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
752 dccp_timestamp(sk, &now);
754 switch (hcrx->ccid3hcrx_state) {
755 case TFRC_RSTATE_NO_DATA:
756 hcrx->ccid3hcrx_x_recv = 0;
757 break;
758 case TFRC_RSTATE_DATA: {
759 const u32 delta = timeval_delta(&now,
760 &hcrx->ccid3hcrx_tstamp_last_feedback);
761 hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
762 delta);
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;
772 packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
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);
777 return;
780 hcrx->ccid3hcrx_tstamp_last_feedback = now;
781 hcrx->ccid3hcrx_last_counter = packet->dccphrx_ccval;
782 hcrx->ccid3hcrx_seqno_last_counter = packet->dccphrx_seqno;
783 hcrx->ccid3hcrx_bytes_recv = 0;
785 /* Convert to multiples of 10us */
786 hcrx->ccid3hcrx_elapsed_time =
787 timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
788 if (hcrx->ccid3hcrx_p == 0)
789 hcrx->ccid3hcrx_pinv = ~0;
790 else
791 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
792 dp->dccps_hc_rx_insert_options = 1;
793 dccp_send_ack(sk);
796 static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
798 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
799 u32 x_recv, pinv;
801 BUG_ON(hcrx == NULL);
803 if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
804 return;
806 DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
808 if (dccp_packet_without_ack(skb))
809 return;
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));
823 /* calculate first loss interval
825 * returns estimated loss interval in usecs */
827 static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
829 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
830 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
831 u32 rtt, delta, x_recv, fval, p, tmp2;
832 struct timeval tstamp = { 0, };
833 int interval = 0;
834 int win_count = 0;
835 int step = 0;
836 u64 tmp1;
838 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
839 dccphrx_node) {
840 if (dccp_rx_hist_entry_data_packet(entry)) {
841 tail = entry;
843 switch (step) {
844 case 0:
845 tstamp = entry->dccphrx_tstamp;
846 win_count = entry->dccphrx_ccval;
847 step = 1;
848 break;
849 case 1:
850 interval = win_count - entry->dccphrx_ccval;
851 if (interval < 0)
852 interval += TFRC_WIN_COUNT_LIMIT;
853 if (interval > 4)
854 goto found;
855 break;
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);
864 return ~0;
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);
871 interval = 1;
873 found:
874 rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
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;
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);
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 */
889 p = tfrc_calc_x_reverse_lookup(fval);
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);
893 if (p == 0)
894 return ~0;
895 else
896 return 1000000 / p;
899 static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
901 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
903 if (seq_loss != DCCP_MAX_SEQNO + 1 &&
904 list_empty(&hcrx->ccid3hcrx_li_hist)) {
905 struct dccp_li_hist_entry *li_tail;
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);
913 } else
914 LIMIT_NETDEBUG(KERN_WARNING "%s: FIXME: find end of "
915 "interval\n", __FUNCTION__);
918 static void ccid3_hc_rx_detect_loss(struct sock *sk)
920 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
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);
926 ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
929 static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
931 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
932 const struct dccp_options_received *opt_recv;
933 struct dccp_rx_hist_entry *packet;
934 struct timeval now;
935 u8 win_count;
936 u32 p_prev, r_sample, t_elapsed;
937 int ins;
939 BUG_ON(hcrx == NULL ||
940 !(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
941 hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
943 opt_recv = &dccp_sk(sk)->dccps_options_received;
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:
950 if (opt_recv->dccpor_timestamp_echo == 0)
951 break;
952 p_prev = hcrx->ccid3hcrx_rtt;
953 dccp_timestamp(sk, &now);
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;
958 if (unlikely(r_sample <= t_elapsed))
959 LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
960 "t_elapsed=%uus\n",
961 __FUNCTION__, r_sample, t_elapsed);
962 else
963 r_sample -= t_elapsed;
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;
971 if (p_prev != hcrx->ccid3hcrx_rtt)
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);
975 break;
976 case DCCP_PKT_DATA:
977 break;
978 default: /* We're not interested in other packet types, move along */
979 return;
982 packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
983 skb, SLAB_ATOMIC);
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);
988 return;
991 win_count = packet->dccphrx_ccval;
993 ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
994 &hcrx->ccid3hcrx_li_hist, packet);
996 if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
997 return;
999 switch (hcrx->ccid3hcrx_state) {
1000 case TFRC_RSTATE_NO_DATA:
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);
1005 ccid3_hc_rx_send_feedback(sk);
1006 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
1007 return;
1008 case TFRC_RSTATE_DATA:
1009 hcrx->ccid3hcrx_bytes_recv += skb->len -
1010 dccp_hdr(skb)->dccph_doff * 4;
1011 if (ins != 0)
1012 break;
1014 dccp_timestamp(sk, &now);
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);
1020 return;
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;
1028 /* Dealing with packet loss */
1029 ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1030 dccp_role(sk), sk, dccp_state_name(sk->sk_state));
1032 ccid3_hc_rx_detect_loss(sk);
1033 p_prev = hcrx->ccid3hcrx_p;
1035 /* Calculate loss event rate */
1036 if (!list_empty(&hcrx->ccid3hcrx_li_hist))
1037 /* Scaling up by 1000000 as fixed decimal */
1038 hcrx->ccid3hcrx_p = 1000000 / dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
1040 if (hcrx->ccid3hcrx_p > p_prev) {
1041 ccid3_hc_rx_send_feedback(sk);
1042 return;
1046 static int ccid3_hc_rx_init(struct sock *sk)
1048 struct dccp_sock *dp = dccp_sk(sk);
1049 struct ccid3_hc_rx_sock *hcrx;
1051 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1053 dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx), gfp_any());
1054 if (dp->dccps_hc_rx_ccid_private == NULL)
1055 return -ENOMEM;
1057 hcrx = ccid3_hc_rx_sk(sk);
1058 memset(hcrx, 0, sizeof(*hcrx));
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;
1063 else
1064 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1066 hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1067 INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
1068 INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
1069 dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
1070 hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
1071 hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
1072 return 0;
1075 static void ccid3_hc_rx_exit(struct sock *sk)
1077 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1078 struct dccp_sock *dp = dccp_sk(sk);
1080 BUG_ON(hcrx == NULL);
1082 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1084 /* Empty packet history */
1085 dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
1087 /* Empty loss interval history */
1088 dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
1090 kfree(dp->dccps_hc_rx_ccid_private);
1091 dp->dccps_hc_rx_ccid_private = NULL;
1094 static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1096 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1098 /* Listen socks doesn't have a private CCID block */
1099 if (sk->sk_state == DCCP_LISTEN)
1100 return;
1102 BUG_ON(hcrx == NULL);
1104 info->tcpi_ca_state = hcrx->ccid3hcrx_state;
1105 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1106 info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
1109 static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1111 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1113 /* Listen socks doesn't have a private CCID block */
1114 if (sk->sk_state == DCCP_LISTEN)
1115 return;
1117 BUG_ON(hctx == NULL);
1119 info->tcpi_rto = hctx->ccid3hctx_t_rto;
1120 info->tcpi_rtt = hctx->ccid3hctx_rtt;
1123 static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
1124 u32 __user *optval, int __user *optlen)
1126 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
1127 const void *val;
1129 /* Listen socks doesn't have a private CCID block */
1130 if (sk->sk_state == DCCP_LISTEN)
1131 return -EINVAL;
1133 switch (optname) {
1134 case DCCP_SOCKOPT_CCID_RX_INFO:
1135 if (len < sizeof(hcrx->ccid3hcrx_tfrc))
1136 return -EINVAL;
1137 len = sizeof(hcrx->ccid3hcrx_tfrc);
1138 val = &hcrx->ccid3hcrx_tfrc;
1139 break;
1140 default:
1141 return -ENOPROTOOPT;
1144 if (put_user(len, optlen) || copy_to_user(optval, val, len))
1145 return -EFAULT;
1147 return 0;
1150 static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
1151 u32 __user *optval, int __user *optlen)
1153 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
1154 const void *val;
1156 /* Listen socks doesn't have a private CCID block */
1157 if (sk->sk_state == DCCP_LISTEN)
1158 return -EINVAL;
1160 switch (optname) {
1161 case DCCP_SOCKOPT_CCID_TX_INFO:
1162 if (len < sizeof(hctx->ccid3hctx_tfrc))
1163 return -EINVAL;
1164 len = sizeof(hctx->ccid3hctx_tfrc);
1165 val = &hctx->ccid3hctx_tfrc;
1166 break;
1167 default:
1168 return -ENOPROTOOPT;
1171 if (put_user(len, optlen) || copy_to_user(optval, val, len))
1172 return -EFAULT;
1174 return 0;
1177 static struct ccid ccid3 = {
1178 .ccid_id = 3,
1179 .ccid_name = "ccid3",
1180 .ccid_owner = THIS_MODULE,
1181 .ccid_init = ccid3_init,
1182 .ccid_exit = ccid3_exit,
1183 .ccid_hc_tx_init = ccid3_hc_tx_init,
1184 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
1185 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
1186 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
1187 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
1188 .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1189 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
1190 .ccid_hc_rx_init = ccid3_hc_rx_init,
1191 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
1192 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1193 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
1194 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
1195 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
1196 .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
1197 .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
1200 module_param(ccid3_debug, int, 0444);
1201 MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1203 static __init int ccid3_module_init(void)
1205 int rc = -ENOBUFS;
1207 ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1208 if (ccid3_rx_hist == NULL)
1209 goto out;
1211 ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1212 if (ccid3_tx_hist == NULL)
1213 goto out_free_rx;
1215 ccid3_li_hist = dccp_li_hist_new("ccid3");
1216 if (ccid3_li_hist == NULL)
1217 goto out_free_tx;
1219 rc = ccid_register(&ccid3);
1220 if (rc != 0)
1221 goto out_free_loss_interval_history;
1222 out:
1223 return rc;
1225 out_free_loss_interval_history:
1226 dccp_li_hist_delete(ccid3_li_hist);
1227 ccid3_li_hist = NULL;
1228 out_free_tx:
1229 dccp_tx_hist_delete(ccid3_tx_hist);
1230 ccid3_tx_hist = NULL;
1231 out_free_rx:
1232 dccp_rx_hist_delete(ccid3_rx_hist);
1233 ccid3_rx_hist = NULL;
1234 goto out;
1236 module_init(ccid3_module_init);
1238 static __exit void ccid3_module_exit(void)
1240 #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1242 * Hack to use while developing, so that we get rid of the control
1243 * sock, that is what keeps a refcount on dccp.ko -acme
1245 extern void dccp_ctl_sock_exit(void);
1247 dccp_ctl_sock_exit();
1248 #endif
1249 ccid_unregister(&ccid3);
1251 if (ccid3_tx_hist != NULL) {
1252 dccp_tx_hist_delete(ccid3_tx_hist);
1253 ccid3_tx_hist = NULL;
1255 if (ccid3_rx_hist != NULL) {
1256 dccp_rx_hist_delete(ccid3_rx_hist);
1257 ccid3_rx_hist = NULL;
1259 if (ccid3_li_hist != NULL) {
1260 dccp_li_hist_delete(ccid3_li_hist);
1261 ccid3_li_hist = NULL;
1264 module_exit(ccid3_module_exit);
1266 MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1267 "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
1268 MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1269 MODULE_LICENSE("GPL");
1270 MODULE_ALIAS("net-dccp-ccid-3");