ccid3.c 37 KB

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