ccid3.c 35 KB

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