ccid3.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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 Value" in RFC 4342.
  344. */
  345. quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count);
  346. if (likely(hctx->ccid3hctx_rtt > 8))
  347. quarter_rtt /= hctx->ccid3hctx_rtt / 4;
  348. if (quarter_rtt > 0) {
  349. hctx->ccid3hctx_t_last_win_count = now;
  350. hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count +
  351. min_t(unsigned long, quarter_rtt, 5)) % 16;
  352. ccid3_pr_debug("%s, sk=%p, window changed from "
  353. "%u to %u!\n",
  354. dccp_role(sk), sk,
  355. packet->dccphtx_ccval,
  356. hctx->ccid3hctx_last_win_count);
  357. }
  358. hctx->ccid3hctx_idle = 0;
  359. packet->dccphtx_rtt = hctx->ccid3hctx_rtt;
  360. packet->dccphtx_sent = 1;
  361. } else
  362. ccid3_pr_debug("%s, sk=%p, seqno=%llu NOT inserted!\n",
  363. dccp_role(sk), sk, dp->dccps_gss);
  364. switch (hctx->ccid3hctx_state) {
  365. case TFRC_SSTATE_NO_SENT:
  366. /* if first wasn't pure ack */
  367. if (len != 0)
  368. printk(KERN_CRIT "%s: %s, First packet sent is noted "
  369. "as a data packet\n",
  370. __FUNCTION__, dccp_role(sk));
  371. return;
  372. case TFRC_SSTATE_NO_FBACK:
  373. case TFRC_SSTATE_FBACK:
  374. if (len > 0) {
  375. timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
  376. hctx->ccid3hctx_t_ipi);
  377. ccid3_calc_new_t_ipi(hctx);
  378. ccid3_calc_new_delta(hctx);
  379. timeval_add_usecs(&hctx->ccid3hctx_t_nom,
  380. hctx->ccid3hctx_t_ipi);
  381. }
  382. break;
  383. default:
  384. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  385. __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
  386. dump_stack();
  387. break;
  388. }
  389. }
  390. static void ccid3_hc_tx_packet_recv(struct sock *sk, struct sk_buff *skb)
  391. {
  392. const struct dccp_sock *dp = dccp_sk(sk);
  393. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  394. struct ccid3_options_received *opt_recv;
  395. struct dccp_tx_hist_entry *packet;
  396. struct timeval now;
  397. unsigned long next_tmout;
  398. u32 t_elapsed;
  399. u32 pinv;
  400. u32 x_recv;
  401. u32 r_sample;
  402. BUG_ON(hctx == NULL || hctx->ccid3hctx_state == TFRC_SSTATE_TERM);
  403. /* we are only interested in ACKs */
  404. if (!(DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK ||
  405. DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_DATAACK))
  406. return;
  407. opt_recv = &hctx->ccid3hctx_options_received;
  408. t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10;
  409. x_recv = opt_recv->ccid3or_receive_rate;
  410. pinv = opt_recv->ccid3or_loss_event_rate;
  411. switch (hctx->ccid3hctx_state) {
  412. case TFRC_SSTATE_NO_SENT:
  413. /* FIXME: what to do here? */
  414. return;
  415. case TFRC_SSTATE_NO_FBACK:
  416. case TFRC_SSTATE_FBACK:
  417. /* Calculate new round trip sample by
  418. * R_sample = (now - t_recvdata) - t_delay */
  419. /* get t_recvdata from history */
  420. packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist,
  421. DCCP_SKB_CB(skb)->dccpd_ack_seq);
  422. if (unlikely(packet == NULL)) {
  423. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, seqno "
  424. "%llu(%s) does't exist in history!\n",
  425. __FUNCTION__, dccp_role(sk), sk,
  426. (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
  427. dccp_packet_name(DCCP_SKB_CB(skb)->dccpd_type));
  428. return;
  429. }
  430. /* Update RTT */
  431. dccp_timestamp(sk, &now);
  432. r_sample = timeval_delta(&now, &packet->dccphtx_tstamp);
  433. if (unlikely(r_sample <= t_elapsed))
  434. LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
  435. "t_elapsed=%uus\n",
  436. __FUNCTION__, r_sample, t_elapsed);
  437. else
  438. r_sample -= t_elapsed;
  439. /* Update RTT estimate by
  440. * If (No feedback recv)
  441. * R = R_sample;
  442. * Else
  443. * R = q * R + (1 - q) * R_sample;
  444. *
  445. * q is a constant, RFC 3448 recomments 0.9
  446. */
  447. if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) {
  448. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_FBACK);
  449. hctx->ccid3hctx_rtt = r_sample;
  450. } else
  451. hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 +
  452. r_sample / 10;
  453. ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, "
  454. "r_sample=%us\n", dccp_role(sk), sk,
  455. hctx->ccid3hctx_rtt, r_sample);
  456. /* Update timeout interval */
  457. hctx->ccid3hctx_t_rto = max_t(u32, 4 * hctx->ccid3hctx_rtt,
  458. USEC_PER_SEC);
  459. /* Update receive rate */
  460. hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */
  461. /* Update loss event rate */
  462. if (pinv == ~0 || pinv == 0)
  463. hctx->ccid3hctx_p = 0;
  464. else {
  465. hctx->ccid3hctx_p = 1000000 / pinv;
  466. if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) {
  467. hctx->ccid3hctx_p = TFRC_SMALLEST_P;
  468. ccid3_pr_debug("%s, sk=%p, Smallest p used!\n",
  469. dccp_role(sk), sk);
  470. }
  471. }
  472. /* unschedule no feedback timer */
  473. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  474. /* Update sending rate */
  475. ccid3_hc_tx_update_x(sk);
  476. /* Update next send time */
  477. timeval_sub_usecs(&hctx->ccid3hctx_t_nom,
  478. hctx->ccid3hctx_t_ipi);
  479. ccid3_calc_new_t_ipi(hctx);
  480. timeval_add_usecs(&hctx->ccid3hctx_t_nom,
  481. hctx->ccid3hctx_t_ipi);
  482. ccid3_calc_new_delta(hctx);
  483. /* remove all packets older than the one acked from history */
  484. dccp_tx_hist_purge_older(ccid3_tx_hist,
  485. &hctx->ccid3hctx_hist, packet);
  486. /*
  487. * As we have calculated new ipi, delta, t_nom it is possible that
  488. * we now can send a packet, so wake up dccp_wait_for_ccids.
  489. */
  490. sk->sk_write_space(sk);
  491. /*
  492. * Schedule no feedback timer to expire in
  493. * max(4 * R, 2 * s / X)
  494. */
  495. next_tmout = max(hctx->ccid3hctx_t_rto,
  496. 2 * usecs_div(hctx->ccid3hctx_s,
  497. hctx->ccid3hctx_x));
  498. ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to "
  499. "expire in %lu jiffies (%luus)\n",
  500. dccp_role(sk), sk,
  501. usecs_to_jiffies(next_tmout), next_tmout);
  502. sk_reset_timer(sk, &hctx->ccid3hctx_no_feedback_timer,
  503. jiffies + max_t(u32, 1, usecs_to_jiffies(next_tmout)));
  504. /* set idle flag */
  505. hctx->ccid3hctx_idle = 1;
  506. break;
  507. default:
  508. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  509. __FUNCTION__, dccp_role(sk), sk, hctx->ccid3hctx_state);
  510. dump_stack();
  511. break;
  512. }
  513. }
  514. static int ccid3_hc_tx_insert_options(struct sock *sk, struct sk_buff *skb)
  515. {
  516. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  517. BUG_ON(hctx == NULL);
  518. if (sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN)
  519. DCCP_SKB_CB(skb)->dccpd_ccval = hctx->ccid3hctx_last_win_count;
  520. return 0;
  521. }
  522. static int ccid3_hc_tx_parse_options(struct sock *sk, unsigned char option,
  523. unsigned char len, u16 idx,
  524. unsigned char *value)
  525. {
  526. int rc = 0;
  527. const struct dccp_sock *dp = dccp_sk(sk);
  528. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  529. struct ccid3_options_received *opt_recv;
  530. BUG_ON(hctx == NULL);
  531. opt_recv = &hctx->ccid3hctx_options_received;
  532. if (opt_recv->ccid3or_seqno != dp->dccps_gsr) {
  533. opt_recv->ccid3or_seqno = dp->dccps_gsr;
  534. opt_recv->ccid3or_loss_event_rate = ~0;
  535. opt_recv->ccid3or_loss_intervals_idx = 0;
  536. opt_recv->ccid3or_loss_intervals_len = 0;
  537. opt_recv->ccid3or_receive_rate = 0;
  538. }
  539. switch (option) {
  540. case TFRC_OPT_LOSS_EVENT_RATE:
  541. if (unlikely(len != 4)) {
  542. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
  543. "len for TFRC_OPT_LOSS_EVENT_RATE\n",
  544. __FUNCTION__, dccp_role(sk), sk);
  545. rc = -EINVAL;
  546. } else {
  547. opt_recv->ccid3or_loss_event_rate = ntohl(*(__be32 *)value);
  548. ccid3_pr_debug("%s, sk=%p, LOSS_EVENT_RATE=%u\n",
  549. dccp_role(sk), sk,
  550. opt_recv->ccid3or_loss_event_rate);
  551. }
  552. break;
  553. case TFRC_OPT_LOSS_INTERVALS:
  554. opt_recv->ccid3or_loss_intervals_idx = idx;
  555. opt_recv->ccid3or_loss_intervals_len = len;
  556. ccid3_pr_debug("%s, sk=%p, LOSS_INTERVALS=(%u, %u)\n",
  557. dccp_role(sk), sk,
  558. opt_recv->ccid3or_loss_intervals_idx,
  559. opt_recv->ccid3or_loss_intervals_len);
  560. break;
  561. case TFRC_OPT_RECEIVE_RATE:
  562. if (unlikely(len != 4)) {
  563. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, invalid "
  564. "len for TFRC_OPT_RECEIVE_RATE\n",
  565. __FUNCTION__, dccp_role(sk), sk);
  566. rc = -EINVAL;
  567. } else {
  568. opt_recv->ccid3or_receive_rate = ntohl(*(__be32 *)value);
  569. ccid3_pr_debug("%s, sk=%p, RECEIVE_RATE=%u\n",
  570. dccp_role(sk), sk,
  571. opt_recv->ccid3or_receive_rate);
  572. }
  573. break;
  574. }
  575. return rc;
  576. }
  577. static int ccid3_hc_tx_init(struct ccid *ccid, struct sock *sk)
  578. {
  579. struct dccp_sock *dp = dccp_sk(sk);
  580. struct ccid3_hc_tx_sock *hctx = ccid_priv(ccid);
  581. if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
  582. dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
  583. hctx->ccid3hctx_s = dp->dccps_packet_size;
  584. else
  585. hctx->ccid3hctx_s = TFRC_STD_PACKET_SIZE;
  586. /* Set transmission rate to 1 packet per second */
  587. hctx->ccid3hctx_x = hctx->ccid3hctx_s;
  588. hctx->ccid3hctx_t_rto = USEC_PER_SEC;
  589. hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT;
  590. INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
  591. hctx->ccid3hctx_no_feedback_timer.function = ccid3_hc_tx_no_feedback_timer;
  592. hctx->ccid3hctx_no_feedback_timer.data = (unsigned long)sk;
  593. init_timer(&hctx->ccid3hctx_no_feedback_timer);
  594. return 0;
  595. }
  596. static void ccid3_hc_tx_exit(struct sock *sk)
  597. {
  598. struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  599. BUG_ON(hctx == NULL);
  600. ccid3_hc_tx_set_state(sk, TFRC_SSTATE_TERM);
  601. sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer);
  602. /* Empty packet history */
  603. dccp_tx_hist_purge(ccid3_tx_hist, &hctx->ccid3hctx_hist);
  604. }
  605. /*
  606. * RX Half Connection methods
  607. */
  608. /* TFRC receiver states */
  609. enum ccid3_hc_rx_states {
  610. TFRC_RSTATE_NO_DATA = 1,
  611. TFRC_RSTATE_DATA,
  612. TFRC_RSTATE_TERM = 127,
  613. };
  614. #ifdef CCID3_DEBUG
  615. static const char *ccid3_rx_state_name(enum ccid3_hc_rx_states state)
  616. {
  617. static char *ccid3_rx_state_names[] = {
  618. [TFRC_RSTATE_NO_DATA] = "NO_DATA",
  619. [TFRC_RSTATE_DATA] = "DATA",
  620. [TFRC_RSTATE_TERM] = "TERM",
  621. };
  622. return ccid3_rx_state_names[state];
  623. }
  624. #endif
  625. static void ccid3_hc_rx_set_state(struct sock *sk,
  626. enum ccid3_hc_rx_states state)
  627. {
  628. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  629. enum ccid3_hc_rx_states oldstate = hcrx->ccid3hcrx_state;
  630. ccid3_pr_debug("%s(%p) %-8.8s -> %s\n",
  631. dccp_role(sk), sk, ccid3_rx_state_name(oldstate),
  632. ccid3_rx_state_name(state));
  633. WARN_ON(state == oldstate);
  634. hcrx->ccid3hcrx_state = state;
  635. }
  636. static void ccid3_hc_rx_send_feedback(struct sock *sk)
  637. {
  638. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  639. struct dccp_sock *dp = dccp_sk(sk);
  640. struct dccp_rx_hist_entry *packet;
  641. struct timeval now;
  642. ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
  643. dccp_timestamp(sk, &now);
  644. switch (hcrx->ccid3hcrx_state) {
  645. case TFRC_RSTATE_NO_DATA:
  646. hcrx->ccid3hcrx_x_recv = 0;
  647. break;
  648. case TFRC_RSTATE_DATA: {
  649. const u32 delta = timeval_delta(&now,
  650. &hcrx->ccid3hcrx_tstamp_last_feedback);
  651. hcrx->ccid3hcrx_x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv,
  652. delta);
  653. }
  654. break;
  655. default:
  656. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  657. __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
  658. dump_stack();
  659. return;
  660. }
  661. packet = dccp_rx_hist_find_data_packet(&hcrx->ccid3hcrx_hist);
  662. if (unlikely(packet == NULL)) {
  663. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, no data packet "
  664. "in history!\n",
  665. __FUNCTION__, dccp_role(sk), sk);
  666. return;
  667. }
  668. hcrx->ccid3hcrx_tstamp_last_feedback = now;
  669. hcrx->ccid3hcrx_ccval_last_counter = packet->dccphrx_ccval;
  670. hcrx->ccid3hcrx_bytes_recv = 0;
  671. /* Convert to multiples of 10us */
  672. hcrx->ccid3hcrx_elapsed_time =
  673. timeval_delta(&now, &packet->dccphrx_tstamp) / 10;
  674. if (hcrx->ccid3hcrx_p == 0)
  675. hcrx->ccid3hcrx_pinv = ~0;
  676. else
  677. hcrx->ccid3hcrx_pinv = 1000000 / hcrx->ccid3hcrx_p;
  678. dp->dccps_hc_rx_insert_options = 1;
  679. dccp_send_ack(sk);
  680. }
  681. static int ccid3_hc_rx_insert_options(struct sock *sk, struct sk_buff *skb)
  682. {
  683. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  684. __be32 x_recv, pinv;
  685. BUG_ON(hcrx == NULL);
  686. if (!(sk->sk_state == DCCP_OPEN || sk->sk_state == DCCP_PARTOPEN))
  687. return 0;
  688. DCCP_SKB_CB(skb)->dccpd_ccval = hcrx->ccid3hcrx_ccval_last_counter;
  689. if (dccp_packet_without_ack(skb))
  690. return 0;
  691. x_recv = htonl(hcrx->ccid3hcrx_x_recv);
  692. pinv = htonl(hcrx->ccid3hcrx_pinv);
  693. if ((hcrx->ccid3hcrx_elapsed_time != 0 &&
  694. dccp_insert_option_elapsed_time(sk, skb,
  695. hcrx->ccid3hcrx_elapsed_time)) ||
  696. dccp_insert_option_timestamp(sk, skb) ||
  697. dccp_insert_option(sk, skb, TFRC_OPT_LOSS_EVENT_RATE,
  698. &pinv, sizeof(pinv)) ||
  699. dccp_insert_option(sk, skb, TFRC_OPT_RECEIVE_RATE,
  700. &x_recv, sizeof(x_recv)))
  701. return -1;
  702. return 0;
  703. }
  704. /* calculate first loss interval
  705. *
  706. * returns estimated loss interval in usecs */
  707. static u32 ccid3_hc_rx_calc_first_li(struct sock *sk)
  708. {
  709. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  710. struct dccp_rx_hist_entry *entry, *next, *tail = NULL;
  711. u32 rtt, delta, x_recv, fval, p, tmp2;
  712. struct timeval tstamp = { 0, };
  713. int interval = 0;
  714. int win_count = 0;
  715. int step = 0;
  716. u64 tmp1;
  717. list_for_each_entry_safe(entry, next, &hcrx->ccid3hcrx_hist,
  718. dccphrx_node) {
  719. if (dccp_rx_hist_entry_data_packet(entry)) {
  720. tail = entry;
  721. switch (step) {
  722. case 0:
  723. tstamp = entry->dccphrx_tstamp;
  724. win_count = entry->dccphrx_ccval;
  725. step = 1;
  726. break;
  727. case 1:
  728. interval = win_count - entry->dccphrx_ccval;
  729. if (interval < 0)
  730. interval += TFRC_WIN_COUNT_LIMIT;
  731. if (interval > 4)
  732. goto found;
  733. break;
  734. }
  735. }
  736. }
  737. if (unlikely(step == 0)) {
  738. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, packet history "
  739. "contains no data packets!\n",
  740. __FUNCTION__, dccp_role(sk), sk);
  741. return ~0;
  742. }
  743. if (unlikely(interval == 0)) {
  744. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Could not find a "
  745. "win_count interval > 0. Defaulting to 1\n",
  746. __FUNCTION__, dccp_role(sk), sk);
  747. interval = 1;
  748. }
  749. found:
  750. if (!tail) {
  751. LIMIT_NETDEBUG(KERN_WARNING "%s: tail is null\n",
  752. __FUNCTION__);
  753. return ~0;
  754. }
  755. rtt = timeval_delta(&tstamp, &tail->dccphrx_tstamp) * 4 / interval;
  756. ccid3_pr_debug("%s, sk=%p, approximated RTT to %uus\n",
  757. dccp_role(sk), sk, rtt);
  758. if (rtt == 0)
  759. rtt = 1;
  760. dccp_timestamp(sk, &tstamp);
  761. delta = timeval_delta(&tstamp, &hcrx->ccid3hcrx_tstamp_last_feedback);
  762. x_recv = usecs_div(hcrx->ccid3hcrx_bytes_recv, delta);
  763. if (x_recv == 0)
  764. x_recv = hcrx->ccid3hcrx_x_recv;
  765. tmp1 = (u64)x_recv * (u64)rtt;
  766. do_div(tmp1,10000000);
  767. tmp2 = (u32)tmp1;
  768. if (!tmp2) {
  769. LIMIT_NETDEBUG(KERN_WARNING "tmp2 = 0 "
  770. "%s: x_recv = %u, rtt =%u\n",
  771. __FUNCTION__, x_recv, rtt);
  772. return ~0;
  773. }
  774. fval = (hcrx->ccid3hcrx_s * 100000) / tmp2;
  775. /* do not alter order above or you will get overflow on 32 bit */
  776. p = tfrc_calc_x_reverse_lookup(fval);
  777. ccid3_pr_debug("%s, sk=%p, receive rate=%u bytes/s, implied "
  778. "loss rate=%u\n", dccp_role(sk), sk, x_recv, p);
  779. if (p == 0)
  780. return ~0;
  781. else
  782. return 1000000 / p;
  783. }
  784. static void ccid3_hc_rx_update_li(struct sock *sk, u64 seq_loss, u8 win_loss)
  785. {
  786. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  787. struct dccp_li_hist_entry *head;
  788. u64 seq_temp;
  789. if (list_empty(&hcrx->ccid3hcrx_li_hist)) {
  790. if (!dccp_li_hist_interval_new(ccid3_li_hist,
  791. &hcrx->ccid3hcrx_li_hist, seq_loss, win_loss))
  792. return;
  793. head = list_entry(hcrx->ccid3hcrx_li_hist.next,
  794. struct dccp_li_hist_entry, dccplih_node);
  795. head->dccplih_interval = ccid3_hc_rx_calc_first_li(sk);
  796. } else {
  797. struct dccp_li_hist_entry *entry;
  798. struct list_head *tail;
  799. head = list_entry(hcrx->ccid3hcrx_li_hist.next,
  800. struct dccp_li_hist_entry, dccplih_node);
  801. /* FIXME win count check removed as was wrong */
  802. /* should make this check with receive history */
  803. /* and compare there as per section 10.2 of RFC4342 */
  804. /* new loss event detected */
  805. /* calculate last interval length */
  806. seq_temp = dccp_delta_seqno(head->dccplih_seqno, seq_loss);
  807. entry = dccp_li_hist_entry_new(ccid3_li_hist, SLAB_ATOMIC);
  808. if (entry == NULL) {
  809. printk(KERN_CRIT "%s: out of memory\n",__FUNCTION__);
  810. dump_stack();
  811. return;
  812. }
  813. list_add(&entry->dccplih_node, &hcrx->ccid3hcrx_li_hist);
  814. tail = hcrx->ccid3hcrx_li_hist.prev;
  815. list_del(tail);
  816. kmem_cache_free(ccid3_li_hist->dccplih_slab, tail);
  817. /* Create the newest interval */
  818. entry->dccplih_seqno = seq_loss;
  819. entry->dccplih_interval = seq_temp;
  820. entry->dccplih_win_count = win_loss;
  821. }
  822. }
  823. static int ccid3_hc_rx_detect_loss(struct sock *sk,
  824. struct dccp_rx_hist_entry *packet)
  825. {
  826. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  827. struct dccp_rx_hist_entry *rx_hist = dccp_rx_hist_head(&hcrx->ccid3hcrx_hist);
  828. u64 seqno = packet->dccphrx_seqno;
  829. u64 tmp_seqno;
  830. int loss = 0;
  831. u8 ccval;
  832. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  833. if (!rx_hist ||
  834. follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  835. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  836. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  837. goto detect_out;
  838. }
  839. while (dccp_delta_seqno(hcrx->ccid3hcrx_seqno_nonloss, seqno)
  840. > TFRC_RECV_NUM_LATE_LOSS) {
  841. loss = 1;
  842. ccid3_hc_rx_update_li(sk, hcrx->ccid3hcrx_seqno_nonloss,
  843. hcrx->ccid3hcrx_ccval_nonloss);
  844. tmp_seqno = hcrx->ccid3hcrx_seqno_nonloss;
  845. dccp_inc_seqno(&tmp_seqno);
  846. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  847. dccp_inc_seqno(&tmp_seqno);
  848. while (dccp_rx_hist_find_entry(&hcrx->ccid3hcrx_hist,
  849. tmp_seqno, &ccval)) {
  850. hcrx->ccid3hcrx_seqno_nonloss = tmp_seqno;
  851. hcrx->ccid3hcrx_ccval_nonloss = ccval;
  852. dccp_inc_seqno(&tmp_seqno);
  853. }
  854. }
  855. /* FIXME - this code could be simplified with above while */
  856. /* but works at moment */
  857. if (follows48(packet->dccphrx_seqno, hcrx->ccid3hcrx_seqno_nonloss)) {
  858. hcrx->ccid3hcrx_seqno_nonloss = seqno;
  859. hcrx->ccid3hcrx_ccval_nonloss = packet->dccphrx_ccval;
  860. }
  861. detect_out:
  862. dccp_rx_hist_add_packet(ccid3_rx_hist, &hcrx->ccid3hcrx_hist,
  863. &hcrx->ccid3hcrx_li_hist, packet,
  864. hcrx->ccid3hcrx_seqno_nonloss);
  865. return loss;
  866. }
  867. static void ccid3_hc_rx_packet_recv(struct sock *sk, struct sk_buff *skb)
  868. {
  869. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  870. const struct dccp_options_received *opt_recv;
  871. struct dccp_rx_hist_entry *packet;
  872. struct timeval now;
  873. u8 win_count;
  874. u32 p_prev, rtt_prev, r_sample, t_elapsed;
  875. int loss;
  876. BUG_ON(hcrx == NULL ||
  877. !(hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA ||
  878. hcrx->ccid3hcrx_state == TFRC_RSTATE_DATA));
  879. opt_recv = &dccp_sk(sk)->dccps_options_received;
  880. switch (DCCP_SKB_CB(skb)->dccpd_type) {
  881. case DCCP_PKT_ACK:
  882. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  883. return;
  884. case DCCP_PKT_DATAACK:
  885. if (opt_recv->dccpor_timestamp_echo == 0)
  886. break;
  887. rtt_prev = hcrx->ccid3hcrx_rtt;
  888. dccp_timestamp(sk, &now);
  889. timeval_sub_usecs(&now, opt_recv->dccpor_timestamp_echo * 10);
  890. r_sample = timeval_usecs(&now);
  891. t_elapsed = opt_recv->dccpor_elapsed_time * 10;
  892. if (unlikely(r_sample <= t_elapsed))
  893. LIMIT_NETDEBUG(KERN_WARNING "%s: r_sample=%uus, "
  894. "t_elapsed=%uus\n",
  895. __FUNCTION__, r_sample, t_elapsed);
  896. else
  897. r_sample -= t_elapsed;
  898. if (hcrx->ccid3hcrx_state == TFRC_RSTATE_NO_DATA)
  899. hcrx->ccid3hcrx_rtt = r_sample;
  900. else
  901. hcrx->ccid3hcrx_rtt = (hcrx->ccid3hcrx_rtt * 9) / 10 +
  902. r_sample / 10;
  903. if (rtt_prev != hcrx->ccid3hcrx_rtt)
  904. ccid3_pr_debug("%s, New RTT=%uus, elapsed time=%u\n",
  905. dccp_role(sk), hcrx->ccid3hcrx_rtt,
  906. opt_recv->dccpor_elapsed_time);
  907. break;
  908. case DCCP_PKT_DATA:
  909. break;
  910. default: /* We're not interested in other packet types, move along */
  911. return;
  912. }
  913. packet = dccp_rx_hist_entry_new(ccid3_rx_hist, sk, opt_recv->dccpor_ndp,
  914. skb, SLAB_ATOMIC);
  915. if (unlikely(packet == NULL)) {
  916. LIMIT_NETDEBUG(KERN_WARNING "%s: %s, sk=%p, Not enough mem to "
  917. "add rx packet to history, consider it lost!\n",
  918. __FUNCTION__, dccp_role(sk), sk);
  919. return;
  920. }
  921. win_count = packet->dccphrx_ccval;
  922. loss = ccid3_hc_rx_detect_loss(sk, packet);
  923. if (DCCP_SKB_CB(skb)->dccpd_type == DCCP_PKT_ACK)
  924. return;
  925. switch (hcrx->ccid3hcrx_state) {
  926. case TFRC_RSTATE_NO_DATA:
  927. ccid3_pr_debug("%s, sk=%p(%s), skb=%p, sending initial "
  928. "feedback\n",
  929. dccp_role(sk), sk,
  930. dccp_state_name(sk->sk_state), skb);
  931. ccid3_hc_rx_send_feedback(sk);
  932. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_DATA);
  933. return;
  934. case TFRC_RSTATE_DATA:
  935. hcrx->ccid3hcrx_bytes_recv += skb->len -
  936. dccp_hdr(skb)->dccph_doff * 4;
  937. if (loss)
  938. break;
  939. dccp_timestamp(sk, &now);
  940. if (timeval_delta(&now, &hcrx->ccid3hcrx_tstamp_last_ack) >=
  941. hcrx->ccid3hcrx_rtt) {
  942. hcrx->ccid3hcrx_tstamp_last_ack = now;
  943. ccid3_hc_rx_send_feedback(sk);
  944. }
  945. return;
  946. default:
  947. printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n",
  948. __FUNCTION__, dccp_role(sk), sk, hcrx->ccid3hcrx_state);
  949. dump_stack();
  950. return;
  951. }
  952. /* Dealing with packet loss */
  953. ccid3_pr_debug("%s, sk=%p(%s), data loss! Reacting...\n",
  954. dccp_role(sk), sk, dccp_state_name(sk->sk_state));
  955. p_prev = hcrx->ccid3hcrx_p;
  956. /* Calculate loss event rate */
  957. if (!list_empty(&hcrx->ccid3hcrx_li_hist)) {
  958. u32 i_mean = dccp_li_hist_calc_i_mean(&hcrx->ccid3hcrx_li_hist);
  959. /* Scaling up by 1000000 as fixed decimal */
  960. if (i_mean != 0)
  961. hcrx->ccid3hcrx_p = 1000000 / i_mean;
  962. } else {
  963. printk(KERN_CRIT "%s: empty loss hist\n",__FUNCTION__);
  964. dump_stack();
  965. }
  966. if (hcrx->ccid3hcrx_p > p_prev) {
  967. ccid3_hc_rx_send_feedback(sk);
  968. return;
  969. }
  970. }
  971. static int ccid3_hc_rx_init(struct ccid *ccid, struct sock *sk)
  972. {
  973. struct dccp_sock *dp = dccp_sk(sk);
  974. struct ccid3_hc_rx_sock *hcrx = ccid_priv(ccid);
  975. ccid3_pr_debug("%s, sk=%p\n", dccp_role(sk), sk);
  976. if (dp->dccps_packet_size >= TFRC_MIN_PACKET_SIZE &&
  977. dp->dccps_packet_size <= TFRC_MAX_PACKET_SIZE)
  978. hcrx->ccid3hcrx_s = dp->dccps_packet_size;
  979. else
  980. hcrx->ccid3hcrx_s = TFRC_STD_PACKET_SIZE;
  981. hcrx->ccid3hcrx_state = TFRC_RSTATE_NO_DATA;
  982. INIT_LIST_HEAD(&hcrx->ccid3hcrx_hist);
  983. INIT_LIST_HEAD(&hcrx->ccid3hcrx_li_hist);
  984. dccp_timestamp(sk, &hcrx->ccid3hcrx_tstamp_last_ack);
  985. hcrx->ccid3hcrx_tstamp_last_feedback = hcrx->ccid3hcrx_tstamp_last_ack;
  986. hcrx->ccid3hcrx_rtt = 5000; /* XXX 5ms for now... */
  987. return 0;
  988. }
  989. static void ccid3_hc_rx_exit(struct sock *sk)
  990. {
  991. struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  992. BUG_ON(hcrx == NULL);
  993. ccid3_hc_rx_set_state(sk, TFRC_RSTATE_TERM);
  994. /* Empty packet history */
  995. dccp_rx_hist_purge(ccid3_rx_hist, &hcrx->ccid3hcrx_hist);
  996. /* Empty loss interval history */
  997. dccp_li_hist_purge(ccid3_li_hist, &hcrx->ccid3hcrx_li_hist);
  998. }
  999. static void ccid3_hc_rx_get_info(struct sock *sk, struct tcp_info *info)
  1000. {
  1001. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  1002. /* Listen socks doesn't have a private CCID block */
  1003. if (sk->sk_state == DCCP_LISTEN)
  1004. return;
  1005. BUG_ON(hcrx == NULL);
  1006. info->tcpi_ca_state = hcrx->ccid3hcrx_state;
  1007. info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
  1008. info->tcpi_rcv_rtt = hcrx->ccid3hcrx_rtt;
  1009. }
  1010. static void ccid3_hc_tx_get_info(struct sock *sk, struct tcp_info *info)
  1011. {
  1012. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  1013. /* Listen socks doesn't have a private CCID block */
  1014. if (sk->sk_state == DCCP_LISTEN)
  1015. return;
  1016. BUG_ON(hctx == NULL);
  1017. info->tcpi_rto = hctx->ccid3hctx_t_rto;
  1018. info->tcpi_rtt = hctx->ccid3hctx_rtt;
  1019. }
  1020. static int ccid3_hc_rx_getsockopt(struct sock *sk, const int optname, int len,
  1021. u32 __user *optval, int __user *optlen)
  1022. {
  1023. const struct ccid3_hc_rx_sock *hcrx = ccid3_hc_rx_sk(sk);
  1024. const void *val;
  1025. /* Listen socks doesn't have a private CCID block */
  1026. if (sk->sk_state == DCCP_LISTEN)
  1027. return -EINVAL;
  1028. switch (optname) {
  1029. case DCCP_SOCKOPT_CCID_RX_INFO:
  1030. if (len < sizeof(hcrx->ccid3hcrx_tfrc))
  1031. return -EINVAL;
  1032. len = sizeof(hcrx->ccid3hcrx_tfrc);
  1033. val = &hcrx->ccid3hcrx_tfrc;
  1034. break;
  1035. default:
  1036. return -ENOPROTOOPT;
  1037. }
  1038. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  1039. return -EFAULT;
  1040. return 0;
  1041. }
  1042. static int ccid3_hc_tx_getsockopt(struct sock *sk, const int optname, int len,
  1043. u32 __user *optval, int __user *optlen)
  1044. {
  1045. const struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk);
  1046. const void *val;
  1047. /* Listen socks doesn't have a private CCID block */
  1048. if (sk->sk_state == DCCP_LISTEN)
  1049. return -EINVAL;
  1050. switch (optname) {
  1051. case DCCP_SOCKOPT_CCID_TX_INFO:
  1052. if (len < sizeof(hctx->ccid3hctx_tfrc))
  1053. return -EINVAL;
  1054. len = sizeof(hctx->ccid3hctx_tfrc);
  1055. val = &hctx->ccid3hctx_tfrc;
  1056. break;
  1057. default:
  1058. return -ENOPROTOOPT;
  1059. }
  1060. if (put_user(len, optlen) || copy_to_user(optval, val, len))
  1061. return -EFAULT;
  1062. return 0;
  1063. }
  1064. static struct ccid_operations ccid3 = {
  1065. .ccid_id = DCCPC_CCID3,
  1066. .ccid_name = "ccid3",
  1067. .ccid_owner = THIS_MODULE,
  1068. .ccid_hc_tx_obj_size = sizeof(struct ccid3_hc_tx_sock),
  1069. .ccid_hc_tx_init = ccid3_hc_tx_init,
  1070. .ccid_hc_tx_exit = ccid3_hc_tx_exit,
  1071. .ccid_hc_tx_send_packet = ccid3_hc_tx_send_packet,
  1072. .ccid_hc_tx_packet_sent = ccid3_hc_tx_packet_sent,
  1073. .ccid_hc_tx_packet_recv = ccid3_hc_tx_packet_recv,
  1074. .ccid_hc_tx_insert_options = ccid3_hc_tx_insert_options,
  1075. .ccid_hc_tx_parse_options = ccid3_hc_tx_parse_options,
  1076. .ccid_hc_rx_obj_size = sizeof(struct ccid3_hc_rx_sock),
  1077. .ccid_hc_rx_init = ccid3_hc_rx_init,
  1078. .ccid_hc_rx_exit = ccid3_hc_rx_exit,
  1079. .ccid_hc_rx_insert_options = ccid3_hc_rx_insert_options,
  1080. .ccid_hc_rx_packet_recv = ccid3_hc_rx_packet_recv,
  1081. .ccid_hc_rx_get_info = ccid3_hc_rx_get_info,
  1082. .ccid_hc_tx_get_info = ccid3_hc_tx_get_info,
  1083. .ccid_hc_rx_getsockopt = ccid3_hc_rx_getsockopt,
  1084. .ccid_hc_tx_getsockopt = ccid3_hc_tx_getsockopt,
  1085. };
  1086. module_param(ccid3_debug, int, 0444);
  1087. MODULE_PARM_DESC(ccid3_debug, "Enable debug messages");
  1088. static __init int ccid3_module_init(void)
  1089. {
  1090. int rc = -ENOBUFS;
  1091. ccid3_rx_hist = dccp_rx_hist_new("ccid3");
  1092. if (ccid3_rx_hist == NULL)
  1093. goto out;
  1094. ccid3_tx_hist = dccp_tx_hist_new("ccid3");
  1095. if (ccid3_tx_hist == NULL)
  1096. goto out_free_rx;
  1097. ccid3_li_hist = dccp_li_hist_new("ccid3");
  1098. if (ccid3_li_hist == NULL)
  1099. goto out_free_tx;
  1100. rc = ccid_register(&ccid3);
  1101. if (rc != 0)
  1102. goto out_free_loss_interval_history;
  1103. out:
  1104. return rc;
  1105. out_free_loss_interval_history:
  1106. dccp_li_hist_delete(ccid3_li_hist);
  1107. ccid3_li_hist = NULL;
  1108. out_free_tx:
  1109. dccp_tx_hist_delete(ccid3_tx_hist);
  1110. ccid3_tx_hist = NULL;
  1111. out_free_rx:
  1112. dccp_rx_hist_delete(ccid3_rx_hist);
  1113. ccid3_rx_hist = NULL;
  1114. goto out;
  1115. }
  1116. module_init(ccid3_module_init);
  1117. static __exit void ccid3_module_exit(void)
  1118. {
  1119. ccid_unregister(&ccid3);
  1120. if (ccid3_tx_hist != NULL) {
  1121. dccp_tx_hist_delete(ccid3_tx_hist);
  1122. ccid3_tx_hist = NULL;
  1123. }
  1124. if (ccid3_rx_hist != NULL) {
  1125. dccp_rx_hist_delete(ccid3_rx_hist);
  1126. ccid3_rx_hist = NULL;
  1127. }
  1128. if (ccid3_li_hist != NULL) {
  1129. dccp_li_hist_delete(ccid3_li_hist);
  1130. ccid3_li_hist = NULL;
  1131. }
  1132. }
  1133. module_exit(ccid3_module_exit);
  1134. MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>, "
  1135. "Arnaldo Carvalho de Melo <acme@ghostprotocols.net>");
  1136. MODULE_DESCRIPTION("DCCP TFRC CCID3 CCID");
  1137. MODULE_LICENSE("GPL");
  1138. MODULE_ALIAS("net-dccp-ccid-3");