xref: /OK3568_Linux_fs/kernel/Documentation/networking/strparser.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun=========================
4*4882a593SmuzhiyunStream Parser (strparser)
5*4882a593Smuzhiyun=========================
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunIntroduction
8*4882a593Smuzhiyun============
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunThe stream parser (strparser) is a utility that parses messages of an
11*4882a593Smuzhiyunapplication layer protocol running over a data stream. The stream
12*4882a593Smuzhiyunparser works in conjunction with an upper layer in the kernel to provide
13*4882a593Smuzhiyunkernel support for application layer messages. For instance, Kernel
14*4882a593SmuzhiyunConnection Multiplexor (KCM) uses the Stream Parser to parse messages
15*4882a593Smuzhiyunusing a BPF program.
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunThe strparser works in one of two modes: receive callback or general
18*4882a593Smuzhiyunmode.
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunIn receive callback mode, the strparser is called from the data_ready
21*4882a593Smuzhiyuncallback of a TCP socket. Messages are parsed and delivered as they are
22*4882a593Smuzhiyunreceived on the socket.
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunIn general mode, a sequence of skbs are fed to strparser from an
25*4882a593Smuzhiyunoutside source. Message are parsed and delivered as the sequence is
26*4882a593Smuzhiyunprocessed. This modes allows strparser to be applied to arbitrary
27*4882a593Smuzhiyunstreams of data.
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunInterface
30*4882a593Smuzhiyun=========
31*4882a593Smuzhiyun
32*4882a593SmuzhiyunThe API includes a context structure, a set of callbacks, utility
33*4882a593Smuzhiyunfunctions, and a data_ready function for receive callback mode. The
34*4882a593Smuzhiyuncallbacks include a parse_msg function that is called to perform
35*4882a593Smuzhiyunparsing (e.g.  BPF parsing in case of KCM), and a rcv_msg function
36*4882a593Smuzhiyunthat is called when a full message has been completed.
37*4882a593Smuzhiyun
38*4882a593SmuzhiyunFunctions
39*4882a593Smuzhiyun=========
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun     ::
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun	strp_init(struct strparser *strp, struct sock *sk,
44*4882a593Smuzhiyun		const struct strp_callbacks *cb)
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun     Called to initialize a stream parser. strp is a struct of type
47*4882a593Smuzhiyun     strparser that is allocated by the upper layer. sk is the TCP
48*4882a593Smuzhiyun     socket associated with the stream parser for use with receive
49*4882a593Smuzhiyun     callback mode; in general mode this is set to NULL. Callbacks
50*4882a593Smuzhiyun     are called by the stream parser (the callbacks are listed below).
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun     ::
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun	void strp_pause(struct strparser *strp)
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun     Temporarily pause a stream parser. Message parsing is suspended
57*4882a593Smuzhiyun     and no new messages are delivered to the upper layer.
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun     ::
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun	void strp_unpause(struct strparser *strp)
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun     Unpause a paused stream parser.
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun     ::
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun	void strp_stop(struct strparser *strp);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun     strp_stop is called to completely stop stream parser operations.
70*4882a593Smuzhiyun     This is called internally when the stream parser encounters an
71*4882a593Smuzhiyun     error, and it is called from the upper layer to stop parsing
72*4882a593Smuzhiyun     operations.
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun     ::
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun	void strp_done(struct strparser *strp);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun     strp_done is called to release any resources held by the stream
79*4882a593Smuzhiyun     parser instance. This must be called after the stream processor
80*4882a593Smuzhiyun     has been stopped.
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun     ::
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun	int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
85*4882a593Smuzhiyun			 unsigned int orig_offset, size_t orig_len,
86*4882a593Smuzhiyun			 size_t max_msg_size, long timeo)
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun    strp_process is called in general mode for a stream parser to
89*4882a593Smuzhiyun    parse an sk_buff. The number of bytes processed or a negative
90*4882a593Smuzhiyun    error number is returned. Note that strp_process does not
91*4882a593Smuzhiyun    consume the sk_buff. max_msg_size is maximum size the stream
92*4882a593Smuzhiyun    parser will parse. timeo is timeout for completing a message.
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun    ::
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun	void strp_data_ready(struct strparser *strp);
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun    The upper layer calls strp_tcp_data_ready when data is ready on
99*4882a593Smuzhiyun    the lower socket for strparser to process. This should be called
100*4882a593Smuzhiyun    from a data_ready callback that is set on the socket. Note that
101*4882a593Smuzhiyun    maximum messages size is the limit of the receive socket
102*4882a593Smuzhiyun    buffer and message timeout is the receive timeout for the socket.
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun    ::
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun	void strp_check_rcv(struct strparser *strp);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun    strp_check_rcv is called to check for new messages on the socket.
109*4882a593Smuzhiyun    This is normally called at initialization of a stream parser
110*4882a593Smuzhiyun    instance or after strp_unpause.
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunCallbacks
113*4882a593Smuzhiyun=========
114*4882a593Smuzhiyun
115*4882a593SmuzhiyunThere are six callbacks:
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun    ::
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun	int (*parse_msg)(struct strparser *strp, struct sk_buff *skb);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun    parse_msg is called to determine the length of the next message
122*4882a593Smuzhiyun    in the stream. The upper layer must implement this function. It
123*4882a593Smuzhiyun    should parse the sk_buff as containing the headers for the
124*4882a593Smuzhiyun    next application layer message in the stream.
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun    The skb->cb in the input skb is a struct strp_msg. Only
127*4882a593Smuzhiyun    the offset field is relevant in parse_msg and gives the offset
128*4882a593Smuzhiyun    where the message starts in the skb.
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun    The return values of this function are:
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun    =========    ===========================================================
133*4882a593Smuzhiyun    >0           indicates length of successfully parsed message
134*4882a593Smuzhiyun    0            indicates more data must be received to parse the message
135*4882a593Smuzhiyun    -ESTRPIPE    current message should not be processed by the
136*4882a593Smuzhiyun		 kernel, return control of the socket to userspace which
137*4882a593Smuzhiyun		 can proceed to read the messages itself
138*4882a593Smuzhiyun    other < 0    Error in parsing, give control back to userspace
139*4882a593Smuzhiyun		 assuming that synchronization is lost and the stream
140*4882a593Smuzhiyun		 is unrecoverable (application expected to close TCP socket)
141*4882a593Smuzhiyun    =========    ===========================================================
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun    In the case that an error is returned (return value is less than
144*4882a593Smuzhiyun    zero) and the parser is in receive callback mode, then it will set
145*4882a593Smuzhiyun    the error on TCP socket and wake it up. If parse_msg returned
146*4882a593Smuzhiyun    -ESTRPIPE and the stream parser had previously read some bytes for
147*4882a593Smuzhiyun    the current message, then the error set on the attached socket is
148*4882a593Smuzhiyun    ENODATA since the stream is unrecoverable in that case.
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun    ::
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun	void (*lock)(struct strparser *strp)
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun    The lock callback is called to lock the strp structure when
155*4882a593Smuzhiyun    the strparser is performing an asynchronous operation (such as
156*4882a593Smuzhiyun    processing a timeout). In receive callback mode the default
157*4882a593Smuzhiyun    function is to lock_sock for the associated socket. In general
158*4882a593Smuzhiyun    mode the callback must be set appropriately.
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun    ::
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun	void (*unlock)(struct strparser *strp)
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun    The unlock callback is called to release the lock obtained
165*4882a593Smuzhiyun    by the lock callback. In receive callback mode the default
166*4882a593Smuzhiyun    function is release_sock for the associated socket. In general
167*4882a593Smuzhiyun    mode the callback must be set appropriately.
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun    ::
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun	void (*rcv_msg)(struct strparser *strp, struct sk_buff *skb);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun    rcv_msg is called when a full message has been received and
174*4882a593Smuzhiyun    is queued. The callee must consume the sk_buff; it can
175*4882a593Smuzhiyun    call strp_pause to prevent any further messages from being
176*4882a593Smuzhiyun    received in rcv_msg (see strp_pause above). This callback
177*4882a593Smuzhiyun    must be set.
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun    The skb->cb in the input skb is a struct strp_msg. This
180*4882a593Smuzhiyun    struct contains two fields: offset and full_len. Offset is
181*4882a593Smuzhiyun    where the message starts in the skb, and full_len is the
182*4882a593Smuzhiyun    the length of the message. skb->len - offset may be greater
183*4882a593Smuzhiyun    then full_len since strparser does not trim the skb.
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun    ::
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun	int (*read_sock_done)(struct strparser *strp, int err);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun     read_sock_done is called when the stream parser is done reading
190*4882a593Smuzhiyun     the TCP socket in receive callback mode. The stream parser may
191*4882a593Smuzhiyun     read multiple messages in a loop and this function allows cleanup
192*4882a593Smuzhiyun     to occur when exiting the loop. If the callback is not set (NULL
193*4882a593Smuzhiyun     in strp_init) a default function is used.
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun     ::
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun	void (*abort_parser)(struct strparser *strp, int err);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun     This function is called when stream parser encounters an error
200*4882a593Smuzhiyun     in parsing. The default function stops the stream parser and
201*4882a593Smuzhiyun     sets the error in the socket if the parser is in receive callback
202*4882a593Smuzhiyun     mode. The default function can be changed by setting the callback
203*4882a593Smuzhiyun     to non-NULL in strp_init.
204*4882a593Smuzhiyun
205*4882a593SmuzhiyunStatistics
206*4882a593Smuzhiyun==========
207*4882a593Smuzhiyun
208*4882a593SmuzhiyunVarious counters are kept for each stream parser instance. These are in
209*4882a593Smuzhiyunthe strp_stats structure. strp_aggr_stats is a convenience structure for
210*4882a593Smuzhiyunaccumulating statistics for multiple stream parser instances.
211*4882a593Smuzhiyunsave_strp_stats and aggregate_strp_stats are helper functions to save
212*4882a593Smuzhiyunand aggregate statistics.
213*4882a593Smuzhiyun
214*4882a593SmuzhiyunMessage assembly limits
215*4882a593Smuzhiyun=======================
216*4882a593Smuzhiyun
217*4882a593SmuzhiyunThe stream parser provide mechanisms to limit the resources consumed by
218*4882a593Smuzhiyunmessage assembly.
219*4882a593Smuzhiyun
220*4882a593SmuzhiyunA timer is set when assembly starts for a new message. In receive
221*4882a593Smuzhiyuncallback mode the message timeout is taken from rcvtime for the
222*4882a593Smuzhiyunassociated TCP socket. In general mode, the timeout is passed as an
223*4882a593Smuzhiyunargument in strp_process. If the timer fires before assembly completes
224*4882a593Smuzhiyunthe stream parser is aborted and the ETIMEDOUT error is set on the TCP
225*4882a593Smuzhiyunsocket if in receive callback mode.
226*4882a593Smuzhiyun
227*4882a593SmuzhiyunIn receive callback mode, message length is limited to the receive
228*4882a593Smuzhiyunbuffer size of the associated TCP socket. If the length returned by
229*4882a593Smuzhiyunparse_msg is greater than the socket buffer size then the stream parser
230*4882a593Smuzhiyunis aborted with EMSGSIZE error set on the TCP socket. Note that this
231*4882a593Smuzhiyunmakes the maximum size of receive skbuffs for a socket with a stream
232*4882a593Smuzhiyunparser to be 2*sk_rcvbuf of the TCP socket.
233*4882a593Smuzhiyun
234*4882a593SmuzhiyunIn general mode the message length limit is passed in as an argument
235*4882a593Smuzhiyunto strp_process.
236*4882a593Smuzhiyun
237*4882a593SmuzhiyunAuthor
238*4882a593Smuzhiyun======
239*4882a593Smuzhiyun
240*4882a593SmuzhiyunTom Herbert (tom@quantonium.net)
241