tcmu-design.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. Contents:
  2. 1) TCM Userspace Design
  3. a) Background
  4. b) Benefits
  5. c) Design constraints
  6. d) Implementation overview
  7. i. Mailbox
  8. ii. Command ring
  9. iii. Data Area
  10. e) Device discovery
  11. f) Device events
  12. g) Other contingencies
  13. 2) Writing a user pass-through handler
  14. a) Discovering and configuring TCMU uio devices
  15. b) Waiting for events on the device(s)
  16. c) Managing the command ring
  17. 3) A final note
  18. TCM Userspace Design
  19. --------------------
  20. TCM is another name for LIO, an in-kernel iSCSI target (server).
  21. Existing TCM targets run in the kernel. TCMU (TCM in Userspace)
  22. allows userspace programs to be written which act as iSCSI targets.
  23. This document describes the design.
  24. The existing kernel provides modules for different SCSI transport
  25. protocols. TCM also modularizes the data storage. There are existing
  26. modules for file, block device, RAM or using another SCSI device as
  27. storage. These are called "backstores" or "storage engines". These
  28. built-in modules are implemented entirely as kernel code.
  29. Background:
  30. In addition to modularizing the transport protocol used for carrying
  31. SCSI commands ("fabrics"), the Linux kernel target, LIO, also modularizes
  32. the actual data storage as well. These are referred to as "backstores"
  33. or "storage engines". The target comes with backstores that allow a
  34. file, a block device, RAM, or another SCSI device to be used for the
  35. local storage needed for the exported SCSI LUN. Like the rest of LIO,
  36. these are implemented entirely as kernel code.
  37. These backstores cover the most common use cases, but not all. One new
  38. use case that other non-kernel target solutions, such as tgt, are able
  39. to support is using Gluster's GLFS or Ceph's RBD as a backstore. The
  40. target then serves as a translator, allowing initiators to store data
  41. in these non-traditional networked storage systems, while still only
  42. using standard protocols themselves.
  43. If the target is a userspace process, supporting these is easy. tgt,
  44. for example, needs only a small adapter module for each, because the
  45. modules just use the available userspace libraries for RBD and GLFS.
  46. Adding support for these backstores in LIO is considerably more
  47. difficult, because LIO is entirely kernel code. Instead of undertaking
  48. the significant work to port the GLFS or RBD APIs and protocols to the
  49. kernel, another approach is to create a userspace pass-through
  50. backstore for LIO, "TCMU".
  51. Benefits:
  52. In addition to allowing relatively easy support for RBD and GLFS, TCMU
  53. will also allow easier development of new backstores. TCMU combines
  54. with the LIO loopback fabric to become something similar to FUSE
  55. (Filesystem in Userspace), but at the SCSI layer instead of the
  56. filesystem layer. A SUSE, if you will.
  57. The disadvantage is there are more distinct components to configure, and
  58. potentially to malfunction. This is unavoidable, but hopefully not
  59. fatal if we're careful to keep things as simple as possible.
  60. Design constraints:
  61. - Good performance: high throughput, low latency
  62. - Cleanly handle if userspace:
  63. 1) never attaches
  64. 2) hangs
  65. 3) dies
  66. 4) misbehaves
  67. - Allow future flexibility in user & kernel implementations
  68. - Be reasonably memory-efficient
  69. - Simple to configure & run
  70. - Simple to write a userspace backend
  71. Implementation overview:
  72. The core of the TCMU interface is a memory region that is shared
  73. between kernel and userspace. Within this region is: a control area
  74. (mailbox); a lockless producer/consumer circular buffer for commands
  75. to be passed up, and status returned; and an in/out data buffer area.
  76. TCMU uses the pre-existing UIO subsystem. UIO allows device driver
  77. development in userspace, and this is conceptually very close to the
  78. TCMU use case, except instead of a physical device, TCMU implements a
  79. memory-mapped layout designed for SCSI commands. Using UIO also
  80. benefits TCMU by handling device introspection (e.g. a way for
  81. userspace to determine how large the shared region is) and signaling
  82. mechanisms in both directions.
  83. There are no embedded pointers in the memory region. Everything is
  84. expressed as an offset from the region's starting address. This allows
  85. the ring to still work if the user process dies and is restarted with
  86. the region mapped at a different virtual address.
  87. See target_core_user.h for the struct definitions.
  88. The Mailbox:
  89. The mailbox is always at the start of the shared memory region, and
  90. contains a version, details about the starting offset and size of the
  91. command ring, and head and tail pointers to be used by the kernel and
  92. userspace (respectively) to put commands on the ring, and indicate
  93. when the commands are completed.
  94. version - 1 (userspace should abort if otherwise)
  95. flags:
  96. - TCMU_MAILBOX_FLAG_CAP_OOOC: indicates out-of-order completion is
  97. supported. See "The Command Ring" for details.
  98. cmdr_off - The offset of the start of the command ring from the start
  99. of the memory region, to account for the mailbox size.
  100. cmdr_size - The size of the command ring. This does *not* need to be a
  101. power of two.
  102. cmd_head - Modified by the kernel to indicate when a command has been
  103. placed on the ring.
  104. cmd_tail - Modified by userspace to indicate when it has completed
  105. processing of a command.
  106. The Command Ring:
  107. Commands are placed on the ring by the kernel incrementing
  108. mailbox.cmd_head by the size of the command, modulo cmdr_size, and
  109. then signaling userspace via uio_event_notify(). Once the command is
  110. completed, userspace updates mailbox.cmd_tail in the same way and
  111. signals the kernel via a 4-byte write(). When cmd_head equals
  112. cmd_tail, the ring is empty -- no commands are currently waiting to be
  113. processed by userspace.
  114. TCMU commands are 8-byte aligned. They start with a common header
  115. containing "len_op", a 32-bit value that stores the length, as well as
  116. the opcode in the lowest unused bits. It also contains cmd_id and
  117. flags fields for setting by the kernel (kflags) and userspace
  118. (uflags).
  119. Currently only two opcodes are defined, TCMU_OP_CMD and TCMU_OP_PAD.
  120. When the opcode is CMD, the entry in the command ring is a struct
  121. tcmu_cmd_entry. Userspace finds the SCSI CDB (Command Data Block) via
  122. tcmu_cmd_entry.req.cdb_off. This is an offset from the start of the
  123. overall shared memory region, not the entry. The data in/out buffers
  124. are accessible via tht req.iov[] array. iov_cnt contains the number of
  125. entries in iov[] needed to describe either the Data-In or Data-Out
  126. buffers. For bidirectional commands, iov_cnt specifies how many iovec
  127. entries cover the Data-Out area, and iov_bidi_cnt specifies how many
  128. iovec entries immediately after that in iov[] cover the Data-In
  129. area. Just like other fields, iov.iov_base is an offset from the start
  130. of the region.
  131. When completing a command, userspace sets rsp.scsi_status, and
  132. rsp.sense_buffer if necessary. Userspace then increments
  133. mailbox.cmd_tail by entry.hdr.length (mod cmdr_size) and signals the
  134. kernel via the UIO method, a 4-byte write to the file descriptor.
  135. If TCMU_MAILBOX_FLAG_CAP_OOOC is set for mailbox->flags, kernel is
  136. capable of handling out-of-order completions. In this case, userspace can
  137. handle command in different order other than original. Since kernel would
  138. still process the commands in the same order it appeared in the command
  139. ring, userspace need to update the cmd->id when completing the
  140. command(a.k.a steal the original command's entry).
  141. When the opcode is PAD, userspace only updates cmd_tail as above --
  142. it's a no-op. (The kernel inserts PAD entries to ensure each CMD entry
  143. is contiguous within the command ring.)
  144. More opcodes may be added in the future. If userspace encounters an
  145. opcode it does not handle, it must set UNKNOWN_OP bit (bit 0) in
  146. hdr.uflags, update cmd_tail, and proceed with processing additional
  147. commands, if any.
  148. The Data Area:
  149. This is shared-memory space after the command ring. The organization
  150. of this area is not defined in the TCMU interface, and userspace
  151. should access only the parts referenced by pending iovs.
  152. Device Discovery:
  153. Other devices may be using UIO besides TCMU. Unrelated user processes
  154. may also be handling different sets of TCMU devices. TCMU userspace
  155. processes must find their devices by scanning sysfs
  156. class/uio/uio*/name. For TCMU devices, these names will be of the
  157. format:
  158. tcm-user/<hba_num>/<device_name>/<subtype>/<path>
  159. where "tcm-user" is common for all TCMU-backed UIO devices. <hba_num>
  160. and <device_name> allow userspace to find the device's path in the
  161. kernel target's configfs tree. Assuming the usual mount point, it is
  162. found at:
  163. /sys/kernel/config/target/core/user_<hba_num>/<device_name>
  164. This location contains attributes such as "hw_block_size", that
  165. userspace needs to know for correct operation.
  166. <subtype> will be a userspace-process-unique string to identify the
  167. TCMU device as expecting to be backed by a certain handler, and <path>
  168. will be an additional handler-specific string for the user process to
  169. configure the device, if needed. The name cannot contain ':', due to
  170. LIO limitations.
  171. For all devices so discovered, the user handler opens /dev/uioX and
  172. calls mmap():
  173. mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)
  174. where size must be equal to the value read from
  175. /sys/class/uio/uioX/maps/map0/size.
  176. Device Events:
  177. If a new device is added or removed, a notification will be broadcast
  178. over netlink, using a generic netlink family name of "TCM-USER" and a
  179. multicast group named "config". This will include the UIO name as
  180. described in the previous section, as well as the UIO minor
  181. number. This should allow userspace to identify both the UIO device and
  182. the LIO device, so that after determining the device is supported
  183. (based on subtype) it can take the appropriate action.
  184. Other contingencies:
  185. Userspace handler process never attaches:
  186. - TCMU will post commands, and then abort them after a timeout period
  187. (30 seconds.)
  188. Userspace handler process is killed:
  189. - It is still possible to restart and re-connect to TCMU
  190. devices. Command ring is preserved. However, after the timeout period,
  191. the kernel will abort pending tasks.
  192. Userspace handler process hangs:
  193. - The kernel will abort pending tasks after a timeout period.
  194. Userspace handler process is malicious:
  195. - The process can trivially break the handling of devices it controls,
  196. but should not be able to access kernel memory outside its shared
  197. memory areas.
  198. Writing a user pass-through handler (with example code)
  199. -------------------------------------------------------
  200. A user process handing a TCMU device must support the following:
  201. a) Discovering and configuring TCMU uio devices
  202. b) Waiting for events on the device(s)
  203. c) Managing the command ring: Parsing operations and commands,
  204. performing work as needed, setting response fields (scsi_status and
  205. possibly sense_buffer), updating cmd_tail, and notifying the kernel
  206. that work has been finished
  207. First, consider instead writing a plugin for tcmu-runner. tcmu-runner
  208. implements all of this, and provides a higher-level API for plugin
  209. authors.
  210. TCMU is designed so that multiple unrelated processes can manage TCMU
  211. devices separately. All handlers should make sure to only open their
  212. devices, based opon a known subtype string.
  213. a) Discovering and configuring TCMU UIO devices:
  214. (error checking omitted for brevity)
  215. int fd, dev_fd;
  216. char buf[256];
  217. unsigned long long map_len;
  218. void *map;
  219. fd = open("/sys/class/uio/uio0/name", O_RDONLY);
  220. ret = read(fd, buf, sizeof(buf));
  221. close(fd);
  222. buf[ret-1] = '\0'; /* null-terminate and chop off the \n */
  223. /* we only want uio devices whose name is a format we expect */
  224. if (strncmp(buf, "tcm-user", 8))
  225. exit(-1);
  226. /* Further checking for subtype also needed here */
  227. fd = open(/sys/class/uio/%s/maps/map0/size, O_RDONLY);
  228. ret = read(fd, buf, sizeof(buf));
  229. close(fd);
  230. str_buf[ret-1] = '\0'; /* null-terminate and chop off the \n */
  231. map_len = strtoull(buf, NULL, 0);
  232. dev_fd = open("/dev/uio0", O_RDWR);
  233. map = mmap(NULL, map_len, PROT_READ|PROT_WRITE, MAP_SHARED, dev_fd, 0);
  234. b) Waiting for events on the device(s)
  235. while (1) {
  236. char buf[4];
  237. int ret = read(dev_fd, buf, 4); /* will block */
  238. handle_device_events(dev_fd, map);
  239. }
  240. c) Managing the command ring
  241. #include <linux/target_core_user.h>
  242. int handle_device_events(int fd, void *map)
  243. {
  244. struct tcmu_mailbox *mb = map;
  245. struct tcmu_cmd_entry *ent = (void *) mb + mb->cmdr_off + mb->cmd_tail;
  246. int did_some_work = 0;
  247. /* Process events from cmd ring until we catch up with cmd_head */
  248. while (ent != (void *)mb + mb->cmdr_off + mb->cmd_head) {
  249. if (tcmu_hdr_get_op(ent->hdr.len_op) == TCMU_OP_CMD) {
  250. uint8_t *cdb = (void *)mb + ent->req.cdb_off;
  251. bool success = true;
  252. /* Handle command here. */
  253. printf("SCSI opcode: 0x%x\n", cdb[0]);
  254. /* Set response fields */
  255. if (success)
  256. ent->rsp.scsi_status = SCSI_NO_SENSE;
  257. else {
  258. /* Also fill in rsp->sense_buffer here */
  259. ent->rsp.scsi_status = SCSI_CHECK_CONDITION;
  260. }
  261. }
  262. else if (tcmu_hdr_get_op(ent->hdr.len_op) != TCMU_OP_PAD) {
  263. /* Tell the kernel we didn't handle unknown opcodes */
  264. ent->hdr.uflags |= TCMU_UFLAG_UNKNOWN_OP;
  265. }
  266. else {
  267. /* Do nothing for PAD entries except update cmd_tail */
  268. }
  269. /* update cmd_tail */
  270. mb->cmd_tail = (mb->cmd_tail + tcmu_hdr_get_len(&ent->hdr)) % mb->cmdr_size;
  271. ent = (void *) mb + mb->cmdr_off + mb->cmd_tail;
  272. did_some_work = 1;
  273. }
  274. /* Notify the kernel that work has been finished */
  275. if (did_some_work) {
  276. uint32_t buf = 0;
  277. write(fd, &buf, 4);
  278. }
  279. return 0;
  280. }
  281. A final note
  282. ------------
  283. Please be careful to return codes as defined by the SCSI
  284. specifications. These are different than some values defined in the
  285. scsi/scsi.h include file. For example, CHECK CONDITION's status code
  286. is 2, not 1.