Skip to content

Commit 0c3f6f6

Browse files
lxindavem330
authored andcommitted
sctp: implement make_datafrag for sctp_stream_interleave
To avoid hundreds of checks for the different process on I-DATA chunk, struct sctp_stream_interleave is defined as a group of functions used to replace the codes in some place where it needs to do different job according to if the asoc intl_enabled is set. With these ops, it only needs to initialize asoc->stream.si with sctp_stream_interleave_0 for normal data if asoc intl_enable is 0, or sctp_stream_interleave_1 for idata if asoc intl_enable is set in sctp_stream_init. After that, the members in asoc->stream.si can be used directly in some special places without checking asoc intl_enable. make_datafrag is the first member for sctp_stream_interleave, it's used to make data or idata frags, called in sctp_datamsg_from_user. The old function sctp_make_datafrag_empty needs to be adjust some to fit in this ops. Note that as idata and data chunks have different length, it also defines data_chunk_len for sctp_stream_interleave to describe the chunk size. Signed-off-by: Xin Long <[email protected]> Acked-by: Marcelo Ricardo Leitner <[email protected]> Acked-by: Neil Horman <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ad05a7a commit 0c3f6f6

File tree

8 files changed

+149
-21
lines changed

8 files changed

+149
-21
lines changed

include/net/sctp/sm.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,9 @@ struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
199199
const struct sctp_chunk *chunk);
200200
struct sctp_chunk *sctp_make_idata(const struct sctp_association *asoc,
201201
__u8 flags, int paylen, gfp_t gfp);
202-
struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
202+
struct sctp_chunk *sctp_make_datafrag_empty(const struct sctp_association *asoc,
203203
const struct sctp_sndrcvinfo *sinfo,
204-
int len, const __u8 flags,
205-
__u16 ssn, gfp_t gfp);
204+
int len, __u8 flags, gfp_t gfp);
206205
struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
207206
const __u32 lowest_tsn);
208207
struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc);

include/net/sctp/stream_interleave.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* SCTP kernel implementation
2+
* (C) Copyright Red Hat Inc. 2017
3+
*
4+
* These are definitions used by the stream schedulers, defined in RFC
5+
* draft ndata (https://tools.ietf.org/html/draft-ietf-tsvwg-sctp-ndata-11)
6+
*
7+
* This SCTP implementation is free software;
8+
* you can redistribute it and/or modify it under the terms of
9+
* the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2, or (at your option)
11+
* any later version.
12+
*
13+
* This SCTP implementation is distributed in the hope that it
14+
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
15+
* ************************
16+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17+
* See the GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with GNU CC; see the file COPYING. If not, see
21+
* <http://www.gnu.org/licenses/>.
22+
*
23+
* Please send any bug reports or fixes you make to the
24+
* email addresses:
25+
* lksctp developers <[email protected]>
26+
*
27+
* Written or modified by:
28+
* Xin Long <[email protected]>
29+
*/
30+
31+
#ifndef __sctp_stream_interleave_h__
32+
#define __sctp_stream_interleave_h__
33+
34+
struct sctp_stream_interleave {
35+
__u16 data_chunk_len;
36+
/* (I-)DATA process */
37+
struct sctp_chunk *(*make_datafrag)(const struct sctp_association *asoc,
38+
const struct sctp_sndrcvinfo *sinfo,
39+
int len, __u8 flags, gfp_t gfp);
40+
};
41+
42+
void sctp_stream_interleave_init(struct sctp_stream *stream);
43+
44+
#endif /* __sctp_stream_interleave_h__ */

include/net/sctp/structs.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct sctp_stream;
8989
#include <net/sctp/tsnmap.h>
9090
#include <net/sctp/ulpevent.h>
9191
#include <net/sctp/ulpqueue.h>
92+
#include <net/sctp/stream_interleave.h>
9293

9394
/* Structures useful for managing bind/connect. */
9495

@@ -1389,11 +1390,22 @@ struct sctp_stream {
13891390
struct sctp_stream_out_ext *rr_next;
13901391
};
13911392
};
1393+
struct sctp_stream_interleave *si;
13921394
};
13931395

13941396
#define SCTP_STREAM_CLOSED 0x00
13951397
#define SCTP_STREAM_OPEN 0x01
13961398

1399+
static inline __u16 sctp_datachk_len(const struct sctp_stream *stream)
1400+
{
1401+
return stream->si->data_chunk_len;
1402+
}
1403+
1404+
static inline __u16 sctp_datahdr_len(const struct sctp_stream *stream)
1405+
{
1406+
return stream->si->data_chunk_len - sizeof(struct sctp_chunkhdr);
1407+
}
1408+
13971409
/* SCTP_GET_ASSOC_STATS counters */
13981410
struct sctp_priv_assoc_stats {
13991411
/* Maximum observed rto in the association during subsequent

net/sctp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ sctp-y := sm_statetable.o sm_statefuns.o sm_sideeffect.o \
1414
tsnmap.o bind_addr.o socket.o primitive.o \
1515
output.o input.o debug.o stream.o auth.o \
1616
offload.o stream_sched.o stream_sched_prio.o \
17-
stream_sched_rr.o
17+
stream_sched_rr.o stream_interleave.o
1818

1919
sctp_probe-y := probe.o
2020

net/sctp/chunk.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
191191
*/
192192
max_data = asoc->pathmtu -
193193
sctp_sk(asoc->base.sk)->pf->af->net_header_len -
194-
sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk);
194+
sizeof(struct sctphdr) - sctp_datachk_len(&asoc->stream);
195195
max_data = SCTP_TRUNC4(max_data);
196196

