ccid3.c 36 KB

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