ccid3.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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 <ian.mcdonald@jandi.co.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 "../ccid.h"
  37. #include "../dccp.h"
  38. #include "lib/packet_history.h"
  39. #include "lib/loss_interval.h"
  40. #include "lib/tfrc.h"
  41. #include "ccid3.h"
  42. /*
  43. * Reason for maths here is to avoid 32 bit overflow when a is big.
  44. * With this we get close to the limit.
  45. */
  46. static u32 usecs_div(const u32 a, const u32 b)
  47. {
  48. const u32 div = a < (UINT_MAX / (USEC_PER_SEC / 10)) ? 10 :
  49. a < (UINT_MAX / (USEC_PER_SEC / 50)) ? 50 :
  50. a < (UINT_MAX / (USEC_PER_SEC / 100)) ? 100 :
  51. a < (UINT_MAX / (USEC_PER_SEC / 500)) ? 500 :
  52. a < (UINT_MAX / (USEC_PER_SEC / 1000)) ? 1000 :
  53. a < (UINT_MAX / (USEC_PER_SEC / 5000)) ? 5000 :
  54. a < (UINT_MAX / (USEC_PER_SEC / 10000)) ? 10000 :
  55. a < (UINT_MAX / (USEC_PER_SEC / 50000)) ? 50000 :
  56. 100000;
  57. const u32 tmp = a * (USEC_PER_SEC / div);
  58. return (b >= 2 * div) ? tmp / (b / div) : tmp;
  59. }
  60. static int ccid3_debug;
  61. #ifdef CCID3_DEBUG
  62. #define ccid3_pr_debug(format, a...) \
  63. do { if (ccid3_debug) \
  64. printk(KERN_DEBUG "%s: " format, __FUNCTION__, ##a); \
  65. } while (0)
  66. #else
  67. #define ccid3_pr_debug(format, a...)
  68. #endif
  69. static struct dccp_tx_hist *ccid3_tx_hist;
  70. static struct dccp_rx_hist *ccid3_rx_hist;
  71. static struct dccp_li_hist *ccid3_li_hist;
  72. /* TFRC sender states */
  73. enum ccid3_hc_tx_states {
  74. TFRC_SSTATE_NO_SENT = 1,
  75. TFRC_SSTATE_NO_FBACK,
  76. TFRC_SSTATE_FBACK,
  77. TFRC_SSTATE_TERM,
  78. };
  79. #ifdef CCID3_DEBUG
  80. static const char *ccid3_tx_state_name(enum ccid3_hc_tx_states state)
  81. {
  82. static char *ccid3_state_names[] = {
  83. [TFRC_SSTATE_NO_SENT] = "NO_SENT",
  84. [TFRC_SSTATE_NO_FBACK] = "NO_FBACK",
  85. [TFRC_SSTATE_FBACK] = "FBACK",
  86. [TFRC_SSTATE_TERM] = "TERM",
  87. };
  88. return ccid3_state_names[state];
  89. }
  90. #endif
  91. static void ccid3_hc_tx_set_state(struct sock *sk,
  92. enum ccid3_hc_tx_states state)
  93. {
  94. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  95. enum ccid3_hc_tx_states oldstate = hctx->ccid3hctx_state;
  96. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  97. dccp_role(sk), sk, ccid3_tx_state_name(oldstate),
  98. ccid3_tx_state_name(state));
  99. WARN_ON(state == oldstate);
  100. hctx->ccid3hctx_state = state;
  101. }
  102. /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */
  103. static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx)
  104. {
  105. /*
  106. * If no feedback spec says t_ipi is 1 second (set elsewhere and then
  107. * doubles after every no feedback timer (separate function)
  108. */
  109. if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK)
  110. hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s,
  111. hctx->ccid3hctx_x);
  112. }
  113. /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */
  114. static inline void ccid3_calc_new_delta(struct ccid3_hc_tx_sock *hctx)
  115. {
  116. hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2,
  117. TFRC_OPSYS_HALF_TIME_GRAN);
  118. }
  119. /*
  120. * Update X by
  121. * If (p > 0)
  122. * x_calc = calcX(s, R, p);
  123. * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
  124. * Else
  125. * If (now - tld >= R)
  126. * X = max(min(2 * X, 2 * X_recv), s / R);
  127. * tld = now;
  128. */
  129. static void ccid3_hc_tx_update_x(struct sock *sk)
  130. {
  131. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  132. /* To avoid large error in calcX */
  133. if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) {
  134. hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
  135. hctx->ccid3hctx_rtt,
  136. hctx->ccid3hctx_p);
  137. hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_calc,
  138. 2 * hctx->ccid3hctx_x_recv),
  139. (hctx->ccid3hctx_s /
  140. TFRC_MAX_BACK_OFF_TIME));
  141. } else {
  142. struct timeval now;
  143. dccp_timestamp(sk, &now);
  144. if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >=
  145. hctx->ccid3hctx_rtt) {
  146. hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv,
  147. hctx->ccid3hctx_x) * 2,
  148. usecs_div(hctx->ccid3hctx_s,
  149. hctx->ccid3hctx_rtt));
  150. hctx->ccid3hctx_t_ld = now;
  151. }
  152. }
  153. }
  154. static void ccid3_hc_tx_no_feedback_timer(unsigned long data)
  155. {
  156. struct sock *sk = (struct sock *)data;
  157. unsigned long next_tmout = 0;
  158. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  159. bh_lock_sock(sk);
  160. if (sock_owned_by_user(sk)) {
  161. /* Try again later. */
  162. /* XXX: set some sensible MIB */
  163. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  164. jiffies + HZ / 5);
  165. goto out;
  166. }
  167. ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk,
  168. ccid3_tx_state_name(hctx->ccid3hctx_state));
  169. switch (hctx->ccid3hctx_state) {
  170. case TFRC_SSTATE_TERM:
  171. goto out;
  172. case TFRC_SSTATE_NO_FBACK:
  173. /* Halve send rate */
  174. hctx->ccid3hctx_x /= 2;
  175. if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s /
  176. TFRC_MAX_BACK_OFF_TIME))
  177. hctx->ccid3hctx_x = (hctx->ccid3hctx_s /
  178. TFRC_MAX_BACK_OFF_TIME);
  179. ccid3_pr_debug("%s, sk=%p, state=%s, updated tx rate to %d "
  180. "bytes/s\n",
  181. dccp_role(sk), sk,
  182. ccid3_tx_state_name(hctx->ccid3hctx_state),
  183. hctx->ccid3hctx_x);
  184. next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s,
  185. hctx->ccid3hctx_x),
  186. TFRC_INITIAL_TIMEOUT);
  187. /*
  188. * FIXME - not sure above calculation is correct. See section
  189. * 5 of CCID3 11 should adjust tx_t_ipi and double that to
  190. * achieve it really
  191. */
  192. break;
  193. case TFRC_SSTATE_FBACK:
  194. /*
  195. * Check if IDLE since last timeout and recv rate is less than
  196. * 4 packets per RTT
  197. */
  198. if (!hctx->ccid3hctx_idle ||
  199. (hctx->ccid3hctx_x_recv >=
  200. 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) {
  201. ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n",
  202. dccp_role(sk), sk,
  203. ccid3_tx_state_name(hctx->ccid3hctx_state));
  204. /* Halve sending rate */
  205. /* If (X_calc > 2 * X_recv)
  206. * X_recv = max(X_recv / 2, s / (2 * t_mbi));
  207. * Else
  208. * X_recv = X_calc / 4;
  209. */
  210. BUG_ON(hctx->ccid3hctx_p >= TFRC_SMALLEST_P &&
  211. hctx->ccid3hctx_x_calc == 0);
  212. /* check also if p is zero -> x_calc is infinity? */
  213. if (hctx->ccid3hctx_p < TFRC_SMALLEST_P ||
  214. hctx->ccid3hctx_x_calc > 2 * hctx->ccid3hctx_x_recv)
  215. hctx->ccid3hctx_x_recv = max_t(u32, hctx->ccid3hctx_x_recv / 2,
  216. hctx->ccid3hctx_s / (2 * TFRC_MAX_BACK_OFF_TIME));
  217. else
  218. hctx->ccid3hctx_x_recv = hctx->ccid3hctx_x_calc / 4;
  219. /* Update sending rate */
  220. ccid3_hc_tx_update_x(sk);
  221. }
  222. /*
  223. * Schedule no feedback timer to expire in
  224. * max(4 * R, 2 * s / X)
  225. */
  226. next_tmout = max_t(u32, hctx->ccid3hctx_t_rto,
  227. 2 * usecs_div(hctx->ccid3hctx_s,
  228. hctx->ccid3hctx_x));
  229. break;
  230. default:
  231. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  232. __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
  233. dump_stack();
  234. goto out;
  235. }
  236. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  237. jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
  238. hctx->ccid3hctx_idle = 1;
  239. out:
  240. bh_unlock_sock(sk);
  241. sock_put(sk);
  242. }
  243. static int ccid3_hc_tx_send_packet(struct sock *sk,
  244. struct sk_buff *skb, int len)
  245. {
  246. struct dccp_sock *dp = dccp_sk(sk);
  247. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  248. struct dccp_tx_hist_entry *new_packet;
  249. struct timeval now;
  250. long delay;
  251. int rc = -ENOTCONN;
  252. BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
  253. /* Check if pure ACK or Terminating*/
  254. /*
  255. * XXX: We only call this function for DATA and DATAACK, on, these
  256. * packets can have zero length, but why the comment about "pure ACK"?
  257. */
  258. if (unlikely(len == 0))
  259. goto out;
  260. /* See if last packet allocated was not sent */
  261. new_packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
  262. if (new_packet == NULL || new_packet->dccphtx_sent) {
  263. new_packet = dccp_tx_hist_entry_new(ccid3_tx_hist,
  264. SLAB_ATOMIC);
  265. rc = -ENOBUFS;
  266. if (unlikely(new_packet == NULL)) {
  267. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, not enough "
  268. "mem to add to history, send refused\n",
  269. __FUNCTION__, dccp_role(sk), sk);
  270. goto out;
  271. }
  272. dccp_tx_hist_add_entry(&hctx->ccid3hctx_hist, new_packet);
  273. }
  274. dccp_timestamp(sk, &now);
  275. switch (hctx->ccid3hctx_state) {
  276. case TFRC_SSTATE_NO_SENT:
  277. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  278. jiffies + usecs_to_jiffies(TFRC_INITIAL_TIMEOUT));
  279. hctx->ccid3hctx_last_win_count = 0;
  280. hctx->ccid3hctx_t_last_win_count = now;
  281. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_NO_FBACK);
  282. hctx->ccid3hctx_t_ipi = TFRC_INITIAL_IPI;
  283. /* Set nominal send time for initial packet */
  284. hctx->ccid3hctx_t_nom = now;
  285. timeval_add_usecs(&hctx->ccid3hctx_t_nom,
  286. hctx->ccid3hctx_t_ipi);
  287. ccid3_calc_new_delta(hctx);
  288. rc = 0;
  289. break;
  290. case TFRC_SSTATE_NO_FBACK:
  291. case TFRC_SSTATE_FBACK:
  292. delay = (timeval_delta(&now, &hctx->ccid3hctx_t_nom) -
  293. hctx->ccid3hctx_delta);
  294. delay /= -1000;
  295. /* divide by -1000 is to convert to ms and get sign right */
  296. rc = delay > 0 ? delay : 0;
  297. break;
  298. default:
  299. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  300. __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
  301. dump_stack();
  302. rc = -EINVAL;
  303. break;
  304. }
  305. /* Can we send? if so add options and add to packet history */
  306. if (rc == 0) {
  307. dp->dccps_hc_tx_insert_options = 1;
  308. new_packet->dccphtx_ccval =
  309. DCCP_SKB_CB(skb)->dccpd_ccval =
  310. hctx->ccid3hctx_last_win_count;
  311. timeval_add_usecs(&hctx->ccid3hctx_t_nom,
  312. hctx->ccid3hctx_t_ipi);
  313. }
  314. out:
  315. return rc;
  316. }
  317. static void ccid3_hc_tx_packet_sent(struct sock *sk, int more, int len)
  318. {
  319. const struct dccp_sock *dp = dccp_sk(sk);
  320. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  321. struct timeval now;
  322. BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
  323. dccp_timestamp(sk, &now);
  324. /* check if we have sent a data packet */
  325. if (len > 0) {
  326. unsigned long quarter_rtt;
  327. struct dccp_tx_hist_entry *packet;
  328. packet = dccp_tx_hist_head(&hctx->ccid3hctx_hist);
  329. if (unlikely(packet == NULL)) {
  330. LIMIT_NETDEBUG(KERN_WARNING "%s: packet doesn't "
  331. "exists in history!\n", __FUNCTION__);
  332. return;
  333. }
  334. if (unlikely(packet->dccphtx_sent)) {
  335. LIMIT_NETDEBUG(KERN_WARNING "%s: no unsent packet in "
  336. "history!\n", __FUNCTION__);
  337. return;
  338. }
  339. packet->dccphtx_tstamp = now;
  340. packet->dccphtx_seqno = dp->dccps_gss;
  341. /*
  342. * Check if win_count have changed
  343. * Algorithm in "8.1. Window Counter Valuer" in
  344. * draft-ietf-dccp-ccid3-11.txt
  345. */
  346. quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
  347. if (likely(hctx->ccid3hctx_rtt > 8))
  348. quarter_rtt /= hctx->ccid3hctx_rtt / 4;
  349. if (quarter_rtt > 0) {
  350. hctx->ccid3hctx_t_last_win_count = now;
  351. hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count +
  352. min_t(unsigned long, quarter_rtt, 5)) % 16;
  353. ccid3_pr_debug("%s, sk=%p, window changed from "
  354. "%u to %u!\n",
  355. dccp_role(sk), sk,
  356. packet->dccphtx_ccval,
  357. hctx->ccid3hctx_last_win_count);
  358. }
  359. hctx->ccid3hctx_idle = 0;
  360. packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
  361. packet->dccphtx_sent = 1;
  362. } else
  363. ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
  364. dccp_role(sk), sk, dp->dccps_gss);
  365. switch (hctx->ccid3hctx_state) {
  366. case TFRC_SSTATE_NO_SENT:
  367. /* if first wasn't pure ack */
  368. if (len != 0)
  369. printk(KERN_CRIT "%s: %s, First packet sent is noted "
  370. "as a data packet\n",
  371. __FUNCTION__, dccp_role(sk));
  372. return;
  373. case TFRC_SSTATE_NO_FBACK:
  374. case TFRC_SSTATE_FBACK:
  375. if (len > 0) {
  376. timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
  377. hctx->ccid3hctx_t_ipi);
  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 int 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. DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
  521. return 0;
  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(*(__be32 *)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(*(__be32 *)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 ccid *ccid, struct sock *sk)
  579. {
  580. struct dccp_sock *dp = dccp_sk(sk);
  581. struct ccid3_hc_tx_sock *hctx = ccid_priv(ccid);
  582. if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
  583. dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
  584. hctx->ccid3hctx_s = dp->dccps_packet_size;
  585. else
  586. hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
  587. /* Set transmission rate to 1 packet per second */
  588. hctx->ccid3hctx_x = hctx->ccid3hctx_s;
  589. hctx->ccid3hctx_t_rto = USEC_PER_SEC;
  590. hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
  591. INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
  592. hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
  593. hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
  594. init_timer(&hctx->ccid3hctx_no_feedback_timer);
  595. return 0;
  596. }
  597. static void ccid3_hc_tx_exit(struct sock *sk)
  598. {
  599. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  600. BUG_ON(hctx == NULL);
  601. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
  602. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  603. /* Empty packet history */
  604. dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
  605. }
  606. /*
  607. * RX Half Connection methods
  608. */
  609. /* TFRC receiver states */
  610. enum ccid3_hc_rx_states {
  611. TFRC_RSTATE_NO_DATA = 1,
  612. TFRC_RSTATE_DATA,
  613. TFRC_RSTATE_TERM = 127,
  614. };
  615. #ifdef CCID3_DEBUG
  616. static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
  617. {
  618. static char *ccid3_rx_state_names[] = {
  619. [TFRC_RSTATE_NO_DATA] = "NO_DATA",
  620. [TFRC_RSTATE_DATA] = "DATA",
  621. [TFRC_RSTATE_TERM] = "TERM",
  622. };
  623. return ccid3_rx_state_names[state];
  624. }
  625. #endif
  626. static void ccid3_hc_rx_set_state(struct sock *sk,
  627. enum ccid3_hc_rx_states state)
  628. {
  629. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  630. enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
  631. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  632. dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
  633. ccid3_rx_state_name(state));
  634. WARN_ON(state == oldstate);
  635. hcrx->ccid3hcrx_state = state;
  636. }
  637. static void ccid3_hc_rx_send_feedback(struct sock *sk)
  638. {
  639. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  640. struct dccp_sock *dp = dccp_sk(sk);
  641. struct dccp_rx_hist_entry *packet;
  642. struct timeval now;
  643. ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
  644. dccp_timestamp(sk, &now);
  645. switch (hcrx->ccid3hcrx_state) {
  646. case TFRC_RSTATE_NO_DATA:
  647. hcrx->ccid3hcrx_x_recv = 0;
  648. break;
  649. case TFRC_RSTATE_DATA: {
  650. const u32 delta = timeval_delta(&now,
  651. &hcrx->ccid3hcrx_tstamp_last_feedback);
  652. hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
  653. delta);
  654. }
  655. break;
  656. default:
  657. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  658. __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
  659. dump_stack();
  660. return;
  661. }
  662. packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
  663. if (unlikely(packet == NULL)) {
  664. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, no data packet "
  665. "in history!\n",
  666. __FUNCTION__, dccp_role(sk), sk);
  667. return;
  668. }
  669. hcrx->ccid3hcrx_tstamp_last_feedback = now;
  670. hcrx->ccid3hcrx_ccval_last_counter = packet->dccphrx_ccval;
  671. hcrx->ccid3hcrx_bytes_recv = 0;
  672. /* Convert to multiples of 10us */
  673. hcrx->ccid3hcrx_elapsed_time =
  674. timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
  675. if (hcrx->ccid3hcrx_p == 0)
  676. hcrx->ccid3hcrx_pinv = ~0;
  677. else
  678. hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
  679. dp->dccps_hc_rx_insert_options = 1;
  680. dccp_send_ack(sk);
  681. }
  682. static int ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
  683. {
  684. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  685. __be32 x_recv, pinv;
  686. BUG_ON(hcrx == NULL);
  687. if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
  688. return 0;
  689. DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_ccval_last_counter;
  690. if (dccp_packet_without_ack(skb))
  691. return 0;
  692. x_recv = htonl(hcrx->ccid3hcrx_x_recv);
  693. pinv = htonl(hcrx->ccid3hcrx_pinv);
  694. if ((hcrx->ccid3hcrx_elapsed_time != 0 &&
  695. dccp_insert_option_elapsed_time(sk, skb,
  696. hcrx->ccid3hcrx_elapsed_time)) ||
  697. dccp_insert_option_timestamp(sk, skb) ||
  698. dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
  699. &pinv, sizeof(pinv)) ||
  700. dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
  701. &x_recv, sizeof(x_recv)))
  702. return -1;
  703. return 0;
  704. }
  705. /* calculate first loss interval
  706. *
  707. * returns estimated loss interval in usecs */
  708. static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
  709. {
  710. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  711. struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
  712. u32 rtt, delta, x_recv, fval, p, tmp2;
  713. struct timeval tstamp = { 0, };
  714. int interval = 0;
  715. int win_count = 0;
  716. int step = 0;
  717. u64 tmp1;
  718. list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
  719. dccphrx_node) {
  720. if (dccp_rx_hist_entry_data_packet(entry)) {
  721. tail = entry;
  722. switch (step) {
  723. case 0:
  724. tstamp = entry->dccphrx_tstamp;
  725. win_count = entry->dccphrx_ccval;
  726. step = 1;
  727. break;
  728. case 1:
  729. interval = win_count - entry->dccphrx_ccval;
  730. if (interval < 0)
  731. interval += TFRC_WIN_COUNT_LIMIT;
  732. if (interval > 4)
  733. goto found;
  734. break;
  735. }
  736. }
  737. }
  738. if (unlikely(step == 0)) {
  739. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, packet history "
  740. "contains no data packets!\n",
  741. __FUNCTION__, dccp_role(sk), sk);
  742. return ~0;
  743. }
  744. if (unlikely(interval == 0)) {
  745. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Could not find a "
  746. "win_count interval > 0. Defaulting to 1\n",
  747. __FUNCTION__, dccp_role(sk), sk);
  748. interval = 1;
  749. }
  750. found:
  751. if (!tail) {
  752. LIMIT_NETDEBUG(KERN_WARNING "%s: tail is null\n",
  753. __FUNCTION__);
  754. return ~0;
  755. }
  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. if (x_recv == 0)
  765. x_recv = hcrx->ccid3hcrx_x_recv;
  766. tmp1 = (u64)x_recv * (u64)rtt;
  767. do_div(tmp1,10000000);
  768. tmp2 = (u32)tmp1;
  769. if (!tmp2) {
  770. LIMIT_NETDEBUG(KERN_WARNING "tmp2 = 0 "
  771. "%s: x_recv = %u, rtt =%u\n",
  772. __FUNCTION__, x_recv, rtt);
  773. return ~0;
  774. }
  775. fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
  776. /* do not alter order above or you will get overflow on 32 bit */
  777. p = tfrc_calc_x_reverse_lookup(fval);
  778. ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
  779. "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
  780. if (p == 0)
  781. return ~0;
  782. else
  783. return 1000000 / p;
  784. }
  785. static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
  786. {
  787. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  788. struct dccp_li_hist_entry *head;
  789. u64 seq_temp;
  790. if (list_empty(&hcrx->ccid3hcrx_li_hist)) {
  791. if (!dccp_li_hist_interval_new(ccid3_li_hist,
  792. &hcrx->ccid3hcrx_li_hist, seq_loss, win_loss))
  793. return;
  794. head = list_entry(hcrx->ccid3hcrx_li_hist.next,
  795. struct dccp_li_hist_entry, dccplih_node);
  796. head->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
  797. } else {
  798. struct dccp_li_hist_entry *entry;
  799. struct list_head *tail;
  800. head = list_entry(hcrx->ccid3hcrx_li_hist.next,
  801. struct dccp_li_hist_entry, dccplih_node);
  802. /* FIXME win count check removed as was wrong */
  803. /* should make this check with receive history */
  804. /* and compare there as per section 10.2 of RFC4342 */
  805. /* new loss event detected */
  806. /* calculate last interval length */
  807. seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss);
  808. entry = dccp_li_hist_entry_new(ccid3_li_hist, SLAB_ATOMIC);
  809. if (entry == NULL) {
  810. printk(KERN_CRIT "%s: out of memory\n",__FUNCTION__);
  811. dump_stack();
  812. return;
  813. }
  814. list_add(&entry->dccplih_node, &hcrx->ccid3hcrx_li_hist);
  815. tail = hcrx->ccid3hcrx_li_hist.prev;
  816. list_del(tail);
  817. kmem_cache_free(ccid3_li_hist->dccplih_slab, tail);
  818. /* Create the newest interval */
  819. entry->dccplih_seqno = seq_loss;
  820. entry->dccplih_interval = seq_temp;
  821. entry->dccplih_win_count = win_loss;
  822. }
  823. }
  824. static int ccid3_hc_rx_detect_loss(struct sock *sk,
  825. struct dccp_rx_hist_entry *packet)
  826. {
  827. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  828. struct dccp_rx_hist_entry *rx_hist = dccp_rx_hist_head(&hcrx->ccid3hcrx_hist);
  829. u64 seqno = packet->dccphrx_seqno;
  830. u64 tmp_seqno;
  831. int loss = 0;
  832. u8 ccval;
  833. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  834. if (!rx_hist ||
  835. follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  836. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  837. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  838. goto detect_out;
  839. }
  840. while (dccp_delta_seqno(hcrx->ccid3hcrx_seqno_nonloss, seqno)
  841. > TFRC_RECV_NUM_LATE_LOSS) {
  842. loss = 1;
  843. ccid3_hc_rx_update_li(sk, hcrx->ccid3hcrx_seqno_nonloss,
  844. hcrx->ccid3hcrx_ccval_nonloss);
  845. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  846. dccp_inc_seqno(&tmp_seqno);
  847. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  848. dccp_inc_seqno(&tmp_seqno);
  849. while (dccp_rx_hist_find_entry(&hcrx->ccid3hcrx_hist,
  850. tmp_seqno, &ccval)) {
  851. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  852. hcrx->ccid3hcrx_ccval_nonloss = ccval;
  853. dccp_inc_seqno(&tmp_seqno);
  854. }
  855. }
  856. /* FIXME - this code could be simplified with above while */
  857. /* but works at moment */
  858. if (follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  859. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  860. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  861. }
  862. detect_out:
  863. dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
  864. &hcrx->ccid3hcrx_li_hist, packet,
  865. hcrx->ccid3hcrx_seqno_nonloss);
  866. return loss;
  867. }
  868. static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  869. {
  870. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  871. const struct dccp_options_received *opt_recv;
  872. struct dccp_rx_hist_entry *packet;
  873. struct timeval now;
  874. u8 win_count;
  875. u32 p_prev, rtt_prev, r_sample, t_elapsed;
  876. int loss;
  877. BUG_ON(hcrx == NULL ||
  878. !(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
  879. hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
  880. opt_recv = &dccp_sk(sk)->dccps_options_received;
  881. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  882. case DCCP_PKT_ACK:
  883. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  884. return;
  885. case DCCP_PKT_DATAACK:
  886. if (opt_recv->dccpor_timestamp_echo == 0)
  887. break;
  888. rtt_prev = hcrx->ccid3hcrx_rtt;
  889. dccp_timestamp(sk, &now);
  890. timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
  891. r_sample = timeval_usecs(&now);
  892. t_elapsed = opt_recv->dccpor_elapsed_time * 10;
  893. if (unlikely(r_sample <= t_elapsed))
  894. LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
  895. "t_elapsed=%uus\n",
  896. __FUNCTION__, r_sample, t_elapsed);
  897. else
  898. r_sample -= t_elapsed;
  899. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  900. hcrx->ccid3hcrx_rtt = r_sample;
  901. else
  902. hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
  903. r_sample / 10;
  904. if (rtt_prev != hcrx->ccid3hcrx_rtt)
  905. ccid3_pr_debug("%s, New RTT=%uus, elapsed time=%u\n",
  906. dccp_role(sk), hcrx->ccid3hcrx_rtt,
  907. opt_recv->dccpor_elapsed_time);
  908. break;
  909. case DCCP_PKT_DATA:
  910. break;
  911. default: /* We're not interested in other packet types, move along */
  912. return;
  913. }
  914. packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
  915. skb, SLAB_ATOMIC);
  916. if (unlikely(packet == NULL)) {
  917. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Not enough mem to "
  918. "add rx packet to history, consider it lost!\n",
  919. __FUNCTION__, dccp_role(sk), sk);
  920. return;
  921. }
  922. win_count = packet->dccphrx_ccval;
  923. loss = ccid3_hc_rx_detect_loss(sk, packet);
  924. if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
  925. return;
  926. switch (hcrx->ccid3hcrx_state) {
  927. case TFRC_RSTATE_NO_DATA:
  928. ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
  929. "feedback\n",
  930. dccp_role(sk), sk,
  931. dccp_state_name(sk->sk_state), skb);
  932. ccid3_hc_rx_send_feedback(sk);
  933. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
  934. return;
  935. case TFRC_RSTATE_DATA:
  936. hcrx->ccid3hcrx_bytes_recv += skb->len -
  937. dccp_hdr(skb)->dccph_doff * 4;
  938. if (loss)
  939. break;
  940. dccp_timestamp(sk, &now);
  941. if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
  942. hcrx->ccid3hcrx_rtt) {
  943. hcrx->ccid3hcrx_tstamp_last_ack = now;
  944. ccid3_hc_rx_send_feedback(sk);
  945. }
  946. return;
  947. default:
  948. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  949. __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
  950. dump_stack();
  951. return;
  952. }
  953. /* Dealing with packet loss */
  954. ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
  955. dccp_role(sk), sk, dccp_state_name(sk->sk_state));
  956. p_prev = hcrx->ccid3hcrx_p;
  957. /* Calculate loss event rate */
  958. if (!list_empty(&hcrx->ccid3hcrx_li_hist)) {
  959. u32 i_mean = dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
  960. /* Scaling up by 1000000 as fixed decimal */
  961. if (i_mean != 0)
  962. hcrx->ccid3hcrx_p = 1000000 / i_mean;
  963. } else {
  964. printk(KERN_CRIT "%s: empty loss hist\n",__FUNCTION__);
  965. dump_stack();
  966. }
  967. if (hcrx->ccid3hcrx_p > p_prev) {
  968. ccid3_hc_rx_send_feedback(sk);
  969. return;
  970. }
  971. }
  972. static int ccid3_hc_rx_init(struct ccid *ccid, struct sock *sk)
  973. {
  974. struct dccp_sock *dp = dccp_sk(sk);
  975. struct ccid3_hc_rx_sock *hcrx = ccid_priv(ccid);
  976. ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
  977. if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
  978. dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
  979. hcrx->ccid3hcrx_s = dp->dccps_packet_size;
  980. else
  981. hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
  982. hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
  983. INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
  984. INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
  985. dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
  986. hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
  987. hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
  988. return 0;
  989. }
  990. static void ccid3_hc_rx_exit(struct sock *sk)
  991. {
  992. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  993. BUG_ON(hcrx == NULL);
  994. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
  995. /* Empty packet history */
  996. dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
  997. /* Empty loss interval history */
  998. dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
  999. }
  1000. static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
  1001. {
  1002. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  1003. /* Listen socks doesn't have a private CCID block */
  1004. if (sk->sk_state == DCCP_LISTEN)
  1005. return;
  1006. BUG_ON(hcrx == NULL);
  1007. info->tcpi_ca_state = hcrx->ccid3hcrx_state;
  1008. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  1009. info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
  1010. }
  1011. static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
  1012. {
  1013. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  1014. /* Listen socks doesn't have a private CCID block */
  1015. if (sk->sk_state == DCCP_LISTEN)
  1016. return;
  1017. BUG_ON(hctx == NULL);
  1018. info->tcpi_rto = hctx->ccid3hctx_t_rto;
  1019. info->tcpi_rtt = hctx->ccid3hctx_rtt;
  1020. }
  1021. static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
  1022. u32 __user *optval, int __user *optlen)
  1023. {
  1024. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  1025. const void *val;
  1026. /* Listen socks doesn't have a private CCID block */
  1027. if (sk->sk_state == DCCP_LISTEN)
  1028. return -EINVAL;
  1029. switch (optname) {
  1030. case DCCP_SOCKOPT_CCID_RX_INFO:
  1031. if (len < sizeof(hcrx->ccid3hcrx_tfrc))
  1032. return -EINVAL;
  1033. len = sizeof(hcrx->ccid3hcrx_tfrc);
  1034. val = &hcrx->ccid3hcrx_tfrc;
  1035. break;
  1036. default:
  1037. return -ENOPROTOOPT;
  1038. }
  1039. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  1040. return -EFAULT;
  1041. return 0;
  1042. }
  1043. static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
  1044. u32 __user *optval, int __user *optlen)
  1045. {
  1046. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  1047. const void *val;
  1048. /* Listen socks doesn't have a private CCID block */
  1049. if (sk->sk_state == DCCP_LISTEN)
  1050. return -EINVAL;
  1051. switch (optname) {
  1052. case DCCP_SOCKOPT_CCID_TX_INFO:
  1053. if (len < sizeof(hctx->ccid3hctx_tfrc))
  1054. return -EINVAL;
  1055. len = sizeof(hctx->ccid3hctx_tfrc);
  1056. val = &hctx->ccid3hctx_tfrc;
  1057. break;
  1058. default:
  1059. return -ENOPROTOOPT;
  1060. }
  1061. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  1062. return -EFAULT;
  1063. return 0;
  1064. }
  1065. static struct ccid_operations ccid3 = {
  1066. .ccid_id = DCCPC_CCID3,
  1067. .ccid_name = "ccid3",
  1068. .ccid_owner = THIS_MODULE,
  1069. .ccid_hc_tx_obj_size = sizeof(struct ccid3_hc_tx_sock),
  1070. .ccid_hc_tx_init = ccid3_hc_tx_init,
  1071. .ccid_hc_tx_exit = ccid3_hc_tx_exit,
  1072. .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
  1073. .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
  1074. .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
  1075. .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
  1076. .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
  1077. .ccid_hc_rx_obj_size = sizeof(struct ccid3_hc_rx_sock),
  1078. .ccid_hc_rx_init = ccid3_hc_rx_init,
  1079. .ccid_hc_rx_exit = ccid3_hc_rx_exit,
  1080. .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
  1081. .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
  1082. .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
  1083. .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
  1084. .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
  1085. .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
  1086. };
  1087. module_param(ccid3_debug, int, 0444);
  1088. MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
  1089. static __init int ccid3_module_init(void)
  1090. {
  1091. int rc = -ENOBUFS;
  1092. ccid3_rx_hist = dccp_rx_hist_new("ccid3");
  1093. if (ccid3_rx_hist == NULL)
  1094. goto out;
  1095. ccid3_tx_hist = dccp_tx_hist_new("ccid3");
  1096. if (ccid3_tx_hist == NULL)
  1097. goto out_free_rx;
  1098. ccid3_li_hist = dccp_li_hist_new("ccid3");
  1099. if (ccid3_li_hist == NULL)
  1100. goto out_free_tx;
  1101. rc = ccid_register(&ccid3);
  1102. if (rc != 0)
  1103. goto out_free_loss_interval_history;
  1104. out:
  1105. return rc;
  1106. out_free_loss_interval_history:
  1107. dccp_li_hist_delete(ccid3_li_hist);
  1108. ccid3_li_hist = NULL;
  1109. out_free_tx:
  1110. dccp_tx_hist_delete(ccid3_tx_hist);
  1111. ccid3_tx_hist = NULL;
  1112. out_free_rx:
  1113. dccp_rx_hist_delete(ccid3_rx_hist);
  1114. ccid3_rx_hist = NULL;
  1115. goto out;
  1116. }
  1117. module_init(ccid3_module_init);
  1118. static __exit void ccid3_module_exit(void)
  1119. {
  1120. ccid_unregister(&ccid3);
  1121. if (ccid3_tx_hist != NULL) {
  1122. dccp_tx_hist_delete(ccid3_tx_hist);
  1123. ccid3_tx_hist = NULL;
  1124. }
  1125. if (ccid3_rx_hist != NULL) {
  1126. dccp_rx_hist_delete(ccid3_rx_hist);
  1127. ccid3_rx_hist = NULL;
  1128. }
  1129. if (ccid3_li_hist != NULL) {
  1130. dccp_li_hist_delete(ccid3_li_hist);
  1131. ccid3_li_hist = NULL;
  1132. }
  1133. }
  1134. module_exit(ccid3_module_exit);
  1135. MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>, "
  1136. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  1137. MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
  1138. MODULE_LICENSE("GPL");
  1139. MODULE_ALIAS("net-dccp-ccid-3");