ccid3.c 37 KB

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