kcm.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =============================
  3. Kernel Connection Multiplexor
  4. =============================
  5. Kernel Connection Multiplexor (KCM) is a mechanism that provides a message based
  6. interface over TCP for generic application protocols. With KCM an application
  7. can efficiently send and receive application protocol messages over TCP using
  8. datagram sockets.
  9. KCM implements an NxM multiplexor in the kernel as diagrammed below::
  10. +------------+ +------------+ +------------+ +------------+
  11. | KCM socket | | KCM socket | | KCM socket | | KCM socket |
  12. +------------+ +------------+ +------------+ +------------+
  13. | | | |
  14. +-----------+ | | +----------+
  15. | | | |
  16. +----------------------------------+
  17. | Multiplexor |
  18. +----------------------------------+
  19. | | | | |
  20. +---------+ | | | ------------+
  21. | | | | |
  22. +----------+ +----------+ +----------+ +----------+ +----------+
  23. | Psock | | Psock | | Psock | | Psock | | Psock |
  24. +----------+ +----------+ +----------+ +----------+ +----------+
  25. | | | | |
  26. +----------+ +----------+ +----------+ +----------+ +----------+
  27. | TCP sock | | TCP sock | | TCP sock | | TCP sock | | TCP sock |
  28. +----------+ +----------+ +----------+ +----------+ +----------+
  29. KCM sockets
  30. ===========
  31. The KCM sockets provide the user interface to the multiplexor. All the KCM sockets
  32. bound to a multiplexor are considered to have equivalent function, and I/O
  33. operations in different sockets may be done in parallel without the need for
  34. synchronization between threads in userspace.
  35. Multiplexor
  36. ===========
  37. The multiplexor provides the message steering. In the transmit path, messages
  38. written on a KCM socket are sent atomically on an appropriate TCP socket.
  39. Similarly, in the receive path, messages are constructed on each TCP socket
  40. (Psock) and complete messages are steered to a KCM socket.
  41. TCP sockets & Psocks
  42. ====================
  43. TCP sockets may be bound to a KCM multiplexor. A Psock structure is allocated
  44. for each bound TCP socket, this structure holds the state for constructing
  45. messages on receive as well as other connection specific information for KCM.
  46. Connected mode semantics
  47. ========================
  48. Each multiplexor assumes that all attached TCP connections are to the same
  49. destination and can use the different connections for load balancing when
  50. transmitting. The normal send and recv calls (include sendmmsg and recvmmsg)
  51. can be used to send and receive messages from the KCM socket.
  52. Socket types
  53. ============
  54. KCM supports SOCK_DGRAM and SOCK_SEQPACKET socket types.
  55. Message delineation
  56. -------------------
  57. Messages are sent over a TCP stream with some application protocol message
  58. format that typically includes a header which frames the messages. The length
  59. of a received message can be deduced from the application protocol header
  60. (often just a simple length field).
  61. A TCP stream must be parsed to determine message boundaries. Berkeley Packet
  62. Filter (BPF) is used for this. When attaching a TCP socket to a multiplexor a
  63. BPF program must be specified. The program is called at the start of receiving
  64. a new message and is given an skbuff that contains the bytes received so far.
  65. It parses the message header and returns the length of the message. Given this
  66. information, KCM will construct the message of the stated length and deliver it
  67. to a KCM socket.
  68. TCP socket management
  69. ---------------------
  70. When a TCP socket is attached to a KCM multiplexor data ready (POLLIN) and
  71. write space available (POLLOUT) events are handled by the multiplexor. If there
  72. is a state change (disconnection) or other error on a TCP socket, an error is
  73. posted on the TCP socket so that a POLLERR event happens and KCM discontinues
  74. using the socket. When the application gets the error notification for a
  75. TCP socket, it should unattach the socket from KCM and then handle the error
  76. condition (the typical response is to close the socket and create a new
  77. connection if necessary).
  78. KCM limits the maximum receive message size to be the size of the receive
  79. socket buffer on the attached TCP socket (the socket buffer size can be set by
  80. SO_RCVBUF). If the length of a new message reported by the BPF program is
  81. greater than this limit a corresponding error (EMSGSIZE) is posted on the TCP
  82. socket. The BPF program may also enforce a maximum messages size and report an
  83. error when it is exceeded.
  84. A timeout may be set for assembling messages on a receive socket. The timeout
  85. value is taken from the receive timeout of the attached TCP socket (this is set
  86. by SO_RCVTIMEO). If the timer expires before assembly is complete an error
  87. (ETIMEDOUT) is posted on the socket.
  88. User interface
  89. ==============
  90. Creating a multiplexor
  91. ----------------------
  92. A new multiplexor and initial KCM socket is created by a socket call::
  93. socket(AF_KCM, type, protocol)
  94. - type is either SOCK_DGRAM or SOCK_SEQPACKET
  95. - protocol is KCMPROTO_CONNECTED
  96. Cloning KCM sockets
  97. -------------------
  98. After the first KCM socket is created using the socket call as described
  99. above, additional sockets for the multiplexor can be created by cloning
  100. a KCM socket. This is accomplished by an ioctl on a KCM socket::
  101. /* From linux/kcm.h */
  102. struct kcm_clone {
  103. int fd;
  104. };
  105. struct kcm_clone info;
  106. memset(&info, 0, sizeof(info));
  107. err = ioctl(kcmfd, SIOCKCMCLONE, &info);
  108. if (!err)
  109. newkcmfd = info.fd;
  110. Attach transport sockets
  111. ------------------------
  112. Attaching of transport sockets to a multiplexor is performed by calling an
  113. ioctl on a KCM socket for the multiplexor. e.g.::
  114. /* From linux/kcm.h */
  115. struct kcm_attach {
  116. int fd;
  117. int bpf_fd;
  118. };
  119. struct kcm_attach info;
  120. memset(&info, 0, sizeof(info));
  121. info.fd = tcpfd;
  122. info.bpf_fd = bpf_prog_fd;
  123. ioctl(kcmfd, SIOCKCMATTACH, &info);
  124. The kcm_attach structure contains:
  125. - fd: file descriptor for TCP socket being attached
  126. - bpf_prog_fd: file descriptor for compiled BPF program downloaded
  127. Unattach transport sockets
  128. --------------------------
  129. Unattaching a transport socket from a multiplexor is straightforward. An
  130. "unattach" ioctl is done with the kcm_unattach structure as the argument::
  131. /* From linux/kcm.h */
  132. struct kcm_unattach {
  133. int fd;
  134. };
  135. struct kcm_unattach info;
  136. memset(&info, 0, sizeof(info));
  137. info.fd = cfd;
  138. ioctl(fd, SIOCKCMUNATTACH, &info);
  139. Disabling receive on KCM socket
  140. -------------------------------
  141. A setsockopt is used to disable or enable receiving on a KCM socket.
  142. When receive is disabled, any pending messages in the socket's
  143. receive buffer are moved to other sockets. This feature is useful
  144. if an application thread knows that it will be doing a lot of
  145. work on a request and won't be able to service new messages for a
  146. while. Example use::
  147. int val = 1;
  148. setsockopt(kcmfd, SOL_KCM, KCM_RECV_DISABLE, &val, sizeof(val))
  149. BFP programs for message delineation
  150. ------------------------------------
  151. BPF programs can be compiled using the BPF LLVM backend. For example,
  152. the BPF program for parsing Thrift is::
  153. #include "bpf.h" /* for __sk_buff */
  154. #include "bpf_helpers.h" /* for load_word intrinsic */
  155. SEC("socket_kcm")
  156. int bpf_prog1(struct __sk_buff *skb)
  157. {
  158. return load_word(skb, 0) + 4;
  159. }
  160. char _license[] SEC("license") = "GPL";
  161. Use in applications
  162. ===================
  163. KCM accelerates application layer protocols. Specifically, it allows
  164. applications to use a message based interface for sending and receiving
  165. messages. The kernel provides necessary assurances that messages are sent
  166. and received atomically. This relieves much of the burden applications have
  167. in mapping a message based protocol onto the TCP stream. KCM also make
  168. application layer messages a unit of work in the kernel for the purposes of
  169. steering and scheduling, which in turn allows a simpler networking model in
  170. multithreaded applications.
  171. Configurations
  172. --------------
  173. In an Nx1 configuration, KCM logically provides multiple socket handles
  174. to the same TCP connection. This allows parallelism between in I/O
  175. operations on the TCP socket (for instance copyin and copyout of data is
  176. parallelized). In an application, a KCM socket can be opened for each
  177. processing thread and inserted into the epoll (similar to how SO_REUSEPORT
  178. is used to allow multiple listener sockets on the same port).
  179. In a MxN configuration, multiple connections are established to the
  180. same destination. These are used for simple load balancing.
  181. Message batching
  182. ----------------
  183. The primary purpose of KCM is load balancing between KCM sockets and hence
  184. threads in a nominal use case. Perfect load balancing, that is steering
  185. each received message to a different KCM socket or steering each sent
  186. message to a different TCP socket, can negatively impact performance
  187. since this doesn't allow for affinities to be established. Balancing
  188. based on groups, or batches of messages, can be beneficial for performance.
  189. On transmit, there are three ways an application can batch (pipeline)
  190. messages on a KCM socket.
  191. 1) Send multiple messages in a single sendmmsg.
  192. 2) Send a group of messages each with a sendmsg call, where all messages
  193. except the last have MSG_BATCH in the flags of sendmsg call.
  194. 3) Create "super message" composed of multiple messages and send this
  195. with a single sendmsg.
  196. On receive, the KCM module attempts to queue messages received on the
  197. same KCM socket during each TCP ready callback. The targeted KCM socket
  198. changes at each receive ready callback on the KCM socket. The application
  199. does not need to configure this.
  200. Error handling
  201. --------------
  202. An application should include a thread to monitor errors raised on
  203. the TCP connection. Normally, this will be done by placing each
  204. TCP socket attached to a KCM multiplexor in epoll set for POLLERR
  205. event. If an error occurs on an attached TCP socket, KCM sets an EPIPE
  206. on the socket thus waking up the application thread. When the application
  207. sees the error (which may just be a disconnect) it should unattach the
  208. socket from KCM and then close it. It is assumed that once an error is
  209. posted on the TCP socket the data stream is unrecoverable (i.e. an error
  210. may have occurred in the middle of receiving a message).
  211. TCP connection monitoring
  212. -------------------------
  213. In KCM there is no means to correlate a message to the TCP socket that
  214. was used to send or receive the message (except in the case there is
  215. only one attached TCP socket). However, the application does retain
  216. an open file descriptor to the socket so it will be able to get statistics
  217. from the socket which can be used in detecting issues (such as high
  218. retransmissions on the socket).