ccid3.c 35 KB

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