Skip to content

Commit bbb0302

Browse files
Tom Herbertdavem330
authored andcommitted
strparser: Generalize strparser
Generalize strparser from more than just being used in conjunction with read_sock. strparser will also be used in the send path with zero proxy. The primary change is to create strp_process function that performs the critical processing on skbs. The documentation is also updated to reflect the new uses. Signed-off-by: Tom Herbert <tom@quantonium.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 20bf50d commit bbb0302

5 files changed

Lines changed: 424 additions & 287 deletions

File tree

‎Documentation/networking/strparser.txt‎

Lines changed: 139 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,107 @@
1-
Stream Parser
2-
-------------
1+
Stream Parser (strparser)
2+
3+
Introduction
4+
============
35

46
The stream parser (strparser) is a utility that parses messages of an
5-
application layer protocol running over a TCP connection. The stream
7+
application layer protocol running over a data stream. The stream
68
parser works in conjunction with an upper layer in the kernel to provide
79
kernel support for application layer messages. For instance, Kernel
810
Connection Multiplexor (KCM) uses the Stream Parser to parse messages
911
using a BPF program.
1012

13+
The strparser works in one of two modes: receive callback or general
14+
mode.
15+
16+
In receive callback mode, the strparser is called from the data_ready
17+
callback of a TCP socket. Messages are parsed and delivered as they are
18+
received on the socket.
19+
20+
In general mode, a sequence of skbs are fed to strparser from an
21+
outside source. Message are parsed and delivered as the sequence is
22+
processed. This modes allows strparser to be applied to arbitrary
23+
streams of data.
24+
1125
Interface
12-
---------
26+
=========
1327

1428
The API includes a context structure, a set of callbacks, utility
15-
functions, and a data_ready function. The callbacks include
16-
a parse_msg function that is called to perform parsing (e.g.
17-
BPF parsing in case of KCM), and a rcv_msg function that is called
18-
when a full message has been completed.
29+
functions, and a data_ready function for receive callback mode. The
30+
callbacks include a parse_msg function that is called to perform
31+
parsing (e.g. BPF parsing in case of KCM), and a rcv_msg function
32+
that is called when a full message has been completed.
1933

20-
A stream parser can be instantiated for a TCP connection. This is done
21-
by:
34+
Functions
35+
=========
2236

23-
strp_init(struct strparser *strp, struct sock *csk,
37+
strp_init(struct strparser *strp, struct sock *sk,
2438
struct strp_callbacks *cb)
2539

26-
strp is a struct of type strparser that is allocated by the upper layer.
27-
csk is the TCP socket associated with the stream parser. Callbacks are
28-
called by the stream parser.
40+
Called to initialize a stream parser. strp is a struct of type
41+
strparser that is allocated by the upper layer. sk is the TCP
42+
socket associated with the stream parser for use with receive
43+
callback mode; in general mode this is set to NULL. Callbacks
44+
are called by the stream parser (the callbacks are listed below).
45+
46+
void strp_pause(struct strparser *strp)
47+
48+
Temporarily pause a stream parser. Message parsing is suspended
49+
and no new messages are delivered to the upper layer.
50+
51+
void strp_pause(struct strparser *strp)
52+
53+
Unpause a paused stream parser.
54+
55+
void strp_stop(struct strparser *strp);
56+
57+
strp_stop is called to completely stop stream parser operations.
58+
This is called internally when the stream parser encounters an
59+
error, and it is called from the upper layer to stop parsing
60+
operations.
61+
62+
void strp_done(struct strparser *strp);
63+
64+
strp_done is called to release any resources held by the stream
65+
parser instance. This must be called after the stream processor
66+
has been stopped.
67+
68+
int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
69+
unsigned int orig_offset, size_t orig_len,
70+
size_t max_msg_size, long timeo)
71+
72+
strp_process is called in general mode for a stream parser to
73+
parse an sk_buff. The number of bytes processed or a negative
74+
error number is returned. Note that strp_process does not
75+
consume the sk_buff. max_msg_size is maximum size the stream
76+
parser will parse. timeo is timeout for completing a message.
77+
78+
void strp_data_ready(struct strparser *strp);
79+
80+
The upper layer calls strp_tcp_data_ready when data is ready on
81+
the lower socket for strparser to process. This should be called
82+
from a data_ready callback that is set on the socket. Note that
83+
maximum messages size is the limit of the receive socket
84+
buffer and message timeout is the receive timeout for the socket.
85+
86+
void strp_check_rcv(struct strparser *strp);
87+
88+
strp_check_rcv is called to check for new messages on the socket.
89+
This is normally called at initialization of a stream parser
90+
instance or after strp_unpause.
2991