197197
/* If the the peer requested that we authenticate DATA chunks
@@ -264,8 +264,8 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
264264
frag |= SCTP_DATA_SACK_IMM;
265265
}
266266

267-
chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag,
268-
0, GFP_KERNEL);
267+
chunk = asoc->stream.si->make_datafrag(asoc, sinfo, len, frag,
268+
GFP_KERNEL);
269269
if (!chunk) {
270270
err = -ENOMEM;
271271
goto errout;

net/sctp/sm_make_chunk.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -721,38 +721,31 @@ struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
721721
/* Make a DATA chunk for the given association from the provided
722722
* parameters. However, do not populate the data payload.
723723
*/
724-
struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
724+
struct sctp_chunk *sctp_make_datafrag_empty(const struct sctp_association *asoc,
725725
const struct sctp_sndrcvinfo *sinfo,
726-
int data_len, __u8 flags, __u16 ssn,
727-
gfp_t gfp)
726+
int len, __u8 flags, gfp_t gfp)
728727
{
729728
struct sctp_chunk *retval;
730729
struct sctp_datahdr dp;
731-
int chunk_len;
732730

733731
/* We assign the TSN as LATE as possible, not here when
734732
* creating the chunk.
735733
*/
736-
dp.tsn = 0;
734+
memset(&dp, 0, sizeof(dp));
735+
dp.ppid = sinfo->sinfo_ppid;
737736
dp.stream = htons(sinfo->sinfo_stream);
738-
dp.ppid = sinfo->sinfo_ppid;
739737

740738
/* Set the flags for an unordered send. */
741-
if (sinfo->sinfo_flags & SCTP_UNORDERED) {
739+
if (sinfo->sinfo_flags & SCTP_UNORDERED)
742740
flags |= SCTP_DATA_UNORDERED;
743-
dp.ssn = 0;
744-
} else
745-
dp.ssn = htons(ssn);
746741

747-
chunk_len = sizeof(dp) + data_len;
748-
retval = sctp_make_data(asoc, flags, chunk_len, gfp);
742+
retval = sctp_make_data(asoc, flags, sizeof(dp) + len, gfp);
749743
if (!retval)
750-
goto nodata;
744+
return NULL;
751745

752746
retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
753747
memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
754748

755-
nodata:
756749
return retval;
757750
}
758751

net/sctp/stream.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt,
167167
sched->init(stream);
168168

169169
in:
170+
sctp_stream_interleave_init(stream);
170171
if (!incnt)
171172
goto out;
172173

net/sctp/stream_interleave.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* SCTP kernel implementation
2+
* (C) Copyright Red Hat Inc. 2017
3+
*
4+
* This file is part of the SCTP kernel implementation
5+
*
6+
* These functions manipulate sctp stream queue/scheduling.
7+
*
8+
* This SCTP 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+
* This SCTP 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, see
22+
* <http://www.gnu.org/licenses/>.
23+
*
24+
* Please send any bug reports or fixes you make to the
25+
* email addresched(es):
26+
* lksctp developers <[email protected]>
27+
*
28+
* Written or modified by:
29+
* Xin Long <[email protected]>
30+
*/
31+
32+
#include <net/sctp/sctp.h>
33+
#include <net/sctp/sm.h>
34+
#include <linux/sctp.h>
35+
36+
static struct sctp_chunk *sctp_make_idatafrag_empty(
37+
const struct sctp_association *asoc,
38+
const struct sctp_sndrcvinfo *sinfo,
39+
int len, __u8 flags, gfp_t gfp)
40+
{
41+
struct sctp_chunk *retval;
42+
struct sctp_idatahdr dp;
43+
44+
memset(&dp, 0, sizeof(dp));
45+
dp.stream = htons(sinfo->sinfo_stream);
46+
47+
if (sinfo->sinfo_flags & SCTP_UNORDERED)
48+
flags |= SCTP_DATA_UNORDERED;
49+
50+
retval = sctp_make_idata(asoc, flags, sizeof(dp) + len, gfp);
51+
if (!retval)
52+
return NULL;
53+
54+
retval->subh.idata_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
55+
memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
56+
57+
return retval;
58+
}
59+
60+
static struct sctp_stream_interleave sctp_stream_interleave_0 = {
61+
.data_chunk_len = sizeof(struct sctp_data_chunk),
62+
/* DATA process functions */
63+
.make_datafrag = sctp_make_datafrag_empty,
64+
};
65+
66+
static struct sctp_stream_interleave sctp_stream_interleave_1 = {
67+
.data_chunk_len = sizeof(struct sctp_idata_chunk),
68+
/* I-DATA process functions */
69+
.make_datafrag = sctp_make_idatafrag_empty,
70+
};
71+
72+
void sctp_stream_interleave_init(struct sctp_stream *stream)
73+
{
74+
struct sctp_association *asoc;
75+
76+
asoc = container_of(stream, struct sctp_association, stream);
77+
stream->si = asoc->intl_enable ? &sctp_stream_interleave_1
78+
: &sctp_stream_interleave_0;
79+
}

0 commit comments

Comments
 (0)