rds.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ==
  3. RDS
  4. ===
  5. Overview
  6. ========
  7. This readme tries to provide some background on the hows and whys of RDS,
  8. and will hopefully help you find your way around the code.
  9. In addition, please see this email about RDS origins:
  10. http://oss.oracle.com/pipermail/rds-devel/2007-November/000228.html
  11. RDS Architecture
  12. ================
  13. RDS provides reliable, ordered datagram delivery by using a single
  14. reliable connection between any two nodes in the cluster. This allows
  15. applications to use a single socket to talk to any other process in the
  16. cluster - so in a cluster with N processes you need N sockets, in contrast
  17. to N*N if you use a connection-oriented socket transport like TCP.
  18. RDS is not Infiniband-specific; it was designed to support different
  19. transports. The current implementation used to support RDS over TCP as well
  20. as IB.
  21. The high-level semantics of RDS from the application's point of view are
  22. * Addressing
  23. RDS uses IPv4 addresses and 16bit port numbers to identify
  24. the end point of a connection. All socket operations that involve
  25. passing addresses between kernel and user space generally
  26. use a struct sockaddr_in.
  27. The fact that IPv4 addresses are used does not mean the underlying
  28. transport has to be IP-based. In fact, RDS over IB uses a
  29. reliable IB connection; the IP address is used exclusively to
  30. locate the remote node's GID (by ARPing for the given IP).
  31. The port space is entirely independent of UDP, TCP or any other
  32. protocol.
  33. * Socket interface
  34. RDS sockets work *mostly* as you would expect from a BSD
  35. socket. The next section will cover the details. At any rate,
  36. all I/O is performed through the standard BSD socket API.
  37. Some additions like zerocopy support are implemented through
  38. control messages, while other extensions use the getsockopt/
  39. setsockopt calls.
  40. Sockets must be bound before you can send or receive data.
  41. This is needed because binding also selects a transport and
  42. attaches it to the socket. Once bound, the transport assignment
  43. does not change. RDS will tolerate IPs moving around (eg in
  44. a active-active HA scenario), but only as long as the address
  45. doesn't move to a different transport.
  46. * sysctls
  47. RDS supports a number of sysctls in /proc/sys/net/rds
  48. Socket Interface
  49. ================
  50. AF_RDS, PF_RDS, SOL_RDS
  51. AF_RDS and PF_RDS are the domain type to be used with socket(2)
  52. to create RDS sockets. SOL_RDS is the socket-level to be used
  53. with setsockopt(2) and getsockopt(2) for RDS specific socket
  54. options.
  55. fd = socket(PF_RDS, SOCK_SEQPACKET, 0);
  56. This creates a new, unbound RDS socket.
  57. setsockopt(SOL_SOCKET): send and receive buffer size
  58. RDS honors the send and receive buffer size socket options.
  59. You are not allowed to queue more than SO_SNDSIZE bytes to
  60. a socket. A message is queued when sendmsg is called, and
  61. it leaves the queue when the remote system acknowledges
  62. its arrival.
  63. The SO_RCVSIZE option controls the maximum receive queue length.
  64. This is a soft limit rather than a hard limit - RDS will
  65. continue to accept and queue incoming messages, even if that
  66. takes the queue length over the limit. However, it will also
  67. mark the port as "congested" and send a congestion update to
  68. the source node. The source node is supposed to throttle any
  69. processes sending to this congested port.
  70. bind(fd, &sockaddr_in, ...)
  71. This binds the socket to a local IP address and port, and a
  72. transport, if one has not already been selected via the
  73. SO_RDS_TRANSPORT socket option
  74. sendmsg(fd, ...)
  75. Sends a message to the indicated recipient. The kernel will
  76. transparently establish the underlying reliable connection
  77. if it isn't up yet.
  78. An attempt to send a message that exceeds SO_SNDSIZE will
  79. return with -EMSGSIZE
  80. An attempt to send a message that would take the total number
  81. of queued bytes over the SO_SNDSIZE threshold will return
  82. EAGAIN.
  83. An attempt to send a message to a destination that is marked
  84. as "congested" will return ENOBUFS.
  85. recvmsg(fd, ...)
  86. Receives a message that was queued to this socket. The sockets
  87. recv queue accounting is adjusted, and if the queue length
  88. drops below SO_SNDSIZE, the port is marked uncongested, and
  89. a congestion update is sent to all peers.
  90. Applications can ask the RDS kernel module to receive
  91. notifications via control messages (for instance, there is a
  92. notification when a congestion update arrived, or when a RDMA
  93. operation completes). These notifications are received through
  94. the msg.msg_control buffer of struct msghdr. The format of the
  95. messages is described in manpages.
  96. poll(fd)
  97. RDS supports the poll interface to allow the application
  98. to implement async I/O.
  99. POLLIN handling is pretty straightforward. When there's an
  100. incoming message queued to the socket, or a pending notification,
  101. we signal POLLIN.
  102. POLLOUT is a little harder. Since you can essentially send
  103. to any destination, RDS will always signal POLLOUT as long as
  104. there's room on the send queue (ie the number of bytes queued
  105. is less than the sendbuf size).
  106. However, the kernel will refuse to accept messages to
  107. a destination marked congested - in this case you will loop
  108. forever if you rely on poll to tell you what to do.
  109. This isn't a trivial problem, but applications can deal with
  110. this - by using congestion notifications, and by checking for
  111. ENOBUFS errors returned by sendmsg.
  112. setsockopt(SOL_RDS, RDS_CANCEL_SENT_TO, &sockaddr_in)
  113. This allows the application to discard all messages queued to a
  114. specific destination on this particular socket.
  115. This allows the application to cancel outstanding messages if
  116. it detects a timeout. For instance, if it tried to send a message,
  117. and the remote host is unreachable, RDS will keep trying forever.
  118. The application may decide it's not worth it, and cancel the
  119. operation. In this case, it would use RDS_CANCEL_SENT_TO to
  120. nuke any pending messages.
  121. ``setsockopt(fd, SOL_RDS, SO_RDS_TRANSPORT, (int *)&transport ..), getsockopt(fd, SOL_RDS, SO_RDS_TRANSPORT, (int *)&transport ..)``
  122. Set or read an integer defining the underlying
  123. encapsulating transport to be used for RDS packets on the
  124. socket. When setting the option, integer argument may be
  125. one of RDS_TRANS_TCP or RDS_TRANS_IB. When retrieving the
  126. value, RDS_TRANS_NONE will be returned on an unbound socket.
  127. This socket option may only be set exactly once on the socket,
  128. prior to binding it via the bind(2) system call. Attempts to
  129. set SO_RDS_TRANSPORT on a socket for which the transport has
  130. been previously attached explicitly (by SO_RDS_TRANSPORT) or
  131. implicitly (via bind(2)) will return an error of EOPNOTSUPP.
  132. An attempt to set SO_RDS_TRANSPORT to RDS_TRANS_NONE will
  133. always return EINVAL.
  134. RDMA for RDS
  135. ============
  136. see rds-rdma(7) manpage (available in rds-tools)
  137. Congestion Notifications
  138. ========================
  139. see rds(7) manpage
  140. RDS Protocol
  141. ============
  142. Message header
  143. The message header is a 'struct rds_header' (see rds.h):
  144. Fields:
  145. h_sequence:
  146. per-packet sequence number
  147. h_ack:
  148. piggybacked acknowledgment of last packet received
  149. h_len:
  150. length of data, not including header
  151. h_sport:
  152. source port
  153. h_dport:
  154. destination port
  155. h_flags:
  156. Can be:
  157. ============= ==================================
  158. CONG_BITMAP this is a congestion update bitmap
  159. ACK_REQUIRED receiver must ack this packet
  160. RETRANSMITTED packet has previously been sent
  161. ============= ==================================
  162. h_credit:
  163. indicate to other end of connection that
  164. it has more credits available (i.e. there is
  165. more send room)
  166. h_padding[4]:
  167. unused, for future use
  168. h_csum:
  169. header checksum
  170. h_exthdr:
  171. optional data can be passed here. This is currently used for
  172. passing RDMA-related information.
  173. ACK and retransmit handling
  174. One might think that with reliable IB connections you wouldn't need
  175. to ack messages that have been received. The problem is that IB
  176. hardware generates an ack message before it has DMAed the message
  177. into memory. This creates a potential message loss if the HCA is
  178. disabled for any reason between when it sends the ack and before
  179. the message is DMAed and processed. This is only a potential issue
  180. if another HCA is available for fail-over.
  181. Sending an ack immediately would allow the sender to free the sent
  182. message from their send queue quickly, but could cause excessive
  183. traffic to be used for acks. RDS piggybacks acks on sent data
  184. packets. Ack-only packets are reduced by only allowing one to be
  185. in flight at a time, and by the sender only asking for acks when
  186. its send buffers start to fill up. All retransmissions are also
  187. acked.
  188. Flow Control
  189. RDS's IB transport uses a credit-based mechanism to verify that
  190. there is space in the peer's receive buffers for more data. This
  191. eliminates the need for hardware retries on the connection.
  192. Congestion
  193. Messages waiting in the receive queue on the receiving socket
  194. are accounted against the sockets SO_RCVBUF option value. Only
  195. the payload bytes in the message are accounted for. If the
  196. number of bytes queued equals or exceeds rcvbuf then the socket
  197. is congested. All sends attempted to this socket's address
  198. should return block or return -EWOULDBLOCK.
  199. Applications are expected to be reasonably tuned such that this
  200. situation very rarely occurs. An application encountering this
  201. "back-pressure" is considered a bug.
  202. This is implemented by having each node maintain bitmaps which
  203. indicate which ports on bound addresses are congested. As the
  204. bitmap changes it is sent through all the connections which
  205. terminate in the local address of the bitmap which changed.
  206. The bitmaps are allocated as connections are brought up. This
  207. avoids allocation in the interrupt handling path which queues
  208. sages on sockets. The dense bitmaps let transports send the
  209. entire bitmap on any bitmap change reasonably efficiently. This
  210. is much easier to implement than some finer-grained
  211. communication of per-port congestion. The sender does a very
  212. inexpensive bit test to test if the port it's about to send to
  213. is congested or not.
  214. RDS Transport Layer
  215. ===================
  216. As mentioned above, RDS is not IB-specific. Its code is divided
  217. into a general RDS layer and a transport layer.
  218. The general layer handles the socket API, congestion handling,
  219. loopback, stats, usermem pinning, and the connection state machine.
  220. The transport layer handles the details of the transport. The IB
  221. transport, for example, handles all the queue pairs, work requests,
  222. CM event handlers, and other Infiniband details.
  223. RDS Kernel Structures
  224. =====================
  225. struct rds_message
  226. aka possibly "rds_outgoing", the generic RDS layer copies data to
  227. be sent and sets header fields as needed, based on the socket API.
  228. This is then queued for the individual connection and sent by the
  229. connection's transport.
  230. struct rds_incoming
  231. a generic struct referring to incoming data that can be handed from
  232. the transport to the general code and queued by the general code
  233. while the socket is awoken. It is then passed back to the transport
  234. code to handle the actual copy-to-user.
  235. struct rds_socket
  236. per-socket information
  237. struct rds_connection
  238. per-connection information
  239. struct rds_transport
  240. pointers to transport-specific functions
  241. struct rds_statistics
  242. non-transport-specific statistics
  243. struct rds_cong_map
  244. wraps the raw congestion bitmap, contains rbnode, waitq, etc.
  245. Connection management
  246. =====================
  247. Connections may be in UP, DOWN, CONNECTING, DISCONNECTING, and
  248. ERROR states.
  249. The first time an attempt is made by an RDS socket to send data to
  250. a node, a connection is allocated and connected. That connection is
  251. then maintained forever -- if there are transport errors, the
  252. connection will be dropped and re-established.
  253. Dropping a connection while packets are queued will cause queued or
  254. partially-sent datagrams to be retransmitted when the connection is
  255. re-established.
  256. The send path
  257. =============
  258. rds_sendmsg()
  259. - struct rds_message built from incoming data
  260. - CMSGs parsed (e.g. RDMA ops)
  261. - transport connection alloced and connected if not already
  262. - rds_message placed on send queue
  263. - send worker awoken
  264. rds_send_worker()
  265. - calls rds_send_xmit() until queue is empty
  266. rds_send_xmit()
  267. - transmits congestion map if one is pending
  268. - may set ACK_REQUIRED
  269. - calls transport to send either non-RDMA or RDMA message
  270. (RDMA ops never retransmitted)
  271. rds_ib_xmit()
  272. - allocs work requests from send ring
  273. - adds any new send credits available to peer (h_credits)
  274. - maps the rds_message's sg list
  275. - piggybacks ack
  276. - populates work requests
  277. - post send to connection's queue pair
  278. The recv path
  279. =============
  280. rds_ib_recv_cq_comp_handler()
  281. - looks at write completions
  282. - unmaps recv buffer from device
  283. - no errors, call rds_ib_process_recv()
  284. - refill recv ring
  285. rds_ib_process_recv()
  286. - validate header checksum
  287. - copy header to rds_ib_incoming struct if start of a new datagram
  288. - add to ibinc's fraglist
  289. - if competed datagram:
  290. - update cong map if datagram was cong update
  291. - call rds_recv_incoming() otherwise
  292. - note if ack is required
  293. rds_recv_incoming()
  294. - drop duplicate packets
  295. - respond to pings
  296. - find the sock associated with this datagram
  297. - add to sock queue
  298. - wake up sock
  299. - do some congestion calculations
  300. rds_recvmsg
  301. - copy data into user iovec
  302. - handle CMSGs
  303. - return to application
  304. Multipath RDS (mprds)
  305. =====================
  306. Mprds is multipathed-RDS, primarily intended for RDS-over-TCP
  307. (though the concept can be extended to other transports). The classical
  308. implementation of RDS-over-TCP is implemented by demultiplexing multiple
  309. PF_RDS sockets between any 2 endpoints (where endpoint == [IP address,
  310. port]) over a single TCP socket between the 2 IP addresses involved. This
  311. has the limitation that it ends up funneling multiple RDS flows over a
  312. single TCP flow, thus it is
  313. (a) upper-bounded to the single-flow bandwidth,
  314. (b) suffers from head-of-line blocking for all the RDS sockets.
  315. Better throughput (for a fixed small packet size, MTU) can be achieved
  316. by having multiple TCP/IP flows per rds/tcp connection, i.e., multipathed
  317. RDS (mprds). Each such TCP/IP flow constitutes a path for the rds/tcp
  318. connection. RDS sockets will be attached to a path based on some hash
  319. (e.g., of local address and RDS port number) and packets for that RDS
  320. socket will be sent over the attached path using TCP to segment/reassemble
  321. RDS datagrams on that path.
  322. Multipathed RDS is implemented by splitting the struct rds_connection into
  323. a common (to all paths) part, and a per-path struct rds_conn_path. All
  324. I/O workqs and reconnect threads are driven from the rds_conn_path.
  325. Transports such as TCP that are multipath capable may then set up a
  326. TCP socket per rds_conn_path, and this is managed by the transport via
  327. the transport privatee cp_transport_data pointer.
  328. Transports announce themselves as multipath capable by setting the
  329. t_mp_capable bit during registration with the rds core module. When the
  330. transport is multipath-capable, rds_sendmsg() hashes outgoing traffic
  331. across multiple paths. The outgoing hash is computed based on the
  332. local address and port that the PF_RDS socket is bound to.
  333. Additionally, even if the transport is MP capable, we may be
  334. peering with some node that does not support mprds, or supports
  335. a different number of paths. As a result, the peering nodes need
  336. to agree on the number of paths to be used for the connection.
  337. This is done by sending out a control packet exchange before the
  338. first data packet. The control packet exchange must have completed
  339. prior to outgoing hash completion in rds_sendmsg() when the transport
  340. is mutlipath capable.
  341. The control packet is an RDS ping packet (i.e., packet to rds dest
  342. port 0) with the ping packet having a rds extension header option of
  343. type RDS_EXTHDR_NPATHS, length 2 bytes, and the value is the
  344. number of paths supported by the sender. The "probe" ping packet will
  345. get sent from some reserved port, RDS_FLAG_PROBE_PORT (in <linux/rds.h>)
  346. The receiver of a ping from RDS_FLAG_PROBE_PORT will thus immediately
  347. be able to compute the min(sender_paths, rcvr_paths). The pong
  348. sent in response to a probe-ping should contain the rcvr's npaths
  349. when the rcvr is mprds-capable.
  350. If the rcvr is not mprds-capable, the exthdr in the ping will be
  351. ignored. In this case the pong will not have any exthdrs, so the sender
  352. of the probe-ping can default to single-path mprds.