rds.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. Work is in progress to support RDS over iWARP, and using DCE to
  17. guarantee no dropped packets on Ethernet, it may be possible to use RDS over
  18. UDP in the future.
  19. The high-level semantics of RDS from the application's point of view are
  20. * Addressing
  21. RDS uses IPv4 addresses and 16bit port numbers to identify
  22. the end point of a connection. All socket operations that involve
  23. passing addresses between kernel and user space generally
  24. use a struct sockaddr_in.
  25. The fact that IPv4 addresses are used does not mean the underlying
  26. transport has to be IP-based. In fact, RDS over IB uses a
  27. reliable IB connection; the IP address is used exclusively to
  28. locate the remote node's GID (by ARPing for the given IP).
  29. The port space is entirely independent of UDP, TCP or any other
  30. protocol.
  31. * Socket interface
  32. RDS sockets work *mostly* as you would expect from a BSD
  33. socket. The next section will cover the details. At any rate,
  34. all I/O is performed through the standard BSD socket API.
  35. Some additions like zerocopy support are implemented through
  36. control messages, while other extensions use the getsockopt/
  37. setsockopt calls.
  38. Sockets must be bound before you can send or receive data.
  39. This is needed because binding also selects a transport and
  40. attaches it to the socket. Once bound, the transport assignment
  41. does not change. RDS will tolerate IPs moving around (eg in
  42. a active-active HA scenario), but only as long as the address
  43. doesn't move to a different transport.
  44. * sysctls
  45. RDS supports a number of sysctls in /proc/sys/net/rds
  46. Socket Interface
  47. ================
  48. AF_RDS, PF_RDS, SOL_RDS
  49. AF_RDS and PF_RDS are the domain type to be used with socket(2)
  50. to create RDS sockets. SOL_RDS is the socket-level to be used
  51. with setsockopt(2) and getsockopt(2) for RDS specific socket
  52. options.
  53. fd = socket(PF_RDS, SOCK_SEQPACKET, 0);
  54. This creates a new, unbound RDS socket.
  55. setsockopt(SOL_SOCKET): send and receive buffer size
  56. RDS honors the send and receive buffer size socket options.
  57. You are not allowed to queue more than SO_SNDSIZE bytes to
  58. a socket. A message is queued when sendmsg is called, and
  59. it leaves the queue when the remote system acknowledges
  60. its arrival.
  61. The SO_RCVSIZE option controls the maximum receive queue length.
  62. This is a soft limit rather than a hard limit - RDS will
  63. continue to accept and queue incoming messages, even if that
  64. takes the queue length over the limit. However, it will also
  65. mark the port as "congested" and send a congestion update to
  66. the source node. The source node is supposed to throttle any
  67. processes sending to this congested port.
  68. bind(fd, &sockaddr_in, ...)
  69. This binds the socket to a local IP address and port, and a
  70. transport.
  71. sendmsg(fd, ...)
  72. Sends a message to the indicated recipient. The kernel will
  73. transparently establish the underlying reliable connection
  74. if it isn't up yet.
  75. An attempt to send a message that exceeds SO_SNDSIZE will
  76. return with -EMSGSIZE
  77. An attempt to send a message that would take the total number
  78. of queued bytes over the SO_SNDSIZE threshold will return
  79. EAGAIN.
  80. An attempt to send a message to a destination that is marked
  81. as "congested" will return ENOBUFS.
  82. recvmsg(fd, ...)
  83. Receives a message that was queued to this socket. The sockets
  84. recv queue accounting is adjusted, and if the queue length
  85. drops below SO_SNDSIZE, the port is marked uncongested, and
  86. a congestion update is sent to all peers.
  87. Applications can ask the RDS kernel module to receive
  88. notifications via control messages (for instance, there is a
  89. notification when a congestion update arrived, or when a RDMA
  90. operation completes). These notifications are received through
  91. the msg.msg_control buffer of struct msghdr. The format of the
  92. messages is described in manpages.
  93. poll(fd)
  94. RDS supports the poll interface to allow the application
  95. to implement async I/O.
  96. POLLIN handling is pretty straightforward. When there's an
  97. incoming message queued to the socket, or a pending notification,
  98. we signal POLLIN.
  99. POLLOUT is a little harder. Since you can essentially send
  100. to any destination, RDS will always signal POLLOUT as long as
  101. there's room on the send queue (ie the number of bytes queued
  102. is less than the sendbuf size).
  103. However, the kernel will refuse to accept messages to
  104. a destination marked congested - in this case you will loop
  105. forever if you rely on poll to tell you what to do.
  106. This isn't a trivial problem, but applications can deal with
  107. this - by using congestion notifications, and by checking for
  108. ENOBUFS errors returned by sendmsg.
  109. setsockopt(SOL_RDS, RDS_CANCEL_SENT_TO, &sockaddr_in)
  110. This allows the application to discard all messages queued to a
  111. specific destination on this particular socket.
  112. This allows the application to cancel outstanding messages if
  113. it detects a timeout. For instance, if it tried to send a message,
  114. and the remote host is unreachable, RDS will keep trying forever.
  115. The application may decide it's not worth it, and cancel the
  116. operation. In this case, it would use RDS_CANCEL_SENT_TO to
  117. nuke any pending messages.
  118. RDMA for RDS
  119. ============
  120. see rds-rdma(7) manpage (available in rds-tools)
  121. Congestion Notifications
  122. ========================
  123. see rds(7) manpage
  124. RDS Protocol
  125. ============
  126. Message header
  127. The message header is a 'struct rds_header' (see rds.h):
  128. Fields:
  129. h_sequence:
  130. per-packet sequence number
  131. h_ack:
  132. piggybacked acknowledgment of last packet received
  133. h_len:
  134. length of data, not including header
  135. h_sport:
  136. source port
  137. h_dport:
  138. destination port
  139. h_flags:
  140. CONG_BITMAP - this is a congestion update bitmap
  141. ACK_REQUIRED - receiver must ack this packet
  142. RETRANSMITTED - packet has previously been sent
  143. h_credit:
  144. indicate to other end of connection that
  145. it has more credits available (i.e. there is
  146. more send room)
  147. h_padding[4]:
  148. unused, for future use
  149. h_csum:
  150. header checksum
  151. h_exthdr:
  152. optional data can be passed here. This is currently used for
  153. passing RDMA-related information.
  154. ACK and retransmit handling
  155. One might think that with reliable IB connections you wouldn't need
  156. to ack messages that have been received. The problem is that IB
  157. hardware generates an ack message before it has DMAed the message
  158. into memory. This creates a potential message loss if the HCA is
  159. disabled for any reason between when it sends the ack and before
  160. the message is DMAed and processed. This is only a potential issue
  161. if another HCA is available for fail-over.
  162. Sending an ack immediately would allow the sender to free the sent
  163. message from their send queue quickly, but could cause excessive
  164. traffic to be used for acks. RDS piggybacks acks on sent data
  165. packets. Ack-only packets are reduced by only allowing one to be
  166. in flight at a time, and by the sender only asking for acks when
  167. its send buffers start to fill up. All retransmissions are also
  168. acked.
  169. Flow Control
  170. RDS's IB transport uses a credit-based mechanism to verify that
  171. there is space in the peer's receive buffers for more data. This
  172. eliminates the need for hardware retries on the connection.
  173. Congestion
  174. Messages waiting in the receive queue on the receiving socket
  175. are accounted against the sockets SO_RCVBUF option value. Only
  176. the payload bytes in the message are accounted for. If the
  177. number of bytes queued equals or exceeds rcvbuf then the socket
  178. is congested. All sends attempted to this socket's address
  179. should return block or return -EWOULDBLOCK.
  180. Applications are expected to be reasonably tuned such that this
  181. situation very rarely occurs. An application encountering this
  182. "back-pressure" is considered a bug.
  183. This is implemented by having each node maintain bitmaps which
  184. indicate which ports on bound addresses are congested. As the
  185. bitmap changes it is sent through all the connections which
  186. terminate in the local address of the bitmap which changed.
  187. The bitmaps are allocated as connections are brought up. This
  188. avoids allocation in the interrupt handling path which queues
  189. sages on sockets. The dense bitmaps let transports send the
  190. entire bitmap on any bitmap change reasonably efficiently. This
  191. is much easier to implement than some finer-grained
  192. communication of per-port congestion. The sender does a very
  193. inexpensive bit test to test if the port it's about to send to
  194. is congested or not.
  195. RDS Transport Layer
  196. ==================
  197. As mentioned above, RDS is not IB-specific. Its code is divided
  198. into a general RDS layer and a transport layer.
  199. The general layer handles the socket API, congestion handling,
  200. loopback, stats, usermem pinning, and the connection state machine.
  201. The transport layer handles the details of the transport. The IB
  202. transport, for example, handles all the queue pairs, work requests,
  203. CM event handlers, and other Infiniband details.
  204. RDS Kernel Structures
  205. =====================
  206. struct rds_message
  207. aka possibly "rds_outgoing", the generic RDS layer copies data to
  208. be sent and sets header fields as needed, based on the socket API.
  209. This is then queued for the individual connection and sent by the
  210. connection's transport.
  211. struct rds_incoming
  212. a generic struct referring to incoming data that can be handed from
  213. the transport to the general code and queued by the general code
  214. while the socket is awoken. It is then passed back to the transport
  215. code to handle the actual copy-to-user.
  216. struct rds_socket
  217. per-socket information
  218. struct rds_connection
  219. per-connection information
  220. struct rds_transport
  221. pointers to transport-specific functions
  222. struct rds_statistics
  223. non-transport-specific statistics
  224. struct rds_cong_map
  225. wraps the raw congestion bitmap, contains rbnode, waitq, etc.
  226. Connection management
  227. =====================
  228. Connections may be in UP, DOWN, CONNECTING, DISCONNECTING, and
  229. ERROR states.
  230. The first time an attempt is made by an RDS socket to send data to
  231. a node, a connection is allocated and connected. That connection is
  232. then maintained forever -- if there are transport errors, the
  233. connection will be dropped and re-established.
  234. Dropping a connection while packets are queued will cause queued or
  235. partially-sent datagrams to be retransmitted when the connection is
  236. re-established.
  237. The send path
  238. =============
  239. rds_sendmsg()
  240. struct rds_message built from incoming data
  241. CMSGs parsed (e.g. RDMA ops)
  242. transport connection alloced and connected if not already
  243. rds_message placed on send queue
  244. send worker awoken
  245. rds_send_worker()
  246. calls rds_send_xmit() until queue is empty
  247. rds_send_xmit()
  248. transmits congestion map if one is pending
  249. may set ACK_REQUIRED
  250. calls transport to send either non-RDMA or RDMA message
  251. (RDMA ops never retransmitted)
  252. rds_ib_xmit()
  253. allocs work requests from send ring
  254. adds any new send credits available to peer (h_credits)
  255. maps the rds_message's sg list
  256. piggybacks ack
  257. populates work requests
  258. post send to connection's queue pair
  259. The recv path
  260. =============
  261. rds_ib_recv_cq_comp_handler()
  262. looks at write completions
  263. unmaps recv buffer from device
  264. no errors, call rds_ib_process_recv()
  265. refill recv ring
  266. rds_ib_process_recv()
  267. validate header checksum
  268. copy header to rds_ib_incoming struct if start of a new datagram
  269. add to ibinc's fraglist
  270. if competed datagram:
  271. update cong map if datagram was cong update
  272. call rds_recv_incoming() otherwise
  273. note if ack is required
  274. rds_recv_incoming()
  275. drop duplicate packets
  276. respond to pings
  277. find the sock associated with this datagram
  278. add to sock queue
  279. wake up sock
  280. do some congestion calculations
  281. rds_recvmsg
  282. copy data into user iovec
  283. handle CMSGs
  284. return to application