3092
Callbacks
31-
---------
93+
=========
3294

33-
There are four callbacks:
95+
There are six callbacks:
3496

3597
int (*parse_msg)(struct strparser *strp, struct sk_buff *skb);
3698

3799
parse_msg is called to determine the length of the next message
38100
in the stream. The upper layer must implement this function. It
39101
should parse the sk_buff as containing the headers for the
40-
next application layer messages in the stream.
102+
next application layer message in the stream.
41103

42-
The skb->cb in the input skb is a struct strp_rx_msg. Only
104+
The skb->cb in the input skb is a struct strp_msg. Only
43105
the offset field is relevant in parse_msg and gives the offset
44106
where the message starts in the skb.
45107

@@ -50,26 +112,41 @@ int (*parse_msg)(struct strparser *strp, struct sk_buff *skb);
50112
-ESTRPIPE : current message should not be processed by the
51113
kernel, return control of the socket to userspace which
52114
can proceed to read the messages itself
53-
other < 0 : Error is parsing, give control back to userspace
115+
other < 0 : Error in parsing, give control back to userspace
54116
assuming that synchronization is lost and the stream
55117
is unrecoverable (application expected to close TCP socket)
56118

57119
In the case that an error is returned (return value is less than
58-
zero) the stream parser will set the error on TCP socket and wake
59-
it up. If parse_msg returned -ESTRPIPE and the stream parser had
60-
previously read some bytes for the current message, then the error
61-
set on the attached socket is ENODATA since the stream is
62-
unrecoverable in that case.
120+
zero) and the parser is in receive callback mode, then it will set
121+
the error on TCP socket and wake it up. If parse_msg returned
122+
-ESTRPIPE and the stream parser had previously read some bytes for
123+
the current message, then the error set on the attached socket is
124+
ENODATA since the stream is unrecoverable in that case.
125+
126+
void (*lock)(struct strparser *strp)
127+
128+
The lock callback is called to lock the strp structure when
129+
the strparser is performing an asynchronous operation (such as
130+
processing a timeout). In receive callback mode the default
131+
function is to lock_sock for the associated socket. In general
132+
mode the callback must be set appropriately.
133+
134+
void (*unlock)(struct strparser *strp)
135+
136+
The unlock callback is called to release the lock obtained
137+
by the lock callback. In receive callback mode the default
138+
function is release_sock for the associated socket. In general
139+
mode the callback must be set appropriately.
63140

64141
void (*rcv_msg)(struct strparser *strp, struct sk_buff *skb);
65142

66143
rcv_msg is called when a full message has been received and
67144
is queued. The callee must consume the sk_buff; it can
68145
call strp_pause to prevent any further messages from being
69-
received in rcv_msg (see strp_pause below). This callback
146+
received in rcv_msg (see strp_pause above). This callback
70147
must be set.
71148

72-
The skb->cb in the input skb is a struct strp_rx_msg. This
149+
The skb->cb in the input skb is a struct strp_msg. This
73150
struct contains two fields: offset and full_len. Offset is
74151
where the message starts in the skb, and full_len is the
75152
the length of the message. skb->len - offset may be greater
@@ -78,59 +155,53 @@ void (*rcv_msg)(struct strparser *strp, struct sk_buff *skb);
78155
int (*read_sock_done)(struct strparser *strp, int err);
79156

