blob: ea30012dd19545cb925a94456bedb6e88addb7b2 [file] [log] [blame]
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001/*
2 * net/dccp/ccids/ccid3.c
3 *
4 * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
Ian McDonald1bc09862005-08-20 00:23:43 -03005 * Copyright (c) 2005 Ian McDonald <iam4@cs.waikato.ac.nz>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07006 *
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/
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070011 *
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
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -030037#include <linux/config.h>
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070038#include "../ccid.h"
39#include "../dccp.h"
Arnaldo Carvalho de Melo4524b252005-08-27 23:18:26 -030040#include "lib/packet_history.h"
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030041#include "lib/loss_interval.h"
Arnaldo Carvalho de Melo36729c12005-08-28 00:47:15 -030042#include "lib/tfrc.h"
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070043#include "ccid3.h"
44
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -030045/*
Arnaldo Carvalho de Melo0ba7a3b2005-09-09 02:28:47 -030046 * Reason for maths here is to avoid 32 bit overflow when a is big.
47 * With this we get close to the limit.
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -030048 */
49static inline u32 usecs_div(const u32 a, const u32 b)
50{
Arnaldo Carvalho de Melo0ba7a3b2005-09-09 02:28:47 -030051 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;
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -030062}
63
Arnaldo Carvalho de Melocfc3c522005-08-27 20:20:37 -030064static int ccid3_debug;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070065
Arnaldo Carvalho de Melocfc3c522005-08-27 20:20:37 -030066#ifdef CCID3_DEBUG
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070067#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
Arnaldo Carvalho de Meloa1d3a352005-08-13 22:42:25 -030075static struct dccp_tx_hist *ccid3_tx_hist;
76static struct dccp_rx_hist *ccid3_rx_hist;
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -030077static struct dccp_li_hist *ccid3_li_hist;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -070078
79static int ccid3_init(struct sock *sk)
80{
81 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
82 return 0;
83}
84
85static void ccid3_exit(struct sock *sk)
86{
87 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
88}
89
90/* TFRC sender states */
91enum ccid3_hc_tx_states {
92 TFRC_SSTATE_NO_SENT = 1,
93 TFRC_SSTATE_NO_FBACK,
94 TFRC_SSTATE_FBACK,
95 TFRC_SSTATE_TERM,
96};
97
98#ifdef CCID3_DEBUG
99static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
100{
101 static char *ccid3_state_names[] = {
102 [TFRC_SSTATE_NO_SENT] = "NO_SENT",
103 [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
104 [TFRC_SSTATE_FBACK] = "FBACK",
105 [TFRC_SSTATE_TERM] = "TERM",
106 };
107
108 return ccid3_state_names[state];
109}
110#endif
111
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300112static inline void ccid3_hc_tx_set_state(struct sock *sk,
113 enum ccid3_hc_tx_states state)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700114{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300115 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700116 enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
117
118 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300119 dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
120 ccid3_tx_state_name(state));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700121 WARN_ON(state == oldstate);
122 hctx->ccid3hctx_state = state;
123}
124
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700125/* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
126static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
127{
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -0300128 /*
129 * If no feedback spec says t_ipi is 1 second (set elsewhere and then
130 * doubles after every no feedback timer (separate function)
131 */
132 if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
133 hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
134 hctx->ccid3hctx_x);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700135}
136
137/* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
138static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
139{
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300140 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
141 TFRC_OPSYS_HALF_TIME_GRAN);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700142}
143
144/*
145 * Update X by
146 * If (p > 0)
147 * x_calc = calcX(s, R, p);
148 * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
149 * Else
150 * If (now - tld >= R)
151 * X = max(min(2 * X, 2 * X_recv), s / R);
152 * tld = now;
153 */
154static void ccid3_hc_tx_update_x(struct sock *sk)
155{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300156 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700157
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300158 /* To avoid large error in calcX */
159 if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
Arnaldo Carvalho de Melo36729c12005-08-28 00:47:15 -0300160 hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
161 hctx->ccid3hctx_rtt,
162 hctx->ccid3hctx_p);
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300163 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
164 2 * hctx->ccid3hctx_x_recv),
165 (hctx->ccid3hctx_s /
166 TFRC_MAX_BACK_OFF_TIME));
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300167 } else {
168 struct timeval now;
169
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -0300170 dccp_timestamp(sk, &now);
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300171 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
172 hctx->ccid3hctx_rtt) {
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -0300173 hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
174 hctx->ccid3hctx_x) * 2,
175 usecs_div(hctx->ccid3hctx_s,
176 hctx->ccid3hctx_rtt));
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300177 hctx->ccid3hctx_t_ld = now;
178 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700179 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700180}
181
182static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
183{
184 struct sock *sk = (struct sock *)data;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700185 unsigned long next_tmout = 0;
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300186 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700187
188 bh_lock_sock(sk);
189 if (sock_owned_by_user(sk)) {
190 /* Try again later. */
191 /* XXX: set some sensible MIB */
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300192 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
193 jiffies + HZ / 5);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700194 goto out;
195 }
196
197 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
198 ccid3_tx_state_name(hctx->ccid3hctx_state));
199
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700200 switch (hctx->ccid3hctx_state) {
201 case TFRC_SSTATE_TERM:
202 goto out;
203 case TFRC_SSTATE_NO_FBACK:
204 /* Halve send rate */
205 hctx->ccid3hctx_x /= 2;
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -0300206 if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
207 TFRC_MAX_BACK_OFF_TIME))
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300208 hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
209 TFRC_MAX_BACK_OFF_TIME);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700210
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300211 ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
212 "bytes/s\n",
213 dccp_role(sk), sk,
214 ccid3_tx_state_name(hctx->ccid3hctx_state),
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700215 hctx->ccid3hctx_x);
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -0300216 next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
217 hctx->ccid3hctx_x),
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300218 TFRC_INITIAL_TIMEOUT);
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300219 /*
220 * FIXME - not sure above calculation is correct. See section
221 * 5 of CCID3 11 should adjust tx_t_ipi and double that to
222 * achieve it really
223 */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700224 break;
225 case TFRC_SSTATE_FBACK:
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300226 /*
227 * Check if IDLE since last timeout and recv rate is less than
228 * 4 packets per RTT
229 */
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300230 if (!hctx->ccid3hctx_idle ||
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -0300231 (hctx->ccid3hctx_x_recv >=
232 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300233 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
234 dccp_role(sk), sk,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700235 ccid3_tx_state_name(hctx->ccid3hctx_state));
236 /* Halve sending rate */
237
238 /* If (X_calc > 2 * X_recv)
239 * X_recv = max(X_recv / 2, s / (2 * t_mbi));
240 * Else
241 * X_recv = X_calc / 4;
242 */
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300243 BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
244 hctx->ccid3hctx_x_calc == 0);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700245
246 /* check also if p is zero -> x_calc is infinity? */
247 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
248 hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
249 hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
250 hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
251 else
252 hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
253
254 /* Update sending rate */
255 ccid3_hc_tx_update_x(sk);
256 }
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -0300257 /*
258 * Schedule no feedback timer to expire in
259 * max(4 * R, 2 * s / X)
260 */
Arnaldo Carvalho de Meloc68e64c2005-08-21 05:07:37 -0300261 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -0300262 2 * usecs_div(hctx->ccid3hctx_s,
263 hctx->ccid3hctx_x));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700264 break;
265 default:
266 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
267 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
268 dump_stack();
269 goto out;
270 }
271
272 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300273 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700274 hctx->ccid3hctx_idle = 1;
275out:
276 bh_unlock_sock(sk);
277 sock_put(sk);
278}
279
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700280static int ccid3_hc_tx_send_packet(struct sock *sk,
281 struct sk_buff *skb, int len)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700282{
283 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300284 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300285 struct dccp_tx_hist_entry *new_packet;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700286 struct timeval now;
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700287 long delay;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700288 int rc = -ENOTCONN;
289
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300290 /* Check if pure ACK or Terminating*/
291
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700292 /*
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300293 * XXX: We only call this function for DATA and DATAACK, on, these
294 * packets can have zero length, but why the comment about "pure ACK"?
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700295 */
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300296 if (hctx == NULL || len == 0 ||
297 hctx->ccid3hctx_state == TFRC_SSTATE_TERM)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700298 goto out;
299
300 /* See if last packet allocated was not sent */
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300301 new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
302 if (new_packet == NULL || new_packet->dccphtx_sent) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300303 new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
304 SLAB_ATOMIC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700305
306 rc = -ENOBUFS;
307 if (new_packet == NULL) {
308 ccid3_pr_debug("%s, sk=%p, not enough mem to add "
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300309 "to history, send refused\n",
310 dccp_role(sk), sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700311 goto out;
312 }
313
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300314 dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700315 }
316
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -0300317 dccp_timestamp(sk, &now);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700318
319 switch (hctx->ccid3hctx_state) {
320 case TFRC_SSTATE_NO_SENT:
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300321 ccid3_pr_debug("%s, sk=%p, first packet(%llu)\n",
322 dccp_role(sk), sk, dp->dccps_gss);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700323
324 hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
325 hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300326 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
327 jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700328 hctx->ccid3hctx_last_win_count = 0;
329 hctx->ccid3hctx_t_last_win_count = now;
330 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
331 hctx->ccid3hctx_t_ipi = TFRC_INITIAL_TIMEOUT;
332
333 /* Set nominal send time for initial packet */
334 hctx->ccid3hctx_t_nom = now;
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300335 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
336 hctx->ccid3hctx_t_ipi);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700337 ccid3_calc_new_delta(hctx);
338 rc = 0;
339 break;
340 case TFRC_SSTATE_NO_FBACK:
341 case TFRC_SSTATE_FBACK:
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300342 delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
343 hctx->ccid3hctx_delta);
Arnaldo Carvalho de Melo27258ee2005-08-09 20:30:56 -0700344 ccid3_pr_debug("send_packet delay=%ld\n", delay);
345 delay /= -1000;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700346 /* divide by -1000 is to convert to ms and get sign right */
Arnaldo Carvalho de Melod6809c12005-08-27 03:06:35 -0300347 rc = delay > 0 ? delay : 0;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700348 break;
349 default:
350 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
351 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
352 dump_stack();
353 rc = -EINVAL;
354 break;
355 }
356
357 /* Can we send? if so add options and add to packet history */
Arnaldo Carvalho de Melo507d37c2005-09-09 02:30:07 -0300358 if (rc == 0) {
359 dp->dccps_hc_tx_insert_options = 1;
Arnaldo Carvalho de Meloc1734372005-08-13 20:34:23 -0300360 new_packet->dccphtx_ccval =
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300361 DCCP_SKB_CB(skb)->dccpd_ccval =
362 hctx->ccid3hctx_last_win_count;
Arnaldo Carvalho de Melo507d37c2005-09-09 02:30:07 -0300363 }
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700364out:
365 return rc;
366}
367
368static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
369{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300370 const struct dccp_sock *dp = dccp_sk(sk);
371 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700372 struct timeval now;
373
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700374 BUG_ON(hctx == NULL);
375
376 if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
377 ccid3_pr_debug("%s, sk=%p, while state is TFRC_SSTATE_TERM!\n",
378 dccp_role(sk), sk);
379 return;
380 }
381
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -0300382 dccp_timestamp(sk, &now);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700383
384 /* check if we have sent a data packet */
385 if (len > 0) {
386 unsigned long quarter_rtt;
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300387 struct dccp_tx_hist_entry *packet;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700388
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300389 packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
390 if (packet == NULL) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300391 printk(KERN_CRIT "%s: packet doesn't exists in "
392 "history!\n", __FUNCTION__);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700393 return;
394 }
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300395 if (packet->dccphtx_sent) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300396 printk(KERN_CRIT "%s: no unsent packet in history!\n",
397 __FUNCTION__);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700398 return;
399 }
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300400 packet->dccphtx_tstamp = now;
401 packet->dccphtx_seqno = dp->dccps_gss;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700402 /*
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300403 * Check if win_count have changed
404 * Algorithm in "8.1. Window Counter Valuer" in
405 * draft-ietf-dccp-ccid3-11.txt
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700406 */
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -0300407 quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
408 if (likely(hctx->ccid3hctx_rtt > 8))
409 quarter_rtt /= hctx->ccid3hctx_rtt / 4;
410
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700411 if (quarter_rtt > 0) {
412 hctx->ccid3hctx_t_last_win_count = now;
413 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count +
414 min_t(unsigned long, quarter_rtt, 5)) % 16;
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300415 ccid3_pr_debug("%s, sk=%p, window changed from "
416 "%u to %u!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700417 dccp_role(sk), sk,
Arnaldo Carvalho de Meloc1734372005-08-13 20:34:23 -0300418 packet->dccphtx_ccval,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700419 hctx->ccid3hctx_last_win_count);
420 }
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300421
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700422 hctx->ccid3hctx_idle = 0;
Arnaldo Carvalho de Meloc1734372005-08-13 20:34:23 -0300423 packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300424 packet->dccphtx_sent = 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700425 } else
426 ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
427 dccp_role(sk), sk, dp->dccps_gss);
428
429 switch (hctx->ccid3hctx_state) {
430 case TFRC_SSTATE_NO_SENT:
431 /* if first wasn't pure ack */
432 if (len != 0)
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300433 printk(KERN_CRIT "%s: %s, First packet sent is noted "
434 "as a data packet\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700435 __FUNCTION__, dccp_role(sk));
436 return;
437 case TFRC_SSTATE_NO_FBACK:
438 case TFRC_SSTATE_FBACK:
439 if (len > 0) {
440 hctx->ccid3hctx_t_nom = now;
441 ccid3_calc_new_t_ipi(hctx);
442 ccid3_calc_new_delta(hctx);
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300443 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
444 hctx->ccid3hctx_t_ipi);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700445 }
446 break;
447 default:
448 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
449 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
450 dump_stack();
451 break;
452 }
453}
454
455static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
456{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300457 const struct dccp_sock *dp = dccp_sk(sk);
458 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700459 struct ccid3_options_received *opt_recv;
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300460 struct dccp_tx_hist_entry *packet;
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -0300461 struct timeval now;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700462 unsigned long next_tmout;
Ian McDonald1bc09862005-08-20 00:23:43 -0300463 u32 t_elapsed;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700464 u32 pinv;
465 u32 x_recv;
466 u32 r_sample;
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300467
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700468 if (hctx == NULL)
469 return;
470
471 if (hctx->ccid3hctx_state == TFRC_SSTATE_TERM) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300472 ccid3_pr_debug("%s, sk=%p, received a packet when "
473 "terminating!\n", dccp_role(sk), sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700474 return;
475 }
476
477 /* we are only interested in ACKs */
478 if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
479 DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
480 return;
481
482 opt_recv = &hctx->ccid3hctx_options_received;
483
Arnaldo Carvalho de Melo1a285992005-09-09 02:32:56 -0300484 t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700485 x_recv = opt_recv->ccid3or_receive_rate;
486 pinv = opt_recv->ccid3or_loss_event_rate;
487
488 switch (hctx->ccid3hctx_state) {
489 case TFRC_SSTATE_NO_SENT:
490 /* FIXME: what to do here? */
491 return;
492 case TFRC_SSTATE_NO_FBACK:
493 case TFRC_SSTATE_FBACK:
494 /* Calculate new round trip sample by
495 * R_sample = (now - t_recvdata) - t_delay */
496 /* get t_recvdata from history */
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300497 packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
498 DCCP_SKB_CB(skb)->dccpd_ack_seq);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700499 if (packet == NULL) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300500 ccid3_pr_debug("%s, sk=%p, seqno %llu(%s) does't "
501 "exist in history!\n",
502 dccp_role(sk), sk,
503 DCCP_SKB_CB(skb)->dccpd_ack_seq,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700504 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
505 return;
506 }
507
508 /* Update RTT */
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -0300509 dccp_timestamp(sk, &now);
510 r_sample = timeval_delta(&now, &packet->dccphtx_tstamp);
Arnaldo Carvalho de Melo1a285992005-09-09 02:32:56 -0300511 if (unlikely(r_sample <= t_elapsed))
512 LIMIT_NETDEBUG(KERN_WARNING
513 "%s: r_sample=%uus, t_elapsed=%uus\n",
514 __FUNCTION__, r_sample, t_elapsed);
515 else
516 r_sample -= t_elapsed;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700517
518 /* Update RTT estimate by
519 * If (No feedback recv)
520 * R = R_sample;
521 * Else
522 * R = q * R + (1 - q) * R_sample;
523 *
524 * q is a constant, RFC 3448 recomments 0.9
525 */
526 if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
527 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
528 hctx->ccid3hctx_rtt = r_sample;
529 } else
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300530 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
531 r_sample / 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700532
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300533 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
534 "r_sample=%us\n", dccp_role(sk), sk,
535 hctx->ccid3hctx_rtt, r_sample);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700536
537 /* Update timeout interval */
Arnaldo Carvalho de Meloc68e64c2005-08-21 05:07:37 -0300538 hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
539 USEC_PER_SEC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700540
541 /* Update receive rate */
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300542 hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700543
544 /* Update loss event rate */
545 if (pinv == ~0 || pinv == 0)
546 hctx->ccid3hctx_p = 0;
547 else {
548 hctx->ccid3hctx_p = 1000000 / pinv;
549
550 if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
551 hctx->ccid3hctx_p = TFRC_SMALLEST_P;
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300552 ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
553 dccp_role(sk), sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700554 }
555 }
556
557 /* unschedule no feedback timer */
558 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
559
560 /* Update sending rate */
561 ccid3_hc_tx_update_x(sk);
562
563 /* Update next send time */
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300564 timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
565 hctx->ccid3hctx_t_ipi);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700566 ccid3_calc_new_t_ipi(hctx);
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300567 timeval_add_usecs(&hctx->ccid3hctx_t_nom,
568 hctx->ccid3hctx_t_ipi);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700569 ccid3_calc_new_delta(hctx);
570
571 /* remove all packets older than the one acked from history */
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300572 dccp_tx_hist_purge_older(ccid3_tx_hist,
573 &hctx->ccid3hctx_hist, packet);
Arnaldo Carvalho de Meloc530cfb2005-08-29 02:15:54 -0300574 /*
575 * As we have calculated new ipi, delta, t_nom it is possible that
576 * we now can send a packet, so wake up dccp_wait_for_ccids.
577 */
578 sk->sk_write_space(sk);
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300579
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300580 /*
581 * Schedule no feedback timer to expire in
582 * max(4 * R, 2 * s / X)
583 */
Arnaldo Carvalho de Meloc68e64c2005-08-21 05:07:37 -0300584 next_tmout = max(hctx->ccid3hctx_t_rto,
Arnaldo Carvalho de Melo6b5e6332005-08-27 20:11:28 -0300585 2 * usecs_div(hctx->ccid3hctx_s,
586 hctx->ccid3hctx_x));
587
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300588 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
589 "expire in %lu jiffies (%luus)\n",
590 dccp_role(sk), sk,
591 usecs_to_jiffies(next_tmout), next_tmout);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700592
593 sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300594 jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700595
596 /* set idle flag */
597 hctx->ccid3hctx_idle = 1;
598 break;
599 default:
600 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
601 __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
602 dump_stack();
603 break;
604 }
605}
606
607static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
608{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300609 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700610
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300611 if (hctx == NULL || !(sk->sk_state == DCCP_OPEN ||
612 sk->sk_state == DCCP_PARTOPEN))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700613 return;
614
615 DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
616}
617
618static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300619 unsigned char len, u16 idx,
620 unsigned char *value)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700621{
622 int rc = 0;
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300623 const struct dccp_sock *dp = dccp_sk(sk);
624 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700625 struct ccid3_options_received *opt_recv;
626
627 if (hctx == NULL)
628 return 0;
629
630 opt_recv = &hctx->ccid3hctx_options_received;
631
632 if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
633 opt_recv->ccid3or_seqno = dp->dccps_gsr;
634 opt_recv->ccid3or_loss_event_rate = ~0;
635 opt_recv->ccid3or_loss_intervals_idx = 0;
636 opt_recv->ccid3or_loss_intervals_len = 0;
637 opt_recv->ccid3or_receive_rate = 0;
638 }
639
640 switch (option) {
641 case TFRC_OPT_LOSS_EVENT_RATE:
642 if (len != 4) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300643 ccid3_pr_debug("%s, sk=%p, invalid len for "
644 "TFRC_OPT_LOSS_EVENT_RATE\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700645 dccp_role(sk), sk);
646 rc = -EINVAL;
647 } else {
648 opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
649 ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
650 dccp_role(sk), sk,
651 opt_recv->ccid3or_loss_event_rate);
652 }
653 break;
654 case TFRC_OPT_LOSS_INTERVALS:
655 opt_recv->ccid3or_loss_intervals_idx = idx;
656 opt_recv->ccid3or_loss_intervals_len = len;
657 ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
658 dccp_role(sk), sk,
659 opt_recv->ccid3or_loss_intervals_idx,
660 opt_recv->ccid3or_loss_intervals_len);
661 break;
662 case TFRC_OPT_RECEIVE_RATE:
663 if (len != 4) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300664 ccid3_pr_debug("%s, sk=%p, invalid len for "
665 "TFRC_OPT_RECEIVE_RATE\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700666 dccp_role(sk), sk);
667 rc = -EINVAL;
668 } else {
669 opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
670 ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
671 dccp_role(sk), sk,
672 opt_recv->ccid3or_receive_rate);
673 }
674 break;
675 }
676
677 return rc;
678}
679
680static int ccid3_hc_tx_init(struct sock *sk)
681{
682 struct dccp_sock *dp = dccp_sk(sk);
683 struct ccid3_hc_tx_sock *hctx;
684
685 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
686
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300687 dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx), gfp_any());
688 if (dp->dccps_hc_tx_ccid_private == NULL)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700689 return -ENOMEM;
690
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300691 hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700692 memset(hctx, 0, sizeof(*hctx));
693
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -0300694 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
695 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
696 hctx->ccid3hctx_s = dp->dccps_packet_size;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700697 else
698 hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
699
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300700 /* Set transmission rate to 1 packet per second */
701 hctx->ccid3hctx_x = hctx->ccid3hctx_s;
Arnaldo Carvalho de Meloc68e64c2005-08-21 05:07:37 -0300702 hctx->ccid3hctx_t_rto = USEC_PER_SEC;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700703 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
704 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
705 init_timer(&hctx->ccid3hctx_no_feedback_timer);
706
707 return 0;
708}
709
710static void ccid3_hc_tx_exit(struct sock *sk)
711{
712 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300713 struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700714
715 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
716 BUG_ON(hctx == NULL);
717
718 ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
719 sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
720
721 /* Empty packet history */
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300722 dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700723
724 kfree(dp->dccps_hc_tx_ccid_private);
725 dp->dccps_hc_tx_ccid_private = NULL;
726}
727
728/*
729 * RX Half Connection methods
730 */
731
732/* TFRC receiver states */
733enum ccid3_hc_rx_states {
734 TFRC_RSTATE_NO_DATA = 1,
735 TFRC_RSTATE_DATA,
736 TFRC_RSTATE_TERM = 127,
737};
738
739#ifdef CCID3_DEBUG
740static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
741{
742 static char *ccid3_rx_state_names[] = {
743 [TFRC_RSTATE_NO_DATA] = "NO_DATA",
744 [TFRC_RSTATE_DATA] = "DATA",
745 [TFRC_RSTATE_TERM] = "TERM",
746 };
747
748 return ccid3_rx_state_names[state];
749}
750#endif
751
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300752static inline void ccid3_hc_rx_set_state(struct sock *sk,
753 enum ccid3_hc_rx_states state)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700754{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300755 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700756 enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
757
758 ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300759 dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
760 ccid3_rx_state_name(state));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700761 WARN_ON(state == oldstate);
762 hcrx->ccid3hcrx_state = state;
763}
764
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700765static void ccid3_hc_rx_send_feedback(struct sock *sk)
766{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300767 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700768 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300769 struct dccp_rx_hist_entry *packet;
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300770 struct timeval now;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700771
772 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
773
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -0300774 dccp_timestamp(sk, &now);
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300775
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700776 switch (hcrx->ccid3hcrx_state) {
777 case TFRC_RSTATE_NO_DATA:
778 hcrx->ccid3hcrx_x_recv = 0;
779 break;
780 case TFRC_RSTATE_DATA: {
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300781 const u32 delta = timeval_delta(&now,
782 &hcrx->ccid3hcrx_tstamp_last_feedback);
Arnaldo Carvalho de Melo27ae543e2005-09-09 02:31:07 -0300783 hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
784 delta);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700785 }
786 break;
787 default:
788 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
789 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
790 dump_stack();
791 return;
792 }
793
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300794 packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700795 if (packet == NULL) {
796 printk(KERN_CRIT "%s: %s, sk=%p, no data packet in history!\n",
797 __FUNCTION__, dccp_role(sk), sk);
798 dump_stack();
799 return;
800 }
801
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300802 hcrx->ccid3hcrx_tstamp_last_feedback = now;
Arnaldo Carvalho de Meloc1734372005-08-13 20:34:23 -0300803 hcrx->ccid3hcrx_last_counter = packet->dccphrx_ccval;
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300804 hcrx->ccid3hcrx_seqno_last_counter = packet->dccphrx_seqno;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700805 hcrx->ccid3hcrx_bytes_recv = 0;
806
807 /* Convert to multiples of 10us */
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300808 hcrx->ccid3hcrx_elapsed_time =
809 timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700810 if (hcrx->ccid3hcrx_p == 0)
811 hcrx->ccid3hcrx_pinv = ~0;
812 else
813 hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
Arnaldo Carvalho de Melo507d37c2005-09-09 02:30:07 -0300814 dp->dccps_hc_rx_insert_options = 1;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700815 dccp_send_ack(sk);
816}
817
818static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
819{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300820 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo4fded332005-08-23 21:51:59 -0700821 u32 x_recv, pinv;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700822
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300823 if (hcrx == NULL || !(sk->sk_state == DCCP_OPEN ||
824 sk->sk_state == DCCP_PARTOPEN))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700825 return;
826
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700827 DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
Arnaldo Carvalho de Melo4fded332005-08-23 21:51:59 -0700828
829 if (dccp_packet_without_ack(skb))
830 return;
831
832 if (hcrx->ccid3hcrx_elapsed_time != 0)
833 dccp_insert_option_elapsed_time(sk, skb,
834 hcrx->ccid3hcrx_elapsed_time);
835 dccp_insert_option_timestamp(sk, skb);
836 x_recv = htonl(hcrx->ccid3hcrx_x_recv);
837 pinv = htonl(hcrx->ccid3hcrx_pinv);
838 dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
839 &pinv, sizeof(pinv));
840 dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
841 &x_recv, sizeof(x_recv));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700842}
843
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700844/* calculate first loss interval
845 *
846 * returns estimated loss interval in usecs */
847
848static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
849{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300850 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300851 struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700852 u32 rtt, delta, x_recv, fval, p, tmp2;
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300853 struct timeval tstamp = { 0, };
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700854 int interval = 0;
855 int win_count = 0;
856 int step = 0;
857 u64 tmp1;
858
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300859 list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
860 dccphrx_node) {
861 if (dccp_rx_hist_entry_data_packet(entry)) {
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700862 tail = entry;
863
864 switch (step) {
865 case 0:
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300866 tstamp = entry->dccphrx_tstamp;
Arnaldo Carvalho de Meloc1734372005-08-13 20:34:23 -0300867 win_count = entry->dccphrx_ccval;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700868 step = 1;
869 break;
870 case 1:
Arnaldo Carvalho de Meloc1734372005-08-13 20:34:23 -0300871 interval = win_count - entry->dccphrx_ccval;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700872 if (interval < 0)
873 interval += TFRC_WIN_COUNT_LIMIT;
874 if (interval > 4)
875 goto found;
876 break;
877 }
878 }
879 }
880
881 if (step == 0) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300882 printk(KERN_CRIT "%s: %s, sk=%p, packet history contains no "
883 "data packets!\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700884 __FUNCTION__, dccp_role(sk), sk);
885 return ~0;
886 }
887
888 if (interval == 0) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300889 ccid3_pr_debug("%s, sk=%p, Could not find a win_count "
890 "interval > 0. Defaulting to 1\n",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700891 dccp_role(sk), sk);
892 interval = 1;
893 }
894found:
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -0300895 rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700896 ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
897 dccp_role(sk), sk, rtt);
898 if (rtt == 0)
899 rtt = 1;
900
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -0300901 dccp_timestamp(sk, &tstamp);
902 delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
903 x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv, delta);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700904
905 tmp1 = (u64)x_recv * (u64)rtt;
906 do_div(tmp1,10000000);
907 tmp2 = (u32)tmp1;
908 fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
909 /* do not alter order above or you will get overflow on 32 bit */
Arnaldo Carvalho de Melo36729c12005-08-28 00:47:15 -0300910 p = tfrc_calc_x_reverse_lookup(fval);
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300911 ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
912 "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700913
914 if (p == 0)
915 return ~0;
916 else
917 return 1000000 / p;
918}
919
920static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
921{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300922 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700923
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300924 if (seq_loss != DCCP_MAX_SEQNO + 1 &&
925 list_empty(&hcrx->ccid3hcrx_li_hist)) {
926 struct dccp_li_hist_entry *li_tail;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700927
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -0300928 li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
929 &hcrx->ccid3hcrx_li_hist,
930 seq_loss, win_loss);
931 if (li_tail == NULL)
932 return;
933 li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700934 }
935 /* FIXME: find end of interval */
936}
937
938static void ccid3_hc_rx_detect_loss(struct sock *sk)
939{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300940 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo29e4f8b2005-08-28 02:00:28 -0300941 u8 win_loss;
942 const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
943 &hcrx->ccid3hcrx_li_hist,
944 &win_loss);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700945
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700946 ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
947}
948
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700949static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
950{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300951 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo4fded332005-08-23 21:51:59 -0700952 const struct dccp_options_received *opt_recv;
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -0300953 struct dccp_rx_hist_entry *packet;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700954 struct timeval now;
955 u8 win_count;
Arnaldo Carvalho de Melob3a30772005-09-09 02:34:10 -0300956 u32 p_prev, r_sample, t_elapsed;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700957 int ins;
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -0300958
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700959 if (hcrx == NULL)
960 return;
961
962 BUG_ON(!(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
963 hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
964
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -0300965 opt_recv = &dccp_sk(sk)->dccps_options_received;
Arnaldo Carvalho de Melo4fded332005-08-23 21:51:59 -0700966
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700967 switch (DCCP_SKB_CB(skb)->dccpd_type) {
968 case DCCP_PKT_ACK:
969 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
970 return;
971 case DCCP_PKT_DATAACK:
Arnaldo Carvalho de Melo4fded332005-08-23 21:51:59 -0700972 if (opt_recv->dccpor_timestamp_echo == 0)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700973 break;
974 p_prev = hcrx->ccid3hcrx_rtt;
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -0300975 dccp_timestamp(sk, &now);
Arnaldo Carvalho de Melob3a30772005-09-09 02:34:10 -0300976 timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
977 r_sample = timeval_usecs(&now);
978 t_elapsed = opt_recv->dccpor_elapsed_time * 10;
979
980 if (unlikely(r_sample <= t_elapsed))
981 LIMIT_NETDEBUG(KERN_WARNING
982 "%s: r_sample=%uus, t_elapsed=%uus\n",
983 __FUNCTION__, r_sample, t_elapsed);
984 else
985 r_sample -= t_elapsed;
986
987 if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
988 hcrx->ccid3hcrx_rtt = r_sample;
989 else
990 hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
991 r_sample / 10;
992
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700993 if (p_prev != hcrx->ccid3hcrx_rtt)
Arnaldo Carvalho de Melo4fded332005-08-23 21:51:59 -0700994 ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
995 dccp_role(sk), hcrx->ccid3hcrx_rtt,
996 opt_recv->dccpor_elapsed_time);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -0700997 break;
998 case DCCP_PKT_DATA:
999 break;
1000 default:
1001 ccid3_pr_debug("%s, sk=%p, not DATA/DATAACK/ACK packet(%s)\n",
1002 dccp_role(sk), sk,
1003 dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
1004 return;
1005 }
1006
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -03001007 packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001008 skb, SLAB_ATOMIC);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001009 if (packet == NULL) {
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -03001010 ccid3_pr_debug("%s, sk=%p, Not enough mem to add rx packet "
1011 "to history (consider it lost)!",
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001012 dccp_role(sk), sk);
1013 return;
1014 }
1015
Arnaldo Carvalho de Meloc1734372005-08-13 20:34:23 -03001016 win_count = packet->dccphrx_ccval;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001017
Arnaldo Carvalho de Melo072ab6c2005-08-28 01:19:14 -03001018 ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
1019 &hcrx->ccid3hcrx_li_hist, packet);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001020
1021 if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
1022 return;
1023
1024 switch (hcrx->ccid3hcrx_state) {
1025 case TFRC_RSTATE_NO_DATA:
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -03001026 ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
1027 "feedback\n",
1028 dccp_role(sk), sk,
1029 dccp_state_name(sk->sk_state), skb);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001030 ccid3_hc_rx_send_feedback(sk);
1031 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
1032 return;
1033 case TFRC_RSTATE_DATA:
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -03001034 hcrx->ccid3hcrx_bytes_recv += skb->len -
1035 dccp_hdr(skb)->dccph_doff * 4;
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -03001036 if (ins != 0)
1037 break;
1038
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -03001039 dccp_timestamp(sk, &now);
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -03001040 if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
1041 hcrx->ccid3hcrx_rtt) {
1042 hcrx->ccid3hcrx_tstamp_last_ack = now;
1043 ccid3_hc_rx_send_feedback(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001044 }
Arnaldo Carvalho de Melob6ee3d42005-08-27 18:18:18 -03001045 return;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001046 default:
1047 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
1048 __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
1049 dump_stack();
1050 return;
1051 }
1052
1053 /* Dealing with packet loss */
Arnaldo Carvalho de Melo4fded332005-08-23 21:51:59 -07001054 ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
1055 dccp_role(sk), sk, dccp_state_name(sk->sk_state));
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001056
1057 ccid3_hc_rx_detect_loss(sk);
1058 p_prev = hcrx->ccid3hcrx_p;
1059
1060 /* Calculate loss event rate */
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -03001061 if (!list_empty(&hcrx->ccid3hcrx_li_hist))
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001062 /* Scaling up by 1000000 as fixed decimal */
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -03001063 hcrx->ccid3hcrx_p = 1000000 / dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001064
1065 if (hcrx->ccid3hcrx_p > p_prev) {
1066 ccid3_hc_rx_send_feedback(sk);
1067 return;
1068 }
1069}
1070
1071static int ccid3_hc_rx_init(struct sock *sk)
1072{
1073 struct dccp_sock *dp = dccp_sk(sk);
1074 struct ccid3_hc_rx_sock *hcrx;
1075
1076 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1077
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -03001078 dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx), gfp_any());
1079 if (dp->dccps_hc_rx_ccid_private == NULL)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001080 return -ENOMEM;
1081
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -03001082 hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001083 memset(hcrx, 0, sizeof(*hcrx));
1084
Arnaldo Carvalho de Meloa84ffe42005-08-28 04:51:32 -03001085 if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
1086 dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
1087 hcrx->ccid3hcrx_s = dp->dccps_packet_size;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001088 else
1089 hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
1090
1091 hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
1092 INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -03001093 INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
Arnaldo Carvalho de Melob0e56782005-09-09 02:38:35 -03001094 dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
Arnaldo Carvalho de Melo954ee312005-09-09 02:37:05 -03001095 hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
1096 hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001097 return 0;
1098}
1099
1100static void ccid3_hc_rx_exit(struct sock *sk)
1101{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -03001102 struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001103 struct dccp_sock *dp = dccp_sk(sk);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001104
1105 ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
1106
1107 if (hcrx == NULL)
1108 return;
1109
1110 ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
1111
1112 /* Empty packet history */
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001113 dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001114
1115 /* Empty loss interval history */
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -03001116 dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001117
1118 kfree(dp->dccps_hc_rx_ccid_private);
1119 dp->dccps_hc_rx_ccid_private = NULL;
1120}
1121
Arnaldo Carvalho de Melo2babe1f2005-08-23 21:52:35 -07001122static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
1123{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -03001124 const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
Arnaldo Carvalho de Melo2babe1f2005-08-23 21:52:35 -07001125
1126 if (hcrx == NULL)
1127 return;
1128
1129 info->tcpi_ca_state = hcrx->ccid3hcrx_state;
1130 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1131 info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
1132}
1133
1134static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
1135{
Arnaldo Carvalho de Melo59725dc2005-09-09 02:40:58 -03001136 const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
Arnaldo Carvalho de Melo2babe1f2005-08-23 21:52:35 -07001137
1138 if (hctx == NULL)
1139 return;
1140
1141 info->tcpi_rto = hctx->ccid3hctx_t_rto;
1142 info->tcpi_rtt = hctx->ccid3hctx_rtt;
1143}
1144
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001145static struct ccid ccid3 = {
1146 .ccid_id = 3,
1147 .ccid_name = "ccid3",
1148 .ccid_owner = THIS_MODULE,
1149 .ccid_init = ccid3_init,
1150 .ccid_exit = ccid3_exit,
1151 .ccid_hc_tx_init = ccid3_hc_tx_init,
1152 .ccid_hc_tx_exit = ccid3_hc_tx_exit,
1153 .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
1154 .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
1155 .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
1156 .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
1157 .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
1158 .ccid_hc_rx_init = ccid3_hc_rx_init,
1159 .ccid_hc_rx_exit = ccid3_hc_rx_exit,
1160 .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
1161 .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
Arnaldo Carvalho de Melo2babe1f2005-08-23 21:52:35 -07001162 .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
1163 .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001164};
1165
1166module_param(ccid3_debug, int, 0444);
1167MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
1168
1169static __init int ccid3_module_init(void)
1170{
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001171 int rc = -ENOBUFS;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001172
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001173 ccid3_rx_hist = dccp_rx_hist_new("ccid3");
1174 if (ccid3_rx_hist == NULL)
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001175 goto out;
1176
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001177 ccid3_tx_hist = dccp_tx_hist_new("ccid3");
1178 if (ccid3_tx_hist == NULL)
1179 goto out_free_rx;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001180
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -03001181 ccid3_li_hist = dccp_li_hist_new("ccid3");
1182 if (ccid3_li_hist == NULL)
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001183 goto out_free_tx;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001184
1185 rc = ccid_register(&ccid3);
1186 if (rc != 0)
1187 goto out_free_loss_interval_history;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001188out:
1189 return rc;
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001190
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001191out_free_loss_interval_history:
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -03001192 dccp_li_hist_delete(ccid3_li_hist);
1193 ccid3_li_hist = NULL;
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001194out_free_tx:
1195 dccp_tx_hist_delete(ccid3_tx_hist);
1196 ccid3_tx_hist = NULL;
1197out_free_rx:
1198 dccp_rx_hist_delete(ccid3_rx_hist);
1199 ccid3_rx_hist = NULL;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001200 goto out;
1201}
1202module_init(ccid3_module_init);
1203
1204static __exit void ccid3_module_exit(void)
1205{
Arnaldo Carvalho de Melo725ba8e2005-08-13 20:35:39 -03001206#ifdef CONFIG_IP_DCCP_UNLOAD_HACK
1207 /*
1208 * Hack to use while developing, so that we get rid of the control
1209 * sock, that is what keeps a refcount on dccp.ko -acme
1210 */
1211 extern void dccp_ctl_sock_exit(void);
1212
1213 dccp_ctl_sock_exit();
1214#endif
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001215 ccid_unregister(&ccid3);
1216
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001217 if (ccid3_tx_hist != NULL) {
1218 dccp_tx_hist_delete(ccid3_tx_hist);
1219 ccid3_tx_hist = NULL;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001220 }
Arnaldo Carvalho de Melo8c60f3f2005-08-10 12:59:38 -03001221 if (ccid3_rx_hist != NULL) {
1222 dccp_rx_hist_delete(ccid3_rx_hist);
1223 ccid3_rx_hist = NULL;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001224 }
Arnaldo Carvalho de Meloae6706f2005-08-27 23:03:09 -03001225 if (ccid3_li_hist != NULL) {
1226 dccp_li_hist_delete(ccid3_li_hist);
1227 ccid3_li_hist = NULL;
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001228 }
1229}
1230module_exit(ccid3_module_exit);
1231
Arnaldo Carvalho de Melo1f2333a2005-08-27 03:51:58 -03001232MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
1233 "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
Arnaldo Carvalho de Melo7c657872005-08-09 20:14:34 -07001234MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
1235MODULE_LICENSE("GPL");
1236MODULE_ALIAS("net-dccp-ccid-3");