strparser.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. Stream Parser
  2. -------------
  3. The stream parser (strparser) is a utility that parses messages of an
  4. application layer protocol running over a TCP connection. The stream
  5. parser works in conjunction with an upper layer in the kernel to provide
  6. kernel support for application layer messages. For instance, Kernel
  7. Connection Multiplexor (KCM) uses the Stream Parser to parse messages
  8. using a BPF program.
  9. Interface
  10. ---------
  11. The API includes a context structure, a set of callbacks, utility
  12. functions, and a data_ready function. The callbacks include
  13. a parse_msg function that is called to perform parsing (e.g.
  14. BPF parsing in case of KCM), and a rcv_msg function that is called
  15. when a full message has been completed.
  16. A stream parser can be instantiated for a TCP connection. This is done
  17. by:
  18. strp_init(struct strparser *strp, struct sock *csk,
  19. struct strp_callbacks *cb)
  20. strp is a struct of type strparser that is allocated by the upper layer.
  21. csk is the TCP socket associated with the stream parser. Callbacks are
  22. called by the stream parser.
  23. Callbacks
  24. ---------
  25. There are four callbacks:
  26. int (*parse_msg)(struct strparser *strp, struct sk_buff *skb);
  27. parse_msg is called to determine the length of the next message
  28. in the stream. The upper layer must implement this function. It
  29. should parse the sk_buff as containing the headers for the
  30. next application layer messages in the stream.
  31. The skb->cb in the input skb is a struct strp_rx_msg. Only
  32. the offset field is relevant in parse_msg and gives the offset
  33. where the message starts in the skb.
  34. The return values of this function are:
  35. >0 : indicates length of successfully parsed message
  36. 0 : indicates more data must be received to parse the message
  37. -ESTRPIPE : current message should not be processed by the
  38. kernel, return control of the socket to userspace which
  39. can proceed to read the messages itself
  40. other < 0 : Error is parsing, give control back to userspace
  41. assuming that synchronization is lost and the stream
  42. is unrecoverable (application expected to close TCP socket)
  43. In the case that an error is returned (return value is less than
  44. zero) the stream parser will set the error on TCP socket and wake
  45. it up. If parse_msg returned -ESTRPIPE and the stream parser had
  46. previously read some bytes for the current message, then the error
  47. set on the attached socket is ENODATA since the stream is
  48. unrecoverable in that case.
  49. void (*rcv_msg)(struct strparser *strp, struct sk_buff *skb);
  50. rcv_msg is called when a full message has been received and
  51. is queued. The callee must consume the sk_buff; it can
  52. call strp_pause to prevent any further messages from being
  53. received in rcv_msg (see strp_pause below). This callback
  54. must be set.
  55. The skb->cb in the input skb is a struct strp_rx_msg. This
  56. struct contains two fields: offset and full_len. Offset is
  57. where the message starts in the skb, and full_len is the
  58. the length of the message. skb->len - offset may be greater
  59. then full_len since strparser does not trim the skb.
  60. int (*read_sock_done)(struct strparser *strp, int err);
  61. read_sock_done is called when the stream parser is done reading
  62. the TCP socket. The stream parser may read multiple messages
  63. in a loop and this function allows cleanup to occur when existing
  64. the loop. If the callback is not set (NULL in strp_init) a
  65. default function is used.
  66. void (*abort_parser)(struct strparser *strp, int err);
  67. This function is called when stream parser encounters an error
  68. in parsing. The default function stops the stream parser for the
  69. TCP socket and sets the error in the socket. The default function
  70. can be changed by setting the callback to non-NULL in strp_init.
  71. Functions
  72. ---------
  73. The upper layer calls strp_tcp_data_ready when data is ready on the lower
  74. socket for strparser to process. This should be called from a data_ready
  75. callback that is set on the socket.
  76. strp_stop is called to completely stop stream parser operations. This
  77. is called internally when the stream parser encounters an error, and
  78. it is called from the upper layer when unattaching a TCP socket.
  79. strp_done is called to unattach the stream parser from the TCP socket.
  80. This must be called after the stream processor has be stopped.
  81. strp_check_rcv is called to check for new messages on the socket. This
  82. is normally called at initialization of the a stream parser instance
  83. of after strp_unpause.
  84. Statistics
  85. ----------
  86. Various counters are kept for each stream parser for a TCP socket.
  87. These are in the strp_stats structure. strp_aggr_stats is a convenience
  88. structure for accumulating statistics for multiple stream parser
  89. instances. save_strp_stats and aggregate_strp_stats are helper functions
  90. to save and aggregate statistics.
  91. Message assembly limits
  92. -----------------------
  93. The stream parser provide mechanisms to limit the resources consumed by
  94. message assembly.
  95. A timer is set when assembly starts for a new message. The message
  96. timeout is taken from rcvtime for the associated TCP socket. If the
  97. timer fires before assembly completes the stream parser is aborted
  98. and the ETIMEDOUT error is set on the TCP socket.
  99. Message length is limited to the receive buffer size of the associated
  100. TCP socket. If the length returned by parse_msg is greater than
  101. the socket buffer size then the stream parser is aborted with
  102. EMSGSIZE error set on the TCP socket. Note that this makes the
  103. maximum size of receive skbuffs for a socket with a stream parser
  104. to be 2*sk_rcvbuf of the TCP socket.