ccid3.c 36 KB

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