ccid3.c 35 KB

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