target: userspace pass-through backend
Subsystems (5)
cover target: userspace pass-through backend
Shaohua Li wrote an initial implementation of this, late last year[1]. Starting from that, I started working on some alternate implementation choices, and ended up with something rather different.
Please take a look and let me know what you think. Patch 1 is a design and overview doc, and patch 2 is the actual code, along with implementation rationale.
Thanks -- Andy
[1] http://thread.gmane.org/gmane.linux.scsi.target.devel/5044
Andy Grover (2):
target: Add documentation on the target userspace pass-through driver
target: Add a user-passthrough backstore
Documentation/target/tcmu-design.txt | 210 +++++++ drivers/target/Kconfig | 5 + drivers/target/Makefile | 1 + drivers/target/target_core_transport.c | 4 + drivers/target/target_core_user.c | 1078 ++++++++++++++++++++++++++++++++ drivers/target/target_core_user.h | 126 ++++ 6 files changed, 1424 insertions(+) create mode 100644 Documentation/target/tcmu-design.txt create mode 100644 drivers/target/target_core_user.c create mode 100644 drivers/target/target_core_user.h
Signature · 1 line
1.9.3
RFC 1/2 target: Add documentation on the target userspace pass-through driver
Describes the driver and its interface to make it possible for user programs to back a LIO-exported LUN.
--- Documentation/target/tcmu-design.txt | 210 +++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 Documentation/target/tcmu-design.txt
Documentation/target/tcmu-design.txt new file +210 -0
@@ -0,0 +1,210 @@ +TCM Userspace Design +-------------------- + + +Background: + +In addition to modularizing the transport protocol used for carrying +SCSI commands ("fabrics"), the Linux kernel target, LIO, also modularizes +the actual data storage as well. These are referred to as "backstores" +or "storage engines". The target comes with backstores that allow a +file, a block device, RAM, or another SCSI device to be used for the +local storage needed for the exported SCSI LUN. Like the rest of LIO, +these are implemented entirely as kernel code. + +These backstores cover the most common use cases, but not all. One new +use case that other non-kernel target solutions, such as tgt, are able +to support is using Gluster's GLFS or Ceph's RBD as a backstore. The +target then serves as a translator, allowing initiators to store data +in these non-traditional networked storage systems, while still only +using standard protocols themselves. + +If the target is a userspace process, supporting these is easy. tgt, +for example, needs only a small adapter module for each, because the +modules just use the available userspace libraries for RBD and GLFS. + +Adding support for these backstores in LIO is considerably more +difficult, because LIO is entirely kernel code. Instead of undertaking +the significant work to port the GLFS or RBD APIs and protocols to the +kernel, another approach is to create a userspace pass-through +backstore for LIO, "TCMU". + + +Benefits: + +In addition to allowing relatively easy support for RBD and GLFS, TCMU +will also allow easier development of new backstores. TCMU combines +with the LIO loopback fabric to become something similar to FUSE +(Filesystem in Userspace), but at the SCSI layer instead of the +filesystem layer. A SUSE, if you will. + +The disadvantage is there are more distinct components to configure, and +potentially to malfunction. This is unavoidable, but hopefully not +fatal if we're careful to keep things as simple as possible. + +Design constraints: + +- Good performance: high throughput, low latency +- Cleanly handle if userspace: + 1) never attaches + 2) hangs + 3) dies + 4) misbehaves +- Allow future flexibility in user & kernel implementations +- Be reasonably memory-efficient +- Simple to configure & run +- Simple to write a userspace backend + + +Implementation overview: + +The core of the TCMU interface is a memory region that is shared +between kernel and userspace. Within this region is: a control area +(mailbox); a lockless producer/consumer circular buffer for commands +to be passed up, and status returned; and an in/out data buffer area. + +TCMU uses the pre-existing UIO subsystem. UIO allows device driver +development in userspace, and this is conceptually very close to the +TCMU use case, except instead of a physical device, TCMU implements a +memory-mapped layout designed for SCSI commands. Using UIO also +benefits TCMU by handling device introspection (e.g. a way for +userspace to determine how large the shared region is) and signaling +mechanisms in both directions. + +There are no embedded pointers in the memory region. Everything is +expressed as an offset from the region's starting address. This allows +the ring to still work if the user process dies and is restarted with +the region mapped at a different virtual address. + +See target_core_user.h for the struct definitions. + +The Mailbox: + +The mailbox is always at the start of the shared memory region, and +contains a version, details about the starting offset and size of the +command ring, and head and tail pointers to be used by the kernel and +userspace (respectively) to put commands on the ring, and indicate +when the commands are completed. + +version - 1 (userspace should abort if otherwise) +flags - none yet defined. +cmdr_off - The offset of the start of the command ring from the start +of the memory region, to account for the mailbox size. +cmdr_size - The size of the command ring. This does *not* need to be a +power of two. +cmd_head - Modified by the kernel to indicate when a command has been +placed on the ring. +cmd_tail - Modified by userspace to indicate when it has completed +processing of a command. + +The Command Ring: + +Commands are placed on the ring by the kernel incrementing +mailbox.cmd_head by the size of the command, modulo cmdr_size, and +then signaling userspace via uio_event_notify(). Once the command is +completed, userspace updates mailbox.cmd_tail in the same way and +signals the kernel via a 4-byte write(). When cmd_head equals +cmd_tail, the ring is empty -- no commands are currently waiting to be +processed by userspace. + +TCMU commands start with a common header containing "len_op", a 32-bit +value that stores the length, as well as the opcode in the lowest +unused bits. Currently only two opcodes are defined, TCMU_OP_PAD and +TCMU_OP_CMD. When userspace encounters a command with PAD opcode, it +should skip ahead by the bytes in "length". (The kernel inserts PAD +entries to ensure each CMD entry fits contigously into the circular +buffer.) + +When userspace handles a CMD, it finds the SCSI CDB (Command Data +Block) via tcmu_cmd_entry.req.cdb_off. This is an offset from the +start of the overall shared memory region, not the entry. The data +in/out buffers are accessible via tht req.iov[] array. Note that +each iov.iov_base is also an offset from the start of the region. + +TCMU currently does not support BIDI operations. + +When completing a command, userspace sets rsp.scsi_status, and +rsp.sense_buffer if necessary. Userspace then increments +mailbox.cmd_tail by entry.hdr.length (mod cmdr_size) and signals the +kernel via the UIO method, a 4-byte write to the file descriptor. + +The Data Area: + +This is shared-memory space after the command ring. The organization +of this area is not defined in the TCMU interface, and userspace +should access only the parts referenced by pending iovs. + + +Device Discovery: + +Other devices may be using UIO besides TCMU. Unrelated user processes +may also be handling different sets of TCMU devices. TCMU userspace +processes must find their devices by scanning sysfs +class/uio/uio*/name. For TCMU devices, these names will be of the +format: + +tcm-user/<subtype>/<path> + +where "tcm-user" is common for all TCMU-backed UIO devices. <subtype> +will be a userspace-process-unique string to identify the TCMU device +as expecting to be backed by a certain handler, and <path> will be an +additional handler-specific string for the user process to configure +the device, if needed. Neither <subtype> or <path> can contain ':', +due to LIO limitations. + +For all devices so discovered, the user handler opens /dev/uioX and +calls mmap(): + +mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0) + +where size must be equal to the value read from +/sys/class/uio/uioX/maps/map0/size. + + +Device Events: + +If a new device is added or removed, user processes will recieve a HUP +signal, and should re-scan sysfs. File descriptors for devices no +longer in sysfs should be closed, and new devices should be opened and +handled. + + +Other contingencies: + +Userspace handler process never attaches: + +- TCMU will post commands, and then abort them after a timeout period + (30 seconds.) + +Userspace handler process is killed: + +- It is still possible to restart and re-connect to TCMU + devices. Command ring is preserved. However, after the timeout period, + the kernel will abort pending tasks. + +Userspace handler process hangs: + +- The kernel will abort pending tasks after a timeout period. + +Userspace handler process is malicious: + +- The process can trivially break the handling of devices it controls, + but should not be able to access kernel memory outside its shared + memory areas. + + +Writing a user backstore handler: + +Variable emulation with pass_level: + +TCMU supports a "pass_level" option with valid values of 1, 2, or +3. This controls how many different SCSI commands are passed up, +versus being emulated by LIO. The purpose of this is to give the user +handler author a choice of how much of the full SCSI command set they +care to support. + +At level 1, only READ and WRITE commands will be seen. At level 2, +additional commands defined in the SBC SCSI specification such as +WRITE SAME, SYNCRONIZE CACHE, and UNMAP will be passed up. Finally, at +level 3, almost all commands defined in the SPC SCSI specification +will also be passed up for processing by the user handler.
Signature · 1 line
1.9.3
RFC 2/2 target: Add a user-passthrough backstore
In reply to Andy GroverAdd a LIO storage engine that presents commands to userspace for execution. This would allow more complex backstores to be implemented out-of-kernel, and also make experimentation a-la FUSE (but at the SCSI level -- "SUSE"?) possible.
It uses a mmap()able UIO device per LUN to share a command ring and data area. The commands are raw SCSI CDBs and iovs for in/out data. The command ring is also reused for scsi command status and sense data, if present.
This implementation is based on Shaohua Li's earlier version but heavily modified. Differences include:
* Shared memory allocated by kernel, not locked-down user pages
* Single ring for command request and response
* Offsets instead of embedded pointers
* Generic SCSI CDB passthrough instead of per-cmd specialization in ring format.
* Uses UIO device instead of anon_file passed in mailbox.
* Optional in-kernel handling of some commands.
The main reason for these differences is to permit greater resiliency if the user process dies or hangs.
Things not yet implemented (on purpose):
* Zero copy. The data area is flexible enough to allow page flipping or backend-allocated pages to be used by fabrics, but it's not clear these are performance wins. Can come later.
* Out-of-order command completion by userspace. Possible to add by just allowing userspace to change cmd_id in rsp cmd entries, but currently not supported.
* No locks between kernel cmd submission and completion routines. Sounds like it's possible, but this can come later.
* Sparse allocation of mmaped area. Current code vmallocs the whole thing. If the mapped area was larger and not fully mapped then the driver would have more freedom to change cmd and data area sizes based on demand.
Current code open issues:
* The use of idrs may be overkill -- we maybe can replace them with a simple counter to generate cmd_ids, and a hash table to get a cmd_id's associated pointer.
* Use of a free-running counter for cmd ring instead of explicit modulo math. This would require power-of-2 cmd ring size.
* Random printks in code, still.
--- drivers/target/Kconfig | 5 + drivers/target/Makefile | 1 + drivers/target/target_core_transport.c | 4 + drivers/target/target_core_user.c | 1078 ++++++++++++++++++++++++++++++++ drivers/target/target_core_user.h | 126 ++++ 5 files changed, 1214 insertions(+) create mode 100644 drivers/target/target_core_user.c create mode 100644 drivers/target/target_core_user.h
drivers/target/Kconfig +5 -0
@@ -31,6 +31,11 @@ config TCM_PSCSI Say Y here to enable the TCM/pSCSI subsystem plugin for non-buffered passthrough access to Linux/SCSI device +config TCM_USER + tristate "TCM/USER Subsystem Plugin for Linux" + help + Say Y here to enable the TCM/USER subsystem plugin + source "drivers/target/loopback/Kconfig" source "drivers/target/tcm_fc/Kconfig" source "drivers/target/iscsi/Kconfig"
drivers/target/Makefile +1 -0
@@ -22,6 +22,7 @@ obj-$(CONFIG_TARGET_CORE) += target_core_mod.o obj-$(CONFIG_TCM_IBLOCK) += target_core_iblock.o obj-$(CONFIG_TCM_FILEIO) += target_core_file.o obj-$(CONFIG_TCM_PSCSI) += target_core_pscsi.o +obj-$(CONFIG_TCM_USER) += target_core_user.o # Fabric modules obj-$(CONFIG_LOOPBACK_TARGET) += loopback/
drivers/target/target_core_transport.c +4 -0
@@ -232,6 +232,10 @@ void transport_subsystem_check_init(void)if (ret !=0 )pr_err ("Unable to load target_core_pscsi\n" ); + ret =request_module ("target_core_user" ); +if (ret !=0 ) +pr_err ("Unable to load target_core_user\n" ); + sub_api_initialized =1 ; }
drivers/target/target_core_user.c new file +1078 -0
1,078 lines not on this page · raw message
drivers/target/target_core_user.h new file +126 -0
126 lines not on this page · raw message
Signature · 1 line
1.9.3
Re: [RFC 1/2] target: Add documentation on the target userspace pass-through driver
In reply to Andy GroverReply inline, with a good bit of snipping done (posting via gmane, so quote/content ratio is an issue).
Documentation/target/tcmu-design.txt line 20
Quoted 6 lines · Andy Grover
Documentation/target/tcmu-design.txt new file +6
-0+These backstores cover the most common use cases, but not all. One new +use case that other non-kernel target solutions, such as tgt, are able +to support is using Gluster's GLFS or Ceph's RBD as a backstore. The +target then serves as a translator, allowing initiators to store data +in these non-traditional networked storage systems, while still only +using standard protocols themselves.
Another use case is in supporting various image formats, like (say) qcow2, and then handing those off to vhost_scsi.
Documentation/target/tcmu-design.txt line 39
Quoted 7 lines
Documentation/target/tcmu-design.txt new file +7
-0+Benefits: + +In addition to allowing relatively easy support for RBD and GLFS, TCMU +will also allow easier development of new backstores. TCMU combines +with the LIO loopback fabric to become something similar to FUSE +(Filesystem in Userspace), but at the SCSI layer instead of the +filesystem layer. A SUSE, if you will.
As long as people don't start calling it L[UNs in ]USER[space] :P
Between that and ABUSE (A Block device in USErspace), this domain has some real naming potential...
Quoted 6 lines
Documentation/target/tcmu-design.txt new file +6
-0+Device Discovery: + +Other devices may be using UIO besides TCMU. Unrelated user processes +may also be handling different sets of TCMU devices. TCMU userspace +processes must find their devices by scanning sysfs +class/uio/uio*/name. For TCMU devices, these names will be of the
Documentation/target/tcmu-design.txt line 153
Quoted 10 lines
Documentation/target/tcmu-design.txt new file +10
-0+format: + +tcm-user/<subtype>/<path> + +where "tcm-user" is common for all TCMU-backed UIO devices. <subtype> +will be a userspace-process-unique string to identify the TCMU device +as expecting to be backed by a certain handler, and <path> will be an +additional handler-specific string for the user process to configure +the device, if needed. Neither <subtype> or <path> can contain ':', +due to LIO limitations.
It might be good to change this somewhat; in the vast majority of cases it'd be saner for userspace programs to figure this information out via udev etc. rather than parsing sysfs themselves. This information is still worth documenting, but saying things like "must find their devices by scanning sysfs" is likely to lead to users of this interface making suboptimal choices.
Documentation/target/tcmu-design.txt line 169
Quoted 6 lines
Documentation/target/tcmu-design.txt new file +6
-0+Device Events: + +If a new device is added or removed, user processes will recieve a HUP +signal, and should re-scan sysfs. File descriptors for devices no +longer in sysfs should be closed, and new devices should be opened and +handled.
Is there a cleaner way to do this? In particular, re-scanning sysfs may cause race conditions (device removed, one of the same name re-added but a different UIO device node; probably more to be found). Perhaps recommend netlink uevents, so that remove+add is noticeable? Also, is the SIGHUP itself the best option? Could we simply require the user process to listen for add/remove uevents to get such change notifications, and thus enforce good behavior?
Quoted 5 lines
Documentation/target/tcmu-design.txt new file +5
-0+Writing a user backstore handler: + +Variable emulation with pass_level: + +TCMU supports a "pass_level" option with valid values of 1, 2, or
Documentation/target/tcmu-design.txt line 210
Quoted 10 lines
Documentation/target/tcmu-design.txt new file +10
-0+3. This controls how many different SCSI commands are passed up, +versus being emulated by LIO. The purpose of this is to give the user +handler author a choice of how much of the full SCSI command set they +care to support. + +At level 1, only READ and WRITE commands will be seen. At level 2, +additional commands defined in the SBC SCSI specification such as +WRITE SAME, SYNCRONIZE CACHE, and UNMAP will be passed up. Finally, at +level 3, almost all commands defined in the SPC SCSI specification +will also be passed up for processing by the user handler.
One use case I'm actually interested in is having userspace provide something other than just SPC - for instance, tgt can provide a virtual tape library or an OSD, and CDemu can provide emulated optical discs from various image formats.
Currently, CDemu uses its own out-of-tree driver called VHBA (Virtual Host Bus Adapter) to do pretty much exactly what TCMU+Loopback would accomplish... and in the process misses out on all of the other fabrics, unless you're willing to _re-import_ those devices using PSCSI, which has its own quirks.
Perhaps there could be a level 0 (or 4, or whatever) which means "explicitly enabled list of commands" - maybe as a bitmap that could be passed to the kernel somehow? Hopefully, that could also avoid some of the quirks of PSCSI regarding ALUA and such - if it's not implemented, leave the relevant bits at zero, and LIO handles it.
This does look really nice, thanks for writing it!
Hi, here's some additional beginning bits for the configuration and use of this code:
Changes to targetcli-fb and rtslib-fb:
https://github.com/agrover/targetcli-fb/tree/userback
https://github.com/agrover/rtslib-fb/tree/userback
An incredibly-crappy stand-in for the user process that will handle requests:
https://github.com/agrover/test-userback
I look forward to your feedback :)
Thanks -- Regards -- Andy
p.s. kernel changes are also available here:
https://github.com/agrover/linux/tree/userback
Re: [RFC 1/2] target: Add documentation on the target userspace pass-through driver
In reply to Alex Elsayed[re-adding individual CCs that were dropped]
Quoted 15 lines · Alex Elsayed
Quoted 15 lines
Documentation/target/tcmu-design.txt new file +15
-0+Device Discovery: + +Other devices may be using UIO besides TCMU. Unrelated user processes +may also be handling different sets of TCMU devices. TCMU userspace +processes must find their devices by scanning sysfs +class/uio/uio*/name. For TCMU devices, these names will be of the +format: + +tcm-user/<subtype>/<path> + +where "tcm-user" is common for all TCMU-backed UIO devices. <subtype> +will be a userspace-process-unique string to identify the TCMU device +as expecting to be backed by a certain handler, and <path> will be an +additional handler-specific string for the user process to configure +the device, if needed. Neither <subtype> or <path> can contain ':',
Quoted 8 lines
Quoted 1 line
+due to LIO limitations.
It might be good to change this somewhat; in the vast majority of cases it'd be saner for userspace programs to figure this information out via udev etc. rather than parsing sysfs themselves. This information is still worth documenting, but saying things like "must find their devices by scanning sysfs" is likely to lead to users of this interface making suboptimal choices.
I agree. There's no getting around a certain degree of work required by the backing user program. I'm planning on writing a "tcmu-runner" program with a plugin interface, that will handle the event loop, device notifications, enumeration, and possibly thread pools, to minimize the amount of boilerplate code each implementation must contain.
Quoted 7 lines
Documentation/target/tcmu-design.txt line 169
Quoted 6 lines
Documentation/target/tcmu-design.txt new file +6
-0+Device Events: + +If a new device is added or removed, user processes will recieve a HUP +signal, and should re-scan sysfs. File descriptors for devices no +longer in sysfs should be closed, and new devices should be opened and +handled.
Quoted 7 lines
Is there a cleaner way to do this? In particular, re-scanning sysfs may cause race conditions (device removed, one of the same name re-added but a different UIO device node; probably more to be found). Perhaps recommend netlink uevents, so that remove+add is noticeable? Also, is the SIGHUP itself the best option? Could we simply require the user process to listen for add/remove uevents to get such change notifications, and thus enforce good behavior?
Yes this sounds better, let's do it this way.
Quoted 11 lines
One use case I'm actually interested in is having userspace provide something other than just SPC - for instance, tgt can provide a virtual tape library or an OSD, and CDemu can provide emulated optical discs from various image formats.
Currently, CDemu uses its own out-of-tree driver called VHBA (Virtual Host Bus Adapter) to do pretty much exactly what TCMU+Loopback would accomplish... and in the process misses out on all of the other fabrics, unless you're willing to _re-import_ those devices using PSCSI, which has its own quirks.
Quoted 5 lines
Perhaps there could be a level 0 (or 4, or whatever) which means "explicitly enabled list of commands" - maybe as a bitmap that could be passed to the kernel somehow? Hopefully, that could also avoid some of the quirks of PSCSI regarding ALUA and such - if it's not implemented, leave the relevant bits at zero, and LIO handles it.
I'm beginning to sour on pass_level and having configurable cmd filtering in the kernel interface.
I think a less-clever but simpler approach might be to eliminate filtering, and the user process can return CHECK_CONDITION, INVALID COMMAND OPERATION CODE for commands it doesn't wish to support. TCMU checks for this, and the pending command thus returned can still be emulated by LIO (it looks like we could just re-call sbc_parse_cdb and target_execute_cmd).
Quoted 1 line
This does look really nice, thanks for writing it!
Thanks for your helpful feedback! :)
-- Andy
Quoted 16 lines · Andy Grover
Shaohua Li wrote an initial implementation of this, late last year[1]. Starting from that, I started working on some alternate implementation choices, and ended up with something rather different.
Please take a look and let me know what you think. Patch 1 is a design and overview doc, and patch 2 is the actual code, along with implementation rationale.
Thanks -- Andy
[1] http://thread.gmane.org/gmane.linux.scsi.target.devel/5044
Andy Grover (2):
target: Add documentation on the target userspace pass-through driver
target: Add a user-passthrough backstore
Quoted 10 lines
Documentation/target/tcmu-design.txt | 210 +++++++ drivers/target/Kconfig | 5 + drivers/target/Makefile | 1 + drivers/target/target_core_transport.c | 4 + drivers/target/target_core_user.c | 1078 ++++++++++++++++++++++++++++++++ drivers/target/target_core_user.h | 126 ++++ 6 files changed, 1424 insertions(+) create mode 100644 Documentation/target/tcmu-design.txt create mode 100644 drivers/target/target_core_user.c create mode 100644 drivers/target/target_core_user.h
Hi Andy,
Just wanted to let you know that a userspace backstore would potentially be useful for QEMU. QEMU supports a number of disk image formats (VMDK, VHDX, qcow2, and more). Make these available as SCSI LUNs on the host or to remote SCSI initiators is cool.
We currently have a tool called qemu-nbd that exports disk images using the Network Block Device protocol. Your userspace backstore provides other options like iSCSI target or loopback access on the host.
I took a quick look at the patch and imagine it's not hard to hook up to QEMU. Looks promising!
Stefan
v2 cover Userspace pass-through storage engine (backend)
In reply to Andy GroverHi all,
Here is version two of the userspace passthrough backstore code I've been working on.
You can find some documentation on the design in last patch. Please let me know what you think about the design and the code.
Thanks -- Andy
Changes since version 1:
* Use netlink for reporting device add/removes to userspace
* Remove pass_level in favor of automatic emulation of commands when userspace doesn't support them
* Implemented 'tcmu-runner' daemon to handle the user side of the interface and make writing handlers easier (not in this patchset, see https://github.com/agrover/tcmu-runner).
* Many bug fixes. Can now successfully partition, format, and use a TCMU-backed volume.
* Two small patches added for other changes we need.
Operation:
* Using https://github.com/agrover/targetcli-fb/commits/userback and
https://github.com/agrover/rtslib-fb/commits/userback, create a user storage object and give it a size, and 'file/foo.img' as config parameter
* Start tcmu-runner (manually for now)
* Create a loopback fabric and link the above storage object to it
Andy Grover (4):
target: Remove unneeded check in sbc_parse_cdb
uio: Export definition of struct uio_device
target: Add a user-passthrough backstore
target: Add documentation on the target userspace pass-through driver
Documentation/target/tcmu-design.txt | 229 +++++++ drivers/target/Kconfig | 5 + drivers/target/Makefile | 1 + drivers/target/target_core_sbc.c | 2 +- drivers/target/target_core_transport.c | 4 + drivers/target/target_core_user.c | 1158 ++++++++++++++++++++++++++++++++ drivers/uio/uio.c | 12 - include/linux/uio_driver.h | 12 +- include/uapi/linux/Kbuild | 1 + include/uapi/linux/target_core_user.h | 142 ++++ 10 files changed, 1552 insertions(+), 14 deletions(-) create mode 100644 Documentation/target/tcmu-design.txt create mode 100644 drivers/target/target_core_user.c create mode 100644 include/uapi/linux/target_core_user.h
Signature · 1 line
1.9.3
RFC v2 1/4 target: Remove unneeded check in sbc_parse_cdb
The check of SCF_SCSI_DATA_CDB seems to be a remnant from before hch's refactoring of this function. There are no places where that flag is set that cmd->execute_cmd isn't also set.
--- drivers/target/target_core_sbc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/target/target_core_sbc.c +1 -1
@@ -948,7 +948,7 @@ sbc_parse_cdb(struct se_cmd *cmd, struct sbc_ops *ops) }/* reject any command that we don't have a handler for */ -if (!(cmd->se_cmd_flags &SCF_SCSI_DATA_CDB ) && !cmd->execute_cmd) +if (!cmd->execute_cmd)return TCM_UNSUPPORTED_SCSI_OPCODE ;if (cmd->se_cmd_flags &SCF_SCSI_DATA_CDB ) {
Signature · 1 line
1.9.3
RFC v2 3/4 target: Add a user-passthrough backstore
In reply to Andy GroverAdd a LIO storage engine that presents commands to userspace for execution. This would allow more complex backstores to be implemented out-of-kernel, and also make experimentation a-la FUSE (but at the SCSI level -- "SUSE"?) possible.
It uses a mmap()able UIO device per LUN to share a command ring and data area. The commands are raw SCSI CDBs and iovs for in/out data. The command ring is also reused for scsi command status and sense data, if present.
This implementation is based on Shaohua Li's earlier version but heavily modified. Differences include:
* Shared memory allocated by kernel, not locked-down user pages
* Single ring for command request and response
* Offsets instead of embedded pointers
* Generic SCSI CDB passthrough instead of per-cmd specialization in ring format.
* Uses UIO device instead of anon_file passed in mailbox.
* Optional in-kernel handling of some commands.
The main reason for these differences is to permit greater resiliency if the user process dies or hangs.
Things not yet implemented (on purpose):
* Zero copy. The data area is flexible enough to allow page flipping or backend-allocated pages to be used by fabrics, but it's not clear these are performance wins. Can come later.
* Out-of-order command completion by userspace. Possible to add by just allowing userspace to change cmd_id in rsp cmd entries, but currently not supported.
* No locks between kernel cmd submission and completion routines. Sounds like it's possible, but this can come later.
* Sparse allocation of mmaped area. Current code vmallocs the whole thing. If the mapped area was larger and not fully mapped then the driver would have more freedom to change cmd and data area sizes based on demand.
Current code open issues:
* The use of idrs may be overkill -- we maybe can replace them with a simple counter to generate cmd_ids, and a hash table to get a cmd_id's associated pointer.
* Use of a free-running counter for cmd ring instead of explicit modulo math. This would require power-of-2 cmd ring size.
* Random printks in code, still.
--- drivers/target/Kconfig | 5 + drivers/target/Makefile | 1 + drivers/target/target_core_transport.c | 4 + drivers/target/target_core_user.c | 1158 ++++++++++++++++++++++++++++++++ include/uapi/linux/Kbuild | 1 + include/uapi/linux/target_core_user.h | 142 ++++ 6 files changed, 1311 insertions(+) create mode 100644 drivers/target/target_core_user.c create mode 100644 include/uapi/linux/target_core_user.h
drivers/target/Kconfig +5 -0
@@ -31,6 +31,11 @@ config TCM_PSCSI Say Y here to enable the TCM/pSCSI subsystem plugin for non-buffered passthrough access to Linux/SCSI device +config TCM_USER + tristate "TCM/USER Subsystem Plugin for Linux" + help + Say Y here to enable the TCM/USER subsystem plugin + source "drivers/target/loopback/Kconfig" source "drivers/target/tcm_fc/Kconfig" source "drivers/target/iscsi/Kconfig"
drivers/target/Makefile +1 -0
@@ -22,6 +22,7 @@ obj-$(CONFIG_TARGET_CORE) += target_core_mod.o obj-$(CONFIG_TCM_IBLOCK) += target_core_iblock.o obj-$(CONFIG_TCM_FILEIO) += target_core_file.o obj-$(CONFIG_TCM_PSCSI) += target_core_pscsi.o +obj-$(CONFIG_TCM_USER) += target_core_user.o # Fabric modules obj-$(CONFIG_LOOPBACK_TARGET) += loopback/
drivers/target/target_core_transport.c +4 -0
@@ -232,6 +232,10 @@ void transport_subsystem_check_init(void)if (ret !=0 )pr_err ("Unable to load target_core_pscsi\n" ); + ret =request_module ("target_core_user" ); +if (ret !=0 ) +pr_err ("Unable to load target_core_user\n" ); + sub_api_initialized =1 ; }
drivers/target/target_core_user.c new file +1158 -0
1,158 lines not on this page · raw message
include/uapi/linux/Kbuild +1 -0
@@ -370,6 +370,7 @@ header-y += swab.h header-y += synclink.h header-y += sysctl.h header-y += sysinfo.h +header-y += target_core_user.h header-y += taskstats.h header-y += tcp.h header-y += tcp_metrics.h
include/uapi/linux/target_core_user.h new file +142 -0
142 lines not on this page · raw message
Signature · 1 line
1.9.3
RFC v2 4/4 target: Add documentation on the target userspace pass-through driver
In reply to Andy GroverDescribes the driver and its interface to make it possible for user programs to back a LIO-exported LUN.
--- Documentation/target/tcmu-design.txt | 229 +++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 Documentation/target/tcmu-design.txt
Documentation/target/tcmu-design.txt new file +229 -0
@@ -0,0 +1,229 @@ +TCM Userspace Design +-------------------- + + +Background: + +In addition to modularizing the transport protocol used for carrying +SCSI commands ("fabrics"), the Linux kernel target, LIO, also modularizes +the actual data storage as well. These are referred to as "backstores" +or "storage engines". The target comes with backstores that allow a +file, a block device, RAM, or another SCSI device to be used for the +local storage needed for the exported SCSI LUN. Like the rest of LIO, +these are implemented entirely as kernel code. + +These backstores cover the most common use cases, but not all. One new +use case that other non-kernel target solutions, such as tgt, are able +to support is using Gluster's GLFS or Ceph's RBD as a backstore. The +target then serves as a translator, allowing initiators to store data +in these non-traditional networked storage systems, while still only +using standard protocols themselves. + +If the target is a userspace process, supporting these is easy. tgt, +for example, needs only a small adapter module for each, because the +modules just use the available userspace libraries for RBD and GLFS. + +Adding support for these backstores in LIO is considerably more +difficult, because LIO is entirely kernel code. Instead of undertaking +the significant work to port the GLFS or RBD APIs and protocols to the +kernel, another approach is to create a userspace pass-through +backstore for LIO, "TCMU". + + +Benefits: + +In addition to allowing relatively easy support for RBD and GLFS, TCMU +will also allow easier development of new backstores. TCMU combines +with the LIO loopback fabric to become something similar to FUSE +(Filesystem in Userspace), but at the SCSI layer instead of the +filesystem layer. A SUSE, if you will. + +The disadvantage is there are more distinct components to configure, and +potentially to malfunction. This is unavoidable, but hopefully not +fatal if we're careful to keep things as simple as possible. + +Design constraints: + +- Good performance: high throughput, low latency +- Cleanly handle if userspace: + 1) never attaches + 2) hangs + 3) dies + 4) misbehaves +- Allow future flexibility in user & kernel implementations +- Be reasonably memory-efficient +- Simple to configure & run +- Simple to write a userspace backend + + +Implementation overview: + +The core of the TCMU interface is a memory region that is shared +between kernel and userspace. Within this region is: a control area +(mailbox); a lockless producer/consumer circular buffer for commands +to be passed up, and status returned; and an in/out data buffer area. + +TCMU uses the pre-existing UIO subsystem. UIO allows device driver +development in userspace, and this is conceptually very close to the +TCMU use case, except instead of a physical device, TCMU implements a +memory-mapped layout designed for SCSI commands. Using UIO also +benefits TCMU by handling device introspection (e.g. a way for +userspace to determine how large the shared region is) and signaling +mechanisms in both directions. + +There are no embedded pointers in the memory region. Everything is +expressed as an offset from the region's starting address. This allows +the ring to still work if the user process dies and is restarted with +the region mapped at a different virtual address. + +See target_core_user.h for the struct definitions. + +The Mailbox: + +The mailbox is always at the start of the shared memory region, and +contains a version, details about the starting offset and size of the +command ring, and head and tail pointers to be used by the kernel and +userspace (respectively) to put commands on the ring, and indicate +when the commands are completed. + +version - 1 (userspace should abort if otherwise) +flags - none yet defined. +cmdr_off - The offset of the start of the command ring from the start +of the memory region, to account for the mailbox size. +cmdr_size - The size of the command ring. This does *not* need to be a +power of two. +cmd_head - Modified by the kernel to indicate when a command has been +placed on the ring. +cmd_tail - Modified by userspace to indicate when it has completed +processing of a command. + +The Command Ring: + +Commands are placed on the ring by the kernel incrementing +mailbox.cmd_head by the size of the command, modulo cmdr_size, and +then signaling userspace via uio_event_notify(). Once the command is +completed, userspace updates mailbox.cmd_tail in the same way and +signals the kernel via a 4-byte write(). When cmd_head equals +cmd_tail, the ring is empty -- no commands are currently waiting to be +processed by userspace. + +TCMU commands start with a common header containing "len_op", a 32-bit +value that stores the length, as well as the opcode in the lowest +unused bits. Currently only two opcodes are defined, TCMU_OP_PAD and +TCMU_OP_CMD. When userspace encounters a command with PAD opcode, it +should skip ahead by the bytes in "length". (The kernel inserts PAD +entries to ensure each CMD entry fits contigously into the circular +buffer.) + +When userspace handles a CMD, it finds the SCSI CDB (Command Data +Block) via tcmu_cmd_entry.req.cdb_off. This is an offset from the +start of the overall shared memory region, not the entry. The data +in/out buffers are accessible via tht req.iov[] array. Note that +each iov.iov_base is also an offset from the start of the region. + +TCMU currently does not support BIDI operations. + +When completing a command, userspace sets rsp.scsi_status, and +rsp.sense_buffer if necessary. Userspace then increments +mailbox.cmd_tail by entry.hdr.length (mod cmdr_size) and signals the +kernel via the UIO method, a 4-byte write to the file descriptor. + +The Data Area: + +This is shared-memory space after the command ring. The organization +of this area is not defined in the TCMU interface, and userspace +should access only the parts referenced by pending iovs. + + +Device Discovery: + +Other devices may be using UIO besides TCMU. Unrelated user processes +may also be handling different sets of TCMU devices. TCMU userspace +processes must find their devices by scanning sysfs +class/uio/uio*/name. For TCMU devices, these names will be of the +format: + +tcm-user/<hba_num>/<device_name>/<subtype>/<path> + +where "tcm-user" is common for all TCMU-backed UIO devices. <hba_num> +and <device_name> allow userspace to find the device's path in the +kernel target's configfs tree. Assuming the usual mount point, it is +found at: + +/sys/kernel/config/target/core/user_<hba_num>/<device_name> + +This location contains attributes such as "hw_block_size", that +userspace needs to know for correct operation. + +<subtype> will be a userspace-process-unique string to identify the +TCMU device as expecting to be backed by a certain handler, and <path> +will be an additional handler-specific string for the user process to +configure the device, if needed. The name cannot contain ':', due to +LIO limitations. + +For all devices so discovered, the user handler opens /dev/uioX and +calls mmap(): + +mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0) + +where size must be equal to the value read from +/sys/class/uio/uioX/maps/map0/size. + + +Device Events: + +If a new device is added or removed, a notification will be broadcast +over netlink, using a generic netlink family name of "TCM-USER" and a +multicast group named "config". This will include the UIO name as +described in the previous section, as well as the UIO minor +number. This should allow userspace to identify both the UIO device and +the LIO device, so that after determining the device is supported +(based on subtype) it can take the appropriate action. + + +Other contingencies: + +Userspace handler process never attaches: + +- TCMU will post commands, and then abort them after a timeout period + (30 seconds.) + +Userspace handler process is killed: + +- It is still possible to restart and re-connect to TCMU + devices. Command ring is preserved. However, after the timeout period, + the kernel will abort pending tasks. + +Userspace handler process hangs: + +- The kernel will abort pending tasks after a timeout period. + +Userspace handler process is malicious: + +- The process can trivially break the handling of devices it controls, + but should not be able to access kernel memory outside its shared + memory areas. + + +Writing a user backstore handler: + +First, consider instead writing a plugin for tcmu-runner. tcmu-runner +implements the device enumeration and ring processing, and provides a +higher-level API to plugin writers. If you're determined to start from +scratch, it should at least provide working reference code for using +the user-kernel interface. Multiple unrelated processes can run +concurrently with disjoint sets of TCMU devices, as long as they +differentiate "their" devices based on the subtype string. + +All SCSI commands that the device receives are presented to userspace +via the command ring. + +However, in order to reduce the amount of boilerplate code needed, +handlers can opt to complete commands with CHECK CONDITION: INVALID +COMMAND OPERATION CODE and TCMU will attempt to emulate it using LIO's +in-kernel support. See tcmu-runner for an example of this. + +Finally, be careful to return codes as defined by the SCSI +specifications. These are different than some values defined in the +scsi/scsi.h include file. For example, CHECK CONDITION's status code +is 2, not 1.
Signature · 1 line
1.9.3
RFC v2 2/4 uio: Export definition of struct uio_device
In reply to Andy GroverIn order to prevent a O(n) search of the filesystem to link up its uio node with its target configuration, TCMU needs to know the minor number that UIO assigned. Expose the definition of this struct so TCMU can access this field.
--- drivers/uio/uio.c | 12 ------------ include/linux/uio_driver.h | 12 +++++++++++- 2 files changed, 11 insertions(+), 13 deletions(-)
drivers/uio/uio.c +0 -12
@@ -28,18 +28,6 @@#define UIO_MAX_DEVICES (1U << MINORBITS) -struct uio_device { -struct module *owner; -struct device *dev; -int minor; -atomic_t event; -struct fasync_struct *async_queue; -wait_queue_head_t wait; -struct uio_info *info; -struct kobject *map_dir; -struct kobject *portio_dir; -}; -static int uio_major;static struct cdev *uio_cdev;static DEFINE_IDR (uio_idr);
include/linux/uio_driver.h +11 -1
@@ -63,7 +63,17 @@ struct uio_port {#define MAX_UIO_PORT_REGIONS 5 -struct uio_device ; +struct uio_device { +struct module *owner; +struct device *dev; +int minor; +atomic_t event; +struct fasync_struct *async_queue; +wait_queue_head_t wait; +struct uio_info *info; +struct kobject *map_dir; +struct kobject *portio_dir; +};/** * struct uio_info - UIO device capabilities
Signature · 1 line
1.9.3
Re: [RFC 1/2] target: Add documentation on the target userspace pass-through driver
In reply to Andy GroverQuoted 35 lines · Andy Grover
Describes the driver and its interface to make it possible for user programs to back a LIO-exported LUN.
Signed-off-by Andy Grover <[email protected]>--- Documentation/target/tcmu-design.txt | 210 +++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 Documentation/target/tcmu-design.txtDocumentation/target/tcmu-design.txt new file +20
-0@@ -0,0 +1,210 @@ +TCM Userspace Design +-------------------- + + +Background: + +In addition to modularizing the transport protocol used for carrying +SCSI commands ("fabrics"), the Linux kernel target, LIO, also modularizes +the actual data storage as well. These are referred to as "backstores" +or "storage engines". The target comes with backstores that allow a +file, a block device, RAM, or another SCSI device to be used for the +local storage needed for the exported SCSI LUN. Like the rest of LIO, +these are implemented entirely as kernel code. + +These backstores cover the most common use cases, but not all. One new +use case that other non-kernel target solutions, such as tgt, are able +to support is using Gluster's GLFS or Ceph's RBD as a backstore. The +target then serves as a translator, allowing initiators to store data +in these non-traditional networked storage systems, while still only +using standard protocols themselves.
Documentation/target/tcmu-design.txt line 30
Quoted 10 lines
Documentation/target/tcmu-design.txt new file +10
-0+ +If the target is a userspace process, supporting these is easy. tgt, +for example, needs only a small adapter module for each, because the +modules just use the available userspace libraries for RBD and GLFS. + +Adding support for these backstores in LIO is considerably more +difficult, because LIO is entirely kernel code. Instead of undertaking +the significant work to port the GLFS or RBD APIs and protocols to the +kernel, another approach is to create a userspace pass-through +backstore for LIO, "TCMU".
It has to be said that this documentation is terrible.
Jumping "in medias res"[1] is great for fiction, awful for technical documentation.
I would recommend the Economist Style Guide[2]. They always say "Barak Obama, President of the United States" the first time he is mentioned in an article, even though almost everyone knows who Barak Obama is.
In this case you're leaping into something .. fabrics, LIO, backstores, target solutions, ... aargh. Explain what you mean by each term and how it all fits together.
Thanks,
Rich.
[1] https://en.wikipedia.org/wiki/In_medias_res
[2] http://www.economist.com/styleguide/introduction
Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Re: [RFC 1/2] target: Add documentation on the target userspace pass-through driver
Quoted 52 lines · Richard W.M. Jones
Quoted 35 lines · Andy Grover
Describes the driver and its interface to make it possible for user programs to back a LIO-exported LUN.
Signed-off-by Andy Grover <[email protected]>--- Documentation/target/tcmu-design.txt | 210 +++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+)create mode 100644
Documentation/target/tcmu-design.txtDocumentation/target/tcmu-design.txt new file +20
-0@@ -0,0 +1,210 @@ +TCM Userspace Design +-------------------- + + +Background: + +In addition to modularizing the transport protocol used for carrying +SCSI commands ("fabrics"), the Linux kernel target, LIO, also modularizes +the actual data storage as well. These are referred to as "backstores" +or "storage engines". The target comes with backstores that allow a +file, a block device, RAM, or another SCSI device to be used for the +local storage needed for the exported SCSI LUN. Like the rest of LIO, +these are implemented entirely as kernel code. + +These backstores cover the most common use cases, but not all. One new +use case that other non-kernel target solutions, such as tgt, are able +to support is using Gluster's GLFS or Ceph's RBD as a backstore. The +target then serves as a translator, allowing initiators to store data +in these non-traditional networked storage systems, while still only +using standard protocols themselves.Documentation/target/tcmu-design.txt line 30
Quoted 10 lines
Documentation/target/tcmu-design.txt new file +10
-0+ +If the target is a userspace process, supporting these is easy. tgt, +for example, needs only a small adapter module for each, because the +modules just use the available userspace libraries for RBD and GLFS. + +Adding support for these backstores in LIO is considerably more +difficult, because LIO is entirely kernel code. Instead of undertaking +the significant work to port the GLFS or RBD APIs and protocols to the +kernel, another approach is to create a userspace pass-through +backstore for LIO, "TCMU".It has to be said that this documentation is terrible.
Jumping "in medias res"[1] is great for fiction, awful for technical documentation.
Quoted 8 lines
I would recommend the Economist Style Guide[2]. They always say "Barak Obama, President of the United States" the first time he is mentioned in an article, even though almost everyone knows who Barak Obama is.
In this case you're leaping into something .. fabrics, LIO, backstores, target solutions, ... aargh. Explain what you mean by each term and how it all fits together.
Thanks for the feedback. I am undoubtedly too close to the details, because I thought I *was* explaining things :)
This doc is for people like you -- tech-savvy but unfamiliar with this specific area. Would you be so kind as to point out exactly the terms this document should explain? Should it explain SCSI and SCSI commands? What a SCSI target is? Say "target implementations" rather than "target solutions"? Do I need some ASCII art?
Or, if in finding these gaps you've actually picked up the jargon and wanted to just take a pass a rewriting it, that would be fine too :) I've read some of your libguestfs docs and they were very understandable.
Regards -- Andy
Re: [RFC 1/2] target: Add documentation on the target userspace pass-through driver
Quoted 2 lines · Andy Grover
Thanks for the feedback. I am undoubtedly too close to the details, because I thought I *was* explaining things :)
Yeah, sorry it came across as a bit harsh.
Benoit did explain it to me so I understood it in the end (I think!)
Quoted 5 lines
This doc is for people like you -- tech-savvy but unfamiliar with this specific area. Would you be so kind as to point out exactly the terms this document should explain? Should it explain SCSI and SCSI commands? What a SCSI target is? Say "target implementations" rather than "target solutions"? Do I need some ASCII art?
So I can only speak for myself here, but I'm pretty familiar with iSCSI, using it, and some of the internals -- in fact I'm using the Linux kernel target daily.
Quoted 5 lines
TCM Userspace Design
In addition to modularizing the transport protocol used for carrying SCSI commands ("fabrics"), the Linux kernel target, LIO, also modularizes the actual data storage as well. These are referred to as "backstores" or "storage engines".
Reading this several times, I now think I get what it's trying to say, but I think it needs to introduces the terms (as the Economist style does). Something like this:
"TCM is the new name for LIO, an in-kernel iSCSI target (server). Existing TCM targets run in the kernel. TCMU (TCM in Userspace) allows userspace programs to be written which act as iSCSI targets. This document describes the design.
The existing kernel provides modules for different SCSI transport protocols. TCM also modularizes the data storage. There are existing modules for file, block device, RAM or using another SCSI device as storage. These are called "backstores" or "storage engines". These built-in modules are implemented entirely as kernel code."
And hopefully having defined a bit of background, the rest of the document just flows nicely:
Quoted 6 lines
These backstores cover the most common use cases, but not all. One new use case that other non-kernel target solutions, such as tgt, are able to support is using Gluster's GLFS or Ceph's RBD as a backstore. The target then serves as a translator, allowing initiators to store data in these non-traditional networked storage systems, while still only using standard protocols themselves.
Rich.
Signature · 5 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
Re: [RFC 1/2] target: Add documentation on the target userspace pass-through driver
Quoted 16 lines · Richard W.M. Jones
Reading this several times, I now think I get what it's trying to say, but I think it needs to introduces the terms (as the Economist style does). Something like this:
"TCM is the new name for LIO, an in-kernel iSCSI target (server). Existing TCM targets run in the kernel. TCMU (TCM in Userspace) allows userspace programs to be written which act as iSCSI targets. This document describes the design.
The existing kernel provides modules for different SCSI transport protocols. TCM also modularizes the data storage. There are existing modules for file, block device, RAM or using another SCSI device as storage. These are called "backstores" or "storage engines". These built-in modules are implemented entirely as kernel code."
Quoted 2 lines
And hopefully having defined a bit of background, the rest of the document just flows nicely:
Thanks much! I've put this in the doc, and will hopefully send out a final patchset for inclusion with the new text in the next week or so.
<begin naming potential-bikeshed wasteoftime>
The only change I made was "another" instead of "the new name" -- because to be honest I don't know if there was actually an attempt at a name change, and if so if it was successful or not :) LIO seems to have stuck, but TCM seems to refer just to the "backend" part of LIO.
<end bikeshed>
Thanks again -- Andy