chunk.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2003, 2004
  3. *
  4. * This file is part of the SCTP kernel reference Implementation
  5. *
  6. * This file contains the code relating the chunk abstraction.
  7. *
  8. * The SCTP reference implementation is free software;
  9. * you can redistribute it and/or modify it under the terms of
  10. * the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * The SCTP reference implementation is distributed in the hope that it
  15. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  16. * ************************
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. * See the GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GNU CC; see the file COPYING. If not, write to
  22. * the Free Software Foundation, 59 Temple Place - Suite 330,
  23. * Boston, MA 02111-1307, USA.
  24. *
  25. * Please send any bug reports or fixes you make to the
  26. * email address(es):
  27. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  28. *
  29. * Or submit a bug report through the following website:
  30. * http://www.sf.net/projects/lksctp
  31. *
  32. * Written or modified by:
  33. * Jon Grimm <jgrimm@us.ibm.com>
  34. * Sridhar Samudrala <sri@us.ibm.com>
  35. *
  36. * Any bugs reported given to us we will try to fix... any fixes shared will
  37. * be incorporated into the next SCTP release.
  38. */
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/net.h>
  42. #include <linux/inet.h>
  43. #include <linux/skbuff.h>
  44. #include <net/sock.h>
  45. #include <net/sctp/sctp.h>
  46. #include <net/sctp/sm.h>
  47. /* This file is mostly in anticipation of future work, but initially
  48. * populate with fragment tracking for an outbound message.
  49. */
  50. /* Initialize datamsg from memory. */
  51. static void sctp_datamsg_init(struct sctp_datamsg *msg)
  52. {
  53. atomic_set(&msg->refcnt, 1);
  54. msg->send_failed = 0;
  55. msg->send_error = 0;
  56. msg->can_abandon = 0;
  57. msg->expires_at = 0;
  58. INIT_LIST_HEAD(&msg->chunks);
  59. }
  60. /* Allocate and initialize datamsg. */
  61. SCTP_STATIC struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
  62. {
  63. struct sctp_datamsg *msg;
  64. msg = kmalloc(sizeof(struct sctp_datamsg), gfp);
  65. if (msg)
  66. sctp_datamsg_init(msg);
  67. SCTP_DBG_OBJCNT_INC(datamsg);
  68. return msg;
  69. }
  70. /* Final destructruction of datamsg memory. */
  71. static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
  72. {
  73. struct list_head *pos, *temp;
  74. struct sctp_chunk *chunk;
  75. struct sctp_sock *sp;
  76. struct sctp_ulpevent *ev;
  77. struct sctp_association *asoc = NULL;
  78. int error = 0, notify;
  79. /* If we failed, we may need to notify. */
  80. notify = msg->send_failed ? -1 : 0;
  81. /* Release all references. */
  82. list_for_each_safe(pos, temp, &msg->chunks) {
  83. list_del_init(pos);
  84. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  85. /* Check whether we _really_ need to notify. */
  86. if (notify < 0) {
  87. asoc = chunk->asoc;
  88. if (msg->send_error)
  89. error = msg->send_error;
  90. else
  91. error = asoc->outqueue.error;
  92. sp = sctp_sk(asoc->base.sk);
  93. notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED,
  94. &sp->subscribe);
  95. }
  96. /* Generate a SEND FAILED event only if enabled. */
  97. if (notify > 0) {
  98. int sent;
  99. if (chunk->has_tsn)
  100. sent = SCTP_DATA_SENT;
  101. else
  102. sent = SCTP_DATA_UNSENT;
  103. ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
  104. error, GFP_ATOMIC);
  105. if (ev)
  106. sctp_ulpq_tail_event(&asoc->ulpq, ev);
  107. }
  108. sctp_chunk_put(chunk);
  109. }
  110. SCTP_DBG_OBJCNT_DEC(datamsg);
  111. kfree(msg);
  112. }
  113. /* Hold a reference. */
  114. static void sctp_datamsg_hold(struct sctp_datamsg *msg)
  115. {
  116. atomic_inc(&msg->refcnt);
  117. }
  118. /* Release a reference. */
  119. void sctp_datamsg_put(struct sctp_datamsg *msg)
  120. {
  121. if (atomic_dec_and_test(&msg->refcnt))
  122. sctp_datamsg_destroy(msg);
  123. }
  124. /* Free a message. Really just give up a reference, the
  125. * really free happens in sctp_datamsg_destroy().
  126. */
  127. void sctp_datamsg_free(struct sctp_datamsg *msg)
  128. {
  129. sctp_datamsg_put(msg);
  130. }
  131. /* Hold on to all the fragments until all chunks have been sent. */
  132. void sctp_datamsg_track(struct sctp_chunk *chunk)
  133. {
  134. sctp_chunk_hold(chunk);
  135. }
  136. /* Assign a chunk to this datamsg. */
  137. static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
  138. {
  139. sctp_datamsg_hold(msg);
  140. chunk->msg = msg;
  141. }
  142. /* A data chunk can have a maximum payload of (2^16 - 20). Break
  143. * down any such message into smaller chunks. Opportunistically, fragment
  144. * the chunks down to the current MTU constraints. We may get refragmented
  145. * later if the PMTU changes, but it is _much better_ to fragment immediately
  146. * with a reasonable guess than always doing our fragmentation on the
  147. * soft-interrupt.
  148. */
  149. struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
  150. struct sctp_sndrcvinfo *sinfo,
  151. struct msghdr *msgh, int msg_len)
  152. {
  153. int max, whole, i, offset, over, err;
  154. int len, first_len;
  155. struct sctp_chunk *chunk;
  156. struct sctp_datamsg *msg;
  157. struct list_head *pos, *temp;
  158. __u8 frag;
  159. msg = sctp_datamsg_new(GFP_KERNEL);
  160. if (!msg)
  161. return NULL;
  162. /* Note: Calculate this outside of the loop, so that all fragments
  163. * have the same expiration.
  164. */
  165. if (sinfo->sinfo_timetolive) {
  166. /* sinfo_timetolive is in milliseconds */
  167. msg->expires_at = jiffies +
  168. msecs_to_jiffies(sinfo->sinfo_timetolive);
  169. msg->can_abandon = 1;
  170. SCTP_DEBUG_PRINTK("%s: msg:%p expires_at: %ld jiffies:%ld\n",
  171. __FUNCTION__, msg, msg->expires_at, jiffies);
  172. }
  173. max = asoc->frag_point;
  174. whole = 0;
  175. first_len = max;
  176. /* Encourage Cookie-ECHO bundling. */
  177. if (asoc->state < SCTP_STATE_COOKIE_ECHOED) {
  178. whole = msg_len / (max - SCTP_ARBITRARY_COOKIE_ECHO_LEN);
  179. /* Account for the DATA to be bundled with the COOKIE-ECHO. */
  180. if (whole) {
  181. first_len = max - SCTP_ARBITRARY_COOKIE_ECHO_LEN;
  182. msg_len -= first_len;
  183. whole = 1;
  184. }
  185. }
  186. /* How many full sized? How many bytes leftover? */
  187. whole += msg_len / max;
  188. over = msg_len % max;
  189. offset = 0;
  190. if ((whole > 1) || (whole && over))
  191. SCTP_INC_STATS_USER(SCTP_MIB_FRAGUSRMSGS);
  192. /* Create chunks for all the full sized DATA chunks. */
  193. for (i=0, len=first_len; i < whole; i++) {
  194. frag = SCTP_DATA_MIDDLE_FRAG;
  195. if (0 == i)
  196. frag |= SCTP_DATA_FIRST_FRAG;
  197. if ((i == (whole - 1)) && !over)
  198. frag |= SCTP_DATA_LAST_FRAG;
  199. chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0);
  200. if (!chunk)
  201. goto errout;
  202. err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov);
  203. if (err < 0)
  204. goto errout;
  205. offset += len;
  206. /* Put the chunk->skb back into the form expected by send. */
  207. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  208. - (__u8 *)chunk->skb->data);
  209. sctp_datamsg_assign(msg, chunk);
  210. list_add_tail(&chunk->frag_list, &msg->chunks);
  211. /* The first chunk, the first chunk was likely short
  212. * to allow bundling, so reset to full size.
  213. */
  214. if (0 == i)
  215. len = max;
  216. }
  217. /* .. now the leftover bytes. */
  218. if (over) {
  219. if (!whole)
  220. frag = SCTP_DATA_NOT_FRAG;
  221. else
  222. frag = SCTP_DATA_LAST_FRAG;
  223. chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0);
  224. if (!chunk)
  225. goto errout;
  226. err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov);
  227. /* Put the chunk->skb back into the form expected by send. */
  228. __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
  229. - (__u8 *)chunk->skb->data);
  230. if (err < 0)
  231. goto errout;
  232. sctp_datamsg_assign(msg, chunk);
  233. list_add_tail(&chunk->frag_list, &msg->chunks);
  234. }
  235. return msg;
  236. errout:
  237. list_for_each_safe(pos, temp, &msg->chunks) {
  238. list_del_init(pos);
  239. chunk = list_entry(pos, struct sctp_chunk, frag_list);
  240. sctp_chunk_free(chunk);
  241. }
  242. sctp_datamsg_free(msg);
  243. return NULL;
  244. }
  245. /* Check whether this message has expired. */
  246. int sctp_chunk_abandoned(struct sctp_chunk *chunk)
  247. {
  248. struct sctp_datamsg *msg = chunk->msg;
  249. if (!msg->can_abandon)
  250. return 0;
  251. if (time_after(jiffies, msg->expires_at))
  252. return 1;
  253. return 0;
  254. }
  255. /* This chunk (and consequently entire message) has failed in its sending. */
  256. void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
  257. {
  258. chunk->msg->send_failed = 1;
  259. chunk->msg->send_error = error;
  260. }