ccid3.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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. * As we have calculated new ipi, delta, t_nom it is possible that
  497. * we now can send a packet, so wake up dccp_wait_for_ccids.
  498. */
  499. sk->sk_write_space(sk);
  500. /*
  501. * Schedule no feedback timer to expire in
  502. * max(4 * R, 2 * s / X)
  503. */
  504. next_tmout = max(hctx->ccid3hctx_t_rto,
  505. 2 * usecs_div(hctx->ccid3hctx_s,
  506. hctx->ccid3hctx_x));
  507. ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
  508. "expire in %lu jiffies (%luus)\n",
  509. dccp_role(sk), sk,
  510. usecs_to_jiffies(next_tmout), next_tmout);
  511. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  512. jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
  513. /* set idle flag */
  514. hctx->ccid3hctx_idle = 1;
  515. break;
  516. default:
  517. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  518. __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
  519. dump_stack();
  520. break;
  521. }
  522. }
  523. static void ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
  524. {
  525. const struct dccp_sock *dp = dccp_sk(sk);
  526. struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
  527. if (hctx == NULL || !(sk->sk_state == DCCP_OPEN ||
  528. sk->sk_state == DCCP_PARTOPEN))
  529. return;
  530. DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
  531. }
  532. static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
  533. unsigned char len, u16 idx,
  534. unsigned char *value)
  535. {
  536. int rc = 0;
  537. struct dccp_sock *dp = dccp_sk(sk);
  538. struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
  539. struct ccid3_options_received *opt_recv;
  540. if (hctx == NULL)
  541. return 0;
  542. opt_recv = &hctx->ccid3hctx_options_received;
  543. if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
  544. opt_recv->ccid3or_seqno = dp->dccps_gsr;
  545. opt_recv->ccid3or_loss_event_rate = ~0;
  546. opt_recv->ccid3or_loss_intervals_idx = 0;
  547. opt_recv->ccid3or_loss_intervals_len = 0;
  548. opt_recv->ccid3or_receive_rate = 0;
  549. }
  550. switch (option) {
  551. case TFRC_OPT_LOSS_EVENT_RATE:
  552. if (len != 4) {
  553. ccid3_pr_debug("%s, sk=%p, invalid len for "
  554. "TFRC_OPT_LOSS_EVENT_RATE\n",
  555. dccp_role(sk), sk);
  556. rc = -EINVAL;
  557. } else {
  558. opt_recv->ccid3or_loss_event_rate = ntohl(*(u32 *)value);
  559. ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
  560. dccp_role(sk), sk,
  561. opt_recv->ccid3or_loss_event_rate);
  562. }
  563. break;
  564. case TFRC_OPT_LOSS_INTERVALS:
  565. opt_recv->ccid3or_loss_intervals_idx = idx;
  566. opt_recv->ccid3or_loss_intervals_len = len;
  567. ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
  568. dccp_role(sk), sk,
  569. opt_recv->ccid3or_loss_intervals_idx,
  570. opt_recv->ccid3or_loss_intervals_len);
  571. break;
  572. case TFRC_OPT_RECEIVE_RATE:
  573. if (len != 4) {
  574. ccid3_pr_debug("%s, sk=%p, invalid len for "
  575. "TFRC_OPT_RECEIVE_RATE\n",
  576. dccp_role(sk), sk);
  577. rc = -EINVAL;
  578. } else {
  579. opt_recv->ccid3or_receive_rate = ntohl(*(u32 *)value);
  580. ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
  581. dccp_role(sk), sk,
  582. opt_recv->ccid3or_receive_rate);
  583. }
  584. break;
  585. }
  586. return rc;
  587. }
  588. static int ccid3_hc_tx_init(struct sock *sk)
  589. {
  590. struct dccp_sock *dp = dccp_sk(sk);
  591. struct ccid3_hc_tx_sock *hctx;
  592. ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
  593. hctx = dp->dccps_hc_tx_ccid_private = kmalloc(sizeof(*hctx),
  594. gfp_any());
  595. if (hctx == NULL)
  596. return -ENOMEM;
  597. memset(hctx, 0, sizeof(*hctx));
  598. if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
  599. dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
  600. hctx->ccid3hctx_s = dp->dccps_packet_size;
  601. else
  602. hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
  603. /* Set transmission rate to 1 packet per second */
  604. hctx->ccid3hctx_x = hctx->ccid3hctx_s;
  605. hctx->ccid3hctx_t_rto = USEC_PER_SEC;
  606. hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
  607. INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
  608. init_timer(&hctx->ccid3hctx_no_feedback_timer);
  609. return 0;
  610. }
  611. static void ccid3_hc_tx_exit(struct sock *sk)
  612. {
  613. struct dccp_sock *dp = dccp_sk(sk);
  614. struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
  615. ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
  616. BUG_ON(hctx == NULL);
  617. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
  618. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  619. /* Empty packet history */
  620. dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
  621. kfree(dp->dccps_hc_tx_ccid_private);
  622. dp->dccps_hc_tx_ccid_private = NULL;
  623. }
  624. /*
  625. * RX Half Connection methods
  626. */
  627. /* TFRC receiver states */
  628. enum ccid3_hc_rx_states {
  629. TFRC_RSTATE_NO_DATA = 1,
  630. TFRC_RSTATE_DATA,
  631. TFRC_RSTATE_TERM = 127,
  632. };
  633. #ifdef CCID3_DEBUG
  634. static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
  635. {
  636. static char *ccid3_rx_state_names[] = {
  637. [TFRC_RSTATE_NO_DATA] = "NO_DATA",
  638. [TFRC_RSTATE_DATA] = "DATA",
  639. [TFRC_RSTATE_TERM] = "TERM",
  640. };
  641. return ccid3_rx_state_names[state];
  642. }
  643. #endif
  644. static inline void ccid3_hc_rx_set_state(struct sock *sk,
  645. enum ccid3_hc_rx_states state)
  646. {
  647. struct dccp_sock *dp = dccp_sk(sk);
  648. struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
  649. enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
  650. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  651. dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
  652. ccid3_rx_state_name(state));
  653. WARN_ON(state == oldstate);
  654. hcrx->ccid3hcrx_state = state;
  655. }
  656. static void ccid3_hc_rx_send_feedback(struct sock *sk)
  657. {
  658. struct dccp_sock *dp = dccp_sk(sk);
  659. struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
  660. struct dccp_rx_hist_entry *packet;
  661. struct timeval now;
  662. ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
  663. do_gettimeofday(&now);
  664. switch (hcrx->ccid3hcrx_state) {
  665. case TFRC_RSTATE_NO_DATA:
  666. hcrx->ccid3hcrx_x_recv = 0;
  667. break;
  668. case TFRC_RSTATE_DATA: {
  669. const u32 delta = timeval_delta(&now,
  670. &hcrx->ccid3hcrx_tstamp_last_feedback);
  671. hcrx->ccid3hcrx_x_recv = (hcrx->ccid3hcrx_bytes_recv *
  672. USEC_PER_SEC);
  673. if (likely(delta > 1))
  674. hcrx->ccid3hcrx_x_recv /= delta;
  675. }
  676. break;
  677. default:
  678. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  679. __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
  680. dump_stack();
  681. return;
  682. }
  683. packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
  684. if (packet == NULL) {
  685. printk(KERN_CRIT "%s: %s, sk=%p, no data packet in history!\n",
  686. __FUNCTION__, dccp_role(sk), sk);
  687. dump_stack();
  688. return;
  689. }
  690. hcrx->ccid3hcrx_tstamp_last_feedback = now;
  691. hcrx->ccid3hcrx_last_counter = packet->dccphrx_ccval;
  692. hcrx->ccid3hcrx_seqno_last_counter = packet->dccphrx_seqno;
  693. hcrx->ccid3hcrx_bytes_recv = 0;
  694. /* Convert to multiples of 10us */
  695. hcrx->ccid3hcrx_elapsed_time =
  696. timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
  697. if (hcrx->ccid3hcrx_p == 0)
  698. hcrx->ccid3hcrx_pinv = ~0;
  699. else
  700. hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
  701. dccp_send_ack(sk);
  702. }
  703. static void ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
  704. {
  705. const struct dccp_sock *dp = dccp_sk(sk);
  706. u32 x_recv, pinv;
  707. struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
  708. if (hcrx == NULL || !(sk->sk_state == DCCP_OPEN ||
  709. sk->sk_state == DCCP_PARTOPEN))
  710. return;
  711. DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_last_counter;
  712. if (dccp_packet_without_ack(skb))
  713. return;
  714. if (hcrx->ccid3hcrx_elapsed_time != 0)
  715. dccp_insert_option_elapsed_time(sk, skb,
  716. hcrx->ccid3hcrx_elapsed_time);
  717. dccp_insert_option_timestamp(sk, skb);
  718. x_recv = htonl(hcrx->ccid3hcrx_x_recv);
  719. pinv = htonl(hcrx->ccid3hcrx_pinv);
  720. dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
  721. &pinv, sizeof(pinv));
  722. dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
  723. &x_recv, sizeof(x_recv));
  724. }
  725. /* calculate first loss interval
  726. *
  727. * returns estimated loss interval in usecs */
  728. static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
  729. {
  730. struct dccp_sock *dp = dccp_sk(sk);
  731. struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
  732. struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
  733. u32 rtt, delta, x_recv, fval, p, tmp2;
  734. struct timeval tstamp = { 0, };
  735. int interval = 0;
  736. int win_count = 0;
  737. int step = 0;
  738. u64 tmp1;
  739. list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
  740. dccphrx_node) {
  741. if (dccp_rx_hist_entry_data_packet(entry)) {
  742. tail = entry;
  743. switch (step) {
  744. case 0:
  745. tstamp = entry->dccphrx_tstamp;
  746. win_count = entry->dccphrx_ccval;
  747. step = 1;
  748. break;
  749. case 1:
  750. interval = win_count - entry->dccphrx_ccval;
  751. if (interval < 0)
  752. interval += TFRC_WIN_COUNT_LIMIT;
  753. if (interval > 4)
  754. goto found;
  755. break;
  756. }
  757. }
  758. }
  759. if (step == 0) {
  760. printk(KERN_CRIT "%s: %s, sk=%p, packet history contains no "
  761. "data packets!\n",
  762. __FUNCTION__, dccp_role(sk), sk);
  763. return ~0;
  764. }
  765. if (interval == 0) {
  766. ccid3_pr_debug("%s, sk=%p, Could not find a win_count "
  767. "interval > 0. Defaulting to 1\n",
  768. dccp_role(sk), sk);
  769. interval = 1;
  770. }
  771. found:
  772. rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
  773. ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
  774. dccp_role(sk), sk, rtt);
  775. if (rtt == 0)
  776. rtt = 1;
  777. delta = timeval_now_delta(&hcrx->ccid3hcrx_tstamp_last_feedback);
  778. x_recv = hcrx->ccid3hcrx_bytes_recv * USEC_PER_SEC;
  779. if (likely(delta > 1))
  780. x_recv /= delta;
  781. tmp1 = (u64)x_recv * (u64)rtt;
  782. do_div(tmp1,10000000);
  783. tmp2 = (u32)tmp1;
  784. fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
  785. /* do not alter order above or you will get overflow on 32 bit */
  786. p = tfrc_calc_x_reverse_lookup(fval);
  787. ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
  788. "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
  789. if (p == 0)
  790. return ~0;
  791. else
  792. return 1000000 / p;
  793. }
  794. static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
  795. {
  796. struct dccp_sock *dp = dccp_sk(sk);
  797. struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
  798. if (seq_loss != DCCP_MAX_SEQNO + 1 &&
  799. list_empty(&hcrx->ccid3hcrx_li_hist)) {
  800. struct dccp_li_hist_entry *li_tail;
  801. li_tail = dccp_li_hist_interval_new(ccid3_li_hist,
  802. &hcrx->ccid3hcrx_li_hist,
  803. seq_loss, win_loss);
  804. if (li_tail == NULL)
  805. return;
  806. li_tail->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
  807. }
  808. /* FIXME: find end of interval */
  809. }
  810. static void ccid3_hc_rx_detect_loss(struct sock *sk)
  811. {
  812. struct dccp_sock *dp = dccp_sk(sk);
  813. struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
  814. u8 win_loss;
  815. const u64 seq_loss = dccp_rx_hist_detect_loss(&hcrx->ccid3hcrx_hist,
  816. &hcrx->ccid3hcrx_li_hist,
  817. &win_loss);
  818. ccid3_hc_rx_update_li(sk, seq_loss, win_loss);
  819. }
  820. static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  821. {
  822. struct dccp_sock *dp = dccp_sk(sk);
  823. struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
  824. const struct dccp_options_received *opt_recv;
  825. struct dccp_rx_hist_entry *packet;
  826. struct timeval now;
  827. u8 win_count;
  828. u32 p_prev;
  829. int ins;
  830. if (hcrx == NULL)
  831. return;
  832. BUG_ON(!(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
  833. hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
  834. opt_recv = &dp->dccps_options_received;
  835. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  836. case DCCP_PKT_ACK:
  837. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  838. return;
  839. case DCCP_PKT_DATAACK:
  840. if (opt_recv->dccpor_timestamp_echo == 0)
  841. break;
  842. p_prev = hcrx->ccid3hcrx_rtt;
  843. do_gettimeofday(&now);
  844. hcrx->ccid3hcrx_rtt = timeval_usecs(&now) -
  845. (opt_recv->dccpor_timestamp_echo -
  846. opt_recv->dccpor_elapsed_time) * 10;
  847. if (p_prev != hcrx->ccid3hcrx_rtt)
  848. ccid3_pr_debug("%s, New RTT=%luus, elapsed time=%u\n",
  849. dccp_role(sk), hcrx->ccid3hcrx_rtt,
  850. opt_recv->dccpor_elapsed_time);
  851. break;
  852. case DCCP_PKT_DATA:
  853. break;
  854. default:
  855. ccid3_pr_debug("%s, sk=%p, not DATA/DATAACK/ACK packet(%s)\n",
  856. dccp_role(sk), sk,
  857. dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
  858. return;
  859. }
  860. packet = dccp_rx_hist_entry_new(ccid3_rx_hist, opt_recv->dccpor_ndp,
  861. skb, SLAB_ATOMIC);
  862. if (packet == NULL) {
  863. ccid3_pr_debug("%s, sk=%p, Not enough mem to add rx packet "
  864. "to history (consider it lost)!",
  865. dccp_role(sk), sk);
  866. return;
  867. }
  868. win_count = packet->dccphrx_ccval;
  869. ins = dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
  870. &hcrx->ccid3hcrx_li_hist, packet);
  871. if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
  872. return;
  873. switch (hcrx->ccid3hcrx_state) {
  874. case TFRC_RSTATE_NO_DATA:
  875. ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
  876. "feedback\n",
  877. dccp_role(sk), sk,
  878. dccp_state_name(sk->sk_state), skb);
  879. ccid3_hc_rx_send_feedback(sk);
  880. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
  881. return;
  882. case TFRC_RSTATE_DATA:
  883. hcrx->ccid3hcrx_bytes_recv += skb->len -
  884. dccp_hdr(skb)->dccph_doff * 4;
  885. if (ins != 0)
  886. break;
  887. do_gettimeofday(&now);
  888. if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
  889. hcrx->ccid3hcrx_rtt) {
  890. hcrx->ccid3hcrx_tstamp_last_ack = now;
  891. ccid3_hc_rx_send_feedback(sk);
  892. }
  893. return;
  894. default:
  895. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  896. __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
  897. dump_stack();
  898. return;
  899. }
  900. /* Dealing with packet loss */
  901. ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
  902. dccp_role(sk), sk, dccp_state_name(sk->sk_state));
  903. ccid3_hc_rx_detect_loss(sk);
  904. p_prev = hcrx->ccid3hcrx_p;
  905. /* Calculate loss event rate */
  906. if (!list_empty(&hcrx->ccid3hcrx_li_hist))
  907. /* Scaling up by 1000000 as fixed decimal */
  908. hcrx->ccid3hcrx_p = 1000000 / dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
  909. if (hcrx->ccid3hcrx_p > p_prev) {
  910. ccid3_hc_rx_send_feedback(sk);
  911. return;
  912. }
  913. }
  914. static int ccid3_hc_rx_init(struct sock *sk)
  915. {
  916. struct dccp_sock *dp = dccp_sk(sk);
  917. struct ccid3_hc_rx_sock *hcrx;
  918. ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
  919. hcrx = dp->dccps_hc_rx_ccid_private = kmalloc(sizeof(*hcrx),
  920. gfp_any());
  921. if (hcrx == NULL)
  922. return -ENOMEM;
  923. memset(hcrx, 0, sizeof(*hcrx));
  924. if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
  925. dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
  926. hcrx->ccid3hcrx_s = dp->dccps_packet_size;
  927. else
  928. hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
  929. hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
  930. INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
  931. INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
  932. /*
  933. * XXX this seems to be paranoid, need to think more about this, for
  934. * now start with something different than zero. -acme
  935. */
  936. hcrx->ccid3hcrx_rtt = USEC_PER_SEC / 5;
  937. return 0;
  938. }
  939. static void ccid3_hc_rx_exit(struct sock *sk)
  940. {
  941. struct dccp_sock *dp = dccp_sk(sk);
  942. struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
  943. ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
  944. if (hcrx == NULL)
  945. return;
  946. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
  947. /* Empty packet history */
  948. dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
  949. /* Empty loss interval history */
  950. dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
  951. kfree(dp->dccps_hc_rx_ccid_private);
  952. dp->dccps_hc_rx_ccid_private = NULL;
  953. }
  954. static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
  955. {
  956. const struct dccp_sock *dp = dccp_sk(sk);
  957. const struct ccid3_hc_rx_sock *hcrx = dp->dccps_hc_rx_ccid_private;
  958. if (hcrx == NULL)
  959. return;
  960. info->tcpi_ca_state = hcrx->ccid3hcrx_state;
  961. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  962. info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
  963. }
  964. static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
  965. {
  966. const struct dccp_sock *dp = dccp_sk(sk);
  967. const struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private;
  968. if (hctx == NULL)
  969. return;
  970. info->tcpi_rto = hctx->ccid3hctx_t_rto;
  971. info->tcpi_rtt = hctx->ccid3hctx_rtt;
  972. }
  973. static struct ccid ccid3 = {
  974. .ccid_id = 3,
  975. .ccid_name = "ccid3",
  976. .ccid_owner = THIS_MODULE,
  977. .ccid_init = ccid3_init,
  978. .ccid_exit = ccid3_exit,
  979. .ccid_hc_tx_init = ccid3_hc_tx_init,
  980. .ccid_hc_tx_exit = ccid3_hc_tx_exit,
  981. .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
  982. .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
  983. .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
  984. .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
  985. .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
  986. .ccid_hc_rx_init = ccid3_hc_rx_init,
  987. .ccid_hc_rx_exit = ccid3_hc_rx_exit,
  988. .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
  989. .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
  990. .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
  991. .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
  992. };
  993. module_param(ccid3_debug, int, 0444);
  994. MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
  995. static __init int ccid3_module_init(void)
  996. {
  997. int rc = -ENOBUFS;
  998. ccid3_rx_hist = dccp_rx_hist_new("ccid3");
  999. if (ccid3_rx_hist == NULL)
  1000. goto out;
  1001. ccid3_tx_hist = dccp_tx_hist_new("ccid3");
  1002. if (ccid3_tx_hist == NULL)
  1003. goto out_free_rx;
  1004. ccid3_li_hist = dccp_li_hist_new("ccid3");
  1005. if (ccid3_li_hist == NULL)
  1006. goto out_free_tx;
  1007. rc = ccid_register(&ccid3);
  1008. if (rc != 0)
  1009. goto out_free_loss_interval_history;
  1010. out:
  1011. return rc;
  1012. out_free_loss_interval_history:
  1013. dccp_li_hist_delete(ccid3_li_hist);
  1014. ccid3_li_hist = NULL;
  1015. out_free_tx:
  1016. dccp_tx_hist_delete(ccid3_tx_hist);
  1017. ccid3_tx_hist = NULL;
  1018. out_free_rx:
  1019. dccp_rx_hist_delete(ccid3_rx_hist);
  1020. ccid3_rx_hist = NULL;
  1021. goto out;
  1022. }
  1023. module_init(ccid3_module_init);
  1024. static __exit void ccid3_module_exit(void)
  1025. {
  1026. #ifdef CONFIG_IP_DCCP_UNLOAD_HACK
  1027. /*
  1028. * Hack to use while developing, so that we get rid of the control
  1029. * sock, that is what keeps a refcount on dccp.ko -acme
  1030. */
  1031. extern void dccp_ctl_sock_exit(void);
  1032. dccp_ctl_sock_exit();
  1033. #endif
  1034. ccid_unregister(&ccid3);
  1035. if (ccid3_tx_hist != NULL) {
  1036. dccp_tx_hist_delete(ccid3_tx_hist);
  1037. ccid3_tx_hist = NULL;
  1038. }
  1039. if (ccid3_rx_hist != NULL) {
  1040. dccp_rx_hist_delete(ccid3_rx_hist);
  1041. ccid3_rx_hist = NULL;
  1042. }
  1043. if (ccid3_li_hist != NULL) {
  1044. dccp_li_hist_delete(ccid3_li_hist);
  1045. ccid3_li_hist = NULL;
  1046. }
  1047. }
  1048. module_exit(ccid3_module_exit);
  1049. MODULE_AUTHOR("Ian McDonald <iam4@cs.waikato.ac.nz>, "
  1050. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  1051. MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
  1052. MODULE_LICENSE("GPL");
  1053. MODULE_ALIAS("net-dccp-ccid-3");