ccid3.c 35 KB

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