ccid3.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*
  2. * net/dccp/ccids/ccid3.c
  3. *
  4. * Copyright (c) 2005 The University of Waikato, Hamilton, New Zealand.
  5. * Copyright (c) 2005-6 Ian McDonald <ian.mcdonald@jandi.co.nz>
  6. *
  7. * An implementation of the DCCP protocol
  8. *
  9. * This code has been developed by the University of Waikato WAND
  10. * research group. For further information please see http://www.wand.net.nz/
  11. *
  12. * This code also uses code from Lulea University, rereleased as GPL by its
  13. * authors:
  14. * Copyright (c) 2003 Nils-Erik Mattsson, Joacim Haggmark, Magnus Erixzon
  15. *
  16. * Changes to meet Linux coding standards, to make it meet latest ccid3 draft
  17. * and to make it work as a loadable module in the DCCP stack written by
  18. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>.
  19. *
  20. * Copyright (c) 2005 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License as published by
  24. * the Free Software Foundation; either version 2 of the License, or
  25. * (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to the Free Software
  34. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35. */
  36. #include "../ccid.h"
  37. #include "../dccp.h"
  38. #include "lib/packet_history.h"
  39. #include "lib/loss_interval.h"
  40. #include "lib/tfrc.h"
  41. #include "ccid3.h"
  42. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  43. static int ccid3_debug;
  44. #define ccid3_pr_debug(format, a...) DCCP_PR_DEBUG(ccid3_debug, format, ##a)
  45. #else
  46. #define ccid3_pr_debug(format, a...)
  47. #endif
  48. static struct dccp_tx_hist *ccid3_tx_hist;
  49. static struct dccp_rx_hist *ccid3_rx_hist;
  50. static struct dccp_li_hist *ccid3_li_hist;
  51. /*
  52. * Transmitter Half-Connection Routines
  53. */
  54. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  55. static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
  56. {
  57. static char *ccid3_state_names[] = {
  58. [TFRC_SSTATE_NO_SENT] = "NO_SENT",
  59. [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
  60. [TFRC_SSTATE_FBACK] = "FBACK",
  61. [TFRC_SSTATE_TERM] = "TERM",
  62. };
  63. return ccid3_state_names[state];
  64. }
  65. #endif
  66. static void ccid3_hc_tx_set_state(struct sock *sk,
  67. enum ccid3_hc_tx_states state)
  68. {
  69. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  70. enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
  71. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  72. dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
  73. ccid3_tx_state_name(state));
  74. WARN_ON(state == oldstate);
  75. hctx->ccid3hctx_state = state;
  76. }
  77. /*
  78. * Compute the initial sending rate X_init according to RFC 3390:
  79. * w_init = min(4 * MSS, max(2 * MSS, 4380 bytes))
  80. * X_init = w_init / RTT
  81. * For consistency with other parts of the code, X_init is scaled by 2^6.
  82. */
  83. static inline u64 rfc3390_initial_rate(struct sock *sk)
  84. {
  85. const struct dccp_sock *dp = dccp_sk(sk);
  86. const __u32 w_init = min(4 * dp->dccps_mss_cache,
  87. max(2 * dp->dccps_mss_cache, 4380U));
  88. return scaled_div(w_init << 6, ccid3_hc_tx_sk(sk)->ccid3hctx_rtt);
  89. }
  90. /*
  91. * Recalculate t_ipi and delta (should be called whenever X changes)
  92. */
  93. static inline void ccid3_update_send_interval(struct ccid3_hc_tx_sock *hctx)
  94. {
  95. /* Calculate new t_ipi = s / X_inst (X_inst is in 64 * bytes/second) */
  96. hctx->ccid3hctx_t_ipi = scaled_div(hctx->ccid3hctx_s,
  97. hctx->ccid3hctx_x >> 6);
  98. /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
  99. hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
  100. TFRC_OPSYS_HALF_TIME_GRAN);
  101. ccid3_pr_debug("t_ipi=%u, delta=%u, s=%u, X=%u\n",
  102. hctx->ccid3hctx_t_ipi, hctx->ccid3hctx_delta,
  103. hctx->ccid3hctx_s, (unsigned)(hctx->ccid3hctx_x >> 6));
  104. }
  105. /*
  106. * Update X by
  107. * If (p > 0)
  108. * X_calc = calcX(s, R, p);
  109. * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
  110. * Else
  111. * If (now - tld >= R)
  112. * X = max(min(2 * X, 2 * X_recv), s / R);
  113. * tld = now;
  114. *
  115. * Note: X and X_recv are both stored in units of 64 * bytes/second, to support
  116. * fine-grained resolution of sending rates. This requires scaling by 2^6
  117. * throughout the code. Only X_calc is unscaled (in bytes/second).
  118. *
  119. */
  120. static void ccid3_hc_tx_update_x(struct sock *sk, struct timeval *now)
  121. {
  122. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  123. __u64 min_rate = 2 * hctx->ccid3hctx_x_recv;
  124. const __u64 old_x = hctx->ccid3hctx_x;
  125. /*
  126. * Handle IDLE periods: do not reduce below RFC3390 initial sending rate
  127. * when idling [RFC 4342, 5.1]. See also draft-ietf-dccp-rfc3448bis.
  128. * For consistency with X and X_recv, min_rate is also scaled by 2^6.
  129. */
  130. if (unlikely(hctx->ccid3hctx_idle)) {
  131. min_rate = rfc3390_initial_rate(sk);
  132. min_rate = max(min_rate, 2 * hctx->ccid3hctx_x_recv);
  133. }
  134. if (hctx->ccid3hctx_p > 0) {
  135. hctx->ccid3hctx_x = min(((__u64)hctx->ccid3hctx_x_calc) << 6,
  136. min_rate);
  137. hctx->ccid3hctx_x = max(hctx->ccid3hctx_x,
  138. (((__u64)hctx->ccid3hctx_s) << 6) /
  139. TFRC_T_MBI);
  140. } else if (timeval_delta(now, &hctx->ccid3hctx_t_ld) -
  141. (suseconds_t)hctx->ccid3hctx_rtt >= 0) {
  142. hctx->ccid3hctx_x =
  143. max(min(2 * hctx->ccid3hctx_x, min_rate),
  144. scaled_div(((__u64)hctx->ccid3hctx_s) << 6,
  145. hctx->ccid3hctx_rtt));
  146. hctx->ccid3hctx_t_ld = *now;
  147. }
  148. if (hctx->ccid3hctx_x != old_x) {
  149. ccid3_pr_debug("X_prev=%u, X_now=%u, X_calc=%u, "
  150. "X_recv=%u\n", (unsigned)(old_x >> 6),
  151. (unsigned)(hctx->ccid3hctx_x >> 6),
  152. hctx->ccid3hctx_x_calc,
  153. (unsigned)(hctx->ccid3hctx_x_recv >> 6));
  154. ccid3_update_send_interval(hctx);
  155. }
  156. }
  157. /*
  158. * Track the mean packet size `s' (cf. RFC 4342, 5.3 and RFC 3448, 4.1)
  159. * @len: DCCP packet payload size in bytes
  160. */
  161. static inline void ccid3_hc_tx_update_s(struct ccid3_hc_tx_sock *hctx, int len)
  162. {
  163. const u16 old_s = hctx->ccid3hctx_s;
  164. hctx->ccid3hctx_s = old_s == 0 ? len : (9 * old_s + len) / 10;
  165. if (hctx->ccid3hctx_s != old_s)
  166. ccid3_update_send_interval(hctx);
  167. }
  168. /*
  169. * Update Window Counter using the algorithm from [RFC 4342, 8.1].
  170. * The algorithm is not applicable if RTT < 4 microseconds.
  171. */
  172. static inline void ccid3_hc_tx_update_win_count(struct ccid3_hc_tx_sock *hctx,
  173. struct timeval *now)
  174. {
  175. suseconds_t delta;
  176. u32 quarter_rtts;
  177. if (unlikely(hctx->ccid3hctx_rtt < 4)) /* avoid divide-by-zero */
  178. return;
  179. delta = timeval_delta(now, &hctx->ccid3hctx_t_last_win_count);
  180. DCCP_BUG_ON(delta < 0);
  181. quarter_rtts = (u32)delta / (hctx->ccid3hctx_rtt / 4);
  182. if (quarter_rtts > 0) {
  183. hctx->ccid3hctx_t_last_win_count = *now;
  184. hctx->ccid3hctx_last_win_count += min_t(u32, quarter_rtts, 5);
  185. hctx->ccid3hctx_last_win_count &= 0xF; /* mod 16 */
  186. ccid3_pr_debug("now at %#X\n", hctx->ccid3hctx_last_win_count);
  187. }
  188. }
  189. static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
  190. {
  191. struct sock *sk = (struct sock *)data;
  192. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  193. struct timeval now;
  194. unsigned long t_nfb = USEC_PER_SEC / 5;
  195. bh_lock_sock(sk);
  196. if (sock_owned_by_user(sk)) {
  197. /* Try again later. */
  198. /* XXX: set some sensible MIB */
  199. goto restart_timer;
  200. }
  201. ccid3_pr_debug("%s(%p, state=%s) - entry \n", dccp_role(sk), sk,
  202. ccid3_tx_state_name(hctx->ccid3hctx_state));
  203. hctx->ccid3hctx_idle = 1;
  204. switch (hctx->ccid3hctx_state) {
  205. case TFRC_SSTATE_NO_FBACK:
  206. /* RFC 3448, 4.4: Halve send rate directly */
  207. hctx->ccid3hctx_x = max(hctx->ccid3hctx_x / 2,
  208. (((__u64)hctx->ccid3hctx_s) << 6) /
  209. TFRC_T_MBI);
  210. ccid3_pr_debug("%s(%p, state=%s), updated tx rate to %u "
  211. "bytes/s\n", dccp_role(sk), sk,
  212. ccid3_tx_state_name(hctx->ccid3hctx_state),
  213. (unsigned)(hctx->ccid3hctx_x >> 6));
  214. /* The value of R is still undefined and so we can not recompute
  215. * the timout value. Keep initial value as per [RFC 4342, 5]. */
  216. t_nfb = TFRC_INITIAL_TIMEOUT;
  217. ccid3_update_send_interval(hctx);
  218. break;
  219. case TFRC_SSTATE_FBACK:
  220. /*
  221. * Modify the cached value of X_recv [RFC 3448, 4.4]
  222. *
  223. * If (p == 0 || X_calc > 2 * X_recv)
  224. * X_recv = max(X_recv / 2, s / (2 * t_mbi));
  225. * Else
  226. * X_recv = X_calc / 4;
  227. *
  228. * Note that X_recv is scaled by 2^6 while X_calc is not
  229. */
  230. BUG_ON(hctx->ccid3hctx_p && !hctx->ccid3hctx_x_calc);
  231. if (hctx->ccid3hctx_p == 0 ||
  232. (hctx->ccid3hctx_x_calc > (hctx->ccid3hctx_x_recv >> 5))) {
  233. hctx->ccid3hctx_x_recv =
  234. max(hctx->ccid3hctx_x_recv / 2,
  235. (((__u64)hctx->ccid3hctx_s) << 6) /
  236. (2 * TFRC_T_MBI));
  237. if (hctx->ccid3hctx_p == 0)
  238. dccp_timestamp(sk, &now);
  239. } else {
  240. hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc;
  241. hctx->ccid3hctx_x_recv <<= 4;
  242. }
  243. /* Now recalculate X [RFC 3448, 4.3, step (4)] */
  244. ccid3_hc_tx_update_x(sk, &now);
  245. /*
  246. * Schedule no feedback timer to expire in
  247. * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi)
  248. * See comments in packet_recv() regarding the value of t_RTO.
  249. */
  250. t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);
  251. break;
  252. case TFRC_SSTATE_NO_SENT:
  253. DCCP_BUG("%s(%p) - Illegal state NO_SENT", dccp_role(sk), sk);
  254. /* fall through */
  255. case TFRC_SSTATE_TERM:
  256. goto out;
  257. }
  258. restart_timer:
  259. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  260. jiffies + usecs_to_jiffies(t_nfb));
  261. out:
  262. bh_unlock_sock(sk);
  263. sock_put(sk);
  264. }
  265. /*
  266. * returns
  267. * > 0: delay (in msecs) that should pass before actually sending
  268. * = 0: can send immediately
  269. * < 0: error condition; do not send packet
  270. */
  271. static int ccid3_hc_tx_send_packet(struct sock *sk, struct sk_buff *skb)
  272. {
  273. struct dccp_sock *dp = dccp_sk(sk);
  274. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  275. struct timeval now;
  276. suseconds_t delay;
  277. BUG_ON(hctx == NULL);
  278. /*
  279. * This function is called only for Data and DataAck packets. Sending
  280. * zero-sized Data(Ack)s is theoretically possible, but for congestion
  281. * control this case is pathological - ignore it.
  282. */
  283. if (unlikely(skb->len == 0))
  284. return -EBADMSG;
  285. dccp_timestamp(sk, &now);
  286. switch (hctx->ccid3hctx_state) {
  287. case TFRC_SSTATE_NO_SENT:
  288. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  289. (jiffies +
  290. usecs_to_jiffies(TFRC_INITIAL_TIMEOUT)));
  291. hctx->ccid3hctx_last_win_count = 0;
  292. hctx->ccid3hctx_t_last_win_count = now;
  293. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
  294. /* Set initial sending rate X/s to 1pps (X is scaled by 2^6) */
  295. hctx->ccid3hctx_x = hctx->ccid3hctx_s = skb->len;
  296. hctx->ccid3hctx_x <<= 6;
  297. /* First timeout, according to [RFC 3448, 4.2], is 1 second */
  298. hctx->ccid3hctx_t_ipi = USEC_PER_SEC;
  299. /* Initial delta: minimum of 0.5 sec and t_gran/2 */
  300. hctx->ccid3hctx_delta = TFRC_OPSYS_HALF_TIME_GRAN;
  301. /* Set t_0 for initial packet */
  302. hctx->ccid3hctx_t_nom = now;
  303. break;
  304. case TFRC_SSTATE_NO_FBACK:
  305. case TFRC_SSTATE_FBACK:
  306. delay = timeval_delta(&hctx->ccid3hctx_t_nom, &now);
  307. ccid3_pr_debug("delay=%ld\n", (long)delay);
  308. /*
  309. * Scheduling of packet transmissions [RFC 3448, 4.6]
  310. *
  311. * if (t_now > t_nom - delta)
  312. * // send the packet now
  313. * else
  314. * // send the packet in (t_nom - t_now) milliseconds.
  315. */
  316. if (delay - (suseconds_t)hctx->ccid3hctx_delta >= 0)
  317. return delay / 1000L;
  318. ccid3_hc_tx_update_win_count(hctx, &now);
  319. break;
  320. case TFRC_SSTATE_TERM:
  321. DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);
  322. return -EINVAL;
  323. }
  324. /* prepare to send now (add options etc.) */
  325. dp->dccps_hc_tx_insert_options = 1;
  326. DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
  327. hctx->ccid3hctx_idle = 0;
  328. /* set the nominal send time for the next following packet */
  329. timeval_add_usecs(&hctx->ccid3hctx_t_nom, hctx->ccid3hctx_t_ipi);
  330. return 0;
  331. }
  332. static void ccid3_hc_tx_packet_sent(struct sock *sk, int more,
  333. unsigned int len)
  334. {
  335. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  336. struct timeval now;
  337. struct dccp_tx_hist_entry *packet;
  338. BUG_ON(hctx == NULL);
  339. ccid3_hc_tx_update_s(hctx, len);
  340. packet = dccp_tx_hist_entry_new(ccid3_tx_hist, GFP_ATOMIC);
  341. if (unlikely(packet == NULL)) {
  342. DCCP_CRIT("packet history - out of memory!");
  343. return;
  344. }
  345. dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, packet);
  346. dccp_timestamp(sk, &now);
  347. packet->dccphtx_tstamp = now;
  348. packet->dccphtx_seqno = dccp_sk(sk)->dccps_gss;
  349. packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
  350. packet->dccphtx_sent = 1;
  351. }
  352. static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  353. {
  354. const struct dccp_sock *dp = dccp_sk(sk);
  355. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  356. struct ccid3_options_received *opt_recv;
  357. struct dccp_tx_hist_entry *packet;
  358. struct timeval now;
  359. unsigned long t_nfb;
  360. u32 pinv, r_sample;
  361. BUG_ON(hctx == NULL);
  362. /* we are only interested in ACKs */
  363. if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
  364. DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
  365. return;
  366. opt_recv = &hctx->ccid3hctx_options_received;
  367. switch (hctx->ccid3hctx_state) {
  368. case TFRC_SSTATE_NO_FBACK:
  369. case TFRC_SSTATE_FBACK:
  370. /* get packet from history to look up t_recvdata */
  371. packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
  372. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  373. if (unlikely(packet == NULL)) {
  374. DCCP_WARN("%s(%p), seqno %llu(%s) doesn't exist "
  375. "in history!\n", dccp_role(sk), sk,
  376. (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
  377. dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
  378. return;
  379. }
  380. /* Update receive rate in units of 64 * bytes/second */
  381. hctx->ccid3hctx_x_recv = opt_recv->ccid3or_receive_rate;
  382. hctx->ccid3hctx_x_recv <<= 6;
  383. /* Update loss event rate */
  384. pinv = opt_recv->ccid3or_loss_event_rate;
  385. if (pinv == ~0U || pinv == 0) /* see RFC 4342, 8.5 */
  386. hctx->ccid3hctx_p = 0;
  387. else /* can not exceed 100% */
  388. hctx->ccid3hctx_p = 1000000 / pinv;
  389. dccp_timestamp(sk, &now);
  390. /*
  391. * Calculate new round trip sample as per [RFC 3448, 4.3] by
  392. * R_sample = (now - t_recvdata) - t_elapsed
  393. */
  394. r_sample = dccp_sample_rtt(sk, &now, &packet->dccphtx_tstamp);
  395. /*
  396. * Update RTT estimate by
  397. * If (No feedback recv)
  398. * R = R_sample;
  399. * Else
  400. * R = q * R + (1 - q) * R_sample;
  401. *
  402. * q is a constant, RFC 3448 recomments 0.9
  403. */
  404. if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
  405. /*
  406. * Larger Initial Windows [RFC 4342, sec. 5]
  407. */
  408. hctx->ccid3hctx_rtt = r_sample;
  409. hctx->ccid3hctx_x = rfc3390_initial_rate(sk);
  410. hctx->ccid3hctx_t_ld = now;
  411. ccid3_update_send_interval(hctx);
  412. ccid3_pr_debug("%s(%p), s=%u, MSS=%u, "
  413. "R_sample=%uus, X=%u\n", dccp_role(sk),
  414. sk, hctx->ccid3hctx_s,
  415. dp->dccps_mss_cache, r_sample,
  416. (unsigned)(hctx->ccid3hctx_x >> 6));
  417. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
  418. } else {
  419. hctx->ccid3hctx_rtt = (9 * hctx->ccid3hctx_rtt +
  420. r_sample) / 10;
  421. /* Update sending rate (step 4 of [RFC 3448, 4.3]) */
  422. if (hctx->ccid3hctx_p > 0)
  423. hctx->ccid3hctx_x_calc =
  424. tfrc_calc_x(hctx->ccid3hctx_s,
  425. hctx->ccid3hctx_rtt,
  426. hctx->ccid3hctx_p);
  427. ccid3_hc_tx_update_x(sk, &now);
  428. ccid3_pr_debug("%s(%p), RTT=%uus (sample=%uus), s=%u, "
  429. "p=%u, X_calc=%u, X_recv=%u, X=%u\n",
  430. dccp_role(sk),
  431. sk, hctx->ccid3hctx_rtt, r_sample,
  432. hctx->ccid3hctx_s, hctx->ccid3hctx_p,
  433. hctx->ccid3hctx_x_calc,
  434. (unsigned)(hctx->ccid3hctx_x_recv >> 6),
  435. (unsigned)(hctx->ccid3hctx_x >> 6));
  436. }
  437. /* unschedule no feedback timer */
  438. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  439. /* remove all packets older than the one acked from history */
  440. dccp_tx_hist_purge_older(ccid3_tx_hist,
  441. &hctx->ccid3hctx_hist, packet);
  442. /*
  443. * As we have calculated new ipi, delta, t_nom it is possible
  444. * that we now can send a packet, so wake up dccp_wait_for_ccid
  445. */
  446. sk->sk_write_space(sk);
  447. /*
  448. * Update timeout interval for the nofeedback timer.
  449. * We use a configuration option to increase the lower bound.
  450. * This can help avoid triggering the nofeedback timer too
  451. * often ('spinning') on LANs with small RTTs.
  452. */
  453. hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
  454. CONFIG_IP_DCCP_CCID3_RTO *
  455. (USEC_PER_SEC/1000));
  456. /*
  457. * Schedule no feedback timer to expire in
  458. * max(t_RTO, 2 * s/X) = max(t_RTO, 2 * t_ipi)
  459. */
  460. t_nfb = max(hctx->ccid3hctx_t_rto, 2 * hctx->ccid3hctx_t_ipi);
  461. ccid3_pr_debug("%s(%p), Scheduled no feedback timer to "
  462. "expire in %lu jiffies (%luus)\n",
  463. dccp_role(sk),
  464. sk, usecs_to_jiffies(t_nfb), t_nfb);
  465. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  466. jiffies + usecs_to_jiffies(t_nfb));
  467. /* set idle flag */
  468. hctx->ccid3hctx_idle = 1;
  469. break;
  470. case TFRC_SSTATE_NO_SENT: /* fall through */
  471. case TFRC_SSTATE_TERM: /* ignore feedback when closing */
  472. break;
  473. }
  474. }
  475. static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
  476. unsigned char len, u16 idx,
  477. unsigned char *value)
  478. {
  479. int rc = 0;
  480. const struct dccp_sock *dp = dccp_sk(sk);
  481. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  482. struct ccid3_options_received *opt_recv;
  483. BUG_ON(hctx == NULL);
  484. opt_recv = &hctx->ccid3hctx_options_received;
  485. if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
  486. opt_recv->ccid3or_seqno = dp->dccps_gsr;
  487. opt_recv->ccid3or_loss_event_rate = ~0;
  488. opt_recv->ccid3or_loss_intervals_idx = 0;
  489. opt_recv->ccid3or_loss_intervals_len = 0;
  490. opt_recv->ccid3or_receive_rate = 0;
  491. }
  492. switch (option) {
  493. case TFRC_OPT_LOSS_EVENT_RATE:
  494. if (unlikely(len != 4)) {
  495. DCCP_WARN("%s(%p), invalid len %d "
  496. "for TFRC_OPT_LOSS_EVENT_RATE\n",
  497. dccp_role(sk), sk, len);
  498. rc = -EINVAL;
  499. } else {
  500. opt_recv->ccid3or_loss_event_rate =
  501. ntohl(*(__be32 *)value);
  502. ccid3_pr_debug("%s(%p), LOSS_EVENT_RATE=%u\n",
  503. dccp_role(sk), sk,
  504. opt_recv->ccid3or_loss_event_rate);
  505. }
  506. break;
  507. case TFRC_OPT_LOSS_INTERVALS:
  508. opt_recv->ccid3or_loss_intervals_idx = idx;
  509. opt_recv->ccid3or_loss_intervals_len = len;
  510. ccid3_pr_debug("%s(%p), LOSS_INTERVALS=(%u, %u)\n",
  511. dccp_role(sk), sk,
  512. opt_recv->ccid3or_loss_intervals_idx,
  513. opt_recv->ccid3or_loss_intervals_len);
  514. break;
  515. case TFRC_OPT_RECEIVE_RATE:
  516. if (unlikely(len != 4)) {
  517. DCCP_WARN("%s(%p), invalid len %d "
  518. "for TFRC_OPT_RECEIVE_RATE\n",
  519. dccp_role(sk), sk, len);
  520. rc = -EINVAL;
  521. } else {
  522. opt_recv->ccid3or_receive_rate =
  523. ntohl(*(__be32 *)value);
  524. ccid3_pr_debug("%s(%p), RECEIVE_RATE=%u\n",
  525. dccp_role(sk), sk,
  526. opt_recv->ccid3or_receive_rate);
  527. }
  528. break;
  529. }
  530. return rc;
  531. }
  532. static int ccid3_hc_tx_init(struct ccid *ccid, struct sock *sk)
  533. {
  534. struct ccid3_hc_tx_sock *hctx = ccid_priv(ccid);
  535. hctx->ccid3hctx_s = 0;
  536. hctx->ccid3hctx_rtt = 0;
  537. hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
  538. INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
  539. hctx->ccid3hctx_no_feedback_timer.function =
  540. ccid3_hc_tx_no_feedback_timer;
  541. hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
  542. init_timer(&hctx->ccid3hctx_no_feedback_timer);
  543. return 0;
  544. }
  545. static void ccid3_hc_tx_exit(struct sock *sk)
  546. {
  547. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  548. BUG_ON(hctx == NULL);
  549. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
  550. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  551. /* Empty packet history */
  552. dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
  553. }
  554. static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
  555. {
  556. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  557. /* Listen socks doesn't have a private CCID block */
  558. if (sk->sk_state == DCCP_LISTEN)
  559. return;
  560. BUG_ON(hctx == NULL);
  561. info->tcpi_rto = hctx->ccid3hctx_t_rto;
  562. info->tcpi_rtt = hctx->ccid3hctx_rtt;
  563. }
  564. static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
  565. u32 __user *optval, int __user *optlen)
  566. {
  567. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  568. const void *val;
  569. /* Listen socks doesn't have a private CCID block */
  570. if (sk->sk_state == DCCP_LISTEN)
  571. return -EINVAL;
  572. switch (optname) {
  573. case DCCP_SOCKOPT_CCID_TX_INFO:
  574. if (len < sizeof(hctx->ccid3hctx_tfrc))
  575. return -EINVAL;
  576. len = sizeof(hctx->ccid3hctx_tfrc);
  577. val = &hctx->ccid3hctx_tfrc;
  578. break;
  579. default:
  580. return -ENOPROTOOPT;
  581. }
  582. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  583. return -EFAULT;
  584. return 0;
  585. }
  586. /*
  587. * Receiver Half-Connection Routines
  588. */
  589. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  590. static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
  591. {
  592. static char *ccid3_rx_state_names[] = {
  593. [TFRC_RSTATE_NO_DATA] = "NO_DATA",
  594. [TFRC_RSTATE_DATA] = "DATA",
  595. [TFRC_RSTATE_TERM] = "TERM",
  596. };
  597. return ccid3_rx_state_names[state];
  598. }
  599. #endif
  600. static void ccid3_hc_rx_set_state(struct sock *sk,
  601. enum ccid3_hc_rx_states state)
  602. {
  603. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  604. enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
  605. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  606. dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
  607. ccid3_rx_state_name(state));
  608. WARN_ON(state == oldstate);
  609. hcrx->ccid3hcrx_state = state;
  610. }
  611. static inline void ccid3_hc_rx_update_s(struct ccid3_hc_rx_sock *hcrx, int len)
  612. {
  613. if (unlikely(len == 0)) /* don't update on empty packets (e.g. ACKs) */
  614. ccid3_pr_debug("Packet payload length is 0 - not updating\n");
  615. else
  616. hcrx->ccid3hcrx_s = hcrx->ccid3hcrx_s == 0 ? len :
  617. (9 * hcrx->ccid3hcrx_s + len) / 10;
  618. }
  619. static void ccid3_hc_rx_send_feedback(struct sock *sk)
  620. {
  621. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  622. struct dccp_sock *dp = dccp_sk(sk);
  623. struct dccp_rx_hist_entry *packet;
  624. struct timeval now;
  625. suseconds_t delta;
  626. ccid3_pr_debug("%s(%p) - entry \n", dccp_role(sk), sk);
  627. dccp_timestamp(sk, &now);
  628. switch (hcrx->ccid3hcrx_state) {
  629. case TFRC_RSTATE_NO_DATA:
  630. hcrx->ccid3hcrx_x_recv = 0;
  631. break;
  632. case TFRC_RSTATE_DATA:
  633. delta = timeval_delta(&now,
  634. &hcrx->ccid3hcrx_tstamp_last_feedback);
  635. DCCP_BUG_ON(delta < 0);
  636. hcrx->ccid3hcrx_x_recv =
  637. scaled_div32(hcrx->ccid3hcrx_bytes_recv, delta);
  638. break;
  639. case TFRC_RSTATE_TERM:
  640. DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);
  641. return;
  642. }
  643. packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
  644. if (unlikely(packet == NULL)) {
  645. DCCP_WARN("%s(%p), no data packet in history!\n",
  646. dccp_role(sk), sk);
  647. return;
  648. }
  649. hcrx->ccid3hcrx_tstamp_last_feedback = now;
  650. hcrx->ccid3hcrx_ccval_last_counter = packet->dccphrx_ccval;
  651. hcrx->ccid3hcrx_bytes_recv = 0;
  652. /* Elapsed time information [RFC 4340, 13.2] in units of 10 * usecs */
  653. delta = timeval_delta(&now, &packet->dccphrx_tstamp);
  654. DCCP_BUG_ON(delta < 0);
  655. hcrx->ccid3hcrx_elapsed_time = delta / 10;
  656. if (hcrx->ccid3hcrx_p == 0)
  657. hcrx->ccid3hcrx_pinv = ~0U; /* see RFC 4342, 8.5 */
  658. else if (hcrx->ccid3hcrx_p > 1000000) {
  659. DCCP_WARN("p (%u) > 100%%\n", hcrx->ccid3hcrx_p);
  660. hcrx->ccid3hcrx_pinv = 1; /* use 100% in this case */
  661. } else
  662. hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
  663. dp->dccps_hc_rx_insert_options = 1;
  664. dccp_send_ack(sk);
  665. }
  666. static int ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
  667. {
  668. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  669. __be32 x_recv, pinv;
  670. BUG_ON(hcrx == NULL);
  671. if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
  672. return 0;
  673. DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_ccval_last_counter;
  674. if (dccp_packet_without_ack(skb))
  675. return 0;
  676. x_recv = htonl(hcrx->ccid3hcrx_x_recv);
  677. pinv = htonl(hcrx->ccid3hcrx_pinv);
  678. if ((hcrx->ccid3hcrx_elapsed_time != 0 &&
  679. dccp_insert_option_elapsed_time(sk, skb,
  680. hcrx->ccid3hcrx_elapsed_time)) ||
  681. dccp_insert_option_timestamp(sk, skb) ||
  682. dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
  683. &pinv, sizeof(pinv)) ||
  684. dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
  685. &x_recv, sizeof(x_recv)))
  686. return -1;
  687. return 0;
  688. }
  689. /* calculate first loss interval
  690. *
  691. * returns estimated loss interval in usecs */
  692. static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
  693. {
  694. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  695. struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
  696. u32 x_recv, p;
  697. suseconds_t rtt, delta;
  698. struct timeval tstamp = { 0, };
  699. int interval = 0;
  700. int win_count = 0;
  701. int step = 0;
  702. u64 fval;
  703. list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
  704. dccphrx_node) {
  705. if (dccp_rx_hist_entry_data_packet(entry)) {
  706. tail = entry;
  707. switch (step) {
  708. case 0:
  709. tstamp = entry->dccphrx_tstamp;
  710. win_count = entry->dccphrx_ccval;
  711. step = 1;
  712. break;
  713. case 1:
  714. interval = win_count - entry->dccphrx_ccval;
  715. if (interval < 0)
  716. interval += TFRC_WIN_COUNT_LIMIT;
  717. if (interval > 4)
  718. goto found;
  719. break;
  720. }
  721. }
  722. }
  723. if (unlikely(step == 0)) {
  724. DCCP_WARN("%s(%p), packet history has no data packets!\n",
  725. dccp_role(sk), sk);
  726. return ~0;
  727. }
  728. if (unlikely(interval == 0)) {
  729. DCCP_WARN("%s(%p), Could not find a win_count interval > 0."
  730. "Defaulting to 1\n", dccp_role(sk), sk);
  731. interval = 1;
  732. }
  733. found:
  734. if (!tail) {
  735. DCCP_CRIT("tail is null\n");
  736. return ~0;
  737. }
  738. delta = timeval_delta(&tstamp, &tail->dccphrx_tstamp);
  739. DCCP_BUG_ON(delta < 0);
  740. rtt = delta * 4 / interval;
  741. ccid3_pr_debug("%s(%p), approximated RTT to %dus\n",
  742. dccp_role(sk), sk, (int)rtt);
  743. /*
  744. * Determine the length of the first loss interval via inverse lookup.
  745. * Assume that X_recv can be computed by the throughput equation
  746. * s
  747. * X_recv = --------
  748. * R * fval
  749. * Find some p such that f(p) = fval; return 1/p [RFC 3448, 6.3.1].
  750. */
  751. if (rtt == 0) { /* would result in divide-by-zero */
  752. DCCP_WARN("RTT==0\n");
  753. return ~0;
  754. }
  755. dccp_timestamp(sk, &tstamp);
  756. delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
  757. DCCP_BUG_ON(delta <= 0);
  758. x_recv = scaled_div32(hcrx->ccid3hcrx_bytes_recv, delta);
  759. if (x_recv == 0) { /* would also trigger divide-by-zero */
  760. DCCP_WARN("X_recv==0\n");
  761. if ((x_recv = hcrx->ccid3hcrx_x_recv) == 0) {
  762. DCCP_BUG("stored value of X_recv is zero");
  763. return ~0;
  764. }
  765. }
  766. fval = scaled_div(hcrx->ccid3hcrx_s, rtt);
  767. fval = scaled_div32(fval, x_recv);
  768. p = tfrc_calc_x_reverse_lookup(fval);
  769. ccid3_pr_debug("%s(%p), receive rate=%u bytes/s, implied "
  770. "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
  771. if (p == 0)
  772. return ~0;
  773. else
  774. return 1000000 / p;
  775. }
  776. static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
  777. {
  778. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  779. struct dccp_li_hist_entry *head;
  780. u64 seq_temp;
  781. if (list_empty(&hcrx->ccid3hcrx_li_hist)) {
  782. if (!dccp_li_hist_interval_new(ccid3_li_hist,
  783. &hcrx->ccid3hcrx_li_hist, seq_loss, win_loss))
  784. return;
  785. head = list_entry(hcrx->ccid3hcrx_li_hist.next,
  786. struct dccp_li_hist_entry, dccplih_node);
  787. head->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
  788. } else {
  789. struct dccp_li_hist_entry *entry;
  790. struct list_head *tail;
  791. head = list_entry(hcrx->ccid3hcrx_li_hist.next,
  792. struct dccp_li_hist_entry, dccplih_node);
  793. /* FIXME win count check removed as was wrong */
  794. /* should make this check with receive history */
  795. /* and compare there as per section 10.2 of RFC4342 */
  796. /* new loss event detected */
  797. /* calculate last interval length */
  798. seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss);
  799. entry = dccp_li_hist_entry_new(ccid3_li_hist, GFP_ATOMIC);
  800. if (entry == NULL) {
  801. DCCP_BUG("out of memory - can not allocate entry");
  802. return;
  803. }
  804. list_add(&entry->dccplih_node, &hcrx->ccid3hcrx_li_hist);
  805. tail = hcrx->ccid3hcrx_li_hist.prev;
  806. list_del(tail);
  807. kmem_cache_free(ccid3_li_hist->dccplih_slab, tail);
  808. /* Create the newest interval */
  809. entry->dccplih_seqno = seq_loss;
  810. entry->dccplih_interval = seq_temp;
  811. entry->dccplih_win_count = win_loss;
  812. }
  813. }
  814. static int ccid3_hc_rx_detect_loss(struct sock *sk,
  815. struct dccp_rx_hist_entry *packet)
  816. {
  817. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  818. struct dccp_rx_hist_entry *rx_hist =
  819. dccp_rx_hist_head(&hcrx->ccid3hcrx_hist);
  820. u64 seqno = packet->dccphrx_seqno;
  821. u64 tmp_seqno;
  822. int loss = 0;
  823. u8 ccval;
  824. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  825. if (!rx_hist ||
  826. follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  827. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  828. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  829. goto detect_out;
  830. }
  831. while (dccp_delta_seqno(hcrx->ccid3hcrx_seqno_nonloss, seqno)
  832. > TFRC_RECV_NUM_LATE_LOSS) {
  833. loss = 1;
  834. ccid3_hc_rx_update_li(sk, hcrx->ccid3hcrx_seqno_nonloss,
  835. hcrx->ccid3hcrx_ccval_nonloss);
  836. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  837. dccp_inc_seqno(&tmp_seqno);
  838. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  839. dccp_inc_seqno(&tmp_seqno);
  840. while (dccp_rx_hist_find_entry(&hcrx->ccid3hcrx_hist,
  841. tmp_seqno, &ccval)) {
  842. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  843. hcrx->ccid3hcrx_ccval_nonloss = ccval;
  844. dccp_inc_seqno(&tmp_seqno);
  845. }
  846. }
  847. /* FIXME - this code could be simplified with above while */
  848. /* but works at moment */
  849. if (follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  850. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  851. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  852. }
  853. detect_out:
  854. dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
  855. &hcrx->ccid3hcrx_li_hist, packet,
  856. hcrx->ccid3hcrx_seqno_nonloss);
  857. return loss;
  858. }
  859. static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  860. {
  861. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  862. const struct dccp_options_received *opt_recv;
  863. struct dccp_rx_hist_entry *packet;
  864. struct timeval now;
  865. u32 p_prev, r_sample, rtt_prev;
  866. int loss, payload_size;
  867. BUG_ON(hcrx == NULL);
  868. opt_recv = &dccp_sk(sk)->dccps_options_received;
  869. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  870. case DCCP_PKT_ACK:
  871. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  872. return;
  873. case DCCP_PKT_DATAACK:
  874. if (opt_recv->dccpor_timestamp_echo == 0)
  875. break;
  876. rtt_prev = hcrx->ccid3hcrx_rtt;
  877. dccp_timestamp(sk, &now);
  878. r_sample = dccp_sample_rtt(sk, &now, NULL);
  879. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  880. hcrx->ccid3hcrx_rtt = r_sample;
  881. else
  882. hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
  883. r_sample / 10;
  884. if (rtt_prev != hcrx->ccid3hcrx_rtt)
  885. ccid3_pr_debug("%s(%p), New RTT=%uus, elapsed time=%u\n",
  886. dccp_role(sk), sk, hcrx->ccid3hcrx_rtt,
  887. opt_recv->dccpor_elapsed_time);
  888. break;
  889. case DCCP_PKT_DATA:
  890. break;
  891. default: /* We're not interested in other packet types, move along */
  892. return;
  893. }
  894. packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
  895. skb, GFP_ATOMIC);
  896. if (unlikely(packet == NULL)) {
  897. DCCP_WARN("%s(%p), Not enough mem to add rx packet "
  898. "to history, consider it lost!\n", dccp_role(sk), sk);
  899. return;
  900. }
  901. loss = ccid3_hc_rx_detect_loss(sk, packet);
  902. if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
  903. return;
  904. payload_size = skb->len - dccp_hdr(skb)->dccph_doff * 4;
  905. ccid3_hc_rx_update_s(hcrx, payload_size);
  906. switch (hcrx->ccid3hcrx_state) {
  907. case TFRC_RSTATE_NO_DATA:
  908. ccid3_pr_debug("%s(%p, state=%s), skb=%p, sending initial "
  909. "feedback\n", dccp_role(sk), sk,
  910. dccp_state_name(sk->sk_state), skb);
  911. ccid3_hc_rx_send_feedback(sk);
  912. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
  913. return;
  914. case TFRC_RSTATE_DATA:
  915. hcrx->ccid3hcrx_bytes_recv += payload_size;
  916. if (loss)
  917. break;
  918. dccp_timestamp(sk, &now);
  919. if ((timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) -
  920. (suseconds_t)hcrx->ccid3hcrx_rtt) >= 0) {
  921. hcrx->ccid3hcrx_tstamp_last_ack = now;
  922. ccid3_hc_rx_send_feedback(sk);
  923. }
  924. return;
  925. case TFRC_RSTATE_TERM:
  926. DCCP_BUG("%s(%p) - Illegal state TERM", dccp_role(sk), sk);
  927. return;
  928. }
  929. /* Dealing with packet loss */
  930. ccid3_pr_debug("%s(%p, state=%s), data loss! Reacting...\n",
  931. dccp_role(sk), sk, dccp_state_name(sk->sk_state));
  932. p_prev = hcrx->ccid3hcrx_p;
  933. /* Calculate loss event rate */
  934. if (!list_empty(&hcrx->ccid3hcrx_li_hist)) {
  935. u32 i_mean = dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
  936. /* Scaling up by 1000000 as fixed decimal */
  937. if (i_mean != 0)
  938. hcrx->ccid3hcrx_p = 1000000 / i_mean;
  939. } else
  940. DCCP_BUG("empty loss history");
  941. if (hcrx->ccid3hcrx_p > p_prev) {
  942. ccid3_hc_rx_send_feedback(sk);
  943. return;
  944. }
  945. }
  946. static int ccid3_hc_rx_init(struct ccid *ccid, struct sock *sk)
  947. {
  948. struct ccid3_hc_rx_sock *hcrx = ccid_priv(ccid);
  949. ccid3_pr_debug("entry\n");
  950. hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
  951. INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
  952. INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
  953. dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
  954. hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
  955. hcrx->ccid3hcrx_s = 0;
  956. hcrx->ccid3hcrx_rtt = 0;
  957. return 0;
  958. }
  959. static void ccid3_hc_rx_exit(struct sock *sk)
  960. {
  961. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  962. BUG_ON(hcrx == NULL);
  963. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
  964. /* Empty packet history */
  965. dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
  966. /* Empty loss interval history */
  967. dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
  968. }
  969. static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
  970. {
  971. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  972. /* Listen socks doesn't have a private CCID block */
  973. if (sk->sk_state == DCCP_LISTEN)
  974. return;
  975. BUG_ON(hcrx == NULL);
  976. info->tcpi_ca_state = hcrx->ccid3hcrx_state;
  977. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  978. info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
  979. }
  980. static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
  981. u32 __user *optval, int __user *optlen)
  982. {
  983. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  984. const void *val;
  985. /* Listen socks doesn't have a private CCID block */
  986. if (sk->sk_state == DCCP_LISTEN)
  987. return -EINVAL;
  988. switch (optname) {
  989. case DCCP_SOCKOPT_CCID_RX_INFO:
  990. if (len < sizeof(hcrx->ccid3hcrx_tfrc))
  991. return -EINVAL;
  992. len = sizeof(hcrx->ccid3hcrx_tfrc);
  993. val = &hcrx->ccid3hcrx_tfrc;
  994. break;
  995. default:
  996. return -ENOPROTOOPT;
  997. }
  998. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  999. return -EFAULT;
  1000. return 0;
  1001. }
  1002. static struct ccid_operations ccid3 = {
  1003. .ccid_id = DCCPC_CCID3,
  1004. .ccid_name = "ccid3",
  1005. .ccid_owner = THIS_MODULE,
  1006. .ccid_hc_tx_obj_size = sizeof(struct ccid3_hc_tx_sock),
  1007. .ccid_hc_tx_init = ccid3_hc_tx_init,
  1008. .ccid_hc_tx_exit = ccid3_hc_tx_exit,
  1009. .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
  1010. .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
  1011. .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
  1012. .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
  1013. .ccid_hc_rx_obj_size = sizeof(struct ccid3_hc_rx_sock),
  1014. .ccid_hc_rx_init = ccid3_hc_rx_init,
  1015. .ccid_hc_rx_exit = ccid3_hc_rx_exit,
  1016. .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
  1017. .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
  1018. .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
  1019. .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
  1020. .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
  1021. .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
  1022. };
  1023. #ifdef CONFIG_IP_DCCP_CCID3_DEBUG
  1024. module_param(ccid3_debug, int, 0444);
  1025. MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
  1026. #endif
  1027. static __init int ccid3_module_init(void)
  1028. {
  1029. int rc = -ENOBUFS;
  1030. ccid3_rx_hist = dccp_rx_hist_new("ccid3");
  1031. if (ccid3_rx_hist == NULL)
  1032. goto out;
  1033. ccid3_tx_hist = dccp_tx_hist_new("ccid3");
  1034. if (ccid3_tx_hist == NULL)
  1035. goto out_free_rx;
  1036. ccid3_li_hist = dccp_li_hist_new("ccid3");
  1037. if (ccid3_li_hist == NULL)
  1038. goto out_free_tx;
  1039. rc = ccid_register(&ccid3);
  1040. if (rc != 0)
  1041. goto out_free_loss_interval_history;
  1042. out:
  1043. return rc;
  1044. out_free_loss_interval_history:
  1045. dccp_li_hist_delete(ccid3_li_hist);
  1046. ccid3_li_hist = NULL;
  1047. out_free_tx:
  1048. dccp_tx_hist_delete(ccid3_tx_hist);
  1049. ccid3_tx_hist = NULL;
  1050. out_free_rx:
  1051. dccp_rx_hist_delete(ccid3_rx_hist);
  1052. ccid3_rx_hist = NULL;
  1053. goto out;
  1054. }
  1055. module_init(ccid3_module_init);
  1056. static __exit void ccid3_module_exit(void)
  1057. {
  1058. ccid_unregister(&ccid3);
  1059. if (ccid3_tx_hist != NULL) {
  1060. dccp_tx_hist_delete(ccid3_tx_hist);
  1061. ccid3_tx_hist = NULL;
  1062. }
  1063. if (ccid3_rx_hist != NULL) {
  1064. dccp_rx_hist_delete(ccid3_rx_hist);
  1065. ccid3_rx_hist = NULL;
  1066. }
  1067. if (ccid3_li_hist != NULL) {
  1068. dccp_li_hist_delete(ccid3_li_hist);
  1069. ccid3_li_hist = NULL;
  1070. }
  1071. }
  1072. module_exit(ccid3_module_exit);
  1073. MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>, "
  1074. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  1075. MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
  1076. MODULE_LICENSE("GPL");
  1077. MODULE_ALIAS("net-dccp-ccid-3");