ccid3.c 36 KB

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