kcm.txt 11 KB

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