hw/nvme: Support for Namespaces Management from guest OS - create-ns
Subsystems (2)
PATCH v2 1/1 hw/nvme: Support for Namespaces Management from guest OS - create-ns
Added support for NVMEe NameSpaces Mangement allowing the guest OS to create, delete namespaces by issuing create-ns and delet-ns commands. It is an extension to currently implemented Qemu nvme virtual device. Virtual devices representing namespaces will be created and/or deleted during Qemu's running session, at anytime.
The implementation of the Namespaces Management consists of two patches:
First: create-ns (this patch)
Second: delete-ns (a patch will follow)
Description:
Currently namespaces could be defined as follows:
1. Legacy Namespace - just one namespace within Nvme controller's where the back-end was specified for nvme device by -drive parameter pointing directly to the image file.
2. Additional Namespaces - specified by nvme-ns devices each having its own back-end. To have multiple namespaces each needed to be specified at Qemu's command line being associated with the most recently defined nvme-bus from nvme device.
If a such additional namespace should be attached and/or detached by the guest OS, nvme controller has to be linked with another device nvme-subsys.
All that have a static behavior, all need to be specified at the Qemu's command line, all specified virtual nvme entities will be processed during Qemu's start-up created and provided to the guest OS.
To have a support for nvme create-ns and delete-ns commands with specified parameters, as the NVMe specification defines, a different approach is needed. Virtual devices representing namespaces need to be created and/or deleted during Qemu's running session, at anytime. The back-end image sizes for a namespace must accommodate the payload size and size of metadata resulted from specified parameters. And the total capacity of the nvme controller together with un-allocated capacity needs to be taken into account and updated following the commands nvme create-ns and delete-ns respectively.
Here is the approach:
The nvme device will get new parameters:
- auto-ns-path, specifies the path to to image and necessary configuration files.
- auto-ns-purge, controls behavior when delete-ns command is issued. If set to 'on' the associated back-end images will be deleted, otherwise such will be preserved as backup files (not Qemu backup files)
- auto-ns-tnvmcap, specifies total controller's space pool in bytes that can be allocated for namespaces, usually when nvme device is created first time. When Qemu restarted this parameter could be omitted.
The virtual devices representing namespace will be created during the Qemu running session dynamically. QOM classes and instances will be created utilizing existing configuration scheme already used for Qemu's start-up. Back-end images will be created and associated with QOM namespaces (virtual instances) or disassociated deleted or renamed. Also it is assured that all settings will remain persistent over Qemu start-ups and shutdowns. The implementation makes it possible to combine the existing "Additional Namespace" implementation with the new "Managed Namespaces", those will coexist with obvious restrictions, like both will share the same NsIds space, "static" namespaces will not be deleted etc..
--- docs/system/devices/nvme.rst | 24 ++- hw/nvme/cfg_key_checker.c | 51 ++++++ hw/nvme/ctrl.c | 168 +++++++++++++++++++- hw/nvme/meson.build | 2 +- hw/nvme/ns-backend.c | 293 +++++++++++++++++++++++++++++++++++ hw/nvme/ns.c | 232 +++++++++++++++++++++++---- hw/nvme/nvme.h | 16 +- hw/nvme/trace-events | 2 + include/block/nvme.h | 30 ++++ 9 files changed, 783 insertions(+), 35 deletions(-) create mode 100644 hw/nvme/cfg_key_checker.c create mode 100644 hw/nvme/ns-backend.c
docs/system/devices/nvme.rst +23 -1
@@ -92,6 +92,28 @@ There are a number of parameters available: attach the namespace to a specific ``nvme`` device (identified by an ``id`` parameter on the controller device). +Additional Namespaces managed by guest OS Namespaces management +--------------------------------------------------------------------- + +.. code-block:: console + + -device nvme,id=nvme-ctrl,serial=1234,subsys=nvme-subsys,auto-ns-path=path + +Parameters: + +``auto-ns-path=<path to nvme storage location>`` + If specified indicates that support for dynamic management of nvme namespaces + through nvme create-ns and nvme delete-ns commands is turned on. The path to + the storage area for backend images, must exist. The controller has to be + linked to an NVM Subsystem device, ``nvme-subsys`` must be specified, + whereas parameter ``drive`` must not be specified. + The legacy namespace backend is disabled, instead a pair of files + "nvme_<ctrl SN>_ns_<NS-ID>.cfg" and "nvme_<ctrl SN>_ns_<NS-ID>.img" will + be created within the storage area following nvme create-ns command issued + at the guest side. Image files will be created in raw format, config files + contain namespace parameters and state of attachment allowing QEMU to + configure namespaces accordingly during its start up. + NVM Subsystems --------------
@@ -320,4 +342,4 @@ controller are: .. code-block:: console - echo 0000:01:00.1 > /sys/bus/pci/drivers/nvme/bind + echo 0000:01:00.1 > /sys/bus/pci/drivers/nvme/bind
hw/nvme/cfg_key_checker.c new file +51 -0
51 lines not on this page · raw message
hw/nvme/ctrl.c +165 -3
240 lines not on this page · raw message
hw/nvme/meson.build +1 -1
@@ -1 +1 @@ -softmmu_ss.add(when: 'CONFIG_NVME_PCI', if_true: files('ctrl.c', 'dif.c', 'ns.c', 'subsys.c')) +softmmu_ss.add(when: 'CONFIG_NVME_PCI', if_true: files('ctrl.c', 'dif.c', 'ns.c', 'subsys.c', 'ns-backend.c', 'cfg_key_checker.c'))
hw/nvme/ns-backend.c new file +293 -0
293 lines not on this page · raw message
hw/nvme/ns.c +203 -29
282 lines not on this page · raw message
hw/nvme/nvme.h +15 -1
@@ -279,6 +279,8 @@ int nvme_ns_setup(NvmeNamespace *ns, Error **errp);void nvme_ns_drain (NvmeNamespace *ns);void nvme_ns_shutdown (NvmeNamespace *ns);void nvme_ns_cleanup (NvmeNamespace *ns); +void nvme_validate_flbas (uint8_t flbas,Error **errp); +NvmeNamespace *nvme_ns_create (NvmeCtrl *n,uint32_t nsid,NvmeIdNsMgmt *id_ns,Error **errp); typedef struct NvmeAsyncEvent { QTAILQ_ENTRY(NvmeAsyncEvent) entry;
@@ -339,6 +341,7 @@ static inline const char *nvme_adm_opc_str(uint8_t opc)case NVME_ADM_CMD_SET_FEATURES :return "NVME_ADM_CMD_SET_FEATURES" ;case NVME_ADM_CMD_GET_FEATURES :return "NVME_ADM_CMD_GET_FEATURES" ;case NVME_ADM_CMD_ASYNC_EV_REQ :return "NVME_ADM_CMD_ASYNC_EV_REQ" ; +case NVME_ADM_CMD_NS_MGMT :return "NVME_ADM_CMD_NS_MGMT" ;case NVME_ADM_CMD_NS_ATTACHMENT :return "NVME_ADM_CMD_NS_ATTACHMENT" ;case NVME_ADM_CMD_VIRT_MNGMT :return "NVME_ADM_CMD_VIRT_MNGMT" ;case NVME_ADM_CMD_DBBUF_CONFIG :return "NVME_ADM_CMD_DBBUF_CONFIG" ;
@@ -427,6 +430,8 @@ typedef struct NvmeParams {uint16_t sriov_vi_flexible;uint8_t sriov_max_vq_per_vf;uint8_t sriov_max_vi_per_vf; +char *ns_directory;/* if empty (default) one legacy ns will be created */ +bool ns_purge;/* allowing purging of auto ns images if ns deleted */ } NvmeParams; typedef struct NvmeCtrl {
@@ -485,7 +490,7 @@ typedef struct NvmeCtrl {NvmeSubsystem *subsys; -NvmeNamespace namespace; +NvmeNamespace namespace;/* if ns_directory is empty this will be used */ NvmeNamespace *namespaces[NVME_MAX_NAMESPACES +1 ];NvmeSQueue **sq;NvmeCQueue **cq;
@@ -575,6 +580,9 @@ static inline NvmeSecCtrlEntry *nvme_sctrl_for_cntlid(NvmeCtrl *n,return NULL ; } +BlockBackend *ns_blockdev_init (const char *file, +uint64_t img_size,Error **errp); +int nvme_ns_backend_setup (NvmeCtrl *n,Error **errp);void nvme_attach_ns (NvmeCtrl *n,NvmeNamespace *ns);uint16_t nvme_bounce_data (NvmeCtrl *n,void *ptr,uint32_t len,NvmeTxDirection dir,NvmeRequest *req);
@@ -583,5 +591,11 @@ uint16_t nvme_bounce_mdata(NvmeCtrl *n, void *ptr, uint32_t len,void nvme_rw_complete_cb (void *opaque,int ret);uint16_t nvme_map_dptr (NvmeCtrl *n,NvmeSg *sg,size_t len,NvmeCmd *cmd); +char *ns_create_image_name (NvmeCtrl *n,uint32_t nsid,Error **errp); +int ns_auto_check (NvmeCtrl *n,NvmeNamespace *ns,uint32_t nsid); +int ns_cfg_save (NvmeCtrl *n,NvmeNamespace *ns,uint32_t nsid); +int ns_cfg_load (NvmeCtrl *n,NvmeNamespace *ns,uint32_t nsid); +int64_t qdict_get_int_chkd (const QDict *qdict,const char *key,Error **errp); +bool qdict_get_bool_chkd (const QDict *qdict,const char *key,Error **errp);#endif /* HW_NVME_NVME_H */
hw/nvme/trace-events +2 -0
@@ -77,6 +77,8 @@ pci_nvme_aer(uint16_t cid) "cid %"PRIu16"" pci_nvme_aer_aerl_exceeded(void) "aerl exceeded" pci_nvme_aer_masked(uint8_t type, uint8_t mask) "type 0x%"PRIx8" mask 0x%"PRIx8"" pci_nvme_aer_post_cqe(uint8_t typ, uint8_t info, uint8_t log_page) "type 0x%"PRIx8" info 0x%"PRIx8" lid 0x%"PRIx8"" +pci_nvme_ns_mgmt(uint16_t cid, uint32_t nsid, uint8_t sel, uint8_t csi, uint8_t psdt) "cid %"PRIu16", nsid=%"PRIu32", sel=0x%"PRIx8", csi=0x%"PRIx8", psdt=0x%"PRIx8"" +pci_nvme_ns_create(uint16_t nsid, uint64_t nsze, uint64_t ncap, uint8_t flbas) "nsid %"PRIu16", nsze=%"PRIu64", ncap=%"PRIu64", flbas=%"PRIu8"" pci_nvme_ns_attachment(uint16_t cid, uint8_t sel) "cid %"PRIu16", sel=0x%"PRIx8"" pci_nvme_ns_attachment_attach(uint16_t cntlid, uint32_t nsid) "cntlid=0x%"PRIx16", nsid=0x%"PRIx32"" pci_nvme_enqueue_event(uint8_t typ, uint8_t info, uint8_t log_page) "type 0x%"PRIx8" info 0x%"PRIx8" lid 0x%"PRIx8""
include/block/nvme.h +30 -0
@@ -592,6 +592,7 @@ enum NvmeAdminCommands {NVME_ADM_CMD_SET_FEATURES =0x09 ,NVME_ADM_CMD_GET_FEATURES =0x0a ,NVME_ADM_CMD_ASYNC_EV_REQ =0x0c , +NVME_ADM_CMD_NS_MGMT =0x0d ,NVME_ADM_CMD_ACTIVATE_FW =0x10 ,NVME_ADM_CMD_DOWNLOAD_FW =0x11 , NVME_ADM_CMD_NS_ATTACHMENT = 0x15,
@@ -897,14 +898,17 @@ enum NvmeStatusCodes {NVME_FEAT_NOT_CHANGEABLE =0x010e ,NVME_FEAT_NOT_NS_SPEC =0x010f ,NVME_FW_REQ_SUSYSTEM_RESET =0x0110 , +NVME_NS_IDNTIFIER_UNAVAIL =0x0116 ,NVME_NS_ALREADY_ATTACHED =0x0118 ,NVME_NS_PRIVATE =0x0119 ,NVME_NS_NOT_ATTACHED =0x011a , +NVME_THIN_PROVISION_NOTSPRD =0x011b ,NVME_NS_CTRL_LIST_INVALID =0x011c ,NVME_INVALID_CTRL_ID =0x011f ,NVME_INVALID_SEC_CTRL_STATE =0x0120 ,NVME_INVALID_NUM_RESOURCES =0x0121 ,NVME_INVALID_RESOURCE_ID =0x0122 , +NVME_NS_ATTACH_MGMT_NOTSPRD =0x0129 ,NVME_CONFLICTING_ATTRS =0x0180 ,NVME_INVALID_PROT_INFO =0x0181 , NVME_WRITE_TO_RO = 0x0182,
@@ -1184,6 +1188,11 @@ enum NvmeIdCtrlCmic { NVME_CMIC_MULTI_CTRL = 1 << 1, }; +enum NvmeNsManagementOperation { +NVME_NS_MANAGEMENT_CREATE =0x0 , +NVME_NS_MANAGEMENT_DELETE =0x1 , +}; +enum NvmeNsAttachmentOperation {NVME_NS_ATTACHMENT_ATTACH =0x0 ,NVME_NS_ATTACHMENT_DETACH =0x1 ,
@@ -1345,6 +1354,26 @@ typedef struct QEMU_PACKED NvmeIdNs {uint8_t vs[3712 ]; } NvmeIdNs; +typedef struct QEMU_PACKED NvmeIdNsMgmt { +uint64_t nsze; +uint64_t ncap; +uint8_t rsvd16[10 ]; +uint8_t flbas; +uint8_t rsvd27[2 ]; +uint8_t dps; +uint8_t nmic; +uint8_t rsvd31[61 ]; +uint32_t anagrpid; +uint8_t rsvd96[4 ]; +uint16_t nvmsetid; +uint16_t endgid; +uint8_t rsvd104[280 ]; +uint64_t lbstm; +uint8_t rsvd392[120 ]; +uint8_t rsvd512[512 ]; +uint8_t vs[3072 ]; +} NvmeIdNsMgmt; +#define NVME_ID_NS_NVM_ELBAF_PIF (elbaf) (((elbaf) >> 7) & 0x3) typedef struct QEMU_PACKED NvmeIdNsNvm {
@@ -1646,6 +1675,7 @@ static inline void _nvme_check_size(void)QEMU_BUILD_BUG_ON (sizeof (NvmeLBAF) !=4 );QEMU_BUILD_BUG_ON (sizeof (NvmeLBAFE) !=16 );QEMU_BUILD_BUG_ON (sizeof (NvmeIdNs) !=4096 ); +QEMU_BUILD_BUG_ON (sizeof (NvmeIdNsMgmt) !=4096 );QEMU_BUILD_BUG_ON (sizeof (NvmeIdNsNvm) !=4096 );QEMU_BUILD_BUG_ON (sizeof (NvmeIdNsZoned) !=4096 );QEMU_BUILD_BUG_ON (sizeof (NvmeSglDescriptor) !=16 );
Signature · 1 line
2.37.1
Hi Michael,
Thanks for your interest in contributing this!
(CC: +Naveen, +Keith, -linux-nvme)
Some observations and commets on your patch submission below.
Firstly, your mail delivery is broken - this is not getting through to qemu-devel. Also, I don't think kernel developers have that much interest in this, so no need to CC linux-nvme.
Also, please CC all maintainers (see ./scripts/get_maintainer.pl).
Quoted 10 lines · Michael Kropaczek
Added support for NVMEe NameSpaces Mangement allowing the guest OS to create, delete namespaces by issuing create-ns and delet-ns commands. It is an extension to currently implemented Qemu nvme virtual device. Virtual devices representing namespaces will be created and/or deleted during Qemu's running session, at anytime.
The implementation of the Namespaces Management consists of two patches:
First: create-ns (this patch)
Second: delete-ns (a patch will follow)
1. Please format the commit message without the indendations.
2. You do not need to state the patches belonging to the series (that goes into the cover letter - see below).
Quoted 39 lines
Signed-off-by Michael Kropaczek <[email protected]>Description:
Currently namespaces could be defined as follows:
1. Legacy Namespace - just one namespace within Nvme controller's where the back-end was specified for nvme device by -drive parameter pointing directly to the image file.
2. Additional Namespaces - specified by nvme-ns devices each having its own back-end. To have multiple namespaces each needed to be specified at Qemu's command line being associated with the most recently defined nvme-bus from nvme device.
If a such additional namespace should be attached and/or detached by the guest OS, nvme controller has to be linked with another device nvme-subsys.All that have a static behavior, all need to be specified at the Qemu's command line, all specified virtual nvme entities will be processed during Qemu's start-up created and provided to the guest OS.
To have a support for nvme create-ns and delete-ns commands with specified parameters, as the NVMe specification defines, a different approach is needed. Virtual devices representing namespaces need to be created and/or deleted during Qemu's running session, at anytime. The back-end image sizes for a namespace must accommodate the payload size and size of metadata resulted from specified parameters. And the total capacity of the nvme controller together with un-allocated capacity needs to be taken into account and updated following the commands nvme create-ns and delete-ns respectively.
Here is the approach:
The nvme device will get new parameters:
- auto-ns-path, specifies the path to to image and necessary configuration files.
- auto-ns-purge, controls behavior when delete-ns command is issued. If set to 'on' the associated back-end images will be deleted, otherwise such will be preserved as backup files (not Qemu backup files)
- auto-ns-tnvmcap, specifies total controller's space pool in bytes that can be allocated for namespaces, usually when nvme device is created first time. When Qemu restarted this parameter could be omitted.
Quoted 11 lines
The virtual devices representing namespace will be created during the Qemu running session dynamically. QOM classes and instances will be created utilizing existing configuration scheme already used for Qemu's start-up. Back-end images will be created and associated with QOM namespaces (virtual instances) or disassociated deleted or renamed. Also it is assured that all settings will remain persistent over Qemu start-ups and shutdowns. The implementation makes it possible to combine the existing "Additional Namespace" implementation with the new "Managed Namespaces", those will coexist with obvious restrictions, like both will share the same NsIds space, "static" namespaces will not be deleted etc..
This should go into your "cover letter". See git format-patch --cover-letter.
Your two patches should be sent as a series with the above as the cover letter. Each patch should carry a commit message explaining why it is being done and why it is being done in a particular way, but the cover letter should (if required) elaborate on your motivation and approach for the series as a whole.
git format-patch and git send-email can do this properly for you. Alternatively, you can use git-publish which is a nice frontend for working with patch series.
Now, as for the patches themselves. There is actually already a patch for namespace management on the list - see[1] for a 7.1-based tree with this applied. The approach there is to pre-add a set of namespaces with zero sized images (indicating that they are "unallocated") and then resize them on namespace create (using blk_truncate). This limits the number of creatable namespaces, but this
is indicated in the MN field.
The core issue with that patch is that it blocks the QEMU main thread while the truncation is going on, which is not good and which is why this has never been merged. However, I just peeked into block/block-backend.c and it looks like truncate could be used with AIO (though currently not wired up).
The same core blocking issues exist in your approach. Also, doing filesystem stuff is a layering violation. The emulated devices really should not break out of their "bubble", even though they can.
However! I'm a little intrigued by your approach to storing persistent state by writing out a config file and auto loading it. I have been considering various ways of doing that for some time since there are other areas where we need persistent state like that (format nvm is one example, zone state another).
[1]: https://github.com/OpenMPDK/qemu/commit/a7357a4c553f4106e76e3ec052205659bcc5d741
Cheers,
Klaus