ccid3.c 36 KB

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