80157
read_sock_done is called when the stream parser is done reading
81-
the TCP socket. The stream parser may read multiple messages
82-
in a loop and this function allows cleanup to occur when existing
83-
the loop. If the callback is not set (NULL in strp_init) a
84-
default function is used.
158+
the TCP socket in receive callback mode. The stream parser may
159+
read multiple messages in a loop and this function allows cleanup
160+
to occur when exiting the loop. If the callback is not set (NULL
161+
in strp_init) a default function is used.
85162

86163
void (*abort_parser)(struct strparser *strp, int err);
87164

88165
This function is called when stream parser encounters an error
89-
in parsing. The default function stops the stream parser for the
90-
TCP socket and sets the error in the socket. The default function
91-
can be changed by setting the callback to non-NULL in strp_init.
166+
in parsing. The default function stops the stream parser and
167+
sets the error in the socket if the parser is in receive callback
168+
mode. The default function can be changed by setting the callback
169+
to non-NULL in strp_init.
92170

93-
Functions
94-
---------
171+
Statistics
172+
==========
95173

96-
The upper layer calls strp_tcp_data_ready when data is ready on the lower
97-
socket for strparser to process. This should be called from a data_ready
98-
callback that is set on the socket.
174+
Various counters are kept for each stream parser instance. These are in
175+
the strp_stats structure. strp_aggr_stats is a convenience structure for
176+
accumulating statistics for multiple stream parser instances.
177+
save_strp_stats and aggregate_strp_stats are helper functions to save
178+
and aggregate statistics.
99179

100-
strp_stop is called to completely stop stream parser operations. This
101-
is called internally when the stream parser encounters an error, and
102-
it is called from the upper layer when unattaching a TCP socket.
180+
Message assembly limits
181+
=======================
103182

104-
strp_done is called to unattach the stream parser from the TCP socket.
105-
This must be called after the stream processor has be stopped.
183+
The stream parser provide mechanisms to limit the resources consumed by
184+
message assembly.
106185

107-
strp_check_rcv is called to check for new messages on the socket. This
108-
is normally called at initialization of the a stream parser instance
109-
of after strp_unpause.
186+
A timer is set when assembly starts for a new message. In receive
187+
callback mode the message timeout is taken from rcvtime for the
188+
associated TCP socket. In general mode, the timeout is passed as an
189+
argument in strp_process. If the timer fires before assembly completes
190+
the stream parser is aborted and the ETIMEDOUT error is set on the TCP
191+
socket if in receive callback mode.
110192

111-
Statistics
112-
----------
193+
In receive callback mode, message length is limited to the receive
194+
buffer size of the associated TCP socket. If the length returned by
195+
parse_msg is greater than the socket buffer size then the stream parser
196+
is aborted with EMSGSIZE error set on the TCP socket. Note that this
197+
makes the maximum size of receive skbuffs for a socket with a stream
198+
parser to be 2*sk_rcvbuf of the TCP socket.
113199

114-
Various counters are kept for each stream parser for a TCP socket.
115-
These are in the strp_stats structure. strp_aggr_stats is a convenience
116-
structure for accumulating statistics for multiple stream parser
117-
instances. save_strp_stats and aggregate_strp_stats are helper functions
118-
to save and aggregate statistics.
200+
In general mode the message length limit is passed in as an argument
201+
to strp_process.
119202

120-
Message assembly limits
121-
-----------------------
203+
Author
204+
======
122205

123-
The stream parser provide mechanisms to limit the resources consumed by
124-
message assembly.
206+
Tom Herbert (tom@quantonium.net)
125207

126-
A timer is set when assembly starts for a new message. The message
127-
timeout is taken from rcvtime for the associated TCP socket. If the
128-
timer fires before assembly completes the stream parser is aborted
129-
and the ETIMEDOUT error is set on the TCP socket.
130-
131-
Message length is limited to the receive buffer size of the associated
132-
TCP socket. If the length returned by parse_msg is greater than
133-
the socket buffer size then the stream parser is aborted with
134-
EMSGSIZE error set on the TCP socket. Note that this makes the
135-
maximum size of receive skbuffs for a socket with a stream parser
136-
to be 2*sk_rcvbuf of the TCP socket.

0 commit comments

Comments
 (0)