ccid3.c 37 KB

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