rds.txt 17 KB

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