sctp: Nagle delay should be based on path mtu
[linux-2.6-block.git] / net / sctp / chunk.c
CommitLineData
60c778b2 1/* SCTP kernel implementation
1da177e4
LT
2 * (C) Copyright IBM Corp. 2003, 2004
3 *
60c778b2 4 * This file is part of the SCTP kernel implementation
1da177e4 5 *
59c51591 6 * This file contains the code relating the chunk abstraction.
1da177e4 7 *
60c778b2 8 * This SCTP implementation is free software;
1da177e4
LT
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 *
60c778b2 14 * This SCTP implementation is distributed in the hope that it
1da177e4
LT
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
40#include <linux/types.h>
41#include <linux/kernel.h>
42#include <linux/net.h>
43#include <linux/inet.h>
44#include <linux/skbuff.h>
45#include <net/sock.h>
46#include <net/sctp/sctp.h>
47#include <net/sctp/sm.h>
48
49/* This file is mostly in anticipation of future work, but initially
50 * populate with fragment tracking for an outbound message.
51 */
52
53/* Initialize datamsg from memory. */
54static void sctp_datamsg_init(struct sctp_datamsg *msg)
55{
56 atomic_set(&msg->refcnt, 1);
57 msg->send_failed = 0;
58 msg->send_error = 0;
59 msg->can_abandon = 0;
60 msg->expires_at = 0;
61 INIT_LIST_HEAD(&msg->chunks);
62}
63
64/* Allocate and initialize datamsg. */
dd0fc66f 65SCTP_STATIC struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
1da177e4
LT
66{
67 struct sctp_datamsg *msg;
68 msg = kmalloc(sizeof(struct sctp_datamsg), gfp);
e8c38751 69 if (msg) {
1da177e4 70 sctp_datamsg_init(msg);
e8c38751
LZ
71 SCTP_DBG_OBJCNT_INC(datamsg);
72 }
1da177e4
LT
73 return msg;
74}
75
9c5c62be
VY
76void sctp_datamsg_free(struct sctp_datamsg *msg)
77{
78 struct sctp_chunk *chunk;
79
80 /* This doesn't have to be a _safe vairant because
81 * sctp_chunk_free() only drops the refs.
82 */
83 list_for_each_entry(chunk, &msg->chunks, frag_list)
84 sctp_chunk_free(chunk);
85
86 sctp_datamsg_put(msg);
87}
88
1da177e4
LT
89/* Final destructruction of datamsg memory. */
90static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
91{
92 struct list_head *pos, *temp;
93 struct sctp_chunk *chunk;
94 struct sctp_sock *sp;
95 struct sctp_ulpevent *ev;
96 struct sctp_association *asoc = NULL;
97 int error = 0, notify;
98
99 /* If we failed, we may need to notify. */
100 notify = msg->send_failed ? -1 : 0;
101
102 /* Release all references. */
103 list_for_each_safe(pos, temp, &msg->chunks) {
104 list_del_init(pos);
105 chunk = list_entry(pos, struct sctp_chunk, frag_list);
106 /* Check whether we _really_ need to notify. */
107 if (notify < 0) {
108 asoc = chunk->asoc;
109 if (msg->send_error)
110 error = msg->send_error;
111 else
112 error = asoc->outqueue.error;
113
114 sp = sctp_sk(asoc->base.sk);
115 notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED,
116 &sp->subscribe);
117 }
118
119 /* Generate a SEND FAILED event only if enabled. */
120 if (notify > 0) {
121 int sent;
122 if (chunk->has_tsn)
123 sent = SCTP_DATA_SENT;
124 else
125 sent = SCTP_DATA_UNSENT;
126
127 ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
128 error, GFP_ATOMIC);
129 if (ev)
130 sctp_ulpq_tail_event(&asoc->ulpq, ev);
131 }
132
133 sctp_chunk_put(chunk);
134 }
135
136 SCTP_DBG_OBJCNT_DEC(datamsg);
137 kfree(msg);
138}
139
140/* Hold a reference. */
141static void sctp_datamsg_hold(struct sctp_datamsg *msg)
142{
143 atomic_inc(&msg->refcnt);
144}
145
146/* Release a reference. */
147void sctp_datamsg_put(struct sctp_datamsg *msg)
148{
149 if (atomic_dec_and_test(&msg->refcnt))
150 sctp_datamsg_destroy(msg);
151}
152
1da177e4
LT
153/* Assign a chunk to this datamsg. */
154static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
155{
156 sctp_datamsg_hold(msg);
157 chunk->msg = msg;
158}
159
160
161/* A data chunk can have a maximum payload of (2^16 - 20). Break
162 * down any such message into smaller chunks. Opportunistically, fragment
163 * the chunks down to the current MTU constraints. We may get refragmented
164 * later if the PMTU changes, but it is _much better_ to fragment immediately
165 * with a reasonable guess than always doing our fragmentation on the
166 * soft-interrupt.
167 */
168struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
169 struct sctp_sndrcvinfo *sinfo,
170 struct msghdr *msgh, int msg_len)
171{
172 int max, whole, i, offset, over, err;
173 int len, first_len;
3e62abf9 174 int max_data;
1da177e4
LT
175 struct sctp_chunk *chunk;
176 struct sctp_datamsg *msg;
177 struct list_head *pos, *temp;
178 __u8 frag;
179
180 msg = sctp_datamsg_new(GFP_KERNEL);
181 if (!msg)
182 return NULL;
183
184 /* Note: Calculate this outside of the loop, so that all fragments
185 * have the same expiration.
186 */
187 if (sinfo->sinfo_timetolive) {
188 /* sinfo_timetolive is in milliseconds */
189 msg->expires_at = jiffies +
190 msecs_to_jiffies(sinfo->sinfo_timetolive);
191 msg->can_abandon = 1;
192 SCTP_DEBUG_PRINTK("%s: msg:%p expires_at: %ld jiffies:%ld\n",
0dc47877 193 __func__, msg, msg->expires_at, jiffies);
1da177e4
LT
194 }
195
3e62abf9
VY
196 /* This is the biggest possible DATA chunk that can fit into
197 * the packet
198 */
199 max_data = asoc->pathmtu -
200 sctp_sk(asoc->base.sk)->pf->af->net_header_len -
201 sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk);
1da177e4 202
3e62abf9 203 max = asoc->frag_point;
4cd57c80
VY
204 /* If the the peer requested that we authenticate DATA chunks
205 * we need to accound for bundling of the AUTH chunks along with
206 * DATA.
207 */
208 if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
209 struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
210
211 if (hmac_desc)
3e62abf9 212 max_data -= WORD_ROUND(sizeof(sctp_auth_chunk_t) +
4cd57c80
VY
213 hmac_desc->hmac_len);
214 }
215
3e62abf9
VY
216 /* Now, check if we need to reduce our max */
217 if (max > max_data)
218 max = max_data;
219
1da177e4
LT
220 whole = 0;
221 first_len = max;
222
5d7ff261
VY
223 /* Check to see if we have a pending SACK and try to let it be bundled
224 * with this message. Do this if we don't have any data queued already.
225 * To check that, look at out_qlen and retransmit list.
226 * NOTE: we will not reduce to account for SACK, if the message would
227 * not have been fragmented.
228 */
229 if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
230 asoc->outqueue.out_qlen == 0 &&
231 list_empty(&asoc->outqueue.retransmit) &&
232 msg_len > max)
233 max_data -= WORD_ROUND(sizeof(sctp_sack_chunk_t));
234
1da177e4 235 /* Encourage Cookie-ECHO bundling. */
5d7ff261 236 if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
3e62abf9 237 max_data -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
1da177e4 238
5d7ff261
VY
239 /* Now that we adjusted completely, reset first_len */
240 if (first_len > max_data)
241 first_len = max_data;
3e62abf9
VY
242
243 /* Account for a different sized first fragment */
244 if (msg_len >= first_len) {
245 msg_len -= first_len;
246 whole = 1;
1da177e4
LT
247 }
248
249 /* How many full sized? How many bytes leftover? */
250 whole += msg_len / max;
251 over = msg_len % max;
252 offset = 0;
253
254 if ((whole > 1) || (whole && over))
255 SCTP_INC_STATS_USER(SCTP_MIB_FRAGUSRMSGS);
256
257 /* Create chunks for all the full sized DATA chunks. */
258 for (i=0, len=first_len; i < whole; i++) {
259 frag = SCTP_DATA_MIDDLE_FRAG;
260
261 if (0 == i)
262 frag |= SCTP_DATA_FIRST_FRAG;
263
264 if ((i == (whole - 1)) && !over)
265 frag |= SCTP_DATA_LAST_FRAG;
266
267 chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0);
268
269 if (!chunk)
270 goto errout;
271 err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov);
272 if (err < 0)
273 goto errout;
274
275 offset += len;
276
277 /* Put the chunk->skb back into the form expected by send. */
278 __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
279 - (__u8 *)chunk->skb->data);
280
281 sctp_datamsg_assign(msg, chunk);
282 list_add_tail(&chunk->frag_list, &msg->chunks);
283
284 /* The first chunk, the first chunk was likely short
285 * to allow bundling, so reset to full size.
286 */
287 if (0 == i)
288 len = max;
289 }
290
291 /* .. now the leftover bytes. */
292 if (over) {
293 if (!whole)
294 frag = SCTP_DATA_NOT_FRAG;
295 else
296 frag = SCTP_DATA_LAST_FRAG;
297
298 chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0);
299
300 if (!chunk)
301 goto errout;
302
303 err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov);
304
305 /* Put the chunk->skb back into the form expected by send. */
306 __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
307 - (__u8 *)chunk->skb->data);
308 if (err < 0)
309 goto errout;
310
311 sctp_datamsg_assign(msg, chunk);
312 list_add_tail(&chunk->frag_list, &msg->chunks);
313 }
314
315 return msg;
316
317errout:
318 list_for_each_safe(pos, temp, &msg->chunks) {
319 list_del_init(pos);
320 chunk = list_entry(pos, struct sctp_chunk, frag_list);
321 sctp_chunk_free(chunk);
322 }
80445cfb 323 sctp_datamsg_put(msg);
1da177e4
LT
324 return NULL;
325}
326
327/* Check whether this message has expired. */
328int sctp_chunk_abandoned(struct sctp_chunk *chunk)
329{
330 struct sctp_datamsg *msg = chunk->msg;
331
332 if (!msg->can_abandon)
333 return 0;
334
335 if (time_after(jiffies, msg->expires_at))
336 return 1;
337
338 return 0;
339}
340
341/* This chunk (and consequently entire message) has failed in its sending. */
342void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
343{
344 chunk->msg->send_failed = 1;
345 chunk->msg->send_error = error;
346}