nvmeof API#
Controller and namespace#
Userspace NVMe over Fabrics RDMA with direct host and GPU I/O.
- class nvmeof.Completion(result, sq_head, sq_id, command_id, status_field)#
Bases:
objectDecoded 16-byte NVMe completion queue entry.
status_fieldretains the phase bit exactly as received. The derived status properties remove that bit and expose the NVMe status code, status code type, and do-not-retry flag.- classmethod from_bytes(data)#
Decode the first 16 bytes of an NVMe completion queue entry.
- Parameters:
data (bytes) –
- Return type:
- raise_for_status()#
Raise
NVMeStatusErrorunless the command succeeded.- Return type:
None
- class nvmeof.Controller(host, subsystem_nqn, *, port=4420, host_id=None, host_nqn=None, queue_depth=128, keep_alive_ms=0, timeout=30.0, source=None)#
Bases:
objectA connected userspace NVMe-oF/RDMA controller.
Construction connects an admin queue, enables and identifies the remote controller, then creates one direct I/O queue. Use
connect()for a descriptive constructor or instantiate the class directly. Controllers are context managers and should be closed after every registered buffer.- Parameters:
host (str) – Target hostname or IP address.
subsystem_nqn (str) – NQN of the target subsystem to connect to.
port – NVMe/RDMA service port, normally
nvmeof.protocol.NVME_RDMA_PORT.host_id – Stable initiator UUID. A random UUID is generated by default.
host_nqn – Initiator NQN. By default it is derived from
host_id.queue_depth – Requested I/O queue depth from 2 through 256. The target may negotiate a smaller depth.
keep_alive_ms – Keep-alive timeout. Only zero is currently supported.
timeout – Command timeout in seconds.
source – Optional initiator IP address used to select and bind the local RDMA device.
- classmethod connect(host, subsystem_nqn, **kwargs)#
Connect to
subsystem_nqnathostand return a controller.- Parameters:
- Return type:
- identify_namespace(nsid)#
Identify
nsidand return its high-level I/O interface.Namespaces using separate metadata are rejected because this initiator currently transfers data blocks only.
- namespace(nsid)#
Identify
nsidand return its high-level I/O interface.Namespaces using separate metadata are rejected because this initiator currently transfers data blocks only.
- allocate(length)#
Allocate host memory registered for direct NVMe/RDMA I/O.
lengthis the positive allocation size in bytes. The returned buffer is owned by the controller’s I/O protection domain and must be closed before the controller.- Parameters:
length (int) –
- Return type:
- register(tensor)#
Register contiguous host tensor/array memory for direct I/O.
The returned MR retains the allocation and must be closed before the controller. Use
register_gpu()for CUDA tensors.
- register_gpu(tensor)#
Register CUDA tensor memory for direct NVMe/RDMA I/O.
The returned
ibverbs.cuda.GpuMRretainstensorand must be closed before the controller.
- close()#
Close the I/O and admin queues and release their RDMA resources.
- Return type:
None
- class nvmeof.ControllerInfo(serial, model, firmware, mdts, controller_id, version, max_commands, namespace_count, sgls, ioccsz, iorcsz, icdoff)#
Bases:
objectController fields consumed from a 4096-byte Identify response.
The record includes display identity, MDTS, controller and namespace limits, keyed-SGL capability bits, and NVMe-oF capsule sizing fields.
- Parameters:
- class nvmeof.HostBuffer(pd, length, access=None)#
Bases:
objectPinned host allocation registered with an NVMe/RDMA queue’s PD.
- Parameters:
pd – Protection domain used to register the allocation.
length (int) – Allocation size in bytes.
access – Optional verbs access mask. By default the buffer permits the local and remote operations required by NVMe/RDMA.
The buffer is a context manager. Close it before closing the protection domain or controller that owns
pd.- write(data, offset=0)#
Copy
datainto this allocation at byteoffset.
- read(length=None, offset=0)#
Copy bytes from this allocation into a new
bytesobject.lengthdefaults to all bytes fromoffsetthrough the end of the allocation.
- close()#
Deregister the memory region.
- Return type:
None
- class nvmeof.Namespace(controller, info)#
Bases:
objectAn identified NVM namespace accessed through a controller’s I/O queue.
infocontains the full identified geometry;nsidandlba_sizeprovide the fields most I/O callers need.- Parameters:
controller (Controller) –
info (p.NamespaceInfo) –
- read(buffer, slba, blocks=None, *, buffer_offset=0)#
Read namespace LBAs directly into a registered host/GPU buffer.
blocksdefaults to the whole remaining buffer and must describe a whole number of LBAs. Transfers exceeding MDTS are split into serial commands. A GPU read flushes completed GPUDirect writes before return.- Parameters:
buffer – Registered host MR or
ibverbs.cuda.GpuMR.slba (int) – Starting logical block address in the namespace.
blocks – Logical block count, or
Noneto use the remaining registered buffer.buffer_offset (int) – Starting byte offset in
buffer.
- Return type:
None
- write(buffer, slba, blocks=None, *, buffer_offset=0)#
Write namespace LBAs directly from a registered host/GPU buffer.
blocksdefaults to the whole remaining buffer and must describe a whole number of LBAs. Transfers exceeding MDTS are split into serial commands. CUDA work is synchronized before a GPU write is submitted.- Parameters:
buffer – Registered host MR or
ibverbs.cuda.GpuMR.slba (int) – Starting logical block address in the namespace.
blocks – Logical block count, or
Noneto use the remaining registered buffer.buffer_offset (int) – Starting byte offset in
buffer.
- Return type:
None
- flush()#
Commit volatile write data for this namespace to nonvolatile media.
- Return type:
None
- class nvmeof.NamespaceInfo(nsid, size_lbas, capacity_lbas, used_lbas, lba_size, metadata_size)#
Bases:
objectNamespace capacity and active LBA-format information.
- Parameters:
- classmethod from_bytes(nsid, data)#
Parse an Identify Namespace data structure for
nsid.- Parameters:
- Return type:
- exception nvmeof.NVMeStatusError(completion)#
Bases:
OSErrorAn NVMe command completed with a non-zero status.
- Parameters:
completion (Completion) –
- exception nvmeof.QueueFullError#
Bases:
RuntimeErrorRaised when every command ID on an NVMe/RDMA queue is in use.
- class nvmeof.RDMAQueue(host, port, qid, depth, controller_id=0, source=None)#
Bases:
objectOne connected NVMe/RDMA submission/completion queue pair.
This is the low-level command transport used by
Controller. Queue construction resolves the route, creates an RC QP, connects with NVMe/RDMA private data, and posts the response receives.- Parameters:
host (str) – Target hostname or IP address.
port (int) – Target NVMe/RDMA service port.
qid (int) – NVMe queue identifier. Zero creates an admin queue.
depth (int) – Queue depth from 2 through 256. At most
depth - 1commands may be outstanding.controller_id (int) – Connected controller ID for an I/O queue.
source – Optional initiator IP address used to select the local HCA.
- submit(command, data_owner=None)#
Submit one 64-byte command capsule without waiting.
The queue assigns and writes the command ID.
data_ownershould own any buffer addressed by the command’s keyed SGL so it remains alive until the returned request completes. Callpoll()to advance and retrieve completed requests.- Raises:
QueueFullError – If
depth - 1commands are already outstanding.- Parameters:
command (bytes) –
- Return type:
- poll(max_entries=None)#
Poll transport completions and return newly completed requests.
A request is returned only after its capsule SEND completion and matching NVMe response have both arrived. This method does not block;
max_entriesdefaults to enough entries to drain the CQ.
- execute(command, data_owner=None, timeout=30.0)#
Submit a command and wait for its successful NVMe completion.
- Parameters:
- Returns:
The matching
Completion.- Raises:
TimeoutError – If the request does not finish before
timeout.nvmeof.protocol.NVMeStatusError – If the target returns a non-zero NVMe status.
- close()#
Disconnect and close every RDMA resource owned by this queue.
- Return type:
None
- class nvmeof.Request(command_id, data_owner=None, send_complete=False, response=None, done=False)#
Bases:
objectState retained for an asynchronously submitted NVMe command.
A request becomes
doneonly after both the capsule SEND and the matching NVMe response complete.data_ownerkeeps any keyed-SGL allocation alive while the command is outstanding. Callers should treat the state fields as read-only.- Parameters:
command_id (int) –
data_owner (object) –
send_complete (bool) –
response (Completion | None) –
done (bool) –
- response: Completion | None = None#
NVMe/RDMA transport#
NVMe/RDMA queue transport built directly on ibverbs.
- exception nvmeof.rdma.QueueFullError#
Bases:
RuntimeErrorRaised when every command ID on an NVMe/RDMA queue is in use.
- class nvmeof.rdma.HostBuffer(pd, length, access=None)#
Bases:
objectPinned host allocation registered with an NVMe/RDMA queue’s PD.
- Parameters:
pd – Protection domain used to register the allocation.
length (int) – Allocation size in bytes.
access – Optional verbs access mask. By default the buffer permits the local and remote operations required by NVMe/RDMA.
The buffer is a context manager. Close it before closing the protection domain or controller that owns
pd.- write(data, offset=0)#
Copy
datainto this allocation at byteoffset.
- read(length=None, offset=0)#
Copy bytes from this allocation into a new
bytesobject.lengthdefaults to all bytes fromoffsetthrough the end of the allocation.
- close()#
Deregister the memory region.
- Return type:
None
- class nvmeof.rdma.Request(command_id, data_owner=None, send_complete=False, response=None, done=False)#
Bases:
objectState retained for an asynchronously submitted NVMe command.
A request becomes
doneonly after both the capsule SEND and the matching NVMe response complete.data_ownerkeeps any keyed-SGL allocation alive while the command is outstanding. Callers should treat the state fields as read-only.- Parameters:
command_id (int) –
data_owner (object) –
send_complete (bool) –
response (Completion | None) –
done (bool) –
- response: Completion | None = None#
- class nvmeof.rdma.RDMAQueue(host, port, qid, depth, controller_id=0, source=None)#
Bases:
objectOne connected NVMe/RDMA submission/completion queue pair.
This is the low-level command transport used by
Controller. Queue construction resolves the route, creates an RC QP, connects with NVMe/RDMA private data, and posts the response receives.- Parameters:
host (str) – Target hostname or IP address.
port (int) – Target NVMe/RDMA service port.
qid (int) – NVMe queue identifier. Zero creates an admin queue.
depth (int) – Queue depth from 2 through 256. At most
depth - 1commands may be outstanding.controller_id (int) – Connected controller ID for an I/O queue.
source – Optional initiator IP address used to select the local HCA.
- submit(command, data_owner=None)#
Submit one 64-byte command capsule without waiting.
The queue assigns and writes the command ID.
data_ownershould own any buffer addressed by the command’s keyed SGL so it remains alive until the returned request completes. Callpoll()to advance and retrieve completed requests.- Raises:
QueueFullError – If
depth - 1commands are already outstanding.- Parameters:
command (bytes) –
- Return type:
- poll(max_entries=None)#
Poll transport completions and return newly completed requests.
A request is returned only after its capsule SEND completion and matching NVMe response have both arrived. This method does not block;
max_entriesdefaults to enough entries to drain the CQ.
- execute(command, data_owner=None, timeout=30.0)#
Submit a command and wait for its successful NVMe completion.
- Parameters:
- Returns:
The matching
Completion.- Raises:
TimeoutError – If the request does not finish before
timeout.nvmeof.protocol.NVMeStatusError – If the target returns a non-zero NVMe status.
- close()#
Disconnect and close every RDMA resource owned by this queue.
- Return type:
None
Protocol#
NVMe, NVMe-oF, and NVMe/RDMA wire layouts used by the initiator.
- nvmeof.protocol.NVME_RDMA_PORT = 4420#
IANA service port for NVMe over Fabrics/RDMA.
- nvmeof.protocol.ADMIN_QUEUE_DEPTH = 32#
Queue depth used for the controller’s admin queue.
- nvmeof.protocol.MAX_QUEUE_DEPTH = 256#
Largest queue supported by this initiator’s command-ID layout.
- nvmeof.protocol.MAX_KEYED_SGL_LENGTH = 16777215#
Largest byte count representable by one keyed SGL descriptor.
- nvmeof.protocol.OPC_FLUSH = 0#
NVM Flush opcode.
- nvmeof.protocol.OPC_WRITE = 1#
NVM Write opcode.
- nvmeof.protocol.OPC_READ = 2#
NVM Read opcode.
- nvmeof.protocol.OPC_IDENTIFY = 6#
Admin Identify opcode.
- nvmeof.protocol.OPC_SET_FEATURES = 9#
Admin Set Features opcode.
- nvmeof.protocol.OPC_FABRICS = 127#
Fabrics command opcode.
- nvmeof.protocol.FCTYPE_PROPERTY_SET = 0#
Fabrics Property Set command type.
- nvmeof.protocol.FCTYPE_CONNECT = 1#
Fabrics Connect command type.
- nvmeof.protocol.FCTYPE_PROPERTY_GET = 4#
Fabrics Property Get command type.
- nvmeof.protocol.REG_CAP = 0#
Controller Capabilities property offset.
- nvmeof.protocol.REG_VS = 8#
Controller Version property offset.
- nvmeof.protocol.REG_CC = 20#
Controller Configuration property offset.
- nvmeof.protocol.REG_CSTS = 28#
Controller Status property offset.
- nvmeof.protocol.CC_ENABLE = 1#
Controller Configuration enable bit.
- nvmeof.protocol.CC_CSS_CSI = 96#
Controller Configuration command-set selection for CSI.
- nvmeof.protocol.CC_MPS_SHIFT = 7#
Controller Configuration memory-page-size shift.
- nvmeof.protocol.CC_IOSQES = 393216#
Controller Configuration I/O submission queue entry size.
- nvmeof.protocol.CC_IOCQES = 4194304#
Controller Configuration I/O completion queue entry size.
- nvmeof.protocol.CSTS_READY = 1#
Controller Status ready bit.
- nvmeof.protocol.CSTS_FATAL = 2#
Controller Status fatal-status bit.
- nvmeof.protocol.FEAT_NUMBER_OF_QUEUES = 7#
Set Features identifier for Number of Queues.
- nvmeof.protocol.PSDT_SGL = 64#
Command data-pointer flag selecting an SGL.
- nvmeof.protocol.KEYED_DATA_BLOCK = 64#
NVMe/RDMA keyed data block SGL descriptor type.
- exception nvmeof.protocol.NVMeStatusError(completion)#
Bases:
OSErrorAn NVMe command completed with a non-zero status.
- Parameters:
completion (Completion) –
- class nvmeof.protocol.Completion(result, sq_head, sq_id, command_id, status_field)#
Bases:
objectDecoded 16-byte NVMe completion queue entry.
status_fieldretains the phase bit exactly as received. The derived status properties remove that bit and expose the NVMe status code, status code type, and do-not-retry flag.- classmethod from_bytes(data)#
Decode the first 16 bytes of an NVMe completion queue entry.
- Parameters:
data (bytes) –
- Return type:
- raise_for_status()#
Raise
NVMeStatusErrorunless the command succeeded.- Return type:
None
- class nvmeof.protocol.ControllerInfo(serial, model, firmware, mdts, controller_id, version, max_commands, namespace_count, sgls, ioccsz, iorcsz, icdoff)#
Bases:
objectController fields consumed from a 4096-byte Identify response.
The record includes display identity, MDTS, controller and namespace limits, keyed-SGL capability bits, and NVMe-oF capsule sizing fields.
- Parameters:
- class nvmeof.protocol.NamespaceInfo(nsid, size_lbas, capacity_lbas, used_lbas, lba_size, metadata_size)#
Bases:
objectNamespace capacity and active LBA-format information.
- Parameters:
- classmethod from_bytes(nsid, data)#
Parse an Identify Namespace data structure for
nsid.- Parameters:
- Return type:
- nvmeof.protocol.set_keyed_sgl(command, buffer, length=None, offset=0)#
Point a command at a registered host MR or
ibverbs.cuda.GpuMR.buffermust exposeaddr,length, andrkey. The selected range must fit one 24-bit keyed data block descriptor.- Parameters:
command (bytearray) –
- Return type:
None
- nvmeof.protocol.rdma_cm_request(qid, depth, controller_id=0)#
Build the 32-byte NVMe/RDMA connection-management request record.
The admin queue (
qid=0) always encodes controller ID zero. I/O queues encode the ID returned by the admin Fabrics Connect command.
- nvmeof.protocol.parse_rdma_cm_response(data)#
Validate NVMe/RDMA CM response data and return its receive queue size.
- nvmeof.protocol.connect_data(host_id, subsystem_nqn, host_nqn, controller_id)#
Build the 1024-byte Fabrics Connect data structure.
host_idaccepts auuid.UUIDor UUID string. Both NQNs must contain 11 through 223 non-NUL bytes when UTF-8 encoded.
- nvmeof.protocol.fabrics_connect(qid, depth, data_buffer, kato_ms=0)#
Build a Fabrics Connect command using
data_bufferas its payload.
- nvmeof.protocol.property_get(offset, size)#
Build a Fabrics Property Get command for a 4- or 8-byte register.
- nvmeof.protocol.property_set(offset, value, size=4)#
Build a Fabrics Property Set command for a 4- or 8-byte register.
- nvmeof.protocol.identify(data_buffer, *, nsid=0, controller=False)#
Build an Identify Controller or Identify Namespace command.
data_buffermust provide at least 4096 registered bytes. Passcontroller=Truefor controller data; otherwisensidselects the namespace.
- nvmeof.protocol.set_features(feature_id, value)#
Build a Set Features command with command-dword 11
value.
- nvmeof.protocol.rw_command(opcode, nsid, slba, blocks, buffer, *, lba_size, buffer_offset=0)#
Build one NVM Read or Write command with a keyed SGL.
A command may transfer 1 through 65536 logical blocks, subject to the 24-bit keyed-SGL length limit. Higher-level namespace I/O splits larger transfers before calling this builder.