Skip to content

Commit bfd2e4b

Browse files
marceloleitnerdavem330
authored andcommitted
sctp: refactor sctp_datamsg_from_user
This patch refactors sctp_datamsg_from_user() in an attempt to make it better to read and avoid code duplication for handling the last fragment. It also avoids doing division and remaining operations. Even though, it should still operate similarly as before this patch. Signed-off-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 5e45853 commit bfd2e4b

1 file changed

Lines changed: 32 additions & 75 deletions

File tree

‎net/sctp/chunk.c‎

Lines changed: 32 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,12 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
165165
struct sctp_sndrcvinfo *sinfo,
166166
struct iov_iter *from)
167167
{
168-
int max, whole, i, offset, over, err;
169-
int len, first_len;
170-
int max_data;
168+
size_t len, first_len, max_data, remaining;
169+
size_t msg_len = iov_iter_count(from);
170+
struct list_head *pos, *temp;
171171
struct sctp_chunk *chunk;
172172
struct sctp_datamsg *msg;
173-
struct list_head *pos, *temp;
174-
size_t msg_len = iov_iter_count(from);
175-
__u8 frag;
173+
int err;
176174

177175
msg = sctp_datamsg_new(GFP_KERNEL);
178176
if (!msg)
@@ -185,7 +183,7 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
185183
(SCTP_PR_TTL_ENABLED(sinfo->sinfo_flags) ||
186184
!SCTP_PR_POLICY(sinfo->sinfo_flags)))
187185
msg->expires_at = jiffies +
188-
msecs_to_jiffies(sinfo->sinfo_timetolive);
186+
msecs_to_jiffies(sinfo->sinfo_timetolive);
189187

190188
/* This is the biggest possible DATA chunk that can fit into
191189
* the packet
@@ -195,7 +193,6 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
195193
sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk);
196194
max_data = SCTP_TRUNC4(max_data);
197195

198-
max = asoc->frag_point;
199196
/* If the the peer requested that we authenticate DATA chunks
200197
* we need to account for bundling of the AUTH chunks along with
201198
* DATA.
@@ -208,12 +205,11 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
208205
hmac_desc->hmac_len);
209206
}
210207

211-
/* Now, check if we need to reduce our max */
212-
if (max > max_data)
213-
max = max_data;
208+
/* Check what's our max considering the above */
209+
max_data = min_t(size_t, max_data, asoc->frag_point);
214210

215-
whole = 0;
216-
first_len = max;
211+
/* Set first_len and then account for possible bundles on first frag */
212+
first_len = max_data;
217213

218214
/* Check to see if we have a pending SACK and try to let it be bundled
219215
* with this message. Do this if we don't have any data queued already.
@@ -224,40 +220,38 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
224220
if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
225221
asoc->outqueue.out_qlen == 0 &&
226222
list_empty(&asoc->outqueue.retransmit) &&
227-
msg_len > max)
228-
max_data -= SCTP_PAD4(sizeof(sctp_sack_chunk_t));
223+
msg_len > max_data)
224+
first_len -= SCTP_PAD4(sizeof(sctp_sack_chunk_t));
229225

230226
/* Encourage Cookie-ECHO bundling. */
231227
if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
232-
max_data -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
233-
234-
/* Now that we adjusted completely, reset first_len */
235-
if (first_len > max_data)
236-
first_len = max_data;
228+
first_len -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
237229

238230
/* Account for a different sized first fragment */
239231
if (msg_len >= first_len) {
240-
msg_len -= first_len;
241-
whole = 1;
242232
msg->can_delay = 0;
243-
}
244-
245-
/* How many full sized? How many bytes leftover? */
246-
whole += msg_len / max;
247-
over = msg_len % max;
248-
offset = 0;
249-
250-
if ((whole > 1) || (whole && over))
251233
SCTP_INC_STATS(sock_net(asoc->base.sk), SCTP_MIB_FRAGUSRMSGS);
234+
} else {
235+
/* Which may be the only one... */
236+
first_len = msg_len;
237+
}
252238

253-
/* Create chunks for all the full sized DATA chunks. */
254-
for (i = 0, len = first_len; i < whole; i++) {
255-
frag = SCTP_DATA_MIDDLE_FRAG;
239+
/* Create chunks for all DATA chunks. */
240+
for (remaining = msg_len; remaining; remaining -= len) {
241+
u8 frag = SCTP_DATA_MIDDLE_FRAG;
256242

257-
if (0 == i)
243+
if (remaining == msg_len) {
244+
/* First frag, which may also be the last */
258245
frag |= SCTP_DATA_FIRST_FRAG;
246+
len = first_len;
247+
} else {
248+
/* Middle frags */
249+
len = max_data;
250+
}
259251

260-
if ((i == (whole - 1)) && !over) {
252+
if (len >= remaining) {
253+
/* Last frag, which may also be the first */
254+
len = remaining;
261255
frag |= SCTP_DATA_LAST_FRAG;
262256

263257
/* The application requests to set the I-bit of the
@@ -271,7 +265,6 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
271265

272266
chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag,
273267
0, GFP_KERNEL);
274-
275268
if (!chunk) {
276269
err = -ENOMEM;
277270
goto errout;
@@ -282,45 +275,8 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
282275
goto errout_chunk_free;
283276

284277
/* Put the chunk->skb back into the form expected by send. */
285-
__skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
286-
- (__u8 *)chunk->skb->data);
287-
288-
sctp_datamsg_assign(msg, chunk);
289-
list_add_tail(&chunk->frag_list, &msg->chunks);
290-
291-
/* The first chunk, the first chunk was likely short
292-
* to allow bundling, so reset to full size.
293-
*/
294-
if (0 == i)
295-
len = max;
296-
}
297-
298-
/* .. now the leftover bytes. */
299-
if (over) {
300-
if (!whole)
301-
frag = SCTP_DATA_NOT_FRAG;
302-
else
303-
frag = SCTP_DATA_LAST_FRAG;
304-
305-
if ((sinfo->sinfo_flags & SCTP_EOF) ||
306-
(sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
307-
frag |= SCTP_DATA_SACK_IMM;
308-
309-
chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag,
310-
0, GFP_KERNEL);
311-
312-
if (!chunk) {
313-
err = -ENOMEM;
314-
goto errout;
315-
}
316-
317-
err = sctp_user_addto_chunk(chunk, over, from);
318-
319-
/* Put the chunk->skb back into the form expected by send. */
320-
__skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
321-
- (__u8 *)chunk->skb->data);
322-
if (err < 0)
323-
goto errout_chunk_free;
278+
__skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr -
279+
chunk->skb->data);
324280

325281
sctp_datamsg_assign(msg, chunk);
326282
list_add_tail(&chunk->frag_list, &msg->chunks);
@@ -338,6 +294,7 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
338294
sctp_chunk_free(chunk);
339295
}
340296
sctp_datamsg_put(msg);
297+
341298
return ERR_PTR(err);
342299
}
343300

0 commit comments

Comments
 (0)